diff options
author | Anna Larch <anna@nextcloud.com> | 2021-03-04 19:31:21 +0300 |
---|---|---|
committer | Christoph Wurst <christoph@winzerhof-wurst.at> | 2021-03-22 20:06:02 +0300 |
commit | 83a58623b116cd3f8c79f751a3df5963b3392692 (patch) | |
tree | 8f9660e75ea8748057e752c4f1573e3c618b5b85 /tests | |
parent | f27fb61c0fe15ef6a278a40d7774cde4b7c748b8 (diff) |
Add tagging to messages
Signed-off-by: Anna Larch <anna@nextcloud.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Unit/Controller/MessagesControllerTest.php | 253 | ||||
-rw-r--r-- | tests/Unit/Listener/MessageCacheUpdaterListenerTest.php | 3 | ||||
-rw-r--r-- | tests/Unit/Service/MailManagerTest.php | 109 | ||||
-rw-r--r-- | tests/Unit/Service/SetupServiceTest.php | 13 | ||||
-rw-r--r-- | tests/psalm-baseline.xml | 5 |
5 files changed, 349 insertions, 34 deletions
diff --git a/tests/Unit/Controller/MessagesControllerTest.php b/tests/Unit/Controller/MessagesControllerTest.php index b795948b1..8943aa331 100644 --- a/tests/Unit/Controller/MessagesControllerTest.php +++ b/tests/Unit/Controller/MessagesControllerTest.php @@ -24,40 +24,41 @@ declare(strict_types=1); namespace OCA\Mail\Tests\Unit\Controller; -use ChristophWurst\Nextcloud\Testing\TestCase; -use OC\AppFramework\Http\Request; -use OC\Security\CSP\ContentSecurityPolicyNonceManager; +use OCP\IL10N; +use OCP\IRequest; +use OCA\Mail\Db\Tag; use OCA\Mail\Account; +use OCA\Mail\Mailbox; +use OCP\Files\Folder; +use ReflectionObject; +use OCP\IURLGenerator; use OCA\Mail\Attachment; -use OCA\Mail\Contracts\IMailManager; -use OCA\Mail\Contracts\IMailSearch; -use OCA\Mail\Contracts\IMailTransmission; -use OCA\Mail\Contracts\ITrustedSenderService; -use OCA\Mail\Controller\MessagesController; -use OCA\Mail\Exception\ClientException; -use OCA\Mail\Exception\ServiceException; -use OCA\Mail\Http\AttachmentDownloadResponse; +use OCP\AppFramework\Http; +use OCA\Mail\Model\Message; +use Psr\Log\LoggerInterface; use OCA\Mail\Http\HtmlResponse; -use OCA\Mail\Mailbox; use OCA\Mail\Model\IMAPMessage; -use OCA\Mail\Model\Message; +use OCP\Files\IMimeTypeDetector; +use OC\AppFramework\Http\Request; +use OCA\Mail\Service\MailManager; +use OCA\Mail\Contracts\IMailSearch; +use OCA\Mail\Contracts\IMailManager; use OCA\Mail\Service\AccountService; use OCA\Mail\Service\ItineraryService; -use OCA\Mail\Service\MailManager; -use OCP\AppFramework\Db\DoesNotExistException; -use OCP\AppFramework\Http; -use OCP\AppFramework\Http\ContentSecurityPolicy; -use OCP\AppFramework\Http\JSONResponse; use OCP\AppFramework\Http\ZipResponse; +use OCA\Mail\Exception\ClientException; +use OCP\AppFramework\Http\JSONResponse; +use OCA\Mail\Exception\ServiceException; +use OCA\Mail\Contracts\IMailTransmission; use OCP\AppFramework\Utility\ITimeFactory; -use OCP\Files\Folder; -use OCP\Files\IMimeTypeDetector; -use OCP\IL10N; -use OCP\IRequest; -use OCP\IURLGenerator; +use OCA\Mail\Controller\MessagesController; use PHPUnit\Framework\MockObject\MockObject; -use Psr\Log\LoggerInterface; -use ReflectionObject; +use OCA\Mail\Contracts\ITrustedSenderService; +use OCA\Mail\Http\AttachmentDownloadResponse; +use ChristophWurst\Nextcloud\Testing\TestCase; +use OCP\AppFramework\Db\DoesNotExistException; +use OCP\AppFramework\Http\ContentSecurityPolicy; +use OC\Security\CSP\ContentSecurityPolicyNonceManager; class MessagesControllerTest extends TestCase { @@ -647,6 +648,208 @@ class MessagesControllerTest extends TestCase { $this->assertEquals($expected, $response); } + public function testSetTagFailing() { + $accountId = 17; + $mailboxId = 987; + $id = 1; + $message = new \OCA\Mail\Db\Message(); + $message->setUid(444); + $message->setMailboxId($mailboxId); + $message->setMessageId('<jhfjkhdsjkfhdsjkhfjkdsh@test.com>'); + $mailbox = new \OCA\Mail\Db\Mailbox(); + $mailbox->setName('INBOX'); + $mailbox->setAccountId($accountId); + $this->mailManager->expects($this->once()) + ->method('getMessage') + ->with($this->userId, $id) + ->willReturn($message); + $this->mailManager->expects($this->once()) + ->method('getMailbox') + ->with($this->userId, $mailboxId) + ->willReturn($mailbox); + $this->accountService->expects($this->once()) + ->method('find') + ->with($this->equalTo($this->userId), $this->equalTo($accountId)) + ->willThrowException(new DoesNotExistException('')); + $this->mailManager->expects($this->never()) + ->method('getTagByImapLabel'); + $this->mailManager->expects($this->never()) + ->method('tagMessage'); + + $this->controller->setTag($id, Tag::LABEL_IMPORTANT); + } + + public function testSetTagNotFound() { + $accountId = 17; + $mailboxId = 987; + $id = 1; + $imapLabel = '$label6'; + $message = new \OCA\Mail\Db\Message(); + $message->setUid(444); + $message->setMailboxId($mailboxId); + $message->setMessageId('<jhfjkhdsjkfhdsjkhfjkdsh@test.com>'); + $mailbox = new \OCA\Mail\Db\Mailbox(); + $mailbox->setName('INBOX'); + $mailbox->setAccountId($accountId); + $this->mailManager->expects($this->once()) + ->method('getMessage') + ->with($this->userId, $id) + ->willReturn($message); + $this->mailManager->expects($this->once()) + ->method('getMailbox') + ->with($this->userId, $mailboxId) + ->willReturn($mailbox); + $this->accountService->expects($this->once()) + ->method('find') + ->with($this->equalTo($this->userId), $this->equalTo($accountId)) + ->will($this->returnValue($this->account)); + $this->mailManager->expects($this->once()) + ->method('getTagByImapLabel') + ->with($imapLabel,$this->userId) + ->willThrowException(new DoesNotExistException('')); + $this->mailManager->expects($this->never()) + ->method('tagMessage'); + + $this->controller->setTag($id, $imapLabel); + } + + public function testSetTag() { + $accountId = 17; + $mailboxId = 987; + $id = 1; + $tag = new Tag(); + $tag->setImapLabel(Tag::LABEL_IMPORTANT); + $message = new \OCA\Mail\Db\Message(); + $message->setUid(444); + $message->setMailboxId($mailboxId); + $message->setMessageId('<jhfjkhdsjkfhdsjkhfjkdsh@test.com>'); + $mailbox = new \OCA\Mail\Db\Mailbox(); + $mailbox->setName('INBOX'); + $mailbox->setAccountId($accountId); + $this->mailManager->expects($this->once()) + ->method('getMessage') + ->with($this->userId, $id) + ->willReturn($message); + $this->mailManager->expects($this->once()) + ->method('getMailbox') + ->with($this->userId, $mailboxId) + ->willReturn($mailbox); + $this->accountService->expects($this->once()) + ->method('find') + ->with($this->equalTo($this->userId), $this->equalTo($accountId)) + ->will($this->returnValue($this->account)); + $this->mailManager->expects($this->once()) + ->method('getTagByImapLabel') + ->with($tag->getImapLabel(),$this->userId) + ->willReturn($tag); + $this->mailManager->expects($this->once()) + ->method('tagMessage') + ->with($this->account, $mailbox->getName(), $message, $tag, true); + + $this->controller->setTag($id, $tag->getImapLabel()); + } + + public function testRemoveTagFailing() { + $accountId = 17; + $mailboxId = 987; + $id = 1; + $message = new \OCA\Mail\Db\Message(); + $message->setUid(444); + $message->setMailboxId($mailboxId); + $message->setMessageId('<jhfjkhdsjkfhdsjkhfjkdsh@test.com>'); + $mailbox = new \OCA\Mail\Db\Mailbox(); + $mailbox->setName('INBOX'); + $mailbox->setAccountId($accountId); + $this->mailManager->expects($this->once()) + ->method('getMessage') + ->with($this->userId, $id) + ->willReturn($message); + $this->mailManager->expects($this->once()) + ->method('getMailbox') + ->with($this->userId, $mailboxId) + ->willReturn($mailbox); + $this->accountService->expects($this->once()) + ->method('find') + ->with($this->equalTo($this->userId), $this->equalTo($accountId)) + ->willThrowException(new DoesNotExistException('')); + $this->mailManager->expects($this->never()) + ->method('getTagByImapLabel'); + $this->mailManager->expects($this->never()) + ->method('tagMessage'); + + $this->controller->removeTag($id, Tag::LABEL_IMPORTANT); + } + + public function testRemoveTagNotFound() { + $accountId = 17; + $mailboxId = 987; + $id = 1; + $imapLabel = '$label6'; + $message = new \OCA\Mail\Db\Message(); + $message->setUid(444); + $message->setMailboxId($mailboxId); + $message->setMessageId('<jhfjkhdsjkfhdsjkhfjkdsh@test.com>'); + $mailbox = new \OCA\Mail\Db\Mailbox(); + $mailbox->setName('INBOX'); + $mailbox->setAccountId($accountId); + $this->mailManager->expects($this->once()) + ->method('getMessage') + ->with($this->userId, $id) + ->willReturn($message); + $this->mailManager->expects($this->once()) + ->method('getMailbox') + ->with($this->userId, $mailboxId) + ->willReturn($mailbox); + $this->accountService->expects($this->once()) + ->method('find') + ->with($this->equalTo($this->userId), $this->equalTo($accountId)) + ->will($this->returnValue($this->account)); + $this->mailManager->expects($this->once()) + ->method('getTagByImapLabel') + ->with($imapLabel,$this->userId) + ->willThrowException(new DoesNotExistException('')); + $this->mailManager->expects($this->never()) + ->method('tagMessage'); + + $this->controller->removeTag($id, $imapLabel); + } + + public function testRemoveTag() { + $accountId = 17; + $mailboxId = 987; + $id = 1; + $tag = new Tag(); + $tag->setImapLabel(Tag::LABEL_IMPORTANT); + $message = new \OCA\Mail\Db\Message(); + $message->setUid(444); + $message->setMailboxId($mailboxId); + $message->setMessageId('<jhfjkhdsjkfhdsjkhfjkdsh@test.com>'); + $mailbox = new \OCA\Mail\Db\Mailbox(); + $mailbox->setName('INBOX'); + $mailbox->setAccountId($accountId); + $this->mailManager->expects($this->once()) + ->method('getMessage') + ->with($this->userId, $id) + ->willReturn($message); + $this->mailManager->expects($this->once()) + ->method('getMailbox') + ->with($this->userId, $mailboxId) + ->willReturn($mailbox); + $this->accountService->expects($this->once()) + ->method('find') + ->with($this->equalTo($this->userId), $this->equalTo($accountId)) + ->will($this->returnValue($this->account)); + $this->mailManager->expects($this->once()) + ->method('getTagByImapLabel') + ->with($tag->getImapLabel(),$this->userId) + ->willReturn($tag); + $this->mailManager->expects($this->once()) + ->method('tagMessage') + ->with($this->account, $mailbox->getName(), $message, $tag, false); + + $this->controller->removeTag($id, $tag->getImapLabel()); + } + public function testSetFlagsFlagged() { $accountId = 17; $mailboxId = 987; diff --git a/tests/Unit/Listener/MessageCacheUpdaterListenerTest.php b/tests/Unit/Listener/MessageCacheUpdaterListenerTest.php index 5fac913cf..3981b3744 100644 --- a/tests/Unit/Listener/MessageCacheUpdaterListenerTest.php +++ b/tests/Unit/Listener/MessageCacheUpdaterListenerTest.php @@ -30,6 +30,7 @@ use ChristophWurst\Nextcloud\Testing\TestCase; use OCA\Mail\Account; use OCA\Mail\Db\Mailbox; use OCA\Mail\Db\Message; +use OCA\Mail\Db\Tag; use OCA\Mail\Events\MessageFlaggedEvent; use OCA\Mail\Listener\MessageCacheUpdaterListener; use OCP\EventDispatcher\Event; @@ -65,7 +66,7 @@ class MessageCacheUpdaterListenerTest extends TestCase { $account, $mailbox, 123, - 'important', + Tag::LABEL_IMPORTANT, true ); $this->serviceMock->getParameter('mapper') diff --git a/tests/Unit/Service/MailManagerTest.php b/tests/Unit/Service/MailManagerTest.php index d46e96cbf..8406205dc 100644 --- a/tests/Unit/Service/MailManagerTest.php +++ b/tests/Unit/Service/MailManagerTest.php @@ -31,6 +31,8 @@ use OCA\Mail\Db\Mailbox; use OCA\Mail\Db\MailboxMapper; use OCA\Mail\Db\Message; use OCA\Mail\Db\MessageMapper as DbMessageMapper; +use OCA\Mail\Db\Tag; +use OCA\Mail\Db\TagMapper; use OCA\Mail\Events\BeforeMessageDeletedEvent; use OCA\Mail\Exception\ServiceException; use OCA\Mail\Folder; @@ -74,6 +76,9 @@ class MailManagerTest extends TestCase { /** @var MockObject|LoggerInterface */ private $logger; + /** @var MockObject|TagMapper */ + private $tagMapper; + protected function setUp(): void { parent::setUp(); @@ -85,6 +90,7 @@ class MailManagerTest extends TestCase { $this->mailboxSync = $this->createMock(MailboxSync::class); $this->eventDispatcher = $this->createMock(IEventDispatcher::class); $this->logger = $this->createMock(LoggerInterface::class); + $this->tagMapper = $this->createMock(TagMapper::class); $this->manager = new MailManager( $this->imapClientFactory, @@ -94,7 +100,8 @@ class MailManagerTest extends TestCase { $this->imapMessageMapper, $this->dbMessageMapper, $this->eventDispatcher, - $this->logger + $this->logger, + $this->tagMapper ); } @@ -310,8 +317,8 @@ class MailManagerTest extends TestCase { $this->imapMessageMapper->expects($this->never()) ->method('removeFlag'); - $this->manager->flagMessage($account, 'INBOX', 123, 'important', true); - $this->manager->flagMessage($account, 'INBOX', 123, 'important', false); + $this->manager->flagMessage($account, 'INBOX', 123, Tag::LABEL_IMPORTANT, true); + $this->manager->flagMessage($account, 'INBOX', 123, Tag::LABEL_IMPORTANT, false); } public function testSetCustomFlagWithIMAPCapabilities(): void { @@ -327,7 +334,7 @@ class MailManagerTest extends TestCase { $this->imapMessageMapper->expects($this->once()) ->method('addFlag'); - $this->manager->flagMessage($account, 'INBOX', 123, 'important', true); + $this->manager->flagMessage($account, 'INBOX', 123, Tag::LABEL_IMPORTANT, true); } public function testUnsetCustomFlagWithIMAPCapabilities(): void { @@ -343,7 +350,7 @@ class MailManagerTest extends TestCase { $this->imapMessageMapper->expects($this->once()) ->method('removeFlag'); - $this->manager->flagMessage($account, 'INBOX', 123, 'important', false); + $this->manager->flagMessage($account, 'INBOX', 123, Tag::LABEL_IMPORTANT, false); } public function testFilterFlagStandard(): void { @@ -378,7 +385,7 @@ class MailManagerTest extends TestCase { ->method('getClient') ->willReturn($client); - $this->assertEquals([], $this->manager->filterFlags($account, '$important' , 'INBOX')); + $this->assertEquals([], $this->manager->filterFlags($account, Tag::LABEL_IMPORTANT , 'INBOX')); } public function testSetFilterFlagsImportant() { @@ -392,7 +399,7 @@ class MailManagerTest extends TestCase { ->method('status') ->willReturn(['permflags' => [ "11" => "\*" ]]); - $this->assertEquals(['$important'], $this->manager->filterFlags($account, 'important' , 'INBOX')); + $this->assertEquals([Tag::LABEL_IMPORTANT], $this->manager->filterFlags($account, Tag::LABEL_IMPORTANT , 'INBOX')); } public function testIsPermflagsEnabledTrue(): void { @@ -443,6 +450,94 @@ class MailManagerTest extends TestCase { $this->manager->flagMessage($account, 'INBOX', 123, 'seen', false); } + public function testTagMessage(): void { + $client = $this->createMock(Horde_Imap_Client_Socket::class); + $account = $this->createMock(Account::class); + $tag = new Tag(); + $tag->setImapLabel(Tag::LABEL_IMPORTANT); + $message = new \OCA\Mail\Db\Message(); + $message->setUid(123); + $message->setMessageId('<jhfjkhdsjkfhdsjkhfjkdsh@test.com>'); + $this->imapClientFactory->expects($this->any()) + ->method('getClient') + ->willReturn($client); + $mb = $this->createMock(Mailbox::class); + $this->mailboxMapper->expects($this->once()) + ->method('find') + ->with($account, 'INBOX') + ->willReturn($mb); + $client->expects($this->once()) + ->method('status') + ->willReturn(['permflags' => [ "11" => "\*"] ]); + $this->imapMessageMapper->expects($this->once()) + ->method('addFlag') + ->with($client, $mb, 123, Tag::LABEL_IMPORTANT); + $account->expects($this->once()) + ->method('getUserId') + ->willReturn('test'); + $this->manager->tagMessage($account, 'INBOX', $message, $tag, true); + } + + public function testUntagMessage(): void { + $client = $this->createMock(Horde_Imap_Client_Socket::class); + $account = $this->createMock(Account::class); + $tag = new Tag(); + $tag->setImapLabel(Tag::LABEL_IMPORTANT); + $message = new \OCA\Mail\Db\Message(); + $message->setUid(123); + $message->setMessageId('<jhfjkhdsjkfhdsjkhfjkdsh@test.com>'); + $this->imapClientFactory->expects($this->any()) + ->method('getClient') + ->willReturn($client); + $mb = $this->createMock(Mailbox::class); + $this->mailboxMapper->expects($this->once()) + ->method('find') + ->with($account, 'INBOX') + ->willReturn($mb); + $client->expects($this->once()) + ->method('status') + ->willReturn(['permflags' => [ "11" => "\*"] ]); + $this->imapMessageMapper->expects($this->once()) + ->method('removeFlag') + ->with($client, $mb, 123, Tag::LABEL_IMPORTANT); + $this->imapMessageMapper->expects($this->never()) + ->method('addFlag'); + $account->expects($this->never()) + ->method('getUserId') + ->willReturn('test'); + $this->manager->tagMessage($account, 'INBOX', $message, $tag, false); + } + + public function testTagNoIMAPCapabilities(): void { + $client = $this->createMock(Horde_Imap_Client_Socket::class); + $account = $this->createMock(Account::class); + $message = new \OCA\Mail\Db\Message(); + $message->setUid(123); + $message->setMessageId('<jhfjkhdsjkfhdsjkhfjkdsh@test.com>'); + $tag = new Tag(); + $tag->setImapLabel(Tag::LABEL_IMPORTANT); + + $this->imapClientFactory->expects($this->any()) + ->method('getClient') + ->willReturn($client); + $mb = $this->createMock(Mailbox::class); + $this->mailboxMapper->expects($this->once()) + ->method('find') + ->with($account, 'INBOX') + ->willReturn($mb); + $client->expects($this->once()) + ->method('status') + ->willReturn([]); + $this->imapMessageMapper->expects($this->never()) + ->method('removeFlag'); + $this->imapMessageMapper->expects($this->never()) + ->method('addFlag'); + $account->expects($this->once()) + ->method('getUserId') + ->willReturn('test'); + $this->manager->tagMessage($account, 'INBOX', $message, $tag, true); + } + public function testGetThread(): void { $account = $this->createMock(Account::class); $messageId = 123; diff --git a/tests/Unit/Service/SetupServiceTest.php b/tests/Unit/Service/SetupServiceTest.php index 70288db2a..1d5ff27bc 100644 --- a/tests/Unit/Service/SetupServiceTest.php +++ b/tests/Unit/Service/SetupServiceTest.php @@ -34,6 +34,7 @@ use OCA\Mail\Service\AutoConfig\AutoConfig; use OCA\Mail\Service\SetupService; use OCA\Mail\SMTP\SmtpClientFactory; use ChristophWurst\Nextcloud\Testing\TestCase; +use OCA\Mail\Db\TagMapper; use OCP\Security\ICrypto; use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; @@ -61,6 +62,9 @@ class SetupServiceTest extends TestCase { /** @var SetupService */ private $service; + /** @var TagMapper|MockObject */ + private $tagMapper; + protected function setUp(): void { parent::setUp(); @@ -70,6 +74,7 @@ class SetupServiceTest extends TestCase { $this->smtpClientFactory = $this->createMock(SmtpClientFactory::class); $this->imapClientFactory = $this->createMock(IMAPClientFactory::class); $this->logger = $this->createMock(LoggerInterface::class); + $this->tagMapper = $this->createMock(TagMapper::class); $this->service = new SetupService( $this->autoConfig, @@ -77,7 +82,8 @@ class SetupServiceTest extends TestCase { $this->crypto, $this->smtpClientFactory, $this->imapClientFactory, - $this->logger + $this->logger, + $this->tagMapper ); } @@ -109,6 +115,11 @@ class SetupServiceTest extends TestCase { $this->accountService->expects($this->once()) ->method('save') ->with($account); + + $this->tagMapper->expects($this->once()) + ->method('createDefaultTags') + ->with($account); + $expected = new Account($account); $actual = $this->service->createNewAutoConfiguredAccount($name, $email, $password); diff --git a/tests/psalm-baseline.xml b/tests/psalm-baseline.xml index 1cb8ae6f3..f05676999 100644 --- a/tests/psalm-baseline.xml +++ b/tests/psalm-baseline.xml @@ -93,6 +93,11 @@ <code>$qb2->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)</code> </ImplicitToStringCast> </file> + <file src="lib/Db/TagMapper.php"> + <ImplicitToStringCast occurrences="1"> + <code>$qb->createNamedParameter($ids, IQueryBuilder::PARAM_STR_ARRAY)</code> + </ImplicitToStringCast> + </file> <file src="lib/Db/MessageMapper.php"> <ImplicitToStringCast occurrences="26"> <code>$deleteRecipientsQuery->createFunction($messageIdQuery->getSQL())</code> |