Comments on MakeSeatingDecisionsController.php and add test for that file
This commit is contained in:
parent
09f4ed6636
commit
437ba6020b
|
|
@ -12,6 +12,10 @@ use Illuminate\Support\Arr;
|
|||
|
||||
use function redirect;
|
||||
|
||||
/**
|
||||
* Selects entries for seating in an audition and saves them to the session
|
||||
* for later formal seating.
|
||||
*/
|
||||
class MakeSeatingDecisionsController extends Controller
|
||||
{
|
||||
public function draftSeats(
|
||||
|
|
@ -30,6 +34,8 @@ class MakeSeatingDecisionsController extends Controller
|
|||
return redirect()->route('seating.audition', ['audition' => $audition->id])
|
||||
->with('error', $e->getMessage());
|
||||
}
|
||||
|
||||
// Pull out entries that have declined a seat in this audition
|
||||
$rankedEntries = $rankedEntries->reject(function ($entry) {
|
||||
return $entry->hasFlag('declined');
|
||||
});
|
||||
|
|
@ -37,13 +43,20 @@ class MakeSeatingDecisionsController extends Controller
|
|||
$rankedEntries->load(['student.school']);
|
||||
$rankedEnsembles = Ensemble::orderBy('rank')->where('event_id', $audition->event_id)->get();
|
||||
$ensembleRankOn = 1;
|
||||
|
||||
// Iterate over all ensembles that exist for the event
|
||||
foreach ($rankedEnsembles as $ensemble) {
|
||||
// If the user didn't ask for any seats in this ensemble, skip it
|
||||
if (! Arr::has($validated['ensemble'], $ensemble->id)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Set up an entry in the session for each ensemble we're going to seat
|
||||
$proposedSeatingArray[$ensembleRankOn]['ensemble_id'] = $ensemble->id;
|
||||
$proposedSeatingArray[$ensembleRankOn]['ensemble_name'] = $ensemble->name;
|
||||
$proposedSeatingArray[$ensembleRankOn]['accept_count'] = $validated['ensemble'][$ensemble->id];
|
||||
|
||||
// Pull the top rated entry for each seat in order
|
||||
for ($n = 1; $n <= $validated['ensemble'][$ensemble->id]; $n++) {
|
||||
// Escape the loop if we're out of entries
|
||||
if ($rankedEntries->isEmpty()) {
|
||||
|
|
@ -59,6 +72,8 @@ class MakeSeatingDecisionsController extends Controller
|
|||
|
||||
$ensembleRankOn++;
|
||||
}
|
||||
|
||||
// Save the data to the session
|
||||
$sessionKeyName = 'proposedSeatingArray-'.$audition->id;
|
||||
$request->session()->put($sessionKeyName, $proposedSeatingArray);
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,92 @@
|
|||
<?php
|
||||
|
||||
use App\Models\Audition;
|
||||
use App\Models\Ensemble;
|
||||
use App\Models\Entry;
|
||||
use App\Models\EntryTotalScore;
|
||||
use App\Models\Event;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
beforeEach(function () {
|
||||
$this->event = Event::factory()->create();
|
||||
$this->audition = Audition::factory()->create(['event_id' => $this->event->id]);
|
||||
$this->entries = Entry::factory()->count(3)->create(['audition_id' => $this->audition->id]);
|
||||
$score = 100;
|
||||
foreach ($this->entries as $entry) {
|
||||
EntryTotalScore::create([
|
||||
'entry_id' => $entry->id,
|
||||
'seating_total' => $score,
|
||||
'advancement_total' => $score,
|
||||
'seating_subscore_totals' => json_encode([$score, $score]),
|
||||
'advancement_subscore_totals' => json_encode([$score, $score]),
|
||||
]);
|
||||
$score--;
|
||||
}
|
||||
$this->ensemble1 = Ensemble::factory()->create(['event_id' => $this->event->id, 'rank' => 1]);
|
||||
$this->ensemble2 = Ensemble::factory()->create(['event_id' => $this->event->id, 'rank' => 2]);
|
||||
$this->ensemble3 = Ensemble::factory()->create(['event_id' => $this->event->id, 'rank' => 3]);
|
||||
});
|
||||
|
||||
it('saves proposed seating to the session', function () {
|
||||
actAsAdmin();
|
||||
$response = $this->post(route('seating.audition.draftSeats', $this->audition), [
|
||||
'ensemble' => [
|
||||
$this->ensemble1->id => 2,
|
||||
$this->ensemble2->id => 2,
|
||||
],
|
||||
]);
|
||||
$response->assertRedirect(route('seating.audition', $this->audition));
|
||||
$sessionKey = 'proposedSeatingArray-'.$this->audition->id;
|
||||
$response->assertSessionHas($sessionKey, [
|
||||
1 => [
|
||||
'ensemble_id' => $this->ensemble1->id,
|
||||
'ensemble_name' => $this->ensemble1->name,
|
||||
'accept_count' => 2,
|
||||
'seats' => [
|
||||
1 => [
|
||||
'seat' => 1,
|
||||
'entry_id' => $this->entries[0]->id,
|
||||
'entry_name' => $this->entries[0]->student->full_name(),
|
||||
'entry_school' => $this->entries[0]->student->school->name,
|
||||
],
|
||||
2 => [
|
||||
'seat' => 2,
|
||||
'entry_id' => $this->entries[1]->id,
|
||||
'entry_name' => $this->entries[1]->student->full_name(),
|
||||
'entry_school' => $this->entries[1]->student->school->name,
|
||||
],
|
||||
],
|
||||
],
|
||||
2 => [
|
||||
'ensemble_id' => $this->ensemble2->id,
|
||||
'ensemble_name' => $this->ensemble2->name,
|
||||
'accept_count' => 2,
|
||||
'seats' => [
|
||||
1 => [
|
||||
'seat' => 1,
|
||||
'entry_id' => $this->entries[2]->id,
|
||||
'entry_name' => $this->entries[2]->student->full_name(),
|
||||
'entry_school' => $this->entries[2]->student->school->name,
|
||||
],
|
||||
],
|
||||
],
|
||||
]);
|
||||
});
|
||||
|
||||
it('can clear proposed seats', function () {
|
||||
actAsAdmin();
|
||||
$response = $this->post(route('seating.audition.draftSeats', $this->audition), [
|
||||
'ensemble' => [
|
||||
$this->ensemble1->id => 2,
|
||||
$this->ensemble2->id => 2,
|
||||
],
|
||||
]);
|
||||
$response->assertRedirect(route('seating.audition', $this->audition));
|
||||
$sessionKey = 'proposedSeatingArray-'.$this->audition->id;
|
||||
$response->assertSessionHas($sessionKey);
|
||||
$response2 = $this->post(route('seating.audition.clearDraft', $this->audition));
|
||||
$response2->assertRedirect(route('seating.audition', $this->audition));
|
||||
$response2->assertSessionMissing($sessionKey);
|
||||
});
|
||||
Loading…
Reference in New Issue