1<?php
2
3namespace Sabre\CalDAV\Backend;
4
5use Sabre\CalDAV;
6use Sabre\DAV;
7use Sabre\DAV\PropPatch;
8
9abstract class AbstractPDOTest extends \PHPUnit_Framework_TestCase {
10
11    protected $pdo;
12
13    function testConstruct() {
14
15        $backend = new PDO($this->pdo);
16        $this->assertTrue($backend instanceof PDO);
17
18    }
19
20    /**
21     * @depends testConstruct
22     */
23    function testGetCalendarsForUserNoCalendars() {
24
25        $backend = new PDO($this->pdo);
26        $calendars = $backend->getCalendarsForUser('principals/user2');
27        $this->assertEquals(array(),$calendars);
28
29    }
30
31    /**
32     * @depends testConstruct
33     */
34    function testCreateCalendarAndFetch() {
35
36        $backend = new PDO($this->pdo);
37        $returnedId = $backend->createCalendar('principals/user2','somerandomid',array(
38            '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set' => new CalDAV\Xml\Property\SupportedCalendarComponentSet(array('VEVENT')),
39            '{DAV:}displayname' => 'Hello!',
40            '{urn:ietf:params:xml:ns:caldav}schedule-calendar-transp' => new CalDAV\Xml\Property\ScheduleCalendarTransp('transparent'),
41        ));
42        $calendars = $backend->getCalendarsForUser('principals/user2');
43
44        $elementCheck = array(
45            'id'                => $returnedId,
46            'uri'               => 'somerandomid',
47            '{DAV:}displayname' => 'Hello!',
48            '{urn:ietf:params:xml:ns:caldav}calendar-description' => '',
49            '{urn:ietf:params:xml:ns:caldav}schedule-calendar-transp' => new CalDAV\Xml\Property\ScheduleCalendarTransp('transparent'),
50        );
51
52        $this->assertInternalType('array',$calendars);
53        $this->assertEquals(1,count($calendars));
54
55        foreach($elementCheck as $name=>$value) {
56
57            $this->assertArrayHasKey($name, $calendars[0]);
58            $this->assertEquals($value,$calendars[0][$name]);
59
60        }
61
62    }
63
64    /**
65     * @depends testConstruct
66     */
67    function testUpdateCalendarAndFetch() {
68
69        $backend = new PDO($this->pdo);
70
71        //Creating a new calendar
72        $newId = $backend->createCalendar('principals/user2','somerandomid',array());
73
74        $propPatch = new PropPatch([
75            '{DAV:}displayname' => 'myCalendar',
76            '{urn:ietf:params:xml:ns:caldav}schedule-calendar-transp' => new CalDAV\Xml\Property\ScheduleCalendarTransp('transparent'),
77        ]);
78
79        // Updating the calendar
80        $backend->updateCalendar($newId, $propPatch);
81        $result = $propPatch->commit();
82
83        // Verifying the result of the update
84        $this->assertTrue($result);
85
86        // Fetching all calendars from this user
87        $calendars = $backend->getCalendarsForUser('principals/user2');
88
89        // Checking if all the information is still correct
90        $elementCheck = array(
91            'id'                => $newId,
92            'uri'               => 'somerandomid',
93            '{DAV:}displayname' => 'myCalendar',
94            '{urn:ietf:params:xml:ns:caldav}calendar-description' => '',
95            '{urn:ietf:params:xml:ns:caldav}calendar-timezone' => '',
96            '{http://calendarserver.org/ns/}getctag' => 'http://sabre.io/ns/sync/2',
97            '{urn:ietf:params:xml:ns:caldav}schedule-calendar-transp' => new CalDAV\Xml\Property\ScheduleCalendarTransp('transparent'),
98        );
99
100        $this->assertInternalType('array',$calendars);
101        $this->assertEquals(1,count($calendars));
102
103        foreach($elementCheck as $name=>$value) {
104
105            $this->assertArrayHasKey($name, $calendars[0]);
106            $this->assertEquals($value,$calendars[0][$name]);
107
108        }
109
110    }
111
112    /**
113     * @depends testUpdateCalendarAndFetch
114     */
115    function testUpdateCalendarUnknownProperty() {
116
117        $backend = new PDO($this->pdo);
118
119        //Creating a new calendar
120        $newId = $backend->createCalendar('principals/user2','somerandomid',array());
121
122        $propPatch = new PropPatch([
123            '{DAV:}displayname' => 'myCalendar',
124            '{DAV:}yourmom'     => 'wittycomment',
125        ]);
126
127        // Updating the calendar
128        $backend->updateCalendar($newId, $propPatch);
129        $propPatch->commit();
130
131        // Verifying the result of the update
132        $this->assertEquals([
133            '{DAV:}yourmom' => 403,
134            '{DAV:}displayname' => 424,
135        ], $propPatch->getResult());
136
137    }
138
139    /**
140     * @depends testCreateCalendarAndFetch
141     */
142    function testDeleteCalendar() {
143
144        $backend = new PDO($this->pdo);
145        $returnedId = $backend->createCalendar('principals/user2','somerandomid',array(
146            '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set' => new CalDAV\Xml\Property\SupportedCalendarComponentSet(array('VEVENT')),
147            '{DAV:}displayname' => 'Hello!',
148        ));
149
150        $backend->deleteCalendar($returnedId);
151
152        $calendars = $backend->getCalendarsForUser('principals/user2');
153        $this->assertEquals(array(),$calendars);
154
155    }
156
157    /**
158     * @depends testCreateCalendarAndFetch
159     * @expectedException \Sabre\DAV\Exception
160     */
161    function testCreateCalendarIncorrectComponentSet() {;
162
163        $backend = new PDO($this->pdo);
164
165        //Creating a new calendar
166        $newId = $backend->createCalendar('principals/user2','somerandomid',array(
167            '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set' => 'blabla',
168        ));
169
170    }
171
172    function testCreateCalendarObject() {
173
174        $backend = new PDO($this->pdo);
175        $returnedId = $backend->createCalendar('principals/user2','somerandomid',array());
176
177        $object = "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nDTSTART;VALUE=DATE:20120101\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n";
178
179        $backend->createCalendarObject($returnedId, 'random-id', $object);
180
181        $result = $this->pdo->query('SELECT etag, size, calendardata, firstoccurence, lastoccurence, componenttype FROM calendarobjects WHERE uri = "random-id"');
182        $this->assertEquals(array(
183            'etag' => md5($object),
184            'size' => strlen($object),
185            'calendardata' => $object,
186            'firstoccurence' => strtotime('20120101'),
187            'lastoccurence' => strtotime('20120101')+(3600*24),
188            'componenttype' => 'VEVENT',
189        ), $result->fetch(\PDO::FETCH_ASSOC));
190
191    }
192    function testGetMultipleObjects() {
193
194        $backend = new PDO($this->pdo);
195        $returnedId = $backend->createCalendar('principals/user2','somerandomid',array());
196
197        $object = "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nDTSTART;VALUE=DATE:20120101\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n";
198
199        $backend->createCalendarObject($returnedId, 'id-1', $object);
200        $backend->createCalendarObject($returnedId, 'id-2', $object);
201
202        $check = [
203            [
204                'id' => 1,
205                'etag' => '"' . md5($object) . '"',
206                'uri' => 'id-1',
207                'size' => strlen($object),
208                'calendardata' => $object,
209                'lastmodified' => null,
210                'calendarid' => $returnedId,
211            ],
212            [
213                'id' => 2,
214                'etag' => '"' . md5($object) . '"',
215                'uri' => 'id-2',
216                'size' => strlen($object),
217                'calendardata' => $object,
218                'lastmodified' => null,
219                'calendarid' => $returnedId,
220            ],
221        ];
222
223        $result = $backend->getMultipleCalendarObjects($returnedId, [ 'id-1', 'id-2' ]);
224
225        foreach($check as $index => $props) {
226
227            foreach($props as $key=>$value) {
228
229                if ($key!=='lastmodified') {
230                    $this->assertEquals($value, $result[$index][$key]);
231                } else {
232                    $this->assertTrue(isset($result[$index][$key]));
233                }
234
235            }
236
237        }
238
239    }
240
241    /**
242     * @expectedException Sabre\DAV\Exception\BadRequest
243     * @depends testCreateCalendarObject
244     */
245    function testCreateCalendarObjectNoComponent() {
246
247        $backend = new PDO($this->pdo);
248        $returnedId = $backend->createCalendar('principals/user2','somerandomid',array());
249
250        $object = "BEGIN:VCALENDAR\r\nBEGIN:VTIMEZONE\r\nEND:VTIMEZONE\r\nEND:VCALENDAR\r\n";
251
252        $backend->createCalendarObject($returnedId, 'random-id', $object);
253
254    }
255
256    /**
257     * @depends testCreateCalendarObject
258     */
259    function testCreateCalendarObjectDuration() {
260
261        $backend = new PDO($this->pdo);
262        $returnedId = $backend->createCalendar('principals/user2','somerandomid',array());
263
264        $object = "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nDTSTART;VALUE=DATE:20120101\r\nDURATION:P2D\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n";
265
266        $backend->createCalendarObject($returnedId, 'random-id', $object);
267
268        $result = $this->pdo->query('SELECT etag, size, calendardata, firstoccurence, lastoccurence, componenttype FROM calendarobjects WHERE uri = "random-id"');
269        $this->assertEquals(array(
270            'etag' => md5($object),
271            'size' => strlen($object),
272            'calendardata' => $object,
273            'firstoccurence' => strtotime('20120101'),
274            'lastoccurence' => strtotime('20120101')+(3600*48),
275            'componenttype' => 'VEVENT',
276        ), $result->fetch(\PDO::FETCH_ASSOC));
277
278    }
279
280    /**
281     * @depends testCreateCalendarObject
282     */
283    function testCreateCalendarObjectNoDTEND() {
284
285        $backend = new PDO($this->pdo);
286        $returnedId = $backend->createCalendar('principals/user2','somerandomid',array());
287
288        $object = "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nDTSTART;VALUE=DATE-TIME:20120101T100000Z\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n";
289
290        $backend->createCalendarObject($returnedId, 'random-id', $object);
291
292        $result = $this->pdo->query('SELECT etag, size, calendardata, firstoccurence, lastoccurence, componenttype FROM calendarobjects WHERE uri = "random-id"');
293        $this->assertEquals(array(
294            'etag' => md5($object),
295            'size' => strlen($object),
296            'calendardata' => $object,
297            'firstoccurence' => strtotime('2012-01-01 10:00:00'),
298            'lastoccurence' => strtotime('2012-01-01 10:00:00'),
299            'componenttype' => 'VEVENT',
300        ), $result->fetch(\PDO::FETCH_ASSOC));
301
302    }
303
304    /**
305     * @depends testCreateCalendarObject
306     */
307    function testCreateCalendarObjectWithDTEND() {
308
309        $backend = new PDO($this->pdo);
310        $returnedId = $backend->createCalendar('principals/user2','somerandomid',array());
311
312        $object = "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nDTSTART;VALUE=DATE-TIME:20120101T100000Z\r\nDTEND:20120101T110000Z\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n";
313
314        $backend->createCalendarObject($returnedId, 'random-id', $object);
315
316        $result = $this->pdo->query('SELECT etag, size, calendardata, firstoccurence, lastoccurence, componenttype FROM calendarobjects WHERE uri = "random-id"');
317        $this->assertEquals(array(
318            'etag' => md5($object),
319            'size' => strlen($object),
320            'calendardata' => $object,
321            'firstoccurence' => strtotime('2012-01-01 10:00:00'),
322            'lastoccurence' => strtotime('2012-01-01 11:00:00'),
323            'componenttype' => 'VEVENT',
324        ), $result->fetch(\PDO::FETCH_ASSOC));
325
326    }
327
328    /**
329     * @depends testCreateCalendarObject
330     */
331    function testCreateCalendarObjectInfiniteReccurence() {
332
333        $backend = new PDO($this->pdo);
334        $returnedId = $backend->createCalendar('principals/user2','somerandomid',array());
335
336        $object = "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nDTSTART;VALUE=DATE-TIME:20120101T100000Z\r\nRRULE:FREQ=DAILY\r\nUID:foo\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n";
337
338        $backend->createCalendarObject($returnedId, 'random-id', $object);
339
340        $result = $this->pdo->query('SELECT etag, size, calendardata, firstoccurence, lastoccurence, componenttype FROM calendarobjects WHERE uri = "random-id"');
341        $this->assertEquals(array(
342            'etag' => md5($object),
343            'size' => strlen($object),
344            'calendardata' => $object,
345            'firstoccurence' => strtotime('2012-01-01 10:00:00'),
346            'lastoccurence' => strtotime(PDO::MAX_DATE),
347            'componenttype' => 'VEVENT',
348        ), $result->fetch(\PDO::FETCH_ASSOC));
349
350    }
351
352    /**
353     * @depends testCreateCalendarObject
354     */
355    function testCreateCalendarObjectEndingReccurence() {
356
357        $backend = new PDO($this->pdo);
358        $returnedId = $backend->createCalendar('principals/user2','somerandomid',array());
359
360        $object = "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nDTSTART;VALUE=DATE-TIME:20120101T100000Z\r\nDTEND;VALUE=DATE-TIME:20120101T110000Z\r\nUID:foo\r\nRRULE:FREQ=DAILY;COUNT=1000\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n";
361
362        $backend->createCalendarObject($returnedId, 'random-id', $object);
363
364        $result = $this->pdo->query('SELECT etag, size, calendardata, firstoccurence, lastoccurence, componenttype FROM calendarobjects WHERE uri = "random-id"');
365        $this->assertEquals(array(
366            'etag' => md5($object),
367            'size' => strlen($object),
368            'calendardata' => $object,
369            'firstoccurence' => strtotime('2012-01-01 10:00:00'),
370            'lastoccurence' => strtotime('2012-01-01 11:00:00') + (3600 * 24 * 999),
371            'componenttype' => 'VEVENT',
372        ), $result->fetch(\PDO::FETCH_ASSOC));
373
374    }
375
376    /**
377     * @depends testCreateCalendarObject
378     */
379    function testCreateCalendarObjectTask() {
380
381        $backend = new PDO($this->pdo);
382        $returnedId = $backend->createCalendar('principals/user2','somerandomid',array());
383
384        $object = "BEGIN:VCALENDAR\r\nBEGIN:VTODO\r\nDUE;VALUE=DATE-TIME:20120101T100000Z\r\nEND:VTODO\r\nEND:VCALENDAR\r\n";
385
386        $backend->createCalendarObject($returnedId, 'random-id', $object);
387
388        $result = $this->pdo->query('SELECT etag, size, calendardata, firstoccurence, lastoccurence, componenttype FROM calendarobjects WHERE uri = "random-id"');
389        $this->assertEquals(array(
390            'etag' => md5($object),
391            'size' => strlen($object),
392            'calendardata' => $object,
393            'firstoccurence' => null,
394            'lastoccurence' => null,
395            'componenttype' => 'VTODO',
396        ), $result->fetch(\PDO::FETCH_ASSOC));
397
398    }
399
400    /**
401     * @depends testCreateCalendarObject
402     */
403    function testGetCalendarObjects() {
404
405        $backend = new PDO($this->pdo);
406        $returnedId = $backend->createCalendar('principals/user2','somerandomid',array());
407
408        $object = "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nDTSTART;VALUE=DATE:20120101\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n";
409        $backend->createCalendarObject($returnedId, 'random-id', $object);
410
411        $data = $backend->getCalendarObjects($returnedId,'random-id');
412
413        $this->assertEquals(1, count($data));
414        $data = $data[0];
415
416        $this->assertEquals($returnedId, $data['calendarid']);
417        $this->assertEquals('random-id', $data['uri']);
418        $this->assertEquals(strlen($object),$data['size']);
419
420
421    }
422    /**
423     * @depends testCreateCalendarObject
424     */
425    function testGetCalendarObjectByUID() {
426
427        $backend = new PDO($this->pdo);
428        $returnedId = $backend->createCalendar('principals/user2','somerandomid',[]);
429
430        $object = "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nUID:foo\r\nDTSTART;VALUE=DATE:20120101\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n";
431        $backend->createCalendarObject($returnedId, 'random-id', $object);
432
433        $this->assertNull(
434            $backend->getCalendarObjectByUID('principals/user2', 'bar')
435        );
436        $this->assertEquals(
437            'somerandomid/random-id',
438            $backend->getCalendarObjectByUID('principals/user2', 'foo')
439        );
440
441    }
442
443    /**
444     * @depends testCreateCalendarObject
445     */
446    function testUpdateCalendarObject() {
447
448        $backend = new PDO($this->pdo);
449        $returnedId = $backend->createCalendar('principals/user2','somerandomid',array());
450
451        $object = "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nDTSTART;VALUE=DATE:20120101\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n";
452        $object2 = "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nDTSTART;VALUE=DATE:20130101\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n";
453        $backend->createCalendarObject($returnedId, 'random-id', $object);
454        $backend->updateCalendarObject($returnedId, 'random-id', $object2);
455
456        $data = $backend->getCalendarObject($returnedId,'random-id');
457
458        $this->assertEquals($object2, $data['calendardata']);
459        $this->assertEquals($returnedId, $data['calendarid']);
460        $this->assertEquals('random-id', $data['uri']);
461
462
463    }
464
465    /**
466     * @depends testCreateCalendarObject
467     */
468    function testDeleteCalendarObject() {
469
470        $backend = new PDO($this->pdo);
471        $returnedId = $backend->createCalendar('principals/user2','somerandomid',array());
472
473        $object = "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nDTSTART;VALUE=DATE:20120101\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n";
474        $backend->createCalendarObject($returnedId, 'random-id', $object);
475        $backend->deleteCalendarObject($returnedId, 'random-id');
476
477        $data = $backend->getCalendarObject($returnedId,'random-id');
478        $this->assertNull($data);
479
480    }
481
482    function testCalendarQueryNoResult() {
483
484        $abstract = new PDO($this->pdo);
485        $filters = array(
486            'name' => 'VCALENDAR',
487            'comp-filters' => array(
488                array(
489                    'name' => 'VJOURNAL',
490                    'comp-filters' => array(),
491                    'prop-filters' => array(),
492                    'is-not-defined' => false,
493                    'time-range' => null,
494                ),
495            ),
496            'prop-filters' => array(),
497            'is-not-defined' => false,
498            'time-range' => null,
499        );
500
501        $this->assertEquals(array(
502        ), $abstract->calendarQuery(1, $filters));
503
504    }
505
506    function testCalendarQueryTodo() {
507
508        $backend = new PDO($this->pdo);
509        $backend->createCalendarObject(1, "todo", "BEGIN:VCALENDAR\r\nBEGIN:VTODO\r\nEND:VTODO\r\nEND:VCALENDAR\r\n");
510        $backend->createCalendarObject(1, "event", "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nDTSTART:20120101\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n");
511
512        $filters = array(
513            'name' => 'VCALENDAR',
514            'comp-filters' => array(
515                array(
516                    'name' => 'VTODO',
517                    'comp-filters' => array(),
518                    'prop-filters' => array(),
519                    'is-not-defined' => false,
520                    'time-range' => null,
521                ),
522            ),
523            'prop-filters' => array(),
524            'is-not-defined' => false,
525            'time-range' => null,
526        );
527
528        $this->assertEquals(array(
529            "todo",
530        ), $backend->calendarQuery(1, $filters));
531
532    }
533    function testCalendarQueryTodoNotMatch() {
534
535        $backend = new PDO($this->pdo);
536        $backend->createCalendarObject(1, "todo", "BEGIN:VCALENDAR\r\nBEGIN:VTODO\r\nEND:VTODO\r\nEND:VCALENDAR\r\n");
537        $backend->createCalendarObject(1, "event", "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nDTSTART:20120101\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n");
538
539        $filters = array(
540            'name' => 'VCALENDAR',
541            'comp-filters' => array(
542                array(
543                    'name' => 'VTODO',
544                    'comp-filters' => array(),
545                    'prop-filters' => array(
546                        array(
547                            'name' => 'summary',
548                            'text-match' => null,
549                            'time-range' => null,
550                            'param-filters' => array(),
551                            'is-not-defined' => false,
552                        ),
553                    ),
554                    'is-not-defined' => false,
555                    'time-range' => null,
556                ),
557            ),
558            'prop-filters' => array(),
559            'is-not-defined' => false,
560            'time-range' => null,
561        );
562
563        $this->assertEquals(array(
564        ), $backend->calendarQuery(1, $filters));
565
566    }
567
568    function testCalendarQueryNoFilter() {
569
570        $backend = new PDO($this->pdo);
571        $backend->createCalendarObject(1, "todo", "BEGIN:VCALENDAR\r\nBEGIN:VTODO\r\nEND:VTODO\r\nEND:VCALENDAR\r\n");
572        $backend->createCalendarObject(1, "event", "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nDTSTART:20120101\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n");
573
574        $filters = array(
575            'name' => 'VCALENDAR',
576            'comp-filters' => array(),
577            'prop-filters' => array(),
578            'is-not-defined' => false,
579            'time-range' => null,
580        );
581
582        $result = $backend->calendarQuery(1, $filters);
583        $this->assertTrue(in_array('todo', $result));
584        $this->assertTrue(in_array('event', $result));
585
586    }
587
588    function testCalendarQueryTimeRange() {
589
590        $backend = new PDO($this->pdo);
591        $backend->createCalendarObject(1, "todo", "BEGIN:VCALENDAR\r\nBEGIN:VTODO\r\nEND:VTODO\r\nEND:VCALENDAR\r\n");
592        $backend->createCalendarObject(1, "event", "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nDTSTART;VALUE=DATE:20120101\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n");
593        $backend->createCalendarObject(1, "event2", "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nDTSTART;VALUE=DATE:20120103\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n");
594
595        $filters = array(
596            'name' => 'VCALENDAR',
597            'comp-filters' => array(
598                array(
599                    'name' => 'VEVENT',
600                    'comp-filters' => array(),
601                    'prop-filters' => array(),
602                    'is-not-defined' => false,
603                    'time-range' => array(
604                        'start' => new \DateTime('20120103'),
605                        'end'   => new \DateTime('20120104'),
606                    ),
607                ),
608            ),
609            'prop-filters' => array(),
610            'is-not-defined' => false,
611            'time-range' => null,
612        );
613
614        $this->assertEquals(array(
615            "event2",
616        ), $backend->calendarQuery(1, $filters));
617
618    }
619    function testCalendarQueryTimeRangeNoEnd() {
620
621        $backend = new PDO($this->pdo);
622        $backend->createCalendarObject(1, "todo", "BEGIN:VCALENDAR\r\nBEGIN:VTODO\r\nEND:VTODO\r\nEND:VCALENDAR\r\n");
623        $backend->createCalendarObject(1, "event", "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nDTSTART:20120101\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n");
624        $backend->createCalendarObject(1, "event2", "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nDTSTART:20120103\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n");
625
626        $filters = array(
627            'name' => 'VCALENDAR',
628            'comp-filters' => array(
629                array(
630                    'name' => 'VEVENT',
631                    'comp-filters' => array(),
632                    'prop-filters' => array(),
633                    'is-not-defined' => false,
634                    'time-range' => array(
635                        'start' => new \DateTime('20120102'),
636                        'end' => null,
637                    ),
638                ),
639            ),
640            'prop-filters' => array(),
641            'is-not-defined' => false,
642            'time-range' => null,
643        );
644
645        $this->assertEquals(array(
646            "event2",
647        ), $backend->calendarQuery(1, $filters));
648
649    }
650
651    function testGetChanges() {
652
653        $backend = new PDO($this->pdo);
654        $id = $backend->createCalendar(
655            'principals/user1',
656            'bla',
657            []
658        );
659        $result = $backend->getChangesForCalendar($id, null, 1);
660
661        $this->assertEquals([
662            'syncToken' => 1,
663            'modified' => [],
664            'deleted' => [],
665            'added' => [],
666        ], $result);
667
668        $currentToken = $result['syncToken'];
669
670        $dummyTodo = "BEGIN:VCALENDAR\r\nBEGIN:VTODO\r\nEND:VTODO\r\nEND:VCALENDAR\r\n";
671
672        $backend->createCalendarObject($id, "todo1.ics", $dummyTodo);
673        $backend->createCalendarObject($id, "todo2.ics", $dummyTodo);
674        $backend->createCalendarObject($id, "todo3.ics", $dummyTodo);
675        $backend->updateCalendarObject($id, "todo1.ics", $dummyTodo);
676        $backend->deleteCalendarObject($id, "todo2.ics");
677
678        $result = $backend->getChangesForCalendar($id, $currentToken, 1);
679
680        $this->assertEquals([
681            'syncToken' => 6,
682            'modified'  => ["todo1.ics"],
683            'deleted'   => ["todo2.ics"],
684            'added'     => ["todo3.ics"],
685        ], $result);
686
687        $result = $backend->getChangesForCalendar($id, null, 1);
688
689        $this->assertEquals([
690            'syncToken' => 6,
691            'modified' => [],
692            'deleted' => [],
693            'added' => ["todo1.ics", "todo3.ics"],
694        ], $result);
695    }
696
697    function testCreateSubscriptions() {
698
699        $props = [
700            '{http://calendarserver.org/ns/}source' => new \Sabre\DAV\Xml\Property\Href('http://example.org/cal.ics', false),
701            '{DAV:}displayname' => 'cal',
702            '{http://apple.com/ns/ical/}refreshrate' => 'P1W',
703            '{http://apple.com/ns/ical/}calendar-color' => '#FF00FFFF',
704            '{http://calendarserver.org/ns/}subscribed-strip-todos' => true,
705            //'{http://calendarserver.org/ns/}subscribed-strip-alarms' => true,
706            '{http://calendarserver.org/ns/}subscribed-strip-attachments' => true,
707        ];
708
709        $backend = new PDO($this->pdo);
710        $backend->createSubscription('principals/user1', 'sub1', $props);
711
712        $subs = $backend->getSubscriptionsForUser('principals/user1');
713
714        $expected = $props;
715        $expected['id'] = 1;
716        $expected['uri'] = 'sub1';
717        $expected['principaluri'] = 'principals/user1';
718
719        unset($expected['{http://calendarserver.org/ns/}source']);
720        $expected['source'] = 'http://example.org/cal.ics';
721
722        $this->assertEquals(1, count($subs));
723        foreach($expected as $k=>$v) {
724            $this->assertEquals($subs[0][$k], $expected[$k]);
725        }
726
727    }
728
729    /**
730     * @expectedException \Sabre\DAV\Exception\Forbidden
731     */
732    function testCreateSubscriptionFail() {
733
734        $props = [
735        ];
736
737        $backend = new PDO($this->pdo);
738        $backend->createSubscription('principals/user1', 'sub1', $props);
739
740    }
741
742    function testUpdateSubscriptions() {
743
744        $props = [
745            '{http://calendarserver.org/ns/}source' => new \Sabre\DAV\Xml\Property\Href('http://example.org/cal.ics', false),
746            '{DAV:}displayname' => 'cal',
747            '{http://apple.com/ns/ical/}refreshrate' => 'P1W',
748            '{http://apple.com/ns/ical/}calendar-color' => '#FF00FFFF',
749            '{http://calendarserver.org/ns/}subscribed-strip-todos' => true,
750            //'{http://calendarserver.org/ns/}subscribed-strip-alarms' => true,
751            '{http://calendarserver.org/ns/}subscribed-strip-attachments' => true,
752        ];
753
754        $backend = new PDO($this->pdo);
755        $backend->createSubscription('principals/user1', 'sub1', $props);
756
757        $newProps = [
758            '{DAV:}displayname' => 'new displayname',
759            '{http://calendarserver.org/ns/}source' => new \Sabre\DAV\Xml\Property\Href('http://example.org/cal2.ics', false),
760        ];
761
762        $propPatch = new DAV\PropPatch($newProps);
763        $backend->updateSubscription(1, $propPatch);
764        $result = $propPatch->commit();
765
766        $this->assertTrue($result);
767
768        $subs = $backend->getSubscriptionsForUser('principals/user1');
769
770        $expected = array_merge($props, $newProps);
771        $expected['id'] = 1;
772        $expected['uri'] = 'sub1';
773        $expected['principaluri'] = 'principals/user1';
774
775        unset($expected['{http://calendarserver.org/ns/}source']);
776        $expected['source'] = 'http://example.org/cal2.ics';
777
778        $this->assertEquals(1, count($subs));
779        foreach($expected as $k=>$v) {
780            $this->assertEquals($subs[0][$k], $expected[$k]);
781        }
782
783    }
784
785    function testUpdateSubscriptionsFail() {
786
787        $props = [
788            '{http://calendarserver.org/ns/}source' => new \Sabre\DAV\Xml\Property\Href('http://example.org/cal.ics', false),
789            '{DAV:}displayname' => 'cal',
790            '{http://apple.com/ns/ical/}refreshrate' => 'P1W',
791            '{http://apple.com/ns/ical/}calendar-color' => '#FF00FFFF',
792            '{http://calendarserver.org/ns/}subscribed-strip-todos' => true,
793            //'{http://calendarserver.org/ns/}subscribed-strip-alarms' => true,
794            '{http://calendarserver.org/ns/}subscribed-strip-attachments' => true,
795        ];
796
797        $backend = new PDO($this->pdo);
798        $backend->createSubscription('principals/user1', 'sub1', $props);
799
800        $propPatch = new DAV\PropPatch([
801            '{DAV:}displayname' => 'new displayname',
802            '{http://calendarserver.org/ns/}source' => new \Sabre\DAV\Xml\Property\Href('http://example.org/cal2.ics', false),
803            '{DAV:}unknown' => 'foo',
804        ]);
805
806        $backend->updateSubscription(1, $propPatch);
807        $propPatch->commit();
808
809        $this->assertEquals([
810            '{DAV:}unknown' => 403,
811            '{DAV:}displayname' => 424,
812            '{http://calendarserver.org/ns/}source' => 424,
813        ], $propPatch->getResult());
814
815    }
816
817    function testDeleteSubscriptions() {
818
819        $props = [
820            '{http://calendarserver.org/ns/}source' => new \Sabre\DAV\Xml\Property\Href('http://example.org/cal.ics', false),
821            '{DAV:}displayname' => 'cal',
822            '{http://apple.com/ns/ical/}refreshrate' => 'P1W',
823            '{http://apple.com/ns/ical/}calendar-color' => '#FF00FFFF',
824            '{http://calendarserver.org/ns/}subscribed-strip-todos' => true,
825            //'{http://calendarserver.org/ns/}subscribed-strip-alarms' => true,
826            '{http://calendarserver.org/ns/}subscribed-strip-attachments' => true,
827        ];
828
829        $backend = new PDO($this->pdo);
830        $backend->createSubscription('principals/user1', 'sub1', $props);
831
832        $newProps = [
833            '{DAV:}displayname' => 'new displayname',
834            '{http://calendarserver.org/ns/}source' => new \Sabre\DAV\Xml\Property\Href('http://example.org/cal2.ics', false),
835        ];
836
837        $backend->deleteSubscription(1);
838
839        $subs = $backend->getSubscriptionsForUser('principals/user1');
840        $this->assertEquals(0, count($subs));
841    }
842
843    function testSchedulingMethods() {
844
845        $backend = new PDO($this->pdo);
846
847        $calData = "BEGIN:VCALENDAR\r\nEND:VCALENDAR\r\n";
848
849        $backend->createSchedulingObject(
850            'principals/user1',
851            'schedule1.ics',
852            $calData
853        );
854
855        $expected = [
856            'calendardata' => $calData,
857            'uri' => 'schedule1.ics',
858            'etag' => '"' . md5($calData) . '"',
859            'size' => strlen($calData)
860        ];
861
862        $result = $backend->getSchedulingObject('principals/user1', 'schedule1.ics');
863        foreach($expected as $k=>$v) {
864            $this->assertArrayHasKey($k, $result);
865            $this->assertEquals($v, $result[$k]);
866        }
867
868        $results = $backend->getSchedulingObjects('principals/user1');
869
870        $this->assertEquals(1, count($results));
871        $result = $results[0];
872        foreach($expected as $k=>$v) {
873            $this->assertEquals($v, $result[$k]);
874        }
875
876        $backend->deleteSchedulingObject('principals/user1', 'schedule1.ics');
877        $result = $backend->getSchedulingObject('principals/user1', 'schedule1.ics');
878
879        $this->assertNull($result);
880
881    }
882
883}
884