CopyRenameTest.php 15 KB

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