Cache the folder types.
authorGunnar Wrobel <p@rdus.de>
Mon, 3 Jan 2011 13:06:35 +0000 (14:06 +0100)
committerGunnar Wrobel <p@rdus.de>
Tue, 4 Jan 2011 07:54:23 +0000 (08:54 +0100)
framework/Kolab_Storage/lib/Horde/Kolab/Storage/List/Decorator/Cache.php
framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/List/Decorator/CacheTest.php

index 5aa6b99..31cf8aa 100644 (file)
 class Horde_Kolab_Storage_List_Decorator_Cache
 implements Horde_Kolab_Storage_List
 {
+    /** Marks the folder list. */
+    const FOLDERS = 'F';
+
+    /** Marks the type list. */
+    const TYPES = 'T';
+
     /**
      * Decorated list handler.
      *
@@ -86,7 +92,7 @@ implements Horde_Kolab_Storage_List
         $this->_init();
         $a = $this->_cache->loadListData(
             $this->_list->getConnectionId(),
-            'FOLDERS'
+            self::FOLDERS
         );
         if (empty($a)) {
             $a = $this->_cacheFolders();
@@ -104,7 +110,7 @@ implements Horde_Kolab_Storage_List
         $list = $this->_list->listFolders();
         $this->_cache->storeListData(
             $this->_list->getConnectionId(),
-            'FOLDERS',
+            self::FOLDERS,
             $list
         );
         return $list;
@@ -118,8 +124,32 @@ implements Horde_Kolab_Storage_List
      */
     public function listFolderTypes()
     {
-        $result = $this->_list->listFolderTypes();
-        return $result;
+        $this->_init();
+        $a = $this->_cache->loadListData(
+            $this->_list->getConnectionId(),
+            self::TYPES
+        );
+        if (empty($a)) {
+            $a = $this->_cacheFolderTypes();
+        }
+        return $a;
+    }
+
+    /**
+     * Cache and returns the folder type annotation as associative array.
+     *
+     * @return array The list folder types with the folder names as key and the
+     *               folder type as values.
+     */
+    private function _cacheFolderTypes()
+    {
+        $types = $this->_list->listFolderTypes();
+        $this->_cache->storeListData(
+            $this->_list->getConnectionId(),
+            self::TYPES,
+            $types
+        );
+        return $types;
     }
 
     /**
@@ -130,5 +160,6 @@ implements Horde_Kolab_Storage_List
     public function synchronize()
     {
         $this->_cacheFolders();
+        $this->_cacheFolderTypes();
     }
 }
\ No newline at end of file
index 4aa60a2..0a9e8eb 100644 (file)
@@ -159,4 +159,98 @@ extends Horde_Kolab_Storage_TestCase
         $list->synchronize();
         $list->listFolders();
     }
+
+    public function testTypeListIsArray()
+    {
+        $list = new Horde_Kolab_Storage_List_Decorator_Cache(
+            $this->getNullList(),
+            $this->getMockCache()
+        );
+        $this->assertType('array', $list->listFolderTypes());
+    }
+
+    public function testFolderTypes()
+    {
+        $list = new Horde_Kolab_Storage_List_Decorator_Cache(
+            $this->getTwoFolderList(),
+            $this->getMockCache()
+        );
+        $this->assertEquals(
+            array(),
+            $list->listFolderTypes()
+        );
+    }
+
+    public function testMoreTypes()
+    {
+        $list = new Horde_Kolab_Storage_List_Decorator_Cache(
+            $this->getAnnotatedList(),
+            $this->getMockCache()
+        );
+        $this->assertEquals(
+            array(
+                'INBOX/Calendar' => 'event.default',
+                'INBOX/Contacts' => 'contact.default',
+                'INBOX/Notes' => 'note.default',
+                'INBOX/Tasks' => 'task.default'
+            ),
+            $list->listFolderTypes()
+        );
+    }
+
+    public function testMockedTypes()
+    {
+        $list = new Horde_Kolab_Storage_List_Decorator_Cache(
+            $this->getMockDriverList(),
+            $this->getMockCache()
+        );
+        $this->mockDriver->expects($this->once())
+            ->method('listAnnotation')
+            ->will($this->returnValue(array('INBOX' => 'mail.default')));
+        $this->assertEquals(
+            array('INBOX' => 'mail.default'),
+            $list->listFolderTypes()
+        );
+    }
+
+    public function testCachedTypes()
+    {
+        $list = new Horde_Kolab_Storage_List_Decorator_Cache(
+            $this->getMockDriverList(),
+            $this->getMockCache()
+        );
+        $this->mockDriver->expects($this->once())
+            ->method('listAnnotation')
+            ->will($this->returnValue(array('INBOX' => 'mail.default')));
+        $list->listFolderTypes();
+        $this->assertEquals(
+            array('INBOX' => 'mail.default'),
+            $list->listFolderTypes()
+        );
+    }
+
+    public function testSynchronizeTypes()
+    {
+        $list = new Horde_Kolab_Storage_List_Decorator_Cache(
+            $this->getMockDriverList(),
+            $this->getMockCache()
+        );
+        $this->mockDriver->expects($this->once())
+            ->method('listAnnotation')
+            ->will($this->returnValue(array('INBOX' => 'mail.default')));
+        $list->synchronize();
+    }
+
+    public function testSynchronizeTypeCache()
+    {
+        $list = new Horde_Kolab_Storage_List_Decorator_Cache(
+            $this->getMockDriverList(),
+            $this->getMockCache()
+        );
+        $this->mockDriver->expects($this->once())
+            ->method('listAnnotation')
+            ->will($this->returnValue(array('INBOX' => 'mail.default')));
+        $list->synchronize();
+        $list->listFolderTypes();
+    }
 }