Files
Toiminnanohjaus/tests/Unit/TranslationFilesTest.php
petriandClaude Opus 5.5 45dbb04344 Keep the chosen language across sign-in and forced sign-out
A language picked on a signed-out page is now stored on the account when
the user signs in (StoreLocaleChosenBeforeLogin), so the app does not
switch back to the account default. EnsureAccountIsUsable keeps the user's
language for the login page when it ends a disabled account's session.

ChangePreferredLocale runs in a transaction like the other use cases, the
language names come from layout.languages instead of the Locale enum, and
the translation test also compares fi.json with en.json and rejects
English files without a Finnish counterpart.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-25 07:09:22 +02:00

76 lines
2.5 KiB
PHP

<?php
/**
* Every user interface text exists in every language, with the same placeholders,
* so a new Finnish text cannot be left without its English counterpart.
*/
function langPath(string $path = ''): string
{
return dirname(__DIR__, 2).'/lang'.($path === '' ? '' : '/'.$path);
}
/**
* @return array<string, string>
*/
function translationLines(string $locale, string $file): array
{
$lines = require langPath("{$locale}/{$file}");
$flat = [];
$flatten = function (array $values, string $prefix) use (&$flatten, &$flat): void {
foreach ($values as $key => $value) {
is_array($value) ? $flatten($value, $prefix.$key.'.') : $flat[$prefix.$key] = (string) $value;
}
};
$flatten($lines, '');
return $flat;
}
/**
* @return list<string>
*/
function placeholdersOf(string $text): array
{
// A placeholder starts a word: "ALV:n" (Finnish case ending) is not one.
preg_match_all('/(?<![\p{L}\d]):([a-z_]+)/u', $text, $matches);
$names = array_values(array_unique($matches[1]));
sort($names);
return $names;
}
/**
* @param array<string, string> $finnish
* @param array<string, string> $english
*/
function expectSameTexts(array $finnish, array $english, string $file): void
{
expect(array_keys($english))->toEqualCanonicalizing(array_keys($finnish));
foreach ($finnish as $key => $text) {
expect(placeholdersOf($english[$key]))->toBe(placeholdersOf($text), "Placeholders differ in {$file}: {$key}");
}
}
dataset('translation files', fn () => array_map('basename', glob(langPath('fi/*.php'))));
it('has every Finnish text in English with the same placeholders', function (string $file) {
expect(file_exists(langPath("en/{$file}")))->toBeTrue("lang/en/{$file} is missing");
expectSameTexts(translationLines('fi', $file), translationLines('en', $file), $file);
})->with('translation files');
it('has no English translation file without a Finnish one', function () {
$finnish = array_map('basename', glob(langPath('fi/*.php')));
$english = array_map('basename', glob(langPath('en/*.php')));
expect($english)->toEqualCanonicalizing($finnish);
});
it('has the same JSON texts in Finnish and English', function () {
$finnish = json_decode((string) file_get_contents(langPath('fi.json')), true, flags: JSON_THROW_ON_ERROR);
$english = json_decode((string) file_get_contents(langPath('en.json')), true, flags: JSON_THROW_ON_ERROR);
expectSameTexts($finnish, $english, 'fi.json / en.json');
});