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>
|
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;
|
2017-04-07 23:42:43 +03:00
|
|
|
use OCP\Defaults;
|
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;
|
2016-05-19 09:50:14 +03:00
|
|
|
use Test\TestCase;
|
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;
|
|
|
|
|
2017-04-11 17:35:46 +03:00
|
|
|
public function setUp() {
|
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);
|
|
|
|
$this->mailer = new Mailer(
|
|
|
|
$this->config,
|
|
|
|
$this->logger,
|
|
|
|
$this->defaults,
|
|
|
|
$this->urlGenerator,
|
|
|
|
$this->l10n
|
|
|
|
);
|
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')
|
2018-11-28 22:06:48 +03:00
|
|
|
->will($this->returnValueMap([
|
|
|
|
['mail_smtpmode', 'smtp', 'sendmail'],
|
|
|
|
['mail_sendmailmode', 'smtp', $sendmailMode],
|
|
|
|
]));
|
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')
|
2018-11-28 22:06:48 +03:00
|
|
|
->will($this->returnValueMap([
|
|
|
|
['mail_smtpmode', 'smtp', 'qmail'],
|
|
|
|
['mail_sendmailmode', 'smtp', $sendmailMode],
|
|
|
|
]));
|
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')
|
2018-11-28 22:06:48 +03:00
|
|
|
->will($this->returnValueMap([
|
|
|
|
['mail_smtpmode', 'smtp', 'sendmail'],
|
|
|
|
['mail_sendmailmode', 'smtp', 'smtp'],
|
|
|
|
]));
|
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
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreateMessage() {
|
2018-04-16 16:55:39 +03:00
|
|
|
$this->config
|
|
|
|
->expects($this->any())
|
|
|
|
->method('getSystemValue')
|
|
|
|
->with('mail_send_plaintext_only', false)
|
|
|
|
->will($this->returnValue(false));
|
2015-02-12 15:53:27 +03:00
|
|
|
$this->assertInstanceOf('\OC\Mail\Message', $this->mailer->createMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Exception
|
|
|
|
*/
|
|
|
|
public function testSendInvalidMailException() {
|
|
|
|
$message = $this->getMockBuilder('\OC\Mail\Message')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
$message->expects($this->once())
|
|
|
|
->method('getSwiftMessage')
|
|
|
|
->will($this->returnValue(new \Swift_Message()));
|
|
|
|
|
|
|
|
$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')
|
|
|
|
->will($this->returnValueMap([
|
|
|
|
['mail_smtpmode', 'smtp', 'smtp'],
|
2018-12-18 01:34:48 +03:00
|
|
|
['mail_smtpstreamoptions', [], ['foo' => 1]]
|
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')
|
|
|
|
->will($this->returnValueMap([
|
|
|
|
['mail_smtpmode', 'smtp', 'smtp'],
|
2018-12-18 01:34:48 +03:00
|
|
|
['mail_smtpstreamoptions', [], 'bar']
|
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
|
|
|
}
|