Write tests - Write tests for what was done to this point that will be kept #11

Merged
okorpheus merged 61 commits from write-tests into master 2024-07-05 21:21:32 +00:00
6 changed files with 245 additions and 8 deletions
Showing only changes of commit 5a5207357e - Show all commits

View File

@ -118,7 +118,7 @@ class SchoolController extends Controller
'school_id' => $school->id, 'school_id' => $school->id,
'domain' => request('domain')], []); 'domain' => request('domain')], []);
return redirect('/admin/schools/'.$school->id.'/edit')->with('success', 'Domain Added'); return redirect()->route('admin.schools.show', $school)->with('success', 'Domain Added');
} }

View File

@ -22,7 +22,7 @@
<x-table.body> <x-table.body>
@foreach($schools as $school) @foreach($schools as $school)
<tr> <tr>
<x-table.td><a href="{{ route('admin.schools.edit',$school) }}">{{ $school->name }}</a></x-table.td> <x-table.td><a href="{{ route('admin.schools.show',$school) }}">{{ $school->name }}</a></x-table.td>
<x-table.td> <x-table.td>
<a href="{{ route('admin.schools.invoice',$school) }}"> <a href="{{ route('admin.schools.invoice',$school) }}">
${{ number_format($schoolTotalFees[$school->id],2) }} ${{ number_format($schoolTotalFees[$school->id],2) }}

View File

@ -3,7 +3,7 @@
<x-card.list.body> <x-card.list.body>
@foreach($school->emailDomains as $domain) @foreach($school->emailDomains as $domain)
<x-card.list.row class="!py-1.5 align-middle"> <x-card.list.row class="!py-1.5 align-middle">
<form method="POST" action="/admin/schools/domain/{{ $domain->id }}" id="deleteDomain{{$domain->id}}"> <form method="POST" action="{{ route('admin.schools.destroy_domain',$domain) }}" id="deleteDomain{{$domain->id}}">
@csrf @csrf
@method('DELETE') @method('DELETE')
<button type="submit"> <button type="submit">
@ -16,7 +16,7 @@
</x-card.list.row> </x-card.list.row>
@endforeach @endforeach
<x-card.list.row class="!py-1.5"> <x-card.list.row class="!py-1.5">
<form method="POST" action="/admin/schools/{{ $school->id }}/add_domain" class="grid sm:grid-cols-2 gap-4"> <form method="POST" action="{{ route('admin.schools.add_domain',$school) }}" class="grid sm:grid-cols-2 gap-4">
@csrf @csrf
<x-form.field name="domain" label_text="Add Domain" /><x-form.button class="sm:mt-6">Add Domain</x-form.button> <x-form.field name="domain" label_text="Add Domain" /><x-form.button class="sm:mt-6">Add Domain</x-form.button>
</form> </form>

View File

@ -119,7 +119,7 @@ it('has a domain add form', function () {
'/form', '/form',
]); ]);
}); });
it('as a field to submit a new domain', function () { it('has a field to submit a new domain', function () {
// Arrange // Arrange
actingAs($this->adminUser); actingAs($this->adminUser);
// Act & Assert // Act & Assert
@ -141,7 +141,7 @@ it('allows an administrator to add a domain and shows it on the school edit page
/** @noinspection PhpUnhandledExceptionInspection */ /** @noinspection PhpUnhandledExceptionInspection */
$response $response
->assertSessionHasNoErrors() ->assertSessionHasNoErrors()
->assertRedirect(route('admin.schools.edit', $this->school)); ->assertRedirect(route('admin.schools.show', $this->school));
$this->school->refresh(); $this->school->refresh();

View File

@ -90,14 +90,14 @@ it('shows school data', function () {
], false); ], false);
} }
}); });
it('has an edit link for each school', function () { it('has a show link for each school', function () {
// Arrange // Arrange
actingAs($this->adminUser); actingAs($this->adminUser);
// Act & Assert // Act & Assert
$response = get(route('admin.schools.index')); $response = get(route('admin.schools.index'));
$response->assertOk(); $response->assertOk();
foreach ($this->schools as $school) { foreach ($this->schools as $school) {
$response->assertSee(route('admin.schools.edit', $school)); $response->assertSee(route('admin.schools.show', $school));
} }
}); });
it('has an invoice link for each school', function () { it('has an invoice link for each school', function () {

View File

@ -0,0 +1,237 @@
<?php
use App\Models\School;
use App\Models\SchoolEmailDomain;
use App\Models\User;
use Database\Factories\SchoolEmailDomainFactory;
use Illuminate\Foundation\Testing\RefreshDatabase;
use function Pest\Laravel\actingAs;
use function Pest\Laravel\delete;
use function Pest\Laravel\get;
use function Pest\Laravel\patch;
use function Pest\Laravel\post;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->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('<input type="hidden" name="_method" value="PATCH">', 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']);
});