1<?php
2
3namespace Sabre\DAVACL;
4
5use Sabre\DAV;
6use Sabre\HTTP;
7
8class BlockAccessTest extends \PHPUnit_Framework_TestCase {
9
10    /**
11     * @var DAV\Server
12     */
13    protected $server;
14    protected $plugin;
15
16    function setUp() {
17
18        $nodes = [
19            new DAV\SimpleCollection('testdir'),
20        ];
21
22        $this->server = new DAV\Server($nodes);
23        $this->plugin = new Plugin();
24        $this->plugin->allowAccessToNodesWithoutACL = false;
25        $this->server->addPlugin($this->plugin);
26
27    }
28
29    /**
30     * @expectedException Sabre\DAVACL\Exception\NeedPrivileges
31     */
32    function testGet() {
33
34        $this->server->httpRequest->setMethod('GET');
35        $this->server->httpRequest->setUrl('/testdir');
36
37        $this->server->emit('beforeMethod', [$this->server->httpRequest, $this->server->httpResponse]);
38
39    }
40
41    function testGetDoesntExist() {
42
43        $this->server->httpRequest->setMethod('GET');
44        $this->server->httpRequest->setUrl('/foo');
45
46        $r = $this->server->emit('beforeMethod', [$this->server->httpRequest, $this->server->httpResponse]);
47        $this->assertTrue($r);
48
49    }
50
51    /**
52     * @expectedException Sabre\DAVACL\Exception\NeedPrivileges
53     */
54    function testHEAD() {
55
56        $this->server->httpRequest->setMethod('HEAD');
57        $this->server->httpRequest->setUrl('/testdir');
58
59        $this->server->emit('beforeMethod', [$this->server->httpRequest, $this->server->httpResponse]);
60
61    }
62
63    /**
64     * @expectedException Sabre\DAVACL\Exception\NeedPrivileges
65     */
66    function testOPTIONS() {
67
68        $this->server->httpRequest->setMethod('OPTIONS');
69        $this->server->httpRequest->setUrl('/testdir');
70
71        $this->server->emit('beforeMethod', [$this->server->httpRequest, $this->server->httpResponse]);
72
73    }
74
75    /**
76     * @expectedException Sabre\DAVACL\Exception\NeedPrivileges
77     */
78    function testPUT() {
79
80        $this->server->httpRequest->setMethod('PUT');
81        $this->server->httpRequest->setUrl('/testdir');
82
83        $this->server->emit('beforeMethod', [$this->server->httpRequest, $this->server->httpResponse]);
84
85    }
86
87    /**
88     * @expectedException Sabre\DAVACL\Exception\NeedPrivileges
89     */
90    function testPROPPATCH() {
91
92        $this->server->httpRequest->setMethod('PROPPATCH');
93        $this->server->httpRequest->setUrl('/testdir');
94
95        $this->server->emit('beforeMethod', [$this->server->httpRequest, $this->server->httpResponse]);
96
97    }
98
99    /**
100     * @expectedException Sabre\DAVACL\Exception\NeedPrivileges
101     */
102    function testCOPY() {
103
104        $this->server->httpRequest->setMethod('COPY');
105        $this->server->httpRequest->setUrl('/testdir');
106
107        $this->server->emit('beforeMethod', [$this->server->httpRequest, $this->server->httpResponse]);
108
109    }
110
111    /**
112     * @expectedException Sabre\DAVACL\Exception\NeedPrivileges
113     */
114    function testMOVE() {
115
116        $this->server->httpRequest->setMethod('MOVE');
117        $this->server->httpRequest->setUrl('/testdir');
118
119        $this->server->emit('beforeMethod', [$this->server->httpRequest, $this->server->httpResponse]);
120
121    }
122
123    /**
124     * @expectedException Sabre\DAVACL\Exception\NeedPrivileges
125     */
126    function testACL() {
127
128        $this->server->httpRequest->setMethod('ACL');
129        $this->server->httpRequest->setUrl('/testdir');
130
131        $this->server->emit('beforeMethod', [$this->server->httpRequest, $this->server->httpResponse]);
132
133    }
134
135    /**
136     * @expectedException Sabre\DAVACL\Exception\NeedPrivileges
137     */
138    function testLOCK() {
139
140        $this->server->httpRequest->setMethod('LOCK');
141        $this->server->httpRequest->setUrl('/testdir');
142
143        $this->server->emit('beforeMethod', [$this->server->httpRequest, $this->server->httpResponse]);
144
145    }
146
147    /**
148     * @expectedException Sabre\DAVACL\Exception\NeedPrivileges
149     */
150    function testBeforeBind() {
151
152        $this->server->emit('beforeBind', ['testdir/file']);
153
154    }
155
156    /**
157     * @expectedException Sabre\DAVACL\Exception\NeedPrivileges
158     */
159    function testBeforeUnbind() {
160
161        $this->server->emit('beforeUnbind', ['testdir']);
162
163    }
164
165    function testPropFind() {
166
167        $propFind = new DAV\PropFind('testdir', [
168            '{DAV:}displayname',
169            '{DAV:}getcontentlength',
170            '{DAV:}bar',
171            '{DAV:}owner',
172        ]);
173
174        $r = $this->server->emit('propFind', [$propFind, new DAV\SimpleCollection('testdir')]);
175        $this->assertTrue($r);
176
177        $expected = [
178            200 => [],
179            404 => [],
180            403 => [
181                '{DAV:}displayname' => null,
182                '{DAV:}getcontentlength' => null,
183                '{DAV:}bar' => null,
184                '{DAV:}owner' => null,
185            ],
186        ];
187
188        $this->assertEquals($expected, $propFind->getResultForMultiStatus());
189
190    }
191
192    function testBeforeGetPropertiesNoListing() {
193
194        $this->plugin->hideNodesFromListings = true;
195        $propFind = new DAV\PropFind('testdir', [
196            '{DAV:}displayname',
197            '{DAV:}getcontentlength',
198            '{DAV:}bar',
199            '{DAV:}owner',
200        ]);
201
202        $r = $this->server->emit('propFind', [$propFind, new DAV\SimpleCollection('testdir')]);
203        $this->assertFalse($r);
204
205    }
206}
207