119 lines
4.8 KiB
PHP
119 lines
4.8 KiB
PHP
<?php
|
|
|
|
use App\Models\Audition;
|
|
use App\Models\Entry;
|
|
use App\Models\Event;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
|
|
use function Pest\Laravel\delete;
|
|
use function Pest\Laravel\get;
|
|
use function Pest\Laravel\patch;
|
|
|
|
uses(RefreshDatabase::class);
|
|
beforeEach(function () {
|
|
$this->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();
|
|
});
|