2013-10-21 22:46:31 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Sabre;
|
|
|
|
|
2016-05-11 00:26:44 +00:00
|
|
|
use Sabre\HTTP\Request;
|
|
|
|
use Sabre\HTTP\Response;
|
|
|
|
use Sabre\HTTP\Sapi;
|
2013-10-21 22:46:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This class may be used as a basis for other webdav-related unittests.
|
|
|
|
*
|
|
|
|
* This class is supposed to provide a reasonably big framework to quickly get
|
|
|
|
* a testing environment running.
|
|
|
|
*
|
2016-05-11 00:26:44 +00:00
|
|
|
* @copyright Copyright (C) fruux GmbH (https://fruux.com/)
|
2014-04-12 17:13:37 +00:00
|
|
|
* @author Evert Pot (http://evertpot.com/)
|
2014-06-28 20:28:08 +00:00
|
|
|
* @license http://sabre.io/license/ Modified BSD License
|
2013-10-21 22:46:31 +00:00
|
|
|
*/
|
|
|
|
abstract class DAVServerTest extends \PHPUnit_Framework_TestCase {
|
|
|
|
|
|
|
|
protected $setupCalDAV = false;
|
|
|
|
protected $setupCardDAV = false;
|
|
|
|
protected $setupACL = false;
|
|
|
|
protected $setupCalDAVSharing = false;
|
2016-05-11 00:26:44 +00:00
|
|
|
protected $setupCalDAVScheduling = false;
|
|
|
|
protected $setupCalDAVSubscriptions = false;
|
|
|
|
protected $setupCalDAVICSExport = false;
|
|
|
|
protected $setupLocks = false;
|
|
|
|
protected $setupFiles = false;
|
|
|
|
protected $setupPropertyStorage = false;
|
2013-10-21 22:46:31 +00:00
|
|
|
|
2016-05-11 00:26:44 +00:00
|
|
|
/**
|
|
|
|
* An array with calendars. Every calendar should have
|
|
|
|
* - principaluri
|
|
|
|
* - uri
|
|
|
|
*/
|
|
|
|
protected $caldavCalendars = [];
|
|
|
|
protected $caldavCalendarObjects = [];
|
2013-10-21 22:46:31 +00:00
|
|
|
|
2016-05-11 00:26:44 +00:00
|
|
|
protected $carddavAddressBooks = [];
|
|
|
|
protected $carddavCards = [];
|
2013-10-21 22:46:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Sabre\DAV\Server
|
|
|
|
*/
|
|
|
|
protected $server;
|
2016-05-11 00:26:44 +00:00
|
|
|
protected $tree = [];
|
2013-10-21 22:46:31 +00:00
|
|
|
|
|
|
|
protected $caldavBackend;
|
|
|
|
protected $carddavBackend;
|
|
|
|
protected $principalBackend;
|
2016-05-11 00:26:44 +00:00
|
|
|
protected $locksBackend;
|
|
|
|
protected $propertyStorageBackend;
|
2013-10-21 22:46:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Sabre\CalDAV\Plugin
|
|
|
|
*/
|
|
|
|
protected $caldavPlugin;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Sabre\CardDAV\Plugin
|
|
|
|
*/
|
|
|
|
protected $carddavPlugin;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Sabre\DAVACL\Plugin
|
|
|
|
*/
|
|
|
|
protected $aclPlugin;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Sabre\CalDAV\SharingPlugin
|
|
|
|
*/
|
|
|
|
protected $caldavSharingPlugin;
|
|
|
|
|
2016-05-11 00:26:44 +00:00
|
|
|
/**
|
|
|
|
* CalDAV scheduling plugin
|
|
|
|
*
|
|
|
|
* @var CalDAV\Schedule\Plugin
|
|
|
|
*/
|
|
|
|
protected $caldavSchedulePlugin;
|
|
|
|
|
2013-10-21 22:46:31 +00:00
|
|
|
/**
|
|
|
|
* @var Sabre\DAV\Auth\Plugin
|
|
|
|
*/
|
|
|
|
protected $authPlugin;
|
|
|
|
|
2016-05-11 00:26:44 +00:00
|
|
|
/**
|
|
|
|
* @var Sabre\DAV\Locks\Plugin
|
|
|
|
*/
|
|
|
|
protected $locksPlugin;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Sabre\DAV\PropertyStorage\Plugin
|
|
|
|
*/
|
|
|
|
protected $propertyStoragePlugin;
|
|
|
|
|
2013-10-21 22:46:31 +00:00
|
|
|
/**
|
|
|
|
* If this string is set, we will automatically log in the user with this
|
|
|
|
* name.
|
|
|
|
*/
|
|
|
|
protected $autoLogin = null;
|
|
|
|
|
|
|
|
function setUp() {
|
|
|
|
|
|
|
|
$this->setUpBackends();
|
|
|
|
$this->setUpTree();
|
|
|
|
|
|
|
|
$this->server = new DAV\Server($this->tree);
|
2016-05-11 00:26:44 +00:00
|
|
|
$this->server->sapi = new HTTP\SapiMock();
|
2013-10-21 22:46:31 +00:00
|
|
|
$this->server->debugExceptions = true;
|
|
|
|
|
|
|
|
if ($this->setupCalDAV) {
|
|
|
|
$this->caldavPlugin = new CalDAV\Plugin();
|
|
|
|
$this->server->addPlugin($this->caldavPlugin);
|
|
|
|
}
|
|
|
|
if ($this->setupCalDAVSharing) {
|
|
|
|
$this->caldavSharingPlugin = new CalDAV\SharingPlugin();
|
|
|
|
$this->server->addPlugin($this->caldavSharingPlugin);
|
|
|
|
}
|
2016-05-11 00:26:44 +00:00
|
|
|
if ($this->setupCalDAVScheduling) {
|
|
|
|
$this->caldavSchedulePlugin = new CalDAV\Schedule\Plugin();
|
|
|
|
$this->server->addPlugin($this->caldavSchedulePlugin);
|
|
|
|
}
|
|
|
|
if ($this->setupCalDAVSubscriptions) {
|
|
|
|
$this->server->addPlugin(new CalDAV\Subscriptions\Plugin());
|
|
|
|
}
|
|
|
|
if ($this->setupCalDAVICSExport) {
|
|
|
|
$this->caldavICSExportPlugin = new CalDAV\ICSExportPlugin();
|
|
|
|
$this->server->addPlugin($this->caldavICSExportPlugin);
|
|
|
|
}
|
2013-10-21 22:46:31 +00:00
|
|
|
if ($this->setupCardDAV) {
|
|
|
|
$this->carddavPlugin = new CardDAV\Plugin();
|
|
|
|
$this->server->addPlugin($this->carddavPlugin);
|
|
|
|
}
|
|
|
|
if ($this->setupACL) {
|
|
|
|
$this->aclPlugin = new DAVACL\Plugin();
|
|
|
|
$this->server->addPlugin($this->aclPlugin);
|
|
|
|
}
|
2016-05-11 00:26:44 +00:00
|
|
|
if ($this->setupLocks) {
|
|
|
|
$this->locksPlugin = new DAV\Locks\Plugin(
|
|
|
|
$this->locksBackend
|
|
|
|
);
|
|
|
|
$this->server->addPlugin($this->locksPlugin);
|
|
|
|
}
|
|
|
|
if ($this->setupPropertyStorage) {
|
|
|
|
$this->propertyStoragePlugin = new DAV\PropertyStorage\Plugin(
|
|
|
|
$this->propertyStorageBackend
|
|
|
|
);
|
|
|
|
$this->server->addPlugin($this->propertyStoragePlugin);
|
|
|
|
}
|
2013-10-21 22:46:31 +00:00
|
|
|
if ($this->autoLogin) {
|
|
|
|
$authBackend = new DAV\Auth\Backend\Mock();
|
2016-05-11 00:26:44 +00:00
|
|
|
$authBackend->setPrincipal('principals/' . $this->autoLogin);
|
|
|
|
$this->authPlugin = new DAV\Auth\Plugin($authBackend);
|
2013-10-21 22:46:31 +00:00
|
|
|
$this->server->addPlugin($this->authPlugin);
|
|
|
|
|
|
|
|
// This will trigger the actual login procedure
|
2016-05-11 00:26:44 +00:00
|
|
|
$this->authPlugin->beforeMethod(new Request(), new Response());
|
2013-10-21 22:46:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes a request, and returns a response object.
|
|
|
|
*
|
|
|
|
* You can either pass an instance of Sabre\HTTP\Request, or an array,
|
|
|
|
* which will then be used as the _SERVER array.
|
|
|
|
*
|
|
|
|
* @param array|\Sabre\HTTP\Request $request
|
|
|
|
* @return \Sabre\HTTP\Response
|
|
|
|
*/
|
|
|
|
function request($request) {
|
|
|
|
|
|
|
|
if (is_array($request)) {
|
2016-05-11 00:26:44 +00:00
|
|
|
$request = HTTP\Request::createFromServerArray($request);
|
2013-10-21 22:46:31 +00:00
|
|
|
}
|
|
|
|
$this->server->httpRequest = $request;
|
|
|
|
$this->server->httpResponse = new HTTP\ResponseMock();
|
|
|
|
$this->server->exec();
|
|
|
|
|
|
|
|
return $this->server->httpResponse;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-05-11 00:26:44 +00:00
|
|
|
/**
|
|
|
|
* Override this to provide your own Tree for your test-case.
|
|
|
|
*/
|
2013-10-21 22:46:31 +00:00
|
|
|
function setUpTree() {
|
|
|
|
|
|
|
|
if ($this->setupCalDAV) {
|
2016-05-11 00:26:44 +00:00
|
|
|
$this->tree[] = new CalDAV\CalendarRoot(
|
2013-10-21 22:46:31 +00:00
|
|
|
$this->principalBackend,
|
|
|
|
$this->caldavBackend
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if ($this->setupCardDAV) {
|
|
|
|
$this->tree[] = new CardDAV\AddressBookRoot(
|
|
|
|
$this->principalBackend,
|
|
|
|
$this->carddavBackend
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->setupCardDAV || $this->setupCalDAV) {
|
2016-05-11 00:26:44 +00:00
|
|
|
$this->tree[] = new CalDAV\Principal\Collection(
|
2013-10-21 22:46:31 +00:00
|
|
|
$this->principalBackend
|
|
|
|
);
|
|
|
|
}
|
2016-05-11 00:26:44 +00:00
|
|
|
if ($this->setupFiles) {
|
|
|
|
|
|
|
|
$this->tree[] = new DAV\Mock\Collection('files');
|
|
|
|
|
|
|
|
}
|
2013-10-21 22:46:31 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function setUpBackends() {
|
|
|
|
|
2016-05-11 00:26:44 +00:00
|
|
|
if ($this->setupCalDAVSharing && is_null($this->caldavBackend)) {
|
|
|
|
$this->caldavBackend = new CalDAV\Backend\MockSharing($this->caldavCalendars, $this->caldavCalendarObjects);
|
|
|
|
}
|
|
|
|
if ($this->setupCalDAVSubscriptions && is_null($this->caldavBackend)) {
|
|
|
|
$this->caldavBackend = new CalDAV\Backend\MockSubscriptionSupport($this->caldavCalendars, $this->caldavCalendarObjects);
|
|
|
|
}
|
2013-10-21 22:46:31 +00:00
|
|
|
if ($this->setupCalDAV && is_null($this->caldavBackend)) {
|
2016-05-11 00:26:44 +00:00
|
|
|
if ($this->setupCalDAVScheduling) {
|
|
|
|
$this->caldavBackend = new CalDAV\Backend\MockScheduling($this->caldavCalendars, $this->caldavCalendarObjects);
|
|
|
|
} else {
|
|
|
|
$this->caldavBackend = new CalDAV\Backend\Mock($this->caldavCalendars, $this->caldavCalendarObjects);
|
|
|
|
}
|
2013-10-21 22:46:31 +00:00
|
|
|
}
|
|
|
|
if ($this->setupCardDAV && is_null($this->carddavBackend)) {
|
|
|
|
$this->carddavBackend = new CardDAV\Backend\Mock($this->carddavAddressBooks, $this->carddavCards);
|
|
|
|
}
|
|
|
|
if ($this->setupCardDAV || $this->setupCalDAV) {
|
|
|
|
$this->principalBackend = new DAVACL\PrincipalBackend\Mock();
|
|
|
|
}
|
2016-05-11 00:26:44 +00:00
|
|
|
if ($this->setupLocks) {
|
|
|
|
$this->locksBackend = new DAV\Locks\Backend\Mock();
|
|
|
|
}
|
|
|
|
if ($this->setupPropertyStorage) {
|
|
|
|
$this->propertyStorageBackend = new DAV\PropertyStorage\Backend\Mock();
|
|
|
|
}
|
2013-10-21 22:46:31 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function assertHTTPStatus($expectedStatus, HTTP\Request $req) {
|
|
|
|
|
|
|
|
$resp = $this->request($req);
|
2016-05-11 00:26:44 +00:00
|
|
|
$this->assertEquals((int)$expectedStatus, (int)$resp->status, 'Incorrect HTTP status received: ' . $resp->body);
|
2013-10-21 22:46:31 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|