PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Sabre/CalDAV/Backend/Mock.php

https://github.com/KOLANICH/SabreDAV
PHP | 402 lines | 134 code | 65 blank | 203 comment | 14 complexity | bad56370a85b4151e16d7b08d934d471 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. namespace Sabre\CalDAV\Backend;
  3. use Sabre\DAV;
  4. use Sabre\CalDAV;
  5. class Mock extends AbstractBackend implements NotificationSupport, SharingSupport {
  6. private $calendarData;
  7. private $calendars;
  8. private $notifications;
  9. private $shares = array();
  10. function __construct(array $calendars, array $calendarData, array $notifications = array()) {
  11. $this->calendars = $calendars;
  12. $this->calendarData = $calendarData;
  13. $this->notifications = $notifications;
  14. }
  15. /**
  16. * Returns a list of calendars for a principal.
  17. *
  18. * Every project is an array with the following keys:
  19. * * id, a unique id that will be used by other functions to modify the
  20. * calendar. This can be the same as the uri or a database key.
  21. * * uri, which the basename of the uri with which the calendar is
  22. * accessed.
  23. * * principalUri. The owner of the calendar. Almost always the same as
  24. * principalUri passed to this method.
  25. *
  26. * Furthermore it can contain webdav properties in clark notation. A very
  27. * common one is '{DAV:}displayname'.
  28. *
  29. * @param string $principalUri
  30. * @return array
  31. */
  32. function getCalendarsForUser($principalUri) {
  33. $r = array();
  34. foreach($this->calendars as $row) {
  35. if ($row['principaluri'] == $principalUri) {
  36. $r[] = $row;
  37. }
  38. }
  39. return $r;
  40. }
  41. /**
  42. * Creates a new calendar for a principal.
  43. *
  44. * If the creation was a success, an id must be returned that can be used to reference
  45. * this calendar in other methods, such as updateCalendar.
  46. *
  47. * This function must return a server-wide unique id that can be used
  48. * later to reference the calendar.
  49. *
  50. * @param string $principalUri
  51. * @param string $calendarUri
  52. * @param array $properties
  53. * @return string|int
  54. */
  55. function createCalendar($principalUri,$calendarUri,array $properties) {
  56. $id = DAV\UUIDUtil::getUUID();
  57. $this->calendars[] = array_merge(array(
  58. 'id' => $id,
  59. 'principaluri' => $principalUri,
  60. 'uri' => $calendarUri,
  61. '{' . CalDAV\Plugin::NS_CALDAV . '}supported-calendar-component-set' => new CalDAV\Property\SupportedCalendarComponentSet(array('VEVENT','VTODO')),
  62. ), $properties);
  63. return $id;
  64. }
  65. /**
  66. * Updates properties on this node,
  67. *
  68. * The properties array uses the propertyName in clark-notation as key,
  69. * and the array value for the property value. In the case a property
  70. * should be deleted, the property value will be null.
  71. *
  72. * This method must be atomic. If one property cannot be changed, the
  73. * entire operation must fail.
  74. *
  75. * If the operation was successful, true can be returned.
  76. * If the operation failed, false can be returned.
  77. *
  78. * Deletion of a non-existent property is always successful.
  79. *
  80. * Lastly, it is optional to return detailed information about any
  81. * failures. In this case an array should be returned with the following
  82. * structure:
  83. *
  84. * array(
  85. * 403 => array(
  86. * '{DAV:}displayname' => null,
  87. * ),
  88. * 424 => array(
  89. * '{DAV:}owner' => null,
  90. * )
  91. * )
  92. *
  93. * In this example it was forbidden to update {DAV:}displayname.
  94. * (403 Forbidden), which in turn also caused {DAV:}owner to fail
  95. * (424 Failed Dependency) because the request needs to be atomic.
  96. *
  97. * @param string $calendarId
  98. * @param array $properties
  99. * @return bool|array
  100. */
  101. public function updateCalendar($calendarId, array $properties) {
  102. return false;
  103. }
  104. /**
  105. * Delete a calendar and all it's objects
  106. *
  107. * @param string $calendarId
  108. * @return void
  109. */
  110. public function deleteCalendar($calendarId) {
  111. foreach($this->calendars as $k=>$calendar) {
  112. if ($calendar['id'] === $calendarId) {
  113. unset($this->calendars[$k]);
  114. }
  115. }
  116. }
  117. /**
  118. * Returns all calendar objects within a calendar object.
  119. *
  120. * Every item contains an array with the following keys:
  121. * * id - unique identifier which will be used for subsequent updates
  122. * * calendardata - The iCalendar-compatible calendar data
  123. * * uri - a unique key which will be used to construct the uri. This can be any arbitrary string.
  124. * * lastmodified - a timestamp of the last modification time
  125. * * etag - An arbitrary string, surrounded by double-quotes. (e.g.:
  126. * ' "abcdef"')
  127. * * calendarid - The calendarid as it was passed to this function.
  128. *
  129. * Note that the etag is optional, but it's highly encouraged to return for
  130. * speed reasons.
  131. *
  132. * The calendardata is also optional. If it's not returned
  133. * 'getCalendarObject' will be called later, which *is* expected to return
  134. * calendardata.
  135. *
  136. * @param string $calendarId
  137. * @return array
  138. */
  139. public function getCalendarObjects($calendarId) {
  140. if (!isset($this->calendarData[$calendarId]))
  141. return array();
  142. $objects = $this->calendarData[$calendarId];
  143. foreach($objects as $uri => &$object) {
  144. $object['calendarid'] = $calendarId;
  145. $object['uri'] = $uri;
  146. }
  147. return $objects;
  148. }
  149. /**
  150. * Returns information from a single calendar object, based on it's object
  151. * uri.
  152. *
  153. * The returned array must have the same keys as getCalendarObjects. The
  154. * 'calendardata' object is required here though, while it's not required
  155. * for getCalendarObjects.
  156. *
  157. * @param string $calendarId
  158. * @param string $objectUri
  159. * @return array
  160. */
  161. function getCalendarObject($calendarId,$objectUri) {
  162. if (!isset($this->calendarData[$calendarId][$objectUri])) {
  163. throw new DAV\Exception\NotFound('Object could not be found');
  164. }
  165. $object = $this->calendarData[$calendarId][$objectUri];
  166. $object['calendarid'] = $calendarId;
  167. $object['uri'] = $objectUri;
  168. return $object;
  169. }
  170. /**
  171. * Creates a new calendar object.
  172. *
  173. * @param string $calendarId
  174. * @param string $objectUri
  175. * @param string $calendarData
  176. * @return void
  177. */
  178. function createCalendarObject($calendarId,$objectUri,$calendarData) {
  179. $this->calendarData[$calendarId][$objectUri] = array(
  180. 'calendardata' => $calendarData,
  181. 'calendarid' => $calendarId,
  182. 'uri' => $objectUri,
  183. );
  184. return '"' . md5($calendarData) . '"';
  185. }
  186. /**
  187. * Updates an existing calendarobject, based on it's uri.
  188. *
  189. * @param string $calendarId
  190. * @param string $objectUri
  191. * @param string $calendarData
  192. * @return void
  193. */
  194. function updateCalendarObject($calendarId,$objectUri,$calendarData) {
  195. $this->calendarData[$calendarId][$objectUri] = array(
  196. 'calendardata' => $calendarData,
  197. 'calendarid' => $calendarId,
  198. 'uri' => $objectUri,
  199. );
  200. return '"' . md5($calendarData) . '"';
  201. }
  202. /**
  203. * Deletes an existing calendar object.
  204. *
  205. * @param string $calendarId
  206. * @param string $objectUri
  207. * @return void
  208. */
  209. function deleteCalendarObject($calendarId,$objectUri) {
  210. throw new Exception('Not implemented');
  211. }
  212. /**
  213. * Returns a list of notifications for a given principal url.
  214. *
  215. * The returned array should only consist of implementations of
  216. * Sabre\CalDAV\Notifications\INotificationType.
  217. *
  218. * @param string $principalUri
  219. * @return array
  220. */
  221. public function getNotificationsForPrincipal($principalUri) {
  222. if (isset($this->notifications[$principalUri])) {
  223. return $this->notifications[$principalUri];
  224. }
  225. return array();
  226. }
  227. /**
  228. * This deletes a specific notifcation.
  229. *
  230. * This may be called by a client once it deems a notification handled.
  231. *
  232. * @param string $principalUri
  233. * @param Sabre\CalDAV\Notifications\INotificationType $notification
  234. * @return void
  235. */
  236. public function deleteNotification($principalUri, CalDAV\Notifications\INotificationType $notification) {
  237. foreach($this->notifications[$principalUri] as $key=>$value) {
  238. if ($notification === $value) {
  239. unset($this->notifications[$principalUri][$key]);
  240. }
  241. }
  242. }
  243. /**
  244. * Updates the list of shares.
  245. *
  246. * The first array is a list of people that are to be added to the
  247. * calendar.
  248. *
  249. * Every element in the add array has the following properties:
  250. * * href - A url. Usually a mailto: address
  251. * * commonName - Usually a first and last name, or false
  252. * * summary - A description of the share, can also be false
  253. * * readOnly - A boolean value
  254. *
  255. * Every element in the remove array is just the address string.
  256. *
  257. * Note that if the calendar is currently marked as 'not shared' by and
  258. * this method is called, the calendar should be 'upgraded' to a shared
  259. * calendar.
  260. *
  261. * @param mixed $calendarId
  262. * @param array $add
  263. * @param array $remove
  264. * @return void
  265. */
  266. public function updateShares($calendarId, array $add, array $remove) {
  267. if (!isset($this->shares[$calendarId])) {
  268. $this->shares[$calendarId] = array();
  269. }
  270. foreach($add as $val) {
  271. $val['status'] = CalDAV\SharingPlugin::STATUS_NORESPONSE;
  272. $this->shares[$calendarId][] = $val;
  273. }
  274. foreach($this->shares[$calendarId] as $k=>$share) {
  275. if (in_array($share['href'], $remove)) {
  276. unset($this->shares[$calendarId][$k]);
  277. }
  278. }
  279. // Re-numbering keys
  280. $this->shares[$calendarId] = array_values($this->shares[$calendarId]);
  281. }
  282. /**
  283. * Returns the list of people whom this calendar is shared with.
  284. *
  285. * Every element in this array should have the following properties:
  286. * * href - Often a mailto: address
  287. * * commonName - Optional, for example a first + last name
  288. * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants.
  289. * * readOnly - boolean
  290. * * summary - Optional, a description for the share
  291. *
  292. * @param mixed $calendarId
  293. * @return array
  294. */
  295. public function getShares($calendarId) {
  296. if (!isset($this->shares[$calendarId])) {
  297. return array();
  298. }
  299. return $this->shares[$calendarId];
  300. }
  301. /**
  302. * This method is called when a user replied to a request to share.
  303. *
  304. * @param string href The sharee who is replying (often a mailto: address)
  305. * @param int status One of the SharingPlugin::STATUS_* constants
  306. * @param string $calendarUri The url to the calendar thats being shared
  307. * @param string $inReplyTo The unique id this message is a response to
  308. * @param string $summary A description of the reply
  309. * @return void
  310. */
  311. public function shareReply($href, $status, $calendarUri, $inReplyTo, $summary = null) {
  312. // This operation basically doesn't do anything yet
  313. if ($status === CalDAV\SharingPlugin::STATUS_ACCEPTED) {
  314. return 'calendars/blabla/calendar';
  315. }
  316. }
  317. /**
  318. * Publishes a calendar
  319. *
  320. * @param mixed $calendarId
  321. * @param bool $value
  322. * @return void
  323. */
  324. public function setPublishStatus($calendarId, $value) {
  325. foreach($this->calendars as $k=>$cal) {
  326. if ($cal['id'] === $calendarId) {
  327. if (!$value) {
  328. unset($cal['{http://calendarserver.org/ns/}publish-url']);
  329. } else {
  330. $cal['{http://calendarserver.org/ns/}publish-url'] = 'http://example.org/public/ ' . $calendarId . '.ics';
  331. }
  332. return;
  333. }
  334. }
  335. throw new DAV\Exception('Calendar with id "' . $calendarId . '" not found');
  336. }
  337. }