fix localized page management and builder synchronization

Validate locales and route uniqueness, report page deletion failures, and keep translation CRUD and route caches consistent. Show translation completeness in page settings and listings, and preserve locale-specific block data while synchronizing shared structure. Add translation service and page builder synchronization tests.
This commit is contained in:
2026-11-25 14:53:35 +01:00
parent 9d192c2141
commit a07e3ad8d9
19 changed files with 983 additions and 122 deletions
@@ -0,0 +1,111 @@
<?php
require_once __DIR__ . '/../src/PageTranslationService.php';
$pageTranslationServiceConfig = [
'general' => [
'language' => 'en',
],
];
function phpb_config(?string $key = null): mixed
{
global $pageTranslationServiceConfig;
if ($key === null) {
return $pageTranslationServiceConfig;
}
$value = $pageTranslationServiceConfig;
foreach (explode('.', $key) as $segment) {
if (! is_array($value) || ! array_key_exists($segment, $value)) {
return '';
}
$value = $value[$segment];
}
return $value;
}
function assertPageTranslationService(bool $condition, string $message): void
{
if (! $condition) {
throw new RuntimeException($message);
}
}
function runPageTranslationServiceCase(string $name, callable $case): bool
{
try {
$case();
fwrite(STDOUT, "PASS: {$name}\n");
return true;
} catch (Throwable $exception) {
fwrite(STDERR, "FAIL: {$name}: {$exception->getMessage()}\n");
return false;
}
}
$service = new Plugi\PageTranslationService();
$cases = [
'requested locale wins when available' => function () use ($service): void {
assertPageTranslationService(
$service->resolveLocale('fr', ['de', 'fr']) === 'fr',
'An available requested locale should be selected before the configured default.'
);
},
'configured locale wins when requested locale is absent' => function () use ($service): void {
global $pageTranslationServiceConfig;
$pageTranslationServiceConfig['general']['language'] = 'fr';
assertPageTranslationService(
$service->resolveLocale('it', ['de', 'fr']) === 'fr',
'The configured default should be used when the requested locale is unavailable.'
);
},
'first available locale is used when configured locale is unavailable' => function () use ($service): void {
global $pageTranslationServiceConfig;
$pageTranslationServiceConfig['general']['language'] = 'en';
assertPageTranslationService(
$service->resolveLocale('it', ['de', 'fr']) === 'de',
'The first available locale should be used without assuming an English translation exists.'
);
},
'no available locale resolves to null' => function () use ($service): void {
assertPageTranslationService($service->resolveLocale('en', []) === null, 'An empty locale list should resolve to null.');
},
'unsupported locales are rejected instead of receiving fallback' => function () use ($service): void {
assertPageTranslationService(
$service->isAvailableLocale('en', ['en' => 'English'])
&& ! $service->isAvailableLocale('it', ['en' => 'English']),
'Locale validation should accept active languages only and must not apply fallback.'
);
},
'translation values follow the same fallback order and preserve empty strings' => function () use ($service): void {
global $pageTranslationServiceConfig;
$pageTranslationServiceConfig['general']['language'] = 'fr';
$translations = [
'de' => ['title' => 'Deutscher Titel', 'meta_title' => ''],
'fr' => ['title' => 'Titre français', 'meta_title' => 'Titre de métadonnées'],
];
assertPageTranslationService(
$service->resolveTranslation($translations, 'title', 'it') === 'Titre français',
'Translation metadata did not use the configured locale fallback.'
);
assertPageTranslationService(
$service->resolveTranslation($translations, 'meta_title', 'de') === '',
'An intentionally empty translation should not be replaced by fallback text.'
);
},
];
$passed = 0;
foreach ($cases as $name => $case) {
if (runPageTranslationServiceCase($name, $case)) {
$passed++;
}
}
fwrite(STDOUT, sprintf("%d/%d page translation service cases passed.\n", $passed, count($cases)));
exit($passed === count($cases) ? 0 : 1);