* * @license GNU AGPL version 3 or any later version * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . * */ namespace Tests\Settings\Mailer; use OC\Mail\EMailTemplate; use OCP\L10N\IFactory; use OCP\Mail\IEMailTemplate; use OC\Mail\Message; use OC\Settings\Mailer\NewUserMailHelper; use OCP\AppFramework\Utility\ITimeFactory; use OCP\Defaults; use OCP\IConfig; use OCP\IL10N; use OCP\IURLGenerator; use OCP\IUser; use OCP\Mail\IMailer; use OCP\Security\ICrypto; use OCP\Security\ISecureRandom; use Test\TestCase; class NewUserMailHelperTest extends TestCase { /** @var Defaults|\PHPUnit_Framework_MockObject_MockObject */ private $defaults; /** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */ private $urlGenerator; /** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */ private $l10n; /** @var IMailer|\PHPUnit_Framework_MockObject_MockObject */ private $mailer; /** @var ISecureRandom|\PHPUnit_Framework_MockObject_MockObject */ private $secureRandom; /** @var ITimeFactory|\PHPUnit_Framework_MockObject_MockObject */ private $timeFactory; /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */ private $config; /** @var ICrypto|\PHPUnit_Framework_MockObject_MockObject */ private $crypto; /** @var NewUserMailHelper */ private $newUserMailHelper; public function setUp() { parent::setUp(); $this->defaults = $this->createMock(Defaults::class); $this->defaults->method('getLogo') ->willReturn('myLogo'); $this->urlGenerator = $this->createMock(IURLGenerator::class); $this->l10n = $this->createMock(IL10N::class); $this->l10nFactory = $this->createMock(IFactory::class); $this->mailer = $this->createMock(IMailer::class); $template = new EMailTemplate( $this->defaults, $this->urlGenerator, $this->l10n, 'test.TestTemplate', [] ); $this->mailer->method('createEMailTemplate') ->will($this->returnValue($template)); $this->secureRandom = $this->createMock(ISecureRandom::class); $this->timeFactory = $this->createMock(ITimeFactory::class); $this->config = $this->createMock(IConfig::class); $this->crypto = $this->createMock(ICrypto::class); $this->l10n->method('t') ->will($this->returnCallback(function ($text, $parameters = []) { return vsprintf($text, $parameters); })); $this->l10nFactory->method('get') ->will($this->returnCallback(function ($text, $lang) { return $this->l10n; })); $this->newUserMailHelper = new NewUserMailHelper( $this->defaults, $this->urlGenerator, $this->l10nFactory, $this->mailer, $this->secureRandom, $this->timeFactory, $this->config, $this->crypto, 'no-reply@nextcloud.com' ); } public function testGenerateTemplateWithPasswordResetToken() { $this->secureRandom ->expects($this->once()) ->method('generate') ->with(21, ISecureRandom::CHAR_DIGITS . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER ) ->willReturn('MySuperLongSecureRandomToken'); $this->timeFactory ->expects($this->once()) ->method('getTime') ->willReturn('12345'); /** @var IUser|\PHPUnit_Framework_MockObject_MockObject $user */ $user = $this->createMock(IUser::class); $user ->expects($this->any()) ->method('getEmailAddress') ->willReturn('recipient@example.com'); $this->config ->expects($this->any()) ->method('getSystemValue') ->with('secret') ->willReturn('MyInstanceWideSecret'); $this->crypto ->expects($this->once()) ->method('encrypt') ->with('12345:MySuperLongSecureRandomToken', 'recipient@example.comMyInstanceWideSecret') ->willReturn('TokenCiphertext'); $user ->expects($this->any()) ->method('getUID') ->willReturn('john'); $this->config ->expects($this->once()) ->method('setUserValue') ->with('john', 'core', 'lostpassword', 'TokenCiphertext'); $this->urlGenerator ->expects($this->at(0)) ->method('linkToRouteAbsolute') ->with('core.lost.resetform', ['userId' => 'john', 'token' => 'MySuperLongSecureRandomToken']) ->willReturn('https://example.com/resetPassword/MySuperLongSecureRandomToken'); $user ->expects($this->any()) ->method('getDisplayName') ->willReturn('john'); $user ->expects($this->at(5)) ->method('getUID') ->willReturn('john'); $this->defaults ->expects($this->any()) ->method('getName') ->willReturn('TestCloud'); $this->defaults ->expects($this->any()) ->method('getTextColorPrimary') ->willReturn('#ffffff'); $expectedHtmlBody = <<
 

Welcome aboard

 
 

Welcome to your TestCloud account, you can add, protect, and share your data.

Your username is: john

 
Set your password
Install Client
 
                                                           
EOF; $expectedTextBody = <<newUserMailHelper->generateTemplate($user, true); $this->assertEquals($expectedHtmlBody, $result->renderHtml()); $this->assertEquals($expectedTextBody, $result->renderText()); $this->assertSame('OC\Mail\EMailTemplate', get_class($result)); } public function testGenerateTemplateWithoutPasswordResetToken() { $this->urlGenerator ->expects($this->at(0)) ->method('getAbsoluteURL') ->with('/') ->willReturn('https://example.com/'); /** @var IUser|\PHPUnit_Framework_MockObject_MockObject $user */ $user = $this->createMock(IUser::class); $user ->expects($this->any()) ->method('getDisplayName') ->willReturn('John Doe'); $user ->expects($this->any()) ->method('getUID') ->willReturn('john'); $this->defaults ->expects($this->any()) ->method('getName') ->willReturn('TestCloud'); $this->defaults ->expects($this->any()) ->method('getTextColorPrimary') ->willReturn('#ffffff'); $expectedHtmlBody = <<
 

Welcome aboard John Doe

 
 

Welcome to your TestCloud account, you can add, protect, and share your data.

Your username is: john

 
Go to TestCloud
Install Client
 
                                                           
EOF; $expectedTextBody = <<newUserMailHelper->generateTemplate($user, false); $this->assertEquals($expectedHtmlBody, $result->renderHtml()); $this->assertEquals($expectedTextBody, $result->renderText()); $this->assertSame('OC\Mail\EMailTemplate', get_class($result)); } public function testSendMail() { /** @var IUser|\PHPUnit_Framework_MockObject_MockObject $user */ $user = $this->createMock(IUser::class); $user ->expects($this->at(0)) ->method('getEMailAddress') ->willReturn('recipient@example.com'); $user ->expects($this->at(1)) ->method('getDisplayName') ->willReturn('John Doe'); /** @var IEMailTemplate|\PHPUnit_Framework_MockObject_MockObject $emailTemplate */ $emailTemplate = $this->createMock(IEMailTemplate::class); $message = $this->createMock(Message::class); $message ->expects($this->at(0)) ->method('setTo') ->with(['recipient@example.com' => 'John Doe']); $message ->expects($this->at(1)) ->method('setFrom') ->with(['no-reply@nextcloud.com' => 'TestCloud']); $message ->expects($this->at(2)) ->method('useTemplate') ->with($emailTemplate); $this->defaults ->expects($this->exactly(1)) ->method('getName') ->willReturn('TestCloud'); $this->mailer ->expects($this->once()) ->method('createMessage') ->willReturn($message); $this->newUserMailHelper->sendMail($user, $emailTemplate); } }