Implement getDefault() query.
authorGunnar Wrobel <p@rdus.de>
Sat, 8 Jan 2011 21:19:09 +0000 (22:19 +0100)
committerGunnar Wrobel <p@rdus.de>
Sat, 8 Jan 2011 21:19:09 +0000 (22:19 +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/TestCase.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 4269c7a..0f55dfe 100644 (file)
@@ -68,7 +68,7 @@ extends Horde_Kolab_Storage_Query
      *
      * @return string|boolean The name of the default folder, false if there is no default.
      */
-//    public function getDefault($type);
+    public function getDefault($type);
 
     /**
      * Get the default folder for a certain type from a different owner.
index 45c11b6..dbd0313 100644 (file)
@@ -65,8 +65,7 @@ implements Horde_Kolab_Storage_List_Query
     public function listTypes()
     {
         $result = array();
-        $list = $this->listFolderTypeAnnotations();
-        foreach ($list as $folder => $annotation) {
+        foreach ($this->listFolderTypeAnnotations() as $folder => $annotation) {
             $result[$folder] = $annotation->getType();
         }
         return $result;
@@ -81,8 +80,7 @@ implements Horde_Kolab_Storage_List_Query
     public function listFolderTypeAnnotations()
     {
         $result = array();
-        $list = $this->_list->listFolderTypes();
-        foreach ($list as $folder => $annotation) {
+        foreach ($this->_list->listFolderTypes() as $folder => $annotation) {
             $result[$folder] = $this->_factory->createFolderType($annotation);
         }
         return $result;
@@ -123,6 +121,43 @@ implements Horde_Kolab_Storage_List_Query
     }
 
     /**
+     * Get the default folder for a certain type.
+     *
+     * @param string $type The type of the share/folder.
+     *
+     * @return string|boolean The name of the default folder, false if there is no default.
+     */
+    public function getDefault($type)
+    {
+        $result = null;
+        $namespace = $this->_list->getNamespace();
+        foreach ($this->listFolderTypeAnnotations() as $folder => $annotation) {
+            if ($annotation->getType() == $type
+                && $annotation->isDefault()
+                && ($namespace->matchNamespace($folder)->getType()
+                    == Horde_Kolab_Storage_Folder_Namespace::PERSONAL)) {
+                if ($result === null) {
+                    $result = $folder;
+                } else {
+                    throw new Horde_Kolab_Storage_Exception(
+                        sprintf(
+                            'Both folders %s and %s are marked as default folder of type %s!',
+                            $result,
+                            $folder,
+                            $type
+                        )
+                    );
+                }
+            }
+        }
+        if ($result === null) {
+            return false;
+        } else {
+            return $result;
+        }
+    }
+
+    /**
      * Synchronize the query data with the information from the backend.
      *
      * @return NULL
index 714c98f..8567579 100644 (file)
@@ -37,6 +37,12 @@ implements Horde_Kolab_Storage_List_Query
     /** The folder owner list */
     const OWNERS = 'OWNERS';
 
+    /** The default folder list for the current user */
+    const PERSONAL_DEFAULTS = 'PERSONAL_DEFAULTS';
+
+    /** The default folder list */
+    const DEFAULTS = 'DEFAULTS';
+
     /**
      * The queriable list.
      *
@@ -129,6 +135,23 @@ implements Horde_Kolab_Storage_List_Query
     }
 
     /**
+     * Get the default folder for a certain type.
+     *
+     * @param string $type The type of the share/folder.
+     *
+     * @return string|boolean The name of the default folder, false if there is no default.
+     */
+    public function getDefault($type)
+    {
+        $defaults = $this->_list_cache->getQuery(self::PERSONAL_DEFAULTS);
+        if (isset($defaults[$type])) {
+            return $defaults[$type];
+        } else {
+            return false;
+        }
+    }
+
+    /**
      * Synchronize the query data with the information from the backend.
      *
      * @return NULL
@@ -151,5 +174,30 @@ implements Horde_Kolab_Storage_List_Query
             $owners[$folder] = $namespace->getOwner($folder);
         }
         $this->_list_cache->setQuery(self::OWNERS, $owners);
+
+        $personal_defaults = array();
+        $namespace = $this->_list->getNamespace();
+        foreach ($this->listFolderTypeAnnotations() as $folder => $annotation) {
+            if ($annotation->isDefault()
+                && ($namespace->matchNamespace($folder)->getType()
+                    == Horde_Kolab_Storage_Folder_Namespace::PERSONAL)) {
+                $type = $annotation->getType();
+                if (!isset($personal_defaults[$type])) {
+                    $personal_defaults[$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],
+                            $folder,
+                            $type
+                        )
+                    );
+                }
+            }
+        }
+        $this->_list_cache->setQuery(
+            self::PERSONAL_DEFAULTS, $personal_defaults
+        );
     }
 }
\ No newline at end of file
index 9121b69..d4a5b1d 100644 (file)
@@ -267,6 +267,120 @@ extends PHPUnit_Framework_TestCase
         );
     }
 
+    protected function getForeignDefaultAccount()
+    {
+        return array(
+            'username' => 'test@example.com',
+            'data' => array(
+                'user/test' => null,
+                'user/example/Calendar' => array(
+                    'annotations' => array(
+                        '/shared/vendor/kolab/folder-type' => 'event.default',
+                    )
+                ),
+                'user/someone/Calendars/Events' => array(
+                    'annotations' => array(
+                        '/shared/vendor/kolab/folder-type' => 'event.default',
+                    )
+                ),
+            )
+        );
+    }
+
+    protected function getForeignDefaultMock($factory = null)
+    {
+        $factory = $this->completeFactory($factory);
+        return new Horde_Kolab_Storage_Driver_Mock(
+            $factory,
+            $this->getForeignDefaultAccount()
+        );
+    }
+
+    protected function getForeignDefaultList($factory = null)
+    {
+        $factory = $this->completeFactory($factory);
+        return new Horde_Kolab_Storage_List_Base(
+            $this->getForeignDefaultMock($factory),
+            $factory
+        );
+    }
+
+    protected function getEventAccount()
+    {
+        return array(
+            'username' => 'test@example.com',
+            'data' => array(
+                'user/test' => null,
+                'user/test/Calendar' => array(
+                    'annotations' => array(
+                        '/shared/vendor/kolab/folder-type' => 'event',
+                    )
+                ),
+                'user/test/Events' => array(
+                    'annotations' => array(
+                        '/shared/vendor/kolab/folder-type' => 'event.default',
+                    )
+                ),
+            )
+        );
+    }
+
+    protected function getEventMock($factory = null)
+    {
+        $factory = $this->completeFactory($factory);
+        return new Horde_Kolab_Storage_Driver_Mock(
+            $factory,
+            $this->getEventAccount()
+        );
+    }
+
+    protected function getEventList($factory = null)
+    {
+        $factory = $this->completeFactory($factory);
+        return new Horde_Kolab_Storage_List_Base(
+            $this->getEventMock($factory),
+            $factory
+        );
+    }
+
+    protected function getDoubleEventAccount()
+    {
+        return array(
+            'username' => 'test@example.com',
+            'data' => array(
+                'user/test' => null,
+                'user/test/Calendar' => array(
+                    'annotations' => array(
+                        '/shared/vendor/kolab/folder-type' => 'event.default',
+                    )
+                ),
+                'user/test/Events' => array(
+                    'annotations' => array(
+                        '/shared/vendor/kolab/folder-type' => 'event.default',
+                    )
+                ),
+            )
+        );
+    }
+
+    protected function getDoubleEventMock($factory = null)
+    {
+        $factory = $this->completeFactory($factory);
+        return new Horde_Kolab_Storage_Driver_Mock(
+            $factory,
+            $this->getDoubleEventAccount()
+        );
+    }
+
+    protected function getDoubleEventList($factory = null)
+    {
+        $factory = $this->completeFactory($factory);
+        return new Horde_Kolab_Storage_List_Base(
+            $this->getDoubleEventMock($factory),
+            $factory
+        );
+    }
+
     protected function getCachedQueryForList($bare_list, $factory)
     {
         $list_cache = $this->getMockListCache();
index 21c76fc..6b3f1ef 100644 (file)
@@ -136,4 +136,72 @@ extends Horde_Kolab_Storage_TestCase
             $query->listOwners()
         );
     }
+
+    public function testDefaultReturn()
+    {
+        $factory = new Horde_Kolab_Storage_Factory();
+        $query = $factory->createListQuery('Base', $this->getAnnotatedList($factory));
+        $this->assertType(
+            'string',
+            $query->getDefault('event')
+        );
+    }
+
+    public function testDefaultCalendar()
+    {
+        $factory = new Horde_Kolab_Storage_Factory();
+        $query = $factory->createListQuery('Base', $this->getAnnotatedList($factory));
+        $this->assertEquals(
+            'INBOX/Calendar',
+            $query->getDefault('event')
+        );
+    }
+
+    public function testDefaultNotes()
+    {
+        $factory = new Horde_Kolab_Storage_Factory();
+        $query = $factory->createListQuery('Base', $this->getAnnotatedList($factory));
+        $this->assertEquals(
+            'INBOX/Notes',
+            $query->getDefault('note')
+        );
+    }
+
+    public function testMissingDefault()
+    {
+        $factory = new Horde_Kolab_Storage_Factory();
+        $query = $factory->createListQuery('Base', $this->getNullList($factory));
+        $this->assertFalse(
+            $query->getDefault('note')
+        );
+    }
+
+    public function testIgnoreForeignDefault()
+    {
+        $factory = new Horde_Kolab_Storage_Factory();
+        $query = $factory->createListQuery('Base', $this->getForeignDefaultList($factory));
+        $this->assertFalse(
+            $query->getDefault('event')
+        );
+    }
+
+    public function testIdentifyDefault()
+    {
+        $factory = new Horde_Kolab_Storage_Factory();
+        $query = $factory->createListQuery('Base', $this->getEventList($factory));
+        $this->assertEquals(
+            'INBOX/Events',
+            $query->getDefault('event')
+        );
+    }
+
+    /**
+     * @expectedException Horde_Kolab_Storage_Exception
+     */
+    public function testBailOnDoubleDefault()
+    {
+        $factory = new Horde_Kolab_Storage_Factory();
+        $query = $factory->createListQuery('Base', $this->getDoubleEventList($factory));
+        $query->getDefault('event');
+    }
 }
index c1f6df3..ed52846 100644 (file)
@@ -143,4 +143,72 @@ extends Horde_Kolab_Storage_TestCase
             $query->listOwners()
         );
     }
+
+    public function testDefaultReturn()
+    {
+        $factory = new Horde_Kolab_Storage_Factory();
+        $query = $this->getCachedQueryForList($this->getAnnotatedList($factory), $factory);
+        $this->assertType(
+            'string',
+            $query->getDefault('event')
+        );
+    }
+
+    public function testDefaultCalendar()
+    {
+        $factory = new Horde_Kolab_Storage_Factory();
+        $query = $this->getCachedQueryForList($this->getAnnotatedList($factory), $factory);
+        $this->assertEquals(
+            'INBOX/Calendar',
+            $query->getDefault('event')
+        );
+    }
+
+    public function testDefaultNotes()
+    {
+        $factory = new Horde_Kolab_Storage_Factory();
+        $query = $this->getCachedQueryForList($this->getAnnotatedList($factory), $factory);
+        $this->assertEquals(
+            'INBOX/Notes',
+            $query->getDefault('note')
+        );
+    }
+
+    public function testMissingDefault()
+    {
+        $factory = new Horde_Kolab_Storage_Factory();
+        $query = $this->getCachedQueryForList($this->getNullList($factory), $factory);
+        $this->assertFalse(
+            $query->getDefault('note')
+        );
+    }
+
+    public function testIgnoreForeignDefault()
+    {
+        $factory = new Horde_Kolab_Storage_Factory();
+        $query = $this->getCachedQueryForList($this->getForeignDefaultList($factory), $factory);
+        $this->assertFalse(
+            $query->getDefault('event')
+        );
+    }
+
+    public function testIdentifyDefault()
+    {
+        $factory = new Horde_Kolab_Storage_Factory();
+        $query = $this->getCachedQueryForList($this->getEventList($factory), $factory);
+        $this->assertEquals(
+            'INBOX/Events',
+            $query->getDefault('event')
+        );
+    }
+
+    /**
+     * @expectedException Horde_Kolab_Storage_Exception
+     */
+    public function testBailOnDoubleDefault()
+    {
+        $factory = new Horde_Kolab_Storage_Factory();
+        $query = $this->getCachedQueryForList($this->getDoubleEventList($factory), $factory);
+        $query->getDefault('event');
+    }
 }