Files
WP-LIT-MCP/tests/Unit/Integrations/Divi/BlockTree/BlockTest.php
T
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

78 lines
2.6 KiB
PHP

<?php
declare(strict_types=1);
namespace LIT\MCP\Tests\Unit\Integrations\Divi\BlockTree;
use InvalidArgumentException;
use LIT\MCP\Integrations\Divi\BlockTree\Block;
use LIT\MCP\Integrations\Divi\BlockTree\Parser;
use LogicException;
use PHPUnit\Framework\TestCase;
use stdClass;
final class BlockTest extends TestCase
{
/** @return array<string, array{string}> */
public static function invalidNames(): array
{
return [
'no namespace' => ['button'],
'trailing markup injection' => ['divi/x --><script>'],
'trailing newline' => ["divi/x\n"],
'uppercase' => ['Divi/Button'],
'empty' => [''],
];
}
/** @dataProvider invalidNames */
public function testCreateRejectsInvalidBlockNames(string $name): void
{
$this->expectException(InvalidArgumentException::class);
Block::create($name);
}
public function testCreateAcceptsAWellFormedName(): void
{
self::assertSame('divi/button', Block::create('divi/button')->name());
}
public function testOnlyThePlaceholderIsAPlaceholder(): void
{
self::assertTrue(Block::create('divi/placeholder')->isPlaceholder());
self::assertFalse(Block::create('divi/section')->isPlaceholder());
self::assertFalse(Block::create('core/placeholder')->isPlaceholder());
}
public function testSetAttrsRefusesOnAnOpaqueGlobalModule(): void
{
$document = (new Parser())->parse('<!-- wp:divi/section {"globalModule":"x"} --><!-- /wp:divi/section -->');
$section = $document->children()[0];
self::assertTrue($section->isOpaque());
$this->expectException(LogicException::class);
$section->setAttrs(['a' => 1]);
}
public function testSetAttrsRefusesOnABlockWithUndecodableAttrs(): void
{
$document = (new Parser())->parse('<!-- wp:divi/text {"a":} /-->');
$block = $document->children()[0];
self::assertTrue($block->isOpaque());
$this->expectException(LogicException::class);
$block->setAttrs(['a' => 1]);
}
public function testCopyDeepClonesStdClassAttributeValues(): void
{
$block = Block::create('divi/text', ['localAttrs' => new stdClass(), 'nested' => ['obj' => new stdClass()]]);
$copy = $block->copy();
self::assertNotSame($block->attrs()['localAttrs'], $copy->attrs()['localAttrs']);
self::assertEquals($block->attrs()['localAttrs'], $copy->attrs()['localAttrs']);
self::assertNotSame($block->attrs()['nested']['obj'], $copy->attrs()['nested']['obj']);
}
}