1<?php
2
3namespace Sabre\VObject\Splitter;
4
5use Sabre\VObject;
6
7class ICalendarTest extends \PHPUnit_Framework_TestCase {
8
9    protected $version;
10
11    function setUp() {
12        $this->version = VObject\Version::VERSION;
13    }
14
15    function createStream($data) {
16
17        $stream = fopen('php://memory','r+');
18        fwrite($stream, $data);
19        rewind($stream);
20        return $stream;
21
22    }
23
24    function testICalendarImportValidEvent() {
25
26        $data = <<<EOT
27BEGIN:VCALENDAR
28BEGIN:VEVENT
29UID:foo
30DTSTAMP:20140122T233226Z
31DTSTART:20140101T070000Z
32END:VEVENT
33END:VCALENDAR
34EOT;
35        $tempFile = $this->createStream($data);
36
37        $objects = new ICalendar($tempFile);
38
39        $return = "";
40        while($object=$objects->getNext()) {
41            $return .= $object->serialize();
42        }
43        $this->assertEquals(array(), VObject\Reader::read($return)->validate());
44    }
45
46    /**
47     * @expectedException Sabre\VObject\ParseException
48     */
49    function testICalendarImportWrongType() {
50
51        $data = <<<EOT
52BEGIN:VCARD
53UID:foo1
54END:VCARD
55BEGIN:VCARD
56UID:foo2
57END:VCARD
58EOT;
59        $tempFile = $this->createStream($data);
60
61        $objects = new ICalendar($tempFile);
62
63    }
64
65    function testICalendarImportEndOfData() {
66        $data = <<<EOT
67BEGIN:VCALENDAR
68BEGIN:VEVENT
69UID:foo
70DTSTAMP:20140122T233226Z
71END:VEVENT
72END:VCALENDAR
73EOT;
74        $tempFile = $this->createStream($data);
75
76        $objects = new ICalendar($tempFile);
77
78        $return = "";
79        while($object=$objects->getNext()) {
80            $return .= $object->serialize();
81        }
82        $this->assertNull($object=$objects->getNext());
83    }
84
85    /**
86     * @expectedException Sabre\VObject\ParseException
87     */
88    function testICalendarImportInvalidEvent() {
89        $data = <<<EOT
90EOT;
91        $tempFile = $this->createStream($data);
92        $objects = new ICalendar($tempFile);
93
94    }
95
96    function testICalendarImportMultipleValidEvents() {
97
98        $event[] = <<<EOT
99BEGIN:VEVENT
100UID:foo1
101DTSTAMP:20140122T233226Z
102DTSTART:20140101T050000Z
103END:VEVENT
104EOT;
105
106$event[] = <<<EOT
107BEGIN:VEVENT
108UID:foo2
109DTSTAMP:20140122T233226Z
110DTSTART:20140101T060000Z
111END:VEVENT
112EOT;
113
114        $data = <<<EOT
115BEGIN:VCALENDAR
116$event[0]
117$event[1]
118END:VCALENDAR
119
120EOT;
121        $tempFile = $this->createStream($data);
122
123        $objects = new ICalendar($tempFile);
124
125        $return = "";
126        $i = 0;
127        while($object=$objects->getNext()) {
128
129            $expected = <<<EOT
130BEGIN:VCALENDAR
131VERSION:2.0
132PRODID:-//Sabre//Sabre VObject $this->version//EN
133CALSCALE:GREGORIAN
134$event[$i]
135END:VCALENDAR
136
137EOT;
138
139            $return .= $object->serialize();
140            $expected = str_replace("\n", "\r\n", $expected);
141            $this->assertEquals($expected, $object->serialize());
142            $i++;
143        }
144        $this->assertEquals(array(), VObject\Reader::read($return)->validate());
145    }
146
147    function testICalendarImportEventWithoutUID() {
148
149        $data = <<<EOT
150BEGIN:VCALENDAR
151VERSION:2.0
152PRODID:-//Sabre//Sabre VObject $this->version//EN
153CALSCALE:GREGORIAN
154BEGIN:VEVENT
155DTSTART:20140101T040000Z
156DTSTAMP:20140122T233226Z
157END:VEVENT
158END:VCALENDAR
159
160EOT;
161        $tempFile = $this->createStream($data);
162
163        $objects = new ICalendar($tempFile);
164
165        $return = "";
166        while($object=$objects->getNext()) {
167            $return .= $object->serialize();
168        }
169
170        $messages = VObject\Reader::read($return)->validate();
171
172        if ($messages) {
173            $messages = array_map(
174                function($item) { return $item['message']; },
175                $messages
176            );
177            $this->fail('Validation errors: ' . implode("\n", $messages));
178        } else {
179            $this->assertEquals(array(), $messages);
180        }
181    }
182
183    function testICalendarImportMultipleVTIMEZONESAndMultipleValidEvents() {
184
185        $timezones = <<<EOT
186BEGIN:VTIMEZONE
187TZID:Europe/Berlin
188BEGIN:DAYLIGHT
189TZOFFSETFROM:+0100
190RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
191DTSTART:19810329T020000
192TZNAME:MESZ
193TZOFFSETTO:+0200
194END:DAYLIGHT
195BEGIN:STANDARD
196TZOFFSETFROM:+0200
197RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
198DTSTART:19961027T030000
199TZNAME:MEZ
200TZOFFSETTO:+0100
201END:STANDARD
202END:VTIMEZONE
203BEGIN:VTIMEZONE
204TZID:Europe/London
205BEGIN:DAYLIGHT
206TZOFFSETFROM:+0000
207RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
208DTSTART:19810329T010000
209TZNAME:GMT+01:00
210TZOFFSETTO:+0100
211END:DAYLIGHT
212BEGIN:STANDARD
213TZOFFSETFROM:+0100
214RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
215DTSTART:19961027T020000
216TZNAME:GMT
217TZOFFSETTO:+0000
218END:STANDARD
219END:VTIMEZONE
220EOT;
221
222        $event[] = <<<EOT
223BEGIN:VEVENT
224UID:foo1
225DTSTAMP:20140122T232710Z
226DTSTART:20140101T010000Z
227END:VEVENT
228EOT;
229
230        $event[] = <<<EOT
231BEGIN:VEVENT
232UID:foo2
233DTSTAMP:20140122T232710Z
234DTSTART:20140101T020000Z
235END:VEVENT
236EOT;
237
238        $event[] = <<<EOT
239BEGIN:VEVENT
240UID:foo3
241DTSTAMP:20140122T232710Z
242DTSTART:20140101T030000Z
243END:VEVENT
244EOT;
245
246        $data = <<<EOT
247BEGIN:VCALENDAR
248$timezones
249$event[0]
250$event[1]
251$event[2]
252END:VCALENDAR
253
254EOT;
255        $tempFile = $this->createStream($data);
256
257        $objects = new ICalendar($tempFile);
258
259        $return = "";
260        $i = 0;
261        while($object=$objects->getNext()) {
262
263            $expected = <<<EOT
264BEGIN:VCALENDAR
265VERSION:2.0
266PRODID:-//Sabre//Sabre VObject $this->version//EN
267CALSCALE:GREGORIAN
268$timezones
269$event[$i]
270END:VCALENDAR
271
272EOT;
273            $expected = str_replace("\n", "\r\n", $expected);
274
275            $this->assertEquals($expected, $object->serialize());
276            $return .= $object->serialize();
277            $i++;
278
279        }
280
281        $this->assertEquals(array(), VObject\Reader::read($return)->validate());
282    }
283
284    function testICalendarImportWithOutVTIMEZONES() {
285
286        $data = <<<EOT
287BEGIN:VCALENDAR
288VERSION:2.0
289PRODID:-//Apple Inc.//Mac OS X 10.8//EN
290CALSCALE:GREGORIAN
291BEGIN:VEVENT
292CREATED:20120605T072109Z
293UID:D6716295-C10F-4B20-82F9-E1A3026C7DCF
294DTEND;VALUE=DATE:20120717
295TRANSP:TRANSPARENT
296SUMMARY:Start Vorbereitung
297DTSTART;VALUE=DATE:20120716
298DTSTAMP:20120605T072115Z
299SEQUENCE:2
300BEGIN:VALARM
301X-WR-ALARMUID:A99EDA6A-35EB-4446-B8BC-CDA3C60C627D
302UID:A99EDA6A-35EB-4446-B8BC-CDA3C60C627D
303TRIGGER:-PT15H
304X-APPLE-DEFAULT-ALARM:TRUE
305ATTACH;VALUE=URI:Basso
306ACTION:AUDIO
307END:VALARM
308END:VEVENT
309END:VCALENDAR
310
311EOT;
312        $tempFile = $this->createStream($data);
313
314        $objects = new ICalendar($tempFile);
315
316        $return = "";
317        while($object=$objects->getNext()) {
318            $return .= $object->serialize();
319        }
320
321        $messages = VObject\Reader::read($return)->validate();
322        $this->assertEquals(array(), $messages);
323    }
324
325}
326