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>
This commit is contained in:
2026-09-26 17:53:54 +02:00
co-authored by Claude Opus 5.5
parent 3d96a41366
commit 72eeaa46ad
34 changed files with 725 additions and 353 deletions
@@ -13,6 +13,8 @@ use LIT\MCP\ChangeLog\ChangeLog;
use LIT\MCP\Integrations\WordPress\Content\ContentCreateAbility;
use LIT\MCP\Integrations\WordPress\Content\PostWriter;
use LIT\MCP\Integrations\WordPress\Schema\ContentSchema;
use LIT\MCP\Tests\Support\FakeChangeLog;
use LIT\MCP\Tests\Support\SchemaFixture;
use LIT\MCP\Tests\Unit\TestCase;
use Mockery;
use Mockery\MockInterface;
@@ -27,8 +29,7 @@ final class ContentCreateAbilityTest extends TestCase
{
Functions\when('get_post_type_object')->justReturn((object) ['cap' => (object) ['create_posts' => 'edit_pages']]);
Functions\when('current_user_can')->justReturn(true);
$schema = Mockery::mock(ContentSchema::class);
$schema->allows('isManaged')->andReturn(true);
$schema = SchemaFixture::managed();
return $schema;
}
@@ -72,7 +73,7 @@ final class ContentCreateAbilityTest extends TestCase
$writer->allows('prepare')->andReturn(Errors::invalidInput('ungültig', ['fields.role' => 'Pflichtfeld fehlt.']));
Functions\expect('wp_insert_post')->never();
$result = (new ContentCreateAbility($this->schema(), $writer, Mockery::mock(ChangeLog::class)))
$result = (new ContentCreateAbility($this->schema(), $writer, FakeChangeLog::forbidden()))
->execute(['post_type' => 'lit_person', 'title' => 'Anna']);
self::assertSame('lit_validation_failed', $result->get_error_code());
@@ -97,8 +98,7 @@ final class ContentCreateAbilityTest extends TestCase
], true)->andReturn(42);
Actions\expectDone('lit_mcp_after_save')->once()->with(42, 'lit/content-create', $input);
$post = $this->stubResultLinks();
$log = Mockery::mock(ChangeLog::class);
$log->expects('record')->with('lit/content-create', 'lit_person', '42', 'Entwurf "Anna" angelegt', null)->andReturn(7);
$log = new FakeChangeLog(7);
$result = (new ContentCreateAbility($schema, $writer, $log))->execute($input);
@@ -108,6 +108,13 @@ final class ContentCreateAbilityTest extends TestCase
self::assertSame(['w'], $result['warnings']);
self::assertStringContainsString('Entwurf', $result['summary']);
self::assertSame(Rev::ofPost($post), $result['rev']);
$log->assertRecorded([
'ability' => 'lit/content-create',
'object_type' => 'lit_person',
'object_id' => '42',
'summary' => 'Entwurf "Anna" angelegt',
'revision_id' => null,
]);
}
public function testFailingExtrasDeleteCreatedTermsAndTheNewDraft(): void
@@ -118,8 +125,7 @@ final class ContentCreateAbilityTest extends TestCase
Functions\when('wp_insert_post')->justReturn(42);
Functions\expect('wp_delete_post')->once()->with(42, true)->andReturn(new WP_Post(['ID' => 42]));
Actions\expectDone('lit_mcp_after_save')->never();
$log = Mockery::mock(ChangeLog::class);
$log->expects('record')->never();
$log = FakeChangeLog::forbidden();
$failure = $this->execute($writer, $log);
@@ -139,8 +145,7 @@ final class ContentCreateAbilityTest extends TestCase
Functions\when('get_current_user_id')->justReturn(3);
Functions\when('wp_insert_post')->justReturn(42);
Functions\expect('wp_delete_post')->once()->with(42, true)->andReturn(new WP_Post(['ID' => 42]));
$log = Mockery::mock(ChangeLog::class);
$log->expects('record')->never();
$log = FakeChangeLog::forbidden();
$result = (new ContentCreateAbility($this->schema(), $writer, $log))->execute(self::INPUT);
@@ -157,8 +162,7 @@ final class ContentCreateAbilityTest extends TestCase
Functions\when('get_current_user_id')->justReturn(3);
Functions\when('wp_insert_post')->justReturn(42);
Functions\when('wp_delete_post')->justReturn(false);
$log = Mockery::mock(ChangeLog::class);
$log->expects('recordPartial');
$log = FakeChangeLog::partialsOnly();
try {
(new ContentCreateAbility($this->schema(), $writer, $log))->execute(self::INPUT);
@@ -167,6 +171,7 @@ final class ContentCreateAbilityTest extends TestCase
self::assertFalse($e->rolledBack);
self::assertSame(42, $e->postId);
}
$log->assertPartials([]);
}
public function testADraftThatCannotBeDeletedIsAPartialWriteAndLogged(): void
@@ -176,16 +181,14 @@ final class ContentCreateAbilityTest extends TestCase
$writer->expects('deleteCreatedTerms');
Functions\when('wp_insert_post')->justReturn(42);
Functions\when('wp_delete_post')->justReturn(false);
$log = Mockery::mock(ChangeLog::class);
$log->expects('recordPartial')
->with('lit/content-create', 'lit_person', '42', Mockery::type('string'))
->andReturn(9);
$log = FakeChangeLog::partialsOnly();
$failure = $this->execute($writer, $log);
self::assertFalse($failure->rolledBack);
self::assertSame(42, $failure->postId);
self::assertStringContainsString('Eintrag 42', $failure->toError()->get_error_message());
$log->assertPartials(['ability' => 'lit/content-create', 'object_type' => 'lit_person', 'object_id' => '42']);
}
public function testLeftoverCreatedTermsMakeItAPartialWrite(): void
@@ -195,13 +198,13 @@ final class ContentCreateAbilityTest extends TestCase
$writer->expects('deleteCreatedTerms')->andThrow(new RuntimeException('term 15 bleibt'));
Functions\when('wp_insert_post')->justReturn(42);
Functions\when('wp_delete_post')->justReturn(new WP_Post(['ID' => 42]));
$log = Mockery::mock(ChangeLog::class);
$log->expects('recordPartial');
$log = FakeChangeLog::partialsOnly();
$failure = $this->execute($writer, $log);
self::assertFalse($failure->rolledBack);
self::assertStringContainsString('term 15 bleibt', $failure->getMessage());
$log->assertPartials([]);
}
public function testAnInsertThatThrowsIsAPartialWriteWithoutId(): void
@@ -210,16 +213,14 @@ final class ContentCreateAbilityTest extends TestCase
Functions\when('wp_insert_post')->alias(static function (): never {
throw new RuntimeException('save_post hook kaputt');
});
$log = Mockery::mock(ChangeLog::class);
$log->expects('recordPartial')
->with('lit/content-create', 'lit_person', '', Mockery::type('string'))
->andReturn(9);
$log = FakeChangeLog::partialsOnly();
$failure = $this->execute($writer, $log);
self::assertFalse($failure->rolledBack);
self::assertNull($failure->postId);
self::assertSame('lit_partial_write', $failure->toError()->get_error_code());
$log->assertPartials(['ability' => 'lit/content-create', 'object_type' => 'lit_person', 'object_id' => '']);
}
public function testFailuresAfterTheWriteOnlyAddWarnings(): void
@@ -231,8 +232,7 @@ final class ContentCreateAbilityTest extends TestCase
Actions\expectDone('lit_mcp_after_save')->once()->whenHappen(static function (): void {
throw new RuntimeException('listener kaputt');
});
$log = Mockery::mock(ChangeLog::class);
$log->expects('record')->andThrow(new RuntimeException('db weg'));
$log = new FakeChangeLog(1, new RuntimeException('db weg'));
$file = (string) tempnam(sys_get_temp_dir(), 'lit-mcp-log');
$previous = ini_set('error_log', $file);
@@ -248,12 +248,13 @@ final class ContentCreateAbilityTest extends TestCase
self::assertStringContainsString('listener kaputt', (string) file_get_contents($file));
self::assertStringContainsString('db weg', (string) file_get_contents($file));
unlink($file);
$log->assertRecorded([]);
}
public function testStatusIsNotAnInputAndPermissionNeedsCreateRights(): void
{
$schema = $this->schema();
$ability = new ContentCreateAbility($schema, Mockery::mock(PostWriter::class), Mockery::mock(ChangeLog::class));
$ability = new ContentCreateAbility($schema, Mockery::mock(PostWriter::class), FakeChangeLog::forbidden());
self::assertArrayNotHasKey('status', $ability->inputSchema()['properties']);
self::assertFalse($ability->inputSchema()['additionalProperties']);
@@ -16,6 +16,8 @@ use LIT\MCP\Integrations\WordPress\Content\ContentCreateAbility;
use LIT\MCP\Integrations\WordPress\Content\PostWriter;
use LIT\MCP\Integrations\WordPress\Schema\ContentSchema;
use LIT\MCP\Tests\Support\DiviWriterFixture;
use LIT\MCP\Tests\Support\FakeChangeLog;
use LIT\MCP\Tests\Support\SchemaFixture;
use LIT\MCP\Tests\Unit\TestCase;
use Mockery;
use Mockery\MockInterface;
@@ -34,9 +36,7 @@ final class ContentCreateDiviTest extends TestCase
Functions\when('get_post_type_object')->justReturn((object) ['cap' => (object) ['create_posts' => 'edit_pages']]);
Functions\when('current_user_can')->justReturn(true);
Functions\when('get_current_user_id')->justReturn(3);
$schema = Mockery::mock(ContentSchema::class);
$schema->allows('isManaged')->andReturn(true);
$schema->allows('diviEnabled')->andReturn(true);
$schema = SchemaFixture::managed(true);
return $schema;
}
@@ -66,9 +66,7 @@ final class ContentCreateDiviTest extends TestCase
private function log(): ChangeLog
{
$log = Mockery::mock(ChangeLog::class);
$log->allows('record')->andReturn(9);
$log->allows('recordPartial');
$log = FakeChangeLog::any(9);
return $log;
}
@@ -8,6 +8,7 @@ use Brain\Monkey\Functions;
use LIT\MCP\Integrations\WordPress\Content\ContentGetAbility;
use LIT\MCP\Integrations\WordPress\Content\PostPresenter;
use LIT\MCP\Integrations\WordPress\Schema\ContentSchema;
use LIT\MCP\Tests\Support\SchemaFixture;
use LIT\MCP\Tests\Unit\TestCase;
use Mockery;
use WP_Post;
@@ -19,8 +20,7 @@ final class ContentGetAbilityTest extends TestCase
$post = new WP_Post(['ID' => 5, 'post_type' => 'page']);
Functions\when('get_post')->justReturn($post);
Functions\when('current_user_can')->justReturn(true);
$schema = Mockery::mock(ContentSchema::class);
$schema->allows('isManaged')->andReturn(true);
$schema = SchemaFixture::managed();
$presenter = Mockery::mock(PostPresenter::class);
$presenter->expects('full')->with($post)->andReturn(['id' => 5]);
$ability = new ContentGetAbility($schema, $presenter);
@@ -34,8 +34,7 @@ final class ContentGetAbilityTest extends TestCase
$post = new WP_Post(['ID' => 5, 'post_type' => 'page', 'post_status' => 'trash']);
Functions\when('get_post')->justReturn($post);
Functions\when('current_user_can')->justReturn(true);
$schema = Mockery::mock(ContentSchema::class);
$schema->allows('isManaged')->andReturn(true);
$schema = SchemaFixture::managed();
$presenter = Mockery::mock(PostPresenter::class);
$presenter->expects('full')->with($post)->andReturn(['id' => 5]);
@@ -10,6 +10,7 @@ use LIT\MCP\Integrations\WordPress\Content\ContentQueryAbility;
use LIT\MCP\Integrations\WordPress\Content\PostPresenter;
use LIT\MCP\Integrations\WordPress\Schema\ContentSchema;
use LIT\MCP\Integrations\WordPress\Schema\FieldDefinition;
use LIT\MCP\Tests\Support\SchemaFixture;
use LIT\MCP\Tests\Unit\TestCase;
use Mockery;
use WP_Post;
@@ -241,8 +242,7 @@ final class ContentQueryAbilityTest extends TestCase
{
Functions\when('get_post_type_object')->justReturn((object) ['cap' => (object) ['edit_posts' => 'edit_posts']]);
Functions\when('current_user_can')->justReturn(false);
$schema = Mockery::mock(ContentSchema::class);
$schema->allows('isManaged')->andReturn(true);
$schema = SchemaFixture::managed();
$ability = new ContentQueryAbility($schema, Mockery::mock(PostPresenter::class));
self::assertSame('lit_forbidden', $ability->permission(['post_type' => 'page'])->get_error_code());
@@ -7,6 +7,7 @@ namespace LIT\MCP\Tests\Unit\Integrations\WordPress\Content;
use Brain\Monkey\Functions;
use LIT\MCP\Integrations\WordPress\Content\ContentTypesAbility;
use LIT\MCP\Integrations\WordPress\Schema\ContentSchema;
use LIT\MCP\Tests\Support\SchemaFixture;
use LIT\MCP\Tests\Unit\TestCase;
use Mockery;
use WP_Error;
@@ -89,8 +90,7 @@ final class ContentTypesAbilityTest extends TestCase
{
Functions\when('get_post_type_object')->justReturn((object) ['cap' => (object) ['edit_posts' => 'edit_pages']]);
Functions\when('current_user_can')->justReturn(false);
$schema = Mockery::mock(ContentSchema::class);
$schema->allows('isManaged')->andReturn(true);
$schema = SchemaFixture::managed();
$result = (new ContentTypesAbility($schema))->execute(['post_type' => 'page']);
@@ -110,8 +110,7 @@ final class ContentTypesAbilityTest extends TestCase
{
Functions\when('current_user_can')->alias(static fn (string $cap): bool => $cap === 'edit_posts');
Functions\when('get_post_type_object')->justReturn((object) ['cap' => (object) ['edit_posts' => 'edit_pages']]);
$schema = Mockery::mock(ContentSchema::class);
$schema->allows('isManaged')->andReturn(true);
$schema = SchemaFixture::managed();
$result = (new ContentTypesAbility($schema))->permission(['post_type' => 'lit_person']);
@@ -123,8 +122,7 @@ final class ContentTypesAbilityTest extends TestCase
{
Functions\when('current_user_can')->justReturn(true);
Functions\when('get_post_type_object')->justReturn((object) ['cap' => (object) ['edit_posts' => 'edit_posts']]);
$schema = Mockery::mock(ContentSchema::class);
$schema->allows('isManaged')->andReturn(true);
$schema = SchemaFixture::managed();
self::assertTrue((new ContentTypesAbility($schema))->permission(['post_type' => 'page']));
}
@@ -13,6 +13,8 @@ use LIT\MCP\Integrations\WordPress\Content\ContentUpdateAbility;
use LIT\MCP\Integrations\WordPress\Content\PostPresenter;
use LIT\MCP\Integrations\WordPress\Content\PostWriter;
use LIT\MCP\Integrations\WordPress\Schema\ContentSchema;
use LIT\MCP\Tests\Support\FakeChangeLog;
use LIT\MCP\Tests\Support\SchemaFixture;
use LIT\MCP\Tests\Unit\TestCase;
use Mockery;
use RuntimeException;
@@ -39,15 +41,14 @@ final class ContentUpdateAbilityTest extends TestCase
private function schema(): ContentSchema
{
$schema = Mockery::mock(ContentSchema::class);
$schema->allows('isManaged')->andReturn(true);
$schema = SchemaFixture::managed();
return $schema;
}
private function ability(PostWriter $writer, ?ChangeLog $log = null): ContentUpdateAbility
{
return new ContentUpdateAbility($this->schema(), $writer, $log ?? Mockery::mock(ChangeLog::class));
return new ContentUpdateAbility($this->schema(), $writer, $log ?? FakeChangeLog::forbidden());
}
public function testRevIsRequired(): void
@@ -155,8 +156,7 @@ final class ContentUpdateAbilityTest extends TestCase
Actions\expectDone('lit_mcp_after_save')->once()->with(5, 'lit/content-update', $input);
Functions\when('get_preview_post_link')->justReturn('https://kunde.de/team/');
Functions\when('get_edit_post_link')->justReturn('https://kunde.de/wp-admin/post.php?post=5&action=edit');
$log = Mockery::mock(ChangeLog::class);
$log->expects('record')->with('lit/content-update', 'page', '5', Mockery::type('string'), 811)->andReturn(4711);
$log = new FakeChangeLog(4711);
$result = $this->ability($writer, $log)->execute($input);
@@ -166,6 +166,12 @@ final class ContentUpdateAbilityTest extends TestCase
self::assertSame('updated', $result['changed'][0]['action']);
self::assertSame(Rev::ofPost($this->post), $result['rev']);
self::assertStringContainsString('Status unverändert: publish', $result['summary']);
$log->assertRecorded([
'ability' => 'lit/content-update',
'object_type' => 'page',
'object_id' => '5',
'revision_id' => 811,
]);
}
/**
@@ -197,8 +203,7 @@ final class ContentUpdateAbilityTest extends TestCase
private function executeExpectingFailure(PostWriter $writer, array $input, ?ChangeLog $log = null): WriteFailed
{
if ($log === null) {
$log = Mockery::mock(ChangeLog::class);
$log->expects('record')->never();
$log = FakeChangeLog::forbidden();
}
try {
@@ -298,14 +303,14 @@ final class ContentUpdateAbilityTest extends TestCase
});
Functions\when('wp_revisions_enabled')->justReturn(true);
Functions\expect('wp_restore_post_revision')->never();
$log = Mockery::mock(ChangeLog::class);
$log->expects('recordPartial');
$log = FakeChangeLog::partialsOnly();
$failure = $this->executeExpectingFailure($writer, $input, $log);
self::assertFalse($failure->rolledBack);
self::assertCount(1, $updates);
self::assertStringContainsString('publish', $failure->getMessage());
$log->assertPartials([]);
}
public function testAnIncompleteRollbackIsAPartialWriteAndLogged(): void
@@ -314,10 +319,7 @@ final class ContentUpdateAbilityTest extends TestCase
$updates = [];
$this->captureUpdates($updates);
Functions\when('wp_revisions_enabled')->justReturn(false);
$log = Mockery::mock(ChangeLog::class);
$log->expects('recordPartial')
->with('lit/content-update', 'page', '5', Mockery::type('string'), 811)
->andReturn(12);
$log = FakeChangeLog::partialsOnly();
$failure = $this->executeExpectingFailure($writer, $input, $log);
@@ -327,6 +329,12 @@ final class ContentUpdateAbilityTest extends TestCase
self::assertSame('lit_partial_write', $error->get_error_code());
self::assertSame(5, $error->get_error_data()['post_id']);
self::assertSame(811, $error->get_error_data()['revision_id']);
$log->assertPartials([
'ability' => 'lit/content-update',
'object_type' => 'page',
'object_id' => '5',
'revision_id' => 811,
]);
}
public function testFailuresAfterTheWriteOnlyAddWarnings(): void
@@ -345,8 +353,7 @@ final class ContentUpdateAbilityTest extends TestCase
Actions\expectDone('lit_mcp_after_save')->once()->whenHappen(static function (): void {
throw new RuntimeException('listener kaputt');
});
$log = Mockery::mock(ChangeLog::class);
$log->expects('record')->andThrow(new RuntimeException('db weg'));
$log = new FakeChangeLog(1, new RuntimeException('db weg'));
$file = (string) tempnam(sys_get_temp_dir(), 'lit-mcp-log');
$previous = ini_set('error_log', $file);
@@ -361,5 +368,6 @@ final class ContentUpdateAbilityTest extends TestCase
self::assertCount(2, $result['warnings']);
self::assertStringContainsString('listener kaputt', (string) file_get_contents($file));
unlink($file);
$log->assertRecorded([]);
}
}
@@ -6,9 +6,9 @@ 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\Support\FakeChangeLog;
use LIT\MCP\Tests\Unit\TestCase;
use Mockery;
use RuntimeException;
@@ -22,9 +22,7 @@ final class NewDraftTest extends TestCase
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');
$log = FakeChangeLog::partialsOnly();
$draft = new NewDraft($writer, $log, 'lit/content-create', 'page');
try {
@@ -40,6 +38,12 @@ final class NewDraftTest extends TestCase
$e->getMessage()
);
}
$log->assertPartials([
'ability' => 'lit/content-create',
'object_type' => 'page',
'object_id' => '42',
'summary' => 'Entwurf 42: Anlegen nur teilweise zurückgenommen',
]);
}
public function testACleanDiscardIsRolledBackWithTheGivenReason(): void
@@ -47,8 +51,7 @@ final class NewDraftTest extends TestCase
$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();
$log = FakeChangeLog::forbidden();
$draft = new NewDraft($writer, $log, 'lit/content-create', 'page');
try {