1<?php
2
3namespace Sabre\DAVACL;
4
5use Sabre\DAV;
6use Sabre\HTTP;
7
8require_once 'Sabre/HTTP/ResponseMock.php';
9
10class ExpandPropertiesTest extends \PHPUnit_Framework_TestCase {
11
12    function getServer() {
13
14        $tree = array(
15            new DAV\Mock\PropertiesCollection('node1', [], array(
16                '{http://sabredav.org/ns}simple' => 'foo',
17                '{http://sabredav.org/ns}href'   => new DAV\Xml\Property\Href('node2'),
18                '{DAV:}displayname'     => 'Node 1',
19            )),
20            new DAV\Mock\PropertiesCollection('node2', [], array(
21                '{http://sabredav.org/ns}simple' => 'simple',
22                '{http://sabredav.org/ns}hreflist' => new DAV\Xml\Property\Href(['node1','node3']),
23                '{DAV:}displayname'     => 'Node 2',
24            )),
25            new DAV\Mock\PropertiesCollection('node3', [], array(
26                '{http://sabredav.org/ns}simple' => 'simple',
27                '{DAV:}displayname'     => 'Node 3',
28            )),
29        );
30
31        $fakeServer = new DAV\Server($tree);
32        $fakeServer->sapi = new HTTP\SapiMock();
33        $fakeServer->debugExceptions = true;
34        $fakeServer->httpResponse = new HTTP\ResponseMock();
35        $plugin = new Plugin();
36        $plugin->allowAccessToNodesWithoutACL = true;
37
38        $this->assertTrue($plugin instanceof Plugin);
39        $fakeServer->addPlugin($plugin);
40        $this->assertEquals($plugin, $fakeServer->getPlugin('acl'));
41
42        return $fakeServer;
43
44    }
45
46    function testSimple() {
47
48        $xml = '<?xml version="1.0"?>
49<d:expand-property xmlns:d="DAV:">
50  <d:property name="displayname" />
51  <d:property name="foo" namespace="http://www.sabredav.org/NS/2010/nonexistant" />
52  <d:property name="simple" namespace="http://sabredav.org/ns" />
53  <d:property name="href" namespace="http://sabredav.org/ns" />
54</d:expand-property>';
55
56        $serverVars = array(
57            'REQUEST_METHOD' => 'REPORT',
58            'HTTP_DEPTH'     => '0',
59            'REQUEST_URI'    => '/node1',
60        );
61
62        $request = HTTP\Sapi::createFromServerArray($serverVars);
63        $request->setBody($xml);
64
65        $server = $this->getServer();
66        $server->httpRequest = $request;
67
68        $server->exec();
69
70        $this->assertEquals(207, $server->httpResponse->status,'Incorrect status code received. Full body: ' . $server->httpResponse->body);
71        $this->assertEquals(array(
72            'X-Sabre-Version' => [DAV\Version::VERSION],
73            'Content-Type' => ['application/xml; charset=utf-8'],
74        ), $server->httpResponse->getHeaders());
75
76
77        $check = array(
78            '/d:multistatus',
79            '/d:multistatus/d:response' => 1,
80            '/d:multistatus/d:response/d:href' => 1,
81            '/d:multistatus/d:response/d:propstat' => 2,
82            '/d:multistatus/d:response/d:propstat/d:prop' => 2,
83            '/d:multistatus/d:response/d:propstat/d:prop/d:displayname' => 1,
84            '/d:multistatus/d:response/d:propstat/d:prop/s:simple' => 1,
85            '/d:multistatus/d:response/d:propstat/d:prop/s:href' => 1,
86            '/d:multistatus/d:response/d:propstat/d:prop/s:href/d:href' => 1,
87        );
88
89        $xml = simplexml_load_string($server->httpResponse->body);
90        $xml->registerXPathNamespace('d','DAV:');
91        $xml->registerXPathNamespace('s','http://sabredav.org/ns');
92        foreach($check as $v1=>$v2) {
93
94            $xpath = is_int($v1)?$v2:$v1;
95
96            $result = $xml->xpath($xpath);
97
98            $count = 1;
99            if (!is_int($v1)) $count = $v2;
100
101            $this->assertEquals($count,count($result), 'we expected ' . $count . ' appearances of ' . $xpath . ' . We found ' . count($result) . '. Full response: ' . $server->httpResponse->body);
102
103        }
104
105    }
106
107    /**
108     * @depends testSimple
109     */
110    function testExpand() {
111
112        $xml = '<?xml version="1.0"?>
113<d:expand-property xmlns:d="DAV:">
114  <d:property name="href" namespace="http://sabredav.org/ns">
115      <d:property name="displayname" />
116  </d:property>
117</d:expand-property>';
118
119        $serverVars = array(
120            'REQUEST_METHOD' => 'REPORT',
121            'HTTP_DEPTH'     => '0',
122            'REQUEST_URI'    => '/node1',
123        );
124
125        $request = HTTP\Sapi::createFromServerArray($serverVars);
126        $request->setBody($xml);
127
128        $server = $this->getServer();
129        $server->httpRequest = $request;
130
131        $server->exec();
132
133        $this->assertEquals(207, $server->httpResponse->status, 'Incorrect response status received. Full response body: ' . $server->httpResponse->body);
134        $this->assertEquals(array(
135            'X-Sabre-Version' => [DAV\Version::VERSION],
136            'Content-Type' => ['application/xml; charset=utf-8'],
137        ), $server->httpResponse->getHeaders());
138
139
140        $check = array(
141            '/d:multistatus',
142            '/d:multistatus/d:response' => 1,
143            '/d:multistatus/d:response/d:href' => 1,
144            '/d:multistatus/d:response/d:propstat' => 1,
145            '/d:multistatus/d:response/d:propstat/d:prop' => 1,
146            '/d:multistatus/d:response/d:propstat/d:prop/s:href' => 1,
147            '/d:multistatus/d:response/d:propstat/d:prop/s:href/d:response' => 1,
148            '/d:multistatus/d:response/d:propstat/d:prop/s:href/d:response/d:href' => 1,
149            '/d:multistatus/d:response/d:propstat/d:prop/s:href/d:response/d:propstat' => 1,
150            '/d:multistatus/d:response/d:propstat/d:prop/s:href/d:response/d:propstat/d:prop' => 1,
151            '/d:multistatus/d:response/d:propstat/d:prop/s:href/d:response/d:propstat/d:prop/d:displayname' => 1,
152        );
153
154        $xml = simplexml_load_string($server->httpResponse->body);
155        $xml->registerXPathNamespace('d','DAV:');
156        $xml->registerXPathNamespace('s','http://sabredav.org/ns');
157        foreach($check as $v1=>$v2) {
158
159            $xpath = is_int($v1)?$v2:$v1;
160
161            $result = $xml->xpath($xpath);
162
163            $count = 1;
164            if (!is_int($v1)) $count = $v2;
165
166            $this->assertEquals($count,count($result), 'we expected ' . $count . ' appearances of ' . $xpath . ' . We found ' . count($result) . ' Full response body: ' . $server->httpResponse->getBodyAsString());
167
168        }
169
170    }
171
172    /**
173     * @depends testSimple
174     */
175    function testExpandHrefList() {
176
177        $xml = '<?xml version="1.0"?>
178<d:expand-property xmlns:d="DAV:">
179  <d:property name="hreflist" namespace="http://sabredav.org/ns">
180      <d:property name="displayname" />
181  </d:property>
182</d:expand-property>';
183
184        $serverVars = array(
185            'REQUEST_METHOD' => 'REPORT',
186            'HTTP_DEPTH'     => '0',
187            'REQUEST_URI'    => '/node2',
188        );
189
190        $request = HTTP\Sapi::createFromServerArray($serverVars);
191        $request->setBody($xml);
192
193        $server = $this->getServer();
194        $server->httpRequest = $request;
195
196        $server->exec();
197
198        $this->assertEquals(207, $server->httpResponse->status);
199        $this->assertEquals(array(
200            'X-Sabre-Version' => [DAV\Version::VERSION],
201            'Content-Type' => ['application/xml; charset=utf-8'],
202        ), $server->httpResponse->getHeaders());
203
204
205        $check = array(
206            '/d:multistatus',
207            '/d:multistatus/d:response' => 1,
208            '/d:multistatus/d:response/d:href' => 1,
209            '/d:multistatus/d:response/d:propstat' => 1,
210            '/d:multistatus/d:response/d:propstat/d:prop' => 1,
211            '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist' => 1,
212            '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response' => 2,
213            '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:href' => 2,
214            '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat' => 2,
215            '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat/d:prop' => 2,
216            '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat/d:prop/d:displayname' => 2,
217        );
218
219        $xml = simplexml_load_string($server->httpResponse->body);
220        $xml->registerXPathNamespace('d','DAV:');
221        $xml->registerXPathNamespace('s','http://sabredav.org/ns');
222        foreach($check as $v1=>$v2) {
223
224            $xpath = is_int($v1)?$v2:$v1;
225
226            $result = $xml->xpath($xpath);
227
228            $count = 1;
229            if (!is_int($v1)) $count = $v2;
230
231            $this->assertEquals($count,count($result), 'we expected ' . $count . ' appearances of ' . $xpath . ' . We found ' . count($result));
232
233        }
234
235    }
236
237    /**
238     * @depends testExpand
239     */
240    function testExpandDeep() {
241
242        $xml = '<?xml version="1.0"?>
243<d:expand-property xmlns:d="DAV:">
244  <d:property name="hreflist" namespace="http://sabredav.org/ns">
245      <d:property name="href" namespace="http://sabredav.org/ns">
246          <d:property name="displayname" />
247      </d:property>
248      <d:property name="displayname" />
249  </d:property>
250</d:expand-property>';
251
252        $serverVars = array(
253            'REQUEST_METHOD' => 'REPORT',
254            'HTTP_DEPTH'     => '0',
255            'REQUEST_URI'    => '/node2',
256        );
257
258        $request = HTTP\Sapi::createFromServerArray($serverVars);
259        $request->setBody($xml);
260
261        $server = $this->getServer();
262        $server->httpRequest = $request;
263
264        $server->exec();
265
266        $this->assertEquals(207, $server->httpResponse->status);
267        $this->assertEquals(array(
268            'X-Sabre-Version' => [DAV\Version::VERSION],
269            'Content-Type' => ['application/xml; charset=utf-8'],
270        ), $server->httpResponse->getHeaders());
271
272
273        $check = array(
274            '/d:multistatus',
275            '/d:multistatus/d:response' => 1,
276            '/d:multistatus/d:response/d:href' => 1,
277            '/d:multistatus/d:response/d:propstat' => 1,
278            '/d:multistatus/d:response/d:propstat/d:prop' => 1,
279            '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist' => 1,
280            '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response' => 2,
281            '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:href' => 2,
282            '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat' => 3,
283            '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat/d:prop' => 3,
284            '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat/d:prop/d:displayname' => 2,
285            '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat/d:prop/s:href' => 2,
286            '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat/d:prop/s:href/d:response' => 1,
287            '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat/d:prop/s:href/d:response/d:href' => 1,
288            '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat/d:prop/s:href/d:response/d:propstat' => 1,
289            '/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,
290            '/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,
291        );
292
293        $xml = simplexml_load_string($server->httpResponse->body);
294        $xml->registerXPathNamespace('d','DAV:');
295        $xml->registerXPathNamespace('s','http://sabredav.org/ns');
296        foreach($check as $v1=>$v2) {
297
298            $xpath = is_int($v1)?$v2:$v1;
299
300            $result = $xml->xpath($xpath);
301
302            $count = 1;
303            if (!is_int($v1)) $count = $v2;
304
305            $this->assertEquals($count,count($result), 'we expected ' . $count . ' appearances of ' . $xpath . ' . We found ' . count($result));
306
307        }
308
309    }
310}
311