Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d798080

Browse files
author
Kyle Evans
committed
[Ldap] Add pagination tests
There are only a couple of pagination scenarios to test: - That we can make paged queries following non-paged - That we can make non-paged queries following paged - That responses are indeed being broken up into the number of sets we're expecting - That the different size restrictions operate in a sane manner with respect to each other
1 parent c20d1b1 commit d798080

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

src/Symfony/Component/Ldap/Tests/Adapter/ExtLdap/AdapterTest.php

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Ldap\Adapter\ExtLdap\Collection;
1616
use Symfony\Component\Ldap\Adapter\ExtLdap\Query;
1717
use Symfony\Component\Ldap\Entry;
18+
use Symfony\Component\Ldap\Exception\LdapException;
1819
use Symfony\Component\Ldap\Exception\NotBoundException;
1920
use Symfony\Component\Ldap\LdapInterface;
2021

@@ -23,6 +24,12 @@
2324
*/
2425
class AdapterTest extends LdapTestCase
2526
{
27+
const PAGINATION_REQUIRED_CONFIG = [
28+
'options' => [
29+
'protocol_version' => 3,
30+
],
31+
];
32+
2633
public function testLdapEscape()
2734
{
2835
$ldap = new Adapter();
@@ -111,4 +118,121 @@ public function testLdapQueryScopeOneLevel()
111118
$this->assertEquals($one_level_result->count(), 1);
112119
$this->assertEquals($one_level_result[0]->getAttribute('ou'), ['Ldap']);
113120
}
121+
122+
public function testLdapPagination()
123+
{
124+
$ldap = new Adapter(array_merge($this->getLdapConfig(), static::PAGINATION_REQUIRED_CONFIG));
125+
$ldap->getConnection()->bind('cn=admin,dc=symfony,dc=com', 'symfony');
126+
$entries = $this->setupTestUsers($ldap);
127+
128+
$unpaged_query = $ldap->createQuery('dc=symfony,dc=com', '(&(objectClass=applicationProcess)(cn=user*))', [
129+
'scope' => Query::SCOPE_ONE,
130+
]);
131+
$fully_paged_query = $ldap->createQuery('dc=symfony,dc=com', '(&(objectClass=applicationProcess)(cn=user*))', [
132+
'scope' => Query::SCOPE_ONE,
133+
'pageSize' => 25,
134+
]);
135+
$paged_query = $ldap->createQuery('dc=symfony,dc=com', '(&(objectClass=applicationProcess)(cn=user*))', [
136+
'scope' => Query::SCOPE_ONE,
137+
'pageSize' => 5,
138+
]);
139+
140+
try {
141+
$unpaged_results = $unpaged_query->execute();
142+
$fully_paged_results = $fully_paged_query->execute();
143+
$paged_results = $paged_query->execute();
144+
145+
// All four of the above queries should result in the 25 'users' being returned
146+
$this->assertEquals($unpaged_results->count(), 25);
147+
$this->assertEquals($fully_paged_results->count(), 25);
148+
$this->assertEquals($paged_results->count(), 25);
149+
150+
// They should also result in 1 or 25 / pageSize results
151+
$this->assertEquals(\count($unpaged_query->getResources()), 1);
152+
$this->assertEquals(\count($fully_paged_query->getResources()), 1);
153+
$this->assertEquals(\count($paged_query->getResources()), 5);
154+
155+
if (PHP_MAJOR_VERSION > 7 || (PHP_MAJOR_VERSION == 7 && PHP_MINOR_VERSION >= 2)) {
156+
// This last query is to ensure that we haven't botched the state of our connection
157+
// by not resetting pagination properly. extldap <= PHP 7.1 do not implement the necessary
158+
// bits to work around an implementation flaw, so we simply can't guarantee this to work there.
159+
$final_query = $ldap->createQuery('dc=symfony,dc=com', '(&(objectClass=applicationProcess)(cn=user*))', [
160+
'scope' => Query::SCOPE_ONE,
161+
]);
162+
163+
$final_results = $final_query->execute();
164+
165+
$this->assertEquals($final_results->count(), 25);
166+
$this->assertEquals(\count($final_query->getResources()), 1);
167+
}
168+
} catch (LdapException $exc) {
169+
$this->markTestSkipped('Test LDAP server does not support pagination');
170+
}
171+
172+
$this->destroyEntries($ldap, $entries);
173+
}
174+
175+
private function setupTestUsers($ldap)
176+
{
177+
$entries = [];
178+
179+
// Create 25 'users' that we'll query for in different page sizes
180+
$em = $ldap->getEntryManager();
181+
for ($i = 0; $i < 25; ++$i) {
182+
$cn = sprintf('user%d', $i);
183+
$entry = new Entry(sprintf('cn=%s,dc=symfony,dc=com', $cn));
184+
$entry->setAttribute('objectClass', ['applicationProcess']);
185+
$entry->setAttribute('cn', [$cn]);
186+
try {
187+
$em->add($entry);
188+
} catch (LdapException $exc) {
189+
// ignored
190+
}
191+
$entries[] = $entry;
192+
}
193+
194+
return $entries;
195+
}
196+
197+
private function destroyEntries($ldap, $entries)
198+
{
199+
$em = $ldap->getEntryManager();
200+
foreach ($entries as $entry) {
201+
$em->remove($entry);
202+
}
203+
}
204+
205+
public function testLdapPaginationLimits()
206+
{
207+
$ldap = new Adapter(array_merge($this->getLdapConfig(), static::PAGINATION_REQUIRED_CONFIG));
208+
$ldap->getConnection()->bind('cn=admin,dc=symfony,dc=com', 'symfony');
209+
210+
$entries = $this->setupTestUsers($ldap);
211+
212+
$low_max_query = $ldap->createQuery('dc=symfony,dc=com', '(&(objectClass=applicationProcess)(cn=user*))', [
213+
'scope' => Query::SCOPE_ONE,
214+
'pageSize' => 10,
215+
'maxItems' => 5,
216+
]);
217+
$high_max_query = $ldap->createQuery('dc=symfony,dc=com', '(&(objectClass=applicationProcess)(cn=user*))', [
218+
'scope' => Query::SCOPE_ONE,
219+
'pageSize' => 10,
220+
'maxItems' => 13,
221+
]);
222+
223+
try {
224+
$low_max_results = $low_max_query->execute();
225+
$high_max_results = $high_max_query->execute();
226+
227+
$this->assertEquals($low_max_results->count(), 5);
228+
$this->assertEquals($high_max_results->count(), 13);
229+
230+
$this->assertEquals(\count($low_max_query->getResources()), 1);
231+
$this->assertEquals(\count($high_max_query->getResources()), 2);
232+
} catch (LdapException $exc) {
233+
$this->markTestSkipped('Test LDAP server does not support pagination');
234+
}
235+
236+
$this->destroyEntries($ldap, $entries);
237+
}
114238
}

0 commit comments

Comments
 (0)