CalDavBackendTest.php 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\Tests\unit\CalDAV;
  8. use DateInterval;
  9. use DateTime;
  10. use DateTimeImmutable;
  11. use DateTimeZone;
  12. use OCA\DAV\CalDAV\CalDavBackend;
  13. use OCA\DAV\CalDAV\Calendar;
  14. use OCA\DAV\DAV\Sharing\Plugin as SharingPlugin;
  15. use OCA\DAV\Events\CalendarDeletedEvent;
  16. use OCP\IConfig;
  17. use OCP\IL10N;
  18. use Sabre\DAV\Exception\NotFound;
  19. use Sabre\DAV\PropPatch;
  20. use Sabre\DAV\Xml\Property\Href;
  21. use Sabre\DAVACL\IACL;
  22. use function time;
  23. /**
  24. * Class CalDavBackendTest
  25. *
  26. * @group DB
  27. *
  28. * @package OCA\DAV\Tests\unit\CalDAV
  29. */
  30. class CalDavBackendTest extends AbstractCalDavBackend {
  31. public function testCalendarOperations(): void {
  32. $calendarId = $this->createTestCalendar();
  33. // update its display name
  34. $patch = new PropPatch([
  35. '{DAV:}displayname' => 'Unit test',
  36. '{urn:ietf:params:xml:ns:caldav}calendar-description' => 'Calendar used for unit testing'
  37. ]);
  38. $this->dispatcher->expects(self::atLeastOnce())
  39. ->method('dispatchTyped');
  40. $this->backend->updateCalendar($calendarId, $patch);
  41. $patch->commit();
  42. $this->assertEquals(1, $this->backend->getCalendarsForUserCount(self::UNIT_TEST_USER));
  43. $calendars = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER);
  44. $this->assertCount(1, $calendars);
  45. $this->assertEquals('Unit test', $calendars[0]['{DAV:}displayname']);
  46. $this->assertEquals('Calendar used for unit testing', $calendars[0]['{urn:ietf:params:xml:ns:caldav}calendar-description']);
  47. $this->assertEquals('User\'s displayname', $calendars[0]['{http://nextcloud.com/ns}owner-displayname']);
  48. // delete the address book
  49. $this->dispatcher->expects(self::atLeastOnce())
  50. ->method('dispatchTyped');
  51. $this->backend->deleteCalendar($calendars[0]['id'], true);
  52. $calendars = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER);
  53. self::assertEmpty($calendars);
  54. }
  55. public function providesSharingData() {
  56. return [
  57. [true, true, true, false, [
  58. [
  59. 'href' => 'principal:' . self::UNIT_TEST_USER1,
  60. 'readOnly' => false
  61. ],
  62. [
  63. 'href' => 'principal:' . self::UNIT_TEST_GROUP,
  64. 'readOnly' => true
  65. ]
  66. ], [
  67. self::UNIT_TEST_USER1,
  68. self::UNIT_TEST_GROUP,
  69. ]],
  70. [true, true, true, false, [
  71. [
  72. 'href' => 'principal:' . self::UNIT_TEST_GROUP,
  73. 'readOnly' => true,
  74. ],
  75. [
  76. 'href' => 'principal:' . self::UNIT_TEST_GROUP2,
  77. 'readOnly' => false,
  78. ],
  79. ], [
  80. self::UNIT_TEST_GROUP,
  81. self::UNIT_TEST_GROUP2,
  82. ]],
  83. [true, true, true, true, [
  84. [
  85. 'href' => 'principal:' . self::UNIT_TEST_GROUP,
  86. 'readOnly' => false,
  87. ],
  88. [
  89. 'href' => 'principal:' . self::UNIT_TEST_GROUP2,
  90. 'readOnly' => true,
  91. ],
  92. ], [
  93. self::UNIT_TEST_GROUP,
  94. self::UNIT_TEST_GROUP2,
  95. ]],
  96. [true, false, false, false, [
  97. [
  98. 'href' => 'principal:' . self::UNIT_TEST_USER1,
  99. 'readOnly' => true
  100. ],
  101. ], [
  102. self::UNIT_TEST_USER1,
  103. ]],
  104. ];
  105. }
  106. /**
  107. * @dataProvider providesSharingData
  108. */
  109. public function testCalendarSharing($userCanRead, $userCanWrite, $groupCanRead, $groupCanWrite, $add, $principals): void {
  110. $logger = $this->createMock(\Psr\Log\LoggerInterface::class);
  111. $config = $this->createMock(IConfig::class);
  112. $l10n = $this->createMock(IL10N::class);
  113. $l10n->expects($this->any())
  114. ->method('t')
  115. ->willReturnCallback(function ($text, $parameters = []) {
  116. return vsprintf($text, $parameters);
  117. });
  118. $this->userManager->expects($this->any())
  119. ->method('userExists')
  120. ->willReturn(true);
  121. $this->groupManager->expects($this->any())
  122. ->method('groupExists')
  123. ->willReturn(true);
  124. $this->principal->expects(self::atLeastOnce())
  125. ->method('findByUri')
  126. ->willReturnOnConsecutiveCalls(...$principals);
  127. $calendarId = $this->createTestCalendar();
  128. $calendars = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER);
  129. $this->assertCount(1, $calendars);
  130. $calendar = new Calendar($this->backend, $calendars[0], $l10n, $config, $logger);
  131. $this->backend->updateShares($calendar, $add, []);
  132. $calendars = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER1);
  133. $this->assertCount(1, $calendars);
  134. $calendar = new Calendar($this->backend, $calendars[0], $l10n, $config, $logger);
  135. $acl = $calendar->getACL();
  136. $this->assertAcl(self::UNIT_TEST_USER, '{DAV:}read', $acl);
  137. $this->assertAcl(self::UNIT_TEST_USER, '{DAV:}write', $acl);
  138. $this->assertAccess($userCanRead, self::UNIT_TEST_USER1, '{DAV:}read', $acl);
  139. $this->assertAccess($userCanWrite, self::UNIT_TEST_USER1, '{DAV:}write', $acl);
  140. $this->assertEquals(self::UNIT_TEST_USER, $calendar->getOwner());
  141. // test acls on the child
  142. $uri = static::getUniqueID('calobj');
  143. $calData = <<<'EOD'
  144. BEGIN:VCALENDAR
  145. VERSION:2.0
  146. PRODID:ownCloud Calendar
  147. BEGIN:VEVENT
  148. CREATED;VALUE=DATE-TIME:20130910T125139Z
  149. UID:47d15e3ec8
  150. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  151. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  152. SUMMARY:Test Event
  153. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  154. DTEND;VALUE=DATE-TIME:20130912T140000Z
  155. CLASS:PUBLIC
  156. END:VEVENT
  157. END:VCALENDAR
  158. EOD;
  159. $this->dispatcher->expects(self::atLeastOnce())
  160. ->method('dispatchTyped');
  161. $this->backend->createCalendarObject($calendarId, $uri, $calData);
  162. /** @var IACL $child */
  163. $child = $calendar->getChild($uri);
  164. $acl = $child->getACL();
  165. $this->assertAcl(self::UNIT_TEST_USER, '{DAV:}read', $acl);
  166. $this->assertAcl(self::UNIT_TEST_USER, '{DAV:}write', $acl);
  167. $this->assertAccess($userCanRead, self::UNIT_TEST_USER1, '{DAV:}read', $acl);
  168. $this->assertAccess($userCanWrite, self::UNIT_TEST_USER1, '{DAV:}write', $acl);
  169. // delete the calendar
  170. $this->dispatcher->expects(self::once())
  171. ->method('dispatchTyped')
  172. ->with(self::callback(function ($event) {
  173. return $event instanceof CalendarDeletedEvent;
  174. }));
  175. $this->backend->deleteCalendar($calendars[0]['id'], true);
  176. $calendars = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER);
  177. self::assertEmpty($calendars);
  178. }
  179. public function testCalendarObjectsOperations(): void {
  180. $calendarId = $this->createTestCalendar();
  181. // create a card
  182. $uri = static::getUniqueID('calobj');
  183. $calData = <<<'EOD'
  184. BEGIN:VCALENDAR
  185. VERSION:2.0
  186. PRODID:ownCloud Calendar
  187. BEGIN:VEVENT
  188. CREATED;VALUE=DATE-TIME:20130910T125139Z
  189. UID:47d15e3ec8
  190. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  191. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  192. SUMMARY:Test Event
  193. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  194. DTEND;VALUE=DATE-TIME:20130912T140000Z
  195. CLASS:PUBLIC
  196. END:VEVENT
  197. END:VCALENDAR
  198. EOD;
  199. $this->dispatcher->expects(self::atLeastOnce())
  200. ->method('dispatchTyped');
  201. $this->backend->createCalendarObject($calendarId, $uri, $calData);
  202. // get all the calendar objects
  203. $calendarObjects = $this->backend->getCalendarObjects($calendarId);
  204. $this->assertCount(1, $calendarObjects);
  205. $this->assertEquals($calendarId, $calendarObjects[0]['calendarid']);
  206. $this->assertArrayHasKey('classification', $calendarObjects[0]);
  207. // get the calendar objects
  208. $calendarObject = $this->backend->getCalendarObject($calendarId, $uri);
  209. $this->assertNotNull($calendarObject);
  210. $this->assertArrayHasKey('id', $calendarObject);
  211. $this->assertArrayHasKey('uri', $calendarObject);
  212. $this->assertArrayHasKey('lastmodified', $calendarObject);
  213. $this->assertArrayHasKey('etag', $calendarObject);
  214. $this->assertArrayHasKey('size', $calendarObject);
  215. $this->assertArrayHasKey('classification', $calendarObject);
  216. $this->assertArrayHasKey('{' . SharingPlugin::NS_NEXTCLOUD . '}deleted-at', $calendarObject);
  217. $this->assertEquals($calData, $calendarObject['calendardata']);
  218. // update the card
  219. $calData = <<<'EOD'
  220. BEGIN:VCALENDAR
  221. VERSION:2.0
  222. PRODID:ownCloud Calendar
  223. BEGIN:VEVENT
  224. CREATED;VALUE=DATE-TIME:20130910T125139Z
  225. UID:47d15e3ec8
  226. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  227. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  228. SUMMARY:Test Event
  229. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  230. DTEND;VALUE=DATE-TIME:20130912T140000Z
  231. END:VEVENT
  232. END:VCALENDAR
  233. EOD;
  234. $this->dispatcher->expects(self::atLeastOnce())
  235. ->method('dispatchTyped');
  236. $this->backend->updateCalendarObject($calendarId, $uri, $calData);
  237. $calendarObject = $this->backend->getCalendarObject($calendarId, $uri);
  238. $this->assertEquals($calData, $calendarObject['calendardata']);
  239. // delete the card
  240. $this->dispatcher->expects(self::atLeastOnce())
  241. ->method('dispatchTyped');
  242. $this->backend->deleteCalendarObject($calendarId, $uri);
  243. $calendarObjects = $this->backend->getCalendarObjects($calendarId);
  244. $this->assertCount(0, $calendarObjects);
  245. }
  246. public function testMultipleCalendarObjectsWithSameUID(): void {
  247. $this->expectException(\Sabre\DAV\Exception\BadRequest::class);
  248. $this->expectExceptionMessage('Calendar object with uid already exists in this calendar collection.');
  249. $calendarId = $this->createTestCalendar();
  250. $calData = <<<'EOD'
  251. BEGIN:VCALENDAR
  252. VERSION:2.0
  253. PRODID:ownCloud Calendar
  254. BEGIN:VEVENT
  255. CREATED;VALUE=DATE-TIME:20130910T125139Z
  256. UID:47d15e3ec8-1
  257. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  258. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  259. SUMMARY:Test Event
  260. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  261. DTEND;VALUE=DATE-TIME:20130912T140000Z
  262. CLASS:PUBLIC
  263. END:VEVENT
  264. END:VCALENDAR
  265. EOD;
  266. $uri0 = static::getUniqueID('event');
  267. $uri1 = static::getUniqueID('event');
  268. $this->backend->createCalendarObject($calendarId, $uri0, $calData);
  269. $this->backend->createCalendarObject($calendarId, $uri1, $calData);
  270. }
  271. public function testMultiCalendarObjects(): void {
  272. $calendarId = $this->createTestCalendar();
  273. // create an event
  274. $calData = [];
  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. $calData[] = <<<'EOD'
  292. BEGIN:VCALENDAR
  293. VERSION:2.0
  294. PRODID:ownCloud Calendar
  295. BEGIN:VEVENT
  296. CREATED;VALUE=DATE-TIME:20130910T125139Z
  297. UID:47d15e3ec8-2
  298. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  299. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  300. SUMMARY:Test Event
  301. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  302. DTEND;VALUE=DATE-TIME:20130912T140000Z
  303. CLASS:PUBLIC
  304. END:VEVENT
  305. END:VCALENDAR
  306. EOD;
  307. $calData[] = <<<'EOD'
  308. BEGIN:VCALENDAR
  309. VERSION:2.0
  310. PRODID:ownCloud Calendar
  311. BEGIN:VEVENT
  312. CREATED;VALUE=DATE-TIME:20130910T125139Z
  313. UID:47d15e3ec8-3
  314. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  315. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  316. SUMMARY:Test Event
  317. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  318. DTEND;VALUE=DATE-TIME:20130912T140000Z
  319. CLASS:PUBLIC
  320. END:VEVENT
  321. END:VCALENDAR
  322. EOD;
  323. $uri0 = static::getUniqueID('card');
  324. $this->dispatcher->expects(self::atLeastOnce())
  325. ->method('dispatchTyped');
  326. $this->backend->createCalendarObject($calendarId, $uri0, $calData[0]);
  327. $uri1 = static::getUniqueID('card');
  328. $this->dispatcher->expects(self::atLeastOnce())
  329. ->method('dispatchTyped');
  330. $this->backend->createCalendarObject($calendarId, $uri1, $calData[1]);
  331. $uri2 = static::getUniqueID('card');
  332. $this->dispatcher->expects(self::atLeastOnce())
  333. ->method('dispatchTyped');
  334. $this->backend->createCalendarObject($calendarId, $uri2, $calData[2]);
  335. // get all the cards
  336. $calendarObjects = $this->backend->getCalendarObjects($calendarId);
  337. $this->assertCount(3, $calendarObjects);
  338. // get the cards
  339. $calendarObjects = $this->backend->getMultipleCalendarObjects($calendarId, [$uri1, $uri2]);
  340. $this->assertCount(2, $calendarObjects);
  341. foreach ($calendarObjects as $card) {
  342. $this->assertArrayHasKey('id', $card);
  343. $this->assertArrayHasKey('uri', $card);
  344. $this->assertArrayHasKey('lastmodified', $card);
  345. $this->assertArrayHasKey('etag', $card);
  346. $this->assertArrayHasKey('size', $card);
  347. $this->assertArrayHasKey('classification', $card);
  348. }
  349. usort($calendarObjects, function ($a, $b) {
  350. return $a['id'] - $b['id'];
  351. });
  352. $this->assertEquals($calData[1], $calendarObjects[0]['calendardata']);
  353. $this->assertEquals($calData[2], $calendarObjects[1]['calendardata']);
  354. // delete the card
  355. $this->dispatcher->expects(self::atLeastOnce())
  356. ->method('dispatchTyped');
  357. $this->backend->deleteCalendarObject($calendarId, $uri0);
  358. $this->dispatcher->expects(self::atLeastOnce())
  359. ->method('dispatchTyped');
  360. $this->backend->deleteCalendarObject($calendarId, $uri1);
  361. $this->dispatcher->expects(self::atLeastOnce())
  362. ->method('dispatchTyped');
  363. $this->backend->deleteCalendarObject($calendarId, $uri2);
  364. $calendarObjects = $this->backend->getCalendarObjects($calendarId);
  365. $this->assertCount(0, $calendarObjects);
  366. }
  367. /**
  368. * @dataProvider providesCalendarQueryParameters
  369. */
  370. public function testCalendarQuery($expectedEventsInResult, $propFilters, $compFilter): void {
  371. $calendarId = $this->createTestCalendar();
  372. $events = [];
  373. $events[0] = $this->createEvent($calendarId, '20130912T130000Z', '20130912T140000Z');
  374. $events[1] = $this->createEvent($calendarId, '20130912T150000Z', '20130912T170000Z');
  375. $events[2] = $this->createEvent($calendarId, '20130912T173000Z', '20130912T220000Z');
  376. if (PHP_INT_SIZE > 8) {
  377. $events[3] = $this->createEvent($calendarId, '21130912T130000Z', '22130912T130000Z');
  378. } else {
  379. /* On 32bit we do not support events after 2038 */
  380. $events[3] = $this->createEvent($calendarId, '20370912T130000Z', '20370912T130000Z');
  381. }
  382. $result = $this->backend->calendarQuery($calendarId, [
  383. 'name' => '',
  384. 'prop-filters' => $propFilters,
  385. 'comp-filters' => $compFilter
  386. ]);
  387. $expectedEventsInResult = array_map(function ($index) use ($events) {
  388. return $events[$index];
  389. }, $expectedEventsInResult);
  390. $this->assertEqualsCanonicalizing($expectedEventsInResult, $result);
  391. }
  392. public function testGetCalendarObjectByUID(): void {
  393. $calendarId = $this->createTestCalendar();
  394. $uri = static::getUniqueID('calobj');
  395. $calData = <<<'EOD'
  396. BEGIN:VCALENDAR
  397. VERSION:2.0
  398. PRODID:ownCloud Calendar
  399. BEGIN:VEVENT
  400. CREATED;VALUE=DATE-TIME:20130910T125139Z
  401. UID:47d15e3ec8
  402. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  403. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  404. SUMMARY:Test Event
  405. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  406. DTEND;VALUE=DATE-TIME:20130912T140000Z
  407. CLASS:PUBLIC
  408. END:VEVENT
  409. END:VCALENDAR
  410. EOD;
  411. $this->backend->createCalendarObject($calendarId, $uri, $calData);
  412. $co = $this->backend->getCalendarObjectByUID(self::UNIT_TEST_USER, '47d15e3ec8');
  413. $this->assertNotNull($co);
  414. }
  415. public function providesCalendarQueryParameters() {
  416. return [
  417. 'all' => [[0, 1, 2, 3], [], []],
  418. 'only-todos' => [[], ['name' => 'VTODO'], []],
  419. 'only-events' => [[0, 1, 2, 3], [], [['name' => 'VEVENT', 'is-not-defined' => false, 'comp-filters' => [], 'time-range' => ['start' => null, 'end' => null], 'prop-filters' => []]],],
  420. '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' => []]],],
  421. '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' => []]],],
  422. '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' => []]],],
  423. ];
  424. }
  425. public function testCalendarSynchronization(): void {
  426. // construct calendar for testing
  427. $calendarId = $this->createTestCalendar();
  428. /** test fresh sync state with NO events in calendar */
  429. // construct test state
  430. $stateTest = ['syncToken' => 1, 'added' => [], 'modified' => [], 'deleted' => []];
  431. // retrieve live state
  432. $stateLive = $this->backend->getChangesForCalendar($calendarId, '', 1);
  433. // test live state
  434. $this->assertEquals($stateTest, $stateLive, 'Failed test fresh sync state with NO events in calendar');
  435. /** test delta sync state with NO events in calendar */
  436. // construct test state
  437. $stateTest = ['syncToken' => 1, 'added' => [], 'modified' => [], 'deleted' => []];
  438. // retrieve live state
  439. $stateLive = $this->backend->getChangesForCalendar($calendarId, '2', 1);
  440. // test live state
  441. $this->assertEquals($stateTest, $stateLive, 'Failed test delta sync state with NO events in calendar');
  442. /** add events to calendar */
  443. $event1 = $this->createEvent($calendarId, '20240701T130000Z', '20240701T140000Z');
  444. $event2 = $this->createEvent($calendarId, '20240701T140000Z', '20240701T150000Z');
  445. $event3 = $this->createEvent($calendarId, '20240701T150000Z', '20240701T160000Z');
  446. /** test fresh sync state with events in calendar */
  447. // construct expected state
  448. $stateTest = ['syncToken' => 4, 'added' => [$event1, $event2, $event3], 'modified' => [], 'deleted' => []];
  449. sort($stateTest['added']);
  450. // retrieve live state
  451. $stateLive = $this->backend->getChangesForCalendar($calendarId, '', 1);
  452. // sort live state results
  453. sort($stateLive['added']);
  454. sort($stateLive['modified']);
  455. sort($stateLive['deleted']);
  456. // test live state
  457. $this->assertEquals($stateTest, $stateLive, 'Failed test fresh sync state with events in calendar');
  458. /** test delta sync state with events in calendar */
  459. // construct expected state
  460. $stateTest = ['syncToken' => 4, 'added' => [$event2, $event3], 'modified' => [], 'deleted' => []];
  461. sort($stateTest['added']);
  462. // retrieve live state
  463. $stateLive = $this->backend->getChangesForCalendar($calendarId, '2', 1);
  464. // sort live state results
  465. sort($stateLive['added']);
  466. sort($stateLive['modified']);
  467. sort($stateLive['deleted']);
  468. // test live state
  469. $this->assertEquals($stateTest, $stateLive, 'Failed test delta sync state with events in calendar');
  470. /** modify/delete events in calendar */
  471. $this->deleteEvent($calendarId, $event1);
  472. $this->modifyEvent($calendarId, $event2, '20250701T140000Z', '20250701T150000Z');
  473. /** test fresh sync state with modified/deleted events in calendar */
  474. // construct expected state
  475. $stateTest = ['syncToken' => 6, 'added' => [$event2, $event3], 'modified' => [], 'deleted' => []];
  476. sort($stateTest['added']);
  477. // retrieve live state
  478. $stateLive = $this->backend->getChangesForCalendar($calendarId, '', 1);
  479. // sort live state results
  480. sort($stateLive['added']);
  481. sort($stateLive['modified']);
  482. sort($stateLive['deleted']);
  483. // test live state
  484. $this->assertEquals($stateTest, $stateLive, 'Failed test fresh sync state with modified/deleted events in calendar');
  485. /** test delta sync state with modified/deleted events in calendar */
  486. // construct expected state
  487. $stateTest = ['syncToken' => 6, 'added' => [$event3], 'modified' => [$event2], 'deleted' => [$event1]];
  488. // retrieve live state
  489. $stateLive = $this->backend->getChangesForCalendar($calendarId, '3', 1);
  490. // test live state
  491. $this->assertEquals($stateTest, $stateLive, 'Failed test delta sync state with modified/deleted events in calendar');
  492. /** test delta sync state with modified/deleted events in calendar and invalid token */
  493. // construct expected state
  494. $stateTest = ['syncToken' => 6, 'added' => [], 'modified' => [], 'deleted' => []];
  495. // retrieve live state
  496. $stateLive = $this->backend->getChangesForCalendar($calendarId, '6', 1);
  497. // test live state
  498. $this->assertEquals($stateTest, $stateLive, 'Failed test delta sync state with modified/deleted events in calendar and invalid token');
  499. }
  500. public function testPublications(): void {
  501. $this->dispatcher->expects(self::atLeastOnce())
  502. ->method('dispatchTyped');
  503. $this->backend->createCalendar(self::UNIT_TEST_USER, 'Example', []);
  504. $calendarInfo = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER)[0];
  505. /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject $l10n */
  506. $l10n = $this->createMock(IL10N::class);
  507. $config = $this->createMock(IConfig::class);
  508. $logger = $this->createMock(\Psr\Log\LoggerInterface::class);
  509. $calendar = new Calendar($this->backend, $calendarInfo, $l10n, $config, $logger);
  510. $calendar->setPublishStatus(true);
  511. $this->assertNotEquals(false, $calendar->getPublishStatus());
  512. $publicCalendars = $this->backend->getPublicCalendars();
  513. $this->assertCount(1, $publicCalendars);
  514. $this->assertEquals(true, $publicCalendars[0]['{http://owncloud.org/ns}public']);
  515. $this->assertEquals('User\'s displayname', $publicCalendars[0]['{http://nextcloud.com/ns}owner-displayname']);
  516. $publicCalendarURI = $publicCalendars[0]['uri'];
  517. $publicCalendar = $this->backend->getPublicCalendar($publicCalendarURI);
  518. $this->assertEquals(true, $publicCalendar['{http://owncloud.org/ns}public']);
  519. $calendar->setPublishStatus(false);
  520. $this->assertEquals(false, $calendar->getPublishStatus());
  521. $this->expectException(NotFound::class);
  522. $this->backend->getPublicCalendar($publicCalendarURI);
  523. }
  524. public function testSubscriptions(): void {
  525. $id = $this->backend->createSubscription(self::UNIT_TEST_USER, 'Subscription', [
  526. '{http://calendarserver.org/ns/}source' => new Href('test-source'),
  527. '{http://apple.com/ns/ical/}calendar-color' => '#1C4587',
  528. '{http://calendarserver.org/ns/}subscribed-strip-todos' => ''
  529. ]);
  530. $subscriptions = $this->backend->getSubscriptionsForUser(self::UNIT_TEST_USER);
  531. $this->assertCount(1, $subscriptions);
  532. $this->assertEquals('#1C4587', $subscriptions[0]['{http://apple.com/ns/ical/}calendar-color']);
  533. $this->assertEquals(true, $subscriptions[0]['{http://calendarserver.org/ns/}subscribed-strip-todos']);
  534. $this->assertEquals($id, $subscriptions[0]['id']);
  535. $patch = new PropPatch([
  536. '{DAV:}displayname' => 'Unit test',
  537. '{http://apple.com/ns/ical/}calendar-color' => '#ac0606',
  538. ]);
  539. $this->backend->updateSubscription($id, $patch);
  540. $patch->commit();
  541. $subscriptions = $this->backend->getSubscriptionsForUser(self::UNIT_TEST_USER);
  542. $this->assertCount(1, $subscriptions);
  543. $this->assertEquals($id, $subscriptions[0]['id']);
  544. $this->assertEquals('Unit test', $subscriptions[0]['{DAV:}displayname']);
  545. $this->assertEquals('#ac0606', $subscriptions[0]['{http://apple.com/ns/ical/}calendar-color']);
  546. $this->backend->deleteSubscription($id);
  547. $subscriptions = $this->backend->getSubscriptionsForUser(self::UNIT_TEST_USER);
  548. $this->assertCount(0, $subscriptions);
  549. }
  550. public function providesSchedulingData() {
  551. $data = <<<EOS
  552. BEGIN:VCALENDAR
  553. VERSION:2.0
  554. PRODID:-//Sabre//Sabre VObject 3.5.0//EN
  555. CALSCALE:GREGORIAN
  556. METHOD:REQUEST
  557. BEGIN:VTIMEZONE
  558. TZID:Europe/Warsaw
  559. BEGIN:DAYLIGHT
  560. TZOFFSETFROM:+0100
  561. TZOFFSETTO:+0200
  562. TZNAME:CEST
  563. DTSTART:19700329T020000
  564. RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=3
  565. END:DAYLIGHT
  566. BEGIN:STANDARD
  567. TZOFFSETFROM:+0200
  568. TZOFFSETTO:+0100
  569. TZNAME:CET
  570. DTSTART:19701025T030000
  571. RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10
  572. END:STANDARD
  573. END:VTIMEZONE
  574. BEGIN:VEVENT
  575. CREATED:20170320T131655Z
  576. LAST-MODIFIED:20170320T135019Z
  577. DTSTAMP:20170320T135019Z
  578. UID:7e908a6d-4c4e-48d7-bd62-59ab80fbf1a3
  579. SUMMARY:TEST Z pg_escape_bytea
  580. ORGANIZER;RSVP=TRUE;PARTSTAT=ACCEPTED;ROLE=CHAIR:mailto:k.klimczak@gromar.e
  581. u
  582. ATTENDEE;RSVP=TRUE;CN=Zuzanna Leszek;PARTSTAT=NEEDS-ACTION;ROLE=REQ-PARTICI
  583. PANT:mailto:z.leszek@gromar.eu
  584. ATTENDEE;RSVP=TRUE;CN=Marcin Pisarski;PARTSTAT=NEEDS-ACTION;ROLE=REQ-PARTIC
  585. IPANT:mailto:m.pisarski@gromar.eu
  586. ATTENDEE;RSVP=TRUE;PARTSTAT=NEEDS-ACTION;ROLE=REQ-PARTICIPANT:mailto:klimcz
  587. ak.k@gmail.com
  588. ATTENDEE;RSVP=TRUE;PARTSTAT=NEEDS-ACTION;ROLE=REQ-PARTICIPANT:mailto:k_klim
  589. czak@tlen.pl
  590. DTSTART;TZID=Europe/Warsaw:20170325T150000
  591. DTEND;TZID=Europe/Warsaw:20170325T160000
  592. TRANSP:OPAQUE
  593. DESCRIPTION:Magiczna treść uzyskana za pomocą magicznego proszku.\n\nę
  594. żźćńłóÓŻŹĆŁĘ€śśśŚŚ\n \,\,))))))))\;\,\n
  595. __))))))))))))))\,\n \\|/ -\\(((((''''((((((((.\n -*-==///
  596. ///(('' . `))))))\,\n /|\\ ))| o \;-. '(((((
  597. \,(\,\n ( `| / ) \;))))'
  598. \,_))^\;(~\n | | | \,))((((_ _____-
  599. -----~~~-. %\,\;(\;(>'\;'~\n o_)\; \; )))(((` ~---
  600. ~ `:: \\ %%~~)(v\;(`('~\n \; ''''````
  601. `: `:::|\\\,__\,%% )\;`'\; ~\n | _
  602. ) / `:|`----' `-'\n ______/\\/~ |
  603. / /\n /~\;\;.____/\;\;' / ___--\
  604. ,-( `\;\;\;/\n / // _\;______\;'------~~~~~ /\;\;/\\ /\n
  605. // | | / \; \\\;\;\,\\\n (<_ | \;
  606. /'\,/-----' _>\n \\_| ||_
  607. //~\;~~~~~~~~~\n `\\_| (\,~~ -Tua Xiong\n
  608. \\~\\\n
  609. ~~\n\n
  610. SEQUENCE:1
  611. X-MOZ-GENERATION:1
  612. END:VEVENT
  613. END:VCALENDAR
  614. EOS;
  615. return [
  616. 'no data' => [''],
  617. 'failing on postgres' => [$data]
  618. ];
  619. }
  620. /**
  621. * @dataProvider providesSchedulingData
  622. * @param $objectData
  623. */
  624. public function testScheduling($objectData): void {
  625. $this->backend->createSchedulingObject(self::UNIT_TEST_USER, 'Sample Schedule', $objectData);
  626. $sos = $this->backend->getSchedulingObjects(self::UNIT_TEST_USER);
  627. $this->assertCount(1, $sos);
  628. $so = $this->backend->getSchedulingObject(self::UNIT_TEST_USER, 'Sample Schedule');
  629. $this->assertNotNull($so);
  630. $this->backend->deleteSchedulingObject(self::UNIT_TEST_USER, 'Sample Schedule');
  631. $sos = $this->backend->getSchedulingObjects(self::UNIT_TEST_USER);
  632. $this->assertCount(0, $sos);
  633. }
  634. /**
  635. * @dataProvider providesCalDataForGetDenormalizedData
  636. */
  637. public function testGetDenormalizedData($expected, $key, $calData): void {
  638. try {
  639. $actual = $this->backend->getDenormalizedData($calData);
  640. $this->assertEquals($expected, $actual[$key]);
  641. } catch (\Throwable $e) {
  642. if (($e->getMessage() === 'Epoch doesn\'t fit in a PHP integer') && (PHP_INT_SIZE < 8)) {
  643. $this->markTestSkipped('This fail on 32bits because of PHP limitations in DateTime');
  644. }
  645. throw $e;
  646. }
  647. }
  648. public function providesCalDataForGetDenormalizedData(): array {
  649. return [
  650. '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"],
  651. '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"],
  652. '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"],
  653. '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"],
  654. '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"],
  655. '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"],
  656. '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"],
  657. '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"],
  658. '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"],
  659. '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"],
  660. ];
  661. }
  662. public function testCalendarSearch(): void {
  663. $calendarId = $this->createTestCalendar();
  664. $uri = static::getUniqueID('calobj');
  665. $calData = <<<EOD
  666. BEGIN:VCALENDAR
  667. VERSION:2.0
  668. PRODID:ownCloud Calendar
  669. BEGIN:VEVENT
  670. CREATED;VALUE=DATE-TIME:20130910T125139Z
  671. UID:47d15e3ec8
  672. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  673. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  674. SUMMARY:Test Event
  675. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  676. DTEND;VALUE=DATE-TIME:20130912T140000Z
  677. CLASS:PUBLIC
  678. END:VEVENT
  679. END:VCALENDAR
  680. EOD;
  681. $this->backend->createCalendarObject($calendarId, $uri, $calData);
  682. $search1 = $this->backend->calendarSearch(self::UNIT_TEST_USER, [
  683. 'comps' => [
  684. 'VEVENT',
  685. 'VTODO'
  686. ],
  687. 'props' => [
  688. 'SUMMARY',
  689. 'LOCATION'
  690. ],
  691. 'search-term' => 'Test',
  692. ]);
  693. $this->assertEquals(count($search1), 1);
  694. // update the card
  695. $calData = <<<'EOD'
  696. BEGIN:VCALENDAR
  697. VERSION:2.0
  698. PRODID:ownCloud Calendar
  699. BEGIN:VEVENT
  700. CREATED;VALUE=DATE-TIME:20130910T125139Z
  701. UID:47d15e3ec8
  702. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  703. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  704. SUMMARY:123 Event 🙈
  705. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  706. DTEND;VALUE=DATE-TIME:20130912T140000Z
  707. ATTENDEE;CN=test:mailto:foo@bar.com
  708. END:VEVENT
  709. END:VCALENDAR
  710. EOD;
  711. $this->backend->updateCalendarObject($calendarId, $uri, $calData);
  712. $search2 = $this->backend->calendarSearch(self::UNIT_TEST_USER, [
  713. 'comps' => [
  714. 'VEVENT',
  715. 'VTODO'
  716. ],
  717. 'props' => [
  718. 'SUMMARY',
  719. 'LOCATION'
  720. ],
  721. 'search-term' => 'Test',
  722. ]);
  723. $this->assertEquals(count($search2), 0);
  724. $search3 = $this->backend->calendarSearch(self::UNIT_TEST_USER, [
  725. 'comps' => [
  726. 'VEVENT',
  727. 'VTODO'
  728. ],
  729. 'props' => [
  730. 'SUMMARY',
  731. 'LOCATION'
  732. ],
  733. 'params' => [
  734. [
  735. 'property' => 'ATTENDEE',
  736. 'parameter' => 'CN'
  737. ]
  738. ],
  739. 'search-term' => 'Test',
  740. ]);
  741. $this->assertEquals(count($search3), 1);
  742. // t matches both summary and attendee's CN, but we want unique results
  743. $search4 = $this->backend->calendarSearch(self::UNIT_TEST_USER, [
  744. 'comps' => [
  745. 'VEVENT',
  746. 'VTODO'
  747. ],
  748. 'props' => [
  749. 'SUMMARY',
  750. 'LOCATION'
  751. ],
  752. 'params' => [
  753. [
  754. 'property' => 'ATTENDEE',
  755. 'parameter' => 'CN'
  756. ]
  757. ],
  758. 'search-term' => 't',
  759. ]);
  760. $this->assertEquals(count($search4), 1);
  761. $this->backend->deleteCalendarObject($calendarId, $uri);
  762. $search5 = $this->backend->calendarSearch(self::UNIT_TEST_USER, [
  763. 'comps' => [
  764. 'VEVENT',
  765. 'VTODO'
  766. ],
  767. 'props' => [
  768. 'SUMMARY',
  769. 'LOCATION'
  770. ],
  771. 'params' => [
  772. [
  773. 'property' => 'ATTENDEE',
  774. 'parameter' => 'CN'
  775. ]
  776. ],
  777. 'search-term' => 't',
  778. ]);
  779. $this->assertEquals(count($search5), 0);
  780. }
  781. /**
  782. * @dataProvider searchDataProvider
  783. */
  784. public function testSearch(bool $isShared, array $searchOptions, int $count): void {
  785. $calendarId = $this->createTestCalendar();
  786. $uris = [];
  787. $calData = [];
  788. $uris[] = static::getUniqueID('calobj');
  789. $calData[] = <<<EOD
  790. BEGIN:VCALENDAR
  791. VERSION:2.0
  792. PRODID:Nextcloud Calendar
  793. BEGIN:VEVENT
  794. CREATED;VALUE=DATE-TIME:20130910T125139Z
  795. UID:47d15e3ec8-1
  796. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  797. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  798. SUMMARY:Test Event
  799. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  800. DTEND;VALUE=DATE-TIME:20130912T140000Z
  801. CLASS:PUBLIC
  802. END:VEVENT
  803. END:VCALENDAR
  804. EOD;
  805. $uris[] = static::getUniqueID('calobj');
  806. $calData[] = <<<EOD
  807. BEGIN:VCALENDAR
  808. VERSION:2.0
  809. PRODID:Nextcloud Calendar
  810. BEGIN:VEVENT
  811. CREATED;VALUE=DATE-TIME:20130910T125139Z
  812. UID:47d15e3ec8-2
  813. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  814. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  815. SUMMARY:123
  816. LOCATION:Test
  817. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  818. DTEND;VALUE=DATE-TIME:20130912T140000Z
  819. CLASS:PUBLIC
  820. END:VEVENT
  821. END:VCALENDAR
  822. EOD;
  823. $uris[] = static::getUniqueID('calobj');
  824. $calData[] = <<<EOD
  825. BEGIN:VCALENDAR
  826. VERSION:2.0
  827. PRODID:Nextcloud Calendar
  828. BEGIN:VEVENT
  829. CREATED;VALUE=DATE-TIME:20130910T125139Z
  830. UID:47d15e3ec8-3
  831. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  832. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  833. SUMMARY:123
  834. ATTENDEE;CN=test:mailto:foo@bar.com
  835. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  836. DTEND;VALUE=DATE-TIME:20130912T140000Z
  837. CLASS:PRIVATE
  838. END:VEVENT
  839. END:VCALENDAR
  840. EOD;
  841. $uris[] = static::getUniqueID('calobj');
  842. $calData[] = <<<EOD
  843. BEGIN:VCALENDAR
  844. VERSION:2.0
  845. PRODID:Nextcloud Calendar
  846. BEGIN:VEVENT
  847. CREATED;VALUE=DATE-TIME:20130910T125139Z
  848. UID:47d15e3ec8-4
  849. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  850. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  851. SUMMARY:123
  852. ATTENDEE;CN=foobar:mailto:test@bar.com
  853. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  854. DTEND;VALUE=DATE-TIME:20130912T140000Z
  855. CLASS:CONFIDENTIAL
  856. END:VEVENT
  857. END:VCALENDAR
  858. EOD;
  859. $uriCount = count($uris);
  860. for ($i = 0; $i < $uriCount; $i++) {
  861. $this->backend->createCalendarObject($calendarId,
  862. $uris[$i], $calData[$i]);
  863. }
  864. $calendarInfo = [
  865. 'id' => $calendarId,
  866. 'principaluri' => 'user1',
  867. '{http://owncloud.org/ns}owner-principal' => $isShared ? 'user2' : 'user1',
  868. ];
  869. $result = $this->backend->search($calendarInfo, 'Test',
  870. ['SUMMARY', 'LOCATION', 'ATTENDEE'], $searchOptions, null, null);
  871. $this->assertCount($count, $result);
  872. }
  873. public function searchDataProvider() {
  874. return [
  875. [false, [], 4],
  876. [true, ['timerange' => ['start' => new DateTime('2013-09-12 13:00:00'), 'end' => new DateTime('2013-09-12 14:00:00')]], 2],
  877. [true, ['timerange' => ['start' => new DateTime('2013-09-12 15:00:00'), 'end' => new DateTime('2013-09-12 16:00:00')]], 0],
  878. ];
  879. }
  880. public function testSameUriSameIdForDifferentCalendarTypes(): void {
  881. $calendarId = $this->createTestCalendar();
  882. $subscriptionId = $this->createTestSubscription();
  883. $uri = static::getUniqueID('calobj');
  884. $calData = <<<EOD
  885. BEGIN:VCALENDAR
  886. VERSION:2.0
  887. PRODID:ownCloud Calendar
  888. BEGIN:VEVENT
  889. CREATED;VALUE=DATE-TIME:20130910T125139Z
  890. UID:47d15e3ec8
  891. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  892. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  893. SUMMARY:Test Event
  894. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  895. DTEND;VALUE=DATE-TIME:20130912T140000Z
  896. CLASS:PUBLIC
  897. END:VEVENT
  898. END:VCALENDAR
  899. EOD;
  900. $calData2 = <<<EOD
  901. BEGIN:VCALENDAR
  902. VERSION:2.0
  903. PRODID:ownCloud Calendar
  904. BEGIN:VEVENT
  905. CREATED;VALUE=DATE-TIME:20130910T125139Z
  906. UID:47d15e3ec8
  907. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  908. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  909. SUMMARY:Test Event 123
  910. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  911. DTEND;VALUE=DATE-TIME:20130912T140000Z
  912. CLASS:PUBLIC
  913. END:VEVENT
  914. END:VCALENDAR
  915. EOD;
  916. $this->backend->createCalendarObject($calendarId, $uri, $calData);
  917. $this->backend->createCalendarObject($subscriptionId, $uri, $calData2, CalDavBackend::CALENDAR_TYPE_SUBSCRIPTION);
  918. $this->assertEquals($calData, $this->backend->getCalendarObject($calendarId, $uri, CalDavBackend::CALENDAR_TYPE_CALENDAR)['calendardata']);
  919. $this->assertEquals($calData2, $this->backend->getCalendarObject($subscriptionId, $uri, CalDavBackend::CALENDAR_TYPE_SUBSCRIPTION)['calendardata']);
  920. }
  921. public function testPurgeAllCachedEventsForSubscription(): void {
  922. $subscriptionId = $this->createTestSubscription();
  923. $uri = static::getUniqueID('calobj');
  924. $calData = <<<EOD
  925. BEGIN:VCALENDAR
  926. VERSION:2.0
  927. PRODID:ownCloud Calendar
  928. BEGIN:VEVENT
  929. CREATED;VALUE=DATE-TIME:20130910T125139Z
  930. UID:47d15e3ec8
  931. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  932. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  933. SUMMARY:Test Event
  934. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  935. DTEND;VALUE=DATE-TIME:20130912T140000Z
  936. CLASS:PUBLIC
  937. END:VEVENT
  938. END:VCALENDAR
  939. EOD;
  940. $this->backend->createCalendarObject($subscriptionId, $uri, $calData, CalDavBackend::CALENDAR_TYPE_SUBSCRIPTION);
  941. $this->backend->purgeAllCachedEventsForSubscription($subscriptionId);
  942. $this->assertEquals(null, $this->backend->getCalendarObject($subscriptionId, $uri, CalDavBackend::CALENDAR_TYPE_SUBSCRIPTION));
  943. }
  944. public function testCalendarMovement(): void {
  945. $this->backend->createCalendar(self::UNIT_TEST_USER, 'Example', []);
  946. $this->assertCount(1, $this->backend->getCalendarsForUser(self::UNIT_TEST_USER));
  947. $calendarInfoUser = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER)[0];
  948. $this->backend->moveCalendar('Example', self::UNIT_TEST_USER, self::UNIT_TEST_USER1);
  949. $this->assertCount(0, $this->backend->getCalendarsForUser(self::UNIT_TEST_USER));
  950. $this->assertCount(1, $this->backend->getCalendarsForUser(self::UNIT_TEST_USER1));
  951. $calendarInfoUser1 = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER1)[0];
  952. $this->assertEquals($calendarInfoUser['id'], $calendarInfoUser1['id']);
  953. $this->assertEquals($calendarInfoUser['uri'], $calendarInfoUser1['uri']);
  954. }
  955. public function testSearchPrincipal(): void {
  956. $myPublic = <<<EOD
  957. BEGIN:VCALENDAR
  958. VERSION:2.0
  959. PRODID:-//dmfs.org//mimedir.icalendar//EN
  960. BEGIN:VTIMEZONE
  961. TZID:Europe/Berlin
  962. X-LIC-LOCATION:Europe/Berlin
  963. BEGIN:DAYLIGHT
  964. TZOFFSETFROM:+0100
  965. TZOFFSETTO:+0200
  966. TZNAME:CEST
  967. DTSTART:19700329T020000
  968. RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
  969. END:DAYLIGHT
  970. BEGIN:STANDARD
  971. TZOFFSETFROM:+0200
  972. TZOFFSETTO:+0100
  973. TZNAME:CET
  974. DTSTART:19701025T030000
  975. RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
  976. END:STANDARD
  977. END:VTIMEZONE
  978. BEGIN:VEVENT
  979. DTSTART;TZID=Europe/Berlin:20160419T130000
  980. SUMMARY:My Test (public)
  981. CLASS:PUBLIC
  982. TRANSP:OPAQUE
  983. STATUS:CONFIRMED
  984. DTEND;TZID=Europe/Berlin:20160419T140000
  985. LAST-MODIFIED:20160419T074202Z
  986. DTSTAMP:20160419T074202Z
  987. CREATED:20160419T074202Z
  988. UID:2e468c48-7860-492e-bc52-92fa0daeeccf.1461051722310-1
  989. END:VEVENT
  990. END:VCALENDAR
  991. EOD;
  992. $myPrivate = <<<EOD
  993. BEGIN:VCALENDAR
  994. VERSION:2.0
  995. PRODID:-//dmfs.org//mimedir.icalendar//EN
  996. BEGIN:VTIMEZONE
  997. TZID:Europe/Berlin
  998. X-LIC-LOCATION:Europe/Berlin
  999. BEGIN:DAYLIGHT
  1000. TZOFFSETFROM:+0100
  1001. TZOFFSETTO:+0200
  1002. TZNAME:CEST
  1003. DTSTART:19700329T020000
  1004. RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
  1005. END:DAYLIGHT
  1006. BEGIN:STANDARD
  1007. TZOFFSETFROM:+0200
  1008. TZOFFSETTO:+0100
  1009. TZNAME:CET
  1010. DTSTART:19701025T030000
  1011. RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
  1012. END:STANDARD
  1013. END:VTIMEZONE
  1014. BEGIN:VEVENT
  1015. DTSTART;TZID=Europe/Berlin:20160419T130000
  1016. SUMMARY:My Test (private)
  1017. CLASS:PRIVATE
  1018. TRANSP:OPAQUE
  1019. STATUS:CONFIRMED
  1020. DTEND;TZID=Europe/Berlin:20160419T140000
  1021. LAST-MODIFIED:20160419T074202Z
  1022. DTSTAMP:20160419T074202Z
  1023. CREATED:20160419T074202Z
  1024. UID:2e468c48-7860-492e-bc52-92fa0daeeccf.1461051722310-2
  1025. END:VEVENT
  1026. END:VCALENDAR
  1027. EOD;
  1028. $myConfidential = <<<EOD
  1029. BEGIN:VCALENDAR
  1030. VERSION:2.0
  1031. PRODID:-//dmfs.org//mimedir.icalendar//EN
  1032. BEGIN:VTIMEZONE
  1033. TZID:Europe/Berlin
  1034. X-LIC-LOCATION:Europe/Berlin
  1035. BEGIN:DAYLIGHT
  1036. TZOFFSETFROM:+0100
  1037. TZOFFSETTO:+0200
  1038. TZNAME:CEST
  1039. DTSTART:19700329T020000
  1040. RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
  1041. END:DAYLIGHT
  1042. BEGIN:STANDARD
  1043. TZOFFSETFROM:+0200
  1044. TZOFFSETTO:+0100
  1045. TZNAME:CET
  1046. DTSTART:19701025T030000
  1047. RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
  1048. END:STANDARD
  1049. END:VTIMEZONE
  1050. BEGIN:VEVENT
  1051. DTSTART;TZID=Europe/Berlin:20160419T130000
  1052. SUMMARY:My Test (confidential)
  1053. CLASS:CONFIDENTIAL
  1054. TRANSP:OPAQUE
  1055. STATUS:CONFIRMED
  1056. DTEND;TZID=Europe/Berlin:20160419T140000
  1057. LAST-MODIFIED:20160419T074202Z
  1058. DTSTAMP:20160419T074202Z
  1059. CREATED:20160419T074202Z
  1060. UID:2e468c48-7860-492e-bc52-92fa0daeeccf.1461051722310-3
  1061. END:VEVENT
  1062. END:VCALENDAR
  1063. EOD;
  1064. $sharerPublic = <<<EOD
  1065. BEGIN:VCALENDAR
  1066. VERSION:2.0
  1067. PRODID:-//dmfs.org//mimedir.icalendar//EN
  1068. BEGIN:VTIMEZONE
  1069. TZID:Europe/Berlin
  1070. X-LIC-LOCATION:Europe/Berlin
  1071. BEGIN:DAYLIGHT
  1072. TZOFFSETFROM:+0100
  1073. TZOFFSETTO:+0200
  1074. TZNAME:CEST
  1075. DTSTART:19700329T020000
  1076. RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
  1077. END:DAYLIGHT
  1078. BEGIN:STANDARD
  1079. TZOFFSETFROM:+0200
  1080. TZOFFSETTO:+0100
  1081. TZNAME:CET
  1082. DTSTART:19701025T030000
  1083. RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
  1084. END:STANDARD
  1085. END:VTIMEZONE
  1086. BEGIN:VEVENT
  1087. DTSTART;TZID=Europe/Berlin:20160419T130000
  1088. SUMMARY:Sharer Test (public)
  1089. CLASS:PUBLIC
  1090. TRANSP:OPAQUE
  1091. STATUS:CONFIRMED
  1092. DTEND;TZID=Europe/Berlin:20160419T140000
  1093. LAST-MODIFIED:20160419T074202Z
  1094. DTSTAMP:20160419T074202Z
  1095. CREATED:20160419T074202Z
  1096. UID:2e468c48-7860-492e-bc52-92fa0daeeccf.1461051722310-4
  1097. END:VEVENT
  1098. END:VCALENDAR
  1099. EOD;
  1100. $sharerPrivate = <<<EOD
  1101. BEGIN:VCALENDAR
  1102. VERSION:2.0
  1103. PRODID:-//dmfs.org//mimedir.icalendar//EN
  1104. BEGIN:VTIMEZONE
  1105. TZID:Europe/Berlin
  1106. X-LIC-LOCATION:Europe/Berlin
  1107. BEGIN:DAYLIGHT
  1108. TZOFFSETFROM:+0100
  1109. TZOFFSETTO:+0200
  1110. TZNAME:CEST
  1111. DTSTART:19700329T020000
  1112. RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
  1113. END:DAYLIGHT
  1114. BEGIN:STANDARD
  1115. TZOFFSETFROM:+0200
  1116. TZOFFSETTO:+0100
  1117. TZNAME:CET
  1118. DTSTART:19701025T030000
  1119. RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
  1120. END:STANDARD
  1121. END:VTIMEZONE
  1122. BEGIN:VEVENT
  1123. DTSTART;TZID=Europe/Berlin:20160419T130000
  1124. SUMMARY:Sharer Test (private)
  1125. CLASS:PRIVATE
  1126. TRANSP:OPAQUE
  1127. STATUS:CONFIRMED
  1128. DTEND;TZID=Europe/Berlin:20160419T140000
  1129. LAST-MODIFIED:20160419T074202Z
  1130. DTSTAMP:20160419T074202Z
  1131. CREATED:20160419T074202Z
  1132. UID:2e468c48-7860-492e-bc52-92fa0daeeccf.1461051722310-5
  1133. END:VEVENT
  1134. END:VCALENDAR
  1135. EOD;
  1136. $sharerConfidential = <<<EOD
  1137. BEGIN:VCALENDAR
  1138. VERSION:2.0
  1139. PRODID:-//dmfs.org//mimedir.icalendar//EN
  1140. BEGIN:VTIMEZONE
  1141. TZID:Europe/Berlin
  1142. X-LIC-LOCATION:Europe/Berlin
  1143. BEGIN:DAYLIGHT
  1144. TZOFFSETFROM:+0100
  1145. TZOFFSETTO:+0200
  1146. TZNAME:CEST
  1147. DTSTART:19700329T020000
  1148. RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
  1149. END:DAYLIGHT
  1150. BEGIN:STANDARD
  1151. TZOFFSETFROM:+0200
  1152. TZOFFSETTO:+0100
  1153. TZNAME:CET
  1154. DTSTART:19701025T030000
  1155. RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
  1156. END:STANDARD
  1157. END:VTIMEZONE
  1158. BEGIN:VEVENT
  1159. DTSTART;TZID=Europe/Berlin:20160419T130000
  1160. SUMMARY:Sharer Test (confidential)
  1161. CLASS:CONFIDENTIAL
  1162. TRANSP:OPAQUE
  1163. STATUS:CONFIRMED
  1164. DTEND;TZID=Europe/Berlin:20160419T140000
  1165. LAST-MODIFIED:20160419T074202Z
  1166. DTSTAMP:20160419T074202Z
  1167. CREATED:20160419T074202Z
  1168. UID:2e468c48-7860-492e-bc52-92fa0daeeccf.1461051722310-6
  1169. END:VEVENT
  1170. END:VCALENDAR
  1171. EOD;
  1172. $l10n = $this->createMock(IL10N::class);
  1173. $l10n
  1174. ->expects($this->any())
  1175. ->method('t')
  1176. ->willReturnCallback(function ($text, $parameters = []) {
  1177. return vsprintf($text, $parameters);
  1178. });
  1179. $config = $this->createMock(IConfig::class);
  1180. $this->userManager->expects($this->any())
  1181. ->method('userExists')
  1182. ->willReturn(true);
  1183. $this->groupManager->expects($this->any())
  1184. ->method('groupExists')
  1185. ->willReturn(true);
  1186. $this->principal->expects(self::atLeastOnce())
  1187. ->method('findByUri')
  1188. ->willReturn(self::UNIT_TEST_USER);
  1189. $me = self::UNIT_TEST_USER;
  1190. $sharer = self::UNIT_TEST_USER1;
  1191. $this->backend->createCalendar($me, 'calendar-uri-me', []);
  1192. $this->backend->createCalendar($sharer, 'calendar-uri-sharer', []);
  1193. $myCalendars = $this->backend->getCalendarsForUser($me);
  1194. $this->assertCount(1, $myCalendars);
  1195. $sharerCalendars = $this->backend->getCalendarsForUser($sharer);
  1196. $this->assertCount(1, $sharerCalendars);
  1197. $logger = $this->createMock(\Psr\Log\LoggerInterface::class);
  1198. $sharerCalendar = new Calendar($this->backend, $sharerCalendars[0], $l10n, $config, $logger);
  1199. $this->backend->updateShares($sharerCalendar, [
  1200. [
  1201. 'href' => 'principal:' . $me,
  1202. 'readOnly' => false,
  1203. ],
  1204. ], []);
  1205. $this->assertCount(2, $this->backend->getCalendarsForUser($me));
  1206. $this->backend->createCalendarObject($myCalendars[0]['id'], 'event0.ics', $myPublic);
  1207. $this->backend->createCalendarObject($myCalendars[0]['id'], 'event1.ics', $myPrivate);
  1208. $this->backend->createCalendarObject($myCalendars[0]['id'], 'event2.ics', $myConfidential);
  1209. $this->backend->createCalendarObject($sharerCalendars[0]['id'], 'event3.ics', $sharerPublic);
  1210. $this->backend->createCalendarObject($sharerCalendars[0]['id'], 'event4.ics', $sharerPrivate);
  1211. $this->backend->createCalendarObject($sharerCalendars[0]['id'], 'event5.ics', $sharerConfidential);
  1212. $mySearchResults = $this->backend->searchPrincipalUri($me, 'Test', ['VEVENT'], ['SUMMARY'], []);
  1213. $sharerSearchResults = $this->backend->searchPrincipalUri($sharer, 'Test', ['VEVENT'], ['SUMMARY'], []);
  1214. $this->assertCount(4, $mySearchResults);
  1215. $this->assertCount(3, $sharerSearchResults);
  1216. $this->assertEquals($myPublic, $mySearchResults[0]['calendardata']);
  1217. $this->assertEquals($myPrivate, $mySearchResults[1]['calendardata']);
  1218. $this->assertEquals($myConfidential, $mySearchResults[2]['calendardata']);
  1219. $this->assertEquals($sharerPublic, $mySearchResults[3]['calendardata']);
  1220. $this->assertEquals($sharerPublic, $sharerSearchResults[0]['calendardata']);
  1221. $this->assertEquals($sharerPrivate, $sharerSearchResults[1]['calendardata']);
  1222. $this->assertEquals($sharerConfidential, $sharerSearchResults[2]['calendardata']);
  1223. }
  1224. /**
  1225. * @throws \OCP\DB\Exception
  1226. * @throws \Sabre\DAV\Exception\BadRequest
  1227. */
  1228. public function testPruneOutdatedSyncTokens(): void {
  1229. $calendarId = $this->createTestCalendar();
  1230. $changes = $this->backend->getChangesForCalendar($calendarId, '', 1);
  1231. $syncToken = $changes['syncToken'];
  1232. $uri = static::getUniqueID('calobj');
  1233. $calData = <<<EOD
  1234. BEGIN:VCALENDAR
  1235. VERSION:2.0
  1236. PRODID:Nextcloud Calendar
  1237. BEGIN:VEVENT
  1238. CREATED;VALUE=DATE-TIME:20130910T125139Z
  1239. UID:47d15e3ec8
  1240. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  1241. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  1242. SUMMARY:Test Event
  1243. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  1244. DTEND;VALUE=DATE-TIME:20130912T140000Z
  1245. CLASS:PUBLIC
  1246. END:VEVENT
  1247. END:VCALENDAR
  1248. EOD;
  1249. $this->backend->createCalendarObject($calendarId, $uri, $calData);
  1250. // update the card
  1251. $calData = <<<'EOD'
  1252. BEGIN:VCALENDAR
  1253. VERSION:2.0
  1254. PRODID:Nextcloud Calendar
  1255. BEGIN:VEVENT
  1256. CREATED;VALUE=DATE-TIME:20130910T125139Z
  1257. UID:47d15e3ec8
  1258. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  1259. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  1260. SUMMARY:123 Event 🙈
  1261. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  1262. DTEND;VALUE=DATE-TIME:20130912T140000Z
  1263. ATTENDEE;CN=test:mailto:foo@bar.com
  1264. END:VEVENT
  1265. END:VCALENDAR
  1266. EOD;
  1267. $this->backend->updateCalendarObject($calendarId, $uri, $calData);
  1268. // Keep everything
  1269. $deleted = $this->backend->pruneOutdatedSyncTokens(0, 0);
  1270. self::assertSame(0, $deleted);
  1271. $deleted = $this->backend->pruneOutdatedSyncTokens(0, time());
  1272. // At least one from the object creation and one from the object update
  1273. $this->assertGreaterThanOrEqual(2, $deleted);
  1274. $changes = $this->backend->getChangesForCalendar($calendarId, $syncToken, 1);
  1275. $this->assertEmpty($changes['added']);
  1276. $this->assertEmpty($changes['modified']);
  1277. $this->assertEmpty($changes['deleted']);
  1278. // Test that objects remain
  1279. // Currently changes are empty
  1280. $changes = $this->backend->getChangesForCalendar($calendarId, $syncToken, 100);
  1281. $this->assertEquals(0, count($changes['added'] + $changes['modified'] + $changes['deleted']));
  1282. // Create card
  1283. $uri = static::getUniqueID('calobj');
  1284. $calData = <<<EOD
  1285. BEGIN:VCALENDAR
  1286. VERSION:2.0
  1287. PRODID:Nextcloud Calendar
  1288. BEGIN:VEVENT
  1289. CREATED;VALUE=DATE-TIME:20230910T125139Z
  1290. UID:47d15e3ec9
  1291. LAST-MODIFIED;VALUE=DATE-TIME:20230910T125139Z
  1292. DTSTAMP;VALUE=DATE-TIME:20230910T125139Z
  1293. SUMMARY:Test Event
  1294. DTSTART;VALUE=DATE-TIME:20230912T130000Z
  1295. DTEND;VALUE=DATE-TIME:20230912T140000Z
  1296. CLASS:PUBLIC
  1297. END:VEVENT
  1298. END:VCALENDAR
  1299. EOD;
  1300. $this->backend->createCalendarObject($calendarId, $uri, $calData);
  1301. // We now have one add
  1302. $changes = $this->backend->getChangesForCalendar($calendarId, $syncToken, 100);
  1303. $this->assertEquals(1, count($changes['added']));
  1304. $this->assertEmpty($changes['modified']);
  1305. $this->assertEmpty($changes['deleted']);
  1306. // update the card
  1307. $calData = <<<'EOD'
  1308. BEGIN:VCALENDAR
  1309. VERSION:2.0
  1310. PRODID:Nextcloud Calendar
  1311. BEGIN:VEVENT
  1312. CREATED;VALUE=DATE-TIME:20230910T125139Z
  1313. UID:47d15e3ec9
  1314. LAST-MODIFIED;VALUE=DATE-TIME:20230910T125139Z
  1315. DTSTAMP;VALUE=DATE-TIME:20230910T125139Z
  1316. SUMMARY:123 Event 🙈
  1317. DTSTART;VALUE=DATE-TIME:20230912T130000Z
  1318. DTEND;VALUE=DATE-TIME:20230912T140000Z
  1319. ATTENDEE;CN=test:mailto:foo@bar.com
  1320. END:VEVENT
  1321. END:VCALENDAR
  1322. EOD;
  1323. $this->backend->updateCalendarObject($calendarId, $uri, $calData);
  1324. // One add, one modify, but shortened to modify
  1325. $changes = $this->backend->getChangesForCalendar($calendarId, $syncToken, 100);
  1326. $this->assertEmpty($changes['added']);
  1327. $this->assertEquals(1, count($changes['modified']));
  1328. $this->assertEmpty($changes['deleted']);
  1329. // Delete all but last change
  1330. $deleted = $this->backend->pruneOutdatedSyncTokens(1, time());
  1331. $this->assertEquals(1, $deleted); // We had two changes before, now one
  1332. // Only update should remain
  1333. $changes = $this->backend->getChangesForCalendar($calendarId, $syncToken, 100);
  1334. $this->assertEmpty($changes['added']);
  1335. $this->assertEquals(1, count($changes['modified']));
  1336. $this->assertEmpty($changes['deleted']);
  1337. // Check that no crash occurs when prune is called without current changes
  1338. $deleted = $this->backend->pruneOutdatedSyncTokens(1, time());
  1339. self::assertSame(0, $deleted);
  1340. }
  1341. public function testSearchAndExpandRecurrences(): void {
  1342. $calendarId = $this->createTestCalendar();
  1343. $calendarInfo = [
  1344. 'id' => $calendarId,
  1345. 'principaluri' => 'user1',
  1346. '{http://owncloud.org/ns}owner-principal' => 'user1',
  1347. ];
  1348. $calData = <<<'EOD'
  1349. BEGIN:VCALENDAR
  1350. PRODID:-//IDN nextcloud.com//Calendar app 4.5.0-alpha.2//EN
  1351. CALSCALE:GREGORIAN
  1352. VERSION:2.0
  1353. BEGIN:VEVENT
  1354. CREATED:20230921T133401Z
  1355. DTSTAMP:20230921T133448Z
  1356. LAST-MODIFIED:20230921T133448Z
  1357. SEQUENCE:2
  1358. UID:7b7d5d12-683c-48ce-973a-b3e1cb0bae2a
  1359. DTSTART;VALUE=DATE:20230912
  1360. DTEND;VALUE=DATE:20230913
  1361. STATUS:CONFIRMED
  1362. SUMMARY:Daily Event
  1363. RRULE:FREQ=DAILY
  1364. END:VEVENT
  1365. END:VCALENDAR
  1366. EOD;
  1367. $uri = static::getUniqueID('calobj');
  1368. $this->backend->createCalendarObject($calendarId, $uri, $calData);
  1369. $start = new DateTimeImmutable('2023-09-20T00:00:00Z');
  1370. $end = $start->add(new DateInterval('P14D'));
  1371. $results = $this->backend->search(
  1372. $calendarInfo,
  1373. '',
  1374. [],
  1375. [
  1376. 'timerange' => [
  1377. 'start' => $start,
  1378. 'end' => $end,
  1379. ]
  1380. ],
  1381. null,
  1382. null,
  1383. );
  1384. $this->assertCount(1, $results);
  1385. $this->assertCount(14, $results[0]['objects']);
  1386. foreach ($results as $result) {
  1387. foreach ($result['objects'] as $object) {
  1388. $this->assertEquals($object['UID'][0], '7b7d5d12-683c-48ce-973a-b3e1cb0bae2a');
  1389. $this->assertEquals($object['SUMMARY'][0], 'Daily Event');
  1390. $this->assertGreaterThanOrEqual(
  1391. $start->getTimestamp(),
  1392. $object['DTSTART'][0]->getTimestamp(),
  1393. 'Recurrence starting before requested start',
  1394. );
  1395. $this->assertLessThanOrEqual(
  1396. $end->getTimestamp(),
  1397. $object['DTSTART'][0]->getTimestamp(),
  1398. 'Recurrence starting after requested end',
  1399. );
  1400. }
  1401. }
  1402. }
  1403. public function testRestoreChanges(): void {
  1404. $calendarId = $this->createTestCalendar();
  1405. $uri1 = static::getUniqueID('calobj1') . '.ics';
  1406. $calData = <<<EOD
  1407. BEGIN:VCALENDAR
  1408. VERSION:2.0
  1409. PRODID:Nextcloud Calendar
  1410. BEGIN:VEVENT
  1411. CREATED;VALUE=DATE-TIME:20130910T125139Z
  1412. UID:47d15e3ec8
  1413. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  1414. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  1415. SUMMARY:Test Event
  1416. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  1417. DTEND;VALUE=DATE-TIME:20130912T140000Z
  1418. CLASS:PUBLIC
  1419. END:VEVENT
  1420. END:VCALENDAR
  1421. EOD;
  1422. $this->backend->createCalendarObject($calendarId, $uri1, $calData);
  1423. $calData = <<<EOD
  1424. BEGIN:VCALENDAR
  1425. VERSION:2.0
  1426. PRODID:Nextcloud Calendar
  1427. BEGIN:VEVENT
  1428. CREATED;VALUE=DATE-TIME:20130910T125139Z
  1429. UID:47d15e3ec8
  1430. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  1431. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  1432. SUMMARY:Test Event – UPDATED
  1433. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  1434. DTEND;VALUE=DATE-TIME:20130912T140000Z
  1435. CLASS:PUBLIC
  1436. SEQUENCE:1
  1437. END:VEVENT
  1438. END:VCALENDAR
  1439. EOD;
  1440. $this->backend->updateCalendarObject($calendarId, $uri1, $calData);
  1441. $uri2 = static::getUniqueID('calobj2') . '.ics';
  1442. $calData = <<<EOD
  1443. BEGIN:VCALENDAR
  1444. VERSION:2.0
  1445. PRODID:Nextcloud Calendar
  1446. BEGIN:VEVENT
  1447. CREATED;VALUE=DATE-TIME:20130910T125139Z
  1448. UID:47d15e3ec9
  1449. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  1450. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  1451. SUMMARY:Test Event
  1452. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  1453. DTEND;VALUE=DATE-TIME:20130912T140000Z
  1454. CLASS:PUBLIC
  1455. END:VEVENT
  1456. END:VCALENDAR
  1457. EOD;
  1458. $this->backend->createCalendarObject($calendarId, $uri2, $calData);
  1459. $changesBefore = $this->backend->getChangesForCalendar($calendarId, null, 1);
  1460. $this->backend->deleteCalendarObject($calendarId, $uri2);
  1461. $uri3 = static::getUniqueID('calobj3') . '.ics';
  1462. $calData = <<<EOD
  1463. BEGIN:VCALENDAR
  1464. VERSION:2.0
  1465. PRODID:Nextcloud Calendar
  1466. BEGIN:VEVENT
  1467. CREATED;VALUE=DATE-TIME:20130910T125139Z
  1468. UID:47d15e3e10
  1469. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  1470. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  1471. SUMMARY:Test Event
  1472. DTSTART;VALUE=DATE-TIME:20130912T130000Z
  1473. DTEND;VALUE=DATE-TIME:20130912T140000Z
  1474. CLASS:PUBLIC
  1475. END:VEVENT
  1476. END:VCALENDAR
  1477. EOD;
  1478. $this->backend->createCalendarObject($calendarId, $uri3, $calData);
  1479. $deleteChanges = $this->db->getQueryBuilder();
  1480. $deleteChanges->delete('calendarchanges')
  1481. ->where($deleteChanges->expr()->eq('calendarid', $deleteChanges->createNamedParameter($calendarId)));
  1482. $deleteChanges->executeStatement();
  1483. $this->backend->restoreChanges($calendarId);
  1484. $changesAfter = $this->backend->getChangesForCalendar($calendarId, $changesBefore['syncToken'], 1);
  1485. self::assertEquals([], $changesAfter['added']);
  1486. self::assertEqualsCanonicalizing([$uri1, $uri3], $changesAfter['modified']);
  1487. self::assertEquals([$uri2], $changesAfter['deleted']);
  1488. }
  1489. public function testSearchWithLimitAndTimeRange(): void {
  1490. $calendarId = $this->createTestCalendar();
  1491. $calendarInfo = [
  1492. 'id' => $calendarId,
  1493. 'principaluri' => 'user1',
  1494. '{http://owncloud.org/ns}owner-principal' => 'user1',
  1495. ];
  1496. $testFiles = [
  1497. __DIR__ . '/../test_fixtures/caldav-search-limit-timerange-1.ics',
  1498. __DIR__ . '/../test_fixtures/caldav-search-limit-timerange-2.ics',
  1499. __DIR__ . '/../test_fixtures/caldav-search-limit-timerange-3.ics',
  1500. __DIR__ . '/../test_fixtures/caldav-search-limit-timerange-4.ics',
  1501. __DIR__ . '/../test_fixtures/caldav-search-limit-timerange-5.ics',
  1502. __DIR__ . '/../test_fixtures/caldav-search-limit-timerange-6.ics',
  1503. ];
  1504. foreach ($testFiles as $testFile) {
  1505. $objectUri = static::getUniqueID('search-limit-timerange-');
  1506. $calendarData = \file_get_contents($testFile);
  1507. $this->backend->createCalendarObject($calendarId, $objectUri, $calendarData);
  1508. }
  1509. $start = new DateTimeImmutable('2024-05-06T00:00:00Z');
  1510. $end = $start->add(new DateInterval('P14D'));
  1511. $results = $this->backend->search(
  1512. $calendarInfo,
  1513. '',
  1514. [],
  1515. [
  1516. 'timerange' => [
  1517. 'start' => $start,
  1518. 'end' => $end,
  1519. ]
  1520. ],
  1521. 4,
  1522. null,
  1523. );
  1524. $this->assertCount(2, $results);
  1525. $this->assertEquals('Cake Tasting', $results[0]['objects'][0]['SUMMARY'][0]);
  1526. $this->assertGreaterThanOrEqual(
  1527. $start->getTimestamp(),
  1528. $results[0]['objects'][0]['DTSTART'][0]->getTimestamp(),
  1529. 'Recurrence starting before requested start',
  1530. );
  1531. $this->assertEquals('Pasta Day', $results[1]['objects'][0]['SUMMARY'][0]);
  1532. $this->assertGreaterThanOrEqual(
  1533. $start->getTimestamp(),
  1534. $results[1]['objects'][0]['DTSTART'][0]->getTimestamp(),
  1535. 'Recurrence starting before requested start',
  1536. );
  1537. }
  1538. public function testSearchWithLimitAndTimeRangeShouldNotReturnMoreObjectsThenLimit(): void {
  1539. $calendarId = $this->createTestCalendar();
  1540. $calendarInfo = [
  1541. 'id' => $calendarId,
  1542. 'principaluri' => 'user1',
  1543. '{http://owncloud.org/ns}owner-principal' => 'user1',
  1544. ];
  1545. $testFiles = [
  1546. __DIR__ . '/../test_fixtures/caldav-search-limit-timerange-1.ics',
  1547. __DIR__ . '/../test_fixtures/caldav-search-limit-timerange-2.ics',
  1548. __DIR__ . '/../test_fixtures/caldav-search-limit-timerange-3.ics',
  1549. __DIR__ . '/../test_fixtures/caldav-search-limit-timerange-4.ics',
  1550. __DIR__ . '/../test_fixtures/caldav-search-limit-timerange-5.ics',
  1551. __DIR__ . '/../test_fixtures/caldav-search-limit-timerange-6.ics',
  1552. ];
  1553. foreach ($testFiles as $testFile) {
  1554. $objectUri = static::getUniqueID('search-limit-timerange-');
  1555. $calendarData = \file_get_contents($testFile);
  1556. $this->backend->createCalendarObject($calendarId, $objectUri, $calendarData);
  1557. }
  1558. $start = new DateTimeImmutable('2024-05-06T00:00:00Z');
  1559. $end = $start->add(new DateInterval('P14D'));
  1560. $results = $this->backend->search(
  1561. $calendarInfo,
  1562. '',
  1563. [],
  1564. [
  1565. 'timerange' => [
  1566. 'start' => $start,
  1567. 'end' => $end,
  1568. ]
  1569. ],
  1570. 1,
  1571. null,
  1572. );
  1573. $this->assertCount(1, $results);
  1574. $this->assertEquals('Cake Tasting', $results[0]['objects'][0]['SUMMARY'][0]);
  1575. $this->assertGreaterThanOrEqual(
  1576. $start->getTimestamp(),
  1577. $results[0]['objects'][0]['DTSTART'][0]->getTimestamp(),
  1578. 'Recurrence starting before requested start',
  1579. );
  1580. }
  1581. public function testSearchWithLimitAndTimeRangeShouldReturnObjectsInTheSameOrder(): void {
  1582. $calendarId = $this->createTestCalendar();
  1583. $calendarInfo = [
  1584. 'id' => $calendarId,
  1585. 'principaluri' => 'user1',
  1586. '{http://owncloud.org/ns}owner-principal' => 'user1',
  1587. ];
  1588. $testFiles = [
  1589. __DIR__ . '/../test_fixtures/caldav-search-limit-timerange-1.ics',
  1590. __DIR__ . '/../test_fixtures/caldav-search-limit-timerange-2.ics',
  1591. __DIR__ . '/../test_fixtures/caldav-search-limit-timerange-3.ics',
  1592. __DIR__ . '/../test_fixtures/caldav-search-limit-timerange-4.ics',
  1593. __DIR__ . '/../test_fixtures/caldav-search-limit-timerange-6.ics', // <-- intentional!
  1594. __DIR__ . '/../test_fixtures/caldav-search-limit-timerange-5.ics',
  1595. ];
  1596. foreach ($testFiles as $testFile) {
  1597. $objectUri = static::getUniqueID('search-limit-timerange-');
  1598. $calendarData = \file_get_contents($testFile);
  1599. $this->backend->createCalendarObject($calendarId, $objectUri, $calendarData);
  1600. }
  1601. $start = new DateTimeImmutable('2024-05-06T00:00:00Z');
  1602. $end = $start->add(new DateInterval('P14D'));
  1603. $results = $this->backend->search(
  1604. $calendarInfo,
  1605. '',
  1606. [],
  1607. [
  1608. 'timerange' => [
  1609. 'start' => $start,
  1610. 'end' => $end,
  1611. ]
  1612. ],
  1613. 2,
  1614. null,
  1615. );
  1616. $this->assertCount(2, $results);
  1617. $this->assertEquals('Cake Tasting', $results[0]['objects'][0]['SUMMARY'][0]);
  1618. $this->assertGreaterThanOrEqual(
  1619. $start->getTimestamp(),
  1620. $results[0]['objects'][0]['DTSTART'][0]->getTimestamp(),
  1621. 'Recurrence starting before requested start',
  1622. );
  1623. $this->assertEquals('Pasta Day', $results[1]['objects'][0]['SUMMARY'][0]);
  1624. $this->assertGreaterThanOrEqual(
  1625. $start->getTimestamp(),
  1626. $results[1]['objects'][0]['DTSTART'][0]->getTimestamp(),
  1627. 'Recurrence starting before requested start',
  1628. );
  1629. }
  1630. public function testSearchShouldReturnObjectsInTheSameOrderMissingDate(): void {
  1631. $calendarId = $this->createTestCalendar();
  1632. $calendarInfo = [
  1633. 'id' => $calendarId,
  1634. 'principaluri' => 'user1',
  1635. '{http://owncloud.org/ns}owner-principal' => 'user1',
  1636. ];
  1637. $testFiles = [
  1638. __DIR__ . '/../test_fixtures/caldav-search-limit-timerange-6.ics', // <-- intentional!
  1639. __DIR__ . '/../test_fixtures/caldav-search-limit-timerange-5.ics',
  1640. __DIR__ . '/../test_fixtures/caldav-search-missing-start-1.ics',
  1641. __DIR__ . '/../test_fixtures/caldav-search-missing-start-2.ics',
  1642. ];
  1643. foreach ($testFiles as $testFile) {
  1644. $objectUri = static::getUniqueID('search-return-objects-in-same-order-');
  1645. $calendarData = \file_get_contents($testFile);
  1646. $this->backend->createCalendarObject($calendarId, $objectUri, $calendarData);
  1647. }
  1648. $results = $this->backend->search(
  1649. $calendarInfo,
  1650. '',
  1651. [],
  1652. [],
  1653. 4,
  1654. null,
  1655. );
  1656. $this->assertCount(4, $results);
  1657. $this->assertEquals('Cake Tasting', $results[0]['objects'][0]['SUMMARY'][0]);
  1658. $this->assertEquals('Pasta Day', $results[1]['objects'][0]['SUMMARY'][0]);
  1659. $this->assertEquals('Missing DTSTART 1', $results[2]['objects'][0]['SUMMARY'][0]);
  1660. $this->assertEquals('Missing DTSTART 2', $results[3]['objects'][0]['SUMMARY'][0]);
  1661. }
  1662. }