summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorGunnar Wrobel <wrobel@pardus.de>2010-04-07 07:28:40 (GMT)
committerGunnar Wrobel <wrobel@pardus.de>2010-04-07 07:28:40 (GMT)
commit9d409a4e7fbf63740ee55a87afa75ee17b06bac2 (patch)
tree50830aeb70fba63a93298ead7a5d96f528daaf81 /test
parent3dfc6be617f22500e903a99e89e9339d6833e250 (diff)
downloadkolab-webadmin-9d409a4e7fbf63740ee55a87afa75ee17b06bac2.tar.gz
kolab/issue1446 (Webinterface for setting vacation, email-delivery and forwarding (rt#5033))
This also adds the tests that drove the implementation.
Diffstat (limited to 'test')
-rw-r--r--test/KolabAdmin/Autoload.php30
-rw-r--r--test/KolabAdmin/Unit/Sieve/Segment/DeliveryTest.php188
-rw-r--r--test/KolabAdmin/Unit/Sieve/Segment/ForwardTest.php268
-rw-r--r--test/KolabAdmin/Unit/Sieve/Segment/VacationTest.php414
-rw-r--r--test/KolabAdmin/Unit/SieveTest.php291
5 files changed, 1191 insertions, 0 deletions
diff --git a/test/KolabAdmin/Autoload.php b/test/KolabAdmin/Autoload.php
new file mode 100644
index 0000000..60ee9ac
--- /dev/null
+++ b/test/KolabAdmin/Autoload.php
@@ -0,0 +1,30 @@
+<?php
+/**
+ * Setup autoloading for the tests.
+ *
+ * PHP version 5
+ *
+ * @category Kolab
+ * @package KolabAdmin
+ * @author Gunnar Wrobel <wrobel@pardus.de>
+ * @license http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link http://pear.horde.org/index.php?package=KolabAdmin
+ */
+
+ini_set('include_path', dirname(__FILE__) . '/../../lib' . PATH_SEPARATOR . ini_get('include_path'));
+
+if (!spl_autoload_functions()) {
+ spl_autoload_register(
+ create_function(
+ '$class',
+ '$filename = str_replace(array(\'::\', \'_\'), \'/\', $class);'
+ . '$err_mask = E_ALL ^ E_WARNING;'
+ . '$oldErrorReporting = error_reporting($err_mask);'
+ . 'include "$filename.php";'
+ . 'error_reporting($oldErrorReporting);'
+ )
+ );
+}
+
+/** Catch strict standards */
+error_reporting(E_ALL | E_STRICT);
diff --git a/test/KolabAdmin/Unit/Sieve/Segment/DeliveryTest.php b/test/KolabAdmin/Unit/Sieve/Segment/DeliveryTest.php
new file mode 100644
index 0000000..3acd758
--- /dev/null
+++ b/test/KolabAdmin/Unit/Sieve/Segment/DeliveryTest.php
@@ -0,0 +1,188 @@
+<?php
+/**
+ * Test the sieve script delivery segment.
+ *
+ * PHP version 5
+ *
+ * @category Kolab
+ * @package KolabAdmin
+ * @author Gunnar Wrobel <wrobel@pardus.de>
+ * @license http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link http://pear.horde.org/index.php?package=KolabAdmin
+ */
+
+/**
+ * Require the tested classes.
+ */
+require_once dirname(__FILE__) . '/../../../Autoload.php';
+
+/**
+ * Test the sieve script delivery segment.
+ *
+ * Copyright 2010 Klarälvdalens Datakonsult AB
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @category Kolab
+ * @package KolabAdmin
+ * @author Gunnar Wrobel <wrobel@pardus.de>
+ * @license http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link http://pear.horde.org/index.php?package=KolabAdmin
+ */
+class KolabAdmin_Unit_Sieve_Segment_DeliveryTest extends PHPUnit_Framework_TestCase
+{
+ public function setUp()
+ {
+ $this->sieve = $this->getMock('Net_Sieve');
+ $this->manager = new KolabAdmin_Sieve($this->sieve);
+ }
+
+ public function testSieveHandlerAllowsFetchingDeliverySegment()
+ {
+ $this->assertType(
+ 'KolabAdmin_Sieve_Segment_Delivery',
+ $this->manager->fetchDeliverySegment()
+ );
+ }
+
+ public function testSieveHandlerIndicatesThatTheDeliverySegmentIsActiveIfAnOldActiveScriptWasFound()
+ {
+ $this->_provideActiveScript(
+ 'kolab-deliver.siv', $this->_getOldDeliveryScript()
+ );
+ $this->assertTrue(
+ $this->manager->fetchDeliverySegment()->isActive()
+ );
+ }
+
+ public function testSieveHandlerIndicatesThatTheDeliverySegmentIsInactiveIfAnOldInactiveScriptWasFound()
+ {
+ $this->_provideInactiveScript(
+ 'kolab-deliver.siv', $this->_getOldDeliveryScript()
+ );
+ $this->assertFalse(
+ $this->manager->fetchDeliverySegment()->isActive()
+ );
+ }
+
+ public function testSieveHandlerIndicatesThatTheDeliverySegmentIsActiveIfANewActiveScriptWasFound()
+ {
+ $this->_provideInactiveScript(
+ 'kolab.siv', $this->_getDeliveryScript()
+ );
+ $this->assertTrue(
+ $this->manager->fetchDeliverySegment()->isActive()
+ );
+ }
+
+ public function testSieveHandlerIndicatesAnInactiveDeliverySegmentIfNoActiveScriptWasFound()
+ {
+ $this->sieve->expects($this->once())
+ ->method('listScripts')
+ ->will($this->returnValue(array()));
+ $this->assertFalse(
+ $this->manager->fetchDeliverySegment()->isActive()
+ );
+ }
+
+ public function testSieveHandlerIndicatesAnInactiveDeliverySegmentIfAnInactiveScriptWasFound()
+ {
+ $this->_provideInactiveScript(
+ 'kolab.siv', $this->_getDeliveryScript('Test', false)
+ );
+ $this->assertFalse(
+ $this->manager->fetchDeliverySegment()->isActive()
+ );
+ }
+
+ public function testSieveHandlerAllowsActivatingDeliverySegment()
+ {
+ $segment = $this->manager->fetchDeliverySegment();
+ $segment->setActive();
+ $this->assertEquals(
+ $this->_getDeliveryScript('Inbox'),
+ $segment->generate()
+ );
+ }
+
+ public function testSieveHandlerAllowsDeactivatingDeliverySegment()
+ {
+ $segment = $this->manager->fetchDeliverySegment();
+ $segment->setInactive();
+ $this->assertEquals(
+ $this->_getDeliveryScript('Inbox', false),
+ $segment->generate()
+ );
+ }
+
+ public function testOldSieveSegmentDeliveryProvidesDeliveryFolder()
+ {
+ $this->_provideActiveScript(
+ 'kolab-deliver.siv', $this->_getOldDeliveryScript()
+ );
+ $segment = $this->manager->fetchDeliverySegment();
+ $this->assertEquals('Test', $segment->getDeliveryFolder());
+ }
+
+ public function testSieveSegmentDeliveryProvidesDeliveryFolder()
+ {
+ $this->_provideInactiveScript(
+ 'kolab.siv', $this->_getDeliveryScript('Test', false)
+ );
+ $segment = $this->manager->fetchDeliverySegment();
+ $this->assertEquals('Test', $segment->getDeliveryFolder());
+ }
+
+ public function testSieveSegmentDeliveryAllowsSettingDeliveryFolder()
+ {
+ $segment = $this->manager->fetchDeliverySegment();
+ $segment->setActive();
+ $segment->setDeliveryFolder('Dummy');
+ $this->assertEquals(
+ $this->_getDeliveryScript('Dummy'),
+ $segment->generate()
+ );
+ }
+
+ private function _provideInactiveScript($name, $script)
+ {
+ $this->sieve->expects($this->once())
+ ->method('listScripts')
+ ->will($this->returnValue(array($name)));
+ $this->sieve->expects($this->once())
+ ->method('getScript')
+ ->with($name)
+ ->will($this->returnValue($script));
+ }
+
+ private function _provideActiveScript($name, $script)
+ {
+ $this->_provideInactiveScript($name, $script);
+ $this->sieve->expects($this->once())
+ ->method('getActive')
+ ->will($this->returnValue($name));
+ }
+
+ private function _getOldDeliveryScript()
+ {
+ return 'require "fileinto";' . "\r\n" .
+ 'if header :contains ["X-Kolab-Scheduling-Message"] ["FALSE"] {' . "\r\n" .
+ 'fileinto "INBOX/Test";' . "\r\n" .
+ '}' . "\r\n";
+ }
+
+ private function _getDeliveryScript($box = 'Test', $active = true)
+ {
+ $script = 'if allof (' . (($active) ? 'true, ## delivery enabled' : 'false, ## delivery disabled') . "\r\n" .
+ 'header :contains ["X-Kolab-Scheduling-Message"] ["FALSE"]) {' . "\r\n" .
+ 'fileinto "INBOX/' . $box . '";' . "\r\n" .
+ '}' . "\r\n";
+ if (!$active) {
+ $script = preg_replace('/^(.)/m', '#$1', $script);
+ }
+ return '### SEGMENT START DELIVERY ' . (($active) ? 'ENABLED' : 'DISABLED') . "\r\n" .
+ $script .
+ '### SEGMENT END DELIVERY' . "\r\n";
+ }
+} \ No newline at end of file
diff --git a/test/KolabAdmin/Unit/Sieve/Segment/ForwardTest.php b/test/KolabAdmin/Unit/Sieve/Segment/ForwardTest.php
new file mode 100644
index 0000000..f82cb28
--- /dev/null
+++ b/test/KolabAdmin/Unit/Sieve/Segment/ForwardTest.php
@@ -0,0 +1,268 @@
+<?php
+/**
+ * Test the sieve script forward segment.
+ *
+ * PHP version 5
+ *
+ * @category Kolab
+ * @package KolabAdmin
+ * @author Gunnar Wrobel <wrobel@pardus.de>
+ * @license http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link http://pear.horde.org/index.php?package=KolabAdmin
+ */
+
+/**
+ * Require the tested classes.
+ */
+require_once dirname(__FILE__) . '/../../../Autoload.php';
+
+/**
+ * Test the sieve script forward segment.
+ *
+ * Copyright 2010 Klarälvdalens Datakonsult AB
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @category Kolab
+ * @package KolabAdmin
+ * @author Gunnar Wrobel <wrobel@pardus.de>
+ * @license http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link http://pear.horde.org/index.php?package=KolabAdmin
+ */
+class KolabAdmin_Unit_Sieve_Segment_ForwardTest extends PHPUnit_Framework_TestCase
+{
+ public function setUp()
+ {
+ $this->sieve = $this->getMock('Net_Sieve');
+ $this->manager = new KolabAdmin_Sieve($this->sieve);
+ }
+
+ public function testSieveHandlerAllowsFetchingForwardSegment()
+ {
+ $this->assertType(
+ 'KolabAdmin_Sieve_Segment_Forward',
+ $this->manager->fetchForwardSegment()
+ );
+ }
+
+ public function testSieveHandlerIndicatesThatTheForwardSegmentIsActiveIfAnOldActiveScriptWasFound()
+ {
+ $this->_provideActiveScript(
+ 'kolab-forward.siv', $this->_getOldForwardScript()
+ );
+ $this->assertTrue(
+ $this->manager->fetchForwardSegment()->isActive()
+ );
+ }
+
+ public function testSieveHandlerIndicatesThatTheForwardSegmentIsInactiveIfAnOldInactiveScriptWasFound()
+ {
+ $this->_provideInactiveScript(
+ 'kolab-forward.siv', $this->_getOldForwardScript()
+ );
+ $this->assertFalse(
+ $this->manager->fetchForwardSegment()->isActive()
+ );
+ }
+
+ public function testSieveHandlerIndicatesThatTheForwardSegmentIsActiveIfANewActiveScriptWasFound()
+ {
+ $this->_provideInactiveScript(
+ 'kolab.siv', $this->_getForwardScript()
+ );
+ $this->assertTrue(
+ $this->manager->fetchForwardSegment()->isActive()
+ );
+ }
+
+ public function testSieveHandlerIndicatesAnInactiveForwardSegmentIfNoActiveScriptWasFound()
+ {
+ $this->sieve->expects($this->once())
+ ->method('listScripts')
+ ->will($this->returnValue(array()));
+ $this->assertFalse(
+ $this->manager->fetchForwardSegment()->isActive()
+ );
+ }
+
+ public function testSieveHandlerIndicatesAnInactiveForwardSegmentIfAnInactiveScriptWasFound()
+ {
+ $this->sieve->expects($this->once())
+ ->method('listScripts')
+ ->will($this->returnValue(array('kolab.siv')));
+ $this->sieve->expects($this->once())
+ ->method('getScript')
+ ->with('kolab.siv')
+ ->will($this->returnValue($this->_getForwardScript('test@example.com', false)));
+ $this->assertFalse(
+ $this->manager->fetchForwardSegment()->isActive()
+ );
+ }
+
+ public function testSieveHandlerAllowsActivatingForwardSegment()
+ {
+ $segment = $this->manager->fetchForwardSegment();
+ $segment->setActive();
+ $segment->setForwardAddress('somebody@example.com');
+ $this->assertEquals(
+ $this->_getForwardScript('somebody@example.com'),
+ $segment->generate()
+ );
+ }
+
+ public function testSieveHandlerAllowsDeactivatingForwardSegment()
+ {
+ $segment = $this->manager->fetchForwardSegment();
+ $segment->setInactive();
+ $segment->setForwardAddress('somebody@example.com');
+ $this->assertEquals(
+ $this->_getForwardScript('somebody@example.com', false),
+ $segment->generate()
+ );
+ }
+
+ public function testOldSieveSegmentForwardProvidesForwardAddress()
+ {
+ $this->sieve->expects($this->once())
+ ->method('listScripts')
+ ->will($this->returnValue(array('kolab-forward.siv')));
+ $this->sieve->expects($this->once())
+ ->method('getScript')
+ ->with('kolab-forward.siv')
+ ->will($this->returnValue($this->_getOldForwardScript()));
+ $segment = $this->manager->fetchForwardSegment();
+ $this->assertEquals('test@example.org', $segment->getForwardAddress());
+ }
+
+ public function testSieveSegmentForwardProvidesForwardAddress()
+ {
+ $this->sieve->expects($this->once())
+ ->method('listScripts')
+ ->will($this->returnValue(array('kolab.siv')));
+ $this->sieve->expects($this->once())
+ ->method('getScript')
+ ->with('kolab.siv')
+ ->will($this->returnValue($this->_getForwardScript('dummy@example.com', false)));
+ $segment = $this->manager->fetchForwardSegment();
+ $this->assertEquals('dummy@example.com', $segment->getForwardAddress());
+ }
+
+ public function testSieveSegmentForwardAllowsSettingForwardAddress()
+ {
+ $segment = $this->manager->fetchForwardSegment();
+ $segment->setActive();
+ $segment->setForwardAddress('dummy@example.com');
+ $this->assertEquals(
+ $this->_getForwardScript('dummy@example.com'),
+ $segment->generate()
+ );
+ }
+
+ public function testOldSieveSegmentForwardProvidesKeepOnServerAttributeTrueIfSetToTrue()
+ {
+ $this->sieve->expects($this->once())
+ ->method('listScripts')
+ ->will($this->returnValue(array('kolab-forward.siv')));
+ $this->sieve->expects($this->once())
+ ->method('getScript')
+ ->with('kolab-forward.siv')
+ ->will($this->returnValue($this->_getOldForwardScript()));
+ $segment = $this->manager->fetchForwardSegment();
+ $this->assertTrue($segment->getKeepOnServer());
+ }
+
+ public function testOldSieveSegmentForwardProvidesKeepOnServerAttributeFalseIfSetToFalse()
+ {
+ $this->sieve->expects($this->once())
+ ->method('listScripts')
+ ->will($this->returnValue(array('kolab-forward.siv')));
+ $this->sieve->expects($this->once())
+ ->method('getScript')
+ ->with('kolab-forward.siv')
+ ->will($this->returnValue($this->_getOldForwardScript(false)));
+ $segment = $this->manager->fetchForwardSegment();
+ $this->assertFalse($segment->getKeepOnServer());
+ }
+
+ public function testSieveSegmentForwardProvidesKeepOnServerAttributeTrueIfSetToTrue()
+ {
+ $this->sieve->expects($this->once())
+ ->method('listScripts')
+ ->will($this->returnValue(array('kolab.siv')));
+ $this->sieve->expects($this->once())
+ ->method('getScript')
+ ->with('kolab.siv')
+ ->will($this->returnValue($this->_getForwardScript('dummy@example.com', false, true)));
+ $segment = $this->manager->fetchForwardSegment();
+ $this->assertTrue($segment->getKeepOnServer());
+ }
+
+ public function testSieveSegmentForwardAllowsSettingKeepOnServerToTrue()
+ {
+ $segment = $this->manager->fetchForwardSegment();
+ $segment->setActive();
+ $segment->setForwardAddress('dummy@example.com');
+ $segment->setKeepOnServer(true);
+ $this->assertEquals(
+ $this->_getForwardScript('dummy@example.com', true, true),
+ $segment->generate()
+ );
+ }
+
+ public function testSieveSegmentForwardAllowsSettingKeepOnServerToFalse()
+ {
+ $segment = $this->manager->fetchForwardSegment();
+ $segment->setActive();
+ $segment->setKeepOnServer(false);
+ $segment->setForwardAddress('dummy@example.com');
+ $this->assertEquals(
+ $this->_getForwardScript('dummy@example.com', true, false),
+ $segment->generate()
+ );
+ }
+
+ private function _provideInactiveScript($name, $script)
+ {
+ $this->sieve->expects($this->once())
+ ->method('listScripts')
+ ->will($this->returnValue(array($name)));
+ $this->sieve->expects($this->once())
+ ->method('getScript')
+ ->with($name)
+ ->will($this->returnValue($script));
+ }
+
+ private function _provideActiveScript($name, $script)
+ {
+ $this->_provideInactiveScript($name, $script);
+ $this->sieve->expects($this->once())
+ ->method('getActive')
+ ->will($this->returnValue($name));
+ }
+
+ private function _getOldForwardScript($keep_on_server = true)
+ {
+ return 'require "fileinto";' . "\r\n" .
+ 'redirect "test@example.org";' . (($keep_on_server) ? ' keep;' : '') . "\r\n";
+ }
+
+ private function _getForwardScript($address = 'test@example.com', $active = true, $keep_on_server = true)
+ {
+ if (!empty($address)) {
+ $address = 'redirect "' . $address . '";';
+ } else {
+ $address = '';
+ }
+ $script = 'if allof (' . (($active) ? 'true ## forward enabled' : 'false ## forward disabled') . "\r\n" .
+ ') {' . "\r\n" .
+ $address . (($keep_on_server) ? ' keep;' : '') . "\r\n" .
+ '}' . "\r\n";
+ if (!$active) {
+ $script = preg_replace('/^(.)/m', '#$1', $script);
+ }
+ return '### SEGMENT START FORWARD ' . (($active) ? 'ENABLED' : 'DISABLED') . "\r\n" .
+ $script .
+ '### SEGMENT END FORWARD' . "\r\n";
+ }
+} \ No newline at end of file
diff --git a/test/KolabAdmin/Unit/Sieve/Segment/VacationTest.php b/test/KolabAdmin/Unit/Sieve/Segment/VacationTest.php
new file mode 100644
index 0000000..503c6a4
--- /dev/null
+++ b/test/KolabAdmin/Unit/Sieve/Segment/VacationTest.php
@@ -0,0 +1,414 @@
+<?php
+/**
+ * Test the sieve script vacation segment.
+ *
+ * PHP version 5
+ *
+ * @category Kolab
+ * @package KolabAdmin
+ * @author Gunnar Wrobel <wrobel@pardus.de>
+ * @license http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link http://pear.horde.org/index.php?package=KolabAdmin
+ */
+
+/**
+ * Require the tested classes.
+ */
+require_once dirname(__FILE__) . '/../../../Autoload.php';
+
+/**
+ * Test the sieve script vacation segment.
+ *
+ * Copyright 2010 Klarälvdalens Datakonsult AB
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @category Kolab
+ * @package KolabAdmin
+ * @author Gunnar Wrobel <wrobel@pardus.de>
+ * @license http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link http://pear.horde.org/index.php?package=KolabAdmin
+ */
+class KolabAdmin_Unit_Sieve_Segment_VacationTest extends PHPUnit_Framework_TestCase
+{
+ public function setUp()
+ {
+ $this->sieve = $this->getMock('Net_Sieve');
+ $this->manager = new KolabAdmin_Sieve($this->sieve);
+ }
+
+ public function testSieveHandlerAllowsFetchingVacationSegment()
+ {
+ $this->assertType(
+ 'KolabAdmin_Sieve_Segment_Vacation',
+ $this->manager->fetchVacationSegment()
+ );
+ }
+
+ public function testSieveHandlerIndicatesThatTheVacationSegmentIsActiveIfAnOldActiveScriptWasFound()
+ {
+ $this->_provideActiveScript(
+ 'kolab-vacation.siv', $this->_getOldVacationScript()
+ );
+ $this->assertTrue(
+ $this->manager->fetchVacationSegment()->isActive()
+ );
+ }
+
+ public function testSieveHandlerIndicatesThatTheVacationSegmentIsInactiveIfAnOldInactiveScriptWasFound()
+ {
+ $this->_provideInactiveScript(
+ 'kolab-vacation.siv', $this->_getOldVacationScript()
+ );
+ $this->assertFalse(
+ $this->manager->fetchVacationSegment()->isActive()
+ );
+ }
+
+ public function testSieveHandlerIndicatesThatTheVacationSegmentIsActiveIfANewActiveScriptWasFound()
+ {
+ $this->_provideInactiveScript(
+ 'kolab.siv', $this->_getVacationScript()
+ );
+ $this->assertTrue(
+ $this->manager->fetchVacationSegment()->isActive()
+ );
+ }
+
+ public function testSieveHandlerIndicatesAnInactiveVacationSegmentIfNoActiveScriptWasFound()
+ {
+ $this->sieve->expects($this->once())
+ ->method('listScripts')
+ ->will($this->returnValue(array()));
+ $this->assertFalse(
+ $this->manager->fetchVacationSegment()->isActive()
+ );
+ }
+
+ public function testSieveHandlerIndicatesAnInactiveVacationSegmentIfAnInactiveScriptWasFound()
+ {
+ $this->sieve->expects($this->once())
+ ->method('listScripts')
+ ->will($this->returnValue(array('kolab.siv')));
+ $this->sieve->expects($this->once())
+ ->method('getScript')
+ ->with('kolab.siv')
+ ->will($this->returnValue($this->_getVacationScript(false)));
+ $this->assertFalse(
+ $this->manager->fetchVacationSegment()->isActive()
+ );
+ }
+
+ public function testSieveHandlerAllowsActivatingVacationSegment()
+ {
+ $segment = $this->manager->fetchVacationSegment();
+ $segment->setActive();
+ $this->assertEquals(
+ $this->_getVacationScript(),
+ $segment->generate()
+ );
+ }
+
+ public function testSieveHandlerAllowsDeactivatingVacationSegment()
+ {
+ $segment = $this->manager->fetchVacationSegment();
+ $segment->setInactive();
+ $this->assertEquals(
+ $this->_getVacationScript(false),
+ $segment->generate()
+ );
+ }
+
+ public function testOldSieveSegmentVacationProvidesResponse()
+ {
+ $this->sieve->expects($this->once())
+ ->method('listScripts')
+ ->will($this->returnValue(array('kolab-vacation.siv')));
+ $this->sieve->expects($this->once())
+ ->method('getScript')
+ ->with('kolab-vacation.siv')
+ ->will($this->returnValue($this->_getOldVacationScript("REPLY\r\nLINE2\r\n")));
+ $segment = $this->manager->fetchVacationSegment();
+ $this->assertEquals("REPLY\r\nLINE2", $segment->getResponse());
+ }
+
+ public function testSieveSegmentVacationProvidesResponse()
+ {
+ $this->sieve->expects($this->once())
+ ->method('listScripts')
+ ->will($this->returnValue(array('kolab.siv')));
+ $this->sieve->expects($this->once())
+ ->method('getScript')
+ ->with('kolab.siv')
+ ->will($this->returnValue($this->_getVacationScript(true, "REPLY\r\nLINE2\r\n")));
+ $segment = $this->manager->fetchVacationSegment();
+ $this->assertEquals("REPLY\r\nLINE2", $segment->getResponse());
+ }
+
+ public function testSieveSegmentVacationAllowsSettingResponse()
+ {
+ $segment = $this->manager->fetchVacationSegment();
+ $segment->setActive();
+ $segment->setResponse("REPLY\r\nLINE2");
+ $this->assertEquals(
+ $this->_getVacationScript(true, "REPLY\r\nLINE2"),
+ $segment->generate()
+ );
+ }
+
+ public function testOldSieveSegmentVacationProvidesResendAfter()
+ {
+ $this->sieve->expects($this->once())
+ ->method('listScripts')
+ ->will($this->returnValue(array('kolab-vacation.siv')));
+ $this->sieve->expects($this->once())
+ ->method('getScript')
+ ->with('kolab-vacation.siv')
+ ->will($this->returnValue($this->_getOldVacationScript('', '', false, array(), 8)));
+ $segment = $this->manager->fetchVacationSegment();
+ $this->assertEquals(8, $segment->getResendAfter());
+ }
+
+ public function testSieveSegmentVacationProvidesResendAfter()
+ {
+ $this->sieve->expects($this->once())
+ ->method('listScripts')
+ ->will($this->returnValue(array('kolab.siv')));
+ $this->sieve->expects($this->once())
+ ->method('getScript')
+ ->with('kolab.siv')
+ ->will($this->returnValue($this->_getVacationScript(true, '', '', false, array(), 12)));
+ $segment = $this->manager->fetchVacationSegment();
+ $this->assertEquals(12, $segment->getResendAfter());
+ }
+
+ public function testSieveSegmentVacationAllowsSettingResendAfter()
+ {
+ $segment = $this->manager->fetchVacationSegment();
+ $segment->setActive();
+ $segment->setResendAfter(9);
+ $this->assertEquals(
+ $this->_getVacationScript(true, '', '', false, array(), 9),
+ $segment->generate()
+ );
+ }
+
+ public function testOldSieveSegmentVacationProvidesDomain()
+ {
+ $this->sieve->expects($this->once())
+ ->method('listScripts')
+ ->will($this->returnValue(array('kolab-vacation.siv')));
+ $this->sieve->expects($this->once())
+ ->method('getScript')
+ ->with('kolab-vacation.siv')
+ ->will($this->returnValue($this->_getOldVacationScript('REPLY', 'example.com')));
+ $segment = $this->manager->fetchVacationSegment();
+ $this->assertEquals('example.com', $segment->getDomain());
+ }
+
+ public function testSieveSegmentVacationProvidesDomain()
+ {
+ $this->sieve->expects($this->once())
+ ->method('listScripts')
+ ->will($this->returnValue(array('kolab.siv')));
+ $this->sieve->expects($this->once())
+ ->method('getScript')
+ ->with('kolab.siv')
+ ->will($this->returnValue($this->_getVacationScript(true, 'REPLY', 'example.com')));
+ $segment = $this->manager->fetchVacationSegment();
+ $this->assertEquals('example.com', $segment->getDomain());
+ }
+
+ public function testSieveSegmentVacationAllowsSettingDomain()
+ {
+ $segment = $this->manager->fetchVacationSegment();
+ $segment->setActive();
+ $segment->setDomain('example.org');
+ $this->assertEquals(
+ $this->_getVacationScript(true, '', 'example.org'),
+ $segment->generate()
+ );
+ }
+
+ public function testOldSieveSegmentVacationProvidesRecipientAddresses()
+ {
+ $this->sieve->expects($this->once())
+ ->method('listScripts')
+ ->will($this->returnValue(array('kolab-vacation.siv')));
+ $this->sieve->expects($this->once())
+ ->method('getScript')
+ ->with('kolab-vacation.siv')
+ ->will($this->returnValue($this->_getOldVacationScript('', '', false, array('1@example.org', '2@example.org'))));
+ $segment = $this->manager->fetchVacationSegment();
+ $this->assertEquals(array('1@example.org', '2@example.org'), $segment->getRecipientAddresses());
+ }
+
+ public function testSieveSegmentVacationProvidesRecipientAddresses()
+ {
+ $this->sieve->expects($this->once())
+ ->method('listScripts')
+ ->will($this->returnValue(array('kolab.siv')));
+ $this->sieve->expects($this->once())
+ ->method('getScript')
+ ->with('kolab.siv')
+ ->will($this->returnValue($this->_getVacationScript(true, '', '', false, array('1@example.org', '2@example.org'))));
+ $segment = $this->manager->fetchVacationSegment();
+ $this->assertEquals(array('1@example.org', '2@example.org'), $segment->getRecipientAddresses());
+ }
+
+ public function testSieveSegmentVacationAllowsSettingRecipientAddresses()
+ {
+ $segment = $this->manager->fetchVacationSegment();
+ $segment->setActive();
+ $segment->setRecipientAddresses(array('1@example.com', '2@example.com'));
+ $this->assertEquals(
+ $this->_getVacationScript(true, '', '', false, array('1@example.com', '2@example.com')),
+ $segment->generate()
+ );
+ }
+
+ public function testOldSieveSegmentVacationProvidesReactToSpamAttributeTrueIfSetToTrue()
+ {
+ $this->sieve->expects($this->once())
+ ->method('listScripts')
+ ->will($this->returnValue(array('kolab-vacation.siv')));
+ $this->sieve->expects($this->once())
+ ->method('getScript')
+ ->with('kolab-vacation.siv')
+ ->will($this->returnValue($this->_getOldVacationScript('', '', true)));
+ $segment = $this->manager->fetchVacationSegment();
+ $this->assertTrue($segment->getReactToSpam());
+ }
+
+ public function testOldSieveSegmentVacationProvidesReactToSpamAttributeFalseIfSetToFalse()
+ {
+ $this->sieve->expects($this->once())
+ ->method('listScripts')
+ ->will($this->returnValue(array('kolab-vacation.siv')));
+ $this->sieve->expects($this->once())
+ ->method('getScript')
+ ->with('kolab-vacation.siv')
+ ->will($this->returnValue($this->_getOldVacationScript('', '', false)));
+ $segment = $this->manager->fetchVacationSegment();
+ $this->assertFalse($segment->getReactToSpam());
+ }
+
+ public function testSieveSegmentVacationProvidesReactToSpamAttributeFalseIfSetToFalse()
+ {
+ $this->sieve->expects($this->once())
+ ->method('listScripts')
+ ->will($this->returnValue(array('kolab.siv')));
+ $this->sieve->expects($this->once())
+ ->method('getScript')
+ ->with('kolab.siv')
+ ->will($this->returnValue($this->_getVacationScript(true, '', '', false)));
+ $segment = $this->manager->fetchVacationSegment();
+ $this->assertFalse($segment->getReactToSpam());
+ }
+
+ public function testSieveSegmentVacationProvidesReactToSpamAttributeTrueIfSetToTrue()
+ {
+ $this->sieve->expects($this->once())
+ ->method('listScripts')
+ ->will($this->returnValue(array('kolab.siv')));
+ $this->sieve->expects($this->once())
+ ->method('getScript')
+ ->with('kolab.siv')
+ ->will($this->returnValue($this->_getVacationScript(true, '', '', true)));
+ $segment = $this->manager->fetchVacationSegment();
+ $this->assertTrue($segment->getReactToSpam());
+ }
+
+ public function testSieveSegmentVacationAllowsSettingReactToSpamToTrue()
+ {
+ $segment = $this->manager->fetchVacationSegment();
+ $segment->setActive();
+ $segment->setReactToSpam(true);
+ $this->assertEquals(
+ $this->_getVacationScript(true, '', '', true),
+ $segment->generate()
+ );
+ }
+
+ public function testSieveSegmentVacationAllowsSettingReactToSpamToFalse()
+ {
+ $segment = $this->manager->fetchVacationSegment();
+ $segment->setActive();
+ $segment->setReactToSpam(false);
+ $this->assertEquals(
+ $this->_getVacationScript(true, '', '', false),
+ $segment->generate()
+ );
+ }
+
+ private function _provideInactiveScript($name, $script)
+ {
+ $this->sieve->expects($this->once())
+ ->method('listScripts')
+ ->will($this->returnValue(array($name)));
+ $this->sieve->expects($this->once())
+ ->method('getScript')
+ ->with($name)
+ ->will($this->returnValue($script));
+ }
+
+ private function _provideActiveScript($name, $script)
+ {
+ $this->_provideInactiveScript($name, $script);
+ $this->sieve->expects($this->once())
+ ->method('getActive')
+ ->will($this->returnValue($name));
+ }
+
+ private function _getOldVacationScript($text = '', $domain = null, $react_to_spam = false, $addresses = array(), $days = 7)
+ {
+ return 'require "vacation";' . "\r\n" .
+ ((empty($domain)) ? '' : 'if not address :domain :contains "From" "' . $domain . '" { keep; stop; }' . "\r\n") .
+ (($react_to_spam) ? '' : 'if header :contains "X-Spam-Flag" "YES" { keep; stop; }' . "\r\n") .
+ 'vacation :addresses [ "' . join('", "', $addresses) . '" ] :days ' . $days . ' text:' . "\r\n" .
+ $text . "\r\n" .
+ '.' . "\r\n" .
+ ';' . "\r\n";
+ }
+
+ private function _getVacationScript($active = true, $text = '', $domain = null, $react_to_spam = false, $addresses = array(), $days = 7)
+ {
+ if (empty($text)) {
+ $text = sprintf(
+ _("I am out of office until %s.\r\n").
+ _("In urgent cases, please contact Mrs. <vacation replacement>\r\n\r\n").
+ _("email: <email address of vacation replacement>\r\n").
+ _("phone: +49 711 1111 11\r\n").
+ _("fax.: +49 711 1111 12\r\n\r\n").
+ _("Yours sincerely,\r\n").
+ _("-- \r\n").
+ _("<enter your name and email address here>"),
+ strftime(_('%x'))
+ );
+ }
+
+ if (!empty($addresses)) {
+ $addresses = ':addresses [ "' . join('", "', $addresses) . '" ]';
+ } else {
+ $addresses = '';
+ }
+
+ $script = 'if allof (' . (($active) ? 'true, ## vacation enabled' : 'false, ## vacation disabled') . "\r\n" .
+ ((empty($domain)) ? 'true,' : 'address :domain :contains "From" "' . $domain . '",') . "\r\n" .
+ (($react_to_spam) ? 'true' : 'not header :contains "X-Spam-Flag" "YES"') . ') {' . "\r\n" .
+ 'vacation ' . $addresses . ' :days ' . $days . ' text:' . "\r\n" .
+ $text . "\r\n" .
+ '.' . "\r\n" .
+ ';' . "\r\n" .
+ '}' . "\r\n";
+
+ if (!$active) {
+ $script = preg_replace('/^(.)/m', '#$1', $script);
+ }
+
+ return '### SEGMENT START VACATION ' . (($active) ? 'ENABLED' : 'DISABLED') . "\r\n" .
+ $script .
+ '### SEGMENT END VACATION' . "\r\n";
+ }
+} \ No newline at end of file
diff --git a/test/KolabAdmin/Unit/SieveTest.php b/test/KolabAdmin/Unit/SieveTest.php
new file mode 100644
index 0000000..3027a7b
--- /dev/null
+++ b/test/KolabAdmin/Unit/SieveTest.php
@@ -0,0 +1,291 @@
+<?php
+/**
+ * Test the sieve utilities provided by the webadmin.
+ *
+ * PHP version 5
+ *
+ * @category Kolab
+ * @package KolabAdmin
+ * @author Gunnar Wrobel <wrobel@pardus.de>
+ * @license http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link http://pear.horde.org/index.php?package=KolabAdmin
+ */
+
+/**
+ * Require the tested classes.
+ */
+require_once dirname(__FILE__) . '/../Autoload.php';
+
+/**
+ * Test the sieve utilities provided by the webadmin.
+ *
+ * Copyright 2010 Klarälvdalens Datakonsult AB
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @category Kolab
+ * @package KolabAdmin
+ * @author Gunnar Wrobel <wrobel@pardus.de>
+ * @license http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link http://pear.horde.org/index.php?package=KolabAdmin
+ */
+class KolabAdmin_Unit_SieveTest extends PHPUnit_Framework_TestCase
+{
+ public function testMultiLineDotEscaping()
+ {
+ $this->assertEquals("abc\n..xyz", KolabAdmin_Sieve_Script::dotstuff("abc\n.xyz"));
+ }
+
+ public function testMultiLineDotUnscaping()
+ {
+ $this->assertEquals("abc\n.xyz", KolabAdmin_Sieve_Script::undotstuff("abc\n..xyz"));
+ }
+
+ public function testGetDeliveryFolder()
+ {
+ $this->assertEquals('Test', KolabAdmin_Sieve_Script::getDeliverFolder($this->_getScript()));
+ }
+
+ public function testEmptyDeliveryFolder()
+ {
+ $this->assertFalse(KolabAdmin_Sieve_Script::getDeliverFolder(''));
+ }
+
+ public function testGetVacationAddresses()
+ {
+ $this->assertEquals(
+ array('a@example.com', 'b@example.com'),
+ KolabAdmin_Sieve_Script::getVacationAddresses($this->_getScript())
+ );
+ }
+
+ public function testGetVacationAddressesWithSingleAddress()
+ {
+ $this->assertEquals(
+ array('a@example.com'),
+ KolabAdmin_Sieve_Script::getVacationAddresses(
+ 'vacation :addresses [a@example.com]'
+ )
+ );
+ }
+
+ public function testEmptyVacationAddresses()
+ {
+ $this->assertFalse(KolabAdmin_Sieve_Script::getVacationAddresses(''));
+ }
+
+ public function testGetMailDomain()
+ {
+ $this->assertEquals('example.org', KolabAdmin_Sieve_Script::getMailDomain($this->_getScript()));
+ }
+
+ public function testEmptyMailDomain()
+ {
+ $this->assertFalse(KolabAdmin_Sieve_Script::getMailDomain(''));
+ }
+
+ public function testGetReactToSpam()
+ {
+ $this->assertTrue(KolabAdmin_Sieve_Script::getReactToSpam($this->_getScript()));
+ }
+
+ public function testEmptyReactToSpam()
+ {
+ $this->assertFalse(KolabAdmin_Sieve_Script::getReactToSpam(''));
+ }
+
+ public function testGetVacationDays()
+ {
+ $this->assertEquals(60, KolabAdmin_Sieve_Script::getVacationDays($this->_getScript()));
+ }
+
+ public function testEmptyVacationDays()
+ {
+ $this->assertFalse(KolabAdmin_Sieve_Script::getVacationDays(''));
+ }
+
+ public function testGetVacationText()
+ {
+ $this->assertEquals("\r\nI'm on vacation\r\n", KolabAdmin_Sieve_Script::getVacationText($this->_getScript()));
+ }
+
+ public function testEmptyVacationText()
+ {
+ $this->assertFalse(KolabAdmin_Sieve_Script::getVacationText(''));
+ }
+
+ public function testGetForwardAddress()
+ {
+ $this->assertEquals("test@example.com", KolabAdmin_Sieve_Script::getForwardAddress($this->_getScript()));
+ }
+
+ public function testEmptyForwardAddress()
+ {
+ $this->assertFalse(KolabAdmin_Sieve_Script::getForwardAddress(''));
+ }
+
+ public function testGetKeepOnServer()
+ {
+ $this->assertTrue(KolabAdmin_Sieve_Script::getKeepOnServer($this->_getScript()));
+ }
+
+ public function testEmptyKeepOnServer()
+ {
+ $this->assertFalse(KolabAdmin_Sieve_Script::getKeepOnServer(''));
+ }
+
+ public function testIsDeliveryEnabled()
+ {
+ $this->assertTrue(KolabAdmin_Sieve_Script::isDeliveryEnabled($this->_getScript()));
+ }
+
+ public function testDeliveryNotEnables()
+ {
+ $this->assertFalse(KolabAdmin_Sieve_Script::isDeliveryEnabled(''));
+ }
+
+ public function testIsVacationEnabled()
+ {
+ $this->assertTrue(KolabAdmin_Sieve_Script::isVacationEnabled($this->_getScript()));
+ }
+
+ public function testVacationNotEnables()
+ {
+ $this->assertFalse(KolabAdmin_Sieve_Script::isVacationEnabled(''));
+ }
+
+ public function testGetScriptInfo()
+ {
+ $this->assertEquals(
+ array(
+ 'maildomain' => 'example.org',
+ 'vacationaddresses' => array('a@example.com', 'b@example.com'),
+ 'days' => '60',
+ 'reacttospam' => true,
+ 'vacationtext' => "\r\nI'm on vacation\r\n",
+ 'vacationenabled' => true,
+ 'deliveryfolder' => 'Test',
+ 'deliveryenabled' => true
+ ),
+ KolabAdmin_Sieve_Script::getScriptInfo($this->_getScript())
+ );
+ }
+
+ public function testGetEmptyScriptInfo()
+ {
+ $this->assertEquals(
+ array(
+ 'maildomain' => false,
+ 'vacationaddresses' => false,
+ 'days' => false,
+ 'reacttospam' => false,
+ 'vacationtext' => false,
+ 'vacationenabled' => false,
+ 'deliveryfolder' => false,
+ 'deliveryenabled' => false
+ ),
+ KolabAdmin_Sieve_Script::getScriptInfo('')
+ );
+ }
+
+ public function testCreateScript()
+ {
+ $this->assertEquals(
+ $this->_getScript2(),
+ KolabAdmin_Sieve_Script::createScript(
+ array(
+ 'maildomain' => 'example.org',
+ 'vacationaddresses' => array('a@example.com', 'b@example.com'),
+ 'days' => '60',
+ 'reacttospam' => true,
+ 'vacationtext' => "\r\nI'm on vacation\r\n",
+ 'vacationenabled' => true,
+ 'deliveryfolder' => 'Test',
+ 'deliveryenabled' => true
+ )
+ )
+ );
+ }
+
+ public function testCreateScriptGetScriptInfo()
+ {
+ $info = array(
+ 'maildomain' => 'example.org',
+ 'vacationaddresses' => array('a@example.com', 'b@example.com'),
+ 'days' => '60',
+ 'reacttospam' => true,
+ 'vacationtext' => "\r\nI'm on vacation\r\n",
+ 'vacationenabled' => true,
+ 'deliveryfolder' => 'Test',
+ 'deliveryenabled' => true
+ );
+ $this->assertEquals(
+ $info,
+ KolabAdmin_Sieve_Script::getScriptInfo(
+ KolabAdmin_Sieve_Script::createScript(
+ $info
+ )
+ )
+ );
+ }
+
+ private function _getScript()
+ {
+ return
+ 'fileinto "INBOX/Test";'
+ . '## delivery enabled'
+ . '## vacation enabled'
+ . 'redirect "test@example.com"; keep;"'
+ . 'if header :contains "X-Spam-Flag" "YES" { keep; stop; }' . "\r\n"
+ . 'if not address :domain :contains "From" "example.org" { keep; stop; }' . "\r\n"
+ . 'vacation :addresses [ "a@example.com", "b@example.com" ] :days 60 text:' . "\r\n"
+ . 'I\'m on vacation' . "\r\n.\r\n;\r\n\r\n";
+ }
+
+ private function _getScript2()
+ {
+ return
+ 'require "vacation";' . "\r\n" .
+ '' . "\r\n" .
+ 'require "fileinto";' . "\r\n" .
+ '' . "\r\n" .
+ 'if allof (## vacation enabled' . "\r\n" .
+ 'true,' . "\r\n" .
+ 'address :domain :contains "From" "example.org",' . "\r\n" .
+ 'not header :contains "X-Spam-Flag" "YES") {' . "\r\n" .
+ ' vacation :addresses [ "a@example.com", "b@example.com" ] :days 60 text:' . "\r\n" .
+ 'I\'m on vacation' . "\r\n" .
+ '.' . "\r\n" .
+ ';' . "\r\n" .
+ '}' . "\r\n" .
+ 'if allof (true, ## delivery enabled' . "\r\n" .
+ 'header :contains ["X-Kolab-Scheduling-Message"] ["FALSE"]) {' . "\r\n" .
+ 'fileinto "INBOX/Test";' . "\r\n" .
+ '}' . "\r\n";
+ }
+
+ private function _getOldDeliveryScript()
+ {
+ return 'require "fileinto";' . "\r\n" .
+ 'if header :contains ["X-Kolab-Scheduling-Message"] ["FALSE"] {' . "\r\n" .
+ 'fileinto "INBOX/Test";' . "\r\n" .
+ '}' . "\r\n";
+ }
+
+ private function _getActiveDeliveryScript()
+ {
+ return 'if allof (true, ## delivery enabled' . "\r\n" .
+ 'header :contains ["X-Kolab-Scheduling-Message"] ["FALSE"]) {' . "\r\n" .
+ 'fileinto "INBOX/Test";' . "\r\n" .
+ '}' . "\r\n";
+ }
+
+ private function _getInactiveDeliveryScript()
+ {
+ return 'if allof (false, ## delivery disabled' . "\r\n" .
+ 'header :contains ["X-Kolab-Scheduling-Message"] ["FALSE"]) {' . "\r\n" .
+ 'fileinto "INBOX/Test";' . "\r\n" .
+ '}' . "\r\n";
+ }
+} \ No newline at end of file