From e4505d2da287833289da37f7ba7716e75906b637 Mon Sep 17 00:00:00 2001 From: Hasan Zaki Date: Sun, 6 Oct 2024 08:11:02 +0000 Subject: [PATCH 01/15] Update README.md --- README.md | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index f6be4910..867a37fe 100644 --- a/README.md +++ b/README.md @@ -50,10 +50,26 @@ avoid any timeout response ```sh /etc/nginx/sites_available/exmapl.com ``` -| Variable | Value | Condtion | Context | -|:---------------------------------:|:--------:|:------------------------------------------------------------------:|:-----------------------:| -| proxy_connect_timeout | `120` | no condition | `http, server, location`| -| proxy_send_timeout | `120` | no condition | `http, server, location`| -| proxy_read_timeout | `120` | no condition | `http, server, location`| -| send_timeout | `120` | no condition | `http, server, location`| -| fastcgi_read_timeout | `300` | no condition | `location` | \ No newline at end of file +| Variable | Value | Condtion | Context | +|:---------------------:|:-----:|:------------:|:------------------------:| +| proxy_connect_timeout | `120` | no condition | `http, server, location` | +| proxy_send_timeout | `120` | no condition | `http, server, location` | +| proxy_read_timeout | `120` | no condition | `http, server, location` | +| send_timeout | `120` | no condition | `http, server, location` | +| fastcgi_read_timeout | `300` | no condition | `location` | +| client_max_body_size | `10M` | no condition | `http` | + + **Configuration** + +change php.ini through this path + + ```sh + /etc/php/8.1/fpm/php.ini + ``` + +set this values + +| Variable | Value | Condtion | Context | +|:-------------------:|:-----:|:------------:|:-------------:| +| post_max_size | `10M` | no condition | fpm : php.ini | +| upload_max_filesize | `10M` | no condition | fpm : php.ini | \ No newline at end of file From 336d7a8cbcdb08ea13f63749bb706d7ab8e56195 Mon Sep 17 00:00:00 2001 From: Hasan Zaki Date: Sun, 6 Oct 2024 08:14:09 +0000 Subject: [PATCH 02/15] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 867a37fe..c88862ec 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ ## Prod Prerequisites apply bellow changes in order to set up the project on prod -### Database configuration +**Database Configuration** optimize MariaDB configuration through this path ```sh /etc/mysql/mariadb.conf.d/50-server.cnf From 9455ef8aeb9d1bb445e33274426047ad27c26feb Mon Sep 17 00:00:00 2001 From: Hasan Zaki Date: Sun, 6 Oct 2024 08:14:56 +0000 Subject: [PATCH 03/15] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index c88862ec..abb943c1 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ ## Prod Prerequisites apply bellow changes in order to set up the project on prod + **Database Configuration** optimize MariaDB configuration through this path ```sh From 33db67ac42c0b1902078dc3ded5cd897b0d49793 Mon Sep 17 00:00:00 2001 From: Hasan Zaki Date: Sun, 6 Oct 2024 08:15:45 +0000 Subject: [PATCH 04/15] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index abb943c1..572ba51f 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ apply bellow changes in order to set up the project on prod **Database Configuration** + optimize MariaDB configuration through this path ```sh /etc/mysql/mariadb.conf.d/50-server.cnf From 7ac6f4de6e512cc781a51cc603294861b979125d Mon Sep 17 00:00:00 2001 From: Hasan Zaki Date: Wed, 9 Oct 2024 07:00:22 +0000 Subject: [PATCH 05/15] Update README.md --- README.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 572ba51f..4fe34ea4 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,8 @@ change php.ini through this path set this values -| Variable | Value | Condtion | Context | -|:-------------------:|:-----:|:------------:|:-------------:| -| post_max_size | `10M` | no condition | fpm : php.ini | -| upload_max_filesize | `10M` | no condition | fpm : php.ini | \ No newline at end of file +| Variable | Value | Condtion | Context | +|:-------------------:|:------:|:------------:|:-------------:| +| post_max_size | `24M` | no condition | fpm : php.ini | +| upload_max_filesize | `8M` | no condition | fpm : php.ini | +| max_exec_time | `120s` | no condition | fpm : php.ini | \ No newline at end of file From 9aa3cf320fad0eb7dfaa3e7f03b943963d1d9480 Mon Sep 17 00:00:00 2001 From: Hasan Zaki Date: Sun, 13 Oct 2024 10:24:30 +0000 Subject: [PATCH 06/15] Update README.md --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 4fe34ea4..c6da1124 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,11 @@ # RMS (road management system) +## Table of Contents +- [Installation](#installation) +- [Dependencies](#dependencies) + ## Dependencies
Server From c2ceb3d3432194d0e20d9ed18cd6cc8580f30d2f Mon Sep 17 00:00:00 2001 From: amirghasempoor Date: Tue, 22 Oct 2024 16:40:36 +0330 Subject: [PATCH 07/15] create all azmayesh resources and its dependencies --- .../V3/Azmayesh/AzmayeshController.php | 69 +++++++++++++++++++ .../V3/Azmayesh/AzmayeshTypeController.php | 28 ++++++++ app/Models/Azmayesh.php | 19 +++++ app/Models/AzmayeshField.php | 19 +++++ app/Models/AzmayeshSample.php | 11 +++ app/Models/AzmayeshType.php | 25 +++++++ database/factories/AzmayeshFactory.php | 23 +++++++ database/factories/AzmayeshFieldFactory.php | 23 +++++++ database/factories/AzmayeshSampleFactory.php | 23 +++++++ database/factories/AzmayeshTypeFactory.php | 23 +++++++ ...22_092756_create_azmayesh_fields_table.php | 31 +++++++++ ..._22_092762_create_azmayesh_types_table.php | 28 ++++++++ ...4_10_22_092800_create_azmayeshes_table.php | 33 +++++++++ ...2_114035_create_azmayesh_samples_table.php | 30 ++++++++ ...ate_azmayesh_field_azmayesh_type_table.php | 29 ++++++++ database/seeders/AzmayeshFieldSeeder.php | 37 ++++++++++ database/seeders/AzmayeshSampleSeeder.php | 32 +++++++++ database/seeders/AzmayeshSeeder.php | 31 +++++++++ database/seeders/AzmayeshTypeSeeder.php | 25 +++++++ routes/v3.php | 9 +++ 20 files changed, 548 insertions(+) create mode 100644 app/Http/Controllers/V3/Azmayesh/AzmayeshController.php create mode 100644 app/Http/Controllers/V3/Azmayesh/AzmayeshTypeController.php create mode 100644 app/Models/Azmayesh.php create mode 100644 app/Models/AzmayeshField.php create mode 100644 app/Models/AzmayeshSample.php create mode 100644 app/Models/AzmayeshType.php create mode 100644 database/factories/AzmayeshFactory.php create mode 100644 database/factories/AzmayeshFieldFactory.php create mode 100644 database/factories/AzmayeshSampleFactory.php create mode 100644 database/factories/AzmayeshTypeFactory.php create mode 100644 database/migrations/2024_10_22_092756_create_azmayesh_fields_table.php create mode 100644 database/migrations/2024_10_22_092762_create_azmayesh_types_table.php create mode 100644 database/migrations/2024_10_22_092800_create_azmayeshes_table.php create mode 100644 database/migrations/2024_10_22_114035_create_azmayesh_samples_table.php create mode 100644 database/migrations/2024_10_22_152052_create_azmayesh_field_azmayesh_type_table.php create mode 100644 database/seeders/AzmayeshFieldSeeder.php create mode 100644 database/seeders/AzmayeshSampleSeeder.php create mode 100644 database/seeders/AzmayeshSeeder.php create mode 100644 database/seeders/AzmayeshTypeSeeder.php diff --git a/app/Http/Controllers/V3/Azmayesh/AzmayeshController.php b/app/Http/Controllers/V3/Azmayesh/AzmayeshController.php new file mode 100644 index 00000000..a4e2a3a0 --- /dev/null +++ b/app/Http/Controllers/V3/Azmayesh/AzmayeshController.php @@ -0,0 +1,69 @@ +successResponse($data); + } + + /** + * Store a newly created resource in storage. + */ + public function store(Request $request) + { + // + } + + /** + * Display the specified resource. + */ + public function show(Azmayesh $azmayesh): JsonResponse + { + return $this->successResponse($azmayesh->load('azmayeshSamples')); + } + + /** + * Update the specified resource in storage. + */ + public function update(Request $request, Azmayesh $azmayesh) + { + // + } + + /** + * Remove the specified resource from storage. + */ + public function destroy(Azmayesh $azmayesh): JsonResponse + { + $azmayesh->delete(); + + return $this->successResponse(); + } +} diff --git a/app/Http/Controllers/V3/Azmayesh/AzmayeshTypeController.php b/app/Http/Controllers/V3/Azmayesh/AzmayeshTypeController.php new file mode 100644 index 00000000..d3dbe072 --- /dev/null +++ b/app/Http/Controllers/V3/Azmayesh/AzmayeshTypeController.php @@ -0,0 +1,28 @@ +successResponse($allTypes); + } + + public function fields(AzmayeshType $azmayeshType): JsonResponse + { + $fields = $azmayeshType->azmayeshFields(); + + return $this->successResponse($fields); + } +} diff --git a/app/Models/Azmayesh.php b/app/Models/Azmayesh.php new file mode 100644 index 00000000..a790c42c --- /dev/null +++ b/app/Models/Azmayesh.php @@ -0,0 +1,19 @@ +hasMany(AzmayeshSample::class); + } +} diff --git a/app/Models/AzmayeshField.php b/app/Models/AzmayeshField.php new file mode 100644 index 00000000..d6bd90a2 --- /dev/null +++ b/app/Models/AzmayeshField.php @@ -0,0 +1,19 @@ +belongsToMany(AzmayeshType::class); + } +} diff --git a/app/Models/AzmayeshSample.php b/app/Models/AzmayeshSample.php new file mode 100644 index 00000000..baa62a61 --- /dev/null +++ b/app/Models/AzmayeshSample.php @@ -0,0 +1,11 @@ +hasMany(Azmayesh::class); + } + + public function azmayeshFields(): BelongsToMany + { + return $this->belongsToMany(AzmayeshField::class); + } +} diff --git a/database/factories/AzmayeshFactory.php b/database/factories/AzmayeshFactory.php new file mode 100644 index 00000000..8b388147 --- /dev/null +++ b/database/factories/AzmayeshFactory.php @@ -0,0 +1,23 @@ + + */ +class AzmayeshFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + // + ]; + } +} diff --git a/database/factories/AzmayeshFieldFactory.php b/database/factories/AzmayeshFieldFactory.php new file mode 100644 index 00000000..01716b78 --- /dev/null +++ b/database/factories/AzmayeshFieldFactory.php @@ -0,0 +1,23 @@ + + */ +class AzmayeshFieldFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + // + ]; + } +} diff --git a/database/factories/AzmayeshSampleFactory.php b/database/factories/AzmayeshSampleFactory.php new file mode 100644 index 00000000..73670225 --- /dev/null +++ b/database/factories/AzmayeshSampleFactory.php @@ -0,0 +1,23 @@ + + */ +class AzmayeshSampleFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + // + ]; + } +} diff --git a/database/factories/AzmayeshTypeFactory.php b/database/factories/AzmayeshTypeFactory.php new file mode 100644 index 00000000..3a2bb0a2 --- /dev/null +++ b/database/factories/AzmayeshTypeFactory.php @@ -0,0 +1,23 @@ + + */ +class AzmayeshTypeFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + // + ]; + } +} diff --git a/database/migrations/2024_10_22_092756_create_azmayesh_fields_table.php b/database/migrations/2024_10_22_092756_create_azmayesh_fields_table.php new file mode 100644 index 00000000..b68d79c7 --- /dev/null +++ b/database/migrations/2024_10_22_092756_create_azmayesh_fields_table.php @@ -0,0 +1,31 @@ +id(); + $table->string('name'); + $table->string('unit'); + $table->string('type'); + $table->string('option')->nullable(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('azmayesh_fields'); + } +}; diff --git a/database/migrations/2024_10_22_092762_create_azmayesh_types_table.php b/database/migrations/2024_10_22_092762_create_azmayesh_types_table.php new file mode 100644 index 00000000..db0e636c --- /dev/null +++ b/database/migrations/2024_10_22_092762_create_azmayesh_types_table.php @@ -0,0 +1,28 @@ +id(); + $table->string('name'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('azmayesh_types'); + } +}; diff --git a/database/migrations/2024_10_22_092800_create_azmayeshes_table.php b/database/migrations/2024_10_22_092800_create_azmayeshes_table.php new file mode 100644 index 00000000..e36d00cb --- /dev/null +++ b/database/migrations/2024_10_22_092800_create_azmayeshes_table.php @@ -0,0 +1,33 @@ +id(); + $table->foreignId('azmayesh_type_id')->constrained('azmayesh_types'); + $table->string('azmayesh_type_name'); + $table->string('lat'); + $table->string('lng'); + $table->foreignId('province_id')->constrained('provinces'); + $table->string('province_name'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('azmayeshes'); + } +}; diff --git a/database/migrations/2024_10_22_114035_create_azmayesh_samples_table.php b/database/migrations/2024_10_22_114035_create_azmayesh_samples_table.php new file mode 100644 index 00000000..a4e61680 --- /dev/null +++ b/database/migrations/2024_10_22_114035_create_azmayesh_samples_table.php @@ -0,0 +1,30 @@ +id(); + $table->foreignId('azmayesh_id')->constrained('azmayeshes'); + $table->string('azmayesh_name'); + $table->json('data'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('azmayesh_samples'); + } +}; diff --git a/database/migrations/2024_10_22_152052_create_azmayesh_field_azmayesh_type_table.php b/database/migrations/2024_10_22_152052_create_azmayesh_field_azmayesh_type_table.php new file mode 100644 index 00000000..5507811c --- /dev/null +++ b/database/migrations/2024_10_22_152052_create_azmayesh_field_azmayesh_type_table.php @@ -0,0 +1,29 @@ +id(); + $table->foreignId('azmayesh_field_id')->constrained('azmayesh_fields'); + $table->foreignId('azmayesh_type_id')->constrained('azmayesh_types'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('azmayesh_field_azmayesh_type'); + } +}; diff --git a/database/seeders/AzmayeshFieldSeeder.php b/database/seeders/AzmayeshFieldSeeder.php new file mode 100644 index 00000000..a4201fa3 --- /dev/null +++ b/database/seeders/AzmayeshFieldSeeder.php @@ -0,0 +1,37 @@ +insert([ + [ + 'id' => 1, + 'name' => 'طول', + 'unit' => 'سانتی متر', + 'type' => 'input', + 'option' => null, + 'created_at' => now(), + 'updated_at' => now() + ], + [ + 'id' => 2, + 'name' => 'عرض', + 'unit' => 'سانتی متر', + 'type' => 'input', + 'option' => null, + 'created_at' => now(), + 'updated_at' => now() + ], + ]); + } +} diff --git a/database/seeders/AzmayeshSampleSeeder.php b/database/seeders/AzmayeshSampleSeeder.php new file mode 100644 index 00000000..66a53b02 --- /dev/null +++ b/database/seeders/AzmayeshSampleSeeder.php @@ -0,0 +1,32 @@ +insert([ + [ + 'id' => 1, + 'azmayesh_id' => Azmayesh::factory(), + 'azmayesh_name' => function (array $attributes) { + return Azmayesh::query()->find($attributes['azmayesh_id'])->name; + }, + 'data' => json_encode([ + 1 => 'test' + ]), + 'created_at' => now(), + 'updated_at' => now() + ], + ]); + } +} diff --git a/database/seeders/AzmayeshSeeder.php b/database/seeders/AzmayeshSeeder.php new file mode 100644 index 00000000..b0051ad9 --- /dev/null +++ b/database/seeders/AzmayeshSeeder.php @@ -0,0 +1,31 @@ +insert([ + [ + 'id' => 1, + 'name' => '', + 'created_at' => now(), + 'updated_at' => now() + ], + [ + 'id' => 2, + 'name' => '', + 'created_at' => now(), + 'updated_at' => now() + ], + ]); + } +} diff --git a/database/seeders/AzmayeshTypeSeeder.php b/database/seeders/AzmayeshTypeSeeder.php new file mode 100644 index 00000000..3d3f56e3 --- /dev/null +++ b/database/seeders/AzmayeshTypeSeeder.php @@ -0,0 +1,25 @@ +insert([ + [ + 'id' => 1, + 'name' => 'تعیین مقاومت فشاری بتن', + 'created_at' => now(), + 'updated_at' => now() + ], + ]); + } +} diff --git a/routes/v3.php b/routes/v3.php index 9597fc93..193d6b0f 100644 --- a/routes/v3.php +++ b/routes/v3.php @@ -1,5 +1,6 @@ name('edit'); Route::post('change_password', 'changePassword')->name('changePassword'); }); + +Route::prefix('azmayesh') + ->name('azmayesh.') + ->controller(AzmayeshController::class) + ->group(function () { + Route::get('/', 'index')->name('index'); + Route::get('/fields', 'fields')->name('fields'); + }); From e9098b0a556baaed4a28e0515286be33262f9a5f Mon Sep 17 00:00:00 2001 From: amirghasempoor Date: Wed, 23 Oct 2024 15:54:01 +0330 Subject: [PATCH 08/15] add static azmayesh fields --- .../V3/Azmayesh/AzmayeshController.php | 58 +++++++++++++++++-- .../V3/Azmayesh/AzmayeshTypeController.php | 2 +- app/Models/Azmayesh.php | 5 ++ app/Models/AzmayeshSample.php | 2 + app/Models/AzmayeshType.php | 2 +- database/factories/AzmayeshFactory.php | 11 +++- database/factories/AzmayeshFieldFactory.php | 4 +- database/factories/AzmayeshSampleFactory.php | 7 ++- database/factories/AzmayeshTypeFactory.php | 2 +- ...4_10_22_092800_create_azmayeshes_table.php | 12 +++- database/seeders/AzmayeshSeeder.php | 32 +++++++++- routes/v3.php | 18 +++++- 12 files changed, 137 insertions(+), 18 deletions(-) diff --git a/app/Http/Controllers/V3/Azmayesh/AzmayeshController.php b/app/Http/Controllers/V3/Azmayesh/AzmayeshController.php index a4e2a3a0..aea47c94 100644 --- a/app/Http/Controllers/V3/Azmayesh/AzmayeshController.php +++ b/app/Http/Controllers/V3/Azmayesh/AzmayeshController.php @@ -6,6 +6,8 @@ use App\Facades\DataTable\DataTableFacade; use App\Http\Controllers\Controller; use App\Http\Traits\ApiResponse; use App\Models\Azmayesh; +use App\Models\AzmayeshType; +use App\Models\Province; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; @@ -17,7 +19,17 @@ class AzmayeshController extends Controller */ public function index(Request $request): JsonResponse { - $columns = array('id', 'name'); + $columns = array( + 'id', + 'request_date', + 'report_date', + 'request_number', + 'employer', + 'contractor', + 'work_number', + 'consultant', + 'applicant', + ); $allowedFilters = $columns; $allowedSortings = $columns; @@ -30,15 +42,36 @@ class AzmayeshController extends Controller allowedFilters: $allowedFilters, allowedSortings: $allowedSortings); - return $this->successResponse($data); + return response()->json($data); } /** * Store a newly created resource in storage. */ - public function store(Request $request) + public function store(Request $request): JsonResponse { - // + $azmayeshType = AzmayeshType::query()->find($request->azmayesh_type_id); + $province = Province::query()->find($request->province_id); + + Azmayesh::query()->create([ + 'lat' => $request->lat, + 'lng' => $request->lng, + 'azmayesh_type_id' => $azmayeshType->id, + 'azmayesh_type_name' => $azmayeshType->name, + 'request_date' => $request->lat, + 'report_date' => $request->lat, + 'request_number' => $request->lat, + 'employer' => $request->lat, + 'contractor' => $request->lat, + 'work_number' => $request->lat, + 'project_name' => $request->lat, + 'consultant' => $request->lat, + 'applicant' => $request->lat, + 'province_id' => $province->id, + 'province_name' => $province->name, + ]); + + return $this->successResponse(); } /** @@ -52,9 +85,22 @@ class AzmayeshController extends Controller /** * Update the specified resource in storage. */ - public function update(Request $request, Azmayesh $azmayesh) + public function update(Request $request, Azmayesh $azmayesh): JsonResponse { - // + $province = Province::query()->find($request->province_id); + + $azmayesh->update([ + 'name' => $request->name, + 'username' => $request->username, + 'email' => $request->email, + 'phone_number' => $request->phone_number, + 'national_id' => $request->national_id, + 'position' => $request->position, + 'province_id' => $province->id, + 'province_name' => $province->name, + ]); + + return $this->successResponse(); } /** diff --git a/app/Http/Controllers/V3/Azmayesh/AzmayeshTypeController.php b/app/Http/Controllers/V3/Azmayesh/AzmayeshTypeController.php index d3dbe072..f9d1cf88 100644 --- a/app/Http/Controllers/V3/Azmayesh/AzmayeshTypeController.php +++ b/app/Http/Controllers/V3/Azmayesh/AzmayeshTypeController.php @@ -21,7 +21,7 @@ class AzmayeshTypeController extends Controller public function fields(AzmayeshType $azmayeshType): JsonResponse { - $fields = $azmayeshType->azmayeshFields(); + $fields = $azmayeshType->azmayeshFields; return $this->successResponse($fields); } diff --git a/app/Models/Azmayesh.php b/app/Models/Azmayesh.php index a790c42c..88adfa5e 100644 --- a/app/Models/Azmayesh.php +++ b/app/Models/Azmayesh.php @@ -16,4 +16,9 @@ class Azmayesh extends Model { return $this->hasMany(AzmayeshSample::class); } + + public function getRouteKeyName(): string + { + return 'id'; + } } diff --git a/app/Models/AzmayeshSample.php b/app/Models/AzmayeshSample.php index baa62a61..e79b544a 100644 --- a/app/Models/AzmayeshSample.php +++ b/app/Models/AzmayeshSample.php @@ -8,4 +8,6 @@ use Illuminate\Database\Eloquent\Model; class AzmayeshSample extends Model { use HasFactory; + + protected $guarded = []; } diff --git a/app/Models/AzmayeshType.php b/app/Models/AzmayeshType.php index 80a8c6aa..0a5db09d 100644 --- a/app/Models/AzmayeshType.php +++ b/app/Models/AzmayeshType.php @@ -20,6 +20,6 @@ class AzmayeshType extends Model public function azmayeshFields(): BelongsToMany { - return $this->belongsToMany(AzmayeshField::class); + return $this->belongsToMany(AzmayeshField::class,'azmayesh_field_azmayesh_type'); } } diff --git a/database/factories/AzmayeshFactory.php b/database/factories/AzmayeshFactory.php index 8b388147..78c19dae 100644 --- a/database/factories/AzmayeshFactory.php +++ b/database/factories/AzmayeshFactory.php @@ -2,6 +2,8 @@ namespace Database\Factories; +use App\Models\AzmayeshType; +use App\Models\Province; use Illuminate\Database\Eloquent\Factories\Factory; /** @@ -17,7 +19,14 @@ class AzmayeshFactory extends Factory public function definition(): array { return [ - // + 'azmayesh_type_id' => AzmayeshType::factory(), + 'azmayesh_type_name' => function (array $attributes) { + return AzmayeshType::query()->find($attributes['azmayesh_type_id'])->name; + }, + 'province_id' => Province::factory(), + 'province_name' => function (array $attributes) { + return Province::query()->find($attributes['province_id'])->name; + }, ]; } } diff --git a/database/factories/AzmayeshFieldFactory.php b/database/factories/AzmayeshFieldFactory.php index 01716b78..e0729e35 100644 --- a/database/factories/AzmayeshFieldFactory.php +++ b/database/factories/AzmayeshFieldFactory.php @@ -17,7 +17,9 @@ class AzmayeshFieldFactory extends Factory public function definition(): array { return [ - // + 'name' => $this->faker->name, + 'unit' => $this->faker->name, + 'type' => $this->faker->name, ]; } } diff --git a/database/factories/AzmayeshSampleFactory.php b/database/factories/AzmayeshSampleFactory.php index 73670225..f4b518a4 100644 --- a/database/factories/AzmayeshSampleFactory.php +++ b/database/factories/AzmayeshSampleFactory.php @@ -2,6 +2,7 @@ namespace Database\Factories; +use App\Models\Azmayesh; use Illuminate\Database\Eloquent\Factories\Factory; /** @@ -17,7 +18,11 @@ class AzmayeshSampleFactory extends Factory public function definition(): array { return [ - // + 'azmayesh_id' => Azmayesh::factory(), + 'azmayesh_name' => function (array $attributes) { + return Azmayesh::query()->find($attributes['azmayesh_id'])->name; + }, + 'data' => json_encode(['test']), ]; } } diff --git a/database/factories/AzmayeshTypeFactory.php b/database/factories/AzmayeshTypeFactory.php index 3a2bb0a2..0a0cf008 100644 --- a/database/factories/AzmayeshTypeFactory.php +++ b/database/factories/AzmayeshTypeFactory.php @@ -17,7 +17,7 @@ class AzmayeshTypeFactory extends Factory public function definition(): array { return [ - // + 'name' => $this->faker->name ]; } } diff --git a/database/migrations/2024_10_22_092800_create_azmayeshes_table.php b/database/migrations/2024_10_22_092800_create_azmayeshes_table.php index e36d00cb..f477cea9 100644 --- a/database/migrations/2024_10_22_092800_create_azmayeshes_table.php +++ b/database/migrations/2024_10_22_092800_create_azmayeshes_table.php @@ -17,8 +17,18 @@ return new class extends Migration $table->string('azmayesh_type_name'); $table->string('lat'); $table->string('lng'); - $table->foreignId('province_id')->constrained('provinces'); + $table->unsignedTinyInteger('province_id'); + $table->foreign('province_id')->references('id')->on('provinces'); $table->string('province_name'); + $table->date('request_date'); + $table->date('report_date'); + $table->integer('request_number'); + $table->string('employer'); + $table->string('contractor'); + $table->integer('work_number'); + $table->string('project_name'); + $table->string('consultant'); + $table->string('applicant'); $table->timestamps(); }); } diff --git a/database/seeders/AzmayeshSeeder.php b/database/seeders/AzmayeshSeeder.php index b0051ad9..65c04fea 100644 --- a/database/seeders/AzmayeshSeeder.php +++ b/database/seeders/AzmayeshSeeder.php @@ -16,13 +16,41 @@ class AzmayeshSeeder extends Seeder DB::table('azmayeshes')->insert([ [ 'id' => 1, - 'name' => '', + 'lat' => '13.45', + 'lng' => '11.85', + 'azmayesh_type_id' => 1, + 'azmayesh_type_name' => 'test', + 'province_id' => 11, + 'province_name' => 'test', + 'request_date' => '1403-07-04', + 'report_date' => '1403-07-05', + 'request_number' => 0, + 'employer' => 'test', + 'contractor' => 'test', + 'work_number' => 0, + 'project_name' => 'test', + 'consultant' => 'test', + 'applicant' => 'test', 'created_at' => now(), 'updated_at' => now() ], [ 'id' => 2, - 'name' => '', + 'lat' => '13.45', + 'lng' => '11.85', + 'azmayesh_type_id' => 1, + 'azmayesh_type_name' => 'test', + 'province_id' => 11, + 'province_name' => 'test', + 'request_date' => '1403-07-04', + 'report_date' => '1403-07-05', + 'request_number' => '0', + 'employer' => 'test', + 'contractor' => 'test', + 'work_number' => '0', + 'project_name' => 'test', + 'consultant' => 'test', + 'applicant' => 'test', 'created_at' => now(), 'updated_at' => now() ], diff --git a/routes/v3.php b/routes/v3.php index 193d6b0f..bf6e68a6 100644 --- a/routes/v3.php +++ b/routes/v3.php @@ -1,6 +1,7 @@ name('changePassword'); }); -Route::prefix('azmayesh') - ->name('azmayesh.') +Route::prefix('azmayesh_types') + ->name('azmayesh_types') + ->controller(AzmayeshTypeController::class) + ->group(function () { + Route::get('/', 'index')->name('index'); + Route::get('/fields/{azmayeshType}', 'fields')->name('fields'); + }); + +Route::prefix('azmayeshes') + ->name('azmayeshes.') ->controller(AzmayeshController::class) ->group(function () { Route::get('/', 'index')->name('index'); - Route::get('/fields', 'fields')->name('fields'); + Route::get('/{azmayesh}', 'show')->name('show'); + Route::post('/store', 'store')->name('store'); + Route::post('/update/{azmayesh}', 'update')->name('update'); + Route::post('/delete/{azmayesh}', 'delete')->name('delete'); }); From 1a15a5d7ede1b879a916f816ec850b74d6c9e401 Mon Sep 17 00:00:00 2001 From: Witel Group Date: Sun, 3 Nov 2024 06:26:26 +0000 Subject: [PATCH 09/15] change the migration columns and add form request --- .../V3/Azmayesh/AzmayeshController.php | 54 ++++--- .../V3/Azmayesh/AzmayeshSampleController.php | 90 ++++++++++++ .../V3/Azmayesh/AzmayeshTypeController.php | 2 +- .../Requests/V3/Azmayesh/StoreRequest.php | 40 ++++++ .../Requests/V3/Azmayesh/UpdateRequest.php | 40 ++++++ .../V3/AzmayeshSample/StoreRequest.php | 29 ++++ .../V3/AzmayeshSample/UpdateRequest.php | 29 ++++ app/Models/AzmayeshField.php | 10 +- ...22_092756_create_azmayesh_fields_table.php | 4 +- ...4_10_22_092800_create_azmayeshes_table.php | 8 +- ...2_114035_create_azmayesh_samples_table.php | 4 +- database/seeders/AzmayeshFieldSeeder.php | 18 +++ database/seeders/AzmayeshSampleSeeder.php | 6 +- database/seeders/AzmayeshSeeder.php | 134 ++++++++++++++---- database/seeders/AzmayeshTypeSeeder.php | 24 ++++ routes/v3.php | 18 ++- 16 files changed, 444 insertions(+), 66 deletions(-) create mode 100644 app/Http/Controllers/V3/Azmayesh/AzmayeshSampleController.php create mode 100644 app/Http/Requests/V3/Azmayesh/StoreRequest.php create mode 100644 app/Http/Requests/V3/Azmayesh/UpdateRequest.php create mode 100644 app/Http/Requests/V3/AzmayeshSample/StoreRequest.php create mode 100644 app/Http/Requests/V3/AzmayeshSample/UpdateRequest.php diff --git a/app/Http/Controllers/V3/Azmayesh/AzmayeshController.php b/app/Http/Controllers/V3/Azmayesh/AzmayeshController.php index aea47c94..e6aa7574 100644 --- a/app/Http/Controllers/V3/Azmayesh/AzmayeshController.php +++ b/app/Http/Controllers/V3/Azmayesh/AzmayeshController.php @@ -10,6 +10,8 @@ use App\Models\AzmayeshType; use App\Models\Province; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; +use App\Http\Requests\V3\Azmayesh\StoreRequest; +use App\Http\Requests\V3\Azmayesh\UpdateRequest; class AzmayeshController extends Controller { @@ -48,27 +50,27 @@ class AzmayeshController extends Controller /** * Store a newly created resource in storage. */ - public function store(Request $request): JsonResponse + public function store(StoreRequest $request): JsonResponse { - $azmayeshType = AzmayeshType::query()->find($request->azmayesh_type_id); - $province = Province::query()->find($request->province_id); + $azmayeshType = AzmayeshType::query()->findOrFail($request->azmayesh_type_id); + $province = Province::query()->findOrFail($request->province_id); Azmayesh::query()->create([ 'lat' => $request->lat, 'lng' => $request->lng, 'azmayesh_type_id' => $azmayeshType->id, 'azmayesh_type_name' => $azmayeshType->name, - 'request_date' => $request->lat, - 'report_date' => $request->lat, - 'request_number' => $request->lat, - 'employer' => $request->lat, - 'contractor' => $request->lat, - 'work_number' => $request->lat, - 'project_name' => $request->lat, - 'consultant' => $request->lat, - 'applicant' => $request->lat, + 'request_date' => $request->request_date, + 'report_date' => $request->report_date, + 'request_number' => $request->request_number ?? '-', + 'employer' => $request->employer ?? '-', + 'contractor' => $request->contractor ?? '-', + 'work_number' => $request->work_number ?? '-', + 'project_name' => $request->project_name, + 'consultant' => $request->consultant ?? '-', + 'applicant' => $request->applicant ?? '-', 'province_id' => $province->id, - 'province_name' => $province->name, + 'province_name' => $province->name_fa, ]); return $this->successResponse(); @@ -85,19 +87,27 @@ class AzmayeshController extends Controller /** * Update the specified resource in storage. */ - public function update(Request $request, Azmayesh $azmayesh): JsonResponse + public function update(UpdateRequest $request, Azmayesh $azmayesh): JsonResponse { - $province = Province::query()->find($request->province_id); + $province = Province::query()->findOrFail($request->province_id); + $azmayeshType = AzmayeshType::query()->findOrFail($request->azmayesh_type_id); $azmayesh->update([ - 'name' => $request->name, - 'username' => $request->username, - 'email' => $request->email, - 'phone_number' => $request->phone_number, - 'national_id' => $request->national_id, - 'position' => $request->position, + 'lat' => $request->lat, + 'lng' => $request->lng, + 'azmayesh_type_id' => $azmayeshType->id, + 'azmayesh_type_name' => $azmayeshType->name, + 'request_date' => $request->request_date, + 'report_date' => $request->report_date, + 'request_number' => $request->request_number ?? '-', + 'employer' => $request->employer ?? '-', + 'contractor' => $request->contractor ?? '-', + 'work_number' => $request->work_number ?? '-', + 'project_name' => $request->project_name, + 'consultant' => $request->consultant ?? '-', + 'applicant' => $request->applicant ?? '-', 'province_id' => $province->id, - 'province_name' => $province->name, + 'province_name' => $province->name_fa, ]); return $this->successResponse(); diff --git a/app/Http/Controllers/V3/Azmayesh/AzmayeshSampleController.php b/app/Http/Controllers/V3/Azmayesh/AzmayeshSampleController.php new file mode 100644 index 00000000..a6861062 --- /dev/null +++ b/app/Http/Controllers/V3/Azmayesh/AzmayeshSampleController.php @@ -0,0 +1,90 @@ +where('azmayesh_id', '=', $azmayesh_id); + + $data = DataTableFacade::run( + $query, + $request, + allowedFilters: $allowedFilters, + allowedSortings: $allowedSortings); + + return response()->json($data); + } + + /** + * Store a newly created resource in storage. + */ + public function store(StoreRequest $request): JsonResponse + { + $azmayesh = Azmayesh::query()->findOrFail($request->azmayesh_id); + + AzmayeshSample::query()->create([ + 'azmayesh_id' => $azmayesh->id, + 'azmayesh_project_name' => $azmayesh->project_name, + 'data' => $request->data, + ]); + + return $this->successResponse(); + } + + /** + * Display the specified resource. + */ + public function show(AzmayeshSample $azmayeshSample) + { + return $this->successResponse($azmayeshSample); + } + + /** + * Update the specified resource in storage. + */ + public function update(UpdateRequest $request, AzmayeshSample $azmayeshSample) + { + $azmayeshSample->update([ + 'azmayesh_id' => $azmayesh->id, + 'azmayesh_project_name' => $azmayesh->project_name, + 'data' => json_encode($request->data), + ]); + + return $this->successResponse(); + } + + /** + * Remove the specified resource from storage. + */ + public function destroy(AzmayeshSample $azmayeshSample) + { + $azmayeshSample->delete(); + + return $this->successResponse(); + } +} diff --git a/app/Http/Controllers/V3/Azmayesh/AzmayeshTypeController.php b/app/Http/Controllers/V3/Azmayesh/AzmayeshTypeController.php index f9d1cf88..1f9f0b8d 100644 --- a/app/Http/Controllers/V3/Azmayesh/AzmayeshTypeController.php +++ b/app/Http/Controllers/V3/Azmayesh/AzmayeshTypeController.php @@ -21,7 +21,7 @@ class AzmayeshTypeController extends Controller public function fields(AzmayeshType $azmayeshType): JsonResponse { - $fields = $azmayeshType->azmayeshFields; + $fields = $azmayeshType->azmayeshFields->select('id', 'name', 'unit', 'type', 'option'); return $this->successResponse($fields); } diff --git a/app/Http/Requests/V3/Azmayesh/StoreRequest.php b/app/Http/Requests/V3/Azmayesh/StoreRequest.php new file mode 100644 index 00000000..aea7c1c4 --- /dev/null +++ b/app/Http/Requests/V3/Azmayesh/StoreRequest.php @@ -0,0 +1,40 @@ +|string> + */ + public function rules(): array + { + return [ + 'lat' => 'required|string', + 'lng' => 'required|string', + 'azmayesh_type_id' => 'required|exists:azmayesh_types,id', + 'request_date' => 'date', + 'report_date' => 'date', + 'request_number' => 'string', + 'employer' => 'string', + 'contractor' => 'string', + 'work_number' => 'string', + 'project_name' => 'required|string', + 'consultant' => 'string', + 'applicant' => 'string', + 'province_id' => 'required|exists:provinces,id', + ]; + } +} diff --git a/app/Http/Requests/V3/Azmayesh/UpdateRequest.php b/app/Http/Requests/V3/Azmayesh/UpdateRequest.php new file mode 100644 index 00000000..ad2854b2 --- /dev/null +++ b/app/Http/Requests/V3/Azmayesh/UpdateRequest.php @@ -0,0 +1,40 @@ +|string> + */ + public function rules(): array + { + return [ + 'lat' => 'required|string', + 'lng' => 'required|string', + 'azmayesh_type_id' => 'required|exists:azmayesh_types,id', + 'request_date' => 'date', + 'report_date' => 'date', + 'request_number' => 'string', + 'employer' => 'string', + 'contractor' => 'string', + 'work_number' => 'string', + 'project_name' => 'required|string', + 'consultant' => 'string', + 'applicant' => 'string', + 'province_id' => 'required|exists:provinces,id', + ]; + } +} diff --git a/app/Http/Requests/V3/AzmayeshSample/StoreRequest.php b/app/Http/Requests/V3/AzmayeshSample/StoreRequest.php new file mode 100644 index 00000000..fb1a5ea5 --- /dev/null +++ b/app/Http/Requests/V3/AzmayeshSample/StoreRequest.php @@ -0,0 +1,29 @@ +|string> + */ + public function rules(): array + { + return [ + 'azmayesh_id' => 'required|exists:azmayeshes,id', + 'data' => 'required', + ]; + } +} diff --git a/app/Http/Requests/V3/AzmayeshSample/UpdateRequest.php b/app/Http/Requests/V3/AzmayeshSample/UpdateRequest.php new file mode 100644 index 00000000..4ce5d38d --- /dev/null +++ b/app/Http/Requests/V3/AzmayeshSample/UpdateRequest.php @@ -0,0 +1,29 @@ +|string> + */ + public function rules(): array + { + return [ + 'azmayesh_id' => 'required|exists:azmayeshes,id', + 'data' => 'required', + ]; + } +} diff --git a/app/Models/AzmayeshField.php b/app/Models/AzmayeshField.php index d6bd90a2..efb49a4a 100644 --- a/app/Models/AzmayeshField.php +++ b/app/Models/AzmayeshField.php @@ -5,6 +5,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsToMany; +use Illuminate\Database\Eloquent\Casts\Attribute; class AzmayeshField extends Model { @@ -12,8 +13,15 @@ class AzmayeshField extends Model protected $guarded = []; + protected function option(): Attribute + { + return Attribute::make( + get: fn ($value) => json_decode($value), + ); + } + public function azmayeshTypes(): BelongsToMany { - return $this->belongsToMany(AzmayeshType::class); + return $this->belongsToMany(AzmayeshType::class, 'azmayesh_field_azmayesh_type'); } } diff --git a/database/migrations/2024_10_22_092756_create_azmayesh_fields_table.php b/database/migrations/2024_10_22_092756_create_azmayesh_fields_table.php index b68d79c7..56584489 100644 --- a/database/migrations/2024_10_22_092756_create_azmayesh_fields_table.php +++ b/database/migrations/2024_10_22_092756_create_azmayesh_fields_table.php @@ -14,9 +14,9 @@ return new class extends Migration Schema::create('azmayesh_fields', function (Blueprint $table) { $table->id(); $table->string('name'); - $table->string('unit'); + $table->string('unit')->nullable(); $table->string('type'); - $table->string('option')->nullable(); + $table->json('option')->nullable(); $table->timestamps(); }); } diff --git a/database/migrations/2024_10_22_092800_create_azmayeshes_table.php b/database/migrations/2024_10_22_092800_create_azmayeshes_table.php index f477cea9..a740e91d 100644 --- a/database/migrations/2024_10_22_092800_create_azmayeshes_table.php +++ b/database/migrations/2024_10_22_092800_create_azmayeshes_table.php @@ -20,12 +20,12 @@ return new class extends Migration $table->unsignedTinyInteger('province_id'); $table->foreign('province_id')->references('id')->on('provinces'); $table->string('province_name'); - $table->date('request_date'); - $table->date('report_date'); - $table->integer('request_number'); + $table->date('request_date')->nullable(); + $table->date('report_date')->nullable(); + $table->string('request_number'); $table->string('employer'); $table->string('contractor'); - $table->integer('work_number'); + $table->string('work_number'); $table->string('project_name'); $table->string('consultant'); $table->string('applicant'); diff --git a/database/migrations/2024_10_22_114035_create_azmayesh_samples_table.php b/database/migrations/2024_10_22_114035_create_azmayesh_samples_table.php index a4e61680..ae579b37 100644 --- a/database/migrations/2024_10_22_114035_create_azmayesh_samples_table.php +++ b/database/migrations/2024_10_22_114035_create_azmayesh_samples_table.php @@ -13,8 +13,8 @@ return new class extends Migration { Schema::create('azmayesh_samples', function (Blueprint $table) { $table->id(); - $table->foreignId('azmayesh_id')->constrained('azmayeshes'); - $table->string('azmayesh_name'); + $table->foreignId('azmayesh_id')->constrained('azmayeshes')->cascadeOnDelete(); + $table->string('azmayesh_project_name'); $table->json('data'); $table->timestamps(); }); diff --git a/database/seeders/AzmayeshFieldSeeder.php b/database/seeders/AzmayeshFieldSeeder.php index a4201fa3..123e2b4b 100644 --- a/database/seeders/AzmayeshFieldSeeder.php +++ b/database/seeders/AzmayeshFieldSeeder.php @@ -32,6 +32,24 @@ class AzmayeshFieldSeeder extends Seeder 'created_at' => now(), 'updated_at' => now() ], + [ + 'id' => 3, + 'name' => 'تاریخ آزمایش', + 'unit' => '-', + 'type' => 'date', + 'option' => null, + 'created_at' => now(), + 'updated_at' => now() + ], + [ + 'id' => 4, + 'name' => 'وضعیت سینی', + 'unit' => '-', + 'type' => 'select', + 'option' => json_encode(['طولی', 'عرضی']), + 'created_at' => now(), + 'updated_at' => now() + ], ]); } } diff --git a/database/seeders/AzmayeshSampleSeeder.php b/database/seeders/AzmayeshSampleSeeder.php index 66a53b02..c602e92e 100644 --- a/database/seeders/AzmayeshSampleSeeder.php +++ b/database/seeders/AzmayeshSampleSeeder.php @@ -17,10 +17,8 @@ class AzmayeshSampleSeeder extends Seeder DB::table('azmayesh_samples')->insert([ [ 'id' => 1, - 'azmayesh_id' => Azmayesh::factory(), - 'azmayesh_name' => function (array $attributes) { - return Azmayesh::query()->find($attributes['azmayesh_id'])->name; - }, + 'azmayesh_id' => 1, + 'azmayesh_project_name' => 'اجرای پروژه پل میرزای شیرازی شریفیه', 'data' => json_encode([ 1 => 'test' ]), diff --git a/database/seeders/AzmayeshSeeder.php b/database/seeders/AzmayeshSeeder.php index 65c04fea..b404afdb 100644 --- a/database/seeders/AzmayeshSeeder.php +++ b/database/seeders/AzmayeshSeeder.php @@ -16,41 +16,121 @@ class AzmayeshSeeder extends Seeder DB::table('azmayeshes')->insert([ [ 'id' => 1, - 'lat' => '13.45', - 'lng' => '11.85', + 'lat' => '36.2816000000', + 'lng' => '50.0153000000', 'azmayesh_type_id' => 1, - 'azmayesh_type_name' => 'test', - 'province_id' => 11, - 'province_name' => 'test', - 'request_date' => '1403-07-04', - 'report_date' => '1403-07-05', - 'request_number' => 0, - 'employer' => 'test', - 'contractor' => 'test', - 'work_number' => 0, - 'project_name' => 'test', - 'consultant' => 'test', - 'applicant' => 'test', + 'azmayesh_type_name' => 'تعیین مقاومت فشاری بتن', + 'province_id' => 20, + 'province_name' => 'قزوین', + 'request_date' => '1403-02-12', + 'report_date' => '1403-02-12', + 'request_number' => '-', + 'employer' => 'شهرداری شریفیه', + 'contractor' => 'شرکت طرح گستر رواق', + 'work_number' => '020040035', + 'project_name' => 'اجرای پروژه پل میرزای شیرازی شریفیه', + 'consultant' => '-', + 'applicant' => 'شهرداری شریفیه', 'created_at' => now(), 'updated_at' => now() ], [ 'id' => 2, - 'lat' => '13.45', - 'lng' => '11.85', - 'azmayesh_type_id' => 1, - 'azmayesh_type_name' => 'test', - 'province_id' => 11, - 'province_name' => 'test', + 'lat' => '36.2816000000', + 'lng' => '50.0153000000', + 'azmayesh_type_id' => 4, + 'azmayesh_type_name' => 'تعیین تراکم نسبی', + 'province_id' => 20, + 'province_name' => 'قزوین', + 'request_date' => '1403-01-15', + 'report_date' => '1403-07-07', + 'request_number' => '-', + 'employer' => 'شهرداری الوند', + 'contractor' => '-', + 'work_number' => '020040001', + 'project_name' => 'کنترل کیفی پروژه های اجرایی شهرداری الوند', + 'consultant' => '-', + 'applicant' => 'شهرداری الوند', + 'created_at' => now(), + 'updated_at' => now() + ], + [ + 'id' => 3, + 'lat' => '36.2816000000', + 'lng' => '50.0153000000', + 'azmayesh_type_id' => 3, + 'azmayesh_type_name' => 'مقدار قیر پاشیده شده', + 'province_id' => 20, + 'province_name' => 'قزوین', 'request_date' => '1403-07-04', 'report_date' => '1403-07-05', - 'request_number' => '0', - 'employer' => 'test', - 'contractor' => 'test', - 'work_number' => '0', - 'project_name' => 'test', - 'consultant' => 'test', - 'applicant' => 'test', + 'request_number' => '-', + 'employer' => 'بنیاد مسکن استان قزوین', + 'contractor' => 'امور اجرایی و ماشین آلات عمرانی بنیاد مسکن', + 'work_number' => '-', + 'project_name' => 'کنترل کیفی پروژه های روستای ولدآباد', + 'consultant' => '-', + 'applicant' => 'بنیاد مسکن استان قزوین', + 'created_at' => now(), + 'updated_at' => now() + ], + [ + 'id' => 4, + 'lat' => '36.2816000000', + 'lng' => '50.0153000000', + 'azmayesh_type_id' => 1, + 'azmayesh_type_name' => 'تعیین مقاومت فشاری بتن', + 'province_id' => 20, + 'province_name' => 'قزوین', + 'request_date' => '1403-06-15', + 'report_date' => null, + 'request_number' => '15/28296', + 'employer' => 'اداره کل راهداری و حمل و نقل جاده ای استان قزوین', + 'contractor' => 'شرکت تاژه صنعت سنگسر', + 'work_number' => '010060027', + 'project_name' => 'نیوجرسی مفصلی جهت ایمن سازی آزادراه های قزوین-کرج و قزوین-زنجان', + 'consultant' => '-', + 'applicant' => 'اداره کل راهداری و حمل و نقل جاده ای استان قزوین', + 'created_at' => now(), + 'updated_at' => now() + ], + [ + 'id' => 5, + 'lat' => '36.2816000000', + 'lng' => '50.0153000000', + 'azmayesh_type_id' => 2, + 'azmayesh_type_name' => 'دانه بندی و نتایج آزمایش های آسفالت گرم', + 'province_id' => 20, + 'province_name' => 'قزوین', + 'request_date' => '1403-06-27', + 'report_date' => '1403-07-07', + 'request_number' => 'راهداری-مجتمع آقای کلانی', + 'employer' => 'اداره کل راهداری و حمل و نقل جاده ای استان قزوین', + 'contractor' => 'شرکت آرتان راهسازان البرز قزوین', + 'work_number' => '010100005', + 'project_name' => 'احداث راه های دسترسی مجتمع خدمات رفاهی حوزه آبیک-مجتمع آقای کلانی', + 'consultant' => '-', + 'applicant' => 'اداره کل راهداری و حمل و نقل جاده ای استان قزوین', + 'created_at' => now(), + 'updated_at' => now() + ], + [ + 'id' => 6, + 'lat' => '36.2816000000', + 'lng' => '50.0153000000', + 'azmayesh_type_id' => 5, + 'azmayesh_type_name' => 'دانه بندی و نتایج آزمایش های مصالح سنگی', + 'province_id' => 20, + 'province_name' => 'قزوین', + 'request_date' => '1403-03-20', + 'report_date' => '1403-07-09', + 'request_number' => '1400/4361-ص', + 'employer' => 'شرکت سهامی عام کارخانجات قند قزوین', + 'contractor' => '-', + 'work_number' => '070020123', + 'project_name' => 'آماده سازی محوطه دپوی سیلو کارخانه قند قزوین', + 'consultant' => '-', + 'applicant' => 'شرکت سهامی عام کارخانجات قند قزوین', 'created_at' => now(), 'updated_at' => now() ], diff --git a/database/seeders/AzmayeshTypeSeeder.php b/database/seeders/AzmayeshTypeSeeder.php index 3d3f56e3..77244255 100644 --- a/database/seeders/AzmayeshTypeSeeder.php +++ b/database/seeders/AzmayeshTypeSeeder.php @@ -20,6 +20,30 @@ class AzmayeshTypeSeeder extends Seeder 'created_at' => now(), 'updated_at' => now() ], + [ + 'id' => 2, + 'name' => 'دانه بندی و نتایج آزمایش های آسفالت گرم', + 'created_at' => now(), + 'updated_at' => now() + ], + [ + 'id' => 3, + 'name' => 'مقدار قیر پاشیده شده', + 'created_at' => now(), + 'updated_at' => now() + ], + [ + 'id' => 4, + 'name' => 'تعیین تراکم نسبی', + 'created_at' => now(), + 'updated_at' => now() + ], + [ + 'id' => 5, + 'name' => 'دانه بندی و نتایج آزمایش های مصالح سنگی', + 'created_at' => now(), + 'updated_at' => now() + ], ]); } } diff --git a/routes/v3.php b/routes/v3.php index bf6e68a6..7dc98798 100644 --- a/routes/v3.php +++ b/routes/v3.php @@ -1,6 +1,7 @@ name('azmayesh_types') + ->name('azmayesh_types.') ->controller(AzmayeshTypeController::class) ->group(function () { Route::get('/', 'index')->name('index'); Route::get('/fields/{azmayeshType}', 'fields')->name('fields'); }); -Route::prefix('azmayeshes') +Route::prefix('azmayesh_samples') + ->name('azmayesh_samples.') + ->controller(AzmayeshSampleController::class) + ->group(function () { + Route::get('/index/{azmayesh_id}', 'index')->name('index'); + Route::get('/{azmayeshSample}', 'show')->name('show'); + Route::post('/store', 'store')->name('store'); + Route::post('/update/{azmayeshSample}', 'update')->name('update'); + Route::post('/delete/{azmayeshSample}', 'destroy')->name('delete'); + }); + + Route::prefix('azmayeshes') ->name('azmayeshes.') ->controller(AzmayeshController::class) ->group(function () { @@ -51,5 +63,5 @@ Route::prefix('azmayeshes') Route::get('/{azmayesh}', 'show')->name('show'); Route::post('/store', 'store')->name('store'); Route::post('/update/{azmayesh}', 'update')->name('update'); - Route::post('/delete/{azmayesh}', 'delete')->name('delete'); + Route::post('/delete/{azmayesh}', 'destroy')->name('delete'); }); From f37cfdb845cb834c9058c894d49487b9507262ce Mon Sep 17 00:00:00 2001 From: amirghasempoor Date: Sun, 3 Nov 2024 14:47:25 +0330 Subject: [PATCH 10/15] write tests for azmayesh resources --- .../V3/Azmayesh/AzmayeshSampleController.php | 2 + database/factories/AzmayeshFactory.php | 13 +- database/factories/AzmayeshSampleFactory.php | 4 +- resources/lang/fa/validation.php | 17 +- tests/Feature/V3/Azmayesh/DeleteTest.php | 44 +++ tests/Feature/V3/Azmayesh/IndexTest.php | 55 ++++ tests/Feature/V3/Azmayesh/StoreTest.php | 305 +++++++++++++++++ tests/Feature/V3/Azmayesh/UpdateTest.php | 309 ++++++++++++++++++ .../Feature/V3/AzmayeshSample/DeleteTest.php | 32 ++ tests/Feature/V3/AzmayeshSample/IndexTest.php | 86 +++++ tests/Feature/V3/AzmayeshSample/StoreTest.php | 79 +++++ .../Feature/V3/AzmayeshSample/UpdateTest.php | 82 +++++ tests/Feature/V3/AzmayeshType/FieldsTest.php | 65 ++++ tests/Feature/V3/AzmayeshType/IndexTest.php | 35 ++ 14 files changed, 1124 insertions(+), 4 deletions(-) create mode 100644 tests/Feature/V3/Azmayesh/DeleteTest.php create mode 100644 tests/Feature/V3/Azmayesh/IndexTest.php create mode 100644 tests/Feature/V3/Azmayesh/StoreTest.php create mode 100644 tests/Feature/V3/Azmayesh/UpdateTest.php create mode 100644 tests/Feature/V3/AzmayeshSample/DeleteTest.php create mode 100644 tests/Feature/V3/AzmayeshSample/IndexTest.php create mode 100644 tests/Feature/V3/AzmayeshSample/StoreTest.php create mode 100644 tests/Feature/V3/AzmayeshSample/UpdateTest.php create mode 100644 tests/Feature/V3/AzmayeshType/FieldsTest.php create mode 100644 tests/Feature/V3/AzmayeshType/IndexTest.php diff --git a/app/Http/Controllers/V3/Azmayesh/AzmayeshSampleController.php b/app/Http/Controllers/V3/Azmayesh/AzmayeshSampleController.php index a6861062..c5f98372 100644 --- a/app/Http/Controllers/V3/Azmayesh/AzmayeshSampleController.php +++ b/app/Http/Controllers/V3/Azmayesh/AzmayeshSampleController.php @@ -69,6 +69,8 @@ class AzmayeshSampleController extends Controller */ public function update(UpdateRequest $request, AzmayeshSample $azmayeshSample) { + $azmayesh = Azmayesh::query()->findOrFail($request->azmayesh_id); + $azmayeshSample->update([ 'azmayesh_id' => $azmayesh->id, 'azmayesh_project_name' => $azmayesh->project_name, diff --git a/database/factories/AzmayeshFactory.php b/database/factories/AzmayeshFactory.php index 78c19dae..e8aa9d2e 100644 --- a/database/factories/AzmayeshFactory.php +++ b/database/factories/AzmayeshFactory.php @@ -19,14 +19,25 @@ class AzmayeshFactory extends Factory public function definition(): array { return [ + 'lat' => $this->faker->numerify, + 'lng' => $this->faker->numerify, 'azmayesh_type_id' => AzmayeshType::factory(), 'azmayesh_type_name' => function (array $attributes) { return AzmayeshType::query()->find($attributes['azmayesh_type_id'])->name; }, 'province_id' => Province::factory(), 'province_name' => function (array $attributes) { - return Province::query()->find($attributes['province_id'])->name; + return Province::query()->find($attributes['province_id'])->name_fa; }, + 'request_date' => $this->faker->date, + 'report_date' => $this->faker->date, + 'request_number' => $this->faker->numerify, + 'employer' => $this->faker->name, + 'contractor' => $this->faker->name, + 'work_number' => $this->faker->numerify, + 'project_name' => $this->faker->name, + 'consultant' => $this->faker->name, + 'applicant' => $this->faker->name, ]; } } diff --git a/database/factories/AzmayeshSampleFactory.php b/database/factories/AzmayeshSampleFactory.php index f4b518a4..c288abf4 100644 --- a/database/factories/AzmayeshSampleFactory.php +++ b/database/factories/AzmayeshSampleFactory.php @@ -19,8 +19,8 @@ class AzmayeshSampleFactory extends Factory { return [ 'azmayesh_id' => Azmayesh::factory(), - 'azmayesh_name' => function (array $attributes) { - return Azmayesh::query()->find($attributes['azmayesh_id'])->name; + 'azmayesh_project_name' => function (array $attributes) { + return Azmayesh::query()->find($attributes['azmayesh_id'])->project_name; }, 'data' => json_encode(['test']), ]; diff --git a/resources/lang/fa/validation.php b/resources/lang/fa/validation.php index 8fc265b8..377a776d 100644 --- a/resources/lang/fa/validation.php +++ b/resources/lang/fa/validation.php @@ -179,6 +179,21 @@ return [ 'new_password' => 'رمز عبور جدید', 'degree' => 'مدرک تحصیلی', 'major' => 'شغل', - 'avatar' => 'آواتار' + 'avatar' => 'آواتار', + 'lat' => 'طول جغرافیایی', + 'lng' => 'عرض جغرافیایی', + 'azmayesh_type_id' => 'نوع آزمایش', + 'request_date' => 'تاریخ درخواست', + 'report_date' => 'تاریخ گزارش', + 'request_number' => 'شماره درخواست', + 'employer' => 'کارفرما', + 'contractor' => 'پیمانکار', + 'work_number' => 'شماره کار', + 'project_name' => 'نام پروژه', + 'consultant' => 'مشاور', + 'applicant' => 'متقاضی', + 'province_id' => 'استان', + 'azmayesh_id' => 'آزمایش', + 'data' => 'داده' ], ]; diff --git a/tests/Feature/V3/Azmayesh/DeleteTest.php b/tests/Feature/V3/Azmayesh/DeleteTest.php new file mode 100644 index 00000000..cafa4f74 --- /dev/null +++ b/tests/Feature/V3/Azmayesh/DeleteTest.php @@ -0,0 +1,44 @@ +create(); + $azmayesh = Azmayesh::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.delete', [$azmayesh->id])); + + $response->assertOk(); + + $this->assertDatabaseMissing('azmayeshes', [ + 'lat' => $azmayesh->lat, + 'lng' => $azmayesh->lng, + 'azmayesh_type_id' => $azmayesh->azmayesh_type_id, + 'azmayesh_type_name' => $azmayesh->azmayesh_type_name, + 'request_date' => $azmayesh->request_date, + 'report_date' => $azmayesh->report_date, + 'request_number' => $azmayesh->request_number, + 'employer' => $azmayesh->employer, + 'contractor' => $azmayesh->contractor, + 'work_number' => $azmayesh->work_number, + 'project_name' => $azmayesh->project_name, + 'consultant' => $azmayesh->consultant, + 'applicant' => $azmayesh->applicant, + 'province_id' => $azmayesh->province_id, + 'province_name' => $azmayesh->province_name, + ]); + } +} diff --git a/tests/Feature/V3/Azmayesh/IndexTest.php b/tests/Feature/V3/Azmayesh/IndexTest.php new file mode 100644 index 00000000..eae71b0c --- /dev/null +++ b/tests/Feature/V3/Azmayesh/IndexTest.php @@ -0,0 +1,55 @@ +create(); + + Azmayesh::factory(5)->create(); + + $response = $this->actingAs($user)->getJson(route('v3.azmayeshes.index', [ + 'start' => 0, + 'size' => 10, + 'filters' => json_encode([]), + 'sorting' => json_encode([]), + ])); + + $response->assertOk(); + + $response->assertJsonStructure([ + 'data' => [ + '*' => [ + 'id', + 'lat', + 'lng', + 'request_date', + 'report_date', + 'request_number', + 'employer', + 'contractor', + 'work_number', + 'consultant', + 'applicant', + 'project_name', + 'province_id', + ], + ], + "meta" => [ + "totalRowCount" + ] + ]); + } +} diff --git a/tests/Feature/V3/Azmayesh/StoreTest.php b/tests/Feature/V3/Azmayesh/StoreTest.php new file mode 100644 index 00000000..6ee556cf --- /dev/null +++ b/tests/Feature/V3/Azmayesh/StoreTest.php @@ -0,0 +1,305 @@ +create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.store')); + + $response->assertUnprocessable() + ->assertInvalid([ + 'lat' => __('validation.required', ['attribute' => 'طول جغرافیایی']) + ]); + } + + public function test_lat_attribute_should_be_string(): void + { + $user = User::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.store'), [ + 'lat' => $this->faker->numberBetween(1, 10) + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'lat' => __('validation.string', ['attribute' => 'طول جغرافیایی']) + ]); + } + + public function test_lng_attribute_is_required(): void + { + $user = User::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.store')); + + $response->assertUnprocessable() + ->assertInvalid([ + 'lng' => __('validation.required', ['attribute' => 'عرض جغرافیایی']) + ]); + } + + public function test_lng_attribute_should_be_string(): void + { + $user = User::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.store'), [ + 'lng' => $this->faker->numberBetween(1, 10) + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'lng' => __('validation.string', ['attribute' => 'عرض جغرافیایی']) + ]); + } + + public function test_azmayesh_type_id_attribute_is_required(): void + { + $user = User::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.store')); + + $response->assertUnprocessable() + ->assertInvalid([ + 'azmayesh_type_id' => __('validation.required', ['attribute' => 'نوع آزمایش']) + ]); + } + + public function test_azmayesh_type_id_attribute_should_exist_on_azmayesh_types_table(): void + { + $user = User::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.store'), [ + 'azmayesh_type_id' => $this->faker->numberBetween(10, 100) + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'azmayesh_type_id' => __('validation.exists', ['attribute' => 'نوع آزمایش']) + ]); + } + + public function test_request_date_attribute_should_be_date_type(): void + { + $user = User::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.store'), [ + 'request_date' => $this->faker->lexify + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'request_date' => __('validation.date', ['attribute' => 'تاریخ درخواست']) + ]); + } + + public function test_report_date_attribute_should_be_date_type(): void + { + $user = User::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.store'), [ + 'report_date' => $this->faker->lexify + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'report_date' => __('validation.date', ['attribute' => 'تاریخ گزارش']) + ]); + } + + public function test_request_number_attribute_should_be_string_type(): void + { + $user = User::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.store'), [ + 'request_number' => $this->faker->numberBetween(1, 10) + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'request_number' => __('validation.string', ['attribute' => 'شماره درخواست']) + ]); + } + + public function test_employer_attribute_should_be_string_type(): void + { + $user = User::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.store'), [ + 'employer' => $this->faker->numberBetween(1, 10) + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'employer' => __('validation.string', ['attribute' => 'کارفرما']) + ]); + } + + public function test_contractor_attribute_should_be_string_type(): void + { + $user = User::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.store'), [ + 'contractor' => $this->faker->numberBetween(1, 10) + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'contractor' => __('validation.string', ['attribute' => 'پیمانکار']) + ]); + } + + public function test_work_number_attribute_should_be_string_type(): void + { + $user = User::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.store'), [ + 'work_number' => $this->faker->numberBetween(1, 10) + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'work_number' => __('validation.string', ['attribute' => 'شماره کار']) + ]); + } + + public function test_project_name_attribute_is_required(): void + { + $user = User::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.store')); + + $response->assertUnprocessable() + ->assertInvalid([ + 'project_name' => __('validation.required', ['attribute' => 'نام پروژه']) + ]); + } + + public function test_project_name_attribute_should_be_string_type(): void + { + $user = User::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.store'), [ + 'project_name' => $this->faker->numberBetween(1, 10) + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'project_name' => __('validation.string', ['attribute' => 'نام پروژه']) + ]); + } + + public function test_consultant_attribute_should_be_string_type(): void + { + $user = User::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.store'), [ + 'consultant' => $this->faker->numberBetween(1, 10) + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'consultant' => __('validation.string', ['attribute' => 'مشاور']) + ]); + } + + public function test_applicant_attribute_should_be_string_type(): void + { + $user = User::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.store'), [ + 'applicant' => $this->faker->numberBetween(1, 10) + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'applicant' => __('validation.string', ['attribute' => 'متقاضی']) + ]); + } + + public function test_province_id_attribute_is_required(): void + { + $user = User::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.store')); + + $response->assertUnprocessable() + ->assertInvalid([ + 'province_id' => __('validation.required', ['attribute' => 'استان']) + ]); + } + + public function test_province_id_attribute_should_exists_on_province_table(): void + { + $user = User::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.store'), [ + 'province_id' => $this->faker->numberBetween(10, 100) + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'province_id' => __('validation.exists', ['attribute' => 'استان']) + ]); + } + + public function test_user_can_store_a_new_azmayesh(): void + { + $user = User::factory()->create(); + $province = Province::factory()->create(); + $azmayeshType = AzmayeshType::factory()->create(); + + $data = [ + 'lat' => $this->faker->numerify, + 'lng' => $this->faker->numerify, + 'azmayesh_type_id' => $azmayeshType->id, + 'request_date' => $this->faker->date, + 'report_date' => $this->faker->date, + 'request_number' => $this->faker->numerify, + 'employer' => $this->faker->name, + 'contractor' => $this->faker->name, + 'work_number' => $this->faker->numerify, + 'project_name' => $this->faker->name, + 'consultant' => $this->faker->name, + 'applicant' => $this->faker->name, + 'province_id' => $province->id, + ]; + + $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.store'), $data); + + $response->assertOk() + ->assertExactJson([ + 'message' => __('messages.successful') + ]); + + $this->assertDatabaseHas('azmayeshes', [ + 'lat' => $data['lat'], + 'lng' => $data['lng'], + 'azmayesh_type_id' => $data['azmayesh_type_id'], + 'request_date' => $data['request_date'], + 'report_date' => $data['report_date'], + 'request_number' => $data['request_number'], + 'employer' => $data['employer'], + 'contractor' => $data['contractor'], + 'work_number' => $data['work_number'], + 'project_name' => $data['project_name'], + 'consultant' => $data['consultant'], + 'applicant' => $data['applicant'], + 'province_id' => $data['province_id'], + ]); + } +} diff --git a/tests/Feature/V3/Azmayesh/UpdateTest.php b/tests/Feature/V3/Azmayesh/UpdateTest.php new file mode 100644 index 00000000..b20a0d8e --- /dev/null +++ b/tests/Feature/V3/Azmayesh/UpdateTest.php @@ -0,0 +1,309 @@ +create(); + $azmayesh = Azmayesh::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id])); + + $response->assertUnprocessable() + ->assertInvalid([ + 'lat' => __('validation.required', ['attribute' => 'طول جغرافیایی']) + ]); + } + + public function test_lat_attribute_should_be_string(): void + { + $user = User::factory()->create(); + $azmayesh = Azmayesh::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id]), [ + 'lat' => $this->faker->numberBetween(1, 10) + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'lat' => __('validation.string', ['attribute' => 'طول جغرافیایی']) + ]); + } + + public function test_lng_attribute_is_required(): void + { + $user = User::factory()->create(); + $azmayesh = Azmayesh::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id])); + + $response->assertUnprocessable() + ->assertInvalid([ + 'lng' => __('validation.required', ['attribute' => 'عرض جغرافیایی']) + ]); + } + + public function test_lng_attribute_should_be_string(): void + { + $user = User::factory()->create(); + $azmayesh = Azmayesh::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id]), [ + 'lng' => $this->faker->numberBetween(1, 10) + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'lng' => __('validation.string', ['attribute' => 'عرض جغرافیایی']) + ]); + } + + public function test_azmayesh_type_id_attribute_is_required(): void + { + $user = User::factory()->create(); + $azmayesh = Azmayesh::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id])); + + $response->assertUnprocessable() + ->assertInvalid([ + 'azmayesh_type_id' => __('validation.required', ['attribute' => 'نوع آزمایش']) + ]); + } + + public function test_azmayesh_type_id_attribute_should_exist_on_azmayesh_types_table(): void + { + $user = User::factory()->create(); + $azmayesh = Azmayesh::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id]), [ + 'azmayesh_type_id' => $this->faker->numberBetween(10, 100) + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'azmayesh_type_id' => __('validation.exists', ['attribute' => 'نوع آزمایش']) + ]); + } + + public function test_request_date_attribute_should_be_date_type(): void + { + $user = User::factory()->create(); + $azmayesh = Azmayesh::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id]), [ + 'request_date' => $this->faker->lexify + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'request_date' => __('validation.date', ['attribute' => 'تاریخ درخواست']) + ]); + } + + public function test_report_date_attribute_should_be_date_type(): void + { + $user = User::factory()->create(); + $azmayesh = Azmayesh::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id]), [ + 'report_date' => $this->faker->lexify + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'report_date' => __('validation.date', ['attribute' => 'تاریخ گزارش']) + ]); + } + + public function test_request_number_attribute_should_be_string_type(): void + { + $user = User::factory()->create(); + $azmayesh = Azmayesh::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id]), [ + 'request_number' => $this->faker->numberBetween(1, 10) + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'request_number' => __('validation.string', ['attribute' => 'شماره درخواست']) + ]); + } + + public function test_employer_attribute_should_be_string_type(): void + { + $user = User::factory()->create(); + $azmayesh = Azmayesh::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id]), [ + 'employer' => $this->faker->numberBetween(1, 10) + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'employer' => __('validation.string', ['attribute' => 'کارفرما']) + ]); + } + + public function test_contractor_attribute_should_be_string_type(): void + { + $user = User::factory()->create(); + $azmayesh = Azmayesh::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id]), [ + 'contractor' => $this->faker->numberBetween(1, 10) + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'contractor' => __('validation.string', ['attribute' => 'پیمانکار']) + ]); + } + + public function test_work_number_attribute_should_be_string_type(): void + { + $user = User::factory()->create(); + $azmayesh = Azmayesh::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id]), [ + 'work_number' => $this->faker->numberBetween(1, 10) + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'work_number' => __('validation.string', ['attribute' => 'شماره کار']) + ]); + } + + public function test_project_name_attribute_is_required(): void + { + $user = User::factory()->create(); + $azmayesh = Azmayesh::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id])); + + $response->assertUnprocessable() + ->assertInvalid([ + 'project_name' => __('validation.required', ['attribute' => 'نام پروژه']) + ]); + } + + public function test_project_name_attribute_should_be_string_type(): void + { + $user = User::factory()->create(); + $azmayesh = Azmayesh::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id]), [ + 'project_name' => $this->faker->numberBetween(1, 10) + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'project_name' => __('validation.string', ['attribute' => 'نام پروژه']) + ]); + } + + public function test_consultant_attribute_should_be_string_type(): void + { + $user = User::factory()->create(); + $azmayesh = Azmayesh::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id]), [ + 'consultant' => $this->faker->numberBetween(1, 10) + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'consultant' => __('validation.string', ['attribute' => 'مشاور']) + ]); + } + + public function test_applicant_attribute_should_be_string_type(): void + { + $user = User::factory()->create(); + $azmayesh = Azmayesh::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id]), [ + 'applicant' => $this->faker->numberBetween(1, 10) + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'applicant' => __('validation.string', ['attribute' => 'متقاضی']) + ]); + } + + public function test_province_id_attribute_is_required(): void + { + $user = User::factory()->create(); + $azmayesh = Azmayesh::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id])); + + $response->assertUnprocessable() + ->assertInvalid([ + 'province_id' => __('validation.required', ['attribute' => 'استان']) + ]); + } + + public function test_province_id_attribute_should_exists_on_province_table(): void + { + $user = User::factory()->create(); + $azmayesh = Azmayesh::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id]), [ + 'province_id' => $this->faker->numberBetween(10, 100) + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'province_id' => __('validation.exists', ['attribute' => 'استان']) + ]); + } + + public function test_user_can_update_the_azmayesh(): void + { + $user = User::factory()->create(); + $province = Province::factory()->create(); + $azmayeshType = AzmayeshType::factory()->create(); + $azmayesh = Azmayesh::factory()->create(); + + $data = [ + 'lat' => $this->faker->numerify, + 'lng' => $this->faker->numerify, + 'azmayesh_type_id' => $azmayeshType->id, + 'request_date' => $this->faker->date, + 'report_date' => $this->faker->date, + 'request_number' => $this->faker->numerify, + 'employer' => $this->faker->name, + 'contractor' => $this->faker->name, + 'work_number' => $this->faker->numerify, + 'project_name' => $this->faker->name, + 'consultant' => $this->faker->name, + 'applicant' => $this->faker->name, + 'province_id' => $province->id, + ]; + + $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id]), $data); + + $response->assertOk() + ->assertExactJson([ + 'message' => __('messages.successful') + ]); + } +} diff --git a/tests/Feature/V3/AzmayeshSample/DeleteTest.php b/tests/Feature/V3/AzmayeshSample/DeleteTest.php new file mode 100644 index 00000000..ac901f6e --- /dev/null +++ b/tests/Feature/V3/AzmayeshSample/DeleteTest.php @@ -0,0 +1,32 @@ +create(); + $azmayeshSample = AzmayeshSample::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_samples.delete', [$azmayeshSample->id])); + + $response->assertOk(); + + $this->assertDatabaseMissing('azmayesh_samples', [ + 'azmayesh_id' => $azmayeshSample->azmayesh_id, + 'data' => $azmayeshSample->data, + ]); + } +} diff --git a/tests/Feature/V3/AzmayeshSample/IndexTest.php b/tests/Feature/V3/AzmayeshSample/IndexTest.php new file mode 100644 index 00000000..4a447d99 --- /dev/null +++ b/tests/Feature/V3/AzmayeshSample/IndexTest.php @@ -0,0 +1,86 @@ +create(); + + Azmayesh::factory(5) + ->sequence( + ['id' => 1], + ['id' => 2], + ['id' => 3], + ['id' => 4], + ['id' => 5] + ) + ->create(); + + AzmayeshSample::factory(5) + ->sequence( + [ + 'azmayesh_id' => 1, + 'azmayesh_project_name' => $this->faker->name, + 'data' => json_encode([$this->faker->numerify]), + ], + [ + 'azmayesh_id' => 2, + 'azmayesh_project_name' => $this->faker->name, + 'data' => json_encode([$this->faker->numerify]), + ], + [ + 'azmayesh_id' => 3, + 'azmayesh_project_name' => $this->faker->name, + 'data' => json_encode([$this->faker->numerify]), + ], + [ + 'azmayesh_id' => 4, + 'azmayesh_project_name' => $this->faker->name, + 'data' => json_encode([$this->faker->numerify]), + ], + [ + 'azmayesh_id' => 1, + 'azmayesh_project_name' => $this->faker->name, + 'data' => json_encode([$this->faker->numerify]), + ], + ) + ->create(); + + $response = $this->actingAs($user)->getJson(route('v3.azmayesh_samples.index', [ + 1, + 'start' => 0, + 'size' => 10, + 'filters' => json_encode([]), + 'sorting' => json_encode([]), + ])); + + $response->assertOk(); + + $response->assertJsonStructure([ + 'data' => [ + '*' => [ + 'id', + 'azmayesh_id', + 'azmayesh_project_name', + 'data', + ], + ], + "meta" => [ + "totalRowCount" + ] + ]); + } +} diff --git a/tests/Feature/V3/AzmayeshSample/StoreTest.php b/tests/Feature/V3/AzmayeshSample/StoreTest.php new file mode 100644 index 00000000..28a6e1e0 --- /dev/null +++ b/tests/Feature/V3/AzmayeshSample/StoreTest.php @@ -0,0 +1,79 @@ +create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_samples.store')); + + $response->assertUnprocessable() + ->assertInvalid([ + 'azmayesh_id' => __('validation.required', ['attribute' => 'آزمایش']) + ]); + } + + public function test_azmayesh_id_attribute_should_exists_on_azmayeshes_table(): void + { + $user = User::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_samples.store'), [ + 'azmayesh_id' => $this->faker->numberBetween(10, 100) + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'azmayesh_id' => __('validation.exists', ['attribute' => 'آزمایش']) + ]); + } + + public function test_data_attribute_is_required(): void + { + $user = User::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_samples.store')); + + $response->assertUnprocessable() + ->assertInvalid([ + 'data' => __('validation.required', ['attribute' => 'داده']) + ]); + } + + public function test_user_can_store_a_new_azmayesh_sample(): void + { + $user = User::factory()->create(); + $azmayesh = Azmayesh::factory()->create(); + + $data = [ + 'azmayesh_id' => $azmayesh->id, + 'data' => json_encode([$this->faker->numerify]), + ]; + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_samples.store'), $data); + + $response->assertOk() + ->assertExactJson([ + 'message' => __('messages.successful') + ]); + + $this->assertDatabaseHas('azmayesh_samples', [ + 'azmayesh_id' => $data['azmayesh_id'], + 'data' => $data['data'], + ]); + } +} diff --git a/tests/Feature/V3/AzmayeshSample/UpdateTest.php b/tests/Feature/V3/AzmayeshSample/UpdateTest.php new file mode 100644 index 00000000..c79fef01 --- /dev/null +++ b/tests/Feature/V3/AzmayeshSample/UpdateTest.php @@ -0,0 +1,82 @@ +create(); + $azmayeshSample = AzmayeshSample::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_samples.update', [$azmayeshSample->id])); + + $response->assertUnprocessable() + ->assertInvalid([ + 'azmayesh_id' => __('validation.required', ['attribute' => 'آزمایش']) + ]); + } + + public function test_azmayesh_id_attribute_should_exists_on_azmayeshes_table(): void + { + $user = User::factory()->create(); + $azmayeshSample = AzmayeshSample::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_samples.update', [$azmayeshSample->id]), [ + 'azmayesh_id' => $this->faker->numberBetween(10, 100) + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'azmayesh_id' => __('validation.exists', ['attribute' => 'آزمایش']) + ]); + } + + public function test_data_attribute_is_required(): void + { + $user = User::factory()->create(); + $azmayeshSample = AzmayeshSample::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_samples.update', [$azmayeshSample->id])); + + $response->assertUnprocessable() + ->assertInvalid([ + 'data' => __('validation.required', ['attribute' => 'داده']) + ]); + } + + public function test_user_can_update_the_azmayesh_sample(): void + { + $user = User::factory()->create(); + $azmayesh = Azmayesh::factory()->create(); + $azmayeshSample = AzmayeshSample::factory()->create(); + + $data = [ + 'azmayesh_id' => $azmayesh->id, + 'data' => json_encode([$this->faker->numerify]), + ]; + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_samples.update', [$azmayeshSample->id]), $data); + + $response->assertOk() + ->assertExactJson([ + 'message' => __('messages.successful') + ]); + + $this->assertDatabaseHas('azmayesh_samples', [ + 'azmayesh_id' => $data['azmayesh_id'], + 'data' => $data['data'], + ]); + } +} diff --git a/tests/Feature/V3/AzmayeshType/FieldsTest.php b/tests/Feature/V3/AzmayeshType/FieldsTest.php new file mode 100644 index 00000000..b35cfc31 --- /dev/null +++ b/tests/Feature/V3/AzmayeshType/FieldsTest.php @@ -0,0 +1,65 @@ +sequence( + [ + 'id' => 1, + 'name' => $this->faker->name, + 'unit' => $this->faker->name, + 'type' => $this->faker->name, + ], + [ + 'id' => 2, + 'name' => $this->faker->name, + 'unit' => $this->faker->name, + 'type' => $this->faker->name, + ], + [ + 'id' => 3, + 'name' => $this->faker->name, + 'unit' => $this->faker->name, + 'type' => $this->faker->name, + ], + ) + ->create(); + + $azmayeshType = AzmayeshType::factory()->create(); + + $azmayeshType->azmayeshFields()->attach(1); + + $user = User::factory()->create(); + + $response = $this->actingAs($user)->getJson(route('v3.azmayesh_types.fields', [$azmayeshType->id])); + + $response->assertOk(); + + $response->assertJsonStructure([ + 'data' => [ + '*' => [ + 'name', + 'name', + 'unit', + 'type', + 'option' + ], + ] + ]); + } +} diff --git a/tests/Feature/V3/AzmayeshType/IndexTest.php b/tests/Feature/V3/AzmayeshType/IndexTest.php new file mode 100644 index 00000000..e96695f2 --- /dev/null +++ b/tests/Feature/V3/AzmayeshType/IndexTest.php @@ -0,0 +1,35 @@ +create(); + + $user = User::factory()->create(); + + $response = $this->actingAs($user)->getJson(route('v3.azmayesh_types.index')); + + $response->assertOk(); + + $response->assertJsonStructure([ + 'data' => [ + '*' => [ + 'name', + ], + ] + ]); + } +} From 8a1477660f01423eda9480719862b1605839a149 Mon Sep 17 00:00:00 2001 From: Witel Group Date: Wed, 6 Nov 2024 07:59:26 +0000 Subject: [PATCH 11/15] add more fields for azmayeshes --- .../V3/Azmayesh/AzmayeshSampleController.php | 2 +- database/seeders/AzmayeshFieldSeeder.php | 67 ++++++++++++++++++- tests/Feature/V3/AzmayeshType/FieldsTest.php | 1 - 3 files changed, 66 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/V3/Azmayesh/AzmayeshSampleController.php b/app/Http/Controllers/V3/Azmayesh/AzmayeshSampleController.php index c5f98372..2dee40af 100644 --- a/app/Http/Controllers/V3/Azmayesh/AzmayeshSampleController.php +++ b/app/Http/Controllers/V3/Azmayesh/AzmayeshSampleController.php @@ -74,7 +74,7 @@ class AzmayeshSampleController extends Controller $azmayeshSample->update([ 'azmayesh_id' => $azmayesh->id, 'azmayesh_project_name' => $azmayesh->project_name, - 'data' => json_encode($request->data), + 'data' => $request->data, ]); return $this->successResponse(); diff --git a/database/seeders/AzmayeshFieldSeeder.php b/database/seeders/AzmayeshFieldSeeder.php index 123e2b4b..c1b720ab 100644 --- a/database/seeders/AzmayeshFieldSeeder.php +++ b/database/seeders/AzmayeshFieldSeeder.php @@ -17,7 +17,7 @@ class AzmayeshFieldSeeder extends Seeder [ 'id' => 1, 'name' => 'طول', - 'unit' => 'سانتی متر', + 'unit' => 'cm', 'type' => 'input', 'option' => null, 'created_at' => now(), @@ -26,7 +26,7 @@ class AzmayeshFieldSeeder extends Seeder [ 'id' => 2, 'name' => 'عرض', - 'unit' => 'سانتی متر', + 'unit' => 'cm', 'type' => 'input', 'option' => null, 'created_at' => now(), @@ -50,6 +50,69 @@ class AzmayeshFieldSeeder extends Seeder 'created_at' => now(), 'updated_at' => now() ], + [ + 'id' => 5, + 'name' => 'ضخامت', + 'unit' => 'cm', + 'type' => 'input', + 'option' => null, + 'created_at' => now(), + 'updated_at' => now() + ], + [ + 'id' => 6, + 'name' => 'نوع لایه', + 'unit' => '-', + 'type' => 'select', + 'option' => json_encode(['اساس']), + 'created_at' => now(), + 'updated_at' => now() + ], + [ + 'id' => 7, + 'name' => 'مشخصات تراکم نسبی', + 'unit' => 'percent', + 'type' => 'input', + 'option' => null, + 'created_at' => now(), + 'updated_at' => now() + ], + [ + 'id' => 8, + 'name' => 'نیروی تصحیح شده', + 'unit' => 'kg', + 'type' => 'input', + 'option' => null, + 'created_at' => now(), + 'updated_at' => now() + ], + [ + 'id' => 9, + 'name' => 'مقاومت فشاری آزمونه', + 'unit' => 'kg/cm2', + 'type' => 'input', + 'option' => null, + 'created_at' => now(), + 'updated_at' => now() + ], + [ + 'id' => 10, + 'name' => 'مقاومت فشاری آزمونه استوانه ای متناظر', + 'unit' => 'kg/cm2', + 'type' => 'input', + 'option' => null, + 'created_at' => now(), + 'updated_at' => now() + ], + [ + 'id' => 11, + 'name' => 'مقاومت فشاری آزمونه استوانه ای متناظر', + 'unit' => 'kg/cm2', + 'type' => 'input', + 'option' => null, + 'created_at' => now(), + 'updated_at' => now() + ], ]); } } diff --git a/tests/Feature/V3/AzmayeshType/FieldsTest.php b/tests/Feature/V3/AzmayeshType/FieldsTest.php index b35cfc31..c6e5f7c4 100644 --- a/tests/Feature/V3/AzmayeshType/FieldsTest.php +++ b/tests/Feature/V3/AzmayeshType/FieldsTest.php @@ -53,7 +53,6 @@ class FieldsTest extends TestCase $response->assertJsonStructure([ 'data' => [ '*' => [ - 'name', 'name', 'unit', 'type', From fbb4db5e608647a49f9802fd1af09b9a4bfafffd Mon Sep 17 00:00:00 2001 From: amirghasempoor Date: Sat, 9 Nov 2024 10:52:57 +0330 Subject: [PATCH 12/15] create panel for azmayesh type crud --- .../V3/Azmayesh/AzmayeshSampleController.php | 6 +- .../V3/Azmayesh/AzmayeshTypeController.php | 69 +++++++++++++++++++ .../Requests/V3/AzmayeshType/StoreRequest.php | 29 ++++++++ .../V3/AzmayeshType/UpdateRequest.php | 29 ++++++++ routes/v3.php | 4 ++ 5 files changed, 134 insertions(+), 3 deletions(-) create mode 100644 app/Http/Requests/V3/AzmayeshType/StoreRequest.php create mode 100644 app/Http/Requests/V3/AzmayeshType/UpdateRequest.php diff --git a/app/Http/Controllers/V3/Azmayesh/AzmayeshSampleController.php b/app/Http/Controllers/V3/Azmayesh/AzmayeshSampleController.php index 2dee40af..9aa4303d 100644 --- a/app/Http/Controllers/V3/Azmayesh/AzmayeshSampleController.php +++ b/app/Http/Controllers/V3/Azmayesh/AzmayeshSampleController.php @@ -59,7 +59,7 @@ class AzmayeshSampleController extends Controller /** * Display the specified resource. */ - public function show(AzmayeshSample $azmayeshSample) + public function show(AzmayeshSample $azmayeshSample): JsonResponse { return $this->successResponse($azmayeshSample); } @@ -67,7 +67,7 @@ class AzmayeshSampleController extends Controller /** * Update the specified resource in storage. */ - public function update(UpdateRequest $request, AzmayeshSample $azmayeshSample) + public function update(UpdateRequest $request, AzmayeshSample $azmayeshSample): JsonResponse { $azmayesh = Azmayesh::query()->findOrFail($request->azmayesh_id); @@ -83,7 +83,7 @@ class AzmayeshSampleController extends Controller /** * Remove the specified resource from storage. */ - public function destroy(AzmayeshSample $azmayeshSample) + public function destroy(AzmayeshSample $azmayeshSample): JsonResponse { $azmayeshSample->delete(); diff --git a/app/Http/Controllers/V3/Azmayesh/AzmayeshTypeController.php b/app/Http/Controllers/V3/Azmayesh/AzmayeshTypeController.php index 1f9f0b8d..87d7af75 100644 --- a/app/Http/Controllers/V3/Azmayesh/AzmayeshTypeController.php +++ b/app/Http/Controllers/V3/Azmayesh/AzmayeshTypeController.php @@ -3,10 +3,14 @@ namespace App\Http\Controllers\V3\Azmayesh; use App\Http\Controllers\Controller; +use App\Http\Requests\V3\AzmayeshType\StoreRequest; +use App\Http\Requests\V3\AzmayeshType\UpdateRequest; use App\Http\Traits\ApiResponse; +use App\Models\AzmayeshField; use App\Models\AzmayeshType; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; +use Illuminate\Support\Facades\DB; class AzmayeshTypeController extends Controller { @@ -25,4 +29,69 @@ class AzmayeshTypeController extends Controller return $this->successResponse($fields); } + + public function store(StoreRequest $request): JsonResponse + { + DB::transaction(function () use ($request) + { + $azmayeshType = AzmayeshType::query()->create([ + 'name' => $request->name + ]); + + $fieldsID = []; + + foreach ($request->fields as $field) + { + $azmayeshField = AzmayeshField::query()->firstOrCreate($field, $field); + $fieldsID[] = $azmayeshField->id; + } + + $azmayeshType->azmayeshFields()->attach($fieldsID); + }); + + return $this->successResponse(); + } + + /** + * Display the specified resource. + */ + public function show(AzmayeshType $azmayeshType): JsonResponse + { + return $this->successResponse($azmayeshType->load(['azmayeshFields'])); + } + + /** + * Update the specified resource in storage. + */ + public function update(UpdateRequest $request, AzmayeshType $azmayeshType): JsonResponse + { + DB::transaction(function () use ($request, $azmayeshType) + { + $azmayeshType->update([ + 'name' => $request->name + ]); + + $fieldsID = []; + + foreach ($request->fields as $field) + { + $azmayeshField = AzmayeshField::query()->updateOrCreate($field, $field); + $fieldsID[] = $azmayeshField->id; + } + + $azmayeshType->azmayeshFields()->sync($fieldsID); + }); + + return $this->successResponse(); + } + + /** + * Remove the specified resource from storage. + */ + public function destroy(AzmayeshType $azmayeshType): JsonResponse + { + $azmayeshType->delete(); + + return $this->successResponse(); + } } diff --git a/app/Http/Requests/V3/AzmayeshType/StoreRequest.php b/app/Http/Requests/V3/AzmayeshType/StoreRequest.php new file mode 100644 index 00000000..e575eb20 --- /dev/null +++ b/app/Http/Requests/V3/AzmayeshType/StoreRequest.php @@ -0,0 +1,29 @@ +|string> + */ + public function rules(): array + { + return [ + 'name' => 'required|string', + 'fields' => 'required|array', + ]; + } +} diff --git a/app/Http/Requests/V3/AzmayeshType/UpdateRequest.php b/app/Http/Requests/V3/AzmayeshType/UpdateRequest.php new file mode 100644 index 00000000..deb49567 --- /dev/null +++ b/app/Http/Requests/V3/AzmayeshType/UpdateRequest.php @@ -0,0 +1,29 @@ +|string> + */ + public function rules(): array + { + return [ + 'name' => 'required|string', + 'fields' => 'required|array', + ]; + } +} diff --git a/routes/v3.php b/routes/v3.php index 7dc98798..4a75b278 100644 --- a/routes/v3.php +++ b/routes/v3.php @@ -42,6 +42,10 @@ Route::prefix('azmayesh_types') ->group(function () { Route::get('/', 'index')->name('index'); Route::get('/fields/{azmayeshType}', 'fields')->name('fields'); + Route::get('/{azmayeshType}', 'show')->name('show'); + Route::post('/store', 'store')->name('store'); + Route::post('/update/{azmayeshType}', 'update')->name('update'); + Route::post('/delete/{azmayeshType}', 'destroy')->name('delete'); }); Route::prefix('azmayesh_samples') From 117bf0269e24c7923c01614d2a3dd019c460ca96 Mon Sep 17 00:00:00 2001 From: amirghasempoor Date: Sat, 9 Nov 2024 11:36:16 +0330 Subject: [PATCH 13/15] add nested array validation to form requests --- .../V3/Azmayesh/AzmayeshTypeController.php | 23 ++++++++++++++++++- .../Requests/V3/AzmayeshType/StoreRequest.php | 4 ++++ .../V3/AzmayeshType/UpdateRequest.php | 4 ++++ ...ate_azmayesh_field_azmayesh_type_table.php | 4 ++-- routes/v3.php | 1 + 5 files changed, 33 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/V3/Azmayesh/AzmayeshTypeController.php b/app/Http/Controllers/V3/Azmayesh/AzmayeshTypeController.php index 87d7af75..f521c273 100644 --- a/app/Http/Controllers/V3/Azmayesh/AzmayeshTypeController.php +++ b/app/Http/Controllers/V3/Azmayesh/AzmayeshTypeController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers\V3\Azmayesh; +use App\Facades\DataTable\DataTableFacade; use App\Http\Controllers\Controller; use App\Http\Requests\V3\AzmayeshType\StoreRequest; use App\Http\Requests\V3\AzmayeshType\UpdateRequest; @@ -16,7 +17,27 @@ class AzmayeshTypeController extends Controller { use ApiResponse; - public function index(): JsonResponse + public function index(Request $request): JsonResponse + { + $columns = array( + 'id', 'name' + ); + + $allowedFilters = $columns; + $allowedSortings = $columns; + + $query = AzmayeshType::query(); + + $data = DataTableFacade::run( + $query, + $request, + allowedFilters: $allowedFilters, + allowedSortings: $allowedSortings); + + return response()->json($data); + } + + public function list(): JsonResponse { $allTypes = AzmayeshType::all(); diff --git a/app/Http/Requests/V3/AzmayeshType/StoreRequest.php b/app/Http/Requests/V3/AzmayeshType/StoreRequest.php index e575eb20..2c22ffc1 100644 --- a/app/Http/Requests/V3/AzmayeshType/StoreRequest.php +++ b/app/Http/Requests/V3/AzmayeshType/StoreRequest.php @@ -24,6 +24,10 @@ class StoreRequest extends FormRequest return [ 'name' => 'required|string', 'fields' => 'required|array', + 'fields.*.name' => 'required|string', + 'fields.*.unit' => 'string', + 'fields.*.type' => 'required|string', + 'fields.*.option' => 'string', ]; } } diff --git a/app/Http/Requests/V3/AzmayeshType/UpdateRequest.php b/app/Http/Requests/V3/AzmayeshType/UpdateRequest.php index deb49567..b7af342e 100644 --- a/app/Http/Requests/V3/AzmayeshType/UpdateRequest.php +++ b/app/Http/Requests/V3/AzmayeshType/UpdateRequest.php @@ -24,6 +24,10 @@ class UpdateRequest extends FormRequest return [ 'name' => 'required|string', 'fields' => 'required|array', + 'fields.*.name' => 'required|string', + 'fields.*.unit' => 'string', + 'fields.*.type' => 'required|string', + 'fields.*.option' => 'string', ]; } } diff --git a/database/migrations/2024_10_22_152052_create_azmayesh_field_azmayesh_type_table.php b/database/migrations/2024_10_22_152052_create_azmayesh_field_azmayesh_type_table.php index 5507811c..8d1505ac 100644 --- a/database/migrations/2024_10_22_152052_create_azmayesh_field_azmayesh_type_table.php +++ b/database/migrations/2024_10_22_152052_create_azmayesh_field_azmayesh_type_table.php @@ -13,8 +13,8 @@ return new class extends Migration { Schema::create('azmayesh_field_azmayesh_type', function (Blueprint $table) { $table->id(); - $table->foreignId('azmayesh_field_id')->constrained('azmayesh_fields'); - $table->foreignId('azmayesh_type_id')->constrained('azmayesh_types'); + $table->foreignId('azmayesh_field_id')->constrained('azmayesh_fields')->cascadeOnDelete(); + $table->foreignId('azmayesh_type_id')->constrained('azmayesh_types')->cascadeOnDelete(); $table->timestamps(); }); } diff --git a/routes/v3.php b/routes/v3.php index 4a75b278..8a43ad37 100644 --- a/routes/v3.php +++ b/routes/v3.php @@ -41,6 +41,7 @@ Route::prefix('azmayesh_types') ->controller(AzmayeshTypeController::class) ->group(function () { Route::get('/', 'index')->name('index'); + Route::get('/list', 'list')->name('list'); Route::get('/fields/{azmayeshType}', 'fields')->name('fields'); Route::get('/{azmayeshType}', 'show')->name('show'); Route::post('/store', 'store')->name('store'); From a84b1f946921dc994ca96fc64e9e8e7f7f86d78f Mon Sep 17 00:00:00 2001 From: amirghasempoor Date: Sun, 17 Nov 2024 14:13:25 +0330 Subject: [PATCH 14/15] write test for azmayesh type entity --- .../V3/Azmayesh/AzmayeshTypeController.php | 4 +- app/Models/AzmayeshField.php | 1 + app/Models/AzmayeshType.php | 1 + resources/lang/fa/validation.php | 7 +- storage/sql-files/edarate_shahri.sql | 1 + tests/Feature/V3/AzmayeshSample/StoreTest.php | 5 +- .../Feature/V3/AzmayeshSample/UpdateTest.php | 4 +- tests/Feature/V3/AzmayeshType/DeleteTest.php | 43 ++++ tests/Feature/V3/AzmayeshType/FieldsTest.php | 9 +- tests/Feature/V3/AzmayeshType/IndexTest.php | 8 +- tests/Feature/V3/AzmayeshType/ListTest.php | 35 +++ tests/Feature/V3/AzmayeshType/ShowTest.php | 51 ++++ tests/Feature/V3/AzmayeshType/StoreTest.php | 208 +++++++++++++++++ tests/Feature/V3/AzmayeshType/UpdateTest.php | 220 ++++++++++++++++++ 14 files changed, 588 insertions(+), 9 deletions(-) create mode 100644 tests/Feature/V3/AzmayeshType/DeleteTest.php create mode 100644 tests/Feature/V3/AzmayeshType/ListTest.php create mode 100644 tests/Feature/V3/AzmayeshType/ShowTest.php create mode 100644 tests/Feature/V3/AzmayeshType/StoreTest.php create mode 100644 tests/Feature/V3/AzmayeshType/UpdateTest.php diff --git a/app/Http/Controllers/V3/Azmayesh/AzmayeshTypeController.php b/app/Http/Controllers/V3/Azmayesh/AzmayeshTypeController.php index f521c273..d5445f45 100644 --- a/app/Http/Controllers/V3/Azmayesh/AzmayeshTypeController.php +++ b/app/Http/Controllers/V3/Azmayesh/AzmayeshTypeController.php @@ -46,9 +46,7 @@ class AzmayeshTypeController extends Controller public function fields(AzmayeshType $azmayeshType): JsonResponse { - $fields = $azmayeshType->azmayeshFields->select('id', 'name', 'unit', 'type', 'option'); - - return $this->successResponse($fields); + return $this->successResponse($azmayeshType->azmayeshFields); } public function store(StoreRequest $request): JsonResponse diff --git a/app/Models/AzmayeshField.php b/app/Models/AzmayeshField.php index efb49a4a..186d1ba4 100644 --- a/app/Models/AzmayeshField.php +++ b/app/Models/AzmayeshField.php @@ -12,6 +12,7 @@ class AzmayeshField extends Model use HasFactory; protected $guarded = []; + protected $hidden = ['pivot']; protected function option(): Attribute { diff --git a/app/Models/AzmayeshType.php b/app/Models/AzmayeshType.php index 0a5db09d..6ae6e396 100644 --- a/app/Models/AzmayeshType.php +++ b/app/Models/AzmayeshType.php @@ -12,6 +12,7 @@ class AzmayeshType extends Model use HasFactory; protected $guarded = []; + protected $hidden = ['pivot']; public function azmayeshes(): HasMany { diff --git a/resources/lang/fa/validation.php b/resources/lang/fa/validation.php index 377a776d..f35716e0 100644 --- a/resources/lang/fa/validation.php +++ b/resources/lang/fa/validation.php @@ -194,6 +194,11 @@ return [ 'applicant' => 'متقاضی', 'province_id' => 'استان', 'azmayesh_id' => 'آزمایش', - 'data' => 'داده' + 'data' => 'داده', + 'fields' => 'فیلد ها', + 'fields.*.name' => 'نام فیلد', + 'fields.*.unit' => 'واحد فیلد', + 'fields.*.type' => 'نوع فیلد', + 'fields.*.option' => 'گزینه های فیلد', ], ]; diff --git a/storage/sql-files/edarate_shahri.sql b/storage/sql-files/edarate_shahri.sql index b3a50c74..b61474b3 100644 --- a/storage/sql-files/edarate_shahri.sql +++ b/storage/sql-files/edarate_shahri.sql @@ -519,3 +519,4 @@ INSERT INTO edarate_shahri (name_fa,province_id,created_at,updated_at) VALUES INSERT INTO edarate_shahri (name_fa,province_id,created_at,updated_at) VALUES ('اداره خمام',28,'2023-06-17 08:43:14','2023-06-17 08:43:14'), ('اداره اصلاندوز',3,'2023-08-15 09:33:55','2023-08-15 09:33:55'); + ('اداره گالیکش',27,'2024-11-16 14:33:55','2024-11-16 14:33:55'); diff --git a/tests/Feature/V3/AzmayeshSample/StoreTest.php b/tests/Feature/V3/AzmayeshSample/StoreTest.php index 28a6e1e0..25772e48 100644 --- a/tests/Feature/V3/AzmayeshSample/StoreTest.php +++ b/tests/Feature/V3/AzmayeshSample/StoreTest.php @@ -3,6 +3,7 @@ namespace Tests\Feature\V3\AzmayeshSample; use App\Models\Azmayesh; +use App\Models\AzmayeshSample; use App\Models\AzmayeshType; use App\Models\Province; use App\Models\User; @@ -73,7 +74,9 @@ class StoreTest extends TestCase $this->assertDatabaseHas('azmayesh_samples', [ 'azmayesh_id' => $data['azmayesh_id'], - 'data' => $data['data'], +// 'data' => $data['data'], ]); + + $this->assertEquals(AzmayeshSample::first()->data, $data['data']); } } diff --git a/tests/Feature/V3/AzmayeshSample/UpdateTest.php b/tests/Feature/V3/AzmayeshSample/UpdateTest.php index c79fef01..1ec0f42e 100644 --- a/tests/Feature/V3/AzmayeshSample/UpdateTest.php +++ b/tests/Feature/V3/AzmayeshSample/UpdateTest.php @@ -76,7 +76,9 @@ class UpdateTest extends TestCase $this->assertDatabaseHas('azmayesh_samples', [ 'azmayesh_id' => $data['azmayesh_id'], - 'data' => $data['data'], +// 'data' => $data['data'], ]); + + $this->assertEquals(AzmayeshSample::first()->data, $data['data']); } } diff --git a/tests/Feature/V3/AzmayeshType/DeleteTest.php b/tests/Feature/V3/AzmayeshType/DeleteTest.php new file mode 100644 index 00000000..05b49035 --- /dev/null +++ b/tests/Feature/V3/AzmayeshType/DeleteTest.php @@ -0,0 +1,43 @@ +create(); + + $azmayeshType = AzmayeshType::factory()->create(); + $azmayeshField = AzmayeshField::factory()->create(); + + $azmayeshType->azmayeshFields()->attach([$azmayeshField->id]); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.delete', [$azmayeshType->id])); + + $response->assertOk() + ->assertExactJson([ + 'message' => __('messages.successful') + ]); + + $this->assertDatabaseMissing('azmayesh_types', [ + 'name' => $azmayeshType->name, + ]); + + $this->assertDatabaseMissing('azmayesh_field_azmayesh_type', [ + 'azmayesh_type_id' => $azmayeshType->id, + 'azmayesh_field_id' => $azmayeshField->id, + ]); + } +} diff --git a/tests/Feature/V3/AzmayeshType/FieldsTest.php b/tests/Feature/V3/AzmayeshType/FieldsTest.php index c6e5f7c4..7e4832ff 100644 --- a/tests/Feature/V3/AzmayeshType/FieldsTest.php +++ b/tests/Feature/V3/AzmayeshType/FieldsTest.php @@ -24,25 +24,28 @@ class FieldsTest extends TestCase 'name' => $this->faker->name, 'unit' => $this->faker->name, 'type' => $this->faker->name, + 'option' => json_encode([$this->faker->name]), ], [ 'id' => 2, 'name' => $this->faker->name, 'unit' => $this->faker->name, 'type' => $this->faker->name, + 'option' => json_encode([$this->faker->name]), ], [ 'id' => 3, 'name' => $this->faker->name, 'unit' => $this->faker->name, 'type' => $this->faker->name, + 'option' => json_encode([$this->faker->name]), ], ) ->create(); $azmayeshType = AzmayeshType::factory()->create(); - $azmayeshType->azmayeshFields()->attach(1); + $azmayeshType->azmayeshFields()->attach([1, 2, 3]); $user = User::factory()->create(); @@ -56,7 +59,9 @@ class FieldsTest extends TestCase 'name', 'unit', 'type', - 'option' + 'option', + "created_at", + "updated_at" ], ] ]); diff --git a/tests/Feature/V3/AzmayeshType/IndexTest.php b/tests/Feature/V3/AzmayeshType/IndexTest.php index e96695f2..d78ebe24 100644 --- a/tests/Feature/V3/AzmayeshType/IndexTest.php +++ b/tests/Feature/V3/AzmayeshType/IndexTest.php @@ -20,13 +20,19 @@ class IndexTest extends TestCase $user = User::factory()->create(); - $response = $this->actingAs($user)->getJson(route('v3.azmayesh_types.index')); + $response = $this->actingAs($user)->getJson(route('v3.azmayesh_types.index', [ + 'start' => 0, + 'size' => 10, + 'filters' => json_encode([]), + 'sorting' => json_encode([]), + ])); $response->assertOk(); $response->assertJsonStructure([ 'data' => [ '*' => [ + 'id', 'name', ], ] diff --git a/tests/Feature/V3/AzmayeshType/ListTest.php b/tests/Feature/V3/AzmayeshType/ListTest.php new file mode 100644 index 00000000..5aed890b --- /dev/null +++ b/tests/Feature/V3/AzmayeshType/ListTest.php @@ -0,0 +1,35 @@ +create(); + + $user = User::factory()->create(); + + $response = $this->actingAs($user)->getJson(route('v3.azmayesh_types.list')); + + $response->assertOk(); + + $response->assertJsonStructure([ + 'data' => [ + '*' => [ + 'name', + ], + ] + ]); + } +} diff --git a/tests/Feature/V3/AzmayeshType/ShowTest.php b/tests/Feature/V3/AzmayeshType/ShowTest.php new file mode 100644 index 00000000..f682f9e8 --- /dev/null +++ b/tests/Feature/V3/AzmayeshType/ShowTest.php @@ -0,0 +1,51 @@ +create(); + $azmayeshField = AzmayeshField::factory()->create(); + + $azmayeshType->azmayeshFields()->attach([$azmayeshField->id]); + + $user = User::factory()->create(); + + $response = $this->actingAs($user)->getJson(route('v3.azmayesh_types.show', [$azmayeshType->id])); + + $response->assertOk(); + + $response->assertExactJson([ + 'data' => [ + "id" => $azmayeshType->id, + "name" => $azmayeshType->name, + "created_at" => $azmayeshType->created_at, + "updated_at" => $azmayeshType->updated_at, + "azmayesh_fields" => [ + [ + "id" => $azmayeshField->id, + "name" => $azmayeshField->name, + "unit" => $azmayeshField->unit, + "type" => $azmayeshField->type, + "option" => $azmayeshField->option, + "created_at" => $azmayeshField->created_at, + "updated_at" => $azmayeshField->updated_at, + ] + ] + ] + ]); + } +} diff --git a/tests/Feature/V3/AzmayeshType/StoreTest.php b/tests/Feature/V3/AzmayeshType/StoreTest.php new file mode 100644 index 00000000..18cee337 --- /dev/null +++ b/tests/Feature/V3/AzmayeshType/StoreTest.php @@ -0,0 +1,208 @@ +create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.store')); + + $response->assertUnprocessable() + ->assertInvalid([ + 'name' => __('validation.required', ['attribute' => "نام"]) + ]); + } + + public function test_name_attribute_should_be_string(): void + { + $user = User::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.store'), [ + 'name' => $this->faker->numberBetween(1, 10) + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'name' => __('validation.string', ['attribute' => "نام"]) + ]); + } + + public function test_fields_attribute_is_required(): void + { + $user = User::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.store')); + + $response->assertUnprocessable() + ->assertInvalid([ + 'fields' => __('validation.required', ['attribute' => 'فیلد ها']) + ]); + } + + public function test_fields_attribute_should_be_array(): void + { + $user = User::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.store'), [ + 'fields' => $this->faker->numberBetween(1, 10) + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'fields' => __('validation.array', ['attribute' => 'فیلد ها']) + ]); + } + + public function test_field_name_is_required(): void + { + $user = User::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.store'), [ + 'fields' => [ + [] + ] + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'fields.0.name' => __('validation.required', ['attribute' => 'نام فیلد']) + ]); + } + + public function test_field_name_should_be_string(): void + { + $user = User::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.store'), [ + 'fields' => [ + ['name' => $this->faker->numberBetween(1, 10)] + ] + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'fields.0.name' => __('validation.string', ['attribute' => 'نام فیلد']) + ]); + } + + public function test_field_unit_should_be_string(): void + { + $user = User::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.store'), [ + 'fields' => [ + ['unit' => $this->faker->numberBetween(1, 10)] + ] + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'fields.0.unit' => __('validation.string', ['attribute' => 'واحد فیلد']) + ]); + } + + public function test_field_type_is_required(): void + { + $user = User::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.store'), [ + 'fields' => [ + [] + ] + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'fields.0.type' => __('validation.required', ['attribute' => 'نوع فیلد']) + ]); + } + + public function test_field_type_should_be_string(): void + { + $user = User::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.store'), [ + 'fields' => [ + ['type' => $this->faker->numberBetween(1, 10)] + ] + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'fields.0.type' => __('validation.string', ['attribute' => 'نوع فیلد']) + ]); + } + + public function test_field_option_should_be_string(): void + { + $user = User::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.store'), [ + 'fields' => [ + ['option' => $this->faker->numberBetween(1, 10)] + ] + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'fields.0.option' => __('validation.string', ['attribute' => 'گزینه های فیلد']) + ]); + } + + public function test_user_can_store_fields_for_an_azmayesh_type(): void + { + $user = User::factory()->create(); + + $data = [ + 'name' => $this->faker->name, + 'fields' => [ + [ + 'name' => $this->faker->name, + 'type' => $this->faker->randomElement(['input', 'select', 'date']), + 'unit' => $this->faker->name, + 'option' => json_encode(['test']), + ], + [ + 'name' => $this->faker->name, + 'type' => $this->faker->randomElement(['input', 'select', 'date']), + 'unit' => $this->faker->name, + ], + ] + ]; + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.store'), $data); + + $response->assertOk() + ->assertExactJson([ + 'message' => __('messages.successful') + ]); + + $this->assertDatabaseHas('azmayesh_fields', [ + 'name' => $data['fields'][0]['name'], + 'type' => $data['fields'][0]['type'], + 'unit' => $data['fields'][0]['unit'], + ]); + + $this->assertDatabaseHas('azmayesh_fields', [ + 'name' => $data['fields'][1]['name'], + 'type' => $data['fields'][1]['type'], + 'unit' => $data['fields'][1]['unit'], + ]); + + $this->assertDatabaseHas('azmayesh_types', [ + 'name' => $data['name'], + ]); + } +} diff --git a/tests/Feature/V3/AzmayeshType/UpdateTest.php b/tests/Feature/V3/AzmayeshType/UpdateTest.php new file mode 100644 index 00000000..7cc6ba40 --- /dev/null +++ b/tests/Feature/V3/AzmayeshType/UpdateTest.php @@ -0,0 +1,220 @@ +create(); + $azmayeshType = AzmayeshType::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.update', [$azmayeshType->id])); + + $response->assertUnprocessable() + ->assertInvalid([ + 'name' => __('validation.required', ['attribute' => "نام"]) + ]); + } + + public function test_name_attribute_should_be_string(): void + { + $user = User::factory()->create(); + $azmayeshType = AzmayeshType::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.update', [$azmayeshType->id]), [ + 'name' => $this->faker->numberBetween(1, 10) + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'name' => __('validation.string', ['attribute' => "نام"]) + ]); + } + + public function test_fields_attribute_is_required(): void + { + $user = User::factory()->create(); + $azmayeshType = AzmayeshType::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.update', [$azmayeshType->id])); + + $response->assertUnprocessable() + ->assertInvalid([ + 'fields' => __('validation.required', ['attribute' => 'فیلد ها']) + ]); + } + + public function test_fields_attribute_should_be_array(): void + { + $user = User::factory()->create(); + $azmayeshType = AzmayeshType::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.update', [$azmayeshType->id]), [ + 'fields' => $this->faker->numberBetween(1, 10) + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'fields' => __('validation.array', ['attribute' => 'فیلد ها']) + ]); + } + + public function test_field_name_is_required(): void + { + $user = User::factory()->create(); + $azmayeshType = AzmayeshType::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.update', [$azmayeshType->id]), [ + 'fields' => [ + [] + ] + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'fields.0.name' => __('validation.required', ['attribute' => 'نام فیلد']) + ]); + } + + public function test_field_name_should_be_string(): void + { + $user = User::factory()->create(); + $azmayeshType = AzmayeshType::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.update', [$azmayeshType->id]), [ + 'fields' => [ + ['name' => $this->faker->numberBetween(1, 10)] + ] + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'fields.0.name' => __('validation.string', ['attribute' => 'نام فیلد']) + ]); + } + + public function test_field_unit_should_be_string(): void + { + $user = User::factory()->create(); + $azmayeshType = AzmayeshType::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.update', [$azmayeshType->id]), [ + 'fields' => [ + ['unit' => $this->faker->numberBetween(1, 10)] + ] + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'fields.0.unit' => __('validation.string', ['attribute' => 'واحد فیلد']) + ]); + } + + public function test_field_type_is_required(): void + { + $user = User::factory()->create(); + $azmayeshType = AzmayeshType::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.update', [$azmayeshType->id]), [ + 'fields' => [ + [] + ] + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'fields.0.type' => __('validation.required', ['attribute' => 'نوع فیلد']) + ]); + } + + public function test_field_type_should_be_string(): void + { + $user = User::factory()->create(); + $azmayeshType = AzmayeshType::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.update', [$azmayeshType->id]), [ + 'fields' => [ + ['type' => $this->faker->numberBetween(1, 10)] + ] + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'fields.0.type' => __('validation.string', ['attribute' => 'نوع فیلد']) + ]); + } + + public function test_field_option_should_be_string(): void + { + $user = User::factory()->create(); + $azmayeshType = AzmayeshType::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.update', [$azmayeshType->id]), [ + 'fields' => [ + ['option' => $this->faker->numberBetween(1, 10)] + ] + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'fields.0.option' => __('validation.string', ['attribute' => 'گزینه های فیلد']) + ]); + } + + public function test_user_can_update_fields_for_an_azmayesh_type(): void + { + $user = User::factory()->create(); + $azmayeshType = AzmayeshType::factory()->create(); + + $data = [ + 'name' => $this->faker->name, + 'fields' => [ + [ + 'name' => $this->faker->name, + 'type' => $this->faker->randomElement(['input', 'select', 'date']), + 'unit' => $this->faker->name, + 'option' => json_encode(['test']), + ], + [ + 'name' => $this->faker->name, + 'type' => $this->faker->randomElement(['input', 'select', 'date']), + 'unit' => $this->faker->name, + ], + ] + ]; + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.update', [$azmayeshType->id]), $data); + + $response->assertOk() + ->assertExactJson([ + 'message' => __('messages.successful') + ]); + + $this->assertDatabaseHas('azmayesh_fields', [ + 'name' => $data['fields'][0]['name'], + 'type' => $data['fields'][0]['type'], + 'unit' => $data['fields'][0]['unit'], + ]); + + $this->assertDatabaseHas('azmayesh_fields', [ + 'name' => $data['fields'][1]['name'], + 'type' => $data['fields'][1]['type'], + 'unit' => $data['fields'][1]['unit'], + ]); + + $this->assertDatabaseHas('azmayesh_types', [ + 'name' => $data['name'], + ]); + } +} From 2f45671c05844521bf5266139321289e23fd92e3 Mon Sep 17 00:00:00 2001 From: amirghasempoor Date: Mon, 18 Nov 2024 11:37:30 +0330 Subject: [PATCH 15/15] create two permissions for azmayesh panel --- routes/v3.php | 3 + storage/sql-files/permissions.sql | 2 + tests/Feature/V3/Azmayesh/DeleteTest.php | 18 ++- tests/Feature/V3/Azmayesh/IndexTest.php | 21 ++- tests/Feature/V3/Azmayesh/StoreTest.php | 124 ++++++++++++++--- tests/Feature/V3/Azmayesh/UpdateTest.php | 125 +++++++++++++++--- .../Feature/V3/AzmayeshSample/DeleteTest.php | 17 ++- tests/Feature/V3/AzmayeshSample/IndexTest.php | 22 ++- tests/Feature/V3/AzmayeshSample/StoreTest.php | 34 ++++- .../Feature/V3/AzmayeshSample/UpdateTest.php | 35 ++++- tests/Feature/V3/AzmayeshType/DeleteTest.php | 17 ++- tests/Feature/V3/AzmayeshType/FieldsTest.php | 17 ++- tests/Feature/V3/AzmayeshType/IndexTest.php | 21 ++- tests/Feature/V3/AzmayeshType/ListTest.php | 16 ++- tests/Feature/V3/AzmayeshType/ShowTest.php | 17 ++- tests/Feature/V3/AzmayeshType/StoreTest.php | 76 +++++++++-- tests/Feature/V3/AzmayeshType/UpdateTest.php | 78 +++++++++-- 17 files changed, 566 insertions(+), 77 deletions(-) diff --git a/routes/v3.php b/routes/v3.php index 8a43ad37..69abb65c 100644 --- a/routes/v3.php +++ b/routes/v3.php @@ -38,6 +38,7 @@ Route::prefix('profile') Route::prefix('azmayesh_types') ->name('azmayesh_types.') + ->middleware('can:azmayesh-type-management') ->controller(AzmayeshTypeController::class) ->group(function () { Route::get('/', 'index')->name('index'); @@ -51,6 +52,7 @@ Route::prefix('azmayesh_types') Route::prefix('azmayesh_samples') ->name('azmayesh_samples.') + ->middleware('can:azmayesh-management') ->controller(AzmayeshSampleController::class) ->group(function () { Route::get('/index/{azmayesh_id}', 'index')->name('index'); @@ -62,6 +64,7 @@ Route::prefix('azmayesh_samples') Route::prefix('azmayeshes') ->name('azmayeshes.') + ->middleware('can:azmayesh-management') ->controller(AzmayeshController::class) ->group(function () { Route::get('/', 'index')->name('index'); diff --git a/storage/sql-files/permissions.sql b/storage/sql-files/permissions.sql index 819a8445..a03a7003 100644 --- a/storage/sql-files/permissions.sql +++ b/storage/sql-files/permissions.sql @@ -133,3 +133,5 @@ INSERT INTO permissions (name,guard_name,name_fa,description,created_at,updated_ ('show-receipt','web','نمایش خسارت (کشوری)','نمایش خسارات کشوری',NULL,NULL,NULL,NULL,'receipt','خسارات وارد بر ابنیه فنی راه',0,0), ('show-receipt-province','web','نمایش خسارت (استانی)','نمایش خسارات استانی',NULL,NULL,NULL,NULL,'receipt','خسارات وارد بر ابنیه فنی راه',1,0), ('supervise-road-item-by-edareostani','web','نظارت بر ثبت فعالیت روزانه و جاری(ادارات استانی)',NULL,'2023-07-09 09:48:35','2023-07-09 09:48:37',NULL,NULL,'road-item','فعالیت روزانه',0,0); + ('azmayesh-management','web','مدیریت آزمایشات',NULL,'2024-11-18 09:48:35','2024-11-18 09:48:35',NULL,NULL,'azmayesh','آزمایشات',0,0); + ('azmayesh-type-management','web','مدیریت نوع آزمایشات',NULL,'2024-11-18 09:48:35','2024-11-18 09:48:35',NULL,NULL,'azmayesh','آزمایشات',0,0); diff --git a/tests/Feature/V3/Azmayesh/DeleteTest.php b/tests/Feature/V3/Azmayesh/DeleteTest.php index cafa4f74..04b819ab 100644 --- a/tests/Feature/V3/Azmayesh/DeleteTest.php +++ b/tests/Feature/V3/Azmayesh/DeleteTest.php @@ -3,6 +3,7 @@ namespace Tests\Feature\V3\Azmayesh; use App\Models\Azmayesh; +use App\Models\Permission; use App\Models\User; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithFaker; @@ -14,13 +15,28 @@ class DeleteTest extends TestCase /** * A basic feature test example. */ - public function test_user_can_delete_azmayesh(): void + public function test_user_should_have_azmayesh_management_permission(): void { $user = User::factory()->create(); $azmayesh = Azmayesh::factory()->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.delete', [$azmayesh->id])); + $response->assertForbidden(); + } + + public function test_user_can_delete_azmayesh(): void + { + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); + + $azmayesh = Azmayesh::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.delete', [$azmayesh->id])); + $response->assertOk(); $this->assertDatabaseMissing('azmayeshes', [ diff --git a/tests/Feature/V3/Azmayesh/IndexTest.php b/tests/Feature/V3/Azmayesh/IndexTest.php index eae71b0c..cb525109 100644 --- a/tests/Feature/V3/Azmayesh/IndexTest.php +++ b/tests/Feature/V3/Azmayesh/IndexTest.php @@ -3,6 +3,7 @@ namespace Tests\Feature\V3\Azmayesh; use App\Models\Azmayesh; +use App\Models\Permission; use App\Models\User; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithFaker; @@ -14,10 +15,28 @@ class IndexTest extends TestCase /** * A basic feature test example. */ - public function test_all_azmayeshes_returned_successfully(): void + public function test_user_should_have_azmayesh_management_permission(): void { $user = User::factory()->create(); + $response = $this->actingAs($user)->getJson(route('v3.azmayeshes.index', [ + 'start' => 0, + 'size' => 10, + 'filters' => json_encode([]), + 'sorting' => json_encode([]), + ])); + + $response->assertForbidden(); + } + + public function test_all_azmayeshes_returned_successfully(): void + { + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); + Azmayesh::factory(5)->create(); $response = $this->actingAs($user)->getJson(route('v3.azmayeshes.index', [ diff --git a/tests/Feature/V3/Azmayesh/StoreTest.php b/tests/Feature/V3/Azmayesh/StoreTest.php index 6ee556cf..9f1d71ea 100644 --- a/tests/Feature/V3/Azmayesh/StoreTest.php +++ b/tests/Feature/V3/Azmayesh/StoreTest.php @@ -3,6 +3,7 @@ namespace Tests\Feature\V3\Azmayesh; use App\Models\AzmayeshType; +use App\Models\Permission; use App\Models\Province; use App\Models\User; use Illuminate\Foundation\Testing\RefreshDatabase; @@ -15,12 +16,25 @@ class StoreTest extends TestCase /** * A basic feature test example. */ - public function test_lat_attribute_is_required(): void + public function test_user_should_have_azmayesh_management_permission(): void { $user = User::factory()->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.store')); + $response->assertForbidden(); + } + + public function test_lat_attribute_is_required(): void + { + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.store')); + $response->assertUnprocessable() ->assertInvalid([ 'lat' => __('validation.required', ['attribute' => 'طول جغرافیایی']) @@ -29,7 +43,11 @@ class StoreTest extends TestCase public function test_lat_attribute_should_be_string(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.store'), [ 'lat' => $this->faker->numberBetween(1, 10) @@ -43,7 +61,11 @@ class StoreTest extends TestCase public function test_lng_attribute_is_required(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.store')); @@ -55,7 +77,11 @@ class StoreTest extends TestCase public function test_lng_attribute_should_be_string(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.store'), [ 'lng' => $this->faker->numberBetween(1, 10) @@ -69,7 +95,11 @@ class StoreTest extends TestCase public function test_azmayesh_type_id_attribute_is_required(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.store')); @@ -81,7 +111,11 @@ class StoreTest extends TestCase public function test_azmayesh_type_id_attribute_should_exist_on_azmayesh_types_table(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.store'), [ 'azmayesh_type_id' => $this->faker->numberBetween(10, 100) @@ -95,7 +129,11 @@ class StoreTest extends TestCase public function test_request_date_attribute_should_be_date_type(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.store'), [ 'request_date' => $this->faker->lexify @@ -109,7 +147,11 @@ class StoreTest extends TestCase public function test_report_date_attribute_should_be_date_type(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.store'), [ 'report_date' => $this->faker->lexify @@ -123,7 +165,11 @@ class StoreTest extends TestCase public function test_request_number_attribute_should_be_string_type(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.store'), [ 'request_number' => $this->faker->numberBetween(1, 10) @@ -137,7 +183,11 @@ class StoreTest extends TestCase public function test_employer_attribute_should_be_string_type(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.store'), [ 'employer' => $this->faker->numberBetween(1, 10) @@ -151,7 +201,11 @@ class StoreTest extends TestCase public function test_contractor_attribute_should_be_string_type(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.store'), [ 'contractor' => $this->faker->numberBetween(1, 10) @@ -165,7 +219,11 @@ class StoreTest extends TestCase public function test_work_number_attribute_should_be_string_type(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.store'), [ 'work_number' => $this->faker->numberBetween(1, 10) @@ -179,7 +237,11 @@ class StoreTest extends TestCase public function test_project_name_attribute_is_required(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.store')); @@ -191,7 +253,11 @@ class StoreTest extends TestCase public function test_project_name_attribute_should_be_string_type(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.store'), [ 'project_name' => $this->faker->numberBetween(1, 10) @@ -205,7 +271,11 @@ class StoreTest extends TestCase public function test_consultant_attribute_should_be_string_type(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.store'), [ 'consultant' => $this->faker->numberBetween(1, 10) @@ -219,7 +289,11 @@ class StoreTest extends TestCase public function test_applicant_attribute_should_be_string_type(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.store'), [ 'applicant' => $this->faker->numberBetween(1, 10) @@ -233,7 +307,11 @@ class StoreTest extends TestCase public function test_province_id_attribute_is_required(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.store')); @@ -245,7 +323,11 @@ class StoreTest extends TestCase public function test_province_id_attribute_should_exists_on_province_table(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.store'), [ 'province_id' => $this->faker->numberBetween(10, 100) @@ -259,7 +341,11 @@ class StoreTest extends TestCase public function test_user_can_store_a_new_azmayesh(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); $province = Province::factory()->create(); $azmayeshType = AzmayeshType::factory()->create(); diff --git a/tests/Feature/V3/Azmayesh/UpdateTest.php b/tests/Feature/V3/Azmayesh/UpdateTest.php index b20a0d8e..9a51ecb3 100644 --- a/tests/Feature/V3/Azmayesh/UpdateTest.php +++ b/tests/Feature/V3/Azmayesh/UpdateTest.php @@ -4,6 +4,7 @@ namespace Tests\Feature\V3\Azmayesh; use App\Models\Azmayesh; use App\Models\AzmayeshType; +use App\Models\Permission; use App\Models\Province; use App\Models\User; use Illuminate\Foundation\Testing\RefreshDatabase; @@ -16,13 +17,27 @@ class UpdateTest extends TestCase /** * A basic feature test example. */ - public function test_lat_attribute_is_required(): void + public function test_user_should_have_azmayesh_management_permission(): void { $user = User::factory()->create(); $azmayesh = Azmayesh::factory()->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id])); + $response->assertForbidden(); + } + + public function test_lat_attribute_is_required(): void + { + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); + $azmayesh = Azmayesh::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id])); + $response->assertUnprocessable() ->assertInvalid([ 'lat' => __('validation.required', ['attribute' => 'طول جغرافیایی']) @@ -31,7 +46,11 @@ class UpdateTest extends TestCase public function test_lat_attribute_should_be_string(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); $azmayesh = Azmayesh::factory()->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id]), [ @@ -46,7 +65,11 @@ class UpdateTest extends TestCase public function test_lng_attribute_is_required(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); $azmayesh = Azmayesh::factory()->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id])); @@ -59,7 +82,11 @@ class UpdateTest extends TestCase public function test_lng_attribute_should_be_string(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); $azmayesh = Azmayesh::factory()->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id]), [ @@ -74,7 +101,11 @@ class UpdateTest extends TestCase public function test_azmayesh_type_id_attribute_is_required(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); $azmayesh = Azmayesh::factory()->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id])); @@ -87,7 +118,11 @@ class UpdateTest extends TestCase public function test_azmayesh_type_id_attribute_should_exist_on_azmayesh_types_table(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); $azmayesh = Azmayesh::factory()->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id]), [ @@ -102,7 +137,11 @@ class UpdateTest extends TestCase public function test_request_date_attribute_should_be_date_type(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); $azmayesh = Azmayesh::factory()->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id]), [ @@ -117,7 +156,11 @@ class UpdateTest extends TestCase public function test_report_date_attribute_should_be_date_type(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); $azmayesh = Azmayesh::factory()->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id]), [ @@ -132,7 +175,11 @@ class UpdateTest extends TestCase public function test_request_number_attribute_should_be_string_type(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); $azmayesh = Azmayesh::factory()->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id]), [ @@ -147,7 +194,11 @@ class UpdateTest extends TestCase public function test_employer_attribute_should_be_string_type(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); $azmayesh = Azmayesh::factory()->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id]), [ @@ -162,7 +213,11 @@ class UpdateTest extends TestCase public function test_contractor_attribute_should_be_string_type(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); $azmayesh = Azmayesh::factory()->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id]), [ @@ -177,7 +232,11 @@ class UpdateTest extends TestCase public function test_work_number_attribute_should_be_string_type(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); $azmayesh = Azmayesh::factory()->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id]), [ @@ -192,7 +251,11 @@ class UpdateTest extends TestCase public function test_project_name_attribute_is_required(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); $azmayesh = Azmayesh::factory()->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id])); @@ -205,7 +268,11 @@ class UpdateTest extends TestCase public function test_project_name_attribute_should_be_string_type(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); $azmayesh = Azmayesh::factory()->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id]), [ @@ -220,7 +287,11 @@ class UpdateTest extends TestCase public function test_consultant_attribute_should_be_string_type(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); $azmayesh = Azmayesh::factory()->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id]), [ @@ -235,7 +306,11 @@ class UpdateTest extends TestCase public function test_applicant_attribute_should_be_string_type(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); $azmayesh = Azmayesh::factory()->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id]), [ @@ -250,7 +325,11 @@ class UpdateTest extends TestCase public function test_province_id_attribute_is_required(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); $azmayesh = Azmayesh::factory()->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id])); @@ -263,7 +342,11 @@ class UpdateTest extends TestCase public function test_province_id_attribute_should_exists_on_province_table(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); $azmayesh = Azmayesh::factory()->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id]), [ @@ -278,7 +361,11 @@ class UpdateTest extends TestCase public function test_user_can_update_the_azmayesh(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); $province = Province::factory()->create(); $azmayeshType = AzmayeshType::factory()->create(); $azmayesh = Azmayesh::factory()->create(); diff --git a/tests/Feature/V3/AzmayeshSample/DeleteTest.php b/tests/Feature/V3/AzmayeshSample/DeleteTest.php index ac901f6e..bc99d51d 100644 --- a/tests/Feature/V3/AzmayeshSample/DeleteTest.php +++ b/tests/Feature/V3/AzmayeshSample/DeleteTest.php @@ -4,6 +4,7 @@ namespace Tests\Feature\V3\AzmayeshSample; use App\Models\Azmayesh; use App\Models\AzmayeshSample; +use App\Models\Permission; use App\Models\User; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithFaker; @@ -15,13 +16,27 @@ class DeleteTest extends TestCase /** * A basic feature test example. */ - public function test_user_can_delete_azmayesh_sample(): void + public function test_user_should_have_azmayesh_management_permission(): void { $user = User::factory()->create(); $azmayeshSample = AzmayeshSample::factory()->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayesh_samples.delete', [$azmayeshSample->id])); + $response->assertForbidden(); + } + + public function test_user_can_delete_azmayesh_sample(): void + { + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); + $azmayeshSample = AzmayeshSample::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_samples.delete', [$azmayeshSample->id])); + $response->assertOk(); $this->assertDatabaseMissing('azmayesh_samples', [ diff --git a/tests/Feature/V3/AzmayeshSample/IndexTest.php b/tests/Feature/V3/AzmayeshSample/IndexTest.php index 4a447d99..59b17fbc 100644 --- a/tests/Feature/V3/AzmayeshSample/IndexTest.php +++ b/tests/Feature/V3/AzmayeshSample/IndexTest.php @@ -4,6 +4,7 @@ namespace Tests\Feature\V3\AzmayeshSample; use App\Models\Azmayesh; use App\Models\AzmayeshSample; +use App\Models\Permission; use App\Models\User; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithFaker; @@ -15,10 +16,29 @@ class IndexTest extends TestCase /** * A basic feature test example. */ - public function test_all_azmayesh_samples_returned_successfully(): void + public function test_user_should_have_azmayesh_management_permission(): void { $user = User::factory()->create(); + $response = $this->actingAs($user)->getJson(route('v3.azmayesh_samples.index', [ + 1, + 'start' => 0, + 'size' => 10, + 'filters' => json_encode([]), + 'sorting' => json_encode([]), + ])); + + $response->assertForbidden(); + } + + public function test_all_azmayesh_samples_returned_successfully(): void + { + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); + Azmayesh::factory(5) ->sequence( ['id' => 1], diff --git a/tests/Feature/V3/AzmayeshSample/StoreTest.php b/tests/Feature/V3/AzmayeshSample/StoreTest.php index 25772e48..b6fffa55 100644 --- a/tests/Feature/V3/AzmayeshSample/StoreTest.php +++ b/tests/Feature/V3/AzmayeshSample/StoreTest.php @@ -5,6 +5,7 @@ namespace Tests\Feature\V3\AzmayeshSample; use App\Models\Azmayesh; use App\Models\AzmayeshSample; use App\Models\AzmayeshType; +use App\Models\Permission; use App\Models\Province; use App\Models\User; use Illuminate\Foundation\Testing\RefreshDatabase; @@ -17,12 +18,25 @@ class StoreTest extends TestCase /** * A basic feature test example. */ - public function test_azmayesh_id_attribute_is_required(): void + public function test_user_should_have_azmayesh_management_permission(): void { $user = User::factory()->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayesh_samples.store')); + $response->assertForbidden(); + } + + public function test_azmayesh_id_attribute_is_required(): void + { + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_samples.store')); + $response->assertUnprocessable() ->assertInvalid([ 'azmayesh_id' => __('validation.required', ['attribute' => 'آزمایش']) @@ -31,7 +45,11 @@ class StoreTest extends TestCase public function test_azmayesh_id_attribute_should_exists_on_azmayeshes_table(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayesh_samples.store'), [ 'azmayesh_id' => $this->faker->numberBetween(10, 100) @@ -45,7 +63,11 @@ class StoreTest extends TestCase public function test_data_attribute_is_required(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayesh_samples.store')); @@ -57,7 +79,11 @@ class StoreTest extends TestCase public function test_user_can_store_a_new_azmayesh_sample(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); $azmayesh = Azmayesh::factory()->create(); $data = [ diff --git a/tests/Feature/V3/AzmayeshSample/UpdateTest.php b/tests/Feature/V3/AzmayeshSample/UpdateTest.php index 1ec0f42e..542fb97f 100644 --- a/tests/Feature/V3/AzmayeshSample/UpdateTest.php +++ b/tests/Feature/V3/AzmayeshSample/UpdateTest.php @@ -4,6 +4,7 @@ namespace Tests\Feature\V3\AzmayeshSample; use App\Models\Azmayesh; use App\Models\AzmayeshSample; +use App\Models\Permission; use App\Models\User; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithFaker; @@ -15,13 +16,27 @@ class UpdateTest extends TestCase /** * A basic feature test example. */ - public function test_azmayesh_id_attribute_is_required(): void + public function test_user_should_have_azmayesh_management_permission(): void { $user = User::factory()->create(); $azmayeshSample = AzmayeshSample::factory()->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayesh_samples.update', [$azmayeshSample->id])); + $response->assertForbidden(); + } + + public function test_azmayesh_id_attribute_is_required(): void + { + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); + $azmayeshSample = AzmayeshSample::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_samples.update', [$azmayeshSample->id])); + $response->assertUnprocessable() ->assertInvalid([ 'azmayesh_id' => __('validation.required', ['attribute' => 'آزمایش']) @@ -30,7 +45,11 @@ class UpdateTest extends TestCase public function test_azmayesh_id_attribute_should_exists_on_azmayeshes_table(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); $azmayeshSample = AzmayeshSample::factory()->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayesh_samples.update', [$azmayeshSample->id]), [ @@ -45,7 +64,11 @@ class UpdateTest extends TestCase public function test_data_attribute_is_required(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); $azmayeshSample = AzmayeshSample::factory()->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayesh_samples.update', [$azmayeshSample->id])); @@ -58,7 +81,11 @@ class UpdateTest extends TestCase public function test_user_can_update_the_azmayesh_sample(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-management' + ])) + ->create(); $azmayesh = Azmayesh::factory()->create(); $azmayeshSample = AzmayeshSample::factory()->create(); diff --git a/tests/Feature/V3/AzmayeshType/DeleteTest.php b/tests/Feature/V3/AzmayeshType/DeleteTest.php index 05b49035..aaaad978 100644 --- a/tests/Feature/V3/AzmayeshType/DeleteTest.php +++ b/tests/Feature/V3/AzmayeshType/DeleteTest.php @@ -4,6 +4,7 @@ namespace Tests\Feature\V3\AzmayeshType; use App\Models\AzmayeshField; use App\Models\AzmayeshType; +use App\Models\Permission; use App\Models\User; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithFaker; @@ -15,9 +16,23 @@ class DeleteTest extends TestCase /** * A basic feature test example. */ - public function test_user_can_delete_azmayesh_type(): void + public function test_user_should_have_azmayesh_type_management_permission(): void { $user = User::factory()->create(); + $azmayeshType = AzmayeshType::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.delete', [$azmayeshType->id])); + + $response->assertForbidden(); + } + + public function test_user_can_delete_azmayesh_type(): void + { + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-type-management' + ])) + ->create(); $azmayeshType = AzmayeshType::factory()->create(); $azmayeshField = AzmayeshField::factory()->create(); diff --git a/tests/Feature/V3/AzmayeshType/FieldsTest.php b/tests/Feature/V3/AzmayeshType/FieldsTest.php index 7e4832ff..51985190 100644 --- a/tests/Feature/V3/AzmayeshType/FieldsTest.php +++ b/tests/Feature/V3/AzmayeshType/FieldsTest.php @@ -4,6 +4,7 @@ namespace Tests\Feature\V3\AzmayeshType; use App\Models\AzmayeshField; use App\Models\AzmayeshType; +use App\Models\Permission; use App\Models\User; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithFaker; @@ -15,6 +16,16 @@ class FieldsTest extends TestCase /** * A basic feature test example. */ + public function test_user_should_have_azmayesh_type_management_permission(): void + { + $user = User::factory()->create(); + $azmayeshType = AzmayeshType::factory()->create(); + + $response = $this->actingAs($user)->getJson(route('v3.azmayesh_types.fields', [$azmayeshType->id])); + + $response->assertForbidden(); + } + public function test_all_fields_for_azmayesh_types_returned_successfully(): void { AzmayeshField::factory(3) @@ -47,7 +58,11 @@ class FieldsTest extends TestCase $azmayeshType->azmayeshFields()->attach([1, 2, 3]); - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-type-management' + ])) + ->create(); $response = $this->actingAs($user)->getJson(route('v3.azmayesh_types.fields', [$azmayeshType->id])); diff --git a/tests/Feature/V3/AzmayeshType/IndexTest.php b/tests/Feature/V3/AzmayeshType/IndexTest.php index d78ebe24..55e50b08 100644 --- a/tests/Feature/V3/AzmayeshType/IndexTest.php +++ b/tests/Feature/V3/AzmayeshType/IndexTest.php @@ -3,6 +3,7 @@ namespace Tests\Feature\V3\AzmayeshType; use App\Models\AzmayeshType; +use App\Models\Permission; use App\Models\User; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithFaker; @@ -14,11 +15,29 @@ class IndexTest extends TestCase /** * A basic feature test example. */ + public function test_user_should_have_azmayesh_type_management_permission(): void + { + $user = User::factory()->create(); + + $response = $this->actingAs($user)->getJson(route('v3.azmayesh_types.index', [ + 'start' => 0, + 'size' => 10, + 'filters' => json_encode([]), + 'sorting' => json_encode([]), + ])); + + $response->assertForbidden(); + } + public function test_all_azmayesh_types_returned_successfully(): void { AzmayeshType::factory(5)->create(); - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-type-management' + ])) + ->create(); $response = $this->actingAs($user)->getJson(route('v3.azmayesh_types.index', [ 'start' => 0, diff --git a/tests/Feature/V3/AzmayeshType/ListTest.php b/tests/Feature/V3/AzmayeshType/ListTest.php index 5aed890b..62caa7aa 100644 --- a/tests/Feature/V3/AzmayeshType/ListTest.php +++ b/tests/Feature/V3/AzmayeshType/ListTest.php @@ -3,6 +3,7 @@ namespace Tests\Feature\V3\AzmayeshType; use App\Models\AzmayeshType; +use App\Models\Permission; use App\Models\User; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithFaker; @@ -14,11 +15,24 @@ class ListTest extends TestCase /** * A basic feature test example. */ + public function test_user_should_have_azmayesh_type_management_permission(): void + { + $user = User::factory()->create(); + + $response = $this->actingAs($user)->getJson(route('v3.azmayesh_types.list')); + + $response->assertForbidden(); + } + public function test_azmayesh_type_list_returned_successfully(): void { AzmayeshType::factory(5)->create(); - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-type-management' + ])) + ->create(); $response = $this->actingAs($user)->getJson(route('v3.azmayesh_types.list')); diff --git a/tests/Feature/V3/AzmayeshType/ShowTest.php b/tests/Feature/V3/AzmayeshType/ShowTest.php index f682f9e8..318d4554 100644 --- a/tests/Feature/V3/AzmayeshType/ShowTest.php +++ b/tests/Feature/V3/AzmayeshType/ShowTest.php @@ -4,6 +4,7 @@ namespace Tests\Feature\V3\AzmayeshType; use App\Models\AzmayeshField; use App\Models\AzmayeshType; +use App\Models\Permission; use App\Models\User; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithFaker; @@ -15,6 +16,16 @@ class ShowTest extends TestCase /** * A basic feature test example. */ + public function test_user_should_have_azmayesh_type_management_permission(): void + { + $user = User::factory()->create(); + $azmayeshType = AzmayeshType::factory()->create(); + + $response = $this->actingAs($user)->getJson(route('v3.azmayesh_types.show', [$azmayeshType->id])); + + $response->assertForbidden(); + } + public function test_azmayesh_type_details_returned_successfully(): void { $azmayeshType = AzmayeshType::factory()->create(); @@ -22,7 +33,11 @@ class ShowTest extends TestCase $azmayeshType->azmayeshFields()->attach([$azmayeshField->id]); - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-type-management' + ])) + ->create(); $response = $this->actingAs($user)->getJson(route('v3.azmayesh_types.show', [$azmayeshType->id])); diff --git a/tests/Feature/V3/AzmayeshType/StoreTest.php b/tests/Feature/V3/AzmayeshType/StoreTest.php index 18cee337..12d3e54b 100644 --- a/tests/Feature/V3/AzmayeshType/StoreTest.php +++ b/tests/Feature/V3/AzmayeshType/StoreTest.php @@ -2,6 +2,7 @@ namespace Tests\Feature\V3\AzmayeshType; +use App\Models\Permission; use App\Models\User; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithFaker; @@ -13,12 +14,25 @@ class StoreTest extends TestCase /** * A basic feature test example. */ - public function test_name_attribute_is_required(): void + public function test_user_should_have_azmayesh_type_management_permission(): void { $user = User::factory()->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.store')); + $response->assertForbidden(); + } + + public function test_name_attribute_is_required(): void + { + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-type-management' + ])) + ->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.store')); + $response->assertUnprocessable() ->assertInvalid([ 'name' => __('validation.required', ['attribute' => "نام"]) @@ -27,7 +41,11 @@ class StoreTest extends TestCase public function test_name_attribute_should_be_string(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-type-management' + ])) + ->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.store'), [ 'name' => $this->faker->numberBetween(1, 10) @@ -41,7 +59,11 @@ class StoreTest extends TestCase public function test_fields_attribute_is_required(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-type-management' + ])) + ->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.store')); @@ -53,7 +75,11 @@ class StoreTest extends TestCase public function test_fields_attribute_should_be_array(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-type-management' + ])) + ->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.store'), [ 'fields' => $this->faker->numberBetween(1, 10) @@ -67,7 +93,11 @@ class StoreTest extends TestCase public function test_field_name_is_required(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-type-management' + ])) + ->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.store'), [ 'fields' => [ @@ -83,7 +113,11 @@ class StoreTest extends TestCase public function test_field_name_should_be_string(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-type-management' + ])) + ->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.store'), [ 'fields' => [ @@ -99,7 +133,11 @@ class StoreTest extends TestCase public function test_field_unit_should_be_string(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-type-management' + ])) + ->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.store'), [ 'fields' => [ @@ -115,7 +153,11 @@ class StoreTest extends TestCase public function test_field_type_is_required(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-type-management' + ])) + ->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.store'), [ 'fields' => [ @@ -131,7 +173,11 @@ class StoreTest extends TestCase public function test_field_type_should_be_string(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-type-management' + ])) + ->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.store'), [ 'fields' => [ @@ -147,7 +193,11 @@ class StoreTest extends TestCase public function test_field_option_should_be_string(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-type-management' + ])) + ->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.store'), [ 'fields' => [ @@ -163,7 +213,11 @@ class StoreTest extends TestCase public function test_user_can_store_fields_for_an_azmayesh_type(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-type-management' + ])) + ->create(); $data = [ 'name' => $this->faker->name, diff --git a/tests/Feature/V3/AzmayeshType/UpdateTest.php b/tests/Feature/V3/AzmayeshType/UpdateTest.php index 7cc6ba40..067beda9 100644 --- a/tests/Feature/V3/AzmayeshType/UpdateTest.php +++ b/tests/Feature/V3/AzmayeshType/UpdateTest.php @@ -2,7 +2,9 @@ namespace Tests\Feature\V3\AzmayeshType; +use App\Models\AzmayeshSample; use App\Models\AzmayeshType; +use App\Models\Permission; use App\Models\User; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithFaker; @@ -14,13 +16,27 @@ class UpdateTest extends TestCase /** * A basic feature test example. */ - public function test_name_attribute_is_required(): void + public function test_user_should_have_azmayesh_management_permission(): void { $user = User::factory()->create(); $azmayeshType = AzmayeshType::factory()->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.update', [$azmayeshType->id])); + $response->assertForbidden(); + } + + public function test_name_attribute_is_required(): void + { + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-type-management' + ])) + ->create(); + $azmayeshType = AzmayeshType::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.update', [$azmayeshType->id])); + $response->assertUnprocessable() ->assertInvalid([ 'name' => __('validation.required', ['attribute' => "نام"]) @@ -29,7 +45,11 @@ class UpdateTest extends TestCase public function test_name_attribute_should_be_string(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-type-management' + ])) + ->create(); $azmayeshType = AzmayeshType::factory()->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.update', [$azmayeshType->id]), [ @@ -44,7 +64,11 @@ class UpdateTest extends TestCase public function test_fields_attribute_is_required(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-type-management' + ])) + ->create(); $azmayeshType = AzmayeshType::factory()->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.update', [$azmayeshType->id])); @@ -57,7 +81,11 @@ class UpdateTest extends TestCase public function test_fields_attribute_should_be_array(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-type-management' + ])) + ->create(); $azmayeshType = AzmayeshType::factory()->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.update', [$azmayeshType->id]), [ @@ -72,7 +100,11 @@ class UpdateTest extends TestCase public function test_field_name_is_required(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-type-management' + ])) + ->create(); $azmayeshType = AzmayeshType::factory()->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.update', [$azmayeshType->id]), [ @@ -89,7 +121,11 @@ class UpdateTest extends TestCase public function test_field_name_should_be_string(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-type-management' + ])) + ->create(); $azmayeshType = AzmayeshType::factory()->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.update', [$azmayeshType->id]), [ @@ -106,7 +142,11 @@ class UpdateTest extends TestCase public function test_field_unit_should_be_string(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-type-management' + ])) + ->create(); $azmayeshType = AzmayeshType::factory()->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.update', [$azmayeshType->id]), [ @@ -123,7 +163,11 @@ class UpdateTest extends TestCase public function test_field_type_is_required(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-type-management' + ])) + ->create(); $azmayeshType = AzmayeshType::factory()->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.update', [$azmayeshType->id]), [ @@ -140,7 +184,11 @@ class UpdateTest extends TestCase public function test_field_type_should_be_string(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-type-management' + ])) + ->create(); $azmayeshType = AzmayeshType::factory()->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.update', [$azmayeshType->id]), [ @@ -157,7 +205,11 @@ class UpdateTest extends TestCase public function test_field_option_should_be_string(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-type-management' + ])) + ->create(); $azmayeshType = AzmayeshType::factory()->create(); $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.update', [$azmayeshType->id]), [ @@ -174,7 +226,11 @@ class UpdateTest extends TestCase public function test_user_can_update_fields_for_an_azmayesh_type(): void { - $user = User::factory()->create(); + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'azmayesh-type-management' + ])) + ->create(); $azmayeshType = AzmayeshType::factory()->create(); $data = [