PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/zendframework/zendframework/tests/ZendTest/Ldap/Node/OnlineTest.php

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