diff options
Diffstat (limited to 'tests/lib/Mail/MailerTest.php')
-rw-r--r-- | tests/lib/Mail/MailerTest.php | 104 |
1 files changed, 69 insertions, 35 deletions
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); + } } |