- tests/Support/FakeChangeLog extends ChangeLog: records record()/recordPartial() calls, increasing ids, optional failure; new FakeChangeLog(id) / partialsOnly() / any(id) / forbidden() mirror what the replaced mocks allowed. assertRecorded() / assertPartials() check count and the named arguments. Calls a fake does not allow throw and are also verified in TestCase::tearDown (one assertion per test that uses a fake), so swallowed change-log calls still fail like a never() expectation. - All ChangeLog mocks of ability/integration tests replaced (the admin tests that stub query/purge/clear keep their mocks); expectException tests that checked the partial log now catch and assert. - SchemaFixture::managed(?divi) replaces the 21 isManaged-only ContentSchema mocks; DiviFixtures::schema() uses it. Tests 1397 -> 1404 (FakeChangeLogTest); no test has fewer assertions. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
71 lines
2.6 KiB
PHP
71 lines
2.6 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\MenuCreateAbility;
|
|
use LIT\MCP\Tests\Support\FakeChangeLog;
|
|
use LIT\MCP\Tests\Unit\TestCase;
|
|
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(FakeChangeLog::forbidden());
|
|
$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(FakeChangeLog::forbidden()))->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 = new FakeChangeLog(5);
|
|
|
|
$result = (new MenuCreateAbility($log))->execute(['name' => 'Neu', 'location' => 'primary-menu']);
|
|
|
|
self::assertSame(8, $result['menu_id']);
|
|
self::assertSame('created', $result['changed'][0]['action']);
|
|
self::assertSame(5, $result['log_id']);
|
|
$log->assertRecorded([
|
|
'ability' => 'lit/menu-create',
|
|
'object_type' => 'nav_menu',
|
|
'object_id' => '8',
|
|
'summary' => 'Menü "Neu" angelegt (Position primary-menu)',
|
|
]);
|
|
}
|
|
|
|
public function testPermissionNeedsThemeOptionRights(): void
|
|
{
|
|
Functions\when('current_user_can')->justReturn(false);
|
|
|
|
$ability = new MenuCreateAbility(FakeChangeLog::forbidden());
|
|
|
|
self::assertSame('lit_forbidden', $ability->permission(['name' => 'Neu'])->get_error_code());
|
|
}
|
|
}
|