Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
//@encoding iso-8859-1
3
 
4
// Call Net_LDAP2_LDIFTest::main() if this source file is executed directly.
5
if (!defined("PHPUnit_MAIN_METHOD")) {
6
    define("PHPUnit_MAIN_METHOD", "Net_LDAP2_LDIFTest::main");
7
}
8
 
9
require_once "PHPUnit/Framework/TestCase.php";
10
require_once "PHPUnit/Framework/TestSuite.php";
11
 
12
require_once 'Net/LDAP2/LDIF.php';
13
 
14
/**
15
 * Test class for Net_LDAP2_LDIF.
16
 * Generated by PHPUnit_Util_Skeleton on 2007-12-20 at 10:11:52.
17
 */
18
class Net_LDAP2_LDIFTest extends PHPUnit_Framework_TestCase {
19
    /**
20
    * Default config for tests.
21
    *
22
    * The config is bound to the ldif test file
23
    * tests/ldif_data/unsorted_w50.ldif
24
    * so don't change or tests will fail
25
    *
26
    * @var array
27
    */
28
    var $defaultConfig = array(
29
        'onerror' => 'undef',
30
        'encode'  => 'base64',
31
        'wrap'    => 50,
32
        'change'  => 0,
33
        'sort'    => 0,
34
        'version' => 1   // mimic pre 2.0.0RC5 behavior, so test files need no adjusting
35
    );
36
 
37
    /**
38
    * Test entries data
39
    *
40
    * Please do not just modify these values, they
41
    * are closely related to the LDIF test data.
42
    *
43
    * @var string
44
    */
45
    var $testentries_data = array(
46
        'cn=test1,ou=example,dc=cno' => array(
47
            'cn'    => 'test1',
48
            'attr3' => array('foo', 'bar'),
49
            'attr1' => 12345,
50
            'attr4' => 'brrrzztt',
51
            'objectclass' => 'oc1',
52
            'attr2' => array('1234', 'baz')),
53
 
54
        'cn=test blabla,ou=example,dc=cno' => array(
55
            'cn'    => 'test blabla',
56
            'attr3' => array('foo', 'bar'),
57
            'attr1' => 12345,
58
            'attr4' => 'blablaöäü',
59
            'objectclass' => 'oc2',
60
            'attr2' => array('1234', 'baz'),
61
            'verylong' => 'fhu08rhvt7b478vt5hv78h45nfgt45h78t34hhhhhhhhhv5bg8h6ttttttttt3489t57nhvgh4788trhg8999vnhtgthgui65hgb5789thvngwr789cghm738'),
62
 
63
        'cn=test öäü,ou=example,dc=cno' => array(
64
            'cn'    => 'test öäü',
65
            'attr3' => array('foo', 'bar'),
66
            'attr1' => 12345,
67
            'attr4' => 'blablaöäü',
68
            'objectclass' => 'oc3',
69
            'attr2' => array('1234', 'baz'),
70
            'attr5' => 'endspace ',
71
            'attr6' => ':badinitchar'),
72
 
73
        ':cn=endspace,dc=cno ' => array(
74
            'cn'    => 'endspace')
75
    );
76
 
77
    /**
78
    * Test file written to
79
    *
80
    * @var string
81
    */
82
    var $outfile = 'test.out.ldif';
83
 
84
    /**
85
    * Test entries
86
    *
87
    * They will be created in main()
88
    *
89
    * @var array
90
    */
91
    var $testentries = array();
92
 
93
    /**
94
     * Runs the test methods of this class.
95
     *
96
     * @access public
97
     * @static
98
     */
99
    public static function main() {
100
        require_once "PHPUnit/TextUI/TestRunner.php";
101
 
102
        $suite  = new PHPUnit_Framework_TestSuite("Net_LDAP2_LDIFTest");
103
        $result = PHPUnit_TextUI_TestRunner::run($suite);
104
    }
105
 
106
    /**
107
     * Open some outfile and ensure correct rights
108
     *
109
     * @access protected
110
     */
111
    protected function setUp() {
112
        // initialize test entries
113
        $this->testentries = array();
114
        foreach ($this->testentries_data as $dn => $attrs) {
115
            $entry = Net_LDAP2_Entry::createfresh($dn, $attrs);
116
            $this->assertType('Net_LDAP2_Entry', $entry, 'ERROR inittializing test entries');
117
            array_push($this->testentries, $entry);
118
        }
119
 
120
        // create outfile if not exists and enforce proper access rights
121
        if (!file_exists($this->outfile)) {
122
            if (!touch($this->outfile)) {
123
                $this->markTestSkipped('Unable to create '.$this->outfile.', skipping test');
124
            }
125
        }
126
        if (!chmod($this->outfile, 0644)) {
127
            $this->markTestSkipped('Unable to chmod(0644) '.$this->outfile.', skipping test');
128
        }
129
    }
130
 
131
    /**
132
     * Remove the outfile
133
     *
134
     * @access protected
135
     */
136
    protected function tearDown() {
137
       @unlink($this->outfile);
138
    }
139
 
140
    /**
141
     * Construction tests
142
     *
143
     * Construct LDIF object and see if we can get a handle
144
     */
145
    public function testConstruction() {
146
        $supported_modes = array('r', 'w', 'a');
147
        $plus            = array('', '+');
148
 
149
        // Test all open modes,
150
        // all of them should return a correct handle
151
        foreach ($supported_modes as $mode) {
152
            foreach ($plus as $p) {
153
                $ldif = new Net_LDAP2_LDIF($this->outfile, $mode, $this->defaultConfig);
154
                $this->assertTrue(is_resource($ldif->handle()));
155
            }
156
        }
157
 
158
        // Test illegal option passing
159
        $ldif = new Net_LDAP2_LDIF($this->outfile, $mode, array('somebad' => 'option'));
160
        $this->assertType('Net_LDAP2_Error', $ldif->error());
161
 
162
        // Test passing custom handle
163
        $handle = fopen($this->outfile, 'r');
164
        $ldif = new Net_LDAP2_LDIF($handle, $mode, $this->defaultConfig);
165
        $this->assertTrue(is_resource($ldif->handle()));
166
 
167
        // Reading test with invalid file mode
168
        $ldif = new Net_LDAP2_LDIF($this->outfile, 'y', $this->defaultConfig);
169
        $this->assertNull($ldif->handle());
170
        $this->assertType('Net_LDAP2_Error', $ldif->error());
171
 
172
        // Reading test with nonexistent file
173
        $ldif = new Net_LDAP2_LDIF('some/nonexistent/file_for_net_ldap_ldif', 'r', $this->defaultConfig);
174
        $this->assertNull($ldif->handle());
175
        $this->assertType('Net_LDAP2_Error', $ldif->error());
176
 
177
        // writing to nonexistent file
178
        $ldif = new Net_LDAP2_LDIF('testfile_for_net_ldap_ldif', 'w', $this->defaultConfig);
179
        $this->assertTrue(is_resource($ldif->handle()));
180
        @unlink('testfile_for_net_ldap_ldif');
181
 
182
        // writing to nonexistent path
183
        $ldif = new Net_LDAP2_LDIF('some/nonexistent/file_for_net_ldap_ldif', 'w', $this->defaultConfig);
184
        $this->assertNull($ldif->handle());
185
        $this->assertType('Net_LDAP2_Error', $ldif->error());
186
 
187
        // writing to existing file but without permission
188
        // note: chmod should succeed since we do that in setUp()
189
        if (chmod($this->outfile, 0444)) {
190
            $ldif = new Net_LDAP2_LDIF($this->outfile, 'w', $this->defaultConfig);
191
            $this->assertNull($ldif->handle());
192
            $this->assertType('Net_LDAP2_Error', $ldif->error());
193
        } else {
194
            $this->markTestSkipped("Could not chmod ".$this->outfile.", write test without permission skipped");
195
        }
196
    }
197
 
198
    /**
199
     * Tests if entries from an LDIF file are correctly constructed
200
     */
201
    public function testRead_entry() {
202
        /*
203
        * UNIX line endings
204
        */
205
        $ldif = new Net_LDAP2_LDIF(dirname(__FILE__).'/ldif_data/unsorted_w50.ldif', 'r', $this->defaultConfig);
206
        $this->assertTrue(is_resource($ldif->handle()));
207
 
208
        $entries = array();
209
        do {
210
            $entry = $ldif->read_entry();
211
            $this->assertFalse((boolean)$ldif->error(), 'failed building entry from LDIF: '.$ldif->error(true));
212
            $this->assertType('Net_LDAP2_Entry', $entry);
213
            array_push($entries, $entry);
214
        } while (!$ldif->eof());
215
 
216
        $this->compareEntries($this->testentries, $entries);
217
 
218
        /*
219
        * Windows line endings
220
        */
221
        $ldif = new Net_LDAP2_LDIF(dirname(__FILE__).'/ldif_data/unsorted_w50_WIN.ldif', 'r', $this->defaultConfig);
222
        $this->assertTrue(is_resource($ldif->handle()));
223
 
224
        $entries = array();
225
        do {
226
            $entry = $ldif->read_entry();
227
            $this->assertFalse((boolean)$ldif->error(), 'failed building entry from LDIF: '.$ldif->error(true));
228
            $this->assertType('Net_LDAP2_Entry', $entry);
229
            array_push($entries, $entry);
230
        } while (!$ldif->eof());
231
 
232
        $this->compareEntries($this->testentries, $entries);
233
    }
234
 
235
    /**
236
     * Tests if entries are correctly written
237
     *
238
     * This tests converting entries to LDIF lines, wrapping, encoding, etc
239
     */
240
    public function testWrite_entry() {
241
        $testconf = $this->defaultConfig;
242
 
243
        /*
244
        * test wrapped operation
245
        */
246
        $testconf['wrap'] = 50;
247
        $testconf['sort'] = 0;
248
        $expected = array_map('conv_lineend', file(dirname(__FILE__).'/ldif_data/unsorted_w50.ldif'));
249
        // strip 4 starting lines because of comments in the file header:
250
        array_shift($expected);array_shift($expected);
251
        array_shift($expected);array_shift($expected);
252
 
253
        // Write LDIF
254
        $ldif = new Net_LDAP2_LDIF($this->outfile, 'w', $testconf);
255
        $this->assertTrue(is_resource($ldif->handle()));
256
        $ldif->write_entry($this->testentries);
257
        $this->assertFalse((boolean)$ldif->error(), 'Failed writing entry to '.$this->outfile.': '.$ldif->error(true));
258
        $ldif->done();
259
 
260
        // Compare files
261
        $this->assertEquals($expected, file($this->outfile));
262
 
263
 
264
        $testconf['wrap'] = 30;
265
        $testconf['sort'] = 0;
266
        $expected = array_map('conv_lineend', file(dirname(__FILE__).'/ldif_data/unsorted_w30.ldif'));
267
        // strip 4 starting lines because of comments in the file header:
268
        array_shift($expected);array_shift($expected);
269
        array_shift($expected);array_shift($expected);
270
 
271
        // Write LDIF
272
        $ldif = new Net_LDAP2_LDIF($this->outfile, 'w', $testconf);
273
        $this->assertTrue(is_resource($ldif->handle()));
274
        $ldif->write_entry($this->testentries);
275
        $this->assertFalse((boolean)$ldif->error(), 'Failed writing entry to '.$this->outfile.': '.$ldif->error(true));
276
        $ldif->done();
277
 
278
        // Compare files
279
        $this->assertEquals($expected, file($this->outfile));
280
 
281
 
282
 
283
        /*
284
        * Test unwrapped operation
285
        */
286
        $testconf['wrap'] = 40;
287
        $testconf['sort'] = 1;
288
        $expected = array_map('conv_lineend', file(dirname(__FILE__).'/ldif_data/sorted_w40.ldif'));
289
        // strip 4 starting lines because of comments in the file header:
290
        array_shift($expected);array_shift($expected);
291
        array_shift($expected);array_shift($expected);
292
 
293
        // Write LDIF
294
        $ldif = new Net_LDAP2_LDIF($this->outfile, 'w', $testconf);
295
        $this->assertTrue(is_resource($ldif->handle()));
296
        $ldif->write_entry($this->testentries);
297
        $this->assertFalse((boolean)$ldif->error(), 'Failed writing entry to '.$this->outfile.': '.$ldif->error(true));
298
        $ldif->done();
299
 
300
        // Compare files
301
        $this->assertEquals($expected, file($this->outfile));
302
 
303
 
304
        $testconf['wrap'] = 50;
305
        $testconf['sort'] = 1;
306
        $expected = array_map('conv_lineend', file(dirname(__FILE__).'/ldif_data/sorted_w50.ldif'));
307
        // strip 4 starting lines because of comments in the file header:
308
        array_shift($expected);array_shift($expected);
309
        array_shift($expected);array_shift($expected);
310
 
311
        // Write LDIF
312
        $ldif = new Net_LDAP2_LDIF($this->outfile, 'w', $testconf);
313
        $this->assertTrue(is_resource($ldif->handle()));
314
        $ldif->write_entry($this->testentries);
315
        $this->assertFalse((boolean)$ldif->error(), 'Failed writing entry to '.$this->outfile.': '.$ldif->error(true));
316
        $ldif->done();
317
 
318
        // Compare files
319
        $this->assertEquals($expected, file($this->outfile));
320
 
321
 
322
        /*
323
        * Test raw option
324
        */
325
        $testconf['wrap'] = 50;
326
        $testconf['sort'] = 1;
327
        $testconf['raw']  = '/attr6/';
328
        $expected = array_map('conv_lineend', file(dirname(__FILE__).'/ldif_data/sorted_w50.ldif'));
329
        // strip 4 starting lines because of comments in the file header:
330
        array_shift($expected);array_shift($expected);
331
        array_shift($expected);array_shift($expected);
332
 
333
        // Write LDIF
334
        $ldif = new Net_LDAP2_LDIF($this->outfile, 'w', $testconf);
335
        $this->assertTrue(is_resource($ldif->handle()));
336
        $ldif->write_entry($this->testentries);
337
        $this->assertFalse((boolean)$ldif->error(), 'Failed writing entry to '.$this->outfile.': '.$ldif->error(true));
338
        $ldif->done();
339
 
340
        // Compare files, with expected attr adjusted
341
        $this->assertEquals($expected, file($this->outfile));
342
 
343
 
344
        /*
345
        * Test writing with non entry as parameter
346
        */
347
        $ldif = new Net_LDAP2_LDIF($this->outfile, 'w');
348
        $this->assertTrue(is_resource($ldif->handle()));
349
        $ldif->write_entry('malformed_parameter');
350
        $this->assertTrue((boolean)$ldif->error());
351
    }
352
 
353
    /**
354
    * Test version writing
355
    *
356
    * In the default config, we supply a version which causes the ldif writer to
357
    * spill out a version int. However, since 2.0.0RC5, the default behavior changed,
358
    * so that no version is written by default (compatibility to perl interface).
359
    * The following code tests that new behavior.
360
    */
361
    public function testWriteVersion() {
362
        $testconf = $this->defaultConfig;
363
 
364
        $expected = array_map('conv_lineend', file(dirname(__FILE__).'/ldif_data/unsorted_w50.ldif'));
365
        // strip 4 starting lines because of comments in the file header:
366
        array_shift($expected);array_shift($expected);
367
        array_shift($expected);array_shift($expected);
368
 
369
        // strip 1 additional line (the "version: 1" line that should not be written now)
370
        // and adjust test config
371
        array_shift($expected);
372
        unset($testconf['version']);
373
 
374
        // Write LDIF
375
        $ldif = new Net_LDAP2_LDIF($this->outfile, 'w', $testconf);
376
        $this->assertTrue(is_resource($ldif->handle()));
377
        $ldif->write_entry($this->testentries);
378
        $this->assertFalse((boolean)$ldif->error(), 'Failed writing entry to '.$this->outfile.': '.$ldif->error(true));
379
        $ldif->done();
380
 
381
        // Compare files
382
        $this->assertEquals($expected, file($this->outfile));
383
    }
384
 
385
    /**
386
     * Round trip test: Read LDIF, parse to entries, write that to LDIF and compare both files
387
     */
388
    public function testReadWriteRead() {
389
        $ldif = new Net_LDAP2_LDIF(dirname(__FILE__).'/ldif_data/unsorted_w50.ldif', 'r', $this->defaultConfig);
390
        $this->assertTrue(is_resource($ldif->handle()));
391
 
392
        // Read LDIF
393
        $entries = array();
394
        do {
395
            $entry = $ldif->read_entry();
396
            $this->assertFalse((boolean)$ldif->error(), 'failed building entry from LDIF: '.$ldif->error(true));
397
            $this->assertType('Net_LDAP2_Entry', $entry);
398
            array_push($entries, $entry);
399
        } while (!$ldif->eof());
400
        $ldif->done();
401
 
402
         // Write LDIF
403
         $ldif = new Net_LDAP2_LDIF($this->outfile, 'w', $this->defaultConfig);
404
         $this->assertTrue(is_resource($ldif->handle()));
405
         $ldif->write_entry($entries);
406
         $this->assertFalse((boolean)$ldif->error(), 'Failed writing entry to '.$this->outfile.': '.$ldif->error(true));
407
         $ldif->done();
408
 
409
         // Compare files
410
         $expected = array_map('conv_lineend', file(dirname(__FILE__).'/ldif_data/unsorted_w50.ldif'));
411
         // strip 4 starting lines because of comments in the file header:
412
         array_shift($expected);array_shift($expected);
413
         array_shift($expected);array_shift($expected);
414
         $this->assertEquals($expected, file($this->outfile));
415
    }
416
 
417
    /**
418
     * Tests if entriy changes are correctly written
419
     */
420
    public function testWrite_entryChanges() {
421
        $testentries = $this->testentries;
422
        $testentries[] = Net_LDAP2_Entry::createFresh('cn=foo,ou=example,dc=cno', array('cn' => 'foo'));
423
        $testentries[] = Net_LDAP2_Entry::createFresh('cn=footest,ou=example,dc=cno', array('cn' => 'foo'));
424
 
425
        $testconf = $this->defaultConfig;
426
        $testconf['change'] = 1;
427
 
428
        /*
429
        * no changes should produce empty file
430
        */
431
        $ldif = new Net_LDAP2_LDIF($this->outfile, 'w', $testconf);
432
        $this->assertTrue(is_resource($ldif->handle()));
433
        $ldif->write_entry($testentries);
434
        $this->assertFalse((boolean)$ldif->error(), 'Failed writing entry to '.$this->outfile.': '.$ldif->error(true));
435
        $ldif->done();
436
        $this->assertEquals(array(), file($this->outfile));
437
 
438
        /*
439
        * changes test
440
        */
441
        //prepare some changes
442
        $testentries[0]->delete('attr1'); // del whole attr
443
        $testentries[0]->delete(array('attr2' => 'baz')); // del spec. value
444
        $testentries[0]->delete(array('attr4', 'attr3' => 'bar')); // del mixed
445
 
446
        // prepare some replaces and adds
447
        $testentries[2]->replace(array('attr1' => 'newvaluefor1'));
448
        $testentries[2]->replace(array('attr2' => array('newvalue1for2', 'newvalue2for2')));
449
        $testentries[2]->replace(array('attr3' => ''));      // will result in delete
450
        $testentries[2]->replace(array('newattr' => 'foo')); // will result in add
451
 
452
        // delete whole entry
453
        $testentries[3]->delete();
454
 
455
        // rename and move
456
        $testentries[4]->dn('cn=Bar,ou=example,dc=cno');
457
        $testentries[5]->dn('cn=foobartest,ou=newexample,dc=cno');
458
 
459
        // carry out write
460
        $ldif = new Net_LDAP2_LDIF($this->outfile, 'w', $testconf);
461
        $this->assertTrue(is_resource($ldif->handle()));
462
        $ldif->write_entry($testentries);
463
        $this->assertFalse((boolean)$ldif->error(), 'Failed writing entry to '.$this->outfile.': '.$ldif->error(true));
464
        $ldif->done();
465
 
466
        //compare results
467
        $expected = array_map('conv_lineend', file(dirname(__FILE__).'/ldif_data/changes.ldif'));
468
        // strip 4 starting lines because of comments in the file header:
469
        array_shift($expected);array_shift($expected);
470
        array_shift($expected);array_shift($expected);
471
        $this->assertEquals($expected, file($this->outfile));
472
    }
473
 
474
    /**
475
    * Tests if syntax errors are detected
476
    *
477
    * The used LDIF files have several damaged entries but always one
478
    * correct too, to test if Net_LDAP2_LDIF is continue reading as it should
479
    * Each Entry must have 2 correct attributes.
480
    */
481
    public function testSyntaxerrors() {
482
        // Test malformed encoding
483
        // I think we can ignore this test, because if the LDIF is not encoded properly, we
484
        // might be able to successfully fetch the entries data. However, it is possible
485
        // that it will be corrupted, but thats not our fault then.
486
        // If we should catch that error, we must adjust Net_LDAP2_LDIF::next_lines().
487
        /*
488
        $ldif = new Net_LDAP2_LDIF(dirname(__FILE__).'/ldif_data/malformed_encoding.ldif', 'r', $this->defaultConfig);
489
        $this->assertFalse((boolean)$ldif->error());
490
        $entries = array();
491
        do {
492
            $entry = $ldif->read_entry();
493
            if ($entry) {
494
                // the correct attributes need to be parsed
495
                $this->assertThat(count(array_keys($entry->getValues())), $this->equalTo(2));
496
                $entries[] = $entry;
497
            }
498
        } while (!$ldif->eof());
499
        $this->assertTrue((boolean)$ldif->error());
500
        $this->assertThat($ldif->error_lines(), $this->greaterThan(1));
501
        $this->assertThat(count($entries), $this->equalTo(1));
502
        */
503
 
504
        // Test malformed syntax
505
        $ldif = new Net_LDAP2_LDIF(dirname(__FILE__).'/ldif_data/malformed_syntax.ldif', 'r', $this->defaultConfig);
506
        $this->assertFalse((boolean)$ldif->error());
507
        $entries = array();
508
        do {
509
            $entry = $ldif->read_entry();
510
            if ($entry) {
511
                // the correct attributes need to be parsed
512
                $this->assertThat(count(array_keys($entry->getValues())), $this->equalTo(2));
513
                $entries[] = $entry;
514
            }
515
        } while (!$ldif->eof());
516
        $this->assertTrue((boolean)$ldif->error());
517
        $this->assertThat($ldif->error_lines(), $this->greaterThan(1));
518
        $this->assertThat(count($entries), $this->equalTo(2));
519
 
520
        // test bad wrapping
521
        $ldif = new Net_LDAP2_LDIF(dirname(__FILE__).'/ldif_data/malformed_wrapping.ldif', 'r', $this->defaultConfig);
522
        $this->assertFalse((boolean)$ldif->error());
523
        $entries = array();
524
        do {
525
           $entry = $ldif->read_entry();
526
            if ($entry) {
527
                // the correct attributes need to be parsed
528
                $this->assertThat(count(array_keys($entry->getValues())), $this->equalTo(2));
529
                $entries[] = $entry;
530
            }
531
        } while (!$ldif->eof());
532
        $this->assertTrue((boolean)$ldif->error());
533
        $this->assertThat($ldif->error_lines(), $this->greaterThan(1));
534
        $this->assertThat(count($entries), $this->equalTo(2));
535
    }
536
 
537
    /**
538
     * Test error dropping functionality
539
     */
540
    public function testError() {
541
        // NO error:
542
        $ldif = new Net_LDAP2_LDIF(dirname(__FILE__).'/ldif_data/unsorted_w50.ldif', 'r', $this->defaultConfig);
543
        $this->assertFalse((boolean)$ldif->error());
544
 
545
        // Error giving error msg and line number:
546
        $ldif = new Net_LDAP2_LDIF(dirname(__FILE__).'/some_not_existing/path/for/net_ldap_ldif', 'r', $this->defaultConfig);
547
        $this->assertTrue((boolean)$ldif->error());
548
        $this->assertType('Net_LDAP2_Error', $ldif->error());
549
        $this->assertType('string', $ldif->error(true));
550
        $this->assertType('int', $ldif->error_lines());
551
        $this->assertThat(strlen($ldif->error(true)), $this->greaterThan(0));
552
 
553
        // Test for line number reporting
554
        $ldif = new Net_LDAP2_LDIF(dirname(__FILE__).'/ldif_data/malformed_syntax.ldif', 'r', $this->defaultConfig);
555
        $this->assertFalse((boolean)$ldif->error());
556
        do { $entry = $ldif->read_entry(); } while (!$ldif->eof());
557
        $this->assertTrue((boolean)$ldif->error());
558
        $this->assertThat($ldif->error_lines(), $this->greaterThan(1));
559
    }
560
 
561
    /**
562
     * Tests current_lines() and next_lines().
563
     *
564
     * This should always return the same lines unless forced
565
     */
566
    public function testLineMethods() {
567
        $ldif = new Net_LDAP2_LDIF(dirname(__FILE__).'/ldif_data/unsorted_w50.ldif', 'r', $this->defaultConfig);
568
        $this->assertFalse((boolean)$ldif->error());
569
        $this->assertEquals(array(), $ldif->current_lines(), 'Net_LDAP2_LDIF initialization error!');
570
 
571
        // read first lines
572
        $lines = $ldif->next_lines();
573
        $this->assertFalse((boolean)$ldif->error(), 'unable to read first lines');
574
 
575
        // read the first lines several times and test
576
        for ($i = 0; $i <= 10; $i++) {
577
            $r_lines = $ldif->next_lines();
578
            $this->assertFalse((boolean)$ldif->error());
579
            $this->assertEquals($lines, $r_lines);
580
        }
581
 
582
        // now force to iterate and see if the content changes
583
        $r_lines = $ldif->next_lines(true);
584
        $this->assertFalse((boolean)$ldif->error());
585
        $this->assertNotEquals($lines, $r_lines);
586
 
587
        // it could be confusing to some people, but calling
588
        // current_entry would not work now, like the description
589
        // of the method says.
590
        $no_entry = $ldif->current_lines();
591
        $this->assertEquals(array(), $no_entry);
592
    }
593
 
594
    /**
595
     * Tests current_entry(). This should always return the same object
596
     */
597
    public function testCurrent_entry() {
598
        $ldif = new Net_LDAP2_LDIF(dirname(__FILE__).'/ldif_data/unsorted_w50.ldif', 'r', $this->defaultConfig);
599
        $this->assertFalse((boolean)$ldif->error());
600
 
601
        // read first entry
602
        $entry = $ldif->read_entry();
603
        $this->assertFalse((boolean)$ldif->error(), 'First entry failed: '.$ldif->error(true));
604
 
605
        // test if current_Entry remains the first one
606
        for ($i = 0; $i <= 10; $i++) {
607
            $e = $ldif->current_entry();
608
            $this->assertFalse((boolean)$ldif->error(), $ldif->error(true));
609
            $this->assertEquals($entry, $e);
610
        }
611
    }
612
 
613
 
614
 
615
 
616
 
617
    /**
618
    * Compare Net_LDAP2_Entries
619
    *
620
    * This helper function compares two entries (or array of entries)
621
    * and tells if they are equal. They are equal if all DNs from
622
    * the first crowd exist in the second AND each attribute is present
623
    * and equal at the respicitve entry.
624
    * The search is case sensitive.
625
    *
626
    * @param array|Net_LDAP2_Entry $entry1
627
    * @param array|Net_LDAP2_Entry $entry2
628
    * @return true|false
629
    */
630
    function compareEntries($entry1, $entry2) {
631
        if (!is_array($entry1)) $entry1 = array($entry1);
632
        if (!is_array($entry2)) $entry2 = array($entry2);
633
 
634
        $entries_data1  = array();
635
        $entries_data2  = array();
636
 
637
        // step 1: extract and sort data
638
        foreach ($entry1 as $e) {
639
            $values = $e->getValues();
640
            foreach ($values as $attr_name => $attr_values) {
641
                if (!is_array($attr_values)) $attr_values = array($attr_values);
642
                $values[$attr_name] = $attr_values;
643
            }
644
            $entries_data1[$e->dn()] = $values;
645
        }
646
        foreach ($entry2 as $e) {
647
            $values = $e->getValues();
648
            foreach ($values as $attr_name => $attr_values) {
649
                if (!is_array($attr_values)) $attr_values = array($attr_values);
650
                $values[$attr_name] = $attr_values;
651
            }
652
            $entries_data2[$e->dn()] = $values;
653
        }
654
 
655
        // step 2: compare DNs (entries)
656
        $this->assertEquals(array_keys($entries_data1), array_keys($entries_data2), 'Entries DNs not equal! (missing entry or wrong DN)');
657
 
658
        // step 3: look for attribute existence and compare values
659
        foreach ($entries_data1 as $dn => $attributes) {
660
            $this->assertEquals($entries_data1[$dn], $entries_data2[$dn], 'Entries '.$dn.' attributes are not equal');
661
            foreach ($attributes as $attr_name => $attr_values) {
662
                $this->assertEquals(0, count(array_diff($entries_data1[$dn][$attr_name], $entries_data2[$dn][$attr_name])), 'Entries '.$dn.' attribute '.$attr_name.' values are not equal');
663
            }
664
        }
665
 
666
        return true;
667
    }
668
 
669
}
670
 
671
// Call Net_LDAP2_LDIFTest::main() if this source file is executed directly.
672
if (PHPUnit_MAIN_METHOD == "Net_LDAP2_LDIFTest::main") {
673
    Net_LDAP2_LDIFTest::main();
674
}
675
 
676
if (!function_exists('conv_lineend')) {
677
    /**
678
    * Function transfers line endings to current OS
679
    *
680
    * This is neccessary to make write tests platform indendent.
681
    *
682
    * @param string $line Line
683
    * @return string
684
    */
685
    function conv_lineend($line) {
686
        return rtrim($line).PHP_EOL;
687
    }
688
}
689
?>