feat: media usage finder and media-get tool
Usage search distinguishes certain (ID referenced) and possible (file URL only) usages, hides titles the user cannot read, and keeps queries bounded. MediaGuard checks per-attachment capabilities and refuses trashed media unless the caller opts in. 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:
@@ -0,0 +1,55 @@
|
||||
<?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);
|
||||
$finder->expects('usages')->with(33)->andReturn([]);
|
||||
$presenter = Mockery::mock(MediaPresenter::class);
|
||||
$presenter->expects('full')->with($attachment, [])->andReturn(['id' => 33]);
|
||||
|
||||
self::assertSame(['id' => 33], (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::assertSame('lit_not_found', $ability->permission(['id' => 5])->get_error_code());
|
||||
}
|
||||
|
||||
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']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user