Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
115 lines
4.1 KiB
PHP
115 lines
4.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace LIT\MCP\Tests\Unit\Admin;
|
|
|
|
use Brain\Monkey\Functions;
|
|
use LIT\MCP\Admin\AdminTab;
|
|
use LIT\MCP\Admin\SettingsPage;
|
|
use LIT\MCP\Tests\Unit\TestCase;
|
|
use Mockery;
|
|
|
|
final class SettingsPageTest extends TestCase
|
|
{
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
Functions\stubEscapeFunctions();
|
|
Functions\stubTranslationFunctions();
|
|
Functions\when('admin_url')->alias(static fn (string $path = ''): string => 'https://kunde.de/wp-admin/' . $path);
|
|
Functions\when('add_query_arg')->alias(static fn (array $args, string $url): string => $url . '?' . http_build_query($args));
|
|
Functions\when('sanitize_key')->alias(static fn (string $key): string => strtolower((string) preg_replace('/[^a-z0-9_\-]/i', '', $key)));
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
unset($_GET['tab'], $_GET['lit_mcp_notice']);
|
|
parent::tearDown();
|
|
}
|
|
|
|
private function tab(string $slug, string $label): AdminTab
|
|
{
|
|
$tab = Mockery::mock(AdminTab::class);
|
|
$tab->allows('slug')->andReturn($slug);
|
|
$tab->allows('label')->andReturn($label);
|
|
$tab->allows('registerActions');
|
|
$tab->allows('render')->andReturnUsing(static function () use ($slug): void {
|
|
echo '<p>Inhalt ' . $slug . '</p>';
|
|
});
|
|
|
|
return $tab;
|
|
}
|
|
|
|
public function testRendersTabsAndTheRequestedTab(): void
|
|
{
|
|
Functions\when('current_user_can')->justReturn(true);
|
|
$_GET['tab'] = 'zugriff';
|
|
$page = new SettingsPage([$this->tab('verbindung', 'Verbindung'), $this->tab('zugriff', 'Zugriff')]);
|
|
|
|
ob_start();
|
|
$page->render();
|
|
$html = (string) ob_get_clean();
|
|
|
|
self::assertStringContainsString('Inhalt zugriff', $html);
|
|
self::assertStringNotContainsString('Inhalt verbindung', $html);
|
|
self::assertMatchesRegularExpression('/tab=zugriff" class="nav-tab nav-tab-active"/', $html);
|
|
self::assertSame(['verbindung', 'zugriff'], $page->tabSlugs());
|
|
}
|
|
|
|
public function testRendersTheLogoTopRightOnlyWhenGiven(): void
|
|
{
|
|
Functions\when('current_user_can')->justReturn(true);
|
|
Functions\when('esc_url')->returnArg();
|
|
|
|
ob_start();
|
|
(new SettingsPage([$this->tab('verbindung', 'Verbindung')], 'https://example.test/logo.svg'))->render();
|
|
$withLogo = (string) ob_get_clean();
|
|
ob_start();
|
|
(new SettingsPage([$this->tab('verbindung', 'Verbindung')]))->render();
|
|
$withoutLogo = (string) ob_get_clean();
|
|
|
|
self::assertStringContainsString('<img src="https://example.test/logo.svg" alt="Loosen-IT Logo"', $withLogo);
|
|
self::assertStringContainsString('right:0', $withLogo);
|
|
self::assertStringNotContainsString('lit-mcp-logo', $withoutLogo);
|
|
}
|
|
|
|
public function testUnknownTabFallsBackToTheFirstAndNoticesAreShown(): void
|
|
{
|
|
Functions\when('current_user_can')->justReturn(true);
|
|
$_GET['tab'] = '../../evil';
|
|
$_GET['lit_mcp_notice'] = 'revoked';
|
|
$page = new SettingsPage([$this->tab('verbindung', 'Verbindung')]);
|
|
|
|
ob_start();
|
|
$page->render();
|
|
$html = (string) ob_get_clean();
|
|
|
|
self::assertSame('verbindung', $page->currentTab());
|
|
self::assertStringContainsString('Inhalt verbindung', $html);
|
|
self::assertStringContainsString('notice-success', $html);
|
|
}
|
|
|
|
public function testNothingIsRenderedWithoutManageOptions(): void
|
|
{
|
|
Functions\when('current_user_can')->justReturn(false);
|
|
|
|
ob_start();
|
|
(new SettingsPage([$this->tab('verbindung', 'Verbindung')]))->render();
|
|
|
|
self::assertSame('', ob_get_clean());
|
|
}
|
|
|
|
public function testRegisterHooksMenuSettingsAndTabActions(): void
|
|
{
|
|
$tab = Mockery::mock(AdminTab::class);
|
|
$tab->allows('slug')->andReturn('verbindung');
|
|
$tab->expects('registerActions')->once();
|
|
$page = new SettingsPage([$tab]);
|
|
Functions\expect('add_action')->once()->with('admin_menu', [$page, 'addMenu']);
|
|
Functions\expect('add_action')->once()->with('admin_init', [$page, 'registerSettings']);
|
|
|
|
$page->register();
|
|
}
|
|
}
|