diff options
author | Lukas Reschke <lukas@owncloud.com> | 2015-02-12 13:53:27 +0100 |
---|---|---|
committer | Lukas Reschke <lukas@owncloud.com> | 2015-03-16 12:47:05 +0100 |
commit | 13486a5ada62e4355473ca01e07371c162951e84 (patch) | |
tree | c079e9c53691ca14e5e1898c40a93322d6e727f5 /tests | |
parent | 0d9f149dd93997085b85e2b174f5989a1b996263 (diff) | |
download | nextcloud-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')
-rw-r--r-- | tests/lib/mail.php | 53 | ||||
-rw-r--r-- | tests/lib/mail/mailer.php | 118 | ||||
-rw-r--r-- | tests/lib/mail/message.php | 161 | ||||
-rw-r--r-- | tests/lib/mail/util.php | 39 | ||||
-rw-r--r-- | tests/settings/controller/mailsettingscontrollertest.php | 8 | ||||
-rw-r--r-- | tests/settings/controller/userscontrollertest.php | 116 |
6 files changed, 393 insertions, 102 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)); + } +} diff --git a/tests/settings/controller/mailsettingscontrollertest.php b/tests/settings/controller/mailsettingscontrollertest.php index ed33d7fbe49..84432f656bf 100644 --- a/tests/settings/controller/mailsettingscontrollertest.php +++ b/tests/settings/controller/mailsettingscontrollertest.php @@ -30,7 +30,7 @@ class MailSettingsControllerTest extends \Test\TestCase { $this->container['AppName'] = 'settings'; $this->container['UserSession'] = $this->getMockBuilder('\OC\User\Session') ->disableOriginalConstructor()->getMock(); - $this->container['Mail'] = $this->getMockBuilder('\OC_Mail') + $this->container['MailMessage'] = $this->getMockBuilder('\OCP\Mail\IMessage') ->disableOriginalConstructor()->getMock(); $this->container['Defaults'] = $this->getMockBuilder('\OC_Defaults') ->disableOriginalConstructor()->getMock(); @@ -152,12 +152,6 @@ class MailSettingsControllerTest extends \Test\TestCase { } public function testSendTestMail() { - /** - * FIXME: Disabled due to missing DI on mail class. - * TODO: Re-enable when https://github.com/owncloud/core/pull/12085 is merged. - */ - $this->markTestSkipped('Disable test until OC_Mail is rewritten.'); - $user = $this->getMockBuilder('\OC\User\User') ->disableOriginalConstructor() ->getMock(); diff --git a/tests/settings/controller/userscontrollertest.php b/tests/settings/controller/userscontrollertest.php index b813da038a3..3f69d1e7d18 100644 --- a/tests/settings/controller/userscontrollertest.php +++ b/tests/settings/controller/userscontrollertest.php @@ -45,7 +45,7 @@ class UsersControllerTest extends \Test\TestCase { })); $this->container['Defaults'] = $this->getMockBuilder('\OC_Defaults') ->disableOriginalConstructor()->getMock(); - $this->container['Mail'] = $this->getMockBuilder('\OC_Mail') + $this->container['Mailer'] = $this->getMockBuilder('\OCP\Mail\IMailer') ->disableOriginalConstructor()->getMock(); $this->container['DefaultMailAddress'] = 'no-reply@owncloud.com'; $this->container['Logger'] = $this->getMockBuilder('\OCP\ILogger') @@ -1151,24 +1151,12 @@ class UsersControllerTest extends \Test\TestCase { public function testCreateUnsuccessfulWithInvalidEmailAdmin() { $this->container['IsAdmin'] = true; - /** - * FIXME: Disabled due to missing DI on mail class. - * TODO: Re-enable when https://github.com/owncloud/core/pull/12085 is merged. - */ - $this->markTestSkipped('Disable test until OC_Mail is rewritten.'); - - $this->container['Mail'] - ->expects($this->once()) - ->method('validateAddress') - ->will($this->returnValue(false)); - - $expectedResponse = new DataResponse( - array( - 'message' => 'Invalid mail address' - ), + $expectedResponse = new DataResponse([ + 'message' => 'Invalid mail address', + ], Http::STATUS_UNPROCESSABLE_ENTITY ); - $response = $this->container['UsersController']->create('foo', 'password', array(), 'invalidMailAdress'); + $response = $this->container['UsersController']->create('foo', 'password', [], 'invalidMailAdress'); $this->assertEquals($expectedResponse, $response); } @@ -1177,35 +1165,79 @@ class UsersControllerTest extends \Test\TestCase { */ public function testCreateSuccessfulWithValidEmailAdmin() { $this->container['IsAdmin'] = true; + $message = $this->getMockBuilder('\OC\Mail\Message') + ->disableOriginalConstructor()->getMock(); + $message + ->expects($this->at(0)) + ->method('setTo') + ->with(['validMail@Adre.ss' => 'foo']); + $message + ->expects($this->at(1)) + ->method('setSubject') + ->with('Your account was created'); + $htmlBody = new Http\TemplateResponse( + 'settings', + 'email.new_user', + [ + 'username' => 'foo', + 'url' => '', + ], + 'blank' + ); + $message + ->expects($this->at(2)) + ->method('setHtmlBody') + ->with($htmlBody->render()); + $plainBody = new Http\TemplateResponse( + 'settings', + 'email.new_user_plain_text', + [ + 'username' => 'foo', + 'url' => '', + ], + 'blank' + ); + $message + ->expects($this->at(3)) + ->method('setPlainBody') + ->with($plainBody->render()); + $message + ->expects($this->at(4)) + ->method('setFrom') + ->with(['no-reply@owncloud.com' => null]); + + $this->container['Mailer'] + ->expects($this->at(0)) + ->method('createMessage') + ->will($this->returnValue($message)); + $this->container['Mailer'] + ->expects($this->at(1)) + ->method('send') + ->with($message); - /** - * FIXME: Disabled due to missing DI on mail class. - * TODO: Re-enable when https://github.com/owncloud/core/pull/12085 is merged. - */ - $this->markTestSkipped('Disable test until OC_Mail is rewritten.'); - - $this->container['Mail'] + $user = $this->getMockBuilder('\OC\User\User') + ->disableOriginalConstructor()->getMock(); + $user + ->method('getHome') + ->will($this->returnValue('/home/user')); + $user + ->method('getHome') + ->will($this->returnValue('/home/user')); + $user + ->method('getUID') + ->will($this->returnValue('foo')); + $user ->expects($this->once()) - ->method('validateAddress') - ->will($this->returnValue(true)); - $this->container['Mail'] + ->method('getBackendClassName') + ->will($this->returnValue('bar')); + + $this->container['UserManager'] ->expects($this->once()) - ->method('send') - ->with( - $this->equalTo('validMail@Adre.ss'), - $this->equalTo('foo'), - $this->anything(), - $this->anything(), - $this->anything(), - $this->equalTo('no-reply@owncloud.com'), - $this->equalTo(1), - $this->anything() - ); - $this->container['Logger'] - ->expects($this->never()) - ->method('error'); + ->method('createUser') + ->will($this->onConsecutiveCalls($user)); + - $response = $this->container['UsersController']->create('foo', 'password', array(), 'validMail@Adre.ss'); + $response = $this->container['UsersController']->create('foo', 'password', [], 'validMail@Adre.ss'); $this->assertEquals(Http::STATUS_CREATED, $response->getStatus()); } |