summaryrefslogtreecommitdiffstats
path: root/tests/lib
diff options
context:
space:
mode:
authorLukas Reschke <lukas@owncloud.com>2015-02-12 13:53:27 +0100
committerLukas Reschke <lukas@owncloud.com>2015-03-16 12:47:05 +0100
commit13486a5ada62e4355473ca01e07371c162951e84 (patch)
treec079e9c53691ca14e5e1898c40a93322d6e727f5 /tests/lib
parent0d9f149dd93997085b85e2b174f5989a1b996263 (diff)
downloadnextcloud-server-13486a5ada62e4355473ca01e07371c162951e84.tar.gz
nextcloud-server-13486a5ada62e4355473ca01e07371c162951e84.zip
Migrate to SwiftMail
Replaces the OC_Mail and phpmailer with SwiftMail allowing us to mock it properly. Fixes the unit test execution on master on local machines and https://github.com/owncloud/core/issues/12014 Conflicts: 3rdparty lib/private/server.php lib/public/iservercontainer.php tests/lib/mail.php tests/settings/controller/mailsettingscontrollertest.php Conflicts: 3rdparty lib/private/mail.php lib/private/server.php lib/public/iservercontainer.php settings/ajax/lostpassword.php settings/application.php
Diffstat (limited to 'tests/lib')
-rw-r--r--tests/lib/mail.php53
-rw-r--r--tests/lib/mail/mailer.php118
-rw-r--r--tests/lib/mail/message.php161
-rw-r--r--tests/lib/mail/util.php39
4 files changed, 318 insertions, 53 deletions
diff --git a/tests/lib/mail.php b/tests/lib/mail.php
deleted file mode 100644
index 813dde1944f..00000000000
--- a/tests/lib/mail.php
+++ /dev/null
@@ -1,53 +0,0 @@
-<?php
-/**
- * Copyright (c) 2014 Thomas Müller <deepdiver@owncloud.com>
- * This file is licensed under the Affero General Public License version 3 or
- * later.
- * See the COPYING-README file.
- */
-
-class Test_Mail extends \Test\TestCase {
-
- /**
- * @dataProvider buildAsciiEmailProvider
- * @param $expected
- * @param $address
- */
- public function testBuildAsciiEmail($expected, $address) {
- if (!function_exists('idn_to_ascii')) {
- $this->markTestSkipped(
- 'The intl extension is not available.'
- );
- }
-
- $actual = \OC_Mail::buildAsciiEmail($address);
- $this->assertEquals($expected, $actual);
- }
-
- public function buildAsciiEmailProvider() {
- return array(
- array('info@example.com', 'info@example.com'),
- array('info@xn--cjr6vy5ejyai80u.com', 'info@國際化域名.com'),
- array('info@xn--mller-kva.de', 'info@müller.de'),
- array('info@xn--mller-kva.xn--mller-kva.de', 'info@müller.müller.de'),
- );
- }
-
- public function validateMailProvider() {
- return array(
- array('infoatexample.com', false),
- array('info', false),
- );
- }
-
- /**
- * @dataProvider validateMailProvider
- * @param $address
- * @param $expected
- */
- public function testValidateEmail($address, $expected) {
- $actual = \OC_Mail::validateAddress($address);
- $this->assertEquals($expected, $actual);
- }
-
-}
diff --git a/tests/lib/mail/mailer.php b/tests/lib/mail/mailer.php
new file mode 100644
index 00000000000..bd410dd3383
--- /dev/null
+++ b/tests/lib/mail/mailer.php
@@ -0,0 +1,118 @@
+<?php
+/**
+ * Copyright (c) 2014 Lukas Reschke <lukas@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace Test;
+use OC\Mail\Mailer;
+use OCP\IConfig;
+use OC_Defaults;
+
+class MailerTest extends TestCase {
+ /** @var IConfig */
+ private $config;
+ /** @var OC_Defaults */
+ private $defaults;
+ /** @var Mailer */
+ private $mailer;
+
+ function setUp() {
+ parent::setUp();
+
+ $this->config = $this->getMockBuilder('\OCP\IConfig')
+ ->disableOriginalConstructor()->getMock();
+ $this->defaults = $this->getMockBuilder('\OC_Defaults')
+ ->disableOriginalConstructor()->getMock();
+ $this->mailer = new Mailer($this->config, $this->defaults);
+ }
+
+ public function testGetMailInstance() {
+ $this->assertEquals(\Swift_MailTransport::newInstance(), \Test_Helper::invokePrivate($this->mailer, 'getMailinstance'));
+ }
+
+ public function testGetSendMailInstanceSendMail() {
+ $this->config
+ ->expects($this->once())
+ ->method('getSystemValue')
+ ->with('mail_smtpmode', 'sendmail')
+ ->will($this->returnValue('sendmail'));
+
+ $this->assertEquals(\Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs'), \Test_Helper::invokePrivate($this->mailer, 'getSendMailInstance'));
+ }
+
+ public function testGetSendMailInstanceSendMailQmail() {
+ $this->config
+ ->expects($this->once())
+ ->method('getSystemValue')
+ ->with('mail_smtpmode', 'sendmail')
+ ->will($this->returnValue('qmail'));
+
+ $this->assertEquals(\Swift_SendmailTransport::newInstance('/var/qmail/bin/sendmail -bs'), \Test_Helper::invokePrivate($this->mailer, 'getSendMailInstance'));
+ }
+
+ public function testGetSmtpInstanceDefaults() {
+ $expected = \Swift_SmtpTransport::newInstance();
+ $expected->setHost('127.0.0.1');
+ $expected->setTimeout(10);
+ $expected->setPort(25);
+
+ $this->config
+ ->expects($this->any())
+ ->method('getSystemValue')
+ ->will($this->returnArgument(1));
+
+ $this->assertEquals($expected, \Test_Helper::invokePrivate($this->mailer, 'getSmtpInstance'));
+ }
+
+ public function testGetInstanceDefault() {
+ $this->assertInstanceOf('\Swift_MailTransport', \Test_Helper::invokePrivate($this->mailer, 'getInstance'));
+ }
+
+ public function testGetInstancePhp() {
+ $this->config
+ ->expects($this->any())
+ ->method('getSystemValue')
+ ->will($this->returnValue('php'));
+
+ $this->assertInstanceOf('\Swift_MailTransport', \Test_Helper::invokePrivate($this->mailer, 'getInstance'));
+ }
+
+ public function testGetInstanceSmtp() {
+ $this->config
+ ->expects($this->any())
+ ->method('getSystemValue')
+ ->will($this->returnValue('smtp'));
+
+ $this->assertInstanceOf('\Swift_SmtpTransport', \Test_Helper::invokePrivate($this->mailer, 'getInstance'));
+ }
+
+ public function testGetInstanceSendmail() {
+ $this->config
+ ->expects($this->any())
+ ->method('getSystemValue')
+ ->will($this->returnValue('sendmail'));
+
+ $this->assertInstanceOf('\Swift_SendmailTransport', \Test_Helper::invokePrivate($this->mailer, 'getInstance'));
+ }
+
+ 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);
+ }
+
+}
diff --git a/tests/lib/mail/message.php b/tests/lib/mail/message.php
new file mode 100644
index 00000000000..0db2017d81e
--- /dev/null
+++ b/tests/lib/mail/message.php
@@ -0,0 +1,161 @@
+<?php
+/**
+ * Copyright (c) 2014 Lukas Reschke <lukas@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace Test;
+
+use OC\Mail\Message;
+use Swift_Message;
+
+class MessageTest extends TestCase {
+ /** @var Swift_Message */
+ private $swiftMessage;
+ /** @var Message */
+ private $message;
+
+ /**
+ * @return array
+ */
+ public function mailAddressProvider() {
+ return array(
+ array(array('lukas@owncloud.com' => 'Lukas Reschke'), array('lukas@owncloud.com' => 'Lukas Reschke')),
+ array(array('lukas@owncloud.com' => 'Lukas Reschke', 'lukas@öwnclöüd.com', 'lukäs@owncloud.örg' => 'Lükäs Réschke'),
+ array('lukas@owncloud.com' => 'Lukas Reschke', 'lukas@xn--wncld-iuae2c.com', 'lukäs@owncloud.xn--rg-eka' => 'Lükäs Réschke')),
+ array(array('lukas@öwnclöüd.com'), array('lukas@xn--wncld-iuae2c.com'))
+ );
+ }
+
+ function setUp() {
+ parent::setUp();
+
+ $this->swiftMessage = $this->getMockBuilder('\Swift_Message')
+ ->disableOriginalConstructor()->getMock();
+
+ $this->message = new Message($this->swiftMessage);
+ }
+
+ /**
+ * @dataProvider mailAddressProvider
+ */
+ public function testConvertAddresses($unconverted, $expected) {
+ $this->assertSame($expected, \Test_Helper::invokePrivate($this->message, 'convertAddresses', array($unconverted)));
+ }
+
+ public function testSetFrom() {
+ $this->swiftMessage
+ ->expects($this->once())
+ ->method('setFrom')
+ ->with(array('lukas@owncloud.com'));
+ $this->message->setFrom(array('lukas@owncloud.com'));
+ }
+
+ public function testGetFrom() {
+ $this->swiftMessage
+ ->expects($this->once())
+ ->method('getFrom')
+ ->will($this->returnValue(array('lukas@owncloud.com')));
+
+ $this->assertSame(array('lukas@owncloud.com'), $this->message->getFrom());
+ }
+
+ public function testSetTo() {
+ $this->swiftMessage
+ ->expects($this->once())
+ ->method('setTo')
+ ->with(array('lukas@owncloud.com'));
+ $this->message->setTo(array('lukas@owncloud.com'));
+ }
+
+ public function testGetTo() {
+ $this->swiftMessage
+ ->expects($this->once())
+ ->method('getTo')
+ ->will($this->returnValue(array('lukas@owncloud.com')));
+
+ $this->assertSame(array('lukas@owncloud.com'), $this->message->getTo());
+ }
+
+ public function testSetCc() {
+ $this->swiftMessage
+ ->expects($this->once())
+ ->method('setCc')
+ ->with(array('lukas@owncloud.com'));
+ $this->message->setCc(array('lukas@owncloud.com'));
+ }
+
+ public function testGetCc() {
+ $this->swiftMessage
+ ->expects($this->once())
+ ->method('getCc')
+ ->will($this->returnValue(array('lukas@owncloud.com')));
+
+ $this->assertSame(array('lukas@owncloud.com'), $this->message->getCc());
+ }
+
+ public function testSetBcc() {
+ $this->swiftMessage
+ ->expects($this->once())
+ ->method('setBcc')
+ ->with(array('lukas@owncloud.com'));
+ $this->message->setBcc(array('lukas@owncloud.com'));
+ }
+
+ public function testGetBcc() {
+ $this->swiftMessage
+ ->expects($this->once())
+ ->method('getBcc')
+ ->will($this->returnValue(array('lukas@owncloud.com')));
+
+ $this->assertSame(array('lukas@owncloud.com'), $this->message->getBcc());
+ }
+
+ public function testSetSubject() {
+ $this->swiftMessage
+ ->expects($this->once())
+ ->method('setSubject')
+ ->with('Fancy Subject');
+
+ $this->message->setSubject('Fancy Subject');
+ }
+
+ public function testGetSubject() {
+ $this->swiftMessage
+ ->expects($this->once())
+ ->method('getSubject')
+ ->will($this->returnValue('Fancy Subject'));
+
+ $this->assertSame('Fancy Subject', $this->message->getSubject());
+ }
+
+ public function testSetPlainBody() {
+ $this->swiftMessage
+ ->expects($this->once())
+ ->method('setBody')
+ ->with('Fancy Body');
+
+ $this->message->setPlainBody('Fancy Body');
+ }
+
+ public function testGetPlainBody() {
+ $this->swiftMessage
+ ->expects($this->once())
+ ->method('getBody')
+ ->will($this->returnValue('Fancy Body'));
+
+ $this->assertSame('Fancy Body', $this->message->getPlainBody());
+ }
+
+ public function testSetHtmlBody() {
+ $this->swiftMessage
+ ->expects($this->once())
+ ->method('addPart')
+ ->with('<blink>Fancy Body</blink>', 'text/html');
+
+ $this->message->setHtmlBody('<blink>Fancy Body</blink>');
+ }
+
+}
diff --git a/tests/lib/mail/util.php b/tests/lib/mail/util.php
new file mode 100644
index 00000000000..04d9d5df256
--- /dev/null
+++ b/tests/lib/mail/util.php
@@ -0,0 +1,39 @@
+<?php
+/**
+ * Copyright (c) 2014 Lukas Reschke <lukas@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace Test;
+use OCP\Mail\Util;
+
+/**
+ * Class Util
+ *
+ * @package OC\Mail
+ */
+class UtilTest extends TestCase {
+
+ /**
+ * @return array
+ */
+ public function mailAddressProvider() {
+ return array(
+ array('lukas@owncloud.com', true),
+ array('lukas@localhost', true),
+ array('lukas@192.168.1.1', true),
+ array('lukas@éxämplè.com', true),
+ array('asdf', false),
+ array('lukas@owncloud.org@owncloud.com', false)
+ );
+ }
+
+ /**
+ * @dataProvider mailAddressProvider
+ */
+ public function testValidateMailAddress($email, $expected) {
+ $this->assertSame($expected, Util::validateMailAddress($email));
+ }
+}