Merge pull request #1197 from nextcloud/oc-public-sharing
CalDAV calendar public sharingpull/1554/head
commit
06e969cb74
@ -0,0 +1,67 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
||||
*
|
||||
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
||||
* @license AGPL-3.0
|
||||
*
|
||||
* This code is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License, version 3,
|
||||
* as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License, version 3,
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
*/
|
||||
namespace OCA\DAV\CalDAV;
|
||||
|
||||
use Sabre\DAV\Collection;
|
||||
use Sabre\DAV\Exception\NotFound;
|
||||
|
||||
class PublicCalendarRoot extends Collection {
|
||||
|
||||
/** @var CalDavBackend */
|
||||
protected $caldavBackend;
|
||||
|
||||
/** @var \OCP\IL10N */
|
||||
protected $l10n;
|
||||
|
||||
function __construct(CalDavBackend $caldavBackend) {
|
||||
$this->caldavBackend = $caldavBackend;
|
||||
$this->l10n = \OC::$server->getL10N('dav');
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
function getName() {
|
||||
return 'public-calendars';
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
function getChild($name) {
|
||||
$calendar = $this->caldavBackend->getPublicCalendar($name);
|
||||
return new Calendar($this->caldavBackend, $calendar, $this->l10n);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
function getChildren() {
|
||||
$calendars = $this->caldavBackend->getPublicCalendars();
|
||||
$children = [];
|
||||
foreach ($calendars as $calendar) {
|
||||
// TODO: maybe implement a new class PublicCalendar ???
|
||||
$children[] = new Calendar($this->caldavBackend, $calendar, $this->l10n);
|
||||
}
|
||||
|
||||
return $children;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,227 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Thomas Citharel <tcit@tcit.fr>
|
||||
*
|
||||
* @copyright Copyright (c) 2016 Thomas Citharel <tcit@tcit.fr>
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This code is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License, version 3,
|
||||
* as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License, version 3,
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
*/
|
||||
namespace OCA\DAV\CalDAV\Publishing;
|
||||
|
||||
use Sabre\DAV\PropFind;
|
||||
use Sabre\DAV\INode;
|
||||
use Sabre\DAV\Server;
|
||||
use Sabre\DAV\ServerPlugin;
|
||||
use Sabre\DAV\Exception\NotFound;
|
||||
use Sabre\HTTP\RequestInterface;
|
||||
use Sabre\HTTP\ResponseInterface;
|
||||
use Sabre\CalDAV\Xml\Property\AllowedSharingModes;
|
||||
use OCA\DAV\CalDAV\Publishing\Xml\Publisher;
|
||||
use OCA\DAV\CalDAV\Calendar;
|
||||
use OCP\IURLGenerator;
|
||||
use OCP\IConfig;
|
||||
|
||||
class PublishPlugin extends ServerPlugin {
|
||||
const NS_CALENDARSERVER = 'http://calendarserver.org/ns/';
|
||||
|
||||
/**
|
||||
* Reference to SabreDAV server object.
|
||||
*
|
||||
* @var \Sabre\DAV\Server
|
||||
*/
|
||||
protected $server;
|
||||
|
||||
/**
|
||||
* Config instance to get instance secret.
|
||||
*
|
||||
* @var IConfig
|
||||
*/
|
||||
protected $config;
|
||||
|
||||
/**
|
||||
* URL Generator for absolute URLs.
|
||||
*
|
||||
* @var IURLGenerator
|
||||
*/
|
||||
protected $urlGenerator;
|
||||
|
||||
/**
|
||||
* PublishPlugin constructor.
|
||||
*
|
||||
* @param IConfig $config
|
||||
* @param IURLGenerator $urlGenerator
|
||||
*/
|
||||
public function __construct(IConfig $config, IURLGenerator $urlGenerator) {
|
||||
$this->config = $config;
|
||||
$this->urlGenerator = $urlGenerator;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method should return a list of server-features.
|
||||
*
|
||||
* This is for example 'versioning' and is added to the DAV: header
|
||||
* in an OPTIONS response.
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function getFeatures() {
|
||||
// May have to be changed to be detected
|
||||
return ['oc-calendar-publishing', 'calendarserver-sharing'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a plugin name.
|
||||
*
|
||||
* Using this name other plugins will be able to access other plugins
|
||||
* using Sabre\DAV\Server::getPlugin
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getPluginName() {
|
||||
return 'oc-calendar-publishing';
|
||||
}
|
||||
|
||||
/**
|
||||
* This initializes the plugin.
|
||||
*
|
||||
* This function is called by Sabre\DAV\Server, after
|
||||
* addPlugin is called.
|
||||
*
|
||||
* This method should set up the required event subscriptions.
|
||||
*
|
||||
* @param Server $server
|
||||
*/
|
||||
public function initialize(Server $server) {
|
||||
$this->server = $server;
|
||||
|
||||
$this->server->on('method:POST', [$this, 'httpPost']);
|
||||
$this->server->on('propFind', [$this, 'propFind']);
|
||||
}
|
||||
|
||||
public function propFind(PropFind $propFind, INode $node) {
|
||||
if ($node instanceof Calendar) {
|
||||
$propFind->handle('{'.self::NS_CALENDARSERVER.'}publish-url', function () use ($node) {
|
||||
if ($node->getPublishStatus()) {
|
||||
// We return the publish-url only if the calendar is published.
|
||||
$token = $node->getPublishStatus();
|
||||
$publishUrl = $this->urlGenerator->getAbsoluteURL($this->server->getBaseUri().'public-calendars/').$token;
|
||||
|
||||
return new Publisher($publishUrl, true);
|
||||
}
|
||||
});
|
||||
|
||||
$propFind->handle('{'.self::NS_CALENDARSERVER.'}allowed-sharing-modes', function() use ($node) {
|
||||
return new AllowedSharingModes(!$node->isSubscription(), !$node->isSubscription());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* We intercept this to handle POST requests on calendars.
|
||||
*
|
||||
* @param RequestInterface $request
|
||||
* @param ResponseInterface $response
|
||||
*
|
||||
* @return void|bool
|
||||
*/
|
||||
public function httpPost(RequestInterface $request, ResponseInterface $response) {
|
||||
$path = $request->getPath();
|
||||
|
||||
// Only handling xml
|
||||
$contentType = $request->getHeader('Content-Type');
|
||||
if (strpos($contentType, 'application/xml') === false && strpos($contentType, 'text/xml') === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Making sure the node exists
|
||||
try {
|
||||
$node = $this->server->tree->getNodeForPath($path);
|
||||
} catch (NotFound $e) {
|
||||
return;
|
||||
}
|
||||
|
||||
$requestBody = $request->getBodyAsString();
|
||||
|
||||
// If this request handler could not deal with this POST request, it
|
||||
// will return 'null' and other plugins get a chance to handle the
|
||||
// request.
|
||||
//
|
||||
// However, we already requested the full body. This is a problem,
|
||||
// because a body can only be read once. This is why we preemptively
|
||||
// re-populated the request body with the existing data.
|
||||
$request->setBody($requestBody);
|
||||
|
||||
$this->server->xml->parse($requestBody, $request->getUrl(), $documentType);
|
||||
|
||||
switch ($documentType) {
|
||||
|
||||
case '{'.self::NS_CALENDARSERVER.'}publish-calendar' :
|
||||
|
||||
// We can only deal with IShareableCalendar objects
|
||||
if (!$node instanceof Calendar) {
|
||||
return;
|
||||
}
|
||||
$this->server->transactionType = 'post-publish-calendar';
|
||||
|
||||
// Getting ACL info
|
||||
$acl = $this->server->getPlugin('acl');
|
||||
|
||||
// If there's no ACL support, we allow everything
|
||||
if ($acl) {
|
||||
$acl->checkPrivileges($path, '{DAV:}write');
|
||||
}
|
||||
|
||||
$node->setPublishStatus(true);
|
||||
|
||||
// iCloud sends back the 202, so we will too.
|
||||
$response->setStatus(202);
|
||||
|
||||
// Adding this because sending a response body may cause issues,
|
||||
// and I wanted some type of indicator the response was handled.
|
||||
$response->setHeader('X-Sabre-Status', 'everything-went-well');
|
||||
|
||||
// Breaking the event chain
|
||||
return false;
|
||||
|
||||
case '{'.self::NS_CALENDARSERVER.'}unpublish-calendar' :
|
||||
|
||||
// We can only deal with IShareableCalendar objects
|
||||
if (!$node instanceof Calendar) {
|
||||
return;
|
||||
}
|
||||
$this->server->transactionType = 'post-unpublish-calendar';
|
||||
|
||||
// Getting ACL info
|
||||
$acl = $this->server->getPlugin('acl');
|
||||
|
||||
// If there's no ACL support, we allow everything
|
||||
if ($acl) {
|
||||
$acl->checkPrivileges($path, '{DAV:}write');
|
||||
}
|
||||
|
||||
$node->setPublishStatus(false);
|
||||
|
||||
$response->setStatus(200);
|
||||
|
||||
// Adding this because sending a response body may cause issues,
|
||||
// and I wanted some type of indicator the response was handled.
|
||||
$response->setHeader('X-Sabre-Status', 'everything-went-well');
|
||||
|
||||
// Breaking the event chain
|
||||
return false;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,83 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Thomas Citharel <tcit@tcit.fr>
|
||||
*
|
||||
* @copyright Copyright (c) 2016 Thomas Citharel <tcit@tcit.fr>
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This code is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License, version 3,
|
||||
* as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License, version 3,
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
*/
|
||||
namespace OCA\DAV\CalDAV\Publishing\Xml;
|
||||
|
||||
use OCA\DAV\CalDAV\Publishing\PublishPlugin as Plugin;
|
||||
use Sabre\Xml\Writer;
|
||||
use Sabre\Xml\XmlSerializable;
|
||||
|
||||
class Publisher implements XmlSerializable {
|
||||
|
||||
/**
|
||||
* @var string $publishUrl
|
||||
*/
|
||||
protected $publishUrl;
|
||||
|
||||
/**
|
||||
* @var boolean $isPublished
|
||||
*/
|
||||
protected $isPublished;
|
||||
|
||||
/**
|
||||
* @param string $publishUrl
|
||||
* @param boolean $isPublished
|
||||
*/
|
||||
function __construct($publishUrl, $isPublished) {
|
||||
$this->publishUrl = $publishUrl;
|
||||
$this->isPublished = $isPublished;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
function getValue() {
|
||||
return $this->publishUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* The xmlSerialize metod is called during xml writing.
|
||||
*
|
||||
* Use the $writer argument to write its own xml serialization.
|
||||
*
|
||||
* An important note: do _not_ create a parent element. Any element
|
||||
* implementing XmlSerializble should only ever write what's considered
|
||||
* its 'inner xml'.
|
||||
*
|
||||
* The parent of the current element is responsible for writing a
|
||||
* containing element.
|
||||
*
|
||||
* This allows serializers to be re-used for different element names.
|
||||
*
|
||||
* If you are opening new elements, you must also close them again.
|
||||
*
|
||||
* @param Writer $writer
|
||||
* @return void
|
||||
*/
|
||||
function xmlSerialize(Writer $writer) {
|
||||
if (!$this->isPublished) {
|
||||
// for pre-publish-url
|
||||
$writer->write($this->publishUrl);
|
||||
} else {
|
||||
// for publish-url
|
||||
$writer->writeElement('{DAV:}href', $this->publishUrl);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,92 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
||||
*
|
||||
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
||||
* @license AGPL-3.0
|
||||
*
|
||||
* This code is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License, version 3,
|
||||
* as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License, version 3,
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
*/
|
||||
namespace OCA\DAV\DAV;
|
||||
|
||||
use Sabre\DAV\Auth\Backend\BackendInterface;
|
||||
use Sabre\HTTP\RequestInterface;
|
||||
use Sabre\HTTP\ResponseInterface;
|
||||
|
||||
class PublicAuth implements BackendInterface {
|
||||
|
||||
/** @var string[] */
|
||||
private $publicURLs;
|
||||
|
||||
public function __construct() {
|
||||
$this->publicURLs = [
|
||||
'public-calendars',
|
||||
'principals/system/public'
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* When this method is called, the backend must check if authentication was
|
||||
* successful.
|
||||
*
|
||||
* The returned value must be one of the following
|
||||
*
|
||||
* [true, "principals/username"]
|
||||
* [false, "reason for failure"]
|
||||
*
|
||||
* If authentication was successful, it's expected that the authentication
|
||||
* backend returns a so-called principal url.
|
||||
*
|
||||
* Examples of a principal url:
|
||||
*
|
||||
* principals/admin
|
||||
* principals/user1
|
||||
* principals/users/joe
|
||||
* principals/uid/123457
|
||||
*
|
||||
* If you don't use WebDAV ACL (RFC3744) we recommend that you simply
|
||||
* return a string such as:
|
||||
*
|
||||
* principals/users/[username]
|
||||
*
|
||||
* @param RequestInterface $request
|
||||
* @param ResponseInterface $response
|
||||
* @return array
|
||||
*/
|
||||
function check(RequestInterface $request, ResponseInterface $response) {
|
||||
|
||||
if ($this->isRequestPublic($request)) {
|
||||
return [true, "principals/system/public"];
|
||||
}
|
||||
return [false, "No public access to this resource."];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
function challenge(RequestInterface $request, ResponseInterface $response) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param RequestInterface $request
|
||||
* @return bool
|
||||
*/
|
||||
private function isRequestPublic(RequestInterface $request) {
|
||||
$url = $request->getPath();
|
||||
$matchingUrls = array_filter($this->publicURLs, function ($publicUrl) use ($url) {
|
||||
return strpos($url, $publicUrl, 0) === 0;
|
||||
});
|
||||
return !empty($matchingUrls);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,122 @@
|
||||
<?php
|
||||
|
||||
namespace OCA\DAV\Tests\unit\CalDAV;
|
||||
|
||||
use OCA\DAV\CalDAV\Calendar;
|
||||
use OCA\DAV\Connector\Sabre\Principal;
|
||||
use OCP\IL10N;
|
||||
use OCA\DAV\CalDAV\CalDavBackend;
|
||||
use OCA\DAV\CalDAV\PublicCalendarRoot;
|
||||
use OCP\IUserManager;
|
||||
use OCP\Security\ISecureRandom;
|
||||
use Test\TestCase;
|
||||
|
||||
/**
|
||||
* Class PublicCalendarRootTest
|
||||
*
|
||||
* @group DB
|
||||
*
|
||||
* @package OCA\DAV\Tests\unit\CalDAV
|
||||
*/
|
||||
class PublicCalendarRootTest extends TestCase {
|
||||
|
||||
const UNIT_TEST_USER = 'principals/users/caldav-unit-test';
|
||||
/** @var CalDavBackend */
|
||||
private $backend;
|
||||
/** @var PublicCalendarRoot */
|
||||
private $publicCalendarRoot;
|
||||
/** @var IL10N */
|
||||
private $l10n;
|
||||
/** @var IUserManager */
|
||||
private $userManager;
|
||||
/** @var Principal */
|
||||
private $principal;
|
||||
/** var IConfig */
|
||||
protected $config;
|
||||
/** @var ISecureRandom */
|
||||
private $random;
|
||||
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$db = \OC::$server->getDatabaseConnection();
|
||||
$this->principal = $this->getMockBuilder('OCA\DAV\Connector\Sabre\Principal')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$this->config = \OC::$server->getConfig();
|
||||
$this->userManager = $this->getMockBuilder('\OCP\IUserManager')->getMock();
|
||||
$this->random = \OC::$server->getSecureRandom();
|
||||
|
||||
$this->backend = new CalDavBackend(
|
||||
$db,
|
||||
$this->principal,
|
||||
$this->userManager,
|
||||
$this->config,
|
||||
$this->random
|
||||
);
|
||||
|
||||
$this->publicCalendarRoot = new PublicCalendarRoot($this->backend);
|
||||
|
||||
$this->l10n = $this->getMockBuilder('\OCP\IL10N')
|
||||
->disableOriginalConstructor()->getMock();
|
||||
}
|
||||
|
||||
public function tearDown() {
|
||||
parent::tearDown();
|
||||
|
||||
if (is_null($this->backend)) {
|
||||
return;
|
||||
}
|
||||
$books = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER);
|
||||
foreach ($books as $book) {
|
||||
$this->backend->deleteCalendar($book['id']);
|
||||
}
|
||||
}
|
||||
|
||||
public function testGetName() {
|
||||
$name = $this->publicCalendarRoot->getName();
|
||||
$this->assertEquals('public-calendars', $name);
|
||||
}
|
||||
|
||||
public function testGetChild() {
|
||||
|
||||
$calendar = $this->createPublicCalendar();
|
||||
|
||||
$publicCalendars = $this->backend->getPublicCalendars();
|
||||
$this->assertEquals(1, count($publicCalendars));
|
||||
$this->assertEquals(true, $publicCalendars[0]['{http://owncloud.org/ns}public']);
|
||||
|
||||
$publicCalendarURI = $publicCalendars[0]['uri'];
|
||||
|
||||
$calendarResult = $this->publicCalendarRoot->getChild($publicCalendarURI);
|
||||
$this->assertEquals($calendar, $calendarResult);
|
||||
}
|
||||
|
||||
public function testGetChildren() {
|
||||
$this->createPublicCalendar();
|
||||
|
||||
$publicCalendars = $this->backend->getPublicCalendars();
|
||||
|
||||
$calendarResults = $this->publicCalendarRoot->getChildren();
|
||||
|
||||
$this->assertEquals(1, count($calendarResults));
|
||||
$this->assertEquals(new Calendar($this->backend, $publicCalendars[0], $this->l10n), $calendarResults[0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Calendar
|
||||
*/
|
||||
protected function createPublicCalendar() {
|
||||
$this->backend->createCalendar(self::UNIT_TEST_USER, 'Example', []);
|
||||
|
||||
$calendarInfo = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER)[0];
|
||||
$calendar = new Calendar($this->backend, $calendarInfo, $this->l10n);
|
||||
$publicUri = $calendar->setPublishStatus(true);
|
||||
|
||||
$calendarInfo = $this->backend->getPublicCalendar($publicUri);
|
||||
$calendar = new Calendar($this->backend, $calendarInfo, $this->l10n);
|
||||
|
||||
return $calendar;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace OCA\DAV\Tests\unit\CalDAV\Publishing;
|
||||
|
||||
use OCA\DAV\CalDAV\Publishing\Xml\Publisher;
|
||||
use Sabre\Xml\Writer;
|
||||
|
||||
class PublisherTest extends \PHPUnit_Framework_TestCase {
|
||||
|
||||
const NS_CALENDARSERVER = 'http://calendarserver.org/ns/';
|
||||
|
||||
public function testSerializePublished() {
|
||||
$publish = new Publisher('urltopublish', true);
|
||||
|
||||
$xml = $this->write([
|
||||
'{' . self::NS_CALENDARSERVER . '}publish-url' => $publish,
|
||||
]);
|
||||
|
||||
$this->assertEquals('urltopublish', $publish->getValue());
|
||||
|
||||
$this->assertXmlStringEqualsXmlString(
|
||||
'<?xml version="1.0"?>
|
||||
<x1:publish-url xmlns:d="DAV:" xmlns:x1="' . self::NS_CALENDARSERVER . '">
|
||||
<d:href>urltopublish</d:href>
|
||||
</x1:publish-url>', $xml);
|
||||
}
|
||||
|
||||
public function testSerializeNotPublished() {
|
||||
$publish = new Publisher('urltopublish', false);
|
||||
|
||||
$xml = $this->write([
|
||||
'{' . self::NS_CALENDARSERVER . '}pre-publish-url' => $publish,
|
||||
]);
|
||||
|
||||
$this->assertEquals('urltopublish', $publish->getValue());
|
||||
|
||||
$this->assertXmlStringEqualsXmlString(
|
||||
'<?xml version="1.0"?>
|
||||
<x1:pre-publish-url xmlns:d="DAV:" xmlns:x1="' . self::NS_CALENDARSERVER . '">urltopublish</x1:pre-publish-url>', $xml);
|
||||
}
|
||||
|
||||
|
||||
protected $elementMap = [];
|
||||
protected $namespaceMap = ['DAV:' => 'd'];
|
||||
protected $contextUri = '/';
|
||||
|
||||
private function write($input) {
|
||||
$writer = new Writer();
|
||||
$writer->contextUri = $this->contextUri;
|
||||
$writer->namespaceMap = $this->namespaceMap;
|
||||
$writer->openMemory();
|
||||
$writer->setIndent(true);
|
||||
$writer->write($input);
|
||||
return $writer->outputMemory();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace OCA\DAV\Tests\unit\CalDAV\Publishing;
|
||||
|
||||
use OCA\DAV\CalDAV\Calendar;
|
||||
use OCA\DAV\CalDAV\Publishing\PublishPlugin;
|
||||
use OCP\IRequest;
|
||||
use OCP\IURLGenerator;
|
||||
use OCP\IConfig;
|
||||
use Sabre\DAV\Server;
|
||||
use Sabre\DAV\SimpleCollection;
|
||||
use Sabre\HTTP\Request;
|
||||
use Sabre\HTTP\Response;
|
||||
use Test\TestCase;
|
||||
|
||||
class PluginTest extends TestCase {
|
||||
|
||||
/** @var PublishPlugin */
|
||||
private $plugin;
|
||||
/** @var Server */
|
||||
private $server;
|
||||
/** @var Calendar | \PHPUnit_Framework_MockObject_MockObject */
|
||||
private $book;
|
||||
/** @var IConfig | \PHPUnit_Framework_MockObject_MockObject */
|
||||
private $config;
|
||||
/** @var IURLGenerator | \PHPUnit_Framework_MockObject_MockObject */
|
||||
private $urlGenerator;
|
||||
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->config = $this->getMockBuilder('\OCP\IConfig')->
|
||||
disableOriginalConstructor()->
|
||||
getMock();
|
||||
$this->config->expects($this->any())->method('getSystemValue')
|
||||
->with($this->equalTo('secret'))
|
||||
->willReturn('mysecret');
|
||||
|
||||
$this->urlGenerator = $this->getMockBuilder('OCP\IURLGenerator')->
|
||||
disableOriginalConstructor()->
|
||||
getMock();
|
||||
|
||||
/** @var IRequest $request */
|
||||
$this->plugin = new PublishPlugin($this->config, $this->urlGenerator);
|
||||
|
||||
$root = new SimpleCollection('calendars');
|
||||
$this->server = new Server($root);
|
||||
/** @var SimpleCollection $node */
|
||||
$this->book = $this->getMockBuilder('OCA\DAV\CalDAV\Calendar')->
|
||||
disableOriginalConstructor()->
|
||||
getMock();
|
||||
$this->book->method('getName')->willReturn('cal1');
|
||||
$root->addChild($this->book);
|
||||
$this->plugin->initialize($this->server);
|
||||
}
|
||||
|
||||
public function testPublishing() {
|
||||
|
||||
$this->book->expects($this->once())->method('setPublishStatus')->with(true);
|
||||
|
||||
// setup request
|
||||
$request = new Request();
|
||||
$request->addHeader('Content-Type', 'application/xml');
|
||||
$request->setUrl('cal1');
|
||||
$request->setBody('<o:publish-calendar xmlns:o="http://calendarserver.org/ns/"/>');
|
||||
$response = new Response();
|
||||
$this->plugin->httpPost($request, $response);
|
||||
}
|
||||
|
||||
public function testUnPublishing() {
|
||||
|
||||
$this->book->expects($this->once())->method('setPublishStatus')->with(false);
|
||||
|
||||
// setup request
|
||||
$request = new Request();
|
||||
$request->addHeader('Content-Type', 'application/xml');
|
||||
$request->setUrl('cal1');
|
||||
$request->setBody('<o:unpublish-calendar xmlns:o="http://calendarserver.org/ns/"/>');
|
||||
$response = new Response();
|
||||
$this->plugin->httpPost($request, $response);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue