CopyRenameTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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-2015 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. * @category Zend
  32. * @package Zend_Ldap
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Ldap
  37. */
  38. class Zend_Ldap_CopyRenameTest extends Zend_Ldap_OnlineTestCase
  39. {
  40. /**
  41. * @var string
  42. */
  43. private $_orgDn;
  44. /**
  45. * @var string
  46. */
  47. private $_newDn;
  48. /**
  49. * @var string
  50. */
  51. private $_orgSubTreeDn;
  52. /**
  53. * @var string
  54. */
  55. private $_newSubTreeDn;
  56. /**
  57. * @var string
  58. */
  59. private $_targetSubTreeDn;
  60. /**
  61. * @var array
  62. */
  63. private $_nodes;
  64. protected function setUp()
  65. {
  66. parent::setUp();
  67. $this->_prepareLdapServer();
  68. $this->_orgDn=$this->_createDn('ou=OrgTest,');
  69. $this->_newDn=$this->_createDn('ou=NewTest,');
  70. $this->_orgSubTreeDn=$this->_createDn('ou=OrgSubtree,');
  71. $this->_newSubTreeDn=$this->_createDn('ou=NewSubtree,');
  72. $this->_targetSubTreeDn=$this->_createDn('ou=Target,');
  73. $this->_nodes=array(
  74. $this->_orgDn => array("objectClass" => "organizationalUnit", "ou" => "OrgTest"),
  75. $this->_orgSubTreeDn => array("objectClass" => "organizationalUnit", "ou" => "OrgSubtree"),
  76. 'ou=Subtree1,' . $this->_orgSubTreeDn =>
  77. array("objectClass" => "organizationalUnit", "ou" => "Subtree1"),
  78. 'ou=Subtree11,ou=Subtree1,' . $this->_orgSubTreeDn =>
  79. array("objectClass" => "organizationalUnit", "ou" => "Subtree11"),
  80. 'ou=Subtree12,ou=Subtree1,' . $this->_orgSubTreeDn =>
  81. array("objectClass" => "organizationalUnit", "ou" => "Subtree12"),
  82. 'ou=Subtree13,ou=Subtree1,' . $this->_orgSubTreeDn =>
  83. array("objectClass" => "organizationalUnit", "ou" => "Subtree13"),
  84. 'ou=Subtree2,' . $this->_orgSubTreeDn =>
  85. array("objectClass" => "organizationalUnit", "ou" => "Subtree2"),
  86. 'ou=Subtree3,' . $this->_orgSubTreeDn =>
  87. array("objectClass" => "organizationalUnit", "ou" => "Subtree3"),
  88. $this->_targetSubTreeDn => array("objectClass" => "organizationalUnit", "ou" => "Target")
  89. );
  90. $ldap=$this->_getLdap()->getResource();
  91. foreach ($this->_nodes as $dn => $entry) {
  92. ldap_add($ldap, $dn, $entry);
  93. }
  94. }
  95. protected function tearDown()
  96. {
  97. if ($this->_getLdap()->exists($this->_newDn))
  98. $this->_getLdap()->delete($this->_newDn, false);
  99. if ($this->_getLdap()->exists($this->_orgDn))
  100. $this->_getLdap()->delete($this->_orgDn, false);
  101. if ($this->_getLdap()->exists($this->_orgSubTreeDn))
  102. $this->_getLdap()->delete($this->_orgSubTreeDn, true);
  103. if ($this->_getLdap()->exists($this->_newSubTreeDn))
  104. $this->_getLdap()->delete($this->_newSubTreeDn, true);
  105. if ($this->_getLdap()->exists($this->_targetSubTreeDn))
  106. $this->_getLdap()->delete($this->_targetSubTreeDn, true);
  107. $this->_cleanupLdapServer();
  108. parent::tearDown();
  109. }
  110. public function testSimpleLeafRename()
  111. {
  112. $org=$this->_getLdap()->getEntry($this->_orgDn, array(), true);
  113. $this->_getLdap()->rename($this->_orgDn, $this->_newDn, false);
  114. $this->assertFalse($this->_getLdap()->exists($this->_orgDn));
  115. $this->assertTrue($this->_getLdap()->exists($this->_newDn));
  116. $new=$this->_getLdap()->getEntry($this->_newDn);
  117. $this->assertEquals($org['objectclass'], $new['objectclass']);
  118. $this->assertEquals(array('NewTest'), $new['ou']);
  119. }
  120. public function testSimpleLeafMoveAlias()
  121. {
  122. $this->_getLdap()->move($this->_orgDn, $this->_newDn, false);
  123. $this->assertFalse($this->_getLdap()->exists($this->_orgDn));
  124. $this->assertTrue($this->_getLdap()->exists($this->_newDn));
  125. }
  126. public function testSimpleLeafMoveToSubtree()
  127. {
  128. $this->_getLdap()->moveToSubtree($this->_orgDn, $this->_orgSubTreeDn, false);
  129. $this->assertFalse($this->_getLdap()->exists($this->_orgDn));
  130. $this->assertTrue($this->_getLdap()->exists('ou=OrgTest,' . $this->_orgSubTreeDn));
  131. }
  132. /**
  133. * @expectedException Zend_Ldap_Exception
  134. */
  135. public function testRenameSourceNotExists()
  136. {
  137. $this->_getLdap()->rename($this->_createDn('ou=DoesNotExist,'), $this->_newDn, false);
  138. }
  139. /**
  140. * @expectedException Zend_Ldap_Exception
  141. */
  142. public function testRenameTargetExists()
  143. {
  144. $this->_getLdap()->rename($this->_orgDn, $this->_createDn('ou=Test1,'), false);
  145. }
  146. /**
  147. * @expectedException Zend_Ldap_Exception
  148. */
  149. public function testRenameTargetParentNotExists()
  150. {
  151. $this->_getLdap()->rename($this->_orgDn, $this->_createDn('ou=Test1,ou=ParentDoesNotExist,'), false);
  152. }
  153. /**
  154. * @expectedException Zend_Ldap_Exception
  155. */
  156. public function testRenameEmulationSourceNotExists()
  157. {
  158. $this->_getLdap()->rename($this->_createDn('ou=DoesNotExist,'), $this->_newDn, false, true);
  159. }
  160. /**
  161. * @expectedException Zend_Ldap_Exception
  162. */
  163. public function testRenameEmulationTargetExists()
  164. {
  165. $this->_getLdap()->rename($this->_orgDn, $this->_createDn('ou=Test1,'), false, true);
  166. }
  167. /**
  168. * @expectedException Zend_Ldap_Exception
  169. */
  170. public function testRenameEmulationTargetParentNotExists()
  171. {
  172. $this->_getLdap()->rename($this->_orgDn, $this->_createDn('ou=Test1,ou=ParentDoesNotExist,'),
  173. false, true);
  174. }
  175. public function testSimpleLeafRenameEmulation()
  176. {
  177. $this->_getLdap()->rename($this->_orgDn, $this->_newDn, false, true);
  178. $this->assertFalse($this->_getLdap()->exists($this->_orgDn));
  179. $this->assertTrue($this->_getLdap()->exists($this->_newDn));
  180. }
  181. public function testSimpleLeafCopyToSubtree()
  182. {
  183. $this->_getLdap()->copyToSubtree($this->_orgDn, $this->_orgSubTreeDn, false);
  184. $this->assertTrue($this->_getLdap()->exists($this->_orgDn));
  185. $this->assertTrue($this->_getLdap()->exists('ou=OrgTest,' . $this->_orgSubTreeDn));
  186. }
  187. public function testSimpleLeafCopy()
  188. {
  189. $this->_getLdap()->copy($this->_orgDn, $this->_newDn, false);
  190. $this->assertTrue($this->_getLdap()->exists($this->_orgDn));
  191. $this->assertTrue($this->_getLdap()->exists($this->_newDn));
  192. }
  193. public function testRecursiveRename()
  194. {
  195. $this->_getLdap()->rename($this->_orgSubTreeDn, $this->_newSubTreeDn, true);
  196. $this->assertFalse($this->_getLdap()->exists($this->_orgSubTreeDn));
  197. $this->assertTrue($this->_getLdap()->exists($this->_newSubTreeDn));
  198. $this->assertEquals(3, $this->_getLdap()->countChildren($this->_newSubTreeDn));
  199. $this->assertEquals(3, $this->_getLdap()->countChildren('ou=Subtree1,' . $this->_newSubTreeDn));
  200. }
  201. public function testRecursiveMoveToSubtree()
  202. {
  203. $this->_getLdap()->moveToSubtree($this->_orgSubTreeDn, $this->_targetSubTreeDn, true);
  204. $this->assertFalse($this->_getLdap()->exists($this->_orgSubTreeDn));
  205. $this->assertTrue($this->_getLdap()->exists('ou=OrgSubtree,' . $this->_targetSubTreeDn));
  206. $this->assertEquals(3, $this->_getLdap()->countChildren('ou=OrgSubtree,' . $this->_targetSubTreeDn));
  207. $this->assertEquals(3, $this->_getLdap()->countChildren('ou=Subtree1,ou=OrgSubtree,' . $this->_targetSubTreeDn));
  208. }
  209. public function testRecursiveCopyToSubtree()
  210. {
  211. $this->_getLdap()->copyToSubtree($this->_orgSubTreeDn, $this->_targetSubTreeDn, true);
  212. $this->assertTrue($this->_getLdap()->exists($this->_orgSubTreeDn));
  213. $this->assertTrue($this->_getLdap()->exists('ou=OrgSubtree,' . $this->_targetSubTreeDn));
  214. $this->assertEquals(3, $this->_getLdap()->countChildren($this->_orgSubTreeDn));
  215. $this->assertEquals(3, $this->_getLdap()->countChildren('ou=Subtree1,' . $this->_orgSubTreeDn));
  216. $this->assertEquals(3, $this->_getLdap()->countChildren('ou=OrgSubtree,' . $this->_targetSubTreeDn));
  217. $this->assertEquals(3, $this->_getLdap()->countChildren('ou=Subtree1,ou=OrgSubtree,' . $this->_targetSubTreeDn));
  218. }
  219. public function testRecursiveCopy()
  220. {
  221. $this->_getLdap()->copy($this->_orgSubTreeDn, $this->_newSubTreeDn, true);
  222. $this->assertTrue($this->_getLdap()->exists($this->_orgSubTreeDn));
  223. $this->assertTrue($this->_getLdap()->exists($this->_newSubTreeDn));
  224. $this->assertEquals(3, $this->_getLdap()->countChildren($this->_orgSubTreeDn));
  225. $this->assertEquals(3, $this->_getLdap()->countChildren('ou=Subtree1,' . $this->_orgSubTreeDn));
  226. $this->assertEquals(3, $this->_getLdap()->countChildren($this->_newSubTreeDn));
  227. $this->assertEquals(3, $this->_getLdap()->countChildren('ou=Subtree1,' . $this->_newSubTreeDn));
  228. }
  229. public function testSimpleLeafRenameWithDnObjects()
  230. {
  231. $orgDn=Zend_Ldap_Dn::fromString($this->_orgDn);
  232. $newDn=Zend_Ldap_Dn::fromString($this->_newDn);
  233. $this->_getLdap()->rename($orgDn, $newDn, false);
  234. $this->assertFalse($this->_getLdap()->exists($orgDn));
  235. $this->assertTrue($this->_getLdap()->exists($newDn));
  236. $this->_getLdap()->move($newDn, $orgDn, false);
  237. $this->assertTrue($this->_getLdap()->exists($orgDn));
  238. $this->assertFalse($this->_getLdap()->exists($newDn));
  239. }
  240. public function testSimpleLeafMoveToSubtreeWithDnObjects()
  241. {
  242. $orgDn=Zend_Ldap_Dn::fromString($this->_orgDn);
  243. $orgSubTreeDn=Zend_Ldap_Dn::fromString($this->_orgSubTreeDn);
  244. $this->_getLdap()->moveToSubtree($orgDn, $orgSubTreeDn, false);
  245. $this->assertFalse($this->_getLdap()->exists($orgDn));
  246. $this->assertTrue($this->_getLdap()->exists('ou=OrgTest,' . $orgSubTreeDn->toString()));
  247. }
  248. public function testSimpleLeafRenameEmulationWithDnObjects()
  249. {
  250. $orgDn=Zend_Ldap_Dn::fromString($this->_orgDn);
  251. $newDn=Zend_Ldap_Dn::fromString($this->_newDn);
  252. $this->_getLdap()->rename($orgDn, $newDn, false, true);
  253. $this->assertFalse($this->_getLdap()->exists($orgDn));
  254. $this->assertTrue($this->_getLdap()->exists($newDn));
  255. }
  256. public function testSimpleLeafCopyToSubtreeWithDnObjects()
  257. {
  258. $orgDn=Zend_Ldap_Dn::fromString($this->_orgDn);
  259. $orgSubTreeDn=Zend_Ldap_Dn::fromString($this->_orgSubTreeDn);
  260. $this->_getLdap()->copyToSubtree($orgDn, $orgSubTreeDn, false);
  261. $this->assertTrue($this->_getLdap()->exists($orgDn));
  262. $this->assertTrue($this->_getLdap()->exists('ou=OrgTest,' . $orgSubTreeDn->toString()));
  263. }
  264. public function testSimpleLeafCopyWithDnObjects()
  265. {
  266. $orgDn=Zend_Ldap_Dn::fromString($this->_orgDn);
  267. $newDn=Zend_Ldap_Dn::fromString($this->_newDn);
  268. $this->_getLdap()->copy($orgDn, $newDn, false);
  269. $this->assertTrue($this->_getLdap()->exists($orgDn));
  270. $this->assertTrue($this->_getLdap()->exists($newDn));
  271. }
  272. public function testRecursiveRenameWithDnObjects()
  273. {
  274. $orgSubTreeDn=Zend_Ldap_Dn::fromString($this->_orgSubTreeDn);
  275. $newSubTreeDn=Zend_Ldap_Dn::fromString($this->_newSubTreeDn);
  276. $this->_getLdap()->rename($orgSubTreeDn, $newSubTreeDn, true);
  277. $this->assertFalse($this->_getLdap()->exists($orgSubTreeDn));
  278. $this->assertTrue($this->_getLdap()->exists($newSubTreeDn));
  279. $this->assertEquals(3, $this->_getLdap()->countChildren($newSubTreeDn));
  280. $this->assertEquals(3, $this->_getLdap()->countChildren('ou=Subtree1,' . $newSubTreeDn->toString()));
  281. }
  282. public function testRecursiveMoveToSubtreeWithDnObjects()
  283. {
  284. $orgSubTreeDn=Zend_Ldap_Dn::fromString($this->_orgSubTreeDn);
  285. $targetSubTreeDn=Zend_Ldap_Dn::fromString($this->_targetSubTreeDn);
  286. $this->_getLdap()->moveToSubtree($orgSubTreeDn, $targetSubTreeDn, true);
  287. $this->assertFalse($this->_getLdap()->exists($orgSubTreeDn));
  288. $this->assertTrue($this->_getLdap()->exists('ou=OrgSubtree,' . $targetSubTreeDn->toString()));
  289. $this->assertEquals(3, $this->_getLdap()->countChildren('ou=OrgSubtree,' . $targetSubTreeDn->toString()));
  290. $this->assertEquals(3, $this->_getLdap()->countChildren('ou=Subtree1,ou=OrgSubtree,' . $targetSubTreeDn->toString()));
  291. }
  292. public function testRecursiveCopyToSubtreeWithDnObjects()
  293. {
  294. $orgSubTreeDn=Zend_Ldap_Dn::fromString($this->_orgSubTreeDn);
  295. $targetSubTreeDn=Zend_Ldap_Dn::fromString($this->_targetSubTreeDn);
  296. $this->_getLdap()->copyToSubtree($orgSubTreeDn, $targetSubTreeDn, true);
  297. $this->assertTrue($this->_getLdap()->exists($orgSubTreeDn));
  298. $this->assertTrue($this->_getLdap()->exists('ou=OrgSubtree,' . $targetSubTreeDn->toString()));
  299. $this->assertEquals(3, $this->_getLdap()->countChildren($orgSubTreeDn));
  300. $this->assertEquals(3, $this->_getLdap()->countChildren('ou=Subtree1,' . $orgSubTreeDn->toString()));
  301. $this->assertEquals(3, $this->_getLdap()->countChildren('ou=OrgSubtree,' . $targetSubTreeDn->toString()));
  302. $this->assertEquals(3, $this->_getLdap()->countChildren('ou=Subtree1,ou=OrgSubtree,' . $targetSubTreeDn->toString()));
  303. }
  304. public function testRecursiveCopyWithDnObjects()
  305. {
  306. $orgSubTreeDn=Zend_Ldap_Dn::fromString($this->_orgSubTreeDn);
  307. $newSubTreeDn=Zend_Ldap_Dn::fromString($this->_newSubTreeDn);
  308. $this->_getLdap()->copy($orgSubTreeDn, $newSubTreeDn, true);
  309. $this->assertTrue($this->_getLdap()->exists($orgSubTreeDn));
  310. $this->assertTrue($this->_getLdap()->exists($newSubTreeDn));
  311. $this->assertEquals(3, $this->_getLdap()->countChildren($orgSubTreeDn));
  312. $this->assertEquals(3, $this->_getLdap()->countChildren('ou=Subtree1,' . $orgSubTreeDn->toString()));
  313. $this->assertEquals(3, $this->_getLdap()->countChildren($newSubTreeDn));
  314. $this->assertEquals(3, $this->_getLdap()->countChildren('ou=Subtree1,' . $newSubTreeDn->toString()));
  315. }
  316. }