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 testGetMailInstance() {
|
2015-06-03 10:03:02 +00:00
|
|
|
$this->assertEquals(\Swift_MailTransport::newInstance(), self::invokePrivate($this->mailer, 'getMailinstance'));
|
2015-02-12 12:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetSendMailInstanceSendMail() {
|
|
|
|
$this->config
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getSystemValue')
|
2017-01-20 11:55:48 +00:00
|
|
|
->with('mail_smtpmode', 'php')
|
2015-02-12 12:53:27 +00:00
|
|
|
->will($this->returnValue('sendmail'));
|
|
|
|
|
2015-06-03 10:03:02 +00:00
|
|
|
$this->assertEquals(\Swift_SendmailTransport::newInstance('/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')
|
2017-01-20 11:55:48 +00:00
|
|
|
->with('mail_smtpmode', 'php')
|
2015-02-12 12:53:27 +00:00
|
|
|
->will($this->returnValue('qmail'));
|
|
|
|
|
2015-06-03 10:03:02 +00:00
|
|
|
$this->assertEquals(\Swift_SendmailTransport::newInstance('/var/qmail/bin/sendmail -bs'), self::invokePrivate($this->mailer, 'getSendMailInstance'));
|
2015-02-12 12:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetInstanceDefault() {
|
2015-06-03 10:03:02 +00:00
|
|
|
$this->assertInstanceOf('\Swift_MailTransport', self::invokePrivate($this->mailer, 'getInstance'));
|
2015-02-12 12:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetInstancePhp() {
|
|
|
|
$this->config
|
|
|
|
->expects($this->any())
|
|
|
|
->method('getSystemValue')
|
|
|
|
->will($this->returnValue('php'));
|
|
|
|
|
2015-06-03 10:03:02 +00:00
|
|
|
$this->assertInstanceOf('\Swift_MailTransport', self::invokePrivate($this->mailer, 'getInstance'));
|
2015-02-12 12:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetInstanceSendmail() {
|
|
|
|
$this->config
|
|
|
|
->expects($this->any())
|
|
|
|
->method('getSystemValue')
|
|
|
|
->will($this->returnValue('sendmail'));
|
|
|
|
|
2015-10-08 14:46:42 +00:00
|
|
|
$this->assertInstanceOf('\Swift_Mailer', self::invokePrivate($this->mailer, 'getInstance'));
|
2015-02-12 12:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreateMessage() {
|
|
|
|
$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() {
|
|
|
|
$this->assertSame(EMailTemplate::class, get_class($this->mailer->createEMailTemplate()));
|
|
|
|
}
|
2015-02-12 12:53:27 +00:00
|
|
|
}
|