diff options
author | Christoph Wurst <christoph@winzerhof-wurst.at> | 2019-08-28 19:25:54 +0300 |
---|---|---|
committer | Christoph Wurst <christoph@winzerhof-wurst.at> | 2019-08-29 13:00:40 +0300 |
commit | f40aba36d6356acb23cd0fb6aa9e6584f69fdca3 (patch) | |
tree | 572320b9584e8e60f41aa8bc4e21295768834aa9 /tests | |
parent | 3cc08ff470a716fd7177fc84f89c64ca772ffe8e (diff) |
Add a database cache for mailbox data
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Db/MailboxMapperTest.php | 91 | ||||
-rw-r--r-- | tests/FolderTest.php | 13 | ||||
-rw-r--r-- | tests/IMAP/FolderMapperTest.php | 10 | ||||
-rw-r--r-- | tests/IMAP/MailboxSyncTest.php | 122 | ||||
-rw-r--r-- | tests/Integration/Service/FolderMapperIntegrationTest.php | 1 | ||||
-rw-r--r-- | tests/SearchFolderTest.php | 10 | ||||
-rw-r--r-- | tests/Service/MailManagerTest.php | 44 |
7 files changed, 261 insertions, 30 deletions
diff --git a/tests/Db/MailboxMapperTest.php b/tests/Db/MailboxMapperTest.php new file mode 100644 index 000000000..8310d671d --- /dev/null +++ b/tests/Db/MailboxMapperTest.php @@ -0,0 +1,91 @@ +<?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\Db; + +use ChristophWurst\Nextcloud\Testing\DatabaseTransaction; +use ChristophWurst\Nextcloud\Testing\TestCase; +use OCA\Mail\Account; +use OCA\Mail\Db\MailboxMapper; +use OCP\DB\QueryBuilder\IQueryBuilder; +use OCP\Diagnostics\IQuery; +use OCP\IDBConnection; +use PHPUnit\Framework\MockObject\MockObject; + +class MailboxMapperTest extends TestCase { + + use DatabaseTransaction; + + /** @var IDBConnection */ + private $db; + + /** @var MailboxMapper */ + private $mapper; + + protected function setUp() { + parent::setUp(); + + $this->db = \OC::$server->getDatabaseConnection(); + $this->mapper = new MailboxMapper( + $this->db + ); + + $qb = $this->db->getQueryBuilder(); + + $delete = $qb->delete($this->mapper->getTableName()); + $delete->execute(); + } + + public function testFindAllNoData() { + $account = $this->createMock(Account::class); + $account->method('getId')->willReturn(13); + + $result = $this->mapper->findAll($account); + + $this->assertEmpty($result); + } + + public function testFindAll() { + $account = $this->createMock(Account::class); + $account->method('getId')->willReturn(13); + foreach (range(1, 10) as $i) { + $qb = $this->db->getQueryBuilder(); + $insert = $qb->insert($this->mapper->getTableName()) + ->values([ + 'id' => $qb->createNamedParameter("folder$i"), + 'account_id' => $qb->createNamedParameter($i <= 5 ? 13 : 14, IQueryBuilder::PARAM_INT), + 'sync_token' => $qb->createNamedParameter('VTEsVjE0Mjg1OTkxNDk='), + 'delimiter' => $qb->createNamedParameter('.'), + 'messages' => $qb->createNamedParameter($i * 100, IQueryBuilder::PARAM_INT), + 'unseen' => $qb->createNamedParameter($i, IQueryBuilder::PARAM_INT), + 'selectable' => $qb->createNamedParameter(true, IQueryBuilder::PARAM_BOOL), + ]); + $insert->execute(); + } + + $result = $this->mapper->findAll($account); + + $this->assertCount(5, $result); + } + +} diff --git a/tests/FolderTest.php b/tests/FolderTest.php index c0649376c..5d9f9e876 100644 --- a/tests/FolderTest.php +++ b/tests/FolderTest.php @@ -29,8 +29,8 @@ use PHPUnit_Framework_MockObject_MockObject; class FolderTest extends TestCase { - /** @var Account|PHPUnit_Framework_MockObject_MockObject */ - private $account; + /** @var int */ + private $accountId; /** @var Horde_Imap_Client_Mailbox|PHPUnit_Framework_MockObject_MockObject */ private $mailbox; @@ -39,10 +39,10 @@ class FolderTest extends TestCase { private $folder; private function mockFolder(array $attributes = [], $delimiter = '.') { - $this->account = $this->createMock(Account::class); + $this->accountId = 15; $this->mailbox = $this->createMock(Horde_Imap_Client_Mailbox::class); - $this->folder = new Folder($this->account, $this->mailbox, $attributes, $delimiter); + $this->folder = new Folder($this->accountId, $this->mailbox, $attributes, $delimiter); } public function testGetMailbox() { @@ -131,9 +131,6 @@ class FolderTest extends TestCase { ->method('__get') ->with($this->equalTo('utf8')) ->willReturn('Sent'); - $this->account->expects($this->once()) - ->method('getId') - ->willReturn(123); $this->folder->setDisplayName('Gesendet'); $this->folder->addSpecialUse('sent'); @@ -145,7 +142,7 @@ class FolderTest extends TestCase { $expected = [ 'id' => base64_encode('Sent'), - 'accountId' => 123, + 'accountId' => 15, 'name' => 'Gesendet', 'specialRole' => null, 'unseen' => 13, diff --git a/tests/IMAP/FolderMapperTest.php b/tests/IMAP/FolderMapperTest.php index 44d4c432b..52046d4ed 100644 --- a/tests/IMAP/FolderMapperTest.php +++ b/tests/IMAP/FolderMapperTest.php @@ -62,6 +62,7 @@ class FolderMapperTest extends TestCase { public function testGetFolders() { $account = $this->createMock(Account::class); + $account->method('getId')->willReturn(27); $client = $this->createMock(Horde_Imap_Client_Socket::class); $client->expects($this->once()) ->method('listMailboxes') @@ -86,9 +87,9 @@ class FolderMapperTest extends TestCase { ], ]); $expected = [ - new Folder($account, new Horde_Imap_Client_Mailbox('INBOX'), [], '.'), - new SearchFolder($account, new Horde_Imap_Client_Mailbox('INBOX'), [], '.'), - new Folder($account, new Horde_Imap_Client_Mailbox('Sent'), ['\sent'], '.'), + new Folder(27, new Horde_Imap_Client_Mailbox('INBOX'), [], '.'), + new SearchFolder(27, new Horde_Imap_Client_Mailbox('INBOX'), [], '.'), + new Folder(27, new Horde_Imap_Client_Mailbox('Sent'), ['\sent'], '.'), ]; $folders = $this->mapper->getFolders($account, $client); @@ -98,6 +99,7 @@ class FolderMapperTest extends TestCase { public function testCreateFolder() { $account = $this->createMock(Account::class); + $account->method('getId')->willReturn(42); $client = $this->createMock(Horde_Imap_Client_Socket::class); $client->expects($this->once()) ->method('createMailbox') @@ -120,7 +122,7 @@ class FolderMapperTest extends TestCase { $created = $this->mapper->createFolder($client, $account, 'new'); - $expected = new Folder($account, new Horde_Imap_Client_Mailbox('new'), [], '.'); + $expected = new Folder(42, new Horde_Imap_Client_Mailbox('new'), [], '.'); $this->assertEquals($expected, $created); } diff --git a/tests/IMAP/MailboxSyncTest.php b/tests/IMAP/MailboxSyncTest.php new file mode 100644 index 000000000..3fa7c0a54 --- /dev/null +++ b/tests/IMAP/MailboxSyncTest.php @@ -0,0 +1,122 @@ +<?php +/** + * @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\IMAP; + +use ChristophWurst\Nextcloud\Testing\TestCase; +use Horde_Imap_Client_Socket; +use OC\AppFramework\Utility\TimeFactory; +use OCA\Mail\Account; +use OCA\Mail\Db\MailAccount; +use OCA\Mail\Db\MailAccountMapper; +use OCA\Mail\Db\MailboxMapper; +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\ILogger; +use PHPUnit\Framework\MockObject\MockObject; + +class MailboxSyncTest extends TestCase { + + /** @var MailboxMapper|MockObject */ + private $mailboxMapper; + + /** @var FolderMapper|MockObject */ + private $folderMapper; + + /** @var MailAccountMapper|MockObject */ + private $mailAccountMapper; + + /** @var IMAPClientFactory|MockObject */ + private $imapClientFactory; + + /** @var TimeFactory|MockObject */ + private $timeFactory; + + /** @var ILogger|MockObject */ + private $logger; + + /** @var MailboxSync */ + private $sync; + + protected function setUp() { + parent::setUp(); + + $this->mailboxMapper = $this->createMock(MailboxMapper::class); + $this->folderMapper = $this->createMock(FolderMapper::class); + $this->mailAccountMapper = $this->createMock(MailAccountMapper::class); + $this->imapClientFactory = $this->createMock(IMAPClientFactory::class); + $this->timeFactory = $this->createMock(ITimeFactory::class); + $this->logger = $this->createMock(ILogger::class); + + $this->sync = new MailboxSync( + $this->mailboxMapper, + $this->folderMapper, + $this->mailAccountMapper, + $this->imapClientFactory, + $this->timeFactory, + $this->logger + ); + } + + public function testSyncSkipped() { + $account = $this->createMock(Account::class); + $mailAccount = new MailAccount(); + $mailAccount->setLastMailboxSync(100000 - 2000); + $account->method('getMailAccount')->willReturn($mailAccount); + $this->timeFactory->method('getTime')->willReturn(100000); + $this->imapClientFactory->expects($this->never()) + ->method('getClient'); + + $this->sync->sync($account); + } + + public function testSync() { + $account = $this->createMock(Account::class); + $mailAccount = new MailAccount(); + $mailAccount->setLastMailboxSync(0); + $account->method('getMailAccount')->willReturn($mailAccount); + $this->timeFactory->method('getTime')->willReturn(100000); + $client = $this->createMock(Horde_Imap_Client_Socket::class); + $this->imapClientFactory->expects($this->once()) + ->method('getClient') + ->with($account) + ->willReturn($client); + $folders = [ + $this->createMock(Folder::class), + $this->createMock(Folder::class), + ]; + $this->folderMapper->expects($this->once()) + ->method('getFolders') + ->with($account, $client) + ->willReturn($folders); + $this->folderMapper->expects($this->once()) + ->method('detectFolderSpecialUse') + ->with($folders) + ->willReturn($folders); + + $this->sync->sync($account); + } + +} diff --git a/tests/Integration/Service/FolderMapperIntegrationTest.php b/tests/Integration/Service/FolderMapperIntegrationTest.php index cfce6cf4c..d3d1a60c9 100644 --- a/tests/Integration/Service/FolderMapperIntegrationTest.php +++ b/tests/Integration/Service/FolderMapperIntegrationTest.php @@ -53,6 +53,7 @@ class FolderMapperIntegrationTest extends TestCase { public function testGetFolders() { $account = $this->createMock(Account::class); + $account->method('getId')->willReturn(13); $client = $this->getTestClient(); $folders = $this->mapper->getFolders($account, $client); diff --git a/tests/SearchFolderTest.php b/tests/SearchFolderTest.php index 2a7630a77..05b634228 100644 --- a/tests/SearchFolderTest.php +++ b/tests/SearchFolderTest.php @@ -1,4 +1,4 @@ -<?php +<?php declare(strict_types=1); /** * @author Christoph Wurst <christoph@winzerhof-wurst.at> @@ -29,8 +29,8 @@ use PHPUnit_Framework_MockObject_MockObject; class SearchFolderTest extends TestCase { - /** @var Account|PHPUnit_Framework_MockObject_MockObject */ - private $account; + /** @var int */ + private $accountId; /** @var Horde_Imap_Client_Mailbox|PHPUnit_Framework_MockObject_MockObject */ private $mailbox; @@ -41,10 +41,10 @@ class SearchFolderTest extends TestCase { protected function setUp() { parent::setUp(); - $this->account = $this->createMock(Account::class); + $this->accountId = 16; $this->mailbox = $this->createMock(Horde_Imap_Client_Mailbox::class); - $this->folder = new SearchFolder($this->account, $this->mailbox, [], ','); + $this->folder = new SearchFolder($this->accountId, $this->mailbox, [], ','); } public function testGetMailbox() { diff --git a/tests/Service/MailManagerTest.php b/tests/Service/MailManagerTest.php index 9d144a340..05615e099 100644 --- a/tests/Service/MailManagerTest.php +++ b/tests/Service/MailManagerTest.php @@ -1,4 +1,4 @@ -<?php +<?php declare(strict_types=1); /** * @author Christoph Wurst <christoph@winzerhof-wurst.at> @@ -24,10 +24,13 @@ namespace OCA\Mail\Tests\Service; use ChristophWurst\Nextcloud\Testing\TestCase; use Horde_Imap_Client_Socket; use OCA\Mail\Account; +use OCA\Mail\Db\Mailbox; +use OCA\Mail\Db\MailboxMapper; use OCA\Mail\Folder; use OCA\Mail\IMAP\FolderMapper; use OCA\Mail\IMAP\FolderStats; use OCA\Mail\IMAP\IMAPClientFactory; +use OCA\Mail\IMAP\MailboxSync; use OCA\Mail\IMAP\MessageMapper; use OCA\Mail\IMAP\Sync\Request; use OCA\Mail\IMAP\Sync\Response; @@ -40,6 +43,12 @@ class MailManagerTest extends TestCase { /** @var IMAPClientFactory|MockObject */ private $imapClientFactory; + /** @var MailboxMapper|MockObject */ + private $mailboxMapper; + + /** @var MailboxSync|MockObject */ + private $mailboxSync; + /** @var FolderMapper|MockObject */ private $folderMapper; @@ -56,12 +65,16 @@ class MailManagerTest extends TestCase { parent::setUp(); $this->imapClientFactory = $this->createMock(IMAPClientFactory::class); + $this->mailboxMapper = $this->createMock(MailboxMapper::class); + $this->mailboxSync = $this->createMock(MailboxSync::class); $this->folderMapper = $this->createMock(FolderMapper::class); $this->messageMapper = $this->createMock(MessageMapper::class); $this->sync = $this->createMock(Synchronizer::class); $this->manager = new MailManager( $this->imapClientFactory, + $this->mailboxMapper, + $this->mailboxSync, $this->folderMapper, $this->sync, $this->messageMapper @@ -69,27 +82,32 @@ class MailManagerTest extends TestCase { } public function testGetFolders() { - $client = $this->createMock(Horde_Imap_Client_Socket::class); + /** @var Account|MockObject $account */ $account = $this->createMock(Account::class); - $this->imapClientFactory->expects($this->once()) - ->method('getClient') - ->willReturn($client); + $mailboxes = [ + $this->createMock(Mailbox::class), + $this->createMock(Mailbox::class), + ]; $folders = [ $this->createMock(Folder::class), $this->createMock(Folder::class), ]; - $this->folderMapper->expects($this->once()) - ->method('getFolders') - ->with($this->equalTo($account), $this->equalTo($client)) - ->willReturn($folders); - $this->folderMapper->expects($this->once()) - ->method('getFoldersStatus') - ->with($this->equalTo($folders)); + $mailboxes[0]->method('toFolder')->willReturn($folders[0]); + $mailboxes[1]->method('toFolder')->willReturn($folders[1]); + $this->mailboxSync->expects($this->once()) + ->method('sync') + ->with($this->equalTo($account)); + $this->mailboxMapper->expects($this->once()) + ->method('findAll') + ->with($this->equalTo($account)) + ->willReturn($mailboxes); $this->folderMapper->expects($this->once()) ->method('detectFolderSpecialUse') ->with($this->equalTo($folders)); - $this->manager->getFolders($account); + $result = $this->manager->getFolders($account); + + $this->assertSame($folders, $result); } public function testCreateFolder() { |