feat: setup Docker production stack, Vite asset compilation, and resolve audit findings (P1/P2)
This commit is contained in:
@@ -140,30 +140,43 @@ class AuthenticationTest extends TestCase
|
||||
}
|
||||
|
||||
/**
|
||||
* Memastikan Admin HSE dapat mengedit data karyawan & SIMPER.
|
||||
* Memastikan Admin HSE hanya mengelola status driver & SIMPER tanpa mengubah master profil karyawan.
|
||||
*/
|
||||
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('Nama Karyawan Diperbarui', $employee->name);
|
||||
$this->assertEquals('Senior Technician', $employee->position);
|
||||
$this->assertEquals($originalName, $employee->name);
|
||||
$this->assertEquals($originalPosition, $employee->position);
|
||||
$this->assertTrue((bool) $employee->is_operator);
|
||||
$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.
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,696 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\Employee;
|
||||
use App\Models\P2hRecord;
|
||||
use App\Models\P2hTemplate;
|
||||
use App\Models\User;
|
||||
use App\Models\Vehicle;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Tests\TestCase;
|
||||
|
||||
class P2hInspectionTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
protected User $qmaAdmin;
|
||||
protected User $operatorUser;
|
||||
protected Employee $operatorEmp;
|
||||
protected Vehicle $supportVehicle;
|
||||
protected P2hTemplate $template;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
// 1. Akun Admin QMA
|
||||
$this->qmaAdmin = User::firstOrCreate(
|
||||
['username' => '1171'],
|
||||
[
|
||||
'name' => 'Pinto Yuliandra',
|
||||
'email' => '[email protected]',
|
||||
'password' => Hash::make('password'),
|
||||
'role' => 'qma_admin',
|
||||
]
|
||||
);
|
||||
|
||||
// 2. Akun Operator Karyawan dengan SIMPER Aktif
|
||||
$this->operatorUser = User::firstOrCreate(
|
||||
['username' => 'OPR-001'],
|
||||
[
|
||||
'name' => 'Bambang Operator',
|
||||
'email' => '[email protected]',
|
||||
'password' => Hash::make('password'),
|
||||
'role' => 'employee',
|
||||
]
|
||||
);
|
||||
|
||||
$this->operatorEmp = Employee::firstOrCreate(
|
||||
['nik' => 'OPR-001'],
|
||||
[
|
||||
'user_id' => $this->operatorUser->id,
|
||||
'name' => 'Bambang Operator',
|
||||
'unit_kerja' => 'Pembangkit',
|
||||
'division' => 'Operasional',
|
||||
'position' => 'Operator Excavator',
|
||||
'is_operator' => true,
|
||||
'simper_expired_at' => Carbon::now()->addMonths(6),
|
||||
]
|
||||
);
|
||||
|
||||
// 3. Template & Unit Support
|
||||
$this->template = P2hTemplate::firstOrCreate(
|
||||
['code' => 'EXD4RP'],
|
||||
[
|
||||
'name' => 'Excavator',
|
||||
'file_name' => 'P2H_Excavator_EXD4RP.md',
|
||||
'is_active' => true,
|
||||
]
|
||||
);
|
||||
|
||||
$this->supportVehicle = Vehicle::firstOrCreate(
|
||||
['code' => 'EX99'],
|
||||
[
|
||||
'category' => 'unit_support',
|
||||
'p2h_template_id' => $this->template->id,
|
||||
'brand_type' => 'KOMATSU PC200 TEST',
|
||||
'unit_category' => 'Excavator',
|
||||
'status' => 'Operation',
|
||||
'current_km' => 1500.0,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function test_guest_cannot_access_p2h_catalog(): void
|
||||
{
|
||||
$response = $this->get(route('p2h.index'));
|
||||
$response->assertRedirect(route('login'));
|
||||
}
|
||||
|
||||
public function test_authenticated_operator_can_view_p2h_catalog(): void
|
||||
{
|
||||
$response = $this->actingAs($this->operatorUser)->get(route('p2h.index'));
|
||||
$response->assertStatus(200);
|
||||
$response->assertSee('Katalog Pemeriksaan Pra-Harian (P2H)');
|
||||
$response->assertSee($this->supportVehicle->code);
|
||||
}
|
||||
|
||||
public function test_all_28_p2h_categories_are_seeded_and_displayed(): void
|
||||
{
|
||||
$this->seed(\Database\Seeders\P2hTemplateSeeder::class);
|
||||
$this->assertEquals(28, P2hTemplate::count());
|
||||
$this->assertGreaterThanOrEqual(28, Vehicle::where('category', 'unit_support')->count());
|
||||
|
||||
$response = $this->actingAs($this->operatorUser)->get(route('p2h.index'));
|
||||
$response->assertStatus(200);
|
||||
$response->assertSee('Boomlift');
|
||||
$response->assertSee('Excavator');
|
||||
$response->assertSee('Dump Truck');
|
||||
$response->assertSee('Wheel Loader');
|
||||
$response->assertSee('Water Truck');
|
||||
$response->assertSee('Vibro');
|
||||
}
|
||||
|
||||
public function test_operator_can_open_p2h_form(): void
|
||||
{
|
||||
$response = $this->actingAs($this->operatorUser)->get(route('p2h.form', ['code' => $this->supportVehicle->code]));
|
||||
$response->assertStatus(200);
|
||||
$response->assertSee('Pemeriksaan Pra-Harian (P2H) Unit Support');
|
||||
$response->assertSee($this->supportVehicle->code);
|
||||
$response->assertSee('Bambang Operator');
|
||||
$response->assertSee('SIMPER Aktif');
|
||||
$response->assertDontSee('<aside', false);
|
||||
$response->assertDontSee('userProfileDropdownBtn');
|
||||
$response->assertDontSee('openDrawerBtn');
|
||||
|
||||
// Pastikan saat admin (seperti admin QMA) membuka form, sidebar dan topbar juga disembunyikan
|
||||
$admin = User::where('role', 'qma_admin')->first();
|
||||
$resAdmin = $this->actingAs($admin)->get(route('p2h.form', ['code' => $this->supportVehicle->code]));
|
||||
$resAdmin->assertStatus(200);
|
||||
$resAdmin->assertSee('Admin QMA (Validator / Verifikator)');
|
||||
$resAdmin->assertSee('Buka Verifikasi');
|
||||
$resAdmin->assertDontSee('<aside', false);
|
||||
$resAdmin->assertDontSee('userProfileDropdownBtn');
|
||||
$resAdmin->assertDontSee('openDrawerBtn');
|
||||
}
|
||||
|
||||
public function test_qma_admin_cannot_submit_p2h(): void
|
||||
{
|
||||
$admin = User::where('role', 'qma_admin')->first();
|
||||
$response = $this->actingAs($admin)->post(route('p2h.store', ['code' => $this->supportVehicle->code]), [
|
||||
'template_id' => $this->template->id,
|
||||
'start_hm' => 1500,
|
||||
'items' => [],
|
||||
]);
|
||||
|
||||
$response->assertSessionHas('error');
|
||||
}
|
||||
|
||||
public function test_operator_can_submit_p2h_with_pending_status_for_qma(): void
|
||||
{
|
||||
// Pastikan ada beberapa item template
|
||||
$item1 = $this->template->items()->create([
|
||||
'section' => 'Sebelum Mesin Hidup',
|
||||
'title' => 'Periksa Level & Kondisi Oli Engine',
|
||||
'question_type' => 'choice',
|
||||
'sequence' => 1,
|
||||
]);
|
||||
|
||||
$item2 = $this->template->items()->create([
|
||||
'section' => 'Sebelum Mesin Hidup',
|
||||
'title' => 'Periksa Level Air Radiator',
|
||||
'question_type' => 'choice',
|
||||
'sequence' => 2,
|
||||
]);
|
||||
|
||||
$payload = [
|
||||
'template_id' => $this->template->id,
|
||||
'shift' => 'Shift 1 (Siang)',
|
||||
'start_hm' => 1520.5,
|
||||
'general_notes' => 'Unit bersih siap bekerja di disposal.',
|
||||
'items' => [
|
||||
$item1->id => [
|
||||
'condition' => 'baik',
|
||||
],
|
||||
$item2->id => [
|
||||
'condition' => 'rusak',
|
||||
'notes' => 'Air radiator sedikit bocor di sambungan klem',
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$response = $this->actingAs($this->operatorUser)->post(
|
||||
route('p2h.store', ['code' => $this->supportVehicle->code]),
|
||||
$payload
|
||||
);
|
||||
|
||||
$response->assertRedirect(route('p2h.form', ['code' => $this->supportVehicle->code]));
|
||||
$response->assertSessionHas('success');
|
||||
|
||||
// Cek record P2H tersimpan dengan status 'pending'
|
||||
$this->assertDatabaseHas('p2h_records', [
|
||||
'vehicle_id' => $this->supportVehicle->id,
|
||||
'employee_id' => $this->operatorEmp->id,
|
||||
'shift' => 'Shift 1 (Siang)',
|
||||
'start_hm' => 1520.5,
|
||||
'summary_condition' => 'has_defect',
|
||||
'defect_count' => 1,
|
||||
'validation_status' => 'pending',
|
||||
]);
|
||||
|
||||
// Cek HM kendaraan ter-update
|
||||
$this->supportVehicle->refresh();
|
||||
$this->assertEquals(1520.5, (float)$this->supportVehicle->current_km);
|
||||
}
|
||||
|
||||
public function test_qma_admin_can_view_and_approve_p2h_record(): void
|
||||
{
|
||||
$record = P2hRecord::create([
|
||||
'transaction_no' => 'P2H-TEST-001',
|
||||
'vehicle_id' => $this->supportVehicle->id,
|
||||
'employee_id' => $this->operatorEmp->id,
|
||||
'template_id' => $this->template->id,
|
||||
'shift' => 'Shift 1 (Siang)',
|
||||
'start_hm' => 1530.0,
|
||||
'summary_condition' => 'good',
|
||||
'defect_count' => 0,
|
||||
'validation_status' => 'pending',
|
||||
'inspected_at' => Carbon::now(),
|
||||
]);
|
||||
|
||||
// QMA melihat index
|
||||
$resIndex = $this->actingAs($this->qmaAdmin)->get(route('qma.p2h.index'));
|
||||
$resIndex->assertStatus(200);
|
||||
$resIndex->assertSee('P2H-TEST-001');
|
||||
|
||||
// QMA melihat detail
|
||||
$resShow = $this->actingAs($this->qmaAdmin)->get(route('qma.p2h.show', $record->id));
|
||||
$resShow->assertStatus(200);
|
||||
$resShow->assertSee('P2H-TEST-001');
|
||||
|
||||
// QMA menyetujui (Approve)
|
||||
$resApprove = $this->actingAs($this->qmaAdmin)->post(
|
||||
route('qma.p2h.approve', $record->id),
|
||||
['validation_notes' => 'P2H disetujui, unit laik operasi.']
|
||||
);
|
||||
|
||||
$resApprove->assertSessionHas('success');
|
||||
$record->refresh();
|
||||
$this->assertEquals('approved', $record->validation_status);
|
||||
$this->assertEquals($this->qmaAdmin->id, $record->validated_by);
|
||||
}
|
||||
|
||||
public function test_qma_admin_can_reject_p2h_record(): void
|
||||
{
|
||||
$record = P2hRecord::create([
|
||||
'transaction_no' => 'P2H-TEST-002',
|
||||
'vehicle_id' => $this->supportVehicle->id,
|
||||
'employee_id' => $this->operatorEmp->id,
|
||||
'template_id' => $this->template->id,
|
||||
'shift' => 'Shift 2 (Malam)',
|
||||
'start_hm' => 1540.0,
|
||||
'summary_condition' => 'has_defect',
|
||||
'defect_count' => 2,
|
||||
'validation_status' => 'pending',
|
||||
'inspected_at' => Carbon::now(),
|
||||
]);
|
||||
|
||||
// QMA menolak (Reject)
|
||||
$resReject = $this->actingAs($this->qmaAdmin)->post(
|
||||
route('qma.p2h.reject', $record->id),
|
||||
['validation_notes' => 'Unit dilarang beroperasi sebelum rem hidrolik diperbaiki mekanik.']
|
||||
);
|
||||
|
||||
$resReject->assertSessionHas('success');
|
||||
$record->refresh();
|
||||
$this->assertEquals('rejected', $record->validation_status);
|
||||
$this->assertEquals($this->qmaAdmin->id, $record->validated_by);
|
||||
|
||||
// Status unit otomatis menjadi Breakdown
|
||||
$this->supportVehicle->refresh();
|
||||
$this->assertEquals('Breakdown', $this->supportVehicle->status);
|
||||
}
|
||||
|
||||
public function test_qma_admin_can_view_p2h_templates(): void
|
||||
{
|
||||
$response = $this->actingAs($this->qmaAdmin)->get(route('qma.p2h.templates'));
|
||||
$response->assertStatus(200);
|
||||
$response->assertSee('Master Template P2H Unit Support');
|
||||
$response->assertSee('Excavator');
|
||||
$response->assertSee('EXD4RP');
|
||||
}
|
||||
|
||||
public function test_adding_unit_support_category_automatically_creates_empty_p2h_template(): void
|
||||
{
|
||||
// 1. Admin QMA menambah kategori aset Unit Support baru
|
||||
$storeResponse = $this->actingAs($this->qmaAdmin)->post('/qma/vehicles', [
|
||||
'code' => 'TH01',
|
||||
'category' => 'unit_support',
|
||||
'brand_type' => 'Manitou MT-X 733',
|
||||
'unit_category' => 'Telehandler',
|
||||
'status' => 'Operation',
|
||||
'current_km' => 0,
|
||||
]);
|
||||
|
||||
$storeResponse->assertSessionHas('success');
|
||||
|
||||
// 2. Pastikan Kategori Aset terbuat dan terhubung ke template
|
||||
$category = Vehicle::where('code', 'TH01')->first();
|
||||
$this->assertNotNull($category);
|
||||
$this->assertNotNull($category->p2h_template_id);
|
||||
|
||||
// 3. Pastikan Master Template P2H otomatis terbuat dengan default kosong (0 butir pertanyaan)
|
||||
$template = P2hTemplate::where('code', 'TH01')->first();
|
||||
$this->assertNotNull($template);
|
||||
$this->assertEquals('Telehandler', $template->name);
|
||||
$this->assertEquals(0, $template->items()->count());
|
||||
$this->assertEquals($category->p2h_template_id, $template->id);
|
||||
|
||||
// 4. Halaman Master Template P2H menampilkan card template baru tersebut
|
||||
$listResponse = $this->actingAs($this->qmaAdmin)->get(route('qma.p2h.templates'));
|
||||
$listResponse->assertStatus(200);
|
||||
$listResponse->assertSee('TH01');
|
||||
$listResponse->assertSee('Telehandler');
|
||||
$listResponse->assertSee('Template Baru (Kosong)');
|
||||
$listResponse->assertSee('Kelola & Isi Pertanyaan');
|
||||
|
||||
// 5. Update kategori menyinkronkan kode template P2H
|
||||
$updateResponse = $this->actingAs($this->qmaAdmin)->post('/qma/vehicles/' . $category->id, [
|
||||
'code' => 'TH99',
|
||||
'category' => 'unit_support',
|
||||
'brand_type' => 'Manitou MT-X 733 Heavy Duty',
|
||||
'unit_category' => 'Telehandler HD',
|
||||
'status' => 'Operation',
|
||||
'current_km' => 10,
|
||||
]);
|
||||
$updateResponse->assertSessionHas('success');
|
||||
|
||||
$this->assertDatabaseHas('p2h_templates', [
|
||||
'id' => $template->id,
|
||||
'code' => 'TH99',
|
||||
'name' => 'Telehandler HD',
|
||||
]);
|
||||
|
||||
// 6. Hapus kategori aset juga menghapus master template yang belum ada transaksi
|
||||
$deleteResponse = $this->actingAs($this->qmaAdmin)->delete('/qma/vehicles/' . $category->id);
|
||||
$deleteResponse->assertSessionHas('success');
|
||||
$this->assertDatabaseMissing('p2h_templates', ['id' => $template->id]);
|
||||
}
|
||||
|
||||
public function test_qma_admin_can_edit_p2h_template_question(): void
|
||||
{
|
||||
$item = \App\Models\P2hTemplateItem::create([
|
||||
'template_id' => $this->template->id,
|
||||
'section' => 'Pemeriksaan Keliling',
|
||||
'title' => 'Level Oli Hidrolik',
|
||||
'question_type' => 'condition',
|
||||
'sequence' => 1,
|
||||
]);
|
||||
|
||||
// Akses halaman detail template
|
||||
$response = $this->actingAs($this->qmaAdmin)->get(route('qma.p2h.templates.show', $this->template->id));
|
||||
$response->assertStatus(200);
|
||||
$response->assertSee('Edit');
|
||||
$response->assertSee('Tambah Pertanyaan');
|
||||
$response->assertSee($item->title);
|
||||
|
||||
// Edit butir pertanyaan
|
||||
$editResponse = $this->actingAs($this->qmaAdmin)->put(
|
||||
route('qma.p2h.templates.items.update', ['templateId' => $this->template->id, 'itemId' => $item->id]),
|
||||
[
|
||||
'title' => 'Level Oli Hidrolik dan Kebocoran Selang (Diperbarui)',
|
||||
'section' => 'Sistem Hidrolik',
|
||||
'question_type' => 'Multiple choice: only one answer',
|
||||
'sequence' => 1,
|
||||
]
|
||||
);
|
||||
|
||||
$editResponse->assertRedirect(route('qma.p2h.templates.show', $this->template->id));
|
||||
$editResponse->assertSessionHas('success');
|
||||
|
||||
$this->assertDatabaseHas('p2h_template_items', [
|
||||
'id' => $item->id,
|
||||
'title' => 'Level Oli Hidrolik dan Kebocoran Selang (Diperbarui)',
|
||||
'section' => 'Sistem Hidrolik',
|
||||
'question_type' => 'Multiple choice: only one answer',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_qma_admin_can_view_two_column_section_layout_for_p2h_template_questions(): void
|
||||
{
|
||||
$this->template->items()->delete();
|
||||
|
||||
$this->template->items()->createMany([
|
||||
[
|
||||
'section' => 'START',
|
||||
'title' => 'Start Engine',
|
||||
'question_type' => 'Datetime',
|
||||
'sequence' => 1,
|
||||
],
|
||||
[
|
||||
'section' => 'START',
|
||||
'title' => 'Shift Operator',
|
||||
'question_type' => 'Multiple choice: only one answer',
|
||||
'sequence' => 2,
|
||||
],
|
||||
[
|
||||
'section' => 'ATTACHMENT',
|
||||
'title' => 'Basket / Keranjang (enginee)',
|
||||
'question_type' => 'Multiple choice: only one answer',
|
||||
'sequence' => 3,
|
||||
],
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($this->qmaAdmin)->get(route('qma.p2h.templates.show', $this->template->id));
|
||||
$response->assertStatus(200);
|
||||
$response->assertSee('sectionsContainer');
|
||||
$response->assertSee('section-card');
|
||||
$response->assertSee('section-content');
|
||||
$response->assertSee('START');
|
||||
$response->assertSee('ATTACHMENT');
|
||||
$response->assertSee('2 Pertanyaan');
|
||||
$response->assertSee('1 Pertanyaan');
|
||||
$response->assertSee('Basket / Keranjang (enginee)');
|
||||
$response->assertSee('addItemSectionContainer');
|
||||
$response->assertSee('addItemSectionBadge');
|
||||
}
|
||||
|
||||
public function test_authenticated_operator_can_view_dashboard_p2h_and_timesheet_menus(): void
|
||||
{
|
||||
// Pastikan ada kendaraan ringan
|
||||
Vehicle::firstOrCreate(
|
||||
['code' => 'LV99'],
|
||||
[
|
||||
'brand_type' => 'Toyota Hilux Double Cabin',
|
||||
'license_plate' => 'BH 9999 KM',
|
||||
'category' => 'light_vehicle',
|
||||
'current_km' => 5400,
|
||||
'ownership' => 'Operasional Site',
|
||||
'status' => 'Operation',
|
||||
]
|
||||
);
|
||||
|
||||
// 1. Login redirect ke dashboard karyawan
|
||||
$resLogin = $this->post('/login', [
|
||||
'username' => 'OPR-001',
|
||||
'password' => 'password',
|
||||
]);
|
||||
$resLogin->assertRedirect(route('employee.dashboard'));
|
||||
|
||||
// 2. Halaman Dashboard Karyawan menampilkan 2 card fitur utama
|
||||
$resDashboard = $this->actingAs($this->operatorUser)->get(route('employee.dashboard'));
|
||||
$resDashboard->assertStatus(200);
|
||||
$resDashboard->assertSee('Dashboard Karyawan');
|
||||
$resDashboard->assertSee('Buka Modul Pemeriksaan P2H');
|
||||
$resDashboard->assertSee('Buka Modul Timesheet LV');
|
||||
$resDashboard->assertSee('Pemeriksaan P2H');
|
||||
$resDashboard->assertSee('Timesheet');
|
||||
|
||||
// 3. Sidebar di halaman P2H memuat menu Dashboard, Pemeriksaan P2H, dan Timesheet
|
||||
$resCatalog = $this->actingAs($this->operatorUser)->get(route('p2h.index'));
|
||||
$resCatalog->assertStatus(200);
|
||||
$resCatalog->assertSee('Dashboard');
|
||||
$resCatalog->assertSee('Pemeriksaan P2H');
|
||||
$resCatalog->assertSee('Timesheet');
|
||||
$resCatalog->assertSee(route('employee.dashboard'));
|
||||
$resCatalog->assertSee(route('p2h.index'));
|
||||
$resCatalog->assertSee(route('timesheet.index'));
|
||||
|
||||
// 4. Akses langsung halaman Timesheet
|
||||
$resTimesheet = $this->actingAs($this->operatorUser)->get(route('timesheet.index'));
|
||||
$resTimesheet->assertStatus(200);
|
||||
$resTimesheet->assertSee('Timesheet Kendaraan Ringan');
|
||||
$resTimesheet->assertSee('LV99');
|
||||
$resTimesheet->assertSee('BH 9999 KM');
|
||||
$resTimesheet->assertSee('Isi Form Mobilisasi LV');
|
||||
}
|
||||
|
||||
public function test_qma_admin_can_add_section_with_multiple_questions_and_types_attached(): void
|
||||
{
|
||||
$response = $this->actingAs($this->qmaAdmin)->post(
|
||||
route('qma.p2h.templates.items.store', $this->template->id),
|
||||
[
|
||||
'section' => 'Pemeriksaan Ruang Kemudi (Cabin)',
|
||||
'questions' => [
|
||||
[
|
||||
'title' => 'Kondisi kaca spion kanan & kiri',
|
||||
'question_type' => 'Multiple choice: only one answer',
|
||||
],
|
||||
[
|
||||
'title' => 'Catatan kelainan instrumen dashboard',
|
||||
'question_type' => 'Multiple Lines Text Box',
|
||||
],
|
||||
[
|
||||
'title' => 'Jumlah sabuk pengaman yang terpasang baik',
|
||||
'question_type' => 'Numerical Value',
|
||||
],
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
$response->assertRedirect(route('qma.p2h.templates.show', $this->template->id));
|
||||
$response->assertSessionHas('success');
|
||||
|
||||
$this->assertDatabaseHas('p2h_template_items', [
|
||||
'template_id' => $this->template->id,
|
||||
'section' => 'Pemeriksaan Ruang Kemudi (Cabin)',
|
||||
'title' => 'Kondisi kaca spion kanan & kiri',
|
||||
'question_type' => 'Multiple choice: only one answer',
|
||||
'sequence' => 1,
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('p2h_template_items', [
|
||||
'template_id' => $this->template->id,
|
||||
'section' => 'Pemeriksaan Ruang Kemudi (Cabin)',
|
||||
'title' => 'Catatan kelainan instrumen dashboard',
|
||||
'question_type' => 'Multiple Lines Text Box',
|
||||
'sequence' => 2,
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('p2h_template_items', [
|
||||
'template_id' => $this->template->id,
|
||||
'section' => 'Pemeriksaan Ruang Kemudi (Cabin)',
|
||||
'title' => 'Jumlah sabuk pengaman yang terpasang baik',
|
||||
'question_type' => 'Numerical Value',
|
||||
'sequence' => 3,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
public function test_qma_admin_template_detail_renders_add_section_modal(): void
|
||||
{
|
||||
$response = $this->actingAs($this->qmaAdmin)->get(route('qma.p2h.templates.show', $this->template->id));
|
||||
$response->assertStatus(200);
|
||||
$response->assertSee('id="addSectionModal"', false);
|
||||
$response->assertSee('id="sectionQuestionsList"', false);
|
||||
$response->assertSee('openAddSectionModal', false);
|
||||
$response->assertSee('closeAddSectionModal', false);
|
||||
$response->assertSee('addQuestionToSection', false);
|
||||
$response->assertSee('removeQuestionFromSection', false);
|
||||
}
|
||||
|
||||
public function test_qma_admin_can_add_section_before_target_section(): void
|
||||
{
|
||||
// 1. Buat Section A (sequence 1, 2) dan Section C (sequence 3, 4)
|
||||
$this->template->items()->createMany([
|
||||
['section' => 'Section A', 'title' => 'Pertanyaan A1', 'question_type' => 'choice', 'sequence' => 1],
|
||||
['section' => 'Section A', 'title' => 'Pertanyaan A2', 'question_type' => 'choice', 'sequence' => 2],
|
||||
['section' => 'Section C', 'title' => 'Pertanyaan C1', 'question_type' => 'choice', 'sequence' => 3],
|
||||
['section' => 'Section C', 'title' => 'Pertanyaan C2', 'question_type' => 'choice', 'sequence' => 4],
|
||||
]);
|
||||
|
||||
// 2. Tambah Section B dengan posisi 'before' Section C
|
||||
$response = $this->actingAs($this->qmaAdmin)->post(
|
||||
route('qma.p2h.templates.items.store', $this->template->id),
|
||||
[
|
||||
'section' => 'Section B',
|
||||
'position_type' => 'before',
|
||||
'target_section' => 'Section C',
|
||||
'questions' => [
|
||||
[
|
||||
'title' => 'Pertanyaan B1',
|
||||
'question_type' => 'choice',
|
||||
],
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
$response->assertRedirect(route('qma.p2h.templates.show', $this->template->id));
|
||||
$response->assertSessionHas('success');
|
||||
|
||||
// Sequence Section B harus berada sebelum Section C
|
||||
$itemB = $this->template->items()->where('section', 'Section B')->first();
|
||||
$itemC1 = $this->template->items()->where('title', 'Pertanyaan C1')->first();
|
||||
$this->assertNotNull($itemB);
|
||||
$this->assertLessThan($itemC1->sequence, $itemB->sequence);
|
||||
$this->assertEquals(3, $itemB->sequence);
|
||||
// Dan sequence C1 tergeser menjadi 4
|
||||
$this->assertEquals(4, $itemC1->sequence);
|
||||
}
|
||||
|
||||
public function test_qma_admin_can_reorder_sections(): void
|
||||
{
|
||||
// 1. Buat 3 Section: Section 1 (seq 1,2), Section 2 (seq 3,4), Section 3 (seq 5,6)
|
||||
$this->template->items()->createMany([
|
||||
['section' => 'Section 1', 'title' => 'Pertanyaan 1A', 'question_type' => 'choice', 'sequence' => 1],
|
||||
['section' => 'Section 1', 'title' => 'Pertanyaan 1B', 'question_type' => 'choice', 'sequence' => 2],
|
||||
['section' => 'Section 2', 'title' => 'Pertanyaan 2A', 'question_type' => 'choice', 'sequence' => 3],
|
||||
['section' => 'Section 2', 'title' => 'Pertanyaan 2B', 'question_type' => 'choice', 'sequence' => 4],
|
||||
['section' => 'Section 3', 'title' => 'Pertanyaan 3A', 'question_type' => 'choice', 'sequence' => 5],
|
||||
['section' => 'Section 3', 'title' => 'Pertanyaan 3B', 'question_type' => 'choice', 'sequence' => 6],
|
||||
]);
|
||||
|
||||
// 2. Reorder menjadi: Section 3, Section 1, Section 2
|
||||
$response = $this->actingAs($this->qmaAdmin)->post(
|
||||
route('qma.p2h.templates.sections.reorder', $this->template->id),
|
||||
[
|
||||
'sections' => ['Section 3', 'Section 1', 'Section 2'],
|
||||
]
|
||||
);
|
||||
|
||||
$response->assertRedirect();
|
||||
$response->assertSessionHas('success');
|
||||
|
||||
// Refresh template items urut sequence
|
||||
$items = $this->template->items()->orderBy('sequence')->get();
|
||||
|
||||
// 2 item pertama harus Section 3
|
||||
$this->assertEquals('Section 3', $items[0]->section);
|
||||
$this->assertEquals(1, $items[0]->sequence);
|
||||
$this->assertEquals('Section 3', $items[1]->section);
|
||||
$this->assertEquals(2, $items[1]->sequence);
|
||||
|
||||
// 2 item berikutnya harus Section 1
|
||||
$this->assertEquals('Section 1', $items[2]->section);
|
||||
$this->assertEquals(3, $items[2]->sequence);
|
||||
$this->assertEquals('Section 1', $items[3]->section);
|
||||
$this->assertEquals(4, $items[3]->sequence);
|
||||
|
||||
// 2 item terakhir harus Section 2
|
||||
$this->assertEquals('Section 2', $items[4]->section);
|
||||
$this->assertEquals(5, $items[4]->sequence);
|
||||
$this->assertEquals('Section 2', $items[5]->section);
|
||||
$this->assertEquals(6, $items[5]->sequence);
|
||||
}
|
||||
|
||||
public function test_qma_admin_template_detail_renders_reorder_sections_modal_and_controls(): void
|
||||
{
|
||||
$this->template->items()->createMany([
|
||||
['section' => 'Section A', 'title' => 'Tanya A', 'question_type' => 'choice', 'sequence' => 1],
|
||||
['section' => 'Section B', 'title' => 'Tanya B', 'question_type' => 'choice', 'sequence' => 2],
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($this->qmaAdmin)->get(route('qma.p2h.templates.show', $this->template->id));
|
||||
$response->assertStatus(200);
|
||||
$response->assertSee('id="reorderSectionsModal"', false);
|
||||
$response->assertSee('openReorderSectionsModal', false);
|
||||
$response->assertSee('closeReorderSectionsModal', false);
|
||||
$response->assertSee('moveSectionItemUp', false);
|
||||
$response->assertSee('moveSectionItemDown', false);
|
||||
$response->assertSee('Atur Urutan Section');
|
||||
}
|
||||
|
||||
public function test_questions_are_automatically_and_contiguously_numbered_across_all_sections(): void
|
||||
{
|
||||
// 1. Buat Bagian Satu (2 pertanyaan) dan Bagian Dua (2 pertanyaan)
|
||||
$this->actingAs($this->qmaAdmin)->post(
|
||||
route('qma.p2h.templates.items.store', $this->template->id),
|
||||
[
|
||||
'section' => 'Bagian Satu',
|
||||
'questions' => [
|
||||
['title' => 'Pertanyaan 1.1', 'question_type' => 'choice'],
|
||||
['title' => 'Pertanyaan 1.2', 'question_type' => 'choice'],
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
$this->actingAs($this->qmaAdmin)->post(
|
||||
route('qma.p2h.templates.items.store', $this->template->id),
|
||||
[
|
||||
'section' => 'Bagian Dua',
|
||||
'questions' => [
|
||||
['title' => 'Pertanyaan 2.1', 'question_type' => 'choice'],
|
||||
['title' => 'Pertanyaan 2.2', 'question_type' => 'choice'],
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
// Pertanyaan harus berurutan 1, 2 di Bagian Satu, dan 3, 4 di Bagian Dua
|
||||
$items = $this->template->items()->orderBy('sequence')->get();
|
||||
$this->assertCount(4, $items);
|
||||
$this->assertEquals(['Pertanyaan 1.1', 'Pertanyaan 1.2', 'Pertanyaan 2.1', 'Pertanyaan 2.2'], $items->pluck('title')->all());
|
||||
$this->assertEquals([1, 2, 3, 4], $items->pluck('sequence')->all());
|
||||
|
||||
// 2. Tambah satu pertanyaan baru ke Bagian Satu melalui form butir tunggal
|
||||
$this->actingAs($this->qmaAdmin)->post(
|
||||
route('qma.p2h.templates.items.store', $this->template->id),
|
||||
[
|
||||
'section' => 'Bagian Satu',
|
||||
'title' => 'Pertanyaan 1.3 Baru',
|
||||
'question_type' => 'choice',
|
||||
]
|
||||
);
|
||||
|
||||
// Pertanyaan baru harus berada di akhir Bagian Satu (nomor 3), dan Bagian Dua bergeser menjadi 4, 5
|
||||
$itemsAfter = $this->template->items()->orderBy('sequence')->get();
|
||||
$this->assertCount(5, $itemsAfter);
|
||||
$this->assertEquals(['Pertanyaan 1.1', 'Pertanyaan 1.2', 'Pertanyaan 1.3 Baru', 'Pertanyaan 2.1', 'Pertanyaan 2.2'], $itemsAfter->pluck('title')->all());
|
||||
$this->assertEquals([1, 2, 3, 4, 5], $itemsAfter->pluck('sequence')->all());
|
||||
|
||||
// 3. Hapus item kedua (Pertanyaan 1.2)
|
||||
$itemToDelete = $itemsAfter->firstWhere('title', 'Pertanyaan 1.2');
|
||||
$this->actingAs($this->qmaAdmin)->delete(
|
||||
route('qma.p2h.templates.items.destroy', ['templateId' => $this->template->id, 'itemId' => $itemToDelete->id])
|
||||
);
|
||||
|
||||
// Seluruh pertanyaan otomatis ternormalisasi kembali tanpa celah: 1, 2, 3, 4
|
||||
$itemsAfterDelete = $this->template->items()->orderBy('sequence')->get();
|
||||
$this->assertCount(4, $itemsAfterDelete);
|
||||
$this->assertEquals([1, 2, 3, 4], $itemsAfterDelete->pluck('sequence')->all());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,362 @@
|
||||
<?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',
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,274 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Models\Vehicle;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class UnitSupportBusinessProcessTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
protected User $qmaAdmin;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->qmaAdmin = User::firstOrCreate(
|
||||
['username' => 'qma_test_admin'],
|
||||
[
|
||||
'name' => 'QMA Tester',
|
||||
'email' => '[email protected]',
|
||||
'role' => 'qma_admin',
|
||||
'password' => bcrypt('password'),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Pastikan halaman daftar kategori unit support menampilkan kategori dengan units_count.
|
||||
*/
|
||||
public function test_qma_admin_can_view_support_categories(): void
|
||||
{
|
||||
$category = Vehicle::firstOrCreate(
|
||||
['code' => 'TEST-CAT'],
|
||||
[
|
||||
'category' => 'unit_support',
|
||||
'brand_type' => 'Test Excavator X',
|
||||
'unit_category' => 'Excavator',
|
||||
'status' => 'Operation',
|
||||
'current_km' => 100,
|
||||
]
|
||||
);
|
||||
|
||||
$response = $this->actingAs($this->qmaAdmin)
|
||||
->get(route('qma.vehicles.support'));
|
||||
|
||||
$response->assertStatus(200);
|
||||
$response->assertSee('TEST-CAT');
|
||||
$response->assertSee('Tambah Kategori Unit');
|
||||
}
|
||||
|
||||
/**
|
||||
* Admin dapat melihat halaman daftar unit fisik di bawah kategori tertentu.
|
||||
*/
|
||||
public function test_qma_admin_can_view_physical_units_page(): void
|
||||
{
|
||||
$category = Vehicle::firstOrCreate(
|
||||
['code' => 'TEST-BL'],
|
||||
[
|
||||
'category' => 'unit_support',
|
||||
'brand_type' => 'Test Boomlift',
|
||||
'unit_category' => 'Boomlift',
|
||||
'status' => 'Operation',
|
||||
'current_km' => 50,
|
||||
]
|
||||
);
|
||||
|
||||
$response = $this->actingAs($this->qmaAdmin)
|
||||
->get(route('qma.vehicles.support.units', $category->id));
|
||||
|
||||
$response->assertStatus(200);
|
||||
$response->assertSee('Tambah Unit Baru');
|
||||
$response->assertSee('TEST-BL');
|
||||
}
|
||||
|
||||
/**
|
||||
* Pencarian unit fisik menampilkan hasil cocok atau pesan seragam jika tidak ditemukan.
|
||||
*/
|
||||
public function test_qma_admin_can_search_physical_units_and_renders_empty_state_when_not_found(): void
|
||||
{
|
||||
$category = Vehicle::firstOrCreate(
|
||||
['code' => 'TEST-SEARCH'],
|
||||
[
|
||||
'category' => 'unit_support',
|
||||
'brand_type' => 'Test Forklift',
|
||||
'unit_category' => 'Forklift',
|
||||
'status' => 'Operation',
|
||||
'current_km' => 50,
|
||||
]
|
||||
);
|
||||
|
||||
$category->units()->create([
|
||||
'code' => 'TEST-SEARCH-001',
|
||||
'category' => 'unit_support',
|
||||
'brand_type' => 'Test Forklift',
|
||||
'status' => 'Operation',
|
||||
'current_km' => 120,
|
||||
'location' => 'Workshop Central',
|
||||
'silo_document' => 'SILO-FL-2026',
|
||||
]);
|
||||
|
||||
// 1. Pencarian dengan kata kunci yang cocok
|
||||
$resFound = $this->actingAs($this->qmaAdmin)
|
||||
->get(route('qma.vehicles.support.units', ['id' => $category->id, 'search' => 'Workshop']));
|
||||
$resFound->assertStatus(200);
|
||||
$resFound->assertSee('TEST-SEARCH-001');
|
||||
$resFound->assertSee('Workshop Central');
|
||||
$resFound->assertSee('unitSearchInput');
|
||||
$resFound->assertSee('name="search"', false);
|
||||
$resFound->assertSee('value="Workshop"', false);
|
||||
|
||||
// 2. Pencarian dengan kata kunci yang tidak ditemukan
|
||||
$resNotFound = $this->actingAs($this->qmaAdmin)
|
||||
->get(route('qma.vehicles.support.units', ['id' => $category->id, 'search' => 'nonexistentkeyword']));
|
||||
$resNotFound->assertStatus(200);
|
||||
$resNotFound->assertDontSee('TEST-SEARCH-001');
|
||||
$resNotFound->assertSee('Data "<span class="text-rose-600 font-bold">nonexistentkeyword</span>" tidak ditemukan di tabel', false);
|
||||
$resNotFound->assertSee('Pastikan kata kunci yang dicari sudah benar.');
|
||||
$resNotFound->assertSee('Hapus Pencarian');
|
||||
}
|
||||
|
||||
/**
|
||||
* Admin dapat menambahkan unit fisik baru dan kode digenerate format 3-digit.
|
||||
*/
|
||||
public function test_qma_admin_can_create_physical_units_with_sequential_code(): void
|
||||
{
|
||||
$category = Vehicle::firstOrCreate(
|
||||
['code' => 'TEST-CR'],
|
||||
[
|
||||
'category' => 'unit_support',
|
||||
'brand_type' => 'Test Crane',
|
||||
'unit_category' => 'Crane',
|
||||
'status' => 'Operation',
|
||||
'current_km' => 0,
|
||||
]
|
||||
);
|
||||
|
||||
// Tambah unit fisik pertama
|
||||
$response1 = $this->actingAs($this->qmaAdmin)
|
||||
->post(route('qma.vehicles.support.units.store', $category->id), [
|
||||
'unit_number' => '001',
|
||||
'serial_number' => 'SN-CRANE-9988',
|
||||
'license_plate' => 'BH 5544 XX',
|
||||
'production_year' => 2022,
|
||||
'status' => 'Operation',
|
||||
'current_km' => 10,
|
||||
'location' => 'Inlet Dam',
|
||||
]);
|
||||
|
||||
$response1->assertRedirect();
|
||||
$this->assertDatabaseHas('vehicles', [
|
||||
'code' => 'TEST-CR-001',
|
||||
'parent_id' => $category->id,
|
||||
'category' => 'unit_support',
|
||||
'serial_number' => 'SN-CRANE-9988',
|
||||
'license_plate' => 'BH 5544 XX',
|
||||
'production_year' => 2022,
|
||||
]);
|
||||
|
||||
// Tambah unit fisik kedua dengan auto-number
|
||||
$response2 = $this->actingAs($this->qmaAdmin)
|
||||
->post(route('qma.vehicles.support.units.store', $category->id), [
|
||||
'unit_number' => '',
|
||||
'status' => 'Standby',
|
||||
'current_km' => 0,
|
||||
]);
|
||||
|
||||
$response2->assertRedirect();
|
||||
$this->assertDatabaseHas('vehicles', [
|
||||
'code' => 'TEST-CR-002',
|
||||
'parent_id' => $category->id,
|
||||
'category' => 'unit_support',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ketika kode kategori diubah, kode unit fisik di bawahnya otomatis tersinkronisasi.
|
||||
*/
|
||||
public function test_updating_category_code_cascades_to_physical_units(): void
|
||||
{
|
||||
$category = Vehicle::firstOrCreate(
|
||||
['code' => 'OLD01'],
|
||||
[
|
||||
'category' => 'unit_support',
|
||||
'brand_type' => 'Old Category',
|
||||
'unit_category' => 'Bulldozer',
|
||||
'status' => 'Operation',
|
||||
'current_km' => 0,
|
||||
]
|
||||
);
|
||||
|
||||
// Buat unit fisik
|
||||
$unit1 = Vehicle::firstOrCreate(
|
||||
['code' => 'OLD01-001'],
|
||||
[
|
||||
'parent_id' => $category->id,
|
||||
'category' => 'unit_support',
|
||||
'brand_type' => 'Old Category',
|
||||
'status' => 'Operation',
|
||||
'current_km' => 0,
|
||||
]
|
||||
);
|
||||
|
||||
$unit2 = Vehicle::firstOrCreate(
|
||||
['code' => 'OLD01-002'],
|
||||
[
|
||||
'parent_id' => $category->id,
|
||||
'category' => 'unit_support',
|
||||
'brand_type' => 'Old Category',
|
||||
'status' => 'Operation',
|
||||
'current_km' => 0,
|
||||
]
|
||||
);
|
||||
|
||||
// Update kategori dari OLD01 menjadi NEW01
|
||||
$response = $this->actingAs($this->qmaAdmin)
|
||||
->post(route('qma.vehicles.update', $category->id), [
|
||||
'code' => 'NEW01',
|
||||
'category' => 'unit_support',
|
||||
'brand_type' => 'Old Category Renamed',
|
||||
'status' => 'Operation',
|
||||
'current_km' => 0,
|
||||
]);
|
||||
|
||||
$response->assertRedirect();
|
||||
|
||||
// Kategori terupdate
|
||||
$this->assertDatabaseHas('vehicles', [
|
||||
'id' => $category->id,
|
||||
'code' => 'NEW01',
|
||||
]);
|
||||
|
||||
// Seluruh anak fisik otomatis tersinkronisasi
|
||||
$this->assertDatabaseHas('vehicles', [
|
||||
'id' => $unit1->id,
|
||||
'code' => 'NEW01-001',
|
||||
]);
|
||||
$this->assertDatabaseHas('vehicles', [
|
||||
'id' => $unit2->id,
|
||||
'code' => 'NEW01-002',
|
||||
]);
|
||||
|
||||
// Kode lama tidak boleh ada lagi
|
||||
$this->assertDatabaseMissing('vehicles', ['code' => 'OLD01-001']);
|
||||
$this->assertDatabaseMissing('vehicles', ['code' => 'OLD01-002']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Pastikan modul Light Vehicle (LV) tidak terpengaruh sama sekali.
|
||||
*/
|
||||
public function test_light_vehicle_module_remains_unaffected(): void
|
||||
{
|
||||
$lv = Vehicle::firstOrCreate(
|
||||
['code' => 'LV-TEST-99'],
|
||||
[
|
||||
'category' => 'light_vehicle',
|
||||
'brand_type' => 'Toyota Hilux Test',
|
||||
'license_plate' => 'BH 9999 XX',
|
||||
'status' => 'Operation',
|
||||
'current_km' => 12000,
|
||||
]
|
||||
);
|
||||
|
||||
$response = $this->actingAs($this->qmaAdmin)
|
||||
->get(route('qma.vehicles.lv'));
|
||||
|
||||
$response->assertStatus(200);
|
||||
$response->assertSee('LV-TEST-99');
|
||||
$this->assertNull($lv->parent_id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user