diff options
Diffstat (limited to 'tests/lib/Mail')
-rw-r--r-- | tests/lib/Mail/EMailTemplateTest.php | 56 | ||||
-rw-r--r-- | tests/lib/Mail/MailerTest.php | 104 | ||||
-rw-r--r-- | tests/lib/Mail/MessageTest.php | 59 | ||||
-rw-r--r-- | tests/lib/Mail/Provider/AddressTest.php | 46 | ||||
-rw-r--r-- | tests/lib/Mail/Provider/AttachmentTest.php | 71 | ||||
-rw-r--r-- | tests/lib/Mail/Provider/ManagerTest.php | 189 | ||||
-rw-r--r-- | tests/lib/Mail/Provider/MessageTest.php | 163 |
7 files changed, 588 insertions, 100 deletions
diff --git a/tests/lib/Mail/EMailTemplateTest.php b/tests/lib/Mail/EMailTemplateTest.php index 284c53e73b8..1912bf060e2 100644 --- a/tests/lib/Mail/EMailTemplateTest.php +++ b/tests/lib/Mail/EMailTemplateTest.php @@ -1,24 +1,8 @@ <?php + /** - * @copyright 2017, Morris Jobke <hey@morrisjobke.de> - * - * @author Morris Jobke <hey@morrisjobke.de> - * - * @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/>. - * + * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\Mail; @@ -55,14 +39,16 @@ class EMailTemplateTest extends TestCase { $this->defaults, $this->urlGenerator, $this->l10n, + 252, + 120, 'test.TestTemplate', [] ); } - public function testEMailTemplateCustomFooter() { + public function testEMailTemplateCustomFooter(): void { $this->defaults - ->expects($this->any()) + ->expects($this->atLeastOnce()) ->method('getDefaultColorPrimary') ->willReturn('#0082c9'); $this->defaults @@ -74,8 +60,8 @@ class EMailTemplateTest extends TestCase { ->method('getName') ->willReturn('TestCloud'); $this->defaults - ->expects($this->any()) - ->method('getTextColorPrimary') + ->expects($this->atLeastOnce()) + ->method('getDefaultTextColorPrimary') ->willReturn('#ffffff'); $this->urlGenerator ->expects($this->once()) @@ -101,9 +87,9 @@ class EMailTemplateTest extends TestCase { $this->assertSame($expectedTXT, $this->emailTemplate->renderText()); } - public function testEMailTemplateDefaultFooter() { + public function testEMailTemplateDefaultFooter(): void { $this->defaults - ->expects($this->any()) + ->expects($this->atLeastOnce()) ->method('getDefaultColorPrimary') ->willReturn('#0082c9'); $this->defaults @@ -119,8 +105,8 @@ class EMailTemplateTest extends TestCase { ->method('getLogo') ->willReturn('/img/logo-mail-header.png'); $this->defaults - ->expects($this->any()) - ->method('getTextColorPrimary') + ->expects($this->atLeastOnce()) + ->method('getDefaultTextColorPrimary') ->willReturn('#ffffff'); $this->urlGenerator ->expects($this->once()) @@ -144,9 +130,9 @@ class EMailTemplateTest extends TestCase { $this->assertSame($expectedTXT, $this->emailTemplate->renderText()); } - public function testEMailTemplateSingleButton() { + public function testEMailTemplateSingleButton(): void { $this->defaults - ->expects($this->any()) + ->expects($this->atLeastOnce()) ->method('getDefaultColorPrimary') ->willReturn('#0082c9'); $this->defaults @@ -162,8 +148,8 @@ class EMailTemplateTest extends TestCase { ->method('getLogo') ->willReturn('/img/logo-mail-header.png'); $this->defaults - ->expects($this->any()) - ->method('getTextColorPrimary') + ->expects($this->atLeastOnce()) + ->method('getDefaultTextColorPrimary') ->willReturn('#ffffff'); $this->urlGenerator ->expects($this->once()) @@ -189,9 +175,9 @@ class EMailTemplateTest extends TestCase { - public function testEMailTemplateAlternativePlainTexts() { + public function testEMailTemplateAlternativePlainTexts(): void { $this->defaults - ->expects($this->any()) + ->expects($this->atLeastOnce()) ->method('getDefaultColorPrimary') ->willReturn('#0082c9'); $this->defaults @@ -207,8 +193,8 @@ class EMailTemplateTest extends TestCase { ->method('getLogo') ->willReturn('/img/logo-mail-header.png'); $this->defaults - ->expects($this->any()) - ->method('getTextColorPrimary') + ->expects($this->atLeastOnce()) + ->method('getDefaultTextColorPrimary') ->willReturn('#ffffff'); $this->urlGenerator ->expects($this->once()) diff --git a/tests/lib/Mail/MailerTest.php b/tests/lib/Mail/MailerTest.php index 0eee7db126a..7b911e5c4e2 100644 --- a/tests/lib/Mail/MailerTest.php +++ b/tests/lib/Mail/MailerTest.php @@ -1,12 +1,9 @@ <?php + /** - * Copyright (c) 2014-2015 Lukas Reschke <lukas@owncloud.com> - * - * @author Arne Hamann <github@arne.email> - * - * This file is licensed under the Affero General Public License version 3 or - * later. - * See the COPYING-README file. + * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\Mail; @@ -16,11 +13,13 @@ use OC\Mail\Mailer; use OC\Mail\Message; use OCP\Defaults; use OCP\EventDispatcher\IEventDispatcher; +use OCP\IBinaryFinder; use OCP\IConfig; use OCP\IL10N; use OCP\IURLGenerator; use OCP\L10N\IFactory; use OCP\Mail\Events\BeforeMessageSent; +use OCP\Server; use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; use Symfony\Component\Mailer\Mailer as SymfonyMailer; @@ -42,7 +41,7 @@ class MailerTest extends TestCase { private $l10n; /** @var Mailer */ private $mailer; - /** @var IEventDispatcher */ + /** @var IEventDispatcher&MockObject */ private $dispatcher; @@ -69,7 +68,7 @@ class MailerTest extends TestCase { /** * @return array */ - public function sendmailModeProvider(): array { + public static function sendmailModeProvider(): array { return [ 'smtp' => ['smtp', ' -bs'], 'pipe' => ['pipe', ' -t -i'], @@ -77,11 +76,11 @@ class MailerTest extends TestCase { } /** - * @dataProvider sendmailModeProvider * @param $sendmailMode * @param $binaryParam */ - public function testGetSendmailInstanceSendMail($sendmailMode, $binaryParam) { + #[\PHPUnit\Framework\Attributes\DataProvider('sendmailModeProvider')] + public function testGetSendmailInstanceSendMail($sendmailMode, $binaryParam): void { $this->config ->expects($this->exactly(2)) ->method('getSystemValueString') @@ -90,7 +89,7 @@ class MailerTest extends TestCase { ['mail_sendmailmode', 'smtp', $sendmailMode], ]); - $path = \OC_Helper::findBinaryPath('sendmail'); + $path = Server::get(IBinaryFinder::class)->findBinaryPath('sendmail'); if ($path === false) { $path = '/usr/sbin/sendmail'; } @@ -100,11 +99,11 @@ class MailerTest extends TestCase { } /** - * @dataProvider sendmailModeProvider * @param $sendmailMode * @param $binaryParam */ - public function testGetSendmailInstanceSendMailQmail($sendmailMode, $binaryParam) { + #[\PHPUnit\Framework\Attributes\DataProvider('sendmailModeProvider')] + public function testGetSendmailInstanceSendMailQmail($sendmailMode, $binaryParam): void { $this->config ->expects($this->exactly(2)) ->method('getSystemValueString') @@ -117,7 +116,27 @@ class MailerTest extends TestCase { $this->assertEquals($sendmail, self::invokePrivate($this->mailer, 'getSendMailInstance')); } - public function testGetInstanceDefault() { + public function testEventForNullTransport(): void { + $this->config + ->expects($this->exactly(1)) + ->method('getSystemValueString') + ->with('mail_smtpmode', 'smtp') + ->willReturn('null'); + + $message = $this->createMock(Message::class); + $message->expects($this->once()) + ->method('getSymfonyEmail') + ->willReturn((new Email())->to('foo@bar.com')->from('bar@foo.com')->text('')); + + $event = new BeforeMessageSent($message); + $this->dispatcher->expects($this->once()) + ->method('dispatchTyped') + ->with($this->equalTo($event)); + + $this->mailer->send($message); + } + + public function testGetInstanceDefault(): void { $this->config ->method('getSystemValue') ->willReturnMap([ @@ -131,7 +150,7 @@ class MailerTest extends TestCase { $this->assertInstanceOf(EsmtpTransport::class, $transport); } - public function testGetInstanceSendmail() { + public function testGetInstanceSendmail(): void { $this->config ->method('getSystemValueString') ->willReturnMap([ @@ -145,7 +164,7 @@ class MailerTest extends TestCase { $this->assertInstanceOf(SendmailTransport::class, $transport); } - public function testEvents() { + public function testEvents(): void { $this->config ->method('getSystemValue') ->willReturnMap([ @@ -153,7 +172,7 @@ class MailerTest extends TestCase { ['mail_smtpport', 25, 25], ]); $this->mailer = $this->getMockBuilder(Mailer::class) - ->setMethods(['getInstance']) + ->onlyMethods(['getInstance']) ->setConstructorArgs( [ $this->config, @@ -177,7 +196,7 @@ class MailerTest extends TestCase { $this->mailer->send($message); } - public function testCreateMessage() { + public function testCreateMessage(): void { $this->config ->expects($this->any()) ->method('getSystemValueBool') @@ -187,7 +206,7 @@ class MailerTest extends TestCase { } - public function testSendInvalidMailException() { + public function testSendInvalidMailException(): void { $this->config ->method('getSystemValue') ->willReturnMap([ @@ -197,6 +216,7 @@ class MailerTest extends TestCase { ]); $this->expectException(\Exception::class); + /** @var Message&MockObject */ $message = $this->getMockBuilder('\OC\Mail\Message') ->disableOriginalConstructor()->getMock(); $message->expects($this->once()) @@ -209,34 +229,42 @@ class MailerTest extends TestCase { /** * @return array */ - public function mailAddressProvider() { + public static function mailAddressProvider(): array { return [ - ['lukas@owncloud.com', true], - ['lukas@localhost', true], - ['lukas@192.168.1.1', true], - ['lukas@éxämplè.com', true], - ['asdf', false], - ['', false], - ['lukas@owncloud.org@owncloud.com', false], + ['lukas@owncloud.com', true, false], + ['lukas@localhost', true, false], + ['lukas@192.168.1.1', true, false], + ['lukas@éxämplè.com', true, false], + ['asdf', false, false], + ['', false, false], + ['lukas@owncloud.org@owncloud.com', false, false], + ['test@localhost', true, false], + ['test@localhost', false, true], ]; } - /** - * @dataProvider mailAddressProvider - */ - public function testValidateMailAddress($email, $expected) { + #[\PHPUnit\Framework\Attributes\DataProvider('mailAddressProvider')] + public function testValidateMailAddress($email, $expected, $strict): void { + $this->config + ->expects($this->atMost(1)) + ->method('getAppValue') + ->with('core', 'enforce_strict_email_check') + ->willReturn($strict ? 'yes' : 'no'); $this->assertSame($expected, $this->mailer->validateMailAddress($email)); } - public function testCreateEMailTemplate() { + public function testCreateEMailTemplate(): void { $this->config->method('getSystemValueString') ->with('mail_template_class', '') ->willReturnArgument(1); + $this->config->method('getAppValue') + ->with('theming', 'logoDimensions', Mailer::DEFAULT_DIMENSIONS) + ->willReturn(Mailer::DEFAULT_DIMENSIONS); $this->assertSame(EMailTemplate::class, get_class($this->mailer->createEMailTemplate('tests.MailerTest'))); } - public function testStreamingOptions() { + public function testStreamingOptions(): void { $this->config->method('getSystemValue') ->willReturnMap([ ['mail_smtpstreamoptions', [], ['foo' => 1]], @@ -260,7 +288,7 @@ class MailerTest extends TestCase { $this->assertTrue(isset($transport->getStream()->getStreamOptions()['foo'])); } - public function testStreamingOptionsWrongType() { + public function testStreamingOptionsWrongType(): void { $this->config->method('getSystemValue') ->willReturnMap([ ['mail_smtpstreamoptions', [], 'bar'], @@ -329,4 +357,10 @@ class MailerTest extends TestCase { self::assertInstanceOf(EsmtpTransport::class, $transport); self::assertEquals('[127.0.0.1]', $transport->getLocalDomain()); } + + public function testCaching(): void { + $symfonyMailer1 = self::invokePrivate($this->mailer, 'getInstance'); + $symfonyMailer2 = self::invokePrivate($this->mailer, 'getInstance'); + self::assertSame($symfonyMailer1, $symfonyMailer2); + } } diff --git a/tests/lib/Mail/MessageTest.php b/tests/lib/Mail/MessageTest.php index 1d309ca742d..18ef9793b5b 100644 --- a/tests/lib/Mail/MessageTest.php +++ b/tests/lib/Mail/MessageTest.php @@ -1,9 +1,9 @@ <?php + /** - * Copyright (c) 2014 Lukas Reschke <lukas@owncloud.com> - * This file is licensed under the Affero General Public License version 3 or - * later. - * See the COPYING-README file. + * SPDX-FileCopyrightText: 2018-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\Mail; @@ -28,7 +28,7 @@ class MessageTest extends TestCase { /** * @return array */ - public function mailAddressProvider() { + public static function mailAddressProvider(): array { return [ [ ['lukas@owncloud.com' => 'Lukas Reschke'], @@ -66,19 +66,18 @@ class MessageTest extends TestCase { protected function setUp(): void { parent::setUp(); - $this->symfonyEmail = $this->getMockBuilder(Email::class) - ->disableOriginalConstructor()->getMock(); + $this->symfonyEmail = $this->createMock(Email::class); $this->message = new Message($this->symfonyEmail, false); } /** - * @dataProvider mailAddressProvider * * @param string $unconverted * @param string $expected */ - public function testConvertAddresses($unconverted, $expected) { + #[\PHPUnit\Framework\Attributes\DataProvider('mailAddressProvider')] + public function testConvertAddresses($unconverted, $expected): void { $this->assertEquals($expected, self::invokePrivate($this->message, 'convertAddresses', [$unconverted])); } @@ -92,28 +91,28 @@ class MessageTest extends TestCase { $this->symfonyEmail ->expects($this->once()) ->method('from') - ->willReturn(new Address('pierres-general-store@stardewvalley.com', 'Pierres General Store')); + ->with(new Address('pierres-general-store@stardewvalley.com', 'Pierres General Store')); $this->symfonyEmail ->expects($this->once()) ->method('to') - ->willReturn(new Address('lewis-tent@stardewvalley.com', "Lewis' Tent Life")); + ->with(new Address('lewis-tent@stardewvalley.com', "Lewis' Tent Life")); $this->symfonyEmail ->expects($this->once()) ->method('replyTo') - ->willReturn(new Address('penny@stardewvalley-library.co.edu', 'Penny')); + ->with(new Address('penny@stardewvalley-library.co.edu', 'Penny')); $this->symfonyEmail ->expects($this->once()) ->method('cc') - ->willReturn(new Address('gunther@stardewvalley-library.co.edu', 'Gunther')); + ->with(new Address('gunther@stardewvalley-library.co.edu', 'Gunther')); $this->symfonyEmail ->expects($this->once()) ->method('bcc') - ->willReturn(new Address('pam@stardewvalley-bus.com', 'Pam')); + ->with(new Address('pam@stardewvalley-bus.com', 'Pam')); $this->message->setRecipients(); } - public function testSetTo() { + public function testSetTo(): void { $expected = ['pierres-general-store@stardewvalley.com' => 'Pierres General Store']; $message = $this->message->setTo(['pierres-general-store@stardewvalley.com' => 'Pierres General Store']); @@ -142,7 +141,7 @@ class MessageTest extends TestCase { $message->setRecipients(); } - public function testSetGetFrom() { + public function testSetGetFrom(): void { $expected = ['pierres-general-store@stardewvalley.com' => 'Pierres General Store']; $message = $this->message->setFrom(['pierres-general-store@stardewvalley.com' => 'Pierres General Store']); @@ -150,7 +149,7 @@ class MessageTest extends TestCase { $this->assertEquals($expected, $message->getFrom()); } - public function testSetGetTo() { + public function testSetGetTo(): void { $expected = ['lewis-tent@stardewvalley.com' => "Lewis' Tent Life"]; $message = $this->message->setTo(['lewis-tent@stardewvalley.com' => "Lewis' Tent Life"]); @@ -158,7 +157,7 @@ class MessageTest extends TestCase { $this->assertEquals($expected, $message->getTo()); } - public function testSetGetReplyTo() { + public function testSetGetReplyTo(): void { $expected = ['penny@stardewvalley-library.co.edu' => 'Penny']; $message = $this->message->setReplyTo(['penny@stardewvalley-library.co.edu' => 'Penny']); @@ -166,7 +165,7 @@ class MessageTest extends TestCase { $this->assertEquals($expected, $message->getReplyTo()); } - public function testSetGetCC() { + public function testSetGetCC(): void { $expected = ['gunther@stardewvalley-library.co.edu' => 'Gunther']; $message = $this->message->setCc(['gunther@stardewvalley-library.co.edu' => 'Gunther']); @@ -174,7 +173,7 @@ class MessageTest extends TestCase { $this->assertEquals($expected, $message->getCc()); } - public function testSetGetBCC() { + public function testSetGetBCC(): void { $expected = ['pam@stardewvalley-bus.com' => 'Pam']; $message = $this->message->setBcc(['pam@stardewvalley-bus.com' => 'Pam']); @@ -182,7 +181,7 @@ class MessageTest extends TestCase { $this->assertEquals($expected, $message->getBcc()); } - public function testSetPlainBody() { + public function testSetPlainBody(): void { $this->symfonyEmail ->expects($this->once()) ->method('text') @@ -191,7 +190,7 @@ class MessageTest extends TestCase { $this->message->setPlainBody('Fancy Body'); } - public function testGetPlainBody() { + public function testGetPlainBody(): void { $this->symfonyEmail ->expects($this->once()) ->method('getTextBody') @@ -200,7 +199,7 @@ class MessageTest extends TestCase { $this->assertSame('Fancy Body', $this->message->getPlainBody()); } - public function testSetHtmlBody() { + public function testSetHtmlBody(): void { $this->symfonyEmail ->expects($this->once()) ->method('html') @@ -209,7 +208,7 @@ class MessageTest extends TestCase { $this->message->setHtmlBody('<blink>Fancy Body</blink>'); } - public function testPlainTextRenderOption() { + public function testPlainTextRenderOption(): void { /** @var MockObject|Email $symfonyEmail */ $symfonyEmail = $this->getMockBuilder(Email::class) ->disableOriginalConstructor()->getMock(); @@ -232,7 +231,7 @@ class MessageTest extends TestCase { $message->useTemplate($template); } - public function testBothRenderingOptions() { + public function testBothRenderingOptions(): void { /** @var MockObject|Email $symfonyEmail */ $symfonyEmail = $this->getMockBuilder(Email::class) ->disableOriginalConstructor()->getMock(); @@ -255,9 +254,9 @@ class MessageTest extends TestCase { $message->useTemplate($template); } - public function testSetAutoSubmitted1() { + public function testSetAutoSubmitted1(): void { $headers = new Headers($this->createMock(HeaderInterface::class)); - $headers->addTextHeader(AutoSubmitted::HEADER, "yes"); + $headers->addTextHeader(AutoSubmitted::HEADER, 'yes'); $symfonyEmail = $this->createMock(Email::class); $symfonyEmail->method('getHeaders') @@ -268,7 +267,7 @@ class MessageTest extends TestCase { $this->assertNotSame('no', $message->getAutoSubmitted()); } - public function testSetAutoSubmitted2() { + public function testSetAutoSubmitted2(): void { $headers = new Headers($this->createMock(HeaderInterface::class)); $headers->addTextHeader(AutoSubmitted::HEADER, 'no'); $symfonyEmail = $this->createMock(Email::class); @@ -281,7 +280,7 @@ class MessageTest extends TestCase { $this->assertSame('auto-generated', $message->getAutoSubmitted()); } - public function testGetAutoSubmitted() { + public function testGetAutoSubmitted(): void { $headers = new Headers($this->createMock(HeaderInterface::class)); $headers->addTextHeader(AutoSubmitted::HEADER, 'no'); $symfonyEmail = $this->createMock(Email::class); @@ -290,6 +289,6 @@ class MessageTest extends TestCase { ->willReturn($headers); $message = new Message($symfonyEmail, false); - $this->assertSame("no", $message->getAutoSubmitted()); + $this->assertSame('no', $message->getAutoSubmitted()); } } diff --git a/tests/lib/Mail/Provider/AddressTest.php b/tests/lib/Mail/Provider/AddressTest.php new file mode 100644 index 00000000000..dc5e73526eb --- /dev/null +++ b/tests/lib/Mail/Provider/AddressTest.php @@ -0,0 +1,46 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +namespace Test\Mail\Provider; + +use OCP\Mail\Provider\Address; +use Test\TestCase; + +class AddressTest extends TestCase { + + /** @var Address&MockObject */ + private Address $address; + + protected function setUp(): void { + parent::setUp(); + + $this->address = new Address('user1@testing.com', 'User One'); + + } + + public function testAddress(): void { + + // test set by constructor + $this->assertEquals('user1@testing.com', $this->address->getAddress()); + // test set by setter + $this->address->setAddress('user2@testing.com'); + $this->assertEquals('user2@testing.com', $this->address->getAddress()); + + } + + public function testLabel(): void { + + // test set by constructor + $this->assertEquals('User One', $this->address->getLabel()); + // test set by setter + $this->address->setLabel('User Two'); + $this->assertEquals('User Two', $this->address->getLabel()); + + } + +} diff --git a/tests/lib/Mail/Provider/AttachmentTest.php b/tests/lib/Mail/Provider/AttachmentTest.php new file mode 100644 index 00000000000..f02b1d54a7b --- /dev/null +++ b/tests/lib/Mail/Provider/AttachmentTest.php @@ -0,0 +1,71 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +namespace Test\Mail\Provider; + +use OCP\Mail\Provider\Attachment; +use Test\TestCase; + +class AttachmentTest extends TestCase { + + /** @var Attachment&MockObject */ + private Attachment $attachment; + + protected function setUp(): void { + parent::setUp(); + + $this->attachment = new Attachment( + 'This is the contents of a file', + 'example1.txt', + 'text/plain', + false + ); + + } + + public function testName(): void { + + // test set by constructor + $this->assertEquals('example1.txt', $this->attachment->getName()); + // test set by setter + $this->attachment->setName('example2.txt'); + $this->assertEquals('example2.txt', $this->attachment->getName()); + + } + + public function testType(): void { + + // test set by constructor + $this->assertEquals('text/plain', $this->attachment->getType()); + // test set by setter + $this->attachment->setType('text/html'); + $this->assertEquals('text/html', $this->attachment->getType()); + + } + + public function testContents(): void { + + // test set by constructor + $this->assertEquals('This is the contents of a file', $this->attachment->getContents()); + // test set by setter + $this->attachment->setContents('This is the modified contents of a file'); + $this->assertEquals('This is the modified contents of a file', $this->attachment->getContents()); + + } + + public function testEmbedded(): void { + + // test set by constructor + $this->assertEquals(false, $this->attachment->getEmbedded()); + // test set by setter + $this->attachment->setEmbedded(true); + $this->assertEquals(true, $this->attachment->getEmbedded()); + + } + +} diff --git a/tests/lib/Mail/Provider/ManagerTest.php b/tests/lib/Mail/Provider/ManagerTest.php new file mode 100644 index 00000000000..d3dabc0ea75 --- /dev/null +++ b/tests/lib/Mail/Provider/ManagerTest.php @@ -0,0 +1,189 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +namespace Test\Mail\Provider; + +use OC\AppFramework\Bootstrap\Coordinator; +use OC\AppFramework\Bootstrap\RegistrationContext; +use OC\AppFramework\Bootstrap\ServiceRegistration; +use OC\Mail\Provider\Manager; +use OCP\Mail\Provider\Address; +use OCP\Mail\Provider\IProvider; +use OCP\Mail\Provider\IService; +use Psr\Container\ContainerInterface; +use Psr\Log\LoggerInterface; +use Test\TestCase; + +class ManagerTest extends TestCase { + + /** @var Coordinator&MockObject */ + private Coordinator $coordinator; + /** @var ContainerInterface&MockObject */ + private ContainerInterface $container; + /** @var LoggerInterface&MockObject */ + private LoggerInterface $logger; + /** @var IProvider&MockObject */ + private IProvider $provider; + /** @var IService&MockObject */ + private IService $service; + + protected function setUp(): void { + parent::setUp(); + + $this->logger = $this->createMock(LoggerInterface::class); + + // construct service registration + $registration = $this->createMock(ServiceRegistration::class); + $registration + ->method('getService') + ->willReturn('Mock\Provider\MailProvider'); + // construct registration context + $context = $this->createMock(RegistrationContext::class); + $context + ->method('getMailProviders') + ->willReturn([$registration]); + // construct coordinator + $this->coordinator = $this->createMock(Coordinator::class); + $this->coordinator + ->method('getRegistrationContext') + ->willReturn($context); + + // construct mail service + $this->service = $this->createMock(IService::class); + $this->service + ->method('id') + ->willReturn('100'); + $this->service + ->method('getLabel') + ->willReturn('Mock Mail Service'); + $this->service + ->method('getPrimaryAddress') + ->willReturn((new Address('user1@testing.com', 'User One'))); + // construct mail provider + $this->provider = $this->createMock(IProvider::class); + $this->provider + ->method('id') + ->willReturn('mock-provider'); + $this->provider + ->method('label') + ->willReturn('Mock Provider'); + $this->provider + ->method('listServices') + ->willReturnMap([ + ['user0', []], + ['user1', [$this->service->id() => $this->service]] + ]); + $this->provider + ->method('findServiceById') + ->willReturnMap([ + ['user0', '100', null], + ['user1', '100', $this->service] + ]); + $this->provider + ->method('findServiceByAddress') + ->willReturnMap([ + ['user0', 'user0@testing.com', null], + ['user1', 'user1@testing.com', $this->service] + ]); + // construct container interface + $this->container = $this->createMock(ContainerInterface::class); + $this->container + ->method('get') + ->willReturnMap([ + ['Mock\Provider\MailProvider', $this->provider] + ]); + + } + + public function testHas(): void { + + // construct mail manager + $manager = new Manager($this->coordinator, $this->container, $this->logger); + // test result with providers found + $this->assertTrue($manager->has()); + + } + + public function testCount(): void { + + // construct mail manager + $manager = new Manager($this->coordinator, $this->container, $this->logger); + // test result with providers found + $this->assertGreaterThan(0, $manager->count()); + + } + + public function testTypes(): void { + + // construct mail manager + $manager = new Manager($this->coordinator, $this->container, $this->logger); + // test result with providers found + $this->assertEquals(['mock-provider' => 'Mock Provider'], $manager->types()); + + } + + public function testProviders(): void { + + // construct mail manager + $manager = new Manager($this->coordinator, $this->container, $this->logger); + // test result with providers found + $this->assertEquals([$this->provider->id() => $this->provider], $manager->providers()); + + } + + public function testFindProviderById(): void { + + // construct mail manager + $manager = new Manager($this->coordinator, $this->container, $this->logger); + // test result with providers found + $this->assertEquals($this->provider, $manager->findProviderById($this->provider->id())); + + } + + public function testServices(): void { + + // construct mail manager + $manager = new Manager($this->coordinator, $this->container, $this->logger); + // test result with no services found + $this->assertEquals([], $manager->services('user0')); + // test result with services found + $this->assertEquals([$this->provider->id() => [$this->service->id() => $this->service]], $manager->services('user1')); + + } + + public function testFindServiceById(): void { + + // construct mail manager + $manager = new Manager($this->coordinator, $this->container, $this->logger); + // test result with no services found and not provider specified + $this->assertEquals(null, $manager->findServiceById('user0', '100')); + // test result with no services found and provider specified + $this->assertEquals(null, $manager->findServiceById('user0', '100', $this->provider->id())); + // test result with services found and not provider specified + $this->assertEquals($this->service, $manager->findServiceById('user1', '100')); + // test result with services found and provider specified + $this->assertEquals($this->service, $manager->findServiceById('user1', '100', $this->provider->id())); + + } + + public function testFindServiceByAddress(): void { + + // construct mail manager + $manager = new Manager($this->coordinator, $this->container, $this->logger); + // test result with no services found and not provider specified + $this->assertEquals(null, $manager->findServiceByAddress('user0', 'user0@testing.com')); + // test result with no services found and provider specified + $this->assertEquals(null, $manager->findServiceByAddress('user0', 'user0@testing.com', $this->provider->id())); + // test result with services found and not provider specified + $this->assertEquals($this->service, $manager->findServiceByAddress('user1', 'user1@testing.com')); + // test result with services found and provider specified + $this->assertEquals($this->service, $manager->findServiceByAddress('user1', 'user1@testing.com', $this->provider->id())); + + } + +} diff --git a/tests/lib/Mail/Provider/MessageTest.php b/tests/lib/Mail/Provider/MessageTest.php new file mode 100644 index 00000000000..6b387e5b010 --- /dev/null +++ b/tests/lib/Mail/Provider/MessageTest.php @@ -0,0 +1,163 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +namespace Test\Mail\Provider; + +use OCP\Mail\Provider\Address; +use OCP\Mail\Provider\Attachment; +use OCP\Mail\Provider\Message; +use Test\TestCase; + +class MessageTest extends TestCase { + + /** @var Message&MockObject */ + private Message $message; + /** @var Address&MockObject */ + private Address $address1; + /** @var Address&MockObject */ + private Address $address2; + /** @var Attachment&MockObject */ + private Attachment $attachment1; + /** @var Attachment&MockObject */ + private Attachment $attachment2; + + protected function setUp(): void { + parent::setUp(); + + $this->message = new Message( + ['id' => 'cd02ea42-feac-4863-b9d8-484d16a587ea'] + ); + $this->address1 = new Address( + 'user1@testing.com', + 'User One' + ); + $this->address2 = new Address( + 'user2@testing.com', + 'User Two' + ); + $this->attachment1 = new Attachment( + 'This is the contents of the first attachment', + 'example1.txt', + 'text/plain', + false + ); + $this->attachment2 = new Attachment( + 'This is the contents of the second attachment', + 'example1.txt', + 'text/plain', + false + ); + + } + + public function testId(): void { + + // test set by constructor + $this->assertEquals('cd02ea42-feac-4863-b9d8-484d16a587ea', $this->message->id()); + + } + + public function testFrom(): void { + + // test not set + $this->assertNull($this->message->getFrom()); + // test set by setter + $this->message->setFrom($this->address1); + $this->assertEquals($this->address1, $this->message->getFrom()); + + } + + public function testReplyTo(): void { + + // test not set + $this->assertNull($this->message->getReplyTo()); + // test set by setter + $this->message->setReplyTo($this->address1); + $this->assertEquals($this->address1, $this->message->getReplyTo()); + + } + + public function testTo(): void { + + // test not set + $this->assertEquals([], $this->message->getTo()); + // test set by setter single + $this->message->setTo($this->address1); + $this->assertEquals([$this->address1], $this->message->getTo()); + // test set by setter multiple + $this->message->setTo($this->address1, $this->address2); + $this->assertEquals([$this->address1, $this->address2], $this->message->getTo()); + + } + + public function testCc(): void { + + // test not set + $this->assertEquals([], $this->message->getCc()); + // test set by setter single + $this->message->setCc($this->address1); + $this->assertEquals([$this->address1], $this->message->getCc()); + // test set by setter multiple + $this->message->setCc($this->address1, $this->address2); + $this->assertEquals([$this->address1, $this->address2], $this->message->getCc()); + + } + + public function testBcc(): void { + + // test not set + $this->assertEquals([], $this->message->getBcc()); + // test set by setter single + $this->message->setBcc($this->address1); + $this->assertEquals([$this->address1], $this->message->getBcc()); + // test set by setter multiple + $this->message->setBcc($this->address1, $this->address2); + $this->assertEquals([$this->address1, $this->address2], $this->message->getBcc()); + + } + + public function testSubject(): void { + + // test not set + $this->assertNull($this->message->getSubject()); + // test set by setter + $this->message->setSubject('Testing Mail Subject'); + $this->assertEquals('Testing Mail Subject', $this->message->getSubject()); + + } + + public function testBody(): void { + + // test not set + $this->assertNull($this->message->getBody()); + // test set by setter - text body + $this->message->setBody('Testing Text Body', false); + $this->assertEquals('Testing Text Body', $this->message->getBody()); + $this->message->setBodyPlain('Testing Text Body Again', false); + $this->assertEquals('Testing Text Body Again', $this->message->getBodyPlain()); + // test set by setter - html body + $this->message->setBody('Testing HTML Body', true); + $this->assertEquals('Testing HTML Body', $this->message->getBody()); + $this->message->setBodyHtml('Testing HTML Body Again', false); + $this->assertEquals('Testing HTML Body Again', $this->message->getBodyHtml()); + + } + + public function testAttachments(): void { + + // test not set + $this->assertEquals([], $this->message->getAttachments()); + // test set by setter single + $this->message->setAttachments($this->attachment1); + $this->assertEquals([$this->attachment1], $this->message->getAttachments()); + // test set by setter multiple + $this->message->setAttachments($this->attachment1, $this->attachment2); + $this->assertEquals([$this->attachment1, $this->attachment2], $this->message->getAttachments()); + + } +} |