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