Initial Release of the calendar plugin

This commit is contained in:
Tobias Hößl 2012-06-03 18:19:28 +00:00
parent 45cc9885fc
commit 7115197a33
561 changed files with 189494 additions and 0 deletions

View file

@ -0,0 +1,326 @@
<?php
class Sabre_DAVACL_ACLMethodTest extends PHPUnit_Framework_TestCase {
/**
* @expectedException Sabre_DAV_Exception_BadRequest
*/
function testCallback() {
$acl = new Sabre_DAVACL_Plugin();
$server = new Sabre_DAV_Server();
$server->addPlugin($acl);
$acl->unknownMethod('ACL','test');
}
function testCallbackPassthru() {
$acl = new Sabre_DAVACL_Plugin();
$server = new Sabre_DAV_Server();
$server->addPlugin($acl);
$this->assertNull($acl->unknownMethod('FOO','test'));
}
/**
/**
* @expectedException Sabre_DAV_Exception_MethodNotAllowed
*/
function testNotSupportedByNode() {
$tree = array(
new Sabre_DAV_SimpleCollection('test'),
);
$acl = new Sabre_DAVACL_Plugin();
$server = new Sabre_DAV_Server($tree);
$server->httpRequest = new Sabre_HTTP_Request();
$body = '<?xml version="1.0"?>
<d:acl xmlns:d="DAV:">
</d:acl>';
$server->httpRequest->setBody($body);
$server->addPlugin($acl);
$acl->httpACL('test');
}
function testSuccessSimple() {
$tree = array(
new Sabre_DAVACL_MockACLNode('test',array()),
);
$acl = new Sabre_DAVACL_Plugin();
$server = new Sabre_DAV_Server($tree);
$server->httpRequest = new Sabre_HTTP_Request();
$body = '<?xml version="1.0"?>
<d:acl xmlns:d="DAV:">
</d:acl>';
$server->httpRequest->setBody($body);
$server->addPlugin($acl);
$this->assertNull($acl->httpACL('test'));
}
/**
* @expectedException Sabre_DAVACL_Exception_NotRecognizedPrincipal
*/
function testUnrecognizedPrincipal() {
$tree = array(
new Sabre_DAVACL_MockACLNode('test',array()),
);
$acl = new Sabre_DAVACL_Plugin();
$server = new Sabre_DAV_Server($tree);
$server->httpRequest = new Sabre_HTTP_Request();
$body = '<?xml version="1.0"?>
<d:acl xmlns:d="DAV:">
<d:ace>
<d:grant><d:privilege><d:read /></d:privilege></d:grant>
<d:principal><d:href>/principals/notfound</d:href></d:principal>
</d:ace>
</d:acl>';
$server->httpRequest->setBody($body);
$server->addPlugin($acl);
$acl->httpACL('test');
}
/**
* @expectedException Sabre_DAVACL_Exception_NotRecognizedPrincipal
*/
function testUnrecognizedPrincipal2() {
$tree = array(
new Sabre_DAVACL_MockACLNode('test',array()),
new Sabre_DAV_SimpleCollection('principals',array(
new Sabre_DAV_SimpleCollection('notaprincipal'),
)),
);
$acl = new Sabre_DAVACL_Plugin();
$server = new Sabre_DAV_Server($tree);
$server->httpRequest = new Sabre_HTTP_Request();
$body = '<?xml version="1.0"?>
<d:acl xmlns:d="DAV:">
<d:ace>
<d:grant><d:privilege><d:read /></d:privilege></d:grant>
<d:principal><d:href>/principals/notaprincipal</d:href></d:principal>
</d:ace>
</d:acl>';
$server->httpRequest->setBody($body);
$server->addPlugin($acl);
$acl->httpACL('test');
}
/**
* @expectedException Sabre_DAVACL_Exception_NotSupportedPrivilege
*/
function testUnknownPrivilege() {
$tree = array(
new Sabre_DAVACL_MockACLNode('test',array()),
);
$acl = new Sabre_DAVACL_Plugin();
$server = new Sabre_DAV_Server($tree);
$server->httpRequest = new Sabre_HTTP_Request();
$body = '<?xml version="1.0"?>
<d:acl xmlns:d="DAV:">
<d:ace>
<d:grant><d:privilege><d:bananas /></d:privilege></d:grant>
<d:principal><d:href>/principals/notfound</d:href></d:principal>
</d:ace>
</d:acl>';
$server->httpRequest->setBody($body);
$server->addPlugin($acl);
$acl->httpACL('test');
}
/**
* @expectedException Sabre_DAVACL_Exception_NoAbstract
*/
function testAbstractPrivilege() {
$tree = array(
new Sabre_DAVACL_MockACLNode('test',array()),
);
$acl = new Sabre_DAVACL_Plugin();
$server = new Sabre_DAV_Server($tree);
$server->httpRequest = new Sabre_HTTP_Request();
$body = '<?xml version="1.0"?>
<d:acl xmlns:d="DAV:">
<d:ace>
<d:grant><d:privilege><d:read-acl /></d:privilege></d:grant>
<d:principal><d:href>/principals/notfound</d:href></d:principal>
</d:ace>
</d:acl>';
$server->httpRequest->setBody($body);
$server->addPlugin($acl);
$acl->httpACL('test');
}
/**
* @expectedException Sabre_DAVACL_Exception_AceConflict
*/
function testUpdateProtectedPrivilege() {
$oldACL = array(
array(
'principal' => 'principals/notfound',
'privilege' => '{DAV:}write',
'protected' => true,
),
);
$tree = array(
new Sabre_DAVACL_MockACLNode('test',$oldACL),
);
$acl = new Sabre_DAVACL_Plugin();
$server = new Sabre_DAV_Server($tree);
$server->httpRequest = new Sabre_HTTP_Request();
$body = '<?xml version="1.0"?>
<d:acl xmlns:d="DAV:">
<d:ace>
<d:grant><d:privilege><d:read /></d:privilege></d:grant>
<d:principal><d:href>/principals/notfound</d:href></d:principal>
</d:ace>
</d:acl>';
$server->httpRequest->setBody($body);
$server->addPlugin($acl);
$acl->httpACL('test');
}
/**
* @expectedException Sabre_DAVACL_Exception_AceConflict
*/
function testUpdateProtectedPrivilege2() {
$oldACL = array(
array(
'principal' => 'principals/notfound',
'privilege' => '{DAV:}write',
'protected' => true,
),
);
$tree = array(
new Sabre_DAVACL_MockACLNode('test',$oldACL),
);
$acl = new Sabre_DAVACL_Plugin();
$server = new Sabre_DAV_Server($tree);
$server->httpRequest = new Sabre_HTTP_Request();
$body = '<?xml version="1.0"?>
<d:acl xmlns:d="DAV:">
<d:ace>
<d:grant><d:privilege><d:write /></d:privilege></d:grant>
<d:principal><d:href>/principals/foo</d:href></d:principal>
</d:ace>
</d:acl>';
$server->httpRequest->setBody($body);
$server->addPlugin($acl);
$acl->httpACL('test');
}
/**
* @expectedException Sabre_DAVACL_Exception_AceConflict
*/
function testUpdateProtectedPrivilege3() {
$oldACL = array(
array(
'principal' => 'principals/notfound',
'privilege' => '{DAV:}write',
'protected' => true,
),
);
$tree = array(
new Sabre_DAVACL_MockACLNode('test',$oldACL),
);
$acl = new Sabre_DAVACL_Plugin();
$server = new Sabre_DAV_Server($tree);
$server->httpRequest = new Sabre_HTTP_Request();
$body = '<?xml version="1.0"?>
<d:acl xmlns:d="DAV:">
<d:ace>
<d:grant><d:privilege><d:write /></d:privilege></d:grant>
<d:principal><d:href>/principals/notfound</d:href></d:principal>
</d:ace>
</d:acl>';
$server->httpRequest->setBody($body);
$server->addPlugin($acl);
$acl->httpACL('test');
}
function testSuccessComplex () {
$oldACL = array(
array(
'principal' => 'principals/foo',
'privilege' => '{DAV:}write',
'protected' => true,
),
array(
'principal' => 'principals/bar',
'privilege' => '{DAV:}read',
),
);
$tree = array(
$node = new Sabre_DAVACL_MockACLNode('test',$oldACL),
new Sabre_DAV_SimpleCollection('principals', array(
new Sabre_DAVACL_MockPrincipal('foo','principals/foo'),
new Sabre_DAVACL_MockPrincipal('baz','principals/baz'),
)),
);
$acl = new Sabre_DAVACL_Plugin();
$server = new Sabre_DAV_Server($tree);
$server->httpRequest = new Sabre_HTTP_Request();
$body = '<?xml version="1.0"?>
<d:acl xmlns:d="DAV:">
<d:ace>
<d:grant><d:privilege><d:write /></d:privilege></d:grant>
<d:principal><d:href>/principals/foo</d:href></d:principal>
<d:protected />
</d:ace>
<d:ace>
<d:grant><d:privilege><d:write /></d:privilege></d:grant>
<d:principal><d:href>/principals/baz</d:href></d:principal>
</d:ace>
</d:acl>';
$server->httpRequest->setBody($body);
$server->addPlugin($acl);
$this->assertFalse($acl->unknownMethod('ACL','test'));
$this->assertEquals(array(
array(
'principal' => 'principals/foo',
'privilege' => '{DAV:}write',
'protected' => true,
),
array(
'principal' => 'principals/baz',
'privilege' => '{DAV:}write',
'protected' => false,
),
), $node->getACL());
}
}

View file

@ -0,0 +1,134 @@
<?php
class Sabre_DAVACL_AllowAccessTest extends PHPUnit_Framework_TestCase {
/**
* @var Sabre_DAV_Server
*/
protected $server;
function setUp() {
$nodes = array(
new Sabre_DAV_SimpleCollection('testdir'),
);
$this->server = new Sabre_DAV_Server($nodes);
$aclPlugin = new Sabre_DAVACL_Plugin();
$aclPlugin->allowAccessToNodesWithoutACL = true;
$this->server->addPlugin($aclPlugin);
}
function testGet() {
$this->assertTrue($this->server->broadcastEvent('beforeMethod',array('GET','testdir')));
}
function testGetDoesntExist() {
$r = $this->server->broadcastEvent('beforeMethod',array('GET','foo'));
$this->assertTrue($r);
}
function testHEAD() {
$this->assertTrue($this->server->broadcastEvent('beforeMethod',array('HEAD','testdir')));
}
function testOPTIONS() {
$this->assertTrue($this->server->broadcastEvent('beforeMethod',array('OPTIONS','testdir')));
}
function testPUT() {
$this->assertTrue($this->server->broadcastEvent('beforeMethod',array('PUT','testdir')));
}
function testACL() {
$this->assertTrue($this->server->broadcastEvent('beforeMethod',array('ACL','testdir')));
}
function testPROPPATCH() {
$this->assertTrue($this->server->broadcastEvent('beforeMethod',array('PROPPATCH','testdir')));
}
function testCOPY() {
$this->assertTrue($this->server->broadcastEvent('beforeMethod',array('COPY','testdir')));
}
function testMOVE() {
$this->assertTrue($this->server->broadcastEvent('beforeMethod',array('MOVE','testdir')));
}
function testLOCK() {
$this->assertTrue($this->server->broadcastEvent('beforeMethod',array('LOCK','testdir')));
}
function testBeforeBind() {
$this->assertTrue($this->server->broadcastEvent('beforeBind',array('testdir/file')));
}
function testBeforeUnbind() {
$this->assertTrue($this->server->broadcastEvent('beforeUnbind',array('testdir')));
}
function testAfterGetProperties() {
$properties = array(
'href' => 'foo',
'200' => array(
'{DAV:}displayname' => 'foo',
'{DAV:}getcontentlength' => 500,
),
'404' => array(
'{DAV:}bar' => null,
),
'403' => array(
'{DAV:}owner' => null,
),
);
$expected = array(
'href' => 'foo',
'200' => array(
'{DAV:}displayname' => 'foo',
'{DAV:}getcontentlength' => 500,
),
'404' => array(
'{DAV:}bar' => null,
),
'403' => array(
'{DAV:}owner' => null,
),
);
$r = $this->server->broadcastEvent('afterGetProperties',array('testdir',&$properties));
$this->assertTrue($r);
$this->assertEquals($expected, $properties);
}
}

View file

@ -0,0 +1,185 @@
<?php
class Sabre_DAVACL_BlockAccessTest extends PHPUnit_Framework_TestCase {
/**
* @var Sabre_DAV_Server
*/
protected $server;
protected $plugin;
function setUp() {
$nodes = array(
new Sabre_DAV_SimpleCollection('testdir'),
);
$this->server = new Sabre_DAV_Server($nodes);
$this->plugin = new Sabre_DAVACL_Plugin();
$this->plugin->allowAccessToNodesWithoutACL = false;
$this->server->addPlugin($this->plugin);
}
/**
* @expectedException Sabre_DAVACL_Exception_NeedPrivileges
*/
function testGet() {
$this->server->broadcastEvent('beforeMethod',array('GET','testdir'));
}
function testGetDoesntExist() {
$r = $this->server->broadcastEvent('beforeMethod',array('GET','foo'));
$this->assertTrue($r);
}
/**
* @expectedException Sabre_DAVACL_Exception_NeedPrivileges
*/
function testHEAD() {
$this->server->broadcastEvent('beforeMethod',array('HEAD','testdir'));
}
/**
* @expectedException Sabre_DAVACL_Exception_NeedPrivileges
*/
function testOPTIONS() {
$this->server->broadcastEvent('beforeMethod',array('OPTIONS','testdir'));
}
/**
* @expectedException Sabre_DAVACL_Exception_NeedPrivileges
*/
function testPUT() {
$this->server->broadcastEvent('beforeMethod',array('PUT','testdir'));
}
/**
* @expectedException Sabre_DAVACL_Exception_NeedPrivileges
*/
function testPROPPATCH() {
$this->server->broadcastEvent('beforeMethod',array('PROPPATCH','testdir'));
}
/**
* @expectedException Sabre_DAVACL_Exception_NeedPrivileges
*/
function testCOPY() {
$this->server->broadcastEvent('beforeMethod',array('COPY','testdir'));
}
/**
* @expectedException Sabre_DAVACL_Exception_NeedPrivileges
*/
function testMOVE() {
$this->server->broadcastEvent('beforeMethod',array('MOVE','testdir'));
}
/**
* @expectedException Sabre_DAVACL_Exception_NeedPrivileges
*/
function testACL() {
$this->server->broadcastEvent('beforeMethod',array('ACL','testdir'));
}
/**
* @expectedException Sabre_DAVACL_Exception_NeedPrivileges
*/
function testLOCK() {
$this->server->broadcastEvent('beforeMethod',array('LOCK','testdir'));
}
/**
* @expectedException Sabre_DAVACL_Exception_NeedPrivileges
*/
function testBeforeBind() {
$this->server->broadcastEvent('beforeBind',array('testdir/file'));
}
/**
* @expectedException Sabre_DAVACL_Exception_NeedPrivileges
*/
function testBeforeUnbind() {
$this->server->broadcastEvent('beforeUnbind',array('testdir'));
}
function testBeforeGetProperties() {
$requestedProperties = array(
'{DAV:}displayname',
'{DAV:}getcontentlength',
'{DAV:}bar',
'{DAV:}owner',
);
$returnedProperties = array();
$arguments = array(
'testdir',
new Sabre_DAV_SimpleCollection('testdir'),
&$requestedProperties,
&$returnedProperties
);
$r = $this->server->broadcastEvent('beforeGetProperties',$arguments);
$this->assertTrue($r);
$expected = array(
'403' => array(
'{DAV:}displayname' => null,
'{DAV:}getcontentlength' => null,
'{DAV:}bar' => null,
'{DAV:}owner' => null,
),
);
$this->assertEquals($expected, $returnedProperties);
$this->assertEquals(array(), $requestedProperties);
}
function testBeforeGetPropertiesNoListing() {
$this->plugin->hideNodesFromListings = true;
$requestedProperties = array(
'{DAV:}displayname',
'{DAV:}getcontentlength',
'{DAV:}bar',
'{DAV:}owner',
);
$returnedProperties = array();
$arguments = array(
'testdir',
new Sabre_DAV_SimpleCollection('testdir'),
&$requestedProperties,
&$returnedProperties
);
$r = $this->server->broadcastEvent('beforeGetProperties',$arguments);
$this->assertFalse($r);
}
}

View file

@ -0,0 +1,35 @@
<?php
class Sabre_DAVACL_Exception_AceConflictTest extends PHPUnit_Framework_TestCase {
function testSerialize() {
$ex = new Sabre_DAVACL_Exception_AceConflict('message');
$server = new Sabre_DAV_Server();
$dom = new DOMDocument('1.0','utf-8');
$root = $dom->createElementNS('DAV:','d:root');
$dom->appendChild($root);
$ex->serialize($server, $root);
$xpaths = array(
'/d:root' => 1,
'/d:root/d:no-ace-conflict' => 1,
);
// Reloading because PHP DOM sucks
$dom2 = new DOMDocument('1.0', 'utf-8');
$dom2->loadXML($dom->saveXML());
$dxpath = new DOMXPath($dom2);
$dxpath->registerNamespace('d','DAV:');
foreach($xpaths as $xpath=>$count) {
$this->assertEquals($count, $dxpath->query($xpath)->length, 'Looking for : ' . $xpath . ', we could only find ' . $dxpath->query($xpath)->length . ' elements, while we expected ' . $count);
}
}
}

View file

@ -0,0 +1,45 @@
<?php
class Sabre_DAVACL_Exception_NeedPrivilegesTest extends PHPUnit_Framework_TestCase {
function testSerialize() {
$uri = 'foo';
$privileges = array(
'{DAV:}read',
'{DAV:}write',
);
$ex = new Sabre_DAVACL_Exception_NeedPrivileges($uri, $privileges);
$server = new Sabre_DAV_Server();
$dom = new DOMDocument('1.0','utf-8');
$root = $dom->createElementNS('DAV:','d:root');
$dom->appendChild($root);
$ex->serialize($server, $root);
$xpaths = array(
'/d:root' => 1,
'/d:root/d:need-privileges' => 1,
'/d:root/d:need-privileges/d:resource' => 2,
'/d:root/d:need-privileges/d:resource/d:href' => 2,
'/d:root/d:need-privileges/d:resource/d:privilege' => 2,
'/d:root/d:need-privileges/d:resource/d:privilege/d:read' => 1,
'/d:root/d:need-privileges/d:resource/d:privilege/d:write' => 1,
);
// Reloading because PHP DOM sucks
$dom2 = new DOMDocument('1.0', 'utf-8');
$dom2->loadXML($dom->saveXML());
$dxpath = new DOMXPath($dom2);
$dxpath->registerNamespace('d','DAV:');
foreach($xpaths as $xpath=>$count) {
$this->assertEquals($count, $dxpath->query($xpath)->length, 'Looking for : ' . $xpath . ', we could only find ' . $dxpath->query($xpath)->length . ' elements, while we expected ' . $count);
}
}
}

View file

@ -0,0 +1,35 @@
<?php
class Sabre_DAVACL_Exception_NoAbstractTest extends PHPUnit_Framework_TestCase {
function testSerialize() {
$ex = new Sabre_DAVACL_Exception_NoAbstract('message');
$server = new Sabre_DAV_Server();
$dom = new DOMDocument('1.0','utf-8');
$root = $dom->createElementNS('DAV:','d:root');
$dom->appendChild($root);
$ex->serialize($server, $root);
$xpaths = array(
'/d:root' => 1,
'/d:root/d:no-abstract' => 1,
);
// Reloading because PHP DOM sucks
$dom2 = new DOMDocument('1.0', 'utf-8');
$dom2->loadXML($dom->saveXML());
$dxpath = new DOMXPath($dom2);
$dxpath->registerNamespace('d','DAV:');
foreach($xpaths as $xpath=>$count) {
$this->assertEquals($count, $dxpath->query($xpath)->length, 'Looking for : ' . $xpath . ', we could only find ' . $dxpath->query($xpath)->length . ' elements, while we expected ' . $count);
}
}
}

View file

@ -0,0 +1,35 @@
<?php
class Sabre_DAVACL_Exception_NotRecognizedPrincipalTest extends PHPUnit_Framework_TestCase {
function testSerialize() {
$ex = new Sabre_DAVACL_Exception_NotRecognizedPrincipal('message');
$server = new Sabre_DAV_Server();
$dom = new DOMDocument('1.0','utf-8');
$root = $dom->createElementNS('DAV:','d:root');
$dom->appendChild($root);
$ex->serialize($server, $root);
$xpaths = array(
'/d:root' => 1,
'/d:root/d:recognized-principal' => 1,
);
// Reloading because PHP DOM sucks
$dom2 = new DOMDocument('1.0', 'utf-8');
$dom2->loadXML($dom->saveXML());
$dxpath = new DOMXPath($dom2);
$dxpath->registerNamespace('d','DAV:');
foreach($xpaths as $xpath=>$count) {
$this->assertEquals($count, $dxpath->query($xpath)->length, 'Looking for : ' . $xpath . ', we could only find ' . $dxpath->query($xpath)->length . ' elements, while we expected ' . $count);
}
}
}

View file

@ -0,0 +1,35 @@
<?php
class Sabre_DAVACL_Exception_NotSupportedPrivilegeTest extends PHPUnit_Framework_TestCase {
function testSerialize() {
$ex = new Sabre_DAVACL_Exception_NotSupportedPrivilege('message');
$server = new Sabre_DAV_Server();
$dom = new DOMDocument('1.0','utf-8');
$root = $dom->createElementNS('DAV:','d:root');
$dom->appendChild($root);
$ex->serialize($server, $root);
$xpaths = array(
'/d:root' => 1,
'/d:root/d:not-supported-privilege' => 1,
);
// Reloading because PHP DOM sucks
$dom2 = new DOMDocument('1.0', 'utf-8');
$dom2->loadXML($dom->saveXML());
$dxpath = new DOMXPath($dom2);
$dxpath->registerNamespace('d','DAV:');
foreach($xpaths as $xpath=>$count) {
$this->assertEquals($count, $dxpath->query($xpath)->length, 'Looking for : ' . $xpath . ', we could only find ' . $dxpath->query($xpath)->length . ' elements, while we expected ' . $count);
}
}
}

View file

@ -0,0 +1,353 @@
<?php
require_once 'Sabre/HTTP/ResponseMock.php';
class Sabre_DAVACL_ExpandPropertiesTest extends PHPUnit_Framework_TestCase {
function getServer() {
$tree = array(
new Sabre_DAVACL_MockPropertyNode('node1', array(
'{http://sabredav.org/ns}simple' => 'foo',
'{http://sabredav.org/ns}href' => new Sabre_DAV_Property_Href('node2'),
'{DAV:}displayname' => 'Node 1',
)),
new Sabre_DAVACL_MockPropertyNode('node2', array(
'{http://sabredav.org/ns}simple' => 'simple',
'{http://sabredav.org/ns}hreflist' => new Sabre_DAV_Property_HrefList(array('node1','node3')),
'{DAV:}displayname' => 'Node 2',
)),
new Sabre_DAVACL_MockPropertyNode('node3', array(
'{http://sabredav.org/ns}simple' => 'simple',
'{DAV:}displayname' => 'Node 3',
)),
);
$fakeServer = new Sabre_DAV_Server($tree);
$fakeServer->debugExceptions = true;
$fakeServer->httpResponse = new Sabre_HTTP_ResponseMock();
$plugin = new Sabre_DAVACL_Plugin();
$plugin->allowAccessToNodesWithoutACL = true;
$this->assertTrue($plugin instanceof Sabre_DAVACL_Plugin);
$fakeServer->addPlugin($plugin);
$this->assertEquals($plugin, $fakeServer->getPlugin('acl'));
return $fakeServer;
}
function testSimple() {
$xml = '<?xml version="1.0"?>
<d:expand-property xmlns:d="DAV:">
<d:property name="displayname" />
<d:property name="foo" namespace="http://www.sabredav.org/NS/2010/nonexistant" />
<d:property name="simple" namespace="http://sabredav.org/ns" />
<d:property name="href" namespace="http://sabredav.org/ns" />
</d:expand-property>';
$serverVars = array(
'REQUEST_METHOD' => 'REPORT',
'HTTP_DEPTH' => '0',
'REQUEST_URI' => '/node1',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody($xml);
$server = $this->getServer();
$server->httpRequest = $request;
$server->exec();
$this->assertEquals('HTTP/1.1 207 Multi-Status', $server->httpResponse->status,'Incorrect status code received. Full body: ' . $server->httpResponse->body);
$this->assertEquals(array(
'Content-Type' => 'application/xml; charset=utf-8',
), $server->httpResponse->headers);
$check = array(
'/d:multistatus',
'/d:multistatus/d:response' => 1,
'/d:multistatus/d:response/d:href' => 1,
'/d:multistatus/d:response/d:propstat' => 2,
'/d:multistatus/d:response/d:propstat/d:prop' => 2,
'/d:multistatus/d:response/d:propstat/d:prop/d:displayname' => 1,
'/d:multistatus/d:response/d:propstat/d:prop/s:simple' => 1,
'/d:multistatus/d:response/d:propstat/d:prop/s:href' => 1,
'/d:multistatus/d:response/d:propstat/d:prop/s:href/d:href' => 1,
);
$xml = simplexml_load_string($server->httpResponse->body);
$xml->registerXPathNamespace('d','DAV:');
$xml->registerXPathNamespace('s','http://sabredav.org/ns');
foreach($check as $v1=>$v2) {
$xpath = is_int($v1)?$v2:$v1;
$result = $xml->xpath($xpath);
$count = 1;
if (!is_int($v1)) $count = $v2;
$this->assertEquals($count,count($result), 'we expected ' . $count . ' appearances of ' . $xpath . ' . We found ' . count($result) . '. Full response: ' . $server->httpResponse->body);
}
}
/**
* @depends testSimple
*/
function testExpand() {
$xml = '<?xml version="1.0"?>
<d:expand-property xmlns:d="DAV:">
<d:property name="href" namespace="http://sabredav.org/ns">
<d:property name="displayname" />
</d:property>
</d:expand-property>';
$serverVars = array(
'REQUEST_METHOD' => 'REPORT',
'HTTP_DEPTH' => '0',
'REQUEST_URI' => '/node1',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody($xml);
$server = $this->getServer();
$server->httpRequest = $request;
$server->exec();
$this->assertEquals('HTTP/1.1 207 Multi-Status', $server->httpResponse->status, 'Incorrect response status received. Full response body: ' . $server->httpResponse->body);
$this->assertEquals(array(
'Content-Type' => 'application/xml; charset=utf-8',
), $server->httpResponse->headers);
$check = array(
'/d:multistatus',
'/d:multistatus/d:response' => 1,
'/d:multistatus/d:response/d:href' => 1,
'/d:multistatus/d:response/d:propstat' => 1,
'/d:multistatus/d:response/d:propstat/d:prop' => 1,
'/d:multistatus/d:response/d:propstat/d:prop/s:href' => 1,
'/d:multistatus/d:response/d:propstat/d:prop/s:href/d:response' => 1,
'/d:multistatus/d:response/d:propstat/d:prop/s:href/d:response/d:href' => 1,
'/d:multistatus/d:response/d:propstat/d:prop/s:href/d:response/d:propstat' => 1,
'/d:multistatus/d:response/d:propstat/d:prop/s:href/d:response/d:propstat/d:prop' => 1,
'/d:multistatus/d:response/d:propstat/d:prop/s:href/d:response/d:propstat/d:prop/d:displayname' => 1,
);
$xml = simplexml_load_string($server->httpResponse->body);
$xml->registerXPathNamespace('d','DAV:');
$xml->registerXPathNamespace('s','http://sabredav.org/ns');
foreach($check as $v1=>$v2) {
$xpath = is_int($v1)?$v2:$v1;
$result = $xml->xpath($xpath);
$count = 1;
if (!is_int($v1)) $count = $v2;
$this->assertEquals($count,count($result), 'we expected ' . $count . ' appearances of ' . $xpath . ' . We found ' . count($result));
}
}
/**
* @depends testSimple
*/
function testExpandHrefList() {
$xml = '<?xml version="1.0"?>
<d:expand-property xmlns:d="DAV:">
<d:property name="hreflist" namespace="http://sabredav.org/ns">
<d:property name="displayname" />
</d:property>
</d:expand-property>';
$serverVars = array(
'REQUEST_METHOD' => 'REPORT',
'HTTP_DEPTH' => '0',
'REQUEST_URI' => '/node2',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody($xml);
$server = $this->getServer();
$server->httpRequest = $request;
$server->exec();
$this->assertEquals('HTTP/1.1 207 Multi-Status', $server->httpResponse->status);
$this->assertEquals(array(
'Content-Type' => 'application/xml; charset=utf-8',
), $server->httpResponse->headers);
$check = array(
'/d:multistatus',
'/d:multistatus/d:response' => 1,
'/d:multistatus/d:response/d:href' => 1,
'/d:multistatus/d:response/d:propstat' => 1,
'/d:multistatus/d:response/d:propstat/d:prop' => 1,
'/d:multistatus/d:response/d:propstat/d:prop/s:hreflist' => 1,
'/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response' => 2,
'/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:href' => 2,
'/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat' => 2,
'/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat/d:prop' => 2,
'/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat/d:prop/d:displayname' => 2,
);
$xml = simplexml_load_string($server->httpResponse->body);
$xml->registerXPathNamespace('d','DAV:');
$xml->registerXPathNamespace('s','http://sabredav.org/ns');
foreach($check as $v1=>$v2) {
$xpath = is_int($v1)?$v2:$v1;
$result = $xml->xpath($xpath);
$count = 1;
if (!is_int($v1)) $count = $v2;
$this->assertEquals($count,count($result), 'we expected ' . $count . ' appearances of ' . $xpath . ' . We found ' . count($result));
}
}
/**
* @depends testExpand
*/
function testExpandDeep() {
$xml = '<?xml version="1.0"?>
<d:expand-property xmlns:d="DAV:">
<d:property name="hreflist" namespace="http://sabredav.org/ns">
<d:property name="href" namespace="http://sabredav.org/ns">
<d:property name="displayname" />
</d:property>
<d:property name="displayname" />
</d:property>
</d:expand-property>';
$serverVars = array(
'REQUEST_METHOD' => 'REPORT',
'HTTP_DEPTH' => '0',
'REQUEST_URI' => '/node2',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody($xml);
$server = $this->getServer();
$server->httpRequest = $request;
$server->exec();
$this->assertEquals('HTTP/1.1 207 Multi-Status', $server->httpResponse->status);
$this->assertEquals(array(
'Content-Type' => 'application/xml; charset=utf-8',
), $server->httpResponse->headers);
$check = array(
'/d:multistatus',
'/d:multistatus/d:response' => 1,
'/d:multistatus/d:response/d:href' => 1,
'/d:multistatus/d:response/d:propstat' => 1,
'/d:multistatus/d:response/d:propstat/d:prop' => 1,
'/d:multistatus/d:response/d:propstat/d:prop/s:hreflist' => 1,
'/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response' => 2,
'/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:href' => 2,
'/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat' => 3,
'/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat/d:prop' => 3,
'/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat/d:prop/d:displayname' => 2,
'/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat/d:prop/s:href' => 2,
'/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat/d:prop/s:href/d:response' => 1,
'/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat/d:prop/s:href/d:response/d:href' => 1,
'/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat/d:prop/s:href/d:response/d:propstat' => 1,
'/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat/d:prop/s:href/d:response/d:propstat/d:prop' => 1,
'/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat/d:prop/s:href/d:response/d:propstat/d:prop/d:displayname' => 1,
);
$xml = simplexml_load_string($server->httpResponse->body);
$xml->registerXPathNamespace('d','DAV:');
$xml->registerXPathNamespace('s','http://sabredav.org/ns');
foreach($check as $v1=>$v2) {
$xpath = is_int($v1)?$v2:$v1;
$result = $xml->xpath($xpath);
$count = 1;
if (!is_int($v1)) $count = $v2;
$this->assertEquals($count,count($result), 'we expected ' . $count . ' appearances of ' . $xpath . ' . We found ' . count($result));
}
}
}
class Sabre_DAVACL_MockPropertyNode implements Sabre_DAV_INode, Sabre_DAV_IProperties {
function __construct($name, array $properties) {
$this->name = $name;
$this->properties = $properties;
}
function getName() {
return $this->name;
}
function getProperties($requestedProperties) {
$returnedProperties = array();
foreach($requestedProperties as $requestedProperty) {
if (isset($this->properties[$requestedProperty])) {
$returnedProperties[$requestedProperty] =
$this->properties[$requestedProperty];
}
}
return $returnedProperties;
}
function delete() {
throw new Sabre_DAV_Exception('Not implemented');
}
function setName($name) {
throw new Sabre_DAV_Exception('Not implemented');
}
function getLastModified() {
return null;
}
function updateProperties($properties) {
throw new Sabre_DAV_Exception('Not implemented');
}
}

View file

@ -0,0 +1,51 @@
<?php
class Sabre_DAVACL_MockACLNode extends Sabre_DAV_Node implements Sabre_DAVACL_IACL {
public $name;
public $acl;
function __construct($name, array $acl = array()) {
$this->name = $name;
$this->acl = $acl;
}
function getName() {
return $this->name;
}
function getOwner() {
return null;
}
function getGroup() {
return null;
}
function getACL() {
return $this->acl;
}
function setACL(array $acl) {
$this->acl = $acl;
}
function getSupportedPrivilegeSet() {
return null;
}
}

View file

@ -0,0 +1,61 @@
<?php
class Sabre_DAVACL_MockPrincipal extends Sabre_DAV_Node implements Sabre_DAVACL_IPrincipal {
public $name;
public $principalUrl;
public $groupMembership = array();
public $groupMemberSet = array();
function __construct($name,$principalUrl,array $groupMembership = array(), array $groupMemberSet = array()) {
$this->name = $name;
$this->principalUrl = $principalUrl;
$this->groupMembership = $groupMembership;
$this->groupMemberSet = $groupMemberSet;
}
function getName() {
return $this->name;
}
function getDisplayName() {
return $this->getName();
}
function getAlternateUriSet() {
return array();
}
function getPrincipalUrl() {
return $this->principalUrl;
}
function getGroupMemberSet() {
return $this->groupMemberSet;
}
function getGroupMemberShip() {
return $this->groupMembership;
}
function setGroupMemberSet(array $groupMemberSet) {
$this->groupMemberSet = $groupMemberSet;
}
}

View file

@ -0,0 +1,182 @@
<?php
class Sabre_DAVACL_MockPrincipalBackend implements Sabre_DAVACL_IPrincipalBackend {
public $groupMembers = array();
public $principals;
function __construct() {
$this->principals = array(
array(
'uri' => 'principals/user1',
'{DAV:}displayname' => 'User 1',
'{http://sabredav.org/ns}email-address' => 'user1.sabredav@sabredav.org',
'{http://sabredav.org/ns}vcard-url' => 'addressbooks/user1/book1/vcard1.vcf',
),
array(
'uri' => 'principals/admin',
'{DAV:}displayname' => 'Admin',
),
array(
'uri' => 'principals/user2',
'{DAV:}displayname' => 'User 2',
'{http://sabredav.org/ns}email-address' => 'user2.sabredav@sabredav.org',
),
);
}
function getPrincipalsByPrefix($prefix) {
$prefix = trim($prefix,'/') . '/';
$return = array();
foreach($this->principals as $principal) {
if (strpos($principal['uri'], $prefix)!==0) continue;
$return[] = $principal;
}
return $return;
}
function addPrincipal(array $principal) {
$this->principals[] = $principal;
}
function getPrincipalByPath($path) {
foreach($this->getPrincipalsByPrefix('principals') as $principal) {
if ($principal['uri'] === $path) return $principal;
}
}
function searchPrincipals($prefixPath, array $searchProperties) {
$matches = array();
foreach($this->getPrincipalsByPrefix($prefixPath) as $principal) {
foreach($searchProperties as $key=>$value) {
if (!isset($principal[$key])) {
continue 2;
}
if (mb_stripos($principal[$key],$value, 0, 'UTF-8')===false) {
continue 2;
}
}
$matches[] = $principal['uri'];
}
return $matches;
}
function getGroupMemberSet($path) {
return isset($this->groupMembers[$path]) ? $this->groupMembers[$path] : array();
}
function getGroupMembership($path) {
$membership = array();
foreach($this->groupMembers as $group=>$members) {
if (in_array($path, $members)) $membership[] = $group;
}
return $membership;
}
function setGroupMemberSet($path, array $members) {
$this->groupMembers[$path] = $members;
}
/**
* Updates one ore more webdav properties on a principal.
*
* The list of mutations is supplied as an array. Each key in the array is
* a propertyname, such as {DAV:}displayname.
*
* Each value is the actual value to be updated. If a value is null, it
* must be deleted.
*
* This method should be atomic. It must either completely succeed, or
* completely fail. Success and failure can simply be returned as 'true' or
* 'false'.
*
* It is also possible to return detailed failure information. In that case
* an array such as this should be returned:
*
* array(
* 200 => array(
* '{DAV:}prop1' => null,
* ),
* 201 => array(
* '{DAV:}prop2' => null,
* ),
* 403 => array(
* '{DAV:}prop3' => null,
* ),
* 424 => array(
* '{DAV:}prop4' => null,
* ),
* );
*
* In this previous example prop1 was successfully updated or deleted, and
* prop2 was succesfully created.
*
* prop3 failed to update due to '403 Forbidden' and because of this prop4
* also could not be updated with '424 Failed dependency'.
*
* This last example was actually incorrect. While 200 and 201 could appear
* in 1 response, if there's any error (403) the other properties should
* always fail with 423 (failed dependency).
*
* But anyway, if you don't want to scratch your head over this, just
* return true or false.
*
* @param string $path
* @param array $mutations
* @return array|bool
*/
public function updatePrincipal($path, $mutations) {
$value = null;
foreach($this->principals as $principalIndex=>$value) {
if ($value['uri'] === $path) {
$principal = $value;
break;
}
}
if (!$principal) return false;
foreach($mutations as $prop=>$value) {
if (is_null($value) && isset($principal[$prop])) {
unset($principal[$prop]);
} else {
$principal[$prop] = $value;
}
}
$this->principals[$principalIndex] = $principal;
return true;
}
}

View file

@ -0,0 +1,79 @@
<?php
require_once 'Sabre/DAVACL/MockACLNode.php';
require_once 'Sabre/DAV/Auth/MockBackend.php';
require_once 'Sabre/HTTP/ResponseMock.php';
require_once 'Sabre/DAVACL/MockPrincipalBackend.php';
class Sabre_DAVACL_PluginAdminTest extends PHPUnit_Framework_TestCase {
function testNoAdminAccess() {
$principalBackend = new Sabre_DAVACL_MockPrincipalBackend();
$tree = array(
new Sabre_DAVACL_MockACLNode('adminonly', array()),
new Sabre_DAVACL_PrincipalCollection($principalBackend),
);
$fakeServer = new Sabre_DAV_Server($tree);
$plugin = new Sabre_DAV_Auth_Plugin(new Sabre_DAV_Auth_MockBackend(),'realm');
$fakeServer->addPlugin($plugin);
$plugin = new Sabre_DAVACL_Plugin();
$fakeServer->addPlugin($plugin);
$request = new Sabre_HTTP_Request(array(
'REQUEST_METHOD' => 'OPTIONS',
'HTTP_DEPTH' => 1,
'REQUEST_URI' => '/adminonly',
));
$response = new Sabre_HTTP_ResponseMock();
$fakeServer->httpRequest = $request;
$fakeServer->httpResponse = $response;
$fakeServer->exec();
$this->assertEquals('HTTP/1.1 403 Forbidden', $response->status);
}
/**
* @depends testNoAdminAccess
*/
function testAdminAccess() {
$principalBackend = new Sabre_DAVACL_MockPrincipalBackend();
$tree = array(
new Sabre_DAVACL_MockACLNode('adminonly', array()),
new Sabre_DAVACL_PrincipalCollection($principalBackend),
);
$fakeServer = new Sabre_DAV_Server($tree);
$plugin = new Sabre_DAV_Auth_Plugin(new Sabre_DAV_Auth_MockBackend(),'realm');
$fakeServer->addPlugin($plugin);
$plugin = new Sabre_DAVACL_Plugin();
$plugin->adminPrincipals = array(
'principals/admin',
);
$fakeServer->addPlugin($plugin);
$request = new Sabre_HTTP_Request(array(
'REQUEST_METHOD' => 'OPTIONS',
'HTTP_DEPTH' => 1,
'REQUEST_URI' => '/adminonly',
));
$response = new Sabre_HTTP_ResponseMock();
$fakeServer->httpRequest = $request;
$fakeServer->httpResponse = $response;
$fakeServer->exec();
$this->assertEquals('HTTP/1.1 200 OK', $response->status);
}
}

View file

@ -0,0 +1,404 @@
<?php
require_once 'Sabre/DAV/Auth/MockBackend.php';
require_once 'Sabre/DAVACL/MockPrincipal.php';
class Sabre_DAVACL_PluginPropertiesTest extends PHPUnit_Framework_TestCase {
function testPrincipalCollectionSet() {
$plugin = new Sabre_DAVACL_Plugin();
$plugin->principalCollectionSet = array(
'principals1',
'principals2',
);
$requestedProperties = array(
'{DAV:}principal-collection-set',
);
$returnedProperties = array(
200 => array(),
404 => array(),
);
$server = new Sabre_DAV_Server();
$server->addPlugin($plugin);
$this->assertNull($plugin->beforeGetProperties('', new Sabre_DAV_SimpleCollection('root'), $requestedProperties, $returnedProperties));
$this->assertEquals(1,count($returnedProperties[200]));
$this->assertArrayHasKey('{DAV:}principal-collection-set',$returnedProperties[200]);
$this->assertInstanceOf('Sabre_DAV_Property_HrefList', $returnedProperties[200]['{DAV:}principal-collection-set']);
$expected = array(
'principals1/',
'principals2/',
);
$this->assertEquals($expected, $returnedProperties[200]['{DAV:}principal-collection-set']->getHrefs());
}
function testCurrentUserPrincipal() {
$fakeServer = new Sabre_DAV_Server();
$plugin = new Sabre_DAV_Auth_Plugin(new Sabre_DAV_Auth_MockBackend(),'realm');
$fakeServer->addPlugin($plugin);
$plugin = new Sabre_DAVACL_Plugin();
$fakeServer->addPlugin($plugin);
$requestedProperties = array(
'{DAV:}current-user-principal',
);
$returnedProperties = array(
200 => array(),
404 => array(),
);
$this->assertNull($plugin->beforeGetProperties('', new Sabre_DAV_SimpleCollection('root'), $requestedProperties, $returnedProperties));
$this->assertEquals(1,count($returnedProperties[200]));
$this->assertArrayHasKey('{DAV:}current-user-principal',$returnedProperties[200]);
$this->assertInstanceOf('Sabre_DAVACL_Property_Principal', $returnedProperties[200]['{DAV:}current-user-principal']);
$this->assertEquals(Sabre_DAVACL_Property_Principal::UNAUTHENTICATED, $returnedProperties[200]['{DAV:}current-user-principal']->getType());
// This will force the login
$fakeServer->broadCastEvent('beforeMethod',array('GET',''));
$requestedProperties = array(
'{DAV:}current-user-principal',
);
$returnedProperties = array(
200 => array(),
404 => array(),
);
$this->assertNull($plugin->beforeGetProperties('', new Sabre_DAV_SimpleCollection('root'), $requestedProperties, $returnedProperties));
$this->assertEquals(1,count($returnedProperties[200]));
$this->assertArrayHasKey('{DAV:}current-user-principal',$returnedProperties[200]);
$this->assertInstanceOf('Sabre_DAVACL_Property_Principal', $returnedProperties[200]['{DAV:}current-user-principal']);
$this->assertEquals(Sabre_DAVACL_Property_Principal::HREF, $returnedProperties[200]['{DAV:}current-user-principal']->getType());
$this->assertEquals('principals/admin/', $returnedProperties[200]['{DAV:}current-user-principal']->getHref());
}
function testSupportedPrivilegeSet() {
$plugin = new Sabre_DAVACL_Plugin();
$server = new Sabre_DAV_Server();
$server->addPlugin($plugin);
$requestedProperties = array(
'{DAV:}supported-privilege-set',
);
$returnedProperties = array(
200 => array(),
404 => array(),
);
$this->assertNull($plugin->beforeGetProperties('', new Sabre_DAV_SimpleCollection('root'), $requestedProperties, $returnedProperties));
$this->assertEquals(1,count($returnedProperties[200]));
$this->assertArrayHasKey('{DAV:}supported-privilege-set',$returnedProperties[200]);
$this->assertInstanceOf('Sabre_DAVACL_Property_SupportedPrivilegeSet', $returnedProperties[200]['{DAV:}supported-privilege-set']);
$server = new Sabre_DAV_Server();
$prop = $returnedProperties[200]['{DAV:}supported-privilege-set'];
$dom = new DOMDocument('1.0', 'utf-8');
$root = $dom->createElement('d:root');
$root->setAttribute('xmlns:d','DAV:');
$dom->appendChild($root);
$prop->serialize($server, $root);
$xpaths = array(
'/d:root' => 1,
'/d:root/d:supported-privilege' => 1,
'/d:root/d:supported-privilege/d:privilege' => 1,
'/d:root/d:supported-privilege/d:privilege/d:all' => 1,
'/d:root/d:supported-privilege/d:abstract' => 1,
'/d:root/d:supported-privilege/d:supported-privilege' => 2,
'/d:root/d:supported-privilege/d:supported-privilege/d:privilege' => 2,
'/d:root/d:supported-privilege/d:supported-privilege/d:privilege/d:read' => 1,
'/d:root/d:supported-privilege/d:supported-privilege/d:privilege/d:write' => 1,
'/d:root/d:supported-privilege/d:supported-privilege/d:supported-privilege' => 8,
'/d:root/d:supported-privilege/d:supported-privilege/d:supported-privilege/d:privilege' => 8,
'/d:root/d:supported-privilege/d:supported-privilege/d:supported-privilege/d:privilege/d:read-acl' => 1,
'/d:root/d:supported-privilege/d:supported-privilege/d:supported-privilege/d:privilege/d:read-current-user-privilege-set' => 1,
'/d:root/d:supported-privilege/d:supported-privilege/d:supported-privilege/d:privilege/d:write-content' => 1,
'/d:root/d:supported-privilege/d:supported-privilege/d:supported-privilege/d:privilege/d:write-properties' => 1,
'/d:root/d:supported-privilege/d:supported-privilege/d:supported-privilege/d:privilege/d:write-acl' => 1,
'/d:root/d:supported-privilege/d:supported-privilege/d:supported-privilege/d:privilege/d:bind' => 1,
'/d:root/d:supported-privilege/d:supported-privilege/d:supported-privilege/d:privilege/d:unbind' => 1,
'/d:root/d:supported-privilege/d:supported-privilege/d:supported-privilege/d:privilege/d:unlock' => 1,
'/d:root/d:supported-privilege/d:supported-privilege/d:supported-privilege/d:abstract' => 8,
);
// reloading because php dom sucks
$dom2 = new DOMDocument('1.0', 'utf-8');
$dom2->loadXML($dom->saveXML());
$dxpath = new DOMXPath($dom2);
$dxpath->registerNamespace('d','DAV:');
foreach($xpaths as $xpath=>$count) {
$this->assertEquals($count, $dxpath->query($xpath)->length, 'Looking for : ' . $xpath . ', we could only find ' . $dxpath->query($xpath)->length . ' elements, while we expected ' . $count);
}
}
function testACL() {
$plugin = new Sabre_DAVACL_Plugin();
$nodes = array(
new Sabre_DAVACL_MockACLNode('foo', array(
array(
'principal' => 'principals/admin',
'privilege' => '{DAV:}read',
)
)),
new Sabre_DAV_SimpleCollection('principals', array(
$principal = new Sabre_DAVACL_MockPrincipal('admin','principals/admin'),
)),
);
$server = new Sabre_DAV_Server($nodes);
$server->addPlugin($plugin);
$authPlugin = new Sabre_DAV_Auth_Plugin(new Sabre_DAV_Auth_MockBackend(),'realm');
$server->addPlugin($authPlugin);
// Force login
$authPlugin->beforeMethod('BLA','foo');
$requestedProperties = array(
'{DAV:}acl',
);
$returnedProperties = array(
200 => array(),
404 => array(),
);
$this->assertNull($plugin->beforeGetProperties('foo', $nodes[0], $requestedProperties, $returnedProperties));
$this->assertEquals(1,count($returnedProperties[200]),'The {DAV:}acl property did not return from the list. Full list: ' . print_r($returnedProperties,true));
$this->assertArrayHasKey('{DAV:}acl',$returnedProperties[200]);
$this->assertInstanceOf('Sabre_DAVACL_Property_ACL', $returnedProperties[200]['{DAV:}acl']);
}
function testACLRestrictions() {
$plugin = new Sabre_DAVACL_Plugin();
$nodes = array(
new Sabre_DAVACL_MockACLNode('foo', array(
array(
'principal' => 'principals/admin',
'privilege' => '{DAV:}read',
)
)),
new Sabre_DAV_SimpleCollection('principals', array(
$principal = new Sabre_DAVACL_MockPrincipal('admin','principals/admin'),
)),
);
$server = new Sabre_DAV_Server($nodes);
$server->addPlugin($plugin);
$authPlugin = new Sabre_DAV_Auth_Plugin(new Sabre_DAV_Auth_MockBackend(),'realm');
$server->addPlugin($authPlugin);
// Force login
$authPlugin->beforeMethod('BLA','foo');
$requestedProperties = array(
'{DAV:}acl-restrictions',
);
$returnedProperties = array(
200 => array(),
404 => array(),
);
$this->assertNull($plugin->beforeGetProperties('foo', $nodes[0], $requestedProperties, $returnedProperties));
$this->assertEquals(1,count($returnedProperties[200]),'The {DAV:}acl-restrictions property did not return from the list. Full list: ' . print_r($returnedProperties,true));
$this->assertArrayHasKey('{DAV:}acl-restrictions',$returnedProperties[200]);
$this->assertInstanceOf('Sabre_DAVACL_Property_ACLRestrictions', $returnedProperties[200]['{DAV:}acl-restrictions']);
}
function testAlternateUriSet() {
$tree = array(
new Sabre_DAV_SimpleCollection('principals', array(
$principal = new Sabre_DAVACL_MockPrincipal('user','principals/user'),
)),
);
$fakeServer = new Sabre_DAV_Server($tree);
//$plugin = new Sabre_DAV_Auth_Plugin(new Sabre_DAV_Auth_MockBackend(),'realm');
//$fakeServer->addPlugin($plugin);
$plugin = new Sabre_DAVACL_Plugin();
$fakeServer->addPlugin($plugin);
$requestedProperties = array(
'{DAV:}alternate-URI-set',
);
$returnedProperties = array();
$result = $plugin->beforeGetProperties('principals/user',$principal,$requestedProperties,$returnedProperties);
$this->assertNull($result);
$this->assertTrue(isset($returnedProperties[200]));
$this->assertTrue(isset($returnedProperties[200]['{DAV:}alternate-URI-set']));
$this->assertInstanceOf('Sabre_DAV_Property_HrefList', $returnedProperties[200]['{DAV:}alternate-URI-set']);
$this->assertEquals(array(), $returnedProperties[200]['{DAV:}alternate-URI-set']->getHrefs());
}
function testPrincipalURL() {
$tree = array(
new Sabre_DAV_SimpleCollection('principals', array(
$principal = new Sabre_DAVACL_MockPrincipal('user','principals/user'),
)),
);
$fakeServer = new Sabre_DAV_Server($tree);
//$plugin = new Sabre_DAV_Auth_Plugin(new Sabre_DAV_Auth_MockBackend(),'realm');
//$fakeServer->addPlugin($plugin);
$plugin = new Sabre_DAVACL_Plugin();
$fakeServer->addPlugin($plugin);
$requestedProperties = array(
'{DAV:}principal-URL',
);
$returnedProperties = array();
$result = $plugin->beforeGetProperties('principals/user',$principal,$requestedProperties,$returnedProperties);
$this->assertNull($result);
$this->assertTrue(isset($returnedProperties[200]));
$this->assertTrue(isset($returnedProperties[200]['{DAV:}principal-URL']));
$this->assertInstanceOf('Sabre_DAV_Property_Href', $returnedProperties[200]['{DAV:}principal-URL']);
$this->assertEquals('principals/user/', $returnedProperties[200]['{DAV:}principal-URL']->getHref());
}
function testGroupMemberSet() {
$tree = array(
new Sabre_DAV_SimpleCollection('principals', array(
$principal = new Sabre_DAVACL_MockPrincipal('user','principals/user'),
)),
);
$fakeServer = new Sabre_DAV_Server($tree);
//$plugin = new Sabre_DAV_Auth_Plugin(new Sabre_DAV_Auth_MockBackend(),'realm');
//$fakeServer->addPlugin($plugin);
$plugin = new Sabre_DAVACL_Plugin();
$fakeServer->addPlugin($plugin);
$requestedProperties = array(
'{DAV:}group-member-set',
);
$returnedProperties = array();
$result = $plugin->beforeGetProperties('principals/user',$principal,$requestedProperties,$returnedProperties);
$this->assertNull($result);
$this->assertTrue(isset($returnedProperties[200]));
$this->assertTrue(isset($returnedProperties[200]['{DAV:}group-member-set']));
$this->assertInstanceOf('Sabre_DAV_Property_HrefList', $returnedProperties[200]['{DAV:}group-member-set']);
$this->assertEquals(array(), $returnedProperties[200]['{DAV:}group-member-set']->getHrefs());
}
function testGroupMemberShip() {
$tree = array(
new Sabre_DAV_SimpleCollection('principals', array(
$principal = new Sabre_DAVACL_MockPrincipal('user','principals/user'),
)),
);
$fakeServer = new Sabre_DAV_Server($tree);
//$plugin = new Sabre_DAV_Auth_Plugin(new Sabre_DAV_Auth_MockBackend(),'realm');
//$fakeServer->addPlugin($plugin);
$plugin = new Sabre_DAVACL_Plugin();
$fakeServer->addPlugin($plugin);
$requestedProperties = array(
'{DAV:}group-membership',
);
$returnedProperties = array();
$result = $plugin->beforeGetProperties('principals/user',$principal,$requestedProperties,$returnedProperties);
$this->assertNull($result);
$this->assertTrue(isset($returnedProperties[200]));
$this->assertTrue(isset($returnedProperties[200]['{DAV:}group-membership']));
$this->assertInstanceOf('Sabre_DAV_Property_HrefList', $returnedProperties[200]['{DAV:}group-membership']);
$this->assertEquals(array(), $returnedProperties[200]['{DAV:}group-membership']->getHrefs());
}
function testGetDisplayName() {
$tree = array(
new Sabre_DAV_SimpleCollection('principals', array(
$principal = new Sabre_DAVACL_MockPrincipal('user','principals/user'),
)),
);
$fakeServer = new Sabre_DAV_Server($tree);
//$plugin = new Sabre_DAV_Auth_Plugin(new Sabre_DAV_Auth_MockBackend(),'realm');
//$fakeServer->addPlugin($plugin);
$plugin = new Sabre_DAVACL_Plugin();
$fakeServer->addPlugin($plugin);
$requestedProperties = array(
'{DAV:}displayname',
);
$returnedProperties = array();
$result = $plugin->beforeGetProperties('principals/user',$principal,$requestedProperties,$returnedProperties);
$this->assertNull($result);
$this->assertTrue(isset($returnedProperties[200]));
$this->assertTrue(isset($returnedProperties[200]['{DAV:}displayname']));
$this->assertEquals('user', $returnedProperties[200]['{DAV:}displayname']);
}
}

View file

@ -0,0 +1,121 @@
<?php
require_once 'Sabre/DAVACL/MockPrincipal.php';
class Sabre_DAVACL_PluginUpdatePropertiesTest extends PHPUnit_Framework_TestCase {
public function testUpdatePropertiesPassthrough() {
$tree = array(
new Sabre_DAV_SimpleCollection('foo'),
);
$server = new Sabre_DAV_Server($tree);
$server->addPlugin(new Sabre_DAVACL_Plugin());
$result = $server->updateProperties('foo', array(
'{DAV:}foo' => 'bar',
));
$expected = array(
'href' => 'foo',
'403' => array(
'{DAV:}foo' => null,
),
);
$this->assertEquals($expected, $result);
}
public function testRemoveGroupMembers() {
$tree = array(
new Sabre_DAVACL_MockPrincipal('foo','foo'),
);
$server = new Sabre_DAV_Server($tree);
$server->addPlugin(new Sabre_DAVACL_Plugin());
$result = $server->updateProperties('foo', array(
'{DAV:}group-member-set' => null,
));
$expected = array(
'href' => 'foo',
'200' => array(
'{DAV:}group-member-set' => null,
),
);
$this->assertEquals($expected, $result);
$this->assertEquals(array(),$tree[0]->getGroupMemberSet());
}
public function testSetGroupMembers() {
$tree = array(
new Sabre_DAVACL_MockPrincipal('foo','foo'),
);
$server = new Sabre_DAV_Server($tree);
$server->addPlugin(new Sabre_DAVACL_Plugin());
$result = $server->updateProperties('foo', array(
'{DAV:}group-member-set' => new Sabre_DAV_Property_HrefList(array('bar','baz')),
));
$expected = array(
'href' => 'foo',
'200' => array(
'{DAV:}group-member-set' => null,
),
);
$this->assertEquals($expected, $result);
$this->assertEquals(array('bar','baz'),$tree[0]->getGroupMemberSet());
}
/**
* @expectedException sabre_DAV_Exception
*/
public function testSetBadValue() {
$tree = array(
new Sabre_DAVACL_MockPrincipal('foo','foo'),
);
$server = new Sabre_DAV_Server($tree);
$server->addPlugin(new Sabre_DAVACL_Plugin());
$result = $server->updateProperties('foo', array(
'{DAV:}group-member-set' => new StdClass(),
));
}
public function testSetBadNode() {
$tree = array(
new Sabre_DAV_SimpleCollection('foo'),
);
$server = new Sabre_DAV_Server($tree);
$server->addPlugin(new Sabre_DAVACL_Plugin());
$result = $server->updateProperties('foo', array(
'{DAV:}group-member-set' => new Sabre_DAV_Property_HrefList(array('bar','baz')),
'{DAV:}bar' => 'baz',
));
$expected = array(
'href' => 'foo',
'403' => array(
'{DAV:}group-member-set' => null,
),
'424' => array(
'{DAV:}bar' => null,
),
);
$this->assertEquals($expected, $result);
}
}

View file

@ -0,0 +1,172 @@
<?php
abstract class Sabre_DAVACL_PrincipalBackend_AbstractPDOTest extends PHPUnit_Framework_TestCase {
abstract function getPDO();
function testConstruct() {
$pdo = $this->getPDO();
$backend = new Sabre_DAVACL_PrincipalBackend_PDO($pdo);
$this->assertTrue($backend instanceof Sabre_DAVACL_PrincipalBackend_PDO);
}
/**
* @depends testConstruct
*/
function testGetPrincipalsByPrefix() {
$pdo = $this->getPDO();
$backend = new Sabre_DAVACL_PrincipalBackend_PDO($pdo);
$expected = array(
array(
'uri' => 'principals/user',
'{http://sabredav.org/ns}email-address' => 'user@example.org',
'{DAV:}displayname' => 'User',
),
array(
'uri' => 'principals/group',
'{http://sabredav.org/ns}email-address' => 'group@example.org',
'{DAV:}displayname' => 'Group',
),
);
$this->assertEquals($expected, $backend->getPrincipalsByPrefix('principals'));
$this->assertEquals(array(), $backend->getPrincipalsByPrefix('foo'));
}
/**
* @depends testConstruct
*/
function testGetPrincipalByPath() {
$pdo = $this->getPDO();
$backend = new Sabre_DAVACL_PrincipalBackend_PDO($pdo);
$expected = array(
'id' => 1,
'uri' => 'principals/user',
'{http://sabredav.org/ns}email-address' => 'user@example.org',
'{DAV:}displayname' => 'User',
);
$this->assertEquals($expected, $backend->getPrincipalByPath('principals/user'));
$this->assertEquals(null, $backend->getPrincipalByPath('foo'));
}
function testGetGroupMemberSet() {
$pdo = $this->getPDO();
$backend = new Sabre_DAVACL_PrincipalBackend_PDO($pdo);
$expected = array('principals/user');
$this->assertEquals($expected,$backend->getGroupMemberSet('principals/group'));
}
function testGetGroupMembership() {
$pdo = $this->getPDO();
$backend = new Sabre_DAVACL_PrincipalBackend_PDO($pdo);
$expected = array('principals/group');
$this->assertEquals($expected,$backend->getGroupMembership('principals/user'));
}
function testSetGroupMemberSet() {
$pdo = $this->getPDO();
// Start situation
$backend = new Sabre_DAVACL_PrincipalBackend_PDO($pdo);
$this->assertEquals(array('principals/user'), $backend->getGroupMemberSet('principals/group'));
// Removing all principals
$backend->setGroupMemberSet('principals/group', array());
$this->assertEquals(array(), $backend->getGroupMemberSet('principals/group'));
// Adding principals again
$backend->setGroupMemberSet('principals/group', array('principals/user'));
$this->assertEquals(array('principals/user'), $backend->getGroupMemberSet('principals/group'));
}
function testSearchPrincipals() {
$pdo = $this->getPDO();
$backend = new Sabre_DAVACL_PrincipalBackend_PDO($pdo);
$result = $backend->searchPrincipals('principals', array('{DAV:}blabla' => 'foo'));
$this->assertEquals(array(), $result);
$result = $backend->searchPrincipals('principals', array('{DAV:}displayname' => 'ou'));
$this->assertEquals(array('principals/group'), $result);
$result = $backend->searchPrincipals('principals', array('{DAV:}displayname' => 'UsEr', '{http://sabredav.org/ns}email-address' => 'USER@EXAMPLE'));
$this->assertEquals(array('principals/user'), $result);
$result = $backend->searchPrincipals('mom', array('{DAV:}displayname' => 'UsEr', '{http://sabredav.org/ns}email-address' => 'USER@EXAMPLE'));
$this->assertEquals(array(), $result);
}
function testUpdatePrincipal() {
$pdo = $this->getPDO();
$backend = new Sabre_DAVACL_PrincipalBackend_PDO($pdo);
$result = $backend->updatePrincipal('principals/user', array(
'{DAV:}displayname' => 'pietje',
'{http://sabredav.org/ns}vcard-url' => 'blabla',
));
$this->assertTrue($result);
$this->assertEquals(array(
'id' => 1,
'uri' => 'principals/user',
'{DAV:}displayname' => 'pietje',
'{http://sabredav.org/ns}vcard-url' => 'blabla',
'{http://sabredav.org/ns}email-address' => 'user@example.org',
), $backend->getPrincipalByPath('principals/user'));
}
function testUpdatePrincipalUnknownField() {
$pdo = $this->getPDO();
$backend = new Sabre_DAVACL_PrincipalBackend_PDO($pdo);
$result = $backend->updatePrincipal('principals/user', array(
'{DAV:}displayname' => 'pietje',
'{http://sabredav.org/ns}vcard-url' => 'blabla',
'{DAV:}unknown' => 'foo',
));
$this->assertEquals(array(
424 => array(
'{DAV:}displayname' => null,
'{http://sabredav.org/ns}vcard-url' => null,
),
403 => array(
'{DAV:}unknown' => null,
),
), $result);
$this->assertEquals(array(
'id' => '1',
'uri' => 'principals/user',
'{DAV:}displayname' => 'User',
'{http://sabredav.org/ns}email-address' => 'user@example.org',
), $backend->getPrincipalByPath('principals/user'));
}
}

View file

@ -0,0 +1,39 @@
<?php
require_once 'Sabre/TestUtil.php';
class Sabre_DAVACL_PrincipalBackend_PDOMySQLTest extends Sabre_DAVACL_PrincipalBackend_AbstractPDOTest {
function getPDO() {
if (!SABRE_HASMYSQL) $this->markTestSkipped('MySQL driver is not available, or not properly configured');
$pdo = Sabre_TestUtil::getMySQLDB();
if (!$pdo) $this->markTestSkipped('Could not connect to MySQL database');
$pdo->query("DROP TABLE IF EXISTS principals");
$pdo->query("
create table principals (
id integer unsigned not null primary key auto_increment,
uri varchar(50),
email varchar(80),
displayname VARCHAR(80),
vcardurl VARCHAR(80),
unique(uri)
);");
$pdo->query("INSERT INTO principals (uri,email,displayname) VALUES ('principals/user','user@example.org','User')");
$pdo->query("INSERT INTO principals (uri,email,displayname) VALUES ('principals/group','group@example.org','Group')");
$pdo->query("DROP TABLE IF EXISTS groupmembers");
$pdo->query("CREATE TABLE groupmembers (
id INTEGER UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
principal_id INTEGER UNSIGNED NOT NULL,
member_id INTEGER UNSIGNED NOT NULL,
UNIQUE(principal_id, member_id)
);");
$pdo->query("INSERT INTO groupmembers (principal_id,member_id) VALUES (2,1)");
return $pdo;
}
}

View file

@ -0,0 +1,36 @@
<?php
require_once 'Sabre/DAV/Auth/Backend/AbstractPDOTest.php';
class Sabre_DAVACL_PrincipalBackend_PDOSQLiteTest extends Sabre_DAVACL_PrincipalBackend_AbstractPDOTest {
function tearDown() {
if (file_exists(SABRE_TEMPDIR . '/pdobackend')) unlink(SABRE_TEMPDIR . '/pdobackend');
if (file_exists(SABRE_TEMPDIR . '/pdobackend2')) unlink(SABRE_TEMPDIR . '/pdobackend2');
}
function getPDO() {
if (!SABRE_HASSQLITE) $this->markTestSkipped('SQLite driver is not available');
$pdo = new PDO('sqlite:'.SABRE_TEMPDIR.'/pdobackend');
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$pdo->query('CREATE TABLE principals (id INTEGER PRIMARY KEY ASC, uri TEXT, email VARCHAR(80), displayname VARCHAR(80), vcardurl VARCHAR(80))');
$pdo->query('INSERT INTO principals VALUES (1, "principals/user","user@example.org","User",null)');
$pdo->query('INSERT INTO principals VALUES (2, "principals/group","group@example.org","Group",null)');
$pdo->query("CREATE TABLE groupmembers (
id INTEGER PRIMARY KEY ASC,
principal_id INT,
member_id INT,
UNIQUE(principal_id, member_id)
);");
$pdo->query("INSERT INTO groupmembers (principal_id,member_id) VALUES (2,1)");
return $pdo;
}
}

View file

@ -0,0 +1,48 @@
<?php
require_once 'Sabre/DAVACL/MockPrincipalBackend.php';
class Sabre_DAVACL_PrincipalCollectionTest extends PHPUnit_Framework_TestCase {
public function testBasic() {
$backend = new Sabre_DAVACL_MockPrincipalBackend();
$pc = new Sabre_DAVACL_PrincipalCollection($backend);
$this->assertTrue($pc instanceof Sabre_DAVACL_PrincipalCollection);
$this->assertEquals('principals',$pc->getName());
}
/**
* @depends testBasic
*/
public function testGetChildren() {
$backend = new Sabre_DAVACL_MockPrincipalBackend();
$pc = new Sabre_DAVACL_PrincipalCollection($backend);
$children = $pc->getChildren();
$this->assertTrue(is_array($children));
foreach($children as $child) {
$this->assertTrue($child instanceof Sabre_DAVACL_IPrincipal);
}
}
/**
* @depends testBasic
* @expectedException Sabre_DAV_Exception_MethodNotAllowed
*/
public function testGetChildrenDisable() {
$backend = new Sabre_DAVACL_MockPrincipalBackend();
$pc = new Sabre_DAVACL_PrincipalCollection($backend);
$pc->disableListing = true;
$children = $pc->getChildren();
}
}

View file

@ -0,0 +1,240 @@
<?php
require_once 'Sabre/HTTP/ResponseMock.php';
require_once 'Sabre/DAV/Auth/MockBackend.php';
require_once 'Sabre/DAVACL/MockPrincipalBackend.php';
class Sabre_DAVACL_PrincipalPropertySearchTest extends PHPUnit_Framework_TestCase {
function getServer() {
$backend = new Sabre_DAVACL_MockPrincipalBackend();
$dir = new Sabre_DAV_SimpleCollection('root');
$principals = new Sabre_DAVACL_PrincipalCollection($backend);
$dir->addChild($principals);
$fakeServer = new Sabre_DAV_Server(new Sabre_DAV_ObjectTree($dir));
$fakeServer->httpResponse = new Sabre_HTTP_ResponseMock();
$fakeServer->debugExceptions = true;
$plugin = new Sabre_DAVACL_MockPlugin($backend,'realm');
$plugin->allowAccessToNodesWithoutACL = true;
$this->assertTrue($plugin instanceof Sabre_DAVACL_Plugin);
$fakeServer->addPlugin($plugin);
$this->assertEquals($plugin, $fakeServer->getPlugin('acl'));
return $fakeServer;
}
function testDepth1() {
$xml = '<?xml version="1.0"?>
<d:principal-property-search xmlns:d="DAV:">
<d:property-search>
<d:prop>
<d:displayname />
</d:prop>
<d:match>user</d:match>
</d:property-search>
<d:prop>
<d:displayname />
<d:getcontentlength />
</d:prop>
</d:principal-property-search>';
$serverVars = array(
'REQUEST_METHOD' => 'REPORT',
'HTTP_DEPTH' => '1',
'REQUEST_URI' => '/principals',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody($xml);
$server = $this->getServer();
$server->httpRequest = $request;
$server->exec();
$this->assertEquals('HTTP/1.1 400 Bad request', $server->httpResponse->status);
$this->assertEquals(array(
'Content-Type' => 'application/xml; charset=utf-8',
), $server->httpResponse->headers);
}
function testUnknownSearchField() {
$xml = '<?xml version="1.0"?>
<d:principal-property-search xmlns:d="DAV:">
<d:property-search>
<d:prop>
<d:yourmom />
</d:prop>
<d:match>user</d:match>
</d:property-search>
<d:prop>
<d:displayname />
<d:getcontentlength />
</d:prop>
</d:principal-property-search>';
$serverVars = array(
'REQUEST_METHOD' => 'REPORT',
'HTTP_DEPTH' => '0',
'REQUEST_URI' => '/principals',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody($xml);
$server = $this->getServer();
$server->httpRequest = $request;
$server->exec();
$this->assertEquals('HTTP/1.1 207 Multi-Status', $server->httpResponse->status);
$this->assertEquals(array(
'Content-Type' => 'application/xml; charset=utf-8',
), $server->httpResponse->headers);
}
function testCorrect() {
$xml = '<?xml version="1.0"?>
<d:principal-property-search xmlns:d="DAV:">
<d:apply-to-principal-collection-set />
<d:property-search>
<d:prop>
<d:displayname />
</d:prop>
<d:match>user</d:match>
</d:property-search>
<d:prop>
<d:displayname />
<d:getcontentlength />
</d:prop>
</d:principal-property-search>';
$serverVars = array(
'REQUEST_METHOD' => 'REPORT',
'HTTP_DEPTH' => '0',
'REQUEST_URI' => '/',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody($xml);
$server = $this->getServer();
$server->httpRequest = $request;
$server->exec();
$this->assertEquals('HTTP/1.1 207 Multi-Status', $server->httpResponse->status, $server->httpResponse->body);
$this->assertEquals(array(
'Content-Type' => 'application/xml; charset=utf-8',
), $server->httpResponse->headers);
$check = array(
'/d:multistatus',
'/d:multistatus/d:response' => 2,
'/d:multistatus/d:response/d:href' => 2,
'/d:multistatus/d:response/d:propstat' => 4,
'/d:multistatus/d:response/d:propstat/d:prop' => 4,
'/d:multistatus/d:response/d:propstat/d:prop/d:displayname' => 2,
'/d:multistatus/d:response/d:propstat/d:prop/d:getcontentlength' => 2,
'/d:multistatus/d:response/d:propstat/d:status' => 4,
);
$xml = simplexml_load_string($server->httpResponse->body);
$xml->registerXPathNamespace('d','DAV:');
foreach($check as $v1=>$v2) {
$xpath = is_int($v1)?$v2:$v1;
$result = $xml->xpath($xpath);
$count = 1;
if (!is_int($v1)) $count = $v2;
$this->assertEquals($count,count($result), 'we expected ' . $count . ' appearances of ' . $xpath . ' . We found ' . count($result) . '. Full response body: ' . $server->httpResponse->body);
}
}
function testWrongUri() {
$xml = '<?xml version="1.0"?>
<d:principal-property-search xmlns:d="DAV:">
<d:property-search>
<d:prop>
<d:displayname />
</d:prop>
<d:match>user</d:match>
</d:property-search>
<d:prop>
<d:displayname />
<d:getcontentlength />
</d:prop>
</d:principal-property-search>';
$serverVars = array(
'REQUEST_METHOD' => 'REPORT',
'HTTP_DEPTH' => '0',
'REQUEST_URI' => '/',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody($xml);
$server = $this->getServer();
$server->httpRequest = $request;
$server->exec();
$this->assertEquals('HTTP/1.1 207 Multi-Status', $server->httpResponse->status, $server->httpResponse->body);
$this->assertEquals(array(
'Content-Type' => 'application/xml; charset=utf-8',
), $server->httpResponse->headers);
$check = array(
'/d:multistatus',
'/d:multistatus/d:response' => 0,
);
$xml = simplexml_load_string($server->httpResponse->body);
$xml->registerXPathNamespace('d','DAV:');
foreach($check as $v1=>$v2) {
$xpath = is_int($v1)?$v2:$v1;
$result = $xml->xpath($xpath);
$count = 1;
if (!is_int($v1)) $count = $v2;
$this->assertEquals($count,count($result), 'we expected ' . $count . ' appearances of ' . $xpath . ' . We found ' . count($result) . '. Full response body: ' . $server->httpResponse->body);
}
}
}
class Sabre_DAVACL_MockPlugin extends Sabre_DAVACL_Plugin {
function getCurrentUserPrivilegeSet($node) {
return array(
'{DAV:}read',
'{DAV:}write',
);
}
}

View file

@ -0,0 +1,131 @@
<?php
require_once 'Sabre/HTTP/ResponseMock.php';
require_once 'Sabre/DAVACL/MockPrincipalBackend.php';
class Sabre_DAVACL_PrincipalSearchPropertySetTest extends PHPUnit_Framework_TestCase {
function getServer() {
$backend = new Sabre_DAVACL_MockPrincipalBackend();
$dir = new Sabre_DAV_SimpleCollection('root');
$principals = new Sabre_DAVACL_PrincipalCollection($backend);
$dir->addChild($principals);
$fakeServer = new Sabre_DAV_Server(new Sabre_DAV_ObjectTree($dir));
$fakeServer->httpResponse = new Sabre_HTTP_ResponseMock();
$plugin = new Sabre_DAVACL_Plugin($backend,'realm');
$this->assertTrue($plugin instanceof Sabre_DAVACL_Plugin);
$fakeServer->addPlugin($plugin);
$this->assertEquals($plugin, $fakeServer->getPlugin('acl'));
return $fakeServer;
}
function testDepth1() {
$xml = '<?xml version="1.0"?>
<d:principal-search-property-set xmlns:d="DAV:" />';
$serverVars = array(
'REQUEST_METHOD' => 'REPORT',
'HTTP_DEPTH' => '1',
'REQUEST_URI' => '/principals',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody($xml);
$server = $this->getServer();
$server->httpRequest = $request;
$server->exec();
$this->assertEquals('HTTP/1.1 400 Bad request', $server->httpResponse->status);
$this->assertEquals(array(
'Content-Type' => 'application/xml; charset=utf-8',
), $server->httpResponse->headers);
}
function testDepthIncorrectXML() {
$xml = '<?xml version="1.0"?>
<d:principal-search-property-set xmlns:d="DAV:"><d:ohell /></d:principal-search-property-set>';
$serverVars = array(
'REQUEST_METHOD' => 'REPORT',
'HTTP_DEPTH' => '0',
'REQUEST_URI' => '/principals',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody($xml);
$server = $this->getServer();
$server->httpRequest = $request;
$server->exec();
$this->assertEquals('HTTP/1.1 400 Bad request', $server->httpResponse->status, $server->httpResponse->body);
$this->assertEquals(array(
'Content-Type' => 'application/xml; charset=utf-8',
), $server->httpResponse->headers);
}
function testCorrect() {
$xml = '<?xml version="1.0"?>
<d:principal-search-property-set xmlns:d="DAV:"/>';
$serverVars = array(
'REQUEST_METHOD' => 'REPORT',
'HTTP_DEPTH' => '0',
'REQUEST_URI' => '/principals',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody($xml);
$server = $this->getServer();
$server->httpRequest = $request;
$server->exec();
$this->assertEquals('HTTP/1.1 200 OK', $server->httpResponse->status, $server->httpResponse->body);
$this->assertEquals(array(
'Content-Type' => 'application/xml; charset=utf-8',
), $server->httpResponse->headers);
$check = array(
'/d:principal-search-property-set',
'/d:principal-search-property-set/d:principal-search-property' => 2,
'/d:principal-search-property-set/d:principal-search-property/d:prop' => 2,
'/d:principal-search-property-set/d:principal-search-property/d:prop/d:displayname' => 1,
'/d:principal-search-property-set/d:principal-search-property/d:prop/s:email-address' => 1,
'/d:principal-search-property-set/d:principal-search-property/d:description' => 2,
);
$xml = simplexml_load_string($server->httpResponse->body);
$xml->registerXPathNamespace('d','DAV:');
$xml->registerXPathNamespace('s','http://sabredav.org/ns');
foreach($check as $v1=>$v2) {
$xpath = is_int($v1)?$v2:$v1;
$result = $xml->xpath($xpath);
$count = 1;
if (!is_int($v1)) $count = $v2;
$this->assertEquals($count,count($result), 'we expected ' . $count . ' appearances of ' . $xpath . ' . We found ' . count($result) . '. Full response body: ' . $server->httpResponse->body);
}
}
}

View file

@ -0,0 +1,201 @@
<?php
require_once 'Sabre/DAVACL/MockPrincipalBackend.php';
class Sabre_DAVACL_PrincipalTest extends PHPUnit_Framework_TestCase {
public function testConstruct() {
$principalBackend = new Sabre_DAVACL_MockPrincipalBackend();
$principal = new Sabre_DAVACL_Principal($principalBackend, array('uri' => 'principals/admin'));
$this->assertTrue($principal instanceof Sabre_DAVACL_Principal);
}
/**
* @expectedException Sabre_DAV_Exception
*/
public function testConstructNoUri() {
$principalBackend = new Sabre_DAVACL_MockPrincipalBackend();
$principal = new Sabre_DAVACL_Principal($principalBackend, array());
}
public function testGetName() {
$principalBackend = new Sabre_DAVACL_MockPrincipalBackend();
$principal = new Sabre_DAVACL_Principal($principalBackend, array('uri' => 'principals/admin'));
$this->assertEquals('admin',$principal->getName());
}
public function testGetDisplayName() {
$principalBackend = new Sabre_DAVACL_MockPrincipalBackend();
$principal = new Sabre_DAVACL_Principal($principalBackend, array('uri' => 'principals/admin'));
$this->assertEquals('admin',$principal->getDisplayname());
$principal = new Sabre_DAVACL_Principal($principalBackend, array(
'uri' => 'principals/admin',
'{DAV:}displayname' => 'Mr. Admin'
));
$this->assertEquals('Mr. Admin',$principal->getDisplayname());
}
public function testGetProperties() {
$principalBackend = new Sabre_DAVACL_MockPrincipalBackend();
$principal = new Sabre_DAVACL_Principal($principalBackend, array(
'uri' => 'principals/admin',
'{DAV:}displayname' => 'Mr. Admin',
'{http://www.example.org/custom}custom' => 'Custom',
'{http://sabredav.org/ns}email-address' => 'admin@example.org',
));
$keys = array(
'{DAV:}displayname',
'{http://www.example.org/custom}custom',
'{http://sabredav.org/ns}email-address',
);
$props = $principal->getProperties($keys);
foreach($keys as $key) $this->assertArrayHasKey($key,$props);
$this->assertEquals('Mr. Admin',$props['{DAV:}displayname']);
$this->assertEquals('admin@example.org', $props['{http://sabredav.org/ns}email-address']);
}
public function testUpdateProperties() {
$principalBackend = new Sabre_DAVACL_MockPrincipalBackend();
$principal = new Sabre_DAVACL_Principal($principalBackend, array('uri' => 'principals/admin'));
$result = $principal->updateProperties(array('{DAV:}yourmom'=>'test'));
$this->assertEquals(true,$result);
}
public function testGetPrincipalUrl() {
$principalBackend = new Sabre_DAVACL_MockPrincipalBackend();
$principal = new Sabre_DAVACL_Principal($principalBackend, array('uri' => 'principals/admin'));
$this->assertEquals('principals/admin',$principal->getPrincipalUrl());
}
public function testGetAlternateUriSet() {
$principalBackend = new Sabre_DAVACL_MockPrincipalBackend();
$principal = new Sabre_DAVACL_Principal($principalBackend, array(
'uri' => 'principals/admin',
'{DAV:}displayname' => 'Mr. Admin',
'{http://www.example.org/custom}custom' => 'Custom',
'{http://sabredav.org/ns}email-address' => 'admin@example.org',
'{DAV:}alternate-URI-set' => array(
'mailto:admin+1@example.org',
'mailto:admin+2@example.org',
'mailto:admin@example.org',
),
));
$expected = array(
'mailto:admin+1@example.org',
'mailto:admin+2@example.org',
'mailto:admin@example.org',
);
$this->assertEquals($expected,$principal->getAlternateUriSet());
}
public function testGetAlternateUriSetEmpty() {
$principalBackend = new Sabre_DAVACL_MockPrincipalBackend();
$principal = new Sabre_DAVACL_Principal($principalBackend, array(
'uri' => 'principals/admin',
));
$expected = array();
$this->assertEquals($expected,$principal->getAlternateUriSet());
}
public function testGetGroupMemberSet() {
$principalBackend = new Sabre_DAVACL_MockPrincipalBackend();
$principal = new Sabre_DAVACL_Principal($principalBackend, array('uri' => 'principals/admin'));
$this->assertEquals(array(),$principal->getGroupMemberSet());
}
public function testGetGroupMembership() {
$principalBackend = new Sabre_DAVACL_MockPrincipalBackend();
$principal = new Sabre_DAVACL_Principal($principalBackend, array('uri' => 'principals/admin'));
$this->assertEquals(array(),$principal->getGroupMembership());
}
public function testSetGroupMemberSet() {
$principalBackend = new Sabre_DAVACL_MockPrincipalBackend();
$principal = new Sabre_DAVACL_Principal($principalBackend, array('uri' => 'principals/admin'));
$principal->setGroupMemberSet(array('principals/foo'));
$this->assertEquals(array(
'principals/admin' => array('principals/foo'),
), $principalBackend->groupMembers);
}
public function testGetOwner() {
$principalBackend = new Sabre_DAVACL_MockPrincipalBackend();
$principal = new Sabre_DAVACL_Principal($principalBackend, array('uri' => 'principals/admin'));
$this->assertEquals('principals/admin',$principal->getOwner());
}
public function testGetGroup() {
$principalBackend = new Sabre_DAVACL_MockPrincipalBackend();
$principal = new Sabre_DAVACL_Principal($principalBackend, array('uri' => 'principals/admin'));
$this->assertNull($principal->getGroup());
}
public function testGetACl() {
$principalBackend = new Sabre_DAVACL_MockPrincipalBackend();
$principal = new Sabre_DAVACL_Principal($principalBackend, array('uri' => 'principals/admin'));
$this->assertEquals(array(
array(
'privilege' => '{DAV:}read',
'principal' => 'principals/admin',
'protected' => true,
)
),$principal->getACL());
}
/**
* @expectedException Sabre_DAV_Exception_MethodNotAllowed
*/
public function testSetACl() {
$principalBackend = new Sabre_DAVACL_MockPrincipalBackend();
$principal = new Sabre_DAVACL_Principal($principalBackend, array('uri' => 'principals/admin'));
$principal->setACL(array());
}
public function testGetSupportedPrivilegeSet() {
$principalBackend = new Sabre_DAVACL_MockPrincipalBackend();
$principal = new Sabre_DAVACL_Principal($principalBackend, array('uri' => 'principals/admin'));
$this->assertNull($principal->getSupportedPrivilegeSet());
}
}

View file

@ -0,0 +1,30 @@
<?php
class Sabre_DAVACL_Property_ACLRestrictionsTest extends PHPUnit_Framework_TestCase {
function testConstruct() {
$prop = new Sabre_DAVACL_Property_AclRestrictions();
}
function testSerializeEmpty() {
$dom = new DOMDocument('1.0');
$root = $dom->createElementNS('DAV:','d:root');
$dom->appendChild($root);
$acl = new Sabre_DAVACL_Property_AclRestrictions();
$acl->serialize(new Sabre_DAV_Server(), $root);
$xml = $dom->saveXML();
$expected = '<?xml version="1.0"?>
<d:root xmlns:d="DAV:"><d:grant-only/><d:no-invert/></d:root>
';
$this->assertEquals($expected, $xml);
}
}

View file

@ -0,0 +1,329 @@
<?php
class Sabre_DAVACL_Property_ACLTest extends PHPUnit_Framework_TestCase {
function testConstruct() {
$acl = new Sabre_DAVACL_Property_Acl(array());
}
function testSerializeEmpty() {
$dom = new DOMDocument('1.0');
$root = $dom->createElementNS('DAV:','d:root');
$dom->appendChild($root);
$acl = new Sabre_DAVACL_Property_Acl(array());
$acl->serialize(new Sabre_DAV_Server(), $root);
$xml = $dom->saveXML();
$expected = '<?xml version="1.0"?>
<d:root xmlns:d="DAV:"/>
';
$this->assertEquals($expected, $xml);
}
function testSerialize() {
$dom = new DOMDocument('1.0');
$root = $dom->createElementNS('DAV:','d:root');
$dom->appendChild($root);
$privileges = array(
array(
'principal' => 'principals/evert',
'privilege' => '{DAV:}write',
'uri' => 'articles',
),
array(
'principal' => 'principals/foo',
'privilege' => '{DAV:}read',
'uri' => 'articles',
'protected' => true,
),
);
$acl = new Sabre_DAVACL_Property_Acl($privileges);
$acl->serialize(new Sabre_DAV_Server(), $root);
$dom->formatOutput = true;
$xml = $dom->saveXML();
$expected = '<?xml version="1.0"?>
<d:root xmlns:d="DAV:">
<d:ace>
<d:principal>
<d:href>/principals/evert/</d:href>
</d:principal>
<d:grant>
<d:privilege>
<d:write/>
</d:privilege>
</d:grant>
</d:ace>
<d:ace>
<d:principal>
<d:href>/principals/foo/</d:href>
</d:principal>
<d:grant>
<d:privilege>
<d:read/>
</d:privilege>
</d:grant>
<d:protected/>
</d:ace>
</d:root>
';
$this->assertEquals($expected, $xml);
}
function testSerializeSpecialPrincipals() {
$dom = new DOMDocument('1.0');
$root = $dom->createElementNS('DAV:','d:root');
$dom->appendChild($root);
$privileges = array(
array(
'principal' => '{DAV:}authenticated',
'privilege' => '{DAV:}write',
'uri' => 'articles',
),
array(
'principal' => '{DAV:}unauthenticated',
'privilege' => '{DAV:}write',
'uri' => 'articles',
),
array(
'principal' => '{DAV:}all',
'privilege' => '{DAV:}write',
'uri' => 'articles',
),
);
$acl = new Sabre_DAVACL_Property_Acl($privileges);
$acl->serialize(new Sabre_DAV_Server(), $root);
$dom->formatOutput = true;
$xml = $dom->saveXML();
$expected = '<?xml version="1.0"?>
<d:root xmlns:d="DAV:">
<d:ace>
<d:principal>
<d:authenticated/>
</d:principal>
<d:grant>
<d:privilege>
<d:write/>
</d:privilege>
</d:grant>
</d:ace>
<d:ace>
<d:principal>
<d:unauthenticated/>
</d:principal>
<d:grant>
<d:privilege>
<d:write/>
</d:privilege>
</d:grant>
</d:ace>
<d:ace>
<d:principal>
<d:all/>
</d:principal>
<d:grant>
<d:privilege>
<d:write/>
</d:privilege>
</d:grant>
</d:ace>
</d:root>
';
$this->assertEquals($expected, $xml);
}
function testUnserialize() {
$source = '<?xml version="1.0"?>
<d:root xmlns:d="DAV:">
<d:ace>
<d:principal>
<d:href>/principals/evert/</d:href>
</d:principal>
<d:grant>
<d:privilege>
<d:write/>
</d:privilege>
</d:grant>
</d:ace>
<d:ace>
<d:principal>
<d:href>/principals/foo/</d:href>
</d:principal>
<d:grant>
<d:privilege>
<d:read/>
</d:privilege>
</d:grant>
<d:protected/>
</d:ace>
</d:root>
';
$dom = Sabre_DAV_XMLUtil::loadDOMDocument($source);
$result = Sabre_DAVACL_Property_Acl::unserialize($dom->firstChild);
$this->assertInstanceOf('Sabre_DAVACL_Property_Acl', $result);
$expected = array(
array(
'principal' => '/principals/evert/',
'protected' => false,
'privilege' => '{DAV:}write',
),
array(
'principal' => '/principals/foo/',
'protected' => true,
'privilege' => '{DAV:}read',
),
);
$this->assertEquals($expected, $result->getPrivileges());
}
/**
* @expectedException Sabre_DAV_Exception_BadRequest
*/
function testUnserializeNoPrincipal() {
$source = '<?xml version="1.0"?>
<d:root xmlns:d="DAV:">
<d:ace>
<d:grant>
<d:privilege>
<d:write/>
</d:privilege>
</d:grant>
</d:ace>
</d:root>
';
$dom = Sabre_DAV_XMLUtil::loadDOMDocument($source);
Sabre_DAVACL_Property_Acl::unserialize($dom->firstChild);
}
function testUnserializeOtherPrincipal() {
$source = '<?xml version="1.0"?>
<d:root xmlns:d="DAV:">
<d:ace>
<d:grant>
<d:privilege>
<d:write/>
</d:privilege>
</d:grant>
<d:principal><d:authenticated /></d:principal>
</d:ace>
<d:ace>
<d:grant>
<d:privilege>
<d:write/>
</d:privilege>
</d:grant>
<d:principal><d:unauthenticated /></d:principal>
</d:ace>
<d:ace>
<d:grant>
<d:privilege>
<d:write/>
</d:privilege>
</d:grant>
<d:principal><d:all /></d:principal>
</d:ace>
</d:root>
';
$dom = Sabre_DAV_XMLUtil::loadDOMDocument($source);
$result = Sabre_DAVACL_Property_Acl::unserialize($dom->firstChild);
$this->assertInstanceOf('Sabre_DAVACL_Property_Acl', $result);
$expected = array(
array(
'principal' => '{DAV:}authenticated',
'protected' => false,
'privilege' => '{DAV:}write',
),
array(
'principal' => '{DAV:}unauthenticated',
'protected' => false,
'privilege' => '{DAV:}write',
),
array(
'principal' => '{DAV:}all',
'protected' => false,
'privilege' => '{DAV:}write',
),
);
$this->assertEquals($expected, $result->getPrivileges());
}
/**
* @expectedException Sabre_DAV_Exception_NotImplemented
*/
function testUnserializeDeny() {
$source = '<?xml version="1.0"?>
<d:root xmlns:d="DAV:">
<d:ace>
<d:deny>
<d:privilege>
<d:write/>
</d:privilege>
</d:deny>
<d:principal><d:href>/principals/evert</d:href></d:principal>
</d:ace>
</d:root>
';
$dom = Sabre_DAV_XMLUtil::loadDOMDocument($source);
Sabre_DAVACL_Property_Acl::unserialize($dom->firstChild);
}
/**
* @expectedException Sabre_DAV_Exception_BadRequest
*/
function testUnserializeMissingPriv() {
$source = '<?xml version="1.0"?>
<d:root xmlns:d="DAV:">
<d:ace>
<d:grant>
<d:privilege />
</d:grant>
<d:principal><d:href>/principals/evert</d:href></d:principal>
</d:ace>
</d:root>
';
$dom = Sabre_DAV_XMLUtil::loadDOMDocument($source);
Sabre_DAVACL_Property_Acl::unserialize($dom->firstChild);
}
}

View file

@ -0,0 +1,41 @@
<?php
class Sabre_DAVACL_Property_CurrentUserPrivilegeSetTest extends PHPUnit_Framework_TestCase {
function testSerialize() {
$privileges = array(
'{DAV:}read',
'{DAV:}write',
);
$prop = new Sabre_DAVACL_Property_CurrentUserPrivilegeSet($privileges);
$server = new Sabre_DAV_Server();
$dom = new DOMDocument('1.0','utf-8');
$root = $dom->createElementNS('DAV:','d:root');
$dom->appendChild($root);
$prop->serialize($server, $root);
$xpaths = array(
'/d:root' => 1,
'/d:root/d:privilege' => 2,
'/d:root/d:privilege/d:read' => 1,
'/d:root/d:privilege/d:write' => 1,
);
// Reloading because PHP DOM sucks
$dom2 = new DOMDocument('1.0', 'utf-8');
$dom2->loadXML($dom->saveXML());
$dxpath = new DOMXPath($dom2);
$dxpath->registerNamespace('d','DAV:');
foreach($xpaths as $xpath=>$count) {
$this->assertEquals($count, $dxpath->query($xpath)->length, 'Looking for : ' . $xpath . ', we could only find ' . $dxpath->query($xpath)->length . ' elements, while we expected ' . $count);
}
}
}

View file

@ -0,0 +1,176 @@
<?php
class Sabre_DAVACL_Property_PrincipalTest extends PHPUnit_Framework_TestCase {
function testSimple() {
$principal = new Sabre_DAVACL_Property_Principal(Sabre_DAVACL_Property_Principal::UNAUTHENTICATED);
$this->assertEquals(Sabre_DAVACL_Property_Principal::UNAUTHENTICATED, $principal->getType());
$this->assertNull($principal->getHref());
$principal = new Sabre_DAVACL_Property_Principal(Sabre_DAVACL_Property_Principal::AUTHENTICATED);
$this->assertEquals(Sabre_DAVACL_Property_Principal::AUTHENTICATED, $principal->getType());
$this->assertNull($principal->getHref());
$principal = new Sabre_DAVACL_Property_Principal(Sabre_DAVACL_Property_Principal::HREF,'admin');
$this->assertEquals(Sabre_DAVACL_Property_Principal::HREF, $principal->getType());
$this->assertEquals('admin',$principal->getHref());
}
/**
* @depends testSimple
* @expectedException Sabre_DAV_Exception
*/
function testNoHref() {
$principal = new Sabre_DAVACL_Property_Principal(Sabre_DAVACL_Property_Principal::HREF);
}
/**
* @depends testSimple
*/
function testSerializeUnAuthenticated() {
$prin = new Sabre_DAVACL_Property_Principal(Sabre_DAVACL_Property_Principal::UNAUTHENTICATED);
$doc = new DOMDocument();
$root = $doc->createElement('d:principal');
$root->setAttribute('xmlns:d','DAV:');
$doc->appendChild($root);
$objectTree = new Sabre_DAV_ObjectTree(new Sabre_DAV_SimpleCollection('rootdir'));
$server = new Sabre_DAV_Server($objectTree);
$prin->serialize($server, $root);
$xml = $doc->saveXML();
$this->assertEquals(
'<?xml version="1.0"?>
<d:principal xmlns:d="DAV:">' .
'<d:unauthenticated/>' .
'</d:principal>
', $xml);
}
/**
* @depends testSerializeUnAuthenticated
*/
function testSerializeAuthenticated() {
$prin = new Sabre_DAVACL_Property_Principal(Sabre_DAVACL_Property_Principal::AUTHENTICATED);
$doc = new DOMDocument();
$root = $doc->createElement('d:principal');
$root->setAttribute('xmlns:d','DAV:');
$doc->appendChild($root);
$objectTree = new Sabre_DAV_ObjectTree(new Sabre_DAV_SimpleCollection('rootdir'));
$server = new Sabre_DAV_Server($objectTree);
$prin->serialize($server, $root);
$xml = $doc->saveXML();
$this->assertEquals(
'<?xml version="1.0"?>
<d:principal xmlns:d="DAV:">' .
'<d:authenticated/>' .
'</d:principal>
', $xml);
}
/**
* @depends testSerializeUnAuthenticated
*/
function testSerializeHref() {
$prin = new Sabre_DAVACL_Property_Principal(Sabre_DAVACL_Property_Principal::HREF,'principals/admin');
$doc = new DOMDocument();
$root = $doc->createElement('d:principal');
$root->setAttribute('xmlns:d','DAV:');
$doc->appendChild($root);
$objectTree = new Sabre_DAV_ObjectTree(new Sabre_DAV_SimpleCollection('rootdir'));
$server = new Sabre_DAV_Server($objectTree);
$prin->serialize($server, $root);
$xml = $doc->saveXML();
$this->assertEquals(
'<?xml version="1.0"?>
<d:principal xmlns:d="DAV:">' .
'<d:href>/principals/admin</d:href>' .
'</d:principal>
', $xml);
}
function testUnserializeHref() {
$xml = '<?xml version="1.0"?>
<d:principal xmlns:d="DAV:">' .
'<d:href>/principals/admin</d:href>' .
'</d:principal>';
$dom = Sabre_DAV_XMLUtil::loadDOMDocument($xml);
$principal = Sabre_DAVACL_Property_Principal::unserialize($dom->firstChild);
$this->assertEquals(Sabre_DAVACL_Property_Principal::HREF, $principal->getType());
$this->assertEquals('/principals/admin', $principal->getHref());
}
function testUnserializeAuthenticated() {
$xml = '<?xml version="1.0"?>
<d:principal xmlns:d="DAV:">' .
' <d:authenticated />' .
'</d:principal>';
$dom = Sabre_DAV_XMLUtil::loadDOMDocument($xml);
$principal = Sabre_DAVACL_Property_Principal::unserialize($dom->firstChild);
$this->assertEquals(Sabre_DAVACL_Property_Principal::AUTHENTICATED, $principal->getType());
}
function testUnserializeUnauthenticated() {
$xml = '<?xml version="1.0"?>
<d:principal xmlns:d="DAV:">' .
' <d:unauthenticated />' .
'</d:principal>';
$dom = Sabre_DAV_XMLUtil::loadDOMDocument($xml);
$principal = Sabre_DAVACL_Property_Principal::unserialize($dom->firstChild);
$this->assertEquals(Sabre_DAVACL_Property_Principal::UNAUTHENTICATED, $principal->getType());
}
/**
* @expectedException Sabre_DAV_Exception_BadRequest
*/
function testUnserializeUnknown() {
$xml = '<?xml version="1.0"?>
<d:principal xmlns:d="DAV:">' .
' <d:foo />' .
'</d:principal>';
$dom = Sabre_DAV_XMLUtil::loadDOMDocument($xml);
Sabre_DAVACL_Property_Principal::unserialize($dom->firstChild);
}
}

View file

@ -0,0 +1,99 @@
<?php
class Sabre_DAVACL_Property_SupportedPrivilegeSetTest extends PHPUnit_Framework_TestCase {
function testSimple() {
$prop = new Sabre_DAVACL_Property_SupportedPrivilegeSet(array(
'privilege' => '{DAV:}all',
));
}
/**
* @depends testSimple
*/
function testSerializeSimple() {
$prop = new Sabre_DAVACL_Property_SupportedPrivilegeSet(array(
'privilege' => '{DAV:}all',
));
$doc = new DOMDocument();
$root = $doc->createElementNS('DAV:', 'd:supported-privilege-set');
$doc->appendChild($root);
$server = new Sabre_DAV_Server();
$prop->serialize($server, $root);
$xml = $doc->saveXML();
$this->assertEquals(
'<?xml version="1.0"?>
<d:supported-privilege-set xmlns:d="DAV:">' .
'<d:supported-privilege>' .
'<d:privilege>' .
'<d:all/>' .
'</d:privilege>' .
'</d:supported-privilege>' .
'</d:supported-privilege-set>
', $xml);
}
/**
* @depends testSimple
*/
function testSerializeAggregate() {
$prop = new Sabre_DAVACL_Property_SupportedPrivilegeSet(array(
'privilege' => '{DAV:}all',
'abstract' => true,
'aggregates' => array(
array(
'privilege' => '{DAV:}read',
),
array(
'privilege' => '{DAV:}write',
'description' => 'booh',
),
),
));
$doc = new DOMDocument();
$root = $doc->createElementNS('DAV:', 'd:supported-privilege-set');
$doc->appendChild($root);
$server = new Sabre_DAV_Server();
$prop->serialize($server, $root);
$xml = $doc->saveXML();
$this->assertEquals(
'<?xml version="1.0"?>
<d:supported-privilege-set xmlns:d="DAV:">' .
'<d:supported-privilege>' .
'<d:privilege>' .
'<d:all/>' .
'</d:privilege>' .
'<d:abstract/>' .
'<d:supported-privilege>' .
'<d:privilege>' .
'<d:read/>' .
'</d:privilege>' .
'</d:supported-privilege>' .
'<d:supported-privilege>' .
'<d:privilege>' .
'<d:write/>' .
'</d:privilege>' .
'<d:description>booh</d:description>' .
'</d:supported-privilege>' .
'</d:supported-privilege>' .
'</d:supported-privilege-set>
', $xml);
}
}

View file

@ -0,0 +1,311 @@
<?php
require_once 'Sabre/DAV/Auth/MockBackend.php';
require_once 'Sabre/DAVACL/MockPrincipal.php';
require_once 'Sabre/DAVACL/MockACLNode.php';
class Sabre_DAVACL_SimplePluginTest extends PHPUnit_Framework_TestCase {
function testValues() {
$aclPlugin = new Sabre_DAVACL_Plugin();
$this->assertEquals('acl',$aclPlugin->getPluginName());
$this->assertEquals(array('access-control'), $aclPlugin->getFeatures());
$this->assertEquals(
array(
'{DAV:}expand-property',
'{DAV:}principal-property-search',
'{DAV:}principal-search-property-set'
),
$aclPlugin->getSupportedReportSet(''));
$this->assertEquals(array('ACL'), $aclPlugin->getMethods(''));
}
function testGetFlatPrivilegeSet() {
$expected = array(
'{DAV:}all' => array(
'privilege' => '{DAV:}all',
'abstract' => true,
'aggregates' => array(
'{DAV:}read',
'{DAV:}write',
),
'concrete' => null,
),
'{DAV:}read' => array(
'privilege' => '{DAV:}read',
'abstract' => false,
'aggregates' => array(
'{DAV:}read-acl',
'{DAV:}read-current-user-privilege-set',
),
'concrete' => '{DAV:}read',
),
'{DAV:}read-acl' => array(
'privilege' => '{DAV:}read-acl',
'abstract' => true,
'aggregates' => array(),
'concrete' => '{DAV:}read',
),
'{DAV:}read-current-user-privilege-set' => array(
'privilege' => '{DAV:}read-current-user-privilege-set',
'abstract' => true,
'aggregates' => array(),
'concrete' => '{DAV:}read',
),
'{DAV:}write' => array(
'privilege' => '{DAV:}write',
'abstract' => false,
'aggregates' => array(
'{DAV:}write-acl',
'{DAV:}write-properties',
'{DAV:}write-content',
'{DAV:}bind',
'{DAV:}unbind',
'{DAV:}unlock',
),
'concrete' => '{DAV:}write',
),
'{DAV:}write-acl' => array(
'privilege' => '{DAV:}write-acl',
'abstract' => true,
'aggregates' => array(),
'concrete' => '{DAV:}write',
),
'{DAV:}write-properties' => array(
'privilege' => '{DAV:}write-properties',
'abstract' => true,
'aggregates' => array(),
'concrete' => '{DAV:}write',
),
'{DAV:}write-content' => array(
'privilege' => '{DAV:}write-content',
'abstract' => true,
'aggregates' => array(),
'concrete' => '{DAV:}write',
),
'{DAV:}unlock' => array(
'privilege' => '{DAV:}unlock',
'abstract' => true,
'aggregates' => array(),
'concrete' => '{DAV:}write',
),
'{DAV:}bind' => array(
'privilege' => '{DAV:}bind',
'abstract' => true,
'aggregates' => array(),
'concrete' => '{DAV:}write',
),
'{DAV:}unbind' => array(
'privilege' => '{DAV:}unbind',
'abstract' => true,
'aggregates' => array(),
'concrete' => '{DAV:}write',
),
);
$plugin = new Sabre_DAVACL_Plugin();
$server = new Sabre_DAV_Server();
$server->addPlugin($plugin);
$this->assertEquals($expected, $plugin->getFlatPrivilegeSet(''));
}
function testCurrentUserPrincipalsNotLoggedIn() {
$acl = new Sabre_DAVACL_Plugin();
$server = new Sabre_DAV_Server();
$server->addPlugin($acl);
$this->assertEquals(array(),$acl->getCurrentUserPrincipals());
}
function testCurrentUserPrincipalsSimple() {
$tree = array(
new Sabre_DAV_SimpleCollection('principals', array(
new Sabre_DAVACL_MockPrincipal('admin','principals/admin'),
))
);
$acl = new Sabre_DAVACL_Plugin();
$server = new Sabre_DAV_Server($tree);
$server->addPlugin($acl);
$auth = new Sabre_DAV_Auth_Plugin(new Sabre_DAV_Auth_MockBackend(),'SabreDAV');
$server->addPlugin($auth);
//forcing login
$auth->beforeMethod('GET','/');
$this->assertEquals(array('principals/admin'),$acl->getCurrentUserPrincipals());
}
function testCurrentUserPrincipalsGroups() {
$tree = array(
new Sabre_DAV_SimpleCollection('principals', array(
new Sabre_DAVACL_MockPrincipal('admin','principals/admin',array('principals/administrators', 'principals/everyone')),
new Sabre_DAVACL_MockPrincipal('administrators','principals/administrators',array('principals/groups'), array('principals/admin')),
new Sabre_DAVACL_MockPrincipal('everyone','principals/everyone',array(), array('principals/admin')),
new Sabre_DAVACL_MockPrincipal('groups','principals/groups',array(), array('principals/administrators')),
))
);
$acl = new Sabre_DAVACL_Plugin();
$server = new Sabre_DAV_Server($tree);
$server->addPlugin($acl);
$auth = new Sabre_DAV_Auth_Plugin(new Sabre_DAV_Auth_MockBackend(),'SabreDAV');
$server->addPlugin($auth);
//forcing login
$auth->beforeMethod('GET','/');
$expected = array(
'principals/admin',
'principals/administrators',
'principals/everyone',
'principals/groups',
);
$this->assertEquals($expected,$acl->getCurrentUserPrincipals());
}
function testGetACL() {
$acl = array(
array(
'principal' => 'principals/admin',
'privilege' => '{DAV:}read',
),
array(
'principal' => 'principals/admin',
'privilege' => '{DAV:}write',
),
);
$tree = array(
new Sabre_DAVACL_MockACLNode('foo',$acl),
);
$server = new Sabre_DAV_Server($tree);
$aclPlugin = new Sabre_DAVACL_Plugin();
$server->addPlugin($aclPlugin);
$this->assertEquals($acl,$aclPlugin->getACL('foo'));
}
function testGetCurrentUserPrivilegeSet() {
$acl = array(
array(
'principal' => 'principals/admin',
'privilege' => '{DAV:}read',
),
array(
'principal' => 'principals/user1',
'privilege' => '{DAV:}read',
),
array(
'principal' => 'principals/admin',
'privilege' => '{DAV:}write',
),
);
$tree = array(
new Sabre_DAVACL_MockACLNode('foo',$acl),
new Sabre_DAV_SimpleCollection('principals', array(
new Sabre_DAVACL_MockPrincipal('admin','principals/admin'),
)),
);
$server = new Sabre_DAV_Server($tree);
$aclPlugin = new Sabre_DAVACL_Plugin();
$server->addPlugin($aclPlugin);
$auth = new Sabre_DAV_Auth_Plugin(new Sabre_DAV_Auth_MockBackend(),'SabreDAV');
$server->addPlugin($auth);
//forcing login
$auth->beforeMethod('GET','/');
$expected = array(
'{DAV:}write',
'{DAV:}write-acl',
'{DAV:}write-properties',
'{DAV:}write-content',
'{DAV:}bind',
'{DAV:}unbind',
'{DAV:}unlock',
'{DAV:}read',
'{DAV:}read-acl',
'{DAV:}read-current-user-privilege-set',
);
$this->assertEquals($expected,$aclPlugin->getCurrentUserPrivilegeSet('foo'));
}
function testCheckPrivileges() {
$acl = array(
array(
'principal' => 'principals/admin',
'privilege' => '{DAV:}read',
),
array(
'principal' => 'principals/user1',
'privilege' => '{DAV:}read',
),
array(
'principal' => 'principals/admin',
'privilege' => '{DAV:}write',
),
);
$tree = array(
new Sabre_DAVACL_MockACLNode('foo',$acl),
new Sabre_DAV_SimpleCollection('principals', array(
new Sabre_DAVACL_MockPrincipal('admin','principals/admin'),
)),
);
$server = new Sabre_DAV_Server($tree);
$aclPlugin = new Sabre_DAVACL_Plugin();
$server->addPlugin($aclPlugin);
$auth = new Sabre_DAV_Auth_Plugin(new Sabre_DAV_Auth_MockBackend(),'SabreDAV');
$server->addPlugin($auth);
//forcing login
//$auth->beforeMethod('GET','/');
$this->assertFalse($aclPlugin->checkPrivileges('foo', array('{DAV:}read'), Sabre_DAVACL_Plugin::R_PARENT, false));
}
}

View file

@ -0,0 +1,15 @@
<?php
class Sabre_DAVACL_VersionTest extends PHPUnit_Framework_TestCase {
function testString() {
$v = Sabre_DAVACL_Version::VERSION;
$this->assertEquals(-1, version_compare('1.0.0',$v));
$s = Sabre_DAVACL_Version::STABILITY;
$this->assertTrue($s == 'alpha' || $s == 'beta' || $s =='stable');
}
}