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
@@ -8,10 +8,11 @@ use Brain\Monkey\Functions;
use LIT\MCP\Abilities\Rev;
use LIT\MCP\Abilities\WriteFailed;
use LIT\MCP\ChangeLog\ChangeLog;
use LIT\MCP\Integrations\WordPress\Schema\ContentSchema;
use LIT\MCP\Integrations\WordPress\Status\PostCloner;
use LIT\MCP\Integrations\WordPress\Status\PostReplaceWithCopyAbility;
use LIT\MCP\Integrations\WordPress\Status\ReplaceFailed;
use LIT\MCP\Tests\Support\FakeChangeLog;
use LIT\MCP\Tests\Support\SchemaFixture;
use LIT\MCP\Tests\Unit\TestCase;
use Mockery;
use RuntimeException;
@@ -39,8 +40,7 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
private function ability(PostCloner $cloner, ChangeLog $log, bool $trash = true): PostReplaceWithCopyAbility
{
$schema = Mockery::mock(ContentSchema::class);
$schema->allows('isManaged')->andReturn(true);
$schema = SchemaFixture::managed();
return new PostReplaceWithCopyAbility($schema, $cloner, $log, static fn (): bool => $trash);
}
@@ -65,7 +65,7 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
$cloner = Mockery::mock(PostCloner::class);
$cloner->expects('replace')->never();
$result = $this->ability($cloner, Mockery::mock(ChangeLog::class))
$result = $this->ability($cloner, FakeChangeLog::forbidden())
->execute(['original_id' => 5, 'copy_id' => 77, 'rev' => Rev::ofPost($this->original)]);
self::assertSame('lit_confirm_required', $result->get_error_code());
@@ -77,7 +77,7 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
$cloner = Mockery::mock(PostCloner::class);
$cloner->expects('replace')->never();
$result = $this->ability($cloner, Mockery::mock(ChangeLog::class))->execute($this->input());
$result = $this->ability($cloner, FakeChangeLog::forbidden())->execute($this->input());
self::assertSame('lit_validation_failed', $result->get_error_code());
}
@@ -101,7 +101,7 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
$cloner->expects('backup')->never();
$cloner->expects('replace')->never();
$result = $this->ability($cloner, Mockery::mock(ChangeLog::class))->execute($this->input());
$result = $this->ability($cloner, FakeChangeLog::forbidden())->execute($this->input());
self::assertSame('lit_validation_failed', $result->get_error_code());
}
@@ -114,7 +114,7 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
$cloner->expects('backup')->never();
$cloner->expects('replace')->never();
$result = $this->ability($cloner, Mockery::mock(ChangeLog::class))->execute($this->input());
$result = $this->ability($cloner, FakeChangeLog::forbidden())->execute($this->input());
self::assertSame('lit_validation_failed', $result->get_error_code());
self::assertArrayHasKey('original_id', $result->get_error_data()['fields']);
@@ -129,8 +129,7 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
$cloner->allows('backup')->andReturn(88);
$cloner->expects('replace');
Functions\when('wp_trash_post')->justReturn(new WP_Post(['ID' => 77]));
$log = Mockery::mock(ChangeLog::class);
$log->allows('record')->andReturn(11);
$log = new FakeChangeLog(11);
self::assertTrue($this->ability($cloner, $log)->execute($this->input())['ok']);
}
@@ -141,7 +140,7 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
$cloner->expects('replace')->never();
Functions\expect('wp_save_post_revision')->never();
$result = $this->ability($cloner, Mockery::mock(ChangeLog::class))->execute($this->input(['rev' => 'veraltet']));
$result = $this->ability($cloner, FakeChangeLog::forbidden())->execute($this->input(['rev' => 'veraltet']));
self::assertSame('lit_rev_mismatch', $result->get_error_code());
}
@@ -151,7 +150,7 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
$cloner = Mockery::mock(PostCloner::class);
$cloner->expects('replace')->never();
$result = $this->ability($cloner, Mockery::mock(ChangeLog::class), false)->execute($this->input());
$result = $this->ability($cloner, FakeChangeLog::forbidden(), false)->execute($this->input());
self::assertSame('lit_unsupported', $result->get_error_code());
}
@@ -181,8 +180,7 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
$order[] = 'trash';
return new WP_Post(['ID' => 77]);
});
$log = Mockery::mock(ChangeLog::class);
$log->expects('record')->with('lit/post-replace-with-copy', 'page', '5', Mockery::type('string'), 900)->andReturn(11);
$log = new FakeChangeLog(11);
$result = $this->ability($cloner, $log)->execute($this->input());
@@ -194,6 +192,12 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
self::assertSame(Rev::ofPost($this->original), $result['rev']);
self::assertSame(['replaced', 'trashed'], array_column($result['changed'], 'action'));
self::assertSame([], $result['warnings']);
$log->assertRecorded([
'ability' => 'lit/post-replace-with-copy',
'object_type' => 'page',
'object_id' => '5',
'revision_id' => 900,
]);
}
public function testAFailedTrashOfTheCopyIsAWarningOnly(): void
@@ -204,8 +208,7 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
$cloner->allows('backup')->andReturn(88);
$cloner->expects('replace');
Functions\when('wp_trash_post')->justReturn(false);
$log = Mockery::mock(ChangeLog::class);
$log->allows('record')->andReturn(11);
$log = new FakeChangeLog(11);
$result = $this->ability($cloner, $log)->execute($this->input());
@@ -225,7 +228,7 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
Functions\expect('wp_trash_post')->never();
try {
$this->ability($cloner, Mockery::mock(ChangeLog::class))->execute($this->input());
$this->ability($cloner, FakeChangeLog::forbidden())->execute($this->input());
self::fail('WriteFailed expected');
} catch (WriteFailed $e) {
self::assertTrue($e->rolledBack);
@@ -246,10 +249,7 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
$cloner->expects('replace')->andThrow(new RuntimeException('hook kaputt'));
$cloner->expects('restore')->with($this->original, self::SNAPSHOT, PostCloner::PARTS)->andThrow(new RuntimeException('Restore incomplete: terms'));
Functions\expect('wp_trash_post')->never();
$log = Mockery::mock(ChangeLog::class);
$log->expects('recordPartial')
->with('lit/post-replace-with-copy', 'page', '5', Mockery::type('string'), 900)
->andReturn(12);
$log = FakeChangeLog::partialsOnly();
try {
$this->ability($cloner, $log)->execute($this->input());
@@ -264,6 +264,12 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
self::assertStringContainsString('Sicherung', $error->get_error_message());
self::assertStringContainsString('88', $error->get_error_message());
}
$log->assertPartials([
'ability' => 'lit/post-replace-with-copy',
'object_type' => 'page',
'object_id' => '5',
'revision_id' => 900,
]);
}
public function testNeedsTheRightToCreateEntriesForTheBackup(): void
@@ -272,7 +278,7 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
$cloner = Mockery::mock(PostCloner::class);
$cloner->expects('backup')->never();
$cloner->expects('replace')->never();
$ability = $this->ability($cloner, Mockery::mock(ChangeLog::class));
$ability = $this->ability($cloner, FakeChangeLog::forbidden());
self::assertSame('lit_forbidden', $ability->permission($this->input())->get_error_code());
self::assertSame('lit_forbidden', $ability->execute($this->input())->get_error_code());
@@ -289,7 +295,7 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
Functions\expect('wp_trash_post')->never();
try {
$this->ability($cloner, Mockery::mock(ChangeLog::class))->execute($this->input());
$this->ability($cloner, FakeChangeLog::forbidden())->execute($this->input());
self::fail('WriteFailed expected');
} catch (WriteFailed $e) {
self::assertSame('lit_write_failed', $e->toError()->get_error_code());
@@ -298,7 +304,7 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
public function testRequiresRevAndIsDestructive(): void
{
$ability = $this->ability(Mockery::mock(PostCloner::class), Mockery::mock(ChangeLog::class));
$ability = $this->ability(Mockery::mock(PostCloner::class), FakeChangeLog::forbidden());
self::assertContains('rev', $ability->inputSchema()['required']);
self::assertContains('backup_id', $ability->outputSchema()['required']);