Skip to content

Commit 48d48ad

Browse files
committed
OXDEV-9078 Translate OTP emails
1 parent 1009968 commit 48d48ad

5 files changed

Lines changed: 29 additions & 5 deletions

File tree

src/Authentication/TwoFactorAuth/OTP/Notifier/Email/OtpEmailNotifier.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace OxidEsales\SecurityModule\Authentication\TwoFactorAuth\OTP\Notifier\Email;
1111

12+
use OxidEsales\Eshop\Core\Language;
1213
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Infrastructure\Factory\EmailFactoryInterface;
1314
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Infrastructure\Repository\NewUserRepositoryInterface;
1415
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\OTP\Notifier\OtpNotifierInterface;
@@ -18,18 +19,18 @@ class OtpEmailNotifier implements OtpNotifierInterface
1819
public function __construct(
1920
private EmailFactoryInterface $emailFactory,
2021
private NewUserRepositoryInterface $userRepository,
22+
private Language $language,
2123
) {
2224
}
2325

2426
public function notify(string $userId, #[\SensitiveParameter] string $code): void
2527
{
2628
$email = $this->userRepository->getUserById($userId)->getEmail();
2729

28-
// todo-critical: load the translations
2930
$this->emailFactory->create()->sendEmail(
3031
$email,
31-
'Your verification code',
32-
"Your verification code is: {$code}"
32+
$this->language->translateString('OTP_EMAIL_SUBJECT'),
33+
sprintf($this->language->translateString('OTP_EMAIL_BODY'), $code)
3334
);
3435
}
3536
}

src/Authentication/TwoFactorAuth/OTP/Notifier/services.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ services:
22
_defaults:
33
autowire: true
44
public: false
5+
bind:
6+
OxidEsales\Eshop\Core\Language: '@=service("OxidEsales\\SecurityModule\\Core\\Registry").getLang()'
57

68
OxidEsales\SecurityModule\Authentication\TwoFactorAuth\OTP\Notifier\OtpNotifierInterface:
79
class: OxidEsales\SecurityModule\Authentication\TwoFactorAuth\OTP\Notifier\Email\OtpEmailNotifier

tests/Unit/Authentication/TwoFactorAuth/OTP/Notifier/Email/OtpEmailNotifierTest.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace OxidEsales\SecurityModule\Tests\Unit\Authentication\TwoFactorAuth\OTP\Notifier\Email;
1111

1212
use OxidEsales\Eshop\Core\Email;
13+
use OxidEsales\Eshop\Core\Language;
1314
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\DTO\NewUserInterface;
1415
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Infrastructure\Factory\EmailFactoryInterface;
1516
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Infrastructure\Repository\NewUserRepositoryInterface;
@@ -24,6 +25,8 @@ public function notifySendsEmailToUserAddress(): void
2425
{
2526
$email = uniqid() . '@example.com';
2627
$code = uniqid();
28+
$subject = uniqid();
29+
$bodyTemplate = uniqid() . ' %s';
2730

2831
$userStub = $this->createStub(NewUserInterface::class);
2932
$userStub->method('getEmail')->willReturn($email);
@@ -34,13 +37,22 @@ public function notifySendsEmailToUserAddress(): void
3437
->with($userId = uniqid())
3538
->willReturn($userStub);
3639

40+
$translations = [
41+
'OTP_EMAIL_SUBJECT' => $subject,
42+
'OTP_EMAIL_BODY' => $bodyTemplate,
43+
];
44+
$languageStub = $this->createStub(Language::class);
45+
$languageStub->method('translateString')->willReturnCallback(
46+
fn(string $key) => $translations[$key] ?? $key
47+
);
48+
3749
$emailModelSpy = $this->createMock(Email::class);
3850
$emailModelSpy->expects($this->once())
3951
->method('sendEmail')
4052
->with(
4153
$email,
42-
'Your verification code',
43-
"Your verification code is: {$code}"
54+
$subject,
55+
sprintf($bodyTemplate, $code)
4456
);
4557

4658
$emailFactoryStub = $this->createStub(EmailFactoryInterface::class);
@@ -49,6 +61,7 @@ public function notifySendsEmailToUserAddress(): void
4961
$sut = $this->getSut(
5062
emailFactory: $emailFactoryStub,
5163
userRepository: $userRepositoryMock,
64+
language: $languageStub,
5265
);
5366

5467
$sut->notify(userId: $userId, code: $code);
@@ -57,10 +70,12 @@ public function notifySendsEmailToUserAddress(): void
5770
private function getSut(
5871
EmailFactoryInterface $emailFactory = null,
5972
NewUserRepositoryInterface $userRepository = null,
73+
Language $language = null,
6074
): OtpEmailNotifier {
6175
return new OtpEmailNotifier(
6276
emailFactory: $emailFactory ?? $this->createStub(EmailFactoryInterface::class),
6377
userRepository: $userRepository ?? $this->createStub(NewUserRepositoryInterface::class),
78+
language: $language ?? $this->createStub(Language::class),
6479
);
6580
}
6681
}

translations/de/oesecuritymodule_lang.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242

4343
'SIGN_IN_PROVIDER' => 'Anmelden mit %s',
4444

45+
'OTP_EMAIL_SUBJECT' => 'Ihr Verifizierungscode',
46+
'OTP_EMAIL_BODY' => 'Ihr Verifizierungscode lautet: %s',
47+
4548
'TWO_FACTOR_AUTHENTICATION_TITLE' => 'Zwei-Faktor-Authentifizierung',
4649
'TWO_FACTOR_AUTHENTICATION_DESCRIPTION' => 'Ein Code wurde an Ihre E-Mail-Adresse gesendet. Bitte geben Sie ihn unten ein, um fortzufahren.',
4750

translations/en/oesecuritymodule_lang.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242

4343
'SIGN_IN_PROVIDER' => 'Sign in with %s',
4444

45+
'OTP_EMAIL_SUBJECT' => 'Your verification code',
46+
'OTP_EMAIL_BODY' => 'Your verification code is: %s',
47+
4548
'TWO_FACTOR_AUTHENTICATION_TITLE' => 'Two Factor Authentication',
4649
'TWO_FACTOR_AUTHENTICATION_DESCRIPTION' => 'Code has been sent to your email. Please enter it below to proceed.',
4750

0 commit comments

Comments
 (0)