Add unit test for hierarchical driver.
authorJan Schneider <jan@horde.org>
Mon, 3 Jan 2011 16:33:04 +0000 (17:33 +0100)
committerJan Schneider <jan@horde.org>
Mon, 3 Jan 2011 16:33:04 +0000 (17:33 +0100)
framework/Share/package.xml
framework/Share/test/Horde/Share/SqlHierarchicalTest.php [new file with mode: 0644]
framework/Share/test/Horde/Share/SqlTest.php
framework/Share/test/Horde/Share/TestBase.php

index a1d51c9..97f541e 100644 (file)
@@ -12,7 +12,7 @@ owns or has access to.</description>
   <active>yes</active>
  </lead>
  <date>2011-01-03</date>
- <time>15:20:05</time>
+ <time>17:30:14</time>
  <version>
   <release>0.0.4</release>
   <api>0.0.4</api>
@@ -322,6 +322,7 @@ owns or has access to.</description>
      <dir name="Share">
       <file name="AllTests.php" role="test" />
       <file name="Autoload.php" role="test" />
+      <file name="SqlHierarchicalTest.php" role="test" />
       <file name="SqlTest.php" role="test" />
       <file name="TestBase.php" role="test" />
      </dir> <!-- /test/Horde/Share -->
@@ -451,6 +452,7 @@ owns or has access to.</description>
    <install as="locale/zh_TW/LC_MESSAGES/Horde_Share.po" name="locale/zh_TW/LC_MESSAGES/Horde_Share.po" />
    <install as="Horde/Share/AllTests.php" name="test/Horde/Share/AllTests.php" />
    <install as="Horde/Share/Autoload.php" name="test/Horde/Share/Autoload.php" />
+   <install as="Horde/Share/SqlHierarchicalTest.php" name="test/Horde/Share/SqlHierarchicalTest.php" />
    <install as="Horde/Share/SqlTest.php" name="test/Horde/Share/SqlTest.php" />
    <install as="Horde/Share/TestBase.php" name="test/Horde/Share/TestBase.php" />
   </filelist>
diff --git a/framework/Share/test/Horde/Share/SqlHierarchicalTest.php b/framework/Share/test/Horde/Share/SqlHierarchicalTest.php
new file mode 100644 (file)
index 0000000..423ddbb
--- /dev/null
@@ -0,0 +1,212 @@
+<?php
+/**
+ * Prepare the test setup.
+ */
+require_once dirname(__FILE__) . '/TestBase.php';
+
+/**
+ * @author     Jan Schneider <jan@horde.org>
+ * @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_SqlHierarchicalTest extends Horde_Share_TestBase
+{
+    protected static $db;
+
+    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 = parent::baseAddShare();
+        $this->assertInstanceOf('Horde_Share_Object_Sql_Hierarchical', $share);
+        return $share->getId();
+    }
+
+    /**
+     * @depends testAddShare
+     */
+    public function testPermissions($myshareid)
+    {
+        $shareids = parent::basePermissions($myshareid);
+        return array(self::$share->getShareById($shareids[0]),
+                     self::$share->getShareById($shareids[1]),
+                     self::$share->getShareById($shareids[2]));
+    }
+
+    /**
+     * @depends testAddShare
+     */
+    public function testExists()
+    {
+        $this->markTestSkipped('Not supported by hierarchical driver.');
+    }
+
+    /**
+     * @depends testPermissions
+     */
+    public function testCountShares()
+    {
+        parent::baseCountShares();
+    }
+
+    /**
+     * @depends testPermissions
+     */
+    public function testGetShare()
+    {
+        $this->markTestSkipped('Not supported by hierarchical driver.');
+    }
+
+    /**
+     * @depends testPermissions
+     */
+    public function testGetShareById(array $shares)
+    {
+        parent::baseGetShareById($shares);
+    }
+
+    /**
+     * @depends testPermissions
+     */
+    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->assertInstanceOf('Horde_Share_Object_Sql_Hierarchical', $newshares[0]);
+        $this->assertInstanceOf('Horde_Share_Object_Sql_Hierarchical', $newshares[1]);
+        $this->assertInstanceOf('Horde_Share_Object_Sql_Hierarchical', $newshares[2]);
+        $this->assertEquals($newshares[0], $shares[0]);
+        $this->assertEquals($newshares[1], $shares[1]);
+        $this->assertEquals($newshares[2], $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->assertInstanceOf('Horde_Share_Object_Sql_Hierarchical', $newshares[$shares[0]->getId()]);
+        $this->assertInstanceOf('Horde_Share_Object_Sql_Hierarchical', $newshares[$shares[1]->getId()]);
+        $this->assertInstanceOf('Horde_Share_Object_Sql_Hierarchical', $newshares[$shares[2]->getId()]);
+        $this->assertEquals($newshares[$shares[0]->getId()], $shares[0]);
+        $this->assertEquals($newshares[$shares[1]->getId()], $shares[1]);
+        $this->assertEquals($newshares[$shares[2]->getId()], $shares[2]);
+    }
+
+    /**
+     * @depends testPermissions
+     */
+    public function testListAllShares()
+    {
+        $this->markTestSkipped('Not supported by hierarchical driver.');
+    }
+
+    /**
+     * @depends testPermissions
+     */
+    public function testListSystemShares()
+    {
+        $this->markTestSkipped('Not supported by hierarchical driver.');
+    }
+
+    /**
+     * @depends testPermissions
+     */
+    public function testRemoveShare(array $share)
+    {
+        parent::baseRemoveShare($share);
+    }
+
+    public static function setUpBeforeClass()
+    {
+        if (!extension_loaded('pdo') ||
+            !in_array('sqlite', PDO::getAvailableDrivers())) {
+            return;
+        }
+
+        self::$db = new Horde_Db_Adapter_Pdo_Sqlite(array('dbname' => ':memory:'));
+        //self::$db->setLogger(new Horde_Log_Logger(new Horde_Log_Handler_Stream(STDOUT)));
+        $migration = new Horde_Db_Migration_Base(self::$db);
+
+        $t = $migration->createTable('test_shares', array('primaryKey' => 'share_id'));
+        //$t->column('share_id', 'integer', array('null' => false, 'autoincrement' => true));
+        $t->column('share_owner', 'string', array('limit' => 255));
+        $t->column('share_flags', 'integer', array('default' => 0, 'null' => false));
+        $t->column('share_parents', 'string', array('limit' => 255));
+        $t->column('perm_creator', 'integer', array('default' => 0, 'null' => false));
+        $t->column('perm_default', 'integer', array('default' => 0, 'null' => false));
+        $t->column('perm_guest', 'integer', array('default' => 0, 'null' => false));
+        $t->column('attribute_name', 'string', array('limit' => 255));
+        $t->column('attribute_desc', 'string', array('limit' => 255));
+        $t->end();
+
+        $migration->addIndex('test_shares', array('share_owner'));
+        $migration->addIndex('test_shares', array('perm_creator'));
+        $migration->addIndex('test_shares', array('perm_default'));
+        $migration->addIndex('test_shares', array('perm_guest'));
+        $migration->addIndex('test_shares', array('share_parents'));
+
+        $t = $migration->createTable('test_shares_groups');
+        $t->column('share_id', 'integer', array('null' => false));
+        $t->column('group_uid', 'string', array('limit' => 255, 'null' => false));
+        $t->column('perm', 'integer', array('null' => false));
+        $t->end();
+
+        $migration->addIndex('test_shares_groups', array('share_id'));
+        $migration->addIndex('test_shares_groups', array('group_uid'));
+        $migration->addIndex('test_shares_groups', array('perm'));
+
+        $t = $migration->createTable('test_shares_users');
+        $t->column('share_id', 'integer', array('null' => false));
+        $t->column('user_uid', 'string', array('limit' => 255));
+        $t->column('perm', 'integer', array('null' => false));
+        $t->end();
+
+        $migration->addIndex('test_shares_users', array('share_id'));
+        $migration->addIndex('test_shares_users', array('user_uid'));
+        $migration->addIndex('test_shares_users', array('perm'));
+
+        $migration->migrate('up');
+
+        $group = new Horde_Group_Test();
+        self::$share = new Horde_Share_Sql_Hierarchical('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()
+    {
+        if (self::$db) {
+            $migration = new Horde_Db_Migration_Base(self::$db);
+            $migration->dropTable('test_shares');
+            $migration->dropTable('test_shares_groups');
+            $migration->dropTable('test_shares_users');
+        }
+    }
+
+    public function setUp()
+    {
+        if (!self::$db) {
+            $this->markTestSkipped('No sqlite extension or no sqlite PDO driver.');
+        }
+    }
+}
index 3003fdc..7ddbe4c 100644 (file)
@@ -34,14 +34,15 @@ class Horde_Share_SqlTest extends Horde_Share_TestBase
     {
         $share = parent::baseAddShare();
         $this->assertInstanceOf('Horde_Share_Object_Sql', $share);
+        return $share->getId();
     }
 
     /**
      * @depends testAddShare
      */
-    public function testPermissions()
+    public function testPermissions($myshareid)
     {
-        parent::basePermissions();
+        return parent::basePermissions($myshareid);
     }
 
     /**
index a14afc0..7a810b6 100644 (file)
@@ -29,10 +29,7 @@ class Horde_Share_TestBase extends PHPUnit_Framework_TestCase
         return $share;
     }
 
-    /**
-     * @depends testAddShare
-     */
-    public function basePermissions()
+    public function basePermissions($myshareid)
     {
         // System share.
         $share = self::$share->newShare(null, 'systemshare');
@@ -47,31 +44,30 @@ class Horde_Share_TestBase extends PHPUnit_Framework_TestCase
         $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));
+        $janeshare = self::$share->newShare('jane', 'janeshare');
+        $janeshare->addUserPermission('john', Horde_Perms::SHOW | Horde_Perms::READ | Horde_Perms::EDIT);
+        $janeshare->save();
+        $this->assertTrue($janeshare->hasPermission('john', Horde_Perms::SHOW));
+        $this->assertTrue($janeshare->hasPermission('john', Horde_Perms::READ));
+        $this->assertTrue($janeshare->hasPermission('john', Horde_Perms::EDIT));
+        $this->assertFalse($janeshare->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));
+        $groupshare = self::$share->newShare('jane', 'groupshare');
+        $groupshare->addGroupPermission('mygroup', Horde_Perms::SHOW | Horde_Perms::READ | Horde_Perms::DELETE);
+        $groupshare->save();
+        $this->assertTrue($groupshare->hasPermission('john', Horde_Perms::SHOW));
+        $this->assertTrue($groupshare->hasPermission('john', Horde_Perms::READ));
+        $this->assertFalse($groupshare->hasPermission('john', Horde_Perms::EDIT));
+        $this->assertTrue($groupshare->hasPermission('john', Horde_Perms::DELETE));
 
         // Foreign share without permissions.
         $share = self::$share->newShare('jane', 'noshare');
         $share->save();
+
+        return array($myshareid, $janeshare->getId(), $groupshare->getId());
     }
 
-    /**
-     * @depends testAddShare
-     */
     public function baseExists()
     {
         $this->assertTrue(self::$share->exists('myshare'));
@@ -82,9 +78,6 @@ class Horde_Share_TestBase extends PHPUnit_Framework_TestCase
         $this->assertTrue(self::$share->exists('myshare'));
     }
 
-    /**
-     * @depends testPermissions
-     */
     public function baseCountShares()
     {
         // Getting shares from cache.
@@ -99,9 +92,6 @@ class Horde_Share_TestBase extends PHPUnit_Framework_TestCase
         $this->assertEquals(2, self::$share->countShares('john', Horde_Perms::EDIT));
     }
 
-    /**
-     * @depends testPermissions
-     */
     public function baseGetShare()
     {
         $share = self::$share->getShare('myshare');
@@ -174,9 +164,6 @@ class Horde_Share_TestBase extends PHPUnit_Framework_TestCase
         $this->assertEquals($newshares['groupshare'], $shares[2]);
     }
 
-    /**
-     * @depends testPermissions
-     */
     public function baseListAllShares()
     {
         // Getting shares from cache.
@@ -203,9 +190,6 @@ class Horde_Share_TestBase extends PHPUnit_Framework_TestCase
         $this->assertArrayHasKey('noshare', $shares);
     }
 
-    /**
-     * @depends testPermissions
-     */
     public function baseListSystemShares()
     {
         // Getting shares from cache.
@@ -227,12 +211,20 @@ class Horde_Share_TestBase extends PHPUnit_Framework_TestCase
     public function baseRemoveShare(array $share)
     {
         self::$share->removeShare($share[0]);
-        $this->assertEquals(4, count(self::$share->listAllShares()));
+        try {
+            self::$share->getShareById($share[0]->getId());
+            $this->fail('Share ' . $share[0]->getId() . ' should be removed by now.');
+        } catch (Horde_Exception_NotFound $e) {
+        }
 
         // Reset cache.
         self::$share->resetCache();
 
-        $this->assertEquals(4, count(self::$share->listAllShares()));
+        try {
+            self::$share->getShareById($share[0]->getId());
+            $this->fail('Share ' . $share[0]->getId() . ' should be removed by now.');
+        } catch (Horde_Exception_NotFound $e) {
+        }
     }
 }