Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide the proper language to the mailer #20246

Merged
merged 1 commit into from
Apr 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions apps/settings/tests/Mailer/NewUserMailHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ protected function setUp(): void {
$template = new EMailTemplate(
$this->defaults,
$this->urlGenerator,
$this->l10n,
$this->l10nFactory,
'test.TestTemplate',
[]
);
Expand Down Expand Up @@ -377,8 +377,8 @@ public function testGenerateTemplateWithPasswordResetToken() {
Install Client: https://nextcloud.com/install/#install-clients


--
TestCloud -
--
TestCloud -
This is an automatically sent email, please do not reply.
EOF;

Expand Down
4 changes: 2 additions & 2 deletions apps/theming/lib/ThemingDefaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ public function getBaseUrl() {
return $this->config->getAppValue('theming', 'url', $this->url);
}

public function getSlogan() {
return \OCP\Util::sanitizeHTML($this->config->getAppValue('theming', 'slogan', parent::getSlogan()));
public function getSlogan(?string $lang = null) {
return \OCP\Util::sanitizeHTML($this->config->getAppValue('theming', 'slogan', parent::getSlogan($lang)));
}

public function getImprintUrl() {
Expand Down
22 changes: 8 additions & 14 deletions lib/private/Mail/EMailTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
namespace OC\Mail;

use OCP\Defaults;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\L10N\IFactory;
use OCP\Mail\IEMailTemplate;

/**
Expand All @@ -52,8 +52,8 @@ class EMailTemplate implements IEMailTemplate {
protected $themingDefaults;
/** @var IURLGenerator */
protected $urlGenerator;
/** @var IL10N */
protected $l10n;
/** @var IFactory */
protected $l10nFactory;
/** @var string */
protected $emailId;
/** @var array */
Expand Down Expand Up @@ -350,21 +350,14 @@ class EMailTemplate implements IEMailTemplate {
</table>
EOF;

/**
* @param Defaults $themingDefaults
* @param IURLGenerator $urlGenerator
* @param IL10N $l10n
* @param string $emailId
* @param array $data
*/
public function __construct(Defaults $themingDefaults,
IURLGenerator $urlGenerator,
IL10N $l10n,
IFactory $l10nFactory,
$emailId,
array $data) {
$this->themingDefaults = $themingDefaults;
$this->urlGenerator = $urlGenerator;
$this->l10n = $l10n;
$this->l10nFactory = $l10nFactory;
$this->htmlBody .= $this->head;
$this->emailId = $emailId;
$this->data = $data;
Expand Down Expand Up @@ -605,9 +598,10 @@ protected function ensureBodyIsClosed() {
*
* @param string $text If the text is empty the default "Name - Slogan<br>This is an automatically sent email" will be used
*/
public function addFooter(string $text = '') {
public function addFooter(string $text = '', ?string $lang = null) {
if ($text === '') {
$text = $this->themingDefaults->getName() . ' - ' . $this->themingDefaults->getSlogan() . '<br>' . $this->l10n->t('This is an automatically sent email, please do not reply.');
$l10n = $this->l10nFactory->get('lib', $lang);
$text = $this->themingDefaults->getName() . ' - ' . $this->themingDefaults->getSlogan($lang) . '<br>' . $l10n->t('This is an automatically sent email, please do not reply.');
}

if ($this->footerAdded) {
Expand Down
9 changes: 7 additions & 2 deletions lib/private/Mail/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use OCP\IL10N;
use OCP\ILogger;
use OCP\IURLGenerator;
use OCP\L10N\IFactory;
use OCP\Mail\IAttachment;
use OCP\Mail\IEMailTemplate;
use OCP\Mail\IMailer;
Expand Down Expand Up @@ -80,6 +81,8 @@ class Mailer implements IMailer {
private $l10n;
/** @var IEventDispatcher */
private $dispatcher;
/** @var IFactory */
private $l10nFactory;

/**
* @param IConfig $config
Expand All @@ -94,13 +97,15 @@ public function __construct(IConfig $config,
Defaults $defaults,
IURLGenerator $urlGenerator,
IL10N $l10n,
IEventDispatcher $dispatcher) {
IEventDispatcher $dispatcher,
IFactory $l10nFactory) {
$this->config = $config;
$this->logger = $logger;
$this->defaults = $defaults;
$this->urlGenerator = $urlGenerator;
$this->l10n = $l10n;
$this->dispatcher = $dispatcher;
$this->l10nFactory = $l10nFactory;
}

/**
Expand Down Expand Up @@ -158,7 +163,7 @@ public function createEMailTemplate(string $emailId, array $data = []): IEMailTe
return new EMailTemplate(
$this->defaults,
$this->urlGenerator,
$this->l10n,
$this->l10nFactory,
$emailId,
$data
);
Expand Down
3 changes: 2 additions & 1 deletion lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -995,7 +995,8 @@ public function __construct($webRoot, \OC\Config $config) {
$c->query(Defaults::class),
$c->getURLGenerator(),
$c->getL10N('lib'),
$c->query(IEventDispatcher::class)
$c->query(IEventDispatcher::class),
$c->getL10NFactory()
);
});
$this->registerDeprecatedAlias('Mailer', IMailer::class);
Expand Down
4 changes: 2 additions & 2 deletions lib/private/Share20/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -891,9 +891,9 @@ protected function sendMailNotification(IL10N $l,
$initiatorEmail = $initiatorUser->getEMailAddress();
if ($initiatorEmail !== null) {
$message->setReplyTo([$initiatorEmail => $initiatorDisplayName]);
$emailTemplate->addFooter($instanceName . ($this->defaults->getSlogan() !== '' ? ' - ' . $this->defaults->getSlogan() : ''));
$emailTemplate->addFooter($instanceName . ($this->defaults->getSlogan($l->getLanguageCode()) !== '' ? ' - ' . $this->defaults->getSlogan($l->getLanguageCode()) : ''));
} else {
$emailTemplate->addFooter();
$emailTemplate->addFooter('', $l->getLanguageCode());
nickvergessen marked this conversation as resolved.
Show resolved Hide resolved
}

$message->useTemplate($emailTemplate);
Expand Down
6 changes: 3 additions & 3 deletions lib/private/legacy/OC_Defaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,12 @@ public function getEntity() {
* Returns slogan
* @return string slogan
*/
public function getSlogan() {
public function getSlogan(?string $lang = null) {
if ($this->themeExist('getSlogan')) {
return $this->theme->getSlogan();
return $this->theme->getSlogan($lang);
} else {
if ($this->defaultSlogan === null) {
$l10n = \OC::$server->getL10N('lib');
$l10n = \OC::$server->getL10N('lib', $lang);
$this->defaultSlogan = $l10n->t('a safe home for all your data');
}
return $this->defaultSlogan;
Expand Down
4 changes: 2 additions & 2 deletions lib/public/Defaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ public function getEntity() {
* @return string
* @since 6.0.0
*/
public function getSlogan() {
return $this->defaults->getSlogan();
public function getSlogan(?string $lang = null) {
return $this->defaults->getSlogan($lang);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion lib/public/Mail/IEMailTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,11 @@ public function addBodyButton(string $text, string $url, $plainText = '');
* Adds a logo and a text to the footer. <br> in the text will be replaced by new lines in the plain text email
*
* @param string $text If the text is empty the default "Name - Slogan<br>This is an automatically sent email" will be used
* @param string $lang Optional language to set the default footer in
*
* @since 12.0.0
*/
public function addFooter(string $text = '');
public function addFooter(string $text = '', ?string $lang = null);

/**
* Returns the rendered email subject as string
Expand Down
9 changes: 7 additions & 2 deletions tests/lib/Mail/EMailTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@
use OCP\Defaults;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\L10N\IFactory;
use Test\TestCase;

class EMailTemplateTest extends TestCase {
/** @var Defaults|\PHPUnit_Framework_MockObject_MockObject */
private $defaults;
/** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
private $urlGenerator;
/** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */
/** @var IFactory|\PHPUnit_Framework_MockObject_MockObject */
private $l10n;
/** @var EMailTemplate */
private $emailTemplate;
Expand All @@ -44,7 +45,11 @@ protected function setUp(): void {

$this->defaults = $this->createMock(Defaults::class);
$this->urlGenerator = $this->createMock(IURLGenerator::class);
$this->l10n = $this->createMock(IL10N::class);
$this->l10n = $this->createMock(IFactory::class);

$this->l10n->method('get')
->with('lib', '')
->willReturn($this->createMock(IL10N::class));

$this->emailTemplate = new EMailTemplate(
$this->defaults,
Expand Down
6 changes: 4 additions & 2 deletions tests/lib/Mail/MailerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use OCP\IL10N;
use OCP\ILogger;
use OCP\IURLGenerator;
use OCP\L10N\IFactory;
use OCP\Mail\Events\BeforeMessageSent;
use Test\TestCase;
use Swift_SwiftException;
Expand Down Expand Up @@ -56,7 +57,8 @@ protected function setUp(): void {
$this->defaults,
$this->urlGenerator,
$this->l10n,
$this->dispatcher
$this->dispatcher,
$this->createMock(IFactory::class)
);
}

Expand Down Expand Up @@ -153,7 +155,7 @@ public function testCreateMessage() {
$this->assertInstanceOf('\OC\Mail\Message', $this->mailer->createMessage());
}


public function testSendInvalidMailException() {
$this->expectException(\Exception::class);

Expand Down