Files
alrahma_sunday_school_api/tests/Feature/Api/V1/ClassProgress/ClassProgressControllerTest.php
T
root 90f9857b06
API CI/CD / Validate (composer + pint) (push) Successful in 3m7s
API CI/CD / Test (PHPUnit) (push) Failing after 5m46s
API CI/CD / Build frontend assets (push) Successful in 1m2s
API CI/CD / Security audit (push) Failing after 49s
API CI/CD / Deploy to shared hosting (PHP) (push) Has been skipped
security fix and fix parent pages
2026-07-06 02:14:16 -04:00

399 lines
14 KiB
PHP

<?php
namespace Tests\Feature\Api\V1\ClassProgress;
use App\Models\ClassProgressAttachment;
use App\Models\ClassProgressReport;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class ClassProgressControllerTest extends TestCase
{
use RefreshDatabase;
public function test_meta_returns_sections(): void
{
$user = $this->createUser();
Sanctum::actingAs($user);
$response = $this->getJson('/api/v1/class-progress/meta');
$response->assertOk();
$response->assertJsonPath('status', true);
$this->assertNotEmpty($response->json('data.subject_sections'));
}
public function test_index_returns_paginated_reports(): void
{
$user = $this->createUser();
$this->seedClassSection();
ClassProgressReport::factory()->count(2)->create([
'teacher_id' => $user->id,
'class_section_id' => 101,
]);
Sanctum::actingAs($user);
$response = $this->getJson('/api/v1/class-progress');
$response->assertOk();
$response->assertJsonPath('status', true);
$this->assertCount(2, $response->json('data.items'));
}
public function test_admin_grouped_index_accepts_school_year_id_and_includes_legacy_rows(): void
{
$admin = $this->createUser('admin@example.com');
$this->assignRole($admin->id, 'administrator');
$this->seedClassSection();
DB::table('classSection')->where('class_section_id', 101)->update([
'semester' => '',
'school_year' => null,
]);
$schoolYearId = DB::table('school_years')->insertGetId([
'name' => '2025-2026',
'start_date' => '2025-08-01',
'end_date' => '2026-07-31',
'status' => 'active',
'is_current' => 1,
'created_at' => now(),
'updated_at' => now(),
]);
ClassProgressReport::factory()->create([
'teacher_id' => $admin->id,
'class_section_id' => 101,
'school_year' => null,
'semester' => null,
'week_start' => '2026-02-08',
'week_end' => '2026-02-14',
]);
Sanctum::actingAs($admin);
$response = $this->getJson('/api/v1/class-progress?school_year_id='.$schoolYearId.'&group_by_week=1');
$response->assertOk();
$response->assertJsonPath('status', true);
$this->assertCount(1, $response->json('data.items'));
$administratorAlias = $this->getJson('/api/v1/administrator/progress?school_year_id='.$schoolYearId.'&group_by_week=1');
$administratorAlias->assertOk();
$this->assertCount(1, $administratorAlias->json('data.items'));
$adminAlias = $this->getJson('/api/v1/admin/progress?school_year_id='.$schoolYearId.'&group_by_week=1');
$adminAlias->assertOk();
$this->assertCount(1, $adminAlias->json('data.items'));
}
public function test_parent_progress_returns_reports_for_enrolled_sections(): void
{
$parent = $this->createUser('parent@example.com');
$teacher = $this->createUser('teacher-for-parent-progress@example.com');
$this->assignRole($parent->id, 'parent');
$this->seedClassSection();
DB::table('classSection')->where('class_section_id', 101)->update([
'semester' => '',
'school_year' => null,
]);
DB::table('students')->insert([
'id' => 301,
'school_id' => '301',
'firstname' => 'Student',
'lastname' => 'One',
'is_active' => 1,
'is_new' => 0,
'parent_id' => $parent->id,
'semester' => 'Fall',
'school_year' => '2025-2026',
]);
DB::table('enrollments')->insert([
'student_id' => 301,
'class_section_id' => 101,
'parent_id' => $parent->id,
'enrollment_status' => 'enrolled',
'is_withdrawn' => 0,
'admission_status' => 'accepted',
'semester' => 'Fall',
'school_year' => '2025-2026',
'created_at' => now(),
'updated_at' => now(),
]);
$report = ClassProgressReport::factory()->create([
'teacher_id' => $teacher->id,
'class_section_id' => 101,
'school_year' => null,
'semester' => null,
'week_start' => '2026-02-08',
'week_end' => '2026-02-14',
]);
ClassProgressAttachment::factory()->create([
'report_id' => $report->id,
'file_path' => 'storage/class_material/sample.pdf',
'original_name' => 'sample.pdf',
]);
Sanctum::actingAs($parent);
$response = $this->getJson('/api/v1/parents/progress?school_year=2025-2026');
$response->assertOk();
$response->assertJsonPath('ok', true);
$this->assertCount(1, $response->json('data.items'));
$this->assertCount(1, $response->json('data.students'));
$this->assertSame($report->id, $response->json('data.items.0.id'));
$this->assertSame($report->id, $response->json('data.items.0.report_id'));
$this->assertNotEmpty($response->json('data.items.0.view_url'));
$this->assertNotEmpty($response->json('data.items.0.reports.Islamic Studies.attachments.0.download_url'));
$this->assertSame($report->id, $response->json('data.items.0.weekly_reports.0.id'));
$this->assertNotEmpty($response->json('data.items.0.weekly_reports.0.attachments.0.download_url'));
$sharedResponse = $this->getJson('/api/v1/class-progress?school_year=2025-2026&group_by_week=1');
$sharedResponse->assertOk();
$sharedResponse->assertJsonPath('status', true);
$this->assertCount(1, $sharedResponse->json('data.items'));
$this->assertSame($report->id, $sharedResponse->json('data.items.0.id'));
$this->assertSame($report->id, $sharedResponse->json('data.items.0.weekly_reports.0.id'));
$detailResponse = $this->getJson('/api/v1/class-progress/'.$report->id);
$detailResponse->assertOk();
$detailResponse->assertJsonPath('status', true);
$this->assertSame($report->id, $detailResponse->json('data.report.id'));
$this->assertNotEmpty($detailResponse->json('data.weekly_reports.0.attachments.0.download_url'));
$this->assertSame($report->id, $detailResponse->json('data.group.weekly_reports.0.id'));
$parentDetailResponse = $this->getJson('/api/v1/parents/progress/'.$report->id);
$parentDetailResponse->assertOk();
$parentDetailResponse->assertJsonPath('ok', true);
$parentDetailResponse->assertJsonPath('status', true);
$this->assertSame($report->id, $parentDetailResponse->json('data.report.id'));
$this->assertNotEmpty($parentDetailResponse->json('data.report.attachments.0.download_url'));
$this->assertSame($report->id, $parentDetailResponse->json('data.group.weekly_reports.0.id'));
}
public function test_store_creates_reports(): void
{
Storage::fake('public');
$user = $this->createUser();
$this->seedClassSection();
$this->assignTeacher($user->id, 101);
Sanctum::actingAs($user);
$payload = [
'class_section_id' => 101,
'week_start' => '2025-09-07',
'week_end' => '2025-09-13',
'covered_islamic' => 'Lesson A',
'covered_quran' => 'Lesson B',
'homework_islamic' => 'HW A',
'homework_quran' => 'HW B',
'unit_islamic' => ['Unit 1'],
'chapter_islamic' => ['Chapter A'],
'flags' => ['needs_support'],
];
$response = $this->postJson('/api/v1/class-progress', $payload);
$response->assertStatus(201);
$response->assertJsonPath('status', true);
$this->assertDatabaseCount('class_progress_reports', 2);
}
public function test_show_returns_weekly_reports(): void
{
$user = $this->createUser();
$this->seedClassSection();
$report = ClassProgressReport::factory()->create([
'teacher_id' => $user->id,
'class_section_id' => 101,
'week_start' => '2025-09-07',
'week_end' => '2025-09-13',
'subject' => 'Islamic Studies',
]);
ClassProgressReport::factory()->create([
'teacher_id' => $user->id,
'class_section_id' => 101,
'week_start' => '2025-09-07',
'week_end' => '2025-09-13',
'subject' => 'Quran/Arabic',
]);
Sanctum::actingAs($user);
$response = $this->getJson('/api/v1/class-progress/'.$report->id);
$response->assertOk();
$response->assertJsonPath('status', true);
$this->assertCount(2, $response->json('data.weekly_reports'));
}
public function test_update_modifies_report(): void
{
$user = $this->createUser();
$this->seedClassSection();
$report = ClassProgressReport::factory()->create([
'teacher_id' => $user->id,
'class_section_id' => 101,
]);
Sanctum::actingAs($user);
$response = $this->patchJson('/api/v1/class-progress/'.$report->id, [
'status' => 'behind',
]);
$response->assertOk();
$this->assertDatabaseHas('class_progress_reports', [
'id' => $report->id,
'status' => 'behind',
]);
}
public function test_destroy_deletes_report(): void
{
$user = $this->createUser();
$this->seedClassSection();
$report = ClassProgressReport::factory()->create([
'teacher_id' => $user->id,
'class_section_id' => 101,
]);
Sanctum::actingAs($user);
$response = $this->deleteJson('/api/v1/class-progress/'.$report->id);
$response->assertOk();
$this->assertDatabaseMissing('class_progress_reports', [
'id' => $report->id,
]);
}
public function test_authorization_blocks_other_teachers(): void
{
$owner = $this->createUser('teacher1@example.com');
$other = $this->createUser('teacher2@example.com');
$this->seedClassSection();
$report = ClassProgressReport::factory()->create([
'teacher_id' => $owner->id,
'class_section_id' => 101,
]);
Sanctum::actingAs($other);
$response = $this->getJson('/api/v1/class-progress/'.$report->id);
$response->assertStatus(403);
}
public function test_validation_rejects_invalid_payload(): void
{
$user = $this->createUser();
Sanctum::actingAs($user);
$response = $this->postJson('/api/v1/class-progress', [
'class_section_id' => 'invalid',
]);
$response->assertStatus(422);
}
public function test_attachment_download_returns_file(): void
{
Storage::fake('public');
Storage::disk('public')->put('class_material/sample.pdf', 'content');
$user = $this->createUser();
$this->seedClassSection();
$report = ClassProgressReport::factory()->create([
'teacher_id' => $user->id,
'class_section_id' => 101,
]);
$attachment = ClassProgressAttachment::factory()->create([
'report_id' => $report->id,
'file_path' => 'storage/class_material/sample.pdf',
]);
Sanctum::actingAs($user);
$response = $this->get('/api/v1/class-progress/attachments/'.$attachment->id);
$response->assertOk();
}
private function seedClassSection(): void
{
DB::table('classSection')->insert([
'id' => 1,
'class_id' => 1,
'class_section_id' => 101,
'class_section_name' => '1-A',
'semester' => 'Fall',
'school_year' => '2025-2026',
]);
}
private function assignTeacher(int $teacherId, int $classSectionId): void
{
DB::table('teacher_class')->insert([
'class_section_id' => $classSectionId,
'teacher_id' => $teacherId,
'position' => 'main',
'semester' => 'Fall',
'school_year' => '2025-2026',
'created_at' => now(),
'updated_at' => now(),
]);
}
private function createUser(string $email = 'teacher@example.com'): User
{
DB::table('users')->insert([
'school_id' => 1,
'firstname' => 'Teacher',
'lastname' => 'User',
'cellphone' => '5555555555',
'email' => $email,
'address_street' => '123 Main',
'city' => 'City',
'state' => 'ST',
'zip' => '12345',
'accept_school_policy' => 1,
'is_verified' => 1,
'status' => 'Active',
'is_suspended' => 0,
'failed_attempts' => 0,
'password' => bcrypt('secret'),
'semester' => 'Fall',
'school_year' => '2025-2026',
]);
return User::query()->where('email', $email)->firstOrFail();
}
private function assignRole(int $userId, string $roleName): void
{
$roleId = DB::table('roles')->insertGetId([
'name' => $roleName,
'slug' => $roleName,
'dashboard_route' => $roleName === 'parent' ? 'parent/dashboard' : 'administrator/administratordashboard',
'priority' => 100,
'is_active' => 1,
'created_at' => now(),
'updated_at' => now(),
]);
DB::table('user_roles')->insert([
'user_id' => $userId,
'role_id' => $roleId,
'created_at' => now(),
'updated_at' => now(),
]);
}
}