Files
WP-LIT-MCP/tests/Unit/Integrations/Divi/Pages/DiviPageLoaderTest.php
T

150 lines
6.3 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\Integrations\Divi\BlockTree\Parser;
use LIT\MCP\Integrations\Divi\Library\LibraryAccess;
use LIT\MCP\Integrations\WordPress\Schema\ContentSchema;
use LIT\MCP\Tests\Support\LibraryFixtures;
use Mockery;
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');
}
public function testLoadWritableCombinesLoadAndWritable(): void
{
$loader = new DiviPageLoader(DiviFixtures::schema());
DiviFixtures::givenPost(DiviFixtures::markup('synthetic-basic.html'));
self::assertInstanceOf(DiviPage::class, $loader->loadWritable(7));
DiviFixtures::givenPost(DiviFixtures::markup('synthetic-basic.html'), ['post_status' => 'trash']);
self::assertToolError($loader->loadWritable(7), 'validation_failed');
DiviFixtures::givenPost("<!-- wp:paragraph -->\n<p>Gutenberg</p>\n<!-- /wp:paragraph -->");
self::assertToolError($loader->loadWritable(7), 'unsupported');
}
public function testLoadsALibraryItemForReading(): void
{
LibraryFixtures::given([LibraryFixtures::item(1058)]);
$page = LibraryFixtures::loader()->load(1058, false);
self::assertInstanceOf(DiviPage::class, $page);
self::assertTrue($page->isLibraryItem());
self::assertSame('divi/section', $page->index->find('s0')?->name());
self::assertSame('https://kunde.test/wp-admin/post.php?post=1058&action=edit', $page->editUrl());
}
public function testLibraryItemsNeedTheLibraryAccess(): void
{
LibraryFixtures::given([LibraryFixtures::item(1058)]);
$schema = Mockery::mock(ContentSchema::class);
$schema->allows('isManaged')->with('et_pb_layout')->andReturn(false);
self::assertToolError((new DiviPageLoader($schema))->load(1058, false), 'not_found');
self::assertToolError(
(new DiviPageLoader($schema, new Parser(), LibraryFixtures::access(['divi_library'])))->load(1058, false),
'not_found'
);
}
public function testLibraryItemsAreNotWritableYet(): void
{
LibraryFixtures::given([LibraryFixtures::item(1058)]);
$schema = Mockery::mock(ContentSchema::class);
$schema->allows('isManaged')->with('et_pb_layout')->andReturn(false);
$result = (new DiviPageLoader($schema, new Parser(), LibraryFixtures::access()))->load(1058, true);
self::assertToolError($result, 'not_found');
}
/** Ruling P9: the group "Divi-Bibliothek" disabled hides library entries from the Divi read tools. */
public function testAHiddenLibraryLeavesLibraryItemsNotFound(): void
{
LibraryFixtures::given([LibraryFixtures::item(1058)]);
$hidden = new LibraryAccess(LibraryFixtures::store(), LibraryFixtures::roles(), null, static fn (): bool => false);
self::assertToolError(LibraryFixtures::loader($hidden)->load(1058, false), 'not_found');
}
}