feat: lit/divi-outline and lit/divi-node-get with tree_rev
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01At3V6SjwbMe6eMzVGW3Hs7
This commit is contained in:
@@ -14,6 +14,8 @@ final class DiviIntegrationTest extends TestCase
|
||||
{
|
||||
/** Grows with every task that adds Divi abilities – keep in the order of abilities(). */
|
||||
private const EXPECTED = [
|
||||
'lit/divi-outline',
|
||||
'lit/divi-node-get',
|
||||
'lit/divi-catalog',
|
||||
'lit/divi-design-system',
|
||||
];
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace LIT\MCP\Tests\Unit\Integrations\Divi\Pages;
|
||||
|
||||
use Brain\Monkey\Functions;
|
||||
use LIT\MCP\Integrations\Divi\Pages\DiviPage;
|
||||
use LIT\MCP\Integrations\Divi\Pages\DiviPageLoader;
|
||||
use LIT\MCP\Integrations\WordPress\Schema\ContentSchema;
|
||||
use LIT\MCP\Tests\Unit\TestCase;
|
||||
use Mockery;
|
||||
use WP_Error;
|
||||
use WP_Post;
|
||||
|
||||
final class DiviPageLoaderTest extends TestCase
|
||||
{
|
||||
public static function fixture(string $name): string
|
||||
{
|
||||
return (string) file_get_contents(dirname(__DIR__, 4) . '/Fixtures/divi/' . $name);
|
||||
}
|
||||
|
||||
/** Stubs everything PostGuard::editable() needs for a post with $content. */
|
||||
public static function givenPost(string $content, array $fields = []): WP_Post
|
||||
{
|
||||
$post = new WP_Post(array_merge(
|
||||
['ID' => 7, 'post_type' => 'page', 'post_status' => 'draft', 'post_title' => 'Start', 'post_content' => $content],
|
||||
$fields
|
||||
));
|
||||
Functions\when('get_post')->justReturn($post);
|
||||
Functions\when('current_user_can')->justReturn(true);
|
||||
Functions\when('get_permalink')->justReturn('https://kunde.test/?page_id=7');
|
||||
Functions\when('add_query_arg')->alias(static fn (string $key, string $value, string $url): string => $url . '&' . $key . '=' . $value);
|
||||
|
||||
return $post;
|
||||
}
|
||||
|
||||
public static function schema(bool $diviEnabled = true): ContentSchema
|
||||
{
|
||||
$schema = Mockery::mock(ContentSchema::class);
|
||||
$schema->allows('isManaged')->andReturn(true);
|
||||
$schema->allows('diviEnabled')->andReturn($diviEnabled);
|
||||
|
||||
return $schema;
|
||||
}
|
||||
|
||||
public function testLoadsTreeIndexAndTreeRev(): void
|
||||
{
|
||||
$content = self::fixture('synthetic-basic.html');
|
||||
self::givenPost($content);
|
||||
|
||||
$page = (new DiviPageLoader(self::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(self::schema()))->writable($page));
|
||||
}
|
||||
|
||||
public function testRefusesDivi4Content(): void
|
||||
{
|
||||
self::givenPost(self::fixture('divi4-shortcodes.html'));
|
||||
|
||||
$result = (new DiviPageLoader(self::schema()))->load(7, false);
|
||||
|
||||
self::assertInstanceOf(WP_Error::class, $result);
|
||||
self::assertSame('lit_unsupported', $result->get_error_code());
|
||||
self::assertStringContainsString('Divi-4', $result->get_error_message());
|
||||
}
|
||||
|
||||
public function testRefusesMalformedMarkupAndLogsIt(): void
|
||||
{
|
||||
self::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(self::schema()))->load(7, false);
|
||||
} finally {
|
||||
ini_set('error_log', (string) $previous);
|
||||
}
|
||||
|
||||
self::assertSame('lit_unsupported', $result->get_error_code());
|
||||
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
|
||||
{
|
||||
self::givenPost(self::fixture('synthetic-basic.html'), ['post_status' => 'trash']);
|
||||
$loader = new DiviPageLoader(self::schema());
|
||||
|
||||
self::assertInstanceOf(DiviPage::class, $loader->load(7, false));
|
||||
self::assertSame('lit_validation_failed', $loader->load(7, true)->get_error_code());
|
||||
}
|
||||
|
||||
public function testWritableChecks(): void
|
||||
{
|
||||
$loader = new DiviPageLoader(self::schema(false));
|
||||
self::givenPost('');
|
||||
$empty = $loader->load(7, true);
|
||||
self::assertSame('lit_unsupported', $loader->writable($empty)->get_error_code());
|
||||
|
||||
$loader = new DiviPageLoader(self::schema());
|
||||
self::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());
|
||||
|
||||
self::givenPost('', ['post_status' => 'future', 'post_date_gmt' => '2020-01-01 00:00:00']);
|
||||
$missed = $loader->load(7, true);
|
||||
self::assertTrue($missed->isDiviLayout());
|
||||
self::assertSame('lit_validation_failed', $loader->writable($missed)->get_error_code());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
<?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\Unit\Integrations\Divi\Design\DesignSystemTest;
|
||||
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(DesignSystemTest::sample());
|
||||
|
||||
return $reader;
|
||||
}
|
||||
|
||||
public function testOutlineListsNodesWithTreeRevAndPostRev(): void
|
||||
{
|
||||
$content = DiviPageLoaderTest::fixture('synthetic-basic.html');
|
||||
DiviPageLoaderTest::givenPost($content);
|
||||
$ability = new DiviOutlineAbility(new DiviPageLoader(DiviPageLoaderTest::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::assertSame([], $result['hints']);
|
||||
self::assertTrue($ability->isDirect());
|
||||
self::assertTrue($ability->annotations()['readonly']);
|
||||
}
|
||||
|
||||
public function testOutlineOfASubtreeAndUnknownNode(): void
|
||||
{
|
||||
DiviPageLoaderTest::givenPost(DiviPageLoaderTest::fixture('synthetic-basic.html'));
|
||||
$ability = new DiviOutlineAbility(new DiviPageLoader(DiviPageLoaderTest::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::assertSame('lit_not_found', $missing->get_error_code());
|
||||
self::assertStringContainsString('tree_rev', $missing->get_error_message());
|
||||
}
|
||||
|
||||
public function testOutlineHintsForEmptyAndReadOnlyContent(): void
|
||||
{
|
||||
DiviPageLoaderTest::givenPost('');
|
||||
$ability = new DiviOutlineAbility(new DiviPageLoader(DiviPageLoaderTest::schema()), $this->design());
|
||||
self::assertSame(['Die Seite hat noch keinen Divi-Inhalt.'], $ability->execute(['post_id' => 7])['hints']);
|
||||
|
||||
DiviPageLoaderTest::givenPost(DiviPageLoaderTest::fixture('synthetic-mixed.html'));
|
||||
$hints = $ability->execute(['post_id' => 7])['hints'];
|
||||
self::assertStringContainsString('nur lesbar', $hints[0]);
|
||||
}
|
||||
|
||||
public function testNodeGetReturnsFullFormShortformAndPresets(): void
|
||||
{
|
||||
DiviPageLoaderTest::givenPost(DiviPageLoaderTest::fixture('synthetic-basic.html'));
|
||||
$ability = new DiviNodeGetAbility(new DiviPageLoader(DiviPageLoaderTest::schema()), $this->design());
|
||||
|
||||
$heading = $ability->execute(['post_id' => 7, 'id' => 's0.r0.c1.m0']);
|
||||
|
||||
self::assertSame('divi/heading', $heading['type']);
|
||||
self::assertSame('s0.r0.c1', $heading['parent']);
|
||||
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
|
||||
{
|
||||
DiviPageLoaderTest::givenPost(DiviPageLoaderTest::fixture('synthetic-basic.html'));
|
||||
$ability = new DiviNodeGetAbility(new DiviPageLoader(DiviPageLoaderTest::schema()), $this->design());
|
||||
|
||||
self::assertSame('lit_validation_failed', $ability->execute(['post_id' => 7, 'id' => 'hero'])->get_error_code());
|
||||
self::assertSame('lit_not_found', $ability->execute(['post_id' => 7, 'id' => 's5.r0'])->get_error_code());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user