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:
2026-09-25 23:11:18 +02:00
co-authored by Claude Opus 5.5
parent 4f9a47fe32
commit a9b2f2db71
18 changed files with 934 additions and 130 deletions
@@ -10,6 +10,7 @@ use LIT\MCP\Integrations\WordPress\Schema\ContentSchema;
use LIT\MCP\Integrations\WordPress\Schema\FieldDefinition;
use LIT\MCP\Integrations\WordPress\Schema\FieldSource;
use LIT\MCP\Integrations\WordPress\Schema\FieldValidator;
use LIT\MCP\Integrations\WordPress\Schema\RegisteredMetaSource;
use LIT\MCP\Integrations\WordPress\Schema\TermAssigner;
use LIT\MCP\Tests\Unit\TestCase;
use Mockery;
@@ -31,6 +32,9 @@ final class PostWriterTest extends TestCase
Functions\when('get_object_taxonomies')->justReturn([]);
Functions\when('post_type_supports')->justReturn(false);
Functions\when('get_post')->justReturn(new WP_Post(['ID' => 3, 'post_type' => 'post']));
Functions\when('is_post_type_hierarchical')->justReturn(true);
Functions\when('get_post_ancestors')->justReturn([]);
Functions\when('current_user_can')->justReturn(true);
$schema = Mockery::mock(ContentSchema::class);
$schema->allows('fields')->andReturn([
'role' => new FieldDefinition('role', 'Funktion', 'string', 'pods', 'writable', true),
@@ -53,6 +57,53 @@ final class PostWriterTest extends TestCase
self::assertStringContainsString('tpl-wide.php', $fields['template']);
}
private function writerWithoutFields(): PostWriter
{
Functions\when('get_object_taxonomies')->justReturn([]);
$schema = Mockery::mock(ContentSchema::class);
$schema->allows('fields')->andReturn([]);
return new PostWriter($schema, new FieldValidator(), new TermAssigner());
}
/** @param array<string, mixed> $input */
private function errorFor(PostWriter $writer, array $input, string $path): string
{
$result = $writer->prepare('page', $input, false);
self::assertFalse(is_array($result), $path . ' should be rejected');
return (string) $result->get_error_data()['fields'][$path];
}
public function testParentMustBeAReadableNonDescendantEntryOfAHierarchicalType(): void
{
$writer = $this->writerWithoutFields();
Functions\when('get_post')->justReturn(new WP_Post(['ID' => 8, 'post_type' => 'page']));
Functions\when('get_post_ancestors')->justReturn([5, 1]);
Functions\when('current_user_can')->justReturn(true);
Functions\when('is_post_type_hierarchical')->justReturn(false);
self::assertStringContainsString('hierarchisch', $this->errorFor($writer, ['parent' => 8], 'parent'));
Functions\when('is_post_type_hierarchical')->justReturn(true);
self::assertStringContainsString('selbst', $this->errorFor($writer, ['post_id' => 8, 'parent' => 8], 'parent'));
self::assertStringContainsString('untergeordnet', $this->errorFor($writer, ['post_id' => 5, 'parent' => 8], 'parent'));
self::assertIsArray($writer->prepare('page', ['post_id' => 9, 'parent' => 8], false));
Functions\when('current_user_can')->justReturn(false);
self::assertStringContainsString('Berechtigung', $this->errorFor($writer, ['parent' => 8], 'parent'));
}
public function testFeaturedImageMustBeReadable(): void
{
$writer = $this->writerWithoutFields();
Functions\when('post_type_supports')->justReturn(true);
Functions\when('wp_attachment_is_image')->justReturn(true);
Functions\expect('current_user_can')->once()->with('read_post', 9)->andReturn(false);
self::assertStringContainsString('Berechtigung', $this->errorFor($writer, ['featured_image' => 9], 'featured_image'));
}
public function testKnownDefaultAndEmptyTemplatesAreValid(): void
{
$this->stubTheme();
@@ -141,7 +192,7 @@ final class PostWriterTest extends TestCase
$pods->expects('write')->with(5, 'lit_person', ['role' => 'Alt'], $definitions);
$sample = Mockery::mock(FieldSource::class);
$sample->allows('read')->with(5, $city)->andReturn('');
$sample->expects('write')->with(5, 'lit_person', ['city' => null], $definitions);
$sample->expects('write')->with(5, 'lit_person', ['city' => ''], $definitions);
$schema = Mockery::mock(ContentSchema::class);
$schema->allows('fields')->andReturn($definitions);
$schema->allows('sourceOf')->andReturnUsing(static fn (FieldDefinition $f) => $f->source === 'pods' ? $pods : $sample);
@@ -157,7 +208,7 @@ final class PostWriterTest extends TestCase
);
self::assertSame(
['fields' => ['role' => 'Alt', 'city' => null], 'terms' => ['category' => [2, 3]], 'featured_image' => 7],
['fields' => ['role' => 'Alt', 'city' => ''], 'terms' => ['category' => [2, 3]], 'featured_image' => 7],
$snapshot
);
@@ -166,6 +217,60 @@ final class PostWriterTest extends TestCase
$writer->restore(5, 'lit_person', $snapshot);
}
public function testSnapshotTellsAbsentMetaFromStoredEmptyValues(): void
{
$kept = new FieldDefinition('kept', 'kept', 'string', 'meta', 'writable');
$absent = new FieldDefinition('absent', 'absent', 'string', 'meta', 'writable');
$flag = new FieldDefinition('flag', 'flag', 'boolean', 'pods', 'writable');
$pods = Mockery::mock(FieldSource::class);
$pods->allows('read')->andReturn(false);
$schema = Mockery::mock(ContentSchema::class);
$schema->allows('fields')->andReturn(['absent' => $absent, 'flag' => $flag, 'kept' => $kept]);
$schema->allows('sourceOf')->andReturnUsing(
static fn (FieldDefinition $f) => $f->source === 'pods' ? $pods : new RegisteredMetaSource()
);
Functions\when('get_post_meta')->justReturn('');
Functions\when('metadata_exists')->alias(static fn (string $type, int $id, string $key): bool => $key === 'kept');
$snapshot = (new PostWriter($schema, new FieldValidator(), new TermAssigner()))->snapshot(
5,
'lit_person',
['fields' => ['kept' => 'x', 'absent' => 'y', 'flag' => true], 'terms' => [], 'warnings' => []],
[]
);
self::assertSame(['kept' => '', 'absent' => null, 'flag' => false], $snapshot['fields']);
}
public function testTermsCreatedByWriteExtrasCanBeDeletedAgain(): void
{
$role = new FieldDefinition('role', 'Funktion', 'string', 'pods', 'writable');
$pods = Mockery::mock(FieldSource::class);
$pods->allows('write')->andThrow(new RuntimeException('pods kaputt'));
$schema = Mockery::mock(ContentSchema::class);
$schema->allows('fields')->andReturn(['role' => $role]);
$schema->allows('sourceOf')->andReturn($pods);
Functions\when('wp_insert_term')->justReturn(['term_id' => 15]);
Functions\when('wp_set_object_terms')->justReturn([15]);
$writer = new PostWriter($schema, new FieldValidator(), new TermAssigner());
try {
$writer->writeExtras(
5,
'lit_person',
['fields' => ['role' => 'x'], 'terms' => ['category' => ['Neu']], 'warnings' => []],
[]
);
self::fail('RuntimeException expected');
} catch (RuntimeException $e) {
self::assertSame('pods kaputt', $e->getMessage());
}
Functions\expect('wp_delete_term')->once()->with(15, 'category')->andReturn(true);
$writer->deleteCreatedTerms();
$writer->deleteCreatedTerms(); // nothing left – no second call
}
public function testRestoreTriesEveryStepAndReportsFailures(): void
{
$role = new FieldDefinition('role', 'Funktion', 'string', 'pods', 'writable');