From 1a6768be7adab331e2149a808c431ab54daa035d Mon Sep 17 00:00:00 2001 From: Gunnar Wrobel Date: Sun, 21 Feb 2010 10:00:49 +0100 Subject: Provide an add method to the Ldap class and add unit testing for it. The web admin uses ldap_add() directly in the views at the moment. This should later be refactored to use the add() method in the library. --- lib/KolabAdmin/Ldap.php | 4 +++ test/KolabAdmin/Unit/BaseTest.php | 53 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/lib/KolabAdmin/Ldap.php b/lib/KolabAdmin/Ldap.php index a839458..cf4213f 100644 --- a/lib/KolabAdmin/Ldap.php +++ b/lib/KolabAdmin/Ldap.php @@ -176,6 +176,10 @@ class KolabLDAP { return $ldap_object; } + function add( $dn, $attr ) { + return ldap_add($this->connection, $dn, $attr); + } + function search( $base, $filter, $attrs = false ) { $this->freeSearchResult(); if( $attrs ) { diff --git a/test/KolabAdmin/Unit/BaseTest.php b/test/KolabAdmin/Unit/BaseTest.php index 9ee2d41..d75f49b 100644 --- a/test/KolabAdmin/Unit/BaseTest.php +++ b/test/KolabAdmin/Unit/BaseTest.php @@ -36,14 +36,63 @@ class KolabAdmin_Unit_BaseTest extends PHPUnit_Framework_TestCase { $_SESSION = array(); require '/kolab/etc/kolab/session_vars.php'; + $this->ldap = new KolabLdap(); + $this->ldap->bind( + 'cn=manager,cn=internal,' . $_SESSION['base_dn'], + 'test' + ); + $this->cleanup = array(); + } + + public function tearDown() + { + foreach ($this->cleanup as $dn) { + if (!$this->ldap->deleteObject($dn, true)) { + throw new Exception('Deleting ' . $dn . ' failed!'); + } + } } public function testCountmailReturnsZeroOnNonExistantMail() { - $ldap = new KolabLdap(); $this->assertEquals( 0, - $ldap->countMail($_SESSION['base_dn'], 'certainly@does@not@exist') + $this->ldap->countMail( + $_SESSION['base_dn'], + 'certainly@does@not@exist' + ) + ); + } + + public function testAddingObjectIsSuccessful() + { + $this->_add($this->_getTestUser()); + } + + private function _add($object) + { + if ($this->ldap->add($object['dn'], $object['attributes'])) { + $this->cleanup[] = $object['dn']; + } else { + throw new Exception('Adding ' . $object['dn'] . ' failed!'); + } + } + + private function _getTestUser($id = null) + { + return array( + 'dn' => 'cn=KolabAdmin TestUser' . $id . ',' . $_SESSION['base_dn'], + 'attributes' => array( + 'objectClass' => array( + 'top', 'inetOrgPerson', 'kolabInetOrgPerson' + ), + 'userPassword' => 'test', + 'sn' => 'TestUser' . $id, + 'cn' => 'KolabAdmin TestUser' . $id, + 'givenName' => 'KolabAdmin', + 'mail' => 'kolabadmin.test.' . $id . $_SESSION['base_dn'], + 'uid' => 'kolabadmin.test.' . $id, + ) ); } } -- cgit v0.12