plugins/libkolab
Thomas Brüderli
bruederli at kolabsys.com
Tue Feb 3 22:14:15 CET 2015
plugins/libkolab/lib/kolab_storage_folder.php | 18 ++++
plugins/libkolab/tests/README.md | 43 ++++++++++
plugins/libkolab/tests/kolab_storage_folder.php | 102 ++++++++++++++++++++++++
3 files changed, 163 insertions(+)
New commits:
commit 17e6662a23ac104f17375e08c4f286cb519a1a32
Author: Thomas Bruederli <bruederli at kolabsys.com>
Date: Tue Feb 3 22:14:06 2015 +0100
Add unit/functional tests for kolab_storage_folder error checking methods
diff --git a/plugins/libkolab/lib/kolab_storage_folder.php b/plugins/libkolab/lib/kolab_storage_folder.php
index b1546e9..ab3c63f 100644
--- a/plugins/libkolab/lib/kolab_storage_folder.php
+++ b/plugins/libkolab/lib/kolab_storage_folder.php
@@ -103,6 +103,21 @@ class kolab_storage_folder extends kolab_storage_folder_api
}
/**
+ * Check IMAP connection error state
+ */
+ public function check_error()
+ {
+ if (($err_code = $this->imap->get_error_code()) < 0) {
+ $this->error = kolab_storage::ERROR_IMAP_CONN;
+ if (($res_code = $this->imap->get_response_code()) !== 0 && in_array($res_code, array(rcube_storage::NOPERM, rcube_storage::READONLY))) {
+ $this->error = kolab_storage::ERROR_NO_PERMISSION;
+ }
+ }
+
+ return $this->error;
+ }
+
+ /**
* Compose a unique resource URI for this IMAP folder
*/
public function get_resource_uri()
@@ -166,6 +181,8 @@ class kolab_storage_folder extends kolab_storage_folder_api
if (!($success = $this->set_metadata(array(kolab_storage::UID_KEY_SHARED => $uid)))) {
$success = $this->set_metadata(array(kolab_storage::UID_KEY_PRIVATE => $uid));
}
+
+ $this->check_error();
return $success;
}
@@ -175,6 +192,7 @@ class kolab_storage_folder extends kolab_storage_folder_api
public function get_ctag()
{
$fdata = $this->get_imap_data();
+ $this->check_error();
return sprintf('%d-%d-%d', $fdata['UIDVALIDITY'], $fdata['HIGHESTMODSEQ'], $fdata['UIDNEXT']);
}
diff --git a/plugins/libkolab/tests/README.md b/plugins/libkolab/tests/README.md
new file mode 100644
index 0000000..942822b
--- /dev/null
+++ b/plugins/libkolab/tests/README.md
@@ -0,0 +1,43 @@
+libkolab plugin tests
+=====================
+
+In order to run the functional tests for libkolab classes, some configuration
+for the Roundcube test instance need to be created. Along with the default
+config for a given Roundcube instance, you should provide a config specifically
+for running tests. To do so, create a config file named `config-test.inc.php`
+in the regular Roundcube config dir. That should provide specific `db_dsnw` and
+`default_host` values for testing purposes as well as the credentials of a
+valid IMAP user account used for running the tests with.
+
+Add these config options used by the libkolab tests:
+
+```
+ // Unit tests settings
+ $config['tests_username'] = 'roundcube.test at example.org';
+ $config['tests_password'] = '<test-account-password>';
+ $config['default_host'] = '<kolab-server>';
+
+ // disable all plugins
+ $config['plugins'] = array();
+```
+
+WARNING
+-------
+Please note that the configured IMAP account as well as the Roundcube database
+configred in `db_dsnw` will be wiped and filled with test data in every test
+run. Under no circumstances you should use credentials of a production database
+or email account!
+
+
+Run the tests
+-------------
+
+The tests are based on PHPUnit and need to be exected from the Roundcube
+test directory in order to load and initialize the Roundcube framework context.
+
+To execute individual tests, call `phpunit` from the tests directory:
+
+```
+ cd <roundcube-dir>/tests/
+ phpunit ../plugins/libkolab/tests/<filename>
+```
\ No newline at end of file
diff --git a/plugins/libkolab/tests/kolab_storage_folder.php b/plugins/libkolab/tests/kolab_storage_folder.php
new file mode 100644
index 0000000..835e9c8
--- /dev/null
+++ b/plugins/libkolab/tests/kolab_storage_folder.php
@@ -0,0 +1,102 @@
+<?php
+
+/**
+ * libkolab/kolab_storage_folder class tests
+ *
+ * @author Thomas Bruederli <bruederli at kolabsys.com>
+ *
+ * Copyright (C) 2015, Kolab Systems AG <contact at kolabsys.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+class kolab_storage_folder_test extends PHPUnit_Framework_TestCase
+{
+ public static function setUpBeforeClass()
+ {
+ // load libkolab plugin
+ $rcmail = rcmail::get_instance();
+ $rcmail->plugins->load_plugin('libkolab', true, true);
+
+ if ($rcmail->config->get('tests_username')) {
+ $authenticated = $rcmail->login(
+ $rcmail->config->get('tests_username'),
+ $rcmail->config->get('tests_password'),
+ $rcmail->config->get('default_host'),
+ false
+ );
+
+ if (!$authenticated) {
+ throw new Exception('IMAP login failed for user ' . $rcmail->config->get('tests_username'));
+ }
+ }
+ else {
+ throw new Exception('Missing test account username/password in config-test.inc.php');
+ }
+
+ kolab_storage::setup();
+ }
+
+ function test_folder_type_check()
+ {
+ $folder = new kolab_storage_folder('Calendar', 'event', 'event.default');
+ $this->assertTrue($folder->valid);
+ $this->assertEquals($folder->get_error(), 0);
+
+ $folder = new kolab_storage_folder('Calendar', 'event', 'mail');
+ $this->assertFalse($folder->valid);
+ $this->assertEquals($folder->get_error(), kolab_storage::ERROR_INVALID_FOLDER);
+ }
+
+ function test_get_resource_uri()
+ {
+ $rcmail = rcmail::get_instance();
+ $foldername = 'Calendar';
+
+ $folder = new kolab_storage_folder($foldername, 'event', 'event.default');
+ $this->assertEquals($folder->get_resource_uri(), sprintf('imap://%s@%s/%s',
+ urlencode($rcmail->config->get('tests_username')),
+ $rcmail->config->get('default_host'),
+ $foldername
+ ));
+ }
+
+ function test_get_owner()
+ {
+ $rcmail = rcmail::get_instance();
+ $folder = new kolab_storage_folder('Calendar', 'event', 'event');
+ $this->assertEquals($folder->get_owner(), $rcmail->config->get('tests_username'));
+
+ $domain = preg_replace('/^.+@/', '@', $rcmail->config->get('tests_username'));
+
+ $shared_ns = kolab_storage::namespace_root('shared');
+ $folder = new kolab_storage_folder($shared_ns . 'A-shared-folder', 'event', 'event');
+ $this->assertEquals($folder->get_owner(true), 'anonymous' . $domain);
+
+ $other_ns = kolab_storage::namespace_root('other');
+ $folder = new kolab_storage_folder($other_ns . 'major.tom/Calendar', 'event', 'event');
+ $this->assertEquals($folder->get_owner(true), 'major.tom' . $domain);
+ }
+
+ function test_get_uid()
+ {
+ $rcmail = rcmail::get_instance();
+ $folder = new kolab_storage_folder('Doesnt-Exist', 'event', 'event');
+
+ // generate UID from folder name if IMAP operations fail
+ $uid1 = $folder->get_uid();
+ $this->assertEquals($folder->get_uid(), $uid1);
+ $this->assertEquals($folder->get_error(), kolab_storage::ERROR_IMAP_CONN);
+ }
+}
More information about the commits
mailing list