Fix extension translations staying cached after an extension update

Fixes #7245
This commit is contained in:
Anthony
2026-10-06 00:18:41 -05:00
parent 0a688b2eac
commit e52fdfd416
9 changed files with 160 additions and 9 deletions
@@ -24,3 +24,57 @@ test('serves namespaced extension translations and rejects path traversal', func
\Illuminate\Support\Facades\File::deleteDirectory($directory);
}
});
test('caches translations whose URL changes with them and makes the browser revalidate the rest', function (): void {
$directory = sys_get_temp_dir().'/ptero-locale-'.uniqid();
\Illuminate\Support\Facades\File::ensureDirectoryExists($directory.'/en');
\Illuminate\Support\Facades\File::put($directory.'/en/messages.php', '<?php return ["welcome" => "Welcome"];');
$this->app->make(\Illuminate\Translation\Translator::class)->addNamespace('ext-probe', $directory);
try {
$core = $this->getJson('/locales/locale.json?locale=en&namespace=auth')->assertOk();
expect($core->headers->get('Cache-Control'))->toContain('max-age=3600')->toContain('public');
// The frontend sends the revision of the extension's translations, so upgrading
// the extension changes the URL.
$revision = str_repeat('a1', 16);
$extension = $this->getJson("/locales/locale.json?locale=en&namespace=ext-probe%3A%3Amessages&revision={$revision}")->assertOk();
expect($extension->headers->get('Cache-Control'))->toContain('max-age=3600')->toContain('public');
// Without it an upgrade leaves the URL as it was, so a cached copy must never be
// used without asking the server first.
$bare = $this->getJson('/locales/locale.json?locale=en&namespace=ext-probe%3A%3Amessages')->assertOk();
expect($bare->headers->get('Cache-Control'))->toContain('no-cache')->not->toContain('max-age');
$this->getJson('/locales/locale.json?locale=en&namespace=ext-probe%3A%3Amessages&revision=..%2F')->assertUnprocessable();
} finally {
\Illuminate\Support\Facades\File::deleteDirectory($directory);
}
});
test('answers an unchanged translation set with 304 and a changed one with the new strings', function (): void {
$directory = sys_get_temp_dir().'/ptero-locale-'.uniqid();
$url = '/locales/locale.json?locale=en&namespace=ext-probe%3A%3Amessages';
\Illuminate\Support\Facades\File::ensureDirectoryExists($directory.'/en');
\Illuminate\Support\Facades\File::put($directory.'/en/messages.php', '<?php return ["welcome" => "Welcome"];');
$this->app->make(\Illuminate\Translation\Translator::class)->addNamespace('ext-probe', $directory);
try {
$etag = $this->getJson($url)->assertOk()->headers->get('ETag');
expect($etag)->toBeString()->toStartWith('"');
$this->getJson($url, ['If-None-Match' => $etag])->assertStatus(304);
// What an extension upgrade does: the same URL now holds different strings.
\Illuminate\Support\Facades\File::put($directory.'/en/messages.php', '<?php return ["welcome" => "Welcome", "summary" => "Summary"];');
if (function_exists('opcache_invalidate')) {
opcache_invalidate($directory.'/en/messages.php', true);
}
$this->refreshApplication();
$this->app->make(\Illuminate\Translation\Translator::class)->addNamespace('ext-probe', $directory);
$this->getJson($url, ['If-None-Match' => $etag])
->assertOk()
->assertJsonPath('en.ext-probe::messages.summary', 'Summary');
} finally {
\Illuminate\Support\Facades\File::deleteDirectory($directory);
}
});