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,60 @@
<?php
require_once 'Sabre/HTTP/ResponseMock.php';
abstract class Sabre_DAV_AbstractServer extends PHPUnit_Framework_TestCase {
/**
* @var Sabre_HTTP_ResponseMock
*/
protected $response;
protected $request;
/**
* @var Sabre_DAV_Server
*/
protected $server;
protected $tempDir = SABRE_TEMPDIR;
function setUp() {
$this->response = new Sabre_HTTP_ResponseMock();
$this->server = new Sabre_DAV_Server($this->getRootNode());
$this->server->httpResponse = $this->response;
$this->server->debugExceptions = true;
file_put_contents(SABRE_TEMPDIR . '/test.txt', 'Test contents');
mkdir(SABRE_TEMPDIR . '/dir');
file_put_contents(SABRE_TEMPDIR . '/dir/child.txt', 'Child contents');
}
function tearDown() {
$this->deleteTree(SABRE_TEMPDIR,false);
}
protected function getRootNode() {
return new Sabre_DAV_FS_Directory(SABRE_TEMPDIR);
}
private function deleteTree($path,$deleteRoot = true) {
foreach(scandir($path) as $node) {
if ($node=='.' || $node=='.svn' || $node=='..') continue;
$myPath = $path.'/'. $node;
if (is_file($myPath)) {
unlink($myPath);
} else {
$this->deleteTree($myPath);
}
}
if ($deleteRoot) rmdir($path);
}
}

View file

@ -0,0 +1,87 @@
<?php
require_once 'Sabre/HTTP/ResponseMock.php';
class Sabre_DAV_Auth_Backend_AbstractBasicTest extends PHPUnit_Framework_TestCase {
/**
* @expectedException Sabre_DAV_Exception_NotAuthenticated
*/
public function testAuthenticateNoHeaders() {
$response = new Sabre_HTTP_ResponseMock();
$tree = new Sabre_DAV_ObjectTree(new Sabre_DAV_SimpleCollection('bla'));
$server = new Sabre_DAV_Server($tree);
$server->httpResponse = $response;
$backend = new Sabre_DAV_Auth_Backend_AbstractBasicMock();
$backend->authenticate($server,'myRealm');
}
/**
* @expectedException Sabre_DAV_Exception_NotAuthenticated
*/
public function testAuthenticateUnknownUser() {
$response = new Sabre_HTTP_ResponseMock();
$tree = new Sabre_DAV_ObjectTree(new Sabre_DAV_SimpleCollection('bla'));
$server = new Sabre_DAV_Server($tree);
$server->httpResponse = $response;
$request = new Sabre_HTTP_Request(array(
'PHP_AUTH_USER' => 'username',
'PHP_AUTH_PW' => 'wrongpassword',
));
$server->httpRequest = $request;
$backend = new Sabre_DAV_Auth_Backend_AbstractBasicMock();
$backend->authenticate($server,'myRealm');
}
public function testAuthenticate() {
$response = new Sabre_HTTP_ResponseMock();
$tree = new Sabre_DAV_ObjectTree(new Sabre_DAV_SimpleCollection('bla'));
$server = new Sabre_DAV_Server($tree);
$server->httpResponse = $response;
$request = new Sabre_HTTP_Request(array(
'PHP_AUTH_USER' => 'username',
'PHP_AUTH_PW' => 'password',
));
$server->httpRequest = $request;
$backend = new Sabre_DAV_Auth_Backend_AbstractBasicMock();
$this->assertTrue($backend->authenticate($server,'myRealm'));
$result = $backend->getCurrentUser();
$this->assertEquals('username', $result);
}
}
class Sabre_DAV_Auth_Backend_AbstractBasicMock extends Sabre_DAV_Auth_Backend_AbstractBasic {
/**
* Validates a username and password
*
* This method should return true or false depending on if login
* succeeded.
*
* @param string $username
* @param string $password
* @return bool
*/
function validateUserPass($username, $password) {
return ($username == 'username' && $password == 'password');
}
}

View file

@ -0,0 +1,150 @@
<?php
require_once 'Sabre/HTTP/ResponseMock.php';
class Sabre_DAV_Auth_Backend_AbstractDigestTest extends PHPUnit_Framework_TestCase {
/**
* @expectedException Sabre_DAV_Exception_NotAuthenticated
*/
public function testAuthenticateNoHeaders() {
$response = new Sabre_HTTP_ResponseMock();
$tree = new Sabre_DAV_ObjectTree(new Sabre_DAV_SimpleCollection('bla'));
$server = new Sabre_DAV_Server($tree);
$server->httpResponse = $response;
$backend = new Sabre_DAV_Auth_Backend_AbstractDigestMock();
$backend->authenticate($server,'myRealm');
}
/**
* @expectedException Sabre_DAV_Exception
*/
public function testAuthenticateBadGetUserInfoResponse() {
$response = new Sabre_HTTP_ResponseMock();
$tree = new Sabre_DAV_ObjectTree(new Sabre_DAV_SimpleCollection('bla'));
$server = new Sabre_DAV_Server($tree);
$server->httpResponse = $response;
$header = 'username=null, realm=myRealm, nonce=12345, uri=/, response=HASH, opaque=1, qop=auth, nc=1, cnonce=1';
$request = new Sabre_HTTP_Request(array(
'PHP_AUTH_DIGEST' => $header,
));
$server->httpRequest = $request;
$backend = new Sabre_DAV_Auth_Backend_AbstractDigestMock();
$backend->authenticate($server,'myRealm');
}
/**
* @expectedException Sabre_DAV_Exception
*/
public function testAuthenticateBadGetUserInfoResponse2() {
$response = new Sabre_HTTP_ResponseMock();
$tree = new Sabre_DAV_ObjectTree(new Sabre_DAV_SimpleCollection('bla'));
$server = new Sabre_DAV_Server($tree);
$server->httpResponse = $response;
$header = 'username=array, realm=myRealm, nonce=12345, uri=/, response=HASH, opaque=1, qop=auth, nc=1, cnonce=1';
$request = new Sabre_HTTP_Request(array(
'PHP_AUTH_DIGEST' => $header,
));
$server->httpRequest = $request;
$backend = new Sabre_DAV_Auth_Backend_AbstractDigestMock();
$backend->authenticate($server,'myRealm');
}
/**
* @expectedException Sabre_DAV_Exception_NotAuthenticated
*/
public function testAuthenticateUnknownUser() {
$response = new Sabre_HTTP_ResponseMock();
$tree = new Sabre_DAV_ObjectTree(new Sabre_DAV_SimpleCollection('bla'));
$server = new Sabre_DAV_Server($tree);
$server->httpResponse = $response;
$header = 'username=false, realm=myRealm, nonce=12345, uri=/, response=HASH, opaque=1, qop=auth, nc=1, cnonce=1';
$request = new Sabre_HTTP_Request(array(
'PHP_AUTH_DIGEST' => $header,
));
$server->httpRequest = $request;
$backend = new Sabre_DAV_Auth_Backend_AbstractDigestMock();
$backend->authenticate($server,'myRealm');
}
/**
* @expectedException Sabre_DAV_Exception_NotAuthenticated
*/
public function testAuthenticateBadPassword() {
$response = new Sabre_HTTP_ResponseMock();
$tree = new Sabre_DAV_ObjectTree(new Sabre_DAV_SimpleCollection('bla'));
$server = new Sabre_DAV_Server($tree);
$server->httpResponse = $response;
$header = 'username=user, realm=myRealm, nonce=12345, uri=/, response=HASH, opaque=1, qop=auth, nc=1, cnonce=1';
$request = new Sabre_HTTP_Request(array(
'PHP_AUTH_DIGEST' => $header,
'REQUEST_METHOD' => 'PUT',
));
$server->httpRequest = $request;
$backend = new Sabre_DAV_Auth_Backend_AbstractDigestMock();
$backend->authenticate($server,'myRealm');
}
public function testAuthenticate() {
$response = new Sabre_HTTP_ResponseMock();
$tree = new Sabre_DAV_ObjectTree(new Sabre_DAV_SimpleCollection('bla'));
$server = new Sabre_DAV_Server($tree);
$server->httpResponse = $response;
$digestHash = md5('HELLO:12345:1:1:auth:' . md5('GET:/'));
$header = 'username=user, realm=myRealm, nonce=12345, uri=/, response='.$digestHash.', opaque=1, qop=auth, nc=1, cnonce=1';
$request = new Sabre_HTTP_Request(array(
'REQUEST_METHOD' => 'GET',
'PHP_AUTH_DIGEST' => $header,
'REQUEST_URI' => '/',
));
$server->httpRequest = $request;
$backend = new Sabre_DAV_Auth_Backend_AbstractDigestMock();
$this->assertTrue($backend->authenticate($server,'myRealm'));
$result = $backend->getCurrentUser();
$this->assertEquals('user', $result);
$this->assertEquals('HELLO', $backend->getDigestHash('myRealm', $result));
}
}
class Sabre_DAV_Auth_Backend_AbstractDigestMock extends Sabre_DAV_Auth_Backend_AbstractDigest {
function getDigestHash($realm, $userName) {
switch($userName) {
case 'null' : return null;
case 'false' : return false;
case 'array' : return array();
case 'user' : return 'HELLO';
}
}
}

View file

@ -0,0 +1,31 @@
<?php
abstract class Sabre_DAV_Auth_Backend_AbstractPDOTest extends PHPUnit_Framework_TestCase {
abstract function getPDO();
function testConstruct() {
$pdo = $this->getPDO();
$backend = new Sabre_DAV_Auth_Backend_PDO($pdo);
$this->assertTrue($backend instanceof Sabre_DAV_Auth_Backend_PDO);
}
/**
* @depends testConstruct
*/
function testUserInfo() {
$pdo = $this->getPDO();
$backend = new Sabre_DAV_Auth_Backend_PDO($pdo);
$this->assertNull($backend->getDigestHash('realm','blabla'));
$expected = 'hash';
$this->assertEquals($expected, $backend->getDigestHash('realm','user'));
}
}

View file

@ -0,0 +1,40 @@
<?php
class Sabre_DAV_Auth_Backend_ApacheTest extends PHPUnit_Framework_TestCase {
function testConstruct() {
$backend = new Sabre_DAV_Auth_Backend_Apache();
}
/**
* @expectedException Sabre_DAV_Exception
*/
function testNoHeader() {
$server = new Sabre_DAV_Server();
$backend = new Sabre_DAV_Auth_Backend_Apache();
$backend->authenticate($server,'Realm');
}
function testRemoteUser() {
$backend = new Sabre_DAV_Auth_Backend_Apache();
$server = new Sabre_DAV_Server();
$request = new Sabre_HTTP_Request(array(
'REMOTE_USER' => 'username',
));
$server->httpRequest = $request;
$this->assertTrue($backend->authenticate($server, 'Realm'));
$userInfo = 'username';
$this->assertEquals($userInfo, $backend->getCurrentUser());
}
}

View file

@ -0,0 +1,40 @@
<?php
class Sabre_DAV_Auth_Backend_FileTest extends PHPUnit_Framework_TestCase {
function tearDown() {
if (file_exists(SABRE_TEMPDIR . '/filebackend')) unlink(SABRE_TEMPDIR .'/filebackend');
}
function testConstruct() {
$file = new Sabre_DAV_Auth_Backend_File();
$this->assertTrue($file instanceof Sabre_DAV_Auth_Backend_File);
}
/**
* @expectedException Sabre_DAV_Exception
*/
function testLoadFileBroken() {
file_put_contents(SABRE_TEMPDIR . '/backend','user:realm:hash');
$file = new Sabre_DAV_Auth_Backend_File();
$file->loadFile(SABRE_TEMPDIR .'/backend');
}
function testLoadFile() {
file_put_contents(SABRE_TEMPDIR . '/backend','user:realm:' . md5('user:realm:password'));
$file = new Sabre_DAV_Auth_Backend_File();
$file->loadFile(SABRE_TEMPDIR . '/backend');
$this->assertFalse($file->getDigestHash('realm','blabla'));
$this->assertEquals(md5('user:realm:password'), $file->getDigesthash('realm','user'));
}
}

View file

@ -0,0 +1,24 @@
<?php
class Sabre_DAV_Auth_Backend_PDOSQLiteTest extends Sabre_DAV_Auth_Backend_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 users (username TEXT, digesta1 TEXT)');
$pdo->query('INSERT INTO users VALUES ("user","hash")');
return $pdo;
}
}

View file

@ -0,0 +1,29 @@
<?php
require_once 'Sabre/TestUtil.php';
class Sabre_DAV_Auth_Backend_PDOMySQLTest extends Sabre_DAV_Auth_Backend_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 users");
$pdo->query("
create table users (
id integer unsigned not null primary key auto_increment,
username varchar(50),
digesta1 varchar(32),
email varchar(80),
displayname varchar(80),
unique(username)
);");
$pdo->query("INSERT INTO users (username,digesta1,email,displayname) VALUES ('user','hash','user@example.org','User')");
return $pdo;
}
}

View file

@ -0,0 +1,26 @@
<?php
require_once 'Sabre/DAV/Auth/Backend/AbstractPDOTest.php';
class Sabre_DAV_Auth_Backend_PDOSQLiteTest extends Sabre_DAV_Auth_Backend_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 users (username TEXT, digesta1 TEXT, email VARCHAR(80), displayname VARCHAR(80))');
$pdo->query('INSERT INTO users VALUES ("user","hash","user@example.org","User")');
return $pdo;
}
}

View file

@ -0,0 +1,32 @@
<?php
class Sabre_DAV_Auth_MockBackend implements Sabre_DAV_Auth_IBackend {
protected $currentUser;
/**
* @param Sabre_DAV_Server $server
* @param string $realm
* @throws Sabre_DAV_Exception_NotAuthenticated
*/
function authenticate(Sabre_DAV_Server $server, $realm) {
if ($realm=='failme') throw new Sabre_DAV_Exception_NotAuthenticated('deliberate fail');
$this->currentUser = 'admin';
}
function setCurrentUser($user) {
$this->currentUser = $user;
}
function getCurrentUser() {
return $this->currentUser;
}
}

View file

@ -0,0 +1,80 @@
<?php
require_once 'Sabre/DAV/Auth/MockBackend.php';
require_once 'Sabre/HTTP/ResponseMock.php';
class Sabre_DAV_Auth_PluginTest extends PHPUnit_Framework_TestCase {
function testInit() {
$fakeServer = new Sabre_DAV_Server(new Sabre_DAV_ObjectTree(new Sabre_DAV_SimpleCollection('bla')));
$plugin = new Sabre_DAV_Auth_Plugin(new Sabre_DAV_Auth_MockBackend(),'realm');
$this->assertTrue($plugin instanceof Sabre_DAV_Auth_Plugin);
$fakeServer->addPlugin($plugin);
$this->assertEquals($plugin, $fakeServer->getPlugin('auth'));
}
/**
* @depends testInit
*/
function testAuthenticate() {
$fakeServer = new Sabre_DAV_Server(new Sabre_DAV_ObjectTree(new Sabre_DAV_SimpleCollection('bla')));
$plugin = new Sabre_DAV_Auth_Plugin(new Sabre_DAV_Auth_MockBackend(),'realm');
$fakeServer->addPlugin($plugin);
$fakeServer->broadCastEvent('beforeMethod',array('GET','/'));
}
/**
* @depends testInit
* @expectedException Sabre_DAV_Exception_NotAuthenticated
*/
function testAuthenticateFail() {
$fakeServer = new Sabre_DAV_Server(new Sabre_DAV_ObjectTree(new Sabre_DAV_SimpleCollection('bla')));
$plugin = new Sabre_DAV_Auth_Plugin(new Sabre_DAV_Auth_MockBackend(),'failme');
$fakeServer->addPlugin($plugin);
$fakeServer->broadCastEvent('beforeMethod',array('GET','/'));
}
function testReportPassThrough() {
$fakeServer = new Sabre_DAV_Server(new Sabre_DAV_ObjectTree(new Sabre_DAV_SimpleCollection('bla')));
$plugin = new Sabre_DAV_Auth_Plugin(new Sabre_DAV_Auth_MockBackend(),'realm');
$fakeServer->addPlugin($plugin);
$request = new Sabre_HTTP_Request(array(
'REQUEST_METHOD' => 'REPORT',
'HTTP_CONTENT_TYPE' => 'application/xml',
'REQUEST_URI' => '/',
));
$request->setBody('<?xml version="1.0"?><s:somereport xmlns:s="http://www.rooftopsolutions.nl/NS/example" />');
$fakeServer->httpRequest = $request;
$fakeServer->httpResponse = new Sabre_HTTP_ResponseMock();
$fakeServer->exec();
$this->assertEquals('HTTP/1.1 501 Not Implemented', $fakeServer->httpResponse->status);
}
/**
* @depends testInit
*/
function testGetCurrentUserPrincipal() {
$fakeServer = new Sabre_DAV_Server(new Sabre_DAV_ObjectTree(new Sabre_DAV_SimpleCollection('bla')));
$plugin = new Sabre_DAV_Auth_Plugin(new Sabre_DAV_Auth_MockBackend(),'realm');
$fakeServer->addPlugin($plugin);
$fakeServer->broadCastEvent('beforeMethod',array('GET','/'));
$this->assertEquals('admin', $plugin->getCurrentUser());
}
}

View file

@ -0,0 +1,232 @@
<?php
class Sabre_DAV_BasicNodeTest extends PHPUnit_Framework_TestCase {
/**
* @expectedException Sabre_DAV_Exception_Forbidden
*/
public function testPut() {
$file = new Sabre_DAV_FileMock();
$file->put('hi');
}
/**
* @expectedException Sabre_DAV_Exception_Forbidden
*/
public function testGet() {
$file = new Sabre_DAV_FileMock();
$file->get();
}
public function testGetSize() {
$file = new Sabre_DAV_FileMock();
$this->assertEquals(0,$file->getSize());
}
public function testGetETag() {
$file = new Sabre_DAV_FileMock();
$this->assertNull($file->getETag());
}
public function testGetContentType() {
$file = new Sabre_DAV_FileMock();
$this->assertNull($file->getContentType());
}
/**
* @expectedException Sabre_DAV_Exception_Forbidden
*/
public function testDelete() {
$file = new Sabre_DAV_FileMock();
$file->delete();
}
/**
* @expectedException Sabre_DAV_Exception_Forbidden
*/
public function testSetName() {
$file = new Sabre_DAV_FileMock();
$file->setName('hi');
}
public function testGetLastModified() {
$file = new Sabre_DAV_FileMock();
// checking if lastmod is within the range of a few seconds
$lastMod = $file->getLastModified();
$compareTime = ($lastMod + 1)-time();
$this->assertTrue($compareTime < 3);
}
public function testGetChild() {
$dir = new Sabre_DAV_DirectoryMock();
$file = $dir->getChild('mockfile');
$this->assertTrue($file instanceof Sabre_DAV_FileMock);
}
public function testChildExists() {
$dir = new Sabre_DAV_DirectoryMock();
$this->assertTrue($dir->childExists('mockfile'));
}
public function testChildExistsFalse() {
$dir = new Sabre_DAV_DirectoryMock();
$this->assertFalse($dir->childExists('mockfile2'));
}
/**
* @expectedException Sabre_DAV_Exception_NotFound
*/
public function testGetChild404() {
$dir = new Sabre_DAV_DirectoryMock();
$file = $dir->getChild('blabla');
}
/**
* @expectedException Sabre_DAV_Exception_Forbidden
*/
public function testCreateFile() {
$dir = new Sabre_DAV_DirectoryMock();
$dir->createFile('hello','data');
}
/**
* @expectedException Sabre_DAV_Exception_Forbidden
*/
public function testCreateDirectory() {
$dir = new Sabre_DAV_DirectoryMock();
$dir->createDirectory('hello');
}
public function testSimpleDirectoryConstruct() {
$dir = new Sabre_DAV_SimpleCollection('simpledir',array());
}
/**
* @depends testSimpleDirectoryConstruct
*/
public function testSimpleDirectoryConstructChild() {
$file = new Sabre_DAV_FileMock();
$dir = new Sabre_DAV_SimpleCollection('simpledir',array($file));
$file2 = $dir->getChild('mockfile');
$this->assertEquals($file,$file2);
}
/**
* @expectedException Sabre_DAV_Exception
* @depends testSimpleDirectoryConstruct
*/
public function testSimpleDirectoryBadParam() {
$dir = new Sabre_DAV_SimpleCollection('simpledir',array('string shouldn\'t be here'));
}
/**
* @depends testSimpleDirectoryConstruct
*/
public function testSimpleDirectoryAddChild() {
$file = new Sabre_DAV_FileMock();
$dir = new Sabre_DAV_SimpleCollection('simpledir');
$dir->addChild($file);
$file2 = $dir->getChild('mockfile');
$this->assertEquals($file,$file2);
}
/**
* @depends testSimpleDirectoryConstruct
* @depends testSimpleDirectoryAddChild
*/
public function testSimpleDirectoryGetChildren() {
$file = new Sabre_DAV_FileMock();
$dir = new Sabre_DAV_SimpleCollection('simpledir');
$dir->addChild($file);
$this->assertEquals(array($file),$dir->getChildren());
}
/*
* @depends testSimpleDirectoryConstruct
*/
public function testSimpleDirectoryGetName() {
$dir = new Sabre_DAV_SimpleCollection('simpledir');
$this->assertEquals('simpledir',$dir->getName());
}
/**
* @depends testSimpleDirectoryConstruct
* @expectedException Sabre_DAV_Exception_NotFound
*/
public function testSimpleDirectoryGetChild404() {
$dir = new Sabre_DAV_SimpleCollection('simpledir');
$dir->getChild('blabla');
}
}
class Sabre_DAV_DirectoryMock extends Sabre_DAV_Collection {
function getName() {
return 'mockdir';
}
function getChildren() {
return array(new Sabre_DAV_FileMock());
}
}
class Sabre_DAV_FileMock extends Sabre_DAV_File {
function getName() {
return 'mockfile';
}
}

View file

@ -0,0 +1,64 @@
<?php
require_once 'Sabre/DAV/AbstractServer.php';
class Sabre_DAV_Browser_GuessContentTypeTest extends Sabre_DAV_AbstractServer {
function setUp() {
parent::setUp();
file_put_contents(SABRE_TEMPDIR . '/somefile.jpg','blabla');
file_put_contents(SABRE_TEMPDIR . '/somefile.hoi','blabla');
}
function tearDown() {
unlink(SABRE_TEMPDIR . '/somefile.jpg');
parent::tearDown();
}
function testGetProperties() {
$properties = array(
'{DAV:}getcontenttype',
);
$result = $this->server->getPropertiesForPath('/somefile.jpg',$properties);
$this->assertArrayHasKey(0,$result);
$this->assertArrayHasKey(404,$result[0]);
$this->assertArrayHasKey('{DAV:}getcontenttype',$result[0][404]);
}
/**
* @depends testGetProperties
*/
function testGetPropertiesPluginEnabled() {
$this->server->addPlugin(new Sabre_DAV_Browser_GuessContentType());
$properties = array(
'{DAV:}getcontenttype',
);
$result = $this->server->getPropertiesForPath('/somefile.jpg',$properties);
$this->assertArrayHasKey(0,$result);
$this->assertArrayHasKey(200,$result[0]);
$this->assertArrayHasKey('{DAV:}getcontenttype',$result[0][200]);
$this->assertEquals('image/jpeg',$result[0][200]['{DAV:}getcontenttype']);
}
/**
* @depends testGetPropertiesPluginEnabled
*/
function testGetPropertiesUnknown() {
$this->server->addPlugin(new Sabre_DAV_Browser_GuessContentType());
$properties = array(
'{DAV:}getcontenttype',
);
$result = $this->server->getPropertiesForPath('/somefile.hoi',$properties);
$this->assertArrayHasKey(0,$result);
$this->assertArrayHasKey(404,$result[0]);
$this->assertArrayHasKey('{DAV:}getcontenttype',$result[0][404]);
}
}

View file

@ -0,0 +1,38 @@
<?php
require_once 'Sabre/DAV/AbstractServer.php';
class Sabre_DAV_Browser_MapGetToPropFindTest extends Sabre_DAV_AbstractServer {
function setUp() {
parent::setUp();
$this->server->addPlugin(new Sabre_DAV_Browser_MapGetToPropFind());
}
function testCollectionGet() {
$serverVars = array(
'REQUEST_URI' => '/',
'REQUEST_METHOD' => 'GET',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('');
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals(array(
'Content-Type' => 'application/xml; charset=utf-8',
'DAV' => '1, 3, extended-mkcol',
),
$this->response->headers
);
$this->assertEquals('HTTP/1.1 207 Multi-Status',$this->response->status,'Incorrect status response received. Full response body: ' . $this->response->body);
}
}

View file

@ -0,0 +1,108 @@
<?php
require_once 'Sabre/DAV/AbstractServer.php';
class Sabre_DAV_Browser_PluginTest extends Sabre_DAV_AbstractServer{
function setUp() {
parent::setUp();
$this->server->addPlugin(new Sabre_DAV_Browser_Plugin());
}
function testCollectionGet() {
$serverVars = array(
'REQUEST_URI' => '/dir',
'REQUEST_METHOD' => 'GET',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals('HTTP/1.1 200 OK',$this->response->status);
$this->assertEquals(array(
'Content-Type' => 'text/html; charset=utf-8',
),
$this->response->headers
);
$this->assertTrue(strpos($this->response->body, 'Index for dir/') !== false);
}
function testNotFound() {
$serverVars = array(
'REQUEST_URI' => '/random',
'REQUEST_METHOD' => 'GET',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals('HTTP/1.1 404 Not Found',$this->response->status);
}
function testPostOtherContentType() {
$serverVars = array(
'REQUEST_URI' => '/',
'REQUEST_METHOD' => 'POST',
'CONTENT_TYPE' => 'text/xml',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = $request;
$this->server->exec();
$this->assertEquals('HTTP/1.1 501 Not Implemented', $this->response->status);
}
function testPostNoSabreAction() {
$serverVars = array(
'REQUEST_URI' => '/',
'REQUEST_METHOD' => 'POST',
'CONTENT_TYPE' => 'application/x-www-form-urlencoded',
);
$postVars = array();
$request = new Sabre_HTTP_Request($serverVars,$postVars);
$this->server->httpRequest = $request;
$this->server->exec();
$this->assertEquals('HTTP/1.1 501 Not Implemented', $this->response->status);
}
function testPostMkCol() {
$serverVars = array(
'REQUEST_URI' => '/',
'REQUEST_METHOD' => 'POST',
'CONTENT_TYPE' => 'application/x-www-form-urlencoded',
);
$postVars = array(
'sabreAction' => 'mkcol',
'name' => 'new_collection',
);
$request = new Sabre_HTTP_Request($serverVars,$postVars);
$this->server->httpRequest = $request;
$this->server->exec();
$this->assertEquals('HTTP/1.1 302 Found', $this->response->status);
$this->assertEquals(array(
'Location' => '/',
), $this->response->headers);
$this->assertTrue(is_dir(SABRE_TEMPDIR . '/new_collection'));
}
}

View file

@ -0,0 +1,30 @@
<?php
class Sabre_DAV_ClientMock extends Sabre_DAV_Client {
public $response;
public $url;
public $curlSettings;
protected function curlRequest($url, $curlSettings) {
$this->url = $url;
$this->curlSettings = $curlSettings;
return $this->response;
}
/**
* Just making this method public
*
* @param string $url
* @return string
*/
public function getAbsoluteUrl($url) {
return parent::getAbsoluteUrl($url);
}
}

View file

@ -0,0 +1,791 @@
<?php
require_once 'Sabre/DAV/ClientMock.php';
class Sabre_DAV_ClientTest extends PHPUnit_Framework_TestCase {
function testConstruct() {
$client = new Sabre_DAV_ClientMock(array(
'baseUri' => '/',
));
}
/**
* @expectedException InvalidArgumentException
*/
function testConstructNoBaseUri() {
$client = new Sabre_DAV_ClientMock(array());
}
function testRequest() {
$client = new Sabre_DAV_ClientMock(array(
'baseUri' => 'http://example.org/foo/bar/',
));
$responseBlob = array(
"HTTP/1.1 200 OK",
"Content-Type: text/plain",
"",
"Hello there!"
);
$client->response = array(
implode("\r\n", $responseBlob),
array(
'header_size' => 45,
'http_code' => 200,
),
0,
""
);
$result = $client->request('POST', 'baz', 'sillybody', array('Content-Type' => 'text/plain'));
$this->assertEquals('http://example.org/foo/bar/baz', $client->url);
$this->assertEquals(array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 5,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => 'sillybody',
CURLOPT_HEADER => true,
CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
), $client->curlSettings);
$this->assertEquals(array(
'statusCode' => 200,
'headers' => array(
'content-type' => 'text/plain',
),
'body' => 'Hello there!'
), $result);
}
function testRequestProxy() {
$client = new Sabre_DAV_ClientMock(array(
'baseUri' => 'http://example.org/foo/bar/',
'proxy' => 'http://localhost:8000/',
));
$responseBlob = array(
"HTTP/1.1 200 OK",
"Content-Type: text/plain",
"",
"Hello there!"
);
$client->response = array(
implode("\r\n", $responseBlob),
array(
'header_size' => 45,
'http_code' => 200,
),
0,
""
);
$result = $client->request('POST', 'baz', 'sillybody', array('Content-Type' => 'text/plain'));
$this->assertEquals('http://example.org/foo/bar/baz', $client->url);
$this->assertEquals(array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 5,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => 'sillybody',
CURLOPT_HEADER => true,
CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
CURLOPT_PROXY => 'http://localhost:8000/',
), $client->curlSettings);
$this->assertEquals(array(
'statusCode' => 200,
'headers' => array(
'content-type' => 'text/plain',
),
'body' => 'Hello there!'
), $result);
}
function testRequestAuth() {
$client = new Sabre_DAV_ClientMock(array(
'baseUri' => 'http://example.org/foo/bar/',
'userName' => 'user',
'password' => 'password',
));
$responseBlob = array(
"HTTP/1.1 200 OK",
"Content-Type: text/plain",
"",
"Hello there!"
);
$client->response = array(
implode("\r\n", $responseBlob),
array(
'header_size' => 45,
'http_code' => 200,
),
0,
""
);
$result = $client->request('POST', 'baz', 'sillybody', array('Content-Type' => 'text/plain'));
$this->assertEquals('http://example.org/foo/bar/baz', $client->url);
$this->assertEquals(array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 5,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => 'sillybody',
CURLOPT_HEADER => true,
CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
CURLOPT_HTTPAUTH => CURLAUTH_BASIC | CURLAUTH_DIGEST,
CURLOPT_USERPWD => 'user:password'
), $client->curlSettings);
$this->assertEquals(array(
'statusCode' => 200,
'headers' => array(
'content-type' => 'text/plain',
),
'body' => 'Hello there!'
), $result);
}
function testRequestAuthBasic() {
$client = new Sabre_DAV_ClientMock(array(
'baseUri' => 'http://example.org/foo/bar/',
'userName' => 'user',
'password' => 'password',
'authType' => Sabre_DAV_Client::AUTH_BASIC,
));
$responseBlob = array(
"HTTP/1.1 200 OK",
"Content-Type: text/plain",
"",
"Hello there!"
);
$client->response = array(
implode("\r\n", $responseBlob),
array(
'header_size' => 45,
'http_code' => 200,
),
0,
""
);
$result = $client->request('POST', 'baz', 'sillybody', array('Content-Type' => 'text/plain'));
$this->assertEquals('http://example.org/foo/bar/baz', $client->url);
$this->assertEquals(array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 5,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => 'sillybody',
CURLOPT_HEADER => true,
CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => 'user:password'
), $client->curlSettings);
$this->assertEquals(array(
'statusCode' => 200,
'headers' => array(
'content-type' => 'text/plain',
),
'body' => 'Hello there!'
), $result);
}
function testRequestAuthDigest() {
$client = new Sabre_DAV_ClientMock(array(
'baseUri' => 'http://example.org/foo/bar/',
'userName' => 'user',
'password' => 'password',
'authType' => Sabre_DAV_Client::AUTH_DIGEST,
));
$responseBlob = array(
"HTTP/1.1 200 OK",
"Content-Type: text/plain",
"",
"Hello there!"
);
$client->response = array(
implode("\r\n", $responseBlob),
array(
'header_size' => 45,
'http_code' => 200,
),
0,
""
);
$result = $client->request('POST', 'baz', 'sillybody', array('Content-Type' => 'text/plain'));
$this->assertEquals('http://example.org/foo/bar/baz', $client->url);
$this->assertEquals(array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 5,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => 'sillybody',
CURLOPT_HEADER => true,
CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
CURLOPT_HTTPAUTH => CURLAUTH_DIGEST,
CURLOPT_USERPWD => 'user:password'
), $client->curlSettings);
$this->assertEquals(array(
'statusCode' => 200,
'headers' => array(
'content-type' => 'text/plain',
),
'body' => 'Hello there!'
), $result);
}
function testRequestError() {
$client = new Sabre_DAV_ClientMock(array(
'baseUri' => 'http://example.org/foo/bar/',
));
$responseBlob = array(
"HTTP/1.1 200 OK",
"Content-Type: text/plain",
"",
"Hello there!"
);
$client->response = array(
implode("\r\n", $responseBlob),
array(
'header_size' => 45,
'http_code' => 200,
),
CURLE_COULDNT_CONNECT,
"Could not connect, or something"
);
$caught = false;
try {
$client->request('POST', 'baz', 'sillybody', array('Content-Type' => 'text/plain'));
} catch (Sabre_DAV_Exception $e) {
$caught = true;
}
if (!$caught) {
$this->markTestFailed('Exception was not thrown');
}
}
function testRequestHTTPError() {
$client = new Sabre_DAV_ClientMock(array(
'baseUri' => 'http://example.org/foo/bar/',
));
$responseBlob = array(
"HTTP/1.1 400 Bad Request",
"Content-Type: text/plain",
"",
"Hello there!"
);
$client->response = array(
implode("\r\n", $responseBlob),
array(
'header_size' => 45,
'http_code' => 400,
),
0,
""
);
$caught = false;
try {
$client->request('POST', 'baz', 'sillybody', array('Content-Type' => 'text/plain'));
} catch (Sabre_DAV_Exception $e) {
$caught = true;
}
if (!$caught) {
$this->fail('Exception was not thrown');
}
}
function testRequestHTTP404() {
$client = new Sabre_DAV_ClientMock(array(
'baseUri' => 'http://example.org/foo/bar/',
));
$responseBlob = array(
"HTTP/1.1 404 Not Found",
"Content-Type: text/plain",
"",
"Hello there!"
);
$client->response = array(
implode("\r\n", $responseBlob),
array(
'header_size' => 45,
'http_code' => 404,
),
0,
""
);
$caught = false;
try {
$client->request('POST', 'baz', 'sillybody', array('Content-Type' => 'text/plain'));
} catch (Sabre_DAV_Exception_NotFound $e) {
$caught = true;
}
if (!$caught) {
$this->fail('Exception was not thrown');
}
}
/**
* @dataProvider supportedHTTPCodes
*/
function testSpecificHTTPErrors($error) {
$client = new Sabre_DAV_ClientMock(array(
'baseUri' => 'http://example.org/foo/bar/',
));
$responseBlob = array(
"HTTP/1.1 $error blabla",
"Content-Type: text/plain",
"",
"Hello there!"
);
$client->response = array(
implode("\r\n", $responseBlob),
array(
'header_size' => 42,
'http_code' => $error,
),
0,
""
);
$caught = false;
try {
$client->request('POST', 'baz', 'sillybody', array('Content-Type' => 'text/plain'));
} catch (Sabre_DAV_Exception $e) {
$caught = true;
$this->assertEquals($e->getHTTPCode(), $error);
}
if (!$caught) {
$this->fail('Exception was not thrown');
}
}
public function supportedHTTPCodes() {
return array(
array(400),
array(401),
array(402),
array(403),
array(404),
array(405),
array(409),
array(412),
array(416),
array(500),
array(501),
array(507),
);
}
function testGetAbsoluteUrl() {
$client = new Sabre_DAV_ClientMock(array(
'baseUri' => 'http://example.org/foo/',
));
$this->assertEquals(
'http://example.org/foo/bar',
$client->getAbsoluteUrl('bar')
);
$this->assertEquals(
'http://example.org/bar',
$client->getAbsoluteUrl('/bar')
);
$this->assertEquals(
'http://example.com/bar',
$client->getAbsoluteUrl('http://example.com/bar')
);
}
function testOptions() {
$client = new Sabre_DAV_ClientMock(array(
'baseUri' => 'http://example.org/foo/bar/',
));
$responseBlob = array(
"HTTP/1.1 200 OK",
"DAV: feature1, feature2",
"",
);
$client->response = array(
implode("\r\n", $responseBlob),
array(
'header_size' => 40,
'http_code' => 200,
),
0,
""
);
$result = $client->options();
$this->assertEquals(
array('feature1', 'feature2'),
$result
);
}
function testOptionsNoDav() {
$client = new Sabre_DAV_ClientMock(array(
'baseUri' => 'http://example.org/foo/bar/',
));
$responseBlob = array(
"HTTP/1.1 200 OK",
"",
);
$client->response = array(
implode("\r\n", $responseBlob),
array(
'header_size' => 20,
'http_code' => 200,
),
0,
""
);
$result = $client->options();
$this->assertEquals(
array(),
$result
);
}
/**
* @expectedException InvalidArgumentException
*/
function testPropFindNoXML() {
$client = new Sabre_DAV_ClientMock(array(
'baseUri' => 'http://example.org/foo/bar/',
));
$responseBlob = array(
"HTTP/1.1 200 OK",
"",
);
$client->response = array(
implode("\r\n", $responseBlob),
array(
'header_size' => 20,
'http_code' => 200,
),
0,
""
);
$client->propfind('', array('{DAV:}foo','{DAV:}bar'));
}
function testPropFind() {
$client = new Sabre_DAV_ClientMock(array(
'baseUri' => 'http://example.org/foo/bar/',
));
$responseBlob = array(
"HTTP/1.1 200 OK",
"",
"<?xml version=\"1.0\"?>",
"<d:multistatus xmlns:d=\"DAV:\">",
" <d:response>",
" <d:href>/foo/bar/</d:href>",
" <d:propstat>",
" <d:prop>",
" <d:foo>hello</d:foo>",
" </d:prop>",
" <d:status>HTTP/1.1 200 OK</d:status>",
" </d:propstat>",
" <d:propstat>",
" <d:prop>",
" <d:bar />",
" </d:prop>",
" <d:status>HTTP/1.1 404 Not Found</d:status>",
" </d:propstat>",
" </d:response>",
"</d:multistatus>",
);
$client->response = array(
implode("\r\n", $responseBlob),
array(
'header_size' => 19,
'http_code' => 200,
),
0,
""
);
$result = $client->propfind('', array('{DAV:}foo','{DAV:}bar'));
$this->assertEquals(array(
'{DAV:}foo' => 'hello',
), $result);
$requestBody = array(
'<?xml version="1.0"?>',
'<d:propfind xmlns:d="DAV:">',
' <d:prop>',
' <d:foo />',
' <d:bar />',
' </d:prop>',
'</d:propfind>'
);
$requestBody = implode("\n", $requestBody);
$this->assertEquals($requestBody, $client->curlSettings[CURLOPT_POSTFIELDS]);
}
function testPropFindDepth1CustomProp() {
$client = new Sabre_DAV_ClientMock(array(
'baseUri' => 'http://example.org/foo/bar/',
));
$responseBlob = array(
"HTTP/1.1 200 OK",
"",
"<?xml version=\"1.0\"?>",
"<d:multistatus xmlns:d=\"DAV:\" xmlns:x=\"urn:custom\">",
" <d:response>",
" <d:href>/foo/bar/</d:href>",
" <d:propstat>",
" <d:prop>",
" <d:foo>hello</d:foo>",
" <x:bar>world</x:bar>",
" </d:prop>",
" <d:status>HTTP/1.1 200 OK</d:status>",
" </d:propstat>",
" </d:response>",
"</d:multistatus>",
);
$client->response = array(
implode("\r\n", $responseBlob),
array(
'header_size' => 19,
'http_code' => 200,
),
0,
""
);
$result = $client->propfind('', array('{DAV:}foo','{urn:custom}bar'),1);
$this->assertEquals(array(
"/foo/bar/" => array(
'{DAV:}foo' => 'hello',
'{urn:custom}bar' => 'world',
),
), $result);
$requestBody = array(
'<?xml version="1.0"?>',
'<d:propfind xmlns:d="DAV:">',
' <d:prop>',
' <d:foo />',
' <x:bar xmlns:x="urn:custom"/>',
' </d:prop>',
'</d:propfind>'
);
$requestBody = implode("\n", $requestBody);
$this->assertEquals($requestBody, $client->curlSettings[CURLOPT_POSTFIELDS]);
}
function testPropPatch() {
$client = new Sabre_DAV_ClientMock(array(
'baseUri' => 'http://example.org/foo/bar/',
));
$responseBlob = array(
"HTTP/1.1 200 OK",
"",
);
$client->response = array(
implode("\r\n", $responseBlob),
array(
'header_size' => 20,
'http_code' => 200,
),
0,
""
);
$client->proppatch('', array(
'{DAV:}foo' => 'newvalue',
'{urn:custom}foo' => 'newvalue2',
'{DAV:}bar' => null,
'{urn:custom}bar' => null,
));
$requestBody = array(
'<?xml version="1.0"?>',
'<d:propertyupdate xmlns:d="DAV:">',
'<d:set><d:prop>',
' <d:foo>newvalue</d:foo>',
'</d:prop></d:set>',
'<d:set><d:prop>',
' <x:foo xmlns:x="urn:custom">newvalue2</x:foo>',
'</d:prop></d:set>',
'<d:remove><d:prop>',
' <d:bar />',
'</d:prop></d:remove>',
'<d:remove><d:prop>',
' <x:bar xmlns:x="urn:custom"/>',
'</d:prop></d:remove>',
'</d:propertyupdate>'
);
$requestBody = implode("\n", $requestBody);
$this->assertEquals($requestBody, $client->curlSettings[CURLOPT_POSTFIELDS]);
}
function testHEADRequest() {
$client = new Sabre_DAV_ClientMock(array(
'baseUri' => 'http://example.org/foo/bar/',
));
$responseBlob = array(
"HTTP/1.1 200 OK",
"Content-Type: text/plain",
"",
"Hello there!"
);
$client->response = array(
implode("\r\n", $responseBlob),
array(
'header_size' => 45,
'http_code' => 200,
),
0,
""
);
$result = $client->request('HEAD', 'baz');
$this->assertEquals('http://example.org/foo/bar/baz', $client->url);
$this->assertEquals(array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 5,
CURLOPT_CUSTOMREQUEST => 'HEAD',
CURLOPT_NOBODY => true,
CURLOPT_HEADER => true,
CURLOPT_HTTPHEADER => array(),
CURLOPT_POSTFIELDS => null,
), $client->curlSettings);
}
function testPUTRequest() {
$client = new Sabre_DAV_ClientMock(array(
'baseUri' => 'http://example.org/foo/bar/',
));
$responseBlob = array(
"HTTP/1.1 200 OK",
"Content-Type: text/plain",
"",
"Hello there!"
);
$client->response = array(
implode("\r\n", $responseBlob),
array(
'header_size' => 45,
'http_code' => 200,
),
0,
""
);
$result = $client->request('PUT', 'bar','newcontent');
$this->assertEquals('http://example.org/foo/bar/bar', $client->url);
$this->assertEquals(array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 5,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => 'newcontent',
CURLOPT_HEADER => true,
CURLOPT_HTTPHEADER => array(),
), $client->curlSettings);
}
}

View file

@ -0,0 +1,12 @@
<?php
class Sabre_DAV_Exception_PaymentRequiredTest extends PHPUnit_Framework_TestCase {
function testGetHTTPCode() {
$ex = new Sabre_DAV_Exception_PaymentRequired();
$this->assertEquals(402, $ex->getHTTPCode());
}
}

View file

@ -0,0 +1,28 @@
<?php
class Sabre_DAV_ExceptionTest extends PHPUnit_Framework_TestCase {
function testStatus() {
$e = new Sabre_DAV_Exception();
$this->assertEquals(500,$e->getHTTPCode());
}
function testExceptionStatuses() {
$c = array(
'Sabre_DAV_Exception_NotAuthenticated' => 401,
'Sabre_DAV_Exception_InsufficientStorage' => 507,
);
foreach($c as $class=>$status) {
$obj = new $class();
$this->assertEquals($status, $obj->getHTTPCode());
}
}
}

View file

@ -0,0 +1,76 @@
<?php
require_once 'Sabre/TestUtil.php';
class Sabre_DAV_FSExt_FileTest extends PHPUnit_Framework_TestCase {
function setUp() {
file_put_contents(SABRE_TEMPDIR . '/file.txt', 'Contents');
}
function tearDown() {
Sabre_TestUtil::clearTempDir();
}
function testPut() {
$file = new Sabre_DAV_FSExt_File(SABRE_TEMPDIR . '/file.txt');
$result = $file->put('New contents');
$this->assertEquals('New contents',file_get_contents(SABRE_TEMPDIR . '/file.txt'));
$this->assertEquals('"' . md5('New contents') . '"', $result);
}
function testRange() {
$file = new Sabre_DAV_FSExt_File(SABRE_TEMPDIR . '/file.txt');
$file->put('0000000');
$file->putRange('111',3);
$this->assertEquals('0011100',file_get_contents(SABRE_TEMPDIR . '/file.txt'));
}
function testGet() {
$file = new Sabre_DAV_FSExt_File(SABRE_TEMPDIR . '/file.txt');
$this->assertEquals('Contents',stream_get_contents($file->get()));
}
function testDelete() {
$file = new Sabre_DAV_FSExt_File(SABRE_TEMPDIR . '/file.txt');
$file->delete();
$this->assertFalse(file_exists(SABRE_TEMPDIR . '/file.txt'));
}
function testGetETag() {
$file = new Sabre_DAV_FSExt_File(SABRE_TEMPDIR . '/file.txt');
$this->assertEquals('"' . md5('Contents') . '"',$file->getETag());
}
function testGetContentType() {
$file = new Sabre_DAV_FSExt_File(SABRE_TEMPDIR . '/file.txt');
$this->assertNull($file->getContentType());
}
function testGetSize() {
$file = new Sabre_DAV_FSExt_File(SABRE_TEMPDIR . '/file.txt');
$this->assertEquals(8,$file->getSize());
}
}

View file

@ -0,0 +1,175 @@
<?php
require_once 'Sabre/TestUtil.php';
class Sabre_DAV_FSExt_NodeTest extends PHPUnit_Framework_TestCase {
function setUp() {
mkdir(SABRE_TEMPDIR . '/dir');
file_put_contents(SABRE_TEMPDIR . '/dir/file.txt', 'Contents');
file_put_contents(SABRE_TEMPDIR . '/dir/file2.txt', 'Contents2');
}
function tearDown() {
Sabre_TestUtil::clearTempDir();
}
function testUpdateProperties() {
$file = new Sabre_DAV_FSExt_File(SABRE_TEMPDIR . '/dir/file.txt');
$properties = array(
'{http://sabredav.org/NS/2010}test1' => 'foo',
'{http://sabredav.org/NS/2010}test2' => 'bar',
);
$result = $file->updateProperties($properties);
$expected = true;
$this->assertEquals($expected, $result);
$getProperties = $file->getProperties(array_keys($properties));
$this->assertEquals($properties, $getProperties);
}
/**
* @depends testUpdateProperties
*/
function testUpdatePropertiesAgain() {
$file = new Sabre_DAV_FSExt_File(SABRE_TEMPDIR . '/dir/file.txt');
$mutations = array(
'{http://sabredav.org/NS/2010}test1' => 'foo',
'{http://sabredav.org/NS/2010}test2' => 'bar',
);
$result = $file->updateProperties($mutations);
$this->assertEquals(true, $result);
$mutations = array(
'{http://sabredav.org/NS/2010}test1' => 'foo',
'{http://sabredav.org/NS/2010}test3' => 'baz',
);
$result = $file->updateProperties($mutations);
$this->assertEquals(true, $result);
}
/**
* @depends testUpdateProperties
*/
function testUpdatePropertiesDelete() {
$file = new Sabre_DAV_FSExt_File(SABRE_TEMPDIR . '/dir/file.txt');
$mutations = array(
'{http://sabredav.org/NS/2010}test1' => 'foo',
'{http://sabredav.org/NS/2010}test2' => 'bar',
);
$result = $file->updateProperties($mutations);
$this->assertEquals(true, $result);
$mutations = array(
'{http://sabredav.org/NS/2010}test1' => null,
'{http://sabredav.org/NS/2010}test3' => null
);
$result = $file->updateProperties($mutations);
$this->assertEquals(true, $result);
$properties = $file->getProperties(array('http://sabredav.org/NS/2010}test1','{http://sabredav.org/NS/2010}test2','{http://sabredav.org/NS/2010}test3'));
$this->assertEquals(array(
'{http://sabredav.org/NS/2010}test2' => 'bar',
), $properties);
}
/**
* @depends testUpdateProperties
*/
function testUpdatePropertiesMove() {
$file = new Sabre_DAV_FSExt_File(SABRE_TEMPDIR . '/dir/file.txt');
$mutations = array(
'{http://sabredav.org/NS/2010}test1' => 'foo',
'{http://sabredav.org/NS/2010}test2' => 'bar',
);
$result = $file->updateProperties($mutations);
$this->assertEquals(true, $result);
$properties = $file->getProperties(array('{http://sabredav.org/NS/2010}test1','{http://sabredav.org/NS/2010}test2','{http://sabredav.org/NS/2010}test3'));
$this->assertEquals(array(
'{http://sabredav.org/NS/2010}test1' => 'foo',
'{http://sabredav.org/NS/2010}test2' => 'bar',
), $properties);
// Renaming
$file->setName('file3.txt');
$this->assertFalse(file_exists(SABRE_TEMPDIR . '/dir/file.txt'));
$this->assertTrue(file_exists(SABRE_TEMPDIR . '/dir/file3.txt'));
$this->assertEquals('file3.txt',$file->getName());
$newFile = new Sabre_DAV_FSExt_File(SABRE_TEMPDIR . '/dir/file3.txt');
$this->assertEquals('file3.txt',$newFile->getName());
$properties = $newFile->getProperties(array('{http://sabredav.org/NS/2010}test1','{http://sabredav.org/NS/2010}test2','{http://sabredav.org/NS/2010}test3'));
$this->assertEquals(array(
'{http://sabredav.org/NS/2010}test1' => 'foo',
'{http://sabredav.org/NS/2010}test2' => 'bar',
), $properties);
}
/**
* @depends testUpdatePropertiesMove
*/
function testUpdatePropertiesDeleteBleed() {
$file = new Sabre_DAV_FSExt_File(SABRE_TEMPDIR . '/dir/file.txt');
$mutations = array(
'{http://sabredav.org/NS/2010}test1' => 'foo',
'{http://sabredav.org/NS/2010}test2' => 'bar',
);
$result = $file->updateProperties($mutations);
$this->assertEquals(true, $result);
$properties = $file->getProperties(array('{http://sabredav.org/NS/2010}test1','{http://sabredav.org/NS/2010}test2','{http://sabredav.org/NS/2010}test3'));
$this->assertEquals(array(
'{http://sabredav.org/NS/2010}test1' => 'foo',
'{http://sabredav.org/NS/2010}test2' => 'bar',
), $properties);
// Deleting
$file->delete();
$this->assertFalse(file_exists(SABRE_TEMPDIR . '/dir/file.txt'));
// Creating it again
file_put_contents(SABRE_TEMPDIR . '/dir/file.txt','New Contents');
$file = new Sabre_DAV_FSExt_File(SABRE_TEMPDIR . '/dir/file.txt');
$properties = $file->getProperties(array('http://sabredav.org/NS/2010}test1','{http://sabredav.org/NS/2010}test2','{http://sabredav.org/NS/2010}test3'));
$this->assertEquals(array(), $properties);
}
}

View file

@ -0,0 +1,219 @@
<?php
require_once 'Sabre/DAV/AbstractServer.php';
class Sabre_DAV_FSExt_ServerTest extends Sabre_DAV_AbstractServer{
protected function getRootNode() {
return new Sabre_DAV_FSExt_Directory($this->tempDir);
}
function testGet() {
$serverVars = array(
'REQUEST_URI' => '/test.txt',
'REQUEST_METHOD' => 'GET',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals(array(
'Content-Type' => 'application/octet-stream',
'Content-Length' => 13,
'Last-Modified' => Sabre_HTTP_Util::toHTTPDate(new DateTime('@' . filemtime($this->tempDir . '/test.txt'))),
'ETag' => '"' .md5_file($this->tempDir . '/test.txt') . '"',
),
$this->response->headers
);
$this->assertEquals('HTTP/1.1 200 OK',$this->response->status);
$this->assertEquals('Test contents', stream_get_contents($this->response->body));
}
function testHEAD() {
$serverVars = array(
'REQUEST_URI' => '/test.txt',
'REQUEST_METHOD' => 'HEAD',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals(array(
'Content-Type' => 'application/octet-stream',
'Content-Length' => 13,
'Last-Modified' => Sabre_HTTP_Util::toHTTPDate(new DateTime('@' . filemtime($this->tempDir . '/test.txt'))),
'ETag' => '"' . md5_file($this->tempDir . '/test.txt') . '"',
),
$this->response->headers
);
$this->assertEquals('HTTP/1.1 200 OK',$this->response->status);
$this->assertEquals('', $this->response->body);
}
function testPut() {
$serverVars = array(
'REQUEST_URI' => '/testput.txt',
'REQUEST_METHOD' => 'PUT',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('Testing new file');
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals(array(
'Content-Length' => 0,
'ETag' => '"' . md5('Testing new file') . '"',
), $this->response->headers);
$this->assertEquals('HTTP/1.1 201 Created',$this->response->status);
$this->assertEquals('', $this->response->body);
$this->assertEquals('Testing new file',file_get_contents($this->tempDir . '/testput.txt'));
}
function testPutAlreadyExists() {
$serverVars = array(
'REQUEST_URI' => '/test.txt',
'REQUEST_METHOD' => 'PUT',
'HTTP_IF_NONE_MATCH' => '*',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('Testing new file');
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals(array(
'Content-Type' => 'application/xml; charset=utf-8',
),$this->response->headers);
$this->assertEquals('HTTP/1.1 412 Precondition failed',$this->response->status);
$this->assertNotEquals('Testing new file',file_get_contents($this->tempDir . '/test.txt'));
}
function testMkcol() {
$serverVars = array(
'REQUEST_URI' => '/testcol',
'REQUEST_METHOD' => 'MKCOL',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody("");
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals(array(
'Content-Length' => '0',
),$this->response->headers);
$this->assertEquals('HTTP/1.1 201 Created',$this->response->status);
$this->assertEquals('', $this->response->body);
$this->assertTrue(is_dir($this->tempDir . '/testcol'));
}
function testPutUpdate() {
$serverVars = array(
'REQUEST_URI' => '/test.txt',
'REQUEST_METHOD' => 'PUT',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('Testing updated file');
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals('0', $this->response->headers['Content-Length']);
$this->assertEquals('HTTP/1.1 204 No Content',$this->response->status);
$this->assertEquals('', $this->response->body);
$this->assertEquals('Testing updated file',file_get_contents($this->tempDir . '/test.txt'));
}
function testDelete() {
$serverVars = array(
'REQUEST_URI' => '/test.txt',
'REQUEST_METHOD' => 'DELETE',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals(array(
'Content-Length' => '0',
),$this->response->headers);
$this->assertEquals('HTTP/1.1 204 No Content',$this->response->status);
$this->assertEquals('', $this->response->body);
$this->assertFalse(file_exists($this->tempDir . '/test.txt'));
}
function testDeleteDirectory() {
$serverVars = array(
'REQUEST_URI' => '/testcol',
'REQUEST_METHOD' => 'DELETE',
);
mkdir($this->tempDir.'/testcol');
file_put_contents($this->tempDir.'/testcol/test.txt','Hi! I\'m a file with a short lifespan');
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals(array(
'Content-Length' => '0',
),$this->response->headers);
$this->assertEquals('HTTP/1.1 204 No Content',$this->response->status);
$this->assertEquals('', $this->response->body);
$this->assertFalse(file_exists($this->tempDir . '/col'));
}
function testOptions() {
$serverVars = array(
'REQUEST_URI' => '/',
'REQUEST_METHOD' => 'OPTIONS',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals(array(
'DAV' => '1, 3, extended-mkcol',
'MS-Author-Via' => 'DAV',
'Allow' => 'OPTIONS, GET, HEAD, DELETE, PROPFIND, PUT, PROPPATCH, COPY, MOVE, REPORT',
'Accept-Ranges' => 'bytes',
'Content-Length' => '0',
'X-Sabre-Version'=> Sabre_DAV_Version::VERSION,
),$this->response->headers);
$this->assertEquals('HTTP/1.1 200 OK',$this->response->status);
$this->assertEquals('', $this->response->body);
}
}

View file

@ -0,0 +1,102 @@
<?php
require_once 'Sabre/TestUtil.php';
class Sabre_DAV_Issue33Test extends PHPUnit_Framework_TestCase {
function setUp() {
Sabre_TestUtil::clearTempDir();
}
function testCopyMoveInfo() {
$foo = new Sabre_DAV_SimpleCollection('foo');
$root = new Sabre_DAV_SimpleCollection('webdav',array($foo));
$tree = new Sabre_DAV_ObjectTree($root);
$server = new Sabre_DAV_Server($tree);
$server->setBaseUri('/webdav/');
$serverVars = array(
'REQUEST_URI' => '/webdav/foo',
'HTTP_DESTINATION' => 'http://dev2.tribalos.com/webdav/%C3%A0fo%C3%B3',
'HTTP_OVERWRITE' => 'F',
);
$request = new Sabre_HTTP_Request($serverVars);
$server->httpRequest = $request;
$info = $server->getCopyAndMoveInfo();
$this->assertEquals('%C3%A0fo%C3%B3', urlencode($info['destination']));
$this->assertFalse($info['destinationExists']);
$this->assertFalse($info['destinationNode']);
}
function testTreeMove() {
mkdir(SABRE_TEMPDIR . '/issue33');
$dir = new Sabre_DAV_FS_Directory(SABRE_TEMPDIR . '/issue33');
$dir->createDirectory('foo');
$tree = new Sabre_DAV_ObjectTree($dir);
$tree->move('foo',urldecode('%C3%A0fo%C3%B3'));
$node = $tree->getNodeForPath(urldecode('%C3%A0fo%C3%B3'));
$this->assertEquals(urldecode('%C3%A0fo%C3%B3'),$node->getName());
}
function testDirName() {
$dirname1 = 'foo';
$dirname2 = urlencode('%C3%A0fo%C3%B3');;
$this->assertTrue(dirname($dirname1)==dirname($dirname2));
}
/**
* @depends testTreeMove
* @depends testCopyMoveInfo
*/
function testEverything() {
// Request object
$serverVars = array(
'REQUEST_METHOD' => 'MOVE',
'REQUEST_URI' => '/webdav/foo',
'HTTP_DESTINATION' => 'http://dev2.tribalos.com/webdav/%C3%A0fo%C3%B3',
'HTTP_OVERWRITE' => 'F',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('');
$response = new Sabre_HTTP_ResponseMock();
// Server setup
mkdir(SABRE_TEMPDIR . '/issue33');
$dir = new Sabre_DAV_FS_Directory(SABRE_TEMPDIR . '/issue33');
$dir->createDirectory('foo');
$tree = new Sabre_DAV_ObjectTree($dir);
$server = new Sabre_DAV_Server($tree);
$server->setBaseUri('/webdav/');
$server->httpRequest = $request;
$server->httpResponse = $response;
$server->exec();
$this->assertTrue(file_exists(SABRE_TEMPDIR . '/issue33/' . urldecode('%C3%A0fo%C3%B3')));
}
}

View file

@ -0,0 +1,192 @@
<?php
abstract class Sabre_DAV_Locks_Backend_AbstractTest extends PHPUnit_Framework_TestCase {
/**
* @abstract
* @return Sabre_DAV_Locks_Backend_Abstract
*/
abstract function getBackend();
function testSetup() {
$backend = $this->getBackend();
$this->assertInstanceOf('Sabre_DAV_Locks_Backend_Abstract', $backend);
}
/**
* @depends testSetup
*/
function testGetLocks() {
$backend = $this->getBackend();
$lock = new Sabre_DAV_Locks_LockInfo();
$lock->owner = 'Sinterklaas';
$lock->timeout = 60;
$lock->created = time();
$lock->token = 'MY-UNIQUE-TOKEN';
$lock->uri ='someuri';
$this->assertTrue($backend->lock('someuri', $lock));
$locks = $backend->getLocks('someuri', false);
$this->assertEquals(1,count($locks));
$this->assertEquals('Sinterklaas',$locks[0]->owner);
$this->assertEquals('someuri',$locks[0]->uri);
}
/**
* @depends testGetLocks
*/
function testGetLocksParent() {
$backend = $this->getBackend();
$lock = new Sabre_DAV_Locks_LockInfo();
$lock->owner = 'Sinterklaas';
$lock->timeout = 60;
$lock->created = time();
$lock->depth = Sabre_DAV_Server::DEPTH_INFINITY;
$lock->token = 'MY-UNIQUE-TOKEN';
$this->assertTrue($backend->lock('someuri', $lock));
$locks = $backend->getLocks('someuri/child', false);
$this->assertEquals(1,count($locks));
$this->assertEquals('Sinterklaas',$locks[0]->owner);
$this->assertEquals('someuri',$locks[0]->uri);
}
/**
* @depends testGetLocks
*/
function testGetLocksParentDepth0() {
$backend = $this->getBackend();
$lock = new Sabre_DAV_Locks_LockInfo();
$lock->owner = 'Sinterklaas';
$lock->timeout = 60;
$lock->created = time();
$lock->depth = 0;
$lock->token = 'MY-UNIQUE-TOKEN';
$this->assertTrue($backend->lock('someuri', $lock));
$locks = $backend->getLocks('someuri/child', false);
$this->assertEquals(0,count($locks));
}
function testGetLocksChildren() {
$backend = $this->getBackend();
$lock = new Sabre_DAV_Locks_LockInfo();
$lock->owner = 'Sinterklaas';
$lock->timeout = 60;
$lock->created = time();
$lock->depth = 0;
$lock->token = 'MY-UNIQUE-TOKEN';
$this->assertTrue($backend->lock('someuri/child', $lock));
$locks = $backend->getLocks('someuri/child', false);
$this->assertEquals(1,count($locks));
$locks = $backend->getLocks('someuri', false);
$this->assertEquals(0,count($locks));
$locks = $backend->getLocks('someuri', true);
$this->assertEquals(1,count($locks));
}
/**
* @depends testGetLocks
*/
function testLockRefresh() {
$backend = $this->getBackend();
$lock = new Sabre_DAV_Locks_LockInfo();
$lock->owner = 'Sinterklaas';
$lock->timeout = 60;
$lock->created = time();
$lock->token = 'MY-UNIQUE-TOKEN';
$this->assertTrue($backend->lock('someuri', $lock));
/* Second time */
$lock->owner = 'Santa Clause';
$this->assertTrue($backend->lock('someuri', $lock));
$locks = $backend->getLocks('someuri', false);
$this->assertEquals(1,count($locks));
$this->assertEquals('Santa Clause',$locks[0]->owner);
$this->assertEquals('someuri',$locks[0]->uri);
}
/**
* @depends testGetLocks
*/
function testUnlock() {
$backend = $this->getBackend();
$lock = new Sabre_DAV_Locks_LockInfo();
$lock->owner = 'Sinterklaas';
$lock->timeout = 60;
$lock->created = time();
$lock->token = 'MY-UNIQUE-TOKEN';
$this->assertTrue($backend->lock('someuri', $lock));
$locks = $backend->getLocks('someuri', false);
$this->assertEquals(1,count($locks));
$this->assertTrue($backend->unlock('someuri',$lock));
$locks = $backend->getLocks('someuri', false);
$this->assertEquals(0,count($locks));
}
/**
* @depends testUnlock
*/
function testUnlockUnknownToken() {
$backend = $this->getBackend();
$lock = new Sabre_DAV_Locks_LockInfo();
$lock->owner = 'Sinterklaas';
$lock->timeout = 60;
$lock->created = time();
$lock->token = 'MY-UNIQUE-TOKEN';
$this->assertTrue($backend->lock('someuri', $lock));
$locks = $backend->getLocks('someuri', false);
$this->assertEquals(1,count($locks));
$lock->token = 'SOME-OTHER-TOKEN';
$this->assertFalse($backend->unlock('someuri',$lock));
$locks = $backend->getLocks('someuri', false);
$this->assertEquals(1,count($locks));
}
}

View file

@ -0,0 +1,29 @@
<?php
require_once 'Sabre/TestUtil.php';
class Sabre_DAV_Locks_Backend_FSTest extends Sabre_DAV_Locks_Backend_AbstractTest {
function getBackend() {
Sabre_TestUtil::clearTempDir();
mkdir(SABRE_TEMPDIR . '/locks');
$backend = new Sabre_DAV_Locks_Backend_FS(SABRE_TEMPDIR . '/locks/');
return $backend;
}
function tearDown() {
Sabre_TestUtil::clearTempDir();
}
function testGetLocksChildren() {
// We're skipping this test. This doesn't work, and it will
// never. The class is deprecated anyway.
}
}

View file

@ -0,0 +1,22 @@
<?php
require_once 'Sabre/TestUtil.php';
class Sabre_DAV_Locks_Backend_FileTest extends Sabre_DAV_Locks_Backend_AbstractTest {
function getBackend() {
Sabre_TestUtil::clearTempDir();
$backend = new Sabre_DAV_Locks_Backend_File(SABRE_TEMPDIR . '/lockdb');
return $backend;
}
function tearDown() {
Sabre_TestUtil::clearTempDir();
}
}

View file

@ -0,0 +1,30 @@
<?php
require_once 'Sabre/TestUtil.php';
class Sabre_DAV_Locks_Backend_PDOMySQLTest extends Sabre_DAV_Locks_Backend_AbstractTest {
function getBackend() {
if (!SABRE_HASMYSQL) $this->markTestSkipped('MySQL driver is not available, or it was not properly configured');
$pdo = Sabre_TestUtil::getMySQLDB();
if (!$pdo) $this->markTestSkipped('Could not connect to MySQL database');
$pdo->query('DROP TABLE IF EXISTS locks;');
$pdo->query("
CREATE TABLE locks (
id INTEGER UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
owner VARCHAR(100),
timeout INTEGER UNSIGNED,
created INTEGER,
token VARCHAR(100),
scope TINYINT,
depth TINYINT,
uri text
);");
$backend = new Sabre_DAV_Locks_Backend_PDO($pdo);
return $backend;
}
}

View file

@ -0,0 +1,27 @@
<?php
require_once 'Sabre/TestUtil.php';
require_once 'Sabre/DAV/Locks/Backend/AbstractTest.php';
class Sabre_DAV_Locks_Backend_PDOTest extends Sabre_DAV_Locks_Backend_AbstractTest {
function getBackend() {
if (!SABRE_HASSQLITE) $this->markTestSkipped('SQLite driver is not available');
Sabre_TestUtil::clearTempDir();
mkdir(SABRE_TEMPDIR . '/pdolocks');
$pdo = new PDO('sqlite:' . SABRE_TEMPDIR . '/pdolocks/db.sqlite');
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$pdo->query('CREATE TABLE locks ( id integer primary key asc, owner text, timeout text, created integer, token text, scope integer, depth integer, uri text)');
$backend = new Sabre_DAV_Locks_Backend_PDO($pdo);
return $backend;
}
function tearDown() {
Sabre_TestUtil::clearTempDir();
}
}

View file

@ -0,0 +1,370 @@
<?php
require_once 'Sabre/HTTP/ResponseMock.php';
require_once 'Sabre/DAV/AbstractServer.php';
class Sabre_DAV_Locks_GetIfConditionsTest extends Sabre_DAV_AbstractServer {
/**
* @var Sabre_DAV_Locks_Plugin
*/
protected $locksPlugin;
function setUp() {
parent::setUp();
$locksPlugin = new Sabre_DAV_Locks_Plugin();
$this->server->addPlugin($locksPlugin);
$this->locksPlugin = $locksPlugin;
}
function testNoConditions() {
$serverVars = array(
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$conditions = $this->locksPlugin->getIfConditions();
$this->assertEquals(array(),$conditions);
}
function testLockToken() {
$serverVars = array(
'HTTP_IF' => '(<opaquelocktoken:token1>)',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$conditions = $this->locksPlugin->getIfConditions();
$compare = array(
array(
'uri' => '',
'tokens' => array(
array(
1,
'opaquelocktoken:token1',
'',
),
),
),
);
$this->assertEquals($compare,$conditions);
}
function testNotLockToken() {
$serverVars = array(
'HTTP_IF' => '(Not <opaquelocktoken:token1>)',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$conditions = $this->locksPlugin->getIfConditions();
$compare = array(
array(
'uri' => '',
'tokens' => array(
array(
0,
'opaquelocktoken:token1',
'',
),
),
),
);
$this->assertEquals($compare,$conditions);
}
function testLockTokenUrl() {
$serverVars = array(
'HTTP_IF' => '<http://www.example.com/> (<opaquelocktoken:token1>)',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$conditions = $this->locksPlugin->getIfConditions();
$compare = array(
array(
'uri' => 'http://www.example.com/',
'tokens' => array(
array(
1,
'opaquelocktoken:token1',
'',
),
),
),
);
$this->assertEquals($compare,$conditions);
}
function test2LockTokens() {
$serverVars = array(
'HTTP_IF' => '(<opaquelocktoken:token1>) (Not <opaquelocktoken:token2>)',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$conditions = $this->locksPlugin->getIfConditions();
$compare = array(
array(
'uri' => '',
'tokens' => array(
array(
1,
'opaquelocktoken:token1',
'',
),
array(
0,
'opaquelocktoken:token2',
'',
),
),
),
);
$this->assertEquals($compare,$conditions);
}
function test2UriLockTokens() {
$serverVars = array(
'HTTP_IF' => '<http://www.example.org/node1> (<opaquelocktoken:token1>) <http://www.example.org/node2> (Not <opaquelocktoken:token2>)',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$conditions = $this->locksPlugin->getIfConditions();
$compare = array(
array(
'uri' => 'http://www.example.org/node1',
'tokens' => array(
array(
1,
'opaquelocktoken:token1',
'',
),
),
),
array(
'uri' => 'http://www.example.org/node2',
'tokens' => array(
array(
0,
'opaquelocktoken:token2',
'',
),
),
),
);
$this->assertEquals($compare,$conditions);
}
function test2UriMultiLockTokens() {
$serverVars = array(
'HTTP_IF' => '<http://www.example.org/node1> (<opaquelocktoken:token1>) (<opaquelocktoken:token2>) <http://www.example.org/node2> (Not <opaquelocktoken:token3>)',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$conditions = $this->locksPlugin->getIfConditions();
$compare = array(
array(
'uri' => 'http://www.example.org/node1',
'tokens' => array(
array(
1,
'opaquelocktoken:token1',
'',
),
array(
1,
'opaquelocktoken:token2',
'',
),
),
),
array(
'uri' => 'http://www.example.org/node2',
'tokens' => array(
array(
0,
'opaquelocktoken:token3',
'',
),
),
),
);
$this->assertEquals($compare,$conditions);
}
function testEtag() {
$serverVars = array(
'HTTP_IF' => '([etag1])',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$conditions = $this->locksPlugin->getIfConditions();
$compare = array(
array(
'uri' => '',
'tokens' => array(
array(
1,
'',
'etag1',
),
),
),
);
$this->assertEquals($compare,$conditions);
}
function test2Etags() {
$serverVars = array(
'HTTP_IF' => '<http://www.example.org/> ([etag1]) ([etag2])',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$conditions = $this->locksPlugin->getIfConditions();
$compare = array(
array(
'uri' => 'http://www.example.org/',
'tokens' => array(
array(
1,
'',
'etag1',
),
array(
1,
'',
'etag2',
),
),
),
);
$this->assertEquals($compare,$conditions);
}
function testComplexIf() {
$serverVars = array(
'HTTP_IF' => '<http://www.example.org/node1> (<opaquelocktoken:token1> [etag1]) ' .
'(Not <opaquelocktoken:token2>) ([etag2]) <http://www.example.org/node2> ' .
'(<opaquelocktoken:token3>) (Not <opaquelocktoken:token4>) ([etag3])',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$conditions = $this->locksPlugin->getIfConditions();
$compare = array(
array(
'uri' => 'http://www.example.org/node1',
'tokens' => array(
array(
1,
'opaquelocktoken:token1',
'etag1',
),
array(
0,
'opaquelocktoken:token2',
'',
),
array(
1,
'',
'etag2',
),
),
),
array(
'uri' => 'http://www.example.org/node2',
'tokens' => array(
array(
1,
'opaquelocktoken:token3',
'',
),
array(
0,
'opaquelocktoken:token4',
'',
),
array(
1,
'',
'etag3',
),
),
),
);
$this->assertEquals($compare,$conditions);
}
}

View file

@ -0,0 +1,118 @@
<?php
require_once 'Sabre/HTTP/ResponseMock.php';
require_once 'Sabre/TestUtil.php';
class Sabre_DAV_Locks_MSWordTest extends PHPUnit_Framework_TestCase {
function testLockEtc() {
mkdir(SABRE_TEMPDIR . '/mstest');
$tree = new Sabre_DAV_FS_Directory(SABRE_TEMPDIR . '/mstest');
$server = new Sabre_DAV_Server($tree);
$server->debugExceptions = true;
$locksBackend = new Sabre_DAV_Locks_Backend_File(SABRE_TEMPDIR . '/locksdb');
$locksPlugin = new Sabre_DAV_Locks_Plugin($locksBackend);
$server->addPlugin($locksPlugin);
$response1 = new Sabre_HTTP_ResponseMock();
$server->httpRequest = $this->getLockRequest();
$server->httpResponse = $response1;
$server->exec();
$this->assertEquals('HTTP/1.1 201 Created', $server->httpResponse->status);
$this->assertTrue(isset($server->httpResponse->headers['Lock-Token']));
$lockToken = $server->httpResponse->headers['Lock-Token'];
//sleep(10);
$response2 = new Sabre_HTTP_ResponseMock();
$server->httpRequest = $this->getLockRequest2();
$server->httpResponse = $response2;
$server->exec();
$this->assertEquals('HTTP/1.1 201 Created', $server->httpResponse->status);
$this->assertTrue(isset($server->httpResponse->headers['Lock-Token']));
//sleep(10);
$response3 = new Sabre_HTTP_ResponseMock();
$server->httpRequest = $this->getPutRequest($lockToken);
$server->httpResponse = $response3;
$server->exec();
$this->assertEquals('HTTP/1.1 204 No Content', $server->httpResponse->status);
}
function tearDown() {
Sabre_TestUtil::clearTempDir();
}
function getLockRequest() {
$request = new Sabre_HTTP_Request(array(
'REQUEST_METHOD' => 'LOCK',
'HTTP_CONTENT_TYPE' => 'application/xml',
'HTTP_TIMEOUT' => 'Second-3600',
'REQUEST_URI' => '/Nouveau%20Microsoft%20Office%20Excel%20Worksheet.xlsx',
));
$request->setBody('<D:lockinfo xmlns:D="DAV:">
<D:lockscope>
<D:exclusive />
</D:lockscope>
<D:locktype>
<D:write />
</D:locktype>
<D:owner>
<D:href>PC-Vista\User</D:href>
</D:owner>
</D:lockinfo>');
return $request;
}
function getLockRequest2() {
$request = new Sabre_HTTP_Request(array(
'REQUEST_METHOD' => 'LOCK',
'HTTP_CONTENT_TYPE' => 'application/xml',
'HTTP_TIMEOUT' => 'Second-3600',
'REQUEST_URI' => '/~$Nouveau%20Microsoft%20Office%20Excel%20Worksheet.xlsx',
));
$request->setBody('<D:lockinfo xmlns:D="DAV:">
<D:lockscope>
<D:exclusive />
</D:lockscope>
<D:locktype>
<D:write />
</D:locktype>
<D:owner>
<D:href>PC-Vista\User</D:href>
</D:owner>
</D:lockinfo>');
return $request;
}
function getPutRequest($lockToken) {
$request = new Sabre_HTTP_Request(array(
'REQUEST_METHOD' => 'PUT',
'REQUEST_URI' => '/Nouveau%20Microsoft%20Office%20Excel%20Worksheet.xlsx',
'HTTP_IF' => 'If: ('.$lockToken.')',
));
$request->setBody('FAKE BODY');
return $request;
}
}

View file

@ -0,0 +1,961 @@
<?php
require_once 'Sabre/DAV/AbstractServer.php';
class Sabre_DAV_Locks_PluginTest extends Sabre_DAV_AbstractServer {
/**
* @var Sabre_DAV_Locks_Plugin
*/
protected $locksPlugin;
function setUp() {
parent::setUp();
$locksBackend = new Sabre_DAV_Locks_Backend_File(SABRE_TEMPDIR . '/locksdb');
$locksPlugin = new Sabre_DAV_Locks_Plugin($locksBackend);
$this->server->addPlugin($locksPlugin);
$this->locksPlugin = $locksPlugin;
}
function testGetFeatures() {
$this->assertEquals(array(2),$this->locksPlugin->getFeatures());
}
function testGetHTTPMethods() {
$this->assertEquals(array('LOCK','UNLOCK'),$this->locksPlugin->getHTTPMethods(''));
}
function testGetHTTPMethodsNoBackend() {
$locksPlugin = new Sabre_DAV_Locks_Plugin();
$this->server->addPlugin($locksPlugin);
$this->assertEquals(array(),$locksPlugin->getHTTPMethods(''));
}
function testUnknownMethodPassthough() {
$this->assertNull($this->locksPlugin->unknownMethod('BLA','/'));
}
function testLockNoBody() {
$serverVars = array(
'REQUEST_URI' => '/test.txt',
'REQUEST_METHOD' => 'LOCK',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('');
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals(array(
'Content-Type' => 'application/xml; charset=utf-8',
),
$this->response->headers
);
$this->assertEquals('HTTP/1.1 400 Bad request',$this->response->status);
}
function testLock() {
$serverVars = array(
'REQUEST_URI' => '/test.txt',
'REQUEST_METHOD' => 'LOCK',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('<?xml version="1.0"?>
<D:lockinfo xmlns:D="DAV:">
<D:lockscope><D:exclusive/></D:lockscope>
<D:locktype><D:write/></D:locktype>
<D:owner>
<D:href>http://example.org/~ejw/contact.html</D:href>
</D:owner>
</D:lockinfo>');
$this->server->httpRequest = $request;
$this->server->exec();
$this->assertEquals('application/xml; charset=utf-8',$this->response->headers['Content-Type']);
$this->assertTrue(preg_match('/^<opaquelocktoken:(.*)>$/',$this->response->headers['Lock-Token'])===1,'We did not get a valid Locktoken back (' . $this->response->headers['Lock-Token'] . ')');
$this->assertEquals('HTTP/1.1 200 OK',$this->response->status,'Got an incorrect status back. Response body: ' . $this->response->body);
$body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/","xmlns\\1=\"urn:DAV\"",$this->response->body);
$xml = simplexml_load_string($body);
$xml->registerXPathNamespace('d','urn:DAV');
$elements = array(
'/d:prop',
'/d:prop/d:lockdiscovery',
'/d:prop/d:lockdiscovery/d:activelock',
'/d:prop/d:lockdiscovery/d:activelock/d:locktype',
'/d:prop/d:lockdiscovery/d:activelock/d:lockroot',
'/d:prop/d:lockdiscovery/d:activelock/d:lockroot/d:href',
'/d:prop/d:lockdiscovery/d:activelock/d:locktype/d:write',
'/d:prop/d:lockdiscovery/d:activelock/d:lockscope',
'/d:prop/d:lockdiscovery/d:activelock/d:lockscope/d:exclusive',
'/d:prop/d:lockdiscovery/d:activelock/d:depth',
'/d:prop/d:lockdiscovery/d:activelock/d:owner',
'/d:prop/d:lockdiscovery/d:activelock/d:timeout',
'/d:prop/d:lockdiscovery/d:activelock/d:locktoken',
'/d:prop/d:lockdiscovery/d:activelock/d:locktoken/d:href',
);
foreach($elements as $elem) {
$data = $xml->xpath($elem);
$this->assertEquals(1,count($data),'We expected 1 match for the xpath expression "' . $elem . '". ' . count($data) . ' were found. Full response body: ' . $this->response->body);
}
$depth = $xml->xpath('/d:prop/d:lockdiscovery/d:activelock/d:depth');
$this->assertEquals('infinity',(string)$depth[0]);
$token = $xml->xpath('/d:prop/d:lockdiscovery/d:activelock/d:locktoken/d:href');
$this->assertEquals($this->response->headers['Lock-Token'],'<' . (string)$token[0] . '>','Token in response body didn\'t match token in response header.');
}
/**
* @depends testLock
*/
function testDoubleLock() {
$serverVars = array(
'REQUEST_URI' => '/test.txt',
'REQUEST_METHOD' => 'LOCK',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('<?xml version="1.0"?>
<D:lockinfo xmlns:D="DAV:">
<D:lockscope><D:exclusive/></D:lockscope>
<D:locktype><D:write/></D:locktype>
<D:owner>
<D:href>http://example.org/~ejw/contact.html</D:href>
</D:owner>
</D:lockinfo>');
$this->server->httpRequest = $request;
$this->server->exec();
$this->response = new Sabre_HTTP_ResponseMock();
$this->server->httpResponse = $this->response;
$this->server->exec();
$this->assertEquals('application/xml; charset=utf-8',$this->response->headers['Content-Type']);
$this->assertEquals('HTTP/1.1 423 Locked',$this->response->status);
}
/**
* @depends testLock
*/
function testLockRefresh() {
$serverVars = array(
'REQUEST_URI' => '/test.txt',
'REQUEST_METHOD' => 'LOCK',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('<?xml version="1.0"?>
<D:lockinfo xmlns:D="DAV:">
<D:lockscope><D:exclusive/></D:lockscope>
<D:locktype><D:write/></D:locktype>
<D:owner>
<D:href>http://example.org/~ejw/contact.html</D:href>
</D:owner>
</D:lockinfo>');
$this->server->httpRequest = $request;
$this->server->exec();
$lockToken = $this->response->headers['Lock-Token'];
$this->response = new Sabre_HTTP_ResponseMock();
$this->server->httpResponse = $this->response;
$serverVars = array(
'REQUEST_URI' => '/test.txt',
'REQUEST_METHOD' => 'LOCK',
'HTTP_IF' => '(' . $lockToken . ')',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('');
$this->server->httpRequest = $request;
$this->server->exec();
$this->assertEquals('application/xml; charset=utf-8',$this->response->headers['Content-Type']);
$this->assertEquals('HTTP/1.1 200 OK',$this->response->status,'We received an incorrect status code. Full response body: ' . $this->response->body);
}
/**
* @depends testLock
*/
function testLockNoFile() {
$serverVars = array(
'REQUEST_URI' => '/notfound.txt',
'REQUEST_METHOD' => 'LOCK',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('<?xml version="1.0"?>
<D:lockinfo xmlns:D="DAV:">
<D:lockscope><D:exclusive/></D:lockscope>
<D:locktype><D:write/></D:locktype>
<D:owner>
<D:href>http://example.org/~ejw/contact.html</D:href>
</D:owner>
</D:lockinfo>');
$this->server->httpRequest = $request;
$this->server->exec();
$this->assertEquals('application/xml; charset=utf-8',$this->response->headers['Content-Type']);
$this->assertTrue(preg_match('/^<opaquelocktoken:(.*)>$/',$this->response->headers['Lock-Token'])===1,'We did not get a valid Locktoken back (' . $this->response->headers['Lock-Token'] . ')');
$this->assertEquals('HTTP/1.1 201 Created',$this->response->status);
}
/**
* @depends testLock
*/
function testUnlockNoToken() {
$serverVars = array(
'REQUEST_URI' => '/test.txt',
'REQUEST_METHOD' => 'UNLOCK',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals(array(
'Content-Type' => 'application/xml; charset=utf-8',
),
$this->response->headers
);
$this->assertEquals('HTTP/1.1 400 Bad request',$this->response->status);
}
/**
* @depends testLock
*/
function testUnlockBadToken() {
$serverVars = array(
'REQUEST_URI' => '/test.txt',
'REQUEST_METHOD' => 'UNLOCK',
'HTTP_LOCK_TOKEN' => '<opaquelocktoken:blablabla>',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals(array(
'Content-Type' => 'application/xml; charset=utf-8',
),
$this->response->headers
);
$this->assertEquals('HTTP/1.1 409 Conflict',$this->response->status,'Got an incorrect status code. Full response body: ' . $this->response->body);
}
/**
* @depends testLock
*/
function testLockPutNoToken() {
$serverVars = array(
'REQUEST_URI' => '/test.txt',
'REQUEST_METHOD' => 'LOCK',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('<?xml version="1.0"?>
<D:lockinfo xmlns:D="DAV:">
<D:lockscope><D:exclusive/></D:lockscope>
<D:locktype><D:write/></D:locktype>
<D:owner>
<D:href>http://example.org/~ejw/contact.html</D:href>
</D:owner>
</D:lockinfo>');
$this->server->httpRequest = $request;
$this->server->exec();
$this->assertEquals('application/xml; charset=utf-8',$this->response->headers['Content-Type']);
$this->assertTrue(preg_match('/^<opaquelocktoken:(.*)>$/',$this->response->headers['Lock-Token'])===1,'We did not get a valid Locktoken back (' . $this->response->headers['Lock-Token'] . ')');
$this->assertEquals('HTTP/1.1 200 OK',$this->response->status);
$serverVars = array(
'REQUEST_URI' => '/test.txt',
'REQUEST_METHOD' => 'PUT',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('newbody');
$this->server->httpRequest = $request;
$this->server->exec();
$this->assertEquals('application/xml; charset=utf-8',$this->response->headers['Content-Type']);
$this->assertTrue(preg_match('/^<opaquelocktoken:(.*)>$/',$this->response->headers['Lock-Token'])===1,'We did not get a valid Locktoken back (' . $this->response->headers['Lock-Token'] . ')');
$this->assertEquals('HTTP/1.1 423 Locked',$this->response->status);
}
/**
* @depends testLock
*/
function testUnlock() {
$request = new Sabre_HTTP_Request(array());
$this->server->httpRequest = $request;
$request->setBody('<?xml version="1.0"?>
<D:lockinfo xmlns:D="DAV:">
<D:lockscope><D:exclusive/></D:lockscope>
<D:locktype><D:write/></D:locktype>
<D:owner>
<D:href>http://example.org/~ejw/contact.html</D:href>
</D:owner>
</D:lockinfo>');
$this->server->invokeMethod('LOCK','test.txt');
$lockToken = $this->server->httpResponse->headers['Lock-Token'];
$serverVars = array(
'HTTP_LOCK_TOKEN' => $lockToken,
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$this->server->httpResponse = new Sabre_HTTP_ResponseMock();
$this->server->invokeMethod('UNLOCK', 'test.txt');
$this->assertEquals('HTTP/1.1 204 No Content',$this->server->httpResponse->status,'Got an incorrect status code. Full response body: ' . $this->response->body);
$this->assertEquals(array(
'Content-Length' => '0',
),
$this->server->httpResponse->headers
);
}
/**
* @depends testLock
*/
function testUnlockWindowsBug() {
$request = new Sabre_HTTP_Request(array());
$this->server->httpRequest = $request;
$request->setBody('<?xml version="1.0"?>
<D:lockinfo xmlns:D="DAV:">
<D:lockscope><D:exclusive/></D:lockscope>
<D:locktype><D:write/></D:locktype>
<D:owner>
<D:href>http://example.org/~ejw/contact.html</D:href>
</D:owner>
</D:lockinfo>');
$this->server->invokeMethod('LOCK','test.txt');
$lockToken = $this->server->httpResponse->headers['Lock-Token'];
// See Issue 123
$lockToken = trim($lockToken,'<>');
$serverVars = array(
'HTTP_LOCK_TOKEN' => $lockToken,
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$this->server->httpResponse = new Sabre_HTTP_ResponseMock();
$this->server->invokeMethod('UNLOCK', 'test.txt');
$this->assertEquals('HTTP/1.1 204 No Content',$this->server->httpResponse->status,'Got an incorrect status code. Full response body: ' . $this->response->body);
$this->assertEquals(array(
'Content-Length' => '0',
),
$this->server->httpResponse->headers
);
}
/**
* @depends testLock
*/
function testLockRetainOwner() {
$request = new Sabre_HTTP_Request(array());
$this->server->httpRequest = $request;
$request->setBody('<?xml version="1.0"?>
<D:lockinfo xmlns:D="DAV:">
<D:lockscope><D:exclusive/></D:lockscope>
<D:locktype><D:write/></D:locktype>
<D:owner>Evert</D:owner>
</D:lockinfo>');
$this->server->invokeMethod('LOCK','test.txt');
$lockToken = $this->server->httpResponse->headers['Lock-Token'];
$locks = $this->locksPlugin->getLocks('test.txt');
$this->assertEquals(1,count($locks));
$this->assertEquals('Evert',$locks[0]->owner);
}
/**
* @depends testLock
*/
function testLockPutBadToken() {
$serverVars = array(
'REQUEST_URI' => '/test.txt',
'REQUEST_METHOD' => 'LOCK',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('<?xml version="1.0"?>
<D:lockinfo xmlns:D="DAV:">
<D:lockscope><D:exclusive/></D:lockscope>
<D:locktype><D:write/></D:locktype>
<D:owner>
<D:href>http://example.org/~ejw/contact.html</D:href>
</D:owner>
</D:lockinfo>');
$this->server->httpRequest = $request;
$this->server->exec();
$this->assertEquals('application/xml; charset=utf-8',$this->response->headers['Content-Type']);
$this->assertTrue(preg_match('/^<opaquelocktoken:(.*)>$/',$this->response->headers['Lock-Token'])===1,'We did not get a valid Locktoken back (' . $this->response->headers['Lock-Token'] . ')');
$this->assertEquals('HTTP/1.1 200 OK',$this->response->status);
$serverVars = array(
'REQUEST_URI' => '/test.txt',
'REQUEST_METHOD' => 'PUT',
'HTTP_IF' => '(<opaquelocktoken:token1>)',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('newbody');
$this->server->httpRequest = $request;
$this->server->exec();
$this->assertEquals('application/xml; charset=utf-8',$this->response->headers['Content-Type']);
$this->assertTrue(preg_match('/^<opaquelocktoken:(.*)>$/',$this->response->headers['Lock-Token'])===1,'We did not get a valid Locktoken back (' . $this->response->headers['Lock-Token'] . ')');
$this->assertEquals('HTTP/1.1 412 Precondition failed',$this->response->status);
}
/**
* @depends testLock
*/
function testLockDeleteParent() {
$serverVars = array(
'REQUEST_URI' => '/dir/child.txt',
'REQUEST_METHOD' => 'LOCK',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('<?xml version="1.0"?>
<D:lockinfo xmlns:D="DAV:">
<D:lockscope><D:exclusive/></D:lockscope>
<D:locktype><D:write/></D:locktype>
<D:owner>
<D:href>http://example.org/~ejw/contact.html</D:href>
</D:owner>
</D:lockinfo>');
$this->server->httpRequest = $request;
$this->server->exec();
$this->assertEquals('application/xml; charset=utf-8',$this->response->headers['Content-Type']);
$this->assertTrue(preg_match('/^<opaquelocktoken:(.*)>$/',$this->response->headers['Lock-Token'])===1,'We did not get a valid Locktoken back (' . $this->response->headers['Lock-Token'] . ')');
$this->assertEquals('HTTP/1.1 200 OK',$this->response->status);
$serverVars = array(
'REQUEST_URI' => '/dir',
'REQUEST_METHOD' => 'DELETE',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = $request;
$this->server->exec();
$this->assertEquals('HTTP/1.1 423 Locked',$this->response->status);
$this->assertEquals('application/xml; charset=utf-8',$this->response->headers['Content-Type']);
}
/**
* @depends testLock
*/
function testLockDeleteSucceed() {
$serverVars = array(
'REQUEST_URI' => '/dir/child.txt',
'REQUEST_METHOD' => 'LOCK',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('<?xml version="1.0"?>
<D:lockinfo xmlns:D="DAV:">
<D:lockscope><D:exclusive/></D:lockscope>
<D:locktype><D:write/></D:locktype>
<D:owner>
<D:href>http://example.org/~ejw/contact.html</D:href>
</D:owner>
</D:lockinfo>');
$this->server->httpRequest = $request;
$this->server->exec();
$this->assertEquals('application/xml; charset=utf-8',$this->response->headers['Content-Type']);
$this->assertTrue(preg_match('/^<opaquelocktoken:(.*)>$/',$this->response->headers['Lock-Token'])===1,'We did not get a valid Locktoken back (' . $this->response->headers['Lock-Token'] . ')');
$this->assertEquals('HTTP/1.1 200 OK',$this->response->status);
$serverVars = array(
'REQUEST_URI' => '/dir/child.txt',
'REQUEST_METHOD' => 'DELETE',
'HTTP_IF' => '(' . $this->response->headers['Lock-Token'] . ')',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = $request;
$this->server->exec();
$this->assertEquals('HTTP/1.1 204 No Content',$this->response->status);
$this->assertEquals('application/xml; charset=utf-8',$this->response->headers['Content-Type']);
}
/**
* @depends testLock
*/
function testLockCopyLockSource() {
$serverVars = array(
'REQUEST_URI' => '/dir/child.txt',
'REQUEST_METHOD' => 'LOCK',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('<?xml version="1.0"?>
<D:lockinfo xmlns:D="DAV:">
<D:lockscope><D:exclusive/></D:lockscope>
<D:locktype><D:write/></D:locktype>
<D:owner>
<D:href>http://example.org/~ejw/contact.html</D:href>
</D:owner>
</D:lockinfo>');
$this->server->httpRequest = $request;
$this->server->exec();
$this->assertEquals('application/xml; charset=utf-8',$this->response->headers['Content-Type']);
$this->assertTrue(preg_match('/^<opaquelocktoken:(.*)>$/',$this->response->headers['Lock-Token'])===1,'We did not get a valid Locktoken back (' . $this->response->headers['Lock-Token'] . ')');
$this->assertEquals('HTTP/1.1 200 OK',$this->response->status);
$serverVars = array(
'REQUEST_URI' => '/dir/child.txt',
'REQUEST_METHOD' => 'COPY',
'HTTP_DESTINATION' => '/dir/child2.txt',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = $request;
$this->server->exec();
$this->assertEquals('HTTP/1.1 201 Created',$this->response->status,'Copy must succeed if only the source is locked, but not the destination');
$this->assertEquals('application/xml; charset=utf-8',$this->response->headers['Content-Type']);
}
/**
* @depends testLock
*/
function testLockCopyLockDestination() {
$serverVars = array(
'REQUEST_URI' => '/dir/child2.txt',
'REQUEST_METHOD' => 'LOCK',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('<?xml version="1.0"?>
<D:lockinfo xmlns:D="DAV:">
<D:lockscope><D:exclusive/></D:lockscope>
<D:locktype><D:write/></D:locktype>
<D:owner>
<D:href>http://example.org/~ejw/contact.html</D:href>
</D:owner>
</D:lockinfo>');
$this->server->httpRequest = $request;
$this->server->exec();
$this->assertEquals('application/xml; charset=utf-8',$this->response->headers['Content-Type']);
$this->assertTrue(preg_match('/^<opaquelocktoken:(.*)>$/',$this->response->headers['Lock-Token'])===1,'We did not get a valid Locktoken back (' . $this->response->headers['Lock-Token'] . ')');
$this->assertEquals('HTTP/1.1 201 Created',$this->response->status);
$serverVars = array(
'REQUEST_URI' => '/dir/child.txt',
'REQUEST_METHOD' => 'COPY',
'HTTP_DESTINATION' => '/dir/child2.txt',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = $request;
$this->server->exec();
$this->assertEquals('HTTP/1.1 423 Locked',$this->response->status,'Copy must succeed if only the source is locked, but not the destination');
$this->assertEquals('application/xml; charset=utf-8',$this->response->headers['Content-Type']);
}
/**
* @depends testLock
*/
function testLockMoveLockSourceLocked() {
$serverVars = array(
'REQUEST_URI' => '/dir/child.txt',
'REQUEST_METHOD' => 'LOCK',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('<?xml version="1.0"?>
<D:lockinfo xmlns:D="DAV:">
<D:lockscope><D:exclusive/></D:lockscope>
<D:locktype><D:write/></D:locktype>
<D:owner>
<D:href>http://example.org/~ejw/contact.html</D:href>
</D:owner>
</D:lockinfo>');
$this->server->httpRequest = $request;
$this->server->exec();
$this->assertEquals('application/xml; charset=utf-8',$this->response->headers['Content-Type']);
$this->assertTrue(preg_match('/^<opaquelocktoken:(.*)>$/',$this->response->headers['Lock-Token'])===1,'We did not get a valid Locktoken back (' . $this->response->headers['Lock-Token'] . ')');
$this->assertEquals('HTTP/1.1 200 OK',$this->response->status);
$serverVars = array(
'REQUEST_URI' => '/dir/child.txt',
'REQUEST_METHOD' => 'MOVE',
'HTTP_DESTINATION' => '/dir/child2.txt',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = $request;
$this->server->exec();
$this->assertEquals('HTTP/1.1 423 Locked',$this->response->status,'Copy must succeed if only the source is locked, but not the destination');
$this->assertEquals('application/xml; charset=utf-8',$this->response->headers['Content-Type']);
}
/**
* @depends testLock
*/
function testLockMoveLockSourceSucceed() {
$serverVars = array(
'REQUEST_URI' => '/dir/child.txt',
'REQUEST_METHOD' => 'LOCK',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('<?xml version="1.0"?>
<D:lockinfo xmlns:D="DAV:">
<D:lockscope><D:exclusive/></D:lockscope>
<D:locktype><D:write/></D:locktype>
<D:owner>
<D:href>http://example.org/~ejw/contact.html</D:href>
</D:owner>
</D:lockinfo>');
$this->server->httpRequest = $request;
$this->server->exec();
$this->assertEquals('application/xml; charset=utf-8',$this->response->headers['Content-Type']);
$this->assertTrue(preg_match('/^<opaquelocktoken:(.*)>$/',$this->response->headers['Lock-Token'])===1,'We did not get a valid Locktoken back (' . $this->response->headers['Lock-Token'] . ')');
$this->assertEquals('HTTP/1.1 200 OK',$this->response->status);
$serverVars = array(
'REQUEST_URI' => '/dir/child.txt',
'REQUEST_METHOD' => 'MOVE',
'HTTP_DESTINATION' => '/dir/child2.txt',
'HTTP_IF' => '(' . $this->response->headers['Lock-Token'] . ')',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = $request;
$this->server->exec();
$this->assertEquals('HTTP/1.1 201 Created',$this->response->status,'A valid lock-token was provided for the source, so this MOVE operation must succeed. Full response body: ' . $this->response->body);
}
/**
* @depends testLock
*/
function testLockMoveLockDestination() {
$serverVars = array(
'REQUEST_URI' => '/dir/child2.txt',
'REQUEST_METHOD' => 'LOCK',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('<?xml version="1.0"?>
<D:lockinfo xmlns:D="DAV:">
<D:lockscope><D:exclusive/></D:lockscope>
<D:locktype><D:write/></D:locktype>
<D:owner>
<D:href>http://example.org/~ejw/contact.html</D:href>
</D:owner>
</D:lockinfo>');
$this->server->httpRequest = $request;
$this->server->exec();
$this->assertEquals('application/xml; charset=utf-8',$this->response->headers['Content-Type']);
$this->assertTrue(preg_match('/^<opaquelocktoken:(.*)>$/',$this->response->headers['Lock-Token'])===1,'We did not get a valid Locktoken back (' . $this->response->headers['Lock-Token'] . ')');
$this->assertEquals('HTTP/1.1 201 Created',$this->response->status);
$serverVars = array(
'REQUEST_URI' => '/dir/child.txt',
'REQUEST_METHOD' => 'MOVE',
'HTTP_DESTINATION' => '/dir/child2.txt',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = $request;
$this->server->exec();
$this->assertEquals('HTTP/1.1 423 Locked',$this->response->status,'Copy must succeed if only the source is locked, but not the destination');
$this->assertEquals('application/xml; charset=utf-8',$this->response->headers['Content-Type']);
}
/**
* @depends testLock
*/
function testLockMoveLockParent() {
$serverVars = array(
'REQUEST_URI' => '/dir',
'REQUEST_METHOD' => 'LOCK',
'HTTP_DEPTH' => 'infinite',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('<?xml version="1.0"?>
<D:lockinfo xmlns:D="DAV:">
<D:lockscope><D:exclusive/></D:lockscope>
<D:locktype><D:write/></D:locktype>
<D:owner>
<D:href>http://example.org/~ejw/contact.html</D:href>
</D:owner>
</D:lockinfo>');
$this->server->httpRequest = $request;
$this->server->exec();
$this->assertEquals('application/xml; charset=utf-8',$this->response->headers['Content-Type']);
$this->assertTrue(preg_match('/^<opaquelocktoken:(.*)>$/',$this->response->headers['Lock-Token'])===1,'We did not get a valid Locktoken back (' . $this->response->headers['Lock-Token'] . ')');
$this->assertEquals('HTTP/1.1 200 OK',$this->response->status);
$serverVars = array(
'REQUEST_URI' => '/dir/child.txt',
'REQUEST_METHOD' => 'MOVE',
'HTTP_DESTINATION' => '/dir/child2.txt',
'HTTP_IF' => '</dir> (' . $this->response->headers['Lock-Token'] . ')',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = $request;
$this->server->exec();
$this->assertEquals('HTTP/1.1 201 Created',$this->response->status,'We locked the parent of both the source and destination, but the move didn\'t succeed.');
$this->assertEquals('application/xml; charset=utf-8',$this->response->headers['Content-Type']);
}
/**
* @depends testLock
*/
function testLockPutGoodToken() {
$serverVars = array(
'REQUEST_URI' => '/test.txt',
'REQUEST_METHOD' => 'LOCK',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('<?xml version="1.0"?>
<D:lockinfo xmlns:D="DAV:">
<D:lockscope><D:exclusive/></D:lockscope>
<D:locktype><D:write/></D:locktype>
<D:owner>
<D:href>http://example.org/~ejw/contact.html</D:href>
</D:owner>
</D:lockinfo>');
$this->server->httpRequest = $request;
$this->server->exec();
$this->assertEquals('application/xml; charset=utf-8',$this->response->headers['Content-Type']);
$this->assertTrue(preg_match('/^<opaquelocktoken:(.*)>$/',$this->response->headers['Lock-Token'])===1,'We did not get a valid Locktoken back (' . $this->response->headers['Lock-Token'] . ')');
$this->assertEquals('HTTP/1.1 200 OK',$this->response->status);
$serverVars = array(
'REQUEST_URI' => '/test.txt',
'REQUEST_METHOD' => 'PUT',
'HTTP_IF' => '('.$this->response->headers['Lock-Token'].')',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('newbody');
$this->server->httpRequest = $request;
$this->server->exec();
$this->assertEquals('application/xml; charset=utf-8',$this->response->headers['Content-Type']);
$this->assertTrue(preg_match('/^<opaquelocktoken:(.*)>$/',$this->response->headers['Lock-Token'])===1,'We did not get a valid Locktoken back (' . $this->response->headers['Lock-Token'] . ')');
$this->assertEquals('HTTP/1.1 204 No Content',$this->response->status);
}
function testPutWithIncorrectETag() {
$serverVars = array(
'REQUEST_URI' => '/test.txt',
'REQUEST_METHOD' => 'PUT',
'HTTP_IF' => '(["etag1"])',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('newbody');
$this->server->httpRequest = $request;
$this->server->exec();
$this->assertEquals('HTTP/1.1 412 Precondition failed',$this->response->status);
}
/**
* @depends testPutWithIncorrectETag
*/
function testPutWithCorrectETag() {
// We need an etag-enabled file node.
$tree = new Sabre_DAV_ObjectTree(new Sabre_DAV_FSExt_Directory(SABRE_TEMPDIR));
$this->server->tree = $tree;
$etag = md5(file_get_contents(SABRE_TEMPDIR . '/test.txt'));
$serverVars = array(
'REQUEST_URI' => '/test.txt',
'REQUEST_METHOD' => 'PUT',
'HTTP_IF' => '(["'.$etag.'"])',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('newbody');
$this->server->httpRequest = $request;
$this->server->exec();
$this->assertEquals('HTTP/1.1 204 No Content',$this->response->status, 'Incorrect status received. Full response body:' . $this->response->body);
}
function testGetTimeoutHeader() {
$request = new Sabre_HTTP_Request(array(
'HTTP_TIMEOUT' => 'second-100',
));
$this->server->httpRequest = $request;
$this->assertEquals(100, $this->locksPlugin->getTimeoutHeader());
}
function testGetTimeoutHeaderNotSet() {
$request = new Sabre_HTTP_Request(array(
));
$this->server->httpRequest = $request;
$this->assertEquals(0, $this->locksPlugin->getTimeoutHeader());
}
function testGetTimeoutHeaderInfinite() {
$request = new Sabre_HTTP_Request(array(
'HTTP_TIMEOUT' => 'infinite',
));
$this->server->httpRequest = $request;
$this->assertEquals(Sabre_DAV_Locks_LockInfo::TIMEOUT_INFINITE, $this->locksPlugin->getTimeoutHeader());
}
/**
* @expectedException Sabre_DAV_Exception_BadRequest
*/
function testGetTimeoutHeaderInvalid() {
$request = new Sabre_HTTP_Request(array(
'HTTP_TIMEOUT' => 'yourmom',
));
$this->server->httpRequest = $request;
$this->locksPlugin->getTimeoutHeader();
}
}

View file

@ -0,0 +1,53 @@
<?php
require_once 'Sabre/DAV/AbstractServer.php';
class Sabre_DAV_Mount_PluginTest extends Sabre_DAV_AbstractServer {
function setUp() {
parent::setUp();
$this->server->addPlugin(new Sabre_DAV_Mount_Plugin());
}
function testPassThrough() {
$serverVars = array(
'REQUEST_URI' => '/',
'REQUEST_METHOD' => 'GET',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals('HTTP/1.1 501 Not Implemented',$this->response->status,'We expected GET to not be implemented for Directories. Response body: ' . $this->response->body);
}
function testMountResponse() {
$serverVars = array(
'REQUEST_URI' => '/?mount',
'REQUEST_METHOD' => 'GET',
'QUERY_STRING' => 'mount',
'HTTP_HOST' => 'example.org',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals('HTTP/1.1 200 OK',$this->response->status);
$xml = simplexml_load_string($this->response->body);
$this->assertTrue($xml==true,'Response was not a valid xml document');
$xml->registerXPathNamespace('dm','http://purl.org/NET/webdav/mount');
$url = $xml->xpath('//dm:url');
$this->assertEquals('http://example.org/',(string)$url[0]);
}
}

View file

@ -0,0 +1,98 @@
<?php
require_once 'Sabre/TestUtil.php';
class Sabre_DAV_ObjectTreeTest extends PHPUnit_Framework_TestCase {
protected $tree;
function setup() {
Sabre_TestUtil::clearTempDir();
mkdir(SABRE_TEMPDIR . '/root');
mkdir(SABRE_TEMPDIR . '/root/subdir');
file_put_contents(SABRE_TEMPDIR . '/root/file.txt','contents');
file_put_contents(SABRE_TEMPDIR . '/root/subdir/subfile.txt','subcontents');
$rootNode = new Sabre_DAV_FSExt_Directory(SABRE_TEMPDIR . '/root');
$this->tree = new Sabre_DAV_ObjectTree($rootNode);
}
function teardown() {
Sabre_TestUtil::clearTempDir();
}
function testGetRootNode() {
$root = $this->tree->getNodeForPath('');
$this->assertInstanceOf('Sabre_DAV_FSExt_Directory',$root);
}
function testGetSubDir() {
$root = $this->tree->getNodeForPath('subdir');
$this->assertInstanceOf('Sabre_DAV_FSExt_Directory',$root);
}
function testCopyFile() {
$this->tree->copy('file.txt','file2.txt');
$this->assertTrue(file_exists(SABRE_TEMPDIR.'/root/file2.txt'));
$this->assertEquals('contents',file_get_contents(SABRE_TEMPDIR.'/root/file2.txt'));
}
/**
* @depends testCopyFile
*/
function testCopyDirectory() {
$this->tree->copy('subdir','subdir2');
$this->assertTrue(file_exists(SABRE_TEMPDIR.'/root/subdir2'));
$this->assertTrue(file_exists(SABRE_TEMPDIR.'/root/subdir2/subfile.txt'));
$this->assertEquals('subcontents',file_get_contents(SABRE_TEMPDIR.'/root/subdir2/subfile.txt'));
}
/**
* @depends testCopyFile
*/
function testMoveFile() {
$this->tree->move('file.txt','file2.txt');
$this->assertTrue(file_exists(SABRE_TEMPDIR.'/root/file2.txt'));
$this->assertFalse(file_exists(SABRE_TEMPDIR.'/root/file.txt'));
$this->assertEquals('contents',file_get_contents(SABRE_TEMPDIR.'/root/file2.txt'));
}
/**
* @depends testMoveFile
*/
function testMoveFileNewParent() {
$this->tree->move('file.txt','subdir/file2.txt');
$this->assertTrue(file_exists(SABRE_TEMPDIR.'/root/subdir/file2.txt'));
$this->assertFalse(file_exists(SABRE_TEMPDIR.'/root/file.txt'));
$this->assertEquals('contents',file_get_contents(SABRE_TEMPDIR.'/root/subdir/file2.txt'));
}
/**
* @depends testCopyDirectory
*/
function testMoveDirectory() {
$this->tree->move('subdir','subdir2');
$this->assertTrue(file_exists(SABRE_TEMPDIR.'/root/subdir2'));
$this->assertTrue(file_exists(SABRE_TEMPDIR.'/root/subdir2/subfile.txt'));
$this->assertFalse(file_exists(SABRE_TEMPDIR.'/root/subdir'));
$this->assertEquals('subcontents',file_get_contents(SABRE_TEMPDIR.'/root/subdir2/subfile.txt'));
}
}

View file

@ -0,0 +1,76 @@
<?php
class Sabre_DAV_PartialUpdate_FileMock implements Sabre_DAV_PartialUpdate_IFile {
protected $data = '';
function put($str) {
if (is_resource($str)) {
$str = stream_get_contents($str);
}
$this->data = $str;
}
function putRange($str,$start) {
if (is_resource($str)) {
$str = stream_get_contents($str);
}
$this->data = substr($this->data, 0, $start) . $str . substr($this->data, $start + strlen($str));
}
function get() {
return $this->data;
}
function getContentType() {
return 'text/plain';
}
function getSize() {
return strlen($this->data);
}
function getETag() {
return '"' . $this->data . '"';
}
function delete() {
throw new Sabre_DAV_Exception_MethodNotAllowed();
}
function setName($name) {
throw new Sabre_DAV_Exception_MethodNotAllowed();
}
function getName() {
return 'partial';
}
function getLastModified() {
return null;
}
}

View file

@ -0,0 +1,125 @@
<?php
require_once 'Sabre/DAV/PartialUpdate/FileMock.php';
class Sabre_DAV_PartialUpdate_PluginTest extends Sabre_DAVServerTest {
protected $node;
protected $plugin;
public function setUp() {
$this->node = new Sabre_DAV_PartialUpdate_FileMock();
$this->tree[] = $this->node;
parent::setUp();
$this->plugin = new Sabre_DAV_PartialUpdate_Plugin();
$this->server->addPlugin($this->plugin);
}
public function testInit() {
$this->assertEquals('partialupdate', $this->plugin->getPluginName());
$this->assertEquals(array('sabredav-partialupdate'), $this->plugin->getFeatures());
$this->assertEquals(array(
'PATCH'
), $this->plugin->getHTTPMethods('partial'));
$this->assertEquals(array(
), $this->plugin->getHTTPMethods(''));
$this->assertNull($this->plugin->unknownMethod('FOO','partial'));
}
public function testPatchNoRange() {
$this->node->put('00000000');
$request = new Sabre_HTTP_Request(array(
'REQUEST_METHOD' => 'PATCH',
'REQUEST_URI' => '/partial',
));
$response = $this->request($request);
$this->assertEquals('HTTP/1.1 400 Bad request', $response->status, 'Full response body:' . $response->body);
}
public function testPatchNotSupported() {
$this->node->put('00000000');
$request = new Sabre_HTTP_Request(array(
'REQUEST_METHOD' => 'PATCH',
'REQUEST_URI' => '/',
'X_UPDATE_RANGE' => '3-4',
));
$request->setBody(
'111'
);
$response = $this->request($request);
$this->assertEquals('HTTP/1.1 405 Method Not Allowed', $response->status, 'Full response body:' . $response->body);
}
public function testPatchNoContentType() {
$this->node->put('00000000');
$request = new Sabre_HTTP_Request(array(
'REQUEST_METHOD' => 'PATCH',
'REQUEST_URI' => '/partial',
'HTTP_X_UPDATE_RANGE' => 'bytes=3-4',
));
$request->setBody(
'111'
);
$response = $this->request($request);
$this->assertEquals('HTTP/1.1 415 Unsupported Media Type', $response->status, 'Full response body:' . $response->body);
}
public function testPatchBadRange() {
$this->node->put('00000000');
$request = new Sabre_HTTP_Request(array(
'REQUEST_METHOD' => 'PATCH',
'REQUEST_URI' => '/partial',
'HTTP_X_UPDATE_RANGE' => 'bytes=3-4',
'HTTP_CONTENT_TYPE' => 'application/x-sabredav-partialupdate',
));
$request->setBody(
'111'
);
$response = $this->request($request);
$this->assertEquals('HTTP/1.1 416 Requested Range Not Satisfiable', $response->status, 'Full response body:' . $response->body);
}
public function testPatchSuccess() {
$this->node->put('00000000');
$request = new Sabre_HTTP_Request(array(
'REQUEST_METHOD' => 'PATCH',
'REQUEST_URI' => '/partial',
'HTTP_X_UPDATE_RANGE' => 'bytes=3-5',
'HTTP_CONTENT_TYPE' => 'application/x-sabredav-partialupdate',
'HTTP_CONTENT_LENGTH' => 3,
));
$request->setBody(
'111'
);
$response = $this->request($request);
$this->assertEquals('HTTP/1.1 204 No Content', $response->status, 'Full response body:' . $response->body);
$this->assertEquals('00111000', $this->node->get());
}
}

View file

@ -0,0 +1,63 @@
<?php
class Sabre_DAV_Property_GetLastModifiedTest extends PHPUnit_Framework_TestCase {
function testConstructDateTime() {
$dt = new DateTime('2010-03-14 16:35', new DateTimeZone('UTC'));
$lastMod = new Sabre_DAV_Property_GetLastModified($dt);
$this->assertEquals($dt->format(DateTime::ATOM), $lastMod->getTime()->format(DateTime::ATOM));
}
function testConstructString() {
$dt = new DateTime('2010-03-14 16:35', new DateTimeZone('UTC'));
$lastMod = new Sabre_DAV_Property_GetLastModified('2010-03-14 16:35');
$this->assertEquals($dt->format(DateTime::ATOM), $lastMod->getTime()->format(DateTime::ATOM));
}
function testConstructInt() {
$dt = new DateTime('2010-03-14 16:35', new DateTimeZone('UTC'));
$lastMod = new Sabre_DAV_Property_GetLastModified((int)$dt->format('U'));
$this->assertEquals($dt->format(DateTime::ATOM), $lastMod->getTime()->format(DateTime::ATOM));
}
function testSerialize() {
$dt = new DateTime('2010-03-14 16:35', new DateTimeZone('UTC'));
$lastMod = new Sabre_DAV_Property_GetLastModified($dt);
$doc = new DOMDocument();
$root = $doc->createElement('d:getlastmodified');
$root->setAttribute('xmlns:d','DAV:');
$doc->appendChild($root);
$objectTree = new Sabre_DAV_ObjectTree(new Sabre_DAV_SimpleCollection('rootdir'));
$server = new Sabre_DAV_Server($objectTree);
$lastMod->serialize($server, $root);
$xml = $doc->saveXML();
$this->assertEquals(
'<?xml version="1.0"?>
<d:getlastmodified xmlns:d="DAV:" xmlns:b="urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/" b:dt="dateTime.rfc1123">' .
Sabre_HTTP_Util::toHTTPDate($dt) .
'</d:getlastmodified>
', $xml);
$ok = false;
try {
Sabre_DAV_Property_GetLastModified::unserialize(Sabre_DAV_XMLUtil::loadDOMDocument($xml)->firstChild);
} catch (Sabre_DAV_Exception $e) {
$ok = true;
}
if (!$ok) $this->markTestFailed('Unserialize should not be supported');
}
}

View file

@ -0,0 +1,88 @@
<?php
class Sabre_DAV_Property_HrefListTest extends PHPUnit_Framework_TestCase {
function testConstruct() {
$href = new Sabre_DAV_Property_HrefList(array('foo','bar'));
$this->assertEquals(array('foo','bar'),$href->getHrefs());
}
function testSerialize() {
$href = new Sabre_DAV_Property_HrefList(array('foo','bar'));
$this->assertEquals(array('foo','bar'),$href->getHrefs());
$doc = new DOMDocument();
$root = $doc->createElement('d:anything');
$root->setAttribute('xmlns:d','DAV:');
$doc->appendChild($root);
$server = new Sabre_DAV_Server();
$server->setBaseUri('/bla/');
$href->serialize($server, $root);
$xml = $doc->saveXML();
$this->assertEquals(
'<?xml version="1.0"?>
<d:anything xmlns:d="DAV:"><d:href>/bla/foo</d:href><d:href>/bla/bar</d:href></d:anything>
', $xml);
}
function testSerializeNoPrefix() {
$href = new Sabre_DAV_Property_HrefList(array('foo','bar'), false);
$this->assertEquals(array('foo','bar'),$href->getHrefs());
$doc = new DOMDocument();
$root = $doc->createElement('d:anything');
$root->setAttribute('xmlns:d','DAV:');
$doc->appendChild($root);
$server = new Sabre_DAV_Server();
$server->setBaseUri('/bla/');
$href->serialize($server, $root);
$xml = $doc->saveXML();
$this->assertEquals(
'<?xml version="1.0"?>
<d:anything xmlns:d="DAV:"><d:href>foo</d:href><d:href>bar</d:href></d:anything>
', $xml);
}
function testUnserialize() {
$xml = '<?xml version="1.0"?>
<d:anything xmlns:d="urn:DAV"><d:href>/bla/foo</d:href><d:href>/bla/bar</d:href></d:anything>
';
$dom = new DOMDocument();
$dom->loadXML($xml);
$href = Sabre_DAV_Property_HrefList::unserialize($dom->firstChild);
$this->assertEquals(array('/bla/foo','/bla/bar'),$href->getHrefs());
}
function testUnserializeIncompatible() {
$xml = '<?xml version="1.0"?>
<d:anything xmlns:d="urn:DAV"><d:href2>/bla/foo</d:href2></d:anything>
';
$dom = new DOMDocument();
$dom->loadXML($xml);
$href = Sabre_DAV_Property_HrefList::unserialize($dom->firstChild);
$this->assertEquals(array(), $href->getHrefs());
}
}

View file

@ -0,0 +1,88 @@
<?php
class Sabre_DAV_Property_HrefTest extends PHPUnit_Framework_TestCase {
function testConstruct() {
$href = new Sabre_DAV_Property_Href('path');
$this->assertEquals('path',$href->getHref());
}
function testSerialize() {
$href = new Sabre_DAV_Property_Href('path');
$this->assertEquals('path',$href->getHref());
$doc = new DOMDocument();
$root = $doc->createElement('d:anything');
$root->setAttribute('xmlns:d','DAV:');
$doc->appendChild($root);
$server = new Sabre_DAV_Server();
$server->setBaseUri('/bla/');
$href->serialize($server, $root);
$xml = $doc->saveXML();
$this->assertEquals(
'<?xml version="1.0"?>
<d:anything xmlns:d="DAV:"><d:href>/bla/path</d:href></d:anything>
', $xml);
}
function testSerializeNoPrefix() {
$href = new Sabre_DAV_Property_Href('path',false);
$this->assertEquals('path',$href->getHref());
$doc = new DOMDocument();
$root = $doc->createElement('d:anything');
$root->setAttribute('xmlns:d','DAV:');
$doc->appendChild($root);
$server = new Sabre_DAV_Server();
$server->setBaseUri('/bla/');
$href->serialize($server, $root);
$xml = $doc->saveXML();
$this->assertEquals(
'<?xml version="1.0"?>
<d:anything xmlns:d="DAV:"><d:href>path</d:href></d:anything>
', $xml);
}
function testUnserialize() {
$xml = '<?xml version="1.0"?>
<d:anything xmlns:d="urn:DAV"><d:href>/bla/path</d:href></d:anything>
';
$dom = new DOMDocument();
$dom->loadXML($xml);
$href = Sabre_DAV_Property_Href::unserialize($dom->firstChild);
$this->assertEquals('/bla/path',$href->getHref());
}
function testUnserializeIncompatible() {
$xml = '<?xml version="1.0"?>
<d:anything xmlns:d="urn:DAV"><d:href2>/bla/path</d:href2></d:anything>
';
$dom = new DOMDocument();
$dom->loadXML($xml);
$href = Sabre_DAV_Property_Href::unserialize($dom->firstChild);
$this->assertNull($href);
}
}

View file

@ -0,0 +1,107 @@
<?php
class Sabre_DAV_Property_ResourceTypeTest extends PHPUnit_Framework_TestCase {
function testConstruct() {
$resourceType = new Sabre_DAV_Property_ResourceType(array('{DAV:}collection'));
$this->assertEquals(array('{DAV:}collection'),$resourceType->getValue());
$resourceType = new Sabre_DAV_Property_ResourceType(Sabre_DAV_Server::NODE_FILE);
$this->assertEquals(array(),$resourceType->getValue());
$resourceType = new Sabre_DAV_Property_ResourceType(Sabre_DAV_Server::NODE_DIRECTORY);
$this->assertEquals(array('{DAV:}collection'),$resourceType->getValue());
$resourceType = new Sabre_DAV_Property_ResourceType('{DAV:}principal');
$this->assertEquals(array('{DAV:}principal'),$resourceType->getValue());
}
/**
* @depends testConstruct
*/
function testSerialize() {
$resourceType = new Sabre_DAV_Property_ResourceType(array('{DAV:}collection','{DAV:}principal'));
$doc = new DOMDocument();
$root = $doc->createElement('d:anything');
$root->setAttribute('xmlns:d','DAV:');
$doc->appendChild($root);
$server = new Sabre_DAV_Server();
$resourceType->serialize($server, $root);
$xml = $doc->saveXML();
$this->assertEquals(
'<?xml version="1.0"?>
<d:anything xmlns:d="DAV:"><d:collection/><d:principal/></d:anything>
', $xml);
}
/**
* @depends testSerialize
*/
function testSerializeCustomNS() {
$resourceType = new Sabre_DAV_Property_ResourceType(array('{http://example.org/NS}article'));
$doc = new DOMDocument();
$root = $doc->createElement('d:anything');
$root->setAttribute('xmlns:d','DAV:');
$doc->appendChild($root);
$server = new Sabre_DAV_Server();
$resourceType->serialize($server, $root);
$xml = $doc->saveXML();
$this->assertEquals(
'<?xml version="1.0"?>
<d:anything xmlns:d="DAV:"><custom:article xmlns:custom="http://example.org/NS"/></d:anything>
', $xml);
}
/**
* @depends testConstruct
*/
function testIs() {
$resourceType = new Sabre_DAV_Property_ResourceType(array('{DAV:}collection','{DAV:}principal'));
$this->assertTrue($resourceType->is('{DAV:}collection'));
$this->assertFalse($resourceType->is('{DAV:}blabla'));
}
/**
* @depends testConstruct
*/
function testAdd() {
$resourceType = new Sabre_DAV_Property_ResourceType(array('{DAV:}collection','{DAV:}principal'));
$resourceType->add('{DAV:}foo');
$this->assertEquals(array('{DAV:}collection','{DAV:}principal','{DAV:}foo'), $resourceType->getValue());
}
/**
* @depends testConstruct
*/
function testUnserialize() {
$xml ='<?xml version="1.0"?>
<d:anything xmlns:d="DAV:"><d:collection/><d:principal/></d:anything>
';
$dom = Sabre_DAV_XMLUtil::loadDOMDocument($xml);
$resourceType = Sabre_DAV_Property_ResourceType::unserialize($dom->firstChild);
$this->assertEquals(array('{DAV:}collection','{DAV:}principal'),$resourceType->getValue());
}
}

View file

@ -0,0 +1,231 @@
<?php
class Sabre_DAV_Property_ResponseTest extends PHPUnit_Framework_TestCase {
function testSimple() {
$innerProps = array(
200 => array(
'{DAV:}displayname' => 'my file',
),
404 => array(
'{DAV:}owner' => null,
)
);
$property = new Sabre_DAV_Property_Response('uri',$innerProps);
$this->assertEquals('uri',$property->getHref());
$this->assertEquals($innerProps,$property->getResponseProperties());
}
/**
* @depends testSimple
*/
function testSerialize() {
$innerProps = array(
200 => array(
'{DAV:}displayname' => 'my file',
),
404 => array(
'{DAV:}owner' => null,
)
);
$property = new Sabre_DAV_Property_Response('uri',$innerProps);
$doc = new DOMDocument();
$root = $doc->createElement('d:root');
$root->setAttribute('xmlns:d','DAV:');
$doc->appendChild($root);
$objectTree = new Sabre_DAV_ObjectTree(new Sabre_DAV_SimpleCollection('rootdir'));
$server = new Sabre_DAV_Server($objectTree);
$property->serialize($server, $root);
$xml = $doc->saveXML();
$this->assertEquals(
'<?xml version="1.0"?>
<d:root xmlns:d="DAV:">' .
'<d:response>' .
'<d:href>/uri</d:href>' .
'<d:propstat>' .
'<d:prop>' .
'<d:displayname>my file</d:displayname>' .
'</d:prop>' .
'<d:status>HTTP/1.1 200 OK</d:status>' .
'</d:propstat>' .
'<d:propstat>' .
'<d:prop>' .
'<d:owner/>' .
'</d:prop>' .
'<d:status>HTTP/1.1 404 Not Found</d:status>' .
'</d:propstat>' .
'</d:response>' .
'</d:root>
', $xml);
}
/**
* This one is specifically for testing properties with no namespaces, which is legal xml
*
* @depends testSerialize
*/
function testSerializeEmptyNamespace() {
$innerProps = array(
200 => array(
'{}propertyname' => 'value',
),
);
$property = new Sabre_DAV_Property_Response('uri',$innerProps);
$doc = new DOMDocument();
$root = $doc->createElement('d:root');
$root->setAttribute('xmlns:d','DAV:');
$doc->appendChild($root);
$objectTree = new Sabre_DAV_ObjectTree(new Sabre_DAV_SimpleCollection('rootdir'));
$server = new Sabre_DAV_Server($objectTree);
$property->serialize($server, $root);
$xml = $doc->saveXML();
$this->assertEquals(
'<?xml version="1.0"?>
<d:root xmlns:d="DAV:">' .
'<d:response>' .
'<d:href>/uri</d:href>' .
'<d:propstat>' .
'<d:prop>' .
'<propertyname xmlns="">value</propertyname>' .
'</d:prop>' .
'<d:status>HTTP/1.1 200 OK</d:status>' .
'</d:propstat>' .
'</d:response>' .
'</d:root>
', $xml);
}
/**
* This one is specifically for testing properties with no namespaces, which is legal xml
*
* @depends testSerialize
*/
function testSerializeCustomNamespace() {
$innerProps = array(
200 => array(
'{http://sabredav.org/NS/example}propertyname' => 'value',
),
);
$property = new Sabre_DAV_Property_Response('uri',$innerProps);
$doc = new DOMDocument();
$root = $doc->createElement('d:root');
$root->setAttribute('xmlns:d','DAV:');
$doc->appendChild($root);
$objectTree = new Sabre_DAV_ObjectTree(new Sabre_DAV_SimpleCollection('rootdir'));
$server = new Sabre_DAV_Server($objectTree);
$property->serialize($server, $root);
$xml = $doc->saveXML();
$this->assertEquals(
'<?xml version="1.0"?>
<d:root xmlns:d="DAV:">' .
'<d:response>' .
'<d:href>/uri</d:href>' .
'<d:propstat>' .
'<d:prop>' .
'<x2:propertyname xmlns:x2="http://sabredav.org/NS/example">value</x2:propertyname>' .
'</d:prop>' .
'<d:status>HTTP/1.1 200 OK</d:status>' .
'</d:propstat>' .
'</d:response>' .
'</d:root>
', $xml);
}
/**
* @depends testSerialize
*/
function testSerializeComplexProperty() {
$innerProps = array(
200 => array(
'{DAV:}link' => new Sabre_DAV_Property_Href('http://sabredav.org/', false)
),
);
$property = new Sabre_DAV_Property_Response('uri',$innerProps);
$doc = new DOMDocument();
$root = $doc->createElement('d:root');
$root->setAttribute('xmlns:d','DAV:');
$doc->appendChild($root);
$objectTree = new Sabre_DAV_ObjectTree(new Sabre_DAV_SimpleCollection('rootdir'));
$server = new Sabre_DAV_Server($objectTree);
$property->serialize($server, $root);
$xml = $doc->saveXML();
$this->assertEquals(
'<?xml version="1.0"?>
<d:root xmlns:d="DAV:">' .
'<d:response>' .
'<d:href>/uri</d:href>' .
'<d:propstat>' .
'<d:prop>' .
'<d:link><d:href>http://sabredav.org/</d:href></d:link>' .
'</d:prop>' .
'<d:status>HTTP/1.1 200 OK</d:status>' .
'</d:propstat>' .
'</d:response>' .
'</d:root>
', $xml);
}
/**
* @depends testSerialize
* @expectedException Sabre_DAV_Exception
*/
function testSerializeBreak() {
$innerProps = array(
200 => array(
'{DAV:}link' => new STDClass()
),
);
$property = new Sabre_DAV_Property_Response('uri',$innerProps);
$doc = new DOMDocument();
$root = $doc->createElement('d:root');
$root->setAttribute('xmlns:d','DAV:');
$doc->appendChild($root);
$objectTree = new Sabre_DAV_ObjectTree(new Sabre_DAV_SimpleCollection('rootdir'));
$server = new Sabre_DAV_Server($objectTree);
$property->serialize($server, $root);
}
}

View file

@ -0,0 +1,123 @@
<?php
require_once 'Sabre/HTTP/ResponseMock.php';
require_once 'Sabre/DAV/AbstractServer.php';
class Sabre_DAV_Property_SupportedReportSetTest extends Sabre_DAV_AbstractServer {
public function sendPROPFIND($body) {
$serverVars = array(
'REQUEST_URI' => '/',
'REQUEST_METHOD' => 'PROPFIND',
'HTTP_DEPTH' => '0',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody($body);
$this->server->httpRequest = ($request);
$this->server->exec();
}
/**
* @covers Sabre_DAV_Property_SupportedReportSet
*/
function testNoReports() {
$xml = '<?xml version="1.0"?>
<d:propfind xmlns:d="DAV:">
<d:prop>
<d:supported-report-set />
</d:prop>
</d:propfind>';
$this->sendPROPFIND($xml);
$this->assertEquals('HTTP/1.1 207 Multi-Status',$this->response->status,'We expected a multi-status response. Full response body: ' . $this->response->body);
$body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/","xmlns\\1=\"urn:DAV\"",$this->response->body);
$xml = simplexml_load_string($body);
$xml->registerXPathNamespace('d','urn:DAV');
$data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop');
$this->assertEquals(1,count($data),'We expected 1 \'d:prop\' element');
$data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supported-report-set');
$this->assertEquals(1,count($data),'We expected 1 \'d:supported-report-set\' element');
$data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:status');
$this->assertEquals(1,count($data),'We expected 1 \'d:status\' element');
$this->assertEquals('HTTP/1.1 200 OK',(string)$data[0],'The status for this property should have been 200');
}
/**
* @covers Sabre_DAV_Property_SupportedReportSet
* @depends testNoReports
*/
function testCustomReport() {
// Intercepting the report property
$this->server->subscribeEvent('afterGetProperties',array($this,'addProp'));
$xml = '<?xml version="1.0"?>
<d:propfind xmlns:d="DAV:">
<d:prop>
<d:supported-report-set />
</d:prop>
</d:propfind>';
$this->sendPROPFIND($xml);
$this->assertEquals('HTTP/1.1 207 Multi-Status',$this->response->status,'We expected a multi-status response. Full response body: ' . $this->response->body);
$body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/","xmlns\\1=\"urn:DAV\"",$this->response->body);
$xml = simplexml_load_string($body);
$xml->registerXPathNamespace('d','urn:DAV');
$xml->registerXPathNamespace('x','http://www.rooftopsolutions.nl/testnamespace');
$data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop');
$this->assertEquals(1,count($data),'We expected 1 \'d:prop\' element');
$data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supported-report-set');
$this->assertEquals(1,count($data),'We expected 1 \'d:supported-report-set\' element');
$data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supported-report-set/d:supported-report');
$this->assertEquals(2,count($data),'We expected 2 \'d:supported-report\' elements');
$data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supported-report-set/d:supported-report/d:report');
$this->assertEquals(2,count($data),'We expected 2 \'d:report\' elements');
$data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supported-report-set/d:supported-report/d:report/x:myreport');
$this->assertEquals(1,count($data),'We expected 1 \'x:myreport\' element. Full body: ' . $this->response->body);
$data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supported-report-set/d:supported-report/d:report/d:anotherreport');
$this->assertEquals(1,count($data),'We expected 1 \'d:anotherreport\' element. Full body: ' . $this->response->body);
$data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:status');
$this->assertEquals(1,count($data),'We expected 1 \'d:status\' element');
$this->assertEquals('HTTP/1.1 200 OK',(string)$data[0],'The status for this property should have been 200');
}
/**
* This method is used as a callback for afterGetProperties
*/
function addProp($path, &$properties) {
if (isset($properties[200]['{DAV:}supported-report-set'])) {
$properties[200]['{DAV:}supported-report-set']->addReport('{http://www.rooftopsolutions.nl/testnamespace}myreport');
$properties[200]['{DAV:}supported-report-set']->addReport('{DAV:}anotherreport');
}
}
}
?>

View file

@ -0,0 +1,264 @@
<?php
require_once 'Sabre/HTTP/ResponseMock.php';
class Sabre_DAV_ServerCopyMoveTest extends PHPUnit_Framework_TestCase {
private $response;
/**
* @var Sabre_DAV_Server
*/
private $server;
function setUp() {
$this->response = new Sabre_HTTP_ResponseMock();
$dir = new Sabre_DAV_FS_Directory(SABRE_TEMPDIR);
$tree = new Sabre_DAV_ObjectTree($dir);
$this->server = new Sabre_DAV_Server($tree);
$this->server->debugExceptions = true;
$this->server->httpResponse = $this->response;
file_put_contents(SABRE_TEMPDIR . '/test.txt', 'Test contents');
file_put_contents(SABRE_TEMPDIR . '/test2.txt', 'Test contents2');
mkdir(SABRE_TEMPDIR . '/col');
file_put_contents(SABRE_TEMPDIR . 'col/test.txt', 'Test contents');
}
function tearDown() {
$cleanUp = array('test.txt','testput.txt','testcol','test2.txt','test3.txt','col/test.txt','col','col2/test.txt','col2');
foreach($cleanUp as $file) {
$tmpFile = SABRE_TEMPDIR . '/' . $file;
if (file_exists($tmpFile)) {
if (is_dir($tmpFile)) {
rmdir($tmpFile);
} else {
unlink($tmpFile);
}
}
}
}
function testCopyOverWrite() {
$serverVars = array(
'REQUEST_URI' => '/test.txt',
'REQUEST_METHOD' => 'COPY',
'HTTP_DESTINATION' => '/test2.txt',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals('HTTP/1.1 204 No Content',$this->response->status,'Received an incorrect HTTP status. Full body inspection: ' . $this->response->body);
$this->assertEquals(array(
'Content-Length' => '0',
),
$this->response->headers
);
$this->assertEquals('Test contents',file_get_contents(SABRE_TEMPDIR. '/test2.txt'));
}
function testCopyToSelf() {
$serverVars = array(
'REQUEST_URI' => '/test.txt',
'REQUEST_METHOD' => 'COPY',
'HTTP_DESTINATION' => '/test.txt',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals('HTTP/1.1 403 Forbidden',$this->response->status,'Received an incorrect HTTP status. Full body inspection: ' . $this->response->body);
$this->assertEquals('Test contents',file_get_contents(SABRE_TEMPDIR. '/test.txt'));
}
function testMoveToSelf() {
$serverVars = array(
'REQUEST_URI' => '/test.txt',
'REQUEST_METHOD' => 'MOVE',
'HTTP_DESTINATION' => '/test.txt',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals('HTTP/1.1 403 Forbidden',$this->response->status,'Received an incorrect HTTP status. Full body inspection: ' . $this->response->body);
$this->assertEquals('Test contents',file_get_contents(SABRE_TEMPDIR. '/test.txt'));
}
function testMoveOverWrite() {
$serverVars = array(
'REQUEST_URI' => '/test.txt',
'REQUEST_METHOD' => 'MOVE',
'HTTP_DESTINATION' => '/test2.txt',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals(array(
'Content-Length' => 0,
),
$this->response->headers
);
$this->assertEquals('HTTP/1.1 204 No Content',$this->response->status);
$this->assertEquals('Test contents',file_get_contents(SABRE_TEMPDIR . '/test2.txt'));
$this->assertFalse(file_exists(SABRE_TEMPDIR . '/test.txt'),'The sourcefile test.txt should no longer exist at this point');
}
function testBlockedOverWrite() {
$serverVars = array(
'REQUEST_URI' => '/test.txt',
'REQUEST_METHOD' => 'COPY',
'HTTP_DESTINATION' => '/test2.txt',
'HTTP_OVERWRITE' => 'F',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals(array(
'Content-Type' => 'application/xml; charset=utf-8',
),
$this->response->headers
);
$this->assertEquals('HTTP/1.1 412 Precondition failed',$this->response->status);
$this->assertEquals('Test contents2',file_get_contents(SABRE_TEMPDIR . '/test2.txt'));
}
function testNonExistantParent() {
$serverVars = array(
'REQUEST_URI' => '/test.txt',
'REQUEST_METHOD' => 'COPY',
'HTTP_DESTINATION' => '/testcol2/test2.txt',
'HTTP_OVERWRITE' => 'F',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals(array(
'Content-Type' => 'application/xml; charset=utf-8',
),
$this->response->headers
);
$this->assertEquals('HTTP/1.1 409 Conflict',$this->response->status);
}
function testRandomOverwriteHeader() {
$serverVars = array(
'REQUEST_URI' => '/test.txt',
'REQUEST_METHOD' => 'COPY',
'HTTP_DESTINATION' => '/testcol2/test2.txt',
'HTTP_OVERWRITE' => 'SURE!',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals('HTTP/1.1 400 Bad request',$this->response->status);
}
function testCopyDirectory() {
$serverVars = array(
'REQUEST_URI' => '/col',
'REQUEST_METHOD' => 'COPY',
'HTTP_DESTINATION' => '/col2',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals(array(
'Content-Length' => '0',
),
$this->response->headers
);
$this->assertEquals('HTTP/1.1 201 Created',$this->response->status);
$this->assertEquals('Test contents',file_get_contents(SABRE_TEMPDIR . '/col2/test.txt'));
}
function testSimpleCopyFile() {
$serverVars = array(
'REQUEST_URI' => '/test.txt',
'REQUEST_METHOD' => 'COPY',
'HTTP_DESTINATION' => '/test3.txt',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals(array(
'Content-Length' => '0',
),
$this->response->headers
);
$this->assertEquals('HTTP/1.1 201 Created',$this->response->status);
$this->assertEquals('Test contents',file_get_contents(SABRE_TEMPDIR . '/test3.txt'));
}
function testSimpleCopyCollection() {
$serverVars = array(
'REQUEST_URI' => '/col',
'REQUEST_METHOD' => 'COPY',
'HTTP_DESTINATION' => '/col2',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals('HTTP/1.1 201 Created',$this->response->status,'Incorrect status received. Full response body: ' . $this->response->body);
$this->assertEquals(array(
'Content-Length' => '0',
),
$this->response->headers
);
$this->assertEquals('Test contents',file_get_contents(SABRE_TEMPDIR . '/col2/test.txt'));
}
}

View file

@ -0,0 +1,51 @@
<?php
require_once 'Sabre/DAV/AbstractServer.php';
class Sabre_DAV_ServerEventsTest extends Sabre_DAV_AbstractServer {
private $tempPath;
function testAfterBind() {
$this->server->subscribeEvent('afterBind',array($this,'afterBindHandler'));
$newPath = 'afterBind';
$this->tempPath = '';
$this->server->createFile($newPath,'body');
$this->assertEquals($newPath, $this->tempPath);
}
function afterBindHandler($path) {
$this->tempPath = $path;
}
function testBeforeBindCancel() {
$this->server->subscribeEvent('beforeBind', array($this,'beforeBindCancelHandler'));
$this->assertFalse($this->server->createFile('bla','body'));
// Also testing put()
$req = new Sabre_HTTP_Request(array(
'REQUEST_METHOD' => 'PUT',
'REQUEST_URI' => '/foobar',
));
$this->server->httpRequest = $req;
$this->server->exec();
$this->assertEquals('',$this->server->httpResponse->status);
}
function beforeBindCancelHandler() {
return false;
}
}

View file

@ -0,0 +1,50 @@
<?php
require_once 'Sabre/HTTP/ResponseMock.php';
require_once 'Sabre/DAV/AbstractServer.php';
require_once 'Sabre/DAV/Exception.php';
class Sabre_DAV_ServerFinderBlockTest extends Sabre_DAV_AbstractServer{
function testPut() {
$serverVars = array(
'REQUEST_URI' => '/testput.txt',
'REQUEST_METHOD' => 'PUT',
'HTTP_X_EXPECTED_ENTITY_LENGTH' => '20',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('Testing finder');
$this->server->httpRequest = $request;
$this->server->exec();
$this->assertEquals('', $this->response->body);
$this->assertEquals('HTTP/1.1 201 Created',$this->response->status);
$this->assertEquals('0', $this->response->headers['Content-Length']);
$this->assertEquals('Testing finder',file_get_contents(SABRE_TEMPDIR . '/testput.txt'));
}
function testPutFail() {
$serverVars = array(
'REQUEST_URI' => '/testput.txt',
'REQUEST_METHOD' => 'PUT',
'HTTP_X_EXPECTED_ENTITY_LENGTH' => '20',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('');
$this->server->httpRequest = $request;
$this->server->exec();
$this->assertEquals('HTTP/1.1 403 Forbidden',$this->response->status);
$this->assertEquals(array(
'Content-Type' => 'application/xml; charset=utf-8',
),$this->response->headers);
$this->assertFalse(file_exists(SABRE_TEMPDIR . '/testput.txt'));
}
}

View file

@ -0,0 +1,336 @@
<?php
require_once 'Sabre/HTTP/ResponseMock.php';
require_once 'Sabre/DAV/AbstractServer.php';
require_once 'Sabre/DAV/Exception.php';
class Sabre_DAV_ServerMKCOLTest extends Sabre_DAV_AbstractServer {
function testMkcol() {
$serverVars = array(
'REQUEST_URI' => '/testcol',
'REQUEST_METHOD' => 'MKCOL',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody("");
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals(array(
'Content-Length' => '0',
),$this->response->headers);
$this->assertEquals('HTTP/1.1 201 Created',$this->response->status);
$this->assertEquals('', $this->response->body);
$this->assertTrue(is_dir($this->tempDir . '/testcol'));
}
/**
* @depends testMkcol
*/
function testMKCOLUnknownBody() {
$serverVars = array(
'REQUEST_URI' => '/testcol',
'REQUEST_METHOD' => 'MKCOL',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody("Hello");
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals(array(
'Content-Type' => 'application/xml; charset=utf-8',
),$this->response->headers);
$this->assertEquals('HTTP/1.1 415 Unsupported Media Type',$this->response->status);
}
/**
* @depends testMkcol
*/
function testMKCOLBrokenXML() {
$serverVars = array(
'REQUEST_URI' => '/testcol',
'REQUEST_METHOD' => 'MKCOL',
'HTTP_CONTENT_TYPE' => 'application/xml',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody("Hello");
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals(array(
'Content-Type' => 'application/xml; charset=utf-8',
),$this->response->headers);
$this->assertEquals('HTTP/1.1 400 Bad request',$this->response->status);
}
/**
* @depends testMkcol
*/
function testMKCOLUnknownXML() {
$serverVars = array(
'REQUEST_URI' => '/testcol',
'REQUEST_METHOD' => 'MKCOL',
'HTTP_CONTENT_TYPE' => 'application/xml',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('<?xml version="1.0"?><html></html>');
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals(array(
'Content-Type' => 'application/xml; charset=utf-8',
),$this->response->headers);
$this->assertEquals('HTTP/1.1 415 Unsupported Media Type',$this->response->status);
}
/**
* @depends testMkcol
*/
function testMKCOLNoResourceType() {
$serverVars = array(
'REQUEST_URI' => '/testcol',
'REQUEST_METHOD' => 'MKCOL',
'HTTP_CONTENT_TYPE' => 'application/xml',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('<?xml version="1.0"?>
<mkcol xmlns="DAV:">
<set>
<prop>
<displayname>Evert</displayname>
</prop>
</set>
</mkcol>');
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals(array(
'Content-Type' => 'application/xml; charset=utf-8',
),$this->response->headers);
$this->assertEquals('HTTP/1.1 400 Bad request',$this->response->status,'Wrong statuscode received. Full response body: ' .$this->response->body);
}
/**
* @depends testMKCOLNoResourceType
*/
function testMKCOLIncorrectResourceType() {
$serverVars = array(
'REQUEST_URI' => '/testcol',
'REQUEST_METHOD' => 'MKCOL',
'HTTP_CONTENT_TYPE' => 'application/xml',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('<?xml version="1.0"?>
<mkcol xmlns="DAV:">
<set>
<prop>
<resourcetype><blabla /></resourcetype>
</prop>
</set>
</mkcol>');
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals(array(
'Content-Type' => 'application/xml; charset=utf-8',
),$this->response->headers);
$this->assertEquals('HTTP/1.1 403 Forbidden',$this->response->status,'Wrong statuscode received. Full response body: ' .$this->response->body);
}
/**
* @depends testMKCOLIncorrectResourceType
*/
function testMKCOLIncorrectResourceType2() {
$serverVars = array(
'REQUEST_URI' => '/testcol',
'REQUEST_METHOD' => 'MKCOL',
'HTTP_CONTENT_TYPE' => 'application/xml',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('<?xml version="1.0"?>
<mkcol xmlns="DAV:">
<set>
<prop>
<resourcetype><collection /><blabla /></resourcetype>
</prop>
</set>
</mkcol>');
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals(array(
'Content-Type' => 'application/xml; charset=utf-8',
),$this->response->headers);
$this->assertEquals('HTTP/1.1 403 Forbidden',$this->response->status,'Wrong statuscode received. Full response body: ' .$this->response->body);
}
/**
* @depends testMKCOLIncorrectResourceType2
*/
function testMKCOLSuccess() {
$serverVars = array(
'REQUEST_URI' => '/testcol',
'REQUEST_METHOD' => 'MKCOL',
'HTTP_CONTENT_TYPE' => 'application/xml',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('<?xml version="1.0"?>
<mkcol xmlns="DAV:">
<set>
<prop>
<resourcetype><collection /></resourcetype>
</prop>
</set>
</mkcol>');
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals(array(
'Content-Length' => '0',
),$this->response->headers);
$this->assertEquals('HTTP/1.1 201 Created',$this->response->status,'Wrong statuscode received. Full response body: ' .$this->response->body);
}
/**
* @depends testMKCOLIncorrectResourceType2
*/
function testMKCOLNoParent() {
$serverVars = array(
'REQUEST_URI' => '/testnoparent/409me',
'REQUEST_METHOD' => 'MKCOL',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('');
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals(array(
'Content-Type' => 'application/xml; charset=utf-8',
),$this->response->headers);
$this->assertEquals('HTTP/1.1 409 Conflict',$this->response->status,'Wrong statuscode received. Full response body: ' .$this->response->body);
}
/**
* @depends testMKCOLIncorrectResourceType2
*/
function testMKCOLParentIsNoCollection() {
$serverVars = array(
'REQUEST_URI' => '/test.txt/409me',
'REQUEST_METHOD' => 'MKCOL',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('');
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals(array(
'Content-Type' => 'application/xml; charset=utf-8',
),$this->response->headers);
$this->assertEquals('HTTP/1.1 409 Conflict',$this->response->status,'Wrong statuscode received. Full response body: ' .$this->response->body);
}
/**
* @depends testMKCOLIncorrectResourceType2
*/
function testMKCOLAlreadyExists() {
$serverVars = array(
'REQUEST_URI' => '/test.txt',
'REQUEST_METHOD' => 'MKCOL',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('');
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals(array(
'Content-Type' => 'application/xml; charset=utf-8',
'Allow' => 'OPTIONS, GET, HEAD, DELETE, PROPFIND, PUT, PROPPATCH, COPY, MOVE, REPORT',
),$this->response->headers);
$this->assertEquals('HTTP/1.1 405 Method Not Allowed',$this->response->status,'Wrong statuscode received. Full response body: ' .$this->response->body);
}
/**
* @depends testMKCOLSuccess
* @depends testMKCOLAlreadyExists
*/
function testMKCOLAndProps() {
$serverVars = array(
'REQUEST_URI' => '/testcol',
'REQUEST_METHOD' => 'MKCOL',
'HTTP_CONTENT_TYPE' => 'application/xml',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('<?xml version="1.0"?>
<mkcol xmlns="DAV:">
<set>
<prop>
<resourcetype><collection /></resourcetype>
<displayname>my new collection</displayname>
</prop>
</set>
</mkcol>');
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals('HTTP/1.1 207 Multi-Status',$this->response->status,'Wrong statuscode received. Full response body: ' .$this->response->body);
$this->assertEquals(array(
'Content-Type' => 'application/xml; charset=utf-8',
),$this->response->headers);
}
}

View file

@ -0,0 +1,95 @@
<?php
require_once 'Sabre/DAV/AbstractServer.php';
require_once 'Sabre/DAV/TestPlugin.php';
class Sabre_DAV_ServerPluginTest extends Sabre_DAV_AbstractServer {
/**
* @var Sabre_DAV_TestPlugin
*/
protected $testPlugin;
function setUp() {
parent::setUp();
$testPlugin = new Sabre_DAV_TestPlugin();
$this->server->addPlugin($testPlugin);
$this->testPlugin = $testPlugin;
}
/**
* @covers Sabre_DAV_ServerPlugin
*/
function testBaseClass() {
$p = new Sabre_DAV_ServerPluginMock();
$this->assertEquals(array(),$p->getFeatures());
$this->assertEquals(array(),$p->getHTTPMethods(''));
}
function testOptions() {
$serverVars = array(
'REQUEST_URI' => '/',
'REQUEST_METHOD' => 'OPTIONS',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals(array(
'DAV' => '1, 3, extended-mkcol, drinking',
'MS-Author-Via' => 'DAV',
'Allow' => 'OPTIONS, GET, HEAD, DELETE, PROPFIND, PUT, PROPPATCH, COPY, MOVE, REPORT, BEER, WINE',
'Accept-Ranges' => 'bytes',
'Content-Length' => '0',
'X-Sabre-Version' => Sabre_DAV_Version::VERSION,
),$this->response->headers);
$this->assertEquals('HTTP/1.1 200 OK',$this->response->status);
$this->assertEquals('', $this->response->body);
$this->assertEquals('OPTIONS',$this->testPlugin->beforeMethod);
}
function testGetPlugin() {
$this->assertEquals($this->testPlugin,$this->server->getPlugin(get_class($this->testPlugin)));
}
function testUnknownPlugin() {
$this->assertNull($this->server->getPlugin('SomeRandomClassName'));
}
function testGetSupportedReportSet() {
$this->assertEquals(array(), $this->testPlugin->getSupportedReportSet('/'));
}
function testGetPlugins() {
$this->assertEquals(
array(get_class($this->testPlugin) => $this->testPlugin),
$this->server->getPlugins()
);
}
}
class Sabre_DAV_ServerPluginMock extends Sabre_DAV_ServerPlugin {
function initialize(Sabre_DAV_Server $s) { }
}

View file

@ -0,0 +1,391 @@
<?php
require_once 'Sabre/HTTP/ResponseMock.php';
class Sabre_DAV_ServerPreconditionsTest extends PHPUnit_Framework_TestCase {
/**
* @covers Sabre_DAV_Server::checkPreconditions
* @expectedException Sabre_DAV_Exception_PreconditionFailed
*/
function testIfMatchNoNode() {
$root = new Sabre_DAV_SimpleCollection('root',array(new Sabre_DAV_ServerPreconditionsNode()));
$server = new Sabre_DAV_Server($root);
$httpRequest = new Sabre_HTTP_Request(array(
'HTTP_IF_MATCH' => '*',
'REQUEST_URI' => '/bar'
));
$server->httpRequest = $httpRequest;
$server->checkPreconditions();
}
/**
* @covers Sabre_DAV_Server::checkPreconditions
*/
function testIfMatchHasNode() {
$root = new Sabre_DAV_SimpleCollection('root',array(new Sabre_DAV_ServerPreconditionsNode()));
$server = new Sabre_DAV_Server($root);
$httpRequest = new Sabre_HTTP_Request(array(
'HTTP_IF_MATCH' => '*',
'REQUEST_URI' => '/foo'
));
$server->httpRequest = $httpRequest;
$this->assertTrue($server->checkPreconditions());
}
/**
* @covers Sabre_DAV_Server::checkPreconditions
* @expectedException Sabre_DAV_Exception_PreconditionFailed
*/
function testIfMatchWrongEtag() {
$root = new Sabre_DAV_SimpleCollection('root',array(new Sabre_DAV_ServerPreconditionsNode()));
$server = new Sabre_DAV_Server($root);
$httpRequest = new Sabre_HTTP_Request(array(
'HTTP_IF_MATCH' => '1234',
'REQUEST_URI' => '/foo'
));
$server->httpRequest = $httpRequest;
$server->checkPreconditions();
}
/**
* @covers Sabre_DAV_Server::checkPreconditions
*/
function testIfMatchCorrectEtag() {
$root = new Sabre_DAV_SimpleCollection('root',array(new Sabre_DAV_ServerPreconditionsNode()));
$server = new Sabre_DAV_Server($root);
$httpRequest = new Sabre_HTTP_Request(array(
'HTTP_IF_MATCH' => '"abc123"',
'REQUEST_URI' => '/foo'
));
$server->httpRequest = $httpRequest;
$this->assertTrue($server->checkPreconditions());
}
/**
* Evolution sometimes uses \" instead of " for If-Match headers.
*
* @covers Sabre_DAV_Server::checkPreconditions
* @depends testIfMatchCorrectEtag
*/
function testIfMatchEvolutionEtag() {
$root = new Sabre_DAV_SimpleCollection('root',array(new Sabre_DAV_ServerPreconditionsNode()));
$server = new Sabre_DAV_Server($root);
$httpRequest = new Sabre_HTTP_Request(array(
'HTTP_IF_MATCH' => '\\"abc123\\"',
'REQUEST_URI' => '/foo'
));
$server->httpRequest = $httpRequest;
$this->assertTrue($server->checkPreconditions());
}
/**
* @covers Sabre_DAV_Server::checkPreconditions
*/
function testIfMatchMultiple() {
$root = new Sabre_DAV_SimpleCollection('root',array(new Sabre_DAV_ServerPreconditionsNode()));
$server = new Sabre_DAV_Server($root);
$httpRequest = new Sabre_HTTP_Request(array(
'HTTP_IF_MATCH' => '"hellothere", "abc123"',
'REQUEST_URI' => '/foo'
));
$server->httpRequest = $httpRequest;
$this->assertTrue($server->checkPreconditions());
}
/**
* @covers Sabre_DAV_Server::checkPreconditions
*/
function testIfNoneMatchNoNode() {
$root = new Sabre_DAV_SimpleCollection('root',array(new Sabre_DAV_ServerPreconditionsNode()));
$server = new Sabre_DAV_Server($root);
$httpRequest = new Sabre_HTTP_Request(array(
'HTTP_IF_NONE_MATCH' => '*',
'REQUEST_URI' => '/bar'
));
$server->httpRequest = $httpRequest;
$this->assertTrue($server->checkPreconditions());
}
/**
* @covers Sabre_DAV_Server::checkPreconditions
* @expectedException Sabre_DAV_Exception_PreconditionFailed
*/
function testIfNoneMatchHasNode() {
$root = new Sabre_DAV_SimpleCollection('root',array(new Sabre_DAV_ServerPreconditionsNode()));
$server = new Sabre_DAV_Server($root);
$httpRequest = new Sabre_HTTP_Request(array(
'HTTP_IF_NONE_MATCH' => '*',
'REQUEST_URI' => '/foo'
));
$server->httpRequest = $httpRequest;
$server->checkPreconditions();
}
/**
* @covers Sabre_DAV_Server::checkPreconditions
*/
function testIfNoneMatchWrongEtag() {
$root = new Sabre_DAV_SimpleCollection('root',array(new Sabre_DAV_ServerPreconditionsNode()));
$server = new Sabre_DAV_Server($root);
$httpRequest = new Sabre_HTTP_Request(array(
'HTTP_IF_NONE_MATCH' => '"1234"',
'REQUEST_URI' => '/foo'
));
$server->httpRequest = $httpRequest;
$this->assertTrue($server->checkPreconditions());
}
/**
* @covers Sabre_DAV_Server::checkPreconditions
*/
function testIfNoneMatchWrongEtagMultiple() {
$root = new Sabre_DAV_SimpleCollection('root',array(new Sabre_DAV_ServerPreconditionsNode()));
$server = new Sabre_DAV_Server($root);
$httpRequest = new Sabre_HTTP_Request(array(
'HTTP_IF_NONE_MATCH' => '"1234", "5678"',
'REQUEST_URI' => '/foo'
));
$server->httpRequest = $httpRequest;
$this->assertTrue($server->checkPreconditions());
}
/**
* @covers Sabre_DAV_Server::checkPreconditions
* @expectedException Sabre_DAV_Exception_PreconditionFailed
*/
public function testIfNoneMatchCorrectEtag() {
$root = new Sabre_DAV_SimpleCollection('root',array(new Sabre_DAV_ServerPreconditionsNode()));
$server = new Sabre_DAV_Server($root);
$httpRequest = new Sabre_HTTP_Request(array(
'HTTP_IF_NONE_MATCH' => '"abc123"',
'REQUEST_URI' => '/foo'
));
$server->httpRequest = $httpRequest;
$server->checkPreconditions();
}
/**
* @covers Sabre_DAV_Server::checkPreconditions
* @expectedException Sabre_DAV_Exception_PreconditionFailed
*/
public function testIfNoneMatchCorrectEtagMultiple() {
$root = new Sabre_DAV_SimpleCollection('root',array(new Sabre_DAV_ServerPreconditionsNode()));
$server = new Sabre_DAV_Server($root);
$httpRequest = new Sabre_HTTP_Request(array(
'HTTP_IF_NONE_MATCH' => '"1234", "abc123"',
'REQUEST_URI' => '/foo'
));
$server->httpRequest = $httpRequest;
$server->checkPreconditions();
}
/**
* @covers Sabre_DAV_Server::checkPreconditions
*/
public function testIfNoneMatchCorrectEtagAsGet() {
$root = new Sabre_DAV_SimpleCollection('root',array(new Sabre_DAV_ServerPreconditionsNode()));
$server = new Sabre_DAV_Server($root);
$httpRequest = new Sabre_HTTP_Request(array(
'HTTP_IF_NONE_MATCH' => '"abc123"',
'REQUEST_URI' => '/foo'
));
$server->httpRequest = $httpRequest;
$server->httpResponse = new Sabre_HTTP_ResponseMock();
$this->assertFalse($server->checkPreconditions(true));
$this->assertEquals('HTTP/1.1 304 Not Modified',$server->httpResponse->status);
}
/**
* @covers Sabre_DAV_Server::checkPreconditions
*/
public function testIfModifiedSinceUnModified() {
$root = new Sabre_DAV_SimpleCollection('root',array(new Sabre_DAV_ServerPreconditionsNode()));
$server = new Sabre_DAV_Server($root);
$httpRequest = new Sabre_HTTP_Request(array(
'HTTP_IF_MODIFIED_SINCE' => 'Sun, 06 Nov 1994 08:49:37 GMT',
'REQUEST_URI' => '/foo'
));
$server->httpRequest = $httpRequest;
$server->httpResponse = new Sabre_HTTP_ResponseMock();
$this->assertFalse($server->checkPreconditions());
$this->assertEquals('HTTP/1.1 304 Not Modified',$server->httpResponse->status);
$this->assertEquals(array(
'Last-Modified' => 'Sat, 06 Apr 1985 23:30:00 GMT',
), $server->httpResponse->headers);
}
/**
* @covers Sabre_DAV_Server::checkPreconditions
*/
public function testIfModifiedSinceModified() {
$root = new Sabre_DAV_SimpleCollection('root',array(new Sabre_DAV_ServerPreconditionsNode()));
$server = new Sabre_DAV_Server($root);
$httpRequest = new Sabre_HTTP_Request(array(
'HTTP_IF_MODIFIED_SINCE' => 'Tue, 06 Nov 1984 08:49:37 GMT',
'REQUEST_URI' => '/foo'
));
$server->httpRequest = $httpRequest;
$server->httpResponse = new Sabre_HTTP_ResponseMock();
$this->assertTrue($server->checkPreconditions());
}
/**
* @covers Sabre_DAV_Server::checkPreconditions
*/
public function testIfModifiedSinceInvalidDate() {
$root = new Sabre_DAV_SimpleCollection('root',array(new Sabre_DAV_ServerPreconditionsNode()));
$server = new Sabre_DAV_Server($root);
$httpRequest = new Sabre_HTTP_Request(array(
'HTTP_IF_MODIFIED_SINCE' => 'Your mother',
'REQUEST_URI' => '/foo'
));
$server->httpRequest = $httpRequest;
$server->httpResponse = new Sabre_HTTP_ResponseMock();
// Invalid dates must be ignored, so this should return true
$this->assertTrue($server->checkPreconditions());
}
/**
* @covers Sabre_DAV_Server::checkPreconditions
*/
public function testIfModifiedSinceInvalidDate2() {
$root = new Sabre_DAV_SimpleCollection('root',array(new Sabre_DAV_ServerPreconditionsNode()));
$server = new Sabre_DAV_Server($root);
$httpRequest = new Sabre_HTTP_Request(array(
'HTTP_IF_MODIFIED_SINCE' => 'Sun, 06 Nov 1994 08:49:37 EST',
'REQUEST_URI' => '/foo'
));
$server->httpRequest = $httpRequest;
$server->httpResponse = new Sabre_HTTP_ResponseMock();
$this->assertTrue($server->checkPreconditions());
}
/**
* @covers Sabre_DAV_Server::checkPreconditions
*/
public function testIfUnmodifiedSinceUnModified() {
$root = new Sabre_DAV_SimpleCollection('root',array(new Sabre_DAV_ServerPreconditionsNode()));
$server = new Sabre_DAV_Server($root);
$httpRequest = new Sabre_HTTP_Request(array(
'HTTP_IF_UNMODIFIED_SINCE' => 'Sun, 06 Nov 1994 08:49:37 GMT',
'REQUEST_URI' => '/foo'
));
$server->httpRequest = $httpRequest;
$this->assertTrue($server->checkPreconditions());
}
/**
* @covers Sabre_DAV_Server::checkPreconditions
* @expectedException Sabre_DAV_Exception_PreconditionFailed
*/
public function testIfUnmodifiedSinceModified() {
$root = new Sabre_DAV_SimpleCollection('root',array(new Sabre_DAV_ServerPreconditionsNode()));
$server = new Sabre_DAV_Server($root);
$httpRequest = new Sabre_HTTP_Request(array(
'HTTP_IF_UNMODIFIED_SINCE' => 'Tue, 06 Nov 1984 08:49:37 GMT',
'REQUEST_URI' => '/foo'
));
$server->httpRequest = $httpRequest;
$server->httpResponse = new Sabre_HTTP_ResponseMock();
$server->checkPreconditions();
}
/**
* @covers Sabre_DAV_Server::checkPreconditions
*/
public function testIfUnmodifiedSinceInvalidDate() {
$root = new Sabre_DAV_SimpleCollection('root',array(new Sabre_DAV_ServerPreconditionsNode()));
$server = new Sabre_DAV_Server($root);
$httpRequest = new Sabre_HTTP_Request(array(
'HTTP_IF_UNMODIFIED_SINCE' => 'Sun, 06 Nov 1984 08:49:37 CET',
'REQUEST_URI' => '/foo'
));
$server->httpRequest = $httpRequest;
$server->httpResponse = new Sabre_HTTP_ResponseMock();
$this->assertTrue($server->checkPreconditions());
}
}
class Sabre_DAV_ServerPreconditionsNode extends Sabre_DAV_File {
function getETag() {
return '"abc123"';
}
function getLastModified() {
/* my birthday & time, I believe */
return strtotime('1985-04-07 01:30 +02:00');
}
function getName() {
return 'foo';
}
}

View file

@ -0,0 +1,393 @@
<?php
require_once 'Sabre/HTTP/ResponseMock.php';
require_once 'Sabre/DAV/AbstractServer.php';
class Sabre_DAV_ServerPropsTest extends Sabre_DAV_AbstractServer {
protected function getRootNode() {
return new Sabre_DAV_FSExt_Directory(SABRE_TEMPDIR);
}
function setUp() {
if (file_exists(SABRE_TEMPDIR.'../.sabredav')) unlink(SABRE_TEMPDIR.'../.sabredav');
parent::setUp();
file_put_contents(SABRE_TEMPDIR . '/test2.txt', 'Test contents2');
mkdir(SABRE_TEMPDIR . '/col');
file_put_contents(SABRE_TEMPDIR . 'col/test.txt', 'Test contents');
$this->server->addPlugin(new Sabre_DAV_Locks_Plugin(new Sabre_DAV_Locks_Backend_File(SABRE_TEMPDIR . '/.locksdb')));
}
function tearDown() {
parent::tearDown();
if (file_exists(SABRE_TEMPDIR.'../.locksdb')) unlink(SABRE_TEMPDIR.'../.locksdb');
}
private function sendRequest($body) {
$serverVars = array(
'REQUEST_URI' => '/',
'REQUEST_METHOD' => 'PROPFIND',
'HTTP_DEPTH' => '0',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody($body);
$this->server->httpRequest = ($request);
$this->server->exec();
}
public function testPropFindEmptyBody() {
$this->sendRequest("");
$this->assertEquals('HTTP/1.1 207 Multi-Status',$this->response->status);
$this->assertEquals(array(
'Content-Type' => 'application/xml; charset=utf-8',
'DAV' => '1, 3, extended-mkcol, 2',
),
$this->response->headers
);
$body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/","xmlns\\1=\"urn:DAV\"",$this->response->body);
$xml = simplexml_load_string($body);
$xml->registerXPathNamespace('d','urn:DAV');
list($data) = $xml->xpath('/d:multistatus/d:response/d:href');
$this->assertEquals('/',(string)$data,'href element should have been /');
$data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:resourcetype');
$this->assertEquals(1,count($data));
}
function testSupportedLocks() {
$xml = '<?xml version="1.0"?>
<d:propfind xmlns:d="DAV:">
<d:prop>
<d:supportedlock />
</d:prop>
</d:propfind>';
$this->sendRequest($xml);
$body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/","xmlns\\1=\"urn:DAV\"",$this->response->body);
$xml = simplexml_load_string($body);
$xml->registerXPathNamespace('d','urn:DAV');
$data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supportedlock/d:lockentry');
$this->assertEquals(2,count($data),'We expected two \'d:lockentry\' tags');
$data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supportedlock/d:lockentry/d:lockscope');
$this->assertEquals(2,count($data),'We expected two \'d:lockscope\' tags');
$data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supportedlock/d:lockentry/d:locktype');
$this->assertEquals(2,count($data),'We expected two \'d:locktype\' tags');
$data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supportedlock/d:lockentry/d:lockscope/d:shared');
$this->assertEquals(1,count($data),'We expected a \'d:shared\' tag');
$data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supportedlock/d:lockentry/d:lockscope/d:exclusive');
$this->assertEquals(1,count($data),'We expected a \'d:exclusive\' tag');
$data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supportedlock/d:lockentry/d:locktype/d:write');
$this->assertEquals(2,count($data),'We expected two \'d:write\' tags');
}
function testLockDiscovery() {
$xml = '<?xml version="1.0"?>
<d:propfind xmlns:d="DAV:">
<d:prop>
<d:lockdiscovery />
</d:prop>
</d:propfind>';
$this->sendRequest($xml);
$body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/","xmlns\\1=\"urn:DAV\"",$this->response->body);
$xml = simplexml_load_string($body);
$xml->registerXPathNamespace('d','urn:DAV');
$data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:lockdiscovery');
$this->assertEquals(1,count($data),'We expected a \'d:lockdiscovery\' tag');
}
function testUnknownProperty() {
$xml = '<?xml version="1.0"?>
<d:propfind xmlns:d="DAV:">
<d:prop>
<d:macaroni />
</d:prop>
</d:propfind>';
$this->sendRequest($xml);
$body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/","xmlns\\1=\"urn:DAV\"",$this->response->body);
$xml = simplexml_load_string($body);
$xml->registerXPathNamespace('d','urn:DAV');
$pathTests = array(
'/d:multistatus',
'/d:multistatus/d:response',
'/d:multistatus/d:response/d:propstat',
'/d:multistatus/d:response/d:propstat/d:status',
'/d:multistatus/d:response/d:propstat/d:prop',
'/d:multistatus/d:response/d:propstat/d:prop/d:macaroni',
);
foreach($pathTests as $test) {
$this->assertTrue(count($xml->xpath($test))==true,'We expected the ' . $test . ' element to appear in the response, we got: ' . $body);
}
$val = $xml->xpath('/d:multistatus/d:response/d:propstat/d:status');
$this->assertEquals(1,count($val),$body);
$this->assertEquals('HTTP/1.1 404 Not Found',(string)$val[0]);
}
/**
* @covers Sabre_DAV_Server::parsePropPatchRequest
*/
public function testParsePropPatchRequest() {
$body = '<?xml version="1.0"?>
<d:propertyupdate xmlns:d="DAV:" xmlns:s="http://sabredav.org/NS/test">
<d:set><d:prop><s:someprop>somevalue</s:someprop></d:prop></d:set>
<d:remove><d:prop><s:someprop2 /></d:prop></d:remove>
<d:set><d:prop><s:someprop3>removeme</s:someprop3></d:prop></d:set>
<d:remove><d:prop><s:someprop3 /></d:prop></d:remove>
</d:propertyupdate>';
$result = $this->server->parsePropPatchRequest($body);
$this->assertEquals(array(
'{http://sabredav.org/NS/test}someprop' => 'somevalue',
'{http://sabredav.org/NS/test}someprop2' => null,
'{http://sabredav.org/NS/test}someprop3' => null,
), $result);
}
/**
* @covers Sabre_DAV_Server::updateProperties
*/
public function testUpdateProperties() {
$props = array(
'{http://sabredav.org/NS/test}someprop' => 'somevalue',
);
$result = $this->server->updateProperties('/test2.txt',$props);
$this->assertEquals(array(
'200' => array('{http://sabredav.org/NS/test}someprop' => null),
'href' => '/test2.txt',
), $result);
}
/**
* @covers Sabre_DAV_Server::updateProperties
* @depends testUpdateProperties
*/
public function testUpdatePropertiesProtected() {
$props = array(
'{http://sabredav.org/NS/test}someprop' => 'somevalue',
'{DAV:}getcontentlength' => 50,
);
$result = $this->server->updateProperties('/test2.txt',$props);
$this->assertEquals(array(
'424' => array('{http://sabredav.org/NS/test}someprop' => null),
'403' => array('{DAV:}getcontentlength' => null),
'href' => '/test2.txt',
), $result);
}
/**
* @covers Sabre_DAV_Server::updateProperties
* @depends testUpdateProperties
*/
public function testUpdatePropertiesFail1() {
$dir = new Sabre_DAV_PropTestDirMock('updatepropsfalse');
$objectTree = new Sabre_DAV_ObjectTree($dir);
$this->server->tree = $objectTree;
$props = array(
'{http://sabredav.org/NS/test}someprop' => 'somevalue',
);
$result = $this->server->updateProperties('/',$props);
$this->assertEquals(array(
'403' => array('{http://sabredav.org/NS/test}someprop' => null),
'href' => '/',
), $result);
}
/**
* @covers Sabre_DAV_Server::updateProperties
* @depends testUpdateProperties
*/
public function testUpdatePropertiesFail2() {
$dir = new Sabre_DAV_PropTestDirMock('updatepropsarray');
$objectTree = new Sabre_DAV_ObjectTree($dir);
$this->server->tree = $objectTree;
$props = array(
'{http://sabredav.org/NS/test}someprop' => 'somevalue',
);
$result = $this->server->updateProperties('/',$props);
$this->assertEquals(array(
'402' => array('{http://sabredav.org/NS/test}someprop' => null),
'href' => '/',
), $result);
}
/**
* @covers Sabre_DAV_Server::updateProperties
* @depends testUpdateProperties
* @expectedException Sabre_DAV_Exception
*/
public function testUpdatePropertiesFail3() {
$dir = new Sabre_DAV_PropTestDirMock('updatepropsobj');
$objectTree = new Sabre_DAV_ObjectTree($dir);
$this->server->tree = $objectTree;
$props = array(
'{http://sabredav.org/NS/test}someprop' => 'somevalue',
);
$result = $this->server->updateProperties('/',$props);
}
/**
* @depends testParsePropPatchRequest
* @depends testUpdateProperties
* @covers Sabre_DAV_Server::httpPropPatch
*/
public function testPropPatch() {
$serverVars = array(
'REQUEST_URI' => '/',
'REQUEST_METHOD' => 'PROPPATCH',
);
$body = '<?xml version="1.0"?>
<d:propertyupdate xmlns:d="DAV:" xmlns:s="http://www.rooftopsolutions.nl/testnamespace">
<d:set><d:prop><s:someprop>somevalue</s:someprop></d:prop></d:set>
</d:propertyupdate>';
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody($body);
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals(array(
'Content-Type' => 'application/xml; charset=utf-8',
),
$this->response->headers
);
$this->assertEquals('HTTP/1.1 207 Multi-Status',$this->response->status,'We got the wrong status. Full XML response: ' . $this->response->body);
$body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/","xmlns\\1=\"urn:DAV\"",$this->response->body);
$xml = simplexml_load_string($body);
$xml->registerXPathNamespace('d','urn:DAV');
$xml->registerXPathNamespace('bla','http://www.rooftopsolutions.nl/testnamespace');
$data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop');
$this->assertEquals(1,count($data),'We expected one \'d:prop\' element. Response body: ' . $body);
$data = $xml->xpath('//bla:someprop');
$this->assertEquals(1,count($data),'We expected one \'s:someprop\' element. Response body: ' . $body);
$data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:status');
$this->assertEquals(1,count($data),'We expected one \'s:status\' element. Response body: ' . $body);
$this->assertEquals('HTTP/1.1 200 OK',(string)$data[0]);
}
/**
* @depends testPropPatch
*/
public function testPropPatchAndFetch() {
$this->testPropPatch();
$xml = '<?xml version="1.0"?>
<d:propfind xmlns:d="DAV:" xmlns:s="http://www.rooftopsolutions.nl/testnamespace">
<d:prop>
<s:someprop />
</d:prop>
</d:propfind>';
$this->sendRequest($xml);
$body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/","xmlns\\1=\"urn:DAV\"",$this->response->body);
$xml = simplexml_load_string($body);
$xml->registerXPathNamespace('d','urn:DAV');
$xml->registerXPathNamespace('bla','http://www.rooftopsolutions.nl/testnamespace');
$xpath='//bla:someprop';
$result = $xml->xpath($xpath);
$this->assertEquals(1,count($result),'We couldn\'t find our new property in the response. Full response body:' . "\n" . $body);
$this->assertEquals('somevalue',(string)$result[0],'We couldn\'t find our new property in the response. Full response body:' . "\n" . $body);
}
}
class Sabre_DAV_PropTestDirMock extends Sabre_DAV_SimpleCollection implements Sabre_DAV_IProperties {
public $type;
function __construct($type) {
$this->type =$type;
parent::__construct('root');
}
function updateProperties($updateProperties) {
switch($this->type) {
case 'updatepropsfalse' : return false;
case 'updatepropsarray' :
$r = array(402 => array());
foreach($updateProperties as $k=>$v) $r[402][$k] = null;
return $r;
case 'updatepropsobj' :
return new STDClass();
}
}
function getProperties($requestedPropeties) {
return array();
}
}

View file

@ -0,0 +1,271 @@
<?php
require_once 'Sabre/DAV/AbstractServer.php';
class Sabre_DAV_ServerRangeTest extends Sabre_DAV_AbstractServer{
protected function getRootNode() {
return new Sabre_DAV_FSExt_Directory(SABRE_TEMPDIR);
}
function testRange() {
$serverVars = array(
'REQUEST_URI' => '/test.txt',
'REQUEST_METHOD' => 'GET',
'HTTP_RANGE' => 'bytes=2-5',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals(array(
'Content-Type' => 'application/octet-stream',
'Content-Length' => 4,
'Content-Range' => 'bytes 2-5/13',
'Last-Modified' => Sabre_HTTP_Util::toHTTPDate(new DateTime('@' . filemtime($this->tempDir . '/test.txt'))),
'ETag' => '"' . md5(file_get_contents(SABRE_TEMPDIR . '/test.txt')). '"',
),
$this->response->headers
);
$this->assertEquals('HTTP/1.1 206 Partial Content',$this->response->status);
$this->assertEquals('st c', stream_get_contents($this->response->body));
}
/**
* @depends testRange
*/
function testStartRange() {
$serverVars = array(
'REQUEST_URI' => '/test.txt',
'REQUEST_METHOD' => 'GET',
'HTTP_RANGE' => 'bytes=2-',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals(array(
'Content-Type' => 'application/octet-stream',
'Content-Length' => 11,
'Content-Range' => 'bytes 2-12/13',
'Last-Modified' => Sabre_HTTP_Util::toHTTPDate(new DateTime('@' . filemtime($this->tempDir . '/test.txt'))),
'ETag' => '"' . md5(file_get_contents(SABRE_TEMPDIR . '/test.txt')) . '"',
),
$this->response->headers
);
$this->assertEquals('HTTP/1.1 206 Partial Content',$this->response->status);
$this->assertEquals('st contents', stream_get_contents($this->response->body));
}
/**
* @depends testRange
*/
function testEndRange() {
$serverVars = array(
'REQUEST_URI' => '/test.txt',
'REQUEST_METHOD' => 'GET',
'HTTP_RANGE' => 'bytes=-8',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals(array(
'Content-Type' => 'application/octet-stream',
'Content-Length' => 8,
'Content-Range' => 'bytes 5-12/13',
'Last-Modified' => Sabre_HTTP_Util::toHTTPDate(new DateTime('@' . filemtime($this->tempDir . '/test.txt'))),
'ETag' => '"' . md5(file_get_contents(SABRE_TEMPDIR . '/test.txt')). '"',
),
$this->response->headers
);
$this->assertEquals('HTTP/1.1 206 Partial Content',$this->response->status);
$this->assertEquals('contents', stream_get_contents($this->response->body));
}
/**
* @depends testRange
*/
function testTooHighRange() {
$serverVars = array(
'REQUEST_URI' => '/test.txt',
'REQUEST_METHOD' => 'GET',
'HTTP_RANGE' => 'bytes=100-200',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals('HTTP/1.1 416 Requested Range Not Satisfiable',$this->response->status);
}
/**
* @depends testRange
*/
function testCrazyRange() {
$serverVars = array(
'REQUEST_URI' => '/test.txt',
'REQUEST_METHOD' => 'GET',
'HTTP_RANGE' => 'bytes=8-4',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals('HTTP/1.1 416 Requested Range Not Satisfiable',$this->response->status);
}
/**
* @depends testRange
* @covers Sabre_DAV_Server::httpGet
*/
function testIfRangeEtag() {
$node = $this->server->tree->getNodeForPath('test.txt');
$serverVars = array(
'REQUEST_URI' => '/test.txt',
'REQUEST_METHOD' => 'GET',
'HTTP_RANGE' => 'bytes=2-5',
'HTTP_IF_RANGE' => $node->getETag(),
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals(array(
'Content-Type' => 'application/octet-stream',
'Content-Length' => 4,
'Content-Range' => 'bytes 2-5/13',
'Last-Modified' => Sabre_HTTP_Util::toHTTPDate(new DateTime('@' . filemtime($this->tempDir . '/test.txt'))),
'ETag' => '"' . md5(file_get_contents(SABRE_TEMPDIR . '/test.txt')) . '"',
),
$this->response->headers
);
$this->assertEquals('HTTP/1.1 206 Partial Content',$this->response->status);
$this->assertEquals('st c', stream_get_contents($this->response->body));
}
/**
* @depends testRange
* @covers Sabre_DAV_Server::httpGet
*/
function testIfRangeEtagIncorrect() {
$node = $this->server->tree->getNodeForPath('test.txt');
$serverVars = array(
'REQUEST_URI' => '/test.txt',
'REQUEST_METHOD' => 'GET',
'HTTP_RANGE' => 'bytes=2-5',
'HTTP_IF_RANGE' => $node->getETag() . 'blabla',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals(array(
'Content-Type' => 'application/octet-stream',
'Content-Length' => 13,
'Last-Modified' => Sabre_HTTP_Util::toHTTPDate(new DateTime('@' . filemtime($this->tempDir . '/test.txt'))),
'ETag' => '"' . md5(file_get_contents(SABRE_TEMPDIR . '/test.txt')) . '"',
),
$this->response->headers
);
$this->assertEquals('HTTP/1.1 200 OK',$this->response->status);
$this->assertEquals('Test contents', stream_get_contents($this->response->body));
}
/**
* @depends testRange
* @covers Sabre_DAV_Server::httpGet
*/
function testIfRangeModificationDate() {
$node = $this->server->tree->getNodeForPath('test.txt');
$serverVars = array(
'REQUEST_URI' => '/test.txt',
'REQUEST_METHOD' => 'GET',
'HTTP_RANGE' => 'bytes=2-5',
'HTTP_IF_RANGE' => 'tomorrow',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals(array(
'Content-Type' => 'application/octet-stream',
'Content-Length' => 4,
'Content-Range' => 'bytes 2-5/13',
'Last-Modified' => Sabre_HTTP_Util::toHTTPDate(new DateTime('@' . filemtime($this->tempDir . '/test.txt'))),
'ETag' => '"' . md5(file_get_contents(SABRE_TEMPDIR . '/test.txt')) . '"',
),
$this->response->headers
);
$this->assertEquals('HTTP/1.1 206 Partial Content',$this->response->status);
$this->assertEquals('st c', stream_get_contents($this->response->body));
}
/**
* @depends testRange
* @covers Sabre_DAV_Server::httpGet
*/
function testIfRangeModificationDateModified() {
$node = $this->server->tree->getNodeForPath('test.txt');
$serverVars = array(
'REQUEST_URI' => '/test.txt',
'REQUEST_METHOD' => 'GET',
'HTTP_RANGE' => 'bytes=2-5',
'HTTP_IF_RANGE' => '-2 years',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals(array(
'Content-Type' => 'application/octet-stream',
'Content-Length' => 13,
'Last-Modified' => Sabre_HTTP_Util::toHTTPDate(new DateTime('@' . filemtime($this->tempDir . '/test.txt'))),
'ETag' => '"' . md5(file_get_contents(SABRE_TEMPDIR . '/test.txt')) . '"',
),
$this->response->headers
);
$this->assertEquals('HTTP/1.1 200 OK',$this->response->status);
$this->assertEquals('Test contents', stream_get_contents($this->response->body));
}
}

View file

@ -0,0 +1,696 @@
<?php
require_once 'Sabre/HTTP/ResponseMock.php';
require_once 'Sabre/DAV/AbstractServer.php';
require_once 'Sabre/DAV/Exception.php';
class Sabre_DAV_ServerSimpleTest extends Sabre_DAV_AbstractServer{
function testConstructArray() {
$nodes = array(
new Sabre_DAV_SimpleCollection('hello')
);
$server = new Sabre_DAV_Server($nodes);
$this->assertEquals($nodes[0], $server->tree->getNodeForPath('hello'));
}
/**
* @expectedException Sabre_DAV_Exception
*/
function testConstructIncorrectObj() {
$nodes = array(
new Sabre_DAV_SimpleCollection('hello'),
new STDClass(),
);
$server = new Sabre_DAV_Server($nodes);
}
/**
* @expectedException Sabre_DAV_Exception
*/
function testConstructInvalidArg() {
$server = new Sabre_DAV_Server(1);
}
function testGet() {
$serverVars = array(
'REQUEST_URI' => '/test.txt',
'REQUEST_METHOD' => 'GET',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals(array(
'Content-Type' => 'application/octet-stream',
'Content-Length' => 13,
'Last-Modified' => Sabre_HTTP_Util::toHTTPDate(new DateTime('@' . filemtime($this->tempDir . '/test.txt'))),
),
$this->response->headers
);
$this->assertEquals('HTTP/1.1 200 OK',$this->response->status);
$this->assertEquals('Test contents', stream_get_contents($this->response->body));
}
function testGetDoesntExist() {
$serverVars = array(
'REQUEST_URI' => '/test.txt_randomblbla',
'REQUEST_METHOD' => 'GET',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals('HTTP/1.1 404 Not Found',$this->response->status);
}
function testGetDoesntExist2() {
$serverVars = array(
'REQUEST_URI' => '/test.txt/randomblbla',
'REQUEST_METHOD' => 'GET',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals('HTTP/1.1 404 Not Found',$this->response->status);
}
/**
* This test should have the exact same result as testGet.
*
* The idea is that double slashes // are converted to single ones /
*
*/
function testGetDoubleSlash() {
$serverVars = array(
'REQUEST_URI' => '//test.txt',
'REQUEST_METHOD' => 'GET',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals(array(
'Content-Type' => 'application/octet-stream',
'Content-Length' => 13,
'Last-Modified' => Sabre_HTTP_Util::toHTTPDate(new DateTime('@' . filemtime($this->tempDir . '/test.txt'))),
),
$this->response->headers
);
$this->assertEquals('HTTP/1.1 200 OK',$this->response->status);
$this->assertEquals('Test contents', stream_get_contents($this->response->body));
}
function testHEAD() {
$serverVars = array(
'REQUEST_URI' => '/test.txt',
'REQUEST_METHOD' => 'HEAD',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals(array(
'Content-Type' => 'application/octet-stream',
'Content-Length' => 13,
'Last-Modified' => Sabre_HTTP_Util::toHTTPDate(new DateTime('@' . filemtime($this->tempDir . '/test.txt'))),
),
$this->response->headers
);
$this->assertEquals('HTTP/1.1 200 OK',$this->response->status);
$this->assertEquals('', $this->response->body);
}
function testPut() {
$serverVars = array(
'REQUEST_URI' => '/testput.txt',
'REQUEST_METHOD' => 'PUT',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('Testing new file');
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals('', $this->response->body);
$this->assertEquals('HTTP/1.1 201 Created',$this->response->status);
$this->assertEquals(array(
"Content-Length" => "0",
), $this->response->headers);
$this->assertEquals('Testing new file',file_get_contents($this->tempDir . '/testput.txt'));
}
function testPutAlreadyExists() {
$serverVars = array(
'REQUEST_URI' => '/test.txt',
'REQUEST_METHOD' => 'PUT',
'HTTP_IF_NONE_MATCH' => '*',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('Testing new file');
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals(array(
'Content-Type' => 'application/xml; charset=utf-8',
),$this->response->headers);
$this->assertEquals('HTTP/1.1 412 Precondition failed',$this->response->status);
$this->assertNotEquals('Testing new file',file_get_contents($this->tempDir . '/test.txt'));
}
function testPutUpdate() {
$serverVars = array(
'REQUEST_URI' => '/test.txt',
'REQUEST_METHOD' => 'PUT',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('Testing updated file');
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals('0', $this->response->headers['Content-Length']);
$this->assertEquals('HTTP/1.1 204 No Content',$this->response->status);
$this->assertEquals('', $this->response->body);
$this->assertEquals('Testing updated file',file_get_contents($this->tempDir . '/test.txt'));
}
function testPutContentRange() {
$serverVars = array(
'REQUEST_URI' => '/testput.txt',
'REQUEST_METHOD' => 'PUT',
'HTTP_CONTENT_RANGE' => 'bytes/100-200',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('Testing new file');
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals('HTTP/1.1 501 Not Implemented',$this->response->status);
}
function testDelete() {
$serverVars = array(
'REQUEST_URI' => '/test.txt',
'REQUEST_METHOD' => 'DELETE',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals(array(
'Content-Length' => '0',
),$this->response->headers);
$this->assertEquals('HTTP/1.1 204 No Content',$this->response->status);
$this->assertEquals('', $this->response->body);
$this->assertFalse(file_exists($this->tempDir . '/test.txt'));
}
function testDeleteDirectory() {
$serverVars = array(
'REQUEST_URI' => '/testcol',
'REQUEST_METHOD' => 'DELETE',
);
mkdir($this->tempDir.'/testcol');
file_put_contents($this->tempDir.'/testcol/test.txt','Hi! I\'m a file with a short lifespan');
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals(array(
'Content-Length' => '0',
),$this->response->headers);
$this->assertEquals('HTTP/1.1 204 No Content',$this->response->status);
$this->assertEquals('', $this->response->body);
$this->assertFalse(file_exists($this->tempDir . '/col'));
}
function testOptions() {
$serverVars = array(
'REQUEST_URI' => '/',
'REQUEST_METHOD' => 'OPTIONS',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals(array(
'DAV' => '1, 3, extended-mkcol',
'MS-Author-Via' => 'DAV',
'Allow' => 'OPTIONS, GET, HEAD, DELETE, PROPFIND, PUT, PROPPATCH, COPY, MOVE, REPORT',
'Accept-Ranges' => 'bytes',
'Content-Length' => '0',
'X-Sabre-Version' => Sabre_DAV_Version::VERSION,
),$this->response->headers);
$this->assertEquals('HTTP/1.1 200 OK',$this->response->status);
$this->assertEquals('', $this->response->body);
}
function testNonExistantMethod() {
$serverVars = array(
'REQUEST_URI' => '/',
'REQUEST_METHOD' => 'BLABLA',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals(array(
'Content-Type' => 'application/xml; charset=utf-8',
),$this->response->headers);
$this->assertEquals('HTTP/1.1 501 Not Implemented',$this->response->status);
}
function testGETOnCollection() {
$serverVars = array(
'REQUEST_URI' => '/',
'REQUEST_METHOD' => 'GET',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals(array(
'Content-Type' => 'application/xml; charset=utf-8',
),$this->response->headers);
$this->assertEquals('HTTP/1.1 501 Not Implemented',$this->response->status);
}
function testHEADOnCollection() {
$serverVars = array(
'REQUEST_URI' => '/',
'REQUEST_METHOD' => 'HEAD',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals('HTTP/1.1 200 OK',$this->response->status);
}
function testBaseUri() {
$serverVars = array(
'REQUEST_URI' => '/blabla/test.txt',
'REQUEST_METHOD' => 'GET',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->setBaseUri('/blabla/');
$this->assertEquals('/blabla/',$this->server->getBaseUri());
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals(array(
'Content-Type' => 'application/octet-stream',
'Content-Length' => 13,
'Last-Modified' => Sabre_HTTP_Util::toHTTPDate(new DateTime('@' . filemtime($this->tempDir . '/test.txt'))),
),
$this->response->headers
);
$this->assertEquals('HTTP/1.1 200 OK',$this->response->status);
$this->assertEquals('Test contents', stream_get_contents($this->response->body));
}
function testBaseUriAddSlash() {
$tests = array(
'/' => '/',
'/foo' => '/foo/',
'/foo/' => '/foo/',
'/foo/bar' => '/foo/bar/',
'/foo/bar/' => '/foo/bar/',
);
foreach($tests as $test=>$result) {
$this->server->setBaseUri($test);
$this->assertEquals($result, $this->server->getBaseUri());
}
}
function testCalculateUri() {
$uris = array(
'http://www.example.org/root/somepath',
'/root/somepath',
'/root/somepath/',
);
$this->server->setBaseUri('/root/');
foreach($uris as $uri) {
$this->assertEquals('somepath',$this->server->calculateUri($uri));
}
$this->server->setBaseUri('/root');
foreach($uris as $uri) {
$this->assertEquals('somepath',$this->server->calculateUri($uri));
}
$this->assertEquals('', $this->server->calculateUri('/root'));
}
function testCalculateUriSpecialChars() {
$uris = array(
'http://www.example.org/root/%C3%A0fo%C3%B3',
'/root/%C3%A0fo%C3%B3',
'/root/%C3%A0fo%C3%B3/'
);
$this->server->setBaseUri('/root/');
foreach($uris as $uri) {
$this->assertEquals("\xc3\xa0fo\xc3\xb3",$this->server->calculateUri($uri));
}
$this->server->setBaseUri('/root');
foreach($uris as $uri) {
$this->assertEquals("\xc3\xa0fo\xc3\xb3",$this->server->calculateUri($uri));
}
$this->server->setBaseUri('/');
foreach($uris as $uri) {
$this->assertEquals("root/\xc3\xa0fo\xc3\xb3",$this->server->calculateUri($uri));
}
}
function testBaseUriCheck() {
$uris = array(
'http://www.example.org/root/somepath',
'/root/somepath',
'/root/somepath/'
);
try {
$this->server->setBaseUri('root/');
$this->server->calculateUri('/root/testuri');
$this->fail('Expected an exception');
} catch (Sabre_DAV_Exception_Forbidden $e) {
// This was expected
}
}
/**
* @covers Sabre_DAV_Server::guessBaseUri
*/
function testGuessBaseUri() {
$serverVars = array(
'REQUEST_URI' => '/index.php/root',
'PATH_INFO' => '/root',
);
$httpRequest = new Sabre_HTTP_Request($serverVars);
$server = new Sabre_DAV_Server();
$server->httpRequest = $httpRequest;
$this->assertEquals('/index.php/', $server->guessBaseUri());
}
/**
* @depends testGuessBaseUri
* @covers Sabre_DAV_Server::guessBaseUri
*/
function testGuessBaseUriPercentEncoding() {
$serverVars = array(
'REQUEST_URI' => '/index.php/dir/path2/path%20with%20spaces',
'PATH_INFO' => '/dir/path2/path with spaces',
);
$httpRequest = new Sabre_HTTP_Request($serverVars);
$server = new Sabre_DAV_Server();
$server->httpRequest = $httpRequest;
$this->assertEquals('/index.php/', $server->guessBaseUri());
}
/**
* @depends testGuessBaseUri
* @covers Sabre_DAV_Server::guessBaseUri
*/
/*
function testGuessBaseUriPercentEncoding2() {
$this->markTestIncomplete('This behaviour is not yet implemented');
$serverVars = array(
'REQUEST_URI' => '/some%20directory+mixed/index.php/dir/path2/path%20with%20spaces',
'PATH_INFO' => '/dir/path2/path with spaces',
);
$httpRequest = new Sabre_HTTP_Request($serverVars);
$server = new Sabre_DAV_Server();
$server->httpRequest = $httpRequest;
$this->assertEquals('/some%20directory+mixed/index.php/', $server->guessBaseUri());
}*/
function testGuessBaseUri2() {
$serverVars = array(
'REQUEST_URI' => '/index.php/root/',
'PATH_INFO' => '/root/',
);
$httpRequest = new Sabre_HTTP_Request($serverVars);
$server = new Sabre_DAV_Server();
$server->httpRequest = $httpRequest;
$this->assertEquals('/index.php/', $server->guessBaseUri());
}
function testGuessBaseUriNoPathInfo() {
$serverVars = array(
'REQUEST_URI' => '/index.php/root',
);
$httpRequest = new Sabre_HTTP_Request($serverVars);
$server = new Sabre_DAV_Server();
$server->httpRequest = $httpRequest;
$this->assertEquals('/', $server->guessBaseUri());
}
function testGuessBaseUriNoPathInfo2() {
$serverVars = array(
'REQUEST_URI' => '/a/b/c/test.php',
);
$httpRequest = new Sabre_HTTP_Request($serverVars);
$server = new Sabre_DAV_Server();
$server->httpRequest = $httpRequest;
$this->assertEquals('/', $server->guessBaseUri());
}
/**
* @covers Sabre_DAV_Server::guessBaseUri
* @depends testGuessBaseUri
*/
function testGuessBaseUriQueryString() {
$serverVars = array(
'REQUEST_URI' => '/index.php/root?query_string=blabla',
'PATH_INFO' => '/root',
);
$httpRequest = new Sabre_HTTP_Request($serverVars);
$server = new Sabre_DAV_Server();
$server->httpRequest = $httpRequest;
$this->assertEquals('/index.php/', $server->guessBaseUri());
}
function testTriggerException() {
$this->server->subscribeEvent('beforeMethod',array($this,'exceptionTrigger'));
$this->server->exec();
$this->assertEquals(array(
'Content-Type' => 'application/xml; charset=utf-8',
),$this->response->headers);
$this->assertEquals('HTTP/1.1 500 Internal Server Error',$this->response->status);
}
function exceptionTrigger() {
throw new Sabre_DAV_Exception('Hola');
}
function testReportNotFound() {
$serverVars = array(
'REQUEST_URI' => '/',
'REQUEST_METHOD' => 'REPORT',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$this->server->httpRequest->setBody('<?xml version="1.0"?><bla:myreport xmlns:bla="http://www.rooftopsolutions.nl/NS"></bla:myreport>');
$this->server->exec();
$this->assertEquals(array(
'Content-Type' => 'application/xml; charset=utf-8',
),
$this->response->headers
);
$this->assertEquals('HTTP/1.1 501 Not Implemented',$this->response->status,'We got an incorrect status back. Full response body follows: ' . $this->response->body);
}
function testReportIntercepted() {
$serverVars = array(
'REQUEST_URI' => '/',
'REQUEST_METHOD' => 'REPORT',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$this->server->httpRequest->setBody('<?xml version="1.0"?><bla:myreport xmlns:bla="http://www.rooftopsolutions.nl/NS"></bla:myreport>');
$this->server->subscribeEvent('report',array($this,'reportHandler'));
$this->server->exec();
$this->assertEquals(array(
'testheader' => 'testvalue',
),
$this->response->headers
);
$this->assertEquals('HTTP/1.1 418 I\'m a teapot',$this->response->status,'We got an incorrect status back. Full response body follows: ' . $this->response->body);
}
function reportHandler($reportName) {
if ($reportName=='{http://www.rooftopsolutions.nl/NS}myreport') {
$this->server->httpResponse->sendStatus(418);
$this->server->httpResponse->setHeader('testheader','testvalue');
return false;
}
else return;
}
function testGetPropertiesForChildren() {
$result = $this->server->getPropertiesForChildren('',array(
'{DAV:}getcontentlength',
));
$expected = array(
'test.txt' => array('{DAV:}getcontentlength' => 13),
'dir/' => array(),
);
$this->assertEquals($expected,$result);
}
}

View file

@ -0,0 +1,127 @@
<?php
class Sabre_DAV_ServerUpdatePropertiesTest extends PHPUnit_Framework_TestCase {
function testUpdatePropertiesFail() {
$tree = array(
new Sabre_DAV_SimpleCollection('foo'),
);
$server = new Sabre_DAV_Server($tree);
$result = $server->updateProperties('foo', array(
'{DAV:}foo' => 'bar'
));
$expected = array(
'href' => 'foo',
'403' => array(
'{DAV:}foo' => null,
),
);
$this->assertEquals($expected, $result);
}
function testUpdatePropertiesProtected() {
$tree = array(
new Sabre_DAV_SimpleCollection('foo'),
);
$server = new Sabre_DAV_Server($tree);
$result = $server->updateProperties('foo', array(
'{DAV:}getetag' => 'bla',
'{DAV:}foo' => 'bar'
));
$expected = array(
'href' => 'foo',
'403' => array(
'{DAV:}getetag' => null,
),
'424' => array(
'{DAV:}foo' => null,
),
);
$this->assertEquals($expected, $result);
}
function testUpdatePropertiesEventFail() {
$tree = array(
new Sabre_DAV_SimpleCollection('foo'),
);
$server = new Sabre_DAV_Server($tree);
$server->subscribeEvent('updateProperties', array($this,'updatepropfail'));
$result = $server->updateProperties('foo', array(
'{DAV:}foo' => 'bar',
'{DAV:}foo2' => 'bla',
));
$expected = array(
'href' => 'foo',
'404' => array(
'{DAV:}foo' => null,
),
'424' => array(
'{DAV:}foo2' => null,
),
);
$this->assertEquals($expected, $result);
}
function updatePropFail(&$propertyDelta, &$result, $node) {
$result[404] = array(
'{DAV:}foo' => null,
);
unset($propertyDelta['{DAV:}foo']);
return false;
}
function testUpdatePropertiesEventSuccess() {
$tree = array(
new Sabre_DAV_SimpleCollection('foo'),
);
$server = new Sabre_DAV_Server($tree);
$server->subscribeEvent('updateProperties', array($this,'updatepropsuccess'));
$result = $server->updateProperties('foo', array(
'{DAV:}foo' => 'bar',
'{DAV:}foo2' => 'bla',
));
$expected = array(
'href' => 'foo',
'200' => array(
'{DAV:}foo' => null,
),
'201' => array(
'{DAV:}foo2' => null,
),
);
$this->assertEquals($expected, $result);
}
function updatePropSuccess(&$propertyDelta, &$result, $node) {
$result[200] = array(
'{DAV:}foo' => null,
);
$result[201] = array(
'{DAV:}foo2' => null,
);
unset($propertyDelta['{DAV:}foo']);
unset($propertyDelta['{DAV:}foo2']);
return;
}
}

View file

@ -0,0 +1,17 @@
<?php
class Sabre_DAV_SimpleFileTest extends PHPUnit_Framework_TestCase {
function testAll() {
$file = new Sabre_DAV_SimpleFile('filename.txt','contents','text/plain');
$this->assertEquals('filename.txt', $file->getName());
$this->assertEquals('contents', $file->get());
$this->assertEquals('8', $file->getSize());
$this->assertEquals('"' . md5('contents') . '"', $file->getETag());
$this->assertEquals('text/plain', $file->getContentType());
}
}

View file

@ -0,0 +1,120 @@
<?php
class Sabre_DAV_StringUtilTest extends PHPUnit_Framework_TestCase {
/**
* @dataProvider dataset
*/
function testTextMatch($haystack, $needle, $collation, $matchType, $result) {
$this->assertEquals($result, Sabre_DAV_StringUtil::textMatch($haystack, $needle, $collation, $matchType));
}
function dataset() {
return array(
array('FOOBAR', 'FOO', 'i;octet', 'contains', true),
array('FOOBAR', 'foo', 'i;octet', 'contains', false),
array('FÖÖBAR', 'FÖÖ', 'i;octet', 'contains', true),
array('FÖÖBAR', 'föö', 'i;octet', 'contains', false),
array('FOOBAR', 'FOOBAR', 'i;octet', 'equals', true),
array('FOOBAR', 'fooBAR', 'i;octet', 'equals', false),
array('FOOBAR', 'FOO', 'i;octet', 'starts-with', true),
array('FOOBAR', 'foo', 'i;octet', 'starts-with', false),
array('FOOBAR', 'BAR', 'i;octet', 'starts-with', false),
array('FOOBAR', 'bar', 'i;octet', 'starts-with', false),
array('FOOBAR', 'FOO', 'i;octet', 'ends-with', false),
array('FOOBAR', 'foo', 'i;octet', 'ends-with', false),
array('FOOBAR', 'BAR', 'i;octet', 'ends-with', true),
array('FOOBAR', 'bar', 'i;octet', 'ends-with', false),
array('FOOBAR', 'FOO', 'i;ascii-casemap', 'contains', true),
array('FOOBAR', 'foo', 'i;ascii-casemap', 'contains', true),
array('FÖÖBAR', 'FÖÖ', 'i;ascii-casemap', 'contains', true),
array('FÖÖBAR', 'föö', 'i;ascii-casemap', 'contains', false),
array('FOOBAR', 'FOOBAR', 'i;ascii-casemap', 'equals', true),
array('FOOBAR', 'fooBAR', 'i;ascii-casemap', 'equals', true),
array('FOOBAR', 'FOO', 'i;ascii-casemap', 'starts-with', true),
array('FOOBAR', 'foo', 'i;ascii-casemap', 'starts-with', true),
array('FOOBAR', 'BAR', 'i;ascii-casemap', 'starts-with', false),
array('FOOBAR', 'bar', 'i;ascii-casemap', 'starts-with', false),
array('FOOBAR', 'FOO', 'i;ascii-casemap', 'ends-with', false),
array('FOOBAR', 'foo', 'i;ascii-casemap', 'ends-with', false),
array('FOOBAR', 'BAR', 'i;ascii-casemap', 'ends-with', true),
array('FOOBAR', 'bar', 'i;ascii-casemap', 'ends-with', true),
array('FOOBAR', 'FOO', 'i;unicode-casemap', 'contains', true),
array('FOOBAR', 'foo', 'i;unicode-casemap', 'contains', true),
array('FÖÖBAR', 'FÖÖ', 'i;unicode-casemap', 'contains', true),
array('FÖÖBAR', 'föö', 'i;unicode-casemap', 'contains', true),
array('FOOBAR', 'FOOBAR', 'i;unicode-casemap', 'equals', true),
array('FOOBAR', 'fooBAR', 'i;unicode-casemap', 'equals', true),
array('FOOBAR', 'FOO', 'i;unicode-casemap', 'starts-with', true),
array('FOOBAR', 'foo', 'i;unicode-casemap', 'starts-with', true),
array('FOOBAR', 'BAR', 'i;unicode-casemap', 'starts-with', false),
array('FOOBAR', 'bar', 'i;unicode-casemap', 'starts-with', false),
array('FOOBAR', 'FOO', 'i;unicode-casemap', 'ends-with', false),
array('FOOBAR', 'foo', 'i;unicode-casemap', 'ends-with', false),
array('FOOBAR', 'BAR', 'i;unicode-casemap', 'ends-with', true),
array('FOOBAR', 'bar', 'i;unicode-casemap', 'ends-with', true),
);
}
/**
* @expectedException Sabre_DAV_Exception_BadRequest
*/
public function testBadCollation() {
Sabre_DAV_StringUtil::textMatch('foobar','foo','blabla','contains');
}
/**
* @expectedException Sabre_DAV_Exception_BadRequest
*/
public function testBadMatchType() {
Sabre_DAV_StringUtil::textMatch('foobar','foo','i;octet','booh');
}
public function testEnsureUTF8_ascii() {
$inputString = "harkema";
$outputString = "harkema";
$this->assertEquals(
$outputString,
Sabre_DAV_StringUtil::ensureUTF8($inputString)
);
}
public function testEnsureUTF8_latin1() {
$inputString = "m\xfcnster";
$outputString = "münster";
$this->assertEquals(
$outputString,
Sabre_DAV_StringUtil::ensureUTF8($inputString)
);
}
public function testEnsureUTF8_utf8() {
$inputString = "m\xc3\xbcnster";
$outputString = "münster";
$this->assertEquals(
$outputString,
Sabre_DAV_StringUtil::ensureUTF8($inputString)
);
}
}

View file

@ -0,0 +1,248 @@
<?php
class Sabre_DAV_TemporaryFileFilterTest extends Sabre_DAV_AbstractServer {
function setUp() {
parent::setUp();
$plugin = new Sabre_DAV_TemporaryFileFilterPlugin(SABRE_TEMPDIR . '/tff');
$this->server->addPlugin($plugin);
}
function testPutNormal() {
$serverVars = array(
'REQUEST_URI' => '/testput.txt',
'REQUEST_METHOD' => 'PUT',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('Testing new file');
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals('', $this->response->body);
$this->assertEquals('HTTP/1.1 201 Created',$this->response->status);
$this->assertEquals('0', $this->response->headers['Content-Length']);
$this->assertEquals('Testing new file',file_get_contents(SABRE_TEMPDIR . '/testput.txt'));
}
function testPutTemp() {
// mimicking an OS/X resource fork
$serverVars = array(
'REQUEST_URI' => '/._testput.txt',
'REQUEST_METHOD' => 'PUT',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('Testing new file');
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals('', $this->response->body);
$this->assertEquals('HTTP/1.1 201 Created',$this->response->status);
$this->assertEquals(array(
'X-Sabre-Temp' => 'true',
),$this->response->headers);
$this->assertFalse(file_exists(SABRE_TEMPDIR . '/._testput.txt'),'._testput.txt should not exist in the regular file structure.');
}
function testPutTempIfNoneMatch() {
// mimicking an OS/X resource fork
$serverVars = array(
'REQUEST_URI' => '/._testput.txt',
'REQUEST_METHOD' => 'PUT',
'HTTP_IF_NONE_MATCH' => '*',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('Testing new file');
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals('', $this->response->body);
$this->assertEquals('HTTP/1.1 201 Created',$this->response->status);
$this->assertEquals(array(
'X-Sabre-Temp' => 'true',
),$this->response->headers);
$this->assertFalse(file_exists(SABRE_TEMPDIR . '/._testput.txt'),'._testput.txt should not exist in the regular file structure.');
$this->server->exec();
$this->assertEquals('HTTP/1.1 412 Precondition failed',$this->response->status);
$this->assertEquals(array(
'X-Sabre-Temp' => 'true',
'Content-Type' => 'application/xml; charset=utf-8',
),$this->response->headers);
}
function testPutGet() {
// mimicking an OS/X resource fork
$serverVars = array(
'REQUEST_URI' => '/._testput.txt',
'REQUEST_METHOD' => 'PUT',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('Testing new file');
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals('', $this->response->body);
$this->assertEquals('HTTP/1.1 201 Created',$this->response->status);
$this->assertEquals(array(
'X-Sabre-Temp' => 'true',
),$this->response->headers);
$serverVars = array(
'REQUEST_URI' => '/._testput.txt',
'REQUEST_METHOD' => 'GET',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals('HTTP/1.1 200 OK',$this->response->status);
$this->assertEquals(array(
'X-Sabre-Temp' => 'true',
'Content-Length' => 16,
'Content-Type' => 'application/octet-stream',
),$this->response->headers);
$this->assertEquals('Testing new file',stream_get_contents($this->response->body));
}
function testLockNonExistant() {
mkdir(SABRE_TEMPDIR . '/locksdir');
$locksBackend = new Sabre_DAV_Locks_Backend_FS(SABRE_TEMPDIR . '/locksdir');
$locksPlugin = new Sabre_DAV_Locks_Plugin($locksBackend);
$this->server->addPlugin($locksPlugin);
// mimicking an OS/X resource fork
$serverVars = array(
'REQUEST_URI' => '/._testlock.txt',
'REQUEST_METHOD' => 'LOCK',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('<?xml version="1.0"?>
<D:lockinfo xmlns:D="DAV:">
<D:lockscope><D:exclusive/></D:lockscope>
<D:locktype><D:write/></D:locktype>
<D:owner>
<D:href>http://example.org/~ejw/contact.html</D:href>
</D:owner>
</D:lockinfo>');
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals('HTTP/1.1 201 Created',$this->response->status);
$this->assertEquals('application/xml; charset=utf-8',$this->response->headers['Content-Type']);
$this->assertTrue(preg_match('/^<opaquelocktoken:(.*)>$/',$this->response->headers['Lock-Token'])===1,'We did not get a valid Locktoken back (' . $this->response->headers['Lock-Token'] . ')');
$this->assertEquals('true',$this->response->headers['X-Sabre-Temp']);
$this->assertFalse(file_exists(SABRE_TEMPDIR . '/._testlock.txt'),'._testlock.txt should not exist in the regular file structure.');
}
function testPutDelete() {
// mimicking an OS/X resource fork
$serverVars = array(
'REQUEST_URI' => '/._testput.txt',
'REQUEST_METHOD' => 'PUT',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('Testing new file');
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals('', $this->response->body);
$this->assertEquals('HTTP/1.1 201 Created',$this->response->status);
$this->assertEquals(array(
'X-Sabre-Temp' => 'true',
),$this->response->headers);
$serverVars = array(
'REQUEST_URI' => '/._testput.txt',
'REQUEST_METHOD' => 'DELETE',
);
$request = new Sabre_HTTP_Request($serverVars);
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals('HTTP/1.1 204 No Content',$this->response->status, "Incorrect status code received. Full body:\n". $this->response->body);
$this->assertEquals(array(
'X-Sabre-Temp' => 'true',
),$this->response->headers);
$this->assertEquals('',$this->response->body);
}
function testPutPropfind() {
// mimicking an OS/X resource fork
$serverVars = array(
'REQUEST_URI' => '/._testput.txt',
'REQUEST_METHOD' => 'PUT',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('Testing new file');
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals('', $this->response->body);
$this->assertEquals('HTTP/1.1 201 Created',$this->response->status);
$this->assertEquals(array(
'X-Sabre-Temp' => 'true',
),$this->response->headers);
$serverVars = array(
'REQUEST_URI' => '/._testput.txt',
'REQUEST_METHOD' => 'PROPFIND',
);
$request = new Sabre_HTTP_Request($serverVars);
$request->setBody('');
$this->server->httpRequest = ($request);
$this->server->exec();
$this->assertEquals('HTTP/1.1 207 Multi-Status',$this->response->status,'Incorrect status code returned. Body: ' . $this->response->body);
$this->assertEquals(array(
'X-Sabre-Temp' => 'true',
'Content-Type' => 'application/xml; charset=utf-8',
),$this->response->headers);
$body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/","xmlns\\1=\"urn:DAV\"",$this->response->body);
$xml = simplexml_load_string($body);
$xml->registerXPathNamespace('d','urn:DAV');
list($data) = $xml->xpath('/d:multistatus/d:response/d:href');
$this->assertEquals('/._testput.txt',(string)$data,'href element should have been /._testput.txt');
$data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:resourcetype');
$this->assertEquals(1,count($data));
}
}

View file

@ -0,0 +1,32 @@
<?php
class Sabre_DAV_TestPlugin extends Sabre_DAV_ServerPlugin {
public $beforeMethod;
function getFeatures() {
return array('drinking');
}
function getHTTPMethods($uri) {
return array('BEER','WINE');
}
function initialize(Sabre_DAV_Server $server) {
$server->subscribeEvent('beforeMethod',array($this,'beforeMethod'));
}
function beforeMethod($method) {
$this->beforeMethod = $method;
return true;
}
}

View file

@ -0,0 +1,72 @@
<?php
/**
* @covers Sabre_DAV_Tree
* @covers Sabre_DAV_Tree_Filesystem
* @covers Sabre_DAV_FS_Node
* @covers Sabre_DAV_FS_File
* @covers Sabre_DAV_FS_Directory
*/
class Sabre_DAV_Tree_FilesystemTest extends PHPUnit_Framework_TestCase {
function setUp() {
Sabre_TestUtil::clearTempDir();
file_put_contents(SABRE_TEMPDIR. '/file.txt','Body');
mkdir(SABRE_TEMPDIR.'/dir');
file_put_contents(SABRE_TEMPDIR.'/dir/subfile.txt','Body');
}
function tearDown() {
Sabre_TestUtil::clearTempDir();
}
function testGetNodeForPath_File() {
$fs = new Sabre_DAV_Tree_Filesystem(SABRE_TEMPDIR);
$node = $fs->getNodeForPath('file.txt');
$this->assertTrue($node instanceof Sabre_DAV_FS_File);
}
function testGetNodeForPath_Directory() {
$fs = new Sabre_DAV_Tree_Filesystem(SABRE_TEMPDIR);
$node = $fs->getNodeForPath('dir');
$this->assertTrue($node instanceof Sabre_DAV_FS_Directory);
}
function testCopy() {
$fs = new Sabre_DAV_Tree_Filesystem(SABRE_TEMPDIR);
$fs->copy('file.txt','file2.txt');
$this->assertTrue(file_exists(SABRE_TEMPDIR . '/file2.txt'));
$this->assertEquals('Body',file_get_contents(SABRE_TEMPDIR . '/file2.txt'));
}
function testCopyDir() {
$fs = new Sabre_DAV_Tree_Filesystem(SABRE_TEMPDIR);
$fs->copy('dir','dir2');
$this->assertTrue(file_exists(SABRE_TEMPDIR . '/dir2'));
$this->assertEquals('Body',file_get_contents(SABRE_TEMPDIR . '/dir2/subfile.txt'));
}
function testMove() {
$fs = new Sabre_DAV_Tree_Filesystem(SABRE_TEMPDIR);
$fs->move('file.txt','file2.txt');
$this->assertTrue(file_exists(SABRE_TEMPDIR . '/file2.txt'));
$this->assertTrue(!file_exists(SABRE_TEMPDIR . '/file.txt'));
$this->assertEquals('Body',file_get_contents(SABRE_TEMPDIR . '/file2.txt'));
}
}

View file

@ -0,0 +1,173 @@
<?php
/**
* @covers Sabre_DAV_Tree
*/
class Sabre_DAV_TreeTest extends PHPUnit_Framework_TestCase {
function testNodeExists() {
$tree = new Sabre_DAV_TreeMock();
$this->assertTrue($tree->nodeExists('hi'));
$this->assertFalse($tree->nodeExists('hello'));
}
function testCopy() {
$tree = new Sabre_DAV_TreeMock();
$tree->copy('hi','hi2');
$this->assertArrayHasKey('hi2', $tree->getNodeForPath('')->newDirectories);
$this->assertEquals('foobar', $tree->getNodeForPath('hi/file')->get());
$this->assertEquals(array('test1'=>'value'), $tree->getNodeForPath('hi/file')->getProperties(array()));
}
function testMove() {
$tree = new Sabre_DAV_TreeMock();
$tree->move('hi','hi2');
$this->assertEquals('hi2', $tree->getNodeForPath('hi')->getName());
$this->assertTrue($tree->getNodeForPath('hi')->isRenamed);
}
function testDeepMove() {
$tree = new Sabre_DAV_TreeMock();
$tree->move('hi/sub','hi2');
$this->assertArrayHasKey('hi2', $tree->getNodeForPath('')->newDirectories);
$this->assertTrue($tree->getNodeForPath('hi/sub')->isDeleted);
}
function testDelete() {
$tree = new Sabre_DAV_TreeMock();
$tree->delete('hi');
$this->assertTrue($tree->getNodeForPath('hi')->isDeleted);
}
function testGetChildren() {
$tree = new Sabre_DAV_TreeMock();
$children = $tree->getChildren('');
$this->assertEquals(1,count($children));
$this->assertEquals('hi', $children[0]->getName());
}
}
class Sabre_DAV_TreeMock extends Sabre_DAV_Tree {
private $nodes = array();
function __construct() {
$this->nodes['hi/sub'] = new Sabre_DAV_TreeDirectoryTester('sub');
$this->nodes['hi/file'] = new Sabre_DAV_TreeFileTester('file');
$this->nodes['hi/file']->properties = array('test1' => 'value');
$this->nodes['hi/file']->data = 'foobar';
$this->nodes['hi'] = new Sabre_DAV_TreeDirectoryTester('hi',array($this->nodes['hi/sub'], $this->nodes['hi/file']));
$this->nodes[''] = new Sabre_DAV_TreeDirectoryTester('hi', array($this->nodes['hi']));
}
function getNodeForPath($path) {
if (isset($this->nodes[$path])) return $this->nodes[$path];
throw new Sabre_DAV_Exception_NotFound('item not found');
}
}
class Sabre_DAV_TreeDirectoryTester extends Sabre_DAV_SimpleCollection {
public $newDirectories = array();
public $newFiles = array();
public $isDeleted = false;
public $isRenamed = false;
function createDirectory($name) {
$this->newDirectories[$name] = true;
}
function createFile($name,$data = null) {
$this->newFiles[$name] = $data;
}
function getChild($name) {
if (isset($this->newDirectories[$name])) return new Sabre_DAV_TreeDirectoryTester($name);
if (isset($this->newFiles[$name])) return new Sabre_DAV_TreeFileTester($name, $this->newFiles[$name]);
return parent::getChild($name);
}
function delete() {
$this->isDeleted = true;
}
function setName($name) {
$this->isRenamed = true;
$this->name = $name;
}
}
class Sabre_DAV_TreeFileTester extends Sabre_DAV_File implements Sabre_DAV_IProperties {
public $name;
public $data;
public $properties;
function __construct($name, $data = null) {
$this->name = $name;
if (is_null($data)) $data = 'bla';
$this->data = $data;
}
function getName() {
return $this->name;
}
function get() {
return $this->data;
}
function getProperties($properties) {
return $this->properties;
}
function updateProperties($properties) {
$this->properties = $properties;
return true;
}
}

View file

@ -0,0 +1,129 @@
<?php
class Sabre_DAV_URLUtilTest extends PHPUnit_Framework_TestCase{
function testEncodePath() {
$str = '';
for($i=0;$i<128;$i++) $str.=chr($i);
$newStr = Sabre_DAV_URLUtil::encodePath($str);
$this->assertEquals(
'%00%01%02%03%04%05%06%07%08%09%0a%0b%0c%0d%0e%0f'.
'%10%11%12%13%14%15%16%17%18%19%1a%1b%1c%1d%1e%1f'.
'%20%21%22%23%24%25%26%27()%2a%2b%2c-./'.
'0123456789%3a%3b%3c%3d%3e%3f'.
'%40ABCDEFGHIJKLMNO' .
'PQRSTUVWXYZ%5b%5c%5d%5e_' .
'%60abcdefghijklmno' .
'pqrstuvwxyz%7b%7c%7d~%7f',
$newStr);
$this->assertEquals($str,Sabre_DAV_URLUtil::decodePath($newStr));
}
function testEncodePathSegment() {
$str = '';
for($i=0;$i<128;$i++) $str.=chr($i);
$newStr = Sabre_DAV_URLUtil::encodePathSegment($str);
// Note: almost exactly the same as the last test, with the
// exception of the encoding of / (ascii code 2f)
$this->assertEquals(
'%00%01%02%03%04%05%06%07%08%09%0a%0b%0c%0d%0e%0f'.
'%10%11%12%13%14%15%16%17%18%19%1a%1b%1c%1d%1e%1f'.
'%20%21%22%23%24%25%26%27()%2a%2b%2c-.%2f'.
'0123456789%3a%3b%3c%3d%3e%3f'.
'%40ABCDEFGHIJKLMNO' .
'PQRSTUVWXYZ%5b%5c%5d%5e_' .
'%60abcdefghijklmno' .
'pqrstuvwxyz%7b%7c%7d~%7f',
$newStr);
$this->assertEquals($str,Sabre_DAV_URLUtil::decodePathSegment($newStr));
}
function testDecode() {
$str = 'Hello%20Test+Test2.txt';
$newStr = Sabre_DAV_URLUtil::decodePath($str);
$this->assertEquals('Hello Test+Test2.txt',$newStr);
}
/**
* @depends testDecode
*/
function testDecodeUmlaut() {
$str = 'Hello%C3%BC.txt';
$newStr = Sabre_DAV_URLUtil::decodePath($str);
$this->assertEquals("Hello\xC3\xBC.txt",$newStr);
}
/**
* @depends testDecodeUmlaut
*/
function testDecodeUmlautLatin1() {
$str = 'Hello%FC.txt';
$newStr = Sabre_DAV_URLUtil::decodePath($str);
$this->assertEquals("Hello\xC3\xBC.txt",$newStr);
}
/**
* This testcase was sent by a bug reporter
*
* @depends testDecode
*/
function testDecodeAccentsWindows7() {
$str = '/webdav/%C3%A0fo%C3%B3';
$newStr = Sabre_DAV_URLUtil::decodePath($str);
$this->assertEquals(strtolower($str),Sabre_DAV_URLUtil::encodePath($newStr));
}
function testSplitPath() {
$strings = array(
// input // expected result
'/foo/bar' => array('/foo','bar'),
'/foo/bar/' => array('/foo','bar'),
'foo/bar/' => array('foo','bar'),
'foo/bar' => array('foo','bar'),
'foo/bar/baz' => array('foo/bar','baz'),
'foo/bar/baz/' => array('foo/bar','baz'),
'foo' => array('','foo'),
'foo/' => array('','foo'),
'/foo/' => array('','foo'),
'/foo' => array('','foo'),
'' => array(null,null),
// UTF-8
"/\xC3\xA0fo\xC3\xB3/bar" => array("/\xC3\xA0fo\xC3\xB3",'bar'),
"/\xC3\xA0foo/b\xC3\xBCr/" => array("/\xC3\xA0foo","b\xC3\xBCr"),
"foo/\xC3\xA0\xC3\xBCr" => array("foo","\xC3\xA0\xC3\xBCr"),
);
foreach($strings as $input => $expected) {
$output = Sabre_DAV_URLUtil::splitPath($input);
$this->assertEquals($expected, $output, 'The expected output for \'' . $input . '\' was incorrect');
}
}
}

View file

@ -0,0 +1,23 @@
<?php
class Sabre_DAV_UUIDUtilTest extends PHPUnit_Framework_TestCase {
function testValidateUUID() {
$this->assertTrue(
Sabre_DAV_UUIDUtil::validateUUID('11111111-2222-3333-4444-555555555555')
);
$this->assertFalse(
Sabre_DAV_UUIDUtil::validateUUID(' 11111111-2222-3333-4444-555555555555')
);
$this->assertTrue(
Sabre_DAV_UUIDUtil::validateUUID('ffffffff-2222-3333-4444-555555555555')
);
$this->assertFalse(
Sabre_DAV_UUIDUtil::validateUUID('fffffffg-2222-3333-4444-555555555555')
);
}
}

View file

@ -0,0 +1,282 @@
<?php
class Sabre_DAV_XMLUtilTest extends PHPUnit_Framework_TestCase {
function testToClarkNotation() {
$dom = new DOMDocument();
$dom->loadXML('<?xml version="1.0"?><test1 xmlns="http://www.example.org/">Testdoc</test1>');
$this->assertEquals(
'{http://www.example.org/}test1',
Sabre_DAV_XMLUtil::toClarkNotation($dom->firstChild)
);
}
function testToClarkNotation2() {
$dom = new DOMDocument();
$dom->loadXML('<?xml version="1.0"?><s:test1 xmlns:s="http://www.example.org/">Testdoc</s:test1>');
$this->assertEquals(
'{http://www.example.org/}test1',
Sabre_DAV_XMLUtil::toClarkNotation($dom->firstChild)
);
}
function testToClarkNotationDAVNamespace() {
$dom = new DOMDocument();
$dom->loadXML('<?xml version="1.0"?><s:test1 xmlns:s="urn:DAV">Testdoc</s:test1>');
$this->assertEquals(
'{DAV:}test1',
Sabre_DAV_XMLUtil::toClarkNotation($dom->firstChild)
);
}
function testToClarkNotationNoElem() {
$dom = new DOMDocument();
$dom->loadXML('<?xml version="1.0"?><s:test1 xmlns:s="urn:DAV">Testdoc</s:test1>');
$this->assertNull(
Sabre_DAV_XMLUtil::toClarkNotation($dom->firstChild->firstChild)
);
}
function testConvertDAVNamespace() {
$xml='<?xml version="1.0"?><document xmlns="DAV:">blablabla</document>';
$this->assertEquals(
'<?xml version="1.0"?><document xmlns="urn:DAV">blablabla</document>',
Sabre_DAV_XMLUtil::convertDAVNamespace($xml)
);
}
function testConvertDAVNamespace2() {
$xml='<?xml version="1.0"?><s:document xmlns:s="DAV:">blablabla</s:document>';
$this->assertEquals(
'<?xml version="1.0"?><s:document xmlns:s="urn:DAV">blablabla</s:document>',
Sabre_DAV_XMLUtil::convertDAVNamespace($xml)
);
}
function testConvertDAVNamespace3() {
$xml='<?xml version="1.0"?><s:document xmlns="http://bla" xmlns:s="DAV:" xmlns:z="http://othernamespace">blablabla</s:document>';
$this->assertEquals(
'<?xml version="1.0"?><s:document xmlns="http://bla" xmlns:s="urn:DAV" xmlns:z="http://othernamespace">blablabla</s:document>',
Sabre_DAV_XMLUtil::convertDAVNamespace($xml)
);
}
function testConvertDAVNamespace4() {
$xml='<?xml version="1.0"?><document xmlns=\'DAV:\'>blablabla</document>';
$this->assertEquals(
'<?xml version="1.0"?><document xmlns=\'urn:DAV\'>blablabla</document>',
Sabre_DAV_XMLUtil::convertDAVNamespace($xml)
);
}
function testConvertDAVNamespaceMixedQuotes() {
$xml='<?xml version="1.0"?><document xmlns=\'DAV:" xmlns="Another attribute\'>blablabla</document>';
$this->assertEquals(
$xml,
Sabre_DAV_XMLUtil::convertDAVNamespace($xml)
);
}
/**
* @depends testConvertDAVNamespace
*/
function testLoadDOMDocument() {
$xml='<?xml version="1.0"?><document></document>';
$dom = Sabre_DAV_XMLUtil::loadDOMDocument($xml);
$this->assertTrue($dom instanceof DOMDocument);
}
/**
* @depends testLoadDOMDocument
* @expectedException Sabre_DAV_Exception_BadRequest
*/
function testLoadDOMDocumentEmpty() {
Sabre_DAV_XMLUtil::loadDOMDocument('');
}
/**
* @depends testConvertDAVNamespace
* @expectedException Sabre_DAV_Exception_BadRequest
*/
function testLoadDOMDocumentInvalid() {
$xml='<?xml version="1.0"?><document></docu';
$dom = Sabre_DAV_XMLUtil::loadDOMDocument($xml);
}
/**
* @depends testLoadDOMDocument
*/
function testLoadDOMDocumentUTF16() {
$xml='<?xml version="1.0" encoding="UTF-16"?><root xmlns="DAV:">blabla</root>';
$xml = iconv('UTF-8','UTF-16LE',$xml);
$dom = Sabre_DAV_XMLUtil::loadDOMDocument($xml);
$this->assertEquals('blabla',$dom->firstChild->nodeValue);
}
function testParseProperties() {
$xml='<?xml version="1.0"?>
<root xmlns="DAV:">
<prop>
<displayname>Calendars</displayname>
</prop>
</root>';
$dom = Sabre_DAV_XMLUtil::loadDOMDocument($xml);
$properties = Sabre_DAV_XMLUtil::parseProperties($dom->firstChild);
$this->assertEquals(array(
'{DAV:}displayname' => 'Calendars',
), $properties);
}
/**
* @depends testParseProperties
*/
function testParsePropertiesEmpty() {
$xml='<?xml version="1.0"?>
<root xmlns="DAV:" xmlns:s="http://www.rooftopsolutions.nl/example">
<prop>
<displayname>Calendars</displayname>
</prop>
<prop>
<s:example />
</prop>
</root>';
$dom = Sabre_DAV_XMLUtil::loadDOMDocument($xml);
$properties = Sabre_DAV_XMLUtil::parseProperties($dom->firstChild);
$this->assertEquals(array(
'{DAV:}displayname' => 'Calendars',
'{http://www.rooftopsolutions.nl/example}example' => null
), $properties);
}
/**
* @depends testParseProperties
*/
function testParsePropertiesComplex() {
$xml='<?xml version="1.0"?>
<root xmlns="DAV:">
<prop>
<displayname>Calendars</displayname>
</prop>
<prop>
<someprop>Complex value <b>right here</b></someprop>
</prop>
</root>';
$dom = Sabre_DAV_XMLUtil::loadDOMDocument($xml);
$properties = Sabre_DAV_XMLUtil::parseProperties($dom->firstChild);
$this->assertEquals(array(
'{DAV:}displayname' => 'Calendars',
'{DAV:}someprop' => 'Complex value right here',
), $properties);
}
/**
* @depends testParseProperties
*/
function testParsePropertiesNoProperties() {
$xml='<?xml version="1.0"?>
<root xmlns="DAV:">
<prop>
</prop>
</root>';
$dom = Sabre_DAV_XMLUtil::loadDOMDocument($xml);
$properties = Sabre_DAV_XMLUtil::parseProperties($dom->firstChild);
$this->assertEquals(array(), $properties);
}
function testParsePropertiesMapHref() {
$xml='<?xml version="1.0"?>
<root xmlns="DAV:">
<prop>
<displayname>Calendars</displayname>
</prop>
<prop>
<someprop><href>http://sabredav.org/</href></someprop>
</prop>
</root>';
$dom = Sabre_DAV_XMLUtil::loadDOMDocument($xml);
$properties = Sabre_DAV_XMLUtil::parseProperties($dom->firstChild,array('{DAV:}someprop'=>'Sabre_DAV_Property_Href'));
$this->assertEquals(array(
'{DAV:}displayname' => 'Calendars',
'{DAV:}someprop' => new Sabre_DAV_Property_Href('http://sabredav.org/',false),
), $properties);
}
function testParseClarkNotation() {
$this->assertEquals(array(
'DAV:',
'foo',
), Sabre_DAV_XMLUtil::parseClarkNotation('{DAV:}foo'));
$this->assertEquals(array(
'http://example.org/ns/bla',
'bar-soap',
), Sabre_DAV_XMLUtil::parseClarkNotation('{http://example.org/ns/bla}bar-soap'));
}
/**
* @expectedException InvalidArgumentException
*/
function testParseClarkNotationFail() {
Sabre_DAV_XMLUtil::parseClarkNotation('}foo');
}
}