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']);