diff --git a/apps/provisioning_api/lib/AppInfo/Application.php b/apps/provisioning_api/lib/AppInfo/Application.php index 2d6a82e2ff9bf..fd03fd41e16c3 100644 --- a/apps/provisioning_api/lib/AppInfo/Application.php +++ b/apps/provisioning_api/lib/AppInfo/Application.php @@ -3,8 +3,11 @@ namespace OCA\Provisioning_API\AppInfo; use OC\AppFramework\Utility\SimpleContainer; +use OC\AppFramework\Utility\TimeFactory; +use OC\Settings\Mailer\NewUserMailHelper; use OCA\Provisioning_API\Middleware\ProvisioningApiMiddleware; use OCP\AppFramework\App; +use OCP\Util; class Application extends App { public function __construct(array $urlParams = array()) { @@ -13,6 +16,19 @@ public function __construct(array $urlParams = array()) { $container = $this->getContainer(); $server = $container->getServer(); + $container->registerService(NewUserMailHelper::class, function(SimpleContainer $c) use ($server) { + return new NewUserMailHelper( + $server->getThemingDefaults(), + $server->getURLGenerator(), + $server->getL10N('settings'), + $server->getMailer(), + $server->getSecureRandom(), + new TimeFactory(), + $server->getConfig(), + $server->getCrypto(), + Util::getDefaultEmailAddress('no-reply') + ); + }); $container->registerService('ProvisioningApiMiddleware', function(SimpleContainer $c) use ($server) { $user = $server->getUserManager()->get($c['UserId']); $isAdmin = $user !== null ? $server->getGroupManager()->isAdmin($user->getUID()) : false; diff --git a/apps/provisioning_api/lib/Controller/UsersController.php b/apps/provisioning_api/lib/Controller/UsersController.php index 9155d34276b75..0ccbc978a69c6 100644 --- a/apps/provisioning_api/lib/Controller/UsersController.php +++ b/apps/provisioning_api/lib/Controller/UsersController.php @@ -30,6 +30,7 @@ namespace OCA\Provisioning_API\Controller; use OC\Accounts\AccountManager; +use OC\Settings\Mailer\NewUserMailHelper; use \OC_Helper; use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\Http\TemplateResponse; @@ -72,6 +73,8 @@ class UsersController extends OCSController { private $defaults; /** @var IFactory */ private $l10nFactory; + /** @var NewUserMailHelper */ + private $newUserMailHelper; /** * @param string $appName @@ -87,6 +90,7 @@ class UsersController extends OCSController { * @param IMailer $mailer * @param \OC_Defaults $defaults * @param IFactory $l10nFactory + * @param NewUserMailHelper $newUserMailHelper */ public function __construct($appName, IRequest $request, @@ -100,7 +104,8 @@ public function __construct($appName, IURLGenerator $urlGenerator, IMailer $mailer, \OC_Defaults $defaults, - IFactory $l10nFactory) { + IFactory $l10nFactory, + NewUserMailHelper $newUserMailHelper) { parent::__construct($appName, $request); $this->userManager = $userManager; @@ -114,6 +119,7 @@ public function __construct($appName, $this->mailer = $mailer; $this->defaults = $defaults; $this->l10nFactory = $l10nFactory; + $this->newUserMailHelper = $newUserMailHelper; } /** @@ -786,30 +792,10 @@ public function resendWelcomeMessage($userId) { $l10n = $this->l10nFactory->get('settings', $lang); - // data for the mail template - $mailData = [ - 'username' => $username, - 'url' => $this->urlGenerator->getAbsoluteURL('/') - ]; - - // FIXME: set users language in email - $mail = new TemplateResponse('settings', 'email.new_user', $mailData, 'blank'); - $mailContent = $mail->render(); - - // FIXME: set users language in email - $mail = new TemplateResponse('settings', 'email.new_user_plain_text', $mailData, 'blank'); - $plainTextMailContent = $mail->render(); - - $subject = $l10n->t('Your %s account was created', [$this->defaults->getName()]); - try { - $message = $this->mailer->createMessage(); - $message->setTo([$email => $username]); - $message->setSubject($subject); - $message->setHtmlBody($mailContent); - $message->setPlainBody($plainTextMailContent); - $message->setFrom([$this->fromMailAddress => $this->defaults->getName()]); - $this->mailer->send($message); + $this->newUserMailHelper->setL10N($l10n); + $emailTemplate = $this->newUserMailHelper->generateTemplate($targetUser, false); + $this->newUserMailHelper->sendMail($targetUser, $emailTemplate); } catch(\Exception $e) { $this->logger->error("Can't send new user mail to $email: " . $e->getMessage(), array('app' => 'settings')); throw new OCSException('Sending email failed', 102); diff --git a/apps/provisioning_api/tests/Controller/UsersControllerTest.php b/apps/provisioning_api/tests/Controller/UsersControllerTest.php index b5b63319d3534..0cbcccebf6ffd 100644 --- a/apps/provisioning_api/tests/Controller/UsersControllerTest.php +++ b/apps/provisioning_api/tests/Controller/UsersControllerTest.php @@ -32,6 +32,8 @@ use Exception; use OC\Accounts\AccountManager; use OC\Group\Manager; +use OC\Mail\IEMailTemplate; +use OC\Settings\Mailer\NewUserMailHelper; use OC\SubAdmin; use OCA\Provisioning_API\Controller\UsersController; use OCP\AppFramework\Http\DataResponse; @@ -54,39 +56,30 @@ class UsersControllerTest extends TestCase { /** @var IUserManager|PHPUnit_Framework_MockObject_MockObject */ protected $userManager; - /** @var IConfig|PHPUnit_Framework_MockObject_MockObject */ protected $config; - /** @var Manager|PHPUnit_Framework_MockObject_MockObject */ protected $groupManager; - /** @var IUserSession|PHPUnit_Framework_MockObject_MockObject */ protected $userSession; - /** @var ILogger|PHPUnit_Framework_MockObject_MockObject */ protected $logger; - /** @var UsersController|PHPUnit_Framework_MockObject_MockObject */ protected $api; - /** @var AccountManager|PHPUnit_Framework_MockObject_MockObject */ protected $accountManager; - /** @var IRequest|PHPUnit_Framework_MockObject_MockObject */ protected $request; - - /** @var IURLGenerator | PHPUnit_Framework_MockObject_MockObject */ + /** @var IURLGenerator|PHPUnit_Framework_MockObject_MockObject */ private $urlGenerator; - - /** @var IMailer | PHPUnit_Framework_MockObject_MockObject */ + /** @var IMailer|PHPUnit_Framework_MockObject_MockObject */ private $mailer; - - /** @var \OC_Defaults | PHPUnit_Framework_MockObject_MockObject */ + /** @var \OC_Defaults|PHPUnit_Framework_MockObject_MockObject */ private $defaults; - - /** @var IFactory | PHPUnit_Framework_MockObject_MockObject */ + /** @var IFactory|PHPUnit_Framework_MockObject_MockObject */ private $l10nFactory; + /** @var NewUserMailHelper|PHPUnit_Framework_MockObject_MockObject */ + private $newUserMailHelper; protected function setUp() { parent::setUp(); @@ -102,6 +95,7 @@ protected function setUp() { $this->mailer = $this->createMock(IMailer::class); $this->defaults = $this->createMock(\OC_Defaults::class); $this->l10nFactory = $this->createMock(IFactory::class); + $this->newUserMailHelper = $this->createMock(NewUserMailHelper::class); $this->api = $this->getMockBuilder(UsersController::class) ->setConstructorArgs([ @@ -117,7 +111,8 @@ protected function setUp() { $this->urlGenerator, $this->mailer, $this->defaults, - $this->l10nFactory + $this->l10nFactory, + $this->newUserMailHelper ]) ->setMethods(['fillStorageInfo']) ->getMock(); @@ -2621,7 +2616,8 @@ public function testGetCurrentUserLoggedIn() { $this->urlGenerator, $this->mailer, $this->defaults, - $this->l10nFactory + $this->l10nFactory, + $this->newUserMailHelper ]) ->setMethods(['getUserData']) ->getMock(); @@ -2684,7 +2680,8 @@ public function testGetUser() { $this->urlGenerator, $this->mailer, $this->defaults, - $this->l10nFactory + $this->l10nFactory, + $this->newUserMailHelper ]) ->setMethods(['getUserData']) ->getMock(); @@ -2884,56 +2881,6 @@ public function testResendWelcomeMessageSuccess() { ->expects($this->once()) ->method('getEmailAddress') ->will($this->returnValue('abc@example.org')); - $message = $this->getMockBuilder('\OC\Mail\Message') - ->disableOriginalConstructor()->getMock(); - $message - ->expects($this->at(0)) - ->method('setTo') - ->with(['abc@example.org' => 'user-id']); - $message - ->expects($this->at(1)) - ->method('setSubject') - ->with('Your account was created'); - $htmlBody = new TemplateResponse( - 'settings', - 'email.new_user', - [ - 'username' => 'user-id', - 'url' => null, - ], - 'blank' - ); - $message - ->expects($this->at(2)) - ->method('setHtmlBody') - ->with($htmlBody->render()); - $plainBody = new TemplateResponse( - 'settings', - 'email.new_user_plain_text', - [ - 'username' => 'user-id', - 'url' => null, - ], - 'blank' - ); - $message - ->expects($this->at(3)) - ->method('setPlainBody') - ->with($plainBody->render()); - $message - ->expects($this->at(4)) - ->method('setFrom') - ->with(['test@example.org' => null]); - - $this->mailer - ->expects($this->at(0)) - ->method('createMessage') - ->will($this->returnValue($message)); - $this->mailer - ->expects($this->at(1)) - ->method('send') - ->with($message); - $this->config ->expects($this->at(0)) ->method('getUserValue') @@ -2942,11 +2889,6 @@ public function testResendWelcomeMessageSuccess() { $l10n = $this->getMockBuilder(IL10N::class) ->disableOriginalConstructor() ->getMock(); - $l10n - ->expects($this->at(0)) - ->method('t') - ->with('Your %s account was created', [null]) - ->willReturn('Your account was created'); $this->l10nFactory ->expects($this->at(0)) ->method('languageExists') @@ -2957,6 +2899,19 @@ public function testResendWelcomeMessageSuccess() { ->method('get') ->with('settings', 'es') ->willReturn($l10n); + $emailTemplate = $this->createMock(IEMailTemplate::class); + $this->newUserMailHelper + ->expects($this->at(0)) + ->method('setL10N') + ->willReturn($l10n); + $this->newUserMailHelper + ->expects($this->at(1)) + ->method('generateTemplate') + ->willReturn($emailTemplate); + $this->newUserMailHelper + ->expects($this->at(2)) + ->method('sendMail') + ->with($targetUser, $emailTemplate); $this->api->resendWelcomeMessage('UserToGet'); } @@ -2996,56 +2951,6 @@ public function testResendWelcomeMessageSuccessWithFallbackLanguage() { ->expects($this->once()) ->method('getEmailAddress') ->will($this->returnValue('abc@example.org')); - $message = $this->getMockBuilder('\OC\Mail\Message') - ->disableOriginalConstructor()->getMock(); - $message - ->expects($this->at(0)) - ->method('setTo') - ->with(['abc@example.org' => 'user-id']); - $message - ->expects($this->at(1)) - ->method('setSubject') - ->with('Your account was created'); - $htmlBody = new TemplateResponse( - 'settings', - 'email.new_user', - [ - 'username' => 'user-id', - 'url' => null, - ], - 'blank' - ); - $message - ->expects($this->at(2)) - ->method('setHtmlBody') - ->with($htmlBody->render()); - $plainBody = new TemplateResponse( - 'settings', - 'email.new_user_plain_text', - [ - 'username' => 'user-id', - 'url' => null, - ], - 'blank' - ); - $message - ->expects($this->at(3)) - ->method('setPlainBody') - ->with($plainBody->render()); - $message - ->expects($this->at(4)) - ->method('setFrom') - ->with(['test@example.org' => null]); - - $this->mailer - ->expects($this->at(0)) - ->method('createMessage') - ->will($this->returnValue($message)); - $this->mailer - ->expects($this->at(1)) - ->method('send') - ->with($message); - $this->config ->expects($this->at(0)) ->method('getUserValue') @@ -3054,11 +2959,6 @@ public function testResendWelcomeMessageSuccessWithFallbackLanguage() { $l10n = $this->getMockBuilder(IL10N::class) ->disableOriginalConstructor() ->getMock(); - $l10n - ->expects($this->at(0)) - ->method('t') - ->with('Your %s account was created', [null]) - ->willReturn('Your account was created'); $this->l10nFactory ->expects($this->at(0)) ->method('languageExists') @@ -3069,6 +2969,19 @@ public function testResendWelcomeMessageSuccessWithFallbackLanguage() { ->method('get') ->with('settings', 'en') ->willReturn($l10n); + $emailTemplate = $this->createMock(IEMailTemplate::class); + $this->newUserMailHelper + ->expects($this->at(0)) + ->method('setL10N') + ->willReturn($l10n); + $this->newUserMailHelper + ->expects($this->at(1)) + ->method('generateTemplate') + ->willReturn($emailTemplate); + $this->newUserMailHelper + ->expects($this->at(2)) + ->method('sendMail') + ->with($targetUser, $emailTemplate); $this->api->resendWelcomeMessage('UserToGet'); } @@ -3113,56 +3026,6 @@ public function testResendWelcomeMessageFailed() { ->expects($this->once()) ->method('getEmailAddress') ->will($this->returnValue('abc@example.org')); - $message = $this->getMockBuilder('\OC\Mail\Message') - ->disableOriginalConstructor()->getMock(); - $message - ->expects($this->at(0)) - ->method('setTo') - ->with(['abc@example.org' => 'user-id']); - $message - ->expects($this->at(1)) - ->method('setSubject') - ->with('Your account was created'); - $htmlBody = new TemplateResponse( - 'settings', - 'email.new_user', - [ - 'username' => 'user-id', - 'url' => null, - ], - 'blank' - ); - $message - ->expects($this->at(2)) - ->method('setHtmlBody') - ->with($htmlBody->render()); - $plainBody = new TemplateResponse( - 'settings', - 'email.new_user_plain_text', - [ - 'username' => 'user-id', - 'url' => null, - ], - 'blank' - ); - $message - ->expects($this->at(3)) - ->method('setPlainBody') - ->with($plainBody->render()); - $message - ->expects($this->at(4)) - ->method('setFrom') - ->with(['test@example.org' => null]); - - $this->mailer - ->expects($this->at(0)) - ->method('createMessage') - ->will($this->returnValue($message)); - $this->mailer - ->expects($this->at(1)) - ->method('send') - ->will($this->throwException(new \Exception())); - $this->config ->expects($this->at(0)) ->method('getUserValue') @@ -3171,11 +3034,6 @@ public function testResendWelcomeMessageFailed() { $l10n = $this->getMockBuilder(IL10N::class) ->disableOriginalConstructor() ->getMock(); - $l10n - ->expects($this->at(0)) - ->method('t') - ->with('Your %s account was created', [null]) - ->willReturn('Your account was created'); $this->l10nFactory ->expects($this->at(0)) ->method('languageExists') @@ -3186,6 +3044,20 @@ public function testResendWelcomeMessageFailed() { ->method('get') ->with('settings', 'es') ->willReturn($l10n); + $emailTemplate = $this->createMock(IEMailTemplate::class); + $this->newUserMailHelper + ->expects($this->at(0)) + ->method('setL10N') + ->willReturn($l10n); + $this->newUserMailHelper + ->expects($this->at(1)) + ->method('generateTemplate') + ->willReturn($emailTemplate); + $this->newUserMailHelper + ->expects($this->at(2)) + ->method('sendMail') + ->with($targetUser, $emailTemplate) + ->willThrowException(new \Exception()); $this->api->resendWelcomeMessage('UserToGet'); } diff --git a/apps/theming/lib/ThemingDefaults.php b/apps/theming/lib/ThemingDefaults.php index 5a863b1eb22dd..d4dc56d3ba900 100644 --- a/apps/theming/lib/ThemingDefaults.php +++ b/apps/theming/lib/ThemingDefaults.php @@ -1,6 +1,7 @@ + * @copyright Copyright (c) 2017 Lukas Reschke * * @license GNU AGPL version 3 or any later version * @@ -19,15 +20,14 @@ * */ - namespace OCA\Theming; +use OCP\Files\IAppData; use OCP\ICacheFactory; use OCP\IConfig; use OCP\IL10N; use OCP\IURLGenerator; -use OCP\Files\IRootFolder; use OCP\Util; class ThemingDefaults extends \OC_Defaults { @@ -38,8 +38,8 @@ class ThemingDefaults extends \OC_Defaults { private $l; /** @var IURLGenerator */ private $urlGenerator; - /** @var IRootFolder */ - private $rootFolder; + /** @var IAppData */ + private $appData; /** @var ICacheFactory */ private $cacheFactory; /** @var string */ @@ -58,21 +58,21 @@ class ThemingDefaults extends \OC_Defaults { * @param IL10N $l * @param IURLGenerator $urlGenerator * @param \OC_Defaults $defaults - * @param IRootFolder $rootFolder + * @param IAppData $appData * @param ICacheFactory $cacheFactory */ public function __construct(IConfig $config, IL10N $l, IURLGenerator $urlGenerator, \OC_Defaults $defaults, - IRootFolder $rootFolder, + IAppData $appData, ICacheFactory $cacheFactory ) { parent::__construct(); $this->config = $config; $this->l = $l; $this->urlGenerator = $urlGenerator; - $this->rootFolder = $rootFolder; + $this->appData = $appData; $this->cacheFactory = $cacheFactory; $this->name = $defaults->getName(); @@ -130,11 +130,19 @@ public function getColorPrimary() { */ public function getLogo() { $logo = $this->config->getAppValue('theming', 'logoMime'); - if(!$logo || !$this->rootFolder->nodeExists('/themedinstancelogo')) { + + $logoExists = true; + try { + $this->appData->getFolder('images')->getFile('logo'); + } catch (\Exception $e) { + $logoExists = false; + } + + if(!$logo || !$logoExists) { return $this->urlGenerator->imagePath('core','logo.svg'); - } else { - return $this->urlGenerator->linkToRoute('theming.Theming.getLogo'); } + + return $this->urlGenerator->linkToRoute('theming.Theming.getLogo'); } /** @@ -144,11 +152,19 @@ public function getLogo() { */ public function getBackground() { $backgroundLogo = $this->config->getAppValue('theming', 'backgroundMime'); - if(!$backgroundLogo || !$this->rootFolder->nodeExists('/themedbackgroundlogo')) { + + $backgroundExists = true; + try { + $this->appData->getFolder('images')->getFile('background'); + } catch (\Exception $e) { + $backgroundExists = false; + } + + if(!$backgroundLogo || !$backgroundExists) { return $this->urlGenerator->imagePath('core','background.jpg'); - } else { - return $this->urlGenerator->linkToRoute('theming.Theming.getLoginBackground'); } + + return $this->urlGenerator->linkToRoute('theming.Theming.getLoginBackground'); } /** @@ -174,6 +190,15 @@ public function shouldReplaceIcons() { return $value; } + /** + * Gets the current cache buster count + * + * @return string + */ + public function getCacheBusterCounter() { + return $this->config->getAppValue('theming', 'cachebuster', '0'); + } + /** * Increases the cache buster key */ diff --git a/apps/theming/tests/ThemingDefaultsTest.php b/apps/theming/tests/ThemingDefaultsTest.php index 72ccaa57d7707..986b2f342674e 100644 --- a/apps/theming/tests/ThemingDefaultsTest.php +++ b/apps/theming/tests/ThemingDefaultsTest.php @@ -24,37 +24,36 @@ namespace OCA\Theming\Tests; use OCA\Theming\ThemingDefaults; +use OCP\Files\IAppData; +use OCP\Files\SimpleFS\ISimpleFolder; use OCP\ICacheFactory; use OCP\IConfig; use OCP\IL10N; use OCP\IURLGenerator; -use OCP\Files\IRootFolder; use Test\TestCase; class ThemingDefaultsTest extends TestCase { - /** @var IConfig */ + /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */ private $config; - /** @var IL10N */ + /** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */ private $l10n; - /** @var IURLGenerator */ + /** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */ private $urlGenerator; - /** @var \OC_Defaults */ + /** @var \OC_Defaults|\PHPUnit_Framework_MockObject_MockObject */ private $defaults; + /** @var IAppData|\PHPUnit_Framework_MockObject_MockObject */ + private $appData; + /** @var ICacheFactory|\PHPUnit_Framework_MockObject_MockObject */ + private $cacheFactory; /** @var ThemingDefaults */ private $template; - /** @var IRootFolder */ - private $rootFolder; - /** @var ICacheFactory */ - private $cacheFactory; public function setUp() { parent::setUp(); $this->config = $this->getMockBuilder(IConfig::class)->getMock(); $this->l10n = $this->getMockBuilder(IL10N::class)->getMock(); $this->urlGenerator = $this->getMockBuilder(IURLGenerator::class)->getMock(); - $this->rootFolder = $this->getMockBuilder(IRootFolder::class) - ->disableOriginalConstructor() - ->getMock(); + $this->appData = $this->createMock(IAppData::class); $this->cacheFactory = $this->getMockBuilder(ICacheFactory::class)->getMock(); $this->defaults = $this->getMockBuilder(\OC_Defaults::class) ->disableOriginalConstructor() @@ -80,7 +79,7 @@ public function setUp() { $this->l10n, $this->urlGenerator, $this->defaults, - $this->rootFolder, + $this->appData, $this->cacheFactory ); } @@ -386,6 +385,11 @@ public function testGetBackgroundDefault() { ->method('getAppValue') ->with('theming', 'backgroundMime') ->willReturn(''); + $this->appData + ->expects($this->once()) + ->method('getFolder') + ->with('images') + ->willThrowException(new \Exception()); $expected = $this->urlGenerator->imagePath('core','background.jpg'); $this->assertEquals($expected, $this->template->getBackground()); } @@ -396,6 +400,17 @@ public function testGetBackgroundCustom() { ->method('getAppValue') ->with('theming', 'backgroundMime') ->willReturn('image/svg+xml'); + $simpleFolder = $this->createMock(ISimpleFolder::class); + $this->appData + ->expects($this->once()) + ->method('getFolder') + ->with('images') + ->willReturn($simpleFolder); + $simpleFolder + ->expects($this->once()) + ->method('getFile') + ->with('background') + ->willReturn(''); $expected = $this->urlGenerator->linkToRoute('theming.Theming.getLoginBackground'); $this->assertEquals($expected, $this->template->getBackground()); } @@ -406,6 +421,11 @@ public function testGetLogoDefault() { ->method('getAppValue') ->with('theming', 'logoMime') ->willReturn(''); + $this->appData + ->expects($this->once()) + ->method('getFolder') + ->with('images') + ->willThrowException(new \Exception()); $expected = $this->urlGenerator->imagePath('core','logo.svg'); $this->assertEquals($expected, $this->template->getLogo()); } @@ -416,6 +436,17 @@ public function testGetLogoCustom() { ->method('getAppValue') ->with('theming', 'logoMime') ->willReturn('image/svg+xml'); + $simpleFolder = $this->createMock(ISimpleFolder::class); + $this->appData + ->expects($this->once()) + ->method('getFolder') + ->with('images') + ->willReturn($simpleFolder); + $simpleFolder + ->expects($this->once()) + ->method('getFile') + ->with('logo') + ->willReturn(''); $expected = $this->urlGenerator->linkToRoute('theming.Theming.getLogo'); $this->assertEquals($expected, $this->template->getLogo()); } diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 915f5229280b1..fd73a9561a1eb 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -637,6 +637,8 @@ 'OC\\Log\\File' => $baseDir . '/lib/private/Log/File.php', 'OC\\Log\\Rotate' => $baseDir . '/lib/private/Log/Rotate.php', 'OC\\Log\\Syslog' => $baseDir . '/lib/private/Log/Syslog.php', + 'OC\\Mail\\EMailTemplate' => $baseDir . '/lib/private/Mail/EMailTemplate.php', + 'OC\\Mail\\IEMailTemplate' => $baseDir . '/lib/private/Mail/IEMailTemplate.php', 'OC\\Mail\\Mailer' => $baseDir . '/lib/private/Mail/Mailer.php', 'OC\\Mail\\Message' => $baseDir . '/lib/private/Mail/Message.php', 'OC\\Memcache\\APCu' => $baseDir . '/lib/private/Memcache/APCu.php', @@ -769,6 +771,7 @@ 'OC\\Settings\\Controller\\PersonalController' => $baseDir . '/settings/Controller/PersonalController.php', 'OC\\Settings\\Controller\\SecuritySettingsController' => $baseDir . '/settings/Controller/SecuritySettingsController.php', 'OC\\Settings\\Controller\\UsersController' => $baseDir . '/settings/Controller/UsersController.php', + 'OC\\Settings\\Mailer\\NewUserMailHelper' => $baseDir . '/settings/Mailer/NewUserMailHelper.php', 'OC\\Settings\\Manager' => $baseDir . '/lib/private/Settings/Manager.php', 'OC\\Settings\\Mapper' => $baseDir . '/lib/private/Settings/Mapper.php', 'OC\\Settings\\Middleware\\SubadminMiddleware' => $baseDir . '/settings/Middleware/SubadminMiddleware.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index a4dc2dbb48967..9fe795cfb0717 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -667,6 +667,8 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c 'OC\\Log\\File' => __DIR__ . '/../../..' . '/lib/private/Log/File.php', 'OC\\Log\\Rotate' => __DIR__ . '/../../..' . '/lib/private/Log/Rotate.php', 'OC\\Log\\Syslog' => __DIR__ . '/../../..' . '/lib/private/Log/Syslog.php', + 'OC\\Mail\\EMailTemplate' => __DIR__ . '/../../..' . '/lib/private/Mail/EMailTemplate.php', + 'OC\\Mail\\IEMailTemplate' => __DIR__ . '/../../..' . '/lib/private/Mail/IEMailTemplate.php', 'OC\\Mail\\Mailer' => __DIR__ . '/../../..' . '/lib/private/Mail/Mailer.php', 'OC\\Mail\\Message' => __DIR__ . '/../../..' . '/lib/private/Mail/Message.php', 'OC\\Memcache\\APCu' => __DIR__ . '/../../..' . '/lib/private/Memcache/APCu.php', @@ -799,6 +801,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c 'OC\\Settings\\Controller\\PersonalController' => __DIR__ . '/../../..' . '/settings/Controller/PersonalController.php', 'OC\\Settings\\Controller\\SecuritySettingsController' => __DIR__ . '/../../..' . '/settings/Controller/SecuritySettingsController.php', 'OC\\Settings\\Controller\\UsersController' => __DIR__ . '/../../..' . '/settings/Controller/UsersController.php', + 'OC\\Settings\\Mailer\\NewUserMailHelper' => __DIR__ . '/../../..' . '/settings/Mailer/NewUserMailHelper.php', 'OC\\Settings\\Manager' => __DIR__ . '/../../..' . '/lib/private/Settings/Manager.php', 'OC\\Settings\\Mapper' => __DIR__ . '/../../..' . '/lib/private/Settings/Mapper.php', 'OC\\Settings\\Middleware\\SubadminMiddleware' => __DIR__ . '/../../..' . '/settings/Middleware/SubadminMiddleware.php', diff --git a/lib/private/Mail/EMailTemplate.php b/lib/private/Mail/EMailTemplate.php new file mode 100644 index 0000000000000..7d3ca51c50e69 --- /dev/null +++ b/lib/private/Mail/EMailTemplate.php @@ -0,0 +1,408 @@ + + * @copyright 2017, Lukas Reschke + * + * @author Morris Jobke + * @author Lukas Reschke + * + * @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 OC\Mail; + +use OCA\Theming\ThemingDefaults; +use OCP\IL10N; +use OCP\IURLGenerator; + +/** + * Class EMailTemplate + * + * addBodyText and addBodyButtonGroup automatically opens the body + * addFooter, renderHTML, renderText automatically closes the body and the HTML if opened + * + * @package OC\Mail + */ +class EMailTemplate implements IEMailTemplate { + /** @var ThemingDefaults */ + protected $themingDefaults; + /** @var IURLGenerator */ + protected $urlGenerator; + /** @var IL10N */ + protected $l10n; + + /** @var string */ + protected $htmlBody = ''; + /** @var string */ + protected $plainBody = ''; + /** @var bool indicated if the footer is added */ + protected $headerAdded = false; + /** @var bool indicated if the body is already opened */ + protected $bodyOpened = false; + /** @var bool indicated if the footer is added */ + protected $footerAdded = false; + + protected $head = << + + + + + + + + + + + + + +
+
+EOF; + + protected $tail = << +
+ +
                                                           
+ + +EOF; + + protected $header = << + + + + + + + + +
+ + + +
+ +
+ + +
+
+ + + + + + + + + +
 
+EOF; + + protected $heading = << + + + +

%s

+ + + + + + + + + + +
 
+EOF; + + protected $bodyBegin = << + + + + + + + + + + +
+ + + + + + +
 
+EOF; + + protected $bodyText = << +
+ + + + + +
+

%s

+
+
+EOF; + + protected $buttonGroup = << + + +   + + + + + + + + + +
+ + + + + +
+
+ + + + +
+ + + + +
+ %s +
+
+ + + + +
+ + + + +
+ %s +
+
+
+
+
+EOF; + + protected $bodyEnd = << + + + + + + +EOF; + + protected $footer = << + + +   + + + + + + + + +EOF; + + /** + * @param ThemingDefaults $themingDefaults + * @param IURLGenerator $urlGenerator + * @param IL10N $l10n + */ + public function __construct(ThemingDefaults $themingDefaults, + IURLGenerator $urlGenerator, + IL10N $l10n) { + $this->themingDefaults = $themingDefaults; + $this->urlGenerator = $urlGenerator; + $this->l10n = $l10n; + $this->htmlBody .= $this->head; + } + + /** + * Adds a header to the email + */ + public function addHeader() { + if ($this->headerAdded) { + return; + } + $this->headerAdded = true; + + $logoUrl = $this->urlGenerator->getAbsoluteURL($this->themingDefaults->getLogo()) . '?v='. $this->themingDefaults->getCacheBusterCounter(); + $this->htmlBody .= vsprintf($this->header, [$this->themingDefaults->getColorPrimary(), $logoUrl]); + } + + /** + * Adds a heading to the email + * + * @param string $title + */ + public function addHeading($title) { + if ($this->footerAdded) { + return; + } + + $this->htmlBody .= vsprintf($this->heading, [$title]); + $this->plainBody .= $title . PHP_EOL . PHP_EOL; + } + + /** + * Adds a paragraph to the body of the email + * + * @param string $text + */ + public function addBodyText($text) { + if ($this->footerAdded) { + return; + } + + if (!$this->bodyOpened) { + $this->htmlBody .= $this->bodyBegin; + $this->bodyOpened = true; + } + + $this->htmlBody .= vsprintf($this->bodyText, [$text]); + $this->plainBody .= $text . PHP_EOL . PHP_EOL; + } + + /** + * Adds a button group of two buttons to the body of the email + * + * @param string $textLeft Text of left button + * @param string $urlLeft URL of left button + * @param string $textRight Text of right button + * @param string $urlRight URL of right button + */ + public function addBodyButtonGroup($textLeft, $urlLeft, $textRight, $urlRight) { + if ($this->footerAdded) { + return; + } + + if (!$this->bodyOpened) { + $this->htmlBody .= $this->bodyBegin; + $this->bodyOpened = true; + } + + $color = $this->themingDefaults->getColorPrimary(); + $this->htmlBody .= vsprintf($this->buttonGroup, [$color, $color, $urlLeft, $color, $textLeft, $urlRight, $textRight]); + $this->plainBody .= $textLeft . ': ' . $urlLeft . PHP_EOL; + $this->plainBody .= $textRight . ': ' . $urlRight . PHP_EOL . PHP_EOL; + + } + + /** + * Adds a logo and a text to the footer.
in the text will be replaced by new lines in the plain text email + * + * @param string $text + */ + public function addFooter($text = '') { + if($text === '') { + $text = $this->themingDefaults->getName() . ' - ' . $this->themingDefaults->getSlogan() . '
' . $this->l10n->t('This is an automatically generated email, please do not reply.'); + } + + if ($this->footerAdded) { + return; + } + $this->footerAdded = true; + + if ($this->bodyOpened) { + $this->htmlBody .= $this->bodyEnd; + $this->bodyOpened = false; + } + + $this->htmlBody .= vsprintf($this->footer, [$text]); + $this->htmlBody .= $this->tail; + $this->plainBody .= '--' . PHP_EOL; + $this->plainBody .= str_replace('
', PHP_EOL, $text); + } + + /** + * Returns the rendered HTML email as string + * + * @return string + */ + public function renderHTML() { + if (!$this->footerAdded) { + $this->footerAdded = true; + if ($this->bodyOpened) { + $this->htmlBody .= $this->bodyEnd; + } + $this->htmlBody .= $this->tail; + } + return $this->htmlBody; + } + + /** + * Returns the rendered plain text email as string + * + * @return string + */ + public function renderText() { + if (!$this->footerAdded) { + $this->footerAdded = true; + if ($this->bodyOpened) { + $this->htmlBody .= $this->bodyEnd; + } + $this->htmlBody .= $this->tail; + } + return $this->plainBody; + } +} diff --git a/lib/private/Mail/IEMailTemplate.php b/lib/private/Mail/IEMailTemplate.php new file mode 100644 index 0000000000000..0d660a3551671 --- /dev/null +++ b/lib/private/Mail/IEMailTemplate.php @@ -0,0 +1,110 @@ + + * + * @author Morris Jobke + * + * @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 OC\Mail; + +/** + * Interface IEMailTemplate + * + * Interface to a class that allows to build HTML emails + * + * Example: + * + * defaults); + * + * $emailTemplate->addHeader(); + * $emailTemplate->addHeading('Welcome aboard'); + * $emailTemplate->addBodyText('You have now an Nextcloud account, you can add, protect, and share your data.'); + * + * $emailTemplate->addBodyButtonGroup( + * 'Set your password', 'https://example.org/resetPassword/q1234567890qwertz', + * 'Install Client', 'https://nextcloud.com/install/#install-clients' + * ); + * + * $emailTemplate->addFooter('Optional footer text'); + * + * $htmlContent = $emailTemplate->renderHTML(); + * $plainContent = $emailTemplate->renderText(); + */ +interface IEMailTemplate { + /** + * @param \OCA\Theming\ThemingDefaults $themingDefaults + * @param \OCP\IURLGenerator $urlGenerator + * @param \OCP\IL10N $l10n + */ + public function __construct(\OCA\Theming\ThemingDefaults $themingDefaults, + \OCP\IURLGenerator $urlGenerator, + \OCP\IL10N $l10n); + + /** + * Adds a header to the email + */ + public function addHeader(); + + /** + * Adds a heading to the email + * + * @param string $title + */ + public function addHeading($title); + + /** + * Adds a paragraph to the body of the email + * + * @param string $text + */ + public function addBodyText($text); + + /** + * Adds a button group of two buttons to the body of the email + * + * @param string $textLeft Text of left button + * @param string $urlLeft URL of left button + * @param string $textRight Text of right button + * @param string $urlRight URL of right button + */ + public function addBodyButtonGroup($textLeft, $urlLeft, $textRight, $urlRight); + + /** + * Adds a logo and a text to the footer.
in the text will be replaced by new lines in the plain text email + * + * @param string $text + */ + public function addFooter($text = ''); + + /** + * Returns the rendered HTML email as string + * + * @return string + */ + public function renderHTML(); + + /** + * Returns the rendered plain text email as string + * + * @return string + */ + public function renderText(); +} diff --git a/lib/private/Server.php b/lib/private/Server.php index 98910b097b719..0d05bfe0dc8b0 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -823,7 +823,7 @@ public function __construct($webRoot, \OC\Config $config) { $c->getL10N('theming'), $c->getURLGenerator(), new \OC_Defaults(), - $c->getLazyRootFolder(), + $c->getAppDataDir('theming'), $c->getMemCacheFactory() ); } @@ -1572,7 +1572,7 @@ public function getCommentsManager() { } /** - * @return \OC_Defaults + * @return \OCA\Theming\ThemingDefaults */ public function getThemingDefaults() { return $this->query('ThemingDefaults'); diff --git a/settings/Application.php b/settings/Application.php index 44747c2f6817e..bf149def94d95 100644 --- a/settings/Application.php +++ b/settings/Application.php @@ -35,7 +35,10 @@ use OC\AppFramework\Utility\TimeFactory; use OC\Authentication\Token\IProvider; use OC\Server; +use OC\ServerContainer; +use OC\Settings\Mailer\NewUserMailHelper; use OC\Settings\Middleware\SubadminMiddleware; +use OCA\Theming\ThemingDefaults; use OCP\AppFramework\App; use OCP\IContainer; use OCP\Settings\IManager; @@ -75,9 +78,6 @@ public function __construct(array $urlParams=[]){ } return $isSubAdmin; }); - $container->registerService('fromMailAddress', function() { - return Util::getDefaultEmailAddress('no-reply'); - }); $container->registerService('userCertificateManager', function(IContainer $c) { return $c->query('ServerContainer')->getCertificateManager(); }, false); @@ -90,6 +90,23 @@ public function __construct(array $urlParams=[]){ $container->registerService(IManager::class, function (IContainer $c) { return $c->query('ServerContainer')->getSettingsManager(); }); + + $container->registerService(NewUserMailHelper::class, function (IContainer $c) { + /** @var Server $server */ + $server = $c->query('ServerContainer'); + + return new NewUserMailHelper( + $server->getThemingDefaults(), + $server->getURLGenerator(), + $server->getL10N('settings'), + $server->getMailer(), + $server->getSecureRandom(), + new TimeFactory(), + $server->getConfig(), + $server->getCrypto(), + Util::getDefaultEmailAddress('no-reply') + ); + }); $container->registerService(AppFetcher::class, function (IContainer $c) { /** @var Server $server */ $server = $c->query('ServerContainer'); diff --git a/settings/Controller/UsersController.php b/settings/Controller/UsersController.php index 19c5b0681673c..b42d4faa56993 100644 --- a/settings/Controller/UsersController.php +++ b/settings/Controller/UsersController.php @@ -33,11 +33,10 @@ use OC\Accounts\AccountManager; use OC\AppFramework\Http; use OC\ForbiddenException; -use OC\User\User; +use OC\Settings\Mailer\NewUserMailHelper; use OCP\App\IAppManager; use OCP\AppFramework\Controller; use OCP\AppFramework\Http\DataResponse; -use OCP\AppFramework\Http\TemplateResponse; use OCP\IConfig; use OCP\IGroupManager; use OCP\IL10N; @@ -49,9 +48,7 @@ use OCP\IUserSession; use OCP\Mail\IMailer; use OCP\IAvatarManager; -use OCP\Security\ICrypto; use OCP\Security\ISecureRandom; -use OCP\AppFramework\Utility\ITimeFactory; /** * @package OC\Settings\Controller @@ -71,14 +68,8 @@ class UsersController extends Controller { private $config; /** @var ILogger */ private $log; - /** @var \OC_Defaults */ - private $defaults; /** @var IMailer */ private $mailer; - /** @var string */ - private $fromMailAddress; - /** @var IURLGenerator */ - private $urlGenerator; /** @var bool contains the state of the encryption app */ private $isEncryptionAppEnabled; /** @var bool contains the state of the admin recovery setting */ @@ -89,11 +80,8 @@ class UsersController extends Controller { private $accountManager; /** @var ISecureRandom */ private $secureRandom; - /** @var ITimeFactory */ - private $timeFactory; - /** @var ICrypto */ - private $crypto; - + /** @var NewUserMailHelper */ + private $newUserMailHelper; /** * @param string $appName @@ -105,16 +93,13 @@ class UsersController extends Controller { * @param bool $isAdmin * @param IL10N $l10n * @param ILogger $log - * @param \OC_Defaults $defaults * @param IMailer $mailer - * @param string $fromMailAddress * @param IURLGenerator $urlGenerator * @param IAppManager $appManager * @param IAvatarManager $avatarManager * @param AccountManager $accountManager * @param ISecureRandom $secureRandom - * @param ITimeFactory $timeFactory - * @param ICrypto $crypto + * @param NewUserMailHelper $newUserMailHelper */ public function __construct($appName, IRequest $request, @@ -125,16 +110,13 @@ public function __construct($appName, $isAdmin, IL10N $l10n, ILogger $log, - \OC_Defaults $defaults, IMailer $mailer, - $fromMailAddress, IURLGenerator $urlGenerator, IAppManager $appManager, IAvatarManager $avatarManager, AccountManager $accountManager, ISecureRandom $secureRandom, - ITimeFactory $timeFactory, - ICrypto $crypto) { + NewUserMailHelper $newUserMailHelper) { parent::__construct($appName, $request); $this->userManager = $userManager; $this->groupManager = $groupManager; @@ -143,15 +125,11 @@ public function __construct($appName, $this->isAdmin = $isAdmin; $this->l10n = $l10n; $this->log = $log; - $this->defaults = $defaults; $this->mailer = $mailer; - $this->fromMailAddress = $fromMailAddress; - $this->urlGenerator = $urlGenerator; $this->avatarManager = $avatarManager; $this->accountManager = $accountManager; $this->secureRandom = $secureRandom; - $this->timeFactory = $timeFactory; - $this->crypto = $crypto; + $this->newUserMailHelper = $newUserMailHelper; // check for encryption state - TODO see formatUserForIndex $this->isEncryptionAppEnabled = $appManager->isEnabledForUser('encryption'); @@ -379,7 +357,7 @@ public function create($username, $password, array $groups=array(), $email='') { ); } - $generatedPassword = false; + $generatePasswordResetToken = false; if ($password === '') { if ($email === '') { return new DataResponse( @@ -391,7 +369,7 @@ public function create($username, $password, array $groups=array(), $email='') { } $password = $this->secureRandom->generate(32); - $generatedPassword = true; + $generatePasswordResetToken = true; } try { @@ -425,46 +403,9 @@ public function create($username, $password, array $groups=array(), $email='') { */ if($email !== '') { $user->setEMailAddress($email); - - if ($generatedPassword) { - $token = $this->secureRandom->generate( - 21, - ISecureRandom::CHAR_DIGITS . - ISecureRandom::CHAR_LOWER . - ISecureRandom::CHAR_UPPER - ); - $tokenValue = $this->timeFactory->getTime() . ':' . $token; - $mailAddress = !is_null($user->getEMailAddress()) ? $user->getEMailAddress() : ''; - $encryptedValue = $this->crypto->encrypt($tokenValue, $mailAddress . $this->config->getSystemValue('secret')); - $this->config->setUserValue($username, 'core', 'lostpassword', $encryptedValue); - - $link = $this->urlGenerator->linkToRouteAbsolute('core.lost.resetform', ['userId' => $username, 'token' => $token]); - } else { - $link = $this->urlGenerator->getAbsoluteURL('/'); - } - - // data for the mail template - $mailData = array( - 'username' => $username, - 'url' => $link - ); - - $mail = new TemplateResponse('settings', 'email.new_user', $mailData, 'blank'); - $mailContent = $mail->render(); - - $mail = new TemplateResponse('settings', 'email.new_user_plain_text', $mailData, 'blank'); - $plainTextMailContent = $mail->render(); - - $subject = $this->l10n->t('Your %s account was created', [$this->defaults->getName()]); - try { - $message = $this->mailer->createMessage(); - $message->setTo([$email => $username]); - $message->setSubject($subject); - $message->setHtmlBody($mailContent); - $message->setPlainBody($plainTextMailContent); - $message->setFrom([$this->fromMailAddress => $this->defaults->getName()]); - $this->mailer->send($message); + $emailTemplate = $this->newUserMailHelper->generateTemplate($user, $generatePasswordResetToken); + $this->newUserMailHelper->sendMail($user, $emailTemplate); } catch(\Exception $e) { $this->log->error("Can't send new user mail to $email: " . $e->getMessage(), array('app' => 'settings')); } diff --git a/settings/Mailer/NewUserMailHelper.php b/settings/Mailer/NewUserMailHelper.php new file mode 100644 index 0000000000000..15b912f803a86 --- /dev/null +++ b/settings/Mailer/NewUserMailHelper.php @@ -0,0 +1,166 @@ + + * + * @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 OC\Settings\Mailer; + +use OC\Mail\EMailTemplate; +use OC\Mail\IEMailTemplate; +use OCA\Theming\ThemingDefaults; +use OCP\AppFramework\Utility\ITimeFactory; +use OCP\IConfig; +use OCP\IL10N; +use OCP\IURLGenerator; +use OCP\IUser; +use OCP\Mail\IMailer; +use OCP\Security\ICrypto; +use OCP\Security\ISecureRandom; + +class NewUserMailHelper { + /** @var ThemingDefaults */ + private $themingDefaults; + /** @var IURLGenerator */ + private $urlGenerator; + /** @var IL10N */ + private $l10n; + /** @var IMailer */ + private $mailer; + /** @var ISecureRandom */ + private $secureRandom; + /** @var ITimeFactory */ + private $timeFactory; + /** @var IConfig */ + private $config; + /** @var ICrypto */ + private $crypto; + /** @var string */ + private $fromAddress; + + /** + * @param ThemingDefaults $themingDefaults + * @param IURLGenerator $urlGenerator + * @param IL10N $l10n + * @param IMailer $mailer + * @param ISecureRandom $secureRandom + * @param ITimeFactory $timeFactory + * @param IConfig $config + * @param ICrypto $crypto + * @param string $fromAddress + */ + public function __construct(ThemingDefaults $themingDefaults, + IURLGenerator $urlGenerator, + IL10N $l10n, + IMailer $mailer, + ISecureRandom $secureRandom, + ITimeFactory $timeFactory, + IConfig $config, + ICrypto $crypto, + $fromAddress) { + $this->themingDefaults = $themingDefaults; + $this->urlGenerator = $urlGenerator; + $this->l10n = $l10n; + $this->mailer = $mailer; + $this->secureRandom = $secureRandom; + $this->timeFactory = $timeFactory; + $this->config = $config; + $this->crypto = $crypto; + $this->fromAddress = $fromAddress; + } + + /** + * Set the IL10N object + * + * @param IL10N $l10n + */ + public function setL10N(IL10N $l10n) { + $this->l10n = $l10n; + } + + /** + * @param IUser $user + * @param bool $generatePasswordResetToken + * @return EMailTemplate + */ + public function generateTemplate(IUser $user, $generatePasswordResetToken = false) { + if ($generatePasswordResetToken) { + $token = $this->secureRandom->generate( + 21, + ISecureRandom::CHAR_DIGITS . + ISecureRandom::CHAR_LOWER . + ISecureRandom::CHAR_UPPER + ); + $tokenValue = $this->timeFactory->getTime() . ':' . $token; + $mailAddress = (null !== $user->getEMailAddress()) ? $user->getEMailAddress() : ''; + $encryptedValue = $this->crypto->encrypt($tokenValue, $mailAddress . $this->config->getSystemValue('secret')); + $this->config->setUserValue($user->getUID(), 'core', 'lostpassword', $encryptedValue); + $link = $this->urlGenerator->linkToRouteAbsolute('core.lost.resetform', ['userId' => $user->getUID(), 'token' => $token]); + } else { + $link = $this->urlGenerator->getAbsoluteURL('/'); + } + + $emailTemplate = new EMailTemplate( + $this->themingDefaults, + $this->urlGenerator, + $this->l10n + ); + $emailTemplate->addHeader(); + $displayName = $user->getDisplayName(); + $userName = $user->getUID(); + if ($displayName === $userName) { + $emailTemplate->addHeading($this->l10n->t('Welcome aboard')); + } else { + $emailTemplate->addHeading($this->l10n->t('Welcome aboard %s', [$displayName])); + } + $emailTemplate->addBodyText($this->l10n->t('You have now an %s account, you can add, protect, and share your data.', [$this->themingDefaults->getName()])); + $emailTemplate->addBodyText($this->l10n->t('Your username is: %s', [$userName])); + if ($generatePasswordResetToken) { + $leftButtonText = $this->l10n->t('Set your password'); + } else { + $leftButtonText = $this->l10n->t('Go to %s', [$this->themingDefaults->getName()]); + } + $emailTemplate->addBodyButtonGroup( + $leftButtonText, + $link, + $this->l10n->t('Install Client'), + 'https://nextcloud.com/install/#install-clients' + ); + $emailTemplate->addFooter(); + + return $emailTemplate; + } + + /** + * Sends a welcome mail to $user + * + * @param IUser $user + * @param IEmailTemplate $emailTemplate + * @throws \Exception If mail could not be sent + */ + public function sendMail(IUser $user, + IEMailTemplate $emailTemplate) { + $message = $this->mailer->createMessage(); + $message->setTo([$user->getEMailAddress() => $user->getDisplayName()]); + $message->setSubject($this->l10n->t('Your %s account was created', [$this->themingDefaults->getName()])); + $message->setHtmlBody($emailTemplate->renderHTML()); + $message->setPlainBody($emailTemplate->renderText()); + $message->setFrom([$this->fromAddress => $this->themingDefaults->getName()]); + $this->mailer->send($message); + } +} diff --git a/settings/templates/email.new_user.php b/settings/templates/email.new_user.php deleted file mode 100644 index 9418c21c6cb0c..0000000000000 --- a/settings/templates/email.new_user.php +++ /dev/null @@ -1,35 +0,0 @@ - - -
- - - - - - - - - - - - - - - - - -
- <?php p($theme->getName()); ?> -
 
  - t('Hey there,

just letting you know that you now have a %s account.

Your username: %s
Access it: %s

', array($theme->getName(), $_['username'], $_['url'], $_['url']))); - - // TRANSLATORS term at the end of a mail - p($l->t('Cheers!')); - ?> -
 
 --
- getName()); ?> - - getSlogan()); ?> -
getBaseUrl());?> -
 
-
diff --git a/settings/templates/email.new_user_plain_text.php b/settings/templates/email.new_user_plain_text.php deleted file mode 100644 index 2cc70ddfa2fca..0000000000000 --- a/settings/templates/email.new_user_plain_text.php +++ /dev/null @@ -1,10 +0,0 @@ -t("Hey there,\n\njust letting you know that you now have a %s account.\n\nYour username: %s\nAccess it: %s\n\n", array($theme->getName(), $_['username'], $_['url']))); - -// TRANSLATORS term at the end of a mail -p($l->t("Cheers!")); -?> - - -- -getName() . ' - ' . $theme->getSlogan()); ?> -getBaseUrl()); diff --git a/tests/Settings/Controller/UsersControllerTest.php b/tests/Settings/Controller/UsersControllerTest.php index 3cdde1b2d2768..d7961e2332b89 100644 --- a/tests/Settings/Controller/UsersControllerTest.php +++ b/tests/Settings/Controller/UsersControllerTest.php @@ -12,7 +12,9 @@ use OC\Accounts\AccountManager; use OC\Group\Manager; +use OC\Mail\IEMailTemplate; use OC\Settings\Controller\UsersController; +use OC\Settings\Mailer\NewUserMailHelper; use OCP\App\IAppManager; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; @@ -68,6 +70,8 @@ class UsersControllerTest extends \Test\TestCase { private $secureRandom; /** @var ITimeFactory | \PHPUnit_Framework_MockObject_MockObject */ private $timeFactory; + /** @var NewUserMailHelper|\PHPUnit_Framework_MockObject_MockObject */ + private $newUserMailHelper; /** @var ICrypto | \PHPUnit_Framework_MockObject_MockObject */ private $crypto; @@ -79,7 +83,6 @@ protected function setUp() { $this->userSession = $this->createMock(IUserSession::class); $this->config = $this->createMock(IConfig::class); $this->logger = $this->createMock(ILogger::class); - $this->defaults = $this->createMock(\OC_Defaults::class); $this->mailer = $this->createMock(IMailer::class); $this->urlGenerator = $this->createMock(IURLGenerator::class); $this->appManager = $this->createMock(IAppManager::class); @@ -88,6 +91,7 @@ protected function setUp() { $this->secureRandom = $this->createMock(ISecureRandom::class); $this->timeFactory = $this->createMock(ITimeFactory::class); $this->crypto = $this->createMock(ICrypto::class); + $this->newUserMailHelper = $this->createMock(NewUserMailHelper::class); $this->l = $this->createMock(IL10N::class); $this->l->method('t') ->will($this->returnCallback(function ($text, $parameters = []) { @@ -126,16 +130,13 @@ protected function getController($isAdmin = false, $mockedMethods = []) { $isAdmin, $this->l, $this->logger, - $this->defaults, $this->mailer, - 'no-reply@owncloud.com', $this->urlGenerator, $this->appManager, $this->avatarManager, $this->accountManager, $this->secureRandom, - $this->timeFactory, - $this->crypto + $this->newUserMailHelper ); } else { return $this->getMockBuilder(UsersController::class) @@ -150,16 +151,13 @@ protected function getController($isAdmin = false, $mockedMethods = []) { $isAdmin, $this->l, $this->logger, - $this->defaults, $this->mailer, - 'no-reply@owncloud.com', $this->urlGenerator, $this->appManager, $this->avatarManager, $this->accountManager, $this->secureRandom, - $this->timeFactory, - $this->crypto + $this->newUserMailHelper ] )->setMethods($mockedMethods)->getMock(); } @@ -1417,60 +1415,11 @@ public function testCreateUnsuccessfulWithInvalidEmailAdmin() { */ public function testCreateSuccessfulWithValidEmailAdmin() { $controller = $this->getController(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->mailer ->expects($this->at(0)) ->method('validateMailAddress') ->with('validMail@Adre.ss') ->will($this->returnValue(true)); - $this->mailer - ->expects($this->at(1)) - ->method('createMessage') - ->will($this->returnValue($message)); - $this->mailer - ->expects($this->at(2)) - ->method('send') - ->with($message); $user = $this->getMockBuilder('\OC\User\User') ->disableOriginalConstructor()->getMock(); @@ -1483,11 +1432,25 @@ public function testCreateSuccessfulWithValidEmailAdmin() { $user ->method('getUID') ->will($this->returnValue('foo')); + $user + ->method('getDisplayName') + ->will($this->returnValue('foo')); $user ->expects($this->once()) ->method('getBackendClassName') ->will($this->returnValue('bar')); + $emailTemplate = $this->createMock(IEMailTemplate::class); + $this->newUserMailHelper + ->expects($this->at(0)) + ->method('generateTemplate') + ->with($user, false) + ->willReturn($emailTemplate); + $this->newUserMailHelper + ->expects($this->at(1)) + ->method('sendMail') + ->with($user, $emailTemplate); + $this->userManager ->expects($this->once()) ->method('createUser') @@ -2262,20 +2225,24 @@ public function testCreateUnsuccessfulWithoutPasswordAndEmail() { public function testCreateSuccessfulWithoutPasswordAndWithEmail() { - $controller = $this->getController(true); - $user = $this->getMockBuilder('\OC\User\User') ->disableOriginalConstructor()->getMock(); $user ->method('getHome') - ->will($this->returnValue('/home/user')); + ->willReturn('/home/user'); $user ->method('getUID') - ->will($this->returnValue('foo')); + ->willReturn('foo'); + $user + ->method('getDisplayName') + ->willReturn('John Doe'); + $user + ->method('getEmailAddress') + ->willReturn('abc@example.org'); $user ->expects($this->once()) ->method('getBackendClassName') - ->will($this->returnValue('bar')); + ->willReturn('bar'); $this->userManager ->expects($this->once()) @@ -2295,95 +2262,37 @@ public function testCreateSuccessfulWithoutPasswordAndWithEmail() { ->method('getSubAdmin') ->will($this->returnValue($subadmin)); - $this->secureRandom - ->expects($this->at(0)) - ->method('generate') - ->with(32) - ->will($this->returnValue('abc123')); - $this->secureRandom - ->expects($this->at(1)) - ->method('generate') - ->with(21, - ISecureRandom::CHAR_DIGITS . - ISecureRandom::CHAR_LOWER . - ISecureRandom::CHAR_UPPER) - ->will($this->returnValue('mytoken')); - $this->urlGenerator - ->expects($this->once()) - ->method('linkToRouteAbsolute') - ->with('core.lost.resetform', ['userId' => 'foo', 'token' => 'mytoken']) - ->will($this->returnValue('link-with-my-token')); - $controller = $this->getController(true); - $message = $this->getMockBuilder('\OC\Mail\Message') - ->disableOriginalConstructor()->getMock(); - $message - ->expects($this->at(0)) - ->method('setTo') - ->with(['abc@example.org' => 'foo']); - $message - ->expects($this->at(1)) - ->method('setSubject') - ->with('Your account was created'); - $htmlBody = new Http\TemplateResponse( - 'settings', - 'email.new_user', - [ - 'username' => 'foo', - 'url' => 'link-with-my-token', - ], - 'blank' - ); - $message - ->expects($this->at(2)) - ->method('setHtmlBody') - ->with($htmlBody->render()); - $plainBody = new Http\TemplateResponse( - 'settings', - 'email.new_user_plain_text', - [ - 'username' => 'foo', - 'url' => 'link-with-my-token', - ], - '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->mailer ->expects($this->at(0)) ->method('validateMailAddress') ->with('abc@example.org') ->will($this->returnValue(true)); - $this->mailer + $emailTemplate = $this->createMock(IEMailTemplate::class); + $this->newUserMailHelper + ->expects($this->at(0)) + ->method('generateTemplate') + ->with($user, true) + ->willReturn($emailTemplate); + $this->newUserMailHelper ->expects($this->at(1)) - ->method('createMessage') - ->will($this->returnValue($message)); - $this->mailer - ->expects($this->at(2)) - ->method('send') - ->with($message); + ->method('sendMail') + ->with($user, $emailTemplate); $expectedResponse = new DataResponse( - array( + [ 'name' => 'foo', 'groups' => null, 'storageLocation' => '/home/user', 'backend' => 'bar', - 'lastLogin' => null, - 'displayname' => null, + 'lastLogin' => 0, + 'displayname' => 'John Doe', 'quota' => null, 'subadmin' => array(), - 'email' => null, + 'email' => 'abc@example.org', 'isRestoreDisabled' => false, 'isAvatarAvailable' => true, - ), + ], Http::STATUS_CREATED ); $response = $controller->create('foo', '', array(), 'abc@example.org'); diff --git a/tests/Settings/Mailer/NewUserMailHelperTest.php b/tests/Settings/Mailer/NewUserMailHelperTest.php new file mode 100644 index 0000000000000..e77e8e197b8f8 --- /dev/null +++ b/tests/Settings/Mailer/NewUserMailHelperTest.php @@ -0,0 +1,639 @@ + + * + * @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 OC\Mail\IEMailTemplate; +use OC\Mail\Message; +use OC\Settings\Mailer\NewUserMailHelper; +use OCA\Theming\ThemingDefaults; +use OCP\AppFramework\Utility\ITimeFactory; +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 ThemingDefaults|\PHPUnit_Framework_MockObject_MockObject */ + private $themingDefaults; + /** @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->themingDefaults = $this->createMock(ThemingDefaults::class); + $this->urlGenerator = $this->createMock(IURLGenerator::class); + $this->l10n = $this->createMock(IL10N::class); + $this->mailer = $this->createMock(IMailer::class); + $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->newUserMailHelper = new NewUserMailHelper( + $this->themingDefaults, + $this->urlGenerator, + $this->l10n, + $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->at(0)) + ->method('getEmailAddress') + ->willReturn('recipient@example.com'); + $user + ->expects($this->at(1)) + ->method('getEmailAddress') + ->willReturn('recipient@example.com'); + $this->config + ->expects($this->at(0)) + ->method('getSystemValue') + ->with('secret') + ->willReturn('MyInstanceWideSecret'); + $this->crypto + ->expects($this->once()) + ->method('encrypt') + ->with('12345:MySuperLongSecureRandomToken', 'recipient@example.comMyInstanceWideSecret') + ->willReturn('TokenCiphertext'); + $user + ->expects($this->at(2)) + ->method('getUID') + ->willReturn('john'); + $this->config + ->expects($this->at(1)) + ->method('setUserValue') + ->with('john', 'core', 'lostpassword', 'TokenCiphertext'); + $user + ->expects($this->at(3)) + ->method('getUID') + ->willReturn('john'); + $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->at(4)) + ->method('getDisplayName') + ->willReturn('john'); + $user + ->expects($this->at(5)) + ->method('getUID') + ->willReturn('john'); + $this->themingDefaults + ->expects($this->at(0)) + ->method('getName') + ->willReturn('TestCloud'); + + $expectedHtmlBody = << + + + + + + + + + + + + + +
+
+ + + +
+ + + + + + +
+ + + +
+ +
+ + +
+
+
+ + + + + + +
 
+ + + + + +
+

Welcome aboard

+
+ + + + + + +
 
+ + + +
+ + + + + + +
+ + + + + + +
 
+ + + + + +
+ + + + + +
+

You have now an 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->at(0)) + ->method('getDisplayName') + ->willReturn('John Doe'); + $user + ->expects($this->at(1)) + ->method('getUID') + ->willReturn('john'); + $this->themingDefaults + ->expects($this->any()) + ->method('getName') + ->willReturn('TestCloud'); + + $expectedHtmlBody = << + + + + + + + + + + + + + +
+
+ + + +
+ + + + + + +
+ + + +
+ +
+ + +
+
+
+ + + + + + +
 
+ + + + + +
+

Welcome aboard John Doe

+
+ + + + + + +
 
+ + + +
+ + + + + + +
+ + + + + + +
 
+ + + + + +
+ + + + + +
+

You have now an 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']); + $this->themingDefaults + ->expects($this->exactly(2)) + ->method('getName') + ->willReturn('TestCloud'); + $message + ->expects($this->at(1)) + ->method('setSubject') + ->with('Your TestCloud account was created'); + $message + ->expects($this->at(2)) + ->method('setHtmlBody') + ->with($emailTemplate->renderHTML()); + $message + ->expects($this->at(3)) + ->method('setPlainBody') + ->with($emailTemplate->renderText()); + $message + ->expects($this->at(4)) + ->method('setFrom') + ->with(['no-reply@nextcloud.com' => 'TestCloud']); + $this->mailer + ->expects($this->once()) + ->method('createMessage') + ->willReturn($message); + + $this->newUserMailHelper->sendMail($user, $emailTemplate); + } +} diff --git a/tests/data/emails/new-account-email-custom.html b/tests/data/emails/new-account-email-custom.html new file mode 100644 index 0000000000000..10d3330604d9e --- /dev/null +++ b/tests/data/emails/new-account-email-custom.html @@ -0,0 +1,187 @@ + + + + + + + + + + + + + + +
+
+ + + +
+ + + + + + +
+ + + +
+ +
+ + +
+
+
+ + + + + + +
 
+ + + + + +
+

Welcome aboard

+
+ + + + + + +
 
+ + + +
+ + + + + + +
+ + + + + + +
 
+ + + + + +
+ + + + + +
+

You have now an Nextcloud account, you can add, protect, and share your data.

+
+
+ + + + + +
+ + + + + +
+

Your username is: abc

+
+
+ + + + + +
 
+ + + + + + +
+ + + + + +
+
+ + + + +
+ + + + +
+ Set your password +
+
+ + + + +
+ + + + +
+ Install Client +
+
+
+
+
+
+
+ + + + + +
 
+ + + + +
+
+ +
                                                           
+ + \ No newline at end of file diff --git a/tests/data/emails/new-account-email-custom.txt b/tests/data/emails/new-account-email-custom.txt new file mode 100644 index 0000000000000..962128980e018 --- /dev/null +++ b/tests/data/emails/new-account-email-custom.txt @@ -0,0 +1,11 @@ +Welcome aboard + +You have now an Nextcloud account, you can add, protect, and share your data. + +Your username is: abc + +Set your password: https://example.org/resetPassword/123 +Install Client: https://nextcloud.com/install/#install-clients + +-- +TestCloud - A safe home for your data diff --git a/tests/data/emails/new-account-email.html b/tests/data/emails/new-account-email.html new file mode 100644 index 0000000000000..a32ad61c49c77 --- /dev/null +++ b/tests/data/emails/new-account-email.html @@ -0,0 +1,187 @@ + + + + + + + + + + + + + + +
+
+ + + +
+ + + + + + +
+ + + +
+ +
+ + +
+
+
+ + + + + + +
 
+ + + + + +
+

Welcome aboard

+
+ + + + + + +
 
+ + + +
+ + + + + + +
+ + + + + + +
 
+ + + + + +
+ + + + + +
+

You have now an Nextcloud account, you can add, protect, and share your data.

+
+
+ + + + + +
+ + + + + +
+

Your username is: abc

+
+
+ + + + + +
 
+ + + + + + +
+ + + + + +
+
+ + + + +
+ + + + +
+ Set your password +
+
+ + + + +
+ + + + +
+ Install Client +
+
+
+
+
+
+
+ + + + + +
 
+ + + + +
+
+ +
                                                           
+ + \ No newline at end of file diff --git a/tests/data/emails/new-account-email.txt b/tests/data/emails/new-account-email.txt new file mode 100644 index 0000000000000..6732d806d765e --- /dev/null +++ b/tests/data/emails/new-account-email.txt @@ -0,0 +1,12 @@ +Welcome aboard + +You have now an Nextcloud account, you can add, protect, and share your data. + +Your username is: abc + +Set your password: https://example.org/resetPassword/123 +Install Client: https://nextcloud.com/install/#install-clients + +-- +TestCloud - A safe home for your data +This is an automatically generated email, please do not reply. \ No newline at end of file diff --git a/tests/lib/Mail/EMailTemplateTest.php b/tests/lib/Mail/EMailTemplateTest.php new file mode 100644 index 0000000000000..47fe09aabbb62 --- /dev/null +++ b/tests/lib/Mail/EMailTemplateTest.php @@ -0,0 +1,137 @@ + + * + * @author Morris Jobke + * + * @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 Test\Mail; + +use OC\Mail\EMailTemplate; +use OCA\Theming\ThemingDefaults; +use OCP\IL10N; +use OCP\IURLGenerator; +use Test\TestCase; + +class EMailTemplateTest extends TestCase { + /** @var ThemingDefaults|\PHPUnit_Framework_MockObject_MockObject */ + private $defaults; + /** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */ + private $urlGenerator; + /** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */ + private $l10n; + /** @var EMailTemplate */ + private $emailTemplate; + + public function setUp() { + parent::setUp(); + + $this->defaults = $this->createMock(ThemingDefaults::class); + $this->urlGenerator = $this->createMock(IURLGenerator::class); + $this->l10n = $this->createMock(IL10N::class); + + $this->emailTemplate = new EMailTemplate( + $this->defaults, + $this->urlGenerator, + $this->l10n + ); + } + + public function testEMailTemplateCustomFooter() { + $this->defaults + ->expects($this->any()) + ->method('getColorPrimary') + ->willReturn('#0082c9'); + $this->defaults + ->expects($this->any()) + ->method('getLogo') + ->willReturn('/img/logo-mail-header.png'); + $this->defaults + ->expects($this->any()) + ->method('getCacheBusterCounter') + ->willReturn('48'); + $this->urlGenerator + ->expects($this->once()) + ->method('getAbsoluteURL') + ->with('/img/logo-mail-header.png') + ->willReturn('https://example.org/img/logo-mail-header.png'); + + $this->emailTemplate->addHeader(); + $this->emailTemplate->addHeading('Welcome aboard'); + $this->emailTemplate->addBodyText('You have now an Nextcloud account, you can add, protect, and share your data.'); + $this->emailTemplate->addBodyText('Your username is: abc'); + $this->emailTemplate->addBodyButtonGroup( + 'Set your password', 'https://example.org/resetPassword/123', + 'Install Client', 'https://nextcloud.com/install/#install-clients' + ); + $this->emailTemplate->addFooter( + 'TestCloud - A safe home for your data
This is an automatically generated email, please do not reply.' + ); + + $expectedHTML = file_get_contents(\OC::$SERVERROOT . '/tests/data/emails/new-account-email.html'); + $this->assertSame($expectedHTML, $this->emailTemplate->renderHTML()); + $expectedTXT = file_get_contents(\OC::$SERVERROOT . '/tests/data/emails/new-account-email.txt'); + $this->assertSame($expectedTXT, $this->emailTemplate->renderText()); + } + + public function testEMailTemplateDefaultFooter() { + $this->defaults + ->expects($this->any()) + ->method('getColorPrimary') + ->willReturn('#0082c9'); + $this->defaults + ->expects($this->any()) + ->method('getName') + ->willReturn('TestCloud'); + $this->defaults + ->expects($this->any()) + ->method('getSlogan') + ->willReturn('A safe home for your data'); + $this->defaults + ->expects($this->any()) + ->method('getLogo') + ->willReturn('/img/logo-mail-header.png'); + $this->defaults + ->expects($this->any()) + ->method('getCacheBusterCounter') + ->willReturn('48'); + $this->urlGenerator + ->expects($this->once()) + ->method('getAbsoluteURL') + ->with('/img/logo-mail-header.png') + ->willReturn('https://example.org/img/logo-mail-header.png'); + + $this->emailTemplate->addHeader(); + $this->emailTemplate->addHeading('Welcome aboard'); + $this->emailTemplate->addBodyText('You have now an Nextcloud account, you can add, protect, and share your data.'); + $this->emailTemplate->addBodyText('Your username is: abc'); + $this->emailTemplate->addBodyButtonGroup( + 'Set your password', 'https://example.org/resetPassword/123', + 'Install Client', 'https://nextcloud.com/install/#install-clients' + ); + $this->emailTemplate->addFooter(); + + $expectedHTML = file_get_contents(\OC::$SERVERROOT . '/tests/data/emails/new-account-email-custom.html'); + $this->assertSame($expectedHTML, $this->emailTemplate->renderHTML()); + $expectedTXT = file_get_contents(\OC::$SERVERROOT . '/tests/data/emails/new-account-email-custom.txt'); + $this->assertSame($expectedTXT, $this->emailTemplate->renderText()); + } + + +}