fix: typed write failures, template rollback, parent and term cleanup
WriteFailed maps to lit_write_failed / lit_partial_write (partial writes are logged with result partial); old template read before the update; row marker set before wp_update_post; created terms deleted on rollback; parent must be a readable non-descendant of a hierarchical type; featured image must be readable; snapshot keeps empty values; meta comparison after sanitize_meta; follow-up failures (listener, change log) only add warnings. 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:
@@ -7,6 +7,7 @@ namespace LIT\MCP\Tests\Unit\Integrations\WordPress\Content;
|
||||
use Brain\Monkey\Actions;
|
||||
use Brain\Monkey\Functions;
|
||||
use LIT\MCP\Abilities\Rev;
|
||||
use LIT\MCP\Abilities\WriteFailed;
|
||||
use LIT\MCP\ChangeLog\ChangeLog;
|
||||
use LIT\MCP\Integrations\WordPress\Content\ContentUpdateAbility;
|
||||
use LIT\MCP\Integrations\WordPress\Content\PostWriter;
|
||||
@@ -142,9 +143,9 @@ final class ContentUpdateAbilityTest extends TestCase
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
* @return array{PostWriter, array<string, mixed>, array<string, mixed>}
|
||||
* @return array{PostWriter&\Mockery\MockInterface, array<string, mixed>}
|
||||
*/
|
||||
private function failingWriter(array $data): array
|
||||
private function failingWriter(array $data, bool $restoreWorks = true): array
|
||||
{
|
||||
$input = ['post_id' => 5, 'rev' => Rev::ofPost($this->post), 'title' => 'Unser Team', 'fields' => ['a' => 1]];
|
||||
$snapshot = ['fields' => ['a' => 0], 'terms' => []];
|
||||
@@ -152,66 +153,159 @@ final class ContentUpdateAbilityTest extends TestCase
|
||||
$writer->allows('postData')->andReturn($data);
|
||||
$writer->allows('prepare')->andReturn(['fields' => ['a' => 1], 'terms' => [], 'warnings' => []]);
|
||||
$writer->expects('snapshot')->with(5, 'page', Mockery::type('array'), $input)->andReturn($snapshot);
|
||||
$writer->expects('writeExtras')->andThrow(new RuntimeException('meta kaputt'));
|
||||
$writer->expects('restore')->with(5, 'page', $snapshot);
|
||||
$writer->allows('writeExtras')->andThrow(new RuntimeException('meta kaputt'));
|
||||
$restore = $writer->expects('restore')->with(5, 'page', $snapshot);
|
||||
if (! $restoreWorks) {
|
||||
$restore->andThrow(new RuntimeException('restore kaputt'));
|
||||
}
|
||||
$writer->expects('deleteCreatedTerms');
|
||||
Functions\when('wp_save_post_revision')->justReturn(811);
|
||||
Functions\when('wp_get_post_revisions')->justReturn([811 => new WP_Post(['ID' => 811])]);
|
||||
Functions\when('get_page_template_slug')->justReturn('');
|
||||
Actions\expectDone('lit_mcp_after_save')->never();
|
||||
|
||||
return [$writer, $input, $snapshot];
|
||||
return [$writer, $input];
|
||||
}
|
||||
|
||||
/** @param array<string, mixed> $input */
|
||||
private function executeExpectingRollback(PostWriter $writer, array $input): void
|
||||
private function executeExpectingFailure(PostWriter $writer, array $input, ?ChangeLog $log = null): WriteFailed
|
||||
{
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->expects('record')->never();
|
||||
if ($log === null) {
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->expects('record')->never();
|
||||
}
|
||||
|
||||
try {
|
||||
$this->ability($writer, $log)->execute($input);
|
||||
self::fail('RuntimeException expected');
|
||||
} catch (RuntimeException $e) {
|
||||
} catch (WriteFailed $e) {
|
||||
self::assertStringContainsString('meta kaputt', $e->getMessage());
|
||||
self::assertStringContainsString('rolled back', $e->getMessage());
|
||||
return $e;
|
||||
}
|
||||
|
||||
self::fail('WriteFailed expected');
|
||||
}
|
||||
|
||||
/** @return list<array<string, mixed>> Captured wp_update_post() calls. */
|
||||
private function captureUpdates(array &$updates): void
|
||||
{
|
||||
Functions\when('wp_update_post')->alias(static function (array $data) use (&$updates): int {
|
||||
$updates[] = $data;
|
||||
return 5;
|
||||
});
|
||||
}
|
||||
|
||||
public function testFailingExtrasRestoreTheRevisionAndTheOldValues(): void
|
||||
{
|
||||
[$writer, $input] = $this->failingWriter(['post_title' => 'Unser Team', 'post_name' => 'unser-team']);
|
||||
$updates = [];
|
||||
Functions\when('wp_update_post')->alias(static function (array $data) use (&$updates): int {
|
||||
$updates[] = $data;
|
||||
return 5;
|
||||
});
|
||||
$this->captureUpdates($updates);
|
||||
Functions\when('wp_revisions_enabled')->justReturn(true);
|
||||
Functions\expect('wp_restore_post_revision')->once()->with(811, ['post_title'])->andReturn(5);
|
||||
|
||||
$this->executeExpectingRollback($writer, $input);
|
||||
$failure = $this->executeExpectingFailure($writer, $input);
|
||||
|
||||
self::assertTrue($failure->rolledBack);
|
||||
self::assertSame('lit_write_failed', $failure->toError()->get_error_code());
|
||||
self::assertSame([
|
||||
['post_title' => 'Unser Team', 'post_name' => 'unser-team', 'ID' => 5],
|
||||
['post_name' => 'team', 'ID' => 5],
|
||||
], $updates);
|
||||
}
|
||||
|
||||
public function testWithoutRevisionsTheOldColumnValuesAreWrittenBack(): void
|
||||
public function testWithoutRevisionsTheOldColumnValuesIncludingTheOldTemplateAreWrittenBack(): void
|
||||
{
|
||||
[$writer, $input] = $this->failingWriter(['post_title' => 'Unser Team', 'page_template' => 'tpl-wide.php']);
|
||||
$template = 'tpl-alt.php';
|
||||
$updates = [];
|
||||
Functions\when('wp_update_post')->alias(static function (array $data) use (&$updates): int {
|
||||
Functions\when('get_page_template_slug')->alias(static function () use (&$template): string {
|
||||
return $template;
|
||||
});
|
||||
Functions\when('wp_update_post')->alias(static function (array $data) use (&$updates, &$template): int {
|
||||
$updates[] = $data;
|
||||
$template = (string) ($data['page_template'] ?? $template);
|
||||
return 5;
|
||||
});
|
||||
Functions\when('wp_revisions_enabled')->justReturn(false);
|
||||
Functions\expect('wp_restore_post_revision')->never();
|
||||
|
||||
$this->executeExpectingRollback($writer, $input);
|
||||
$this->executeExpectingFailure($writer, $input);
|
||||
|
||||
self::assertSame([
|
||||
['post_title' => 'Unser Team', 'page_template' => 'tpl-wide.php', 'ID' => 5],
|
||||
['post_title' => 'Team', 'page_template' => 'default', 'ID' => 5],
|
||||
['post_title' => 'Team', 'page_template' => 'tpl-alt.php', 'ID' => 5],
|
||||
], $updates);
|
||||
}
|
||||
|
||||
public function testAnUpdateThatThrowsStillRestoresTheRow(): void
|
||||
{
|
||||
[$writer, $input] = $this->failingWriter(['post_name' => 'unser-team']);
|
||||
$writer->shouldNotReceive('writeExtras');
|
||||
$updates = [];
|
||||
Functions\when('wp_update_post')->alias(static function (array $data) use (&$updates): int {
|
||||
$updates[] = $data;
|
||||
if (count($updates) === 1) {
|
||||
throw new RuntimeException('meta kaputt im save_post-Hook');
|
||||
}
|
||||
return 5;
|
||||
});
|
||||
|
||||
$failure = $this->executeExpectingFailure($writer, $input);
|
||||
|
||||
self::assertTrue($failure->rolledBack);
|
||||
self::assertSame(['post_name' => 'team', 'ID' => 5], $updates[1]);
|
||||
}
|
||||
|
||||
public function testAnIncompleteRollbackIsAPartialWriteAndLogged(): void
|
||||
{
|
||||
[$writer, $input] = $this->failingWriter(['post_title' => 'Unser Team'], false);
|
||||
$updates = [];
|
||||
$this->captureUpdates($updates);
|
||||
Functions\when('wp_revisions_enabled')->justReturn(false);
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->expects('record')
|
||||
->with('lit/content-update', 'page', '5', Mockery::type('string'), 811, null, 'partial')
|
||||
->andReturn(12);
|
||||
|
||||
$failure = $this->executeExpectingFailure($writer, $input, $log);
|
||||
|
||||
self::assertFalse($failure->rolledBack);
|
||||
self::assertStringContainsString('restore kaputt', $failure->getMessage());
|
||||
$error = $failure->toError();
|
||||
self::assertSame('lit_partial_write', $error->get_error_code());
|
||||
self::assertSame(5, $error->get_error_data()['post_id']);
|
||||
self::assertSame(811, $error->get_error_data()['revision_id']);
|
||||
}
|
||||
|
||||
public function testFailuresAfterTheWriteOnlyAddWarnings(): void
|
||||
{
|
||||
$input = ['post_id' => 5, 'rev' => Rev::ofPost($this->post), 'title' => 'Unser Team'];
|
||||
$writer = Mockery::mock(PostWriter::class);
|
||||
$writer->allows('postData')->andReturn(['post_title' => 'Unser Team']);
|
||||
$writer->allows('prepare')->andReturn(['fields' => [], 'terms' => [], 'warnings' => []]);
|
||||
$writer->allows('snapshot')->andReturn(['fields' => [], 'terms' => []]);
|
||||
$writer->allows('writeExtras');
|
||||
Functions\when('wp_save_post_revision')->justReturn(811);
|
||||
Functions\when('wp_get_post_revisions')->justReturn([811 => new WP_Post(['ID' => 811])]);
|
||||
Functions\when('wp_update_post')->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($writer, $log)->execute($input);
|
||||
} 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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user