Files
WP-LIT-MCP/tests/Unit/Admin/SettingsPageTest.php
T
Jan-Niclas LoosenandClaude Sonnet 5.5 96fa58188b feat(admin): Einstellungsseite überarbeitet, auch für das Handy
Verbindung: Probleme vor der Anleitung. Tools: Suche, Filter, Alle ein-/ausklappen, Zurücksetzen je Gruppe.
Divi-Reiter in Klartext mit Statussymbolen. Rollenhinweise, Hilfe-Tab, Reiter als Raster und Karten auf dem Handy,
Kontrast, Tippflächen, einheitliche Du-Anrede. Bestätigungen über data-lit-mcp-confirm statt Inline-onclick.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-05 16:36:16 +01:00

229 lines
9.4 KiB
PHP

<?php
declare(strict_types=1);
namespace LIT\MCP\Tests\Unit\Admin;
use Brain\Monkey\Filters;
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" aria-current="page"/', $html);
self::assertMatchesRegularExpression('/tab=verbindung" class="nav-tab">/', $html);
self::assertSame(['verbindung', 'zugriff'], $page->tabSlugs());
}
/** Integrations add their tabs in Bootable::boot() (Divi: "Divi", last); the filter runs once, on first use. */
public function testIntegrationsAppendTheirTabsThroughTheFilter(): void
{
$divi = Mockery::mock(AdminTab::class);
$divi->allows('slug')->andReturn('divi');
$divi->expects('registerActions')->once();
Filters\expectApplied(SettingsPage::TABS_FILTER)->once()->andReturnUsing(
static fn (array $tabs): array => [...$tabs, $divi]
);
$page = new SettingsPage([$this->tab('verbindung', 'Verbindung'), $this->tab('zugriff', 'Zugriff')]);
Functions\when('add_action')->justReturn(true);
$page->register();
self::assertSame(['verbindung', 'zugriff', 'divi'], $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::assertStringNotContainsString('lit-mcp-logo', $withoutLogo);
// Logo sits in the header next to the h1 (flex, no absolute positioning over the title).
self::assertLessThan(strpos($withLogo, 'lit-mcp-logo'), strpos($withLogo, '<h1'));
self::assertStringNotContainsString('position:absolute', $withLogo);
}
public function testHeaderEndMarkerSitsBetweenTitleAndTabsWithNoticesAfterIt(): void
{
Functions\when('current_user_can')->justReturn(true);
$_GET['lit_mcp_notice'] = 'revoked';
ob_start();
(new SettingsPage([$this->tab('verbindung', 'Verbindung')]))->render();
$html = (string) ob_get_clean();
$h1 = (int) strpos($html, '<h1');
$end = strpos($html, '<hr class="wp-header-end">');
self::assertNotFalse($end);
self::assertLessThan($end, $h1);
self::assertLessThan(strpos($html, 'notice-success'), $end);
self::assertLessThan(strpos($html, 'nav-tab-wrapper'), (int) strpos($html, 'notice-success'));
self::assertStringContainsString('<nav class="nav-tab-wrapper wp-clearfix" aria-label="', $html);
}
public function testEnqueuesStylesAndCopyScriptOnlyOnItsOwnScreen(): void
{
$page = new SettingsPage([$this->tab('verbindung', 'Verbindung')], '', 'https://example.test/assets', '1.2.3');
Functions\expect('wp_enqueue_style')->once()
->with('lit-mcp-admin', 'https://example.test/assets/css/admin.css', [], '1.2.3');
Functions\expect('wp_enqueue_script')->once()
->with('lit-mcp-admin', 'https://example.test/assets/js/admin.js', ['clipboard', 'wp-a11y'], '1.2.3', true);
Functions\when('wp_localize_script')->justReturn(true);
$page->enqueueAssets('settings_page_lit-mcp');
$page->enqueueAssets('options-general.php');
}
public function testToolsTabAlsoLoadsWordPressPostboxScriptForCollapsibleGroups(): void
{
$_GET['tab'] = 'tools';
$page = new SettingsPage([$this->tab('verbindung', 'V'), $this->tab('tools', 'T')], '', 'https://example.test/assets/', '1');
Functions\when('wp_enqueue_style')->justReturn(null);
Functions\expect('wp_enqueue_script')->once()
->with('lit-mcp-admin', 'https://example.test/assets/js/admin.js', ['clipboard', 'wp-a11y', 'postbox'], '1', true);
Functions\when('wp_localize_script')->justReturn(true);
$page->enqueueAssets('settings_page_lit-mcp');
}
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']);
Functions\expect('add_action')->once()->with('admin_enqueue_scripts', [$page, 'enqueueAssets']);
$page->register();
}
/** @dataProvider diviNotices */
public function testDiviNoticesAreShown(string $key, string $class, string $text): void
{
Functions\when('current_user_can')->justReturn(true);
$_GET['lit_mcp_notice'] = $key;
ob_start();
(new SettingsPage([$this->tab('verbindung', 'Verbindung')]))->render();
$html = (string) ob_get_clean();
self::assertStringContainsString('notice-' . $class, $html);
self::assertStringContainsString($text, $html);
}
/** @return list<list<string>> */
public static function diviNotices(): array
{
return [
['divi_checked', 'success', 'Divi-Prüfung abgeschlossen'],
['divi_check_running', 'warning', 'bereits eine Divi-Prüfung'],
['divi_check_failed', 'error', 'PHP-Error-Log'],
['divi_released', 'success', 'für LIT MCP freigegeben'],
['divi_release_refused', 'error', 'Prüfung erneut ausführen'],
['divi_withdrawn', 'success', 'Freigabe zurückgezogen'],
['divi_withdraw_failed', 'error', 'keine Freigabe'],
];
}
public function testTheNoticeParameterIsRemovedFromTheAddressBarAfterwards(): void
{
$page = new SettingsPage([$this->tab('verbindung', 'Verbindung')]);
self::assertSame(['a', 'lit_mcp_notice'], $page->removableQueryArgs(['a']));
}
public function testAddsAnOverviewHelpTab(): void
{
Functions\stubEscapeFunctions();
Functions\stubTranslationFunctions();
$screen = Mockery::mock();
$screen->expects('add_help_tab')->once()->with(Mockery::on(
static fn (array $tab): bool => $tab['id'] === 'lit-mcp-overview'
&& str_contains($tab['content'], 'Tab „Verbindung“')
&& str_contains($tab['content'], 'Tab „Tools“')
));
(new SettingsPage([$this->tab('verbindung', 'Verbindung')]))->helpTabFor($screen);
}
}