StatusServiceTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\DAV\Tests\unit\CalDAV\Status;
  7. use OC\Calendar\CalendarQuery;
  8. use OCA\DAV\CalDAV\CalendarImpl;
  9. use OCA\DAV\CalDAV\Status\StatusService;
  10. use OCA\UserStatus\Db\UserStatus;
  11. use OCA\UserStatus\Service\StatusService as UserStatusService;
  12. use OCP\AppFramework\Db\DoesNotExistException;
  13. use OCP\AppFramework\Utility\ITimeFactory;
  14. use OCP\Calendar\IManager;
  15. use OCP\ICache;
  16. use OCP\ICacheFactory;
  17. use OCP\IUser;
  18. use OCP\IUserManager;
  19. use OCP\User\IAvailabilityCoordinator;
  20. use OCP\User\IOutOfOfficeData;
  21. use OCP\UserStatus\IUserStatus;
  22. use PHPUnit\Framework\MockObject\MockObject;
  23. use Psr\Log\LoggerInterface;
  24. use Test\TestCase;
  25. class StatusServiceTest extends TestCase {
  26. private ITimeFactory|MockObject $timeFactory;
  27. private IManager|MockObject $calendarManager;
  28. private IUserManager|MockObject $userManager;
  29. private UserStatusService|MockObject $userStatusService;
  30. private IAvailabilityCoordinator|MockObject $availabilityCoordinator;
  31. private ICacheFactory|MockObject $cacheFactory;
  32. private LoggerInterface|MockObject $logger;
  33. private ICache|MockObject $cache;
  34. private StatusService $service;
  35. protected function setUp(): void {
  36. parent::setUp();
  37. $this->timeFactory = $this->createMock(ITimeFactory::class);
  38. $this->calendarManager = $this->createMock(IManager::class);
  39. $this->userManager = $this->createMock(IUserManager::class);
  40. $this->userStatusService = $this->createMock(UserStatusService::class);
  41. $this->availabilityCoordinator = $this->createMock(IAvailabilityCoordinator::class);
  42. $this->cacheFactory = $this->createMock(ICacheFactory::class);
  43. $this->logger = $this->createMock(LoggerInterface::class);
  44. $this->cache = $this->createMock(ICache::class);
  45. $this->cacheFactory->expects(self::once())
  46. ->method('createLocal')
  47. ->with('CalendarStatusService')
  48. ->willReturn($this->cache);
  49. $this->service = new StatusService($this->timeFactory,
  50. $this->calendarManager,
  51. $this->userManager,
  52. $this->userStatusService,
  53. $this->availabilityCoordinator,
  54. $this->cacheFactory,
  55. $this->logger,
  56. );
  57. }
  58. public function testNoUser(): void {
  59. $this->userManager->expects(self::once())
  60. ->method('get')
  61. ->willReturn(null);
  62. $this->availabilityCoordinator->expects(self::never())
  63. ->method('getCurrentOutOfOfficeData');
  64. $this->availabilityCoordinator->expects(self::never())
  65. ->method('isInEffect');
  66. $this->logger->expects(self::never())
  67. ->method('debug');
  68. $this->cache->expects(self::never())
  69. ->method('get');
  70. $this->cache->expects(self::never())
  71. ->method('set');
  72. $this->calendarManager->expects(self::never())
  73. ->method('getCalendarsForPrincipal');
  74. $this->calendarManager->expects(self::never())
  75. ->method('newQuery');
  76. $this->timeFactory->expects(self::never())
  77. ->method('getDateTime');
  78. $this->calendarManager->expects(self::never())
  79. ->method('searchForPrincipal');
  80. $this->userStatusService->expects(self::never())
  81. ->method('revertUserStatus');
  82. $this->userStatusService->expects(self::never())
  83. ->method('setUserStatus');
  84. $this->userStatusService->expects(self::never())
  85. ->method('findByUserId');
  86. $this->service->processCalendarStatus('admin');
  87. }
  88. public function testOOOInEffect(): void {
  89. $user = $this->createConfiguredMock(IUser::class, [
  90. 'getUID' => 'admin',
  91. ]);
  92. $this->userManager->expects(self::once())
  93. ->method('get')
  94. ->willReturn($user);
  95. $this->availabilityCoordinator->expects(self::once())
  96. ->method('getCurrentOutOfOfficeData')
  97. ->willReturn($this->createMock(IOutOfOfficeData::class));
  98. $this->availabilityCoordinator->expects(self::once())
  99. ->method('isInEffect')
  100. ->willReturn(true);
  101. $this->logger->expects(self::once())
  102. ->method('debug');
  103. $this->cache->expects(self::never())
  104. ->method('get');
  105. $this->cache->expects(self::never())
  106. ->method('set');
  107. $this->calendarManager->expects(self::never())
  108. ->method('getCalendarsForPrincipal');
  109. $this->calendarManager->expects(self::never())
  110. ->method('newQuery');
  111. $this->timeFactory->expects(self::never())
  112. ->method('getDateTime');
  113. $this->calendarManager->expects(self::never())
  114. ->method('searchForPrincipal');
  115. $this->userStatusService->expects(self::never())
  116. ->method('revertUserStatus');
  117. $this->userStatusService->expects(self::never())
  118. ->method('setUserStatus');
  119. $this->userStatusService->expects(self::never())
  120. ->method('findByUserId');
  121. $this->service->processCalendarStatus('admin');
  122. }
  123. public function testNoCalendars(): void {
  124. $user = $this->createConfiguredMock(IUser::class, [
  125. 'getUID' => 'admin',
  126. ]);
  127. $this->userManager->expects(self::once())
  128. ->method('get')
  129. ->willReturn($user);
  130. $this->availabilityCoordinator->expects(self::once())
  131. ->method('getCurrentOutOfOfficeData')
  132. ->willReturn(null);
  133. $this->availabilityCoordinator->expects(self::never())
  134. ->method('isInEffect');
  135. $this->cache->expects(self::once())
  136. ->method('get')
  137. ->willReturn(null);
  138. $this->cache->expects(self::once())
  139. ->method('set');
  140. $this->calendarManager->expects(self::once())
  141. ->method('getCalendarsForPrincipal')
  142. ->willReturn([]);
  143. $this->calendarManager->expects(self::never())
  144. ->method('newQuery');
  145. $this->timeFactory->expects(self::never())
  146. ->method('getDateTime');
  147. $this->calendarManager->expects(self::never())
  148. ->method('searchForPrincipal');
  149. $this->userStatusService->expects(self::once())
  150. ->method('revertUserStatus');
  151. $this->logger->expects(self::once())
  152. ->method('debug');
  153. $this->userStatusService->expects(self::never())
  154. ->method('setUserStatus');
  155. $this->userStatusService->expects(self::never())
  156. ->method('findByUserId');
  157. $this->service->processCalendarStatus('admin');
  158. }
  159. public function testNoCalendarEvents(): void {
  160. $user = $this->createConfiguredMock(IUser::class, [
  161. 'getUID' => 'admin',
  162. ]);
  163. $this->userManager->expects(self::once())
  164. ->method('get')
  165. ->willReturn($user);
  166. $this->availabilityCoordinator->expects(self::once())
  167. ->method('getCurrentOutOfOfficeData')
  168. ->willReturn(null);
  169. $this->availabilityCoordinator->expects(self::never())
  170. ->method('isInEffect');
  171. $this->cache->expects(self::once())
  172. ->method('get')
  173. ->willReturn(null);
  174. $this->cache->expects(self::once())
  175. ->method('set');
  176. $this->calendarManager->expects(self::once())
  177. ->method('getCalendarsForPrincipal')
  178. ->willReturn([$this->createMock(CalendarImpl::class)]);
  179. $this->calendarManager->expects(self::once())
  180. ->method('newQuery')
  181. ->willReturn(new CalendarQuery('admin'));
  182. $this->timeFactory->expects(self::exactly(2))
  183. ->method('getDateTime')
  184. ->willReturn(new \DateTime());
  185. $this->calendarManager->expects(self::once())
  186. ->method('searchForPrincipal')
  187. ->willReturn([]);
  188. $this->userStatusService->expects(self::once())
  189. ->method('revertUserStatus');
  190. $this->logger->expects(self::once())
  191. ->method('debug');
  192. $this->userStatusService->expects(self::never())
  193. ->method('setUserStatus');
  194. $this->userStatusService->expects(self::never())
  195. ->method('findByUserId');
  196. $this->service->processCalendarStatus('admin');
  197. }
  198. public function testCalendarNoEventObjects(): void {
  199. $user = $this->createConfiguredMock(IUser::class, [
  200. 'getUID' => 'admin',
  201. ]);
  202. $this->userManager->expects(self::once())
  203. ->method('get')
  204. ->willReturn($user);
  205. $this->availabilityCoordinator->expects(self::once())
  206. ->method('getCurrentOutOfOfficeData')
  207. ->willReturn(null);
  208. $this->availabilityCoordinator->expects(self::never())
  209. ->method('isInEffect');
  210. $this->cache->expects(self::once())
  211. ->method('get')
  212. ->willReturn(null);
  213. $this->cache->expects(self::once())
  214. ->method('set');
  215. $this->calendarManager->expects(self::once())
  216. ->method('getCalendarsForPrincipal')
  217. ->willReturn([$this->createMock(CalendarImpl::class)]);
  218. $this->calendarManager->expects(self::once())
  219. ->method('newQuery')
  220. ->willReturn(new CalendarQuery('admin'));
  221. $this->timeFactory->expects(self::exactly(2))
  222. ->method('getDateTime')
  223. ->willReturn(new \DateTime());
  224. $this->userStatusService->expects(self::once())
  225. ->method('findByUserId')
  226. ->willThrowException(new DoesNotExistException(''));
  227. $this->calendarManager->expects(self::once())
  228. ->method('searchForPrincipal')
  229. ->willReturn([['objects' => []]]);
  230. $this->userStatusService->expects(self::once())
  231. ->method('revertUserStatus');
  232. $this->logger->expects(self::once())
  233. ->method('debug');
  234. $this->userStatusService->expects(self::never())
  235. ->method('setUserStatus');
  236. $this->service->processCalendarStatus('admin');
  237. }
  238. public function testCalendarEvent(): void {
  239. $user = $this->createConfiguredMock(IUser::class, [
  240. 'getUID' => 'admin',
  241. ]);
  242. $this->userManager->expects(self::once())
  243. ->method('get')
  244. ->willReturn($user);
  245. $this->availabilityCoordinator->expects(self::once())
  246. ->method('getCurrentOutOfOfficeData')
  247. ->willReturn(null);
  248. $this->availabilityCoordinator->expects(self::never())
  249. ->method('isInEffect');
  250. $this->cache->expects(self::once())
  251. ->method('get')
  252. ->willReturn(null);
  253. $this->cache->expects(self::once())
  254. ->method('set');
  255. $this->calendarManager->expects(self::once())
  256. ->method('getCalendarsForPrincipal')
  257. ->willReturn([$this->createMock(CalendarImpl::class)]);
  258. $this->calendarManager->expects(self::once())
  259. ->method('newQuery')
  260. ->willReturn(new CalendarQuery('admin'));
  261. $this->timeFactory->expects(self::exactly(2))
  262. ->method('getDateTime')
  263. ->willReturn(new \DateTime());
  264. $this->userStatusService->expects(self::once())
  265. ->method('findByUserId')
  266. ->willThrowException(new DoesNotExistException(''));
  267. $this->calendarManager->expects(self::once())
  268. ->method('searchForPrincipal')
  269. ->willReturn([['objects' => [[]]]]);
  270. $this->userStatusService->expects(self::never())
  271. ->method('revertUserStatus');
  272. $this->logger->expects(self::once())
  273. ->method('debug');
  274. $this->userStatusService->expects(self::once())
  275. ->method('setUserStatus');
  276. $this->service->processCalendarStatus('admin');
  277. }
  278. public function testCallStatus(): void {
  279. $user = $this->createConfiguredMock(IUser::class, [
  280. 'getUID' => 'admin',
  281. ]);
  282. $this->userManager->expects(self::once())
  283. ->method('get')
  284. ->willReturn($user);
  285. $this->availabilityCoordinator->expects(self::once())
  286. ->method('getCurrentOutOfOfficeData')
  287. ->willReturn(null);
  288. $this->availabilityCoordinator->expects(self::never())
  289. ->method('isInEffect');
  290. $this->cache->expects(self::once())
  291. ->method('get')
  292. ->willReturn(null);
  293. $this->cache->expects(self::once())
  294. ->method('set');
  295. $this->calendarManager->expects(self::once())
  296. ->method('getCalendarsForPrincipal')
  297. ->willReturn([$this->createMock(CalendarImpl::class)]);
  298. $this->calendarManager->expects(self::once())
  299. ->method('newQuery')
  300. ->willReturn(new CalendarQuery('admin'));
  301. $this->timeFactory->expects(self::exactly(2))
  302. ->method('getDateTime')
  303. ->willReturn(new \DateTime());
  304. $this->calendarManager->expects(self::once())
  305. ->method('searchForPrincipal')
  306. ->willReturn([['objects' => [[]]]]);
  307. $userStatus = new UserStatus();
  308. $userStatus->setMessageId(IUserStatus::MESSAGE_CALL);
  309. $userStatus->setStatusTimestamp(123456);
  310. $this->userStatusService->expects(self::once())
  311. ->method('findByUserId')
  312. ->willReturn($userStatus);
  313. $this->logger->expects(self::once())
  314. ->method('debug');
  315. $this->userStatusService->expects(self::never())
  316. ->method('revertUserStatus');
  317. $this->userStatusService->expects(self::never())
  318. ->method('setUserStatus');
  319. $this->service->processCalendarStatus('admin');
  320. }
  321. public function testInvisibleStatus(): void {
  322. $user = $this->createConfiguredMock(IUser::class, [
  323. 'getUID' => 'admin',
  324. ]);
  325. $this->userManager->expects(self::once())
  326. ->method('get')
  327. ->willReturn($user);
  328. $this->availabilityCoordinator->expects(self::once())
  329. ->method('getCurrentOutOfOfficeData')
  330. ->willReturn(null);
  331. $this->availabilityCoordinator->expects(self::never())
  332. ->method('isInEffect');
  333. $this->cache->expects(self::once())
  334. ->method('get')
  335. ->willReturn(null);
  336. $this->cache->expects(self::once())
  337. ->method('set');
  338. $this->calendarManager->expects(self::once())
  339. ->method('getCalendarsForPrincipal')
  340. ->willReturn([$this->createMock(CalendarImpl::class)]);
  341. $this->calendarManager->expects(self::once())
  342. ->method('newQuery')
  343. ->willReturn(new CalendarQuery('admin'));
  344. $this->timeFactory->expects(self::exactly(2))
  345. ->method('getDateTime')
  346. ->willReturn(new \DateTime());
  347. $this->calendarManager->expects(self::once())
  348. ->method('searchForPrincipal')
  349. ->willReturn([['objects' => [[]]]]);
  350. $userStatus = new UserStatus();
  351. $userStatus->setStatus(IUserStatus::INVISIBLE);
  352. $userStatus->setStatusTimestamp(123456);
  353. $this->userStatusService->expects(self::once())
  354. ->method('findByUserId')
  355. ->willReturn($userStatus);
  356. $this->logger->expects(self::once())
  357. ->method('debug');
  358. $this->userStatusService->expects(self::never())
  359. ->method('revertUserStatus');
  360. $this->userStatusService->expects(self::never())
  361. ->method('setUserStatus');
  362. $this->service->processCalendarStatus('admin');
  363. }
  364. public function testDNDStatus(): void {
  365. $user = $this->createConfiguredMock(IUser::class, [
  366. 'getUID' => 'admin',
  367. ]);
  368. $this->userManager->expects(self::once())
  369. ->method('get')
  370. ->willReturn($user);
  371. $this->availabilityCoordinator->expects(self::once())
  372. ->method('getCurrentOutOfOfficeData')
  373. ->willReturn(null);
  374. $this->availabilityCoordinator->expects(self::never())
  375. ->method('isInEffect');
  376. $this->cache->expects(self::once())
  377. ->method('get')
  378. ->willReturn(null);
  379. $this->cache->expects(self::once())
  380. ->method('set');
  381. $this->calendarManager->expects(self::once())
  382. ->method('getCalendarsForPrincipal')
  383. ->willReturn([$this->createMock(CalendarImpl::class)]);
  384. $this->calendarManager->expects(self::once())
  385. ->method('newQuery')
  386. ->willReturn(new CalendarQuery('admin'));
  387. $this->timeFactory->expects(self::exactly(2))
  388. ->method('getDateTime')
  389. ->willReturn(new \DateTime());
  390. $this->calendarManager->expects(self::once())
  391. ->method('searchForPrincipal')
  392. ->willReturn([['objects' => [[]]]]);
  393. $userStatus = new UserStatus();
  394. $userStatus->setStatus(IUserStatus::DND);
  395. $userStatus->setStatusTimestamp(123456);
  396. $this->userStatusService->expects(self::once())
  397. ->method('findByUserId')
  398. ->willReturn($userStatus);
  399. $this->logger->expects(self::once())
  400. ->method('debug');
  401. $this->userStatusService->expects(self::never())
  402. ->method('revertUserStatus');
  403. $this->userStatusService->expects(self::never())
  404. ->method('setUserStatus');
  405. $this->service->processCalendarStatus('admin');
  406. }
  407. }