UpdateTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'OnlineTestCase.php';
  26. /**
  27. * @see Zend_Ldap_Node
  28. */
  29. require_once 'Zend/Ldap/Node.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. * @group Zend_Ldap_Node
  38. */
  39. class Zend_Ldap_Node_UpdateTest extends Zend_Ldap_OnlineTestCase
  40. {
  41. protected function setUp()
  42. {
  43. parent::setUp();
  44. $this->_prepareLdapServer();
  45. }
  46. protected function tearDown()
  47. {
  48. foreach ($this->_getLdap()->getBaseNode()->searchChildren('objectClass=*') as $child) {
  49. $this->_getLdap()->delete($child->getDn(), true);
  50. }
  51. parent::tearDown();
  52. }
  53. protected function _stripActiveDirectorySystemAttributes(&$entry)
  54. {
  55. $adAttributes = array('distinguishedname', 'instancetype', 'name', 'objectcategory',
  56. 'objectguid', 'usnchanged', 'usncreated', 'whenchanged', 'whencreated');
  57. foreach ($adAttributes as $attr) {
  58. if (array_key_exists($attr, $entry)) {
  59. unset($entry[$attr]);
  60. }
  61. }
  62. if (array_key_exists('objectclass', $entry) && count($entry['objectclass']) > 0) {
  63. if ($entry['objectclass'][0] !== 'top') {
  64. $entry['objectclass']=array_merge(array('top'), $entry['objectclass']);
  65. }
  66. }
  67. }
  68. public function testSimpleUpdateOneValue()
  69. {
  70. $dn=$this->_createDn('ou=Test1,');
  71. $node1=Zend_Ldap_Node::fromLdap($dn, $this->_getLdap());
  72. $node1->l='f';
  73. $node1->update();
  74. $this->assertTrue($this->_getLdap()->exists($dn));
  75. $node2=$this->_getLdap()->getEntry($dn);
  76. $this->_stripActiveDirectorySystemAttributes($node2);
  77. unset($node2['dn']);
  78. $node1=$node1->getData(false);
  79. $this->_stripActiveDirectorySystemAttributes($node1);
  80. $this->assertEquals($node2, $node1);
  81. }
  82. public function testAddNewNode()
  83. {
  84. $dn=$this->_createDn('ou=Test,');
  85. $node1=Zend_Ldap_Node::create($dn, array('organizationalUnit'));
  86. $node1->l='a';
  87. $node1->update($this->_getLdap());
  88. $this->assertTrue($this->_getLdap()->exists($dn));
  89. $node2=$this->_getLdap()->getEntry($dn);
  90. $this->_stripActiveDirectorySystemAttributes($node2);
  91. unset($node2['dn']);
  92. $node1=$node1->getData(false);
  93. $this->_stripActiveDirectorySystemAttributes($node1);
  94. $this->assertEquals($node2, $node1);
  95. }
  96. public function testMoveExistingNode()
  97. {
  98. $dnOld=$this->_createDn('ou=Test1,');
  99. $dnNew=$this->_createDn('ou=Test,');
  100. $node1=Zend_Ldap_Node::fromLdap($dnOld, $this->_getLdap());
  101. $node1->l='f';
  102. $node1->setDn($dnNew);
  103. $node1->update();
  104. $this->assertFalse($this->_getLdap()->exists($dnOld));
  105. $this->assertTrue($this->_getLdap()->exists($dnNew));
  106. $node2=$this->_getLdap()->getEntry($dnNew);
  107. $this->_stripActiveDirectorySystemAttributes($node2);
  108. unset($node2['dn']);
  109. $node1=$node1->getData(false);
  110. $this->_stripActiveDirectorySystemAttributes($node1);
  111. $this->assertEquals($node2, $node1);
  112. }
  113. public function testMoveNewNode()
  114. {
  115. $dnOld=$this->_createDn('ou=Test,');
  116. $dnNew=$this->_createDn('ou=TestNew,');
  117. $node1=Zend_Ldap_Node::create($dnOld, array('organizationalUnit'));
  118. $node1->l='a';
  119. $node1->setDn($dnNew);
  120. $node1->update($this->_getLdap());
  121. $this->assertFalse($this->_getLdap()->exists($dnOld));
  122. $this->assertTrue($this->_getLdap()->exists($dnNew));
  123. $node2=$this->_getLdap()->getEntry($dnNew);
  124. $this->_stripActiveDirectorySystemAttributes($node2);
  125. unset($node2['dn']);
  126. $node1=$node1->getData(false);
  127. $this->_stripActiveDirectorySystemAttributes($node1);
  128. $this->assertEquals($node2, $node1);
  129. }
  130. public function testModifyDeletedNode()
  131. {
  132. $dn=$this->_createDn('ou=Test1,');
  133. $node1=Zend_Ldap_Node::create($dn, array('organizationalUnit'));
  134. $node1->delete();
  135. $node1->update($this->_getLdap());
  136. $this->assertFalse($this->_getLdap()->exists($dn));
  137. $node1->l='a';
  138. $node1->update();
  139. $this->assertFalse($this->_getLdap()->exists($dn));
  140. }
  141. public function testAddDeletedNode()
  142. {
  143. $dn=$this->_createDn('ou=Test,');
  144. $node1=Zend_Ldap_Node::create($dn, array('organizationalUnit'));
  145. $node1->delete();
  146. $node1->update($this->_getLdap());
  147. $this->assertFalse($this->_getLdap()->exists($dn));
  148. }
  149. public function testMoveDeletedExistingNode()
  150. {
  151. $dnOld=$this->_createDn('ou=Test1,');
  152. $dnNew=$this->_createDn('ou=Test,');
  153. $node1=Zend_Ldap_Node::fromLdap($dnOld, $this->_getLdap());
  154. $node1->setDn($dnNew);
  155. $node1->delete();
  156. $node1->update();
  157. $this->assertFalse($this->_getLdap()->exists($dnOld));
  158. $this->assertFalse($this->_getLdap()->exists($dnNew));
  159. }
  160. public function testMoveDeletedNewNode()
  161. {
  162. $dnOld=$this->_createDn('ou=Test,');
  163. $dnNew=$this->_createDn('ou=TestNew,');
  164. $node1=Zend_Ldap_Node::create($dnOld, array('organizationalUnit'));
  165. $node1->setDn($dnNew);
  166. $node1->delete();
  167. $node1->update($this->_getLdap());
  168. $this->assertFalse($this->_getLdap()->exists($dnOld));
  169. $this->assertFalse($this->_getLdap()->exists($dnNew));
  170. }
  171. public function testMoveNode()
  172. {
  173. $dnOld=$this->_createDn('ou=Test1,');
  174. $dnNew=$this->_createDn('ou=Test,');
  175. $node=Zend_Ldap_Node::fromLdap($dnOld, $this->_getLdap());
  176. $node->setDn($dnNew);
  177. $node->update();
  178. $this->assertFalse($this->_getLdap()->exists($dnOld));
  179. $this->assertTrue($this->_getLdap()->exists($dnNew));
  180. $node=Zend_Ldap_Node::fromLdap($dnNew, $this->_getLdap());
  181. $node->move($dnOld);
  182. $node->update();
  183. $this->assertFalse($this->_getLdap()->exists($dnNew));
  184. $this->assertTrue($this->_getLdap()->exists($dnOld));
  185. $node=Zend_Ldap_Node::fromLdap($dnOld, $this->_getLdap());
  186. $node->rename($dnNew);
  187. $node->update();
  188. $this->assertFalse($this->_getLdap()->exists($dnOld));
  189. $this->assertTrue($this->_getLdap()->exists($dnNew));
  190. }
  191. }