AvailabilityCoordinatorTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. $absence->setReplacementUserId('batman');
  64. $absence->setReplacementUserDisplayName('Bruce Wayne');
  65. $this->timezoneService->method('getUserTimezone')->with('user')->willReturn('Europe/Berlin');
  66. $user = $this->createMock(IUser::class);
  67. $user->method('getUID')
  68. ->willReturn('user');
  69. $this->cache->expects(self::exactly(2))
  70. ->method('get')
  71. ->willReturnOnConsecutiveCalls(null, null);
  72. $this->absenceService->expects(self::once())
  73. ->method('getAbsence')
  74. ->with($user->getUID())
  75. ->willReturn($absence);
  76. $this->cache->expects(self::exactly(2))
  77. ->method('set')
  78. ->withConsecutive([$user->getUID() . '_timezone', 'Europe/Berlin', 3600],
  79. [$user->getUID(), '{"id":"420","startDate":1696111200,"endDate":1696802340,"shortMessage":"Vacation","message":"On vacation","replacementUserId":"batman","replacementUserDisplayName":"Bruce Wayne"}', 300]);
  80. $expected = new OutOfOfficeData(
  81. '420',
  82. $user,
  83. 1696111200,
  84. 1696802340,
  85. 'Vacation',
  86. 'On vacation',
  87. 'batman',
  88. 'Bruce Wayne',
  89. );
  90. $actual = $this->availabilityCoordinator->getCurrentOutOfOfficeData($user);
  91. self::assertEquals($expected, $actual);
  92. }
  93. public function testGetOutOfOfficeDataCachedAll(): void {
  94. $absence = new Absence();
  95. $absence->setId(420);
  96. $absence->setUserId('user');
  97. $absence->setFirstDay('2023-10-01');
  98. $absence->setLastDay('2023-10-08');
  99. $absence->setStatus('Vacation');
  100. $absence->setMessage('On vacation');
  101. $absence->setReplacementUserId('batman');
  102. $absence->setReplacementUserDisplayName('Bruce Wayne');
  103. $user = $this->createMock(IUser::class);
  104. $user->method('getUID')
  105. ->willReturn('user');
  106. $this->cache->expects(self::exactly(2))
  107. ->method('get')
  108. ->willReturnOnConsecutiveCalls('UTC', '{"id":"420","startDate":1696118400,"endDate":1696809540,"shortMessage":"Vacation","message":"On vacation","replacementUserId":"batman","replacementUserDisplayName":"Bruce Wayne"}');
  109. $this->absenceService->expects(self::never())
  110. ->method('getAbsence');
  111. $this->cache->expects(self::exactly(1))
  112. ->method('set');
  113. $expected = new OutOfOfficeData(
  114. '420',
  115. $user,
  116. 1696118400,
  117. 1696809540,
  118. 'Vacation',
  119. 'On vacation',
  120. 'batman',
  121. 'Bruce Wayne'
  122. );
  123. $actual = $this->availabilityCoordinator->getCurrentOutOfOfficeData($user);
  124. self::assertEquals($expected, $actual);
  125. }
  126. public function testGetOutOfOfficeDataNoData(): void {
  127. $absence = new Absence();
  128. $absence->setId(420);
  129. $absence->setUserId('user');
  130. $absence->setFirstDay('2023-10-01');
  131. $absence->setLastDay('2023-10-08');
  132. $absence->setStatus('Vacation');
  133. $absence->setMessage('On vacation');
  134. $user = $this->createMock(IUser::class);
  135. $user->method('getUID')
  136. ->willReturn('user');
  137. $this->cache->expects(self::exactly(2))
  138. ->method('get')
  139. ->willReturnOnConsecutiveCalls('UTC', null);
  140. $this->absenceService->expects(self::once())
  141. ->method('getAbsence')
  142. ->willReturn(null);
  143. $this->cache->expects(self::never())
  144. ->method('set');
  145. $actual = $this->availabilityCoordinator->getCurrentOutOfOfficeData($user);
  146. self::assertNull($actual);
  147. }
  148. public function testGetOutOfOfficeDataWithInvalidCachedData(): void {
  149. $absence = new Absence();
  150. $absence->setId(420);
  151. $absence->setUserId('user');
  152. $absence->setFirstDay('2023-10-01');
  153. $absence->setLastDay('2023-10-08');
  154. $absence->setStatus('Vacation');
  155. $absence->setMessage('On vacation');
  156. $absence->setReplacementUserId('batman');
  157. $absence->setReplacementUserDisplayName('Bruce Wayne');
  158. $this->timezoneService->method('getUserTimezone')->with('user')->willReturn('Europe/Berlin');
  159. $user = $this->createMock(IUser::class);
  160. $user->method('getUID')
  161. ->willReturn('user');
  162. $this->cache->expects(self::exactly(2))
  163. ->method('get')
  164. ->willReturnOnConsecutiveCalls('UTC', '{"id":"420",}');
  165. $this->absenceService->expects(self::once())
  166. ->method('getAbsence')
  167. ->with('user')
  168. ->willReturn($absence);
  169. $this->cache->expects(self::once())
  170. ->method('set')
  171. ->with('user', '{"id":"420","startDate":1696118400,"endDate":1696809540,"shortMessage":"Vacation","message":"On vacation","replacementUserId":"batman","replacementUserDisplayName":"Bruce Wayne"}', 300);
  172. $expected = new OutOfOfficeData(
  173. '420',
  174. $user,
  175. 1696118400,
  176. 1696809540,
  177. 'Vacation',
  178. 'On vacation',
  179. 'batman',
  180. 'Bruce Wayne'
  181. );
  182. $actual = $this->availabilityCoordinator->getCurrentOutOfOfficeData($user);
  183. self::assertEquals($expected, $actual);
  184. }
  185. }