Write tests - Write tests for what was done to this point that will be kept #11
|
|
@ -5,7 +5,7 @@
|
||||||
</x-card.heading>
|
</x-card.heading>
|
||||||
|
|
||||||
|
|
||||||
<x-form.form method="POST" action="/admin/schools" class="!mt-4">
|
<x-form.form method="POST" action="{{ route('admin.schools.store') }}" class="!mt-4">
|
||||||
<x-form.body-grid columns="6" class="max-w-full">
|
<x-form.body-grid columns="6" class="max-w-full">
|
||||||
<x-form.field name="name" label_text="School Name" colspan="6"/>
|
<x-form.field name="name" label_text="School Name" colspan="6"/>
|
||||||
<x-form.field name="address" label_text="School Address" colspan="6"/>
|
<x-form.field name="address" label_text="School Address" colspan="6"/>
|
||||||
|
|
|
||||||
|
|
@ -85,11 +85,11 @@ Route::middleware(['auth', 'verified', CheckIfAdmin::class])->prefix('admin/')->
|
||||||
|
|
||||||
// Admin Student Routes
|
// Admin Student Routes
|
||||||
Route::prefix('students')->controller(\App\Http\Controllers\Admin\StudentController::class)->group(function () {
|
Route::prefix('students')->controller(\App\Http\Controllers\Admin\StudentController::class)->group(function () {
|
||||||
Route::get('/', 'index');
|
Route::get('/', 'index')->name('admin.students.index');
|
||||||
Route::get('/create', 'create');
|
Route::get('/create', 'create')->name('admin.students.create');
|
||||||
Route::post('/', 'store');
|
Route::post('/', 'store')->name('admin.students.store');
|
||||||
Route::get('/{student}/edit', 'edit');
|
Route::get('/{student}/edit', 'edit')->name('admin.students.edit');
|
||||||
Route::patch('/{student}', 'update');
|
Route::patch('/{student}', 'update')->name('admin.students.update');
|
||||||
});
|
});
|
||||||
|
|
||||||
// Admin School Routes
|
// Admin School Routes
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,101 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Models\School;
|
||||||
|
use App\Models\User;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use function Pest\Laravel\actingAs;
|
||||||
|
use function Pest\Laravel\get;
|
||||||
|
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();
|
||||||
|
});
|
||||||
|
it('only shows for an admin user', function () {
|
||||||
|
// Act & Assert
|
||||||
|
$checkRoute = 'admin.schools.create';
|
||||||
|
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('submits a post request', function () {
|
||||||
|
// Arrange
|
||||||
|
actingAs($this->adminUser);
|
||||||
|
// Act & Assert
|
||||||
|
$response = get(route('admin.schools.create'));
|
||||||
|
$response->assertOk();
|
||||||
|
$response->assertSeeInOrder([
|
||||||
|
'form',
|
||||||
|
'method',
|
||||||
|
'POST',
|
||||||
|
'action=',
|
||||||
|
route('admin.schools.store'),
|
||||||
|
'/form',
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
it('has all needed fields', function () {
|
||||||
|
// Arrange
|
||||||
|
actingAs($this->adminUser);
|
||||||
|
$fieldNames = [
|
||||||
|
'name',
|
||||||
|
'address',
|
||||||
|
'city',
|
||||||
|
'state',
|
||||||
|
'zip',
|
||||||
|
];
|
||||||
|
// Act & Assert
|
||||||
|
$response = get(route('admin.schools.create'));
|
||||||
|
$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 = post(route('admin.schools.store'), [
|
||||||
|
'name' => 'Hacker High',
|
||||||
|
'address' => 'Lost Highway',
|
||||||
|
]);
|
||||||
|
$response->assertRedirect(route('dashboard'));
|
||||||
|
});
|
||||||
|
it('allows an administrator to create a school', function () {
|
||||||
|
// Arrange
|
||||||
|
actingAs($this->adminUser);
|
||||||
|
$newData = [
|
||||||
|
'name' => fake()->city(),
|
||||||
|
'address' => fake()->streetAddress(),
|
||||||
|
'city' => fake()->city(),
|
||||||
|
'state' => 'OK',
|
||||||
|
'zip' => fake()->postcode(),
|
||||||
|
];
|
||||||
|
// Act
|
||||||
|
$response = post(route('admin.schools.store'), $newData);
|
||||||
|
/** @noinspection PhpUnhandledExceptionInspection */
|
||||||
|
$response
|
||||||
|
->assertSessionHasNoErrors()
|
||||||
|
->assertRedirect(route('admin.schools.index'));
|
||||||
|
|
||||||
|
// Get the latest created user
|
||||||
|
$createdSchool = School::where('name', $newData['name'])->first();
|
||||||
|
expect($createdSchool->name)->toBe($newData['name'])
|
||||||
|
->and($createdSchool->address)->toBe($newData['address'])
|
||||||
|
->and($createdSchool->city)->toBe($newData['city'])
|
||||||
|
->and($createdSchool->state)->toBe($newData['state'])
|
||||||
|
->and($createdSchool->zip)->toEqual($newData['zip']);
|
||||||
|
|
||||||
|
get(route('admin.schools.index'))
|
||||||
|
->assertOk()
|
||||||
|
->assertSee($newData['name']);
|
||||||
|
});
|
||||||
Loading…
Reference in New Issue