penambahan fitur ekspor di hse, qma, dan pg

This commit is contained in:
Ray1111-aa
2026-10-16 11:02:28 +07:00
parent a54f80a45e
commit 6e5ae727f9
34 changed files with 4964 additions and 169 deletions
+112
View File
@@ -0,0 +1,112 @@
<?php
namespace Tests\Feature;
use App\Models\Employee;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
use Tests\TestCase;
class HseSimperExportTest extends TestCase
{
use RefreshDatabase;
private User $hseAdmin;
private User $unauthorizedUser;
protected function setUp(): void
{
parent::setUp();
$roleHse = Role::firstOrCreate(['name' => 'hse_admin', 'guard_name' => 'web']);
$permissionView = Permission::firstOrCreate(['name' => 'hse.employees.view', 'guard_name' => 'web']);
$roleHse->givePermissionTo($permissionView);
$this->hseAdmin = User::factory()->create();
$this->hseAdmin->assignRole('hse_admin');
$this->unauthorizedUser = User::factory()->create();
// Seed sample employees
Employee::create([
'nik' => '10001',
'name' => 'Budi Operator',
'position' => 'Operator Excavator',
'unit_kerja' => 'Operasional',
'division' => 'Alat Berat',
'is_operator' => true,
'simper_expired_at' => now()->addMonths(6)->toDateString(),
]);
Employee::create([
'nik' => '10002',
'name' => 'Siti Staff',
'position' => 'Admin Keuangan',
'unit_kerja' => 'Kantor Utama',
'division' => 'Finance',
'is_operator' => false,
'simper_expired_at' => null,
]);
}
public function test_admin_hse_can_export_simper_excel(): void
{
$response = $this->actingAs($this->hseAdmin)
->get(route('hse.employees.export_excel'));
$response->assertStatus(200);
$response->assertHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
$this->assertStringContainsString('Laporan_Data_Simper_', $response->headers->get('Content-Disposition'));
$this->assertNotEmpty($response->getContent());
}
public function test_admin_hse_can_export_simper_excel_with_filters(): void
{
$response = $this->actingAs($this->hseAdmin)
->get(route('hse.employees.export_excel', [
'driver_status' => 'driver',
'simper_status' => 'valid',
'search' => 'Budi',
]));
$response->assertStatus(200);
$response->assertHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
}
public function test_admin_hse_can_export_simper_pdf(): void
{
$response = $this->actingAs($this->hseAdmin)
->get(route('hse.employees.export_pdf'));
$response->assertStatus(200);
$response->assertHeader('Content-Type', 'application/pdf');
$this->assertStringContainsString('Laporan_Daftar_Simper_', (string) $response->headers->get('Content-Disposition'));
$this->assertStringStartsWith('%PDF-', $response->getContent());
$this->assertNotEmpty($response->getContent());
}
public function test_admin_hse_can_export_simper_pdf_with_driver_filter(): void
{
$response = $this->actingAs($this->hseAdmin)
->get(route('hse.employees.export_pdf', ['driver_status' => 'driver']));
$response->assertStatus(200);
$response->assertHeader('Content-Type', 'application/pdf');
$this->assertStringStartsWith('%PDF-', $response->getContent());
$this->assertNotEmpty($response->getContent());
$this->assertStringContainsString('Laporan_Daftar_Simper_', (string) $response->headers->get('Content-Disposition'));
}
public function test_unauthorized_user_cannot_export_simper(): void
{
$responseExcel = $this->actingAs($this->unauthorizedUser)
->get(route('hse.employees.export_excel'));
$responseExcel->assertStatus(403);
$responsePdf = $this->actingAs($this->unauthorizedUser)
->get(route('hse.employees.export_pdf'));
$responsePdf->assertStatus(403);
}
}
+262
View File
@@ -0,0 +1,262 @@
<?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 Database\Seeders\RolePermissionSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class PgExportTest extends TestCase
{
use RefreshDatabase;
private User $pgAdmin;
private User $unauthorizedUser;
private Gate $gate1;
private Gate $gate2;
private SecurityGuard $guard1;
private SecurityGuard $guard2;
private Vehicle $vehicle;
private Employee $driver;
protected function setUp(): void
{
parent::setUp();
$this->seed(RolePermissionSeeder::class);
$this->pgAdmin = User::factory()->create([
'name' => 'Admin PG Test',
'username' => 'pg_test',
'role' => 'pg_admin',
]);
$this->pgAdmin->assignRole('pg_admin');
$this->unauthorizedUser = User::factory()->create([
'name' => 'Unauthorized User',
'username' => 'unauthorized_test',
'role' => 'employee',
]);
$this->gate1 = Gate::create([
'name' => 'Pos Gerbang Utama',
'code' => 'GATE-01',
'location' => 'Main Entrance',
'is_active' => true,
]);
$this->gate2 = Gate::create([
'name' => 'Pos Gerbang Bendungan',
'code' => 'GATE-02',
'location' => 'Dam Access',
'is_active' => true,
]);
$userGuard1 = User::factory()->create([
'name' => 'Budi Santoso',
'username' => 'SEC001',
'role' => 'gate_officer',
'gate_id' => $this->gate1->id,
]);
$this->guard1 = SecurityGuard::create([
'badge_id' => 'SEC001',
'name' => 'Budi Santoso',
'team' => 1,
'gate_id' => $this->gate1->id,
'user_id' => $userGuard1->id,
'is_active' => true,
]);
$userGuard2 = User::factory()->create([
'name' => 'Agus Wijaya',
'username' => 'SEC002',
'role' => 'gate_officer',
'gate_id' => $this->gate2->id,
]);
$this->guard2 = SecurityGuard::create([
'badge_id' => 'SEC002',
'name' => 'Agus Wijaya',
'team' => 2,
'gate_id' => $this->gate2->id,
'user_id' => $userGuard2->id,
'is_active' => false,
]);
$this->vehicle = Vehicle::create([
'code' => 'LV-01',
'category' => 'light_vehicle',
'brand' => 'Toyota',
'brand_type' => 'Toyota Hilux 4x4',
'unit_type' => 'Double Cabin',
'license_plate' => 'BH 1234 XY',
'ownership' => 'PT KMH',
'department' => 'Civil & Inviro',
'status' => 'Operation',
'current_km' => 15000,
]);
$this->driver = Employee::create([
'name' => 'Rahmat Hidayat',
'nik' => 'KMH-1001',
'department' => 'Civil & Inviro',
'position' => 'Driver',
'status' => 'active',
]);
}
public function test_admin_pg_can_export_guards_excel(): void
{
$response = $this->actingAs($this->pgAdmin)
->get(route('pg.guards.export_excel'));
$response->assertOk();
$response->assertHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
$this->assertStringContainsString('data_satpam_', $response->headers->get('Content-Disposition'));
$this->assertNotEmpty($response->getContent());
}
public function test_admin_pg_can_export_guards_excel_with_filters(): void
{
$response = $this->actingAs($this->pgAdmin)
->get(route('pg.guards.export_excel', [
'team' => 1,
'gate_id' => $this->gate1->id,
'search' => 'Budi',
]));
$response->assertOk();
$response->assertHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
$this->assertNotEmpty($response->getContent());
}
public function test_admin_pg_can_export_guards_pdf(): void
{
$response = $this->actingAs($this->pgAdmin)
->get(route('pg.guards.export_pdf'));
$response->assertOk();
$response->assertHeader('Content-Type', 'application/pdf');
$this->assertStringContainsString('inline;', $response->headers->get('Content-Disposition'));
$this->assertStringContainsString('laporan_data_satpam_', $response->headers->get('Content-Disposition'));
$this->assertStringStartsWith('%PDF-', $response->getContent());
}
public function test_admin_pg_can_export_verification_logs_excel(): void
{
VehicleActivity::create([
'transaction_no' => 'TRX-20261016-0001',
'vehicle_id' => $this->vehicle->id,
'driver_id' => $this->driver->id,
'gate_id' => $this->gate1->id,
'security_guard_id' => $this->guard1->id,
'direction' => 'out',
'previous_km' => 14950,
'current_km' => 15000,
'distance' => 50,
'purpose' => 'Mobilisasi material ke Dam Site',
'validation_status' => 'approved',
'recorded_at' => Carbon::now(),
'validated_at' => Carbon::now(),
]);
$response = $this->actingAs($this->pgAdmin)
->get(route('pg.logs.export_excel'));
$response->assertOk();
$response->assertHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
$this->assertStringContainsString('log_aktivitas_gerbang_', $response->headers->get('Content-Disposition'));
$this->assertNotEmpty($response->getContent());
}
public function test_admin_pg_can_export_verification_logs_excel_with_filters(): void
{
VehicleActivity::create([
'transaction_no' => 'TRX-20261016-0002',
'vehicle_id' => $this->vehicle->id,
'driver_id' => $this->driver->id,
'gate_id' => $this->gate1->id,
'security_guard_id' => $this->guard1->id,
'direction' => 'in',
'previous_km' => 15000,
'current_km' => 15050,
'distance' => 50,
'purpose' => 'Kembali ke workshop',
'validation_status' => 'rejected',
'rejection_note' => 'Odometer tidak sesuai',
'recorded_at' => Carbon::now(),
'validated_at' => Carbon::now(),
]);
$today = Carbon::today()->toDateString();
$response = $this->actingAs($this->pgAdmin)
->get(route('pg.logs.export_excel', [
'team' => 1,
'gate_id' => $this->gate1->id,
'direction' => 'in',
'status' => 'rejected',
'start_date' => $today,
'end_date' => $today,
'search' => 'TRX',
]));
$response->assertOk();
$response->assertHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
$this->assertNotEmpty($response->getContent());
}
public function test_admin_pg_can_export_verification_logs_pdf(): void
{
VehicleActivity::create([
'transaction_no' => 'TRX-20261016-0003',
'vehicle_id' => $this->vehicle->id,
'driver_id' => $this->driver->id,
'gate_id' => $this->gate1->id,
'security_guard_id' => $this->guard1->id,
'direction' => 'out',
'previous_km' => 15050,
'current_km' => 15100,
'distance' => 50,
'purpose' => 'Inspeksi lapangan',
'validation_status' => 'approved',
'recorded_at' => Carbon::now(),
'validated_at' => Carbon::now(),
]);
$response = $this->actingAs($this->pgAdmin)
->get(route('pg.logs.export_pdf'));
$response->assertOk();
$response->assertHeader('Content-Type', 'application/pdf');
$this->assertStringContainsString('inline;', $response->headers->get('Content-Disposition'));
$this->assertStringContainsString('log_verifikasi_gerbang_', $response->headers->get('Content-Disposition'));
$this->assertStringStartsWith('%PDF-', $response->getContent());
}
public function test_unauthorized_user_cannot_export_pg_guards_or_logs(): void
{
$this->actingAs($this->unauthorizedUser)
->get(route('pg.guards.export_excel'))
->assertForbidden();
$this->actingAs($this->unauthorizedUser)
->get(route('pg.guards.export_pdf'))
->assertForbidden();
$this->actingAs($this->unauthorizedUser)
->get(route('pg.logs.export_excel'))
->assertForbidden();
$this->actingAs($this->unauthorizedUser)
->get(route('pg.logs.export_pdf'))
->assertForbidden();
}
}
@@ -0,0 +1,236 @@
<?php
namespace Tests\Feature;
use App\Models\Employee;
use App\Models\Gate;
use App\Models\P2hRecord;
use App\Models\P2hTemplate;
use App\Models\SecurityGuard;
use App\Models\User;
use App\Models\Vehicle;
use App\Models\VehicleActivity;
use Database\Seeders\RolePermissionSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class QmaActivityAndP2hExportTest extends TestCase
{
use RefreshDatabase;
private User $qmaAdmin;
private User $unauthorizedUser;
private Vehicle $lvVehicle;
private Vehicle $supportVehicle;
private Employee $driver;
private Gate $gate;
private SecurityGuard $guard;
private P2hTemplate $template;
protected function setUp(): void
{
parent::setUp();
$this->seed(RolePermissionSeeder::class);
$this->qmaAdmin = User::factory()->create([
'name' => 'Admin QMA Test',
'username' => 'qma_test',
'role' => 'qma_admin',
]);
$this->qmaAdmin->assignRole('qma_admin');
$this->unauthorizedUser = User::factory()->create([
'name' => 'Unauthorized User',
'username' => 'unauthorized_test',
'role' => 'employee',
]);
$this->lvVehicle = Vehicle::create([
'code' => 'LV-01',
'category' => 'light_vehicle',
'brand' => 'Toyota',
'brand_type' => 'Toyota Hilux 4x4',
'unit_type' => 'Double Cabin',
'license_plate' => 'BH 1234 XY',
'ownership' => 'PT KMH',
'department' => 'Civil & Inviro',
'status' => 'Operation',
'current_km' => 12500,
]);
$this->supportVehicle = Vehicle::create([
'code' => 'EXC-01',
'category' => 'unit_support',
'unit_category' => 'Excavator',
'brand' => 'Komatsu',
'brand_type' => 'Komatsu PC200-8',
'location' => 'Main Dam',
'status' => 'Operation',
'current_km' => 2400,
]);
$this->driver = Employee::create([
'nik' => '20001',
'name' => 'Doni Driver',
'position' => 'Driver LV',
'unit_kerja' => 'Operasional',
'is_operator' => true,
'simper_expired_at' => now()->addYear()->toDateString(),
]);
$this->gate = Gate::create([
'name' => 'Main Gate KMH',
'code' => 'MG-01',
'location' => 'Pintu Masuk Utama',
'is_active' => true,
]);
$this->guard = SecurityGuard::create([
'name' => 'Bambang Security',
'badge_id' => 'SEC-001',
'phone_number' => '08123456789',
'status' => 'active',
'assigned_gate_id' => $this->gate->id,
'current_shift' => 'Shift 1',
]);
$this->template = P2hTemplate::create([
'name' => 'Checksheet Excavator',
'code' => 'P2H-EXC',
'is_active' => true,
]);
// Seed Sample Vehicle Activity
VehicleActivity::create([
'transaction_no' => 'TRX-ACT-001',
'vehicle_id' => $this->lvVehicle->id,
'driver_id' => $this->driver->id,
'gate_id' => $this->gate->id,
'direction' => 'out',
'previous_km' => 12450,
'current_km' => 12500,
'distance' => 50,
'purpose' => 'Antar Logistik ke Quarry 2',
'recorded_at' => now(),
'security_guard_id' => $this->guard->id,
'validation_status' => 'approved',
'validated_at' => now(),
]);
// Seed Sample P2H Record
P2hRecord::create([
'transaction_no' => 'P2H-20261016-0001',
'vehicle_id' => $this->supportVehicle->id,
'employee_id' => $this->driver->id,
'template_id' => $this->template->id,
'shift' => 'Shift 1',
'start_hm' => 2400.5,
'summary_condition' => 'normal',
'defect_count' => 0,
'general_notes' => 'Kondisi mesin dan hidrolik baik',
'validation_status' => 'approved',
'validated_by' => $this->qmaAdmin->id,
'validated_at' => now(),
'inspected_at' => now(),
]);
}
public function test_admin_qma_can_export_activity_log_excel(): void
{
$response = $this->actingAs($this->qmaAdmin)
->get(route('qma.activity.export_excel'));
$response->assertStatus(200);
$response->assertHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
$this->assertStringContainsString('Laporan_Timesheet_LV_', (string) $response->headers->get('Content-Disposition'));
$content = $response->getContent();
$this->assertStringStartsWith("PK\x03\x04", $content);
}
public function test_admin_qma_can_export_activity_log_excel_with_filters(): void
{
$response = $this->actingAs($this->qmaAdmin)
->get(route('qma.activity.export_excel', [
'direction' => 'out',
'gate_id' => $this->gate->id,
'status' => 'approved',
'search' => 'Hilux',
]));
$response->assertStatus(200);
$response->assertHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
$this->assertStringStartsWith("PK\x03\x04", $response->getContent());
}
public function test_admin_qma_can_export_activity_log_pdf(): void
{
$response = $this->actingAs($this->qmaAdmin)
->get(route('qma.activity.export_pdf'));
$response->assertStatus(200);
$response->assertHeader('Content-Type', 'application/pdf');
$this->assertStringContainsString('inline; filename="Laporan_Timesheet_LV_', (string) $response->headers->get('Content-Disposition'));
$content = $response->getContent();
$this->assertStringStartsWith('%PDF-', $content);
$this->assertNotEmpty($content);
}
public function test_admin_qma_can_export_p2h_records_excel(): void
{
$response = $this->actingAs($this->qmaAdmin)
->get(route('qma.p2h.export_excel'));
$response->assertStatus(200);
$response->assertHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
$this->assertStringContainsString('Laporan_P2H_Unit_Support_', (string) $response->headers->get('Content-Disposition'));
$content = $response->getContent();
$this->assertStringStartsWith("PK\x03\x04", $content);
}
public function test_admin_qma_can_export_p2h_records_excel_with_filters(): void
{
$response = $this->actingAs($this->qmaAdmin)
->get(route('qma.p2h.export_excel', [
'validation_status' => 'approved',
'summary_condition' => 'normal',
'search' => 'EXC-01',
]));
$response->assertStatus(200);
$response->assertHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
$this->assertStringStartsWith("PK\x03\x04", $response->getContent());
}
public function test_admin_qma_can_export_p2h_records_pdf(): void
{
$response = $this->actingAs($this->qmaAdmin)
->get(route('qma.p2h.export_pdf'));
$response->assertStatus(200);
$response->assertHeader('Content-Type', 'application/pdf');
$this->assertStringContainsString('inline; filename="Laporan_P2H_Unit_Support_', (string) $response->headers->get('Content-Disposition'));
$content = $response->getContent();
$this->assertStringStartsWith('%PDF-', $content);
$this->assertNotEmpty($content);
}
public function test_unauthorized_user_cannot_export_activity_and_p2h(): void
{
$routes = [
route('qma.activity.export_excel'),
route('qma.activity.export_pdf'),
route('qma.p2h.export_excel'),
route('qma.p2h.export_pdf'),
];
foreach ($routes as $routeUrl) {
$response = $this->actingAs($this->unauthorizedUser)->get($routeUrl);
$response->assertStatus(403);
}
}
}
+235
View File
@@ -0,0 +1,235 @@
<?php
namespace Tests\Feature;
use App\Models\Employee;
use App\Models\Gate;
use App\Models\P2hRecord;
use App\Models\P2hTemplate;
use App\Models\SecurityGuard;
use App\Models\User;
use App\Models\Vehicle;
use App\Models\VehicleActivity;
use Database\Seeders\RolePermissionSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class QmaSummaryExportTest extends TestCase
{
use RefreshDatabase;
private User $qmaAdmin;
private User $unauthorizedUser;
private Vehicle $lvVehicle;
private Vehicle $supportVehicle;
private Employee $driver;
private Gate $gate;
private SecurityGuard $guard;
private P2hTemplate $template;
protected function setUp(): void
{
parent::setUp();
$this->seed(RolePermissionSeeder::class);
$this->qmaAdmin = User::factory()->create([
'name' => 'Admin QMA Test',
'username' => 'qma_test',
'role' => 'qma_admin',
]);
$this->qmaAdmin->assignRole('qma_admin');
$this->unauthorizedUser = User::factory()->create([
'name' => 'Unauthorized User',
'username' => 'unauthorized_test',
'role' => 'employee',
]);
$this->lvVehicle = Vehicle::create([
'code' => 'LV-01',
'category' => 'light_vehicle',
'brand' => 'Toyota',
'brand_type' => 'Toyota Hilux 4x4',
'unit_type' => 'Double Cabin',
'license_plate' => 'BH 1234 XY',
'ownership' => 'PT KMH',
'department' => 'Civil & Inviro',
'status' => 'Operation',
'current_km' => 12500,
]);
$this->supportVehicle = Vehicle::create([
'code' => 'EXC-01',
'category' => 'unit_support',
'unit_category' => 'Excavator',
'brand' => 'Komatsu',
'brand_type' => 'Komatsu PC200-8',
'location' => 'Main Dam',
'status' => 'Operation',
'current_km' => 2400,
]);
$this->driver = Employee::create([
'nik' => '20001',
'name' => 'Doni Driver',
'position' => 'Driver LV',
'unit_kerja' => 'Operasional',
'is_operator' => true,
'simper_expired_at' => now()->addYear()->toDateString(),
]);
$this->gate = Gate::create([
'name' => 'Main Gate KMH',
'code' => 'MG-01',
'location' => 'Pintu Masuk Utama',
'is_active' => true,
]);
$this->guard = SecurityGuard::create([
'name' => 'Bambang Security',
'badge_id' => 'SEC-001',
'phone_number' => '08123456789',
'status' => 'active',
'assigned_gate_id' => $this->gate->id,
'current_shift' => 'Shift 1',
]);
$this->template = P2hTemplate::create([
'name' => 'Checksheet Excavator',
'code' => 'P2H-EXC',
'is_active' => true,
]);
// Seed Sample Vehicle Activities
VehicleActivity::create([
'transaction_no' => 'TRX-ACT-001',
'vehicle_id' => $this->lvVehicle->id,
'driver_id' => $this->driver->id,
'gate_id' => $this->gate->id,
'direction' => 'out',
'previous_km' => 12450,
'current_km' => 12500,
'distance' => 50,
'purpose' => 'Antar Logistik ke Quarry 2',
'recorded_at' => now(),
'security_guard_id' => $this->guard->id,
'validation_status' => 'approved',
'validated_at' => now(),
]);
// Seed Sample P2H Record
P2hRecord::create([
'transaction_no' => 'P2H-20261016-0001',
'vehicle_id' => $this->supportVehicle->id,
'employee_id' => $this->driver->id,
'template_id' => $this->template->id,
'shift' => 'Shift 1',
'start_hm' => 2400.5,
'summary_condition' => 'normal',
'defect_count' => 0,
'general_notes' => 'Kondisi alat prima',
'validation_status' => 'approved',
'validated_by' => $this->qmaAdmin->id,
'validated_at' => now(),
'inspected_at' => now(),
]);
}
public function test_admin_qma_can_export_activity_summary_excel(): void
{
$response = $this->actingAs($this->qmaAdmin)
->get(route('qma.activity.summary.export_excel'));
$response->assertStatus(200);
$response->assertHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
$this->assertStringContainsString('Laporan_Summary_Aktivitas_Kendaraan_', (string) $response->headers->get('Content-Disposition'));
$content = $response->getContent();
$this->assertStringStartsWith("PK\x03\x04", $content);
}
public function test_admin_qma_can_export_activity_summary_excel_with_filters(): void
{
$response = $this->actingAs($this->qmaAdmin)
->get(route('qma.activity.summary.export_excel', [
'type' => 'lv',
'gate_id' => $this->gate->id,
'search' => 'Hilux',
]));
$response->assertStatus(200);
$response->assertHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
$this->assertStringStartsWith("PK\x03\x04", $response->getContent());
}
public function test_admin_qma_can_export_activity_summary_pdf(): void
{
$response = $this->actingAs($this->qmaAdmin)
->get(route('qma.activity.summary.export_pdf'));
$response->assertStatus(200);
$response->assertHeader('Content-Type', 'application/pdf');
$this->assertStringContainsString('inline; filename="Laporan_Summary_Aktivitas_Kendaraan_', (string) $response->headers->get('Content-Disposition'));
$content = $response->getContent();
$this->assertStringStartsWith('%PDF-', $content);
$this->assertNotEmpty($content);
}
public function test_admin_qma_can_export_p2h_summary_excel(): void
{
$response = $this->actingAs($this->qmaAdmin)
->get(route('qma.p2h.summary.export_excel'));
$response->assertStatus(200);
$response->assertHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
$this->assertStringContainsString('Laporan_Summary_P2H_Unit_Support_', (string) $response->headers->get('Content-Disposition'));
$content = $response->getContent();
$this->assertStringStartsWith("PK\x03\x04", $content);
}
public function test_admin_qma_can_export_p2h_summary_excel_with_filters(): void
{
$response = $this->actingAs($this->qmaAdmin)
->get(route('qma.p2h.summary.export_excel', [
'validation_status' => 'approved',
'shift' => 'Shift 1',
'search' => 'EXC-01',
]));
$response->assertStatus(200);
$response->assertHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
$this->assertStringStartsWith("PK\x03\x04", $response->getContent());
}
public function test_admin_qma_can_export_p2h_summary_pdf(): void
{
$response = $this->actingAs($this->qmaAdmin)
->get(route('qma.p2h.summary.export_pdf'));
$response->assertStatus(200);
$response->assertHeader('Content-Type', 'application/pdf');
$this->assertStringContainsString('inline; filename="Laporan_Summary_P2H_Unit_Support_', (string) $response->headers->get('Content-Disposition'));
$content = $response->getContent();
$this->assertStringStartsWith('%PDF-', $content);
$this->assertNotEmpty($content);
}
public function test_unauthorized_user_cannot_export_qma_summaries(): void
{
$routes = [
route('qma.activity.summary.export_excel'),
route('qma.activity.summary.export_pdf'),
route('qma.p2h.summary.export_excel'),
route('qma.p2h.summary.export_pdf'),
];
foreach ($routes as $routeUrl) {
$response = $this->actingAs($this->unauthorizedUser)->get($routeUrl);
$response->assertStatus(403);
}
}
}
+230
View File
@@ -0,0 +1,230 @@
<?php
namespace Tests\Feature;
use App\Models\User;
use App\Models\Vehicle;
use Database\Seeders\RolePermissionSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class QmaVehicleExportTest extends TestCase
{
use RefreshDatabase;
private User $qmaAdmin;
private User $unauthorizedUser;
private Vehicle $lvVehicle;
private Vehicle $supportCategory;
protected function setUp(): void
{
parent::setUp();
$this->seed(RolePermissionSeeder::class);
$this->qmaAdmin = User::factory()->create([
'name' => 'Admin QMA Test',
'username' => 'qma_test',
'role' => 'qma_admin',
]);
$this->qmaAdmin->assignRole('qma_admin');
$this->unauthorizedUser = User::factory()->create([
'name' => 'Unauthorized User',
'username' => 'unauthorized_test',
'role' => 'employee',
]);
// Seed Sample Light Vehicle
$this->lvVehicle = Vehicle::create([
'code' => 'LV-01',
'category' => 'light_vehicle',
'brand' => 'Toyota',
'brand_type' => 'Toyota Hilux Double Cabin 4x4',
'unit_type' => 'Double Cabin',
'license_plate' => 'BH 8888 KM',
'ownership' => 'PT KMH',
'department' => 'Civil & Inviro',
'division' => 'Operasional',
'status' => 'Operation',
'current_km' => 15400,
]);
// Seed Sample Unit Support Category
$this->supportCategory = Vehicle::create([
'code' => 'CAT-EXC-01',
'category' => 'unit_support',
'unit_category' => 'Excavator',
'brand' => 'Komatsu',
'brand_type' => 'Komatsu PC200-8',
'model_number' => 'PC200-8',
'location' => 'Main Dam KMH',
'ownership' => 'PT KMH Project',
'status' => 'Operation',
'parent_id' => null,
]);
}
public function test_admin_qma_can_export_lv_excel(): void
{
$response = $this->actingAs($this->qmaAdmin)
->get(route('qma.vehicles.lv.export_excel'));
$response->assertStatus(200);
$response->assertHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
$this->assertStringContainsString('Laporan_Data_Light_Vehicle_', (string) $response->headers->get('Content-Disposition'));
$content = $response->getContent();
$this->assertStringStartsWith("PK\x03\x04", $content);
}
public function test_admin_qma_can_export_lv_excel_with_filters(): void
{
$response = $this->actingAs($this->qmaAdmin)
->get(route('qma.vehicles.lv.export_excel', [
'status' => 'Operation',
'search' => 'Hilux',
]));
$response->assertStatus(200);
$response->assertHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
$this->assertStringStartsWith("PK\x03\x04", $response->getContent());
}
public function test_admin_qma_can_export_lv_pdf(): void
{
$response = $this->actingAs($this->qmaAdmin)
->get(route('qma.vehicles.lv.export_pdf'));
$response->assertStatus(200);
$response->assertHeader('Content-Type', 'application/pdf');
$this->assertStringContainsString('inline; filename="Laporan_Light_Vehicle_', (string) $response->headers->get('Content-Disposition'));
$content = $response->getContent();
$this->assertStringStartsWith('%PDF-', $content);
$this->assertNotEmpty($content);
}
public function test_admin_qma_can_export_support_excel_with_category_fallback(): void
{
// Saat belum ada unit fisik, mengekspor daftar master kategori
$response = $this->actingAs($this->qmaAdmin)
->get(route('qma.vehicles.support.export_excel'));
$response->assertStatus(200);
$response->assertHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
$this->assertStringContainsString('Laporan_Kategori_Unit_Support_', (string) $response->headers->get('Content-Disposition'));
$content = $response->getContent();
$this->assertStringStartsWith("PK\x03\x04", $content);
}
public function test_admin_qma_can_export_support_excel_with_physical_units(): void
{
// Buat unit fisik alat berat di bawah kategori
Vehicle::create([
'code' => 'EXC-01',
'category' => 'unit_support',
'parent_id' => $this->supportCategory->id,
'brand_type' => 'Komatsu PC200-8',
'serial_number' => 'KMH-SN-998811',
'production_year' => 2023,
'license_plate' => 'BH 1234 EX',
'location' => 'Powerhouse Intake',
'current_km' => 3200,
'status' => 'Operation',
'silo_document' => 'silo_docs/sample.pdf',
]);
$response = $this->actingAs($this->qmaAdmin)
->get(route('qma.vehicles.support.export_excel'));
$response->assertStatus(200);
$response->assertHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
$this->assertStringContainsString('Laporan_Unit_Support_Lengkap_', (string) $response->headers->get('Content-Disposition'));
$content = $response->getContent();
$this->assertStringStartsWith("PK\x03\x04", $content);
}
public function test_admin_qma_can_export_support_pdf(): void
{
// 1. Fallback master kategori
$responseFallback = $this->actingAs($this->qmaAdmin)
->get(route('qma.vehicles.support.export_pdf'));
$responseFallback->assertStatus(200);
$responseFallback->assertHeader('Content-Type', 'application/pdf');
$this->assertStringStartsWith('%PDF-', $responseFallback->getContent());
// 2. Dengan unit fisik
Vehicle::create([
'code' => 'EXC-02',
'category' => 'unit_support',
'parent_id' => $this->supportCategory->id,
'brand_type' => 'Komatsu PC200-8',
'serial_number' => 'KMH-SN-776655',
'production_year' => 2022,
'location' => 'Quarry 2',
'current_km' => 1500,
'status' => 'Standby',
]);
$responsePhysical = $this->actingAs($this->qmaAdmin)
->get(route('qma.vehicles.support.export_pdf'));
$responsePhysical->assertStatus(200);
$responsePhysical->assertHeader('Content-Type', 'application/pdf');
$this->assertStringContainsString('Laporan_Unit_Support_', (string) $responsePhysical->headers->get('Content-Disposition'));
$this->assertStringStartsWith('%PDF-', $responsePhysical->getContent());
}
public function test_admin_qma_can_export_category_specific_units_excel_and_pdf(): void
{
Vehicle::create([
'code' => 'EXC-03',
'category' => 'unit_support',
'parent_id' => $this->supportCategory->id,
'brand_type' => 'Komatsu PC200-8',
'serial_number' => 'KMH-SN-443322',
'production_year' => 2024,
'location' => 'Dam Site',
'current_km' => 450,
'status' => 'Operation',
]);
// Excel
$responseExcel = $this->actingAs($this->qmaAdmin)
->get(route('qma.vehicles.support.units.export_excel', $this->supportCategory->id));
$responseExcel->assertStatus(200);
$responseExcel->assertHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
$this->assertStringStartsWith("PK\x03\x04", $responseExcel->getContent());
// PDF
$responsePdf = $this->actingAs($this->qmaAdmin)
->get(route('qma.vehicles.support.units.export_pdf', $this->supportCategory->id));
$responsePdf->assertStatus(200);
$responsePdf->assertHeader('Content-Type', 'application/pdf');
$this->assertStringStartsWith('%PDF-', $responsePdf->getContent());
}
public function test_unauthorized_user_cannot_export_qma_vehicles(): void
{
$routes = [
route('qma.vehicles.lv.export_excel'),
route('qma.vehicles.lv.export_pdf'),
route('qma.vehicles.support.export_excel'),
route('qma.vehicles.support.export_pdf'),
route('qma.vehicles.support.units.export_excel', $this->supportCategory->id),
route('qma.vehicles.support.units.export_pdf', $this->supportCategory->id),
];
foreach ($routes as $routeUrl) {
$response = $this->actingAs($this->unauthorizedUser)->get($routeUrl);
$response->assertStatus(403);
}
}
}
+18 -3
View File
@@ -166,17 +166,32 @@ class SuperAdminRbacTest extends TestCase
{
$superAdmin = User::where('username', 'superadmin')->first();
// HSE Dashboard
// 1. HSE Module
$response = $this->actingAs($superAdmin)->get(route('hse.dashboard'));
$response->assertOk();
$this->actingAs($superAdmin)->get(route('hse.employees.index'))->assertOk();
// QMA Dashboard
// 2. QMA Module (LV, Support, Activity Logs, P2H Records, P2H Templates)
$response = $this->actingAs($superAdmin)->get(route('qma.dashboard'));
$response->assertOk();
$this->actingAs($superAdmin)->get(route('qma.vehicles.lv'))->assertOk();
$this->actingAs($superAdmin)->get(route('qma.vehicles.support'))->assertOk();
$this->actingAs($superAdmin)->get(route('qma.activity.log'))->assertOk();
$this->actingAs($superAdmin)->get(route('qma.activity.summary'))->assertOk();
$this->actingAs($superAdmin)->get(route('qma.p2h.index'))->assertOk();
$this->actingAs($superAdmin)->get(route('qma.p2h.summary'))->assertOk();
$this->actingAs($superAdmin)->get(route('qma.p2h.templates'))->assertOk();
// PG Dashboard
// 3. PG Module (Guards, Logs, Gate Inspection)
$response = $this->actingAs($superAdmin)->get(route('pg.dashboard'));
$response->assertOk();
$this->actingAs($superAdmin)->get(route('pg.guards.index'))->assertOk();
$this->actingAs($superAdmin)->get(route('pg.logs.index'))->assertOk();
$this->actingAs($superAdmin)->get(route('pg.gate.inspection'))->assertOk();
// 4. Employee & P2H Public/Operator Module
$this->actingAs($superAdmin)->get(route('p2h.index'))->assertOk();
$this->actingAs($superAdmin)->get(route('employee.dashboard'))->assertOk();
}
public function test_permission_revocation_immediately_blocks_action_and_hides_button(): void