3 * Tine 2.0 - http://www.tine20.org
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>
12 * Test class for Tinebase_Server_WebDAV
16 class Tinebase_Server_WebDAVTests extends ServerTestCase
18 #protected $_logPriority = Zend_Log::DEBUG;
21 * test general functionality of Tinebase_Server_WebDAV
24 public function testServer($noAssert = false)
26 $request = \Zend\Http\PhpEnvironment\Request::fromString(<<<EOS
27 PROPFIND /calendars/64d7fdf9202f7b1faf7467f5066d461c2e75cf2b/4/ HTTP/1.1
30 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:15.0) Gecko/20120824 Thunderbird/15.0 Lightning/1.7
34 $_SERVER['REQUEST_METHOD'] = $request->getMethod();
35 $_SERVER['REQUEST_URI'] = $request->getUri()->getPath();
36 $_SERVER['HTTP_DEPTH'] = '0';
38 $credentials = $this->getTestCredentials();
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');
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>');
50 $server = new Tinebase_Server_WebDAV();
52 $server->handle($request, $body);
54 $result = ob_get_contents();
58 if (true === $noAssert) {
62 $this->assertEquals('PD94bWwgdmVyc2lvbj0iMS4wIiBlbm', substr(base64_encode($result),0,30));
65 public function testDenyingWebDavClient()
67 Tinebase_Config::getInstance()->set(Tinebase_Config::DENY_WEBDAV_CLIENT_LIST, array('/deniedClient/'));
69 $_SERVER['HTTP_USER_AGENT'] = 'deniedClient';
70 static::assertTrue(empty($this->testServer(true)));
74 * test general functionality of Tinebase_Server_WebDAV
77 public function testServerWithAuthorizationHeader()
79 $credentials = $this->getTestCredentials();
81 $hash = base64_encode($credentials['username'] . ':' . $credentials['password']);
83 $request = \Zend\Http\PhpEnvironment\Request::fromString(
84 "PROPFIND /calendars/64d7fdf9202f7b1faf7467f5066d461c2e75cf2b/4/ HTTP/1.1\r\n"
85 . "Host: localhost\r\n"
87 . "User-Agent: Mozilla/5.0 (X11; Linux i686; rv:15.0) Gecko/20120824 Thunderbird/15.0 Lightning/1.7\r\n"
88 . "Authorization: Basic $hash\r\n"
91 $_SERVER['REQUEST_METHOD'] = $request->getMethod();
92 $_SERVER['REQUEST_URI'] = $request->getUri()->getPath();
93 $_SERVER['HTTP_DEPTH'] = '0';
95 $request->getServer()->set('REMOTE_ADDR', 'localhost');
97 $body = fopen('php://temp', 'r+');
98 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>');
103 $server = new Tinebase_Server_WebDAV();
105 $server->handle($request, $body);
107 $result = ob_get_contents();
111 print_r($request->getHeader('HTTP_AUTHORIZATION'));
112 print_r($request->getHeaders());
114 $this->assertEquals('PD94bWwgdmVyc2lvbj0iMS4wIiBlbm', substr(base64_encode($result),0,30), $result);
118 * test general functionality of Tinebase_Server_WebDAV
121 public function testServerWithAuthorizationEnv()
123 $credentials = $this->getTestCredentials();
125 $hash = base64_encode($credentials['username'] . ':' . $credentials['password']);
127 $request = \Zend\Http\PhpEnvironment\Request::fromString(<<<EOS
128 PROPFIND /calendars/64d7fdf9202f7b1faf7467f5066d461c2e75cf2b/4/ HTTP/1.1
131 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:15.0) Gecko/20120824 Thunderbird/15.0 Lightning/1.7
135 $_SERVER['REQUEST_METHOD'] = $request->getMethod();
136 $_SERVER['REQUEST_URI'] = $request->getUri()->getPath();
137 $_SERVER['HTTP_DEPTH'] = '0';
139 $request->getServer()->set('HTTP_AUTHORIZATION', 'Basic ' . $hash);
140 $request->getServer()->set('REMOTE_ADDR', 'localhost');
142 $body = fopen('php://temp', 'r+');
143 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>');
148 $server = new Tinebase_Server_WebDAV();
150 $server->handle($request, $body);
152 $result = ob_get_contents();
156 $this->assertEquals('PD94bWwgdmVyc2lvbj0iMS4wIiBlbm', substr(base64_encode($result),0,30));
160 * test general functionality of Tinebase_Server_WebDAV
163 public function testServerWithAuthorizationRemoteUser()
165 $credentials = $this->getTestCredentials();
167 $hash = base64_encode($credentials['username'] . ':' . $credentials['password']);
169 $request = \Zend\Http\PhpEnvironment\Request::fromString(<<<EOS
170 PROPFIND /calendars/64d7fdf9202f7b1faf7467f5066d461c2e75cf2b/4/ HTTP/1.1
173 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:15.0) Gecko/20120824 Thunderbird/15.0 Lightning/1.7
177 $_SERVER['REQUEST_METHOD'] = $request->getMethod();
178 $_SERVER['REQUEST_URI'] = $request->getUri()->getPath();
179 $_SERVER['HTTP_DEPTH'] = '0';
181 $request->getServer()->set('REDIRECT_REMOTE_USER', 'Basic ' . $hash);
182 $request->getServer()->set('REMOTE_ADDR', 'localhost');
184 $body = fopen('php://temp', 'r+');
185 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>');
190 $server = new Tinebase_Server_WebDAV();
192 $server->handle($request, $body);
194 $result = ob_get_contents();
198 $this->assertEquals('PD94bWwgdmVyc2lvbj0iMS4wIiBlbm', substr(base64_encode($result),0,30));
202 * test propfind for current-user-principal
204 * you have to provide a valid contactid
207 public function testPropfindCurrentUserPrincipal()
209 $request = \Zend\Http\PhpEnvironment\Request::fromString(<<<EOS
210 PROPFIND /principals/users/ HTTP/1.1
213 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:15.0) Gecko/20120824 Thunderbird/15.0 Lightning/1.7
217 $_SERVER['REQUEST_METHOD'] = $request->getMethod();
218 $_SERVER['REQUEST_URI'] = $request->getUri()->getPath();
219 $_SERVER['HTTP_DEPTH'] = '0';
221 $credentials = $this->getTestCredentials();
223 $request->getServer()->set('PHP_AUTH_USER', $credentials['username']);
224 $request->getServer()->set('PHP_AUTH_PW', $credentials['password']);
225 $request->getServer()->set('REMOTE_ADDR', 'localhost');
227 $body = fopen('php://temp', 'r+');
228 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>');
231 $bbody = fopen('php://temp', 'r+');
232 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>');
237 $server = new Tinebase_Server_WebDAV();
239 $server->handle($request, $body);
241 $result = ob_get_contents();
247 $responseDoc = new DOMDocument();
248 $responseDoc->loadXML($result);
249 #$responseDoc->formatOutput = true; error_log($responseDoc->saveXML());
250 $xpath = new DomXPath($responseDoc);
251 $xpath->registerNamespace('d', 'DAV:');
253 $nodes = $xpath->query('//d:multistatus/d:response/d:propstat/d:prop/d:current-user-principal/d:href');
254 $this->assertEquals(1, $nodes->length, $responseDoc->saveXML());
255 $this->assertNotEmpty($nodes->item(0)->nodeValue, $responseDoc->saveXML());
259 * test propfind for current-user-principal
261 * you have to provide a valid contactid
264 public function testPropfindPrincipal()
266 $credentials = $this->getTestCredentials();
268 $account = $this->getAccountByName($credentials['username']);
270 $this->assertInstanceOf('Tinebase_Model_FullUser', $account);
272 $request = \Zend\Http\PhpEnvironment\Request::fromString(
273 "PROPFIND /principals/users/{$account->contact_id}/ HTTP/1.1\r\n"
274 . "Host: localhost\r\n"
276 . "User-Agent: Mozilla/5.0 (X11; Linux i686; rv:15.0) Gecko/20120824 Thunderbird/15.0 Lightning/1.7\r\n"
278 . "<?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"
281 $_SERVER['REQUEST_METHOD'] = $request->getMethod();
282 $_SERVER['REQUEST_URI'] = $request->getUri()->getPath();
283 $_SERVER['HTTP_DEPTH'] = '0';
285 $request->getServer()->set('PHP_AUTH_USER', $credentials['username']);
286 $request->getServer()->set('PHP_AUTH_PW', $credentials['password']);
287 $request->getServer()->set('REMOTE_ADDR', 'localhost');
291 $server = new Tinebase_Server_WebDAV();
293 $server->handle($request);
295 $result = ob_get_contents();
301 $responseDoc = new DOMDocument();
302 $responseDoc->loadXML($result);
303 #$responseDoc->formatOutput = true; error_log($responseDoc->saveXML());
304 $xpath = new DomXPath($responseDoc);
305 $xpath->registerNamespace('d', 'DAV:');
306 $xpath->registerNamespace('cal', 'urn:ietf:params:xml:ns:caldav');
308 $nodes = $xpath->query('//d:multistatus/d:response/d:propstat/d:prop/cal:calendar-home-set');
309 $this->assertEquals(1, $nodes->length, $responseDoc->saveXML());
310 $this->assertNotEmpty($nodes->item(0)->nodeValue, $responseDoc->saveXML());
312 $nodes = $xpath->query('//d:multistatus/d:response/d:propstat/d:prop/cal:calendar-user-address-set');
313 $this->assertEquals(1, $nodes->length, $responseDoc->saveXML());
314 $this->assertNotEmpty($nodes->item(0)->nodeValue, $responseDoc->saveXML());
316 $nodes = $xpath->query('//d:multistatus/d:response/d:propstat/d:prop/cal:schedule-inbox-URL');
317 $this->assertEquals(1, $nodes->length, $responseDoc->saveXML());
318 $this->assertNotEmpty($nodes->item(0)->nodeValue, $responseDoc->saveXML());
322 * test general functionality of Tinebase_Server_WebDAV
325 public function testReportQuery()
327 $credentials = $this->getTestCredentials();
329 $account = $this->getAccountByName($credentials['username']);
331 $this->assertInstanceOf('Tinebase_Model_FullUser', $account);
332 if (Tinebase_Core::getUser() === null) {
333 Tinebase_Core::set(Tinebase_Core::USER, $account);
336 $containerId = $this->getPersonalContainer($account, 'Calendar_Model_Event')
340 $request = \Zend\Http\PhpEnvironment\Request::fromString(<<<EOS
341 REPORT /calendars/{$account->contact_id}/{$containerId}/ HTTP/1.1
344 Content-Type: application/xml; charset="utf-8"
345 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:15.0) Gecko/20120824 Thunderbird/15.0 Lightning/1.7
347 <?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>
351 $_SERVER['REQUEST_METHOD'] = $request->getMethod();
352 $_SERVER['REQUEST_URI'] = $request->getUri()->getPath();
353 $_SERVER['HTTP_DEPTH'] = '0';
355 $request->getServer()->set('PHP_AUTH_USER', $credentials['username']);
356 $request->getServer()->set('PHP_AUTH_PW', $credentials['password']);
357 $request->getServer()->set('REMOTE_ADDR', 'localhost');
361 $server = new Tinebase_Server_WebDAV();
363 $server->handle($request);
365 $result = ob_get_contents();
369 $responseDoc = new DOMDocument();
370 $responseDoc->loadXML($result);
371 #$responseDoc->formatOutput = true; error_log($responseDoc->saveXML());
373 $xpath = new DomXPath($responseDoc);
374 $xpath->registerNamespace('cal', 'urn:ietf:params:xml:ns:caldav');
375 $xpath->registerNamespace('cs', 'http://calendarserver.org/ns/');
377 #$nodes = $xpath->query('//d:multistatus/d:response/d:propstat/d:prop/d:current-user-principal');
378 #$this->assertEquals(1, $nodes->length, $responseDoc->saveXML());
381 $this->assertEquals('PD94bWwgdmVyc2lvbj0iMS4wIiBlbm', substr(base64_encode($result),0,30));
385 * test PROPFIND on calendar
389 public function testPropfindThundebird()
391 $credentials = $this->getTestCredentials();
393 $account = $this->getAccountByName($credentials['username']);
395 $this->assertInstanceOf('Tinebase_Model_FullUser', $account);
397 if (Tinebase_Core::getUser() === null) {
398 Tinebase_Core::set(Tinebase_Core::USER, $account);
401 $containerId = $this->getPersonalContainer($account, 'Calendar_Model_Event')
405 $request = \Zend\Http\PhpEnvironment\Request::fromString(
406 "PROPFIND /calendars/{$account->contact_id}/{$containerId}/ HTTP/1.1" . "\r\n"
407 . "Host: localhost" . "\r\n"
408 . "Depth: 1" . "\r\n"
409 . 'Content-Type: application/xml; charset="utf-8"' . "\r\n"
410 . "User-Agent: Mozilla/5.0 (X11; Linux i686; rv:15.0) Gecko/20120824 Thunderbird/15.0 Lightning/1.7" . "\r\n"
412 . '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
413 . '<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>'
417 $_SERVER['REQUEST_METHOD'] = $request->getMethod();
418 $_SERVER['REQUEST_URI'] = $request->getUri()->getPath();
419 $_SERVER['HTTP_DEPTH'] = '0';
421 $request->getServer()->set('PHP_AUTH_USER', $credentials['username']);
422 $request->getServer()->set('PHP_AUTH_PW', $credentials['password']);
423 $request->getServer()->set('REMOTE_ADDR', 'localhost');
427 $server = new Tinebase_Server_WebDAV();
429 $server->handle($request);
431 $result = ob_get_contents();
435 $responseDoc = new DOMDocument();
436 $responseDoc->loadXML($result);
437 #$responseDoc->formatOutput = true; error_log($responseDoc->saveXML());
439 $xpath = new DomXPath($responseDoc);
440 $xpath->registerNamespace('cal', 'urn:ietf:params:xml:ns:caldav');
441 $xpath->registerNamespace('cs', 'http://calendarserver.org/ns/');
443 $nodes = $xpath->query('//d:multistatus/d:response/d:propstat/d:prop/d:current-user-principal');
444 $this->assertEquals(1, $nodes->length, $responseDoc->saveXML());