diff options
author | Christoph Wurst <christoph@winzerhof-wurst.at> | 2020-10-15 12:24:38 +0300 |
---|---|---|
committer | Christoph Wurst <christoph@winzerhof-wurst.at> | 2020-10-30 19:16:24 +0300 |
commit | 15867528b25cad3e1262014d0fa4f967a73d64be (patch) | |
tree | 6dd7c46876325acf8aad147b9ef791a73376c102 /tests | |
parent | fe074001833362fa1eb4d18576131b0678907fb5 (diff) |
Store special mailboxes as preference of the account
… instead of using a fragile autodetection every time we need one of
those. We will still try to auto-detect the mailboxes but the users will
have to option to change the destinations.
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Integration/Db/MailAccountTest.php | 6 | ||||
-rw-r--r-- | tests/Integration/Db/MailboxMapperTest.php | 34 | ||||
-rw-r--r-- | tests/Integration/Service/MailTransmissionIntegrationTest.php | 5 | ||||
-rw-r--r-- | tests/Unit/IMAP/MailboxSyncTest.php | 14 | ||||
-rw-r--r-- | tests/Unit/Listener/DeleteDraftListenerTest.php | 87 | ||||
-rw-r--r-- | tests/Unit/Listener/DraftMailboxCreatorListenerTest.php | 156 | ||||
-rw-r--r-- | tests/Unit/Listener/SaveSentMessageListenerTest.php | 109 | ||||
-rw-r--r-- | tests/Unit/Listener/TrashMailboxCreatorListenerTest.php | 168 | ||||
-rw-r--r-- | tests/Unit/Service/MailManagerTest.php | 24 | ||||
-rw-r--r-- | tests/Unit/Service/MailTransmissionTest.php | 9 |
10 files changed, 123 insertions, 489 deletions
diff --git a/tests/Integration/Db/MailAccountTest.php b/tests/Integration/Db/MailAccountTest.php index c05f4c342..8bf2af949 100644 --- a/tests/Integration/Db/MailAccountTest.php +++ b/tests/Integration/Db/MailAccountTest.php @@ -66,6 +66,9 @@ class MailAccountTest extends TestCase { 'order' => 13, 'showSubscribedOnly' => null, 'personalNamespace' => null, + 'draftsMailboxId' => null, + 'sentMailboxId' => null, + 'trashMailboxId' => null, ], $a->toJson()); } @@ -89,6 +92,9 @@ class MailAccountTest extends TestCase { 'order' => null, 'showSubscribedOnly' => null, 'personalNamespace' => null, + 'draftsMailboxId' => null, + 'sentMailboxId' => null, + 'trashMailboxId' => null, ]; $a = new MailAccount($expected); // TODO: fix inconsistency diff --git a/tests/Integration/Db/MailboxMapperTest.php b/tests/Integration/Db/MailboxMapperTest.php index d086d993a..9c67c0664 100644 --- a/tests/Integration/Db/MailboxMapperTest.php +++ b/tests/Integration/Db/MailboxMapperTest.php @@ -129,38 +129,4 @@ class MailboxMapperTest extends TestCase { $this->assertSame('INBOX', $result->getName()); } - - public function testNoTrashFound() { - /** @var Account|MockObject $account */ - $account = $this->createMock(Account::class); - $account->method('getId')->willReturn(13); - $this->expectException(DoesNotExistException::class); - - $this->mapper->findSpecial($account, 'trash'); - } - - public function testFindTrash() { - /** @var Account|MockObject $account */ - $account = $this->createMock(Account::class); - $account->method('getId')->willReturn(13); - $qb = $this->db->getQueryBuilder(); - $insert = $qb->insert($this->mapper->getTableName()) - ->values([ - 'name' => $qb->createNamedParameter('Trash'), - 'account_id' => $qb->createNamedParameter(13, IQueryBuilder::PARAM_INT), - 'sync_new_token' => $qb->createNamedParameter('VTEsVjE0Mjg1OTkxNDk='), - 'sync_changed_token' => $qb->createNamedParameter('VTEsVjE0Mjg1OTkxNDk='), - 'sync_vanished_token' => $qb->createNamedParameter('VTEsVjE0Mjg1OTkxNDk='), - 'delimiter' => $qb->createNamedParameter('.'), - 'messages' => $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT), - 'unseen' => $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT), - 'selectable' => $qb->createNamedParameter(true, IQueryBuilder::PARAM_BOOL), - 'special_use' => $qb->createNamedParameter(json_encode(['trash'])), - ]); - $insert->execute(); - - $result = $this->mapper->findSpecial($account, 'trash'); - - $this->assertSame('Trash', $result->getName()); - } } diff --git a/tests/Integration/Service/MailTransmissionIntegrationTest.php b/tests/Integration/Service/MailTransmissionIntegrationTest.php index 097b20313..8540465dc 100644 --- a/tests/Integration/Service/MailTransmissionIntegrationTest.php +++ b/tests/Integration/Service/MailTransmissionIntegrationTest.php @@ -95,6 +95,11 @@ class MailTransmissionIntegrationTest extends TestCase { $this->attachmentService = OC::$server->query(IAttachmentService::class); $userFolder = OC::$server->getUserFolder($this->user->getUID()); + // Make sure the mailbox preferences are set + /** @var MailboxSync $mbSync */ + $mbSync = OC::$server->query(MailboxSync::class); + $mbSync->sync($this->account, new NullLogger(), true); + $this->transmission = new MailTransmission( $userFolder, $this->attachmentService, diff --git a/tests/Unit/IMAP/MailboxSyncTest.php b/tests/Unit/IMAP/MailboxSyncTest.php index 645c9b628..159edcee1 100644 --- a/tests/Unit/IMAP/MailboxSyncTest.php +++ b/tests/Unit/IMAP/MailboxSyncTest.php @@ -33,11 +33,13 @@ use OCA\Mail\Account; use OCA\Mail\Db\MailAccount; use OCA\Mail\Db\MailAccountMapper; use OCA\Mail\Db\MailboxMapper; +use OCA\Mail\Events\MailboxesSynchronizedEvent; use OCA\Mail\Folder; use OCA\Mail\IMAP\FolderMapper; use OCA\Mail\IMAP\IMAPClientFactory; use OCA\Mail\IMAP\MailboxSync; use OCP\AppFramework\Utility\ITimeFactory; +use OCP\EventDispatcher\IEventDispatcher; use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\NullLogger; @@ -61,6 +63,9 @@ class MailboxSyncTest extends TestCase { /** @var MailboxSync */ private $sync; + /** @var IEventDispatcher|MockObject */ + private $dispatcher; + protected function setUp(): void { parent::setUp(); @@ -69,13 +74,15 @@ class MailboxSyncTest extends TestCase { $this->mailAccountMapper = $this->createMock(MailAccountMapper::class); $this->imapClientFactory = $this->createMock(IMAPClientFactory::class); $this->timeFactory = $this->createMock(ITimeFactory::class); + $this->dispatcher = $this->createMock(IEventDispatcher::class); $this->sync = new MailboxSync( $this->mailboxMapper, $this->folderMapper, $this->mailAccountMapper, $this->imapClientFactory, - $this->timeFactory + $this->timeFactory, + $this->dispatcher ); } @@ -87,6 +94,7 @@ class MailboxSyncTest extends TestCase { $this->timeFactory->method('getTime')->willReturn(100000); $this->imapClientFactory->expects($this->never()) ->method('getClient'); + $this->dispatcher->expects($this->never())->method('dispatchTyped'); $this->sync->sync($account, new NullLogger()); } @@ -116,6 +124,10 @@ class MailboxSyncTest extends TestCase { $this->folderMapper->expects($this->once()) ->method('detectFolderSpecialUse') ->with($folders); + $this->dispatcher + ->expects($this->once()) + ->method('dispatchTyped') + ->with($this->equalTo(new MailboxesSynchronizedEvent($account))); $this->sync->sync($account, new NullLogger()); } diff --git a/tests/Unit/Listener/DeleteDraftListenerTest.php b/tests/Unit/Listener/DeleteDraftListenerTest.php index f262120a2..774c42588 100644 --- a/tests/Unit/Listener/DeleteDraftListenerTest.php +++ b/tests/Unit/Listener/DeleteDraftListenerTest.php @@ -27,13 +27,13 @@ namespace OCA\Mail\Tests\Unit\Listener; use ChristophWurst\Nextcloud\Testing\TestCase; use OCA\Mail\Account; +use OCA\Mail\Db\MailAccount; use OCA\Mail\Db\Mailbox; use OCA\Mail\Db\MailboxMapper; use OCA\Mail\Db\Message; use OCA\Mail\Events\DraftSavedEvent; use OCA\Mail\Events\MessageSentEvent; use OCA\Mail\IMAP\IMAPClientFactory; -use OCA\Mail\IMAP\MailboxSync; use OCA\Mail\IMAP\MessageMapper; use OCA\Mail\Listener\DeleteDraftListener; use OCA\Mail\Model\IMessage; @@ -56,9 +56,6 @@ class DeleteDraftListenerTest extends TestCase { /** @var MessageMapper|MockObject */ private $messageMapper; - /** @var MailboxSync|MockObject */ - private $mailboxSync; - /** @var LoggerInterface|MockObject */ private $logger; @@ -71,14 +68,12 @@ class DeleteDraftListenerTest extends TestCase { $this->imapClientFactory = $this->createMock(IMAPClientFactory::class); $this->mailboxMapper = $this->createMock(MailboxMapper::class); $this->messageMapper = $this->createMock(MessageMapper::class); - $this->mailboxSync = $this->createMock(MailboxSync::class); $this->logger = $this->createMock(LoggerInterface::class); $this->listener = new DeleteDraftListener( $this->imapClientFactory, $this->mailboxMapper, $this->messageMapper, - $this->mailboxSync, $this->logger ); } @@ -109,9 +104,11 @@ class DeleteDraftListenerTest extends TestCase { $this->listener->handle($event); } - public function testHandleDraftSavedEventCreatesDraftsMailbox(): void { + public function testHandleDraftSavedEventNoDraftMailboxSet(): void { /** @var Account|MockObject $account */ $account = $this->createMock(Account::class); + $mailAccount = new MailAccount(); + $account->method('getMailAccount')->willReturn($mailAccount); /** @var NewMessageData|MockObject $newMessageData */ $newMessageData = $this->createMock(NewMessageData::class); $draft = new Message(); @@ -130,39 +127,42 @@ class DeleteDraftListenerTest extends TestCase { ->willReturn($client); $mailbox = new Mailbox(); $mailbox->setName('Drafts'); - $this->mailboxMapper->expects($this->exactly(2)) - ->method('findSpecial') - ->with($account, 'drafts') - ->willReturnOnConsecutiveCalls( - $this->throwException(new DoesNotExistException('')), - $mailbox - ); - $client->expects($this->once()) - ->method('createMailbox') - ->with( - 'Drafts', - [ - 'special_use' => [ - \Horde_Imap_Client::SPECIALUSE_DRAFTS, - ], - ] - ); - $this->mailboxSync->expects($this->once()) - ->method('sync') - ->with($account, $this->logger, true); - $this->messageMapper->expects($this->once()) - ->method('addFlag') - ->with( - $client, - $mailbox, - $uid, - \Horde_Imap_Client::FLAG_DELETED - ); - $client->expects($this->once()) - ->method('expunge') - ->with('Drafts'); - $this->logger->expects($this->never()) - ->method('error'); + $this->mailboxMapper->expects($this->never()) + ->method('findById'); + $this->logger->expects($this->once())->method('warning'); + + $this->listener->handle($event); + } + + public function testHandleDraftSavedEventDraftMailboxNotFound(): void { + /** @var Account|MockObject $account */ + $account = $this->createMock(Account::class); + $mailAccount = new MailAccount(); + $mailAccount->setDraftsMailboxId(123); + $account->method('getMailAccount')->willReturn($mailAccount); + /** @var NewMessageData|MockObject $newMessageData */ + $newMessageData = $this->createMock(NewMessageData::class); + $draft = new Message(); + $uid = 123; + $draft->setUid($uid); + $event = new DraftSavedEvent( + $account, + $newMessageData, + $draft + ); + /** @var \Horde_Imap_Client_Socket|MockObject $client */ + $client = $this->createMock(\Horde_Imap_Client_Socket::class); + $this->imapClientFactory + ->method('getClient') + ->with($account) + ->willReturn($client); + $mailbox = new Mailbox(); + $mailbox->setName('Drafts'); + $this->mailboxMapper->expects($this->once()) + ->method('findById') + ->with(123) + ->willThrowException(new DoesNotExistException("")); + $this->logger->expects($this->once())->method('warning'); $this->listener->handle($event); } @@ -188,6 +188,9 @@ class DeleteDraftListenerTest extends TestCase { public function testHandleMessageSentEvent(): void { /** @var Account|MockObject $account */ $account = $this->createMock(Account::class); + $mailAccount = new MailAccount(); + $mailAccount->setDraftsMailboxId(123); + $account->method('getMailAccount')->willReturn($mailAccount); /** @var NewMessageData|MockObject $newMessageData */ $newMessageData = $this->createMock(NewMessageData::class); /** @var RepliedMessageData|MockObject $repliedMessageData */ @@ -216,8 +219,8 @@ class DeleteDraftListenerTest extends TestCase { $mailbox = new Mailbox(); $mailbox->setName('Drafts'); $this->mailboxMapper->expects($this->once()) - ->method('findSpecial') - ->with($account, 'drafts') + ->method('findById') + ->with(123) ->willReturn($mailbox); $this->messageMapper->expects($this->once()) ->method('addFlag') diff --git a/tests/Unit/Listener/DraftMailboxCreatorListenerTest.php b/tests/Unit/Listener/DraftMailboxCreatorListenerTest.php deleted file mode 100644 index a7c1bb1dd..000000000 --- a/tests/Unit/Listener/DraftMailboxCreatorListenerTest.php +++ /dev/null @@ -1,156 +0,0 @@ -<?php - -declare(strict_types=1); - -/** - * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @author 2019 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 OCA\Mail\Tests\Unit\Listener; - -use ChristophWurst\Nextcloud\Testing\TestCase; -use OCA\Mail\Account; -use OCA\Mail\Db\Mailbox; -use OCA\Mail\Db\MailboxMapper; -use OCA\Mail\Db\Message; -use OCA\Mail\Events\SaveDraftEvent; -use OCA\Mail\IMAP\IMAPClientFactory; -use OCA\Mail\IMAP\MailboxSync; -use OCA\Mail\Listener\DraftMailboxCreatorListener; -use OCA\Mail\Model\NewMessageData; -use OCP\AppFramework\Db\DoesNotExistException; -use OCP\EventDispatcher\Event; -use PHPUnit\Framework\MockObject\MockObject; -use Psr\Log\LoggerInterface; - -class DraftMailboxCreatorListenerTest extends TestCase { - - /** @var MailboxMapper|MockObject */ - private $mailboxMapper; - - /** @var IMAPClientFactory|MockObject */ - private $imapClientFactory; - - /** @var MailboxSync|MockObject */ - private $mailboxSync; - - /** @var LoggerInterface|MockObject */ - private $logger; - - /** @var DraftMailboxCreatorListener */ - private $listener; - - protected function setUp(): void { - parent::setUp(); - - $this->mailboxMapper = $this->createMock(MailboxMapper::class); - $this->imapClientFactory = $this->createMock(IMAPClientFactory::class); - $this->mailboxSync = $this->createMock(MailboxSync::class); - $this->logger = $this->createMock(LoggerInterface::class); - - $this->listener = new DraftMailboxCreatorListener( - $this->mailboxMapper, - $this->imapClientFactory, - $this->mailboxSync, - $this->logger - ); - } - - public function testHandleUnrelated() { - $event = new Event(); - - $this->listener->handle($event); - - $this->addToAssertionCount(1); - } - - public function testHandleSaveDraftEventMailboxExists(): void { - /** @var Account|MockObject $account */ - $account = $this->createMock(Account::class); - /** @var NewMessageData|MockObject $newMessageData */ - $newMessageData = $this->createMock(NewMessageData::class); - $draft = new Message(); - $draft->setUid(123); - $event = new SaveDraftEvent( - $account, - $newMessageData, - $draft - ); - /** @var \Horde_Imap_Client_Socket|MockObject $client */ - $client = $this->createMock(\Horde_Imap_Client_Socket::class); - $this->imapClientFactory - ->method('getClient') - ->with($account) - ->willReturn($client); - $mailbox = new Mailbox(); - $mailbox->setName('Drafts'); - $this->mailboxMapper->expects($this->once()) - ->method('findSpecial') - ->with($account, 'drafts') - ->willReturn($mailbox); - $this->logger->expects($this->never()) - ->method('error'); - - $this->listener->handle($event); - } - - public function testHandleSaveDraftEventCreatesDraftsMailbox(): void { - /** @var Account|MockObject $account */ - $account = $this->createMock(Account::class); - /** @var NewMessageData|MockObject $newMessageData */ - $newMessageData = $this->createMock(NewMessageData::class); - $draft = new Message(); - $draft->setUid(123); - $event = new SaveDraftEvent( - $account, - $newMessageData, - $draft - ); - /** @var \Horde_Imap_Client_Socket|MockObject $client */ - $client = $this->createMock(\Horde_Imap_Client_Socket::class); - $this->imapClientFactory - ->method('getClient') - ->with($account) - ->willReturn($client); - $mailbox = new Mailbox(); - $mailbox->setName('Drafts'); - $this->mailboxMapper->expects($this->once()) - ->method('findSpecial') - ->with($account, 'drafts') - ->willThrowException(new DoesNotExistException('')); - $client->expects($this->once()) - ->method('createMailbox') - ->with( - 'Drafts', - [ - 'special_use' => [ - \Horde_Imap_Client::SPECIALUSE_DRAFTS, - ], - ] - ); - $this->mailboxSync->expects($this->once()) - ->method('sync') - ->with($account, $this->logger, true); - $this->logger->expects($this->never()) - ->method('error'); - - $this->listener->handle($event); - } -} diff --git a/tests/Unit/Listener/SaveSentMessageListenerTest.php b/tests/Unit/Listener/SaveSentMessageListenerTest.php index 35b627d43..e8c46e213 100644 --- a/tests/Unit/Listener/SaveSentMessageListenerTest.php +++ b/tests/Unit/Listener/SaveSentMessageListenerTest.php @@ -27,13 +27,13 @@ namespace OCA\Mail\Tests\Unit\Listener; use ChristophWurst\Nextcloud\Testing\TestCase; use OCA\Mail\Account; +use OCA\Mail\Db\MailAccount; use OCA\Mail\Db\Mailbox; use OCA\Mail\Db\MailboxMapper; use OCA\Mail\Db\Message; use OCA\Mail\Events\MessageSentEvent; use OCA\Mail\Exception\ServiceException; use OCA\Mail\IMAP\IMAPClientFactory; -use OCA\Mail\IMAP\MailboxSync; use OCA\Mail\IMAP\MessageMapper; use OCA\Mail\Listener\SaveSentMessageListener; use OCA\Mail\Model\IMessage; @@ -56,9 +56,6 @@ class SaveSentMessageListenerTest extends TestCase { /** @var MessageMapper|MockObject */ private $messageMapper; - /** @var MailboxSync|MockObject */ - private $mailboxSync; - /** @var LoggerInterface|MockObject */ private $logger; @@ -71,14 +68,12 @@ class SaveSentMessageListenerTest extends TestCase { $this->mailboxMapper = $this->createMock(MailboxMapper::class); $this->imapClientFactory = $this->createMock(IMAPClientFactory::class); $this->messageMapper = $this->createMock(MessageMapper::class); - $this->mailboxSync = $this->createMock(MailboxSync::class); $this->logger = $this->createMock(LoggerInterface::class); $this->listener = new SaveSentMessageListener( $this->mailboxMapper, $this->imapClientFactory, $this->messageMapper, - $this->mailboxSync, $this->logger ); } @@ -91,9 +86,11 @@ class SaveSentMessageListenerTest extends TestCase { $this->addToAssertionCount(1); } - public function testHandleMessageSentMailboxDoesNotExistCantCreate(): void { + public function testHandleMessageSentMailboxNotSet(): void { /** @var Account|MockObject $account */ $account = $this->createMock(Account::class); + $mailAccount = new MailAccount(); + $account->method('getMailAccount')->willReturn($mailAccount); /** @var NewMessageData|MockObject $newMessageData */ $newMessageData = $this->createMock(NewMessageData::class); /** @var RepliedMessageData|MockObject $repliedMessageData */ @@ -112,42 +109,10 @@ class SaveSentMessageListenerTest extends TestCase { $message, $mail ); - $mailbox = new Mailbox(); - $this->mailboxMapper->expects($this->exactly(2)) - ->method('findSpecial') - ->withConsecutive( - [$account, 'sent'], - [$account, 'sent'] - ) - ->willReturnOnConsecutiveCalls( - $this->throwException(new DoesNotExistException('')), - $mailbox - ); - $client = $this->createMock(\Horde_Imap_Client_Socket::class); - $this->imapClientFactory - ->method('getClient') - ->with($account) - ->willReturn($client); - $exception = new \Horde_Imap_Client_Exception(); - $client->expects($this->once()) - ->method('createMailbox') - ->with( - 'Sent', - [ - 'special_use' => [ - \Horde_Imap_Client::SPECIALUSE_SENT, - ], - ] - ) - ->willThrowException($exception); + $this->mailboxMapper->expects($this->never()) + ->method('findById'); $this->logger->expects($this->once()) - ->method('warning') - ->with( - 'Could not create sent mailbox: ', - $this->equalTo([ - 'exception' => $exception, - ]) - ); + ->method('warning'); $this->listener->handle($event); } @@ -155,6 +120,9 @@ class SaveSentMessageListenerTest extends TestCase { public function testHandleMessageSentMailboxDoesNotExist(): void { /** @var Account|MockObject $account */ $account = $this->createMock(Account::class); + $mailAccount = new MailAccount(); + $mailAccount->setSentMailboxId(123); + $account->method('getMailAccount')->willReturn($mailAccount); /** @var NewMessageData|MockObject $newMessageData */ $newMessageData = $this->createMock(NewMessageData::class); /** @var RepliedMessageData|MockObject $repliedMessageData */ @@ -173,39 +141,14 @@ class SaveSentMessageListenerTest extends TestCase { $message, $mail ); - $mailbox = new Mailbox(); - $this->mailboxMapper->expects($this->exactly(2)) - ->method('findSpecial') - ->withConsecutive( - [$account, 'sent'], - [$account, 'sent'] - ) - ->willReturnOnConsecutiveCalls( - $this->throwException(new DoesNotExistException('')), - $mailbox - ); - $client = $this->createMock(\Horde_Imap_Client_Socket::class); - $this->imapClientFactory - ->method('getClient') - ->with($account) - ->willReturn($client); - $client->expects($this->once()) - ->method('createMailbox') - ->with( - 'Sent', - [ - 'special_use' => [ - \Horde_Imap_Client::SPECIALUSE_SENT, - ], - ] - ); - $this->messageMapper->expects($this->once()) - ->method('save') - ->with( - $this->anything(), - $mailbox, - $mail - ); + $this->mailboxMapper->expects($this->once()) + ->method('findById') + ->with(123) + ->willThrowException(new DoesNotExistException('')); + $this->messageMapper->expects($this->never()) + ->method('save'); + $this->logger->expects($this->once()) + ->method('error'); $this->listener->handle($event); } @@ -213,6 +156,9 @@ class SaveSentMessageListenerTest extends TestCase { public function testHandleMessageSentSavingError(): void { /** @var Account|MockObject $account */ $account = $this->createMock(Account::class); + $mailAccount = new MailAccount(); + $mailAccount->setSentMailboxId(123); + $account->method('getMailAccount')->willReturn($mailAccount); /** @var NewMessageData|MockObject $newMessageData */ $newMessageData = $this->createMock(NewMessageData::class); /** @var RepliedMessageData|MockObject $repliedMessageData */ @@ -233,8 +179,8 @@ class SaveSentMessageListenerTest extends TestCase { ); $mailbox = new Mailbox(); $this->mailboxMapper->expects($this->once()) - ->method('findSpecial') - ->with($account, 'sent') + ->method('findById') + ->with(123) ->willReturn($mailbox); $this->messageMapper->expects($this->once()) ->method('save') @@ -252,6 +198,9 @@ class SaveSentMessageListenerTest extends TestCase { public function testHandleMessageSent(): void { /** @var Account|MockObject $account */ $account = $this->createMock(Account::class); + $mailAccount = new MailAccount(); + $mailAccount->setSentMailboxId(123); + $account->method('getMailAccount')->willReturn($mailAccount); /** @var NewMessageData|MockObject $newMessageData */ $newMessageData = $this->createMock(NewMessageData::class); /** @var RepliedMessageData|MockObject $repliedMessageData */ @@ -272,8 +221,8 @@ class SaveSentMessageListenerTest extends TestCase { ); $mailbox = new Mailbox(); $this->mailboxMapper->expects($this->once()) - ->method('findSpecial') - ->with($account, 'sent') + ->method('findById') + ->with(123) ->willReturn($mailbox); $this->messageMapper->expects($this->once()) ->method('save') @@ -282,6 +231,8 @@ class SaveSentMessageListenerTest extends TestCase { $mailbox, $mail ); + $this->logger->expects($this->never())->method('warning'); + $this->logger->expects($this->never())->method('error'); $this->listener->handle($event); } diff --git a/tests/Unit/Listener/TrashMailboxCreatorListenerTest.php b/tests/Unit/Listener/TrashMailboxCreatorListenerTest.php deleted file mode 100644 index b54d93fb4..000000000 --- a/tests/Unit/Listener/TrashMailboxCreatorListenerTest.php +++ /dev/null @@ -1,168 +0,0 @@ -<?php - -declare(strict_types=1); - -/** - * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @author 2019 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 OCA\Mail\Tests\Unit\Listener; - -use ChristophWurst\Nextcloud\Testing\TestCase; -use OCA\Mail\Account; -use OCA\Mail\Db\Mailbox; -use OCA\Mail\Db\MailboxMapper; -use OCA\Mail\Events\BeforeMessageDeletedEvent; -use OCA\Mail\IMAP\IMAPClientFactory; -use OCA\Mail\IMAP\MailboxSync; -use OCA\Mail\Listener\TrashMailboxCreatorListener; -use OCP\AppFramework\Db\DoesNotExistException; -use OCP\EventDispatcher\Event; -use PHPUnit\Framework\MockObject\MockObject; -use Psr\Log\LoggerInterface; - -class TrashMailboxCreatorListenerTest extends TestCase { - - /** @var MailboxMapper|MockObject */ - private $mailboxMapper; - - /** @var IMAPClientFactory|MockObject */ - private $imapClientFactory; - - /** @var MailboxSync|MockObject */ - private $mailboxSync; - - /** @var LoggerInterface|MockObject */ - private $logger; - - /** @var TrashMailboxCreatorListener */ - private $listener; - - protected function setUp(): void { - parent::setUp(); - - $this->mailboxMapper = $this->createMock(MailboxMapper::class); - $this->imapClientFactory = $this->createMock(IMAPClientFactory::class); - $this->mailboxSync = $this->createMock(MailboxSync::class); - $this->logger = $this->createMock(LoggerInterface::class); - - $this->listener = new TrashMailboxCreatorListener( - $this->mailboxMapper, - $this->imapClientFactory, - $this->mailboxSync, - $this->logger - ); - } - - public function testHandleUnrelated(): void { - $event = new Event(); - - $this->listener->handle($event); - - $this->addToAssertionCount(1); - } - - public function testHandleDoesAlreadyExist(): void { - /** @var Account|MockObject $account */ - $account = $this->createMock(Account::class); - $event = new BeforeMessageDeletedEvent( - $account, - 'INBOX', - 123 - ); - $mailbox = new Mailbox(); - $this->mailboxMapper->expects($this->once()) - ->method('findSpecial') - ->with($account, 'trash') - ->willReturn($mailbox); - - $this->listener->handle($event); - } - - public function testHandleDoesNotExistCantCreate(): void { - /** @var Account|MockObject $account */ - $account = $this->createMock(Account::class); - $event = new BeforeMessageDeletedEvent( - $account, - 'INBOX', - 123 - ); - $this->mailboxMapper->expects($this->once()) - ->method('findSpecial') - ->with($account, 'trash') - ->willThrowException(new DoesNotExistException('')); - $client = $this->createMock(\Horde_Imap_Client_Socket::class); - $this->imapClientFactory->expects($this->once()) - ->method('getClient') - ->with($account) - ->willReturn($client); - $client->expects($this->once()) - ->method('createMailbox') - ->with( - 'Trash', - [ - 'special_use' => [ - \Horde_Imap_Client::SPECIALUSE_TRASH, - ], - ] - ) - ->willThrowException(new \Horde_Imap_Client_Exception()); - $this->logger->expects($this->once()) - ->method('error'); - - $this->listener->handle($event); - } - - public function testHandleDoesNotExist(): void { - /** @var Account|MockObject $account */ - $account = $this->createMock(Account::class); - $event = new BeforeMessageDeletedEvent( - $account, - 'INBOX', - 123 - ); - $this->mailboxMapper->expects($this->once()) - ->method('findSpecial') - ->with($account, 'trash') - ->willThrowException(new DoesNotExistException('')); - $client = $this->createMock(\Horde_Imap_Client_Socket::class); - $this->imapClientFactory->expects($this->once()) - ->method('getClient') - ->with($account) - ->willReturn($client); - $client->expects($this->once()) - ->method('createMailbox') - ->with( - 'Trash', - [ - 'special_use' => [ - \Horde_Imap_Client::SPECIALUSE_TRASH, - ], - ] - ); - $this->mailboxSync->expects($this->once()) - ->method('sync') - ->with($account, $this->logger, true); - $this->logger->expects($this->never()) - ->method('error'); - - $this->listener->handle($event); - } -} diff --git a/tests/Unit/Service/MailManagerTest.php b/tests/Unit/Service/MailManagerTest.php index d5cece922..e553abb31 100644 --- a/tests/Unit/Service/MailManagerTest.php +++ b/tests/Unit/Service/MailManagerTest.php @@ -26,6 +26,7 @@ namespace OCA\Mail\Tests\Unit\Service; use ChristophWurst\Nextcloud\Testing\TestCase; use Horde_Imap_Client_Socket; use OCA\Mail\Account; +use OCA\Mail\Db\MailAccount; use OCA\Mail\Db\Mailbox; use OCA\Mail\Db\MailboxMapper; use OCA\Mail\Db\MessageMapper as DbMessageMapper; @@ -188,9 +189,12 @@ class MailManagerTest extends TestCase { ); } - public function testDeleteMessageTrashFolderNotFound(): void { + public function testDeleteMessageTrashMailboxNotFound(): void { /** @var Account|MockObject $account */ $account = $this->createMock(Account::class); + $mailAccount = new MailAccount(); + $mailAccount->setTrashMailboxId(123); + $account->method('getMailAccount')->willReturn($mailAccount); $this->eventDispatcher->expects($this->once()) ->method('dispatch') ->with( @@ -202,8 +206,8 @@ class MailManagerTest extends TestCase { ->with($account, 'INBOX') ->willReturn($this->createMock(Mailbox::class)); $this->mailboxMapper->expects($this->once()) - ->method('findSpecial') - ->with($account, 'trash') + ->method('findById') + ->with(123) ->willThrowException(new DoesNotExistException("")); $this->expectException(ServiceException::class); @@ -217,6 +221,9 @@ class MailManagerTest extends TestCase { public function testDeleteMessage(): void { /** @var Account|MockObject $account */ $account = $this->createMock(Account::class); + $mailAccount = new MailAccount(); + $mailAccount->setTrashMailboxId(123); + $account->method('getMailAccount')->willReturn($mailAccount); $inbox = new Mailbox(); $inbox->setName('INBOX'); $trash = new Mailbox(); @@ -228,8 +235,8 @@ class MailManagerTest extends TestCase { ->with($account, 'INBOX') ->willReturn($inbox); $this->mailboxMapper->expects($this->once()) - ->method('findSpecial') - ->with($account, 'trash') + ->method('findById') + ->with(123) ->willReturn($trash); $client = $this->createMock(Horde_Imap_Client_Socket::class); $this->imapClientFactory->expects($this->once()) @@ -254,6 +261,9 @@ class MailManagerTest extends TestCase { public function testExpungeMessage(): void { /** @var Account|MockObject $account */ $account = $this->createMock(Account::class); + $mailAccount = new MailAccount(); + $mailAccount->setTrashMailboxId(123); + $account->method('getMailAccount')->willReturn($mailAccount); $source = new Mailbox(); $source->setName('Trash'); $trash = new Mailbox(); @@ -265,8 +275,8 @@ class MailManagerTest extends TestCase { ->with($account, 'Trash') ->willReturn($source); $this->mailboxMapper->expects($this->once()) - ->method('findSpecial') - ->with($account, 'trash') + ->method('findById') + ->with(123) ->willReturn($trash); $client = $this->createMock(Horde_Imap_Client_Socket::class); $this->imapClientFactory->expects($this->once()) diff --git a/tests/Unit/Service/MailTransmissionTest.php b/tests/Unit/Service/MailTransmissionTest.php index 4aeaf2cda..63cf1587d 100644 --- a/tests/Unit/Service/MailTransmissionTest.php +++ b/tests/Unit/Service/MailTransmissionTest.php @@ -100,6 +100,7 @@ class MailTransmissionTest extends TestCase { public function testSendNewMessage() { $mailAccount = new MailAccount(); $mailAccount->setUserId('testuser'); + $mailAccount->setSentMailboxId(123); /** @var Account|MockObject $account */ $account = $this->createMock(Account::class); $account->method('getMailAccount')->willReturn($mailAccount); @@ -122,6 +123,7 @@ class MailTransmissionTest extends TestCase { public function testSendMessageFromAlias() { $mailAccount = new MailAccount(); $mailAccount->setUserId('testuser'); + $mailAccount->setSentMailboxId(123); /** @var Account|MockObject $account */ $account = $this->createMock(Account::class); $account->method('getMailAccount')->willReturn($mailAccount); @@ -152,6 +154,7 @@ class MailTransmissionTest extends TestCase { public function testSendNewMessageWithCloudAttachments() { $mailAccount = new MailAccount(); $mailAccount->setUserId('testuser'); + $mailAccount->setSentMailboxId(123); /** @var Account|MockObject $account */ $account = $this->createMock(Account::class); $account->method('getMailAccount')->willReturn($mailAccount); @@ -194,6 +197,7 @@ class MailTransmissionTest extends TestCase { public function testReplyToAnExistingMessage() { $mailAccount = new MailAccount(); $mailAccount->setUserId('testuser'); + $mailAccount->setSentMailboxId(123); /** @var Account|MockObject $account */ $account = $this->createMock(Account::class); $account->method('getMailAccount')->willReturn($mailAccount); @@ -222,6 +226,7 @@ class MailTransmissionTest extends TestCase { public function testSaveDraft() { $mailAccount = new MailAccount(); $mailAccount->setUserId('testuser'); + $mailAccount->setDraftsMailboxId(123); /** @var Account|MockObject $account */ $account = $this->createMock(Account::class); $account->method('getMailAccount')->willReturn($mailAccount); @@ -239,8 +244,8 @@ class MailTransmissionTest extends TestCase { ->willReturn($client); $draftsMailbox = new \OCA\Mail\Db\Mailbox(); $this->mailboxMapper->expects($this->once()) - ->method('findSpecial') - ->with($account, 'drafts') + ->method('findById') + ->with(123) ->willReturn($draftsMailbox); $this->messageMapper->expects($this->once()) ->method('save') |