write test for permission controller

This commit is contained in:
2025-03-11 11:47:43 +03:30
parent f394f9cd0c
commit 6ec1f6c2b4
9 changed files with 375 additions and 17 deletions

View File

@@ -33,16 +33,14 @@ class SendRoadObservationToNikarayanCommand extends Command
$this->info("Dispatching jobs with a delay between each...");
DB::transaction(function () use (&$count) {
$delay = 0;
RoadObserved::query()
->whereBetween('StartTime_DateTime_fa', ['1403-01-01 00:00:00', '1403-07-13 23:59:59'])
->chunkById(50, function ($roads) use (&$delay, &$count) {
->chunkById(50, function ($roads) use (&$count) {
foreach ($roads as $road) {
$road->update(['status' => 1]);
SendRoadObservationToNikarayan::dispatch($road)->delay(now()->addSeconds($delay));
Log::channel('road_observation_problem')->info("Job for Road Observed ID {$road->id} scheduled with a {$delay} seconds delay.");
$this->info("Scheduled job for Road ID: {$road->id} {$delay}s");
$delay += 3;
SendRoadObservationToNikarayan::dispatch($road);
Log::channel('road_observation_problem')->info("Job for Road Observed ID {$road->id} scheduled.");
$this->info("Scheduled job for Road ID: {$road->id}");
$count +=1;
}
});

View File

@@ -45,12 +45,14 @@ class SendRoadObservationToNikarayan implements ShouldQueue
);
$soapClient = new SoapClient($url);
$soapClient->UpdateInfo_RoadObservedProblems($array);
$result = $soapClient->UpdateInfo_RoadObservedProblems($array);
Log::channel('road_observation_problem')->info(json_encode($result), [$this->roadObserved->id]);
}
catch (\Exception $exception){
Log::channel('road_observation_problem')->error($exception->getMessage(), [
'roadObserved' => $this->roadObserved,
'roadObserved' => $this->roadObserved->id,
'line' => $exception->getLine(),
'file' => $exception->getFile(),
]);
}
}

View File

@@ -33,7 +33,7 @@ class NikarayanService
$soapClient = new SoapClient($url);
$result = $soapClient->UpdateInfo_RoadObservedProblems($array);
Log::channel('nikarayan')->info($result);
Log::channel('nikarayan')->info(json_encode($result));
return $result;
}

View File

@@ -21,7 +21,7 @@ return new class extends Migration
$table->decimal('end_lat', 12, 10)->nullable();
$table->decimal('end_lng', 12, 10)->nullable();
$table->string('project_distance')->nullable();
$table->unsignedTinyInteger('item')->index('item');
$table->unsignedTinyInteger('item');//->index('item');
$table->string('item_fa')->nullable();
$table->integer('sub_item')->nullable();
$table->string('sub_item_fa')->nullable();

View File

@@ -36,10 +36,10 @@ return new class extends Migration
$table->string('action_type', 100)->nullable();
$table->text('model')->nullable();
$table->timestamps();
$table->integer('mm')->nullable()->storedAs('month(`created_at`)');
$table->integer('yy')->nullable()->storedAs('year(`created_at`)');
$table->integer('dd')->nullable()->storedAs('dayofmonth(`created_at`)');
$table->integer('hh')->nullable()->storedAs('hour(`created_at`)');
$table->integer('mm')->nullable();//->storedAs('month(`created_at`)');
$table->integer('yy')->nullable();//->storedAs('year(`created_at`)');
$table->integer('dd')->nullable();//->storedAs('dayofmonth(`created_at`)');
$table->integer('hh')->nullable();//->storedAs('hour(`created_at`)');
});
}

View File

@@ -21,8 +21,8 @@
<env name="APP_ENV" value="testing"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="DB_CONNECTION" value="mysql"/>
<env name="DB_DATABASE" value="rms_test_db"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
<env name="MAIL_MAILER" value="array"/>
<env name="QUEUE_CONNECTION" value="sync"/>
<env name="SESSION_DRIVER" value="array"/>

View File

@@ -321,6 +321,7 @@ Route::prefix('road_observations')
Route::prefix('log_list')
->name('logList.')
->middleware('permission:manage-log-list')
->controller(LogListManagementController::class)
->group(function () {
Route::get('/', 'index')->name('index');
@@ -372,6 +373,7 @@ Route::name('items.')
Route::prefix('permissions')
->name('permissions.')
->middleware('permission:manage-permissions')
->controller(PermissionManagementController::class)
->group(function () {
Route::get('/', 'index')->name('index');

View File

@@ -0,0 +1,59 @@
<?php
namespace Tests\Feature\V3\Permission;
use App\Models\Permission;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class IndexTest extends TestCase
{
use RefreshDatabase, WithFaker;
/**
* A basic feature test example.
*/
public function test_user_should_have_manage_permission(): void
{
$user = User::factory()->create();
$response = $this->actingAs($user)->getJson(route('v3.permissions.index'));
$response->assertForbidden();
}
public function test_all_permissions_returned_successfully(): void
{
$user = User::factory()
->has(Permission::factory([
'name' => 'manage-permissions'
]))
->create();
$response = $this->actingAs($user)->getJson(route('v3.permissions.index', [
'start' => 0,
'size' => 10,
'filters' => json_encode([]),
'sorting' => json_encode([]),
]));
$response->assertOk();
$response->assertJsonStructure([
'data' => [
'*' => [
'id',
'name',
'name_fa',
'type',
'type_fa',
'description',
],
],
"meta" => [
"totalRowCount"
]
]);
}
}

View File

@@ -0,0 +1,297 @@
<?php
namespace Tests\Feature\V3\Permission;
use App\Models\Permission;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class StoreTest extends TestCase
{
use RefreshDatabase, WithFaker;
/**
* A basic feature test example.
*/
public function test_user_should_have_manage_permission(): void
{
$user = User::factory()->create();
$response = $this->actingAs($user)->postJson(route('v3.permissions.store'));
$response->assertForbidden();
}
public function test_name_attribute_is_required(): void
{
$user = User::factory()
->has(Permission::factory([
'name' => 'manage-permissions'
]))
->create();
$response = $this->actingAs($user)->postJson(route('v3.permissions.store'));
$response->assertUnprocessable()
->assertInvalid([
'name' => __('validation.required', ['attribute' => 'نام'])
]);
}
public function test_name_attribute_should_be_string(): void
{
$user = User::factory()
->has(Permission::factory([
'name' => 'manage-permissions'
]))
->create();
$response = $this->actingAs($user)->postJson(route('v3.permissions.store'), [
'name' => $this->faker->numberBetween(1, 10)
]);
$response->assertUnprocessable()
->assertInvalid([
'name' => __('validation.string', ['attribute' => 'نام'])
]);
}
public function test_name_attribute_should_be_unique(): void
{
$user = User::factory()
->has(Permission::factory([
'name' => 'manage-permissions'
]))
->create();
$permission = Permission::factory()->create([
'name' => $this->faker->name()
]);
$response = $this->actingAs($user)->postJson(route('v3.permissions.store'), [
'name' => $permission->name,
]);
$response->assertUnprocessable()
->assertInvalid([
'name' => __('validation.unique', ['attribute' => 'نام'])
]);
}
public function test_name_fa_attribute_is_required(): void
{
$user = User::factory()
->has(Permission::factory([
'name' => 'manage-permissions'
]))
->create();
$response = $this->actingAs($user)->postJson(route('v3.permissions.store'));
$response->assertUnprocessable()
->assertInvalid([
'name_fa' => __('validation.required', ['attribute' => 'name fa'])
]);
}
public function test_name_fa_attribute_should_be_string(): void
{
$user = User::factory()
->has(Permission::factory([
'name' => 'manage-permissions'
]))
->create();
$response = $this->actingAs($user)->postJson(route('v3.permissions.store'), [
'name_fa' => $this->faker->numberBetween(1, 10)
]);
$response->assertUnprocessable()
->assertInvalid([
'name_fa' => __('validation.string', ['attribute' => 'name fa'])
]);
}
public function test_description_attribute_should_be_string(): void
{
$user = User::factory()
->has(Permission::factory([
'name' => 'manage-permissions'
]))
->create();
$response = $this->actingAs($user)->postJson(route('v3.permissions.store'), [
'description' => $this->faker->numberBetween(1, 10)
]);
$response->assertUnprocessable()
->assertInvalid([
'description' => __('validation.string', ['attribute' => 'توضیحات'])
]);
}
public function test_type_attribute_is_required(): void
{
$user = User::factory()
->has(Permission::factory([
'name' => 'manage-permissions'
]))
->create();
$response = $this->actingAs($user)->postJson(route('v3.permissions.store'));
$response->assertUnprocessable()
->assertInvalid([
'type' => __('validation.required', ['attribute' => 'type'])
]);
}
public function test_type_attribute_should_be_string(): void
{
$user = User::factory()
->has(Permission::factory([
'name' => 'manage-permissions'
]))
->create();
$response = $this->actingAs($user)->postJson(route('v3.permissions.store'), [
'type' => $this->faker->numberBetween(1, 10)
]);
$response->assertUnprocessable()
->assertInvalid([
'type' => __('validation.string', ['attribute' => 'type'])
]);
}
public function test_type_fa_attribute_is_required(): void
{
$user = User::factory()
->has(Permission::factory([
'name' => 'manage-permissions'
]))
->create();
$response = $this->actingAs($user)->postJson(route('v3.permissions.store'));
$response->assertUnprocessable()
->assertInvalid([
'type_fa' => __('validation.required', ['attribute' => 'type fa'])
]);
}
public function test_type_fa_attribute_should_be_string(): void
{
$user = User::factory()
->has(Permission::factory([
'name' => 'manage-permissions'
]))
->create();
$response = $this->actingAs($user)->postJson(route('v3.permissions.store'), [
'type_fa' => $this->faker->numberBetween(1, 10)
]);
$response->assertUnprocessable()
->assertInvalid([
'type_fa' => __('validation.string', ['attribute' => 'type fa'])
]);
}
public function test_need_province_attribute_is_required(): void
{
$user = User::factory()
->has(Permission::factory([
'name' => 'manage-permissions'
]))
->create();
$response = $this->actingAs($user)->postJson(route('v3.permissions.store'));
$response->assertUnprocessable()
->assertInvalid([
'need_province' => __('validation.required', ['attribute' => 'need province'])
]);
}
public function test_need_province_attribute_should_be_1_or_0(): void
{
$user = User::factory()
->has(Permission::factory([
'name' => 'manage-permissions'
]))
->create();
$response = $this->actingAs($user)->postJson(route('v3.permissions.store'), [
'need_province' => $this->faker->numberBetween(3, 9),
]);
$response->assertUnprocessable()
->assertInvalid([
'need_province' => __('validation.in', ['attribute' => 'need province'])
]);
}
public function test_need_edare_shahri_attribute_is_required(): void
{
$user = User::factory()
->has(Permission::factory([
'name' => 'manage-permissions'
]))
->create();
$response = $this->actingAs($user)->postJson(route('v3.permissions.store'));
$response->assertUnprocessable()
->assertInvalid([
'need_edare_shahri' => __('validation.required', ['attribute' => 'need edare shahri'])
]);
}
public function test_need_edare_shahri_attribute_should_be_1_or_0(): void
{
$user = User::factory()
->has(Permission::factory([
'name' => 'manage-permissions'
]))
->create();
$response = $this->actingAs($user)->postJson(route('v3.permissions.store'), [
'need_edare_shahri' => $this->faker->numberBetween(3, 9),
]);
$response->assertUnprocessable()
->assertInvalid([
'need_edare_shahri' => __('validation.in', ['attribute' => 'need edare shahri'])
]);
}
public function test_user_can_create_a_permission()
{
$user = User::factory()
->has(Permission::factory([
'name' => 'manage-permissions'
]))
->create();
$inputData = [
'name' => $this->faker->name(),
'name_fa' => $this->faker->name(),
'description' => $this->faker->lexify(),
'type' => $this->faker->name(),
'type_fa' => $this->faker->name(),
'need_province' => $this->faker->numberBetween(0, 1),
'need_edare_shahri' => $this->faker->numberBetween(0, 1),
];
$response = $this->actingAs($user)->postJson(route('v3.permissions.store'), $inputData);
$response->assertOk()
->assertExactJson([
'message' => __('messages.successful'),
]);
$this->assertDatabaseHas('permissions', $inputData);
}
}