Skip to content

Commit ad1bd68

Browse files
committed
Add end-to-end test that __() in error pages translates per URL locale
Renders a fixture template containing a {{ __('...') }} string through the error controller and asserts it resolves in the locale recovered from the URL (nl) and the default locale (en). The fixture path is registered on the existing Twig filesystem loader so no test-only template ships in templates/.
1 parent aa4449a commit ad1bd68

2 files changed

Lines changed: 59 additions & 0 deletions

File tree

tests/php/Controller/Frontend/ErrorControllerTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
use Symfony\Contracts\Translation\TranslatorInterface;
1919
use Throwable;
2020
use Twig\Environment;
21+
use Twig\Loader\ChainLoader;
22+
use Twig\Loader\FilesystemLoader;
2123

2224
class ErrorControllerTest extends DbAwareTestCase
2325
{
@@ -119,6 +121,34 @@ public function testErrorPageRecoversTranslatorLocaleFromPath(): void
119121
self::assertSame('nl', $translator->getLocale());
120122
}
121123

124+
public function testErrorPageTranslatesUnderscoreFunctionInRequestedLocale(): void
125+
{
126+
// End-to-end: a `{{ __('...') }}` string in the rendered error page must be
127+
// translated in the locale recovered from the URL. `http_error.name` is
128+
// "Error %status_code%" (en) / "Fout %status_code%" (nl).
129+
$this->registerFixtureTemplatePath();
130+
$this->setGeneralConfig('notfound', ['locale_probe.html.twig']);
131+
132+
$body = (string) $this->renderError(new NotFoundHttpException(), '/nl/this-page-does-not-exist')->getContent();
133+
134+
self::assertStringContainsString('LOCALE_PROBE:Fout 404', $body);
135+
self::assertStringNotContainsString('Error 404', $body);
136+
}
137+
138+
public function testErrorPageTranslatesUnderscoreFunctionInDefaultLocale(): void
139+
{
140+
// The counterpart of the test above: with no locale segment in the URL, the
141+
// same `__()` string must fall back to the default locale (en) - and must not
142+
// leak a previous request's locale onto the shared translator.
143+
$this->registerFixtureTemplatePath();
144+
$this->setGeneralConfig('notfound', ['locale_probe.html.twig']);
145+
146+
$body = (string) $this->renderError(new NotFoundHttpException(), '/this-page-does-not-exist')->getContent();
147+
148+
self::assertStringContainsString('LOCALE_PROBE:Error 404', $body);
149+
self::assertStringNotContainsString('Fout 404', $body);
150+
}
151+
122152
public function testNotFoundPageWithNonLocalizedContentTypeDoesNotError(): void
123153
{
124154
// The default `notfound` points at `blocks/404-not-found`. The `blocks`
@@ -185,6 +215,31 @@ private function renderError(Throwable $exception, string $path): Response
185215
}
186216
}
187217

218+
/**
219+
* Make the `Fixtures/` directory resolvable by name, so a fixture template can be
220+
* pointed at via the `notfound` config. Mirrors how Bolt prepends paths to the
221+
* existing filesystem loader (see TwigAwareController::setTwigLoader()) rather than
222+
* replacing it, so the added path survives rendering.
223+
*/
224+
private function registerFixtureTemplatePath(): void
225+
{
226+
/** @var Environment $twig */
227+
$twig = self::getContainer()->get('twig');
228+
229+
$loader = $twig->getLoader();
230+
$loaders = $loader instanceof ChainLoader ? $loader->getLoaders() : [$loader];
231+
232+
foreach ($loaders as $candidate) {
233+
if ($candidate instanceof FilesystemLoader) {
234+
$candidate->addPath(__DIR__ . '/Fixtures');
235+
236+
return;
237+
}
238+
}
239+
240+
self::fail('Could not find a FilesystemLoader to register the fixture template path on.');
241+
}
242+
188243
private function getPublishedPage(): Content
189244
{
190245
$page = $this->getEm()->getRepository(Content::class)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{# Test fixture for ErrorControllerTest: renders a `__()` string so the test can
2+
assert error pages translate via the translator locale recovered from the URL.
3+
`http_error.name` is "Error %status_code%" (en) / "Fout %status_code%" (nl). #}
4+
LOCALE_PROBE:{{ __('http_error.name', {'%status_code%': 404}) }}

0 commit comments

Comments
 (0)