1<?php
2
3namespace Sabre\CalDAV;
4
5use Sabre\DAV;
6use Sabre\HTTP;
7
8class SharingPluginTest extends \Sabre\DAVServerTest {
9
10    protected $setupCalDAV = true;
11    protected $setupCalDAVSharing = true;
12    protected $setupACL = true;
13    protected $autoLogin = 'user1';
14
15    function setUp() {
16
17        $this->caldavCalendars = array(
18            array(
19                'principaluri' => 'principals/user1',
20                'id' => 1,
21                'uri' => 'cal1',
22            ),
23            array(
24                'principaluri' => 'principals/user1',
25                'id' => 2,
26                'uri' => 'cal2',
27                '{' . Plugin::NS_CALENDARSERVER . '}shared-url' => 'calendars/user1/cal2',
28                '{http://sabredav.org/ns}owner-principal' => 'principals/user2',
29                '{http://sabredav.org/ns}read-only' => 'true',
30            ),
31            array(
32                'principaluri' => 'principals/user1',
33                'id' => 3,
34                'uri' => 'cal3',
35            ),
36        );
37
38        parent::setUp();
39
40        // Making the logged in user an admin, for full access:
41        $this->aclPlugin->adminPrincipals[] = 'principals/user1';
42        $this->aclPlugin->adminPrincipals[] = 'principals/user2';
43
44    }
45
46    function testSimple() {
47
48        $this->assertInstanceOf('Sabre\\CalDAV\\SharingPlugin', $this->server->getPlugin('caldav-sharing'));
49        $this->assertEquals(
50            'caldav-sharing',
51            $this->caldavSharingPlugin->getPluginInfo()['name']
52        );
53
54    }
55
56    function testGetFeatures() {
57
58        $this->assertEquals(array('calendarserver-sharing'), $this->caldavSharingPlugin->getFeatures());
59
60    }
61
62    function testBeforeGetShareableCalendar() {
63
64        // Forcing the server to authenticate:
65        $this->authPlugin->beforeMethod(new HTTP\Request(), new HTTP\Response());
66        $props = $this->server->getProperties('calendars/user1/cal1', array(
67            '{' . Plugin::NS_CALENDARSERVER . '}invite',
68            '{' . Plugin::NS_CALENDARSERVER . '}allowed-sharing-modes',
69        ));
70
71        $this->assertInstanceOf('Sabre\\CalDAV\\Xml\\Property\\Invite', $props['{' . Plugin::NS_CALENDARSERVER . '}invite']);
72        $this->assertInstanceOf('Sabre\\CalDAV\\Xml\\Property\\AllowedSharingModes', $props['{' . Plugin::NS_CALENDARSERVER . '}allowed-sharing-modes']);
73
74    }
75
76    function testBeforeGetSharedCalendar() {
77
78        $props = $this->server->getProperties('calendars/user1/cal2', array(
79            '{' . Plugin::NS_CALENDARSERVER . '}shared-url',
80            '{' . Plugin::NS_CALENDARSERVER . '}invite',
81        ));
82
83        $this->assertInstanceOf('Sabre\\CalDAV\\Xml\\Property\\Invite', $props['{' . Plugin::NS_CALENDARSERVER . '}invite']);
84        $this->assertInstanceOf('Sabre\\DAV\\Xml\\Property\\Href', $props['{' . Plugin::NS_CALENDARSERVER . '}shared-url']);
85
86    }
87
88    function testUpdateProperties() {
89
90        $this->caldavBackend->updateShares(1,
91            array(
92                array(
93                    'href' => 'mailto:joe@example.org',
94                ),
95            ),
96            array()
97        );
98        $result = $this->server->updateProperties('calendars/user1/cal1', array(
99            '{DAV:}resourcetype' => new DAV\Xml\Property\ResourceType(['{DAV:}collection'])
100        ));
101
102        $this->assertEquals([
103            '{DAV:}resourcetype' => 200
104        ], $result);
105
106        $this->assertEquals(0, count($this->caldavBackend->getShares(1)));
107
108    }
109
110    function testUpdatePropertiesPassThru() {
111
112        $result = $this->server->updateProperties('calendars/user1/cal3', array(
113            '{DAV:}foo' => 'bar',
114        ));
115
116        $this->assertEquals(array(
117            '{DAV:}foo' => 403,
118        ), $result);
119
120    }
121
122    function testUnknownMethodNoPOST() {
123
124        $request = HTTP\Sapi::createFromServerArray(array(
125            'REQUEST_METHOD' => 'PATCH',
126            'REQUEST_URI'    => '/',
127        ));
128
129        $response = $this->request($request);
130
131        $this->assertEquals(501, $response->status, $response->body);
132
133    }
134
135    function testUnknownMethodNoXML() {
136
137        $request = HTTP\Sapi::createFromServerArray(array(
138            'REQUEST_METHOD' => 'POST',
139            'REQUEST_URI'    => '/',
140            'CONTENT_TYPE'   => 'text/plain',
141        ));
142
143        $response = $this->request($request);
144
145        $this->assertEquals(501, $response->status, $response->body);
146
147    }
148
149    function testUnknownMethodNoNode() {
150
151        $request = HTTP\Sapi::createFromServerArray(array(
152            'REQUEST_METHOD' => 'POST',
153            'REQUEST_URI'    => '/foo',
154            'CONTENT_TYPE'   => 'text/xml',
155        ));
156
157        $response = $this->request($request);
158
159        $this->assertEquals(501, $response->status, $response->body);
160
161    }
162
163    function testShareRequest() {
164
165        $request = HTTP\Sapi::createFromServerArray(array(
166            'REQUEST_METHOD' => 'POST',
167            'REQUEST_URI'    => '/calendars/user1/cal1',
168            'CONTENT_TYPE'   => 'text/xml',
169        ));
170
171        $xml = <<<RRR
172<?xml version="1.0"?>
173<cs:share xmlns:cs="http://calendarserver.org/ns/" xmlns:d="DAV:">
174    <cs:set>
175        <d:href>mailto:joe@example.org</d:href>
176        <cs:common-name>Joe Shmoe</cs:common-name>
177        <cs:read-write />
178    </cs:set>
179    <cs:remove>
180        <d:href>mailto:nancy@example.org</d:href>
181    </cs:remove>
182</cs:share>
183RRR;
184
185        $request->setBody($xml);
186
187        $response = $this->request($request);
188        $this->assertEquals(200, $response->status, $response->body);
189
190        $this->assertEquals(array(array(
191            'href' => 'mailto:joe@example.org',
192            'commonName' => 'Joe Shmoe',
193            'readOnly' => false,
194            'status' => SharingPlugin::STATUS_NORESPONSE,
195            'summary' => '',
196        )), $this->caldavBackend->getShares(1));
197
198        // Verifying that the calendar is now marked shared.
199        $props = $this->server->getProperties('calendars/user1/cal1', array('{DAV:}resourcetype'));
200        $this->assertTrue(
201            $props['{DAV:}resourcetype']->is('{http://calendarserver.org/ns/}shared-owner')
202        );
203
204    }
205
206    function testShareRequestNoShareableCalendar() {
207
208        $request = HTTP\Sapi::createFromServerArray(array(
209            'REQUEST_METHOD' => 'POST',
210            'REQUEST_URI'    => '/calendars/user1/cal2',
211            'CONTENT_TYPE'   => 'text/xml',
212        ));
213
214        $xml = '<?xml version="1.0"?>
215<cs:share xmlns:cs="' . Plugin::NS_CALENDARSERVER . '" xmlns:d="DAV:">
216    <cs:set>
217        <d:href>mailto:joe@example.org</d:href>
218        <cs:common-name>Joe Shmoe</cs:common-name>
219        <cs:read-write />
220    </cs:set>
221    <cs:remove>
222        <d:href>mailto:nancy@example.org</d:href>
223    </cs:remove>
224</cs:share>
225';
226
227        $request->setBody($xml);
228
229        $response = $this->request($request);
230        $this->assertEquals(501, $response->status, $response->body);
231
232    }
233
234    function testInviteReply() {
235
236        $request = HTTP\Sapi::createFromServerArray(array(
237            'REQUEST_METHOD' => 'POST',
238            'REQUEST_URI'    => '/calendars/user1',
239            'CONTENT_TYPE'   => 'text/xml',
240        ));
241
242        $xml = '<?xml version="1.0"?>
243<cs:invite-reply xmlns:cs="' . Plugin::NS_CALENDARSERVER . '" xmlns:d="DAV:">
244    <cs:hosturl><d:href>/principals/owner</d:href></cs:hosturl>
245    <cs:invite-accepted />
246</cs:invite-reply>
247';
248
249        $request->setBody($xml);
250        $response = $this->request($request);
251        $this->assertEquals(200, $response->status, $response->body);
252
253    }
254
255    function testInviteBadXML() {
256
257        $request = HTTP\Sapi::createFromServerArray(array(
258            'REQUEST_METHOD' => 'POST',
259            'REQUEST_URI'    => '/calendars/user1',
260            'CONTENT_TYPE'   => 'text/xml',
261        ));
262
263        $xml = '<?xml version="1.0"?>
264<cs:invite-reply xmlns:cs="' . Plugin::NS_CALENDARSERVER . '" xmlns:d="DAV:">
265</cs:invite-reply>
266';
267        $request->setBody($xml);
268        $response = $this->request($request);
269        $this->assertEquals(400, $response->status, $response->body);
270
271    }
272
273    function testInviteWrongUrl() {
274
275        $request = HTTP\Sapi::createFromServerArray(array(
276            'REQUEST_METHOD' => 'POST',
277            'REQUEST_URI'    => '/calendars/user1/cal1',
278            'CONTENT_TYPE'   => 'text/xml',
279        ));
280
281        $xml = '<?xml version="1.0"?>
282<cs:invite-reply xmlns:cs="' . Plugin::NS_CALENDARSERVER . '" xmlns:d="DAV:">
283    <cs:hosturl><d:href>/principals/owner</d:href></cs:hosturl>
284</cs:invite-reply>
285';
286        $request->setBody($xml);
287        $response = $this->request($request);
288        $this->assertEquals(501, $response->status, $response->body);
289
290        // If the plugin did not handle this request, it must ensure that the
291        // body is still accessible by other plugins.
292        $this->assertEquals($xml, $request->getBody(true));
293
294    }
295
296    function testPublish() {
297
298        $request = HTTP\Sapi::createFromServerArray(array(
299            'REQUEST_METHOD' => 'POST',
300            'REQUEST_URI'    => '/calendars/user1/cal1',
301            'CONTENT_TYPE'   => 'text/xml',
302        ));
303
304        $xml = '<?xml version="1.0"?>
305<cs:publish-calendar xmlns:cs="' . Plugin::NS_CALENDARSERVER . '" xmlns:d="DAV:" />
306';
307
308        $request->setBody($xml);
309
310        $response = $this->request($request);
311        $this->assertEquals(202, $response->status, $response->body);
312
313    }
314
315    function testUnpublish() {
316
317        $request = HTTP\Sapi::createFromServerArray(array(
318            'REQUEST_METHOD' => 'POST',
319            'REQUEST_URI'    => '/calendars/user1/cal1',
320            'CONTENT_TYPE'   => 'text/xml',
321        ));
322
323        $xml = '<?xml version="1.0"?>
324<cs:unpublish-calendar xmlns:cs="' . Plugin::NS_CALENDARSERVER . '" xmlns:d="DAV:" />
325';
326
327        $request->setBody($xml);
328
329        $response = $this->request($request);
330        $this->assertEquals(200, $response->status, $response->body);
331
332    }
333
334    function testPublishWrongUrl() {
335
336        $request = HTTP\Sapi::createFromServerArray(array(
337            'REQUEST_METHOD' => 'POST',
338            'REQUEST_URI'    => '/calendars/user1/cal2',
339            'CONTENT_TYPE'   => 'text/xml',
340        ));
341
342        $xml = '<?xml version="1.0"?>
343<cs:publish-calendar xmlns:cs="' . Plugin::NS_CALENDARSERVER . '" xmlns:d="DAV:" />
344';
345
346        $request->setBody($xml);
347
348        $response = $this->request($request);
349        $this->assertEquals(501, $response->status, $response->body);
350
351    }
352
353    function testUnpublishWrongUrl() {
354
355        $request = HTTP\Sapi::createFromServerArray(array(
356            'REQUEST_METHOD' => 'POST',
357            'REQUEST_URI'    => '/calendars/user1/cal2',
358            'CONTENT_TYPE'   => 'text/xml',
359        ));
360
361        $xml = '<?xml version="1.0"?>
362<cs:unpublish-calendar xmlns:cs="' . Plugin::NS_CALENDARSERVER . '" xmlns:d="DAV:" />
363';
364
365        $request->setBody($xml);
366
367        $response = $this->request($request);
368        $this->assertEquals(501, $response->status, $response->body);
369
370    }
371
372    function testUnknownXmlDoc() {
373
374        $request = HTTP\Sapi::createFromServerArray(array(
375            'REQUEST_METHOD' => 'POST',
376            'REQUEST_URI'    => '/calendars/user1/cal2',
377            'CONTENT_TYPE'   => 'text/xml',
378        ));
379
380        $xml = '<?xml version="1.0"?>
381<cs:foo-bar xmlns:cs="' . Plugin::NS_CALENDARSERVER . '" xmlns:d="DAV:" />';
382
383        $request->setBody($xml);
384
385        $response = $this->request($request);
386        $this->assertEquals(501, $response->status, $response->body);
387
388    }
389}
390