feat: initial commit Sistem RKM Laravel (HSE, QMA, Pos Gerbang, QR Activity)

This commit is contained in:
Wahyudin
2026-10-12 23:47:41 +07:00
commit 21ce908199
94 changed files with 17600 additions and 0 deletions
+222
View File
@@ -0,0 +1,222 @@
<?php
namespace Tests\Feature;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class AuthenticationTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
$this->seed();
}
/**
* Memastikan halaman login dapat diakses publik.
*/
public function test_login_page_renders_successfully(): void
{
$response = $this->get('/login');
$response->assertStatus(200);
$response->assertSee('PLTA Merangin 350 MW');
$response->assertSee('Username');
$response->assertSee('Password');
}
/**
* Memastikan Admin HSE (Mima - 1185) dapat login dan diarahkan ke dashboard HSE.
*/
public function test_admin_hse_can_login_and_redirects_to_hse_module(): void
{
$response = $this->post('/login', [
'username' => '1185',
'password' => 'password',
]);
$response->assertRedirect('/hse/dashboard');
$this->assertAuthenticated();
$user = auth()->user();
$this->assertEquals('1185', $user->username);
$this->assertEquals('Mima', $user->name);
$this->assertEquals('hse_admin', $user->role);
}
/**
* Memastikan Admin QMA (Pinto Yuliandra - 1171) dapat login dan diarahkan ke Log QMA.
*/
public function test_admin_qma_can_login_and_redirects_to_qma_module(): void
{
$response = $this->post('/login', [
'username' => '1171',
'password' => 'password',
]);
$response->assertRedirect('/qma/dashboard');
$this->assertAuthenticated();
$user = auth()->user();
$this->assertEquals('1171', $user->username);
$this->assertEquals('Pinto Yuliandra', $user->name);
$this->assertEquals('qma_admin', $user->role);
}
/**
* Memastikan Admin PG (Admin PG - AdminPG) dapat login dan diarahkan ke Master Satpam PG.
*/
public function test_admin_pg_can_login_and_redirects_to_pg_module(): void
{
$response = $this->post('/login', [
'username' => 'AdminPG',
'password' => 'password',
]);
$response->assertRedirect('/pg/guards');
$this->assertAuthenticated();
$user = auth()->user();
$this->assertEquals('AdminPG', $user->username);
$this->assertEquals('Admin PG', $user->name);
$this->assertEquals('pg_admin', $user->role);
}
/**
* Memastikan login gagal dengan pesan error jika kredensial tidak cocok.
*/
public function test_login_fails_with_invalid_credentials(): void
{
$response = $this->post('/login', [
'username' => '1185',
'password' => 'salah-password',
]);
$response->assertSessionHasErrors('username');
$this->assertGuest();
}
/**
* Memastikan Role-Based Access Control (RBAC) melindungi rute secara ketat.
*/
public function test_role_based_access_control_protection(): void
{
$hseUser = User::where('username', '1185')->first();
$qmaUser = User::where('username', '1171')->first();
$pgUser = User::where('username', 'AdminPG')->first();
// 1. Guest mencoba akses rute terproteksi -> dialihkan ke login
$this->get('/hse/employees')->assertRedirect('/login');
$this->get('/qma/activity-log')->assertRedirect('/login');
$this->get('/pg/guards')->assertRedirect('/login');
// 2. Admin HSE dapat akses modul HSE, ditolak dari modul QMA & PG
$this->actingAs($hseUser)->get('/hse/employees')->assertStatus(200);
$this->actingAs($hseUser)->get('/qma/activity-log')->assertStatus(403);
$this->actingAs($hseUser)->get('/pg/guards')->assertStatus(403);
// 3. Admin QMA dapat akses modul QMA, ditolak dari modul HSE & PG
$this->actingAs($qmaUser)->get('/qma/activity-log')->assertStatus(200);
$this->actingAs($qmaUser)->get('/hse/employees')->assertStatus(403);
$this->actingAs($qmaUser)->get('/pg/guards')->assertStatus(403);
// 4. Admin PG dapat akses modul PG guards & gate inspection, ditolak dari HSE
$this->actingAs($pgUser)->get('/pg/guards')->assertStatus(200);
$this->actingAs($pgUser)->get('/pg/gate-inspection')->assertStatus(200);
$this->actingAs($pgUser)->get('/hse/employees')->assertStatus(403);
}
/**
* 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();
$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('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 jika bukan driver maka tanggal SIMPER otomatis bernilai null.
*/
public function test_non_driver_employee_has_null_simper_date(): void
{
$hseUser = User::where('username', '1185')->first();
$employee = \App\Models\Employee::first();
$response = $this->actingAs($hseUser)->post("/hse/employees/{$employee->id}", [
'nik' => $employee->nik,
'name' => $employee->name,
'unit_kerja' => $employee->unit_kerja,
'division' => $employee->division,
'position' => 'Staff Keuangan',
'is_operator' => '0',
'simper_expired_at' => '2029-01-01',
]);
$response->assertSessionHas('success');
$employee->refresh();
$this->assertFalse((bool) $employee->is_operator);
$this->assertNull($employee->simper_expired_at);
}
/**
* Memastikan user dapat logout dengan aman.
*/
public function test_authenticated_user_can_logout(): void
{
$user = User::where('username', '1185')->first();
$response = $this->actingAs($user)->post('/logout');
$response->assertRedirect('/login');
$this->assertGuest();
}
/**
* Memastikan halaman profil dapat diakses oleh pengguna yang login.
*/
public function test_authenticated_user_can_access_and_update_profile(): void
{
$user = User::where('username', '1185')->first();
// Guest dilarang akses
$this->get('/profile')->assertRedirect('/login');
// User terautentikasi dapat melihat halaman profil
$this->actingAs($user)
->get('/profile')
->assertStatus(200)
->assertSee('Profil Saya');
// User dapat memperbarui nama & email
$response = $this->actingAs($user)->put('/profile', [
'name' => 'Mima HSE Updated',
'email' => '[email protected]',
]);
$response->assertSessionHas('success');
$user->refresh();
$this->assertEquals('Mima HSE Updated', $user->name);
$this->assertEquals('[email protected]', $user->email);
}
}
+19
View File
@@ -0,0 +1,19 @@
<?php
namespace Tests\Feature;
// use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class ExampleTest extends TestCase
{
/**
* A basic test example.
*/
public function test_the_application_returns_a_successful_response(): void
{
$response = $this->get('/');
$response->assertRedirect('/login');
}
}
+243
View File
@@ -0,0 +1,243 @@
<?php
namespace Tests\Feature;
use App\Models\User;
use App\Models\Vehicle;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class QmaModuleTest extends TestCase
{
use RefreshDatabase;
protected User $qmaAdmin;
protected User $hseAdmin;
protected function setUp(): void
{
parent::setUp();
$this->qmaAdmin = User::factory()->create([
'name' => 'Pinto Yuliandra',
'username' => '1171',
'role' => 'qma_admin',
]);
$this->hseAdmin = User::factory()->create([
'name' => 'Mima',
'username' => '1185',
'role' => 'hse_admin',
]);
}
/**
* Memastikan QMA dashboard dapat diakses oleh Admin QMA.
*/
public function test_qma_dashboard_renders_successfully(): void
{
$response = $this->actingAs($this->qmaAdmin)->get('/qma/dashboard');
$response->assertStatus(200);
$response->assertSee('Divisi QMA (Quality Management Asset)');
$response->assertSee('Selamat datang, Pinto Yuliandra');
$response->assertSee('Light Vehicle');
$response->assertSee('Unit Support');
$response->assertSee('Log Aktivitas Kendaraan');
}
/**
* Memastikan route /qma/vehicles redirect ke /qma/vehicles/light-vehicle.
*/
public function test_qma_vehicles_index_redirects_to_lv(): void
{
$response = $this->actingAs($this->qmaAdmin)->get('/qma/vehicles');
$response->assertRedirect('/qma/vehicles/light-vehicle');
}
/**
* Memastikan halaman khusus Light Vehicle (LV) memuat armada LV.
*/
public function test_qma_lv_page_renders_successfully(): void
{
Vehicle::create([
'code' => 'LV99',
'category' => 'light_vehicle',
'brand_type' => 'Toyota Hilux 4x4',
'license_plate' => 'BH 1234 XY',
'ownership' => 'PT KMH',
'status' => 'Operation',
'current_km' => 10000,
]);
Vehicle::create([
'code' => 'EX99',
'category' => 'unit_support',
'brand_type' => 'Komatsu PC200',
'unit_category' => 'Excavator',
'ownership' => 'PT KMH Project',
'status' => 'Standby',
'current_km' => 2500,
]);
$response = $this->actingAs($this->qmaAdmin)->get('/qma/vehicles/light-vehicle');
$response->assertStatus(200);
$response->assertSee('Light Vehicle');
$response->assertSee('LV99');
$response->assertDontSee('EX99');
}
/**
* Memastikan halaman khusus Unit Support memuat armada alat berat.
*/
public function test_qma_unit_support_page_renders_successfully(): void
{
Vehicle::create([
'code' => 'LV99',
'category' => 'light_vehicle',
'brand_type' => 'Toyota Hilux 4x4',
'license_plate' => 'BH 1234 XY',
'ownership' => 'PT KMH',
'status' => 'Operation',
'current_km' => 10000,
]);
Vehicle::create([
'code' => 'EX99',
'category' => 'unit_support',
'brand_type' => 'Komatsu PC200',
'unit_category' => 'Excavator',
'ownership' => 'PT KMH Project',
'status' => 'Standby',
'current_km' => 2500,
]);
$response = $this->actingAs($this->qmaAdmin)->get('/qma/vehicles/unit-support');
$response->assertStatus(200);
$response->assertSee('Unit Support');
$response->assertSee('EX99');
$response->assertDontSee('LV99');
}
/**
* Memastikan Admin QMA dapat menambah kendaraan ringan baru.
*/
public function test_admin_qma_can_store_light_vehicle(): void
{
$response = $this->actingAs($this->qmaAdmin)->post('/qma/vehicles', [
'code' => 'LV77',
'category' => 'light_vehicle',
'brand_type' => 'Isuzu D-Max',
'license_plate' => 'BH 5555 ZZ',
'ownership' => 'PT KMH Divisi HSE',
'division' => 'HSE',
'status' => 'Operation',
'current_km' => 1500,
]);
$response->assertSessionHas('success');
$this->assertDatabaseHas('vehicles', [
'code' => 'LV77',
'category' => 'light_vehicle',
'license_plate' => 'BH 5555 ZZ',
]);
}
/**
* Memastikan Admin QMA dapat menambah unit support baru dengan dokumen teknis.
*/
public function test_admin_qma_can_store_unit_support(): void
{
$response = $this->actingAs($this->qmaAdmin)->post('/qma/vehicles', [
'code' => 'DZ01',
'category' => 'unit_support',
'brand_type' => 'Caterpillar D6R',
'unit_category' => 'Bulldozer',
'unit_type' => 'Track-type Tractor',
'brand' => 'Caterpillar',
'model_number' => 'D6R XL',
'production_year' => 2020,
'ownership' => 'PT KMH',
'division' => 'Civil',
'location' => 'CCR DAM',
'status' => 'Operation',
'current_km' => 4500,
'fuel_consumption' => 22.5,
'silo_document' => 'SILO-2026/01',
'kir_document' => 'KIR-2026/01',
]);
$response->assertSessionHas('success');
$this->assertDatabaseHas('vehicles', [
'code' => 'DZ01',
'category' => 'unit_support',
'unit_category' => 'Bulldozer',
'silo_document' => 'SILO-2026/01',
]);
}
/**
* Memastikan Admin QMA dapat mengupdate kendaraan.
*/
public function test_admin_qma_can_update_vehicle(): void
{
$vehicle = Vehicle::create([
'code' => 'LV50',
'category' => 'light_vehicle',
'brand_type' => 'Toyota Hilux',
'status' => 'Standby',
'current_km' => 3000,
]);
$response = $this->actingAs($this->qmaAdmin)->post("/qma/vehicles/{$vehicle->id}", [
'code' => 'LV50',
'category' => 'light_vehicle',
'brand_type' => 'Toyota Hilux Facelift',
'status' => 'Operation',
'current_km' => 3500,
]);
$response->assertSessionHas('success');
$this->assertDatabaseHas('vehicles', [
'id' => $vehicle->id,
'brand_type' => 'Toyota Hilux Facelift',
'status' => 'Operation',
'current_km' => 3500,
]);
}
/**
* Memastikan penghapusan kendaraan terlindungi jika tidak ada relasi aktivitas.
*/
public function test_vehicle_deletion_protection_when_activities_exist(): void
{
$vehicle = Vehicle::create([
'code' => 'LV05',
'category' => 'light_vehicle',
'brand_type' => 'Toyota Hilux',
'status' => 'Operation',
'current_km' => 1000,
]);
$response = $this->actingAs($this->qmaAdmin)->delete("/qma/vehicles/{$vehicle->id}");
$response->assertSessionHas('success');
$this->assertDatabaseMissing('vehicles', ['id' => $vehicle->id]);
}
/**
* Memastikan role selain QMA Admin ditolak dari rute QMA.
*/
public function test_unauthorized_role_cannot_access_qma_routes(): void
{
$response = $this->actingAs($this->hseAdmin)->get('/qma/dashboard');
$response->assertStatus(403);
$responseLv = $this->actingAs($this->hseAdmin)->get('/qma/vehicles/light-vehicle');
$responseLv->assertStatus(403);
$responseSupport = $this->actingAs($this->hseAdmin)->get('/qma/vehicles/unit-support');
$responseSupport->assertStatus(403);
}
}
+185
View File
@@ -0,0 +1,185 @@
<?php
namespace Tests\Feature;
use App\Models\Employee;
use App\Models\Gate;
use App\Models\SecurityGuard;
use App\Models\User;
use App\Models\Vehicle;
use App\Models\VehicleActivity;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class VehicleActivityTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
// Seed data dasar
$this->seed();
}
public function test_form_qr_loads_successfully_for_light_vehicle(): void
{
$response = $this->get('/gate-activity/LV62');
$response->assertStatus(200);
$response->assertSee('Input Timesheet Unit');
$response->assertSee('LV62');
$response->assertSee('MITSUBISHI - L300 - PICK UP');
}
public function test_submit_blocked_if_driver_simper_is_expired(): void
{
$vehicle = Vehicle::where('code', 'LV62')->first();
$gate = Gate::first();
// Driver dengan SIMPER kedaluwarsa (Randi Kurniawan)
$expiredDriver = Employee::where('nik', 'KMH-2023-012')->first();
$response = $this->post("/gate-activity/{$vehicle->code}", [
'driver_id' => $expiredDriver->id,
'gate_id' => $gate->id,
'direction' => 'out',
'current_km' => 12600,
'purpose' => 'Inspeksi Adit 1',
]);
$response->assertSessionHasErrors('driver_id');
$this->assertDatabaseMissing('vehicle_activities', [
'vehicle_id' => $vehicle->id,
'driver_id' => $expiredDriver->id,
]);
}
public function test_submit_blocked_if_odometer_is_less_than_previous(): void
{
$vehicle = Vehicle::where('code', 'LV62')->first(); // current_km: 12500
$gate = Gate::first();
$validDriver = Employee::where('nik', 'KMH-2022-001')->first(); // Eka Sufiatna (SIMPER Aktif)
// Coba input KM lebih kecil dari 12500
$response = $this->post("/gate-activity/{$vehicle->code}", [
'driver_id' => $validDriver->id,
'gate_id' => $gate->id,
'direction' => 'out',
'current_km' => 12400, // Error: < 12500
'purpose' => 'Perjalanan dinas',
]);
$response->assertSessionHasErrors('current_km');
}
public function test_submit_successful_with_valid_data_and_enforces_alternating_direction(): void
{
$vehicle = Vehicle::where('code', 'LV62')->first(); // current_km: 12500
$gate = Gate::first();
$validDriver = Employee::where('nik', 'KMH-2022-001')->first();
// 1. Transaksi pertama: KELUAR (Out)
$response1 = $this->post("/gate-activity/{$vehicle->code}", [
'driver_id' => $validDriver->id,
'gate_id' => $gate->id,
'direction' => 'out',
'current_km' => 12550,
'purpose' => 'Antar teknisi ke Power House',
]);
$response1->assertRedirect();
$this->assertDatabaseHas('vehicle_activities', [
'vehicle_id' => $vehicle->id,
'driver_id' => $validDriver->id,
'direction' => 'out',
'current_km' => 12550,
'validation_status' => 'pending',
]);
// 2. Coba kirim KELUAR (Out) lagi -> Wajib Ditolak karena harus MASUK (In)!
$response2 = $this->post("/gate-activity/{$vehicle->code}", [
'driver_id' => $validDriver->id,
'gate_id' => $gate->id,
'direction' => 'out', // Error: Harus 'in'
'current_km' => 12580,
'purpose' => 'Coba keluar lagi',
]);
$response2->assertSessionHasErrors('direction');
// 3. Kirim MASUK (In) -> Berhasil!
$response3 = $this->post("/gate-activity/{$vehicle->code}", [
'driver_id' => $validDriver->id,
'gate_id' => $gate->id,
'direction' => 'in',
'current_km' => 12600,
'purpose' => 'Kembali ke kantor',
]);
$response3->assertRedirect();
$this->assertDatabaseHas('vehicle_activities', [
'vehicle_id' => $vehicle->id,
'direction' => 'in',
'current_km' => 12600,
]);
}
public function test_security_guard_can_approve_activity_and_updates_master_odometer(): void
{
$vehicle = Vehicle::where('code', 'LV62')->first();
$gate = Gate::first();
$validDriver = Employee::where('nik', 'KMH-2022-001')->first();
$guard = SecurityGuard::first();
$activity = VehicleActivity::create([
'transaction_no' => 'TRX-TEST-001',
'vehicle_id' => $vehicle->id,
'driver_id' => $validDriver->id,
'gate_id' => $gate->id,
'direction' => 'out',
'previous_km' => 12500,
'current_km' => 12580,
'distance' => 80,
'purpose' => 'Perjalanan dinas Adit 2',
'recorded_at' => Carbon::now(),
'validation_status' => 'pending',
]);
// Satpam approve (Petugas Pos Gerbang login)
$gateOfficer = User::where('role', 'gate_officer')->first();
$response = $this->actingAs($gateOfficer)->post("/pg/gate-inspection/{$activity->id}/approve", [
'security_guard_id' => $guard->id,
]);
$response->assertRedirect();
// Verifikasi status transaksi
$this->assertDatabaseHas('vehicle_activities', [
'id' => $activity->id,
'validation_status' => 'approved',
'security_guard_id' => $guard->id,
]);
// Verifikasi master odometer terupdate
$this->assertEquals(12580, (float) $vehicle->fresh()->current_km);
}
public function test_qma_log_renders_10_columns(): void
{
$qmaAdmin = User::where('role', 'qma_admin')->first();
$response = $this->actingAs($qmaAdmin)->get('/qma/activity-log');
$response->assertStatus(200);
$response->assertSee('Log Aktivitas Kendaraan QMA');
$response->assertSee('1. Tanggal');
$response->assertSee('2. Kode LV');
$response->assertSee('3. Unit');
$response->assertSee('4. Penanggung Jawab');
$response->assertSee('5. Driver');
$response->assertSee('6. Gate');
$response->assertSee('7. Akses');
$response->assertSee('8. KM Sebelumnya');
$response->assertSee('9. KM Berjalan');
$response->assertSee('10. Aktifitas');
}
}