Extend the base query to allow quickly fetching folder information.
authorGunnar Wrobel <p@rdus.de>
Thu, 20 Jan 2011 14:48:11 +0000 (15:48 +0100)
committerGunnar Wrobel <p@rdus.de>
Thu, 20 Jan 2011 14:48:11 +0000 (15:48 +0100)
framework/Kolab_Storage/lib/Horde/Kolab/Storage/List/Query.php
framework/Kolab_Storage/lib/Horde/Kolab/Storage/List/Query/Base.php
framework/Kolab_Storage/lib/Horde/Kolab/Storage/List/Query/Cache.php
framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/List/Query/BaseTest.php
framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/List/Query/CacheTest.php

index 2d95b1f..1821d02 100644 (file)
@@ -54,6 +54,15 @@ extends Horde_Kolab_Storage_Query
     public function dataByType($type);
 
     /**
+     * List basic folder data for the specified folder.
+     *
+     * @param string $folder The folder path.
+     *
+     * @return array The folder data.
+     */
+    public function folderData($folder);
+
+    /**
      * List all folders of a specific type.
      *
      * @param string $type The folder type the listing should be limited to.
index ffb6b8b..1af0c9e 100644 (file)
@@ -127,6 +127,38 @@ implements Horde_Kolab_Storage_List_Query
     }
 
     /**
+     * List basic folder data for the specified folder.
+     *
+     * @param string $folder The folder path.
+     *
+     * @return array The folder data.
+     */
+    public function folderData($folder)
+    {
+        $list = $this->_list->listFolders();
+        if (!in_array($folder, $list)) {
+            throw new Horde_Kolab_Storage_Exception(
+                sprintf('Folder %s does not exist!', $folder)
+            );
+        }
+        $annotations = $this->listFolderTypeAnnotations();
+        if (!isset($annotations[$folder])) {
+            $type = $this->_factory->createFolderType('mail');
+        } else {
+            $type = $annotations[$folder];
+        }
+        $namespace = $this->_list->getNamespace();
+        return array(
+            'type' => $type->getType(),
+            'default' => $type->isDefault(),
+            'namespace' => $namespace->matchNamespace($folder)->getType(),
+            'owner' => $namespace->getOwner($folder),
+            'name' => $namespace->getTitle($folder),
+            'subpath' => $namespace->getSubpath($folder),
+        );
+    }
+
+    /**
      * Get the folder owners.
      *
      * @return array The folder owners with the folder names as key and the
index 7ab28c9..c8b784d 100644 (file)
@@ -34,6 +34,9 @@ implements Horde_Kolab_Storage_List_Query
     /** The folder list sorted by type */
     const BY_TYPE = 'BY_TYPE';
 
+    /** The list of folder data */
+    const FOLDERS = 'FOLDERS';
+
     /** The folder owner list */
     const OWNERS = 'OWNERS';
 
@@ -141,6 +144,25 @@ implements Horde_Kolab_Storage_List_Query
     }
 
     /**
+     * List basic folder data for the specified folder.
+     *
+     * @param string $folder The folder path.
+     *
+     * @return array The folder data.
+     */
+    public function folderData($folder)
+    {
+        $folders = $this->_list_cache->getQuery(self::FOLDERS);
+        if (isset($folders[$folder])) {
+            return $folders[$folder];
+        } else {
+            throw new Horde_Kolab_Storage_Exception(
+                sprintf('Folder %s does not exist!', $folder)
+            );
+        }
+    }
+
+    /**
      * Get the folder owners.
      *
      * @return array The folder owners with the folder names as key and the
@@ -217,58 +239,74 @@ implements Horde_Kolab_Storage_List_Query
     public function synchronize()
     {
         $namespace = $this->_list->getNamespace();
+        $annotations = $this->listFolderTypeAnnotations();
+        $mail_type = $this->_factory->createFolderType('mail');
 
+        $folders = array();
         $owners = array();
-        foreach ($this->_list->listFolders() as $folder) {
-            $owners[$folder] = $namespace->getOwner($folder);
-        }
-        $this->_list_cache->setQuery(self::OWNERS, $owners);
-
         $types = array();
         $by_type = array();
         $personal_defaults = array();
         $defaults = array();
 
-        foreach ($this->listFolderTypeAnnotations() as $folder => $annotation) {
-            $type = $annotation->getType();
+        foreach ($this->_list->listFolders() as $folder) {
+            if (!isset($annotations[$folder])) {
+                $type = $mail_type;
+            } else {
+                $type = $annotations[$folder];
+            }
+            $folder_type = $type->getType();
             $owner = $namespace->getOwner($folder);
-            $title = $namespace->getTitle($folder);
 
-            $types[$folder] = $type;
-            $by_type[$type][$folder] = array(
-                'owner' => $owner, 'name' => $title
+            $owners[$folder] = $owner;
+            $folders[$folder] = array(
+                'type' => $folder_type,
+                'default' => $type->isDefault(),
+                'namespace' => $namespace->matchNamespace($folder)->getType(),
+                'owner' => $owner,
+                'name' => $namespace->getTitle($folder),
+                'subpath' => $namespace->getSubpath($folder),
+            );
+
+            $types[$folder] = $folders[$folder]['type'];
+            $by_type[$folder_type][$folder] = array(
+                'owner' => $folders[$folder]['owner'],
+                'name' => $folders[$folder]['name']
             );
 
-            if ($annotation->isDefault()) {
-                if (!isset($defaults[$owner][$type])) {
-                    $defaults[$owner][$type] = $folder;
+            if ($folders[$folder]['default']) {
+                if (!isset($defaults[$owner][$folder_type])) {
+                    $defaults[$owner][$folder_type] = $folder;
                 } else {
                     throw new Horde_Kolab_Storage_Exception(
                         sprintf(
                             'Both folders %s and %s are marked as default folder of type %s!',
-                            $defaults[$owner][$type],
+                            $defaults[$owner][$folder_type],
                             $folder,
-                            $type
+                            $folder_type
                         )
                     );
                 }
-                if ($namespace->matchNamespace($folder)->getType()
+                if ($folders[$folder]['namespace']
                     == Horde_Kolab_Storage_Folder_Namespace::PERSONAL) {
-                    if (!isset($personal_defaults[$type])) {
-                        $personal_defaults[$type] = $folder;
+                    if (!isset($personal_defaults[$folder_type])) {
+                        $personal_defaults[$folder_type] = $folder;
                     } else {
                         throw new Horde_Kolab_Storage_Exception(
                             sprintf(
                                 'Both folders %s and %s are marked as default folder of type %s!',
-                                $personal_defaults[$type],
+                                $personal_defaults[$folder_type],
                                 $folder,
-                                $type
+                                $folder_type
                             )
                         );
                     }
                 }
             }
         }
+
+        $this->_list_cache->setQuery(self::FOLDERS, $folders);
+        $this->_list_cache->setQuery(self::OWNERS, $owners);
         $this->_list_cache->setQuery(self::TYPES, $types);
         $this->_list_cache->setQuery(self::BY_TYPE, $by_type);
         $this->_list_cache->setQuery(self::DEFAULTS, $defaults);
index e24a0ca..924a6c1 100644 (file)
@@ -305,4 +305,87 @@ extends Horde_Kolab_Storage_TestCase
             $data['INBOX/Calendar']['name']
         );
     }
+
+    /**
+     * @expectedException Horde_Kolab_Storage_Exception
+     */
+    public function testMissingFolderData()
+    {
+        $this->assertType('array', $this->getNullQuery()->folderData('INBOX/Calendar'));
+    }
+
+    public function testFolderDataReturnsArray()
+    {
+        $this->assertType('array', $this->getAnnotatedQuery()->folderData('INBOX/Calendar'));
+    }
+
+    public function testFolderDataHasOwner()
+    {
+        $data = $this->getAnnotatedQuery()->folderData('INBOX/Calendar');
+        $this->assertEquals(
+            'test@example.com',
+            $data['owner']
+        );
+    }
+
+    public function testFolderDataHasTitle()
+    {
+        $data = $this->getAnnotatedQuery()->folderData('INBOX/Calendar');
+        $this->assertEquals(
+            'Calendar',
+            $data['name']
+        );
+    }
+
+    public function testFolderDataHasType()
+    {
+        $data = $this->getAnnotatedQuery()->folderData('INBOX/Calendar');
+        $this->assertEquals(
+            'event',
+            $data['type']
+        );
+    }
+
+    public function testFolderDataHasDefault()
+    {
+        $data = $this->getAnnotatedQuery()->folderData('INBOX/Calendar');
+        $this->assertTrue(
+            $data['default']
+        );
+    }
+
+    public function testMailFolderDataType()
+    {
+        $data = $this->getAnnotatedQuery()->folderData('INBOX');
+        $this->assertEquals(
+            'mail',
+            $data['type']
+        );
+    }
+
+    public function testMailFolderDataNoDefault()
+    {
+        $data = $this->getAnnotatedQuery()->folderData('INBOX');
+        $this->assertFalse(
+            $data['default']
+        );
+    }
+
+    public function testFolderDataHasNamespace()
+    {
+        $data = $this->getAnnotatedQuery()->folderData('INBOX/Calendar');
+        $this->assertEquals(
+            'personal',
+            $data['namespace']
+        );
+    }
+
+    public function testFolderDataHasSubpath()
+    {
+        $data = $this->getAnnotatedQuery()->folderData('INBOX/Calendar');
+        $this->assertEquals(
+            'Calendar',
+            $data['subpath']
+        );
+    }
 }
index 844c8d0..2224e35 100644 (file)
@@ -64,10 +64,12 @@ extends Horde_Kolab_Storage_TestCase
         $query = $this->getCachedQueryForList($this->getAnnotatedList($factory), $factory);
         $this->assertEquals(
             array(
+                'INBOX' => 'mail',
                 'INBOX/Calendar' => 'event',
                 'INBOX/Contacts' => 'contact',
                 'INBOX/Notes' => 'note',
                 'INBOX/Tasks' => 'task',
+                'INBOX/a' => 'mail',
             ),
             $query->listTypes()
         );
@@ -345,4 +347,97 @@ extends Horde_Kolab_Storage_TestCase
             $data['INBOX/Calendar']['name']
         );
     }
+
+    /**
+     * @expectedException Horde_Kolab_Storage_Exception
+     */
+    public function testMissingFolderData()
+    {
+        $this->assertType('array', $this->getNullQuery()->folderData('INBOX/Calendar'));
+    }
+
+    public function testFolderDataReturnsArray()
+    {
+        $factory = new Horde_Kolab_Storage_Factory();
+        $data = $this->getCachedQueryForList($this->getAnnotatedList($factory), $factory)->folderData('INBOX/Calendar');
+        $this->assertType('array', $data);
+    }
+
+    public function testFolderDataHasOwner()
+    {
+        $factory = new Horde_Kolab_Storage_Factory();
+        $data = $this->getCachedQueryForList($this->getAnnotatedList($factory), $factory)->folderData('INBOX/Calendar');
+        $this->assertEquals(
+            'test@example.com',
+            $data['owner']
+        );
+    }
+
+    public function testFolderDataHasTitle()
+    {
+        $factory = new Horde_Kolab_Storage_Factory();
+        $data = $this->getCachedQueryForList($this->getAnnotatedList($factory), $factory)->folderData('INBOX/Calendar');
+        $this->assertEquals(
+            'Calendar',
+            $data['name']
+        );
+    }
+
+    public function testFolderDataHasType()
+    {
+        $factory = new Horde_Kolab_Storage_Factory();
+        $data = $this->getCachedQueryForList($this->getAnnotatedList($factory), $factory)->folderData('INBOX/Calendar');
+        $this->assertEquals(
+            'event',
+            $data['type']
+        );
+    }
+
+    public function testFolderDataHasDefault()
+    {
+        $factory = new Horde_Kolab_Storage_Factory();
+        $data = $this->getCachedQueryForList($this->getAnnotatedList($factory), $factory)->folderData('INBOX/Calendar');
+        $this->assertTrue(
+            $data['default']
+        );
+    }
+
+    public function testMailFolderDataType()
+    {
+        $factory = new Horde_Kolab_Storage_Factory();
+        $data = $this->getCachedQueryForList($this->getAnnotatedList($factory), $factory)->folderData('INBOX');
+        $this->assertEquals(
+            'mail',
+            $data['type']
+        );
+    }
+
+    public function testMailFolderDataNoDefault()
+    {
+        $factory = new Horde_Kolab_Storage_Factory();
+        $data = $this->getCachedQueryForList($this->getAnnotatedList($factory), $factory)->folderData('INBOX');
+        $this->assertFalse(
+            $data['default']
+        );
+    }
+
+    public function testFolderDataHasNamespace()
+    {
+        $factory = new Horde_Kolab_Storage_Factory();
+        $data = $this->getCachedQueryForList($this->getAnnotatedList($factory), $factory)->folderData('INBOX/Calendar');
+        $this->assertEquals(
+            'personal',
+            $data['namespace']
+        );
+    }
+
+    public function testFolderDataHasSubpath()
+    {
+        $factory = new Horde_Kolab_Storage_Factory();
+        $data = $this->getCachedQueryForList($this->getAnnotatedList($factory), $factory)->folderData('INBOX/Calendar');
+        $this->assertEquals(
+            'Calendar',
+            $data['subpath']
+        );
+    }
 }