feat: deferred 1a items - menu_rev lock, lit_mcp_guides filter, warnings, schedule guard precision
Task 14 (rulings P17/P18, Q10): - lit/menu-get returns menu_rev (Rev::ofMenu over id, parent, position, title, url, object, object_id); lit/menu-set-items requires it, refuses a stale one with rev_mismatch (hint: lit/menu-get) and returns the new menu_rev. Errors::revMismatch takes an optional message/hint. - GuideRepository: filter lit_mcp_guides adds/overrides topics; invalid topic names and non-string content are ignored, files stay the default. - lit/post-trash names front page, posts page and linking menus in the confirmation and as warnings; lit/menu-assign-location needs confirm to remove an assigned menu and names it; lit/post-schedule warns that a private entry becomes public; Divi hints no longer point to non-existing lit-divi-* tools. - PostGuard::missedSchedule: 60 s publish window, zero GMT date falls back to get_gmt_from_date(post_date); post-schedule refuses dates within that window. - write_failed after a clean rollback that keeps a revision/backup says "Die Aenderung wurde nicht gespeichert (... bleibt erhalten)" via the new state parameter (Errors::writeFailed / WriteFailed rolledBackState). - wp-basis-smoke: menu_rev flow, unassign confirm, trash/menu and private-schedule warnings. 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:
@@ -99,7 +99,15 @@ PAST=$(ability lit/post-schedule "{\"post_id\":$PAGE_ID,\"date\":\"2020-01-01 10
|
||||
[ "$(error_code "$PAST")" = "validation_failed" ] || fail "past schedule accepted: $PAST"
|
||||
tool lit-post-publish "{\"post_id\":$PAGE_ID}" >/dev/null
|
||||
[ "$(wp post get "$PAGE_ID" --field=post_status)" = "publish" ] || fail "scheduled page did not publish now"
|
||||
pass "publish/unpublish/schedule, past dates rejected"
|
||||
PRIVATE=$(tool lit-content-create '{"post_type":"page","title":"LIT Smoke Privat"}')
|
||||
PRIVATE_ID=$(echo "$PRIVATE" | jq -r '.changed[0].id // empty')
|
||||
[ -n "$PRIVATE_ID" ] || fail "content-create (private): $PRIVATE"
|
||||
CREATED_POSTS+=("$PRIVATE_ID")
|
||||
wp post update "$PRIVATE_ID" --post_status=private >/dev/null
|
||||
SCHEDULED_PRIVATE=$(ability lit/post-schedule "{\"post_id\":$PRIVATE_ID,\"date\":\"$FUTURE\"}")
|
||||
echo "$SCHEDULED_PRIVATE" | jq -e '.ok == true and (.warnings | any(contains("ÖFFENTLICH")))' >/dev/null \
|
||||
|| fail "scheduling a private page does not warn: $SCHEDULED_PRIVATE"
|
||||
pass "publish/unpublish/schedule, past dates rejected, private schedule warns"
|
||||
|
||||
# 6. Copy and replace (lit-content-update, lit/post-replace-with-copy: rev required)
|
||||
COPY=$(ability lit/post-duplicate "{\"post_id\":$PAGE_ID}")
|
||||
@@ -153,29 +161,44 @@ REFUSED=$(tool lit-content-update "{\"post_id\":$MISSED_ID,\"rev\":\"$MISSED_REV
|
||||
[ "$(wp post get "$MISSED_ID" --field=post_status)" = "future" ] || fail "missed schedule got published"
|
||||
pass "content-update refuses a missed schedule instead of publishing it"
|
||||
|
||||
# 9. Menus
|
||||
# 9. Menus (lit/menu-set-items: menu_rev from lit/menu-get required)
|
||||
MENU=$(ability lit/menu-create '{"name":"LIT Smoke Menü"}')
|
||||
MENU_ID=$(echo "$MENU" | jq -r '.menu_id // empty')
|
||||
[ -n "$MENU_ID" ] || fail "menu-create: $MENU"
|
||||
ability lit/menu-set-items "{\"menu_id\":$MENU_ID,\"items\":[{\"type\":\"post\",\"object_id\":$PAGE_ID,\"children\":[{\"type\":\"custom\",\"url\":\"/kontakt\",\"title\":\"Kontakt\"}]}]}" \
|
||||
| jq -e '.ok == true' >/dev/null || fail "menu-set-items (tree)"
|
||||
MENU_REV=$(ability lit/menu-get "{\"menu_id\":$MENU_ID}" | jq -r '.menu_rev // empty')
|
||||
[ -n "$MENU_REV" ] || fail "menu-get lacks menu_rev"
|
||||
NOREV=$(ability lit/menu-set-items "{\"menu_id\":$MENU_ID,\"menu_rev\":\"000000000000\",\"items\":[]}")
|
||||
[ "$(error_code "$NOREV")" = "rev_mismatch" ] || fail "stale menu_rev accepted: $NOREV"
|
||||
SET=$(ability lit/menu-set-items "{\"menu_id\":$MENU_ID,\"menu_rev\":\"$MENU_REV\",\"items\":[{\"type\":\"post\",\"object_id\":$PAGE_ID,\"children\":[{\"type\":\"custom\",\"url\":\"/kontakt\",\"title\":\"Kontakt\"}]}]}")
|
||||
echo "$SET" | jq -e '.ok == true and (.menu_rev | type) == "string"' >/dev/null || fail "menu-set-items (tree): $SET"
|
||||
STALE_MENU=$(ability lit/menu-set-items "{\"menu_id\":$MENU_ID,\"menu_rev\":\"$MENU_REV\",\"items\":[]}")
|
||||
[ "$(error_code "$STALE_MENU")" = "rev_mismatch" ] || fail "outdated menu_rev accepted: $STALE_MENU"
|
||||
TREE=$(ability lit/menu-get "{\"menu_id\":$MENU_ID}")
|
||||
[ "$(echo "$TREE" | jq -r '.menu_rev')" = "$(echo "$SET" | jq -r '.menu_rev')" ] || fail "menu_rev of set-items differs from menu-get"
|
||||
CHILD_ID=$(echo "$TREE" | jq -r '.items[0].children[0].id // empty')
|
||||
[ "$(echo "$TREE" | jq -r '.items[0].children[0].title')" = "Kontakt" ] || fail "menu tree: $TREE"
|
||||
UNCHANGED_ARGS=$(jq -cn --argjson id "$MENU_ID" --argjson items "$(echo "$TREE" | jq -c '.items')" '{menu_id: $id, items: $items}')
|
||||
UNCHANGED_ARGS=$(echo "$TREE" | jq -c --argjson id "$MENU_ID" '{menu_id: $id, menu_rev: .menu_rev, items: .items}')
|
||||
UNCHANGED=$(ability lit/menu-set-items "$UNCHANGED_ARGS")
|
||||
echo "$UNCHANGED" | jq -e '.summary | contains("keine Änderung")' >/dev/null || fail "menu no-op: $UNCHANGED"
|
||||
REMOVE=$(ability lit/menu-set-items "{\"menu_id\":$MENU_ID,\"operations\":[{\"op\":\"remove\",\"id\":$CHILD_ID}]}")
|
||||
MENU_REV=$(echo "$UNCHANGED" | jq -r '.menu_rev')
|
||||
REMOVE=$(ability lit/menu-set-items "{\"menu_id\":$MENU_ID,\"menu_rev\":\"$MENU_REV\",\"operations\":[{\"op\":\"remove\",\"id\":$CHILD_ID}]}")
|
||||
[ "$(error_code "$REMOVE")" = "confirm_required" ] || fail "menu removal without confirm: $REMOVE"
|
||||
ability lit/menu-set-items "{\"menu_id\":$MENU_ID,\"operations\":[{\"op\":\"remove\",\"id\":$CHILD_ID}],\"confirm\":true}" \
|
||||
ability lit/menu-set-items "{\"menu_id\":$MENU_ID,\"menu_rev\":\"$MENU_REV\",\"operations\":[{\"op\":\"remove\",\"id\":$CHILD_ID}],\"confirm\":true}" \
|
||||
| jq -e '(.previous_items[0].children | length) == 1 and (.items[0].children | length) == 0' >/dev/null \
|
||||
|| fail "menu removal with confirm"
|
||||
LOCATION=$(ability lit/menu-list '{}' | jq -r '.locations[0].location // empty')
|
||||
if [ -n "$LOCATION" ]; then
|
||||
ability lit/menu-assign-location "{\"location\":\"$LOCATION\",\"menu_id\":$MENU_ID}" | jq -e '.ok == true' >/dev/null \
|
||||
|| fail "menu-assign-location"
|
||||
UNASSIGN=$(ability lit/menu-assign-location "{\"location\":\"$LOCATION\",\"menu_id\":0}")
|
||||
[ "$(error_code "$UNASSIGN")" = "confirm_required" ] || fail "unassign without confirm: $UNASSIGN"
|
||||
ability lit/menu-assign-location "{\"location\":\"$LOCATION\",\"menu_id\":0,\"confirm\":true}" \
|
||||
| jq -e '.summary | contains("LIT Smoke Menü")' >/dev/null || fail "unassign with confirm does not name the menu"
|
||||
fi
|
||||
pass "menu create, tree, no-op resend, operations with confirm, location"
|
||||
TRASH_LINKED=$(ability lit/post-trash "{\"post_id\":$PAGE_ID,\"confirm\":true}")
|
||||
echo "$TRASH_LINKED" | jq -e '.changed[0].action == "trashed" and (.warnings | any(contains("LIT Smoke Menü")))' >/dev/null \
|
||||
|| fail "trashing a menu-linked page does not name the menu: $TRASH_LINKED"
|
||||
pass "menu create, menu_rev lock, tree, no-op resend, operations with confirm, location (unassign with confirm), trash warns about menu link"
|
||||
|
||||
# 10. WordPress settings (whitelist)
|
||||
ability lit/wp-settings-get '{"group":"general"}' | jq -e '.settings[] | select(.key == "blogname")' >/dev/null \
|
||||
|
||||
@@ -67,6 +67,30 @@ final class ErrorsTest extends TestCase
|
||||
self::assertStringContainsString('erneut', $payload['hint']);
|
||||
}
|
||||
|
||||
public function testWriteFailedCanDescribeWhatWasKept(): void
|
||||
{
|
||||
$error = Errors::writeFailed(
|
||||
'Felder konnten nicht gespeichert werden.',
|
||||
[],
|
||||
null,
|
||||
'Die Änderung wurde nicht gespeichert (Revision 811 bleibt erhalten).'
|
||||
);
|
||||
|
||||
$message = json_decode($error->get_error_message(), true)['message'];
|
||||
self::assertStringNotContainsString('Nichts wurde gespeichert', $message);
|
||||
self::assertStringEndsWith('Die Änderung wurde nicht gespeichert (Revision 811 bleibt erhalten).', $message);
|
||||
}
|
||||
|
||||
public function testRevMismatchCanNameItsOwnObjectAndReadTool(): void
|
||||
{
|
||||
$error = Errors::revMismatch('Das Menü wurde geändert.', 'Mit lit/menu-get neu lesen.');
|
||||
|
||||
self::assertSame('lit_rev_mismatch', $error->get_error_code());
|
||||
self::assertSame('Das Menü wurde geändert.', json_decode($error->get_error_message(), true)['message']);
|
||||
self::assertSame('Mit lit/menu-get neu lesen.', $error->get_error_data()['hint']);
|
||||
self::assertStringContainsString('lit/content-get', Errors::revMismatch()->get_error_data()['hint']);
|
||||
}
|
||||
|
||||
public function testPartialWriteNamesWhatMayHaveBeenSavedAndWarnsAgainstRetrying(): void
|
||||
{
|
||||
$error = Errors::partialWrite('Felder konnten nicht gespeichert werden.', 5, 811);
|
||||
|
||||
@@ -49,6 +49,31 @@ final class RevAndJsonMapTest extends TestCase
|
||||
self::assertFalse(Rev::matches(['rev' => 123], $post));
|
||||
}
|
||||
|
||||
public function testMenuRevCoversTheComparedFieldsOfEveryItem(): void
|
||||
{
|
||||
$item = static fn (array $overrides = []): object => (object) array_merge([
|
||||
'ID' => 10,
|
||||
'menu_item_parent' => '0',
|
||||
'menu_order' => 1,
|
||||
'title' => 'Start',
|
||||
'url' => '/',
|
||||
'object' => 'page',
|
||||
'object_id' => '110',
|
||||
'classes' => [''],
|
||||
], $overrides);
|
||||
$rev = Rev::ofMenu([$item()]);
|
||||
|
||||
self::assertMatchesRegularExpression('/^[0-9a-f]{12}$/', $rev);
|
||||
self::assertSame($rev, Rev::ofMenu([$item()]));
|
||||
self::assertSame($rev, Rev::ofMenu([$item(['classes' => ['x']])]), 'not compared by menu-set-items');
|
||||
self::assertNotSame($rev, Rev::ofMenu([]));
|
||||
self::assertNotSame($rev, Rev::ofMenu([$item(), $item(['ID' => 11])]));
|
||||
foreach (['ID' => 12, 'menu_item_parent' => '9', 'menu_order' => 2, 'title' => 'Home', 'url' => '/x',
|
||||
'object' => 'post', 'object_id' => '111'] as $key => $value) {
|
||||
self::assertNotSame($rev, Rev::ofMenu([$item([$key => $value])]), $key);
|
||||
}
|
||||
}
|
||||
|
||||
public function testEmptyMapBecomesJsonObject(): void
|
||||
{
|
||||
self::assertInstanceOf(stdClass::class, JsonMap::of([]));
|
||||
|
||||
@@ -56,6 +56,17 @@ final class WriteFailedTest extends TestCase
|
||||
self::assertSame(3, $error->get_error_data()['menu_id']);
|
||||
}
|
||||
|
||||
public function testRolledBackStateReplacesNothingWasSavedOfWriteFailedOnly(): void
|
||||
{
|
||||
$state = 'Die Änderung wurde nicht gespeichert (Sicherung 88 bleibt erhalten).';
|
||||
$clean = (new WriteFailed('x', true, rolledBackState: $state))->toError();
|
||||
$partial = (new WriteFailed('x', false, rolledBackState: $state))->toError();
|
||||
|
||||
self::assertStringContainsString($state, $clean->get_error_message());
|
||||
self::assertStringNotContainsString('Nichts wurde gespeichert', $clean->get_error_message());
|
||||
self::assertStringNotContainsString($state, $partial->get_error_message());
|
||||
}
|
||||
|
||||
public function testCleanRollbackKeepsItsDetails(): void
|
||||
{
|
||||
$error = (new WriteFailed('Nicht übernommen.', true, details: ['backup_id' => 88]))->toError();
|
||||
|
||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace LIT\MCP\Tests\Unit\Guides;
|
||||
|
||||
use Brain\Monkey\Filters;
|
||||
use LIT\MCP\Guides\GuideRepository;
|
||||
use LIT\MCP\Tests\Unit\TestCase;
|
||||
|
||||
@@ -48,6 +49,40 @@ final class GuideRepositoryTest extends TestCase
|
||||
];
|
||||
}
|
||||
|
||||
public function testFilesAreTheDefaultWhenNoFilterIsRegistered(): void
|
||||
{
|
||||
Filters\expectApplied('lit_mcp_guides')->once()->with([])->andReturn([]);
|
||||
|
||||
self::assertSame(['divi-shortform', 'overview'], $this->guides->topics());
|
||||
self::assertStringContainsString('Testinhalt Kurzform', (string) $this->guides->get('divi-shortform'));
|
||||
}
|
||||
|
||||
public function testTheFilterAddsAndOverridesTopicsAndIgnoresInvalidNames(): void
|
||||
{
|
||||
Filters\expectApplied('lit_mcp_guides')->andReturn([
|
||||
'site-rules' => "# Regeln\n<b>as-is</b>",
|
||||
'overview' => 'Eigene Übersicht',
|
||||
'../../wp-config' => 'nope',
|
||||
'Upper' => 'nope',
|
||||
'not-string' => ['nope'],
|
||||
7 => 'nope',
|
||||
]);
|
||||
|
||||
self::assertSame(['divi-shortform', 'overview', 'site-rules'], $this->guides->topics());
|
||||
self::assertSame("# Regeln\n<b>as-is</b>", $this->guides->get('site-rules'));
|
||||
self::assertSame('Eigene Übersicht', $this->guides->get('overview'));
|
||||
self::assertNull($this->guides->get('../../wp-config'));
|
||||
self::assertNull($this->guides->get('Upper'));
|
||||
self::assertNull($this->guides->get('not-string'));
|
||||
}
|
||||
|
||||
public function testANonArrayFilterResultIsIgnored(): void
|
||||
{
|
||||
Filters\expectApplied('lit_mcp_guides')->andReturn('kaputt');
|
||||
|
||||
self::assertSame(['divi-shortform', 'overview'], $this->guides->topics());
|
||||
}
|
||||
|
||||
public function testMissingDirectoryYieldsNoTopics(): void
|
||||
{
|
||||
self::assertSame([], (new GuideRepository('/nonexistent/dir'))->topics());
|
||||
|
||||
@@ -116,7 +116,9 @@ final class ContentUpdateAbilityTest extends TestCase
|
||||
->execute(['post_id' => 5, 'rev' => Rev::ofPost($this->post), 'content' => '<p>Neu</p>']);
|
||||
|
||||
self::assertSame('lit_unsupported', $result->get_error_code());
|
||||
self::assertStringContainsString('lit-divi-outline', $result->get_error_message());
|
||||
self::assertStringNotContainsString('lit-divi', $result->get_error_message());
|
||||
self::assertStringContainsString('Divi-Werkzeuge folgen in einer späteren Version; bis dahin im Divi-Builder '
|
||||
. 'bearbeiten.', $result->get_error_data()['hint']);
|
||||
}
|
||||
|
||||
public function testNothingToChangeIsAValidationError(): void
|
||||
@@ -231,6 +233,9 @@ final class ContentUpdateAbilityTest extends TestCase
|
||||
|
||||
self::assertTrue($failure->rolledBack);
|
||||
self::assertSame('lit_write_failed', $failure->toError()->get_error_code());
|
||||
$message = $failure->toError()->get_error_message();
|
||||
self::assertStringNotContainsString('Nichts wurde gespeichert', $message);
|
||||
self::assertStringContainsString('Die Änderung wurde nicht gespeichert (Revision 811 bleibt erhalten).', $message);
|
||||
self::assertSame([
|
||||
['post_title' => 'Unser Team', 'post_name' => 'unser-team', 'ID' => 5],
|
||||
['post_name' => 'team', 'ID' => 5],
|
||||
|
||||
@@ -87,7 +87,8 @@ final class PostPresenterTest extends TestCase
|
||||
self::assertTrue($full['divi']);
|
||||
self::assertNull($full['content']);
|
||||
self::assertNull($full['featured_image']);
|
||||
self::assertStringContainsString('lit-divi-outline', $full['hints'][0]);
|
||||
self::assertStringNotContainsString('lit-divi', $full['hints'][0]);
|
||||
self::assertStringContainsString('Divi-Werkzeuge folgen in einer späteren Version', $full['hints'][0]);
|
||||
self::assertInstanceOf(stdClass::class, $full['terms']);
|
||||
self::assertInstanceOf(stdClass::class, $full['fields']);
|
||||
self::assertSame(Rev::ofPost($post), $full['rev']);
|
||||
|
||||
@@ -18,7 +18,11 @@ final class MenuAssignLocationAbilityTest extends TestCase
|
||||
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
|
||||
static fn (int $id) => match ($id) {
|
||||
8 => (object) ['term_id' => 8, 'name' => 'Neu'],
|
||||
3 => (object) ['term_id' => 3, 'name' => 'Hauptmenü'],
|
||||
default => false,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@@ -42,7 +46,21 @@ final class MenuAssignLocationAbilityTest extends TestCase
|
||||
self::assertSame(6, $result['log_id']);
|
||||
}
|
||||
|
||||
public function testZeroRemovesTheAssignment(): void
|
||||
public function testRemovingAnAssignmentNeedsConfirmation(): void
|
||||
{
|
||||
Functions\expect('set_theme_mod')->never();
|
||||
|
||||
$result = (new MenuAssignLocationAbility(Mockery::mock(ChangeLog::class)))
|
||||
->execute(['location' => 'primary-menu', 'menu_id' => 0]);
|
||||
|
||||
self::assertSame('lit_confirm_required', $result->get_error_code());
|
||||
self::assertStringContainsString(
|
||||
'"Hauptmenü" (ID 3)',
|
||||
json_decode($result->get_error_message(), true)['message']
|
||||
);
|
||||
}
|
||||
|
||||
public function testZeroRemovesTheAssignmentAndNamesTheRemovedMenu(): void
|
||||
{
|
||||
Functions\expect('set_theme_mod')->once()->with('nav_menu_locations', []);
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
@@ -51,16 +69,40 @@ final class MenuAssignLocationAbilityTest extends TestCase
|
||||
'lit/menu-assign-location',
|
||||
'menu_location',
|
||||
'primary-menu',
|
||||
'Zuweisung der Position primary-menu entfernt'
|
||||
'Menü "Hauptmenü" (ID 3) von der Position primary-menu entfernt'
|
||||
)
|
||||
->andReturn(7);
|
||||
|
||||
$result = (new MenuAssignLocationAbility($log))->execute(['location' => 'primary-menu', 'menu_id' => 0]);
|
||||
$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']);
|
||||
}
|
||||
|
||||
public function testRemovingFromAnEmptyLocationNeedsNoConfirmation(): void
|
||||
{
|
||||
Functions\when('get_nav_menu_locations')->justReturn([]);
|
||||
Functions\expect('set_theme_mod')->once()->with('nav_menu_locations', []);
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->allows('record')->andReturn(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(Mockery::mock(ChangeLog::class));
|
||||
|
||||
self::assertArrayHasKey('confirm', $ability->inputSchema()['properties']);
|
||||
self::assertStringContainsString('confirm: true', $ability->description());
|
||||
}
|
||||
|
||||
public function testUnknownMenuIsNotFound(): void
|
||||
{
|
||||
Functions\expect('set_theme_mod')->never();
|
||||
|
||||
@@ -5,6 +5,7 @@ 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;
|
||||
@@ -48,6 +49,8 @@ final class MenuReadAbilitiesTest extends TestCase
|
||||
|
||||
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::assertSame('lit_not_found', $ability->execute(['menu_id' => 9])->get_error_code());
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||
namespace LIT\MCP\Tests\Unit\Integrations\WordPress\Menus;
|
||||
|
||||
use Brain\Monkey\Functions;
|
||||
use LIT\MCP\Abilities\Rev;
|
||||
use LIT\MCP\Abilities\WriteFailed;
|
||||
use LIT\MCP\ChangeLog\ChangeLog;
|
||||
use LIT\MCP\Integrations\WordPress\Menus\MenuOperations;
|
||||
@@ -26,14 +27,52 @@ final class MenuSetItemsAbilityTest extends TestCase
|
||||
Functions\when('wp_get_nav_menu_items')->justReturn([self::item(10, 0, 'Home', 'custom', '/')]);
|
||||
}
|
||||
|
||||
/** The menu_rev of the items wp_get_nav_menu_items() currently returns (what lit/menu-get would show). */
|
||||
private static function rev(): string
|
||||
{
|
||||
return Rev::ofMenu(wp_get_nav_menu_items(3));
|
||||
}
|
||||
|
||||
public function testAStaleOrMissingMenuRevIsRefusedAndNothingIsWritten(): void
|
||||
{
|
||||
$writer = Mockery::mock(MenuWriter::class);
|
||||
$writer->expects('apply')->never();
|
||||
$ability = new MenuSetItemsAbility($writer, new MenuOperations(), Mockery::mock(ChangeLog::class));
|
||||
|
||||
$stale = $ability->execute(['menu_id' => 3, 'menu_rev' => 'abcdefabcdef', 'items' => [['id' => 10]]]);
|
||||
$missing = $ability->execute(['menu_id' => 3, 'items' => [['id' => 10]]]);
|
||||
|
||||
self::assertSame('lit_rev_mismatch', $stale->get_error_code());
|
||||
self::assertStringContainsString('lit/menu-get', $stale->get_error_data()['hint']);
|
||||
self::assertStringContainsString('Menü', $stale->get_error_message());
|
||||
self::assertSame('lit_rev_mismatch', $missing->get_error_code());
|
||||
self::assertContains('menu_rev', $ability->inputSchema()['required']);
|
||||
self::assertStringContainsString('menu_rev', $ability->description());
|
||||
}
|
||||
|
||||
public function testTheResultCarriesTheNewMenuRev(): void
|
||||
{
|
||||
$writer = Mockery::mock(MenuWriter::class);
|
||||
$writer->allows('plan')->andReturn(['errors' => [], 'removals' => [], 'warnings' => []]);
|
||||
$writer->allows('apply')->andReturn(['created' => 0, 'updated' => 1, 'removed' => 0]);
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->allows('record')->andReturn(30);
|
||||
|
||||
$result = (new MenuSetItemsAbility($writer, new MenuOperations(), $log))->execute(
|
||||
['menu_id' => 3, 'menu_rev' => self::rev(), 'items' => [['id' => 10]]]
|
||||
);
|
||||
|
||||
self::assertSame(self::rev(), $result['menu_rev']);
|
||||
}
|
||||
|
||||
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, 'menu_rev' => self::rev()])->get_error_code());
|
||||
self::assertSame(
|
||||
'lit_validation_failed',
|
||||
$ability->execute(['menu_id' => 3, 'items' => [], 'operations' => [['op' => 'remove', 'id' => 10]]])
|
||||
$ability->execute(['menu_id' => 3, 'menu_rev' => self::rev(), 'items' => [], 'operations' => [['op' => 'remove', 'id' => 10]]])
|
||||
->get_error_code()
|
||||
);
|
||||
}
|
||||
@@ -52,7 +91,7 @@ final class MenuSetItemsAbilityTest extends TestCase
|
||||
Functions\expect('wp_delete_post')->never();
|
||||
$ability = new MenuSetItemsAbility(new MenuWriter(), new MenuOperations(), Mockery::mock(ChangeLog::class));
|
||||
|
||||
$result = $ability->execute(['menu_id' => 3, 'items' => []]);
|
||||
$result = $ability->execute(['menu_id' => 3, 'menu_rev' => self::rev(), 'items' => []]);
|
||||
|
||||
self::assertSame('lit_confirm_required', $result->get_error_code());
|
||||
self::assertSame([['id' => 10, 'title' => 'Home']], $result->get_error_data()['removals']);
|
||||
@@ -66,7 +105,7 @@ final class MenuSetItemsAbilityTest extends TestCase
|
||||
$ability = new MenuSetItemsAbility(new MenuWriter(), new MenuOperations(), Mockery::mock(ChangeLog::class));
|
||||
|
||||
$result = $ability->execute([
|
||||
'menu_id' => 3,
|
||||
'menu_id' => 3, 'menu_rev' => self::rev(),
|
||||
'confirm' => true,
|
||||
'items' => [['id' => 10], ['type' => 'custom', 'url' => 'javascript:alert(1)', 'title' => 'X']],
|
||||
]);
|
||||
@@ -81,7 +120,7 @@ final class MenuSetItemsAbilityTest extends TestCase
|
||||
$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]]]);
|
||||
$result = $ability->execute(['menu_id' => 3, 'menu_rev' => self::rev(), 'operations' => [['op' => 'move', 'id' => 99]]]);
|
||||
|
||||
self::assertSame('lit_validation_failed', $result->get_error_code());
|
||||
}
|
||||
@@ -95,7 +134,7 @@ final class MenuSetItemsAbilityTest extends TestCase
|
||||
$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,
|
||||
'menu_id' => 3, 'menu_rev' => self::rev(),
|
||||
'operations' => [['op' => 'add', 'item' => ['type' => 'custom', 'url' => '/blog', 'title' => 'Blog']]],
|
||||
]);
|
||||
|
||||
@@ -116,7 +155,7 @@ final class MenuSetItemsAbilityTest extends TestCase
|
||||
$log->allows('record')->andReturn(22);
|
||||
|
||||
$result = (new MenuSetItemsAbility($writer, new MenuOperations(), $log))->execute(
|
||||
['menu_id' => 3, 'items' => [], 'confirm' => true]
|
||||
['menu_id' => 3, 'menu_rev' => self::rev(), 'items' => [], 'confirm' => true]
|
||||
);
|
||||
|
||||
self::assertSame(10, $result['previous_items'][0]['id']);
|
||||
@@ -134,7 +173,7 @@ final class MenuSetItemsAbilityTest extends TestCase
|
||||
$log->expects('record')->never();
|
||||
|
||||
$result = (new MenuSetItemsAbility(new MenuWriter(), new MenuOperations(), $log))->execute(
|
||||
['menu_id' => 3, 'items' => MenuTree::build($items)]
|
||||
['menu_id' => 3, 'menu_rev' => self::rev(), 'items' => MenuTree::build($items)]
|
||||
);
|
||||
|
||||
self::assertSame('Menü "Hauptmenü": keine Änderung.', $result['summary']);
|
||||
@@ -151,7 +190,7 @@ final class MenuSetItemsAbilityTest extends TestCase
|
||||
$ability = new MenuSetItemsAbility($writer, new MenuOperations(), Mockery::mock(ChangeLog::class));
|
||||
|
||||
$result = $ability->execute(
|
||||
['menu_id' => 3, 'items' => [['type' => 'post', 'object_id' => 7]], 'allow_unpublished' => true]
|
||||
['menu_id' => 3, 'menu_rev' => self::rev(), 'items' => [['type' => 'post', 'object_id' => 7]], 'allow_unpublished' => true]
|
||||
);
|
||||
|
||||
self::assertSame('lit_validation_failed', $result->get_error_code());
|
||||
@@ -169,7 +208,7 @@ final class MenuSetItemsAbilityTest extends TestCase
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->allows('record')->andReturn(24);
|
||||
$ability = new MenuSetItemsAbility(new MenuWriter(), new MenuOperations(), $log);
|
||||
$input = ['menu_id' => 3, 'operations' => [['op' => 'add', 'item' => ['type' => 'post', 'object_id' => 7]]]];
|
||||
$input = ['menu_id' => 3, 'menu_rev' => self::rev(), 'operations' => [['op' => 'add', 'item' => ['type' => 'post', 'object_id' => 7]]]];
|
||||
|
||||
$refused = $ability->execute($input);
|
||||
$allowed = $ability->execute($input + ['allow_unpublished' => true]);
|
||||
@@ -190,7 +229,7 @@ final class MenuSetItemsAbilityTest extends TestCase
|
||||
$ability = new MenuSetItemsAbility(new MenuWriter(), new MenuOperations(), Mockery::mock(ChangeLog::class));
|
||||
|
||||
$result = $ability->execute([
|
||||
'menu_id' => 3,
|
||||
'menu_id' => 3, 'menu_rev' => self::rev(),
|
||||
'allow_unpublished' => true,
|
||||
'operations' => [['op' => 'add', 'item' => ['type' => 'post', 'object_id' => 7]]],
|
||||
]);
|
||||
@@ -209,7 +248,7 @@ final class MenuSetItemsAbilityTest extends TestCase
|
||||
$ability = new MenuSetItemsAbility($writer, new MenuOperations(), $log);
|
||||
|
||||
$this->expectException(WriteFailed::class);
|
||||
$ability->execute(['menu_id' => 3, 'items' => [['id' => 10]]]);
|
||||
$ability->execute(['menu_id' => 3, 'menu_rev' => self::rev(), 'items' => [['id' => 10]]]);
|
||||
}
|
||||
|
||||
public function testAPartialWriteIsLoggedAsPartial(): void
|
||||
@@ -222,7 +261,7 @@ final class MenuSetItemsAbilityTest extends TestCase
|
||||
$ability = new MenuSetItemsAbility($writer, new MenuOperations(), $log);
|
||||
|
||||
$this->expectException(WriteFailed::class);
|
||||
$ability->execute(['menu_id' => 3, 'items' => [['id' => 10]]]);
|
||||
$ability->execute(['menu_id' => 3, 'menu_rev' => self::rev(), 'items' => [['id' => 10]]]);
|
||||
}
|
||||
|
||||
public function testAChangeLogFailureIsOnlyAWarning(): void
|
||||
@@ -237,7 +276,7 @@ final class MenuSetItemsAbilityTest extends TestCase
|
||||
|
||||
try {
|
||||
$result = (new MenuSetItemsAbility($writer, new MenuOperations(), $log))->execute(
|
||||
['menu_id' => 3, 'items' => [['id' => 10]]]
|
||||
['menu_id' => 3, 'menu_rev' => self::rev(), 'items' => [['id' => 10]]]
|
||||
);
|
||||
} finally {
|
||||
ini_set('error_log', (string) $errorLog);
|
||||
@@ -253,7 +292,7 @@ final class MenuSetItemsAbilityTest extends TestCase
|
||||
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());
|
||||
self::assertSame('lit_forbidden', $ability->permission(['menu_id' => 3, 'menu_rev' => self::rev()])->get_error_code());
|
||||
}
|
||||
|
||||
public function testIsDestructiveAndDescribesWhenConfirmIsNeeded(): void
|
||||
|
||||
@@ -67,11 +67,40 @@ final class PostGuardTest extends TestCase
|
||||
self::assertTrue(PostGuard::missedSchedule(new WP_Post(['post_status' => 'future', 'post_date_gmt' => $past])));
|
||||
self::assertFalse(PostGuard::missedSchedule(new WP_Post(['post_status' => 'future', 'post_date_gmt' => $future])));
|
||||
self::assertFalse(PostGuard::missedSchedule(new WP_Post(['post_status' => 'draft', 'post_date_gmt' => $past])));
|
||||
Functions\when('get_gmt_from_date')->justReturn('');
|
||||
self::assertFalse(PostGuard::missedSchedule(
|
||||
new WP_Post(['post_status' => 'future', 'post_date_gmt' => '0000-00-00 00:00:00'])
|
||||
));
|
||||
}
|
||||
|
||||
public function testMissedScheduleIncludesTheLastMinuteBeforePublication(): void
|
||||
{
|
||||
// wp_update_post() publishes a future post on save when its date is within the next 60 seconds.
|
||||
$in30s = gmdate('Y-m-d H:i:s', time() + 30);
|
||||
$in5min = gmdate('Y-m-d H:i:s', time() + 300);
|
||||
|
||||
self::assertTrue(PostGuard::missedSchedule(new WP_Post(['post_status' => 'future', 'post_date_gmt' => $in30s])));
|
||||
self::assertFalse(PostGuard::missedSchedule(new WP_Post(['post_status' => 'future', 'post_date_gmt' => $in5min])));
|
||||
}
|
||||
|
||||
public function testMissedScheduleFallsBackToTheLocalDateWhenTheGmtDateIsZero(): void
|
||||
{
|
||||
Functions\when('get_gmt_from_date')->alias(
|
||||
static fn (string $local): string => $local === '2020-01-01 10:00:00' ? '2020-01-01 09:00:00' : $local
|
||||
);
|
||||
|
||||
self::assertTrue(PostGuard::missedSchedule(new WP_Post([
|
||||
'post_status' => 'future',
|
||||
'post_date_gmt' => '0000-00-00 00:00:00',
|
||||
'post_date' => '2020-01-01 10:00:00',
|
||||
])));
|
||||
self::assertFalse(PostGuard::missedSchedule(new WP_Post([
|
||||
'post_status' => 'future',
|
||||
'post_date_gmt' => '0000-00-00 00:00:00',
|
||||
'post_date' => '2099-01-01 10:00:00',
|
||||
])));
|
||||
}
|
||||
|
||||
public function testStatusStableRefusesAMissedScheduleWithAHint(): void
|
||||
{
|
||||
$post = new WP_Post(['ID' => 5, 'post_status' => 'future', 'post_date_gmt' => gmdate('Y-m-d H:i:s', time() - 60)]);
|
||||
|
||||
@@ -171,6 +171,11 @@ final class RevisionRestoreAbilityTest extends TestCase
|
||||
]);
|
||||
|
||||
self::assertSame('lit_write_failed', $result->get_error_code());
|
||||
self::assertStringNotContainsString('Nichts wurde gespeichert', $result->get_error_message());
|
||||
self::assertStringContainsString(
|
||||
'Die Änderung wurde nicht gespeichert (Revision 813 bleibt erhalten).',
|
||||
$result->get_error_message()
|
||||
);
|
||||
}
|
||||
|
||||
public function testFailuresAfterTheWriteOnlyAddWarnings(): void
|
||||
|
||||
@@ -231,6 +231,9 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
|
||||
self::assertTrue($e->rolledBack);
|
||||
self::assertSame('lit_write_failed', $e->toError()->get_error_code());
|
||||
self::assertSame(88, $e->toError()->get_error_data()['backup_id']);
|
||||
$message = $e->toError()->get_error_message();
|
||||
self::assertStringNotContainsString('Nichts wurde gespeichert', $message);
|
||||
self::assertStringContainsString('Die Änderung wurde nicht gespeichert (Sicherung 88', $message);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -54,6 +54,33 @@ final class PostScheduleAbilityTest extends TestCase
|
||||
self::assertStringContainsString('2099-06-01 09:30', $result['summary']);
|
||||
}
|
||||
|
||||
public function testWarnsThatAPrivatePostBecomesPublic(): void
|
||||
{
|
||||
Functions\when('get_post')->justReturn(new WP_Post(['ID' => 5, 'post_type' => 'post', 'post_title' => 'Intern', 'post_status' => 'private']));
|
||||
Functions\when('wp_update_post')->justReturn(5);
|
||||
Functions\when('get_post_status')->justReturn('future');
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->allows('record')->andReturn(4);
|
||||
|
||||
$result = $this->ability($log)->execute(['post_id' => 5, 'date' => '2099-06-01 09:30']);
|
||||
|
||||
self::assertCount(1, $result['warnings']);
|
||||
self::assertStringContainsString('öffentlich', $result['warnings'][0]);
|
||||
self::assertStringContainsString('2099-06-01 09:30', $result['warnings'][0]);
|
||||
}
|
||||
|
||||
public function testADraftGetsNoPrivacyWarning(): void
|
||||
{
|
||||
Functions\when('wp_update_post')->justReturn(5);
|
||||
Functions\when('get_post_status')->justReturn('future');
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->allows('record')->andReturn(4);
|
||||
|
||||
$result = $this->ability($log)->execute(['post_id' => 5, 'date' => '2099-06-01 09:30']);
|
||||
|
||||
self::assertSame([], $result['warnings']);
|
||||
}
|
||||
|
||||
public function testAHookThrowingDuringTheWriteIsAPartialWrite(): void
|
||||
{
|
||||
Functions\when('wp_update_post')->alias(static function (): void {
|
||||
@@ -95,4 +122,18 @@ final class PostScheduleAbilityTest extends TestCase
|
||||
self::assertStringContainsString('Zukunft', $past->get_error_message());
|
||||
self::assertSame('lit_validation_failed', $impossible->get_error_code());
|
||||
}
|
||||
|
||||
public function testADateWithinTheNextMinuteIsRejectedBecauseWordPressWouldPublishAtOnce(): void
|
||||
{
|
||||
Functions\expect('wp_update_post')->never();
|
||||
// The start of the next minute: always in the future, but at most 60 seconds away.
|
||||
$soon = (new \DateTimeImmutable('@' . (intdiv(time(), 60) * 60 + 60)))
|
||||
->setTimezone(new DateTimeZone('Europe/Berlin'));
|
||||
Functions\when('get_post_status')->justReturn('future');
|
||||
|
||||
$result = $this->ability(Mockery::mock(ChangeLog::class))
|
||||
->execute(['post_id' => 5, 'date' => $soon->format('Y-m-d H:i')]);
|
||||
|
||||
self::assertSame('lit_validation_failed', $result->get_error_code());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,19 @@ use WP_Post;
|
||||
|
||||
final class PostTrashAbilityTest extends TestCase
|
||||
{
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->options([]);
|
||||
Functions\when('wp_get_associated_nav_menu_items')->justReturn([]);
|
||||
}
|
||||
|
||||
/** @param array<string, mixed> $options */
|
||||
private function options(array $options): void
|
||||
{
|
||||
Functions\when('get_option')->alias(static fn (string $name, mixed $default = false) => $options[$name] ?? $default);
|
||||
}
|
||||
|
||||
private function ability(ChangeLog $log): PostTrashAbility
|
||||
{
|
||||
Functions\when('get_post')->justReturn(new WP_Post(['ID' => 5, 'post_type' => 'page', 'post_title' => 'Alt', 'post_status' => 'publish']));
|
||||
@@ -93,6 +106,51 @@ final class PostTrashAbilityTest extends TestCase
|
||||
self::assertStringContainsString('bereits im Papierkorb', $result['summary']);
|
||||
}
|
||||
|
||||
public function testWarnsWhenTrashingTheFrontPageOrThePostsPage(): void
|
||||
{
|
||||
$this->options(['show_on_front' => 'page', 'page_on_front' => '5', 'page_for_posts' => '9']);
|
||||
Functions\when('wp_trash_post')->justReturn(new WP_Post(['ID' => 5]));
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->allows('record')->andReturn(3);
|
||||
$ability = $this->ability($log);
|
||||
|
||||
$confirm = $ability->execute(['post_id' => 5]);
|
||||
$result = $ability->execute(['post_id' => 5, 'confirm' => true]);
|
||||
|
||||
self::assertStringContainsString('Startseite', $confirm->get_error_message());
|
||||
self::assertCount(1, $result['warnings']);
|
||||
self::assertStringContainsString('Startseite', $result['warnings'][0]);
|
||||
self::assertTrue($result['ok']);
|
||||
|
||||
$this->options(['show_on_front' => 'page', 'page_on_front' => '9', 'page_for_posts' => '5']);
|
||||
self::assertStringContainsString('Beitragsseite', $ability->execute(['post_id' => 5, 'confirm' => true])['warnings'][0]);
|
||||
|
||||
$this->options(['show_on_front' => 'posts', 'page_on_front' => '5']);
|
||||
self::assertSame([], $ability->execute(['post_id' => 5, 'confirm' => true])['warnings']);
|
||||
}
|
||||
|
||||
public function testWarnsAndNamesTheMenusThatLinkThePost(): void
|
||||
{
|
||||
Functions\when('wp_get_associated_nav_menu_items')->alias(
|
||||
static fn (int $id, string $type): array => $id === 5 && $type === 'post_type' ? [40, 41] : []
|
||||
);
|
||||
Functions\when('wp_get_object_terms')->alias(
|
||||
static fn (array $ids, string $taxonomy, array $args): array =>
|
||||
$ids === [40, 41] && $taxonomy === 'nav_menu' && $args === ['fields' => 'names']
|
||||
? ['Hauptmenü', 'Footer', 'Hauptmenü']
|
||||
: []
|
||||
);
|
||||
Functions\when('wp_trash_post')->justReturn(new WP_Post(['ID' => 5]));
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->allows('record')->andReturn(3);
|
||||
|
||||
$result = $this->ability($log)->execute(['post_id' => 5, 'confirm' => true]);
|
||||
|
||||
self::assertCount(1, $result['warnings']);
|
||||
self::assertStringContainsString('"Hauptmenü", "Footer"', $result['warnings'][0]);
|
||||
self::assertSame(1, substr_count($result['warnings'][0], 'Hauptmenü'));
|
||||
}
|
||||
|
||||
public function testIsDestructiveAndIndirect(): void
|
||||
{
|
||||
$ability = $this->ability(Mockery::mock(ChangeLog::class));
|
||||
|
||||
Reference in New Issue
Block a user