update api and add more features

This commit is contained in:
root
2026-06-04 02:24:41 -04:00
parent fa6c9519a0
commit 4e33882ac7
131 changed files with 34596 additions and 340 deletions
@@ -0,0 +1,86 @@
<?php
namespace Tests\Unit\Services\Promotions;
use App\Models\LevelProgression;
use App\Services\Promotions\LevelProgressionService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
class LevelProgressionServiceTest extends TestCase
{
use RefreshDatabase;
public function test_seed_defaults_creates_baseline_levels(): void
{
$service = new LevelProgressionService();
$created = $service->seedDefaults();
$this->assertGreaterThan(0, $created);
$this->assertSame($created, LevelProgression::query()->count());
$this->assertDatabaseHas('level_progressions', [
'current_level_name' => 'Grade 3',
'next_level_name' => 'Grade 4',
]);
$this->assertDatabaseHas('level_progressions', [
'current_level_name' => 'Youth',
'is_terminal' => 1,
]);
}
public function test_resolve_by_class_id_uses_mapping_when_present(): void
{
DB::table('classes')->insert([
['id' => 7, 'class_name' => 'Grade 3', 'school_year' => '2025-2026', 'semester' => 'Fall', 'capacity' => 0],
['id' => 8, 'class_name' => 'Grade 4', 'school_year' => '2025-2026', 'semester' => 'Fall', 'capacity' => 0],
]);
$service = new LevelProgressionService();
$service->seedDefaults();
$resolved = $service->resolveByCurrentClassId(7);
$this->assertNotNull($resolved);
$this->assertSame('Grade 3', $resolved['current_level_name']);
$this->assertSame('Grade 4', $resolved['next_level_name']);
$this->assertSame(8, $resolved['next_level_id']);
$this->assertFalse($resolved['is_terminal']);
}
public function test_resolve_falls_back_to_class_id_plus_one_when_no_mapping(): void
{
DB::table('classes')->insert([
['id' => 100, 'class_name' => 'Custom A', 'school_year' => '2025-2026', 'semester' => 'Fall', 'capacity' => 0],
['id' => 101, 'class_name' => 'Custom B', 'school_year' => '2025-2026', 'semester' => 'Fall', 'capacity' => 0],
]);
$service = new LevelProgressionService();
$resolved = $service->resolveByCurrentClassId(100);
$this->assertNotNull($resolved);
$this->assertSame('Custom A', $resolved['current_level_name']);
$this->assertSame('Custom B', $resolved['next_level_name']);
$this->assertSame(101, $resolved['next_level_id']);
}
public function test_upsert_mapping_updates_existing_row(): void
{
$service = new LevelProgressionService();
$service->upsertMapping([
'current_level_name' => 'Grade 5',
'next_level_name' => 'Grade 6',
'order_index' => 70,
]);
$service->upsertMapping([
'current_level_name' => 'Grade 5',
'next_level_name' => 'Grade 6 (Updated)',
'order_index' => 75,
]);
$this->assertDatabaseHas('level_progressions', [
'current_level_name' => 'Grade 5',
'next_level_name' => 'Grade 6 (Updated)',
'order_index' => 75,
]);
$this->assertSame(1, LevelProgression::query()->where('current_level_name', 'Grade 5')->count());
}
}
@@ -0,0 +1,169 @@
<?php
namespace Tests\Unit\Services\Promotions;
use App\Models\StudentPromotionRecord;
use App\Services\Promotions\LevelProgressionService;
use App\Services\Promotions\PromotionAuditService;
use App\Services\Promotions\PromotionEligibilityService;
use App\Services\Promotions\PromotionStatusService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
class PromotionEligibilityServiceTest extends TestCase
{
use RefreshDatabase;
public function test_passing_student_becomes_awaiting_parent_enrollment(): void
{
$this->seedConfig();
$studentId = $this->seedStudent();
$this->seedClassChain();
$this->seedAssignment($studentId, 701, '2025-2026');
$this->seedScores($studentId, '2025-2026', 80.0, 75.0);
$service = $this->makeService();
$record = $service->evaluateStudent($studentId, '2025-2026', 1);
$this->assertNotNull($record);
$this->assertSame(StudentPromotionRecord::STATUS_AWAITING_PARENT, $record->promotion_status);
$this->assertTrue((bool) $record->passed_current_level);
$this->assertSame(77.5, (float) $record->final_average);
$this->assertSame('Grade 3', $record->current_level_name);
$this->assertSame('Grade 4', $record->promoted_level_name);
$this->assertSame('2026-2027', $record->next_school_year);
}
public function test_failing_student_becomes_repeated(): void
{
$this->seedConfig();
$studentId = $this->seedStudent();
$this->seedClassChain();
$this->seedAssignment($studentId, 701, '2025-2026');
$this->seedScores($studentId, '2025-2026', 50.0, 55.0);
$service = $this->makeService();
$record = $service->evaluateStudent($studentId, '2025-2026', 1);
$this->assertNotNull($record);
$this->assertSame(StudentPromotionRecord::STATUS_REPEATED, $record->promotion_status);
$this->assertFalse((bool) $record->passed_current_level);
}
public function test_missing_scores_marks_on_hold(): void
{
$this->seedConfig();
$studentId = $this->seedStudent();
$this->seedClassChain();
$this->seedAssignment($studentId, 701, '2025-2026');
$service = $this->makeService();
$record = $service->evaluateStudent($studentId, '2025-2026', 1);
$this->assertNotNull($record);
$this->assertSame(StudentPromotionRecord::STATUS_ON_HOLD, $record->promotion_status);
$this->assertNull($record->passed_current_level);
}
public function test_terminal_level_is_graduated(): void
{
$this->seedConfig();
$studentId = $this->seedStudent();
$this->seedClassChain();
$this->seedAssignment($studentId, 901, '2025-2026');
$this->seedScores($studentId, '2025-2026', 90.0, 90.0);
// Mark Youth as terminal
\App\Models\LevelProgression::query()->where('current_level_name', 'Youth')->update(['is_terminal' => 1]);
$service = $this->makeService();
$record = $service->evaluateStudent($studentId, '2025-2026', 1);
$this->assertNotNull($record);
$this->assertSame(StudentPromotionRecord::STATUS_GRADUATED, $record->promotion_status);
}
public function test_audit_log_records_status_changes(): void
{
$this->seedConfig();
$studentId = $this->seedStudent();
$this->seedClassChain();
$this->seedAssignment($studentId, 701, '2025-2026');
$this->seedScores($studentId, '2025-2026', 80.0, 80.0);
$service = $this->makeService();
$service->evaluateStudent($studentId, '2025-2026', 99);
$this->assertDatabaseHas('promotion_audit_log', [
'student_id' => $studentId,
'action_type' => 'eligibility_evaluated',
'user_id' => 99,
]);
$this->assertDatabaseHas('promotion_audit_log', [
'student_id' => $studentId,
'action_type' => 'record_created',
]);
}
private function makeService(): PromotionEligibilityService
{
$progression = new LevelProgressionService();
$audit = new PromotionAuditService();
$status = new PromotionStatusService($audit);
return new PromotionEligibilityService($progression, $status, $audit);
}
private function seedConfig(): void
{
DB::table('configuration')->insert([
['config_key' => 'school_year', 'config_value' => '2025-2026'],
['config_key' => 'semester', 'config_value' => 'Fall'],
]);
}
private function seedStudent(int $parentId = 5): int
{
return DB::table('students')->insertGetId([
'school_id' => 'S-100',
'firstname' => 'Sara',
'lastname' => 'Test',
'parent_id' => $parentId,
'year_of_registration' => '2025',
'school_year' => '2025-2026',
'semester' => 'Fall',
]);
}
private function seedClassChain(): void
{
DB::table('classes')->insert([
['id' => 7, 'class_name' => 'Grade 3', 'school_year' => '2025-2026', 'semester' => 'Fall', 'capacity' => 30],
['id' => 8, 'class_name' => 'Grade 4', 'school_year' => '2025-2026', 'semester' => 'Fall', 'capacity' => 30],
['id' => 13, 'class_name' => 'Youth', 'school_year' => '2025-2026', 'semester' => 'Fall', 'capacity' => 30],
]);
DB::table('classSection')->insert([
['class_id' => 7, 'class_section_id' => 701, 'class_section_name' => '3A'],
['class_id' => 13, 'class_section_id' => 901, 'class_section_name' => 'Youth A'],
]);
}
private function seedAssignment(int $studentId, int $classSectionId, string $schoolYear): void
{
DB::table('student_class')->insert([
'student_id' => $studentId,
'class_section_id' => $classSectionId,
'school_year' => $schoolYear,
'semester' => 'Fall',
'is_event_only' => 0,
]);
}
private function seedScores(int $studentId, string $schoolYear, float $fall, float $spring): void
{
DB::table('semester_scores')->insert([
['student_id' => $studentId, 'class_section_id' => 0, 'school_year' => $schoolYear, 'semester' => 'fall', 'semester_score' => $fall],
['student_id' => $studentId, 'class_section_id' => 0, 'school_year' => $schoolYear, 'semester' => 'spring', 'semester_score' => $spring],
]);
}
}
@@ -0,0 +1,146 @@
<?php
namespace Tests\Unit\Services\Promotions;
use App\Models\StudentPromotionRecord;
use App\Services\Promotions\PromotionAuditService;
use App\Services\Promotions\PromotionEnrollmentService;
use App\Services\Promotions\PromotionStatusService;
use Carbon\CarbonImmutable;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use RuntimeException;
use Tests\TestCase;
class PromotionEnrollmentServiceTest extends TestCase
{
use RefreshDatabase;
public function test_completing_checklist_finalises_promotion_and_creates_enrollment(): void
{
[$record, $service, $parentId] = $this->bootstrap();
$service->updateSteps($record, $parentId, [
'info_confirmed' => true,
'documents_uploaded' => true,
'agreement_accepted' => true,
], $parentId);
$service->updateSteps($record, $parentId, [
'payment_completed' => true,
], $parentId);
$record->refresh();
$this->assertSame(StudentPromotionRecord::STATUS_PROMOTED_AND_ENROLLED, $record->promotion_status);
$this->assertSame(StudentPromotionRecord::ENROLLMENT_COMPLETED, $record->enrollment_status);
$this->assertNotNull($record->promotion_finalized_at);
$this->assertDatabaseHas('enrollments', [
'student_id' => $record->student_id,
'school_year' => $record->next_school_year,
'enrollment_status' => 'enrolled',
'admission_status' => 'accepted',
]);
$this->assertDatabaseHas('promotion_audit_log', [
'promotion_id' => $record->getKey(),
'action_type' => 'promotion_finalized',
]);
}
public function test_partial_checklist_keeps_status_in_progress(): void
{
[$record, $service, $parentId] = $this->bootstrap();
$service->updateSteps($record, $parentId, [
'info_confirmed' => true,
'documents_uploaded' => true,
], $parentId);
$record->refresh();
$this->assertSame(StudentPromotionRecord::STATUS_ENROLLMENT_STARTED, $record->promotion_status);
$this->assertSame(StudentPromotionRecord::ENROLLMENT_IN_PROGRESS, $record->enrollment_status);
$this->assertNull($record->promotion_finalized_at);
}
public function test_submit_requires_complete_checklist(): void
{
[$record, $service, $parentId] = $this->bootstrap();
$service->updateSteps($record, $parentId, [
'info_confirmed' => true,
], $parentId);
$this->expectException(RuntimeException::class);
$service->submitEnrollment($record->refresh(), $parentId, $parentId);
}
public function test_other_parent_cannot_act_on_record(): void
{
[$record, $service] = $this->bootstrap();
$this->expectException(RuntimeException::class);
$service->startEnrollment($record, 9999, 9999);
}
public function test_expired_records_become_not_enrolled(): void
{
[$record, $service] = $this->bootstrap();
$record->enrollment_deadline = now()->subDays(2)->toDateString();
$record->save();
$result = $service->markExpiredDeadlines(CarbonImmutable::now(), 99);
$this->assertSame(1, $result['expired']);
$record->refresh();
$this->assertSame(StudentPromotionRecord::STATUS_NOT_ENROLLED, $record->promotion_status);
$this->assertSame(StudentPromotionRecord::ENROLLMENT_EXPIRED, $record->enrollment_status);
}
/**
* @return array{0:StudentPromotionRecord, 1:PromotionEnrollmentService, 2:int}
*/
private function bootstrap(): array
{
$audit = new PromotionAuditService();
$service = new PromotionEnrollmentService(new PromotionStatusService($audit), $audit);
$parentId = (int) DB::table('users')->insertGetId([
'firstname' => 'Parent',
'lastname' => 'One',
'email' => 'parent@example.com',
'cellphone' => '5551234567',
'address_street' => '1 Way',
'city' => 'C',
'state' => 'S',
'zip' => '00000',
'accept_school_policy' => 1,
'password' => bcrypt('x'),
'school_year' => '2025-2026',
'semester' => 'Fall',
'status' => 'Active',
]);
$studentId = (int) DB::table('students')->insertGetId([
'school_id' => 'S-1',
'firstname' => 'Sara',
'lastname' => 'Test',
'parent_id' => $parentId,
'year_of_registration' => '2025',
'school_year' => '2025-2026',
'semester' => 'Fall',
]);
$record = StudentPromotionRecord::query()->create([
'student_id' => $studentId,
'parent_id' => $parentId,
'current_school_year' => '2025-2026',
'next_school_year' => '2026-2027',
'promotion_status' => StudentPromotionRecord::STATUS_AWAITING_PARENT,
'enrollment_required' => true,
'enrollment_status' => StudentPromotionRecord::ENROLLMENT_NOT_STARTED,
'promoted_level_name' => 'Grade 4',
'enrollment_deadline' => now()->addDays(30)->toDateString(),
]);
return [$record, $service, $parentId];
}
}
@@ -0,0 +1,110 @@
<?php
namespace Tests\Unit\Services\Promotions;
use App\Models\StudentPromotionRecord;
use App\Services\Promotions\PromotionAuditService;
use App\Services\Promotions\PromotionStatusService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use RuntimeException;
use Tests\TestCase;
class PromotionStatusServiceTest extends TestCase
{
use RefreshDatabase;
public function test_allowed_transition_updates_status_and_audit_log(): void
{
$record = $this->makeRecord(StudentPromotionRecord::STATUS_AWAITING_PARENT);
$service = $this->makeService();
$updated = $service->transition(
$record,
StudentPromotionRecord::STATUS_ENROLLMENT_STARTED,
42,
'parent began enrollment'
);
$this->assertSame(StudentPromotionRecord::STATUS_ENROLLMENT_STARTED, $updated->promotion_status);
$this->assertDatabaseHas('promotion_audit_log', [
'promotion_id' => $record->getKey(),
'action_type' => 'status_changed',
'old_value' => StudentPromotionRecord::STATUS_AWAITING_PARENT,
'new_value' => StudentPromotionRecord::STATUS_ENROLLMENT_STARTED,
'user_id' => 42,
]);
}
public function test_disallowed_transition_throws(): void
{
$record = $this->makeRecord(StudentPromotionRecord::STATUS_NOT_REVIEWED);
$service = $this->makeService();
$this->expectException(RuntimeException::class);
$service->transition($record, StudentPromotionRecord::STATUS_PROMOTED_AND_ENROLLED, 1);
}
public function test_force_status_bypasses_transition_map(): void
{
$record = $this->makeRecord(StudentPromotionRecord::STATUS_NOT_REVIEWED);
$service = $this->makeService();
$updated = $service->forceStatus($record, StudentPromotionRecord::STATUS_PROMOTED_AND_ENROLLED, 7, 'manual fix');
$this->assertSame(StudentPromotionRecord::STATUS_PROMOTED_AND_ENROLLED, $updated->promotion_status);
$this->assertDatabaseHas('promotion_audit_log', [
'promotion_id' => $record->getKey(),
'action_type' => 'manual_override',
'field' => 'promotion_status',
'old_value' => StudentPromotionRecord::STATUS_NOT_REVIEWED,
'new_value' => StudentPromotionRecord::STATUS_PROMOTED_AND_ENROLLED,
]);
}
public function test_no_op_transition_returns_record_unchanged(): void
{
$record = $this->makeRecord(StudentPromotionRecord::STATUS_AWAITING_PARENT);
$service = $this->makeService();
$updated = $service->transition(
$record,
StudentPromotionRecord::STATUS_AWAITING_PARENT,
1
);
$this->assertSame(StudentPromotionRecord::STATUS_AWAITING_PARENT, $updated->promotion_status);
$this->assertDatabaseMissing('promotion_audit_log', [
'promotion_id' => $record->getKey(),
'action_type' => 'status_changed',
]);
}
private function makeService(): PromotionStatusService
{
return new PromotionStatusService(new PromotionAuditService());
}
private function makeRecord(string $status): StudentPromotionRecord
{
$studentId = DB::table('students')->insertGetId([
'school_id' => 'S-1',
'firstname' => 'Test',
'lastname' => 'Student',
'parent_id' => 1,
'year_of_registration' => '2025',
'school_year' => '2025-2026',
'semester' => 'Fall',
]);
return StudentPromotionRecord::query()->create([
'student_id' => $studentId,
'parent_id' => 1,
'current_school_year' => '2025-2026',
'next_school_year' => '2026-2027',
'promotion_status' => $status,
'enrollment_required' => true,
'enrollment_status' => StudentPromotionRecord::ENROLLMENT_NOT_STARTED,
]);
}
}