xref: /plugin/davcal/vendor/sabre/dav/tests/Sabre/CalDAV/ValidateICalTest.php (revision a1a3b6794e0e143a4a8b51d3185ce2d339be61ab)
1*a1a3b679SAndreas Boehler<?php
2*a1a3b679SAndreas Boehler
3*a1a3b679SAndreas Boehlernamespace Sabre\CalDAV;
4*a1a3b679SAndreas Boehler
5*a1a3b679SAndreas Boehleruse Sabre\DAV;
6*a1a3b679SAndreas Boehleruse Sabre\DAVACL;
7*a1a3b679SAndreas Boehleruse Sabre\HTTP;
8*a1a3b679SAndreas Boehler
9*a1a3b679SAndreas Boehlerrequire_once 'Sabre/HTTP/ResponseMock.php';
10*a1a3b679SAndreas Boehler
11*a1a3b679SAndreas Boehlerclass ValidateICalTest extends \PHPUnit_Framework_TestCase {
12*a1a3b679SAndreas Boehler
13*a1a3b679SAndreas Boehler    /**
14*a1a3b679SAndreas Boehler     * @var Sabre\DAV\Server
15*a1a3b679SAndreas Boehler     */
16*a1a3b679SAndreas Boehler    protected $server;
17*a1a3b679SAndreas Boehler    /**
18*a1a3b679SAndreas Boehler     * @var Sabre\CalDAV\Backend\Mock
19*a1a3b679SAndreas Boehler     */
20*a1a3b679SAndreas Boehler    protected $calBackend;
21*a1a3b679SAndreas Boehler
22*a1a3b679SAndreas Boehler    function setUp() {
23*a1a3b679SAndreas Boehler
24*a1a3b679SAndreas Boehler        $calendars = array(
25*a1a3b679SAndreas Boehler            array(
26*a1a3b679SAndreas Boehler                'id' => 'calendar1',
27*a1a3b679SAndreas Boehler                'principaluri' => 'principals/admin',
28*a1a3b679SAndreas Boehler                'uri' => 'calendar1',
29*a1a3b679SAndreas Boehler                '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set' => new Xml\Property\SupportedCalendarComponentSet( ['VEVENT','VTODO','VJOURNAL'] ),
30*a1a3b679SAndreas Boehler            ),
31*a1a3b679SAndreas Boehler            array(
32*a1a3b679SAndreas Boehler                'id' => 'calendar2',
33*a1a3b679SAndreas Boehler                'principaluri' => 'principals/admin',
34*a1a3b679SAndreas Boehler                'uri' => 'calendar2',
35*a1a3b679SAndreas Boehler                '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set' => new Xml\Property\SupportedCalendarComponentSet( ['VTODO','VJOURNAL'] ),
36*a1a3b679SAndreas Boehler            )
37*a1a3b679SAndreas Boehler        );
38*a1a3b679SAndreas Boehler
39*a1a3b679SAndreas Boehler        $this->calBackend = new Backend\Mock($calendars, []);
40*a1a3b679SAndreas Boehler        $principalBackend = new DAVACL\PrincipalBackend\Mock();
41*a1a3b679SAndreas Boehler
42*a1a3b679SAndreas Boehler        $tree = [
43*a1a3b679SAndreas Boehler            new CalendarRoot($principalBackend, $this->calBackend),
44*a1a3b679SAndreas Boehler        ];
45*a1a3b679SAndreas Boehler
46*a1a3b679SAndreas Boehler        $this->server = new DAV\Server($tree);
47*a1a3b679SAndreas Boehler        $this->server->sapi = new HTTP\SapiMock();
48*a1a3b679SAndreas Boehler        $this->server->debugExceptions = true;
49*a1a3b679SAndreas Boehler
50*a1a3b679SAndreas Boehler        $plugin = new Plugin();
51*a1a3b679SAndreas Boehler        $this->server->addPlugin($plugin);
52*a1a3b679SAndreas Boehler
53*a1a3b679SAndreas Boehler        $response = new HTTP\ResponseMock();
54*a1a3b679SAndreas Boehler        $this->server->httpResponse = $response;
55*a1a3b679SAndreas Boehler
56*a1a3b679SAndreas Boehler    }
57*a1a3b679SAndreas Boehler
58*a1a3b679SAndreas Boehler    function request(HTTP\Request $request) {
59*a1a3b679SAndreas Boehler
60*a1a3b679SAndreas Boehler        $this->server->httpRequest = $request;
61*a1a3b679SAndreas Boehler        $this->server->exec();
62*a1a3b679SAndreas Boehler
63*a1a3b679SAndreas Boehler        return $this->server->httpResponse;
64*a1a3b679SAndreas Boehler
65*a1a3b679SAndreas Boehler    }
66*a1a3b679SAndreas Boehler
67*a1a3b679SAndreas Boehler    function testCreateFile() {
68*a1a3b679SAndreas Boehler
69*a1a3b679SAndreas Boehler        $request = HTTP\Sapi::createFromServerArray(array(
70*a1a3b679SAndreas Boehler            'REQUEST_METHOD' => 'PUT',
71*a1a3b679SAndreas Boehler            'REQUEST_URI' => '/calendars/admin/calendar1/blabla.ics',
72*a1a3b679SAndreas Boehler        ));
73*a1a3b679SAndreas Boehler
74*a1a3b679SAndreas Boehler        $response = $this->request($request);
75*a1a3b679SAndreas Boehler
76*a1a3b679SAndreas Boehler        $this->assertEquals(415, $response->status);
77*a1a3b679SAndreas Boehler
78*a1a3b679SAndreas Boehler    }
79*a1a3b679SAndreas Boehler
80*a1a3b679SAndreas Boehler    function testCreateFileValid() {
81*a1a3b679SAndreas Boehler
82*a1a3b679SAndreas Boehler        $request = HTTP\Sapi::createFromServerArray(array(
83*a1a3b679SAndreas Boehler            'REQUEST_METHOD' => 'PUT',
84*a1a3b679SAndreas Boehler            'REQUEST_URI' => '/calendars/admin/calendar1/blabla.ics',
85*a1a3b679SAndreas Boehler        ));
86*a1a3b679SAndreas Boehler        $request->setBody("BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nUID:foo\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n");
87*a1a3b679SAndreas Boehler
88*a1a3b679SAndreas Boehler        $response = $this->request($request);
89*a1a3b679SAndreas Boehler
90*a1a3b679SAndreas Boehler        $this->assertEquals(201, $response->status, 'Incorrect status returned! Full response body: ' . $response->body);
91*a1a3b679SAndreas Boehler        $this->assertEquals(array(
92*a1a3b679SAndreas Boehler            'X-Sabre-Version' => [DAV\Version::VERSION],
93*a1a3b679SAndreas Boehler            'Content-Length' => ['0'],
94*a1a3b679SAndreas Boehler            'ETag' => ['"' . md5("BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nUID:foo\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n") . '"'],
95*a1a3b679SAndreas Boehler        ), $response->getHeaders());
96*a1a3b679SAndreas Boehler
97*a1a3b679SAndreas Boehler        $expected = array(
98*a1a3b679SAndreas Boehler            'uri'          => 'blabla.ics',
99*a1a3b679SAndreas Boehler            'calendardata' => "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nUID:foo\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n",
100*a1a3b679SAndreas Boehler            'calendarid'   => 'calendar1',
101*a1a3b679SAndreas Boehler            'lastmodified' => null,
102*a1a3b679SAndreas Boehler        );
103*a1a3b679SAndreas Boehler
104*a1a3b679SAndreas Boehler        $this->assertEquals($expected, $this->calBackend->getCalendarObject('calendar1','blabla.ics'));
105*a1a3b679SAndreas Boehler
106*a1a3b679SAndreas Boehler    }
107*a1a3b679SAndreas Boehler
108*a1a3b679SAndreas Boehler    function testCreateFileNoComponents() {
109*a1a3b679SAndreas Boehler
110*a1a3b679SAndreas Boehler        $request = HTTP\Sapi::createFromServerArray(array(
111*a1a3b679SAndreas Boehler            'REQUEST_METHOD' => 'PUT',
112*a1a3b679SAndreas Boehler            'REQUEST_URI' => '/calendars/admin/calendar1/blabla.ics',
113*a1a3b679SAndreas Boehler        ));
114*a1a3b679SAndreas Boehler        $request->setBody("BEGIN:VCALENDAR\r\nEND:VCALENDAR\r\n");
115*a1a3b679SAndreas Boehler
116*a1a3b679SAndreas Boehler        $response = $this->request($request);
117*a1a3b679SAndreas Boehler
118*a1a3b679SAndreas Boehler        $this->assertEquals(400, $response->status, 'Incorrect status returned! Full response body: ' . $response->body);
119*a1a3b679SAndreas Boehler
120*a1a3b679SAndreas Boehler    }
121*a1a3b679SAndreas Boehler
122*a1a3b679SAndreas Boehler    function testCreateFileNoUID() {
123*a1a3b679SAndreas Boehler
124*a1a3b679SAndreas Boehler        $request = HTTP\Sapi::createFromServerArray(array(
125*a1a3b679SAndreas Boehler            'REQUEST_METHOD' => 'PUT',
126*a1a3b679SAndreas Boehler            'REQUEST_URI' => '/calendars/admin/calendar1/blabla.ics',
127*a1a3b679SAndreas Boehler        ));
128*a1a3b679SAndreas Boehler        $request->setBody("BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n");
129*a1a3b679SAndreas Boehler
130*a1a3b679SAndreas Boehler        $response = $this->request($request);
131*a1a3b679SAndreas Boehler
132*a1a3b679SAndreas Boehler        $this->assertEquals(400, $response->status, 'Incorrect status returned! Full response body: ' . $response->body);
133*a1a3b679SAndreas Boehler
134*a1a3b679SAndreas Boehler    }
135*a1a3b679SAndreas Boehler
136*a1a3b679SAndreas Boehler    function testCreateFileVCard() {
137*a1a3b679SAndreas Boehler
138*a1a3b679SAndreas Boehler        $request = HTTP\Sapi::createFromServerArray(array(
139*a1a3b679SAndreas Boehler            'REQUEST_METHOD' => 'PUT',
140*a1a3b679SAndreas Boehler            'REQUEST_URI' => '/calendars/admin/calendar1/blabla.ics',
141*a1a3b679SAndreas Boehler        ));
142*a1a3b679SAndreas Boehler        $request->setBody("BEGIN:VCARD\r\nEND:VCARD\r\n");
143*a1a3b679SAndreas Boehler
144*a1a3b679SAndreas Boehler        $response = $this->request($request);
145*a1a3b679SAndreas Boehler
146*a1a3b679SAndreas Boehler        $this->assertEquals(415, $response->status, 'Incorrect status returned! Full response body: ' . $response->body);
147*a1a3b679SAndreas Boehler
148*a1a3b679SAndreas Boehler    }
149*a1a3b679SAndreas Boehler
150*a1a3b679SAndreas Boehler    function testCreateFile2Components() {
151*a1a3b679SAndreas Boehler
152*a1a3b679SAndreas Boehler        $request = HTTP\Sapi::createFromServerArray(array(
153*a1a3b679SAndreas Boehler            'REQUEST_METHOD' => 'PUT',
154*a1a3b679SAndreas Boehler            'REQUEST_URI' => '/calendars/admin/calendar1/blabla.ics',
155*a1a3b679SAndreas Boehler        ));
156*a1a3b679SAndreas Boehler        $request->setBody("BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nUID:foo\r\nEND:VEVENT\r\nBEGIN:VJOURNAL\r\nUID:foo\r\nEND:VJOURNAL\r\nEND:VCALENDAR\r\n");
157*a1a3b679SAndreas Boehler
158*a1a3b679SAndreas Boehler        $response = $this->request($request);
159*a1a3b679SAndreas Boehler
160*a1a3b679SAndreas Boehler        $this->assertEquals(400, $response->status, 'Incorrect status returned! Full response body: ' . $response->body);
161*a1a3b679SAndreas Boehler
162*a1a3b679SAndreas Boehler    }
163*a1a3b679SAndreas Boehler
164*a1a3b679SAndreas Boehler    function testCreateFile2UIDS() {
165*a1a3b679SAndreas Boehler
166*a1a3b679SAndreas Boehler        $request = HTTP\Sapi::createFromServerArray(array(
167*a1a3b679SAndreas Boehler            'REQUEST_METHOD' => 'PUT',
168*a1a3b679SAndreas Boehler            'REQUEST_URI' => '/calendars/admin/calendar1/blabla.ics',
169*a1a3b679SAndreas Boehler        ));
170*a1a3b679SAndreas Boehler        $request->setBody("BEGIN:VCALENDAR\r\nBEGIN:VTIMEZONE\r\nEND:VTIMEZONE\r\nBEGIN:VEVENT\r\nUID:foo\r\nEND:VEVENT\r\nBEGIN:VEVENT\r\nUID:bar\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n");
171*a1a3b679SAndreas Boehler
172*a1a3b679SAndreas Boehler        $response = $this->request($request);
173*a1a3b679SAndreas Boehler
174*a1a3b679SAndreas Boehler        $this->assertEquals(400, $response->status, 'Incorrect status returned! Full response body: ' . $response->body);
175*a1a3b679SAndreas Boehler
176*a1a3b679SAndreas Boehler    }
177*a1a3b679SAndreas Boehler
178*a1a3b679SAndreas Boehler    function testCreateFileWrongComponent() {
179*a1a3b679SAndreas Boehler
180*a1a3b679SAndreas Boehler        $request = HTTP\Sapi::createFromServerArray(array(
181*a1a3b679SAndreas Boehler            'REQUEST_METHOD' => 'PUT',
182*a1a3b679SAndreas Boehler            'REQUEST_URI' => '/calendars/admin/calendar1/blabla.ics',
183*a1a3b679SAndreas Boehler        ));
184*a1a3b679SAndreas Boehler        $request->setBody("BEGIN:VCALENDAR\r\nBEGIN:VTIMEZONE\r\nEND:VTIMEZONE\r\nBEGIN:VFREEBUSY\r\nUID:foo\r\nEND:VFREEBUSY\r\nEND:VCALENDAR\r\n");
185*a1a3b679SAndreas Boehler
186*a1a3b679SAndreas Boehler        $response = $this->request($request);
187*a1a3b679SAndreas Boehler
188*a1a3b679SAndreas Boehler        $this->assertEquals(400, $response->status, 'Incorrect status returned! Full response body: ' . $response->body);
189*a1a3b679SAndreas Boehler
190*a1a3b679SAndreas Boehler    }
191*a1a3b679SAndreas Boehler
192*a1a3b679SAndreas Boehler    function testUpdateFile() {
193*a1a3b679SAndreas Boehler
194*a1a3b679SAndreas Boehler        $this->calBackend->createCalendarObject('calendar1','blabla.ics','foo');
195*a1a3b679SAndreas Boehler        $request = HTTP\Sapi::createFromServerArray(array(
196*a1a3b679SAndreas Boehler            'REQUEST_METHOD' => 'PUT',
197*a1a3b679SAndreas Boehler            'REQUEST_URI' => '/calendars/admin/calendar1/blabla.ics',
198*a1a3b679SAndreas Boehler        ));
199*a1a3b679SAndreas Boehler
200*a1a3b679SAndreas Boehler        $response = $this->request($request);
201*a1a3b679SAndreas Boehler
202*a1a3b679SAndreas Boehler        $this->assertEquals(415, $response->status);
203*a1a3b679SAndreas Boehler
204*a1a3b679SAndreas Boehler    }
205*a1a3b679SAndreas Boehler
206*a1a3b679SAndreas Boehler    function testUpdateFileParsableBody() {
207*a1a3b679SAndreas Boehler
208*a1a3b679SAndreas Boehler        $this->calBackend->createCalendarObject('calendar1','blabla.ics','foo');
209*a1a3b679SAndreas Boehler        $request = HTTP\Sapi::createFromServerArray(array(
210*a1a3b679SAndreas Boehler            'REQUEST_METHOD' => 'PUT',
211*a1a3b679SAndreas Boehler            'REQUEST_URI' => '/calendars/admin/calendar1/blabla.ics',
212*a1a3b679SAndreas Boehler        ));
213*a1a3b679SAndreas Boehler        $body = "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nUID:foo\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n";
214*a1a3b679SAndreas Boehler        $request->setBody($body);
215*a1a3b679SAndreas Boehler
216*a1a3b679SAndreas Boehler        $response = $this->request($request);
217*a1a3b679SAndreas Boehler
218*a1a3b679SAndreas Boehler        $this->assertEquals(204, $response->status);
219*a1a3b679SAndreas Boehler
220*a1a3b679SAndreas Boehler        $expected = array(
221*a1a3b679SAndreas Boehler            'uri'          => 'blabla.ics',
222*a1a3b679SAndreas Boehler            'calendardata' => $body,
223*a1a3b679SAndreas Boehler            'calendarid'   => 'calendar1',
224*a1a3b679SAndreas Boehler            'lastmodified' => null,
225*a1a3b679SAndreas Boehler        );
226*a1a3b679SAndreas Boehler
227*a1a3b679SAndreas Boehler        $this->assertEquals($expected, $this->calBackend->getCalendarObject('calendar1','blabla.ics'));
228*a1a3b679SAndreas Boehler
229*a1a3b679SAndreas Boehler    }
230*a1a3b679SAndreas Boehler
231*a1a3b679SAndreas Boehler    function testCreateFileInvalidComponent() {
232*a1a3b679SAndreas Boehler
233*a1a3b679SAndreas Boehler        $request = HTTP\Sapi::createFromServerArray(array(
234*a1a3b679SAndreas Boehler            'REQUEST_METHOD' => 'PUT',
235*a1a3b679SAndreas Boehler            'REQUEST_URI' => '/calendars/admin/calendar2/blabla.ics',
236*a1a3b679SAndreas Boehler        ));
237*a1a3b679SAndreas Boehler        $request->setBody("BEGIN:VCALENDAR\r\nBEGIN:VTIMEZONE\r\nEND:VTIMEZONE\r\nBEGIN:VEVENT\r\nUID:foo\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n");
238*a1a3b679SAndreas Boehler
239*a1a3b679SAndreas Boehler        $response = $this->request($request);
240*a1a3b679SAndreas Boehler
241*a1a3b679SAndreas Boehler        $this->assertEquals(403, $response->status, 'Incorrect status returned! Full response body: ' . $response->body);
242*a1a3b679SAndreas Boehler
243*a1a3b679SAndreas Boehler    }
244*a1a3b679SAndreas Boehler
245*a1a3b679SAndreas Boehler    function testUpdateFileInvalidComponent() {
246*a1a3b679SAndreas Boehler
247*a1a3b679SAndreas Boehler        $this->calBackend->createCalendarObject('calendar2','blabla.ics','foo');
248*a1a3b679SAndreas Boehler        $request = HTTP\Sapi::createFromServerArray(array(
249*a1a3b679SAndreas Boehler            'REQUEST_METHOD' => 'PUT',
250*a1a3b679SAndreas Boehler            'REQUEST_URI' => '/calendars/admin/calendar2/blabla.ics',
251*a1a3b679SAndreas Boehler        ));
252*a1a3b679SAndreas Boehler        $request->setBody("BEGIN:VCALENDAR\r\nBEGIN:VTIMEZONE\r\nEND:VTIMEZONE\r\nBEGIN:VEVENT\r\nUID:foo\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n");
253*a1a3b679SAndreas Boehler
254*a1a3b679SAndreas Boehler        $response = $this->request($request);
255*a1a3b679SAndreas Boehler
256*a1a3b679SAndreas Boehler        $this->assertEquals(403, $response->status, 'Incorrect status returned! Full response body: ' . $response->body);
257*a1a3b679SAndreas Boehler
258*a1a3b679SAndreas Boehler    }
259*a1a3b679SAndreas Boehler
260*a1a3b679SAndreas Boehler    /**
261*a1a3b679SAndreas Boehler     * What we are testing here, is if we send in a latin1 character, the
262*a1a3b679SAndreas Boehler     * server should automatically transform this into UTF-8.
263*a1a3b679SAndreas Boehler     *
264*a1a3b679SAndreas Boehler     * More importantly. If any transformation happens, the etag must no longer
265*a1a3b679SAndreas Boehler     * be returned by the server.
266*a1a3b679SAndreas Boehler     */
267*a1a3b679SAndreas Boehler    function testCreateFileModified() {
268*a1a3b679SAndreas Boehler
269*a1a3b679SAndreas Boehler        $request = HTTP\Sapi::createFromServerArray(array(
270*a1a3b679SAndreas Boehler            'REQUEST_METHOD' => 'PUT',
271*a1a3b679SAndreas Boehler            'REQUEST_URI' => '/calendars/admin/calendar1/blabla.ics',
272*a1a3b679SAndreas Boehler        ));
273*a1a3b679SAndreas Boehler        $request->setBody("BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nUID:foo\r\nSUMMARY:Meeting in M\xfcnster\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n");
274*a1a3b679SAndreas Boehler
275*a1a3b679SAndreas Boehler        $response = $this->request($request);
276*a1a3b679SAndreas Boehler
277*a1a3b679SAndreas Boehler        $this->assertEquals(201, $response->status, 'Incorrect status returned! Full response body: ' . $response->body);
278*a1a3b679SAndreas Boehler        $this->assertNull($response->getHeader('ETag'));
279*a1a3b679SAndreas Boehler
280*a1a3b679SAndreas Boehler    }
281*a1a3b679SAndreas Boehler}
282