feat: settings page with connection tab, admin notices and site health test
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,97 @@
|
||||
<?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 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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user