From 95cf05f0d60d4e59ce663a49b46c6a6551296af4 Mon Sep 17 00:00:00 2001 From: Matt Young Date: Sun, 4 Aug 2024 15:51:36 -0500 Subject: [PATCH] Fix issue with edit user form Allow setting no school for a user from the admin edit user form. Closes #59 --- app/Http/Controllers/Admin/UserController.php | 3 +-- resources/views/admin/users/edit.blade.php | 1 + tests/Feature/Pages/Admin/UsersEditTest.php | 26 +++++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/Admin/UserController.php b/app/Http/Controllers/Admin/UserController.php index 961089e..dec1523 100644 --- a/app/Http/Controllers/Admin/UserController.php +++ b/app/Http/Controllers/Admin/UserController.php @@ -56,11 +56,10 @@ class UserController extends Controller 'email' => ['required', 'email'], 'cell_phone' => ['required'], 'judging_preference' => ['required'], - 'school_id' => ['required', 'exists:schools,id'], + 'school_id' => ['nullable', 'exists:schools,id'], ]); $validData['is_admin'] = $request->get('is_admin') == 'on' ? 1 : 0; $validData['is_tab'] = $request->get('is_tab') == 'on' ? 1 : 0; - $user->update([ 'first_name' => $validData['first_name'], 'last_name' => $validData['last_name'], diff --git a/resources/views/admin/users/edit.blade.php b/resources/views/admin/users/edit.blade.php index 5ba71e6..f0afd81 100644 --- a/resources/views/admin/users/edit.blade.php +++ b/resources/views/admin/users/edit.blade.php @@ -24,6 +24,7 @@ value="{{ $user->judging_preference }}"/> School + @foreach ($schools as $school) diff --git a/tests/Feature/Pages/Admin/UsersEditTest.php b/tests/Feature/Pages/Admin/UsersEditTest.php index ffb3722..260a368 100644 --- a/tests/Feature/Pages/Admin/UsersEditTest.php +++ b/tests/Feature/Pages/Admin/UsersEditTest.php @@ -5,6 +5,7 @@ use App\Models\User; use Illuminate\Foundation\Testing\RefreshDatabase; use function Pest\Laravel\actingAs; +use function Pest\Laravel\assertDatabaseHas; use function Pest\Laravel\delete; use function Pest\Laravel\get; use function Pest\Laravel\patch; @@ -171,6 +172,31 @@ it('allows an administrator to modify a user', function () { ->assertSee($newData['judging_preference']) ->assertSee($newSchool->name); }); +it('allows a users school to be set to no school', function () { + // Arrange + $school = School::factory()->create(); + $user = User::factory()->create(['school_id' => $school->id]); + $newData = [ + 'first_name' => 'New First Name', + 'last_name' => 'New Last Name', + 'email' => 'new@emailllllll.com', + 'cell_phone' => '123-456-7890', + 'judging_preference' => 'New Judging Preference', + 'school_id' => '', + ]; + actAsAdmin(); + // Act & Assert + $response = patch(route('admin.users.update', $user), $newData); + /** @noinspection PhpUnhandledExceptionInspection */ + $response + ->assertSessionHasNoErrors() + ->assertRedirect(route('admin.users.index')); + // Assert DB has user id with null school + assertDatabaseHas('users', [ + 'id' => $user->id, + 'school_id' => null, + ]); +}); it('has a delete link for the user if not the current user', function () { // Arrange actingAs($this->adminUser);