add role permission logic

This commit is contained in:
root
2026-03-10 17:11:48 -04:00
parent abebe0d9c0
commit c235fd2170
40 changed files with 1896 additions and 838 deletions
@@ -0,0 +1,247 @@
<?php
namespace Tests\Feature\Api\V1\Roles;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class RolePermissionControllerTest extends TestCase
{
use RefreshDatabase;
public function test_roles_index_returns_roles(): void
{
$user = $this->seedUser();
$this->seedRole();
Sanctum::actingAs($user);
$response = $this->getJson('/api/v1/role-permissions/roles');
$response->assertOk();
$response->assertJsonPath('status', true);
$this->assertNotEmpty($response->json('data.roles'));
}
public function test_store_role_creates_role(): void
{
$user = $this->seedUser();
Sanctum::actingAs($user);
$response = $this->postJson('/api/v1/role-permissions/roles', [
'name' => 'coach',
'slug' => 'coach',
'description' => 'Coach role',
'dashboard_route' => 'coach/dashboard',
'priority' => 5,
'is_active' => 1,
]);
$response->assertStatus(201);
$this->assertDatabaseHas('roles', ['name' => 'coach']);
}
public function test_update_role_updates_role(): void
{
$user = $this->seedUser();
$roleId = $this->seedRole();
Sanctum::actingAs($user);
$response = $this->patchJson('/api/v1/role-permissions/roles/' . $roleId, [
'description' => 'updated',
]);
$response->assertOk();
$this->assertDatabaseHas('roles', ['id' => $roleId, 'description' => 'updated']);
}
public function test_delete_role_removes_role(): void
{
$user = $this->seedUser();
$roleId = $this->seedRole();
Sanctum::actingAs($user);
$response = $this->deleteJson('/api/v1/role-permissions/roles/' . $roleId);
$response->assertOk();
$this->assertDatabaseMissing('roles', ['id' => $roleId]);
}
public function test_users_index_returns_users_with_roles(): void
{
$user = $this->seedUser();
$roleId = $this->seedRole();
DB::table('user_roles')->insert([
'user_id' => $user->id,
'role_id' => $roleId,
]);
Sanctum::actingAs($user);
$response = $this->getJson('/api/v1/role-permissions/users');
$response->assertOk();
$response->assertJsonPath('status', true);
$this->assertNotEmpty($response->json('data.users'));
}
public function test_assign_roles_updates_user_roles(): void
{
$user = $this->seedUser();
$roleId = $this->seedRole();
Sanctum::actingAs($user);
$response = $this->postJson('/api/v1/role-permissions/users/' . $user->id . '/roles', [
'role_ids' => [$roleId],
]);
$response->assertOk();
$this->assertDatabaseHas('user_roles', [
'user_id' => $user->id,
'role_id' => $roleId,
]);
$this->assertDatabaseHas('staff', [
'user_id' => $user->id,
]);
}
public function test_permissions_crud(): void
{
$user = $this->seedUser();
Sanctum::actingAs($user);
$create = $this->postJson('/api/v1/role-permissions/permissions', [
'name' => 'manage_settings',
'description' => 'Manage settings',
]);
$create->assertStatus(201);
$permissionId = (int) $create->json('data.permission.id');
$show = $this->getJson('/api/v1/role-permissions/permissions/' . $permissionId);
$show->assertOk();
$update = $this->patchJson('/api/v1/role-permissions/permissions/' . $permissionId, [
'description' => 'Updated',
]);
$update->assertOk();
$delete = $this->deleteJson('/api/v1/role-permissions/permissions/' . $permissionId);
$delete->assertOk();
$this->assertDatabaseMissing('permissions', ['id' => $permissionId]);
}
public function test_role_permissions_update(): void
{
$user = $this->seedUser();
$roleId = $this->seedRole();
$permissionId = $this->seedPermission();
Sanctum::actingAs($user);
$response = $this->putJson('/api/v1/role-permissions/roles/' . $roleId . '/permissions', [
'permissions' => [
$permissionId => ['create' => true, 'read' => true],
],
]);
$response->assertOk();
$this->assertDatabaseHas('role_permissions', [
'role_id' => $roleId,
'permission_id' => $permissionId,
'can_create' => 1,
'can_read' => 1,
]);
}
public function test_validation_rejects_invalid_role_payload(): void
{
$user = $this->seedUser();
Sanctum::actingAs($user);
$response = $this->postJson('/api/v1/role-permissions/roles', [
'name' => 'a',
]);
$response->assertStatus(422);
}
public function test_store_role_returns_error_on_service_exception(): void
{
$user = $this->seedUser();
Sanctum::actingAs($user);
$this->app->bind(\App\Services\Roles\RoleCrudService::class, function () {
return new class {
public function create(array $payload)
{
throw new \RuntimeException('fail');
}
};
});
$response = $this->postJson('/api/v1/role-permissions/roles', [
'name' => 'admin',
'slug' => 'admin',
'description' => 'Admin',
'dashboard_route' => 'administrator/administratordashboard',
'priority' => 1,
'is_active' => 1,
]);
$response->assertStatus(500);
$response->assertJsonPath('status', false);
}
public function test_requires_authentication(): void
{
$response = $this->getJson('/api/v1/role-permissions/roles');
$response->assertStatus(401);
}
private function seedUser(): User
{
$userId = DB::table('users')->insertGetId([
'firstname' => 'Admin',
'lastname' => 'User',
'cellphone' => '9999999999',
'email' => 'roles@example.com',
'address_street' => '123 Street',
'city' => 'City',
'state' => 'ST',
'zip' => '12345',
'accept_school_policy' => 1,
'password' => bcrypt('password'),
'user_type' => 'primary',
'semester' => 'Fall',
'school_year' => '2025-2026',
'status' => 'Active',
]);
return User::query()->findOrFail($userId);
}
private function seedRole(): int
{
return DB::table('roles')->insertGetId([
'name' => 'admin',
'slug' => 'admin',
'description' => 'Admin',
'dashboard_route' => 'administrator/administratordashboard',
'priority' => 1,
'is_active' => 1,
'created_at' => now(),
'updated_at' => now(),
]);
}
private function seedPermission(): int
{
return DB::table('permissions')->insertGetId([
'name' => 'manage_roles',
'description' => 'Manage roles',
'created_at' => now(),
'updated_at' => now(),
]);
}
}
@@ -0,0 +1,90 @@
<?php
namespace Tests\Feature\Api\V1\Roles;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class RoleSwitcherControllerTest extends TestCase
{
use RefreshDatabase;
public function test_index_returns_roles(): void
{
$user = $this->seedUser();
$roleId = $this->seedRole();
DB::table('user_roles')->insert([
'user_id' => $user->id,
'role_id' => $roleId,
]);
Sanctum::actingAs($user);
$response = $this->getJson('/api/v1/role-switcher');
$response->assertOk();
$response->assertJsonPath('status', true);
$this->assertNotEmpty($response->json('data.roles'));
}
public function test_switch_sets_role(): void
{
$user = $this->seedUser();
$this->seedRole();
Sanctum::actingAs($user);
$response = $this->postJson('/api/v1/role-switcher/switch', [
'role' => 'admin',
]);
$response->assertOk();
$response->assertJsonPath('status', true);
$response->assertJsonPath('data.role', 'admin');
}
public function test_requires_authentication(): void
{
$response = $this->getJson('/api/v1/role-switcher');
$response->assertStatus(401);
}
private function seedUser(): User
{
$userId = DB::table('users')->insertGetId([
'firstname' => 'Admin',
'lastname' => 'User',
'cellphone' => '9999999999',
'email' => 'switch@example.com',
'address_street' => '123 Street',
'city' => 'City',
'state' => 'ST',
'zip' => '12345',
'accept_school_policy' => 1,
'password' => bcrypt('password'),
'user_type' => 'primary',
'semester' => 'Fall',
'school_year' => '2025-2026',
'status' => 'Active',
]);
return User::query()->findOrFail($userId);
}
private function seedRole(): int
{
return DB::table('roles')->insertGetId([
'name' => 'admin',
'slug' => 'admin',
'description' => 'Admin',
'dashboard_route' => 'administrator/administratordashboard',
'priority' => 1,
'is_active' => 1,
'created_at' => now(),
'updated_at' => now(),
]);
}
}
@@ -0,0 +1,24 @@
<?php
namespace Tests\Unit\Resources\Roles;
use App\Http\Resources\Roles\PermissionResource;
use Tests\TestCase;
class PermissionResourceTest extends TestCase
{
public function test_resource_shape(): void
{
$resource = new PermissionResource([
'id' => 1,
'name' => 'manage_roles',
'description' => 'Manage roles',
]);
$payload = $resource->toArray(request());
$this->assertArrayHasKey('id', $payload);
$this->assertArrayHasKey('name', $payload);
$this->assertArrayHasKey('description', $payload);
}
}
@@ -0,0 +1,25 @@
<?php
namespace Tests\Unit\Resources\Roles;
use App\Http\Resources\Roles\RolePermissionResource;
use Tests\TestCase;
class RolePermissionResourceTest extends TestCase
{
public function test_resource_shape(): void
{
$resource = new RolePermissionResource([
'id' => 1,
'name' => 'manage_roles',
'can_create' => 1,
'can_read' => 0,
]);
$payload = $resource->toArray(request());
$this->assertArrayHasKey('permission_id', $payload);
$this->assertArrayHasKey('can_create', $payload);
$this->assertArrayHasKey('can_read', $payload);
}
}
@@ -0,0 +1,29 @@
<?php
namespace Tests\Unit\Resources\Roles;
use App\Http\Resources\Roles\RoleResource;
use Tests\TestCase;
class RoleResourceTest extends TestCase
{
public function test_resource_shape(): void
{
$resource = new RoleResource([
'id' => 1,
'name' => 'admin',
'slug' => 'admin',
'description' => 'Admin',
'dashboard_route' => 'administrator/administratordashboard',
'priority' => 1,
'is_active' => 1,
]);
$payload = $resource->toArray(request());
$this->assertArrayHasKey('id', $payload);
$this->assertArrayHasKey('name', $payload);
$this->assertArrayHasKey('slug', $payload);
$this->assertArrayHasKey('dashboard_route', $payload);
}
}
@@ -0,0 +1,26 @@
<?php
namespace Tests\Unit\Resources\Roles;
use App\Http\Resources\Roles\UserWithRolesResource;
use Tests\TestCase;
class UserWithRolesResourceTest extends TestCase
{
public function test_resource_shape(): void
{
$resource = new UserWithRolesResource([
'id' => 1,
'firstname' => 'Admin',
'lastname' => 'User',
'email' => 'admin@example.com',
'account_id' => 'A1',
'roles' => 'admin,teacher',
]);
$payload = $resource->toArray(request());
$this->assertArrayHasKey('roles', $payload);
$this->assertCount(2, $payload['roles']);
}
}
@@ -0,0 +1,28 @@
<?php
namespace Tests\Unit\Services\Roles;
use App\Models\Permission;
use App\Services\Roles\PermissionCrudService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class PermissionCrudServiceTest extends TestCase
{
use RefreshDatabase;
public function test_create_and_update_permission(): void
{
$service = new PermissionCrudService();
$permission = $service->create([
'name' => 'manage_roles',
'description' => 'Manage roles',
]);
$this->assertInstanceOf(Permission::class, $permission);
$this->assertDatabaseHas('permissions', ['name' => 'manage_roles']);
$service->update($permission, ['description' => 'Updated']);
$this->assertDatabaseHas('permissions', ['id' => $permission->id, 'description' => 'Updated']);
}
}
@@ -0,0 +1,57 @@
<?php
namespace Tests\Unit\Services\Roles;
use App\Models\User;
use App\Services\Roles\RoleAssignmentService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
class RoleAssignmentServiceTest extends TestCase
{
use RefreshDatabase;
public function test_assign_roles_creates_user_roles_and_staff(): void
{
$userId = DB::table('users')->insertGetId([
'firstname' => 'Admin',
'lastname' => 'User',
'cellphone' => '9999999999',
'email' => 'assign@example.com',
'address_street' => '123 Street',
'city' => 'City',
'state' => 'ST',
'zip' => '12345',
'accept_school_policy' => 1,
'password' => bcrypt('password'),
'user_type' => 'primary',
'semester' => 'Fall',
'school_year' => '2025-2026',
'status' => 'Active',
]);
$roleId = DB::table('roles')->insertGetId([
'name' => 'admin',
'slug' => 'admin',
'description' => 'Admin',
'dashboard_route' => 'administrator/administratordashboard',
'priority' => 1,
'is_active' => 1,
'created_at' => now(),
'updated_at' => now(),
]);
$service = new RoleAssignmentService(new RoleStaffService());
$result = $service->assignRoles($userId, [$roleId], $userId);
$this->assertTrue($result['ok']);
$this->assertDatabaseHas('user_roles', [
'user_id' => $userId,
'role_id' => $roleId,
]);
$this->assertDatabaseHas('staff', [
'user_id' => $userId,
]);
}
}
@@ -0,0 +1,32 @@
<?php
namespace Tests\Unit\Services\Roles;
use App\Models\Role;
use App\Services\Roles\RoleCrudService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class RoleCrudServiceTest extends TestCase
{
use RefreshDatabase;
public function test_create_and_update_role(): void
{
$service = new RoleCrudService();
$role = $service->create([
'name' => 'admin',
'slug' => 'admin',
'description' => 'Admin',
'dashboard_route' => 'administrator/administratordashboard',
'priority' => 1,
'is_active' => 1,
]);
$this->assertInstanceOf(Role::class, $role);
$this->assertDatabaseHas('roles', ['name' => 'admin']);
$service->update($role, ['description' => 'updated']);
$this->assertDatabaseHas('roles', ['id' => $role->id, 'description' => 'updated']);
}
}
@@ -0,0 +1,32 @@
<?php
namespace Tests\Unit\Services\Roles;
use App\Services\Roles\RoleDashboardService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
class RoleDashboardServiceTest extends TestCase
{
use RefreshDatabase;
public function test_best_dashboard_route_for_returns_route(): void
{
DB::table('roles')->insert([
'name' => 'admin',
'slug' => 'admin',
'description' => 'Admin',
'dashboard_route' => 'administrator/administratordashboard',
'priority' => 1,
'is_active' => 1,
'created_at' => now(),
'updated_at' => now(),
]);
$service = new RoleDashboardService();
$route = $service->bestDashboardRouteFor(['admin']);
$this->assertSame('administrator/administratordashboard', $route);
}
}
@@ -0,0 +1,45 @@
<?php
namespace Tests\Unit\Services\Roles;
use App\Services\Roles\RolePermissionService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
class RolePermissionServiceTest extends TestCase
{
use RefreshDatabase;
public function test_save_role_permissions_persists_rows(): void
{
$roleId = DB::table('roles')->insertGetId([
'name' => 'admin',
'slug' => 'admin',
'description' => 'Admin',
'dashboard_route' => 'administrator/administratordashboard',
'priority' => 1,
'is_active' => 1,
'created_at' => now(),
'updated_at' => now(),
]);
$permissionId = DB::table('permissions')->insertGetId([
'name' => 'manage_roles',
'description' => 'Manage roles',
'created_at' => now(),
'updated_at' => now(),
]);
$service = new RolePermissionService();
$service->saveRolePermissions($roleId, [
$permissionId => ['create' => true, 'read' => true],
]);
$this->assertDatabaseHas('role_permissions', [
'role_id' => $roleId,
'permission_id' => $permissionId,
'can_create' => 1,
'can_read' => 1,
]);
}
}
@@ -0,0 +1,74 @@
<?php
namespace Tests\Unit\Services\Roles;
use App\Services\Roles\RoleQueryService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
class RoleQueryServiceTest extends TestCase
{
use RefreshDatabase;
public function test_list_roles_returns_paginator(): void
{
DB::table('roles')->insert([
'name' => 'admin',
'slug' => 'admin',
'description' => 'Admin',
'dashboard_route' => 'administrator/administratordashboard',
'priority' => 1,
'is_active' => 1,
'created_at' => now(),
'updated_at' => now(),
]);
$service = new RoleQueryService();
$page = $service->listRoles([]);
$this->assertCount(1, $page->items());
}
public function test_list_users_with_roles_returns_roles(): void
{
$userId = DB::table('users')->insertGetId([
'firstname' => 'Admin',
'lastname' => 'User',
'cellphone' => '9999999999',
'email' => 'role.query@example.com',
'address_street' => '123 Street',
'city' => 'City',
'state' => 'ST',
'zip' => '12345',
'accept_school_policy' => 1,
'password' => bcrypt('password'),
'user_type' => 'primary',
'semester' => 'Fall',
'school_year' => '2025-2026',
'status' => 'Active',
]);
$roleId = DB::table('roles')->insertGetId([
'name' => 'admin',
'slug' => 'admin',
'description' => 'Admin',
'dashboard_route' => 'administrator/administratordashboard',
'priority' => 1,
'is_active' => 1,
'created_at' => now(),
'updated_at' => now(),
]);
DB::table('user_roles')->insert([
'user_id' => $userId,
'role_id' => $roleId,
]);
$service = new RoleQueryService();
$page = $service->listUsersWithRoles([]);
$this->assertCount(1, $page->items());
$this->assertStringContainsString('admin', $page->items()[0]->roles);
}
}
@@ -0,0 +1,55 @@
<?php
namespace Tests\Unit\Services\Roles;
use App\Services\Roles\RoleDashboardService;
use App\Services\Roles\RoleSwitchService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
class RoleSwitchServiceTest extends TestCase
{
use RefreshDatabase;
public function test_get_user_role_names_returns_roles(): void
{
$userId = DB::table('users')->insertGetId([
'firstname' => 'Admin',
'lastname' => 'User',
'cellphone' => '9999999999',
'email' => 'switch.unit@example.com',
'address_street' => '123 Street',
'city' => 'City',
'state' => 'ST',
'zip' => '12345',
'accept_school_policy' => 1,
'password' => bcrypt('password'),
'user_type' => 'primary',
'semester' => 'Fall',
'school_year' => '2025-2026',
'status' => 'Active',
]);
$roleId = DB::table('roles')->insertGetId([
'name' => 'admin',
'slug' => 'admin',
'description' => 'Admin',
'dashboard_route' => 'administrator/administratordashboard',
'priority' => 1,
'is_active' => 1,
'created_at' => now(),
'updated_at' => now(),
]);
DB::table('user_roles')->insert([
'user_id' => $userId,
'role_id' => $roleId,
]);
$service = new RoleSwitchService(new RoleDashboardService());
$roles = $service->getUserRoleNames($userId);
$this->assertSame(['admin'], $roles);
}
}