From 425c67d196ccf7bc32833467ccce4c2b13b0f7c3 Mon Sep 17 00:00:00 2001 From: Jan Schneider Date: Mon, 3 Jan 2011 15:20:41 +0100 Subject: [PATCH] Move backend independent tests to a base class. --- framework/Share/package.xml | 10 +- framework/Share/test/Horde/Share/SqlTest.php | 201 ++------------------ framework/Share/test/Horde/Share/TestBase.php | 257 ++++++++++++++++++++++++++ 3 files changed, 281 insertions(+), 187 deletions(-) create mode 100644 framework/Share/test/Horde/Share/TestBase.php diff --git a/framework/Share/package.xml b/framework/Share/package.xml index e40a1e810..a1d51c9d1 100644 --- a/framework/Share/package.xml +++ b/framework/Share/package.xml @@ -11,8 +11,8 @@ owns or has access to. chuck@horde.org yes - 2010-12-16 - + 2011-01-03 + 0.0.4 0.0.4 @@ -321,7 +321,9 @@ owns or has access to. + + @@ -448,7 +450,9 @@ owns or has access to. + + @@ -517,7 +521,7 @@ Initial release as a PEAR package beta beta - 2010-12-16 + 2011-01-03 LGPL * Converted to Horde 4 coding standards diff --git a/framework/Share/test/Horde/Share/SqlTest.php b/framework/Share/test/Horde/Share/SqlTest.php index 720a19136..3003fdc1e 100644 --- a/framework/Share/test/Horde/Share/SqlTest.php +++ b/framework/Share/test/Horde/Share/SqlTest.php @@ -2,7 +2,7 @@ /** * Prepare the test setup. */ -require_once dirname(__FILE__) . '/Autoload.php'; +require_once dirname(__FILE__) . '/TestBase.php'; /** * @author Jan Schneider @@ -12,17 +12,10 @@ require_once dirname(__FILE__) . '/Autoload.php'; * @copyright 2010 The Horde Project (http://www.horde.org/) * @license http://www.fsf.org/copyleft/lgpl.html LGPL */ -class Horde_Share_SqlTest extends PHPUnit_Framework_TestCase +class Horde_Share_SqlTest extends Horde_Share_TestBase { protected static $db; - protected static $share; - - public function testGetApp() - { - $this->assertEquals('test', self::$share->getApp()); - } - public function testSetTable() { $this->assertEquals('test_shares', self::$share->getTable()); @@ -39,9 +32,8 @@ class Horde_Share_SqlTest extends PHPUnit_Framework_TestCase public function testAddShare() { - $share = self::$share->newShare('john', 'myshare'); + $share = parent::baseAddShare(); $this->assertInstanceOf('Horde_Share_Object_Sql', $share); - self::$share->addShare($share); } /** @@ -49,39 +41,7 @@ class Horde_Share_SqlTest extends PHPUnit_Framework_TestCase */ public function testPermissions() { - // System share. - $share = self::$share->newShare(null, 'systemshare'); - $perm = $share->getPermission(); - $this->assertInstanceOf('Horde_Perms_Permission', $perm); - $perm->addDefaultPermission(Horde_Perms::SHOW | Horde_Perms::READ); - $share->setPermission($perm); - $share->save(); - $this->assertTrue($share->hasPermission('john', Horde_Perms::SHOW)); - $this->assertTrue($share->hasPermission('john', Horde_Perms::READ)); - $this->assertFalse($share->hasPermission('john', Horde_Perms::EDIT)); - $this->assertFalse($share->hasPermission('john', Horde_Perms::DELETE)); - - // Foreign share with user permissions. - $share = self::$share->newShare('jane', 'janeshare'); - $share->addUserPermission('john', Horde_Perms::SHOW | Horde_Perms::READ | Horde_Perms::EDIT); - $share->save(); - $this->assertTrue($share->hasPermission('john', Horde_Perms::SHOW)); - $this->assertTrue($share->hasPermission('john', Horde_Perms::READ)); - $this->assertTrue($share->hasPermission('john', Horde_Perms::EDIT)); - $this->assertFalse($share->hasPermission('john', Horde_Perms::DELETE)); - - // Foreign share with group permissions. - $share = self::$share->newShare('jane', 'groupshare'); - $share->addGroupPermission('mygroup', Horde_Perms::SHOW | Horde_Perms::READ | Horde_Perms::DELETE); - $share->save(); - $this->assertTrue($share->hasPermission('john', Horde_Perms::SHOW)); - $this->assertTrue($share->hasPermission('john', Horde_Perms::READ)); - $this->assertFalse($share->hasPermission('john', Horde_Perms::EDIT)); - $this->assertTrue($share->hasPermission('john', Horde_Perms::DELETE)); - - // Foreign share without permissions. - $share = self::$share->newShare('jane', 'noshare'); - $share->save(); + parent::basePermissions(); } /** @@ -89,12 +49,7 @@ class Horde_Share_SqlTest extends PHPUnit_Framework_TestCase */ public function testExists() { - $this->assertTrue(self::$share->exists('myshare')); - - // Reset cache. - self::$share->resetCache(); - - $this->assertTrue(self::$share->exists('myshare')); + parent::baseExists(); } /** @@ -102,33 +57,19 @@ class Horde_Share_SqlTest extends PHPUnit_Framework_TestCase */ public function testCountShares() { - // Getting shares from cache. - $this->assertEquals(4, self::$share->countShares('john')); - $this->assertEquals(2, self::$share->countShares('john', Horde_Perms::EDIT)); - - // Reset cache. - self::$share->resetCache(); - - // Getting shares from backend. - $this->assertEquals(4, self::$share->countShares('john')); - $this->assertEquals(2, self::$share->countShares('john', Horde_Perms::EDIT)); + parent::baseCountShares(); } /** - * @depends testAddShare + * @depends testPermissions */ public function testGetShare() { - $share = self::$share->getShare('myshare'); - $this->assertInstanceOf('Horde_Share_Object_Sql', $share); - - // Reset cache. - self::$share->resetCache(); - - $share = self::$share->getShare('myshare'); - $this->assertInstanceOf('Horde_Share_Object_Sql', $share); - - return array($share, self::$share->getShare('janeshare'), self::$share->getShare('groupshare')); + $shares = parent::baseGetShare(); + $this->assertInstanceOf('Horde_Share_Object_Sql', $shares[0]); + $this->assertInstanceOf('Horde_Share_Object_Sql', $shares[1]); + $this->assertInstanceOf('Horde_Share_Object_Sql', $shares[2]); + return $shares; } /** @@ -136,28 +77,7 @@ class Horde_Share_SqlTest extends PHPUnit_Framework_TestCase */ public function testGetShareById(array $shares) { - $newshare = self::$share->getShareById($shares[0]->getId()); - $this->assertInstanceOf('Horde_Share_Object_Sql', $newshare); - $this->assertEquals($shares[0], $newshare); - $newshare = self::$share->getShareById($shares[1]->getId()); - $this->assertInstanceOf('Horde_Share_Object_Sql', $newshare); - $this->assertEquals($shares[1], $newshare); - $newshare = self::$share->getShareById($shares[2]->getId()); - $this->assertInstanceOf('Horde_Share_Object_Sql', $newshare); - $this->assertEquals($shares[2], $newshare); - - // Reset cache. - self::$share->resetCache(); - - $newshare = self::$share->getShareById($shares[0]->getId()); - $this->assertInstanceOf('Horde_Share_Object_Sql', $newshare); - $this->assertEquals($shares[0], $newshare); - $newshare = self::$share->getShareById($shares[1]->getId()); - $this->assertInstanceOf('Horde_Share_Object_Sql', $newshare); - $this->assertEquals($shares[1], $newshare); - $newshare = self::$share->getShareById($shares[2]->getId()); - $this->assertInstanceOf('Horde_Share_Object_Sql', $newshare); - $this->assertEquals($shares[2], $newshare); + parent::baseGetShareById($shares); } /** @@ -165,34 +85,7 @@ class Horde_Share_SqlTest extends PHPUnit_Framework_TestCase */ public function testGetShares(array $shares) { - $newshares = self::$share->getShares(array($shares[0]->getId(), $shares[1]->getId(), $shares[2]->getId())); - $this->assertType('array', $newshares); - $this->assertEquals(3, count($newshares)); - $this->assertArrayHasKey('myshare', $newshares); - $this->assertArrayHasKey('janeshare', $newshares); - $this->assertArrayHasKey('groupshare', $newshares); - $this->assertInstanceOf('Horde_Share_Object_Sql', $newshares['myshare']); - $this->assertInstanceOf('Horde_Share_Object_Sql', $newshares['janeshare']); - $this->assertInstanceOf('Horde_Share_Object_Sql', $newshares['groupshare']); - $this->assertEquals($newshares['myshare'], $shares[0]); - $this->assertEquals($newshares['janeshare'], $shares[1]); - $this->assertEquals($newshares['groupshare'], $shares[2]); - - // Reset cache. - self::$share->resetCache(); - - $newshares = self::$share->getShares(array($shares[0]->getId(), $shares[1]->getId(), $shares[2]->getId())); - $this->assertType('array', $newshares); - $this->assertEquals(3, count($newshares)); - $this->assertArrayHasKey('myshare', $newshares); - $this->assertArrayHasKey('janeshare', $newshares); - $this->assertArrayHasKey('groupshare', $newshares); - $this->assertInstanceOf('Horde_Share_Object_Sql', $newshares['myshare']); - $this->assertInstanceOf('Horde_Share_Object_Sql', $newshares['janeshare']); - $this->assertInstanceOf('Horde_Share_Object_Sql', $newshares['groupshare']); - $this->assertEquals($newshares['myshare'], $shares[0]); - $this->assertEquals($newshares['janeshare'], $shares[1]); - $this->assertEquals($newshares['groupshare'], $shares[2]); + parent::baseGetShares($shares); } /** @@ -200,28 +93,7 @@ class Horde_Share_SqlTest extends PHPUnit_Framework_TestCase */ public function testListAllShares() { - // Getting shares from cache. - $shares = self::$share->listAllShares(); - $this->assertType('array', $shares); - $this->assertEquals(5, count($shares)); - $this->assertArrayHasKey('myshare', $shares); - $this->assertArrayHasKey('systemshare', $shares); - $this->assertArrayHasKey('janeshare', $shares); - $this->assertArrayHasKey('groupshare', $shares); - $this->assertArrayHasKey('noshare', $shares); - - // Reset cache. - self::$share->resetCache(); - - // Getting shares from backend. - $shares = self::$share->listAllShares(); - $this->assertType('array', $shares); - $this->assertEquals(5, count($shares)); - $this->assertArrayHasKey('myshare', $shares); - $this->assertArrayHasKey('systemshare', $shares); - $this->assertArrayHasKey('janeshare', $shares); - $this->assertArrayHasKey('groupshare', $shares); - $this->assertArrayHasKey('noshare', $shares); + parent::baseListAllShares(); } /** @@ -229,20 +101,7 @@ class Horde_Share_SqlTest extends PHPUnit_Framework_TestCase */ public function testListSystemShares() { - // Getting shares from cache. - $shares = self::$share->listSystemShares(); - $this->assertType('array', $shares); - $this->assertEquals(1, count($shares)); - $this->assertArrayHasKey('systemshare', $shares); - - // Reset cache. - self::$share->resetCache(); - - // Getting shares from backend. - $shares = self::$share->listSystemShares(); - $this->assertType('array', $shares); - $this->assertEquals(1, count($shares)); - $this->assertArrayHasKey('systemshare', $shares); + parent::baseListSystemShares(); } /** @@ -250,13 +109,7 @@ class Horde_Share_SqlTest extends PHPUnit_Framework_TestCase */ public function testRemoveShare(array $share) { - self::$share->removeShare($share[0]); - $this->assertEquals(4, count(self::$share->listAllShares())); - - // Reset cache. - self::$share->resetCache(); - - $this->assertEquals(4, count(self::$share->listAllShares())); + parent::baseRemoveShare($share); } public static function setUpBeforeClass() @@ -336,23 +189,3 @@ class Horde_Share_SqlTest extends PHPUnit_Framework_TestCase } } } - -class Horde_Group_Test extends Horde_Group { - public function __construct() - { - } - - public function __wakeup() - { - } - - public function userIsInGroup($user, $gid, $subgroups = true) - { - return $user == 'john' && $gid == 'mygroup'; - } - - public function getGroupMemberships($user, $parentGroups = false) - { - return $user == 'john' ? array('mygroup' => 'mygroup') : array(); - } -} diff --git a/framework/Share/test/Horde/Share/TestBase.php b/framework/Share/test/Horde/Share/TestBase.php new file mode 100644 index 000000000..a14afc057 --- /dev/null +++ b/framework/Share/test/Horde/Share/TestBase.php @@ -0,0 +1,257 @@ + + * @category Horde + * @package Share + * @subpackage UnitTests + * @copyright 2010 The Horde Project (http://www.horde.org/) + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + */ +class Horde_Share_TestBase extends PHPUnit_Framework_TestCase +{ + protected static $share; + + public function testGetApp() + { + $this->assertEquals('test', self::$share->getApp()); + } + + public function baseAddShare() + { + $share = self::$share->newShare('john', 'myshare'); + $this->assertInstanceOf('Horde_Share_Object', $share); + self::$share->addShare($share); + return $share; + } + + /** + * @depends testAddShare + */ + public function basePermissions() + { + // System share. + $share = self::$share->newShare(null, 'systemshare'); + $perm = $share->getPermission(); + $this->assertInstanceOf('Horde_Perms_Permission', $perm); + $perm->addDefaultPermission(Horde_Perms::SHOW | Horde_Perms::READ); + $share->setPermission($perm); + $share->save(); + $this->assertTrue($share->hasPermission('john', Horde_Perms::SHOW)); + $this->assertTrue($share->hasPermission('john', Horde_Perms::READ)); + $this->assertFalse($share->hasPermission('john', Horde_Perms::EDIT)); + $this->assertFalse($share->hasPermission('john', Horde_Perms::DELETE)); + + // Foreign share with user permissions. + $share = self::$share->newShare('jane', 'janeshare'); + $share->addUserPermission('john', Horde_Perms::SHOW | Horde_Perms::READ | Horde_Perms::EDIT); + $share->save(); + $this->assertTrue($share->hasPermission('john', Horde_Perms::SHOW)); + $this->assertTrue($share->hasPermission('john', Horde_Perms::READ)); + $this->assertTrue($share->hasPermission('john', Horde_Perms::EDIT)); + $this->assertFalse($share->hasPermission('john', Horde_Perms::DELETE)); + + // Foreign share with group permissions. + $share = self::$share->newShare('jane', 'groupshare'); + $share->addGroupPermission('mygroup', Horde_Perms::SHOW | Horde_Perms::READ | Horde_Perms::DELETE); + $share->save(); + $this->assertTrue($share->hasPermission('john', Horde_Perms::SHOW)); + $this->assertTrue($share->hasPermission('john', Horde_Perms::READ)); + $this->assertFalse($share->hasPermission('john', Horde_Perms::EDIT)); + $this->assertTrue($share->hasPermission('john', Horde_Perms::DELETE)); + + // Foreign share without permissions. + $share = self::$share->newShare('jane', 'noshare'); + $share->save(); + } + + /** + * @depends testAddShare + */ + public function baseExists() + { + $this->assertTrue(self::$share->exists('myshare')); + + // Reset cache. + self::$share->resetCache(); + + $this->assertTrue(self::$share->exists('myshare')); + } + + /** + * @depends testPermissions + */ + public function baseCountShares() + { + // Getting shares from cache. + $this->assertEquals(4, self::$share->countShares('john')); + $this->assertEquals(2, self::$share->countShares('john', Horde_Perms::EDIT)); + + // Reset cache. + self::$share->resetCache(); + + // Getting shares from backend. + $this->assertEquals(4, self::$share->countShares('john')); + $this->assertEquals(2, self::$share->countShares('john', Horde_Perms::EDIT)); + } + + /** + * @depends testPermissions + */ + public function baseGetShare() + { + $share = self::$share->getShare('myshare'); + $this->assertInstanceOf('Horde_Share_Object', $share); + + // Reset cache. + self::$share->resetCache(); + + $share = self::$share->getShare('myshare'); + $this->assertInstanceOf('Horde_Share_Object', $share); + + return array($share, self::$share->getShare('janeshare'), self::$share->getShare('groupshare')); + } + + public function baseGetShareById(array $shares) + { + $newshare = self::$share->getShareById($shares[0]->getId()); + $this->assertInstanceOf('Horde_Share_Object', $newshare); + $this->assertEquals($shares[0], $newshare); + $newshare = self::$share->getShareById($shares[1]->getId()); + $this->assertInstanceOf('Horde_Share_Object', $newshare); + $this->assertEquals($shares[1], $newshare); + $newshare = self::$share->getShareById($shares[2]->getId()); + $this->assertInstanceOf('Horde_Share_Object', $newshare); + $this->assertEquals($shares[2], $newshare); + + // Reset cache. + self::$share->resetCache(); + + $newshare = self::$share->getShareById($shares[0]->getId()); + $this->assertInstanceOf('Horde_Share_Object', $newshare); + $this->assertEquals($shares[0], $newshare); + $newshare = self::$share->getShareById($shares[1]->getId()); + $this->assertInstanceOf('Horde_Share_Object', $newshare); + $this->assertEquals($shares[1], $newshare); + $newshare = self::$share->getShareById($shares[2]->getId()); + $this->assertInstanceOf('Horde_Share_Object', $newshare); + $this->assertEquals($shares[2], $newshare); + } + + public function baseGetShares(array $shares) + { + $newshares = self::$share->getShares(array($shares[0]->getId(), $shares[1]->getId(), $shares[2]->getId())); + $this->assertType('array', $newshares); + $this->assertEquals(3, count($newshares)); + $this->assertArrayHasKey('myshare', $newshares); + $this->assertArrayHasKey('janeshare', $newshares); + $this->assertArrayHasKey('groupshare', $newshares); + $this->assertInstanceOf('Horde_Share_Object', $newshares['myshare']); + $this->assertInstanceOf('Horde_Share_Object', $newshares['janeshare']); + $this->assertInstanceOf('Horde_Share_Object', $newshares['groupshare']); + $this->assertEquals($newshares['myshare'], $shares[0]); + $this->assertEquals($newshares['janeshare'], $shares[1]); + $this->assertEquals($newshares['groupshare'], $shares[2]); + + // Reset cache. + self::$share->resetCache(); + + $newshares = self::$share->getShares(array($shares[0]->getId(), $shares[1]->getId(), $shares[2]->getId())); + $this->assertType('array', $newshares); + $this->assertEquals(3, count($newshares)); + $this->assertArrayHasKey('myshare', $newshares); + $this->assertArrayHasKey('janeshare', $newshares); + $this->assertArrayHasKey('groupshare', $newshares); + $this->assertInstanceOf('Horde_Share_Object', $newshares['myshare']); + $this->assertInstanceOf('Horde_Share_Object', $newshares['janeshare']); + $this->assertInstanceOf('Horde_Share_Object', $newshares['groupshare']); + $this->assertEquals($newshares['myshare'], $shares[0]); + $this->assertEquals($newshares['janeshare'], $shares[1]); + $this->assertEquals($newshares['groupshare'], $shares[2]); + } + + /** + * @depends testPermissions + */ + public function baseListAllShares() + { + // Getting shares from cache. + $shares = self::$share->listAllShares(); + $this->assertType('array', $shares); + $this->assertEquals(5, count($shares)); + $this->assertArrayHasKey('myshare', $shares); + $this->assertArrayHasKey('systemshare', $shares); + $this->assertArrayHasKey('janeshare', $shares); + $this->assertArrayHasKey('groupshare', $shares); + $this->assertArrayHasKey('noshare', $shares); + + // Reset cache. + self::$share->resetCache(); + + // Getting shares from backend. + $shares = self::$share->listAllShares(); + $this->assertType('array', $shares); + $this->assertEquals(5, count($shares)); + $this->assertArrayHasKey('myshare', $shares); + $this->assertArrayHasKey('systemshare', $shares); + $this->assertArrayHasKey('janeshare', $shares); + $this->assertArrayHasKey('groupshare', $shares); + $this->assertArrayHasKey('noshare', $shares); + } + + /** + * @depends testPermissions + */ + public function baseListSystemShares() + { + // Getting shares from cache. + $shares = self::$share->listSystemShares(); + $this->assertType('array', $shares); + $this->assertEquals(1, count($shares)); + $this->assertArrayHasKey('systemshare', $shares); + + // Reset cache. + self::$share->resetCache(); + + // Getting shares from backend. + $shares = self::$share->listSystemShares(); + $this->assertType('array', $shares); + $this->assertEquals(1, count($shares)); + $this->assertArrayHasKey('systemshare', $shares); + } + + public function baseRemoveShare(array $share) + { + self::$share->removeShare($share[0]); + $this->assertEquals(4, count(self::$share->listAllShares())); + + // Reset cache. + self::$share->resetCache(); + + $this->assertEquals(4, count(self::$share->listAllShares())); + } +} + +class Horde_Group_Test extends Horde_Group { + public function __construct() + { + } + + public function __wakeup() + { + } + + public function userIsInGroup($user, $gid, $subgroups = true) + { + return $user == 'john' && $gid == 'mygroup'; + } + + public function getGroupMemberships($user, $parentGroups = false) + { + return $user == 'john' ? array('mygroup' => 'mygroup') : array(); + } +} -- 2.11.0