Tets for Admin EventController

This commit is contained in:
Matt Young 2025-07-08 10:48:52 -05:00
parent 47200b1f01
commit 66fe859f06
2 changed files with 111 additions and 5 deletions

View File

@ -5,9 +5,7 @@ namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Models\Event; use App\Models\Event;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use function abort;
use function compact; use function compact;
class EventController extends Controller class EventController extends Controller
@ -21,9 +19,6 @@ class EventController extends Controller
public function store(Request $request) public function store(Request $request)
{ {
if (! Auth::user()->is_admin) {
abort(403);
}
request()->validate([ request()->validate([
'name' => ['required', 'unique:events,name'], 'name' => ['required', 'unique:events,name'],
]); ]);
@ -46,3 +41,4 @@ class EventController extends Controller
return redirect()->route('admin.events.index')->with('success', 'Event deleted successfully'); return redirect()->route('admin.events.index')->with('success', 'Event deleted successfully');
} }
} }
// TODO add form to modify an event

View File

@ -0,0 +1,110 @@
<?php
use App\Models\Audition;
use App\Models\Event;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
describe('EventController::Index', function () {
it('denies access to a non-admin user', function () {
$this->get(route('admin.events.index'))->assertRedirect(route('home'));
actAsNormal();
$this->get(route('admin.events.index'))->assertRedirect(route('dashboard'));
actAsTab();
$this->get(route('admin.events.index'))->assertRedirect(route('dashboard'));
});
it('shows the event index page', function () {
$events = Event::factory()->count(3)->create();
actAsAdmin();
$response = $this->get(route('admin.events.index'))->assertOk();
foreach ($events as $event) {
$response->assertSee($event->name);
}
});
it('includes the count of auditions in each event', function () {
$event1 = Event::factory()->create();
$event2 = Event::factory()->create();
Audition::factory()->count(3)->create([
'event_id' => $event1->id,
]);
Audition::factory()->count(2)->create([
'event_id' => $event2->id,
]);
actAsAdmin();
$response = $this->get(route('admin.events.index'))
->assertOk();
$response->assertSeeInOrder([
$event1->name,
'3 Auditions',
]);
$response->assertSeeInOrder([
$event1->name,
'2 Auditions',
]);
});
it('includes a form to add an event', function () {
$events = \App\Models\Event::factory()->count(3)->create();
actAsAdmin();
$response = $this->get(route('admin.events.index'))
->assertOk()
->assertSee(route('admin.events.store'));
});
});
describe('EventController::Store', function () {
it('denies access to a non-admin user', function () {
$this->post(route('admin.events.store'), [])->assertRedirect(route('home'));
actAsNormal();
$this->post(route('admin.events.store'), [])->assertRedirect(route('dashboard'));
actAsTab();
$this->post(route('admin.events.store'), [])->assertRedirect(route('dashboard'));
});
it('creates an event', function () {
actAsAdmin();
$this->post(route('admin.events.store'), [
'name' => 'Test Event',
])->assertRedirect(route('admin.events.index'))->assertSessionHas('success');
$this->assertDatabaseHas('events', [
'name' => 'Test Event',
]);
});
it('will not create an event with a duplicate name', function () {
\App\Models\Event::create(['name' => 'Test Event']);
actAsAdmin();
$this->post(route('admin.events.store'), [
'name' => 'Test Event',
])->assertRedirect()->assertSessionHasErrors('name');
$this->assertDatabaseCount('events', 1);
});
});
describe('EventController::Destroy', function () {
it('denies access to a non-admin user', function () {
$event = \App\Models\Event::factory()->create();
$this->delete(route('admin.events.destroy', $event))->assertRedirect(route('home'));
actAsNormal();
$this->delete(route('admin.events.destroy', $event))->assertRedirect(route('dashboard'));
actAsTab();
$this->delete(route('admin.events.destroy', $event))->assertRedirect(route('dashboard'));
});
it('deletes an event', function () {
$event = \App\Models\Event::factory()->create();
actAsAdmin();
$this->delete(route('admin.events.destroy',
$event))->assertRedirect(route('admin.events.index'))->assertSessionHas('success');
$this->assertDatabaseMissing('events', [
'id' => $event->id,
]);
});
it('will not delete an event with auditions', function () {
$event = \App\Models\Event::factory()->create();
\App\Models\Audition::factory()->count(3)->create([
'event_id' => $event->id,
]);
actAsAdmin();
$this->delete(route('admin.events.destroy', $event))
->assertRedirect(route('admin.events.index'));
$this->assertDatabaseCount('events', 1);
});
});