fix(hse): allow soft deleting employee without losing operational history, fix simper checklist badge overflow, and enforce valid future expiry date

This commit is contained in:
Ray1111-aa
2026-11-02 14:35:24 +07:00
parent 23377631b3
commit 008e5f6b9d
12 changed files with 302 additions and 58 deletions
@@ -256,7 +256,7 @@ class HseEmployeeFlexibleUnitKerjaAndModalTest extends TestCase
$this->assertSoftDeleted('users', ['id' => $linkedUser->id]);
}
public function test_cannot_delete_employee_with_operational_records(): void
public function test_can_delete_employee_with_operational_records_and_preserves_history(): void
{
$superAdmin = User::factory()->create([
'username' => 'superadmin_check',
@@ -290,7 +290,7 @@ class HseEmployeeFlexibleUnitKerjaAndModalTest extends TestCase
'current_km' => 1000,
]);
\App\Models\VehicleActivity::create([
$activity = \App\Models\VehicleActivity::create([
'transaction_no' => 'TRX-TEST-001',
'vehicle_id' => $vehicle->id,
'driver_id' => $employee->id,
@@ -309,8 +309,13 @@ class HseEmployeeFlexibleUnitKerjaAndModalTest extends TestCase
->from(route('hse.employees.index'))
->delete(route('hse.employees.destroy', $employee->id));
$response->assertSessionHas('error');
$this->assertDatabaseHas('employees', ['id' => $employee->id]);
$response->assertSessionHas('success');
$this->assertSoftDeleted('employees', ['id' => $employee->id]);
// Pastikan riwayat operasional tetap terhubung via withTrashed()
$activityFresh = \App\Models\VehicleActivity::find($activity->id);
$this->assertNotNull($activityFresh->driver);
$this->assertEquals($employee->name, $activityFresh->driver->name);
}
public function test_hse_edit_modal_and_edit_page_render_division_and_position_inputs(): void
+127
View File
@@ -425,4 +425,131 @@ class HseSimperTypeTest extends TestCase
$this->assertSoftDeleted('simper_types', ['id' => $simperType->id]);
$this->assertSoftDeleted('employee_simpers', ['id' => $empSimper->id]);
}
/**
* Mencentang SIMPER tanpa memasukkan tanggal masa berlaku ditolak dengan pesan error.
*/
public function test_checking_simper_without_expiry_date_fails_validation(): void
{
$type = SimperType::create([
'code' => 'SIMPER-DT-TEST',
'name' => 'SIMPER Dump Truck',
'category_scope' => 'all',
'is_active' => true,
]);
$employee = Employee::create([
'nik' => 'EMP-EXP-VALIDATE-01',
'name' => 'Operator Test',
'is_operator' => true,
'unit_kerja' => 'Pembangkit',
'division' => 'Operasi',
'position' => 'Operator',
]);
$response = $this->actingAs($this->adminHse)
->post(route('hse.employees.update', $employee->id), [
'nik' => $employee->nik,
'name' => $employee->name,
'is_operator' => 1,
'unit_kerja' => 'Pembangkit',
'division' => 'Operasi',
'position' => 'Operator',
'simper_assignments' => [
$type->id => [
'enabled' => 1,
'expired_at' => '',
],
],
]);
$response->assertSessionHas('error');
}
/**
* Mencentang SIMPER dengan tanggal masa berlaku di masa lampau ditolak.
*/
public function test_checking_simper_with_past_expiry_date_fails_validation(): void
{
$type = SimperType::create([
'code' => 'SIMPER-DT-PAST',
'name' => 'SIMPER Dump Truck',
'category_scope' => 'all',
'is_active' => true,
]);
$employee = Employee::create([
'nik' => 'EMP-EXP-VALIDATE-02',
'name' => 'Operator Test Past',
'is_operator' => true,
'unit_kerja' => 'Pembangkit',
'division' => 'Operasi',
'position' => 'Operator',
]);
$response = $this->actingAs($this->adminHse)
->post(route('hse.employees.update', $employee->id), [
'nik' => $employee->nik,
'name' => $employee->name,
'is_operator' => 1,
'unit_kerja' => 'Pembangkit',
'division' => 'Operasi',
'position' => 'Operator',
'simper_assignments' => [
$type->id => [
'enabled' => 1,
'expired_at' => Carbon::yesterday()->toDateString(),
],
],
]);
$response->assertSessionHas('error');
}
/**
* Mencentang SIMPER dengan tanggal masa depan berhasil disimpan.
*/
public function test_checking_simper_with_future_expiry_date_succeeds(): void
{
$type = SimperType::create([
'code' => 'SIMPER-DT-OK',
'name' => 'SIMPER Dump Truck OK',
'category_scope' => 'all',
'is_active' => true,
]);
$employee = Employee::create([
'nik' => 'EMP-EXP-VALIDATE-03',
'name' => 'Operator Test Future',
'is_operator' => true,
'unit_kerja' => 'Pembangkit',
'division' => 'Operasi',
'position' => 'Operator',
]);
$futureDate = Carbon::now()->addMonths(6)->toDateString();
$response = $this->actingAs($this->adminHse)
->post(route('hse.employees.update', $employee->id), [
'nik' => $employee->nik,
'name' => $employee->name,
'is_operator' => 1,
'unit_kerja' => 'Pembangkit',
'division' => 'Operasi',
'position' => 'Operator',
'simper_assignments' => [
$type->id => [
'enabled' => 1,
'expired_at' => $futureDate,
],
],
]);
$response->assertSessionHas('success');
$this->assertDatabaseHas('employee_simpers', [
'employee_id' => $employee->id,
'simper_type_id' => $type->id,
'expired_at' => $futureDate . ' 00:00:00',
]);
}
}