Files
database-kesiswaan/tests/Feature/MaintenanceResetDataTest.php

194 lines
7.3 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\AcademicYear;
use App\Models\Department;
use App\Models\DocumentType;
use App\Models\SchoolClass;
use App\Models\Student;
use App\Models\StudentAchievement;
use App\Models\StudentDocument;
use App\Models\StudentExit;
use App\Models\StudentGraduation;
use App\Models\StudentTransfer;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;
class MaintenanceResetDataTest extends TestCase
{
use RefreshDatabase;
public function test_reset_data_index_shows_resolved_target_preview(): void
{
$admin = User::factory()->create([
'role' => 'admin',
'password' => Hash::make('secret-reset'),
]);
$response = $this->actingAs($admin)->get(route('settings.reset-data.index'));
$response->assertOk();
$response->assertSee('Ikut dibersihkan', false);
$response->assertSee('Prestasi Siswa + file storage');
$response->assertSee('Arsip Kelulusan Siswa + file storage');
$response->assertSee('Pilih Semua');
}
public function test_reset_students_also_cleans_related_records_and_storage_files(): void
{
Storage::fake('public');
$admin = User::factory()->create([
'role' => 'admin',
'password' => Hash::make('secret-reset'),
]);
$year = AcademicYear::create([
'name' => '2025/2026',
'start_year' => 2025,
'end_year' => 2026,
'is_active' => true,
]);
$department = Department::create([
'name' => 'Rekayasa Perangkat Lunak',
'code' => 'RPL',
]);
$class = SchoolClass::create([
'name' => 'X RPL 1',
'level' => 'X',
'academic_year_id' => $year->id,
'department_id' => $department->id,
'capacity' => 36,
]);
$documentType = DocumentType::create([
'name' => 'Kartu Keluarga',
'code' => 'KK',
'description' => 'Dokumen keluarga',
'is_required' => true,
]);
$student = Student::create([
'student_number' => 'S-9001',
'nisn' => '1234567890',
'full_name' => 'Siswa Reset Total',
'gender' => 'L',
'academic_year_id' => $year->id,
'department_id' => $department->id,
'class_id' => $class->id,
'status' => 'active',
]);
\DB::table('student_parents')->insert([
'student_id' => $student->id,
'relation_type' => 'father',
'name' => 'Ayah Reset',
'created_at' => now(),
'updated_at' => now(),
]);
\DB::table('student_academic_histories')->insert([
'student_id' => $student->id,
'academic_year_id' => $year->id,
'department_id' => $department->id,
'class_id' => $class->id,
'status' => 'active',
'start_date' => now()->toDateString(),
'is_current' => true,
'created_at' => now(),
'updated_at' => now(),
]);
Storage::disk('public')->put('student-documents/sample.pdf', 'doc');
StudentDocument::create([
'student_id' => $student->id,
'document_type_id' => $documentType->id,
'file_name' => 'sample.pdf',
'file_path' => 'student-documents/sample.pdf',
'file_extension' => 'pdf',
'mime_type' => 'application/pdf',
'file_size' => 3,
]);
Storage::disk('public')->put('student-achievements/certificates/cert.pdf', 'cert');
Storage::disk('public')->put('student-achievements/activity-photos/photo.jpg', 'photo');
StudentAchievement::create([
'student_id' => $student->id,
'achievement_name' => 'Juara 1',
'certificate_file' => 'student-achievements/certificates/cert.pdf',
'activity_photo' => 'student-achievements/activity-photos/photo.jpg',
]);
Storage::disk('public')->put("student-exit-letters/{$student->id}/exit.pdf", 'exit');
StudentExit::create([
'student_id' => $student->id,
'full_name' => $student->full_name,
'academic_year_id' => $year->id,
'department_id' => $department->id,
'class_id' => $class->id,
'status' => 'dropped',
'exit_letter_file_name' => 'exit.pdf',
'exit_letter_file_path' => "student-exit-letters/{$student->id}/exit.pdf",
'exit_letter_mime_type' => 'application/pdf',
'exit_letter_file_size' => 4,
]);
Storage::disk('public')->put("student-transfer-letters/{$student->id}/transfer.pdf", 'transfer');
StudentTransfer::create([
'student_id' => $student->id,
'full_name' => $student->full_name,
'academic_year_id' => $year->id,
'department_id' => $department->id,
'class_id' => $class->id,
'status' => 'transferred',
'transfer_letter_file_name' => 'transfer.pdf',
'transfer_letter_file_path' => "student-transfer-letters/{$student->id}/transfer.pdf",
'transfer_letter_mime_type' => 'application/pdf',
'transfer_letter_file_size' => 8,
]);
Storage::disk('public')->put("student-graduation-documents/{$student->id}/skl/skl.pdf", 'skl');
StudentGraduation::create([
'student_id' => $student->id,
'full_name' => $student->full_name,
'academic_year_id' => $year->id,
'department_id' => $department->id,
'class_id' => $class->id,
'status' => 'graduated',
'skl_file_name' => 'skl.pdf',
'skl_file_path' => "student-graduation-documents/{$student->id}/skl/skl.pdf",
'skl_mime_type' => 'application/pdf',
'skl_file_size' => 3,
]);
$response = $this->actingAs($admin)->post(route('settings.reset-data.execute'), [
'targets' => ['students'],
'admin_password' => 'secret-reset',
]);
$response->assertRedirect(route('settings.reset-data.index'));
$this->assertDatabaseCount('students', 0);
$this->assertDatabaseCount('student_parents', 0);
$this->assertDatabaseCount('student_documents', 0);
$this->assertDatabaseCount('student_academic_histories', 0);
$this->assertDatabaseCount('student_achievements', 0);
$this->assertDatabaseCount('student_exits', 0);
$this->assertDatabaseCount('student_transfers', 0);
$this->assertDatabaseCount('student_graduations', 0);
Storage::disk('public')->assertMissing('student-documents/sample.pdf');
Storage::disk('public')->assertMissing('student-achievements/certificates/cert.pdf');
Storage::disk('public')->assertMissing('student-achievements/activity-photos/photo.jpg');
Storage::disk('public')->assertMissing("student-exit-letters/{$student->id}/exit.pdf");
Storage::disk('public')->assertMissing("student-transfer-letters/{$student->id}/transfer.pdf");
Storage::disk('public')->assertMissing("student-graduation-documents/{$student->id}/skl/skl.pdf");
}
}