1<?php
2
3namespace Sabre\DAVACL\Xml\Property;
4
5use Sabre\DAV;
6use Sabre\HTTP;
7use Sabre\DAV\Browser\HtmlOutputHelper;
8
9class ACLTest extends \PHPUnit_Framework_TestCase {
10
11    function testConstruct() {
12
13        $acl = new Acl([]);
14        $this->assertInstanceOf('Sabre\DAVACL\Xml\Property\ACL', $acl);
15
16    }
17
18    function testSerializeEmpty() {
19
20        $acl = new Acl([]);
21        $xml = (new DAV\Server())->xml->write('{DAV:}root', $acl);
22
23        $expected = '<?xml version="1.0"?>
24<d:root xmlns:d="DAV:" xmlns:s="http://sabredav.org/ns" />';
25
26        $this->assertXmlStringEqualsXmlString($expected, $xml);
27
28    }
29
30    function testSerialize() {
31
32        $privileges = [
33            [
34                'principal' => 'principals/evert',
35                'privilege' => '{DAV:}write',
36            ],
37            [
38                'principal' => 'principals/foo',
39                'privilege' => '{DAV:}read',
40                'protected' => true,
41            ],
42        ];
43
44        $acl = new Acl($privileges);
45        $xml = (new DAV\Server())->xml->write('{DAV:}root', $acl, '/');
46
47        $expected = '<?xml version="1.0"?>
48<d:root xmlns:d="DAV:" xmlns:s="http://sabredav.org/ns">
49  <d:ace>
50    <d:principal>
51      <d:href>/principals/evert/</d:href>
52    </d:principal>
53    <d:grant>
54      <d:privilege>
55        <d:write/>
56      </d:privilege>
57    </d:grant>
58  </d:ace>
59  <d:ace>
60    <d:principal>
61      <d:href>/principals/foo/</d:href>
62    </d:principal>
63    <d:grant>
64      <d:privilege>
65        <d:read/>
66      </d:privilege>
67    </d:grant>
68    <d:protected/>
69  </d:ace>
70</d:root>
71';
72        $this->assertXmlStringEqualsXmlString($expected, $xml);
73
74    }
75
76    function testSerializeSpecialPrincipals() {
77
78        $privileges = [
79            [
80                'principal' => '{DAV:}authenticated',
81                'privilege' => '{DAV:}write',
82            ],
83            [
84                'principal' => '{DAV:}unauthenticated',
85                'privilege' => '{DAV:}write',
86            ],
87            [
88                'principal' => '{DAV:}all',
89                'privilege' => '{DAV:}write',
90            ],
91
92        ];
93
94        $acl = new Acl($privileges);
95        $xml = (new DAV\Server())->xml->write('{DAV:}root', $acl, '/');
96
97        $expected = '<?xml version="1.0"?>
98<d:root xmlns:d="DAV:" xmlns:s="http://sabredav.org/ns">
99  <d:ace>
100    <d:principal>
101      <d:authenticated/>
102    </d:principal>
103    <d:grant>
104      <d:privilege>
105        <d:write/>
106      </d:privilege>
107    </d:grant>
108  </d:ace>
109  <d:ace>
110    <d:principal>
111      <d:unauthenticated/>
112    </d:principal>
113    <d:grant>
114      <d:privilege>
115        <d:write/>
116      </d:privilege>
117    </d:grant>
118  </d:ace>
119  <d:ace>
120    <d:principal>
121      <d:all/>
122    </d:principal>
123    <d:grant>
124      <d:privilege>
125        <d:write/>
126      </d:privilege>
127    </d:grant>
128  </d:ace>
129</d:root>
130';
131        $this->assertXmlStringEqualsXmlString($expected, $xml);
132
133    }
134
135    function testUnserialize() {
136
137        $source = '<?xml version="1.0"?>
138<d:root xmlns:d="DAV:">
139  <d:ace>
140    <d:principal>
141      <d:href>/principals/evert/</d:href>
142    </d:principal>
143    <d:grant>
144      <d:privilege>
145        <d:write/>
146      </d:privilege>
147    </d:grant>
148  </d:ace>
149  <d:ace>
150    <d:principal>
151      <d:href>/principals/foo/</d:href>
152    </d:principal>
153    <d:grant>
154      <d:privilege>
155        <d:read/>
156      </d:privilege>
157    </d:grant>
158    <d:protected/>
159  </d:ace>
160</d:root>
161';
162
163        $reader = new \Sabre\Xml\Reader();
164        $reader->elementMap['{DAV:}root'] = 'Sabre\DAVACL\Xml\Property\Acl';
165        $reader->xml($source);
166
167        $result = $reader->parse();
168        $result = $result['value'];
169
170        $this->assertInstanceOf('Sabre\\DAVACL\\Xml\\Property\\Acl', $result);
171
172        $expected = [
173            [
174                'principal' => '/principals/evert/',
175                'protected' => false,
176                'privilege' => '{DAV:}write',
177            ],
178            [
179                'principal' => '/principals/foo/',
180                'protected' => true,
181                'privilege' => '{DAV:}read',
182            ],
183        ];
184
185        $this->assertEquals($expected, $result->getPrivileges());
186
187
188    }
189
190    /**
191     * @expectedException Sabre\DAV\Exception\BadRequest
192     */
193    function testUnserializeNoPrincipal() {
194
195        $source = '<?xml version="1.0"?>
196<d:root xmlns:d="DAV:">
197  <d:ace>
198    <d:grant>
199      <d:privilege>
200        <d:write/>
201      </d:privilege>
202    </d:grant>
203  </d:ace>
204</d:root>
205';
206
207
208        $reader = new \Sabre\Xml\Reader();
209        $reader->elementMap['{DAV:}root'] = 'Sabre\DAVACL\Xml\Property\Acl';
210        $reader->xml($source);
211
212        $result = $reader->parse();
213
214    }
215
216    function testUnserializeOtherPrincipal() {
217
218        $source = '<?xml version="1.0"?>
219<d:root xmlns:d="DAV:">
220  <d:ace>
221    <d:grant>
222      <d:privilege>
223        <d:write/>
224      </d:privilege>
225    </d:grant>
226    <d:principal><d:authenticated /></d:principal>
227  </d:ace>
228  <d:ace>
229    <d:grant>
230      <d:ignoreme />
231      <d:privilege>
232        <d:write/>
233      </d:privilege>
234    </d:grant>
235    <d:principal><d:unauthenticated /></d:principal>
236  </d:ace>
237  <d:ace>
238    <d:grant>
239      <d:privilege>
240        <d:write/>
241      </d:privilege>
242    </d:grant>
243    <d:principal><d:all /></d:principal>
244  </d:ace>
245</d:root>
246';
247
248        $reader = new \Sabre\Xml\Reader();
249        $reader->elementMap['{DAV:}root'] = 'Sabre\DAVACL\Xml\Property\Acl';
250        $reader->xml($source);
251
252        $result = $reader->parse();
253        $result = $result['value'];
254
255        $this->assertInstanceOf('Sabre\\DAVACL\\Xml\\Property\\Acl', $result);
256
257        $expected = [
258            [
259                'principal' => '{DAV:}authenticated',
260                'protected' => false,
261                'privilege' => '{DAV:}write',
262            ],
263            [
264                'principal' => '{DAV:}unauthenticated',
265                'protected' => false,
266                'privilege' => '{DAV:}write',
267            ],
268            [
269                'principal' => '{DAV:}all',
270                'protected' => false,
271                'privilege' => '{DAV:}write',
272            ],
273        ];
274
275        $this->assertEquals($expected, $result->getPrivileges());
276
277    }
278
279    /**
280     * @expectedException Sabre\DAV\Exception\NotImplemented
281     */
282    function testUnserializeDeny() {
283
284        $source = '<?xml version="1.0"?>
285<d:root xmlns:d="DAV:">
286  <d:ignore-me />
287  <d:ace>
288    <d:deny>
289      <d:privilege>
290        <d:write/>
291      </d:privilege>
292    </d:deny>
293    <d:principal><d:href>/principals/evert</d:href></d:principal>
294  </d:ace>
295</d:root>
296';
297
298        $reader = new \Sabre\Xml\Reader();
299        $reader->elementMap['{DAV:}root'] = 'Sabre\DAVACL\Xml\Property\Acl';
300        $reader->xml($source);
301
302        $result = $reader->parse();
303
304    }
305
306    function testToHtml() {
307
308        $privileges = [
309            [
310                'principal' => 'principals/evert',
311                'privilege' => '{DAV:}write',
312            ],
313            [
314                'principal' => 'principals/foo',
315                'privilege' => '{http://example.org/ns}read',
316                'protected' => true,
317            ],
318            [
319                'principal' => '{DAV:}authenticated',
320                'privilege' => '{DAV:}write',
321            ],
322        ];
323
324        $acl = new Acl($privileges);
325        $html = new HtmlOutputHelper(
326            '/base/',
327            ['DAV:' => 'd']
328        );
329
330        $expected =
331            '<table>' .
332            '<tr><th>Principal</th><th>Privilege</th><th></th></tr>' .
333            '<tr><td><a href="/base/principals/evert">/base/principals/evert</a></td><td><span title="{DAV:}write">d:write</span></td><td></td></tr>' .
334            '<tr><td><a href="/base/principals/foo">/base/principals/foo</a></td><td><span title="{http://example.org/ns}read">{http://example.org/ns}read</span></td><td>(protected)</td></tr>' .
335            '<tr><td><span title="{DAV:}authenticated">d:authenticated</span></td><td><span title="{DAV:}write">d:write</span></td><td></td></tr>' .
336            '</table>';
337
338        $this->assertEquals($expected, $acl->toHtml($html));
339
340    }
341
342}
343