Files
WP-LIT-MCP/tests/Unit/Integrations/Divi/Pages/DiviPageLoaderTest.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

87 lines
3.5 KiB
PHP

<?php
declare(strict_types=1);
namespace LIT\MCP\Tests\Unit\Integrations\Divi\Pages;
use LIT\MCP\Integrations\Divi\Pages\DiviPage;
use LIT\MCP\Integrations\Divi\Pages\DiviPageLoader;
use LIT\MCP\Tests\Support\DiviFixtures;
use LIT\MCP\Tests\Unit\TestCase;
use WP_Error;
final class DiviPageLoaderTest extends TestCase
{
public function testLoadsTreeIndexAndTreeRev(): void
{
$content = DiviFixtures::markup('synthetic-basic.html');
DiviFixtures::givenPost($content);
$page = (new DiviPageLoader(DiviFixtures::schema()))->load(7, true);
self::assertInstanceOf(DiviPage::class, $page);
self::assertSame(substr(hash('sha256', $content), 0, 12), $page->treeRev());
self::assertSame('divi/button', $page->index->find('s0.r0.c1.m2')?->name());
self::assertTrue($page->isDiviLayout());
self::assertSame('https://kunde.test/?page_id=7&et_fb=1', $page->editUrl());
self::assertNull((new DiviPageLoader(DiviFixtures::schema()))->writable($page));
}
public function testRefusesDivi4Content(): void
{
DiviFixtures::givenPost(DiviFixtures::markup('divi4-shortcodes.html'));
$result = (new DiviPageLoader(DiviFixtures::schema()))->load(7, false);
self::assertInstanceOf(WP_Error::class, $result);
self::assertToolError($result, 'unsupported');
self::assertStringContainsString('Divi-4', $result->get_error_message());
}
public function testRefusesMalformedMarkupAndLogsIt(): void
{
DiviFixtures::givenPost('<!-- wp:divi/section --><!-- wp:divi/row -->');
// error_log() is a PHP internal and cannot be stubbed; redirect it to a temp file instead.
$log = (string) tempnam(sys_get_temp_dir(), 'lit-log');
$previous = ini_set('error_log', $log);
try {
$result = (new DiviPageLoader(DiviFixtures::schema()))->load(7, false);
} finally {
ini_set('error_log', (string) $previous);
}
self::assertToolError($result, 'unsupported');
self::assertStringContainsString('kein gültiges Block-Markup', $result->get_error_message());
self::assertStringContainsString('divi parse of post 7 failed', (string) file_get_contents($log));
unlink($log);
}
public function testTrashedEntriesCanBeReadButNotWritten(): void
{
DiviFixtures::givenPost(DiviFixtures::markup('synthetic-basic.html'), ['post_status' => 'trash']);
$loader = new DiviPageLoader(DiviFixtures::schema());
self::assertInstanceOf(DiviPage::class, $loader->load(7, false));
self::assertToolError($loader->load(7, true), 'validation_failed');
}
public function testWritableChecks(): void
{
$loader = new DiviPageLoader(DiviFixtures::schema(false));
DiviFixtures::givenPost('');
$empty = $loader->load(7, true);
self::assertToolError($loader->writable($empty), 'unsupported');
$loader = new DiviPageLoader(DiviFixtures::schema());
DiviFixtures::givenPost("<!-- wp:paragraph -->\n<p>Gutenberg</p>\n<!-- /wp:paragraph -->");
$gutenberg = $loader->load(7, true);
self::assertFalse($gutenberg->isDiviLayout());
self::assertStringContainsString('kein Divi-Layout', $loader->writable($gutenberg)->get_error_message());
DiviFixtures::givenPost('', ['post_status' => 'future', 'post_date_gmt' => '2020-01-01 00:00:00']);
$missed = $loader->load(7, true);
self::assertTrue($missed->isDiviLayout());
self::assertToolError($loader->writable($missed), 'validation_failed');
}
}