feat: global instance in the builder's format, deglobalize, library content rules
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace LIT\MCP\Tests\Unit\Integrations\Divi\BlockTree;
|
||||
|
||||
use LIT\MCP\Integrations\Divi\BlockTree\Block;
|
||||
use LIT\MCP\Integrations\Divi\BlockTree\GlobalInstance;
|
||||
use LIT\MCP\Integrations\Divi\BlockTree\LibraryContent;
|
||||
use LIT\MCP\Integrations\Divi\BlockTree\NodeIndex;
|
||||
use LIT\MCP\Integrations\Divi\BlockTree\Parser;
|
||||
use LIT\MCP\Integrations\Divi\BlockTree\Serializer;
|
||||
use LIT\MCP\Tests\Support\DiviFixtures;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class GlobalInstanceTest extends TestCase
|
||||
{
|
||||
private static function libraryRoot(string $file): Block
|
||||
{
|
||||
return LibraryContent::roots(LibraryContent::load(new Parser(), DiviFixtures::markup('stage3/' . $file)))[0];
|
||||
}
|
||||
|
||||
private static function pageIndex(): NodeIndex
|
||||
{
|
||||
return NodeIndex::build((new Parser())->parse(DiviFixtures::markup('stage3/page--globale-instanzen.html')));
|
||||
}
|
||||
|
||||
/** @return array<string, array{string, int, string}> */
|
||||
public static function instances(): array
|
||||
{
|
||||
return [
|
||||
'global section' => ['library-global--globaler-abschnitt.html', 1058, '<!-- /wp:divi/global-layout -->'],
|
||||
'global module (void)' => ['library-global--globales-modul.html', 1059, '/-->'],
|
||||
];
|
||||
}
|
||||
|
||||
/** @dataProvider instances */
|
||||
public function testInstanceMatchesTheVisualBuilderByteForByte(string $file, int $id, string $end): void
|
||||
{
|
||||
$page = DiviFixtures::markup('stage3/page--globale-instanzen.html');
|
||||
$start = (int) strpos($page, '<!-- wp:divi/global-layout {"globalModule":"' . $id . '"');
|
||||
$expected = substr($page, $start, (int) strpos($page, $end, $start) + strlen($end) - $start);
|
||||
|
||||
$instance = GlobalInstance::of(self::libraryRoot($file), $id);
|
||||
|
||||
self::assertSame($expected, Serializer::block($instance));
|
||||
self::assertTrue($instance->isOpaque());
|
||||
self::assertTrue($instance->isGlobal());
|
||||
}
|
||||
|
||||
public function testOfLeavesTheLibraryRootUnchanged(): void
|
||||
{
|
||||
$root = self::libraryRoot('library-global--globaler-abschnitt.html');
|
||||
$before = Serializer::block($root);
|
||||
|
||||
GlobalInstance::of($root, 1058);
|
||||
|
||||
self::assertSame($before, Serializer::block($root));
|
||||
}
|
||||
|
||||
public function testEffectiveNameOfAnInstanceIsItsRootType(): void
|
||||
{
|
||||
$index = self::pageIndex();
|
||||
|
||||
self::assertSame('divi/section', GlobalInstance::effectiveName($index->find('g0')));
|
||||
self::assertSame('divi/text', GlobalInstance::effectiveName($index->find('s0.r0.c0.g0')));
|
||||
self::assertSame('divi/section', GlobalInstance::effectiveName($index->find('s0')));
|
||||
}
|
||||
|
||||
public function testDeglobalizeRestoresTheLibraryRoot(): void
|
||||
{
|
||||
$root = self::libraryRoot('library-global--globaler-abschnitt.html');
|
||||
|
||||
$plain = GlobalInstance::deglobalize(self::pageIndex()->find('g0')->copy());
|
||||
|
||||
self::assertSame('divi/section', $plain->name());
|
||||
self::assertFalse($plain->isOpaque());
|
||||
self::assertEquals($root->attrs(), $plain->attrs());
|
||||
self::assertSame(
|
||||
array_map([Serializer::class, 'block'], $root->children()),
|
||||
array_map([Serializer::class, 'block'], $plain->children())
|
||||
);
|
||||
self::assertStringNotContainsString('globalParent', Serializer::block($plain));
|
||||
}
|
||||
|
||||
public function testDeglobalizeReachesNestedInstances(): void
|
||||
{
|
||||
$section = GlobalInstance::deglobalize(self::pageIndex()->find('s0')->copy());
|
||||
|
||||
$markup = Serializer::block($section);
|
||||
self::assertStringNotContainsString('global-layout', $markup);
|
||||
self::assertStringContainsString('<!-- wp:divi/text {"builderVersion":"5.2.1","content":', $markup);
|
||||
}
|
||||
|
||||
/** Stored content is not trusted: an instance with an unusable blockName stays an opaque instance. */
|
||||
public function testDeglobalizeKeepsAnInstanceWithAnInvalidBlockName(): void
|
||||
{
|
||||
$markup = '<!-- wp:divi/global-layout {"globalModule":"5","blockName":"Divi Text"} /-->';
|
||||
$instance = (new Parser())->parse($markup)->children()[0];
|
||||
|
||||
$result = GlobalInstance::deglobalize($instance);
|
||||
|
||||
self::assertSame($instance, $result);
|
||||
self::assertSame($markup, Serializer::block($result));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user