mirror of https://github.com/nextcloud/server.git
feat: Add out-of-office message API
[skipci] Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at> Signed-off-by: Richard Steinmetz <richard@steinmetz.cloud>pull/40653/head
parent
1acc7c0468
commit
ab1a1d688d
@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @copyright Copyright (c) 2023 Richard Steinmetz <richard@steinmetz.cloud>
|
||||
*
|
||||
* @author Richard Steinmetz <richard@steinmetz.cloud>
|
||||
*
|
||||
* @license AGPL-3.0-or-later
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCA\DAV\Controller;
|
||||
|
||||
use OCA\DAV\Db\AbsenceMapper;
|
||||
use OCA\DAV\ResponseDefinitions;
|
||||
use OCP\AppFramework\Db\DoesNotExistException;
|
||||
use OCP\AppFramework\Http;
|
||||
use OCP\AppFramework\Http\DataResponse;
|
||||
use OCP\AppFramework\OCSController;
|
||||
use OCP\IRequest;
|
||||
|
||||
/**
|
||||
* @psalm-import-type DAVOutOfOfficeData from ResponseDefinitions
|
||||
*/
|
||||
class OutOfOfficeController extends OCSController {
|
||||
|
||||
public function __construct(
|
||||
string $appName,
|
||||
IRequest $request,
|
||||
private AbsenceMapper $absenceMapper,
|
||||
) {
|
||||
parent::__construct($appName, $request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the currently configured out-of-office data of a user.
|
||||
*
|
||||
* @NoAdminRequired
|
||||
* @NoCSRFRequired
|
||||
*
|
||||
* @param string $userId The user id to get out-of-office data for.
|
||||
* @return DataResponse<Http::STATUS_OK|Http::STATUS_NOT_FOUND, ?DAVOutOfOfficeData, array{}>
|
||||
*
|
||||
* 200: Out-of-office data
|
||||
* 404: No out-of-office data was found
|
||||
*/
|
||||
public function getCurrentOutOfOfficeData(string $userId): DataResponse {
|
||||
try {
|
||||
$data = $this->absenceMapper->findByUserId($userId);
|
||||
} catch (DoesNotExistException) {
|
||||
return new DataResponse(null, Http::STATUS_NOT_FOUND);
|
||||
}
|
||||
|
||||
return new DataResponse([
|
||||
'id' => $data->getId(),
|
||||
'userId' => $data->getUserId(),
|
||||
'firstDay' => $data->getFirstDay(),
|
||||
'lastDay' => $data->getLastDay(),
|
||||
'status' => $data->getStatus(),
|
||||
'message' => $data->getMessage(),
|
||||
]);
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @copyright Copyright (c) 2023 Richard Steinmetz <richard@steinmetz.cloud>
|
||||
*
|
||||
* @author Richard Steinmetz <richard@steinmetz.cloud>
|
||||
*
|
||||
* @license AGPL-3.0-or-later
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCA\DAV;
|
||||
|
||||
/**
|
||||
* @psalm-type DAVOutOfOfficeData = array{
|
||||
* id: int,
|
||||
* userId: string,
|
||||
* firstDay: string,
|
||||
* lastDay: string,
|
||||
* status: string,
|
||||
* message: string,
|
||||
* }
|
||||
*/
|
||||
class ResponseDefinitions {
|
||||
}
|
@ -0,0 +1,111 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @copyright 2023 Christoph Wurst <christoph@winzerhof-wurst.at>
|
||||
*
|
||||
* @author 2023 Christoph Wurst <christoph@winzerhof-wurst.at>
|
||||
* @author Richard Steinmetz <richard@steinmetz.cloud>
|
||||
*
|
||||
* @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 OC\User;
|
||||
|
||||
use JsonException;
|
||||
use OCA\DAV\Db\AbsenceMapper;
|
||||
use OCP\AppFramework\Db\DoesNotExistException;
|
||||
use OCP\ICache;
|
||||
use OCP\ICacheFactory;
|
||||
use OCP\IUser;
|
||||
use OCP\User\IAvailabilityCoordinator;
|
||||
use OCP\User\IOutOfOfficeData;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class AvailabilityCoordinator implements IAvailabilityCoordinator {
|
||||
private ICache $cache;
|
||||
|
||||
public function __construct(
|
||||
ICacheFactory $cacheFactory,
|
||||
private AbsenceMapper $absenceMapper,
|
||||
private LoggerInterface $logger,
|
||||
) {
|
||||
$this->cache = $cacheFactory->createLocal('OutOfOfficeData');
|
||||
}
|
||||
|
||||
private function getCachedOutOfOfficeData(IUser $user): ?OutOfOfficeData {
|
||||
$cachedString = $this->cache->get($user->getUID());
|
||||
if ($cachedString === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
$cachedData = json_decode($cachedString, true, 10, JSON_THROW_ON_ERROR);
|
||||
} catch (JsonException $e) {
|
||||
$this->logger->error('Failed to deserialize cached out-of-office data: ' . $e->getMessage(), [
|
||||
'exception' => $e,
|
||||
'json' => $cachedString,
|
||||
]);
|
||||
return null;
|
||||
}
|
||||
|
||||
return new OutOfOfficeData(
|
||||
$cachedData['id'],
|
||||
$user,
|
||||
$cachedData['startDate'],
|
||||
$cachedData['endDate'],
|
||||
$cachedData['shortMessage'],
|
||||
$cachedData['message'],
|
||||
);
|
||||
}
|
||||
|
||||
private function setCachedOutOfOfficeData(IOutOfOfficeData $data): void {
|
||||
try {
|
||||
$cachedString = json_encode([
|
||||
'id' => $data->getId(),
|
||||
'startDate' => $data->getStartDate(),
|
||||
'endDate' => $data->getEndDate(),
|
||||
'shortMessage' => $data->getShortMessage(),
|
||||
'message' => $data->getMessage(),
|
||||
], JSON_THROW_ON_ERROR);
|
||||
} catch (JsonException $e) {
|
||||
$this->logger->error('Failed to serialize out-of-office data: ' . $e->getMessage(), [
|
||||
'exception' => $e,
|
||||
]);
|
||||
return;
|
||||
}
|
||||
|
||||
$this->cache->set($data->getUser()->getUID(), $cachedString, 300);
|
||||
}
|
||||
|
||||
public function getCurrentOutOfOfficeData(IUser $user): ?IOutOfOfficeData {
|
||||
$cachedData = $this->getCachedOutOfOfficeData($user);
|
||||
if ($cachedData !== null) {
|
||||
return $cachedData;
|
||||
}
|
||||
|
||||
try {
|
||||
$absenceData = $this->absenceMapper->findByUserId($user->getUID());
|
||||
} catch (DoesNotExistException $e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$data = $absenceData->toOutOufOfficeData($user);
|
||||
$this->setCachedOutOfOfficeData($data);
|
||||
return $data;
|
||||
}
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @copyright 2023 Christoph Wurst <christoph@winzerhof-wurst.at>
|
||||
*
|
||||
* @author 2023 Christoph Wurst <christoph@winzerhof-wurst.at>
|
||||
*
|
||||
* @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 OC\User;
|
||||
|
||||
use OCP\IUser;
|
||||
use OCP\User\IOutOfOfficeData;
|
||||
|
||||
class OutOfOfficeData implements IOutOfOfficeData {
|
||||
public function __construct(private string $id,
|
||||
private IUser $user,
|
||||
private int $startDate,
|
||||
private int $endDate,
|
||||
private string $shortMessage,
|
||||
private string $message) {
|
||||
}
|
||||
|
||||
public function getId(): string {
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getUser(): IUser {
|
||||
return $this->user;
|
||||
}
|
||||
|
||||
public function getStartDate(): int {
|
||||
return $this->startDate;
|
||||
}
|
||||
|
||||
public function getEndDate(): int {
|
||||
return $this->endDate;
|
||||
}
|
||||
|
||||
public function getShortMessage(): string {
|
||||
return $this->shortMessage;
|
||||
}
|
||||
|
||||
public function getMessage(): string {
|
||||
return $this->message;
|
||||
}
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* @copyright 2023 Christoph Wurst <christoph@winzerhof-wurst.at>
|
||||
*
|
||||
* @author 2023 Christoph Wurst <christoph@winzerhof-wurst.at>
|
||||
*
|
||||
* @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 OCP\User\Events;
|
||||
|
||||
use OCP\EventDispatcher\Event;
|
||||
use OCP\User\IOutOfOfficeData;
|
||||
|
||||
/**
|
||||
* Emitted when a user's out-of-office period has changed
|
||||
*
|
||||
* @since 28.0.0
|
||||
*/
|
||||
class OutOfOfficeChangedEvent extends Event {
|
||||
/**
|
||||
* @since 28.0.0
|
||||
*/
|
||||
public function __construct(private IOutOfOfficeData $data) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 28.0.0
|
||||
*/
|
||||
public function getData(): IOutOfOfficeData {
|
||||
return $this->data;
|
||||
}
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* @copyright 2023 Christoph Wurst <christoph@winzerhof-wurst.at>
|
||||
*
|
||||
* @author 2023 Christoph Wurst <christoph@winzerhof-wurst.at>
|
||||
*
|
||||
* @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 OCP\User\Events;
|
||||
|
||||
use OCP\EventDispatcher\Event;
|
||||
use OCP\User\IOutOfOfficeData;
|
||||
|
||||
/**
|
||||
* Emitted when a user's out-of-office period is cleared
|
||||
*
|
||||
* @since 28.0.0
|
||||
*/
|
||||
class OutOfOfficeClearedEvent extends Event {
|
||||
/**
|
||||
* @since 28.0.0
|
||||
*/
|
||||
public function __construct(private IOutOfOfficeData $data) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 28.0.0
|
||||
*/
|
||||
public function getData(): IOutOfOfficeData {
|
||||
return $this->data;
|
||||
}
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* @copyright 2023 Christoph Wurst <christoph@winzerhof-wurst.at>
|
||||
*
|
||||
* @author 2023 Christoph Wurst <christoph@winzerhof-wurst.at>
|
||||
*
|
||||
* @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 OCP\User\Events;
|
||||
|
||||
use OCP\EventDispatcher\Event;
|
||||
use OCP\User\IOutOfOfficeData;
|
||||
|
||||
/**
|
||||
* Emitted when a user's out-of-office period is scheduled
|
||||
*
|
||||
* @since 28.0.0
|
||||
*/
|
||||
class OutOfOfficeScheduledEvent extends Event {
|
||||
/**
|
||||
* @since 28.0.0
|
||||
*/
|
||||
public function __construct(private IOutOfOfficeData $data) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 28.0.0
|
||||
*/
|
||||
public function getData(): IOutOfOfficeData {
|
||||
return $this->data;
|
||||
}
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @copyright 2023 Christoph Wurst <christoph@winzerhof-wurst.at>
|
||||
*
|
||||
* @author 2023 Christoph Wurst <christoph@winzerhof-wurst.at>
|
||||
*
|
||||
* @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 OCP\User;
|
||||
|
||||
use OCP\IUser;
|
||||
|
||||
/**
|
||||
* Coordinator for availability and out-of-office messages
|
||||
*
|
||||
* @since 28.0.0
|
||||
*/
|
||||
interface IAvailabilityCoordinator {
|
||||
/**
|
||||
* Get the user's out-of-office message, if any
|
||||
*
|
||||
* @since 28.0.0
|
||||
*/
|
||||
public function getCurrentOutOfOfficeData(IUser $user): ?IOutOfOfficeData;
|
||||
}
|
@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* @copyright 2023 Christoph Wurst <christoph@winzerhof-wurst.at>
|
||||
*
|
||||
* @author 2023 Christoph Wurst <christoph@winzerhof-wurst.at>
|
||||
*
|
||||
* @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 OCP\User;
|
||||
|
||||
use OCP\IUser;
|
||||
|
||||
/**
|
||||
* DTO to hold out-of-office information of a user
|
||||
*
|
||||
* @since 28.0.0
|
||||
*/
|
||||
interface IOutOfOfficeData {
|
||||
/**
|
||||
* Get the unique token assigned to the current out-of-office event
|
||||
*
|
||||
* @since 28.0.0
|
||||
*/
|
||||
public function getId(): string;
|
||||
|
||||
/**
|
||||
* @since 28.0.0
|
||||
*/
|
||||
public function getUser(): IUser;
|
||||
|
||||
/**
|
||||
* Get the accurate out-of-office start date
|
||||
*
|
||||
* This event is not guaranteed to be emitted exactly at start date
|
||||
*
|
||||
* @since 28.0.0
|
||||
*/
|
||||
public function getStartDate(): int;
|
||||
|
||||
/**
|
||||
* Get the (preliminary) out-of-office end date
|
||||
*
|
||||
* @since 28.0.0
|
||||
*/
|
||||
public function getEndDate(): int;
|
||||
|
||||
/**
|
||||
* Get the short summary text displayed in the user status and similar
|
||||
*
|
||||
* @since 28.0.0
|
||||
*/
|
||||
public function getShortMessage(): string;
|
||||
|
||||
/**
|
||||
* Get the long out-of-office message for auto responders and similar
|
||||
*
|
||||
* @since 28.0.0
|
||||
*/
|
||||
public function getMessage(): string;
|
||||
}
|
@ -0,0 +1,164 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @copyright Copyright (c) 2023 Richard Steinmetz <richard@steinmetz.cloud>
|
||||
*
|
||||
* @author Richard Steinmetz <richard@steinmetz.cloud>
|
||||
*
|
||||
* @license AGPL-3.0-or-later
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Test\User;
|
||||
|
||||
use OC\User\AvailabilityCoordinator;
|
||||
use OC\User\OutOfOfficeData;
|
||||
use OCA\DAV\Db\Absence;
|
||||
use OCA\DAV\Db\AbsenceMapper;
|
||||
use OCP\ICache;
|
||||
use OCP\ICacheFactory;
|
||||
use OCP\IUser;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Test\TestCase;
|
||||
|
||||
class AvailabilityCoordinatorTest extends TestCase {
|
||||
private AvailabilityCoordinator $availabilityCoordinator;
|
||||
private ICacheFactory $cacheFactory;
|
||||
private ICache $cache;
|
||||
private AbsenceMapper $absenceMapper;
|
||||
private LoggerInterface $logger;
|
||||
|
||||
protected function setUp(): void {
|
||||
parent::setUp();
|
||||
|
||||
$this->cacheFactory = $this->createMock(ICacheFactory::class);
|
||||
$this->cache = $this->createMock(ICache::class);
|
||||
$this->absenceMapper = $this->createMock(AbsenceMapper::class);
|
||||
$this->logger = $this->createMock(LoggerInterface::class);
|
||||
|
||||
$this->cacheFactory->expects(self::once())
|
||||
->method('createLocal')
|
||||
->willReturn($this->cache);
|
||||
|
||||
$this->availabilityCoordinator = new AvailabilityCoordinator(
|
||||
$this->cacheFactory,
|
||||
$this->absenceMapper,
|
||||
$this->logger,
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetOutOfOfficeData(): void {
|
||||
$absence = new Absence();
|
||||
$absence->setId(420);
|
||||
$absence->setUserId('user');
|
||||
$absence->setFirstDay('2023-10-01');
|
||||
$absence->setLastDay('2023-10-08');
|
||||
$absence->setStatus('Vacation');
|
||||
$absence->setMessage('On vacation');
|
||||
|
||||
$user = $this->createMock(IUser::class);
|
||||
$user->method('getUID')
|
||||
->willReturn('user');
|
||||
|
||||
$this->cache->expects(self::once())
|
||||
->method('get')
|
||||
->with('user')
|
||||
->willReturn(null);
|
||||
$this->absenceMapper->expects(self::once())
|
||||
->method('findByUserId')
|
||||
->with('user')
|
||||
->willReturn($absence);
|
||||
$this->cache->expects(self::once())
|
||||
->method('set')
|
||||
->with('user', '{"id":"420","startDate":1696118400,"endDate":1696723200,"shortMessage":"Vacation","message":"On vacation"}', 300);
|
||||
|
||||
$expected = new OutOfOfficeData(
|
||||
'420',
|
||||
$user,
|
||||
1696118400,
|
||||
1696723200,
|
||||
'Vacation',
|
||||
'On vacation',
|
||||
);
|
||||
$actual = $this->availabilityCoordinator->getCurrentOutOfOfficeData($user);
|
||||
self::assertEquals($expected, $actual);
|
||||
}
|
||||
|
||||
public function testGetOutOfOfficeDataWithCachedData(): void {
|
||||
$user = $this->createMock(IUser::class);
|
||||
$user->method('getUID')
|
||||
->willReturn('user');
|
||||
|
||||
$this->cache->expects(self::once())
|
||||
->method('get')
|
||||
->with('user')
|
||||
->willReturn('{"id":"420","startDate":1696118400,"endDate":1696723200,"shortMessage":"Vacation","message":"On vacation"}');
|
||||
$this->absenceMapper->expects(self::never())
|
||||
->method('findByUserId');
|
||||
$this->cache->expects(self::never())
|
||||
->method('set');
|
||||
|
||||
$expected = new OutOfOfficeData(
|
||||
'420',
|
||||
$user,
|
||||
1696118400,
|
||||
1696723200,
|
||||
'Vacation',
|
||||
'On vacation',
|
||||
);
|
||||
$actual = $this->availabilityCoordinator->getCurrentOutOfOfficeData($user);
|
||||
self::assertEquals($expected, $actual);
|
||||
}
|
||||
|
||||
public function testGetOutOfOfficeDataWithInvalidCachedData(): void {
|
||||
$absence = new Absence();
|
||||
$absence->setId(420);
|
||||
$absence->setUserId('user');
|
||||
$absence->setFirstDay('2023-10-01');
|
||||
$absence->setLastDay('2023-10-08');
|
||||
$absence->setStatus('Vacation');
|
||||
$absence->setMessage('On vacation');
|
||||
|
||||
$user = $this->createMock(IUser::class);
|
||||
$user->method('getUID')
|
||||
->willReturn('user');
|
||||
|
||||
$this->cache->expects(self::once())
|
||||
->method('get')
|
||||
->with('user')
|
||||
->willReturn('{"id":"420",}');
|
||||
$this->absenceMapper->expects(self::once())
|
||||
->method('findByUserId')
|
||||
->with('user')
|
||||
->willReturn($absence);
|
||||
$this->cache->expects(self::once())
|
||||
->method('set')
|
||||
->with('user', '{"id":"420","startDate":1696118400,"endDate":1696723200,"shortMessage":"Vacation","message":"On vacation"}', 300);
|
||||
|
||||
$expected = new OutOfOfficeData(
|
||||
'420',
|
||||
$user,
|
||||
1696118400,
|
||||
1696723200,
|
||||
'Vacation',
|
||||
'On vacation',
|
||||
);
|
||||
$actual = $this->availabilityCoordinator->getCurrentOutOfOfficeData($user);
|
||||
self::assertEquals($expected, $actual);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue