1<?php
2
3namespace Sabre\DAVACL;
4
5use Sabre\DAV;
6use Sabre\HTTP;
7
8require_once 'Sabre/HTTP/ResponseMock.php';
9
10class PrincipalPropertySearchTest extends \PHPUnit_Framework_TestCase {
11
12    function getServer() {
13
14        $backend = new PrincipalBackend\Mock();
15
16        $dir = new DAV\SimpleCollection('root');
17        $principals = new PrincipalCollection($backend);
18        $dir->addChild($principals);
19
20        $fakeServer = new DAV\Server($dir);
21        $fakeServer->sapi = new HTTP\SapiMock();
22        $fakeServer->httpResponse = new HTTP\ResponseMock();
23        $fakeServer->debugExceptions = true;
24        $plugin = new MockPlugin($backend,'realm');
25        $plugin->allowAccessToNodesWithoutACL = true;
26
27        $this->assertTrue($plugin instanceof Plugin);
28        $fakeServer->addPlugin($plugin);
29        $this->assertEquals($plugin, $fakeServer->getPlugin('acl'));
30
31        return $fakeServer;
32
33    }
34
35    function testDepth1() {
36
37        $xml = '<?xml version="1.0"?>
38<d:principal-property-search xmlns:d="DAV:">
39  <d:property-search>
40     <d:prop>
41       <d:displayname />
42     </d:prop>
43     <d:match>user</d:match>
44  </d:property-search>
45  <d:prop>
46    <d:displayname />
47    <d:getcontentlength />
48  </d:prop>
49</d:principal-property-search>';
50
51        $serverVars = array(
52            'REQUEST_METHOD' => 'REPORT',
53            'HTTP_DEPTH'     => '1',
54            'REQUEST_URI'    => '/principals',
55        );
56
57        $request = HTTP\Sapi::createFromServerArray($serverVars);
58        $request->setBody($xml);
59
60        $server = $this->getServer();
61        $server->httpRequest = $request;
62
63        $server->exec();
64
65        $this->assertEquals(400, $server->httpResponse->getStatus(), $server->httpResponse->getBodyAsString());
66        $this->assertEquals(array(
67            'X-Sabre-Version' => [DAV\Version::VERSION],
68            'Content-Type' => ['application/xml; charset=utf-8'],
69        ), $server->httpResponse->getHeaders());
70
71    }
72
73
74    function testUnknownSearchField() {
75
76        $xml = '<?xml version="1.0"?>
77<d:principal-property-search xmlns:d="DAV:">
78  <d:property-search>
79     <d:prop>
80       <d:yourmom />
81     </d:prop>
82     <d:match>user</d:match>
83  </d:property-search>
84  <d:prop>
85    <d:displayname />
86    <d:getcontentlength />
87  </d:prop>
88</d:principal-property-search>';
89
90        $serverVars = array(
91            'REQUEST_METHOD' => 'REPORT',
92            'HTTP_DEPTH'     => '0',
93            'REQUEST_URI'    => '/principals',
94        );
95
96        $request = HTTP\Sapi::createFromServerArray($serverVars);
97        $request->setBody($xml);
98
99        $server = $this->getServer();
100        $server->httpRequest = $request;
101
102        $server->exec();
103
104        $this->assertEquals(207, $server->httpResponse->getStatus(), "Full body: " . $server->httpResponse->getBodyAsString());
105        $this->assertEquals(array(
106            'X-Sabre-Version' => [DAV\Version::VERSION],
107            'Content-Type' => ['application/xml; charset=utf-8'],
108            'Vary'         => ['Brief,Prefer'],
109        ), $server->httpResponse->getHeaders());
110
111    }
112
113    function testCorrect() {
114
115        $xml = '<?xml version="1.0"?>
116<d:principal-property-search xmlns:d="DAV:">
117  <d:apply-to-principal-collection-set />
118  <d:property-search>
119     <d:prop>
120       <d:displayname />
121     </d:prop>
122     <d:match>user</d:match>
123  </d:property-search>
124  <d:prop>
125    <d:displayname />
126    <d:getcontentlength />
127  </d:prop>
128</d:principal-property-search>';
129
130        $serverVars = array(
131            'REQUEST_METHOD' => 'REPORT',
132            'HTTP_DEPTH'     => '0',
133            'REQUEST_URI'    => '/',
134        );
135
136        $request = HTTP\Sapi::createFromServerArray($serverVars);
137        $request->setBody($xml);
138
139        $server = $this->getServer();
140        $server->httpRequest = $request;
141
142        $server->exec();
143
144        $this->assertEquals(207, $server->httpResponse->status, $server->httpResponse->body);
145        $this->assertEquals(array(
146            'X-Sabre-Version' => [DAV\Version::VERSION],
147            'Content-Type' => ['application/xml; charset=utf-8'],
148            'Vary'         => ['Brief,Prefer'],
149        ), $server->httpResponse->getHeaders());
150
151
152        $check = array(
153            '/d:multistatus',
154            '/d:multistatus/d:response' => 2,
155            '/d:multistatus/d:response/d:href' => 2,
156            '/d:multistatus/d:response/d:propstat' => 4,
157            '/d:multistatus/d:response/d:propstat/d:prop' => 4,
158            '/d:multistatus/d:response/d:propstat/d:prop/d:displayname' => 2,
159            '/d:multistatus/d:response/d:propstat/d:prop/d:getcontentlength' => 2,
160            '/d:multistatus/d:response/d:propstat/d:status' => 4,
161        );
162
163        $xml = simplexml_load_string($server->httpResponse->body);
164        $xml->registerXPathNamespace('d','DAV:');
165        foreach($check as $v1=>$v2) {
166
167            $xpath = is_int($v1)?$v2:$v1;
168
169            $result = $xml->xpath($xpath);
170
171            $count = 1;
172            if (!is_int($v1)) $count = $v2;
173
174            $this->assertEquals($count,count($result), 'we expected ' . $count . ' appearances of ' . $xpath . ' . We found ' . count($result) . '. Full response body: ' . $server->httpResponse->body);
175
176        }
177
178    }
179
180    function testAND() {
181
182        $xml = '<?xml version="1.0"?>
183<d:principal-property-search xmlns:d="DAV:">
184  <d:apply-to-principal-collection-set />
185  <d:property-search>
186     <d:prop>
187       <d:displayname />
188     </d:prop>
189     <d:match>user</d:match>
190  </d:property-search>
191  <d:property-search>
192     <d:prop>
193       <d:foo />
194     </d:prop>
195     <d:match>bar</d:match>
196  </d:property-search>
197  <d:prop>
198    <d:displayname />
199    <d:getcontentlength />
200  </d:prop>
201</d:principal-property-search>';
202
203        $serverVars = array(
204            'REQUEST_METHOD' => 'REPORT',
205            'HTTP_DEPTH'     => '0',
206            'REQUEST_URI'    => '/',
207        );
208
209        $request = HTTP\Sapi::createFromServerArray($serverVars);
210        $request->setBody($xml);
211
212        $server = $this->getServer();
213        $server->httpRequest = $request;
214
215        $server->exec();
216
217        $this->assertEquals(207, $server->httpResponse->status, $server->httpResponse->body);
218        $this->assertEquals(array(
219            'X-Sabre-Version' => [DAV\Version::VERSION],
220            'Content-Type' => ['application/xml; charset=utf-8'],
221            'Vary'         => ['Brief,Prefer'],
222        ), $server->httpResponse->getHeaders());
223
224
225        $check = array(
226            '/d:multistatus',
227            '/d:multistatus/d:response' => 0,
228            '/d:multistatus/d:response/d:href' => 0,
229            '/d:multistatus/d:response/d:propstat' => 0,
230            '/d:multistatus/d:response/d:propstat/d:prop' => 0,
231            '/d:multistatus/d:response/d:propstat/d:prop/d:displayname' => 0,
232            '/d:multistatus/d:response/d:propstat/d:prop/d:getcontentlength' => 0,
233            '/d:multistatus/d:response/d:propstat/d:status' => 0,
234        );
235
236        $xml = simplexml_load_string($server->httpResponse->body);
237        $xml->registerXPathNamespace('d','DAV:');
238        foreach($check as $v1=>$v2) {
239
240            $xpath = is_int($v1)?$v2:$v1;
241
242            $result = $xml->xpath($xpath);
243
244            $count = 1;
245            if (!is_int($v1)) $count = $v2;
246
247            $this->assertEquals($count,count($result), 'we expected ' . $count . ' appearances of ' . $xpath . ' . We found ' . count($result) . '. Full response body: ' . $server->httpResponse->body);
248
249        }
250
251    }
252    function testOR() {
253
254        $xml = '<?xml version="1.0"?>
255<d:principal-property-search xmlns:d="DAV:" test="anyof">
256  <d:apply-to-principal-collection-set />
257  <d:property-search>
258     <d:prop>
259       <d:displayname />
260     </d:prop>
261     <d:match>user</d:match>
262  </d:property-search>
263  <d:property-search>
264     <d:prop>
265       <d:foo />
266     </d:prop>
267     <d:match>bar</d:match>
268  </d:property-search>
269  <d:prop>
270    <d:displayname />
271    <d:getcontentlength />
272  </d:prop>
273</d:principal-property-search>';
274
275        $serverVars = array(
276            'REQUEST_METHOD' => 'REPORT',
277            'HTTP_DEPTH'     => '0',
278            'REQUEST_URI'    => '/',
279        );
280
281        $request = HTTP\Sapi::createFromServerArray($serverVars);
282        $request->setBody($xml);
283
284        $server = $this->getServer();
285        $server->httpRequest = $request;
286
287        $server->exec();
288
289        $this->assertEquals(207, $server->httpResponse->status, $server->httpResponse->body);
290        $this->assertEquals(array(
291            'X-Sabre-Version' => [DAV\Version::VERSION],
292            'Content-Type' => ['application/xml; charset=utf-8'],
293            'Vary'         => ['Brief,Prefer'],
294        ), $server->httpResponse->getHeaders());
295
296
297        $check = array(
298            '/d:multistatus',
299            '/d:multistatus/d:response' => 2,
300            '/d:multistatus/d:response/d:href' => 2,
301            '/d:multistatus/d:response/d:propstat' => 4,
302            '/d:multistatus/d:response/d:propstat/d:prop' => 4,
303            '/d:multistatus/d:response/d:propstat/d:prop/d:displayname' => 2,
304            '/d:multistatus/d:response/d:propstat/d:prop/d:getcontentlength' => 2,
305            '/d:multistatus/d:response/d:propstat/d:status' => 4,
306        );
307
308        $xml = simplexml_load_string($server->httpResponse->body);
309        $xml->registerXPathNamespace('d','DAV:');
310        foreach($check as $v1=>$v2) {
311
312            $xpath = is_int($v1)?$v2:$v1;
313
314            $result = $xml->xpath($xpath);
315
316            $count = 1;
317            if (!is_int($v1)) $count = $v2;
318
319            $this->assertEquals($count,count($result), 'we expected ' . $count . ' appearances of ' . $xpath . ' . We found ' . count($result) . '. Full response body: ' . $server->httpResponse->body);
320
321        }
322
323    }
324    function testWrongUri() {
325
326        $xml = '<?xml version="1.0"?>
327<d:principal-property-search xmlns:d="DAV:">
328  <d:property-search>
329     <d:prop>
330       <d:displayname />
331     </d:prop>
332     <d:match>user</d:match>
333  </d:property-search>
334  <d:prop>
335    <d:displayname />
336    <d:getcontentlength />
337  </d:prop>
338</d:principal-property-search>';
339
340        $serverVars = array(
341            'REQUEST_METHOD' => 'REPORT',
342            'HTTP_DEPTH'     => '0',
343            'REQUEST_URI'    => '/',
344        );
345
346        $request = HTTP\Sapi::createFromServerArray($serverVars);
347        $request->setBody($xml);
348
349        $server = $this->getServer();
350        $server->httpRequest = $request;
351
352        $server->exec();
353
354        $this->assertEquals(207, $server->httpResponse->status, $server->httpResponse->body);
355        $this->assertEquals(array(
356            'X-Sabre-Version' => [DAV\Version::VERSION],
357            'Content-Type' => ['application/xml; charset=utf-8'],
358            'Vary'         => ['Brief,Prefer'],
359        ), $server->httpResponse->getHeaders());
360
361
362        $check = array(
363            '/d:multistatus',
364            '/d:multistatus/d:response' => 0,
365        );
366
367        $xml = simplexml_load_string($server->httpResponse->body);
368        $xml->registerXPathNamespace('d','DAV:');
369        foreach($check as $v1=>$v2) {
370
371            $xpath = is_int($v1)?$v2:$v1;
372
373            $result = $xml->xpath($xpath);
374
375            $count = 1;
376            if (!is_int($v1)) $count = $v2;
377
378            $this->assertEquals($count,count($result), 'we expected ' . $count . ' appearances of ' . $xpath . ' . We found ' . count($result) . '. Full response body: ' . $server->httpResponse->body);
379
380        }
381
382    }
383}
384
385class MockPlugin extends Plugin {
386
387    function getCurrentUserPrivilegeSet($node) {
388
389        return array(
390            '{DAV:}read',
391            '{DAV:}write',
392        );
393
394    }
395
396}
397