[Pkg-owncloud-commits] [php-sabredav] 110/220: MySQL PDO sharing is now tested.

David Prévot taffit at moszumanska.debian.org
Thu May 12 01:21:14 UTC 2016


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

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

commit 18c2fd5fb1dd95326452efad745b7a2af6114cb4
Author: Evert Pot <me at evertpot.com>
Date:   Wed Mar 30 00:14:38 2016 -0400

    MySQL PDO sharing is now tested.
---
 examples/sql/mysql.calendars.sql               |   7 +-
 lib/CalDAV/Backend/PDO.php                     | 120 +++++++++++++++++--
 lib/DAV/Xml/Element/Sharee.php                 |   9 ++
 tests/Sabre/CalDAV/Backend/AbstractPDOTest.php | 160 +++++++++++++++++++++++++
 4 files changed, 283 insertions(+), 13 deletions(-)

diff --git a/examples/sql/mysql.calendars.sql b/examples/sql/mysql.calendars.sql
index 4785b7f..7581efc 100644
--- a/examples/sql/mysql.calendars.sql
+++ b/examples/sql/mysql.calendars.sql
@@ -31,7 +31,12 @@ CREATE TABLE calendarinstances (
     calendarcolor VARBINARY(10),
     timezone TEXT,
     transparent TINYINT(1) NOT NULL DEFAULT '0',
-    UNIQUE(principaluri, uri)
+    share_href VARBINARY(100),
+    share_displayname VARCHAR(100),
+    share_invitestatus TINYINT(1) NOT NULL DEFAULT '2' COMMENT '1 = noresponse, 2 = accepted, 3 = declined, 4 = invalid',
+    UNIQUE(principaluri, uri),
+    UNIQUE(calendarid, principaluri),
+    UNIQUE(calendarid, share_href)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
 
 CREATE TABLE calendarchanges (
diff --git a/lib/CalDAV/Backend/PDO.php b/lib/CalDAV/Backend/PDO.php
index 3dc808a..ac4419a 100644
--- a/lib/CalDAV/Backend/PDO.php
+++ b/lib/CalDAV/Backend/PDO.php
@@ -167,7 +167,7 @@ class PDO extends AbstractBackend
         // Making fields a comma-delimited list
         $fields = implode(', ', $fields);
         $stmt = $this->pdo->prepare(<<<SQL
-SELECT calendars.id as id, $fields FROM {$this->calendarInstancesTableName}
+SELECT {$this->calendarInstancesTableName}.id as id, $fields FROM {$this->calendarInstancesTableName}
     LEFT JOIN {$this->calendarTableName} ON
         {$this->calendarInstancesTableName}.calendarid = {$this->calendarTableName}.id
 WHERE principaluri = ? ORDER BY calendarorder ASC
@@ -184,7 +184,7 @@ SQL
             }
 
             $calendar = [
-                'id'                                                                 => [$row['id'], $row['calendarid']],
+                'id'                                                                 => [(int)$row['calendarid'], (int)$row['id']],
                 'uri'                                                                => $row['uri'],
                 'principaluri'                                                       => $row['principaluri'],
                 '{' . CalDAV\Plugin::NS_CALENDARSERVER . '}getctag'                  => 'http://sabre.io/ns/sync/' . ($row['synctoken'] ? $row['synctoken'] : '0'),
@@ -197,14 +197,14 @@ SQL
             // 1 = owner, 2 = readonly, 3 = readwrite
             if ($row['access'] > 1) {
                 // We need to find more information about the original owner.
-                $stmt2 = $this->pdo->prepare('SELECT principaluri FROM ' . $this->calendarInstancesTableName . ' WHERE access = 1 AND id = ?');
-                $stmt2->execute([$row['id']]);
+                //$stmt2 = $this->pdo->prepare('SELECT principaluri FROM ' . $this->calendarInstancesTableName . ' WHERE access = 1 AND id = ?');
+                //$stmt2->execute([$row['id']]);
 
-                $calendar['share-access'] = $row['access'];
+                $calendar['share-access'] = (int)$row['access'];
 
                 // read-only is for backwards compatbility. Might go away in
                 // the future.
-                $calendar['read-only'] = $row['access'] === \Sabre\DAV\Sharing\Plugin::ACCESS_READONLY;
+                $calendar['read-only'] = (int)$row['access'] === \Sabre\DAV\Sharing\Plugin::ACCESS_READ;
             }
 
             foreach ($this->propertyMap as $xmlName => $dbName) {
@@ -1328,7 +1328,95 @@ SQL;
      */
     function updateInvites($calendarId, array $sharees) {
 
-        throw new \Exception('Not implemented');
+        if (!is_array($calendarId)) {
+            throw new \LogicException('The value passed to $calendarId is expected to be an array with a calendarId and an instanceId');
+        }
+        $currentInvites = $this->getInvites($calendarId);
+        list($calendarId, $instanceId) = $calendarId;
+
+        $removeStmt = $this->pdo->prepare("DELETE FROM " . $this->calendarInstancesTableName . " WHERE calendarid = ? AND share_href = ? AND access IN (2,3)");
+        $updateStmt = $this->pdo->prepare("UPDATE " . $this->calendarInstancesTableName . " SET access = ?, share_displayname = ?, share_invitestatus = ? WHERE calendarid = ? AND share_href = ?");
+
+        $insertStmt = $this->pdo->prepare('
+INSERT INTO ' . $this->calendarInstancesTableName . '
+    (
+        calendarid,
+        principaluri,
+        access,
+        displayname,
+        uri,
+        description,
+        calendarorder,
+        calendarcolor,
+        timezone,
+        transparent,
+        share_href,
+        share_displayname,
+        share_invitestatus
+    )
+    SELECT
+        ?,
+        ?,
+        ?,
+        displayname,
+        ?,
+        description,
+        calendarorder,
+        calendarcolor,
+        timezone,
+        1,
+        ?,
+        ?,
+        ?
+    FROM ' . $this->calendarInstancesTableName . ' WHERE id = ?');
+
+        foreach($sharees as $sharee) {
+
+            if ($sharee->access === \Sabre\DAV\Sharing\Plugin::ACCESS_NOACCESS) {
+                // if access was set no NOACCESS, it means access for an
+                // existing sharee was removed.
+                $removeStmt->execute([$calendarId, $sharee->href]);
+                continue;
+            }
+
+            if (is_null($sharee->principal)) {
+                // If the server could not determine the principal automatically,
+                // we will mark the invite status as invalid.
+                $sharee->inviteStatus = \Sabre\DAV\Sharing\Plugin::INVITE_INVALID;
+            }
+
+            foreach($currentInvites as $oldSharee) {
+
+                if ($oldSharee->href === $sharee->href) {
+                    // This is an update
+                    $sharee->properties = array_merge(
+                        $oldSharee->properties,
+                        $sharee->properties
+                    );
+                    $updateStmt->execute([
+                        $sharee->access,
+                        isset($sharee->properties['{DAV:}displayname']) ? $sharee->properties['{DAV:}displayname'] : null,
+                        $sharee->inviteStatus,
+                        $calendarId,
+                        $sharee->href
+                    ]);
+                    continue 2;
+                }
+
+            }
+            // If we got here, it means it was a new sharee
+            $insertStmt->execute([
+                $calendarId,
+                $sharee->principal,
+                $sharee->access,
+                \Sabre\DAV\UUIDUtil::getUUID(),
+                $sharee->href,
+                isset($sharee->properties['{DAV:}displayname']) ? $sharee->properties['{DAV:}displayname'] : null,
+                $sharee->inviteStatus,
+                $instanceId
+            ]);
+
+        }
 
     }
 
@@ -1350,14 +1438,17 @@ SQL;
     function getInvites($calendarId) {
 
         if (!is_array($calendarId)) {
-            throw new \LogicException('The value passed to $calendarId is expected to be an array with a calendarId and an instanceId');
+            throw new \LogicException('The value passed to getInvites() is expected to be an array with a calendarId and an instanceId');
         }
         list($calendarId, $instanceId) = $calendarId;
 
         $query = <<<SQL
 SELECT
     principaluri,
-    access
+    access,
+    share_href,
+    share_displayname,
+    share_invitestatus
 FROM {$this->calendarInstancesTableName}
 WHERE
     calendarid = ?
@@ -1370,10 +1461,15 @@ SQL;
         while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
 
             $result[] = new Sharee([
-                'href'   => \Sabre\HTTP\encodePath($row['principaluri']),
-                'access' => $row['access'],
+                'href'   => isset( $row['share_href'] ) ? $row['share_href'] : \Sabre\HTTP\encodePath($row['principaluri']),
+                'access' => (int)$row['access'],
                 /// Everyone is always immediately accepted, for now.
-                'inviteStatus' => \Sabre\DAV\Sharing\Plugin::INVITE_ACCEPTED,
+                'inviteStatus' => (int)$row['share_invitestatus'],
+                'properties' =>
+                    !empty($row['share_displayname'])
+                    ?  [ '{DAV:}displayname' => $row['share_displayname'] ]
+                    : [],
+                'principal' => $row['principaluri'],
             ]);
 
         }
diff --git a/lib/DAV/Xml/Element/Sharee.php b/lib/DAV/Xml/Element/Sharee.php
index 820f821..be76328 100644
--- a/lib/DAV/Xml/Element/Sharee.php
+++ b/lib/DAV/Xml/Element/Sharee.php
@@ -28,6 +28,15 @@ class Sharee implements Element {
     public $href;
 
     /**
+     * A local principal path. The server will do its best to locate the
+     * principal uri based on the given uri. If we could find a local matching
+     * principal uri, this property will contain the value.
+     *
+     * @var string|null
+     */
+    public $principal;
+
+    /**
      * A list of WebDAV properties that describe the sharee. This might for
      * example contain a {DAV:}displayname with the real name of the user.
      *
diff --git a/tests/Sabre/CalDAV/Backend/AbstractPDOTest.php b/tests/Sabre/CalDAV/Backend/AbstractPDOTest.php
index b71cada..3263048 100644
--- a/tests/Sabre/CalDAV/Backend/AbstractPDOTest.php
+++ b/tests/Sabre/CalDAV/Backend/AbstractPDOTest.php
@@ -5,6 +5,7 @@ namespace Sabre\CalDAV\Backend;
 use Sabre\CalDAV;
 use Sabre\DAV;
 use Sabre\DAV\PropPatch;
+use Sabre\DAV\Xml\Element\Sharee;
 
 abstract class AbstractPDOTest extends \PHPUnit_Framework_TestCase {
 
@@ -875,4 +876,163 @@ abstract class AbstractPDOTest extends \PHPUnit_Framework_TestCase {
 
     }
 
+    function testGetInvites() {
+
+        $backend = new PDO($this->pdo);
+
+        // creating a new calendar
+        $backend->createCalendar('principals/user1', 'somerandomid', []);
+        $calendar = $backend->getCalendarsForUser('principals/user1')[0];
+
+        $result = $backend->getInvites($calendar['id']);
+        $expected = [
+            new Sharee([
+                'href'         => 'principals/user1',
+                'principal'    => 'principals/user1',
+                'access'       => \Sabre\DAV\Sharing\Plugin::ACCESS_SHAREDOWNER,
+                'inviteStatus' => \Sabre\DAV\Sharing\Plugin::INVITE_ACCEPTED,
+            ])
+        ];
+
+        $this->assertEquals($expected, $result);
+
+    }
+
+    /**
+     * @depends testCreateCalendarAndFetch
+     */
+    function testUpdateInvites() {
+
+        $backend = new PDO($this->pdo);
+
+        // creating a new calendar
+        $backend->createCalendar('principals/user1', 'somerandomid', []);
+        $calendar = $backend->getCalendarsForUser('principals/user1')[0];
+
+        $ownerSharee = new Sharee([
+            'href'         => 'principals/user1',
+            'principal'    => 'principals/user1',
+            'access'       => \Sabre\DAV\Sharing\Plugin::ACCESS_SHAREDOWNER,
+            'inviteStatus' => \Sabre\DAV\Sharing\Plugin::INVITE_ACCEPTED,
+        ]);
+
+        // Add a new invite
+        $backend->updateInvites(
+            $calendar['id'],
+            [
+                new Sharee([
+                    'href'         => 'mailto:user at example.org',
+                    'principal'    => 'principals/user2',
+                    'access'       => \Sabre\DAV\Sharing\Plugin::ACCESS_READ,
+                    'inviteStatus' => \Sabre\DAV\Sharing\Plugin::INVITE_ACCEPTED,
+                    'properties'   => ['{DAV:}displayname' => 'User 2'],
+                ])
+            ]
+        );
+
+        $result = $backend->getInvites($calendar['id']);
+        $expected = [
+            $ownerSharee,
+            new Sharee([
+                'href'         => 'mailto:user at example.org',
+                'principal'    => 'principals/user2',
+                'access'       => \Sabre\DAV\Sharing\Plugin::ACCESS_READ,
+                'inviteStatus' => \Sabre\DAV\Sharing\Plugin::INVITE_ACCEPTED,
+                'properties'   => [
+                    '{DAV:}displayname' => 'User 2',
+                ],
+            ])
+        ];
+        $this->assertEquals($expected, $result);
+
+        // Checking calendar_instances too
+        $expectedCalendar = [
+            'id' => [1,2],
+            'principaluri' => 'principals/user2',
+            '{http://calendarserver.org/ns/}getctag' => 'http://sabre.io/ns/sync/1',
+            '{http://sabredav.org/ns}sync-token'     => '1',
+            'share-access' => \Sabre\DAV\Sharing\Plugin::ACCESS_READ,
+            'read-only' => true,
+            'share-resource-uri' => '/ns/share/1',
+        ];
+        $calendars = $backend->getCalendarsForUser('principals/user2');
+
+        foreach($expectedCalendar as $k=>$v) {
+            $this->assertEquals(
+                $v,
+                $calendars[0][$k],
+                "Key " . $k . " in calendars array did not have the expected value."
+            );
+        }
+
+
+        // Updating an invite
+        $backend->updateInvites(
+            $calendar['id'],
+            [
+                new Sharee([
+                    'href'         => 'mailto:user at example.org',
+                    'principal'    => 'principals/user2',
+                    'access'       => \Sabre\DAV\Sharing\Plugin::ACCESS_READWRITE,
+                    'inviteStatus' => \Sabre\DAV\Sharing\Plugin::INVITE_ACCEPTED,
+                ])
+            ]
+        );
+
+        $result = $backend->getInvites($calendar['id']);
+        $expected = [
+            $ownerSharee,
+            new Sharee([
+                'href'         => 'mailto:user at example.org',
+                'principal'    => 'principals/user2',
+                'access'       => \Sabre\DAV\Sharing\Plugin::ACCESS_READWRITE,
+                'inviteStatus' => \Sabre\DAV\Sharing\Plugin::INVITE_ACCEPTED,
+                'properties'   => [
+                    '{DAV:}displayname' => 'User 2',
+                ],
+            ])
+        ];
+        $this->assertEquals($expected, $result);
+
+        // Removing an invite
+        $backend->updateInvites(
+            $calendar['id'],
+            [
+                new Sharee([
+                    'href'         => 'mailto:user at example.org',
+                    'access'       => \Sabre\DAV\Sharing\Plugin::ACCESS_NOACCESS,
+                ])
+            ]
+        );
+
+        $result = $backend->getInvites($calendar['id']);
+        $expected = [
+            $ownerSharee
+        ];
+        $this->assertEquals($expected, $result);
+
+        // Preventing the owner share from being removed
+        $backend->updateInvites(
+            $calendar['id'],
+            [
+                new Sharee([
+                    'href'         => 'principals/user2',
+                    'access'       => \Sabre\DAV\Sharing\Plugin::ACCESS_NOACCESS,
+                ])
+            ]
+        );
+
+        $result = $backend->getInvites($calendar['id']);
+        $expected = [
+            new Sharee([
+                'href'         => 'principals/user1',
+                'principal'    => 'principals/user1',
+                'access'       => \Sabre\DAV\Sharing\Plugin::ACCESS_SHAREDOWNER,
+                'inviteStatus' => \Sabre\DAV\Sharing\Plugin::INVITE_ACCEPTED,
+            ]),
+        ];
+        $this->assertEquals($expected, $result);
+
+    }
+
 }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-php/php-sabredav.git



More information about the Pkg-owncloud-commits mailing list