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