diff options
author | Anna Larch <anna@nextcloud.com> | 2022-02-11 12:41:59 +0300 |
---|---|---|
committer | Anna Larch <anna@nextcloud.com> | 2022-03-18 11:12:17 +0300 |
commit | bd428bb32b1f35c4accbc6d44de768a5c5e384cb (patch) | |
tree | 2bd33911b013d7ab5ddf12f1665876a9938fd682 /tests | |
parent | 643aedb56ae9c8b00c2e8bea04ea678106e6f132 (diff) |
DB for outbox
Signed-off-by: Anna Larch <anna@nextcloud.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Integration/Db/LocalMessageMapperTest.php | 173 | ||||
-rw-r--r-- | tests/Integration/Db/LocalMessageTest.php | 66 | ||||
-rw-r--r-- | tests/Integration/Db/RecipientMapperTest.php | 170 | ||||
-rw-r--r-- | tests/Integration/Db/RecipientTest.php | 50 |
4 files changed, 459 insertions, 0 deletions
diff --git a/tests/Integration/Db/LocalMessageMapperTest.php b/tests/Integration/Db/LocalMessageMapperTest.php new file mode 100644 index 000000000..a63a10490 --- /dev/null +++ b/tests/Integration/Db/LocalMessageMapperTest.php @@ -0,0 +1,173 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright 2022 Anna Larch <anna.larch@gmx.net> + * + * @author 2022 Anna Larch <anna.larch@gmx.net> + * + * @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\Db; + +use ChristophWurst\Nextcloud\Testing\TestCase; +use OCA\Mail\Db\LocalAttachmentMapper; +use OCA\Mail\Db\LocalMessage; +use OCA\Mail\Db\LocalMessageMapper; +use OCA\Mail\Db\MailAccount; +use OCA\Mail\Db\Recipient; +use OCA\Mail\Db\RecipientMapper; +use OCA\Mail\Tests\Integration\Framework\ImapTestAccount; +use OCP\AppFramework\Db\DoesNotExistException; +use OCP\AppFramework\Utility\ITimeFactory; +use OCP\IDBConnection; +use PHPUnit\Framework\MockObject\MockObject; + +class LocalMessageMapperTest extends TestCase { + use ImapTestAccount; + + /** @var IDBConnection */ + private $db; + + /** @var LocalMessageMapper */ + private $mapper; + + /** @var ITimeFactory| MockObject */ + private $timeFactory; + + /** @var LocalMessage */ + private $entity; + + /** @var MailAccount */ + private $account; + + protected function setUp(): void { + parent::setUp(); + + $this->db = \OC::$server->getDatabaseConnection(); + $recipientMapper = new RecipientMapper( + $this->db + ); + $this->mapper = new LocalMessageMapper( + $this->db, + $this->createMock(LocalAttachmentMapper::class), + $recipientMapper + ); + + $qb = $this->db->getQueryBuilder(); + $delete = $qb->delete($this->mapper->getTableName()); + $delete->execute(); + + $this->account = $this->createTestAccount(); + + $message = new LocalMessage(); + $message->setType(LocalMessage::TYPE_OUTGOING); + $message->setAccountId($this->account->getId()); + $message->setAliasId(2); + $message->setSendAt(123); + $message->setSubject('subject'); + $message->setBody('message'); + $message->setHtml(true); + $message->setInReplyToMessageId('abc'); + $this->entity = $this->mapper->insert($message); + } + + public function testFindAllForUser(): void { + $result = $this->mapper->getAllForUser($this->getTestAccountUserId()); + + $this->assertCount(1, $result); + $row = $result[0]; + $this->assertEquals(LocalMessage::TYPE_OUTGOING, $row->getType()); + $this->assertEquals(2, $row->getAliasId()); + $this->assertEquals($this->account->getId(), $row->getAccountId()); + $this->assertEquals('subject', $row->getSubject()); + $this->assertEquals('message', $row->getBody()); + $this->assertEquals('abc', $row->getInReplyToMessageId()); + $this->assertTrue($row->isHtml()); + $this->assertEmpty($row->getAttachments()); + $this->assertEmpty($row->getRecipients()); + } + + /** + * @depends testFindAllForUser + */ + public function testFindById(): void { + $row = $this->mapper->findById($this->entity->getId(), $this->account->getUserId()); + + $this->assertEquals(LocalMessage::TYPE_OUTGOING, $row->getType()); + $this->assertEquals(2, $row->getAliasId()); + $this->assertEquals($this->account->getId(), $row->getAccountId()); + $this->assertEquals('subject', $row->getSubject()); + $this->assertEquals('message', $row->getBody()); + $this->assertEquals('abc', $row->getInReplyToMessageId()); + $this->assertTrue($row->isHtml()); + $this->assertEmpty($row->getAttachments()); + $this->assertEmpty($row->getRecipients()); + } + + public function testFindByIdNotFound(): void { + $this->expectException(DoesNotExistException::class); + $this->mapper->findById(1337, $this->account->getUserId()); + } + + /** + * @depends testFindById + */ + public function testDeleteWithRelated(): void { + $this->mapper->deleteWithRelated($this->entity); + + $result = $this->mapper->getAllForUser($this->getTestAccountUserId()); + + $this->assertEmpty($result); + } + + public function testSaveWithRelatedData(): void { + // cleanup + $qb = $this->db->getQueryBuilder(); + $delete = $qb->delete($this->mapper->getTableName()); + $delete->execute(); + + $message = new LocalMessage(); + $message->setType(LocalMessage::TYPE_OUTGOING); + $message->setAccountId($this->account->getId()); + $message->setAliasId(3); + $message->setSendAt(3); + $message->setSubject('savedWithRelated'); + $message->setBody('message'); + $message->setHtml(true); + $message->setInReplyToMessageId('abcdefg'); + $recipient = new Recipient(); + $recipient->setEmail('wizard@stardew-valley.com'); + $recipient->setLabel('M. Rasmodeus'); + $to = [$recipient]; + + $this->mapper->saveWithRelatedData($message, $to, [], []); + + $results = $this->mapper->getAllForUser($this->account->getUserId()); + $row = $results[0]; + $this->assertEquals(LocalMessage::TYPE_OUTGOING, $row->getType()); + $this->assertEquals(3, $row->getAliasId()); + $this->assertEquals($this->account->getId(), $row->getAccountId()); + $this->assertEquals('savedWithRelated', $row->getSubject()); + $this->assertEquals('message', $row->getBody()); + $this->assertEquals('abcdefg', $row->getInReplyToMessageId()); + $this->assertTrue($row->isHtml()); + $this->assertEmpty($row->getAttachments()); + $this->assertCount(1, $row->getRecipients()); + } +} diff --git a/tests/Integration/Db/LocalMessageTest.php b/tests/Integration/Db/LocalMessageTest.php new file mode 100644 index 000000000..449aa56d5 --- /dev/null +++ b/tests/Integration/Db/LocalMessageTest.php @@ -0,0 +1,66 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright 2022 Anna Larch <anna.larch@gmx.net> + * + * @author 2022 Anna Larch <anna.larch@gmx.net> + * + * @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\Db; + +use ChristophWurst\Nextcloud\Testing\TestCase; +use OCA\Mail\Db\LocalMessage; +use OCP\AppFramework\Utility\ITimeFactory; +use PHPUnit\Framework\MockObject\MockObject; + +class LocalMessageTest extends TestCase { + + /** @var ITimeFactory|MockObject */ + private $timeFactory; + + protected function setUp(): void { + $this->timeFactory = $this->createMock(ITimeFactory::class); + } + + public function testGettersSetters(): void { + $time = $this->timeFactory->getTime(); + $message = new LocalMessage(); + + $message->setType(LocalMessage::TYPE_OUTGOING); + $message->setAccountId(1); + $message->setAliasId(2); + $message->setSendAt($time); + $message->setSubject('subject'); + $message->setBody('message'); + $message->setHtml(true); + $message->setInReplyToMessageId('<abcdefg@12345678.com>'); + + $this->assertEquals(LocalMessage::TYPE_OUTGOING, $message->getType()); + $this->assertEquals(1, $message->getAccountId()); + $this->assertEquals(2, $message->getAliasId()); + $this->assertEquals($time, $message->getSendAt()); + $this->assertEquals('subject', $message->getSubject()); + $this->assertEquals('message', $message->getBody()); + $this->assertTrue($message->isHtml()); + $this->assertEquals('<abcdefg@12345678.com>', $message->getInReplyToMessageId()); + $this->assertNull($message->getAttachments()); + $this->assertNull($message->getRecipients()); + } +} diff --git a/tests/Integration/Db/RecipientMapperTest.php b/tests/Integration/Db/RecipientMapperTest.php new file mode 100644 index 000000000..e9e4e5e5b --- /dev/null +++ b/tests/Integration/Db/RecipientMapperTest.php @@ -0,0 +1,170 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright 2022 Anna Larch <anna.larch@gmx.net> + * + * @author 2022 Anna Larch <anna.larch@gmx.net> + * + * @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\Db; + +use ChristophWurst\Nextcloud\Testing\TestCase; +use OCA\Mail\Db\LocalAttachmentMapper; +use OCA\Mail\Db\LocalMessage; +use OCA\Mail\Db\LocalMessageMapper; +use OCA\Mail\Db\Recipient; +use OCA\Mail\Db\RecipientMapper; +use OCA\Mail\Tests\Integration\Framework\ImapTestAccount; +use OCP\AppFramework\Utility\ITimeFactory; +use OCP\IDBConnection; +use PHPUnit\Framework\MockObject\MockObject; + +class RecipientMapperTest extends TestCase { + use ImapTestAccount; + + /** @var IDBConnection */ + private $db; + + /** @var RecipientMapper */ + private $mapper; + + /** @var ITimeFactory| MockObject */ + private $timeFactory; + + /** @var Recipient */ + private $inboxRecipient; + + /** @var Recipient */ + private $outboxRecipient; + + /** @var LocalMessage */ + private $message; + + protected function setUp(): void { + parent::setUp(); + + $this->db = \OC::$server->getDatabaseConnection(); + $this->mapper = new RecipientMapper( + $this->db + ); + $this->localMessageMapper = new LocalMessageMapper( + $this->db, + $this->createMock(LocalAttachmentMapper::class), + $this->createMock(RecipientMapper::class) + ); + + $qb = $this->db->getQueryBuilder(); + + $delete = $qb->delete($this->mapper->getTableName()); + $delete->execute(); + + $qb = $this->db->getQueryBuilder(); + + $delete = $qb->delete($this->localMessageMapper->getTableName()); + $delete->execute(); + + $message = new LocalMessage(); + $message->setType(LocalMessage::TYPE_OUTGOING); + $message->setAccountId(1); + $message->setAliasId(2); + $message->setSendAt(123); + $message->setSubject('subject'); + $message->setBody('message'); + $message->setHtml(true); + $message->setInReplyToMessageId('abcd'); + $this->message = $this->localMessageMapper->insert($message); + + $this->outboxRecipient = new Recipient(); + $this->outboxRecipient->setLocalMessageId($this->message->getId()); + $this->outboxRecipient->setEmail('doc@stardew-clinic.com'); + $this->outboxRecipient->setType(Recipient::TYPE_TO); + $this->outboxRecipient->setLabel('Dr. Harvey'); + $this->mapper->insert($this->outboxRecipient); + + $inboxRecipientTwo = new Recipient(); + $inboxRecipientTwo->setLocalMessageId($this->message->getId()); + $inboxRecipientTwo->setEmail('pierre@stardewvalley.com'); + $inboxRecipientTwo->setType(Recipient::TYPE_CC); + $inboxRecipientTwo->setLabel("Pierre's General Store"); + $this->mapper->insert($inboxRecipientTwo); + } + + public function testFindRecipients(): void { + $result = $this->mapper->findByLocalMessageId($this->message->getId()); + $this->assertCount(2, $result); + } + + /** + * @depends testFindRecipients + */ + public function testFindAllRecipients(): void { + $result = $this->mapper->findByLocalMessageIds([$this->message->getId(),789,789]); + $this->assertCount(2, $result); + } + + /** + * @depends testFindAllRecipients + */ + public function testFindAllRecipientsEmpty(): void { + $result = $this->mapper->findByLocalMessageIds([12,57842]); + $this->assertEmpty($result); + } + + /** + * @depends testFindAllRecipientsEmpty + */ + public function testDeleteForLocalMailbox(): void { + $this->mapper->deleteForLocalMailbox($this->message->getId()); + $result = $this->mapper->findByLocalMessageId($this->message->getId()); + $this->assertEmpty($result); + } + + /** + * @depends testDeleteForLocalMailbox + */ + public function testSaveRecipients(): void { + $message = new LocalMessage(); + $message->setType(LocalMessage::TYPE_OUTGOING); + $message->setAccountId(1); + $message->setAliasId(2); + $message->setSendAt(123); + $message->setSubject('subject'); + $message->setBody('message'); + $message->setHtml(true); + $message->setInReplyToMessageId('abcd'); + $message = $this->localMessageMapper->insert($message); + + $recipient = new Recipient(); + $recipient->setEmail('penny@stardewvalleylibrary.edu'); + $recipient->setLabel('Penny'); + $this->mapper->saveRecipients($message->getId(), [$recipient], Recipient::TYPE_FROM); + + $results = $this->mapper->findByLocalMessageId($message->getId()); + $this->assertCount(1, $results); + + /** @var Recipient $entity */ + $entity = $results[0]; + $this->assertEquals($message->getId(), $entity->getLocalMessageId()); + $this->assertNull($entity->getMessageId()); + $this->assertEquals(Recipient::TYPE_FROM, $entity->getType()); + $this->assertEquals('Penny', $entity->getLabel()); + $this->assertEquals('penny@stardewvalleylibrary.edu', $entity->getEmail()); + } +} diff --git a/tests/Integration/Db/RecipientTest.php b/tests/Integration/Db/RecipientTest.php new file mode 100644 index 000000000..9eff50dbd --- /dev/null +++ b/tests/Integration/Db/RecipientTest.php @@ -0,0 +1,50 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright 2022 Anna Larch <anna.larch@gmx.net> + * + * @author 2022 Anna Larch <anna.larch@gmx.net> + * + * @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\Db; + +use ChristophWurst\Nextcloud\Testing\TestCase; +use OCA\Mail\Db\Recipient; + +class RecipientTest extends TestCase { + protected function setUp(): void { + } + + public function testGettersSetters(): void { + $recipient = new Recipient(); + $recipient->setMessageId(1); + $recipient->setLocalMessageId(100); + $recipient->setType(Recipient::TYPE_TO); + $recipient->setLabel('Penny'); + $recipient->setEmail('penny@stardew-library.edu'); + + + $this->assertEquals(1, $recipient->getMessageId()); + $this->assertEquals(100, $recipient->getLocalMessageId()); + $this->assertEquals(Recipient::TYPE_TO, $recipient->getType()); + $this->assertEquals('Penny', $recipient->getLabel()); + $this->assertEquals('penny@stardew-library.edu', $recipient->getEmail()); + } +} |