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([
'
name, '',
'| max_score, ' | ',
'weight, ' | ',
'for_seating) ? 'Yes' : 'No', ' | ',
'for_advance) ? 'Yes' : 'No', ' | ',
], 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([
'
name, '',
'| max_score, ' | ',
'weight, ' | ',
], false);
$response->assertDontSee('Yes', false);
$response->assertDontSee('No', 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();
});