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
@@ -9,9 +9,9 @@ use Brain\Monkey\Functions;
use LIT\MCP\Abilities\Rev;
use LIT\MCP\ChangeLog\ChangeLog;
use LIT\MCP\Integrations\WordPress\Revisions\RevisionRestoreAbility;
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;
use WP_Post;
@@ -38,8 +38,7 @@ final class RevisionRestoreAbilityTest extends TestCase
private function ability(ChangeLog $log): RevisionRestoreAbility
{
$schema = Mockery::mock(ContentSchema::class);
$schema->allows('isManaged')->andReturn(true);
$schema = SchemaFixture::managed();
return new RevisionRestoreAbility($schema, $log);
}
@@ -51,14 +50,14 @@ final class RevisionRestoreAbilityTest extends TestCase
public function testNonRevisionIdIsNotFound(): void
{
self::assertSame('lit_not_found', $this->ability(Mockery::mock(ChangeLog::class))->permission(['revision_id' => 5])->get_error_code());
self::assertSame('lit_not_found', $this->ability(FakeChangeLog::forbidden())->permission(['revision_id' => 5])->get_error_code());
}
public function testRequiresConfirmation(): void
{
Functions\expect('wp_restore_post_revision')->never();
$result = $this->ability(Mockery::mock(ChangeLog::class))->execute(['revision_id' => 810]);
$result = $this->ability(FakeChangeLog::forbidden())->execute(['revision_id' => 810]);
self::assertSame('lit_confirm_required', $result->get_error_code());
}
@@ -69,7 +68,7 @@ final class RevisionRestoreAbilityTest extends TestCase
? new WP_Post(['ID' => 810, 'post_type' => 'revision', 'post_parent' => 0])
: null);
$error = $this->ability(Mockery::mock(ChangeLog::class))
$error = $this->ability(FakeChangeLog::forbidden())
->permission(['revision_id' => 810, 'rev' => 'irrelevant', 'confirm' => true]);
self::assertSame('lit_validation_failed', $error->get_error_code());
@@ -85,7 +84,7 @@ final class RevisionRestoreAbilityTest extends TestCase
});
Functions\expect('wp_restore_post_revision')->never();
$error = $this->ability(Mockery::mock(ChangeLog::class))->execute(['revision_id' => 810, 'rev' => 'x', 'confirm' => true]);
$error = $this->ability(FakeChangeLog::forbidden())->execute(['revision_id' => 810, 'rev' => 'x', 'confirm' => true]);
self::assertSame('lit_validation_failed', $error->get_error_code());
self::assertArrayHasKey('revision_id', $error->get_error_data()['fields']);
@@ -105,7 +104,7 @@ final class RevisionRestoreAbilityTest extends TestCase
Functions\expect('wp_save_post_revision')->never();
Functions\expect('wp_restore_post_revision')->never();
$error = $this->ability(Mockery::mock(ChangeLog::class))->execute([
$error = $this->ability(FakeChangeLog::forbidden())->execute([
'revision_id' => 810,
'rev' => Rev::ofPost(new WP_Post($current)),
'confirm' => true,
@@ -119,7 +118,7 @@ final class RevisionRestoreAbilityTest extends TestCase
{
Functions\expect('wp_restore_post_revision')->never();
$result = $this->ability(Mockery::mock(ChangeLog::class))
$result = $this->ability(FakeChangeLog::forbidden())
->execute(['revision_id' => 810, 'confirm' => true, 'rev' => 'stale']);
self::assertSame('lit_rev_mismatch', $result->get_error_code());
@@ -139,8 +138,7 @@ final class RevisionRestoreAbilityTest extends TestCase
});
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/revision-restore', 'page', '5', Mockery::type('string'), 813)->andReturn(12);
$log = new FakeChangeLog(12);
$result = $this->ability($log)->execute([
'revision_id' => 810,
@@ -154,6 +152,12 @@ final class RevisionRestoreAbilityTest extends TestCase
self::assertStringContainsString('2026-09-20 10:00:00', $result['summary']);
self::assertStringContainsString('Status unverändert: publish', $result['summary']);
self::assertSame([], $result['warnings']);
$log->assertRecorded([
'ability' => 'lit/revision-restore',
'object_type' => 'page',
'object_id' => '5',
'revision_id' => 813,
]);
}
public function testRestoreFailureIsWriteFailed(): void
@@ -161,8 +165,7 @@ final class RevisionRestoreAbilityTest extends TestCase
Functions\when('wp_save_post_revision')->justReturn(813);
Functions\when('wp_get_post_revisions')->justReturn([813 => new WP_Post(['ID' => 813])]);
Functions\when('wp_restore_post_revision')->justReturn(false);
$log = Mockery::mock(ChangeLog::class);
$log->expects('record')->never();
$log = FakeChangeLog::forbidden();
$result = $this->ability($log)->execute([
'revision_id' => 810,
@@ -188,8 +191,7 @@ final class RevisionRestoreAbilityTest 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);
@@ -208,5 +210,6 @@ final class RevisionRestoreAbilityTest extends TestCase
self::assertCount(2, $result['warnings']);
self::assertStringContainsString('listener kaputt', (string) file_get_contents($file));
unlink($file);
$log->assertRecorded([]);
}
}