diff --git a/app/Http/Controllers/V2/Dashboard/RoadPatrolProjectController.php b/app/Http/Controllers/V2/Dashboard/RoadPatrolProjectController.php index 19cfdc01..8dd482cc 100644 --- a/app/Http/Controllers/V2/Dashboard/RoadPatrolProjectController.php +++ b/app/Http/Controllers/V2/Dashboard/RoadPatrolProjectController.php @@ -477,6 +477,7 @@ class RoadPatrolProjectController extends Controller public function getAllDataToMap(Request $request) { +// dd($request->date_from, $request->date_to, $request->province_id, $request->edare_id); $date_from = $request->date_from ? $request->date_from .' 00:00:00' : Date('Y-m-d') .' 00:00:00'; $date_to = $request->date_to ? $request->date_to.' 23:59:59' : (new \DateTime('tomorrow'))->format('Y-m-d').' 23:59:59'; diff --git a/app/Models/RoadPatrol.php b/app/Models/RoadPatrol.php index 89d15445..d439e2d8 100644 --- a/app/Models/RoadPatrol.php +++ b/app/Models/RoadPatrol.php @@ -2,10 +2,13 @@ namespace App\Models; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class RoadPatrol extends Model { + use HasFactory; + protected $guarded = ['id']; public function observedItems() diff --git a/database/factories/RoadPatrolFactory.php b/database/factories/RoadPatrolFactory.php new file mode 100644 index 00000000..490cbfd1 --- /dev/null +++ b/database/factories/RoadPatrolFactory.php @@ -0,0 +1,23 @@ + + */ +class RoadPatrolFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'operator_name' => $this->faker->name + ]; + } +} diff --git a/tests/Feature/V2/Dashboard/RoadPatrolProject/DeleteTest.php b/tests/Feature/V2/Dashboard/RoadPatrolProject/DeleteTest.php new file mode 100644 index 00000000..f558652d --- /dev/null +++ b/tests/Feature/V2/Dashboard/RoadPatrolProject/DeleteTest.php @@ -0,0 +1,134 @@ +create(); + + $roadPatrol = RoadPatrol::factory()->create(); + + $response = $this->actingAs($user)->postJson("v2/road_patrols/supervisor/cartable/delete/{$roadPatrol->id}"); + + $response->assertUnprocessable() + ->assertInvalid([ + 'type' => __('validation.required', ['attribute' => 'type']) + ]); + } + + public function test_user_should_have_partial_delete_road_patrol_permission(): void + { + $user = User::factory()->create(); + + $roadPatrol = RoadPatrol::factory()->create(); + + $response = $this->actingAs($user)->postJson("v2/road_patrols/supervisor/cartable/delete/{$roadPatrol->id}", [ + 'type' => 1 + ]); + + $response->assertForbidden(); + } + + public function test_user_can_partial_delete_a_observed_item_and_road_patrol(): void + { + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'partial-delete-road-patrol' + ])) + ->create(); + + $roadPatrol = RoadPatrol::factory()->create(); + $observedItems = ObservedItem::factory()->create([ + 'road_patrol_id' => $roadPatrol->id + ]); + + $response = $this->actingAs($user)->postJson("v2/road_patrols/supervisor/cartable/delete/{$roadPatrol->id}", [ + 'type' => 1 + ]); + + $response->assertStatus(200) + ->assertExactJson([ + 'message' => 'succussful' + ]); + + $this->assertDatabaseMissing('road_patrols', [ + 'id' => $roadPatrol->id, + 'operator_name' => $roadPatrol->operator_name, + 'operator_id' => $roadPatrol->operator_id, + ]); + + $this->assertDatabaseMissing('observed_items', [ + 'item_name' => $observedItems->item_name, + 'road_patrol_id' => $observedItems->road_patrol_id, + ]); + } + + public function test_user_should_have_complete_delete_road_patrol_permission(): void + { + $user = User::factory()->create(); + + $roadPatrol = RoadPatrol::factory()->create(); + + $response = $this->actingAs($user)->postJson("v2/road_patrols/supervisor/cartable/delete/{$roadPatrol->id}", [ + 'type' => 2 + ]); + + $response->assertForbidden(); + } + + public function test_user_can_complete_delete_a_observed_item_and_road_patrol(): void + { + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'complete-delete-road-patrol' + ])) + ->create(); + + $roadPatrol = RoadPatrol::factory()->create(); + $logList = LogList::factory()->create([ + 'log_unique_code' => 1144 + ]); + $observedItems = ObservedItem::factory()->create([ + 'road_patrol_id' => $roadPatrol->id + ]); + + $response = $this->actingAs($user)->postJson("v2/road_patrols/supervisor/cartable/delete/{$roadPatrol->id}", [ + 'type' => 2 + ]); + + $response->assertStatus(200) + ->assertExactJson([ + 'message' => 'succussful' + ]); + + $this->assertDatabaseMissing('road_patrols', [ + 'id' => $roadPatrol->id, + 'operator_name' => $roadPatrol->operator_name, + 'operator_id' => $roadPatrol->operator_id, + ]); + + $this->assertDatabaseMissing('observed_items', [ + 'item_name' => $observedItems->item_name, + 'road_patrol_id' => $observedItems->road_patrol_id, + ]); + } +} diff --git a/tests/Feature/V2/Dashboard/RoadPatrolProject/GetAllDataToMapTest.php b/tests/Feature/V2/Dashboard/RoadPatrolProject/GetAllDataToMapTest.php new file mode 100644 index 00000000..f7e04406 --- /dev/null +++ b/tests/Feature/V2/Dashboard/RoadPatrolProject/GetAllDataToMapTest.php @@ -0,0 +1,95 @@ +create(); + + RoadPatrol::factory() + ->sequence( + ['id' => 1], + ['id' => 2], + ['id' => 3], + ['id' => 4], + ['id' => 5], + ['id' => 6], + ['id' => 7], + ) + ->count(7) + ->create(); + + $response = $this->actingAs($user)->getJson('/v2/road_patrols/report/map'); + + $response->assertStatus(200) + ->assertExactJson([ + 'status' => 'succeed', + 'count' => 7, + 'message' => '', + 'data' => [ + ["id" => 1, "start_lat" => 0, "start_lon" => 0], + ["id" => 2, "start_lat" => 0, "start_lon" => 0], + ["id" => 3, "start_lat" => 0, "start_lon" => 0], + ["id" => 4, "start_lat" => 0, "start_lon" => 0], + ["id" => 5, "start_lat" => 0, "start_lon" => 0], + ["id" => 6, "start_lat" => 0, "start_lon" => 0], + ["id" => 7, "start_lat" => 0, "start_lon" => 0], + ] + ]); + } + + public function test_all_data_can_be_seen_with_filter(): void + { + $user = User::factory()->create(); + $province = Province::factory()->create(); + + RoadPatrol::factory() + ->sequence( + ['created_at' => '2022-05-03 12:25:00', 'province_id' => $province->id, 'edare_id' => 1], + ['created_at' => '2025-05-03 12:25:00', 'province_id' => $province->id, 'edare_id' => 5], + ['created_at' => '2022-05-03 12:25:00', 'province_id' => $province->id, 'edare_id' => 9], + ['created_at' => '2026-05-03 12:25:00', 'province_id' => $province->id, 'edare_id' => 2], + ['created_at' => '2022-05-03 12:25:00', 'province_id' => $province->id, 'edare_id' => 6], + ['created_at' => '2026-08-03 12:25:00', 'province_id' => $province->id, 'edare_id' => 16], + ['created_at' => '2022-05-03 12:25:00', 'province_id' => $province->id, 'edare_id' => 35], + ) + ->count(7) + ->create(); + +// $response = $this->actingAs($user)->getJson(route('v2.road_patrols.report.map', [ +// 'date_from' => '2021-05-02', +// 'date_to' => '2024-09-01', +// 'province_id' => 1, +// 'edare_id' => 1, +// ])); + + $response = $this->actingAs($user)->getJson('/v2/road_patrols/report/map?date_from=2021-05-02&date_to=2024-09-01&province_id=1&edare_id=1'); + + $response->assertStatus(200) + ->assertExactJson([ + 'status' => 'succeed', + 'count' => 1, + 'message' => '', + 'data' => [ + [ + "id" => 1, + "start_lat" => 0, + "start_lon" => 0 + ] + ] + ]); + } +} diff --git a/tests/Feature/V2/Dashboard/RoadPatrolProject/StoreTest.php b/tests/Feature/V2/Dashboard/RoadPatrolProject/StoreTest.php new file mode 100644 index 00000000..7d5b986f --- /dev/null +++ b/tests/Feature/V2/Dashboard/RoadPatrolProject/StoreTest.php @@ -0,0 +1,1072 @@ +create([ + 'name' => 'add-road-patrol' + ]); + + $user = User::factory()->create(); + + $response = $this->actingAs($user)->postJson('v2/road_patrols/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' => 'add-road-patrol' + ])) + ->create([ + 'edarate_ostani_id' => $edarateOstani->id + ]); + + $response = $this->actingAs($user)->postJson('v2/road_patrols/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' => 'add-road-patrol' + ])) + ->create([ + 'city_id' => $city->id + ]); + + $response = $this->actingAs($user)->postJson('v2/road_patrols/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' => 'add-road-patrol' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store'); + + $response->assertUnprocessable() + ->assertInvalid([ + 'start_point' => __('validation.required', ['attribute' => 'start point']) + ]); + } + + public function test_end_point_is_required(): void + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'add-road-patrol' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store'); + + $response->assertUnprocessable() + ->assertInvalid([ + 'end_point' => __('validation.required', ['attribute' => 'end point']) + ]); + } + + public function test_distance_should_be_numeric(): void + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'add-road-patrol' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store', [ + 'distance' => $this->faker->name + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'distance' => __('validation.numeric', ['attribute' => 'distance']) + ]); + } + + public function test_officer_name_is_required(): void + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'add-road-patrol' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store'); + + $response->assertUnprocessable() + ->assertInvalid([ + 'officer_name' => __('validation.required', ['attribute' => 'officer name']) + ]); + } + + public function test_officer_plaque_is_required(): void + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'add-road-patrol' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store'); + + $response->assertUnprocessable() + ->assertInvalid([ + 'officer_plaque' => __('validation.required', ['attribute' => 'officer plaque']) + ]); + } + + public function test_officer_phone_number_is_required(): void + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'add-road-patrol' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store'); + + $response->assertUnprocessable() + ->assertInvalid([ + 'officer_phone_number' => __('validation.required', ['attribute' => 'officer phone number']) + ]); + } + + public function test_start_time_is_required(): void + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'add-road-patrol' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store'); + + $response->assertUnprocessable() + ->assertInvalid([ + 'start_time' => __('validation.required', ['attribute' => 'ساعت شروع گشت']) + ]); + } + + public function test_start_time_should_match_the_format(): void + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'add-road-patrol' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store', [ + 'start_time' => $this->faker->date('d-m-Y-H-i-s') + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'start_time' => __('validation.date_format', ['attribute' => 'ساعت شروع گشت', 'format' => 'Y-m-d H:i']) + ]); + } + + public function test_end_time_is_required(): void + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'add-road-patrol' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store'); + + $response->assertUnprocessable() + ->assertInvalid([ + 'end_time' => __('validation.required', ['attribute' => 'ساعت پایان گشت']) + ]); + } + + public function test_end_time_should_match_the_format(): void + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'add-road-patrol' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store', [ + 'end_time' => $this->faker->date('d-m-Y-H-i-s') + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'end_time' => __('validation.date_format', ['attribute' => 'ساعت پایان گشت', 'format' => 'Y-m-d H:i']) + ]); + } + + public function test_description_should_be_string(): void + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'add-road-patrol' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store', [ + 'description' => $this->faker->numberBetween(1, 10) + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'description' => __('validation.string', ['attribute' => 'توضیحات']) + ]); + } + + public function test_observed_items_should_be_array(): void + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'add-road-patrol' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store', [ + 'observed_items' => $this->faker->numberBetween(1, 10) + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'observed_items' => __('validation.array', ['attribute' => 'observed items']) + ]); + } + + public function test_observed_items_item_id_is_required(): void + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'add-road-patrol' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $observed_items = [ + [] + ]; + + $response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store', [ + 'observed_items' => $observed_items + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'observed_items.0.item_id' => __('validation.required', ['attribute' => 'observed_items.0.item_id']) + ]); + } + + public function test_observed_items_sub_item_id_is_required(): void + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'add-road-patrol' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $observed_items = [ + [] + ]; + + $response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store', [ + 'observed_items' => $observed_items + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'observed_items.0.sub_item_id' => __('validation.required', ['attribute' => 'observed_items.0.sub_item_id']) + ]); + } + + public function test_observed_items_local_name_is_required(): void + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'add-road-patrol' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $observed_items = [ + [] + ]; + + $response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store', [ + 'observed_items' => $observed_items + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'observed_items.0.local_name' => __('validation.required', ['attribute' => 'observed_items.0.local_name']) + ]); + } + + public function test_observed_items_instant_action_is_required(): void + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'add-road-patrol' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $observed_items = [ + [] + ]; + + $response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store', [ + 'observed_items' => $observed_items + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'observed_items.0.instant_action' => __('validation.required', ['attribute' => 'observed_items.0.instant_action']) + ]); + } + + public function test_observed_items_start_point_is_required(): void + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'add-road-patrol' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $observed_items = [ + [] + ]; + + $response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store', [ + 'observed_items' => $observed_items + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'observed_items.0.start_point' => __('validation.required', ['attribute' => 'observed_items.0.start_point']) + ]); + } + + public function test_observed_items_amount_should_be_numeric(): void + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'add-road-patrol' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $observed_items = [ + ['amount' => $this->faker->name] + ]; + + $response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store', [ + 'observed_items' => $observed_items + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'observed_items.0.amount' => __('validation.numeric', ['attribute' => 'observed_items.0.amount']) + ]); + } + + public function test_observed_items_before_image_should_be_image(): void + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'add-road-patrol' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $observed_items = [ + ['before_image' => $this->faker->name] + ]; + + $response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store', [ + 'observed_items' => $observed_items + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'observed_items.0.before_image' => __('validation.image', ['attribute' => 'observed_items.0.before_image']) + ]); + } + + public function test_observed_items_after_image_should_be_image(): void + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'add-road-patrol' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $observed_items = [ + ['after_image' => $this->faker->name] + ]; + + $response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store', [ + 'observed_items' => $observed_items + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'observed_items.0.after_image' => __('validation.image', ['attribute' => 'observed_items.0.after_image']) + ]); + } + + public function test_priority_is_required(): void + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'add-road-patrol' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $attributes = [ + 'start_point' => '12,12', + 'end_point' => '13,13', + 'officer_name' => $this->faker->name, + 'officer_plaque' => $this->faker->numberBetween(1, 10), + 'officer_phone_number' => $this->faker->numerify("###########"), + 'start_time' => '2016-01-23 11:53', + 'end_time' => '2017-01-23 11:53', + 'observed_items' => [ + [ + 'after_image' => UploadedFile::fake()->create('image.jpg', 10), + 'item_id' => $this->faker->randomNumber(), + 'sub_item_id' => $this->faker->randomNumber(), + 'local_name' => $this->faker->name(), + 'instant_action' => 0, + 'start_point' => '12,12', + ] + ] + ]; + + $infoItem = InfoItem::factory()->create([ + 'item' => $attributes['observed_items'][0]['item_id'], + 'sub_item' => $attributes['observed_items'][0]['sub_item_id'], + ]); + + $mock = $this->mock(NominatimService::class, function (MockInterface $mock) { + $mock->shouldReceive('get_way_id_from_nominatim')->andReturn(0); + }); + + $response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store', $attributes); + + $response->assertUnprocessable() + ->assertInvalid([ + "observed_items.0.priority" => __('validation.required', ['attribute' => 'priority']), + ]); + } + + public function test_can_store_a_road_patrol_when_instant_action_is_0(): void + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + $logList = LogList::factory()->create([ + 'log_unique_code' => 1147 + ]); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'add-road-patrol' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $attributes = [ + 'start_point' => '12,12', + 'end_point' => '13,13', + 'officer_name' => $this->faker->name, + 'officer_plaque' => $this->faker->numerify(), + 'officer_phone_number' => $this->faker->numerify("###########"), + 'start_time' => '2016-01-23 11:53', + 'end_time' => '2017-01-23 11:53', + 'observed_items' => [ + [ + 'after_image' => UploadedFile::fake()->create('image.jpg', 10), + 'item_id' => 1, + 'sub_item_id' => 1, + 'local_name' => $this->faker->name(), + 'instant_action' => 0, + 'start_point' => '12,12', + 'priority' => $this->faker->numberBetween(1, 3), + 'priority_fa' => $this->faker->name, + ] + ] + ]; + + $infoItem = InfoItem::factory()->create([ + 'item' => 1, + 'sub_item' => 1, + ]); + + $mock = $this->mock(NominatimService::class, function (MockInterface $mock) { + $mock->shouldReceive('get_way_id_from_nominatim')->andReturn(0); + }); + + Sms::shouldReceive('sendSms'); + + $response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store', $attributes); + + $response->assertOk(); + + $this->assertDatabaseHas('road_patrols', [ + 'start_lat' => 12, + 'start_lon' => 12, + 'end_lat' => 13, + 'end_lon' => 13, + 'officer_name' => $attributes['officer_name'], + 'officer_phone_number' => $attributes['officer_phone_number'], + 'officer_plaque' => $attributes['officer_plaque'], + 'operator_id' => $user->id, + 'operator_name' => $user->name, + 'province_id' => $user->province_id, + 'province_fa' => $user->province_fa, + 'city_id' => $user->city_id, + 'city_fa' => $user->city_fa, + 'edare_id' => $user->edarate_shahri_id, + 'edare_name' => $user->edarate_shahri_name, + 'start_way_id' => 0, + 'end_way_id' => 0, + 'status' => 0, + 'status_fa' => 'در حال بررسی', + 'start_time' => $attributes['start_time'], + 'end_time' => $attributes['end_time'], + 'description' => null, + ]); + + $this->assertDatabaseHas('observed_items', [ + 'item_id' => $infoItem->item, + 'item_name' => $infoItem->item_str, + 'sub_item_id' => $infoItem->sub_item, + 'sub_item_name' => $infoItem->sub_item_str, + 'local_name' => $attributes['observed_items'][0]['local_name'], + 'description' => null, + 'start_lat' => 12, + 'start_lon' => 12, + ]); + } + + public function test_end_point_is_required_when_instant_action_is_1(): void + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + $logList = LogList::factory()->create([ + 'log_unique_code' => 1147 + ]); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'add-road-patrol' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $attributes = [ + 'start_point' => '12,12', + 'end_point' => '13,13', + 'officer_name' => $this->faker->name, + 'officer_plaque' => $this->faker->numerify(), + 'officer_phone_number' => $this->faker->numerify("###########"), + 'start_time' => '2016-01-23 11:53', + 'end_time' => '2017-01-23 11:53', + 'observed_items' => [ + [ + 'after_image' => UploadedFile::fake()->create('image.jpg', 10), + 'item_id' => $this->faker->randomNumber(), + 'sub_item_id' => $this->faker->randomNumber(), + 'local_name' => $this->faker->name(), + 'instant_action' => 1, + 'start_point' => '12,12', + 'priority' => $this->faker->numberBetween(1, 3), + 'priority_fa' => $this->faker->name, + ] + ] + ]; + + $infoItem = InfoItem::factory()->create([ + 'item' => $attributes['observed_items'][0]['item_id'], + 'sub_item' => $attributes['observed_items'][0]['sub_item_id'], + 'needs_end_point' => 1 + ]); + + $mock = $this->mock(NominatimService::class, function (MockInterface $mock) { + $mock->shouldReceive('get_way_id_from_nominatim')->andReturn(0); + }); + + Sms::shouldReceive('sendSms'); + + $response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store', $attributes); + + $response->assertUnprocessable() + ->assertInvalid([ + "observed_items.0.end_point" => __('validation.required', ['attribute' => 'end_point']), + ]); + } + + public function test_before_image_is_required_when_instant_action_is_1(): void + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + $logList = LogList::factory()->create([ + 'log_unique_code' => 1147 + ]); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'add-road-patrol' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $attributes = [ + 'start_point' => '12,12', + 'end_point' => '13,13', + 'officer_name' => $this->faker->name, + 'officer_plaque' => $this->faker->numerify(), + 'officer_phone_number' => $this->faker->numerify("###########"), + 'start_time' => '2016-01-23 11:53', + 'end_time' => '2017-01-23 11:53', + 'observed_items' => [ + [ + 'after_image' => UploadedFile::fake()->create('image.jpg', 10), + 'item_id' => $this->faker->randomNumber(), + 'sub_item_id' => $this->faker->randomNumber(), + 'local_name' => $this->faker->name(), + 'instant_action' => 1, + 'start_point' => '12,12', + 'priority' => $this->faker->numberBetween(1, 3), + 'priority_fa' => $this->faker->name, + ] + ] + ]; + + $infoItem = InfoItem::factory()->create([ + 'item' => $attributes['observed_items'][0]['item_id'], + 'sub_item' => $attributes['observed_items'][0]['sub_item_id'], + 'needs_image' => 1 + ]); + + $mock = $this->mock(NominatimService::class, function (MockInterface $mock) { + $mock->shouldReceive('get_way_id_from_nominatim')->andReturn(0); + }); + + Sms::shouldReceive('sendSms'); + + $response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store', $attributes); + + $response->assertUnprocessable() + ->assertInvalid([ + "observed_items.0.before_image" => __('validation.required', ['attribute' => 'before_image']), + ]); + } + + public function test_after_image_is_required_when_instant_action_is_1(): void + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + $logList = LogList::factory()->create([ + 'log_unique_code' => 1147 + ]); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'add-road-patrol' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $attributes = [ + 'start_point' => '12,12', + 'end_point' => '13,13', + 'officer_name' => $this->faker->name, + 'officer_plaque' => $this->faker->numerify(), + 'officer_phone_number' => $this->faker->numerify("###########"), + 'start_time' => '2016-01-23 11:53', + 'end_time' => '2017-01-23 11:53', + 'observed_items' => [ + [ + 'before_image' => UploadedFile::fake()->create('image.jpg', 10), + 'item_id' => $this->faker->randomNumber(), + 'sub_item_id' => $this->faker->randomNumber(), + 'local_name' => $this->faker->name(), + 'instant_action' => 1, + 'start_point' => '12,12', + 'priority' => $this->faker->numberBetween(1, 3), + 'priority_fa' => $this->faker->name, + ] + ] + ]; + + $infoItem = InfoItem::factory()->create([ + 'item' => $attributes['observed_items'][0]['item_id'], + 'sub_item' => $attributes['observed_items'][0]['sub_item_id'], + 'needs_image' => 1 + ]); + + $mock = $this->mock(NominatimService::class, function (MockInterface $mock) { + $mock->shouldReceive('get_way_id_from_nominatim')->andReturn(0); + }); + + Sms::shouldReceive('sendSms'); + + $response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store', $attributes); + + $response->assertUnprocessable() + ->assertInvalid([ + "observed_items.0.after_image" => __('validation.required', ['attribute' => 'after_image']), + ]); + } + + public function test_amount_is_required_when_instant_action_is_1(): void + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + $logList = LogList::factory()->create([ + 'log_unique_code' => 1147 + ]); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'add-road-patrol' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $attributes = [ + 'start_point' => '12,12', + 'end_point' => '13,13', + 'officer_name' => $this->faker->name, + 'officer_plaque' => $this->faker->numerify(), + 'officer_phone_number' => $this->faker->numerify("###########"), + 'start_time' => '2016-01-23 11:53', + 'end_time' => '2017-01-23 11:53', + 'observed_items' => [ + [ + 'item_id' => $this->faker->randomNumber(), + 'sub_item_id' => $this->faker->randomNumber(), + 'local_name' => $this->faker->name(), + 'instant_action' => 1, + 'start_point' => '12,12', + 'priority' => $this->faker->numberBetween(1, 3), + 'priority_fa' => $this->faker->name, + ] + ] + ]; + + $infoItem = InfoItem::factory()->create([ + 'item' => $attributes['observed_items'][0]['item_id'], + 'sub_item' => $attributes['observed_items'][0]['sub_item_id'], + ]); + + $mock = $this->mock(NominatimService::class, function (MockInterface $mock) { + $mock->shouldReceive('get_way_id_from_nominatim')->andReturn(0); + }); + + Sms::shouldReceive('sendSms'); + + $response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store', $attributes); + + $response->assertUnprocessable() + ->assertInvalid([ + "observed_items.0.amount" => __('validation.required', ['attribute' => 'amount']), + ]); + } + + public function test_can_store_a_road_patrol_when_instant_action_is_1(): void + { + $city = City::factory()->create(); + $province = Province::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + $logList = LogList::factory()->create([ + 'log_unique_code' => 1147 + ]); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'add-road-patrol' + ])) + ->create([ + 'city_id' => $city->id, + 'province_id' => $province->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $attributes = [ + 'start_point' => '12,12', + 'end_point' => '13,13', + 'officer_name' => $this->faker->name, + 'officer_plaque' => $this->faker->numerify(), + 'officer_phone_number' => $this->faker->numerify("###########"), + 'start_time' => '2016-01-23 11:53', + 'end_time' => '2017-01-23 11:53', + 'observed_items' => [ + [ + 'item_id' => 1, + 'sub_item_id' => 1, + 'local_name' => $this->faker->name(), + 'instant_action' => 1, + 'start_point' => '12,12', + 'priority' => $this->faker->numberBetween(1, 3), + 'priority_fa' => $this->faker->name, + 'amount' => $this->faker->numberBetween(1, 10) + ] + ] + ]; + + $infoItem = InfoItem::factory()->create([ + 'item' => 1, + 'sub_item' => 1, + ]); + + $mock = $this->mock(NominatimService::class, function (MockInterface $mock) { + $mock->shouldReceive('get_way_id_from_nominatim')->andReturn(0); + }); + + Sms::shouldReceive('sendSms'); + + $response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store', $attributes); + + $response->assertOk(); + + $this->assertDatabaseHas('road_patrols', [ + 'start_lat' => 12, + 'start_lon' => 12, + 'end_lat' => 13, + 'end_lon' => 13, + 'officer_name' => $attributes['officer_name'], + 'officer_phone_number' => $attributes['officer_phone_number'], + 'officer_plaque' => $attributes['officer_plaque'], + 'operator_id' => $user->id, + 'operator_name' => $user->name, + 'province_id' => $user->province_id, + 'province_fa' => $user->province_fa, + 'city_id' => $user->city_id, + 'city_fa' => $user->city_fa, + 'edare_id' => $user->edarate_shahri_id, + 'edare_name' => $user->edarate_shahri_name, + 'start_way_id' => 0, + 'end_way_id' => 0, + 'status' => 0, + 'status_fa' => 'در حال بررسی', + 'start_time' => $attributes['start_time'], + 'end_time' => $attributes['end_time'], + 'description' => null, + ]); + + $this->assertDatabaseHas('observed_items', [ + 'item_id' => $infoItem->item, + 'item_name' => $infoItem->item_str, + 'sub_item_id' => $infoItem->sub_item, + 'sub_item_name' => $infoItem->sub_item_str, + 'local_name' => $attributes['observed_items'][0]['local_name'], + 'description' => null, + 'start_lat' => 12, + 'start_lon' => 12, + ]); + + $this->assertDatabaseHas('road_items_projects', [ + 'start_lat' => 12, + 'start_lng' => 12, + 'end_lat' => 13, + 'end_lng' => 13, + 'item' => $infoItem->item, + 'item_fa' => $infoItem->item_fa, + 'sub_item' => $infoItem->sub_item, + 'sub_item_fa' => $infoItem->sub_item_fa, + 'sub_item_data' => $attributes['observed_items'][0]['amount'], + 'province_id' => $user->province_id, + 'province_fa' => $user->province_fa, + 'city_id' => $user->city_id, + 'city_fa' => $user->city_fa, + 'user_name' => $user->name, + 'start_way_id' => 0, + 'end_way_id' => 0, + 'info_id' => $infoItem->id, + 'status' => 0, + 'status_fa' => 'در حال بررسی', + 'edarat_id' => $user->edarate_shahri_id, + 'edarat_type' => EdarateShahri::class, + 'is_new' => 1, + ]); + } +}