adds test class for calendar caldav client
authorPhilipp Schüle <p.schuele@metaways.de>
Tue, 26 Aug 2014 10:44:32 +0000 (12:44 +0200)
committerPhilipp Schüle <p.schuele@metaways.de>
Thu, 4 Sep 2014 09:26:45 +0000 (11:26 +0200)
* implements mocked caldav requests (some more to add)
* implements first test: testImportCalendars

Change-Id: Iab2092c3e13bce4cbd8a546bc2d2081bdec1e835
Reviewed-on: http://gerrit.tine20.com/customers/1041
Tested-by: Jenkins CI (http://ci.tine20.com/)
Reviewed-by: Philipp Schüle <p.schuele@metaways.de>
tests/tine20/Calendar/AllTests.php
tests/tine20/Calendar/Import/CalDAV/ClientMock.php [new file with mode: 0644]
tests/tine20/Calendar/Import/CalDAVTest.php [new file with mode: 0644]

index 1646549..5880f87 100644 (file)
@@ -40,6 +40,7 @@ class Calendar_AllTests
         $suite->addTestSuite('Calendar_Controller_MSEventFacadeTest');
         $suite->addTestSuite('Calendar_JsonTests');
         $suite->addTestSuite('Calendar_Import_ICalTest');
+        $suite->addTestSuite('Calendar_Import_CalDAVTest');
         $suite->addTestSuite('Calendar_Export_ICalTest');
         $suite->addTestSuite('Calendar_Convert_Event_VCalendar_AllTests');
         $suite->addTestSuite('Calendar_Setup_DemoDataTests');
diff --git a/tests/tine20/Calendar/Import/CalDAV/ClientMock.php b/tests/tine20/Calendar/Import/CalDAV/ClientMock.php
new file mode 100644 (file)
index 0000000..a659d7c
--- /dev/null
@@ -0,0 +1,269 @@
+<?php
+/**
+ * Tine 2.0
+ * 
+ * @package     Calendar
+ * @subpackage  Import
+ * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
+ * @author      Philipp Schüle <p.schuele@metaways.de>
+ * @copyright   Copyright (c) 2014 Metaways Infosystems GmbH (http://www.metaways.de)
+ */
+
+/**
+ * Calendar_Import_CalDAV_ClientMock
+ * 
+ * @package     Calendar
+ * @subpackage  Import
+ */
+class Calendar_Import_CalDAV_ClientMock extends Calendar_Import_CalDav_Client
+{
+    
+    protected $_currentUserPrincipalResponse = array(
+        '{DAV:}current-user-principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/'
+    );
+    
+    protected $_calendarHomeSetResponse =  array (
+        '{urn:ietf:params:xml:ns:caldav}calendar-home-set' => '/calendars/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0',
+    );
+    
+    protected function _findAllCalendarsResponse()
+    {
+        return array (
+          '/calendars/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/' => 
+               array(
+                  '{DAV:}resourcetype' => new Sabre\DAV\Property\ResourceType(array('{DAV:}collection')),
+                  '{DAV:}acl' => new Sabre\DAVACL\Property\Acl(array(
+                    array (
+                      'principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/',
+                      'protected' => true,
+                      'privilege' => '{DAV:}all',
+                    ),
+                    array (
+                      'principal' => '{DAV:}authenticated',
+                      'protected' => false,
+                      'privilege' => '{urn:ietf:params:xml:ns:caldav}read-free-busy',
+                    ),
+                    array (
+                      'principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar-proxy-read/',
+                      'protected' => true,
+                      'privilege' => '{DAV:}read',
+                    ),
+                    array (
+                      'principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar-proxy-read/',
+                      'protected' => true,
+                      'privilege' => '{DAV:}read-current-user-privilege-set',
+                    ),
+                    array (
+                      'principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar-proxy-write/',
+                      'protected' => true,
+                      'privilege' => '{DAV:}read',
+                    ),
+                    array (
+                      'principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar-proxy-write/',
+                      'protected' => true,
+                      'privilege' => '{DAV:}read-current-user-privilege-set',
+                    ),
+                    array (
+                      'principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar-proxy-write/',
+                      'protected' => true,
+                      'privilege' => '{DAV:}write',
+                    ),
+                )),
+                '{DAV:}displayname' => 'User1 Test',
+                '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set' => new
+                    Sabre\CalDAV\Property\SupportedCalendarComponentSet(array(
+                        0 => 'VEVENT',
+                        1 => 'VTODO',
+                        2 => 'VTIMEZONE',
+                        3 => 'VJOURNAL',
+                        4 => 'VFREEBUSY',
+                    )),
+               ),
+            '/calendars/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar/' =>
+            array (
+                '{DAV:}resourcetype' =>
+                new Sabre\DAV\Property\ResourceType(array(
+                    0 => '{DAV:}collection',
+                    1 => '{urn:ietf:params:xml:ns:caldav}calendar',
+                )),
+                '{DAV:}acl' =>
+                new Sabre\DAVACL\Property\Acl(array(
+                    array (
+                        'principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/',
+                        'protected' => true,
+                        'privilege' => '{DAV:}all',
+                    ),
+                    array (
+                        'principal' => '{DAV:}authenticated',
+                        'protected' => false,
+                        'privilege' => '{urn:ietf:params:xml:ns:caldav}read-free-busy',
+                    ),
+                    array (
+                        'principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar-proxy-read/',
+                        'protected' => true,
+                        'privilege' => '{DAV:}read',
+                    ),
+                    array (
+                        'principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar-proxy-read/',
+                        'protected' => true,
+                        'privilege' => '{DAV:}read-current-user-privilege-set',
+                    ),
+                    array (
+                        'principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar-proxy-write/',
+                        'protected' => true,
+                        'privilege' => '{DAV:}read',
+                    ),
+                    array (
+                        'principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar-proxy-write/',
+                        'protected' => true,
+                        'privilege' => '{DAV:}read-current-user-privilege-set',
+                    ),
+                    array (
+                        'principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar-proxy-write/',
+                        'protected' => true,
+                        'privilege' => '{DAV:}write',
+                    ),
+                )),
+                '{DAV:}displayname' => 'calendar',
+                '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set' =>
+                new Sabre\CalDAV\Property\SupportedCalendarComponentSet(array(
+                    0 => 'VEVENT',
+                    1 => 'VTODO',
+                    2 => 'VTIMEZONE',
+                    3 => 'VJOURNAL',
+                    4 => 'VFREEBUSY',
+                )),
+                '{http://apple.com/ns/ical/}calendar-color' => '#711A76FF',
+            ),
+            
+            /*
+                '/calendars/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/inbox/' =>
+                array (
+                    '{DAV:}resourcetype' => new
+                    Sabre\DAV\Property\ResourceType(array(
+                         '{DAV:}collection',
+                        '{urn:ietf:params:xml:ns:caldav}schedule-inbox',
+                    )),
+                    '{DAV:}acl' => new Sabre\DAVACL\Property\Acl(
+                        array (
+                            array (
+                                'principal' => '{DAV:}authenticated',
+                                'protected' => false,
+                                'privilege' => '{urn:ietf:params:xml:ns:caldav}schedule-deliver',
+                            ),
+                            array (
+                                'principal' => '{DAV:}authenticated',
+                                'protected' => false,
+                                'privilege' => '{urn:ietf:params:xml:ns:caldav}schedule',
+                            ),
+                            array (
+                                'principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/',
+                                'protected' => true,
+                                'privilege' => '{DAV:}all',
+                            ),
+                            array (
+                                'principal' => '{DAV:}authenticated',
+                                'protected' => false,
+                                'privilege' => '{urn:ietf:params:xml:ns:caldav}read-free-busy',
+                            ),
+                            array (
+                                'principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar-proxy-read/',
+                                'protected' => true,
+                                'privilege' => '{DAV:}read',
+                            ),
+                            array (
+                                'principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar-proxy-read/',
+                                'protected' => true,
+                                'privilege' => '{DAV:}read-current-user-privilege-set',
+                            ),
+                            array (
+                                'principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar-proxy-write/',
+                                'protected' => true,
+                                'privilege' => '{DAV:}read',
+                            ),
+                            array (
+                                'principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar-proxy-write/',
+                                'protected' => true,
+                                'privilege' => '{DAV:}read-current-user-privilege-set',
+                            ),
+                            array (
+                                'principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar-proxy-write/',
+                                'protected' => true,
+                                'privilege' => '{DAV:}write',
+                            ),
+                        )),
+                        '{DAV:}displayname' => 'inbox',
+                        '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set' =>
+                        new Sabre\CalDAV\Property\SupportedCalendarComponentSet(array(
+                            'components' =>
+                            array (
+                                0 => 'VEVENT',
+                                1 => 'VTODO',
+                                2 => 'VTIMEZONE',
+                                3 => 'VJOURNAL',
+                                4 => 'VFREEBUSY',
+                            ),
+                        )),
+                        ),
+                        */
+            );
+    }
+    
+    protected function _resolvePrincipalResponse()
+    {
+        return array (
+          '{DAV:}group-member-set' => 
+          new Tinebase_Import_CalDav_GroupMemberSet(array(
+          )),
+          '{DAV:}displayname' => 'calendar-proxy-write',
+        );
+    }
+    
+    /**
+     * perform mocked calDavRequest
+     *
+     * @param string $method
+     * @param string $uri
+     * @param strubg $body
+     * @param number $depth
+     * @param number $tries
+     * @param number $sleep
+     * @throws Tinebase_Exception
+     */
+    public function calDavRequest($method, $uri, $body, $depth = 0, $tries = 10, $sleep = 30)
+    {
+        if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__
+                 . ' Sending ' . $method . ' request for uri ' . $uri . ': ' . $body);
+        
+        if ($body == self::findCurrentUserPrincipalRequest) {
+            return $this->_currentUserPrincipalResponse;
+            
+        } else if ($body == self::findCalendarHomeSetRequest) {
+            return $this->_calendarHomeSetResponse;
+            
+        } else if ($body == '<?xml version="1.0"?>
+<d:propfind xmlns:d="DAV:">
+  <d:prop>
+    <d:resourcetype/>
+    <d:acl/>
+    <d:displayname/>
+    <x:supported-calendar-component-set xmlns:x="urn:ietf:params:xml:ns:caldav"/>
+  <osxical:calendar-color xmlns:osxical="http://apple.com/ns/ical/"/></d:prop>
+</d:propfind>
+'/* self::findAllCalendarsRequest */) {
+            return $this->_findAllCalendarsResponse();
+            
+        } else if ($body == self::resolvePrincipalRequest) {
+            return $this->_resolvePrincipalResponse();
+            
+        } else if ($body == self::findAllCalendarICSsRequest) {
+            throw new Tinebase_Exception_NotImplemented('findAllCalendarICSsRequest to be implemented');
+            
+        } else if ($body == self::getAllCalendarDataRequest) {
+            throw new Tinebase_Exception_NotImplemented('getAllCalendarDataRequest to be implemented');
+            
+        } else {
+            throw new Tinebase_Exception_InvalidArgument('request not supported by mock');
+        }
+    }
+}
diff --git a/tests/tine20/Calendar/Import/CalDAVTest.php b/tests/tine20/Calendar/Import/CalDAVTest.php
new file mode 100644 (file)
index 0000000..52e3b82
--- /dev/null
@@ -0,0 +1,75 @@
+<?php
+/**
+ * Tine 2.0 - http://www.tine20.org
+ * 
+ * @package     Calendar
+ * @license     http://www.gnu.org/licenses/agpl.html
+ * @copyright   Copyright (c) 2014 Metaways Infosystems GmbH (http://www.metaways.de)
+ * @author      Philipp Schüle <p.schuele@metaways.de>
+ * 
+ */
+
+/**
+ * Test class for Calendar_Import_CalDAV
+ */
+class Calendar_Import_CalDAVTest extends Calendar_TestCase
+{
+    /**
+     * unit in test
+     *
+     * @var Calendar_Import_CalDav_Client
+     */
+    protected $_uit = null;
+    
+    /**
+     * lazy init of uit
+     *
+     * @return Calendar_Import_CalDav_Client
+     */
+    protected function _getUit()
+    {
+        if ($this->_uit === null) {
+            $caldavClientOptions = array(
+                'baseUri' => 'localhost',
+                'userName' => Tinebase_Core::getUser()->accountLoginName,
+                'password' => Zend_Registry::get('testConfig')->password, // TODO use credential cache?
+            );
+            $this->_uit = new Calendar_Import_CalDAV_ClientMock($caldavClientOptions, 'MacOSX');
+            $this->_uit->setVerifyPeer(false);
+        }
+        
+        return $this->_uit;
+    }
+    
+    /**
+     * test import of a single container/calendar of current user
+     * 
+     * @todo uuid needs to be changed as we need different uuids for tasks + events
+     */
+    public function testImportCalendars()
+    {
+        $this->_getUit()->importAllCalendars();
+        $calendarUuid = sha1('/calendars/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar/');
+        $importedCalendar = Tinebase_Container::getInstance()->getByProperty($calendarUuid, 'uuid');
+        
+        $this->assertEquals('calendar', $importedCalendar->name);
+        $this->assertEquals('Calendar_Model_Event', $importedCalendar->model, print_r($importedCalendar->toArray(), true));
+        $this->assertEquals( Tinebase_Core::getUser()->getId(), $importedCalendar->owner_id, print_r($importedCalendar->toArray(), true));
+    }
+    
+    public function testImportEvents()
+    {
+        $this->markTestIncomplete('TODO: finish test');
+        $this->_getUit()->importAllCalendarData();
+        // @todo add assertions
+    }
+    
+    public function testUpdateEvents()
+    {
+        $this->markTestIncomplete('TODO: finish test');
+        $this->testImport();
+        // @todo change some events
+        $this->_getUit()->updateAllCalendarData();
+        // @todo add assertions
+    }
+}