[Pkg-owncloud-commits] [php-sabredav] 27/148: Moved the invite property to the new xml system.

David Prévot taffit at moszumanska.debian.org
Wed Apr 15 01:37:06 UTC 2015


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository php-sabredav.

commit e5f414f2f5bee397e5ef668fe3bddfc1ff05895c
Author: Evert Pot <me at evertpot.com>
Date:   Sat Feb 7 00:32:36 2015 -0500

    Moved the invite property to the new xml system.
---
 lib/CalDAV/Property/Invite.php                     | 228 ---------------------
 lib/CalDAV/SharingPlugin.php                       |  33 ++-
 lib/CalDAV/Xml/Property/Invite.php                 |  11 +-
 tests/Sabre/CalDAV/SharingPluginTest.php           |   6 +-
 .../Sabre/CalDAV/{ => Xml}/Property/InviteTest.php | 106 +++++-----
 tests/Sabre/DAV/Xml/XmlTest.php                    |  10 +
 6 files changed, 78 insertions(+), 316 deletions(-)

diff --git a/lib/CalDAV/Property/Invite.php b/lib/CalDAV/Property/Invite.php
deleted file mode 100644
index 401fe91..0000000
--- a/lib/CalDAV/Property/Invite.php
+++ /dev/null
@@ -1,228 +0,0 @@
-<?php
-
-namespace Sabre\CalDAV\Property;
-
-use Sabre\CalDAV\SharingPlugin as SharingPlugin;
-use Sabre\DAV;
-use Sabre\CalDAV;
-
-/**
- * Invite property
- *
- * This property encodes the 'invite' property, as defined by
- * the 'caldav-sharing-02' spec, in the http://calendarserver.org/ns/
- * namespace.
- *
- * @see https://trac.calendarserver.org/browser/CalendarServer/trunk/doc/Extensions/caldav-sharing-02.txt
- * @copyright Copyright (C) 2007-2015 fruux GmbH (https://fruux.com/).
- * @author Evert Pot (http://evertpot.com/)
- * @license http://sabre.io/license/ Modified BSD License
- */
-class Invite extends DAV\Property {
-
-    /**
-     * The list of users a calendar has been shared to.
-     *
-     * @var array
-     */
-    protected $users;
-
-    /**
-     * The organizer contains information about the person who shared the
-     * object.
-     *
-     * @var array
-     */
-    protected $organizer;
-
-    /**
-     * Creates the property.
-     *
-     * Users is an array. Each element of the array has the following
-     * properties:
-     *
-     *   * href - Often a mailto: address
-     *   * commonName - Optional, for example a first and lastname for a user.
-     *   * status - One of the SharingPlugin::STATUS_* constants.
-     *   * readOnly - true or false
-     *   * summary - Optional, description of the share
-     *
-     * The organizer key is optional to specify. It's only useful when a
-     * 'sharee' requests the sharing information.
-     *
-     * The organizer may have the following properties:
-     *   * href - Often a mailto: address.
-     *   * commonName - Optional human-readable name.
-     *   * firstName - Optional first name.
-     *   * lastName - Optional last name.
-     *
-     * If you wonder why these two structures are so different, I guess a
-     * valid answer is that the current spec is still a draft.
-     *
-     * @param array $users
-     */
-    function __construct(array $users, array $organizer = null) {
-
-        $this->users = $users;
-        $this->organizer = $organizer;
-
-    }
-
-    /**
-     * Returns the list of users, as it was passed to the constructor.
-     *
-     * @return array
-     */
-    function getValue() {
-
-        return $this->users;
-
-    }
-
-    /**
-     * Serializes the property in a DOMDocument
-     *
-     * @param DAV\Server $server
-     * @param \DOMElement $node
-     * @return void
-     */
-    function serialize(DAV\Server $server,\DOMElement $node) {
-
-        $doc = $node->ownerDocument;
-
-        if (!is_null($this->organizer)) {
-
-            $xorganizer = $doc->createElement('cs:organizer');
-
-            $href = $doc->createElement('d:href');
-            $href->appendChild($doc->createTextNode($this->organizer['href']));
-            $xorganizer->appendChild($href);
-
-            if (isset($this->organizer['commonName']) && $this->organizer['commonName']) {
-                $commonName = $doc->createElement('cs:common-name');
-                $commonName->appendChild($doc->createTextNode($this->organizer['commonName']));
-                $xorganizer->appendChild($commonName);
-            }
-            if (isset($this->organizer['firstName']) && $this->organizer['firstName']) {
-                $firstName = $doc->createElement('cs:first-name');
-                $firstName->appendChild($doc->createTextNode($this->organizer['firstName']));
-                $xorganizer->appendChild($firstName);
-            }
-            if (isset($this->organizer['lastName']) && $this->organizer['lastName']) {
-                $lastName = $doc->createElement('cs:last-name');
-                $lastName->appendChild($doc->createTextNode($this->organizer['lastName']));
-                $xorganizer->appendChild($lastName);
-            }
-
-            $node->appendChild($xorganizer);
-
-
-        }
-
-        foreach($this->users as $user) {
-
-            $xuser = $doc->createElement('cs:user');
-
-            $href = $doc->createElement('d:href');
-            $href->appendChild($doc->createTextNode($user['href']));
-            $xuser->appendChild($href);
-
-            if (isset($user['commonName']) && $user['commonName']) {
-                $commonName = $doc->createElement('cs:common-name');
-                $commonName->appendChild($doc->createTextNode($user['commonName']));
-                $xuser->appendChild($commonName);
-            }
-
-            switch($user['status']) {
-
-                case SharingPlugin::STATUS_ACCEPTED :
-                    $status = $doc->createElement('cs:invite-accepted');
-                    $xuser->appendChild($status);
-                    break;
-                case SharingPlugin::STATUS_DECLINED :
-                    $status = $doc->createElement('cs:invite-declined');
-                    $xuser->appendChild($status);
-                    break;
-                case SharingPlugin::STATUS_NORESPONSE :
-                    $status = $doc->createElement('cs:invite-noresponse');
-                    $xuser->appendChild($status);
-                    break;
-                case SharingPlugin::STATUS_INVALID :
-                    $status = $doc->createElement('cs:invite-invalid');
-                    $xuser->appendChild($status);
-                    break;
-
-            }
-
-            $xaccess = $doc->createElement('cs:access');
-
-            if ($user['readOnly']) {
-                 $xaccess->appendChild(
-                     $doc->createElement('cs:read')
-                 );
-            } else {
-                 $xaccess->appendChild(
-                     $doc->createElement('cs:read-write')
-                 );
-            }
-            $xuser->appendChild($xaccess);
-
-            if (isset($user['summary']) && $user['summary']) {
-                $summary = $doc->createElement('cs:summary');
-                $summary->appendChild($doc->createTextNode($user['summary']));
-                $xuser->appendChild($summary);
-            }
-
-            $node->appendChild($xuser);
-
-        }
-
-
-    }
-
-    /**
-     * Unserializes the property.
-     *
-     * This static method should return a an instance of this object.
-     *
-     * @param \DOMElement $prop
-     * @param array $propertyMap
-     * @return DAV\IProperty
-     */
-    static function unserialize(\DOMElement $prop, array $propertyMap) {
-
-        $xpath = new \DOMXPath($prop->ownerDocument);
-        $xpath->registerNamespace('cs', CalDAV\Plugin::NS_CALENDARSERVER);
-        $xpath->registerNamespace('d',  'urn:DAV');
-
-        $users = array();
-
-        foreach($xpath->query('cs:user', $prop) as $user) {
-
-            $status = null;
-            if ($xpath->evaluate('boolean(cs:invite-accepted)', $user)) {
-                $status = SharingPlugin::STATUS_ACCEPTED;
-            } elseif ($xpath->evaluate('boolean(cs:invite-declined)', $user)) {
-                $status = SharingPlugin::STATUS_DECLINED;
-            } elseif ($xpath->evaluate('boolean(cs:invite-noresponse)', $user)) {
-                $status = SharingPlugin::STATUS_NORESPONSE;
-            } elseif ($xpath->evaluate('boolean(cs:invite-invalid)', $user)) {
-                $status = SharingPlugin::STATUS_INVALID;
-            } else {
-                throw new DAV\Exception('Every cs:user property must have one of cs:invite-accepted, cs:invite-declined, cs:invite-noresponse or cs:invite-invalid');
-            }
-            $users[] = array(
-                'href' => $xpath->evaluate('string(d:href)', $user),
-                'commonName' => $xpath->evaluate('string(cs:common-name)', $user),
-                'readOnly' => $xpath->evaluate('boolean(cs:access/cs:read)', $user),
-                'summary' => $xpath->evaluate('string(cs:summary)', $user),
-                'status' => $status,
-            );
-
-        }
-
-        return new self($users);
-
-    }
-
-}
diff --git a/lib/CalDAV/SharingPlugin.php b/lib/CalDAV/SharingPlugin.php
index 61bc371..faae5fc 100644
--- a/lib/CalDAV/SharingPlugin.php
+++ b/lib/CalDAV/SharingPlugin.php
@@ -2,10 +2,10 @@
 
 namespace Sabre\CalDAV;
 
-use
-    Sabre\DAV,
-    Sabre\HTTP\RequestInterface,
-    Sabre\HTTP\ResponseInterface;
+use Sabre\DAV;
+use Sabre\DAV\Xml\Property\Href;
+use Sabre\HTTP\RequestInterface;
+use Sabre\HTTP\ResponseInterface;
 
 /**
  * This plugin implements support for caldav sharing.
@@ -114,7 +114,7 @@ class SharingPlugin extends DAV\ServerPlugin {
         if ($node instanceof IShareableCalendar) {
 
             $propFind->handle('{' . Plugin::NS_CALENDARSERVER . '}invite', function() use ($node) {
-                return new Property\Invite(
+                return new Xml\Property\Invite(
                     $node->getShares()
                 );
             });
@@ -124,7 +124,7 @@ class SharingPlugin extends DAV\ServerPlugin {
         if ($node instanceof ISharedCalendar) {
 
             $propFind->handle('{' . Plugin::NS_CALENDARSERVER . '}shared-url', function() use ($node) {
-                return new DAV\Property\Href(
+                return new Href(
                     $node->getSharedUrl()
                 );
             });
@@ -154,7 +154,7 @@ class SharingPlugin extends DAV\ServerPlugin {
 
                 }
 
-                return new Property\Invite(
+                return new Xml\Property\Invite(
                     $node->getShares(),
                     $ownerInfo
                 );
@@ -331,20 +331,13 @@ class SharingPlugin extends DAV\ServerPlugin {
                 $response->setHeader('X-Sabre-Status', 'everything-went-well');
 
                 if ($url) {
-                    $dom = new \DOMDocument('1.0', 'UTF-8');
-                    $dom->formatOutput = true;
-
-                    $root = $dom->createElement('cs:shared-as');
-                    foreach($this->server->xmlNamespaces as $namespace => $prefix) {
-                        $root->setAttribute('xmlns:' . $prefix, $namespace);
-                    }
-
-                    $dom->appendChild($root);
-                    $href = new DAV\Property\Href($url);
-
-                    $href->serialize($this->server, $root);
+                    $writer = $this->server->xml->getWriter($this->server->getBaseUri());
+                    $writer->startDocument();
+                    $writer->startElement('{' . Plugin::NS_CALENDARSERVER . '}shared-as');
+                    $writer->write(new Href($url));
+                    $writer->endElement();
                     $response->setHeader('Content-Type','application/xml');
-                    $response->setBody($dom->saveXML());
+                    $response->setBody($writer->outputMemory());
 
                 }
 
diff --git a/lib/CalDAV/Xml/Property/Invite.php b/lib/CalDAV/Xml/Property/Invite.php
index 04cd10c..ed7690a 100644
--- a/lib/CalDAV/Xml/Property/Invite.php
+++ b/lib/CalDAV/Xml/Property/Invite.php
@@ -2,12 +2,11 @@
 
 namespace Sabre\CalDAV\Xml\Property;
 
-use
-    Sabre\Xml\Element,
-    Sabre\Xml\Reader,
-    Sabre\Xml\Writer,
-    Sabre\CalDAV\Plugin,
-    Sabre\CalDAV\SharingPlugin;
+use Sabre\Xml\Element;
+use Sabre\Xml\Reader;
+use Sabre\Xml\Writer;
+use Sabre\CalDAV\Plugin;
+use Sabre\CalDAV\SharingPlugin;
 
 /**
  * Invite property
diff --git a/tests/Sabre/CalDAV/SharingPluginTest.php b/tests/Sabre/CalDAV/SharingPluginTest.php
index 3d1f9fd..1d50074 100644
--- a/tests/Sabre/CalDAV/SharingPluginTest.php
+++ b/tests/Sabre/CalDAV/SharingPluginTest.php
@@ -64,7 +64,7 @@ class SharingPluginTest extends \Sabre\DAVServerTest {
             '{' . Plugin::NS_CALENDARSERVER . '}allowed-sharing-modes',
         ));
 
-        $this->assertInstanceOf('Sabre\\CalDAV\\Property\\Invite', $props['{' . Plugin::NS_CALENDARSERVER . '}invite']);
+        $this->assertInstanceOf('Sabre\\CalDAV\\Xml\\Property\\Invite', $props['{' . Plugin::NS_CALENDARSERVER . '}invite']);
         $this->assertInstanceOf('Sabre\\CalDAV\\Xml\\Property\\AllowedSharingModes', $props['{' . Plugin::NS_CALENDARSERVER . '}allowed-sharing-modes']);
 
     }
@@ -76,8 +76,8 @@ class SharingPluginTest extends \Sabre\DAVServerTest {
             '{' . Plugin::NS_CALENDARSERVER . '}invite',
         ));
 
-        $this->assertInstanceOf('Sabre\\CalDAV\\Property\\Invite', $props['{' . Plugin::NS_CALENDARSERVER . '}invite']);
-        $this->assertInstanceOf('Sabre\\DAV\\Property\\IHref', $props['{' . Plugin::NS_CALENDARSERVER . '}shared-url']);
+        $this->assertInstanceOf('Sabre\\CalDAV\\Xml\\Property\\Invite', $props['{' . Plugin::NS_CALENDARSERVER . '}invite']);
+        $this->assertInstanceOf('Sabre\\DAV\\Xml\\Property\\Href', $props['{' . Plugin::NS_CALENDARSERVER . '}shared-url']);
 
     }
 
diff --git a/tests/Sabre/CalDAV/Property/InviteTest.php b/tests/Sabre/CalDAV/Xml/Property/InviteTest.php
similarity index 67%
rename from tests/Sabre/CalDAV/Property/InviteTest.php
rename to tests/Sabre/CalDAV/Xml/Property/InviteTest.php
index 29f02fd..64102c4 100644
--- a/tests/Sabre/CalDAV/Property/InviteTest.php
+++ b/tests/Sabre/CalDAV/Xml/Property/InviteTest.php
@@ -1,16 +1,24 @@
 <?php
 
-namespace Sabre\CalDAV\Property;
+namespace Sabre\CalDAV\Xml\Property;
 
 use Sabre\CalDAV;
 use Sabre\DAV;
 
-class InviteTest extends \PHPUnit_Framework_TestCase {
+class InviteTest extends DAV\Xml\XmlTest {
+
+    function setUp() {
+
+        $this->namespaceMap[CalDAV\Plugin::NS_CALDAV] = 'cal';
+        $this->namespaceMap[CalDAV\Plugin::NS_CALENDARSERVER] = 'cs';
+
+
+    }
 
     function testSimple() {
 
-        $sccs = new Invite(array());
-        $this->assertInstanceOf('Sabre\CalDAV\Property\Invite', $sccs);
+        $sccs = new Invite([]);
+        $this->assertInstanceOf('Sabre\CalDAV\Xml\Property\Invite', $sccs);
 
     }
 
@@ -19,53 +27,41 @@ class InviteTest extends \PHPUnit_Framework_TestCase {
      */
     function testSerialize() {
 
-        $property = new Invite(array(
-            array(
+        $property = new Invite([
+            [
                 'href' => 'mailto:user1 at example.org',
                 'status' => CalDAV\SharingPlugin::STATUS_ACCEPTED,
                 'readOnly' => false,
-            ),
-            array(
+            ],
+            [
                 'href' => 'mailto:user2 at example.org',
                 'commonName' => 'John Doe',
                 'status' => CalDAV\SharingPlugin::STATUS_DECLINED,
                 'readOnly' => true,
-            ),
-            array(
+            ],
+            [
                 'href' => 'mailto:user3 at example.org',
                 'commonName' => 'Joe Shmoe',
                 'status' => CalDAV\SharingPlugin::STATUS_NORESPONSE,
                 'readOnly' => true,
                 'summary' => 'Something, something',
-            ),
-            array(
+            ],
+            [
                 'href' => 'mailto:user4 at example.org',
                 'commonName' => 'Hoe Boe',
                 'status' => CalDAV\SharingPlugin::STATUS_INVALID,
                 'readOnly' => true,
-            ),
-        ), array(
+            ],
+        ], [
             'href' => 'mailto:thedoctor at example.org',
             'commonName' => 'The Doctor',
             'firstName' => 'The',
             'lastName' => 'Doctor',
-        ));
-
-        $doc = new \DOMDocument();
-        $doc->formatOutput = true;
-        $root = $doc->createElement('d:root');
-        $root->setAttribute('xmlns:d','DAV:');
-        $root->setAttribute('xmlns:cal',CalDAV\Plugin::NS_CALDAV);
-        $root->setAttribute('xmlns:cs',CalDAV\Plugin::NS_CALENDARSERVER);
-
-        $doc->appendChild($root);
-        $server = new DAV\Server();
-
-        $property->serialize($server, $root);
+        ]);
 
-        $xml = $doc->saveXML();
+        $xml = $this->write(['{DAV:}root' => $property]);
 
-        $this->assertEquals(
+        $this->assertXmlStringEqualsXmlString(
 '<?xml version="1.0"?>
 <d:root xmlns:d="DAV:" xmlns:cal="' . CalDAV\Plugin::NS_CALDAV . '" xmlns:cs="' . CalDAV\Plugin::NS_CALENDARSERVER . '">
   <cs:organizer>
@@ -114,67 +110,57 @@ class InviteTest extends \PHPUnit_Framework_TestCase {
     /**
      * @depends testSerialize
      */
-    public function testUnserialize() {
+    function testUnserialize() {
 
-        $input = array(
-            array(
+        $input = [
+            [
                 'href' => 'mailto:user1 at example.org',
                 'status' => CalDAV\SharingPlugin::STATUS_ACCEPTED,
                 'readOnly' => false,
                 'commonName' => '',
                 'summary' => '',
-            ),
-            array(
+            ],
+            [
                 'href' => 'mailto:user2 at example.org',
                 'commonName' => 'John Doe',
                 'status' => CalDAV\SharingPlugin::STATUS_DECLINED,
                 'readOnly' => true,
                 'summary' => '',
-            ),
-            array(
+            ],
+            [
                 'href' => 'mailto:user3 at example.org',
                 'commonName' => 'Joe Shmoe',
                 'status' => CalDAV\SharingPlugin::STATUS_NORESPONSE,
                 'readOnly' => true,
                 'summary' => 'Something, something',
-            ),
-            array(
+            ],
+            [
                 'href' => 'mailto:user4 at example.org',
                 'commonName' => 'Hoe Boe',
                 'status' => CalDAV\SharingPlugin::STATUS_INVALID,
                 'readOnly' => true,
                 'summary' => '',
-            ),
-        );
+            ],
+        ];
 
         // Creating the xml
-        $doc = new \DOMDocument();
-        $doc->formatOutput = true;
-        $root = $doc->createElement('d:root');
-        $root->setAttribute('xmlns:d','DAV:');
-        $root->setAttribute('xmlns:cal',CalDAV\Plugin::NS_CALDAV);
-        $root->setAttribute('xmlns:cs',CalDAV\Plugin::NS_CALENDARSERVER);
-
-        $doc->appendChild($root);
-        $server = new DAV\Server();
-
         $inputProperty = new Invite($input);
-        $inputProperty->serialize($server, $root);
-
-        $xml = $doc->saveXML();
-
+        $xml = $this->write(['{DAV:}root' => $inputProperty]);
         // Parsing it again
 
-        $doc2 = DAV\XMLUtil::loadDOMDocument($xml);
+        $doc2 = $this->parse(
+            $xml,
+            ['{DAV:}root' => 'Sabre\\CalDAV\\Xml\\Property\\Invite']
+        );
 
-        $outputProperty = Invite::unserialize($doc2->firstChild, array());
+        $outputProperty = $doc2['value'];
 
         $this->assertEquals($input, $outputProperty->getValue());
 
     }
 
     /**
-     * @expectedException Sabre\DAV\Exception
+     * @expectedException InvalidArgumentException
      */
     function testUnserializeNoStatus() {
 
@@ -189,8 +175,10 @@ $xml = '<?xml version="1.0"?>
   </cs:user>
 </d:root>';
 
-        $doc2 = DAV\XMLUtil::loadDOMDocument($xml);
-        $outputProperty = Invite::unserialize($doc2->firstChild, array());
+        $this->parse(
+            $xml,
+            ['{DAV:}root' => 'Sabre\\CalDAV\\Xml\\Property\\Invite']
+        );
 
     }
 
diff --git a/tests/Sabre/DAV/Xml/XmlTest.php b/tests/Sabre/DAV/Xml/XmlTest.php
index 299447a..e2aea0b 100644
--- a/tests/Sabre/DAV/Xml/XmlTest.php
+++ b/tests/Sabre/DAV/Xml/XmlTest.php
@@ -3,6 +3,7 @@
 namespace Sabre\DAV\Xml;
 
 use Sabre\Xml\Writer;
+use Sabre\Xml\Reader;
 
 abstract class XmlTest extends \PHPUnit_Framework_TestCase {
 
@@ -20,6 +21,15 @@ abstract class XmlTest extends \PHPUnit_Framework_TestCase {
 
     }
 
+    function parse($xml, $elementMap) {
+
+        $reader = new Reader();
+        $reader->elementMap = $elementMap;
+        $reader->xml($xml);
+        return $reader->parse();
+
+    }
+
     function cleanUp() {
 
         libxml_clear_errors();

-- 
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