ReminderServiceTest.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\Tests\unit\CalDAV\Reminder;
  8. use DateTime;
  9. use DateTimeZone;
  10. use OCA\DAV\CalDAV\CalDavBackend;
  11. use OCA\DAV\CalDAV\Reminder\Backend;
  12. use OCA\DAV\CalDAV\Reminder\INotificationProvider;
  13. use OCA\DAV\CalDAV\Reminder\NotificationProviderManager;
  14. use OCA\DAV\CalDAV\Reminder\ReminderService;
  15. use OCA\DAV\Connector\Sabre\Principal;
  16. use OCP\AppFramework\Utility\ITimeFactory;
  17. use OCP\IConfig;
  18. use OCP\IGroupManager;
  19. use OCP\IUser;
  20. use OCP\IUserManager;
  21. use PHPUnit\Framework\MockObject\MockObject;
  22. use Psr\Log\LoggerInterface;
  23. use Test\TestCase;
  24. class ReminderServiceTest extends TestCase {
  25. /** @var Backend|MockObject */
  26. private $backend;
  27. /** @var NotificationProviderManager|MockObject */
  28. private $notificationProviderManager;
  29. /** @var IUserManager|MockObject */
  30. private $userManager;
  31. /** @var IGroupManager|MockObject */
  32. private $groupManager;
  33. /** @var CalDavBackend|MockObject */
  34. private $caldavBackend;
  35. /** @var ITimeFactory|MockObject */
  36. private $timeFactory;
  37. /** @var IConfig|MockObject */
  38. private $config;
  39. /** @var ReminderService */
  40. private $reminderService;
  41. /** @var MockObject|LoggerInterface */
  42. private $logger;
  43. /** @var MockObject|Principal */
  44. private $principalConnector;
  45. public const CALENDAR_DATA = <<<EOD
  46. BEGIN:VCALENDAR
  47. PRODID:-//Nextcloud calendar v1.6.4
  48. BEGIN:VEVENT
  49. CREATED:20160602T133732
  50. DTSTAMP:20160602T133732
  51. LAST-MODIFIED:20160602T133732
  52. UID:wej2z68l9h
  53. SUMMARY:Test Event
  54. LOCATION:Somewhere ...
  55. DESCRIPTION:maybe ....
  56. DTSTART;TZID=Europe/Berlin;VALUE=DATE:20160609
  57. DTEND;TZID=Europe/Berlin;VALUE=DATE:20160610
  58. BEGIN:VALARM
  59. ACTION:EMAIL
  60. TRIGGER:-PT15M
  61. END:VALARM
  62. BEGIN:VALARM
  63. ACTION:DISPLAY
  64. TRIGGER;VALUE=DATE-TIME:20160608T000000Z
  65. END:VALARM
  66. END:VEVENT
  67. END:VCALENDAR
  68. EOD;
  69. public const CALENDAR_DATA_REPEAT = <<<EOD
  70. BEGIN:VCALENDAR
  71. PRODID:-//Nextcloud calendar v1.6.4
  72. BEGIN:VEVENT
  73. CREATED:20160602T133732
  74. DTSTAMP:20160602T133732
  75. LAST-MODIFIED:20160602T133732
  76. UID:wej2z68l9h
  77. SUMMARY:Test Event
  78. LOCATION:Somewhere ...
  79. DESCRIPTION:maybe ....
  80. DTSTART;TZID=Europe/Berlin;VALUE=DATE:20160609
  81. DTEND;TZID=Europe/Berlin;VALUE=DATE:20160610
  82. BEGIN:VALARM
  83. ACTION:EMAIL
  84. TRIGGER:-PT15M
  85. REPEAT:4
  86. DURATION:PT2M
  87. END:VALARM
  88. END:VEVENT
  89. END:VCALENDAR
  90. EOD;
  91. public const CALENDAR_DATA_RECURRING = <<<EOD
  92. BEGIN:VCALENDAR
  93. PRODID:-//Nextcloud calendar v1.6.4
  94. BEGIN:VEVENT
  95. CREATED:20160602T133732
  96. DTSTAMP:20160602T133732
  97. LAST-MODIFIED:20160602T133732
  98. UID:wej2z68l9h
  99. SUMMARY:Test Event
  100. LOCATION:Somewhere ...
  101. DESCRIPTION:maybe ....
  102. DTSTART;TZID=Europe/Berlin;VALUE=DATE:20160609
  103. DTEND;TZID=Europe/Berlin;VALUE=DATE:20160610
  104. RRULE:FREQ=WEEKLY
  105. BEGIN:VALARM
  106. ACTION:EMAIL
  107. TRIGGER:-PT15M
  108. END:VALARM
  109. BEGIN:VALARM
  110. ACTION:EMAIL
  111. TRIGGER:-P8D
  112. END:VALARM
  113. END:VEVENT
  114. END:VCALENDAR
  115. EOD;
  116. public const CALENDAR_DATA_RECURRING_REPEAT = <<<EOD
  117. BEGIN:VCALENDAR
  118. PRODID:-//Nextcloud calendar v1.6.4
  119. BEGIN:VEVENT
  120. CREATED:20160602T133732
  121. DTSTAMP:20160602T133732
  122. LAST-MODIFIED:20160602T133732
  123. UID:wej2z68l9h
  124. SUMMARY:Test Event
  125. LOCATION:Somewhere ...
  126. DESCRIPTION:maybe ....
  127. DTSTART;TZID=Europe/Berlin;VALUE=DATE:20160609
  128. DTEND;TZID=Europe/Berlin;VALUE=DATE:20160610
  129. RRULE:FREQ=WEEKLY
  130. BEGIN:VALARM
  131. ACTION:EMAIL
  132. TRIGGER:-PT15M
  133. REPEAT:4
  134. DURATION:PT2M
  135. END:VALARM
  136. BEGIN:VALARM
  137. ACTION:EMAIL
  138. TRIGGER:-P8D
  139. END:VALARM
  140. END:VEVENT
  141. END:VCALENDAR
  142. EOD;
  143. public const CALENDAR_DATA_NO_ALARM = <<<EOD
  144. BEGIN:VCALENDAR
  145. PRODID:-//Nextcloud calendar v1.6.4
  146. BEGIN:VEVENT
  147. CREATED:20160602T133732
  148. DTSTAMP:20160602T133732
  149. LAST-MODIFIED:20160602T133732
  150. UID:wej2z68l9h
  151. SUMMARY:Test Event
  152. LOCATION:Somewhere ...
  153. DESCRIPTION:maybe ....
  154. DTSTART;TZID=Europe/Berlin;VALUE=DATE:20160609
  155. DTEND;TZID=Europe/Berlin;VALUE=DATE:20160610
  156. END:VEVENT
  157. END:VCALENDAR
  158. EOD;
  159. private const CALENDAR_DATA_ONE_TIME = <<<EOD
  160. BEGIN:VCALENDAR
  161. PRODID:-//IDN nextcloud.com//Calendar app 4.3.0-alpha.0//EN
  162. CALSCALE:GREGORIAN
  163. VERSION:2.0
  164. BEGIN:VEVENT
  165. CREATED:20230203T154600Z
  166. DTSTAMP:20230203T154602Z
  167. LAST-MODIFIED:20230203T154602Z
  168. SEQUENCE:2
  169. UID:f6a565b6-f9a8-4d1e-9d01-c8dcbe716b7e
  170. DTSTART;TZID=Europe/Vienna:20230204T090000
  171. DTEND;TZID=Europe/Vienna:20230204T120000
  172. STATUS:CONFIRMED
  173. SUMMARY:TEST
  174. BEGIN:VALARM
  175. ACTION:DISPLAY
  176. TRIGGER;RELATED=START:-PT1H
  177. END:VALARM
  178. END:VEVENT
  179. BEGIN:VTIMEZONE
  180. TZID:Europe/Vienna
  181. BEGIN:DAYLIGHT
  182. TZOFFSETFROM:+0100
  183. TZOFFSETTO:+0200
  184. TZNAME:CEST
  185. DTSTART:19700329T020000
  186. RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
  187. END:DAYLIGHT
  188. BEGIN:STANDARD
  189. TZOFFSETFROM:+0200
  190. TZOFFSETTO:+0100
  191. TZNAME:CET
  192. DTSTART:19701025T030000
  193. RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
  194. END:STANDARD
  195. END:VTIMEZONE
  196. END:VCALENDAR
  197. EOD;
  198. private const CALENDAR_DATA_ALL_DAY = <<<EOD
  199. BEGIN:VCALENDAR
  200. PRODID:-//IDN nextcloud.com//Calendar app 4.3.0-alpha.0//EN
  201. CALSCALE:GREGORIAN
  202. VERSION:2.0
  203. BEGIN:VEVENT
  204. CREATED:20230203T113430Z
  205. DTSTAMP:20230203T113432Z
  206. LAST-MODIFIED:20230203T113432Z
  207. SEQUENCE:2
  208. UID:a163a056-ba26-44a2-8080-955f19611a8f
  209. DTSTART;VALUE=DATE:20230204
  210. DTEND;VALUE=DATE:20230205
  211. STATUS:CONFIRMED
  212. SUMMARY:TEST
  213. BEGIN:VALARM
  214. ACTION:EMAIL
  215. TRIGGER;RELATED=START:-PT1H
  216. END:VALARM
  217. END:VEVENT
  218. END:VCALENDAR
  219. EOD;
  220. private const PAGO_PAGO_VTIMEZONE_ICS = <<<ICS
  221. BEGIN:VCALENDAR
  222. BEGIN:VTIMEZONE
  223. TZID:Pacific/Pago_Pago
  224. BEGIN:STANDARD
  225. TZOFFSETFROM:-1100
  226. TZOFFSETTO:-1100
  227. TZNAME:SST
  228. DTSTART:19700101T000000
  229. END:STANDARD
  230. END:VTIMEZONE
  231. END:VCALENDAR
  232. ICS;
  233. /** @var null|string */
  234. private $oldTimezone;
  235. protected function setUp(): void {
  236. parent::setUp();
  237. $this->backend = $this->createMock(Backend::class);
  238. $this->notificationProviderManager = $this->createMock(NotificationProviderManager::class);
  239. $this->userManager = $this->createMock(IUserManager::class);
  240. $this->groupManager = $this->createMock(IGroupManager::class);
  241. $this->caldavBackend = $this->createMock(CalDavBackend::class);
  242. $this->timeFactory = $this->createMock(ITimeFactory::class);
  243. $this->config = $this->createMock(IConfig::class);
  244. $this->logger = $this->createMock(LoggerInterface::class);
  245. $this->principalConnector = $this->createMock(Principal::class);
  246. $this->caldavBackend->method('getShares')->willReturn([]);
  247. $this->reminderService = new ReminderService(
  248. $this->backend,
  249. $this->notificationProviderManager,
  250. $this->userManager,
  251. $this->groupManager,
  252. $this->caldavBackend,
  253. $this->timeFactory,
  254. $this->config,
  255. $this->logger,
  256. $this->principalConnector,
  257. );
  258. }
  259. public function testOnCalendarObjectDelete():void {
  260. $this->backend->expects($this->once())
  261. ->method('cleanRemindersForEvent')
  262. ->with(44);
  263. $objectData = [
  264. 'id' => '44',
  265. 'component' => 'vevent',
  266. ];
  267. $this->reminderService->onCalendarObjectDelete($objectData);
  268. }
  269. public function testOnCalendarObjectCreateSingleEntry():void {
  270. $objectData = [
  271. 'calendardata' => self::CALENDAR_DATA,
  272. 'id' => '42',
  273. 'calendarid' => '1337',
  274. 'component' => 'vevent',
  275. ];
  276. $this->backend->expects($this->exactly(2))
  277. ->method('insertReminder')
  278. ->withConsecutive(
  279. [1337, 42, 'wej2z68l9h', false, 1465430400, false, '5c70531aab15c92b52518ae10a2f78a4', 'de919af7429d3b5c11e8b9d289b411a6', 'EMAIL', true, 1465429500, false],
  280. [1337, 42, 'wej2z68l9h', false, 1465430400, false, '5c70531aab15c92b52518ae10a2f78a4', '35b3eae8e792aa2209f0b4e1a302f105', 'DISPLAY', false, 1465344000, false]
  281. )
  282. ->willReturn(1);
  283. $this->timeFactory->expects($this->once())
  284. ->method('getDateTime')
  285. ->with()
  286. ->willReturn(DateTime::createFromFormat(DateTime::ATOM, '2016-06-08T00:00:00+00:00'));
  287. $this->reminderService->onCalendarObjectCreate($objectData);
  288. }
  289. /**
  290. * RFC5545 says DTSTART is REQUIRED, but we have seen event without the prop
  291. */
  292. public function testOnCalendarObjectCreateNoDtstart(): void {
  293. $calendarData = <<<EOD
  294. BEGIN:VCALENDAR
  295. PRODID:-//Nextcloud calendar v1.6.4
  296. BEGIN:VEVENT
  297. CREATED:20160602T133732
  298. DTSTAMP:20160602T133732
  299. LAST-MODIFIED:20160602T133732
  300. UID:wej2z68l9h
  301. SUMMARY:Test Event
  302. BEGIN:VALARM
  303. ACTION:EMAIL
  304. TRIGGER:-PT15M
  305. END:VALARM
  306. BEGIN:VALARM
  307. ACTION:DISPLAY
  308. TRIGGER;VALUE=DATE-TIME:20160608T000000Z
  309. END:VALARM
  310. END:VEVENT
  311. END:VCALENDAR
  312. EOD;
  313. $objectData = [
  314. 'calendardata' => $calendarData,
  315. 'id' => '42',
  316. 'calendarid' => '1337',
  317. 'component' => 'vevent',
  318. ];
  319. $this->backend->expects($this->never())
  320. ->method('insertReminder')
  321. ->withConsecutive(
  322. [1337, 42, 'wej2z68l9h', false, null, false, '5c70531aab15c92b52518ae10a2f78a4', 'de919af7429d3b5c11e8b9d289b411a6', 'EMAIL', true, 1465429500, false],
  323. [1337, 42, 'wej2z68l9h', false, null, false, '5c70531aab15c92b52518ae10a2f78a4', '35b3eae8e792aa2209f0b4e1a302f105', 'DISPLAY', false, 1465344000, false]
  324. )
  325. ->willReturn(1);
  326. $this->reminderService->onCalendarObjectCreate($objectData);
  327. }
  328. public function testOnCalendarObjectCreateSingleEntryWithRepeat(): void {
  329. $objectData = [
  330. 'calendardata' => self::CALENDAR_DATA_REPEAT,
  331. 'id' => '42',
  332. 'calendarid' => '1337',
  333. 'component' => 'vevent',
  334. ];
  335. $this->backend->expects($this->exactly(5))
  336. ->method('insertReminder')
  337. ->withConsecutive(
  338. [1337, 42, 'wej2z68l9h', false, 1465430400, false, '5c70531aab15c92b52518ae10a2f78a4', 'ecacbf07d413c3c78d1ac7ad8c469602', 'EMAIL', true, 1465429500, false],
  339. [1337, 42, 'wej2z68l9h', false, 1465430400, false, '5c70531aab15c92b52518ae10a2f78a4', 'ecacbf07d413c3c78d1ac7ad8c469602', 'EMAIL', true, 1465429620, true],
  340. [1337, 42, 'wej2z68l9h', false, 1465430400, false, '5c70531aab15c92b52518ae10a2f78a4', 'ecacbf07d413c3c78d1ac7ad8c469602', 'EMAIL', true, 1465429740, true],
  341. [1337, 42, 'wej2z68l9h', false, 1465430400, false, '5c70531aab15c92b52518ae10a2f78a4', 'ecacbf07d413c3c78d1ac7ad8c469602', 'EMAIL', true, 1465429860, true],
  342. [1337, 42, 'wej2z68l9h', false, 1465430400, false, '5c70531aab15c92b52518ae10a2f78a4', 'ecacbf07d413c3c78d1ac7ad8c469602', 'EMAIL', true, 1465429980, true]
  343. )
  344. ->willReturn(1);
  345. $this->timeFactory->expects($this->once())
  346. ->method('getDateTime')
  347. ->with()
  348. ->willReturn(DateTime::createFromFormat(DateTime::ATOM, '2016-06-08T00:00:00+00:00'));
  349. $this->reminderService->onCalendarObjectCreate($objectData);
  350. }
  351. public function testOnCalendarObjectCreateRecurringEntry(): void {
  352. $objectData = [
  353. 'calendardata' => self::CALENDAR_DATA_RECURRING,
  354. 'id' => '42',
  355. 'calendarid' => '1337',
  356. 'component' => 'vevent',
  357. ];
  358. $this->backend->expects($this->exactly(2))
  359. ->method('insertReminder')
  360. ->withConsecutive(
  361. [1337, 42, 'wej2z68l9h', true, 1467244800, false, 'fbdb2726bc0f7dfacac1d881c1453e20', 'de919af7429d3b5c11e8b9d289b411a6', 'EMAIL', true, 1467243900, false],
  362. [1337, 42, 'wej2z68l9h', true, 1467849600, false, 'fbdb2726bc0f7dfacac1d881c1453e20', '8996992118817f9f311ac5cc56d1cc97', 'EMAIL', true, 1467158400, false]
  363. )
  364. ->willReturn(1);
  365. $this->timeFactory->expects($this->once())
  366. ->method('getDateTime')
  367. ->willReturn(DateTime::createFromFormat(DateTime::ATOM, '2016-06-29T00:00:00+00:00'));
  368. $this->reminderService->onCalendarObjectCreate($objectData);
  369. }
  370. public function testOnCalendarObjectCreateEmpty():void {
  371. $objectData = [
  372. 'calendardata' => self::CALENDAR_DATA_NO_ALARM,
  373. 'id' => '42',
  374. 'calendarid' => '1337',
  375. 'component' => 'vevent',
  376. ];
  377. $this->backend->expects($this->never())
  378. ->method('insertReminder');
  379. $this->reminderService->onCalendarObjectCreate($objectData);
  380. }
  381. public function testOnCalendarObjectCreateAllDayWithNullTimezone(): void {
  382. $objectData = [
  383. 'calendardata' => self::CALENDAR_DATA_ALL_DAY,
  384. 'id' => '42',
  385. 'calendarid' => '1337',
  386. 'component' => 'vevent',
  387. ];
  388. $this->timeFactory->expects($this->once())
  389. ->method('getDateTime')
  390. ->with()
  391. ->willReturn(DateTime::createFromFormat(DateTime::ATOM, '2023-02-03T13:28:00+00:00'));
  392. $this->caldavBackend->expects(self::once())
  393. ->method('getCalendarById')
  394. ->with(1337)
  395. ->willReturn([
  396. '{urn:ietf:params:xml:ns:caldav}calendar-timezone' => null,
  397. ]);
  398. // One hour before midnight relative to the server's time
  399. $expectedReminderTimstamp = (new DateTime('2023-02-03T23:00:00'))->getTimestamp();
  400. $this->backend->expects(self::once())
  401. ->method('insertReminder')
  402. ->with(1337, 42, self::anything(), false, 1675468800, false, self::anything(), self::anything(), 'EMAIL', true, $expectedReminderTimstamp, false);
  403. $this->reminderService->onCalendarObjectCreate($objectData);
  404. }
  405. public function testOnCalendarObjectCreateAllDayWithBlankTimezone(): void {
  406. $objectData = [
  407. 'calendardata' => self::CALENDAR_DATA_ALL_DAY,
  408. 'id' => '42',
  409. 'calendarid' => '1337',
  410. 'component' => 'vevent',
  411. ];
  412. $this->timeFactory->expects($this->once())
  413. ->method('getDateTime')
  414. ->with()
  415. ->willReturn(DateTime::createFromFormat(DateTime::ATOM, '2023-02-03T13:28:00+00:00'));
  416. $this->caldavBackend->expects(self::once())
  417. ->method('getCalendarById')
  418. ->with(1337)
  419. ->willReturn([
  420. '{urn:ietf:params:xml:ns:caldav}calendar-timezone' => '',
  421. ]);
  422. // One hour before midnight relative to the server's time
  423. $expectedReminderTimstamp = (new DateTime('2023-02-03T23:00:00'))->getTimestamp();
  424. $this->backend->expects(self::once())
  425. ->method('insertReminder')
  426. ->with(1337, 42, self::anything(), false, 1675468800, false, self::anything(), self::anything(), 'EMAIL', true, $expectedReminderTimstamp, false);
  427. $this->reminderService->onCalendarObjectCreate($objectData);
  428. }
  429. public function testOnCalendarObjectCreateAllDayWithTimezone(): void {
  430. $objectData = [
  431. 'calendardata' => self::CALENDAR_DATA_ALL_DAY,
  432. 'id' => '42',
  433. 'calendarid' => '1337',
  434. 'component' => 'vevent',
  435. ];
  436. $this->timeFactory->expects($this->once())
  437. ->method('getDateTime')
  438. ->with()
  439. ->willReturn(DateTime::createFromFormat(DateTime::ATOM, '2023-02-03T13:28:00+00:00'));
  440. $this->caldavBackend->expects(self::once())
  441. ->method('getCalendarById')
  442. ->with(1337)
  443. ->willReturn([
  444. '{urn:ietf:params:xml:ns:caldav}calendar-timezone' => self::PAGO_PAGO_VTIMEZONE_ICS,
  445. ]);
  446. // One hour before midnight relative to the timezone
  447. $expectedReminderTimstamp = (new DateTime('2023-02-03T23:00:00', new DateTimeZone('Pacific/Pago_Pago')))->getTimestamp();
  448. $this->backend->expects(self::once())
  449. ->method('insertReminder')
  450. ->with(1337, 42, 'a163a056-ba26-44a2-8080-955f19611a8f', false, self::anything(), false, self::anything(), self::anything(), 'EMAIL', true, $expectedReminderTimstamp, false);
  451. $this->reminderService->onCalendarObjectCreate($objectData);
  452. }
  453. public function testOnCalendarObjectCreateRecurringEntryWithRepeat():void {
  454. $objectData = [
  455. 'calendardata' => self::CALENDAR_DATA_RECURRING_REPEAT,
  456. 'id' => '42',
  457. 'calendarid' => '1337',
  458. 'component' => 'vevent',
  459. ];
  460. $this->caldavBackend->expects(self::once())
  461. ->method('getCalendarById')
  462. ->with(1337)
  463. ->willReturn([
  464. '{urn:ietf:params:xml:ns:caldav}calendar-timezone' => null,
  465. ]);
  466. $this->backend->expects($this->exactly(6))
  467. ->method('insertReminder')
  468. ->withConsecutive(
  469. [1337, 42, 'wej2z68l9h', true, 1467244800, false, 'fbdb2726bc0f7dfacac1d881c1453e20', 'ecacbf07d413c3c78d1ac7ad8c469602', 'EMAIL', true, 1467243900, false],
  470. [1337, 42, 'wej2z68l9h', true, 1467244800, false, 'fbdb2726bc0f7dfacac1d881c1453e20', 'ecacbf07d413c3c78d1ac7ad8c469602', 'EMAIL', true, 1467244020, true],
  471. [1337, 42, 'wej2z68l9h', true, 1467244800, false, 'fbdb2726bc0f7dfacac1d881c1453e20', 'ecacbf07d413c3c78d1ac7ad8c469602', 'EMAIL', true, 1467244140, true],
  472. [1337, 42, 'wej2z68l9h', true, 1467244800, false, 'fbdb2726bc0f7dfacac1d881c1453e20', 'ecacbf07d413c3c78d1ac7ad8c469602', 'EMAIL', true, 1467244260, true],
  473. [1337, 42, 'wej2z68l9h', true, 1467244800, false, 'fbdb2726bc0f7dfacac1d881c1453e20', 'ecacbf07d413c3c78d1ac7ad8c469602', 'EMAIL', true, 1467244380, true],
  474. [1337, 42, 'wej2z68l9h', true, 1467849600, false, 'fbdb2726bc0f7dfacac1d881c1453e20', '8996992118817f9f311ac5cc56d1cc97', 'EMAIL', true, 1467158400, false]
  475. )
  476. ->willReturn(1);
  477. $this->timeFactory->expects($this->once())
  478. ->method('getDateTime')
  479. ->with()
  480. ->willReturn(DateTime::createFromFormat(DateTime::ATOM, '2016-06-29T00:00:00+00:00'));
  481. $this->reminderService->onCalendarObjectCreate($objectData);
  482. }
  483. public function testOnCalendarObjectCreateWithEventTimezoneAndCalendarTimezone():void {
  484. $objectData = [
  485. 'calendardata' => self::CALENDAR_DATA_ONE_TIME,
  486. 'id' => '42',
  487. 'calendarid' => '1337',
  488. 'component' => 'vevent',
  489. ];
  490. $this->caldavBackend->expects(self::once())
  491. ->method('getCalendarById')
  492. ->with(1337)
  493. ->willReturn([
  494. '{urn:ietf:params:xml:ns:caldav}calendar-timezone' => self::PAGO_PAGO_VTIMEZONE_ICS,
  495. ]);
  496. $expectedReminderTimstamp = (new DateTime('2023-02-04T08:00:00', new DateTimeZone('Europe/Vienna')))->getTimestamp();
  497. $this->backend->expects(self::once())
  498. ->method('insertReminder')
  499. ->withConsecutive(
  500. [1337, 42, self::anything(), false, self::anything(), false, self::anything(), self::anything(), self::anything(), true, $expectedReminderTimstamp, false],
  501. )
  502. ->willReturn(1);
  503. $this->caldavBackend->expects(self::once())
  504. ->method('getCalendarById')
  505. ->with(1337)
  506. ->willReturn([
  507. '{urn:ietf:params:xml:ns:caldav}calendar-timezone' => null,
  508. ]);
  509. $this->timeFactory->expects($this->once())
  510. ->method('getDateTime')
  511. ->with()
  512. ->willReturn(DateTime::createFromFormat(DateTime::ATOM, '2023-02-03T13:28:00+00:00'));
  513. ;
  514. $this->reminderService->onCalendarObjectCreate($objectData);
  515. }
  516. public function testProcessReminders():void {
  517. $this->backend->expects($this->once())
  518. ->method('getRemindersToProcess')
  519. ->with()
  520. ->willReturn([
  521. [
  522. 'id' => 1,
  523. 'calendar_id' => 1337,
  524. 'object_id' => 42,
  525. 'uid' => 'wej2z68l9h',
  526. 'is_recurring' => false,
  527. 'recurrence_id' => 1465430400,
  528. 'is_recurrence_exception' => false,
  529. 'event_hash' => '5c70531aab15c92b52518ae10a2f78a4',
  530. 'alarm_hash' => 'de919af7429d3b5c11e8b9d289b411a6',
  531. 'type' => 'EMAIL',
  532. 'is_relative' => true,
  533. 'notification_date' => 1465429500,
  534. 'is_repeat_based' => false,
  535. 'calendardata' => self::CALENDAR_DATA,
  536. 'displayname' => 'Displayname 123',
  537. 'principaluri' => 'principals/users/user001',
  538. ],
  539. [
  540. 'id' => 2,
  541. 'calendar_id' => 1337,
  542. 'object_id' => 42,
  543. 'uid' => 'wej2z68l9h',
  544. 'is_recurring' => false,
  545. 'recurrence_id' => 1465430400,
  546. 'is_recurrence_exception' => false,
  547. 'event_hash' => '5c70531aab15c92b52518ae10a2f78a4',
  548. 'alarm_hash' => 'ecacbf07d413c3c78d1ac7ad8c469602',
  549. 'type' => 'EMAIL',
  550. 'is_relative' => true,
  551. 'notification_date' => 1465429740,
  552. 'is_repeat_based' => true,
  553. 'calendardata' => self::CALENDAR_DATA_REPEAT,
  554. 'displayname' => 'Displayname 123',
  555. 'principaluri' => 'principals/users/user001',
  556. ],
  557. [
  558. 'id' => 3,
  559. 'calendar_id' => 1337,
  560. 'object_id' => 42,
  561. 'uid' => 'wej2z68l9h',
  562. 'is_recurring' => false,
  563. 'recurrence_id' => 1465430400,
  564. 'is_recurrence_exception' => false,
  565. 'event_hash' => '5c70531aab15c92b52518ae10a2f78a4',
  566. 'alarm_hash' => '35b3eae8e792aa2209f0b4e1a302f105',
  567. 'type' => 'DISPLAY',
  568. 'is_relative' => false,
  569. 'notification_date' => 1465344000,
  570. 'is_repeat_based' => false,
  571. 'calendardata' => self::CALENDAR_DATA,
  572. 'displayname' => 'Displayname 123',
  573. 'principaluri' => 'principals/users/user001',
  574. ],
  575. [
  576. 'id' => 4,
  577. 'calendar_id' => 1337,
  578. 'object_id' => 42,
  579. 'uid' => 'wej2z68l9h',
  580. 'is_recurring' => true,
  581. 'recurrence_id' => 1467244800,
  582. 'is_recurrence_exception' => false,
  583. 'event_hash' => 'fbdb2726bc0f7dfacac1d881c1453e20',
  584. 'alarm_hash' => 'ecacbf07d413c3c78d1ac7ad8c469602',
  585. 'type' => 'EMAIL',
  586. 'is_relative' => true,
  587. 'notification_date' => 1467243900,
  588. 'is_repeat_based' => false,
  589. 'calendardata' => self::CALENDAR_DATA_RECURRING_REPEAT,
  590. 'displayname' => 'Displayname 123',
  591. 'principaluri' => 'principals/users/user001',
  592. ],
  593. [
  594. 'id' => 5,
  595. 'calendar_id' => 1337,
  596. 'object_id' => 42,
  597. 'uid' => 'wej2z68l9h',
  598. 'is_recurring' => true,
  599. 'recurrence_id' => 1467849600,
  600. 'is_recurrence_exception' => false,
  601. 'event_hash' => 'fbdb2726bc0f7dfacac1d881c1453e20',
  602. 'alarm_hash' => '8996992118817f9f311ac5cc56d1cc97',
  603. 'type' => 'EMAIL',
  604. 'is_relative' => true,
  605. 'notification_date' => 1467158400,
  606. 'is_repeat_based' => false,
  607. 'calendardata' => self::CALENDAR_DATA_RECURRING,
  608. 'displayname' => 'Displayname 123',
  609. 'principaluri' => 'principals/users/user001',
  610. ]
  611. ]);
  612. $this->notificationProviderManager->expects($this->exactly(5))
  613. ->method('hasProvider')
  614. ->willReturnMap([
  615. ['EMAIL', true],
  616. ['DISPLAY', true],
  617. ]);
  618. $provider1 = $this->createMock(INotificationProvider::class);
  619. $provider2 = $this->createMock(INotificationProvider::class);
  620. $provider3 = $this->createMock(INotificationProvider::class);
  621. $provider4 = $this->createMock(INotificationProvider::class);
  622. $provider5 = $this->createMock(INotificationProvider::class);
  623. $this->notificationProviderManager->expects($this->exactly(5))
  624. ->method('getProvider')
  625. ->withConsecutive(
  626. ['EMAIL'],
  627. ['EMAIL'],
  628. ['DISPLAY'],
  629. ['EMAIL'],
  630. ['EMAIL'],
  631. )
  632. ->willReturnOnConsecutiveCalls(
  633. $provider1,
  634. $provider2,
  635. $provider3,
  636. $provider4,
  637. $provider5,
  638. );
  639. $user = $this->createMock(IUser::class);
  640. $this->userManager->expects($this->exactly(5))
  641. ->method('get')
  642. ->with('user001')
  643. ->willReturn($user);
  644. $provider1->expects($this->once())
  645. ->method('send')
  646. ->with($this->callback(function ($vevent) {
  647. if ($vevent->DTSTART->getDateTime()->format(DateTime::ATOM) !== '2016-06-09T00:00:00+00:00') {
  648. return false;
  649. }
  650. return true;
  651. }, 'Displayname 123', $user));
  652. $provider2->expects($this->once())
  653. ->method('send')
  654. ->with($this->callback(function ($vevent) {
  655. if ($vevent->DTSTART->getDateTime()->format(DateTime::ATOM) !== '2016-06-09T00:00:00+00:00') {
  656. return false;
  657. }
  658. return true;
  659. }, 'Displayname 123', $user));
  660. $provider3->expects($this->once())
  661. ->method('send')
  662. ->with($this->callback(function ($vevent) {
  663. if ($vevent->DTSTART->getDateTime()->format(DateTime::ATOM) !== '2016-06-09T00:00:00+00:00') {
  664. return false;
  665. }
  666. return true;
  667. }, 'Displayname 123', $user));
  668. $provider4->expects($this->once())
  669. ->method('send')
  670. ->with($this->callback(function ($vevent) {
  671. if ($vevent->DTSTART->getDateTime()->format(DateTime::ATOM) !== '2016-06-30T00:00:00+00:00') {
  672. return false;
  673. }
  674. return true;
  675. }, 'Displayname 123', $user));
  676. $provider5->expects($this->once())
  677. ->method('send')
  678. ->with($this->callback(function ($vevent) {
  679. if ($vevent->DTSTART->getDateTime()->format(DateTime::ATOM) !== '2016-07-07T00:00:00+00:00') {
  680. return false;
  681. }
  682. return true;
  683. }, 'Displayname 123', $user));
  684. $this->backend->expects($this->exactly(5))
  685. ->method('removeReminder')
  686. ->withConsecutive([1], [2], [3], [4], [5]);
  687. $this->backend->expects($this->exactly(6))
  688. ->method('insertReminder')
  689. ->withConsecutive(
  690. [1337, 42, 'wej2z68l9h', true, 1467849600, false, 'fbdb2726bc0f7dfacac1d881c1453e20', 'ecacbf07d413c3c78d1ac7ad8c469602', 'EMAIL', true, 1467848700, false],
  691. [1337, 42, 'wej2z68l9h', true, 1467849600, false, 'fbdb2726bc0f7dfacac1d881c1453e20', 'ecacbf07d413c3c78d1ac7ad8c469602', 'EMAIL', true, 1467848820, true],
  692. [1337, 42, 'wej2z68l9h', true, 1467849600, false, 'fbdb2726bc0f7dfacac1d881c1453e20', 'ecacbf07d413c3c78d1ac7ad8c469602', 'EMAIL', true, 1467848940, true],
  693. [1337, 42, 'wej2z68l9h', true, 1467849600, false, 'fbdb2726bc0f7dfacac1d881c1453e20', 'ecacbf07d413c3c78d1ac7ad8c469602', 'EMAIL', true, 1467849060, true],
  694. [1337, 42, 'wej2z68l9h', true, 1467849600, false, 'fbdb2726bc0f7dfacac1d881c1453e20', 'ecacbf07d413c3c78d1ac7ad8c469602', 'EMAIL', true, 1467849180, true],
  695. [1337, 42, 'wej2z68l9h', true, 1468454400, false, 'fbdb2726bc0f7dfacac1d881c1453e20', '8996992118817f9f311ac5cc56d1cc97', 'EMAIL', true, 1467763200, false],
  696. )
  697. ->willReturn(99);
  698. $this->timeFactory->method('getDateTime')
  699. ->willReturn(DateTime::createFromFormat(DateTime::ATOM, '2016-06-08T00:00:00+00:00'));
  700. $this->reminderService->processReminders();
  701. }
  702. }