tests/Support/AssertsToolErrors (used by TestCase): assertToolError($result, code)
checks the WP_Error code lit_<code> and the code of the client JSON and returns the
payload; clientError() decodes the payload; errorFields() returns the fields and
asserts they exist. Replaces the private clientError() decoders of
DiviWriteAbilitiesTest/DiviReplaceTextAbilityTest, 216 single-line
assertSame('lit_…', …->get_error_code()) assertions, 24 get_error_data()['fields']
digs and two json_decode(message) reads. ErrorsTest keeps json_decode: it tests
the format itself. Tests 1404 -> 1409 (AssertsToolErrorsTest); no test has fewer
assertions.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
97 lines
3.7 KiB
PHP
97 lines
3.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace LIT\MCP\Tests\Unit\Integrations\WordPress\Status;
|
|
|
|
use Brain\Monkey\Actions;
|
|
use Brain\Monkey\Functions;
|
|
use LIT\MCP\Abilities\Rev;
|
|
use LIT\MCP\ChangeLog\ChangeLog;
|
|
use LIT\MCP\Integrations\WordPress\Status\PostCloner;
|
|
use LIT\MCP\Integrations\WordPress\Status\PostDuplicateAbility;
|
|
use LIT\MCP\Tests\Support\FakeChangeLog;
|
|
use LIT\MCP\Tests\Support\SchemaFixture;
|
|
use LIT\MCP\Tests\Unit\TestCase;
|
|
use Mockery;
|
|
use WP_Post;
|
|
|
|
final class PostDuplicateAbilityTest extends TestCase
|
|
{
|
|
private WP_Post $original;
|
|
private WP_Post $copy;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
$this->original = new WP_Post(['ID' => 5, 'post_type' => 'page', 'post_title' => 'Team']);
|
|
$this->copy = new WP_Post(['ID' => 77, 'post_type' => 'page', 'post_title' => 'Team (Kopie)']);
|
|
Functions\when('get_post')->alias(fn (int $id) => $id === 5 ? $this->original : $this->copy);
|
|
Functions\when('current_user_can')->justReturn(true);
|
|
Functions\when('get_post_type_object')->justReturn((object) ['cap' => (object) ['create_posts' => 'edit_pages']]);
|
|
Functions\when('get_preview_post_link')->justReturn('https://kunde.de/?p=77&preview=true');
|
|
Functions\when('get_edit_post_link')->justReturn('https://kunde.de/wp-admin/post.php?post=77&action=edit');
|
|
}
|
|
|
|
private function ability(PostCloner $cloner, ChangeLog $log): PostDuplicateAbility
|
|
{
|
|
$schema = SchemaFixture::managed();
|
|
|
|
return new PostDuplicateAbility($schema, $cloner, $log);
|
|
}
|
|
|
|
public function testCreatesADraftCopyAndReportsIt(): void
|
|
{
|
|
$cloner = Mockery::mock(PostCloner::class);
|
|
$cloner->expects('duplicate')->with($this->original)->andReturn(77);
|
|
Actions\expectDone('lit_mcp_after_save')->once()->with(77, 'lit/post-duplicate', ['post_id' => 5]);
|
|
$log = new FakeChangeLog(8);
|
|
|
|
$result = $this->ability($cloner, $log)->execute(['post_id' => 5]);
|
|
|
|
self::assertSame([['id' => '77', 'type' => 'page', 'action' => 'created', 'label' => 'Team (Kopie)']], $result['changed']);
|
|
self::assertSame(5, $result['original_id']);
|
|
self::assertSame(8, $result['log_id']);
|
|
self::assertSame(Rev::ofPost($this->copy), $result['rev']);
|
|
self::assertSame([], $result['warnings']);
|
|
$log->assertRecorded([
|
|
'ability' => 'lit/post-duplicate',
|
|
'object_type' => 'page',
|
|
'object_id' => '77',
|
|
'summary' => 'Kopie von "Team" (ID 5) angelegt',
|
|
'revision_id' => null,
|
|
]);
|
|
}
|
|
|
|
public function testNeedsTheRightToCreateEntriesOfThatType(): void
|
|
{
|
|
Functions\when('current_user_can')->alias(static fn (string $cap): bool => $cap !== 'edit_pages');
|
|
$cloner = Mockery::mock(PostCloner::class);
|
|
$cloner->expects('duplicate')->never();
|
|
|
|
$result = $this->ability($cloner, FakeChangeLog::forbidden())->execute(['post_id' => 5]);
|
|
|
|
self::assertToolError($result, 'forbidden');
|
|
}
|
|
|
|
public function testATrashedOriginalIsRefused(): void
|
|
{
|
|
$this->original->post_status = 'trash';
|
|
$cloner = Mockery::mock(PostCloner::class);
|
|
$cloner->expects('duplicate')->never();
|
|
|
|
$result = $this->ability($cloner, FakeChangeLog::forbidden())->execute(['post_id' => 5]);
|
|
|
|
self::assertToolError($result, 'validation_failed');
|
|
}
|
|
|
|
public function testIsIndirectAndNotIdempotent(): void
|
|
{
|
|
$ability = $this->ability(Mockery::mock(PostCloner::class), FakeChangeLog::forbidden());
|
|
|
|
self::assertFalse($ability->isDirect());
|
|
self::assertFalse($ability->annotations()['idempotent']);
|
|
self::assertFalse($ability->annotations()['destructive']);
|
|
}
|
|
}
|