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('', false); }); it('has all needed fields', function () { // Arrange actingAs($this->adminUser); $fieldNames = [ 'first_name', 'last_name', 'email', 'cell_phone', 'judging_preference', 'is_admin', 'is_tab', ]; // 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); /** @noinspection PhpUnhandledExceptionInspection */ $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); get(route('admin.users.index')) ->assertOk() ->assertSee($newData['first_name']) ->assertSee($newData['last_name']) ->assertSee($newData['email']) ->assertSee($newData['cell_phone']) ->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); // Act & Assert $response = get(route('admin.users.edit', $this->users[0])); $response->assertOk(); $response->assertSee(route('admin.users.destroy', $this->users[0])); $response->assertSee('', false); }); it('does not show a delete link for the current user', function () { // Arrange actingAs($this->adminUser); // Act & Assert $response = get(route('admin.users.edit', $this->adminUser)); $response->assertOk(); $response->assertDontSee('', false); }); it('allows an administrator to destroy a user', function () { // Arrange $newUser = User::factory()->create(); actingAs($this->adminUser); // Act & Assert assert($newUser->exists()); $response = delete(route('admin.users.destroy', $newUser)); /** @noinspection PhpUnhandledExceptionInspection */ $response ->assertSessionHasNoErrors() ->assertSessionHas('success', 'User deleted successfully') ->assertRedirect(route('admin.users.index')); expect(User::find($newUser->id))->toBeNull(); });