auditionadmin/tests-old/Feature/Pages/Setup/BonusScoreIndexTest.php

152 lines
6.0 KiB
PHP

<?php
use App\Models\Audition;
use App\Models\BonusScoreDefinition;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Sinnbeck\DomAssertions\Asserts\AssertForm;
uses(RefreshDatabase::class);
it('denies access to guests and non administrators', function () {
$this->get(route('admin.bonus-scores.index'))
->assertRedirect(route('home'));
actAsNormal();
$this->get(route('admin.bonus-scores.index'))
->assertRedirect(route('dashboard'))
->assertSessionHas('error', 'You are not authorized to perform this action');
actAsTab();
$this->get(route('admin.bonus-scores.index'))
->assertRedirect(route('dashboard'))
->assertSessionHas('error', 'You are not authorized to perform this action');
});
it('grants access to an administrator', function () {
// Arrange
actAsAdmin();
// Act & Assert
$this->get(route('admin.bonus-scores.index'))
->assertOk()
->assertViewIs('admin.bonus-scores.index');
});
it('if no bonus scores exist, show a create bonus score message', function () {
// Arrange
actAsAdmin();
// Act & Assert
$this->get(route('admin.bonus-scores.index'))
->assertOk()
->assertSee('No bonus scores have been created');
});
it('includes a form to add a new bonus score', function () {
// Arrange
actAsAdmin();
// Act & Assert
$this->get(route('admin.bonus-scores.index'))
->assertOk()
->assertFormExists('#create-bonus-score-form', function (AssertForm $form) {
/** @noinspection PhpUndefinedMethodInspection */
$form->hasCSRF()
->hasMethod('POST')
->hasAction(route('admin.bonus-scores.store'))
->containsInput(['name' => 'name'])
->containsInput(['name' => 'max_score', 'type' => 'number'])
->containsInput(['name' => 'weight']);
});
});
it('can create a new bonus score', function () {
// Arrange
$submissionData = [
'name' => 'New Bonus Score',
'max_score' => 10,
'weight' => 1,
];
// Act & Assert
actAsAdmin();
$this->post(route('admin.bonus-scores.store'), $submissionData)
->assertRedirect(route('admin.bonus-scores.index'))
->assertSessionHas('success', 'Bonus Score Created');
$test = BonusScoreDefinition::where('name', 'New Bonus Score')->first();
expect($test->exists())->toBeTrue();
});
it('shows existing bonus scores', function () {
// Arrange
$bonusScores = BonusScoreDefinition::factory()->count(3)->create();
actAsAdmin();
// Act & Assert
$response = $this->get(route('admin.bonus-scores.index'));
$response->assertOk();
$bonusScores->each(fn ($bonusScore) => $response->assertSee($bonusScore->name));
});
it('can delete a bonus score with no auditions', function () {
// Arrange
$bonusScore = BonusScoreDefinition::factory()->create();
actAsAdmin();
// Act & Assert
$this->delete(route('admin.bonus-scores.destroy', $bonusScore))
->assertRedirect(route('admin.bonus-scores.index'))
->assertSessionHas('success', 'Bonus Score Deleted');
expect(BonusScoreDefinition::count())->toBe(0);
});
it('will not delete a bonus score that has auditions attached', function () {
// Arrange
$bonusScore = BonusScoreDefinition::factory()->hasAuditions(1)->create();
actAsAdmin();
// Act & Assert
$this->delete(route('admin.bonus-scores.destroy', $bonusScore))
->assertRedirect(route('admin.bonus-scores.index'))
->assertSessionHas('error', 'Bonus Score has auditions attached');
expect(BonusScoreDefinition::count())->toBe(1);
});
it('can assign auditions to a bonus score', function () {
// Arrange
$bonusScore = BonusScoreDefinition::factory()->create();
$auditions = Audition::factory()->count(3)->create();
$submissionData = [
'bonus_score_id' => $bonusScore->id,
];
foreach ($auditions as $audition) {
$submissionData['audition'][$audition->id] = 'on';
}
// Act & Assert
actAsAdmin();
$this->post(route('admin.bonus-scores.addAuditions'), $submissionData)
->assertRedirect(route('admin.bonus-scores.index'))
->assertSessionHas('success', 'Auditions assigned to bonus score');
$bonusScore->refresh();
$auditions->each(fn ($audition) => expect($bonusScore->auditions->contains($audition))->toBeTrue());
});
it('can unassign auditions from a bonus score', function () {
// Arrange
$bonusScore = BonusScoreDefinition::factory()->hasAuditions(3)->create();
$audition = $bonusScore->auditions->first();
// Act & Assert
actAsAdmin();
$this->delete(route('admin.bonus-scores.unassignAudition', $audition))
->assertRedirect(route('admin.bonus-scores.index'))
->assertSessionHas('success', 'Audition unassigned from bonus score');
$bonusScore->refresh();
expect($bonusScore->auditions->contains($audition))->toBeFalse();
});
it('sends a message when attempting to unassign an audition that is not assigned', function () {
$bonusScore = BonusScoreDefinition::factory()->create();
$audition = Audition::factory()->create();
actAsAdmin();
$this->delete(route('admin.bonus-scores.unassignAudition', $audition))
->assertRedirect(route('admin.bonus-scores.index'))
->assertSessionHas('error', 'Audition does not have a bonus score');
});
it('will not allow an audition to be assigned to multiple bonus scores', function () {
$bonusScore1 = BonusScoreDefinition::factory()->create();
$bonusScore2 = BonusScoreDefinition::factory()->create();
$audition = Audition::factory()->create();
$bonusScore1->auditions()->attach($audition);
$submissionData = [
'bonus_score_id' => $bonusScore2->id,
'audition' => [$audition->id => 'on'],
];
actAsAdmin();
$this->post(route('admin.bonus-scores.addAuditions'), $submissionData)
->assertRedirect(route('admin.bonus-scores.index'))
->assertSessionHas('error', 'Error assigning auditions to bonus score');
});