Task 14 (rulings P17/P18, Q10): - lit/menu-get returns menu_rev (Rev::ofMenu over id, parent, position, title, url, object, object_id); lit/menu-set-items requires it, refuses a stale one with rev_mismatch (hint: lit/menu-get) and returns the new menu_rev. Errors::revMismatch takes an optional message/hint. - GuideRepository: filter lit_mcp_guides adds/overrides topics; invalid topic names and non-string content are ignored, files stay the default. - lit/post-trash names front page, posts page and linking menus in the confirmation and as warnings; lit/menu-assign-location needs confirm to remove an assigned menu and names it; lit/post-schedule warns that a private entry becomes public; Divi hints no longer point to non-existing lit-divi-* tools. - PostGuard::missedSchedule: 60 s publish window, zero GMT date falls back to get_gmt_from_date(post_date); post-schedule refuses dates within that window. - write_failed after a clean rollback that keeps a revision/backup says "Die Aenderung wurde nicht gespeichert (... bleibt erhalten)" via the new state parameter (Errors::writeFailed / WriteFailed rolledBackState). - wp-basis-smoke: menu_rev flow, unassign confirm, trash/menu and private-schedule warnings. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01At3V6SjwbMe6eMzVGW3Hs7
213 lines
8.4 KiB
PHP
213 lines
8.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace LIT\MCP\Tests\Unit\Integrations\WordPress\Revisions;
|
|
|
|
use Brain\Monkey\Actions;
|
|
use Brain\Monkey\Functions;
|
|
use LIT\MCP\Abilities\Rev;
|
|
use LIT\MCP\ChangeLog\ChangeLog;
|
|
use LIT\MCP\Integrations\WordPress\Revisions\RevisionRestoreAbility;
|
|
use LIT\MCP\Integrations\WordPress\Schema\ContentSchema;
|
|
use LIT\MCP\Tests\Unit\TestCase;
|
|
use Mockery;
|
|
use RuntimeException;
|
|
use WP_Post;
|
|
|
|
final class RevisionRestoreAbilityTest extends TestCase
|
|
{
|
|
private const CURRENT_POST = [
|
|
'ID' => 5,
|
|
'post_type' => 'page',
|
|
'post_title' => 'Team',
|
|
'post_status' => 'publish',
|
|
'post_content' => 'alt',
|
|
];
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
Functions\when('get_post')->alias(static fn (int $id) => match ($id) {
|
|
810 => new WP_Post(['ID' => 810, 'post_type' => 'revision', 'post_parent' => 5, 'post_date' => '2026-09-20 10:00:00']),
|
|
5 => new WP_Post(self::CURRENT_POST),
|
|
default => null,
|
|
});
|
|
Functions\when('current_user_can')->justReturn(true);
|
|
}
|
|
|
|
private function ability(ChangeLog $log): RevisionRestoreAbility
|
|
{
|
|
$schema = Mockery::mock(ContentSchema::class);
|
|
$schema->allows('isManaged')->andReturn(true);
|
|
|
|
return new RevisionRestoreAbility($schema, $log);
|
|
}
|
|
|
|
private function currentRev(): string
|
|
{
|
|
return Rev::ofPost(new WP_Post(self::CURRENT_POST));
|
|
}
|
|
|
|
public function testNonRevisionIdIsNotFound(): void
|
|
{
|
|
self::assertSame('lit_not_found', $this->ability(Mockery::mock(ChangeLog::class))->permission(['revision_id' => 5])->get_error_code());
|
|
}
|
|
|
|
public function testRequiresConfirmation(): void
|
|
{
|
|
Functions\expect('wp_restore_post_revision')->never();
|
|
|
|
$result = $this->ability(Mockery::mock(ChangeLog::class))->execute(['revision_id' => 810]);
|
|
|
|
self::assertSame('lit_confirm_required', $result->get_error_code());
|
|
}
|
|
|
|
public function testOrphanRevisionYieldsValidationErrorOnRevisionIdField(): void
|
|
{
|
|
Functions\when('get_post')->alias(static fn (int $id) => $id === 810
|
|
? new WP_Post(['ID' => 810, 'post_type' => 'revision', 'post_parent' => 0])
|
|
: null);
|
|
|
|
$error = $this->ability(Mockery::mock(ChangeLog::class))
|
|
->permission(['revision_id' => 810, 'rev' => 'irrelevant', 'confirm' => true]);
|
|
|
|
self::assertSame('lit_validation_failed', $error->get_error_code());
|
|
self::assertArrayHasKey('revision_id', $error->get_error_data()['fields']);
|
|
}
|
|
|
|
public function testATrashedEntryIsRefused(): void
|
|
{
|
|
Functions\when('get_post')->alias(static fn (int $id) => match ($id) {
|
|
810 => new WP_Post(['ID' => 810, 'post_type' => 'revision', 'post_parent' => 5]),
|
|
5 => new WP_Post(array_merge(self::CURRENT_POST, ['post_status' => 'trash'])),
|
|
default => null,
|
|
});
|
|
Functions\expect('wp_restore_post_revision')->never();
|
|
|
|
$error = $this->ability(Mockery::mock(ChangeLog::class))->execute(['revision_id' => 810, 'rev' => 'x', 'confirm' => true]);
|
|
|
|
self::assertSame('lit_validation_failed', $error->get_error_code());
|
|
self::assertArrayHasKey('revision_id', $error->get_error_data()['fields']);
|
|
}
|
|
|
|
public function testAMissedScheduleIsRefusedBecauseRestoringWouldPublish(): void
|
|
{
|
|
$current = array_merge(self::CURRENT_POST, [
|
|
'post_status' => 'future',
|
|
'post_date_gmt' => gmdate('Y-m-d H:i:s', time() - 600),
|
|
]);
|
|
Functions\when('get_post')->alias(static fn (int $id) => match ($id) {
|
|
810 => new WP_Post(['ID' => 810, 'post_type' => 'revision', 'post_parent' => 5]),
|
|
5 => new WP_Post($current),
|
|
default => null,
|
|
});
|
|
Functions\expect('wp_save_post_revision')->never();
|
|
Functions\expect('wp_restore_post_revision')->never();
|
|
|
|
$error = $this->ability(Mockery::mock(ChangeLog::class))->execute([
|
|
'revision_id' => 810,
|
|
'rev' => Rev::ofPost(new WP_Post($current)),
|
|
'confirm' => true,
|
|
]);
|
|
|
|
self::assertSame('lit_validation_failed', $error->get_error_code());
|
|
self::assertStringContainsString('lit-post-publish', $error->get_error_data()['hint']);
|
|
}
|
|
|
|
public function testMissingOrStaleRevIsRevMismatch(): void
|
|
{
|
|
Functions\expect('wp_restore_post_revision')->never();
|
|
|
|
$result = $this->ability(Mockery::mock(ChangeLog::class))
|
|
->execute(['revision_id' => 810, 'confirm' => true, 'rev' => 'stale']);
|
|
|
|
self::assertSame('lit_rev_mismatch', $result->get_error_code());
|
|
}
|
|
|
|
public function testSavesTheCurrentStateThenRestores(): void
|
|
{
|
|
$order = [];
|
|
Functions\expect('wp_save_post_revision')->once()->with(5)->andReturnUsing(static function () use (&$order) {
|
|
$order[] = 'backup';
|
|
return 813;
|
|
});
|
|
Functions\when('wp_get_post_revisions')->justReturn([813 => new WP_Post(['ID' => 813])]);
|
|
Functions\expect('wp_restore_post_revision')->once()->with(810)->andReturnUsing(static function () use (&$order): int {
|
|
$order[] = 'restore';
|
|
return 5;
|
|
});
|
|
Functions\when('get_preview_post_link')->justReturn('https://kunde.de/team/');
|
|
Functions\when('get_edit_post_link')->justReturn('https://kunde.de/wp-admin/post.php?post=5&action=edit');
|
|
$log = Mockery::mock(ChangeLog::class);
|
|
$log->expects('record')->with('lit/revision-restore', 'page', '5', Mockery::type('string'), 813)->andReturn(12);
|
|
|
|
$result = $this->ability($log)->execute([
|
|
'revision_id' => 810,
|
|
'confirm' => true,
|
|
'rev' => $this->currentRev(),
|
|
]);
|
|
|
|
self::assertSame(['backup', 'restore'], $order);
|
|
self::assertSame(813, $result['revision_id']);
|
|
self::assertSame('restored', $result['changed'][0]['action']);
|
|
self::assertStringContainsString('2026-09-20 10:00:00', $result['summary']);
|
|
self::assertStringContainsString('Status unverändert: publish', $result['summary']);
|
|
self::assertSame([], $result['warnings']);
|
|
}
|
|
|
|
public function testRestoreFailureIsWriteFailed(): void
|
|
{
|
|
Functions\when('wp_save_post_revision')->justReturn(813);
|
|
Functions\when('wp_get_post_revisions')->justReturn([813 => new WP_Post(['ID' => 813])]);
|
|
Functions\when('wp_restore_post_revision')->justReturn(false);
|
|
$log = Mockery::mock(ChangeLog::class);
|
|
$log->expects('record')->never();
|
|
|
|
$result = $this->ability($log)->execute([
|
|
'revision_id' => 810,
|
|
'confirm' => true,
|
|
'rev' => $this->currentRev(),
|
|
]);
|
|
|
|
self::assertSame('lit_write_failed', $result->get_error_code());
|
|
self::assertStringNotContainsString('Nichts wurde gespeichert', $result->get_error_message());
|
|
self::assertStringContainsString(
|
|
'Die Änderung wurde nicht gespeichert (Revision 813 bleibt erhalten).',
|
|
$result->get_error_message()
|
|
);
|
|
}
|
|
|
|
public function testFailuresAfterTheWriteOnlyAddWarnings(): void
|
|
{
|
|
Functions\when('wp_save_post_revision')->justReturn(813);
|
|
Functions\when('wp_get_post_revisions')->justReturn([813 => new WP_Post(['ID' => 813])]);
|
|
Functions\when('wp_restore_post_revision')->justReturn(5);
|
|
Functions\when('get_preview_post_link')->justReturn('');
|
|
Functions\when('get_edit_post_link')->justReturn('');
|
|
Actions\expectDone('lit_mcp_after_save')->once()->whenHappen(static function (): void {
|
|
throw new RuntimeException('listener kaputt');
|
|
});
|
|
$log = Mockery::mock(ChangeLog::class);
|
|
$log->expects('record')->andThrow(new RuntimeException('db weg'));
|
|
$file = (string) tempnam(sys_get_temp_dir(), 'lit-mcp-log');
|
|
$previous = ini_set('error_log', $file);
|
|
|
|
try {
|
|
$result = $this->ability($log)->execute([
|
|
'revision_id' => 810,
|
|
'confirm' => true,
|
|
'rev' => $this->currentRev(),
|
|
]);
|
|
} finally {
|
|
ini_set('error_log', (string) $previous);
|
|
}
|
|
|
|
self::assertTrue($result['ok']);
|
|
self::assertNull($result['log_id']);
|
|
self::assertCount(2, $result['warnings']);
|
|
self::assertStringContainsString('listener kaputt', (string) file_get_contents($file));
|
|
unlink($file);
|
|
}
|
|
}
|