1<?php
2
3namespace Sabre\CalDAV\Schedule;
4
5use Sabre\VObject\ITip\Message;
6use Sabre\VObject\Reader;
7use Sabre\DAV\Server;
8
9class IMipPluginTest extends \PHPUnit_Framework_TestCase {
10
11    function testGetPluginInfo() {
12
13        $plugin = new IMipPlugin('system@example.com');
14        $this->assertEquals(
15            'imip',
16            $plugin->getPluginInfo()['name']
17        );
18
19    }
20
21    function testDeliverReply() {
22
23        $message = new Message();
24        $message->sender = 'mailto:sender@example.org';
25        $message->senderName = 'Sender';
26        $message->recipient = 'mailto:recipient@example.org';
27        $message->recipientName = 'Recipient';
28        $message->method = 'REPLY';
29
30        $ics = <<<ICS
31BEGIN:VCALENDAR\r
32METHOD:REPLY\r
33BEGIN:VEVENT\r
34SUMMARY:Birthday party\r
35END:VEVENT\r
36END:VCALENDAR\r
37
38ICS;
39
40
41        $message->message = Reader::read($ics);
42
43        $result = $this->schedule($message);
44
45        $expected = [
46            [
47                'to' => 'Recipient <recipient@example.org>',
48                'subject' => 'Re: Birthday party',
49                'body' => $ics,
50                'headers' => [
51                    'Reply-To: Sender <sender@example.org>',
52                    'From: system@example.org',
53                    'Content-Type: text/calendar; charset=UTF-8; method=REPLY',
54                    'X-Sabre-Version: ' . \Sabre\DAV\Version::VERSION,
55                ],
56            ]
57        ];
58
59        $this->assertEquals($expected, $result);
60
61    }
62
63    function testDeliverReplyNoMailto() {
64
65        $message = new Message();
66        $message->sender = 'mailto:sender@example.org';
67        $message->senderName = 'Sender';
68        $message->recipient = 'http://example.org/recipient';
69        $message->recipientName = 'Recipient';
70        $message->method = 'REPLY';
71
72        $ics = <<<ICS
73BEGIN:VCALENDAR\r
74METHOD:REPLY\r
75BEGIN:VEVENT\r
76SUMMARY:Birthday party\r
77END:VEVENT\r
78END:VCALENDAR\r
79
80ICS;
81
82
83        $message->message = Reader::read($ics);
84
85        $result = $this->schedule($message);
86
87        $expected = [];
88
89        $this->assertEquals($expected, $result);
90
91    }
92
93    function testDeliverRequest() {
94
95        $message = new Message();
96        $message->sender = 'mailto:sender@example.org';
97        $message->senderName = 'Sender';
98        $message->recipient = 'mailto:recipient@example.org';
99        $message->recipientName = 'Recipient';
100        $message->method = 'REQUEST';
101
102        $ics = <<<ICS
103BEGIN:VCALENDAR\r
104METHOD:REQUEST\r
105BEGIN:VEVENT\r
106SUMMARY:Birthday party\r
107END:VEVENT\r
108END:VCALENDAR\r
109
110ICS;
111
112
113        $message->message = Reader::read($ics);
114
115        $result = $this->schedule($message);
116
117        $expected = [
118            [
119                'to' => 'Recipient <recipient@example.org>',
120                'subject' => 'Birthday party',
121                'body' => $ics,
122                'headers' => [
123                    'Reply-To: Sender <sender@example.org>',
124                    'From: system@example.org',
125                    'Content-Type: text/calendar; charset=UTF-8; method=REQUEST',
126                    'X-Sabre-Version: ' . \Sabre\DAV\Version::VERSION,
127                ],
128            ]
129        ];
130
131        $this->assertEquals($expected, $result);
132
133    }
134
135    function testDeliverCancel() {
136
137        $message = new Message();
138        $message->sender = 'mailto:sender@example.org';
139        $message->senderName = 'Sender';
140        $message->recipient = 'mailto:recipient@example.org';
141        $message->recipientName = 'Recipient';
142        $message->method = 'CANCEL';
143
144        $ics = <<<ICS
145BEGIN:VCALENDAR\r
146METHOD:CANCEL\r
147BEGIN:VEVENT\r
148SUMMARY:Birthday party\r
149END:VEVENT\r
150END:VCALENDAR\r
151
152ICS;
153
154
155        $message->message = Reader::read($ics);
156
157        $result = $this->schedule($message);
158
159        $expected = [
160            [
161                'to' => 'Recipient <recipient@example.org>',
162                'subject' => 'Cancelled: Birthday party',
163                'body' => $ics,
164                'headers' => [
165                    'Reply-To: Sender <sender@example.org>',
166                    'From: system@example.org',
167                    'Content-Type: text/calendar; charset=UTF-8; method=CANCEL',
168                    'X-Sabre-Version: ' . \Sabre\DAV\Version::VERSION,
169                ],
170            ]
171        ];
172
173        $this->assertEquals($expected, $result);
174        $this->assertEquals('1.1', substr($message->scheduleStatus, 0, 3));
175
176    }
177
178    function schedule(Message $message) {
179
180        $plugin = new IMip\MockPlugin('system@example.org');
181
182        $server = new Server();
183        $server->addPlugin($plugin);
184        $server->emit('schedule', [$message]);
185
186        return $plugin->getSentEmails();
187
188    }
189
190    function testDeliverInsignificantRequest() {
191
192        $message = new Message();
193        $message->sender = 'mailto:sender@example.org';
194        $message->senderName = 'Sender';
195        $message->recipient = 'mailto:recipient@example.org';
196        $message->recipientName = 'Recipient';
197        $message->method = 'REQUEST';
198        $message->significantChange = false;
199
200        $ics = <<<ICS
201BEGIN:VCALENDAR\r
202METHOD:REQUEST\r
203BEGIN:VEVENT\r
204SUMMARY:Birthday party\r
205END:VEVENT\r
206END:VCALENDAR\r
207
208ICS;
209
210
211        $message->message = Reader::read($ics);
212
213        $result = $this->schedule($message);
214
215        $expected = [];
216        $this->assertEquals($expected, $result);
217        $this->assertEquals('1.0', $message->getScheduleStatus()[0]);
218
219    }
220
221}
222