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,8 +8,8 @@ use Brain\Monkey\Functions;
use LIT\MCP\Abilities\WriteFailed;
use LIT\MCP\ChangeLog\ChangeLog;
use LIT\MCP\Integrations\Media\MediaUpdateAbility;
use LIT\MCP\Tests\Support\FakeChangeLog;
use LIT\MCP\Tests\Unit\TestCase;
use Mockery;
use RuntimeException;
use WP_Error;
use WP_Post;
@@ -53,25 +53,15 @@ final class MediaUpdateAbilityTest extends TestCase
});
}
private function log(int ...$ids): ChangeLog
/** For paths that must not log. */
private function log(): ChangeLog
{
$log = Mockery::mock(ChangeLog::class);
if ($ids !== []) {
$log->expects('record')->times(count($ids))->andReturn(...$ids);
}
return $log;
return FakeChangeLog::forbidden();
}
public function testUpdatesEveryItemAndReportsPerItem(): void
{
$log = Mockery::mock(ChangeLog::class);
$log->expects('record')
->with('lit/media-update', 'attachment', '33', 'Metadaten von "Foto" geändert: alt, caption')
->andReturn(41);
$log->expects('record')
->with('lit/media-update', 'attachment', '34', 'Metadaten von "Logo" geändert: title')
->andReturn(42);
$log = new FakeChangeLog(41);
$result = (new MediaUpdateAbility($log))->execute(['items' => [
['id' => 33, 'alt' => 'Team vor dem Haus', 'caption' => 'Unser <script>x</script>Team'],
@@ -84,6 +74,12 @@ final class MediaUpdateAbilityTest extends TestCase
self::assertSame(42, $result['results'][1]['log_id']);
self::assertSame(['id' => '34', 'type' => 'attachment', 'action' => 'updated', 'label' => 'Firmenlogo'], $result['changed'][1]);
self::assertSame('2 von 2 Medien aktualisiert.', $result['summary']);
$log->assertRecorded(
['ability' => 'lit/media-update', 'object_type' => 'attachment', 'object_id' => '33',
'summary' => 'Metadaten von "Foto" geändert: alt, caption'],
['ability' => 'lit/media-update', 'object_type' => 'attachment', 'object_id' => '34',
'summary' => 'Metadaten von "Logo" geändert: title']
);
}
public function testAnInvalidItemRefusesTheWholeBatchAndNamesEveryFailure(): void
@@ -226,8 +222,7 @@ final class MediaUpdateAbilityTest extends TestCase
return true;
});
$log = Mockery::mock(ChangeLog::class);
$log->expects('recordPartial')->once()->with('lit/media-update', 'attachment', '33', Mockery::type('string'));
$log = FakeChangeLog::partialsOnly();
try {
(new MediaUpdateAbility($log))->execute(['items' => [
@@ -241,12 +236,12 @@ final class MediaUpdateAbilityTest extends TestCase
self::assertSame([33], $error->get_error_data()['changed_ids']);
self::assertStringContainsString('33', $error->get_error_message());
}
$log->assertPartials(['ability' => 'lit/media-update', 'object_type' => 'attachment', 'object_id' => '33']);
}
public function testChangeLogFailureIsOnlyAWarning(): void
{
$log = Mockery::mock(ChangeLog::class);
$log->expects('record')->andThrow(new RuntimeException('db down'));
$log = new FakeChangeLog(1, new RuntimeException('db down'));
// error_log() is a PHP internal and cannot be stubbed; redirect it to a temp file instead.
$file = (string) tempnam(sys_get_temp_dir(), 'lit-mcp-log');
$previous = ini_set('error_log', $file);
@@ -260,6 +255,7 @@ final class MediaUpdateAbilityTest extends TestCase
self::assertTrue($result['results'][0]['ok']);
self::assertNull($result['results'][0]['log_id']);
self::assertCount(1, $result['warnings']);
$log->assertRecorded([]);
}
public function testIsIdempotentIndirectAndNotDestructive(): void