refactor: GlobalUsages sucht über UsageQuery – Bibliothekssuche als LibraryUsageQuery

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Jan-Niclas Loosen
2026-10-16 17:36:21 +02:00
co-authored by Claude Sonnet 5.5
parent 3fe83e39d3
commit 454116851e
5 changed files with 171 additions and 43 deletions
@@ -282,4 +282,34 @@ final class GlobalUsagesTest extends TestCase
self::assertFalse($report->blocks());
self::assertSame(['affected', 'affected_count'], array_keys($report->listing()));
}
public function testRowsForUsesTheConditionAndMatcherOfTheQuery(): void
{
$db = Mockery::mock(\wpdb::class)->makePartial();
$queries = [];
$db->allows('get_results')->andReturnUsing(static function (string $sql) use (&$queries): array {
$queries[] = $sql;
return str_contains($sql, 'post_content FROM')
? [['ID' => '5', 'post_content' => 'treffer'], ['ID' => '6', 'post_content' => 'nichts']]
: [['ID' => '5', 'post_status' => 'draft', 'post_type' => 'page'],
['ID' => '6', 'post_status' => 'draft', 'post_type' => 'page']];
});
$query = new class implements \LIT\MCP\Integrations\Divi\Library\UsageQuery {
public function candidateCondition(\wpdb $db): string
{
return '(MARKER)';
}
public function matches(string $content): bool
{
return $content === 'treffer';
}
};
$rows = (new GlobalUsages($db))->rowsFor($query);
self::assertSame([['id' => 5, 'status' => 'draft', 'type' => 'page']], $rows);
self::assertStringContainsString('WHERE (MARKER) AND post_type', $queries[0]);
}
}
@@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace LIT\MCP\Tests\Unit\Integrations\Divi\Library;
use LIT\MCP\Integrations\Divi\Library\LibraryUsageQuery;
use LIT\MCP\Tests\Unit\TestCase;
use Mockery;
final class LibraryUsageQueryTest extends TestCase
{
public function testConditionIsTheFormerCandidateFilter(): void
{
$db = Mockery::mock(\wpdb::class);
$db->allows('esc_like')->andReturnUsing(static fn (string $text): string => $text);
$db->expects('prepare')
->with(
'(LOWER(post_content) LIKE %s OR LOWER(post_content) LIKE %s OR post_content REGEXP %s)',
'%globalmodule%',
'%global_module%',
'\\\\u00[4-7][0-9a-fA-F]'
)
->andReturn('(prepared)');
self::assertSame('(prepared)', (new LibraryUsageQuery(12))->candidateCondition($db));
}
public function testMatchesThroughTheUsageMatcher(): void
{
$query = new LibraryUsageQuery(12);
self::assertTrue($query->matches('<!-- wp:divi/section {"globalModule":"12"} /-->'));
self::assertFalse($query->matches('<!-- wp:divi/section {"globalModule":"13"} /-->'));
}
}