Files
WP-LIT-MCP/tests/Unit/Admin/ConnectionStatusTest.php
T

60 lines
2.5 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
{
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']);
}
}