From 8f7c17ffe7bb7151bab5470e70348242c08e50bf Mon Sep 17 00:00:00 2001 From: Matt Young Date: Mon, 1 Jul 2024 15:39:46 -0500 Subject: [PATCH] StudentEdit Page Test --- app/Http/Controllers/StudentController.php | 2 +- tests/Feature/Pages/StudentsEditTest.php | 75 ++++++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 tests/Feature/Pages/StudentsEditTest.php diff --git a/app/Http/Controllers/StudentController.php b/app/Http/Controllers/StudentController.php index ab9ff78..10a616a 100644 --- a/app/Http/Controllers/StudentController.php +++ b/app/Http/Controllers/StudentController.php @@ -108,7 +108,7 @@ class StudentController extends Controller // TODO if a students grade is changed, we need to be sure they are still eligible for the auditions in which they are entered. - return redirect('/students'); + return redirect('/students')->with('success', 'Student updated successfully.'); } /** diff --git a/tests/Feature/Pages/StudentsEditTest.php b/tests/Feature/Pages/StudentsEditTest.php new file mode 100644 index 0000000..77b28e2 --- /dev/null +++ b/tests/Feature/Pages/StudentsEditTest.php @@ -0,0 +1,75 @@ +school = School::factory()->create(); + $this->user = User::factory()->create(['school_id' => $this->school->id]); + $this->student = Student::factory()->create([ + 'school_id' => $this->school->id, 'first_name' => 'Bandit', 'last_name' => 'Heeler', + ]); +}); + +test('responds positively when the user has the same school as the entry', function () { + actingAs($this->user); + get(route('students.edit', $this->student)) + ->assertOk() + ->assertSee([$this->student->first_name, 'Edit Student', 'Save Changes']); +}); + +it('denies guests', function () { + get(route('students.edit', $this->student)) + ->assertRedirect(route('home')); +}); + +it('denies users that are not at the students school', function () { + // Arrange + $otherUser = User::factory()->create(); + // Act & Assert + actingAs($otherUser); + get(route('students.edit', $this->student)) + ->assertForbidden(); +}); + +it('modifies the student', function () { + actingAs($this->user); + get(route('students.index')) + ->assertSee('Bandit') + ->assertDontSee('Bluey'); + + patch(route('students.update', $this->student), [ + 'first_name' => 'Bluey', + 'last_name' => 'Heeler', + 'grade' => 1, + ])->assertRedirect(route('students.index')) + ->assertSessionHas('success', 'Student updated successfully.') + ->assertSessionHasNoErrors(); + + get(route('students.index')) + ->assertSee('Bluey') + ->assertDontSee('Bandit'); + +}); + +it('will not modify a student not at the users school', function () { + actingAs(User::factory()->create()); + patch(route('students.update', $this->student), [ + 'first_name' => 'Bluey', + 'last_name' => 'Heeler', + 'grade' => 1, + ])->assertForbidden(); + + actingAs($this->user); + get(route('students.index')) + ->assertSee('Bandit') + ->assertDontSee('Bluey'); +});