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,47 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace LIT\MCP\Tests\Unit\Admin;
|
||||
|
||||
use Brain\Monkey\Functions;
|
||||
use LIT\MCP\Admin\AdminNotices;
|
||||
use LIT\MCP\Admin\ConnectionStatus;
|
||||
use LIT\MCP\Tests\Unit\TestCase;
|
||||
use Mockery;
|
||||
|
||||
final class AdminNoticesTest extends TestCase
|
||||
{
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
Functions\stubTranslationFunctions();
|
||||
}
|
||||
|
||||
private function status(bool $oauth, ?array $discovery, bool $https): ConnectionStatus
|
||||
{
|
||||
$status = Mockery::mock(ConnectionStatus::class);
|
||||
$status->allows('oauthEnabled')->andReturn($oauth);
|
||||
$status->allows('discovery')->with(false)->andReturn($discovery);
|
||||
$status->allows('https')->andReturn($https);
|
||||
|
||||
return $status;
|
||||
}
|
||||
|
||||
public function testWarnsAboutDisabledOAuthAndBrokenDiscoveryEverywhere(): void
|
||||
{
|
||||
$messages = (new AdminNotices($this->status(false, ['status' => 'critical'], true)))->messages(false);
|
||||
|
||||
self::assertCount(2, $messages);
|
||||
self::assertStringContainsString('OAuth-Server ist deaktiviert', $messages[0]);
|
||||
self::assertStringContainsString('/.well-known/', $messages[1]);
|
||||
}
|
||||
|
||||
public function testHttpsHintOnlyOnTheSettingsPage(): void
|
||||
{
|
||||
$notices = new AdminNotices($this->status(true, null, false));
|
||||
|
||||
self::assertSame([], $notices->messages(false));
|
||||
self::assertCount(1, $notices->messages(true));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace LIT\MCP\Tests\Unit\Admin;
|
||||
|
||||
use Brain\Monkey\Functions;
|
||||
use LIT\MCP\Admin\ConnectionStatus;
|
||||
use LIT\MCP\Tests\Unit\TestCase;
|
||||
use Mockery;
|
||||
|
||||
final class ConnectionStatusTest extends TestCase
|
||||
{
|
||||
public function testEndpointCheckExpects401WithBearerChallengeAndCachesIt(): void
|
||||
{
|
||||
Functions\when('get_transient')->justReturn(false);
|
||||
Functions\when('rest_url')->justReturn('https://kunde.de/wp-json/mcp/mcp-oauth-server');
|
||||
Functions\expect('wp_remote_post')->once()->with('https://kunde.de/wp-json/mcp/mcp-oauth-server', Mockery::type('array'))
|
||||
->andReturn(['response' => ['code' => 401]]);
|
||||
Functions\when('wp_remote_retrieve_response_code')->justReturn(401);
|
||||
Functions\when('wp_remote_retrieve_header')->justReturn('Bearer realm="https://kunde.de"');
|
||||
Functions\expect('set_transient')->once()
|
||||
->with('lit_mcp_endpoint_check', Mockery::on(static fn (array $value): bool => $value['ok'] === true), 300);
|
||||
|
||||
$result = (new ConnectionStatus(static fn (): bool => true))->endpoint(true);
|
||||
|
||||
self::assertTrue($result['ok']);
|
||||
}
|
||||
|
||||
public function testEndpointAnsweringWithoutChallengeIsReported(): void
|
||||
{
|
||||
Functions\when('get_transient')->justReturn(false);
|
||||
Functions\when('rest_url')->justReturn('https://kunde.de/wp-json/mcp/mcp-oauth-server');
|
||||
Functions\when('wp_remote_post')->justReturn(['response' => ['code' => 200]]);
|
||||
Functions\when('wp_remote_retrieve_response_code')->justReturn(200);
|
||||
Functions\when('wp_remote_retrieve_header')->justReturn('');
|
||||
Functions\when('set_transient')->justReturn(true);
|
||||
|
||||
$result = (new ConnectionStatus(static fn (): bool => true))->endpoint(true);
|
||||
|
||||
self::assertFalse($result['ok']);
|
||||
self::assertStringContainsString('200', $result['message']);
|
||||
}
|
||||
|
||||
public function testChecksWithoutFetchingUseOnlyCachedResults(): void
|
||||
{
|
||||
Functions\when('get_transient')->justReturn(false);
|
||||
Functions\expect('wp_remote_post')->never();
|
||||
Functions\when('home_url')->justReturn('http://kunde.test');
|
||||
|
||||
$checks = (new ConnectionStatus(static fn (): bool => false))->checks(false);
|
||||
|
||||
$byLabel = array_column($checks, null, 'label');
|
||||
self::assertFalse($byLabel['OAuth-Server']['ok']);
|
||||
self::assertFalse($byLabel['HTTPS']['ok']);
|
||||
self::assertNull($byLabel['Discovery (/.well-known/)']['ok']);
|
||||
self::assertNull($byLabel['Endpunkt ohne Token']['ok']);
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace LIT\MCP\Tests\Unit\Admin;
|
||||
|
||||
use Brain\Monkey\Functions;
|
||||
use LIT\MCP\Admin\ConnectionStatus;
|
||||
use LIT\MCP\Admin\SiteHealth;
|
||||
use LIT\MCP\Tests\Unit\TestCase;
|
||||
use Mockery;
|
||||
|
||||
final class SiteHealthTest 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));
|
||||
}
|
||||
|
||||
public function testAddsADirectTest(): void
|
||||
{
|
||||
$health = new SiteHealth(Mockery::mock(ConnectionStatus::class));
|
||||
|
||||
$tests = $health->addTest(['direct' => []]);
|
||||
|
||||
self::assertSame([$health, 'run'], $tests['direct']['lit_mcp_endpoint']['test']);
|
||||
}
|
||||
|
||||
public function testGoodWhenOAuthIsOnAndTheEndpointChallenges(): void
|
||||
{
|
||||
$status = Mockery::mock(ConnectionStatus::class);
|
||||
$status->allows('oauthEnabled')->andReturn(true);
|
||||
$status->allows('endpoint')->with(true)->andReturn(['ok' => true, 'message' => '401 mit WWW-Authenticate']);
|
||||
|
||||
$result = (new SiteHealth($status))->run();
|
||||
|
||||
self::assertSame('good', $result['status']);
|
||||
self::assertSame('lit_mcp_endpoint', $result['test']);
|
||||
}
|
||||
|
||||
public function testCriticalWhenOAuthIsDisabled(): void
|
||||
{
|
||||
$status = Mockery::mock(ConnectionStatus::class);
|
||||
$status->allows('oauthEnabled')->andReturn(false);
|
||||
|
||||
$result = (new SiteHealth($status))->run();
|
||||
|
||||
self::assertSame('critical', $result['status']);
|
||||
self::assertStringContainsString('deaktiviert', $result['description']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user