ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
VAvailabilityTest.php
Go to the documentation of this file.
1 <?php
2 
4 
6 use DateTimeZone;
8 use Sabre\VObject;
10 
15 class VAvailabilityTest extends TestCase {
16 
18 
19  $vcal = <<<VCAL
20 BEGIN:VCALENDAR
21 BEGIN:VAVAILABILITY
22 END:VAVAILABILITY
23 END:VCALENDAR
24 VCAL;
25  $document = Reader::read($vcal);
26 
27  $this->assertInstanceOf(__NAMESPACE__ . '\VAvailability', $document->VAVAILABILITY);
28 
29  }
30 
32 
33  $vcal = <<<VCAL
34 BEGIN:VCALENDAR
35 BEGIN:VAVAILABILITY
36 DTSTART:20150717T162200Z
37 DTEND:20150717T172200Z
38 END:VAVAILABILITY
39 END:VCALENDAR
40 VCAL;
41 
42  $document = Reader::read($vcal);
43  $tz = new DateTimeZone('UTC');
44  $this->assertEquals(
45  [
46  new DateTimeImmutable('2015-07-17 16:22:00', $tz),
47  new DateTimeImmutable('2015-07-17 17:22:00', $tz),
48  ],
49  $document->VAVAILABILITY->getEffectiveStartEnd()
50  );
51 
52  }
53 
55 
56  $vcal = <<<VCAL
57 BEGIN:VCALENDAR
58 BEGIN:VAVAILABILITY
59 DTSTART:20150717T162200Z
60 DURATION:PT1H
61 END:VAVAILABILITY
62 END:VCALENDAR
63 VCAL;
64 
65  $document = Reader::read($vcal);
66  $tz = new DateTimeZone('UTC');
67  $this->assertEquals(
68  [
69  new DateTimeImmutable('2015-07-17 16:22:00', $tz),
70  new DateTimeImmutable('2015-07-17 17:22:00', $tz),
71  ],
72  $document->VAVAILABILITY->getEffectiveStartEnd()
73  );
74 
75  }
76 
78 
79  $vcal = <<<VCAL
80 BEGIN:VCALENDAR
81 BEGIN:VAVAILABILITY
82 END:VAVAILABILITY
83 END:VCALENDAR
84 VCAL;
85 
86  $document = Reader::read($vcal);
87  $this->assertEquals(
88  [
89  null,
90  null,
91  ],
92  $document->VAVAILABILITY->getEffectiveStartEnd()
93  );
94 
95  }
96 
98 
99  $vcal = <<<VCAL
100 BEGIN:VCALENDAR
101 BEGIN:VAVAILABILITY
102 END:VAVAILABILITY
103 END:VCALENDAR
104 VCAL;
105 
106  $document = Reader::read($vcal);
107  $this->assertTrue(
108  $document->VAVAILABILITY->isInTimeRange(new DateTimeImmutable('2015-07-17'), new DateTimeImmutable('2015-07-18'))
109  );
110 
111  }
112 
114 
115  $vcal = <<<VCAL
116 BEGIN:VCALENDAR
117 BEGIN:VAVAILABILITY
118 DTSTART:20140101T000000Z
119 DTEND:20140102T000000Z
120 END:VAVAILABILITY
121 END:VCALENDAR
122 VCAL;
123 
124  $document = Reader::read($vcal);
125  $this->assertFalse(
126  $document->VAVAILABILITY->isInTimeRange(new DateTimeImmutable('2015-07-17'), new DateTimeImmutable('2015-07-18'))
127  );
128 
129  }
130 
132 
133  // UID and DTSTAMP are present.
135 <<<VCAL
136 BEGIN:VCALENDAR
137 VERSION:2.0
138 PRODID:-//id
139 BEGIN:VAVAILABILITY
140 UID:foo@test
141 DTSTAMP:20111005T133225Z
142 END:VAVAILABILITY
143 END:VCALENDAR
144 VCAL
145  ));
146 
147  // UID and DTSTAMP are missing.
149 <<<VCAL
150 BEGIN:VCALENDAR
151 VERSION:2.0
152 PRODID:-//id
153 BEGIN:VAVAILABILITY
154 END:VAVAILABILITY
155 END:VCALENDAR
156 VCAL
157  ));
158 
159  // DTSTAMP is missing.
161 <<<VCAL
162 BEGIN:VCALENDAR
163 VERSION:2.0
164 PRODID:-//id
165 BEGIN:VAVAILABILITY
166 UID:foo@test
167 END:VAVAILABILITY
168 END:VCALENDAR
169 VCAL
170  ));
171 
172  // UID is missing.
174 <<<VCAL
175 BEGIN:VCALENDAR
176 VERSION:2.0
177 PRODID:-//id
178 BEGIN:VAVAILABILITY
179 DTSTAMP:20111005T133225Z
180 END:VAVAILABILITY
181 END:VCALENDAR
182 VCAL
183  ));
184 
185  }
186 
188 
189  $properties = [
190  'BUSYTYPE:BUSY',
191  'CLASS:PUBLIC',
192  'CREATED:20111005T135125Z',
193  'DESCRIPTION:Long bla bla',
194  'DTSTART:20111005T020000',
195  'LAST-MODIFIED:20111005T135325Z',
196  'ORGANIZER:mailto:foo@example.com',
197  'PRIORITY:1',
198  'SEQUENCE:0',
199  'SUMMARY:Bla bla',
200  'URL:http://example.org/'
201  ];
202 
203  // They are all present, only once.
204  $this->assertIsValid(Reader::read($this->template($properties)));
205 
206  // We duplicate each one to see if it fails.
207  foreach ($properties as $property) {
208  $this->assertIsNotValid(Reader::read($this->template([
209  $property,
210  $property
211  ])));
212  }
213 
214  }
215 
217 
218  // Only DTEND.
219  $this->assertIsValid(Reader::read($this->template([
220  'DTEND:21111005T133225Z'
221  ])));
222 
223  // Only DURATION.
224  $this->assertIsValid(Reader::read($this->template([
225  'DURATION:PT1H'
226  ])));
227 
228  // Both (not allowed).
229  $this->assertIsNotValid(Reader::read($this->template([
230  'DTEND:21111005T133225Z',
231  'DURATION:PT1H'
232  ])));
233  }
234 
236 
237  $vcal = <<<VCAL
238 BEGIN:VCALENDAR
239 BEGIN:VAVAILABILITY
240 BEGIN:AVAILABLE
241 END:AVAILABLE
242 END:VAVAILABILITY
243 END:VCALENDAR
244 VCAL;
245  $document = Reader::read($vcal);
246 
247  $this->assertInstanceOf(__NAMESPACE__, $document->VAVAILABILITY->AVAILABLE);
248 
249  }
250 
252 
253  // UID, DTSTAMP and DTSTART are present.
255 <<<VCAL
256 BEGIN:VCALENDAR
257 VERSION:2.0
258 PRODID:-//id
259 BEGIN:VAVAILABILITY
260 UID:foo@test
261 DTSTAMP:20111005T133225Z
262 BEGIN:AVAILABLE
263 UID:foo@test
264 DTSTAMP:20111005T133225Z
265 DTSTART:20111005T133225Z
266 END:AVAILABLE
267 END:VAVAILABILITY
268 END:VCALENDAR
269 VCAL
270  ));
271 
272  // UID, DTSTAMP and DTSTART are missing.
274 <<<VCAL
275 BEGIN:VCALENDAR
276 VERSION:2.0
277 PRODID:-//id
278 BEGIN:VAVAILABILITY
279 UID:foo@test
280 DTSTAMP:20111005T133225Z
281 BEGIN:AVAILABLE
282 END:AVAILABLE
283 END:VAVAILABILITY
284 END:VCALENDAR
285 VCAL
286  ));
287 
288  // UID is missing.
290 <<<VCAL
291 BEGIN:VCALENDAR
292 VERSION:2.0
293 PRODID:-//id
294 BEGIN:VAVAILABILITY
295 UID:foo@test
296 DTSTAMP:20111005T133225Z
297 BEGIN:AVAILABLE
298 DTSTAMP:20111005T133225Z
299 DTSTART:20111005T133225Z
300 END:AVAILABLE
301 END:VAVAILABILITY
302 END:VCALENDAR
303 VCAL
304  ));
305 
306  // DTSTAMP is missing.
308 <<<VCAL
309 BEGIN:VCALENDAR
310 VERSION:2.0
311 PRODID:-//id
312 BEGIN:VAVAILABILITY
313 UID:foo@test
314 DTSTAMP:20111005T133225Z
315 BEGIN:AVAILABLE
316 UID:foo@test
317 DTSTART:20111005T133225Z
318 END:AVAILABLE
319 END:VAVAILABILITY
320 END:VCALENDAR
321 VCAL
322  ));
323 
324  // DTSTART is missing.
326 <<<VCAL
327 BEGIN:VCALENDAR
328 VERSION:2.0
329 PRODID:-//id
330 BEGIN:VAVAILABILITY
331 UID:foo@test
332 DTSTAMP:20111005T133225Z
333 BEGIN:AVAILABLE
334 UID:foo@test
335 DTSTAMP:20111005T133225Z
336 END:AVAILABLE
337 END:VAVAILABILITY
338 END:VCALENDAR
339 VCAL
340  ));
341 
342  }
343 
345 
346  // Only DTEND.
348  'DTEND:21111005T133225Z'
349  ])));
350 
351  // Only DURATION.
353  'DURATION:PT1H'
354  ])));
355 
356  // Both (not allowed).
358  'DTEND:21111005T133225Z',
359  'DURATION:PT1H'
360  ])));
361  }
362 
364 
365  $properties = [
366  'CREATED:20111005T135125Z',
367  'DESCRIPTION:Long bla bla',
368  'LAST-MODIFIED:20111005T135325Z',
369  'RECURRENCE-ID;RANGE=THISANDFUTURE:19980401T133000Z',
370  'RRULE:FREQ=WEEKLY;BYDAY=MO,TU,WE,TH,FR',
371  'SUMMARY:Bla bla'
372  ];
373 
374  // They are all present, only once.
375  $this->assertIsValid(Reader::read($this->templateAvailable($properties)));
376 
377  // We duplicate each one to see if it fails.
378  foreach ($properties as $property) {
380  $property,
381  $property
382  ])));
383  }
384 
385  }
386  function testRFCxxxSection3_2() {
387 
388  $this->assertEquals(
389  'BUSY',
391  'BUSYTYPE:BUSY'
392  ]))
393  ->VAVAILABILITY
394  ->AVAILABLE
395  ->BUSYTYPE
396  ->getValue()
397  );
398 
399  $this->assertEquals(
400  'BUSY-UNAVAILABLE',
402  'BUSYTYPE:BUSY-UNAVAILABLE'
403  ]))
404  ->VAVAILABILITY
405  ->AVAILABLE
406  ->BUSYTYPE
407  ->getValue()
408  );
409 
410  $this->assertEquals(
411  'BUSY-TENTATIVE',
413  'BUSYTYPE:BUSY-TENTATIVE'
414  ]))
415  ->VAVAILABILITY
416  ->AVAILABLE
417  ->BUSYTYPE
418  ->getValue()
419  );
420 
421  }
422 
423  protected function assertIsValid(VObject\Document $document) {
424 
425  $validationResult = $document->validate();
426  if ($validationResult) {
427  $messages = array_map(function($item) { return $item['message']; }, $validationResult);
428  $this->fail('Failed to assert that the supplied document is a valid document. Validation messages: ' . implode(', ', $messages));
429  }
430  $this->assertEmpty($document->validate());
431 
432  }
433 
434  protected function assertIsNotValid(VObject\Document $document) {
435 
436  $this->assertNotEmpty($document->validate());
437 
438  }
439 
440  protected function template(array $properties) {
441 
442  return $this->_template(
443  <<<VCAL
444 BEGIN:VCALENDAR
445 VERSION:2.0
446 PRODID:-//id
447 BEGIN:VAVAILABILITY
448 UID:foo@test
449 DTSTAMP:20111005T133225Z
450 …
451 END:VAVAILABILITY
452 END:VCALENDAR
453 VCAL
454 ,
455  $properties
456  );
457 
458  }
459 
460  protected function templateAvailable(array $properties) {
461 
462  return $this->_template(
463  <<<VCAL
464 BEGIN:VCALENDAR
465 VERSION:2.0
466 PRODID:-//id
467 BEGIN:VAVAILABILITY
468 UID:foo@test
469 DTSTAMP:20111005T133225Z
470 BEGIN:AVAILABLE
471 UID:foo@test
472 DTSTAMP:20111005T133225Z
473 DTSTART:20111005T133225Z
474 …
475 END:AVAILABLE
476 END:VAVAILABILITY
477 END:VCALENDAR
478 VCAL
479 ,
480  $properties
481  );
482 
483  }
484 
485  protected function _template($template, array $properties) {
486 
487  return str_replace('…', implode("\r\n", $properties), $template);
488 
489  }
490 
491 }
$template
_template($template, array $properties)
$messages
Definition: en.php:5
assertIsNotValid(VObject\Document $document)
test()
Definition: build.php:107
static read($data, $options=0, $charset='UTF-8')
Parses a vCard or iCalendar object, and returns the top component.
Definition: Reader.php:42
We use RFCxxx has a placeholder for the https://tools.ietf.org/html/draft-daboo-calendar-availability...