adminUser = User::factory()->admin()->create();
$this->nonAdminUser = User::factory()->create();
$this->tabUser = User::factory()->tab()->create();
$this->school = School::factory()->create();
$this->domains = SchoolEmailDomain::factory()->count(3)->create(['school_id' => $this->school->id]);
$this->directors = User::factory()->count(3)->create(['school_id' => $this->school->id]);
});
it('only shows for an admin user', function () {
// Act & Assert
$checkRoute = 'admin.schools.show';
get(route($checkRoute, $this->school))->assertRedirect(route('home'));
actingAs($this->adminUser);
get(route($checkRoute, $this->school))->assertOk();
actingAs($this->nonAdminUser);
get(route($checkRoute, $this->school))->assertRedirect(route('dashboard'));
});
it('shows the school address', function () {
// Arrange
actingAs($this->adminUser);
// Act & Assert
$response = get(route('admin.schools.show', $this->school));
$response->assertOk();
$response->assertSee($this->school->name);
$response->assertSee($this->school->address);
$response->assertSee($this->school->city);
$response->assertSee($this->school->state);
$response->assertSee($this->school->zip);
});
it('shows the schools directors and their information', function () {
// Arrange
actingAs($this->adminUser);
// Act & Assert
$response = get(route('admin.schools.show', $this->school));
$response->assertOk();
foreach ($this->directors as $director) {
$response->assertSee($director->full_name());
$response->assertSee($director->email);
$response->assertSee($director->cell_phone);
$response->assertSee($director->judging_preference);
}
});
// Tests dealing with the school email domains
it('has a domain add form', function () {
// Arrange
actingAs($this->adminUser);
// Act & Assert
get(route('admin.schools.show', $this->school))
->assertOk()
->assertSeeInOrder([
'form',
'method',
'POST',
'action=',
route('admin.schools.add_domain', $this->school),
'/form',
]);
});
it('has a field to submit a new domain', function () {
// Arrange
actingAs($this->adminUser);
// Act & Assert
get(route('admin.schools.show', $this->school))
->assertOk()
->assertSeeInOrder([
'input',
'name=',
'domain',
'/',
]);
});
it('allows an administrator to add a domain and shows it on the school edit page', function () {
// Arrange
actingAs($this->adminUser);
$newDomain = 'example.com';
// Act
$response = post(route('admin.schools.add_domain', $this->school), ['domain' => $newDomain]);
/** @noinspection PhpUnhandledExceptionInspection */
$response
->assertSessionHasNoErrors()
->assertRedirect(route('admin.schools.show', $this->school));
$this->school->refresh();
$this->assertDatabaseHas('school_email_domains', ['school_id' => $this->school->id, 'domain' => $newDomain]);
get(route('admin.schools.show', $this->school))
->assertOk()
->assertSee($newDomain);
});
it('rejects a domain submission by a non administrator', function () {
// Arrange
actingAs($this->nonAdminUser);
// Act & Assert
$response = post(route('admin.schools.add_domain', $this->school), ['domain' => 'example.com']);
$response->assertRedirect(route('dashboard'));
});
it('shows a delete button for each domain', function () {
// Arrange
actingAs($this->adminUser);
$domain = $this->school->emailDomains()->create(['domain' => 'example.com']);
// Act & Assert
get(route('admin.schools.show', $this->school))
->assertOk()
->assertSee(route('admin.schools.destroy_domain', $domain));
});
it('allows an admin to delete a domain', function () {
// Arrange
session()->setPreviousUrl(route('admin.schools.show', $this->school));
actingAs($this->adminUser);
$domain = $this->school->emailDomains()->create(['domain' => 'example.com']);
// Act
$response = delete(route('admin.schools.destroy_domain', $domain));
/** @noinspection PhpUnhandledExceptionInspection */
$response
->assertSessionHasNoErrors()
->assertRedirect(route('admin.schools.show', $this->school));
$this->assertDatabaseMissing('school_email_domains', ['id' => $domain->id]);
});
it('does not allow a non admin user to delete a domain', function () {
// Arrange
actingAs($this->nonAdminUser);
$domain = SchoolEmailDomainFactory::new()->create();
// Act & Assert
$response = delete(route('admin.schools.destroy_domain', $domain));
$response->assertRedirect(route('dashboard'));
});
// Tests Dealing With The Edit Modal
it('has a link to open the edit modal', function () {
// Arrange
actingAs($this->adminUser);
// Act & Assert
get(route('admin.schools.show', $this->school))
->assertOk()
->assertSeeInOrder([
'@click',
'showModal',
'edit',
]);
});
it('submits a patch request', function () {
// Arrange
actingAs($this->adminUser);
// Act & Assert
$response = get(route('admin.schools.show', $this->school));
$response->assertOk();
$response->assertSeeInOrder([
'form',
'method',
'POST',
'action=',
route('admin.schools.update', $this->school),
'/form',
]);
$response->assertSee('', false);
});
it('has all needed fields', function () {
// Arrange
actingAs($this->adminUser);
$fieldNames = [
'name',
'address',
'city',
'state',
'zip',
];
// Act & Assert
$response = get(route('admin.schools.show', $this->school));
$response->assertOk();
foreach ($fieldNames as $fieldName) {
$response->assertSeeInOrder([
'input',
'name=',
$fieldName,
'/',
]);
}
});
it('rejects a submission by a non administrator', function () {
// Arrange
actingAs($this->nonAdminUser);
// Act & Assert
$response = patch(route('admin.schools.update', $this->school), [
'name' => 'Hacker High',
'address' => 'Lost Highway',
]);
$response->assertRedirect(route('dashboard'));
});
it('allows an administrator to update a school', function () {
// Arrange
actingAs($this->adminUser);
$newData = [
'name' => fake()->city(),
'address' => fake()->streetAddress(),
'city' => fake()->city(),
'state' => 'OK',
'zip' => fake()->postcode(),
];
// Act
$response = patch(route('admin.schools.update', $this->school), $newData);
/** @noinspection PhpUnhandledExceptionInspection */
$response
->assertSessionHasNoErrors()
->assertRedirect(route('admin.schools.show', $this->school));
$this->school->refresh();
expect($this->school->name)->toBe($newData['name'])
->and($this->school->address)->toBe($newData['address'])
->and($this->school->city)->toBe($newData['city'])
->and($this->school->state)->toBe($newData['state'])
->and($this->school->zip)->toEqual($newData['zip']);
get(route('admin.schools.index'))
->assertOk()
->assertSee($newData['name']);
});
it('includes a form to destroy the school IF it has no students', function () {
// Arrange
$condemnedSchool = School::factory()->create();
actingAs($this->adminUser);
// Act & Assert
get(route('admin.schools.edit', $condemnedSchool))
->assertOk()
->assertSeeInOrder([
'form',
'method',
'POST',
'action=',
route('admin.schools.destroy', $condemnedSchool),
'/form',
], false)
->assertSee('', false);
});
it('does not include the destruction form if the school has students', function () {
// Arrange
$condemnedSchool = School::factory()->create();
Student::factory()->create(['school_id' => $condemnedSchool->id]);
actingAs($this->adminUser);
// Act & Assert
get(route('admin.schools.edit', $condemnedSchool))
->assertOk()
->assertDontSee('', false);
});
it('allows an administrator to destroy a school without students', function () {
// Arrange
$condemnedSchool = School::factory()->create();
// Act & Assert
expect($condemnedSchool->exists())->toBeTrue();
actingAs($this->adminUser);
/** @noinspection PhpUnhandledExceptionInspection */
delete(route('admin.schools.destroy', $condemnedSchool))
->assertSessionHasNoErrors()
->assertRedirect(route('admin.schools.index'));
expect(School::find($condemnedSchool->id))->toBeNull();
});
it('does not allow an administrator to destroy a student with entries', function () {
// Arrange
$condemnedSchool = School::factory()->create();
Student::factory()->create(['school_id' => $condemnedSchool->id]);
// Act & Assert
expect($condemnedSchool->exists())->toBeTrue();
actingAs($this->adminUser);
/** @noinspection PhpUnhandledExceptionInspection */
delete(route('admin.schools.destroy', $condemnedSchool))
->assertSessionHas('error', 'You cannot delete a school with students.')
->assertRedirect(route('admin.schools.index'))
->assertSessionHasNoErrors();
expect(School::find($condemnedSchool->id))->toBeInstanceOf(School::class);
});
it('does not allow a non administrator to delete a student', function () {
// Arrange
$condemnedSchool = School::factory()->create();
// Act & Assert
expect($condemnedSchool->exists())->toBeTrue();
actingAs(User::factory()->create());
/** @noinspection PhpUnhandledExceptionInspection */
delete(route('admin.schools.destroy', $condemnedSchool))
->assertSessionHasNoErrors()
->assertSessionHas('error', 'You are not authorized to perform this action')
->assertRedirect(route('dashboard'));
expect(School::find($condemnedSchool->id))->toBeInstanceOf(School::class);
});