Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
// Call Net_LDAP2Test::main() if this source file is executed directly.
3
if (!defined("PHPUnit_MAIN_METHOD")) {
4
    define("PHPUnit_MAIN_METHOD", "Net_LDAP2Test::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/Entry.php';
12
 
13
/**
14
 * Test class for Net_LDAP2.
15
 * Generated by PHPUnit_Util_Skeleton on 2007-10-09 at 10:32:36.
16
 */
17
class Net_LDAP2Test 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_LDAP2Test");
33
        $result = PHPUnit_TextUI_TestRunner::run($suite);
34
    }
35
 
36
    /**
37
     * Load ldap config and adjust appropriately
38
     *
39
     * @access protected
40
     */
41
    protected function setUp() {
42
        $this->ldapcfg = $this->getTestConfig();
43
    }
44
 
45
    /**
46
     * Tears down the fixture, for example, close a network connection.
47
     * This method is called after a test is executed.
48
     *
49
     * @access protected
50
     */
51
    protected function tearDown() {
52
    }
53
 
54
    /**
55
     * This checks if a valid LDAP testconfig is present and loads it.
56
     *
57
     * If so, it is loaded and returned as array. If not, false is returned.
58
     *
59
     * @return false|array
60
     */
61
    public function getTestConfig() {
62
        $config = false;
63
        $file = dirname(__FILE__).'/ldapconfig.ini';
64
        if (file_exists($file) && is_readable($file)) {
65
            $config = parse_ini_file($file, true);
66
        } else {
67
            return false;
68
        }
69
        // validate ini
70
        $v_error = $file.' is probably invalid. Did you quoted values correctly?';
71
        $this->assertTrue(array_key_exists('global', $config), $v_error);
72
        $this->assertTrue(array_key_exists('test', $config), $v_error);
73
        $this->assertEquals(7, count($config['global']), $v_error);
74
        $this->assertEquals(7, count($config['test']), $v_error);
75
 
76
        // reformat things a bit, for convinience
77
        $config['global']['server_binddn'] =
78
            $config['global']['server_binddn'].','.$config['global']['server_base_dn'];
79
        $config['test']['existing_attrmv'] = explode('|', $config['test']['existing_attrmv']);
80
        return $config;
81
    }
82
 
83
    /**
84
    * Establishes a working connection
85
    *
86
    * @return Net_LDAP2
87
    */
88
    public function &connect() {
89
        // Check extension
90
        if (true !== Net_LDAP2::checkLDAPExtension()) {
91
            $this->markTestSkipped('PHP LDAP extension not found or not loadable. Skipped Test.');
92
        }
93
 
94
        // Simple working connect and privilegued bind
95
        $lcfg = array(
96
                'host'   => $this->ldapcfg['global']['server_address'],
97
                'port'   => $this->ldapcfg['global']['server_port'],
98
                'basedn' => $this->ldapcfg['global']['server_base_dn'],
99
                'binddn' => $this->ldapcfg['global']['server_binddn'],
100
                'bindpw' => $this->ldapcfg['global']['server_bindpw'],
101
                'filter' => '(ou=*)',
102
            );
103
        $ldap = Net_LDAP2::connect($lcfg);
104
        $this->assertType('Net_LDAP2', $ldap, 'Connect failed but was supposed to work. Check credentials and host address. If those are correct, file a bug!');
105
        return $ldap;
106
    }
107
 
108
/* ---------- TESTS ---------- */
109
 
110
    /**
111
     * testCheckLDAPExtension().
112
     *
113
     * @todo can we unload modules at runtime??
114
     */
115
    public function testCheckLDAPExtension() {
116
        if (extension_loaded('ldap')) {
117
            // If extension is already loaded, then we must get true.
118
            $this->assertTrue(Net_LDAP2::checkLDAPExtension());
119
        } else {
120
            // If not, we should be able to load it - but may fail
121
            $this->assertThat(Net_LDAP2::checkLDAPExtension(),
122
                $this->logicalOr($this->isInstanceOf('Net_LDAP2_Error'), $this->equalTo(true)));
123
        }
124
    }
125
 
126
    /**
127
     * Tests if getVersion() works correctly
128
     */
129
    public function testGetVersion() {
130
        $this->assertTrue(defined('NET_LDAP2_VERSION'));
131
        $this->assertEquals(NET_LDAP2_VERSION, Net_LDAP2::getVersion());
132
    }
133
 
134
    /**
135
     * Tests if the server can connect and bind correctly
136
     */
137
    public function testConnectAndPrivileguedBind() {
138
        // Check extension
139
        if (true !== Net_LDAP2::checkLDAPExtension()) {
140
            $this->markTestSkipped('PHP LDAP extension not found or not loadable. Skipped Test.');
141
        }
142
 
143
        if (!$this->ldapcfg) {
144
            $this->markTestSkipped('No ldapconfig.ini found. Skipping test!');
145
        } else {
146
            // This connect is supposed to fail
147
            $lcfg = array(
148
                    'host' => 'pear.net-ldap.test.hostunknown.cno',
149
                );
150
            $ldap = Net_LDAP2::connect($lcfg);
151
            $this->assertType('Net_LDAP2_Error', $ldap, 'Connect succeeded but was supposed to fail!');
152
 
153
            // Failing with multiple hosts
154
            $lcfg = array(
155
                    'host' => array('pear.net-ldap.test.hostunknown1.cno', 'pear.net-ldap.test.hostunknown2.cno'),
156
                );
157
            $ldap = Net_LDAP2::connect($lcfg);
158
            $this->assertType('Net_LDAP2_Error', $ldap, 'Connect succeeded but was supposed to fail!');
159
 
160
            // Simple working connect and privilegued bind
161
            $ldap =& $this->connect();
162
 
163
            // Working connect and privilegued bind with first host down
164
            $lcfg = array(
165
                    'host'   => array(
166
                            'pear.net-ldap.test.hostunknown1.cno',
167
                            $this->ldapcfg['global']['server_address']
168
                        ),
169
                    'port'   => $this->ldapcfg['global']['server_port'],
170
                    'binddn' => $this->ldapcfg['global']['server_binddn'],
171
                    'bindpw' => $this->ldapcfg['global']['server_bindpw'],
172
                );
173
            $ldap = Net_LDAP2::connect($lcfg);
174
            $this->assertType('Net_LDAP2', $ldap, 'Connect failed but was supposed to work. Check credentials and host address. If those are correct, file a bug!');
175
        }
176
    }
177
 
178
    /**
179
     * Tests if the server can connect and bind anonymously, if supported (->cfg and ldap mode)
180
     */
181
    public function testConnectAndAnonymousBind() {
182
        // Check extension
183
        if (true !== Net_LDAP2::checkLDAPExtension()) {
184
            $this->markTestSkipped('PHP LDAP extension not found or not loadable. Skipped Test.');
185
        }
186
 
187
        if (!$this->ldapcfg) {
188
            $this->markTestSkipped('No ldapconfig.ini found. Skipping test!');
189
        } elseif ($this->ldapcfg['global']['server_cap_anonymous'] == true) {
190
            // Simple working connect and anonymous bind
191
            $lcfg = array(
192
                    'host'   => $this->ldapcfg['global']['server_address'],
193
                    'port'   => $this->ldapcfg['global']['server_port'],
194
                );
195
            $ldap = Net_LDAP2::connect($lcfg);
196
            $this->assertType('Net_LDAP2', $ldap, 'Connect failed but was supposed to work. Check address and if server supports anonymous bind. If those are correct, file a bug!');
197
        } else {
198
            $this->markTestSkipped('Server does not support anonymous bind (see ldapconfig.ini). Skipping test.');
199
        }
200
    }
201
 
202
    /**
203
     * testStartTLS() if server supports it
204
     */
205
    public function testStartTLS() {
206
        // Check extension
207
        if (true !== Net_LDAP2::checkLDAPExtension()) {
208
            $this->markTestSkipped('PHP LDAP extension not found or not loadable. Skipped Test.');
209
        }
210
 
211
        if (!$this->ldapcfg) {
212
            $this->markTestSkipped('No ldapconfig.ini found. Skipping test!');
213
        } elseif ($this->ldapcfg['global']['server_cap_tls'] == true) {
214
            // Simple working connect and privilegued bind
215
            $lcfg = array(
216
                    'host'     => $this->ldapcfg['global']['server_address'],
217
                    'port'     => $this->ldapcfg['global']['server_port'],
218
                    'binddn'   => $this->ldapcfg['global']['server_binddn'].','.$this->ldapcfg['global']['server_binddn'],
219
                    'bindpw'   => $this->ldapcfg['global']['server_bindpw'],
220
                    'starttls' => true
221
                );
222
            $ldap = Net_LDAP2::connect();
223
            $this->assertType('Net_LDAP2', $ldap, 'Connect failed but was supposed to work. Check credentials and host address. If those are correct, file a bug!');
224
        } else {
225
             $this->markTestSkipped('Server does not support TLS (see ldapconfig.ini). Skipping test.');
226
        }
227
    }
228
 
229
    /**
230
     * Test if adding and deleting a fresh entry works
231
     */
232
    public function testAdd() {
233
        if (!$this->ldapcfg) {
234
            $this->markTestSkipped('No ldapconfig.ini found. Skipping test!');
235
        } else {
236
            $ldap =& $this->connect();
237
 
238
            // Adding a fresh entry
239
            $cn          = 'Net-LDAP-TestEntry';
240
            $dn          = 'cn='.$cn.','.$this->ldapcfg['global']['server_base_dn'];
241
            $fresh_entry = Net_LDAP2_Entry::createFresh($dn,
242
                array(
243
                    'objectClass' => array('top','person'),
244
                    'cn'          => $cn,
245
                    'sn'          => 'TestEntry'
246
                )
247
            );
248
            $this->assertType('Net_LDAP2_Entry', $fresh_entry);
249
            $this->assertTrue($ldap->add($fresh_entry));
250
 
251
            // Deleting this Entry
252
            $this->assertTrue($ldap->delete($fresh_entry), 'Deletion of entry failed!');
253
        }
254
    }
255
 
256
    /**
257
     * testDelete().
258
     *
259
     * Basic deletion is tested in testAdd(), so here we just test if
260
     * advanced deletion tasks work properly.
261
     */
262
    public function testDelete() {
263
        if (!$this->ldapcfg) {
264
            $this->markTestSkipped('No ldapconfig.ini found. Skipping test!');
265
        } else {
266
            $ldap =& $this->connect();
267
            // some parameter checks
268
            $this->assertType('Net_LDAP2_Error', $ldap->delete(1234));
269
            $this->assertType('Net_LDAP2_Error', $ldap->delete($ldap));
270
 
271
            // in order to test subtree deletion, we need some little tree
272
            // which we need to establish first
273
            $base   = $this->ldapcfg['global']['server_base_dn'];
274
            $testdn = 'ou=Net_LDAP2_Test_subdelete,'.$base;
275
 
276
            $ou = Net_LDAP2_Entry::createFresh($testdn,
277
                array(
278
                    'objectClass' => array('top','organizationalUnit'),
279
                    'ou' => 'Net_LDAP2_Test_subdelete'
280
                ));
281
            $ou_1 = Net_LDAP2_Entry::createFresh('ou=test1,'.$testdn,
282
                array(
283
                    'objectClass' => array('top','organizationalUnit'),
284
                    'ou' => 'test1'
285
                ));
286
            $ou_1_l1 = Net_LDAP2_Entry::createFresh('l=subtest,ou=test1,'.$testdn,
287
                array(
288
                    'objectClass' => array('top','locality'),
289
                    'l' => 'test1'
290
                ));
291
            $ou_2 = Net_LDAP2_Entry::createFresh('ou=test2,'.$testdn,
292
                array(
293
                    'objectClass' => array('top','organizationalUnit'),
294
                    'ou' => 'test2'
295
                ));
296
            $ou_3 = Net_LDAP2_Entry::createFresh('ou=test3,'.$testdn,
297
                array(
298
                    'objectClass' => array('top','organizationalUnit'),
299
                    'ou' => 'test3'
300
                ));
301
            $this->assertTrue($ldap->add($ou));
302
            $this->assertTrue($ldap->add($ou_1));
303
            $this->assertTrue($ldap->add($ou_1_l1));
304
            $this->assertTrue($ldap->add($ou_2));
305
            $this->assertTrue($ldap->add($ou_3));
306
            $this->assertTrue($ldap->dnExists($ou->dn()));
307
            $this->assertTrue($ldap->dnExists($ou_1->dn()));
308
            $this->assertTrue($ldap->dnExists($ou_1_l1->dn()));
309
            $this->assertTrue($ldap->dnExists($ou_2->dn()));
310
            $this->assertTrue($ldap->dnExists($ou_3->dn()));
311
            // Tree established now. We can run some tests now :D
312
 
313
            // Try to delete some non existent entry inside that subtree (fails)
314
            $this->assertType('Net_LDAP2_Error', $ldap->delete(
315
                'cn=not_existent,ou=test1,'.$testdn));
316
 
317
            // Try to delete main test ou without recursive set (fails too)
318
            $this->assertType('Net_LDAP2_Error', $ldap->delete($testdn));
319
 
320
            // Retry with subtree delete, this should work
321
            $this->assertTrue($ldap->delete($testdn, true));
322
 
323
            // The DN is not allowed to exist anymore
324
            $this->assertFalse($ldap->dnExists($testdn));
325
        }
326
    }
327
 
328
    /**
329
     * testModify().
330
     */
331
    public function testModify() {
332
        if (!$this->ldapcfg) {
333
            $this->markTestSkipped('No ldapconfig.ini found. Skipping test!');
334
        } else {
335
            $ldap =& $this->connect();
336
            // We need a test entry:
337
            $local_entry = Net_LDAP2_Entry::createFresh(
338
                'ou=Net_LDAP2_Test_modify,'.$this->ldapcfg['global']['server_base_dn'],
339
                array(
340
                    'objectClass' => array('top','organizationalUnit'),
341
                    'ou'              => 'Net_LDAP2_Test_modify',
342
                    'street'          => 'Beniroad',
343
                    'telephoneNumber' => array('1234', '5678'),
344
                    'postalcode'      => '12345',
345
                    'postalAddress'   => 'someAddress',
346
                    'facsimileTelephoneNumber' => array('123','456')
347
                ));
348
            $this->assertTrue($ldap->add($local_entry));
349
            $this->assertTrue($ldap->dnExists($local_entry->dn()));
350
 
351
            // Prepare some changes
352
            $changes = array(
353
                'add' => array(
354
                                'businessCategory' => array('foocat', 'barcat'),
355
                                'description' => 'testval'
356
                ),
357
                'delete' => array('postalAddress'),
358
                'replace' => array('telephoneNumber' => array('345', '567')),
359
                'changes' => array(
360
                                'replace' => array('street' => 'Highway to Hell'),
361
                                'add' => array('l' => 'someLocality'),
362
                                'delete' => array(
363
                                    'postalcode',
364
                                    'facsimileTelephoneNumber' => array('123'))
365
                )
366
            );
367
 
368
            // Perform those changes
369
            $this->assertTrue($ldap->modify($local_entry, $changes));
370
 
371
            // verify correct attribute changes
372
            $actual_entry = $ldap->getEntry($local_entry->dn(), array(
373
                'objectClass', 'ou','postalAddress', 'street', 'telephoneNumber', 'postalcode',
374
                'facsimileTelephoneNumber', 'l', 'businessCategory', 'description'));
375
            $this->assertType('Net_LDAP2_Entry', $actual_entry);
376
            $expected_attributes = array(
377
                'objectClass' => array('top', 'organizationalUnit'),
378
                'ou' => 'Net_LDAP2_Test_modify',
379
                'street' => 'Highway to Hell',
380
                'l'      => 'someLocality',
381
                'telephoneNumber' => array('345', '567'),
382
                'businessCategory' => array('foocat', 'barcat'),
383
                'description' => 'testval',
384
                'facsimileTelephoneNumber' => '456'
385
            );
386
 
387
            $local_attributes  = $local_entry->getValues();
388
            $actual_attributes = $actual_entry->getValues();
389
 
390
            // to enable easy check, we need to sort the
391
            // values of the remaining multival attrs as
392
            // well as the attribute names
393
            ksort($expected_attributes);
394
            ksort($local_attributes);
395
            ksort($actual_attributes);
396
            sort($expected_attributes['businessCategory']);
397
            sort($local_attributes['businessCategory']);
398
            sort($actual_attributes['businessCategory']);
399
 
400
            // cleanup directory
401
            $this->assertTrue($ldap->delete($actual_entry),
402
                'Cleanup of test entry failed. Please remove manually: '.$local_entry->dn());
403
 
404
            // The attributes must match the expected values.
405
            // Both, the entry inside the directory and our
406
            // apps local copy must reflect the same values
407
            $this->assertEquals($expected_attributes, $actual_attributes, 'The directory entries attributes are not OK!');
408
            $this->assertEquals($expected_attributes, $local_attributes, 'The local entries attributes are not OK!');
409
        }
410
    }
411
 
412
    /**
413
     * testSearch().
414
     */
415
    public function testSearch() {
416
        if (!$this->ldapcfg) {
417
            $this->markTestSkipped('No ldapconfig.ini found. Skipping test!');
418
        } else {
419
            $ldap =& $this->connect();
420
 
421
            // some testdata, so we can test sizelimit
422
            $base = $this->ldapcfg['global']['server_base_dn'];
423
            $ou1  = Net_LDAP2_Entry::createFresh('ou=Net_LDAP2_Test_search1,'.$base,
424
                array(
425
                    'objectClass' => array('top','organizationalUnit'),
426
                    'ou' => 'Net_LDAP2_Test_search1'
427
                ));
428
            $ou1_1  = Net_LDAP2_Entry::createFresh('ou=Net_LDAP2_Test_search1_1,'.$ou1->dn(),
429
                array(
430
                    'objectClass' => array('top','organizationalUnit'),
431
                    'ou' => 'Net_LDAP2_Test_search2'
432
                ));
433
            $ou2  = Net_LDAP2_Entry::createFresh('ou=Net_LDAP2_Test_search2,'.$base,
434
                array(
435
                    'objectClass' => array('top','organizationalUnit'),
436
                    'ou' => 'Net_LDAP2_Test_search2'
437
                ));
438
            $this->assertTrue($ldap->add($ou1));
439
            $this->assertTrue($ldap->dnExists($ou1->dn()));
440
            $this->assertTrue($ldap->add($ou1_1));
441
            $this->assertTrue($ldap->dnExists($ou1_1->dn()));
442
            $this->assertTrue($ldap->add($ou2));
443
            $this->assertTrue($ldap->dnExists($ou2->dn()));
444
 
445
 
446
            // Search for testfilter, should at least return our two test entries
447
            $res = $ldap->search(null, '(ou=Net_LDAP2*)',
448
                array('attributes' => '1.1')
449
            );
450
            $this->assertType('Net_LDAP2_Search', $res);
451
            $this->assertThat($res->count(), $this->greaterThanOrEqual(2));
452
 
453
            // Same, but with Net_LDAP2_Filter object
454
            $filtero = Net_LDAP2_Filter::create('ou', 'begins', 'Net_LDAP2');
455
            $this->assertType('Net_LDAP2_Filter', $filtero);
456
            $res = $ldap->search(null, $filtero,
457
                array('attributes' => '1.1')
458
            );
459
            $this->assertType('Net_LDAP2_Search', $res);
460
            $this->assertThat($res->count(), $this->greaterThanOrEqual(2));
461
 
462
            // Search using default filter for base-onelevel scope
463
            // should at least return our two test entries
464
            $res = $ldap->search(null, null,
465
                array('scope' => 'one', 'attributes' => '1.1')
466
            );
467
            $this->assertType('Net_LDAP2_Search', $res);
468
            $this->assertThat($res->count(), $this->greaterThanOrEqual(2));
469
 
470
            // Base-search using custom base (string)
471
            // should only return the test entry $ou1 and not the entry below it.
472
            $res = $ldap->search($ou1->dn(), null,
473
                array('scope' => 'base', 'attributes' => '1.1')
474
            );
475
            $this->assertType('Net_LDAP2_Search', $res);
476
            $this->assertEquals(1, $res->count());
477
 
478
            // Search using custom base, this time using an entry object
479
            // This tests if passing an entry object as base works
480
            // should only return the test entry $ou1
481
            $res = $ldap->search($ou1, '(ou=*)',
482
                array('scope' => 'base', 'attributes' => '1.1')
483
            );
484
            $this->assertType('Net_LDAP2_Search', $res);
485
            $this->assertEquals(1, $res->count());
486
 
487
            // Search using default filter for base-onelevel scope with sizelimit
488
            // should of course return more than one entry,
489
            // but not more than sizelimit
490
            $res = $ldap->search(null, null,
491
                array('scope' => 'one', 'sizelimit' => 1, 'attributes' => '1.1')
492
            );
493
            $this->assertType('Net_LDAP2_Search', $res);
494
            $this->assertEquals(1, $res->count());
495
            $this->assertTrue($res->sizeLimitExceeded()); // sizelimit should be exceeded now
496
 
497
            // Bad filter
498
            $res = $ldap->search(null, 'somebadfilter',
499
                array('attributes' => '1.1')
500
            );
501
            $this->assertType('Net_LDAP2_Error', $res);
502
 
503
            // Bad base
504
            $res = $ldap->search('badbase', null,
505
                array('attributes' => '1.1')
506
            );
507
            $this->assertType('Net_LDAP2_Error', $res);
508
 
509
            // Passing Error object as base and as filter object
510
            $error = new Net_LDAP2_Error('Testerror');
511
            $res = $ldap->search($error, null, // error base
512
                array('attributes' => '1.1')
513
            );
514
            $this->assertType('Net_LDAP2_Error', $res);
515
            $res = $ldap->search(null, $error, // error filter
516
                array('attributes' => '1.1')
517
            );
518
            $this->assertType('Net_LDAP2_Error', $res);
519
 
520
            // Nullresult
521
            $res = $ldap->search(null, '(cn=nevermatching_filter)',
522
                array('scope' => 'base', 'attributes' => '1.1')
523
            );
524
            $this->assertType('Net_LDAP2_Search', $res);
525
            $this->assertEquals(0, $res->count());
526
 
527
 
528
            // cleanup
529
            $this->assertTrue($ldap->delete($ou1_1), 'Cleanup failed, please delete manually');
530
            $this->assertTrue($ldap->delete($ou1),   'Cleanup failed, please delete manually');
531
            $this->assertTrue($ldap->delete($ou2),   'Cleanup failed, please delete manually');
532
        }
533
    }
534
 
535
    /**
536
     * @todo Implement testSetOption().
537
     */
538
    public function testSetOption() {
539
        if (!$this->ldapcfg) {
540
            $this->markTestSkipped('No ldapconfig.ini found. Skipping test!');
541
        } else {
542
            $this->markTestIncomplete("This test has not been implemented yet.");
543
        }
544
    }
545
 
546
    /**
547
     * @todo Implement testGetOption().
548
     */
549
    public function testGetOption() {
550
        if (!$this->ldapcfg) {
551
            $this->markTestSkipped('No ldapconfig.ini found. Skipping test!');
552
        } else {
553
            $this->markTestIncomplete("This test has not been implemented yet.");
554
        }
555
    }
556
 
557
    /**
558
     * @todo Implement testGetLDAPVersion().
559
     */
560
    public function testGetLDAPVersion() {
561
        if (!$this->ldapcfg) {
562
            $this->markTestSkipped('No ldapconfig.ini found. Skipping test!');
563
        } else {
564
            $this->markTestIncomplete("This test has not been implemented yet.");
565
        }
566
    }
567
 
568
    /**
569
     * @todo Implement testSetLDAPVersion().
570
     */
571
    public function testSetLDAPVersion() {
572
        if (!$this->ldapcfg) {
573
            $this->markTestSkipped('No ldapconfig.ini found. Skipping test!');
574
        } else {
575
            $this->markTestIncomplete("This test has not been implemented yet.");
576
        }
577
    }
578
 
579
    /**
580
     * testDnExists().
581
     */
582
    public function testDnExists() {
583
        if (!$this->ldapcfg) {
584
            $this->markTestSkipped('No ldapconfig.ini found. Skipping test!');
585
        } else {
586
            $ldap =& $this->connect();
587
            $dn   = $this->ldapcfg['test']['existing_entry'].','.$this->ldapcfg['global']['server_base_dn'];
588
 
589
            // Testing existing and not existing DN; neither should produce an error
590
            $this->assertTrue($ldap->dnExists($dn));
591
            $this->assertFalse($ldap->dnExists('cn=not_existent,'.$dn));
592
 
593
            // Passing an Entry object (should work)
594
            // It should return false, because don't add the test entry
595
            $base = $this->ldapcfg['global']['server_base_dn'];
596
            $ou1  = Net_LDAP2_Entry::createFresh('ou=Net_LDAP2_Test_search1,'.$base,
597
                array(
598
                    'objectClass' => array('top','organizationalUnit'),
599
                    'ou' => 'Net_LDAP2_Test_search1'
600
                ));
601
            $this->assertFalse($ldap->dnExists($ou1));
602
 
603
            // Passing an float instead of a string
604
            $this->assertType('Net_LDAP2_Error', $ldap->dnExists(1.234));
605
 
606
            // Pasing an error object
607
            $error = new Net_LDAP2_Error('Testerror');
608
            $this->assertType('Net_LDAP2_Error', $ldap->dnExists($error));
609
        }
610
    }
611
 
612
    /**
613
     * testGetEntry().
614
     */
615
    public function testGetEntry() {
616
        if (!$this->ldapcfg) {
617
            $this->markTestSkipped('No ldapconfig.ini found. Skipping test!');
618
        } else {
619
            $ldap =& $this->connect();
620
            $dn   = $this->ldapcfg['test']['existing_entry'].','.$this->ldapcfg['global']['server_base_dn'];
621
 
622
            // existing DN
623
            $this->assertType('Net_LDAP2_Entry', $ldap->getEntry($dn), "$dn was supposed to be found. Please check your ldapconfig.ini!");
624
 
625
            // Not existing DN
626
            $this->assertType('Net_LDAP2_Error',
627
                $ldap->getEntry('cn=notexistent,'.$this->ldapcfg['global']['server_base_dn']));
628
        }
629
    }
630
 
631
    /**
632
     * testMove().
633
     */
634
    public function testMove() {
635
        if (!$this->ldapcfg) {
636
            $this->markTestSkipped('No ldapconfig.ini found. Skipping test!');
637
        } else {
638
            $ldap =& $this->connect();
639
 
640
            // For Moving tests, we need some little tree again
641
            $base   = $this->ldapcfg['global']['server_base_dn'];
642
            $testdn = 'ou=Net_LDAP2_Test_moves,'.$base;
643
 
644
            $ou = Net_LDAP2_Entry::createFresh($testdn,
645
                array(
646
                    'objectClass' => array('top','organizationalUnit'),
647
                    'ou' => 'Net_LDAP2_Test_moves'
648
                ));
649
            $ou_1 = Net_LDAP2_Entry::createFresh('ou=source,'.$testdn,
650
                array(
651
                    'objectClass' => array('top','organizationalUnit'),
652
                    'ou' => 'source'
653
                ));
654
            $ou_1_l1 = Net_LDAP2_Entry::createFresh('l=moveitem,ou=source,'.$testdn,
655
                array(
656
                    'objectClass' => array('top','locality'),
657
                    'l' => 'moveitem',
658
                    'description' => 'movetest'
659
                ));
660
            $ou_2 = Net_LDAP2_Entry::createFresh('ou=target,'.$testdn,
661
                array(
662
                    'objectClass' => array('top','organizationalUnit'),
663
                    'ou' => 'target'
664
                ));
665
            $ou_3 = Net_LDAP2_Entry::createFresh('ou=target_otherdir,'.$testdn,
666
                array(
667
                    'objectClass' => array('top','organizationalUnit'),
668
                    'ou' => 'target_otherdir'
669
                ));
670
            $this->assertTrue($ldap->add($ou));
671
            $this->assertTrue($ldap->add($ou_1));
672
            $this->assertTrue($ldap->add($ou_1_l1));
673
            $this->assertTrue($ldap->add($ou_2));
674
            $this->assertTrue($ldap->add($ou_3));
675
            $this->assertTrue($ldap->dnExists($ou->dn()));
676
            $this->assertTrue($ldap->dnExists($ou_1->dn()));
677
            $this->assertTrue($ldap->dnExists($ou_1_l1->dn()));
678
            $this->assertTrue($ldap->dnExists($ou_2->dn()));
679
            $this->assertTrue($ldap->dnExists($ou_3->dn()));
680
            // Tree established
681
 
682
            // Local rename
683
            $olddn = $ou_1_l1->currentDN();
684
            $this->assertTrue($ldap->move($ou_1_l1,
685
                str_replace('moveitem', 'move_item', $ou_1_l1->dn())));
686
            $this->assertTrue($ldap->dnExists($ou_1_l1->dn()));
687
            $this->assertFalse($ldap->dnExists($olddn));
688
 
689
            // Local move
690
            $olddn = $ou_1_l1->currentDN();
691
            $this->assertTrue($ldap->move($ou_1_l1, 'l=move_item,'.$ou_2->dn()));
692
            $this->assertTrue($ldap->dnExists($ou_1_l1->dn()));
693
            $this->assertFalse($ldap->dnExists($olddn));
694
 
695
            // Local move backward, with rename
696
            // Here we use the DN of the object, to test DN conversion.
697
            // Note that this will outdate the object since it does not
698
            // has knowledge about the move.
699
            $olddn = $ou_1_l1->currentDN();
700
            $newdn = 'l=moveditem,'.$ou_2->dn();
701
            $this->assertTrue($ldap->move($olddn, $newdn));
702
            $this->assertTrue($ldap->dnExists($newdn));
703
            $this->assertFalse($ldap->dnExists($olddn));
704
            $ou_1_l1 = $ldap->getEntry($newdn); // Refetch since the objects DN was outdated
705
 
706
            // Fake-cross directory move using two separate
707
            // links to the same directory.
708
            // This other directory is represented by ou=target_otherdir
709
            $ldap2 = $this->connect();
710
            $olddn = $ou_1_l1->currentDN();
711
            $this->assertTrue($ldap->move($ou_1_l1, 'l=movedcrossdir,'.$ou_3->dn(), $ldap2));
712
            $this->assertFalse($ldap->dnExists($olddn));
713
            $this->assertTrue($ldap2->dnExists($ou_1_l1->dn()));
714
 
715
            // Try to move over an existing entry
716
             $this->assertType('Net_LDAP2_Error', $ldap->move($ou_2, $ou_3->dn(), $ldap2));
717
 
718
            // Try cross directory move without providing an valid entry but a DN
719
            $this->assertType('Net_LDAP2_Error',
720
                $ldap->move($ou_1_l1->dn(), 'l=movedcrossdir2,'.$ou_2->dn(), $ldap2));
721
 
722
            // Try passing an invalid entry object
723
            $this->assertType('Net_LDAP2_Error',
724
                $ldap->move($ldap, 'l=move_item,'.$ou_2->dn()));
725
 
726
            // Try passing an invalid ldap object
727
            $this->assertType('Net_LDAP2_Error',
728
                $ldap->move($ou_1_l1, 'l=move_item,'.$ou_2->dn(), $ou_1));
729
 
730
            // cleanup test tree
731
            $this->assertTrue($ldap->delete($testdn, true), "Could not delete $testdn, please cleanup manually");
732
 
733
        }
734
    }
735
 
736
    /**
737
     * testCopy().
738
     */
739
    public function testCopy() {
740
        if (!$this->ldapcfg) {
741
            $this->markTestSkipped('No ldapconfig.ini found. Skipping test!');
742
        } else {
743
            $ldap =& $this->connect();
744
 
745
            // some testdata...
746
            $base = $this->ldapcfg['global']['server_base_dn'];
747
            $ou1  = Net_LDAP2_Entry::createFresh('ou=Net_LDAP2_Test_pool,'.$base,
748
                array(
749
                    'objectClass' => array('top','organizationalUnit'),
750
                    'ou' => 'Net_LDAP2_Test_copy'
751
                ));
752
            $ou2  = Net_LDAP2_Entry::createFresh('ou=Net_LDAP2_Test_tgt,'.$base,
753
                array(
754
                    'objectClass' => array('top','organizationalUnit'),
755
                    'ou' => 'Net_LDAP2_Test_copy'
756
                ));
757
            $this->assertTrue($ldap->add($ou1));
758
            $this->assertTrue($ldap->dnExists($ou1->dn()));
759
            $this->assertTrue($ldap->add($ou2));
760
            $this->assertTrue($ldap->dnExists($ou2->dn()));
761
            $entry  = Net_LDAP2_Entry::createFresh('l=cptest,'.$ou1->dn(),
762
                array(
763
                    'objectClass' => array('top','locality'),
764
                    'l' => 'cptest'
765
                ));
766
            $this->assertTrue($ldap->add($entry));
767
            $this->assertTrue($ldap->dnExists($entry->dn()));
768
 
769
            // copy over the entry to another tree with rename
770
            $entrycp = $ldap->copy($entry, 'l=test_copied,'.$ou2->dn());
771
            $this->assertType('Net_LDAP2_Entry', $entrycp);
772
            $this->assertNotEquals($entry->dn(), $entrycp->dn());
773
            $this->assertTrue($ldap->dnExists($entrycp->dn()));
774
 
775
            // copy same again (fails, entry exists)
776
            $entrycp_f = $ldap->copy($entry, 'l=test_copied,'.$ou2->dn());
777
            $this->assertType('Net_LDAP2_Error', $entrycp_f);
778
 
779
            // use only DNs to copy (fails)
780
            $entrycp = $ldap->copy($entry->dn(), 'l=test_copied2,'.$ou2->dn());
781
 
782
            //cleanup
783
            $this->assertTrue($ldap->delete($ou1, true));
784
            $this->assertTrue($ldap->delete($ou2, true));
785
        }
786
    }
787
 
788
    /**
789
     * testIsError().
790
     */
791
    public function testIsError() {
792
        if (!$this->ldapcfg) {
793
            $this->markTestSkipped('No ldapconfig.ini found. Skipping test!');
794
        } else {
795
            $error = PEAR::raiseError('TestError');
796
            $this->assertTrue(Net_LDAP2::isError($error));
797
            $this->assertFalse(Net_LDAP2::isError('noerror'));
798
        }
799
    }
800
 
801
    /**
802
     * checks retrival of RootDSE object
803
     */
804
    public function testRootDse() {
805
        if (!$this->ldapcfg) {
806
            $this->markTestSkipped('No ldapconfig.ini found. Skipping test!');
807
        } else {
808
            $ldap =& $this->connect();
809
            $this->assertType('Net_LDAP2_RootDSE', $ldap->rootDSE());
810
        }
811
    }
812
 
813
    /**
814
     * Checks retrival of schema through LDAP object
815
     */
816
    public function testSchema() {
817
        if (!$this->ldapcfg) {
818
            $this->markTestSkipped('No ldapconfig.ini found. Skipping test!');
819
        } else {
820
            $ldap =& $this->connect();
821
            $this->assertType('Net_LDAP2_Schema', $ldap->schema());
822
        }
823
    }
824
 
825
    /**
826
     * testUtf8Encode()
827
     */
828
    public function testUtf8Encode() {
829
        if (!$this->ldapcfg) {
830
            $this->markTestSkipped('No ldapconfig.ini found. Skipping test!');
831
        } else {
832
            $ldap    =& $this->connect();
833
            $utf8    = array('cn' => 'this needs utf8: öäü');
834
            $no_utf8 = array('cn' => 'this needs no utf8');
835
 
836
            $this->assertNotEquals($utf8, $ldap->utf8Encode($utf8));
837
            $this->assertEquals($no_utf8, $ldap->utf8Encode($no_utf8));
838
 
839
            // wrong parameter
840
            $this->assertType('Net_LDAP2_Error', $ldap->utf8Encode('foobar'));
841
            $this->assertType('Net_LDAP2_Error', $ldap->utf8Encode(array('foobar')));
842
        }
843
    }
844
 
845
    /**
846
     * testUtf8Decode().
847
     */
848
    public function testUtf8Decode() {
849
        if (!$this->ldapcfg) {
850
            $this->markTestSkipped('No ldapconfig.ini found. Skipping test!');
851
        } else {
852
            $ldap  =& $this->connect();
853
            $entry = $ldap->getEntry($this->ldapcfg['test']['existing_entry'].','.$this->ldapcfg['global']['server_base_dn'],
854
                array($this->ldapcfg['test']['utf8_attr'], $this->ldapcfg['test']['noutf8_attr']));
855
            $this->assertType('Net_LDAP2_Entry', $entry, 'Unable to fetch test entry, check ldapconfig.ini');
856
            $raw_utf8 = array($this->ldapcfg['test']['utf8_attr'] => $entry->getValue($this->ldapcfg['test']['utf8_attr'], 'single'));
857
            $this->assertTrue(is_string($raw_utf8[$this->ldapcfg['test']['utf8_attr']]));
858
            $no_utf8  = array($this->ldapcfg['test']['noutf8_attr'] => $entry->getValue($this->ldapcfg['test']['noutf8_attr'], 'single'));
859
            $this->assertTrue(is_string($no_utf8[$this->ldapcfg['test']['noutf8_attr']]));
860
 
861
            $this->assertNotEquals($raw_utf8, $ldap->utf8Decode($raw_utf8));
862
            $this->assertEquals($no_utf8, $ldap->utf8Decode($no_utf8));
863
 
864
            // wrong parameter
865
            $this->assertType('Net_LDAP2_Error', $ldap->utf8Decode('foobar'));
866
            $this->assertType('Net_LDAP2_Error', $ldap->utf8Decode(array('foobar')));
867
        }
868
    }
869
 
870
    /**
871
     * testGetLink().
872
     */
873
    public function testGetLink() {
874
        if (!$this->ldapcfg) {
875
            $this->markTestSkipped('No ldapconfig.ini found. Skipping test!');
876
        } else {
877
            $ldap =& $this->connect();
878
            $this->assertTrue(is_resource($ldap->getLink()));
879
        }
880
    }
881
 
882
    /**
883
    * Test for bug #18202: "Adding attributes to a Fresh Entry saving and laterly updating fails"
884
    */
885
    public function testEntryAddIsNotPersistent() {
886
        if (!$this->ldapcfg) {
887
            $this->markTestSkipped('No ldapconfig.ini found. Skipping test!');
888
        } else {
889
            $ldap =& $this->connect();
890
 
891
            // setup test entry
892
            $cn   = 'Net_LDAP2_Test_bug_18202';
893
            $dn   = 'cn='.$cn.','.$this->ldapcfg['global']['server_base_dn'];
894
            $data = array(
895
                'objectClass' => array('top', 'inetOrgPerson'),
896
                'givenName'   => 'bug 18202',
897
                'sn'          => 'testentry',
898
                'cn'          => $cn
899
            );
900
 
901
            // Test case
902
            $entry = Net_LDAP2_Entry::createFresh($dn, $data);
903
            $this->assertType('Net_LDAP2_Entry', $entry);
904
            $this->assertTrue( $entry->add(array('uid' => 'Fu Bar')) );
905
            $this->assertTrue( $ldap->add($entry) );
906
            $this->assertTrue( $entry->replace(array('uid' => 'Foo Bar')) );
907
            $this->assertTrue( $result = $entry->update() );
908
 
909
            // cleanup
910
            $this->assertTrue($ldap->delete($entry),
911
                    'Cleanup of test entry failed. Please remove manually: '.$entry->dn());
912
        }
913
    }
914
 
915
}
916
 
917
// Call Net_LDAP2Test::main() if this source file is executed directly.
918
if (PHPUnit_MAIN_METHOD == "Net_LDAP2Test::main") {
919
    Net_LDAP2Test::main();
920
}
921
?>