PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/ZendTest/Ldap/CopyRenameTest.php

https://bitbucket.org/saifshuvo/zf2
PHP | 364 lines | 268 code | 46 blank | 50 comment | 6 complexity | 1932bf1379e14dfb428f64bcb06bffa6 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. * @package Zend_Ldap
  9. */
  10. namespace ZendTest\Ldap;
  11. use Zend\Ldap;
  12. /**
  13. * @category Zend
  14. * @package Zend_Ldap
  15. * @subpackage UnitTests
  16. * @group Zend_Ldap
  17. */
  18. class CopyRenameTest extends AbstractOnlineTestCase
  19. {
  20. /**
  21. * @var string
  22. */
  23. private $orgDn;
  24. /**
  25. * @var string
  26. */
  27. private $newDn;
  28. /**
  29. * @var string
  30. */
  31. private $orgSubTreeDn;
  32. /**
  33. * @var string
  34. */
  35. private $newSubTreeDn;
  36. /**
  37. * @var string
  38. */
  39. private $targetSubTreeDn;
  40. /**
  41. * @var array
  42. */
  43. private $nodes;
  44. protected function setUp()
  45. {
  46. parent::setUp();
  47. $this->prepareLDAPServer();
  48. $this->orgDn = $this->createDn('ou=OrgTest,');
  49. $this->newDn = $this->createDn('ou=NewTest,');
  50. $this->orgSubTreeDn = $this->createDn('ou=OrgSubtree,');
  51. $this->newSubTreeDn = $this->createDn('ou=NewSubtree,');
  52. $this->targetSubTreeDn = $this->createDn('ou=Target,');
  53. $this->nodes = array(
  54. $this->orgDn => array("objectClass" => "organizationalUnit",
  55. "ou" => "OrgTest"),
  56. $this->orgSubTreeDn => array("objectClass" => "organizationalUnit",
  57. "ou" => "OrgSubtree"),
  58. 'ou=Subtree1,' . $this->orgSubTreeDn =>
  59. array("objectClass" => "organizationalUnit",
  60. "ou" => "Subtree1"),
  61. 'ou=Subtree11,ou=Subtree1,' . $this->orgSubTreeDn =>
  62. array("objectClass" => "organizationalUnit",
  63. "ou" => "Subtree11"),
  64. 'ou=Subtree12,ou=Subtree1,' . $this->orgSubTreeDn =>
  65. array("objectClass" => "organizationalUnit",
  66. "ou" => "Subtree12"),
  67. 'ou=Subtree13,ou=Subtree1,' . $this->orgSubTreeDn =>
  68. array("objectClass" => "organizationalUnit",
  69. "ou" => "Subtree13"),
  70. 'ou=Subtree2,' . $this->orgSubTreeDn =>
  71. array("objectClass" => "organizationalUnit",
  72. "ou" => "Subtree2"),
  73. 'ou=Subtree3,' . $this->orgSubTreeDn =>
  74. array("objectClass" => "organizationalUnit",
  75. "ou" => "Subtree3"),
  76. $this->targetSubTreeDn => array("objectClass" => "organizationalUnit",
  77. "ou" => "Target")
  78. );
  79. $ldap = $this->getLDAP()->getResource();
  80. foreach ($this->nodes as $dn => $entry) {
  81. ldap_add($ldap, $dn, $entry);
  82. }
  83. }
  84. protected function tearDown()
  85. {
  86. if (!constant('TESTS_ZEND_LDAP_ONLINE_ENABLED')) {
  87. return;
  88. }
  89. if ($this->getLDAP()->exists($this->newDn)) {
  90. $this->getLDAP()->delete($this->newDn, false);
  91. }
  92. if ($this->getLDAP()->exists($this->orgDn)) {
  93. $this->getLDAP()->delete($this->orgDn, false);
  94. }
  95. if ($this->getLDAP()->exists($this->orgSubTreeDn)) {
  96. $this->getLDAP()->delete($this->orgSubTreeDn, true);
  97. }
  98. if ($this->getLDAP()->exists($this->newSubTreeDn)) {
  99. $this->getLDAP()->delete($this->newSubTreeDn, true);
  100. }
  101. if ($this->getLDAP()->exists($this->targetSubTreeDn)) {
  102. $this->getLDAP()->delete($this->targetSubTreeDn, true);
  103. }
  104. $this->cleanupLDAPServer();
  105. parent::tearDown();
  106. }
  107. public function testSimpleLeafRename()
  108. {
  109. $org = $this->getLDAP()->getEntry($this->orgDn, array(), true);
  110. $this->getLDAP()->rename($this->orgDn, $this->newDn, false);
  111. $this->assertFalse($this->getLDAP()->exists($this->orgDn));
  112. $this->assertTrue($this->getLDAP()->exists($this->newDn));
  113. $new = $this->getLDAP()->getEntry($this->newDn);
  114. $this->assertEquals($org['objectclass'], $new['objectclass']);
  115. $this->assertEquals(array('NewTest'), $new['ou']);
  116. }
  117. public function testSimpleLeafMoveAlias()
  118. {
  119. $this->getLDAP()->move($this->orgDn, $this->newDn, false);
  120. $this->assertFalse($this->getLDAP()->exists($this->orgDn));
  121. $this->assertTrue($this->getLDAP()->exists($this->newDn));
  122. }
  123. public function testSimpleLeafMoveToSubtree()
  124. {
  125. $this->getLDAP()->moveToSubtree($this->orgDn, $this->orgSubTreeDn, false);
  126. $this->assertFalse($this->getLDAP()->exists($this->orgDn));
  127. $this->assertTrue($this->getLDAP()->exists('ou=OrgTest,' . $this->orgSubTreeDn));
  128. }
  129. /**
  130. * @expectedException Zend\Ldap\Exception\LdapException
  131. */
  132. public function testRenameSourceNotExists()
  133. {
  134. $this->getLDAP()->rename($this->createDn('ou=DoesNotExist,'), $this->newDn, false);
  135. }
  136. /**
  137. * @expectedException Zend\Ldap\Exception\LdapException
  138. */
  139. public function testRenameTargetExists()
  140. {
  141. $this->getLDAP()->rename($this->orgDn, $this->createDn('ou=Test1,'), false);
  142. }
  143. /**
  144. * @expectedException Zend\Ldap\Exception\LdapException
  145. */
  146. public function testRenameTargetParentNotExists()
  147. {
  148. $this->getLDAP()->rename($this->orgDn, $this->createDn('ou=Test1,ou=ParentDoesNotExist,'), false);
  149. }
  150. /**
  151. * @expectedException Zend\Ldap\Exception\LdapException
  152. */
  153. public function testRenameEmulationSourceNotExists()
  154. {
  155. $this->getLDAP()->rename($this->createDn('ou=DoesNotExist,'), $this->newDn, false, true);
  156. }
  157. /**
  158. * @expectedException Zend\Ldap\Exception\LdapException
  159. */
  160. public function testRenameEmulationTargetExists()
  161. {
  162. $this->getLDAP()->rename($this->orgDn, $this->createDn('ou=Test1,'), false, true);
  163. }
  164. /**
  165. * @expectedException Zend\Ldap\Exception\LdapException
  166. */
  167. public function testRenameEmulationTargetParentNotExists()
  168. {
  169. $this->getLDAP()->rename($this->orgDn, $this->createDn('ou=Test1,ou=ParentDoesNotExist,'),
  170. false, true
  171. );
  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 = Ldap\Dn::fromString($this->orgDn);
  230. $newDn = 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 = Ldap\Dn::fromString($this->orgDn);
  241. $orgSubTreeDn = 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 = Ldap\Dn::fromString($this->orgDn);
  249. $newDn = 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 = Ldap\Dn::fromString($this->orgDn);
  257. $orgSubTreeDn = 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 = Ldap\Dn::fromString($this->orgDn);
  265. $newDn = 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 = Ldap\Dn::fromString($this->orgSubTreeDn);
  273. $newSubTreeDn = 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 = Ldap\Dn::fromString($this->orgSubTreeDn);
  283. $targetSubTreeDn = 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,
  289. $this->getLDAP()->countChildren('ou=Subtree1,ou=OrgSubtree,' . $targetSubTreeDn->toString())
  290. );
  291. }
  292. public function testRecursiveCopyToSubtreeWithDnObjects()
  293. {
  294. $orgSubTreeDn = Ldap\Dn::fromString($this->orgSubTreeDn);
  295. $targetSubTreeDn = 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,
  303. $this->getLDAP()->countChildren('ou=Subtree1,ou=OrgSubtree,' . $targetSubTreeDn->toString())
  304. );
  305. }
  306. public function testRecursiveCopyWithDnObjects()
  307. {
  308. $orgSubTreeDn = Ldap\Dn::fromString($this->orgSubTreeDn);
  309. $newSubTreeDn = Ldap\Dn::fromString($this->newSubTreeDn);
  310. $this->getLDAP()->copy($orgSubTreeDn, $newSubTreeDn, true);
  311. $this->assertTrue($this->getLDAP()->exists($orgSubTreeDn));
  312. $this->assertTrue($this->getLDAP()->exists($newSubTreeDn));
  313. $this->assertEquals(3, $this->getLDAP()->countChildren($orgSubTreeDn));
  314. $this->assertEquals(3, $this->getLDAP()->countChildren('ou=Subtree1,' . $orgSubTreeDn->toString()));
  315. $this->assertEquals(3, $this->getLDAP()->countChildren($newSubTreeDn));
  316. $this->assertEquals(3, $this->getLDAP()->countChildren('ou=Subtree1,' . $newSubTreeDn->toString()));
  317. }
  318. }