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')); } }