Finish 90% of unit tests.
authorJan Schneider <jan@horde.org>
Wed, 22 Dec 2010 22:47:54 +0000 (23:47 +0100)
committerJan Schneider <jan@horde.org>
Wed, 22 Dec 2010 22:50:02 +0000 (23:50 +0100)
framework/Share/lib/Horde/Share/Base.php
framework/Share/test/Horde/Share/Autoload.php [new file with mode: 0644]
framework/Share/test/Horde/Share/SqlTest.php

index f93e87b..846faa7 100644 (file)
@@ -607,6 +607,15 @@ abstract class Horde_Share_Base
     }
 
     /**
+     * Resets the internal caches.
+     */
+    public function resetCache()
+    {
+        $this->_cache = $this->_shareMap = array();
+        $this->setListCache(array());
+    }
+
+    /**
      * Give public access to call the share callbacks. Needed to run the
      * callbacks from the Horde_Share_Object objects.
      *
diff --git a/framework/Share/test/Horde/Share/Autoload.php b/framework/Share/test/Horde/Share/Autoload.php
new file mode 100644 (file)
index 0000000..3a933ce
--- /dev/null
@@ -0,0 +1,22 @@
+<?php
+/**
+ * Setup autoloading for the tests.
+ *
+ * PHP version 5
+ *
+ * Copyright 2009-2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @category   Horde
+ * @package    Share
+ * @subpackage UnitTests
+ * @author     Gunnar Wrobel <wrobel@pardus.de>
+ * @license    http://www.fsf.org/copyleft/lgpl.html LGPL
+ */
+
+require_once 'Horde/Test/Autoload.php';
+
+/* Catch strict standards */
+error_reporting(E_ALL | E_STRICT);
index aa70d58..720a191 100644 (file)
@@ -1,5 +1,10 @@
 <?php
 /**
+ * Prepare the test setup.
+ */
+require_once dirname(__FILE__) . '/Autoload.php';
+
+/**
  * @author     Jan Schneider <jan@horde.org>
  * @category   Horde
  * @package    Share
@@ -13,6 +18,25 @@ class Horde_Share_SqlTest extends PHPUnit_Framework_TestCase
 
     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');
@@ -20,6 +44,221 @@ class Horde_Share_SqlTest extends PHPUnit_Framework_TestCase
         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') ||
@@ -71,8 +310,13 @@ class Horde_Share_SqlTest extends PHPUnit_Framework_TestCase
 
         $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()
@@ -92,3 +336,23 @@ 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();
+    }
+}