auditionadmin/tests-old/Feature/Pages/Setup/ScoringGuideEditDetailsTest...

156 lines
5.8 KiB
PHP

<?php
use App\Models\ScoringGuide;
use App\Models\SubscoreDefinition;
use App\Settings;
use Illuminate\Foundation\Testing\RefreshDatabase;
use function Pest\Laravel\get;
use function Pest\Laravel\post;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->scoringGuide = ScoringGuide::factory()->create();
$this->subscores = SubscoreDefinition::factory()->count(3)->create([
'scoring_guide_id' => $this->scoringGuide->id,
]);
});
it('shows the details of a scoring guide', function () {
actAsAdmin();
$response = get(route('admin.scoring.edit', $this->scoringGuide));
$response->assertOk();
$response->assertSee($this->scoringGuide->name);
foreach ($this->subscores as $subscore) {
$response->assertSeeInOrder([
'<tr',
'<td', $subscore->name, '</td>',
'<td', $subscore->max_score, '</td>',
'<td', $subscore->weight, '</td>',
'<td', ($subscore->for_seating) ? 'Yes' : 'No', '</td>',
'<td', ($subscore->for_advance) ? 'Yes' : 'No', '</td>',
], false);
}
Settings::set('advanceTo', '');
$response = get(route('admin.scoring.edit', $this->scoringGuide));
$response->assertOk();
$response->assertSee($this->scoringGuide->name);
foreach ($this->subscores as $subscore) {
$response->assertSeeInOrder([
'<tr',
'<td', $subscore->name, '</td>',
'<td', $subscore->max_score, '</td>',
'<td', $subscore->weight, '</td>',
], false);
$response->assertDontSee('Yes</td>', false);
$response->assertDontSee('No</td>', false);
}
});
it('includes a form to add a subscore', function () {
// Arrange
actAsAdmin();
// Act & Assert
$response = get(route('admin.scoring.edit', $this->scoringGuide));
$response->assertOk();
$response->assertSee(route('admin.scoring.subscore_store', $this->scoringGuide));
});
it('can create a subscore', function () {
// Arrange
actAsAdmin();
$formData = [
'name' => 'New Subscore',
'max_score' => 32,
'weight' => 1,
'for_seating' => 1,
];
// Act
$response = $this->post(route('admin.scoring.subscore_store', $this->scoringGuide), $formData);
// Assert
/** @noinspection PhpUnhandledExceptionInspection */
$response->assertSessionHasNoErrors()
->assertRedirect(route('admin.scoring.edit', $this->scoringGuide));
$this->assertDatabaseHas('subscore_definitions', $formData);
});
it('sets for_seating true for new subscores when advancement is not enabled', function () {
Settings::set('advanceTo', '');
actAsAdmin();
$formData = [
'name' => 'New Subscore',
'max_score' => 32,
'weight' => 1,
];
$response = $this->post(route('admin.scoring.subscore_store', $this->scoringGuide), $formData);
$response->assertSessionHasNoErrors()
->assertRedirect(route('admin.scoring.edit', $this->scoringGuide));
$this->assertDatabaseHas('subscore_definitions', $formData);
$newSubscore = SubscoreDefinition::where('name', 'New Subscore')->first();
expect($newSubscore->for_seating)->toBeTruthy();
});
it('only allows an admin to create a subscore', function () {
// Arrange
$formData = [
'name' => 'New Subscore',
'max_score' => 32,
'weight' => 1,
'for_seating' => 1,
];
// Act & Assert
$response = post(route('admin.scoring.subscore_store', $this->scoringGuide), $formData);
$response->assertRedirect(route('home'));
actAsNormal();
$response = $this->post(route('admin.scoring.subscore_store', $this->scoringGuide), $formData);
$response->assertRedirect(route('dashboard'))
->assertSessionHas('error', 'You are not authorized to perform this action');
});
it('allows a subscore to be modified', function () {
// Arrange
$subscore = SubscoreDefinition::factory()->create([
'scoring_guide_id' => $this->scoringGuide->id,
]);
$formData = [
'name' => 'Changed Name',
'max_score' => 32,
'weight' => 1,
'for_seating' => 1,
];
// Act & Assert
actAsAdmin();
$response = $this->patch(route('admin.scoring.subscore_update', [$this->scoringGuide, $subscore]), $formData);
/** @noinspection PhpUnhandledExceptionInspection */
$response->assertSessionHasNoErrors()
->assertRedirect(route('admin.scoring.edit', $this->scoringGuide))
->assertSessionHas('success', 'Subscore updated');
$subscore = SubscoreDefinition::find($subscore->id);
expect($subscore->name)->toBe('Changed Name')
->and($subscore->max_score)->toBe(32)
->and($subscore->weight)->toBe(1)
->and($subscore->for_seating)->toBeTruthy()
->and($subscore->for_advance)->toBeFalsy();
});
it('sets for_seating true if advance is not enabled when modifying a subscore', function () {
// Arrange
Settings::set('advanceTo', '');
$subscore = SubscoreDefinition::factory()->create([
'scoring_guide_id' => $this->scoringGuide->id,
]);
$formData = [
'name' => 'Changed Name',
'max_score' => 32,
'weight' => 1,
];
// Act & Assert
actAsAdmin();
$response = $this->patch(route('admin.scoring.subscore_update', [$this->scoringGuide, $subscore]), $formData);
/** @noinspection PhpUnhandledExceptionInspection */
$response->assertSessionHasNoErrors()
->assertRedirect(route('admin.scoring.edit', $this->scoringGuide))
->assertSessionHas('success', 'Subscore updated');
$subscore = SubscoreDefinition::find($subscore->id);
expect($subscore->name)->toBe('Changed Name')
->and($subscore->max_score)->toBe(32)
->and($subscore->weight)->toBe(1)
->and($subscore->for_seating)->toBeTruthy()
->and($subscore->for_advance)->toBeFalsy();
});