126 lines
3.8 KiB
PHP
126 lines
3.8 KiB
PHP
<?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_unauthenticated_user_cannot_access()
|
|
{
|
|
$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,
|
|
]);
|
|
}
|
|
|
|
}
|