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:
@@ -8,6 +8,7 @@ use Brain\Monkey\Functions;
|
||||
use LIT\MCP\ChangeLog\ChangeLog;
|
||||
use LIT\MCP\Integrations\Media\MediaDeleteAbility;
|
||||
use LIT\MCP\Integrations\Media\MediaUsageFinder;
|
||||
use LIT\MCP\Tests\Support\FakeChangeLog;
|
||||
use LIT\MCP\Tests\Unit\TestCase;
|
||||
use Mockery;
|
||||
use RuntimeException;
|
||||
@@ -38,7 +39,7 @@ final class MediaDeleteAbilityTest extends TestCase
|
||||
{
|
||||
Functions\expect('wp_delete_attachment')->never();
|
||||
|
||||
$result = $this->ability(self::USAGE, Mockery::mock(ChangeLog::class))->execute(['id' => 33]);
|
||||
$result = $this->ability(self::USAGE, FakeChangeLog::forbidden())->execute(['id' => 33]);
|
||||
|
||||
self::assertSame('lit_confirm_required', $result->get_error_code());
|
||||
self::assertStringContainsString('endgültig', $result->get_error_message());
|
||||
@@ -48,7 +49,7 @@ final class MediaDeleteAbilityTest extends TestCase
|
||||
|
||||
public function testConfirmationMentionsTheTrashWhenMediaTrashIsOn(): void
|
||||
{
|
||||
$result = $this->ability([], Mockery::mock(ChangeLog::class), true)->execute(['id' => 33, 'confirm' => false]);
|
||||
$result = $this->ability([], FakeChangeLog::forbidden(), true)->execute(['id' => 33, 'confirm' => false]);
|
||||
|
||||
self::assertSame('lit_confirm_required', $result->get_error_code());
|
||||
self::assertStringContainsString('Papierkorb', $result->get_error_message());
|
||||
@@ -58,7 +59,7 @@ final class MediaDeleteAbilityTest extends TestCase
|
||||
{
|
||||
Functions\expect('wp_delete_attachment')->never();
|
||||
|
||||
$result = $this->ability(self::USAGE, Mockery::mock(ChangeLog::class))->execute(['id' => 33, 'confirm' => true]);
|
||||
$result = $this->ability(self::USAGE, FakeChangeLog::forbidden())->execute(['id' => 33, 'confirm' => true]);
|
||||
|
||||
self::assertSame('lit_in_use', $result->get_error_code());
|
||||
self::assertSame(self::USAGE, $result->get_error_data()['usages']);
|
||||
@@ -68,7 +69,7 @@ final class MediaDeleteAbilityTest extends TestCase
|
||||
{
|
||||
Functions\expect('wp_delete_attachment')->never();
|
||||
|
||||
$result = $this->ability(self::POSSIBLE, Mockery::mock(ChangeLog::class))->execute(['id' => 33, 'confirm' => true]);
|
||||
$result = $this->ability(self::POSSIBLE, FakeChangeLog::forbidden())->execute(['id' => 33, 'confirm' => true]);
|
||||
|
||||
self::assertSame('lit_in_use', $result->get_error_code());
|
||||
}
|
||||
@@ -76,25 +77,35 @@ final class MediaDeleteAbilityTest extends TestCase
|
||||
public function testForceDeletesUsedMedia(): void
|
||||
{
|
||||
Functions\expect('wp_delete_attachment')->once()->with(33, false)->andReturn(new WP_Post(['ID' => 33]));
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->expects('record')->with('lit/media-delete', 'attachment', '33', 'Medium "Foto" gelöscht')->andReturn(50);
|
||||
$log = new FakeChangeLog(50);
|
||||
|
||||
$result = $this->ability(self::USAGE, $log)->execute(['id' => 33, 'confirm' => true, 'force' => true]);
|
||||
|
||||
self::assertSame(['id' => '33', 'type' => 'attachment', 'action' => 'deleted', 'label' => 'Foto'], $result['changed'][0]);
|
||||
self::assertSame(50, $result['log_id']);
|
||||
self::assertSame(self::USAGE, $result['usages']);
|
||||
$log->assertRecorded([
|
||||
'ability' => 'lit/media-delete',
|
||||
'object_type' => 'attachment',
|
||||
'object_id' => '33',
|
||||
'summary' => 'Medium "Foto" gelöscht',
|
||||
]);
|
||||
}
|
||||
|
||||
public function testUnusedMediaGoesToTheTrashWhenMediaTrashIsOn(): void
|
||||
{
|
||||
Functions\expect('wp_delete_attachment')->once()->with(33, false)->andReturn(new WP_Post(['ID' => 33]));
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->expects('record')->with('lit/media-delete', 'attachment', '33', 'Medium "Foto" in den Papierkorb gelegt')->andReturn(51);
|
||||
$log = new FakeChangeLog(51);
|
||||
|
||||
$result = $this->ability([], $log, true)->execute(['id' => 33, 'confirm' => true]);
|
||||
|
||||
self::assertSame('trashed', $result['changed'][0]['action']);
|
||||
$log->assertRecorded([
|
||||
'ability' => 'lit/media-delete',
|
||||
'object_type' => 'attachment',
|
||||
'object_id' => '33',
|
||||
'summary' => 'Medium "Foto" in den Papierkorb gelegt',
|
||||
]);
|
||||
}
|
||||
|
||||
public function testRefusedDeleteWithTheAttachmentIntactIsAWriteFailure(): void
|
||||
@@ -102,7 +113,7 @@ final class MediaDeleteAbilityTest extends TestCase
|
||||
Functions\when('get_post_meta')->justReturn('2026/09/foto.jpg');
|
||||
Functions\expect('wp_delete_attachment')->once()->andReturn(false);
|
||||
|
||||
$result = $this->ability([], Mockery::mock(ChangeLog::class))->execute(['id' => 33, 'confirm' => true]);
|
||||
$result = $this->ability([], FakeChangeLog::forbidden())->execute(['id' => 33, 'confirm' => true]);
|
||||
|
||||
self::assertSame('lit_write_failed', $result->get_error_code());
|
||||
}
|
||||
@@ -118,13 +129,13 @@ final class MediaDeleteAbilityTest extends TestCase
|
||||
|
||||
return null;
|
||||
});
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->expects('recordPartial')->once()->with('lit/media-delete', 'attachment', '33', Mockery::type('string'));
|
||||
$log = FakeChangeLog::partialsOnly();
|
||||
|
||||
$result = $this->ability([], $log)->execute(['id' => 33, 'confirm' => true]);
|
||||
|
||||
self::assertSame('lit_partial_write', $result->get_error_code());
|
||||
self::assertSame(33, $result->get_error_data()['attachment_id']);
|
||||
$log->assertPartials(['ability' => 'lit/media-delete', 'object_type' => 'attachment', 'object_id' => '33']);
|
||||
}
|
||||
|
||||
public function testADatabaseErrorInTheUsageCheckRefusesWithoutForce(): void
|
||||
@@ -138,7 +149,7 @@ final class MediaDeleteAbilityTest extends TestCase
|
||||
$db->allows('get_results')->andReturn(null);
|
||||
$ability = new MediaDeleteAbility(
|
||||
new MediaUsageFinder(static fn (): array => [], $db),
|
||||
Mockery::mock(ChangeLog::class),
|
||||
FakeChangeLog::forbidden(),
|
||||
static fn (): bool => false
|
||||
);
|
||||
|
||||
@@ -158,8 +169,7 @@ final class MediaDeleteAbilityTest extends TestCase
|
||||
public function testChangeLogFailureIsOnlyAWarning(): void
|
||||
{
|
||||
Functions\expect('wp_delete_attachment')->once()->andReturn(new WP_Post(['ID' => 33]));
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->expects('record')->andThrow(new RuntimeException('db down'));
|
||||
$log = new FakeChangeLog(1, new RuntimeException('db down'));
|
||||
|
||||
$file = (string) tempnam(sys_get_temp_dir(), 'lit-mcp-log');
|
||||
$previous = ini_set('error_log', $file);
|
||||
@@ -173,6 +183,7 @@ final class MediaDeleteAbilityTest extends TestCase
|
||||
self::assertSame('deleted', $result['changed'][0]['action']);
|
||||
self::assertNull($result['log_id']);
|
||||
self::assertCount(1, $result['warnings']);
|
||||
$log->assertRecorded([]);
|
||||
}
|
||||
|
||||
public function testTrashedAttachmentIsRefused(): void
|
||||
@@ -180,7 +191,7 @@ final class MediaDeleteAbilityTest extends TestCase
|
||||
Functions\when('get_post')->justReturn(new WP_Post(['ID' => 33, 'post_type' => 'attachment', 'post_status' => 'trash']));
|
||||
Functions\expect('wp_delete_attachment')->never();
|
||||
|
||||
$ability = $this->ability([], Mockery::mock(ChangeLog::class));
|
||||
$ability = $this->ability([], FakeChangeLog::forbidden());
|
||||
|
||||
self::assertSame('lit_validation_failed', $ability->permission(['id' => 33])->get_error_code());
|
||||
self::assertSame('lit_validation_failed', $ability->execute(['id' => 33, 'confirm' => true])->get_error_code());
|
||||
@@ -190,14 +201,14 @@ final class MediaDeleteAbilityTest extends TestCase
|
||||
{
|
||||
Functions\when('current_user_can')->alias(static fn (string $cap, int $id): bool => ! ($cap === 'delete_post' && $id === 33));
|
||||
|
||||
$result = $this->ability([], Mockery::mock(ChangeLog::class))->permission(['id' => 33]);
|
||||
$result = $this->ability([], FakeChangeLog::forbidden())->permission(['id' => 33]);
|
||||
|
||||
self::assertSame('lit_forbidden', $result->get_error_code());
|
||||
}
|
||||
|
||||
public function testIsDestructiveAndDescribesWhatTheUsageCheckMisses(): void
|
||||
{
|
||||
$ability = $this->ability([], Mockery::mock(ChangeLog::class));
|
||||
$ability = $this->ability([], FakeChangeLog::forbidden());
|
||||
|
||||
self::assertSame(['readonly' => false, 'destructive' => true, 'idempotent' => false], $ability->annotations());
|
||||
self::assertFalse($ability->isDirect());
|
||||
|
||||
@@ -5,10 +5,10 @@ declare(strict_types=1);
|
||||
namespace LIT\MCP\Tests\Unit\Integrations\Media;
|
||||
|
||||
use LIT\MCP\Abilities\Ability;
|
||||
use LIT\MCP\ChangeLog\ChangeLog;
|
||||
use LIT\MCP\Integrations\Media\MediaIntegration;
|
||||
use LIT\MCP\Integrations\WordPress\Schema\ContentSchema;
|
||||
use LIT\MCP\Limits;
|
||||
use LIT\MCP\Tests\Support\FakeChangeLog;
|
||||
use LIT\MCP\Tests\Unit\TestCase;
|
||||
use Mockery;
|
||||
|
||||
@@ -28,7 +28,7 @@ final class MediaIntegrationTest extends TestCase
|
||||
|
||||
public function testProvidesTheMediaAbilities(): void
|
||||
{
|
||||
$integration = new MediaIntegration(Mockery::mock(ContentSchema::class), Mockery::mock(ChangeLog::class), new Limits());
|
||||
$integration = new MediaIntegration(Mockery::mock(ContentSchema::class), FakeChangeLog::forbidden(), new Limits());
|
||||
|
||||
self::assertSame('media', $integration->id());
|
||||
self::assertTrue($integration->isAvailable());
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -13,8 +13,9 @@ use LIT\MCP\Integrations\Media\MediaPresenter;
|
||||
use LIT\MCP\Integrations\Media\MediaUploadAbility;
|
||||
use LIT\MCP\Integrations\Media\MediaUploadFromUrlAbility;
|
||||
use LIT\MCP\Integrations\Media\MediaUploads;
|
||||
use LIT\MCP\Integrations\WordPress\Schema\ContentSchema;
|
||||
use LIT\MCP\Limits;
|
||||
use LIT\MCP\Tests\Support\FakeChangeLog;
|
||||
use LIT\MCP\Tests\Support\SchemaFixture;
|
||||
use LIT\MCP\Tests\Unit\TestCase;
|
||||
use Mockery;
|
||||
use RuntimeException;
|
||||
@@ -26,10 +27,9 @@ final class MediaUploadAbilitiesTest extends TestCase
|
||||
{
|
||||
$presenter = Mockery::mock(MediaPresenter::class);
|
||||
$presenter->allows('summary')->andReturn(['id' => 55, 'title' => 'Logo']);
|
||||
$schema = Mockery::mock(ContentSchema::class);
|
||||
$schema->allows('isManaged')->andReturn(true);
|
||||
$schema = SchemaFixture::managed();
|
||||
|
||||
return new MediaUploads($presenter, $log ?? Mockery::mock(ChangeLog::class), $schema);
|
||||
return new MediaUploads($presenter, $log ?? FakeChangeLog::forbidden(), $schema);
|
||||
}
|
||||
|
||||
private function savedAttachment(): void
|
||||
@@ -43,8 +43,7 @@ final class MediaUploadAbilitiesTest extends TestCase
|
||||
$this->savedAttachment();
|
||||
$importer = Mockery::mock(MediaImporter::class);
|
||||
$importer->expects('fromUrl')->with('https://example.com/logo.png', null, ['alt' => 'Logo'])->andReturn(55);
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->expects('record')->with('lit/media-upload-from-url', 'attachment', '55', Mockery::type('string'))->andReturn(3);
|
||||
$log = new FakeChangeLog(3);
|
||||
|
||||
$result = (new MediaUploadFromUrlAbility($importer, $this->uploads($log)))
|
||||
->execute(['url' => 'https://example.com/logo.png', 'alt' => 'Logo']);
|
||||
@@ -54,6 +53,11 @@ final class MediaUploadAbilitiesTest extends TestCase
|
||||
self::assertSame(3, $result['log_id']);
|
||||
self::assertSame('https://kunde.de/wp-admin/post.php?post=55&action=edit', $result['edit_url']);
|
||||
self::assertSame([], $result['warnings']);
|
||||
$log->assertRecorded([
|
||||
'ability' => 'lit/media-upload-from-url',
|
||||
'object_type' => 'attachment',
|
||||
'object_id' => '55',
|
||||
]);
|
||||
}
|
||||
|
||||
public function testBase64UploadPassesFilenameDataAndMeta(): void
|
||||
@@ -61,14 +65,14 @@ final class MediaUploadAbilitiesTest extends TestCase
|
||||
$this->savedAttachment();
|
||||
$importer = Mockery::mock(MediaImporter::class);
|
||||
$importer->expects('fromBase64')->with('AAAA', 'x.png', ['title' => 'Logo', 'post_id' => 5])->andReturn(55);
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->expects('record')->with('lit/media-upload', 'attachment', '55', Mockery::type('string'))->andReturn(4);
|
||||
$log = new FakeChangeLog(4);
|
||||
|
||||
$result = (new MediaUploadAbility($importer, $this->uploads($log), new Limits()))
|
||||
->execute(['filename' => 'x.png', 'data' => 'AAAA', 'title' => 'Logo', 'post_id' => 5]);
|
||||
|
||||
self::assertTrue($result['ok']);
|
||||
self::assertSame(4, $result['log_id']);
|
||||
$log->assertRecorded(['ability' => 'lit/media-upload', 'object_type' => 'attachment', 'object_id' => '55']);
|
||||
}
|
||||
|
||||
public function testAFailingChangeLogIsOnlyAWarning(): void
|
||||
@@ -76,8 +80,7 @@ final class MediaUploadAbilitiesTest extends TestCase
|
||||
$this->savedAttachment();
|
||||
$importer = Mockery::mock(MediaImporter::class);
|
||||
$importer->allows('fromUrl')->andReturn(55);
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->allows('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-log');
|
||||
$previous = ini_set('error_log', $file);
|
||||
@@ -111,11 +114,19 @@ final class MediaUploadAbilitiesTest extends TestCase
|
||||
$failure = WriteFailed::partial('Alt-Text fehlt.', postId: 55);
|
||||
$importer = Mockery::mock(MediaImporter::class);
|
||||
$importer->allows('fromUrl')->andThrow($failure);
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->expects('recordPartial')->with('lit/media-upload-from-url', 'attachment', '55', Mockery::type('string'));
|
||||
$log = FakeChangeLog::partialsOnly();
|
||||
|
||||
$this->expectExceptionObject($failure);
|
||||
(new MediaUploadFromUrlAbility($importer, $this->uploads($log)))->execute(['url' => 'https://example.com/a.png']);
|
||||
try {
|
||||
(new MediaUploadFromUrlAbility($importer, $this->uploads($log)))->execute(['url' => 'https://example.com/a.png']);
|
||||
self::fail('WriteFailed expected');
|
||||
} catch (WriteFailed $e) {
|
||||
self::assertSame($failure, $e);
|
||||
}
|
||||
$log->assertPartials([
|
||||
'ability' => 'lit/media-upload-from-url',
|
||||
'object_type' => 'attachment',
|
||||
'object_id' => '55',
|
||||
]);
|
||||
}
|
||||
|
||||
public function testPermissionNeedsUploadRightsAndEditRightsOnTheParent(): void
|
||||
|
||||
Reference in New Issue
Block a user