CalDavBackendTest.php 55 KB

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