[Pkg-owncloud-commits] [php-sabredav] 24/220: Fixing tests wip.

David Prévot taffit at moszumanska.debian.org
Thu May 12 01:21:03 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 4b1587b92ea0f2ba4acedb8cb87e910791b936cf
Author: Evert Pot <me at evertpot.com>
Date:   Tue Oct 20 00:10:56 2015 -0400

    Fixing tests wip.
---
 lib/DAV/Sharing/ISharedNode.php            |   4 +-
 tests/Sabre/CalDAV/Backend/Mock.php        |  50 ++-
 tests/Sabre/CalDAV/CalendarObjectTest.php  |   2 +-
 tests/Sabre/CalDAV/CalendarTest.php        |  32 +-
 tests/Sabre/CalDAV/ICSExportPluginTest.php | 661 +++++++----------------------
 tests/Sabre/DAVServerTest.php              |  46 +-
 6 files changed, 247 insertions(+), 548 deletions(-)

diff --git a/lib/DAV/Sharing/ISharedNode.php b/lib/DAV/Sharing/ISharedNode.php
index 278bf5c..dae0428 100644
--- a/lib/DAV/Sharing/ISharedNode.php
+++ b/lib/DAV/Sharing/ISharedNode.php
@@ -47,7 +47,7 @@ interface ISharedNode extends INode {
      * @return void
      */
     function updateShares(array $add, array $remove);
-    
+
     /**
      * Returns the list of people whom this resource is shared with.
      *
@@ -55,7 +55,7 @@ interface ISharedNode extends INode {
      *   * href - Often a mailto: address
      *   * commonName - Optional, for example a first + last name
      *   * status - See the Sabre\DAV\Sharing\Plugin::STATUS_ constants.
-     *   * access - one of the Sabre\DAV\Sharing\Plugin::ACCESS_ constants. 
+     *   * access - one of the Sabre\DAV\Sharing\Plugin::ACCESS_ constants.
      *
      * @return array
      */
diff --git a/tests/Sabre/CalDAV/Backend/Mock.php b/tests/Sabre/CalDAV/Backend/Mock.php
index 3060d19..8a6d4b4 100644
--- a/tests/Sabre/CalDAV/Backend/Mock.php
+++ b/tests/Sabre/CalDAV/Backend/Mock.php
@@ -82,6 +82,46 @@ class Mock extends AbstractBackend {
     }
 
     /**
+     * Updates properties for a calendar.
+     *
+     * The list of mutations is stored in a Sabre\DAV\PropPatch object.
+     * To do the actual updates, you must tell this object which properties
+     * you're going to process with the handle() method.
+     *
+     * Calling the handle method is like telling the PropPatch object "I
+     * promise I can handle updating this property".
+     *
+     * Read the PropPatch documentation for more info and examples.
+     *
+     * @param mixed $calendarId
+     * @param \Sabre\DAV\PropPatch $propPatch
+     * @return void
+     */
+    function updateCalendar($calendarId, \Sabre\DAV\PropPatch $propPatch) {
+
+        $propPatch->handleRemaining(function($props) use ($calendarId) {
+
+            foreach($this->calendars as $k=>$calendar) {
+
+                if ($calendar['id'] === $calendarId) {
+                    foreach($props as $propName=>$propValue) {
+                        if (is_null($propValue)) {
+                            unset($this->calendars[$k][$propName]);
+                        } else {
+                            $this->calendars[$k][$propName] = $propValue;
+                        }
+                    }
+                    return true;
+
+                }
+
+            }
+
+        });
+
+    }
+
+    /**
      * Delete a calendar and all it's objects
      *
      * @param string $calendarId
@@ -139,18 +179,22 @@ class Mock extends AbstractBackend {
      * Returns information from a single calendar object, based on it's object
      * uri.
      *
+     * The object uri is only the basename, or filename and not a full path.
+     *
      * The returned array must have the same keys as getCalendarObjects. The
      * 'calendardata' object is required here though, while it's not required
      * for getCalendarObjects.
      *
-     * @param string $calendarId
+     * This method must return null if the object did not exist.
+     *
+     * @param mixed $calendarId
      * @param string $objectUri
-     * @return array
+     * @return array|null
      */
     function getCalendarObject($calendarId, $objectUri) {
 
         if (!isset($this->calendarData[$calendarId][$objectUri])) {
-            throw new DAV\Exception\NotFound('Object could not be found');
+            return null;
         }
         $object = $this->calendarData[$calendarId][$objectUri];
         $object['calendarid'] = $calendarId;
diff --git a/tests/Sabre/CalDAV/CalendarObjectTest.php b/tests/Sabre/CalDAV/CalendarObjectTest.php
index 248a31a..ecbaeef 100644
--- a/tests/Sabre/CalDAV/CalendarObjectTest.php
+++ b/tests/Sabre/CalDAV/CalendarObjectTest.php
@@ -130,7 +130,7 @@ class CalendarObjectTest extends \PHPUnit_Framework_TestCase {
         $obj = $children[0];
 
         $lastMod = $obj->getLastModified();
-        $this->assertTrue(is_int($lastMod) || ctype_digit($lastMod));
+        $this->assertTrue(is_int($lastMod) || ctype_digit($lastMod) || is_null($lastMod));
 
     }
 
diff --git a/tests/Sabre/CalDAV/CalendarTest.php b/tests/Sabre/CalDAV/CalendarTest.php
index ea744d2..989867c 100644
--- a/tests/Sabre/CalDAV/CalendarTest.php
+++ b/tests/Sabre/CalDAV/CalendarTest.php
@@ -25,8 +25,6 @@ class CalendarTest extends \PHPUnit_Framework_TestCase {
 
     function setup() {
 
-        if (!SABRE_HASSQLITE) $this->markTestSkipped('SQLite driver is not available');
-
         $this->backend = TestUtil::getBackend();
 
         $this->calendars = $this->backend->getCalendarsForUser('principals/user1');
@@ -250,40 +248,14 @@ class CalendarTest extends \PHPUnit_Framework_TestCase {
 
     function testGetSyncToken() {
 
-        $this->assertEquals(2, $this->calendar->getSyncToken());
-
-    }
-    function testGetSyncToken2() {
-
-        $calendar = new Calendar(new Backend\Mock([],[]), [
-            '{DAV:}sync-token' => 2
-        ]);
-        $this->assertEquals(2, $this->calendar->getSyncToken());
-
-    }
-
-    function testGetSyncTokenNoSyncSupport() {
-
-        $calendar = new Calendar(new Backend\Mock([],[]), []);
-        $this->assertNull($calendar->getSyncToken());
+        $this->assertNull($this->calendar->getSyncToken());
 
     }
 
     function testGetChanges() {
 
-        $this->assertEquals([
-            'syncToken' => 2,
-            'modified'  => [],
-            'deleted'   => [],
-            'added'     => ['UUID-2345'],
-        ], $this->calendar->getChanges(1, 1));
+        $this->assertNull($this->calendar->getChanges(1,1));
 
     }
 
-    function testGetChangesNoSyncSupport() {
-
-        $calendar = new Calendar(new Backend\Mock([],[]), []);
-        $this->assertNull($calendar->getChanges(1,null));
-
-    }
 }
diff --git a/tests/Sabre/CalDAV/ICSExportPluginTest.php b/tests/Sabre/CalDAV/ICSExportPluginTest.php
index 0788b03..b48c58f 100644
--- a/tests/Sabre/CalDAV/ICSExportPluginTest.php
+++ b/tests/Sabre/CalDAV/ICSExportPluginTest.php
@@ -10,62 +10,82 @@ use Sabre\DAVACL;
 require_once 'Sabre/CalDAV/TestUtil.php';
 require_once 'Sabre/HTTP/ResponseMock.php';
 
-class ICSExportPluginTest extends \PHPUnit_Framework_TestCase {
+class ICSExportPluginTest extends \Sabre\DAVServerTest {
+
+    protected $setupCalDAV = true;
+
+    protected $icsExportPlugin;
 
     function setUp() {
 
-        if (!SABRE_HASSQLITE) $this->markTestSkipped('SQLite driver is not available');
+        parent::setUp();
+        $this->icsExportPlugin = new ICSExportPlugin();
+        $this->server->addPlugin(
+            $this->icsExportPlugin
+        );
+
+        $id = $this->caldavBackend->createCalendar(
+            'principals/admin',
+            'UUID-123467',
+            [
+                '{DAV:}displayname'                         => 'Hello!',
+                '{http://apple.com/ns/ical/}calendar-color' => '#AA0000FF',
+            ]
+        );
+
+        $this->caldavBackend->createCalendarObject(
+            $id,
+            'event-1',
+            <<<ICS
+BEGIN:VCALENDAR
+BEGIN:VTIMEZONE
+TZID:Europe/Amsterdam
+END:VTIMEZONE
+BEGIN:VEVENT
+UID:event-1
+DTSTART;TZID=Europe/Amsterdam:20151020T000000
+END:VEVENT
+END:VCALENDAR
+ICS
+        );
+        $this->caldavBackend->createCalendarObject(
+            $id,
+            'todo-1',
+            <<<ICS
+BEGIN:VCALENDAR
+BEGIN:VTODO
+UID:todo-1
+END:VTODO
+END:VCALENDAR
+ICS
+        );
 
     }
 
     function testInit() {
 
-        $p = new ICSExportPlugin();
-        $s = new DAV\Server();
-        $s->addPlugin($p);
-        $this->assertEquals($p, $s->getPlugin('ics-export'));
+        $this->assertEquals(
+            $this->icsExportPlugin,
+            $this->server->getPlugin('ics-export')
+        );
 
     }
 
     function testBeforeMethod() {
 
-        $cbackend = TestUtil::getBackend();
-
-        $props = [
-            'uri'                                       => 'UUID-123467',
-            'principaluri'                              => 'admin',
-            'id'                                        => [1,1],
-            '{DAV:}displayname'                         => 'Hello!',
-            '{http://apple.com/ns/ical/}calendar-color' => '#AA0000FF',
-        ];
-        $tree = [
-            new Calendar($cbackend, $props),
-        ];
-
-        $p = new ICSExportPlugin();
-
-        $s = new DAV\Server($tree);
-        $s->addPlugin($p);
-        $s->addPlugin(new Plugin());
-
-        $h = HTTP\Sapi::createFromServerArray([
-            'REQUEST_URI'    => '/UUID-123467?export',
-            'REQUEST_METHOD' => 'GET',
-        ]);
-
-        $s->httpRequest = $h;
-        $s->httpResponse = new HTTP\ResponseMock();
+        $request = new HTTP\Request(
+            'GET',
+            '/calendars/admin/UUID-123467?export'
+        );
 
-        $this->assertFalse($p->httpGet($h, $s->httpResponse));
+        $response = $this->request($request);
 
-        $this->assertEquals(200, $s->httpResponse->status);
-        $this->assertEquals([
-            'Content-Type' => ['text/calendar'],
-        ], $s->httpResponse->getHeaders());
+        $this->assertEquals(200, $response->getStatus());
+        $this->assertEquals('text/calendar', $response->getHeader('Content-Type'));
 
-        $obj = VObject\Reader::read($s->httpResponse->body);
+        $obj = VObject\Reader::read($response->body);
 
-        $this->assertEquals(7, count($obj->children()));
+        $this->assertEquals(8, count($obj->children()));
         $this->assertEquals(1, count($obj->VERSION));
         $this->assertEquals(1, count($obj->CALSCALE));
         $this->assertEquals(1, count($obj->PRODID));
@@ -78,45 +98,20 @@ class ICSExportPluginTest extends \PHPUnit_Framework_TestCase {
     }
     function testBeforeMethodNoVersion() {
 
-        if (!SABRE_HASSQLITE) $this->markTestSkipped('SQLite driver is not available');
-        $cbackend = TestUtil::getBackend();
-
-        $props = [
-            'uri'          => 'UUID-123467',
-            'principaluri' => 'admin',
-            'id'           => [1,1],
-        ];
-        $tree = [
-            new Calendar($cbackend, $props),
-        ];
-
-        $p = new ICSExportPlugin();
-
-        $s = new DAV\Server($tree);
-
-        $s->addPlugin($p);
-        $s->addPlugin(new Plugin());
-
-        $h = HTTP\Sapi::createFromServerArray([
-            'REQUEST_URI'    => '/UUID-123467?export',
-            'REQUEST_METHOD' => 'GET',
-        ]);
-
-        $s->httpRequest = $h;
-        $s->httpResponse = new HTTP\ResponseMock();
-
+        $request = new HTTP\Request(
+            'GET',
+            '/calendars/admin/UUID-123467?export'
+        );
         DAV\Server::$exposeVersion = false;
-        $this->assertFalse($p->httpGet($h, $s->httpResponse));
+        $response = $this->request($request);
         DAV\Server::$exposeVersion = true;
 
-        $this->assertEquals(200, $s->httpResponse->status);
-        $this->assertEquals([
-            'Content-Type' => ['text/calendar'],
-        ], $s->httpResponse->getHeaders());
+        $this->assertEquals(200, $response->getStatus());
+        $this->assertEquals('text/calendar', $response->getHeader('Content-Type'));
 
-        $obj = VObject\Reader::read($s->httpResponse->body);
+        $obj = VObject\Reader::read($response->body);
 
-        $this->assertEquals(5, count($obj->children()));
+        $this->assertEquals(8, count($obj->children()));
         $this->assertEquals(1, count($obj->VERSION));
         $this->assertEquals(1, count($obj->CALSCALE));
         $this->assertEquals(1, count($obj->PRODID));
@@ -128,105 +123,53 @@ class ICSExportPluginTest extends \PHPUnit_Framework_TestCase {
 
     function testBeforeMethodNoExport() {
 
-        $p = new ICSExportPlugin();
-
-        $s = new DAV\Server();
-        $s->addPlugin($p);
-
-        $h = HTTP\Sapi::createFromServerArray([
-            'REQUEST_URI'    => '/UUID-123467',
-            'REQUEST_METHOD' => 'GET',
-        ]);
-        $this->assertNull($p->httpGet($h, $s->httpResponse));
+        $request = new HTTP\Request(
+            'GET',
+            '/calendars/admin/UUID-123467'
+        );
+        $response = new HTTP\Response();
+        $this->assertNull($this->icsExportPlugin->httpGet($request, $response));
 
     }
 
     function testACLIntegrationBlocked() {
 
-        $cbackend = TestUtil::getBackend();
-
-        $props = [
-            'uri'          => 'UUID-123467',
-            'principaluri' => 'admin',
-            'id'           => [1,1],
-        ];
-        $tree = [
-            new Calendar($cbackend, $props),
-        ];
-
-        $p = new ICSExportPlugin();
-
-        $s = new DAV\Server($tree);
-        $s->addPlugin($p);
-        $s->addPlugin(new Plugin());
-        $s->addPlugin(new DAVACL\Plugin());
+        $this->server->addPlugin(
+            new DAVACL\Plugin()
+        );
 
-        $h = HTTP\Sapi::createFromServerArray([
-            'REQUEST_URI' => '/UUID-123467?export',
-        ]);
+        $request = new HTTP\Request(
+            'GET',
+            '/calendars/admin/UUID-123467?export'
+        );
 
-        $s->httpRequest = $h;
-        $s->httpResponse = new HTTP\ResponseMock();
-
-        $p->httpGet($h, $s->httpResponse);
-
-        // If the ACL system blocked this request, the effect will be that
-        // there's no response, because the calendar information could not be
-        // fetched.
-        $this->assertNull($s->httpResponse->getStatus());
+        $this->request($request, 403);
 
     }
 
     function testACLIntegrationNotBlocked() {
 
-        $cbackend = TestUtil::getBackend();
-        $pbackend = new DAVACL\PrincipalBackend\Mock();
-
-        $props = [
-            'uri'          => 'UUID-123467',
-            'principaluri' => 'admin',
-            'id'           => [1,1],
-        ];
-        $tree = [
-            new Calendar($cbackend, $props),
-            new DAVACL\PrincipalCollection($pbackend),
-        ];
-
-        $p = new ICSExportPlugin();
-
-        $s = new DAV\Server($tree);
-        $s->sapi = new HTTP\SapiMock();
-        $s->addPlugin($p);
-        $s->addPlugin(new Plugin());
-        $s->addPlugin(new DAVACL\Plugin());
-        $s->addPlugin(new DAV\Auth\Plugin(new DAV\Auth\Backend\Mock(), 'SabreDAV'));
-
-        // Forcing login
-        $s->getPlugin('acl')->adminPrincipals = ['principals/admin'];
+        $this->server->addPlugin(
+            new DAVACL\Plugin()
+        );
 
+        $this->autoLogin('admin');
 
-        $h = HTTP\Sapi::createFromServerArray([
-            'REQUEST_URI'    => '/UUID-123467?export',
-            'REQUEST_METHOD' => 'GET',
-        ]);
+        $request = new HTTP\Request(
+            'GET',
+            '/calendars/admin/UUID-123467?export'
+        );
 
-        $s->httpRequest = $h;
-        $s->httpResponse = new HTTP\ResponseMock();
+        $response = $this->request($request, 200);
+        $this->assertEquals('text/calendar', $response->getHeader('Content-Type'));
 
-        $s->exec();
+        $obj = VObject\Reader::read($response->body);
 
-        $this->assertEquals(200, $s->httpResponse->status, 'Invalid status received. Response body: ' . $s->httpResponse->body);
-        $this->assertEquals([
-            'X-Sabre-Version' => [DAV\Version::VERSION],
-            'Content-Type'    => ['text/calendar'],
-        ], $s->httpResponse->getHeaders());
-
-        $obj = VObject\Reader::read($s->httpResponse->body);
-
-        $this->assertEquals(5, count($obj->children()));
+        $this->assertEquals(8, count($obj->children()));
         $this->assertEquals(1, count($obj->VERSION));
         $this->assertEquals(1, count($obj->CALSCALE));
         $this->assertEquals(1, count($obj->PRODID));
+        $this->assertTrue(strpos((string)$obj->PRODID, DAV\Version::VERSION) !== false);
         $this->assertEquals(1, count($obj->VTIMEZONE));
         $this->assertEquals(1, count($obj->VEVENT));
 
@@ -234,110 +177,33 @@ class ICSExportPluginTest extends \PHPUnit_Framework_TestCase {
 
     function testBadStartParam() {
 
-        $cbackend = TestUtil::getBackend();
-        $pbackend = new DAVACL\PrincipalBackend\Mock();
-
-        $props = [
-            'uri'          => 'UUID-123467',
-            'principaluri' => 'admin',
-            'id'           => 1,
-        ];
-        $tree = [
-            new Calendar($cbackend, $props),
-            new DAVACL\PrincipalCollection($pbackend),
-        ];
-
-        $p = new ICSExportPlugin();
-
-        $s = new DAV\Server($tree);
-        $s->sapi = new HTTP\SapiMock();
-        $s->addPlugin($p);
-        $s->addPlugin(new Plugin());
-
-        $h = HTTP\Sapi::createFromServerArray([
-            'REQUEST_URI'    => '/UUID-123467?export&start=foo',
-            'REQUEST_METHOD' => 'GET',
-        ]);
-
-        $s->httpRequest = $h;
-        $s->httpResponse = new HTTP\ResponseMock();
-
-        $s->exec();
-
-        $this->assertEquals(400, $s->httpResponse->status, 'Invalid status received. Response body: ' . $s->httpResponse->body);
+        $request = new HTTP\Request(
+            'GET',
+            '/calendars/admin/UUID-123467?export&start=foo'
+        );
+        $this->request($request, 400);
 
     }
 
     function testBadEndParam() {
 
-        $cbackend = TestUtil::getBackend();
-        $pbackend = new DAVACL\PrincipalBackend\Mock();
-
-        $props = [
-            'uri'          => 'UUID-123467',
-            'principaluri' => 'admin',
-            'id'           => [1,1],
-        ];
-        $tree = [
-            new Calendar($cbackend, $props),
-            new DAVACL\PrincipalCollection($pbackend),
-        ];
-
-        $p = new ICSExportPlugin();
-
-        $s = new DAV\Server($tree);
-        $s->sapi = new HTTP\SapiMock();
-        $s->addPlugin($p);
-        $s->addPlugin(new Plugin());
-
-        $h = HTTP\Sapi::createFromServerArray([
-            'REQUEST_URI'    => '/UUID-123467?export&end=foo',
-            'REQUEST_METHOD' => 'GET',
-        ]);
-
-        $s->httpRequest = $h;
-        $s->httpResponse = new HTTP\ResponseMock();
-
-        $s->exec();
-
-        $this->assertEquals(400, $s->httpResponse->status, 'Invalid status received. Response body: ' . $s->httpResponse->body);
+        $request = new HTTP\Request(
+            'GET',
+            '/calendars/admin/UUID-123467?export&end=foo'
+        );
+        $this->request($request, 400);
 
     }
 
     function testFilterStartEnd() {
 
-        $cbackend = TestUtil::getBackend();
-        $pbackend = new DAVACL\PrincipalBackend\Mock();
-
-        $props = [
-            'uri'          => 'UUID-123467',
-            'principaluri' => 'admin',
-            'id'           => [1,1],
-        ];
-        $tree = [
-            new Calendar($cbackend, $props),
-            new DAVACL\PrincipalCollection($pbackend),
-        ];
-
-        $p = new ICSExportPlugin();
+        $request = new HTTP\Request(
+            'GET',
+            '/calendars/admin/UUID-123467?export&start=1&end=2'
+        );
+        $response = $this->request($request, 200);
 
-        $s = new DAV\Server($tree);
-        $s->sapi = new HTTP\SapiMock();
-        $s->addPlugin($p);
-        $s->addPlugin(new Plugin());
-
-        $h = HTTP\Sapi::createFromServerArray([
-            'REQUEST_URI'    => '/UUID-123467?export&start=1&end=2',
-            'REQUEST_METHOD' => 'GET',
-        ]);
-
-        $s->httpRequest = $h;
-        $s->httpResponse = new HTTP\ResponseMock();
-
-        $s->exec();
-
-        $this->assertEquals(200, $s->httpResponse->status, 'Invalid status received. Response body: ' . $s->httpResponse->body);
-        $obj = VObject\Reader::read($s->httpResponse->body);
+        $obj = VObject\Reader::read($response->getBody());
 
         $this->assertEquals(0, count($obj->VTIMEZONE));
         $this->assertEquals(0, count($obj->VEVENT));
@@ -346,74 +212,23 @@ class ICSExportPluginTest extends \PHPUnit_Framework_TestCase {
 
     function testExpandNoStart() {
 
-        $cbackend = TestUtil::getBackend();
-        $pbackend = new DAVACL\PrincipalBackend\Mock();
-
-        $props = [
-            'uri'          => 'UUID-123467',
-            'principaluri' => 'admin',
-            'id'           => [1,1],
-        ];
-        $tree = [
-            new Calendar($cbackend, $props),
-            new DAVACL\PrincipalCollection($pbackend),
-        ];
-
-        $p = new ICSExportPlugin();
-
-        $s = new DAV\Server($tree);
-        $s->sapi = new HTTP\SapiMock();
-        $s->addPlugin($p);
-        $s->addPlugin(new Plugin());
-
-        $h = HTTP\Sapi::createFromServerArray([
-            'REQUEST_URI'    => '/UUID-123467?export&expand=1&end=1',
-            'REQUEST_METHOD' => 'GET',
-        ]);
-
-        $s->httpRequest = $h;
-        $s->httpResponse = new HTTP\ResponseMock();
-
-        $s->exec();
-
-        $this->assertEquals(400, $s->httpResponse->status, 'Invalid status received. Response body: ' . $s->httpResponse->body);
+        $request = new HTTP\Request(
+            'GET',
+            '/calendars/admin/UUID-123467?export&expand=1&end=2'
+        );
+        $this->request($request, 400);
 
     }
 
     function testExpand() {
 
-        $cbackend = TestUtil::getBackend();
-        $pbackend = new DAVACL\PrincipalBackend\Mock();
-
-        $props = [
-            'uri'          => 'UUID-123467',
-            'principaluri' => 'admin',
-            'id'           => [1,1],
-        ];
-        $tree = [
-            new Calendar($cbackend, $props),
-            new DAVACL\PrincipalCollection($pbackend),
-        ];
-
-        $p = new ICSExportPlugin();
+        $request = new HTTP\Request(
+            'GET',
+            '/calendars/admin/UUID-123467?export&start=1&end=2000000000&expand=1'
+        );
+        $response = $this->request($request, 200);
 
-        $s = new DAV\Server($tree);
-        $s->sapi = new HTTP\SapiMock();
-        $s->addPlugin($p);
-        $s->addPlugin(new Plugin());
-
-        $h = HTTP\Sapi::createFromServerArray([
-            'REQUEST_URI'    => '/UUID-123467?export&start=1&end=2000000000&expand=1',
-            'REQUEST_METHOD' => 'GET',
-        ]);
-
-        $s->httpRequest = $h;
-        $s->httpResponse = new HTTP\ResponseMock();
-
-        $s->exec();
-
-        $this->assertEquals(200, $s->httpResponse->status, 'Invalid status received. Response body: ' . $s->httpResponse->body);
-        $obj = VObject\Reader::read($s->httpResponse->body);
+        $obj = VObject\Reader::read($response->getBody());
 
         $this->assertEquals(0, count($obj->VTIMEZONE));
         $this->assertEquals(1, count($obj->VEVENT));
@@ -422,155 +237,52 @@ class ICSExportPluginTest extends \PHPUnit_Framework_TestCase {
 
     function testJCal() {
 
-        $cbackend = TestUtil::getBackend();
-        $pbackend = new DAVACL\PrincipalBackend\Mock();
-
-        $props = [
-            'uri'          => 'UUID-123467',
-            'principaluri' => 'admin',
-            'id'           => [1,1],
-        ];
-        $tree = [
-            new Calendar($cbackend, $props),
-            new DAVACL\PrincipalCollection($pbackend),
-        ];
-
-        $p = new ICSExportPlugin();
+        $request = new HTTP\Request(
+            'GET',
+            '/calendars/admin/UUID-123467?export',
+            ['Accept' => 'application/calendar+json']
+        );
 
-        $s = new DAV\Server($tree);
-        $s->sapi = new HTTP\SapiMock();
-        $s->addPlugin($p);
-        $s->addPlugin(new Plugin());
-
-        $h = HTTP\Sapi::createFromServerArray([
-            'REQUEST_URI'    => '/UUID-123467?export',
-            'REQUEST_METHOD' => 'GET',
-            'HTTP_ACCEPT'    => 'application/calendar+json',
-        ]);
-
-        $s->httpRequest = $h;
-        $s->httpResponse = new HTTP\ResponseMock();
-
-        $s->exec();
-
-        $this->assertEquals(200, $s->httpResponse->status, 'Invalid status received. Response body: ' . $s->httpResponse->body);
-        $this->assertEquals('application/calendar+json', $s->httpResponse->getHeader('Content-Type'));
+        $response = $this->request($request, 200);
+        $this->assertEquals('application/calendar+json', $response->getHeader('Content-Type'));
 
     }
 
     function testJCalInUrl() {
 
-        $cbackend = TestUtil::getBackend();
-        $pbackend = new DAVACL\PrincipalBackend\Mock();
-
-        $props = [
-            'uri'          => 'UUID-123467',
-            'principaluri' => 'admin',
-            'id'           => [1,1],
-        ];
-        $tree = [
-            new Calendar($cbackend, $props),
-            new DAVACL\PrincipalCollection($pbackend),
-        ];
-
-        $p = new ICSExportPlugin();
+        $request = new HTTP\Request(
+            'GET',
+            '/calendars/admin/UUID-123467?export&accept=jcal'
+        );
 
-        $s = new DAV\Server($tree);
-        $s->sapi = new HTTP\SapiMock();
-        $s->addPlugin($p);
-        $s->addPlugin(new Plugin());
-
-        $h = HTTP\Sapi::createFromServerArray([
-            'REQUEST_URI'    => '/UUID-123467?export&accept=jcal',
-            'REQUEST_METHOD' => 'GET',
-        ]);
-
-        $s->httpRequest = $h;
-        $s->httpResponse = new HTTP\ResponseMock();
-
-        $s->exec();
-
-        $this->assertEquals(200, $s->httpResponse->status, 'Invalid status received. Response body: ' . $s->httpResponse->body);
-        $this->assertEquals('application/calendar+json', $s->httpResponse->getHeader('Content-Type'));
+        $response = $this->request($request, 200);
+        $this->assertEquals('application/calendar+json', $response->getHeader('Content-Type'));
 
     }
 
     function testNegotiateDefault() {
 
-        $cbackend = TestUtil::getBackend();
-        $pbackend = new DAVACL\PrincipalBackend\Mock();
-
-        $props = [
-            'uri'          => 'UUID-123467',
-            'principaluri' => 'admin',
-            'id'           => [1,1],
-        ];
-        $tree = [
-            new Calendar($cbackend, $props),
-            new DAVACL\PrincipalCollection($pbackend),
-        ];
-
-        $p = new ICSExportPlugin();
+        $request = new HTTP\Request(
+            'GET',
+            '/calendars/admin/UUID-123467?export',
+            ['Accept' => 'text/plain']
+        );
 
-        $s = new DAV\Server($tree);
-        $s->sapi = new HTTP\SapiMock();
-        $s->addPlugin($p);
-        $s->addPlugin(new Plugin());
-
-        $h = HTTP\Sapi::createFromServerArray([
-            'REQUEST_URI'    => '/UUID-123467?export',
-            'REQUEST_METHOD' => 'GET',
-            'HTTP_ACCEPT'    => 'text/plain',
-        ]);
-
-        $s->httpRequest = $h;
-        $s->httpResponse = new HTTP\ResponseMock();
-
-        $s->exec();
-
-        $this->assertEquals(200, $s->httpResponse->status, 'Invalid status received. Response body: ' . $s->httpResponse->body);
-        $this->assertEquals('text/calendar', $s->httpResponse->getHeader('Content-Type'));
+        $response = $this->request($request, 200);
+        $this->assertEquals('text/calendar', $response->getHeader('Content-Type'));
 
     }
 
     function testFilterComponentVEVENT() {
 
-        $cbackend = TestUtil::getBackend();
-        $pbackend = new DAVACL\PrincipalBackend\Mock();
-
-        $props = [
-            'uri'          => 'UUID-123467',
-            'principaluri' => 'admin',
-            'id'           => [1,1],
-        ];
-        // add a todo to the calendar (see /tests/Sabre/TestUtil)
-        $cbackend->createCalendarObject([1, 1], 'UUID-3456', TestUtil::getTestTODO());
-
-        $tree = [
-            new Calendar($cbackend, $props),
-            new DAVACL\PrincipalCollection($pbackend),
-        ];
+        $request = new HTTP\Request(
+            'GET',
+            '/calendars/admin/UUID-123467?export&componentType=VEVENT'
+        );
 
-        $p = new ICSExportPlugin();
-
-        $s = new DAV\Server($tree);
-        $s->sapi = new HTTP\SapiMock();
-        $s->addPlugin($p);
-        $s->addPlugin(new Plugin());
-
-        $h = HTTP\Sapi::createFromServerArray([
-            'REQUEST_URI'    => '/UUID-123467?export&componentType=VEVENT',
-            'REQUEST_METHOD' => 'GET',
-        ]);
-
-        $s->httpRequest = $h;
-        $s->httpResponse = new HTTP\ResponseMock();
-
-        $s->exec();
-
-        $this->assertEquals(200, $s->httpResponse->status, 'Invalid status received. Response body: ' . $s->httpResponse->body);
-        $obj = VObject\Reader::read($s->httpResponse->body);
+        $response = $this->request($request, 200);
 
+        $obj = VObject\Reader::read($response->body);
         $this->assertEquals(1, count($obj->VTIMEZONE));
         $this->assertEquals(1, count($obj->VEVENT));
         $this->assertEquals(0, count($obj->VTODO));
@@ -579,41 +291,14 @@ class ICSExportPluginTest extends \PHPUnit_Framework_TestCase {
 
     function testFilterComponentVTODO() {
 
-        $cbackend = TestUtil::getBackend();
-        $pbackend = new DAVACL\PrincipalBackend\Mock();
-
-        $props = [
-            'uri'          => 'UUID-123467',
-            'principaluri' => 'admin',
-            'id'           => [1,1],
-        ];
-        // add a todo to the calendar (see /tests/Sabre/TestUtil)
-        $cbackend->createCalendarObject([1, 1], 'UUID-3456', TestUtil::getTestTODO());
+        $request = new HTTP\Request(
+            'GET',
+            '/calendars/admin/UUID-123467?export&componentType=VTODO'
+        );
 
-        $tree = [
-            new Calendar($cbackend, $props),
-            new DAVACL\PrincipalCollection($pbackend),
-        ];
+        $response = $this->request($request, 200);
 
-        $p = new ICSExportPlugin();
-
-        $s = new DAV\Server($tree);
-        $s->sapi = new HTTP\SapiMock();
-        $s->addPlugin($p);
-        $s->addPlugin(new Plugin());
-
-        $h = HTTP\Sapi::createFromServerArray([
-            'REQUEST_URI'    => '/UUID-123467?export&componentType=VTODO',
-            'REQUEST_METHOD' => 'GET',
-        ]);
-
-        $s->httpRequest = $h;
-        $s->httpResponse = new HTTP\ResponseMock();
-
-        $s->exec();
-
-        $this->assertEquals(200, $s->httpResponse->status, 'Invalid status received. Response body: ' . $s->httpResponse->body);
-        $obj = VObject\Reader::read($s->httpResponse->body);
+        $obj = VObject\Reader::read($response->body);
 
         $this->assertEquals(0, count($obj->VTIMEZONE));
         $this->assertEquals(0, count($obj->VEVENT));
@@ -623,40 +308,12 @@ class ICSExportPluginTest extends \PHPUnit_Framework_TestCase {
 
     function testFilterComponentBadComponent() {
 
-        $cbackend = TestUtil::getBackend();
-        $pbackend = new DAVACL\PrincipalBackend\Mock();
-
-        $props = [
-            'uri'          => 'UUID-123467',
-            'principaluri' => 'admin',
-            'id'           => [1,1],
-        ];
-        // add a todo to the calendar (see /tests/Sabre/TestUtil)
-        $cbackend->createCalendarObject([1, 1], 'UUID-3456', TestUtil::getTestTODO());
-
-        $tree = [
-            new Calendar($cbackend, $props),
-            new DAVACL\PrincipalCollection($pbackend),
-        ];
-
-        $p = new ICSExportPlugin();
-
-        $s = new DAV\Server($tree);
-        $s->sapi = new HTTP\SapiMock();
-        $s->addPlugin($p);
-        $s->addPlugin(new Plugin());
-
-        $h = HTTP\Sapi::createFromServerArray([
-            'REQUEST_URI'    => '/UUID-123467?export&componentType=VVOODOO',
-            'REQUEST_METHOD' => 'GET',
-        ]);
-
-        $s->httpRequest = $h;
-        $s->httpResponse = new HTTP\ResponseMock();
-
-        $s->exec();
+        $request = new HTTP\Request(
+            'GET',
+            '/calendars/admin/UUID-123467?export&componentType=VVOODOO'
+        );
 
-        $this->assertEquals(400, $s->httpResponse->status, 'Invalid status received. Response body: ' . $s->httpResponse->body);
+        $response = $this->request($request, 400);
 
     }
 }
diff --git a/tests/Sabre/DAVServerTest.php b/tests/Sabre/DAVServerTest.php
index 865e77b..7f0746d 100644
--- a/tests/Sabre/DAVServerTest.php
+++ b/tests/Sabre/DAVServerTest.php
@@ -109,6 +109,12 @@ abstract class DAVServerTest extends \PHPUnit_Framework_TestCase {
 
     function setUp() {
 
+        $this->initializeEverything();
+
+    }
+
+    function initializeEverything() {
+
         $this->setUpBackends();
         $this->setUpTree();
 
@@ -158,13 +164,7 @@ abstract class DAVServerTest extends \PHPUnit_Framework_TestCase {
             $this->server->addPlugin($this->propertyStoragePlugin);
         }
         if ($this->autoLogin) {
-            $authBackend = new DAV\Auth\Backend\Mock();
-            $authBackend->setPrincipal('principals/' . $this->autoLogin);
-            $this->authPlugin = new DAV\Auth\Plugin($authBackend);
-            $this->server->addPlugin($this->authPlugin);
-
-            // This will trigger the actual login procedure
-            $this->authPlugin->beforeMethod(new Request(), new Response());
+            $this->autoLogin($this->autoLogin);
         }
 
     }
@@ -175,23 +175,49 @@ abstract class DAVServerTest extends \PHPUnit_Framework_TestCase {
      * You can either pass an instance of Sabre\HTTP\Request, or an array,
      * which will then be used as the _SERVER array.
      *
+     * If $expectedStatus is set, we'll compare it with the HTTP status of
+     * the returned response. If it doesn't match, we'll immediately fail
+     * the test.
+     *
      * @param array|\Sabre\HTTP\Request $request
+     * @param int $expectedStatus
      * @return \Sabre\HTTP\Response
      */
-    function request($request) {
+    function request($request, $expectedStatus = null) {
 
         if (is_array($request)) {
             $request = HTTP\Request::createFromServerArray($request);
         }
+        $response = new HTTP\ResponseMock();
+
         $this->server->httpRequest = $request;
-        $this->server->httpResponse = new HTTP\ResponseMock();
+        $this->server->httpResponse = $response;
         $this->server->exec();
 
+        if ($expectedStatus) {
+            $this->assertEquals($expectedStatus, $response->getStatus(), 'Incorrect HTTP status received for request');
+        }
         return $this->server->httpResponse;
 
     }
 
     /**
+     * This function takes a username and sets the server in a state where
+     * this user is logged in, and no longer requires an authentication check.
+     *
+     * @param string $userName
+     */
+    function autoLogin($userName) {
+        $authBackend = new DAV\Auth\Backend\Mock();
+        $authBackend->setPrincipal('principals/' . $userName);
+        $this->authPlugin = new DAV\Auth\Plugin($authBackend);
+        $this->server->addPlugin($this->authPlugin);
+
+        // This will trigger the actual login procedure
+        $this->authPlugin->beforeMethod(new Request(), new Response());
+    }
+
+    /**
      * Override this to provide your own Tree for your test-case.
      */
     function setUpTree() {
@@ -253,7 +279,7 @@ abstract class DAVServerTest extends \PHPUnit_Framework_TestCase {
     }
 
 
-    function assertHTTPStatus($expectedStatus, HTTP\Request $req) {
+    function assertHttpStatus($expectedStatus, HTTP\Request $req) {
 
         $resp = $this->request($req);
         $this->assertEquals((int)$expectedStatus, (int)$resp->status, 'Incorrect HTTP status received: ' . $resp->body);

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