feat: list and restore revisions
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:
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace LIT\MCP\Tests\Unit\Integrations\WordPress\Revisions;
|
||||
|
||||
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 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 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']);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace LIT\MCP\Tests\Unit\Integrations\WordPress\Revisions;
|
||||
|
||||
use Brain\Monkey\Functions;
|
||||
use LIT\MCP\Abilities\Cursor;
|
||||
use LIT\MCP\Integrations\WordPress\Revisions\RevisionsListAbility;
|
||||
use LIT\MCP\Integrations\WordPress\Schema\ContentSchema;
|
||||
use LIT\MCP\Tests\Unit\TestCase;
|
||||
use Mockery;
|
||||
use WP_Post;
|
||||
|
||||
final class RevisionsListAbilityTest extends TestCase
|
||||
{
|
||||
public function testListsRevisionsNewestFirstWithCursor(): void
|
||||
{
|
||||
Functions\when('get_post')->justReturn(new WP_Post(['ID' => 5, 'post_type' => 'page']));
|
||||
Functions\when('current_user_can')->justReturn(true);
|
||||
Functions\when('get_the_author_meta')->justReturn('Jan');
|
||||
Functions\when('wp_is_post_autosave')->alias(static fn ($post): bool => $post->ID === 811);
|
||||
Functions\when('wp_strip_all_tags')->alias(static fn (string $text): string => strip_tags($text));
|
||||
Functions\expect('wp_get_post_revisions')->once()->with(5, ['posts_per_page' => 3, 'offset' => 0])->andReturn([
|
||||
812 => new WP_Post(['ID' => 812, 'post_title' => 'Team', 'post_content' => '<p>Neu</p>', 'post_author' => '2']),
|
||||
811 => new WP_Post(['ID' => 811, 'post_title' => 'Team', 'post_content' => '<p>Auto</p>', 'post_author' => '2']),
|
||||
810 => new WP_Post(['ID' => 810, 'post_title' => 'Team', 'post_content' => '<p>Alt</p>', 'post_author' => '2']),
|
||||
]);
|
||||
$schema = Mockery::mock(ContentSchema::class);
|
||||
$schema->allows('isManaged')->andReturn(true);
|
||||
|
||||
$result = (new RevisionsListAbility($schema))->execute(['post_id' => 5, 'limit' => 2]);
|
||||
|
||||
self::assertSame(5, $result['post_id']);
|
||||
self::assertSame([812, 811], array_column($result['items'], 'id'));
|
||||
self::assertSame('Neu', $result['items'][0]['excerpt']);
|
||||
self::assertTrue($result['items'][1]['autosave']);
|
||||
self::assertSame(['id' => 2, 'name' => 'Jan'], $result['items'][0]['author']);
|
||||
self::assertSame(2, Cursor::decode($result['next_cursor']));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user