Files
WP-LIT-MCP/tests/Unit/Integrations/Divi/BlockTree/ContainerTest.php
T

104 lines
3.6 KiB
PHP

<?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\Parser;
use LIT\MCP\Integrations\Divi\BlockTree\Serializer;
use LogicException;
use PHPUnit\Framework\TestCase;
final class ContainerTest extends TestCase
{
public function testInsertChildRefusesOnAnOpaqueBlock(): 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->insertChild(0, Block::create('divi/text'));
}
public function testInsertChildRefusesOnABlockWithUndecodableAttrs(): void
{
$document = (new Parser())->parse('<!-- wp:divi/section {"a":} --><!-- /wp:divi/section -->');
$section = $document->children()[0];
self::assertTrue($section->isOpaque());
$this->expectException(LogicException::class);
$section->insertChild(0, Block::create('divi/text'));
}
public function testRemoveChildRefusesOnAnOpaqueBlock(): void
{
$document = (new Parser())->parse(
'<!-- wp:divi/section {"globalModule":"x"} --><!-- wp:divi/row --><!-- /wp:divi/row --><!-- /wp:divi/section -->'
);
$section = $document->children()[0];
$row = $section->children()[0];
self::assertTrue($section->isOpaque());
$this->expectException(LogicException::class);
$section->removeChild($row);
}
public function testAppendRefusesOnAVoidBlock(): void
{
$void = Block::parsed('divi/image', [], '<!-- wp:divi/image /-->', true, false);
$this->expectException(LogicException::class);
$void->append('text');
}
public function testReplaceChildKeepsTheTextAround(): void
{
$document = (new Parser())->parse("A<!-- wp:divi/text /-->B");
$new = Block::create('divi/button');
$document->replaceChild($document->children()[0], $new);
self::assertSame('A<!-- wp:divi/button --><!-- /wp:divi/button -->B', Serializer::document($document));
}
public function testUnifyLineBreaksOnlyTouchesWhitespaceBetweenBlocks(): void
{
$document = (new Parser())->parse(
"<!-- wp:divi/section -->\r\n<!-- wp:divi/row -->\r\n<!-- /wp:divi/row -->\r\n<!-- /wp:divi/section -->x\r\ny"
);
$document->unifyLineBreaks();
self::assertSame(
"<!-- wp:divi/section -->\n<!-- wp:divi/row -->\n<!-- /wp:divi/row -->\n<!-- /wp:divi/section -->x\r\ny",
Serializer::document($document)
);
}
/** Global instances stay opaque: their bytes are Divi's, even the whitespace inside. */
public function testUnifyLineBreaksLeavesOpaqueBlocksAlone(): void
{
$markup = "<!-- wp:divi/global-layout {\"globalModule\":\"5\"} -->\r\n<!-- wp:divi/text /-->\r\n"
. "<!-- /wp:divi/global-layout -->\r\n";
$document = (new Parser())->parse($markup);
$document->unifyLineBreaks();
self::assertSame(substr($markup, 0, -2) . "\n", Serializer::document($document));
}
public function testReplaceChildRefusesOnAnOpaqueBlock(): void
{
$document = (new Parser())->parse(
'<!-- wp:divi/global-layout {"globalModule":"5"} --><!-- wp:divi/text /--><!-- /wp:divi/global-layout -->'
);
$instance = $document->children()[0];
$this->expectException(LogicException::class);
$instance->replaceChild($instance->children()[0], Block::create('divi/button'));
}
}