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:
@@ -8,6 +8,7 @@ use LIT\MCP\Abilities\JsonMap;
|
||||
use LIT\MCP\Abilities\Rev;
|
||||
use LIT\MCP\Tests\Unit\TestCase;
|
||||
use stdClass;
|
||||
use WP_Post;
|
||||
|
||||
final class RevAndJsonMapTest extends TestCase
|
||||
{
|
||||
@@ -18,6 +19,26 @@ final class RevAndJsonMapTest extends TestCase
|
||||
self::assertNotSame(Rev::of('abc'), Rev::of('abd'));
|
||||
}
|
||||
|
||||
public function testPostRevCoversContentTitleExcerptStatusAndModification(): void
|
||||
{
|
||||
$base = [
|
||||
'post_content' => '<p>A</p>',
|
||||
'post_title' => 'T',
|
||||
'post_excerpt' => 'E',
|
||||
'post_status' => 'draft',
|
||||
'post_modified_gmt' => '2026-09-25 08:00:00',
|
||||
];
|
||||
$rev = Rev::ofPost(new WP_Post($base));
|
||||
|
||||
self::assertMatchesRegularExpression('/^[0-9a-f]{12}$/', $rev);
|
||||
self::assertSame($rev, Rev::ofPost(new WP_Post($base)));
|
||||
self::assertNotSame(Rev::of('<p>A</p>'), $rev);
|
||||
foreach (['post_content' => '<p>B</p>', 'post_title' => 'U', 'post_excerpt' => 'F',
|
||||
'post_status' => 'publish', 'post_modified_gmt' => '2026-09-25 08:00:01'] as $key => $value) {
|
||||
self::assertNotSame($rev, Rev::ofPost(new WP_Post(array_merge($base, [$key => $value]))), $key);
|
||||
}
|
||||
}
|
||||
|
||||
public function testEmptyMapBecomesJsonObject(): void
|
||||
{
|
||||
self::assertInstanceOf(stdClass::class, JsonMap::of([]));
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace LIT\MCP\Tests\Unit\Integrations\WordPress\Content;
|
||||
|
||||
use Brain\Monkey\Actions;
|
||||
use Brain\Monkey\Functions;
|
||||
use LIT\MCP\Abilities\Errors;
|
||||
use LIT\MCP\Abilities\Rev;
|
||||
use LIT\MCP\ChangeLog\ChangeLog;
|
||||
use LIT\MCP\Integrations\WordPress\Content\ContentCreateAbility;
|
||||
use LIT\MCP\Integrations\WordPress\Content\PostWriter;
|
||||
use LIT\MCP\Integrations\WordPress\Schema\ContentSchema;
|
||||
use LIT\MCP\Tests\Unit\TestCase;
|
||||
use Mockery;
|
||||
use RuntimeException;
|
||||
use WP_Post;
|
||||
|
||||
final class ContentCreateAbilityTest extends TestCase
|
||||
{
|
||||
private function schema(): ContentSchema
|
||||
{
|
||||
Functions\when('get_post_type_object')->justReturn((object) ['cap' => (object) ['create_posts' => 'edit_pages']]);
|
||||
Functions\when('current_user_can')->justReturn(true);
|
||||
$schema = Mockery::mock(ContentSchema::class);
|
||||
$schema->allows('isManaged')->andReturn(true);
|
||||
|
||||
return $schema;
|
||||
}
|
||||
|
||||
public function testInvalidInputWritesNothing(): void
|
||||
{
|
||||
$writer = Mockery::mock(PostWriter::class);
|
||||
$writer->allows('prepare')->andReturn(Errors::invalidInput('ungültig', ['fields.role' => 'Pflichtfeld fehlt.']));
|
||||
Functions\expect('wp_insert_post')->never();
|
||||
|
||||
$result = (new ContentCreateAbility($this->schema(), $writer, Mockery::mock(ChangeLog::class)))
|
||||
->execute(['post_type' => 'lit_person', 'title' => 'Anna']);
|
||||
|
||||
self::assertSame('lit_validation_failed', $result->get_error_code());
|
||||
}
|
||||
|
||||
public function testCreatesADraftWritesExtrasLogsAndReturnsAToolResult(): void
|
||||
{
|
||||
$schema = $this->schema();
|
||||
$prepared = ['fields' => ['role' => 'Leitung'], 'terms' => [], 'warnings' => ['w']];
|
||||
$input = ['post_type' => 'lit_person', 'title' => 'Anna', 'fields' => ['role' => 'Leitung']];
|
||||
$writer = Mockery::mock(PostWriter::class);
|
||||
$writer->allows('prepare')->with('lit_person', $input, true)->andReturn($prepared);
|
||||
$writer->allows('postData')->andReturn(['post_title' => 'Anna']);
|
||||
$writer->expects('writeExtras')->with(42, 'lit_person', $prepared, $input);
|
||||
Functions\when('get_current_user_id')->justReturn(3);
|
||||
Functions\expect('wp_insert_post')->once()->with([
|
||||
'post_title' => 'Anna',
|
||||
'post_type' => 'lit_person',
|
||||
'post_status' => 'draft',
|
||||
'post_author' => 3,
|
||||
], true)->andReturn(42);
|
||||
Actions\expectDone('lit_mcp_after_save')->once()->with(42, 'lit/content-create', $input);
|
||||
$post = new WP_Post(['ID' => 42, 'post_title' => 'Anna', 'post_type' => 'lit_person']);
|
||||
Functions\when('get_post')->justReturn($post);
|
||||
Functions\when('get_preview_post_link')->justReturn('https://kunde.de/?p=42&preview=true');
|
||||
Functions\when('get_edit_post_link')->justReturn('https://kunde.de/wp-admin/post.php?post=42&action=edit');
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->expects('record')->with('lit/content-create', 'lit_person', '42', 'Entwurf "Anna" angelegt')->andReturn(7);
|
||||
|
||||
$result = (new ContentCreateAbility($schema, $writer, $log))->execute($input);
|
||||
|
||||
self::assertTrue($result['ok']);
|
||||
self::assertSame([['id' => '42', 'type' => 'lit_person', 'action' => 'created', 'label' => 'Anna']], $result['changed']);
|
||||
self::assertSame(7, $result['log_id']);
|
||||
self::assertSame(['w'], $result['warnings']);
|
||||
self::assertStringContainsString('Entwurf', $result['summary']);
|
||||
self::assertSame(Rev::ofPost($post), $result['rev']);
|
||||
}
|
||||
|
||||
public function testFailingExtrasDeleteTheNewDraftAndNothingIsLogged(): void
|
||||
{
|
||||
$schema = $this->schema();
|
||||
$input = ['post_type' => 'lit_person', 'title' => 'Anna', 'fields' => ['role' => 'Leitung']];
|
||||
$writer = Mockery::mock(PostWriter::class);
|
||||
$writer->allows('prepare')->andReturn(['fields' => ['role' => 'Leitung'], 'terms' => [], 'warnings' => []]);
|
||||
$writer->allows('postData')->andReturn(['post_title' => 'Anna']);
|
||||
$writer->expects('writeExtras')->andThrow(new RuntimeException('Pods could not save the fields.'));
|
||||
Functions\when('get_current_user_id')->justReturn(3);
|
||||
Functions\when('wp_insert_post')->justReturn(42);
|
||||
Functions\expect('wp_delete_post')->once()->with(42, true)->andReturn(new WP_Post(['ID' => 42]));
|
||||
Actions\expectDone('lit_mcp_after_save')->never();
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->expects('record')->never();
|
||||
|
||||
try {
|
||||
(new ContentCreateAbility($schema, $writer, $log))->execute($input);
|
||||
self::fail('RuntimeException expected');
|
||||
} catch (RuntimeException $e) {
|
||||
self::assertStringContainsString('Pods could not save the fields.', $e->getMessage());
|
||||
self::assertStringContainsString('deleted', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function testStatusIsNotAnInputAndPermissionNeedsCreateRights(): void
|
||||
{
|
||||
$schema = $this->schema();
|
||||
$ability = new ContentCreateAbility($schema, Mockery::mock(PostWriter::class), Mockery::mock(ChangeLog::class));
|
||||
|
||||
self::assertArrayNotHasKey('status', $ability->inputSchema()['properties']);
|
||||
self::assertFalse($ability->inputSchema()['additionalProperties']);
|
||||
self::assertTrue($ability->permission(['post_type' => 'page']));
|
||||
|
||||
Functions\when('current_user_can')->justReturn(false);
|
||||
self::assertSame('lit_forbidden', $ability->permission(['post_type' => 'page'])->get_error_code());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,217 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace LIT\MCP\Tests\Unit\Integrations\WordPress\Content;
|
||||
|
||||
use Brain\Monkey\Actions;
|
||||
use Brain\Monkey\Functions;
|
||||
use LIT\MCP\Abilities\Rev;
|
||||
use LIT\MCP\ChangeLog\ChangeLog;
|
||||
use LIT\MCP\Integrations\WordPress\Content\ContentUpdateAbility;
|
||||
use LIT\MCP\Integrations\WordPress\Content\PostWriter;
|
||||
use LIT\MCP\Integrations\WordPress\Schema\ContentSchema;
|
||||
use LIT\MCP\Tests\Unit\TestCase;
|
||||
use Mockery;
|
||||
use RuntimeException;
|
||||
use WP_Post;
|
||||
|
||||
final class ContentUpdateAbilityTest extends TestCase
|
||||
{
|
||||
private WP_Post $post;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->post = new WP_Post([
|
||||
'ID' => 5,
|
||||
'post_type' => 'page',
|
||||
'post_status' => 'publish',
|
||||
'post_title' => 'Team',
|
||||
'post_name' => 'team',
|
||||
'post_content' => '<p>Alt</p>',
|
||||
]);
|
||||
Functions\when('get_post')->justReturn($this->post);
|
||||
Functions\when('current_user_can')->justReturn(true);
|
||||
}
|
||||
|
||||
private function schema(): ContentSchema
|
||||
{
|
||||
$schema = Mockery::mock(ContentSchema::class);
|
||||
$schema->allows('isManaged')->andReturn(true);
|
||||
|
||||
return $schema;
|
||||
}
|
||||
|
||||
private function ability(PostWriter $writer, ?ChangeLog $log = null): ContentUpdateAbility
|
||||
{
|
||||
return new ContentUpdateAbility($this->schema(), $writer, $log ?? Mockery::mock(ChangeLog::class));
|
||||
}
|
||||
|
||||
public function testRevIsRequired(): void
|
||||
{
|
||||
$writer = Mockery::mock(PostWriter::class);
|
||||
Functions\expect('wp_update_post')->never();
|
||||
|
||||
self::assertContains('rev', $this->ability($writer)->inputSchema()['required']);
|
||||
self::assertSame(
|
||||
'lit_rev_mismatch',
|
||||
$this->ability($writer)->execute(['post_id' => 5, 'title' => 'Neu'])->get_error_code()
|
||||
);
|
||||
}
|
||||
|
||||
public function testStaleRevIsRejectedWithoutWriting(): void
|
||||
{
|
||||
Functions\expect('wp_update_post')->never();
|
||||
|
||||
$result = $this->ability(Mockery::mock(PostWriter::class))
|
||||
->execute(['post_id' => 5, 'rev' => 'stale', 'title' => 'Neu']);
|
||||
|
||||
self::assertSame('lit_rev_mismatch', $result->get_error_code());
|
||||
self::assertStringContainsString('lit-content-get', $result->get_error_data()['hint']);
|
||||
}
|
||||
|
||||
public function testAContentOnlyRevIsStaleBecauseTitleAndStatusCountToo(): void
|
||||
{
|
||||
$result = $this->ability(Mockery::mock(PostWriter::class))
|
||||
->execute(['post_id' => 5, 'rev' => Rev::of('<p>Alt</p>'), 'title' => 'Neu']);
|
||||
|
||||
self::assertSame('lit_rev_mismatch', $result->get_error_code());
|
||||
}
|
||||
|
||||
public function testRawContentOnDiviPagesIsUnsupported(): void
|
||||
{
|
||||
Functions\when('get_post_meta')->justReturn('on');
|
||||
$writer = Mockery::mock(PostWriter::class);
|
||||
$writer->expects('prepare')->never();
|
||||
|
||||
$result = $this->ability($writer)
|
||||
->execute(['post_id' => 5, 'rev' => Rev::ofPost($this->post), 'content' => '<p>Neu</p>']);
|
||||
|
||||
self::assertSame('lit_unsupported', $result->get_error_code());
|
||||
self::assertStringContainsString('lit-divi-outline', $result->get_error_message());
|
||||
}
|
||||
|
||||
public function testNothingToChangeIsAValidationError(): void
|
||||
{
|
||||
$writer = Mockery::mock(PostWriter::class);
|
||||
$writer->allows('postData')->andReturn([]);
|
||||
$writer->allows('hasExtras')->andReturn(false);
|
||||
|
||||
$result = $this->ability($writer)->execute(['post_id' => 5, 'rev' => Rev::ofPost($this->post)]);
|
||||
|
||||
self::assertSame('lit_validation_failed', $result->get_error_code());
|
||||
}
|
||||
|
||||
public function testUpdateKeepsTheStatusAndCapturesARestorePointFirst(): void
|
||||
{
|
||||
$order = [];
|
||||
$prepared = ['fields' => [], 'terms' => [], 'warnings' => []];
|
||||
$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('hasExtras')->andReturn(false);
|
||||
$writer->allows('prepare')->with('page', $input, false)->andReturn($prepared);
|
||||
$writer->allows('snapshot')->andReturn(['fields' => [], 'terms' => []]);
|
||||
$writer->expects('writeExtras')->with(5, 'page', $prepared, $input);
|
||||
Functions\expect('wp_save_post_revision')->once()->with(5)->andReturnUsing(static function () use (&$order) {
|
||||
$order[] = 'revision';
|
||||
return 811;
|
||||
});
|
||||
Functions\when('wp_get_post_revisions')->justReturn([811 => new WP_Post(['ID' => 811])]);
|
||||
Functions\expect('wp_update_post')->once()->with(['post_title' => 'Unser Team', 'ID' => 5], true)
|
||||
->andReturnUsing(static function () use (&$order): int {
|
||||
$order[] = 'update';
|
||||
return 5;
|
||||
});
|
||||
Actions\expectDone('lit_mcp_after_save')->once()->with(5, 'lit/content-update', $input);
|
||||
Functions\when('get_preview_post_link')->justReturn('https://kunde.de/team/');
|
||||
Functions\when('get_edit_post_link')->justReturn('https://kunde.de/wp-admin/post.php?post=5&action=edit');
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->expects('record')->with('lit/content-update', 'page', '5', Mockery::type('string'), 811)->andReturn(4711);
|
||||
|
||||
$result = $this->ability($writer, $log)->execute($input);
|
||||
|
||||
self::assertSame(['revision', 'update'], $order);
|
||||
self::assertSame(811, $result['revision_id']);
|
||||
self::assertSame(4711, $result['log_id']);
|
||||
self::assertSame('updated', $result['changed'][0]['action']);
|
||||
self::assertSame(Rev::ofPost($this->post), $result['rev']);
|
||||
self::assertStringContainsString('Status unverändert: publish', $result['summary']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
* @return array{PostWriter, array<string, mixed>, array<string, mixed>}
|
||||
*/
|
||||
private function failingWriter(array $data): array
|
||||
{
|
||||
$input = ['post_id' => 5, 'rev' => Rev::ofPost($this->post), 'title' => 'Unser Team', 'fields' => ['a' => 1]];
|
||||
$snapshot = ['fields' => ['a' => 0], 'terms' => []];
|
||||
$writer = Mockery::mock(PostWriter::class);
|
||||
$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);
|
||||
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];
|
||||
}
|
||||
|
||||
/** @param array<string, mixed> $input */
|
||||
private function executeExpectingRollback(PostWriter $writer, array $input): void
|
||||
{
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->expects('record')->never();
|
||||
|
||||
try {
|
||||
$this->ability($writer, $log)->execute($input);
|
||||
self::fail('RuntimeException expected');
|
||||
} catch (RuntimeException $e) {
|
||||
self::assertStringContainsString('meta kaputt', $e->getMessage());
|
||||
self::assertStringContainsString('rolled back', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
});
|
||||
Functions\when('wp_revisions_enabled')->justReturn(true);
|
||||
Functions\expect('wp_restore_post_revision')->once()->with(811, ['post_title'])->andReturn(5);
|
||||
|
||||
$this->executeExpectingRollback($writer, $input);
|
||||
|
||||
self::assertSame([
|
||||
['post_title' => 'Unser Team', 'post_name' => 'unser-team', 'ID' => 5],
|
||||
['post_name' => 'team', 'ID' => 5],
|
||||
], $updates);
|
||||
}
|
||||
|
||||
public function testWithoutRevisionsTheOldColumnValuesAreWrittenBack(): void
|
||||
{
|
||||
[$writer, $input] = $this->failingWriter(['post_title' => 'Unser Team', 'page_template' => 'tpl-wide.php']);
|
||||
$updates = [];
|
||||
Functions\when('wp_update_post')->alias(static function (array $data) use (&$updates): int {
|
||||
$updates[] = $data;
|
||||
return 5;
|
||||
});
|
||||
Functions\when('wp_revisions_enabled')->justReturn(false);
|
||||
Functions\expect('wp_restore_post_revision')->never();
|
||||
|
||||
$this->executeExpectingRollback($writer, $input);
|
||||
|
||||
self::assertSame([
|
||||
['post_title' => 'Unser Team', 'page_template' => 'tpl-wide.php', 'ID' => 5],
|
||||
['post_title' => 'Team', 'page_template' => 'default', 'ID' => 5],
|
||||
], $updates);
|
||||
}
|
||||
}
|
||||
@@ -68,7 +68,7 @@ final class PostPresenterTest extends TestCase
|
||||
self::assertSame(['role' => 'Leitung'], $full['fields']);
|
||||
self::assertSame(['id' => 33, 'url' => 'https://kunde.de/wp-content/uploads/foto.jpg', 'alt' => 'Porträt'], $full['featured_image']);
|
||||
self::assertSame(['id' => 2, 'name' => 'Jan'], $full['author']);
|
||||
self::assertSame(Rev::of('<p>Hallo</p>'), $full['rev']);
|
||||
self::assertSame(Rev::ofPost($post), $full['rev']);
|
||||
self::assertSame([], $full['hints']);
|
||||
}
|
||||
|
||||
@@ -90,7 +90,7 @@ final class PostPresenterTest extends TestCase
|
||||
self::assertStringContainsString('lit-divi-outline', $full['hints'][0]);
|
||||
self::assertInstanceOf(stdClass::class, $full['terms']);
|
||||
self::assertInstanceOf(stdClass::class, $full['fields']);
|
||||
self::assertSame(Rev::of('<!-- wp:divi/section /-->'), $full['rev']);
|
||||
self::assertSame(Rev::ofPost($post), $full['rev']);
|
||||
}
|
||||
|
||||
public function testPasswordAndProtectedReadonlyMetaFieldsAreOmitted(): void
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ use Brain\Monkey\Functions;
|
||||
use LIT\MCP\Integrations\WordPress\Schema\FieldDefinition;
|
||||
use LIT\MCP\Integrations\WordPress\Schema\RegisteredMetaSource;
|
||||
use LIT\MCP\Tests\Unit\TestCase;
|
||||
use RuntimeException;
|
||||
|
||||
final class RegisteredMetaSourceTest extends TestCase
|
||||
{
|
||||
@@ -89,4 +90,54 @@ final class RegisteredMetaSourceTest extends TestCase
|
||||
['footnotes' => $single, 'tags_list' => $multi, 'old' => $gone]
|
||||
);
|
||||
}
|
||||
|
||||
public function testUnchangedValueIsNotAFailure(): void
|
||||
{
|
||||
$field = new FieldDefinition('count', 'count', 'integer', 'meta', 'writable');
|
||||
Functions\expect('update_post_meta')->once()->with(5, 'count', 3)->andReturn(false);
|
||||
Functions\expect('get_post_meta')->once()->with(5, 'count', false)->andReturn(['3']);
|
||||
|
||||
(new RegisteredMetaSource())->write(5, 'event', ['count' => 3], ['count' => $field]);
|
||||
self::addToAssertionCount(1);
|
||||
}
|
||||
|
||||
public function testFailedUpdateThrows(): void
|
||||
{
|
||||
$field = new FieldDefinition('count', 'count', 'integer', 'meta', 'writable');
|
||||
Functions\when('update_post_meta')->justReturn(false);
|
||||
Functions\when('get_post_meta')->justReturn(['2']);
|
||||
|
||||
$this->expectException(RuntimeException::class);
|
||||
(new RegisteredMetaSource())->write(5, 'event', ['count' => 3], ['count' => $field]);
|
||||
}
|
||||
|
||||
public function testFailedDeleteOfAnExistingKeyThrows(): void
|
||||
{
|
||||
$field = new FieldDefinition('old', 'old', 'string', 'meta', 'writable');
|
||||
Functions\when('delete_post_meta')->justReturn(false);
|
||||
Functions\expect('metadata_exists')->once()->with('post', 5, 'old')->andReturn(true);
|
||||
|
||||
$this->expectException(RuntimeException::class);
|
||||
(new RegisteredMetaSource())->write(5, 'event', ['old' => null], ['old' => $field]);
|
||||
}
|
||||
|
||||
public function testDeletingAMissingKeyIsNotAFailure(): void
|
||||
{
|
||||
$field = new FieldDefinition('old', 'old', 'string', 'meta', 'writable');
|
||||
Functions\when('delete_post_meta')->justReturn(false);
|
||||
Functions\expect('metadata_exists')->once()->with('post', 5, 'old')->andReturn(false);
|
||||
|
||||
(new RegisteredMetaSource())->write(5, 'event', ['old' => null], ['old' => $field]);
|
||||
self::addToAssertionCount(1);
|
||||
}
|
||||
|
||||
public function testFailedAddOfAListItemThrows(): void
|
||||
{
|
||||
$multi = new FieldDefinition('tags_list', 'tags_list', 'array', 'meta', 'writable', false, true);
|
||||
Functions\when('delete_post_meta')->justReturn(true);
|
||||
Functions\when('add_post_meta')->justReturn(false);
|
||||
|
||||
$this->expectException(RuntimeException::class);
|
||||
(new RegisteredMetaSource())->write(5, 'event', ['tags_list' => ['a']], ['tags_list' => $multi]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,8 @@ final class WordPressIntegrationTest extends TestCase
|
||||
'lit/content-types',
|
||||
'lit/content-query',
|
||||
'lit/content-get',
|
||||
'lit/content-create',
|
||||
'lit/content-update',
|
||||
];
|
||||
|
||||
public function testProvidesTheWordPressAbilitiesInOrder(): void
|
||||
|
||||
Reference in New Issue
Block a user