add school year model

This commit is contained in:
root
2026-06-07 00:52:01 -04:00
parent a192ed433d
commit 6866aedf42
36 changed files with 4771 additions and 88 deletions
@@ -5,6 +5,7 @@ namespace Tests\Feature\Api\Administrator;
use App\Models\User;
use App\Services\Administrator\TeacherSubmissionNotificationService;
use App\Services\Administrator\TeacherSubmissionReportService;
use App\Http\Middleware\EnsurePrintRequestsAdminAccess;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
use Mockery;
@@ -22,12 +23,14 @@ class AdministratorTeacherSubmissionControllerTest extends TestCase
public function test_can_get_teacher_submission_report(): void
{
$this->withoutMiddleware(EnsurePrintRequestsAdminAccess::class);
$user = User::factory()->create();
Sanctum::actingAs($user);
Sanctum::actingAs($user, [], 'api');
$mock = Mockery::mock(TeacherSubmissionReportService::class);
$mock->shouldReceive('report')
->once()
->with([])
->andReturn([
'rows' => [],
'semester' => 'Fall',
@@ -55,8 +58,9 @@ class AdministratorTeacherSubmissionControllerTest extends TestCase
public function test_notify_requires_notify_payload(): void
{
$this->withoutMiddleware(EnsurePrintRequestsAdminAccess::class);
$user = User::factory()->create();
Sanctum::actingAs($user);
Sanctum::actingAs($user, [], 'api');
$response = $this->postJson('/api/v1/administrator/teacher-submissions/notify', [
'missing_items' => [],
@@ -68,8 +72,9 @@ class AdministratorTeacherSubmissionControllerTest extends TestCase
public function test_notify_calls_service(): void
{
$this->withoutMiddleware(EnsurePrintRequestsAdminAccess::class);
$user = User::factory()->create();
Sanctum::actingAs($user);
Sanctum::actingAs($user, [], 'api');
$payload = [
'notify' => [
@@ -105,4 +110,4 @@ class AdministratorTeacherSubmissionControllerTest extends TestCase
'failed' => 0,
]);
}
}
}
@@ -32,8 +32,8 @@ class AdministratorTeacherSubmissionNotifyApiTest extends TestCase
public function test_notify_endpoint_requires_notify_payload(): void
{
$admin = User::factory()->create();
Sanctum::actingAs($admin);
$admin = $this->seedAdminUser('admin.notify.required@example.com');
Sanctum::actingAs($admin, [], 'api');
$response = $this->postJson('/api/v1/administrator/teacher-submissions/notify', [
'missing_items' => [],
@@ -47,13 +47,8 @@ class AdministratorTeacherSubmissionNotifyApiTest extends TestCase
{
Mail::fake();
$admin = User::factory()->create([
'id' => 900,
'firstname' => 'Admin',
'lastname' => 'One',
'email' => 'admin@test.com',
]);
Sanctum::actingAs($admin);
$admin = $this->seedAdminUser('admin@test.com', 900, 'Admin', 'One');
Sanctum::actingAs($admin, [], 'api');
User::factory()->create([
'id' => 100,
@@ -117,12 +112,8 @@ class AdministratorTeacherSubmissionNotifyApiTest extends TestCase
{
Mail::fake();
$admin = User::factory()->create([
'id' => 901,
'firstname' => 'Admin',
'lastname' => 'Two',
]);
Sanctum::actingAs($admin);
$admin = $this->seedAdminUser('admin.two@test.com', 901, 'Admin', 'Two');
Sanctum::actingAs($admin, [], 'api');
User::factory()->create([
'id' => 101,
@@ -169,4 +160,94 @@ class AdministratorTeacherSubmissionNotifyApiTest extends TestCase
'semester' => 'Fall',
]);
}
public function test_notify_endpoint_accepts_flat_teacher_ids_from_spa_payload(): void
{
Mail::fake();
$admin = $this->seedAdminUser('admin3@test.com', 902, 'Admin', 'Three');
Sanctum::actingAs($admin, [], 'api');
User::factory()->create([
'id' => 102,
'firstname' => 'Flat',
'lastname' => 'Teacher',
'email' => 'flat@test.com',
]);
DB::table('classSection')->insert([
[
'class_section_id' => 12,
'class_section_name' => 'Grade 7A',
'class_id' => 7,
'semester' => 'Fall',
'school_year' => '2025-2026',
],
]);
DB::table('teacher_class')->insert([
'class_section_id' => 12,
'teacher_id' => 102,
'position' => 'main',
'school_year' => '2025-2026',
'semester' => 'Fall',
]);
$payload = [
'notify' => [102],
'missing_items' => [
102 => ['midterm scores', 'attendance'],
],
];
$response = $this->postJson('/api/v1/administrator/teacher-submissions/notify', $payload);
$response->assertOk()
->assertJson([
'sent' => 1,
'failed' => 0,
]);
$this->assertDatabaseHas('teacher_submission_notification_history', [
'teacher_id' => 102,
'class_section_id' => 12,
'admin_id' => 902,
'notification_category' => 'teacher_submissions',
'status' => 'sent',
]);
}
private function seedAdminUser(string $email, ?int $id = null, string $firstname = 'Admin', string $lastname = 'User'): User
{
$roleId = DB::table('roles')->insertGetId([
'name' => 'administrator',
'slug' => 'administrator',
'is_active' => 1,
]);
$userId = DB::table('users')->insertGetId(array_filter([
'id' => $id,
'firstname' => $firstname,
'lastname' => $lastname,
'cellphone' => '5555555555',
'email' => $email,
'address_street' => '123 Main',
'city' => 'City',
'state' => 'ST',
'zip' => '12345',
'accept_school_policy' => 1,
'password' => bcrypt('secret'),
'user_type' => 'primary',
'semester' => 'Fall',
'school_year' => '2025-2026',
'status' => 'Active',
], static fn ($value) => $value !== null));
DB::table('user_roles')->insert([
'user_id' => $userId,
'role_id' => $roleId,
]);
return User::query()->findOrFail($userId);
}
}
@@ -4,7 +4,6 @@ namespace Tests\Feature\Api\Administrator;
use App\Models\AttendanceDay;
use App\Models\Configuration;
use App\Models\ScoreComment;
use App\Models\SemesterScore;
use App\Models\TeacherSubmissionNotificationHistory;
use App\Models\User;
@@ -35,8 +34,8 @@ class AdministratorTeacherSubmissionReportApiTest extends TestCase
public function test_teacher_submission_report_endpoint_returns_rows_summary_and_history(): void
{
$admin = User::factory()->create(['id' => 900, 'firstname' => 'Admin', 'lastname' => 'One']);
Sanctum::actingAs($admin);
$admin = $this->seedAdminUser('admin.report.one@test.com', 900, 'Admin', 'One');
Sanctum::actingAs($admin, [], 'api');
User::factory()->create([
'id' => 100,
@@ -114,8 +113,7 @@ class AdministratorTeacherSubmissionReportApiTest extends TestCase
'participation_score' => '9',
]);
ScoreComment::unguard();
ScoreComment::query()->create([
DB::table('score_comments')->insert([
'student_id' => 1,
'class_section_id' => 10,
'school_year' => '2025-2026',
@@ -123,7 +121,7 @@ class AdministratorTeacherSubmissionReportApiTest extends TestCase
'score_type' => 'midterm',
'comment' => 'Good progress',
]);
ScoreComment::query()->create([
DB::table('score_comments')->insert([
'student_id' => 1,
'class_section_id' => 10,
'school_year' => '2025-2026',
@@ -199,8 +197,8 @@ class AdministratorTeacherSubmissionReportApiTest extends TestCase
public function test_teacher_submission_report_returns_no_students_status_for_empty_section(): void
{
$admin = User::factory()->create();
Sanctum::actingAs($admin);
$admin = $this->seedAdminUser('admin.report.empty@test.com');
Sanctum::actingAs($admin, [], 'api');
User::factory()->create([
'id' => 200,
@@ -246,8 +244,8 @@ class AdministratorTeacherSubmissionReportApiTest extends TestCase
public function test_teacher_submission_report_limits_history_to_three_entries(): void
{
$admin = User::factory()->create(['id' => 901, 'firstname' => 'Admin', 'lastname' => 'Two']);
Sanctum::actingAs($admin);
$admin = $this->seedAdminUser('admin.report.two@test.com', 901, 'Admin', 'Two');
Sanctum::actingAs($admin, [], 'api');
User::factory()->create([
'id' => 300,
@@ -298,4 +296,82 @@ class AdministratorTeacherSubmissionReportApiTest extends TestCase
$history = $response->json('notificationHistory.12.300');
$this->assertCount(3, $history);
}
public function test_teacher_submission_report_honors_school_year_and_semester_query_filters(): void
{
$admin = $this->seedAdminUser('admin.report.filters@test.com');
Sanctum::actingAs($admin, [], 'api');
User::factory()->create([
'id' => 400,
'firstname' => 'Yearly',
'lastname' => 'Teacher',
'email' => 'yearly@test.com',
]);
DB::table('classSection')->insert([
[
'class_section_id' => 20,
'class_section_name' => 'Grade 8A',
'class_id' => 8,
'semester' => 'Spring',
'school_year' => '2024-2025',
],
]);
DB::table('teacher_class')->insert([
[
'class_section_id' => 20,
'teacher_id' => 400,
'position' => 'main',
'school_year' => '2024-2025',
'semester' => 'Spring',
],
]);
$response = $this->getJson('/api/v1/administrator/teacher-submissions?school_year=2024-2025&semester=Spring');
$response->assertOk()
->assertJson([
'semester' => 'Spring',
'schoolYear' => '2024-2025',
'currentSemester' => 'Fall',
'currentSchoolYear' => '2025-2026',
])
->assertJsonCount(1, 'rows');
}
private function seedAdminUser(string $email, ?int $id = null, string $firstname = 'Admin', string $lastname = 'User'): User
{
$roleId = DB::table('roles')->insertGetId([
'name' => 'administrator',
'slug' => 'administrator',
'is_active' => 1,
]);
$userId = DB::table('users')->insertGetId(array_filter([
'id' => $id,
'firstname' => $firstname,
'lastname' => $lastname,
'cellphone' => '5555555555',
'email' => $email,
'address_street' => '123 Main',
'city' => 'City',
'state' => 'ST',
'zip' => '12345',
'accept_school_policy' => 1,
'password' => bcrypt('secret'),
'user_type' => 'primary',
'semester' => 'Fall',
'school_year' => '2025-2026',
'status' => 'Active',
], static fn ($value) => $value !== null));
DB::table('user_roles')->insert([
'user_id' => $userId,
'role_id' => $roleId,
]);
return User::query()->findOrFail($userId);
}
}
@@ -15,7 +15,7 @@ class EmailExtractorControllerTest extends TestCase
public function test_emails_endpoint_returns_users_and_parents(): void
{
$user = $this->createUser();
Sanctum::actingAs($user);
Sanctum::actingAs($user, [], 'api');
DB::table('parents')->insert([
'secondparent_firstname' => 'Parent',
@@ -31,13 +31,38 @@ class EmailExtractorControllerTest extends TestCase
$response->assertOk();
$response->assertJsonPath('ok', true);
$this->assertContains('admin@example.com', $response->json('users'));
$this->assertContains('parent2@example.com', $response->json('parents'));
$this->assertContains('parent2@example.com', $response->json('emails.parents'));
}
public function test_legacy_emails_endpoint_returns_top_level_arrays(): void
{
$user = $this->createUser();
Sanctum::actingAs($user, [], 'api');
DB::table('parents')->insert([
'secondparent_firstname' => 'Parent',
'secondparent_lastname' => 'Legacy',
'secondparent_email' => 'legacy-parent@example.com',
'secondparent_phone' => '5555555555',
'firstparent_id' => 1,
'secondparent_id' => 3,
'school_year' => '2025-2026',
]);
$response = $this->getJson('/api/emails');
$response->assertOk();
$response->assertJsonPath('ok', true);
$this->assertContains('admin@example.com', $response->json('users'));
$this->assertContains('legacy-parent@example.com', $response->json('parents'));
}
public function test_compare_endpoint_returns_comparison(): void
{
$user = $this->createUser();
Sanctum::actingAs($user);
Sanctum::actingAs($user, [], 'api');
DB::table('parents')->insert([
'secondparent_firstname' => 'Parent',
@@ -0,0 +1,343 @@
<?php
namespace Tests\Feature\Api\V1\SchoolYears;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
class SchoolYearControllerTest extends TestCase
{
use RefreshDatabase;
public function test_preview_close_returns_promotion_and_balance_preview(): void
{
$this->seedClosureData();
$this->actingAs(User::query()->findOrFail(1), 'api');
$response = $this->postJson('/api/v1/school-years/1/preview-close', [
'new_school_year' => [
'name' => '2026-2027',
'start_date' => '2026-09-01',
'end_date' => '2027-06-30',
],
'transfer_unpaid_balances' => true,
]);
$response->assertOk()
->assertJsonPath('data.validation.can_close', true)
->assertJsonPath('data.promotion_preview.summary.promote', 1)
->assertJsonPath('data.parent_balances.summary.total_old_unpaid_balance', 200);
$this->assertSame(200.0, (float) $response->json('data.parent_balances.rows.0.amount_to_transfer'));
$this->assertSame('promote', $response->json('data.promotion_preview.rows.0.promotion_action'));
}
public function test_admin_can_create_draft_school_year_via_api(): void
{
$this->seedClosureData();
$this->actingAs(User::query()->findOrFail(1), 'api');
$response = $this->postJson('/api/v1/school-years', [
'name' => '2026-2027',
'start_date' => '2026-09-01',
'end_date' => '2027-06-30',
]);
$response->assertCreated()
->assertJsonPath('data.name', '2026-2027')
->assertJsonPath('data.status', 'draft');
$this->assertDatabaseHas('school_years', [
'name' => '2026-2027',
'status' => 'draft',
'is_current' => 0,
]);
}
public function test_admin_can_update_draft_school_year_via_api(): void
{
$this->seedClosureData();
DB::table('school_years')->insert([
'id' => 2,
'name' => '2027-2028',
'start_date' => '2027-09-01',
'end_date' => '2028-06-30',
'status' => 'draft',
'is_current' => 0,
'created_at' => now(),
'updated_at' => now(),
]);
$this->actingAs(User::query()->findOrFail(1), 'api');
$response = $this->patchJson('/api/v1/school-years/2', [
'name' => '2028-2029',
'start_date' => '2028-09-01',
'end_date' => '2029-06-30',
]);
$response->assertOk()
->assertJsonPath('data.name', '2028-2029')
->assertJsonPath('data.start_date', '2028-09-01');
$this->assertDatabaseHas('school_years', [
'id' => 2,
'name' => '2028-2029',
'start_date' => '2028-09-01 00:00:00',
'end_date' => '2029-06-30 00:00:00',
]);
}
public function test_close_school_year_creates_new_active_year_enrollment_and_balance_transfer(): void
{
$this->seedClosureData();
$this->actingAs(User::query()->findOrFail(1), 'api');
$response = $this->postJson('/api/v1/school-years/1/close', [
'new_school_year' => [
'name' => '2026-2027',
'start_date' => '2026-09-01',
'end_date' => '2027-06-30',
],
'transfer_unpaid_balances' => true,
'confirmation' => 'CLOSE 2025-2026',
]);
$response->assertOk()
->assertJsonPath('data.closed_school_year.status', 'closed')
->assertJsonPath('data.new_school_year.status', 'active');
$this->assertDatabaseHas('school_years', [
'name' => '2025-2026',
'status' => 'closed',
'is_current' => 0,
]);
$this->assertDatabaseHas('school_years', [
'name' => '2026-2027',
'status' => 'active',
'is_current' => 1,
]);
$this->assertDatabaseHas('configuration', [
'config_key' => 'school_year',
'config_value' => '2026-2027',
]);
$this->assertDatabaseHas('enrollments', [
'student_id' => 100,
'parent_id' => 10,
'school_year' => '2026-2027',
'enrollment_status' => 'enrolled',
]);
$this->assertDatabaseHas('parent_balance_transfers', [
'parent_id' => 10,
'from_school_year' => '2025-2026',
'to_school_year' => '2026-2027',
'amount' => 200.00,
'status' => 'transferred',
]);
$this->assertDatabaseHas('invoices', [
'parent_id' => 10,
'school_year' => '2026-2027',
'total_amount' => 200.00,
'balance' => 200.00,
'description' => 'Opening balance transferred from 2025-2026',
]);
}
public function test_closed_school_year_blocks_legacy_invoice_generation(): void
{
$this->seedClosureData();
$this->actingAs(User::query()->findOrFail(1), 'api');
$this->postJson('/api/v1/school-years/1/close', [
'new_school_year' => [
'name' => '2026-2027',
'start_date' => '2026-09-01',
'end_date' => '2027-06-30',
],
'transfer_unpaid_balances' => true,
'confirmation' => 'CLOSE 2025-2026',
])->assertOk();
$response = $this->postJson('/api/v1/finance/invoices/generate', [
'parent_id' => 10,
'school_year' => '2025-2026',
]);
$response->assertStatus(409)
->assertJsonPath('message', 'School year 2025-2026 is closed and read-only.');
}
private function seedClosureData(): void
{
DB::table('configuration')->insert([
['config_key' => 'school_year', 'config_value' => '2025-2026'],
['config_key' => 'semester', 'config_value' => 'Fall'],
]);
DB::table('roles')->insert([
['id' => 1, 'name' => 'administrator', 'slug' => 'administrator', 'is_active' => 1],
['id' => 2, 'name' => 'parent', 'slug' => 'parent', 'is_active' => 1],
]);
DB::table('users')->insert([
[
'id' => 1,
'school_id' => 1,
'firstname' => 'Admin',
'lastname' => 'User',
'cellphone' => '5555555555',
'email' => 'admin@example.com',
'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',
],
[
'id' => 10,
'school_id' => 1,
'firstname' => 'Parent',
'lastname' => 'User',
'cellphone' => '5555555555',
'email' => 'parent@example.com',
'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',
],
]);
DB::table('user_roles')->insert([
['user_id' => 1, 'role_id' => 1],
['user_id' => 10, 'role_id' => 2],
]);
DB::table('school_years')->insert([
'id' => 1,
'name' => '2025-2026',
'start_date' => '2025-09-01',
'end_date' => '2026-06-30',
'status' => 'active',
'is_current' => 1,
'created_at' => now(),
'updated_at' => now(),
]);
DB::table('students')->insert([
'id' => 100,
'parent_id' => 10,
'firstname' => 'Sara',
'lastname' => 'Student',
'school_id' => 'S-100',
'age' => 9,
'gender' => 'Female',
'is_active' => 1,
'photo_consent' => 1,
'year_of_registration' => '2025',
'school_year' => '2025-2026',
'semester' => 'Fall',
]);
DB::table('classes')->insert([
['id' => 3, 'class_name' => 'Grade 3', 'schedule' => 'Sunday 9:00', 'school_year' => '2025-2026', 'semester' => 'Fall', 'capacity' => 30],
['id' => 4, 'class_name' => 'Grade 4', 'schedule' => 'Sunday 10:00', 'school_year' => '2026-2027', 'semester' => 'Fall', 'capacity' => 30],
]);
DB::table('classSection')->insert([
[
'class_section_id' => 301,
'class_section_name' => '3A',
'class_id' => 3,
'semester' => 'Fall',
'school_year' => '2025-2026',
],
[
'class_section_id' => 401,
'class_section_name' => '4A',
'class_id' => 4,
'semester' => 'Fall',
'school_year' => '2026-2027',
],
]);
DB::table('student_class')->insert([
'student_id' => 100,
'class_section_id' => 301,
'school_year' => '2025-2026',
'semester' => 'Fall',
'is_event_only' => 0,
]);
DB::table('enrollments')->insert([
'student_id' => 100,
'class_section_id' => 301,
'parent_id' => 10,
'enrollment_date' => '2025-09-05',
'enrollment_status' => 'enrolled',
'withdrawal_date' => null,
'is_withdrawn' => 0,
'admission_status' => 'accepted',
'semester' => 'Fall',
'school_year' => '2025-2026',
'created_at' => now(),
'updated_at' => now(),
]);
DB::table('student_decisions')->insert([
'student_id' => 100,
'school_year' => '2025-2026',
'class_section_name' => '3A',
'year_score' => 85,
'decision' => 'promoted',
'source' => 'manual',
'notes' => 'Ready for promotion',
'created_at' => now(),
'updated_at' => now(),
]);
DB::table('invoices')->insert([
'id' => 500,
'parent_id' => 10,
'invoice_number' => 'INV-500',
'total_amount' => 500,
'balance' => 200,
'paid_amount' => 300,
'has_discount' => 0,
'issue_date' => '2026-05-10',
'due_date' => '2026-06-10',
'status' => 'unpaid',
'description' => 'Tuition balance',
'school_year' => '2025-2026',
'created_at' => now(),
'updated_at' => now(),
'updated_by' => 1,
'semester' => 'Fall',
]);
}
}
@@ -0,0 +1,117 @@
<?php
namespace Tests\Feature\Web;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
class SchoolYearAdminPageTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
$compiledPath = storage_path('framework/views');
if (!is_dir($compiledPath)) {
mkdir($compiledPath, 0777, true);
}
config([
'app.key' => 'base64:' . base64_encode(random_bytes(32)),
'app.cipher' => 'AES-256-CBC',
'jwt.secret' => bin2hex(random_bytes(32)),
'view.compiled' => $compiledPath,
]);
}
public function test_admin_can_view_school_year_page(): void
{
$this->seedAdminContext();
$response = $this->actingAs(User::query()->findOrFail(1))
->get('/administrator/school-years');
$response->assertOk();
$response->assertSee('School Years Management');
$response->assertSee('JWT Authorization: Bearer');
$response->assertSee('/api/v1/school-years');
}
public function test_admin_can_create_draft_school_year_from_page(): void
{
$this->seedAdminContext();
$response = $this->actingAs(User::query()->findOrFail(1))
->post('/administrator/school-years', [
'name' => '2026-2027',
'start_date' => '2026-09-01',
'end_date' => '2027-06-30',
]);
$response->assertRedirect();
$this->assertDatabaseHas('school_years', [
'name' => '2026-2027',
'status' => 'draft',
'is_current' => 0,
]);
}
private function seedAdminContext(): void
{
DB::table('configuration')->insert([
['config_key' => 'school_year', 'config_value' => '2025-2026'],
['config_key' => 'semester', 'config_value' => 'Fall'],
]);
DB::table('roles')->insert([
'id' => 1,
'name' => 'administrator',
'slug' => 'administrator',
'is_active' => 1,
]);
DB::table('users')->insert([
'id' => 1,
'school_id' => 1,
'firstname' => 'Admin',
'lastname' => 'User',
'gender' => 'Female',
'cellphone' => '5555555555',
'email' => 'admin@example.com',
'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',
'created_at' => now(),
'updated_at' => now(),
]);
DB::table('user_roles')->insert([
'user_id' => 1,
'role_id' => 1,
]);
DB::table('school_years')->insert([
'id' => 1,
'name' => '2025-2026',
'start_date' => '2025-09-01',
'end_date' => '2026-06-30',
'status' => 'active',
'is_current' => 1,
'created_at' => now(),
'updated_at' => now(),
]);
}
}