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