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

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

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