AdminUserEdit Page Test - includes AdminUserUpdate

This commit is contained in:
Matt Young 2024-07-02 10:58:21 -05:00
parent e5699ed8d8
commit f2e68f7388
3 changed files with 183 additions and 13 deletions

View File

@ -2,8 +2,6 @@
namespace App\Http\Controllers\Admin; namespace App\Http\Controllers\Admin;
use App\Events\AuditionChange;
use App\Events\RoomJudgeChange;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Mail\NewUserPassword; use App\Mail\NewUserPassword;
use App\Models\School; use App\Models\School;
@ -18,28 +16,39 @@ class UserController extends Controller
{ {
public function index() public function index()
{ {
if (! Auth::user()->is_admin) abort(403); if (! Auth::user()->is_admin) {
abort(403);
}
$users = User::with('school')->orderBy('last_name')->orderBy('first_name')->get(); $users = User::with('school')->orderBy('last_name')->orderBy('first_name')->get();
return view('admin.users.index', ['users' => $users]); return view('admin.users.index', ['users' => $users]);
} }
public function edit(User $user) public function edit(User $user)
{ {
if (! Auth::user()->is_admin) abort(403); if (! Auth::user()->is_admin) {
abort(403);
}
$schools = School::orderBy('name')->get(); $schools = School::orderBy('name')->get();
return view('admin.users.edit', ['user' => $user, 'schools' => $schools]); return view('admin.users.edit', ['user' => $user, 'schools' => $schools]);
} }
public function create() public function create()
{ {
if (! Auth::user()->is_admin) abort(403); if (! Auth::user()->is_admin) {
abort(403);
}
$schools = School::orderBy('name')->get(); $schools = School::orderBy('name')->get();
return view('admin.users.create', ['schools' => $schools]); return view('admin.users.create', ['schools' => $schools]);
} }
public function update(Request $request, User $user) public function update(Request $request, User $user)
{ {
if (! Auth::user()->is_admin) abort(403); if (! Auth::user()->is_admin) {
abort(403);
}
request()->validate([ request()->validate([
'first_name' => ['required'], 'first_name' => ['required'],
@ -56,8 +65,9 @@ class UserController extends Controller
'email' => request('email'), 'email' => request('email'),
'cell_phone' => request('cell_phone'), 'cell_phone' => request('cell_phone'),
'judging_preference' => request('judging_preference'), 'judging_preference' => request('judging_preference'),
'school_id' => request('school_id') 'school_id' => request('school_id'),
]); ]);
return redirect('/admin/users'); return redirect('/admin/users');
} }
@ -83,7 +93,7 @@ class UserController extends Controller
if (! is_null(request('school_id'))) { if (! is_null(request('school_id'))) {
$request->validate([ $request->validate([
'school_id' => ['exists:schools,id'] 'school_id' => ['exists:schools,id'],
]); ]);
} }
$user->school_id = request('school_id'); $user->school_id = request('school_id');

View File

@ -1,7 +1,7 @@
<x-layout.app> <x-layout.app>
<x-card.card class="mx-auto max-w-lg"> <x-card.card class="mx-auto max-w-lg">
<x-card.heading>Edit User</x-card.heading> <x-card.heading>Edit User</x-card.heading>
<x-form.form method="PATCH" action="/admin/users/{{ $user->id }}"> <x-form.form method="PATCH" action="{{ route('admin.users.update', $user) }}">
<x-form.body-grid> <x-form.body-grid>
<x-form.field name="first_name" label_text="First Name" colspan="3" value="{{ $user->first_name }}" /> <x-form.field name="first_name" label_text="First Name" colspan="3" value="{{ $user->first_name }}" />
<x-form.field name="last_name" label_text="Last Name" colspan="3" value="{{ $user->last_name }}" /> <x-form.field name="last_name" label_text="Last Name" colspan="3" value="{{ $user->last_name }}" />

View File

@ -0,0 +1,160 @@
<?php
use App\Models\School;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use function Pest\Laravel\actingAs;
use function Pest\Laravel\get;
use function Pest\Laravel\patch;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->adminUser = User::factory()->admin()->create();
$this->nonAdminUser = User::factory()->create();
$this->tabUser = User::factory()->tab()->create();
$this->users = User::factory(3)->create();
$this->schools = [];
foreach ($this->users as $user) {
$school = School::factory()->create();
$user->school_id = $school->id;
$user->save();
}
});
it('only shows for an admin user', function () {
// Act & Assert
$checkRoute = 'admin.users.edit';
get(route($checkRoute, $this->users[0]))->assertRedirect(route('home'));
actingAs($this->adminUser);
get(route($checkRoute, $this->users[0]))->assertOk();
actingAs($this->nonAdminUser);
get(route($checkRoute, $this->users[0]))->assertRedirect(route('dashboard'));
});
it('submits a patch request', function () {
// Arrange
actingAs($this->adminUser);
// Act & Assert
$response = get(route('admin.users.edit', $this->users[0]));
$response->assertOk();
$response->assertSeeInOrder([
'form',
'method',
'POST',
'action=',
route('admin.users.update', $this->users[0]),
'/form',
]);
$response->assertSee('<input type="hidden" name="_method" value="PATCH">', false);
});
it('has all needed fields', function () {
// Arrange
actingAs($this->adminUser);
$fieldNames = [
'first_name',
'last_name',
'email',
'cell_phone',
'judging_preference',
];
// Act & Assert
$response = get(route('admin.users.edit', $this->users[0]));
$response->assertOk();
foreach ($fieldNames as $fieldName) {
$response->assertSeeInOrder([
'input',
'name=',
$fieldName,
'/',
]);
}
$response->assertSeeInOrder([
'select',
'name',
'school_id',
'/select',
]);
});
it('is prefilled with existing user data', function () {
// Arrange
actingAs($this->adminUser);
$valueChecks = [
'first_name' => $this->users[0]->first_name,
'last_name' => $this->users[0]->last_name,
'email' => $this->users[0]->email,
'cell_phone' => $this->users[0]->cell_phone,
'judging_preference' => $this->users[0]->judging_preference,
];
// Act & Assert
$response = get(route('admin.users.edit', $this->users[0]));
$response->assertOk();
foreach ($valueChecks as $check) {
$response->assertSeeInOrder(
[
'input',
'value=',
$check,
'/',
]
);
}
$response->assertSeeInOrder([
'option',
'value=',
$this->users[0]->school_id,
'selected',
'/option',
]);
});
it('has all schools in a dropdown', function () {
// Arrange
actingAs($this->adminUser);
// Act & Assert
$response = get(route('admin.users.edit', $this->users[0]));
$response->assertOk();
foreach ($this->schools as $school) {
$response->assertSeeInOrder([
'option',
'value=',
$school->id,
$school->name,
'/option',
]);
}
});
it('rejects a submission by a non administrator', function () {
// Arrange
actingAs($this->nonAdminUser);
// Act & Assert
$response = patch(route('admin.users.update', $this->users[0]), [
'first_name' => 'New First Name',
'last_name' => 'New Last Name',
]);
$response->assertRedirect(route('dashboard'));
});
it('allows an administrator to modify a user', function () {
// Arrange
$newSchool = School::factory()->create(['name' => 'New School']);
actingAs($this->adminUser);
$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' => $newSchool->id,
];
// Act
$response = patch(route('admin.users.update', $this->users[0]), $newData);
$response
->assertSessionHasNoErrors()
->assertRedirect(route('admin.users.index'));
$this->users[0]->refresh();
expect($this->users[0]->first_name)->toBe($newData['first_name'])
->and($this->users[0]->last_name)->toBe($newData['last_name'])
->and($this->users[0]->email)->toBe($newData['email'])
->and($this->users[0]->cell_phone)->toBe($newData['cell_phone'])
->and($this->users[0]->judging_preference)->toBe($newData['judging_preference'])
->and($this->users[0]->school->name)->toBe($newSchool->name);
});