3 require_once 'Sabre/HTTP/ResponseMock.php';
4 require_once 'Sabre/DAV/AbstractServer.php';
6 class Sabre_DAV_ServerPropsTest extends Sabre_DAV_AbstractServer {
8 protected function getRootNode() {
10 return new Sabre_DAV_FSExt_Directory(SABRE_TEMPDIR);
16 if (file_exists(SABRE_TEMPDIR.'../.sabredav')) unlink(SABRE_TEMPDIR.'../.sabredav');
18 file_put_contents(SABRE_TEMPDIR . '/test2.txt', 'Test contents2');
19 mkdir(SABRE_TEMPDIR . '/col');
20 file_put_contents(SABRE_TEMPDIR . 'col/test.txt', 'Test contents');
21 $this->server->addPlugin(new Sabre_DAV_Locks_Plugin(new Sabre_DAV_Locks_Backend_File(SABRE_TEMPDIR . '/.locksdb')));
28 if (file_exists(SABRE_TEMPDIR.'../.locksdb')) unlink(SABRE_TEMPDIR.'../.locksdb');
32 private function sendRequest($body) {
36 'REQUEST_METHOD' => 'PROPFIND',
40 $request = new Sabre_HTTP_Request($serverVars);
41 $request->setBody($body);
43 $this->server->httpRequest = ($request);
44 $this->server->exec();
48 public function testPropFindEmptyBody() {
50 $this->sendRequest("");
52 $this->assertEquals('HTTP/1.1 207 Multi-Status',$this->response->status);
54 $this->assertEquals(array(
55 'Content-Type' => 'application/xml; charset=utf-8',
56 'DAV' => '1, 3, extended-mkcol, 2',
58 $this->response->headers
61 $body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/","xmlns\\1=\"urn:DAV\"",$this->response->body);
62 $xml = simplexml_load_string($body);
63 $xml->registerXPathNamespace('d','urn:DAV');
65 list($data) = $xml->xpath('/d:multistatus/d:response/d:href');
66 $this->assertEquals('/',(string)$data,'href element should have been /');
68 $data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:resourcetype');
69 $this->assertEquals(1,count($data));
73 function testSupportedLocks() {
75 $xml = '<?xml version="1.0"?>
76 <d:propfind xmlns:d="DAV:">
82 $this->sendRequest($xml);
84 $body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/","xmlns\\1=\"urn:DAV\"",$this->response->body);
85 $xml = simplexml_load_string($body);
86 $xml->registerXPathNamespace('d','urn:DAV');
88 $data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supportedlock/d:lockentry');
89 $this->assertEquals(2,count($data),'We expected two \'d:lockentry\' tags');
91 $data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supportedlock/d:lockentry/d:lockscope');
92 $this->assertEquals(2,count($data),'We expected two \'d:lockscope\' tags');
94 $data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supportedlock/d:lockentry/d:locktype');
95 $this->assertEquals(2,count($data),'We expected two \'d:locktype\' tags');
97 $data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supportedlock/d:lockentry/d:lockscope/d:shared');
98 $this->assertEquals(1,count($data),'We expected a \'d:shared\' tag');
100 $data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supportedlock/d:lockentry/d:lockscope/d:exclusive');
101 $this->assertEquals(1,count($data),'We expected a \'d:exclusive\' tag');
103 $data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supportedlock/d:lockentry/d:locktype/d:write');
104 $this->assertEquals(2,count($data),'We expected two \'d:write\' tags');
107 function testLockDiscovery() {
109 $xml = '<?xml version="1.0"?>
110 <d:propfind xmlns:d="DAV:">
116 $this->sendRequest($xml);
118 $body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/","xmlns\\1=\"urn:DAV\"",$this->response->body);
119 $xml = simplexml_load_string($body);
120 $xml->registerXPathNamespace('d','urn:DAV');
122 $data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:lockdiscovery');
123 $this->assertEquals(1,count($data),'We expected a \'d:lockdiscovery\' tag');
127 function testUnknownProperty() {
129 $xml = '<?xml version="1.0"?>
130 <d:propfind xmlns:d="DAV:">
136 $this->sendRequest($xml);
137 $body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/","xmlns\\1=\"urn:DAV\"",$this->response->body);
138 $xml = simplexml_load_string($body);
139 $xml->registerXPathNamespace('d','urn:DAV');
142 '/d:multistatus/d:response',
143 '/d:multistatus/d:response/d:propstat',
144 '/d:multistatus/d:response/d:propstat/d:status',
145 '/d:multistatus/d:response/d:propstat/d:prop',
146 '/d:multistatus/d:response/d:propstat/d:prop/d:macaroni',
148 foreach($pathTests as $test) {
149 $this->assertTrue(count($xml->xpath($test))==true,'We expected the ' . $test . ' element to appear in the response, we got: ' . $body);
152 $val = $xml->xpath('/d:multistatus/d:response/d:propstat/d:status');
153 $this->assertEquals(1,count($val),$body);
154 $this->assertEquals('HTTP/1.1 404 Not Found',(string)$val[0]);
159 * @covers Sabre_DAV_Server::parsePropPatchRequest
161 public function testParsePropPatchRequest() {
163 $body = '<?xml version="1.0"?>
164 <d:propertyupdate xmlns:d="DAV:" xmlns:s="http://sabredav.org/NS/test">
165 <d:set><d:prop><s:someprop>somevalue</s:someprop></d:prop></d:set>
166 <d:remove><d:prop><s:someprop2 /></d:prop></d:remove>
167 <d:set><d:prop><s:someprop3>removeme</s:someprop3></d:prop></d:set>
168 <d:remove><d:prop><s:someprop3 /></d:prop></d:remove>
169 </d:propertyupdate>';
171 $result = $this->server->parsePropPatchRequest($body);
172 $this->assertEquals(array(
173 '{http://sabredav.org/NS/test}someprop' => 'somevalue',
174 '{http://sabredav.org/NS/test}someprop2' => null,
175 '{http://sabredav.org/NS/test}someprop3' => null,
181 * @covers Sabre_DAV_Server::updateProperties
183 public function testUpdateProperties() {
186 '{http://sabredav.org/NS/test}someprop' => 'somevalue',
189 $result = $this->server->updateProperties('/test2.txt',$props);
191 $this->assertEquals(array(
192 '200' => array('{http://sabredav.org/NS/test}someprop' => null),
193 'href' => '/test2.txt',
199 * @covers Sabre_DAV_Server::updateProperties
200 * @depends testUpdateProperties
202 public function testUpdatePropertiesProtected() {
205 '{http://sabredav.org/NS/test}someprop' => 'somevalue',
206 '{DAV:}getcontentlength' => 50,
209 $result = $this->server->updateProperties('/test2.txt',$props);
211 $this->assertEquals(array(
212 '424' => array('{http://sabredav.org/NS/test}someprop' => null),
213 '403' => array('{DAV:}getcontentlength' => null),
214 'href' => '/test2.txt',
220 * @covers Sabre_DAV_Server::updateProperties
221 * @depends testUpdateProperties
223 public function testUpdatePropertiesFail1() {
225 $dir = new Sabre_DAV_PropTestDirMock('updatepropsfalse');
226 $objectTree = new Sabre_DAV_ObjectTree($dir);
227 $this->server->tree = $objectTree;
230 '{http://sabredav.org/NS/test}someprop' => 'somevalue',
233 $result = $this->server->updateProperties('/',$props);
235 $this->assertEquals(array(
236 '403' => array('{http://sabredav.org/NS/test}someprop' => null),
243 * @covers Sabre_DAV_Server::updateProperties
244 * @depends testUpdateProperties
246 public function testUpdatePropertiesFail2() {
248 $dir = new Sabre_DAV_PropTestDirMock('updatepropsarray');
249 $objectTree = new Sabre_DAV_ObjectTree($dir);
250 $this->server->tree = $objectTree;
253 '{http://sabredav.org/NS/test}someprop' => 'somevalue',
256 $result = $this->server->updateProperties('/',$props);
258 $this->assertEquals(array(
259 '402' => array('{http://sabredav.org/NS/test}someprop' => null),
266 * @covers Sabre_DAV_Server::updateProperties
267 * @depends testUpdateProperties
268 * @expectedException Sabre_DAV_Exception
270 public function testUpdatePropertiesFail3() {
272 $dir = new Sabre_DAV_PropTestDirMock('updatepropsobj');
273 $objectTree = new Sabre_DAV_ObjectTree($dir);
274 $this->server->tree = $objectTree;
277 '{http://sabredav.org/NS/test}someprop' => 'somevalue',
280 $result = $this->server->updateProperties('/',$props);
285 * @depends testParsePropPatchRequest
286 * @depends testUpdateProperties
287 * @covers Sabre_DAV_Server::httpPropPatch
289 public function testPropPatch() {
292 'REQUEST_URI' => '/',
293 'REQUEST_METHOD' => 'PROPPATCH',
296 $body = '<?xml version="1.0"?>
297 <d:propertyupdate xmlns:d="DAV:" xmlns:s="http://www.rooftopsolutions.nl/testnamespace">
298 <d:set><d:prop><s:someprop>somevalue</s:someprop></d:prop></d:set>
299 </d:propertyupdate>';
301 $request = new Sabre_HTTP_Request($serverVars);
302 $request->setBody($body);
304 $this->server->httpRequest = ($request);
305 $this->server->exec();
307 $this->assertEquals(array(
308 'Content-Type' => 'application/xml; charset=utf-8',
310 $this->response->headers
313 $this->assertEquals('HTTP/1.1 207 Multi-Status',$this->response->status,'We got the wrong status. Full XML response: ' . $this->response->body);
315 $body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/","xmlns\\1=\"urn:DAV\"",$this->response->body);
316 $xml = simplexml_load_string($body);
317 $xml->registerXPathNamespace('d','urn:DAV');
318 $xml->registerXPathNamespace('bla','http://www.rooftopsolutions.nl/testnamespace');
320 $data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop');
321 $this->assertEquals(1,count($data),'We expected one \'d:prop\' element. Response body: ' . $body);
323 $data = $xml->xpath('//bla:someprop');
324 $this->assertEquals(1,count($data),'We expected one \'s:someprop\' element. Response body: ' . $body);
326 $data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:status');
327 $this->assertEquals(1,count($data),'We expected one \'s:status\' element. Response body: ' . $body);
329 $this->assertEquals('HTTP/1.1 200 OK',(string)$data[0]);
334 * @depends testPropPatch
336 public function testPropPatchAndFetch() {
338 $this->testPropPatch();
339 $xml = '<?xml version="1.0"?>
340 <d:propfind xmlns:d="DAV:" xmlns:s="http://www.rooftopsolutions.nl/testnamespace">
346 $this->sendRequest($xml);
348 $body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/","xmlns\\1=\"urn:DAV\"",$this->response->body);
349 $xml = simplexml_load_string($body);
350 $xml->registerXPathNamespace('d','urn:DAV');
351 $xml->registerXPathNamespace('bla','http://www.rooftopsolutions.nl/testnamespace');
353 $xpath='//bla:someprop';
354 $result = $xml->xpath($xpath);
355 $this->assertEquals(1,count($result),'We couldn\'t find our new property in the response. Full response body:' . "\n" . $body);
356 $this->assertEquals('somevalue',(string)$result[0],'We couldn\'t find our new property in the response. Full response body:' . "\n" . $body);
362 class Sabre_DAV_PropTestDirMock extends Sabre_DAV_SimpleCollection implements Sabre_DAV_IProperties {
366 function __construct($type) {
369 parent::__construct('root');
373 function updateProperties($updateProperties) {
375 switch($this->type) {
376 case 'updatepropsfalse' : return false;
377 case 'updatepropsarray' :
378 $r = array(402 => array());
379 foreach($updateProperties as $k=>$v) $r[402][$k] = null;
381 case 'updatepropsobj' :
382 return new STDClass();
387 function getProperties($requestedPropeties) {