1<?php
2
3namespace Sabre\DAVACL;
4
5use Sabre\DAV;
6use Sabre\HTTP;
7
8class ACLMethodTest extends \PHPUnit_Framework_TestCase {
9
10    /**
11     * @expectedException Sabre\DAV\Exception\BadRequest
12     */
13    function testCallback() {
14
15        $acl = new Plugin();
16        $server = new DAV\Server();
17        $server->addPlugin($acl);
18
19        $acl->httpAcl($server->httpRequest, $server->httpResponse);
20
21    }
22
23    /**
24
25    /**
26     * @expectedException Sabre\DAV\Exception\MethodNotAllowed
27     */
28    function testNotSupportedByNode() {
29
30        $tree = array(
31            new DAV\SimpleCollection('test'),
32        );
33        $acl = new Plugin();
34        $server = new DAV\Server($tree);
35        $server->httpRequest = new HTTP\Request();
36        $body = '<?xml version="1.0"?>
37<d:acl xmlns:d="DAV:">
38</d:acl>';
39        $server->httpRequest->setBody($body);
40        $server->addPlugin($acl);
41
42        $acl->httpACL($server->httpRequest, $server->httpResponse);
43
44    }
45
46    function testSuccessSimple() {
47
48        $tree = array(
49            new MockACLNode('test',array()),
50        );
51        $acl = new Plugin();
52        $server = new DAV\Server($tree);
53        $server->httpRequest = new HTTP\Request();
54        $server->httpRequest->setUrl('/test');
55
56        $body = '<?xml version="1.0"?>
57<d:acl xmlns:d="DAV:">
58</d:acl>';
59        $server->httpRequest->setBody($body);
60        $server->addPlugin($acl);
61
62        $this->assertFalse($acl->httpACL($server->httpRequest, $server->httpResponse));
63
64    }
65
66    /**
67     * @expectedException Sabre\DAVACL\Exception\NotRecognizedPrincipal
68     */
69    function testUnrecognizedPrincipal() {
70
71        $tree = array(
72            new MockACLNode('test',array()),
73        );
74        $acl = new Plugin();
75        $server = new DAV\Server($tree);
76        $server->httpRequest = new HTTP\Request('ACL','/test');
77        $body = '<?xml version="1.0"?>
78<d:acl xmlns:d="DAV:">
79    <d:ace>
80        <d:grant><d:privilege><d:read /></d:privilege></d:grant>
81        <d:principal><d:href>/principals/notfound</d:href></d:principal>
82    </d:ace>
83</d:acl>';
84        $server->httpRequest->setBody($body);
85        $server->addPlugin($acl);
86
87        $acl->httpACL($server->httpRequest, $server->httpResponse);
88
89    }
90
91    /**
92     * @expectedException Sabre\DAVACL\Exception\NotRecognizedPrincipal
93     */
94    function testUnrecognizedPrincipal2() {
95
96        $tree = array(
97            new MockACLNode('test',array()),
98            new DAV\SimpleCollection('principals',array(
99                new DAV\SimpleCollection('notaprincipal'),
100            )),
101        );
102        $acl = new Plugin();
103        $server = new DAV\Server($tree);
104        $server->httpRequest = new HTTP\Request('ACL','/test');
105        $body = '<?xml version="1.0"?>
106<d:acl xmlns:d="DAV:">
107    <d:ace>
108        <d:grant><d:privilege><d:read /></d:privilege></d:grant>
109        <d:principal><d:href>/principals/notaprincipal</d:href></d:principal>
110    </d:ace>
111</d:acl>';
112        $server->httpRequest->setBody($body);
113        $server->addPlugin($acl);
114
115        $acl->httpACL($server->httpRequest, $server->httpResponse);
116
117    }
118
119    /**
120     * @expectedException Sabre\DAVACL\Exception\NotSupportedPrivilege
121     */
122    function testUnknownPrivilege() {
123
124        $tree = array(
125            new MockACLNode('test',array()),
126        );
127        $acl = new Plugin();
128        $server = new DAV\Server($tree);
129        $server->httpRequest = new HTTP\Request('ACL','/test');
130        $body = '<?xml version="1.0"?>
131<d:acl xmlns:d="DAV:">
132    <d:ace>
133        <d:grant><d:privilege><d:bananas /></d:privilege></d:grant>
134        <d:principal><d:href>/principals/notfound</d:href></d:principal>
135    </d:ace>
136</d:acl>';
137        $server->httpRequest->setBody($body);
138        $server->addPlugin($acl);
139
140        $acl->httpACL($server->httpRequest, $server->httpResponse);
141
142    }
143
144    /**
145     * @expectedException Sabre\DAVACL\Exception\NoAbstract
146     */
147    function testAbstractPrivilege() {
148
149        $tree = array(
150            new MockACLNode('test',array()),
151        );
152        $acl = new Plugin();
153        $server = new DAV\Server($tree);
154        $server->httpRequest = new HTTP\Request('ACL','/test');
155        $body = '<?xml version="1.0"?>
156<d:acl xmlns:d="DAV:">
157    <d:ace>
158        <d:grant><d:privilege><d:all /></d:privilege></d:grant>
159        <d:principal><d:href>/principals/notfound</d:href></d:principal>
160    </d:ace>
161</d:acl>';
162        $server->httpRequest->setBody($body);
163        $server->addPlugin($acl);
164
165        $acl->httpACL($server->httpRequest, $server->httpResponse);
166
167    }
168
169    /**
170     * @expectedException Sabre\DAVACL\Exception\AceConflict
171     */
172    function testUpdateProtectedPrivilege() {
173
174        $oldACL = array(
175            array(
176                'principal' => 'principals/notfound',
177                'privilege' => '{DAV:}write',
178                'protected' => true,
179            ),
180        );
181
182        $tree = array(
183            new MockACLNode('test',$oldACL),
184        );
185        $acl = new Plugin();
186        $server = new DAV\Server($tree);
187        $server->httpRequest = new HTTP\Request('ACL','/test');
188        $body = '<?xml version="1.0"?>
189<d:acl xmlns:d="DAV:">
190    <d:ace>
191        <d:grant><d:privilege><d:read /></d:privilege></d:grant>
192        <d:principal><d:href>/principals/notfound</d:href></d:principal>
193    </d:ace>
194</d:acl>';
195        $server->httpRequest->setBody($body);
196        $server->addPlugin($acl);
197
198        $acl->httpACL($server->httpRequest, $server->httpResponse);
199
200    }
201
202    /**
203     * @expectedException Sabre\DAVACL\Exception\AceConflict
204     */
205    function testUpdateProtectedPrivilege2() {
206
207        $oldACL = array(
208            array(
209                'principal' => 'principals/notfound',
210                'privilege' => '{DAV:}write',
211                'protected' => true,
212            ),
213        );
214
215        $tree = array(
216            new MockACLNode('test',$oldACL),
217        );
218        $acl = new Plugin();
219        $server = new DAV\Server($tree);
220        $server->httpRequest = new HTTP\Request('ACL','/test');
221        $body = '<?xml version="1.0"?>
222<d:acl xmlns:d="DAV:">
223    <d:ace>
224        <d:grant><d:privilege><d:write /></d:privilege></d:grant>
225        <d:principal><d:href>/principals/foo</d:href></d:principal>
226    </d:ace>
227</d:acl>';
228        $server->httpRequest->setBody($body);
229        $server->addPlugin($acl);
230
231        $acl->httpACL($server->httpRequest, $server->httpResponse);
232
233    }
234
235    /**
236     * @expectedException Sabre\DAVACL\Exception\AceConflict
237     */
238    function testUpdateProtectedPrivilege3() {
239
240        $oldACL = array(
241            array(
242                'principal' => 'principals/notfound',
243                'privilege' => '{DAV:}write',
244                'protected' => true,
245            ),
246        );
247
248        $tree = array(
249            new MockACLNode('test',$oldACL),
250        );
251        $acl = new Plugin();
252        $server = new DAV\Server($tree);
253        $server->httpRequest = new HTTP\Request('ACL','/test');
254        $body = '<?xml version="1.0"?>
255<d:acl xmlns:d="DAV:">
256    <d:ace>
257        <d:grant><d:privilege><d:write /></d:privilege></d:grant>
258        <d:principal><d:href>/principals/notfound</d:href></d:principal>
259    </d:ace>
260</d:acl>';
261        $server->httpRequest->setBody($body);
262        $server->addPlugin($acl);
263
264        $acl->httpACL($server->httpRequest, $server->httpResponse);
265
266    }
267
268    function testSuccessComplex() {
269
270        $oldACL = array(
271            array(
272                'principal' => 'principals/foo',
273                'privilege' => '{DAV:}write',
274                'protected' => true,
275            ),
276            array(
277                'principal' => 'principals/bar',
278                'privilege' => '{DAV:}read',
279            ),
280        );
281
282        $tree = array(
283            $node = new MockACLNode('test',$oldACL),
284            new DAV\SimpleCollection('principals', array(
285                new MockPrincipal('foo','principals/foo'),
286                new MockPrincipal('baz','principals/baz'),
287            )),
288        );
289        $acl = new Plugin();
290        $server = new DAV\Server($tree);
291        $server->httpRequest = new HTTP\Request('ACL','/test');
292        $body = '<?xml version="1.0"?>
293<d:acl xmlns:d="DAV:">
294    <d:ace>
295        <d:grant><d:privilege><d:write /></d:privilege></d:grant>
296        <d:principal><d:href>/principals/foo</d:href></d:principal>
297        <d:protected />
298    </d:ace>
299    <d:ace>
300        <d:grant><d:privilege><d:write /></d:privilege></d:grant>
301        <d:principal><d:href>/principals/baz</d:href></d:principal>
302    </d:ace>
303</d:acl>';
304        $server->httpRequest->setBody($body);
305        $server->addPlugin($acl);
306
307
308        $this->assertFalse($acl->httpAcl($server->httpRequest, $server->httpResponse));
309
310        $this->assertEquals(array(
311            array(
312                'principal' => 'principals/foo',
313                'privilege' => '{DAV:}write',
314                'protected' => true,
315            ),
316            array(
317                'principal' => 'principals/baz',
318                'privilege' => '{DAV:}write',
319                'protected' => false,
320            ),
321        ), $node->getACL());
322
323    }
324}
325