ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
PluginPropertiesTest.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Sabre\DAVACL;
4 
5 use Sabre\DAV;
6 use Sabre\HTTP;
7 
9 
11 
12  $plugin = new Plugin();
13  $plugin->allowUnauthenticatedAccess = false;
14  $plugin->setDefaultACL([
15  [
16  'principal' => '{DAV:}all',
17  'privilege' => '{DAV:}all',
18  ],
19  ]);
20  //Anyone can do anything
21  $plugin->principalCollectionSet = [
22  'principals1',
23  'principals2',
24  ];
25 
26  $requestedProperties = [
27  '{DAV:}principal-collection-set',
28  ];
29 
30  $server = new DAV\Server(new DAV\SimpleCollection('root'));
31  $server->addPlugin($plugin);
32 
33  $result = $server->getPropertiesForPath('', $requestedProperties);
34  $result = $result[0];
35 
36  $this->assertEquals(1, count($result[200]));
37  $this->assertArrayHasKey('{DAV:}principal-collection-set', $result[200]);
38  $this->assertInstanceOf('Sabre\\DAV\\Xml\\Property\\Href', $result[200]['{DAV:}principal-collection-set']);
39 
40  $expected = [
41  'principals1/',
42  'principals2/',
43  ];
44 
45 
46  $this->assertEquals($expected, $result[200]['{DAV:}principal-collection-set']->getHrefs());
47 
48 
49  }
50 
52 
53  $fakeServer = new DAV\Server();
54  $plugin = new DAV\Auth\Plugin(new DAV\Auth\Backend\Mock());
55  $fakeServer->addPlugin($plugin);
56  $plugin = new Plugin();
57  $plugin->setDefaultACL([
58  [
59  'principal' => '{DAV:}all',
60  'privilege' => '{DAV:}all',
61  ],
62  ]);
63  $fakeServer->addPlugin($plugin);
64 
65 
66  $requestedProperties = [
67  '{DAV:}current-user-principal',
68  ];
69 
70  $result = $fakeServer->getPropertiesForPath('', $requestedProperties);
71  $result = $result[0];
72 
73  $this->assertEquals(1, count($result[200]));
74  $this->assertArrayHasKey('{DAV:}current-user-principal', $result[200]);
75  $this->assertInstanceOf('Sabre\DAVACL\Xml\Property\Principal', $result[200]['{DAV:}current-user-principal']);
76  $this->assertEquals(Xml\Property\Principal::UNAUTHENTICATED, $result[200]['{DAV:}current-user-principal']->getType());
77 
78  // This will force the login
79  $fakeServer->emit('beforeMethod', [$fakeServer->httpRequest, $fakeServer->httpResponse]);
80 
81  $result = $fakeServer->getPropertiesForPath('', $requestedProperties);
82  $result = $result[0];
83 
84  $this->assertEquals(1, count($result[200]));
85  $this->assertArrayHasKey('{DAV:}current-user-principal', $result[200]);
86  $this->assertInstanceOf('Sabre\DAVACL\Xml\Property\Principal', $result[200]['{DAV:}current-user-principal']);
87  $this->assertEquals(Xml\Property\Principal::HREF, $result[200]['{DAV:}current-user-principal']->getType());
88  $this->assertEquals('principals/admin/', $result[200]['{DAV:}current-user-principal']->getHref());
89 
90  }
91 
93 
94  $plugin = new Plugin();
95  $plugin->allowUnauthenticatedAccess = false;
96  $plugin->setDefaultACL([
97  [
98  'principal' => '{DAV:}all',
99  'privilege' => '{DAV:}all',
100  ],
101  ]);
102  $server = new DAV\Server();
103  $server->addPlugin($plugin);
104 
105  $requestedProperties = [
106  '{DAV:}supported-privilege-set',
107  ];
108 
109  $result = $server->getPropertiesForPath('', $requestedProperties);
110  $result = $result[0];
111 
112  $this->assertEquals(1, count($result[200]));
113  $this->assertArrayHasKey('{DAV:}supported-privilege-set', $result[200]);
114  $this->assertInstanceOf('Sabre\\DAVACL\\Xml\\Property\\SupportedPrivilegeSet', $result[200]['{DAV:}supported-privilege-set']);
115 
116  $server = new DAV\Server();
117 
118  $prop = $result[200]['{DAV:}supported-privilege-set'];
119  $result = $server->xml->write('{DAV:}root', $prop);
120 
121  $xpaths = [
122  '/d:root' => 1,
123  '/d:root/d:supported-privilege' => 1,
124  '/d:root/d:supported-privilege/d:privilege' => 1,
125  '/d:root/d:supported-privilege/d:privilege/d:all' => 1,
126  '/d:root/d:supported-privilege/d:abstract' => 0,
127  '/d:root/d:supported-privilege/d:supported-privilege' => 2,
128  '/d:root/d:supported-privilege/d:supported-privilege/d:privilege' => 2,
129  '/d:root/d:supported-privilege/d:supported-privilege/d:privilege/d:read' => 1,
130  '/d:root/d:supported-privilege/d:supported-privilege/d:privilege/d:write' => 1,
131  '/d:root/d:supported-privilege/d:supported-privilege/d:supported-privilege' => 7,
132  '/d:root/d:supported-privilege/d:supported-privilege/d:supported-privilege/d:privilege' => 7,
133  '/d:root/d:supported-privilege/d:supported-privilege/d:supported-privilege/d:privilege/d:read-acl' => 1,
134  '/d:root/d:supported-privilege/d:supported-privilege/d:supported-privilege/d:privilege/d:read-current-user-privilege-set' => 1,
135  '/d:root/d:supported-privilege/d:supported-privilege/d:supported-privilege/d:privilege/d:write-content' => 1,
136  '/d:root/d:supported-privilege/d:supported-privilege/d:supported-privilege/d:privilege/d:write-properties' => 1,
137  '/d:root/d:supported-privilege/d:supported-privilege/d:supported-privilege/d:privilege/d:bind' => 1,
138  '/d:root/d:supported-privilege/d:supported-privilege/d:supported-privilege/d:privilege/d:unbind' => 1,
139  '/d:root/d:supported-privilege/d:supported-privilege/d:supported-privilege/d:privilege/d:unlock' => 1,
140  '/d:root/d:supported-privilege/d:supported-privilege/d:supported-privilege/d:abstract' => 0,
141  ];
142 
143 
144  // reloading because php dom sucks
145  $dom2 = new \DOMDocument('1.0', 'utf-8');
146  $dom2->loadXML($result);
147 
148  $dxpath = new \DOMXPath($dom2);
149  $dxpath->registerNamespace('d', 'DAV:');
150  foreach ($xpaths as $xpath => $count) {
151 
152  $this->assertEquals($count, $dxpath->query($xpath)->length, 'Looking for : ' . $xpath . ', we could only find ' . $dxpath->query($xpath)->length . ' elements, while we expected ' . $count . ' Full XML: ' . $result);
153 
154  }
155 
156  }
157 
158  function testACL() {
159 
160  $plugin = new Plugin();
161  $plugin->allowUnauthenticatedAccess = false;
162  $plugin->setDefaultACL([
163  [
164  'principal' => '{DAV:}all',
165  'privilege' => '{DAV:}all',
166  ],
167  ]);
168 
169  $nodes = [
170  new MockACLNode('foo', [
171  [
172  'principal' => 'principals/admin',
173  'privilege' => '{DAV:}read',
174  ]
175  ]),
176  new DAV\SimpleCollection('principals', [
177  $principal = new MockPrincipal('admin', 'principals/admin'),
178  ]),
179 
180  ];
181 
182  $server = new DAV\Server($nodes);
183  $server->addPlugin($plugin);
184  $authPlugin = new DAV\Auth\Plugin(new DAV\Auth\Backend\Mock());
185  $server->addPlugin($authPlugin);
186 
187  // Force login
188  $authPlugin->beforeMethod(new HTTP\Request(), new HTTP\Response());
189 
190  $requestedProperties = [
191  '{DAV:}acl',
192  ];
193 
194  $result = $server->getPropertiesForPath('foo', $requestedProperties);
195  $result = $result[0];
196 
197  $this->assertEquals(1, count($result[200]), 'The {DAV:}acl property did not return from the list. Full list: ' . print_r($result, true));
198  $this->assertArrayHasKey('{DAV:}acl', $result[200]);
199  $this->assertInstanceOf('Sabre\\DAVACL\\Xml\Property\\Acl', $result[200]['{DAV:}acl']);
200 
201  }
202 
203  function testACLRestrictions() {
204 
205  $plugin = new Plugin();
206  $plugin->allowUnauthenticatedAccess = false;
207 
208  $nodes = [
209  new MockACLNode('foo', [
210  [
211  'principal' => 'principals/admin',
212  'privilege' => '{DAV:}read',
213  ]
214  ]),
215  new DAV\SimpleCollection('principals', [
216  $principal = new MockPrincipal('admin', 'principals/admin'),
217  ]),
218 
219  ];
220 
221  $server = new DAV\Server($nodes);
222  $server->addPlugin($plugin);
223  $authPlugin = new DAV\Auth\Plugin(new DAV\Auth\Backend\Mock());
224  $server->addPlugin($authPlugin);
225 
226  // Force login
227  $authPlugin->beforeMethod(new HTTP\Request(), new HTTP\Response());
228 
229  $requestedProperties = [
230  '{DAV:}acl-restrictions',
231  ];
232 
233  $result = $server->getPropertiesForPath('foo', $requestedProperties);
234  $result = $result[0];
235 
236  $this->assertEquals(1, count($result[200]), 'The {DAV:}acl-restrictions property did not return from the list. Full list: ' . print_r($result, true));
237  $this->assertArrayHasKey('{DAV:}acl-restrictions', $result[200]);
238  $this->assertInstanceOf('Sabre\\DAVACL\\Xml\\Property\\AclRestrictions', $result[200]['{DAV:}acl-restrictions']);
239 
240  }
241 
242  function testAlternateUriSet() {
243 
244  $tree = [
245  new DAV\SimpleCollection('principals', [
246  $principal = new MockPrincipal('user', 'principals/user'),
247  ])
248  ];
249 
250  $fakeServer = new DAV\Server($tree);
251  //$plugin = new DAV\Auth\Plugin(new DAV\Auth\MockBackend())
252  //$fakeServer->addPlugin($plugin);
253  $plugin = new Plugin();
254  $plugin->allowUnauthenticatedAccess = false;
255  $plugin->setDefaultACL([
256  [
257  'principal' => '{DAV:}all',
258  'privilege' => '{DAV:}all',
259  ],
260  ]);
261  $fakeServer->addPlugin($plugin);
262 
263  $requestedProperties = [
264  '{DAV:}alternate-URI-set',
265  ];
266  $result = $fakeServer->getPropertiesForPath('principals/user', $requestedProperties);
267  $result = $result[0];
268 
269  $this->assertTrue(isset($result[200]));
270  $this->assertTrue(isset($result[200]['{DAV:}alternate-URI-set']));
271  $this->assertInstanceOf('Sabre\\DAV\\Xml\\Property\\Href', $result[200]['{DAV:}alternate-URI-set']);
272 
273  $this->assertEquals([], $result[200]['{DAV:}alternate-URI-set']->getHrefs());
274 
275  }
276 
277  function testPrincipalURL() {
278 
279  $tree = [
280  new DAV\SimpleCollection('principals', [
281  $principal = new MockPrincipal('user', 'principals/user'),
282  ]),
283  ];
284 
285  $fakeServer = new DAV\Server($tree);
286  //$plugin = new DAV\Auth\Plugin(new DAV\Auth\MockBackend());
287  //$fakeServer->addPlugin($plugin);
288  $plugin = new Plugin();
289  $plugin->allowUnauthenticatedAccess = false;
290  $plugin->setDefaultACL([
291  [
292  'principal' => '{DAV:}all',
293  'privilege' => '{DAV:}all',
294  ],
295  ]);
296  $fakeServer->addPlugin($plugin);
297 
298  $requestedProperties = [
299  '{DAV:}principal-URL',
300  ];
301 
302  $result = $fakeServer->getPropertiesForPath('principals/user', $requestedProperties);
303  $result = $result[0];
304 
305  $this->assertTrue(isset($result[200]));
306  $this->assertTrue(isset($result[200]['{DAV:}principal-URL']));
307  $this->assertInstanceOf('Sabre\\DAV\\Xml\\Property\\Href', $result[200]['{DAV:}principal-URL']);
308 
309  $this->assertEquals('principals/user/', $result[200]['{DAV:}principal-URL']->getHref());
310 
311  }
312 
313  function testGroupMemberSet() {
314 
315  $tree = [
316  new DAV\SimpleCollection('principals', [
317  $principal = new MockPrincipal('user', 'principals/user'),
318  ]),
319  ];
320 
321  $fakeServer = new DAV\Server($tree);
322  //$plugin = new DAV\Auth\Plugin(new DAV\Auth\MockBackend());
323  //$fakeServer->addPlugin($plugin);
324  $plugin = new Plugin();
325  $plugin->allowUnauthenticatedAccess = false;
326  $plugin->setDefaultACL([
327  [
328  'principal' => '{DAV:}all',
329  'privilege' => '{DAV:}all',
330  ],
331  ]);
332  $fakeServer->addPlugin($plugin);
333 
334  $requestedProperties = [
335  '{DAV:}group-member-set',
336  ];
337 
338  $result = $fakeServer->getPropertiesForPath('principals/user', $requestedProperties);
339  $result = $result[0];
340 
341  $this->assertTrue(isset($result[200]));
342  $this->assertTrue(isset($result[200]['{DAV:}group-member-set']));
343  $this->assertInstanceOf('Sabre\\DAV\\Xml\\Property\\Href', $result[200]['{DAV:}group-member-set']);
344 
345  $this->assertEquals([], $result[200]['{DAV:}group-member-set']->getHrefs());
346 
347  }
348 
349  function testGroupMemberShip() {
350 
351  $tree = [
352  new DAV\SimpleCollection('principals', [
353  $principal = new MockPrincipal('user', 'principals/user'),
354  ]),
355  ];
356 
357  $fakeServer = new DAV\Server($tree);
358  $plugin = new Plugin();
359  $plugin->allowUnauthenticatedAccess = false;
360  $fakeServer->addPlugin($plugin);
361  $plugin->setDefaultACL([
362  [
363  'principal' => '{DAV:}all',
364  'privilege' => '{DAV:}all',
365  ],
366  ]);
367 
368  $requestedProperties = [
369  '{DAV:}group-membership',
370  ];
371 
372  $result = $fakeServer->getPropertiesForPath('principals/user', $requestedProperties);
373  $result = $result[0];
374 
375  $this->assertTrue(isset($result[200]));
376  $this->assertTrue(isset($result[200]['{DAV:}group-membership']));
377  $this->assertInstanceOf('Sabre\\DAV\\Xml\\Property\\Href', $result[200]['{DAV:}group-membership']);
378 
379  $this->assertEquals([], $result[200]['{DAV:}group-membership']->getHrefs());
380 
381  }
382 
383  function testGetDisplayName() {
384 
385  $tree = [
386  new DAV\SimpleCollection('principals', [
387  $principal = new MockPrincipal('user', 'principals/user'),
388  ]),
389  ];
390 
391  $fakeServer = new DAV\Server($tree);
392  $plugin = new Plugin();
393  $plugin->allowUnauthenticatedAccess = false;
394  $fakeServer->addPlugin($plugin);
395  $plugin->setDefaultACL([
396  [
397  'principal' => '{DAV:}all',
398  'privilege' => '{DAV:}all',
399  ],
400  ]);
401 
402  $requestedProperties = [
403  '{DAV:}displayname',
404  ];
405 
406  $result = $fakeServer->getPropertiesForPath('principals/user', $requestedProperties);
407  $result = $result[0];
408 
409  $this->assertTrue(isset($result[200]));
410  $this->assertTrue(isset($result[200]['{DAV:}displayname']));
411 
412  $this->assertEquals('user', $result[200]['{DAV:}displayname']);
413 
414  }
415 }
This plugin provides Authentication for a WebDAV server.
Definition: Plugin.php:25
$result
The Request class represents a single HTTP request.
Definition: Request.php:18
$server
Definition: sabredav.php:48
This class represents a single HTTP response.
Definition: Response.php:12
Main DAV server class.
Definition: Server.php:23
$authPlugin
SabreDAV ACL Plugin.
Definition: Plugin.php:31