2015-02-12 12:53:27 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2015-02-19 17:55:27 +00:00
|
|
|
* Copyright (c) 2014-2015 Lukas Reschke <lukas@owncloud.com>
|
2015-02-12 12:53:27 +00:00
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
2016-05-19 06:50:14 +00:00
|
|
|
namespace Test\Mail;
|
|
|
|
|
2017-04-11 14:35:46 +00:00
|
|
|
use OC\Mail\EMailTemplate;
|
2015-02-12 12:53:27 +00:00
|
|
|
use OC\Mail\Mailer;
|
2017-04-07 20:42:43 +00:00
|
|
|
use OCP\Defaults;
|
2015-02-12 12:53:27 +00:00
|
|
|
use OCP\IConfig;
|
2017-04-11 14:35:46 +00:00
|
|
|
use OCP\IL10N;
|
2015-03-16 12:01:17 +00:00
|
|
|
use OCP\ILogger;
|
2017-04-11 14:35:46 +00:00
|
|
|
use OCP\IURLGenerator;
|
2016-05-19 06:50:14 +00:00
|
|
|
use Test\TestCase;
|
2015-02-12 12:53:27 +00:00
|
|
|
|
|
|
|
class MailerTest extends TestCase {
|
2017-04-11 14:35:46 +00:00
|
|
|
/** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
|
2015-02-12 12:53:27 +00:00
|
|
|
private $config;
|
2017-04-11 14:35:46 +00:00
|
|
|
/** @var Defaults|\PHPUnit_Framework_MockObject_MockObject */
|
2015-02-12 12:53:27 +00:00
|
|
|
private $defaults;
|
2017-04-11 14:35:46 +00:00
|
|
|
/** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
|
2015-03-16 12:01:17 +00:00
|
|
|
private $logger;
|
2017-04-11 14:35:46 +00:00
|
|
|
/** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $urlGenerator;
|
|
|
|
/** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $l10n;
|
2015-02-12 12:53:27 +00:00
|
|
|
/** @var Mailer */
|
|
|
|
private $mailer;
|
|
|
|
|
2017-04-11 14:35:46 +00:00
|
|
|
public function setUp() {
|
2015-02-12 12:53:27 +00:00
|
|
|
parent::setUp();
|
|
|
|
|
2017-04-11 14:35:46 +00: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 12:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetSendMailInstanceSendMail() {
|
|
|
|
$this->config
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getSystemValue')
|
2018-06-08 08:26:56 +00:00
|
|
|
->with('mail_smtpmode', 'smtp')
|
2015-02-12 12:53:27 +00:00
|
|
|
->will($this->returnValue('sendmail'));
|
|
|
|
|
2018-06-08 08:26:56 +00:00
|
|
|
$this->assertEquals(new \Swift_SendmailTransport('/usr/sbin/sendmail -bs'), self::invokePrivate($this->mailer, 'getSendMailInstance'));
|
2015-02-12 12:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetSendMailInstanceSendMailQmail() {
|
|
|
|
$this->config
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getSystemValue')
|
2018-06-08 08:26:56 +00:00
|
|
|
->with('mail_smtpmode', 'smtp')
|
2015-02-12 12:53:27 +00:00
|
|
|
->will($this->returnValue('qmail'));
|
|
|
|
|
2018-06-08 08:26:56 +00:00
|
|
|
$this->assertEquals(new \Swift_SendmailTransport('/var/qmail/bin/sendmail -bs'), self::invokePrivate($this->mailer, 'getSendMailInstance'));
|
2015-02-12 12:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetInstanceDefault() {
|
2018-06-08 08:26:56 +00:00
|
|
|
$mailer = self::invokePrivate($this->mailer, 'getInstance');
|
|
|
|
$this->assertInstanceOf(\Swift_Mailer::class, $mailer);
|
|
|
|
$this->assertInstanceOf(\Swift_SmtpTransport::class, $mailer->getTransport());
|
2015-02-12 12:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetInstanceSendmail() {
|
|
|
|
$this->config
|
|
|
|
->method('getSystemValue')
|
2018-06-08 08:26:56 +00:00
|
|
|
->with('mail_smtpmode', 'smtp')
|
|
|
|
->willReturn('sendmail');
|
2015-02-12 12:53:27 +00:00
|
|
|
|
2018-06-08 08:26:56 +00:00
|
|
|
$mailer = self::invokePrivate($this->mailer, 'getInstance');
|
|
|
|
$this->assertInstanceOf(\Swift_Mailer::class, $mailer);
|
|
|
|
$this->assertInstanceOf(\Swift_SendmailTransport::class, $mailer->getTransport());
|
2015-02-12 12:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreateMessage() {
|
2018-04-16 13:55:39 +00:00
|
|
|
$this->config
|
|
|
|
->expects($this->any())
|
|
|
|
->method('getSystemValue')
|
|
|
|
->with('mail_send_plaintext_only', false)
|
|
|
|
->will($this->returnValue(false));
|
2015-02-12 12:53:27 +00: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 17:55:27 +00: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 14:35:46 +00:00
|
|
|
public function testCreateEMailTemplate() {
|
2018-03-02 06:50:35 +00:00
|
|
|
$this->config->method('getSystemValue')
|
|
|
|
->with('mail_template_class', '')
|
|
|
|
->willReturnArgument(1);
|
|
|
|
|
2017-09-04 15:01:31 +00:00
|
|
|
$this->assertSame(EMailTemplate::class, get_class($this->mailer->createEMailTemplate('tests.MailerTest')));
|
2017-04-11 14:35:46 +00:00
|
|
|
}
|
2015-02-12 12:53:27 +00:00
|
|
|
}
|