]> git.mxchange.org Git - friendica-addons.git/blob - dav/SabreDAV/tests/Sabre/CalDAV/CalendarQueryParserTest.php
Merge branch '3.6-release'
[friendica-addons.git] / dav / SabreDAV / tests / Sabre / CalDAV / CalendarQueryParserTest.php
1 <?php
2
3 class Sabre_CalDAV_CalendarQueryParserTest extends PHPUnit_Framework_TestCase {
4
5     function parse($xml) {
6
7         $xml =
8 '<?xml version="1.0"?>
9 <c:calendar-query xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:d="DAV:">
10 ' . implode("\n", $xml) . '
11 </c:calendar-query>';
12
13         $dom = Sabre_DAV_XMLUtil::loadDOMDocument($xml);
14
15         $q = new Sabre_CalDAV_CalendarQueryParser($dom);
16         $q->parse();
17         return $q->filters;
18
19     }
20
21     /**
22      * @expectedException Sabre_DAV_Exception_BadRequest
23      */
24     function testNoFilter() {
25
26         $xml = array();
27         $this->parse($xml);
28
29     }
30
31     /**
32      * @expectedException Sabre_DAV_Exception_BadRequest
33      */
34     function testTwoCompFilter() {
35
36         $xml = array(
37             '<c:filter>',
38             '  <c:comp-filter name="VEVENT" />',
39             '  <c:comp-filter name="VEVENT" />',
40             '</c:filter>'
41         );
42         $this->parse($xml);
43
44     }
45
46     function testBasicFilter() {
47
48         $xml = array(
49             '<c:filter>',
50             '  <c:comp-filter name="VCALENDAR" />',
51             '</c:filter>'
52         );
53         $result = $this->parse($xml);
54
55         $expected = array(
56             'name' => 'VCALENDAR',
57             'comp-filters' => array(),
58             'prop-filters' => array(),
59             'is-not-defined' => false,
60             'time-range' => false
61         );
62
63         $this->assertEquals(
64             $expected,
65             $result
66         );
67
68     }
69
70     function testCompIsNotDefined() {
71
72         $xml = array(
73             '<c:filter>',
74             '  <c:comp-filter name="VCALENDAR">',
75             '    <c:comp-filter name="VEVENT">',
76             '       <c:is-not-defined/>',
77             '    </c:comp-filter>',
78             '  </c:comp-filter>',
79             '</c:filter>'
80         );
81         $result = $this->parse($xml);
82
83         $expected = array(
84             'name' => 'VCALENDAR',
85             'comp-filters' => array(
86                 array(
87                     'name' => 'VEVENT',
88                     'comp-filters' => array(),
89                     'prop-filters' => array(),
90                     'is-not-defined' => true,
91                     'time-range' => false
92                 ),
93             ),
94             'prop-filters' => array(),
95             'is-not-defined' => false,
96             'time-range' => false
97         );
98
99         $this->assertEquals(
100             $expected,
101             $result
102         );
103
104     }
105
106     /**
107      * @expectedException Sabre_DAV_Exception_BadRequest
108      */
109     function testCompTimeRangeOnVCALENDAR() {
110
111         $xml = array(
112             '<c:filter>',
113             '  <c:comp-filter name="VCALENDAR">',
114             '     <c:time-range start="20110101T000000Z" end="20111231T235959Z" />',
115             '  </c:comp-filter>',
116             '</c:filter>'
117         );
118         $result = $this->parse($xml);
119
120     }
121
122     function testCompTimeRange() {
123
124         $xml = array(
125             '<c:filter>',
126             '  <c:comp-filter name="VCALENDAR">',
127             '    <c:comp-filter name="VEVENT">',
128             '       <c:time-range start="20110101T000000Z" end="20111231T235959Z" />',
129             '    </c:comp-filter>',
130             '    <c:comp-filter name="VTODO">',
131             '       <c:time-range start="20110101T000000Z" />',
132             '    </c:comp-filter>',
133             '    <c:comp-filter name="VJOURNAL">',
134             '       <c:time-range end="20111231T235959Z" />',
135             '    </c:comp-filter>',
136             '  </c:comp-filter>',
137             '</c:filter>'
138         );
139         $result = $this->parse($xml);
140
141         $expected = array(
142             'name' => 'VCALENDAR',
143             'comp-filters' => array(
144                 array(
145                     'name' => 'VEVENT',
146                     'comp-filters' => array(),
147                     'prop-filters' => array(),
148                     'is-not-defined' => false,
149                     'time-range' => array(
150                         'start' => new DateTime('2011-01-01 00:00:00', new DateTimeZone('GMT')),
151                         'end' => new DateTime('2011-12-31 23:59:59', new DateTimeZone('GMT')),
152                     ),
153                 ),
154                 array(
155                     'name' => 'VTODO',
156                     'comp-filters' => array(),
157                     'prop-filters' => array(),
158                     'is-not-defined' => false,
159                     'time-range' => array(
160                         'start' => new DateTime('2011-01-01 00:00:00', new DateTimeZone('GMT')),
161                         'end' => null,
162                     ),
163                 ),
164                 array(
165                     'name' => 'VJOURNAL',
166                     'comp-filters' => array(),
167                     'prop-filters' => array(),
168                     'is-not-defined' => false,
169                     'time-range' => array(
170                         'start' => null,
171                         'end' => new DateTime('2011-12-31 23:59:59', new DateTimeZone('GMT')),
172                     ),
173                 ),
174             ),
175             'prop-filters' => array(),
176             'is-not-defined' => false,
177             'time-range' => false
178         );
179
180         $this->assertEquals(
181             $expected,
182             $result
183         );
184
185     }
186
187     /**
188      * @expectedException Sabre_DAV_Exception_BadRequest
189      */
190     function testCompTimeRangeBadRange() {
191
192         $xml = array(
193             '<c:filter>',
194             '  <c:comp-filter name="VCALENDAR">',
195             '    <c:comp-filter name="VEVENT">',
196             '       <c:time-range start="20110101T000000Z" end="20100101T000000Z" />',
197             '    </c:comp-filter>',
198             '  </c:comp-filter>',
199             '</c:filter>'
200         );
201         $this->parse($xml);
202
203     }
204
205     function testProp() {
206
207         $xml = array(
208             '<c:filter>',
209             '  <c:comp-filter name="VCALENDAR">',
210             '    <c:comp-filter name="VEVENT">',
211             '       <c:prop-filter name="SUMMARY">',
212             '           <c:text-match>vacation</c:text-match>',
213             '       </c:prop-filter>',
214             '    </c:comp-filter>',
215             '  </c:comp-filter>',
216             '</c:filter>'
217         );
218         $result = $this->parse($xml);
219
220         $expected = array(
221             'name' => 'VCALENDAR',
222             'comp-filters' => array(
223                 array(
224                     'name' => 'VEVENT',
225                     'is-not-defined' => false,
226                     'comp-filters' => array(),
227                     'prop-filters' => array(
228                         array(
229                             'name' => 'SUMMARY',
230                             'is-not-defined' => false,
231                             'param-filters' => array(),
232                             'text-match' => array(
233                                 'negate-condition' => false,
234                                 'collation' => 'i;ascii-casemap',
235                                 'value' => 'vacation',
236                             ),
237                             'time-range' => null,
238                        ),
239                     ),
240                     'time-range' => null,
241                 ),
242             ),
243             'prop-filters' => array(),
244             'is-not-defined' => false,
245             'time-range' => false
246         );
247
248         $this->assertEquals(
249             $expected,
250             $result
251         );
252
253     }
254
255     function testComplex() {
256
257         $xml = array(
258             '<c:filter>',
259             '  <c:comp-filter name="VCALENDAR">',
260             '    <c:comp-filter name="VEVENT">',
261             '       <c:prop-filter name="SUMMARY">',
262             '           <c:text-match collation="i;unicode-casemap">vacation</c:text-match>',
263             '       </c:prop-filter>',
264             '       <c:prop-filter name="DTSTAMP">',
265             '           <c:time-range start="20110704T000000Z" />',
266             '       </c:prop-filter>',
267             '       <c:prop-filter name="ORGANIZER">',
268             '           <c:is-not-defined />',
269             '       </c:prop-filter>',
270             '       <c:prop-filter name="DTSTART">',
271             '           <c:param-filter name="VALUE">',
272             '               <c:text-match negate-condition="yes">DATE</c:text-match>',
273             '           </c:param-filter>',
274             '       </c:prop-filter>',
275             '    </c:comp-filter>',
276             '  </c:comp-filter>',
277             '</c:filter>'
278         );
279         $result = $this->parse($xml);
280
281         $expected = array(
282             'name' => 'VCALENDAR',
283             'comp-filters' => array(
284                 array(
285                     'name' => 'VEVENT',
286                     'is-not-defined' => false,
287                     'comp-filters' => array(),
288                     'prop-filters' => array(
289                         array(
290                             'name' => 'SUMMARY',
291                             'is-not-defined' => false,
292                             'param-filters' => array(),
293                             'text-match' => array(
294                                 'negate-condition' => false,
295                                 'collation' => 'i;unicode-casemap',
296                                 'value' => 'vacation',
297                             ),
298                             'time-range' => null,
299                         ),
300                         array(
301                             'name' => 'DTSTAMP',
302                             'is-not-defined' => false,
303                             'param-filters' => array(),
304                             'text-match' => null,
305                             'time-range' => array(
306                                 'start' => new DateTime('2011-07-04 00:00:00', new DateTimeZone('GMT')),
307                                 'end' => null,
308                             ),
309                         ),
310                         array(
311                             'name' => 'ORGANIZER',
312                             'is-not-defined' => true,
313                             'param-filters' => array(),
314                             'text-match' => null,
315                             'time-range' => null,
316                         ),
317                         array(
318                             'name' => 'DTSTART',
319                             'is-not-defined' => false,
320                             'param-filters' => array(
321                                 array(
322                                     'name' => 'VALUE',
323                                     'is-not-defined' => false,
324                                     'text-match' => array(
325                                         'negate-condition' => true,
326                                         'value' => 'DATE',
327                                         'collation' => 'i;ascii-casemap',
328                                     ),
329                                 ),
330                             ),
331                             'text-match' => null,
332                             'time-range' => null,
333                         ),
334                     ),
335                     'time-range' => null,
336                 ),
337             ),
338             'prop-filters' => array(),
339             'is-not-defined' => false,
340             'time-range' => false
341         );
342
343         $this->assertEquals(
344             $expected,
345             $result
346         );
347
348     }
349
350     function testOther1() {
351
352         // This body was exactly sent to us from the sabredav mailing list. Checking if this parses correctly.
353
354         $body = <<<BLA
355 <?xml version="1.0" encoding="utf-8" ?>
356 <C:calendar-query xmlns:D="DAV:"
357 xmlns:C="urn:ietf:params:xml:ns:caldav">
358  <D:prop>
359    <C:calendar-data/>
360    <D:getetag/>
361  </D:prop>
362  <C:filter>
363    <C:comp-filter name="VCALENDAR">
364      <C:comp-filter name="VEVENT">
365        <C:time-range start="20090101T000000Z" end="20121202T000000Z"/>
366      </C:comp-filter>
367    </C:comp-filter>
368  </C:filter>
369 </C:calendar-query>
370 BLA;
371
372         $dom = Sabre_DAV_XMLUtil::loadDOMDocument($body);
373
374         $q = new Sabre_CalDAV_CalendarQueryParser($dom);
375         $q->parse();
376
377         $this->assertEquals(array(
378             '{urn:ietf:params:xml:ns:caldav}calendar-data',
379             '{DAV:}getetag',
380         ), $q->requestedProperties);
381
382         $expectedFilters = array(
383             'name' => 'VCALENDAR',
384             'comp-filters' => array(
385                 array(
386                     'name' => 'VEVENT',
387                     'comp-filters' => array(),
388                     'prop-filters' => array(),
389                     'time-range' => array(
390                         'start' => new DateTime('2009-01-01 00:00:00', new DateTimeZone('UTC')),
391                         'end' => new DateTime('2012-12-02 00:00:00', new DateTimeZone('UTC')),
392                     ),
393                     'is-not-defined' => false,
394                 ),
395             ),
396             'prop-filters' => array(),
397             'time-range' => null,
398             'is-not-defined' => false,
399         );
400
401         $this->assertEquals($expectedFilters, $q->filters);
402
403     }
404
405     function testExpand() {
406
407         $xml = array(
408             '<d:prop>',
409             '  <c:calendar-data>',
410             '     <c:expand start="20110101T000000Z" end="20120101T000000Z"/>',
411             '  </c:calendar-data>',
412             '</d:prop>',
413             '<c:filter>',
414             '  <c:comp-filter name="VCALENDAR" />',
415             '</c:filter>'
416         );
417
418         $xml =
419 '<?xml version="1.0"?>
420 <c:calendar-query xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:d="DAV:">
421 ' . implode("\n", $xml) . '
422 </c:calendar-query>';
423
424         $dom = Sabre_DAV_XMLUtil::loadDOMDocument($xml);
425         $q = new Sabre_CalDAV_CalendarQueryParser($dom);
426         $q->parse();
427
428
429         $expected = array(
430             'name' => 'VCALENDAR',
431             'comp-filters' => array(),
432             'prop-filters' => array(),
433             'is-not-defined' => false,
434             'time-range' => false
435         );
436
437         $this->assertEquals(
438             $expected,
439             $q->filters
440         );
441
442         $this->assertEquals(array(
443             '{urn:ietf:params:xml:ns:caldav}calendar-data',
444         ), $q->requestedProperties);
445
446         $this->assertEquals(
447             array(
448                 'start' => new DateTime('2011-01-01 00:00:00', new DateTimeZone('UTC')),
449                 'end' => new DateTime('2012-01-01 00:00:00', new DateTimeZone('UTC')),
450             ),
451             $q->expand
452         );
453
454     }
455
456     /**
457      * @expectedException Sabre_DAV_Exception_BadRequest
458      */
459     function testExpandNoStart() {
460
461         $xml = array(
462             '<d:prop>',
463             '  <c:calendar-data>',
464             '     <c:expand end="20120101T000000Z"/>',
465             '  </c:calendar-data>',
466             '</d:prop>',
467             '<c:filter>',
468             '  <c:comp-filter name="VCALENDAR" />',
469             '</c:filter>'
470         );
471
472         $xml =
473 '<?xml version="1.0"?>
474 <c:calendar-query xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:d="DAV:">
475 ' . implode("\n", $xml) . '
476 </c:calendar-query>';
477
478         $dom = Sabre_DAV_XMLUtil::loadDOMDocument($xml);
479         $q = new Sabre_CalDAV_CalendarQueryParser($dom);
480         $q->parse();
481
482     }
483     /**
484      * @expectedException Sabre_DAV_Exception_BadRequest
485      */
486     function testExpandNoEnd() {
487
488         $xml = array(
489             '<d:prop>',
490             '  <c:calendar-data>',
491             '     <c:expand start="20120101T000000Z"/>',
492             '  </c:calendar-data>',
493             '</d:prop>',
494             '<c:filter>',
495             '  <c:comp-filter name="VCALENDAR" />',
496             '</c:filter>'
497         );
498
499         $xml =
500 '<?xml version="1.0"?>
501 <c:calendar-query xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:d="DAV:">
502 ' . implode("\n", $xml) . '
503 </c:calendar-query>';
504
505         $dom = Sabre_DAV_XMLUtil::loadDOMDocument($xml);
506         $q = new Sabre_CalDAV_CalendarQueryParser($dom);
507         $q->parse();
508
509     }
510     /**
511      * @expectedException Sabre_DAV_Exception_BadRequest
512      */
513     function testExpandBadTimes() {
514
515         $xml = array(
516             '<d:prop>',
517             '  <c:calendar-data>',
518             '     <c:expand start="20120101T000000Z" end="19980101T000000Z"/>',
519             '  </c:calendar-data>',
520             '</d:prop>',
521             '<c:filter>',
522             '  <c:comp-filter name="VCALENDAR" />',
523             '</c:filter>'
524         );
525
526         $xml =
527 '<?xml version="1.0"?>
528 <c:calendar-query xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:d="DAV:">
529 ' . implode("\n", $xml) . '
530 </c:calendar-query>';
531
532         $dom = Sabre_DAV_XMLUtil::loadDOMDocument($xml);
533         $q = new Sabre_CalDAV_CalendarQueryParser($dom);
534         $q->parse();
535
536     }
537 }