ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
VCalendarTest.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use DateTimeZone;
7 use Sabre\VObject;
8 
9 class VCalendarTest extends TestCase {
10 
12 
16  function testExpand($input, $output, $timeZone = 'UTC', $start = '2011-12-01', $end = '2011-12-31') {
17 
19 
21 
22  $vcal = $vcal->expand(
23  new \DateTime($start),
24  new \DateTime($end),
25  $timeZone
26  );
27 
28  // This will normalize the output
29  $output = VObject\Reader::read($output)->serialize();
30 
31  $this->assertVObjectEqualsVObject($output, $vcal->serialize());
32 
33  }
34 
35  function expandData() {
36 
37  $tests = [];
38 
39  // No data
40  $input = 'BEGIN:VCALENDAR
41 CALSCALE:GREGORIAN
42 VERSION:2.0
43 END:VCALENDAR
44 ';
45 
46  $output = $input;
47  $tests[] = [$input,$output];
48 
49 
50  // Simple events
51  $input = 'BEGIN:VCALENDAR
52 CALSCALE:GREGORIAN
53 VERSION:2.0
54 BEGIN:VEVENT
55 UID:bla
56 SUMMARY:InExpand
57 DTSTART;VALUE=DATE:20111202
58 END:VEVENT
59 BEGIN:VEVENT
60 UID:bla2
61 SUMMARY:NotInExpand
62 DTSTART;VALUE=DATE:20120101
63 END:VEVENT
64 END:VCALENDAR
65 ';
66 
67  $output = 'BEGIN:VCALENDAR
68 CALSCALE:GREGORIAN
69 VERSION:2.0
70 BEGIN:VEVENT
71 UID:bla
72 SUMMARY:InExpand
73 DTSTART;VALUE=DATE:20111202
74 END:VEVENT
75 END:VCALENDAR
76 ';
77 
78  $tests[] = [$input, $output];
79 
80  // Removing timezone info
81  $input = 'BEGIN:VCALENDAR
82 CALSCALE:GREGORIAN
83 VERSION:2.0
84 BEGIN:VTIMEZONE
85 TZID:Europe/Paris
86 END:VTIMEZONE
87 BEGIN:VEVENT
88 UID:bla4
89 SUMMARY:RemoveTZ info
90 DTSTART;TZID=Europe/Paris:20111203T130102
91 END:VEVENT
92 END:VCALENDAR
93 ';
94 
95  $output = 'BEGIN:VCALENDAR
96 CALSCALE:GREGORIAN
97 VERSION:2.0
98 BEGIN:VEVENT
99 UID:bla4
100 SUMMARY:RemoveTZ info
101 DTSTART:20111203T120102Z
102 END:VEVENT
103 END:VCALENDAR
104 ';
105 
106  $tests[] = [$input, $output];
107 
108  // Removing timezone info from sub-components. See Issue #278
109  $input = 'BEGIN:VCALENDAR
110 CALSCALE:GREGORIAN
111 VERSION:2.0
112 BEGIN:VTIMEZONE
113 TZID:Europe/Paris
114 END:VTIMEZONE
115 BEGIN:VEVENT
116 UID:bla4
117 SUMMARY:RemoveTZ info
118 DTSTART;TZID=Europe/Paris:20111203T130102
119 BEGIN:VALARM
120 TRIGGER;VALUE=DATE-TIME;TZID=America/New_York:20151209T133200
121 END:VALARM
122 END:VEVENT
123 END:VCALENDAR
124 ';
125 
126  $output = 'BEGIN:VCALENDAR
127 CALSCALE:GREGORIAN
128 VERSION:2.0
129 BEGIN:VEVENT
130 UID:bla4
131 SUMMARY:RemoveTZ info
132 DTSTART:20111203T120102Z
133 BEGIN:VALARM
134 TRIGGER;VALUE=DATE-TIME:20151209T183200Z
135 END:VALARM
136 END:VEVENT
137 END:VCALENDAR
138 ';
139 
140  $tests[] = [$input, $output];
141 
142  // Recurrence rule
143  $input = 'BEGIN:VCALENDAR
144 CALSCALE:GREGORIAN
145 VERSION:2.0
146 BEGIN:VEVENT
147 UID:bla6
148 SUMMARY:Testing RRule
149 DTSTART:20111125T120000Z
150 DTEND:20111125T130000Z
151 RRULE:FREQ=WEEKLY
152 END:VEVENT
153 END:VCALENDAR
154 ';
155 
156  $output = 'BEGIN:VCALENDAR
157 CALSCALE:GREGORIAN
158 VERSION:2.0
159 BEGIN:VEVENT
160 UID:bla6
161 SUMMARY:Testing RRule
162 DTSTART:20111202T120000Z
163 DTEND:20111202T130000Z
164 RECURRENCE-ID:20111202T120000Z
165 END:VEVENT
166 BEGIN:VEVENT
167 UID:bla6
168 SUMMARY:Testing RRule
169 DTSTART:20111209T120000Z
170 DTEND:20111209T130000Z
171 RECURRENCE-ID:20111209T120000Z
172 END:VEVENT
173 BEGIN:VEVENT
174 UID:bla6
175 SUMMARY:Testing RRule
176 DTSTART:20111216T120000Z
177 DTEND:20111216T130000Z
178 RECURRENCE-ID:20111216T120000Z
179 END:VEVENT
180 BEGIN:VEVENT
181 UID:bla6
182 SUMMARY:Testing RRule
183 DTSTART:20111223T120000Z
184 DTEND:20111223T130000Z
185 RECURRENCE-ID:20111223T120000Z
186 END:VEVENT
187 BEGIN:VEVENT
188 UID:bla6
189 SUMMARY:Testing RRule
190 DTSTART:20111230T120000Z
191 DTEND:20111230T130000Z
192 RECURRENCE-ID:20111230T120000Z
193 END:VEVENT
194 END:VCALENDAR
195 ';
196 
197  $tests[] = [$input, $output];
198 
199  // Recurrence rule + override
200  $input = 'BEGIN:VCALENDAR
201 CALSCALE:GREGORIAN
202 VERSION:2.0
203 BEGIN:VEVENT
204 UID:bla6
205 SUMMARY:Testing RRule2
206 DTSTART:20111125T120000Z
207 DTEND:20111125T130000Z
208 RRULE:FREQ=WEEKLY
209 END:VEVENT
210 BEGIN:VEVENT
211 UID:bla6
212 RECURRENCE-ID:20111209T120000Z
213 DTSTART:20111209T140000Z
214 DTEND:20111209T150000Z
215 SUMMARY:Override!
216 END:VEVENT
217 END:VCALENDAR
218 ';
219 
220  $output = 'BEGIN:VCALENDAR
221 CALSCALE:GREGORIAN
222 VERSION:2.0
223 BEGIN:VEVENT
224 UID:bla6
225 SUMMARY:Testing RRule2
226 DTSTART:20111202T120000Z
227 DTEND:20111202T130000Z
228 RECURRENCE-ID:20111202T120000Z
229 END:VEVENT
230 BEGIN:VEVENT
231 UID:bla6
232 RECURRENCE-ID:20111209T120000Z
233 DTSTART:20111209T140000Z
234 DTEND:20111209T150000Z
235 SUMMARY:Override!
236 END:VEVENT
237 BEGIN:VEVENT
238 UID:bla6
239 SUMMARY:Testing RRule2
240 DTSTART:20111216T120000Z
241 DTEND:20111216T130000Z
242 RECURRENCE-ID:20111216T120000Z
243 END:VEVENT
244 BEGIN:VEVENT
245 UID:bla6
246 SUMMARY:Testing RRule2
247 DTSTART:20111223T120000Z
248 DTEND:20111223T130000Z
249 RECURRENCE-ID:20111223T120000Z
250 END:VEVENT
251 BEGIN:VEVENT
252 UID:bla6
253 SUMMARY:Testing RRule2
254 DTSTART:20111230T120000Z
255 DTEND:20111230T130000Z
256 RECURRENCE-ID:20111230T120000Z
257 END:VEVENT
258 END:VCALENDAR
259 ';
260 
261  $tests[] = [$input, $output];
262 
263  // Floating dates and times.
264  $input = <<<ICS
265 BEGIN:VCALENDAR
266 VERSION:2.0
267 BEGIN:VEVENT
268 UID:bla1
269 DTSTART:20141112T195000
270 END:VEVENT
271 BEGIN:VEVENT
272 UID:bla2
273 DTSTART;VALUE=DATE:20141112
274 END:VEVENT
275 BEGIN:VEVENT
276 UID:bla3
277 DTSTART;VALUE=DATE:20141112
278 RRULE:FREQ=DAILY;COUNT=2
279 END:VEVENT
280 END:VCALENDAR
281 ICS;
282 
283  $output = <<<ICS
284 BEGIN:VCALENDAR
285 VERSION:2.0
286 BEGIN:VEVENT
287 UID:bla1
288 DTSTART:20141112T225000Z
289 END:VEVENT
290 BEGIN:VEVENT
291 UID:bla2
292 DTSTART;VALUE=DATE:20141112
293 END:VEVENT
294 BEGIN:VEVENT
295 UID:bla3
296 DTSTART;VALUE=DATE:20141112
297 RECURRENCE-ID;VALUE=DATE:20141112
298 END:VEVENT
299 BEGIN:VEVENT
300 UID:bla3
301 DTSTART;VALUE=DATE:20141113
302 RECURRENCE-ID;VALUE=DATE:20141113
303 END:VEVENT
304 END:VCALENDAR
305 ICS;
306 
307  $tests[] = [$input, $output, 'America/Argentina/Buenos_Aires', '2014-01-01', '2015-01-01'];
308 
309  // Recurrence rule with no valid instances
310  $input = 'BEGIN:VCALENDAR
311 CALSCALE:GREGORIAN
312 VERSION:2.0
313 BEGIN:VEVENT
314 UID:bla6
315 SUMMARY:Testing RRule3
316 DTSTART:20111125T120000Z
317 DTEND:20111125T130000Z
318 RRULE:FREQ=WEEKLY;COUNT=1
319 EXDATE:20111125T120000Z
320 END:VEVENT
321 END:VCALENDAR
322 ';
323 
324  $output = 'BEGIN:VCALENDAR
325 CALSCALE:GREGORIAN
326 VERSION:2.0
327 END:VCALENDAR
328 ';
329 
330  $tests[] = [$input, $output];
331  return $tests;
332 
333  }
334 
339 
340  $input = 'BEGIN:VCALENDAR
341 CALSCALE:GREGORIAN
342 VERSION:2.0
343 BEGIN:VEVENT
344 RRULE:FREQ=WEEKLY
345 DTSTART;VALUE=DATE:20111202
346 END:VEVENT
347 END:VCALENDAR
348 ';
350  $vcal->expand(
351  new \DateTime('2011-12-01'),
352  new \DateTime('2011-12-31')
353  );
354 
355  }
356 
357  function testGetDocumentType() {
358 
359  $vcard = new VCalendar();
360  $vcard->VERSION = '2.0';
361  $this->assertEquals(VCalendar::ICALENDAR20, $vcard->getDocumentType());
362 
363  }
364 
365  function testValidateCorrect() {
366 
367  $input = 'BEGIN:VCALENDAR
368 CALSCALE:GREGORIAN
369 VERSION:2.0
370 PRODID:foo
371 BEGIN:VEVENT
372 DTSTART;VALUE=DATE:20111202
373 DTSTAMP:20140122T233226Z
374 UID:foo
375 END:VEVENT
376 END:VCALENDAR
377 ';
378 
380  $this->assertEquals([], $vcal->validate(), 'Got an error');
381 
382  }
383 
385 
386  $input = 'BEGIN:VCALENDAR
387 CALSCALE:GREGORIAN
388 PRODID:foo
389 BEGIN:VEVENT
390 DTSTART;VALUE=DATE:20111202
391 UID:foo
392 DTSTAMP:20140122T234434Z
393 END:VEVENT
394 END:VCALENDAR
395 ';
396 
398  $this->assertEquals(1, count($vcal->validate()));
399 
400  }
401 
403 
404  $input = 'BEGIN:VCALENDAR
405 CALSCALE:GREGORIAN
406 VERSION:3.0
407 PRODID:foo
408 BEGIN:VEVENT
409 DTSTART;VALUE=DATE:20111202
410 UID:foo
411 DTSTAMP:20140122T234434Z
412 END:VEVENT
413 END:VCALENDAR
414 ';
415 
417  $this->assertEquals(1, count($vcal->validate()));
418 
419  }
420 
421  function testValidateNoProdId() {
422 
423  $input = 'BEGIN:VCALENDAR
424 CALSCALE:GREGORIAN
425 VERSION:2.0
426 BEGIN:VEVENT
427 DTSTART;VALUE=DATE:20111202
428 UID:foo
429 DTSTAMP:20140122T234434Z
430 END:VEVENT
431 END:VCALENDAR
432 ';
433 
435  $this->assertEquals(1, count($vcal->validate()));
436 
437  }
438 
440 
441  $input = 'BEGIN:VCALENDAR
442 VERSION:2.0
443 PRODID:foo
444 CALSCALE:GREGORIAN
445 CALSCALE:GREGORIAN
446 BEGIN:VEVENT
447 DTSTART;VALUE=DATE:20111202
448 UID:foo
449 DTSTAMP:20140122T234434Z
450 END:VEVENT
451 END:VCALENDAR
452 ';
453 
455  $this->assertEquals(1, count($vcal->validate()));
456 
457  }
458 
460 
461  $input = 'BEGIN:VCALENDAR
462 VERSION:2.0
463 PRODID:foo
464 METHOD:REQUEST
465 METHOD:REQUEST
466 BEGIN:VEVENT
467 DTSTART;VALUE=DATE:20111202
468 UID:foo
469 DTSTAMP:20140122T234434Z
470 END:VEVENT
471 END:VCALENDAR
472 ';
473 
475  $this->assertEquals(1, count($vcal->validate()));
476 
477  }
478 
480 
481  $input = 'BEGIN:VCALENDAR
482 VERSION:2.0
483 PRODID:foo
484 METHOD:REQUEST
485 BEGIN:VEVENT
486 DTSTART;VALUE=DATE:20111202
487 UID:foo
488 DTSTAMP:20140122T234434Z
489 END:VEVENT
490 BEGIN:VEVENT
491 DTSTART;VALUE=DATE:20111202
492 UID:foo
493 DTSTAMP:20140122T234434Z
494 END:VEVENT
495 END:VCALENDAR
496 ';
497 
499  $this->assertEquals(1, count($vcal->validate()));
500 
501  }
502 
504 
505  $input = 'BEGIN:VCALENDAR
506 VERSION:2.0
507 PRODID:foo
508 METHOD:REQUEST
509 BEGIN:VEVENT
510 DTSTART;VALUE=DATE:20111202
511 UID:foo
512 DTSTAMP:20140122T234434Z
513 END:VEVENT
514 BEGIN:VEVENT
515 DTSTART;VALUE=DATE:20111202
516 UID:foo
517 DTSTAMP:20140122T234434Z
518 RECURRENCE-ID;VALUE=DATE:20111202
519 END:VEVENT
520 END:VCALENDAR
521 ';
522 
524  $this->assertEquals(0, count($vcal->validate()));
525 
526  }
527 
528  function testGetBaseComponent() {
529 
530  $input = 'BEGIN:VCALENDAR
531 VERSION:2.0
532 PRODID:foo
533 METHOD:REQUEST
534 BEGIN:VEVENT
535 SUMMARY:test
536 DTSTART;VALUE=DATE:20111202
537 UID:foo
538 DTSTAMP:20140122T234434Z
539 END:VEVENT
540 BEGIN:VEVENT
541 DTSTART;VALUE=DATE:20111202
542 UID:foo
543 DTSTAMP:20140122T234434Z
544 RECURRENCE-ID;VALUE=DATE:20111202
545 END:VEVENT
546 END:VCALENDAR
547 ';
548 
550 
551  $result = $vcal->getBaseComponent();
552  $this->assertEquals('test', $result->SUMMARY->getValue());
553 
554  }
555 
557 
558  $input = 'BEGIN:VCALENDAR
559 VERSION:2.0
560 PRODID:foo
561 METHOD:REQUEST
562 BEGIN:VEVENT
563 SUMMARY:test
564 RECURRENCE-ID;VALUE=DATE:20111202
565 DTSTART;VALUE=DATE:20111202
566 UID:foo
567 DTSTAMP:20140122T234434Z
568 END:VEVENT
569 BEGIN:VEVENT
570 DTSTART;VALUE=DATE:20111202
571 UID:foo
572 DTSTAMP:20140122T234434Z
573 RECURRENCE-ID;VALUE=DATE:20111202
574 END:VEVENT
575 END:VCALENDAR
576 ';
577 
579 
580  $result = $vcal->getBaseComponent();
581  $this->assertNull($result);
582 
583  }
584 
586 
587  $input = 'BEGIN:VCALENDAR
588 VERSION:2.0
589 PRODID:foo
590 METHOD:REQUEST
591 BEGIN:VEVENT
592 SUMMARY:test
593 DTSTART;VALUE=DATE:20111202
594 UID:foo
595 DTSTAMP:20140122T234434Z
596 END:VEVENT
597 BEGIN:VEVENT
598 DTSTART;VALUE=DATE:20111202
599 UID:foo
600 DTSTAMP:20140122T234434Z
601 RECURRENCE-ID;VALUE=DATE:20111202
602 END:VEVENT
603 END:VCALENDAR
604 ';
605 
607 
608  $result = $vcal->getBaseComponent('VEVENT');
609  $this->assertEquals('test', $result->SUMMARY->getValue());
610 
611  }
612 
614 
615  $input = 'BEGIN:VCALENDAR
616 VERSION:2.0
617 PRODID:foo
618 METHOD:REQUEST
619 BEGIN:VTODO
620 SUMMARY:test
621 UID:foo
622 DTSTAMP:20140122T234434Z
623 END:VTODO
624 END:VCALENDAR
625 ';
626 
628 
629  $result = $vcal->getBaseComponent('VEVENT');
630  $this->assertNull($result);
631 
632  }
633 
634  function testNoComponents() {
635 
636  $input = <<<ICS
637 BEGIN:VCALENDAR
638 VERSION:2.0
639 PRODID:vobject
640 END:VCALENDAR
641 ICS;
642 
643  $this->assertValidate(
644  $input,
645  0,
646  3,
647  "An iCalendar object must have at least 1 component."
648  );
649 
650  }
651 
653 
654  $input = <<<ICS
655 BEGIN:VCALENDAR
656 VERSION:2.0
657 PRODID:vobject
658 BEGIN:VTIMEZONE
659 TZID:America/Toronto
660 END:VTIMEZONE
661 END:VCALENDAR
662 ICS;
663 
664  $this->assertValidate(
665  $input,
667  3,
668  "A calendar object on a CalDAV server must have at least 1 component (VTODO, VEVENT, VJOURNAL)."
669  );
670 
671  }
672 
673  function testCalDAVMultiUID() {
674 
675  $input = <<<ICS
676 BEGIN:VCALENDAR
677 VERSION:2.0
678 PRODID:vobject
679 BEGIN:VEVENT
680 UID:foo
681 DTSTAMP:20150109T184500Z
682 DTSTART:20150109T184500Z
683 END:VEVENT
684 BEGIN:VEVENT
685 UID:bar
686 DTSTAMP:20150109T184500Z
687 DTSTART:20150109T184500Z
688 END:VEVENT
689 END:VCALENDAR
690 ICS;
691 
692  $this->assertValidate(
693  $input,
695  3,
696  "A calendar object on a CalDAV server may only have components with the same UID."
697  );
698 
699  }
700 
702 
703  $input = <<<ICS
704 BEGIN:VCALENDAR
705 VERSION:2.0
706 PRODID:vobject
707 BEGIN:VEVENT
708 UID:foo
709 RECURRENCE-ID:20150109T185200Z
710 DTSTAMP:20150109T184500Z
711 DTSTART:20150109T184500Z
712 END:VEVENT
713 BEGIN:VTODO
714 UID:foo
715 DTSTAMP:20150109T184500Z
716 DTSTART:20150109T184500Z
717 END:VTODO
718 END:VCALENDAR
719 ICS;
720 
721  $this->assertValidate(
722  $input,
724  3,
725  "A calendar object on a CalDAV server may only have 1 type of component (VEVENT, VTODO or VJOURNAL)."
726  );
727 
728  }
729 
730  function testCalDAVMETHOD() {
731 
732  $input = <<<ICS
733 BEGIN:VCALENDAR
734 VERSION:2.0
735 METHOD:PUBLISH
736 PRODID:vobject
737 BEGIN:VEVENT
738 UID:foo
739 RECURRENCE-ID:20150109T185200Z
740 DTSTAMP:20150109T184500Z
741 DTSTART:20150109T184500Z
742 END:VEVENT
743 END:VCALENDAR
744 ICS;
745 
746  $this->assertValidate(
747  $input,
749  3,
750  "A calendar object on a CalDAV server MUST NOT have a METHOD property."
751  );
752 
753  }
754 
755  function assertValidate($ics, $options, $expectedLevel, $expectedMessage = null) {
756 
757  $vcal = VObject\Reader::read($ics);
758  $result = $vcal->validate($options);
759 
760  $this->assertValidateResult($result, $expectedLevel, $expectedMessage);
761 
762  }
763 
764  function assertValidateResult($input, $expectedLevel, $expectedMessage = null) {
765 
766  $messages = [];
767  foreach ($input as $warning) {
768  $messages[] = $warning['message'];
769  }
770 
771  if ($expectedLevel === 0) {
772  $this->assertEquals(0, count($input), 'No validation messages were expected. We got: ' . implode(', ', $messages));
773  } else {
774  $this->assertEquals(1, count($input), 'We expected exactly 1 validation message, We got: ' . implode(', ', $messages));
775 
776  $this->assertEquals($expectedMessage, $input[0]['message']);
777  $this->assertEquals($expectedLevel, $input[0]['level']);
778  }
779 
780  }
781 
782 
783 }
$result
if($argc< 3) $input
The VCalendar component.
Definition: VCalendar.php:23
$start
Definition: bench.php:8
$messages
Definition: en.php:5
$tests
Definition: bench.php:104
$warning
Definition: X509warning.php:13
const ICALENDAR20
iCalendar 2.0.
Definition: Document.php:34
assertValidate($ics, $options, $expectedLevel, $expectedMessage=null)
count()
Returns the number of elements.
Definition: Node.php:177
static read($data, $options=0, $charset='UTF-8')
Parses a vCard or iCalendar object, and returns the top component.
Definition: Reader.php:42
const PROFILE_CALDAV
If this option is set, the validator will operate on iCalendar objects on the assumption that the vca...
Definition: Node.php:45
assertValidateResult($input, $expectedLevel, $expectedMessage=null)
testExpand($input, $output, $timeZone='UTC', $start='2011-12-01', $end='2011-12-31')
expandData