Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
62 lines
2.4 KiB
PHP
62 lines
2.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace LIT\MCP\Tests\Unit\Integrations\Divi\BlockTree;
|
|
|
|
use LIT\MCP\Integrations\Divi\BlockTree\NodeIndex;
|
|
use LIT\MCP\Integrations\Divi\BlockTree\Parser;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
final class NodeIndexTest extends TestCase
|
|
{
|
|
public function testAssignsPathIdsPerParentAndLetter(): void
|
|
{
|
|
$index = NodeIndex::build((new Parser())->parse($this->fixture('synthetic-basic.html')));
|
|
|
|
self::assertSame(
|
|
[
|
|
's0', 's0.r0', 's0.r0.c0', 's0.r0.c0.m0', 's0.r0.c1', 's0.r0.c1.m0', 's0.r0.c1.m1', 's0.r0.c1.m2',
|
|
's1', 's1.r0', 's1.r0.c0', 's1.r0.c0.m0', 's1.r0.c1', 's1.r0.c1.m0', 's1.r0.c1.m0.m0',
|
|
's1.r0.c1.m0.m1',
|
|
],
|
|
array_column($index->entries(), 'id')
|
|
);
|
|
self::assertSame('divi/button', $index->find('s0.r0.c1.m2')?->name());
|
|
self::assertSame('s0.r0.c1', $index->parentId($index->find('s0.r0.c1.m2')));
|
|
self::assertNull($index->parentId($index->find('s1')));
|
|
self::assertSame('divi/placeholder', $index->root() instanceof \LIT\MCP\Integrations\Divi\BlockTree\Block
|
|
? $index->root()->name() : '');
|
|
self::assertSame($index->root(), $index->parentOf($index->find('s0')));
|
|
self::assertNull($index->find('s9'));
|
|
}
|
|
|
|
public function testGlobalAndForeignBlocksAreLeavesWithOwnLetters(): void
|
|
{
|
|
$index = NodeIndex::build((new Parser())->parse($this->fixture('synthetic-mixed.html')));
|
|
|
|
self::assertSame(
|
|
['x0', 's0', 's0.c0', 's0.c0.m0', 's0.c1', 's0.c1.r0', 's0.c1.r0.c0', 's0.c1.r0.c0.m0', 's0.c1.r0.c1',
|
|
's1', 's1.g0', 's1.g1', 's1.g2'],
|
|
array_column($index->entries(), 'id')
|
|
);
|
|
self::assertSame(1, preg_match(NodeIndex::ID_PATTERN, 's1.g2'));
|
|
self::assertSame(0, preg_match(NodeIndex::ID_PATTERN, 's1..g2'));
|
|
self::assertSame(0, preg_match(NodeIndex::ID_PATTERN, "s1\n"));
|
|
}
|
|
|
|
public function testDocumentWithoutPlaceholderIsItsOwnRoot(): void
|
|
{
|
|
$document = (new Parser())->parse('<!-- wp:divi/section /-->');
|
|
$index = NodeIndex::build($document);
|
|
|
|
self::assertSame($document, $index->root());
|
|
self::assertSame('s0', $index->entries()[0]['id']);
|
|
}
|
|
|
|
private function fixture(string $name): string
|
|
{
|
|
return (string) file_get_contents(dirname(__DIR__, 4) . '/Fixtures/divi/' . $name);
|
|
}
|
|
}
|