3 require_once 'Sabre/DAV/Auth/MockBackend.php';
4 require_once 'Sabre/DAVACL/MockPrincipal.php';
6 class Sabre_DAVACL_PluginPropertiesTest extends PHPUnit_Framework_TestCase {
8 function testPrincipalCollectionSet() {
10 $plugin = new Sabre_DAVACL_Plugin();
11 $plugin->principalCollectionSet = array(
16 $requestedProperties = array(
17 '{DAV:}principal-collection-set',
20 $returnedProperties = array(
25 $server = new Sabre_DAV_Server();
26 $server->addPlugin($plugin);
28 $this->assertNull($plugin->beforeGetProperties('', new Sabre_DAV_SimpleCollection('root'), $requestedProperties, $returnedProperties));
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']);
40 $this->assertEquals($expected, $returnedProperties[200]['{DAV:}principal-collection-set']->getHrefs());
45 function testCurrentUserPrincipal() {
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);
54 $requestedProperties = array(
55 '{DAV:}current-user-principal',
58 $returnedProperties = array(
63 $this->assertNull($plugin->beforeGetProperties('', new Sabre_DAV_SimpleCollection('root'), $requestedProperties, $returnedProperties));
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());
70 // This will force the login
71 $fakeServer->broadCastEvent('beforeMethod',array('GET',''));
74 $requestedProperties = array(
75 '{DAV:}current-user-principal',
78 $returnedProperties = array(
84 $this->assertNull($plugin->beforeGetProperties('', new Sabre_DAV_SimpleCollection('root'), $requestedProperties, $returnedProperties));
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());
95 function testSupportedPrivilegeSet() {
97 $plugin = new Sabre_DAVACL_Plugin();
98 $server = new Sabre_DAV_Server();
99 $server->addPlugin($plugin);
101 $requestedProperties = array(
102 '{DAV:}supported-privilege-set',
105 $returnedProperties = array(
111 $this->assertNull($plugin->beforeGetProperties('', new Sabre_DAV_SimpleCollection('root'), $requestedProperties, $returnedProperties));
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']);
117 $server = new Sabre_DAV_Server();
118 $prop = $returnedProperties[200]['{DAV:}supported-privilege-set'];
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);
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,
151 // reloading because php dom sucks
152 $dom2 = new DOMDocument('1.0', 'utf-8');
153 $dom2->loadXML($dom->saveXML());
155 $dxpath = new DOMXPath($dom2);
156 $dxpath->registerNamespace('d','DAV:');
157 foreach($xpaths as $xpath=>$count) {
159 $this->assertEquals($count, $dxpath->query($xpath)->length, 'Looking for : ' . $xpath . ', we could only find ' . $dxpath->query($xpath)->length . ' elements, while we expected ' . $count);
167 $plugin = new Sabre_DAVACL_Plugin();
170 new Sabre_DAVACL_MockACLNode('foo', array(
172 'principal' => 'principals/admin',
173 'privilege' => '{DAV:}read',
176 new Sabre_DAV_SimpleCollection('principals', array(
177 $principal = new Sabre_DAVACL_MockPrincipal('admin','principals/admin'),
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);
188 $authPlugin->beforeMethod('BLA','foo');
190 $requestedProperties = array(
194 $returnedProperties = array(
200 $this->assertNull($plugin->beforeGetProperties('foo', $nodes[0], $requestedProperties, $returnedProperties));
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']);
208 function testACLRestrictions() {
210 $plugin = new Sabre_DAVACL_Plugin();
213 new Sabre_DAVACL_MockACLNode('foo', array(
215 'principal' => 'principals/admin',
216 'privilege' => '{DAV:}read',
219 new Sabre_DAV_SimpleCollection('principals', array(
220 $principal = new Sabre_DAVACL_MockPrincipal('admin','principals/admin'),
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);
231 $authPlugin->beforeMethod('BLA','foo');
233 $requestedProperties = array(
234 '{DAV:}acl-restrictions',
237 $returnedProperties = array(
243 $this->assertNull($plugin->beforeGetProperties('foo', $nodes[0], $requestedProperties, $returnedProperties));
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']);
251 function testAlternateUriSet() {
254 new Sabre_DAV_SimpleCollection('principals', array(
255 $principal = new Sabre_DAVACL_MockPrincipal('user','principals/user'),
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);
265 $requestedProperties = array(
266 '{DAV:}alternate-URI-set',
268 $returnedProperties = array();
270 $result = $plugin->beforeGetProperties('principals/user',$principal,$requestedProperties,$returnedProperties);
272 $this->assertNull($result);
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']);
278 $this->assertEquals(array(), $returnedProperties[200]['{DAV:}alternate-URI-set']->getHrefs());
282 function testPrincipalURL() {
285 new Sabre_DAV_SimpleCollection('principals', array(
286 $principal = new Sabre_DAVACL_MockPrincipal('user','principals/user'),
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);
296 $requestedProperties = array(
297 '{DAV:}principal-URL',
299 $returnedProperties = array();
301 $result = $plugin->beforeGetProperties('principals/user',$principal,$requestedProperties,$returnedProperties);
303 $this->assertNull($result);
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']);
309 $this->assertEquals('principals/user/', $returnedProperties[200]['{DAV:}principal-URL']->getHref());
313 function testGroupMemberSet() {
316 new Sabre_DAV_SimpleCollection('principals', array(
317 $principal = new Sabre_DAVACL_MockPrincipal('user','principals/user'),
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);
327 $requestedProperties = array(
328 '{DAV:}group-member-set',
330 $returnedProperties = array();
332 $result = $plugin->beforeGetProperties('principals/user',$principal,$requestedProperties,$returnedProperties);
334 $this->assertNull($result);
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']);
340 $this->assertEquals(array(), $returnedProperties[200]['{DAV:}group-member-set']->getHrefs());
344 function testGroupMemberShip() {
347 new Sabre_DAV_SimpleCollection('principals', array(
348 $principal = new Sabre_DAVACL_MockPrincipal('user','principals/user'),
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);
358 $requestedProperties = array(
359 '{DAV:}group-membership',
361 $returnedProperties = array();
363 $result = $plugin->beforeGetProperties('principals/user',$principal,$requestedProperties,$returnedProperties);
365 $this->assertNull($result);
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']);
371 $this->assertEquals(array(), $returnedProperties[200]['{DAV:}group-membership']->getHrefs());
375 function testGetDisplayName() {
378 new Sabre_DAV_SimpleCollection('principals', array(
379 $principal = new Sabre_DAVACL_MockPrincipal('user','principals/user'),
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);
389 $requestedProperties = array(
392 $returnedProperties = array();
394 $result = $plugin->beforeGetProperties('principals/user',$principal,$requestedProperties,$returnedProperties);
396 $this->assertNull($result);
398 $this->assertTrue(isset($returnedProperties[200]));
399 $this->assertTrue(isset($returnedProperties[200]['{DAV:}displayname']));
401 $this->assertEquals('user', $returnedProperties[200]['{DAV:}displayname']);