From bb05a8ce1566869957b0920392920be8e7f9d943 Mon Sep 17 00:00:00 2001 From: MathewEm Date: Thu, 5 Dec 2024 10:09:36 -0330 Subject: [PATCH 1/7] update update update update --- tests/Feature/Filament/UserResourceTest.php | 62 +++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 tests/Feature/Filament/UserResourceTest.php diff --git a/tests/Feature/Filament/UserResourceTest.php b/tests/Feature/Filament/UserResourceTest.php new file mode 100644 index 00000000..e2cf9569 --- /dev/null +++ b/tests/Feature/Filament/UserResourceTest.php @@ -0,0 +1,62 @@ +create(); + + $response = $this->actingAs($user)->get($path); + $response->assertForbidden(); +})->with([ + '/librarium', + '/librarium/users' +]); + +test('An admin can access the librarium pages', function (string $path) { + $admin = User::factory()->create(); + $admin->assignRole('admin'); + + $response = $this->actingAs($admin)->get($path); + $response->assertOk(); +})->with([ + '/librarium', + '/librarium/users' +]); + +test('An admin cannot access user create page', function () { + $admin = User::factory()->create(); + $admin-> assignRole('admin'); + + $response = $this->actingAs($admin)->get('/librarium/users/create'); + $response->assertForbidden(); +}); + +test('An admin cannot call create method', function () { + $admin = User::factory()->create(); + $admin-> assignRole('admin'); + + $user = User::factory()->create(); + + $response = $this->actingAs($admin)->delete(route('filament.librarium.resources.users.create', $user)); + $response->assertStatus(405); +}); + +test('An admin can edit an existing user', function () { + $admin = User::factory()->create(); + $admin-> assignRole('admin'); + + $user = User::factory()->create(); + + livewire(UserResouce\Pages\EditUser::class, [ + 'user' => $user->getRouteKey(), + ])->assertFormSet([ + 'id' => $user->id, + 'first_name' => $user->first_name, + ]); +}); From 0e1769c5e31e354a2cedf54f4a21b60659fea21e Mon Sep 17 00:00:00 2001 From: MathewEm Date: Wed, 18 Dec 2024 11:44:43 -0330 Subject: [PATCH 2/7] update --- composer.json | 1 + composer.lock | 70 +++++++++- tests/Feature/Filament/UserResourceTest.php | 135 +++++++++++++++----- 3 files changed, 174 insertions(+), 32 deletions(-) diff --git a/composer.json b/composer.json index 6d13ecd3..2194ceac 100644 --- a/composer.json +++ b/composer.json @@ -55,6 +55,7 @@ "nunomaduro/collision": "^8.1.1", "pestphp/pest": "^3.0", "pestphp/pest-plugin-laravel": "^3.0", + "pestphp/pest-plugin-livewire": "^3.0", "spatie/laravel-ignition": "^2.7.0" }, "autoload": { diff --git a/composer.lock b/composer.lock index 2ba600d2..78a32154 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "a4418280f0fe717dd53bfba8f6dbe3c6", + "content-hash": "70d92576cafe2e6934fe71e5927f9662", "packages": [ { "name": "amphp/amp", @@ -14651,6 +14651,72 @@ ], "time": "2024-09-08T23:32:52+00:00" }, + { + "name": "pestphp/pest-plugin-livewire", + "version": "v3.0.0", + "source": { + "type": "git", + "url": "https://github.com/pestphp/pest-plugin-livewire.git", + "reference": "e2f2edb0a7d414d6837d87908a0e148256d3bf89" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/pestphp/pest-plugin-livewire/zipball/e2f2edb0a7d414d6837d87908a0e148256d3bf89", + "reference": "e2f2edb0a7d414d6837d87908a0e148256d3bf89", + "shasum": "" + }, + "require": { + "livewire/livewire": "^3.5.6", + "pestphp/pest": "^3.0.0", + "php": "^8.1" + }, + "require-dev": { + "orchestra/testbench": "^9.4.0", + "pestphp/pest-dev-tools": "^3.0.0" + }, + "type": "library", + "autoload": { + "files": [ + "src/Autoload.php" + ], + "psr-4": { + "Pest\\Livewire\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "The Pest Livewire Plugin", + "keywords": [ + "framework", + "livewire", + "pest", + "php", + "plugin", + "test", + "testing", + "unit" + ], + "support": { + "source": "https://github.com/pestphp/pest-plugin-livewire/tree/v3.0.0" + }, + "funding": [ + { + "url": "https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=66BYDWAT92N6L", + "type": "custom" + }, + { + "url": "https://github.com/nunomaduro", + "type": "github" + }, + { + "url": "https://www.patreon.com/nunomaduro", + "type": "patreon" + } + ], + "time": "2024-09-09T00:05:59+00:00" + }, { "name": "pestphp/pest-plugin-mutate", "version": "v3.0.5", @@ -16943,5 +17009,5 @@ "platform-overrides": { "php": "8.3" }, - "plugin-api-version": "2.6.0" + "plugin-api-version": "2.2.0" } diff --git a/tests/Feature/Filament/UserResourceTest.php b/tests/Feature/Filament/UserResourceTest.php index e2cf9569..1d8b1510 100644 --- a/tests/Feature/Filament/UserResourceTest.php +++ b/tests/Feature/Filament/UserResourceTest.php @@ -2,61 +2,136 @@ namespace Tests\Feature\Filament; +use App\Filament\Resources\UserResource\Pages\CreateUser; +use App\Filament\Resources\UserResource\Pages\EditUser; +use App\Filament\Resources\UserResource\Pages\ListUsers; use App\Models\User; use Tests\TestCase; -use Livewire\livewire; +use function Pest\Livewire\livewire; -test('An unauthorized user cannot access the librarium pages', function ($path) { + +test('An unauthorized user cannot render the index page', function () { $user = User::factory()->create(); - $response = $this->actingAs($user)->get($path); - $response->assertForbidden(); -})->with([ - '/librarium', - '/librarium/users' -]); + $this->actingAs($user)->get(ListUsers::getUrl())->assertForbidden(); +}); + +test('An unauthorized user cannot render the create page', function () { + $user = User::factory()->create(); + + $this->actingAs($user)->get(CreateUser::getUrl())->assertForbidden(); +}); + +test('An unauthorized user cannot render the edit page', function () { + $user = User::factory()->create(); + $record = User::factory()->create(); -test('An admin can access the librarium pages', function (string $path) { + $this->actingAs($user)->livewire(EditUser::class, ['record' => $record->getRouteKey()])->assertForbidden(); +}); + +test('An admin can render the index page', function () { $admin = User::factory()->create(); $admin->assignRole('admin'); - $response = $this->actingAs($admin)->get($path); - $response->assertOk(); -})->with([ - '/librarium', - '/librarium/users' -]); + $this->actingAs($admin)->get(ListUsers::getUrl())->assertOk(); +}); -test('An admin cannot access user create page', function () { +test('An admin cannot render the user create page', function () { $admin = User::factory()->create(); $admin-> assignRole('admin'); - $response = $this->actingAs($admin)->get('/librarium/users/create'); - $response->assertForbidden(); + $this->actingAs($admin)->get(CreateUser::getUrl())->assertForbidden(); }); -test('An admin cannot call create method', function () { +test('User Table has column', function (string $column) { $admin = User::factory()->create(); $admin-> assignRole('admin'); + + $this->actingAs($admin)->livewire(ListUsers::class)->assertTableColumnExists($column); +})->with([ + 'first_name', + 'last_name', + 'email', + 'email_verified_at', + 'active', + 'roles.name' +]); - $user = User::factory()->create(); +test('Admin can render the User Table column', function ($column) { + $admin = User::factory()->create(); + $admin-> assignRole('admin'); + + $this->actingAs($admin)->livewire(ListUsers::class)->assertCanRenderTableColumn($column); +})->with([ + 'first_name', + 'last_name', + 'email', + 'email_verified_at', + 'active', + 'roles.name' +]); - $response = $this->actingAs($admin)->delete(route('filament.librarium.resources.users.create', $user)); - $response->assertStatus(405); -}); +test('Admin can sort the User Table column', function ($column) { + $admin = User::factory()->create(); + $admin-> assignRole('admin'); + $records = User::factory(5)->create(); + + $this->actingAs($admin)->livewire(ListUsers::class) + ->sortTable($column) + ->assertCanSeeTableRecords($records->sortBy($column), inOrder: true) + ->sortTable($column, 'desc') + ->assertCanSeeTableRecords($records->sortByDesc($column), inOrder: true); +})->with([ + 'first_name', + 'last_name', + 'email_verified_at', + 'active', +]); + +test('Admin can search the User Table column', function (string $column) { + $admin = User::factory()->create(); + $admin-> assignRole('admin'); + $records = User::factory(5)->create(); + + $value = $records->first()->{$column}; + + $this->actingAs($admin)->livewire(ListUsers::class) + ->searchTable($value) + ->assertCanSeeTableRecords($records->where($column, $value)) + ->assertCanNotSeeTableRecords($records->where($column, '!=', $value)); +})->with([ + 'first_name', + 'last_name', + 'email', + 'roles.name', +]); test('An admin can edit an existing user', function () { $admin = User::factory()->create(); $admin-> assignRole('admin'); - $user = User::factory()->create(); + $record = User::factory()->create(); + $newRecord = User::factory()->make(); + + $this->actingAs($admin)->livewire(Edituser::class, ['record' => $record->getRouteKey()]) + ->fillForm([ + 'email' => $newRecord->email, + ]) + ->assertActionExists('save') + ->call('save') + ->assertHasNoFormErrors(); + + $this->actingAs($admin)->assertDatabaseHas(User::class, [ + 'email' => $newRecord->email, + ]); +}); + +test('An admin cannot render a delete button', function () { + $admin = User::factory()->create(); + $admin-> assignRole('admin'); - livewire(UserResouce\Pages\EditUser::class, [ - 'user' => $user->getRouteKey(), - ])->assertFormSet([ - 'id' => $user->id, - 'first_name' => $user->first_name, - ]); + $this->actingAs($admin)->get(ListUsers::getUrl()) + ->assertDontSee('Delete'); }); From df5c47002e597a98b271022ce4a3f14f9919362e Mon Sep 17 00:00:00 2001 From: MathewEm Date: Wed, 18 Dec 2024 15:46:25 -0330 Subject: [PATCH 3/7] feat(tests): added tests for Librarium UserResources --- tests/Feature/Filament/UserResourceTest.php | 252 ++++++++++---------- 1 file changed, 131 insertions(+), 121 deletions(-) diff --git a/tests/Feature/Filament/UserResourceTest.php b/tests/Feature/Filament/UserResourceTest.php index 1d8b1510..d8bea181 100644 --- a/tests/Feature/Filament/UserResourceTest.php +++ b/tests/Feature/Filament/UserResourceTest.php @@ -6,132 +6,142 @@ use App\Filament\Resources\UserResource\Pages\EditUser; use App\Filament\Resources\UserResource\Pages\ListUsers; use App\Models\User; -use Tests\TestCase; -use function Pest\Livewire\livewire; - - - -test('An unauthorized user cannot render the index page', function () { - $user = User::factory()->create(); - - $this->actingAs($user)->get(ListUsers::getUrl())->assertForbidden(); -}); - -test('An unauthorized user cannot render the create page', function () { - $user = User::factory()->create(); - - $this->actingAs($user)->get(CreateUser::getUrl())->assertForbidden(); -}); - -test('An unauthorized user cannot render the edit page', function () { - $user = User::factory()->create(); - $record = User::factory()->create(); - - $this->actingAs($user)->livewire(EditUser::class, ['record' => $record->getRouteKey()])->assertForbidden(); -}); - -test('An admin can render the index page', function () { - $admin = User::factory()->create(); - $admin->assignRole('admin'); - - $this->actingAs($admin)->get(ListUsers::getUrl())->assertOk(); -}); +describe('page authorization', function () { + it('An unauthorized user cannot render the index page', function () { + $user = User::factory()->create(); + + $this->actingAs($user)->get(ListUsers::getUrl())->assertForbidden(); + }); + + it('An unauthorized user cannot render the create page', function () { + $user = User::factory()->create(); + + $this->actingAs($user)->get(CreateUser::getUrl())->assertForbidden(); + }); + + it('An unauthorized user cannot render the edit page', function () { + $user = User::factory()->create(); + $record = User::factory()->create(); + + $this->actingAs($user)->livewire(EditUser::class, ['record' => $record->getRouteKey()])->assertForbidden(); + }); + + it('An admin can render the index page', function () { + $admin = User::factory()->create(); + $admin->assignRole('admin'); + + $this->actingAs($admin)->get(ListUsers::getUrl())->assertOk(); + }); + + it('An admin cannot render the user create page', function () { + $admin = User::factory()->create(); + $admin->assignRole('admin'); + + $this->actingAs($admin)->get(CreateUser::getUrl())->assertForbidden(); + }); +})->todo(note: <<<'NOTE' + render logout button + logout path successful + NOTE); + +describe('list users table', function () { + it('User Table has column', function (string $column) { + $admin = User::factory()->create(); + $admin->assignRole('admin'); + + $this->actingAs($admin)->livewire(ListUsers::class)->assertTableColumnExists($column); + })->with([ + 'first_name', + 'last_name', + 'email', + 'email_verified_at', + 'active', + 'roles.name', + ]); -test('An admin cannot render the user create page', function () { - $admin = User::factory()->create(); - $admin-> assignRole('admin'); + it('Admin can render the User Table column', function ($column) { + $admin = User::factory()->create(); + $admin->assignRole('admin'); + + $this->actingAs($admin)->livewire(ListUsers::class)->assertCanRenderTableColumn($column); + })->with([ + 'first_name', + 'last_name', + 'email', + 'email_verified_at', + 'active', + 'roles.name', + ]); - $this->actingAs($admin)->get(CreateUser::getUrl())->assertForbidden(); -}); + it('Admin can sort the User Table column', function ($column) { + $admin = User::factory()->create(); + $admin->assignRole('admin'); + $records = User::factory(5)->create(); + + $this->actingAs($admin)->livewire(ListUsers::class) + ->sortTable($column) + ->assertCanSeeTableRecords($records->sortBy($column), inOrder: true) + ->sortTable($column, 'desc') + ->assertCanSeeTableRecords($records->sortByDesc($column), inOrder: true); + })->with([ + 'first_name', + 'last_name', + 'email_verified_at', + 'active', + ]); -test('User Table has column', function (string $column) { - $admin = User::factory()->create(); - $admin-> assignRole('admin'); - - $this->actingAs($admin)->livewire(ListUsers::class)->assertTableColumnExists($column); -})->with([ - 'first_name', - 'last_name', - 'email', - 'email_verified_at', - 'active', - 'roles.name' -]); - -test('Admin can render the User Table column', function ($column) { - $admin = User::factory()->create(); - $admin-> assignRole('admin'); - - $this->actingAs($admin)->livewire(ListUsers::class)->assertCanRenderTableColumn($column); -})->with([ - 'first_name', - 'last_name', - 'email', - 'email_verified_at', - 'active', - 'roles.name' -]); - -test('Admin can sort the User Table column', function ($column) { - $admin = User::factory()->create(); - $admin-> assignRole('admin'); - $records = User::factory(5)->create(); - - $this->actingAs($admin)->livewire(ListUsers::class) - ->sortTable($column) - ->assertCanSeeTableRecords($records->sortBy($column), inOrder: true) - ->sortTable($column, 'desc') - ->assertCanSeeTableRecords($records->sortByDesc($column), inOrder: true); -})->with([ - 'first_name', - 'last_name', - 'email_verified_at', - 'active', -]); - -test('Admin can search the User Table column', function (string $column) { - $admin = User::factory()->create(); - $admin-> assignRole('admin'); - $records = User::factory(5)->create(); - - $value = $records->first()->{$column}; - - $this->actingAs($admin)->livewire(ListUsers::class) - ->searchTable($value) - ->assertCanSeeTableRecords($records->where($column, $value)) - ->assertCanNotSeeTableRecords($records->where($column, '!=', $value)); -})->with([ - 'first_name', - 'last_name', - 'email', - 'roles.name', -]); - -test('An admin can edit an existing user', function () { - $admin = User::factory()->create(); - $admin-> assignRole('admin'); - - $record = User::factory()->create(); - $newRecord = User::factory()->make(); - - $this->actingAs($admin)->livewire(Edituser::class, ['record' => $record->getRouteKey()]) - ->fillForm([ - 'email' => $newRecord->email, - ]) - ->assertActionExists('save') - ->call('save') - ->assertHasNoFormErrors(); - - $this->actingAs($admin)->assertDatabaseHas(User::class, [ - 'email' => $newRecord->email, + it('Admin can search the User Table column', function (string $column) { + $admin = User::factory()->create(); + $admin->assignRole('admin'); + $records = User::factory(5)->create(); + + $value = $records->first()->{$column}; + + $this->actingAs($admin)->livewire(ListUsers::class) + ->searchTable($value) + ->assertCanSeeTableRecords($records->where($column, $value)) + ->assertCanNotSeeTableRecords($records->where($column, '!=', $value)); + })->with([ + 'first_name', + 'last_name', + 'email', + 'roles.name', ]); -}); -test('An admin cannot render a delete button', function () { - $admin = User::factory()->create(); - $admin-> assignRole('admin'); + it('An admin cannot render a delete user button', function () { + $admin = User::factory()->create(); + $admin->assignRole('admin'); - $this->actingAs($admin)->get(ListUsers::getUrl()) - ->assertDontSee('Delete'); + $this->actingAs($admin)->get(ListUsers::getUrl()) + ->assertDontSee('Delete'); + }); }); + +describe('edit user', function () { + + it('An admin can edit an existing user', function () { + $admin = User::factory()->create(); + $admin->assignRole('admin'); + + $record = User::factory()->create(); + $newRecord = User::factory()->make(); + + $this->actingAs($admin)->livewire(Edituser::class, ['record' => $record->getRouteKey()]) + ->fillForm([ + 'email' => $newRecord->email, + ]) + ->assertActionExists('save') + ->call('save') + ->assertHasNoFormErrors(); + + $this->actingAs($admin)->assertDatabaseHas(User::class, [ + 'email' => $newRecord->email, + ]); + })->todo(note: <<<'NOTE' + Add attributes: roles +NOTE); +})->todo(issue: 857, + note: <<<'NOTE' + validate inputs + NOTE); From 8d8bc80ad458b4f7a6a91f6f7381aaeaed59e28e Mon Sep 17 00:00:00 2001 From: Vincent Auger Date: Thu, 19 Dec 2024 09:31:56 -0400 Subject: [PATCH 4/7] refactor: optimize tests --- tests/Feature/Filament/UserResourceTest.php | 100 ++++++++++---------- 1 file changed, 52 insertions(+), 48 deletions(-) diff --git a/tests/Feature/Filament/UserResourceTest.php b/tests/Feature/Filament/UserResourceTest.php index d8bea181..5070a321 100644 --- a/tests/Feature/Filament/UserResourceTest.php +++ b/tests/Feature/Filament/UserResourceTest.php @@ -46,68 +46,72 @@ NOTE); describe('list users table', function () { - it('User Table has column', function (string $column) { + it('User Table has required columns', function () { $admin = User::factory()->create(); $admin->assignRole('admin'); - $this->actingAs($admin)->livewire(ListUsers::class)->assertTableColumnExists($column); - })->with([ - 'first_name', - 'last_name', - 'email', - 'email_verified_at', - 'active', - 'roles.name', - ]); - - it('Admin can render the User Table column', function ($column) { + collect([ + 'first_name', + 'last_name', + 'email', + 'email_verified_at', + 'active', + 'roles.name', + ])->each(fn ($column) => $this->actingAs($admin)->livewire(ListUsers::class)->assertTableColumnExists($column)); + }); + + it('Admin can render the User Table column', function () { $admin = User::factory()->create(); $admin->assignRole('admin'); - $this->actingAs($admin)->livewire(ListUsers::class)->assertCanRenderTableColumn($column); - })->with([ - 'first_name', - 'last_name', - 'email', - 'email_verified_at', - 'active', - 'roles.name', - ]); - - it('Admin can sort the User Table column', function ($column) { + collect([ + 'first_name', + 'last_name', + 'email', + 'email_verified_at', + 'active', + 'roles.name', + ])->each(fn ($column) => $this->actingAs($admin)->livewire(ListUsers::class)->assertCanRenderTableColumn($column)); + }); + + it('Admin can sort the User Table column', function () { $admin = User::factory()->create(); $admin->assignRole('admin'); $records = User::factory(5)->create(); - $this->actingAs($admin)->livewire(ListUsers::class) - ->sortTable($column) - ->assertCanSeeTableRecords($records->sortBy($column), inOrder: true) - ->sortTable($column, 'desc') - ->assertCanSeeTableRecords($records->sortByDesc($column), inOrder: true); - })->with([ - 'first_name', - 'last_name', - 'email_verified_at', - 'active', - ]); - - it('Admin can search the User Table column', function (string $column) { + collect([ + 'first_name', + 'last_name', + 'email_verified_at', + 'active', + ])->each(function ($column) use ($admin, $records) { + $this->actingAs($admin)->livewire(ListUsers::class) + ->sortTable($column) + ->assertCanSeeTableRecords($records->sortBy($column), inOrder: true) + ->sortTable($column, 'desc') + ->assertCanSeeTableRecords($records->sortByDesc($column), inOrder: true); + }); + }); + + it('Admin can search the User Table column', function () { $admin = User::factory()->create(); $admin->assignRole('admin'); $records = User::factory(5)->create(); - $value = $records->first()->{$column}; - - $this->actingAs($admin)->livewire(ListUsers::class) - ->searchTable($value) - ->assertCanSeeTableRecords($records->where($column, $value)) - ->assertCanNotSeeTableRecords($records->where($column, '!=', $value)); - })->with([ - 'first_name', - 'last_name', - 'email', - 'roles.name', - ]); + collect([ + 'first_name', + 'last_name', + 'email', + 'roles.name', + ])->each(function ($column) use ($admin, $records) { + $value = $records->first()->{$column}; + + $this->actingAs($admin)->livewire(ListUsers::class) + ->searchTable($value) + ->assertCanSeeTableRecords($records->where($column, $value)) + ->assertCanNotSeeTableRecords($records->where($column, '!=', $value)); + }); + }); it('An admin cannot render a delete user button', function () { $admin = User::factory()->create(); From 69fc3aca2309019b75a95873fac70448842f0c84 Mon Sep 17 00:00:00 2001 From: Vincent Auger Date: Thu, 19 Dec 2024 09:53:57 -0400 Subject: [PATCH 5/7] refactor fun --- tests/Feature/Filament/UserResourceTest.php | 33 +++++++++++---------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/tests/Feature/Filament/UserResourceTest.php b/tests/Feature/Filament/UserResourceTest.php index 5070a321..d3fd360a 100644 --- a/tests/Feature/Filament/UserResourceTest.php +++ b/tests/Feature/Filament/UserResourceTest.php @@ -8,17 +8,15 @@ use App\Models\User; describe('page authorization', function () { - it('An unauthorized user cannot render the index page', function () { - $user = User::factory()->create(); - - $this->actingAs($user)->get(ListUsers::getUrl())->assertForbidden(); - }); - it('An unauthorized user cannot render the create page', function () { + it('An unauthorized user cannot render the create page', function ($url) { $user = User::factory()->create(); - $this->actingAs($user)->get(CreateUser::getUrl())->assertForbidden(); - }); + $this->actingAs($user)->get($url)->assertForbidden(); + })->with([ + 'ListUsers' => [fn() => ListUsers::getUrl()], + 'CreateUser' => [fn() => CreateUser::getUrl()] + ]); it('An unauthorized user cannot render the edit page', function () { $user = User::factory()->create(); @@ -40,10 +38,11 @@ $this->actingAs($admin)->get(CreateUser::getUrl())->assertForbidden(); }); -})->todo(note: <<<'NOTE' - render logout button - logout path successful - NOTE); + + it('render logout button', function(){})->todo(); + it('logout path successful', function(){})->todo(); +}); + describe('list users table', function () { it('User Table has required columns', function () { @@ -57,7 +56,7 @@ 'email_verified_at', 'active', 'roles.name', - ])->each(fn ($column) => $this->actingAs($admin)->livewire(ListUsers::class)->assertTableColumnExists($column)); + ])->each(fn($column) => $this->actingAs($admin)->livewire(ListUsers::class)->assertTableColumnExists($column)); }); it('Admin can render the User Table column', function () { @@ -71,7 +70,7 @@ 'email_verified_at', 'active', 'roles.name', - ])->each(fn ($column) => $this->actingAs($admin)->livewire(ListUsers::class)->assertCanRenderTableColumn($column)); + ])->each(fn($column) => $this->actingAs($admin)->livewire(ListUsers::class)->assertCanRenderTableColumn($column)); }); it('Admin can sort the User Table column', function () { @@ -145,7 +144,9 @@ })->todo(note: <<<'NOTE' Add attributes: roles NOTE); -})->todo(issue: 857, +})->todo( + issue: 857, note: <<<'NOTE' validate inputs - NOTE); + NOTE +); From 10dd455ba4f9262baef286520c262b2652b49816 Mon Sep 17 00:00:00 2001 From: Vincent Auger Date: Thu, 19 Dec 2024 09:54:09 -0400 Subject: [PATCH 6/7] refactor fun --- tests/Feature/Filament/UserResourceTest.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/tests/Feature/Filament/UserResourceTest.php b/tests/Feature/Filament/UserResourceTest.php index d3fd360a..fc683ab1 100644 --- a/tests/Feature/Filament/UserResourceTest.php +++ b/tests/Feature/Filament/UserResourceTest.php @@ -14,8 +14,8 @@ $this->actingAs($user)->get($url)->assertForbidden(); })->with([ - 'ListUsers' => [fn() => ListUsers::getUrl()], - 'CreateUser' => [fn() => CreateUser::getUrl()] + 'ListUsers' => [fn () => ListUsers::getUrl()], + 'CreateUser' => [fn () => CreateUser::getUrl()], ]); it('An unauthorized user cannot render the edit page', function () { @@ -39,11 +39,10 @@ $this->actingAs($admin)->get(CreateUser::getUrl())->assertForbidden(); }); - it('render logout button', function(){})->todo(); - it('logout path successful', function(){})->todo(); + it('render logout button', function () {})->todo(); + it('logout path successful', function () {})->todo(); }); - describe('list users table', function () { it('User Table has required columns', function () { $admin = User::factory()->create(); @@ -56,7 +55,7 @@ 'email_verified_at', 'active', 'roles.name', - ])->each(fn($column) => $this->actingAs($admin)->livewire(ListUsers::class)->assertTableColumnExists($column)); + ])->each(fn ($column) => $this->actingAs($admin)->livewire(ListUsers::class)->assertTableColumnExists($column)); }); it('Admin can render the User Table column', function () { @@ -70,7 +69,7 @@ 'email_verified_at', 'active', 'roles.name', - ])->each(fn($column) => $this->actingAs($admin)->livewire(ListUsers::class)->assertCanRenderTableColumn($column)); + ])->each(fn ($column) => $this->actingAs($admin)->livewire(ListUsers::class)->assertCanRenderTableColumn($column)); }); it('Admin can sort the User Table column', function () { From 3cd81bba3cc171ca0328ebf2a7837ead8213c9ea Mon Sep 17 00:00:00 2001 From: MathewEm Date: Thu, 19 Dec 2024 12:16:29 -0330 Subject: [PATCH 7/7] refactor: cleaned up test functions --- tests/Feature/Filament/UserResourceTest.php | 151 ------------------- tests/Feature/Librarium/UserResourceTest.php | 135 +++++++++++++++++ 2 files changed, 135 insertions(+), 151 deletions(-) delete mode 100644 tests/Feature/Filament/UserResourceTest.php create mode 100644 tests/Feature/Librarium/UserResourceTest.php diff --git a/tests/Feature/Filament/UserResourceTest.php b/tests/Feature/Filament/UserResourceTest.php deleted file mode 100644 index fc683ab1..00000000 --- a/tests/Feature/Filament/UserResourceTest.php +++ /dev/null @@ -1,151 +0,0 @@ -create(); - - $this->actingAs($user)->get($url)->assertForbidden(); - })->with([ - 'ListUsers' => [fn () => ListUsers::getUrl()], - 'CreateUser' => [fn () => CreateUser::getUrl()], - ]); - - it('An unauthorized user cannot render the edit page', function () { - $user = User::factory()->create(); - $record = User::factory()->create(); - - $this->actingAs($user)->livewire(EditUser::class, ['record' => $record->getRouteKey()])->assertForbidden(); - }); - - it('An admin can render the index page', function () { - $admin = User::factory()->create(); - $admin->assignRole('admin'); - - $this->actingAs($admin)->get(ListUsers::getUrl())->assertOk(); - }); - - it('An admin cannot render the user create page', function () { - $admin = User::factory()->create(); - $admin->assignRole('admin'); - - $this->actingAs($admin)->get(CreateUser::getUrl())->assertForbidden(); - }); - - it('render logout button', function () {})->todo(); - it('logout path successful', function () {})->todo(); -}); - -describe('list users table', function () { - it('User Table has required columns', function () { - $admin = User::factory()->create(); - $admin->assignRole('admin'); - - collect([ - 'first_name', - 'last_name', - 'email', - 'email_verified_at', - 'active', - 'roles.name', - ])->each(fn ($column) => $this->actingAs($admin)->livewire(ListUsers::class)->assertTableColumnExists($column)); - }); - - it('Admin can render the User Table column', function () { - $admin = User::factory()->create(); - $admin->assignRole('admin'); - - collect([ - 'first_name', - 'last_name', - 'email', - 'email_verified_at', - 'active', - 'roles.name', - ])->each(fn ($column) => $this->actingAs($admin)->livewire(ListUsers::class)->assertCanRenderTableColumn($column)); - }); - - it('Admin can sort the User Table column', function () { - $admin = User::factory()->create(); - $admin->assignRole('admin'); - $records = User::factory(5)->create(); - - collect([ - 'first_name', - 'last_name', - 'email_verified_at', - 'active', - ])->each(function ($column) use ($admin, $records) { - $this->actingAs($admin)->livewire(ListUsers::class) - ->sortTable($column) - ->assertCanSeeTableRecords($records->sortBy($column), inOrder: true) - ->sortTable($column, 'desc') - ->assertCanSeeTableRecords($records->sortByDesc($column), inOrder: true); - }); - }); - - it('Admin can search the User Table column', function () { - $admin = User::factory()->create(); - $admin->assignRole('admin'); - $records = User::factory(5)->create(); - - collect([ - 'first_name', - 'last_name', - 'email', - 'roles.name', - ])->each(function ($column) use ($admin, $records) { - $value = $records->first()->{$column}; - - $this->actingAs($admin)->livewire(ListUsers::class) - ->searchTable($value) - ->assertCanSeeTableRecords($records->where($column, $value)) - ->assertCanNotSeeTableRecords($records->where($column, '!=', $value)); - }); - }); - - it('An admin cannot render a delete user button', function () { - $admin = User::factory()->create(); - $admin->assignRole('admin'); - - $this->actingAs($admin)->get(ListUsers::getUrl()) - ->assertDontSee('Delete'); - }); -}); - -describe('edit user', function () { - - it('An admin can edit an existing user', function () { - $admin = User::factory()->create(); - $admin->assignRole('admin'); - - $record = User::factory()->create(); - $newRecord = User::factory()->make(); - - $this->actingAs($admin)->livewire(Edituser::class, ['record' => $record->getRouteKey()]) - ->fillForm([ - 'email' => $newRecord->email, - ]) - ->assertActionExists('save') - ->call('save') - ->assertHasNoFormErrors(); - - $this->actingAs($admin)->assertDatabaseHas(User::class, [ - 'email' => $newRecord->email, - ]); - })->todo(note: <<<'NOTE' - Add attributes: roles -NOTE); -})->todo( - issue: 857, - note: <<<'NOTE' - validate inputs - NOTE -); diff --git a/tests/Feature/Librarium/UserResourceTest.php b/tests/Feature/Librarium/UserResourceTest.php new file mode 100644 index 00000000..4f03fa09 --- /dev/null +++ b/tests/Feature/Librarium/UserResourceTest.php @@ -0,0 +1,135 @@ +record = User::factory()->create(); + $this->user = User::factory()->create(); + $this->admin = User::factory()->create(); + $this->admin->assignRole('admin'); + }); + + it('An unauthorized user cannot render the page', function ($url) { + $this->actingAs($this->user)->get($url)->assertForbidden(); + })->with([ + 'ListUsers' => [fn () => ListUsers::getUrl()], + 'CreateUser' => [fn () => CreateUser::getUrl()], + ]); + + it('An unauthorized user cannot render the edit page', function () { + $this->actingAs($this->user)->livewire(EditUser::class, ['record' => $this->record->getRouteKey()])->assertForbidden(); + }); + + it('Can render the index page', function () { + $this->actingAs($this->admin)->get(ListUsers::getUrl())->assertOk(); + }); + + it('Cannot render the user create page', function () { + $this->actingAs($this->admin)->get(CreateUser::getUrl())->assertForbidden(); + }); + + it('Can render logout button', function () {})->todo(); + it('Can logout path successful', function () {})->todo(); +}); + +describe('List Users table features', function () { + + beforeEach(function () { + $this->records = User::factory(5)->create(); + $this->admin = User::factory()->create(); + $this->admin->assignRole('admin'); + }); + + it('Columns are displayed', function () { + collect([ + 'first_name', + 'last_name', + 'email', + 'email_verified_at', + 'active', + 'roles.name', + ])->each(fn ($column) => $this->actingAs($this->admin)->livewire(ListUsers::class) + ->assertTableColumnExists($column) + ->assertCanRenderTableColumn($column)); + }); + + it('Columns can be sorted', function () { + collect([ + 'first_name', + 'last_name', + 'email_verified_at', + 'active', + ])->each(fn ($column) => $this->actingAs($this->admin)->livewire(ListUsers::class) + ->sortTable($column) + ->assertCanSeeTableRecords($this->records->sortBy($column), inOrder: true) + ->sortTable($column, 'desc') + ->assertCanSeeTableRecords($this->records->sortByDesc($column), inOrder: true)); + }); + + it('Columns can be searched', function () { + collect([ + 'first_name', + 'last_name', + 'email', + 'roles.name', + ])->each(function ($column) { + $value = $this->records->first()->{$column}; + + $this->actingAs($this->admin)->livewire(ListUsers::class) + ->searchTable($value) + ->assertCanSeeTableRecords($this->records->where($column, $value)) + ->assertCanNotSeeTableRecords($this->records->where($column, '!=', $value)); + }); + }); + + it('Delete User button cannot be rendered', function () { + $this->actingAs($this->admin)->get(ListUsers::getUrl()) + ->assertDontSee('Delete'); + }); +}); + +describe('Edit User page features', function () { + + beforeEach(function () { + $this->record = User::factory()->create(); + $this->admin = User::factory()->create(); + $this->admin->assignRole('admin'); + }); + + it('Delete User button cannot be rendered', function () { + $this->actingAs($this->admin)->get(ListUsers::getUrl()) + ->assertDontSee('Delete'); + }); + + it('Can edit an existing user', function () { + $this->actingAs($this->admin)->livewire(Edituser::class, ['record' => $this->record->getRouteKey()]) + ->fillForm([ + 'email' => "{$this->record->first_name}.{$this->record->last_name}@dfo-mpo.gc.ca", + ]) + ->assertActionExists('save') + ->call('save') + ->assertHasNoFormErrors(); + + $this->actingAs($this->admin)->assertDatabaseHas(User::class, [ + 'email' => "{$this->record->first_name}.{$this->record->last_name}@dfo-mpo.gc.ca", + ]); + })->todo(note: <<<'NOTE' + Add attributes: roles +NOTE); + + it('Cannot save an invalid field', function () { + // + })->todo( + issue: 857, + note: <<<'NOTE' + validate inputs + NOTE + ); +});