feat: content-create and content-update with schema validation

All-or-nothing writes: validation (fields, terms, featured image, parent,
template) before any write; failed extras delete the new draft (create) or
restore revision, old columns, fields, terms and featured image (update).
content-update requires rev; Rev::ofPost covers content, title, excerpt,
status and post_modified_gmt. MetaFieldSource::write() now throws on failure.

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:01:48 +02:00
co-authored by Claude Opus 5.5
parent b9d46f3cec
commit 4f9a47fe32
15 changed files with 1355 additions and 9 deletions
@@ -0,0 +1,191 @@
<?php
declare(strict_types=1);
namespace LIT\MCP\Tests\Unit\Integrations\WordPress\Content;
use Brain\Monkey\Functions;
use LIT\MCP\Integrations\WordPress\Content\PostWriter;
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\TermAssigner;
use LIT\MCP\Tests\Unit\TestCase;
use Mockery;
use RuntimeException;
use WP_Post;
final class PostWriterTest extends TestCase
{
private function stubTheme(): void
{
$theme = Mockery::mock();
$theme->allows('get_page_templates')->with(null, 'lit_person')->andReturn(['tpl-wide.php' => 'Breit']);
Functions\when('wp_get_theme')->justReturn($theme);
}
public function testPrepareCollectsAllErrorsWithPaths(): void
{
$this->stubTheme();
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']));
$schema = Mockery::mock(ContentSchema::class);
$schema->allows('fields')->andReturn([
'role' => new FieldDefinition('role', 'Funktion', 'string', 'pods', 'writable', true),
]);
$writer = new PostWriter($schema, new FieldValidator(), new TermAssigner());
$result = $writer->prepare('lit_person', [
'terms' => ['genre' => [1]],
'featured_image' => 9,
'parent' => 3,
'template' => 'fehlt.php',
], true);
$fields = $result->get_error_data()['fields'];
self::assertSame('lit_validation_failed', $result->get_error_code());
self::assertSame('Pflichtfeld fehlt.', $fields['fields.role']);
self::assertSame('Taxonomie gehört nicht zu diesem Inhaltstyp.', $fields['terms.genre']);
self::assertSame('Inhaltstyp unterstützt kein Beitragsbild.', $fields['featured_image']);
self::assertStringContainsString('denselben Inhaltstyp', $fields['parent']);
self::assertStringContainsString('tpl-wide.php', $fields['template']);
}
public function testKnownDefaultAndEmptyTemplatesAreValid(): void
{
$this->stubTheme();
Functions\when('get_object_taxonomies')->justReturn([]);
$schema = Mockery::mock(ContentSchema::class);
$schema->allows('fields')->andReturn([]);
$writer = new PostWriter($schema, new FieldValidator(), new TermAssigner());
foreach (['tpl-wide.php', 'default', ''] as $template) {
self::assertIsArray($writer->prepare('lit_person', ['template' => $template], false), $template);
}
}
public function testPrepareReturnsValuesTermsAndWarnings(): void
{
Functions\when('get_object_taxonomies')->justReturn([]);
$schema = Mockery::mock(ContentSchema::class);
$schema->allows('fields')->andReturn(['city' => new FieldDefinition('city', 'city', 'unknown', 'sample', 'unknown')]);
$result = (new PostWriter($schema, new FieldValidator(), new TermAssigner()))
->prepare('page', ['fields' => ['city' => 'Trier']], false);
self::assertSame(['city' => 'Trier'], $result['fields']);
self::assertSame([], $result['terms']);
self::assertCount(1, $result['warnings']);
}
public function testPostDataMapsOnlyGivenKeys(): void
{
$writer = new PostWriter(Mockery::mock(ContentSchema::class), new FieldValidator(), new TermAssigner());
// wp_insert_post() ignores an empty page_template, so "empty = default template" is sent as 'default'.
self::assertSame(
['post_title' => 'Team', 'post_name' => 'team', 'page_template' => 'default'],
$writer->postData(['title' => 'Team', 'slug' => 'team', 'template' => '', 'fields' => []])
);
self::assertTrue($writer->hasExtras(['featured_image' => null]));
self::assertFalse($writer->hasExtras(['title' => 'x', 'fields' => []]));
}
public function testWriteExtrasGroupsFieldsBySourceAndHandlesTheFeaturedImage(): void
{
$role = new FieldDefinition('role', 'Funktion', 'string', 'pods', 'writable');
$city = new FieldDefinition('city', 'city', 'unknown', 'sample', 'unknown');
$definitions = ['city' => $city, 'role' => $role];
$pods = Mockery::mock(FieldSource::class);
$pods->expects('write')->with(5, 'lit_person', ['role' => 'Leitung'], $definitions);
$sample = Mockery::mock(FieldSource::class);
$sample->expects('write')->with(5, 'lit_person', ['city' => 'Trier'], $definitions);
$schema = Mockery::mock(ContentSchema::class);
$schema->allows('fields')->andReturn($definitions);
$schema->allows('sourceOf')->andReturnUsing(static fn (FieldDefinition $f) => $f->source === 'pods' ? $pods : $sample);
Functions\expect('delete_post_thumbnail')->once()->with(5);
(new PostWriter($schema, new FieldValidator(), new TermAssigner()))->writeExtras(
5,
'lit_person',
['fields' => ['role' => 'Leitung', 'city' => 'Trier'], 'terms' => [], 'warnings' => []],
['featured_image' => 0]
);
}
public function testAFeaturedImageThatDidNotStickThrows(): void
{
$schema = Mockery::mock(ContentSchema::class);
$schema->allows('fields')->andReturn([]);
Functions\when('set_post_thumbnail')->justReturn(false);
Functions\when('get_post_thumbnail_id')->justReturn(0);
$this->expectException(RuntimeException::class);
(new PostWriter($schema, new FieldValidator(), new TermAssigner()))->writeExtras(
5,
'lit_person',
['fields' => [], 'terms' => [], 'warnings' => []],
['featured_image' => 9]
);
}
public function testSnapshotKeepsTheOldValuesOfEverythingThatWillBeWrittenAndRestoreWritesThemBack(): void
{
$role = new FieldDefinition('role', 'Funktion', 'string', 'pods', 'writable');
$city = new FieldDefinition('city', 'city', 'unknown', 'sample', 'unknown');
$definitions = ['city' => $city, 'role' => $role];
$pods = Mockery::mock(FieldSource::class);
$pods->allows('read')->with(5, $role)->andReturn('Alt');
$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);
$schema = Mockery::mock(ContentSchema::class);
$schema->allows('fields')->andReturn($definitions);
$schema->allows('sourceOf')->andReturnUsing(static fn (FieldDefinition $f) => $f->source === 'pods' ? $pods : $sample);
Functions\expect('wp_get_object_terms')->once()->with(5, 'category', ['fields' => 'ids'])->andReturn(['2', 3]);
Functions\when('get_post_thumbnail_id')->justReturn(7);
$writer = new PostWriter($schema, new FieldValidator(), new TermAssigner());
$snapshot = $writer->snapshot(
5,
'lit_person',
['fields' => ['role' => 'Neu', 'city' => 'Trier'], 'terms' => ['category' => [4]], 'warnings' => []],
['featured_image' => 9]
);
self::assertSame(
['fields' => ['role' => 'Alt', 'city' => null], 'terms' => ['category' => [2, 3]], 'featured_image' => 7],
$snapshot
);
Functions\expect('wp_set_object_terms')->once()->with(5, [2, 3], 'category', false)->andReturn([2, 3]);
Functions\expect('set_post_thumbnail')->once()->with(5, 7)->andReturn(true);
$writer->restore(5, 'lit_person', $snapshot);
}
public function testRestoreTriesEveryStepAndReportsFailures(): void
{
$role = new FieldDefinition('role', 'Funktion', 'string', 'pods', 'writable');
$pods = Mockery::mock(FieldSource::class);
$pods->expects('write')->andThrow(new RuntimeException('pods kaputt'));
$schema = Mockery::mock(ContentSchema::class);
$schema->allows('fields')->andReturn(['role' => $role]);
$schema->allows('sourceOf')->andReturn($pods);
Functions\expect('wp_set_object_terms')->once()->andReturn([2]);
Functions\expect('delete_post_thumbnail')->once()->andReturn(true);
try {
(new PostWriter($schema, new FieldValidator(), new TermAssigner()))->restore(
5,
'lit_person',
['fields' => ['role' => 'Alt'], 'terms' => ['category' => [2]], 'featured_image' => 0]
);
self::fail('RuntimeException expected');
} catch (RuntimeException $e) {
self::assertStringContainsString('pods kaputt', $e->getMessage());
}
}
}