Files
L-Ami-Fiduciaire/tests/Feature/Declaration/MediaDownloadTest.php
Saad Ibn-Ezzoubayr c89d1879bf feat: complete Epic 1 — team management & permission system
- Story 1.1: Permission enum, config, AuthorizesPermissions & HasWorkspaceScope traits, member→worker migration
- Story 1.2: Team page with member list, invitation system with queued email
- Story 1.3: Role assignment (Manager/Worker) and member removal with activity logging
- Story 1.4: Owner-only permission toggle matrix for Managers (manage team, view logs, configure portal)
- Story 1.5: Role-based access enforcement — Workers see only assigned declarations/clients, sidebar scoping
- Story 1.6: Workspace switcher dropdown for multi-workspace users with session-based switching
- 83 new/modified files, 182 tests passing with zero regressions

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-18 00:12:50 +00:00

104 lines
3.5 KiB
PHP

<?php
use App\Models\Client;
use App\Models\Declaration;
use App\Models\MediaDownload;
use App\Models\User;
use App\Models\Workspace;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
function setupDeclarationWithMedia(): array
{
$user = User::factory()->create();
$workspace = Workspace::factory()->create();
$workspace->users()->attach($user, ['role' => 'owner']);
$client = Client::factory()->create(['workspace_id' => $workspace->id]);
$declaration = Declaration::factory()->create([
'workspace_id' => $workspace->id,
'client_id' => $client->id,
]);
Storage::fake('public');
$file = UploadedFile::fake()->create('document.pdf', 100, 'application/pdf');
$media = $declaration->addMedia($file)->toMediaCollection('documents');
return [$user, $workspace, $declaration, $media];
}
test('downloading creates a media download record', function () {
[$user, $workspace, $declaration, $media] = setupDeclarationWithMedia();
session(['current_workspace_id' => $workspace->id]);
$this->actingAs($user)->get(route('declarations.media.download', [
'declaration' => $declaration,
'mediaId' => $media->id,
]));
$download = MediaDownload::query()
->where('media_id', $media->id)
->where('user_id', $user->id)
->first();
expect($download)->not->toBeNull();
expect($download->downloaded_at)->not->toBeNull();
});
test('re-downloading updates timestamp without creating duplicates', function () {
[$user, $workspace, $declaration, $media] = setupDeclarationWithMedia();
session(['current_workspace_id' => $workspace->id]);
$this->actingAs($user)->get(route('declarations.media.download', [
'declaration' => $declaration,
'mediaId' => $media->id,
]));
$firstDownload = MediaDownload::query()
->where('media_id', $media->id)
->where('user_id', $user->id)
->first();
$firstTimestamp = $firstDownload->downloaded_at;
$this->travel(5)->minutes();
$this->actingAs($user)->get(route('declarations.media.download', [
'declaration' => $declaration,
'mediaId' => $media->id,
]));
$count = MediaDownload::query()
->where('media_id', $media->id)
->where('user_id', $user->id)
->count();
expect($count)->toBe(1);
$firstDownload->refresh();
expect($firstDownload->downloaded_at->gt($firstTimestamp))->toBeTrue();
});
test('download status is per-user in show endpoint', function () {
[$user, $workspace, $declaration, $media] = setupDeclarationWithMedia();
$otherUser = User::factory()->create();
$workspace->users()->attach($otherUser, ['role' => 'manager']);
session(['current_workspace_id' => $workspace->id]);
MediaDownload::query()->create([
'media_id' => $media->id,
'user_id' => $user->id,
'downloaded_at' => now(),
]);
$response = $this->actingAs($user)->get(route('declarations.show', $declaration));
$response->assertOk();
$documents = $response->original->getData()['page']['props']['documents'];
$doc = collect($documents)->firstWhere('id', $media->id);
expect($doc['is_downloaded'])->toBeTrue();
$response2 = $this->actingAs($otherUser)->get(route('declarations.show', $declaration));
$response2->assertOk();
$documents2 = $response2->original->getData()['page']['props']['documents'];
$doc2 = collect($documents2)->firstWhere('id', $media->id);
expect($doc2['is_downloaded'])->toBeFalse();
});