From ec0f80fee927ca572671ac7c733b960fb027e91f Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Wed, 5 Nov 2014 13:05:07 +0100 Subject: Split mapping from Access and Helper classes into it's own. Fully test them, too. remove unused methods split mapping methods off from Access class fix DB query handling move 'clear mapping' methods from static helper to new mapping class add tests test directly with DB finishing tests and fix return value from setDNbyUUID add corresponding class for groups and make abstract test class neutral. helper tests is now obsolete as the tested functions were moved to the new mapper class. add missing info to PHPDoc add unmap method fix namespaces fix test inheritance PHPDoc and a small code restructure for scrutinizer, no effective changes PostgreSQL does not accept LIMIT in DELETE queries phpdoc fixes, no code changes --- apps/user_ldap/tests/helper.php | 31 ---- .../tests/mapping/abstractmappingtest.php | 194 +++++++++++++++++++++ apps/user_ldap/tests/mapping/groupmapping.php | 17 ++ apps/user_ldap/tests/mapping/usermapping.php | 17 ++ 4 files changed, 228 insertions(+), 31 deletions(-) delete mode 100644 apps/user_ldap/tests/helper.php create mode 100644 apps/user_ldap/tests/mapping/abstractmappingtest.php create mode 100644 apps/user_ldap/tests/mapping/groupmapping.php create mode 100644 apps/user_ldap/tests/mapping/usermapping.php (limited to 'apps/user_ldap/tests') diff --git a/apps/user_ldap/tests/helper.php b/apps/user_ldap/tests/helper.php deleted file mode 100644 index a70a57051c8..00000000000 --- a/apps/user_ldap/tests/helper.php +++ /dev/null @@ -1,31 +0,0 @@ -execute(array('db01', 'oc1', '000-0000-0000')); - $statement->execute(array('db02', 'oc2', '000-0000-0001')); - - $statement = \OCP\DB::prepare('SELECT count(*) FROM `*PREFIX*ldap_user_mapping`'); - $result = $statement->execute(); - $this->assertEquals(2, $result->fetchOne()); - - Helper::clearMapping('user'); - - $result = $statement->execute(); - $this->assertEquals(0, $result->fetchOne()); - } -} diff --git a/apps/user_ldap/tests/mapping/abstractmappingtest.php b/apps/user_ldap/tests/mapping/abstractmappingtest.php new file mode 100644 index 00000000000..a5cb62253af --- /dev/null +++ b/apps/user_ldap/tests/mapping/abstractmappingtest.php @@ -0,0 +1,194 @@ + +* This file is licensed under the Affero General Public License version 3 or +* later. +* See the COPYING-README file. +*/ + +namespace OCA\user_ldap\tests\mapping; + +abstract class AbstractMappingTest extends \Test\TestCase { + abstract public function getMapper(\OCP\IDBConnection $dbMock); + + /** + * kiss test on isColNameValid + */ + public function testIsColNameValid() { + $dbMock = $this->getMock('\OCP\IDBConnection'); + $mapper = $this->getMapper($dbMock); + + $this->assertTrue($mapper->isColNameValid('ldap_dn')); + $this->assertFalse($mapper->isColNameValid('foobar')); + } + + /** + * returns an array of test entries with dn, name and uuid as keys + * @return array + */ + protected function getTestData() { + $data = array( + array( + 'dn' => 'uid=foobar,dc=example,dc=org', + 'name' => 'Foobar', + 'uuid' => '1111-AAAA-1234-CDEF', + ), + array( + 'dn' => 'uid=barfoo,dc=example,dc=org', + 'name' => 'Barfoo', + 'uuid' => '2222-BBBB-1234-CDEF', + ), + array( + 'dn' => 'uid=barabara,dc=example,dc=org', + 'name' => 'BaraBara', + 'uuid' => '3333-CCCC-1234-CDEF', + ) + ); + + return $data; + } + + /** + * calls map() on the given mapper and asserts result for true + * @param \OCA\User_LDAP\Mapping\AbstractMapping $mapper + * @param array $data + */ + protected function mapEntries($mapper, $data) { + foreach($data as $entry) { + $done = $mapper->map($entry['dn'], $entry['name'], $entry['uuid']); + $this->assertTrue($done); + } + } + + /** + * initalizes environment for a test run and returns an array with + * test objects. Preparing environment means that all mappings are cleared + * first and then filled with test entries. + * @return array 0 = \OCA\User_LDAP\Mapping\AbstractMapping, 1 = array of + * users or groups + */ + private function initTest() { + $dbc = \OC::$server->getDatabaseConnection(); + $mapper = $this->getMapper($dbc); + $data = $this->getTestData(); + // make sure DB is pristine, then fill it with test entries + $mapper->clear(); + $this->mapEntries($mapper, $data); + + return array($mapper, $data); + } + + /** + * tests map() method with input that should result in not-mapping. + * Hint: successful mapping is tested inherently with mapEntries(). + */ + public function testMap() { + list($mapper, $data) = $this->initTest(); + + // test that mapping will not happen when it shall not + $paramKeys = array('', 'dn', 'name', 'uuid'); + foreach($paramKeys as $key) { + $failEntry = $data[0]; + if(!empty($key)) { + $failEntry[$key] = 'do-not-get-mapped'; + } + $isMapped = $mapper->map($failEntry['dn'], $failEntry['name'], $failEntry['uuid']); + $this->assertFalse($isMapped); + } + } + + /** + * tests unmap() for both successfuly and not successful removing of + * mapping entries + */ + public function testUnmap() { + list($mapper, $data) = $this->initTest(); + + foreach($data as $entry) { + $result = $mapper->unmap($entry['name']); + $this->assertTrue($result); + } + + $result = $mapper->unmap('notAnEntry'); + $this->assertFalse($result); + } + + /** + * tests getDNByName(), getNameByDN() and getNameByUUID() for successful + * and unsuccessful requests. + */ + public function testGetMethods() { + list($mapper, $data) = $this->initTest(); + + foreach($data as $entry) { + $fdn = $mapper->getDNByName($entry['name']); + $this->assertSame($fdn, $entry['dn']); + } + $fdn = $mapper->getDNByName('nosuchname'); + $this->assertFalse($fdn); + + foreach($data as $entry) { + $name = $mapper->getNameByDN($entry['dn']); + $this->assertSame($name, $entry['name']); + } + $name = $mapper->getNameByDN('nosuchdn'); + $this->assertFalse($name); + + foreach($data as $entry) { + $name = $mapper->getNameByUUID($entry['uuid']); + $this->assertSame($name, $entry['name']); + } + $name = $mapper->getNameByUUID('nosuchuuid'); + $this->assertFalse($name); + } + + /** + * tests getNamesBySearch() for successful and unsuccessful requests. + */ + public function testSearch() { + list($mapper,) = $this->initTest(); + + $names = $mapper->getNamesBySearch('%oo%'); + $this->assertTrue(is_array($names)); + $this->assertSame(2, count($names)); + $this->assertTrue(in_array('Foobar', $names)); + $this->assertTrue(in_array('Barfoo', $names)); + $names = $mapper->getNamesBySearch('nada'); + $this->assertTrue(is_array($names)); + $this->assertSame(0, count($names)); + } + + /** + * tests setDNbyUUID() for successful and unsuccessful update. + */ + public function testSetMethod() { + list($mapper, $data) = $this->initTest(); + + $newDN = 'uid=modified,dc=example,dc=org'; + $done = $mapper->setDNbyUUID($newDN, $data[0]['uuid']); + $this->assertTrue($done); + $fdn = $mapper->getDNByName($data[0]['name']); + $this->assertSame($fdn, $newDN); + + $newDN = 'uid=notme,dc=example,dc=org'; + $done = $mapper->setDNbyUUID($newDN, 'iamnothere'); + $this->assertFalse($done); + $name = $mapper->getNameByDN($newDN); + $this->assertFalse($name); + + } + + /** + * tests clear() for successful update. + */ + public function testClear() { + list($mapper, $data) = $this->initTest(); + + $done = $mapper->clear(); + $this->assertTrue($done); + foreach($data as $entry) { + $name = $mapper->getNameByUUID($entry['uuid']); + $this->assertFalse($name); + } + } +} diff --git a/apps/user_ldap/tests/mapping/groupmapping.php b/apps/user_ldap/tests/mapping/groupmapping.php new file mode 100644 index 00000000000..11bb3f40e3a --- /dev/null +++ b/apps/user_ldap/tests/mapping/groupmapping.php @@ -0,0 +1,17 @@ + +* This file is licensed under the Affero General Public License version 3 or +* later. +* See the COPYING-README file. +*/ + +namespace OCA\user_ldap\tests\mapping; + +use OCA\User_LDAP\Mapping\GroupMapping; + +class Test_GroupMapping extends AbstractMappingTest { + public function getMapper(\OCP\IDBConnection $dbMock) { + return new GroupMapping($dbMock); + } +} diff --git a/apps/user_ldap/tests/mapping/usermapping.php b/apps/user_ldap/tests/mapping/usermapping.php new file mode 100644 index 00000000000..2debcecf397 --- /dev/null +++ b/apps/user_ldap/tests/mapping/usermapping.php @@ -0,0 +1,17 @@ + +* This file is licensed under the Affero General Public License version 3 or +* later. +* See the COPYING-README file. +*/ + +namespace OCA\user_ldap\tests\mapping; + +use OCA\User_LDAP\Mapping\UserMapping; + +class Test_UserMapping extends AbstractMappingTest { + public function getMapper(\OCP\IDBConnection $dbMock) { + return new UserMapping($dbMock); + } +} -- cgit v1.2.3