feat: implement flexible hse simper types, multi-simper personil, and mobile heavy timesheet integration

This commit is contained in:
Ray1111-aa
2026-10-25 22:34:11 +07:00
parent c2116712e7
commit 9897404f61
25 changed files with 2096 additions and 70 deletions
+192
View File
@@ -0,0 +1,192 @@
<?php
namespace Tests\Feature;
use App\Models\Employee;
use App\Models\EmployeeSimper;
use App\Models\SimperType;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
use Tests\TestCase;
class HseSimperTypeTest extends TestCase
{
use RefreshDatabase;
protected User $adminHse;
protected User $regularUser;
protected function setUp(): void
{
parent::setUp();
// Siapkan permission & role
Permission::firstOrCreate(['name' => 'hse.employees.view', 'guard_name' => 'web']);
Permission::firstOrCreate(['name' => 'hse.employees.create', 'guard_name' => 'web']);
Permission::firstOrCreate(['name' => 'hse.employees.edit', 'guard_name' => 'web']);
$roleHse = Role::firstOrCreate(['name' => 'admin_hse', 'guard_name' => 'web']);
$roleHse->givePermissionTo(['hse.employees.view', 'hse.employees.create', 'hse.employees.edit']);
$this->adminHse = User::create([
'name' => 'Admin HSE Tester',
'username' => 'admin_hse_tester',
'email' => '[email protected]',
'role' => 'admin_hse',
'password' => bcrypt('password'),
]);
$this->adminHse->assignRole('admin_hse');
$this->regularUser = User::create([
'name' => 'Regular User',
'username' => 'regular_user',
'email' => '[email protected]',
'role' => 'employee',
'password' => bcrypt('password'),
]);
}
/**
* Tamu diarahkan ke login.
*/
public function test_guest_cannot_access_simper_types(): void
{
$response = $this->get(route('hse.simper_types.index'));
$response->assertRedirect(route('login'));
}
/**
* Admin HSE dapat melihat halaman master jenis SIMPER.
*/
public function test_admin_hse_can_view_simper_types_index(): void
{
$response = $this->actingAs($this->adminHse)->get(route('hse.simper_types.index'));
$response->assertStatus(200);
$response->assertSee('Master Jenis SIMPER');
$response->assertSee('SIMPER-LV');
$response->assertSee('SIMPER-DT');
}
/**
* Admin HSE dapat menambahkan jenis SIMPER baru.
*/
public function test_admin_hse_can_create_new_simper_type(): void
{
$data = [
'code' => 'SIMPER-FORKLIFT',
'name' => 'SIMPER Forklift & Material Handler',
'category_scope' => 'unit_support_heavy',
'description' => 'Izin mengoperasikan unit forklift di gudang.',
'is_active' => '1',
];
$response = $this->actingAs($this->adminHse)
->from(route('hse.simper_types.index'))
->post(route('hse.simper_types.store'), $data);
$response->assertRedirect(route('hse.simper_types.index'));
$response->assertSessionHas('success');
$this->assertDatabaseHas('simper_types', [
'code' => 'SIMPER-FORKLIFT',
'name' => 'SIMPER Forklift & Material Handler',
'category_scope' => 'unit_support_heavy',
]);
}
/**
* Validasi menolak kode duplikat.
*/
public function test_cannot_create_duplicate_simper_code(): void
{
$existing = SimperType::first();
$data = [
'code' => $existing->code,
'name' => 'Nama Lain',
'category_scope' => 'all',
];
$response = $this->actingAs($this->adminHse)
->from(route('hse.simper_types.index'))
->post(route('hse.simper_types.store'), $data);
$response->assertSessionHasErrors('code');
}
/**
* Admin HSE dapat mengupdate jenis SIMPER.
*/
public function test_admin_hse_can_update_simper_type(): void
{
$type = SimperType::first();
$data = [
'code' => $type->code,
'name' => 'Nama Terkoreksi',
'category_scope' => 'light_vehicle',
'description' => 'Deskripsi baru',
'is_active' => '1',
];
$response = $this->actingAs($this->adminHse)
->from(route('hse.simper_types.index'))
->put(route('hse.simper_types.update', $type->id), $data);
$response->assertRedirect(route('hse.simper_types.index'));
$response->assertSessionHas('success');
$this->assertDatabaseHas('simper_types', [
'id' => $type->id,
'name' => 'Nama Terkoreksi',
]);
}
/**
* Admin HSE dapat mendaftarkan personil (Sukon / Karyawan) dengan multi-SIMPER.
*/
public function test_admin_hse_can_assign_multi_simper_to_employee(): void
{
$typeLv = SimperType::where('code', 'SIMPER-LV')->first();
$typeDt = SimperType::where('code', 'SIMPER-DT')->first();
$data = [
'nik' => 'SKN-9901',
'name' => 'Bambang Sukon DT',
'employee_type' => 'Sukon',
'unit_kerja' => 'PT Mandiri Sukon Perkasa',
'position' => 'Driver Dump Truck',
'division' => 'Hauling Operation',
'is_operator' => '1',
'simpers' => [
$typeLv->id => [
'simper_number' => 'SMP-LV-001',
'expired_at' => Carbon::now()->addYear()->format('Y-m-d'),
],
$typeDt->id => [
'simper_number' => 'SMP-DT-001',
'expired_at' => Carbon::now()->addMonths(6)->format('Y-m-d'),
],
],
];
$response = $this->actingAs($this->adminHse)
->from(route('hse.employees.index'))
->post(route('hse.employees.store'), $data);
$response->assertRedirect(route('hse.employees.index'));
$response->assertSessionHas('success');
$emp = Employee::where('nik', 'SKN-9901')->first();
$this->assertNotNull($emp);
$this->assertTrue($emp->isSukon());
$this->assertEquals(2, $emp->simpers()->count());
$this->assertTrue($emp->hasValidSimperForType('SIMPER-LV'));
$this->assertTrue($emp->hasValidSimperForType('SIMPER-DT'));
$this->assertFalse($emp->hasValidSimperForType('SIMPER-HEAVY'));
}
}
@@ -0,0 +1,203 @@
<?php
namespace Tests\Feature;
use App\Models\Employee;
use App\Models\EmployeeSimper;
use App\Models\Gate;
use App\Models\SimperType;
use App\Models\User;
use App\Models\Vehicle;
use App\Models\VehicleActivity;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class VehicleActivitySimperValidationTest extends TestCase
{
use RefreshDatabase;
protected User $driverUser;
protected Employee $driverLvOnly;
protected Employee $driverDtOnly;
protected Vehicle $lightVehicle;
protected Vehicle $heavyMobileUnit;
protected Gate $gate;
protected SimperType $simperLv;
protected SimperType $simperDt;
protected function setUp(): void
{
parent::setUp();
$this->simperLv = SimperType::where('code', 'SIMPER-LV')->first();
$this->simperDt = SimperType::where('code', 'SIMPER-DT')->first();
$this->gate = Gate::create([
'code' => 'GATE-01',
'name' => 'Pos Gerbang Utama KMH',
'location' => 'Main Gate Site',
'is_active' => true,
]);
$this->lightVehicle = Vehicle::create([
'code' => 'LV-01',
'category' => 'light_vehicle',
'brand_type' => 'Toyota Hilux 4x4 Double Cabin',
'license_plate' => 'BH 1234 KM',
'status' => 'Operation',
'current_km' => 10000.00,
]);
$this->heavyMobileUnit = Vehicle::create([
'code' => 'DT-01',
'category' => 'unit_support',
'brand_type' => 'Hino 500 Dump Truck',
'license_plate' => 'BH 5678 DT',
'status' => 'Operation',
'current_km' => 5000.00,
'is_timesheet_mobile' => true,
'required_simper_type_id' => $this->simperDt->id,
]);
// Driver 1: Hanya punya SIMPER LV
$this->driverLvOnly = Employee::create([
'nik' => 'DRV-LV-01',
'name' => 'Andi Driver LV',
'position' => 'Driver Operasional',
'division' => 'Logistik',
'is_operator' => true,
'simper_expired_at' => Carbon::now()->addYear(),
]);
EmployeeSimper::create([
'employee_id' => $this->driverLvOnly->id,
'simper_type_id' => $this->simperLv->id,
'simper_number' => 'SMP-LV-001',
'expired_at' => Carbon::now()->addYear(),
'is_active' => true,
]);
// Driver 2: Sukon dengan SIMPER DT
$this->driverDtOnly = Employee::create([
'nik' => 'SKN-DT-01',
'name' => 'Budi Sukon DT',
'employee_type' => 'Sukon',
'position' => 'Driver Dump Truck',
'division' => 'Hauling',
'is_operator' => true,
'simper_expired_at' => Carbon::now()->addYear(),
]);
EmployeeSimper::create([
'employee_id' => $this->driverDtOnly->id,
'simper_type_id' => $this->simperDt->id,
'simper_number' => 'SMP-DT-001',
'expired_at' => Carbon::now()->addYear(),
'is_active' => true,
]);
// Akun login untuk pengujian form
$this->driverUser = User::create([
'name' => 'Andi Driver LV',
'username' => 'driver_andi',
'email' => '[email protected]',
'role' => 'employee',
'password' => bcrypt('password'),
]);
$this->driverLvOnly->update(['user_id' => $this->driverUser->id]);
}
/**
* Unit heavy mobile muncul di katalog /timesheet.
*/
public function test_timesheet_catalog_shows_both_lv_and_heavy_mobile(): void
{
$response = $this->actingAs($this->driverUser)->get(route('mobilisasi.index'));
$response->assertStatus(200);
$response->assertSee('LV-01');
$response->assertSee('DT-01');
$response->assertSee('Heavy Equipment Mobile');
}
/**
* Driver LV dapat mengakses form mobilisasi LV.
*/
public function test_driver_lv_can_access_lv_form(): void
{
$response = $this->actingAs($this->driverUser)
->get(route('vehicle.activity.form', ['code' => 'LV-01']));
$response->assertStatus(200);
$response->assertSee('LV-01');
$response->assertDontSee('Izin SIMPER Tidak Sesuai');
}
/**
* Driver yang hanya punya SIMPER LV ditolak saat mengakses unit Dump Truck (SIMPER-DT).
*/
public function test_driver_lv_only_is_rejected_on_dump_truck(): void
{
$response = $this->actingAs($this->driverUser)
->get(route('vehicle.activity.form', ['code' => 'DT-01']));
$response->assertStatus(200);
$response->assertSee('Izin SIMPER Tidak Sesuai');
$response->assertSee('SIMPER Dump Truck');
}
/**
* Cek NIK AJAX menolak Driver LV pada unit Dump Truck.
*/
public function test_ajax_check_nik_rejects_mismatched_simper(): void
{
$response = $this->actingAs($this->driverUser)
->postJson(route('vehicle.activity.check.nik', ['code' => 'DT-01']), [
'nik' => $this->driverLvOnly->nik,
]);
$response->assertStatus(422);
$response->assertJsonPath('status', 'expired');
$response->assertJsonPath('title', 'Izin SIMPER Tidak Sesuai');
}
/**
* Cek NIK AJAX menerima Driver Sukon DT pada unit Dump Truck.
*/
public function test_ajax_check_nik_accepts_valid_dt_driver(): void
{
$response = $this->actingAs($this->driverUser)
->postJson(route('vehicle.activity.check.nik', ['code' => 'DT-01']), [
'nik' => $this->driverDtOnly->nik,
]);
$response->assertStatus(200);
$response->assertJsonPath('status', 'valid');
$response->assertJsonPath('title', 'SIMPER Valid');
}
/**
* Submit timesheet mobilisasi Dump Truck berhasil bila pengemudi memiliki SIMPER DT.
*/
public function test_submit_activity_succeeds_for_authorized_dt_driver(): void
{
$postData = [
'driver_id' => $this->driverDtOnly->id,
'gate_id' => $this->gate->id,
'direction' => 'out',
'current_km' => 5050.00,
'purpose' => 'Mobilisasi angkut material agregat ke area powerhouse',
];
$response = $this->actingAs($this->driverUser)
->post(route('vehicle.activity.store', ['code' => 'DT-01']), $postData);
$response->assertRedirect(route('employee.dashboard'));
$response->assertSessionHas('success');
$this->assertDatabaseHas('vehicle_activities', [
'vehicle_id' => $this->heavyMobileUnit->id,
'driver_id' => $this->driverDtOnly->id,
'direction' => 'out',
'current_km' => 5050.00,
]);
}
}