CrudTest.php 12 KB

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