PageRenderTime 27ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/Ldap/Node/OnlineTest.php

https://github.com/mfairchild365/zf2
PHP | 286 lines | 219 code | 24 blank | 43 comment | 2 complexity | be077d34ea7cf211554d960caf297567 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\Node;
  25. use Zend\Ldap\Node;
  26. use Zend\Ldap;
  27. /**
  28. * @category Zend
  29. * @package Zend_LDAP
  30. * @subpackage UnitTests
  31. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. * @group Zend_LDAP
  34. * @group Zend_LDAP_Node
  35. */
  36. class OnlineTest extends \ZendTest\Ldap\OnlineTestCase
  37. {
  38. protected function setUp()
  39. {
  40. parent::setUp();
  41. $this->_prepareLDAPServer();
  42. }
  43. protected function tearDown()
  44. {
  45. $this->_cleanupLDAPServer();
  46. parent::tearDown();
  47. }
  48. public function testLoadFromLDAP()
  49. {
  50. $dn=$this->_createDn('ou=Test1,');
  51. $node=Node::fromLDAP($dn, $this->_getLDAP());
  52. $this->assertType('Zend\Ldap\Node', $node);
  53. $this->assertTrue($node->isAttached());
  54. }
  55. public function testChangeReadOnlySystemAttributes()
  56. {
  57. $node=$this->_getLDAP()->getBaseNode();
  58. try {
  59. $node->setAttribute('createTimestamp', false);
  60. $this->fail('Expected exception for modification of read-only attribute createTimestamp');
  61. } catch (Ldap\Exception $e) {
  62. $this->assertEquals('Cannot change attribute because it\'s read-only', $e->getMessage());
  63. }
  64. try {
  65. $node->createTimestamp=false;
  66. $this->fail('Expected exception for modification of read-only attribute createTimestamp');
  67. } catch (Ldap\Exception $e) {
  68. $this->assertEquals('Cannot change attribute because it\'s read-only', $e->getMessage());
  69. }
  70. try {
  71. $node['createTimestamp']=false;
  72. $this->fail('Expected exception for modification of read-only attribute createTimestamp');
  73. } catch (Ldap\Exception $e) {
  74. $this->assertEquals('Cannot change attribute because it\'s read-only', $e->getMessage());
  75. }
  76. try {
  77. $node->appendToAttribute('createTimestamp', 'value');
  78. $this->fail('Expected exception for modification of read-only attribute createTimestamp');
  79. } catch (Ldap\Exception $e) {
  80. $this->assertEquals('Cannot change attribute because it\'s read-only', $e->getMessage());
  81. }
  82. try {
  83. $rdn=$node->getRdnArray(Ldap\Dn::ATTR_CASEFOLD_LOWER);
  84. $attr=key($rdn);
  85. $node->deleteAttribute($attr);
  86. $this->fail('Expected exception for modification of read-only attribute ' . $attr);
  87. } catch (Ldap\Exception $e) {
  88. $this->assertEquals('Cannot change attribute because it\'s part of the RDN', $e->getMessage());
  89. }
  90. }
  91. /**
  92. * @expectedException Zend\Ldap\Exception
  93. */
  94. public function testLoadFromLDAPIllegalEntry()
  95. {
  96. $dn=$this->_createDn('ou=Test99,');
  97. $node=Node::fromLDAP($dn, $this->_getLDAP());
  98. }
  99. public function testDetachAndReattach()
  100. {
  101. $dn=$this->_createDn('ou=Test1,');
  102. $node=Node::fromLDAP($dn, $this->_getLDAP());
  103. $this->assertType('Zend\Ldap\Node', $node);
  104. $this->assertTrue($node->isAttached());
  105. $node->detachLDAP();
  106. $this->assertFalse($node->isAttached());
  107. $node->attachLDAP($this->_getLDAP());
  108. $this->assertTrue($node->isAttached());
  109. }
  110. public function testSerialize()
  111. {
  112. $dn=$this->_createDn('ou=Test1,');
  113. $node=Node::fromLDAP($dn, $this->_getLDAP());
  114. $sdata=serialize($node);
  115. $newObject=unserialize($sdata);
  116. $this->assertFalse($newObject->isAttached());
  117. $this->assertTrue($node->isAttached());
  118. $this->assertEquals($sdata, serialize($newObject));
  119. }
  120. /**
  121. * @expectedException Zend\Ldap\Exception
  122. */
  123. public function testAttachToInvalidLDAP()
  124. {
  125. $data=array(
  126. 'dn' => 'ou=name,dc=example,dc=org',
  127. 'ou' => array('name'),
  128. 'l' => array('a', 'b', 'c'),
  129. 'objectClass' => array('organizationalUnit', 'top'),
  130. );
  131. $node=Node::fromArray($data);
  132. $this->assertFalse($node->isAttached());
  133. $node->attachLDAP($this->_getLDAP());
  134. }
  135. public function testAttachToValidLDAP()
  136. {
  137. $data=array(
  138. 'dn' => $this->_createDn('ou=name,'),
  139. 'ou' => array('name'),
  140. 'l' => array('a', 'b', 'c'),
  141. 'objectClass' => array('organizationalUnit', 'top'),
  142. );
  143. $node=Node::fromArray($data);
  144. $this->assertFalse($node->isAttached());
  145. $node->attachLDAP($this->_getLDAP());
  146. $this->assertTrue($node->isAttached());
  147. }
  148. public function testExistsDn()
  149. {
  150. $data=array(
  151. 'dn' => $this->_createDn('ou=name,'),
  152. 'ou' => array('name'),
  153. 'l' => array('a', 'b', 'c'),
  154. 'objectClass' => array('organizationalUnit', 'top'),
  155. );
  156. $node1=Node::fromArray($data);
  157. $node1->attachLDAP($this->_getLDAP());
  158. $this->assertFalse($node1->exists());
  159. $dn=$this->_createDn('ou=Test1,');
  160. $node2=Node::fromLDAP($dn, $this->_getLDAP());
  161. $this->assertTrue($node2->exists());
  162. }
  163. public function testReload()
  164. {
  165. $dn=$this->_createDn('ou=Test1,');
  166. $node=Node::fromLDAP($dn, $this->_getLDAP());
  167. $node->reload();
  168. $this->assertEquals($dn, $node->getDn()->toString());
  169. $this->assertEquals('ou=Test1', $node->getRdnString());
  170. }
  171. public function testGetNode()
  172. {
  173. $dn=$this->_createDn('ou=Test1,');
  174. $node=$this->_getLDAP()->getNode($dn);
  175. $this->assertEquals($dn, $node->getDn()->toString());
  176. $this->assertEquals("Test1", $node->getAttribute('ou', 0));
  177. }
  178. /**
  179. * @expectedException Zend\Ldap\Exception
  180. */
  181. public function testGetIllegalNode()
  182. {
  183. $dn=$this->_createDn('ou=Test99,');
  184. $node=$this->_getLDAP()->getNode($dn);
  185. }
  186. public function testGetBaseNode()
  187. {
  188. $node=$this->_getLDAP()->getBaseNode();
  189. $this->assertEquals(TESTS_ZEND_LDAP_WRITEABLE_SUBTREE, $node->getDnString());
  190. $dn=Ldap\Dn::fromString(TESTS_ZEND_LDAP_WRITEABLE_SUBTREE,
  191. Ldap\Dn::ATTR_CASEFOLD_LOWER);
  192. $this->assertEquals($dn[0]['ou'], $node->getAttribute('ou', 0));
  193. }
  194. public function testSearchSubtree()
  195. {
  196. $node=$this->_getLDAP()->getNode($this->_createDn('ou=Node,'));
  197. $items=$node->searchSubtree('(objectClass=organizationalUnit)', Ldap\Ldap::SEARCH_SCOPE_SUB,
  198. array(), 'ou');
  199. $this->assertType('Zend\Ldap\Node\Collection', $items);
  200. $this->assertEquals(3, $items->count());
  201. $i=0;
  202. $dns=array(
  203. $this->_createDn('ou=Node,'),
  204. $this->_createDn('ou=Test1,ou=Node,'),
  205. $this->_createDn('ou=Test2,ou=Node,'));
  206. foreach ($items as $key => $node) {
  207. $key=Ldap\Dn::fromString($key)->toString(Ldap\Dn::ATTR_CASEFOLD_LOWER);
  208. $this->assertEquals($dns[$i], $key);
  209. if ($i === 0) {
  210. $this->assertEquals('Node', $node->ou[0]);
  211. } else {
  212. $this->assertEquals('Test' . $i, $node->ou[0]);
  213. }
  214. $this->assertEquals($key, $node->getDnString(Ldap\Dn::ATTR_CASEFOLD_LOWER));
  215. $i++;
  216. }
  217. $this->assertEquals(3, $i);
  218. }
  219. public function testCountSubtree()
  220. {
  221. $node=$this->_getLDAP()->getNode(TESTS_ZEND_LDAP_WRITEABLE_SUBTREE);
  222. $this->assertEquals(9, $node->countSubtree('(objectClass=organizationalUnit)',
  223. Ldap\Ldap::SEARCH_SCOPE_SUB));
  224. }
  225. public function testCountChildren()
  226. {
  227. $node=$this->_getLDAP()->getNode(TESTS_ZEND_LDAP_WRITEABLE_SUBTREE);
  228. $this->assertEquals(6, $node->countChildren());
  229. $node=$this->_getLDAP()->getNode($this->_createDn('ou=Node,'));
  230. $this->assertEquals(2, $node->countChildren());
  231. }
  232. public function testSearchChildren()
  233. {
  234. $node=$this->_getLDAP()->getNode($this->_createDn('ou=Node,'));
  235. $this->assertEquals(2, $node->searchChildren('(objectClass=*)', array(), 'ou')->count());
  236. $node=$this->_getLDAP()->getNode(TESTS_ZEND_LDAP_WRITEABLE_SUBTREE);
  237. $this->assertEquals(6, $node->searchChildren('(objectClass=*)', array(), 'ou')->count());
  238. }
  239. public function testGetParent()
  240. {
  241. $node=$this->_getLDAP()->getNode($this->_createDn('ou=Node,'));
  242. $pnode=$node->getParent();
  243. $this->assertEquals(Ldap\Dn::fromString(TESTS_ZEND_LDAP_WRITEABLE_SUBTREE)
  244. ->toString(Ldap\Dn::ATTR_CASEFOLD_LOWER),
  245. $pnode->getDnString(Ldap\Dn::ATTR_CASEFOLD_LOWER));
  246. }
  247. /**
  248. * @expectedException Zend\Ldap\Exception
  249. */
  250. public function testGetNonexistantParent()
  251. {
  252. $node=$this->_getLDAP()->getNode(TESTS_ZEND_LDAP_WRITEABLE_SUBTREE);
  253. $pnode=$node->getParent();
  254. }
  255. public function testLoadFromLDAPWithDnObject()
  256. {
  257. $dn=Ldap\Dn::fromString($this->_createDn('ou=Test1,'));
  258. $node=Node::fromLDAP($dn, $this->_getLDAP());
  259. $this->assertType('Zend\Ldap\Node', $node);
  260. $this->assertTrue($node->isAttached());
  261. }
  262. }