1<?php
2
3namespace Sabre\CalDAV;
4
5use Sabre\HTTP\Request;
6
7class JCalTransformTest extends \Sabre\DAVServerTest {
8
9    protected $setupCalDAV = true;
10    protected $caldavCalendars = [
11        [
12            'id' => 1,
13            'principaluri' => 'principals/user1',
14            'uri' => 'foo',
15        ]
16    ];
17    protected $caldavCalendarObjects = [
18        1 => [
19            'bar.ics' => [
20                'uri' => 'bar.ics',
21                'calendarid' => 1,
22                'calendardata' => "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n",
23                'lastmodified' => null
24            ]
25        ],
26    ];
27
28    function testGet() {
29
30        $headers = [
31            'Accept' => 'application/calendar+json',
32        ];
33        $request = new Request('GET', '/calendars/user1/foo/bar.ics', $headers);
34
35        $response = $this->request($request);
36
37        $body = $response->getBodyAsString();
38        $this->assertEquals(200, $response->getStatus(), "Incorrect status code: " . $body);
39
40        $response = json_decode($body,true);
41        if (json_last_error() !== JSON_ERROR_NONE) {
42            $this->fail('Json decoding error: ' . json_last_error_msg());
43        }
44        $this->assertEquals(
45            [
46                'vcalendar',
47                [],
48                [
49                    [
50                        'vevent',
51                        [],
52                        [],
53                    ],
54                ],
55            ],
56            $response
57        );
58
59    }
60
61    function testMultiGet() {
62
63        $xml = <<<XML
64<?xml version="1.0"?>
65<c:calendar-multiget xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:d="DAV:">
66    <d:prop>
67        <c:calendar-data content-type="application/calendar+json" />
68    </d:prop>
69    <d:href>/calendars/user1/foo/bar.ics</d:href>
70</c:calendar-multiget>
71XML;
72
73        $headers = [];
74        $request = new Request('REPORT', '/calendars/user1/foo', $headers, $xml);
75
76        $response = $this->request($request);
77
78        $this->assertEquals(207, $response->getStatus(), 'Full rsponse: ' . $response->getBodyAsString());
79
80        $multiStatus = $this->server->xml->parse(
81            $response->getBodyAsString()
82        );
83
84        $responses = $multiStatus->getResponses();
85        $this->assertEquals(1, count($responses));
86
87        $response = $responses[0]->getResponseProperties()[200]["{urn:ietf:params:xml:ns:caldav}calendar-data"];
88
89        $jresponse = json_decode($response,true);
90        if (json_last_error()) {
91            $this->fail('Json decoding error: ' . json_last_error_msg() . '. Full response: ' . $response);
92        }
93        $this->assertEquals(
94            [
95                'vcalendar',
96                [],
97                [
98                    [
99                        'vevent',
100                        [],
101                        [],
102                    ],
103                ],
104            ],
105            $jresponse
106        );
107
108    }
109
110    function testCalendarQueryDepth1() {
111
112        $xml = <<<XML
113<?xml version="1.0"?>
114<c:calendar-query xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:d="DAV:">
115    <d:prop>
116        <c:calendar-data content-type="application/calendar+json" />
117    </d:prop>
118    <c:filter>
119        <c:comp-filter name="VCALENDAR" />
120    </c:filter>
121</c:calendar-query>
122XML;
123
124        $headers = [
125            'Depth' => '1',
126        ];
127        $request = new Request('REPORT', '/calendars/user1/foo', $headers, $xml);
128
129        $response = $this->request($request);
130
131        $this->assertEquals(207, $response->getStatus(), "Invalid response code. Full body: " . $response->getBodyAsString());
132
133        $multiStatus = $this->server->xml->parse(
134            $response->getBodyAsString()
135        );
136
137        $responses = $multiStatus->getResponses();
138
139        $this->assertEquals(1, count($responses));
140
141        $response = $responses[0]->getResponseProperties()[200]["{urn:ietf:params:xml:ns:caldav}calendar-data"];
142        $response = json_decode($response,true);
143        if (json_last_error()) {
144            $this->fail('Json decoding error: ' . json_last_error_msg());
145        }
146        $this->assertEquals(
147            [
148                'vcalendar',
149                [],
150                [
151                    [
152                        'vevent',
153                        [],
154                        [],
155                    ],
156                ],
157            ],
158            $response
159        );
160
161    }
162
163    function testCalendarQueryDepth0() {
164
165        $xml = <<<XML
166<?xml version="1.0"?>
167<c:calendar-query xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:d="DAV:">
168    <d:prop>
169        <c:calendar-data content-type="application/calendar+json" />
170    </d:prop>
171    <c:filter>
172        <c:comp-filter name="VCALENDAR" />
173    </c:filter>
174</c:calendar-query>
175XML;
176
177        $headers = [
178            'Depth' => '0',
179        ];
180        $request = new Request('REPORT', '/calendars/user1/foo/bar.ics', $headers, $xml);
181
182        $response = $this->request($request);
183
184        $this->assertEquals(207, $response->getStatus(), "Invalid response code. Full body: " . $response->getBodyAsString());
185
186        $multiStatus = $this->server->xml->parse(
187            $response->getBodyAsString()
188        );
189
190        $responses = $multiStatus->getResponses();
191
192        $this->assertEquals(1, count($responses));
193
194        $response = $responses[0]->getResponseProperties()[200]["{urn:ietf:params:xml:ns:caldav}calendar-data"];
195        $response = json_decode($response,true);
196        if (json_last_error()) {
197            $this->fail('Json decoding error: ' . json_last_error_msg());
198        }
199        $this->assertEquals(
200            [
201                'vcalendar',
202                [],
203                [
204                    [
205                        'vevent',
206                        [],
207                        [],
208                    ],
209                ],
210            ],
211            $response
212        );
213
214    }
215
216    function testValidateICalendar() {
217
218        $input = [
219            'vcalendar',
220            [],
221            [
222                [
223                    'vevent',
224                    [
225                        ['uid', (object)[], 'text', 'foo'],
226                    ],
227                    [],
228                ],
229            ],
230        ];
231        $input = json_encode($input);
232        $this->caldavPlugin->beforeWriteContent(
233            'calendars/user1/foo/bar.ics',
234            $this->server->tree->getNodeForPath('calendars/user1/foo/bar.ics'),
235            $input,
236            $modified
237        );
238
239        $this->assertEquals("BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nUID:foo\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n", $input);
240
241    }
242
243}
244