Skip to content

Commit 1652316

Browse files
committed
Fix login url
1 parent c20bfc1 commit 1652316

File tree

6 files changed

+40
-21
lines changed

6 files changed

+40
-21
lines changed

features/bootstrap/ProfilerContext.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ public function iShouldGetAnEmail(string $emailType, string $emailAddress = 'use
236236
$this->usernameChangedNotification($headers);
237237
break;
238238
case 'enabled_notification':
239-
$this->validateEnabledNotification($headers);
239+
$this->validateEnabledNotification($context, $headers);
240240
break;
241241
case 'custom_change_email_confirmation':
242242
$this->validateChangeEmailVerification($context, $headers, true);
@@ -277,10 +277,11 @@ private function usernameChangedNotification(Headers $headers): void
277277
Assert::assertStringStartsWith(UsernameChangedEmailFactory::MESSAGE_ID_PREFIX, $headers->get('x-message-id')->getBodyAsString());
278278
}
279279

280-
private function validateEnabledNotification(Headers $headers): void
280+
private function validateEnabledNotification(array $context, Headers $headers): void
281281
{
282282
Assert::assertEquals('Your account has been enabled', $headers->get('subject')->getBodyAsString());
283283
Assert::assertStringStartsWith(UserEnabledEmailFactory::MESSAGE_ID_PREFIX, $headers->get('x-message-id')->getBodyAsString());
284+
Assert::assertMatchesRegularExpression('/^http:\/\/www\.website\.com\/login$/i', $context['redirect_url']);
284285
}
285286

286287
private function validateChangeEmailVerification(array $context, Headers $headers, bool $customPath = false, ?string $username = null): void

src/Factory/User/Mailer/UserEnabledEmailFactory.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace Silverback\ApiComponentsBundle\Factory\User\Mailer;
1515

1616
use Silverback\ApiComponentsBundle\Entity\User\AbstractUser;
17+
use Silverback\ApiComponentsBundle\Helper\RefererUrlResolver;
1718
use Symfony\Component\Mime\RawMessage;
1819

1920
/**
@@ -30,6 +31,8 @@ public function create(AbstractUser $user, array $context = []): ?RawMessage
3031
}
3132
$this->initUser($user);
3233

34+
$context['login_url'] = $this->container->get(RefererUrlResolver::class)?->getAbsoluteUrl('/login');
35+
3336
return $this->createEmailMessage($context);
3437
}
3538

src/Resources/views/emails/user_enabled.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77

88
<p>Hello {{ user.username }},</p>
99
<p>We have reviewed your website registration and your account has now been enabled. You can sign in with the password you provided during the sign up process.</p>
10-
<button class="large expand" href="https://addictovocab.org/login">Login</button>
10+
<button class="large expand" href="{{ login_url }}">Login</button>
1111
{% include "@SilverbackApiComponents/emails/_signature.html.twig" %}
1212
{% endblock %}

tests/Factory/User/Mailer/AbstractFinalEmailFactoryTestCase.php

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,12 @@ protected function setUp(): void
3838
$this->eventDispatcherMock = $this->createMock(EventDispatcherInterface::class);
3939
}
4040

41-
protected function assertCommonMockMethodsCalled(bool $tokenPathExpected = false): void
41+
protected function assertCommonMockMethodsCalled(bool $tokenPathExpected = false, array $additionalExpectations = []): void
4242
{
4343
$loaderMock = $this->createMock(LoaderInterface::class);
4444
$twig = new Environment($loaderMock);
4545

46-
$containerWith = [];
47-
$willReturn = [];
46+
$expectations = [...$additionalExpectations];
4847

4948
if ($tokenPathExpected) {
5049
$requestStackMock = $this->createMock(RequestStack::class);
@@ -60,25 +59,32 @@ protected function assertCommonMockMethodsCalled(bool $tokenPathExpected = false
6059
->with('/default-path')
6160
->willReturn('/transformed-path');
6261

63-
$containerWith[] = [RequestStack::class];
64-
$containerWith[] = [RefererUrlResolver::class];
65-
$willReturn[] = $requestStackMock;
66-
$willReturn[] = $refererUrlMock;
62+
$expectations[] = [
63+
[RequestStack::class],
64+
$requestStackMock,
65+
];
66+
$expectations[] = [
67+
[RefererUrlResolver::class],
68+
$refererUrlMock,
69+
];
6770
}
6871

69-
$containerWith[] = ['twig'];
70-
$willReturn[] = $twig;
72+
$expectations[] = [
73+
['twig'],
74+
$twig,
75+
];
7176

72-
$invokedCount = self::exactly(\count($willReturn));
77+
$invokedCount = self::exactly(\count($expectations));
7378

7479
$this->containerInterfaceMock
7580
->expects($invokedCount)
7681
->method('get')
77-
->willReturnCallback(function (...$parameters) use ($invokedCount, $containerWith, $willReturn) {
82+
->willReturnCallback(function (...$parameters) use ($invokedCount, $expectations) {
7883
$currentInvocationCount = $invokedCount->numberOfInvocations();
79-
$this->assertSame($containerWith[$currentInvocationCount - 1], $parameters);
84+
$currentExpectation = $expectations[$currentInvocationCount - 1];
85+
$this->assertSame($currentExpectation[0], $parameters);
8086

81-
return $willReturn[$currentInvocationCount - 1];
87+
return $currentExpectation[1];
8288
});
8389
}
8490
}

tests/Factory/User/Mailer/UserEnabledEmailFactoryTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use Silverback\ApiComponentsBundle\Entity\User\AbstractUser;
1717
use Silverback\ApiComponentsBundle\Factory\User\Mailer\UserEnabledEmailFactory;
18+
use Silverback\ApiComponentsBundle\Helper\RefererUrlResolver;
1819
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
1920
use Symfony\Component\Mime\Address;
2021

@@ -41,7 +42,14 @@ public function test_redirect_url_context_added_and_html_template_passed(): void
4142

4243
$factory = new UserEnabledEmailFactory($this->containerInterfaceMock, $this->eventDispatcherMock, 'subject', true, '/default-path');
4344

44-
$this->assertCommonMockMethodsCalled();
45+
$referrerUrlMock = $this->createMock(RefererUrlResolver::class);
46+
$referrerUrlMock
47+
->method('getAbsoluteUrl')
48+
->willReturn('https://login');
49+
$this->assertCommonMockMethodsCalled(false, [[
50+
[RefererUrlResolver::class],
51+
$referrerUrlMock,
52+
]]);
4553

4654
$email = (new TemplatedEmail())
4755
->to(Address::create('[email protected]'))
@@ -51,6 +59,7 @@ public function test_redirect_url_context_added_and_html_template_passed(): void
5159
[
5260
'website_name' => 'my website',
5361
'user' => $user,
62+
'login_url' => 'https://login',
5463
]
5564
);
5665

tests/Helper/User/UserMailerTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function test_exception_thrown_if_mailer_send_throws_exception(): void
100100
$additionalExpectations = $this->createEmMockExpectation();
101101

102102
$loggerMock = $this->createMock(Logger::class);
103-
$factoryMock = $this->getFactoryFromContainerMock(PasswordResetEmailFactory::class, [...$additionalExpectations, ['logger', $loggerMock]]);
103+
$factoryMock = $this->getFactoryFromContainerMock(PasswordResetEmailFactory::class, [...$additionalExpectations, [['logger'], $loggerMock]]);
104104

105105
$factoryMock
106106
->expects(self::once())
@@ -215,7 +215,7 @@ private function createEmMockExpectation(): array
215215

216216
return [
217217
[
218-
'doctrine.orm.entity_manager',
218+
['doctrine.orm.entity_manager'],
219219
$emMock,
220220
],
221221
];
@@ -225,7 +225,7 @@ private function getFactoryFromContainerMock(string $factory, array $additionalE
225225
{
226226
$factoryMock = $this->createMock(AbstractUserEmailFactory::class);
227227
$expectations = [
228-
[$factory, $factoryMock],
228+
[[$factory], $factoryMock],
229229
...$additionalExpectations,
230230
];
231231

@@ -234,7 +234,7 @@ private function getFactoryFromContainerMock(string $factory, array $additionalE
234234
$this->containerMock
235235
->expects($invokedCount)
236236
->method('get')
237-
->willReturnCallback(function ($parameters) use ($invokedCount, $expectations) {
237+
->willReturnCallback(function (...$parameters) use ($invokedCount, $expectations) {
238238
$currentInvocationCount = $invokedCount->numberOfInvocations();
239239
$currentExpectation = $expectations[$currentInvocationCount - 1];
240240
$this->assertSame($currentExpectation[0], $parameters);

0 commit comments

Comments
 (0)