merge: integrate Spatie RBAC Super Admin with Docker, Unit Support, and P2H features

This commit is contained in:
Wahyudin
2026-10-15 20:58:21 +07:00
33 changed files with 2964 additions and 911 deletions
+8 -21
View File
@@ -140,43 +140,30 @@ class AuthenticationTest extends TestCase
}
/**
* Memastikan Admin HSE hanya mengelola status driver & SIMPER tanpa mengubah master profil karyawan.
* Memastikan Admin HSE dapat mengedit data karyawan & SIMPER.
*/
public function test_admin_hse_can_edit_employee_simper_data(): void
{
$hseUser = User::where('username', '1185')->first();
$employee = \App\Models\Employee::first();
$originalName = $employee->name;
$originalPosition = $employee->position;
$response = $this->actingAs($hseUser)->post("/hse/employees/{$employee->id}", [
'nik' => $employee->nik,
'name' => 'Nama Karyawan Diperbarui',
'unit_kerja' => 'Pembangkit',
'division' => 'Mechanical',
'position' => 'Senior Technician',
'is_operator' => '1',
'simper_expired_at' => '2028-12-31',
]);
$response->assertSessionHas('success');
$employee->refresh();
$this->assertEquals($originalName, $employee->name);
$this->assertEquals($originalPosition, $employee->position);
$this->assertTrue((bool) $employee->is_operator);
$this->assertEquals('Nama Karyawan Diperbarui', $employee->name);
$this->assertEquals('Senior Technician', $employee->position);
$this->assertEquals('2028-12-31', $employee->simper_expired_at->format('Y-m-d'));
}
/**
* Memastikan Admin HSE tidak dapat menambahkan master karyawan baru (dialihkan ke Super Admin).
*/
public function test_admin_hse_cannot_create_master_employee(): void
{
$hseUser = User::where('username', '1185')->first();
$response = $this->actingAs($hseUser)->post('/hse/employees', [
'nik' => '9999',
'name' => 'Karyawan Baru',
]);
$response->assertSessionHas('error');
}
/**
* Memastikan jika bukan driver maka tanggal SIMPER otomatis bernilai null.
*/
+287
View File
@@ -0,0 +1,287 @@
<?php
namespace Tests\Feature;
use App\Models\Gate;
use App\Models\User;
use Database\Seeders\RolePermissionSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
use Tests\TestCase;
class SuperAdminRbacTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
// Pastikan role dan permissions ada
$this->seed(RolePermissionSeeder::class);
}
public function test_super_admin_can_login_and_redirects_to_admin_dashboard(): void
{
$response = $this->post(route('login.post'), [
'username' => 'superadmin',
'password' => 'password',
]);
$response->assertRedirect(route('admin.dashboard'));
$this->assertAuthenticated();
}
public function test_super_admin_can_view_dashboard_and_modules(): void
{
$superAdmin = User::where('username', 'superadmin')->first();
$response = $this->actingAs($superAdmin)->get(route('admin.dashboard'));
$response->assertOk();
$response->assertSee('Dashboard Super Admin');
}
public function test_non_super_admin_cannot_access_admin_dashboard(): void
{
$regularUser = User::factory()->create([
'role' => 'hse_admin',
]);
$response = $this->actingAs($regularUser)->get(route('admin.dashboard'));
$response->assertForbidden();
}
public function test_super_admin_can_view_users_page(): void
{
$superAdmin = User::where('username', 'superadmin')->first();
$response = $this->actingAs($superAdmin)->get(route('admin.users.index'));
$response->assertOk();
$response->assertSee('Manajemen Pengguna Sistem');
$response->assertSee('superadmin');
}
public function test_super_admin_can_create_new_user_with_spatie_role(): void
{
$superAdmin = User::where('username', 'superadmin')->first();
$response = $this->actingAs($superAdmin)->post(route('admin.users.store'), [
'name' => 'Operator Baru',
'username' => 'operator.baru',
'email' => '[email protected]',
'password' => 'password123',
'role' => 'gate_officer',
]);
$response->assertRedirect(route('admin.users.index'));
$response->assertSessionHas('success');
$newUser = User::where('username', 'operator.baru')->first();
$this->assertNotNull($newUser);
$this->assertTrue($newUser->hasRole('gate_officer'));
$this->assertEquals('gate_officer', $newUser->role);
}
public function test_super_admin_can_update_user_and_change_role(): void
{
$superAdmin = User::where('username', 'superadmin')->first();
$user = User::factory()->create([
'name' => 'Pinto Yuliandra',
'username' => '1171',
'email' => '[email protected]',
'role' => 'qma_admin',
]);
$user->assignRole('qma_admin');
$response = $this->actingAs($superAdmin)->post(route('admin.users.update', $user->id), [
'name' => 'Pinto Yuliandra Updated',
'username' => '1171',
'email' => '[email protected]',
'role' => 'hse_admin',
]);
$response->assertRedirect(route('admin.users.index'));
$user->refresh();
$this->assertEquals('Pinto Yuliandra Updated', $user->name);
$this->assertTrue($user->hasRole('hse_admin'));
}
public function test_super_admin_cannot_delete_own_account_or_main_superadmin(): void
{
$superAdmin = User::where('username', 'superadmin')->first();
$response = $this->actingAs($superAdmin)->delete(route('admin.users.destroy', $superAdmin->id));
$response->assertRedirect(route('admin.users.index'));
$response->assertSessionHas('error');
$this->assertDatabaseHas('users', ['id' => $superAdmin->id]);
}
public function test_super_admin_can_view_and_update_role_permissions(): void
{
$superAdmin = User::where('username', 'superadmin')->first();
$role = Role::findByName('hse_admin');
$response = $this->actingAs($superAdmin)->get(route('admin.roles.index'));
$response->assertOk();
$response->assertSee('Matriks Role');
// Update permissions for hse_admin
$response = $this->actingAs($superAdmin)->post(route('admin.roles.permissions.update', $role->id), [
'permissions' => ['hse.employees.view'],
]);
$response->assertRedirect(route('admin.roles.index'));
$response->assertSessionHas('success');
$role->refresh();
$this->assertTrue($role->hasPermissionTo('hse.employees.view'));
$this->assertFalse($role->hasPermissionTo('hse.employees.create'));
}
public function test_super_admin_can_assign_direct_permissions_to_user(): void
{
$superAdmin = User::where('username', 'superadmin')->first();
$user = User::factory()->create([
'role' => 'gate_officer',
]);
$user->assignRole('gate_officer');
$response = $this->actingAs($superAdmin)->post(route('admin.users.update', $user->id), [
'name' => $user->name,
'username' => $user->username,
'email' => $user->email,
'role' => 'gate_officer',
'direct_permissions' => ['qma.logs.view'],
]);
$response->assertRedirect(route('admin.users.index'));
$user->refresh();
$this->assertTrue($user->hasDirectPermission('qma.logs.view'));
}
public function test_super_admin_has_bypass_access_to_all_modules(): void
{
$superAdmin = User::where('username', 'superadmin')->first();
// HSE Dashboard
$response = $this->actingAs($superAdmin)->get(route('hse.dashboard'));
$response->assertOk();
// QMA Dashboard
$response = $this->actingAs($superAdmin)->get(route('qma.dashboard'));
$response->assertOk();
// PG Dashboard
$response = $this->actingAs($superAdmin)->get(route('pg.dashboard'));
$response->assertOk();
}
public function test_permission_revocation_immediately_blocks_action_and_hides_button(): void
{
$superAdmin = User::where('username', 'superadmin')->first();
$hseUser = User::factory()->create([
'role' => 'hse_admin',
]);
$hseUser->assignRole('hse_admin');
// 1. Sebelum izin dicabut: bisa melihat tombol dan menambah karyawan
$resView = $this->actingAs($hseUser)->get(route('hse.employees.index'));
$resView->assertOk();
$resView->assertSee('Tambah Karyawan');
$resStore = $this->actingAs($hseUser)->post(route('hse.employees.store'), [
'nik' => 'NIK-TEST-001',
'name' => 'Budi Santoso',
'unit_kerja' => 'Teknik',
'division' => 'Civil',
'position' => 'Driver',
'is_operator' => '1',
'simper_expired_at' => now()->addYear()->format('Y-m-d'),
]);
$resStore->assertRedirect();
$this->assertDatabaseHas('employees', ['nik' => 'NIK-TEST-001']);
// 2. Super Admin mencabut izin 'hse.employees.create' dari role hse_admin
$role = Role::findByName('hse_admin');
$this->actingAs($superAdmin)->post(route('admin.roles.permissions.update', $role->id), [
'permissions' => ['hse.employees.view'], // hse.employees.create sengaja dicabut
]);
// 3. Verifikasi Sinkronisasi Langsung:
// A. Tombol 'Tambah Karyawan' langsung hilang dari halaman index
$resViewRevoked = $this->actingAs($hseUser)->get(route('hse.employees.index'));
$resViewRevoked->assertOk();
$resViewRevoked->assertDontSee('<span>Tambah Karyawan</span>', false);
// B. Akses request POST langsung diblokir 403 Forbidden
$resStoreBlocked = $this->actingAs($hseUser)->post(route('hse.employees.store'), [
'nik' => 'NIK-TEST-002',
'name' => 'Karyawan Baru',
'unit_kerja' => 'Teknik',
'division' => 'Civil',
'position' => 'Driver',
'is_operator' => '1',
]);
$resStoreBlocked->assertForbidden();
$this->assertDatabaseMissing('employees', ['nik' => 'NIK-TEST-002']);
// 4. Super Admin mengembalikan izin 'hse.employees.create'
$this->actingAs($superAdmin)->post(route('admin.roles.permissions.update', $role->id), [
'permissions' => ['hse.employees.view', 'hse.employees.create', 'hse.employees.edit'],
]);
// 5. User langsung bisa menambah karyawan kembali
$resStoreRestored = $this->actingAs($hseUser)->post(route('hse.employees.store'), [
'nik' => 'NIK-TEST-002',
'name' => 'Karyawan Baru',
'unit_kerja' => 'Teknik',
'division' => 'Civil',
'position' => 'Driver',
'is_operator' => '1',
]);
$resStoreRestored->assertRedirect();
$this->assertDatabaseHas('employees', ['nik' => 'NIK-TEST-002']);
}
public function test_user_direct_permission_sync_allows_or_denies_immediately(): void
{
$superAdmin = User::where('username', 'superadmin')->first();
$gateUser = User::factory()->create([
'role' => 'gate_officer',
]);
$gateUser->assignRole('gate_officer');
// Awalnya gate officer tidak punya izin melihat qma activity log
$resDenied = $this->actingAs($gateUser)->get(route('qma.activity.log'));
$resDenied->assertForbidden();
// Super Admin memberikan direct permission qma.logs.view ke user ini
$this->actingAs($superAdmin)->post(route('admin.users.update', $gateUser->id), [
'name' => $gateUser->name,
'username' => $gateUser->username,
'email' => $gateUser->email,
'role' => 'gate_officer',
'direct_permissions' => ['qma.logs.view'],
]);
// User langsung dapat mengakses activity log
$resAllowed = $this->actingAs($gateUser->fresh())->get(route('qma.activity.log'));
$resAllowed->assertOk();
// Super Admin mencabut izin langsung tersebut
$this->actingAs($superAdmin)->post(route('admin.users.update', $gateUser->id), [
'name' => $gateUser->name,
'username' => $gateUser->username,
'email' => $gateUser->email,
'role' => 'gate_officer',
'direct_permissions' => [],
]);
// User langsung ditolak kembali
$resDeniedAgain = $this->actingAs($gateUser->fresh())->get(route('qma.activity.log'));
$resDeniedAgain->assertForbidden();
}
}
-362
View File
@@ -1,362 +0,0 @@
<?php
namespace Tests\Feature;
use App\Models\Gate;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Hash;
use Tests\TestCase;
class SuperAdminTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
$this->seed();
}
/**
* Memastikan Super Admin dapat login dan diarahkan langsung ke dashboard Super Admin.
*/
public function test_super_admin_can_login_and_redirects_to_superadmin_dashboard(): void
{
$response = $this->post('/login', [
'username' => 'superadmin',
'password' => 'password',
]);
$response->assertRedirect('/superadmin/dashboard');
$this->assertAuthenticated();
$user = auth()->user();
$this->assertEquals('superadmin', $user->username);
$this->assertEquals('super_admin', $user->role);
$this->assertTrue($user->isSuperAdmin());
}
/**
* Memastikan user non-super-admin ditolak saat mengakses rute /superadmin.
*/
public function test_non_super_admin_cannot_access_superadmin_dashboard(): void
{
$hseUser = User::where('role', 'hse_admin')->first();
$response = $this->actingAs($hseUser)->get('/superadmin');
$response->assertStatus(403);
}
/**
* Memastikan Super Admin dapat melihat halaman dashboard admin kosong (placeholder awal).
*/
public function test_super_admin_can_view_admin_dashboard(): void
{
$superAdmin = User::where('role', 'super_admin')->first();
$response = $this->actingAs($superAdmin)->get('/superadmin/dashboard');
$response->assertStatus(200);
$response->assertSee('Dashboard Admin');
$response->assertSee('Halaman Dashboard Sedang Disiapkan');
$response->assertSee('Buka Manajemen Role');
}
/**
* Memastikan Super Admin dapat melihat halaman manajemen role & hak akses.
*/
public function test_super_admin_can_view_role_management_page(): void
{
$superAdmin = User::where('role', 'super_admin')->first();
$response = $this->actingAs($superAdmin)->get('/superadmin/roles');
$response->assertStatus(200);
$response->assertSee('Manajemen Role');
$response->assertSee('Hak Akses');
$response->assertSee('Tambah Akun Baru');
$response->assertSee('openDetailUserModal');
$response->assertSee('detailUserModal');
$response->assertSee('Detail Akun');
$response->assertDontSee('HIRARKI TERTINGGI SISTEM');
$response->assertDontSee('Pintasan Modul Langsung');
}
/**
* Memastikan Super Admin dapat mengakses seluruh modul departemen (HSE, QMA, PG, Gate).
*/
public function test_super_admin_has_full_access_to_all_modules(): void
{
$superAdmin = User::where('role', 'super_admin')->first();
// 1. Modul HSE
$this->actingAs($superAdmin)->get('/hse/dashboard')->assertStatus(200);
$this->actingAs($superAdmin)->get('/hse/employees')->assertStatus(200);
// 2. Modul QMA
$this->actingAs($superAdmin)->get('/qma/dashboard')->assertStatus(200);
$this->actingAs($superAdmin)->get('/qma/vehicles/light-vehicle')->assertStatus(200);
$this->actingAs($superAdmin)->get('/qma/activity-log')->assertStatus(200);
// 3. Modul PG
$this->actingAs($superAdmin)->get('/pg/dashboard')->assertStatus(200);
$this->actingAs($superAdmin)->get('/pg/guards')->assertStatus(200);
$this->actingAs($superAdmin)->get('/pg/verification-logs')->assertStatus(200);
// 4. Modul Pos Gerbang
$this->actingAs($superAdmin)->get('/pg/gate-inspection')->assertStatus(200);
}
/**
* Memastikan Super Admin dapat membuat akun pengguna baru.
*/
public function test_super_admin_can_create_new_user(): void
{
$superAdmin = User::where('role', 'super_admin')->first();
$response = $this->actingAs($superAdmin)->post('/superadmin/users', [
'name' => 'Admin Baru HSE',
'username' => 'hse.baru',
'email' => '[email protected]',
'password' => 'secret123',
'role' => 'hse_admin',
]);
$response->assertRedirect('/superadmin/roles');
$response->assertSessionHas('success');
$this->assertDatabaseHas('users', [
'username' => 'hse.baru',
'email' => '[email protected]',
'role' => 'hse_admin',
]);
// Verifikasi ketika password tidak dikirim, otomatis memakai default 'password'
$respDefault = $this->actingAs($superAdmin)->post('/superadmin/users', [
'name' => 'Admin Baru QMA',
'username' => 'qma.baru',
'email' => '[email protected]',
'role' => 'qma_admin',
]);
$respDefault->assertRedirect('/superadmin/roles');
$newUser = User::where('username', 'qma.baru')->first();
$this->assertNotNull($newUser);
$this->assertTrue(Hash::check('password', $newUser->password));
}
/**
* Memastikan Super Admin dapat memperbarui akun pengguna.
*/
public function test_super_admin_can_update_user(): void
{
$superAdmin = User::where('role', 'super_admin')->first();
$targetUser = User::where('username', '1185')->first();
$response = $this->actingAs($superAdmin)->put("/superadmin/users/{$targetUser->id}", [
'name' => 'Ibu Mima Updated',
'username' => '1185',
'email' => '[email protected]',
'role' => 'hse_admin',
]);
$response->assertRedirect('/superadmin/roles');
$response->assertSessionHas('success');
$this->assertDatabaseHas('users', [
'id' => $targetUser->id,
'name' => 'Ibu Mima Updated',
'email' => '[email protected]',
]);
}
/**
* Memastikan Super Admin tidak dapat menghapus akunnya sendiri.
*/
public function test_super_admin_cannot_delete_own_account(): void
{
$superAdmin = User::where('role', 'super_admin')->first();
$response = $this->actingAs($superAdmin)->delete("/superadmin/users/{$superAdmin->id}");
$response->assertSessionHas('error');
$this->assertDatabaseHas('users', [
'id' => $superAdmin->id,
]);
}
/**
* Memastikan Super Admin dapat menghapus akun pengguna lain.
*/
public function test_super_admin_can_delete_other_user(): void
{
$superAdmin = User::where('role', 'super_admin')->first();
$dummyUser = User::create([
'name' => 'User Dihapus',
'username' => 'user.hapus',
'email' => '[email protected]',
'password' => Hash::make('password'),
'role' => 'gate_officer',
]);
$response = $this->actingAs($superAdmin)->delete("/superadmin/users/{$dummyUser->id}");
$response->assertRedirect('/superadmin/roles');
$response->assertSessionHas('success');
$this->assertDatabaseMissing('users', [
'id' => $dummyUser->id,
]);
}
/**
* Memastikan ketika akun Karyawan dihapus, record di tabel Employee juga terhapus (sinkron).
*/
public function test_deleting_employee_user_also_deletes_employee_record(): void
{
$superAdmin = User::where('role', 'super_admin')->first();
$employeeUser = User::create([
'name' => 'Karyawan Uji Hapus',
'username' => 'NIK-HAPUS-01',
'email' => '[email protected]',
'password' => Hash::make('password'),
'role' => 'employee',
]);
$employee = \App\Models\Employee::create([
'user_id' => $employeeUser->id,
'nik' => 'NIK-HAPUS-01',
'name' => 'Karyawan Uji Hapus',
'unit_kerja' => 'Pembangkit',
'division' => 'Mechanical',
'position' => 'Teknisi',
'is_operator' => false,
]);
$response = $this->actingAs($superAdmin)->delete("/superadmin/users/{$employeeUser->id}");
$response->assertSessionHas('success');
$this->assertDatabaseMissing('users', ['id' => $employeeUser->id]);
$this->assertDatabaseMissing('employees', ['id' => $employee->id]);
}
/**
* Memastikan akun Karyawan yang memiliki rekam jejak aktivitas operasional TIDAK DAPAT dihapus.
*/
public function test_cannot_delete_employee_user_with_activity_history(): void
{
$superAdmin = User::where('role', 'super_admin')->first();
$gate = \App\Models\Gate::first();
$vehicle = \App\Models\Vehicle::first();
$employeeUser = User::create([
'name' => 'Driver Audit Trail',
'username' => 'NIK-AUDIT-01',
'email' => '[email protected]',
'password' => Hash::make('password'),
'role' => 'employee',
]);
$employee = \App\Models\Employee::create([
'user_id' => $employeeUser->id,
'nik' => 'NIK-AUDIT-01',
'name' => 'Driver Audit Trail',
'unit_kerja' => 'Pembangkit',
'division' => 'Operasional',
'position' => 'Driver',
'is_operator' => true,
]);
\App\Models\VehicleActivity::create([
'transaction_no' => 'KMH-ACT-TEST-001',
'vehicle_id' => $vehicle->id,
'driver_id' => $employee->id,
'gate_id' => $gate->id,
'direction' => 'out',
'previous_km' => 100,
'current_km' => 150,
'distance' => 50,
'purpose' => 'Uji Coba Audit Trail',
'recorded_at' => now(),
'validation_status' => 'approved',
]);
$response = $this->actingAs($superAdmin)->delete("/superadmin/users/{$employeeUser->id}");
$response->assertSessionHas('error');
$this->assertDatabaseHas('users', ['id' => $employeeUser->id]);
$this->assertDatabaseHas('employees', ['id' => $employee->id]);
$this->assertDatabaseHas('vehicle_activities', ['transaction_no' => 'KMH-ACT-TEST-001']);
}
/**
* Memastikan Super Admin dapat mereset password akun pengguna ke default "password".
*/
public function test_super_admin_can_reset_user_password_to_default(): void
{
$superAdmin = User::where('role', 'super_admin')->first();
$user = User::create([
'name' => 'Akun Uji Reset',
'username' => 'uji.reset',
'email' => '[email protected]',
'password' => Hash::make('customPassword123!'),
'role' => 'gate_officer',
]);
$this->assertTrue(Hash::check('customPassword123!', $user->password));
$response = $this->actingAs($superAdmin)->post("/superadmin/users/{$user->id}/reset-password");
$response->assertRedirect('/superadmin/roles');
$response->assertSessionHas('success');
$user->refresh();
$this->assertTrue(Hash::check('password', $user->password));
}
/**
* Memastikan Super Admin dapat melihat, memfilter, dan membuat akun role Karyawan (employee).
*/
public function test_super_admin_can_manage_employee_role(): void
{
$superAdmin = User::where('role', 'super_admin')->first();
// 1. Filter role employee
$response = $this->actingAs($superAdmin)->get('/superadmin/roles?role=employee');
$response->assertOk();
$response->assertSee('Karyawan');
// 2. Tambah akun role employee
$storeResponse = $this->actingAs($superAdmin)->post('/superadmin/users', [
'name' => 'Karyawan Baru Test',
'username' => 'KMH-TEST-999',
'email' => '[email protected]',
'password' => 'password',
'role' => 'employee',
'unit_kerja' => 'Pembangkit',
'division' => 'Mechanical',
'position' => 'Teknisi Turbin',
]);
$storeResponse->assertRedirect('/superadmin/roles');
$storeResponse->assertSessionHas('success');
$this->assertDatabaseHas('users', [
'username' => 'KMH-TEST-999',
'email' => '[email protected]',
'role' => 'employee',
]);
$this->assertDatabaseHas('employees', [
'nik' => 'KMH-TEST-999',
'name' => 'Karyawan Baru Test',
'unit_kerja' => 'Pembangkit',
'division' => 'Mechanical',
'position' => 'Teknisi Turbin',
]);
}
}