Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01At3V6SjwbMe6eMzVGW3Hs7
65 lines
2.0 KiB
PHP
65 lines
2.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace LIT\MCP\Tests\Unit\Integrations\WordPress\Render;
|
|
|
|
use Brain\Monkey\Filters;
|
|
use Brain\Monkey\Functions;
|
|
use LIT\MCP\Integrations\WordPress\Render\PageRenderer;
|
|
use LIT\MCP\Tests\Unit\TestCase;
|
|
use RuntimeException;
|
|
use WP_Post;
|
|
|
|
final class PageRendererTest extends TestCase
|
|
{
|
|
protected function tearDown(): void
|
|
{
|
|
unset($GLOBALS['post']);
|
|
parent::tearDown();
|
|
}
|
|
|
|
public function testRendersTheContentFilterAndRestoresTheGlobalPost(): void
|
|
{
|
|
$previous = new WP_Post(['ID' => 1]);
|
|
$GLOBALS['post'] = $previous;
|
|
$post = new WP_Post(['ID' => 7, 'post_content' => '<!-- wp:divi/section /-->']);
|
|
$setup = [];
|
|
Functions\when('setup_postdata')->alias(static function (WP_Post $p) use (&$setup): bool {
|
|
$setup[] = $p->ID;
|
|
|
|
return true;
|
|
});
|
|
Filters\expectApplied('the_content')->once()->with('<!-- wp:divi/section /-->')->andReturnUsing(
|
|
static function () use ($post): string {
|
|
echo '<p>echoed</p>';
|
|
|
|
return $GLOBALS['post'] === $post ? '<div class="et_pb_section"></div>' : 'wrong post';
|
|
}
|
|
);
|
|
|
|
$html = (new PageRenderer())->render($post);
|
|
|
|
self::assertSame('<p>echoed</p><div class="et_pb_section"></div>', $html);
|
|
self::assertSame($previous, $GLOBALS['post']);
|
|
self::assertSame([7, 1], $setup);
|
|
}
|
|
|
|
public function testRestoresEvenWhenRenderingThrows(): void
|
|
{
|
|
Functions\when('setup_postdata')->justReturn(true);
|
|
Functions\expect('wp_reset_postdata')->once();
|
|
Filters\expectApplied('the_content')->once()->andReturnUsing(static function (): string {
|
|
throw new RuntimeException('module crashed');
|
|
});
|
|
|
|
try {
|
|
(new PageRenderer())->render(new WP_Post(['ID' => 7]));
|
|
self::fail('exception expected');
|
|
} catch (RuntimeException) {
|
|
self::assertArrayHasKey('post', $GLOBALS);
|
|
self::assertNull($GLOBALS['post']);
|
|
}
|
|
}
|
|
}
|