| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509 |
- <?php
- /**
- * Zend Framework
- *
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://framework.zend.com/license/new-bsd
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@zend.com so we can send you a copy immediately.
- *
- * @category Zend
- * @package Zend_Ldap
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @version $Id$
- */
- /**
- * Zend_Ldap_OnlineTestCase
- */
- require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'OnlineTestCase.php';
- /**
- * @see Zend_Ldap_Dn
- */
- require_once 'Zend/Ldap/Dn.php';
- /**
- * @category Zend
- * @package Zend_Ldap
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @group Zend_Ldap
- */
- class Zend_Ldap_CrudTest extends Zend_Ldap_OnlineTestCase
- {
- public function testAddAndDelete()
- {
- $dn=$this->_createDn('ou=TestCreated,');
- $data=array(
- 'ou' => 'TestCreated',
- 'objectClass' => 'organizationalUnit'
- );
- try {
- $this->_getLdap()->add($dn, $data);
- $this->assertEquals(1, $this->_getLdap()->count('ou=TestCreated'));
- $this->_getLdap()->delete($dn);
- $this->assertEquals(0, $this->_getLdap()->count('ou=TestCreated'));
- } catch (Zend_Ldap_Exception $e) {
- if ($this->_getLdap()->exists($dn)) {
- $this->_getLdap()->delete($dn);
- }
- $this->fail($e->getMessage());
- }
- }
- public function testUpdate()
- {
- $dn=$this->_createDn('ou=TestCreated,');
- $data=array(
- 'ou' => 'TestCreated',
- 'l' => 'mylocation1',
- 'objectClass' => 'organizationalUnit'
- );
- try {
- $this->_getLdap()->add($dn, $data);
- $entry=$this->_getLdap()->getEntry($dn);
- $this->assertEquals('mylocation1', $entry['l'][0]);
- $entry['l']='mylocation2';
- $this->_getLdap()->update($dn, $entry);
- $entry=$this->_getLdap()->getEntry($dn);
- $this->_getLdap()->delete($dn);
- $this->assertEquals('mylocation2', $entry['l'][0]);
- } catch (Zend_Ldap_Exception $e) {
- if ($this->_getLdap()->exists($dn)) {
- $this->_getLdap()->delete($dn);
- }
- $this->fail($e->getMessage());
- }
- }
- /**
- * @expectedException Zend_Ldap_Exception
- */
- public function testIllegalAdd()
- {
- $dn=$this->_createDn('ou=TestCreated,ou=Node2,');
- $data=array(
- 'ou' => 'TestCreated',
- 'objectClass' => 'organizationalUnit'
- );
- $this->_getLdap()->add($dn, $data);
- $this->_getLdap()->delete($dn);
- }
- public function testIllegalUpdate()
- {
- $dn=$this->_createDn('ou=TestCreated,');
- $data=array(
- 'ou' => 'TestCreated',
- 'objectclass' => 'organizationalUnit'
- );
- try {
- $this->_getLdap()->add($dn, $data);
- $entry=$this->_getLdap()->getEntry($dn);
- $entry['objectclass'][]='inetOrgPerson';
- $exThrown=false;
- try {
- $this->_getLdap()->update($dn, $entry);
- }
- catch (Zend_Ldap_Exception $e) {
- $exThrown=true;
- }
- $this->_getLdap()->delete($dn);
- if (!$exThrown) $this->fail('no exception thrown while illegaly updating entry');
- }
- catch (Zend_Ldap_Exception $e) {
- $this->fail($e->getMessage());
- }
- }
- /**
- * @expectedException Zend_Ldap_Exception
- */
- public function testIllegalDelete()
- {
- $dn=$this->_createDn('ou=TestCreated,');
- $this->_getLdap()->delete($dn);
- }
- public function testDeleteRecursively()
- {
- $topDn=$this->_createDn('ou=RecursiveTest,');
- $dn=$topDn;
- $data=array('ou' => 'RecursiveTest', 'objectclass' => 'organizationalUnit'
- );
- $this->_getLdap()->add($dn, $data);
- for ($level=1; $level<=5; $level++) {
- $name='Level' . $level;
- $dn='ou=' . $name . ',' . $dn;
- $data=array('ou' => $name, 'objectclass' => 'organizationalUnit');
- $this->_getLdap()->add($dn, $data);
- for ($item=1; $item<=5; $item++) {
- $uid='Item' . $item;
- $idn='ou=' . $uid . ',' . $dn;
- $idata=array('ou' => $uid, 'objectclass' => 'organizationalUnit');
- $this->_getLdap()->add($idn, $idata);
- }
- }
- $exCaught=false;
- try {
- $this->_getLdap()->delete($topDn, false);
- } catch (Zend_Ldap_Exception $e) {
- $exCaught=true;
- }
- $this->assertTrue($exCaught,
- 'Execption not raised when deleting item with children without specifiying recursive delete');
- $this->_getLdap()->delete($topDn, true);
- $this->assertFalse($this->_getLdap()->exists($topDn));
- }
- public function testSave()
- {
- $dn=$this->_createDn('ou=TestCreated,');
- $data=array('ou' => 'TestCreated', 'objectclass' => 'organizationalUnit');
- try {
- $this->_getLdap()->save($dn, $data);
- $this->assertTrue($this->_getLdap()->exists($dn));
- $data['l']='mylocation1';
- $this->_getLdap()->save($dn, $data);
- $this->assertTrue($this->_getLdap()->exists($dn));
- $entry=$this->_getLdap()->getEntry($dn);
- $this->_getLdap()->delete($dn);
- $this->assertEquals('mylocation1', $entry['l'][0]);
- } catch (Zend_Ldap_Exception $e) {
- if ($this->_getLdap()->exists($dn)) {
- $this->_getLdap()->delete($dn);
- }
- $this->fail($e->getMessage());
- }
- }
- public function testPrepareLdapEntryArray()
- {
- $data=array(
- 'a1' => 'TestCreated',
- 'a2' => 'account',
- 'a3' => null,
- 'a4' => '',
- 'a5' => array('TestCreated'),
- 'a6' => array('account'),
- 'a7' => array(null),
- 'a8' => array(''),
- 'a9' => array('', null, 'account', '', null, 'TestCreated', '', null));
- Zend_Ldap::prepareLdapEntryArray($data);
- $expected=array(
- 'a1' => array('TestCreated'),
- 'a2' => array('account'),
- 'a3' => array(),
- 'a4' => array(),
- 'a5' => array('TestCreated'),
- 'a6' => array('account'),
- 'a7' => array(),
- 'a8' => array(),
- 'a9' => array('account', 'TestCreated'));
- $this->assertEquals($expected, $data);
- }
- /**
- * @group ZF-7888
- */
- public function testZeroValueMakesItThroughSanitationProcess()
- {
- $data = array(
- 'string' => '0',
- 'integer' => 0,
- 'stringArray' => array('0'),
- 'integerArray' => array(0),
- 'null' => null,
- 'empty' => '',
- 'nullArray' => array(null),
- 'emptyArray' => array(''),
- );
- Zend_Ldap::prepareLdapEntryArray($data);
- $expected=array(
- 'string' => array('0'),
- 'integer' => array('0'),
- 'stringarray' => array('0'),
- 'integerarray' => array('0'),
- 'null' => array(),
- 'empty' => array(),
- 'nullarray' => array(),
- 'emptyarray' => array()
- );
- $this->assertEquals($expected, $data);
- }
- /**
- * @expectedException InvalidArgumentException
- */
- public function testPrepareLdapEntryArrayArrayData()
- {
- $data=array(
- 'a1' => array(array('account')));
- Zend_Ldap::prepareLdapEntryArray($data);
- }
- /**
- * @expectedException InvalidArgumentException
- */
- public function testPrepareLdapEntryArrayObjectData()
- {
- $class=new stdClass();
- $class->a='b';
- $data=array(
- 'a1' => array($class));
- Zend_Ldap::prepareLdapEntryArray($data);
- }
- public function testAddWithDnObject()
- {
- $dn=Zend_Ldap_Dn::fromString($this->_createDn('ou=TestCreated,'));
- $data=array(
- 'ou' => 'TestCreated',
- 'objectclass' => 'organizationalUnit'
- );
- try {
- $this->_getLdap()->add($dn, $data);
- $this->assertEquals(1, $this->_getLdap()->count('ou=TestCreated'));
- $this->_getLdap()->delete($dn);
- }
- catch (Zend_Ldap_Exception $e) {
- $this->fail($e->getMessage());
- }
- }
- public function testUpdateWithDnObject()
- {
- $dn=Zend_Ldap_Dn::fromString($this->_createDn('ou=TestCreated,'));
- $data=array(
- 'ou' => 'TestCreated',
- 'l' => 'mylocation1',
- 'objectclass' => 'organizationalUnit'
- );
- try {
- $this->_getLdap()->add($dn, $data);
- $entry=$this->_getLdap()->getEntry($dn);
- $this->assertEquals('mylocation1', $entry['l'][0]);
- $entry['l']='mylocation2';
- $this->_getLdap()->update($dn, $entry);
- $entry=$this->_getLdap()->getEntry($dn);
- $this->_getLdap()->delete($dn);
- $this->assertEquals('mylocation2', $entry['l'][0]);
- }
- catch (Zend_Ldap_Exception $e) {
- $this->fail($e->getMessage());
- }
- }
- public function testSaveWithDnObject()
- {
- $dn=Zend_Ldap_Dn::fromString($this->_createDn('ou=TestCreated,'));
- $data=array('ou' => 'TestCreated', 'objectclass' => 'organizationalUnit');
- try {
- $this->_getLdap()->save($dn, $data);
- $this->assertTrue($this->_getLdap()->exists($dn));
- $data['l']='mylocation1';
- $this->_getLdap()->save($dn, $data);
- $this->assertTrue($this->_getLdap()->exists($dn));
- $entry=$this->_getLdap()->getEntry($dn);
- $this->_getLdap()->delete($dn);
- $this->assertEquals('mylocation1', $entry['l'][0]);
- } catch (Zend_Ldap_Exception $e) {
- if ($this->_getLdap()->exists($dn)) {
- $this->_getLdap()->delete($dn);
- }
- $this->fail($e->getMessage());
- }
- }
- public function testAddObjectClass()
- {
- $dn=$this->_createDn('ou=TestCreated,');
- $data=array(
- 'ou' => 'TestCreated',
- 'l' => 'mylocation1',
- 'objectClass' => 'organizationalUnit'
- );
- try {
- $this->_getLdap()->add($dn, $data);
- $entry=$this->_getLdap()->getEntry($dn);
- $entry['objectclass'][]='domainRelatedObject';
- $entry['associatedDomain'][]='domain';
- $this->_getLdap()->update($dn, $entry);
- $entry=$this->_getLdap()->getEntry($dn);
- $this->_getLdap()->delete($dn);
- $this->assertEquals('domain', $entry['associateddomain'][0]);
- $this->assertContains('organizationalUnit', $entry['objectclass']);
- $this->assertContains('domainRelatedObject', $entry['objectclass']);
- } catch (Zend_Ldap_Exception $e) {
- if ($this->_getLdap()->exists($dn)) {
- $this->_getLdap()->delete($dn);
- }
- $this->fail($e->getMessage());
- }
- }
- public function testRemoveObjectClass()
- {
- $dn=$this->_createDn('ou=TestCreated,');
- $data=array(
- 'associatedDomain' => 'domain',
- 'ou' => 'TestCreated',
- 'l' => 'mylocation1',
- 'objectClass' => array('organizationalUnit', 'domainRelatedObject')
- );
- try {
- $this->_getLdap()->add($dn, $data);
- $entry=$this->_getLdap()->getEntry($dn);
- $entry['objectclass']='organizationalUnit';
- $entry['associatedDomain']=null;
- $this->_getLdap()->update($dn, $entry);
- $entry=$this->_getLdap()->getEntry($dn);
- $this->_getLdap()->delete($dn);
- $this->assertArrayNotHasKey('associateddomain', $entry);
- $this->assertContains('organizationalUnit', $entry['objectclass']);
- $this->assertNotContains('domainRelatedObject', $entry['objectclass']);
- } catch (Zend_Ldap_Exception $e) {
- if ($this->_getLdap()->exists($dn)) {
- $this->_getLdap()->delete($dn);
- }
- $this->fail($e->getMessage());
- }
- }
- /**
- * @group ZF-9564
- */
- public function testAddingEntryWithMissingRdnAttribute() {
- $dn = $this->_createDn('ou=TestCreated,');
- $data = array(
- 'objectClass' => array('organizationalUnit')
- );
- try {
- $this->_getLdap()->add($dn, $data);
- $entry = $this->_getLdap()->getEntry($dn);
- $this->_getLdap()->delete($dn);
- $this->assertEquals(array('TestCreated'), $entry['ou']);
- } catch (Zend_Ldap_Exception $e) {
- if ($this->_getLdap()->exists($dn)) {
- $this->_getLdap()->delete($dn);
- }
- $this->fail($e->getMessage());
- }
- }
- /**
- * @group ZF-9564
- */
- public function testAddingEntryWithMissingRdnAttributeValue() {
- $dn = $this->_createDn('ou=TestCreated,');
- $data = array(
- 'ou' => array('SecondOu'),
- 'objectClass' => array('organizationalUnit')
- );
- try {
- $this->_getLdap()->add($dn, $data);
- $entry = $this->_getLdap()->getEntry($dn);
- $this->_getLdap()->delete($dn);
- $this->assertEquals(array('TestCreated', 'SecondOu'), $entry['ou']);
- } catch (Zend_Ldap_Exception $e) {
- if ($this->_getLdap()->exists($dn)) {
- $this->_getLdap()->delete($dn);
- }
- $this->fail($e->getMessage());
- }
- }
- /**
- * @group ZF-9564
- */
- public function testAddingEntryThatHasMultipleValuesOnRdnAttribute() {
- $dn = $this->_createDn('ou=TestCreated,');
- $data = array(
- 'ou' => array('TestCreated', 'SecondOu'),
- 'objectClass' => array('organizationalUnit')
- );
- try {
- $this->_getLdap()->add($dn, $data);
- $entry = $this->_getLdap()->getEntry($dn);
- $this->_getLdap()->delete($dn);
- $this->assertEquals(array('TestCreated', 'SecondOu'), $entry['ou']);
- } catch (Zend_Ldap_Exception $e) {
- if ($this->_getLdap()->exists($dn)) {
- $this->_getLdap()->delete($dn);
- }
- $this->fail($e->getMessage());
- }
- }
- /**
- * @group ZF-9564
- */
- public function testUpdatingEntryWithAttributeThatIsAnRdnAttribute() {
- $dn = $this->_createDn('ou=TestCreated,');
- $data = array(
- 'ou' => array('TestCreated'),
- 'objectClass' => array('organizationalUnit')
- );
- try {
- $this->_getLdap()->add($dn, $data);
- $entry = $this->_getLdap()->getEntry($dn);
- $data = array('ou' => array_merge($entry['ou'], array('SecondOu')));
- $this->_getLdap()->update($dn, $data);
- $entry = $this->_getLdap()->getEntry($dn);
- $this->_getLdap()->delete($dn);
- $this->assertEquals(array('TestCreated', 'SecondOu'), $entry['ou']);
- } catch (Zend_Ldap_Exception $e) {
- if ($this->_getLdap()->exists($dn)) {
- $this->_getLdap()->delete($dn);
- }
- $this->fail($e->getMessage());
- }
- }
- /**
- * @group ZF-9564
- */
- public function testUpdatingEntryWithRdnAttributeValueMissingInData() {
- $dn = $this->_createDn('ou=TestCreated,');
- $data = array(
- 'ou' => array('TestCreated'),
- 'objectClass' => array('organizationalUnit')
- );
- try {
- $this->_getLdap()->add($dn, $data);
- $entry = $this->_getLdap()->getEntry($dn);
- $data = array('ou' => 'SecondOu');
- $this->_getLdap()->update($dn, $data);
- $entry = $this->_getLdap()->getEntry($dn);
- $this->_getLdap()->delete($dn);
- $this->assertEquals(array('TestCreated', 'SecondOu'), $entry['ou']);
- } catch (Zend_Ldap_Exception $e) {
- if ($this->_getLdap()->exists($dn)) {
- $this->_getLdap()->delete($dn);
- }
- $this->fail($e->getMessage());
- }
- }
- }
|