refactor: extract the lit/content-create draft transaction into NewDraft (M7)

NewDraft owns insert (partial write on a throwing hook), refuse and discard (via
Rollback::attempt and WriteFailed::*) and the partial logging; ContentCreateAbility
keeps checkDivi/prepare/combined (Errors::fieldsOf) and the result. Both discard
callers still pass REASON_EXTRAS. Behaviour unchanged; NewDraftTest adds the
throwing-delete branch.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-09-26 17:43:50 +02:00
co-authored by Claude Opus 5.5
parent 4114bdb805
commit d2cc8497c9
3 changed files with 203 additions and 94 deletions
@@ -0,0 +1,62 @@
<?php
declare(strict_types=1);
namespace LIT\MCP\Tests\Unit\Integrations\WordPress\Content;
use Brain\Monkey\Functions;
use LIT\MCP\Abilities\WriteFailed;
use LIT\MCP\ChangeLog\ChangeLog;
use LIT\MCP\Integrations\WordPress\Content\NewDraft;
use LIT\MCP\Integrations\WordPress\Content\PostWriter;
use LIT\MCP\Tests\Unit\TestCase;
use Mockery;
use RuntimeException;
final class NewDraftTest extends TestCase
{
public function testADeleteThatThrowsIsNamedInTheLogDetailAndTheOtherStepsStillRun(): void
{
$writer = Mockery::mock(PostWriter::class);
$writer->expects('deleteCreatedTerms')->andThrow(new RuntimeException('term 15 bleibt'));
Functions\when('wp_delete_post')->alias(static function (): never {
throw new RuntimeException('delete_post hook kaputt');
});
$log = Mockery::mock(ChangeLog::class);
$log->expects('recordPartial')
->with('lit/content-create', 'page', '42', 'Entwurf 42: Anlegen nur teilweise zurückgenommen');
$draft = new NewDraft($writer, $log, 'lit/content-create', 'page');
try {
$draft->discard(42, new RuntimeException('layout kaputt'), 'Grund für den Client.');
self::fail('WriteFailed expected');
} catch (WriteFailed $e) {
self::assertFalse($e->rolledBack);
self::assertSame(42, $e->postId);
self::assertSame('Grund für den Client.', $e->reason);
self::assertSame(
'Extras of new post 42 could not be written: layout kaputt; rollback: term 15 bleibt; '
. 'draft 42 could not be deleted: delete_post hook kaputt [rollback incomplete]',
$e->getMessage()
);
}
}
public function testACleanDiscardIsRolledBackWithTheGivenReason(): void
{
$writer = Mockery::mock(PostWriter::class);
$writer->expects('deleteCreatedTerms');
Functions\expect('wp_delete_post')->once()->with(42, true)->andReturn(true);
$log = Mockery::mock(ChangeLog::class);
$log->expects('recordPartial')->never();
$draft = new NewDraft($writer, $log, 'lit/content-create', 'page');
try {
$draft->discard(42, new RuntimeException('meta kaputt'), NewDraft::REASON_EXTRAS);
self::fail('WriteFailed expected');
} catch (WriteFailed $e) {
self::assertTrue($e->rolledBack);
self::assertSame(NewDraft::REASON_EXTRAS, $e->reason);
}
}
}