/tests/Zend/Ldap/CopyRenameTest.php

https://github.com/bruisedlee/zf2 · PHP · 361 lines · 249 code · 46 blank · 66 comment · 6 complexity · 7a5601359d67a9aacea0a3baad20efe3 MD5 · raw file

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