Skip to content

Commit

Permalink
Merge pull request #10244 from nextcloud/debt/noid/ooo-inject-availab…
Browse files Browse the repository at this point in the history
…ility-coordinator

refactor: drop fallback for availability coordinator
  • Loading branch information
kesselb authored Oct 10, 2024
2 parents b6f3726 + 0cae3ce commit eae40bf
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 45 deletions.
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

0 comments on commit eae40bf

Please sign in to comment.