1<?php
2
3namespace Sabre\CalDAV;
4use Sabre\HTTP;
5use Sabre\VObject;
6
7/**
8 * This unittest is created to check if expand() works correctly with
9 * floating times (using calendar-timezone information).
10 */
11class ExpandEventsFloatingTimeTest extends \Sabre\DAVServerTest {
12
13    protected $setupCalDAV = true;
14
15    protected $setupCalDAVICSExport = true;
16
17    protected $caldavCalendars = array(
18        array(
19            'id' => 1,
20            'name' => 'Calendar',
21            'principaluri' => 'principals/user1',
22            'uri' => 'calendar1',
23            '{urn:ietf:params:xml:ns:caldav}calendar-timezone' => 'BEGIN:VCALENDAR
24VERSION:2.0
25CALSCALE:GREGORIAN
26BEGIN:VTIMEZONE
27TZID:Europe/Berlin
28BEGIN:DAYLIGHT
29TZOFFSETFROM:+0100
30RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
31DTSTART:19810329T020000
32TZNAME:GMT+2
33TZOFFSETTO:+0200
34END:DAYLIGHT
35BEGIN:STANDARD
36TZOFFSETFROM:+0200
37RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
38DTSTART:19961027T030000
39TZNAME:GMT+1
40TZOFFSETTO:+0100
41END:STANDARD
42END:VTIMEZONE
43END:VCALENDAR',
44        )
45    );
46
47    protected $caldavCalendarObjects = array(
48        1 => array(
49           'event.ics' => array(
50                'calendardata' => 'BEGIN:VCALENDAR
51VERSION:2.0
52CALSCALE:GREGORIAN
53BEGIN:VEVENT
54CREATED:20140701T143658Z
55UID:dba46fe8-1631-4d98-a575-97963c364dfe
56DTEND:20141108T073000
57TRANSP:OPAQUE
58SUMMARY:Floating Time event, starting 05:30am Europe/Berlin
59DTSTART:20141108T053000
60DTSTAMP:20140701T143706Z
61SEQUENCE:1
62END:VEVENT
63END:VCALENDAR
64',
65            ),
66        ),
67    );
68
69    function testExpandCalendarQuery() {
70
71        $request = new HTTP\Request('REPORT', '/calendars/user1/calendar1', [
72            'Depth' => 1,
73            'Content-Type' => 'application/xml',
74        ]);
75
76        $request->setBody('<?xml version="1.0" encoding="utf-8" ?>
77<C:calendar-query xmlns:D="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav">
78    <D:prop>
79        <C:calendar-data>
80            <C:expand start="20141107T230000Z" end="20141108T225959Z"/>
81        </C:calendar-data>
82        <D:getetag/>
83    </D:prop>
84    <C:filter>
85        <C:comp-filter name="VCALENDAR">
86            <C:comp-filter name="VEVENT">
87                <C:time-range start="20141107T230000Z" end="20141108T225959Z"/>
88            </C:comp-filter>
89        </C:comp-filter>
90    </C:filter>
91</C:calendar-query>');
92
93        $response = $this->request($request);
94
95        // Everts super awesome xml parser.
96        $body = substr(
97            $response->body,
98            $start = strpos($response->body, 'BEGIN:VCALENDAR'),
99            strpos($response->body, 'END:VCALENDAR') - $start + 13
100        );
101        $body = str_replace('&#13;','',$body);
102
103        $vObject = VObject\Reader::read($body);
104
105        // check if DTSTARTs and DTENDs are correct
106        foreach ($vObject->VEVENT as $vevent) {
107            /** @var $vevent Sabre\VObject\Component\VEvent */
108            foreach ($vevent->children as $child) {
109                /** @var $child Sabre\VObject\Property */
110
111                if ($child->name == 'DTSTART') {
112                    // DTSTART should be the UTC equivalent of given floating time
113                    $this->assertEquals($child->getValue(), '20141108T043000Z');
114                } elseif ($child->name == 'DTEND') {
115                    // DTEND should be the UTC equivalent of given floating time
116                    $this->assertEquals($child->getValue(), '20141108T063000Z');
117                }
118            }
119        }
120    }
121
122    function testExpandMultiGet() {
123
124        $request = new HTTP\Request('REPORT', '/calendars/user1/calendar1', [
125            'Depth' => 1,
126            'Content-Type' => 'application/xml',
127        ]);
128
129        $request->setBody('<?xml version="1.0" encoding="utf-8" ?>
130<C:calendar-multiget xmlns:D="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav">
131    <D:prop>
132        <C:calendar-data>
133            <C:expand start="20141107T230000Z" end="20141108T225959Z"/>
134        </C:calendar-data>
135        <D:getetag/>
136    </D:prop>
137    <D:href>/calendars/user1/calendar1/event.ics</D:href>
138</C:calendar-multiget>');
139
140        $response = $this->request($request);
141
142        $this->assertEquals(207, $response->getStatus());
143
144        // Everts super awesome xml parser.
145        $body = substr(
146            $response->body,
147            $start = strpos($response->body, 'BEGIN:VCALENDAR'),
148            strpos($response->body, 'END:VCALENDAR') - $start + 13
149        );
150        $body = str_replace('&#13;','',$body);
151
152        $vObject = VObject\Reader::read($body);
153
154        // check if DTSTARTs and DTENDs are correct
155        foreach ($vObject->VEVENT as $vevent) {
156            /** @var $vevent Sabre\VObject\Component\VEvent */
157            foreach ($vevent->children as $child) {
158                /** @var $child Sabre\VObject\Property */
159
160                if ($child->name == 'DTSTART') {
161                    // DTSTART should be the UTC equivalent of given floating time
162                    $this->assertEquals($child->getValue(), '20141108T043000Z');
163                } elseif ($child->name == 'DTEND') {
164                    // DTEND should be the UTC equivalent of given floating time
165                    $this->assertEquals($child->getValue(), '20141108T063000Z');
166                }
167            }
168        }
169    }
170
171    function testExpandExport() {
172
173        $request = new HTTP\Request('GET', '/calendars/user1/calendar1?export&start=1&end=2000000000&expand=1', [
174            'Depth' => 1,
175            'Content-Type' => 'application/xml',
176        ]);
177
178        $response = $this->request($request);
179
180        $this->assertEquals(200, $response->getStatus());
181
182        // Everts super awesome xml parser.
183        $body = substr(
184            $response->body,
185            $start = strpos($response->body, 'BEGIN:VCALENDAR'),
186            strpos($response->body, 'END:VCALENDAR') - $start + 13
187        );
188        $body = str_replace('&#13;','',$body);
189
190        $vObject = VObject\Reader::read($body);
191
192        // check if DTSTARTs and DTENDs are correct
193        foreach ($vObject->VEVENT as $vevent) {
194            /** @var $vevent Sabre\VObject\Component\VEvent */
195            foreach ($vevent->children as $child) {
196                /** @var $child Sabre\VObject\Property */
197
198                if ($child->name == 'DTSTART') {
199                    // DTSTART should be the UTC equivalent of given floating time
200                    $this->assertEquals($child->getValue(), '20141108T043000Z');
201                } elseif ($child->name == 'DTEND') {
202                    // DTEND should be the UTC equivalent of given floating time
203                    $this->assertEquals($child->getValue(), '20141108T063000Z');
204                }
205            }
206        }
207    }
208
209}
210
211