From 6ec1f6c2b471d5a2ae744992c9674a7ab0c7996e Mon Sep 17 00:00:00 2001 From: amirghasempoor Date: Tue, 11 Mar 2025 11:47:43 +0330 Subject: [PATCH 1/5] 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); + } +} From 2375cd8b8812900f48eaa9ff1e4a968bd4cd743d Mon Sep 17 00:00:00 2001 From: amirghasempoor Date: Tue, 11 Mar 2025 15:38:26 +0330 Subject: [PATCH 2/5] add update api --- .../Controllers/V3/Dashboard/SafetyAndPrivacyController.php | 2 +- routes/v3.php | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/V3/Dashboard/SafetyAndPrivacyController.php b/app/Http/Controllers/V3/Dashboard/SafetyAndPrivacyController.php index 33c23e27..463a2900 100644 --- a/app/Http/Controllers/V3/Dashboard/SafetyAndPrivacyController.php +++ b/app/Http/Controllers/V3/Dashboard/SafetyAndPrivacyController.php @@ -157,7 +157,7 @@ class SafetyAndPrivacyController extends Controller 'axis_type_name' => AxisTypes::name($request->axis_type_id), ]); - auth()->user()->addActivityComplete(); + auth()->user()->addActivityComplete(1168); }); return $this->successResponse(); diff --git a/routes/v3.php b/routes/v3.php index f87debdc..bef397bb 100644 --- a/routes/v3.php +++ b/routes/v3.php @@ -342,6 +342,7 @@ Route::prefix('safety_and_privacy') Route::get('/sub_items', 'subItems')->name('subItems'); Route::post('/second_step_store/{safetyAndPrivacy}', 'secondStepStore')->name('secondStepStore')->middleware('permission:add-safety-and-privacy'); Route::post('/third_step_store/{safetyAndPrivacy}', 'thirdStepStore')->name('thirdStepStore')->middleware('permission:add-safety-and-privacy'); + Route::post('/{safetyAndPrivacy}', 'update')->name('update')->middleware('permission:update-safety-and-privacy'); Route::get('/{safetyAndPrivacy}', 'show')->name('show'); Route::delete('/{safetyAndPrivacy}', 'destroy')->name('destroy')->middleware('permission:delete-safety-and-privacy'); Route::get('/deserialize/{safetyAndPrivacy}', 'deserialize')->name('deserialize'); From 30ff1b022aa8c01bab1bcf7af41036382ecc937f Mon Sep 17 00:00:00 2001 From: amirghasempoor Date: Wed, 12 Mar 2025 11:53:49 +0330 Subject: [PATCH 3/5] roll back the wrong commit --- ...2024_03_05_133823_create_road_items_projects_table.php | 2 +- .../2024_03_05_133823_create_user_activity_logs_table.php | 8 ++++---- phpunit.xml | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) 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 dc96c4ed..d3257d2d 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 e72b82b5..4a0b14cc 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 ff19ac12..cf1d8ca1 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -21,8 +21,8 @@ - - + + From 1c0ebdb9ab66680fd323f9c5b0305dcf515994c3 Mon Sep 17 00:00:00 2001 From: joddyabott Date: Wed, 12 Mar 2025 17:01:14 +0330 Subject: [PATCH 4/5] wrote test for restore,supervisorIndex,verify --- .../factories/RoadItemsProjectFactory.php | 14 +- .../V3/RoadItemsProject/RestoreTest.php | 72 ++++++++++ .../RoadItemsProject/SupervisorIndexTest.php | 35 +---- .../RoadItemsProject/VerifyBySupervisor.php | 133 ++++++++++++++++++ 4 files changed, 224 insertions(+), 30 deletions(-) create mode 100644 tests/Feature/V3/RoadItemsProject/RestoreTest.php create mode 100644 tests/Feature/V3/RoadItemsProject/VerifyBySupervisor.php diff --git a/database/factories/RoadItemsProjectFactory.php b/database/factories/RoadItemsProjectFactory.php index b6543430..174d68a8 100644 --- a/database/factories/RoadItemsProjectFactory.php +++ b/database/factories/RoadItemsProjectFactory.php @@ -3,6 +3,7 @@ namespace Database\Factories; use Illuminate\Database\Eloquent\Factories\Factory; +use Illuminate\Support\Facades\Schema; /** * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\User> @@ -18,7 +19,16 @@ class RoadItemsProjectFactory extends Factory { return [ 'item' => $this->faker->numberBetween(1, 10), - 'sub_item' => $this->faker->numberBetween(1, 10), - ]; + 'sub_items' => $this->faker->title(), + 'start_lat' =>$this->faker->latitude(), + 'start_lng' =>$this->faker->longitude(), + 'is_new' => $this->faker->boolean(), + 'status' => $this->faker->randomElement([0, 1, 2]), + 'activity_date_time' =>$this->faker->dateTimeBetween('-1 year', 'now'), + 'province_id' => $this->faker->numberBetween(1, 10), + 'city_id' => $this->faker->numberBetween(1, 10), + 'user_id' =>$this->faker->numberBetween(1, 10), + 'user_name' => $this->faker->name(), + ]; } } diff --git a/tests/Feature/V3/RoadItemsProject/RestoreTest.php b/tests/Feature/V3/RoadItemsProject/RestoreTest.php new file mode 100644 index 00000000..a0688df5 --- /dev/null +++ b/tests/Feature/V3/RoadItemsProject/RestoreTest.php @@ -0,0 +1,72 @@ +create(); + $roadItem = RoadItemsProject::factory()->create(); + $response = $this->actingAs($user)->postJson(route('v3.road_items.restore', [$roadItem->id])); + $response->assertForbidden(); + } + public function test_user_cannot_access_authentication_restore(): void + { + $roadItem = RoadItemsProject::factory()->create(); + $response =$this->postJson(route('v3.road_items.restore', [$roadItem->id])); + $response->assertStatus(401); + + } + public function test_user_can_restore_road_item(): void + { + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'restore-road-item' + ])) + ->create(); + + $logList = LogList::factory()->create([ + 'log_unique_code' => 1150, + ]); + UserActivityLog::factory()->create(); + + $roadItem = RoadItemsProject::factory()->create([ + 'status' => 1, + 'status_fa' => 'تایید شده', + 'supervisor_id' => $user->id, + 'supervisor_name' => $user->name, + 'supervisor_description' => 'تایید شده توسط تست', + ]); + + $response = $this->actingAs($user)->postJson(route('v3.road_items.restore', [$roadItem->id])); + + $response->assertOk() + ->assertJson([ + 'message' => __('messages.successful'), + ]); + + $this->assertDatabaseHas('road_items_projects', [ + 'id' => $roadItem->id, + 'status' => 0, + 'status_fa' => 'در حال بررسی', + 'supervisor_id' => null, + 'supervisor_name' => null, + 'supervisor_description' => null, + 'supervising_time' => null, + ]); + } + +} diff --git a/tests/Feature/V3/RoadItemsProject/SupervisorIndexTest.php b/tests/Feature/V3/RoadItemsProject/SupervisorIndexTest.php index 13a080d4..602f4bdb 100644 --- a/tests/Feature/V3/RoadItemsProject/SupervisorIndexTest.php +++ b/tests/Feature/V3/RoadItemsProject/SupervisorIndexTest.php @@ -23,6 +23,13 @@ class SupervisorIndexTest extends TestCase $response->assertForbidden(); } + public function test_user_cannot_access_authentication_to_supervisor_index(): void + { + $response = $this->getJson(route('v3.road_items.supervisorIndex')); + + $response->assertStatus(401); + } + public function test_supervisor_can_see_the_data_table(): void { @@ -47,49 +54,21 @@ class SupervisorIndexTest extends TestCase 'data' => [ '*' => [ "id", - "user_id", "start_lat", "start_lng", "end_lat", "end_lng", - "project_distance", "item", "item_fa", - "sub_item", "sub_item_fa", "sub_item_data", - "sub_items", - "sub_items_data", "created_at", - "updated_at", - "province_id", "province_fa", - "city_id", - "city_fa", - "is_new", - "parent_id", - "start_way_id", - "end_way_id", "unit_fa", - "user_name", - "created_at_fa", - "info_id", "status", "status_fa", - "edarat_id", "edarat_name", - "edarat_type", - "activity_date", - "activity_time", "activity_date_time", - "supervisor_id", - "supervisor_name", - "supervisor_description", - "supervising_time", - "cmms_machine_id", - "cmms_machine_code", - "rahdar_id", - "rahdar_code", "can_supervise", ], ] diff --git a/tests/Feature/V3/RoadItemsProject/VerifyBySupervisor.php b/tests/Feature/V3/RoadItemsProject/VerifyBySupervisor.php new file mode 100644 index 00000000..24c14c43 --- /dev/null +++ b/tests/Feature/V3/RoadItemsProject/VerifyBySupervisor.php @@ -0,0 +1,133 @@ +create(); + + $response = $this->postJson(route('v3.road_items.VerifyBySupervisor', [$roadItem->id])); + + $response->assertStatus(401); + } + + public function test_supervisor_without_permission_cannot_verify_road_item(): void + { + $user = User::factory()->create(); + + Gate::shouldReceive('allows') + ->andReturn(false); + + $roadItem = RoadItemsProject::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.road_items.VerifyBySupervisor', [$roadItem->id,])); + + $response->assertForbidden(); + } + public function test_validation_fails_when_required_fields_are_missing(): void + { + $user = User::factory()->create(); + $roadItem = RoadItemsProject::factory()->create(); + + Gate::shouldReceive('allows') + ->andReturn(true); + + $response = $this->actingAs($user)->postJson(route('v3.road_items.VerifyBySupervisor', [$roadItem->id])); + + $response->assertUnprocessable() + ->assertInvalid([ + 'verify' => __('validation.required', ['attribute' => 'verify']), + ]); + } + + public function test_validation_fails_when_verify_field_is_invalid(): void + { + $user = User::factory()->create(); + $roadItem = RoadItemsProject::factory()->create(); + + Gate::shouldReceive('allows') + ->andReturn(true); + + $response = $this->actingAs($user)->postJson(route('v3.road_items.VerifyBySupervisor', [$roadItem->id]), [ + 'verify' => 5, + 'description' => $this->faker()->sentence(), + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'verify' => __('validation.in', ['attribute' => 'verify']), + ]); + } + + public function test_verify_description_must_be_string(): void + { + $user = User::factory()->create(); + $roadItem = RoadItemsProject::factory()->create(); + + Gate::shouldReceive('allows') + ->andReturn(true); + + $response = $this->actingAs($user)->postJson(route('v3.road_items.VerifyBySupervisor', [$roadItem->id]), [ + 'description' => 12345, + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'description' => __('validation.string', ['attribute' =>"توضیحات"]), + ]); + } + + + + public function test_supervisor_can_view_road_item_data(): void + { + $user = User::factory()->create(); + + LogList::factory()->create([ + 'log_unique_code' => 1149, + ]); + UserActivityLog::factory()->create(); + + $roadItem = RoadItemsProject::factory()->create(); + + Gate::shouldReceive('allows') + ->andReturn(true); + + $description = fake()->sentence(); + $response = $this->actingAs($user)->postJson(route('v3.road_items.VerifyBySupervisor', [$roadItem->id,]), [ + 'verify' => 1, + 'description' =>$description, + ]); + + $response->assertOk() + ->assertJson([ + 'message' => __('messages.successful'), + ]); + $this->assertDatabaseHas('road_items_projects', [ + 'id' => $roadItem->id, + 'status' => 1, + 'status_fa' => 'تایید شده', + 'supervisor_id' => $user->id, + 'supervisor_name' => $user->name, + 'supervisor_description' =>$description, + ]); + } +} From f295034f4f6967b33e9888dc4503409749eeba88 Mon Sep 17 00:00:00 2001 From: joddyabott Date: Sun, 30 Mar 2025 13:47:39 +0330 Subject: [PATCH 5/5] create test for three item in roaditem project --- .../V3/RoadItemsProject/DeleteTest.php | 57 ++ .../V3/RoadItemsProject/OperatorIndexTest.php | 8 + .../Feature/V3/RoadItemsProject/StoreTest.php | 878 +++++++----------- 3 files changed, 401 insertions(+), 542 deletions(-) create mode 100644 tests/Feature/V3/RoadItemsProject/DeleteTest.php diff --git a/tests/Feature/V3/RoadItemsProject/DeleteTest.php b/tests/Feature/V3/RoadItemsProject/DeleteTest.php new file mode 100644 index 00000000..09fb4ae8 --- /dev/null +++ b/tests/Feature/V3/RoadItemsProject/DeleteTest.php @@ -0,0 +1,57 @@ +create(); + $roadItem = RoadItemsProject::factory()->create(); + $response = $this->actingAs($user)->postJson(route('v3.road_items.delete', [$roadItem->id])); + $response->assertForbidden(); + } + + public function test_user_cannot_access_authentication_delete(): void + { + $roadItem = RoadItemsProject::factory()->create(); + $response =$this->postJson(route('v3.road_items.delete', [$roadItem->id])); + $response->assertStatus(401); + + } + + public function test_user_can_delete_road_item(): void + { + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'delete-road-item' + ])) + ->create(); + + $logList = LogList::factory()->create([ + 'log_unique_code' => 1151, + ]); + UserActivityLog::factory()->create(); + $roadItem = RoadItemsProject::factory()->create(); + $response = $this->actingAs($user)->postJson(route('v3.road_items.delete', [$roadItem->id])); + + $response->assertOk() + ->assertJson([ + 'message' => __('messages.successful'), + ]); + $this->assertDatabaseMissing('road_items_projects', [ + 'id' => $roadItem->id, + ]); + } +} diff --git a/tests/Feature/V3/RoadItemsProject/OperatorIndexTest.php b/tests/Feature/V3/RoadItemsProject/OperatorIndexTest.php index b7e9a765..27888ae7 100644 --- a/tests/Feature/V3/RoadItemsProject/OperatorIndexTest.php +++ b/tests/Feature/V3/RoadItemsProject/OperatorIndexTest.php @@ -24,6 +24,14 @@ class OperatorIndexTest extends TestCase $response->assertForbidden(); } + public function test_user_cannot_access_authentication_to_operator_index(): void + { + $response = $this->getJson(route('v3.road_items.operatorIndex')); + + $response->assertStatus(401); + } + + public function test_operator_can_see_the_data_table(): void { $user = User::factory() diff --git a/tests/Feature/V3/RoadItemsProject/StoreTest.php b/tests/Feature/V3/RoadItemsProject/StoreTest.php index ee141ece..7a44821a 100644 --- a/tests/Feature/V3/RoadItemsProject/StoreTest.php +++ b/tests/Feature/V3/RoadItemsProject/StoreTest.php @@ -12,6 +12,7 @@ use App\Models\ObservedItem; use App\Models\Permission; use App\Models\Province; use App\Models\Rahdaran; +use App\Models\RoadItemsProject; use App\Models\User; use App\Models\UserActivityLog; use App\Services\NominatimService; @@ -22,6 +23,8 @@ use Illuminate\Support\Facades\Storage; use Mockery\MockInterface; use Symfony\Component\Finder\Exception\AccessDeniedException; use Tests\TestCase; +use Illuminate\Support\Str; + class StoreTest extends TestCase { @@ -37,104 +40,56 @@ class StoreTest extends TestCase $response->assertForbidden(); } + public function test_user_cannot_access_authentication_store(): void + { +// $roadItem = RoadItemsProject::factory()->create(); + $response =$this->postJson(route('v3.road_items.store')); + $response->assertStatus(401); - public function test_start_point_is_required(): void + } + + public function test_required_fields(): void { $user = User::factory() ->has(Permission::factory([ 'name' => 'create-road-item' ])) ->create(); - $response = $this->actingAs($user)->postJson(route('v3.road_items.store')); $response->assertUnprocessable() ->assertInvalid([ - 'start_point' => __('validation.required', ['attribute' => 'start point']) + 'start_point' => __('validation.required', ['attribute' => 'start point']), + 'item_id' => __('validation.required', ['attribute' => 'item id']), + 'sub_item_id' => __('validation.required', ['attribute' => 'sub item id']), + 'amount' => __('validation.required', ['attribute' => 'amount']), + 'activity_date' => __('validation.required', ['attribute' => 'تاریخ فعالیت']), + 'activity_time' => __('validation.required', ['attribute' => 'ساعت فعالیت']), + 'machines_id' => __('validation.required', ['attribute' => 'machines id']), + 'rahdaran_id' => __('validation.required', ['attribute' => 'rahdaran id']), ]); } - public function test_item_id_is_required(): void + public function test_integer_fields(): void { $user = User::factory() ->has(Permission::factory([ 'name' => 'create-road-item' ])) ->create(); + $data = [ + 'item_id' => $this->faker->name, + 'sub_item_id' => $this->faker->name, + 'observed_item_id' => $this->faker->name, + ]; - $response = $this->actingAs($user)->postJson(route('v3.road_items.store')); + $response = $this->actingAs($user)->postJson(route('v3.road_items.store'), $data); $response->assertUnprocessable() ->assertInvalid([ - 'item_id' => __('validation.required', ['attribute' => 'item id']) - ]); - } - - public function test_item_id_should_be_integer(): void - { - $user = User::factory() - ->has(Permission::factory([ - 'name' => 'create-road-item' - ])) - ->create(); - - $response = $this->actingAs($user)->postJson(route('v3.road_items.store'), [ - 'item_id' => $this->faker->name - ]); - - $response->assertUnprocessable() - ->assertInvalid([ - 'item_id' => __('validation.integer', ['attribute' => 'item id']) - ]); - } - - public function test_sub_item_id_is_required(): void - { - $user = User::factory() - ->has(Permission::factory([ - 'name' => 'create-road-item' - ])) - ->create(); - - $response = $this->actingAs($user)->postJson(route('v3.road_items.store')); - - $response->assertUnprocessable() - ->assertInvalid([ - 'sub_item_id' => __('validation.required', ['attribute' => 'sub item id']) - ]); - } - - public function test_sub_item_id_should_be_integer(): void - { - $user = User::factory() - ->has(Permission::factory([ - 'name' => 'create-road-item' - ])) - ->create(); - - $response = $this->actingAs($user)->postJson(route('v3.road_items.store'), [ - 'sub_item_id' => $this->faker->name - ]); - - $response->assertUnprocessable() - ->assertInvalid([ - 'sub_item_id' => __('validation.integer', ['attribute' => 'sub item id']) - ]); - } - - public function test_amount_is_required(): void - { - $user = User::factory() - ->has(Permission::factory([ - 'name' => 'create-road-item' - ])) - ->create(); - - $response = $this->actingAs($user)->postJson(route('v3.road_items.store')); - - $response->assertUnprocessable() - ->assertInvalid([ - 'amount' => __('validation.required', ['attribute' => 'amount']) + 'item_id' => __('validation.integer', ['attribute' => 'item id']), + 'sub_item_id' => __('validation.integer', ['attribute' => 'sub item id']), + 'observed_item_id' => __('validation.integer', ['attribute' => 'observed item id']), ]); } @@ -156,7 +111,7 @@ class StoreTest extends TestCase ]); } - public function test_observed_item_id_should_be_integer(): void + public function test_exists_fields(): void { $user = User::factory() ->has(Permission::factory([ @@ -164,523 +119,362 @@ class StoreTest extends TestCase ])) ->create(); - $response = $this->actingAs($user)->postJson(route('v3.road_items.store'), [ - 'observed_item_id' => $this->faker->name - ]); - - $response->assertUnprocessable() - ->assertInvalid([ - 'observed_item_id' => __('validation.integer', ['attribute' => 'observed item id']) - ]); - } - - public function test_observed_item_id_should_be_existed_in_db(): void - { - $observedItem = ObservedItem::factory()->create(); - - $user = User::factory() - ->has(Permission::factory([ - 'name' => 'create-road-item' - ])) - ->create(); - - $response = $this->actingAs($user)->postJson(route('v3.road_items.store'), [ - 'observed_item_id' => $this->faker->numberBetween(10, 20) - ]); - - $response->assertUnprocessable() - ->assertInvalid([ - 'observed_item_id' => __('validation.exists', ['attribute' => 'observed item id']) - ]); - } - - public function test_activity_date_is_required(): void - { - $user = User::factory() - ->has(Permission::factory([ - 'name' => 'create-road-item' - ])) - ->create(); - - $response = $this->actingAs($user)->postJson(route('v3.road_items.store')); - - $response->assertUnprocessable() - ->assertInvalid([ - 'activity_date' => __('validation.required', ['attribute' => 'تاریخ فعالیت']) - ]); - } - - public function test_activity_date_should_match_the_format(): void - { - $user = User::factory() - ->has(Permission::factory([ - 'name' => 'create-road-item' - ])) - ->create(); - - $response = $this->actingAs($user)->postJson(route('v3.road_items.store'), [ - 'activity_date' => $this->faker->date('d-m-Y-H-i-s') - ]); - - $response->assertUnprocessable() - ->assertInvalid([ - 'activity_date' => __('validation.date_format', ['attribute' => 'تاریخ فعالیت', 'format' => 'Y-m-d']) - ]); - } - - public function test_activity_time_is_required(): void - { - $user = User::factory() - ->has(Permission::factory([ - 'name' => 'create-road-item' - ])) - ->create(); - - $response = $this->actingAs($user)->postJson(route('v3.road_items.store')); - - $response->assertUnprocessable() - ->assertInvalid([ - 'activity_time' => __('validation.required', ['attribute' => 'ساعت فعالیت']) - ]); - } - - public function test_activity_time_should_match_the_format(): void - { - $user = User::factory() - ->has(Permission::factory([ - 'name' => 'create-road-item' - ])) - ->create(); - - $response = $this->actingAs($user)->postJson(route('v3.road_items.store'), [ - 'activity_time' => $this->faker->date('d-m-Y-H-i-s') - ]); - - $response->assertUnprocessable() - ->assertInvalid([ - 'activity_time' => __('validation.date_format', ['attribute' => 'ساعت فعالیت', 'format' => 'H:i']) - ]); - } - - public function test_before_image_should_be_image(): void - { - $user = User::factory() - ->has(Permission::factory([ - 'name' => 'create-road-item' - ])) - ->create(); - - $response = $this->actingAs($user)->postJson(route('v3.road_items.store'), [ - 'before_image' => $this->faker->name - ]); - - $response->assertUnprocessable() - ->assertInvalid([ - 'before_image' => __('validation.image', ['attribute' => 'before image']) - ]); - } - - public function test_before_image_should_be_less_than_4096kb(): void - { - $user = User::factory() - ->has(Permission::factory([ - 'name' => 'create-road-item' - ])) - ->create(); - - $image = UploadedFile::fake()->create('image.jpg', 5096); - - $response = $this->actingAs($user)->postJson(route('v3.road_items.store'), [ - 'before_image' => $image - ]); - - $response->assertUnprocessable() - ->assertInvalid([ - 'before_image' => __('validation.max.file', ['attribute' => 'before image', 'max' => 4096]) - ]); - } - - public function test_after_image_should_be_image(): void - { - $user = User::factory() - ->has(Permission::factory([ - 'name' => 'create-road-item' - ])) - ->create(); - - $response = $this->actingAs($user)->postJson(route('v3.road_items.store'), [ - 'after_image' => $this->faker->randomNumber() - ]); - - $response->assertUnprocessable() - ->assertInvalid([ - 'after_image' => __('validation.image', ['attribute' => 'after image']) - ]); - } - - public function test_after_image_should_be_less_than_4096kb(): void - { - $user = User::factory() - ->has(Permission::factory([ - 'name' => 'create-road-item' - ])) - ->create(); - - $image = UploadedFile::fake()->create('image.jpg', 5096); - - $response = $this->actingAs($user)->postJson(route('v3.road_items.store'), [ - 'after_image' => $image - ]); - - $response->assertUnprocessable() - ->assertInvalid([ - 'after_image' => __('validation.max.file', ['attribute' => 'after image', 'max' => 4096]) - ]); - } - - public function test_cmms_machine_id_is_required(): void - { - $user = User::factory() - ->has(Permission::factory([ - 'name' => 'create-road-item' - ])) - ->create(); - - $response = $this->actingAs($user)->postJson(route('v3.road_items.store')); - - $response->assertUnprocessable() - ->assertInvalid([ - 'cmms_machine_id' => __('validation.required', ['attribute' => 'کد یکتا ماشین']) - ]); - } - - public function test_cmms_machine_id_should_already_exists_on_the_table(): void - { - $user = User::factory() - ->has(Permission::factory([ - 'name' => 'create-road-item' - ])) - ->create(); - - $response = $this->actingAs($user)->postJson(route('v3.road_items.store'), [ - 'cmms_machine_id' => $this->faker->numberBetween(10, 100) - ]); - - $response->assertUnprocessable() - ->assertInvalid([ - 'cmms_machine_id' => __('validation.exists', ['attribute' => 'کد یکتا ماشین']) - ]); - } - - public function test_rahdar_id_is_required(): void - { - $user = User::factory() - ->has(Permission::factory([ - 'name' => 'create-road-item' - ])) - ->create(); - - $response = $this->actingAs($user)->postJson(route('v3.road_items.store')); - - $response->assertUnprocessable() - ->assertInvalid([ - 'rahdar_id' => __('validation.required', ['attribute' => 'rahdar id']) - ]); - } - - public function test_rahdar_id_should_already_exists_on_the_table(): void - { - $user = User::factory() - ->has(Permission::factory([ - 'name' => 'create-road-item' - ])) - ->create(); - - $response = $this->actingAs($user)->postJson(route('v3.road_items.store'), [ - 'rahdar_id' => $this->faker->numberBetween(10, 100) - ]); - - $response->assertUnprocessable() - ->assertInvalid([ - 'rahdar_id' => __('validation.exists', ['attribute' => 'rahdar id']) - ]); - } - - public function test_province_user_is_prohibited_to_access(): void - { - $edarateOstani = EdarateOstani::factory()->create(); - $cmmsMachine = CMMSMachine::factory()->create(); - $rahdar = Rahdaran::factory()->create(); - - $user = User::factory() - ->has(Permission::factory([ - 'name' => 'create-road-item' - ])) - ->create([ - 'edarate_ostani_id' => $edarateOstani->id - ]); - $data = [ - 'start_point' => '12,12', - 'item_id' => $this->faker->numberBetween(1, 10), - 'sub_item_id' => $this->faker->numberBetween(1, 10), - 'amount' => $this->faker->numberBetween(1, 10), - 'activity_date' => $this->faker->date(), - 'activity_time' => $this->faker->date('H:i'), - 'before_image' => UploadedFile::fake()->create('image.jpg', 10), - 'after_image' => UploadedFile::fake()->create('image.jpg', 10), - 'cmms_machine_id' => $cmmsMachine->id, - 'rahdar_id' => $rahdar->id, + 'machines_id' => [$this->faker->numberBetween(10000, 99999)], + 'rahdaran_id' => [$this->faker->numberBetween(10000, 99999)], + 'observed_item_id' => $this->faker->numberBetween(10000, 99999), ]; $response = $this->actingAs($user)->postJson(route('v3.road_items.store'), $data); - $response->assertStatus(400) - ->assertExactJson([ - 'message' => 'امکان ثبت فعالیت برای ادارات استانی و ستادی وجود ندارد!' + $response->assertUnprocessable() + ->assertInvalid([ + 'machines_id.0', + 'rahdaran_id.0', + 'observed_item_id', ]); } - public function test_user_should_have_edarate_shahri_id(): void + public function test_image_fields(): void { - $city = City::factory()->create(); - $cmmsMachine = CMMSMachine::factory()->create(); - $rahdar = Rahdaran::factory()->create(); - $user = User::factory() ->has(Permission::factory([ - 'name' => 'create-road-item' - ])) - ->create([ - 'city_id' => $city->id - ]); + 'name' => 'create-road-item'])) + ->create(); $data = [ - 'start_point' => '12,12', - 'item_id' => $this->faker->numberBetween(1, 10), - 'sub_item_id' => $this->faker->numberBetween(1, 10), - 'amount' => $this->faker->numberBetween(1, 10), - 'activity_date' => $this->faker->date(), - 'activity_time' => $this->faker->date('H:i'), - 'before_image' => UploadedFile::fake()->create('image.jpg', 10), - 'after_image' => UploadedFile::fake()->create('image.jpg', 10), - 'cmms_machine_id' => $cmmsMachine->id, - 'rahdar_id' => $rahdar->id, + 'before_image' =>$this->faker->name, + 'after_image' =>$this->faker->name, ]; $response = $this->actingAs($user)->postJson(route('v3.road_items.store'), $data); - $response->assertStatus(400) - ->assertExactJson([ - 'message' => 'اداره شهری برای شما در سامانه ثبت نشده است!' - ]); - } - - public function test_end_point_required_when_info_item_needs_end_point(): void - { - $city = City::factory()->create(); - $edarateShari = EdarateShahri::factory()->create(); - $cmmsMachine = CMMSMachine::factory()->create(); - $rahdar = Rahdaran::factory()->create(); - - $user = User::factory() - ->has(Permission::factory([ - 'name' => 'create-road-item' - ])) - ->create([ - 'city_id' => $city->id, - 'edarate_shahri_id' => $edarateShari->id - ]); - - $data = [ - 'start_point' => '12,12', - 'item_id' => $this->faker->numberBetween(1, 10), - 'sub_item_id' => $this->faker->numberBetween(1, 10), - 'amount' => $this->faker->numberBetween(1, 10), - 'activity_date' => $this->faker->date(), - 'activity_time' => $this->faker->date('H:i'), - 'before_image' => UploadedFile::fake()->create('image.jpg', 10), - 'after_image' => UploadedFile::fake()->create('image.jpg', 10), - 'cmms_machine_id' => $cmmsMachine->id, - 'rahdar_id' => $rahdar->id, - ]; - - InfoItem::factory()->create([ - 'item' => $data['item_id'], - 'sub_item' => $data['sub_item_id'], - 'needs_end_point' => 1 - ]); - - $response = $this->actingAs($user)->postJson(route('v3.road_items.store'), $data); - $response->assertUnprocessable() ->assertInvalid([ - 'end_point' => __('validation.required', ['attribute' => 'end_point']) + 'before_image' => __('validation.image', ['attribute' => 'before image']), + 'after_image' => __('validation.image', ['attribute' => 'after image']), ]); } - public function test_before_image_required_when_info_item_needs_image(): void + public function test_image_should_be_less_than_4096kb(): void { - $city = City::factory()->create(); - $edarateShari = EdarateShahri::factory()->create(); - $cmmsMachine = CMMSMachine::factory()->create(); - $rahdar = Rahdaran::factory()->create(); - $user = User::factory() ->has(Permission::factory([ 'name' => 'create-road-item' ])) - ->create([ - 'city_id' => $city->id, - 'edarate_shahri_id' => $edarateShari->id - ]); + ->create(); $data = [ - 'start_point' => '12,12', - 'item_id' => $this->faker->numberBetween(1, 10), - 'sub_item_id' => $this->faker->numberBetween(1, 10), - 'amount' => $this->faker->numberBetween(1, 10), - 'activity_date' => $this->faker->date(), - 'activity_time' => $this->faker->date('H:i'), - 'cmms_machine_id' => $cmmsMachine->id, - 'rahdar_id' => $rahdar->id, + 'before_image' => UploadedFile::fake()->create('image.jpg', 5096), + 'after_image' => UploadedFile::fake()->create('image.jpg', 5096), ]; - InfoItem::factory()->create([ - 'item' => $data['item_id'], - 'sub_item' => $data['sub_item_id'], - 'needs_image' => 1 - ]); - $response = $this->actingAs($user)->postJson(route('v3.road_items.store'), $data); $response->assertUnprocessable() ->assertInvalid([ - 'before_image' => __('validation.required', ['attribute' => 'before_image']) + 'before_image' => __('validation.max.file', ['attribute' => 'before image', 'max' => 4096]), + 'after_image' => __('validation.max.file', ['attribute' => 'after image', 'max' => 4096]), ]); } - public function test_after_image_required_when_info_item_needs_image(): void + public function test_date_format_fields(): void { - $city = City::factory()->create(); - $edarateShari = EdarateShahri::factory()->create(); - $cmmsMachine = CMMSMachine::factory()->create(); - $rahdar = Rahdaran::factory()->create(); - $user = User::factory() ->has(Permission::factory([ - 'name' => 'create-road-item' - ])) - ->create([ - 'city_id' => $city->id, - 'edarate_shahri_id' => $edarateShari->id - ]); + 'name' => 'create-road-item'])) + ->create(); $data = [ - 'start_point' => '12,12', - 'item_id' => $this->faker->numberBetween(1, 10), - 'sub_item_id' => $this->faker->numberBetween(1, 10), - 'amount' => $this->faker->numberBetween(1, 10), - 'activity_date' => $this->faker->date(), - 'activity_time' => $this->faker->date('H:i'), - 'cmms_machine_id' => $cmmsMachine->id, - 'rahdar_id' => $rahdar->id, + 'activity_date' => $this->faker->date('d-m-Y-H-i-s'), + 'activity_time'=> $this->faker->date('d-m-Y-H-i-s'), ]; - InfoItem::factory()->create([ - 'item' => $data['item_id'], - 'sub_item' => $data['sub_item_id'], - 'needs_image' => 1 - ]); - $response = $this->actingAs($user)->postJson(route('v3.road_items.store'), $data); $response->assertUnprocessable() ->assertInvalid([ - 'after_image' => __('validation.required', ['attribute' => 'after_image']) + 'activity_date' => __('validation.date_format', ['attribute' => 'تاریخ فعالیت', 'format' => 'Y-m-d']), + 'activity_time' => __('validation.date_format', ['attribute' => 'ساعت فعالیت', 'format' => 'H:i']), ]); } +// +// public function test_province_user_is_prohibited_to_access(): void +// { +// $edarateOstani = EdarateOstani::factory()->create(); +// $cmmsMachine = CMMSMachine::factory()->create(); +// $rahdar = Rahdaran::factory()->create(); +// +// $user = User::factory() +// ->has(Permission::factory([ +// 'name' => 'create-road-item' +// ])) +// ->create([ +// 'edarate_ostani_id' => $edarateOstani->id +// ]); +// +// $data = [ +// 'start_point' => '12,12', +// 'item_id' => $this->faker->numberBetween(1, 10), +// 'sub_item_id' => $this->faker->numberBetween(1, 10), +// 'amount' => $this->faker->numberBetween(1, 10), +// 'activity_date' => $this->faker->date(), +// 'activity_time' => $this->faker->date('H:i'), +// 'before_image' => UploadedFile::fake()->create('image.jpg', 10), +// 'after_image' => UploadedFile::fake()->create('image.jpg', 10), +// 'cmms_machine_id' => $cmmsMachine->id, +// 'rahdar_id' => $rahdar->id, +// ]; +// +// $response = $this->actingAs($user)->postJson(route('v3.road_items.store'), $data); +// +// $response->assertStatus(400) +// ->assertExactJson([ +// 'message' => 'امکان ثبت فعالیت برای ادارات استانی و ستادی وجود ندارد!' +// ]); +// } +// +// public function test_user_should_have_edarate_shahri_id(): void +// { +// $city = City::factory()->create(); +// $cmmsMachine = CMMSMachine::factory()->create(); +// $rahdar = Rahdaran::factory()->create(); +// +// $user = User::factory() +// ->has(Permission::factory([ +// 'name' => 'create-road-item' +// ])) +// ->create([ +// 'city_id' => $city->id +// ]); +// +// $data = [ +// 'start_point' => '12,12', +// 'item_id' => $this->faker->numberBetween(1, 10), +// 'sub_item_id' => $this->faker->numberBetween(1, 10), +// 'amount' => $this->faker->numberBetween(1, 10), +// 'activity_date' => $this->faker->date(), +// 'activity_time' => $this->faker->date('H:i'), +// 'before_image' => UploadedFile::fake()->create('image.jpg', 10), +// 'after_image' => UploadedFile::fake()->create('image.jpg', 10), +// 'cmms_machine_id' => $cmmsMachine->id, +// 'rahdar_id' => $rahdar->id, +// ]; +// +// $response = $this->actingAs($user)->postJson(route('v3.road_items.store'), $data); +// +// $response->assertStatus(400) +// ->assertExactJson([ +// 'message' => 'اداره شهری برای شما در سامانه ثبت نشده است!' +// ]); +// } +// +// public function test_end_point_required_when_info_item_needs_end_point(): void +// { +// $city = City::factory()->create(); +// $edarateShari = EdarateShahri::factory()->create(); +// $cmmsMachine = CMMSMachine::factory()->create(); +// $rahdar = Rahdaran::factory()->create(); +// +// $user = User::factory() +// ->has(Permission::factory([ +// 'name' => 'create-road-item' +// ])) +// ->create([ +// 'city_id' => $city->id, +// 'edarate_shahri_id' => $edarateShari->id +// ]); +// +// $data = [ +// 'start_point' => '12,12', +// 'item_id' => $this->faker->numberBetween(1, 10), +// 'sub_item_id' => $this->faker->numberBetween(1, 10), +// 'amount' => $this->faker->numberBetween(1, 10), +// 'activity_date' => $this->faker->date(), +// 'activity_time' => $this->faker->date('H:i'), +// 'before_image' => UploadedFile::fake()->create('image.jpg', 10), +// 'after_image' => UploadedFile::fake()->create('image.jpg', 10), +// 'cmms_machine_id' => $cmmsMachine->id, +// 'rahdar_id' => $rahdar->id, +// ]; +// +// InfoItem::factory()->create([ +// 'item' => $data['item_id'], +// 'sub_item' => $data['sub_item_id'], +// 'needs_end_point' => 1 +// ]); +// +// $response = $this->actingAs($user)->postJson(route('v3.road_items.store'), $data); +// +// $response->assertUnprocessable() +// ->assertInvalid([ +// 'end_point' => __('validation.required', ['attribute' => 'end_point']) +// ]); +// } +// +// public function test_before_image_required_when_info_item_needs_image(): void +// { +// $city = City::factory()->create(); +// $edarateShari = EdarateShahri::factory()->create(); +// $cmmsMachine = CMMSMachine::factory()->create(); +// $rahdar = Rahdaran::factory()->create(); +// +// $user = User::factory() +// ->has(Permission::factory([ +// 'name' => 'create-road-item' +// ])) +// ->create([ +// 'city_id' => $city->id, +// 'edarate_shahri_id' => $edarateShari->id +// ]); +// +// $data = [ +// 'start_point' => '12,12', +// 'item_id' => $this->faker->numberBetween(1, 10), +// 'sub_item_id' => $this->faker->numberBetween(1, 10), +// 'amount' => $this->faker->numberBetween(1, 10), +// 'activity_date' => $this->faker->date(), +// 'activity_time' => $this->faker->date('H:i'), +// 'cmms_machine_id' => $cmmsMachine->id, +// 'rahdar_id' => $rahdar->id, +// ]; +// +// InfoItem::factory()->create([ +// 'item' => $data['item_id'], +// 'sub_item' => $data['sub_item_id'], +// 'needs_image' => 1 +// ]); +// +// $response = $this->actingAs($user)->postJson(route('v3.road_items.store'), $data); +// +// $response->assertUnprocessable() +// ->assertInvalid([ +// 'before_image' => __('validation.required', ['attribute' => 'before_image']) +// ]); +// } +// +// public function test_after_image_required_when_info_item_needs_image(): void +// { +// $city = City::factory()->create(); +// $edarateShari = EdarateShahri::factory()->create(); +// $cmmsMachine = CMMSMachine::factory()->create(); +// $rahdar = Rahdaran::factory()->create(); +// +// $user = User::factory() +// ->has(Permission::factory([ +// 'name' => 'create-road-item' +// ])) +// ->create([ +// 'city_id' => $city->id, +// 'edarate_shahri_id' => $edarateShari->id +// ]); +// +// $data = [ +// 'start_point' => '12,12', +// 'item_id' => $this->faker->numberBetween(1, 10), +// 'sub_item_id' => $this->faker->numberBetween(1, 10), +// 'amount' => $this->faker->numberBetween(1, 10), +// 'activity_date' => $this->faker->date(), +// 'activity_time' => $this->faker->date('H:i'), +// 'cmms_machine_id' => $cmmsMachine->id, +// 'rahdar_id' => $rahdar->id, +// ]; +// +// InfoItem::factory()->create([ +// 'item' => $data['item_id'], +// 'sub_item' => $data['sub_item_id'], +// 'needs_image' => 1 +// ]); +// +// $response = $this->actingAs($user)->postJson(route('v3.road_items.store'), $data); +// +// $response->assertUnprocessable() +// ->assertInvalid([ +// 'after_image' => __('validation.required', ['attribute' => 'after_image']) +// ]); +// } +// +// public function test_can_store_a_new_road_item_project(): void +// { +// $city = City::factory()->create(); +// $province = Province::factory()->create(); +// $edarateShari = EdarateShahri::factory()->create(); +// $cmmsMachine = CMMSMachine::factory()->create(); +// $rahdar = Rahdaran::factory()->create(); +// +// LogList::factory()->create([ +// 'log_unique_code' => 1154 +// ]); +// UserActivityLog::factory()->create(); +// +// $user = User::factory() +// ->has(Permission::factory([ +// 'name' => 'create-road-item' +// ])) +// ->create([ +// 'city_id' => $city->id, +// 'province_id' => $province->id, +// 'edarate_shahri_id' => $edarateShari->id +// ]); +// +// Storage::fake('public'); +// +// $this->mock(NominatimService::class, function (MockInterface $mock) { +// $mock->shouldReceive('get_way_id_from_nominatim') +// ->once() +// ->andReturn(0); +// }); +// +// $data = [ +// 'start_point' => '12,12', +// 'item_id' => $this->faker->numberBetween(1, 10), +// 'sub_item_id' => $this->faker->numberBetween(1, 10), +// 'amount' => $this->faker->numberBetween(1, 10), +// 'activity_date' => $this->faker->date(), +// 'activity_time' => $this->faker->date('H:i'), +// 'before_image' => UploadedFile::fake()->create('image.jpg', 10), +// 'after_image' => UploadedFile::fake()->create('image.jpg', 10), +// 'cmms_machine_id' => $cmmsMachine->id, +// 'rahdar_id' => $rahdar->id, +// ]; +// +// $infoItem = InfoItem::factory()->create([ +// 'item' => $data['item_id'], +// 'sub_item' => $data['sub_item_id'], +// 'needs_image' => 1 +// ]); +// +// $response = $this->actingAs($user)->postJson(route('v3.road_items.store'), $data); +// +// $response->assertOk() +// ->assertExactJson([ +// 'message' => __('messages.successful') +// ]); +// +// $this->assertDatabaseHas('road_items_projects', [ +// 'start_lat' => 12, +// 'start_lng' => 12, +// 'item' => $data['item_id'], +// 'sub_item' => $data['sub_item_id'], +// 'province_id' => $province->id, +// 'city_id' => $city->id, +// 'user_name' => $user->name, +// 'start_way_id' => 0, +// 'info_id' => $infoItem->id, +// 'status' => 0, +// 'status_fa' => 'در حال بررسی', +// 'edarat_id' => $user->edarate_shahri_id, +// 'cmms_machine_id' => $cmmsMachine->id, +// 'cmms_machine_code' => $cmmsMachine->machine_code, +// 'rahdar_id' => $rahdar->id, +// 'rahdar_code' => $rahdar->code +// ]); +// +// $this->assertDatabaseHas('user_activity_logs', [ +// 'user_id' => $user->id, +// 'username' => $user->username, +// 'log_unique_code' => 1154, +// ]); +// - public function test_can_store_a_new_road_item_project(): void - { - $city = City::factory()->create(); - $province = Province::factory()->create(); - $edarateShari = EdarateShahri::factory()->create(); - $cmmsMachine = CMMSMachine::factory()->create(); - $rahdar = Rahdaran::factory()->create(); - - LogList::factory()->create([ - 'log_unique_code' => 1154 - ]); - UserActivityLog::factory()->create(); - - $user = User::factory() - ->has(Permission::factory([ - 'name' => 'create-road-item' - ])) - ->create([ - 'city_id' => $city->id, - 'province_id' => $province->id, - 'edarate_shahri_id' => $edarateShari->id - ]); - - Storage::fake('public'); - - $this->mock(NominatimService::class, function (MockInterface $mock) { - $mock->shouldReceive('get_way_id_from_nominatim') - ->once() - ->andReturn(0); - }); - - $data = [ - 'start_point' => '12,12', - 'item_id' => $this->faker->numberBetween(1, 10), - 'sub_item_id' => $this->faker->numberBetween(1, 10), - 'amount' => $this->faker->numberBetween(1, 10), - 'activity_date' => $this->faker->date(), - 'activity_time' => $this->faker->date('H:i'), - 'before_image' => UploadedFile::fake()->create('image.jpg', 10), - 'after_image' => UploadedFile::fake()->create('image.jpg', 10), - 'cmms_machine_id' => $cmmsMachine->id, - 'rahdar_id' => $rahdar->id, - ]; - - $infoItem = InfoItem::factory()->create([ - 'item' => $data['item_id'], - 'sub_item' => $data['sub_item_id'], - 'needs_image' => 1 - ]); - - $response = $this->actingAs($user)->postJson(route('v3.road_items.store'), $data); - - $response->assertOk() - ->assertExactJson([ - 'message' => __('messages.successful') - ]); - - $this->assertDatabaseHas('road_items_projects', [ - 'start_lat' => 12, - 'start_lng' => 12, - 'item' => $data['item_id'], - 'sub_item' => $data['sub_item_id'], - 'province_id' => $province->id, - 'city_id' => $city->id, - 'user_name' => $user->name, - 'start_way_id' => 0, - 'info_id' => $infoItem->id, - 'status' => 0, - 'status_fa' => 'در حال بررسی', - 'edarat_id' => $user->edarate_shahri_id, - 'cmms_machine_id' => $cmmsMachine->id, - 'cmms_machine_code' => $cmmsMachine->machine_code, - 'rahdar_id' => $rahdar->id, - 'rahdar_code' => $rahdar->code - ]); - - $this->assertDatabaseHas('user_activity_logs', [ - 'user_id' => $user->id, - 'username' => $user->username, - 'log_unique_code' => 1154, - ]); - } }