diff --git a/app/Http/Controllers/Admin/AuditionController.php b/app/Http/Controllers/Admin/AuditionController.php index 0dd0625..2f43b95 100644 --- a/app/Http/Controllers/Admin/AuditionController.php +++ b/app/Http/Controllers/Admin/AuditionController.php @@ -160,16 +160,12 @@ class AuditionController extends Controller public function destroy(Audition $audition) { - if (! Auth::user()->is_admin) { - abort(403); - } - // if($audition->entries->count() > 0) abort(403, 'Cannot delete an audition with entries.' if ($audition->entries->count() > 0) { return redirect()->route('admin.auditions.index')->with('error', 'Cannot delete an audition with entries.'); } $audition->delete(); - return redirect('/admin/auditions'); + return to_route('admin.auditions.index')->with('success', 'Audition deleted successfully'); } public function prepareDraw() diff --git a/app/Services/AuditionService.php b/app/Services/AuditionService.php index c45e064..6e2d9a9 100644 --- a/app/Services/AuditionService.php +++ b/app/Services/AuditionService.php @@ -69,9 +69,6 @@ class AuditionService public function clearCache(): void { - if (App::environment('local')) { - Session::flash('success', 'Audition Cache Cleared'); - } Cache::forget($this->cacheKey); } diff --git a/resources/views/admin/auditions/edit.blade.php b/resources/views/admin/auditions/edit.blade.php index 0f362ea..dc2be46 100644 --- a/resources/views/admin/auditions/edit.blade.php +++ b/resources/views/admin/auditions/edit.blade.php @@ -3,6 +3,13 @@ Edit Audition + + @if($audition->entries->count() == 0) + + Please confirm that you would like to delete the audition {{ $audition->name }}. This action cannot be undone. + + @endif + {{-- TODO implement a way to update multiple auditions as once --}} @@ -39,18 +46,13 @@ -
- @if($audition->entries->count() == 0) - - @endif -
+
Edit Audition
-
diff --git a/resources/views/components/delete-resource-modal.blade.php b/resources/views/components/delete-resource-modal.blade.php index b147988..6c444ea 100644 --- a/resources/views/components/delete-resource-modal.blade.php +++ b/resources/views/components/delete-resource-modal.blade.php @@ -1,3 +1,11 @@ +@php +/** + * @var int $size=20 Size of the icon + * @var string $title Title of the modal + * @var string $method='DELETE' method used by the form + * @var string $action action used for the form + */ +@endphp @props(['size' => 20,'title','method'=>'DELETE','action'])
audition = Audition::factory()->seatingOnly()->create(); + $this->newEvent = Event::factory()->create(); + $this->changes = [ + 'event_id' => $this->newEvent->id, + 'name' => 'New Name', + 'entry_deadline' => '1978-01-01', + 'entry_fee' => 10000, + 'minimum_grade' => 3, + 'maximum_grade' => 8, + 'for_advancement' => 'on', + ]; +}); + +it('allows only an admin to manage auditions', function () { + get(route('admin.auditions.edit', $this->audition)) + ->assertRedirect(route('home')); + actAsNormal(); + get(route('admin.auditions.edit', $this->audition)) + ->assertRedirect('/dashboard') + ->assertSessionHas('error', 'You are not authorized to perform this action'); + actasAdmin(); + get(route('admin.auditions.edit', $this->audition)) + ->assertOk(); +}); +it('shows necessary fields', function () { + // Arrange + actAsAdmin(); + // Act & Assert + get(route('admin.auditions.edit', $this->audition)) + ->assertOk() + ->assertSee(route('admin.auditions.store')) + ->assertSee('name="event_id"', false) + ->assertSee('name="name"', false) + ->assertSee('name="entry_deadline"', false) + ->assertSee('name="entry_fee"', false) + ->assertSee('name="minimum_grade"', false) + ->assertSee('name="maximum_grade"', false) + ->assertSee('name="for_seating"', false) + ->assertSee('name="for_advancement"', false); +}); +it('allows an administrator to modify auditions', function () { + actAsAdmin(); + // Act + $response = patch(route('admin.auditions.update', $this->audition), $this->changes); + // Assert + /** @noinspection PhpUnhandledExceptionInspection */ + $response->assertRedirect(route('admin.auditions.index')) + ->assertSessionHasNoErrors() + ->assertSessionHas('success', 'Audition updated successfully'); + $checkAudition = Audition::find($this->audition->id); + expect($checkAudition->event_id)->toBe($this->newEvent->id) + ->and($checkAudition->name)->toBe($this->changes['name']) + ->and($checkAudition->entry_deadline)->toBe($this->changes['entry_deadline']) + ->and($checkAudition->entry_fee)->toBe($this->changes['entry_fee'] * 100) + ->and($checkAudition->minimum_grade)->toBe($this->changes['minimum_grade']) + ->and($checkAudition->maximum_grade)->toBe($this->changes['maximum_grade']) + ->and($checkAudition->for_seating)->toBe(0) + ->and($checkAudition->for_advancement)->toBe(1); +}); +it('does not allow a normal user or guest to create an audition', function () { + $preCheck = Audition::find($this->audition->id); + // Act & Assert + patch(route('admin.auditions.update', $this->audition), $this->changes) + ->assertRedirect(route('home')); + actAsNormal(); + patch(route('admin.auditions.update', $this->audition), $this->changes) + ->assertRedirect('/dashboard') + ->assertSessionHas('error', 'You are not authorized to perform this action'); + $checkAudition = Audition::find($this->audition->id); + expect($checkAudition)->toEqual($preCheck); +}); +it('has a delete function for an audition that has no entries', function () { + // Arrange + actAsAdmin(); + // Act & Assert + get(route('admin.auditions.edit', $this->audition)) + ->assertOk() + ->assertSee(route('admin.auditions.destroy', $this->audition)); +}); +it('does not allow guests or normal users to delete an audition', function () { + delete(route('admin.auditions.destroy', $this->audition)) + ->assertRedirect(route('home')); + actAsNormal(); + delete(route('admin.auditions.destroy', $this->audition)) + ->assertRedirect('/dashboard') + ->assertSessionHas('error', 'You are not authorized to perform this action'); +}); +it('does not allow the deletion of an audition with entries', function () { + // Arrange + actAsAdmin(); + Entry::factory()->create(['audition_id' => $this->audition->id]); + // Act & Assert + delete(route('admin.auditions.destroy', $this->audition)) + ->assertRedirect(route('admin.auditions.index')) + ->assertSessionHas('error', 'Cannot delete an audition with entries.'); +}); +it('allows an administrator to delete an audition that has no entries', function () { + // Arrange + actAsAdmin(); + // Act & Assert + delete(route('admin.auditions.destroy', $this->audition)) + ->assertRedirect(route('admin.auditions.index')) + ->assertSessionHas('success', 'Audition deleted successfully'); + expect(Audition::find($this->audition->id))->toBeNull(); +});