]> git.mxchange.org Git - friendica-addons.git/blob - dav/SabreDAV/tests/Sabre/DAVACL/ExpandPropertiesTest.php
Merge branch 'master' of git://github.com/friendica/friendica-addons
[friendica-addons.git] / dav / SabreDAV / tests / Sabre / DAVACL / ExpandPropertiesTest.php
1 <?php
2
3 require_once 'Sabre/HTTP/ResponseMock.php';
4
5 class Sabre_DAVACL_ExpandPropertiesTest extends PHPUnit_Framework_TestCase {
6
7     function getServer() {
8
9         $tree = array(
10             new Sabre_DAVACL_MockPropertyNode('node1', array(
11                 '{http://sabredav.org/ns}simple' => 'foo',
12                 '{http://sabredav.org/ns}href'   => new Sabre_DAV_Property_Href('node2'),
13                 '{DAV:}displayname'     => 'Node 1',
14             )),
15             new Sabre_DAVACL_MockPropertyNode('node2', array(
16                 '{http://sabredav.org/ns}simple' => 'simple',
17                 '{http://sabredav.org/ns}hreflist' => new Sabre_DAV_Property_HrefList(array('node1','node3')),
18                 '{DAV:}displayname'     => 'Node 2',
19             )),
20             new Sabre_DAVACL_MockPropertyNode('node3', array(
21                 '{http://sabredav.org/ns}simple' => 'simple',
22                 '{DAV:}displayname'     => 'Node 3',
23             )),
24         );
25
26         $fakeServer = new Sabre_DAV_Server($tree);
27         $fakeServer->debugExceptions = true;
28         $fakeServer->httpResponse = new Sabre_HTTP_ResponseMock();
29         $plugin = new Sabre_DAVACL_Plugin();
30         $plugin->allowAccessToNodesWithoutACL = true;
31
32         $this->assertTrue($plugin instanceof Sabre_DAVACL_Plugin);
33         $fakeServer->addPlugin($plugin);
34         $this->assertEquals($plugin, $fakeServer->getPlugin('acl'));
35
36         return $fakeServer;
37
38     }
39
40     function testSimple() {
41
42         $xml = '<?xml version="1.0"?>
43 <d:expand-property xmlns:d="DAV:">
44   <d:property name="displayname" />
45   <d:property name="foo" namespace="http://www.sabredav.org/NS/2010/nonexistant" />
46   <d:property name="simple" namespace="http://sabredav.org/ns" />
47   <d:property name="href" namespace="http://sabredav.org/ns" />
48 </d:expand-property>';
49
50         $serverVars = array(
51             'REQUEST_METHOD' => 'REPORT',
52             'HTTP_DEPTH'     => '0',
53             'REQUEST_URI'    => '/node1',
54         );
55
56         $request = new Sabre_HTTP_Request($serverVars);
57         $request->setBody($xml);
58
59         $server = $this->getServer();
60         $server->httpRequest = $request;
61
62         $server->exec();
63
64         $this->assertEquals('HTTP/1.1 207 Multi-Status', $server->httpResponse->status,'Incorrect status code received. Full body: ' . $server->httpResponse->body);
65         $this->assertEquals(array(
66             'Content-Type' => 'application/xml; charset=utf-8',
67         ), $server->httpResponse->headers);
68
69
70         $check = array(
71             '/d:multistatus',
72             '/d:multistatus/d:response' => 1,
73             '/d:multistatus/d:response/d:href' => 1,
74             '/d:multistatus/d:response/d:propstat' => 2,
75             '/d:multistatus/d:response/d:propstat/d:prop' => 2,
76             '/d:multistatus/d:response/d:propstat/d:prop/d:displayname' => 1,
77             '/d:multistatus/d:response/d:propstat/d:prop/s:simple' => 1,
78             '/d:multistatus/d:response/d:propstat/d:prop/s:href' => 1,
79             '/d:multistatus/d:response/d:propstat/d:prop/s:href/d:href' => 1,
80         );
81
82         $xml = simplexml_load_string($server->httpResponse->body);
83         $xml->registerXPathNamespace('d','DAV:');
84         $xml->registerXPathNamespace('s','http://sabredav.org/ns');
85         foreach($check as $v1=>$v2) {
86
87             $xpath = is_int($v1)?$v2:$v1;
88
89             $result = $xml->xpath($xpath);
90
91             $count = 1;
92             if (!is_int($v1)) $count = $v2;
93
94             $this->assertEquals($count,count($result), 'we expected ' . $count . ' appearances of ' . $xpath . ' . We found ' . count($result) . '. Full response: ' . $server->httpResponse->body);
95
96         }
97
98     }
99
100     /**
101      * @depends testSimple
102      */
103     function testExpand() {
104
105         $xml = '<?xml version="1.0"?>
106 <d:expand-property xmlns:d="DAV:">
107   <d:property name="href" namespace="http://sabredav.org/ns">
108       <d:property name="displayname" />
109   </d:property>
110 </d:expand-property>';
111
112         $serverVars = array(
113             'REQUEST_METHOD' => 'REPORT',
114             'HTTP_DEPTH'     => '0',
115             'REQUEST_URI'    => '/node1',
116         );
117
118         $request = new Sabre_HTTP_Request($serverVars);
119         $request->setBody($xml);
120
121         $server = $this->getServer();
122         $server->httpRequest = $request;
123
124         $server->exec();
125
126         $this->assertEquals('HTTP/1.1 207 Multi-Status', $server->httpResponse->status, 'Incorrect response status received. Full response body: ' . $server->httpResponse->body);
127         $this->assertEquals(array(
128             'Content-Type' => 'application/xml; charset=utf-8',
129         ), $server->httpResponse->headers);
130
131
132         $check = array(
133             '/d:multistatus',
134             '/d:multistatus/d:response' => 1,
135             '/d:multistatus/d:response/d:href' => 1,
136             '/d:multistatus/d:response/d:propstat' => 1,
137             '/d:multistatus/d:response/d:propstat/d:prop' => 1,
138             '/d:multistatus/d:response/d:propstat/d:prop/s:href' => 1,
139             '/d:multistatus/d:response/d:propstat/d:prop/s:href/d:response' => 1,
140             '/d:multistatus/d:response/d:propstat/d:prop/s:href/d:response/d:href' => 1,
141             '/d:multistatus/d:response/d:propstat/d:prop/s:href/d:response/d:propstat' => 1,
142             '/d:multistatus/d:response/d:propstat/d:prop/s:href/d:response/d:propstat/d:prop' => 1,
143             '/d:multistatus/d:response/d:propstat/d:prop/s:href/d:response/d:propstat/d:prop/d:displayname' => 1,
144         );
145
146         $xml = simplexml_load_string($server->httpResponse->body);
147         $xml->registerXPathNamespace('d','DAV:');
148         $xml->registerXPathNamespace('s','http://sabredav.org/ns');
149         foreach($check as $v1=>$v2) {
150
151             $xpath = is_int($v1)?$v2:$v1;
152
153             $result = $xml->xpath($xpath);
154
155             $count = 1;
156             if (!is_int($v1)) $count = $v2;
157
158             $this->assertEquals($count,count($result), 'we expected ' . $count . ' appearances of ' . $xpath . ' . We found ' . count($result));
159
160         }
161
162     }
163
164     /**
165      * @depends testSimple
166      */
167     function testExpandHrefList() {
168
169         $xml = '<?xml version="1.0"?>
170 <d:expand-property xmlns:d="DAV:">
171   <d:property name="hreflist" namespace="http://sabredav.org/ns">
172       <d:property name="displayname" />
173   </d:property>
174 </d:expand-property>';
175
176         $serverVars = array(
177             'REQUEST_METHOD' => 'REPORT',
178             'HTTP_DEPTH'     => '0',
179             'REQUEST_URI'    => '/node2',
180         );
181
182         $request = new Sabre_HTTP_Request($serverVars);
183         $request->setBody($xml);
184
185         $server = $this->getServer();
186         $server->httpRequest = $request;
187
188         $server->exec();
189
190         $this->assertEquals('HTTP/1.1 207 Multi-Status', $server->httpResponse->status);
191         $this->assertEquals(array(
192             'Content-Type' => 'application/xml; charset=utf-8',
193         ), $server->httpResponse->headers);
194
195
196         $check = array(
197             '/d:multistatus',
198             '/d:multistatus/d:response' => 1,
199             '/d:multistatus/d:response/d:href' => 1,
200             '/d:multistatus/d:response/d:propstat' => 1,
201             '/d:multistatus/d:response/d:propstat/d:prop' => 1,
202             '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist' => 1,
203             '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response' => 2,
204             '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:href' => 2,
205             '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat' => 2,
206             '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat/d:prop' => 2,
207             '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat/d:prop/d:displayname' => 2,
208         );
209
210         $xml = simplexml_load_string($server->httpResponse->body);
211         $xml->registerXPathNamespace('d','DAV:');
212         $xml->registerXPathNamespace('s','http://sabredav.org/ns');
213         foreach($check as $v1=>$v2) {
214
215             $xpath = is_int($v1)?$v2:$v1;
216
217             $result = $xml->xpath($xpath);
218
219             $count = 1;
220             if (!is_int($v1)) $count = $v2;
221
222             $this->assertEquals($count,count($result), 'we expected ' . $count . ' appearances of ' . $xpath . ' . We found ' . count($result));
223
224         }
225
226     }
227
228     /**
229      * @depends testExpand
230      */
231     function testExpandDeep() {
232
233         $xml = '<?xml version="1.0"?>
234 <d:expand-property xmlns:d="DAV:">
235   <d:property name="hreflist" namespace="http://sabredav.org/ns">
236       <d:property name="href" namespace="http://sabredav.org/ns">
237           <d:property name="displayname" />
238       </d:property>
239       <d:property name="displayname" />
240   </d:property>
241 </d:expand-property>';
242
243         $serverVars = array(
244             'REQUEST_METHOD' => 'REPORT',
245             'HTTP_DEPTH'     => '0',
246             'REQUEST_URI'    => '/node2',
247         );
248
249         $request = new Sabre_HTTP_Request($serverVars);
250         $request->setBody($xml);
251
252         $server = $this->getServer();
253         $server->httpRequest = $request;
254
255         $server->exec();
256
257         $this->assertEquals('HTTP/1.1 207 Multi-Status', $server->httpResponse->status);
258         $this->assertEquals(array(
259             'Content-Type' => 'application/xml; charset=utf-8',
260         ), $server->httpResponse->headers);
261
262
263         $check = array(
264             '/d:multistatus',
265             '/d:multistatus/d:response' => 1,
266             '/d:multistatus/d:response/d:href' => 1,
267             '/d:multistatus/d:response/d:propstat' => 1,
268             '/d:multistatus/d:response/d:propstat/d:prop' => 1,
269             '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist' => 1,
270             '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response' => 2,
271             '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:href' => 2,
272             '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat' => 3,
273             '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat/d:prop' => 3,
274             '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat/d:prop/d:displayname' => 2,
275             '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat/d:prop/s:href' => 2,
276             '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat/d:prop/s:href/d:response' => 1,
277             '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat/d:prop/s:href/d:response/d:href' => 1,
278             '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat/d:prop/s:href/d:response/d:propstat' => 1,
279             '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat/d:prop/s:href/d:response/d:propstat/d:prop' => 1,
280             '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat/d:prop/s:href/d:response/d:propstat/d:prop/d:displayname' => 1,
281         );
282
283         $xml = simplexml_load_string($server->httpResponse->body);
284         $xml->registerXPathNamespace('d','DAV:');
285         $xml->registerXPathNamespace('s','http://sabredav.org/ns');
286         foreach($check as $v1=>$v2) {
287
288             $xpath = is_int($v1)?$v2:$v1;
289
290             $result = $xml->xpath($xpath);
291
292             $count = 1;
293             if (!is_int($v1)) $count = $v2;
294
295             $this->assertEquals($count,count($result), 'we expected ' . $count . ' appearances of ' . $xpath . ' . We found ' . count($result));
296
297         }
298
299     }
300 }
301 class Sabre_DAVACL_MockPropertyNode implements Sabre_DAV_INode, Sabre_DAV_IProperties {
302
303     function __construct($name, array $properties) {
304
305         $this->name = $name;
306         $this->properties = $properties;
307
308     }
309
310     function getName() {
311
312         return $this->name;
313
314     }
315
316     function getProperties($requestedProperties) {
317
318         $returnedProperties = array();
319         foreach($requestedProperties as $requestedProperty) {
320             if (isset($this->properties[$requestedProperty])) {
321                 $returnedProperties[$requestedProperty] =
322                     $this->properties[$requestedProperty];
323             }
324         }
325         return $returnedProperties;
326
327     }
328
329     function delete() {
330
331         throw new Sabre_DAV_Exception('Not implemented');
332
333     }
334
335     function setName($name) {
336
337         throw new Sabre_DAV_Exception('Not implemented');
338
339     }
340
341     function getLastModified() {
342
343         return null;
344
345     }
346
347     function updateProperties($properties) {
348
349         throw new Sabre_DAV_Exception('Not implemented');
350
351     }
352
353 }