Files
vkv/tests/Feature/Files/FileControllerTest.php
Zdeněk Burda 41e3ce6f25 Initial commit
2026-01-09 21:26:40 +01:00

87 lines
2.2 KiB
PHP

<?php
namespace Tests\Feature\Files;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;
class FileControllerTest extends TestCase
{
use RefreshDatabase;
public function test_index_returns_files(): void
{
$file = $this->createFile();
$response = $this->getJson('/api/files');
$response->assertStatus(200)
->assertJsonFragment(['id' => $file->id]);
}
public function test_show_returns_file_metadata(): void
{
$file = $this->createFile();
$response = $this->getJson("/api/files/{$file->id}");
$response->assertStatus(200)
->assertJsonFragment([
'id' => $file->id,
'filename' => $file->filename,
]);
}
public function test_store_allows_guest_before_deadline(): void
{
Storage::fake();
Queue::fake();
$round = $this->createRound(['logs_deadline' => now()->addDay()]);
$response = $this->postJson('/api/files', [
'round_id' => $round->id,
'file' => UploadedFile::fake()->create('log.edi', 5, 'text/plain'),
]);
$response->assertStatus(201);
}
public function test_content_returns_stored_file(): void
{
Storage::fake();
$file = $this->createFile(['path' => 'uploads/test/file.edi', 'mimetype' => 'text/plain']);
Storage::put($file->path, 'TEST');
$response = $this->get("/api/files/{$file->id}/content");
$response->assertStatus(200);
$this->assertSame('TEST', $response->getContent());
}
public function test_delete_requires_auth(): void
{
$file = $this->createFile();
$this->deleteJson("/api/files/{$file->id}")
->assertStatus(401);
}
public function test_admin_can_delete_file(): void
{
Storage::fake();
$this->actingAsAdmin();
$file = $this->createFile(['path' => 'uploads/test/file.edi']);
Storage::put($file->path, 'TEST');
$this->deleteJson("/api/files/{$file->id}")
->assertStatus(204);
Storage::assertMissing($file->path);
}
}