Files
WP-LIT-MCP/tests/Unit/Integrations/Divi/Release/WriteProbeTest.php
T
Jan-Niclas LoosenandClaude Opus 5.5 7d6a97a54e refactor(compat): Freigabe-Bausteine generisch
ReleaseStore, CheckReportStore, CheckReport, CheckStep und CompatibilityCheck liegen im Kern und arbeiten je Profil (Optionen lit_mcp_<id>_releases|_check_report; Divi behält Namen und Schlüssel divi_version). Divi hängt Katalog-, Format- und Schreibtest über Profile::extraSteps an. Ein roter Prüfbericht für den aktuellen Stand sperrt LockOnFailure-Profile bis zur nächsten grünen Prüfung; Sperrgründe nennen nur Schritt-Labels, Ausnahmen gehen ins Error-Log. Profile bekommt testedVersions, stateKey, extraSteps, manifestCheck und reportVersionKey und wird mit benannten Argumenten gebaut.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 12:51:03 +01:00

184 lines
6.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
declare(strict_types=1);
namespace LIT\MCP\Tests\Unit\Integrations\Divi\Release;
use Brain\Monkey\Functions;
use LIT\MCP\Compatibility\ReleaseState;
use LIT\MCP\Integrations\Divi\Release\DiviFeature;
use LIT\MCP\Integrations\Divi\Release\ReleaseGate;
use LIT\MCP\Integrations\Divi\Release\ReleaseStatus;
use LIT\MCP\Integrations\Divi\Release\WriteProbe;
use LIT\MCP\Integrations\WordPress\Content\ContentComposer;
use LIT\MCP\Integrations\WordPress\Render\PageRenderer;
use LIT\MCP\Tests\Support\ReleaseMatrices;
use LIT\MCP\Tests\Unit\TestCase;
use Mockery;
use RuntimeException;
use WP_Post;
final class WriteProbeTest extends TestCase
{
/** @var list<int> */
private array $deleted = [];
protected function setUp(): void
{
parent::setUp();
// The probe must work while Divi is untested – that is when it runs.
ReleaseGate::useResolver(static fn (): ReleaseStatus => ReleaseMatrices::uniform(ReleaseState::Untested));
$this->deleted = [];
Functions\when('wp_insert_post')->justReturn(41);
Functions\when('get_post')->justReturn(
new WP_Post(['ID' => 41, 'post_content' => '<!-- wp:divi/section /-->'])
);
Functions\when('get_post_meta')->alias(
static fn (int $id, string $key): string => $key === '_et_pb_show_page_creation' ? 'off' : 'on'
);
Functions\when('wp_delete_post')->alias(function (int $id, bool $force): WP_Post {
self::assertTrue($force, 'never to the trash');
$this->deleted[] = $id;
return new WP_Post(['ID' => $id]);
});
}
private function composer(?callable $write = null): ContentComposer
{
$composer = Mockery::mock(ContentComposer::class);
$default = static function (int $id, string $type, array $sections): array {
// The composer writes through the saver, which asks the gate: the probe must have opened it for 41.
ReleaseGate::assertFeatures(DiviFeature::cases(), $id);
return ['warnings' => [], 'outline' => ''];
};
$composer->allows('write')->andReturnUsing($write ?? $default);
return $composer;
}
private function renderer(string $html): PageRenderer
{
$renderer = Mockery::mock(PageRenderer::class);
$renderer->allows('render')->andReturn($html);
return $renderer;
}
public function testGreenWhenEveryModuleRendersAndThePageIsGone(): void
{
$probe = new WriteProbe(
$this->composer(),
$this->renderer(implode(' ', WriteProbe::TEXTS)),
'/nonexistent-divi'
);
self::assertSame([], $probe->run());
self::assertSame([41], $this->deleted);
}
public function testMissingTextsAndFlagsAreReported(): void
{
Functions\when('get_post_meta')->justReturn('');
$probe = new WriteProbe($this->composer(), $this->renderer('litcheck-heading'), '/nonexistent-divi');
$problems = $probe->run();
self::assertContains('Builder-Kennzeichen _et_pb_use_builder fehlt.', $problems);
self::assertContains('„litcheck-button“ fehlt in der gerenderten Seite.', $problems);
self::assertNotContains('„litcheck-heading“ fehlt in der gerenderten Seite.', $problems);
self::assertSame([41], $this->deleted);
}
public function testDeletesThePageEvenWhenWritingThrows(): void
{
$composer = $this->composer(static function (): never {
throw new RuntimeException('Composer kaputt');
});
$log = (string) tempnam(sys_get_temp_dir(), 'lit-mcp-log');
$previous = ini_set('error_log', $log);
try {
$problems = (new WriteProbe($composer, $this->renderer(''), '/nonexistent-divi'))->run();
$logged = (string) file_get_contents($log);
} finally {
ini_set('error_log', (string) $previous);
unlink($log);
}
self::assertSame(['Schreibtest abgebrochen (Details im PHP-Error-Log).'], $problems);
self::assertStringContainsString('Composer kaputt', $logged);
self::assertSame([41], $this->deleted);
$this->expectException(\LIT\MCP\Integrations\Divi\Release\DiviLocked::class);
ReleaseGate::assertFeature(DiviFeature::PageWrite, 41);
}
public function testReportsAPageThatCouldNotBeDeleted(): void
{
Functions\when('wp_delete_post')->justReturn(false);
$probe = new WriteProbe(
$this->composer(),
$this->renderer(implode(' ', WriteProbe::TEXTS)),
'/nonexistent-divi'
);
$problems = $probe->run();
self::assertSame(
['Die Testseite 41 konnte nicht gelöscht werden; bitte unter Seiten endgültig löschen.'],
$problems
);
}
public function testCreatesAMarkedDraftPage(): void
{
// wp_insert_post is already stubbed in setUp(); Functions\expect() cannot rewire the same
// function again (Brain Monkey keeps the first redefinition), so the args are captured here.
$captured = null;
Functions\when('wp_insert_post')->alias(static function (array $post, bool $wpError) use (&$captured): int {
$captured = $post;
return 41;
});
$probe = new WriteProbe(
$this->composer(),
$this->renderer(implode(' ', WriteProbe::TEXTS)),
'/nonexistent-divi'
);
$probe->run();
self::assertIsArray($captured);
self::assertSame('page', $captured['post_type']);
self::assertSame('draft', $captured['post_status']);
self::assertTrue(str_starts_with($captured['post_title'], '[LIT MCP Prüfung] '));
self::assertSame(['_lit_mcp_check_page' => '1'], $captured['meta_input']);
}
public function testRemovesOrphanedCheckPages(): void
{
Functions\expect('get_posts')->once()->with(Mockery::on(static fn (array $query): bool =>
$query['meta_key'] === '_lit_mcp_check_page'
&& $query['post_status'] === 'draft'
&& $query['fields'] === 'all'
&& ($query['date_query'][0]['before'] ?? null) === '10 minutes ago'))->andReturn([
new WP_Post(['ID' => 51, 'post_title' => WriteProbe::TITLE . ' 2026-01-01 00:00']),
new WP_Post(['ID' => 52, 'post_title' => WriteProbe::TITLE . ' 2026-01-02 00:00']),
]);
self::assertSame([51, 52], WriteProbe::removeOrphans());
self::assertSame([51, 52], $this->deleted);
}
public function testKeepsAMarkedDraftWhoseTitleDoesNotMatch(): void
{
Functions\when('get_posts')->justReturn([
new WP_Post(['ID' => 60, 'post_title' => 'Ein anderer Entwurf']),
]);
self::assertSame([], WriteProbe::removeOrphans());
self::assertSame([], $this->deleted);
}
}