1
0

AvailabilityCoordinatorTest.php 6.6 KB

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