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
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetSendMailInstanceSendMail() {
|
|
|
|
$this->config
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getSystemValue')
|
2018-06-08 11:26:56 +03:00
|
|
|
->with('mail_smtpmode', 'smtp')
|
2015-02-12 15:53:27 +03:00
|
|
|
->will($this->returnValue('sendmail'));
|
|
|
|
|
2018-11-02 16:03:11 +03:00
|
|
|
$path = \OC_Helper::findBinaryPath('sendmail');
|
|
|
|
if ($path === null) {
|
|
|
|
$path = '/usr/sbin/sendmail';
|
|
|
|
}
|
|
|
|
|
|
|
|
$expected = new \Swift_SendmailTransport($path . ' -bs');
|
|
|
|
$this->assertEquals($expected, self::invokePrivate($this->mailer, 'getSendMailInstance'));
|
2015-02-12 15:53:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetSendMailInstanceSendMailQmail() {
|
|
|
|
$this->config
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getSystemValue')
|
2018-06-08 11:26:56 +03:00
|
|
|
->with('mail_smtpmode', 'smtp')
|
2015-02-12 15:53:27 +03:00
|
|
|
->will($this->returnValue('qmail'));
|
|
|
|
|
2018-06-08 11:26:56 +03:00
|
|
|
$this->assertEquals(new \Swift_SendmailTransport('/var/qmail/bin/sendmail -bs'), 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-06-08 11:26:56 +03:00
|
|
|
->with('mail_smtpmode', 'smtp')
|
|
|
|
->willReturn('sendmail');
|
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
|
|
|
}
|
2015-02-12 15:53:27 +03:00
|
|
|
}
|