tests/Support/AssertsToolErrors (used by TestCase): assertToolError($result, code)
checks the WP_Error code lit_<code> and the code of the client JSON and returns the
payload; clientError() decodes the payload; errorFields() returns the fields and
asserts they exist. Replaces the private clientError() decoders of
DiviWriteAbilitiesTest/DiviReplaceTextAbilityTest, 216 single-line
assertSame('lit_…', …->get_error_code()) assertions, 24 get_error_data()['fields']
digs and two json_decode(message) reads. ErrorsTest keeps json_decode: it tests
the format itself. Tests 1404 -> 1409 (AssertsToolErrorsTest); no test has fewer
assertions.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
86 lines
3.1 KiB
PHP
86 lines
3.1 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
namespace LIT\MCP\Tests\Unit\Integrations\WordPress;
|
||
|
||
use LIT\MCP\Abilities\Ability;
|
||
use Brain\Monkey\Functions;
|
||
use LIT\MCP\Integrations\WordPress\Content\ContentComposer;
|
||
use LIT\MCP\Integrations\WordPress\Schema\ContentSchema;
|
||
use LIT\MCP\Integrations\WordPress\WordPressIntegration;
|
||
use LIT\MCP\Tests\Support\FakeChangeLog;
|
||
use LIT\MCP\Tests\Support\SchemaFixture;
|
||
use LIT\MCP\Tests\Unit\TestCase;
|
||
use Mockery;
|
||
|
||
final class WordPressIntegrationTest extends TestCase
|
||
{
|
||
/** Grows with every task that adds abilities – keep in the order of abilities(). */
|
||
private const EXPECTED = [
|
||
'lit/content-types',
|
||
'lit/content-query',
|
||
'lit/content-get',
|
||
'lit/content-create',
|
||
'lit/content-update',
|
||
'lit/post-publish',
|
||
'lit/post-unpublish',
|
||
'lit/post-schedule',
|
||
'lit/post-trash',
|
||
'lit/post-duplicate',
|
||
'lit/post-replace-with-copy',
|
||
'lit/revisions-list',
|
||
'lit/revision-restore',
|
||
'lit/menu-list',
|
||
'lit/menu-get',
|
||
'lit/menu-create',
|
||
'lit/menu-set-items',
|
||
'lit/menu-assign-location',
|
||
'lit/wp-settings-get',
|
||
'lit/wp-settings-update',
|
||
'lit/page-render-summary',
|
||
];
|
||
|
||
public function testProvidesTheWordPressAbilitiesInOrder(): void
|
||
{
|
||
$integration = new WordPressIntegration(Mockery::mock(ContentSchema::class), FakeChangeLog::forbidden());
|
||
|
||
self::assertSame('wordpress', $integration->id());
|
||
self::assertTrue($integration->isAvailable());
|
||
self::assertSame(
|
||
self::EXPECTED,
|
||
array_map(static fn (Ability $ability): string => $ability->name(), $integration->abilities())
|
||
);
|
||
}
|
||
|
||
public function testEveryAbilityHasAnObjectInputSchemaThatRejectsUnknownKeys(): void
|
||
{
|
||
$integration = new WordPressIntegration(Mockery::mock(ContentSchema::class), FakeChangeLog::forbidden());
|
||
|
||
foreach ($integration->abilities() as $ability) {
|
||
$schema = $ability->inputSchema();
|
||
self::assertSame('object', $schema['type'], $ability->name());
|
||
self::assertFalse($schema['additionalProperties'], $ability->name());
|
||
self::assertMatchesRegularExpression('#^lit/[a-z]+(-[a-z]+)+$#', $ability->name());
|
||
}
|
||
}
|
||
|
||
public function testContentCreateGetsTheContentComposer(): void
|
||
{
|
||
Functions\when('get_post_type_object')->justReturn((object) ['cap' => (object) ['create_posts' => 'edit_pages']]);
|
||
Functions\when('current_user_can')->justReturn(true);
|
||
$schema = SchemaFixture::managed();
|
||
$composer = Mockery::mock(ContentComposer::class);
|
||
$composer->expects('available')->andReturn(false);
|
||
$integration = new WordPressIntegration($schema, FakeChangeLog::forbidden(), $composer);
|
||
|
||
$create = array_values(array_filter(
|
||
$integration->abilities(),
|
||
static fn (Ability $ability): bool => $ability->name() === 'lit/content-create'
|
||
))[0];
|
||
$result = $create->execute(['post_type' => 'page', 'title' => 'Start', 'divi' => [['type' => 'section']]]);
|
||
|
||
self::assertToolError($result, 'unsupported');
|
||
}
|
||
}
|