171 lines
5.2 KiB
PHP
171 lines
5.2 KiB
PHP
<?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);
|
|
/** @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);
|
|
});
|