1<?php
2
3namespace Sabre\DAVACL;
4
5use Sabre\DAV;
6use Sabre\HTTP;
7
8class PluginPropertiesTest extends \PHPUnit_Framework_TestCase {
9
10    function testPrincipalCollectionSet() {
11
12        $plugin = new Plugin();
13        $plugin->principalCollectionSet = [
14            'principals1',
15            'principals2',
16        ];
17
18        $requestedProperties = [
19            '{DAV:}principal-collection-set',
20        ];
21
22        $server = new DAV\Server(new DAV\SimpleCollection('root'));
23        $server->addPlugin($plugin);
24
25        $result = $server->getPropertiesForPath('', $requestedProperties);
26        $result = $result[0];
27
28        $this->assertEquals(1,count($result[200]));
29        $this->assertArrayHasKey('{DAV:}principal-collection-set',$result[200]);
30        $this->assertInstanceOf('Sabre\\DAV\\Xml\\Property\\Href', $result[200]['{DAV:}principal-collection-set']);
31
32        $expected = [
33            'principals1/',
34            'principals2/',
35        ];
36
37
38        $this->assertEquals($expected, $result[200]['{DAV:}principal-collection-set']->getHrefs());
39
40
41    }
42
43    function testCurrentUserPrincipal() {
44
45        $fakeServer = new DAV\Server();
46        $plugin = new DAV\Auth\Plugin(new DAV\Auth\Backend\Mock(),'realm');
47        $fakeServer->addPlugin($plugin);
48        $plugin = new Plugin();
49        $fakeServer->addPlugin($plugin);
50
51
52        $requestedProperties = [
53            '{DAV:}current-user-principal',
54        ];
55
56        $result = $fakeServer->getPropertiesForPath('', $requestedProperties);
57        $result = $result[0];
58
59        $this->assertEquals(1,count($result[200]));
60        $this->assertArrayHasKey('{DAV:}current-user-principal',$result[200]);
61        $this->assertInstanceOf('Sabre\DAVACL\Xml\Property\Principal', $result[200]['{DAV:}current-user-principal']);
62        $this->assertEquals(Xml\Property\Principal::UNAUTHENTICATED, $result[200]['{DAV:}current-user-principal']->getType());
63
64        // This will force the login
65        $fakeServer->emit('beforeMethod', [$fakeServer->httpRequest, $fakeServer->httpResponse]);
66
67        $result = $fakeServer->getPropertiesForPath('', $requestedProperties);
68        $result = $result[0];
69
70        $this->assertEquals(1,count($result[200]));
71        $this->assertArrayHasKey('{DAV:}current-user-principal',$result[200]);
72        $this->assertInstanceOf('Sabre\DAVACL\Xml\Property\Principal', $result[200]['{DAV:}current-user-principal']);
73        $this->assertEquals(Xml\Property\Principal::HREF, $result[200]['{DAV:}current-user-principal']->getType());
74        $this->assertEquals('principals/admin/', $result[200]['{DAV:}current-user-principal']->getHref());
75
76    }
77
78    function testSupportedPrivilegeSet() {
79
80        $plugin = new Plugin();
81        $server = new DAV\Server();
82        $server->addPlugin($plugin);
83
84        $requestedProperties = [
85            '{DAV:}supported-privilege-set',
86        ];
87
88        $result = $server->getPropertiesForPath('', $requestedProperties);
89        $result = $result[0];
90
91        $this->assertEquals(1,count($result[200]));
92        $this->assertArrayHasKey('{DAV:}supported-privilege-set',$result[200]);
93        $this->assertInstanceOf('Sabre\\DAVACL\\Xml\\Property\\SupportedPrivilegeSet', $result[200]['{DAV:}supported-privilege-set']);
94
95        $server = new DAV\Server();
96
97        $prop = $result[200]['{DAV:}supported-privilege-set'];
98        $result = $server->xml->write('{DAV:}root', $prop);
99
100        $xpaths = [
101            '/d:root' => 1,
102            '/d:root/d:supported-privilege' => 1,
103            '/d:root/d:supported-privilege/d:privilege' => 1,
104            '/d:root/d:supported-privilege/d:privilege/d:all' => 1,
105            '/d:root/d:supported-privilege/d:abstract' => 1,
106            '/d:root/d:supported-privilege/d:supported-privilege' => 2,
107            '/d:root/d:supported-privilege/d:supported-privilege/d:privilege' => 2,
108            '/d:root/d:supported-privilege/d:supported-privilege/d:privilege/d:read' => 1,
109            '/d:root/d:supported-privilege/d:supported-privilege/d:privilege/d:write' => 1,
110            '/d:root/d:supported-privilege/d:supported-privilege/d:supported-privilege' => 8,
111            '/d:root/d:supported-privilege/d:supported-privilege/d:supported-privilege/d:privilege' => 8,
112            '/d:root/d:supported-privilege/d:supported-privilege/d:supported-privilege/d:privilege/d:read-acl' => 1,
113            '/d:root/d:supported-privilege/d:supported-privilege/d:supported-privilege/d:privilege/d:read-current-user-privilege-set' => 1,
114            '/d:root/d:supported-privilege/d:supported-privilege/d:supported-privilege/d:privilege/d:write-content' => 1,
115            '/d:root/d:supported-privilege/d:supported-privilege/d:supported-privilege/d:privilege/d:write-properties' => 1,
116            '/d:root/d:supported-privilege/d:supported-privilege/d:supported-privilege/d:privilege/d:write-acl' => 1,
117            '/d:root/d:supported-privilege/d:supported-privilege/d:supported-privilege/d:privilege/d:bind' => 1,
118            '/d:root/d:supported-privilege/d:supported-privilege/d:supported-privilege/d:privilege/d:unbind' => 1,
119            '/d:root/d:supported-privilege/d:supported-privilege/d:supported-privilege/d:privilege/d:unlock' => 1,
120            '/d:root/d:supported-privilege/d:supported-privilege/d:supported-privilege/d:abstract' => 0,
121        ];
122
123
124        // reloading because php dom sucks
125        $dom2 = new \DOMDocument('1.0', 'utf-8');
126        $dom2->loadXML($result);
127
128        $dxpath = new \DOMXPath($dom2);
129        $dxpath->registerNamespace('d','DAV:');
130        foreach($xpaths as $xpath=>$count) {
131
132            $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);
133
134        }
135
136    }
137
138    function testACL() {
139
140        $plugin = new Plugin();
141
142        $nodes = [
143            new MockACLNode('foo', [
144                [
145                    'principal' => 'principals/admin',
146                    'privilege' => '{DAV:}read',
147                ]
148            ]),
149            new DAV\SimpleCollection('principals', [
150                $principal = new MockPrincipal('admin','principals/admin'),
151            ]),
152
153        ];
154
155        $server = new DAV\Server($nodes);
156        $server->addPlugin($plugin);
157        $authPlugin = new DAV\Auth\Plugin(new DAV\Auth\Backend\Mock(),'realm');
158        $server->addPlugin($authPlugin);
159
160        // Force login
161        $authPlugin->beforeMethod(new HTTP\Request(), new HTTP\Response());
162
163        $requestedProperties = [
164            '{DAV:}acl',
165        ];
166
167        $result = $server->getPropertiesForPath('foo', $requestedProperties);
168        $result = $result[0];
169
170        $this->assertEquals(1,count($result[200]),'The {DAV:}acl property did not return from the list. Full list: ' . print_r($result, true));
171        $this->assertArrayHasKey('{DAV:}acl',$result[200]);
172        $this->assertInstanceOf('Sabre\\DAVACL\\Xml\Property\\Acl', $result[200]['{DAV:}acl']);
173
174    }
175
176    function testACLRestrictions() {
177
178        $plugin = new Plugin();
179
180        $nodes = [
181            new MockACLNode('foo', [
182                [
183                    'principal' => 'principals/admin',
184                    'privilege' => '{DAV:}read',
185                ]
186            ]),
187            new DAV\SimpleCollection('principals', [
188                $principal = new MockPrincipal('admin','principals/admin'),
189            ]),
190
191        ];
192
193        $server = new DAV\Server($nodes);
194        $server->addPlugin($plugin);
195        $authPlugin = new DAV\Auth\Plugin(new DAV\Auth\Backend\Mock(),'realm');
196        $server->addPlugin($authPlugin);
197
198        // Force login
199        $authPlugin->beforeMethod(new HTTP\Request(), new HTTP\Response());
200
201        $requestedProperties = [
202            '{DAV:}acl-restrictions',
203        ];
204
205        $result = $server->getPropertiesForPath('foo', $requestedProperties);
206        $result = $result[0];
207
208        $this->assertEquals(1,count($result[200]),'The {DAV:}acl-restrictions property did not return from the list. Full list: ' . print_r($result, true));
209        $this->assertArrayHasKey('{DAV:}acl-restrictions',$result[200]);
210        $this->assertInstanceOf('Sabre\\DAVACL\\Xml\\Property\\AclRestrictions', $result[200]['{DAV:}acl-restrictions']);
211
212    }
213
214    function testAlternateUriSet() {
215
216        $tree = [
217            new DAV\SimpleCollection('principals', [
218                $principal = new MockPrincipal('user','principals/user'),
219            ])
220        ];
221
222        $fakeServer = new DAV\Server($tree);
223        //$plugin = new DAV\Auth\Plugin(new DAV\Auth\MockBackend(),'realm');
224        //$fakeServer->addPlugin($plugin);
225        $plugin = new Plugin();
226        $fakeServer->addPlugin($plugin);
227
228        $requestedProperties = [
229            '{DAV:}alternate-URI-set',
230        ];
231        $result = $fakeServer->getPropertiesForPath('principals/user', $requestedProperties);
232        $result = $result[0];
233
234        $this->assertTrue(isset($result[200]));
235        $this->assertTrue(isset($result[200]['{DAV:}alternate-URI-set']));
236        $this->assertInstanceOf('Sabre\\DAV\\Xml\\Property\\Href', $result[200]['{DAV:}alternate-URI-set']);
237
238        $this->assertEquals([], $result[200]['{DAV:}alternate-URI-set']->getHrefs());
239
240    }
241
242    function testPrincipalURL() {
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(),'realm');
252        //$fakeServer->addPlugin($plugin);
253        $plugin = new Plugin();
254        $fakeServer->addPlugin($plugin);
255
256        $requestedProperties = [
257            '{DAV:}principal-URL',
258        ];
259
260        $result = $fakeServer->getPropertiesForPath('principals/user', $requestedProperties);
261        $result = $result[0];
262
263        $this->assertTrue(isset($result[200]));
264        $this->assertTrue(isset($result[200]['{DAV:}principal-URL']));
265        $this->assertInstanceOf('Sabre\\DAV\\Xml\\Property\\Href', $result[200]['{DAV:}principal-URL']);
266
267        $this->assertEquals('principals/user/', $result[200]['{DAV:}principal-URL']->getHref());
268
269    }
270
271    function testGroupMemberSet() {
272
273        $tree = [
274            new DAV\SimpleCollection('principals', [
275                $principal = new MockPrincipal('user','principals/user'),
276            ]),
277        ];
278
279        $fakeServer = new DAV\Server($tree);
280        //$plugin = new DAV\Auth\Plugin(new DAV\Auth\MockBackend(),'realm');
281        //$fakeServer->addPlugin($plugin);
282        $plugin = new Plugin();
283        $fakeServer->addPlugin($plugin);
284
285        $requestedProperties = [
286            '{DAV:}group-member-set',
287        ];
288
289        $result = $fakeServer->getPropertiesForPath('principals/user', $requestedProperties);
290        $result = $result[0];
291
292        $this->assertTrue(isset($result[200]));
293        $this->assertTrue(isset($result[200]['{DAV:}group-member-set']));
294        $this->assertInstanceOf('Sabre\\DAV\\Xml\\Property\\Href', $result[200]['{DAV:}group-member-set']);
295
296        $this->assertEquals([], $result[200]['{DAV:}group-member-set']->getHrefs());
297
298    }
299
300    function testGroupMemberShip() {
301
302        $tree = [
303            new DAV\SimpleCollection('principals', [
304                $principal = new MockPrincipal('user','principals/user'),
305            ]),
306        ];
307
308        $fakeServer = new DAV\Server($tree);
309        $plugin = new Plugin();
310        $fakeServer->addPlugin($plugin);
311
312        $requestedProperties = [
313            '{DAV:}group-membership',
314        ];
315
316        $result = $fakeServer->getPropertiesForPath('principals/user', $requestedProperties);
317        $result = $result[0];
318
319        $this->assertTrue(isset($result[200]));
320        $this->assertTrue(isset($result[200]['{DAV:}group-membership']));
321        $this->assertInstanceOf('Sabre\\DAV\\Xml\\Property\\Href', $result[200]['{DAV:}group-membership']);
322
323        $this->assertEquals([], $result[200]['{DAV:}group-membership']->getHrefs());
324
325    }
326
327    function testGetDisplayName() {
328
329        $tree = [
330            new DAV\SimpleCollection('principals', [
331                $principal = new MockPrincipal('user','principals/user'),
332            ]),
333        ];
334
335        $fakeServer = new DAV\Server($tree);
336        $plugin = new Plugin();
337        $fakeServer->addPlugin($plugin);
338
339        $requestedProperties = [
340            '{DAV:}displayname',
341        ];
342
343        $result = $fakeServer->getPropertiesForPath('principals/user', $requestedProperties);
344        $result = $result[0];
345
346        $this->assertTrue(isset($result[200]));
347        $this->assertTrue(isset($result[200]['{DAV:}displayname']));
348
349        $this->assertEquals('user', $result[200]['{DAV:}displayname']);
350
351    }
352}
353