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