Files
crewli/api/tests/Feature/TimeSlot/TimeSlotTest.php
bert.hausmans 7bc0f1a0c7 feat: fix time slot hierarchy — seeder, API include_children, frontend dropdown, navigation
Restructure the festival hierarchy end-to-end:

Seeder: Remove duplicate festival-level VOLUNTEER time slots, keep only CREW
operational slots. Rename sub-events to "Dag 1/2/3 — ..." pattern. Change
Nachtsecurity to Security (cross_event). EHBO/Security shifts now use sub-event
time slots via cross_event exception. Add flat event "Braderie Dorpstown 2026".

API: Add ?include_children=true to TimeSlotController for festivals, returning
all sub-event time slots with source and event_name fields. Update
StoreShiftRequest and UpdateShiftRequest to accept child time slots for
cross_event sections.

Frontend: Create useTimeSlotDropdown composable with 4-scenario dropdown logic.
Replace AppSelect with VAutocomplete in CreateShiftDialog with grouped items,
dimmed festival slots, and info tooltips. Add InfoTooltip reusable component.
Show festival context labels on cross_event sections in sub-event section lists.
Add read-only festival time slots on sub-event time-slots page. Add cross_event
context banner with "Bekijk alle diensten" link.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-14 22:07:37 +02:00

342 lines
12 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Feature\TimeSlot;
use App\Models\Event;
use App\Models\FestivalSection;
use App\Models\Organisation;
use App\Models\Person;
use App\Models\Shift;
use App\Models\ShiftAssignment;
use App\Models\TimeSlot;
use App\Models\User;
use Database\Seeders\RoleSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class TimeSlotTest extends TestCase
{
use RefreshDatabase;
private User $orgAdmin;
private User $outsider;
private Organisation $organisation;
private Organisation $otherOrganisation;
private Event $event;
protected function setUp(): void
{
parent::setUp();
$this->seed(RoleSeeder::class);
$this->organisation = Organisation::factory()->create();
$this->otherOrganisation = Organisation::factory()->create();
$this->orgAdmin = User::factory()->create();
$this->organisation->users()->attach($this->orgAdmin, ['role' => 'org_admin']);
$this->outsider = User::factory()->create();
$this->otherOrganisation->users()->attach($this->outsider, ['role' => 'org_admin']);
$this->event = Event::factory()->create(['organisation_id' => $this->organisation->id]);
}
public function test_index_returns_time_slots(): void
{
TimeSlot::factory()->count(3)->create(['event_id' => $this->event->id]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->getJson("/api/v1/organisations/{$this->organisation->id}/events/{$this->event->id}/time-slots");
$response->assertOk();
$this->assertCount(3, $response->json('data'));
}
public function test_index_includes_shift_statistics(): void
{
$timeSlot = TimeSlot::factory()->create(['event_id' => $this->event->id]);
$sectionA = FestivalSection::factory()->create(['event_id' => $this->event->id]);
$sectionB = FestivalSection::factory()->create(['event_id' => $this->event->id]);
$person = Person::factory()->create(['event_id' => $this->event->id]);
$shift1 = Shift::factory()->create([
'festival_section_id' => $sectionA->id,
'time_slot_id' => $timeSlot->id,
'slots_total' => 5,
]);
$shift2 = Shift::factory()->create([
'festival_section_id' => $sectionB->id,
'time_slot_id' => $timeSlot->id,
'slots_total' => 3,
]);
// Create 2 approved assignments on shift1
ShiftAssignment::factory()->approved()->create([
'shift_id' => $shift1->id,
'person_id' => $person->id,
'time_slot_id' => $timeSlot->id,
]);
ShiftAssignment::factory()->approved()->create([
'shift_id' => $shift1->id,
'person_id' => Person::factory()->create(['event_id' => $this->event->id])->id,
'time_slot_id' => $timeSlot->id,
]);
// Create 1 pending assignment on shift2 (should NOT count towards filled_slots)
ShiftAssignment::factory()->create([
'shift_id' => $shift2->id,
'person_id' => $person->id,
'time_slot_id' => $timeSlot->id,
'status' => 'pending_approval',
]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->getJson("/api/v1/organisations/{$this->organisation->id}/events/{$this->event->id}/time-slots");
$response->assertOk();
$data = $response->json('data.0');
$this->assertEquals(2, $data['shifts_count']);
$this->assertEquals(8, $data['total_slots']);
$this->assertEquals(2, $data['filled_slots']);
$this->assertEquals(2, $data['sections_count']);
}
public function test_store_creates_time_slot(): void
{
Sanctum::actingAs($this->orgAdmin);
$response = $this->postJson("/api/v1/organisations/{$this->organisation->id}/events/{$this->event->id}/time-slots", [
'name' => 'Vrijdag Avond',
'person_type' => 'VOLUNTEER',
'date' => '2026-07-10',
'start_time' => '18:00',
'end_time' => '02:00',
'duration_hours' => 8,
]);
$response->assertCreated()
->assertJson(['data' => [
'name' => 'Vrijdag Avond',
'person_type' => 'VOLUNTEER',
]]);
$this->assertDatabaseHas('time_slots', [
'event_id' => $this->event->id,
'name' => 'Vrijdag Avond',
]);
}
public function test_store_invalid_person_type_returns_422(): void
{
Sanctum::actingAs($this->orgAdmin);
$response = $this->postJson("/api/v1/organisations/{$this->organisation->id}/events/{$this->event->id}/time-slots", [
'name' => 'Test',
'person_type' => 'INVALID',
'date' => '2026-07-10',
'start_time' => '18:00',
'end_time' => '02:00',
]);
$response->assertUnprocessable()
->assertJsonValidationErrors('person_type');
}
public function test_store_invalid_date_format_returns_422(): void
{
Sanctum::actingAs($this->orgAdmin);
$response = $this->postJson("/api/v1/organisations/{$this->organisation->id}/events/{$this->event->id}/time-slots", [
'name' => 'Test',
'person_type' => 'VOLUNTEER',
'date' => 'not-a-date',
'start_time' => '18:00',
'end_time' => '02:00',
]);
$response->assertUnprocessable()
->assertJsonValidationErrors('date');
}
public function test_update_time_slot(): void
{
$timeSlot = TimeSlot::factory()->create([
'event_id' => $this->event->id,
'name' => 'Vrijdag Avond',
]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->putJson("/api/v1/organisations/{$this->organisation->id}/events/{$this->event->id}/time-slots/{$timeSlot->id}", [
'name' => 'Vrijdag Avond Updated',
]);
$response->assertOk()
->assertJson(['data' => ['name' => 'Vrijdag Avond Updated']]);
}
public function test_update_cross_org_returns_403(): void
{
$timeSlot = TimeSlot::factory()->create(['event_id' => $this->event->id]);
Sanctum::actingAs($this->outsider);
$response = $this->putJson("/api/v1/organisations/{$this->organisation->id}/events/{$this->event->id}/time-slots/{$timeSlot->id}", [
'name' => 'Hacked',
]);
$response->assertForbidden();
}
public function test_destroy_cross_org_returns_403(): void
{
$timeSlot = TimeSlot::factory()->create(['event_id' => $this->event->id]);
Sanctum::actingAs($this->outsider);
$response = $this->deleteJson("/api/v1/organisations/{$this->organisation->id}/events/{$this->event->id}/time-slots/{$timeSlot->id}");
$response->assertForbidden();
}
public function test_destroy_deletes_time_slot(): void
{
$timeSlot = TimeSlot::factory()->create(['event_id' => $this->event->id]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->deleteJson("/api/v1/organisations/{$this->organisation->id}/events/{$this->event->id}/time-slots/{$timeSlot->id}");
$response->assertNoContent();
$this->assertDatabaseMissing('time_slots', ['id' => $timeSlot->id]);
}
public function test_unauthenticated_returns_401(): void
{
$response = $this->getJson("/api/v1/organisations/{$this->organisation->id}/events/{$this->event->id}/time-slots");
$response->assertUnauthorized();
}
public function test_cross_org_returns_403(): void
{
Sanctum::actingAs($this->outsider);
$response = $this->getJson("/api/v1/organisations/{$this->organisation->id}/events/{$this->event->id}/time-slots");
$response->assertForbidden();
}
public function test_include_children_returns_sub_event_time_slots(): void
{
$festival = Event::factory()->create([
'organisation_id' => $this->organisation->id,
'event_type' => 'festival',
]);
$subEvent1 = Event::factory()->create([
'organisation_id' => $this->organisation->id,
'parent_event_id' => $festival->id,
]);
$subEvent2 = Event::factory()->create([
'organisation_id' => $this->organisation->id,
'parent_event_id' => $festival->id,
]);
// 2 festival time slots
TimeSlot::factory()->count(2)->create(['event_id' => $festival->id]);
// 3 sub-event 1 time slots
TimeSlot::factory()->count(3)->create(['event_id' => $subEvent1->id]);
// 1 sub-event 2 time slot
TimeSlot::factory()->create(['event_id' => $subEvent2->id]);
Sanctum::actingAs($this->orgAdmin);
// Without include_children: only festival's own
$response = $this->getJson("/api/v1/organisations/{$this->organisation->id}/events/{$festival->id}/time-slots");
$response->assertOk();
$this->assertCount(2, $response->json('data'));
// With include_children: festival + all sub-events
$response = $this->getJson("/api/v1/organisations/{$this->organisation->id}/events/{$festival->id}/time-slots?include_children=true");
$response->assertOk();
$this->assertCount(6, $response->json('data'));
}
public function test_include_children_marks_source_and_event_name(): void
{
$festival = Event::factory()->create([
'organisation_id' => $this->organisation->id,
'event_type' => 'festival',
'name' => 'Test Festival',
]);
$subEvent = Event::factory()->create([
'organisation_id' => $this->organisation->id,
'parent_event_id' => $festival->id,
'name' => 'Day 1',
]);
TimeSlot::factory()->create(['event_id' => $festival->id, 'name' => 'Festival Slot']);
TimeSlot::factory()->create(['event_id' => $subEvent->id, 'name' => 'Sub Slot']);
Sanctum::actingAs($this->orgAdmin);
$response = $this->getJson("/api/v1/organisations/{$this->organisation->id}/events/{$festival->id}/time-slots?include_children=true");
$response->assertOk();
$data = $response->json('data');
$festivalSlot = collect($data)->firstWhere('name', 'Festival Slot');
$subSlot = collect($data)->firstWhere('name', 'Sub Slot');
$this->assertEquals('own', $festivalSlot['source']);
$this->assertEquals('Test Festival', $festivalSlot['event_name']);
$this->assertEquals($subEvent->id, $subSlot['source']);
$this->assertEquals('Day 1', $subSlot['event_name']);
}
public function test_include_children_ignored_for_sub_events(): void
{
$festival = Event::factory()->create([
'organisation_id' => $this->organisation->id,
'event_type' => 'festival',
]);
$subEvent = Event::factory()->create([
'organisation_id' => $this->organisation->id,
'parent_event_id' => $festival->id,
]);
TimeSlot::factory()->count(2)->create(['event_id' => $festival->id]);
TimeSlot::factory()->count(3)->create(['event_id' => $subEvent->id]);
Sanctum::actingAs($this->orgAdmin);
// include_children on a sub-event should have no effect
$response = $this->getJson("/api/v1/organisations/{$this->organisation->id}/events/{$subEvent->id}/time-slots?include_children=true");
$response->assertOk();
$this->assertCount(3, $response->json('data'));
}
public function test_include_children_ignored_for_flat_events(): void
{
TimeSlot::factory()->count(3)->create(['event_id' => $this->event->id]);
Sanctum::actingAs($this->orgAdmin);
// include_children on a flat event should have no effect
$response = $this->getJson("/api/v1/organisations/{$this->organisation->id}/events/{$this->event->id}/time-slots?include_children=true");
$response->assertOk();
$this->assertCount(3, $response->json('data'));
}
}