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 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
|
<?php
// Call Net_LDAP2Test::main() if this source file is executed directly.
if (!defined("PHPUnit_MAIN_METHOD")) {
define("PHPUnit_MAIN_METHOD", "Net_LDAP2Test::main");
}
require_once "PHPUnit/Framework/TestCase.php";
require_once "PHPUnit/Framework/TestSuite.php";
require_once 'Net/LDAP2.php';
require_once 'Net/LDAP2/Entry.php';
/**
* Test class for Net_LDAP2.
* Generated by PHPUnit_Util_Skeleton on 2007-10-09 at 10:32:36.
*/
class Net_LDAP2Test extends PHPUnit_Framework_TestCase {
/**
* Stores the LDAP configuration
*/
var $ldapcfg = false;
/**
* 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_LDAP2Test");
$result = PHPUnit_TextUI_TestRunner::run($suite);
}
/**
* Load ldap config and adjust appropriately
*
* @access protected
*/
protected function setUp() {
$this->ldapcfg = $this->getTestConfig();
}
/**
* Tears down the fixture, for example, close a network connection.
* This method is called after a test is executed.
*
* @access protected
*/
protected function tearDown() {
}
/**
* This checks if a valid LDAP testconfig is present and loads it.
*
* If so, it is loaded and returned as array. If not, false is returned.
*
* @return false|array
*/
public function getTestConfig() {
$config = false;
$file = dirname(__FILE__).'/ldapconfig.ini';
if (file_exists($file) && is_readable($file)) {
$config = parse_ini_file($file, true);
} else {
return false;
}
// validate ini
$v_error = $file.' is probably invalid. Did you quoted values correctly?';
$this->assertTrue(array_key_exists('global', $config), $v_error);
$this->assertTrue(array_key_exists('test', $config), $v_error);
$this->assertEquals(7, count($config['global']), $v_error);
$this->assertEquals(7, count($config['test']), $v_error);
// reformat things a bit, for convinience
$config['global']['server_binddn'] =
$config['global']['server_binddn'].','.$config['global']['server_base_dn'];
$config['test']['existing_attrmv'] = explode('|', $config['test']['existing_attrmv']);
return $config;
}
/**
* Establishes a working connection
*
* @return Net_LDAP2
*/
public function &connect() {
// Check extension
if (true !== Net_LDAP2::checkLDAPExtension()) {
$this->markTestSkipped('PHP LDAP extension not found or not loadable. Skipped Test.');
}
// Simple working connect and privilegued bind
$lcfg = array(
'host' => $this->ldapcfg['global']['server_address'],
'port' => $this->ldapcfg['global']['server_port'],
'basedn' => $this->ldapcfg['global']['server_base_dn'],
'binddn' => $this->ldapcfg['global']['server_binddn'],
'bindpw' => $this->ldapcfg['global']['server_bindpw'],
'filter' => '(ou=*)',
);
$ldap = Net_LDAP2::connect($lcfg);
$this->assertType('Net_LDAP2', $ldap, 'Connect failed but was supposed to work. Check credentials and host address. If those are correct, file a bug!');
return $ldap;
}
/* ---------- TESTS ---------- */
/**
* testCheckLDAPExtension().
*
* @todo can we unload modules at runtime??
*/
public function testCheckLDAPExtension() {
if (extension_loaded('ldap')) {
// If extension is already loaded, then we must get true.
$this->assertTrue(Net_LDAP2::checkLDAPExtension());
} else {
// If not, we should be able to load it - but may fail
$this->assertThat(Net_LDAP2::checkLDAPExtension(),
$this->logicalOr($this->isInstanceOf('Net_LDAP2_Error'), $this->equalTo(true)));
}
}
/**
* Tests if getVersion() works correctly
*/
public function testGetVersion() {
$this->assertTrue(defined('NET_LDAP2_VERSION'));
$this->assertEquals(NET_LDAP2_VERSION, Net_LDAP2::getVersion());
}
/**
* Tests if the server can connect and bind correctly
*/
public function testConnectAndPrivileguedBind() {
// Check extension
if (true !== Net_LDAP2::checkLDAPExtension()) {
$this->markTestSkipped('PHP LDAP extension not found or not loadable. Skipped Test.');
}
if (!$this->ldapcfg) {
$this->markTestSkipped('No ldapconfig.ini found. Skipping test!');
} else {
// This connect is supposed to fail
$lcfg = array(
'host' => 'pear.net-ldap.test.hostunknown.cno',
);
$ldap = Net_LDAP2::connect($lcfg);
$this->assertType('Net_LDAP2_Error', $ldap, 'Connect succeeded but was supposed to fail!');
// Failing with multiple hosts
$lcfg = array(
'host' => array('pear.net-ldap.test.hostunknown1.cno', 'pear.net-ldap.test.hostunknown2.cno'),
);
$ldap = Net_LDAP2::connect($lcfg);
$this->assertType('Net_LDAP2_Error', $ldap, 'Connect succeeded but was supposed to fail!');
// Simple working connect and privilegued bind
$ldap =& $this->connect();
// Working connect and privilegued bind with first host down
$lcfg = array(
'host' => array(
'pear.net-ldap.test.hostunknown1.cno',
$this->ldapcfg['global']['server_address']
),
'port' => $this->ldapcfg['global']['server_port'],
'binddn' => $this->ldapcfg['global']['server_binddn'],
'bindpw' => $this->ldapcfg['global']['server_bindpw'],
);
$ldap = Net_LDAP2::connect($lcfg);
$this->assertType('Net_LDAP2', $ldap, 'Connect failed but was supposed to work. Check credentials and host address. If those are correct, file a bug!');
}
}
/**
* Tests if the server can connect and bind anonymously, if supported (->cfg and ldap mode)
*/
public function testConnectAndAnonymousBind() {
// Check extension
if (true !== Net_LDAP2::checkLDAPExtension()) {
$this->markTestSkipped('PHP LDAP extension not found or not loadable. Skipped Test.');
}
if (!$this->ldapcfg) {
$this->markTestSkipped('No ldapconfig.ini found. Skipping test!');
} elseif ($this->ldapcfg['global']['server_cap_anonymous'] == true) {
// Simple working connect and anonymous bind
$lcfg = array(
'host' => $this->ldapcfg['global']['server_address'],
'port' => $this->ldapcfg['global']['server_port'],
);
$ldap = Net_LDAP2::connect($lcfg);
$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!');
} else {
$this->markTestSkipped('Server does not support anonymous bind (see ldapconfig.ini). Skipping test.');
}
}
/**
* testStartTLS() if server supports it
*/
public function testStartTLS() {
// Check extension
if (true !== Net_LDAP2::checkLDAPExtension()) {
$this->markTestSkipped('PHP LDAP extension not found or not loadable. Skipped Test.');
}
if (!$this->ldapcfg) {
$this->markTestSkipped('No ldapconfig.ini found. Skipping test!');
} elseif ($this->ldapcfg['global']['server_cap_tls'] == true) {
// Simple working connect and privilegued bind
$lcfg = array(
'host' => $this->ldapcfg['global']['server_address'],
'port' => $this->ldapcfg['global']['server_port'],
'binddn' => $this->ldapcfg['global']['server_binddn'].','.$this->ldapcfg['global']['server_binddn'],
'bindpw' => $this->ldapcfg['global']['server_bindpw'],
'starttls' => true
);
$ldap = Net_LDAP2::connect();
$this->assertType('Net_LDAP2', $ldap, 'Connect failed but was supposed to work. Check credentials and host address. If those are correct, file a bug!');
} else {
$this->markTestSkipped('Server does not support TLS (see ldapconfig.ini). Skipping test.');
}
}
/**
* Test if adding and deleting a fresh entry works
*/
public function testAdd() {
if (!$this->ldapcfg) {
$this->markTestSkipped('No ldapconfig.ini found. Skipping test!');
} else {
$ldap =& $this->connect();
// Adding a fresh entry
$cn = 'Net-LDAP-TestEntry';
$dn = 'cn='.$cn.','.$this->ldapcfg['global']['server_base_dn'];
$fresh_entry = Net_LDAP2_Entry::createFresh($dn,
array(
'objectClass' => array('top','person'),
'cn' => $cn,
'sn' => 'TestEntry'
)
);
$this->assertType('Net_LDAP2_Entry', $fresh_entry);
$this->assertTrue($ldap->add($fresh_entry));
// Deleting this Entry
$this->assertTrue($ldap->delete($fresh_entry), 'Deletion of entry failed!');
}
}
/**
* testDelete().
*
* Basic deletion is tested in testAdd(), so here we just test if
* advanced deletion tasks work properly.
*/
public function testDelete() {
if (!$this->ldapcfg) {
$this->markTestSkipped('No ldapconfig.ini found. Skipping test!');
} else {
$ldap =& $this->connect();
// some parameter checks
$this->assertType('Net_LDAP2_Error', $ldap->delete(1234));
$this->assertType('Net_LDAP2_Error', $ldap->delete($ldap));
// in order to test subtree deletion, we need some little tree
// which we need to establish first
$base = $this->ldapcfg['global']['server_base_dn'];
$testdn = 'ou=Net_LDAP2_Test_subdelete,'.$base;
$ou = Net_LDAP2_Entry::createFresh($testdn,
array(
'objectClass' => array('top','organizationalUnit'),
'ou' => 'Net_LDAP2_Test_subdelete'
));
$ou_1 = Net_LDAP2_Entry::createFresh('ou=test1,'.$testdn,
array(
'objectClass' => array('top','organizationalUnit'),
'ou' => 'test1'
));
$ou_1_l1 = Net_LDAP2_Entry::createFresh('l=subtest,ou=test1,'.$testdn,
array(
'objectClass' => array('top','locality'),
'l' => 'test1'
));
$ou_2 = Net_LDAP2_Entry::createFresh('ou=test2,'.$testdn,
array(
'objectClass' => array('top','organizationalUnit'),
'ou' => 'test2'
));
$ou_3 = Net_LDAP2_Entry::createFresh('ou=test3,'.$testdn,
array(
'objectClass' => array('top','organizationalUnit'),
'ou' => 'test3'
));
$this->assertTrue($ldap->add($ou));
$this->assertTrue($ldap->add($ou_1));
$this->assertTrue($ldap->add($ou_1_l1));
$this->assertTrue($ldap->add($ou_2));
$this->assertTrue($ldap->add($ou_3));
$this->assertTrue($ldap->dnExists($ou->dn()));
$this->assertTrue($ldap->dnExists($ou_1->dn()));
$this->assertTrue($ldap->dnExists($ou_1_l1->dn()));
$this->assertTrue($ldap->dnExists($ou_2->dn()));
$this->assertTrue($ldap->dnExists($ou_3->dn()));
// Tree established now. We can run some tests now :D
// Try to delete some non existent entry inside that subtree (fails)
$this->assertType('Net_LDAP2_Error', $ldap->delete(
'cn=not_existent,ou=test1,'.$testdn));
// Try to delete main test ou without recursive set (fails too)
$this->assertType('Net_LDAP2_Error', $ldap->delete($testdn));
// Retry with subtree delete, this should work
$this->assertTrue($ldap->delete($testdn, true));
// The DN is not allowed to exist anymore
$this->assertFalse($ldap->dnExists($testdn));
}
}
/**
* testModify().
*/
public function testModify() {
if (!$this->ldapcfg) {
$this->markTestSkipped('No ldapconfig.ini found. Skipping test!');
} else {
$ldap =& $this->connect();
// We need a test entry:
$local_entry = Net_LDAP2_Entry::createFresh(
'ou=Net_LDAP2_Test_modify,'.$this->ldapcfg['global']['server_base_dn'],
array(
'objectClass' => array('top','organizationalUnit'),
'ou' => 'Net_LDAP2_Test_modify',
'street' => 'Beniroad',
'telephoneNumber' => array('1234', '5678'),
'postalcode' => '12345',
'postalAddress' => 'someAddress',
'facsimileTelephoneNumber' => array('123','456')
));
$this->assertTrue($ldap->add($local_entry));
$this->assertTrue($ldap->dnExists($local_entry->dn()));
// Prepare some changes
$changes = array(
'add' => array(
'businessCategory' => array('foocat', 'barcat'),
'description' => 'testval'
),
'delete' => array('postalAddress'),
'replace' => array('telephoneNumber' => array('345', '567')),
'changes' => array(
'replace' => array('street' => 'Highway to Hell'),
'add' => array('l' => 'someLocality'),
'delete' => array(
'postalcode',
'facsimileTelephoneNumber' => array('123'))
)
);
// Perform those changes
$this->assertTrue($ldap->modify($local_entry, $changes));
// verify correct attribute changes
$actual_entry = $ldap->getEntry($local_entry->dn(), array(
'objectClass', 'ou','postalAddress', 'street', 'telephoneNumber', 'postalcode',
'facsimileTelephoneNumber', 'l', 'businessCategory', 'description'));
$this->assertType('Net_LDAP2_Entry', $actual_entry);
$expected_attributes = array(
'objectClass' => array('top', 'organizationalUnit'),
'ou' => 'Net_LDAP2_Test_modify',
'street' => 'Highway to Hell',
'l' => 'someLocality',
'telephoneNumber' => array('345', '567'),
'businessCategory' => array('foocat', 'barcat'),
'description' => 'testval',
'facsimileTelephoneNumber' => '456'
);
$local_attributes = $local_entry->getValues();
$actual_attributes = $actual_entry->getValues();
// to enable easy check, we need to sort the
// values of the remaining multival attrs as
// well as the attribute names
ksort($expected_attributes);
ksort($local_attributes);
ksort($actual_attributes);
sort($expected_attributes['businessCategory']);
sort($local_attributes['businessCategory']);
sort($actual_attributes['businessCategory']);
// cleanup directory
$this->assertTrue($ldap->delete($actual_entry),
'Cleanup of test entry failed. Please remove manually: '.$local_entry->dn());
// The attributes must match the expected values.
// Both, the entry inside the directory and our
// apps local copy must reflect the same values
$this->assertEquals($expected_attributes, $actual_attributes, 'The directory entries attributes are not OK!');
$this->assertEquals($expected_attributes, $local_attributes, 'The local entries attributes are not OK!');
}
}
/**
* testSearch().
*/
public function testSearch() {
if (!$this->ldapcfg) {
$this->markTestSkipped('No ldapconfig.ini found. Skipping test!');
} else {
$ldap =& $this->connect();
// some testdata, so we can test sizelimit
$base = $this->ldapcfg['global']['server_base_dn'];
$ou1 = Net_LDAP2_Entry::createFresh('ou=Net_LDAP2_Test_search1,'.$base,
array(
'objectClass' => array('top','organizationalUnit'),
'ou' => 'Net_LDAP2_Test_search1'
));
$ou1_1 = Net_LDAP2_Entry::createFresh('ou=Net_LDAP2_Test_search1_1,'.$ou1->dn(),
array(
'objectClass' => array('top','organizationalUnit'),
'ou' => 'Net_LDAP2_Test_search2'
));
$ou2 = Net_LDAP2_Entry::createFresh('ou=Net_LDAP2_Test_search2,'.$base,
array(
'objectClass' => array('top','organizationalUnit'),
'ou' => 'Net_LDAP2_Test_search2'
));
$this->assertTrue($ldap->add($ou1));
$this->assertTrue($ldap->dnExists($ou1->dn()));
$this->assertTrue($ldap->add($ou1_1));
$this->assertTrue($ldap->dnExists($ou1_1->dn()));
$this->assertTrue($ldap->add($ou2));
$this->assertTrue($ldap->dnExists($ou2->dn()));
// Search for testfilter, should at least return our two test entries
$res = $ldap->search(null, '(ou=Net_LDAP2*)',
array('attributes' => '1.1')
);
$this->assertType('Net_LDAP2_Search', $res);
$this->assertThat($res->count(), $this->greaterThanOrEqual(2));
// Same, but with Net_LDAP2_Filter object
$filtero = Net_LDAP2_Filter::create('ou', 'begins', 'Net_LDAP2');
$this->assertType('Net_LDAP2_Filter', $filtero);
$res = $ldap->search(null, $filtero,
array('attributes' => '1.1')
);
$this->assertType('Net_LDAP2_Search', $res);
$this->assertThat($res->count(), $this->greaterThanOrEqual(2));
// Search using default filter for base-onelevel scope
// should at least return our two test entries
$res = $ldap->search(null, null,
array('scope' => 'one', 'attributes' => '1.1')
);
$this->assertType('Net_LDAP2_Search', $res);
$this->assertThat($res->count(), $this->greaterThanOrEqual(2));
// Base-search using custom base (string)
// should only return the test entry $ou1 and not the entry below it.
$res = $ldap->search($ou1->dn(), null,
array('scope' => 'base', 'attributes' => '1.1')
);
$this->assertType('Net_LDAP2_Search', $res);
$this->assertEquals(1, $res->count());
// Search using custom base, this time using an entry object
// This tests if passing an entry object as base works
// should only return the test entry $ou1
$res = $ldap->search($ou1, '(ou=*)',
array('scope' => 'base', 'attributes' => '1.1')
);
$this->assertType('Net_LDAP2_Search', $res);
$this->assertEquals(1, $res->count());
// Search using default filter for base-onelevel scope with sizelimit
// should of course return more than one entry,
// but not more than sizelimit
$res = $ldap->search(null, null,
array('scope' => 'one', 'sizelimit' => 1, 'attributes' => '1.1')
);
$this->assertType('Net_LDAP2_Search', $res);
$this->assertEquals(1, $res->count());
$this->assertTrue($res->sizeLimitExceeded()); // sizelimit should be exceeded now
// Bad filter
$res = $ldap->search(null, 'somebadfilter',
array('attributes' => '1.1')
);
$this->assertType('Net_LDAP2_Error', $res);
// Bad base
$res = $ldap->search('badbase', null,
array('attributes' => '1.1')
);
$this->assertType('Net_LDAP2_Error', $res);
// Passing Error object as base and as filter object
$error = new Net_LDAP2_Error('Testerror');
$res = $ldap->search($error, null, // error base
array('attributes' => '1.1')
);
$this->assertType('Net_LDAP2_Error', $res);
$res = $ldap->search(null, $error, // error filter
array('attributes' => '1.1')
);
$this->assertType('Net_LDAP2_Error', $res);
// Nullresult
$res = $ldap->search(null, '(cn=nevermatching_filter)',
array('scope' => 'base', 'attributes' => '1.1')
);
$this->assertType('Net_LDAP2_Search', $res);
$this->assertEquals(0, $res->count());
// cleanup
$this->assertTrue($ldap->delete($ou1_1), 'Cleanup failed, please delete manually');
$this->assertTrue($ldap->delete($ou1), 'Cleanup failed, please delete manually');
$this->assertTrue($ldap->delete($ou2), 'Cleanup failed, please delete manually');
}
}
/**
* @todo Implement testSetOption().
*/
public function testSetOption() {
if (!$this->ldapcfg) {
$this->markTestSkipped('No ldapconfig.ini found. Skipping test!');
} else {
$this->markTestIncomplete("This test has not been implemented yet.");
}
}
/**
* @todo Implement testGetOption().
*/
public function testGetOption() {
if (!$this->ldapcfg) {
$this->markTestSkipped('No ldapconfig.ini found. Skipping test!');
} else {
$this->markTestIncomplete("This test has not been implemented yet.");
}
}
/**
* @todo Implement testGetLDAPVersion().
*/
public function testGetLDAPVersion() {
if (!$this->ldapcfg) {
$this->markTestSkipped('No ldapconfig.ini found. Skipping test!');
} else {
$this->markTestIncomplete("This test has not been implemented yet.");
}
}
/**
* @todo Implement testSetLDAPVersion().
*/
public function testSetLDAPVersion() {
if (!$this->ldapcfg) {
$this->markTestSkipped('No ldapconfig.ini found. Skipping test!');
} else {
$this->markTestIncomplete("This test has not been implemented yet.");
}
}
/**
* testDnExists().
*/
public function testDnExists() {
if (!$this->ldapcfg) {
$this->markTestSkipped('No ldapconfig.ini found. Skipping test!');
} else {
$ldap =& $this->connect();
$dn = $this->ldapcfg['test']['existing_entry'].','.$this->ldapcfg['global']['server_base_dn'];
// Testing existing and not existing DN; neither should produce an error
$this->assertTrue($ldap->dnExists($dn));
$this->assertFalse($ldap->dnExists('cn=not_existent,'.$dn));
// Passing an Entry object (should work)
// It should return false, because don't add the test entry
$base = $this->ldapcfg['global']['server_base_dn'];
$ou1 = Net_LDAP2_Entry::createFresh('ou=Net_LDAP2_Test_search1,'.$base,
array(
'objectClass' => array('top','organizationalUnit'),
'ou' => 'Net_LDAP2_Test_search1'
));
$this->assertFalse($ldap->dnExists($ou1));
// Passing an float instead of a string
$this->assertType('Net_LDAP2_Error', $ldap->dnExists(1.234));
// Pasing an error object
$error = new Net_LDAP2_Error('Testerror');
$this->assertType('Net_LDAP2_Error', $ldap->dnExists($error));
}
}
/**
* testGetEntry().
*/
public function testGetEntry() {
if (!$this->ldapcfg) {
$this->markTestSkipped('No ldapconfig.ini found. Skipping test!');
} else {
$ldap =& $this->connect();
$dn = $this->ldapcfg['test']['existing_entry'].','.$this->ldapcfg['global']['server_base_dn'];
// existing DN
$this->assertType('Net_LDAP2_Entry', $ldap->getEntry($dn), "$dn was supposed to be found. Please check your ldapconfig.ini!");
// Not existing DN
$this->assertType('Net_LDAP2_Error',
$ldap->getEntry('cn=notexistent,'.$this->ldapcfg['global']['server_base_dn']));
}
}
/**
* testMove().
*/
public function testMove() {
if (!$this->ldapcfg) {
$this->markTestSkipped('No ldapconfig.ini found. Skipping test!');
} else {
$ldap =& $this->connect();
// For Moving tests, we need some little tree again
$base = $this->ldapcfg['global']['server_base_dn'];
$testdn = 'ou=Net_LDAP2_Test_moves,'.$base;
$ou = Net_LDAP2_Entry::createFresh($testdn,
array(
'objectClass' => array('top','organizationalUnit'),
'ou' => 'Net_LDAP2_Test_moves'
));
$ou_1 = Net_LDAP2_Entry::createFresh('ou=source,'.$testdn,
array(
'objectClass' => array('top','organizationalUnit'),
'ou' => 'source'
));
$ou_1_l1 = Net_LDAP2_Entry::createFresh('l=moveitem,ou=source,'.$testdn,
array(
'objectClass' => array('top','locality'),
'l' => 'moveitem',
'description' => 'movetest'
));
$ou_2 = Net_LDAP2_Entry::createFresh('ou=target,'.$testdn,
array(
'objectClass' => array('top','organizationalUnit'),
'ou' => 'target'
));
$ou_3 = Net_LDAP2_Entry::createFresh('ou=target_otherdir,'.$testdn,
array(
'objectClass' => array('top','organizationalUnit'),
'ou' => 'target_otherdir'
));
$this->assertTrue($ldap->add($ou));
$this->assertTrue($ldap->add($ou_1));
$this->assertTrue($ldap->add($ou_1_l1));
$this->assertTrue($ldap->add($ou_2));
$this->assertTrue($ldap->add($ou_3));
$this->assertTrue($ldap->dnExists($ou->dn()));
$this->assertTrue($ldap->dnExists($ou_1->dn()));
$this->assertTrue($ldap->dnExists($ou_1_l1->dn()));
$this->assertTrue($ldap->dnExists($ou_2->dn()));
$this->assertTrue($ldap->dnExists($ou_3->dn()));
// Tree established
// Local rename
$olddn = $ou_1_l1->currentDN();
$this->assertTrue($ldap->move($ou_1_l1,
str_replace('moveitem', 'move_item', $ou_1_l1->dn())));
$this->assertTrue($ldap->dnExists($ou_1_l1->dn()));
$this->assertFalse($ldap->dnExists($olddn));
// Local move
$olddn = $ou_1_l1->currentDN();
$this->assertTrue($ldap->move($ou_1_l1, 'l=move_item,'.$ou_2->dn()));
$this->assertTrue($ldap->dnExists($ou_1_l1->dn()));
$this->assertFalse($ldap->dnExists($olddn));
// Local move backward, with rename
// Here we use the DN of the object, to test DN conversion.
// Note that this will outdate the object since it does not
// has knowledge about the move.
$olddn = $ou_1_l1->currentDN();
$newdn = 'l=moveditem,'.$ou_2->dn();
$this->assertTrue($ldap->move($olddn, $newdn));
$this->assertTrue($ldap->dnExists($newdn));
$this->assertFalse($ldap->dnExists($olddn));
$ou_1_l1 = $ldap->getEntry($newdn); // Refetch since the objects DN was outdated
// Fake-cross directory move using two separate
// links to the same directory.
// This other directory is represented by ou=target_otherdir
$ldap2 = $this->connect();
$olddn = $ou_1_l1->currentDN();
$this->assertTrue($ldap->move($ou_1_l1, 'l=movedcrossdir,'.$ou_3->dn(), $ldap2));
$this->assertFalse($ldap->dnExists($olddn));
$this->assertTrue($ldap2->dnExists($ou_1_l1->dn()));
// Try to move over an existing entry
$this->assertType('Net_LDAP2_Error', $ldap->move($ou_2, $ou_3->dn(), $ldap2));
// Try cross directory move without providing an valid entry but a DN
$this->assertType('Net_LDAP2_Error',
$ldap->move($ou_1_l1->dn(), 'l=movedcrossdir2,'.$ou_2->dn(), $ldap2));
// Try passing an invalid entry object
$this->assertType('Net_LDAP2_Error',
$ldap->move($ldap, 'l=move_item,'.$ou_2->dn()));
// Try passing an invalid ldap object
$this->assertType('Net_LDAP2_Error',
$ldap->move($ou_1_l1, 'l=move_item,'.$ou_2->dn(), $ou_1));
// cleanup test tree
$this->assertTrue($ldap->delete($testdn, true), "Could not delete $testdn, please cleanup manually");
}
}
/**
* testCopy().
*/
public function testCopy() {
if (!$this->ldapcfg) {
$this->markTestSkipped('No ldapconfig.ini found. Skipping test!');
} else {
$ldap =& $this->connect();
// some testdata...
$base = $this->ldapcfg['global']['server_base_dn'];
$ou1 = Net_LDAP2_Entry::createFresh('ou=Net_LDAP2_Test_pool,'.$base,
array(
'objectClass' => array('top','organizationalUnit'),
'ou' => 'Net_LDAP2_Test_copy'
));
$ou2 = Net_LDAP2_Entry::createFresh('ou=Net_LDAP2_Test_tgt,'.$base,
array(
'objectClass' => array('top','organizationalUnit'),
'ou' => 'Net_LDAP2_Test_copy'
));
$this->assertTrue($ldap->add($ou1));
$this->assertTrue($ldap->dnExists($ou1->dn()));
$this->assertTrue($ldap->add($ou2));
$this->assertTrue($ldap->dnExists($ou2->dn()));
$entry = Net_LDAP2_Entry::createFresh('l=cptest,'.$ou1->dn(),
array(
'objectClass' => array('top','locality'),
'l' => 'cptest'
));
$this->assertTrue($ldap->add($entry));
$this->assertTrue($ldap->dnExists($entry->dn()));
// copy over the entry to another tree with rename
$entrycp = $ldap->copy($entry, 'l=test_copied,'.$ou2->dn());
$this->assertType('Net_LDAP2_Entry', $entrycp);
$this->assertNotEquals($entry->dn(), $entrycp->dn());
$this->assertTrue($ldap->dnExists($entrycp->dn()));
// copy same again (fails, entry exists)
$entrycp_f = $ldap->copy($entry, 'l=test_copied,'.$ou2->dn());
$this->assertType('Net_LDAP2_Error', $entrycp_f);
// use only DNs to copy (fails)
$entrycp = $ldap->copy($entry->dn(), 'l=test_copied2,'.$ou2->dn());
//cleanup
$this->assertTrue($ldap->delete($ou1, true));
$this->assertTrue($ldap->delete($ou2, true));
}
}
/**
* testIsError().
*/
public function testIsError() {
if (!$this->ldapcfg) {
$this->markTestSkipped('No ldapconfig.ini found. Skipping test!');
} else {
$error = PEAR::raiseError('TestError');
$this->assertTrue(Net_LDAP2::isError($error));
$this->assertFalse(Net_LDAP2::isError('noerror'));
}
}
/**
* checks retrival of RootDSE object
*/
public function testRootDse() {
if (!$this->ldapcfg) {
$this->markTestSkipped('No ldapconfig.ini found. Skipping test!');
} else {
$ldap =& $this->connect();
$this->assertType('Net_LDAP2_RootDSE', $ldap->rootDSE());
}
}
/**
* Checks retrival of schema through LDAP object
*/
public function testSchema() {
if (!$this->ldapcfg) {
$this->markTestSkipped('No ldapconfig.ini found. Skipping test!');
} else {
$ldap =& $this->connect();
$this->assertType('Net_LDAP2_Schema', $ldap->schema());
}
}
/**
* testUtf8Encode()
*/
public function testUtf8Encode() {
if (!$this->ldapcfg) {
$this->markTestSkipped('No ldapconfig.ini found. Skipping test!');
} else {
$ldap =& $this->connect();
$utf8 = array('cn' => 'this needs utf8: ');
$no_utf8 = array('cn' => 'this needs no utf8');
$this->assertNotEquals($utf8, $ldap->utf8Encode($utf8));
$this->assertEquals($no_utf8, $ldap->utf8Encode($no_utf8));
// wrong parameter
$this->assertType('Net_LDAP2_Error', $ldap->utf8Encode('foobar'));
$this->assertType('Net_LDAP2_Error', $ldap->utf8Encode(array('foobar')));
}
}
/**
* testUtf8Decode().
*/
public function testUtf8Decode() {
if (!$this->ldapcfg) {
$this->markTestSkipped('No ldapconfig.ini found. Skipping test!');
} else {
$ldap =& $this->connect();
$entry = $ldap->getEntry($this->ldapcfg['test']['existing_entry'].','.$this->ldapcfg['global']['server_base_dn'],
array($this->ldapcfg['test']['utf8_attr'], $this->ldapcfg['test']['noutf8_attr']));
$this->assertType('Net_LDAP2_Entry', $entry, 'Unable to fetch test entry, check ldapconfig.ini');
$raw_utf8 = array($this->ldapcfg['test']['utf8_attr'] => $entry->getValue($this->ldapcfg['test']['utf8_attr'], 'single'));
$this->assertTrue(is_string($raw_utf8[$this->ldapcfg['test']['utf8_attr']]));
$no_utf8 = array($this->ldapcfg['test']['noutf8_attr'] => $entry->getValue($this->ldapcfg['test']['noutf8_attr'], 'single'));
$this->assertTrue(is_string($no_utf8[$this->ldapcfg['test']['noutf8_attr']]));
$this->assertNotEquals($raw_utf8, $ldap->utf8Decode($raw_utf8));
$this->assertEquals($no_utf8, $ldap->utf8Decode($no_utf8));
// wrong parameter
$this->assertType('Net_LDAP2_Error', $ldap->utf8Decode('foobar'));
$this->assertType('Net_LDAP2_Error', $ldap->utf8Decode(array('foobar')));
}
}
/**
* testGetLink().
*/
public function testGetLink() {
if (!$this->ldapcfg) {
$this->markTestSkipped('No ldapconfig.ini found. Skipping test!');
} else {
$ldap =& $this->connect();
$this->assertTrue(is_resource($ldap->getLink()));
}
}
/**
* Test for bug #18202: "Adding attributes to a Fresh Entry saving and laterly updating fails"
*/
public function testEntryAddIsNotPersistent() {
if (!$this->ldapcfg) {
$this->markTestSkipped('No ldapconfig.ini found. Skipping test!');
} else {
$ldap =& $this->connect();
// setup test entry
$cn = 'Net_LDAP2_Test_bug_18202';
$dn = 'cn='.$cn.','.$this->ldapcfg['global']['server_base_dn'];
$data = array(
'objectClass' => array('top', 'inetOrgPerson'),
'givenName' => 'bug 18202',
'sn' => 'testentry',
'cn' => $cn
);
// Test case
$entry = Net_LDAP2_Entry::createFresh($dn, $data);
$this->assertType('Net_LDAP2_Entry', $entry);
$this->assertTrue( $entry->add(array('uid' => 'Fu Bar')) );
$this->assertTrue( $ldap->add($entry) );
$this->assertTrue( $entry->replace(array('uid' => 'Foo Bar')) );
$this->assertTrue( $result = $entry->update() );
// cleanup
$this->assertTrue($ldap->delete($entry),
'Cleanup of test entry failed. Please remove manually: '.$entry->dn());
}
}
}
// Call Net_LDAP2Test::main() if this source file is executed directly.
if (PHPUnit_MAIN_METHOD == "Net_LDAP2Test::main") {
Net_LDAP2Test::main();
}
?>
|