135 lines
3.5 KiB
PHP
135 lines
3.5 KiB
PHP
<?php
|
|
|
|
use App\Models\School;
|
|
use App\Models\Student;
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
|
|
use function Pest\Laravel\get;
|
|
use function Pest\Laravel\post;
|
|
|
|
uses(RefreshDatabase::class);
|
|
|
|
it('cannot be accessed by a guest', function () {
|
|
// Act & Assert
|
|
get(route('students.index'))
|
|
->assertStatus(302)
|
|
->assertRedirect(route('home'));
|
|
});
|
|
|
|
it('cannot be accessed by a user without a school', function () {
|
|
// Arrange
|
|
$user = User::factory()->create();
|
|
$userWithSchool = User::factory()->create([
|
|
'school_id' => School::factory()->create()->id,
|
|
]);
|
|
// Act & Assert
|
|
$this->actingAs($user);
|
|
get('/students')
|
|
->assertStatus(403);
|
|
|
|
$this->actingAs($userWithSchool);
|
|
get('/students')
|
|
->assertStatus(200);
|
|
|
|
});
|
|
|
|
it('Shows a student for the user', function () {
|
|
// Arrange
|
|
$school = School::factory()->create();
|
|
$user = User::factory()->create([
|
|
'school_id' => $school->id,
|
|
]);
|
|
$student = Student::factory()->create([
|
|
'school_id' => $school->id,
|
|
]);
|
|
|
|
$students = [
|
|
['first_name' => 'John', 'last_name' => 'Lennon'],
|
|
['first_name' => 'Paul', 'last_name' => 'McCartney'],
|
|
['first_name' => 'George', 'last_name' => 'Harrison'],
|
|
['first_name' => 'Ringo', 'last_name' => 'Starr'],
|
|
];
|
|
|
|
foreach ($students as $s) {
|
|
Student::factory()->create([
|
|
'school_id' => $school->id,
|
|
'first_name' => $s['first_name'],
|
|
'last_name' => $s['last_name'],
|
|
]);
|
|
}
|
|
|
|
// Act and Assert
|
|
$this->actingAs($user);
|
|
get(route('students.index'))
|
|
->assertStatus(200)
|
|
->assertSeeText($student->name)
|
|
->assertSeeText(['Lennon, John', 'McCartney, Paul', 'Harrison, George', 'Starr, Ringo']);
|
|
});
|
|
|
|
it('does not show a student from another school', function () {
|
|
// Arrange
|
|
$school = School::factory()->create();
|
|
$user = User::factory()->create([
|
|
'school_id' => $school->id,
|
|
]);
|
|
$student = Student::factory()->create([
|
|
'school_id' => $school->id,
|
|
]);
|
|
$otherSchool = School::factory()->create();
|
|
$otherStudent = Student::factory()->create([
|
|
'school_id' => $otherSchool->id,
|
|
]);
|
|
// Act & Assert
|
|
$this->actingAs($user);
|
|
get(route('students.index'))
|
|
->assertStatus(200)
|
|
->assertSeeText($student->name)
|
|
->assertDontSeeText($otherStudent->name);
|
|
|
|
// Assert
|
|
|
|
});
|
|
|
|
it('can accept a new student entry', function () {
|
|
// Act and Assert
|
|
$school = School::factory()->create();
|
|
$user = User::factory()->create([
|
|
'school_id' => $school->id,
|
|
]);
|
|
$this->actingAs($user);
|
|
post(route('students.store'), [
|
|
'first_name' => 'James',
|
|
'last_name' => 'Brown',
|
|
'grade' => 10,
|
|
])
|
|
->assertSessionHasNoErrors()
|
|
->assertRedirect(route('students.index'));
|
|
|
|
get(route('students.index'))
|
|
->assertSeeText('Brown, James');
|
|
|
|
expect(Student::count())->toBe(1);
|
|
|
|
});
|
|
|
|
it('cannot have two students with identical names at a school', function () {
|
|
$school = School::factory()->create();
|
|
$user = User::factory()->create([
|
|
'school_id' => $school->id,
|
|
]);
|
|
Student::factory()->create([
|
|
'school_id' => $school->id,
|
|
'first_name' => 'James',
|
|
'last_name' => 'Brown',
|
|
]);
|
|
$this->actingAs($user);
|
|
post(route('students.store'), [
|
|
'first_name' => 'James',
|
|
'last_name' => 'Brown',
|
|
'grade' => 10,
|
|
])
|
|
->assertSessionHasErrors('last_name');
|
|
|
|
});
|