]> git.mxchange.org Git - friendica-addons.git/blob - dav/SabreDAV/tests/Sabre/DAV/ServerPropsTest.php
Merge branch 'master' of git://github.com/friendica/friendica-addons
[friendica-addons.git] / dav / SabreDAV / tests / Sabre / DAV / ServerPropsTest.php
1 <?php
2
3 require_once 'Sabre/HTTP/ResponseMock.php';
4 require_once 'Sabre/DAV/AbstractServer.php';
5
6 class Sabre_DAV_ServerPropsTest extends Sabre_DAV_AbstractServer {
7
8     protected function getRootNode() {
9
10         return new Sabre_DAV_FSExt_Directory(SABRE_TEMPDIR);
11
12     }
13
14     function setUp() {
15
16         if (file_exists(SABRE_TEMPDIR.'../.sabredav')) unlink(SABRE_TEMPDIR.'../.sabredav');
17         parent::setUp();
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')));
22
23     }
24
25     function tearDown() {
26
27         parent::tearDown();
28         if (file_exists(SABRE_TEMPDIR.'../.locksdb')) unlink(SABRE_TEMPDIR.'../.locksdb');
29
30     }
31
32     private function sendRequest($body) {
33
34         $serverVars = array(
35             'REQUEST_URI'    => '/',
36             'REQUEST_METHOD' => 'PROPFIND',
37             'HTTP_DEPTH'          => '0',
38         );
39
40         $request = new Sabre_HTTP_Request($serverVars);
41         $request->setBody($body);
42
43         $this->server->httpRequest = ($request);
44         $this->server->exec();
45
46     }
47
48     public function testPropFindEmptyBody() {
49
50         $this->sendRequest("");
51
52         $this->assertEquals('HTTP/1.1 207 Multi-Status',$this->response->status);
53
54         $this->assertEquals(array(
55                 'Content-Type' => 'application/xml; charset=utf-8',
56                 'DAV' => '1, 3, extended-mkcol, 2',
57             ),
58             $this->response->headers
59          );
60
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');
64
65         list($data) = $xml->xpath('/d:multistatus/d:response/d:href');
66         $this->assertEquals('/',(string)$data,'href element should have been /');
67
68         $data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:resourcetype');
69         $this->assertEquals(1,count($data));
70
71     }
72
73     function testSupportedLocks() {
74
75         $xml = '<?xml version="1.0"?>
76 <d:propfind xmlns:d="DAV:">
77   <d:prop>
78     <d:supportedlock />
79   </d:prop>
80 </d:propfind>';
81
82         $this->sendRequest($xml);
83
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');
87
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');
90
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');
93
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');
96
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');
99
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');
102
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');
105     }
106
107     function testLockDiscovery() {
108
109         $xml = '<?xml version="1.0"?>
110 <d:propfind xmlns:d="DAV:">
111   <d:prop>
112     <d:lockdiscovery />
113   </d:prop>
114 </d:propfind>';
115
116         $this->sendRequest($xml);
117
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');
121
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');
124
125     }
126
127     function testUnknownProperty() {
128
129         $xml = '<?xml version="1.0"?>
130 <d:propfind xmlns:d="DAV:">
131   <d:prop>
132     <d:macaroni />
133   </d:prop>
134 </d:propfind>';
135
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');
140         $pathTests = array(
141             '/d:multistatus',
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',
147         );
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);
150         }
151
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]);
155
156     }
157
158     /**
159      * @covers Sabre_DAV_Server::parsePropPatchRequest
160      */
161     public function testParsePropPatchRequest() {
162
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>';
170
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,
176             ), $result);
177
178     }
179
180     /**
181      * @covers Sabre_DAV_Server::updateProperties
182      */
183     public function testUpdateProperties() {
184
185         $props = array(
186             '{http://sabredav.org/NS/test}someprop' => 'somevalue',
187         );
188
189         $result = $this->server->updateProperties('/test2.txt',$props);
190
191         $this->assertEquals(array(
192             '200' => array('{http://sabredav.org/NS/test}someprop' => null),
193             'href' => '/test2.txt',
194         ), $result);
195
196     }
197
198     /**
199      * @covers Sabre_DAV_Server::updateProperties
200      * @depends testUpdateProperties
201      */
202     public function testUpdatePropertiesProtected() {
203
204         $props = array(
205             '{http://sabredav.org/NS/test}someprop' => 'somevalue',
206             '{DAV:}getcontentlength' => 50,
207         );
208
209         $result = $this->server->updateProperties('/test2.txt',$props);
210
211         $this->assertEquals(array(
212             '424' => array('{http://sabredav.org/NS/test}someprop' => null),
213             '403' => array('{DAV:}getcontentlength' => null),
214             'href' => '/test2.txt',
215         ), $result);
216
217     }
218
219     /**
220      * @covers Sabre_DAV_Server::updateProperties
221      * @depends testUpdateProperties
222      */
223     public function testUpdatePropertiesFail1() {
224
225         $dir = new Sabre_DAV_PropTestDirMock('updatepropsfalse');
226         $objectTree = new Sabre_DAV_ObjectTree($dir);
227         $this->server->tree = $objectTree;
228
229         $props = array(
230             '{http://sabredav.org/NS/test}someprop' => 'somevalue',
231         );
232
233         $result = $this->server->updateProperties('/',$props);
234
235         $this->assertEquals(array(
236             '403' => array('{http://sabredav.org/NS/test}someprop' => null),
237             'href' => '/',
238         ), $result);
239
240     }
241
242     /**
243      * @covers Sabre_DAV_Server::updateProperties
244      * @depends testUpdateProperties
245      */
246     public function testUpdatePropertiesFail2() {
247
248         $dir = new Sabre_DAV_PropTestDirMock('updatepropsarray');
249         $objectTree = new Sabre_DAV_ObjectTree($dir);
250         $this->server->tree = $objectTree;
251
252         $props = array(
253             '{http://sabredav.org/NS/test}someprop' => 'somevalue',
254         );
255
256         $result = $this->server->updateProperties('/',$props);
257
258         $this->assertEquals(array(
259             '402' => array('{http://sabredav.org/NS/test}someprop' => null),
260             'href' => '/',
261         ), $result);
262
263     }
264
265     /**
266      * @covers Sabre_DAV_Server::updateProperties
267      * @depends testUpdateProperties
268      * @expectedException Sabre_DAV_Exception
269      */
270     public function testUpdatePropertiesFail3() {
271
272         $dir = new Sabre_DAV_PropTestDirMock('updatepropsobj');
273         $objectTree = new Sabre_DAV_ObjectTree($dir);
274         $this->server->tree = $objectTree;
275
276         $props = array(
277             '{http://sabredav.org/NS/test}someprop' => 'somevalue',
278         );
279
280         $result = $this->server->updateProperties('/',$props);
281
282     }
283
284     /**
285      * @depends testParsePropPatchRequest
286      * @depends testUpdateProperties
287      * @covers Sabre_DAV_Server::httpPropPatch
288      */
289     public function testPropPatch() {
290
291         $serverVars = array(
292             'REQUEST_URI'    => '/',
293             'REQUEST_METHOD' => 'PROPPATCH',
294         );
295
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>';
300
301         $request = new Sabre_HTTP_Request($serverVars);
302         $request->setBody($body);
303
304         $this->server->httpRequest = ($request);
305         $this->server->exec();
306
307         $this->assertEquals(array(
308                 'Content-Type' => 'application/xml; charset=utf-8',
309             ),
310             $this->response->headers
311          );
312
313         $this->assertEquals('HTTP/1.1 207 Multi-Status',$this->response->status,'We got the wrong status. Full XML response: ' . $this->response->body);
314
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');
319
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);
322
323         $data = $xml->xpath('//bla:someprop');
324         $this->assertEquals(1,count($data),'We expected one \'s:someprop\' element. Response body: ' . $body);
325
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);
328
329         $this->assertEquals('HTTP/1.1 200 OK',(string)$data[0]);
330
331     }
332
333     /**
334      * @depends testPropPatch
335      */
336     public function testPropPatchAndFetch() {
337
338         $this->testPropPatch();
339         $xml = '<?xml version="1.0"?>
340 <d:propfind xmlns:d="DAV:" xmlns:s="http://www.rooftopsolutions.nl/testnamespace">
341   <d:prop>
342     <s:someprop />
343   </d:prop>
344 </d:propfind>';
345
346         $this->sendRequest($xml);
347
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');
352
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);
357
358     }
359
360 }
361
362 class Sabre_DAV_PropTestDirMock extends Sabre_DAV_SimpleCollection implements Sabre_DAV_IProperties {
363
364     public $type;
365
366     function __construct($type) {
367
368         $this->type =$type;
369         parent::__construct('root');
370
371     }
372
373     function updateProperties($updateProperties) {
374
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;
380                 return $r;
381             case 'updatepropsobj' :
382                 return new STDClass();
383         }
384
385     }
386
387     function getProperties($requestedPropeties) {
388
389         return array();
390
391     }
392
393 }