123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- <?php
- declare(strict_types=1);
- /**
- * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
- * SPDX-License-Identifier: AGPL-3.0-or-later
- */
- namespace OCA\UserStatus\Tests\Controller;
- use OCA\DAV\CalDAV\Status\StatusService as CalendarStatusService;
- use OCA\UserStatus\Controller\UserStatusController;
- use OCA\UserStatus\Db\UserStatus;
- use OCA\UserStatus\Exception\InvalidClearAtException;
- use OCA\UserStatus\Exception\InvalidMessageIdException;
- use OCA\UserStatus\Exception\InvalidStatusIconException;
- use OCA\UserStatus\Exception\InvalidStatusTypeException;
- use OCA\UserStatus\Exception\StatusMessageTooLongException;
- use OCA\UserStatus\Service\StatusService;
- use OCP\AppFramework\Db\DoesNotExistException;
- use OCP\AppFramework\OCS\OCSBadRequestException;
- use OCP\AppFramework\OCS\OCSNotFoundException;
- use OCP\IRequest;
- use Psr\Log\LoggerInterface;
- use Test\TestCase;
- use Throwable;
- class UserStatusControllerTest extends TestCase {
- /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
- private $logger;
- /** @var StatusService|\PHPUnit\Framework\MockObject\MockObject */
- private $statusService;
- /** @var CalendarStatusService|\PHPUnit\Framework\MockObject\MockObject $calendarStatusService */
- private $calendarStatusService;
- /** @var UserStatusController */
- private $controller;
- protected function setUp(): void {
- parent::setUp();
- $request = $this->createMock(IRequest::class);
- $userId = 'john.doe';
- $this->logger = $this->createMock(LoggerInterface::class);
- $this->statusService = $this->createMock(StatusService::class);
- $this->calendarStatusService = $this->createMock(CalendarStatusService::class);
- $this->controller = new UserStatusController(
- 'user_status',
- $request,
- $userId,
- $this->logger,
- $this->statusService,
- $this->calendarStatusService,
- );
- }
- public function testGetStatus(): void {
- $userStatus = $this->getUserStatus();
- $this->statusService->expects($this->once())
- ->method('findByUserId')
- ->with('john.doe')
- ->willReturn($userStatus);
- $response = $this->controller->getStatus();
- $this->assertEquals([
- 'userId' => 'john.doe',
- 'status' => 'invisible',
- 'icon' => '🏝',
- 'message' => 'On vacation',
- 'clearAt' => 60000,
- 'statusIsUserDefined' => true,
- 'messageIsPredefined' => false,
- 'messageId' => null,
- ], $response->getData());
- }
- public function testGetStatusDoesNotExist(): void {
- $this->calendarStatusService->expects(self::once())
- ->method('processCalendarStatus')
- ->with('john.doe');
- $this->statusService->expects($this->once())
- ->method('findByUserId')
- ->with('john.doe')
- ->willThrowException(new DoesNotExistException(''));
- $this->expectException(OCSNotFoundException::class);
- $this->expectExceptionMessage('No status for the current user');
- $this->controller->getStatus();
- }
- /**
- * @param string $statusType
- * @param string|null $statusIcon
- * @param string|null $message
- * @param int|null $clearAt
- * @param bool $expectSuccess
- * @param bool $expectException
- * @param Throwable|null $exception
- * @param bool $expectLogger
- * @param string|null $expectedLogMessage
- *
- * @dataProvider setStatusDataProvider
- */
- public function testSetStatus(string $statusType,
- ?string $statusIcon,
- ?string $message,
- ?int $clearAt,
- bool $expectSuccess,
- bool $expectException,
- ?Throwable $exception,
- bool $expectLogger,
- ?string $expectedLogMessage): void {
- $userStatus = $this->getUserStatus();
- if ($expectException) {
- $this->statusService->expects($this->once())
- ->method('setStatus')
- ->with('john.doe', $statusType, null, true)
- ->willThrowException($exception);
- } else {
- $this->statusService->expects($this->once())
- ->method('setStatus')
- ->with('john.doe', $statusType, null, true)
- ->willReturn($userStatus);
- }
- if ($expectLogger) {
- $this->logger->expects($this->once())
- ->method('debug')
- ->with($expectedLogMessage);
- }
- if ($expectException) {
- $this->expectException(OCSBadRequestException::class);
- $this->expectExceptionMessage('Original exception message');
- }
- $response = $this->controller->setStatus($statusType);
- if ($expectSuccess) {
- $this->assertEquals([
- 'userId' => 'john.doe',
- 'status' => 'invisible',
- 'icon' => '🏝',
- 'message' => 'On vacation',
- 'clearAt' => 60000,
- 'statusIsUserDefined' => true,
- 'messageIsPredefined' => false,
- 'messageId' => null,
- ], $response->getData());
- }
- }
- public function setStatusDataProvider(): array {
- return [
- ['busy', '👨🏽💻', 'Busy developing the status feature', 500, true, false, null, false, null],
- ['busy', '👨🏽💻', 'Busy developing the status feature', 500, false, true, new InvalidStatusTypeException('Original exception message'), true,
- 'New user-status for "john.doe" was rejected due to an invalid status type "busy"'],
- ];
- }
- /**
- * @param string $messageId
- * @param int|null $clearAt
- * @param bool $expectSuccess
- * @param bool $expectException
- * @param Throwable|null $exception
- * @param bool $expectLogger
- * @param string|null $expectedLogMessage
- *
- * @dataProvider setPredefinedMessageDataProvider
- */
- public function testSetPredefinedMessage(string $messageId,
- ?int $clearAt,
- bool $expectSuccess,
- bool $expectException,
- ?Throwable $exception,
- bool $expectLogger,
- ?string $expectedLogMessage): void {
- $userStatus = $this->getUserStatus();
- if ($expectException) {
- $this->statusService->expects($this->once())
- ->method('setPredefinedMessage')
- ->with('john.doe', $messageId, $clearAt)
- ->willThrowException($exception);
- } else {
- $this->statusService->expects($this->once())
- ->method('setPredefinedMessage')
- ->with('john.doe', $messageId, $clearAt)
- ->willReturn($userStatus);
- }
- if ($expectLogger) {
- $this->logger->expects($this->once())
- ->method('debug')
- ->with($expectedLogMessage);
- }
- if ($expectException) {
- $this->expectException(OCSBadRequestException::class);
- $this->expectExceptionMessage('Original exception message');
- }
- $response = $this->controller->setPredefinedMessage($messageId, $clearAt);
- if ($expectSuccess) {
- $this->assertEquals([
- 'userId' => 'john.doe',
- 'status' => 'invisible',
- 'icon' => '🏝',
- 'message' => 'On vacation',
- 'clearAt' => 60000,
- 'statusIsUserDefined' => true,
- 'messageIsPredefined' => false,
- 'messageId' => null,
- ], $response->getData());
- }
- }
- public function setPredefinedMessageDataProvider(): array {
- return [
- ['messageId-42', 500, true, false, null, false, null],
- ['messageId-42', 500, false, true, new InvalidClearAtException('Original exception message'), true,
- 'New user-status for "john.doe" was rejected due to an invalid clearAt value "500"'],
- ['messageId-42', 500, false, true, new InvalidMessageIdException('Original exception message'), true,
- 'New user-status for "john.doe" was rejected due to an invalid message-id "messageId-42"'],
- ];
- }
- /**
- * @param string|null $statusIcon
- * @param string $message
- * @param int|null $clearAt
- * @param bool $expectSuccess
- * @param bool $expectException
- * @param Throwable|null $exception
- * @param bool $expectLogger
- * @param string|null $expectedLogMessage
- * @param bool $expectSuccessAsReset
- *
- * @dataProvider setCustomMessageDataProvider
- */
- public function testSetCustomMessage(?string $statusIcon,
- string $message,
- ?int $clearAt,
- bool $expectSuccess,
- bool $expectException,
- ?Throwable $exception,
- bool $expectLogger,
- ?string $expectedLogMessage,
- bool $expectSuccessAsReset = false): void {
- $userStatus = $this->getUserStatus();
- if ($expectException) {
- $this->statusService->expects($this->once())
- ->method('setCustomMessage')
- ->with('john.doe', $statusIcon, $message, $clearAt)
- ->willThrowException($exception);
- } else {
- if ($expectSuccessAsReset) {
- $this->statusService->expects($this->never())
- ->method('setCustomMessage');
- $this->statusService->expects($this->once())
- ->method('clearMessage')
- ->with('john.doe');
- $this->statusService->expects($this->once())
- ->method('findByUserId')
- ->with('john.doe')
- ->willReturn($userStatus);
- } else {
- $this->statusService->expects($this->once())
- ->method('setCustomMessage')
- ->with('john.doe', $statusIcon, $message, $clearAt)
- ->willReturn($userStatus);
- $this->statusService->expects($this->never())
- ->method('clearMessage');
- }
- }
- if ($expectLogger) {
- $this->logger->expects($this->once())
- ->method('debug')
- ->with($expectedLogMessage);
- }
- if ($expectException) {
- $this->expectException(OCSBadRequestException::class);
- $this->expectExceptionMessage('Original exception message');
- }
- $response = $this->controller->setCustomMessage($statusIcon, $message, $clearAt);
- if ($expectSuccess) {
- $this->assertEquals([
- 'userId' => 'john.doe',
- 'status' => 'invisible',
- 'icon' => '🏝',
- 'message' => 'On vacation',
- 'clearAt' => 60000,
- 'statusIsUserDefined' => true,
- 'messageIsPredefined' => false,
- 'messageId' => null,
- ], $response->getData());
- }
- }
- public function setCustomMessageDataProvider(): array {
- return [
- ['👨🏽💻', 'Busy developing the status feature', 500, true, false, null, false, null],
- ['👨🏽💻', '', 500, true, false, null, false, null, false],
- ['👨🏽💻', '', 0, true, false, null, false, null, false],
- ['👨🏽💻', 'Busy developing the status feature', 500, false, true, new InvalidClearAtException('Original exception message'), true,
- 'New user-status for "john.doe" was rejected due to an invalid clearAt value "500"'],
- ['👨🏽💻', 'Busy developing the status feature', 500, false, true, new InvalidStatusIconException('Original exception message'), true,
- 'New user-status for "john.doe" was rejected due to an invalid icon value "👨🏽💻"'],
- ['👨🏽💻', 'Busy developing the status feature', 500, false, true, new StatusMessageTooLongException('Original exception message'), true,
- 'New user-status for "john.doe" was rejected due to a too long status message.'],
- ];
- }
- public function testClearMessage(): void {
- $this->statusService->expects($this->once())
- ->method('clearMessage')
- ->with('john.doe');
- $response = $this->controller->clearMessage();
- $this->assertEquals([], $response->getData());
- }
- private function getUserStatus(): UserStatus {
- $userStatus = new UserStatus();
- $userStatus->setId(1337);
- $userStatus->setUserId('john.doe');
- $userStatus->setStatus('invisible');
- $userStatus->setStatusTimestamp(5000);
- $userStatus->setIsUserDefined(true);
- $userStatus->setCustomIcon('🏝');
- $userStatus->setCustomMessage('On vacation');
- $userStatus->setClearAt(60000);
- return $userStatus;
- }
- }
|