SearchTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Ldap
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * Zend_Ldap_OnlineTestCase
  24. */
  25. require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'OnlineTestCase.php';
  26. /**
  27. * @see Zend_Ldap_Dn
  28. */
  29. require_once 'Zend/Ldap/Dn.php';
  30. /**
  31. * @see Zend_Ldap_Filter
  32. */
  33. require_once 'Zend/Ldap/Filter.php';
  34. /**
  35. * @category Zend
  36. * @package Zend_Ldap
  37. * @subpackage UnitTests
  38. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. * @group Zend_Ldap
  41. */
  42. class Zend_Ldap_SearchTest extends Zend_Ldap_OnlineTestCase
  43. {
  44. protected function setUp()
  45. {
  46. parent::setUp();
  47. $this->_prepareLdapServer();
  48. }
  49. protected function tearDown()
  50. {
  51. $this->_cleanupLdapServer();
  52. parent::tearDown();
  53. }
  54. public function testGetSingleEntry()
  55. {
  56. $dn=$this->_createDn('ou=Test1,');
  57. $entry=$this->_getLdap()->getEntry($dn);
  58. $this->assertEquals($dn, $entry["dn"]);
  59. $this->assertArrayHasKey('ou', $entry);
  60. $this->assertContains('Test1', $entry['ou']);
  61. $this->assertEquals(1, count($entry['ou']));
  62. }
  63. public function testGetSingleIllegalEntry()
  64. {
  65. $dn=$this->_createDn('ou=Test99,');
  66. $entry=$this->_getLdap()->getEntry($dn);
  67. $this->assertNull($entry);
  68. }
  69. /**
  70. * @expectedException Zend_Ldap_Exception
  71. */
  72. public function testGetSingleIllegalEntryWithException()
  73. {
  74. $dn=$this->_createDn('ou=Test99,');
  75. $entry=$this->_getLdap()->getEntry($dn, array(), true);
  76. }
  77. public function testCountBase()
  78. {
  79. $dn=$this->_createDn('ou=Node,');
  80. $count=$this->_getLdap()->count('(objectClass=*)', $dn, Zend_Ldap::SEARCH_SCOPE_BASE);
  81. $this->assertEquals(1, $count);
  82. }
  83. public function testCountOne()
  84. {
  85. $dn1=$this->_createDn('ou=Node,');
  86. $count1=$this->_getLdap()->count('(objectClass=*)', $dn1, Zend_Ldap::SEARCH_SCOPE_ONE);
  87. $this->assertEquals(2, $count1);
  88. $dn2=TESTS_ZEND_LDAP_WRITEABLE_SUBTREE;
  89. $count2=$this->_getLdap()->count('(objectClass=*)', $dn2, Zend_Ldap::SEARCH_SCOPE_ONE);
  90. $this->assertEquals(6, $count2);
  91. }
  92. public function testCountSub()
  93. {
  94. $dn1=$this->_createDn('ou=Node,');
  95. $count1=$this->_getLdap()->count('(objectClass=*)', $dn1, Zend_Ldap::SEARCH_SCOPE_SUB);
  96. $this->assertEquals(3, $count1);
  97. $dn2=TESTS_ZEND_LDAP_WRITEABLE_SUBTREE;
  98. $count2=$this->_getLdap()->count('(objectClass=*)', $dn2, Zend_Ldap::SEARCH_SCOPE_SUB);
  99. $this->assertEquals(9, $count2);
  100. }
  101. public function testResultIteration()
  102. {
  103. $items=$this->_getLdap()->search('(objectClass=organizationalUnit)',
  104. TESTS_ZEND_LDAP_WRITEABLE_SUBTREE, Zend_Ldap::SEARCH_SCOPE_SUB);
  105. $this->assertEquals(9, $items->count());
  106. $i=0;
  107. foreach ($items as $key => $item)
  108. {
  109. $this->assertEquals($i, $key);
  110. $i++;
  111. }
  112. $this->assertEquals(9, $i);
  113. $j=0;
  114. foreach ($items as $item) { $j++; }
  115. $this->assertEquals($i, $j);
  116. }
  117. public function testSearchNoResult()
  118. {
  119. $items=$this->_getLdap()->search('(objectClass=account)', TESTS_ZEND_LDAP_WRITEABLE_SUBTREE,
  120. Zend_Ldap::SEARCH_SCOPE_SUB);
  121. $this->assertEquals(0, $items->count());
  122. }
  123. public function testSearchEntriesShortcut()
  124. {
  125. $entries=$this->_getLdap()->searchEntries('(objectClass=organizationalUnit)',
  126. TESTS_ZEND_LDAP_WRITEABLE_SUBTREE, Zend_Ldap::SEARCH_SCOPE_SUB);
  127. $this->assertType("array", $entries);
  128. $this->assertEquals(9, count($entries));
  129. }
  130. /**
  131. * @expectedException Zend_Ldap_Exception
  132. */
  133. public function testIllegalSearch()
  134. {
  135. $dn=$this->_createDn('ou=Node2,');
  136. $items=$this->_getLdap()->search('(objectClass=account)', $dn, Zend_Ldap::SEARCH_SCOPE_SUB);
  137. }
  138. public function testSearchNothingGetFirst()
  139. {
  140. $entries=$this->_getLdap()->search('(objectClass=account)', TESTS_ZEND_LDAP_WRITEABLE_SUBTREE,
  141. Zend_Ldap::SEARCH_SCOPE_SUB);
  142. $this->assertEquals(0, $entries->count());
  143. $this->assertNull($entries->getFirst());
  144. }
  145. public function testSorting()
  146. {
  147. $lSorted=array('a', 'b', 'c', 'd', 'e');
  148. $items=$this->_getLdap()->search('(l=*)', TESTS_ZEND_LDAP_WRITEABLE_SUBTREE,
  149. Zend_Ldap::SEARCH_SCOPE_SUB, array(), 'l');
  150. $this->assertEquals(5, $items->count());
  151. foreach ($items as $key => $item)
  152. {
  153. $this->assertEquals($lSorted[$key], $item['l'][0]);
  154. }
  155. }
  156. public function testCountChildren()
  157. {
  158. $dn1=$this->_createDn('ou=Node,');
  159. $count1=$this->_getLdap()->countChildren($dn1);
  160. $this->assertEquals(2, $count1);
  161. $dn2=TESTS_ZEND_LDAP_WRITEABLE_SUBTREE;
  162. $count2=$this->_getLdap()->countChildren($dn2);
  163. $this->assertEquals(6, $count2);
  164. }
  165. public function testExistsDn()
  166. {
  167. $dn1=$this->_createDn('ou=Test2,');
  168. $dn2=$this->_createDn('ou=Test99,');
  169. $this->assertTrue($this->_getLdap()->exists($dn1));
  170. $this->assertFalse($this->_getLdap()->exists($dn2));
  171. }
  172. public function testSearchWithDnObjectAndFilterObject()
  173. {
  174. $dn=Zend_Ldap_Dn::fromString(TESTS_ZEND_LDAP_WRITEABLE_SUBTREE);
  175. $filter=Zend_Ldap_Filter::equals('objectClass', 'organizationalUnit');
  176. $items=$this->_getLdap()->search($filter, $dn, Zend_Ldap::SEARCH_SCOPE_SUB);
  177. $this->assertEquals(9, $items->count());
  178. }
  179. public function testCountSubWithDnObjectAndFilterObject()
  180. {
  181. $dn1=Zend_Ldap_Dn::fromString($this->_createDn('ou=Node,'));
  182. $filter=Zend_Ldap_Filter::any('objectClass');
  183. $count1=$this->_getLdap()->count($filter, $dn1, Zend_Ldap::SEARCH_SCOPE_SUB);
  184. $this->assertEquals(3, $count1);
  185. $dn2=Zend_Ldap_Dn::fromString(TESTS_ZEND_LDAP_WRITEABLE_SUBTREE);
  186. $count2=$this->_getLdap()->count($filter, $dn2, Zend_Ldap::SEARCH_SCOPE_SUB);
  187. $this->assertEquals(9, $count2);
  188. }
  189. public function testCountChildrenWithDnObject()
  190. {
  191. $dn1=Zend_Ldap_Dn::fromString($this->_createDn('ou=Node,'));
  192. $count1=$this->_getLdap()->countChildren($dn1);
  193. $this->assertEquals(2, $count1);
  194. $dn2=Zend_Ldap_Dn::fromString(TESTS_ZEND_LDAP_WRITEABLE_SUBTREE);
  195. $count2=$this->_getLdap()->countChildren($dn2);
  196. $this->assertEquals(6, $count2);
  197. }
  198. public function testExistsDnWithDnObject()
  199. {
  200. $dn1=Zend_Ldap_Dn::fromString($this->_createDn('ou=Test2,'));
  201. $dn2=Zend_Ldap_Dn::fromString($this->_createDn('ou=Test99,'));
  202. $this->assertTrue($this->_getLdap()->exists($dn1));
  203. $this->assertFalse($this->_getLdap()->exists($dn2));
  204. }
  205. public function testSearchEntriesShortcutWithDnObjectAndFilterObject()
  206. {
  207. $dn=Zend_Ldap_Dn::fromString(TESTS_ZEND_LDAP_WRITEABLE_SUBTREE);
  208. $filter=Zend_Ldap_Filter::equals('objectClass', 'organizationalUnit');
  209. $entries=$this->_getLdap()->searchEntries($filter, $dn, Zend_Ldap::SEARCH_SCOPE_SUB);
  210. $this->assertType("array", $entries);
  211. $this->assertEquals(9, count($entries));
  212. }
  213. public function testGetSingleEntryWithDnObject()
  214. {
  215. $dn=Zend_Ldap_Dn::fromString($this->_createDn('ou=Test1,'));
  216. $entry=$this->_getLdap()->getEntry($dn);
  217. $this->assertEquals($dn->toString(), $entry["dn"]);
  218. }
  219. public function testMultipleResultIteration()
  220. {
  221. $items=$this->_getLdap()->search('(objectClass=organizationalUnit)',
  222. TESTS_ZEND_LDAP_WRITEABLE_SUBTREE, Zend_Ldap::SEARCH_SCOPE_SUB);
  223. $isCount = 9;
  224. $this->assertEquals($isCount, $items->count());
  225. $i=0;
  226. foreach ($items as $key => $item)
  227. {
  228. $this->assertEquals($i, $key);
  229. $i++;
  230. }
  231. $this->assertEquals($isCount, $i);
  232. $i=0;
  233. foreach ($items as $key => $item)
  234. {
  235. $this->assertEquals($i, $key);
  236. $i++;
  237. }
  238. $this->assertEquals($isCount, $i);
  239. $items->close();
  240. $i=0;
  241. foreach ($items as $key => $item)
  242. {
  243. $this->assertEquals($i, $key);
  244. $i++;
  245. }
  246. $this->assertEquals($isCount, $i);
  247. $i=0;
  248. foreach ($items as $key => $item)
  249. {
  250. $this->assertEquals($i, $key);
  251. $i++;
  252. }
  253. $this->assertEquals($isCount, $i);
  254. }
  255. /**
  256. * Test issue reported by Lance Hendrix on
  257. * http://framework.zend.com/wiki/display/ZFPROP/Zend_Ldap+-+Extended+support+-+Stefan+Gehrig?
  258. * focusedCommentId=13107431#comment-13107431
  259. */
  260. public function testCallingNextAfterIterationShouldNotThrowException()
  261. {
  262. $items = $this->_getLdap()->search('(objectClass=organizationalUnit)',
  263. TESTS_ZEND_LDAP_WRITEABLE_SUBTREE, Zend_Ldap::SEARCH_SCOPE_SUB);
  264. foreach ($items as $key => $item) {
  265. // do nothing - just iterate
  266. }
  267. $items->next();
  268. }
  269. public function testUnknownCollectionClassThrowsException()
  270. {
  271. try {
  272. $items=$this->_getLdap()->search('(objectClass=organizationalUnit)',
  273. TESTS_ZEND_LDAP_WRITEABLE_SUBTREE, Zend_Ldap::SEARCH_SCOPE_SUB, array(), null,
  274. 'This_Class_Does_Not_Exist');
  275. $this->fail('Expected exception not thrown');
  276. } catch (Zend_Ldap_Exception $zle) {
  277. $this->assertContains("Class 'This_Class_Does_Not_Exist' can not be found",
  278. $zle->getMessage());
  279. }
  280. }
  281. public function testCollectionClassNotSubclassingZendLdapCollectionThrowsException()
  282. {
  283. try {
  284. $items=$this->_getLdap()->search('(objectClass=organizationalUnit)',
  285. TESTS_ZEND_LDAP_WRITEABLE_SUBTREE, Zend_Ldap::SEARCH_SCOPE_SUB, array(), null,
  286. 'Zend_Ldap_SearchTest_CollectionClassNotSubclassingZendLdapCollection');
  287. $this->fail('Expected exception not thrown');
  288. } catch (Zend_Ldap_Exception $zle) {
  289. $this->assertContains(
  290. "Class 'Zend_Ldap_SearchTest_CollectionClassNotSubclassingZendLdapCollection' must subclass 'Zend_Ldap_Collection'",
  291. $zle->getMessage());
  292. }
  293. }
  294. }
  295. class Zend_Ldap_SearchTest_CollectionClassNotSubclassingZendLdapCollection
  296. { }