feat: menu list, get, create and location tools
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01At3V6SjwbMe6eMzVGW3Hs7
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace LIT\MCP\Tests\Unit\Integrations\WordPress\Menus;
|
||||
|
||||
use Brain\Monkey\Functions;
|
||||
use LIT\MCP\ChangeLog\ChangeLog;
|
||||
use LIT\MCP\Integrations\WordPress\Menus\MenuAssignLocationAbility;
|
||||
use LIT\MCP\Tests\Unit\TestCase;
|
||||
use Mockery;
|
||||
|
||||
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) => $id === 8 ? (object) ['term_id' => 8, 'name' => 'Neu'] : false
|
||||
);
|
||||
}
|
||||
|
||||
public function testAssignsAndReportsThePreviousMenu(): void
|
||||
{
|
||||
Functions\expect('set_theme_mod')->once()->with('nav_menu_locations', ['primary-menu' => 8]);
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->expects('record')->andReturn(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']);
|
||||
}
|
||||
|
||||
public function testZeroRemovesTheAssignment(): void
|
||||
{
|
||||
Functions\expect('set_theme_mod')->once()->with('nav_menu_locations', []);
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->expects('record')->andReturn(7);
|
||||
|
||||
$result = (new MenuAssignLocationAbility($log))->execute(['location' => 'primary-menu', 'menu_id' => 0]);
|
||||
|
||||
self::assertSame('unassigned', $result['changed'][0]['action']);
|
||||
}
|
||||
|
||||
public function testUnknownMenuIsNotFound(): void
|
||||
{
|
||||
Functions\expect('set_theme_mod')->never();
|
||||
|
||||
$result = (new MenuAssignLocationAbility(Mockery::mock(ChangeLog::class)))
|
||||
->execute(['location' => 'primary-menu', 'menu_id' => 99]);
|
||||
|
||||
self::assertSame('lit_not_found', $result->get_error_code());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace LIT\MCP\Tests\Unit\Integrations\WordPress\Menus;
|
||||
|
||||
use Brain\Monkey\Functions;
|
||||
use LIT\MCP\ChangeLog\ChangeLog;
|
||||
use LIT\MCP\Integrations\WordPress\Menus\MenuCreateAbility;
|
||||
use LIT\MCP\Tests\Unit\TestCase;
|
||||
use Mockery;
|
||||
use WP_Error;
|
||||
|
||||
final class MenuCreateAbilityTest extends TestCase
|
||||
{
|
||||
public function testUnknownLocationIsRejectedBeforeCreating(): void
|
||||
{
|
||||
Functions\when('get_registered_nav_menus')->justReturn(['primary-menu' => 'Hauptmenü']);
|
||||
Functions\expect('wp_create_nav_menu')->never();
|
||||
|
||||
$ability = new MenuCreateAbility(Mockery::mock(ChangeLog::class));
|
||||
$result = $ability->execute(['name' => 'Neu', 'location' => 'nope']);
|
||||
|
||||
self::assertSame('lit_validation_failed', $result->get_error_code());
|
||||
self::assertStringContainsString('primary-menu', $result->get_error_message());
|
||||
}
|
||||
|
||||
public function testExistingNameIsAValidationError(): void
|
||||
{
|
||||
Functions\when('wp_create_nav_menu')->justReturn(
|
||||
new WP_Error('menu_exists', 'Der Menüname existiert bereits.')
|
||||
);
|
||||
|
||||
$result = (new MenuCreateAbility(Mockery::mock(ChangeLog::class)))->execute(['name' => 'Hauptmenü']);
|
||||
|
||||
self::assertSame('lit_validation_failed', $result->get_error_code());
|
||||
}
|
||||
|
||||
public function testCreatesAndAssignsTheLocation(): void
|
||||
{
|
||||
Functions\when('get_registered_nav_menus')->justReturn(['primary-menu' => 'Hauptmenü']);
|
||||
Functions\expect('wp_create_nav_menu')->once()->with('Neu')->andReturn(8);
|
||||
Functions\when('get_nav_menu_locations')->justReturn(['footer-menu' => 2]);
|
||||
Functions\expect('set_theme_mod')->once()->with(
|
||||
'nav_menu_locations',
|
||||
['footer-menu' => 2, 'primary-menu' => 8]
|
||||
);
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->expects('record')
|
||||
->with('lit/menu-create', 'nav_menu', '8', 'Menü "Neu" angelegt (Position primary-menu)')
|
||||
->andReturn(5);
|
||||
|
||||
$result = (new MenuCreateAbility($log))->execute(['name' => 'Neu', 'location' => 'primary-menu']);
|
||||
|
||||
self::assertSame(8, $result['menu_id']);
|
||||
self::assertSame('created', $result['changed'][0]['action']);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace LIT\MCP\Tests\Unit\Integrations\WordPress\Menus;
|
||||
|
||||
/**
|
||||
* Shared wp_get_nav_menu_items()-shaped fixture, reused by every menu test
|
||||
* (tree building, read/write abilities, and the Task 14 menu-set-items tests).
|
||||
*/
|
||||
trait MenuFixtures
|
||||
{
|
||||
public static function item(
|
||||
int $id,
|
||||
int $parent,
|
||||
string $title,
|
||||
string $type = 'post_type',
|
||||
string $url = ''
|
||||
): object {
|
||||
return (object) [
|
||||
'ID' => $id,
|
||||
'menu_item_parent' => (string) $parent,
|
||||
'title' => $title,
|
||||
'post_title' => $type === 'custom' ? $title : '',
|
||||
'type' => $type,
|
||||
'object' => $type === 'custom' ? 'custom' : 'page',
|
||||
'object_id' => (string) ($type === 'custom' ? $id : $id + 100),
|
||||
'url' => $url,
|
||||
'description' => '',
|
||||
'attr_title' => '',
|
||||
'target' => '',
|
||||
'classes' => [''],
|
||||
'xfn' => '',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace LIT\MCP\Tests\Unit\Integrations\WordPress\Menus;
|
||||
|
||||
use Brain\Monkey\Functions;
|
||||
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('lit_not_found', $ability->execute(['menu_id' => 9])->get_error_code());
|
||||
}
|
||||
|
||||
public function testReadToolsNeedThemeOptionRights(): void
|
||||
{
|
||||
Functions\when('current_user_can')->justReturn(false);
|
||||
|
||||
self::assertSame('lit_forbidden', (new MenuListAbility())->permission([])->get_error_code());
|
||||
self::assertSame('lit_forbidden', (new MenuGetAbility())->permission(['menu_id' => 3])->get_error_code());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace LIT\MCP\Tests\Unit\Integrations\WordPress\Menus;
|
||||
|
||||
use LIT\MCP\Integrations\WordPress\Menus\MenuTree;
|
||||
use LIT\MCP\Tests\Unit\TestCase;
|
||||
|
||||
final class MenuTreeTest extends TestCase
|
||||
{
|
||||
use MenuFixtures;
|
||||
|
||||
public function testBuildsANestedTreeInMenuOrder(): void
|
||||
{
|
||||
$tree = MenuTree::build([
|
||||
self::item(10, 0, 'Start'),
|
||||
self::item(11, 0, 'Über uns'),
|
||||
self::item(12, 11, 'Team'),
|
||||
self::item(13, 0, 'Kontakt', 'custom', '/kontakt'),
|
||||
]);
|
||||
|
||||
self::assertSame(['Start', 'Über uns', 'Kontakt'], array_column($tree, 'title'));
|
||||
self::assertSame('Team', $tree[1]['children'][0]['title']);
|
||||
self::assertSame([], $tree[1]['children'][0]['children']);
|
||||
self::assertSame('custom', $tree[2]['type']);
|
||||
self::assertSame('post', $tree[0]['type']);
|
||||
self::assertSame(110, $tree[0]['object_id']);
|
||||
}
|
||||
|
||||
public function testOrphansAndSelfParentsLandOnTheTopLevel(): void
|
||||
{
|
||||
$tree = MenuTree::build([self::item(20, 99, 'Waise'), self::item(21, 21, 'Zirkel')]);
|
||||
|
||||
self::assertSame(['Waise', 'Zirkel'], array_column($tree, 'title'));
|
||||
}
|
||||
}
|
||||
@@ -28,6 +28,10 @@ final class WordPressIntegrationTest extends TestCase
|
||||
'lit/post-replace-with-copy',
|
||||
'lit/revisions-list',
|
||||
'lit/revision-restore',
|
||||
'lit/menu-list',
|
||||
'lit/menu-get',
|
||||
'lit/menu-create',
|
||||
'lit/menu-assign-location',
|
||||
];
|
||||
|
||||
public function testProvidesTheWordPressAbilitiesInOrder(): void
|
||||
|
||||
Reference in New Issue
Block a user