Files
fotospiel-app/tests/Feature/ArchiveEventMediaAssetsTest.php
Codex Agent eeffe4c6f1
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (push) Has been cancelled
tests / ui (push) Has been cancelled
Add checksum validation for archived media
2026-01-30 11:29:40 +01:00

130 lines
4.1 KiB
PHP

<?php
namespace Tests\Feature;
use App\Jobs\ArchiveEventMediaAssets;
use App\Models\Event;
use App\Models\EventMediaAsset;
use App\Models\MediaStorageTarget;
use App\Services\Storage\EventStorageManager;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;
class ArchiveEventMediaAssetsTest extends TestCase
{
use RefreshDatabase;
public function test_archives_asset_and_verifies_checksum(): void
{
Storage::fake('hot-disk');
Storage::fake('archive-disk');
$hotTarget = MediaStorageTarget::create([
'key' => 'hot-disk',
'name' => 'Hot Disk',
'driver' => 'local',
'config' => [],
'is_hot' => true,
'is_default' => true,
'is_active' => true,
'priority' => 100,
]);
MediaStorageTarget::create([
'key' => 'archive-disk',
'name' => 'Archive Disk',
'driver' => 'local',
'config' => [],
'is_hot' => false,
'is_default' => true,
'is_active' => true,
'priority' => 100,
]);
$event = Event::factory()->create();
$path = 'events/'.$event->id.'/photo.jpg';
Storage::disk('hot-disk')->put($path, 'photo-body');
$checksum = hash('sha256', 'photo-body');
$asset = EventMediaAsset::create([
'event_id' => $event->id,
'media_storage_target_id' => $hotTarget->id,
'photo_id' => null,
'variant' => 'original',
'disk' => 'hot-disk',
'path' => $path,
'size_bytes' => 10,
'checksum' => $checksum,
'status' => 'hot',
]);
(new ArchiveEventMediaAssets($event->id, true))->handle(app(EventStorageManager::class));
$asset->refresh();
$this->assertSame('archived', $asset->status);
$this->assertSame('archive-disk', $asset->disk);
$this->assertSame('verified', data_get($asset->meta, 'checksum_status'));
$this->assertNotEmpty(data_get($asset->meta, 'checksum_verified_at'));
Storage::disk('archive-disk')->assertExists($path);
Storage::disk('hot-disk')->assertMissing($path);
}
public function test_marks_asset_failed_on_checksum_mismatch(): void
{
Storage::fake('hot-disk');
Storage::fake('archive-disk');
$hotTarget = MediaStorageTarget::create([
'key' => 'hot-disk',
'name' => 'Hot Disk',
'driver' => 'local',
'config' => [],
'is_hot' => true,
'is_default' => true,
'is_active' => true,
'priority' => 100,
]);
MediaStorageTarget::create([
'key' => 'archive-disk',
'name' => 'Archive Disk',
'driver' => 'local',
'config' => [],
'is_hot' => false,
'is_default' => true,
'is_active' => true,
'priority' => 100,
]);
$event = Event::factory()->create();
$path = 'events/'.$event->id.'/photo.jpg';
Storage::disk('hot-disk')->put($path, 'photo-body');
$asset = EventMediaAsset::create([
'event_id' => $event->id,
'media_storage_target_id' => $hotTarget->id,
'photo_id' => null,
'variant' => 'original',
'disk' => 'hot-disk',
'path' => $path,
'size_bytes' => 10,
'checksum' => hash('sha256', 'different-body'),
'status' => 'hot',
]);
(new ArchiveEventMediaAssets($event->id, true))->handle(app(EventStorageManager::class));
$asset->refresh();
$this->assertSame('failed', $asset->status);
$this->assertSame('checksum_mismatch', $asset->error_message);
$this->assertSame('mismatch', data_get($asset->meta, 'checksum_status'));
$this->assertSame('hot-disk', $asset->disk);
Storage::disk('hot-disk')->assertExists($path);
Storage::disk('archive-disk')->assertMissing($path);
}
}