123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460 |
- <?php
- /**
- * @copyright 2023 Anna Larch <anna.larch@gmx.net>
- *
- * @author Anna Larch <anna.larch@gmx.net>
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- */
- namespace OCA\DAV\Tests\unit\CalDAV\Status;
- use OC\Calendar\CalendarQuery;
- use OCA\DAV\CalDAV\CalendarImpl;
- use OCA\DAV\CalDAV\Status\StatusService;
- use OCA\UserStatus\Db\UserStatus;
- use OCA\UserStatus\Service\StatusService as UserStatusService;
- use OCP\AppFramework\Db\DoesNotExistException;
- use OCP\AppFramework\Utility\ITimeFactory;
- use OCP\Calendar\IManager;
- use OCP\ICache;
- use OCP\ICacheFactory;
- use OCP\IUser;
- use OCP\IUserManager;
- use OCP\User\IAvailabilityCoordinator;
- use OCP\User\IOutOfOfficeData;
- use OCP\UserStatus\IUserStatus;
- use PHPUnit\Framework\MockObject\MockObject;
- use Psr\Log\LoggerInterface;
- use Test\TestCase;
- class StatusServiceTest extends TestCase {
- private ITimeFactory|MockObject $timeFactory;
- private IManager|MockObject $calendarManager;
- private IUserManager|MockObject $userManager;
- private UserStatusService|MockObject $userStatusService;
- private IAvailabilityCoordinator|MockObject $availabilityCoordinator;
- private ICacheFactory|MockObject $cacheFactory;
- private LoggerInterface|MockObject $logger;
- private ICache|MockObject $cache;
- private StatusService $service;
- protected function setUp(): void {
- parent::setUp();
- $this->timeFactory = $this->createMock(ITimeFactory::class);
- $this->calendarManager = $this->createMock(IManager::class);
- $this->userManager = $this->createMock(IUserManager::class);
- $this->userStatusService = $this->createMock(UserStatusService::class);
- $this->availabilityCoordinator = $this->createMock(IAvailabilityCoordinator::class);
- $this->cacheFactory = $this->createMock(ICacheFactory::class);
- $this->logger = $this->createMock(LoggerInterface::class);
- $this->cache = $this->createMock(ICache::class);
- $this->cacheFactory->expects(self::once())
- ->method('createLocal')
- ->with('CalendarStatusService')
- ->willReturn($this->cache);
- $this->service = new StatusService($this->timeFactory,
- $this->calendarManager,
- $this->userManager,
- $this->userStatusService,
- $this->availabilityCoordinator,
- $this->cacheFactory,
- $this->logger,
- );
- }
- public function testNoUser(): void {
- $this->userManager->expects(self::once())
- ->method('get')
- ->willReturn(null);
- $this->availabilityCoordinator->expects(self::never())
- ->method('getCurrentOutOfOfficeData');
- $this->availabilityCoordinator->expects(self::never())
- ->method('isInEffect');
- $this->logger->expects(self::never())
- ->method('debug');
- $this->cache->expects(self::never())
- ->method('get');
- $this->cache->expects(self::never())
- ->method('set');
- $this->calendarManager->expects(self::never())
- ->method('getCalendarsForPrincipal');
- $this->calendarManager->expects(self::never())
- ->method('newQuery');
- $this->timeFactory->expects(self::never())
- ->method('getDateTime');
- $this->calendarManager->expects(self::never())
- ->method('searchForPrincipal');
- $this->userStatusService->expects(self::never())
- ->method('revertUserStatus');
- $this->userStatusService->expects(self::never())
- ->method('setUserStatus');
- $this->userStatusService->expects(self::never())
- ->method('findByUserId');
- $this->service->processCalendarStatus('admin');
- }
- public function testOOOInEffect(): void {
- $user = $this->createConfiguredMock(IUser::class, [
- 'getUID' => 'admin',
- ]);
- $this->userManager->expects(self::once())
- ->method('get')
- ->willReturn($user);
- $this->availabilityCoordinator->expects(self::once())
- ->method('getCurrentOutOfOfficeData')
- ->willReturn($this->createMock(IOutOfOfficeData::class));
- $this->availabilityCoordinator->expects(self::once())
- ->method('isInEffect')
- ->willReturn(true);
- $this->logger->expects(self::once())
- ->method('debug');
- $this->cache->expects(self::never())
- ->method('get');
- $this->cache->expects(self::never())
- ->method('set');
- $this->calendarManager->expects(self::never())
- ->method('getCalendarsForPrincipal');
- $this->calendarManager->expects(self::never())
- ->method('newQuery');
- $this->timeFactory->expects(self::never())
- ->method('getDateTime');
- $this->calendarManager->expects(self::never())
- ->method('searchForPrincipal');
- $this->userStatusService->expects(self::never())
- ->method('revertUserStatus');
- $this->userStatusService->expects(self::never())
- ->method('setUserStatus');
- $this->userStatusService->expects(self::never())
- ->method('findByUserId');
- $this->service->processCalendarStatus('admin');
- }
- public function testNoCalendars(): void {
- $user = $this->createConfiguredMock(IUser::class, [
- 'getUID' => 'admin',
- ]);
- $this->userManager->expects(self::once())
- ->method('get')
- ->willReturn($user);
- $this->availabilityCoordinator->expects(self::once())
- ->method('getCurrentOutOfOfficeData')
- ->willReturn(null);
- $this->availabilityCoordinator->expects(self::never())
- ->method('isInEffect');
- $this->cache->expects(self::once())
- ->method('get')
- ->willReturn(null);
- $this->cache->expects(self::once())
- ->method('set');
- $this->calendarManager->expects(self::once())
- ->method('getCalendarsForPrincipal')
- ->willReturn([]);
- $this->calendarManager->expects(self::never())
- ->method('newQuery');
- $this->timeFactory->expects(self::never())
- ->method('getDateTime');
- $this->calendarManager->expects(self::never())
- ->method('searchForPrincipal');
- $this->userStatusService->expects(self::once())
- ->method('revertUserStatus');
- $this->logger->expects(self::once())
- ->method('debug');
- $this->userStatusService->expects(self::never())
- ->method('setUserStatus');
- $this->userStatusService->expects(self::never())
- ->method('findByUserId');
- $this->service->processCalendarStatus('admin');
- }
- public function testNoCalendarEvents(): void {
- $user = $this->createConfiguredMock(IUser::class, [
- 'getUID' => 'admin',
- ]);
- $this->userManager->expects(self::once())
- ->method('get')
- ->willReturn($user);
- $this->availabilityCoordinator->expects(self::once())
- ->method('getCurrentOutOfOfficeData')
- ->willReturn(null);
- $this->availabilityCoordinator->expects(self::never())
- ->method('isInEffect');
- $this->cache->expects(self::once())
- ->method('get')
- ->willReturn(null);
- $this->cache->expects(self::once())
- ->method('set');
- $this->calendarManager->expects(self::once())
- ->method('getCalendarsForPrincipal')
- ->willReturn([$this->createMock(CalendarImpl::class)]);
- $this->calendarManager->expects(self::once())
- ->method('newQuery')
- ->willReturn(new CalendarQuery('admin'));
- $this->timeFactory->expects(self::exactly(2))
- ->method('getDateTime')
- ->willReturn(new \DateTime());
- $this->calendarManager->expects(self::once())
- ->method('searchForPrincipal')
- ->willReturn([]);
- $this->userStatusService->expects(self::once())
- ->method('revertUserStatus');
- $this->logger->expects(self::once())
- ->method('debug');
- $this->userStatusService->expects(self::never())
- ->method('setUserStatus');
- $this->userStatusService->expects(self::never())
- ->method('findByUserId');
- $this->service->processCalendarStatus('admin');
- }
- public function testCalendarNoEventObjects(): void {
- $user = $this->createConfiguredMock(IUser::class, [
- 'getUID' => 'admin',
- ]);
- $this->userManager->expects(self::once())
- ->method('get')
- ->willReturn($user);
- $this->availabilityCoordinator->expects(self::once())
- ->method('getCurrentOutOfOfficeData')
- ->willReturn(null);
- $this->availabilityCoordinator->expects(self::never())
- ->method('isInEffect');
- $this->cache->expects(self::once())
- ->method('get')
- ->willReturn(null);
- $this->cache->expects(self::once())
- ->method('set');
- $this->calendarManager->expects(self::once())
- ->method('getCalendarsForPrincipal')
- ->willReturn([$this->createMock(CalendarImpl::class)]);
- $this->calendarManager->expects(self::once())
- ->method('newQuery')
- ->willReturn(new CalendarQuery('admin'));
- $this->timeFactory->expects(self::exactly(2))
- ->method('getDateTime')
- ->willReturn(new \DateTime());
- $this->userStatusService->expects(self::once())
- ->method('findByUserId')
- ->willThrowException(new DoesNotExistException(''));
- $this->calendarManager->expects(self::once())
- ->method('searchForPrincipal')
- ->willReturn([['objects' => []]]);
- $this->userStatusService->expects(self::once())
- ->method('revertUserStatus');
- $this->logger->expects(self::once())
- ->method('debug');
- $this->userStatusService->expects(self::never())
- ->method('setUserStatus');
- $this->service->processCalendarStatus('admin');
- }
- public function testCalendarEvent(): void {
- $user = $this->createConfiguredMock(IUser::class, [
- 'getUID' => 'admin',
- ]);
- $this->userManager->expects(self::once())
- ->method('get')
- ->willReturn($user);
- $this->availabilityCoordinator->expects(self::once())
- ->method('getCurrentOutOfOfficeData')
- ->willReturn(null);
- $this->availabilityCoordinator->expects(self::never())
- ->method('isInEffect');
- $this->cache->expects(self::once())
- ->method('get')
- ->willReturn(null);
- $this->cache->expects(self::once())
- ->method('set');
- $this->calendarManager->expects(self::once())
- ->method('getCalendarsForPrincipal')
- ->willReturn([$this->createMock(CalendarImpl::class)]);
- $this->calendarManager->expects(self::once())
- ->method('newQuery')
- ->willReturn(new CalendarQuery('admin'));
- $this->timeFactory->expects(self::exactly(2))
- ->method('getDateTime')
- ->willReturn(new \DateTime());
- $this->userStatusService->expects(self::once())
- ->method('findByUserId')
- ->willThrowException(new DoesNotExistException(''));
- $this->calendarManager->expects(self::once())
- ->method('searchForPrincipal')
- ->willReturn([['objects' => [[]]]]);
- $this->userStatusService->expects(self::never())
- ->method('revertUserStatus');
- $this->logger->expects(self::once())
- ->method('debug');
- $this->userStatusService->expects(self::once())
- ->method('setUserStatus');
- $this->service->processCalendarStatus('admin');
- }
- public function testCallStatus(): void {
- $user = $this->createConfiguredMock(IUser::class, [
- 'getUID' => 'admin',
- ]);
- $this->userManager->expects(self::once())
- ->method('get')
- ->willReturn($user);
- $this->availabilityCoordinator->expects(self::once())
- ->method('getCurrentOutOfOfficeData')
- ->willReturn(null);
- $this->availabilityCoordinator->expects(self::never())
- ->method('isInEffect');
- $this->cache->expects(self::once())
- ->method('get')
- ->willReturn(null);
- $this->cache->expects(self::once())
- ->method('set');
- $this->calendarManager->expects(self::once())
- ->method('getCalendarsForPrincipal')
- ->willReturn([$this->createMock(CalendarImpl::class)]);
- $this->calendarManager->expects(self::once())
- ->method('newQuery')
- ->willReturn(new CalendarQuery('admin'));
- $this->timeFactory->expects(self::exactly(2))
- ->method('getDateTime')
- ->willReturn(new \DateTime());
- $this->calendarManager->expects(self::once())
- ->method('searchForPrincipal')
- ->willReturn([['objects' => [[]]]]);
- $userStatus = new UserStatus();
- $userStatus->setMessageId(IUserStatus::MESSAGE_CALL);
- $userStatus->setStatusTimestamp(123456);
- $this->userStatusService->expects(self::once())
- ->method('findByUserId')
- ->willReturn($userStatus);
- $this->logger->expects(self::once())
- ->method('debug');
- $this->userStatusService->expects(self::never())
- ->method('revertUserStatus');
- $this->userStatusService->expects(self::never())
- ->method('setUserStatus');
- $this->service->processCalendarStatus('admin');
- }
- public function testInvisibleStatus(): void {
- $user = $this->createConfiguredMock(IUser::class, [
- 'getUID' => 'admin',
- ]);
- $this->userManager->expects(self::once())
- ->method('get')
- ->willReturn($user);
- $this->availabilityCoordinator->expects(self::once())
- ->method('getCurrentOutOfOfficeData')
- ->willReturn(null);
- $this->availabilityCoordinator->expects(self::never())
- ->method('isInEffect');
- $this->cache->expects(self::once())
- ->method('get')
- ->willReturn(null);
- $this->cache->expects(self::once())
- ->method('set');
- $this->calendarManager->expects(self::once())
- ->method('getCalendarsForPrincipal')
- ->willReturn([$this->createMock(CalendarImpl::class)]);
- $this->calendarManager->expects(self::once())
- ->method('newQuery')
- ->willReturn(new CalendarQuery('admin'));
- $this->timeFactory->expects(self::exactly(2))
- ->method('getDateTime')
- ->willReturn(new \DateTime());
- $this->calendarManager->expects(self::once())
- ->method('searchForPrincipal')
- ->willReturn([['objects' => [[]]]]);
- $userStatus = new UserStatus();
- $userStatus->setStatus(IUserStatus::INVISIBLE);
- $userStatus->setStatusTimestamp(123456);
- $this->userStatusService->expects(self::once())
- ->method('findByUserId')
- ->willReturn($userStatus);
- $this->logger->expects(self::once())
- ->method('debug');
- $this->userStatusService->expects(self::never())
- ->method('revertUserStatus');
- $this->userStatusService->expects(self::never())
- ->method('setUserStatus');
- $this->service->processCalendarStatus('admin');
- }
- public function testDNDStatus(): void {
- $user = $this->createConfiguredMock(IUser::class, [
- 'getUID' => 'admin',
- ]);
- $this->userManager->expects(self::once())
- ->method('get')
- ->willReturn($user);
- $this->availabilityCoordinator->expects(self::once())
- ->method('getCurrentOutOfOfficeData')
- ->willReturn(null);
- $this->availabilityCoordinator->expects(self::never())
- ->method('isInEffect');
- $this->cache->expects(self::once())
- ->method('get')
- ->willReturn(null);
- $this->cache->expects(self::once())
- ->method('set');
- $this->calendarManager->expects(self::once())
- ->method('getCalendarsForPrincipal')
- ->willReturn([$this->createMock(CalendarImpl::class)]);
- $this->calendarManager->expects(self::once())
- ->method('newQuery')
- ->willReturn(new CalendarQuery('admin'));
- $this->timeFactory->expects(self::exactly(2))
- ->method('getDateTime')
- ->willReturn(new \DateTime());
- $this->calendarManager->expects(self::once())
- ->method('searchForPrincipal')
- ->willReturn([['objects' => [[]]]]);
- $userStatus = new UserStatus();
- $userStatus->setStatus(IUserStatus::DND);
- $userStatus->setStatusTimestamp(123456);
- $this->userStatusService->expects(self::once())
- ->method('findByUserId')
- ->willReturn($userStatus);
- $this->logger->expects(self::once())
- ->method('debug');
- $this->userStatusService->expects(self::never())
- ->method('revertUserStatus');
- $this->userStatusService->expects(self::never())
- ->method('setUserStatus');
- $this->service->processCalendarStatus('admin');
- }
- }
|