Finish StudentsIndex Test

This commit is contained in:
Matt Young 2024-07-01 15:15:46 -05:00
parent 96e5896887
commit 25a6084e25
4 changed files with 103 additions and 10 deletions

View File

@ -23,7 +23,7 @@ class StudentController extends Controller
if (! Auth::user()->school_id) { if (! Auth::user()->school_id) {
return redirect()->route('dashboard'); return redirect()->route('dashboard');
} }
$students = Auth::user()->students()->with('entries')->get(); $students = Auth::user()->students()->withCount('entries')->get();
$auditions = Audition::all(); $auditions = Audition::all();
return view('students.index', ['students' => $students, 'auditions' => $auditions]); return view('students.index', ['students' => $students, 'auditions' => $auditions]);
@ -121,6 +121,6 @@ class StudentController extends Controller
} }
$student->delete(); $student->delete();
return redirect('/students'); return redirect(route('students.index'));
} }
} }

View File

@ -21,8 +21,7 @@ class StudentObserver
*/ */
public function updated(Student $student): void public function updated(Student $student): void
{ {
AuditionChange::dispatch(); //
EntryChange::dispatch();
} }
/** /**
@ -30,7 +29,7 @@ class StudentObserver
*/ */
public function deleted(Student $student): void public function deleted(Student $student): void
{ {
AuditionChange::dispatch(); //
} }
/** /**

View File

@ -6,7 +6,7 @@
<x-layout.page-section-container> <x-layout.page-section-container>
<x-layout.page-section> <x-layout.page-section>
<x-slot:section_name>Add Student</x-slot:section_name> <x-slot:section_name>Add Student</x-slot:section_name>
<x-form.form method="POST" action="/students" class="mb-6 mt-3"> <x-form.form method="POST" action="{{ route('students.store') }}" class="mb-6 mt-3">
<x-form.body-grid columns="8" class="max-w-full"> <x-form.body-grid columns="8" class="max-w-full">
<x-form.field name="first_name" label_text="First Name" colspan="3"/> <x-form.field name="first_name" label_text="First Name" colspan="3"/>
<x-form.field name="last_name" label_text="Last Name" colspan="3"/> <x-form.field name="last_name" label_text="Last Name" colspan="3"/>
@ -46,10 +46,10 @@
<tr> <tr>
<x-table.td first>{{ $student->full_name(true) }}</x-table.td> <x-table.td first>{{ $student->full_name(true) }}</x-table.td>
<x-table.td>{{ $student->grade }}</x-table.td> <x-table.td>{{ $student->grade }}</x-table.td>
<x-table.td>{{ $student->entries->count() }}</x-table.td> <x-table.td>{{ $student->entries_count }}</x-table.td>
<x-table.td for_button> <x-table.td for_button>
@if( $student->entries->count() > 0) @if( $student->entries_count === 0)
<form method="POST" action="/students/{{ $student->id }}" class="inline"> <form method="POST" action="{{ route('students.destroy',$student) }}" class="inline">
@csrf @csrf
@method('DELETE') @method('DELETE')
<x-table.button <x-table.button
@ -59,7 +59,7 @@
</form> </form>
| |
@endif @endif
<x-table.button href="/students/{{ $student->id }}/edit">Edit</x-table.button> <x-table.button href="{{ route('students.edit',$student) }}">Edit</x-table.button>
</x-table.td> </x-table.td>
</tr> </tr>
@endforeach @endforeach

View File

@ -1,10 +1,12 @@
<?php <?php
use App\Models\Entry;
use App\Models\School; use App\Models\School;
use App\Models\Student; use App\Models\Student;
use App\Models\User; use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\RefreshDatabase;
use function Pest\Laravel\delete;
use function Pest\Laravel\get; use function Pest\Laravel\get;
use function Pest\Laravel\post; use function Pest\Laravel\post;
@ -132,3 +134,95 @@ it('cannot have two students with identical names at a school', function () {
->assertSessionHasErrors('last_name'); ->assertSessionHasErrors('last_name');
}); });
it('shows a delete link only if the student has no entries', function () {
// Arrange
$school = School::factory()->create();
$user = User::factory()->create([
'school_id' => $school->id,
]);
$studentWithNoEntries = Student::factory()->create([
'school_id' => $school->id,
]);
$studentWithEntries = Student::factory()->create([
'school_id' => $school->id,
]);
Entry::factory()->create([
'student_id' => $studentWithEntries->id,
]);
// Act & Assert
$this->actingAs($user);
get(route('students.index'))
->assertSee(route('students.destroy', $studentWithNoEntries))
->assertDontSeeText(route('students.destroy', $studentWithEntries));
});
it('shows an edit link for each student', function () {
// Arrange
$school = School::factory()->create();
$user = User::factory()->create([
'school_id' => $school->id,
]);
$students = Student::factory(3)->create([
'school_id' => $school->id,
]);
// Act & Assert
foreach ($students as $student) {
$this->actingAs($user);
get(route('students.index'))
->assertSee(route('students.edit', $student));
}
});
it('deletes a student with no entries', function () {
// Arrange
$school = School::factory()->create();
$user = User::factory()->create([
'school_id' => $school->id,
]);
$student = Student::factory()->create([
'school_id' => $school->id,
'first_name' => 'John Jacob',
]);
// Act & Assert
$this->actingAs($user);
get(route('students.index'))
->assertSee('John Jacob');
expect($student->exists())->toBeTrue();
delete(route('students.destroy', $student))
->assertSessionHasNoErrors()
->assertRedirect(route('students.index'));
expect($student->exists())->toBeFalse();
get(route('students.index'))
->assertOk()
->assertDontSee('John Jacob');
});
it('will not delete a student from another school', function () {
// Arrange
$school = School::factory()->create();
$user = User::factory()->create([
'school_id' => $school->id,
]);
$student = Student::factory()->create([
'first_name' => 'John Jacob',
]);
// Act & Assert
expect($student->exists())->toBeTrue();
$this->actingAs($user);
get(route('students.index'))
->assertOk()
->assertDontSee('John Jacob');
delete(route('students.destroy', $student))
->assertStatus(403);
expect($student->exists())->toBeTrue();
});