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_legacy_teacher_submit_meta_uses_requested_year_and_legacy_curriculum_units(): void { $user = $this->createUser(); DB::table('configuration')->updateOrInsert( ['config_key' => 'school_year'], ['config_value' => '2024-2025'] ); DB::table('configuration')->updateOrInsert( ['config_key' => 'semester'], ['config_value' => 'Spring'] ); DB::table('school_years')->updateOrInsert([ 'name' => '2025-2026', ], [ 'start_date' => '2025-08-01', 'end_date' => '2026-07-31', 'status' => 'active', 'is_current' => 1, 'created_at' => now(), 'updated_at' => now(), ]); DB::table('classes')->insert([ 'id' => 9, 'class_name' => 'Grade 9', 'schedule' => 'Sunday', 'capacity' => 25, 'semester' => 'Fall', 'school_year' => '2025-2026', ]); DB::table('classSection')->insert([ 'id' => 9, 'class_id' => 9, 'class_section_id' => 909, 'class_section_name' => '9-A', 'semester' => 'Fall', 'school_year' => '2025-2026', ]); DB::table('teacher_class')->insert([ 'class_section_id' => 909, 'teacher_id' => $user->id, 'position' => 'main', 'semester' => 'Fall', 'school_year' => '2025-2026', 'created_at' => now(), 'updated_at' => now(), ]); DB::table('classSection')->insert([ 'id' => 8, 'class_id' => 8, 'class_section_id' => 808, 'class_section_name' => '8-A', 'semester' => 'Fall', 'school_year' => '2024-2025', ]); DB::table('teacher_class')->insert([ 'class_section_id' => 808, 'teacher_id' => $user->id, 'position' => 'main', 'semester' => 'Fall', 'school_year' => '2024-2025', 'created_at' => now(), 'updated_at' => now(), ]); DB::table('subject_curriculum_items')->insert([ 'class_id' => 9, 'school_year' => null, 'subject' => 'islamic', 'unit_number' => 1, 'unit_title' => 'Faith', 'chapter_name' => 'Intro', 'created_at' => now(), 'updated_at' => now(), ]); DB::table('subject_curriculum_items')->insert([ 'class_id' => 9, 'school_year' => null, 'subject' => 'Quran/Arabic', 'unit_number' => 2, 'unit_title' => 'Surah Review', 'chapter_name' => 'Al-Fatiha', 'created_at' => now(), 'updated_at' => now(), ]); Sanctum::actingAs($user); $response = $this->getJson('/api/v1/teacher/class-progress-submit?school_year=2025-2026&semester=Fall&class_section_id=909'); $response->assertOk(); $response->assertJsonPath('status', true); $response->assertJsonPath('data.defaults.school_year', '2025-2026'); $response->assertJsonPath('data.defaults.semester', 'Fall'); $response->assertJsonPath('data.defaults.class_section_id', 909); $this->assertCount(1, $response->json('data.classes')); $response->assertJsonPath('data.classes.0.class_section_id', 909); $response->assertJsonPath('data.curriculum.islamic.0.chapter_name', 'Intro'); $response->assertJsonPath('data.unit_options.islamic.0.label', 'Unit 1 - Faith'); $response->assertJsonPath('data.curriculum.quran.0.chapter_name', 'Al-Fatiha'); $response->assertJsonPath('data.unit_options.quran.0.label', 'Unit 2 - Surah Review'); } 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_teacher_history_is_scoped_to_assigned_class_sections(): void { $teacher = $this->createUser(); $otherTeacher = $this->createUser('other-history-teacher@example.com'); $this->assignRole($teacher->id, 'teacher'); $this->seedClassSection(); DB::table('classSection')->insert([ 'id' => 2, 'class_id' => 2, 'class_section_id' => 202, 'class_section_name' => '2-A', 'semester' => 'Fall', 'school_year' => '2025-2026', ]); $this->assignTeacher($teacher->id, 1); $this->assignTeacher($teacher->id, 202); $assignedOwnReport = ClassProgressReport::factory()->create([ 'teacher_id' => $teacher->id, 'class_section_id' => 101, 'school_year' => '2025-2026', 'week_start' => '2025-09-07', ]); $assignedSectionReport = ClassProgressReport::factory()->create([ 'teacher_id' => $otherTeacher->id, 'class_section_id' => 101, 'school_year' => '2025-2026', 'week_start' => '2025-09-14', ]); $otherSectionReport = ClassProgressReport::factory()->create([ 'teacher_id' => $teacher->id, 'class_section_id' => 202, 'school_year' => '2025-2026', 'week_start' => '2025-09-21', ]); Sanctum::actingAs($teacher); $response = $this->getJson('/api/v1/teacher/class-progress-history?school_year=2025-2026&class_section_id=1'); $response->assertOk(); $ids = collect($response->json('data.items'))->pluck('id')->all(); $this->assertContains($assignedOwnReport->id, $ids); $this->assertContains($assignedSectionReport->id, $ids); $this->assertNotContains($otherSectionReport->id, $ids); $detailResponse = $this->getJson('/api/v1/class-progress/'.$assignedSectionReport->id); $detailResponse->assertOk(); $detailResponse->assertJsonPath('data.report.id', $assignedSectionReport->id); $unfilteredResponse = $this ->withSession(['class_section_id' => 202]) ->getJson('/api/v1/teacher/class-progress-history?school_year=2025-2026'); $unfilteredResponse->assertOk(); $unfilteredIds = collect($unfilteredResponse->json('data.items'))->pluck('id')->all(); $this->assertNotContains($assignedOwnReport->id, $unfilteredIds); $this->assertNotContains($assignedSectionReport->id, $unfilteredIds); $this->assertContains($otherSectionReport->id, $unfilteredIds); } public function test_teacher_history_accepts_legacy_progress_rows_saved_with_section_pk(): void { $teacher = $this->createUser(); $otherTeacher = $this->createUser('legacy-pk-history-teacher@example.com'); $this->assignRole($teacher->id, 'teacher_assistant'); $this->seedClassSection(); $this->assignTeacher($teacher->id, 101); $legacyPkReport = ClassProgressReport::factory()->create([ 'teacher_id' => $otherTeacher->id, 'class_section_id' => 1, 'school_year' => '2025-2026', 'week_start' => '2025-09-28', ]); Sanctum::actingAs($teacher); $response = $this->getJson('/api/v1/teacher/class-progress-history?school_year=2025-2026&class_section_id=101'); $response->assertOk(); $ids = collect($response->json('data.items'))->pluck('id')->all(); $this->assertContains($legacyPkReport->id, $ids); } public function test_teacher_history_without_section_uses_logged_in_teacher_assignment(): void { $teacher = $this->createUser(); $otherTeacher = $this->createUser('assignment-history-teacher@example.com'); $this->assignRole($teacher->id, 'teacher'); $this->seedClassSection(); DB::table('classSection')->insert([ 'id' => 2, 'class_id' => 2, 'class_section_id' => 202, 'class_section_name' => '2-A', 'semester' => 'Fall', 'school_year' => '2025-2026', ]); $this->assignTeacher($teacher->id, 1); $sameSectionReport = ClassProgressReport::factory()->create([ 'teacher_id' => $otherTeacher->id, 'class_section_id' => 101, 'school_year' => '2025-2026', 'week_start' => '2025-10-05', ]); $wrongSectionReport = ClassProgressReport::factory()->create([ 'teacher_id' => $otherTeacher->id, 'class_section_id' => 202, 'school_year' => '2025-2026', 'week_start' => '2025-10-12', ]); Sanctum::actingAs($teacher); $response = $this->getJson('/api/v1/teacher/class-progress-history?school_year=2025-2026'); $response->assertOk(); $ids = collect($response->json('data.items'))->pluck('id')->all(); $this->assertContains($sameSectionReport->id, $ids); $this->assertNotContains($wrongSectionReport->id, $ids); } public function test_teacher_history_without_section_does_not_422_for_multiple_assignments(): void { $teacher = $this->createUser(); $otherTeacher = $this->createUser('multi-assignment-history-teacher@example.com'); $this->assignRole($teacher->id, 'teacher'); $this->seedClassSection(); DB::table('classSection')->insert([ 'id' => 2, 'class_id' => 2, 'class_section_id' => 202, 'class_section_name' => '2-A', 'semester' => 'Fall', 'school_year' => '2025-2026', ]); $this->assignTeacher($teacher->id, 1); $this->assignTeacher($teacher->id, 202); $primarySectionReport = ClassProgressReport::factory()->create([ 'teacher_id' => $otherTeacher->id, 'class_section_id' => 101, 'school_year' => '2025-2026', 'week_start' => '2025-10-19', ]); $otherSectionReport = ClassProgressReport::factory()->create([ 'teacher_id' => $otherTeacher->id, 'class_section_id' => 202, 'school_year' => '2025-2026', 'week_start' => '2025-10-26', ]); Sanctum::actingAs($teacher); $response = $this->getJson('/api/v1/teacher/class-progress-history?school_year=2025-2026&per_page=100&sort_by=week_start&sort_dir=desc'); $response->assertOk(); $ids = collect($response->json('data.items'))->pluck('id')->all(); $this->assertContains($primarySectionReport->id, $ids); $this->assertNotContains($otherSectionReport->id, $ids); } public function test_admin_grouped_index_uses_school_year_name_and_includes_legacy_rows(): void { $admin = $this->createUser('admin@example.com'); $this->assignRole($admin->id, 'administrator'); DB::table('configuration')->updateOrInsert( ['config_key' => 'semester'], ['config_value' => 'Fall'] ); $this->seedClassSection(); DB::table('classSection')->where('class_section_id', 101)->update([ 'semester' => '', 'school_year' => null, ]); DB::table('school_years')->updateOrInsert([ '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=2025-2026&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=2025-2026&group_by_week=1'); $administratorAlias->assertOk(); $this->assertCount(1, $administratorAlias->json('data.items')); $adminAlias = $this->getJson('/api/v1/admin/progress?school_year=2025-2026&group_by_week=1'); $adminAlias->assertOk(); $this->assertCount(1, $adminAlias->json('data.items')); } public function test_index_rejects_school_year_id_context(): void { $user = $this->createUser(); Sanctum::actingAs($user); $this->getJson('/api/v1/class-progress?school_year_id=1') ->assertStatus(422); } 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'], 'school_year' => '2025-2026', ]; $response = $this->postJson('/api/v1/class-progress', $payload); $response->assertStatus(201); $response->assertJsonPath('status', true); $this->assertDatabaseCount('class_progress_reports', 2); } public function test_teacher_store_uses_logged_in_teacher_assignment_for_section(): void { Storage::fake('public'); $user = $this->createUser(); $this->assignRole($user->id, 'teacher'); $this->seedClassSection(); DB::table('classSection')->insert([ 'id' => 2, 'class_id' => 2, 'class_section_id' => 202, 'class_section_name' => '2-A', 'semester' => 'Fall', 'school_year' => '2025-2026', ]); $this->assignTeacher($user->id, 101); Sanctum::actingAs($user); $payload = [ 'class_section_id' => 202, 'week_start' => '2025-09-07', 'week_end' => '2025-09-13', 'covered_islamic' => 'Lesson A', 'covered_quran' => 'Lesson B', 'unit_islamic' => ['Unit 1'], 'chapter_islamic' => ['Chapter A'], 'school_year' => '2025-2026', ]; $response = $this->postJson('/api/v1/teacher/class-progress-submit', $payload); $response->assertStatus(201); $this->assertDatabaseHas('class_progress_reports', [ 'teacher_id' => $user->id, 'class_section_id' => 101, 'subject' => 'Islamic Studies', ]); $this->assertDatabaseMissing('class_progress_reports', [ 'teacher_id' => $user->id, 'class_section_id' => 202, ]); } public function test_teacher_store_without_section_uses_logged_in_teacher_assignment(): void { Storage::fake('public'); $user = $this->createUser(); $this->assignRole($user->id, 'teacher'); $this->seedClassSection(); $this->assignTeacher($user->id, 101); Sanctum::actingAs($user); $response = $this->postJson('/api/v1/teacher/class-progress-submit', [ 'week_start' => '2025-09-07', 'week_end' => '2025-09-13', 'covered_islamic' => 'Lesson A', 'unit_islamic' => ['Unit 1'], 'chapter_islamic' => ['Chapter A'], 'school_year' => '2025-2026', ]); $response->assertStatus(201); $this->assertDatabaseHas('class_progress_reports', [ 'teacher_id' => $user->id, 'class_section_id' => 101, 'subject' => 'Islamic Studies', ]); } 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', 'school_year' => '2025-2026', ]); $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.'?school_year=2025-2026'); $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('school_years')->updateOrInsert([ 'name' => '2025-2026', ], [ 'start_date' => '2025-08-01', 'end_date' => '2026-07-31', 'status' => 'active', 'is_current' => 1, 'created_at' => now(), 'updated_at' => now(), ]); 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(), ]); } }