WriteFailed maps to lit_write_failed / lit_partial_write (partial writes are logged with result partial); old template read before the update; row marker set before wp_update_post; created terms deleted on rollback; parent must be a readable non-descendant of a hierarchical type; featured image must be readable; snapshot keeps empty values; meta comparison after sanitize_meta; follow-up failures (listener, change log) only add warnings. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01At3V6SjwbMe6eMzVGW3Hs7
189 lines
7.2 KiB
PHP
189 lines
7.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace LIT\MCP\Tests\Unit\Abilities;
|
|
|
|
use Brain\Monkey\Functions;
|
|
use LIT\MCP\Abilities\Ability;
|
|
use LIT\MCP\Abilities\AbilityRegistrar;
|
|
use LIT\MCP\Abilities\WriteFailed;
|
|
use LIT\MCP\Integrations\Integration;
|
|
use LIT\MCP\Integrations\IntegrationRegistry;
|
|
use LIT\MCP\Server\RequestContext;
|
|
use LIT\MCP\Tests\Unit\TestCase;
|
|
use Mockery;
|
|
use RuntimeException;
|
|
use WP_Error;
|
|
|
|
final class AbilityRegistrarTest extends TestCase
|
|
{
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
// Default to "inside the OAuth server request" so tests unrelated to this
|
|
// gate keep exercising the ability's own permission() as before.
|
|
RequestContext::reset(true);
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
RequestContext::reset();
|
|
parent::tearDown();
|
|
}
|
|
|
|
private function registrarFor(Ability $ability): AbilityRegistrar
|
|
{
|
|
$integration = Mockery::mock(Integration::class);
|
|
$integration->allows('id')->andReturn('core');
|
|
$integration->allows('isAvailable')->andReturn(true);
|
|
$integration->allows('abilities')->andReturn([$ability]);
|
|
|
|
return new AbilityRegistrar(new IntegrationRegistry($integration));
|
|
}
|
|
|
|
private function ability(): Mockery\MockInterface
|
|
{
|
|
$ability = Mockery::mock(Ability::class);
|
|
$ability->allows('name')->andReturn('lit/test');
|
|
$ability->allows('label')->andReturn('Test');
|
|
$ability->allows('description')->andReturn('Test ability');
|
|
$ability->allows('inputSchema')->andReturn(['type' => 'object', 'properties' => []]);
|
|
$ability->allows('outputSchema')->andReturn(['type' => 'object']);
|
|
$ability->allows('annotations')->andReturn(['readonly' => true]);
|
|
$ability->allows('isDirect')->andReturn(true);
|
|
|
|
return $ability;
|
|
}
|
|
|
|
public function testArgsExposeAbilityPubliclyToMcpWithAnnotations(): void
|
|
{
|
|
$args = $this->registrarFor($this->ability())->argsFor($this->ability());
|
|
|
|
self::assertSame('Test', $args['label']);
|
|
self::assertSame(AbilityRegistrar::CATEGORY, $args['category']);
|
|
self::assertSame(['type' => 'object'], $args['output_schema']);
|
|
self::assertTrue($args['meta']['mcp']['public']);
|
|
self::assertSame(['readonly' => true], $args['meta']['annotations']);
|
|
self::assertFalse($args['meta']['show_in_rest']);
|
|
}
|
|
|
|
public function testPermissionDeniedWithoutLitCapability(): void
|
|
{
|
|
Functions\when('current_user_can')->justReturn(false);
|
|
$ability = $this->ability();
|
|
$ability->shouldNotReceive('permission');
|
|
|
|
$result = ($this->registrarFor($ability)->argsFor($ability)['permission_callback'])([]);
|
|
|
|
self::assertInstanceOf(WP_Error::class, $result);
|
|
self::assertSame('lit_forbidden', $result->get_error_code());
|
|
}
|
|
|
|
public function testPermissionDeniedOutsideOAuthServerRequest(): void
|
|
{
|
|
Functions\when('current_user_can')->justReturn(true);
|
|
RequestContext::reset(false);
|
|
$ability = $this->ability();
|
|
$ability->shouldNotReceive('permission');
|
|
|
|
$result = ($this->registrarFor($ability)->argsFor($ability)['permission_callback'])([]);
|
|
|
|
self::assertInstanceOf(WP_Error::class, $result);
|
|
self::assertSame('lit_forbidden', $result->get_error_code());
|
|
}
|
|
|
|
public function testPermissionDelegatesToAbilityWhenGatePasses(): void
|
|
{
|
|
Functions\when('current_user_can')->justReturn(true);
|
|
$ability = $this->ability();
|
|
$ability->expects('permission')->with(['x' => 1])->andReturn(true);
|
|
|
|
$result = ($this->registrarFor($ability)->argsFor($ability)['permission_callback'])(['x' => 1]);
|
|
|
|
self::assertTrue($result);
|
|
}
|
|
|
|
public function testNullInputIsPassedAsEmptyArray(): void
|
|
{
|
|
$ability = $this->ability();
|
|
$ability->expects('execute')->with([])->andReturn(['ok' => true]);
|
|
|
|
$result = ($this->registrarFor($ability)->argsFor($ability)['execute_callback'])(null);
|
|
|
|
self::assertSame(['ok' => true], $result);
|
|
}
|
|
|
|
public function testPermissionExceptionBecomesNeutralInternalError(): void
|
|
{
|
|
Functions\when('current_user_can')->justReturn(true);
|
|
// error_log() is a PHP internal and cannot be stubbed; redirect it to a temp file instead.
|
|
$log = (string) tempnam(sys_get_temp_dir(), 'lit-mcp-log');
|
|
$previous = ini_set('error_log', $log);
|
|
$ability = $this->ability();
|
|
$ability->expects('permission')->andThrow(new RuntimeException('secret path /var/www/x.php'));
|
|
|
|
try {
|
|
$result = ($this->registrarFor($ability)->argsFor($ability)['permission_callback'])([]);
|
|
} finally {
|
|
ini_set('error_log', (string) $previous);
|
|
}
|
|
|
|
self::assertInstanceOf(WP_Error::class, $result);
|
|
self::assertSame('lit_internal_error', $result->get_error_code());
|
|
self::assertStringNotContainsString('/var/www', $result->get_error_message());
|
|
self::assertStringContainsString('lit/test failed', (string) file_get_contents($log));
|
|
unlink($log);
|
|
}
|
|
|
|
public function testExceptionBecomesNeutralInternalError(): void
|
|
{
|
|
// error_log() is a PHP internal and cannot be stubbed; redirect it to a temp file instead.
|
|
$log = (string) tempnam(sys_get_temp_dir(), 'lit-mcp-log');
|
|
$previous = ini_set('error_log', $log);
|
|
$ability = $this->ability();
|
|
$ability->expects('execute')->andThrow(new RuntimeException('secret path /var/www/x.php'));
|
|
|
|
try {
|
|
$result = ($this->registrarFor($ability)->argsFor($ability)['execute_callback'])([]);
|
|
} finally {
|
|
ini_set('error_log', (string) $previous);
|
|
}
|
|
|
|
self::assertInstanceOf(WP_Error::class, $result);
|
|
self::assertSame('lit_internal_error', $result->get_error_code());
|
|
self::assertStringNotContainsString('/var/www', $result->get_error_message());
|
|
self::assertStringContainsString('lit/test failed', (string) file_get_contents($log));
|
|
unlink($log);
|
|
}
|
|
|
|
public function testWriteFailedBecomesItsOwnErrorAndTheDetailIsLogged(): void
|
|
{
|
|
$log = (string) tempnam(sys_get_temp_dir(), 'lit-mcp-log');
|
|
$previous = ini_set('error_log', $log);
|
|
$ability = $this->ability();
|
|
$ability->expects('execute')->andThrow(
|
|
new WriteFailed('Felder konnten nicht gespeichert werden.', false, 5, 811, new RuntimeException('meta /var/www'))
|
|
);
|
|
|
|
try {
|
|
$result = ($this->registrarFor($ability)->argsFor($ability)['execute_callback'])([]);
|
|
} finally {
|
|
ini_set('error_log', (string) $previous);
|
|
}
|
|
|
|
self::assertSame('lit_partial_write', $result->get_error_code());
|
|
self::assertStringNotContainsString('/var/www', $result->get_error_message());
|
|
self::assertStringContainsString('meta /var/www', (string) file_get_contents($log));
|
|
unlink($log);
|
|
}
|
|
|
|
public function testRegisterAbilitiesCallsWordPressForEachAbility(): void
|
|
{
|
|
$ability = $this->ability();
|
|
Functions\expect('wp_register_ability')->once()->with('lit/test', Mockery::type('array'));
|
|
|
|
$this->registrarFor($ability)->registerAbilities();
|
|
}
|
|
}
|