get(route('admin.students.index'))->assertRedirect(route('home')); actAsNormal(); $this->get(route('admin.students.index'))->assertRedirect(route('dashboard')); actAsTab(); $this->get(route('admin.students.index'))->assertRedirect(route('dashboard')); }); it('it shows an index of students', function () { actAsAdmin(); $students = Student::factory()->count(3)->create(); $response = $this->get(route('admin.students.index'))->assertOk(); $studentToView = $response->viewData('students'); expect($studentToView)->toHaveCount(3); foreach ($students as $student) { expect(in_array($student->id, $studentToView->pluck('id')->toArray())); } $response->assertViewIs( 'admin.students.index'); }); it('can filter by first name', function () { $names = ['name1', 'name2', 'name3']; foreach ($names as $name) { $students[] = Student::factory()->create(['first_name' => $name]); } actAsAdmin(); $response = $this->withSession([ 'adminStudentFilters' => [ 'first_name' => 'name3', 'last_name' => '', 'school' => 'all', 'grade' => 'all', ], ]) ->get(route('admin.students.index')); file_put_contents(storage_path('debug.html'), $response->getContent()); $response->assertOk() ->assertViewIs('admin.students.index'); $returnedStudentIds = $response->viewData('students')->pluck('id')->toArray(); expect($returnedStudentIds)->toHaveCount(1) ->and(in_array($students[0]->id, $returnedStudentIds))->toBeFalsy() ->and(in_array($students[1]->id, $returnedStudentIds))->toBeFalsy() ->and(in_array($students[2]->id, $returnedStudentIds))->toBeTruthy(); }); it('can filter by last name', function () { $names = ['name1', 'name2', 'name3']; foreach ($names as $name) { $students[] = Student::factory()->create(['last_name' => $name]); } actAsAdmin(); $response = $this->withSession([ 'adminStudentFilters' => [ 'first_name' => '', 'last_name' => 'name2', 'school' => 'all', 'grade' => 'all', ], ]) ->get(route('admin.students.index')); file_put_contents(storage_path('debug.html'), $response->getContent()); $response->assertOk() ->assertViewIs('admin.students.index'); $returnedStudentIds = $response->viewData('students')->pluck('id')->toArray(); expect($returnedStudentIds)->toHaveCount(1) ->and(in_array($students[0]->id, $returnedStudentIds))->toBeFalsy() ->and(in_array($students[1]->id, $returnedStudentIds))->toBeTruthy() ->and(in_array($students[2]->id, $returnedStudentIds))->toBeFalsy(); }); it('can filter by grade', function () { $grades = [6, 7, 8]; foreach ($grades as $grade) { $students[] = Student::factory()->create(['grade' => $grade]); } actAsAdmin(); $response = $this->withSession([ 'adminStudentFilters' => [ 'first_name' => '', 'last_name' => '', 'school' => 'all', 'grade' => '8', ], ]) ->get(route('admin.students.index')); file_put_contents(storage_path('debug.html'), $response->getContent()); $response->assertOk() ->assertViewIs('admin.students.index'); $returnedStudentIds = $response->viewData('students')->pluck('id')->toArray(); expect($returnedStudentIds)->toHaveCount(1) ->and(in_array($students[0]->id, $returnedStudentIds))->toBeFalsy() ->and(in_array($students[1]->id, $returnedStudentIds))->toBeFalsy() ->and(in_array($students[2]->id, $returnedStudentIds))->toBeTruthy(); }); it('can filter by school', function () { $schools[1] = School::factory()->create(); $schools[2] = School::factory()->create(); $schools[3] = School::factory()->create(); foreach ($schools as $school) { $students[] = Student::factory()->create(['school_id' => $school->id]); } actAsAdmin(); $response = $this->withSession([ 'adminStudentFilters' => [ 'first_name' => '', 'last_name' => '', 'school' => $schools[3]->id, 'grade' => 'all', ], ]) ->get(route('admin.students.index')); $response->assertOk() ->assertViewIs('admin.students.index'); $returnedStudentIds = $response->viewData('students')->pluck('id')->toArray(); expect($returnedStudentIds)->toHaveCount(1) ->and(in_array($students[0]->id, $returnedStudentIds))->toBeFalsy() ->and(in_array($students[1]->id, $returnedStudentIds))->toBeFalsy() ->and(in_array($students[2]->id, $returnedStudentIds))->toBeTruthy(); }); }); describe('StudentController::create', function () { it('denies access to a non-admin user', function () { $this->get(route('admin.students.create'))->assertRedirect(route('home')); actAsNormal(); $this->get(route('admin.students.create'))->assertRedirect(route('dashboard')); actAsTab(); $this->get(route('admin.students.create'))->assertRedirect(route('dashboard')); }); it('shows a form to create a student', function () { School::factory()->count(5)->create(); Audition::factory()->create(['minimum_grade' => 6, 'maximum_grade' => 8]); Audition::factory()->create(['minimum_grade' => 7, 'maximum_grade' => 11]); actAsAdmin(); $response = $this->get(route('admin.students.create'))->assertOk(); $response->assertViewIs('admin.students.create') ->assertViewHas('schools') ->assertViewHas('minGrade', 6) ->assertViewHas('maxGrade', 11); }); it('still works when there are nomination ensembles', function () { School::factory()->count(5)->create(); Audition::factory()->create(['minimum_grade' => 6, 'maximum_grade' => 8]); Audition::factory()->create(['minimum_grade' => 7, 'maximum_grade' => 11]); NominationEnsemble::factory()->create(['minimum_grade' => 6, 'maximum_grade' => 8]); actAsAdmin(); $response = $this->get(route('admin.students.create'))->assertOk(); $response->assertViewIs('admin.students.create') ->assertViewHas('schools') ->assertViewHas('minGrade', 6) ->assertViewHas('maxGrade', 11); }); }); describe('StudentController::store', function () { it('denies access to a non-admin user', function () { $this->post(route('admin.students.store'))->assertRedirect(route('home')); actAsNormal(); $this->post(route('admin.students.store'))->assertRedirect(route('dashboard')); actAsTab(); $this->post(route('admin.students.store'))->assertRedirect(route('dashboard')); }); it('creates a student', function () { $school = School::factory()->create(); actAsAdmin(); $response = $this->post(route('admin.students.store'), [ 'first_name' => 'John', 'last_name' => 'Doe', 'school_id' => $school->id, 'grade' => 6, ]); file_put_contents(storage_path('debug.html'), $response->getContent()); $response->assertRedirect(route('admin.students.index')) ->assertSessionHas('success', 'Student created successfully'); $student = Student::first(); expect($student->first_name)->toEqual('John') ->and($student->last_name)->toEqual('Doe') ->and($student->school_id)->toEqual($school->id) ->and($student->grade)->toEqual(6); }); it('does not allow a duplicate student', function () { $school = School::factory()->create(); Student::create([ 'first_name' => 'John', 'last_name' => 'Doe', 'school_id' => $school->id, 'grade' => 6, ]); actAsAdmin(); $response = $this->post(route('admin.students.store'), [ 'first_name' => 'John', 'last_name' => 'Doe', 'school_id' => $school->id, 'grade' => 6, ]); $response->assertRedirect(route('home')); }); }); describe('StudentController::edit', function () { beforeEach(function () { $this->student = Student::factory()->create(); }); it('denies access to a non-admin user', function () { $this->get(route('admin.students.edit', 1))->assertRedirect(route('home')); actAsNormal(); $this->get(route('admin.students.edit', 1))->assertRedirect(route('dashboard')); actAsTab(); $this->get(route('admin.students.edit', 1))->assertRedirect(route('dashboard')); }); });