adds update events test to caldav client test suite
authorPhilipp Schüle <p.schuele@metaways.de>
Wed, 27 Aug 2014 14:45:33 +0000 (16:45 +0200)
committerPhilipp Schüle <p.schuele@metaways.de>
Thu, 4 Sep 2014 09:26:46 +0000 (11:26 +0200)
* allows to "update" events on mocked caldav server
* adds calendarDataKey const
* some testing code cleanup

Change-Id: I1e94fdcffded533ad4b5309d89d1e57d35171624
Reviewed-on: http://gerrit.tine20.com/customers/1050
Tested-by: Jenkins CI (http://ci.tine20.com/)
Reviewed-by: Philipp Schüle <p.schuele@metaways.de>
tests/tine20/Calendar/Import/CalDAV/ClientMock.php
tests/tine20/Calendar/Import/CalDAVTest.php
tine20/Calendar/Import/CalDav/Client.php

index 600135f..497c2e7 100644 (file)
  */
 class Calendar_Import_CalDAV_ClientMock extends Calendar_Import_CalDav_Client
 {
+    /**
+     * needs to be overwritten because of the added flavor (osxical)
+     * 
+     * @var string
+     */
+    const findAllCalendarsRequest = '<?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>
+';
     
     protected $_currentUserPrincipalResponse = array(
         '{DAV:}current-user-principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/'
@@ -39,22 +54,127 @@ class Calendar_Import_CalDAV_ClientMock extends Calendar_Import_CalDav_Client
       '/calendars/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar/88F077A1-6F5B-4C6C-8D73-94C1F0127492.ics' => 
       array (
       ),
-      /*
-      '/calendars/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar/c6464fb6-6886-8861-11da-5240d50d2df9.ics' => 
-      array (
-      ),
-      '/calendars/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar/c6464fd0-016f-16f1-2bdd-9d79275458a3.ics' => 
-      array (
-      ),
-      '/calendars/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar/D02B2082-ED55-444F-886F-5509AFF4992F.ics' => 
-      array (
-      ),
-      '/calendars/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar/FDEB95A9-289E-48DD-98E7-661E188D1D05.ics' => 
-      array (
-      ),
-      */
     );
     
+    protected $_serverEvents = array();
+    
+    public function __construct(array $a, $flavor)
+    {
+        parent::__construct($a, $flavor);
+        
+        $this->_serverEvents = array(
+            '/calendars/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar/20E3E0E4-762D-42D6-A563-206161A9F1CA.ics' =>
+                array (
+                    self::calendarDataKey => 'BEGIN:VCALENDAR
+VERSION:2.0
+PRODID:-//Apple Inc.//iCal 5.0.3//EN
+CALSCALE:GREGORIAN
+BEGIN:VTIMEZONE
+TZID:Europe/Berlin
+BEGIN:DAYLIGHT
+TZOFFSETFROM:+0100
+RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
+DTSTART:19810329T020000
+TZNAME:CEST
+TZOFFSETTO:+0200
+END:DAYLIGHT
+BEGIN:STANDARD
+TZOFFSETFROM:+0200
+RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
+DTSTART:19961027T030000
+TZNAME:CET
+TZOFFSETTO:+0100
+END:STANDARD
+END:VTIMEZONE
+BEGIN:VEVENT
+CREATED:20140602T131852Z
+UID:20E3E0E4-762D-42D6-A563-206161A9F1CA
+DTEND;TZID=Europe/Berlin:20140604T171500
+TRANSP:OPAQUE
+SUMMARY:TEST06
+DTSTART;TZID=Europe/Berlin:20140604T161500
+DTSTAMP:20140602T131935Z
+SEQUENCE:3
+END:VEVENT
+END:VCALENDAR',
+                    '{DAV:}getetag' => '"bcc36c611f0b60bfee64b4d42e44aa1d"',
+                ),
+                '/calendars/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar/4971F93F-8657-412B-841A-A0FD9139CD61.ics' =>
+                array (
+                    self::calendarDataKey => 'BEGIN:VCALENDAR
+VERSION:2.0
+PRODID:-//Apple Inc.//iCal 5.0.3//EN
+CALSCALE:GREGORIAN
+BEGIN:VTIMEZONE
+TZID:Europe/Berlin
+BEGIN:DAYLIGHT
+TZOFFSETFROM:+0100
+RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
+DTSTART:19810329T020000
+TZNAME:CEST
+TZOFFSETTO:+0200
+END:DAYLIGHT
+BEGIN:STANDARD
+TZOFFSETFROM:+0200
+RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
+DTSTART:19961027T030000
+TZNAME:CET
+TZOFFSETTO:+0100
+END:STANDARD
+END:VTIMEZONE
+BEGIN:VEVENT
+CREATED:20140602T131707Z
+UID:4971F93F-8657-412B-841A-A0FD9139CD61
+DTEND;TZID=Europe/Berlin:20140604T153000
+TRANSP:OPAQUE
+SUMMARY:TEST05
+DTSTART;TZID=Europe/Berlin:20140604T143000
+DTSTAMP:20140602T131725Z
+SEQUENCE:3
+END:VEVENT
+END:VCALENDAR',
+                    '{DAV:}getetag' => '"8b89914690ad7290fa9a2dc1da490489"',
+                ),
+                '/calendars/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar/88F077A1-6F5B-4C6C-8D73-94C1F0127492.ics' =>
+                array (
+                    self::calendarDataKey => 'BEGIN:VCALENDAR
+VERSION:2.0
+PRODID:-//Apple Inc.//iCal 5.0.1//EN
+CALSCALE:GREGORIAN
+BEGIN:VTIMEZONE
+TZID:Europe/Berlin
+BEGIN:DAYLIGHT
+TZOFFSETFROM:+0100
+RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
+DTSTART:19810329T020000
+TZNAME:CEST
+TZOFFSETTO:+0200
+END:DAYLIGHT
+BEGIN:STANDARD
+TZOFFSETFROM:+0200
+RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
+DTSTART:19961027T030000
+TZNAME:CET
+TZOFFSETTO:+0100
+END:STANDARD
+END:VTIMEZONE
+BEGIN:VEVENT
+CREATED:20111207T143455Z
+UID:88F077A1-6F5B-4C6C-8D73-94C1F0127492
+DTEND;TZID=Europe/Berlin:20111207T170000
+TRANSP:OPAQUE
+SUMMARY:test
+DTSTART;TZID=Europe/Berlin:20111207T160000
+DTSTAMP:20111207T143502Z
+SEQUENCE:2
+END:VEVENT
+END:VCALENDAR
+',
+                    '{DAV:}getetag' => '"0b3621a20e9045d8679075db57e881dd"',
+                ),
+        );
+    }
+        
     protected function _findAllCalendarsResponse()
     {
         return array (
@@ -164,77 +284,6 @@ class Calendar_Import_CalDAV_ClientMock extends Calendar_Import_CalDav_Client
                 )),
                 '{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',
-                            ),
-                        )),
-                        ),
-                        */
             );
     }
     
@@ -252,51 +301,49 @@ class Calendar_Import_CalDAV_ClientMock extends Calendar_Import_CalDav_Client
      * calendar data response
      * 
      * @return string
-     * 
-     * @todo make this dynamic (only return requested cal data) 
      */
     protected function _calendarDataResponse()
     {
-        return array (
-  '/calendars/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar/20E3E0E4-762D-42D6-A563-206161A9F1CA.ics' => 
-  array (
-    '{urn:ietf:params:xml:ns:caldav}calendar-data' => 'BEGIN:VCALENDAR
-VERSION:2.0
-PRODID:-//Apple Inc.//iCal 5.0.3//EN
-CALSCALE:GREGORIAN
-BEGIN:VTIMEZONE
-TZID:Europe/Berlin
-BEGIN:DAYLIGHT
-TZOFFSETFROM:+0100
-RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
-DTSTART:19810329T020000
-TZNAME:CEST
-TZOFFSETTO:+0200
-END:DAYLIGHT
-BEGIN:STANDARD
-TZOFFSETFROM:+0200
-RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
-DTSTART:19961027T030000
-TZNAME:CET
-TZOFFSETTO:+0100
-END:STANDARD
-END:VTIMEZONE
-BEGIN:VEVENT
-CREATED:20140602T131852Z
-UID:20E3E0E4-762D-42D6-A563-206161A9F1CA
-DTEND;TZID=Europe/Berlin:20140604T171500
-TRANSP:OPAQUE
-SUMMARY:TEST06
-DTSTART;TZID=Europe/Berlin:20140604T161500
-DTSTAMP:20140602T131935Z
-SEQUENCE:3
-END:VEVENT
-END:VCALENDAR',
-      '{DAV:}getetag' => '"bcc36c611f0b60bfee64b4d42e44aa1d"',
-      ),
-      '/calendars/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar/4971F93F-8657-412B-841A-A0FD9139CD61.ics' =>
-      array (
-          '{urn:ietf:params:xml:ns:caldav}calendar-data' => 'BEGIN:VCALENDAR
+        return $this->_serverEvents;
+    }
+    
+    protected function _calendarEtagResponse()
+    {
+        $etags = array();
+        foreach($this->_serverEvents as $ics => $data) {
+            $etags[$ics] = array(
+                '{DAV:}getetag' => $data['{DAV:}getetag']
+            );
+        }
+        return $etags;
+    }
+    
+    /**
+     * update 1 event, delete one event, add one event
+     */
+    public function updateServerEvents()
+    {
+        // update
+        $ics = '/calendars/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar/88F077A1-6F5B-4C6C-8D73-94C1F0127492.ics';
+        
+        $this->_serverEvents[$ics]['{DAV:}getetag'] =
+            '"aa3621a20e9045d8679075db57e881dd"';
+        
+        $this->_serverEvents[$ics][self::calendarDataKey] = str_replace(array(
+            'SEQUENCE:2',
+           'SUMMARY:test'
+        ), array(
+            'SEQUENCE:3',
+            'SUMMARY:test update'
+        ), $this->_serverEvents[$ics][self::calendarDataKey]);
+        
+        // delete
+        unset($this->_serverEvents['/calendars/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar/4971F93F-8657-412B-841A-A0FD9139CD61.ics']);
+        
+        // add
+        $this->_serverEvents['/calendars/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar/3331F93F-8657-412B-841A-A0FD9139CD61.ics'] =
+        array (
+            self::calendarDataKey => 'BEGIN:VCALENDAR
 VERSION:2.0
 PRODID:-//Apple Inc.//iCal 5.0.3//EN
 CALSCALE:GREGORIAN
@@ -319,54 +366,16 @@ END:STANDARD
 END:VTIMEZONE
 BEGIN:VEVENT
 CREATED:20140602T131707Z
-UID:4971F93F-8657-412B-841A-A0FD9139CD61
-DTEND;TZID=Europe/Berlin:20140604T153000
+UID:3331F93F-8657-412B-841A-A0FD9139CD61
+DTEND;TZID=Europe/Berlin:20140804T153000
 TRANSP:OPAQUE
-SUMMARY:TEST05
-DTSTART;TZID=Europe/Berlin:20140604T143000
+SUMMARY:new event
+DTSTART;TZID=Europe/Berlin:20140804T143000
 DTSTAMP:20140602T131725Z
-SEQUENCE:3
+SEQUENCE:1
 END:VEVENT
 END:VCALENDAR',
-    '{DAV:}getetag' => '"8b89914690ad7290fa9a2dc1da490489"',
-  ),
-  '/calendars/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar/88F077A1-6F5B-4C6C-8D73-94C1F0127492.ics' => 
-  array (
-    '{urn:ietf:params:xml:ns:caldav}calendar-data' => 'BEGIN:VCALENDAR
-VERSION:2.0
-PRODID:-//Apple Inc.//iCal 5.0.1//EN
-CALSCALE:GREGORIAN
-BEGIN:VTIMEZONE
-TZID:Europe/Berlin
-BEGIN:DAYLIGHT
-TZOFFSETFROM:+0100
-RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
-DTSTART:19810329T020000
-TZNAME:CEST
-TZOFFSETTO:+0200
-END:DAYLIGHT
-BEGIN:STANDARD
-TZOFFSETFROM:+0200
-RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
-DTSTART:19961027T030000
-TZNAME:CET
-TZOFFSETTO:+0100
-END:STANDARD
-END:VTIMEZONE
-BEGIN:VEVENT
-CREATED:20111207T143455Z
-UID:88F077A1-6F5B-4C6C-8D73-94C1F0127492
-DTEND;TZID=Europe/Berlin:20111207T170000
-TRANSP:OPAQUE
-SUMMARY:test
-DTSTART;TZID=Europe/Berlin:20111207T160000
-DTSTAMP:20111207T143502Z
-SEQUENCE:2
-END:VEVENT
-END:VCALENDAR
-',
-    '{DAV:}getetag' => '"0b3621a20e9045d8679075db57e881dd"',
-  ),
+            '{DAV:}getetag' => '"3333914690ad7290fa9a2dc1da490489"',
         );
     }
     
@@ -392,16 +401,7 @@ END:VCALENDAR
         } 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 */) {
+        } else if ($body == self::findAllCalendarsRequest) {
             return $this->_findAllCalendarsResponse();
             
         } else if ($body == self::resolvePrincipalRequest) {
@@ -413,6 +413,9 @@ END:VCALENDAR
         } else if (preg_match('/<b:calendar-data \/>/', $body)) {
             return $this->_calendarDataResponse();
             
+        } else if (preg_match('/<a:getetag \/>/', $body)) {
+            return $this->_calendarEtagResponse();
+            
         } else {
             throw new Tinebase_Exception_InvalidArgument('request not supported by mock');
         }
index fd5f353..f36196e 100644 (file)
@@ -17,14 +17,14 @@ class Calendar_Import_CalDAVTest extends Calendar_TestCase
     /**
      * unit in test
      *
-     * @var Calendar_Import_CalDav_Client
+     * @var Calendar_Import_CalDAV_ClientMock
      */
     protected $_uit = null;
     
     /**
      * lazy init of uit
      *
-     * @return Calendar_Import_CalDav_Client
+     * @return Calendar_Import_CalDAV_ClientMock
      */
     protected function _getUit()
     {
@@ -51,6 +51,7 @@ class Calendar_Import_CalDAVTest extends Calendar_TestCase
         $importedCalendar = $this->_getImportCalendar();
         
         $this->assertEquals('calendar', $importedCalendar->name);
+        $this->assertEquals('#711A76', $importedCalendar->color);
         $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));
     }
@@ -88,14 +89,38 @@ class Calendar_Import_CalDAVTest extends Calendar_TestCase
     }
     
     /**
-     * @todo implement
+     * test update of events
      */
     public function testUpdateEvents()
     {
-        $this->markTestIncomplete('TODO: finish test');
         $this->testImportEvents();
-        // @todo change some events
+        
+        $importedCalendar = $this->_getImportCalendar();
+        
+        $tine20Event = $this->_getEvent();
+        $tine20Event->container_id = $importedCalendar->getId();
+        Calendar_Controller_Event::getInstance()->create($tine20Event);
+        
+        $this->_getUit()->updateServerEvents();
+        
         $this->_getUit()->updateAllCalendarData();
-        // @todo add assertions
+
+        $events = Calendar_Controller_Event::getInstance()->search(new Calendar_Model_EventFilter(array(
+            array('field' => 'container_id', 'operator' => 'in', 'value' => array($importedCalendar->getId()))
+        )));
+        $this->assertEquals(4, count($events));
+        $expectedEtags = array(
+            '"bcc36c611f0b60bfee64b4d42e44aa1d"', // unchanged
+            '"3333914690ad7290fa9a2dc1da490489"', // added on server
+            '"aa3621a20e9045d8679075db57e881dd"', // updated
+            null,                                 // added on client (tine20)
+        );
+        sort($expectedEtags);
+        $currentEtags = $events->etag;
+        sort($currentEtags);
+        $this->assertEquals($expectedEtags, $currentEtags, 'etag mismatch');
+        
+        $updatedEvent = $events->filter('etag', '"aa3621a20e9045d8679075db57e881dd"')->getFirstRecord();
+        $this->assertEquals('test update', $updatedEvent->summary);
     }
 }
index febf87d..22094a7 100644 (file)
@@ -33,6 +33,7 @@ class Calendar_Import_CalDav_Client extends Tinebase_Import_CalDav_Client
     protected $webdavFrontend = 'Calendar_Frontend_WebDAV_Event';
     protected $_uuidPrefix = '';
     
+    const calendarDataKey = '{urn:ietf:params:xml:ns:caldav}calendar-data';
     const findAllCalendarsRequest =
 '<?xml version="1.0"?>
 <d:propfind xmlns:d="DAV:">