Files
WP-LIT-MCP/tests/Unit/Integrations/Divi/Pages/DiviReadAbilitiesTest.php
T
janniclas.loosenandClaude Opus 5.5 60290c85a5 test: AssertsToolErrors helper for tool error assertions (T3)
tests/Support/AssertsToolErrors (used by TestCase): assertToolError($result, code)
checks the WP_Error code lit_<code> and the code of the client JSON and returns the
payload; clientError() decodes the payload; errorFields() returns the fields and
asserts they exist. Replaces the private clientError() decoders of
DiviWriteAbilitiesTest/DiviReplaceTextAbilityTest, 216 single-line
assertSame('lit_…', …->get_error_code()) assertions, 24 get_error_data()['fields']
digs and two json_decode(message) reads. ErrorsTest keeps json_decode: it tests
the format itself. Tests 1404 -> 1409 (AssertsToolErrorsTest); no test has fewer
assertions.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 17:55:35 +02:00

127 lines
5.6 KiB
PHP

<?php
declare(strict_types=1);
namespace LIT\MCP\Tests\Unit\Integrations\Divi\Pages;
use LIT\MCP\Integrations\Divi\Design\DesignSystemReader;
use LIT\MCP\Integrations\Divi\Pages\DiviNodeGetAbility;
use LIT\MCP\Integrations\Divi\Pages\DiviOutlineAbility;
use LIT\MCP\Integrations\Divi\Pages\DiviPageLoader;
use LIT\MCP\Tests\Support\DiviFixtures;
use LIT\MCP\Tests\Unit\TestCase;
use Mockery;
use stdClass;
final class DiviReadAbilitiesTest extends TestCase
{
private function design(): DesignSystemReader
{
$reader = Mockery::mock(DesignSystemReader::class);
$reader->allows('read')->andReturn(DiviFixtures::designSystem());
return $reader;
}
public function testOutlineListsNodesWithTreeRevAndPostRev(): void
{
$content = DiviFixtures::markup('synthetic-basic.html');
DiviFixtures::givenPost($content);
$ability = new DiviOutlineAbility(new DiviPageLoader(DiviFixtures::schema()), $this->design());
self::assertTrue($ability->permission(['post_id' => 7]));
$result = $ability->execute(['post_id' => 7]);
self::assertSame(substr(hash('sha256', $content), 0, 12), $result['tree_rev']);
self::assertSame(12, strlen($result['rev']));
self::assertNotSame($result['tree_rev'], $result['rev']);
self::assertCount(16, $result['nodes']);
self::assertStringStartsWith('s0 section "Hero"', $result['outline']);
self::assertFalse($result['truncated']);
self::assertSame([], $result['hints']);
self::assertTrue($ability->isDirect());
self::assertTrue($ability->annotations()['readonly']);
}
/** Ruling S18: the "limit" input caps children per parent; "truncated" reports the cut. */
public function testOutlineBreadthLimitTruncatesAndReportsIt(): void
{
DiviFixtures::givenPost(DiviFixtures::markup('synthetic-basic.html'));
$ability = new DiviOutlineAbility(new DiviPageLoader(DiviFixtures::schema()), $this->design());
$result = $ability->execute(['post_id' => 7, 'id' => 's0.r0.c1', 'limit' => 2]);
self::assertSame(['s0.r0.c1', 's0.r0.c1.m0', 's0.r0.c1.m1'], array_column($result['nodes'], 'id'));
self::assertTrue($result['nodes'][0]['truncated']);
self::assertTrue($result['truncated']);
}
public function testOutlineOfASubtreeAndUnknownNode(): void
{
DiviFixtures::givenPost(DiviFixtures::markup('synthetic-basic.html'));
$ability = new DiviOutlineAbility(new DiviPageLoader(DiviFixtures::schema()), $this->design());
$result = $ability->execute(['post_id' => 7, 'id' => 's1', 'depth' => 1]);
self::assertSame(['s1', 's1.r0'], array_column($result['nodes'], 'id'));
$missing = $ability->execute(['post_id' => 7, 'id' => 's7']);
self::assertToolError($missing, 'not_found');
self::assertStringContainsString('tree_rev', $missing->get_error_message());
}
public function testOutlineValidatesTheSubtreeId(): void
{
DiviFixtures::givenPost(DiviFixtures::markup('synthetic-basic.html'));
$ability = new DiviOutlineAbility(new DiviPageLoader(DiviFixtures::schema()), $this->design());
self::assertSame(
'lit_validation_failed',
$ability->execute(['post_id' => 7, 'id' => 'hero'])->get_error_code()
);
}
public function testOutlineHintsForEmptyAndReadOnlyContent(): void
{
DiviFixtures::givenPost('');
$ability = new DiviOutlineAbility(new DiviPageLoader(DiviFixtures::schema()), $this->design());
self::assertStringContainsString('lit-divi-insert', $ability->execute(['post_id' => 7])['hints'][0]);
DiviFixtures::givenPost(DiviFixtures::markup('synthetic-mixed.html'));
$hints = $ability->execute(['post_id' => 7])['hints'];
self::assertStringContainsString('nur lesbar', $hints[0]);
self::assertStringContainsString('nur als Ganzes', $hints[0]);
}
public function testNodeGetReturnsFullFormShortformAndPresets(): void
{
DiviFixtures::givenPost(DiviFixtures::markup('synthetic-basic.html'));
$ability = new DiviNodeGetAbility(new DiviPageLoader(DiviFixtures::schema()), $this->design());
$heading = $ability->execute(['post_id' => 7, 'id' => 's0.r0.c1.m0']);
self::assertSame('heading', $heading['type']);
self::assertSame('s0.r0.c1', $heading['parent']);
self::assertSame(12, strlen($heading['rev']));
self::assertSame(
['desktop' => ['value' => ['headingLevel' => 'h1', 'color' => '#1a1a1a']], 'phone' => ['value' => ['size' => '28px']]],
$heading['attrs']['title.decoration.font.font']
);
self::assertSame(['text' => 'Willkommen bei Müller & Söhne', 'level' => 'h1'], $heading['shortform']);
self::assertSame([['id' => 'a1b2c3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d', 'name' => '']], $heading['presets']);
self::assertSame('5.2.1', $heading['builder_version']);
$accordion = $ability->execute(['post_id' => 7, 'id' => 's1.r0.c1.m0']);
self::assertSame(['s1.r0.c1.m0.m0', 's1.r0.c1.m0.m1'], $accordion['children']);
self::assertInstanceOf(stdClass::class, $accordion['shortform']);
}
public function testNodeGetValidatesTheId(): void
{
DiviFixtures::givenPost(DiviFixtures::markup('synthetic-basic.html'));
$ability = new DiviNodeGetAbility(new DiviPageLoader(DiviFixtures::schema()), $this->design());
self::assertToolError($ability->execute(['post_id' => 7, 'id' => 'hero']), 'validation_failed');
self::assertToolError($ability->execute(['post_id' => 7, 'id' => 's5.r0']), 'not_found');
}
}