add role permission logic
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user