1
0

CalDavBackendTest.php 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. * @copyright Copyright (c) 2017 Georg Ehrke
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author dartcafe <github@dartcafe.de>
  8. * @author Georg Ehrke <oc.list@georgehrke.com>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author leith abdulla <online-nextcloud@eleith.com>
  11. * @author Morris Jobke <hey@morrisjobke.de>
  12. * @author Robin Appelman <robin@icewind.nl>
  13. * @author Roeland Jago Douma <roeland@famdouma.nl>
  14. * @author Thomas Citharel <nextcloud@tcit.fr>
  15. * @author Thomas Müller <thomas.mueller@tmit.eu>
  16. *
  17. * @license AGPL-3.0
  18. *
  19. * This code is free software: you can redistribute it and/or modify
  20. * it under the terms of the GNU Affero General Public License, version 3,
  21. * as published by the Free Software Foundation.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU Affero General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU Affero General Public License, version 3,
  29. * along with this program. If not, see <http://www.gnu.org/licenses/>
  30. *
  31. */
  32. namespace OCA\DAV\Tests\unit\CalDAV;
  33. use DateInterval;
  34. use DateTime;
  35. use DateTimeImmutable;
  36. use DateTimeZone;
  37. use OCA\DAV\CalDAV\CalDavBackend;
  38. use OCA\DAV\CalDAV\Calendar;
  39. use OCA\DAV\DAV\Sharing\Plugin as SharingPlugin;
  40. use OCA\DAV\Events\CalendarDeletedEvent;
  41. use OCP\IConfig;
  42. use OCP\IL10N;
  43. use Sabre\DAV\Exception\NotFound;
  44. use Sabre\DAV\PropPatch;
  45. use Sabre\DAV\Xml\Property\Href;
  46. use Sabre\DAVACL\IACL;
  47. /**
  48. * Class CalDavBackendTest
  49. *
  50. * @group DB
  51. *
  52. * @package OCA\DAV\Tests\unit\CalDAV
  53. */
  54. class CalDavBackendTest extends AbstractCalDavBackend {
  55. public function testCalendarOperations(): void {
  56. $calendarId = $this->createTestCalendar();
  57. // update its display name
  58. $patch = new PropPatch([
  59. '{DAV:}displayname' => 'Unit test',
  60. '{urn:ietf:params:xml:ns:caldav}calendar-description' => 'Calendar used for unit testing'
  61. ]);
  62. $this->dispatcher->expects(self::atLeastOnce())
  63. ->method('dispatchTyped');
  64. $this->backend->updateCalendar($calendarId, $patch);
  65. $patch->commit();
  66. $this->assertEquals(1, $this->backend->getCalendarsForUserCount(self::UNIT_TEST_USER));
  67. $calendars = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER);
  68. $this->assertCount(1, $calendars);
  69. $this->assertEquals('Unit test', $calendars[0]['{DAV:}displayname']);
  70. $this->assertEquals('Calendar used for unit testing', $calendars[0]['{urn:ietf:params:xml:ns:caldav}calendar-description']);
  71. $this->assertEquals('User\'s displayname', $calendars[0]['{http://nextcloud.com/ns}owner-displayname']);
  72. // delete the address book
  73. $this->dispatcher->expects(self::atLeastOnce())
  74. ->method('dispatchTyped');
  75. $this->backend->deleteCalendar($calendars[0]['id'], true);
  76. $calendars = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER);
  77. self::assertEmpty($calendars);
  78. }
  79. public function providesSharingData() {
  80. return [
  81. [true, true, true, false, [
  82. [
  83. 'href' => 'principal:' . self::UNIT_TEST_USER1,
  84. 'readOnly' => false
  85. ],
  86. [
  87. 'href' => 'principal:' . self::UNIT_TEST_GROUP,
  88. 'readOnly' => true
  89. ]
  90. ], [
  91. self::UNIT_TEST_USER1,
  92. self::UNIT_TEST_GROUP,
  93. ]],
  94. [true, true, true, false, [
  95. [
  96. 'href' => 'principal:' . self::UNIT_TEST_GROUP,
  97. 'readOnly' => true,
  98. ],
  99. [
  100. 'href' => 'principal:' . self::UNIT_TEST_GROUP2,
  101. 'readOnly' => false,
  102. ],
  103. ], [
  104. self::UNIT_TEST_GROUP,
  105. self::UNIT_TEST_GROUP2,
  106. ]],
  107. [true, true, true, true, [
  108. [
  109. 'href' => 'principal:' . self::UNIT_TEST_GROUP,
  110. 'readOnly' => false,
  111. ],
  112. [
  113. 'href' => 'principal:' . self::UNIT_TEST_GROUP2,
  114. 'readOnly' => true,
  115. ],
  116. ], [
  117. self::UNIT_TEST_GROUP,
  118. self::UNIT_TEST_GROUP2,
  119. ]],
  120. [true, false, false, false, [
  121. [
  122. 'href' => 'principal:' . self::UNIT_TEST_USER1,
  123. 'readOnly' => true
  124. ],
  125. ], [
  126. self::UNIT_TEST_USER1,
  127. ]],
  128. ];
  129. }
  130. /**
  131. * @dataProvider providesSharingData
  132. */
  133. public function testCalendarSharing($userCanRead, $userCanWrite, $groupCanRead, $groupCanWrite, $add, $principals): void {
  134. $logger = $this->createMock(\Psr\Log\LoggerInterface::class);
  135. $config = $this->createMock(IConfig::class);
  136. /** @var IL10N|MockObject $l10n */
  137. $l10n = $this->createMock(IL10N::class);
  138. $l10n->expects($this->any())
  139. ->method('t')
  140. ->willReturnCallback(function ($text, $parameters = []) {
  141. return vsprintf($text, $parameters);
  142. });
  143. $this->userManager->expects($this->any())
  144. ->method('userExists')
  145. ->willReturn(true);
  146. $this->groupManager->expects($this->any())
  147. ->method('groupExists')
  148. ->willReturn(true);
  149. $this->principal->expects(self::atLeastOnce())
  150. ->method('findByUri')
  151. ->willReturnOnConsecutiveCalls(...$principals);
  152. $calendarId = $this->createTestCalendar();
  153. $calendars = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER);
  154. $this->assertCount(1, $calendars);
  155. $calendar = new Calendar($this->backend, $calendars[0], $l10n, $config, $logger);
  156. $this->backend->updateShares($calendar, $add, []);
  157. $calendars = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER1);
  158. $this->assertCount(1, $calendars);
  159. $calendar = new Calendar($this->backend, $calendars[0], $l10n, $config, $logger);
  160. $acl = $calendar->getACL();
  161. $this->assertAcl(self::UNIT_TEST_USER, '{DAV:}read', $acl);
  162. $this->assertAcl(self::UNIT_TEST_USER, '{DAV:}write', $acl);
  163. $this->assertAccess($userCanRead, self::UNIT_TEST_USER1, '{DAV:}read', $acl);
  164. $this->assertAccess($userCanWrite, self::UNIT_TEST_USER1, '{DAV:}write', $acl);
  165. $this->assertEquals(self::UNIT_TEST_USER, $calendar->getOwner());
  166. // test acls on the child
  167. $uri = static::getUniqueID('calobj');
  168. $calData = <<<'EOD'
  169. BEGIN:VCALENDAR
  170. VERSION:2.0
  171. PRODID:ownCloud Calendar
  172. BEGIN:VEVENT
  173. CREATED;VALUE=DATE-TIME:20130910T125139Z
  174. UID:47d15e3ec8
  175. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  176. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  177. SUMMARY:Test Event
  178. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  179. DTEND;VALUE=DATE-TIME:20130912T140000Z
  180. CLASS:PUBLIC
  181. END:VEVENT
  182. END:VCALENDAR
  183. EOD;
  184. $this->dispatcher->expects(self::atLeastOnce())
  185. ->method('dispatchTyped');
  186. $this->backend->createCalendarObject($calendarId, $uri, $calData);
  187. /** @var IACL $child */
  188. $child = $calendar->getChild($uri);
  189. $acl = $child->getACL();
  190. $this->assertAcl(self::UNIT_TEST_USER, '{DAV:}read', $acl);
  191. $this->assertAcl(self::UNIT_TEST_USER, '{DAV:}write', $acl);
  192. $this->assertAccess($userCanRead, self::UNIT_TEST_USER1, '{DAV:}read', $acl);
  193. $this->assertAccess($userCanWrite, self::UNIT_TEST_USER1, '{DAV:}write', $acl);
  194. // delete the calendar
  195. $this->dispatcher->expects(self::once())
  196. ->method('dispatchTyped')
  197. ->with(self::callback(function ($event) {
  198. return $event instanceof CalendarDeletedEvent;
  199. }));
  200. $this->backend->deleteCalendar($calendars[0]['id'], true);
  201. $calendars = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER);
  202. self::assertEmpty($calendars);
  203. }
  204. public function testCalendarObjectsOperations(): void {
  205. $calendarId = $this->createTestCalendar();
  206. // create a card
  207. $uri = static::getUniqueID('calobj');
  208. $calData = <<<'EOD'
  209. BEGIN:VCALENDAR
  210. VERSION:2.0
  211. PRODID:ownCloud Calendar
  212. BEGIN:VEVENT
  213. CREATED;VALUE=DATE-TIME:20130910T125139Z
  214. UID:47d15e3ec8
  215. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  216. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  217. SUMMARY:Test Event
  218. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  219. DTEND;VALUE=DATE-TIME:20130912T140000Z
  220. CLASS:PUBLIC
  221. END:VEVENT
  222. END:VCALENDAR
  223. EOD;
  224. $this->dispatcher->expects(self::atLeastOnce())
  225. ->method('dispatchTyped');
  226. $this->backend->createCalendarObject($calendarId, $uri, $calData);
  227. // get all the calendar objects
  228. $calendarObjects = $this->backend->getCalendarObjects($calendarId);
  229. $this->assertCount(1, $calendarObjects);
  230. $this->assertEquals($calendarId, $calendarObjects[0]['calendarid']);
  231. $this->assertArrayHasKey('classification', $calendarObjects[0]);
  232. // get the calendar objects
  233. $calendarObject = $this->backend->getCalendarObject($calendarId, $uri);
  234. $this->assertNotNull($calendarObject);
  235. $this->assertArrayHasKey('id', $calendarObject);
  236. $this->assertArrayHasKey('uri', $calendarObject);
  237. $this->assertArrayHasKey('lastmodified', $calendarObject);
  238. $this->assertArrayHasKey('etag', $calendarObject);
  239. $this->assertArrayHasKey('size', $calendarObject);
  240. $this->assertArrayHasKey('classification', $calendarObject);
  241. $this->assertArrayHasKey('{' . SharingPlugin::NS_NEXTCLOUD . '}deleted-at', $calendarObject);
  242. $this->assertEquals($calData, $calendarObject['calendardata']);
  243. // update the card
  244. $calData = <<<'EOD'
  245. BEGIN:VCALENDAR
  246. VERSION:2.0
  247. PRODID:ownCloud Calendar
  248. BEGIN:VEVENT
  249. CREATED;VALUE=DATE-TIME:20130910T125139Z
  250. UID:47d15e3ec8
  251. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  252. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  253. SUMMARY:Test Event
  254. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  255. DTEND;VALUE=DATE-TIME:20130912T140000Z
  256. END:VEVENT
  257. END:VCALENDAR
  258. EOD;
  259. $this->dispatcher->expects(self::atLeastOnce())
  260. ->method('dispatchTyped');
  261. $this->backend->updateCalendarObject($calendarId, $uri, $calData);
  262. $calendarObject = $this->backend->getCalendarObject($calendarId, $uri);
  263. $this->assertEquals($calData, $calendarObject['calendardata']);
  264. // delete the card
  265. $this->dispatcher->expects(self::atLeastOnce())
  266. ->method('dispatchTyped');
  267. $this->backend->deleteCalendarObject($calendarId, $uri);
  268. $calendarObjects = $this->backend->getCalendarObjects($calendarId);
  269. $this->assertCount(0, $calendarObjects);
  270. }
  271. public function testMultipleCalendarObjectsWithSameUID(): void {
  272. $this->expectException(\Sabre\DAV\Exception\BadRequest::class);
  273. $this->expectExceptionMessage('Calendar object with uid already exists in this calendar collection.');
  274. $calendarId = $this->createTestCalendar();
  275. $calData = <<<'EOD'
  276. BEGIN:VCALENDAR
  277. VERSION:2.0
  278. PRODID:ownCloud Calendar
  279. BEGIN:VEVENT
  280. CREATED;VALUE=DATE-TIME:20130910T125139Z
  281. UID:47d15e3ec8-1
  282. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  283. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  284. SUMMARY:Test Event
  285. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  286. DTEND;VALUE=DATE-TIME:20130912T140000Z
  287. CLASS:PUBLIC
  288. END:VEVENT
  289. END:VCALENDAR
  290. EOD;
  291. $uri0 = static::getUniqueID('event');
  292. $uri1 = static::getUniqueID('event');
  293. $this->backend->createCalendarObject($calendarId, $uri0, $calData);
  294. $this->backend->createCalendarObject($calendarId, $uri1, $calData);
  295. }
  296. public function testMultiCalendarObjects(): void {
  297. $calendarId = $this->createTestCalendar();
  298. // create an event
  299. $calData = [];
  300. $calData[] = <<<'EOD'
  301. BEGIN:VCALENDAR
  302. VERSION:2.0
  303. PRODID:ownCloud Calendar
  304. BEGIN:VEVENT
  305. CREATED;VALUE=DATE-TIME:20130910T125139Z
  306. UID:47d15e3ec8-1
  307. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  308. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  309. SUMMARY:Test Event
  310. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  311. DTEND;VALUE=DATE-TIME:20130912T140000Z
  312. CLASS:PUBLIC
  313. END:VEVENT
  314. END:VCALENDAR
  315. EOD;
  316. $calData[] = <<<'EOD'
  317. BEGIN:VCALENDAR
  318. VERSION:2.0
  319. PRODID:ownCloud Calendar
  320. BEGIN:VEVENT
  321. CREATED;VALUE=DATE-TIME:20130910T125139Z
  322. UID:47d15e3ec8-2
  323. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  324. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  325. SUMMARY:Test Event
  326. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  327. DTEND;VALUE=DATE-TIME:20130912T140000Z
  328. CLASS:PUBLIC
  329. END:VEVENT
  330. END:VCALENDAR
  331. EOD;
  332. $calData[] = <<<'EOD'
  333. BEGIN:VCALENDAR
  334. VERSION:2.0
  335. PRODID:ownCloud Calendar
  336. BEGIN:VEVENT
  337. CREATED;VALUE=DATE-TIME:20130910T125139Z
  338. UID:47d15e3ec8-3
  339. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  340. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  341. SUMMARY:Test Event
  342. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  343. DTEND;VALUE=DATE-TIME:20130912T140000Z
  344. CLASS:PUBLIC
  345. END:VEVENT
  346. END:VCALENDAR
  347. EOD;
  348. $uri0 = static::getUniqueID('card');
  349. $this->dispatcher->expects(self::atLeastOnce())
  350. ->method('dispatchTyped');
  351. $this->backend->createCalendarObject($calendarId, $uri0, $calData[0]);
  352. $uri1 = static::getUniqueID('card');
  353. $this->dispatcher->expects(self::atLeastOnce())
  354. ->method('dispatchTyped');
  355. $this->backend->createCalendarObject($calendarId, $uri1, $calData[1]);
  356. $uri2 = static::getUniqueID('card');
  357. $this->dispatcher->expects(self::atLeastOnce())
  358. ->method('dispatchTyped');
  359. $this->backend->createCalendarObject($calendarId, $uri2, $calData[2]);
  360. // get all the cards
  361. $calendarObjects = $this->backend->getCalendarObjects($calendarId);
  362. $this->assertCount(3, $calendarObjects);
  363. // get the cards
  364. $calendarObjects = $this->backend->getMultipleCalendarObjects($calendarId, [$uri1, $uri2]);
  365. $this->assertCount(2, $calendarObjects);
  366. foreach ($calendarObjects as $card) {
  367. $this->assertArrayHasKey('id', $card);
  368. $this->assertArrayHasKey('uri', $card);
  369. $this->assertArrayHasKey('lastmodified', $card);
  370. $this->assertArrayHasKey('etag', $card);
  371. $this->assertArrayHasKey('size', $card);
  372. $this->assertArrayHasKey('classification', $card);
  373. }
  374. usort($calendarObjects, function ($a, $b) {
  375. return $a['id'] - $b['id'];
  376. });
  377. $this->assertEquals($calData[1], $calendarObjects[0]['calendardata']);
  378. $this->assertEquals($calData[2], $calendarObjects[1]['calendardata']);
  379. // delete the card
  380. $this->dispatcher->expects(self::atLeastOnce())
  381. ->method('dispatchTyped');
  382. $this->backend->deleteCalendarObject($calendarId, $uri0);
  383. $this->dispatcher->expects(self::atLeastOnce())
  384. ->method('dispatchTyped');
  385. $this->backend->deleteCalendarObject($calendarId, $uri1);
  386. $this->dispatcher->expects(self::atLeastOnce())
  387. ->method('dispatchTyped');
  388. $this->backend->deleteCalendarObject($calendarId, $uri2);
  389. $calendarObjects = $this->backend->getCalendarObjects($calendarId);
  390. $this->assertCount(0, $calendarObjects);
  391. }
  392. /**
  393. * @dataProvider providesCalendarQueryParameters
  394. */
  395. public function testCalendarQuery($expectedEventsInResult, $propFilters, $compFilter): void {
  396. $calendarId = $this->createTestCalendar();
  397. $events = [];
  398. $events[0] = $this->createEvent($calendarId, '20130912T130000Z', '20130912T140000Z');
  399. $events[1] = $this->createEvent($calendarId, '20130912T150000Z', '20130912T170000Z');
  400. $events[2] = $this->createEvent($calendarId, '20130912T173000Z', '20130912T220000Z');
  401. if (PHP_INT_SIZE > 8) {
  402. $events[3] = $this->createEvent($calendarId, '21130912T130000Z', '22130912T130000Z');
  403. } else {
  404. /* On 32bit we do not support events after 2038 */
  405. $events[3] = $this->createEvent($calendarId, '20370912T130000Z', '20370912T130000Z');
  406. }
  407. $result = $this->backend->calendarQuery($calendarId, [
  408. 'name' => '',
  409. 'prop-filters' => $propFilters,
  410. 'comp-filters' => $compFilter
  411. ]);
  412. $expectedEventsInResult = array_map(function ($index) use ($events) {
  413. return $events[$index];
  414. }, $expectedEventsInResult);
  415. $this->assertEqualsCanonicalizing($expectedEventsInResult, $result);
  416. }
  417. public function testGetCalendarObjectByUID(): void {
  418. $calendarId = $this->createTestCalendar();
  419. $uri = static::getUniqueID('calobj');
  420. $calData = <<<'EOD'
  421. BEGIN:VCALENDAR
  422. VERSION:2.0
  423. PRODID:ownCloud Calendar
  424. BEGIN:VEVENT
  425. CREATED;VALUE=DATE-TIME:20130910T125139Z
  426. UID:47d15e3ec8
  427. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  428. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  429. SUMMARY:Test Event
  430. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  431. DTEND;VALUE=DATE-TIME:20130912T140000Z
  432. CLASS:PUBLIC
  433. END:VEVENT
  434. END:VCALENDAR
  435. EOD;
  436. $this->backend->createCalendarObject($calendarId, $uri, $calData);
  437. $co = $this->backend->getCalendarObjectByUID(self::UNIT_TEST_USER, '47d15e3ec8');
  438. $this->assertNotNull($co);
  439. }
  440. public function providesCalendarQueryParameters() {
  441. return [
  442. 'all' => [[0, 1, 2, 3], [], []],
  443. 'only-todos' => [[], ['name' => 'VTODO'], []],
  444. 'only-events' => [[0, 1, 2, 3], [], [['name' => 'VEVENT', 'is-not-defined' => false, 'comp-filters' => [], 'time-range' => ['start' => null, 'end' => null], 'prop-filters' => []]],],
  445. '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' => []]],],
  446. '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' => []]],],
  447. 'future' => [[3], [], [['name' => 'VEVENT', 'is-not-defined' => false, 'comp-filters' => [], 'time-range' => ['start' => new DateTime('2036-09-12 14:00:00', new DateTimeZone('UTC')), 'end' => null], 'prop-filters' => []]],],
  448. ];
  449. }
  450. public function testSyncSupport(): void {
  451. $calendarId = $this->createTestCalendar();
  452. // fist call without synctoken
  453. $changes = $this->backend->getChangesForCalendar($calendarId, '', 1);
  454. $syncToken = $changes['syncToken'];
  455. // add a change
  456. $event = $this->createEvent($calendarId, '20130912T130000Z', '20130912T140000Z');
  457. // look for changes
  458. $changes = $this->backend->getChangesForCalendar($calendarId, $syncToken, 1);
  459. $this->assertEquals($event, $changes['added'][0]);
  460. }
  461. public function testPublications(): void {
  462. $this->dispatcher->expects(self::atLeastOnce())
  463. ->method('dispatchTyped');
  464. $this->backend->createCalendar(self::UNIT_TEST_USER, 'Example', []);
  465. $calendarInfo = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER)[0];
  466. /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject $l10n */
  467. $l10n = $this->createMock(IL10N::class);
  468. $config = $this->createMock(IConfig::class);
  469. $logger = $this->createMock(\Psr\Log\LoggerInterface::class);
  470. $calendar = new Calendar($this->backend, $calendarInfo, $l10n, $config, $logger);
  471. $calendar->setPublishStatus(true);
  472. $this->assertNotEquals(false, $calendar->getPublishStatus());
  473. $publicCalendars = $this->backend->getPublicCalendars();
  474. $this->assertCount(1, $publicCalendars);
  475. $this->assertEquals(true, $publicCalendars[0]['{http://owncloud.org/ns}public']);
  476. $this->assertEquals('User\'s displayname', $publicCalendars[0]['{http://nextcloud.com/ns}owner-displayname']);
  477. $publicCalendarURI = $publicCalendars[0]['uri'];
  478. $publicCalendar = $this->backend->getPublicCalendar($publicCalendarURI);
  479. $this->assertEquals(true, $publicCalendar['{http://owncloud.org/ns}public']);
  480. $calendar->setPublishStatus(false);
  481. $this->assertEquals(false, $calendar->getPublishStatus());
  482. $this->expectException(NotFound::class);
  483. $this->backend->getPublicCalendar($publicCalendarURI);
  484. }
  485. public function testSubscriptions(): void {
  486. $id = $this->backend->createSubscription(self::UNIT_TEST_USER, 'Subscription', [
  487. '{http://calendarserver.org/ns/}source' => new Href('test-source'),
  488. '{http://apple.com/ns/ical/}calendar-color' => '#1C4587',
  489. '{http://calendarserver.org/ns/}subscribed-strip-todos' => ''
  490. ]);
  491. $subscriptions = $this->backend->getSubscriptionsForUser(self::UNIT_TEST_USER);
  492. $this->assertCount(1, $subscriptions);
  493. $this->assertEquals('#1C4587', $subscriptions[0]['{http://apple.com/ns/ical/}calendar-color']);
  494. $this->assertEquals(true, $subscriptions[0]['{http://calendarserver.org/ns/}subscribed-strip-todos']);
  495. $this->assertEquals($id, $subscriptions[0]['id']);
  496. $patch = new PropPatch([
  497. '{DAV:}displayname' => 'Unit test',
  498. '{http://apple.com/ns/ical/}calendar-color' => '#ac0606',
  499. ]);
  500. $this->backend->updateSubscription($id, $patch);
  501. $patch->commit();
  502. $subscriptions = $this->backend->getSubscriptionsForUser(self::UNIT_TEST_USER);
  503. $this->assertCount(1, $subscriptions);
  504. $this->assertEquals($id, $subscriptions[0]['id']);
  505. $this->assertEquals('Unit test', $subscriptions[0]['{DAV:}displayname']);
  506. $this->assertEquals('#ac0606', $subscriptions[0]['{http://apple.com/ns/ical/}calendar-color']);
  507. $this->backend->deleteSubscription($id);
  508. $subscriptions = $this->backend->getSubscriptionsForUser(self::UNIT_TEST_USER);
  509. $this->assertCount(0, $subscriptions);
  510. }
  511. public function providesSchedulingData() {
  512. $data = <<<EOS
  513. BEGIN:VCALENDAR
  514. VERSION:2.0
  515. PRODID:-//Sabre//Sabre VObject 3.5.0//EN
  516. CALSCALE:GREGORIAN
  517. METHOD:REQUEST
  518. BEGIN:VTIMEZONE
  519. TZID:Europe/Warsaw
  520. BEGIN:DAYLIGHT
  521. TZOFFSETFROM:+0100
  522. TZOFFSETTO:+0200
  523. TZNAME:CEST
  524. DTSTART:19700329T020000
  525. RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=3
  526. END:DAYLIGHT
  527. BEGIN:STANDARD
  528. TZOFFSETFROM:+0200
  529. TZOFFSETTO:+0100
  530. TZNAME:CET
  531. DTSTART:19701025T030000
  532. RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10
  533. END:STANDARD
  534. END:VTIMEZONE
  535. BEGIN:VEVENT
  536. CREATED:20170320T131655Z
  537. LAST-MODIFIED:20170320T135019Z
  538. DTSTAMP:20170320T135019Z
  539. UID:7e908a6d-4c4e-48d7-bd62-59ab80fbf1a3
  540. SUMMARY:TEST Z pg_escape_bytea
  541. ORGANIZER;RSVP=TRUE;PARTSTAT=ACCEPTED;ROLE=CHAIR:mailto:k.klimczak@gromar.e
  542. u
  543. ATTENDEE;RSVP=TRUE;CN=Zuzanna Leszek;PARTSTAT=NEEDS-ACTION;ROLE=REQ-PARTICI
  544. PANT:mailto:z.leszek@gromar.eu
  545. ATTENDEE;RSVP=TRUE;CN=Marcin Pisarski;PARTSTAT=NEEDS-ACTION;ROLE=REQ-PARTIC
  546. IPANT:mailto:m.pisarski@gromar.eu
  547. ATTENDEE;RSVP=TRUE;PARTSTAT=NEEDS-ACTION;ROLE=REQ-PARTICIPANT:mailto:klimcz
  548. ak.k@gmail.com
  549. ATTENDEE;RSVP=TRUE;PARTSTAT=NEEDS-ACTION;ROLE=REQ-PARTICIPANT:mailto:k_klim
  550. czak@tlen.pl
  551. DTSTART;TZID=Europe/Warsaw:20170325T150000
  552. DTEND;TZID=Europe/Warsaw:20170325T160000
  553. TRANSP:OPAQUE
  554. DESCRIPTION:Magiczna treść uzyskana za pomocą magicznego proszku.\n\nę
  555. żźćńłóÓŻŹĆŁĘ€śśśŚŚ\n \,\,))))))))\;\,\n
  556. __))))))))))))))\,\n \\|/ -\\(((((''''((((((((.\n -*-==///
  557. ///(('' . `))))))\,\n /|\\ ))| o \;-. '(((((
  558. \,(\,\n ( `| / ) \;))))'
  559. \,_))^\;(~\n | | | \,))((((_ _____-
  560. -----~~~-. %\,\;(\;(>'\;'~\n o_)\; \; )))(((` ~---
  561. ~ `:: \\ %%~~)(v\;(`('~\n \; ''''````
  562. `: `:::|\\\,__\,%% )\;`'\; ~\n | _
  563. ) / `:|`----' `-'\n ______/\\/~ |
  564. / /\n /~\;\;.____/\;\;' / ___--\
  565. ,-( `\;\;\;/\n / // _\;______\;'------~~~~~ /\;\;/\\ /\n
  566. // | | / \; \\\;\;\,\\\n (<_ | \;
  567. /'\,/-----' _>\n \\_| ||_
  568. //~\;~~~~~~~~~\n `\\_| (\,~~ -Tua Xiong\n
  569. \\~\\\n
  570. ~~\n\n
  571. SEQUENCE:1
  572. X-MOZ-GENERATION:1
  573. END:VEVENT
  574. END:VCALENDAR
  575. EOS;
  576. return [
  577. 'no data' => [''],
  578. 'failing on postgres' => [$data]
  579. ];
  580. }
  581. /**
  582. * @dataProvider providesSchedulingData
  583. * @param $objectData
  584. */
  585. public function testScheduling($objectData): void {
  586. $this->backend->createSchedulingObject(self::UNIT_TEST_USER, 'Sample Schedule', $objectData);
  587. $sos = $this->backend->getSchedulingObjects(self::UNIT_TEST_USER);
  588. $this->assertCount(1, $sos);
  589. $so = $this->backend->getSchedulingObject(self::UNIT_TEST_USER, 'Sample Schedule');
  590. $this->assertNotNull($so);
  591. $this->backend->deleteSchedulingObject(self::UNIT_TEST_USER, 'Sample Schedule');
  592. $sos = $this->backend->getSchedulingObjects(self::UNIT_TEST_USER);
  593. $this->assertCount(0, $sos);
  594. }
  595. /**
  596. * @dataProvider providesCalDataForGetDenormalizedData
  597. */
  598. public function testGetDenormalizedData($expected, $key, $calData): void {
  599. try {
  600. $actual = $this->backend->getDenormalizedData($calData);
  601. $this->assertEquals($expected, $actual[$key]);
  602. } catch (\Throwable $e) {
  603. if (($e->getMessage() === 'Epoch doesn\'t fit in a PHP integer') && (PHP_INT_SIZE < 8)) {
  604. $this->markTestSkipped('This fail on 32bits because of PHP limitations in DateTime');
  605. }
  606. throw $e;
  607. }
  608. }
  609. public function providesCalDataForGetDenormalizedData(): array {
  610. return [
  611. '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"],
  612. '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"],
  613. 'last occurrence is max when only last VEVENT in group is weekly' => [(new DateTime(CalDavBackend::MAX_DATE))->getTimestamp(), 'lastOccurence', "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Sabre//Sabre VObject 4.3.0//EN\r\nCALSCALE:GREGORIAN\r\nBEGIN:VEVENT\r\nDTSTART;TZID=America/Los_Angeles:20200812T103000\r\nDTEND;TZID=America/Los_Angeles:20200812T110000\r\nDTSTAMP:20200927T180638Z\r\nUID:asdfasdfasdf@google.com\r\nRECURRENCE-ID;TZID=America/Los_Angeles:20200811T123000\r\nCREATED:20200626T181848Z\r\nLAST-MODIFIED:20200922T192707Z\r\nSUMMARY:Weekly 1:1\r\nTRANSP:OPAQUE\r\nEND:VEVENT\r\nBEGIN:VEVENT\r\nDTSTART;TZID=America/Los_Angeles:20200728T123000\r\nDTEND;TZID=America/Los_Angeles:20200728T130000\r\nEXDATE;TZID=America/Los_Angeles:20200818T123000\r\nRRULE:FREQ=WEEKLY;BYDAY=TU\r\nDTSTAMP:20200927T180638Z\r\nUID:asdfasdfasdf@google.com\r\nCREATED:20200626T181848Z\r\nDESCRIPTION:Setting up recurring time on our calendars\r\nLAST-MODIFIED:20200922T192707Z\r\nSUMMARY:Weekly 1:1\r\nTRANSP:OPAQUE\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n"],
  614. 'last occurrence before unix epoch starts' => [0, 'lastOccurence', "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Sabre//Sabre VObject 4.3.0//EN\r\nCALSCALE:GREGORIAN\r\nBEGIN:VEVENT\r\nDTSTART;VALUE=DATE:19110324\r\nDTEND;VALUE=DATE:19110325\r\nDTSTAMP:20200927T180638Z\r\nUID:asdfasdfasdf@google.com\r\nCREATED:20200626T181848Z\r\nDESCRIPTION:Very old event\r\nLAST-MODIFIED:20200922T192707Z\r\nSUMMARY:Some old event\r\nTRANSP:OPAQUE\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n"],
  615. 'first occurrence is found when not first VEVENT in group' => [(new DateTime('2020-09-01T110000', new DateTimeZone("America/Los_Angeles")))->getTimestamp(), 'firstOccurence', "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Sabre//Sabre VObject 4.3.0//EN\r\nCALSCALE:GREGORIAN\r\nBEGIN:VEVENT\r\nDTSTART;TZID=America/Los_Angeles:20201013T110000\r\nDTEND;TZID=America/Los_Angeles:20201013T120000\r\nDTSTAMP:20200927T180638Z\r\nUID:asdf0000@google.com\r\nRECURRENCE-ID;TZID=America/Los_Angeles:20201013T110000\r\nCREATED:20160330T034726Z\r\nLAST-MODIFIED:20200925T042014Z\r\nSTATUS:CONFIRMED\r\nTRANSP:OPAQUE\r\nEND:VEVENT\r\nBEGIN:VEVENT\r\nDTSTART;TZID=America/Los_Angeles:20200901T110000\r\nDTEND;TZID=America/Los_Angeles:20200901T120000\r\nRRULE:FREQ=WEEKLY;BYDAY=TU\r\nEXDATE;TZID=America/Los_Angeles:20200922T110000\r\nEXDATE;TZID=America/Los_Angeles:20200915T110000\r\nEXDATE;TZID=America/Los_Angeles:20200908T110000\r\nDTSTAMP:20200927T180638Z\r\nUID:asdf0000@google.com\r\nCREATED:20160330T034726Z\r\nLAST-MODIFIED:20200915T162810Z\r\nSTATUS:CONFIRMED\r\nTRANSP:OPAQUE\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n"],
  616. '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"],
  617. '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"],
  618. '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"],
  619. '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"],
  620. '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"],
  621. ];
  622. }
  623. public function testCalendarSearch(): void {
  624. $calendarId = $this->createTestCalendar();
  625. $uri = static::getUniqueID('calobj');
  626. $calData = <<<EOD
  627. BEGIN:VCALENDAR
  628. VERSION:2.0
  629. PRODID:ownCloud Calendar
  630. BEGIN:VEVENT
  631. CREATED;VALUE=DATE-TIME:20130910T125139Z
  632. UID:47d15e3ec8
  633. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  634. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  635. SUMMARY:Test Event
  636. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  637. DTEND;VALUE=DATE-TIME:20130912T140000Z
  638. CLASS:PUBLIC
  639. END:VEVENT
  640. END:VCALENDAR
  641. EOD;
  642. $this->backend->createCalendarObject($calendarId, $uri, $calData);
  643. $search1 = $this->backend->calendarSearch(self::UNIT_TEST_USER, [
  644. 'comps' => [
  645. 'VEVENT',
  646. 'VTODO'
  647. ],
  648. 'props' => [
  649. 'SUMMARY',
  650. 'LOCATION'
  651. ],
  652. 'search-term' => 'Test',
  653. ]);
  654. $this->assertEquals(count($search1), 1);
  655. // update the card
  656. $calData = <<<'EOD'
  657. BEGIN:VCALENDAR
  658. VERSION:2.0
  659. PRODID:ownCloud Calendar
  660. BEGIN:VEVENT
  661. CREATED;VALUE=DATE-TIME:20130910T125139Z
  662. UID:47d15e3ec8
  663. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  664. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  665. SUMMARY:123 Event 🙈
  666. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  667. DTEND;VALUE=DATE-TIME:20130912T140000Z
  668. ATTENDEE;CN=test:mailto:foo@bar.com
  669. END:VEVENT
  670. END:VCALENDAR
  671. EOD;
  672. $this->backend->updateCalendarObject($calendarId, $uri, $calData);
  673. $search2 = $this->backend->calendarSearch(self::UNIT_TEST_USER, [
  674. 'comps' => [
  675. 'VEVENT',
  676. 'VTODO'
  677. ],
  678. 'props' => [
  679. 'SUMMARY',
  680. 'LOCATION'
  681. ],
  682. 'search-term' => 'Test',
  683. ]);
  684. $this->assertEquals(count($search2), 0);
  685. $search3 = $this->backend->calendarSearch(self::UNIT_TEST_USER, [
  686. 'comps' => [
  687. 'VEVENT',
  688. 'VTODO'
  689. ],
  690. 'props' => [
  691. 'SUMMARY',
  692. 'LOCATION'
  693. ],
  694. 'params' => [
  695. [
  696. 'property' => 'ATTENDEE',
  697. 'parameter' => 'CN'
  698. ]
  699. ],
  700. 'search-term' => 'Test',
  701. ]);
  702. $this->assertEquals(count($search3), 1);
  703. // t matches both summary and attendee's CN, but we want unique results
  704. $search4 = $this->backend->calendarSearch(self::UNIT_TEST_USER, [
  705. 'comps' => [
  706. 'VEVENT',
  707. 'VTODO'
  708. ],
  709. 'props' => [
  710. 'SUMMARY',
  711. 'LOCATION'
  712. ],
  713. 'params' => [
  714. [
  715. 'property' => 'ATTENDEE',
  716. 'parameter' => 'CN'
  717. ]
  718. ],
  719. 'search-term' => 't',
  720. ]);
  721. $this->assertEquals(count($search4), 1);
  722. $this->backend->deleteCalendarObject($calendarId, $uri);
  723. $search5 = $this->backend->calendarSearch(self::UNIT_TEST_USER, [
  724. 'comps' => [
  725. 'VEVENT',
  726. 'VTODO'
  727. ],
  728. 'props' => [
  729. 'SUMMARY',
  730. 'LOCATION'
  731. ],
  732. 'params' => [
  733. [
  734. 'property' => 'ATTENDEE',
  735. 'parameter' => 'CN'
  736. ]
  737. ],
  738. 'search-term' => 't',
  739. ]);
  740. $this->assertEquals(count($search5), 0);
  741. }
  742. /**
  743. * @dataProvider searchDataProvider
  744. */
  745. public function testSearch(bool $isShared, array $searchOptions, int $count): void {
  746. $calendarId = $this->createTestCalendar();
  747. $uris = [];
  748. $calData = [];
  749. $uris[] = static::getUniqueID('calobj');
  750. $calData[] = <<<EOD
  751. BEGIN:VCALENDAR
  752. VERSION:2.0
  753. PRODID:Nextcloud Calendar
  754. BEGIN:VEVENT
  755. CREATED;VALUE=DATE-TIME:20130910T125139Z
  756. UID:47d15e3ec8-1
  757. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  758. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  759. SUMMARY:Test Event
  760. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  761. DTEND;VALUE=DATE-TIME:20130912T140000Z
  762. CLASS:PUBLIC
  763. END:VEVENT
  764. END:VCALENDAR
  765. EOD;
  766. $uris[] = static::getUniqueID('calobj');
  767. $calData[] = <<<EOD
  768. BEGIN:VCALENDAR
  769. VERSION:2.0
  770. PRODID:Nextcloud Calendar
  771. BEGIN:VEVENT
  772. CREATED;VALUE=DATE-TIME:20130910T125139Z
  773. UID:47d15e3ec8-2
  774. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  775. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  776. SUMMARY:123
  777. LOCATION:Test
  778. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  779. DTEND;VALUE=DATE-TIME:20130912T140000Z
  780. CLASS:PUBLIC
  781. END:VEVENT
  782. END:VCALENDAR
  783. EOD;
  784. $uris[] = static::getUniqueID('calobj');
  785. $calData[] = <<<EOD
  786. BEGIN:VCALENDAR
  787. VERSION:2.0
  788. PRODID:Nextcloud Calendar
  789. BEGIN:VEVENT
  790. CREATED;VALUE=DATE-TIME:20130910T125139Z
  791. UID:47d15e3ec8-3
  792. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  793. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  794. SUMMARY:123
  795. ATTENDEE;CN=test:mailto:foo@bar.com
  796. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  797. DTEND;VALUE=DATE-TIME:20130912T140000Z
  798. CLASS:PRIVATE
  799. END:VEVENT
  800. END:VCALENDAR
  801. EOD;
  802. $uris[] = static::getUniqueID('calobj');
  803. $calData[] = <<<EOD
  804. BEGIN:VCALENDAR
  805. VERSION:2.0
  806. PRODID:Nextcloud Calendar
  807. BEGIN:VEVENT
  808. CREATED;VALUE=DATE-TIME:20130910T125139Z
  809. UID:47d15e3ec8-4
  810. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  811. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  812. SUMMARY:123
  813. ATTENDEE;CN=foobar:mailto:test@bar.com
  814. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  815. DTEND;VALUE=DATE-TIME:20130912T140000Z
  816. CLASS:CONFIDENTIAL
  817. END:VEVENT
  818. END:VCALENDAR
  819. EOD;
  820. $uriCount = count($uris);
  821. for ($i = 0; $i < $uriCount; $i++) {
  822. $this->backend->createCalendarObject($calendarId,
  823. $uris[$i], $calData[$i]);
  824. }
  825. $calendarInfo = [
  826. 'id' => $calendarId,
  827. 'principaluri' => 'user1',
  828. '{http://owncloud.org/ns}owner-principal' => $isShared ? 'user2' : 'user1',
  829. ];
  830. $result = $this->backend->search($calendarInfo, 'Test',
  831. ['SUMMARY', 'LOCATION', 'ATTENDEE'], $searchOptions, null, null);
  832. $this->assertCount($count, $result);
  833. }
  834. public function searchDataProvider() {
  835. return [
  836. [false, [], 4],
  837. [true, ['timerange' => ['start' => new DateTime('2013-09-12 13:00:00'), 'end' => new DateTime('2013-09-12 14:00:00')]], 2],
  838. [true, ['timerange' => ['start' => new DateTime('2013-09-12 15:00:00'), 'end' => new DateTime('2013-09-12 16:00:00')]], 0],
  839. ];
  840. }
  841. public function testSameUriSameIdForDifferentCalendarTypes(): void {
  842. $calendarId = $this->createTestCalendar();
  843. $subscriptionId = $this->createTestSubscription();
  844. $uri = static::getUniqueID('calobj');
  845. $calData = <<<EOD
  846. BEGIN:VCALENDAR
  847. VERSION:2.0
  848. PRODID:ownCloud Calendar
  849. BEGIN:VEVENT
  850. CREATED;VALUE=DATE-TIME:20130910T125139Z
  851. UID:47d15e3ec8
  852. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  853. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  854. SUMMARY:Test Event
  855. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  856. DTEND;VALUE=DATE-TIME:20130912T140000Z
  857. CLASS:PUBLIC
  858. END:VEVENT
  859. END:VCALENDAR
  860. EOD;
  861. $calData2 = <<<EOD
  862. BEGIN:VCALENDAR
  863. VERSION:2.0
  864. PRODID:ownCloud Calendar
  865. BEGIN:VEVENT
  866. CREATED;VALUE=DATE-TIME:20130910T125139Z
  867. UID:47d15e3ec8
  868. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  869. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  870. SUMMARY:Test Event 123
  871. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  872. DTEND;VALUE=DATE-TIME:20130912T140000Z
  873. CLASS:PUBLIC
  874. END:VEVENT
  875. END:VCALENDAR
  876. EOD;
  877. $this->backend->createCalendarObject($calendarId, $uri, $calData);
  878. $this->backend->createCalendarObject($subscriptionId, $uri, $calData2, CalDavBackend::CALENDAR_TYPE_SUBSCRIPTION);
  879. $this->assertEquals($calData, $this->backend->getCalendarObject($calendarId, $uri, CalDavBackend::CALENDAR_TYPE_CALENDAR)['calendardata']);
  880. $this->assertEquals($calData2, $this->backend->getCalendarObject($subscriptionId, $uri, CalDavBackend::CALENDAR_TYPE_SUBSCRIPTION)['calendardata']);
  881. }
  882. public function testPurgeAllCachedEventsForSubscription(): void {
  883. $subscriptionId = $this->createTestSubscription();
  884. $uri = static::getUniqueID('calobj');
  885. $calData = <<<EOD
  886. BEGIN:VCALENDAR
  887. VERSION:2.0
  888. PRODID:ownCloud Calendar
  889. BEGIN:VEVENT
  890. CREATED;VALUE=DATE-TIME:20130910T125139Z
  891. UID:47d15e3ec8
  892. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  893. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  894. SUMMARY:Test Event
  895. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  896. DTEND;VALUE=DATE-TIME:20130912T140000Z
  897. CLASS:PUBLIC
  898. END:VEVENT
  899. END:VCALENDAR
  900. EOD;
  901. $this->backend->createCalendarObject($subscriptionId, $uri, $calData, CalDavBackend::CALENDAR_TYPE_SUBSCRIPTION);
  902. $this->backend->purgeAllCachedEventsForSubscription($subscriptionId);
  903. $this->assertEquals(null, $this->backend->getCalendarObject($subscriptionId, $uri, CalDavBackend::CALENDAR_TYPE_SUBSCRIPTION));
  904. }
  905. public function testCalendarMovement(): void {
  906. $this->backend->createCalendar(self::UNIT_TEST_USER, 'Example', []);
  907. $this->assertCount(1, $this->backend->getCalendarsForUser(self::UNIT_TEST_USER));
  908. $calendarInfoUser = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER)[0];
  909. $this->backend->moveCalendar('Example', self::UNIT_TEST_USER, self::UNIT_TEST_USER1);
  910. $this->assertCount(0, $this->backend->getCalendarsForUser(self::UNIT_TEST_USER));
  911. $this->assertCount(1, $this->backend->getCalendarsForUser(self::UNIT_TEST_USER1));
  912. $calendarInfoUser1 = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER1)[0];
  913. $this->assertEquals($calendarInfoUser['id'], $calendarInfoUser1['id']);
  914. $this->assertEquals($calendarInfoUser['uri'], $calendarInfoUser1['uri']);
  915. }
  916. public function testSearchPrincipal(): void {
  917. $myPublic = <<<EOD
  918. BEGIN:VCALENDAR
  919. VERSION:2.0
  920. PRODID:-//dmfs.org//mimedir.icalendar//EN
  921. BEGIN:VTIMEZONE
  922. TZID:Europe/Berlin
  923. X-LIC-LOCATION:Europe/Berlin
  924. BEGIN:DAYLIGHT
  925. TZOFFSETFROM:+0100
  926. TZOFFSETTO:+0200
  927. TZNAME:CEST
  928. DTSTART:19700329T020000
  929. RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
  930. END:DAYLIGHT
  931. BEGIN:STANDARD
  932. TZOFFSETFROM:+0200
  933. TZOFFSETTO:+0100
  934. TZNAME:CET
  935. DTSTART:19701025T030000
  936. RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
  937. END:STANDARD
  938. END:VTIMEZONE
  939. BEGIN:VEVENT
  940. DTSTART;TZID=Europe/Berlin:20160419T130000
  941. SUMMARY:My Test (public)
  942. CLASS:PUBLIC
  943. TRANSP:OPAQUE
  944. STATUS:CONFIRMED
  945. DTEND;TZID=Europe/Berlin:20160419T140000
  946. LAST-MODIFIED:20160419T074202Z
  947. DTSTAMP:20160419T074202Z
  948. CREATED:20160419T074202Z
  949. UID:2e468c48-7860-492e-bc52-92fa0daeeccf.1461051722310-1
  950. END:VEVENT
  951. END:VCALENDAR
  952. EOD;
  953. $myPrivate = <<<EOD
  954. BEGIN:VCALENDAR
  955. VERSION:2.0
  956. PRODID:-//dmfs.org//mimedir.icalendar//EN
  957. BEGIN:VTIMEZONE
  958. TZID:Europe/Berlin
  959. X-LIC-LOCATION:Europe/Berlin
  960. BEGIN:DAYLIGHT
  961. TZOFFSETFROM:+0100
  962. TZOFFSETTO:+0200
  963. TZNAME:CEST
  964. DTSTART:19700329T020000
  965. RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
  966. END:DAYLIGHT
  967. BEGIN:STANDARD
  968. TZOFFSETFROM:+0200
  969. TZOFFSETTO:+0100
  970. TZNAME:CET
  971. DTSTART:19701025T030000
  972. RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
  973. END:STANDARD
  974. END:VTIMEZONE
  975. BEGIN:VEVENT
  976. DTSTART;TZID=Europe/Berlin:20160419T130000
  977. SUMMARY:My Test (private)
  978. CLASS:PRIVATE
  979. TRANSP:OPAQUE
  980. STATUS:CONFIRMED
  981. DTEND;TZID=Europe/Berlin:20160419T140000
  982. LAST-MODIFIED:20160419T074202Z
  983. DTSTAMP:20160419T074202Z
  984. CREATED:20160419T074202Z
  985. UID:2e468c48-7860-492e-bc52-92fa0daeeccf.1461051722310-2
  986. END:VEVENT
  987. END:VCALENDAR
  988. EOD;
  989. $myConfidential = <<<EOD
  990. BEGIN:VCALENDAR
  991. VERSION:2.0
  992. PRODID:-//dmfs.org//mimedir.icalendar//EN
  993. BEGIN:VTIMEZONE
  994. TZID:Europe/Berlin
  995. X-LIC-LOCATION:Europe/Berlin
  996. BEGIN:DAYLIGHT
  997. TZOFFSETFROM:+0100
  998. TZOFFSETTO:+0200
  999. TZNAME:CEST
  1000. DTSTART:19700329T020000
  1001. RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
  1002. END:DAYLIGHT
  1003. BEGIN:STANDARD
  1004. TZOFFSETFROM:+0200
  1005. TZOFFSETTO:+0100
  1006. TZNAME:CET
  1007. DTSTART:19701025T030000
  1008. RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
  1009. END:STANDARD
  1010. END:VTIMEZONE
  1011. BEGIN:VEVENT
  1012. DTSTART;TZID=Europe/Berlin:20160419T130000
  1013. SUMMARY:My Test (confidential)
  1014. CLASS:CONFIDENTIAL
  1015. TRANSP:OPAQUE
  1016. STATUS:CONFIRMED
  1017. DTEND;TZID=Europe/Berlin:20160419T140000
  1018. LAST-MODIFIED:20160419T074202Z
  1019. DTSTAMP:20160419T074202Z
  1020. CREATED:20160419T074202Z
  1021. UID:2e468c48-7860-492e-bc52-92fa0daeeccf.1461051722310-3
  1022. END:VEVENT
  1023. END:VCALENDAR
  1024. EOD;
  1025. $sharerPublic = <<<EOD
  1026. BEGIN:VCALENDAR
  1027. VERSION:2.0
  1028. PRODID:-//dmfs.org//mimedir.icalendar//EN
  1029. BEGIN:VTIMEZONE
  1030. TZID:Europe/Berlin
  1031. X-LIC-LOCATION:Europe/Berlin
  1032. BEGIN:DAYLIGHT
  1033. TZOFFSETFROM:+0100
  1034. TZOFFSETTO:+0200
  1035. TZNAME:CEST
  1036. DTSTART:19700329T020000
  1037. RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
  1038. END:DAYLIGHT
  1039. BEGIN:STANDARD
  1040. TZOFFSETFROM:+0200
  1041. TZOFFSETTO:+0100
  1042. TZNAME:CET
  1043. DTSTART:19701025T030000
  1044. RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
  1045. END:STANDARD
  1046. END:VTIMEZONE
  1047. BEGIN:VEVENT
  1048. DTSTART;TZID=Europe/Berlin:20160419T130000
  1049. SUMMARY:Sharer Test (public)
  1050. CLASS:PUBLIC
  1051. TRANSP:OPAQUE
  1052. STATUS:CONFIRMED
  1053. DTEND;TZID=Europe/Berlin:20160419T140000
  1054. LAST-MODIFIED:20160419T074202Z
  1055. DTSTAMP:20160419T074202Z
  1056. CREATED:20160419T074202Z
  1057. UID:2e468c48-7860-492e-bc52-92fa0daeeccf.1461051722310-4
  1058. END:VEVENT
  1059. END:VCALENDAR
  1060. EOD;
  1061. $sharerPrivate = <<<EOD
  1062. BEGIN:VCALENDAR
  1063. VERSION:2.0
  1064. PRODID:-//dmfs.org//mimedir.icalendar//EN
  1065. BEGIN:VTIMEZONE
  1066. TZID:Europe/Berlin
  1067. X-LIC-LOCATION:Europe/Berlin
  1068. BEGIN:DAYLIGHT
  1069. TZOFFSETFROM:+0100
  1070. TZOFFSETTO:+0200
  1071. TZNAME:CEST
  1072. DTSTART:19700329T020000
  1073. RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
  1074. END:DAYLIGHT
  1075. BEGIN:STANDARD
  1076. TZOFFSETFROM:+0200
  1077. TZOFFSETTO:+0100
  1078. TZNAME:CET
  1079. DTSTART:19701025T030000
  1080. RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
  1081. END:STANDARD
  1082. END:VTIMEZONE
  1083. BEGIN:VEVENT
  1084. DTSTART;TZID=Europe/Berlin:20160419T130000
  1085. SUMMARY:Sharer Test (private)
  1086. CLASS:PRIVATE
  1087. TRANSP:OPAQUE
  1088. STATUS:CONFIRMED
  1089. DTEND;TZID=Europe/Berlin:20160419T140000
  1090. LAST-MODIFIED:20160419T074202Z
  1091. DTSTAMP:20160419T074202Z
  1092. CREATED:20160419T074202Z
  1093. UID:2e468c48-7860-492e-bc52-92fa0daeeccf.1461051722310-5
  1094. END:VEVENT
  1095. END:VCALENDAR
  1096. EOD;
  1097. $sharerConfidential = <<<EOD
  1098. BEGIN:VCALENDAR
  1099. VERSION:2.0
  1100. PRODID:-//dmfs.org//mimedir.icalendar//EN
  1101. BEGIN:VTIMEZONE
  1102. TZID:Europe/Berlin
  1103. X-LIC-LOCATION:Europe/Berlin
  1104. BEGIN:DAYLIGHT
  1105. TZOFFSETFROM:+0100
  1106. TZOFFSETTO:+0200
  1107. TZNAME:CEST
  1108. DTSTART:19700329T020000
  1109. RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
  1110. END:DAYLIGHT
  1111. BEGIN:STANDARD
  1112. TZOFFSETFROM:+0200
  1113. TZOFFSETTO:+0100
  1114. TZNAME:CET
  1115. DTSTART:19701025T030000
  1116. RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
  1117. END:STANDARD
  1118. END:VTIMEZONE
  1119. BEGIN:VEVENT
  1120. DTSTART;TZID=Europe/Berlin:20160419T130000
  1121. SUMMARY:Sharer Test (confidential)
  1122. CLASS:CONFIDENTIAL
  1123. TRANSP:OPAQUE
  1124. STATUS:CONFIRMED
  1125. DTEND;TZID=Europe/Berlin:20160419T140000
  1126. LAST-MODIFIED:20160419T074202Z
  1127. DTSTAMP:20160419T074202Z
  1128. CREATED:20160419T074202Z
  1129. UID:2e468c48-7860-492e-bc52-92fa0daeeccf.1461051722310-6
  1130. END:VEVENT
  1131. END:VCALENDAR
  1132. EOD;
  1133. $l10n = $this->createMock(IL10N::class);
  1134. $l10n
  1135. ->expects($this->any())
  1136. ->method('t')
  1137. ->willReturnCallback(function ($text, $parameters = []) {
  1138. return vsprintf($text, $parameters);
  1139. });
  1140. $config = $this->createMock(IConfig::class);
  1141. $this->userManager->expects($this->any())
  1142. ->method('userExists')
  1143. ->willReturn(true);
  1144. $this->groupManager->expects($this->any())
  1145. ->method('groupExists')
  1146. ->willReturn(true);
  1147. $this->principal->expects(self::atLeastOnce())
  1148. ->method('findByUri')
  1149. ->willReturn(self::UNIT_TEST_USER);
  1150. $me = self::UNIT_TEST_USER;
  1151. $sharer = self::UNIT_TEST_USER1;
  1152. $this->backend->createCalendar($me, 'calendar-uri-me', []);
  1153. $this->backend->createCalendar($sharer, 'calendar-uri-sharer', []);
  1154. $myCalendars = $this->backend->getCalendarsForUser($me);
  1155. $this->assertCount(1, $myCalendars);
  1156. $sharerCalendars = $this->backend->getCalendarsForUser($sharer);
  1157. $this->assertCount(1, $sharerCalendars);
  1158. $logger = $this->createMock(\Psr\Log\LoggerInterface::class);
  1159. $sharerCalendar = new Calendar($this->backend, $sharerCalendars[0], $l10n, $config, $logger);
  1160. $this->backend->updateShares($sharerCalendar, [
  1161. [
  1162. 'href' => 'principal:' . $me,
  1163. 'readOnly' => false,
  1164. ],
  1165. ], []);
  1166. $this->assertCount(2, $this->backend->getCalendarsForUser($me));
  1167. $this->backend->createCalendarObject($myCalendars[0]['id'], 'event0.ics', $myPublic);
  1168. $this->backend->createCalendarObject($myCalendars[0]['id'], 'event1.ics', $myPrivate);
  1169. $this->backend->createCalendarObject($myCalendars[0]['id'], 'event2.ics', $myConfidential);
  1170. $this->backend->createCalendarObject($sharerCalendars[0]['id'], 'event3.ics', $sharerPublic);
  1171. $this->backend->createCalendarObject($sharerCalendars[0]['id'], 'event4.ics', $sharerPrivate);
  1172. $this->backend->createCalendarObject($sharerCalendars[0]['id'], 'event5.ics', $sharerConfidential);
  1173. $mySearchResults = $this->backend->searchPrincipalUri($me, 'Test', ['VEVENT'], ['SUMMARY'], []);
  1174. $sharerSearchResults = $this->backend->searchPrincipalUri($sharer, 'Test', ['VEVENT'], ['SUMMARY'], []);
  1175. $this->assertCount(4, $mySearchResults);
  1176. $this->assertCount(3, $sharerSearchResults);
  1177. $this->assertEquals($myPublic, $mySearchResults[0]['calendardata']);
  1178. $this->assertEquals($myPrivate, $mySearchResults[1]['calendardata']);
  1179. $this->assertEquals($myConfidential, $mySearchResults[2]['calendardata']);
  1180. $this->assertEquals($sharerPublic, $mySearchResults[3]['calendardata']);
  1181. $this->assertEquals($sharerPublic, $sharerSearchResults[0]['calendardata']);
  1182. $this->assertEquals($sharerPrivate, $sharerSearchResults[1]['calendardata']);
  1183. $this->assertEquals($sharerConfidential, $sharerSearchResults[2]['calendardata']);
  1184. }
  1185. /**
  1186. * @throws \OCP\DB\Exception
  1187. * @throws \Sabre\DAV\Exception\BadRequest
  1188. */
  1189. public function testPruneOutdatedSyncTokens(): void {
  1190. $calendarId = $this->createTestCalendar();
  1191. $changes = $this->backend->getChangesForCalendar($calendarId, '', 1);
  1192. $syncToken = $changes['syncToken'];
  1193. $uri = static::getUniqueID('calobj');
  1194. $calData = <<<EOD
  1195. BEGIN:VCALENDAR
  1196. VERSION:2.0
  1197. PRODID:Nextcloud Calendar
  1198. BEGIN:VEVENT
  1199. CREATED;VALUE=DATE-TIME:20130910T125139Z
  1200. UID:47d15e3ec8
  1201. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  1202. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  1203. SUMMARY:Test Event
  1204. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  1205. DTEND;VALUE=DATE-TIME:20130912T140000Z
  1206. CLASS:PUBLIC
  1207. END:VEVENT
  1208. END:VCALENDAR
  1209. EOD;
  1210. $this->backend->createCalendarObject($calendarId, $uri, $calData);
  1211. // update the card
  1212. $calData = <<<'EOD'
  1213. BEGIN:VCALENDAR
  1214. VERSION:2.0
  1215. PRODID:Nextcloud Calendar
  1216. BEGIN:VEVENT
  1217. CREATED;VALUE=DATE-TIME:20130910T125139Z
  1218. UID:47d15e3ec8
  1219. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  1220. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  1221. SUMMARY:123 Event 🙈
  1222. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  1223. DTEND;VALUE=DATE-TIME:20130912T140000Z
  1224. ATTENDEE;CN=test:mailto:foo@bar.com
  1225. END:VEVENT
  1226. END:VCALENDAR
  1227. EOD;
  1228. $this->backend->updateCalendarObject($calendarId, $uri, $calData);
  1229. $deleted = $this->backend->pruneOutdatedSyncTokens(0);
  1230. // At least one from the object creation and one from the object update
  1231. $this->assertGreaterThanOrEqual(2, $deleted);
  1232. $changes = $this->backend->getChangesForCalendar($calendarId, $syncToken, 1);
  1233. $this->assertEmpty($changes['added']);
  1234. $this->assertEmpty($changes['modified']);
  1235. $this->assertEmpty($changes['deleted']);
  1236. // Test that objects remain
  1237. // Currently changes are empty
  1238. $changes = $this->backend->getChangesForCalendar($calendarId, $syncToken, 100);
  1239. $this->assertEquals(0, count($changes['added'] + $changes['modified'] + $changes['deleted']));
  1240. // Create card
  1241. $uri = static::getUniqueID('calobj');
  1242. $calData = <<<EOD
  1243. BEGIN:VCALENDAR
  1244. VERSION:2.0
  1245. PRODID:Nextcloud Calendar
  1246. BEGIN:VEVENT
  1247. CREATED;VALUE=DATE-TIME:20230910T125139Z
  1248. UID:47d15e3ec9
  1249. LAST-MODIFIED;VALUE=DATE-TIME:20230910T125139Z
  1250. DTSTAMP;VALUE=DATE-TIME:20230910T125139Z
  1251. SUMMARY:Test Event
  1252. DTSTART;VALUE=DATE-TIME:20230912T130000Z
  1253. DTEND;VALUE=DATE-TIME:20230912T140000Z
  1254. CLASS:PUBLIC
  1255. END:VEVENT
  1256. END:VCALENDAR
  1257. EOD;
  1258. $this->backend->createCalendarObject($calendarId, $uri, $calData);
  1259. // We now have one add
  1260. $changes = $this->backend->getChangesForCalendar($calendarId, $syncToken, 100);
  1261. $this->assertEquals(1, count($changes['added']));
  1262. $this->assertEmpty($changes['modified']);
  1263. $this->assertEmpty($changes['deleted']);
  1264. // update the card
  1265. $calData = <<<'EOD'
  1266. BEGIN:VCALENDAR
  1267. VERSION:2.0
  1268. PRODID:Nextcloud Calendar
  1269. BEGIN:VEVENT
  1270. CREATED;VALUE=DATE-TIME:20230910T125139Z
  1271. UID:47d15e3ec9
  1272. LAST-MODIFIED;VALUE=DATE-TIME:20230910T125139Z
  1273. DTSTAMP;VALUE=DATE-TIME:20230910T125139Z
  1274. SUMMARY:123 Event 🙈
  1275. DTSTART;VALUE=DATE-TIME:20230912T130000Z
  1276. DTEND;VALUE=DATE-TIME:20230912T140000Z
  1277. ATTENDEE;CN=test:mailto:foo@bar.com
  1278. END:VEVENT
  1279. END:VCALENDAR
  1280. EOD;
  1281. $this->backend->updateCalendarObject($calendarId, $uri, $calData);
  1282. // One add, one modify, but shortened to modify
  1283. $changes = $this->backend->getChangesForCalendar($calendarId, $syncToken, 100);
  1284. $this->assertEmpty($changes['added']);
  1285. $this->assertEquals(1, count($changes['modified']));
  1286. $this->assertEmpty($changes['deleted']);
  1287. // Delete all but last change
  1288. $deleted = $this->backend->pruneOutdatedSyncTokens(1);
  1289. $this->assertEquals(1, $deleted); // We had two changes before, now one
  1290. // Only update should remain
  1291. $changes = $this->backend->getChangesForCalendar($calendarId, $syncToken, 100);
  1292. $this->assertEmpty($changes['added']);
  1293. $this->assertEquals(1, count($changes['modified']));
  1294. $this->assertEmpty($changes['deleted']);
  1295. // Check that no crash occurs when prune is called without current changes
  1296. $deleted = $this->backend->pruneOutdatedSyncTokens(1);
  1297. }
  1298. public function testSearchAndExpandRecurrences() {
  1299. $calendarId = $this->createTestCalendar();
  1300. $calendarInfo = [
  1301. 'id' => $calendarId,
  1302. 'principaluri' => 'user1',
  1303. '{http://owncloud.org/ns}owner-principal' => 'user1',
  1304. ];
  1305. $calData = <<<'EOD'
  1306. BEGIN:VCALENDAR
  1307. PRODID:-//IDN nextcloud.com//Calendar app 4.5.0-alpha.2//EN
  1308. CALSCALE:GREGORIAN
  1309. VERSION:2.0
  1310. BEGIN:VEVENT
  1311. CREATED:20230921T133401Z
  1312. DTSTAMP:20230921T133448Z
  1313. LAST-MODIFIED:20230921T133448Z
  1314. SEQUENCE:2
  1315. UID:7b7d5d12-683c-48ce-973a-b3e1cb0bae2a
  1316. DTSTART;VALUE=DATE:20230912
  1317. DTEND;VALUE=DATE:20230913
  1318. STATUS:CONFIRMED
  1319. SUMMARY:Daily Event
  1320. RRULE:FREQ=DAILY
  1321. END:VEVENT
  1322. END:VCALENDAR
  1323. EOD;
  1324. $uri = static::getUniqueID('calobj');
  1325. $this->backend->createCalendarObject($calendarId, $uri, $calData);
  1326. $start = new DateTimeImmutable('2023-09-20T00:00:00Z');
  1327. $end = $start->add(new DateInterval('P14D'));
  1328. $results = $this->backend->search(
  1329. $calendarInfo,
  1330. '',
  1331. [],
  1332. [
  1333. 'timerange' => [
  1334. 'start' => $start,
  1335. 'end' => $end,
  1336. ]
  1337. ],
  1338. null,
  1339. null,
  1340. );
  1341. $this->assertCount(1, $results);
  1342. $this->assertCount(14, $results[0]['objects']);
  1343. foreach ($results as $result) {
  1344. foreach ($result['objects'] as $object) {
  1345. $this->assertEquals($object['UID'][0], '7b7d5d12-683c-48ce-973a-b3e1cb0bae2a');
  1346. $this->assertEquals($object['SUMMARY'][0], 'Daily Event');
  1347. $this->assertGreaterThanOrEqual(
  1348. $start->getTimestamp(),
  1349. $object['DTSTART'][0]->getTimestamp(),
  1350. 'Recurrence starting before requested start',
  1351. );
  1352. $this->assertLessThanOrEqual(
  1353. $end->getTimestamp(),
  1354. $object['DTSTART'][0]->getTimestamp(),
  1355. 'Recurrence starting after requested end',
  1356. );
  1357. }
  1358. }
  1359. }
  1360. }