Files
WP-LIT-MCP/tests/Unit/Integrations/WordPress/Menus/MenuAssignLocationAbilityTest.php
T
janniclas.loosenandClaude Opus 5.5 60290c85a5 test: AssertsToolErrors helper for tool error assertions (T3)
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>
2026-09-26 17:55:35 +02:00

127 lines
4.7 KiB
PHP

<?php
declare(strict_types=1);
namespace LIT\MCP\Tests\Unit\Integrations\WordPress\Menus;
use Brain\Monkey\Functions;
use LIT\MCP\Integrations\WordPress\Menus\MenuAssignLocationAbility;
use LIT\MCP\Tests\Support\FakeChangeLog;
use LIT\MCP\Tests\Unit\TestCase;
final class MenuAssignLocationAbilityTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
Functions\when('get_registered_nav_menus')->justReturn(['primary-menu' => 'Hauptmenü']);
Functions\when('get_nav_menu_locations')->justReturn(['primary-menu' => 3]);
Functions\when('wp_get_nav_menu_object')->alias(
static fn (int $id) => match ($id) {
8 => (object) ['term_id' => 8, 'name' => 'Neu'],
3 => (object) ['term_id' => 3, 'name' => 'Hauptmenü'],
default => false,
}
);
}
public function testAssignsAndReportsThePreviousMenu(): void
{
Functions\expect('set_theme_mod')->once()->with('nav_menu_locations', ['primary-menu' => 8]);
$log = new FakeChangeLog(6);
$result = (new MenuAssignLocationAbility($log))->execute(['location' => 'primary-menu', 'menu_id' => 8]);
self::assertSame(3, $result['previous_menu_id']);
self::assertSame('assigned', $result['changed'][0]['action']);
self::assertSame(6, $result['log_id']);
$log->assertRecorded([
'ability' => 'lit/menu-assign-location',
'object_type' => 'menu_location',
'object_id' => 'primary-menu',
'summary' => 'Menü 8 der Position primary-menu zugewiesen',
]);
}
public function testRemovingAnAssignmentNeedsConfirmation(): void
{
Functions\expect('set_theme_mod')->never();
$result = (new MenuAssignLocationAbility(FakeChangeLog::forbidden()))
->execute(['location' => 'primary-menu', 'menu_id' => 0]);
self::assertToolError($result, 'confirm_required');
self::assertStringContainsString(
'"Hauptmenü" (ID 3)',
self::clientError($result)['message']
);
}
public function testZeroRemovesTheAssignmentAndNamesTheRemovedMenu(): void
{
Functions\expect('set_theme_mod')->once()->with('nav_menu_locations', []);
$log = new FakeChangeLog(7);
$result = (new MenuAssignLocationAbility($log))
->execute(['location' => 'primary-menu', 'menu_id' => 0, 'confirm' => true]);
self::assertSame('unassigned', $result['changed'][0]['action']);
self::assertSame(3, $result['previous_menu_id']);
self::assertStringContainsString('"Hauptmenü" (ID 3)', $result['summary']);
self::assertSame(7, $result['log_id']);
$log->assertRecorded([
'ability' => 'lit/menu-assign-location',
'object_type' => 'menu_location',
'object_id' => 'primary-menu',
'summary' => 'Menü "Hauptmenü" (ID 3) von der Position primary-menu entfernt',
]);
}
public function testRemovingFromAnEmptyLocationNeedsNoConfirmation(): void
{
Functions\when('get_nav_menu_locations')->justReturn([]);
Functions\expect('set_theme_mod')->once()->with('nav_menu_locations', []);
$log = new FakeChangeLog(8);
$result = (new MenuAssignLocationAbility($log))->execute(['location' => 'primary-menu', 'menu_id' => 0]);
self::assertTrue($result['ok']);
self::assertNull($result['previous_menu_id']);
}
public function testDescribesTheConfirmationForRemoval(): void
{
$ability = new MenuAssignLocationAbility(FakeChangeLog::forbidden());
self::assertArrayHasKey('confirm', $ability->inputSchema()['properties']);
self::assertStringContainsString('confirm: true', $ability->description());
}
public function testUnknownMenuIsNotFound(): void
{
Functions\expect('set_theme_mod')->never();
$result = (new MenuAssignLocationAbility(FakeChangeLog::forbidden()))
->execute(['location' => 'primary-menu', 'menu_id' => 99]);
self::assertToolError($result, 'not_found');
}
public function testPermissionNeedsThemeOptionRights(): void
{
Functions\when('current_user_can')->justReturn(false);
$ability = new MenuAssignLocationAbility(FakeChangeLog::forbidden());
$input = ['location' => 'primary-menu', 'menu_id' => 8];
self::assertToolError($ability->permission($input), 'forbidden');
}
public function testIsDestructiveBecauseItReplacesTheMenuOfTheLocation(): void
{
$ability = new MenuAssignLocationAbility(FakeChangeLog::forbidden());
self::assertSame(['readonly' => false, 'destructive' => true, 'idempotent' => true], $ability->annotations());
}
}