497c2e7e41642ac8d96e9c097b680fcd932dfd38
[tine20] / tests / tine20 / Calendar / Import / CalDAV / ClientMock.php
1 <?php
2 /**
3  * Tine 2.0
4  * 
5  * @package     Calendar
6  * @subpackage  Import
7  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
8  * @author      Philipp Schüle <p.schuele@metaways.de>
9  * @copyright   Copyright (c) 2014 Metaways Infosystems GmbH (http://www.metaways.de)
10  */
11
12 /**
13  * Calendar_Import_CalDAV_ClientMock
14  * 
15  * @package     Calendar
16  * @subpackage  Import
17  */
18 class Calendar_Import_CalDAV_ClientMock extends Calendar_Import_CalDav_Client
19 {
20     /**
21      * needs to be overwritten because of the added flavor (osxical)
22      * 
23      * @var string
24      */
25     const findAllCalendarsRequest = '<?xml version="1.0"?>
26 <d:propfind xmlns:d="DAV:">
27   <d:prop>
28     <d:resourcetype/>
29     <d:acl/>
30     <d:displayname/>
31     <x:supported-calendar-component-set xmlns:x="urn:ietf:params:xml:ns:caldav"/>
32   <osxical:calendar-color xmlns:osxical="http://apple.com/ns/ical/"/></d:prop>
33 </d:propfind>
34 ';
35     
36     protected $_currentUserPrincipalResponse = array(
37         '{DAV:}current-user-principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/'
38     );
39     
40     protected $_calendarHomeSetResponse =  array (
41         '{urn:ietf:params:xml:ns:caldav}calendar-home-set' => '/calendars/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0',
42     );
43     
44     protected $_calendarICSResponse = array (
45       '/calendars/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar/' => 
46       array (
47       ),
48       '/calendars/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar/20E3E0E4-762D-42D6-A563-206161A9F1CA.ics' => 
49       array (
50       ),
51       '/calendars/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar/4971F93F-8657-412B-841A-A0FD9139CD61.ics' => 
52       array (
53       ),
54       '/calendars/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar/88F077A1-6F5B-4C6C-8D73-94C1F0127492.ics' => 
55       array (
56       ),
57     );
58     
59     protected $_serverEvents = array();
60     
61     public function __construct(array $a, $flavor)
62     {
63         parent::__construct($a, $flavor);
64         
65         $this->_serverEvents = array(
66             '/calendars/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar/20E3E0E4-762D-42D6-A563-206161A9F1CA.ics' =>
67                 array (
68                     self::calendarDataKey => 'BEGIN:VCALENDAR
69 VERSION:2.0
70 PRODID:-//Apple Inc.//iCal 5.0.3//EN
71 CALSCALE:GREGORIAN
72 BEGIN:VTIMEZONE
73 TZID:Europe/Berlin
74 BEGIN:DAYLIGHT
75 TZOFFSETFROM:+0100
76 RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
77 DTSTART:19810329T020000
78 TZNAME:CEST
79 TZOFFSETTO:+0200
80 END:DAYLIGHT
81 BEGIN:STANDARD
82 TZOFFSETFROM:+0200
83 RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
84 DTSTART:19961027T030000
85 TZNAME:CET
86 TZOFFSETTO:+0100
87 END:STANDARD
88 END:VTIMEZONE
89 BEGIN:VEVENT
90 CREATED:20140602T131852Z
91 UID:20E3E0E4-762D-42D6-A563-206161A9F1CA
92 DTEND;TZID=Europe/Berlin:20140604T171500
93 TRANSP:OPAQUE
94 SUMMARY:TEST06
95 DTSTART;TZID=Europe/Berlin:20140604T161500
96 DTSTAMP:20140602T131935Z
97 SEQUENCE:3
98 END:VEVENT
99 END:VCALENDAR',
100                     '{DAV:}getetag' => '"bcc36c611f0b60bfee64b4d42e44aa1d"',
101                 ),
102                 '/calendars/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar/4971F93F-8657-412B-841A-A0FD9139CD61.ics' =>
103                 array (
104                     self::calendarDataKey => 'BEGIN:VCALENDAR
105 VERSION:2.0
106 PRODID:-//Apple Inc.//iCal 5.0.3//EN
107 CALSCALE:GREGORIAN
108 BEGIN:VTIMEZONE
109 TZID:Europe/Berlin
110 BEGIN:DAYLIGHT
111 TZOFFSETFROM:+0100
112 RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
113 DTSTART:19810329T020000
114 TZNAME:CEST
115 TZOFFSETTO:+0200
116 END:DAYLIGHT
117 BEGIN:STANDARD
118 TZOFFSETFROM:+0200
119 RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
120 DTSTART:19961027T030000
121 TZNAME:CET
122 TZOFFSETTO:+0100
123 END:STANDARD
124 END:VTIMEZONE
125 BEGIN:VEVENT
126 CREATED:20140602T131707Z
127 UID:4971F93F-8657-412B-841A-A0FD9139CD61
128 DTEND;TZID=Europe/Berlin:20140604T153000
129 TRANSP:OPAQUE
130 SUMMARY:TEST05
131 DTSTART;TZID=Europe/Berlin:20140604T143000
132 DTSTAMP:20140602T131725Z
133 SEQUENCE:3
134 END:VEVENT
135 END:VCALENDAR',
136                     '{DAV:}getetag' => '"8b89914690ad7290fa9a2dc1da490489"',
137                 ),
138                 '/calendars/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar/88F077A1-6F5B-4C6C-8D73-94C1F0127492.ics' =>
139                 array (
140                     self::calendarDataKey => 'BEGIN:VCALENDAR
141 VERSION:2.0
142 PRODID:-//Apple Inc.//iCal 5.0.1//EN
143 CALSCALE:GREGORIAN
144 BEGIN:VTIMEZONE
145 TZID:Europe/Berlin
146 BEGIN:DAYLIGHT
147 TZOFFSETFROM:+0100
148 RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
149 DTSTART:19810329T020000
150 TZNAME:CEST
151 TZOFFSETTO:+0200
152 END:DAYLIGHT
153 BEGIN:STANDARD
154 TZOFFSETFROM:+0200
155 RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
156 DTSTART:19961027T030000
157 TZNAME:CET
158 TZOFFSETTO:+0100
159 END:STANDARD
160 END:VTIMEZONE
161 BEGIN:VEVENT
162 CREATED:20111207T143455Z
163 UID:88F077A1-6F5B-4C6C-8D73-94C1F0127492
164 DTEND;TZID=Europe/Berlin:20111207T170000
165 TRANSP:OPAQUE
166 SUMMARY:test
167 DTSTART;TZID=Europe/Berlin:20111207T160000
168 DTSTAMP:20111207T143502Z
169 SEQUENCE:2
170 END:VEVENT
171 END:VCALENDAR
172 ',
173                     '{DAV:}getetag' => '"0b3621a20e9045d8679075db57e881dd"',
174                 ),
175         );
176     }
177         
178     protected function _findAllCalendarsResponse()
179     {
180         return array (
181           '/calendars/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/' => 
182                array(
183                   '{DAV:}resourcetype' => new Sabre\DAV\Property\ResourceType(array('{DAV:}collection')),
184                   '{DAV:}acl' => new Sabre\DAVACL\Property\Acl(array(
185                     array (
186                       'principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/',
187                       'protected' => true,
188                       'privilege' => '{DAV:}all',
189                     ),
190                     array (
191                       'principal' => '{DAV:}authenticated',
192                       'protected' => false,
193                       'privilege' => '{urn:ietf:params:xml:ns:caldav}read-free-busy',
194                     ),
195                     array (
196                       'principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar-proxy-read/',
197                       'protected' => true,
198                       'privilege' => '{DAV:}read',
199                     ),
200                     array (
201                       'principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar-proxy-read/',
202                       'protected' => true,
203                       'privilege' => '{DAV:}read-current-user-privilege-set',
204                     ),
205                     array (
206                       'principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar-proxy-write/',
207                       'protected' => true,
208                       'privilege' => '{DAV:}read',
209                     ),
210                     array (
211                       'principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar-proxy-write/',
212                       'protected' => true,
213                       'privilege' => '{DAV:}read-current-user-privilege-set',
214                     ),
215                     array (
216                       'principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar-proxy-write/',
217                       'protected' => true,
218                       'privilege' => '{DAV:}write',
219                     ),
220                 )),
221                 '{DAV:}displayname' => 'User1 Test',
222                 '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set' => new
223                     Sabre\CalDAV\Property\SupportedCalendarComponentSet(array(
224                         0 => 'VEVENT',
225                         1 => 'VTODO',
226                         2 => 'VTIMEZONE',
227                         3 => 'VJOURNAL',
228                         4 => 'VFREEBUSY',
229                     )),
230                ),
231             '/calendars/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar/' =>
232             array (
233                 '{DAV:}resourcetype' =>
234                 new Sabre\DAV\Property\ResourceType(array(
235                     0 => '{DAV:}collection',
236                     1 => '{urn:ietf:params:xml:ns:caldav}calendar',
237                 )),
238                 '{DAV:}acl' =>
239                 new Sabre\DAVACL\Property\Acl(array(
240                     array (
241                         'principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/',
242                         'protected' => true,
243                         'privilege' => '{DAV:}all',
244                     ),
245                     array (
246                         'principal' => '{DAV:}authenticated',
247                         'protected' => false,
248                         'privilege' => '{urn:ietf:params:xml:ns:caldav}read-free-busy',
249                     ),
250                     array (
251                         'principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar-proxy-read/',
252                         'protected' => true,
253                         'privilege' => '{DAV:}read',
254                     ),
255                     array (
256                         'principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar-proxy-read/',
257                         'protected' => true,
258                         'privilege' => '{DAV:}read-current-user-privilege-set',
259                     ),
260                     array (
261                         'principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar-proxy-write/',
262                         'protected' => true,
263                         'privilege' => '{DAV:}read',
264                     ),
265                     array (
266                         'principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar-proxy-write/',
267                         'protected' => true,
268                         'privilege' => '{DAV:}read-current-user-privilege-set',
269                     ),
270                     array (
271                         'principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar-proxy-write/',
272                         'protected' => true,
273                         'privilege' => '{DAV:}write',
274                     ),
275                 )),
276                 '{DAV:}displayname' => 'calendar',
277                 '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set' =>
278                 new Sabre\CalDAV\Property\SupportedCalendarComponentSet(array(
279                     0 => 'VEVENT',
280                     1 => 'VTODO',
281                     2 => 'VTIMEZONE',
282                     3 => 'VJOURNAL',
283                     4 => 'VFREEBUSY',
284                 )),
285                 '{http://apple.com/ns/ical/}calendar-color' => '#711A76FF',
286             ),
287             );
288     }
289     
290     protected function _resolvePrincipalResponse()
291     {
292         return array (
293           '{DAV:}group-member-set' => 
294           new Tinebase_Import_CalDav_GroupMemberSet(array(
295           )),
296           '{DAV:}displayname' => 'calendar-proxy-write',
297         );
298     }
299     
300     /**
301      * calendar data response
302      * 
303      * @return string
304      */
305     protected function _calendarDataResponse()
306     {
307         return $this->_serverEvents;
308     }
309     
310     protected function _calendarEtagResponse()
311     {
312         $etags = array();
313         foreach($this->_serverEvents as $ics => $data) {
314             $etags[$ics] = array(
315                 '{DAV:}getetag' => $data['{DAV:}getetag']
316             );
317         }
318         return $etags;
319     }
320     
321     /**
322      * update 1 event, delete one event, add one event
323      */
324     public function updateServerEvents()
325     {
326         // update
327         $ics = '/calendars/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar/88F077A1-6F5B-4C6C-8D73-94C1F0127492.ics';
328         
329         $this->_serverEvents[$ics]['{DAV:}getetag'] =
330             '"aa3621a20e9045d8679075db57e881dd"';
331         
332         $this->_serverEvents[$ics][self::calendarDataKey] = str_replace(array(
333             'SEQUENCE:2',
334            'SUMMARY:test'
335         ), array(
336             'SEQUENCE:3',
337             'SUMMARY:test update'
338         ), $this->_serverEvents[$ics][self::calendarDataKey]);
339         
340         // delete
341         unset($this->_serverEvents['/calendars/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar/4971F93F-8657-412B-841A-A0FD9139CD61.ics']);
342         
343         // add
344         $this->_serverEvents['/calendars/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar/3331F93F-8657-412B-841A-A0FD9139CD61.ics'] =
345         array (
346             self::calendarDataKey => 'BEGIN:VCALENDAR
347 VERSION:2.0
348 PRODID:-//Apple Inc.//iCal 5.0.3//EN
349 CALSCALE:GREGORIAN
350 BEGIN:VTIMEZONE
351 TZID:Europe/Berlin
352 BEGIN:DAYLIGHT
353 TZOFFSETFROM:+0100
354 RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
355 DTSTART:19810329T020000
356 TZNAME:CEST
357 TZOFFSETTO:+0200
358 END:DAYLIGHT
359 BEGIN:STANDARD
360 TZOFFSETFROM:+0200
361 RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
362 DTSTART:19961027T030000
363 TZNAME:CET
364 TZOFFSETTO:+0100
365 END:STANDARD
366 END:VTIMEZONE
367 BEGIN:VEVENT
368 CREATED:20140602T131707Z
369 UID:3331F93F-8657-412B-841A-A0FD9139CD61
370 DTEND;TZID=Europe/Berlin:20140804T153000
371 TRANSP:OPAQUE
372 SUMMARY:new event
373 DTSTART;TZID=Europe/Berlin:20140804T143000
374 DTSTAMP:20140602T131725Z
375 SEQUENCE:1
376 END:VEVENT
377 END:VCALENDAR',
378             '{DAV:}getetag' => '"3333914690ad7290fa9a2dc1da490489"',
379         );
380     }
381     
382     /**
383      * perform mocked calDavRequest
384      *
385      * @param string $method
386      * @param string $uri
387      * @param strubg $body
388      * @param number $depth
389      * @param number $tries
390      * @param number $sleep
391      * @throws Tinebase_Exception
392      */
393     public function calDavRequest($method, $uri, $body, $depth = 0, $tries = 10, $sleep = 30)
394     {
395         if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__
396                  . ' Sending ' . $method . ' request for uri ' . $uri . ': ' . $body);
397         
398         if ($body == self::findCurrentUserPrincipalRequest) {
399             return $this->_currentUserPrincipalResponse;
400             
401         } else if ($body == self::findCalendarHomeSetRequest) {
402             return $this->_calendarHomeSetResponse;
403             
404         } else if ($body == self::findAllCalendarsRequest) {
405             return $this->_findAllCalendarsResponse();
406             
407         } else if ($body == self::resolvePrincipalRequest) {
408             return $this->_resolvePrincipalResponse();
409             
410         } else if ($body == self::findAllCalendarICSsRequest) {
411             return $this->_calendarICSResponse;
412             
413         } else if (preg_match('/<b:calendar-data \/>/', $body)) {
414             return $this->_calendarDataResponse();
415             
416         } else if (preg_match('/<a:getetag \/>/', $body)) {
417             return $this->_calendarEtagResponse();
418             
419         } else {
420             throw new Tinebase_Exception_InvalidArgument('request not supported by mock');
421         }
422     }
423 }