CrudTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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-2015 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-2015 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. * @group ZF-7888
  206. */
  207. public function testZeroValueMakesItThroughSanitationProcess()
  208. {
  209. $data = array(
  210. 'string' => '0',
  211. 'integer' => 0,
  212. 'stringArray' => array('0'),
  213. 'integerArray' => array(0),
  214. 'null' => null,
  215. 'empty' => '',
  216. 'nullArray' => array(null),
  217. 'emptyArray' => array(''),
  218. );
  219. Zend_Ldap::prepareLdapEntryArray($data);
  220. $expected=array(
  221. 'string' => array('0'),
  222. 'integer' => array('0'),
  223. 'stringarray' => array('0'),
  224. 'integerarray' => array('0'),
  225. 'null' => array(),
  226. 'empty' => array(),
  227. 'nullarray' => array(),
  228. 'emptyarray' => array()
  229. );
  230. $this->assertEquals($expected, $data);
  231. }
  232. /**
  233. * @expectedException InvalidArgumentException
  234. */
  235. public function testPrepareLdapEntryArrayArrayData()
  236. {
  237. $data=array(
  238. 'a1' => array(array('account')));
  239. Zend_Ldap::prepareLdapEntryArray($data);
  240. }
  241. /**
  242. * @expectedException InvalidArgumentException
  243. */
  244. public function testPrepareLdapEntryArrayObjectData()
  245. {
  246. $class=new stdClass();
  247. $class->a='b';
  248. $data=array(
  249. 'a1' => array($class));
  250. Zend_Ldap::prepareLdapEntryArray($data);
  251. }
  252. public function testAddWithDnObject()
  253. {
  254. $dn=Zend_Ldap_Dn::fromString($this->_createDn('ou=TestCreated,'));
  255. $data=array(
  256. 'ou' => 'TestCreated',
  257. 'objectclass' => 'organizationalUnit'
  258. );
  259. try {
  260. $this->_getLdap()->add($dn, $data);
  261. $this->assertEquals(1, $this->_getLdap()->count('ou=TestCreated'));
  262. $this->_getLdap()->delete($dn);
  263. }
  264. catch (Zend_Ldap_Exception $e) {
  265. $this->fail($e->getMessage());
  266. }
  267. }
  268. public function testUpdateWithDnObject()
  269. {
  270. $dn=Zend_Ldap_Dn::fromString($this->_createDn('ou=TestCreated,'));
  271. $data=array(
  272. 'ou' => 'TestCreated',
  273. 'l' => 'mylocation1',
  274. 'objectclass' => 'organizationalUnit'
  275. );
  276. try {
  277. $this->_getLdap()->add($dn, $data);
  278. $entry=$this->_getLdap()->getEntry($dn);
  279. $this->assertEquals('mylocation1', $entry['l'][0]);
  280. $entry['l']='mylocation2';
  281. $this->_getLdap()->update($dn, $entry);
  282. $entry=$this->_getLdap()->getEntry($dn);
  283. $this->_getLdap()->delete($dn);
  284. $this->assertEquals('mylocation2', $entry['l'][0]);
  285. }
  286. catch (Zend_Ldap_Exception $e) {
  287. $this->fail($e->getMessage());
  288. }
  289. }
  290. public function testSaveWithDnObject()
  291. {
  292. $dn=Zend_Ldap_Dn::fromString($this->_createDn('ou=TestCreated,'));
  293. $data=array('ou' => 'TestCreated', 'objectclass' => 'organizationalUnit');
  294. try {
  295. $this->_getLdap()->save($dn, $data);
  296. $this->assertTrue($this->_getLdap()->exists($dn));
  297. $data['l']='mylocation1';
  298. $this->_getLdap()->save($dn, $data);
  299. $this->assertTrue($this->_getLdap()->exists($dn));
  300. $entry=$this->_getLdap()->getEntry($dn);
  301. $this->_getLdap()->delete($dn);
  302. $this->assertEquals('mylocation1', $entry['l'][0]);
  303. } catch (Zend_Ldap_Exception $e) {
  304. if ($this->_getLdap()->exists($dn)) {
  305. $this->_getLdap()->delete($dn);
  306. }
  307. $this->fail($e->getMessage());
  308. }
  309. }
  310. public function testAddObjectClass()
  311. {
  312. $dn=$this->_createDn('ou=TestCreated,');
  313. $data=array(
  314. 'ou' => 'TestCreated',
  315. 'l' => 'mylocation1',
  316. 'objectClass' => 'organizationalUnit'
  317. );
  318. try {
  319. $this->_getLdap()->add($dn, $data);
  320. $entry=$this->_getLdap()->getEntry($dn);
  321. $entry['objectclass'][]='domainRelatedObject';
  322. $entry['associatedDomain'][]='domain';
  323. $this->_getLdap()->update($dn, $entry);
  324. $entry=$this->_getLdap()->getEntry($dn);
  325. $this->_getLdap()->delete($dn);
  326. $this->assertEquals('domain', $entry['associateddomain'][0]);
  327. $this->assertContains('organizationalUnit', $entry['objectclass']);
  328. $this->assertContains('domainRelatedObject', $entry['objectclass']);
  329. } catch (Zend_Ldap_Exception $e) {
  330. if ($this->_getLdap()->exists($dn)) {
  331. $this->_getLdap()->delete($dn);
  332. }
  333. $this->fail($e->getMessage());
  334. }
  335. }
  336. public function testRemoveObjectClass()
  337. {
  338. $dn=$this->_createDn('ou=TestCreated,');
  339. $data=array(
  340. 'associatedDomain' => 'domain',
  341. 'ou' => 'TestCreated',
  342. 'l' => 'mylocation1',
  343. 'objectClass' => array('organizationalUnit', 'domainRelatedObject')
  344. );
  345. try {
  346. $this->_getLdap()->add($dn, $data);
  347. $entry=$this->_getLdap()->getEntry($dn);
  348. $entry['objectclass']='organizationalUnit';
  349. $entry['associatedDomain']=null;
  350. $this->_getLdap()->update($dn, $entry);
  351. $entry=$this->_getLdap()->getEntry($dn);
  352. $this->_getLdap()->delete($dn);
  353. $this->assertArrayNotHasKey('associateddomain', $entry);
  354. $this->assertContains('organizationalUnit', $entry['objectclass']);
  355. $this->assertNotContains('domainRelatedObject', $entry['objectclass']);
  356. } catch (Zend_Ldap_Exception $e) {
  357. if ($this->_getLdap()->exists($dn)) {
  358. $this->_getLdap()->delete($dn);
  359. }
  360. $this->fail($e->getMessage());
  361. }
  362. }
  363. /**
  364. * @group ZF-9564
  365. */
  366. public function testAddingEntryWithMissingRdnAttribute() {
  367. $dn = $this->_createDn('ou=TestCreated,');
  368. $data = array(
  369. 'objectClass' => array('organizationalUnit')
  370. );
  371. try {
  372. $this->_getLdap()->add($dn, $data);
  373. $entry = $this->_getLdap()->getEntry($dn);
  374. $this->_getLdap()->delete($dn);
  375. $this->assertEquals(array('TestCreated'), $entry['ou']);
  376. } catch (Zend_Ldap_Exception $e) {
  377. if ($this->_getLdap()->exists($dn)) {
  378. $this->_getLdap()->delete($dn);
  379. }
  380. $this->fail($e->getMessage());
  381. }
  382. }
  383. /**
  384. * @group ZF-9564
  385. */
  386. public function testAddingEntryWithMissingRdnAttributeValue() {
  387. $dn = $this->_createDn('ou=TestCreated,');
  388. $data = array(
  389. 'ou' => array('SecondOu'),
  390. 'objectClass' => array('organizationalUnit')
  391. );
  392. try {
  393. $this->_getLdap()->add($dn, $data);
  394. $entry = $this->_getLdap()->getEntry($dn);
  395. $this->_getLdap()->delete($dn);
  396. $this->assertEquals(array('TestCreated', 'SecondOu'), $entry['ou']);
  397. } catch (Zend_Ldap_Exception $e) {
  398. if ($this->_getLdap()->exists($dn)) {
  399. $this->_getLdap()->delete($dn);
  400. }
  401. $this->fail($e->getMessage());
  402. }
  403. }
  404. /**
  405. * @group ZF-9564
  406. */
  407. public function testAddingEntryThatHasMultipleValuesOnRdnAttribute() {
  408. $dn = $this->_createDn('ou=TestCreated,');
  409. $data = array(
  410. 'ou' => array('TestCreated', 'SecondOu'),
  411. 'objectClass' => array('organizationalUnit')
  412. );
  413. try {
  414. $this->_getLdap()->add($dn, $data);
  415. $entry = $this->_getLdap()->getEntry($dn);
  416. $this->_getLdap()->delete($dn);
  417. $this->assertEquals(array('TestCreated', 'SecondOu'), $entry['ou']);
  418. } catch (Zend_Ldap_Exception $e) {
  419. if ($this->_getLdap()->exists($dn)) {
  420. $this->_getLdap()->delete($dn);
  421. }
  422. $this->fail($e->getMessage());
  423. }
  424. }
  425. /**
  426. * @group ZF-9564
  427. */
  428. public function testUpdatingEntryWithAttributeThatIsAnRdnAttribute() {
  429. $dn = $this->_createDn('ou=TestCreated,');
  430. $data = array(
  431. 'ou' => array('TestCreated'),
  432. 'objectClass' => array('organizationalUnit')
  433. );
  434. try {
  435. $this->_getLdap()->add($dn, $data);
  436. $entry = $this->_getLdap()->getEntry($dn);
  437. $data = array('ou' => array_merge($entry['ou'], array('SecondOu')));
  438. $this->_getLdap()->update($dn, $data);
  439. $entry = $this->_getLdap()->getEntry($dn);
  440. $this->_getLdap()->delete($dn);
  441. $this->assertEquals(array('TestCreated', 'SecondOu'), $entry['ou']);
  442. } catch (Zend_Ldap_Exception $e) {
  443. if ($this->_getLdap()->exists($dn)) {
  444. $this->_getLdap()->delete($dn);
  445. }
  446. $this->fail($e->getMessage());
  447. }
  448. }
  449. /**
  450. * @group ZF-9564
  451. */
  452. public function testUpdatingEntryWithRdnAttributeValueMissingInData() {
  453. $dn = $this->_createDn('ou=TestCreated,');
  454. $data = array(
  455. 'ou' => array('TestCreated'),
  456. 'objectClass' => array('organizationalUnit')
  457. );
  458. try {
  459. $this->_getLdap()->add($dn, $data);
  460. $entry = $this->_getLdap()->getEntry($dn);
  461. $data = array('ou' => 'SecondOu');
  462. $this->_getLdap()->update($dn, $data);
  463. $entry = $this->_getLdap()->getEntry($dn);
  464. $this->_getLdap()->delete($dn);
  465. $this->assertEquals(array('TestCreated', 'SecondOu'), $entry['ou']);
  466. } catch (Zend_Ldap_Exception $e) {
  467. if ($this->_getLdap()->exists($dn)) {
  468. $this->_getLdap()->delete($dn);
  469. }
  470. $this->fail($e->getMessage());
  471. }
  472. }
  473. }