Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
99 lines
3.7 KiB
PHP
99 lines
3.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace LIT\MCP\Tests\Unit\Integrations\Divi\Design;
|
|
|
|
use LIT\MCP\Integrations\Divi\Design\PresetScope;
|
|
use LIT\MCP\Integrations\Divi\Design\PresetUsageQuery;
|
|
use LIT\MCP\Tests\Unit\TestCase;
|
|
use Mockery;
|
|
|
|
final class PresetUsageQueryTest extends TestCase
|
|
{
|
|
public function testCandidatesContainAnIdOrAJsonEscape(): 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 LIKE %s OR post_content REGEXP %s)',
|
|
'%aaaaaaaaaa%',
|
|
'%gggggggggg%',
|
|
'\\\\u00[4-7][0-9a-fA-F]'
|
|
)
|
|
->andReturn('(prepared)');
|
|
|
|
$query = new PresetUsageQuery([
|
|
['scope' => new PresetScope('module', 'divi/button'), 'id' => 'aaaaaaaaaa', 'default' => false],
|
|
['scope' => new PresetScope('group', 'divi/font-body'), 'id' => 'gggggggggg', 'default' => false],
|
|
]);
|
|
|
|
self::assertSame('(prepared)', $query->candidateCondition($db));
|
|
self::assertTrue($query->matches('<!-- wp:divi/text {"groupPreset":{"content.decoration.bodyFont":'
|
|
. '{"presetId":["gggggggggg"],"groupName":"divi/font-body"}}} /-->'));
|
|
self::assertFalse($query->matches('<!-- wp:divi/text {} /-->'));
|
|
}
|
|
|
|
public function testADefaultModulePresetLooksForTheModule(): 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 LIKE %s OR post_content REGEXP %s)',
|
|
'%dddddddddd%',
|
|
'%wp:divi/button %',
|
|
'\\\\u00[4-7][0-9a-fA-F]'
|
|
)
|
|
->andReturn('(prepared)');
|
|
|
|
$query = new PresetUsageQuery([
|
|
['scope' => new PresetScope('module', 'divi/button'), 'id' => 'dddddddddd', 'default' => true],
|
|
]);
|
|
|
|
self::assertSame('(prepared)', $query->candidateCondition($db));
|
|
self::assertTrue($query->matches('<!-- wp:divi/button {} /-->'));
|
|
}
|
|
|
|
public function testNeedsATarget(): void
|
|
{
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
new PresetUsageQuery([]);
|
|
}
|
|
|
|
public function testADefaultGroupPresetLooksForEveryDiviBlock(): 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 LIKE %s OR post_content REGEXP %s)',
|
|
'%gggggggggg%',
|
|
'%wp:divi/%',
|
|
'\\\\u00[4-7][0-9a-fA-F]'
|
|
)
|
|
->andReturn('(prepared)');
|
|
|
|
$query = new PresetUsageQuery([
|
|
['scope' => new PresetScope('group', 'divi/font-body'), 'id' => 'gggggggggg', 'default' => true],
|
|
]);
|
|
|
|
self::assertSame('(prepared)', $query->candidateCondition($db));
|
|
}
|
|
|
|
public function testWithGroupsDetectsTheImplicitUseOfADefaultGroupPreset(): void
|
|
{
|
|
$query = PresetUsageQuery::withGroups(
|
|
[['scope' => new PresetScope('group', 'divi/font-body'), 'id' => 'gggggggggg', 'default' => true]],
|
|
static fn (string $module): array => $module === 'divi/text'
|
|
? ['content.decoration.bodyFont' => 'divi/font-body']
|
|
: []
|
|
);
|
|
|
|
self::assertTrue($query->matches('<!-- wp:divi/text {} /-->'));
|
|
self::assertFalse($query->matches('<!-- wp:divi/button {} /-->'));
|
|
}
|
|
}
|