Files
WP-LIT-MCP/tests/Unit/Integrations/Media/MediaPreviewAbilityTest.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

97 lines
3.3 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::assertToolError($result, 'unsupported');
self::assertStringContainsString(
'lit/media-download (direkt: lit-media-download)',
(string) $result->get_error_data()['hint']
);
}
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::assertToolError($result, 'not_found');
}
public function testIsADirectToolWithoutOutputSchema(): void
{
$ability = new MediaPreviewAbility();
self::assertNull($ability->outputSchema());
self::assertTrue($ability->isDirect());
self::assertTrue($ability->annotations()['readonly']);
}
}