From 05e2c8f3fd19f57eaaed5d70ffc3fc15bdf19cac Mon Sep 17 00:00:00 2001 From: Gunnar Wrobel Date: Sat, 8 Jan 2011 22:19:09 +0100 Subject: [PATCH] Implement getDefault() query. --- .../lib/Horde/Kolab/Storage/List/Query.php | 2 +- .../lib/Horde/Kolab/Storage/List/Query/Base.php | 43 +++++++- .../lib/Horde/Kolab/Storage/List/Query/Cache.php | 48 +++++++++ .../test/Horde/Kolab/Storage/TestCase.php | 114 +++++++++++++++++++++ .../Kolab/Storage/Unit/List/Query/BaseTest.php | 68 ++++++++++++ .../Kolab/Storage/Unit/List/Query/CacheTest.php | 68 ++++++++++++ 6 files changed, 338 insertions(+), 5 deletions(-) diff --git a/framework/Kolab_Storage/lib/Horde/Kolab/Storage/List/Query.php b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/List/Query.php index 4269c7a37..0f55dfee0 100644 --- a/framework/Kolab_Storage/lib/Horde/Kolab/Storage/List/Query.php +++ b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/List/Query.php @@ -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. diff --git a/framework/Kolab_Storage/lib/Horde/Kolab/Storage/List/Query/Base.php b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/List/Query/Base.php index 45c11b6c3..dbd03133d 100644 --- a/framework/Kolab_Storage/lib/Horde/Kolab/Storage/List/Query/Base.php +++ b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/List/Query/Base.php @@ -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 diff --git a/framework/Kolab_Storage/lib/Horde/Kolab/Storage/List/Query/Cache.php b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/List/Query/Cache.php index 714c98f46..856757977 100644 --- a/framework/Kolab_Storage/lib/Horde/Kolab/Storage/List/Query/Cache.php +++ b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/List/Query/Cache.php @@ -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 diff --git a/framework/Kolab_Storage/test/Horde/Kolab/Storage/TestCase.php b/framework/Kolab_Storage/test/Horde/Kolab/Storage/TestCase.php index 9121b692f..d4a5b1d8a 100644 --- a/framework/Kolab_Storage/test/Horde/Kolab/Storage/TestCase.php +++ b/framework/Kolab_Storage/test/Horde/Kolab/Storage/TestCase.php @@ -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(); diff --git a/framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/List/Query/BaseTest.php b/framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/List/Query/BaseTest.php index 21c76fcb8..6b3f1ef87 100644 --- a/framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/List/Query/BaseTest.php +++ b/framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/List/Query/BaseTest.php @@ -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'); + } } diff --git a/framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/List/Query/CacheTest.php b/framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/List/Query/CacheTest.php index c1f6df303..ed52846c1 100644 --- a/framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/List/Query/CacheTest.php +++ b/framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/List/Query/CacheTest.php @@ -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'); + } } -- 2.11.0