Files
WP-LIT-MCP/tests/Unit/Integrations/Media/ContentMediaReferencesTest.php
janniclas.loosenandClaude Opus 5.5 4cb18d17c3 refactor: ContentMediaReferences holds the pure media matcher (M8)
The block/ID/file-URL matching moves out of MediaUsageFinder into a static
class; the finder keeps queries, limits and result building. Behaviour
unchanged; direct tests for the matcher added.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01At3V6SjwbMe6eMzVGW3Hs7
2026-09-26 18:28:52 +02:00

51 lines
2.3 KiB
PHP

<?php
declare(strict_types=1);
namespace LIT\MCP\Tests\Unit\Integrations\Media;
use LIT\MCP\Integrations\Media\ContentMediaReferences;
use LIT\MCP\Integrations\Media\MediaUsageFinder;
use PHPUnit\Framework\TestCase;
final class ContentMediaReferencesTest extends TestCase
{
/** @return array<string, array{string, ?string}> */
public static function contents(): array
{
$certain = MediaUsageFinder::CERTAIN;
$possible = MediaUsageFinder::POSSIBLE;
return [
'wp-image class' => ['<img class="wp-image-33" src="x">', $certain],
'other wp-image id' => ['<img class="wp-image-334">', null],
'gallery shortcode' => ['[gallery ids="1, 33,5"]', $certain],
'divi 4 gallery_ids' => ['[et_pb_gallery gallery_ids="12,33"]', $certain],
'core image block' => ['<!-- wp:image {"id":33} /-->', $certain],
'core gallery ids' => ['<!-- wp:gallery {"ids":[4,33]} /-->', $certain],
'divi 5 image id' => ['<!-- wp:divi/image {"image":{"innerContent":{"desktop":{"value":{"id":"33"}}}}} /-->', $certain],
'divi 5 galleryIds' => ['<!-- wp:divi/gallery {"image":{"advanced":{"galleryIds":{"desktop":{"value":"7,33"}}}}} /-->', $certain],
'id outside a media block' => ['<!-- wp:paragraph {"id":33} /-->', null],
'file url only' => ['<img src="/wp-content/uploads/2026/09/foto-300x200.jpg">', $possible],
'json-escaped file url' => ['{"src":"\/uploads\/2026\/09\/foto.jpg"}', $possible],
'other file' => ['<img src="/uploads/2026/09/fotos.jpg">', null],
];
}
/** @dataProvider contents */
public function testCertaintyOfStoredContent(string $content, ?string $expected): void
{
$fileRegex = ContentMediaReferences::fileRegex('2026/09/foto');
self::assertSame($expected, ContentMediaReferences::certainty($content, 33, $fileRegex));
}
public function testListsAndLeaves(): void
{
self::assertTrue(ContentMediaReferences::inList(['1, 2', ' 33 '], 33));
self::assertFalse(ContentMediaReferences::inList(['1,333', 'x33'], 33));
self::assertSame(['1', '33', '7'], ContentMediaReferences::stringLeaves(['1', [33, ['7', null, 2.5]]]));
self::assertFalse(ContentMediaReferences::matches(null, 'anything'));
}
}