Files
WP-LIT-MCP/tests/Unit/Admin/ConnectionStatusTest.php
T
janniclas.loosenandClaude Opus 5.5 843f45aeec feat: admin settings page UI/UX pass with WordPress-native patterns
- Header: h1 + logo in a wrapping flex row, hr.wp-header-end before the tabs,
  aria-current on the active tab; own CSS/JS enqueued only on this screen.
- Verbindung: ordered "So verbindest du Claude" steps, copy button (core
  clipboard.js + wp.a11y.speak), Site-Health-style status list, German details.
- Zugriff: roles as form-table fieldset, connections with site date format,
  empty-state row, confirm on revoke; ConnectionUsage records last use via
  WP_Application_Passwords::record_application_password_usage (once a day).
- Tools: plain-language legend, counter as inline notice, groups as core
  postboxes (postbox.js toggles, per-user closed state, summary when closed),
  fixed column widths with new "Art" column, submit above and below.
- Protokoll: site-timezone times with UTC tooltip, result icons, filters with
  labels in the list table nav, retention link to Limits.
- Limits: short labels with units and descriptions; snapshot count hidden but
  preserved.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 16:50:58 +02:00

95 lines
4.1 KiB
PHP

<?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
{
protected function setUp(): void
{
parent::setUp();
Functions\stubTranslationFunctions();
}
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['OAuth-Discovery']['ok']);
self::assertNull($byLabel['MCP-Endpunkt']['ok']);
}
public function testDiscoveryDetailIsGermanInsteadOfTheLibrarysEnglishLabel(): void
{
Functions\when('get_transient')->alias(static fn (string $key) => $key === 'lit_mcp_endpoint_check'
? false
: ['label' => 'MCP OAuth discovery documents', 'status' => 'good']);
Functions\when('home_url')->justReturn('https://kunde.test');
$good = array_column((new ConnectionStatus(static fn (): bool => true))->checks(false), null, 'label');
self::assertTrue($good['OAuth-Discovery']['ok']);
self::assertSame('Discovery-Dokumente erreichbar', $good['OAuth-Discovery']['message']);
Functions\when('get_transient')->alias(static fn (string $key) => $key === 'lit_mcp_endpoint_check'
? false
: ['label' => 'MCP OAuth discovery documents', 'status' => 'critical']);
$bad = array_column((new ConnectionStatus(static fn (): bool => true))->checks(false), null, 'label');
self::assertFalse($bad['OAuth-Discovery']['ok']);
self::assertStringContainsString('nicht erreichbar', $bad['OAuth-Discovery']['message']);
self::assertStringNotContainsString('documents', $bad['OAuth-Discovery']['message']);
}
public function testPermalinksPrettyReflectsThePermalinkStructureOption(): void
{
Functions\when('get_option')->justReturn('/%postname%/');
self::assertTrue((new ConnectionStatus())->permalinksPretty());
Functions\when('get_option')->justReturn('');
self::assertFalse((new ConnectionStatus())->permalinksPretty());
}
}