feature test su institution
This commit is contained in:
231
backend/tests/Feature/InstitutionControllerTest.php
Normal file
231
backend/tests/Feature/InstitutionControllerTest.php
Normal file
@@ -0,0 +1,231 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\Institution;
|
||||
use App\Models\InstitutionLink;
|
||||
use App\Models\Lists\InstitutionCategory;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Tests\TestCase;
|
||||
|
||||
class InstitutionControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $overrides
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function validPayload(array $overrides = []): array
|
||||
{
|
||||
return array_merge([
|
||||
'category_id' => InstitutionCategory::factory()->create()->id,
|
||||
'name' => 'Test Museum',
|
||||
'abbreviation' => 'TM',
|
||||
'address' => 'Some Road 1',
|
||||
'city' => 'Lund',
|
||||
'lat' => 55.7,
|
||||
'lon' => 13.19,
|
||||
'color' => '#ffffff',
|
||||
'is_storage_place' => true,
|
||||
'logo' => UploadedFile::fake()->image('logo.png'),
|
||||
], $overrides);
|
||||
}
|
||||
|
||||
// --- Lettura (tier.app) --------------------------------------------------
|
||||
|
||||
public function test_index_lists_institutions_for_an_operational_user(): void
|
||||
{
|
||||
$this->actingAs($this->operationalUser(), 'sanctum');
|
||||
Institution::factory()->count(2)->create();
|
||||
|
||||
$this->getJson('/api/institutions')
|
||||
->assertOk()
|
||||
->assertJsonStructure(['message', 'data', 'meta' => ['current_page', 'total']])
|
||||
->assertJsonCount(2, 'data');
|
||||
}
|
||||
|
||||
public function test_index_filters_by_category_and_search(): void
|
||||
{
|
||||
$this->actingAs($this->operationalUser(), 'sanctum');
|
||||
$category = InstitutionCategory::factory()->create();
|
||||
Institution::factory()->create(['category_id' => $category->id, 'name' => 'Blekinge Museum']);
|
||||
Institution::factory()->create(['name' => 'Other Place']);
|
||||
|
||||
$this->getJson("/api/institutions?category_id={$category->id}")
|
||||
->assertOk()
|
||||
->assertJsonCount(1, 'data')
|
||||
->assertJsonPath('data.0.name', 'Blekinge Museum');
|
||||
|
||||
$this->getJson('/api/institutions?search=Blekinge')
|
||||
->assertOk()
|
||||
->assertJsonCount(1, 'data');
|
||||
}
|
||||
|
||||
public function test_index_can_list_only_trashed(): void
|
||||
{
|
||||
$this->actingAs($this->operationalUser(), 'sanctum');
|
||||
$trashed = Institution::factory()->create();
|
||||
Institution::factory()->create();
|
||||
$trashed->delete();
|
||||
|
||||
$this->getJson('/api/institutions')->assertJsonCount(1, 'data');
|
||||
$this->getJson('/api/institutions?trashed=only')
|
||||
->assertOk()
|
||||
->assertJsonCount(1, 'data')
|
||||
->assertJsonPath('data.0.id', $trashed->id);
|
||||
$this->getJson('/api/institutions?trashed=with')->assertJsonCount(2, 'data');
|
||||
}
|
||||
|
||||
public function test_show_returns_institution_with_category_and_links(): void
|
||||
{
|
||||
$this->actingAs($this->operationalUser(), 'sanctum');
|
||||
$institution = Institution::factory()->create();
|
||||
InstitutionLink::factory()->create(['institution_id' => $institution->id]);
|
||||
|
||||
$this->getJson("/api/institutions/{$institution->uuid}")
|
||||
->assertOk()
|
||||
->assertJsonPath('data.uuid', $institution->uuid)
|
||||
->assertJsonStructure(['data' => ['category', 'links']]);
|
||||
}
|
||||
|
||||
public function test_show_resolves_by_uuid_not_id(): void
|
||||
{
|
||||
$this->actingAs($this->operationalUser(), 'sanctum');
|
||||
$institution = Institution::factory()->create();
|
||||
|
||||
// L'id interno non è una route key valida.
|
||||
$this->getJson("/api/institutions/{$institution->id}")->assertNotFound();
|
||||
}
|
||||
|
||||
// --- Scrittura (tier.admin) ---------------------------------------------
|
||||
|
||||
public function test_admin_can_create_an_institution_with_a_logo(): void
|
||||
{
|
||||
Storage::fake('public');
|
||||
$this->actingAs($this->adminUser(), 'sanctum');
|
||||
|
||||
$this->post('/api/institutions', $this->validPayload(), ['Accept' => 'application/json'])
|
||||
->assertCreated()
|
||||
->assertJsonPath('data.name', 'Test Museum')
|
||||
->assertJsonStructure(['data' => ['uuid', 'category']]);
|
||||
|
||||
$institution = Institution::firstWhere('name', 'Test Museum');
|
||||
$this->assertNotNull($institution);
|
||||
$this->assertNotEmpty($institution->uuid);
|
||||
Storage::disk('public')->assertExists($institution->logo);
|
||||
}
|
||||
|
||||
public function test_create_applies_default_color_and_storage_flag(): void
|
||||
{
|
||||
Storage::fake('public');
|
||||
$this->actingAs($this->adminUser(), 'sanctum');
|
||||
|
||||
$payload = $this->validPayload();
|
||||
unset($payload['color'], $payload['is_storage_place']);
|
||||
|
||||
$this->post('/api/institutions', $payload, ['Accept' => 'application/json'])->assertCreated();
|
||||
|
||||
$this->assertDatabaseHas('institutions', [
|
||||
'name' => 'Test Museum',
|
||||
'color' => '#c5cae9',
|
||||
'is_storage_place' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_create_validates_required_fields(): void
|
||||
{
|
||||
$this->actingAs($this->adminUser(), 'sanctum');
|
||||
|
||||
$this->postJson('/api/institutions', [])
|
||||
->assertStatus(422)
|
||||
->assertJsonValidationErrors(['category_id', 'name', 'abbreviation', 'lat', 'lon', 'logo']);
|
||||
}
|
||||
|
||||
public function test_admin_can_update_an_institution_keeping_the_logo(): void
|
||||
{
|
||||
$this->actingAs($this->adminUser(), 'sanctum');
|
||||
$institution = Institution::factory()->create(['logo' => 'institution_logo/keep.jpg', 'name' => 'Old']);
|
||||
|
||||
$payload = $this->validPayload(['name' => 'New Name', 'category_id' => $institution->category_id]);
|
||||
unset($payload['logo']);
|
||||
|
||||
$this->put("/api/institutions/{$institution->uuid}", $payload, ['Accept' => 'application/json'])
|
||||
->assertOk()
|
||||
->assertJsonPath('data.name', 'New Name');
|
||||
|
||||
$this->assertSame('institution_logo/keep.jpg', $institution->fresh()->logo);
|
||||
}
|
||||
|
||||
public function test_update_replaces_the_logo_when_a_new_one_is_uploaded(): void
|
||||
{
|
||||
Storage::fake('public');
|
||||
$this->actingAs($this->adminUser(), 'sanctum');
|
||||
Storage::disk('public')->put('institution_logo/old.jpg', 'x');
|
||||
$institution = Institution::factory()->create(['logo' => 'institution_logo/old.jpg']);
|
||||
|
||||
$payload = $this->validPayload(['category_id' => $institution->category_id]);
|
||||
|
||||
$this->put("/api/institutions/{$institution->uuid}", $payload, ['Accept' => 'application/json'])->assertOk();
|
||||
|
||||
Storage::disk('public')->assertMissing('institution_logo/old.jpg');
|
||||
Storage::disk('public')->assertExists($institution->fresh()->logo);
|
||||
}
|
||||
|
||||
public function test_admin_can_soft_delete_an_institution(): void
|
||||
{
|
||||
$this->actingAs($this->adminUser(), 'sanctum');
|
||||
$institution = Institution::factory()->create();
|
||||
|
||||
$this->deleteJson("/api/institutions/{$institution->uuid}")->assertOk();
|
||||
|
||||
$this->assertSoftDeleted($institution);
|
||||
}
|
||||
|
||||
public function test_admin_can_restore_a_trashed_institution(): void
|
||||
{
|
||||
$this->actingAs($this->adminUser(), 'sanctum');
|
||||
$institution = Institution::factory()->create();
|
||||
$institution->delete();
|
||||
|
||||
$this->postJson("/api/institutions/{$institution->uuid}/restore")
|
||||
->assertOk()
|
||||
->assertJsonPath('data.uuid', $institution->uuid);
|
||||
|
||||
$this->assertNotSoftDeleted($institution);
|
||||
}
|
||||
|
||||
public function test_admin_can_force_delete_an_institution_and_its_logo_and_links(): void
|
||||
{
|
||||
Storage::fake('public');
|
||||
$this->actingAs($this->adminUser(), 'sanctum');
|
||||
Storage::disk('public')->put('institution_logo/gone.jpg', 'x');
|
||||
$institution = Institution::factory()->create(['logo' => 'institution_logo/gone.jpg']);
|
||||
$link = InstitutionLink::factory()->create(['institution_id' => $institution->id]);
|
||||
$institution->delete();
|
||||
|
||||
$this->deleteJson("/api/institutions/{$institution->uuid}/force")->assertOk();
|
||||
|
||||
$this->assertDatabaseMissing('institutions', ['id' => $institution->id]);
|
||||
$this->assertDatabaseMissing('institution_links', ['id' => $link->id]);
|
||||
Storage::disk('public')->assertMissing('institution_logo/gone.jpg');
|
||||
}
|
||||
|
||||
// --- Autorizzazione ------------------------------------------------------
|
||||
|
||||
public function test_a_non_admin_cannot_write(): void
|
||||
{
|
||||
$this->actingAs($this->operationalUser(), 'sanctum');
|
||||
$institution = Institution::factory()->create();
|
||||
|
||||
$this->postJson('/api/institutions', [])->assertForbidden();
|
||||
$this->deleteJson("/api/institutions/{$institution->uuid}")->assertForbidden();
|
||||
}
|
||||
|
||||
public function test_guests_cannot_access_institutions(): void
|
||||
{
|
||||
$this->getJson('/api/institutions')->assertUnauthorized();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user