Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
83 lines
3.5 KiB
PHP
83 lines
3.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace LIT\MCP\Tests\Unit\Integrations\Divi\Design;
|
|
|
|
use Brain\Monkey\Functions;
|
|
use LIT\MCP\Integrations\Divi\Design\DesignSystemWriter;
|
|
use LIT\MCP\Integrations\Divi\Design\DiviPresetGetAbility;
|
|
use LIT\MCP\Integrations\Divi\Design\PresetBook;
|
|
use LIT\MCP\Integrations\Divi\Design\PresetUsageQuery;
|
|
use LIT\MCP\Integrations\Divi\Library\GlobalUsages;
|
|
use LIT\MCP\Integrations\Divi\Library\UsageReport;
|
|
use LIT\MCP\Tests\Unit\TestCase;
|
|
use Mockery;
|
|
|
|
final class DiviPresetGetAbilityTest extends TestCase
|
|
{
|
|
private function ability(?GlobalUsages $usages = null): DiviPresetGetAbility
|
|
{
|
|
$writer = Mockery::mock(DesignSystemWriter::class);
|
|
$writer->allows('presets')->andReturn(PresetBookTest::stored());
|
|
$usages ??= Mockery::mock(GlobalUsages::class);
|
|
|
|
return new DiviPresetGetAbility($writer, $usages);
|
|
}
|
|
|
|
public function testReadsAPresetWithItsUsage(): void
|
|
{
|
|
Functions\when('current_user_can')->justReturn(true);
|
|
$usages = Mockery::mock(GlobalUsages::class);
|
|
$usages->expects('reportFor')
|
|
->with(Mockery::type(PresetUsageQuery::class), false)
|
|
->andReturn(new UsageReport(1, [['post_id' => 3, 'title' => 'Start', 'status' => 'publish',
|
|
'type' => 'page']], 0, 1, [], 0, false));
|
|
$ability = $this->ability($usages);
|
|
|
|
self::assertTrue($ability->permission(['module' => 'text', 'preset' => 'Fließtext']));
|
|
$result = $ability->execute(['module' => 'text', 'preset' => 'Fließtext']);
|
|
|
|
self::assertSame(['dddddddddd', 'Fließtext', 'module', 'divi/text', true], [$result['id'], $result['name'],
|
|
$result['kind'], $result['scope'], $result['default']]);
|
|
self::assertSame([[
|
|
'group_id' => 'content.decoration.bodyFont',
|
|
'group_name' => 'divi/font-body',
|
|
'presets' => [['id' => 'gggggggggg', 'name' => 'Lesetext']],
|
|
]], $result['group_presets']);
|
|
self::assertSame(3, $result['usage']['affected'][0]['post_id']);
|
|
self::assertSame(PresetBook::fromDivi(PresetBookTest::stored())->rev(), $result['design_rev']);
|
|
self::assertFalse($ability->isDirect());
|
|
self::assertTrue($ability->annotations()['readonly']);
|
|
}
|
|
|
|
public function testFlattensAttrsLikeNodeGet(): void
|
|
{
|
|
Functions\when('current_user_can')->justReturn(true);
|
|
$usages = Mockery::mock(GlobalUsages::class);
|
|
$usages->allows('reportFor')->andReturn(new UsageReport(0, [], 0, 0, [], 0, false));
|
|
|
|
$result = $this->ability($usages)->execute(['module' => 'button', 'preset' => 'aaaaaaaaaa']);
|
|
|
|
self::assertSame(['button.x' => 1], (array) $result['attrs']);
|
|
}
|
|
|
|
public function testUnknownAndAmbiguousPresets(): void
|
|
{
|
|
Functions\when('current_user_can')->justReturn(true);
|
|
|
|
self::assertToolError($this->ability()->execute(['module' => 'button', 'preset' => 'Fehlt']), 'not_found');
|
|
$ambiguous = self::assertToolError($this->ability()->execute(['module' => 'button', 'preset' => 'Doppelt']),
|
|
'validation_failed');
|
|
self::assertStringContainsString('bbbbbbbbbb, cccccccccc', $ambiguous['fields']['preset']);
|
|
self::assertToolError($this->ability()->execute(['preset' => 'x']), 'validation_failed');
|
|
}
|
|
|
|
public function testNeedsEditPosts(): void
|
|
{
|
|
Functions\when('current_user_can')->justReturn(false);
|
|
|
|
self::assertToolError($this->ability()->permission([]), 'forbidden');
|
|
}
|
|
}
|