From da3cf924801a5d2188027719d5fd0bdfc0d276b9 Mon Sep 17 00:00:00 2001 From: David Coutadeur Date: Wed, 27 Mar 2024 16:00:58 +0100 Subject: [PATCH] add more tests (#8) --- tests/Ltb/AttributeValueTest.php | 8 +- tests/Ltb/LdapTest.php | 198 +++++++++++++++++++++++++++++-- tests/Ltb/ldap_search.php | 47 -------- 3 files changed, 191 insertions(+), 62 deletions(-) delete mode 100644 tests/Ltb/ldap_search.php diff --git a/tests/Ltb/AttributeValueTest.php b/tests/Ltb/AttributeValueTest.php index e248e30..05ad66f 100644 --- a/tests/Ltb/AttributeValueTest.php +++ b/tests/Ltb/AttributeValueTest.php @@ -6,15 +6,9 @@ // global variable for ldap_get_mail_for_notification function $GLOBALS['mail_attributes'] = array("mail"); -final class AttributeValueTest extends TestCase +final class AttributeValueTest extends \Mockery\Adapter\Phpunit\MockeryTestCase { - protected function tearDown(): void - { - // Useful for destroying the mock between two tests - Mockery::close(); - } - public function test_ldap_get_first_available_value(): void { diff --git a/tests/Ltb/LdapTest.php b/tests/Ltb/LdapTest.php index 4cede41..54cf6de 100644 --- a/tests/Ltb/LdapTest.php +++ b/tests/Ltb/LdapTest.php @@ -1,20 +1,13 @@ assertEquals($entries[1]['cn'][0], 'testcn1', "testcn1 has not been ordered correctly in entries array"); } + public function test_sorted_search_with_sort_control(): void + { + + $phpLDAPMock = Mockery::mock('overload:Ltb\PhpLDAP'); + + $phpLDAPMock->shouldreceive('ldap_read') + ->with("ldap_connection", '', '(objectClass=*)', ['supportedControl']) + ->andReturn("check"); + + $phpLDAPMock->shouldreceive('ldap_get_entries') + ->andReturnUsing( function ($ldap, $ldap_result) + { + if ($ldap_result == "check") { + return [ + 'count' => 1, + 0 => [ + 'count' => 1, + 0 => 'supportedcontrol', + 'supportedcontrol' => [ + 'count' => 1, + 0 => LDAP_CONTROL_SORTREQUEST + ] + ] + ]; + } + elseif($ldap_result == "ldap_search_result") + { + return [ + 'count' => 2, + 0 => [ + 'count' => 2, + 0 => 'cn', + 1 => 'sn', + 'cn' => [ + 'count' => 1, + 0 => 'testcn2' + ], + 'sn' => [ + 'count' => 1, + 0 => 'aaaa' + ] + ], + 1 => [ + 'count' => 2, + 0 => 'cn', + 1 => 'sn', + 'cn' => [ + 'count' => 1, + 0 => 'testcn1' + ], + 'sn' => [ + 'count' => 1, + 0 => 'zzzz' + ] + ] + ]; + + } + else + { + return "ldap_get_entries_error"; + } + } + ); + + $phpLDAPMock->shouldreceive('ldap_search') + ->with("ldap_connection", + "ou=people,dc=my-domain,dc=com", + "(objectClass=InetOrgPerson)", + ["cn", "sn"], + 0, + 1000, + -1, + LDAP_DEREF_NEVER, + [['oid' => LDAP_CONTROL_SORTREQUEST, 'value' => [['attr'=>'sn']]]] + ) + ->andReturn("ldap_search_result"); + + $phpLDAPMock->shouldreceive('ldap_errno') + ->with("ldap_connection") + ->andReturn(0); + + list($ldap_result,$errno,$entries) = Ltb\Ldap::sorted_search("ldap_connection", + "ou=people,dc=my-domain,dc=com", + "(objectClass=InetOrgPerson)", + ["cn", "sn"], + "sn", + 1000 + ); + + $this->assertEquals($ldap_result, "ldap_search_result", "error while getting ldap_search sorted result"); + $this->assertEquals($errno, 0, "error code invalid while getting ldap_search sorted result"); + $this->assertEquals($entries[0]['cn'][0], 'testcn2', "error while getting ldap_search sorted result: first entry is not testcn2"); + $this->assertEquals($entries[1]['cn'][0], 'testcn1', "error while getting ldap_search sorted result: second entry is not testcn1"); + + } + + public function test_sorted_search_without_sort_control(): void + { + + $phpLDAPMock = Mockery::mock('overload:Ltb\PhpLDAP'); + + $phpLDAPMock->shouldreceive('ldap_read') + ->with("ldap_connection", '', '(objectClass=*)', ['supportedControl']) + ->andReturn("check"); + + $phpLDAPMock->shouldreceive('ldap_get_entries') + ->andReturnUsing( function ($ldap, $ldap_result) + { + if ($ldap_result == "check") { + return [ + 'count' => 1, + 0 => [ + 'count' => 1, + 0 => 'supportedcontrol', + 'supportedcontrol' => [ + 'count' => 1, + 0 => LDAP_CONTROL_VLVREQUEST + ] + ] + ]; + } + elseif($ldap_result == "ldap_search_result") + { + return [ + 'count' => 2, + 0 => [ + 'count' => 2, + 0 => 'cn', + 1 => 'sn', + 'cn' => [ + 'count' => 1, + 0 => 'testcn1' + ], + 'sn' => [ + 'count' => 1, + 0 => 'zzzz' + ] + ], + 1 => [ + 'count' => 2, + 0 => 'cn', + 1 => 'sn', + 'cn' => [ + 'count' => 1, + 0 => 'testcn2' + ], + 'sn' => [ + 'count' => 1, + 0 => 'aaaa' + ] + ], + ]; + + } + else + { + return "ldap_get_entries_error"; + } + } + ); + $phpLDAPMock->shouldreceive('ldap_search') + ->with("ldap_connection", + "ou=people,dc=my-domain,dc=com", + "(objectClass=InetOrgPerson)", + ["cn", "sn"], + 0, + 1000 + ) + ->andReturn("ldap_search_result"); + + $phpLDAPMock->shouldreceive('ldap_errno') + ->with("ldap_connection") + ->andReturn(0); + + list($ldap_result,$errno,$entries) = Ltb\Ldap::sorted_search("ldap_connection", + "ou=people,dc=my-domain,dc=com", + "(objectClass=InetOrgPerson)", + ["cn", "sn"], + "sn", + 1000 + ); + + $this->assertEquals($ldap_result, "ldap_search_result", "error while getting ldap_search sorted result"); + $this->assertEquals($errno, 0, "error code invalid while getting ldap_search sorted result"); + $this->assertEquals($entries[0]['cn'][0], 'testcn2', "error while getting ldap_search sorted result: first entry is not testcn2"); + $this->assertEquals($entries[1]['cn'][0], 'testcn1', "error while getting ldap_search sorted result: second entry is not testcn1"); + + } } diff --git a/tests/Ltb/ldap_search.php b/tests/Ltb/ldap_search.php deleted file mode 100644 index 8d70050..0000000 --- a/tests/Ltb/ldap_search.php +++ /dev/null @@ -1,47 +0,0 @@ - 6, - [ "count" => 0, "dn" => "ou=People,dc=company,dc=example"], - [ "count" => 1, "uid" => ["count"=>1, 0 => "jho"], 0 => "uid", "dn" => "uid=jho,ou=People,dc=company,dc=example" ], - [ "count" => 1, "uid" => ["count"=>1, 0 => "bfranck"], 0 => "uid", "dn" => "uid=bfrank,ou=People,dc=company,dc=example" ], - [ "count" => 1, "uid" => ["count"=>1, 0 => "nkeith"], 0 =>"uid","dn" => "uid=nkeith,ou=People,dc=company,dc=example" ], - [ "count" => 1, "uid" => ["count"=>1, 0 => "amorton"], 0 =>"uid","dn" => "uid=amorton,ou=People,dc=company,dc=example" ], - [ "count" => 1, "uid" => ["count"=>1, 0 => "irangel"], 0 =>"uid","dn" => "uid=irangel,ou=People,dc=company,dc=example" ] -]; - - -var_dump($array); - -Ldap::ldapSort($array,'uid'); - -# dump ... -var_dump($array); -