feat: declarative and operation-based menu item editing
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,65 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace LIT\MCP\Tests\Unit\Integrations\WordPress\Menus;
|
||||
|
||||
use LIT\MCP\Integrations\WordPress\Menus\MenuOperations;
|
||||
use LIT\MCP\Tests\Unit\TestCase;
|
||||
|
||||
final class MenuOperationsTest extends TestCase
|
||||
{
|
||||
/** @return list<array<string, mixed>> Start(1) > Team(2); Kontakt(3) */
|
||||
private function tree(): array
|
||||
{
|
||||
return [
|
||||
['id' => 1, 'title' => 'Start', 'children' => [['id' => 2, 'title' => 'Team', 'children' => []]]],
|
||||
['id' => 3, 'title' => 'Kontakt', 'children' => []],
|
||||
];
|
||||
}
|
||||
|
||||
public function testAddInsertsANewNodeAtThePosition(): void
|
||||
{
|
||||
$result = (new MenuOperations())->apply($this->tree(), [
|
||||
['op' => 'add', 'item' => ['type' => 'custom', 'url' => '/blog', 'title' => 'Blog'], 'position' => 1],
|
||||
]);
|
||||
|
||||
self::assertSame(1, $result[0]['id']);
|
||||
self::assertSame(['type' => 'custom', 'url' => '/blog', 'title' => 'Blog', 'children' => []], $result[1]);
|
||||
self::assertSame(3, $result[2]['id']);
|
||||
}
|
||||
|
||||
public function testMoveAndRemoveKeepChildren(): void
|
||||
{
|
||||
$result = (new MenuOperations())->apply($this->tree(), [
|
||||
['op' => 'move', 'id' => 3, 'parent_id' => 1, 'position' => 0],
|
||||
['op' => 'remove', 'id' => 1],
|
||||
]);
|
||||
|
||||
self::assertSame([3, 2], array_column($result, 'id'));
|
||||
self::assertSame([], $result[0]['children']);
|
||||
}
|
||||
|
||||
public function testInvalidOperationsAreReportedWithTheirIndex(): void
|
||||
{
|
||||
$operations = new MenuOperations();
|
||||
|
||||
$unknown = $operations->apply($this->tree(), [['op' => 'move', 'id' => 99]]);
|
||||
$cycle = $operations->apply($this->tree(), [['op' => 'move', 'id' => 1, 'parent_id' => 2]]);
|
||||
$badOp = $operations->apply($this->tree(), [['op' => 'rename', 'id' => 1]]);
|
||||
|
||||
self::assertArrayHasKey('operations.0', $unknown->get_error_data()['fields']);
|
||||
self::assertStringContainsString('unter sich selbst', $cycle->get_error_data()['fields']['operations.0']);
|
||||
self::assertStringContainsString('add, move oder remove', $badOp->get_error_data()['fields']['operations.0']);
|
||||
}
|
||||
|
||||
public function testExistingNodesKeepOnlyTheirIdSoTheWriterKeepsTheirData(): void
|
||||
{
|
||||
$result = (new MenuOperations())->apply($this->tree(), [['op' => 'move', 'id' => 2]]);
|
||||
|
||||
self::assertSame(
|
||||
[['id' => 1, 'children' => []], ['id' => 3, 'children' => []], ['id' => 2, 'children' => []]],
|
||||
$result
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,194 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace LIT\MCP\Tests\Unit\Integrations\WordPress\Menus;
|
||||
|
||||
use Brain\Monkey\Functions;
|
||||
use LIT\MCP\Abilities\WriteFailed;
|
||||
use LIT\MCP\ChangeLog\ChangeLog;
|
||||
use LIT\MCP\Integrations\WordPress\Menus\MenuOperations;
|
||||
use LIT\MCP\Integrations\WordPress\Menus\MenuSetItemsAbility;
|
||||
use LIT\MCP\Integrations\WordPress\Menus\MenuTree;
|
||||
use LIT\MCP\Integrations\WordPress\Menus\MenuWriter;
|
||||
use LIT\MCP\Tests\Unit\TestCase;
|
||||
use Mockery;
|
||||
use RuntimeException;
|
||||
|
||||
final class MenuSetItemsAbilityTest extends TestCase
|
||||
{
|
||||
use MenuFixtures;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
Functions\when('wp_get_nav_menu_object')->justReturn((object) ['term_id' => 3, 'name' => 'Hauptmenü']);
|
||||
Functions\when('wp_get_nav_menu_items')->justReturn([self::item(10, 0, 'Home', 'custom', '/')]);
|
||||
}
|
||||
|
||||
public function testItemsAndOperationsAreMutuallyExclusive(): void
|
||||
{
|
||||
$ability = new MenuSetItemsAbility(new MenuWriter(), new MenuOperations(), Mockery::mock(ChangeLog::class));
|
||||
|
||||
self::assertSame('lit_validation_failed', $ability->execute(['menu_id' => 3])->get_error_code());
|
||||
self::assertSame(
|
||||
'lit_validation_failed',
|
||||
$ability->execute(['menu_id' => 3, 'items' => [], 'operations' => [['op' => 'remove', 'id' => 10]]])
|
||||
->get_error_code()
|
||||
);
|
||||
}
|
||||
|
||||
public function testUnknownMenuIsNotFound(): void
|
||||
{
|
||||
Functions\when('wp_get_nav_menu_object')->justReturn(false);
|
||||
$ability = new MenuSetItemsAbility(new MenuWriter(), new MenuOperations(), Mockery::mock(ChangeLog::class));
|
||||
|
||||
self::assertSame('lit_not_found', $ability->execute(['menu_id' => 9, 'items' => []])->get_error_code());
|
||||
}
|
||||
|
||||
public function testRemovalsNeedConfirmationAndNothingIsWritten(): void
|
||||
{
|
||||
Functions\expect('wp_update_nav_menu_item')->never();
|
||||
Functions\expect('wp_delete_post')->never();
|
||||
$ability = new MenuSetItemsAbility(new MenuWriter(), new MenuOperations(), Mockery::mock(ChangeLog::class));
|
||||
|
||||
$result = $ability->execute(['menu_id' => 3, 'items' => []]);
|
||||
|
||||
self::assertSame('lit_confirm_required', $result->get_error_code());
|
||||
self::assertSame([['id' => 10, 'title' => 'Home']], $result->get_error_data()['removals']);
|
||||
}
|
||||
|
||||
public function testAnInvalidTreeWritesNothing(): void
|
||||
{
|
||||
Functions\when('wp_parse_url')->alias('parse_url');
|
||||
Functions\expect('wp_update_nav_menu_item')->never();
|
||||
Functions\expect('wp_delete_post')->never();
|
||||
$ability = new MenuSetItemsAbility(new MenuWriter(), new MenuOperations(), Mockery::mock(ChangeLog::class));
|
||||
|
||||
$result = $ability->execute([
|
||||
'menu_id' => 3,
|
||||
'confirm' => true,
|
||||
'items' => [['id' => 10], ['type' => 'custom', 'url' => 'javascript:alert(1)', 'title' => 'X']],
|
||||
]);
|
||||
|
||||
self::assertSame('lit_validation_failed', $result->get_error_code());
|
||||
self::assertArrayHasKey('items.1', $result->get_error_data()['fields']);
|
||||
}
|
||||
|
||||
public function testInvalidOperationsWriteNothing(): void
|
||||
{
|
||||
$writer = Mockery::mock(MenuWriter::class);
|
||||
$writer->expects('apply')->never();
|
||||
$ability = new MenuSetItemsAbility($writer, new MenuOperations(), Mockery::mock(ChangeLog::class));
|
||||
|
||||
$result = $ability->execute(['menu_id' => 3, 'operations' => [['op' => 'move', 'id' => 99]]]);
|
||||
|
||||
self::assertSame('lit_validation_failed', $result->get_error_code());
|
||||
}
|
||||
|
||||
public function testAppliesAndReturnsTheTreeBeforeAndAfter(): void
|
||||
{
|
||||
$writer = Mockery::mock(MenuWriter::class);
|
||||
$writer->allows('plan')->andReturn(['errors' => [], 'removals' => [], 'warnings' => []]);
|
||||
$writer->expects('apply')->andReturn(['created' => 1, 'updated' => 1, 'removed' => 0]);
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->expects('record')->with('lit/menu-set-items', 'nav_menu', '3', Mockery::type('string'))->andReturn(21);
|
||||
|
||||
$result = (new MenuSetItemsAbility($writer, new MenuOperations(), $log))->execute([
|
||||
'menu_id' => 3,
|
||||
'operations' => [['op' => 'add', 'item' => ['type' => 'custom', 'url' => '/blog', 'title' => 'Blog']]],
|
||||
]);
|
||||
|
||||
self::assertSame('Home', $result['previous_items'][0]['title']);
|
||||
self::assertSame(21, $result['log_id']);
|
||||
self::assertStringContainsString('1 neu, 1 geändert, 0 entfernt', $result['summary']);
|
||||
}
|
||||
|
||||
public function testConfirmedRemovalsAreWrittenAndTheOldTreeIsReturned(): void
|
||||
{
|
||||
$writer = Mockery::mock(MenuWriter::class);
|
||||
$writer->allows('plan')->andReturn(
|
||||
['errors' => [], 'removals' => [['id' => 10, 'title' => 'Home']], 'warnings' => []]
|
||||
);
|
||||
$writer->expects('apply')->with(3, [], Mockery::type('array'))
|
||||
->andReturn(['created' => 0, 'updated' => 0, 'removed' => 1]);
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->allows('record')->andReturn(22);
|
||||
|
||||
$result = (new MenuSetItemsAbility($writer, new MenuOperations(), $log))->execute(
|
||||
['menu_id' => 3, 'items' => [], 'confirm' => true]
|
||||
);
|
||||
|
||||
self::assertSame(10, $result['previous_items'][0]['id']);
|
||||
self::assertStringContainsString('0 neu, 0 geändert, 1 entfernt', $result['summary']);
|
||||
}
|
||||
|
||||
public function testMenuGetOutputIsAcceptedAsInput(): void
|
||||
{
|
||||
$items = [self::item(10, 0, 'Start'), self::item(11, 10, 'Team'), self::item(12, 0, 'Kontakt', 'custom', '/k')];
|
||||
$items[0]->url = 'https://example.test/';
|
||||
Functions\when('wp_get_nav_menu_items')->justReturn($items);
|
||||
Functions\when('wp_update_nav_menu_item')->alias(static fn (int $menu, int $id): int => $id);
|
||||
Functions\expect('wp_delete_post')->never();
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->allows('record')->andReturn(23);
|
||||
|
||||
$result = (new MenuSetItemsAbility(new MenuWriter(), new MenuOperations(), $log))->execute(
|
||||
['menu_id' => 3, 'items' => MenuTree::build($items)]
|
||||
);
|
||||
|
||||
self::assertStringContainsString('0 neu, 3 geändert, 0 entfernt', $result['summary']);
|
||||
self::assertSame($result['previous_items'], $result['items']);
|
||||
}
|
||||
|
||||
public function testAWriteFailureIsPassedOnAsWriteFailed(): void
|
||||
{
|
||||
$writer = Mockery::mock(MenuWriter::class);
|
||||
$writer->allows('plan')->andReturn(['errors' => [], 'removals' => [], 'warnings' => []]);
|
||||
$writer->expects('apply')->andThrow(new WriteFailed('Menü nicht gespeichert.', true));
|
||||
$ability = new MenuSetItemsAbility($writer, new MenuOperations(), Mockery::mock(ChangeLog::class));
|
||||
|
||||
$this->expectException(WriteFailed::class);
|
||||
$ability->execute(['menu_id' => 3, 'items' => [['id' => 10]]]);
|
||||
}
|
||||
|
||||
public function testAChangeLogFailureIsOnlyAWarning(): void
|
||||
{
|
||||
$writer = Mockery::mock(MenuWriter::class);
|
||||
$writer->allows('plan')->andReturn(['errors' => [], 'removals' => [], 'warnings' => ['"X" ist nicht veröffentlicht']]);
|
||||
$writer->allows('apply')->andReturn(['created' => 0, 'updated' => 1, 'removed' => 0]);
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->allows('record')->andThrow(new RuntimeException('db down'));
|
||||
$file = (string) tempnam(sys_get_temp_dir(), 'lit');
|
||||
$errorLog = ini_set('error_log', $file);
|
||||
|
||||
try {
|
||||
$result = (new MenuSetItemsAbility($writer, new MenuOperations(), $log))->execute(
|
||||
['menu_id' => 3, 'items' => [['id' => 10]]]
|
||||
);
|
||||
} finally {
|
||||
ini_set('error_log', (string) $errorLog);
|
||||
unlink($file);
|
||||
}
|
||||
|
||||
self::assertNull($result['log_id']);
|
||||
self::assertCount(2, $result['warnings']);
|
||||
}
|
||||
|
||||
public function testPermissionNeedsThemeOptionRights(): void
|
||||
{
|
||||
Functions\when('current_user_can')->justReturn(false);
|
||||
$ability = new MenuSetItemsAbility(new MenuWriter(), new MenuOperations(), Mockery::mock(ChangeLog::class));
|
||||
|
||||
self::assertSame('lit_forbidden', $ability->permission(['menu_id' => 3])->get_error_code());
|
||||
}
|
||||
|
||||
public function testIsDestructiveAndDescribesWhenConfirmIsNeeded(): void
|
||||
{
|
||||
$ability = new MenuSetItemsAbility(new MenuWriter(), new MenuOperations(), Mockery::mock(ChangeLog::class));
|
||||
|
||||
self::assertSame(['readonly' => false, 'destructive' => true, 'idempotent' => false], $ability->annotations());
|
||||
self::assertStringContainsString('confirm: true', $ability->description());
|
||||
self::assertStringContainsString('nur nötig', $ability->description());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,258 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace LIT\MCP\Tests\Unit\Integrations\WordPress\Menus;
|
||||
|
||||
use Brain\Monkey\Functions;
|
||||
use LIT\MCP\Abilities\WriteFailed;
|
||||
use LIT\MCP\Integrations\WordPress\Menus\MenuTree;
|
||||
use LIT\MCP\Integrations\WordPress\Menus\MenuWriter;
|
||||
use LIT\MCP\Tests\Unit\TestCase;
|
||||
use Mockery;
|
||||
use WP_Error;
|
||||
use WP_Post;
|
||||
use WP_Term;
|
||||
|
||||
final class MenuWriterTest extends TestCase
|
||||
{
|
||||
use MenuFixtures;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
Functions\when('wp_parse_url')->alias('parse_url');
|
||||
Functions\when('esc_url_raw')->returnArg();
|
||||
}
|
||||
|
||||
/** @return array<int, object> */
|
||||
private function current(): array
|
||||
{
|
||||
return [
|
||||
10 => self::item(10, 0, 'Home', 'custom', '/'),
|
||||
11 => self::item(11, 0, 'Über uns'),
|
||||
];
|
||||
}
|
||||
|
||||
/** @return array<int, object> Start(10), Über uns(11) > Team(12), Kontakt(13, custom) */
|
||||
private function realistic(): array
|
||||
{
|
||||
$items = [
|
||||
self::item(10, 0, 'Start'),
|
||||
self::item(11, 0, 'Über uns'),
|
||||
self::item(12, 11, 'Team'),
|
||||
self::item(13, 0, 'Kontakt', 'custom', '/kontakt'),
|
||||
];
|
||||
$items[0]->url = 'https://example.test/';
|
||||
$items[1]->url = 'https://example.test/ueber-uns/';
|
||||
$items[1]->post_title = 'Wir über uns';
|
||||
$items[1]->title = 'Wir über uns';
|
||||
$items[2]->url = 'https://example.test/ueber-uns/team/';
|
||||
|
||||
return array_combine(array_map(static fn (object $item): int => $item->ID, $items), $items);
|
||||
}
|
||||
|
||||
public function testPlanFindsErrorsRemovalsAndWarnings(): void
|
||||
{
|
||||
Functions\when('current_user_can')->justReturn(true);
|
||||
Functions\when('get_post')->alias(static fn (int $id) => $id === 7
|
||||
? new WP_Post(['ID' => 7, 'post_type' => 'page', 'post_status' => 'draft', 'post_title' => 'Neu'])
|
||||
: null);
|
||||
|
||||
$plan = (new MenuWriter())->plan([
|
||||
['id' => 11, 'children' => [['id' => 11]]],
|
||||
['id' => 55],
|
||||
['type' => 'post', 'object_id' => 7],
|
||||
['type' => 'custom', 'url' => 'javascript:alert(1)', 'title' => 'X'],
|
||||
['type' => 'custom', 'url' => '/kontakt'],
|
||||
], $this->current());
|
||||
|
||||
self::assertStringContainsString('mehrfach', $plan['errors']['items.0.children.0']);
|
||||
self::assertStringContainsString('kein Eintrag dieses Menüs', $plan['errors']['items.1']);
|
||||
self::assertArrayNotHasKey('items.2', $plan['errors']);
|
||||
self::assertStringContainsString('url', $plan['errors']['items.3']);
|
||||
self::assertStringContainsString('title', $plan['errors']['items.4']);
|
||||
self::assertSame([['id' => 10, 'title' => 'Home']], $plan['removals']);
|
||||
self::assertStringContainsString('"Neu" ist nicht veröffentlicht', $plan['warnings'][0]);
|
||||
}
|
||||
|
||||
public function testPlanRejectsChildrenThatAreNoListAndNodesThatAreNoObject(): void
|
||||
{
|
||||
$plan = (new MenuWriter())->plan([['id' => 11, 'children' => 'x'], 'y'], $this->current());
|
||||
|
||||
self::assertSame('Liste erwartet.', $plan['errors']['items.0.children']);
|
||||
self::assertSame('Objekt erwartet.', $plan['errors']['items.1']);
|
||||
}
|
||||
|
||||
public function testLinkingNeedsReadAccessAndDoesNotRevealTheObject(): void
|
||||
{
|
||||
Functions\when('get_post')->justReturn(
|
||||
new WP_Post(['ID' => 7, 'post_type' => 'page', 'post_status' => 'private', 'post_title' => 'Geheim'])
|
||||
);
|
||||
Functions\when('get_term')->justReturn(new WP_Term(['term_id' => 4, 'taxonomy' => 'internal']));
|
||||
Functions\when('is_taxonomy_viewable')->justReturn(false);
|
||||
Functions\expect('current_user_can')->with('read_post', 7)->andReturn(false);
|
||||
Functions\expect('current_user_can')->with('edit_term', 4)->andReturn(false);
|
||||
|
||||
$plan = (new MenuWriter())->plan(
|
||||
[['type' => 'post', 'object_id' => 7], ['type' => 'taxonomy', 'object_id' => 4]],
|
||||
[]
|
||||
);
|
||||
|
||||
self::assertSame('object_id muss ein vorhandener, für dich lesbarer Eintrag sein.', $plan['errors']['items.0']);
|
||||
self::assertSame('object_id muss ein vorhandener, für dich lesbarer Begriff sein.', $plan['errors']['items.1']);
|
||||
self::assertSame([], $plan['warnings']);
|
||||
}
|
||||
|
||||
public function testViewableTermsCanBeLinked(): void
|
||||
{
|
||||
Functions\when('get_term')->justReturn(new WP_Term(['term_id' => 4, 'taxonomy' => 'category']));
|
||||
Functions\when('is_taxonomy_viewable')->justReturn(true);
|
||||
|
||||
$plan = (new MenuWriter())->plan([['type' => 'taxonomy', 'object_id' => 4]], []);
|
||||
|
||||
self::assertSame([], $plan['errors']);
|
||||
}
|
||||
|
||||
public function testInformationalFieldsOfExistingItemsAreIgnored(): void
|
||||
{
|
||||
Functions\expect('get_post')->never();
|
||||
|
||||
$plan = (new MenuWriter())->plan([
|
||||
['id' => 11, 'type' => 'custom', 'object' => 'custom', 'object_id' => 5, 'url' => 'https://anders.test/'],
|
||||
['id' => 10, 'url' => 'javascript:alert(1)'],
|
||||
], $this->current());
|
||||
|
||||
self::assertSame(['items.1' => 'Ungültige url (erlaubt: /pfad, #anker, http(s), mailto, tel).'], $plan['errors']);
|
||||
}
|
||||
|
||||
public function testIdZeroMeansANewItem(): void
|
||||
{
|
||||
$plan = (new MenuWriter())->plan([['id' => 0, 'type' => 'custom', 'url' => '#', 'title' => 'Mehr']], []);
|
||||
|
||||
self::assertSame([], $plan['errors']);
|
||||
}
|
||||
|
||||
public function testApplyWritesInTreeOrderKeepsExistingDataAndRemovesTheRest(): void
|
||||
{
|
||||
Functions\expect('wp_update_nav_menu_item')->once()->with(3, 11, Mockery::on(static fn (array $args): bool =>
|
||||
$args['menu-item-title'] === 'Wir'
|
||||
&& $args['menu-item-type'] === 'post_type'
|
||||
&& $args['menu-item-object-id'] === 111
|
||||
&& $args['menu-item-parent-id'] === 0
|
||||
&& $args['menu-item-position'] === 1
|
||||
&& $args['menu-item-status'] === 'publish'))->andReturn(11);
|
||||
Functions\expect('wp_update_nav_menu_item')->once()->with(3, 0, Mockery::on(static fn (array $args): bool =>
|
||||
$args['menu-item-type'] === 'custom'
|
||||
&& $args['menu-item-url'] === '/kontakt'
|
||||
&& $args['menu-item-title'] === 'Kontakt'
|
||||
&& $args['menu-item-parent-id'] === 11
|
||||
&& $args['menu-item-position'] === 2))->andReturn(12);
|
||||
Functions\expect('wp_delete_post')->once()->with(10, true)->andReturn(new WP_Post(['ID' => 10]));
|
||||
|
||||
$stats = (new MenuWriter())->apply(3, [
|
||||
['id' => 11, 'title' => 'Wir', 'children' => [['type' => 'custom', 'url' => '/kontakt', 'title' => 'Kontakt']]],
|
||||
], $this->current());
|
||||
|
||||
self::assertSame(['created' => 1, 'updated' => 1, 'removed' => 1], $stats);
|
||||
}
|
||||
|
||||
public function testMenuGetOutputRoundTripsAsANoOp(): void
|
||||
{
|
||||
Functions\expect('get_post')->never();
|
||||
$current = $this->realistic();
|
||||
$tree = json_decode((string) json_encode(MenuTree::build(array_values($current))), true);
|
||||
$calls = [];
|
||||
Functions\when('wp_update_nav_menu_item')->alias(static function (int $menu, int $id, array $args) use (&$calls) {
|
||||
$calls[$id] = $args;
|
||||
return $id;
|
||||
});
|
||||
Functions\expect('wp_delete_post')->never();
|
||||
|
||||
$writer = new MenuWriter();
|
||||
$plan = $writer->plan($tree, $current);
|
||||
$stats = $writer->apply(3, $tree, $current);
|
||||
|
||||
self::assertSame(['errors' => [], 'removals' => [], 'warnings' => []], $plan);
|
||||
self::assertSame(['created' => 0, 'updated' => 4, 'removed' => 0], $stats);
|
||||
self::assertSame([10, 11, 12, 13], array_keys($calls));
|
||||
foreach ($current as $id => $item) {
|
||||
self::assertSame($item->post_title, $calls[$id]['menu-item-title'], "title of $id");
|
||||
self::assertSame((int) $item->menu_item_parent, $calls[$id]['menu-item-parent-id'], "parent of $id");
|
||||
self::assertSame((int) $item->object_id, $calls[$id]['menu-item-object-id'], "object of $id");
|
||||
}
|
||||
self::assertSame('', $calls[10]['menu-item-url']);
|
||||
self::assertSame('/kontakt', $calls[13]['menu-item-url']);
|
||||
self::assertSame([1, 2, 3, 4], array_column($calls, 'menu-item-position'));
|
||||
}
|
||||
|
||||
public function testChangedTitleOfAnExistingItemBecomesItsLabel(): void
|
||||
{
|
||||
$current = $this->realistic();
|
||||
$calls = [];
|
||||
Functions\when('wp_update_nav_menu_item')->alias(static function (int $menu, int $id, array $args) use (&$calls) {
|
||||
$calls[$id] = $args;
|
||||
return $id;
|
||||
});
|
||||
|
||||
(new MenuWriter())->apply(3, [
|
||||
['id' => 10, 'title' => 'Startseite'],
|
||||
['id' => 11, 'title' => 'Wir über uns', 'children' => [['id' => 12]]],
|
||||
['id' => 13, 'title' => 'Kontakt', 'url' => 'mailto:info@example.test'],
|
||||
], $current);
|
||||
|
||||
self::assertSame('Startseite', $calls[10]['menu-item-title']);
|
||||
self::assertSame('Wir über uns', $calls[11]['menu-item-title']);
|
||||
self::assertSame('mailto:info@example.test', $calls[13]['menu-item-url']);
|
||||
}
|
||||
|
||||
public function testAFailedWriteRestoresThePreviousTree(): void
|
||||
{
|
||||
$calls = [];
|
||||
Functions\when('wp_update_nav_menu_item')->alias(static function (int $menu, int $id, array $args) use (&$calls) {
|
||||
$calls[] = [$id, $args];
|
||||
return count($calls) === 3 ? new WP_Error('db', 'kaputt') : ($id > 0 ? $id : 50);
|
||||
});
|
||||
Functions\expect('wp_delete_post')->once()->with(50, true)->andReturn(new WP_Post(['ID' => 50]));
|
||||
|
||||
try {
|
||||
(new MenuWriter())->apply(3, [
|
||||
['id' => 11, 'title' => 'Wir', 'children' => [
|
||||
['type' => 'custom', 'url' => '/a', 'title' => 'A'],
|
||||
['type' => 'custom', 'url' => '/b', 'title' => 'B'],
|
||||
]],
|
||||
], $this->current());
|
||||
self::fail('WriteFailed expected');
|
||||
} catch (WriteFailed $e) {
|
||||
self::assertTrue($e->rolledBack);
|
||||
self::assertStringContainsString('kaputt', $e->getMessage());
|
||||
self::assertSame('lit_write_failed', $e->toError()->get_error_code());
|
||||
}
|
||||
|
||||
$restore = $calls[3];
|
||||
self::assertSame(11, $restore[0]);
|
||||
self::assertSame('', $restore[1]['menu-item-title']);
|
||||
self::assertSame(0, $restore[1]['menu-item-parent-id']);
|
||||
self::assertSame(2, $restore[1]['menu-item-position']);
|
||||
self::assertCount(4, $calls);
|
||||
}
|
||||
|
||||
public function testAFailedRemovalAfterAnotherRemovalIsAPartialWrite(): void
|
||||
{
|
||||
Functions\when('wp_update_nav_menu_item')->alias(static fn (int $menu, int $id): int => $id);
|
||||
Functions\expect('wp_delete_post')->once()->with(10, true)->andReturn(new WP_Post(['ID' => 10]));
|
||||
Functions\expect('wp_delete_post')->once()->with(11, true)->andReturn(false);
|
||||
|
||||
try {
|
||||
(new MenuWriter())->apply(3, [], $this->current());
|
||||
self::fail('WriteFailed expected');
|
||||
} catch (WriteFailed $e) {
|
||||
self::assertFalse($e->rolledBack);
|
||||
$error = $e->toError();
|
||||
self::assertSame('lit_partial_write', $error->get_error_code());
|
||||
self::assertSame(3, $error->get_error_data()['menu_id']);
|
||||
self::assertSame('Home', $error->get_error_data()['previous_items'][0]['title']);
|
||||
self::assertStringContainsString('lit-menu-get', $error->get_error_data()['hint']);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -31,6 +31,7 @@ final class WordPressIntegrationTest extends TestCase
|
||||
'lit/menu-list',
|
||||
'lit/menu-get',
|
||||
'lit/menu-create',
|
||||
'lit/menu-set-items',
|
||||
'lit/menu-assign-location',
|
||||
];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user