From 3239eeb947ad0866087f257b79f94a81f40d79ce Mon Sep 17 00:00:00 2001 From: Aleksander Machniak Date: Tue, 21 Jul 2015 04:36:34 -0400 Subject: [PATCH] Don't use private namespace for folder uniqueid annotations (T491) --- plugins/libkolab/lib/kolab_storage.php | 1 - plugins/libkolab/lib/kolab_storage_folder.php | 25 ++++++----- .../libkolab/tests/kolab_storage_folder.php | 42 ++++++++++++++++++- 3 files changed, 55 insertions(+), 13 deletions(-) diff --git a/plugins/libkolab/lib/kolab_storage.php b/plugins/libkolab/lib/kolab_storage.php index 6ece5b49..9bafbe99 100644 --- a/plugins/libkolab/lib/kolab_storage.php +++ b/plugins/libkolab/lib/kolab_storage.php @@ -32,7 +32,6 @@ class kolab_storage const NAME_KEY_SHARED = '/shared/vendor/kolab/displayname'; const NAME_KEY_PRIVATE = '/private/vendor/kolab/displayname'; const UID_KEY_SHARED = '/shared/vendor/kolab/uniqueid'; - const UID_KEY_PRIVATE = '/private/vendor/kolab/uniqueid'; const UID_KEY_CYRUS = '/shared/vendor/cmu/cyrus-imapd/uniqueid'; const ERROR_IMAP_CONN = 1; diff --git a/plugins/libkolab/lib/kolab_storage_folder.php b/plugins/libkolab/lib/kolab_storage_folder.php index 56925447..8d86d70f 100644 --- a/plugins/libkolab/lib/kolab_storage_folder.php +++ b/plugins/libkolab/lib/kolab_storage_folder.php @@ -154,19 +154,24 @@ class kolab_storage_folder extends kolab_storage_folder_api public function get_uid() { // UID is defined in folder METADATA - $metakeys = array(kolab_storage::UID_KEY_SHARED, kolab_storage::UID_KEY_PRIVATE, kolab_storage::UID_KEY_CYRUS); + $metakeys = array(kolab_storage::UID_KEY_SHARED, kolab_storage::UID_KEY_CYRUS); $metadata = $this->get_metadata($metakeys); - foreach ($metakeys as $key) { - if (($uid = $metadata[$key])) { + + if ($metadata !== null) { + foreach ($metakeys as $key) { + if ($uid = $metadata[$key]) { + return $uid; + } + } + + // generate a folder UID and set it to IMAP + $uid = rtrim(chunk_split(md5($this->name . $this->get_owner() . uniqid('-', true)), 12, '-'), '-'); + if ($this->set_uid($uid)) { return $uid; } } - // generate a folder UID and set it to IMAP - $uid = rtrim(chunk_split(md5($this->name . $this->get_owner() . uniqid('-', true)), 12, '-'), '-'); - if ($this->set_uid($uid)) { - return $uid; - } + $this->check_error(); // create hash from folder name if we can't write the UID metadata return md5($this->name . $this->get_owner()); @@ -180,9 +185,7 @@ class kolab_storage_folder extends kolab_storage_folder_api */ public function set_uid($uid) { - if (!($success = $this->set_metadata(array(kolab_storage::UID_KEY_SHARED => $uid)))) { - $success = $this->set_metadata(array(kolab_storage::UID_KEY_PRIVATE => $uid)); - } + $success = $this->set_metadata(array(kolab_storage::UID_KEY_SHARED => $uid)); $this->check_error(); return $success; diff --git a/plugins/libkolab/tests/kolab_storage_folder.php b/plugins/libkolab/tests/kolab_storage_folder.php index 845244f4..ed42b9aa 100644 --- a/plugins/libkolab/tests/kolab_storage_folder.php +++ b/plugins/libkolab/tests/kolab_storage_folder.php @@ -135,7 +135,7 @@ class kolab_storage_folder_test extends PHPUnit_Framework_TestCase function test_006_activate() { - $folder = new kolab_storage_folder('Calendar', 'contact'); + $folder = new kolab_storage_folder('Calendar', 'event'); $this->assertTrue($folder->activate(true)); $this->assertTrue($folder->is_active()); @@ -167,4 +167,44 @@ class kolab_storage_folder_test extends PHPUnit_Framework_TestCase $folder = new kolab_storage_folder('Contacts', 'contact'); $this->assertEquals($folder->count(), 1); } + + function test_T491_get_uid() + { + $rcmail = rcmail::get_instance(); + $imap = $rcmail->get_storage(); + $db = $rcmail->get_dbh(); + + // clear cache + //$imap->clear_cache('mailboxes.metadata', true); + + // get folder UID + $folder = new kolab_storage_folder('Calendar', 'event', 'event'); + $uid = $folder->get_uid(); + + // now get folder uniqueid annotations + $annotations = array( + 'cyrus' => kolab_storage::UID_KEY_CYRUS, + 'shared' => kolab_storage::UID_KEY_SHARED, + 'private' => '/private/vendor/kolab/uniqueid', + ); + foreach ($annotations as $key => $annotation) { + $meta = $imap->get_metadata('Calendar', $annotation); + $annotations[$key] = $meta['Calendar'][$annotation]; + } + + // compare results + if ($annotations['shared']) { + $this->assertSame($annotations['shared'], $uid); + } + else if ($annotations['cyrus']) { + $this->assertSame($annotations['cyrus'], $uid); + } + else { + // never use private namespace + $this->assertTrue($annotations['private'] != $uid); + } + + // @TODO: check if the cache contains valid entries, not so simple with memcache + // as the cache key name is quite internal to the rcube_imap class. + } }