Support retrieving shares.
authorGunnar Wrobel <p@rdus.de>
Mon, 17 Jan 2011 10:32:31 +0000 (11:32 +0100)
committerGunnar Wrobel <p@rdus.de>
Mon, 17 Jan 2011 10:33:08 +0000 (11:33 +0100)
framework/Share/lib/Horde/Share/Base.php
framework/Share/lib/Horde/Share/Kolab.php
framework/Share/lib/Horde/Share/Object/Kolab.php
framework/Share/test/Horde/Share/Kolab/UnitTest.php

index e362443..343126a 100644 (file)
@@ -202,7 +202,7 @@ abstract class Horde_Share_Base
     }
 
     /**
-     * Returns a Horde_Share_Object_sql object corresponding to the given
+     * Returns a Horde_Share_Object object corresponding to the given
      * share name, with the details retrieved appropriately.
      *
      * @param string $name  The name of the share to retrieve.
@@ -234,7 +234,7 @@ abstract class Horde_Share_Base
     }
 
     /**
-     * Returns a Horde_Share_Object_sql object corresponding to the given
+     * Returns a Horde_Share_Object object corresponding to the given
      * unique ID, with the details retrieved appropriately.
      *
      * @param integer $id  The id of the share to retrieve.
@@ -276,7 +276,7 @@ abstract class Horde_Share_Base
     }
 
     /**
-     * Returns an array of Horde_Share_Object_sql objects corresponding
+     * Returns an array of Horde_Share_Object objects corresponding
      * to the given set of unique IDs, with the details retrieved
      * appropriately.
      *
index 05cb5d7..c509a9a 100644 (file)
@@ -89,6 +89,121 @@ class Horde_Share_Kolab extends Horde_Share_Base
     }
 
     /**
+     * Return the type of folder this share driver will access in the Kolab
+     * storage backend (depends on the application calling the share driver).
+     *
+     * @return string
+     */
+    public function getType()
+    {
+        return $this->_type;
+    }
+    
+    /**
+     * Encode a share ID.
+     *
+     * @todo: In Horde3 share IDs were not properly escaped everywhere and it
+     * made sense to escape them here just in case they are placed in a
+     * URL. Needs checking, fixing and removal in Horde4.
+     *
+     * @param string $id The ID to be encoded.
+     *
+     * @return string The encoded ID.
+     */
+    private function _idEncode($id)
+    {
+        return rawurlencode($id);
+    }
+
+    /**
+     * Decode a share ID.
+     *
+     * @param string $id The ID to be decoded.
+     *
+     * @return string The decoded ID.
+     */
+    private function _idDecode($id)
+    {
+        return rawurldecode($id);
+    }
+
+    /**
+     * Returns a Horde_Share_Object_sql object corresponding to the given
+     * share name, with the details retrieved appropriately.
+     *
+     * @param string $name  The name of the share to retrieve.
+     *
+     * @return Horde_Share_Object  The requested share.
+     * @throws Horde_Exception_NotFound
+     * @throws Horde_Share_Exception
+     */
+    protected function _getShare($name)
+    {
+        $data = $this->getStorage()
+            ->getList()
+            ->getQuery('Base')
+            ->dataByType($this->_type);
+
+        if (!isset($data[$this->_idDecode($name)])) {
+            $this->_logger->err(sprintf('Share name %s not found', $name));
+            throw new Horde_Exception_NotFound();
+        }
+        return new Horde_Share_Object_Kolab(
+            $name, $data[$this->_idDecode($name)]
+        );
+    }
+
+    /**
+     * Returns a Horde_Share_Object_sql object corresponding to the given
+     * unique ID, with the details retrieved appropriately.
+     *
+     * @param integer $id  The id of the share to retrieve.
+     *
+     * @return Horde_Share_Object_sql  The requested share.
+     * @throws Horde_Share_Exception, Horde_Exception_NotFound
+     */
+    protected function _getShareById($id)
+    {
+        return $this->_getShare($id);
+    }
+
+    /**
+     * Returns an array of Horde_Share_Object_kolab objects corresponding to
+     * the requested folders.
+     *
+     * @param string $ids  The ids of the shares to fetch.
+     *
+     * @return array  An array of Horde_Share_Object_kolab objects.
+     */
+    protected function _getShares(array $ids)
+    {
+        $objects = array();
+        foreach ($ids as $id) {
+            $objects[$id] = $this->_getShare($id);
+        }
+        return $objects;
+    }
+
+    /**
+     * Checks if a share exists in the system.
+     *
+     * @param string $share  The share to check.
+     *
+     * @return boolean  True if the share exists.
+     * @throws Horde_Share_Exception
+     */
+    protected function _exists($share)
+    {
+        return in_array(
+            $this->_idDecode($share),
+            $this->getStorage()
+            ->getList()
+            ->getQuery('Base')
+            ->listByType($this->_type)
+        );
+    }
+
+    /**
      * Returns an array of all shares that $userid has access to.
      *
      * @param string $userid     The userid of the user to check access for.
@@ -99,7 +214,7 @@ class Horde_Share_Kolab extends Horde_Share_Base
     protected function _listShares($userid, array $params = array())
     {
         return array_map(
-            'rawurlencode',
+            array($this, '_idEncode'),
             $this->getStorage()
             ->getList()
             ->getQuery('Base')
@@ -168,76 +283,6 @@ class Horde_Share_Kolab extends Horde_Share_Base
     }
 
     /**
-     * Return the type of folder this share driver will access in the Kolab
-     * storage backend (depends on the application calling the share driver).
-     *
-     * @return string
-     */
-    public function getType()
-    {
-        return $this->_type;
-    }
-    
-    /**
-     * (re)connect the share object to this share driver. Userful for when
-     * share objects are unserialized from a cache separate from the share
-     * driver.
-     *
-     * @param Horde_Share_Object $object
-     */
-    public function initShareObject(Horde_Share_Object $object)
-    {
-        $object->setShareOb($this);
-    }
-
-    /**
-     * Returns a Horde_Share_Object_sql object corresponding to the given
-     * share name, with the details retrieved appropriately.
-     *
-     * @param string $name  The name of the share to retrieve.
-     *
-     * @return Horde_Share_Object  The requested share.
-     * @throws Horde_Exception_NotFound
-     * @throws Horde_Share_Exception
-     */
-    protected function _getShare($name)
-    {
-        //@todo: get $data from the list cache.
-        return new Horde_Share_Object_Kolab($name);
-    }
-
-    /**
-     * Returns a Horde_Share_Object_sql object corresponding to the given
-     * unique ID, with the details retrieved appropriately.
-     *
-     * @param integer $id  The id of the share to retrieve.
-     *
-     * @return Horde_Share_Object_sql  The requested share.
-     * @throws Horde_Share_Exception, Horde_Exception_NotFound
-     */
-    protected function _getShareById($id)
-    {
-        return $this->_getShare($id);
-    }
-
-    /**
-     * Returns an array of Horde_Share_Object_kolab objects corresponding to
-     * the requested folders.
-     *
-     * @param string $ids  The ids of the shares to fetch.
-     *
-     * @return array  An array of Horde_Share_Object_kolab objects.
-     */
-    protected function _getShares(array $ids)
-    {
-        $objects = array();
-        foreach ($ids as $id) {
-            $objects[$id] = $this->_getShare($id);
-        }
-        return $objects;
-    }
-
-    /**
      * Returns a new share object.
      *
      * @param string $name  The share's name.
@@ -277,28 +322,6 @@ class Horde_Share_Kolab extends Horde_Share_Base
     }
 
     /**
-     * Checks if a share exists in the system.
-     *
-     * @param string $share  The share to check.
-     *
-     * @return boolean  True if the share exists.
-     */
-    protected function _exists($object)
-    {
-        if (empty($object)) {
-            return false;
-        }
-
-        /* Get the corresponding folder for this share ID */
-        $folder = $this->_storage->getByShare($object, $this->_type);
-        if ($folder instanceof PEAR_Error) {
-            throw new Horde_Share_Exception($folder->getMessage());
-        }
-
-        return $folder->exists();
-    }
-
-    /**
      * Create a default share for the current app
      *
      * @return string The share ID of the new default share.
index fe7b4a4..8341282 100644 (file)
@@ -133,20 +133,9 @@ class Horde_Share_Object_Kolab extends Horde_Share_Object implements Serializabl
         if (isset($this->_data[$attribute])) {
             return $this->_data[$attribute];
         }
-
-        if (!isset($this->_folder)) {
-            return $this->_folderError();
-        }
+        return;
 
         switch ($attribute) {
-        case 'owner':
-            $this->_data['owner'] = $this->_folder->getOwner();
-            break;
-
-        case 'name':
-            $this->_data['name'] = $this->_folder->getTitle();
-            break;
-
         case 'type':
             $this->_data['type'] = $this->_folder->getType();
             break;
index d14e678..88e0129 100644 (file)
@@ -129,10 +129,65 @@ extends PHPUnit_Framework_TestCase
         $this->assertEquals('test', $object->getName());
     }
 
+    public function testGetShare()
+    {
+        $this->assertEquals(
+            'INBOX%2FCalendar',
+            $this->_getPrefilledDriver()->getShare('INBOX%2FCalendar')->getId()
+        );
+    }
+
+    public function testExists()
+    {
+        $this->assertTrue(
+            $this->_getPrefilledDriver()->exists('INBOX%2FCalendar')
+        );
+    }
+
+    public function testDoesNotExists()
+    {
+        $this->assertFalse(
+            $this->_getPrefilledDriver()->exists('DOES_NOT_EXIST')
+        );
+    }
+
+    public function testGetShareById()
+    {
+        $this->assertEquals(
+            'INBOX%2FCalendar',
+            $this->_getPrefilledDriver()
+            ->getShareById('INBOX%2FCalendar')
+            ->getId()
+        );
+    }
+
     /**
-     * @todo: Reminder: Check that external modification of the Storage system
-     * works (former list->validity).
+     * @expectedException Horde_Exception_NotFound
      */
+    public function testMissingShare()
+    {
+        $this->_getPrefilledDriver()->getShare('DOES_NOT_EXIST');
+    }
+
+    public function testShareOwner()
+    {
+        $this->assertEquals(
+            'john',
+            $this->_getPrefilledDriver()
+            ->getShare('INBOX%2FCalendar')
+            ->get('owner')
+        );
+    }
+
+    public function testShareName()
+    {
+        $this->assertEquals(
+            'Calendar',
+            $this->_getPrefilledDriver()
+            ->getShare('INBOX%2FCalendar')
+            ->get('name')
+        );
+    }
 
     private function _getPrefilledDriver()
     {