From 8b37fe7f6534ad16bd9a357036e95e748e2068e3 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 15 Sep 2017 10:59:11 +0200 Subject: Set the subject with the email template to allow theming Signed-off-by: Joas Schilling --- apps/sharebymail/lib/ShareByMailProvider.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'apps') diff --git a/apps/sharebymail/lib/ShareByMailProvider.php b/apps/sharebymail/lib/ShareByMailProvider.php index 516e4e243bf..6e0664a8147 100644 --- a/apps/sharebymail/lib/ShareByMailProvider.php +++ b/apps/sharebymail/lib/ShareByMailProvider.php @@ -380,8 +380,6 @@ class ShareByMailProvider implements IShareProvider { \DateTime $expiration = null) { $initiatorUser = $this->userManager->get($initiator); $initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator; - $subject = (string)$this->l->t('%s shared »%s« with you', array($initiatorDisplayName, $filename)); - $message = $this->mailer->createMessage(); $emailTemplate = $this->mailer->createEMailTemplate('sharebymail.RecipientNotification', [ @@ -392,6 +390,7 @@ class ShareByMailProvider implements IShareProvider { 'shareWith' => $shareWith, ]); + $emailTemplate->setSubject($this->l->t('%s shared »%s« with you', array($initiatorDisplayName, $filename))); $emailTemplate->addHeader(); $emailTemplate->addHeading($this->l->t('%s shared »%s« with you', [$initiatorDisplayName, $filename]), false); $text = $this->l->t('%s shared »%s« with you.', [$initiatorDisplayName, $filename]); @@ -428,7 +427,7 @@ class ShareByMailProvider implements IShareProvider { $emailTemplate->addFooter(); } - $message->setSubject($subject); + $message->setSubject($emailTemplate->renderSubject()); $message->setPlainBody($emailTemplate->renderText()); $message->setHtmlBody($emailTemplate->renderHtml()); $this->mailer->send($message); @@ -455,7 +454,6 @@ class ShareByMailProvider implements IShareProvider { $initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator; $initiatorEmailAddress = ($initiatorUser instanceof IUser) ? $initiatorUser->getEMailAddress() : null; - $subject = (string)$this->l->t('Password to access »%s« shared to you by %s', [$filename, $initiatorDisplayName]); $plainBodyPart = $this->l->t("%s shared »%s« with you.\nYou should have already received a separate mail with a link to access it.\n", [$initiatorDisplayName, $filename]); $htmlBodyPart = $this->l->t('%s shared »%s« with you. You should have already received a separate mail with a link to access it.', [$initiatorDisplayName, $filename]); @@ -468,6 +466,8 @@ class ShareByMailProvider implements IShareProvider { 'initiatorEmail' => $initiatorEmailAddress, 'shareWith' => $shareWith, ]); + + $emailTemplate->setSubject($this->l->t('Password to access »%s« shared to you by %s', [$filename, $initiatorDisplayName])); $emailTemplate->addHeader(); $emailTemplate->addHeading($this->l->t('Password to access »%s«', [$filename]), false); $emailTemplate->addBodyText($htmlBodyPart, $plainBodyPart); @@ -491,7 +491,7 @@ class ShareByMailProvider implements IShareProvider { } $message->setTo([$shareWith]); - $message->setSubject($subject); + $message->setSubject($emailTemplate->renderSubject()); $message->setBody($emailTemplate->renderText(), 'text/plain'); $message->setHtmlBody($emailTemplate->renderHtml()); $this->mailer->send($message); @@ -524,7 +524,6 @@ class ShareByMailProvider implements IShareProvider { ); } - $subject = (string)$this->l->t('Password to access »%s« shared with %s', [$filename, $shareWith]); $bodyPart = $this->l->t("You just shared »%s« with %s. The share was already send to the recipient. Due to the security policies defined by the administrator of %s each share needs to be protected by password and it is not allowed to send the password directly to the recipient. Therefore you need to forward the password manually to the recipient.", [$filename, $shareWith, $this->defaults->getName()]); $message = $this->mailer->createMessage(); @@ -536,6 +535,7 @@ class ShareByMailProvider implements IShareProvider { 'shareWith' => $shareWith, ]); + $emailTemplate->setSubject($this->l->t('Password to access »%s« shared with %s', [$filename, $shareWith])); $emailTemplate->addHeader(); $emailTemplate->addHeading($this->l->t('Password to access »%s«', [$filename]), false); $emailTemplate->addBodyText($bodyPart); @@ -547,7 +547,7 @@ class ShareByMailProvider implements IShareProvider { $message->setFrom([$initiatorEMailAddress => $initiatorDisplayName]); } $message->setTo([$initiatorEMailAddress => $initiatorDisplayName]); - $message->setSubject($subject); + $message->setSubject($emailTemplate->renderSubject()); $message->setBody($emailTemplate->renderText(), 'text/plain'); $message->setHtmlBody($emailTemplate->renderHtml()); $this->mailer->send($message); -- cgit v1.2.3 From 3119fd41ceebbdab33c4dd86ce0b60b4dc9010e9 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 15 Sep 2017 11:01:21 +0200 Subject: Set the data from the template Signed-off-by: Joas Schilling --- apps/sharebymail/lib/ShareByMailProvider.php | 12 ++--- apps/sharebymail/tests/ShareByMailProviderTest.php | 40 +++------------ core/Controller/LostController.php | 4 +- lib/private/Share20/Manager.php | 4 +- settings/Controller/MailSettingsController.php | 4 +- settings/Hooks.php | 10 +--- settings/Mailer/NewUserMailHelper.php | 4 +- tests/Core/Controller/LostControllerTest.php | 57 ++++++++-------------- tests/Settings/Mailer/NewUserMailHelperTest.php | 24 +++------ 9 files changed, 46 insertions(+), 113 deletions(-) (limited to 'apps') diff --git a/apps/sharebymail/lib/ShareByMailProvider.php b/apps/sharebymail/lib/ShareByMailProvider.php index 6e0664a8147..f610a1a5fa9 100644 --- a/apps/sharebymail/lib/ShareByMailProvider.php +++ b/apps/sharebymail/lib/ShareByMailProvider.php @@ -427,9 +427,7 @@ class ShareByMailProvider implements IShareProvider { $emailTemplate->addFooter(); } - $message->setSubject($emailTemplate->renderSubject()); - $message->setPlainBody($emailTemplate->renderText()); - $message->setHtmlBody($emailTemplate->renderHtml()); + $message->useTemplate($emailTemplate); $this->mailer->send($message); } @@ -491,9 +489,7 @@ class ShareByMailProvider implements IShareProvider { } $message->setTo([$shareWith]); - $message->setSubject($emailTemplate->renderSubject()); - $message->setBody($emailTemplate->renderText(), 'text/plain'); - $message->setHtmlBody($emailTemplate->renderHtml()); + $message->useTemplate($emailTemplate); $this->mailer->send($message); $this->createPasswordSendActivity($share, $shareWith, false); @@ -547,9 +543,7 @@ class ShareByMailProvider implements IShareProvider { $message->setFrom([$initiatorEMailAddress => $initiatorDisplayName]); } $message->setTo([$initiatorEMailAddress => $initiatorDisplayName]); - $message->setSubject($emailTemplate->renderSubject()); - $message->setBody($emailTemplate->renderText(), 'text/plain'); - $message->setHtmlBody($emailTemplate->renderHtml()); + $message->useTemplate($emailTemplate); $this->mailer->send($message); $this->createPasswordSendActivity($share, $shareWith, true); diff --git a/apps/sharebymail/tests/ShareByMailProviderTest.php b/apps/sharebymail/tests/ShareByMailProviderTest.php index 23e61ee58f1..68a645ec0e6 100644 --- a/apps/sharebymail/tests/ShareByMailProviderTest.php +++ b/apps/sharebymail/tests/ShareByMailProviderTest.php @@ -835,26 +835,14 @@ class ShareByMailProviderTest extends TestCase { ->expects($this->once()) ->method('addFooter') ->with('UnitTestCloud - Testing like 1990'); - $message - ->expects($this->once()) - ->method('setSubject') - ->willReturn('Mrs. Owner User shared »file.txt« with you'); $template ->expects($this->once()) - ->method('renderText') - ->willReturn('Text Render'); - $message - ->expects($this->once()) - ->method('setPlainBody') - ->with('Text Render'); - $template - ->expects($this->once()) - ->method('renderHtml') - ->willReturn('HTML Render'); + ->method('setSubject') + ->with('Mrs. Owner User shared »file.txt« with you'); $message ->expects($this->once()) - ->method('setHtmlBody') - ->with('HTML Render'); + ->method('useTemplate') + ->with($template); $this->mailer ->expects($this->once()) ->method('send') @@ -936,26 +924,14 @@ class ShareByMailProviderTest extends TestCase { ->expects($this->once()) ->method('addFooter') ->with(''); - $message - ->expects($this->once()) - ->method('setSubject') - ->willReturn('Mr. Initiator User shared »file.txt« with you'); $template ->expects($this->once()) - ->method('renderText') - ->willReturn('Text Render'); - $message - ->expects($this->once()) - ->method('setPlainBody') - ->with('Text Render'); - $template - ->expects($this->once()) - ->method('renderHtml') - ->willReturn('HTML Render'); + ->method('setSubject') + ->with('Mr. Initiator User shared »file.txt« with you'); $message ->expects($this->once()) - ->method('setHtmlBody') - ->with('HTML Render'); + ->method('useTemplate') + ->with($template); $this->mailer ->expects($this->once()) ->method('send') diff --git a/core/Controller/LostController.php b/core/Controller/LostController.php index f4400b0f20b..04a3a07bea2 100644 --- a/core/Controller/LostController.php +++ b/core/Controller/LostController.php @@ -328,10 +328,8 @@ class LostController extends Controller { try { $message = $this->mailer->createMessage(); $message->setTo([$email => $user->getUID()]); - $message->setSubject($emailTemplate->renderSubject()); - $message->setPlainBody($emailTemplate->renderText()); - $message->setHtmlBody($emailTemplate->renderHtml()); $message->setFrom([$this->from => $this->defaults->getName()]); + $message->useTemplate($emailTemplate); $this->mailer->send($message); } catch (\Exception $e) { throw new \Exception($this->l10n->t( diff --git a/lib/private/Share20/Manager.php b/lib/private/Share20/Manager.php index dd2a110daeb..42f2170122e 100644 --- a/lib/private/Share20/Manager.php +++ b/lib/private/Share20/Manager.php @@ -750,9 +750,7 @@ class Manager implements IManager { $emailTemplate->addFooter(); } - $message->setSubject($emailTemplate->renderSubject()); - $message->setPlainBody($emailTemplate->renderText()); - $message->setHtmlBody($emailTemplate->renderHtml()); + $message->useTemplate($emailTemplate); $this->mailer->send($message); } diff --git a/settings/Controller/MailSettingsController.php b/settings/Controller/MailSettingsController.php index cb622fa9b12..02bd51e843e 100644 --- a/settings/Controller/MailSettingsController.php +++ b/settings/Controller/MailSettingsController.php @@ -159,9 +159,7 @@ class MailSettingsController extends Controller { $message = $this->mailer->createMessage(); $message->setTo([$email => $displayName]); - $message->setSubject($template->renderSubject()); - $message->setHtmlBody($template->renderHtml()); - $message->setPlainBody($template->renderText()); + $message->useTemplate($template); $errors = $this->mailer->send($message); if (!empty($errors)) { throw new \RuntimeException($this->l10n->t('Email could not be sent. Check your mail server log')); diff --git a/settings/Hooks.php b/settings/Hooks.php index 3da85b4ae74..59bd7bb905d 100644 --- a/settings/Hooks.php +++ b/settings/Hooks.php @@ -132,10 +132,7 @@ class Hooks { $message = $this->mailer->createMessage(); $message->setTo([$user->getEMailAddress() => $user->getDisplayName()]); - $message->setSubject($template->renderSubject()); - $message->setBody($template->renderText(), 'text/plain'); - $message->setHtmlBody($template->renderHtml()); - + $message->useTemplate($template); $this->mailer->send($message); } } @@ -208,10 +205,7 @@ class Hooks { $message = $this->mailer->createMessage(); $message->setTo([$oldMailAddress => $user->getDisplayName()]); - $message->setSubject($template->renderSubject()); - $message->setBody($template->renderText(), 'text/plain'); - $message->setHtmlBody($template->renderHtml()); - + $message->useTemplate($template); $this->mailer->send($message); } } diff --git a/settings/Mailer/NewUserMailHelper.php b/settings/Mailer/NewUserMailHelper.php index af1627a08e3..935b3f0e075 100644 --- a/settings/Mailer/NewUserMailHelper.php +++ b/settings/Mailer/NewUserMailHelper.php @@ -160,10 +160,8 @@ class NewUserMailHelper { IEMailTemplate $emailTemplate) { $message = $this->mailer->createMessage(); $message->setTo([$user->getEMailAddress() => $user->getDisplayName()]); - $message->setSubject($emailTemplate->renderSubject()); - $message->setHtmlBody($emailTemplate->renderHtml()); - $message->setPlainBody($emailTemplate->renderText()); $message->setFrom([$this->fromAddress => $this->themingDefaults->getName()]); + $message->useTemplate($emailTemplate); $this->mailer->send($message); } } diff --git a/tests/Core/Controller/LostControllerTest.php b/tests/Core/Controller/LostControllerTest.php index 0bdc11f8a2f..196b1da352b 100644 --- a/tests/Core/Controller/LostControllerTest.php +++ b/tests/Core/Controller/LostControllerTest.php @@ -324,20 +324,9 @@ class LostControllerTest extends \Test\TestCase { ->with(['test@example.com' => 'ExistingUser']); $message ->expects($this->at(1)) - ->method('setSubject') - ->with(' password reset'); - $message - ->expects($this->at(2)) - ->method('setPlainBody') - ->with('text body'); - $message - ->expects($this->at(3)) - ->method('setHtmlBody') - ->with('HTML body'); - $message - ->expects($this->at(4)) ->method('setFrom') ->with(['lostpassword-noreply@localhost' => null]); + $emailTemplate = $this->createMock(IEMailTemplate::class); $emailTemplate->expects($this->any()) ->method('renderHtml') @@ -345,6 +334,12 @@ class LostControllerTest extends \Test\TestCase { $emailTemplate->expects($this->any()) ->method('renderText') ->willReturn('text body'); + + $message + ->expects($this->at(2)) + ->method('useTemplate') + ->with($emailTemplate); + $this->mailer ->expects($this->at(0)) ->method('createEMailTemplate') @@ -407,20 +402,9 @@ class LostControllerTest extends \Test\TestCase { ->with(['test@example.com' => 'ExistingUser']); $message ->expects($this->at(1)) - ->method('setSubject') - ->with(' password reset'); - $message - ->expects($this->at(2)) - ->method('setPlainBody') - ->with('text body'); - $message - ->expects($this->at(3)) - ->method('setHtmlBody') - ->with('HTML body'); - $message - ->expects($this->at(4)) ->method('setFrom') ->with(['lostpassword-noreply@localhost' => null]); + $emailTemplate = $this->createMock(IEMailTemplate::class); $emailTemplate->expects($this->any()) ->method('renderHtml') @@ -428,6 +412,12 @@ class LostControllerTest extends \Test\TestCase { $emailTemplate->expects($this->any()) ->method('renderText') ->willReturn('text body'); + + $message + ->expects($this->at(2)) + ->method('useTemplate') + ->with($emailTemplate); + $this->mailer ->expects($this->at(0)) ->method('createEMailTemplate') @@ -484,20 +474,9 @@ class LostControllerTest extends \Test\TestCase { ->with(['test@example.com' => 'ExistingUser']); $message ->expects($this->at(1)) - ->method('setSubject') - ->with(' password reset'); - $message - ->expects($this->at(2)) - ->method('setPlainBody') - ->with('text body'); - $message - ->expects($this->at(3)) - ->method('setHtmlBody') - ->with('HTML body'); - $message - ->expects($this->at(4)) ->method('setFrom') ->with(['lostpassword-noreply@localhost' => null]); + $emailTemplate = $this->createMock(IEMailTemplate::class); $emailTemplate->expects($this->any()) ->method('renderHtml') @@ -505,6 +484,12 @@ class LostControllerTest extends \Test\TestCase { $emailTemplate->expects($this->any()) ->method('renderText') ->willReturn('text body'); + + $message + ->expects($this->at(2)) + ->method('useTemplate') + ->with($emailTemplate); + $this->mailer ->expects($this->at(0)) ->method('createEMailTemplate') diff --git a/tests/Settings/Mailer/NewUserMailHelperTest.php b/tests/Settings/Mailer/NewUserMailHelperTest.php index 1af1bed808c..9fd90561127 100644 --- a/tests/Settings/Mailer/NewUserMailHelperTest.php +++ b/tests/Settings/Mailer/NewUserMailHelperTest.php @@ -620,26 +620,18 @@ EOF; ->expects($this->at(0)) ->method('setTo') ->with(['recipient@example.com' => 'John Doe']); - $this->defaults - ->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']); + $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') -- cgit v1.2.3