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>
65 lines
2.4 KiB
PHP
65 lines
2.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace LIT\MCP\Tests\Unit\Integrations\WordPress\Menus;
|
|
|
|
use Brain\Monkey\Functions;
|
|
use LIT\MCP\Abilities\Rev;
|
|
use LIT\MCP\Integrations\WordPress\Menus\MenuGetAbility;
|
|
use LIT\MCP\Integrations\WordPress\Menus\MenuListAbility;
|
|
use LIT\MCP\Tests\Unit\TestCase;
|
|
|
|
final class MenuReadAbilitiesTest extends TestCase
|
|
{
|
|
use MenuFixtures;
|
|
|
|
public function testListShowsMenusAndLocations(): void
|
|
{
|
|
Functions\when('wp_get_nav_menus')->justReturn([
|
|
(object) ['term_id' => 3, 'name' => 'Hauptmenü', 'slug' => 'hauptmenue', 'count' => 4],
|
|
]);
|
|
Functions\when('get_registered_nav_menus')->justReturn(
|
|
['primary-menu' => 'Hauptmenü', 'footer-menu' => 'Footer']
|
|
);
|
|
Functions\when('get_nav_menu_locations')->justReturn(['primary-menu' => 3]);
|
|
|
|
$result = (new MenuListAbility())->execute([]);
|
|
|
|
self::assertSame(['primary-menu'], $result['menus'][0]['locations']);
|
|
self::assertSame(
|
|
[
|
|
['location' => 'primary-menu', 'description' => 'Hauptmenü', 'menu_id' => 3],
|
|
['location' => 'footer-menu', 'description' => 'Footer', 'menu_id' => null],
|
|
],
|
|
$result['locations']
|
|
);
|
|
}
|
|
|
|
public function testGetReturnsTheTreeOrNotFound(): void
|
|
{
|
|
Functions\when('wp_get_nav_menu_object')->alias(
|
|
static fn (int $id) => $id === 3 ? (object) ['term_id' => 3, 'name' => 'Hauptmenü'] : false
|
|
);
|
|
Functions\when('wp_get_nav_menu_items')->justReturn([self::item(10, 0, 'Start')]);
|
|
Functions\when('get_nav_menu_locations')->justReturn([]);
|
|
$ability = new MenuGetAbility();
|
|
|
|
$result = $ability->execute(['menu_id' => 3]);
|
|
|
|
self::assertSame('Hauptmenü', $result['name']);
|
|
self::assertSame('Start', $result['items'][0]['title']);
|
|
self::assertSame(Rev::ofMenu([self::item(10, 0, 'Start')]), $result['menu_rev']);
|
|
self::assertContains('menu_rev', $ability->outputSchema()['required']);
|
|
self::assertToolError($ability->execute(['menu_id' => 9]), 'not_found');
|
|
}
|
|
|
|
public function testReadToolsNeedThemeOptionRights(): void
|
|
{
|
|
Functions\when('current_user_can')->justReturn(false);
|
|
|
|
self::assertToolError((new MenuListAbility())->permission([]), 'forbidden');
|
|
self::assertToolError((new MenuGetAbility())->permission(['menu_id' => 3]), 'forbidden');
|
|
}
|
|
}
|