write new tests

This commit is contained in:
2025-05-12 17:05:42 +03:30
parent 70f3c36f58
commit db8cda55df
13 changed files with 507 additions and 18 deletions

View File

@@ -17,7 +17,9 @@ class RoleFactory extends Factory
public function definition(): array public function definition(): array
{ {
return [ return [
// 'name' => $this->faker->unique()->slug,
'name_fa' => $this->faker->unique()->word,
'guard_name' => 'web',
]; ];
} }
} }

View File

@@ -79,13 +79,14 @@ Route::prefix('server')->group(function () {
Route::middleware('auth') Route::middleware('auth')
->prefix('roles') ->prefix('roles')
->name('roles.')
->controller(RoleManagementController::class) ->controller(RoleManagementController::class)
->group(function () { ->group(function () {
Route::get('/', 'index')->name('index'); Route::get('/', 'index')->name('index');
Route::get('list', 'list')->name('list'); Route::get('list', 'list')->name('list');
Route::post('/', 'store')->name('store'); Route::post('/', 'store')->name('store');
Route::get('/{roll}', 'show')->name('show'); Route::get('/{role}', 'show')->name('show');
Route::post('/{roll}', 'update')->name('update'); Route::post('/{role}', 'update')->name('update');
Route::delete('/{roll}', 'destroy')->name('destroy'); Route::delete('/{role}', 'destroy')->name('destroy');
}); });
}); });

View File

@@ -76,11 +76,10 @@ class StoreTest extends TestCase
public function test_can_create_a_call(): void public function test_can_create_a_call(): void
{ {
$operator = User::factory()->create(); $operator = User::factory()->create([
$operator->telephone_id = 'tel-' . $this->faker->unique()->numerify('###'); 'telephone_id' => $this->faker->unique()->numerify('###'),
$operator->save(); ]);
DB::table('events')->truncate();
$this->artisan('db:seed --class=EventSeeder'); $this->artisan('db:seed --class=EventSeeder');
$this->mock(NotificationService::class, function (MockInterface $mock) { $this->mock(NotificationService::class, function (MockInterface $mock) {

View File

@@ -7,6 +7,7 @@ use App\Models\Call;
use App\Models\Category; use App\Models\Category;
use App\Models\Permission; use App\Models\Permission;
use App\Models\User; use App\Models\User;
use Database\Seeders\EventSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker; use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase; use Tests\TestCase;
@@ -220,8 +221,11 @@ class UpdateTest extends TestCase
'telephone_id' => $telephoneId, 'telephone_id' => $telephoneId,
]); ]);
$this->seed(EventSeeder::class);
$category = Category::factory()->create(); $category = Category::factory()->create();
$description = $this->faker->sentence; $description = $this->faker->sentence;
$response = $this->actingAs($user)->postJson(route('calls.update', $call->id), [ $response = $this->actingAs($user)->postJson(route('calls.update', $call->id), [
'category_id' => $category->category_id, 'category_id' => $category->category_id,
'subcategory_id' => $category->subcategory_id, 'subcategory_id' => $category->subcategory_id,

View File

@@ -14,7 +14,7 @@ class IndexTest extends TestCase
public function test_unauthenticated_user_cannot_access_permission_index(): void public function test_unauthenticated_user_cannot_access_permission_index(): void
{ {
$response = $this->getJson(route('permissions.list')); $response = $this->getJson(route('permissions.index'));
$response->assertStatus(401); $response->assertStatus(401);
} }

View File

@@ -14,11 +14,15 @@ class UpdateTest extends TestCase
public function test_user_must_be_authenticated(): void public function test_user_must_be_authenticated(): void
{ {
$response = $this->postJson(route('permissions.store'), []); $permission = Permission::factory()->create();
$response = $this->postJson(route('permissions.update', $permission));
$response->assertUnauthorized(); $response->assertUnauthorized();
} }
public function test_name_and_name_fa_fields_are_required(): void public function test_name_and_name_fa_fields_are_required(): void
{ {
$user = User::factory()->create(); $user = User::factory()->create();

View File

@@ -0,0 +1,65 @@
<?php
namespace Tests\Feature\Role;
use App\Models\Permission;
use App\Models\Role;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class DeleteTest extends TestCase
{
use RefreshDatabase, WithFaker;
public function test_guest_cannot_view_role(): void
{
$role = Role::factory()->create();
$response = $this->postJson(route('roles.destroy', [$role->id]));
$response->assertUnauthorized();
}
public function test_a_role_can_be_deleted()
{
$user = User::factory()->create();
$role = Role::factory()->create();
$permission = Permission::factory([
'id' => 2,
'name' => $this->faker->name
])->create();
$role->syncPermissions($permission);
$this->assertDatabaseHas('roles', [
'id' => $role->id,
'name' => $role->name,
'name_fa' => $role->name_fa,
]);
$this->assertDatabaseHas('role_has_permissions', [
'permission_id' => $permission->id,
'role_id' => $role->id
]);
$response = $this->actingAs($user)->deleteJson(route('roles.destroy', [$role->id]));
$response->assertOk()
->assertExactJson([
'message' => __('messages.successful')
]);
$this->assertDatabaseMissing('roles', [
'id' => $role->id,
'name' => $role->name,
'name_fa' => $role->name_fa,
]);
$this->assertDatabaseMissing('role_has_permissions', [
'permission_id' => $permission->id,
'role_id' => $role->id
]);
}
}

View File

@@ -0,0 +1,47 @@
<?php
namespace Tests\Feature\Role;
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;
public function test_the_user_is_not_authenticated(): void
{
$response = $this->getJson(route('roles.index'));
$response->assertStatus(401);
}
public function test_authenticated_user_can_see_roles_index(): void
{
$user = User::factory()->create();
$response = $this->actingAs($user)->getJson(route('roles.index',[
'filters' => json_encode([]),
'sorting' => json_encode([]),
]));
$response->assertOk();
$response->assertJsonStructure([
'data' => [
'*' => [
'id',
'name',
'name_fa',
],
],
'meta' => [
'totalRowCount'
]
]);
}
}

View File

@@ -0,0 +1,41 @@
<?php
namespace Tests\Feature\Role;
use App\Models\Permission;
use App\Models\Role;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class ListTest extends TestCase
{
use RefreshDatabase, WithFaker;
public function test_unauthenticated_user_cannot_access(): void
{
$response = $this->getJson(route('roles.list'));
$response->assertUnauthorized();
}
public function test_roles_list_returns_correct_structure(): void
{
$user = User::factory()->create();
Role::factory()->count(3)->create();
$response = $this->actingAs($user)->getJson(route('roles.list'));
$response->assertOk()
->assertJsonStructure([
'data' => [
'*' => [
'id',
'name',
'name_fa',
]
]
]);
}
}

View File

@@ -0,0 +1,51 @@
<?php
namespace Tests\Feature\Role;
use App\Models\Permission;
use App\Models\Role;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class ShowTest extends TestCase
{
use RefreshDatabase, WithFaker;
public function test_guest_cannot_view_role(): void
{
$role = Role::factory()->create();
$response = $this->postJson(route('roles.show',[$role->id]));
$response->assertUnauthorized();
}
public function test_a_role_can_be_returned_properly():void
{
$user = User::factory()->create();
$role = Role::factory()->create();
$permission = Permission::factory()->create([
'id' => 2,
'name' => $this->faker->name,
'name_fa' => $this->faker->name
]);
$role->syncPermissions($permission);
$role = Role::factory()->create();
$response = $this->actingAs($user)->getJson(route('roles.show', [$role->id]));
$response->assertOk()
->assertJson([
'data' => [
'id' => $role->id,
'name' => $role->name,
'name_fa' => $role->name_fa,
'permissions' => $role->permissions->pluck('name')->toArray(),
]
]);
}
}

View File

@@ -0,0 +1,125 @@
<?php
namespace Tests\Feature\Role;
use App\Models\Permission;
use App\Models\Role;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class StoreTest extends TestCase
{
use RefreshDatabase, WithFaker;
public function test_the_user_is_not_authenticated()
{
$response = $this->postJson(route('roles.store'));
$response->assertUnauthorized();
}
public function test_name_and_name_fa_fields_are_required(): void
{
$user = User::factory()->create();
$response = $this->actingAs($user)->postJson(route('roles.store'));
$response->assertUnprocessable()
->assertInvalid([
'name' => __('validation.required', ['attribute' => 'نام']),
'name_fa' => __('validation.required', ['attribute' => 'نام فارسی']),
]);
}
public function test_name_and_name_fa_fields_must_be_unique(): void
{
$user = User::factory()->create();
$duplicateName = $this->faker->unique()->slug;
$duplicateNameFa = $this->faker->unique()->word;
Role::create([
'name' => $duplicateName,
'name_fa' => $duplicateNameFa,
'guard_name' => 'web',
]);
$response = $this->actingAs($user)->postJson(route('roles.store'), [
'name' => $duplicateName,
'name_fa' => $duplicateNameFa,
]);
$response->assertUnprocessable()
->assertInvalid([
'name' => __('validation.unique', ['attribute' => 'نام']),
'name_fa' => __('validation.unique', ['attribute' => 'نام فارسی']),
]);
}
public function test_name_and_name_fa_must_be_strings(): void
{
$user = User::factory()->create();
$response = $this->actingAs($user)->postJson(route('roles.store'), [
'name' =>$this->faker->randomDigit(),
'name_fa' => $this->faker->randomDigit(),
]);
$response->assertUnprocessable()
->assertInvalid([
'name' => __('validation.string', ['attribute' => 'نام']),
'name_fa' => __('validation.string', ['attribute' => 'نام فارسی']),
]);
}
public function test_name_and_name_fa_must_not_exceed_max_length(): void
{
$user = User::factory()->create();
$longString = $this->faker->regexify('[a-zA-Z0-9]{256}');
$response = $this->actingAs($user)->postJson(route('roles.store'), [
'name' => $longString,
'name_fa' => $longString,
]);
$response->assertUnprocessable()
->assertInvalid([
'name' => __('validation.max.string', ['attribute' => 'نام', 'max' => 255]),
'name_fa' => __('validation.max.string', ['attribute' => 'نام فارسی', 'max' => 255]),
]);
}
public function test_valid_role_is_stored_successfully(): void
{
$user = User::factory()->create();
$permission = Permission::factory()->create();
$payload = [
'name' => $this->faker->unique()->slug,
'name_fa' => $this->faker->unique()->words(2, true),
'permissions' => [$permission->id],
];
$response = $this->actingAs($user)->postJson(route('roles.store'), $payload);
$response->assertOk()
->assertExactJson([
'message' => __('messages.successful')
]);
$this->assertDatabaseHas('roles', [
'name' => $payload['name'],
'name_fa' => $payload['name_fa'],
]);
$this->assertDatabaseHas('role_has_permissions', [
'permission_id' => $permission->id,
]);
}
}

View File

@@ -0,0 +1,157 @@
<?php
namespace Tests\Feature\Role;
use App\Models\Permission;
use App\Models\Role;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class UpdateTest extends TestCase
{
use RefreshDatabase, WithFaker;
public function test_the_user_is_not_authenticated()
{
$role = Role::factory()->create();
$response = $this->postJson(route('roles.update',$role));
$response->assertUnauthorized();
}
public function test_fields_are_required(): void
{
$user = User::factory()->create();
$response = $this->actingAs($user)->postJson(route('roles.store'));
$response->assertUnprocessable()
->assertInvalid([
'name' => __('validation.required', ['attribute' => 'نام']),
'name_fa' => __('validation.required', ['attribute' => 'نام فارسی']),
'permissions' => __('validation.required', ['attribute' => 'دسترسی ها']),
]);
}
public function test_fields_must_be_unique(): void
{
$user = User::factory()->create();
$duplicateName = $this->faker->unique()->slug;
$duplicateNameFa = $this->faker->unique()->word;
Role::create([
'name' => $duplicateName,
'name_fa' => $duplicateNameFa,
'guard_name' => 'web',
]);
$response = $this->actingAs($user)->postJson(route('roles.store'), [
'name' => $duplicateName,
'name_fa' => $duplicateNameFa,
]);
$response->assertUnprocessable()
->assertInvalid([
'name' => __('validation.unique', ['attribute' => 'نام']),
'name_fa' => __('validation.unique', ['attribute' => 'نام فارسی']),
]);
}
public function test_name_and_name_fa_must_be_strings(): void
{
$user = User::factory()->create();
$permission = Permission::factory()->create();
$response = $this->actingAs($user)->postJson(route('roles.store'), [
'name' => 123,
'name_fa' => 456,
'permissions' => [$permission->id],
]);
$response->assertUnprocessable()
->assertInvalid([
'name' => __('validation.string', ['attribute' => 'نام']),
'name_fa' => __('validation.string', ['attribute' => 'نام فارسی']),
]);
}
public function test_name_and_name_fa_must_not_exceed_max_length(): void
{
$user = User::factory()->create();
$longString = str_repeat('a', 256);
$permission = Permission::factory()->create();
$response = $this->actingAs($user)->postJson(route('roles.store'), [
'name' => $longString,
'name_fa' => $longString,
'permissions' => [$permission->id],
]);
$response->assertUnprocessable()
->assertInvalid([
'name' => __('validation.max.string', ['attribute' => 'نام', 'max' => 255]),
'name_fa' => __('validation.max.string', ['attribute' => 'نام فارسی', 'max' => 255]),
]);
}
public function test_permissions_must_be_array(): void
{
$user = User::factory()->create();
$response = $this->actingAs($user)->postJson(route('roles.store'), [
'name' => $this->faker->slug,
'name_fa' => $this->faker->word,
'permissions' => 1,
]);
$response->assertUnprocessable()
->assertInvalid([
'permissions' => __('validation.array', ['attribute' => 'دسترسی ها']),
]);
}
public function test_permissions_values_must_exist(): void
{
$user = User::factory()->create();
$role = Role::factory()->create();
$response = $this->actingAs($user)->postJson(route('roles.update', [$role->id]), [
'permissions' => $this->faker->name(),
]);
$response->assertUnprocessable()
->assertInvalid([
'permissions' => __('validation.exists', ['attribute' => 'دسترسی ها']),
]);
}
public function test_valid_role_is_update_successfully(): void
{
$user = User::factory()->create();
$permission = Permission::factory()->create();
$payload = [
'name' => $this->faker->unique()->slug,
'name_fa' => $this->faker->unique()->words(2, true),
'permissions' => [$permission->id],
];
$response = $this->actingAs($user)->postJson(route('roles.store'), $payload);
$response->assertOk()
->assertExactJson([
'message' => __('messages.successful'),
]);
$this->assertDatabaseHas('roles', [
'name' => $payload['name'],
'name_fa' => $payload['name_fa'],
]);
$this->assertDatabaseHas('role_has_permissions', [
'permission_id' => $permission->id,
]);
}
}

View File

@@ -7,12 +7,5 @@ use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase abstract class TestCase extends BaseTestCase
{ {
//
protected function setUp(): void
{
parent::setUp();
app()->setLocale('fa');
config(['logging.default' => 'null']);
$this->seed(EventSeeder::class);
}
} }