fix: trashed backup before replace, byte-exact meta copy, partial rollback

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:34:13 +02:00
co-authored by Claude Opus 5.5
parent 4fed69cf65
commit e183874ff7
7 changed files with 490 additions and 152 deletions
@@ -11,6 +11,7 @@ use LIT\MCP\ChangeLog\ChangeLog;
use LIT\MCP\Integrations\WordPress\Schema\ContentSchema;
use LIT\MCP\Integrations\WordPress\Status\PostCloner;
use LIT\MCP\Integrations\WordPress\Status\PostReplaceWithCopyAbility;
use LIT\MCP\Integrations\WordPress\Status\ReplaceFailed;
use LIT\MCP\Tests\Unit\TestCase;
use Mockery;
use RuntimeException;
@@ -80,10 +81,23 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
self::assertSame('lit_validation_failed', $result->get_error_code());
}
public function testATrashedCopyIsRefused(): void
/** @return list<array{0: string, 1: string}> */
public function refusedStatuses(): array
{
$this->copy->post_status = 'trash';
return [
'trashed copy' => ['copy', 'trash'],
'published copy' => ['copy', 'publish'],
'scheduled copy' => ['copy', 'future'],
'trashed original' => ['original', 'trash'],
];
}
/** @dataProvider refusedStatuses */
public function testRefusesCopiesThatAreNoDraftAndTrashedOriginals(string $which, string $status): void
{
$this->{$which}->post_status = $status;
$cloner = Mockery::mock(PostCloner::class);
$cloner->expects('backup')->never();
$cloner->expects('replace')->never();
$result = $this->ability($cloner, Mockery::mock(ChangeLog::class))->execute($this->input());
@@ -91,6 +105,21 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
self::assertSame('lit_validation_failed', $result->get_error_code());
}
public function testAPendingCopyIsAccepted(): void
{
$this->copy->post_status = 'pending';
$this->revisionAt900();
$cloner = Mockery::mock(PostCloner::class);
$cloner->allows('snapshot')->andReturn(self::SNAPSHOT);
$cloner->allows('backup')->andReturn(88);
$cloner->expects('replace');
Functions\when('wp_trash_post')->justReturn(new WP_Post(['ID' => 77]));
$log = Mockery::mock(ChangeLog::class);
$log->allows('record')->andReturn(11);
self::assertTrue($this->ability($cloner, $log)->execute($this->input())['ok']);
}
public function testAStaleRevOfTheOriginalIsRefused(): void
{
$cloner = Mockery::mock(PostCloner::class);
@@ -125,6 +154,10 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
$order[] = 'snapshot';
return self::SNAPSHOT;
});
$cloner->expects('backup')->with($this->original)->andReturnUsing(static function () use (&$order): int {
$order[] = 'backup';
return 88;
});
$cloner->expects('replace')->with($this->original, $this->copy)->andReturnUsing(static function () use (&$order): void {
$order[] = 'replace';
});
@@ -138,7 +171,9 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
$result = $this->ability($cloner, $log)->execute($this->input());
self::assertSame(['snapshot', 'revision', 'replace', 'trash'], $order);
self::assertSame(['snapshot', 'backup', 'revision', 'replace', 'trash'], $order);
self::assertSame(88, $result['backup_id']);
self::assertStringContainsString('Sicherung ID 88 im Papierkorb', $result['summary']);
self::assertSame(900, $result['revision_id']);
self::assertSame(11, $result['log_id']);
self::assertSame(Rev::ofPost($this->original), $result['rev']);
@@ -151,6 +186,7 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
$this->revisionAt900();
$cloner = Mockery::mock(PostCloner::class);
$cloner->allows('snapshot')->andReturn(self::SNAPSHOT);
$cloner->allows('backup')->andReturn(88);
$cloner->expects('replace');
Functions\when('wp_trash_post')->justReturn(false);
$log = Mockery::mock(ChangeLog::class);
@@ -168,8 +204,9 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
$this->revisionAt900();
$cloner = Mockery::mock(PostCloner::class);
$cloner->allows('snapshot')->andReturn(self::SNAPSHOT);
$cloner->expects('replace')->andThrow(new RuntimeException('meta kaputt'));
$cloner->expects('restore')->with($this->original, self::SNAPSHOT);
$cloner->allows('backup')->andReturn(88);
$cloner->expects('replace')->andThrow(new ReplaceFailed(['post'], new RuntimeException('row kaputt')));
$cloner->expects('restore')->with($this->original, self::SNAPSHOT, ['post']);
Functions\expect('wp_trash_post')->never();
try {
@@ -186,8 +223,9 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
$this->revisionAt900();
$cloner = Mockery::mock(PostCloner::class);
$cloner->allows('snapshot')->andReturn(self::SNAPSHOT);
$cloner->expects('replace')->andThrow(new RuntimeException('meta kaputt'));
$cloner->expects('restore')->andThrow(new RuntimeException('Restore incomplete: terms'));
$cloner->allows('backup')->andReturn(88);
$cloner->expects('replace')->andThrow(new RuntimeException('hook kaputt'));
$cloner->expects('restore')->with($this->original, self::SNAPSHOT, PostCloner::PARTS)->andThrow(new RuntimeException('Restore incomplete: terms'));
Functions\expect('wp_trash_post')->never();
$log = Mockery::mock(ChangeLog::class);
$log->expects('record')
@@ -205,11 +243,30 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
}
}
public function testAFailedBackupAbortsBeforeTheOriginalIsTouched(): void
{
$cloner = Mockery::mock(PostCloner::class);
$cloner->allows('snapshot')->andReturn(self::SNAPSHOT);
$cloner->expects('backup')->andThrow(new WriteFailed('Die Sicherung konnte nicht angelegt werden.', true));
$cloner->expects('replace')->never();
$cloner->expects('restore')->never();
Functions\expect('wp_save_post_revision')->never();
Functions\expect('wp_trash_post')->never();
try {
$this->ability($cloner, Mockery::mock(ChangeLog::class))->execute($this->input());
self::fail('WriteFailed expected');
} catch (WriteFailed $e) {
self::assertSame('lit_write_failed', $e->toError()->get_error_code());
}
}
public function testRequiresRevAndIsDestructive(): void
{
$ability = $this->ability(Mockery::mock(PostCloner::class), Mockery::mock(ChangeLog::class));
self::assertContains('rev', $ability->inputSchema()['required']);
self::assertContains('backup_id', $ability->outputSchema()['required']);
self::assertTrue($ability->annotations()['destructive']);
self::assertFalse($ability->annotations()['idempotent']);
self::assertFalse($ability->isDirect());