Get l10n within NewUserMailHelper to ensure it always uses the new user's language.

Some related tests had to be changed because they relied on internals, see also from the PHPUnit documentation:
"Exercise caution when using [the at] matcher as it can lead to brittle tests which are too closely tied to specific implementation details."

Signed-off-by: Zulan <git@zulan.net>
This commit is contained in:
zulan 2018-09-24 22:54:57 +02:00
parent 50a280338e
commit cf266ee004
6 changed files with 41 additions and 115 deletions

View File

@ -43,7 +43,7 @@ class Application extends App {
return new NewUserMailHelper( return new NewUserMailHelper(
$server->query(Defaults::class), $server->query(Defaults::class),
$server->getURLGenerator(), $server->getURLGenerator(),
$server->getL10N('settings'), $server->getL10NFactory(),
$server->getMailer(), $server->getMailer(),
$server->getSecureRandom(), $server->getSecureRandom(),
new TimeFactory(), new TimeFactory(),

View File

@ -894,16 +894,8 @@ class UsersController extends AUserData {
if ($email === '' || $email === null) { if ($email === '' || $email === null) {
throw new OCSException('Email address not available', 101); throw new OCSException('Email address not available', 101);
} }
$username = $targetUser->getUID();
$lang = $this->config->getUserValue($username, 'core', 'lang', 'en');
if (!$this->l10nFactory->languageExists('settings', $lang)) {
$lang = 'en';
}
$l10n = $this->l10nFactory->get('settings', $lang);
try { try {
$this->newUserMailHelper->setL10N($l10n);
$emailTemplate = $this->newUserMailHelper->generateTemplate($targetUser, false); $emailTemplate = $this->newUserMailHelper->generateTemplate($targetUser, false);
$this->newUserMailHelper->sendMail($targetUser, $emailTemplate); $this->newUserMailHelper->sendMail($targetUser, $emailTemplate);
} catch(\Exception $e) { } catch(\Exception $e) {

View File

@ -3257,35 +3257,13 @@ class UsersControllerTest extends TestCase {
->expects($this->once()) ->expects($this->once())
->method('getEmailAddress') ->method('getEmailAddress')
->will($this->returnValue('abc@example.org')); ->will($this->returnValue('abc@example.org'));
$this->config
->expects($this->at(0))
->method('getUserValue')
->with('user-id', 'core', 'lang')
->willReturn('es');
$l10n = $this->getMockBuilder(IL10N::class)
->disableOriginalConstructor()
->getMock();
$this->l10nFactory
->expects($this->at(0))
->method('languageExists')
->with('settings', 'es')
->willReturn(true);
$this->l10nFactory
->expects($this->at(1))
->method('get')
->with('settings', 'es')
->willReturn($l10n);
$emailTemplate = $this->createMock(IEMailTemplate::class); $emailTemplate = $this->createMock(IEMailTemplate::class);
$this->newUserMailHelper $this->newUserMailHelper
->expects($this->at(0)) ->expects($this->at(0))
->method('setL10N')
->willReturn($l10n);
$this->newUserMailHelper
->expects($this->at(1))
->method('generateTemplate') ->method('generateTemplate')
->willReturn($emailTemplate); ->willReturn($emailTemplate);
$this->newUserMailHelper $this->newUserMailHelper
->expects($this->at(2)) ->expects($this->at(1))
->method('sendMail') ->method('sendMail')
->with($targetUser, $emailTemplate); ->with($targetUser, $emailTemplate);
@ -3327,35 +3305,16 @@ class UsersControllerTest extends TestCase {
->expects($this->once()) ->expects($this->once())
->method('getEmailAddress') ->method('getEmailAddress')
->will($this->returnValue('abc@example.org')); ->will($this->returnValue('abc@example.org'));
$this->config
->expects($this->at(0))
->method('getUserValue')
->with('user-id', 'core', 'lang')
->willReturn('es');
$l10n = $this->getMockBuilder(IL10N::class) $l10n = $this->getMockBuilder(IL10N::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$this->l10nFactory
->expects($this->at(0))
->method('languageExists')
->with('settings', 'es')
->willReturn(false);
$this->l10nFactory
->expects($this->at(1))
->method('get')
->with('settings', 'en')
->willReturn($l10n);
$emailTemplate = $this->createMock(IEMailTemplate::class); $emailTemplate = $this->createMock(IEMailTemplate::class);
$this->newUserMailHelper $this->newUserMailHelper
->expects($this->at(0)) ->expects($this->at(0))
->method('setL10N')
->willReturn($l10n);
$this->newUserMailHelper
->expects($this->at(1))
->method('generateTemplate') ->method('generateTemplate')
->willReturn($emailTemplate); ->willReturn($emailTemplate);
$this->newUserMailHelper $this->newUserMailHelper
->expects($this->at(2)) ->expects($this->at(1))
->method('sendMail') ->method('sendMail')
->with($targetUser, $emailTemplate); ->with($targetUser, $emailTemplate);
@ -3402,35 +3361,13 @@ class UsersControllerTest extends TestCase {
->expects($this->once()) ->expects($this->once())
->method('getEmailAddress') ->method('getEmailAddress')
->will($this->returnValue('abc@example.org')); ->will($this->returnValue('abc@example.org'));
$this->config
->expects($this->at(0))
->method('getUserValue')
->with('user-id', 'core', 'lang')
->willReturn('es');
$l10n = $this->getMockBuilder(IL10N::class)
->disableOriginalConstructor()
->getMock();
$this->l10nFactory
->expects($this->at(0))
->method('languageExists')
->with('settings', 'es')
->willReturn(true);
$this->l10nFactory
->expects($this->at(1))
->method('get')
->with('settings', 'es')
->willReturn($l10n);
$emailTemplate = $this->createMock(IEMailTemplate::class); $emailTemplate = $this->createMock(IEMailTemplate::class);
$this->newUserMailHelper $this->newUserMailHelper
->expects($this->at(0)) ->expects($this->at(0))
->method('setL10N')
->willReturn($l10n);
$this->newUserMailHelper
->expects($this->at(1))
->method('generateTemplate') ->method('generateTemplate')
->willReturn($emailTemplate); ->willReturn($emailTemplate);
$this->newUserMailHelper $this->newUserMailHelper
->expects($this->at(2)) ->expects($this->at(1))
->method('sendMail') ->method('sendMail')
->with($targetUser, $emailTemplate) ->with($targetUser, $emailTemplate)
->willThrowException(new \Exception()); ->willThrowException(new \Exception());

View File

@ -100,7 +100,7 @@ class Application extends App {
return new NewUserMailHelper( return new NewUserMailHelper(
$defaults, $defaults,
$server->getURLGenerator(), $server->getURLGenerator(),
$server->getL10N('settings'), $server->getL10NFactory(),
$server->getMailer(), $server->getMailer(),
$server->getSecureRandom(), $server->getSecureRandom(),
new TimeFactory(), new TimeFactory(),

View File

@ -26,6 +26,7 @@
namespace OC\Settings\Mailer; namespace OC\Settings\Mailer;
use OCP\L10N\IFactory;
use OCP\Mail\IEMailTemplate; use OCP\Mail\IEMailTemplate;
use OCP\AppFramework\Utility\ITimeFactory; use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Defaults; use OCP\Defaults;
@ -42,8 +43,8 @@ class NewUserMailHelper {
private $themingDefaults; private $themingDefaults;
/** @var IURLGenerator */ /** @var IURLGenerator */
private $urlGenerator; private $urlGenerator;
/** @var IL10N */ /** @var IFactory */
private $l10n; private $l10nFactory;
/** @var IMailer */ /** @var IMailer */
private $mailer; private $mailer;
/** @var ISecureRandom */ /** @var ISecureRandom */
@ -60,7 +61,7 @@ class NewUserMailHelper {
/** /**
* @param Defaults $themingDefaults * @param Defaults $themingDefaults
* @param IURLGenerator $urlGenerator * @param IURLGenerator $urlGenerator
* @param IL10N $l10n * @param IFactory $l10nFactory
* @param IMailer $mailer * @param IMailer $mailer
* @param ISecureRandom $secureRandom * @param ISecureRandom $secureRandom
* @param ITimeFactory $timeFactory * @param ITimeFactory $timeFactory
@ -70,7 +71,7 @@ class NewUserMailHelper {
*/ */
public function __construct(Defaults $themingDefaults, public function __construct(Defaults $themingDefaults,
IURLGenerator $urlGenerator, IURLGenerator $urlGenerator,
IL10N $l10n, IFactory $l10nFactory,
IMailer $mailer, IMailer $mailer,
ISecureRandom $secureRandom, ISecureRandom $secureRandom,
ITimeFactory $timeFactory, ITimeFactory $timeFactory,
@ -79,7 +80,7 @@ class NewUserMailHelper {
$fromAddress) { $fromAddress) {
$this->themingDefaults = $themingDefaults; $this->themingDefaults = $themingDefaults;
$this->urlGenerator = $urlGenerator; $this->urlGenerator = $urlGenerator;
$this->l10n = $l10n; $this->l10nFactory = $l10nFactory;
$this->mailer = $mailer; $this->mailer = $mailer;
$this->secureRandom = $secureRandom; $this->secureRandom = $secureRandom;
$this->timeFactory = $timeFactory; $this->timeFactory = $timeFactory;
@ -88,21 +89,20 @@ class NewUserMailHelper {
$this->fromAddress = $fromAddress; $this->fromAddress = $fromAddress;
} }
/**
* Set the IL10N object
*
* @param IL10N $l10n
*/
public function setL10N(IL10N $l10n) {
$this->l10n = $l10n;
}
/** /**
* @param IUser $user * @param IUser $user
* @param bool $generatePasswordResetToken * @param bool $generatePasswordResetToken
* @return IEMailTemplate * @return IEMailTemplate
*/ */
public function generateTemplate(IUser $user, $generatePasswordResetToken = false) { public function generateTemplate(IUser $user, $generatePasswordResetToken = false) {
$userId = $user->getUID();
$lang = $this->config->getUserValue($userId, 'core', 'lang', 'en');
if (!$this->l10nFactory->languageExists('settings', $lang)) {
$lang = 'en';
}
$l10n = $this->l10nFactory->get('settings', $lang);
if ($generatePasswordResetToken) { if ($generatePasswordResetToken) {
$token = $this->secureRandom->generate( $token = $this->secureRandom->generate(
21, 21,
@ -119,7 +119,6 @@ class NewUserMailHelper {
$link = $this->urlGenerator->getAbsoluteURL('/'); $link = $this->urlGenerator->getAbsoluteURL('/');
} }
$displayName = $user->getDisplayName(); $displayName = $user->getDisplayName();
$userId = $user->getUID();
$emailTemplate = $this->mailer->createEMailTemplate('settings.Welcome', [ $emailTemplate = $this->mailer->createEMailTemplate('settings.Welcome', [
'link' => $link, 'link' => $link,
@ -129,24 +128,24 @@ class NewUserMailHelper {
'resetTokenGenerated' => $generatePasswordResetToken, 'resetTokenGenerated' => $generatePasswordResetToken,
]); ]);
$emailTemplate->setSubject($this->l10n->t('Your %s account was created', [$this->themingDefaults->getName()])); $emailTemplate->setSubject($l10n->t('Your %s account was created', [$this->themingDefaults->getName()]));
$emailTemplate->addHeader(); $emailTemplate->addHeader();
if ($displayName === $userId) { if ($displayName === $userId) {
$emailTemplate->addHeading($this->l10n->t('Welcome aboard')); $emailTemplate->addHeading($l10n->t('Welcome aboard'));
} else { } else {
$emailTemplate->addHeading($this->l10n->t('Welcome aboard %s', [$displayName])); $emailTemplate->addHeading($l10n->t('Welcome aboard %s', [$displayName]));
} }
$emailTemplate->addBodyText($this->l10n->t('Welcome to your %s account, you can add, protect, and share your data.', [$this->themingDefaults->getName()])); $emailTemplate->addBodyText($l10n->t('Welcome to your %s account, you can add, protect, and share your data.', [$this->themingDefaults->getName()]));
$emailTemplate->addBodyText($this->l10n->t('Your username is: %s', [$userId])); $emailTemplate->addBodyText($l10n->t('Your username is: %s', [$userId]));
if ($generatePasswordResetToken) { if ($generatePasswordResetToken) {
$leftButtonText = $this->l10n->t('Set your password'); $leftButtonText = $l10n->t('Set your password');
} else { } else {
$leftButtonText = $this->l10n->t('Go to %s', [$this->themingDefaults->getName()]); $leftButtonText = $l10n->t('Go to %s', [$this->themingDefaults->getName()]);
} }
$emailTemplate->addBodyButtonGroup( $emailTemplate->addBodyButtonGroup(
$leftButtonText, $leftButtonText,
$link, $link,
$this->l10n->t('Install Client'), $l10n->t('Install Client'),
'https://nextcloud.com/install/#install-clients' 'https://nextcloud.com/install/#install-clients'
); );
$emailTemplate->addFooter(); $emailTemplate->addFooter();

View File

@ -22,6 +22,7 @@
namespace Tests\Settings\Mailer; namespace Tests\Settings\Mailer;
use OC\Mail\EMailTemplate; use OC\Mail\EMailTemplate;
use OCP\L10N\IFactory;
use OCP\Mail\IEMailTemplate; use OCP\Mail\IEMailTemplate;
use OC\Mail\Message; use OC\Mail\Message;
use OC\Settings\Mailer\NewUserMailHelper; use OC\Settings\Mailer\NewUserMailHelper;
@ -64,6 +65,7 @@ class NewUserMailHelperTest extends TestCase {
->willReturn('myLogo'); ->willReturn('myLogo');
$this->urlGenerator = $this->createMock(IURLGenerator::class); $this->urlGenerator = $this->createMock(IURLGenerator::class);
$this->l10n = $this->createMock(IL10N::class); $this->l10n = $this->createMock(IL10N::class);
$this->l10nFactory = $this->createMock(IFactory::class);
$this->mailer = $this->createMock(IMailer::class); $this->mailer = $this->createMock(IMailer::class);
$template = new EMailTemplate( $template = new EMailTemplate(
$this->defaults, $this->defaults,
@ -82,11 +84,15 @@ class NewUserMailHelperTest extends TestCase {
->will($this->returnCallback(function ($text, $parameters = []) { ->will($this->returnCallback(function ($text, $parameters = []) {
return vsprintf($text, $parameters); return vsprintf($text, $parameters);
})); }));
$this->l10nFactory->method('get')
->will($this->returnCallback(function ($text, $lang) {
return $this->l10n;
}));
$this->newUserMailHelper = new NewUserMailHelper( $this->newUserMailHelper = new NewUserMailHelper(
$this->defaults, $this->defaults,
$this->urlGenerator, $this->urlGenerator,
$this->l10n, $this->l10nFactory,
$this->mailer, $this->mailer,
$this->secureRandom, $this->secureRandom,
$this->timeFactory, $this->timeFactory,
@ -113,15 +119,11 @@ class NewUserMailHelperTest extends TestCase {
/** @var IUser|\PHPUnit_Framework_MockObject_MockObject $user */ /** @var IUser|\PHPUnit_Framework_MockObject_MockObject $user */
$user = $this->createMock(IUser::class); $user = $this->createMock(IUser::class);
$user $user
->expects($this->at(0)) ->expects($this->any())
->method('getEmailAddress')
->willReturn('recipient@example.com');
$user
->expects($this->at(1))
->method('getEmailAddress') ->method('getEmailAddress')
->willReturn('recipient@example.com'); ->willReturn('recipient@example.com');
$this->config $this->config
->expects($this->at(0)) ->expects($this->any())
->method('getSystemValue') ->method('getSystemValue')
->with('secret') ->with('secret')
->willReturn('MyInstanceWideSecret'); ->willReturn('MyInstanceWideSecret');
@ -131,24 +133,20 @@ class NewUserMailHelperTest extends TestCase {
->with('12345:MySuperLongSecureRandomToken', 'recipient@example.comMyInstanceWideSecret') ->with('12345:MySuperLongSecureRandomToken', 'recipient@example.comMyInstanceWideSecret')
->willReturn('TokenCiphertext'); ->willReturn('TokenCiphertext');
$user $user
->expects($this->at(2)) ->expects($this->any())
->method('getUID') ->method('getUID')
->willReturn('john'); ->willReturn('john');
$this->config $this->config
->expects($this->at(1)) ->expects($this->once())
->method('setUserValue') ->method('setUserValue')
->with('john', 'core', 'lostpassword', 'TokenCiphertext'); ->with('john', 'core', 'lostpassword', 'TokenCiphertext');
$user
->expects($this->at(3))
->method('getUID')
->willReturn('john');
$this->urlGenerator $this->urlGenerator
->expects($this->at(0)) ->expects($this->at(0))
->method('linkToRouteAbsolute') ->method('linkToRouteAbsolute')
->with('core.lost.resetform', ['userId' => 'john', 'token' => 'MySuperLongSecureRandomToken']) ->with('core.lost.resetform', ['userId' => 'john', 'token' => 'MySuperLongSecureRandomToken'])
->willReturn('https://example.com/resetPassword/MySuperLongSecureRandomToken'); ->willReturn('https://example.com/resetPassword/MySuperLongSecureRandomToken');
$user $user
->expects($this->at(4)) ->expects($this->any())
->method('getDisplayName') ->method('getDisplayName')
->willReturn('john'); ->willReturn('john');
$user $user
@ -385,11 +383,11 @@ EOF;
/** @var IUser|\PHPUnit_Framework_MockObject_MockObject $user */ /** @var IUser|\PHPUnit_Framework_MockObject_MockObject $user */
$user = $this->createMock(IUser::class); $user = $this->createMock(IUser::class);
$user $user
->expects($this->at(0)) ->expects($this->any())
->method('getDisplayName') ->method('getDisplayName')
->willReturn('John Doe'); ->willReturn('John Doe');
$user $user
->expects($this->at(1)) ->expects($this->any())
->method('getUID') ->method('getUID')
->willReturn('john'); ->willReturn('john');
$this->defaults $this->defaults