From b136fc727b1ff287f0de2367942b5e9e199778dd Mon Sep 17 00:00:00 2001 From: Anna Larch Date: Tue, 16 Aug 2022 18:54:40 +0200 Subject: [PATCH] fixup! Add imip processing --- .../PreviewEnhancementProcessingJob.php | 4 +- lib/Service/PreprocessingService.php | 5 + .../PreviewEnhancementProcessingJobTest.php | 187 ++++++++++++++++++ tests/Unit/Service/IMipServiceTest.php | 33 ++++ .../Unit/Service/PreprocessingServiceTest.php | 8 - 5 files changed, 227 insertions(+), 10 deletions(-) create mode 100644 tests/Unit/Job/PreviewEnhancementProcessingJobTest.php diff --git a/lib/BackgroundJob/PreviewEnhancementProcessingJob.php b/lib/BackgroundJob/PreviewEnhancementProcessingJob.php index e05fc42b89..5ac55bd651 100644 --- a/lib/BackgroundJob/PreviewEnhancementProcessingJob.php +++ b/lib/BackgroundJob/PreviewEnhancementProcessingJob.php @@ -64,7 +64,7 @@ public function __construct(ITimeFactory $time, /** * @return void */ - protected function run($argument) { + public function run($argument) { $accountId = (int)$argument['accountId']; try { @@ -92,7 +92,7 @@ protected function run($argument) { } try { - $limitTimestamp = $this->time->getTime() - 60 * 60 * 24 * 14; // Two weeks into the past + $limitTimestamp = $this->time->getTime() - (60 * 60 * 24 * 14); // Two weeks into the past $this->preprocessingService->process($limitTimestamp, $account); } catch (Exception $e) { $this->logger->error('Error processing messages', ['exception' => $e]); diff --git a/lib/Service/PreprocessingService.php b/lib/Service/PreprocessingService.php index ad05e51fb6..46065a6ae6 100644 --- a/lib/Service/PreprocessingService.php +++ b/lib/Service/PreprocessingService.php @@ -57,10 +57,15 @@ public function __construct( */ public function process(int $limitTimestamp, Account $account): void { $mailboxes = $this->mailboxMapper->findAll($account); + if (empty($mailboxes)) { + $this->logger->info('No mailboxes found.'); + return; + } $mailboxIds = array_unique(array_map(function (Mailbox $mailbox) { return $mailbox->getId(); }, $mailboxes)); + $messages = $this->messageMapper->getUnanalysed($limitTimestamp, $mailboxIds); if (empty($messages)) { $this->logger->info('No structure data to analyse.'); diff --git a/tests/Unit/Job/PreviewEnhancementProcessingJobTest.php b/tests/Unit/Job/PreviewEnhancementProcessingJobTest.php new file mode 100644 index 0000000000..846ec43b32 --- /dev/null +++ b/tests/Unit/Job/PreviewEnhancementProcessingJobTest.php @@ -0,0 +1,187 @@ + + * * + * * @author Anna Larch + * * + * * This library is free software; you can redistribute it and/or + * * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE + * * License as published by the Free Software Foundation; either + * * version 3 of the License, or any later version. + * * + * * This library 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 library. If not, see . + * * + * + */ + +namespace OCA\Mail\Tests\Unit\Job; + +use OCA\Mail\Account; +use OCA\Mail\BackgroundJob\PreviewEnhancementProcessingJob; +use OCA\Mail\Db\MailAccount; +use OCA\Mail\Service\AccountService; +use OCA\Mail\Service\PreprocessingService; +use OCP\AppFramework\Db\DoesNotExistException; +use OCP\AppFramework\Utility\ITimeFactory; +use OCP\BackgroundJob\IJobList; +use OCP\DB\Exception; +use OCP\IUser; +use OCP\IUserManager; +use PHPUnit\Framework\MockObject\MockObject; +use ChristophWurst\Nextcloud\Testing\TestCase; +use Psr\Log\LoggerInterface; + +class PreviewEnhancementProcessingJobTest extends TestCase { + + /** @var ITimeFactory|ITimeFactory&MockObject|MockObject */ + private $time; + + /** @var IUserManager|IUserManager&MockObject|MockObject */ + private $manager; + + /** @var AccountService|AccountService&MockObject|MockObject */ + private $accountService; + + /** @var PreprocessingService|PreprocessingService&MockObject|MockObject */ + private $preprocessingService; + + /** @var MockObject|LoggerInterface|LoggerInterface&MockObject */ + private $logger; + + /** @var IJobList|IJobList&MockObject|MockObject */ + private $jobList; + + /** @var int[] */ + private static $argument; + + public function setUp(): void { + parent::setUp(); + $this->time = $this->createMock(ITimeFactory::class); + $this->manager = $this->createMock(IUserManager::class); + $this->accountService = $this->createMock(AccountService::class); + $this->preprocessingService = $this->createMock(PreprocessingService::class); + $this->logger = $this->createMock(LoggerInterface::class); + $this->jobList = $this->createMock(IJobList::class); + $this->job = new PreviewEnhancementProcessingJob( + $this->time, + $this->manager, + $this->accountService, + $this->preprocessingService, + $this->logger, + $this->jobList + ); + + self::$argument = ['accountId' => 1]; + } + + public function testNoAccount(): void { + $this->accountService->expects(self::once()) + ->method('findById') + ->with(self::$argument['accountId']) + ->willThrowException(new DoesNotExistException('Account does not exist')); + + $this->logger->expects(self::once()) + ->method('debug'); + $this->jobList->expects(self::once()) + ->method('remove'); + $this->job->run(self::$argument); + } + + public function testNoUser(): void { + $account = new Account(new MailAccount()); + $this->accountService->expects(self::once()) + ->method('findById') + ->with(self::$argument['accountId']) + ->willReturn($account); + + $this->manager->expects(self::once()) + ->method('get') + ->with($account) + ->willReturn(null); + $this->logger->expects(self::once()) + ->method('debug'); + $this->job->run(self::$argument); + } + + public function testProvisionedNoPassword(): void { + $mailAccount = new MailAccount(); + $mailAccount->setProvisioningId(1); + $account = new Account($mailAccount); + $user = $this->createMock(IUser::class); + $user->setEnabled(); + $this->accountService->expects(self::once()) + ->method('findById') + ->with(self::$argument['accountId']) + ->willReturn($account); + + $this->manager->expects(self::once()) + ->method('get') + ->with($account) + ->willReturn($user); + $this->logger->expects(self::once()) + ->method('info'); + $this->job->run(self::$argument); + } + + public function testProcessingError(): void { + $mailAccount = new MailAccount(); + $account = new Account($mailAccount); + $time = time(); + $user = $this->createMock(IUser::class); + $user->setEnabled(); + $this->accountService->expects(self::once()) + ->method('findById') + ->with(self::$argument['accountId']) + ->willReturn($account); + + $this->manager->expects(self::once()) + ->method('get') + ->with($account) + ->willReturn($user); + $this->time->expects(self::once()) + ->method('getTime') + ->willReturn($time); + $this->preprocessingService->expects(self::once()) + ->method('process') + ->with(($time - (60 * 60 * 24 * 14)), $account) + ->willThrowException(new Exception()); + $this->logger->expects(self::once()) + ->method('error'); + $this->job->run(self::$argument); + } + + public function testProcessing(): void { + $mailAccount = new MailAccount(); + $account = new Account($mailAccount); + $time = time(); + $user = $this->createMock(IUser::class); + $user->setEnabled(); + $this->accountService->expects(self::once()) + ->method('findById') + ->with(self::$argument['accountId']) + ->willReturn($account); + + $this->manager->expects(self::once()) + ->method('get') + ->with($account) + ->willReturn($user); + $this->time->expects(self::once()) + ->method('getTime') + ->willReturn($time); + $this->preprocessingService->expects(self::once()) + ->method('process') + ->with(($time - (60 * 60 * 24 * 14)), $account); + $this->logger->expects(self::never()) + ->method('error'); + $this->job->run(self::$argument); + } +} diff --git a/tests/Unit/Service/IMipServiceTest.php b/tests/Unit/Service/IMipServiceTest.php index 92efc5452f..4ba5a18474 100644 --- a/tests/Unit/Service/IMipServiceTest.php +++ b/tests/Unit/Service/IMipServiceTest.php @@ -159,6 +159,39 @@ public function testIsSpecialUse(): void { $this->service->process(); } + public function testIsArchive(): void { + $message = new Message(); + $message->setImipMessage(true); + $message->setUid(1); + $message->setMailboxId(100); + $mailbox = new Mailbox(); + $mailbox->setId(100); + $mailbox->setAccountId(200); + $mailbox->setSpecialUse('["archive"]'); + $mailAccount = new MailAccount(); + $account = new Account($mailAccount); + + $this->messageMapper->expects(self::once()) + ->method('findIMipMessagesAscending') + ->willReturn([$message]); + $this->mailboxMapper->expects(self::once()) + ->method('findById') + ->with($message->getMailboxId()) + ->willReturn($mailbox); + $this->accountService->expects(self::once()) + ->method('findById') + ->with($mailbox->getAccountId()) + ->willReturn($account); + $this->messageMapper->expects(self::once()) + ->method('updateImipData'); + $this->calendarManager->expects(self::never()) + ->method('handleIMipReply'); + $this->calendarManager->expects(self::never()) + ->method('handleIMipCancel'); + + $this->service->process(); + } + public function testNoSchedulingInfo(): void { $message = new Message(); $message->setImipMessage(true); diff --git a/tests/Unit/Service/PreprocessingServiceTest.php b/tests/Unit/Service/PreprocessingServiceTest.php index 0fed984f4d..010867328f 100644 --- a/tests/Unit/Service/PreprocessingServiceTest.php +++ b/tests/Unit/Service/PreprocessingServiceTest.php @@ -50,21 +50,13 @@ use ChristophWurst\Nextcloud\Testing\TestCase; use OCA\Mail\Account; -use OCA\Mail\Address; -use OCA\Mail\AddressList; use OCA\Mail\Db\MailAccount; use OCA\Mail\Db\MailboxMapper; use OCA\Mail\Db\Message; use OCA\Mail\Db\MessageMapper; use OCA\Mail\Db\Mailbox; -use OCA\Mail\Exception\ServiceException; use OCA\Mail\IMAP\PreviewEnhancer; -use OCA\Mail\Model\IMAPMessage; -use OCA\Mail\Service\AccountService; -use OCA\Mail\Service\IMipService; -use OCA\Mail\Service\MailManager; use OCA\Mail\Service\PreprocessingService; -use OCP\Calendar\IManager; use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface;