ScoringGuideIndex Tests

This commit is contained in:
Matt Young 2024-07-05 11:38:50 -05:00
parent df4095d45a
commit df561164be
5 changed files with 94 additions and 16 deletions

View File

@ -17,13 +17,10 @@ class ScoringGuideController extends Controller
{ {
public function index() public function index()
{ {
if (! Auth::user()->is_admin) {
abort(403);
}
DB::table('auditions') DB::table('auditions')
->whereNull('scoring_guide_id') ->whereNull('scoring_guide_id')
->update(['scoring_guide_id' => 0]); ->update(['scoring_guide_id' => 0]);
$guides = ScoringGuide::with('auditions')->orderBy('name')->get(); $guides = ScoringGuide::with(['auditions'])->withCount('subscores')->orderBy('name')->get();
return view('admin.scoring.index', ['guides' => $guides]); return view('admin.scoring.index', ['guides' => $guides]);
} }
@ -42,7 +39,7 @@ class ScoringGuideController extends Controller
'name' => request('name'), 'name' => request('name'),
]); ]);
return redirect('/admin/scoring'); return redirect(route('admin.scoring.index'))->with('success', 'Scoring guide created');
} }
public function edit(Request $request, ScoringGuide $guide) public function edit(Request $request, ScoringGuide $guide)

View File

@ -1,10 +1,7 @@
<?php <?php
use App\Models\Room; use App\Models\Room;
use App\Models\ScoringGuide;
use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration return new class extends Migration
{ {
@ -16,10 +13,10 @@ return new class extends Migration
if (! Room::find(0)) { if (! Room::find(0)) {
$room = Room::create([ $room = Room::create([
'id' => 0, 'id' => 0,
'name' => 'No Guide Assigned' 'name' => 'No Guide Assigned',
]); ]);
$room->update([ $room->update([
'id' => 0 'id' => 0,
]); ]);
} }
} }

View File

@ -1,6 +1,5 @@
<?php <?php
use App\Models\Room;
use App\Models\ScoringGuide; use App\Models\ScoringGuide;
use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
@ -20,10 +19,10 @@ return new class extends Migration
if (! ScoringGuide::find(0)) { if (! ScoringGuide::find(0)) {
$sg = ScoringGuide::create([ $sg = ScoringGuide::create([
'id' => 0, 'id' => 0,
'name' => 'No Guide Assigned' 'name' => 'No Guide Assigned',
]); ]);
$sg->update([ $sg->update([
'id' => 0 'id' => 0,
]); ]);
} }
} }

View File

@ -17,14 +17,14 @@
@continue @continue
@endif @endif
<tr> <tr>
<x-table.td>{{ $guide->name }} <span class="text-xs text-gray-400">{{ $guide->subscores->count() }} subscores</span></x-table.td> <x-table.td>{{ $guide->name }} <span class="text-xs text-gray-400">{{ $guide->subscores_count }} subscores</span></x-table.td>
<x-table.td class="text-right text-indigo-600"><a href="/admin/scoring/guides/{{ $guide->id }}/edit">Edit</a></x-table.td> <x-table.td class="text-right text-indigo-600"><a href="{{ route('admin.scoring.edit', $guide) }}">Edit</a></x-table.td>
</tr> </tr>
@endforeach @endforeach
</x-table.body> </x-table.body>
<tfoot> <tfoot>
<tr> <tr>
<x-form.form method="POST" action="/admin/scoring/guides" class="!px-0 !py-0"> <x-form.form method="POST" action="{{ route('admin.scoring.store') }}" class="!px-0 !py-0">
<x-table.td> <x-table.td>
<x-form.field name="name" label_text="Add New Scoring Guide" /> <x-form.field name="name" label_text="Add New Scoring Guide" />
</x-table.td> </x-table.td>

View File

@ -0,0 +1,85 @@
<?php
use App\Models\Audition;
use App\Models\ScoringGuide;
use App\Models\SubscoreDefinition;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Sinnbeck\DomAssertions\Asserts\AssertElement;
use function Pest\Laravel\get;
use function Pest\Laravel\post;
uses(RefreshDatabase::class);
it('shows a scoring guide management page only to administrators', function () {
get(route('admin.scoring.index'))
->assertRedirect(route('home'));
actAsNormal();
get(route('admin.scoring.index'))
->assertRedirect(route('dashboard'))
->assertSessionHas('error', 'You are not authorized to perform this action');
actAsAdmin();
get(route('admin.scoring.index'))
->assertOk()
->assertViewIs('admin.scoring.index');
});
it('shows a list of scoring guides and their count of subscores', function () {
$scoringGuide = ScoringGuide::factory()->create();
SubscoreDefinition::factory()->count(3)->create(['scoring_guide_id' => $scoringGuide->id]);
Audition::factory()->count(3)->create(['scoring_guide_id' => $scoringGuide->id]);
actAsAdmin();
$response = get(route('admin.scoring.index'));
$response->assertOk()
->assertSeeInOrder(['<td', $scoringGuide->name, $scoringGuide->subscores()->count()], false);
});
it('shows a link to edit each scoring guide', function () {
$scoringGuide = ScoringGuide::factory()->create();
actAsAdmin();
$response = get(route('admin.scoring.index'));
$response->assertOk()
->assertSee(route('admin.scoring.edit', $scoringGuide));
});
it('shows auditions in groups with their scoring guide', function () {
$guides = ScoringGuide::factory()->count(2)->create();
foreach ($guides as $guide) {
Audition::factory()->count(3)->create(['scoring_guide_id' => $guide->id]);
}
actAsAdmin();
$response = get(route('admin.scoring.index'));
$response->assertOk();
foreach (Audition::all() as $audition) {
$guide = $audition->scoringGuide;
$response->assertElementExists('#guide-'.$guide->id, function (AssertElement $element) use ($audition) {
$element->containsText($audition->name);
});
}
});
it('has a form for a new scoring guide', function () {
actAsAdmin();
$response = get(route('admin.scoring.index'));
$response->assertOk()
->assertSeeInOrder(['<input', 'name=', 'name'], false)
->assertSee(route('admin.scoring.store'));
});
it('creates a new scoring guide', function () {
$formData = ['name' => 'New Scoring Guide'];
actAsAdmin();
$response = post(route('admin.scoring.store'), $formData);
/** @noinspection PhpUnhandledExceptionInspection */
$response
->assertSessionHasNoErrors()
->assertRedirect(route('admin.scoring.index'))
->assertSessionHas('success', 'Scoring guide created');
$this->assertDatabaseHas('scoring_guides', $formData);
});
it('only allows an admin to create a new scoring guide', function () {
// Arrange
$formData = ['name' => 'New Scoring Guide'];
// Act & Assert
$response = post(route('admin.scoring.store'), $formData);
$response->assertRedirect(route('home'));
actAsNormal();
$response = post(route('admin.scoring.store'), $formData);
$response->assertRedirect(route('dashboard'))
->assertSessionHas('error', 'You are not authorized to perform this action');
});