auditionadmin/tests/Feature/app/Http/Controllers/SchoolControllerTest.php

191 lines
6.4 KiB
PHP

<?php
use App\Models\School;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use function Pest\Laravel\actingAs;
uses(RefreshDatabase::class);
describe('store method', function () {
it('will not allow a user to create a school if they already have one', function () {
$user = User::factory()->create();
$school = School::factory()->create();
$user->school_id = $school->id;
$user->save();
actingAs($user);
$response = $this->post(route('schools.store'), [
'name' => 'Test School',
'address' => '123 Test St',
'city' => 'Testville',
'state' => 'TX',
'zip' => '12345',
'phone' => '123-456-7890',
'email' => '<EMAIL>',
'website' => 'https://test.com',
]);
$response->assertStatus(403);
});
it('will allow a user with no schools to create one', function () {
$user = User::factory()->create();
actingAs($user);
$response = $this->post(route('schools.store'), [
'name' => 'Test School',
'address' => '123 Test St',
'city' => 'Testville',
'state' => 'TX',
'zip' => '12345',
]);
$school = School::first();
expect($school->name)->toEqual('Test School')
->and($school->address)->toEqual('123 Test St')
->and($school->city)->toEqual('Testville')
->and($school->state)->toEqual('TX')
->and($school->zip)->toEqual('12345');
});
it('will redirect on success', function () {
$user = User::factory()->create();
actingAs($user);
$response = $this->post(route('schools.store'), [
'name' => 'Test School',
'address' => '123 Test St',
'city' => 'Testville',
'state' => 'TX',
'zip' => '12345',
]);
$response->assertRedirect(route('schools.show', $user->school_id));
});
it('will assign the user to the new school', function () {
$user = User::factory()->create();
actingAs($user);
$response = $this->post(route('schools.store'), [
'name' => 'Test School',
'address' => '123 Test St',
'city' => 'Testville',
'state' => 'TX',
'zip' => '12345',
]);
$user->refresh();
expect($user->school_id)->toEqual(School::first()->id);
});
it('will make the user head director', function () {
$user = User::factory()->create();
actingAs($user);
$response = $this->post(route('schools.store'), [
'name' => 'Test School',
'address' => '123 Test St',
'city' => 'Testville',
'state' => 'TX',
'zip' => '12345',
]);
$user->refresh();
expect($user->hasFlag('head_director'))->toBeTrue();
});
});
describe('show method', function () {
it('will not allow the user to view a school they are not a member of', function () {
$user = User::factory()->create();
$school = School::factory()->create();
$user->school_id = $school->id;
$user->save();
actingAs($user);
$otherSchool = School::factory()->create();
$response = $this->get(route('schools.show', $otherSchool->id));
$response->assertStatus(403);
});
it('shows the view page for the users school', function () {
$user = User::factory()->create();
$school = School::factory()->create();
$user->school_id = $school->id;
$user->save();
actingAs($user);
$response = $this->get(route('schools.show', $school->id));
$response->assertStatus(200);
});
});
describe('create method', function () {
it('will not allow a user to create a school if they already have one', function () {
$user = User::factory()->create();
$school = School::factory()->create();
$user->school_id = $school->id;
$user->save();
actingAs($user);
$response = $this->get(route('schools.create'));
$response->assertStatus(403);
});
it('shows a school creation form', function () {
$user = User::factory()->create();
actingAs($user);
$response = $this->get(route('schools.create'));
$response->assertStatus(200)
->assertViewIs('schools.create');
});
});
describe('edit method', function () {
it('will not allow a user to edit a school that is not theirs', function () {
$user = User::factory()->create();
$school = School::factory()->create();
$user->school_id = $school->id;
$user->save();
actingAs($user);
$otherSchool = School::factory()->create();
$response = $this->get(route('schools.edit', $otherSchool));
$response->assertStatus(403);
});
it('shows a school edit form for the users school', function () {
$user = User::factory()->create();
$school = School::factory()->create();
$user->school_id = $school->id;
$user->save();
actingAs($user);
$response = $this->get(route('schools.edit', $school));
$response->assertStatus(200)
->assertViewIs('schools.edit');
});
});
describe('update method', function () {
it('will not allow a user to update a school that is not theirs', function () {
$user = User::factory()->create();
$school = School::factory()->create();
$user->school_id = $school->id;
$user->save();
actingAs($user);
$otherSchool = School::factory()->create();
$response = $this->patch(route('schools.update', $otherSchool));
$response->assertStatus(403);
});
it('will update the users school', function () {
$user = User::factory()->create();
$school = School::factory()->create();
$user->school_id = $school->id;
$user->save();
actingAs($user);
expect($school->name === 'Eastman')->toBeFalse();
$response = $this->patch(route('schools.update', $school), [
'name' => 'Eastman',
'address' => '26 Gibbs Street',
'city' => 'Rochester',
'state' => 'NY',
'zip' => '14604',
]);
$response->assertSessionHasNoErrors()
->assertRedirect(route('schools.show', $school));
expect($school->name === 'Eastman')->toBeFalse();
});
});