Skip to content

Commit 06244ce

Browse files
committed
Properly emulate LDAP delete attributes
DirectoryTree/LdapRecord#291
1 parent 072c094 commit 06244ce

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

src/Testing/Emulated/EmulatesModelQueries.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,15 @@ public function deleteAttributes($dn, array $attributes)
7777
if (! $model = $this->find($dn)) {
7878
return false;
7979
}
80-
81-
foreach ($attributes as $attribute) {
82-
$model->{$attribute} = null;
80+
81+
foreach ($attributes as $attribute => $value) {
82+
if (empty($value)) {
83+
$model->{$attribute} = null;
84+
} elseif (Arr::exists($model->{$attribute} ?? [], $attribute)) {
85+
$model->{$attribute} = array_values(
86+
array_diff($model->{$attribute}, (array) $value)
87+
);
88+
}
8389
}
8490

8591
return $model->save();

tests/Feature/Emulator/EmulatedModelQueryTest.php

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,29 @@ public function test_delete()
109109
$this->assertNull(TestModelStub::find($model->getDn()));
110110
}
111111

112+
public function test_delete_attribute()
113+
{
114+
$model = tap(new TestModelStub, function ($model) {
115+
$model->cn = 'John Doe';
116+
$model->foo = 'bar';
117+
$model->baz = 'set';
118+
$model->zal = 'ze';
119+
$model->save();
120+
});
121+
122+
$model->deleteAttribute('foo');
123+
$model->deleteAttribute(['baz', 'zal' => 'invalid']);
124+
125+
$this->assertNull($model->foo);
126+
$this->assertNull($model->baz);
127+
$this->assertEquals(['ze'], $model->zal);
128+
$this->assertEquals('John Doe', $model->cn[0]);
129+
130+
$model->deleteAttribute(['baz', 'zal' => 'ze']);
131+
132+
$this->assertEquals([], $model->zal);
133+
}
134+
112135
public function test_delete_attributes()
113136
{
114137
$model = tap(new TestModelStub, function ($model) {
@@ -122,7 +145,7 @@ public function test_delete_attributes()
122145
$this->assertEquals(['bar'], $model->foo);
123146
$this->assertEquals(['set'], $model->baz);
124147

125-
$model->deleteAttributes($model->getDn(), ['foo', 'baz']);
148+
$model->deleteAttributes($model->getDn(), ['foo' => [], 'baz' => []]);
126149

127150
$model = TestModelStub::find($model->getDn());
128151
$this->assertNull($model->foo);
@@ -232,8 +255,7 @@ public function test_get_only_returns_matching_object_classes()
232255
{
233256
TestModelStub::create(['cn' => ['John']]);
234257

235-
$model = new class extends Entry
236-
{
258+
$model = new class extends Entry {
237259
public static $objectClasses = ['three', 'four'];
238260
};
239261

@@ -589,14 +611,12 @@ public function test_domain_scoping()
589611
DirectoryEmulator::setup('alpha');
590612
DirectoryEmulator::setup('bravo');
591613

592-
$alpha = new class extends Entry
593-
{
614+
$alpha = new class extends Entry {
594615
protected $connection = 'alpha';
595616
public static $objectClasses = ['one', 'two'];
596617
};
597618

598-
$bravo = new class extends Entry
599-
{
619+
$bravo = new class extends Entry {
600620
protected $connection = 'bravo';
601621
public static $objectClasses = ['one', 'two'];
602622
};

0 commit comments

Comments
 (0)