feat: field validation, term resolution, post guard and revision points
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01At3V6SjwbMe6eMzVGW3Hs7
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace LIT\MCP\Tests\Unit\Integrations\WordPress\Schema;
|
||||
|
||||
use Brain\Monkey\Functions;
|
||||
use LIT\MCP\Integrations\WordPress\Schema\TermAssigner;
|
||||
use LIT\MCP\Tests\Unit\TestCase;
|
||||
use RuntimeException;
|
||||
use WP_Error;
|
||||
use WP_Term;
|
||||
|
||||
final class TermAssignerTest extends TestCase
|
||||
{
|
||||
private function stubTaxonomies(bool $mayEditTerms = true, bool $mayAssign = true): void
|
||||
{
|
||||
Functions\when('get_object_taxonomies')->justReturn(['category', 'post_tag']);
|
||||
Functions\when('get_taxonomy')->justReturn((object) [
|
||||
'cap' => (object) ['assign_terms' => 'assign_categories', 'edit_terms' => 'manage_categories'],
|
||||
]);
|
||||
Functions\when('current_user_can')->alias(static fn (string $cap): bool => match ($cap) {
|
||||
'assign_categories' => $mayAssign,
|
||||
'manage_categories' => $mayEditTerms,
|
||||
default => false,
|
||||
});
|
||||
Functions\when('get_term')->alias(static fn (int $id) => $id === 4 ? new WP_Term(['term_id' => 4]) : null);
|
||||
Functions\when('term_exists')->alias(
|
||||
static fn (string $name) => $name === 'News' ? ['term_id' => '9', 'term_taxonomy_id' => '9'] : null
|
||||
);
|
||||
}
|
||||
|
||||
public function testResolvesIdsExistingNamesAndNewNames(): void
|
||||
{
|
||||
$this->stubTaxonomies();
|
||||
|
||||
$result = (new TermAssigner())->resolve('post', ['category' => [4, 'News', 'Neu']]);
|
||||
|
||||
self::assertSame([], $result['errors']);
|
||||
self::assertSame(['category' => [4, 9, 'Neu']], $result['terms']);
|
||||
}
|
||||
|
||||
public function testReportsForeignTaxonomyMissingIdsAndMissingRights(): void
|
||||
{
|
||||
$this->stubTaxonomies(false);
|
||||
|
||||
$result = (new TermAssigner())->resolve('post', [
|
||||
'genre' => [1],
|
||||
'category' => [77],
|
||||
'post_tag' => ['Unbekannt'],
|
||||
]);
|
||||
|
||||
self::assertSame('Taxonomie gehört nicht zu diesem Inhaltstyp.', $result['errors']['genre']);
|
||||
self::assertSame('Begriff 77 existiert nicht.', $result['errors']['category']);
|
||||
self::assertStringContainsString('keine Berechtigung', $result['errors']['post_tag']);
|
||||
}
|
||||
|
||||
public function testAssignRightsAreRequired(): void
|
||||
{
|
||||
$this->stubTaxonomies(true, false);
|
||||
|
||||
$result = (new TermAssigner())->resolve('post', ['category' => [4]]);
|
||||
|
||||
self::assertSame('Keine Berechtigung, Begriffe dieser Taxonomie zuzuweisen.', $result['errors']['category']);
|
||||
}
|
||||
|
||||
public function testAssignCreatesNewTermsAndReplacesTheAssignment(): void
|
||||
{
|
||||
Functions\expect('wp_insert_term')->once()->with('Neu', 'category')->andReturn(['term_id' => 15]);
|
||||
Functions\expect('wp_set_object_terms')->once()->with(5, [4, 15], 'category', false)->andReturn([4, 15]);
|
||||
|
||||
(new TermAssigner())->assign(5, ['category' => [4, 'Neu']]);
|
||||
}
|
||||
|
||||
public function testAssignFailureThrows(): void
|
||||
{
|
||||
Functions\when('wp_set_object_terms')->justReturn(new WP_Error('x', 'kaputt'));
|
||||
|
||||
$this->expectException(RuntimeException::class);
|
||||
|
||||
(new TermAssigner())->assign(5, ['category' => [4]]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user