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

98 lines
3.6 KiB
PHP

<?php
declare(strict_types=1);
namespace LIT\MCP\Tests\Unit\Integrations\Media;
use Brain\Monkey\Functions;
use LIT\MCP\Integrations\Media\MediaDownloadAbility;
use LIT\MCP\Limits;
use LIT\MCP\Tests\Unit\TestCase;
use WP_Post;
final class MediaDownloadAbilityTest extends TestCase
{
private string $file = '';
protected function setUp(): void
{
parent::setUp();
$this->file = (string) tempnam(sys_get_temp_dir(), 'lit-download');
file_put_contents($this->file, 'PDFDATA');
Functions\when('get_post')->justReturn(new WP_Post([
'ID' => 40,
'post_type' => 'attachment',
'post_mime_type' => 'application/pdf',
]));
Functions\when('current_user_can')->justReturn(true);
Functions\when('get_attached_file')->justReturn($this->file);
Functions\when('wp_get_attachment_url')->justReturn('https://kunde.de/wp-content/uploads/preise.pdf');
Functions\when('wp_basename')->alias('basename');
Functions\when('wp_check_filetype')->justReturn(['ext' => 'pdf', 'type' => 'application/pdf']);
Functions\when('wp_upload_dir')->justReturn(['basedir' => sys_get_temp_dir()]);
}
protected function tearDown(): void
{
if (is_file($this->file)) {
unlink($this->file);
}
parent::tearDown();
}
public function testReturnsTheFileAsEmbeddedBlobResource(): void
{
Functions\when('get_option')->justReturn([]);
$result = (new MediaDownloadAbility(new Limits()))->execute(['id' => 40]);
self::assertSame('resource', $result['type']);
self::assertSame('https://kunde.de/wp-content/uploads/preise.pdf', $result['resource']['uri']);
self::assertSame('application/pdf', $result['resource']['mimeType']);
self::assertSame(base64_encode('PDFDATA'), $result['resource']['blob']);
}
public function testFilesAboveTheLimitAreRefused(): void
{
Functions\when('get_option')->justReturn(['upload_mb' => 1]);
file_put_contents($this->file, str_repeat('x', 1024 * 1024 + 1));
$result = (new MediaDownloadAbility(new Limits()))->execute(['id' => 40]);
self::assertToolError($result, 'unsupported');
}
public function testDownloadsAreCappedAtTenMegabytesEvenWithAHigherUploadLimit(): void
{
Functions\when('get_option')->justReturn(['upload_mb' => 32]);
file_put_contents($this->file, str_repeat('x', 10 * 1024 * 1024 + 1));
$result = (new MediaDownloadAbility(new Limits()))->execute(['id' => 40]);
self::assertToolError($result, 'unsupported');
self::assertStringContainsString('Limit 10 MB', $result->get_error_message());
self::assertStringContainsString('URL', (string) $result->get_error_data()['hint']);
}
public function testUnknownSizeIsNotFound(): void
{
Functions\when('get_option')->justReturn([]);
Functions\when('image_get_intermediate_size')->justReturn(false);
$result = (new MediaDownloadAbility(new Limits()))->execute(['id' => 40, 'size' => 'huge']);
self::assertToolError($result, 'not_found');
self::assertNull((new MediaDownloadAbility(new Limits()))->outputSchema());
}
public function testFilesOutsideTheUploadsDirectoryAreRefused(): void
{
Functions\when('get_option')->justReturn([]);
Functions\when('wp_upload_dir')->justReturn(['basedir' => sys_get_temp_dir() . '/lit-elsewhere']);
$result = (new MediaDownloadAbility(new Limits()))->execute(['id' => 40]);
self::assertToolError($result, 'not_found');
}
}