summaryrefslogtreecommitdiff
path: root/test/KolabAdmin/Unit/Sieve/Segment/DeliveryTest.php
blob: 3acd758f6cc0b7784edaffff2c84b4d92169b31e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
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";
    }
}