diff options
Diffstat (limited to 'tests/Integration/Migration/MigrateImportantFromImapAndDbTest.php')
-rw-r--r-- | tests/Integration/Migration/MigrateImportantFromImapAndDbTest.php | 239 |
1 files changed, 239 insertions, 0 deletions
diff --git a/tests/Integration/Migration/MigrateImportantFromImapAndDbTest.php b/tests/Integration/Migration/MigrateImportantFromImapAndDbTest.php new file mode 100644 index 000000000..548d63cef --- /dev/null +++ b/tests/Integration/Migration/MigrateImportantFromImapAndDbTest.php @@ -0,0 +1,239 @@ +<?php + +/** + * @copyright 2021 Anna Larch <anna.larch@nextcloud.com> + * + * @author 2021 Anna Larch <anna.larch@nextcloud.com> + * + * @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\Integration\Service; + +use ChristophWurst\Nextcloud\Testing\TestCase; +use Horde_Imap_Client_Exception; +use Horde_Imap_Client_Socket; +use OCA\Mail\Account; +use OCA\Mail\Db\Mailbox; +use OCA\Mail\Db\MailboxMapper; +use OCA\Mail\IMAP\MessageMapper; +use OCA\Mail\Db\Tag; +use OCA\Mail\Exception\ServiceException; +use OCA\Mail\IMAP\IMAPClientFactory; +use OCA\Mail\Migration\MigrateImportantFromImapAndDb; +use Psr\Log\LoggerInterface; + +class MigrateImportantFromImapAndDbTest extends TestCase { + + /** @var MockObject */ + private $clientFactory; + + /** @var MockObject */ + private $client; + + /** @var MockObject */ + private $messageMapper; + + /** @var MockObject */ + private $mailboxMapper; + + /** @var MockObject */ + private $logger; + + /** @var MigrateImportantFromImapAndDb */ + private $migration; + + protected function setUp(): void { + $this->clientFactory = $this->createMock(IMAPClientFactory::class); + $this->client = $this->createMock(Horde_Imap_Client_Socket::class); + $this->messageMapper = $this->createMock(MessageMapper::class); + $this->mailboxMapper = $this->createMock(MailboxMapper::class); + $this->logger = $this->createMock(LoggerInterface::class); + $this->migration = new MigrateImportantFromImapAndDb( + $this->clientFactory, + $this->messageMapper, + $this->mailboxMapper, + $this->logger + ); + parent::setUp(); + } + + public function testMigrateImportantOnImap() { + $account = $this->createMock(Account::class); + $mailbox = $this->createMock(Mailbox::class); + $uids = [1,2,3]; + + $this->clientFactory->expects($this->once()) + ->method('getClient') + ->with($account) + ->willReturn($this->client); + $this->messageMapper->expects($this->once()) + ->method('getFlagged') + ->with($this->client, $mailbox, '$important') + ->willReturn($uids); + $this->messageMapper->expects($this->once()) + ->method('addFlag') + ->with($this->client, $mailbox, $uids, Tag::LABEL_IMPORTANT); + $this->logger->expects($this->never()) + ->method('debug'); + + $this->migration->migrateImportantOnImap($account, $mailbox); + } + + public function testMigrateImportantOnImapNoUids() { + $account = $this->createMock(Account::class); + $mailbox = $this->createMock(Mailbox::class); + $uids = []; + + $this->clientFactory->expects($this->once()) + ->method('getClient') + ->with($account) + ->willReturn($this->client); + $this->messageMapper->expects($this->once()) + ->method('getFlagged') + ->with($this->client, $mailbox, '$important') + ->willReturn($uids); + $this->messageMapper->expects($this->never()) + ->method('addFlag'); + $this->logger->expects($this->never()) + ->method('debug'); + + $this->migration->migrateImportantOnImap($account, $mailbox); + } + + public function testMigrateImportantOnImapExceptionGetFlagged() { + $account = $this->createMock(Account::class); + $mailbox = $this->createMock(Mailbox::class); + $e = new Horde_Imap_Client_Exception(''); + + $this->clientFactory->expects($this->once()) + ->method('getClient') + ->with($account) + ->willReturn($this->client); + $this->messageMapper->expects($this->once()) + ->method('getFlagged') + ->with($this->client, $mailbox, '$important') + ->willThrowException($e); + $this->messageMapper->expects($this->never()) + ->method('addFlag'); + $this->logger->expects($this->never()) + ->method('debug'); + $this->expectException(ServiceException::class); + + $this->migration->migrateImportantOnImap($account, $mailbox); + } + + public function testMigrateImportantOnImapExceptionOnFlag() { + $account = $this->createMock(Account::class); + $mailbox = new Mailbox(); + $mailbox->setName('INBOX'); + $e = new Horde_Imap_Client_Exception(''); + $uids = [1,2,3,4]; + + $this->clientFactory->expects($this->once()) + ->method('getClient') + ->with($account) + ->willReturn($this->client); + $this->messageMapper->expects($this->once()) + ->method('getFlagged') + ->with($this->client, $mailbox, '$important') + ->willReturn($uids); + $this->messageMapper->expects($this->once()) + ->method('addFlag') + ->with($this->client, $mailbox, $uids, Tag::LABEL_IMPORTANT) + ->willThrowException($e); + $this->logger->expects($this->once()) + ->method('debug') + ->with('Could not flag messages in mailbox <' . $mailbox->getId() . '>'); + $this->expectException(ServiceException::class); + + $this->migration->migrateImportantOnImap($account, $mailbox); + } + + public function migrateImportantFromDb() { + $account = $this->createMock(Account::class); + $mailbox = new Mailbox(); + $mailbox->setId(1); + $uids = [1,2,3]; + + $this->clientFactory->expects($this->once()) + ->method('getClient') + ->with($account) + ->willReturn($this->client); + $this->mailboxMapper->expects($this->once()) + ->method('findFlaggedImportantUids') + ->with($mailbox->getId()) + ->willReturn($uids); + $this->messageMapper->expects($this->once()) + ->method('addFlag') + ->with($this->client, $mailbox, $uids, Tag::LABEL_IMPORTANT); + $this->logger->expects($this->never()) + ->method('debug'); + + $this->migration->migrateImportantFromDb($account, $mailbox); + } + + public function testMigrateImportantFromDbNoUids() { + $account = $this->createMock(Account::class); + $mailbox = new Mailbox(); + $mailbox->setId(1); + $uids = []; + + $this->clientFactory->expects($this->once()) + ->method('getClient') + ->with($account) + ->willReturn($this->client); + $this->mailboxMapper->expects($this->once()) + ->method('findFlaggedImportantUids') + ->with($mailbox->getId()) + ->willReturn($uids); + $this->messageMapper->expects($this->never()) + ->method('addFlag'); + $this->logger->expects($this->never()) + ->method('debug'); + + $this->migration->migrateImportantFromDb($account, $mailbox); + } + + public function testMigrateImportantFromDbExceptionOnFlag() { + $account = $this->createMock(Account::class); + $mailbox = new Mailbox(); + $mailbox->setId(1); + $mailbox->setName('INBOX'); + $e = new Horde_Imap_Client_Exception(''); + $uids = [1,2,3]; + + $this->clientFactory->expects($this->once()) + ->method('getClient') + ->with($account) + ->willReturn($this->client); + $this->mailboxMapper->expects($this->once()) + ->method('findFlaggedImportantUids') + ->with($mailbox->getId()) + ->willReturn($uids); + $this->messageMapper->expects($this->once()) + ->method('addFlag') + ->with($this->client, $mailbox, $uids, Tag::LABEL_IMPORTANT) + ->willThrowException($e); + $this->logger->expects($this->once()) + ->method('debug') + ->with('Could not flag messages in mailbox <' . $mailbox->getId() . '>'); + $this->expectException(ServiceException::class); + + $this->migration->migrateImportantFromDb($account, $mailbox); + } +} |