Files
WP-LIT-MCP/tests/Unit/Integrations/Media/MediaGetAbilityTest.php
T
janniclas.loosenandClaude Opus 5.5 60290c85a5 test: AssertsToolErrors helper for tool error assertions (T3)
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>
2026-09-26 17:55:35 +02:00

62 lines
2.6 KiB
PHP

<?php
declare(strict_types=1);
namespace LIT\MCP\Tests\Unit\Integrations\Media;
use Brain\Monkey\Functions;
use LIT\MCP\Integrations\Media\MediaGetAbility;
use LIT\MCP\Integrations\Media\MediaPresenter;
use LIT\MCP\Integrations\Media\MediaUsageFinder;
use LIT\MCP\Tests\Unit\TestCase;
use Mockery;
use WP_Post;
final class MediaGetAbilityTest extends TestCase
{
public function testReturnsMetadataWithUsages(): void
{
$attachment = new WP_Post(['ID' => 33, 'post_type' => 'attachment', 'post_status' => 'inherit']);
Functions\when('get_post')->justReturn($attachment);
Functions\when('current_user_can')->justReturn(true);
$finder = Mockery::mock(MediaUsageFinder::class);
$usages = [['post_id' => 5, 'title' => 'Team', 'post_type' => 'page', 'where' => 'featured', 'certainty' => 'sicher']];
$finder->expects('scan')->with(33)->andReturn(['usages' => $usages, 'complete' => false, 'truncated' => true]);
$presenter = Mockery::mock(MediaPresenter::class);
$presenter->expects('full')->with($attachment, $usages)->andReturn(['id' => 33, 'usages' => $usages]);
self::assertSame(
['id' => 33, 'usages' => $usages, 'usages_complete' => false, 'usages_truncated' => true],
(new MediaGetAbility($presenter, $finder))->execute(['id' => 33])
);
}
public function testTrashedAttachmentsCanBeRead(): void
{
Functions\when('get_post')->justReturn(new WP_Post(['ID' => 33, 'post_type' => 'attachment', 'post_status' => 'trash']));
Functions\when('current_user_can')->justReturn(true);
$ability = new MediaGetAbility(Mockery::mock(MediaPresenter::class), Mockery::mock(MediaUsageFinder::class));
self::assertTrue($ability->permission(['id' => 33]));
}
public function testNonAttachmentIsNotFound(): void
{
Functions\when('get_post')->justReturn(new WP_Post(['ID' => 5, 'post_type' => 'page']));
$ability = new MediaGetAbility(Mockery::mock(MediaPresenter::class), Mockery::mock(MediaUsageFinder::class));
self::assertToolError($ability->permission(['id' => 5]), 'not_found');
}
public function testIsIndirectAndReadonly(): void
{
$ability = new MediaGetAbility(Mockery::mock(MediaPresenter::class), Mockery::mock(MediaUsageFinder::class));
self::assertSame('lit/media-get', $ability->name());
self::assertFalse($ability->isDirect());
self::assertTrue($ability->annotations()['readonly']);
self::assertContains('usages_complete', $ability->outputSchema()['required']);
self::assertContains('usages_truncated', $ability->outputSchema()['required']);
}
}