| 1 |
lars |
1 |
<?php
|
|
|
2 |
// Call Net_LDAP2_SearchTest::main() if this source file is executed directly.
|
|
|
3 |
if (!defined("PHPUnit_MAIN_METHOD")) {
|
|
|
4 |
define("PHPUnit_MAIN_METHOD", "Net_LDAP2_SearchTest::main");
|
|
|
5 |
}
|
|
|
6 |
|
|
|
7 |
require_once "PHPUnit/Framework/TestCase.php";
|
|
|
8 |
require_once "PHPUnit/Framework/TestSuite.php";
|
|
|
9 |
|
|
|
10 |
require_once 'Net/LDAP2.php';
|
|
|
11 |
require_once 'Net/LDAP2/Search.php';
|
|
|
12 |
|
|
|
13 |
/**
|
|
|
14 |
* Test class for Net_LDAP2_Search.
|
|
|
15 |
* Generated by PHPUnit_Util_Skeleton on 2007-10-09 at 10:46:51.
|
|
|
16 |
*/
|
|
|
17 |
class Net_LDAP2_SearchTest extends PHPUnit_Framework_TestCase {
|
|
|
18 |
/**
|
|
|
19 |
* Stores the LDAP configuration
|
|
|
20 |
*/
|
|
|
21 |
var $ldapcfg = false;
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* Runs the test methods of this class.
|
|
|
25 |
*
|
|
|
26 |
* @access public
|
|
|
27 |
* @static
|
|
|
28 |
*/
|
|
|
29 |
public static function main() {
|
|
|
30 |
require_once "PHPUnit/TextUI/TestRunner.php";
|
|
|
31 |
|
|
|
32 |
$suite = new PHPUnit_Framework_TestSuite("Net_LDAP2_SearchTest");
|
|
|
33 |
$result = PHPUnit_TextUI_TestRunner::run($suite);
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* Sets up the fixture, for example, open a network connection.
|
|
|
38 |
* This method is called before a test is executed.
|
|
|
39 |
*
|
|
|
40 |
* @access protected
|
|
|
41 |
*/
|
|
|
42 |
protected function setUp() {
|
|
|
43 |
$this->ldapcfg = $this->getTestConfig();
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
/**
|
|
|
47 |
* Tears down the fixture, for example, close a network connection.
|
|
|
48 |
* This method is called after a test is executed.
|
|
|
49 |
*
|
|
|
50 |
* @access protected
|
|
|
51 |
*/
|
|
|
52 |
protected function tearDown() {
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* This checks if a valid LDAP testconfig is present and loads it.
|
|
|
57 |
*
|
|
|
58 |
* If so, it is loaded and returned as array. If not, false is returned.
|
|
|
59 |
*
|
|
|
60 |
* @return false|array
|
|
|
61 |
*/
|
|
|
62 |
public function getTestConfig() {
|
|
|
63 |
$config = false;
|
|
|
64 |
$file = dirname(__FILE__).'/ldapconfig.ini';
|
|
|
65 |
if (file_exists($file) && is_readable($file)) {
|
|
|
66 |
$config = parse_ini_file($file, true);
|
|
|
67 |
} else {
|
|
|
68 |
return false;
|
|
|
69 |
}
|
|
|
70 |
// validate ini
|
|
|
71 |
$v_error = $file.' is probably invalid. Did you quoted values correctly?';
|
|
|
72 |
$this->assertTrue(array_key_exists('global', $config), $v_error);
|
|
|
73 |
$this->assertTrue(array_key_exists('test', $config), $v_error);
|
|
|
74 |
$this->assertEquals(7, count($config['global']), $v_error);
|
|
|
75 |
$this->assertEquals(7, count($config['test']), $v_error);
|
|
|
76 |
|
|
|
77 |
// reformat things a bit, for convinience
|
|
|
78 |
$config['global']['server_binddn'] =
|
|
|
79 |
$config['global']['server_binddn'].','.$config['global']['server_base_dn'];
|
|
|
80 |
$config['test']['existing_attrmv'] = explode('|', $config['test']['existing_attrmv']);
|
|
|
81 |
return $config;
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
/**
|
|
|
85 |
* Establishes a working connection
|
|
|
86 |
*
|
|
|
87 |
* @return Net_LDAP2
|
|
|
88 |
*/
|
|
|
89 |
public function &connect() {
|
|
|
90 |
// Check extension
|
|
|
91 |
if (true !== Net_LDAP2::checkLDAPExtension()) {
|
|
|
92 |
$this->markTestSkipped('PHP LDAP extension not found or not loadable. Skipped Test.');
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
// Simple working connect and privilegued bind
|
|
|
96 |
$lcfg = array(
|
|
|
97 |
'host' => $this->ldapcfg['global']['server_address'],
|
|
|
98 |
'port' => $this->ldapcfg['global']['server_port'],
|
|
|
99 |
'basedn' => $this->ldapcfg['global']['server_base_dn'],
|
|
|
100 |
'binddn' => $this->ldapcfg['global']['server_binddn'],
|
|
|
101 |
'bindpw' => $this->ldapcfg['global']['server_bindpw'],
|
|
|
102 |
'filter' => '(ou=*)',
|
|
|
103 |
);
|
|
|
104 |
$ldap = Net_LDAP2::connect($lcfg);
|
|
|
105 |
$this->assertType('Net_LDAP2', $ldap, 'Connect failed but was supposed to work. Check credentials and host address. If those are correct, file a bug!');
|
|
|
106 |
return $ldap;
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
/**
|
|
|
110 |
* @todo Implement testEntries().
|
|
|
111 |
*/
|
|
|
112 |
public function testEntries() {
|
|
|
113 |
// Remove the following line when you implement this test.
|
|
|
114 |
$this->markTestIncomplete(
|
|
|
115 |
"This test has not been implemented yet."
|
|
|
116 |
);
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
/**
|
|
|
120 |
* @todo Implement testShiftEntry().
|
|
|
121 |
*/
|
|
|
122 |
public function testShiftEntry() {
|
|
|
123 |
// Remove the following line when you implement this test.
|
|
|
124 |
$this->markTestIncomplete(
|
|
|
125 |
"This test has not been implemented yet."
|
|
|
126 |
);
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
/**
|
|
|
130 |
* @todo Implement testShift_entry().
|
|
|
131 |
*/
|
|
|
132 |
public function testShift_entry() {
|
|
|
133 |
// Remove the following line when you implement this test.
|
|
|
134 |
$this->markTestIncomplete(
|
|
|
135 |
"This test has not been implemented yet."
|
|
|
136 |
);
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
/**
|
|
|
140 |
* @todo Implement testPopEntry().
|
|
|
141 |
*/
|
|
|
142 |
public function testPopEntry() {
|
|
|
143 |
// Remove the following line when you implement this test.
|
|
|
144 |
$this->markTestIncomplete(
|
|
|
145 |
"This test has not been implemented yet."
|
|
|
146 |
);
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
/**
|
|
|
150 |
* @todo Implement testPop_entry().
|
|
|
151 |
*/
|
|
|
152 |
public function testPop_entry() {
|
|
|
153 |
// Remove the following line when you implement this test.
|
|
|
154 |
$this->markTestIncomplete(
|
|
|
155 |
"This test has not been implemented yet."
|
|
|
156 |
);
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
/**
|
|
|
160 |
* @todo Implement testSorted_as_struct().
|
|
|
161 |
*/
|
|
|
162 |
public function testSorted_as_struct() {
|
|
|
163 |
// Remove the following line when you implement this test.
|
|
|
164 |
$this->markTestIncomplete(
|
|
|
165 |
"This test has not been implemented yet."
|
|
|
166 |
);
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
/**
|
|
|
170 |
* @todo Implement testSorted().
|
|
|
171 |
*/
|
|
|
172 |
public function testSorted() {
|
|
|
173 |
// Remove the following line when you implement this test.
|
|
|
174 |
$this->markTestIncomplete(
|
|
|
175 |
"This test has not been implemented yet."
|
|
|
176 |
);
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
/**
|
|
|
180 |
* @todo Implement testAs_struct().
|
|
|
181 |
*/
|
|
|
182 |
public function testAs_struct() {
|
|
|
183 |
// Remove the following line when you implement this test.
|
|
|
184 |
$this->markTestIncomplete(
|
|
|
185 |
"This test has not been implemented yet."
|
|
|
186 |
);
|
|
|
187 |
}
|
|
|
188 |
|
|
|
189 |
/**
|
|
|
190 |
* @todo Implement testSetSearch().
|
|
|
191 |
*/
|
|
|
192 |
public function testSetSearch() {
|
|
|
193 |
// Remove the following line when you implement this test.
|
|
|
194 |
$this->markTestIncomplete(
|
|
|
195 |
"This test has not been implemented yet."
|
|
|
196 |
);
|
|
|
197 |
}
|
|
|
198 |
|
|
|
199 |
/**
|
|
|
200 |
* @todo Implement testSetLink().
|
|
|
201 |
*/
|
|
|
202 |
public function testSetLink() {
|
|
|
203 |
// Remove the following line when you implement this test.
|
|
|
204 |
$this->markTestIncomplete(
|
|
|
205 |
"This test has not been implemented yet."
|
|
|
206 |
);
|
|
|
207 |
}
|
|
|
208 |
|
|
|
209 |
/**
|
|
|
210 |
* @todo Implement testCount().
|
|
|
211 |
*/
|
|
|
212 |
public function testCount() {
|
|
|
213 |
// Remove the following line when you implement this test.
|
|
|
214 |
$this->markTestIncomplete(
|
|
|
215 |
"This test has not been implemented yet."
|
|
|
216 |
);
|
|
|
217 |
}
|
|
|
218 |
|
|
|
219 |
/**
|
|
|
220 |
* @todo Implement testGetErrorCode().
|
|
|
221 |
*/
|
|
|
222 |
public function testGetErrorCode() {
|
|
|
223 |
// Remove the following line when you implement this test.
|
|
|
224 |
$this->markTestIncomplete(
|
|
|
225 |
"This test has not been implemented yet."
|
|
|
226 |
);
|
|
|
227 |
}
|
|
|
228 |
|
|
|
229 |
/**
|
|
|
230 |
* @todo Implement test_Net_LDAP2_Search().
|
|
|
231 |
*/
|
|
|
232 |
public function test_Net_LDAP2_Search() {
|
|
|
233 |
// Remove the following line when you implement this test.
|
|
|
234 |
$this->markTestIncomplete(
|
|
|
235 |
"This test has not been implemented yet."
|
|
|
236 |
);
|
|
|
237 |
}
|
|
|
238 |
|
|
|
239 |
/**
|
|
|
240 |
* @todo Implement testDone().
|
|
|
241 |
*/
|
|
|
242 |
public function testDone() {
|
|
|
243 |
// Remove the following line when you implement this test.
|
|
|
244 |
$this->markTestIncomplete(
|
|
|
245 |
"This test has not been implemented yet."
|
|
|
246 |
);
|
|
|
247 |
}
|
|
|
248 |
|
|
|
249 |
/**
|
|
|
250 |
* @todo Implement test_searchedAttrs().
|
|
|
251 |
*/
|
|
|
252 |
public function test_searchedAttrs() {
|
|
|
253 |
// Remove the following line when you implement this test.
|
|
|
254 |
$this->markTestIncomplete(
|
|
|
255 |
"This test has not been implemented yet."
|
|
|
256 |
);
|
|
|
257 |
}
|
|
|
258 |
|
|
|
259 |
/**
|
|
|
260 |
* tests SPL iterator
|
|
|
261 |
*/
|
|
|
262 |
public function testSPLIterator() {
|
|
|
263 |
if (!$this->ldapcfg) {
|
|
|
264 |
$this->markTestSkipped('No ldapconfig.ini found. Skipping test!');
|
|
|
265 |
} else {
|
|
|
266 |
$ldap =& $this->connect();
|
|
|
267 |
|
|
|
268 |
// some testdata, so we have some entries to search for
|
|
|
269 |
$base = $this->ldapcfg['global']['server_base_dn'];
|
|
|
270 |
$ou1 = Net_LDAP2_Entry::createFresh('ou=Net_LDAP2_Test_search1,'.$base,
|
|
|
271 |
array(
|
|
|
272 |
'objectClass' => array('top','organizationalUnit'),
|
|
|
273 |
'ou' => 'Net_LDAP2_Test_search1'
|
|
|
274 |
));
|
|
|
275 |
$ou2 = Net_LDAP2_Entry::createFresh('ou=Net_LDAP2_Test_search2,'.$base,
|
|
|
276 |
array(
|
|
|
277 |
'objectClass' => array('top','organizationalUnit'),
|
|
|
278 |
'ou' => 'Net_LDAP2_Test_search2'
|
|
|
279 |
));
|
|
|
280 |
$this->assertTrue($ldap->add($ou1));
|
|
|
281 |
$this->assertTrue($ldap->dnExists($ou1->dn()));
|
|
|
282 |
$this->assertTrue($ldap->add($ou2));
|
|
|
283 |
$this->assertTrue($ldap->dnExists($ou2->dn()));
|
|
|
284 |
|
|
|
285 |
/*
|
|
|
286 |
* search and test each method
|
|
|
287 |
*/
|
|
|
288 |
$search = $ldap->search(null, '(ou=Net_LDAP2*)');
|
|
|
289 |
$this->assertType('Net_LDAP2_Search', $search);
|
|
|
290 |
$this->assertEquals(2, $search->count());
|
|
|
291 |
|
|
|
292 |
// current() is supposed to return first valid element
|
|
|
293 |
$e1 = $search->current();
|
|
|
294 |
$this->assertType('Net_LDAP2_Entry', $e1);
|
|
|
295 |
$this->assertEquals($e1->dn(), $search->key());
|
|
|
296 |
$this->assertTrue($search->valid());
|
|
|
297 |
|
|
|
298 |
// shift to next entry
|
|
|
299 |
$search->next();
|
|
|
300 |
$e2 = $search->current();
|
|
|
301 |
$this->assertType('Net_LDAP2_Entry', $e2);
|
|
|
302 |
$this->assertEquals($e2->dn(), $search->key());
|
|
|
303 |
$this->assertTrue($search->valid());
|
|
|
304 |
|
|
|
305 |
// shift to non existent third entry
|
|
|
306 |
$search->next();
|
|
|
307 |
$this->assertFalse($search->current());
|
|
|
308 |
$this->assertFalse($search->key());
|
|
|
309 |
$this->assertFalse($search->valid());
|
|
|
310 |
|
|
|
311 |
// rewind and test,
|
|
|
312 |
// which should return the first entry a second time
|
|
|
313 |
$search->rewind();
|
|
|
314 |
$e1_1 = $search->current();
|
|
|
315 |
$this->assertType('Net_LDAP2_Entry', $e1_1);
|
|
|
316 |
$this->assertEquals($e1_1->dn(), $search->key());
|
|
|
317 |
$this->assertTrue($search->valid());
|
|
|
318 |
$this->assertEquals($e1->dn(), $e1_1->dn());
|
|
|
319 |
|
|
|
320 |
// Dont rewind but call current, should return first entry again
|
|
|
321 |
$e1_2 = $search->current();
|
|
|
322 |
$this->assertType('Net_LDAP2_Entry', $e1_2);
|
|
|
323 |
$this->assertEquals($e1_2->dn(), $search->key());
|
|
|
324 |
$this->assertTrue($search->valid());
|
|
|
325 |
$this->assertEquals($e1->dn(), $e1_2->dn());
|
|
|
326 |
|
|
|
327 |
// rewind again and test,
|
|
|
328 |
// which should return the first entry a third time
|
|
|
329 |
$search->rewind();
|
|
|
330 |
$e1_3 = $search->current();
|
|
|
331 |
$this->assertType('Net_LDAP2_Entry', $e1_3);
|
|
|
332 |
$this->assertEquals($e1_3->dn(), $search->key());
|
|
|
333 |
$this->assertTrue($search->valid());
|
|
|
334 |
$this->assertEquals($e1->dn(), $e1_3->dn());
|
|
|
335 |
|
|
|
336 |
/*
|
|
|
337 |
* Try methods on empty search result
|
|
|
338 |
*/
|
|
|
339 |
$search = $ldap->search(null, '(ou=Net_LDAP2Test_NotExistentEntry)');
|
|
|
340 |
$this->assertType('Net_LDAP2_Search', $search);
|
|
|
341 |
$this->assertEquals(0, $search->count());
|
|
|
342 |
$this->assertFalse($search->current());
|
|
|
343 |
$this->assertFalse($search->key());
|
|
|
344 |
$this->assertFalse($search->valid());
|
|
|
345 |
$search->next();
|
|
|
346 |
$this->assertFalse($search->current());
|
|
|
347 |
$this->assertFalse($search->key());
|
|
|
348 |
$this->assertFalse($search->valid());
|
|
|
349 |
|
|
|
350 |
/*
|
|
|
351 |
* search and simple iterate through the testentries.
|
|
|
352 |
* then, rewind and do it again several times
|
|
|
353 |
*/
|
|
|
354 |
$search2 = $ldap->search(null, '(ou=Net_LDAP2*)');
|
|
|
355 |
$this->assertType('Net_LDAP2_Search', $search2);
|
|
|
356 |
$this->assertEquals(2, $search2->count());
|
|
|
357 |
for ($i = 0; $i <= 5; $i++) {
|
|
|
358 |
$counter = 0;
|
|
|
359 |
foreach ($search2 as $dn => $entry) {
|
|
|
360 |
$counter++;
|
|
|
361 |
|
|
|
362 |
// check on type
|
|
|
363 |
$this->assertType('Net_LDAP2_Entry', $entry);
|
|
|
364 |
|
|
|
365 |
// check on key
|
|
|
366 |
$this->assertThat(strlen($dn), $this->greaterThan(1));
|
|
|
367 |
$this->assertEquals($dn, $entry->dn());
|
|
|
368 |
}
|
|
|
369 |
$this->assertEquals($search2->count(), $counter, "Failed at loop $i");
|
|
|
370 |
|
|
|
371 |
// revert to start
|
|
|
372 |
$search2->rewind();
|
|
|
373 |
}
|
|
|
374 |
|
|
|
375 |
/*
|
|
|
376 |
* Cleanup
|
|
|
377 |
*/
|
|
|
378 |
$this->assertTrue($ldap->delete($ou1), 'Cleanup failed, please delete manually');
|
|
|
379 |
$this->assertTrue($ldap->delete($ou2), 'Cleanup failed, please delete manually');
|
|
|
380 |
}
|
|
|
381 |
}
|
|
|
382 |
}
|
|
|
383 |
|
|
|
384 |
// Call Net_LDAP2_SearchTest::main() if this source file is executed directly.
|
|
|
385 |
if (PHPUnit_MAIN_METHOD == "Net_LDAP2_SearchTest::main") {
|
|
|
386 |
Net_LDAP2_SearchTest::main();
|
|
|
387 |
}
|
|
|
388 |
?>
|