AbsenceServiceTest.php 14 KB

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