diff options
author | Christoph Wurst <christoph@winzerhof-wurst.at> | 2019-08-30 12:19:54 +0300 |
---|---|---|
committer | Christoph Wurst <christoph@winzerhof-wurst.at> | 2019-09-04 12:47:29 +0300 |
commit | 90275abd7796a85c9107eddb7939ed8f1e569c46 (patch) | |
tree | 44ec8d5adb8c684031bdc3f1456cc9bdaca9f2bc /tests | |
parent | 0c59d64478ee20cd1f8371fdda489836af2ca5ce (diff) |
Clean up the code responsible for deleting a message
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Controller/MessagesControllerTest.php | 14 | ||||
-rw-r--r-- | tests/Db/MailboxMapperTest.php | 66 | ||||
-rw-r--r-- | tests/IMAP/MessageMapperTest.php | 11 | ||||
-rw-r--r-- | tests/Service/MailManagerTest.php | 142 |
4 files changed, 223 insertions, 10 deletions
diff --git a/tests/Controller/MessagesControllerTest.php b/tests/Controller/MessagesControllerTest.php index e5b1b1504..b65b20bc5 100644 --- a/tests/Controller/MessagesControllerTest.php +++ b/tests/Controller/MessagesControllerTest.php @@ -28,6 +28,7 @@ use OCA\Mail\Account; use OCA\Mail\Attachment; use OCA\Mail\Contracts\IMailManager; use OCA\Mail\Controller\MessagesController; +use OCA\Mail\Exception\ServiceException; use OCA\Mail\Http\AttachmentDownloadResponse; use OCA\Mail\Http\HtmlResponse; use OCA\Mail\Mailbox; @@ -355,10 +356,9 @@ class MessagesControllerTest extends TestCase { ->method('find') ->with($this->equalTo($this->userId), $this->equalTo($accountId)) ->will($this->returnValue($this->account)); - - $this->account->expects($this->once()) + $this->mailManager->expects($this->once()) ->method('deleteMessage') - ->with(base64_decode($folderId), $messageId); + ->with($this->account, base64_decode($folderId), $messageId); $expected = new JSONResponse(); $result = $this->controller->destroy($accountId, $folderId, $messageId); @@ -387,11 +387,11 @@ class MessagesControllerTest extends TestCase { ->method('find') ->with($this->equalTo($this->userId), $this->equalTo($accountId)) ->will($this->returnValue($this->account)); - $this->account->expects($this->once()) + $this->mailManager->expects($this->once()) ->method('deleteMessage') - ->with(base64_decode($folderId), $messageId) - ->will($this->throwException(new DoesNotExistException(''))); - $this->expectException(DoesNotExistException::class); + ->with($this->account, base64_decode($folderId), $messageId) + ->willThrowException(new ServiceException()); + $this->expectException(ServiceException::class); $this->controller->destroy($accountId, $folderId, $messageId); } diff --git a/tests/Db/MailboxMapperTest.php b/tests/Db/MailboxMapperTest.php index eac76bb53..840dfbf96 100644 --- a/tests/Db/MailboxMapperTest.php +++ b/tests/Db/MailboxMapperTest.php @@ -26,9 +26,12 @@ namespace OCA\Mail\Tests\Db; use ChristophWurst\Nextcloud\Testing\DatabaseTransaction; use ChristophWurst\Nextcloud\Testing\TestCase; use OCA\Mail\Account; +use OCA\Mail\Db\Mailbox; use OCA\Mail\Db\MailboxMapper; +use OCP\AppFramework\Db\DoesNotExistException; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; +use PHPUnit\Framework\MockObject\MockObject; class MailboxMapperTest extends TestCase { @@ -86,4 +89,67 @@ class MailboxMapperTest extends TestCase { $this->assertCount(5, $result); } + public function testNoInboxFound() { + /** @var Account|MockObject $account */ + $account = $this->createMock(Account::class); + $account->method('getId')->willReturn(13); + $this->expectException(DoesNotExistException::class); + + $this->mapper->find($account, 'INBOX'); + } + + public function testFindInbox() { + /** @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('INBOX'), + 'account_id' => $qb->createNamedParameter(13, IQueryBuilder::PARAM_INT), + 'sync_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), + ]); + $insert->execute(); + + $result = $this->mapper->find($account, 'INBOX'); + + $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_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/IMAP/MessageMapperTest.php b/tests/IMAP/MessageMapperTest.php index 09db9b26d..b5e190a3e 100644 --- a/tests/IMAP/MessageMapperTest.php +++ b/tests/IMAP/MessageMapperTest.php @@ -27,16 +27,25 @@ use Horde_Imap_Client_Data_Fetch; use Horde_Imap_Client_Fetch_Results; use OCA\Mail\IMAP\MessageMapper; use OCA\Mail\Model\IMAPMessage; +use OCP\ILogger; +use PHPUnit\Framework\MockObject\MockObject; class MessageMapperTest extends TestCase { + /** @var ILogger|MockObject */ + private $logger; + /** @var MessageMapper */ private $mapper; protected function setUp() { parent::setUp(); - $this->mapper = new MessageMapper(); + $this->logger = $this->createMock(ILogger::class); + + $this->mapper = new MessageMapper( + $this->logger + ); } public function testGetByIds() { diff --git a/tests/Service/MailManagerTest.php b/tests/Service/MailManagerTest.php index 293294ef2..fcc85d00f 100644 --- a/tests/Service/MailManagerTest.php +++ b/tests/Service/MailManagerTest.php @@ -26,6 +26,8 @@ use Horde_Imap_Client_Socket; use OCA\Mail\Account; use OCA\Mail\Db\Mailbox; use OCA\Mail\Db\MailboxMapper; +use OCA\Mail\Events\BeforeMessageDeletedEvent; +use OCA\Mail\Exception\ServiceException; use OCA\Mail\Folder; use OCA\Mail\IMAP\FolderMapper; use OCA\Mail\IMAP\FolderStats; @@ -36,6 +38,8 @@ use OCA\Mail\IMAP\Sync\Request; use OCA\Mail\IMAP\Sync\Response; use OCA\Mail\IMAP\Sync\Synchronizer; use OCA\Mail\Service\MailManager; +use OCP\AppFramework\Db\DoesNotExistException; +use OCP\EventDispatcher\IEventDispatcher; use PHPUnit\Framework\MockObject\MockObject; class MailManagerTest extends TestCase { @@ -58,6 +62,9 @@ class MailManagerTest extends TestCase { /** @var Synchronizer|MockObject */ private $sync; + /** @var IEventDispatcher|MockObject */ + private $eventDispatcher; + /** @var MailManager */ private $manager; @@ -70,6 +77,7 @@ class MailManagerTest extends TestCase { $this->folderMapper = $this->createMock(FolderMapper::class); $this->messageMapper = $this->createMock(MessageMapper::class); $this->sync = $this->createMock(Synchronizer::class); + $this->eventDispatcher = $this->createMock(IEventDispatcher::class); $this->manager = new MailManager( $this->imapClientFactory, @@ -77,7 +85,8 @@ class MailManagerTest extends TestCase { $this->mailboxSync, $this->folderMapper, $this->sync, - $this->messageMapper + $this->messageMapper, + $this->eventDispatcher ); } @@ -113,7 +122,7 @@ class MailManagerTest extends TestCase { $this->imapClientFactory->expects($this->once()) ->method('getClient') ->willReturn($client); - $folder =$this->createMock(Folder::class); + $folder = $this->createMock(Folder::class); $this->folderMapper->expects($this->once()) ->method('createFolder') ->with($this->equalTo($client), $this->equalTo($account), $this->equalTo('new')) @@ -163,4 +172,133 @@ class MailManagerTest extends TestCase { $this->manager->syncMessages($account, $syncRequest); } + public function testDeleteMessageSourceFolderNotFound(): void { + /** @var Account|MockObject $account */ + $account = $this->createMock(Account::class); + $this->eventDispatcher->expects($this->once()) + ->method('dispatch') + ->with( + $this->equalTo(BeforeMessageDeletedEvent::class), + $this->anything() + ); + $this->mailboxMapper->expects($this->once()) + ->method('find') + ->with($account, 'INBOX') + ->willThrowException(new DoesNotExistException("")); + $this->expectException(ServiceException::class); + + $this->manager->deleteMessage( + $account, + 'INBOX', + 123 + ); + } + + public function testDeleteMessageTrashFolderNotFound(): void { + /** @var Account|MockObject $account */ + $account = $this->createMock(Account::class); + $this->eventDispatcher->expects($this->once()) + ->method('dispatch') + ->with( + $this->equalTo(BeforeMessageDeletedEvent::class), + $this->anything() + ); + $this->mailboxMapper->expects($this->once()) + ->method('find') + ->with($account, 'INBOX') + ->willReturn($this->createMock(Mailbox::class)); + $this->mailboxMapper->expects($this->once()) + ->method('findSpecial') + ->with($account, 'trash') + ->willThrowException(new DoesNotExistException("")); + $this->expectException(ServiceException::class); + + $this->manager->deleteMessage( + $account, + 'INBOX', + 123 + ); + } + + public function testDeleteMessage(): void { + /** @var Account|MockObject $account */ + $account = $this->createMock(Account::class); + $inbox = new Mailbox(); + $inbox->setName('INBOX'); + $trash = new Mailbox(); + $trash->setName('Trash'); + $this->eventDispatcher->expects($this->once()) + ->method('dispatch') + ->with( + $this->equalTo(BeforeMessageDeletedEvent::class), + $this->anything() + ); + $this->mailboxMapper->expects($this->once()) + ->method('find') + ->with($account, 'INBOX') + ->willReturn($inbox); + $this->mailboxMapper->expects($this->once()) + ->method('findSpecial') + ->with($account, 'trash') + ->willReturn($trash); + $client = $this->createMock(Horde_Imap_Client_Socket::class); + $this->imapClientFactory->expects($this->once()) + ->method('getClient') + ->willReturn($client); + $this->messageMapper->expects($this->once()) + ->method('move') + ->with( + $client, + 'INBOX', + 123, + 'Trash' + ); + + $this->manager->deleteMessage( + $account, + 'INBOX', + 123 + ); + } + + public function testExpungeMessage(): void { + /** @var Account|MockObject $account */ + $account = $this->createMock(Account::class); + $source = new Mailbox(); + $source->setName('Trash'); + $trash = new Mailbox(); + $trash->setName('Trash'); + $this->eventDispatcher->expects($this->once()) + ->method('dispatch') + ->with( + $this->equalTo(BeforeMessageDeletedEvent::class), + $this->anything() + ); + $this->mailboxMapper->expects($this->once()) + ->method('find') + ->with($account, 'Trash') + ->willReturn($source); + $this->mailboxMapper->expects($this->once()) + ->method('findSpecial') + ->with($account, 'trash') + ->willReturn($trash); + $client = $this->createMock(Horde_Imap_Client_Socket::class); + $this->imapClientFactory->expects($this->once()) + ->method('getClient') + ->willReturn($client); + $this->messageMapper->expects($this->once()) + ->method('expunge') + ->with( + $client, + 'Trash', + 123 + ); + + $this->manager->deleteMessage( + $account, + 'Trash', + 123 + ); + } + } |