Proactively unhook wp-media/mcp-oauth's own mcp_adapter_init callback before creating our server, rather than letting it collide and rely on WP_DEBUG=true being harmless. The collision previously surfaced as a fatal-looking _doing_it_wrong notice under WP-CLI (breaking every wp command while the plugin is active) and could leak debug notices into HTTP responses on customer sites that run WP_DEBUG. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01At3V6SjwbMe6eMzVGW3Hs7
121 lines
4.2 KiB
PHP
121 lines
4.2 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
namespace LIT\MCP\Tests\Unit\Server;
|
||
|
||
use Brain\Monkey\Functions;
|
||
use LIT\MCP\Abilities\Ability;
|
||
use LIT\MCP\Guides\InstructionsBuilder;
|
||
use LIT\MCP\Integrations\Integration;
|
||
use LIT\MCP\Integrations\IntegrationRegistry;
|
||
use LIT\MCP\Server\ServerRegistrar;
|
||
use LIT\MCP\Tests\Unit\TestCase;
|
||
use Mockery;
|
||
use WPMedia\MCP\OAuth\Transport\ServerRegistrar as OAuthServerRegistrar;
|
||
|
||
final class ServerRegistrarTest extends TestCase
|
||
{
|
||
protected function tearDown(): void
|
||
{
|
||
unset($GLOBALS['wp_filter']);
|
||
parent::tearDown();
|
||
}
|
||
|
||
private function registry(): IntegrationRegistry
|
||
{
|
||
$direct = Mockery::mock(Ability::class);
|
||
$direct->allows('name')->andReturn('lit/guide');
|
||
$direct->allows('isDirect')->andReturn(true);
|
||
$indirect = Mockery::mock(Ability::class);
|
||
$indirect->allows('name')->andReturn('lit/rare-tool');
|
||
$indirect->allows('isDirect')->andReturn(false);
|
||
|
||
$core = Mockery::mock(Integration::class);
|
||
$core->allows('id')->andReturn('core');
|
||
$core->allows('isAvailable')->andReturn(true);
|
||
$core->allows('abilities')->andReturn([$direct, $indirect]);
|
||
|
||
return new IntegrationRegistry($core);
|
||
}
|
||
|
||
private function instructions(): InstructionsBuilder
|
||
{
|
||
// InstructionsBuilder is final (not mockable) and pure – use the real one.
|
||
return new InstructionsBuilder($this->registry());
|
||
}
|
||
|
||
public function testServerUsesOauthRouteDirectToolsPlusMetaToolsAndInstructions(): void
|
||
{
|
||
$args = (new ServerRegistrar($this->registry(), $this->instructions(), static fn () => true))->serverArguments();
|
||
|
||
self::assertSame('mcp-oauth-server', $args['id']);
|
||
self::assertSame('mcp', $args['namespace']);
|
||
self::assertSame('mcp-oauth-server', $args['route']);
|
||
self::assertStringContainsString('Aktive Integrationen: core', $args['description']);
|
||
self::assertSame(
|
||
['lit/guide', 'mcp-adapter/discover-abilities', 'mcp-adapter/get-ability-info', 'mcp-adapter/execute-ability'],
|
||
$args['tools']
|
||
);
|
||
self::assertNotContains('lit/rare-tool', $args['tools']);
|
||
}
|
||
|
||
public function testNoServerWhenOauthIsDisabled(): void
|
||
{
|
||
$registrar = new ServerRegistrar($this->registry(), $this->instructions(), static fn () => false);
|
||
|
||
self::assertNull($registrar->serverArguments());
|
||
}
|
||
|
||
public function testCreateServerOutsideAdapterInitDoesNothing(): void
|
||
{
|
||
Functions\when('doing_action')->justReturn(false);
|
||
$oauthChecked = false;
|
||
$registrar = new ServerRegistrar(
|
||
$this->registry(),
|
||
$this->instructions(),
|
||
static function () use (&$oauthChecked): bool {
|
||
$oauthChecked = true;
|
||
return true;
|
||
}
|
||
);
|
||
|
||
$registrar->createServer();
|
||
|
||
self::assertFalse($oauthChecked, 'createServer must bail out before touching the adapter');
|
||
}
|
||
|
||
public function testUnhookOAuthDefaultServerRemovesOnlyTheMcpOauthCallback(): void
|
||
{
|
||
$oauthRegistrar = Mockery::mock(OAuthServerRegistrar::class);
|
||
$oauthCallback = [$oauthRegistrar, 'register_server'];
|
||
$unrelatedCallback = static function (): void {
|
||
};
|
||
|
||
$hook = new class {
|
||
/** @var array<int, array<string, array{function: mixed, accepted_args: int}>> */
|
||
public array $callbacks = [];
|
||
};
|
||
$hook->callbacks = [
|
||
5 => ['unrelated-id' => ['function' => $unrelatedCallback, 'accepted_args' => 0]],
|
||
10 => ['oauth-id' => ['function' => $oauthCallback, 'accepted_args' => 0]],
|
||
];
|
||
$GLOBALS['wp_filter']['mcp_adapter_init'] = $hook;
|
||
|
||
Functions\expect('remove_action')
|
||
->once()
|
||
->with('mcp_adapter_init', $oauthCallback, 10);
|
||
|
||
(new ServerRegistrar($this->registry(), $this->instructions()))->unhookOAuthDefaultServer();
|
||
}
|
||
|
||
public function testUnhookOAuthDefaultServerIsNoOpWhenHookIsMissing(): void
|
||
{
|
||
unset($GLOBALS['wp_filter']['mcp_adapter_init']);
|
||
|
||
Functions\expect('remove_action')->never();
|
||
|
||
(new ServerRegistrar($this->registry(), $this->instructions()))->unhookOAuthDefaultServer();
|
||
}
|
||
}
|