<?php
/**
+ * Prepare the test setup.
+ */
+require_once dirname(__FILE__) . '/Autoload.php';
+
+/**
* @author Jan Schneider <jan@horde.org>
* @category Horde
* @package Share
protected static $share;
+ public function testGetApp()
+ {
+ $this->assertEquals('test', self::$share->getApp());
+ }
+
+ public function testSetTable()
+ {
+ $this->assertEquals('test_shares', self::$share->getTable());
+ self::$share->setTable('foo');
+ $this->assertEquals('foo', self::$share->getTable());
+ self::$share->setTable('test_shares');
+ }
+
+ public function testSetStorage()
+ {
+ self::$share->setStorage(self::$db);
+ $this->assertEquals(self::$db, self::$share->getStorage());
+ }
+
public function testAddShare()
{
$share = self::$share->newShare('john', 'myshare');
self::$share->addShare($share);
}
+ /**
+ * @depends testAddShare
+ */
+ 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();
+ }
+
+ /**
+ * @depends testAddShare
+ */
+ public function testExists()
+ {
+ $this->assertTrue(self::$share->exists('myshare'));
+
+ // Reset cache.
+ self::$share->resetCache();
+
+ $this->assertTrue(self::$share->exists('myshare'));
+ }
+
+ /**
+ * @depends testPermissions
+ */
+ 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));
+ }
+
+ /**
+ * @depends testAddShare
+ */
+ 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'));
+ }
+
+ /**
+ * @depends testGetShare
+ */
+ 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);
+ }
+
+ /**
+ * @depends testGetShare
+ */
+ 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]);
+ }
+
+ /**
+ * @depends testPermissions
+ */
+ 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);
+ }
+
+ /**
+ * @depends testPermissions
+ */
+ 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);
+ }
+
+ /**
+ * @depends testGetShare
+ */
+ 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()));
+ }
+
public static function setUpBeforeClass()
{
if (!extension_loaded('pdo') ||
$migration->migrate('up');
- self::$share = new Horde_Share_Sql('test', 'john', new Horde_Perms(), new Horde_Group_Mock());
+ $group = new Horde_Group_Test();
+ self::$share = new Horde_Share_Sql('test', 'john', new Horde_Perms(), $group);
self::$share->setStorage(self::$db);
+
+ // FIXME
+ $GLOBALS['injector'] = new Horde_Injector(new Horde_Injector_TopLevel());
+ $GLOBALS['injector']->setInstance('Horde_Group', $group);
}
public static function tearDownAfterClass()
}
}
}
+
+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();
+ }
+}