fix: guard revision-restore follow-ups, mark divi revisions

RevisionRestoreAbility now wraps the after-save action and change-log
write in FollowUp::run() and reports failures as warnings instead of
letting them turn a saved restore into an error response, matching
ContentUpdateAbility; a failed wp_restore_post_revision() is now
Errors::writeFailed() (nothing was changed) instead of unsupported().
RevisionsListAbility marks each item divi (reusing
PostPresenter::usesDivi()/DIVI_HINT) since a Divi/block revision's
excerpt is stripped markup, not prose.

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:
2026-09-25 23:53:36 +02:00
co-authored by Claude Opus 5.5
parent b9dd86d223
commit 236d25325d
4 changed files with 131 additions and 17 deletions
@@ -4,6 +4,7 @@ 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;
@@ -11,6 +12,7 @@ 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
@@ -111,5 +113,56 @@ final class RevisionRestoreAbilityTest extends TestCase
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());
}
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);
}
}
@@ -6,6 +6,7 @@ namespace LIT\MCP\Tests\Unit\Integrations\WordPress\Revisions;
use Brain\Monkey\Functions;
use LIT\MCP\Abilities\Cursor;
use LIT\MCP\Integrations\WordPress\Content\PostPresenter;
use LIT\MCP\Integrations\WordPress\Revisions\RevisionsListAbility;
use LIT\MCP\Integrations\WordPress\Schema\ContentSchema;
use LIT\MCP\Tests\Unit\TestCase;
@@ -14,28 +15,64 @@ use WP_Post;
final class RevisionsListAbilityTest extends TestCase
{
public function testListsRevisionsNewestFirstWithCursor(): void
protected function setUp(): void
{
parent::setUp();
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\when('get_post_meta')->justReturn('');
}
private function ability(): RevisionsListAbility
{
$schema = Mockery::mock(ContentSchema::class);
$schema->allows('isManaged')->andReturn(true);
return new RevisionsListAbility($schema);
}
public function testListsRevisionsNewestFirstWithCursor(): void
{
Functions\when('wp_is_post_autosave')->alias(static fn ($post): bool => $post->ID === 811);
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]);
$result = $this->ability()->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::assertFalse($result['items'][0]['divi']);
self::assertSame([], $result['hints']);
self::assertSame(2, Cursor::decode($result['next_cursor']));
}
public function testMarksDiviRevisionsAndAddsTheHintOnce(): void
{
Functions\when('wp_is_post_autosave')->justReturn(false);
Functions\when('wp_get_post_revisions')->justReturn([
812 => new WP_Post(['ID' => 812, 'post_content' => '<!-- wp:divi/placeholder --><!-- /wp:divi/placeholder -->']),
811 => new WP_Post(['ID' => 811, 'post_content' => '<p>Normal</p>']),
]);
$result = $this->ability()->execute(['post_id' => 5]);
self::assertTrue($result['items'][0]['divi']);
self::assertFalse($result['items'][1]['divi']);
self::assertSame([PostPresenter::DIVI_HINT], $result['hints']);
}
public function testInvalidCursorIsRejected(): void
{
$result = $this->ability()->execute(['post_id' => 5, 'cursor' => 'not-a-cursor']);
self::assertSame('lit_validation_failed', $result->get_error_code());
}
}