| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- <?php
- /**
- * Zend Framework
- *
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://framework.zend.com/license/new-bsd
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@zend.com so we can send you a copy immediately.
- *
- * @category Zend
- * @package Zend_Ldap
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @version $Id$
- */
- /**
- * Zend_Ldap_OnlineTestCase
- */
- require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'OnlineTestCase.php';
- /**
- * @see Zend_Ldap_Dn
- */
- require_once 'Zend/Ldap/Dn.php';
- /**
- * @see Zend_Ldap_Filter
- */
- require_once 'Zend/Ldap/Filter.php';
- /**
- * @category Zend
- * @package Zend_Ldap
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @group Zend_Ldap
- */
- class Zend_Ldap_SearchTest extends Zend_Ldap_OnlineTestCase
- {
- protected function setUp()
- {
- parent::setUp();
- $this->_prepareLdapServer();
- }
- protected function tearDown()
- {
- $this->_cleanupLdapServer();
- parent::tearDown();
- }
- public function testGetSingleEntry()
- {
- $dn=$this->_createDn('ou=Test1,');
- $entry=$this->_getLdap()->getEntry($dn);
- $this->assertEquals($dn, $entry["dn"]);
- $this->assertArrayHasKey('ou', $entry);
- $this->assertContains('Test1', $entry['ou']);
- $this->assertEquals(1, count($entry['ou']));
- }
- public function testGetSingleIllegalEntry()
- {
- $dn=$this->_createDn('ou=Test99,');
- $entry=$this->_getLdap()->getEntry($dn);
- $this->assertNull($entry);
- }
- /**
- * @expectedException Zend_Ldap_Exception
- */
- public function testGetSingleIllegalEntryWithException()
- {
- $dn=$this->_createDn('ou=Test99,');
- $entry=$this->_getLdap()->getEntry($dn, array(), true);
- }
- public function testCountBase()
- {
- $dn=$this->_createDn('ou=Node,');
- $count=$this->_getLdap()->count('(objectClass=*)', $dn, Zend_Ldap::SEARCH_SCOPE_BASE);
- $this->assertEquals(1, $count);
- }
- public function testCountOne()
- {
- $dn1=$this->_createDn('ou=Node,');
- $count1=$this->_getLdap()->count('(objectClass=*)', $dn1, Zend_Ldap::SEARCH_SCOPE_ONE);
- $this->assertEquals(2, $count1);
- $dn2=TESTS_ZEND_LDAP_WRITEABLE_SUBTREE;
- $count2=$this->_getLdap()->count('(objectClass=*)', $dn2, Zend_Ldap::SEARCH_SCOPE_ONE);
- $this->assertEquals(6, $count2);
- }
- public function testCountSub()
- {
- $dn1=$this->_createDn('ou=Node,');
- $count1=$this->_getLdap()->count('(objectClass=*)', $dn1, Zend_Ldap::SEARCH_SCOPE_SUB);
- $this->assertEquals(3, $count1);
- $dn2=TESTS_ZEND_LDAP_WRITEABLE_SUBTREE;
- $count2=$this->_getLdap()->count('(objectClass=*)', $dn2, Zend_Ldap::SEARCH_SCOPE_SUB);
- $this->assertEquals(9, $count2);
- }
- public function testResultIteration()
- {
- $items=$this->_getLdap()->search('(objectClass=organizationalUnit)',
- TESTS_ZEND_LDAP_WRITEABLE_SUBTREE, Zend_Ldap::SEARCH_SCOPE_SUB);
- $this->assertEquals(9, $items->count());
- $i=0;
- foreach ($items as $key => $item)
- {
- $this->assertEquals($i, $key);
- $i++;
- }
- $this->assertEquals(9, $i);
- $j=0;
- foreach ($items as $item) { $j++; }
- $this->assertEquals($i, $j);
- }
- public function testSearchNoResult()
- {
- $items=$this->_getLdap()->search('(objectClass=account)', TESTS_ZEND_LDAP_WRITEABLE_SUBTREE,
- Zend_Ldap::SEARCH_SCOPE_SUB);
- $this->assertEquals(0, $items->count());
- }
- public function testSearchEntriesShortcut()
- {
- $entries=$this->_getLdap()->searchEntries('(objectClass=organizationalUnit)',
- TESTS_ZEND_LDAP_WRITEABLE_SUBTREE, Zend_Ldap::SEARCH_SCOPE_SUB);
- $this->assertType("array", $entries);
- $this->assertEquals(9, count($entries));
- }
- /**
- * @expectedException Zend_Ldap_Exception
- */
- public function testIllegalSearch()
- {
- $dn=$this->_createDn('ou=Node2,');
- $items=$this->_getLdap()->search('(objectClass=account)', $dn, Zend_Ldap::SEARCH_SCOPE_SUB);
- }
- public function testSearchNothingGetFirst()
- {
- $entries=$this->_getLdap()->search('(objectClass=account)', TESTS_ZEND_LDAP_WRITEABLE_SUBTREE,
- Zend_Ldap::SEARCH_SCOPE_SUB);
- $this->assertEquals(0, $entries->count());
- $this->assertNull($entries->getFirst());
- }
- public function testSorting()
- {
- $lSorted=array('a', 'b', 'c', 'd', 'e');
- $items=$this->_getLdap()->search('(l=*)', TESTS_ZEND_LDAP_WRITEABLE_SUBTREE,
- Zend_Ldap::SEARCH_SCOPE_SUB, array(), 'l');
- $this->assertEquals(5, $items->count());
- foreach ($items as $key => $item)
- {
- $this->assertEquals($lSorted[$key], $item['l'][0]);
- }
- }
- public function testCountChildren()
- {
- $dn1=$this->_createDn('ou=Node,');
- $count1=$this->_getLdap()->countChildren($dn1);
- $this->assertEquals(2, $count1);
- $dn2=TESTS_ZEND_LDAP_WRITEABLE_SUBTREE;
- $count2=$this->_getLdap()->countChildren($dn2);
- $this->assertEquals(6, $count2);
- }
- public function testExistsDn()
- {
- $dn1=$this->_createDn('ou=Test2,');
- $dn2=$this->_createDn('ou=Test99,');
- $this->assertTrue($this->_getLdap()->exists($dn1));
- $this->assertFalse($this->_getLdap()->exists($dn2));
- }
- public function testSearchWithDnObjectAndFilterObject()
- {
- $dn=Zend_Ldap_Dn::fromString(TESTS_ZEND_LDAP_WRITEABLE_SUBTREE);
- $filter=Zend_Ldap_Filter::equals('objectClass', 'organizationalUnit');
- $items=$this->_getLdap()->search($filter, $dn, Zend_Ldap::SEARCH_SCOPE_SUB);
- $this->assertEquals(9, $items->count());
- }
- public function testCountSubWithDnObjectAndFilterObject()
- {
- $dn1=Zend_Ldap_Dn::fromString($this->_createDn('ou=Node,'));
- $filter=Zend_Ldap_Filter::any('objectClass');
- $count1=$this->_getLdap()->count($filter, $dn1, Zend_Ldap::SEARCH_SCOPE_SUB);
- $this->assertEquals(3, $count1);
- $dn2=Zend_Ldap_Dn::fromString(TESTS_ZEND_LDAP_WRITEABLE_SUBTREE);
- $count2=$this->_getLdap()->count($filter, $dn2, Zend_Ldap::SEARCH_SCOPE_SUB);
- $this->assertEquals(9, $count2);
- }
- public function testCountChildrenWithDnObject()
- {
- $dn1=Zend_Ldap_Dn::fromString($this->_createDn('ou=Node,'));
- $count1=$this->_getLdap()->countChildren($dn1);
- $this->assertEquals(2, $count1);
- $dn2=Zend_Ldap_Dn::fromString(TESTS_ZEND_LDAP_WRITEABLE_SUBTREE);
- $count2=$this->_getLdap()->countChildren($dn2);
- $this->assertEquals(6, $count2);
- }
- public function testExistsDnWithDnObject()
- {
- $dn1=Zend_Ldap_Dn::fromString($this->_createDn('ou=Test2,'));
- $dn2=Zend_Ldap_Dn::fromString($this->_createDn('ou=Test99,'));
- $this->assertTrue($this->_getLdap()->exists($dn1));
- $this->assertFalse($this->_getLdap()->exists($dn2));
- }
- public function testSearchEntriesShortcutWithDnObjectAndFilterObject()
- {
- $dn=Zend_Ldap_Dn::fromString(TESTS_ZEND_LDAP_WRITEABLE_SUBTREE);
- $filter=Zend_Ldap_Filter::equals('objectClass', 'organizationalUnit');
- $entries=$this->_getLdap()->searchEntries($filter, $dn, Zend_Ldap::SEARCH_SCOPE_SUB);
- $this->assertType("array", $entries);
- $this->assertEquals(9, count($entries));
- }
- public function testGetSingleEntryWithDnObject()
- {
- $dn=Zend_Ldap_Dn::fromString($this->_createDn('ou=Test1,'));
- $entry=$this->_getLdap()->getEntry($dn);
- $this->assertEquals($dn->toString(), $entry["dn"]);
- }
- public function testMultipleResultIteration()
- {
- $items=$this->_getLdap()->search('(objectClass=organizationalUnit)',
- TESTS_ZEND_LDAP_WRITEABLE_SUBTREE, Zend_Ldap::SEARCH_SCOPE_SUB);
- $isCount = 9;
- $this->assertEquals($isCount, $items->count());
- $i=0;
- foreach ($items as $key => $item)
- {
- $this->assertEquals($i, $key);
- $i++;
- }
- $this->assertEquals($isCount, $i);
- $i=0;
- foreach ($items as $key => $item)
- {
- $this->assertEquals($i, $key);
- $i++;
- }
- $this->assertEquals($isCount, $i);
- $items->close();
- $i=0;
- foreach ($items as $key => $item)
- {
- $this->assertEquals($i, $key);
- $i++;
- }
- $this->assertEquals($isCount, $i);
- $i=0;
- foreach ($items as $key => $item)
- {
- $this->assertEquals($i, $key);
- $i++;
- }
- $this->assertEquals($isCount, $i);
- }
- /**
- * Test issue reported by Lance Hendrix on
- * http://framework.zend.com/wiki/display/ZFPROP/Zend_Ldap+-+Extended+support+-+Stefan+Gehrig?
- * focusedCommentId=13107431#comment-13107431
- */
- public function testCallingNextAfterIterationShouldNotThrowException()
- {
- $items = $this->_getLdap()->search('(objectClass=organizationalUnit)',
- TESTS_ZEND_LDAP_WRITEABLE_SUBTREE, Zend_Ldap::SEARCH_SCOPE_SUB);
- foreach ($items as $key => $item) {
- // do nothing - just iterate
- }
- $items->next();
- }
- public function testUnknownCollectionClassThrowsException()
- {
- try {
- $items=$this->_getLdap()->search('(objectClass=organizationalUnit)',
- TESTS_ZEND_LDAP_WRITEABLE_SUBTREE, Zend_Ldap::SEARCH_SCOPE_SUB, array(), null,
- 'This_Class_Does_Not_Exist');
- $this->fail('Expected exception not thrown');
- } catch (Zend_Ldap_Exception $zle) {
- $this->assertContains("Class 'This_Class_Does_Not_Exist' can not be found",
- $zle->getMessage());
- }
- }
- public function testCollectionClassNotSubclassingZendLdapCollectionThrowsException()
- {
- try {
- $items=$this->_getLdap()->search('(objectClass=organizationalUnit)',
- TESTS_ZEND_LDAP_WRITEABLE_SUBTREE, Zend_Ldap::SEARCH_SCOPE_SUB, array(), null,
- 'Zend_Ldap_SearchTest_CollectionClassNotSubclassingZendLdapCollection');
- $this->fail('Expected exception not thrown');
- } catch (Zend_Ldap_Exception $zle) {
- $this->assertContains(
- "Class 'Zend_Ldap_SearchTest_CollectionClassNotSubclassingZendLdapCollection' must subclass 'Zend_Ldap_Collection'",
- $zle->getMessage());
- }
- }
- }
- class Zend_Ldap_SearchTest_CollectionClassNotSubclassingZendLdapCollection
- { }
|