Files
WP-LIT-MCP/tests/Unit/Integrations/WordPress/Status/PostDuplicateAbilityTest.php
T
janniclas.loosenandClaude Opus 5.5 72eeaa46ad test: FakeChangeLog and SchemaFixture instead of repeated mocks (T2)
- tests/Support/FakeChangeLog extends ChangeLog: records record()/recordPartial()
  calls, increasing ids, optional failure; new FakeChangeLog(id) / partialsOnly() /
  any(id) / forbidden() mirror what the replaced mocks allowed. assertRecorded() /
  assertPartials() check count and the named arguments. Calls a fake does not allow
  throw and are also verified in TestCase::tearDown (one assertion per test that
  uses a fake), so swallowed change-log calls still fail like a never() expectation.
- All ChangeLog mocks of ability/integration tests replaced (the admin tests that
  stub query/purge/clear keep their mocks); expectException tests that checked the
  partial log now catch and assert.
- SchemaFixture::managed(?divi) replaces the 21 isManaged-only ContentSchema mocks;
  DiviFixtures::schema() uses it.
Tests 1397 -> 1404 (FakeChangeLogTest); no test has fewer assertions.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 17:53:54 +02:00

97 lines
3.7 KiB
PHP

<?php
declare(strict_types=1);
namespace LIT\MCP\Tests\Unit\Integrations\WordPress\Status;
use Brain\Monkey\Actions;
use Brain\Monkey\Functions;
use LIT\MCP\Abilities\Rev;
use LIT\MCP\ChangeLog\ChangeLog;
use LIT\MCP\Integrations\WordPress\Status\PostCloner;
use LIT\MCP\Integrations\WordPress\Status\PostDuplicateAbility;
use LIT\MCP\Tests\Support\FakeChangeLog;
use LIT\MCP\Tests\Support\SchemaFixture;
use LIT\MCP\Tests\Unit\TestCase;
use Mockery;
use WP_Post;
final class PostDuplicateAbilityTest extends TestCase
{
private WP_Post $original;
private WP_Post $copy;
protected function setUp(): void
{
parent::setUp();
$this->original = new WP_Post(['ID' => 5, 'post_type' => 'page', 'post_title' => 'Team']);
$this->copy = new WP_Post(['ID' => 77, 'post_type' => 'page', 'post_title' => 'Team (Kopie)']);
Functions\when('get_post')->alias(fn (int $id) => $id === 5 ? $this->original : $this->copy);
Functions\when('current_user_can')->justReturn(true);
Functions\when('get_post_type_object')->justReturn((object) ['cap' => (object) ['create_posts' => 'edit_pages']]);
Functions\when('get_preview_post_link')->justReturn('https://kunde.de/?p=77&preview=true');
Functions\when('get_edit_post_link')->justReturn('https://kunde.de/wp-admin/post.php?post=77&action=edit');
}
private function ability(PostCloner $cloner, ChangeLog $log): PostDuplicateAbility
{
$schema = SchemaFixture::managed();
return new PostDuplicateAbility($schema, $cloner, $log);
}
public function testCreatesADraftCopyAndReportsIt(): void
{
$cloner = Mockery::mock(PostCloner::class);
$cloner->expects('duplicate')->with($this->original)->andReturn(77);
Actions\expectDone('lit_mcp_after_save')->once()->with(77, 'lit/post-duplicate', ['post_id' => 5]);
$log = new FakeChangeLog(8);
$result = $this->ability($cloner, $log)->execute(['post_id' => 5]);
self::assertSame([['id' => '77', 'type' => 'page', 'action' => 'created', 'label' => 'Team (Kopie)']], $result['changed']);
self::assertSame(5, $result['original_id']);
self::assertSame(8, $result['log_id']);
self::assertSame(Rev::ofPost($this->copy), $result['rev']);
self::assertSame([], $result['warnings']);
$log->assertRecorded([
'ability' => 'lit/post-duplicate',
'object_type' => 'page',
'object_id' => '77',
'summary' => 'Kopie von "Team" (ID 5) angelegt',
'revision_id' => null,
]);
}
public function testNeedsTheRightToCreateEntriesOfThatType(): void
{
Functions\when('current_user_can')->alias(static fn (string $cap): bool => $cap !== 'edit_pages');
$cloner = Mockery::mock(PostCloner::class);
$cloner->expects('duplicate')->never();
$result = $this->ability($cloner, FakeChangeLog::forbidden())->execute(['post_id' => 5]);
self::assertSame('lit_forbidden', $result->get_error_code());
}
public function testATrashedOriginalIsRefused(): void
{
$this->original->post_status = 'trash';
$cloner = Mockery::mock(PostCloner::class);
$cloner->expects('duplicate')->never();
$result = $this->ability($cloner, FakeChangeLog::forbidden())->execute(['post_id' => 5]);
self::assertSame('lit_validation_failed', $result->get_error_code());
}
public function testIsIndirectAndNotIdempotent(): void
{
$ability = $this->ability(Mockery::mock(PostCloner::class), FakeChangeLog::forbidden());
self::assertFalse($ability->isDirect());
self::assertFalse($ability->annotations()['idempotent']);
self::assertFalse($ability->annotations()['destructive']);
}
}