tests/Support/AssertsToolErrors (used by TestCase): assertToolError($result, code)
checks the WP_Error code lit_<code> and the code of the client JSON and returns the
payload; clientError() decodes the payload; errorFields() returns the fields and
asserts they exist. Replaces the private clientError() decoders of
DiviWriteAbilitiesTest/DiviReplaceTextAbilityTest, 216 single-line
assertSame('lit_…', …->get_error_code()) assertions, 24 get_error_data()['fields']
digs and two json_decode(message) reads. ErrorsTest keeps json_decode: it tests
the format itself. Tests 1404 -> 1409 (AssertsToolErrorsTest); no test has fewer
assertions.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
89 lines
3.6 KiB
PHP
89 lines
3.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace LIT\MCP\Tests\Unit\Integrations\Divi\Schema;
|
|
|
|
use Brain\Monkey\Functions;
|
|
use LIT\MCP\Integrations\Divi\Schema\DiviCatalogAbility;
|
|
use LIT\MCP\Integrations\Divi\Schema\DiviModules;
|
|
use LIT\MCP\Tests\Support\DiviFixtures;
|
|
use LIT\MCP\Tests\Unit\TestCase;
|
|
use Mockery;
|
|
|
|
final class DiviCatalogAbilityTest extends TestCase
|
|
{
|
|
private function ability(): DiviCatalogAbility
|
|
{
|
|
$modules = Mockery::mock(DiviModules::class);
|
|
$modules->allows('catalog')->andReturn(DiviFixtures::catalog());
|
|
|
|
return new DiviCatalogAbility($modules);
|
|
}
|
|
|
|
public function testListsModulesWithShortformFlag(): void
|
|
{
|
|
$result = $this->ability()->execute([]);
|
|
|
|
$button = array_values(array_filter($result['modules'], static fn (array $m): bool => $m['name'] === 'divi/button'));
|
|
self::assertSame(
|
|
[['name' => 'divi/button', 'title' => 'Button', 'category' => 'module', 'children' => [], 'shortform' => true]],
|
|
$button
|
|
);
|
|
$header = array_values(array_filter(
|
|
$result['modules'],
|
|
static fn (array $m): bool => $m['name'] === 'divi/fullwidth-header'
|
|
));
|
|
self::assertFalse($header[0]['shortform']);
|
|
self::assertSame(DiviCatalogAbility::CODE_NOTE, $result['notes'][0]);
|
|
self::assertStringContainsString('"divi-shortform"', $result['notes'][1]);
|
|
self::assertStringContainsString('"divi-attrs"', $result['notes'][1]);
|
|
}
|
|
|
|
public function testDetailOfOneModule(): void
|
|
{
|
|
$result = $this->ability()->execute(['module' => 'Accordion']);
|
|
|
|
self::assertSame('divi/accordion', $result['module']['name']);
|
|
self::assertSame(['divi/accordion-item'], $result['module']['children']);
|
|
self::assertSame('accordion-item', $result['module']['item_type']);
|
|
self::assertContains('title.decoration.font.font', $result['module']['paths']);
|
|
|
|
$button = $this->ability()->execute(['module' => 'divi/button'])['module'];
|
|
self::assertSame('linkUrl', $button['shortform']['url']['sub']);
|
|
}
|
|
|
|
public function testDetailExposesTheAttributePathKindsForSanitizationRouting(): void
|
|
{
|
|
$button = $this->ability()->execute(['module' => 'divi/button'])['module'];
|
|
|
|
self::assertSame('text', $button['path_kinds']['module.meta.adminLabel']);
|
|
self::assertArrayNotHasKey('no.such.path', $button['path_kinds']);
|
|
// Composite ("group") paths are left out rather than guessing a single kind.
|
|
self::assertArrayNotHasKey('button.innerContent', $button['path_kinds']);
|
|
}
|
|
|
|
/** A composite path (several named sub-fields under one attribute path) surfaces as a
|
|
* subName => kind map, not a single guessed string (spec §5.6, ruling F1 #1). */
|
|
public function testDetailExposesCompositePathKindsAsASubNameMap(): void
|
|
{
|
|
$header = $this->ability()->execute(['module' => 'divi/fullwidth-header'])['module'];
|
|
|
|
self::assertSame(
|
|
['text' => 'text', 'linkUrl' => 'url'],
|
|
$header['path_kinds']['buttonOne.innerContent']
|
|
);
|
|
self::assertSame(['src' => 'image'], $header['path_kinds']['image.innerContent']);
|
|
}
|
|
|
|
public function testUnknownModuleAndPermission(): void
|
|
{
|
|
self::assertToolError($this->ability()->execute(['module' => 'slider-pro']), 'not_found');
|
|
|
|
Functions\when('current_user_can')->justReturn(false);
|
|
self::assertToolError($this->ability()->permission([]), 'forbidden');
|
|
self::assertTrue($this->ability()->annotations()['readonly']);
|
|
self::assertTrue($this->ability()->isDirect());
|
|
}
|
|
}
|