Skip to content

Commit 885cf99

Browse files
authored
Merge pull request #756 from DirectoryTree/BUG-753
Bug 753 - Ensure model attribute values are numerically indexed upon creation
2 parents 3617d4b + 1779076 commit 885cf99

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/Models/Model.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -973,11 +973,18 @@ protected function performInsert(): void
973973

974974
$this->dispatch('creating');
975975

976+
// Some PHP versions prevent non-numerically indexed arrays
977+
// from being sent to the server. To resolve this, we will
978+
// convert the attributes to numerically indexed arrays.
979+
$attributes = array_map('array_values', array_filter($this->getAttributes()));
980+
976981
// Here we perform the insert of new object in the directory,
977982
// but filter out any empty attributes before sending them
978983
// to the server. LDAP servers will throw an exception if
979984
// attributes have been given empty or null values.
980-
$this->dn = $query->insertAndGetDn($this->getDn(), array_filter($this->getAttributes()));
985+
$this->dn = $query->insertAndGetDn($this->getDn(), $attributes);
986+
987+
$this->attributes = $attributes;
981988

982989
$this->dispatch('created');
983990

tests/Unit/Models/ModelTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,28 @@ public function test_generated_dns_properly_substitute_base_on_creation()
783783
$this->assertEquals('cn=foo,dc=local,dc=com', $model->getDn());
784784
}
785785

786+
public function test_attribute_values_are_numerically_indexed_on_create()
787+
{
788+
Container::addConnection(new Connection([
789+
'base_dn' => 'dc=local,dc=com',
790+
]));
791+
792+
DirectoryFake::setup()->getLdapConnection()->expect(
793+
LdapFake::operation('add')->once()->with(fn ($dn) => true, fn ($attributes) => (
794+
collect($attributes)->every(fn ($values) => array_is_list($values))
795+
))->andReturnTrue(),
796+
);
797+
798+
$model = new Entry;
799+
800+
$model->objectclass = ['bar'];
801+
$model->cn = ['invalid' => 'John Doe'];
802+
803+
$model->save();
804+
805+
$this->assertEquals($model->cn, ['John Doe']);
806+
}
807+
786808
public function test_setting_dn_attributes_set_distinguished_name_on_model()
787809
{
788810
$this->assertEquals('foo', (new Entry(['distinguishedname' => 'foo']))->getDn());

0 commit comments

Comments
 (0)