SearchTest.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * Zend_Ldap_OnlineTestCase
  23. */
  24. require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'OnlineTestCase.php';
  25. /**
  26. * @see Zend_Ldap_Dn
  27. */
  28. require_once 'Zend/Ldap/Dn.php';
  29. /**
  30. * @see Zend_Ldap_Filter
  31. */
  32. require_once 'Zend/Ldap/Filter.php';
  33. /**
  34. * @category Zend
  35. * @package Zend_Ldap
  36. * @subpackage UnitTests
  37. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  38. * @license http://framework.zend.com/license/new-bsd New BSD License
  39. */
  40. class Zend_Ldap_SearchTest extends Zend_Ldap_OnlineTestCase
  41. {
  42. protected function setUp()
  43. {
  44. parent::setUp();
  45. $this->_prepareLdapServer();
  46. }
  47. protected function tearDown()
  48. {
  49. $this->_cleanupLdapServer();
  50. parent::tearDown();
  51. }
  52. public function testGetSingleEntry()
  53. {
  54. $dn=$this->_createDn('ou=Test1,');
  55. $entry=$this->_getLdap()->getEntry($dn);
  56. $this->assertEquals($dn, $entry["dn"]);
  57. $this->assertArrayHasKey('ou', $entry);
  58. $this->assertContains('Test1', $entry['ou']);
  59. $this->assertEquals(1, count($entry['ou']));
  60. }
  61. public function testGetSingleIllegalEntry()
  62. {
  63. $dn=$this->_createDn('ou=Test99,');
  64. $entry=$this->_getLdap()->getEntry($dn);
  65. $this->assertNull($entry);
  66. }
  67. /**
  68. * @expectedException Zend_Ldap_Exception
  69. */
  70. public function testGetSingleIllegalEntryWithException()
  71. {
  72. $dn=$this->_createDn('ou=Test99,');
  73. $entry=$this->_getLdap()->getEntry($dn, array(), true);
  74. }
  75. public function testCountBase()
  76. {
  77. $dn=$this->_createDn('ou=Node,');
  78. $count=$this->_getLdap()->count('(objectClass=*)', $dn, Zend_Ldap::SEARCH_SCOPE_BASE);
  79. $this->assertEquals(1, $count);
  80. }
  81. public function testCountOne()
  82. {
  83. $dn1=$this->_createDn('ou=Node,');
  84. $count1=$this->_getLdap()->count('(objectClass=*)', $dn1, Zend_Ldap::SEARCH_SCOPE_ONE);
  85. $this->assertEquals(2, $count1);
  86. $dn2=TESTS_ZEND_LDAP_WRITEABLE_SUBTREE;
  87. $count2=$this->_getLdap()->count('(objectClass=*)', $dn2, Zend_Ldap::SEARCH_SCOPE_ONE);
  88. $this->assertEquals(6, $count2);
  89. }
  90. public function testCountSub()
  91. {
  92. $dn1=$this->_createDn('ou=Node,');
  93. $count1=$this->_getLdap()->count('(objectClass=*)', $dn1, Zend_Ldap::SEARCH_SCOPE_SUB);
  94. $this->assertEquals(3, $count1);
  95. $dn2=TESTS_ZEND_LDAP_WRITEABLE_SUBTREE;
  96. $count2=$this->_getLdap()->count('(objectClass=*)', $dn2, Zend_Ldap::SEARCH_SCOPE_SUB);
  97. $this->assertEquals(9, $count2);
  98. }
  99. public function testResultIteration()
  100. {
  101. $items=$this->_getLdap()->search('(objectClass=organizationalUnit)',
  102. TESTS_ZEND_LDAP_WRITEABLE_SUBTREE, Zend_Ldap::SEARCH_SCOPE_SUB);
  103. $this->assertEquals(9, $items->count());
  104. $i=0;
  105. foreach ($items as $key => $item)
  106. {
  107. $this->assertEquals($i, $key);
  108. $i++;
  109. }
  110. $this->assertEquals(9, $i);
  111. $j=0;
  112. foreach ($items as $item) { $j++; }
  113. $this->assertEquals($i, $j);
  114. }
  115. public function testSearchNoResult()
  116. {
  117. $items=$this->_getLdap()->search('(objectClass=account)', TESTS_ZEND_LDAP_WRITEABLE_SUBTREE,
  118. Zend_Ldap::SEARCH_SCOPE_SUB);
  119. $this->assertEquals(0, $items->count());
  120. }
  121. public function testSearchEntriesShortcut()
  122. {
  123. $entries=$this->_getLdap()->searchEntries('(objectClass=organizationalUnit)',
  124. TESTS_ZEND_LDAP_WRITEABLE_SUBTREE, Zend_Ldap::SEARCH_SCOPE_SUB);
  125. $this->assertType("array", $entries);
  126. $this->assertEquals(9, count($entries));
  127. }
  128. /**
  129. * @expectedException Zend_Ldap_Exception
  130. */
  131. public function testIllegalSearch()
  132. {
  133. $dn=$this->_createDn('ou=Node2,');
  134. $items=$this->_getLdap()->search('(objectClass=account)', $dn, Zend_Ldap::SEARCH_SCOPE_SUB);
  135. }
  136. public function testSearchNothingGetFirst()
  137. {
  138. $entries=$this->_getLdap()->search('(objectClass=account)', TESTS_ZEND_LDAP_WRITEABLE_SUBTREE,
  139. Zend_Ldap::SEARCH_SCOPE_SUB);
  140. $this->assertEquals(0, $entries->count());
  141. $this->assertNull($entries->getFirst());
  142. }
  143. public function testSorting()
  144. {
  145. $lSorted=array('a', 'b', 'c', 'd', 'e');
  146. $items=$this->_getLdap()->search('(l=*)', TESTS_ZEND_LDAP_WRITEABLE_SUBTREE,
  147. Zend_Ldap::SEARCH_SCOPE_SUB, array(), 'l');
  148. $this->assertEquals(5, $items->count());
  149. foreach ($items as $key => $item)
  150. {
  151. $this->assertEquals($lSorted[$key], $item['l'][0]);
  152. }
  153. }
  154. public function testCountChildren()
  155. {
  156. $dn1=$this->_createDn('ou=Node,');
  157. $count1=$this->_getLdap()->countChildren($dn1);
  158. $this->assertEquals(2, $count1);
  159. $dn2=TESTS_ZEND_LDAP_WRITEABLE_SUBTREE;
  160. $count2=$this->_getLdap()->countChildren($dn2);
  161. $this->assertEquals(6, $count2);
  162. }
  163. public function testExistsDn()
  164. {
  165. $dn1=$this->_createDn('ou=Test2,');
  166. $dn2=$this->_createDn('ou=Test99,');
  167. $this->assertTrue($this->_getLdap()->exists($dn1));
  168. $this->assertFalse($this->_getLdap()->exists($dn2));
  169. }
  170. public function testSearchWithDnObjectAndFilterObject()
  171. {
  172. $dn=Zend_Ldap_Dn::fromString(TESTS_ZEND_LDAP_WRITEABLE_SUBTREE);
  173. $filter=Zend_Ldap_Filter::equals('objectClass', 'organizationalUnit');
  174. $items=$this->_getLdap()->search($filter, $dn, Zend_Ldap::SEARCH_SCOPE_SUB);
  175. $this->assertEquals(9, $items->count());
  176. }
  177. public function testCountSubWithDnObjectAndFilterObject()
  178. {
  179. $dn1=Zend_Ldap_Dn::fromString($this->_createDn('ou=Node,'));
  180. $filter=Zend_Ldap_Filter::any('objectClass');
  181. $count1=$this->_getLdap()->count($filter, $dn1, Zend_Ldap::SEARCH_SCOPE_SUB);
  182. $this->assertEquals(3, $count1);
  183. $dn2=Zend_Ldap_Dn::fromString(TESTS_ZEND_LDAP_WRITEABLE_SUBTREE);
  184. $count2=$this->_getLdap()->count($filter, $dn2, Zend_Ldap::SEARCH_SCOPE_SUB);
  185. $this->assertEquals(9, $count2);
  186. }
  187. public function testCountChildrenWithDnObject()
  188. {
  189. $dn1=Zend_Ldap_Dn::fromString($this->_createDn('ou=Node,'));
  190. $count1=$this->_getLdap()->countChildren($dn1);
  191. $this->assertEquals(2, $count1);
  192. $dn2=Zend_Ldap_Dn::fromString(TESTS_ZEND_LDAP_WRITEABLE_SUBTREE);
  193. $count2=$this->_getLdap()->countChildren($dn2);
  194. $this->assertEquals(6, $count2);
  195. }
  196. public function testExistsDnWithDnObject()
  197. {
  198. $dn1=Zend_Ldap_Dn::fromString($this->_createDn('ou=Test2,'));
  199. $dn2=Zend_Ldap_Dn::fromString($this->_createDn('ou=Test99,'));
  200. $this->assertTrue($this->_getLdap()->exists($dn1));
  201. $this->assertFalse($this->_getLdap()->exists($dn2));
  202. }
  203. public function testSearchEntriesShortcutWithDnObjectAndFilterObject()
  204. {
  205. $dn=Zend_Ldap_Dn::fromString(TESTS_ZEND_LDAP_WRITEABLE_SUBTREE);
  206. $filter=Zend_Ldap_Filter::equals('objectClass', 'organizationalUnit');
  207. $entries=$this->_getLdap()->searchEntries($filter, $dn, Zend_Ldap::SEARCH_SCOPE_SUB);
  208. $this->assertType("array", $entries);
  209. $this->assertEquals(9, count($entries));
  210. }
  211. public function testGetSingleEntryWithDnObject()
  212. {
  213. $dn=Zend_Ldap_Dn::fromString($this->_createDn('ou=Test1,'));
  214. $entry=$this->_getLdap()->getEntry($dn);
  215. $this->assertEquals($dn->toString(), $entry["dn"]);
  216. }
  217. public function testMultipleResultIteration()
  218. {
  219. $items=$this->_getLdap()->search('(objectClass=organizationalUnit)',
  220. TESTS_ZEND_LDAP_WRITEABLE_SUBTREE, Zend_Ldap::SEARCH_SCOPE_SUB);
  221. $isCount = 9;
  222. $this->assertEquals($isCount, $items->count());
  223. $i=0;
  224. foreach ($items as $key => $item)
  225. {
  226. $this->assertEquals($i, $key);
  227. $i++;
  228. }
  229. $this->assertEquals($isCount, $i);
  230. $i=0;
  231. foreach ($items as $key => $item)
  232. {
  233. $this->assertEquals($i, $key);
  234. $i++;
  235. }
  236. $this->assertEquals($isCount, $i);
  237. $items->close();
  238. $i=0;
  239. foreach ($items as $key => $item)
  240. {
  241. $this->assertEquals($i, $key);
  242. $i++;
  243. }
  244. $this->assertEquals($isCount, $i);
  245. $i=0;
  246. foreach ($items as $key => $item)
  247. {
  248. $this->assertEquals($i, $key);
  249. $i++;
  250. }
  251. $this->assertEquals($isCount, $i);
  252. }
  253. /**
  254. * Test issue reported by Lance Hendrix on
  255. * http://framework.zend.com/wiki/display/ZFPROP/Zend_Ldap+-+Extended+support+-+Stefan+Gehrig?
  256. * focusedCommentId=13107431#comment-13107431
  257. */
  258. public function testCallingNextAfterIterationShouldNotThrowException()
  259. {
  260. $items = $this->_getLdap()->search('(objectClass=organizationalUnit)',
  261. TESTS_ZEND_LDAP_WRITEABLE_SUBTREE, Zend_Ldap::SEARCH_SCOPE_SUB);
  262. foreach ($items as $key => $item) {
  263. // do nothing - just iterate
  264. }
  265. $items->next();
  266. }
  267. }