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'); }); it('will not duplicate a student name at the school', function () { actingAs($this->user); $student = Student::factory()->create(['school_id' => $this->school->id, 'first_name' => 'Bluey', 'last_name' => 'Heeler']); patch(route('students.update', $this->student), [ 'first_name' => 'Bluey', 'last_name' => 'Heeler', 'grade' => 1, ])->assertRedirect(route('students.edit', $this->student)) ->assertSessionHas('error', 'A student with that name already exists at your school.') ->assertSessionHasNoErrors(); });