From 3eb0fa45d494c3b8b567d5601a3050a07e7d27b9 Mon Sep 17 00:00:00 2001 From: Ash Monsh Date: Thu, 13 Jun 2024 03:39:41 +0300 Subject: [PATCH] fix tests --- app/Classes/RenderNavItem.php | 2 +- app/Policies/LayoutPolicy.php | 2 +- config/permission.php | 49 ++++++++++++----- database/factories/UserFactory.php | 3 +- ..._07_15_234105_create_permission_tables.php | 53 +++++++++---------- ..._08_22_124903_alter_tables_constraints.php | 36 +++++++------ phpunit.xml | 4 +- 7 files changed, 87 insertions(+), 62 deletions(-) diff --git a/app/Classes/RenderNavItem.php b/app/Classes/RenderNavItem.php index 0ebec2d..2a3ac13 100644 --- a/app/Classes/RenderNavItem.php +++ b/app/Classes/RenderNavItem.php @@ -63,11 +63,11 @@ public static function render(array $item, string $class = ''): string private static function getAppUrl($app) { return match ($app) { - 'blog' => 'blog', 'contact' => 'contact-us', 'faq' => 'blog/faq', 'libraries' => 'blog/library', 'forms' => 'forms', + default => 'blog', }; } } diff --git a/app/Policies/LayoutPolicy.php b/app/Policies/LayoutPolicy.php index ae6b44e..c2d7d8c 100644 --- a/app/Policies/LayoutPolicy.php +++ b/app/Policies/LayoutPolicy.php @@ -4,7 +4,7 @@ use App\Models\User; use Illuminate\Auth\Access\HandlesAuthorization; -use LaraZeus\Rain\Models\Layout; +use LaraZeus\DynamicDashboard\Models\Layout; class LayoutPolicy { diff --git a/config/permission.php b/config/permission.php index 5b6e184..2a520f3 100644 --- a/config/permission.php +++ b/config/permission.php @@ -98,43 +98,68 @@ /* * When set to true, the method for checking permissions will be registered on the gate. - * Set this to false, if you want to implement custom logic for checking permissions. + * Set this to false if you want to implement custom logic for checking permissions. */ 'register_permission_check_method' => true, /* - * When set to true the package implements teams using the 'team_foreign_key'. If you want - * the migrations to register the 'team_foreign_key', you must set this to true - * before doing the migration. If you already did the migration then you must make a new - * migration to also add 'team_foreign_key' to 'roles', 'model_has_roles', and - * 'model_has_permissions'(view the latest version of package's migration file) + * When set to true, Laravel\Octane\Events\OperationTerminated event listener will be registered + * this will refresh permissions on every TickTerminated, TaskTerminated and RequestTerminated + * NOTE: This should not be needed in most cases, but an Octane/Vapor combination benefited from it. + */ + 'register_octane_reset_listener' => false, + + /* + * Teams Feature. + * When set to true the package implements teams using the 'team_foreign_key'. + * If you want the migrations to register the 'team_foreign_key', you must + * set this to true before doing the migration. + * If you already did the migration then you must make a new migration to also + * add 'team_foreign_key' to 'roles', 'model_has_roles', and 'model_has_permissions' + * (view the latest version of this package's migration file) */ 'teams' => false, /* - * When set to true, the required permission names are added to the exception - * message. This could be considered an information leak in some contexts, so - * the default setting is false here for optimum safety. + * Passport Client Credentials Grant + * When set to true the package will use Passports Client to check permissions + */ + + 'use_passport_client_credentials' => false, + + /* + * When set to true, the required permission names are added to exception messages. + * This could be considered an information leak in some contexts, so the default + * setting is false here for optimum safety. */ 'display_permission_in_exception' => false, /* - * When set to true, the required role names are added to the exception - * message. This could be considered an information leak in some contexts, so - * the default setting is false here for optimum safety. + * When set to true, the required role names are added to exception messages. + * This could be considered an information leak in some contexts, so the default + * setting is false here for optimum safety. */ 'display_role_in_exception' => false, /* * By default wildcard permission lookups are disabled. + * See documentation to understand supported syntax. */ 'enable_wildcard_permission' => false, + /* + * The class to use for interpreting wildcard permissions. + * If you need to modify delimiters, override the class and specify its name here. + */ + // 'permission.wildcard_permission' => Spatie\Permission\WildcardPermission::class, + + /* Cache-specific settings */ + 'cache' => [ /* diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index fb953cd..1801188 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -3,6 +3,7 @@ namespace Database\Factories; use Illuminate\Database\Eloquent\Factories\Factory; +use Illuminate\Support\Facades\Hash; use Illuminate\Support\Str; class UserFactory extends Factory @@ -16,7 +17,7 @@ public function definition(): array 'name' => $this->faker->name(), 'email' => $this->faker->unique()->safeEmail(), 'email_verified_at' => now(), - 'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password + 'password' => Hash::make('password'), 'remember_token' => Str::random(10), ]; } diff --git a/database/migrations/2023_07_15_234105_create_permission_tables.php b/database/migrations/2023_07_15_234105_create_permission_tables.php index fad5acf..b865d48 100644 --- a/database/migrations/2023_07_15_234105_create_permission_tables.php +++ b/database/migrations/2023_07_15_234105_create_permission_tables.php @@ -1,22 +1,21 @@ unsignedBigInteger(PermissionRegistrar::$pivotPermission); + Schema::create($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames, $columnNames, $pivotPermission, $teams) { + $table->unsignedBigInteger($pivotPermission); $table->string('model_type'); $table->unsignedBigInteger($columnNames['model_morph_key']); $table->index([$columnNames['model_morph_key'], 'model_type'], 'model_has_permissions_model_id_model_type_index'); - $table->foreign(PermissionRegistrar::$pivotPermission) + $table->foreign($pivotPermission) ->references('id') // permission id ->on($tableNames['permissions']) ->onDelete('cascade'); @@ -65,23 +64,23 @@ public function up() $table->unsignedBigInteger($columnNames['team_foreign_key']); $table->index($columnNames['team_foreign_key'], 'model_has_permissions_team_foreign_key_index'); - $table->primary([$columnNames['team_foreign_key'], PermissionRegistrar::$pivotPermission, $columnNames['model_morph_key'], 'model_type'], + $table->primary([$columnNames['team_foreign_key'], $pivotPermission, $columnNames['model_morph_key'], 'model_type'], 'model_has_permissions_permission_model_type_primary'); } else { - $table->primary([PermissionRegistrar::$pivotPermission, $columnNames['model_morph_key'], 'model_type'], + $table->primary([$pivotPermission, $columnNames['model_morph_key'], 'model_type'], 'model_has_permissions_permission_model_type_primary'); } }); - Schema::create($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames, $columnNames, $teams) { - $table->unsignedBigInteger(PermissionRegistrar::$pivotRole); + Schema::create($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames, $columnNames, $pivotRole, $teams) { + $table->unsignedBigInteger($pivotRole); $table->string('model_type'); $table->unsignedBigInteger($columnNames['model_morph_key']); $table->index([$columnNames['model_morph_key'], 'model_type'], 'model_has_roles_model_id_model_type_index'); - $table->foreign(PermissionRegistrar::$pivotRole) + $table->foreign($pivotRole) ->references('id') // role id ->on($tableNames['roles']) ->onDelete('cascade'); @@ -89,29 +88,29 @@ public function up() $table->unsignedBigInteger($columnNames['team_foreign_key']); $table->index($columnNames['team_foreign_key'], 'model_has_roles_team_foreign_key_index'); - $table->primary([$columnNames['team_foreign_key'], PermissionRegistrar::$pivotRole, $columnNames['model_morph_key'], 'model_type'], + $table->primary([$columnNames['team_foreign_key'], $pivotRole, $columnNames['model_morph_key'], 'model_type'], 'model_has_roles_role_model_type_primary'); } else { - $table->primary([PermissionRegistrar::$pivotRole, $columnNames['model_morph_key'], 'model_type'], + $table->primary([$pivotRole, $columnNames['model_morph_key'], 'model_type'], 'model_has_roles_role_model_type_primary'); } }); - Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames) { - $table->unsignedBigInteger(PermissionRegistrar::$pivotPermission); - $table->unsignedBigInteger(PermissionRegistrar::$pivotRole); + Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames, $pivotRole, $pivotPermission) { + $table->unsignedBigInteger($pivotPermission); + $table->unsignedBigInteger($pivotRole); - $table->foreign(PermissionRegistrar::$pivotPermission) + $table->foreign($pivotPermission) ->references('id') // permission id ->on($tableNames['permissions']) ->onDelete('cascade'); - $table->foreign(PermissionRegistrar::$pivotRole) + $table->foreign($pivotRole) ->references('id') // role id ->on($tableNames['roles']) ->onDelete('cascade'); - $table->primary([PermissionRegistrar::$pivotPermission, PermissionRegistrar::$pivotRole], 'role_has_permissions_permission_id_role_id_primary'); + $table->primary([$pivotPermission, $pivotRole], 'role_has_permissions_permission_id_role_id_primary'); }); app('cache') @@ -121,10 +120,8 @@ public function up() /** * Reverse the migrations. - * - * @return void */ - public function down() + public function down(): void { $tableNames = config('permission.table_names'); @@ -138,4 +135,4 @@ public function down() Schema::drop($tableNames['roles']); Schema::drop($tableNames['permissions']); } -} +}; diff --git a/database/migrations/2023_08_22_124903_alter_tables_constraints.php b/database/migrations/2023_08_22_124903_alter_tables_constraints.php index abb78e8..85a222a 100644 --- a/database/migrations/2023_08_22_124903_alter_tables_constraints.php +++ b/database/migrations/2023_08_22_124903_alter_tables_constraints.php @@ -13,26 +13,28 @@ */ public function up() { - Schema::table('forms', function (Blueprint $table) { - Schema::disableForeignKeyConstraints(); - $table->dropForeign(['user_id']); - $table->dropForeign(['category_id']); + if (!app()->runningUnitTests()) { + Schema::table('forms', function (Blueprint $table) { + Schema::disableForeignKeyConstraints(); + $table->dropForeign(['user_id']); + $table->dropForeign(['category_id']); - $table->unsignedBigInteger('user_id')->nullable()->change(); - Schema::enableForeignKeyConstraints(); + $table->unsignedBigInteger('user_id')->nullable()->change(); + Schema::enableForeignKeyConstraints(); - $table->foreign('user_id') - ->onUpdate('cascade') - ->nullOnDelete() - ->references('id') - ->on('users'); + $table->foreign('user_id') + ->onUpdate('cascade') + ->nullOnDelete() + ->references('id') + ->on('users'); - $table->foreign('category_id') - ->onUpdate('cascade') - ->nullOnDelete() - ->references('id') - ->on('categories'); - }); + $table->foreign('category_id') + ->onUpdate('cascade') + ->nullOnDelete() + ->references('id') + ->on('categories'); + }); + } } /** diff --git a/phpunit.xml b/phpunit.xml index 794cc81..ff19ac1 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -21,8 +21,8 @@ - - + +