CrudTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * Zend_Ldap_OnlineTestCase
  23. */
  24. require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'OnlineTestCase.php';
  25. /**
  26. * @see Zend_Ldap_Dn
  27. */
  28. require_once 'Zend/Ldap/Dn.php';
  29. /**
  30. * @category Zend
  31. * @package Zend_Ldap
  32. * @subpackage UnitTests
  33. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Zend_Ldap_CrudTest extends Zend_Ldap_OnlineTestCase
  37. {
  38. public function testAddAndDelete()
  39. {
  40. $dn=$this->_createDn('ou=TestCreated,');
  41. $data=array(
  42. 'ou' => 'TestCreated',
  43. 'objectClass' => 'organizationalUnit'
  44. );
  45. try {
  46. $this->_getLdap()->add($dn, $data);
  47. $this->assertEquals(1, $this->_getLdap()->count('ou=TestCreated'));
  48. $this->_getLdap()->delete($dn);
  49. $this->assertEquals(0, $this->_getLdap()->count('ou=TestCreated'));
  50. } catch (Zend_Ldap_Exception $e) {
  51. if ($this->_getLdap()->exists($dn)) {
  52. $this->_getLdap()->delete($dn);
  53. }
  54. $this->fail($e->getMessage());
  55. }
  56. }
  57. public function testUpdate()
  58. {
  59. $dn=$this->_createDn('ou=TestCreated,');
  60. $data=array(
  61. 'ou' => 'TestCreated',
  62. 'l' => 'mylocation1',
  63. 'objectClass' => 'organizationalUnit'
  64. );
  65. try {
  66. $this->_getLdap()->add($dn, $data);
  67. $entry=$this->_getLdap()->getEntry($dn);
  68. $this->assertEquals('mylocation1', $entry['l'][0]);
  69. $entry['l']='mylocation2';
  70. $this->_getLdap()->update($dn, $entry);
  71. $entry=$this->_getLdap()->getEntry($dn);
  72. $this->_getLdap()->delete($dn);
  73. $this->assertEquals('mylocation2', $entry['l'][0]);
  74. } catch (Zend_Ldap_Exception $e) {
  75. if ($this->_getLdap()->exists($dn)) {
  76. $this->_getLdap()->delete($dn);
  77. }
  78. $this->fail($e->getMessage());
  79. }
  80. }
  81. /**
  82. * @expectedException Zend_Ldap_Exception
  83. */
  84. public function testIllegalAdd()
  85. {
  86. $dn=$this->_createDn('ou=TestCreated,ou=Node2,');
  87. $data=array(
  88. 'ou' => 'TestCreated',
  89. 'objectClass' => 'organizationalUnit'
  90. );
  91. $this->_getLdap()->add($dn, $data);
  92. $this->_getLdap()->delete($dn);
  93. }
  94. public function testIllegalUpdate()
  95. {
  96. $dn=$this->_createDn('ou=TestCreated,');
  97. $data=array(
  98. 'ou' => 'TestCreated',
  99. 'objectclass' => 'organizationalUnit'
  100. );
  101. try {
  102. $this->_getLdap()->add($dn, $data);
  103. $entry=$this->_getLdap()->getEntry($dn);
  104. $entry['objectclass'][]='inetOrgPerson';
  105. $exThrown=false;
  106. try {
  107. $this->_getLdap()->update($dn, $entry);
  108. }
  109. catch (Zend_Ldap_Exception $e) {
  110. $exThrown=true;
  111. }
  112. $this->_getLdap()->delete($dn);
  113. if (!$exThrown) $this->fail('no exception thrown while illegaly updating entry');
  114. }
  115. catch (Zend_Ldap_Exception $e) {
  116. $this->fail($e->getMessage());
  117. }
  118. }
  119. /**
  120. * @expectedException Zend_Ldap_Exception
  121. */
  122. public function testIllegalDelete()
  123. {
  124. $dn=$this->_createDn('ou=TestCreated,');
  125. $this->_getLdap()->delete($dn);
  126. }
  127. public function testDeleteRecursively()
  128. {
  129. $topDn=$this->_createDn('ou=RecursiveTest,');
  130. $dn=$topDn;
  131. $data=array('ou' => 'RecursiveTest', 'objectclass' => 'organizationalUnit'
  132. );
  133. $this->_getLdap()->add($dn, $data);
  134. for ($level=1; $level<=5; $level++) {
  135. $name='Level' . $level;
  136. $dn='ou=' . $name . ',' . $dn;
  137. $data=array('ou' => $name, 'objectclass' => 'organizationalUnit');
  138. $this->_getLdap()->add($dn, $data);
  139. for ($item=1; $item<=5; $item++) {
  140. $uid='Item' . $item;
  141. $idn='ou=' . $uid . ',' . $dn;
  142. $idata=array('ou' => $uid, 'objectclass' => 'organizationalUnit');
  143. $this->_getLdap()->add($idn, $idata);
  144. }
  145. }
  146. $exCaught=false;
  147. try {
  148. $this->_getLdap()->delete($topDn, false);
  149. } catch (Zend_Ldap_Exception $e) {
  150. $exCaught=true;
  151. }
  152. $this->assertTrue($exCaught,
  153. 'Execption not raised when deleting item with children without specifiying recursive delete');
  154. $this->_getLdap()->delete($topDn, true);
  155. $this->assertFalse($this->_getLdap()->exists($topDn));
  156. }
  157. public function testSave()
  158. {
  159. $dn=$this->_createDn('ou=TestCreated,');
  160. $data=array('ou' => 'TestCreated', 'objectclass' => 'organizationalUnit');
  161. try {
  162. $this->_getLdap()->save($dn, $data);
  163. $this->assertTrue($this->_getLdap()->exists($dn));
  164. $data['l']='mylocation1';
  165. $this->_getLdap()->save($dn, $data);
  166. $this->assertTrue($this->_getLdap()->exists($dn));
  167. $entry=$this->_getLdap()->getEntry($dn);
  168. $this->_getLdap()->delete($dn);
  169. $this->assertEquals('mylocation1', $entry['l'][0]);
  170. } catch (Zend_Ldap_Exception $e) {
  171. if ($this->_getLdap()->exists($dn)) {
  172. $this->_getLdap()->delete($dn);
  173. }
  174. $this->fail($e->getMessage());
  175. }
  176. }
  177. public function testPrepareLdapEntryArray()
  178. {
  179. $data=array(
  180. 'a1' => 'TestCreated',
  181. 'a2' => 'account',
  182. 'a3' => null,
  183. 'a4' => '',
  184. 'a5' => array('TestCreated'),
  185. 'a6' => array('account'),
  186. 'a7' => array(null),
  187. 'a8' => array(''),
  188. 'a9' => array('', null, 'account', '', null, 'TestCreated', '', null));
  189. Zend_Ldap::prepareLdapEntryArray($data);
  190. $expected=array(
  191. 'a1' => array('TestCreated'),
  192. 'a2' => array('account'),
  193. 'a3' => array(),
  194. 'a4' => array(),
  195. 'a5' => array('TestCreated'),
  196. 'a6' => array('account'),
  197. 'a7' => array(),
  198. 'a8' => array(),
  199. 'a9' => array('account', 'TestCreated'));
  200. $this->assertEquals($expected, $data);
  201. }
  202. /**
  203. * @expectedException InvalidArgumentException
  204. */
  205. public function testPrepareLdapEntryArrayArrayData()
  206. {
  207. $data=array(
  208. 'a1' => array(array('account')));
  209. Zend_Ldap::prepareLdapEntryArray($data);
  210. }
  211. /**
  212. * @expectedException InvalidArgumentException
  213. */
  214. public function testPrepareLdapEntryArrayObjectData()
  215. {
  216. $class=new stdClass();
  217. $class->a='b';
  218. $data=array(
  219. 'a1' => array($class));
  220. Zend_Ldap::prepareLdapEntryArray($data);
  221. }
  222. public function testAddWithDnObject()
  223. {
  224. $dn=Zend_Ldap_Dn::fromString($this->_createDn('ou=TestCreated,'));
  225. $data=array(
  226. 'ou' => 'TestCreated',
  227. 'objectclass' => 'organizationalUnit'
  228. );
  229. try {
  230. $this->_getLdap()->add($dn, $data);
  231. $this->assertEquals(1, $this->_getLdap()->count('ou=TestCreated'));
  232. $this->_getLdap()->delete($dn);
  233. }
  234. catch (Zend_Ldap_Exception $e) {
  235. $this->fail($e->getMessage());
  236. }
  237. }
  238. public function testUpdateWithDnObject()
  239. {
  240. $dn=Zend_Ldap_Dn::fromString($this->_createDn('ou=TestCreated,'));
  241. $data=array(
  242. 'ou' => 'TestCreated',
  243. 'l' => 'mylocation1',
  244. 'objectclass' => 'organizationalUnit'
  245. );
  246. try {
  247. $this->_getLdap()->add($dn, $data);
  248. $entry=$this->_getLdap()->getEntry($dn);
  249. $this->assertEquals('mylocation1', $entry['l'][0]);
  250. $entry['l']='mylocation2';
  251. $this->_getLdap()->update($dn, $entry);
  252. $entry=$this->_getLdap()->getEntry($dn);
  253. $this->_getLdap()->delete($dn);
  254. $this->assertEquals('mylocation2', $entry['l'][0]);
  255. }
  256. catch (Zend_Ldap_Exception $e) {
  257. $this->fail($e->getMessage());
  258. }
  259. }
  260. public function testSaveWithDnObject()
  261. {
  262. $dn=Zend_Ldap_Dn::fromString($this->_createDn('ou=TestCreated,'));
  263. $data=array('ou' => 'TestCreated', 'objectclass' => 'organizationalUnit');
  264. try {
  265. $this->_getLdap()->save($dn, $data);
  266. $this->assertTrue($this->_getLdap()->exists($dn));
  267. $data['l']='mylocation1';
  268. $this->_getLdap()->save($dn, $data);
  269. $this->assertTrue($this->_getLdap()->exists($dn));
  270. $entry=$this->_getLdap()->getEntry($dn);
  271. $this->_getLdap()->delete($dn);
  272. $this->assertEquals('mylocation1', $entry['l'][0]);
  273. } catch (Zend_Ldap_Exception $e) {
  274. if ($this->_getLdap()->exists($dn)) {
  275. $this->_getLdap()->delete($dn);
  276. }
  277. $this->fail($e->getMessage());
  278. }
  279. }
  280. public function testAddObjectClass()
  281. {
  282. $dn=$this->_createDn('ou=TestCreated,');
  283. $data=array(
  284. 'ou' => 'TestCreated',
  285. 'l' => 'mylocation1',
  286. 'objectClass' => 'organizationalUnit'
  287. );
  288. try {
  289. $this->_getLdap()->add($dn, $data);
  290. $entry=$this->_getLdap()->getEntry($dn);
  291. $entry['objectclass'][]='domainRelatedObject';
  292. $entry['associatedDomain'][]='domain';
  293. $this->_getLdap()->update($dn, $entry);
  294. $entry=$this->_getLdap()->getEntry($dn);
  295. $this->_getLdap()->delete($dn);
  296. $this->assertEquals('domain', $entry['associateddomain'][0]);
  297. $this->assertContains('organizationalUnit', $entry['objectclass']);
  298. $this->assertContains('domainRelatedObject', $entry['objectclass']);
  299. } catch (Zend_Ldap_Exception $e) {
  300. if ($this->_getLdap()->exists($dn)) {
  301. $this->_getLdap()->delete($dn);
  302. }
  303. $this->fail($e->getMessage());
  304. }
  305. }
  306. public function testRemoveObjectClass()
  307. {
  308. $dn=$this->_createDn('ou=TestCreated,');
  309. $data=array(
  310. 'associatedDomain' => 'domain',
  311. 'ou' => 'TestCreated',
  312. 'l' => 'mylocation1',
  313. 'objectClass' => array('organizationalUnit', 'domainRelatedObject')
  314. );
  315. try {
  316. $this->_getLdap()->add($dn, $data);
  317. $entry=$this->_getLdap()->getEntry($dn);
  318. $entry['objectclass']='organizationalUnit';
  319. $entry['associatedDomain']=null;
  320. $this->_getLdap()->update($dn, $entry);
  321. $entry=$this->_getLdap()->getEntry($dn);
  322. $this->_getLdap()->delete($dn);
  323. $this->assertArrayNotHasKey('associateddomain', $entry);
  324. $this->assertContains('organizationalUnit', $entry['objectclass']);
  325. $this->assertNotContains('domainRelatedObject', $entry['objectclass']);
  326. } catch (Zend_Ldap_Exception $e) {
  327. if ($this->_getLdap()->exists($dn)) {
  328. $this->_getLdap()->delete($dn);
  329. }
  330. $this->fail($e->getMessage());
  331. }
  332. }
  333. }