presseportale/tests/Feature/SyncPressReleaseImagesTest.php
Kevin Adametz 5b8bdf4182
Some checks are pending
linter / quality (push) Waiting to run
tests / ci (push) Waiting to run
12-05-2026 Frontend dev
2026-05-12 18:32:33 +02:00

140 lines
4.5 KiB
PHP

<?php
use App\Enums\Portal;
use App\Models\PressRelease;
use App\Models\PressReleaseImage;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;
function fakeImageBytes(int $width = 800, int $height = 600): string
{
return UploadedFile::fake()->image('seed.jpg', $width, $height)->get();
}
test('legacy press release images are copied with variants and database is updated', function () {
/** @var TestCase $this */
Storage::fake('public');
Storage::disk('public')->put('presseecho/press_release/photo-a.jpg', fakeImageBytes(1600, 1200));
Storage::disk('public')->put('presseecho/press_release/unused.jpg', fakeImageBytes());
$pr = PressRelease::factory()->create([
'portal' => Portal::Presseecho->value,
'legacy_portal' => Portal::Presseecho->value,
]);
$image = PressReleaseImage::query()->create([
'press_release_id' => $pr->id,
'legacy_portal' => Portal::Presseecho->value,
'legacy_id' => 99,
'disk' => 'public',
'path' => 'photo-a.jpg',
]);
$this->artisan('legacy:sync-press-release-images', [
'--portal' => Portal::Presseecho->value,
])
->assertSuccessful()
->expectsOutputToContain('referenziert 1, kopiert 1');
$image->refresh();
expect($image->path)->toBe("press-releases/{$pr->id}/images/photo-a.jpg");
expect(Storage::disk('public')->exists($image->path))->toBeTrue();
expect($image->variants)->toBeArray();
expect($image->variants)->toHaveKeys(['thumb', 'medium', 'large']);
foreach ($image->variants as $variantPath) {
expect(Storage::disk('public')->exists($variantPath))->toBeTrue();
}
});
test('legacy press release image sync dry run does not copy or update anything', function () {
/** @var TestCase $this */
Storage::fake('public');
Storage::disk('public')->put('businessportal24/press_release/photo-b.jpg', fakeImageBytes());
$pr = PressRelease::factory()->create([
'portal' => Portal::Businessportal24->value,
'legacy_portal' => Portal::Businessportal24->value,
]);
$image = PressReleaseImage::query()->create([
'press_release_id' => $pr->id,
'legacy_portal' => Portal::Businessportal24->value,
'legacy_id' => 5,
'disk' => 'public',
'path' => 'photo-b.jpg',
]);
$this->artisan('legacy:sync-press-release-images', [
'--portal' => Portal::Businessportal24->value,
'--dry-run' => true,
])
->assertSuccessful()
->expectsOutputToContain('(Dry-Run)');
$image->refresh();
expect($image->path)->toBe('photo-b.jpg');
expect($image->variants)->toBeNull();
expect(Storage::disk('public')->exists("press-releases/{$pr->id}/images/photo-b.jpg"))->toBeFalse();
});
test('legacy press release image sync reports missing files', function () {
/** @var TestCase $this */
Storage::fake('public');
$pr = PressRelease::factory()->create([
'portal' => Portal::Presseecho->value,
'legacy_portal' => Portal::Presseecho->value,
]);
PressReleaseImage::query()->create([
'press_release_id' => $pr->id,
'legacy_portal' => Portal::Presseecho->value,
'legacy_id' => 7,
'disk' => 'public',
'path' => 'gone.jpg',
]);
$this->artisan('legacy:sync-press-release-images', [
'--portal' => Portal::Presseecho->value,
])
->assertFailed()
->expectsOutputToContain('Fehlt: presseecho/press_release/gone.jpg');
});
test('legacy press release image sync skips already migrated paths but generates missing variants', function () {
/** @var TestCase $this */
Storage::fake('public');
$pr = PressRelease::factory()->create([
'portal' => Portal::Presseecho->value,
'legacy_portal' => Portal::Presseecho->value,
]);
Storage::disk('public')->put("press-releases/{$pr->id}/images/already-here.jpg", fakeImageBytes(1200, 900));
$image = PressReleaseImage::query()->create([
'press_release_id' => $pr->id,
'legacy_portal' => Portal::Presseecho->value,
'legacy_id' => 11,
'disk' => 'public',
'path' => "press-releases/{$pr->id}/images/already-here.jpg",
'variants' => null,
]);
$this->artisan('legacy:sync-press-release-images', [
'--portal' => Portal::Presseecho->value,
])
->assertSuccessful()
->expectsOutputToContain('bereits synchron 1');
$image->refresh();
expect($image->variants)->toBeArray();
expect($image->variants)->toHaveKeys(['thumb', 'medium', 'large']);
});