StatusServiceTest.php 15 KB

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