diff --git a/database/factories/RoleFactory.php b/database/factories/RoleFactory.php index b825646..5dbe458 100644 --- a/database/factories/RoleFactory.php +++ b/database/factories/RoleFactory.php @@ -17,7 +17,9 @@ class RoleFactory extends Factory public function definition(): array { return [ - // + 'name' => $this->faker->unique()->slug, + 'name_fa' => $this->faker->unique()->word, + 'guard_name' => 'web', ]; } } diff --git a/routes/web.php b/routes/web.php index 5da8281..7784c91 100644 --- a/routes/web.php +++ b/routes/web.php @@ -79,13 +79,14 @@ Route::prefix('server')->group(function () { Route::middleware('auth') ->prefix('roles') + ->name('roles.') ->controller(RoleManagementController::class) ->group(function () { Route::get('/', 'index')->name('index'); Route::get('list', 'list')->name('list'); Route::post('/', 'store')->name('store'); - Route::get('/{roll}', 'show')->name('show'); - Route::post('/{roll}', 'update')->name('update'); - Route::delete('/{roll}', 'destroy')->name('destroy'); + Route::get('/{role}', 'show')->name('show'); + Route::post('/{role}', 'update')->name('update'); + Route::delete('/{role}', 'destroy')->name('destroy'); }); }); diff --git a/tests/Feature/Call/StoreTest.php b/tests/Feature/Call/StoreTest.php index c4b6466..71018f3 100644 --- a/tests/Feature/Call/StoreTest.php +++ b/tests/Feature/Call/StoreTest.php @@ -76,11 +76,10 @@ class StoreTest extends TestCase public function test_can_create_a_call(): void { - $operator = User::factory()->create(); - $operator->telephone_id = 'tel-' . $this->faker->unique()->numerify('###'); - $operator->save(); + $operator = User::factory()->create([ + 'telephone_id' => $this->faker->unique()->numerify('###'), + ]); - DB::table('events')->truncate(); $this->artisan('db:seed --class=EventSeeder'); $this->mock(NotificationService::class, function (MockInterface $mock) { diff --git a/tests/Feature/Call/UpdateTest.php b/tests/Feature/Call/UpdateTest.php index f399a7e..a60db06 100644 --- a/tests/Feature/Call/UpdateTest.php +++ b/tests/Feature/Call/UpdateTest.php @@ -7,6 +7,7 @@ use App\Models\Call; use App\Models\Category; use App\Models\Permission; use App\Models\User; +use Database\Seeders\EventSeeder; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithFaker; use Tests\TestCase; @@ -220,8 +221,11 @@ class UpdateTest extends TestCase 'telephone_id' => $telephoneId, ]); + $this->seed(EventSeeder::class); + $category = Category::factory()->create(); $description = $this->faker->sentence; + $response = $this->actingAs($user)->postJson(route('calls.update', $call->id), [ 'category_id' => $category->category_id, 'subcategory_id' => $category->subcategory_id, diff --git a/tests/Feature/Permission/IndexTest.php b/tests/Feature/Permission/IndexTest.php index be3ebd4..ea798bd 100644 --- a/tests/Feature/Permission/IndexTest.php +++ b/tests/Feature/Permission/IndexTest.php @@ -14,7 +14,7 @@ class IndexTest extends TestCase 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); } diff --git a/tests/Feature/Permission/UpdateTest.php b/tests/Feature/Permission/UpdateTest.php index 855832e..b9f8653 100644 --- a/tests/Feature/Permission/UpdateTest.php +++ b/tests/Feature/Permission/UpdateTest.php @@ -14,11 +14,15 @@ class UpdateTest extends TestCase 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(); } + public function test_name_and_name_fa_fields_are_required(): void { $user = User::factory()->create(); diff --git a/tests/Feature/Role/DeleteTest.php b/tests/Feature/Role/DeleteTest.php new file mode 100644 index 0000000..ddaf5aa --- /dev/null +++ b/tests/Feature/Role/DeleteTest.php @@ -0,0 +1,65 @@ +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 + ]); + } +} diff --git a/tests/Feature/Role/IndexTest.php b/tests/Feature/Role/IndexTest.php new file mode 100644 index 0000000..95de2e6 --- /dev/null +++ b/tests/Feature/Role/IndexTest.php @@ -0,0 +1,47 @@ +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' + ] + ]); + } + +} diff --git a/tests/Feature/Role/ListTest.php b/tests/Feature/Role/ListTest.php new file mode 100644 index 0000000..d38d271 --- /dev/null +++ b/tests/Feature/Role/ListTest.php @@ -0,0 +1,41 @@ +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', + ] + ] + ]); + } +} diff --git a/tests/Feature/Role/ShowTest.php b/tests/Feature/Role/ShowTest.php new file mode 100644 index 0000000..fbffde3 --- /dev/null +++ b/tests/Feature/Role/ShowTest.php @@ -0,0 +1,51 @@ +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(), + ] + ]); + } +} diff --git a/tests/Feature/Role/StoreTest.php b/tests/Feature/Role/StoreTest.php new file mode 100644 index 0000000..806a175 --- /dev/null +++ b/tests/Feature/Role/StoreTest.php @@ -0,0 +1,125 @@ +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, + ]); + } + +} diff --git a/tests/Feature/Role/UpdateTest.php b/tests/Feature/Role/UpdateTest.php new file mode 100644 index 0000000..21c01a7 --- /dev/null +++ b/tests/Feature/Role/UpdateTest.php @@ -0,0 +1,157 @@ +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, + ]); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index 1b5fd39..8f98a4a 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -7,12 +7,5 @@ use Illuminate\Foundation\Testing\TestCase as BaseTestCase; abstract class TestCase extends BaseTestCase { - - protected function setUp(): void - { - parent::setUp(); - app()->setLocale('fa'); - config(['logging.default' => 'null']); - $this->seed(EventSeeder::class); - } + // }