Files
plugi/plugi-core/tests/page-builder-translation-sync.mjs
T
deepDiverPaul a07e3ad8d9 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.
2026-11-25 14:53:35 +01:00

110 lines
4.0 KiB
JavaScript

import assert from 'node:assert/strict';
import fs from 'node:fs';
import vm from 'node:vm';
const sourcePath = new URL('../src/Modules/GrapesJS/resources/assets/js/save-page.js', import.meta.url);
const source = fs.readFileSync(sourcePath, 'utf8');
class TestElement {
constructor() {
this._innerHTML = '';
}
set innerHTML(value) {
this._innerHTML = value;
}
get innerHTML() {
return this._innerHTML;
}
querySelectorAll() {
const pattern = /<div([^>]*\bphpb-blocks-container\b[^>]*)>([\s\S]*?)<\/div>/g;
return [...this._innerHTML.matchAll(pattern)].map((match, index) => {
return new TestContainer(this, index, `<div${match[1]}>`, match[2]);
});
}
replaceContainer(index, containerHtml) {
const pattern = /<div([^>]*\bphpb-blocks-container\b[^>]*)>([\s\S]*?)<\/div>/g;
let currentIndex = 0;
this._innerHTML = this._innerHTML.replace(pattern, match => {
return currentIndex++ === index ? containerHtml : match;
});
}
appendChild(container) {
this._innerHTML += container.outerHTML;
}
}
class TestContainer {
constructor(parent, index, openingTag, innerHTML) {
this.parent = parent;
this.index = index;
this.openingTag = openingTag;
this._innerHTML = innerHTML;
}
set innerHTML(value) {
this._innerHTML = value;
this.parent.replaceContainer(this.index, `${this.openingTag}${value}</div>`);
}
get innerHTML() {
return this._innerHTML;
}
get outerHTML() {
return `${this.openingTag}${this._innerHTML}</div>`;
}
cloneNode() {
return new TestContainer(null, 0, this.openingTag, this._innerHTML);
}
}
const window = {};
const document = {
getElementById: () => null,
addEventListener: () => {},
createElement: () => new TestElement(),
};
vm.runInNewContext(source, { window, document, console, setTimeout, URLSearchParams });
const sourceBlocks = {
parent: {
html: '<p>Source locale text</p><div phpb-blocks-container>[block slug="child" id="new-child"]</div>',
settings: {label: 'Source setting'},
blocks: {
existingChild: {html: '<p>Source child text</p>', settings: {label: 'Source child setting'}},
newChild: {html: '<p>New shared block</p>', settings: {label: 'New setting'}},
},
},
newParent: {html: '<p>New shared parent</p>', settings: {label: 'New parent setting'}},
};
const targetBlocks = {
parent: {
html: '<p>Translated parent text</p><div phpb-blocks-container>[block slug="child" id="existing-child"]</div>',
settings: {label: 'Translated parent setting'},
blocks: {
existingChild: {html: '<p>Existing translated child</p>', settings: {label: 'Translated child setting'}},
},
},
localeOnlyParent: {html: '<p>Existing locale-only content</p>'},
};
const synchronizedBlocks = window.synchronizePageBlockVariants(sourceBlocks, targetBlocks);
assert.match(synchronizedBlocks.parent.html, /Translated parent text/);
assert.match(synchronizedBlocks.parent.html, /id="new-child"/);
assert.equal(synchronizedBlocks.parent.settings.label, 'Translated parent setting');
assert.equal(synchronizedBlocks.parent.blocks.existingChild.html, '<p>Existing translated child</p>');
assert.equal(synchronizedBlocks.parent.blocks.existingChild.settings.label, 'Translated child setting');
assert.equal(synchronizedBlocks.parent.blocks.newChild.html, '<p>New shared block</p>');
assert.equal(synchronizedBlocks.newParent.html, '<p>New shared parent</p>');
assert.equal(synchronizedBlocks.localeOnlyParent.html, '<p>Existing locale-only content</p>');
sourceBlocks.parent.blocks.newChild.html = '<p>Source changed after synchronization</p>';
assert.equal(synchronizedBlocks.parent.blocks.newChild.html, '<p>New shared block</p>');
console.log('PASS: shared block structure synchronizes without overwriting existing locale content.');