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:
@@ -21,6 +21,7 @@ require_once __DIR__ . '/../src/Contracts/ThemeContract.php';
|
||||
require_once __DIR__ . '/../src/Core/DB.php';
|
||||
require_once __DIR__ . '/../src/Repositories/BaseRepository.php';
|
||||
require_once __DIR__ . '/../src/Repositories/PageTranslationRepository.php';
|
||||
require_once __DIR__ . '/../src/PageTranslationService.php';
|
||||
require_once __DIR__ . '/../src/Page.php';
|
||||
require_once __DIR__ . '/../src/PageTranslation.php';
|
||||
require_once __DIR__ . '/../src/Repositories/PageRepository.php';
|
||||
@@ -31,8 +32,13 @@ require_once __DIR__ . '/../src/Modules/GrapesJS/PageBuilder.php';
|
||||
class PageCreationTestDatabase extends DB
|
||||
{
|
||||
public int $translationInsertCount = 0;
|
||||
public int $pageTranslationAllCallCount = 0;
|
||||
public int $pageTranslationBatchSelectCount = 0;
|
||||
public ?int $failOnTranslationInsert = null;
|
||||
public bool $throwOnTranslationFailure = false;
|
||||
public ?string $failOnDeleteTable = null;
|
||||
public bool $throwOnDeleteFailure = false;
|
||||
public bool $failOnPageUpdate = false;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
@@ -43,6 +49,16 @@ class PageCreationTestDatabase extends DB
|
||||
|
||||
public function query(string $query, array $parameters = [])
|
||||
{
|
||||
if ($this->failOnDeleteTable !== null && str_starts_with($query, 'DELETE FROM ' . $this->failOnDeleteTable)) {
|
||||
if ($this->throwOnDeleteFailure) {
|
||||
throw new PDOException('Simulated page deletion failure.');
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if ($this->failOnPageUpdate && str_starts_with($query, 'UPDATE pages SET')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (str_starts_with($query, 'INSERT INTO page_translations')) {
|
||||
$this->translationInsertCount++;
|
||||
if ($this->translationInsertCount === $this->failOnTranslationInsert) {
|
||||
@@ -56,6 +72,24 @@ class PageCreationTestDatabase extends DB
|
||||
return parent::query($query, $parameters);
|
||||
}
|
||||
|
||||
public function all(string $table, array|string $columns = '*'): array
|
||||
{
|
||||
if ($table === 'page_translations') {
|
||||
$this->pageTranslationAllCallCount++;
|
||||
}
|
||||
|
||||
return parent::all($table, $columns);
|
||||
}
|
||||
|
||||
public function select(string $query, array $parameters): array
|
||||
{
|
||||
if (str_starts_with($query, 'SELECT * FROM page_translations WHERE page_id IN (')) {
|
||||
$this->pageTranslationBatchSelectCount++;
|
||||
}
|
||||
|
||||
return parent::select($query, $parameters);
|
||||
}
|
||||
|
||||
public function createSchema(): void
|
||||
{
|
||||
$this->pdo->exec('CREATE TABLE pages (
|
||||
@@ -102,8 +136,11 @@ class PageCreationTestWebsiteManager extends WebsiteManager
|
||||
|
||||
class PageCreationTestCache
|
||||
{
|
||||
public static array $invalidatedRoutes = [];
|
||||
|
||||
public function invalidate(string $route): void
|
||||
{
|
||||
self::$invalidatedRoutes[] = $route;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -165,6 +202,11 @@ function phpb_url(string $module, array $parameters = []): string
|
||||
return '/' . $module;
|
||||
}
|
||||
|
||||
function phpb_full_url(string $route): string
|
||||
{
|
||||
return 'https://example.test' . $route;
|
||||
}
|
||||
|
||||
function phpb_csrf_field(): void
|
||||
{
|
||||
}
|
||||
@@ -175,6 +217,9 @@ function resetPageCreationDatabase(array $languages = ['en' => [], 'de' => []]):
|
||||
|
||||
$_ENV['DATABASE_PREFIX'] = '';
|
||||
$pageCreationConfig = [
|
||||
'general' => [
|
||||
'language' => 'en',
|
||||
],
|
||||
'page' => [
|
||||
'table' => 'pages',
|
||||
'translation' => [
|
||||
@@ -184,6 +229,7 @@ function resetPageCreationDatabase(array $languages = ['en' => [], 'de' => []]):
|
||||
],
|
||||
];
|
||||
$pageCreationLanguages = $languages;
|
||||
PageCreationTestCache::$invalidatedRoutes = [];
|
||||
$phpb_db = new PageCreationTestDatabase();
|
||||
$phpb_db->createSchema();
|
||||
}
|
||||
@@ -222,6 +268,21 @@ function runPageCreationCase(string $name, callable $case): bool
|
||||
}
|
||||
|
||||
$cases = [
|
||||
'configured locale is preferred over the first available translation' => function (): void {
|
||||
resetPageCreationDatabase(['de' => [], 'fr' => []]);
|
||||
global $pageCreationConfig;
|
||||
$pageCreationConfig['general']['language'] = 'fr';
|
||||
$page = new Page();
|
||||
$page->setTranslations([
|
||||
'de' => ['title' => 'Deutscher Titel'],
|
||||
'fr' => ['title' => 'Titre français'],
|
||||
]);
|
||||
|
||||
assertPageCreation(
|
||||
$page->getTranslation('title', 'it') === 'Titre français',
|
||||
'Missing requested locale should fall back to the configured default, not the first saved locale.'
|
||||
);
|
||||
},
|
||||
'locked generated routes do not leave a base page without translations' => function (): void {
|
||||
resetPageCreationDatabase();
|
||||
$payload = pageCreationPayload();
|
||||
@@ -321,6 +382,54 @@ $cases = [
|
||||
assertPageCreation(str_contains($malformedHtml, '\\u0027\\u003C/script\\u003E'), 'Submitted route was not safely encoded for Alpine.');
|
||||
$_POST = [];
|
||||
},
|
||||
'page settings leave missing locale values blank and explain the missing translation' => function (): void {
|
||||
resetPageCreationDatabase();
|
||||
$page = new Page();
|
||||
$page->setData([
|
||||
'id' => 42,
|
||||
'name' => 'English page',
|
||||
'layout' => 'default',
|
||||
'type' => 'static',
|
||||
]);
|
||||
$page->setTranslations([
|
||||
'en' => [
|
||||
'title' => 'English title',
|
||||
'route' => '/english-page',
|
||||
'meta_title' => '',
|
||||
'meta_description' => '',
|
||||
'target' => '',
|
||||
],
|
||||
]);
|
||||
$_POST = [];
|
||||
$action = 'edit';
|
||||
$errorMessage = null;
|
||||
$theme = new class {
|
||||
public function getThemeLayouts(): array
|
||||
{
|
||||
return [new class {
|
||||
public function getSlug(): string
|
||||
{
|
||||
return 'default';
|
||||
}
|
||||
|
||||
public function getTitle(): string
|
||||
{
|
||||
return 'Default';
|
||||
}
|
||||
}];
|
||||
}
|
||||
};
|
||||
|
||||
ob_start();
|
||||
include __DIR__ . '/../src/Modules/WebsiteManager/resources/views/page-settings.php';
|
||||
$html = ob_get_clean();
|
||||
|
||||
preg_match('/name="title\[de\]" value="([^"]*)"/', $html, $germanTitle);
|
||||
assertPageCreation(($germanTitle[1] ?? null) === '', 'Missing German title was filled from the English translation.');
|
||||
assertPageCreation(str_contains($html, 'No saved translation exists for this language.'), 'The missing locale was not explained.');
|
||||
assertPageCreation(str_contains($html, 'Each language shows its own saved values; missing translations are left blank instead of using fallback text.'), 'Locale-specific editing guidance was not shown.');
|
||||
$_POST = [];
|
||||
},
|
||||
'generated routes persist for every active locale' => function (): void {
|
||||
resetPageCreationDatabase();
|
||||
|
||||
@@ -403,6 +512,156 @@ $cases = [
|
||||
assertPageCreation($secondPage === false, 'Duplicate route was not rejected.');
|
||||
assertPageCreation(count($phpb_db->rows('pages')) === 1, 'Duplicate create left an extra base page.');
|
||||
assertPageCreation(count($phpb_db->rows('page_translations')) === 2, 'Duplicate create changed translations.');
|
||||
assertPageCreation($phpb_db->pageTranslationAllCallCount === 0, 'Route validation loaded every translation instead of querying for a collision.');
|
||||
},
|
||||
'route collision checks allow an unchanged page route and reject another locale collision' => function (): void {
|
||||
resetPageCreationDatabase();
|
||||
$repository = new PageRepository();
|
||||
$repository->create(pageCreationPayload());
|
||||
global $phpb_db;
|
||||
$page = $repository->findWithId($phpb_db->rows('pages')[0]['id']);
|
||||
|
||||
assertPageCreation($repository->update($page, pageCreationPayload()) === true, 'An unchanged route should not collide with its own page.');
|
||||
assertPageCreation(
|
||||
$repository->create(pageCreationPayload([
|
||||
'name' => 'German route collision',
|
||||
'route' => ['en' => '/unique-route', 'de' => '/de/new-page'],
|
||||
])) === false,
|
||||
'A route collision in a non-default locale was not rejected.'
|
||||
);
|
||||
assertPageCreation($phpb_db->pageTranslationAllCallCount === 0, 'Route validation loaded every translation instead of querying for a collision.');
|
||||
},
|
||||
'page locale coverage summarizes complete partial and absent translations in one batch' => function (): void {
|
||||
resetPageCreationDatabase();
|
||||
$repository = new PageRepository();
|
||||
$repository->create(pageCreationPayload([
|
||||
'route' => ['en' => '/complete-en', 'de' => '/complete-de'],
|
||||
]));
|
||||
$repository->create(pageCreationPayload([
|
||||
'name' => 'Partial page',
|
||||
'route' => ['en' => '/partial-en', 'de' => '/partial-de'],
|
||||
]));
|
||||
$repository->create(pageCreationPayload([
|
||||
'name' => 'Missing locale page',
|
||||
'route' => ['en' => '/missing-en', 'de' => '/missing-de'],
|
||||
]));
|
||||
$pages = $repository->getAll();
|
||||
$pageRows = [];
|
||||
foreach ($pages as $page) {
|
||||
$pageRows[$page->getId()] = $page;
|
||||
}
|
||||
|
||||
global $phpb_db;
|
||||
$pageIds = array_keys($pageRows);
|
||||
$phpb_db->query('UPDATE page_translations SET title = ? WHERE page_id = ? AND locale = ?', ['', $pageIds[1], 'de']);
|
||||
$phpb_db->query('DELETE FROM page_translations WHERE page_id = ? AND locale = ?', [$pageIds[2], 'de']);
|
||||
$missingPage = new Page();
|
||||
$missingPage->setData(['id' => 999, 'type' => 'static']);
|
||||
$pages[] = $missingPage;
|
||||
$phpb_db->pageTranslationBatchSelectCount = 0;
|
||||
|
||||
$coverage = (new Plugi\PageTranslationService())->getPageCoverage($pages, phpb_active_languages());
|
||||
|
||||
assertPageCreation($coverage[$pageIds[0]]['completeCount'] === 2, 'A complete page did not report all active locales complete.');
|
||||
assertPageCreation($coverage[$pageIds[1]]['completeCount'] === 1, 'A partially complete page reported the wrong coverage count.');
|
||||
assertPageCreation($coverage[$pageIds[1]]['locales']['de']['exists'] === true, 'An incomplete saved locale was reported as missing.');
|
||||
assertPageCreation($coverage[$pageIds[1]]['locales']['de']['complete'] === false, 'An incomplete saved locale was reported as complete.');
|
||||
assertPageCreation($coverage[$pageIds[2]]['locales']['de']['exists'] === false, 'An absent locale was reported as saved.');
|
||||
assertPageCreation($coverage[999]['completeCount'] === 0, 'A page without translation records reported complete locales.');
|
||||
assertPageCreation($phpb_db->pageTranslationBatchSelectCount === 1, 'Translation coverage used more than one batched repository query.');
|
||||
|
||||
$activeLanguages = phpb_active_languages();
|
||||
$pageTranslationCoverage = $coverage;
|
||||
ob_start();
|
||||
include __DIR__ . '/../src/Modules/WebsiteManager/resources/views/tabs/pages.php';
|
||||
$html = ob_get_clean();
|
||||
assertPageCreation(str_contains($html, '/complete-en'), 'The page overview did not render the saved English route.');
|
||||
assertPageCreation(str_contains($html, '/complete-de'), 'The page overview did not render the saved German route.');
|
||||
assertPageCreation(str_contains($html, 'New page'), 'The page overview did not render a saved localized title.');
|
||||
assertPageCreation(str_contains($html, '2 / 2 complete'), 'The page overview did not render the translation coverage summary.');
|
||||
},
|
||||
'successful route updates invalidate old and new localized routes' => function (): void {
|
||||
resetPageCreationDatabase();
|
||||
$repository = new PageRepository();
|
||||
$repository->create(pageCreationPayload());
|
||||
global $phpb_db;
|
||||
$page = $repository->findWithId($phpb_db->rows('pages')[0]['id']);
|
||||
|
||||
$result = $repository->update($page, pageCreationPayload([
|
||||
'route' => ['en' => '/changed-en', 'de' => '/changed-de'],
|
||||
]));
|
||||
|
||||
assertPageCreation($result === true, 'A valid route update failed.');
|
||||
assertPageCreation(
|
||||
PageCreationTestCache::$invalidatedRoutes === ['/changed-de', '/changed-en', '/de/new-page', '/new-page'],
|
||||
'A successful route update did not invalidate both old and new localized route caches: ' . json_encode(PageCreationTestCache::$invalidatedRoutes)
|
||||
);
|
||||
},
|
||||
'page deletion removes translations and invalidates routes after commit' => function (): void {
|
||||
resetPageCreationDatabase();
|
||||
$repository = new PageRepository();
|
||||
$repository->create(pageCreationPayload([
|
||||
'route' => ['en' => '/delete-en', 'de' => '/delete-de'],
|
||||
]));
|
||||
global $phpb_db;
|
||||
$pageId = $phpb_db->rows('pages')[0]['id'];
|
||||
|
||||
assertPageCreation($repository->destroy($pageId) === true, 'Page deletion failed.');
|
||||
assertPageCreation(count($phpb_db->rows('pages')) === 0, 'Page deletion left the base page.');
|
||||
assertPageCreation(count($phpb_db->rows('page_translations')) === 0, 'Page deletion left translation rows.');
|
||||
assertPageCreation(
|
||||
PageCreationTestCache::$invalidatedRoutes === ['/delete-de', '/delete-en'],
|
||||
'Successful deletion did not invalidate every stored localized route: ' . json_encode(PageCreationTestCache::$invalidatedRoutes)
|
||||
);
|
||||
},
|
||||
'failed page deletion rolls back translation cleanup without invalidating caches' => function (): void {
|
||||
resetPageCreationDatabase();
|
||||
$repository = new PageRepository();
|
||||
$repository->create(pageCreationPayload());
|
||||
global $phpb_db;
|
||||
$pageId = $phpb_db->rows('pages')[0]['id'];
|
||||
$phpb_db->failOnDeleteTable = 'pages';
|
||||
|
||||
assertPageCreation($repository->destroy($pageId) === false, 'A failed base-row deletion was reported as successful.');
|
||||
assertPageCreation(count($phpb_db->rows('pages')) === 1, 'Failed deletion removed the base page.');
|
||||
assertPageCreation(count($phpb_db->rows('page_translations')) === 2, 'Failed deletion did not roll back translation cleanup.');
|
||||
assertPageCreation(PageCreationTestCache::$invalidatedRoutes === [], 'Failed deletion invalidated route caches.');
|
||||
},
|
||||
'exception during page deletion rolls back rows and leaves caches intact' => function (): void {
|
||||
resetPageCreationDatabase();
|
||||
$repository = new PageRepository();
|
||||
$repository->create(pageCreationPayload());
|
||||
global $phpb_db;
|
||||
$pageId = $phpb_db->rows('pages')[0]['id'];
|
||||
$phpb_db->failOnDeleteTable = 'pages';
|
||||
$phpb_db->throwOnDeleteFailure = true;
|
||||
|
||||
try {
|
||||
$repository->destroy($pageId);
|
||||
} catch (PDOException) {
|
||||
}
|
||||
|
||||
assertPageCreation(count($phpb_db->rows('pages')) === 1, 'Deletion exception removed the base page.');
|
||||
assertPageCreation(count($phpb_db->rows('page_translations')) === 2, 'Deletion exception did not roll back translation cleanup.');
|
||||
assertPageCreation(PageCreationTestCache::$invalidatedRoutes === [], 'Deletion exception invalidated route caches.');
|
||||
},
|
||||
'page content cache invalidation occurs only after successful writes' => function (): void {
|
||||
resetPageCreationDatabase();
|
||||
$repository = new PageRepository();
|
||||
$repository->create(pageCreationPayload());
|
||||
global $phpb_db;
|
||||
$page = $repository->findWithId($phpb_db->rows('pages')[0]['id']);
|
||||
$phpb_db->failOnPageUpdate = true;
|
||||
|
||||
assertPageCreation($repository->updatePageData($page, ['html' => []]) === false, 'A failed page-data update was reported as successful.');
|
||||
assertPageCreation(PageCreationTestCache::$invalidatedRoutes === [], 'A failed page-data update invalidated route caches.');
|
||||
|
||||
$phpb_db->failOnPageUpdate = false;
|
||||
assertPageCreation($repository->updatePageData($page, ['html' => []]) === true, 'A valid page-data update failed.');
|
||||
assertPageCreation(
|
||||
PageCreationTestCache::$invalidatedRoutes === ['/de/new-page', '/new-page'],
|
||||
'A successful page-data update did not invalidate localized route caches: ' . json_encode(PageCreationTestCache::$invalidatedRoutes)
|
||||
);
|
||||
},
|
||||
'translation write failures roll back the base page and translations' => function (): void {
|
||||
resetPageCreationDatabase();
|
||||
@@ -449,6 +708,7 @@ $cases = [
|
||||
assertPageCreation(count($translations) === 2, 'Failed update changed the translation count.');
|
||||
assertPageCreation($translations[0]['route'] === '/new-page', 'Failed update did not restore the English route.');
|
||||
assertPageCreation($translations[1]['route'] === '/de/new-page', 'Failed update did not restore the German route.');
|
||||
assertPageCreation(PageCreationTestCache::$invalidatedRoutes === [], 'Failed metadata update invalidated route caches.');
|
||||
},
|
||||
];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user