]> git.mxchange.org Git - friendica-addons.git/blob - dav/SabreDAV/tests/Sabre/DAVACL/PrincipalPropertySearchTest.php
Merge branch 'master' of git://github.com/friendica/friendica-addons
[friendica-addons.git] / dav / SabreDAV / tests / Sabre / DAVACL / PrincipalPropertySearchTest.php
1 <?php
2
3 require_once 'Sabre/HTTP/ResponseMock.php';
4 require_once 'Sabre/DAV/Auth/MockBackend.php';
5 require_once 'Sabre/DAVACL/MockPrincipalBackend.php';
6
7 class Sabre_DAVACL_PrincipalPropertySearchTest extends PHPUnit_Framework_TestCase {
8
9     function getServer() {
10
11         $backend = new Sabre_DAVACL_MockPrincipalBackend();
12
13         $dir = new Sabre_DAV_SimpleCollection('root');
14         $principals = new Sabre_DAVACL_PrincipalCollection($backend);
15         $dir->addChild($principals);
16
17         $fakeServer = new Sabre_DAV_Server(new Sabre_DAV_ObjectTree($dir));
18         $fakeServer->httpResponse = new Sabre_HTTP_ResponseMock();
19         $fakeServer->debugExceptions = true;
20         $plugin = new Sabre_DAVACL_MockPlugin($backend,'realm');
21         $plugin->allowAccessToNodesWithoutACL = true;
22
23         $this->assertTrue($plugin instanceof Sabre_DAVACL_Plugin);
24         $fakeServer->addPlugin($plugin);
25         $this->assertEquals($plugin, $fakeServer->getPlugin('acl'));
26
27         return $fakeServer;
28
29     }
30
31     function testDepth1() {
32
33         $xml = '<?xml version="1.0"?>
34 <d:principal-property-search xmlns:d="DAV:">
35   <d:property-search>
36      <d:prop>
37        <d:displayname />
38      </d:prop>
39      <d:match>user</d:match>
40   </d:property-search>
41   <d:prop>
42     <d:displayname />
43     <d:getcontentlength />
44   </d:prop>
45 </d:principal-property-search>';
46
47         $serverVars = array(
48             'REQUEST_METHOD' => 'REPORT',
49             'HTTP_DEPTH'     => '1',
50             'REQUEST_URI'    => '/principals',
51         );
52
53         $request = new Sabre_HTTP_Request($serverVars);
54         $request->setBody($xml);
55
56         $server = $this->getServer();
57         $server->httpRequest = $request;
58
59         $server->exec();
60
61         $this->assertEquals('HTTP/1.1 400 Bad request', $server->httpResponse->status);
62         $this->assertEquals(array(
63             'Content-Type' => 'application/xml; charset=utf-8',
64         ), $server->httpResponse->headers);
65
66     }
67
68
69     function testUnknownSearchField() {
70
71         $xml = '<?xml version="1.0"?>
72 <d:principal-property-search xmlns:d="DAV:">
73   <d:property-search>
74      <d:prop>
75        <d:yourmom />
76      </d:prop>
77      <d:match>user</d:match>
78   </d:property-search>
79   <d:prop>
80     <d:displayname />
81     <d:getcontentlength />
82   </d:prop>
83 </d:principal-property-search>';
84
85         $serverVars = array(
86             'REQUEST_METHOD' => 'REPORT',
87             'HTTP_DEPTH'     => '0',
88             'REQUEST_URI'    => '/principals',
89         );
90
91         $request = new Sabre_HTTP_Request($serverVars);
92         $request->setBody($xml);
93
94         $server = $this->getServer();
95         $server->httpRequest = $request;
96
97         $server->exec();
98
99         $this->assertEquals('HTTP/1.1 207 Multi-Status', $server->httpResponse->status);
100         $this->assertEquals(array(
101             'Content-Type' => 'application/xml; charset=utf-8',
102         ), $server->httpResponse->headers);
103
104     }
105
106     function testCorrect() {
107
108         $xml = '<?xml version="1.0"?>
109 <d:principal-property-search xmlns:d="DAV:">
110   <d:apply-to-principal-collection-set />
111   <d:property-search>
112      <d:prop>
113        <d:displayname />
114      </d:prop>
115      <d:match>user</d:match>
116   </d:property-search>
117   <d:prop>
118     <d:displayname />
119     <d:getcontentlength />
120   </d:prop>
121 </d:principal-property-search>';
122
123         $serverVars = array(
124             'REQUEST_METHOD' => 'REPORT',
125             'HTTP_DEPTH'     => '0',
126             'REQUEST_URI'    => '/',
127         );
128
129         $request = new Sabre_HTTP_Request($serverVars);
130         $request->setBody($xml);
131
132         $server = $this->getServer();
133         $server->httpRequest = $request;
134
135         $server->exec();
136
137         $this->assertEquals('HTTP/1.1 207 Multi-Status', $server->httpResponse->status, $server->httpResponse->body);
138         $this->assertEquals(array(
139             'Content-Type' => 'application/xml; charset=utf-8',
140         ), $server->httpResponse->headers);
141
142
143         $check = array(
144             '/d:multistatus',
145             '/d:multistatus/d:response' => 2,
146             '/d:multistatus/d:response/d:href' => 2,
147             '/d:multistatus/d:response/d:propstat' => 4,
148             '/d:multistatus/d:response/d:propstat/d:prop' => 4,
149             '/d:multistatus/d:response/d:propstat/d:prop/d:displayname' => 2,
150             '/d:multistatus/d:response/d:propstat/d:prop/d:getcontentlength' => 2,
151             '/d:multistatus/d:response/d:propstat/d:status' => 4,
152         );
153
154         $xml = simplexml_load_string($server->httpResponse->body);
155         $xml->registerXPathNamespace('d','DAV:');
156         foreach($check as $v1=>$v2) {
157
158             $xpath = is_int($v1)?$v2:$v1;
159
160             $result = $xml->xpath($xpath);
161
162             $count = 1;
163             if (!is_int($v1)) $count = $v2;
164
165             $this->assertEquals($count,count($result), 'we expected ' . $count . ' appearances of ' . $xpath . ' . We found ' . count($result) . '. Full response body: ' . $server->httpResponse->body);
166
167         }
168
169     }
170     function testWrongUri() {
171
172         $xml = '<?xml version="1.0"?>
173 <d:principal-property-search xmlns:d="DAV:">
174   <d:property-search>
175      <d:prop>
176        <d:displayname />
177      </d:prop>
178      <d:match>user</d:match>
179   </d:property-search>
180   <d:prop>
181     <d:displayname />
182     <d:getcontentlength />
183   </d:prop>
184 </d:principal-property-search>';
185
186         $serverVars = array(
187             'REQUEST_METHOD' => 'REPORT',
188             'HTTP_DEPTH'     => '0',
189             'REQUEST_URI'    => '/',
190         );
191
192         $request = new Sabre_HTTP_Request($serverVars);
193         $request->setBody($xml);
194
195         $server = $this->getServer();
196         $server->httpRequest = $request;
197
198         $server->exec();
199
200         $this->assertEquals('HTTP/1.1 207 Multi-Status', $server->httpResponse->status, $server->httpResponse->body);
201         $this->assertEquals(array(
202             'Content-Type' => 'application/xml; charset=utf-8',
203         ), $server->httpResponse->headers);
204
205
206         $check = array(
207             '/d:multistatus',
208             '/d:multistatus/d:response' => 0,
209         );
210
211         $xml = simplexml_load_string($server->httpResponse->body);
212         $xml->registerXPathNamespace('d','DAV:');
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) . '. Full response body: ' . $server->httpResponse->body);
223
224         }
225
226     }
227 }
228
229 class Sabre_DAVACL_MockPlugin extends Sabre_DAVACL_Plugin {
230
231     function getCurrentUserPrivilegeSet($node) {
232
233         return array(
234             '{DAV:}read',
235             '{DAV:}write',
236         );
237
238     }
239
240 }