From 0c782c9a64eb76c028d12d2dee614d8ce531524a Mon Sep 17 00:00:00 2001 From: Matt Young Date: Wed, 3 Jul 2024 02:52:42 -0500 Subject: [PATCH] AdminStudentEdit Page Test --- resources/views/admin/students/edit.blade.php | 2 +- tests/Feature/Pages/Admin/StudentEditTest.php | 159 ++++++++++++++++++ 2 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 tests/Feature/Pages/Admin/StudentEditTest.php diff --git a/resources/views/admin/students/edit.blade.php b/resources/views/admin/students/edit.blade.php index 36dce16..81436b2 100644 --- a/resources/views/admin/students/edit.blade.php +++ b/resources/views/admin/students/edit.blade.php @@ -1,7 +1,7 @@ Edit Student - + diff --git a/tests/Feature/Pages/Admin/StudentEditTest.php b/tests/Feature/Pages/Admin/StudentEditTest.php new file mode 100644 index 0000000..bef15cf --- /dev/null +++ b/tests/Feature/Pages/Admin/StudentEditTest.php @@ -0,0 +1,159 @@ +adminUser = User::factory()->admin()->create(); + $this->schools = School::factory()->count(5)->create(); + $this->student = Student::factory()->create(); +}); + +it('only shows for an admin user', function () { + // Act & Assert + $checkRoute = 'admin.students.edit'; + get(route($checkRoute, $this->student))->assertRedirect(route('home')); + actingAs($this->adminUser); + get(route($checkRoute, $this->student))->assertOk(); + actingAs(User::factory()->create()); + get(route($checkRoute, $this->student))->assertRedirect(route('dashboard')); +}); +it('submits a patch request', function () { + // Arrange + actingAs($this->adminUser); + // Act & Assert + $response = get(route('admin.students.edit', $this->student)); + $response->assertOk(); + $response->assertSeeInOrder([ + 'form', + 'method', + 'POST', + 'action=', + route('admin.students.update', $this->student), + '/form', + ], false); + $response->assertSee('', false); +}); +it('has an Edit Student submit button', function () { + // Arrange + actingAs($this->adminUser); + // Act & Assert + $response = get(route('admin.students.edit', $this->student)); + $response->assertOk(); + $response->assertSeeInOrder([ + 'button', + 'type', + 'submit', + 'Edit Student', + '/button', + ], false); +}); +it('has all needed fields', function () { + // Arrange + actingAs($this->adminUser); + $fieldNames = [ + 'first_name', + 'last_name', + ]; + // Act & Assert + $response = get(route('admin.students.edit', $this->student)); + $response->assertOk(); + foreach ($fieldNames as $fieldName) { + $response->assertSeeInOrder([ + 'input', + 'name=', + $fieldName, + '/', + ]); + } + $response->assertSeeInOrder([ + 'select', + 'name=', + 'school_id', + '/select', + ]); + $response->assertSeeInOrder([ + 'select', + 'name=', + 'grade', + '/select', + ]); +}); +it('has all schools in a dropdown', function () { + // Arrange + actingAs($this->adminUser); + // Act & Assert + $response = get(route('admin.students.edit', $this->student)); + $response->assertOk(); + foreach ($this->schools as $school) { + $response->assertSeeInOrder([ + 'option', + 'value=', + $school->id, + $school->name, + '/option', + ]); + } +}); +it('is populated with existing data', function () { + // Arrange + Audition::factory()->create(['minimum_grade' => 1, 'maximum_grade' => 18]); // Needed for the grade select + actingAs($this->adminUser); + $fieldList = ['first_name', 'last_name', 'grade', 'school_id']; + // Act & Assert + $response = get(route('admin.students.edit', $this->student)); + $response->assertOk(); + $response->assertSeeInOrder(['name=', 'first_name', 'value=', $this->student->first_name]); + $response->assertSeeInOrder(['name=', 'last_name', 'value=', $this->student->last_name]); + $response->assertSeeInOrder(['grade', 'option', 'value=', $this->student->grade], 'selected'); + $response->assertSeeInOrder(['name=', 'school_id', 'value=', $this->student->school_id], 'selected'); +}); +it('rejects a submission by a non administrator', function () { + // Arrange + actingAs(User::factory()->create()); + // Act & Assert + $response = patch(route('admin.students.update', $this->student), [ + 'first_name' => 'New First Name', + 'last_name' => 'New Last Name', + ]); + $response->assertRedirect(route('dashboard')); +}); +it('allows an administrator to edit a student', function () { + // Arrange + $newSchool = School::factory()->create(['name' => 'New School']); + actingAs($this->adminUser); + $newData = [ + 'first_name' => 'New First Name', + 'last_name' => 'New Last Name', + 'grade' => '9', + 'school_id' => $newSchool->id, + ]; + // Act + $response = patch(route('admin.students.update', $this->student), $newData); + /** @noinspection PhpUnhandledExceptionInspection */ + $response + ->assertSessionHasNoErrors() + ->assertRedirect(route('admin.students.index')); + + // Get the changed student + $this->student->refresh(); + expect($this->student->first_name)->toBe($newData['first_name']) + ->and($this->student->last_name)->toBe($newData['last_name']) + ->and($this->student->grade)->toEqual($newData['grade']) + ->and($this->student->school->name)->toBe($newSchool->name); + + get(route('admin.students.index')) + ->assertOk() + ->assertSee($newData['first_name']) + ->assertSee($newData['last_name']) + ->assertSee($newData['grade']) + ->assertSee($newSchool->name); +});