Files
glastree/tests/Feature/DocumentoLinkTest.php
T

312 lines
9.4 KiB
PHP
Raw Normal View History

2026-06-23 11:12:28 +02:00
<?php
declare(strict_types=1);
namespace Tests\Feature;
use App\Models\Documento;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class DocumentoLinkTest extends TestCase
{
use RefreshDatabase;
private User $admin;
protected function setUp(): void
{
parent::setUp();
$this->admin = User::factory()->create([
'is_admin' => true,
'permissions' => ['documenti' => User::LEVEL_FULL],
]);
}
public function test_admin_can_create_link_document(): void
{
$this->actingAs($this->admin);
$response = $this->post('/documenti', [
'nome_file' => 'Documento di prova',
'tipologia' => 'documento',
'tipo_caricamento' => 'link',
'url' => 'https://example.com/document.pdf',
'visibilita' => 'pubblico',
'tags' => [],
]);
$response->assertSessionHas('success');
$response->assertRedirect();
$this->assertDatabaseHas('documenti', [
'nome_file' => 'Documento di prova',
'tipo' => 'link',
'url' => 'https://example.com/document.pdf',
]);
}
public function test_create_link_requires_url(): void
{
$this->actingAs($this->admin);
$response = $this->post('/documenti', [
'nome_file' => 'Documento senza url',
'tipologia' => 'documento',
'tipo_caricamento' => 'link',
'url' => '',
'visibilita' => 'pubblico',
]);
$response->assertSessionHasErrors('url');
}
public function test_create_link_validates_url_format(): void
{
$this->actingAs($this->admin);
$response = $this->post('/documenti', [
'nome_file' => 'Documento url errato',
'tipologia' => 'documento',
'tipo_caricamento' => 'link',
'url' => 'not-a-valid-url',
'visibilita' => 'pubblico',
]);
$response->assertSessionHasErrors('url');
}
public function test_create_link_does_not_require_file(): void
{
$this->actingAs($this->admin);
$response = $this->post('/documenti', [
'nome_file' => 'Solo link',
'tipologia' => 'documento',
'tipo_caricamento' => 'link',
'url' => 'https://example.com/doc.pdf',
'visibilita' => 'pubblico',
]);
$response->assertSessionHas('success');
}
public function test_create_link_sets_tipo_to_link(): void
{
$this->actingAs($this->admin);
$this->post('/documenti', [
'nome_file' => 'Link test',
'tipologia' => 'documento',
'tipo_caricamento' => 'link',
'url' => 'https://example.com/doc.pdf',
'visibilita' => 'pubblico',
]);
$doc = Documento::where('nome_file', 'Link test')->first();
$this->assertNotNull($doc);
$this->assertEquals('link', $doc->tipo);
$this->assertTrue($doc->isLink());
$this->assertNull($doc->file_path);
$this->assertNull($doc->mime_type);
$this->assertNull($doc->dimensione);
$this->assertNull($doc->repository_id);
}
public function test_create_link_with_visibility_individuo(): void
{
$this->actingAs($this->admin);
$individuo = \App\Models\Individuo::create([
'nome' => 'Test',
'cognome' => 'Individuo',
]);
$this->post('/documenti', [
'nome_file' => 'Link per individuo',
'tipologia' => 'documento',
'tipo_caricamento' => 'link',
'url' => 'https://example.com/doc.pdf',
'visibilita' => 'individuo',
'visibilita_target_id' => $individuo->id,
]);
$doc = Documento::where('nome_file', 'Link per individuo')->first();
$this->assertNotNull($doc);
$this->assertEquals(\App\Models\Individuo::class, $doc->visibilita_target_type);
$this->assertEquals($individuo->id, $doc->visibilita_target_id);
}
public function test_create_link_with_visibility_gruppo(): void
{
$this->actingAs($this->admin);
$gruppo = \App\Models\Gruppo::create([
'nome' => 'Gruppo di test',
]);
$this->post('/documenti', [
'nome_file' => 'Link per gruppo',
'tipologia' => 'documento',
'tipo_caricamento' => 'link',
'url' => 'https://example.com/doc.pdf',
'visibilita' => 'gruppo',
'visibilita_target_id' => $gruppo->id,
]);
$doc = Documento::where('nome_file', 'Link per gruppo')->first();
$this->assertNotNull($doc);
$this->assertEquals(\App\Models\Gruppo::class, $doc->visibilita_target_type);
$this->assertEquals($gruppo->id, $doc->visibilita_target_id);
}
public function test_download_link_redirects_to_url(): void
{
$this->actingAs($this->admin);
$doc = Documento::create([
'nome_file' => 'Link download test',
'tipo' => 'link',
'url' => 'https://example.com/download.pdf',
'tipologia' => 'documento',
'visibilita' => 'pubblico',
'user_id' => $this->admin->id,
]);
$response = $this->get("/documenti/{$doc->id}/download");
$response->assertRedirect('https://example.com/download.pdf');
}
public function test_preview_link_returns_fallback_view(): void
{
$this->actingAs($this->admin);
$doc = Documento::create([
'nome_file' => 'Link preview test',
'tipo' => 'link',
'url' => 'https://example.com/preview.pdf',
'tipologia' => 'documento',
'visibilita' => 'pubblico',
'user_id' => $this->admin->id,
]);
$response = $this->get("/documenti/{$doc->id}/preview");
$response->assertStatus(200);
$response->assertHeader('X-Preview-Fallback', 'true');
$response->assertSee('Apri link esterno');
}
public function test_update_link_url(): void
{
$this->actingAs($this->admin);
$doc = Documento::create([
'nome_file' => 'Link update test',
'tipo' => 'link',
'url' => 'https://example.com/old.pdf',
'tipologia' => 'documento',
'visibilita' => 'pubblico',
'user_id' => $this->admin->id,
]);
$response = $this->put("/documenti/{$doc->id}", [
'nome_file' => 'Link update test',
'tipologia' => 'documento',
'url' => 'https://example.com/new.pdf',
'note' => 'Aggiornato',
]);
$response->assertSessionHas('success');
$doc->refresh();
$this->assertEquals('https://example.com/new.pdf', $doc->url);
}
public function test_update_link_preserves_url_when_not_sent(): void
{
$this->actingAs($this->admin);
$doc = Documento::create([
'nome_file' => 'Link preserve url test',
'tipo' => 'link',
'url' => 'https://example.com/preserve.pdf',
'tipologia' => 'documento',
'visibilita' => 'pubblico',
'user_id' => $this->admin->id,
]);
$response = $this->put("/documenti/{$doc->id}", [
'nome_file' => 'Link preserve url test',
'tipologia' => 'documento',
]);
$response->assertSessionHas('success');
$doc->refresh();
$this->assertEquals('https://example.com/preserve.pdf', $doc->url);
}
public function test_update_link_validates_url_format(): void
{
$this->actingAs($this->admin);
$doc = Documento::create([
'nome_file' => 'Link validate url',
'tipo' => 'link',
'url' => 'https://example.com/valid.pdf',
'tipologia' => 'documento',
'visibilita' => 'pubblico',
'user_id' => $this->admin->id,
]);
$response = $this->put("/documenti/{$doc->id}", [
'nome_file' => 'Link validate url',
'tipologia' => 'documento',
'url' => 'not-valid-url',
]);
$response->assertSessionHasErrors('url');
}
public function test_destroy_link_does_not_call_storage(): void
{
$this->actingAs($this->admin);
$doc = Documento::create([
'nome_file' => 'Link destroy test',
'tipo' => 'link',
'url' => 'https://example.com/destroy.pdf',
'tipologia' => 'documento',
'visibilita' => 'pubblico',
'user_id' => $this->admin->id,
]);
$this->delete("/documenti/{$doc->id}");
$this->assertDatabaseMissing('documenti', ['id' => $doc->id]);
}
public function test_mass_download_skips_links(): void
{
$this->actingAs($this->admin);
$linkDoc = Documento::create([
'nome_file' => 'Link in mass download',
'tipo' => 'link',
'url' => 'https://example.com/skipped.pdf',
'tipologia' => 'documento',
'visibilita' => 'pubblico',
'user_id' => $this->admin->id,
]);
$response = $this->post('/documenti/mass-download', [
'ids' => [(string) $linkDoc->id],
]);
$response->assertSessionHas('error');
$response->assertSessionHas('error', fn(string $msg) => str_contains($msg, 'Nessun file'));
}
}