lit/media-preview downscales an image attachment via wp_get_image_editor and returns it as ImageContent; lit/media-download returns a file (full or a named size) as an embedded resource, rejecting files above the upload limit. Both are direct tools (ruling Q6: embedded resources, like ImageContent, only become content blocks for tools called directly). Adds MediaFiles::isWithinUploads() so neither tool ever reads a file outside wp_upload_dir()'s basedir. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01At3V6SjwbMe6eMzVGW3Hs7
93 lines
3.2 KiB
PHP
93 lines
3.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace LIT\MCP\Tests\Unit\Integrations\Media;
|
|
|
|
use Brain\Monkey\Functions;
|
|
use LIT\MCP\Integrations\Media\MediaPreviewAbility;
|
|
use LIT\MCP\Tests\Unit\TestCase;
|
|
use Mockery;
|
|
use WP_Post;
|
|
|
|
final class MediaPreviewAbilityTest extends TestCase
|
|
{
|
|
private string $source = '';
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
$this->source = (string) tempnam(sys_get_temp_dir(), 'lit-src');
|
|
file_put_contents($this->source, 'original');
|
|
Functions\when('get_post')->justReturn(new WP_Post([
|
|
'ID' => 33,
|
|
'post_type' => 'attachment',
|
|
'post_mime_type' => 'image/jpeg',
|
|
]));
|
|
Functions\when('current_user_can')->justReturn(true);
|
|
Functions\when('get_attached_file')->justReturn($this->source);
|
|
Functions\when('wp_upload_dir')->justReturn(['basedir' => sys_get_temp_dir()]);
|
|
Functions\when('wp_tempnam')->alias(static fn (): string => (string) tempnam(sys_get_temp_dir(), 'lit-prev'));
|
|
Functions\when('wp_delete_file')->alias(static function (string $file): void {
|
|
if (is_file($file)) {
|
|
unlink($file);
|
|
}
|
|
});
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
if (is_file($this->source)) {
|
|
unlink($this->source);
|
|
}
|
|
parent::tearDown();
|
|
}
|
|
|
|
public function testReturnsADownscaledImageResult(): void
|
|
{
|
|
Functions\when('wp_attachment_is_image')->justReturn(true);
|
|
$editor = Mockery::mock();
|
|
$editor->allows('get_size')->andReturn(['width' => 2000, 'height' => 1000]);
|
|
$editor->expects('resize')->with(256, 256, false)->andReturn(true);
|
|
$editor->expects('save')->andReturnUsing(static function (string $path, string $mime): array {
|
|
file_put_contents($path, 'JPEGDATA');
|
|
return ['path' => $path, 'mime-type' => $mime];
|
|
});
|
|
Functions\when('wp_get_image_editor')->justReturn($editor);
|
|
|
|
$result = (new MediaPreviewAbility())->execute(['id' => 33, 'max_size' => 256]);
|
|
|
|
self::assertSame('image', $result['type']);
|
|
self::assertSame('JPEGDATA', $result['results']);
|
|
self::assertSame('image/jpeg', $result['mimeType']);
|
|
}
|
|
|
|
public function testNonImagesAreUnsupported(): void
|
|
{
|
|
Functions\when('wp_attachment_is_image')->justReturn(false);
|
|
|
|
$result = (new MediaPreviewAbility())->execute(['id' => 33]);
|
|
|
|
self::assertSame('lit_unsupported', $result->get_error_code());
|
|
}
|
|
|
|
public function testFilesOutsideTheUploadsDirectoryAreRefused(): void
|
|
{
|
|
Functions\when('wp_attachment_is_image')->justReturn(true);
|
|
Functions\when('wp_upload_dir')->justReturn(['basedir' => sys_get_temp_dir() . '/lit-elsewhere']);
|
|
|
|
$result = (new MediaPreviewAbility())->execute(['id' => 33]);
|
|
|
|
self::assertSame('lit_not_found', $result->get_error_code());
|
|
}
|
|
|
|
public function testIsADirectToolWithoutOutputSchema(): void
|
|
{
|
|
$ability = new MediaPreviewAbility();
|
|
|
|
self::assertNull($ability->outputSchema());
|
|
self::assertTrue($ability->isDirect());
|
|
self::assertTrue($ability->annotations()['readonly']);
|
|
}
|
|
}
|