CalDavBackendTest.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. * @copyright Copyright (c) 2017 Georg Ehrke
  5. *
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Thomas Citharel <tcit@tcit.fr>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\DAV\Tests\unit\CalDAV;
  28. use DateTime;
  29. use DateTimeZone;
  30. use OCA\DAV\CalDAV\CalDavBackend;
  31. use OCA\DAV\CalDAV\Calendar;
  32. use OCP\IConfig;
  33. use OCP\IL10N;
  34. use Sabre\DAV\PropPatch;
  35. use Sabre\DAV\Xml\Property\Href;
  36. use Sabre\DAVACL\IACL;
  37. use Sabre\DAV\Exception\NotFound;
  38. /**
  39. * Class CalDavBackendTest
  40. *
  41. * @group DB
  42. *
  43. * @package OCA\DAV\Tests\unit\CalDAV
  44. */
  45. class CalDavBackendTest extends AbstractCalDavBackend {
  46. public function testCalendarOperations() {
  47. $calendarId = $this->createTestCalendar();
  48. // update it's display name
  49. $patch = new PropPatch([
  50. '{DAV:}displayname' => 'Unit test',
  51. '{urn:ietf:params:xml:ns:caldav}calendar-description' => 'Calendar used for unit testing'
  52. ]);
  53. $this->dispatcher->expects($this->at(0))
  54. ->method('dispatch')
  55. ->with('\OCA\DAV\CalDAV\CalDavBackend::updateCalendar');
  56. $this->backend->updateCalendar($calendarId, $patch);
  57. $patch->commit();
  58. $this->assertEquals(1, $this->backend->getCalendarsForUserCount(self::UNIT_TEST_USER));
  59. $calendars = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER);
  60. $this->assertCount(1, $calendars);
  61. $this->assertEquals('Unit test', $calendars[0]['{DAV:}displayname']);
  62. $this->assertEquals('Calendar used for unit testing', $calendars[0]['{urn:ietf:params:xml:ns:caldav}calendar-description']);
  63. $this->assertEquals('User\'s displayname', $calendars[0]['{http://nextcloud.com/ns}owner-displayname']);
  64. // delete the address book
  65. $this->dispatcher->expects($this->at(0))
  66. ->method('dispatch')
  67. ->with('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendar');
  68. $this->backend->deleteCalendar($calendars[0]['id']);
  69. $calendars = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER);
  70. $this->assertCount(0, $calendars);
  71. }
  72. public function providesSharingData() {
  73. return [
  74. [true, true, true, false, [
  75. [
  76. 'href' => 'principal:' . self::UNIT_TEST_USER1,
  77. 'readOnly' => false
  78. ],
  79. [
  80. 'href' => 'principal:' . self::UNIT_TEST_GROUP,
  81. 'readOnly' => true
  82. ]
  83. ]],
  84. [true, true, true, false, [
  85. [
  86. 'href' => 'principal:' . self::UNIT_TEST_GROUP,
  87. 'readOnly' => true,
  88. ],
  89. [
  90. 'href' => 'principal:' . self::UNIT_TEST_GROUP2,
  91. 'readOnly' => false,
  92. ],
  93. ]],
  94. [true, true, true, true, [
  95. [
  96. 'href' => 'principal:' . self::UNIT_TEST_GROUP,
  97. 'readOnly' => false,
  98. ],
  99. [
  100. 'href' => 'principal:' . self::UNIT_TEST_GROUP2,
  101. 'readOnly' => true,
  102. ],
  103. ]],
  104. [true, false, false, false, [
  105. [
  106. 'href' => 'principal:' . self::UNIT_TEST_USER1,
  107. 'readOnly' => true
  108. ],
  109. ]],
  110. ];
  111. }
  112. /**
  113. * @dataProvider providesSharingData
  114. */
  115. public function testCalendarSharing($userCanRead, $userCanWrite, $groupCanRead, $groupCanWrite, $add) {
  116. /** @var IL10N|\PHPUnit_Framework_MockObject_MockObject $l10n */
  117. $l10n = $this->createMock(IL10N::class);
  118. $l10n
  119. ->expects($this->any())
  120. ->method('t')
  121. ->will($this->returnCallback(function ($text, $parameters = array()) {
  122. return vsprintf($text, $parameters);
  123. }));
  124. $config = $this->createMock(IConfig::class);
  125. $this->userManager->expects($this->any())
  126. ->method('userExists')
  127. ->willReturn(true);
  128. $this->groupManager->expects($this->any())
  129. ->method('groupExists')
  130. ->willReturn(true);
  131. $calendarId = $this->createTestCalendar();
  132. $calendars = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER);
  133. $this->assertCount(1, $calendars);
  134. $calendar = new Calendar($this->backend, $calendars[0], $l10n, $config);
  135. $this->dispatcher->expects($this->at(0))
  136. ->method('dispatch')
  137. ->with('\OCA\DAV\CalDAV\CalDavBackend::updateShares');
  138. $this->backend->updateShares($calendar, $add, []);
  139. $calendars = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER1);
  140. $this->assertCount(1, $calendars);
  141. $calendar = new Calendar($this->backend, $calendars[0], $l10n, $config);
  142. $acl = $calendar->getACL();
  143. $this->assertAcl(self::UNIT_TEST_USER, '{DAV:}read', $acl);
  144. $this->assertAcl(self::UNIT_TEST_USER, '{DAV:}write', $acl);
  145. $this->assertAccess($userCanRead, self::UNIT_TEST_USER1, '{DAV:}read', $acl);
  146. $this->assertAccess($userCanWrite, self::UNIT_TEST_USER1, '{DAV:}write', $acl);
  147. $this->assertEquals(self::UNIT_TEST_USER, $calendar->getOwner());
  148. // test acls on the child
  149. $uri = static::getUniqueID('calobj');
  150. $calData = <<<'EOD'
  151. BEGIN:VCALENDAR
  152. VERSION:2.0
  153. PRODID:ownCloud Calendar
  154. BEGIN:VEVENT
  155. CREATED;VALUE=DATE-TIME:20130910T125139Z
  156. UID:47d15e3ec8
  157. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  158. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  159. SUMMARY:Test Event
  160. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  161. DTEND;VALUE=DATE-TIME:20130912T140000Z
  162. CLASS:PUBLIC
  163. END:VEVENT
  164. END:VCALENDAR
  165. EOD;
  166. $this->dispatcher->expects($this->at(0))
  167. ->method('dispatch')
  168. ->with('\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject');
  169. $this->backend->createCalendarObject($calendarId, $uri, $calData);
  170. /** @var IACL $child */
  171. $child = $calendar->getChild($uri);
  172. $acl = $child->getACL();
  173. $this->assertAcl(self::UNIT_TEST_USER, '{DAV:}read', $acl);
  174. $this->assertAcl(self::UNIT_TEST_USER, '{DAV:}write', $acl);
  175. $this->assertAccess($userCanRead, self::UNIT_TEST_USER1, '{DAV:}read', $acl);
  176. $this->assertAccess($userCanWrite, self::UNIT_TEST_USER1, '{DAV:}write', $acl);
  177. // delete the address book
  178. $this->dispatcher->expects($this->at(0))
  179. ->method('dispatch')
  180. ->with('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendar');
  181. $this->backend->deleteCalendar($calendars[0]['id']);
  182. $calendars = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER);
  183. $this->assertCount(0, $calendars);
  184. }
  185. public function testCalendarObjectsOperations() {
  186. $calendarId = $this->createTestCalendar();
  187. // create a card
  188. $uri = static::getUniqueID('calobj');
  189. $calData = <<<'EOD'
  190. BEGIN:VCALENDAR
  191. VERSION:2.0
  192. PRODID:ownCloud Calendar
  193. BEGIN:VEVENT
  194. CREATED;VALUE=DATE-TIME:20130910T125139Z
  195. UID:47d15e3ec8
  196. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  197. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  198. SUMMARY:Test Event
  199. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  200. DTEND;VALUE=DATE-TIME:20130912T140000Z
  201. CLASS:PUBLIC
  202. END:VEVENT
  203. END:VCALENDAR
  204. EOD;
  205. $this->dispatcher->expects($this->at(0))
  206. ->method('dispatch')
  207. ->with('\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject');
  208. $this->backend->createCalendarObject($calendarId, $uri, $calData);
  209. // get all the cards
  210. $calendarObjects = $this->backend->getCalendarObjects($calendarId);
  211. $this->assertCount(1, $calendarObjects);
  212. $this->assertEquals($calendarId, $calendarObjects[0]['calendarid']);
  213. $this->assertArrayHasKey('classification', $calendarObjects[0]);
  214. // get the cards
  215. $calendarObject = $this->backend->getCalendarObject($calendarId, $uri);
  216. $this->assertNotNull($calendarObject);
  217. $this->assertArrayHasKey('id', $calendarObject);
  218. $this->assertArrayHasKey('uri', $calendarObject);
  219. $this->assertArrayHasKey('lastmodified', $calendarObject);
  220. $this->assertArrayHasKey('etag', $calendarObject);
  221. $this->assertArrayHasKey('size', $calendarObject);
  222. $this->assertArrayHasKey('classification', $calendarObject);
  223. $this->assertEquals($calData, $calendarObject['calendardata']);
  224. // update the card
  225. $calData = <<<'EOD'
  226. BEGIN:VCALENDAR
  227. VERSION:2.0
  228. PRODID:ownCloud Calendar
  229. BEGIN:VEVENT
  230. CREATED;VALUE=DATE-TIME:20130910T125139Z
  231. UID:47d15e3ec8
  232. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  233. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  234. SUMMARY:Test Event
  235. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  236. DTEND;VALUE=DATE-TIME:20130912T140000Z
  237. END:VEVENT
  238. END:VCALENDAR
  239. EOD;
  240. $this->dispatcher->expects($this->at(0))
  241. ->method('dispatch')
  242. ->with('\OCA\DAV\CalDAV\CalDavBackend::updateCalendarObject');
  243. $this->backend->updateCalendarObject($calendarId, $uri, $calData);
  244. $calendarObject = $this->backend->getCalendarObject($calendarId, $uri);
  245. $this->assertEquals($calData, $calendarObject['calendardata']);
  246. // delete the card
  247. $this->dispatcher->expects($this->at(0))
  248. ->method('dispatch')
  249. ->with('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject');
  250. $this->backend->deleteCalendarObject($calendarId, $uri);
  251. $calendarObjects = $this->backend->getCalendarObjects($calendarId);
  252. $this->assertCount(0, $calendarObjects);
  253. }
  254. /**
  255. * @expectedException \Sabre\DAV\Exception\BadRequest
  256. * @expectedExceptionMessage Calendar object with uid already exists in this calendar collection.
  257. */
  258. public function testMultipleCalendarObjectsWithSameUID() {
  259. $calendarId = $this->createTestCalendar();
  260. $calData = <<<'EOD'
  261. BEGIN:VCALENDAR
  262. VERSION:2.0
  263. PRODID:ownCloud Calendar
  264. BEGIN:VEVENT
  265. CREATED;VALUE=DATE-TIME:20130910T125139Z
  266. UID:47d15e3ec8-1
  267. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  268. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  269. SUMMARY:Test Event
  270. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  271. DTEND;VALUE=DATE-TIME:20130912T140000Z
  272. CLASS:PUBLIC
  273. END:VEVENT
  274. END:VCALENDAR
  275. EOD;
  276. $uri0 = static::getUniqueID('event');
  277. $uri1 = static::getUniqueID('event');
  278. $this->backend->createCalendarObject($calendarId, $uri0, $calData);
  279. $this->backend->createCalendarObject($calendarId, $uri1, $calData);
  280. }
  281. public function testMultiCalendarObjects() {
  282. $calendarId = $this->createTestCalendar();
  283. // create an event
  284. $calData = [];
  285. $calData[] = <<<'EOD'
  286. BEGIN:VCALENDAR
  287. VERSION:2.0
  288. PRODID:ownCloud Calendar
  289. BEGIN:VEVENT
  290. CREATED;VALUE=DATE-TIME:20130910T125139Z
  291. UID:47d15e3ec8-1
  292. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  293. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  294. SUMMARY:Test Event
  295. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  296. DTEND;VALUE=DATE-TIME:20130912T140000Z
  297. CLASS:PUBLIC
  298. END:VEVENT
  299. END:VCALENDAR
  300. EOD;
  301. $calData[] = <<<'EOD'
  302. BEGIN:VCALENDAR
  303. VERSION:2.0
  304. PRODID:ownCloud Calendar
  305. BEGIN:VEVENT
  306. CREATED;VALUE=DATE-TIME:20130910T125139Z
  307. UID:47d15e3ec8-2
  308. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  309. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  310. SUMMARY:Test Event
  311. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  312. DTEND;VALUE=DATE-TIME:20130912T140000Z
  313. CLASS:PUBLIC
  314. END:VEVENT
  315. END:VCALENDAR
  316. EOD;
  317. $calData[] = <<<'EOD'
  318. BEGIN:VCALENDAR
  319. VERSION:2.0
  320. PRODID:ownCloud Calendar
  321. BEGIN:VEVENT
  322. CREATED;VALUE=DATE-TIME:20130910T125139Z
  323. UID:47d15e3ec8-3
  324. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  325. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  326. SUMMARY:Test Event
  327. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  328. DTEND;VALUE=DATE-TIME:20130912T140000Z
  329. CLASS:PUBLIC
  330. END:VEVENT
  331. END:VCALENDAR
  332. EOD;
  333. $uri0 = static::getUniqueID('card');
  334. $this->dispatcher->expects($this->at(0))
  335. ->method('dispatch')
  336. ->with('\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject');
  337. $this->backend->createCalendarObject($calendarId, $uri0, $calData[0]);
  338. $uri1 = static::getUniqueID('card');
  339. $this->dispatcher->expects($this->at(0))
  340. ->method('dispatch')
  341. ->with('\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject');
  342. $this->backend->createCalendarObject($calendarId, $uri1, $calData[1]);
  343. $uri2 = static::getUniqueID('card');
  344. $this->dispatcher->expects($this->at(0))
  345. ->method('dispatch')
  346. ->with('\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject');
  347. $this->backend->createCalendarObject($calendarId, $uri2, $calData[2]);
  348. // get all the cards
  349. $calendarObjects = $this->backend->getCalendarObjects($calendarId);
  350. $this->assertCount(3, $calendarObjects);
  351. // get the cards
  352. $calendarObjects = $this->backend->getMultipleCalendarObjects($calendarId, [$uri1, $uri2]);
  353. $this->assertCount(2, $calendarObjects);
  354. foreach($calendarObjects as $card) {
  355. $this->assertArrayHasKey('id', $card);
  356. $this->assertArrayHasKey('uri', $card);
  357. $this->assertArrayHasKey('lastmodified', $card);
  358. $this->assertArrayHasKey('etag', $card);
  359. $this->assertArrayHasKey('size', $card);
  360. $this->assertArrayHasKey('classification', $card);
  361. }
  362. usort($calendarObjects, function($a, $b) {
  363. return $a['id'] - $b['id'];
  364. });
  365. $this->assertEquals($calData[1], $calendarObjects[0]['calendardata']);
  366. $this->assertEquals($calData[2], $calendarObjects[1]['calendardata']);
  367. // delete the card
  368. $this->dispatcher->expects($this->at(0))
  369. ->method('dispatch')
  370. ->with('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject');
  371. $this->backend->deleteCalendarObject($calendarId, $uri0);
  372. $this->dispatcher->expects($this->at(0))
  373. ->method('dispatch')
  374. ->with('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject');
  375. $this->backend->deleteCalendarObject($calendarId, $uri1);
  376. $this->dispatcher->expects($this->at(0))
  377. ->method('dispatch')
  378. ->with('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject');
  379. $this->backend->deleteCalendarObject($calendarId, $uri2);
  380. $calendarObjects = $this->backend->getCalendarObjects($calendarId);
  381. $this->assertCount(0, $calendarObjects);
  382. }
  383. /**
  384. * @dataProvider providesCalendarQueryParameters
  385. */
  386. public function testCalendarQuery($expectedEventsInResult, $propFilters, $compFilter) {
  387. $calendarId = $this->createTestCalendar();
  388. $events = [];
  389. $events[0] = $this->createEvent($calendarId, '20130912T130000Z', '20130912T140000Z');
  390. $events[1] = $this->createEvent($calendarId, '20130912T150000Z', '20130912T170000Z');
  391. $events[2] = $this->createEvent($calendarId, '20130912T173000Z', '20130912T220000Z');
  392. $events[3] = $this->createEvent($calendarId, '21130912T130000Z', '22130912T130000Z');
  393. $result = $this->backend->calendarQuery($calendarId, [
  394. 'name' => '',
  395. 'prop-filters' => $propFilters,
  396. 'comp-filters' => $compFilter
  397. ]);
  398. $expectedEventsInResult = array_map(function($index) use($events) {
  399. return $events[$index];
  400. }, $expectedEventsInResult);
  401. $this->assertEquals($expectedEventsInResult, $result, '', 0.0, 10, true);
  402. }
  403. public function testGetCalendarObjectByUID() {
  404. $calendarId = $this->createTestCalendar();
  405. $uri = static::getUniqueID('calobj');
  406. $calData = <<<'EOD'
  407. BEGIN:VCALENDAR
  408. VERSION:2.0
  409. PRODID:ownCloud Calendar
  410. BEGIN:VEVENT
  411. CREATED;VALUE=DATE-TIME:20130910T125139Z
  412. UID:47d15e3ec8
  413. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  414. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  415. SUMMARY:Test Event
  416. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  417. DTEND;VALUE=DATE-TIME:20130912T140000Z
  418. CLASS:PUBLIC
  419. END:VEVENT
  420. END:VCALENDAR
  421. EOD;
  422. $this->backend->createCalendarObject($calendarId, $uri, $calData);
  423. $co = $this->backend->getCalendarObjectByUID(self::UNIT_TEST_USER, '47d15e3ec8');
  424. $this->assertNotNull($co);
  425. }
  426. public function providesCalendarQueryParameters() {
  427. return [
  428. 'all' => [[0, 1, 2, 3], [], []],
  429. 'only-todos' => [[], ['name' => 'VTODO'], []],
  430. 'only-events' => [[0, 1, 2, 3], [], [['name' => 'VEVENT', 'is-not-defined' => false, 'comp-filters' => [], 'time-range' => ['start' => null, 'end' => null], 'prop-filters' => []]],],
  431. 'start' => [[1, 2, 3], [], [['name' => 'VEVENT', 'is-not-defined' => false, 'comp-filters' => [], 'time-range' => ['start' => new DateTime('2013-09-12 14:00:00', new DateTimeZone('UTC')), 'end' => null], 'prop-filters' => []]],],
  432. 'end' => [[0], [], [['name' => 'VEVENT', 'is-not-defined' => false, 'comp-filters' => [], 'time-range' => ['start' => null, 'end' => new DateTime('2013-09-12 14:00:00', new DateTimeZone('UTC'))], 'prop-filters' => []]],],
  433. 'future' => [[3], [], [['name' => 'VEVENT', 'is-not-defined' => false, 'comp-filters' => [], 'time-range' => ['start' => new DateTime('2099-09-12 14:00:00', new DateTimeZone('UTC')), 'end' => null], 'prop-filters' => []]],],
  434. ];
  435. }
  436. public function testSyncSupport() {
  437. $calendarId = $this->createTestCalendar();
  438. // fist call without synctoken
  439. $changes = $this->backend->getChangesForCalendar($calendarId, '', 1);
  440. $syncToken = $changes['syncToken'];
  441. // add a change
  442. $event = $this->createEvent($calendarId, '20130912T130000Z', '20130912T140000Z');
  443. // look for changes
  444. $changes = $this->backend->getChangesForCalendar($calendarId, $syncToken, 1);
  445. $this->assertEquals($event, $changes['added'][0]);
  446. }
  447. public function testPublications() {
  448. $this->dispatcher->expects($this->at(0))
  449. ->method('dispatch')
  450. ->with('\OCA\DAV\CalDAV\CalDavBackend::createCalendar');
  451. $this->backend->createCalendar(self::UNIT_TEST_USER, 'Example', []);
  452. $calendarInfo = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER)[0];
  453. /** @var IL10N|\PHPUnit_Framework_MockObject_MockObject $l10n */
  454. $l10n = $this->createMock(IL10N::class);
  455. $config = $this->createMock(IConfig::class);
  456. $calendar = new Calendar($this->backend, $calendarInfo, $l10n, $config);
  457. $calendar->setPublishStatus(true);
  458. $this->assertNotEquals(false, $calendar->getPublishStatus());
  459. $publicCalendars = $this->backend->getPublicCalendars();
  460. $this->assertCount(1, $publicCalendars);
  461. $this->assertEquals(true, $publicCalendars[0]['{http://owncloud.org/ns}public']);
  462. $this->assertEquals('User\'s displayname', $publicCalendars[0]['{http://nextcloud.com/ns}owner-displayname']);
  463. $publicCalendarURI = $publicCalendars[0]['uri'];
  464. $publicCalendar = $this->backend->getPublicCalendar($publicCalendarURI);
  465. $this->assertEquals(true, $publicCalendar['{http://owncloud.org/ns}public']);
  466. $calendar->setPublishStatus(false);
  467. $this->assertEquals(false, $calendar->getPublishStatus());
  468. $this->expectException(NotFound::class);
  469. $this->backend->getPublicCalendar($publicCalendarURI);
  470. }
  471. public function testSubscriptions() {
  472. $id = $this->backend->createSubscription(self::UNIT_TEST_USER, 'Subscription', [
  473. '{http://calendarserver.org/ns/}source' => new Href('test-source'),
  474. '{http://apple.com/ns/ical/}calendar-color' => '#1C4587',
  475. '{http://calendarserver.org/ns/}subscribed-strip-todos' => ''
  476. ]);
  477. $subscriptions = $this->backend->getSubscriptionsForUser(self::UNIT_TEST_USER);
  478. $this->assertCount(1, $subscriptions);
  479. $this->assertEquals('#1C4587', $subscriptions[0]['{http://apple.com/ns/ical/}calendar-color']);
  480. $this->assertEquals(true, $subscriptions[0]['{http://calendarserver.org/ns/}subscribed-strip-todos']);
  481. $this->assertEquals($id, $subscriptions[0]['id']);
  482. $patch = new PropPatch([
  483. '{DAV:}displayname' => 'Unit test',
  484. '{http://apple.com/ns/ical/}calendar-color' => '#ac0606',
  485. ]);
  486. $this->backend->updateSubscription($id, $patch);
  487. $patch->commit();
  488. $subscriptions = $this->backend->getSubscriptionsForUser(self::UNIT_TEST_USER);
  489. $this->assertCount(1, $subscriptions);
  490. $this->assertEquals($id, $subscriptions[0]['id']);
  491. $this->assertEquals('Unit test', $subscriptions[0]['{DAV:}displayname']);
  492. $this->assertEquals('#ac0606', $subscriptions[0]['{http://apple.com/ns/ical/}calendar-color']);
  493. $this->backend->deleteSubscription($id);
  494. $subscriptions = $this->backend->getSubscriptionsForUser(self::UNIT_TEST_USER);
  495. $this->assertCount(0, $subscriptions);
  496. }
  497. public function testScheduling() {
  498. $this->backend->createSchedulingObject(self::UNIT_TEST_USER, 'Sample Schedule', '');
  499. $sos = $this->backend->getSchedulingObjects(self::UNIT_TEST_USER);
  500. $this->assertCount(1, $sos);
  501. $so = $this->backend->getSchedulingObject(self::UNIT_TEST_USER, 'Sample Schedule');
  502. $this->assertNotNull($so);
  503. $this->backend->deleteSchedulingObject(self::UNIT_TEST_USER, 'Sample Schedule');
  504. $sos = $this->backend->getSchedulingObjects(self::UNIT_TEST_USER);
  505. $this->assertCount(0, $sos);
  506. }
  507. /**
  508. * @dataProvider providesCalDataForGetDenormalizedData
  509. */
  510. public function testGetDenormalizedData($expected, $key, $calData) {
  511. $actual = $this->backend->getDenormalizedData($calData);
  512. $this->assertEquals($expected, $actual[$key]);
  513. }
  514. public function providesCalDataForGetDenormalizedData() {
  515. return [
  516. 'first occurrence before unix epoch starts' => [0, 'firstOccurence', "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nCALSCALE:GREGORIAN\r\nBEGIN:VEVENT\r\nUID:413F269B-B51B-46B1-AFB6-40055C53A4DC\r\nDTSTAMP:20160309T095056Z\r\nDTSTART;VALUE=DATE:16040222\r\nDTEND;VALUE=DATE:16040223\r\nRRULE:FREQ=YEARLY\r\nSUMMARY:SUMMARY\r\nTRANSP:TRANSPARENT\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n"],
  517. 'no first occurrence because yearly' => [null, 'firstOccurence', "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nCALSCALE:GREGORIAN\r\nBEGIN:VEVENT\r\nUID:413F269B-B51B-46B1-AFB6-40055C53A4DC\r\nDTSTAMP:20160309T095056Z\r\nRRULE:FREQ=YEARLY\r\nSUMMARY:SUMMARY\r\nTRANSP:TRANSPARENT\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n"],
  518. 'CLASS:PRIVATE' => [CalDavBackend::CLASSIFICATION_PRIVATE, 'classification', "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//dmfs.org//mimedir.icalendar//EN\r\nBEGIN:VTIMEZONE\r\nTZID:Europe/Berlin\r\nX-LIC-LOCATION:Europe/Berlin\r\nBEGIN:DAYLIGHT\r\nTZOFFSETFROM:+0100\r\nTZOFFSETTO:+0200\r\nTZNAME:CEST\r\nDTSTART:19700329T020000\r\nRRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU\r\nEND:DAYLIGHT\r\nBEGIN:STANDARD\r\nTZOFFSETFROM:+0200\r\nTZOFFSETTO:+0100\r\nTZNAME:CET\r\nDTSTART:19701025T030000\r\nRRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU\r\nEND:STANDARD\r\nEND:VTIMEZONE\r\nBEGIN:VEVENT\r\nDTSTART;TZID=Europe/Berlin:20160419T130000\r\nSUMMARY:Test\r\nCLASS:PRIVATE\r\nTRANSP:OPAQUE\r\nSTATUS:CONFIRMED\r\nDTEND;TZID=Europe/Berlin:20160419T140000\r\nLAST-MODIFIED:20160419T074202Z\r\nDTSTAMP:20160419T074202Z\r\nCREATED:20160419T074202Z\r\nUID:2e468c48-7860-492e-bc52-92fa0daeeccf.1461051722310\r\nEND:VEVENT\r\nEND:VCALENDAR"],
  519. 'CLASS:PUBLIC' => [CalDavBackend::CLASSIFICATION_PUBLIC, 'classification', "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//dmfs.org//mimedir.icalendar//EN\r\nBEGIN:VTIMEZONE\r\nTZID:Europe/Berlin\r\nX-LIC-LOCATION:Europe/Berlin\r\nBEGIN:DAYLIGHT\r\nTZOFFSETFROM:+0100\r\nTZOFFSETTO:+0200\r\nTZNAME:CEST\r\nDTSTART:19700329T020000\r\nRRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU\r\nEND:DAYLIGHT\r\nBEGIN:STANDARD\r\nTZOFFSETFROM:+0200\r\nTZOFFSETTO:+0100\r\nTZNAME:CET\r\nDTSTART:19701025T030000\r\nRRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU\r\nEND:STANDARD\r\nEND:VTIMEZONE\r\nBEGIN:VEVENT\r\nDTSTART;TZID=Europe/Berlin:20160419T130000\r\nSUMMARY:Test\r\nCLASS:PUBLIC\r\nTRANSP:OPAQUE\r\nSTATUS:CONFIRMED\r\nDTEND;TZID=Europe/Berlin:20160419T140000\r\nLAST-MODIFIED:20160419T074202Z\r\nDTSTAMP:20160419T074202Z\r\nCREATED:20160419T074202Z\r\nUID:2e468c48-7860-492e-bc52-92fa0daeeccf.1461051722310\r\nEND:VEVENT\r\nEND:VCALENDAR"],
  520. 'CLASS:CONFIDENTIAL' => [CalDavBackend::CLASSIFICATION_CONFIDENTIAL, 'classification', "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//dmfs.org//mimedir.icalendar//EN\r\nBEGIN:VTIMEZONE\r\nTZID:Europe/Berlin\r\nX-LIC-LOCATION:Europe/Berlin\r\nBEGIN:DAYLIGHT\r\nTZOFFSETFROM:+0100\r\nTZOFFSETTO:+0200\r\nTZNAME:CEST\r\nDTSTART:19700329T020000\r\nRRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU\r\nEND:DAYLIGHT\r\nBEGIN:STANDARD\r\nTZOFFSETFROM:+0200\r\nTZOFFSETTO:+0100\r\nTZNAME:CET\r\nDTSTART:19701025T030000\r\nRRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU\r\nEND:STANDARD\r\nEND:VTIMEZONE\r\nBEGIN:VEVENT\r\nDTSTART;TZID=Europe/Berlin:20160419T130000\r\nSUMMARY:Test\r\nCLASS:CONFIDENTIAL\r\nTRANSP:OPAQUE\r\nSTATUS:CONFIRMED\r\nDTEND;TZID=Europe/Berlin:20160419T140000\r\nLAST-MODIFIED:20160419T074202Z\r\nDTSTAMP:20160419T074202Z\r\nCREATED:20160419T074202Z\r\nUID:2e468c48-7860-492e-bc52-92fa0daeeccf.1461051722310\r\nEND:VEVENT\r\nEND:VCALENDAR"],
  521. 'no class set -> public' => [CalDavBackend::CLASSIFICATION_PUBLIC, 'classification', "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//dmfs.org//mimedir.icalendar//EN\r\nBEGIN:VTIMEZONE\r\nTZID:Europe/Berlin\r\nX-LIC-LOCATION:Europe/Berlin\r\nBEGIN:DAYLIGHT\r\nTZOFFSETFROM:+0100\r\nTZOFFSETTO:+0200\r\nTZNAME:CEST\r\nDTSTART:19700329T020000\r\nRRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU\r\nEND:DAYLIGHT\r\nBEGIN:STANDARD\r\nTZOFFSETFROM:+0200\r\nTZOFFSETTO:+0100\r\nTZNAME:CET\r\nDTSTART:19701025T030000\r\nRRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU\r\nEND:STANDARD\r\nEND:VTIMEZONE\r\nBEGIN:VEVENT\r\nDTSTART;TZID=Europe/Berlin:20160419T130000\r\nSUMMARY:Test\r\nTRANSP:OPAQUE\r\nDTEND;TZID=Europe/Berlin:20160419T140000\r\nLAST-MODIFIED:20160419T074202Z\r\nDTSTAMP:20160419T074202Z\r\nCREATED:20160419T074202Z\r\nUID:2e468c48-7860-492e-bc52-92fa0daeeccf.1461051722310\r\nEND:VEVENT\r\nEND:VCALENDAR"],
  522. 'unknown class -> private' => [CalDavBackend::CLASSIFICATION_PRIVATE, 'classification', "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//dmfs.org//mimedir.icalendar//EN\r\nBEGIN:VTIMEZONE\r\nTZID:Europe/Berlin\r\nX-LIC-LOCATION:Europe/Berlin\r\nBEGIN:DAYLIGHT\r\nTZOFFSETFROM:+0100\r\nTZOFFSETTO:+0200\r\nTZNAME:CEST\r\nDTSTART:19700329T020000\r\nRRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU\r\nEND:DAYLIGHT\r\nBEGIN:STANDARD\r\nTZOFFSETFROM:+0200\r\nTZOFFSETTO:+0100\r\nTZNAME:CET\r\nDTSTART:19701025T030000\r\nRRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU\r\nEND:STANDARD\r\nEND:VTIMEZONE\r\nBEGIN:VEVENT\r\nDTSTART;TZID=Europe/Berlin:20160419T130000\r\nSUMMARY:Test\r\nCLASS:VERTRAULICH\r\nTRANSP:OPAQUE\r\nSTATUS:CONFIRMED\r\nDTEND;TZID=Europe/Berlin:20160419T140000\r\nLAST-MODIFIED:20160419T074202Z\r\nDTSTAMP:20160419T074202Z\r\nCREATED:20160419T074202Z\r\nUID:2e468c48-7860-492e-bc52-92fa0daeeccf.1461051722310\r\nEND:VEVENT\r\nEND:VCALENDAR"],
  523. ];
  524. }
  525. public function testCalendarSearch() {
  526. $calendarId = $this->createTestCalendar();
  527. $uri = static::getUniqueID('calobj');
  528. $calData = <<<EOD
  529. BEGIN:VCALENDAR
  530. VERSION:2.0
  531. PRODID:ownCloud Calendar
  532. BEGIN:VEVENT
  533. CREATED;VALUE=DATE-TIME:20130910T125139Z
  534. UID:47d15e3ec8
  535. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  536. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  537. SUMMARY:Test Event
  538. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  539. DTEND;VALUE=DATE-TIME:20130912T140000Z
  540. CLASS:PUBLIC
  541. END:VEVENT
  542. END:VCALENDAR
  543. EOD;
  544. $this->backend->createCalendarObject($calendarId, $uri, $calData);
  545. $search1 = $this->backend->calendarSearch(self::UNIT_TEST_USER, [
  546. 'comps' => [
  547. 'VEVENT',
  548. 'VTODO'
  549. ],
  550. 'props' => [
  551. 'SUMMARY',
  552. 'LOCATION'
  553. ],
  554. 'search-term' => 'Test',
  555. ]);
  556. $this->assertEquals(count($search1), 1);
  557. // update the card
  558. $calData = <<<'EOD'
  559. BEGIN:VCALENDAR
  560. VERSION:2.0
  561. PRODID:ownCloud Calendar
  562. BEGIN:VEVENT
  563. CREATED;VALUE=DATE-TIME:20130910T125139Z
  564. UID:47d15e3ec8
  565. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  566. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  567. SUMMARY:123 Event 🙈
  568. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  569. DTEND;VALUE=DATE-TIME:20130912T140000Z
  570. ATTENDEE;CN=test:mailto:foo@bar.com
  571. END:VEVENT
  572. END:VCALENDAR
  573. EOD;
  574. $this->backend->updateCalendarObject($calendarId, $uri, $calData);
  575. $search2 = $this->backend->calendarSearch(self::UNIT_TEST_USER, [
  576. 'comps' => [
  577. 'VEVENT',
  578. 'VTODO'
  579. ],
  580. 'props' => [
  581. 'SUMMARY',
  582. 'LOCATION'
  583. ],
  584. 'search-term' => 'Test',
  585. ]);
  586. $this->assertEquals(count($search2), 0);
  587. $search3 = $this->backend->calendarSearch(self::UNIT_TEST_USER, [
  588. 'comps' => [
  589. 'VEVENT',
  590. 'VTODO'
  591. ],
  592. 'props' => [
  593. 'SUMMARY',
  594. 'LOCATION'
  595. ],
  596. 'params' => [
  597. [
  598. 'property' => 'ATTENDEE',
  599. 'parameter' => 'CN'
  600. ]
  601. ],
  602. 'search-term' => 'Test',
  603. ]);
  604. $this->assertEquals(count($search3), 1);
  605. // t matches both summary and attendee's CN, but we want unique results
  606. $search4 = $this->backend->calendarSearch(self::UNIT_TEST_USER, [
  607. 'comps' => [
  608. 'VEVENT',
  609. 'VTODO'
  610. ],
  611. 'props' => [
  612. 'SUMMARY',
  613. 'LOCATION'
  614. ],
  615. 'params' => [
  616. [
  617. 'property' => 'ATTENDEE',
  618. 'parameter' => 'CN'
  619. ]
  620. ],
  621. 'search-term' => 't',
  622. ]);
  623. $this->assertEquals(count($search4), 1);
  624. $this->backend->deleteCalendarObject($calendarId, $uri);
  625. $search5 = $this->backend->calendarSearch(self::UNIT_TEST_USER, [
  626. 'comps' => [
  627. 'VEVENT',
  628. 'VTODO'
  629. ],
  630. 'props' => [
  631. 'SUMMARY',
  632. 'LOCATION'
  633. ],
  634. 'params' => [
  635. [
  636. 'property' => 'ATTENDEE',
  637. 'parameter' => 'CN'
  638. ]
  639. ],
  640. 'search-term' => 't',
  641. ]);
  642. $this->assertEquals(count($search5), 0);
  643. }
  644. /**
  645. * @dataProvider searchDataProvider
  646. */
  647. public function testSearch($isShared, $count) {
  648. $calendarId = $this->createTestCalendar();
  649. $uris = [];
  650. $calData = [];
  651. $uris[] = static::getUniqueID('calobj');
  652. $calData[] = <<<EOD
  653. BEGIN:VCALENDAR
  654. VERSION:2.0
  655. PRODID:Nextcloud Calendar
  656. BEGIN:VEVENT
  657. CREATED;VALUE=DATE-TIME:20130910T125139Z
  658. UID:47d15e3ec8-1
  659. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  660. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  661. SUMMARY:Test Event
  662. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  663. DTEND;VALUE=DATE-TIME:20130912T140000Z
  664. CLASS:PUBLIC
  665. END:VEVENT
  666. END:VCALENDAR
  667. EOD;
  668. $uris[] = static::getUniqueID('calobj');
  669. $calData[] = <<<EOD
  670. BEGIN:VCALENDAR
  671. VERSION:2.0
  672. PRODID:Nextcloud Calendar
  673. BEGIN:VEVENT
  674. CREATED;VALUE=DATE-TIME:20130910T125139Z
  675. UID:47d15e3ec8-2
  676. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  677. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  678. SUMMARY:123
  679. LOCATION:Test
  680. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  681. DTEND;VALUE=DATE-TIME:20130912T140000Z
  682. CLASS:PUBLIC
  683. END:VEVENT
  684. END:VCALENDAR
  685. EOD;
  686. $uris[] = static::getUniqueID('calobj');
  687. $calData[] = <<<EOD
  688. BEGIN:VCALENDAR
  689. VERSION:2.0
  690. PRODID:Nextcloud Calendar
  691. BEGIN:VEVENT
  692. CREATED;VALUE=DATE-TIME:20130910T125139Z
  693. UID:47d15e3ec8-3
  694. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  695. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  696. SUMMARY:123
  697. ATTENDEE;CN=test:mailto:foo@bar.com
  698. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  699. DTEND;VALUE=DATE-TIME:20130912T140000Z
  700. CLASS:PRIVATE
  701. END:VEVENT
  702. END:VCALENDAR
  703. EOD;
  704. $uris[] = static::getUniqueID('calobj');
  705. $calData[] = <<<EOD
  706. BEGIN:VCALENDAR
  707. VERSION:2.0
  708. PRODID:Nextcloud Calendar
  709. BEGIN:VEVENT
  710. CREATED;VALUE=DATE-TIME:20130910T125139Z
  711. UID:47d15e3ec8-4
  712. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  713. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  714. SUMMARY:123
  715. ATTENDEE;CN=foobar:mailto:test@bar.com
  716. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  717. DTEND;VALUE=DATE-TIME:20130912T140000Z
  718. CLASS:CONFIDENTIAL
  719. END:VEVENT
  720. END:VCALENDAR
  721. EOD;
  722. $uriCount = count($uris);
  723. for ($i=0; $i < $uriCount; $i++) {
  724. $this->backend->createCalendarObject($calendarId,
  725. $uris[$i], $calData[$i]);
  726. }
  727. $calendarInfo = [
  728. 'id' => $calendarId,
  729. 'principaluri' => 'user1',
  730. '{http://owncloud.org/ns}owner-principal' => $isShared ? 'user2' : 'user1',
  731. ];
  732. $result = $this->backend->search($calendarInfo, 'Test',
  733. ['SUMMARY', 'LOCATION', 'ATTENDEE'], [], null, null);
  734. $this->assertCount($count, $result);
  735. }
  736. public function searchDataProvider() {
  737. return [
  738. [false, 4],
  739. [true, 2],
  740. ];
  741. }
  742. }