Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
83 lines
3.6 KiB
PHP
83 lines
3.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace LIT\MCP\Tests\Unit\Integrations\Divi\Design;
|
|
|
|
use InvalidArgumentException;
|
|
use LIT\MCP\Integrations\Divi\Design\ReferenceUsageQuery;
|
|
use LIT\MCP\Tests\Unit\TestCase;
|
|
use Mockery;
|
|
|
|
final class ReferenceUsageQueryTest extends TestCase
|
|
{
|
|
/** A color reference as post_content holds it: quotes JSON-escaped (formats §2). */
|
|
private const COLOR_REF = '$variable({"type":"color","value":{"name":'
|
|
. '"gcid-holz000001","settings":{}}})$';
|
|
|
|
public function testCandidatesContainAnIdAnEscapedHyphenOrAnEscapedLetter(): 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 OR post_content REGEXP %s)',
|
|
'%gcid-holz000001%',
|
|
'%gvid-radius0001%',
|
|
'g[cv]id\\\\u002[dD]',
|
|
'\\\\u00[4-7][0-9a-fA-F]'
|
|
)
|
|
->andReturn('(prepared)');
|
|
|
|
$query = new ReferenceUsageQuery(['gcid-holz000001', 'gvid-radius0001', 'gcid-holz000001']);
|
|
|
|
self::assertSame('(prepared)', $query->candidateCondition($db));
|
|
}
|
|
|
|
public function testNeedsAnId(): void
|
|
{
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
new ReferenceUsageQuery(['']);
|
|
}
|
|
|
|
public function testFindsAReferenceInTheAttributesOfAnyBlock(): void
|
|
{
|
|
$query = new ReferenceUsageQuery(['gcid-holz000001', 'gvid-radius0001']);
|
|
|
|
self::assertTrue($query->matches('<!-- wp:divi/text {"module":{"decoration":{"background":{"desktop":'
|
|
. '{"value":{"color":"' . self::COLOR_REF . '"}}}}}} /-->'));
|
|
self::assertTrue($query->matches('<!-- wp:divi/text {"x":"gvid-radius0001"} /-->'));
|
|
self::assertFalse($query->matches('<!-- wp:divi/text {"module":{"decoration":{"background":{"desktop":'
|
|
. '{"value":{"color":"#ffffff"}}}}}} /-->'));
|
|
self::assertFalse($query->matches('<!-- wp:divi/text {"x":"gcid-anders0001"} /-->'));
|
|
self::assertFalse($query->matches('<p>Keine Verweise</p>'));
|
|
}
|
|
|
|
public function testFindsAnIdWrittenWithJsonEscapesAlsoInsideGlobalModules(): void
|
|
{
|
|
$query = new ReferenceUsageQuery(['gcid-holz000001']);
|
|
|
|
self::assertTrue($query->matches('<!-- wp:divi/text {"x":"gcid-holz000001"} /-->'));
|
|
self::assertTrue($query->matches('<!-- wp:divi/section {"globalModule":"12"} -->'
|
|
. '<!-- wp:divi/text {"x":"gcid-holz000001"} /--><!-- /wp:divi/section -->'));
|
|
self::assertFalse($query->matches('<!-- wp:divi/text {"x":"gcid-anders0001"} /-->'));
|
|
}
|
|
|
|
public function testUnreadableMarkupAndDivi4ContentCount(): void
|
|
{
|
|
$query = new ReferenceUsageQuery(['gcid-holz000001']);
|
|
|
|
self::assertTrue($query->matches('<!-- wp:divi/section --><!-- wp:divi/text {"x":1} /-->'), 'never closed');
|
|
self::assertTrue($query->matches('<!-- wp:divi/text {"x":"gcid-holz000001",} /-->'), 'attrs no JSON');
|
|
self::assertTrue($query->matches('[et_pb_text background_color="gcid-holz000001"]x[/et_pb_text]'), 'Divi 4');
|
|
}
|
|
|
|
public function testMentionsLooksIntoNestedStrings(): void
|
|
{
|
|
self::assertTrue(ReferenceUsageQuery::mentions(['a' => ['b' => 'x gvid-radius0001 y']], ['gvid-radius0001']));
|
|
self::assertFalse(ReferenceUsageQuery::mentions(['gvid-radius0001' => 1], ['gvid-radius0001']), 'keys');
|
|
self::assertFalse(ReferenceUsageQuery::mentions(12, ['gvid-radius0001']));
|
|
}
|
|
}
|