ReminderServiceTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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 testOnCalendarObjectCreateAllDayWithoutTimezone(): 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 testOnCalendarObjectCreateAllDayWithTimezone(): 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' => self::PAGO_PAGO_VTIMEZONE_ICS,
  421. ]);
  422. // One hour before midnight relative to the timezone
  423. $expectedReminderTimstamp = (new DateTime('2023-02-03T23:00:00', new DateTimeZone('Pacific/Pago_Pago')))->getTimestamp();
  424. $this->backend->expects(self::once())
  425. ->method('insertReminder')
  426. ->with(1337, 42, 'a163a056-ba26-44a2-8080-955f19611a8f', false, self::anything(), false, self::anything(), self::anything(), 'EMAIL', true, $expectedReminderTimstamp, false);
  427. $this->reminderService->onCalendarObjectCreate($objectData);
  428. }
  429. public function testOnCalendarObjectCreateRecurringEntryWithRepeat():void {
  430. $objectData = [
  431. 'calendardata' => self::CALENDAR_DATA_RECURRING_REPEAT,
  432. 'id' => '42',
  433. 'calendarid' => '1337',
  434. 'component' => 'vevent',
  435. ];
  436. $this->caldavBackend->expects(self::once())
  437. ->method('getCalendarById')
  438. ->with(1337)
  439. ->willReturn([
  440. '{urn:ietf:params:xml:ns:caldav}calendar-timezone' => null,
  441. ]);
  442. $this->backend->expects($this->exactly(6))
  443. ->method('insertReminder')
  444. ->withConsecutive(
  445. [1337, 42, 'wej2z68l9h', true, 1467244800, false, 'fbdb2726bc0f7dfacac1d881c1453e20', 'ecacbf07d413c3c78d1ac7ad8c469602', 'EMAIL', true, 1467243900, false],
  446. [1337, 42, 'wej2z68l9h', true, 1467244800, false, 'fbdb2726bc0f7dfacac1d881c1453e20', 'ecacbf07d413c3c78d1ac7ad8c469602', 'EMAIL', true, 1467244020, true],
  447. [1337, 42, 'wej2z68l9h', true, 1467244800, false, 'fbdb2726bc0f7dfacac1d881c1453e20', 'ecacbf07d413c3c78d1ac7ad8c469602', 'EMAIL', true, 1467244140, true],
  448. [1337, 42, 'wej2z68l9h', true, 1467244800, false, 'fbdb2726bc0f7dfacac1d881c1453e20', 'ecacbf07d413c3c78d1ac7ad8c469602', 'EMAIL', true, 1467244260, true],
  449. [1337, 42, 'wej2z68l9h', true, 1467244800, false, 'fbdb2726bc0f7dfacac1d881c1453e20', 'ecacbf07d413c3c78d1ac7ad8c469602', 'EMAIL', true, 1467244380, true],
  450. [1337, 42, 'wej2z68l9h', true, 1467849600, false, 'fbdb2726bc0f7dfacac1d881c1453e20', '8996992118817f9f311ac5cc56d1cc97', 'EMAIL', true, 1467158400, false]
  451. )
  452. ->willReturn(1);
  453. $this->timeFactory->expects($this->once())
  454. ->method('getDateTime')
  455. ->with()
  456. ->willReturn(DateTime::createFromFormat(DateTime::ATOM, '2016-06-29T00:00:00+00:00'));
  457. $this->reminderService->onCalendarObjectCreate($objectData);
  458. }
  459. public function testOnCalendarObjectCreateWithEventTimezoneAndCalendarTimezone():void {
  460. $objectData = [
  461. 'calendardata' => self::CALENDAR_DATA_ONE_TIME,
  462. 'id' => '42',
  463. 'calendarid' => '1337',
  464. 'component' => 'vevent',
  465. ];
  466. $this->caldavBackend->expects(self::once())
  467. ->method('getCalendarById')
  468. ->with(1337)
  469. ->willReturn([
  470. '{urn:ietf:params:xml:ns:caldav}calendar-timezone' => self::PAGO_PAGO_VTIMEZONE_ICS,
  471. ]);
  472. $expectedReminderTimstamp = (new DateTime('2023-02-04T08:00:00', new DateTimeZone('Europe/Vienna')))->getTimestamp();
  473. $this->backend->expects(self::once())
  474. ->method('insertReminder')
  475. ->withConsecutive(
  476. [1337, 42, self::anything(), false, self::anything(), false, self::anything(), self::anything(), self::anything(), true, $expectedReminderTimstamp, false],
  477. )
  478. ->willReturn(1);
  479. $this->caldavBackend->expects(self::once())
  480. ->method('getCalendarById')
  481. ->with(1337)
  482. ->willReturn([
  483. '{urn:ietf:params:xml:ns:caldav}calendar-timezone' => null,
  484. ]);
  485. $this->timeFactory->expects($this->once())
  486. ->method('getDateTime')
  487. ->with()
  488. ->willReturn(DateTime::createFromFormat(DateTime::ATOM, '2023-02-03T13:28:00+00:00'));
  489. ;
  490. $this->reminderService->onCalendarObjectCreate($objectData);
  491. }
  492. public function testProcessReminders():void {
  493. $this->backend->expects($this->once())
  494. ->method('getRemindersToProcess')
  495. ->with()
  496. ->willReturn([
  497. [
  498. 'id' => 1,
  499. 'calendar_id' => 1337,
  500. 'object_id' => 42,
  501. 'uid' => 'wej2z68l9h',
  502. 'is_recurring' => false,
  503. 'recurrence_id' => 1465430400,
  504. 'is_recurrence_exception' => false,
  505. 'event_hash' => '5c70531aab15c92b52518ae10a2f78a4',
  506. 'alarm_hash' => 'de919af7429d3b5c11e8b9d289b411a6',
  507. 'type' => 'EMAIL',
  508. 'is_relative' => true,
  509. 'notification_date' => 1465429500,
  510. 'is_repeat_based' => false,
  511. 'calendardata' => self::CALENDAR_DATA,
  512. 'displayname' => 'Displayname 123',
  513. 'principaluri' => 'principals/users/user001',
  514. ],
  515. [
  516. 'id' => 2,
  517. 'calendar_id' => 1337,
  518. 'object_id' => 42,
  519. 'uid' => 'wej2z68l9h',
  520. 'is_recurring' => false,
  521. 'recurrence_id' => 1465430400,
  522. 'is_recurrence_exception' => false,
  523. 'event_hash' => '5c70531aab15c92b52518ae10a2f78a4',
  524. 'alarm_hash' => 'ecacbf07d413c3c78d1ac7ad8c469602',
  525. 'type' => 'EMAIL',
  526. 'is_relative' => true,
  527. 'notification_date' => 1465429740,
  528. 'is_repeat_based' => true,
  529. 'calendardata' => self::CALENDAR_DATA_REPEAT,
  530. 'displayname' => 'Displayname 123',
  531. 'principaluri' => 'principals/users/user001',
  532. ],
  533. [
  534. 'id' => 3,
  535. 'calendar_id' => 1337,
  536. 'object_id' => 42,
  537. 'uid' => 'wej2z68l9h',
  538. 'is_recurring' => false,
  539. 'recurrence_id' => 1465430400,
  540. 'is_recurrence_exception' => false,
  541. 'event_hash' => '5c70531aab15c92b52518ae10a2f78a4',
  542. 'alarm_hash' => '35b3eae8e792aa2209f0b4e1a302f105',
  543. 'type' => 'DISPLAY',
  544. 'is_relative' => false,
  545. 'notification_date' => 1465344000,
  546. 'is_repeat_based' => false,
  547. 'calendardata' => self::CALENDAR_DATA,
  548. 'displayname' => 'Displayname 123',
  549. 'principaluri' => 'principals/users/user001',
  550. ],
  551. [
  552. 'id' => 4,
  553. 'calendar_id' => 1337,
  554. 'object_id' => 42,
  555. 'uid' => 'wej2z68l9h',
  556. 'is_recurring' => true,
  557. 'recurrence_id' => 1467244800,
  558. 'is_recurrence_exception' => false,
  559. 'event_hash' => 'fbdb2726bc0f7dfacac1d881c1453e20',
  560. 'alarm_hash' => 'ecacbf07d413c3c78d1ac7ad8c469602',
  561. 'type' => 'EMAIL',
  562. 'is_relative' => true,
  563. 'notification_date' => 1467243900,
  564. 'is_repeat_based' => false,
  565. 'calendardata' => self::CALENDAR_DATA_RECURRING_REPEAT,
  566. 'displayname' => 'Displayname 123',
  567. 'principaluri' => 'principals/users/user001',
  568. ],
  569. [
  570. 'id' => 5,
  571. 'calendar_id' => 1337,
  572. 'object_id' => 42,
  573. 'uid' => 'wej2z68l9h',
  574. 'is_recurring' => true,
  575. 'recurrence_id' => 1467849600,
  576. 'is_recurrence_exception' => false,
  577. 'event_hash' => 'fbdb2726bc0f7dfacac1d881c1453e20',
  578. 'alarm_hash' => '8996992118817f9f311ac5cc56d1cc97',
  579. 'type' => 'EMAIL',
  580. 'is_relative' => true,
  581. 'notification_date' => 1467158400,
  582. 'is_repeat_based' => false,
  583. 'calendardata' => self::CALENDAR_DATA_RECURRING,
  584. 'displayname' => 'Displayname 123',
  585. 'principaluri' => 'principals/users/user001',
  586. ]
  587. ]);
  588. $this->notificationProviderManager->expects($this->exactly(5))
  589. ->method('hasProvider')
  590. ->willReturnMap([
  591. ['EMAIL', true],
  592. ['DISPLAY', true],
  593. ]);
  594. $provider1 = $this->createMock(INotificationProvider::class);
  595. $provider2 = $this->createMock(INotificationProvider::class);
  596. $provider3 = $this->createMock(INotificationProvider::class);
  597. $provider4 = $this->createMock(INotificationProvider::class);
  598. $provider5 = $this->createMock(INotificationProvider::class);
  599. $this->notificationProviderManager->expects($this->exactly(5))
  600. ->method('getProvider')
  601. ->withConsecutive(
  602. ['EMAIL'],
  603. ['EMAIL'],
  604. ['DISPLAY'],
  605. ['EMAIL'],
  606. ['EMAIL'],
  607. )
  608. ->willReturnOnConsecutiveCalls(
  609. $provider1,
  610. $provider2,
  611. $provider3,
  612. $provider4,
  613. $provider5,
  614. );
  615. $user = $this->createMock(IUser::class);
  616. $this->userManager->expects($this->exactly(5))
  617. ->method('get')
  618. ->with('user001')
  619. ->willReturn($user);
  620. $provider1->expects($this->once())
  621. ->method('send')
  622. ->with($this->callback(function ($vevent) {
  623. if ($vevent->DTSTART->getDateTime()->format(DateTime::ATOM) !== '2016-06-09T00:00:00+00:00') {
  624. return false;
  625. }
  626. return true;
  627. }, 'Displayname 123', $user));
  628. $provider2->expects($this->once())
  629. ->method('send')
  630. ->with($this->callback(function ($vevent) {
  631. if ($vevent->DTSTART->getDateTime()->format(DateTime::ATOM) !== '2016-06-09T00:00:00+00:00') {
  632. return false;
  633. }
  634. return true;
  635. }, 'Displayname 123', $user));
  636. $provider3->expects($this->once())
  637. ->method('send')
  638. ->with($this->callback(function ($vevent) {
  639. if ($vevent->DTSTART->getDateTime()->format(DateTime::ATOM) !== '2016-06-09T00:00:00+00:00') {
  640. return false;
  641. }
  642. return true;
  643. }, 'Displayname 123', $user));
  644. $provider4->expects($this->once())
  645. ->method('send')
  646. ->with($this->callback(function ($vevent) {
  647. if ($vevent->DTSTART->getDateTime()->format(DateTime::ATOM) !== '2016-06-30T00:00:00+00:00') {
  648. return false;
  649. }
  650. return true;
  651. }, 'Displayname 123', $user));
  652. $provider5->expects($this->once())
  653. ->method('send')
  654. ->with($this->callback(function ($vevent) {
  655. if ($vevent->DTSTART->getDateTime()->format(DateTime::ATOM) !== '2016-07-07T00:00:00+00:00') {
  656. return false;
  657. }
  658. return true;
  659. }, 'Displayname 123', $user));
  660. $this->backend->expects($this->exactly(5))
  661. ->method('removeReminder')
  662. ->withConsecutive([1], [2], [3], [4], [5]);
  663. $this->backend->expects($this->exactly(6))
  664. ->method('insertReminder')
  665. ->withConsecutive(
  666. [1337, 42, 'wej2z68l9h', true, 1467849600, false, 'fbdb2726bc0f7dfacac1d881c1453e20', 'ecacbf07d413c3c78d1ac7ad8c469602', 'EMAIL', true, 1467848700, false],
  667. [1337, 42, 'wej2z68l9h', true, 1467849600, false, 'fbdb2726bc0f7dfacac1d881c1453e20', 'ecacbf07d413c3c78d1ac7ad8c469602', 'EMAIL', true, 1467848820, true],
  668. [1337, 42, 'wej2z68l9h', true, 1467849600, false, 'fbdb2726bc0f7dfacac1d881c1453e20', 'ecacbf07d413c3c78d1ac7ad8c469602', 'EMAIL', true, 1467848940, true],
  669. [1337, 42, 'wej2z68l9h', true, 1467849600, false, 'fbdb2726bc0f7dfacac1d881c1453e20', 'ecacbf07d413c3c78d1ac7ad8c469602', 'EMAIL', true, 1467849060, true],
  670. [1337, 42, 'wej2z68l9h', true, 1467849600, false, 'fbdb2726bc0f7dfacac1d881c1453e20', 'ecacbf07d413c3c78d1ac7ad8c469602', 'EMAIL', true, 1467849180, true],
  671. [1337, 42, 'wej2z68l9h', true, 1468454400, false, 'fbdb2726bc0f7dfacac1d881c1453e20', '8996992118817f9f311ac5cc56d1cc97', 'EMAIL', true, 1467763200, false],
  672. )
  673. ->willReturn(99);
  674. $this->timeFactory->method('getDateTime')
  675. ->willReturn(DateTime::createFromFormat(DateTime::ATOM, '2016-06-08T00:00:00+00:00'));
  676. $this->reminderService->processReminders();
  677. }
  678. }