Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
89 lines
4.2 KiB
PHP
89 lines
4.2 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({\u0022type\u0022:\u0022color\u0022,\u0022value\u0022:{\u0022name\u0022:'
|
|
. '\u0022gcid-holz000001\u0022,\u0022settings\u0022:{}}})$';
|
|
|
|
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\u002dholz000001"} /-->'));
|
|
self::assertTrue($query->matches('<!-- wp:divi/section {"globalModule":"12"} -->'
|
|
. '<!-- wp:divi/text {"x":"gcid\u002dholz000001"} /--><!-- /wp:divi/section -->'));
|
|
self::assertFalse($query->matches('<!-- wp:divi/text {"x":"gcid\u002danders0001"} /-->'));
|
|
// Escaped letter, nested two levels deep inside a global module: only decoded attributes reveal the ID.
|
|
$nested = '<!-- wp:divi/section {"globalModule":"12"} --><!-- wp:divi/row --><!-- wp:divi/text '
|
|
. '{"x":"\u0067cid-holz000001"} /--><!-- /wp:divi/row --><!-- /wp:divi/section -->';
|
|
self::assertStringNotContainsString('gcid-holz000001', $nested);
|
|
self::assertTrue($query->matches($nested));
|
|
}
|
|
|
|
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\u002dholz000001",} /-->'), 'attrs no JSON');
|
|
self::assertTrue($query->matches('<!-- wp:divi/text {"x":"\u0067cid-holz000001",} /-->'), 'no JSON, no raw ID');
|
|
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']));
|
|
}
|
|
}
|