diff options
author | Lukas Reschke <lukas@owncloud.com> | 2015-03-17 11:15:39 +0100 |
---|---|---|
committer | Lukas Reschke <lukas@owncloud.com> | 2015-03-17 11:15:39 +0100 |
commit | b1c19f74a1d245bd8a4a807e73bbe58860d55314 (patch) | |
tree | 250b6b85573a01ee5677b1c99d73d0e2d13d32bd /tests | |
parent | 65259114391eaeab1f6ceb44e52e2c0a5efa9027 (diff) | |
parent | 7a70fffa6cd1597e588111cd39054b80adf296c0 (diff) | |
download | nextcloud-server-b1c19f74a1d245bd8a4a807e73bbe58860d55314.tar.gz nextcloud-server-b1c19f74a1d245bd8a4a807e73bbe58860d55314.zip |
Merge pull request #12085 from owncloud/add-swift-mailer
Migrate OC_Mail to SwiftMailer
Diffstat (limited to 'tests')
-rw-r--r-- | tests/core/lostpassword/controller/lostcontrollertest.php | 101 | ||||
-rw-r--r-- | tests/lib/mail.php | 53 | ||||
-rw-r--r-- | tests/lib/mail/mailer.php | 121 | ||||
-rw-r--r-- | tests/lib/mail/message.php | 161 | ||||
-rw-r--r-- | tests/settings/controller/mailsettingscontrollertest.php | 11 | ||||
-rw-r--r-- | tests/settings/controller/userscontrollertest.php | 121 |
6 files changed, 457 insertions, 111 deletions
diff --git a/tests/core/lostpassword/controller/lostcontrollertest.php b/tests/core/lostpassword/controller/lostcontrollertest.php index b03cbd7c42f..f82fc1ba3ff 100644 --- a/tests/core/lostpassword/controller/lostcontrollertest.php +++ b/tests/core/lostpassword/controller/lostcontrollertest.php @@ -1,6 +1,6 @@ <?php /** - * Copyright (c) 2014 Lukas Reschke <lukas@owncloud.com> + * Copyright (c) 2014-2015 Lukas Reschke <lukas@owncloud.com> * This file is licensed under the Affero General Public License version 3 or * later. * See the COPYING-README file. @@ -43,6 +43,8 @@ class LostControllerTest extends \PHPUnit_Framework_TestCase { ->disableOriginalConstructor()->getMock(); $this->container['URLGenerator'] = $this->getMockBuilder('\OCP\IURLGenerator') ->disableOriginalConstructor()->getMock(); + $this->container['Mailer'] = $this->getMockBuilder('\OCP\Mail\IMailer') + ->disableOriginalConstructor()->getMock(); $this->container['SecureRandom'] = $this->getMockBuilder('\OCP\Security\ISecureRandom') ->disableOriginalConstructor()->getMock(); $this->container['IsEncryptionEnabled'] = true; @@ -103,14 +105,6 @@ class LostControllerTest extends \PHPUnit_Framework_TestCase { } public function testEmailSuccessful() { - /** - * FIXME: Disable test for systems where no sendmail is available since code is static. - * @link https://github.com/owncloud/core/pull/12085 - */ - if (is_null(\OC_Helper::findBinaryPath('sendmail'))) { - $this->markTestSkipped('sendmail is not available'); - } - $randomToken = $this->container['SecureRandom']; $this->container['SecureRandom'] ->expects($this->once()) @@ -140,12 +134,101 @@ class LostControllerTest extends \PHPUnit_Framework_TestCase { ->method('linkToRouteAbsolute') ->with('core.lost.resetform', array('userId' => 'ExistingUser', 'token' => 'ThisIsMaybeANotSoSecretToken!')) ->will($this->returnValue('https://ownCloud.com/index.php/lostpassword/')); + $message = $this->getMockBuilder('\OC\Mail\Message') + ->disableOriginalConstructor()->getMock(); + $message + ->expects($this->at(0)) + ->method('setTo') + ->with(['test@example.com' => 'ExistingUser']); + $message + ->expects($this->at(1)) + ->method('setSubject') + ->with(' password reset'); + $message + ->expects($this->at(2)) + ->method('setPlainBody') + ->with('Use the following link to reset your password: https://ownCloud.com/index.php/lostpassword/'); + $message + ->expects($this->at(3)) + ->method('setFrom') + ->with(['lostpassword-noreply@localhost' => 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); $response = $this->lostController->email('ExistingUser'); $expectedResponse = array('status' => 'success'); $this->assertSame($expectedResponse, $response); } + public function testEmailCantSendException() { + $randomToken = $this->container['SecureRandom']; + $this->container['SecureRandom'] + ->expects($this->once()) + ->method('generate') + ->with('21') + ->will($this->returnValue('ThisIsMaybeANotSoSecretToken!')); + $this->container['UserManager'] + ->expects($this->once()) + ->method('userExists') + ->with('ExistingUser') + ->will($this->returnValue(true)); + $this->container['Config'] + ->expects($this->once()) + ->method('getUserValue') + ->with('ExistingUser', 'settings', 'email') + ->will($this->returnValue('test@example.com')); + $this->container['SecureRandom'] + ->expects($this->once()) + ->method('getMediumStrengthGenerator') + ->will($this->returnValue($randomToken)); + $this->container['Config'] + ->expects($this->once()) + ->method('setUserValue') + ->with('ExistingUser', 'owncloud', 'lostpassword', 'ThisIsMaybeANotSoSecretToken!'); + $this->container['URLGenerator'] + ->expects($this->once()) + ->method('linkToRouteAbsolute') + ->with('core.lost.resetform', array('userId' => 'ExistingUser', 'token' => 'ThisIsMaybeANotSoSecretToken!')) + ->will($this->returnValue('https://ownCloud.com/index.php/lostpassword/')); + $message = $this->getMockBuilder('\OC\Mail\Message') + ->disableOriginalConstructor()->getMock(); + $message + ->expects($this->at(0)) + ->method('setTo') + ->with(['test@example.com' => 'ExistingUser']); + $message + ->expects($this->at(1)) + ->method('setSubject') + ->with(' password reset'); + $message + ->expects($this->at(2)) + ->method('setPlainBody') + ->with('Use the following link to reset your password: https://ownCloud.com/index.php/lostpassword/'); + $message + ->expects($this->at(3)) + ->method('setFrom') + ->with(['lostpassword-noreply@localhost' => 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) + ->will($this->throwException(new \Exception())); + + $response = $this->lostController->email('ExistingUser'); + $expectedResponse = ['status' => 'error', 'msg' => 'Couldn\'t send reset email. Please contact your administrator.']; + $this->assertSame($expectedResponse, $response); + } + public function testSetPasswordUnsuccessful() { $this->container['Config'] ->expects($this->once()) 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..7e5e689741a --- /dev/null +++ b/tests/lib/mail/mailer.php @@ -0,0 +1,121 @@ +<?php +/** + * Copyright (c) 2014-2015 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; +use OCP\ILogger; + +class MailerTest extends TestCase { + /** @var IConfig */ + private $config; + /** @var OC_Defaults */ + private $defaults; + /** @var ILogger */ + private $logger; + /** @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->logger = $this->getMockBuilder('\OCP\ILogger') + ->disableOriginalConstructor()->getMock(); + $this->mailer = new Mailer($this->config, $this->logger, $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 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 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); + } + + /** + * @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)); + } + +} 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/settings/controller/mailsettingscontrollertest.php b/tests/settings/controller/mailsettingscontrollertest.php index ed33d7fbe49..cc25fda52f7 100644 --- a/tests/settings/controller/mailsettingscontrollertest.php +++ b/tests/settings/controller/mailsettingscontrollertest.php @@ -30,7 +30,10 @@ 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['Mailer'] = $this->getMockBuilder('\OC\Mail\Mailer') + ->setMethods(['send']) ->disableOriginalConstructor()->getMock(); $this->container['Defaults'] = $this->getMockBuilder('\OC_Defaults') ->disableOriginalConstructor()->getMock(); @@ -152,12 +155,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..25c935bef58 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,84 @@ 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('validateMailAddress') + ->with('validMail@Adre.ss') + ->will($this->returnValue(true)); + $this->container['Mailer'] + ->expects($this->at(1)) + ->method('createMessage') + ->will($this->returnValue($message)); + $this->container['Mailer'] + ->expects($this->at(2)) + ->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()); } |