AvailabilityCoordinatorTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 Test\User;
  8. use OC\User\AvailabilityCoordinator;
  9. use OC\User\OutOfOfficeData;
  10. use OCA\DAV\CalDAV\TimezoneService;
  11. use OCA\DAV\Db\Absence;
  12. use OCA\DAV\Service\AbsenceService;
  13. use OCP\ICache;
  14. use OCP\ICacheFactory;
  15. use OCP\IConfig;
  16. use OCP\IUser;
  17. use PHPUnit\Framework\MockObject\MockObject;
  18. use Psr\Log\LoggerInterface;
  19. use Test\TestCase;
  20. class AvailabilityCoordinatorTest extends TestCase {
  21. private AvailabilityCoordinator $availabilityCoordinator;
  22. private ICacheFactory $cacheFactory;
  23. private ICache $cache;
  24. private IConfig|MockObject $config;
  25. private AbsenceService $absenceService;
  26. private LoggerInterface $logger;
  27. private MockObject|TimezoneService $timezoneService;
  28. protected function setUp(): void {
  29. parent::setUp();
  30. $this->cacheFactory = $this->createMock(ICacheFactory::class);
  31. $this->cache = $this->createMock(ICache::class);
  32. $this->absenceService = $this->createMock(AbsenceService::class);
  33. $this->config = $this->createMock(IConfig::class);
  34. $this->logger = $this->createMock(LoggerInterface::class);
  35. $this->timezoneService = $this->createMock(TimezoneService::class);
  36. $this->cacheFactory->expects(self::once())
  37. ->method('createLocal')
  38. ->willReturn($this->cache);
  39. $this->availabilityCoordinator = new AvailabilityCoordinator(
  40. $this->cacheFactory,
  41. $this->config,
  42. $this->absenceService,
  43. $this->logger,
  44. $this->timezoneService,
  45. );
  46. }
  47. public function testIsEnabled(): void {
  48. $this->config->expects(self::once())
  49. ->method('getAppValue')
  50. ->with('dav', 'hide_absence_settings', 'no')
  51. ->willReturn('no');
  52. $isEnabled = $this->availabilityCoordinator->isEnabled();
  53. self::assertTrue($isEnabled);
  54. }
  55. public function testGetOutOfOfficeDataInEffect(): void {
  56. $absence = new Absence();
  57. $absence->setId(420);
  58. $absence->setUserId('user');
  59. $absence->setFirstDay('2023-10-01');
  60. $absence->setLastDay('2023-10-08');
  61. $absence->setStatus('Vacation');
  62. $absence->setMessage('On vacation');
  63. $this->timezoneService->method('getUserTimezone')->with('user')->willReturn('Europe/Berlin');
  64. $user = $this->createMock(IUser::class);
  65. $user->method('getUID')
  66. ->willReturn('user');
  67. $this->cache->expects(self::exactly(2))
  68. ->method('get')
  69. ->willReturnOnConsecutiveCalls(null, null);
  70. $this->absenceService->expects(self::once())
  71. ->method('getAbsence')
  72. ->with($user->getUID())
  73. ->willReturn($absence);
  74. $this->cache->expects(self::exactly(2))
  75. ->method('set')
  76. ->withConsecutive([$user->getUID() . '_timezone', 'Europe/Berlin', 3600],
  77. [$user->getUID(), '{"id":"420","startDate":1696111200,"endDate":1696802340,"shortMessage":"Vacation","message":"On vacation"}', 300]);
  78. $expected = new OutOfOfficeData(
  79. '420',
  80. $user,
  81. 1696111200,
  82. 1696802340,
  83. 'Vacation',
  84. 'On vacation',
  85. );
  86. $actual = $this->availabilityCoordinator->getCurrentOutOfOfficeData($user);
  87. self::assertEquals($expected, $actual);
  88. }
  89. public function testGetOutOfOfficeDataCachedAll(): void {
  90. $absence = new Absence();
  91. $absence->setId(420);
  92. $absence->setUserId('user');
  93. $absence->setFirstDay('2023-10-01');
  94. $absence->setLastDay('2023-10-08');
  95. $absence->setStatus('Vacation');
  96. $absence->setMessage('On vacation');
  97. $user = $this->createMock(IUser::class);
  98. $user->method('getUID')
  99. ->willReturn('user');
  100. $this->cache->expects(self::exactly(2))
  101. ->method('get')
  102. ->willReturnOnConsecutiveCalls('UTC', '{"id":"420","startDate":1696118400,"endDate":1696809540,"shortMessage":"Vacation","message":"On vacation"}');
  103. $this->absenceService->expects(self::never())
  104. ->method('getAbsence');
  105. $this->cache->expects(self::exactly(1))
  106. ->method('set');
  107. $expected = new OutOfOfficeData(
  108. '420',
  109. $user,
  110. 1696118400,
  111. 1696809540,
  112. 'Vacation',
  113. 'On vacation',
  114. );
  115. $actual = $this->availabilityCoordinator->getCurrentOutOfOfficeData($user);
  116. self::assertEquals($expected, $actual);
  117. }
  118. public function testGetOutOfOfficeDataNoData(): void {
  119. $absence = new Absence();
  120. $absence->setId(420);
  121. $absence->setUserId('user');
  122. $absence->setFirstDay('2023-10-01');
  123. $absence->setLastDay('2023-10-08');
  124. $absence->setStatus('Vacation');
  125. $absence->setMessage('On vacation');
  126. $user = $this->createMock(IUser::class);
  127. $user->method('getUID')
  128. ->willReturn('user');
  129. $this->cache->expects(self::exactly(2))
  130. ->method('get')
  131. ->willReturnOnConsecutiveCalls('UTC', null);
  132. $this->absenceService->expects(self::once())
  133. ->method('getAbsence')
  134. ->willReturn(null);
  135. $this->cache->expects(self::never())
  136. ->method('set');
  137. $actual = $this->availabilityCoordinator->getCurrentOutOfOfficeData($user);
  138. self::assertNull($actual);
  139. }
  140. public function testGetOutOfOfficeDataWithInvalidCachedData(): void {
  141. $absence = new Absence();
  142. $absence->setId(420);
  143. $absence->setUserId('user');
  144. $absence->setFirstDay('2023-10-01');
  145. $absence->setLastDay('2023-10-08');
  146. $absence->setStatus('Vacation');
  147. $absence->setMessage('On vacation');
  148. $this->timezoneService->method('getUserTimezone')->with('user')->willReturn('Europe/Berlin');
  149. $user = $this->createMock(IUser::class);
  150. $user->method('getUID')
  151. ->willReturn('user');
  152. $this->cache->expects(self::exactly(2))
  153. ->method('get')
  154. ->willReturnOnConsecutiveCalls('UTC', '{"id":"420",}');
  155. $this->absenceService->expects(self::once())
  156. ->method('getAbsence')
  157. ->with('user')
  158. ->willReturn($absence);
  159. $this->cache->expects(self::once())
  160. ->method('set')
  161. ->with('user', '{"id":"420","startDate":1696118400,"endDate":1696809540,"shortMessage":"Vacation","message":"On vacation"}', 300);
  162. $expected = new OutOfOfficeData(
  163. '420',
  164. $user,
  165. 1696118400,
  166. 1696809540,
  167. 'Vacation',
  168. 'On vacation',
  169. );
  170. $actual = $this->availabilityCoordinator->getCurrentOutOfOfficeData($user);
  171. self::assertEquals($expected, $actual);
  172. }
  173. }