diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ac115ea..28fa120 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -121,7 +121,7 @@ jobs: with: level: 9 php_version: "8.2" - path: src/ tests/Unit/ + path: config/ database/ resources/ src/ tests/Feature/ tests/Unit/ - name: Stopping timer if: ${{ !cancelled() }} id: timer_end diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index 971f185..1b9f8a8 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -217,8 +217,9 @@ __DIR__.'/database', // __DIR__.'/lang', // __DIR__.'/routes', + __DIR__.'/resources', __DIR__.'/src', - // __DIR__.'/tests/Feature', + __DIR__.'/tests/Feature', __DIR__.'/tests/Unit', ]) ->name('*.php') diff --git a/README.md b/README.md index ea6e4d3..b013757 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,7 @@ [![Playground CI Workflow](https://github.com/gammamatrix/playground-test/actions/workflows/ci.yml/badge.svg?branch=develop)](https://raw.githubusercontent.com/gammamatrix/playground-test/testing/develop/testdox.txt) [![Test Coverage](https://raw.githubusercontent.com/gammamatrix/playground-test/testing/develop/coverage.svg)](tests) +[![PHPStan Level 9 src and tests](https://img.shields.io/badge/PHPStan-level%209-brightgreen)](.github/workflows/ci.yml#L120) The Playground Test package. diff --git a/composer.json b/composer.json index c10988f..b607a5a 100644 --- a/composer.json +++ b/composer.json @@ -20,6 +20,7 @@ "php": "^8.1", "fakerphp/faker": "^1.9.1", "friendsofphp/php-cs-fixer": "^3.41", + "gammamatrix/playground": "dev-develop|dev-master|dev-feature/*|^73.0", "larastan/larastan": "^2.0", "orchestra/testbench": "8.*", "phpstan/phpstan-phpunit": "^1.3", diff --git a/database/factories/AbstractUserFactory.php b/database/factories/AbstractUserFactory.php index 773e26e..f042192 100644 --- a/database/factories/AbstractUserFactory.php +++ b/database/factories/AbstractUserFactory.php @@ -22,6 +22,11 @@ abstract class AbstractUserFactory extends Factory */ protected $model = AbstractUser::class; + /** + * The current password being used by the factory. + */ + protected static ?string $password = null; + /** * Define the model's default state. * @@ -29,15 +34,27 @@ abstract class AbstractUserFactory extends Factory */ public function definition(): array { - $password = config('playground-test.password', 'password'); + if (empty(static::$password)) { + $password = config('auth.testing.password'); + $test_password_hashed = config('auth.testing.hashed'); + + if (empty($password) || ! is_string($password)) { + $password = md5(Carbon::now()->format('c')); + $test_password_hashed = false; + } + + if (! $test_password_hashed) { + $password = Hash::make($password); + } + + static::$password = $password; + } return [ 'name' => fake()->name(), 'email' => fake()->unique()->safeEmail(), 'email_verified_at' => Carbon::now()->format('Y-m-d H:i:s'), - 'password' => Hash::make( - $password && is_string($password) ? $password : md5(date('c')) - ), + 'password' => static::$password, 'remember_token' => Str::random(10), ]; } diff --git a/database/factories/PlaygroundUserFactory.php b/database/factories/PlaygroundUserFactory.php new file mode 100644 index 0000000..2a6a2a8 --- /dev/null +++ b/database/factories/PlaygroundUserFactory.php @@ -0,0 +1,112 @@ + + */ + protected $model = PlaygroundUser::class; + + /** + * The current password being used by the factory. + */ + protected static ?string $password = null; + + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + if (empty(static::$password)) { + $password = config('auth.testing.password'); + $test_password_hashed = config('auth.testing.hashed'); + + if (empty($password) || ! is_string($password)) { + $password = md5(Carbon::now()->format('c')); + $test_password_hashed = false; + } + + if (! $test_password_hashed) { + $password = Hash::make($password); + } + + static::$password = $password; + } + + return [ + 'name' => fake()->name(), + 'email' => fake()->unique()->safeEmail(), + 'email_verified_at' => Carbon::now()->format('Y-m-d H:i:s'), + 'role' => 'user', + 'password' => static::$password, + 'remember_token' => Str::random(10), + ]; + } + + /** + * Indicate that the model's email address should be unverified. + */ + public function unverified(): static + { + return $this->state(fn (array $attributes) => [ + 'email_verified_at' => null, + ]); + } + + /** + * Indicate that the user has the admin role. + */ + public function admin(): Factory + { + return $this->state(fn (array $attributes) => [ + 'role' => 'admin', + ]); + } + + /** + * Indicate that the user has the manager role. + */ + public function manager(): Factory + { + return $this->state(fn (array $attributes) => [ + 'role' => 'manager', + ]); + } + + /** + * Indicate that the user has the wheel role. + */ + public function wheel(): Factory + { + return $this->state(fn (array $attributes) => [ + 'role' => 'wheel', + ]); + } + + /** + * Indicate that the user has the root role. + */ + public function root(): Factory + { + return $this->state(fn (array $attributes) => [ + 'role' => 'root', + ]); + } +} diff --git a/database/factories/PlaygroundUserWithSanctumFactory.php b/database/factories/PlaygroundUserWithSanctumFactory.php new file mode 100644 index 0000000..6b012ba --- /dev/null +++ b/database/factories/PlaygroundUserWithSanctumFactory.php @@ -0,0 +1,112 @@ + + */ + protected $model = PlaygroundUserWithSanctum::class; + + /** + * The current password being used by the factory. + */ + protected static ?string $password = null; + + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + if (empty(static::$password)) { + $password = config('auth.testing.password'); + $test_password_hashed = config('auth.testing.hashed'); + + if (empty($password) || ! is_string($password)) { + $password = md5(Carbon::now()->format('c')); + $test_password_hashed = false; + } + + if (! $test_password_hashed) { + $password = Hash::make($password); + } + + static::$password = $password; + } + + return [ + 'name' => fake()->name(), + 'email' => fake()->unique()->safeEmail(), + 'email_verified_at' => Carbon::now()->format('Y-m-d H:i:s'), + 'role' => 'user', + 'password' => static::$password, + 'remember_token' => Str::random(10), + ]; + } + + /** + * Indicate that the model's email address should be unverified. + */ + public function unverified(): static + { + return $this->state(fn (array $attributes) => [ + 'email_verified_at' => null, + ]); + } + + /** + * Indicate that the user has the admin role. + */ + public function admin(): Factory + { + return $this->state(fn (array $attributes) => [ + 'role' => 'admin', + ]); + } + + /** + * Indicate that the user has the manager role. + */ + public function manager(): Factory + { + return $this->state(fn (array $attributes) => [ + 'role' => 'manager', + ]); + } + + /** + * Indicate that the user has the wheel role. + */ + public function wheel(): Factory + { + return $this->state(fn (array $attributes) => [ + 'role' => 'wheel', + ]); + } + + /** + * Indicate that the user has the root role. + */ + public function root(): Factory + { + return $this->state(fn (array $attributes) => [ + 'role' => 'root', + ]); + } +} diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index 34ff510..bc8059f 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -4,6 +4,7 @@ */ namespace Database\Factories\Playground\Test\Models; +use Illuminate\Database\Eloquent\Factories\Factory; use Playground\Test\Models\User; /** @@ -17,4 +18,34 @@ class UserFactory extends AbstractUserFactory * @var class-string */ protected $model = User::class; + + /** + * Set the user up as an admin user. + */ + public function admin(): Factory + { + return $this->state(fn (array $attributes) => [ + 'email' => 'admin@example.dev', + ]); + } + + /** + * Set the user up as a guest user. + */ + public function guest(): Factory + { + return $this->state(fn (array $attributes) => [ + 'email' => 'guest@example.dev', + ]); + } + + /** + * Set the user up as a manager user. + */ + public function manager(): Factory + { + return $this->state(fn (array $attributes) => [ + 'email' => 'manager@example.dev', + ]); + } } diff --git a/database/factories/UserWithRoleAndPrivilegesFactory.php b/database/factories/UserWithRoleAndPrivilegesFactory.php index a2bc420..ff9f287 100644 --- a/database/factories/UserWithRoleAndPrivilegesFactory.php +++ b/database/factories/UserWithRoleAndPrivilegesFactory.php @@ -4,6 +4,7 @@ */ namespace Database\Factories\Playground\Test\Models; +use Illuminate\Database\Eloquent\Factories\Factory; use Playground\Test\Models\UserWithRoleAndPrivileges; /** @@ -17,4 +18,44 @@ class UserWithRoleAndPrivilegesFactory extends AbstractUserFactory * @var class-string */ protected $model = UserWithRoleAndPrivileges::class; + + /** + * Indicate that the user has the admin role. + */ + public function admin(): Factory + { + return $this->state(fn (array $attributes) => [ + 'role' => 'admin', + ]); + } + + /** + * Indicate that the user has the manager role. + */ + public function manager(): Factory + { + return $this->state(fn (array $attributes) => [ + 'role' => 'manager', + ]); + } + + /** + * Indicate that the user has the wheel role. + */ + public function wheel(): Factory + { + return $this->state(fn (array $attributes) => [ + 'role' => 'wheel', + ]); + } + + /** + * Indicate that the user has the root role. + */ + public function root(): Factory + { + return $this->state(fn (array $attributes) => [ + 'role' => 'root', + ]); + } } diff --git a/database/factories/UserWithRoleAndRolesAndPrivilegesFactory.php b/database/factories/UserWithRoleAndRolesAndPrivilegesFactory.php index 9084d98..a62bcf1 100644 --- a/database/factories/UserWithRoleAndRolesAndPrivilegesFactory.php +++ b/database/factories/UserWithRoleAndRolesAndPrivilegesFactory.php @@ -4,6 +4,7 @@ */ namespace Database\Factories\Playground\Test\Models; +use Illuminate\Database\Eloquent\Factories\Factory; use Playground\Test\Models\UserWithRoleAndRolesAndPrivileges; /** @@ -17,4 +18,44 @@ class UserWithRoleAndRolesAndPrivilegesFactory extends AbstractUserFactory * @var class-string */ protected $model = UserWithRoleAndRolesAndPrivileges::class; + + /** + * Indicate that the user has the admin role. + */ + public function admin(): Factory + { + return $this->state(fn (array $attributes) => [ + 'role' => 'admin', + ]); + } + + /** + * Indicate that the user has the manager role. + */ + public function manager(): Factory + { + return $this->state(fn (array $attributes) => [ + 'role' => 'manager', + ]); + } + + /** + * Indicate that the user has the wheel role. + */ + public function wheel(): Factory + { + return $this->state(fn (array $attributes) => [ + 'role' => 'wheel', + ]); + } + + /** + * Indicate that the user has the root role. + */ + public function root(): Factory + { + return $this->state(fn (array $attributes) => [ + 'role' => 'root', + ]); + } } diff --git a/database/factories/UserWithRoleAndRolesFactory.php b/database/factories/UserWithRoleAndRolesFactory.php index f179c82..a215250 100644 --- a/database/factories/UserWithRoleAndRolesFactory.php +++ b/database/factories/UserWithRoleAndRolesFactory.php @@ -4,6 +4,7 @@ */ namespace Database\Factories\Playground\Test\Models; +use Illuminate\Database\Eloquent\Factories\Factory; use Playground\Test\Models\UserWithRoleAndRoles; /** @@ -17,4 +18,44 @@ class UserWithRoleAndRolesFactory extends AbstractUserFactory * @var class-string */ protected $model = UserWithRoleAndRoles::class; + + /** + * Indicate that the user has the admin role. + */ + public function admin(): Factory + { + return $this->state(fn (array $attributes) => [ + 'role' => 'admin', + ]); + } + + /** + * Indicate that the user has the manager role. + */ + public function manager(): Factory + { + return $this->state(fn (array $attributes) => [ + 'role' => 'manager', + ]); + } + + /** + * Indicate that the user has the wheel role. + */ + public function wheel(): Factory + { + return $this->state(fn (array $attributes) => [ + 'role' => 'wheel', + ]); + } + + /** + * Indicate that the user has the root role. + */ + public function root(): Factory + { + return $this->state(fn (array $attributes) => [ + 'role' => 'root', + ]); + } } diff --git a/database/factories/UserWithRoleFactory.php b/database/factories/UserWithRoleFactory.php index 946d769..f854a52 100644 --- a/database/factories/UserWithRoleFactory.php +++ b/database/factories/UserWithRoleFactory.php @@ -4,6 +4,7 @@ */ namespace Database\Factories\Playground\Test\Models; +use Illuminate\Database\Eloquent\Factories\Factory; use Playground\Test\Models\UserWithRole; /** @@ -18,10 +19,24 @@ class UserWithRoleFactory extends AbstractUserFactory */ protected $model = UserWithRole::class; + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + $definition = parent::definition(); + + $definition['role'] = 'user'; + + return $definition; + } + /** * Indicate that the user has the admin role. */ - public function admin(): static + public function admin(): Factory { return $this->state(fn (array $attributes) => [ 'role' => 'admin', @@ -31,7 +46,7 @@ public function admin(): static /** * Indicate that the user has the manager role. */ - public function manager(): static + public function manager(): Factory { return $this->state(fn (array $attributes) => [ 'role' => 'manager', @@ -41,7 +56,7 @@ public function manager(): static /** * Indicate that the user has the wheel role. */ - public function wheel(): static + public function wheel(): Factory { return $this->state(fn (array $attributes) => [ 'role' => 'wheel', @@ -51,7 +66,7 @@ public function wheel(): static /** * Indicate that the user has the root role. */ - public function root(): static + public function root(): Factory { return $this->state(fn (array $attributes) => [ 'role' => 'root', diff --git a/database/factories/UserWithSanctumFactory.php b/database/factories/UserWithSanctumFactory.php index 855491d..4e92010 100644 --- a/database/factories/UserWithSanctumFactory.php +++ b/database/factories/UserWithSanctumFactory.php @@ -4,6 +4,7 @@ */ namespace Database\Factories\Playground\Test\Models; +use Illuminate\Database\Eloquent\Factories\Factory; use Playground\Test\Models\UserWithSanctum; /** @@ -17,4 +18,34 @@ class UserWithSanctumFactory extends AbstractUserFactory * @var class-string */ protected $model = UserWithSanctum::class; + + /** + * Set the user up as an admin user. + */ + public function admin(): Factory + { + return $this->state(fn (array $attributes) => [ + 'email' => 'admin@example.dev', + ]); + } + + /** + * Set the user up as a guest user. + */ + public function guest(): Factory + { + return $this->state(fn (array $attributes) => [ + 'email' => 'guest@example.dev', + ]); + } + + /** + * Set the user up as a manager user. + */ + public function manager(): Factory + { + return $this->state(fn (array $attributes) => [ + 'email' => 'manager@example.dev', + ]); + } } diff --git a/resources/CustomUsersTableSeeder.php b/resources/CustomUsersTableSeeder.php index f1352ea..d7c833d 100644 --- a/resources/CustomUsersTableSeeder.php +++ b/resources/CustomUsersTableSeeder.php @@ -1,15 +1,13 @@ empty($meta['name']) || !is_string($meta['name']) ? 'Some Name' : $meta['name'], - 'description' => empty($meta['description']) || !is_string($meta['description']) ? '' : $meta['description'], + 'name' => empty($meta['name']) || ! is_string($meta['name']) ? 'Some Name' : $meta['name'], + 'description' => empty($meta['description']) || ! is_string($meta['description']) ? '' : $meta['description'], 'active' => true, 'email' => $email, - 'role' => empty($meta['role']) || !is_string($meta['role']) ? '' : $meta['role'], - 'status' => empty($meta['status']) || !is_numeric($meta['status']) ? 0 : $meta['status'], + 'role' => empty($meta['role']) || ! is_string($meta['role']) ? '' : $meta['role'], + 'status' => empty($meta['status']) || ! is_numeric($meta['status']) ? 0 : $meta['status'], ]); } else { $model->update([ - 'name' => empty($meta['name']) || !is_string($meta['name']) ? 'Some Name' : $meta['name'], - 'description' => empty($meta['description']) || !is_string($meta['description']) ? '' : $meta['description'], + 'name' => empty($meta['name']) || ! is_string($meta['name']) ? 'Some Name' : $meta['name'], + 'description' => empty($meta['description']) || ! is_string($meta['description']) ? '' : $meta['description'], 'active' => true, - 'role' => empty($meta['role']) || !is_string($meta['role']) ? '' : $meta['role'], - 'status' => empty($meta['status']) || !is_numeric($meta['status']) ? 0 : $meta['status'], + 'role' => empty($meta['role']) || ! is_string($meta['role']) ? '' : $meta['role'], + 'status' => empty($meta['status']) || ! is_numeric($meta['status']) ? 0 : $meta['status'], ]); } @@ -73,9 +71,9 @@ public function run() if (is_array($meta['roles'])) { foreach ($meta['roles'] as $role) { - if (!empty($role) + if (! empty($role) && is_string($role) - && !in_array($role, $roles) + && ! in_array($role, $roles) && $role !== $model->role ) { $roles[] = $role; diff --git a/resources/DatabaseSeeder.php b/resources/DatabaseSeeder.php index eb18e5d..9c8db31 100644 --- a/resources/DatabaseSeeder.php +++ b/resources/DatabaseSeeder.php @@ -1,7 +1,7 @@ get($url); - $response->assertRedirect(route('login')); + // $response->dump(); + $response->assertStatus(403); + // $response->assertRedirect(route('login')); } - public function test_create_view_rendered_by_user_with_return_url() + public function test_create_view_rendered_by_admin_with_return_url() { - $user = User::factory()->create(); + $user = User::factory()->admin()->create(); $index = route($this->packageInfo['model_route']); @@ -47,9 +52,9 @@ public function test_create_view_rendered_by_user_with_return_url() ), false); } - public function test_create_info_with_user_using_json() + public function test_create_info_with_admin_using_json() { - $user = User::factory()->create(); + $user = User::factory()->admin()->create(); $url = route(sprintf( '%1$s.create', @@ -68,9 +73,9 @@ public function test_create_info_with_user_using_json() $this->assertAuthenticated(); } - public function test_create_view_rendered_by_user_with_invalid_parameter() + public function test_create_view_rendered_by_admin_with_invalid_parameter() { - $user = User::factory()->create(); + $user = User::factory()->admin()->create(); $url = route(sprintf( '%1$s.create', diff --git a/src/Feature/Http/Controllers/Resource/DestroyTrait.php b/src/Feature/Http/Controllers/Resource/Playground/DestroyTrait.php similarity index 80% rename from src/Feature/Http/Controllers/Resource/DestroyTrait.php rename to src/Feature/Http/Controllers/Resource/Playground/DestroyTrait.php index 00f2d0f..de1a105 100644 --- a/src/Feature/Http/Controllers/Resource/DestroyTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/DestroyTrait.php @@ -2,27 +2,26 @@ /** * Playground */ -namespace Playground\Test\Feature\Http\Controllers\Resource; +namespace Playground\Test\Feature\Http\Controllers\Resource\Playground; -use Playground\Test\Models\User; -use Playground\Test\Models\UserWithRole; +use Playground\Test\Models\PlaygroundUser as User; /** - * \Playground\Test\Feature\Http\Controllers\Resource\DestroyTrait + * \Playground\Test\Feature\Http\Controllers\Resource\Playground\DestroyTrait */ trait DestroyTrait { public function test_guest_cannot_destroy() { - config([ - // 'playground.auth.token.name' => 'app', - 'playground.auth.verify' => 'user', - 'playground.auth.userRole' => false, - 'playground.auth.hasRole' => false, - 'playground.auth.userRoles' => false, - 'playground.auth.hasPrivilege' => false, - 'playground.auth.userPrivileges' => false, - ]); + // config([ + // // 'playground-auth.token.name' => 'app', + // 'playground-auth.verify' => 'user', + // 'playground-auth.userRole' => false, + // 'playground-auth.hasRole' => false, + // 'playground-auth.userRoles' => false, + // 'playground-auth.hasPrivilege' => false, + // 'playground-auth.userPrivileges' => false, + // ]); $fqdn = $this->fqdn; @@ -45,7 +44,8 @@ public function test_guest_cannot_destroy() // $response->dump(); - $response->assertRedirect(route('login')); + $response->assertStatus(403); + // $response->assertRedirect(route('login')); $this->assertDatabaseHas($this->packageInfo['table'], [ 'id' => $model->id, @@ -54,11 +54,11 @@ public function test_guest_cannot_destroy() ]); } - public function test_destroy_as_standard_user_and_succeed() + public function test_destroy_as_admin_user_and_succeed() { $fqdn = $this->fqdn; - $user = User::factory()->create(); + $user = User::factory()->admin()->create(); $model = $fqdn::factory()->create([ 'owned_by_id' => $user->id, @@ -97,11 +97,11 @@ public function test_destroy_as_standard_user_and_succeed() $response->assertRedirect(route($this->packageInfo['model_route'])); } - public function test_destroy_as_standard_user_and_succeed_with_force_delete() + public function test_destroy_as_admin_user_and_succeed_with_force_delete() { $fqdn = $this->fqdn; - $user = User::factory()->create(); + $user = User::factory()->admin()->create(); $model = $fqdn::factory()->create([ 'owned_by_id' => $user->id, @@ -135,11 +135,11 @@ public function test_destroy_as_standard_user_and_succeed_with_force_delete() $response->assertRedirect(route($this->packageInfo['model_route'])); } - public function test_destroy_as_standard_user_using_json_and_succeed_with_no_content() + public function test_destroy_as_admin_user_using_json_and_succeed_with_no_content() { $fqdn = $this->fqdn; - $user = User::factory()->create(); + $user = User::factory()->admin()->create(); $model = $fqdn::factory()->create([ 'owned_by_id' => $user->id, @@ -178,11 +178,11 @@ public function test_destroy_as_standard_user_using_json_and_succeed_with_no_con $response->assertNoContent(); } - public function test_destroy_as_standard_user_and_succeed_with_redirect_to_index_with_trash() + public function test_destroy_as_admin_user_and_succeed_with_redirect_to_index_with_trash() { $fqdn = $this->fqdn; - $user = User::factory()->create(); + $user = User::factory()->admin()->create(); $model = $fqdn::factory()->create([ 'owned_by_id' => $user->id, @@ -236,16 +236,16 @@ public function test_destroy_as_standard_user_and_succeed_with_redirect_to_index public function test_destroy_with_user_role_and_get_denied_and_no_force_delete_allowed() { - config([ - 'playground.auth.verify' => 'roles', - 'playground.auth.userRole' => true, - 'playground.auth.hasRole' => true, - 'playground.auth.userRoles' => false, - ]); + // config([ + // 'playground-auth.verify' => 'roles', + // 'playground-auth.userRole' => true, + // 'playground-auth.hasRole' => true, + // 'playground-auth.userRoles' => false, + // ]); $fqdn = $this->fqdn; - $user = UserWithRole::find(User::factory()->create()->id); + $user = User::factory()->create(); $model = $fqdn::factory()->create([ 'owned_by_id' => $user->id, @@ -283,19 +283,16 @@ public function test_destroy_with_user_role_and_get_denied_and_no_force_delete_a public function test_destroy_with_admin_role_and_succeed() { - config([ - 'playground.auth.verify' => 'roles', - 'playground.auth.userRole' => true, - 'playground.auth.hasRole' => true, - 'playground.auth.userRoles' => false, - ]); + // config([ + // 'playground-auth.verify' => 'roles', + // 'playground-auth.userRole' => true, + // 'playground-auth.hasRole' => true, + // 'playground-auth.userRoles' => false, + // ]); $fqdn = $this->fqdn; - $user = UserWithRole::find(User::factory()->create()->id); - - // The role is not saved since the column may not exist. - $user->role = 'admin'; + $user = User::factory()->admin()->create(); $model = $fqdn::factory()->create([ 'owned_by_id' => $user->id, @@ -336,19 +333,16 @@ public function test_destroy_with_admin_role_and_succeed() public function test_destroy_with_admin_role_and_succeed_with_force_delete() { - config([ - 'playground.auth.verify' => 'roles', - 'playground.auth.userRole' => true, - 'playground.auth.hasRole' => true, - 'playground.auth.userRoles' => false, - ]); + // config([ + // 'playground-auth.verify' => 'roles', + // 'playground-auth.userRole' => true, + // 'playground-auth.hasRole' => true, + // 'playground-auth.userRoles' => false, + // ]); $fqdn = $this->fqdn; - $user = UserWithRole::find(User::factory()->create()->id); - - // The role is not saved since the column may not exist. - $user->role = 'admin'; + $user = User::factory()->admin()->create(); $model = $fqdn::factory()->create([ 'owned_by_id' => $user->id, diff --git a/src/Feature/Http/Controllers/Resource/EditTrait.php b/src/Feature/Http/Controllers/Resource/Playground/EditTrait.php similarity index 71% rename from src/Feature/Http/Controllers/Resource/EditTrait.php rename to src/Feature/Http/Controllers/Resource/Playground/EditTrait.php index 9676b75..7bba013 100644 --- a/src/Feature/Http/Controllers/Resource/EditTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/EditTrait.php @@ -2,12 +2,12 @@ /** * Playground */ -namespace Playground\Test\Feature\Http\Controllers\Resource; +namespace Playground\Test\Feature\Http\Controllers\Resource\Playground; -use Playground\Test\Models\User; +use Playground\Test\Models\PlaygroundUser as User; /** - * \Playground\Test\Feature\Http\Controllers\Resource\EditTrait + * \Playground\Test\Feature\Http\Controllers\Resource\Playground\EditTrait */ trait EditTrait { @@ -25,16 +25,17 @@ public function test_guest_cannot_render_edit_view() ]); $response = $this->get($url); - $response->assertRedirect(route('login')); + // $response->assertRedirect(route('login')); + $response->assertStatus(403); } - public function test_edit_view_rendered_by_user_with_return_url() + public function test_edit_as_admin_view_rendered_by_user_with_return_url() { $fqdn = $this->fqdn; $model = $fqdn::factory()->create(); - $user = User::factory()->create(); + $user = User::factory()->admin()->create(); $index = route($this->packageInfo['model_route']); @@ -45,12 +46,17 @@ public function test_edit_view_rendered_by_user_with_return_url() $this->packageInfo['model_slug'] => $model->id, '_return_url' => $index, ]); - + // dump([ + // '__METHOD__' => __METHOD__, + // '$url' => $url, + // '$model' => $model->toArray(), + // '$user' => $user->toArray(), + // ]); $response = $this->actingAs($user)->get($url); - $response->assertStatus(200); - $this->assertAuthenticated(); + // $response->dump(); + $response->assertStatus(200); $response->assertSee(sprintf( '', @@ -58,13 +64,13 @@ public function test_edit_view_rendered_by_user_with_return_url() ), false); } - public function test_edit_info_with_user_using_json() + public function test_edit_as_admin_info_with_user_using_json() { $fqdn = $this->fqdn; $model = $fqdn::factory()->create(); - $user = User::factory()->create(); + $user = User::factory()->admin()->create(); $url = route(sprintf( '%1$s.edit', @@ -85,13 +91,13 @@ public function test_edit_info_with_user_using_json() $this->assertAuthenticated(); } - public function test_edit_view_rendered_by_user_with_invalid_parameter() + public function test_edit_as_admin_view_rendered_by_user_with_invalid_parameter() { $fqdn = $this->fqdn; $model = $fqdn::factory()->create(); - $user = User::factory()->create(); + $user = User::factory()->admin()->create(); $url = route(sprintf( '%1$s.edit', diff --git a/src/Feature/Http/Controllers/Resource/IndexTrait.php b/src/Feature/Http/Controllers/Resource/Playground/IndexTrait.php similarity index 70% rename from src/Feature/Http/Controllers/Resource/IndexTrait.php rename to src/Feature/Http/Controllers/Resource/Playground/IndexTrait.php index 10fc492..93c0887 100644 --- a/src/Feature/Http/Controllers/Resource/IndexTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/IndexTrait.php @@ -2,12 +2,12 @@ /** * Playground */ -namespace Playground\Test\Feature\Http\Controllers\Resource; +namespace Playground\Test\Feature\Http\Controllers\Resource\Playground; -use Playground\Test\Models\UserWithSanctum; +use Playground\Test\Models\PlaygroundUser as User; /** - * \Playground\Test\Feature\Http\Controllers\Resource\IndexTrait + * \Playground\Test\Feature\Http\Controllers\Resource\Playground\IndexTrait */ trait IndexTrait { @@ -22,16 +22,17 @@ public function test_guest_cannot_render_index_view() ]); $response = $this->get($url); - $response->assertRedirect(route('login')); + $response->assertStatus(403); + // $response->assertRedirect(route('login')); } - public function test_index_view_rendered_by_user() + public function test_index_view_rendered_by_admin() { $fqdn = $this->fqdn; $model = $fqdn::factory()->create(); - $user = UserWithSanctum::factory()->create(); + $user = User::factory()->admin()->create(); $url = route($this->packageInfo['model_route'], [ $this->packageInfo['model_slug'] => $model->id, @@ -44,13 +45,13 @@ public function test_index_view_rendered_by_user() $this->assertAuthenticated(); } - public function test_index_info_with_user_using_json() + public function test_index_as_admin_using_json() { $fqdn = $this->fqdn; $model = $fqdn::factory()->create(); - $user = UserWithSanctum::factory()->create(); + $user = User::factory()->admin()->create(); $url = route($this->packageInfo['model_route'], [ $this->packageInfo['model_slug'] => $model->id, diff --git a/src/Feature/Http/Controllers/Resource/LockTrait.php b/src/Feature/Http/Controllers/Resource/Playground/LockTrait.php similarity index 77% rename from src/Feature/Http/Controllers/Resource/LockTrait.php rename to src/Feature/Http/Controllers/Resource/Playground/LockTrait.php index 20164c8..a0038e2 100644 --- a/src/Feature/Http/Controllers/Resource/LockTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/LockTrait.php @@ -2,27 +2,26 @@ /** * Playground */ -namespace Playground\Test\Feature\Http\Controllers\Resource; +namespace Playground\Test\Feature\Http\Controllers\Resource\Playground; -use Playground\Test\Models\User; -use Playground\Test\Models\UserWithRole; +use Playground\Test\Models\PlaygroundUser as User; /** - * \Playground\Test\Feature\Http\Controllers\Resource\LockTrait + * \Playground\Test\Feature\Http\Controllers\Resource\Playground\LockTrait */ trait LockTrait { public function test_guest_cannot_lock() { - config([ - // 'playground.auth.token.name' => 'app', - 'playground.auth.verify' => 'user', - 'playground.auth.userRole' => false, - 'playground.auth.hasRole' => false, - 'playground.auth.userRoles' => false, - 'playground.auth.hasPrivilege' => false, - 'playground.auth.userPrivileges' => false, - ]); + // config([ + // // 'playground-auth.token.name' => 'app', + // 'playground-auth.verify' => 'user', + // 'playground-auth.userRole' => false, + // 'playground-auth.hasRole' => false, + // 'playground-auth.userRoles' => false, + // 'playground-auth.hasPrivilege' => false, + // 'playground-auth.userPrivileges' => false, + // ]); $fqdn = $this->fqdn; @@ -45,7 +44,8 @@ public function test_guest_cannot_lock() // $response->dump(); - $response->assertRedirect(route('login')); + // $response->assertRedirect(route('login')); + $response->assertStatus(403); $this->assertDatabaseHas($this->packageInfo['table'], [ 'id' => $model->id, @@ -54,11 +54,11 @@ public function test_guest_cannot_lock() ]); } - public function test_lock_as_standard_user_and_succeed() + public function test_lock_as_admin_user_and_succeed() { $fqdn = $this->fqdn; - $user = User::factory()->create(); + $user = User::factory()->admin()->create(); $model = $fqdn::factory()->create([ 'owned_by_id' => $user->id, @@ -95,11 +95,11 @@ public function test_lock_as_standard_user_and_succeed() ])); } - public function test_lock_as_standard_user_using_json_and_succeed() + public function test_lock_as_admin_user_using_json_and_succeed() { $fqdn = $this->fqdn; - $user = User::factory()->create(); + $user = User::factory()->admin()->create(); $model = $fqdn::factory()->create([ 'owned_by_id' => $user->id, @@ -134,11 +134,11 @@ public function test_lock_as_standard_user_using_json_and_succeed() $response->assertStatus(200); } - public function test_lock_as_standard_user_and_succeed_with_redirect_to_index_with_sorted_by_locked_desc() + public function test_lock_as_admin_user_and_succeed_with_redirect_to_index_with_sorted_by_locked_desc() { $fqdn = $this->fqdn; - $user = User::factory()->create(); + $user = User::factory()->admin()->create(); $model = $fqdn::factory()->create([ 'owned_by_id' => $user->id, @@ -186,16 +186,17 @@ public function test_lock_as_standard_user_and_succeed_with_redirect_to_index_wi public function test_lock_with_user_role_and_get_denied() { - config([ - 'playground.auth.verify' => 'roles', - 'playground.auth.userRole' => true, - 'playground.auth.hasRole' => true, - 'playground.auth.userRoles' => false, - ]); + // config([ + // 'playground-auth.verify' => 'roles', + // 'playground-auth.userRole' => true, + // 'playground-auth.hasRole' => true, + // 'playground-auth.userRoles' => false, + // ]); $fqdn = $this->fqdn; - $user = UserWithRole::find(User::factory()->create()->id); + $user = User::factory()->create(); + // $user = User::factory()->admin()->create(); $model = $fqdn::factory()->create([ 'owned_by_id' => $user->id, @@ -232,19 +233,16 @@ public function test_lock_with_user_role_and_get_denied() public function test_lock_with_admin_role_and_succeed() { - config([ - 'playground.auth.verify' => 'roles', - 'playground.auth.userRole' => true, - 'playground.auth.hasRole' => true, - 'playground.auth.userRoles' => false, - ]); + // config([ + // 'playground-auth.verify' => 'roles', + // 'playground-auth.userRole' => true, + // 'playground-auth.hasRole' => true, + // 'playground-auth.userRoles' => false, + // ]); $fqdn = $this->fqdn; - $user = UserWithRole::find(User::factory()->create()->id); - - // The role is not saved since the column may not exist. - $user->role = 'admin'; + $user = User::factory()->admin()->create(); $model = $fqdn::factory()->create([ 'owned_by_id' => $user->id, @@ -283,19 +281,16 @@ public function test_lock_with_admin_role_and_succeed() public function test_lock_with_admin_role_and_succeed_with_json() { - config([ - 'playground.auth.verify' => 'roles', - 'playground.auth.userRole' => true, - 'playground.auth.hasRole' => true, - 'playground.auth.userRoles' => false, - ]); + // config([ + // 'playground-auth.verify' => 'roles', + // 'playground-auth.userRole' => true, + // 'playground-auth.hasRole' => true, + // 'playground-auth.userRoles' => false, + // ]); $fqdn = $this->fqdn; - $user = UserWithRole::find(User::factory()->create()->id); - - // The role is not saved since the column may not exist. - $user->role = 'admin'; + $user = User::factory()->admin()->create(); $model = $fqdn::factory()->create([ 'owned_by_id' => $user->id, diff --git a/src/Feature/Http/Controllers/Resource/RestoreTrait.php b/src/Feature/Http/Controllers/Resource/Playground/RestoreTrait.php similarity index 79% rename from src/Feature/Http/Controllers/Resource/RestoreTrait.php rename to src/Feature/Http/Controllers/Resource/Playground/RestoreTrait.php index 28dcc7a..d8ab4fe 100644 --- a/src/Feature/Http/Controllers/Resource/RestoreTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/RestoreTrait.php @@ -2,28 +2,27 @@ /** * Playground */ -namespace Playground\Test\Feature\Http\Controllers\Resource; +namespace Playground\Test\Feature\Http\Controllers\Resource\Playground; use Illuminate\Support\Carbon; -use Playground\Test\Models\User; -use Playground\Test\Models\UserWithRole; +use Playground\Test\Models\PlaygroundUser as User; /** - * \Playground\Test\Feature\Http\Controllers\Resource\RestoreTrait + * \Playground\Test\Feature\Http\Controllers\Resource\Playground\RestoreTrait */ trait RestoreTrait { public function test_guest_cannot_restore() { - config([ - // 'playground.auth.token.name' => 'app', - 'playground.auth.verify' => 'user', - 'playground.auth.userRole' => false, - 'playground.auth.hasRole' => false, - 'playground.auth.userRoles' => false, - 'playground.auth.hasPrivilege' => false, - 'playground.auth.userPrivileges' => false, - ]); + // config([ + // // 'playground-auth.token.name' => 'app', + // 'playground-auth.verify' => 'user', + // 'playground-auth.userRole' => false, + // 'playground-auth.hasRole' => false, + // 'playground-auth.userRoles' => false, + // 'playground-auth.hasPrivilege' => false, + // 'playground-auth.userPrivileges' => false, + // ]); $fqdn = $this->fqdn; @@ -48,7 +47,8 @@ public function test_guest_cannot_restore() // $response->dump(); - $response->assertRedirect(route('login')); + // $response->assertRedirect(route('login')); + $response->assertStatus(403); $this->assertDatabaseHas($this->packageInfo['table'], [ 'id' => $model->id, @@ -57,11 +57,11 @@ public function test_guest_cannot_restore() ]); } - public function test_restore_as_standard_user_and_succeed() + public function test_restore_as_admin_user_and_succeed() { $fqdn = $this->fqdn; - $user = User::factory()->create(); + $user = User::factory()->admin()->create(); $model = $fqdn::factory()->create([ 'owned_by_id' => $user->id, @@ -99,11 +99,11 @@ public function test_restore_as_standard_user_and_succeed() ])); } - public function test_restore_as_standard_user_using_json_and_succeed() + public function test_restore_as_admin_user_using_json_and_succeed() { $fqdn = $this->fqdn; - $user = User::factory()->create(); + $user = User::factory()->admin()->create(); $model = $fqdn::factory()->create([ 'owned_by_id' => $user->id, @@ -139,11 +139,11 @@ public function test_restore_as_standard_user_using_json_and_succeed() $response->assertStatus(200); } - public function test_restore_as_standard_user_and_succeed_with_redirect_to_index_with_only_trash() + public function test_restore_as_admin_user_and_succeed_with_redirect_to_index_with_only_trash() { $fqdn = $this->fqdn; - $user = User::factory()->create(); + $user = User::factory()->admin()->create(); $model = $fqdn::factory()->create([ 'owned_by_id' => $user->id, @@ -194,16 +194,16 @@ public function test_restore_as_standard_user_and_succeed_with_redirect_to_index public function test_restore_with_user_role_and_get_denied() { - config([ - 'playground.auth.verify' => 'roles', - 'playground.auth.userRole' => true, - 'playground.auth.hasRole' => true, - 'playground.auth.userRoles' => false, - ]); + // config([ + // 'playground-auth.verify' => 'roles', + // 'playground-auth.userRole' => true, + // 'playground-auth.hasRole' => true, + // 'playground-auth.userRoles' => false, + // ]); $fqdn = $this->fqdn; - $user = UserWithRole::find(User::factory()->create()->id); + $user = User::factory()->create(['role' => 'user']); $model = $fqdn::factory()->create([ 'owned_by_id' => $user->id, @@ -241,19 +241,16 @@ public function test_restore_with_user_role_and_get_denied() public function test_restore_with_admin_role_and_succeed() { - config([ - 'playground.auth.verify' => 'roles', - 'playground.auth.userRole' => true, - 'playground.auth.hasRole' => true, - 'playground.auth.userRoles' => false, - ]); + // config([ + // 'playground-auth.verify' => 'roles', + // 'playground-auth.userRole' => true, + // 'playground-auth.hasRole' => true, + // 'playground-auth.userRoles' => false, + // ]); $fqdn = $this->fqdn; - $user = UserWithRole::find(User::factory()->create()->id); - - // The role is not saved since the column may not exist. - $user->role = 'admin'; + $user = User::factory()->admin()->create(); $model = $fqdn::factory()->create([ 'owned_by_id' => $user->id, diff --git a/src/Feature/Http/Controllers/Resource/ShowTrait.php b/src/Feature/Http/Controllers/Resource/Playground/ShowTrait.php similarity index 74% rename from src/Feature/Http/Controllers/Resource/ShowTrait.php rename to src/Feature/Http/Controllers/Resource/Playground/ShowTrait.php index bf373a7..81c131c 100644 --- a/src/Feature/Http/Controllers/Resource/ShowTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/ShowTrait.php @@ -2,12 +2,12 @@ /** * Playground */ -namespace Playground\Test\Feature\Http\Controllers\Resource; +namespace Playground\Test\Feature\Http\Controllers\Resource\Playground; -use Playground\Test\Models\UserWithSanctum; +use Playground\Test\Models\PlaygroundUser as User; /** - * \Playground\Test\Feature\Http\Controllers\Resource\ShowTrait + * \Playground\Test\Feature\Http\Controllers\Resource\Playground\ShowTrait */ trait ShowTrait { @@ -25,16 +25,17 @@ public function test_guest_cannot_render_show_view() ]); $response = $this->get($url); - $response->assertRedirect(route('login')); + $response->assertStatus(403); + // $response->assertRedirect(route('login')); } - public function test_show_view_rendered_by_user() + public function test_show_view_rendered_by_admin() { $fqdn = $this->fqdn; $model = $fqdn::factory()->create(); - $user = UserWithSanctum::factory()->create(); + $user = User::factory()->admin()->create(); $index = route($this->packageInfo['model_route']); @@ -52,13 +53,13 @@ public function test_show_view_rendered_by_user() $this->assertAuthenticated(); } - public function test_show_info_with_user_using_json() + public function test_show_as_admin_using_json() { $fqdn = $this->fqdn; $model = $fqdn::factory()->create(); - $user = UserWithSanctum::factory()->create(); + $user = User::factory()->admin()->create(); $url = route(sprintf( '%1$s.show', diff --git a/src/Feature/Http/Controllers/Resource/UnlockTrait.php b/src/Feature/Http/Controllers/Resource/Playground/UnlockTrait.php similarity index 77% rename from src/Feature/Http/Controllers/Resource/UnlockTrait.php rename to src/Feature/Http/Controllers/Resource/Playground/UnlockTrait.php index 49c7ef4..267941c 100644 --- a/src/Feature/Http/Controllers/Resource/UnlockTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/UnlockTrait.php @@ -2,27 +2,26 @@ /** * Playground */ -namespace Playground\Test\Feature\Http\Controllers\Resource; +namespace Playground\Test\Feature\Http\Controllers\Resource\Playground; -use Playground\Test\Models\User; -use Playground\Test\Models\UserWithRole; +use Playground\Test\Models\PlaygroundUser as User; /** - * \Playground\Test\Feature\Http\Controllers\Resource\UnlockTrait + * \Playground\Test\Feature\Http\Controllers\Resource\Playground\UnlockTrait */ trait UnlockTrait { public function test_guest_cannot_unlock() { - config([ - // 'playground.auth.token.name' => 'app', - 'playground.auth.verify' => 'user', - 'playground.auth.userRole' => false, - 'playground.auth.hasRole' => false, - 'playground.auth.userRoles' => false, - 'playground.auth.hasPrivilege' => false, - 'playground.auth.userPrivileges' => false, - ]); + // config([ + // // 'playground-auth.token.name' => 'app', + // 'playground-auth.verify' => 'user', + // 'playground-auth.userRole' => false, + // 'playground-auth.hasRole' => false, + // 'playground-auth.userRoles' => false, + // 'playground-auth.hasPrivilege' => false, + // 'playground-auth.userPrivileges' => false, + // ]); $fqdn = $this->fqdn; @@ -47,7 +46,8 @@ public function test_guest_cannot_unlock() // $response->dump(); - $response->assertRedirect(route('login')); + // $response->assertRedirect(route('login')); + $response->assertStatus(403); $this->assertDatabaseHas($this->packageInfo['table'], [ 'id' => $model->id, @@ -56,11 +56,11 @@ public function test_guest_cannot_unlock() ]); } - public function test_unlock_as_standard_user_and_succeed() + public function test_unlock_as_admin_user_and_succeed() { $fqdn = $this->fqdn; - $user = User::factory()->create(); + $user = User::factory()->admin()->create(); $model = $fqdn::factory()->create([ 'owned_by_id' => $user->id, @@ -98,11 +98,11 @@ public function test_unlock_as_standard_user_and_succeed() ])); } - public function test_unlock_as_standard_user_using_json_and_succeed() + public function test_unlock_as_admin_user_using_json_and_succeed() { $fqdn = $this->fqdn; - $user = User::factory()->create(); + $user = User::factory()->admin()->create(); $model = $fqdn::factory()->create([ 'owned_by_id' => $user->id, @@ -138,11 +138,11 @@ public function test_unlock_as_standard_user_using_json_and_succeed() $response->assertStatus(200); } - public function test_unlock_as_standard_user_and_succeed_with_redirect_to_index_with_sorted_by_unlocked_desc() + public function test_unlock_as_admin_user_and_succeed_with_redirect_to_index_with_sorted_by_unlocked_desc() { $fqdn = $this->fqdn; - $user = User::factory()->create(); + $user = User::factory()->admin()->create(); $model = $fqdn::factory()->create([ 'owned_by_id' => $user->id, @@ -191,17 +191,20 @@ public function test_unlock_as_standard_user_and_succeed_with_redirect_to_index_ public function test_unlock_with_user_role_and_get_denied() { - config([ - 'playground.auth.verify' => 'roles', - 'playground.auth.userRole' => true, - 'playground.auth.hasRole' => true, - 'playground.auth.userRoles' => false, - ]); + // config([ + // 'playground-auth.verify' => 'roles', + // 'playground-auth.userRole' => true, + // 'playground-auth.hasRole' => true, + // 'playground-auth.userRoles' => false, + // ]); $fqdn = $this->fqdn; - $user = UserWithRole::find(User::factory()->create()->id); - + $user = User::factory()->admin()->create(['role' => 'user']); + // dump([ + // '__METHOD__' => __METHOD__, + // '$user' => $user, + // ]); $model = $fqdn::factory()->create([ 'owned_by_id' => $user->id, 'locked' => true, @@ -238,19 +241,16 @@ public function test_unlock_with_user_role_and_get_denied() public function test_unlock_with_admin_role_and_succeed() { - config([ - 'playground.auth.verify' => 'roles', - 'playground.auth.userRole' => true, - 'playground.auth.hasRole' => true, - 'playground.auth.userRoles' => false, - ]); + // config([ + // 'playground-auth.verify' => 'roles', + // 'playground-auth.userRole' => true, + // 'playground-auth.hasRole' => true, + // 'playground-auth.userRoles' => false, + // ]); $fqdn = $this->fqdn; - $user = UserWithRole::find(User::factory()->create()->id); - - // The role is not saved since the column may not exist. - $user->role = 'admin'; + $user = User::factory()->admin()->create(); $model = $fqdn::factory()->create([ 'owned_by_id' => $user->id, @@ -290,19 +290,16 @@ public function test_unlock_with_admin_role_and_succeed() public function test_unlock_with_admin_role_and_succeed_with_json() { - config([ - 'playground.auth.verify' => 'roles', - 'playground.auth.userRole' => true, - 'playground.auth.hasRole' => true, - 'playground.auth.userRoles' => false, - ]); + // config([ + // 'playground-auth.verify' => 'roles', + // 'playground-auth.userRole' => true, + // 'playground-auth.hasRole' => true, + // 'playground-auth.userRoles' => false, + // ]); $fqdn = $this->fqdn; - $user = UserWithRole::find(User::factory()->create()->id); - - // The role is not saved since the column may not exist. - $user->role = 'admin'; + $user = User::factory()->admin()->create(); $model = $fqdn::factory()->create([ 'owned_by_id' => $user->id, diff --git a/src/Models/PlaygroundUser.php b/src/Models/PlaygroundUser.php new file mode 100644 index 0000000..7f97044 --- /dev/null +++ b/src/Models/PlaygroundUser.php @@ -0,0 +1,17 @@ + - */ - protected $fillable = [ - 'name', - 'email', - 'password', - 'email_verified_at', - 'remember_token', - 'parent_id', - ]; - - public $timestamps = false; - - protected $table = 'users'; - - /** - * @var array - */ - protected $attributes = [ - 'name' => '', - 'email' => '', - 'parent_id' => null, - ]; - - public function children(): HasMany - { - return $this->hasMany(self::class); - } - - public function parent(): HasOne - { - return $this->hasOne( - self::class, - 'id', - 'parent_id' - ); - } -} diff --git a/src/Models/UserWithSanctum.php b/src/Models/UserWithSanctum.php index bf75a54..b9f4446 100644 --- a/src/Models/UserWithSanctum.php +++ b/src/Models/UserWithSanctum.php @@ -4,8 +4,8 @@ */ namespace Playground\Test\Models; -use Laravel\Sanctum\HasApiTokens; use Laravel\Sanctum\Contracts\HasApiTokens as HasApiTokensContract; +use Laravel\Sanctum\HasApiTokens; /** * \Playground\Test\Models\UserWithSanctum diff --git a/tests/Feature/Console/Commands/About/CommandTest.php b/tests/Feature/Console/Commands/About/CommandTest.php index 8cc5ac9..34624ca 100644 --- a/tests/Feature/Console/Commands/About/CommandTest.php +++ b/tests/Feature/Console/Commands/About/CommandTest.php @@ -6,8 +6,8 @@ declare(strict_types=1); namespace Tests\Feature\Playground\Test\Console\Commands\About; -use Playground\Test\ServiceProvider; use PHPUnit\Framework\Attributes\CoversClass; +use Playground\Test\ServiceProvider; use Tests\Feature\Playground\Test\TestCase; /** diff --git a/tests/Feature/TestCase.php b/tests/Feature/TestCase.php index 5367c6f..107b08d 100644 --- a/tests/Feature/TestCase.php +++ b/tests/Feature/TestCase.php @@ -14,11 +14,17 @@ class TestCase extends \Tests\Unit\Playground\Test\TestCase protected bool $load_migrations_testing = false; protected bool $load_User = false; + protected bool $load_UserWithChildren = false; + protected bool $load_UserWithRole = false; + protected bool $load_UserWithRoleAndPrivileges = false; + protected bool $load_UserWithRoleAndRoles = false; + protected bool $load_UserWithRoleAndRolesAndPrivileges = false; + protected bool $load_UserWithSanctum = false; /** diff --git a/tests/Unit/Models/PlaygroundUser/ModelTest.php b/tests/Unit/Models/PlaygroundUser/ModelTest.php new file mode 100644 index 0000000..c3ee412 --- /dev/null +++ b/tests/Unit/Models/PlaygroundUser/ModelTest.php @@ -0,0 +1,247 @@ + + */ + public const MODEL_CLASS = PlaygroundUser::class; + + public function test_getAttributes(): void + { + $mc = static::MODEL_CLASS; + + /** + * @var PlaygroundUser $instance + */ + $instance = new $mc(); + + $expected = [ + // 'name' => '', + // 'email' => '', + // 'role' => '', + // 'roles' => [], + 'abilities' => '{}', + 'accounts' => '{}', + 'address' => '{}', + 'meta' => '{}', + 'notes' => '[]', + 'options' => '{}', + 'registration' => '[]', + 'roles' => '[]', + 'permissions' => '[]', + 'privileges' => '[]', + ]; + + $attributes = $instance->getAttributes(); + + $this->assertIsArray($attributes); + + $this->assertSame($expected, $attributes); + } + + public function test_hasRole_is_false_without_role(): void + { + $mc = static::MODEL_CLASS; + + /** + * @var PlaygroundUser $instance + */ + $instance = new $mc(); + + $role = null; + + $this->assertFalse($instance->hasRole($role)); + } + + public function test_hasRole_is_true_with_matching_role(): void + { + $mc = static::MODEL_CLASS; + + /** + * @var PlaygroundUser $instance + */ + $instance = new $mc(); + + $role = 'user'; + + $instance->role = $role; + + $this->assertTrue($instance->hasRole($role)); + } + + public function test_hasRole_is_true_with_matching_secondary_role(): void + { + $mc = static::MODEL_CLASS; + + /** + * @var PlaygroundUser $instance + */ + $instance = new $mc(); + + $role = 'user'; + + $instance->role = $role; + + $instance->roles = [ + 'publisher', + ]; + + $this->assertTrue($instance->hasRole($role)); + $this->assertTrue($instance->hasRole('publisher')); + $this->assertFalse($instance->hasRole('admin')); + $this->assertFalse($instance->isAdmin()); + } + + public function test_isAdmin_is_true_with_admin_role(): void + { + $mc = static::MODEL_CLASS; + + /** + * @var PlaygroundUser $instance + */ + $instance = new $mc(); + + $role = 'admin'; + + $instance->role = $role; + + $instance->roles = [ + 'publisher', + ]; + + $this->assertTrue($instance->isAdmin()); + } + + public function test_isAdmin_is_true_with_admin_secondary_role(): void + { + $mc = static::MODEL_CLASS; + + /** + * @var PlaygroundUser $instance + */ + $instance = new $mc(); + + $role = 'publisher'; + + $instance->role = $role; + + $instance->roles = [ + 'admin', + ]; + + $this->assertTrue($instance->isAdmin()); + } + + public function test_isAdmin_is_true_with_wheel_role(): void + { + $mc = static::MODEL_CLASS; + + /** + * @var PlaygroundUser $instance + */ + $instance = new $mc(); + + $role = 'wheel'; + + $instance->role = $role; + + $instance->roles = [ + 'publisher', + ]; + + $this->assertTrue($instance->isAdmin()); + } + + public function test_isAdmin_is_true_with_wheel_secondary_role(): void + { + $mc = static::MODEL_CLASS; + + /** + * @var PlaygroundUser $instance + */ + $instance = new $mc(); + + $role = 'publisher'; + + $instance->role = $role; + + $instance->roles = [ + 'wheel', + ]; + + $this->assertTrue($instance->isAdmin()); + } + + public function test_isAdmin_is_true_with_root_role(): void + { + $mc = static::MODEL_CLASS; + + /** + * @var PlaygroundUser $instance + */ + $instance = new $mc(); + + $role = 'root'; + + $instance->role = $role; + + $instance->roles = [ + 'publisher', + ]; + + $this->assertTrue($instance->isAdmin()); + } + + public function test_isAdmin_is_false_with_root_secondary_role(): void + { + $mc = static::MODEL_CLASS; + + /** + * @var PlaygroundUser $instance + */ + $instance = new $mc(); + + $role = 'publisher'; + + $instance->role = $role; + + $instance->roles = [ + 'root', + ]; + + $this->assertFalse($instance->isAdmin()); + } + + public function test_factory_with_make(): void + { + $mc = static::MODEL_CLASS; + + /** + * @var PlaygroundUser $instance + */ + $instance = $mc::factory()->make(); + + $role = 'publisher'; + + $instance->role = $role; + + $instance->roles = [ + 'root', + ]; + + $this->assertFalse($instance->isAdmin()); + } +} diff --git a/tests/Unit/Models/User/ModelTest.php b/tests/Unit/Models/User/ModelTest.php index f6aff67..f6e832f 100644 --- a/tests/Unit/Models/User/ModelTest.php +++ b/tests/Unit/Models/User/ModelTest.php @@ -39,4 +39,16 @@ public function test_getAttributes(): void $this->assertSame($expected, $attributes); } + + public function test_factory_with_make(): void + { + $mc = static::MODEL_CLASS; + + /** + * @var User $instance + */ + $instance = $mc::factory()->make(); + + $this->assertInstanceOf($mc, $instance); + } } diff --git a/tests/Unit/Models/UserWithChildren/ModelTest.php b/tests/Unit/Models/UserWithChildren/ModelTest.php deleted file mode 100644 index ba336f1..0000000 --- a/tests/Unit/Models/UserWithChildren/ModelTest.php +++ /dev/null @@ -1,35 +0,0 @@ - - */ - protected string $modelClass = UserWithChildren::class; - - protected bool $hasRelationships = true; - - /** - * @var array - */ - protected array $hasMany = [ - 'children', - ]; - - /** - * @var array - */ - protected array $hasOne = [ - 'parent', - ]; -} diff --git a/tests/Unit/Models/UserWithRoleAndPrivileges/ModelTest.php b/tests/Unit/Models/UserWithRoleAndPrivileges/ModelTest.php index 803f773..268487e 100644 --- a/tests/Unit/Models/UserWithRoleAndPrivileges/ModelTest.php +++ b/tests/Unit/Models/UserWithRoleAndPrivileges/ModelTest.php @@ -95,4 +95,17 @@ public function test_hasPrivilege_is_true_with_correct_privilege(): void $privilege = 'goose'; $this->assertTrue($instance->hasPrivilege($privilege)); } + + public function test_factory_with_make(): void + { + // $mc = $this->modelClass; + $mc = static::MODEL_CLASS; + + /** + * @var UserWithRoleAndPrivileges $instance + */ + $instance = $mc::factory()->make(); + + $this->assertInstanceOf($mc, $instance); + } } diff --git a/tests/Unit/Models/UserWithRoleAndRoles/ModelTest.php b/tests/Unit/Models/UserWithRoleAndRoles/ModelTest.php index c1cd4b2..552ed07 100644 --- a/tests/Unit/Models/UserWithRoleAndRoles/ModelTest.php +++ b/tests/Unit/Models/UserWithRoleAndRoles/ModelTest.php @@ -216,4 +216,17 @@ public function test_isAdmin_is_false_with_root_secondary_role(): void $this->assertFalse($instance->isAdmin()); } + + public function test_factory_with_make(): void + { + // $mc = $this->modelClass; + $mc = static::MODEL_CLASS; + + /** + * @var UserWithRoleAndRoles $instance + */ + $instance = $mc::factory()->make(); + + $this->assertInstanceOf($mc, $instance); + } } diff --git a/tests/Unit/Models/UserWithRoleAndRolesAndPrivileges/ModelTest.php b/tests/Unit/Models/UserWithRoleAndRolesAndPrivileges/ModelTest.php index 536721f..7d85795 100644 --- a/tests/Unit/Models/UserWithRoleAndRolesAndPrivileges/ModelTest.php +++ b/tests/Unit/Models/UserWithRoleAndRolesAndPrivileges/ModelTest.php @@ -44,4 +44,17 @@ public function test_getAttributes(): void $this->assertSame($expected, $attributes); } + + public function test_factory_with_make(): void + { + // $mc = $this->modelClass; + $mc = static::MODEL_CLASS; + + /** + * @var UserWithRoleAndRolesAndPrivileges $instance + */ + $instance = $mc::factory()->make(); + + $this->assertInstanceOf($mc, $instance); + } } diff --git a/tests/Unit/Models/UserWithSanctum/ModelTest.php b/tests/Unit/Models/UserWithSanctum/ModelTest.php index c30b6d2..27c214b 100644 --- a/tests/Unit/Models/UserWithSanctum/ModelTest.php +++ b/tests/Unit/Models/UserWithSanctum/ModelTest.php @@ -39,4 +39,17 @@ public function test_getAttributes(): void $this->assertSame($expected, $attributes); } + + public function test_factory_with_make(): void + { + // $mc = $this->modelClass; + $mc = static::MODEL_CLASS; + + /** + * @var UserWithSanctum $instance + */ + $instance = $mc::factory()->make(); + + $this->assertInstanceOf($mc, $instance); + } }