[Pkg-owncloud-commits] [php-sabredav] 11/42: Migrated notifications-related functionality to its own plugin.
David Prévot
taffit at moszumanska.debian.org
Wed Oct 29 20:52:05 UTC 2014
This is an automated email from the git hooks/post-receive script.
taffit pushed a commit to branch master
in repository php-sabredav.
commit 44102fccd9a69580e9370f3379a0fdbc4e5fb331
Author: Evert Pot <evert at rooftopsolutions.nl>
Date: Sun Oct 5 23:58:04 2014 +0100
Migrated notifications-related functionality to its own plugin.
---
ChangeLog.md | 3 +
lib/CalDAV/Notifications/Plugin.php | 163 +++++++++++++++++++++++
lib/CalDAV/Plugin.php | 70 +---------
tests/Sabre/CalDAV/Notifications/PluginTest.php | 165 ++++++++++++++++++++++++
tests/Sabre/CalDAV/PluginTest.php | 78 -----------
5 files changed, 332 insertions(+), 147 deletions(-)
diff --git a/ChangeLog.md b/ChangeLog.md
index 9c6d30c..a1ec032 100644
--- a/ChangeLog.md
+++ b/ChangeLog.md
@@ -6,6 +6,9 @@ ChangeLog
* Added: calendar-user-address-set to default principal search properties
list. This should fix iOS attendee autocomplete support.
+* Changed: Moved all 'notifications' functionality from `Sabre\CalDAV\Plugin`
+ to a new plugin: `Sabre\CalDAV\Notifications\Plugin`. If you want to use
+ notifications-related functionality, just add this plugin.
2.1.0-alpha1 (2014-09-23)
-------------------------
diff --git a/lib/CalDAV/Notifications/Plugin.php b/lib/CalDAV/Notifications/Plugin.php
new file mode 100644
index 0000000..cd52719
--- /dev/null
+++ b/lib/CalDAV/Notifications/Plugin.php
@@ -0,0 +1,163 @@
+<?php
+
+namespace Sabre\CalDAV\Notifications;
+
+use Sabre\DAV;
+use Sabre\DAV\PropFind;
+use Sabre\DAV\INode as BaseINode;
+use Sabre\DAV\ServerPlugin;
+use Sabre\DAV\Server;
+use Sabre\DAVACL;
+use Sabre\HTTP\RequestInterface;
+use Sabre\HTTP\ResponseInterface;
+
+
+/**
+ * Notifications plugin
+ *
+ * This plugin implements several features required by the caldav-notification
+ * draft specification.
+ *
+ * Before version 2.1.0 this functionality was part of Sabre\CalDAV\Plugin but
+ * this has since been split up.
+ *
+ * @copyright Copyright (C) 2007-2014 fruux GmbH. All rights reserved.
+ * @author Evert Pot (http://evertpot.com/)
+ * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
+ */
+class Plugin extends ServerPlugin {
+
+ /**
+ * This is the namespace for the proprietary calendarserver extensions
+ */
+ const NS_CALENDARSERVER = 'http://calendarserver.org/ns/';
+
+ /**
+ * @var Server
+ */
+ protected $server;
+
+ /**
+ * Returns a plugin name.
+ *
+ * Using this name other plugins will be able to access other plugins
+ * using \Sabre\DAV\Server::getPlugin
+ *
+ * @return string
+ */
+ function getPluginName() {
+
+ return 'notifications';
+
+ }
+
+ /**
+ * 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
+ * @return void
+ */
+ function initialize(Server $server) {
+
+ $this->server = $server;
+ $server->on('method:GET', [$this,'httpGet'], 90);
+ $server->on('propFind', [$this,'propFind']);
+
+ $server->xmlNamespaces[self::NS_CALENDARSERVER] = 'cs';
+ $server->resourceTypeMapping['\\Sabre\\CalDAV\\Notifications\\ICollection'] = '{' . self::NS_CALENDARSERVER . '}notification';
+
+ array_push($server->protectedProperties,
+ '{' . self::NS_CALENDARSERVER . '}notification-URL',
+ '{' . self::NS_CALENDARSERVER . '}notificationtype'
+ );
+
+ }
+
+ /**
+ * PropFind
+ *
+ * @param PropFind $propFind
+ * @param BaseINode $node
+ * @return void
+ */
+ function propFind(PropFind $propFind, BaseINode $node) {
+
+ $caldavPlugin = $this->server->getPlugin('caldav');
+
+ if ($node instanceof DAVACL\IPrincipal) {
+
+ $principalUrl = $node->getPrincipalUrl();
+
+ // notification-URL property
+ $propFind->handle('{' . self::NS_CALENDARSERVER . '}notification-URL', function() use ($node, $principalUrl, $caldavPlugin) {
+
+ $principalId = $node->getName();
+ $notificationPath = $caldavPlugin->getCalendarHomeForPrincipal($principalUrl) . '/notifications/';
+ return new DAV\Property\Href($notificationPath);
+
+ });
+
+ } // instanceof IPrincipal
+
+ if ($node instanceof INode) {
+
+ $propFind->handle(
+ '{' . self::NS_CALENDARSERVER . '}notificationtype',
+ [$node, 'getNotificationType']
+ );
+
+ } // instanceof Notifications_INode
+
+ }
+
+ /**
+ * This event is triggered before the usual GET request handler.
+ *
+ * We use this to intercept GET calls to notification nodes, and return the
+ * proper response.
+ *
+ * @param RequestInterface $request
+ * @param ResponseInterface $response
+ * @return void
+ */
+ function httpGet(RequestInterface $request, ResponseInterface $response) {
+
+ $path = $request->getPath();
+
+ try {
+ $node = $this->server->tree->getNodeForPath($path);
+ } catch (DAV\Exception\NotFound $e) {
+ return;
+ }
+
+ if (!$node instanceof INode)
+ return;
+
+ $dom = new \DOMDocument('1.0', 'UTF-8');
+
+ $dom->formatOutput = true;
+
+ $root = $dom->createElement('cs:notification');
+ foreach($this->server->xmlNamespaces as $namespace => $prefix) {
+ $root->setAttribute('xmlns:' . $prefix, $namespace);
+ }
+
+ $dom->appendChild($root);
+ $node->getNotificationType()->serializeBody($this->server, $root);
+
+ $response->setHeader('Content-Type','application/xml');
+ $response->setHeader('ETag',$node->getETag());
+ $response->setStatus(200);
+ $response->setBody($dom->saveXML());
+
+ // Return false to break the event chain.
+ return false;
+
+ }
+
+}
diff --git a/lib/CalDAV/Plugin.php b/lib/CalDAV/Plugin.php
index f8dcf0f..45ceabe 100644
--- a/lib/CalDAV/Plugin.php
+++ b/lib/CalDAV/Plugin.php
@@ -168,7 +168,6 @@ class Plugin extends DAV\ServerPlugin {
$this->server = $server;
$server->on('method:MKCALENDAR', [$this,'httpMkcalendar']);
- $server->on('method:GET', [$this,'httpGet'], 90);
$server->on('report', [$this,'report']);
$server->on('propFind', [$this,'propFind']);
$server->on('onHTMLActionsPanel', [$this,'htmlActionsPanel']);
@@ -187,7 +186,6 @@ class Plugin extends DAV\ServerPlugin {
$server->resourceTypeMapping['\\Sabre\\CalDAV\\Principal\\IProxyRead'] = '{http://calendarserver.org/ns/}calendar-proxy-read';
$server->resourceTypeMapping['\\Sabre\\CalDAV\\Principal\\IProxyWrite'] = '{http://calendarserver.org/ns/}calendar-proxy-write';
- $server->resourceTypeMapping['\\Sabre\\CalDAV\\Notifications\\ICollection'] = '{' . self::NS_CALENDARSERVER . '}notification';
array_push($server->protectedProperties,
@@ -205,9 +203,7 @@ class Plugin extends DAV\ServerPlugin {
// CalendarServer extensions
'{' . self::NS_CALENDARSERVER . '}getctag',
'{' . self::NS_CALENDARSERVER . '}calendar-proxy-read-for',
- '{' . self::NS_CALENDARSERVER . '}calendar-proxy-write-for',
- '{' . self::NS_CALENDARSERVER . '}notification-URL',
- '{' . self::NS_CALENDARSERVER . '}notificationtype'
+ '{' . self::NS_CALENDARSERVER . '}calendar-proxy-write-for'
);
@@ -391,27 +387,8 @@ class Plugin extends DAV\ServerPlugin {
}
- // notification-URL property
- $propFind->handle('{' . self::NS_CALENDARSERVER . '}notification-URL', function() use ($node, $principalUrl) {
-
- $principalId = $node->getName();
- $notificationPath = $this->getCalendarHomeForPrincipal($principalUrl) . '/notifications/';
- return new DAV\Property\Href($notificationPath);
-
- });
-
} // instanceof IPrincipal
- if ($node instanceof Notifications\INode) {
-
- $propFind->handle(
- '{' . self::NS_CALENDARSERVER . '}notificationtype',
- [$node, 'getNotificationType']
- );
-
- } // instanceof Notifications_INode
-
-
if ($node instanceof ICalendarObject) {
// The calendar-data property is not supposed to be a 'real'
@@ -758,51 +735,6 @@ class Plugin extends DAV\ServerPlugin {
}
/**
- * This event is triggered before the usual GET request handler.
- *
- * We use this to intercept GET calls to notification nodes, and return the
- * proper response.
- *
- * @param RequestInterface $request
- * @param ResponseInterface $response
- * @return void
- */
- function httpGet(RequestInterface $request, ResponseInterface $response) {
-
- $path = $request->getPath();
-
- try {
- $node = $this->server->tree->getNodeForPath($path);
- } catch (DAV\Exception\NotFound $e) {
- return;
- }
-
- if (!$node instanceof Notifications\INode)
- return;
-
- $dom = new \DOMDocument('1.0', 'UTF-8');
-
- $dom->formatOutput = true;
-
- $root = $dom->createElement('cs:notification');
- foreach($this->server->xmlNamespaces as $namespace => $prefix) {
- $root->setAttribute('xmlns:' . $prefix, $namespace);
- }
-
- $dom->appendChild($root);
- $node->getNotificationType()->serializeBody($this->server, $root);
-
- $response->setHeader('Content-Type','application/xml');
- $response->setHeader('ETag',$node->getETag());
- $response->setStatus(200);
- $response->setBody($dom->saveXML());
-
- // Return false to break the event chain.
- return false;
-
- }
-
- /**
* Checks if the submitted iCalendar data is in fact, valid.
*
* An exception is thrown if it's not.
diff --git a/tests/Sabre/CalDAV/Notifications/PluginTest.php b/tests/Sabre/CalDAV/Notifications/PluginTest.php
new file mode 100644
index 0000000..2a2a122
--- /dev/null
+++ b/tests/Sabre/CalDAV/Notifications/PluginTest.php
@@ -0,0 +1,165 @@
+<?php
+
+namespace Sabre\CalDAV\Notifications;
+use Sabre\DAVACL;
+use Sabre\DAV;
+use Sabre\CalDAV;
+use Sabre\HTTP;
+
+class PluginTest extends \PHPUnit_Framework_TestCase {
+
+ /**
+ * @var Sabre\DAV\Server
+ */
+ protected $server;
+ /**
+ * @var Sabre\CalDAV\Plugin
+ */
+ protected $plugin;
+ protected $response;
+ /**
+ * @var Sabre\CalDAV\Backend\PDO
+ */
+ protected $caldavBackend;
+
+ function setup() {
+
+ $this->caldavBackend = new CalDAV\Backend\Mock();
+ $principalBackend = new DAVACL\PrincipalBackend\Mock();
+ $calendars = new CalDAV\CalendarRoot($principalBackend,$this->caldavBackend);
+ $principals = new CalDAV\Principal\Collection($principalBackend);
+
+ $root = new DAV\SimpleCollection('root');
+ $root->addChild($calendars);
+ $root->addChild($principals);
+
+ $this->server = new DAV\Server($root);
+ $this->server->sapi = new HTTP\SapiMock();
+ $this->server->debugExceptions = true;
+ $this->server->setBaseUri('/');
+ $this->plugin = new Plugin();
+ $this->server->addPlugin($this->plugin);
+
+
+ // Adding ACL plugin
+ $this->server->addPlugin(new DAVACL\Plugin());
+
+ // CalDAV is also required.
+ $this->server->addPlugin(new CalDAV\Plugin());
+ // Adding Auth plugin, and ensuring that we are logged in.
+ $authBackend = new DAV\Auth\Backend\Mock();
+ $authBackend->defaultUser = 'user1';
+ $authPlugin = new DAV\Auth\Plugin($authBackend, 'SabreDAV');
+ $this->server->addPlugin($authPlugin);
+
+ // This forces a login
+ $authPlugin->beforeMethod(new HTTP\Request(), new HTTP\Response());
+
+ $this->response = new HTTP\ResponseMock();
+ $this->server->httpResponse = $this->response;
+
+ }
+
+ function testSimple() {
+
+ $this->assertEquals([], $this->plugin->getFeatures());
+ $this->assertEquals('notifications', $this->plugin->getPluginName());
+
+ }
+
+ function testPrincipalProperties() {
+
+ $httpRequest = HTTP\Sapi::createFromServerArray(array(
+ 'HTTP_HOST' => 'sabredav.org',
+ ));
+ $this->server->httpRequest = $httpRequest;
+
+ $props = $this->server->getPropertiesForPath('/principals/user1',array(
+ '{' . Plugin::NS_CALENDARSERVER . '}notification-URL',
+ ));
+
+ $this->assertArrayHasKey(0,$props);
+ $this->assertArrayHasKey(200,$props[0]);
+
+
+ $this->assertArrayHasKey('{'.Plugin::NS_CALENDARSERVER .'}notification-URL',$props[0][200]);
+ $prop = $props[0][200]['{'.Plugin::NS_CALENDARSERVER .'}notification-URL'];
+ $this->assertTrue($prop instanceof DAV\Property\Href);
+ $this->assertEquals('calendars/user1/notifications/', $prop->getHref());
+
+ }
+
+ function testNotificationProperties() {
+
+ $notification = new Node(
+ $this->caldavBackend,
+ 'principals/user1',
+ new Notification\SystemStatus('foo','"1"')
+ );
+ $propFind = new DAV\PropFind('calendars/user1/notifications', [
+ '{' . Plugin::NS_CALENDARSERVER . '}notificationtype',
+ ]);
+
+ $this->plugin->propFind($propFind, $notification);
+
+ $this->assertEquals(
+ $notification->getNotificationType(),
+ $propFind->get('{' . Plugin::NS_CALENDARSERVER . '}notificationtype')
+ );
+
+ }
+
+ function testNotificationGet() {
+
+ $notification = new Node(
+ $this->caldavBackend,
+ 'principals/user1',
+ new Notification\SystemStatus('foo','"1"')
+ );
+
+ $server = new DAV\Server(array($notification));
+ $caldav = new Plugin();
+
+ $server->httpRequest = HTTP\Sapi::createFromServerArray(array(
+ 'REQUEST_URI' => '/foo.xml',
+ ));
+ $httpResponse = new HTTP\ResponseMock();
+ $server->httpResponse = $httpResponse;
+
+ $server->addPlugin($caldav);
+
+ $caldav->httpGet($server->httpRequest, $server->httpResponse);
+
+ $this->assertEquals(200, $httpResponse->status);
+ $this->assertEquals(array(
+ 'Content-Type' => ['application/xml'],
+ 'ETag' => ['"1"'],
+ ), $httpResponse->getHeaders());
+
+ $expected =
+'<?xml version="1.0" encoding="UTF-8"?>
+<cs:notification xmlns:d="DAV:" xmlns:s="http://sabredav.org/ns" xmlns:cs="http://calendarserver.org/ns/">
+ <cs:systemstatus type="high"/>
+</cs:notification>
+';
+
+ $this->assertEquals($expected, $httpResponse->body);
+
+ }
+
+ function testGETPassthrough() {
+
+ $server = new DAV\Server();
+ $caldav = new Plugin();
+
+ $httpResponse = new HTTP\ResponseMock();
+ $server->httpResponse = $httpResponse;
+
+ $server->addPlugin($caldav);
+
+ $this->assertNull($caldav->httpGet(new HTTP\Request('GET','/foozz'), $server->httpResponse));
+
+ }
+
+
+}
diff --git a/tests/Sabre/CalDAV/PluginTest.php b/tests/Sabre/CalDAV/PluginTest.php
index b518f4f..343a5f6 100644
--- a/tests/Sabre/CalDAV/PluginTest.php
+++ b/tests/Sabre/CalDAV/PluginTest.php
@@ -449,11 +449,6 @@ END:VCALENDAR';
$this->assertTrue($prop instanceof DAV\Property\Href);
$this->assertEquals('calendars/user1/',$prop->getHref());
- $this->assertArrayHasKey('{'.Plugin::NS_CALENDARSERVER .'}notification-URL',$props[0][200]);
- $prop = $props[0][200]['{'.Plugin::NS_CALENDARSERVER .'}notification-URL'];
- $this->assertTrue($prop instanceof DAV\Property\Href);
-
-
$this->assertArrayHasKey('{http://calendarserver.org/ns/}calendar-proxy-read-for', $props[0][200]);
$prop = $props[0][200]['{http://calendarserver.org/ns/}calendar-proxy-read-for'];
$this->assertInstanceOf('Sabre\\DAV\\Property\\HrefList', $prop);
@@ -1059,77 +1054,4 @@ END:VCALENDAR';
}
- function testNotificationProperties() {
-
- $notification = new Notifications\Node(
- $this->caldavBackend,
- 'principals/user1',
- new Notifications\Notification\SystemStatus('foo','"1"')
- );
- $propFind = new DAV\PropFind('calendars/user1/notifications', [
- '{' . Plugin::NS_CALENDARSERVER . '}notificationtype',
- ]);
-
- $this->plugin->propFind($propFind, $notification);
-
- $this->assertEquals(
- $notification->getNotificationType(),
- $propFind->get('{' . Plugin::NS_CALENDARSERVER . '}notificationtype')
- );
-
- }
-
- function testNotificationGet() {
-
- $notification = new Notifications\Node(
- $this->caldavBackend,
- 'principals/user1',
- new Notifications\Notification\SystemStatus('foo','"1"')
- );
-
- $server = new DAV\Server(array($notification));
- $caldav = new Plugin();
-
- $server->httpRequest = HTTP\Sapi::createFromServerArray(array(
- 'REQUEST_URI' => '/foo.xml',
- ));
- $httpResponse = new HTTP\ResponseMock();
- $server->httpResponse = $httpResponse;
-
- $server->addPlugin($caldav);
-
- $caldav->httpGet($server->httpRequest, $server->httpResponse);
-
- $this->assertEquals(200, $httpResponse->status);
- $this->assertEquals(array(
- 'Content-Type' => ['application/xml'],
- 'ETag' => ['"1"'],
- ), $httpResponse->getHeaders());
-
- $expected =
-'<?xml version="1.0" encoding="UTF-8"?>
-<cs:notification xmlns:d="DAV:" xmlns:s="http://sabredav.org/ns" xmlns:cal="urn:ietf:params:xml:ns:caldav" xmlns:cs="http://calendarserver.org/ns/">
- <cs:systemstatus type="high"/>
-</cs:notification>
-';
-
- $this->assertEquals($expected, $httpResponse->body);
-
- }
-
- function testGETPassthrough() {
-
- $server = new DAV\Server();
- $caldav = new Plugin();
-
- $httpResponse = new HTTP\ResponseMock();
- $server->httpResponse = $httpResponse;
-
- $server->addPlugin($caldav);
-
- $this->assertNull($caldav->httpGet(new HTTP\Request('GET','/foozz'), $server->httpResponse));
-
- }
-
-
}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/php-sabredav.git
More information about the Pkg-owncloud-commits
mailing list