From 8d8f45d950c14cd9fdb12fa58a85f5d57c9dc278 Mon Sep 17 00:00:00 2001 From: amirghasempoor Date: Sat, 9 Mar 2024 15:46:27 +0330 Subject: [PATCH 1/3] write test for safety and privacy controller --- app/Models/SafetyAndPrivacy.php | 3 +- .../factories/SafetyAndPrivacyFactory.php | 23 ++ .../Dashboard/SafetyAndPrivacy/DeleteTest.php | 51 +++++ .../Dashboard/SafetyAndPrivacy/DetailTest.php | 50 +++++ .../SafetyAndPrivacy/getAllDataToMapTest.php | 41 ++++ .../operatorSecondStepStoreTest.php | 210 ++++++++++++++++++ 6 files changed, 377 insertions(+), 1 deletion(-) create mode 100644 database/factories/SafetyAndPrivacyFactory.php create mode 100644 tests/Feature/V2/Dashboard/SafetyAndPrivacy/DeleteTest.php create mode 100644 tests/Feature/V2/Dashboard/SafetyAndPrivacy/DetailTest.php create mode 100644 tests/Feature/V2/Dashboard/SafetyAndPrivacy/getAllDataToMapTest.php create mode 100644 tests/Feature/V2/Dashboard/SafetyAndPrivacy/operatorSecondStepStoreTest.php diff --git a/app/Models/SafetyAndPrivacy.php b/app/Models/SafetyAndPrivacy.php index 02807c3b..d538781a 100644 --- a/app/Models/SafetyAndPrivacy.php +++ b/app/Models/SafetyAndPrivacy.php @@ -2,12 +2,13 @@ namespace App\Models; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use App\Traits\SafetyAndPrivacyReportAble; class SafetyAndPrivacy extends Model { - use SafetyAndPrivacyReportAble; + use SafetyAndPrivacyReportAble, HasFactory; protected $fillable = [ 'recognize_picture', diff --git a/database/factories/SafetyAndPrivacyFactory.php b/database/factories/SafetyAndPrivacyFactory.php new file mode 100644 index 00000000..32081b72 --- /dev/null +++ b/database/factories/SafetyAndPrivacyFactory.php @@ -0,0 +1,23 @@ + + */ +class SafetyAndPrivacyFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'edare_shahri_name' => $this->faker->name, + ]; + } +} diff --git a/tests/Feature/V2/Dashboard/SafetyAndPrivacy/DeleteTest.php b/tests/Feature/V2/Dashboard/SafetyAndPrivacy/DeleteTest.php new file mode 100644 index 00000000..e42a9ef9 --- /dev/null +++ b/tests/Feature/V2/Dashboard/SafetyAndPrivacy/DeleteTest.php @@ -0,0 +1,51 @@ +create(); + $safetyAndPrivacy = SafetyAndPrivacy::factory()->create(); + + $response = $this->actingAs($user)->postJson("v2/safety_and_privacy/delete/{$safetyAndPrivacy->id}"); + + $response->assertForbidden(); + } + + public function test_can_delete_a_safety_and_privacy(): void + { + $safetyAndPrivacy = SafetyAndPrivacy::factory()->create(); + $logList = LogList::factory()->create([ + 'log_unique_code' => 1155 + ]); + $userActivityLog = UserActivityLog::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'delete-road-item' + ])) + ->create(); + + $response = $this->actingAs($user)->postJson("v2/safety_and_privacy/delete/{$safetyAndPrivacy->id}"); + + $response->assertStatus(200) + ->assertExactJson([ + 'status' => 'succeed' + ]); + } +} diff --git a/tests/Feature/V2/Dashboard/SafetyAndPrivacy/DetailTest.php b/tests/Feature/V2/Dashboard/SafetyAndPrivacy/DetailTest.php new file mode 100644 index 00000000..f8558f10 --- /dev/null +++ b/tests/Feature/V2/Dashboard/SafetyAndPrivacy/DetailTest.php @@ -0,0 +1,50 @@ +create(); + + $user = User::factory() + ->create(); + + $response = $this->actingAs($user)->getJson("v2/safety_and_privacy/{$safetyAndPrivacy->id}"); + + $response->assertStatus(200) + ->assertJsonStructure([ + 'id', + 'lat', + 'lon', + 'province_id', + 'province_fa', + 'city_id', + 'city_fa', + 'edare_shahri_id', + 'edare_shahri_name', + 'recognize_picture', + 'activity_date_time', + 'judiciary_document', + 'judiciary_document_upload_date', + 'action_picture', + 'info_id', + 'info_fa', + 'way_id' + ]); + } +} diff --git a/tests/Feature/V2/Dashboard/SafetyAndPrivacy/getAllDataToMapTest.php b/tests/Feature/V2/Dashboard/SafetyAndPrivacy/getAllDataToMapTest.php new file mode 100644 index 00000000..7d57d379 --- /dev/null +++ b/tests/Feature/V2/Dashboard/SafetyAndPrivacy/getAllDataToMapTest.php @@ -0,0 +1,41 @@ +faker->date; + $date_to = $this->faker->date; + + $user = User::factory() + ->create(); + + $response = $this->actingAs($user)->getJson(route('v2.safety_and_privacy.report.map', [ + 'date_from' => $date_from, + 'date_to' => $date_to + ])); + + $response->assertStatus(200) + ->assertJsonStructure([ + 'status', + 'count', + 'message', + 'data' + ]); + } +} diff --git a/tests/Feature/V2/Dashboard/SafetyAndPrivacy/operatorSecondStepStoreTest.php b/tests/Feature/V2/Dashboard/SafetyAndPrivacy/operatorSecondStepStoreTest.php new file mode 100644 index 00000000..e0c84968 --- /dev/null +++ b/tests/Feature/V2/Dashboard/SafetyAndPrivacy/operatorSecondStepStoreTest.php @@ -0,0 +1,210 @@ +create(); + $safetyAndPrivacy = SafetyAndPrivacy::factory()->create(); + + $response = $this->actingAs($user)->postJson("v2/safety_and_privacy/operator/second_step/{$safetyAndPrivacy->id}"); + + $response->assertForbidden(); + } + + public function test_user_should_not_have_city_id(): void + { + $edarateOstani = EdarateOstani::factory()->create(); + $safetyAndPrivacy = SafetyAndPrivacy::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'add-safety-and-privacy' + ])) + ->create([ + 'edarate_ostani_id' => $edarateOstani->id + ]); + + $response = $this->actingAs($user)->postJson("v2/safety_and_privacy/operator/second_step/{$safetyAndPrivacy->id}"); + + $response->assertStatus(400) + ->assertExactJson([ + 'message' => 'امکان ثبت این مورد برای ادارات استانی و ستادی وجود ندارد!' + ]); + } + + public function test_user_should_not_have_edarate_shahri_id(): void + { + $city = City::factory()->create(); + $safetyAndPrivacy = SafetyAndPrivacy::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'add-safety-and-privacy' + ])) + ->create([ + 'city_id' => $city->id + ]); + + $response = $this->actingAs($user)->postJson("v2/safety_and_privacy/operator/second_step/{$safetyAndPrivacy->id}"); + + $response->assertStatus(400) + ->assertExactJson([ + 'message' => 'اداره شهری برای شما در سامانه ثبت نشده است!' + ]); + } + + public function test_judiciary_document_is_required(): void + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + $safetyAndPrivacy = SafetyAndPrivacy::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'add-safety-and-privacy' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $response = $this->actingAs($user)->postJson("v2/safety_and_privacy/operator/second_step/{$safetyAndPrivacy->id}"); + + $response->assertUnprocessable() + ->assertInvalid([ + 'judiciary_document' => __('validation.required', ['attribute' => 'judiciary document']) + ]); + } + + public function test_judiciary_document_should_be_array(): void + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + $safetyAndPrivacy = SafetyAndPrivacy::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'add-safety-and-privacy' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $response = $this->actingAs($user)->postJson("v2/safety_and_privacy/operator/second_step/{$safetyAndPrivacy->id}", [ + 'judiciary_document' => $this->faker->name + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'judiciary_document' => __('validation.array', ['attribute' => 'judiciary document']) + ]); + } + + public function test_judiciary_document_file_should_be_file_format(): void + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + $safetyAndPrivacy = SafetyAndPrivacy::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'add-safety-and-privacy' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $response = $this->actingAs($user)->postJson("v2/safety_and_privacy/operator/second_step/{$safetyAndPrivacy->id}", [ + 'judiciary_document' => [ + ['text'] + ] + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'judiciary_document.0' => __('validation.file', ['attribute' => 'judiciary_document.0']) + ]); + } + + public function test_new_second_safety_and_privacy_can_be_stored(): void + { + $this->withOutExceptionHandling(); + + $infoItem = InfoItem::factory()->create([ + 'item' => 17 + ]); + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + $logList = LogList::factory()->create(); + $userActivityLog = UserActivityLog::factory()->create(); + $image = UploadedFile::fake()->create('image.jpg', 10); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'add-safety-and-privacy' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $data = [ + 'point' => '12,12', + 'info_id' => $infoItem->id, + 'activity_date' => $this->faker->date('Y-m-d'), + 'activity_time' => $this->faker->date('H:i'), + 'recognize_picture' => $image + ]; + + $mock = $this->mock(NominatimService::class, function (MockInterface $mock) { + $mock->shouldReceive('get_way_id_from_nominatim') + ->once() + ->andReturn(0); + }); + + $response = $this->actingAs($user)->postJson('v2/safety_and_privacy/operator/first_step', $data); + + $response->assertOk(); + + $this->assertDatabaseHas('safety_and_privacy', [ + 'lat' => 12, + 'lon' => 12, + 'operator_id' => $user->id, + 'operator_name' => $user->name, + 'province_id' => $user->province_id, + 'province_fa' => $user->province_fa, + 'info_id' => $infoItem->id, + 'city_id' => $city->id, + 'city_fa' => $user->city_fa, + 'activity_date_time' => $data['activity_date']." ".$data['activity_time'], + 'edare_shahri_id' => $edarateShari->id, + 'edare_shahri_name' => $edarateShari->name, + 'way_id' => 0 + ]); + } +} From 79273622299f00e0cf9dc5fc7537e84830c08227 Mon Sep 17 00:00:00 2001 From: amirghasempoor Date: Sat, 9 Mar 2024 16:14:14 +0330 Subject: [PATCH 2/3] third step test --- .../operatorSecondStepStoreTest.php | 47 +++-- .../operatorThirdStepStoreTest.php | 176 ++++++++++++++++++ 2 files changed, 197 insertions(+), 26 deletions(-) create mode 100644 tests/Feature/V2/Dashboard/SafetyAndPrivacy/operatorThirdStepStoreTest.php diff --git a/tests/Feature/V2/Dashboard/SafetyAndPrivacy/operatorSecondStepStoreTest.php b/tests/Feature/V2/Dashboard/SafetyAndPrivacy/operatorSecondStepStoreTest.php index e0c84968..44a8c550 100644 --- a/tests/Feature/V2/Dashboard/SafetyAndPrivacy/operatorSecondStepStoreTest.php +++ b/tests/Feature/V2/Dashboard/SafetyAndPrivacy/operatorSecondStepStoreTest.php @@ -15,6 +15,7 @@ use App\Services\NominatimService; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithFaker; use Illuminate\Http\UploadedFile; +use Illuminate\Support\Facades\Storage; use Mockery\MockInterface; use Tests\TestCase; @@ -151,16 +152,19 @@ class operatorSecondStepStoreTest extends TestCase ]); } - public function test_new_second_safety_and_privacy_can_be_stored(): void + public function test_second_safety_and_privacy(): void { $this->withOutExceptionHandling(); + $safetyAndPrivacy = SafetyAndPrivacy::factory()->create(); $infoItem = InfoItem::factory()->create([ 'item' => 17 ]); $city = City::factory()->create(); $edarateShari = EdarateShahri::factory()->create(); - $logList = LogList::factory()->create(); + $logList = LogList::factory()->create([ + 'log_unique_code' => 1135, + ]); $userActivityLog = UserActivityLog::factory()->create(); $image = UploadedFile::fake()->create('image.jpg', 10); @@ -174,37 +178,28 @@ class operatorSecondStepStoreTest extends TestCase ]); $data = [ - 'point' => '12,12', - 'info_id' => $infoItem->id, - 'activity_date' => $this->faker->date('Y-m-d'), - 'activity_time' => $this->faker->date('H:i'), - 'recognize_picture' => $image + 'judiciary_document' => [ + $image + ] ]; - $mock = $this->mock(NominatimService::class, function (MockInterface $mock) { - $mock->shouldReceive('get_way_id_from_nominatim') - ->once() - ->andReturn(0); - }); + Storage::fake(); - $response = $this->actingAs($user)->postJson('v2/safety_and_privacy/operator/first_step', $data); + $response = $this->actingAs($user)->postJson("v2/safety_and_privacy/operator/second_step/{$safetyAndPrivacy->id}", $data); $response->assertOk(); $this->assertDatabaseHas('safety_and_privacy', [ - 'lat' => 12, - 'lon' => 12, - 'operator_id' => $user->id, - 'operator_name' => $user->name, - 'province_id' => $user->province_id, - 'province_fa' => $user->province_fa, - 'info_id' => $infoItem->id, - 'city_id' => $city->id, - 'city_fa' => $user->city_fa, - 'activity_date_time' => $data['activity_date']." ".$data['activity_time'], - 'edare_shahri_id' => $edarateShari->id, - 'edare_shahri_name' => $edarateShari->name, - 'way_id' => 0 + 'operator_id' => $safetyAndPrivacy->operator_id, + 'operator_name' => $safetyAndPrivacy->name, + 'province_id' => $safetyAndPrivacy->province_id, + 'province_fa' => $safetyAndPrivacy->province_fa, + 'info_id' => $safetyAndPrivacy->info_id, + 'city_id' => $safetyAndPrivacy->city_id, + 'city_fa' => $safetyAndPrivacy->city_fa, + 'edare_shahri_id' => $safetyAndPrivacy->edare_shahri_id, + 'edare_shahri_name' => $safetyAndPrivacy->edare_shahri_name, + 'way_id' => $safetyAndPrivacy->way_id ]); } } diff --git a/tests/Feature/V2/Dashboard/SafetyAndPrivacy/operatorThirdStepStoreTest.php b/tests/Feature/V2/Dashboard/SafetyAndPrivacy/operatorThirdStepStoreTest.php new file mode 100644 index 00000000..d73b034a --- /dev/null +++ b/tests/Feature/V2/Dashboard/SafetyAndPrivacy/operatorThirdStepStoreTest.php @@ -0,0 +1,176 @@ +create(); + $safetyAndPrivacy = SafetyAndPrivacy::factory()->create(); + + $response = $this->actingAs($user)->postJson("v2/safety_and_privacy/operator/third_step/{$safetyAndPrivacy->id}"); + + $response->assertForbidden(); + } + + public function test_user_should_not_have_city_id(): void + { + $edarateOstani = EdarateOstani::factory()->create(); + $safetyAndPrivacy = SafetyAndPrivacy::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'add-safety-and-privacy' + ])) + ->create([ + 'edarate_ostani_id' => $edarateOstani->id + ]); + + $response = $this->actingAs($user)->postJson("v2/safety_and_privacy/operator/third_step/{$safetyAndPrivacy->id}"); + + $response->assertStatus(400) + ->assertExactJson([ + 'message' => 'امکان ثبت این مورد برای ادارات استانی و ستادی وجود ندارد!' + ]); + } + + public function test_user_should_not_have_edarate_shahri_id(): void + { + $city = City::factory()->create(); + $safetyAndPrivacy = SafetyAndPrivacy::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'add-safety-and-privacy' + ])) + ->create([ + 'city_id' => $city->id + ]); + + $response = $this->actingAs($user)->postJson("v2/safety_and_privacy/operator/third_step/{$safetyAndPrivacy->id}"); + + $response->assertStatus(400) + ->assertExactJson([ + 'message' => 'اداره شهری برای شما در سامانه ثبت نشده است!' + ]); + } + + public function test_action_picture_is_required(): void + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + $safetyAndPrivacy = SafetyAndPrivacy::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'add-safety-and-privacy' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $response = $this->actingAs($user)->postJson("v2/safety_and_privacy/operator/third_step/{$safetyAndPrivacy->id}"); + + $response->assertUnprocessable() + ->assertInvalid([ + 'action_picture' => __('validation.required', ['attribute' => 'action picture']) + ]); + } + + public function test_action_picture_should_be_image(): void + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + $safetyAndPrivacy = SafetyAndPrivacy::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'add-safety-and-privacy' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $response = $this->actingAs($user)->postJson("v2/safety_and_privacy/operator/third_step/{$safetyAndPrivacy->id}", [ + 'action_picture' => $this->faker->name + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'action_picture' => __('validation.image', ['attribute' => 'action picture']) + ]); + } + + public function test_third_safety_and_privacy(): void + { + $this->withOutExceptionHandling(); + + $safetyAndPrivacy = SafetyAndPrivacy::factory()->create(); + $infoItem = InfoItem::factory()->create([ + 'item' => 17 + ]); + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + $logList = LogList::factory()->create([ + 'log_unique_code' => 1136, + ]); + $userActivityLog = UserActivityLog::factory()->create(); + $image = UploadedFile::fake()->create('image.jpg', 10); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'add-safety-and-privacy' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $data = [ + 'action_picture' => $image + ]; + + Storage::fake(); + + $response = $this->actingAs($user)->postJson("v2/safety_and_privacy/operator/third_step/{$safetyAndPrivacy->id}", $data); + + $response->assertOk(); + + $this->assertDatabaseHas('safety_and_privacy', [ + 'operator_id' => $safetyAndPrivacy->operator_id, + 'operator_name' => $safetyAndPrivacy->name, + 'province_id' => $safetyAndPrivacy->province_id, + 'province_fa' => $safetyAndPrivacy->province_fa, + 'info_id' => $safetyAndPrivacy->info_id, + 'city_id' => $safetyAndPrivacy->city_id, + 'city_fa' => $safetyAndPrivacy->city_fa, + 'edare_shahri_id' => $safetyAndPrivacy->edare_shahri_id, + 'edare_shahri_name' => $safetyAndPrivacy->edare_shahri_name, + 'way_id' => $safetyAndPrivacy->way_id + ]); + } +} From 8a662912e30acb4214d3cf3c0913c216342ce95f Mon Sep 17 00:00:00 2001 From: amirghasempoor Date: Sun, 10 Mar 2024 14:18:24 +0330 Subject: [PATCH 3/3] write test for road item projects --- app/Models/ObservedItem.php | 2 + app/Models/Province.php | 2 + app/Models/RoadItemsProject.php | 2 + database/factories/ObservedItemFactory.php | 23 + database/factories/ProvinceFactory.php | 23 + .../factories/RoadItemsProjectFactory.php | 24 + database/factories/UserFactory.php | 5 +- .../Dashboard/RoadItemsProject/DeleteTest.php | 74 +++ .../RoadItemsProject/RestoreTest.php | 94 +++ .../Dashboard/RoadItemsProject/StoreTest.php | 627 ++++++++++++++++++ .../Dashboard/RoadItemsProject/UpdateTest.php | 255 +++++++ 11 files changed, 1130 insertions(+), 1 deletion(-) create mode 100644 database/factories/ObservedItemFactory.php create mode 100644 database/factories/ProvinceFactory.php create mode 100644 database/factories/RoadItemsProjectFactory.php create mode 100644 tests/Feature/V2/Dashboard/RoadItemsProject/DeleteTest.php create mode 100644 tests/Feature/V2/Dashboard/RoadItemsProject/RestoreTest.php create mode 100644 tests/Feature/V2/Dashboard/RoadItemsProject/StoreTest.php create mode 100644 tests/Feature/V2/Dashboard/RoadItemsProject/UpdateTest.php diff --git a/app/Models/ObservedItem.php b/app/Models/ObservedItem.php index c63f371b..c0a4c049 100644 --- a/app/Models/ObservedItem.php +++ b/app/Models/ObservedItem.php @@ -2,10 +2,12 @@ namespace App\Models; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class ObservedItem extends Model { + use HasFactory; protected $guarded = ['id']; public function roadPatrol() diff --git a/app/Models/Province.php b/app/Models/Province.php index 510b682c..0c803401 100644 --- a/app/Models/Province.php +++ b/app/Models/Province.php @@ -2,10 +2,12 @@ namespace App\Models; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Province extends Model { + use HasFactory; /** * The attributes that should be hidden for arrays. * diff --git a/app/Models/RoadItemsProject.php b/app/Models/RoadItemsProject.php index 25d530bb..ff416de9 100644 --- a/app/Models/RoadItemsProject.php +++ b/app/Models/RoadItemsProject.php @@ -2,11 +2,13 @@ namespace App\Models; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Http\UploadedFile; use Illuminate\Database\Eloquent\Model; class RoadItemsProject extends Model { + use HasFactory; ///start_way_id ///end_way_id diff --git a/database/factories/ObservedItemFactory.php b/database/factories/ObservedItemFactory.php new file mode 100644 index 00000000..d5169928 --- /dev/null +++ b/database/factories/ObservedItemFactory.php @@ -0,0 +1,23 @@ + + */ +class ObservedItemFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'item_name' => $this->faker->name + ]; + } +} diff --git a/database/factories/ProvinceFactory.php b/database/factories/ProvinceFactory.php new file mode 100644 index 00000000..b566938c --- /dev/null +++ b/database/factories/ProvinceFactory.php @@ -0,0 +1,23 @@ + + */ +class ProvinceFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'name_fa' => $this->faker->name + ]; + } +} diff --git a/database/factories/RoadItemsProjectFactory.php b/database/factories/RoadItemsProjectFactory.php new file mode 100644 index 00000000..b6543430 --- /dev/null +++ b/database/factories/RoadItemsProjectFactory.php @@ -0,0 +1,24 @@ + + */ +class RoadItemsProjectFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'item' => $this->faker->numberBetween(1, 10), + 'sub_item' => $this->faker->numberBetween(1, 10), + ]; + } +} diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index f54ede76..e0cbc784 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -18,8 +18,11 @@ class UserFactory extends Factory { return [ 'username' => $this->faker->name, + 'name' => $this->faker->name, 'enabled' => true, - 'confirmed' => 1 + 'confirmed' => 1, + 'province_fa' => $this->faker->name, + 'city_fa' => $this->faker->name, ]; } } diff --git a/tests/Feature/V2/Dashboard/RoadItemsProject/DeleteTest.php b/tests/Feature/V2/Dashboard/RoadItemsProject/DeleteTest.php new file mode 100644 index 00000000..e39ad7a0 --- /dev/null +++ b/tests/Feature/V2/Dashboard/RoadItemsProject/DeleteTest.php @@ -0,0 +1,74 @@ +create(); + + $roadItem = RoadItemsProject::factory()->create(); + + $response = $this->actingAs($user)->postJson("/v2/road_items/supervisor/cartable/delete/{$roadItem->id}"); + + $response->assertForbidden(); + } + + public function test_can_delete_a_road_item_project(): void + { + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'delete-road-item' + ])) + ->create(); + + $roadItem = RoadItemsProject::factory()->create(); + + $logList = LogList::factory()->create([ + 'log_unique_code' => 1151 + ]); + + $userActivityLog = UserActivityLog::factory()->create(); + + $response = $this->actingAs($user)->postJson("/v2/road_items/supervisor/cartable/delete/{$roadItem->id}"); + + $response->assertOk() + ->assertExactJson([ + 'status' => 'succeed', + ]); + + $this->assertDatabaseMissing('road_items_projects', [ + 'item' => $roadItem->item, + 'sub_item' => $roadItem->sub_item, + 'start_lat' => $roadItem->start_lat, + 'start_lng' => $roadItem->start_lng, + 'sub_item_data' => $roadItem->sub_item_data, + 'status' => $roadItem->status, + 'status_fa' => $roadItem->status_fa, + ]); + } +} diff --git a/tests/Feature/V2/Dashboard/RoadItemsProject/RestoreTest.php b/tests/Feature/V2/Dashboard/RoadItemsProject/RestoreTest.php new file mode 100644 index 00000000..7a4634b4 --- /dev/null +++ b/tests/Feature/V2/Dashboard/RoadItemsProject/RestoreTest.php @@ -0,0 +1,94 @@ +create(); + + $roadItem = RoadItemsProject::factory()->create(); + + $response = $this->actingAs($user)->postJson("/v2/road_items/supervisor/cartable/restore/{$roadItem->id}"); + + $response->assertForbidden(); + } + + public function test_can_not_restore_if_status_is_0(): void + { + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'restore-road-item' + ]))->create(); + + $roadItem = RoadItemsProject::factory()->create([ + 'status' => 0 + ]); + + $response = $this->actingAs($user)->postJson("/v2/road_items/supervisor/cartable/restore/{$roadItem->id}"); + + $response->assertStatus(400) + ->assertExactJson([ + 'message' => 'امکان بازگردانی وضعیت این فعالیت وجود ندارد!' + ]); + } + + public function test_can_delete_a_road_item_project(): void + { + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'restore-road-item' + ])) + ->create(); + + $roadItem = RoadItemsProject::factory()->create([ + 'status' => 1 + ]); + + $logList = LogList::factory()->create([ + 'log_unique_code' => 1150 + ]); + + $userActivityLog = UserActivityLog::factory()->create(); + + $response = $this->actingAs($user)->postJson("/v2/road_items/supervisor/cartable/restore/{$roadItem->id}"); + + $response->assertOk() + ->assertExactJson([ + 'status' => 'succeed', + ]); + + $this->assertDatabaseHas('road_items_projects', [ + 'status' => 0, + 'status_fa' => 'در حال بررسی', + 'supervisor_id' => null, + 'supervisor_description' => null, + 'supervising_time' => null, + 'supervisor_name' => null, + ]); + } +} diff --git a/tests/Feature/V2/Dashboard/RoadItemsProject/StoreTest.php b/tests/Feature/V2/Dashboard/RoadItemsProject/StoreTest.php new file mode 100644 index 00000000..f07dead9 --- /dev/null +++ b/tests/Feature/V2/Dashboard/RoadItemsProject/StoreTest.php @@ -0,0 +1,627 @@ +create(); + + $response = $this->actingAs($user)->postJson('/v2/road_items/operator/store'); + + $response->assertForbidden(); + } + + public function test_user_should_not_have_city_id(): void + { + $edarateOstani = EdarateOstani::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'create-road-item' + ])) + ->create([ + 'edarate_ostani_id' => $edarateOstani->id + ]); + + $response = $this->actingAs($user)->postJson('/v2/road_items/operator/store'); + + $response->assertStatus(400) + ->assertExactJson([ + 'message' => 'امکان ثبت فعالیت برای ادارات استانی و ستادی وجود ندارد!' + ]); + } + + public function test_user_should_not_have_edarate_shahri_id(): void + { + $city = City::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'create-road-item' + ])) + ->create([ + 'city_id' => $city->id + ]); + + $response = $this->actingAs($user)->postJson('/v2/road_items/operator/store'); + + $response->assertStatus(400) + ->assertExactJson([ + 'message' => 'اداره شهری برای شما در سامانه ثبت نشده است!' + ]); + } + + public function test_start_point_is_required(): void + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'create-road-item' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $response = $this->actingAs($user)->postJson('/v2/road_items/operator/store'); + + $response->assertUnprocessable() + ->assertInvalid([ + 'start_point' => __('validation.required', ['attribute' => 'start point']) + ]); + } + + public function test_item_id_is_required(): void + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'create-road-item' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $response = $this->actingAs($user)->postJson('/v2/road_items/operator/store'); + + $response->assertUnprocessable() + ->assertInvalid([ + 'item_id' => __('validation.required', ['attribute' => 'item id']) + ]); + } + + public function test_item_id_should_be_integer(): void + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'create-road-item' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $response = $this->actingAs($user)->postJson('/v2/road_items/operator/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 + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'create-road-item' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $response = $this->actingAs($user)->postJson('/v2/road_items/operator/store'); + + $response->assertUnprocessable() + ->assertInvalid([ + 'sub_item_id' => __('validation.required', ['attribute' => 'sub item id']) + ]); + } + + public function test_sub_item_id_should_be_integer(): void + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'create-road-item' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $response = $this->actingAs($user)->postJson('/v2/road_items/operator/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 + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'create-road-item' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $response = $this->actingAs($user)->postJson('/v2/road_items/operator/store'); + + $response->assertUnprocessable() + ->assertInvalid([ + 'amount' => __('validation.required', ['attribute' => 'amount']) + ]); + } + + public function test_amount_should_be_numeric(): void + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'create-road-item' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $response = $this->actingAs($user)->postJson('/v2/road_items/operator/store', [ + 'amount' => $this->faker->name + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'amount' => __('validation.numeric', ['attribute' => 'amount']) + ]); + } + + public function test_before_image_should_be_image(): void + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'create-road-item' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $response = $this->actingAs($user)->postJson('/v2/road_items/operator/store', [ + 'before_image' => $this->faker->randomNumber() + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'before_image' => __('validation.image', ['attribute' => 'before image']) + ]); + } + + public function test_before_image_should_be_less_than_4096kb(): void + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'create-road-item' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $image = UploadedFile::fake()->create('image.jpg', 5096); + + $response = $this->actingAs($user)->postJson('/v2/road_items/operator/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 + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'create-road-item' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $response = $this->actingAs($user)->postJson('/v2/road_items/operator/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 + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'create-road-item' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $image = UploadedFile::fake()->create('image.jpg', 5096); + + $response = $this->actingAs($user)->postJson('/v2/road_items/operator/store', [ + 'after_image' => $image + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'after_image' => __('validation.max.file', ['attribute' => 'after image', 'max' => 4096]) + ]); + } + + public function test_observed_item_id_should_be_integer(): void + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'create-road-item' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $response = $this->actingAs($user)->postJson('/v2/road_items/operator/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 + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + $observedItem = ObservedItem::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'create-road-item' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $response = $this->actingAs($user)->postJson('/v2/road_items/operator/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 + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'create-road-item' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $response = $this->actingAs($user)->postJson('/v2/road_items/operator/store'); + + $response->assertUnprocessable() + ->assertInvalid([ + 'activity_date' => __('validation.required', ['attribute' => 'تاریخ فعالیت']) + ]); + } + + public function test_activity_date_should_match_the_format(): void + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'create-road-item' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $response = $this->actingAs($user)->postJson('/v2/road_items/operator/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 + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'create-road-item' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $response = $this->actingAs($user)->postJson('/v2/road_items/operator/store'); + + $response->assertUnprocessable() + ->assertInvalid([ + 'activity_time' => __('validation.required', ['attribute' => 'ساعت فعالیت']) + ]); + } + + public function test_activity_time_should_match_the_format(): void + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'create-road-item' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $response = $this->actingAs($user)->postJson('/v2/road_items/operator/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_validation_exception_will_throw_if_needs_end_point_is_not_null(): void + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + $item_id = $this->faker->numberBetween(1, 10); + $sub_item_id = $this->faker->numberBetween(1, 10); + $infoItem = InfoItem::factory()->create([ + 'item' => $item_id, + 'sub_item' => $sub_item_id, + 'needs_end_point' => $this->faker->numberBetween(1, 10) + ]); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'create-road-item' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $response = $this->actingAs($user)->postJson('/v2/road_items/operator/store', [ + 'start_point' => $this->faker->randomNumber(), + 'item_id' => $item_id, + 'sub_item_id' => $sub_item_id, + 'amount' => $this->faker->numberBetween(1, 10), + 'activity_date' => $this->faker->date(), + 'activity_time' => $this->faker->date('H:i'), + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'end_point' => __('validation.required', ['attribute' => 'end_point']) + ]); + } + + public function test_validation_exception_will_throw_if_needs_image_is_not_null(): void + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + $item_id = $this->faker->numberBetween(1, 10); + $sub_item_id = $this->faker->numberBetween(1, 10); + $infoItem = InfoItem::factory()->create([ + 'item' => $item_id, + 'sub_item' => $sub_item_id, + 'needs_image' => $this->faker->numberBetween(1, 10) + ]); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'create-road-item' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $response = $this->actingAs($user)->postJson('/v2/road_items/operator/store', [ + 'start_point' => $this->faker->randomNumber(), + 'item_id' => $item_id, + 'sub_item_id' => $sub_item_id, + 'amount' => $this->faker->numberBetween(1, 10), + 'activity_date' => $this->faker->date(), + 'activity_time' => $this->faker->date('H:i'), + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'before_image' => __('validation.required', ['attribute' => 'before_image']) + ]); + + $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(); + $item_id = $this->faker->numberBetween(1, 10); + $sub_item_id = $this->faker->numberBetween(1, 10); + $infoItem = InfoItem::factory()->create([ + 'item' => $item_id, + 'sub_item' => $sub_item_id, + 'needs_image' => 1 + ]); + + $logList = LogList::factory()->create([ + 'log_unique_code' => 1154 + ]); + $userActivityLog = 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 + ]); + + $before_image = UploadedFile::fake()->create('image.jpg', 10); + $after_image = UploadedFile::fake()->create('image.jpg', 10); + + Storage::fake(); + + $mock = $this->mock(NominatimService::class, function (MockInterface $mock) { + $mock->shouldReceive('get_way_id_from_nominatim') + ->once() + ->andReturn(0); + }); + + $response = $this->actingAs($user)->postJson('/v2/road_items/operator/store', [ + 'start_point' => '12,12', + 'item_id' => $item_id, + 'sub_item_id' => $sub_item_id, + 'amount' => $this->faker->numberBetween(1, 10), + 'activity_date' => $this->faker->date(), + 'activity_time' => $this->faker->date('H:i'), + 'before_image' => $before_image, + 'after_image' => $after_image + ]); + + $response->assertOk() + ->assertExactJson([ + 'message' => 'succussful' + ]); + + $this->assertDatabaseHas('road_items_projects', [ + 'start_lat' => 12, + 'start_lng' => 12, + 'item' => $item_id, + 'sub_item' => $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, + ]); + } +} diff --git a/tests/Feature/V2/Dashboard/RoadItemsProject/UpdateTest.php b/tests/Feature/V2/Dashboard/RoadItemsProject/UpdateTest.php new file mode 100644 index 00000000..6a8bf7a1 --- /dev/null +++ b/tests/Feature/V2/Dashboard/RoadItemsProject/UpdateTest.php @@ -0,0 +1,255 @@ +create(); + $user2 = User::factory()->create(); + + $roadItem = RoadItemsProject::factory()->create([ + 'status' => $this->faker->numberBetween(3, 6), + 'user_id' => $user2->id, + ]); + + $response = $this->actingAs($user1)->postJson("/v2/road_items/operator/update/{$roadItem->id}"); + + $response->assertForbidden(); + } + + public function test_start_point_is_required(): void + { + $user = User::factory() + ->create(); + + $roadItem = RoadItemsProject::factory()->create([ + 'status' => 2, + 'user_id' => $user->id + ]); + + $response = $this->actingAs($user)->postJson("/v2/road_items/operator/update/{$roadItem->id}"); + + $response->assertUnprocessable() + ->assertInvalid([ + 'start_point' => __('validation.required', ['attribute' => 'start point']) + ]); + } + + public function test_amount_is_required(): void + { + $user = User::factory() + ->create(); + + $roadItem = RoadItemsProject::factory()->create([ + 'status' => 2, + 'user_id' => $user->id + ]); + + $response = $this->actingAs($user)->postJson("/v2/road_items/operator/update/{$roadItem->id}"); + + $response->assertUnprocessable() + ->assertInvalid([ + 'amount' => __('validation.required', ['attribute' => 'amount']) + ]); + } + + public function test_amount_should_be_numeric(): void + { + $user = User::factory() + ->create(); + + $roadItem = RoadItemsProject::factory()->create([ + 'status' => 2, + 'user_id' => $user->id + ]); + + $response = $this->actingAs($user)->postJson("/v2/road_items/operator/update/{$roadItem->id}", [ + 'amount' => $this->faker->name + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'amount' => __('validation.numeric', ['attribute' => 'amount']) + ]); + } + + public function test_before_image_should_be_image(): void + { + $user = User::factory() + ->create(); + + $roadItem = RoadItemsProject::factory()->create([ + 'status' => 2, + 'user_id' => $user->id + ]); + + $response = $this->actingAs($user)->postJson("/v2/road_items/operator/update/{$roadItem->id}", [ + 'before_image' => $this->faker->randomNumber() + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'before_image' => __('validation.image', ['attribute' => 'before image']) + ]); + } + + public function test_before_image_should_be_less_than_4096kb(): void + { + $user = User::factory() + ->create(); + + $roadItem = RoadItemsProject::factory()->create([ + 'status' => 2, + 'user_id' => $user->id + ]); + + $image = UploadedFile::fake()->create('image.jpg', 5096); + + $response = $this->actingAs($user)->postJson("/v2/road_items/operator/update/{$roadItem->id}", [ + '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() + ->create(); + + $roadItem = RoadItemsProject::factory()->create([ + 'status' => 2, + 'user_id' => $user->id + ]); + + $response = $this->actingAs($user)->postJson("/v2/road_items/operator/update/{$roadItem->id}", [ + '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() + ->create(); + + $roadItem = RoadItemsProject::factory()->create([ + 'status' => 2, + 'user_id' => $user->id + ]); + + $image = UploadedFile::fake()->create('image.jpg', 5096); + + $response = $this->actingAs($user)->postJson("/v2/road_items/operator/update/{$roadItem->id}", [ + 'after_image' => $image + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'after_image' => __('validation.max.file', ['attribute' => 'after image', 'max' => 4096]) + ]); + } + + public function test_validation_exception_will_throw_if_needs_end_point_is_not_null(): void + { + $user = User::factory() + ->create(); + + $roadItem = RoadItemsProject::factory()->create([ + 'status' => 2, + 'user_id' => $user->id + ]); + + $infoItem = InfoItem::factory()->create([ + 'item' => $roadItem->item, + 'sub_item' => $roadItem->sub_item, + 'needs_end_point' => $this->faker->numberBetween(1, 10), + ]); + + $response = $this->actingAs($user)->postJson("/v2/road_items/operator/update/{$roadItem->id}", [ + 'start_point' => $this->faker->numberBetween(1, 10), + 'amount' => $this->faker->numberBetween(1, 10), + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'end_point' => __('validation.required', ['attribute' => 'end_point']) + ]); + } + + public function test_can_update_a_road_item_project(): void + { + $user = User::factory() + ->create(); + + $roadItem = RoadItemsProject::factory()->create([ + 'status' => 2, + 'user_id' => $user->id + ]); + + $infoItem = InfoItem::factory()->create([ + 'item' => $roadItem->item, + 'sub_item' => $roadItem->sub_item, + ]); + + $logList = LogList::factory()->create([ + 'log_unique_code' => 1155 + ]); + + $userActivityLog = UserActivityLog::factory()->create(); + + Storage::fake(); + + $amount = $this->faker->numberBetween(1, 10); + + $response = $this->actingAs($user)->postJson("/v2/road_items/operator/update/{$roadItem->id}", [ + 'start_point' => '12,12', + 'amount' => $amount, + ]); + + $response->assertOk() + ->assertExactJson([ + 'message' => 'succussful' + ]); + + $this->assertDatabaseHas('road_items_projects', [ + 'start_lat' => 12, + 'start_lng' => 12, + 'sub_item_data' => $amount, + 'status' => 0, + 'status_fa' => 'در حال بررسی', + ]); + } +}