From 635cf3f7be827176534a5344a7a137cbeb11c277 Mon Sep 17 00:00:00 2001 From: Matt Young Date: Tue, 2 Jul 2024 16:09:12 -0500 Subject: [PATCH] AdminSchoolsCreate Page Test --- .../views/admin/schools/create.blade.php | 2 +- routes/admin.php | 10 +- .../Feature/Pages/Admin/SchoolsCreateTest.php | 101 ++++++++++++++++++ 3 files changed, 107 insertions(+), 6 deletions(-) create mode 100644 tests/Feature/Pages/Admin/SchoolsCreateTest.php diff --git a/resources/views/admin/schools/create.blade.php b/resources/views/admin/schools/create.blade.php index 145406c..c513bad 100644 --- a/resources/views/admin/schools/create.blade.php +++ b/resources/views/admin/schools/create.blade.php @@ -5,7 +5,7 @@ - + diff --git a/routes/admin.php b/routes/admin.php index 9c0c26a..8479387 100644 --- a/routes/admin.php +++ b/routes/admin.php @@ -85,11 +85,11 @@ Route::middleware(['auth', 'verified', CheckIfAdmin::class])->prefix('admin/')-> // Admin Student Routes Route::prefix('students')->controller(\App\Http\Controllers\Admin\StudentController::class)->group(function () { - Route::get('/', 'index'); - Route::get('/create', 'create'); - Route::post('/', 'store'); - Route::get('/{student}/edit', 'edit'); - Route::patch('/{student}', 'update'); + Route::get('/', 'index')->name('admin.students.index'); + Route::get('/create', 'create')->name('admin.students.create'); + Route::post('/', 'store')->name('admin.students.store'); + Route::get('/{student}/edit', 'edit')->name('admin.students.edit'); + Route::patch('/{student}', 'update')->name('admin.students.update'); }); // Admin School Routes diff --git a/tests/Feature/Pages/Admin/SchoolsCreateTest.php b/tests/Feature/Pages/Admin/SchoolsCreateTest.php new file mode 100644 index 0000000..23a26aa --- /dev/null +++ b/tests/Feature/Pages/Admin/SchoolsCreateTest.php @@ -0,0 +1,101 @@ +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']); +});