From 6ec1f6c2b471d5a2ae744992c9674a7ab0c7996e Mon Sep 17 00:00:00 2001 From: amirghasempoor Date: Tue, 11 Mar 2025 11:47:43 +0330 Subject: [PATCH] write test for permission controller --- .../SendRoadObservationToNikarayanCommand.php | 10 +- app/Jobs/SendRoadObservationToNikarayan.php | 8 +- app/Services/NikarayanService.php | 2 +- ...33823_create_road_items_projects_table.php | 2 +- ...133823_create_user_activity_logs_table.php | 8 +- phpunit.xml | 4 +- routes/v3.php | 2 + tests/Feature/V3/Permission/IndexTest.php | 59 ++++ tests/Feature/V3/Permission/StoreTest.php | 297 ++++++++++++++++++ 9 files changed, 375 insertions(+), 17 deletions(-) create mode 100644 tests/Feature/V3/Permission/IndexTest.php create mode 100644 tests/Feature/V3/Permission/StoreTest.php diff --git a/app/Console/Commands/SendRoadObservationToNikarayanCommand.php b/app/Console/Commands/SendRoadObservationToNikarayanCommand.php index e04568bd..5202e148 100644 --- a/app/Console/Commands/SendRoadObservationToNikarayanCommand.php +++ b/app/Console/Commands/SendRoadObservationToNikarayanCommand.php @@ -33,16 +33,14 @@ class SendRoadObservationToNikarayanCommand extends Command $this->info("Dispatching jobs with a delay between each..."); DB::transaction(function () use (&$count) { - $delay = 0; RoadObserved::query() ->whereBetween('StartTime_DateTime_fa', ['1403-01-01 00:00:00', '1403-07-13 23:59:59']) - ->chunkById(50, function ($roads) use (&$delay, &$count) { + ->chunkById(50, function ($roads) use (&$count) { foreach ($roads as $road) { $road->update(['status' => 1]); - SendRoadObservationToNikarayan::dispatch($road)->delay(now()->addSeconds($delay)); - Log::channel('road_observation_problem')->info("Job for Road Observed ID {$road->id} scheduled with a {$delay} seconds delay."); - $this->info("Scheduled job for Road ID: {$road->id} {$delay}s"); - $delay += 3; + SendRoadObservationToNikarayan::dispatch($road); + Log::channel('road_observation_problem')->info("Job for Road Observed ID {$road->id} scheduled."); + $this->info("Scheduled job for Road ID: {$road->id}"); $count +=1; } }); diff --git a/app/Jobs/SendRoadObservationToNikarayan.php b/app/Jobs/SendRoadObservationToNikarayan.php index 87cdeb0c..6b7abb26 100644 --- a/app/Jobs/SendRoadObservationToNikarayan.php +++ b/app/Jobs/SendRoadObservationToNikarayan.php @@ -45,12 +45,14 @@ class SendRoadObservationToNikarayan implements ShouldQueue ); $soapClient = new SoapClient($url); - - $soapClient->UpdateInfo_RoadObservedProblems($array); + $result = $soapClient->UpdateInfo_RoadObservedProblems($array); + Log::channel('road_observation_problem')->info(json_encode($result), [$this->roadObserved->id]); } catch (\Exception $exception){ Log::channel('road_observation_problem')->error($exception->getMessage(), [ - 'roadObserved' => $this->roadObserved, + 'roadObserved' => $this->roadObserved->id, + 'line' => $exception->getLine(), + 'file' => $exception->getFile(), ]); } } diff --git a/app/Services/NikarayanService.php b/app/Services/NikarayanService.php index 19f4945e..2d229668 100644 --- a/app/Services/NikarayanService.php +++ b/app/Services/NikarayanService.php @@ -33,7 +33,7 @@ class NikarayanService $soapClient = new SoapClient($url); $result = $soapClient->UpdateInfo_RoadObservedProblems($array); - Log::channel('nikarayan')->info($result); + Log::channel('nikarayan')->info(json_encode($result)); return $result; } diff --git a/database/old_migrations/2024_03_05_133823_create_road_items_projects_table.php b/database/old_migrations/2024_03_05_133823_create_road_items_projects_table.php index d3257d2d..dc96c4ed 100644 --- a/database/old_migrations/2024_03_05_133823_create_road_items_projects_table.php +++ b/database/old_migrations/2024_03_05_133823_create_road_items_projects_table.php @@ -21,7 +21,7 @@ return new class extends Migration $table->decimal('end_lat', 12, 10)->nullable(); $table->decimal('end_lng', 12, 10)->nullable(); $table->string('project_distance')->nullable(); - $table->unsignedTinyInteger('item')->index('item'); + $table->unsignedTinyInteger('item');//->index('item'); $table->string('item_fa')->nullable(); $table->integer('sub_item')->nullable(); $table->string('sub_item_fa')->nullable(); diff --git a/database/old_migrations/2024_03_05_133823_create_user_activity_logs_table.php b/database/old_migrations/2024_03_05_133823_create_user_activity_logs_table.php index 4a0b14cc..e72b82b5 100644 --- a/database/old_migrations/2024_03_05_133823_create_user_activity_logs_table.php +++ b/database/old_migrations/2024_03_05_133823_create_user_activity_logs_table.php @@ -36,10 +36,10 @@ return new class extends Migration $table->string('action_type', 100)->nullable(); $table->text('model')->nullable(); $table->timestamps(); - $table->integer('mm')->nullable()->storedAs('month(`created_at`)'); - $table->integer('yy')->nullable()->storedAs('year(`created_at`)'); - $table->integer('dd')->nullable()->storedAs('dayofmonth(`created_at`)'); - $table->integer('hh')->nullable()->storedAs('hour(`created_at`)'); + $table->integer('mm')->nullable();//->storedAs('month(`created_at`)'); + $table->integer('yy')->nullable();//->storedAs('year(`created_at`)'); + $table->integer('dd')->nullable();//->storedAs('dayofmonth(`created_at`)'); + $table->integer('hh')->nullable();//->storedAs('hour(`created_at`)'); }); } diff --git a/phpunit.xml b/phpunit.xml index cf1d8ca1..ff19ac12 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -21,8 +21,8 @@ - - + + diff --git a/routes/v3.php b/routes/v3.php index 380dd3c9..f87debdc 100644 --- a/routes/v3.php +++ b/routes/v3.php @@ -321,6 +321,7 @@ Route::prefix('road_observations') Route::prefix('log_list') ->name('logList.') + ->middleware('permission:manage-log-list') ->controller(LogListManagementController::class) ->group(function () { Route::get('/', 'index')->name('index'); @@ -372,6 +373,7 @@ Route::name('items.') Route::prefix('permissions') ->name('permissions.') + ->middleware('permission:manage-permissions') ->controller(PermissionManagementController::class) ->group(function () { Route::get('/', 'index')->name('index'); diff --git a/tests/Feature/V3/Permission/IndexTest.php b/tests/Feature/V3/Permission/IndexTest.php new file mode 100644 index 00000000..e53c1cd6 --- /dev/null +++ b/tests/Feature/V3/Permission/IndexTest.php @@ -0,0 +1,59 @@ +create(); + + $response = $this->actingAs($user)->getJson(route('v3.permissions.index')); + + $response->assertForbidden(); + } + + public function test_all_permissions_returned_successfully(): void + { + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'manage-permissions' + ])) + ->create(); + + $response = $this->actingAs($user)->getJson(route('v3.permissions.index', [ + 'start' => 0, + 'size' => 10, + 'filters' => json_encode([]), + 'sorting' => json_encode([]), + ])); + + $response->assertOk(); + + $response->assertJsonStructure([ + 'data' => [ + '*' => [ + 'id', + 'name', + 'name_fa', + 'type', + 'type_fa', + 'description', + ], + ], + "meta" => [ + "totalRowCount" + ] + ]); + } +} diff --git a/tests/Feature/V3/Permission/StoreTest.php b/tests/Feature/V3/Permission/StoreTest.php new file mode 100644 index 00000000..37df8b84 --- /dev/null +++ b/tests/Feature/V3/Permission/StoreTest.php @@ -0,0 +1,297 @@ +create(); + + $response = $this->actingAs($user)->postJson(route('v3.permissions.store')); + + $response->assertForbidden(); + } + + public function test_name_attribute_is_required(): void + { + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'manage-permissions' + ])) + ->create(); + + $response = $this->actingAs($user)->postJson(route('v3.permissions.store')); + + $response->assertUnprocessable() + ->assertInvalid([ + 'name' => __('validation.required', ['attribute' => 'نام']) + ]); + } + + public function test_name_attribute_should_be_string(): void + { + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'manage-permissions' + ])) + ->create(); + + $response = $this->actingAs($user)->postJson(route('v3.permissions.store'), [ + 'name' => $this->faker->numberBetween(1, 10) + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'name' => __('validation.string', ['attribute' => 'نام']) + ]); + } + + public function test_name_attribute_should_be_unique(): void + { + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'manage-permissions' + ])) + ->create(); + + $permission = Permission::factory()->create([ + 'name' => $this->faker->name() + ]); + + $response = $this->actingAs($user)->postJson(route('v3.permissions.store'), [ + 'name' => $permission->name, + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'name' => __('validation.unique', ['attribute' => 'نام']) + ]); + } + + public function test_name_fa_attribute_is_required(): void + { + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'manage-permissions' + ])) + ->create(); + + $response = $this->actingAs($user)->postJson(route('v3.permissions.store')); + + $response->assertUnprocessable() + ->assertInvalid([ + 'name_fa' => __('validation.required', ['attribute' => 'name fa']) + ]); + } + + public function test_name_fa_attribute_should_be_string(): void + { + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'manage-permissions' + ])) + ->create(); + + $response = $this->actingAs($user)->postJson(route('v3.permissions.store'), [ + 'name_fa' => $this->faker->numberBetween(1, 10) + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'name_fa' => __('validation.string', ['attribute' => 'name fa']) + ]); + } + + public function test_description_attribute_should_be_string(): void + { + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'manage-permissions' + ])) + ->create(); + + $response = $this->actingAs($user)->postJson(route('v3.permissions.store'), [ + 'description' => $this->faker->numberBetween(1, 10) + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'description' => __('validation.string', ['attribute' => 'توضیحات']) + ]); + } + + public function test_type_attribute_is_required(): void + { + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'manage-permissions' + ])) + ->create(); + + $response = $this->actingAs($user)->postJson(route('v3.permissions.store')); + + $response->assertUnprocessable() + ->assertInvalid([ + 'type' => __('validation.required', ['attribute' => 'type']) + ]); + } + + public function test_type_attribute_should_be_string(): void + { + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'manage-permissions' + ])) + ->create(); + + $response = $this->actingAs($user)->postJson(route('v3.permissions.store'), [ + 'type' => $this->faker->numberBetween(1, 10) + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'type' => __('validation.string', ['attribute' => 'type']) + ]); + } + + public function test_type_fa_attribute_is_required(): void + { + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'manage-permissions' + ])) + ->create(); + + $response = $this->actingAs($user)->postJson(route('v3.permissions.store')); + + $response->assertUnprocessable() + ->assertInvalid([ + 'type_fa' => __('validation.required', ['attribute' => 'type fa']) + ]); + } + + public function test_type_fa_attribute_should_be_string(): void + { + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'manage-permissions' + ])) + ->create(); + + $response = $this->actingAs($user)->postJson(route('v3.permissions.store'), [ + 'type_fa' => $this->faker->numberBetween(1, 10) + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'type_fa' => __('validation.string', ['attribute' => 'type fa']) + ]); + } + + public function test_need_province_attribute_is_required(): void + { + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'manage-permissions' + ])) + ->create(); + + $response = $this->actingAs($user)->postJson(route('v3.permissions.store')); + + $response->assertUnprocessable() + ->assertInvalid([ + 'need_province' => __('validation.required', ['attribute' => 'need province']) + ]); + } + + public function test_need_province_attribute_should_be_1_or_0(): void + { + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'manage-permissions' + ])) + ->create(); + + $response = $this->actingAs($user)->postJson(route('v3.permissions.store'), [ + 'need_province' => $this->faker->numberBetween(3, 9), + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'need_province' => __('validation.in', ['attribute' => 'need province']) + ]); + } + + public function test_need_edare_shahri_attribute_is_required(): void + { + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'manage-permissions' + ])) + ->create(); + + $response = $this->actingAs($user)->postJson(route('v3.permissions.store')); + + $response->assertUnprocessable() + ->assertInvalid([ + 'need_edare_shahri' => __('validation.required', ['attribute' => 'need edare shahri']) + ]); + } + + public function test_need_edare_shahri_attribute_should_be_1_or_0(): void + { + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'manage-permissions' + ])) + ->create(); + + $response = $this->actingAs($user)->postJson(route('v3.permissions.store'), [ + 'need_edare_shahri' => $this->faker->numberBetween(3, 9), + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'need_edare_shahri' => __('validation.in', ['attribute' => 'need edare shahri']) + ]); + } + + public function test_user_can_create_a_permission() + { + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'manage-permissions' + ])) + ->create(); + + $inputData = [ + 'name' => $this->faker->name(), + 'name_fa' => $this->faker->name(), + 'description' => $this->faker->lexify(), + 'type' => $this->faker->name(), + 'type_fa' => $this->faker->name(), + 'need_province' => $this->faker->numberBetween(0, 1), + 'need_edare_shahri' => $this->faker->numberBetween(0, 1), + ]; + + $response = $this->actingAs($user)->postJson(route('v3.permissions.store'), $inputData); + + $response->assertOk() + ->assertExactJson([ + 'message' => __('messages.successful'), + ]); + + $this->assertDatabaseHas('permissions', $inputData); + } +}