Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
276 lines
12 KiB
PHP
276 lines
12 KiB
PHP
<?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\Abilities\WriteFailed;
|
|
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\Support\FakeChangeLog;
|
|
use LIT\MCP\Tests\Support\SchemaFixture;
|
|
use LIT\MCP\Tests\Unit\TestCase;
|
|
use Mockery;
|
|
use Mockery\MockInterface;
|
|
use RuntimeException;
|
|
use WP_Post;
|
|
|
|
final class ContentCreateAbilityTest extends TestCase
|
|
{
|
|
private const INPUT = ['post_type' => 'lit_person', 'title' => 'Anna', 'fields' => ['role' => 'Leitung']];
|
|
|
|
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 = SchemaFixture::managed();
|
|
|
|
return $schema;
|
|
}
|
|
|
|
/** @return PostWriter&MockInterface */
|
|
private function writer(array $warnings = []): PostWriter
|
|
{
|
|
$writer = Mockery::mock(PostWriter::class);
|
|
$writer->allows('prepare')->andReturn(['fields' => ['role' => 'Leitung'], 'terms' => [], 'warnings' => $warnings]);
|
|
$writer->allows('postData')->andReturn(['post_title' => 'Anna']);
|
|
$writer->allows('deniedMeta')->andReturn([]);
|
|
Functions\when('get_current_user_id')->justReturn(3);
|
|
|
|
return $writer;
|
|
}
|
|
|
|
private function execute(PostWriter $writer, ChangeLog $log): WriteFailed
|
|
{
|
|
try {
|
|
(new ContentCreateAbility($this->schema(), $writer, $log))->execute(self::INPUT);
|
|
} catch (WriteFailed $e) {
|
|
return $e;
|
|
}
|
|
|
|
self::fail('WriteFailed expected');
|
|
}
|
|
|
|
private function stubResultLinks(): WP_Post
|
|
{
|
|
$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');
|
|
|
|
return $post;
|
|
}
|
|
|
|
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, FakeChangeLog::forbidden()))
|
|
->execute(['post_type' => 'lit_person', 'title' => 'Anna']);
|
|
|
|
self::assertToolError($result, 'validation_failed');
|
|
}
|
|
|
|
public function testCreatesADraftWritesExtrasLogsAndReturnsAToolResult(): void
|
|
{
|
|
$schema = $this->schema();
|
|
$prepared = ['fields' => ['role' => 'Leitung'], 'terms' => [], 'warnings' => ['w']];
|
|
$input = self::INPUT;
|
|
$writer = Mockery::mock(PostWriter::class);
|
|
$writer->allows('prepare')->with('lit_person', $input, true)->andReturn($prepared);
|
|
$writer->allows('postData')->andReturn(['post_title' => 'Anna']);
|
|
$writer->expects('deniedMeta')->with(42, 'lit_person', ['role' => 'Leitung'], 'add_post_meta')->andReturn([]);
|
|
$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 = $this->stubResultLinks();
|
|
$log = new FakeChangeLog(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']);
|
|
$log->assertRecorded([
|
|
'ability' => 'lit/content-create',
|
|
'object_type' => 'lit_person',
|
|
'object_id' => '42',
|
|
'summary' => 'Entwurf "Anna" angelegt',
|
|
'revision_id' => null,
|
|
]);
|
|
}
|
|
|
|
public function testFailingExtrasDeleteCreatedTermsAndTheNewDraft(): void
|
|
{
|
|
$writer = $this->writer();
|
|
$writer->expects('writeExtras')->andThrow(new RuntimeException('Pods could not save the fields.'));
|
|
$writer->expects('deleteCreatedTerms');
|
|
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 = FakeChangeLog::forbidden();
|
|
|
|
$failure = $this->execute($writer, $log);
|
|
|
|
self::assertTrue($failure->rolledBack);
|
|
self::assertStringContainsString('Pods could not save the fields.', $failure->getMessage());
|
|
self::assertToolError($failure->toError(), 'write_failed');
|
|
}
|
|
|
|
public function testMetaFieldsTheUserMayNotAddAreRefusedAndTheDraftIsRemoved(): void
|
|
{
|
|
$writer = Mockery::mock(PostWriter::class);
|
|
$writer->allows('prepare')->andReturn(['fields' => ['city' => 'Trier'], 'terms' => [], 'warnings' => []]);
|
|
$writer->allows('postData')->andReturn(['post_title' => 'Anna']);
|
|
$writer->expects('deniedMeta')->with(42, 'lit_person', ['city' => 'Trier'], 'add_post_meta')
|
|
->andReturn(['fields.city' => 'Keine Berechtigung, dieses Feld zu ändern.']);
|
|
$writer->expects('writeExtras')->never();
|
|
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]));
|
|
$log = FakeChangeLog::forbidden();
|
|
|
|
$result = (new ContentCreateAbility($this->schema(), $writer, $log))->execute(self::INPUT);
|
|
|
|
self::assertToolError($result, 'validation_failed');
|
|
self::assertArrayHasKey('fields.city', self::errorFields($result));
|
|
}
|
|
|
|
public function testARefusedDraftThatCannotBeDeletedIsAPartialWrite(): void
|
|
{
|
|
$writer = Mockery::mock(PostWriter::class);
|
|
$writer->allows('prepare')->andReturn(['fields' => ['city' => 'Trier'], 'terms' => [], 'warnings' => []]);
|
|
$writer->allows('postData')->andReturn(['post_title' => 'Anna']);
|
|
$writer->allows('deniedMeta')->andReturn(['fields.city' => 'Keine Berechtigung, dieses Feld zu ändern.']);
|
|
Functions\when('get_current_user_id')->justReturn(3);
|
|
Functions\when('wp_insert_post')->justReturn(42);
|
|
Functions\when('wp_delete_post')->justReturn(false);
|
|
$log = FakeChangeLog::partialsOnly();
|
|
|
|
try {
|
|
(new ContentCreateAbility($this->schema(), $writer, $log))->execute(self::INPUT);
|
|
self::fail('WriteFailed expected');
|
|
} catch (WriteFailed $e) {
|
|
self::assertFalse($e->rolledBack);
|
|
self::assertSame(42, $e->postId);
|
|
}
|
|
$log->assertPartials([]);
|
|
}
|
|
|
|
public function testADraftThatCannotBeDeletedIsAPartialWriteAndLogged(): void
|
|
{
|
|
$writer = $this->writer();
|
|
$writer->expects('writeExtras')->andThrow(new RuntimeException('meta kaputt'));
|
|
$writer->expects('deleteCreatedTerms');
|
|
Functions\when('wp_insert_post')->justReturn(42);
|
|
Functions\when('wp_delete_post')->justReturn(false);
|
|
$log = FakeChangeLog::partialsOnly();
|
|
|
|
$failure = $this->execute($writer, $log);
|
|
|
|
self::assertFalse($failure->rolledBack);
|
|
self::assertSame(42, $failure->postId);
|
|
self::assertStringContainsString('Eintrag 42', $failure->toError()->get_error_message());
|
|
$log->assertPartials(['ability' => 'lit/content-create', 'object_type' => 'lit_person', 'object_id' => '42']);
|
|
}
|
|
|
|
public function testLeftoverCreatedTermsMakeItAPartialWrite(): void
|
|
{
|
|
$writer = $this->writer();
|
|
$writer->expects('writeExtras')->andThrow(new RuntimeException('meta kaputt'));
|
|
$writer->expects('deleteCreatedTerms')->andThrow(new RuntimeException('term 15 bleibt'));
|
|
Functions\when('wp_insert_post')->justReturn(42);
|
|
Functions\when('wp_delete_post')->justReturn(new WP_Post(['ID' => 42]));
|
|
$log = FakeChangeLog::partialsOnly();
|
|
|
|
$failure = $this->execute($writer, $log);
|
|
|
|
self::assertFalse($failure->rolledBack);
|
|
self::assertStringContainsString('term 15 bleibt', $failure->getMessage());
|
|
$log->assertPartials([]);
|
|
}
|
|
|
|
public function testAnInsertThatThrowsIsAPartialWriteWithoutId(): void
|
|
{
|
|
$writer = $this->writer();
|
|
Functions\when('wp_insert_post')->alias(static function (): never {
|
|
throw new RuntimeException('save_post hook kaputt');
|
|
});
|
|
$log = FakeChangeLog::partialsOnly();
|
|
|
|
$failure = $this->execute($writer, $log);
|
|
|
|
self::assertFalse($failure->rolledBack);
|
|
self::assertNull($failure->postId);
|
|
self::assertToolError($failure->toError(), 'partial_write');
|
|
$log->assertPartials(['ability' => 'lit/content-create', 'object_type' => 'lit_person', 'object_id' => '']);
|
|
}
|
|
|
|
public function testFailuresAfterTheWriteOnlyAddWarnings(): void
|
|
{
|
|
$writer = $this->writer(['w']);
|
|
$writer->allows('writeExtras');
|
|
Functions\when('wp_insert_post')->justReturn(42);
|
|
$this->stubResultLinks();
|
|
Actions\expectDone('lit_mcp_after_save')->once()->whenHappen(static function (): void {
|
|
throw new RuntimeException('listener kaputt');
|
|
});
|
|
$log = new FakeChangeLog(1, new RuntimeException('db weg'));
|
|
$file = (string) tempnam(sys_get_temp_dir(), 'lit-mcp-log');
|
|
$previous = ini_set('error_log', $file);
|
|
|
|
try {
|
|
$result = (new ContentCreateAbility($this->schema(), $writer, $log))->execute(self::INPUT);
|
|
} finally {
|
|
ini_set('error_log', (string) $previous);
|
|
}
|
|
|
|
self::assertTrue($result['ok']);
|
|
self::assertNull($result['log_id']);
|
|
self::assertCount(3, $result['warnings']);
|
|
self::assertStringContainsString('listener kaputt', (string) file_get_contents($file));
|
|
self::assertStringContainsString('db weg', (string) file_get_contents($file));
|
|
unlink($file);
|
|
$log->assertRecorded([]);
|
|
}
|
|
|
|
public function testStatusIsNotAnInputAndPermissionNeedsCreateRights(): void
|
|
{
|
|
$schema = $this->schema();
|
|
$ability = new ContentCreateAbility($schema, Mockery::mock(PostWriter::class), FakeChangeLog::forbidden());
|
|
|
|
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::assertToolError($ability->permission(['post_type' => 'page']), 'forbidden');
|
|
}
|
|
|
|
public function testDescriptionNamesPresetStacksAndGroupPresets(): void
|
|
{
|
|
$ability = new ContentCreateAbility($this->schema(), Mockery::mock(PostWriter::class), FakeChangeLog::forbidden());
|
|
$description = $ability->description();
|
|
|
|
self::assertStringContainsString('group_presets', $description);
|
|
self::assertStringContainsString('clear_overrides', $description);
|
|
}
|
|
}
|