Files
janniclas.loosenandClaude Opus 5.5 d2cc82f31b refactor: small Divi tree helpers out of DiviWriter (M3)
Block::isPlaceholder replaces the divi/placeholder literals (ledger T4);
NodeIndex::parentBlock/sectionLabel replace the two parentBlock copies and
DiviWriter::sectionLabel; DiviChange::idOf and Outline::summary replace the
duplicated id resolution and outline text; DiviSchema holds the shared
input properties. Behaviour unchanged.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01At3V6SjwbMe6eMzVGW3Hs7
2026-09-26 18:24:29 +02:00

76 lines
3.2 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 testParentBlockSkipsThePlaceholderAndSectionLabelWalksUp(): void
{
$index = NodeIndex::build((new Parser())->parse($this->fixture('synthetic-basic.html')));
$button = $index->find('s0.r0.c1.m2');
self::assertTrue($index->root() instanceof \LIT\MCP\Integrations\Divi\BlockTree\Block
&& $index->root()->isPlaceholder());
self::assertSame($index->find('s0.r0.c1'), $index->parentBlock($button));
self::assertNull($index->parentBlock($index->find('s0')), 'the placeholder is no parent node');
self::assertSame('Hero', $index->sectionLabel($button));
self::assertSame('Hero', $index->sectionLabel($index->find('s0')));
self::assertSame('', $index->sectionLabel(null));
}
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);
}
}