257 lines
9.3 KiB
PHP
257 lines
9.3 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
namespace Tests\Feature;
|
||
|
|
|
||
|
|
use App\Models\Employee;
|
||
|
|
use App\Models\User;
|
||
|
|
use App\Models\Vehicle;
|
||
|
|
use Database\Seeders\RolePermissionSeeder;
|
||
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||
|
|
use Illuminate\Http\UploadedFile;
|
||
|
|
use Illuminate\Support\Facades\Storage;
|
||
|
|
use Tests\TestCase;
|
||
|
|
|
||
|
|
class PhotoUploadAndCompressionTest extends TestCase
|
||
|
|
{
|
||
|
|
use RefreshDatabase;
|
||
|
|
|
||
|
|
protected User $adminQma;
|
||
|
|
protected User $adminHse;
|
||
|
|
|
||
|
|
protected function setUp(): void
|
||
|
|
{
|
||
|
|
parent::setUp();
|
||
|
|
$this->seed(RolePermissionSeeder::class);
|
||
|
|
|
||
|
|
$this->adminQma = User::factory()->create([
|
||
|
|
'username' => 'admin_qma',
|
||
|
|
'role' => 'qma_admin',
|
||
|
|
]);
|
||
|
|
$this->adminQma->assignRole('qma_admin');
|
||
|
|
|
||
|
|
$this->adminHse = User::factory()->create([
|
||
|
|
'username' => 'admin_hse',
|
||
|
|
'role' => 'hse_admin',
|
||
|
|
]);
|
||
|
|
$this->adminHse->assignRole('hse_admin');
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_admin_qma_can_upload_and_compress_lv_photo(): void
|
||
|
|
{
|
||
|
|
Storage::fake('public');
|
||
|
|
|
||
|
|
// Simulasi foto mentah resolusi tinggi dari kamera (1600x1200)
|
||
|
|
$file = UploadedFile::fake()->image('hilux.png', 1600, 1200);
|
||
|
|
|
||
|
|
$response = $this->actingAs($this->adminQma)->post(route('qma.vehicles.store'), [
|
||
|
|
'code' => 'LV-99',
|
||
|
|
'category' => 'light_vehicle',
|
||
|
|
'brand_type' => 'Toyota Hilux 4x4 Double Cabin',
|
||
|
|
'license_plate' => 'BH 9999 XX',
|
||
|
|
'division' => 'HSE',
|
||
|
|
'ownership' => 'PT Kerinci Merangin Hidro',
|
||
|
|
'status' => 'Operation',
|
||
|
|
'current_km' => 1500,
|
||
|
|
'photo' => $file,
|
||
|
|
]);
|
||
|
|
|
||
|
|
$response->assertRedirect();
|
||
|
|
$this->assertDatabaseHas('vehicles', [
|
||
|
|
'code' => 'LV-99',
|
||
|
|
'category' => 'light_vehicle',
|
||
|
|
]);
|
||
|
|
|
||
|
|
$vehicle = Vehicle::where('code', 'LV-99')->firstOrFail();
|
||
|
|
$this->assertNotNull($vehicle->photo_path);
|
||
|
|
Storage::disk('public')->assertExists($vehicle->photo_path);
|
||
|
|
|
||
|
|
// Verifikasi resolusi gambar telah dikompresi proporsional (max 800x800)
|
||
|
|
$savedContent = Storage::disk('public')->get($vehicle->photo_path);
|
||
|
|
$imageResource = @imagecreatefromstring($savedContent);
|
||
|
|
$this->assertNotFalse($imageResource);
|
||
|
|
$this->assertLessThanOrEqual(800, imagesx($imageResource));
|
||
|
|
$this->assertLessThanOrEqual(800, imagesy($imageResource));
|
||
|
|
imagedestroy($imageResource);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_updating_lv_photo_replaces_and_deletes_old_photo(): void
|
||
|
|
{
|
||
|
|
Storage::fake('public');
|
||
|
|
|
||
|
|
$oldFile = UploadedFile::fake()->image('old_car.png', 1000, 1000);
|
||
|
|
$this->actingAs($this->adminQma)->post(route('qma.vehicles.store'), [
|
||
|
|
'code' => 'LV-88',
|
||
|
|
'category' => 'light_vehicle',
|
||
|
|
'brand_type' => 'Mitsubishi Triton',
|
||
|
|
'license_plate' => 'BH 8888 XX',
|
||
|
|
'status' => 'Operation',
|
||
|
|
'current_km' => 2000,
|
||
|
|
'photo' => $oldFile,
|
||
|
|
]);
|
||
|
|
|
||
|
|
$vehicle = Vehicle::where('code', 'LV-88')->firstOrFail();
|
||
|
|
$oldPath = $vehicle->photo_path;
|
||
|
|
Storage::disk('public')->assertExists($oldPath);
|
||
|
|
|
||
|
|
// Update dengan foto baru
|
||
|
|
$newFile = UploadedFile::fake()->image('new_car.png', 1200, 800);
|
||
|
|
$updateResponse = $this->actingAs($this->adminQma)->post(route('qma.vehicles.update', $vehicle->id), [
|
||
|
|
'code' => 'LV-88',
|
||
|
|
'category' => 'light_vehicle',
|
||
|
|
'brand_type' => 'Mitsubishi Triton Ultimate',
|
||
|
|
'license_plate' => 'BH 8888 XX',
|
||
|
|
'status' => 'Standby',
|
||
|
|
'current_km' => 2500,
|
||
|
|
'photo' => $newFile,
|
||
|
|
]);
|
||
|
|
|
||
|
|
$updateResponse->assertRedirect();
|
||
|
|
$vehicle->refresh();
|
||
|
|
$newPath = $vehicle->photo_path;
|
||
|
|
|
||
|
|
$this->assertNotEquals($oldPath, $newPath);
|
||
|
|
Storage::disk('public')->assertMissing($oldPath);
|
||
|
|
Storage::disk('public')->assertExists($newPath);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_deleting_vehicle_deletes_its_photo_from_storage(): void
|
||
|
|
{
|
||
|
|
Storage::fake('public');
|
||
|
|
|
||
|
|
$file = UploadedFile::fake()->image('car_to_delete.png', 800, 600);
|
||
|
|
$this->actingAs($this->adminQma)->post(route('qma.vehicles.store'), [
|
||
|
|
'code' => 'LV-DEL',
|
||
|
|
'category' => 'light_vehicle',
|
||
|
|
'brand_type' => 'Isuzu D-Max',
|
||
|
|
'status' => 'Standby',
|
||
|
|
'current_km' => 500,
|
||
|
|
'photo' => $file,
|
||
|
|
]);
|
||
|
|
|
||
|
|
$vehicle = Vehicle::where('code', 'LV-DEL')->firstOrFail();
|
||
|
|
$photoPath = $vehicle->photo_path;
|
||
|
|
Storage::disk('public')->assertExists($photoPath);
|
||
|
|
|
||
|
|
$deleteResponse = $this->actingAs($this->adminQma)->delete(route('qma.vehicles.destroy', $vehicle->id));
|
||
|
|
$deleteResponse->assertRedirect();
|
||
|
|
|
||
|
|
$this->assertDatabaseMissing('vehicles', ['id' => $vehicle->id]);
|
||
|
|
Storage::disk('public')->assertMissing($photoPath);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_admin_qma_can_upload_photo_for_support_unit(): void
|
||
|
|
{
|
||
|
|
Storage::fake('public');
|
||
|
|
|
||
|
|
// Buat kategori Unit Support terlebih dahulu
|
||
|
|
$category = Vehicle::create([
|
||
|
|
'code' => 'EX01',
|
||
|
|
'category' => 'unit_support',
|
||
|
|
'brand_type' => 'Komatsu PC200-8',
|
||
|
|
'unit_category' => 'Excavator',
|
||
|
|
'status' => 'Operation',
|
||
|
|
'current_km' => 0,
|
||
|
|
]);
|
||
|
|
|
||
|
|
$file = UploadedFile::fake()->image('excavator.png', 1500, 1000);
|
||
|
|
|
||
|
|
$response = $this->actingAs($this->adminQma)->post(route('qma.vehicles.support.units.store', $category->id), [
|
||
|
|
'unit_number' => '001',
|
||
|
|
'serial_number' => 'SN-EXC-999',
|
||
|
|
'status' => 'Operation',
|
||
|
|
'current_km' => 120,
|
||
|
|
'location' => 'Main Dam',
|
||
|
|
'photo' => $file,
|
||
|
|
]);
|
||
|
|
|
||
|
|
$response->assertRedirect();
|
||
|
|
$unit = Vehicle::where('code', 'EX01-001')->firstOrFail();
|
||
|
|
$this->assertNotNull($unit->photo_path);
|
||
|
|
Storage::disk('public')->assertExists($unit->photo_path);
|
||
|
|
|
||
|
|
$savedContent = Storage::disk('public')->get($unit->photo_path);
|
||
|
|
$imageResource = @imagecreatefromstring($savedContent);
|
||
|
|
$this->assertNotFalse($imageResource);
|
||
|
|
$this->assertLessThanOrEqual(800, imagesx($imageResource));
|
||
|
|
$this->assertLessThanOrEqual(800, imagesy($imageResource));
|
||
|
|
imagedestroy($imageResource);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_admin_hse_can_upload_and_compress_employee_photo(): void
|
||
|
|
{
|
||
|
|
Storage::fake('public');
|
||
|
|
|
||
|
|
// Simulasi foto avatar mentah ukuran 1200x1200
|
||
|
|
$file = UploadedFile::fake()->image('profile.png', 1200, 1200);
|
||
|
|
|
||
|
|
$response = $this->actingAs($this->adminHse)->post(route('hse.employees.store'), [
|
||
|
|
'nik' => 'KMH-PHOTO-01',
|
||
|
|
'name' => 'Budi Santoso',
|
||
|
|
'unit_kerja' => 'Pembangkit',
|
||
|
|
'division' => 'Operasi',
|
||
|
|
'position' => 'Driver Operasional',
|
||
|
|
'is_operator' => '1',
|
||
|
|
'simper_expired_at' => now()->addYear()->toDateString(),
|
||
|
|
'photo' => $file,
|
||
|
|
]);
|
||
|
|
|
||
|
|
$response->assertRedirect();
|
||
|
|
$this->assertDatabaseHas('employees', [
|
||
|
|
'nik' => 'KMH-PHOTO-01',
|
||
|
|
'name' => 'Budi Santoso',
|
||
|
|
]);
|
||
|
|
|
||
|
|
$employee = Employee::where('nik', 'KMH-PHOTO-01')->firstOrFail();
|
||
|
|
$this->assertNotNull($employee->photo_path);
|
||
|
|
Storage::disk('public')->assertExists($employee->photo_path);
|
||
|
|
|
||
|
|
// Verifikasi resolusi avatar dikompresi proporsional (max 500x500 untuk avatar)
|
||
|
|
$savedContent = Storage::disk('public')->get($employee->photo_path);
|
||
|
|
$imageResource = @imagecreatefromstring($savedContent);
|
||
|
|
$this->assertNotFalse($imageResource);
|
||
|
|
$this->assertLessThanOrEqual(500, imagesx($imageResource));
|
||
|
|
$this->assertLessThanOrEqual(500, imagesy($imageResource));
|
||
|
|
imagedestroy($imageResource);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_updating_employee_photo_replaces_and_deletes_old_photo(): void
|
||
|
|
{
|
||
|
|
Storage::fake('public');
|
||
|
|
|
||
|
|
$oldFile = UploadedFile::fake()->image('old_avatar.png', 600, 600);
|
||
|
|
$this->actingAs($this->adminHse)->post(route('hse.employees.store'), [
|
||
|
|
'nik' => 'KMH-PHOTO-02',
|
||
|
|
'name' => 'Dewi Lestari',
|
||
|
|
'unit_kerja' => 'Umum',
|
||
|
|
'division' => 'Keuangan',
|
||
|
|
'position' => 'Staff Finance',
|
||
|
|
'is_operator' => '0',
|
||
|
|
'photo' => $oldFile,
|
||
|
|
]);
|
||
|
|
|
||
|
|
$employee = Employee::where('nik', 'KMH-PHOTO-02')->firstOrFail();
|
||
|
|
$oldPath = $employee->photo_path;
|
||
|
|
Storage::disk('public')->assertExists($oldPath);
|
||
|
|
|
||
|
|
// Update dengan avatar baru
|
||
|
|
$newFile = UploadedFile::fake()->image('new_avatar.png', 800, 800);
|
||
|
|
$updateResponse = $this->actingAs($this->adminHse)->post(route('hse.employees.update', $employee->id), [
|
||
|
|
'nik' => 'KMH-PHOTO-02',
|
||
|
|
'name' => 'Dewi Lestari, S.E.',
|
||
|
|
'unit_kerja' => 'Umum',
|
||
|
|
'division' => 'Keuangan',
|
||
|
|
'position' => 'Senior Staff Finance',
|
||
|
|
'is_operator' => '0',
|
||
|
|
'photo' => $newFile,
|
||
|
|
]);
|
||
|
|
|
||
|
|
$updateResponse->assertRedirect();
|
||
|
|
$employee->refresh();
|
||
|
|
$newPath = $employee->photo_path;
|
||
|
|
|
||
|
|
$this->assertNotEquals($oldPath, $newPath);
|
||
|
|
Storage::disk('public')->assertMissing($oldPath);
|
||
|
|
Storage::disk('public')->assertExists($newPath);
|
||
|
|
}
|
||
|
|
}
|