Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: drop fallback for availability coordinator #10244

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 1 addition & 14 deletions lib/Service/OutOfOfficeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,18 @@
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IUser;
use OCP\User\IAvailabilityCoordinator;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;

class OutOfOfficeService {
private ?IAvailabilityCoordinator $availabilityCoordinator;

public function __construct(
private OutOfOfficeParser $outOfOfficeParser,
private SieveService $sieveService,
private LoggerInterface $logger,
private AliasesService $aliasesService,
private ITimeFactory $timeFactory,
ContainerInterface $container,
private IAvailabilityCoordinator $availabilityCoordinator,
) {
// TODO: inject directly if we only support Nextcloud >= 28
try {
$this->availabilityCoordinator = $container->get(IAvailabilityCoordinator::class);
} catch (ContainerExceptionInterface $e) {
$this->availabilityCoordinator = null;
}
}

/**
Expand Down Expand Up @@ -102,10 +93,6 @@ public function update(MailAccount $account, OutOfOfficeState $state): void {
* @throws InvalidArgumentException If the given mail account doesn't follow out-of-office settings
*/
public function updateFromSystem(MailAccount $mailAccount, IUser $user): ?OutOfOfficeState {
if ($this->availabilityCoordinator === null) {
throw new ServiceException('System out-of-office data is only available in Nextcloud >= 28');
}

if (!$mailAccount->getOutOfOfficeFollowsSystem()) {
throw new InvalidArgumentException('The mail account does not follow system out-of-office settings');
}
Expand Down
36 changes: 5 additions & 31 deletions tests/Unit/Service/OutOfOfficeServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,40 +19,16 @@
use OCA\Mail\Service\OutOfOfficeService;
use OCA\Mail\Sieve\NamedSieveScript;
use OCP\IUser;
use OCP\User\IAvailabilityCoordinator;
use OCP\User\IOutOfOfficeData;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Container\ContainerInterface;

class OutOfOfficeServiceTest extends TestCase {
private ServiceMockObject $serviceMock;
private OutOfOfficeService $outOfOfficeService;

/** @var ContainerInterface|MockObject */
private $container;

/** @var IAvailabilityCoordinator|MockObject */
private $availabilityCoordinator;

protected function setUp(): void {
parent::setUp();

if (!interface_exists(IAvailabilityCoordinator::class)
|| !interface_exists(IOutOfOfficeData::class)) {
$this->markTestSkipped('Out-of-office feature is not available');
}

$this->container = $this->createMock(ContainerInterface::class);
$this->availabilityCoordinator = $this->createMock(IAvailabilityCoordinator::class);

$this->container->expects(self::once())
->method('get')
->with(IAvailabilityCoordinator::class)
->willReturn($this->availabilityCoordinator);

$this->serviceMock = $this->createServiceMock(OutOfOfficeService::class, [
'container' => $this->container,
]);
$this->serviceMock = $this->createServiceMock(OutOfOfficeService::class);
$this->outOfOfficeService = $this->serviceMock->getService();
}

Expand All @@ -64,10 +40,6 @@ private function createOutOfOfficeData(
string $subject,
string $message,
): ?IOutOfOfficeData {
if (!interface_exists(IOutOfOfficeData::class)) {
return null;
}

$data = $this->createMock(IOutOfOfficeData::class);
$data->method('getId')->willReturn($id);
$data->method('getUser')->willReturn($user);
Expand Down Expand Up @@ -119,7 +91,8 @@ public function testUpdateFromSystemWithEnabledOutOfOffice(?IOutOfOfficeData $da
$timeFactory->expects(self::once())
->method('getTime')
->willReturn(1500);
$this->availabilityCoordinator->expects(self::once())
$availabilityCoordinator = $this->serviceMock->getParameter('availabilityCoordinator');
$availabilityCoordinator->expects(self::once())
->method('getCurrentOutOfOfficeData')
->with($user)
->willReturn($data);
Expand Down Expand Up @@ -196,7 +169,8 @@ public function testUpdateFromSystemWithDisabledOutOfOffice(?IOutOfOfficeData $d
$timeFactory->expects(self::once())
->method('getTime')
->willReturn(1500);
$this->availabilityCoordinator->expects(self::once())
$availabilityCoordinator = $this->serviceMock->getParameter('availabilityCoordinator');
$availabilityCoordinator->expects(self::once())
->method('getCurrentOutOfOfficeData')
->with($user)
->willReturn($data);
Expand Down
Loading