2015-02-12 15:53:27 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2015-02-19 20:55:27 +03:00
|
|
|
* Copyright (c) 2014-2015 Lukas Reschke <lukas@owncloud.com>
|
2019-03-16 04:29:03 +03:00
|
|
|
*
|
|
|
|
* @author Arne Hamann <github@arne.email>
|
|
|
|
*
|
2015-02-12 15:53:27 +03:00
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
2016-05-19 09:50:14 +03:00
|
|
|
namespace Test\Mail;
|
|
|
|
|
2017-04-11 17:35:46 +03:00
|
|
|
use OC\Mail\EMailTemplate;
|
2015-02-12 15:53:27 +03:00
|
|
|
use OC\Mail\Mailer;
|
2019-03-16 04:29:03 +03:00
|
|
|
use OC\Mail\Message;
|
2017-04-07 23:42:43 +03:00
|
|
|
use OCP\Defaults;
|
2019-03-16 04:29:03 +03:00
|
|
|
use OCP\EventDispatcher\IEventDispatcher;
|
2015-02-12 15:53:27 +03:00
|
|
|
use OCP\IConfig;
|
2017-04-11 17:35:46 +03:00
|
|
|
use OCP\IL10N;
|
2015-03-16 15:01:17 +03:00
|
|
|
use OCP\ILogger;
|
2017-04-11 17:35:46 +03:00
|
|
|
use OCP\IURLGenerator;
|
2019-03-16 04:29:03 +03:00
|
|
|
use OCP\Mail\Events\BeforeMessageSent;
|
2016-05-19 09:50:14 +03:00
|
|
|
use Test\TestCase;
|
2019-03-16 04:29:03 +03:00
|
|
|
use Swift_SwiftException;
|
2015-02-12 15:53:27 +03:00
|
|
|
|
|
|
|
class MailerTest extends TestCase {
|
2017-04-11 17:35:46 +03:00
|
|
|
/** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
|
2015-02-12 15:53:27 +03:00
|
|
|
private $config;
|
2017-04-11 17:35:46 +03:00
|
|
|
/** @var Defaults|\PHPUnit_Framework_MockObject_MockObject */
|
2015-02-12 15:53:27 +03:00
|
|
|
private $defaults;
|
2017-04-11 17:35:46 +03:00
|
|
|
/** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
|
2015-03-16 15:01:17 +03:00
|
|
|
private $logger;
|
2017-04-11 17:35:46 +03:00
|
|
|
/** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $urlGenerator;
|
|
|
|
/** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $l10n;
|
2015-02-12 15:53:27 +03:00
|
|
|
/** @var Mailer */
|
|
|
|
private $mailer;
|
2019-03-16 04:29:03 +03:00
|
|
|
/** @var IEventDispatcher */
|
|
|
|
private $dispatcher;
|
|
|
|
|
2015-02-12 15:53:27 +03:00
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function setUp(): void {
|
2015-02-12 15:53:27 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
2017-04-11 17:35:46 +03:00
|
|
|
$this->config = $this->createMock(IConfig::class);
|
|
|
|
$this->defaults = $this->createMock(Defaults::class);
|
|
|
|
$this->logger = $this->createMock(ILogger::class);
|
|
|
|
$this->urlGenerator = $this->createMock(IURLGenerator::class);
|
|
|
|
$this->l10n = $this->createMock(IL10N::class);
|
2019-03-16 04:29:03 +03:00
|
|
|
$this->dispatcher = $this->createMock(IEventDispatcher::class);
|
2017-04-11 17:35:46 +03:00
|
|
|
$this->mailer = new Mailer(
|
|
|
|
$this->config,
|
|
|
|
$this->logger,
|
|
|
|
$this->defaults,
|
|
|
|
$this->urlGenerator,
|
2019-03-16 04:29:03 +03:00
|
|
|
$this->l10n,
|
|
|
|
$this->dispatcher
|
2017-04-11 17:35:46 +03:00
|
|
|
);
|
2015-02-12 15:53:27 +03:00
|
|
|
}
|
|
|
|
|
2018-11-28 22:06:48 +03:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function sendmailModeProvider(): array {
|
|
|
|
return [
|
|
|
|
'smtp' => ['smtp', ' -bs'],
|
|
|
|
'pipe' => ['pipe', ' -t'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider sendmailModeProvider
|
|
|
|
* @param $sendmailMode
|
|
|
|
* @param $binaryParam
|
|
|
|
*/
|
|
|
|
public function testGetSendmailInstanceSendMail($sendmailMode, $binaryParam) {
|
2015-02-12 15:53:27 +03:00
|
|
|
$this->config
|
2018-11-28 22:06:48 +03:00
|
|
|
->expects($this->exactly(2))
|
2015-02-12 15:53:27 +03:00
|
|
|
->method('getSystemValue')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturnMap([
|
2018-11-28 22:06:48 +03:00
|
|
|
['mail_smtpmode', 'smtp', 'sendmail'],
|
|
|
|
['mail_sendmailmode', 'smtp', $sendmailMode],
|
2020-03-26 00:21:27 +03:00
|
|
|
]);
|
2015-02-12 15:53:27 +03:00
|
|
|
|
2018-11-02 16:03:11 +03:00
|
|
|
$path = \OC_Helper::findBinaryPath('sendmail');
|
|
|
|
if ($path === null) {
|
|
|
|
$path = '/usr/sbin/sendmail';
|
|
|
|
}
|
|
|
|
|
2018-11-28 22:06:48 +03:00
|
|
|
$expected = new \Swift_SendmailTransport($path . $binaryParam);
|
2018-11-02 16:03:11 +03:00
|
|
|
$this->assertEquals($expected, self::invokePrivate($this->mailer, 'getSendMailInstance'));
|
2015-02-12 15:53:27 +03:00
|
|
|
}
|
|
|
|
|
2018-11-28 22:06:48 +03:00
|
|
|
/**
|
|
|
|
* @dataProvider sendmailModeProvider
|
|
|
|
* @param $sendmailMode
|
|
|
|
* @param $binaryParam
|
|
|
|
*/
|
|
|
|
public function testGetSendmailInstanceSendMailQmail($sendmailMode, $binaryParam) {
|
2015-02-12 15:53:27 +03:00
|
|
|
$this->config
|
2018-11-28 22:06:48 +03:00
|
|
|
->expects($this->exactly(2))
|
2015-02-12 15:53:27 +03:00
|
|
|
->method('getSystemValue')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturnMap([
|
2018-11-28 22:06:48 +03:00
|
|
|
['mail_smtpmode', 'smtp', 'qmail'],
|
|
|
|
['mail_sendmailmode', 'smtp', $sendmailMode],
|
2020-03-26 00:21:27 +03:00
|
|
|
]);
|
2015-02-12 15:53:27 +03:00
|
|
|
|
2018-11-28 22:06:48 +03:00
|
|
|
$this->assertEquals(new \Swift_SendmailTransport('/var/qmail/bin/sendmail' . $binaryParam), self::invokePrivate($this->mailer, 'getSendMailInstance'));
|
2015-02-12 15:53:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetInstanceDefault() {
|
2018-06-08 11:26:56 +03:00
|
|
|
$mailer = self::invokePrivate($this->mailer, 'getInstance');
|
|
|
|
$this->assertInstanceOf(\Swift_Mailer::class, $mailer);
|
|
|
|
$this->assertInstanceOf(\Swift_SmtpTransport::class, $mailer->getTransport());
|
2015-02-12 15:53:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetInstanceSendmail() {
|
|
|
|
$this->config
|
|
|
|
->method('getSystemValue')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturnMap([
|
2018-11-28 22:06:48 +03:00
|
|
|
['mail_smtpmode', 'smtp', 'sendmail'],
|
|
|
|
['mail_sendmailmode', 'smtp', 'smtp'],
|
2020-03-26 00:21:27 +03:00
|
|
|
]);
|
2015-02-12 15:53:27 +03:00
|
|
|
|
2018-06-08 11:26:56 +03:00
|
|
|
$mailer = self::invokePrivate($this->mailer, 'getInstance');
|
|
|
|
$this->assertInstanceOf(\Swift_Mailer::class, $mailer);
|
|
|
|
$this->assertInstanceOf(\Swift_SendmailTransport::class, $mailer->getTransport());
|
2015-02-12 15:53:27 +03:00
|
|
|
}
|
|
|
|
|
2019-03-16 04:29:03 +03:00
|
|
|
public function testEvents() {
|
|
|
|
$message = $this->createMock(Message::class);
|
|
|
|
|
|
|
|
$event = new BeforeMessageSent($message);
|
|
|
|
$this->dispatcher->expects($this->at(0))
|
|
|
|
->method('dispatchTyped')
|
|
|
|
->with($this->equalTo($event));
|
|
|
|
|
|
|
|
# We do not care at this point about errors in Swiftmailer
|
|
|
|
try {
|
|
|
|
$this->mailer->send($message);
|
|
|
|
} catch (Swift_SwiftException $e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-12 15:53:27 +03:00
|
|
|
public function testCreateMessage() {
|
2018-04-16 16:55:39 +03:00
|
|
|
$this->config
|
|
|
|
->expects($this->any())
|
|
|
|
->method('getSystemValue')
|
|
|
|
->with('mail_send_plaintext_only', false)
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(false);
|
2015-02-12 15:53:27 +03:00
|
|
|
$this->assertInstanceOf('\OC\Mail\Message', $this->mailer->createMessage());
|
|
|
|
}
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
|
2015-02-12 15:53:27 +03:00
|
|
|
public function testSendInvalidMailException() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\Exception::class);
|
|
|
|
|
2015-02-12 15:53:27 +03:00
|
|
|
$message = $this->getMockBuilder('\OC\Mail\Message')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
$message->expects($this->once())
|
|
|
|
->method('getSwiftMessage')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(new \Swift_Message());
|
2015-02-12 15:53:27 +03:00
|
|
|
|
|
|
|
$this->mailer->send($message);
|
|
|
|
}
|
|
|
|
|
2015-02-19 20:55:27 +03:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function mailAddressProvider() {
|
|
|
|
return [
|
|
|
|
['lukas@owncloud.com', true],
|
|
|
|
['lukas@localhost', true],
|
|
|
|
['lukas@192.168.1.1', true],
|
|
|
|
['lukas@éxämplè.com', true],
|
|
|
|
['asdf', false],
|
|
|
|
['lukas@owncloud.org@owncloud.com', false],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider mailAddressProvider
|
|
|
|
*/
|
|
|
|
public function testValidateMailAddress($email, $expected) {
|
|
|
|
$this->assertSame($expected, $this->mailer->validateMailAddress($email));
|
|
|
|
}
|
|
|
|
|
2017-04-11 17:35:46 +03:00
|
|
|
public function testCreateEMailTemplate() {
|
2018-03-02 09:50:35 +03:00
|
|
|
$this->config->method('getSystemValue')
|
|
|
|
->with('mail_template_class', '')
|
|
|
|
->willReturnArgument(1);
|
|
|
|
|
2017-09-04 18:01:31 +03:00
|
|
|
$this->assertSame(EMailTemplate::class, get_class($this->mailer->createEMailTemplate('tests.MailerTest')));
|
2017-04-11 17:35:46 +03:00
|
|
|
}
|
2018-11-30 23:06:44 +03:00
|
|
|
|
|
|
|
public function testStreamingOptions() {
|
|
|
|
$this->config->method('getSystemValue')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturnMap([
|
2018-11-30 23:06:44 +03:00
|
|
|
['mail_smtpmode', 'smtp', 'smtp'],
|
2018-12-18 01:34:48 +03:00
|
|
|
['mail_smtpstreamoptions', [], ['foo' => 1]]
|
2020-03-26 00:21:27 +03:00
|
|
|
]);
|
2018-11-30 23:06:44 +03:00
|
|
|
$mailer = self::invokePrivate($this->mailer, 'getInstance');
|
|
|
|
$this->assertEquals(1, count($mailer->getTransport()->getStreamOptions()));
|
|
|
|
$this->assertTrue(isset($mailer->getTransport()->getStreamOptions()['foo']));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testStreamingOptionsWrongType() {
|
|
|
|
$this->config->method('getSystemValue')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturnMap([
|
2018-11-30 23:06:44 +03:00
|
|
|
['mail_smtpmode', 'smtp', 'smtp'],
|
2018-12-18 01:34:48 +03:00
|
|
|
['mail_smtpstreamoptions', [], 'bar']
|
2020-03-26 00:21:27 +03:00
|
|
|
]);
|
2018-11-30 23:06:44 +03:00
|
|
|
$mailer = self::invokePrivate($this->mailer, 'getInstance');
|
|
|
|
$this->assertEquals(0, count($mailer->getTransport()->getStreamOptions()));
|
|
|
|
}
|
2015-02-12 15:53:27 +03:00
|
|
|
}
|