feat: Fundstellen aller Module eines Typs, Standard-Gruppen-Presets in der Fundstellensuche (ModuleUsageQuery)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Jan-Niclas Loosen
2026-10-18 16:55:54 +02:00
co-authored by Claude Sonnet 5.5
parent 1f9b50f1a6
commit 763d765660
11 changed files with 329 additions and 23 deletions
@@ -0,0 +1,82 @@
<?php
declare(strict_types=1);
namespace LIT\MCP\Tests\Unit\Integrations\Divi\Design;
use InvalidArgumentException;
use LIT\MCP\Integrations\Divi\Design\ModuleUsageQuery;
use LIT\MCP\Tests\Unit\TestCase;
use Mockery;
final class ModuleUsageQueryTest extends TestCase
{
private static function page(string $inner): string
{
return '<!-- wp:divi/section --><!-- wp:divi/row --><!-- wp:divi/column -->' . $inner
. '<!-- /wp:divi/column --><!-- /wp:divi/row --><!-- /wp:divi/section -->';
}
public function testCandidatesContainTheNameAnEscapedSlashOrAnEscapedLetter(): void
{
$db = Mockery::mock(\wpdb::class);
$db->allows('esc_like')->andReturnUsing(static fn (string $text): string => $text);
$db->expects('prepare')
->with(
'(post_content LIKE %s OR post_content REGEXP %s OR post_content REGEXP %s)',
'%divi/button%',
'divi(\\\\/|\\\\u002[fF])button',
'\\\\u00[4-7][0-9a-fA-F]'
)
->andReturn('(prepared)');
self::assertSame('(prepared)', (new ModuleUsageQuery('divi/button'))->candidateCondition($db));
}
public function testNeedsAFullBlockName(): void
{
$this->expectException(InvalidArgumentException::class);
new ModuleUsageQuery('button');
}
public function testMatchesBlocksOfTheModule(): void
{
$query = new ModuleUsageQuery('divi/button');
self::assertTrue($query->matches(self::page('<!-- wp:divi/button {"a":1} /-->')));
self::assertTrue($query->matches(self::page('<!-- wp:divi/button /-->')));
self::assertTrue($query->matches(self::page(
'<!-- wp:divi/button {"a":1} --><!-- wp:divi/text /--><!-- /wp:divi/button -->'
)));
self::assertFalse($query->matches(self::page('<!-- wp:divi/text {"a":1} /-->')));
self::assertFalse($query->matches(self::page('<!-- wp:divi/button-group /-->')), 'another module name');
self::assertFalse($query->matches('<p>Kein Divi-Inhalt</p>'));
}
public function testGlobalInstancesOfTheModuleCountSoThatTheyAreReportedAsSkipped(): void
{
$query = new ModuleUsageQuery('divi/button');
self::assertTrue($query->matches(self::page(
'<!-- wp:divi/global-layout {"globalModule":"1058","blockName":"divi/button"} /-->'
)));
self::assertTrue($query->matches(self::page(
'<!-- wp:divi/global-layout {"globalModule":"1058","blockName":"divi\/button"} /-->'
)), 'an escaped slash decodes to the same name');
self::assertFalse($query->matches(self::page(
'<!-- wp:divi/global-layout {"globalModule":"1058","blockName":"divi/text"} /-->'
)));
self::assertTrue($query->matches(self::page(
'<!-- wp:divi/button {"globalModule":"1058"} /-->'
)), 'Divi 4 conversion: the block itself carries globalModule');
self::assertTrue($query->matches(self::page('<!-- wp:divi/button {"a":1,} /-->')), 'unreadable attributes');
}
public function testBrokenMarkupCountsAsUse(): void
{
self::assertTrue((new ModuleUsageQuery('divi/button'))->matches(
'<!-- wp:divi/section {"a": --><!-- /wp:divi/row -->'
));
}
}