AbsenceServiceTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\dav\tests\unit\Service;
  8. use DateTimeImmutable;
  9. use DateTimeZone;
  10. use OCA\DAV\BackgroundJob\OutOfOfficeEventDispatcherJob;
  11. use OCA\DAV\CalDAV\TimezoneService;
  12. use OCA\DAV\Db\Absence;
  13. use OCA\DAV\Db\AbsenceMapper;
  14. use OCA\DAV\Service\AbsenceService;
  15. use OCP\AppFramework\Db\DoesNotExistException;
  16. use OCP\AppFramework\Utility\ITimeFactory;
  17. use OCP\BackgroundJob\IJobList;
  18. use OCP\EventDispatcher\Event;
  19. use OCP\EventDispatcher\IEventDispatcher;
  20. use OCP\IUser;
  21. use OCP\User\Events\OutOfOfficeChangedEvent;
  22. use OCP\User\Events\OutOfOfficeScheduledEvent;
  23. use PHPUnit\Framework\TestCase;
  24. class AbsenceServiceTest extends TestCase {
  25. private AbsenceService $absenceService;
  26. /** @var MockObject|AbsenceMapper */
  27. private $absenceMapper;
  28. /** @var MockObject|IEventDispatcher */
  29. private $eventDispatcher;
  30. /** @var MockObject|IJobList */
  31. private $jobList;
  32. /** @var MockObject|TimezoneService */
  33. private $timezoneService;
  34. /** @var MockObject|ITimeFactory */
  35. private $timeFactory;
  36. protected function setUp(): void {
  37. parent::setUp();
  38. $this->absenceMapper = $this->createMock(AbsenceMapper::class);
  39. $this->eventDispatcher = $this->createMock(IEventDispatcher::class);
  40. $this->jobList = $this->createMock(IJobList::class);
  41. $this->timezoneService = $this->createMock(TimezoneService::class);
  42. $this->timeFactory = $this->createMock(ITimeFactory::class);
  43. $this->absenceService = new AbsenceService(
  44. $this->absenceMapper,
  45. $this->eventDispatcher,
  46. $this->jobList,
  47. $this->timezoneService,
  48. $this->timeFactory,
  49. );
  50. }
  51. public function testCreateAbsenceEmitsScheduledEvent(): void {
  52. $tz = new DateTimeZone('Europe/Berlin');
  53. $user = $this->createMock(IUser::class);
  54. $user->method('getUID')
  55. ->willReturn('user');
  56. $this->absenceMapper->expects(self::once())
  57. ->method('findByUserId')
  58. ->with('user')
  59. ->willThrowException(new DoesNotExistException('foo bar'));
  60. $this->absenceMapper->expects(self::once())
  61. ->method('insert')
  62. ->willReturnCallback(function (Absence $absence): Absence {
  63. $absence->setId(1);
  64. return $absence;
  65. });
  66. $this->timezoneService->expects(self::once())
  67. ->method('getUserTimezone')
  68. ->with('user')
  69. ->willReturn('Europe/Berlin');
  70. $this->eventDispatcher->expects(self::once())
  71. ->method('dispatchTyped')
  72. ->with(self::callback(static function (Event $event) use ($user, $tz): bool {
  73. self::assertInstanceOf(OutOfOfficeScheduledEvent::class, $event);
  74. /** @var OutOfOfficeScheduledEvent $event */
  75. $data = $event->getData();
  76. self::assertEquals('1', $data->getId());
  77. self::assertEquals($user, $data->getUser());
  78. self::assertEquals(
  79. (new DateTimeImmutable('2023-01-05', $tz))->getTimeStamp(),
  80. $data->getStartDate(),
  81. );
  82. self::assertEquals(
  83. (new DateTimeImmutable('2023-01-10', $tz))->getTimeStamp() + 3600 * 23 + 59 * 60,
  84. $data->getEndDate(),
  85. );
  86. self::assertEquals('status', $data->getShortMessage());
  87. self::assertEquals('message', $data->getMessage());
  88. return true;
  89. }));
  90. $this->timeFactory->expects(self::once())
  91. ->method('getTime')
  92. ->willReturn(PHP_INT_MAX);
  93. $this->jobList->expects(self::never())
  94. ->method('scheduleAfter');
  95. $this->absenceService->createOrUpdateAbsence(
  96. $user,
  97. '2023-01-05',
  98. '2023-01-10',
  99. 'status',
  100. 'message',
  101. );
  102. }
  103. public function testUpdateAbsenceEmitsChangedEvent(): void {
  104. $tz = new DateTimeZone('Europe/Berlin');
  105. $user = $this->createMock(IUser::class);
  106. $user->method('getUID')
  107. ->willReturn('user');
  108. $absence = new Absence();
  109. $absence->setId(1);
  110. $absence->setFirstDay('1970-01-01');
  111. $absence->setLastDay('1970-01-10');
  112. $absence->setStatus('old status');
  113. $absence->setMessage('old message');
  114. $this->absenceMapper->expects(self::once())
  115. ->method('findByUserId')
  116. ->with('user')
  117. ->willReturn($absence);
  118. $this->absenceMapper->expects(self::once())
  119. ->method('update')
  120. ->willReturnCallback(static function (Absence $absence): Absence {
  121. self::assertEquals('2023-01-05', $absence->getFirstDay());
  122. self::assertEquals('2023-01-10', $absence->getLastDay());
  123. self::assertEquals('status', $absence->getStatus());
  124. self::assertEquals('message', $absence->getMessage());
  125. return $absence;
  126. });
  127. $this->timezoneService->expects(self::once())
  128. ->method('getUserTimezone')
  129. ->with('user')
  130. ->willReturn('Europe/Berlin');
  131. $this->eventDispatcher->expects(self::once())
  132. ->method('dispatchTyped')
  133. ->with(self::callback(static function (Event $event) use ($user, $tz): bool {
  134. self::assertInstanceOf(OutOfOfficeChangedEvent::class, $event);
  135. /** @var OutOfOfficeChangedEvent $event */
  136. $data = $event->getData();
  137. self::assertEquals('1', $data->getId());
  138. self::assertEquals($user, $data->getUser());
  139. self::assertEquals(
  140. (new DateTimeImmutable('2023-01-05', $tz))->getTimeStamp(),
  141. $data->getStartDate(),
  142. );
  143. self::assertEquals(
  144. (new DateTimeImmutable('2023-01-10', $tz))->getTimeStamp() + 3600 * 23 + 59 * 60,
  145. $data->getEndDate(),
  146. );
  147. self::assertEquals('status', $data->getShortMessage());
  148. self::assertEquals('message', $data->getMessage());
  149. return true;
  150. }));
  151. $this->timeFactory->expects(self::once())
  152. ->method('getTime')
  153. ->willReturn(PHP_INT_MAX);
  154. $this->jobList->expects(self::never())
  155. ->method('scheduleAfter');
  156. $this->absenceService->createOrUpdateAbsence(
  157. $user,
  158. '2023-01-05',
  159. '2023-01-10',
  160. 'status',
  161. 'message',
  162. );
  163. }
  164. public function testCreateAbsenceSchedulesBothJobs(): void {
  165. $tz = new DateTimeZone('Europe/Berlin');
  166. $startDateString = '2023-01-05';
  167. $startDate = new DateTimeImmutable($startDateString, $tz);
  168. $endDateString = '2023-01-10';
  169. $endDate = new DateTimeImmutable($endDateString, $tz);
  170. $user = $this->createMock(IUser::class);
  171. $user->method('getUID')
  172. ->willReturn('user');
  173. $this->absenceMapper->expects(self::once())
  174. ->method('findByUserId')
  175. ->with('user')
  176. ->willThrowException(new DoesNotExistException('foo bar'));
  177. $this->absenceMapper->expects(self::once())
  178. ->method('insert')
  179. ->willReturnCallback(function (Absence $absence): Absence {
  180. $absence->setId(1);
  181. return $absence;
  182. });
  183. $this->timezoneService->expects(self::once())
  184. ->method('getUserTimezone')
  185. ->with('user')
  186. ->willReturn($tz->getName());
  187. $this->timeFactory->expects(self::once())
  188. ->method('getTime')
  189. ->willReturn((new DateTimeImmutable('2023-01-01', $tz))->getTimestamp());
  190. $this->jobList->expects(self::exactly(2))
  191. ->method('scheduleAfter')
  192. ->willReturnMap([
  193. [OutOfOfficeEventDispatcherJob::class, $startDate->getTimestamp(), [
  194. 'id' => '1',
  195. 'event' => OutOfOfficeEventDispatcherJob::EVENT_START,
  196. ]],
  197. [OutOfOfficeEventDispatcherJob::class, $endDate->getTimestamp() + 3600 * 23 + 59 * 60, [
  198. 'id' => '1',
  199. 'event' => OutOfOfficeEventDispatcherJob::EVENT_END,
  200. ]],
  201. ]);
  202. $this->absenceService->createOrUpdateAbsence(
  203. $user,
  204. $startDateString,
  205. $endDateString,
  206. '',
  207. '',
  208. );
  209. }
  210. public function testCreateAbsenceSchedulesOnlyEndJob(): void {
  211. $tz = new DateTimeZone('Europe/Berlin');
  212. $endDateString = '2023-01-10';
  213. $endDate = new DateTimeImmutable($endDateString, $tz);
  214. $user = $this->createMock(IUser::class);
  215. $user->method('getUID')
  216. ->willReturn('user');
  217. $this->absenceMapper->expects(self::once())
  218. ->method('findByUserId')
  219. ->with('user')
  220. ->willThrowException(new DoesNotExistException('foo bar'));
  221. $this->absenceMapper->expects(self::once())
  222. ->method('insert')
  223. ->willReturnCallback(function (Absence $absence): Absence {
  224. $absence->setId(1);
  225. return $absence;
  226. });
  227. $this->timezoneService->expects(self::once())
  228. ->method('getUserTimezone')
  229. ->with('user')
  230. ->willReturn($tz->getName());
  231. $this->timeFactory->expects(self::once())
  232. ->method('getTime')
  233. ->willReturn((new DateTimeImmutable('2023-01-07', $tz))->getTimestamp());
  234. $this->jobList->expects(self::once())
  235. ->method('scheduleAfter')
  236. ->with(OutOfOfficeEventDispatcherJob::class, $endDate->getTimestamp() + 3600 * 23 + 59 * 60, [
  237. 'id' => '1',
  238. 'event' => OutOfOfficeEventDispatcherJob::EVENT_END,
  239. ]);
  240. $this->absenceService->createOrUpdateAbsence(
  241. $user,
  242. '2023-01-05',
  243. $endDateString,
  244. '',
  245. '',
  246. );
  247. }
  248. public function testCreateAbsenceSchedulesNoJob(): void {
  249. $tz = new DateTimeZone('Europe/Berlin');
  250. $user = $this->createMock(IUser::class);
  251. $user->method('getUID')
  252. ->willReturn('user');
  253. $this->absenceMapper->expects(self::once())
  254. ->method('findByUserId')
  255. ->with('user')
  256. ->willThrowException(new DoesNotExistException('foo bar'));
  257. $this->absenceMapper->expects(self::once())
  258. ->method('insert')
  259. ->willReturnCallback(function (Absence $absence): Absence {
  260. $absence->setId(1);
  261. return $absence;
  262. });
  263. $this->timezoneService->expects(self::once())
  264. ->method('getUserTimezone')
  265. ->with('user')
  266. ->willReturn($tz->getName());
  267. $this->timeFactory->expects(self::once())
  268. ->method('getTime')
  269. ->willReturn((new DateTimeImmutable('2023-01-12', $tz))->getTimestamp());
  270. $this->jobList->expects(self::never())
  271. ->method('scheduleAfter');
  272. $this->absenceService->createOrUpdateAbsence(
  273. $user,
  274. '2023-01-05',
  275. '2023-01-10',
  276. '',
  277. '',
  278. );
  279. }
  280. public function testUpdateAbsenceSchedulesBothJobs(): void {
  281. $tz = new DateTimeZone('Europe/Berlin');
  282. $startDateString = '2023-01-05';
  283. $startDate = new DateTimeImmutable($startDateString, $tz);
  284. $endDateString = '2023-01-10';
  285. $endDate = new DateTimeImmutable($endDateString, $tz);
  286. $user = $this->createMock(IUser::class);
  287. $user->method('getUID')
  288. ->willReturn('user');
  289. $absence = new Absence();
  290. $absence->setId(1);
  291. $absence->setFirstDay('1970-01-01');
  292. $absence->setLastDay('1970-01-10');
  293. $absence->setStatus('old status');
  294. $absence->setMessage('old message');
  295. $this->absenceMapper->expects(self::once())
  296. ->method('findByUserId')
  297. ->with('user')
  298. ->willReturn($absence);
  299. $this->absenceMapper->expects(self::once())
  300. ->method('update')
  301. ->willReturnCallback(static function (Absence $absence) use ($startDateString, $endDateString): Absence {
  302. self::assertEquals($startDateString, $absence->getFirstDay());
  303. self::assertEquals($endDateString, $absence->getLastDay());
  304. return $absence;
  305. });
  306. $this->timezoneService->expects(self::once())
  307. ->method('getUserTimezone')
  308. ->with('user')
  309. ->willReturn($tz->getName());
  310. $this->timeFactory->expects(self::once())
  311. ->method('getTime')
  312. ->willReturn((new DateTimeImmutable('2023-01-01', $tz))->getTimestamp());
  313. $this->jobList->expects(self::exactly(2))
  314. ->method('scheduleAfter')
  315. ->willReturnMap([
  316. [OutOfOfficeEventDispatcherJob::class, $startDate->getTimestamp(), [
  317. 'id' => '1',
  318. 'event' => OutOfOfficeEventDispatcherJob::EVENT_START,
  319. ]],
  320. [OutOfOfficeEventDispatcherJob::class, $endDate->getTimestamp() + 3600 * 23 + 59 * 60, [
  321. 'id' => '1',
  322. 'event' => OutOfOfficeEventDispatcherJob::EVENT_END,
  323. ]],
  324. ]);
  325. $this->absenceService->createOrUpdateAbsence(
  326. $user,
  327. $startDateString,
  328. $endDateString,
  329. '',
  330. '',
  331. );
  332. }
  333. public function testUpdateSchedulesOnlyEndJob(): void {
  334. $tz = new DateTimeZone('Europe/Berlin');
  335. $endDateString = '2023-01-10';
  336. $endDate = new DateTimeImmutable($endDateString, $tz);
  337. $user = $this->createMock(IUser::class);
  338. $user->method('getUID')
  339. ->willReturn('user');
  340. $absence = new Absence();
  341. $absence->setId(1);
  342. $absence->setFirstDay('1970-01-01');
  343. $absence->setLastDay('1970-01-10');
  344. $absence->setStatus('old status');
  345. $absence->setMessage('old message');
  346. $this->absenceMapper->expects(self::once())
  347. ->method('findByUserId')
  348. ->with('user')
  349. ->willReturn($absence);
  350. $this->absenceMapper->expects(self::once())
  351. ->method('update')
  352. ->willReturnCallback(static function (Absence $absence) use ($endDateString): Absence {
  353. self::assertEquals('2023-01-05', $absence->getFirstDay());
  354. self::assertEquals($endDateString, $absence->getLastDay());
  355. return $absence;
  356. });
  357. $this->timezoneService->expects(self::once())
  358. ->method('getUserTimezone')
  359. ->with('user')
  360. ->willReturn($tz->getName());
  361. $this->timeFactory->expects(self::once())
  362. ->method('getTime')
  363. ->willReturn((new DateTimeImmutable('2023-01-07', $tz))->getTimestamp());
  364. $this->jobList->expects(self::once())
  365. ->method('scheduleAfter')
  366. ->with(OutOfOfficeEventDispatcherJob::class, $endDate->getTimestamp() + 23 * 3600 + 59 * 60, [
  367. 'id' => '1',
  368. 'event' => OutOfOfficeEventDispatcherJob::EVENT_END,
  369. ]);
  370. $this->absenceService->createOrUpdateAbsence(
  371. $user,
  372. '2023-01-05',
  373. $endDateString,
  374. '',
  375. '',
  376. );
  377. }
  378. public function testUpdateAbsenceSchedulesNoJob(): void {
  379. $tz = new DateTimeZone('Europe/Berlin');
  380. $user = $this->createMock(IUser::class);
  381. $user->method('getUID')
  382. ->willReturn('user');
  383. $absence = new Absence();
  384. $absence->setId(1);
  385. $absence->setFirstDay('1970-01-01');
  386. $absence->setLastDay('1970-01-10');
  387. $absence->setStatus('old status');
  388. $absence->setMessage('old message');
  389. $this->absenceMapper->expects(self::once())
  390. ->method('findByUserId')
  391. ->with('user')
  392. ->willReturn($absence);
  393. $this->absenceMapper->expects(self::once())
  394. ->method('update')
  395. ->willReturnCallback(static function (Absence $absence): Absence {
  396. self::assertEquals('2023-01-05', $absence->getFirstDay());
  397. self::assertEquals('2023-01-10', $absence->getLastDay());
  398. return $absence;
  399. });
  400. $this->timezoneService->expects(self::once())
  401. ->method('getUserTimezone')
  402. ->with('user')
  403. ->willReturn($tz->getName());
  404. $this->timeFactory->expects(self::once())
  405. ->method('getTime')
  406. ->willReturn((new DateTimeImmutable('2023-01-12', $tz))->getTimestamp());
  407. $this->jobList->expects(self::never())
  408. ->method('scheduleAfter');
  409. $this->absenceService->createOrUpdateAbsence(
  410. $user,
  411. '2023-01-05',
  412. '2023-01-10',
  413. '',
  414. '',
  415. );
  416. }
  417. }