adds test class for calendar caldav client
[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 function _findAllCalendarsResponse()
30     {
31         return array (
32           '/calendars/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/' => 
33                array(
34                   '{DAV:}resourcetype' => new Sabre\DAV\Property\ResourceType(array('{DAV:}collection')),
35                   '{DAV:}acl' => new Sabre\DAVACL\Property\Acl(array(
36                     array (
37                       'principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/',
38                       'protected' => true,
39                       'privilege' => '{DAV:}all',
40                     ),
41                     array (
42                       'principal' => '{DAV:}authenticated',
43                       'protected' => false,
44                       'privilege' => '{urn:ietf:params:xml:ns:caldav}read-free-busy',
45                     ),
46                     array (
47                       'principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar-proxy-read/',
48                       'protected' => true,
49                       'privilege' => '{DAV:}read',
50                     ),
51                     array (
52                       'principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar-proxy-read/',
53                       'protected' => true,
54                       'privilege' => '{DAV:}read-current-user-privilege-set',
55                     ),
56                     array (
57                       'principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar-proxy-write/',
58                       'protected' => true,
59                       'privilege' => '{DAV:}read',
60                     ),
61                     array (
62                       'principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar-proxy-write/',
63                       'protected' => true,
64                       'privilege' => '{DAV:}read-current-user-privilege-set',
65                     ),
66                     array (
67                       'principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar-proxy-write/',
68                       'protected' => true,
69                       'privilege' => '{DAV:}write',
70                     ),
71                 )),
72                 '{DAV:}displayname' => 'User1 Test',
73                 '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set' => new
74                     Sabre\CalDAV\Property\SupportedCalendarComponentSet(array(
75                         0 => 'VEVENT',
76                         1 => 'VTODO',
77                         2 => 'VTIMEZONE',
78                         3 => 'VJOURNAL',
79                         4 => 'VFREEBUSY',
80                     )),
81                ),
82             '/calendars/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar/' =>
83             array (
84                 '{DAV:}resourcetype' =>
85                 new Sabre\DAV\Property\ResourceType(array(
86                     0 => '{DAV:}collection',
87                     1 => '{urn:ietf:params:xml:ns:caldav}calendar',
88                 )),
89                 '{DAV:}acl' =>
90                 new Sabre\DAVACL\Property\Acl(array(
91                     array (
92                         'principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/',
93                         'protected' => true,
94                         'privilege' => '{DAV:}all',
95                     ),
96                     array (
97                         'principal' => '{DAV:}authenticated',
98                         'protected' => false,
99                         'privilege' => '{urn:ietf:params:xml:ns:caldav}read-free-busy',
100                     ),
101                     array (
102                         'principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar-proxy-read/',
103                         'protected' => true,
104                         'privilege' => '{DAV:}read',
105                     ),
106                     array (
107                         'principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar-proxy-read/',
108                         'protected' => true,
109                         'privilege' => '{DAV:}read-current-user-privilege-set',
110                     ),
111                     array (
112                         'principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar-proxy-write/',
113                         'protected' => true,
114                         'privilege' => '{DAV:}read',
115                     ),
116                     array (
117                         'principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar-proxy-write/',
118                         'protected' => true,
119                         'privilege' => '{DAV:}read-current-user-privilege-set',
120                     ),
121                     array (
122                         'principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar-proxy-write/',
123                         'protected' => true,
124                         'privilege' => '{DAV:}write',
125                     ),
126                 )),
127                 '{DAV:}displayname' => 'calendar',
128                 '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set' =>
129                 new Sabre\CalDAV\Property\SupportedCalendarComponentSet(array(
130                     0 => 'VEVENT',
131                     1 => 'VTODO',
132                     2 => 'VTIMEZONE',
133                     3 => 'VJOURNAL',
134                     4 => 'VFREEBUSY',
135                 )),
136                 '{http://apple.com/ns/ical/}calendar-color' => '#711A76FF',
137             ),
138             
139             /*
140                 '/calendars/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/inbox/' =>
141                 array (
142                     '{DAV:}resourcetype' => new
143                     Sabre\DAV\Property\ResourceType(array(
144                          '{DAV:}collection',
145                         '{urn:ietf:params:xml:ns:caldav}schedule-inbox',
146                     )),
147                     '{DAV:}acl' => new Sabre\DAVACL\Property\Acl(
148                         array (
149                             array (
150                                 'principal' => '{DAV:}authenticated',
151                                 'protected' => false,
152                                 'privilege' => '{urn:ietf:params:xml:ns:caldav}schedule-deliver',
153                             ),
154                             array (
155                                 'principal' => '{DAV:}authenticated',
156                                 'protected' => false,
157                                 'privilege' => '{urn:ietf:params:xml:ns:caldav}schedule',
158                             ),
159                             array (
160                                 'principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/',
161                                 'protected' => true,
162                                 'privilege' => '{DAV:}all',
163                             ),
164                             array (
165                                 'principal' => '{DAV:}authenticated',
166                                 'protected' => false,
167                                 'privilege' => '{urn:ietf:params:xml:ns:caldav}read-free-busy',
168                             ),
169                             array (
170                                 'principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar-proxy-read/',
171                                 'protected' => true,
172                                 'privilege' => '{DAV:}read',
173                             ),
174                             array (
175                                 'principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar-proxy-read/',
176                                 'protected' => true,
177                                 'privilege' => '{DAV:}read-current-user-privilege-set',
178                             ),
179                             array (
180                                 'principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar-proxy-write/',
181                                 'protected' => true,
182                                 'privilege' => '{DAV:}read',
183                             ),
184                             array (
185                                 'principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar-proxy-write/',
186                                 'protected' => true,
187                                 'privilege' => '{DAV:}read-current-user-privilege-set',
188                             ),
189                             array (
190                                 'principal' => '/principals/__uids__/0AA03A3B-F7B6-459A-AB3E-4726E53637D0/calendar-proxy-write/',
191                                 'protected' => true,
192                                 'privilege' => '{DAV:}write',
193                             ),
194                         )),
195                         '{DAV:}displayname' => 'inbox',
196                         '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set' =>
197                         new Sabre\CalDAV\Property\SupportedCalendarComponentSet(array(
198                             'components' =>
199                             array (
200                                 0 => 'VEVENT',
201                                 1 => 'VTODO',
202                                 2 => 'VTIMEZONE',
203                                 3 => 'VJOURNAL',
204                                 4 => 'VFREEBUSY',
205                             ),
206                         )),
207                         ),
208                         */
209             );
210     }
211     
212     protected function _resolvePrincipalResponse()
213     {
214         return array (
215           '{DAV:}group-member-set' => 
216           new Tinebase_Import_CalDav_GroupMemberSet(array(
217           )),
218           '{DAV:}displayname' => 'calendar-proxy-write',
219         );
220     }
221     
222     /**
223      * perform mocked calDavRequest
224      *
225      * @param string $method
226      * @param string $uri
227      * @param strubg $body
228      * @param number $depth
229      * @param number $tries
230      * @param number $sleep
231      * @throws Tinebase_Exception
232      */
233     public function calDavRequest($method, $uri, $body, $depth = 0, $tries = 10, $sleep = 30)
234     {
235         if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__
236                  . ' Sending ' . $method . ' request for uri ' . $uri . ': ' . $body);
237         
238         if ($body == self::findCurrentUserPrincipalRequest) {
239             return $this->_currentUserPrincipalResponse;
240             
241         } else if ($body == self::findCalendarHomeSetRequest) {
242             return $this->_calendarHomeSetResponse;
243             
244         } else if ($body == '<?xml version="1.0"?>
245 <d:propfind xmlns:d="DAV:">
246   <d:prop>
247     <d:resourcetype/>
248     <d:acl/>
249     <d:displayname/>
250     <x:supported-calendar-component-set xmlns:x="urn:ietf:params:xml:ns:caldav"/>
251   <osxical:calendar-color xmlns:osxical="http://apple.com/ns/ical/"/></d:prop>
252 </d:propfind>
253 '/* self::findAllCalendarsRequest */) {
254             return $this->_findAllCalendarsResponse();
255             
256         } else if ($body == self::resolvePrincipalRequest) {
257             return $this->_resolvePrincipalResponse();
258             
259         } else if ($body == self::findAllCalendarICSsRequest) {
260             throw new Tinebase_Exception_NotImplemented('findAllCalendarICSsRequest to be implemented');
261             
262         } else if ($body == self::getAllCalendarDataRequest) {
263             throw new Tinebase_Exception_NotImplemented('getAllCalendarDataRequest to be implemented');
264             
265         } else {
266             throw new Tinebase_Exception_InvalidArgument('request not supported by mock');
267         }
268     }
269 }