0c8bbff3aeefd4146539010baf7268f45bd3e319
[tine20] / tests / tine20 / Tinebase / Server / WebDAVTests.php
1 <?php
2 /**
3  * Tine 2.0 - http://www.tine20.org
4  * 
5  * @package     Tinebase
6  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
7  * @copyright   Copyright (c) 2012-2014 Metaways Infosystems GmbH (http://www.metaways.de)
8  * @author      Lars Kneschke <l.kneschke@metaways.de>
9  */
10
11 /**
12  * Test class for Tinebase_Server_WebDAV
13  * 
14  * @package     Tinebase
15  */
16 class Tinebase_Server_WebDAVTests extends ServerTestCase
17 {
18     #protected $_logPriority = Zend_Log::DEBUG;
19     
20     /**
21      * test general functionality of Tinebase_Server_WebDAV
22      * @group ServerTests
23      */
24     public function testServer()
25     {
26         $request = \Zend\Http\PhpEnvironment\Request::fromString(<<<EOS
27 PROPFIND /calendars/64d7fdf9202f7b1faf7467f5066d461c2e75cf2b/4/ HTTP/1.1
28 Host: localhost
29 Depth: 0
30 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:15.0) Gecko/20120824 Thunderbird/15.0 Lightning/1.7
31 EOS
32         );
33         
34         $_SERVER['REQUEST_METHOD'] = $request->getMethod();
35         $_SERVER['REQUEST_URI']    = $request->getUri()->getPath();
36         $_SERVER['HTTP_DEPTH']     = '0';
37         
38         $credentials = $this->getTestCredentials();
39         
40         $request->getServer()->set('PHP_AUTH_USER', $credentials['username']);
41         $request->getServer()->set('PHP_AUTH_PW',   $credentials['password']);
42         $request->getServer()->set('REMOTE_ADDR',   'localhost');
43         
44         $body = fopen('php://temp', 'r+');
45         fwrite($body, '<?xml version="1.0" encoding="UTF-8"?><D:propfind xmlns:D="DAV:" xmlns:CS="http://calendarserver.org/ns/"><D:prop><CS:getctag/></D:prop></D:propfind>');
46         rewind($body);
47         
48         ob_start();
49         
50         $server = new Tinebase_Server_WebDAV();
51         
52         $server->handle($request, $body);
53         
54         $result = ob_get_contents();
55         
56         ob_end_clean();
57         
58         $this->assertEquals('PD94bWwgdmVyc2lvbj0iMS4wIiBlbm', substr(base64_encode($result),0,30));
59     }
60     
61     /**
62      * test general functionality of Tinebase_Server_WebDAV
63      * @group ServerTests
64      */
65     public function testServerWithAuthorizationHeader()
66     {
67         $credentials = $this->getTestCredentials();
68         
69         $hash = base64_encode($credentials['username'] . ':' . $credentials['password']);
70         
71         $request = \Zend\Http\PhpEnvironment\Request::fromString(
72 "PROPFIND /calendars/64d7fdf9202f7b1faf7467f5066d461c2e75cf2b/4/ HTTP/1.1\r\n"
73 . "Host: localhost\r\n"
74 . "Depth: 0\r\n"
75 . "User-Agent: Mozilla/5.0 (X11; Linux i686; rv:15.0) Gecko/20120824 Thunderbird/15.0 Lightning/1.7\r\n"
76 . "Authorization: Basic $hash\r\n"
77         );
78         
79         $_SERVER['REQUEST_METHOD'] = $request->getMethod();
80         $_SERVER['REQUEST_URI']    = $request->getUri()->getPath();
81         $_SERVER['HTTP_DEPTH']     = '0';
82         
83         $request->getServer()->set('REMOTE_ADDR', 'localhost');
84         
85         $body = fopen('php://temp', 'r+');
86         fwrite($body, '<?xml version="1.0" encoding="UTF-8"?><D:propfind xmlns:D="DAV:" xmlns:CS="http://calendarserver.org/ns/"><D:prop><CS:getctag/></D:prop></D:propfind>');
87         rewind($body);
88         
89         ob_start();
90         
91         $server = new Tinebase_Server_WebDAV();
92         
93         $server->handle($request, $body);
94         
95         $result = ob_get_contents();
96         
97         ob_end_clean();
98
99         print_r($request->getHeader('HTTP_AUTHORIZATION'));
100         print_r($request->getHeaders());
101         
102         $this->assertEquals('PD94bWwgdmVyc2lvbj0iMS4wIiBlbm', substr(base64_encode($result),0,30), $result);
103     }
104     
105     /**
106      * test general functionality of Tinebase_Server_WebDAV
107      * @group ServerTests
108      */
109     public function testServerWithAuthorizationEnv()
110     {
111         $credentials = $this->getTestCredentials();
112         
113         $hash = base64_encode($credentials['username'] . ':' . $credentials['password']);
114         
115         $request = \Zend\Http\PhpEnvironment\Request::fromString(<<<EOS
116 PROPFIND /calendars/64d7fdf9202f7b1faf7467f5066d461c2e75cf2b/4/ HTTP/1.1
117 Host: localhost
118 Depth: 0
119 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:15.0) Gecko/20120824 Thunderbird/15.0 Lightning/1.7
120 EOS
121         );
122         
123         $_SERVER['REQUEST_METHOD'] = $request->getMethod();
124         $_SERVER['REQUEST_URI']    = $request->getUri()->getPath();
125         $_SERVER['HTTP_DEPTH']     = '0';
126         
127         $request->getServer()->set('HTTP_AUTHORIZATION', 'Basic ' . $hash);
128         $request->getServer()->set('REMOTE_ADDR',          'localhost');
129         
130         $body = fopen('php://temp', 'r+');
131         fwrite($body, '<?xml version="1.0" encoding="UTF-8"?><D:propfind xmlns:D="DAV:" xmlns:CS="http://calendarserver.org/ns/"><D:prop><CS:getctag/></D:prop></D:propfind>');
132         rewind($body);
133         
134         ob_start();
135         
136         $server = new Tinebase_Server_WebDAV();
137         
138         $server->handle($request, $body);
139         
140         $result = ob_get_contents();
141         
142         ob_end_clean();
143         
144         $this->assertEquals('PD94bWwgdmVyc2lvbj0iMS4wIiBlbm', substr(base64_encode($result),0,30));
145     }
146     
147     /**
148      * test general functionality of Tinebase_Server_WebDAV
149      * @group ServerTests
150      */
151     public function testServerWithAuthorizationRemoteUser()
152     {
153         $credentials = $this->getTestCredentials();
154         
155         $hash = base64_encode($credentials['username'] . ':' . $credentials['password']);
156         
157         $request = \Zend\Http\PhpEnvironment\Request::fromString(<<<EOS
158 PROPFIND /calendars/64d7fdf9202f7b1faf7467f5066d461c2e75cf2b/4/ HTTP/1.1
159 Host: localhost
160 Depth: 0
161 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:15.0) Gecko/20120824 Thunderbird/15.0 Lightning/1.7
162 EOS
163         );
164         
165         $_SERVER['REQUEST_METHOD'] = $request->getMethod();
166         $_SERVER['REQUEST_URI']    = $request->getUri()->getPath();
167         $_SERVER['HTTP_DEPTH']     = '0';
168         
169         $request->getServer()->set('REDIRECT_REMOTE_USER', 'Basic ' . $hash);
170         $request->getServer()->set('REMOTE_ADDR',          'localhost');
171         
172         $body = fopen('php://temp', 'r+');
173         fwrite($body, '<?xml version="1.0" encoding="UTF-8"?><D:propfind xmlns:D="DAV:" xmlns:CS="http://calendarserver.org/ns/"><D:prop><CS:getctag/></D:prop></D:propfind>');
174         rewind($body);
175         
176         ob_start();
177         
178         $server = new Tinebase_Server_WebDAV();
179         
180         $server->handle($request, $body);
181         
182         $result = ob_get_contents();
183         
184         ob_end_clean();
185         
186         $this->assertEquals('PD94bWwgdmVyc2lvbj0iMS4wIiBlbm', substr(base64_encode($result),0,30));
187     }
188     
189     /**
190      * test propfind for current-user-principal
191      * 
192      * you have to provide a valid contactid
193      * @group ServerTests
194      */
195     public function testPropfindCurrentUserPrincipal()
196     {
197         $request = \Zend\Http\PhpEnvironment\Request::fromString(<<<EOS
198 PROPFIND /principals/users/ HTTP/1.1
199 Host: localhost
200 Depth: 0
201 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:15.0) Gecko/20120824 Thunderbird/15.0 Lightning/1.7
202 EOS
203         );
204         
205         $_SERVER['REQUEST_METHOD'] = $request->getMethod();
206         $_SERVER['REQUEST_URI']    = $request->getUri()->getPath();
207         $_SERVER['HTTP_DEPTH']     = '0';
208         
209         $credentials = $this->getTestCredentials();
210         
211         $request->getServer()->set('PHP_AUTH_USER', $credentials['username']);
212         $request->getServer()->set('PHP_AUTH_PW',   $credentials['password']);
213         $request->getServer()->set('REMOTE_ADDR',   'localhost');
214         
215         $body = fopen('php://temp', 'r+');
216         fwrite($body, '<?xml version="1.0" encoding="UTF-8"?><D:propfind xmlns:D="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav"><D:prop><D:current-user-principal/><D:principal-URL/><D:resourcetype/></D:prop></D:propfind>');
217         rewind($body);
218
219         $bbody = fopen('php://temp', 'r+');
220         fwrite($bbody, '<?xml version="1.0" encoding="UTF-8"?><D:propfind xmlns:D="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav"><D:prop><C:calendar-home-set/><C:calendar-user-address-set/><C:schedule-inbox-URL/><C:schedule-outbox-URL/></D:prop></D:propfind>');
221         rewind($bbody);
222         
223         ob_start();
224         
225         $server = new Tinebase_Server_WebDAV();
226         
227         $server->handle($request, $body);
228         
229         $result = ob_get_contents();
230         
231         ob_end_clean();
232         
233         #error_log($result);
234         
235         $responseDoc = new DOMDocument();
236         $responseDoc->loadXML($result);
237         #$responseDoc->formatOutput = true; error_log($responseDoc->saveXML());
238         $xpath = new DomXPath($responseDoc);
239         $xpath->registerNamespace('d', 'DAV:');
240         
241         $nodes = $xpath->query('//d:multistatus/d:response/d:propstat/d:prop/d:current-user-principal/d:href');
242         $this->assertEquals(1, $nodes->length, $responseDoc->saveXML());
243         $this->assertNotEmpty($nodes->item(0)->nodeValue, $responseDoc->saveXML());
244     }
245     
246     /**
247      * test propfind for current-user-principal
248      * 
249      * you have to provide a valid contactid
250      * @group ServerTests
251      */
252     public function testPropfindPrincipal()
253     {
254         $credentials = $this->getTestCredentials();
255         
256         $account = $this->getAccountByName($credentials['username']);
257         
258         $this->assertInstanceOf('Tinebase_Model_FullUser', $account);
259         
260         $request = \Zend\Http\PhpEnvironment\Request::fromString(
261 "PROPFIND /principals/users/{$account->contact_id}/ HTTP/1.1\r\n"
262 . "Host: localhost\r\n"
263 . "Depth: 0\r\n"
264 . "User-Agent: Mozilla/5.0 (X11; Linux i686; rv:15.0) Gecko/20120824 Thunderbird/15.0 Lightning/1.7\r\n"
265 . "\r\n"
266 . "<?xml version=\"1.0\" encoding=\"UTF-8\"?><D:propfind xmlns:D=\"DAV:\" xmlns:C=\"urn:ietf:params:xml:ns:caldav\"><D:prop><C:calendar-home-set/><C:calendar-user-address-set/><C:schedule-inbox-URL/><C:schedule-outbox-URL/></D:prop></D:propfind>\r\n"
267         );
268         
269         $_SERVER['REQUEST_METHOD'] = $request->getMethod();
270         $_SERVER['REQUEST_URI']    = $request->getUri()->getPath();
271         $_SERVER['HTTP_DEPTH']     = '0';
272         
273         $request->getServer()->set('PHP_AUTH_USER', $credentials['username']);
274         $request->getServer()->set('PHP_AUTH_PW',   $credentials['password']);
275         $request->getServer()->set('REMOTE_ADDR',   'localhost');
276         
277         ob_start();
278         
279         $server = new Tinebase_Server_WebDAV();
280         
281         $server->handle($request);
282         
283         $result = ob_get_contents();
284         
285         ob_end_clean();
286         
287         #error_log($result);
288         
289         $responseDoc = new DOMDocument();
290         $responseDoc->loadXML($result);
291         #$responseDoc->formatOutput = true; error_log($responseDoc->saveXML());
292         $xpath = new DomXPath($responseDoc);
293         $xpath->registerNamespace('d', 'DAV:');
294         $xpath->registerNamespace('cal', 'urn:ietf:params:xml:ns:caldav');
295         
296         $nodes = $xpath->query('//d:multistatus/d:response/d:propstat/d:prop/cal:calendar-home-set');
297         $this->assertEquals(1, $nodes->length, $responseDoc->saveXML());
298         $this->assertNotEmpty($nodes->item(0)->nodeValue, $responseDoc->saveXML());
299         
300         $nodes = $xpath->query('//d:multistatus/d:response/d:propstat/d:prop/cal:calendar-user-address-set');
301         $this->assertEquals(1, $nodes->length, $responseDoc->saveXML());
302         $this->assertNotEmpty($nodes->item(0)->nodeValue, $responseDoc->saveXML());
303         
304         $nodes = $xpath->query('//d:multistatus/d:response/d:propstat/d:prop/cal:schedule-inbox-URL');
305         $this->assertEquals(1, $nodes->length, $responseDoc->saveXML());
306         $this->assertNotEmpty($nodes->item(0)->nodeValue, $responseDoc->saveXML());
307     }
308     
309     /**
310      * test general functionality of Tinebase_Server_WebDAV
311      * @group ServerTests
312      */
313     public function testReportQuery()
314     {
315         $credentials = $this->getTestCredentials();
316         
317         $account = $this->getAccountByName($credentials['username']);
318         
319         $this->assertInstanceOf('Tinebase_Model_FullUser', $account);
320         if (Tinebase_Core::getUser() === null) {
321             Tinebase_Core::set(Tinebase_Core::USER, $account);
322         }
323         
324         $containerId = $this->getPersonalContainer($account, 'Calendar_Model_Event')
325             ->getFirstRecord()
326             ->getId();
327         
328         $request = \Zend\Http\PhpEnvironment\Request::fromString(<<<EOS
329 REPORT /calendars/{$account->contact_id}/{$containerId}/ HTTP/1.1
330 Host: localhost
331 Depth: 1
332 Content-Type: application/xml; charset="utf-8"
333 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:15.0) Gecko/20120824 Thunderbird/15.0 Lightning/1.7
334
335 <?xml version="1.0" encoding="utf-8" ?><C:calendar-query xmlns:D="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav"><D:prop><D:getetag/><C:calendar-data/></D:prop><C:filter><C:comp-filter name="VCALENDAR"><C:comp-filter name="VEVENT"><C:time-range start="20060104T000000Z" end="20160105T000000Z"/></C:comp-filter></C:comp-filter></C:filter></C:calendar-query>
336 EOS
337         );
338         
339         $_SERVER['REQUEST_METHOD'] = $request->getMethod();
340         $_SERVER['REQUEST_URI']    = $request->getUri()->getPath();
341         $_SERVER['HTTP_DEPTH']     = '0';
342         
343         $request->getServer()->set('PHP_AUTH_USER', $credentials['username']);
344         $request->getServer()->set('PHP_AUTH_PW',   $credentials['password']);
345         $request->getServer()->set('REMOTE_ADDR',   'localhost');
346         
347         ob_start();
348         
349         $server = new Tinebase_Server_WebDAV();
350         
351         $server->handle($request);
352         
353         $result = ob_get_contents();
354         
355         ob_end_clean();
356         
357         $responseDoc = new DOMDocument();
358         $responseDoc->loadXML($result);
359         #$responseDoc->formatOutput = true; error_log($responseDoc->saveXML());
360         
361         $xpath = new DomXPath($responseDoc);
362         $xpath->registerNamespace('cal', 'urn:ietf:params:xml:ns:caldav');
363         $xpath->registerNamespace('cs',  'http://calendarserver.org/ns/');
364         
365         #$nodes = $xpath->query('//d:multistatus/d:response/d:propstat/d:prop/d:current-user-principal');
366         #$this->assertEquals(1, $nodes->length, $responseDoc->saveXML());
367         
368         // emoty response
369         $this->assertEquals('PD94bWwgdmVyc2lvbj0iMS4wIiBlbm', substr(base64_encode($result),0,30));
370     }
371     
372     /**
373      * test PROPFIND on calendar
374      * 
375      * @group ServerTests
376      */
377     public function testPropfindThundebird()
378     {
379         $credentials = $this->getTestCredentials();
380         
381         $account = $this->getAccountByName($credentials['username']);
382         
383         $this->assertInstanceOf('Tinebase_Model_FullUser', $account);
384
385         if (Tinebase_Core::getUser() === null) {
386             Tinebase_Core::set(Tinebase_Core::USER, $account);
387         }
388         
389         $containerId = $this->getPersonalContainer($account, 'Calendar_Model_Event')
390             ->getFirstRecord()
391             ->getId();
392         
393         $request = \Zend\Http\PhpEnvironment\Request::fromString(
394 "PROPFIND /calendars/{$account->contact_id}/{$containerId}/ HTTP/1.1" . "\r\n"
395 . "Host: localhost" . "\r\n"
396 . "Depth: 1" . "\r\n"
397 . 'Content-Type: application/xml; charset="utf-8"' . "\r\n"
398 . "User-Agent: Mozilla/5.0 (X11; Linux i686; rv:15.0) Gecko/20120824 Thunderbird/15.0 Lightning/1.7" . "\r\n"
399 . "\r\n"
400 . '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
401 . '<D:propfind xmlns:D="DAV:" xmlns:CS="http://calendarserver.org/ns/" xmlns:C="urn:ietf:params:xml:ns:caldav"><D:prop><D:resourcetype/><D:owner/><D:current-user-principal/><D:supported-report-set/><C:supported-calendar-component-set/><CS:getctag/></D:prop></D:propfind>'
402 . "\r\n"
403         );
404
405         $_SERVER['REQUEST_METHOD'] = $request->getMethod();
406         $_SERVER['REQUEST_URI']    = $request->getUri()->getPath();
407         $_SERVER['HTTP_DEPTH']     = '0';
408         
409         $request->getServer()->set('PHP_AUTH_USER', $credentials['username']);
410         $request->getServer()->set('PHP_AUTH_PW',   $credentials['password']);
411         $request->getServer()->set('REMOTE_ADDR',   'localhost');
412         
413         ob_start();
414         
415         $server = new Tinebase_Server_WebDAV();
416         
417         $server->handle($request);
418         
419         $result = ob_get_contents();
420         
421         ob_end_clean();
422         
423         $responseDoc = new DOMDocument();
424         $responseDoc->loadXML($result);
425         #$responseDoc->formatOutput = true; error_log($responseDoc->saveXML());
426         
427         $xpath = new DomXPath($responseDoc);
428         $xpath->registerNamespace('cal', 'urn:ietf:params:xml:ns:caldav');
429         $xpath->registerNamespace('cs',  'http://calendarserver.org/ns/');
430         
431         $nodes = $xpath->query('//d:multistatus/d:response/d:propstat/d:prop/d:current-user-principal');
432         $this->assertEquals(1, $nodes->length, $responseDoc->saveXML());
433     }
434 }