auditionadmin/tests/Feature/app/Http/Controllers/Admin/DrawControllerTest.php

158 lines
6.7 KiB
PHP

<?php
use App\Models\Audition;
use App\Models\Entry;
use App\Models\Event;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
describe('DrawController::index', function () {
it('denies access to a non-admin user', function () {
$this->get(route('admin.draw.index'))->assertRedirect(route('home'));
actAsNormal();
$this->get(route('admin.draw.index'))->assertRedirect(route('dashboard'));
actAsTab();
$this->get(route('admin.draw.index'))->assertRedirect(route('dashboard'));
});
it('returns a page to select auditions to draw, including a section for each event that has entries', function () {
$events = Event::factory()->count(3)->create();
foreach ($events as $event) {
Audition::factory()->forEvent($event)->count(3)->create();
}
actAsAdmin();
$response = $this->get(route('admin.draw.index'));
$response->assertOk()
->assertViewIs('admin.draw.index')
->assertSee('events');
foreach ($events as $event) {
$response->assertSee($event->name, false);
}
});
it('tells the view if any auditions are drawn', function () {
$events = Event::factory()->count(3)->create();
foreach ($events as $event) {
Audition::factory()->forEvent($event)->count(3)->create();
}
actAsAdmin();
$response = $this->get(route('admin.draw.index'));
$response->assertOk();
expect($response->viewData('drawnAuditionsExist'))->toBeFalse();
foreach (Audition::all() as $audition) {
$response->assertSee($audition->name, false);
}
$testCase = Audition::first();
$testCase->addFlag('drawn');
$response = $this->get(route('admin.draw.index'));
$response->assertOk();
expect($response->viewData('drawnAuditionsExist'))->toBeTrue();
$response->assertDontSee($testCase->name, false);
});
});
describe('DrawController::store', function () {
it('denies access to a non-admin user', function () {
$this->post(route('admin.draw.store'))->assertRedirect(route('home'));
actAsNormal();
$this->post(route('admin.draw.store'))->assertRedirect(route('dashboard'));
actAsTab();
$this->post(route('admin.draw.store'))->assertRedirect(route('dashboard'));
});
it('draws selected auditions', function () {
$auditions = Audition::factory()->count(5)->create();
$testCase1 = $auditions[0];
$testCase2 = $auditions[1];
$input = ['audition' => [$testCase1->id => 1, $testCase2->id => 1]];
actAsAdmin();
$this->post(route('admin.draw.store'), $input)
->assertRedirect(route('admin.draw.index'))
->assertSessionHas('success');
$testCase1->refresh();
$testCase2->refresh();
expect($testCase1->hasFlag('drawn'))->toBeTrue();
expect($testCase2->hasFlag('drawn'))->toBeTrue();
});
it('will not draw an audition if it is already drawn', function () {
$auditions = Audition::factory()->count(5)->create();
$testCase = $auditions[0];
$testCase->addFlag('drawn');
$input = ['audition' => [$testCase->id => 1]];
actAsAdmin();
$this->post(route('admin.draw.store'), $input)
->assertRedirect(route('admin.draw.index'))
->assertSessionHas('error', 'Cannot run draw. Some auditions have already been drawn.');
$testCase->refresh();
});
});
describe('DrawController::edit', function () {
it('denies access to a non-admin user', function () {
$this->get(route('admin.draw.edit'))->assertRedirect(route('home'));
actAsNormal();
$this->get(route('admin.draw.edit'))->assertRedirect(route('dashboard'));
actAsTab();
$this->get(route('admin.draw.edit'))->assertRedirect(route('dashboard'));
});
it('returns a page to select drawn auditions to be cleared. Includes all drawn auditions', function () {
$auditions = Audition::factory()->count(5)->create();
$testCase1 = $auditions[0];
$testCase2 = $auditions[1];
$testCase1->addFlag('drawn');
$testCase2->addFlag('drawn');
actAsAdmin();
$response = $this->get(route('admin.draw.edit'), ['audition' => [$testCase1->id => 1, $testCase2->id => 1]]);
$response->assertOk()
->assertViewIs('admin.draw.edit');
expect($response->viewData('drawnAuditions')->contains('id', $testCase1->id))->toBeTrue();
expect($response->viewData('drawnAuditions')->contains('id', $testCase2->id))->toBeTrue();
expect($response->viewData('drawnAuditions')->contains('id', $auditions[2]->id))->toBeFalse();
});
});
describe('DrawController::destroy', function () {
it('denies access to a non-admin user', function () {
$this->delete(route('admin.draw.destroy'))->assertRedirect(route('home'));
actAsNormal();
$this->delete(route('admin.draw.destroy'))->assertRedirect(route('dashboard'));
actAsTab();
$this->delete(route('admin.draw.destroy'))->assertRedirect(route('dashboard'));
});
it('clears selected drawn auditions', function () {
$auditions = Audition::factory()->count(5)->create();
$testCase1 = $auditions[0];
$testCase2 = $auditions[1];
$testCase3 = $auditions[2];
foreach (Audition::all() as $audition) {
Entry::factory()->forAudition($audition)->count(5)->create();
}
actAsAdmin();
expect(Entry::where('audition_id', $testCase1->id)->first()->draw_number)->toBeNull();
// First off, get the draws run
$this->post(route('admin.draw.store'), [
'audition' => [
$testCase1->id => 1,
$testCase2->id => 1,
$testCase3->id => 1,
],
]);
$testCase1->refresh();
$testCase2->refresh();
$testCase3->refresh();
expect($testCase1->hasFlag('drawn'))->toBeTrue()
->and(Entry::where('audition_id', $testCase1->id)->first()->draw_number)->not()->toBeNull();
$this->delete(route('admin.draw.destroy'), ['audition' => [$testCase1->id => 1, $testCase2->id => 1]])
->assertRedirect(route('admin.draw.index'))
->assertSessionHas('success');
$testCase1->refresh();
$testCase2->refresh();
$testCase3->refresh();
expect($testCase1->hasFlag('drawn'))->toBeFalse()
->and($testCase2->hasFlag('drawn'))->toBeFalse()
->and($testCase3->hasFlag('drawn'))->toBeTrue();
expect(Entry::where('audition_id', $testCase1->id)->first()->draw_number)->toBeNull();
});
});