add more controller

This commit is contained in:
root
2026-03-10 00:48:32 -04:00
parent 5eeaec0257
commit 20ee70d153
151 changed files with 9481 additions and 8407 deletions
@@ -0,0 +1,124 @@
<?php
namespace Tests\Feature\Api\V1\Administrator;
use App\Models\EmergencyContact;
use App\Models\Student;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class EmergencyContactControllerTest extends TestCase
{
use RefreshDatabase;
public function test_index_returns_grouped_contacts(): void
{
$admin = User::factory()->create();
$parent = User::factory()->create([
'firstname' => 'Omar',
'lastname' => 'Parent',
'cellphone' => '1111111111',
]);
DB::table('parents')->insert([
'secondparent_firstname' => 'Nora',
'secondparent_lastname' => 'Parent',
'secondparent_gender' => 'Female',
'secondparent_email' => 'nora.parent@example.com',
'secondparent_phone' => '2222222222',
'firstparent_id' => $parent->id,
'secondparent_id' => 1234,
'semester' => 'Fall',
'school_year' => '2025-2026',
'created_at' => now(),
'updated_at' => now(),
]);
Student::factory()->create([
'parent_id' => $parent->id,
'firstname' => 'Rami',
'lastname' => 'Student',
'school_id' => 'S-202',
]);
EmergencyContact::query()->create([
'parent_id' => $parent->id,
'emergency_contact_name' => 'Uncle Amin',
'cellphone' => '3333333333',
'email' => 'amin@example.com',
'relation' => 'Uncle',
'semester' => 'Fall',
'school_year' => '2025-2026',
]);
Sanctum::actingAs($admin);
$response = $this->getJson('/api/v1/administrator/emergency-contacts');
$response->assertOk();
$response->assertJson(['ok' => true]);
$response->assertJsonPath('groups.0.parent_id', $parent->id);
$response->assertJsonPath('groups.0.parent_phones.0', '1111111111');
$response->assertJsonPath('groups.0.parent_phones.1', '2222222222');
}
public function test_update_changes_emergency_contact(): void
{
$admin = User::factory()->create();
$parent = User::factory()->create();
$contact = EmergencyContact::query()->create([
'parent_id' => $parent->id,
'emergency_contact_name' => 'Old Name',
'cellphone' => '4444444444',
'email' => 'old@example.com',
'relation' => 'Friend',
'semester' => 'Fall',
'school_year' => '2025-2026',
]);
Sanctum::actingAs($admin);
$response = $this->patchJson('/api/v1/administrator/emergency-contacts/' . $contact->id, [
'name' => 'New Name',
'cellphone' => '5555555555',
'email' => 'new@example.com',
'relation' => 'Neighbor',
]);
$response->assertOk();
$response->assertJson(['ok' => true]);
$this->assertDatabaseHas('emergency_contacts', [
'id' => $contact->id,
'emergency_contact_name' => 'New Name',
'email' => 'new@example.com',
'relation' => 'Neighbor',
]);
}
public function test_destroy_deletes_emergency_contact(): void
{
$admin = User::factory()->create();
$parent = User::factory()->create();
$contact = EmergencyContact::query()->create([
'parent_id' => $parent->id,
'emergency_contact_name' => 'Delete Name',
'cellphone' => '6666666666',
'email' => 'delete@example.com',
'relation' => 'Friend',
'semester' => 'Fall',
'school_year' => '2025-2026',
]);
Sanctum::actingAs($admin);
$response = $this->deleteJson('/api/v1/administrator/emergency-contacts/' . $contact->id);
$response->assertOk();
$response->assertJson(['ok' => true]);
$this->assertDatabaseMissing('emergency_contacts', [
'id' => $contact->id,
]);
}
}
@@ -0,0 +1,141 @@
<?php
namespace Tests\Feature\Api\V1\Administrator;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class TeacherClassAssignmentControllerTest extends TestCase
{
use RefreshDatabase;
public function test_index_returns_teachers_and_classes(): void
{
$this->seedConfig();
$admin = $this->seedAdminUser();
$this->seedTeacherUser();
$this->seedClassSection();
Sanctum::actingAs($admin);
$response = $this->getJson('/api/v1/administrator/teacher-class/assignments?school_year=2025-2026');
$response->assertOk();
$response->assertJson(['ok' => true]);
$this->assertNotEmpty($response->json('teachers'));
$this->assertNotEmpty($response->json('classes'));
}
public function test_store_and_delete_assignment(): void
{
$this->seedConfig();
$admin = $this->seedAdminUser();
$teacher = $this->seedTeacherUser();
$classSectionId = $this->seedClassSection();
Sanctum::actingAs($admin);
$storeResponse = $this->postJson('/api/v1/administrator/teacher-class/assign', [
'teacher_id' => $teacher->id,
'class_section_id' => $classSectionId,
'teacher_role' => 'teacher',
'school_year' => '2025-2026',
]);
$storeResponse->assertOk();
$this->assertDatabaseHas('teacher_class', [
'teacher_id' => $teacher->id,
'class_section_id' => $classSectionId,
'position' => 'main',
]);
$deleteResponse = $this->postJson('/api/v1/administrator/teacher-class/delete', [
'teacher_id' => $teacher->id,
'class_section_id' => $classSectionId,
'position' => 'main',
'school_year' => '2025-2026',
]);
$deleteResponse->assertOk();
$this->assertDatabaseMissing('teacher_class', [
'teacher_id' => $teacher->id,
'class_section_id' => $classSectionId,
'position' => 'main',
]);
}
private function seedConfig(): void
{
DB::table('configuration')->insert([
['config_key' => 'school_year', 'config_value' => '2025-2026'],
['config_key' => 'semester', 'config_value' => 'Fall'],
]);
}
private function seedAdminUser(): User
{
$userId = DB::table('users')->insertGetId([
'firstname' => 'Admin',
'lastname' => 'User',
'cellphone' => '5555555555',
'email' => 'admin.teacher.assignment@example.com',
'address_street' => '123 Street',
'city' => 'City',
'state' => 'ST',
'zip' => '12345',
'accept_school_policy' => 1,
'password' => bcrypt('password'),
'user_type' => 'primary',
'semester' => 'Fall',
'school_year' => '2025-2026',
'status' => 'Active',
]);
return User::query()->findOrFail($userId);
}
private function seedTeacherUser(): User
{
$roleId = DB::table('roles')->insertGetId([
'name' => 'teacher',
]);
$userId = DB::table('users')->insertGetId([
'firstname' => 'Olivia',
'lastname' => 'Teacher',
'cellphone' => '6666666666',
'email' => 'teacher.assignment.admin@example.com',
'address_street' => '123 Street',
'city' => 'City',
'state' => 'ST',
'zip' => '12345',
'accept_school_policy' => 1,
'password' => bcrypt('password'),
'user_type' => 'primary',
'semester' => 'Fall',
'school_year' => '2025-2026',
'status' => 'Active',
]);
DB::table('user_roles')->insert([
'user_id' => $userId,
'role_id' => $roleId,
]);
return User::query()->findOrFail($userId);
}
private function seedClassSection(): int
{
DB::table('classSection')->insert([
'class_id' => 1,
'class_section_id' => 401,
'class_section_name' => '4A',
'semester' => 'Fall',
'school_year' => '2025-2026',
]);
return 401;
}
}
@@ -0,0 +1,153 @@
<?php
namespace Tests\Feature\Api\V1\Exams;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\DB;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class ExamDraftControllerTest extends TestCase
{
use RefreshDatabase;
public function test_teacher_store_creates_draft(): void
{
$this->seedConfig();
$teacher = $this->seedTeacher();
$classSectionId = $this->seedClassSection();
$this->seedAssignment($teacher->id, $classSectionId);
Sanctum::actingAs($teacher);
$file = UploadedFile::fake()->create('draft.docx', 10, 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
$response = $this->post('/api/v1/exams/drafts/teacher', [
'class_section_id' => $classSectionId,
'exam_type' => 'Final Exam',
'description' => 'Draft',
'draft_file' => $file,
]);
$response->assertStatus(201);
$this->assertDatabaseHas('exam_drafts', [
'teacher_id' => $teacher->id,
'class_section_id' => $classSectionId,
'status' => 'submitted',
]);
}
public function test_admin_review_updates_status(): void
{
$this->seedConfig();
$admin = $this->seedAdmin();
$draftId = $this->seedDraft();
Sanctum::actingAs($admin);
$response = $this->postJson('/api/v1/exams/drafts/admin/review', [
'draft_id' => $draftId,
'review_status' => 'reviewed',
]);
$response->assertOk();
$this->assertDatabaseHas('exam_drafts', [
'id' => $draftId,
'status' => 'reviewed',
]);
}
private function seedConfig(): void
{
DB::table('configuration')->insert([
['config_key' => 'school_year', 'config_value' => '2025-2026'],
['config_key' => 'semester', 'config_value' => 'Fall'],
]);
}
private function seedTeacher(): User
{
$userId = DB::table('users')->insertGetId([
'firstname' => 'Teacher',
'lastname' => 'User',
'cellphone' => '1111111111',
'email' => 'teacher.exam.feature@example.com',
'address_street' => '123 Street',
'city' => 'City',
'state' => 'ST',
'zip' => '12345',
'accept_school_policy' => 1,
'password' => bcrypt('password'),
'user_type' => 'primary',
'semester' => 'Fall',
'school_year' => '2025-2026',
'status' => 'Active',
]);
return User::query()->findOrFail($userId);
}
private function seedAdmin(): User
{
$userId = DB::table('users')->insertGetId([
'firstname' => 'Admin',
'lastname' => 'User',
'cellphone' => '2222222222',
'email' => 'admin.exam.feature@example.com',
'address_street' => '123 Street',
'city' => 'City',
'state' => 'ST',
'zip' => '12345',
'accept_school_policy' => 1,
'password' => bcrypt('password'),
'user_type' => 'primary',
'semester' => 'Fall',
'school_year' => '2025-2026',
'status' => 'Active',
]);
return User::query()->findOrFail($userId);
}
private function seedClassSection(): int
{
DB::table('classSection')->insert([
'class_id' => 1,
'class_section_id' => 802,
'class_section_name' => '8B',
'semester' => 'Fall',
'school_year' => '2025-2026',
]);
return 802;
}
private function seedAssignment(int $teacherId, int $classSectionId): void
{
DB::table('teacher_class')->insert([
'teacher_id' => $teacherId,
'class_section_id' => $classSectionId,
'position' => 'main',
'school_year' => '2025-2026',
'created_at' => now(),
'updated_at' => now(),
]);
}
private function seedDraft(): int
{
return DB::table('exam_drafts')->insertGetId([
'teacher_id' => 1,
'class_section_id' => 802,
'semester' => 'Fall',
'school_year' => '2025-2026',
'exam_type' => 'Quiz',
'draft_title' => 'Quiz Draft',
'description' => 'Draft',
'status' => 'submitted',
'version' => 1,
'created_at' => now(),
'updated_at' => now(),
]);
}
}
@@ -0,0 +1,198 @@
<?php
namespace Tests\Feature\Api\V1\Parents;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class ParentAttendanceReportControllerTest extends TestCase
{
use RefreshDatabase;
public function test_form_returns_students_and_sundays(): void
{
$this->seedConfig();
$parent = $this->seedParent();
$this->seedStudent($parent->id);
Sanctum::actingAs($parent);
$response = $this->getJson('/api/v1/parents/attendance-reports/form');
$response->assertOk();
$response->assertJson(['ok' => true]);
$this->assertNotEmpty($response->json('students'));
$this->assertNotEmpty($response->json('sundays'));
}
public function test_submit_creates_report_and_attendance(): void
{
$this->seedConfig();
$parent = $this->seedParent();
$studentId = $this->seedStudent($parent->id);
$this->seedClassSection($studentId);
$date = $this->nextSunday();
Sanctum::actingAs($parent);
$response = $this->postJson('/api/v1/parents/attendance-reports', [
'student_ids' => [$studentId],
'date' => $date,
'type' => 'absent',
'reason' => 'Sick',
]);
$response->assertStatus(201);
$response->assertJson(['ok' => true]);
$this->assertDatabaseHas('parent_attendance_reports', [
'student_id' => $studentId,
'report_date' => $date,
'type' => 'absent',
]);
$this->assertDatabaseHas('attendance_data', [
'student_id' => $studentId,
'date' => $date,
'status' => 'absent',
]);
}
public function test_check_existing_returns_rows(): void
{
$this->seedConfig();
$parent = $this->seedParent();
$studentId = $this->seedStudent($parent->id);
$date = $this->nextSunday();
DB::table('parent_attendance_reports')->insert([
'parent_id' => $parent->id,
'student_id' => $studentId,
'report_date' => $date,
'type' => 'late',
'semester' => 'Fall',
'school_year' => '2025-2026',
'status' => 'new',
]);
Sanctum::actingAs($parent);
$response = $this->postJson('/api/v1/parents/attendance-reports/check-existing', [
'student_ids' => [$studentId],
'date' => $date,
'type' => 'late',
]);
$response->assertOk();
$response->assertJson(['ok' => true]);
$this->assertSame($studentId, $response->json('students.0.student_id'));
}
public function test_update_report_updates_reason(): void
{
$this->seedConfig();
$parent = $this->seedParent();
$studentId = $this->seedStudent($parent->id);
$date = $this->nextSunday();
$reportId = DB::table('parent_attendance_reports')->insertGetId([
'parent_id' => $parent->id,
'student_id' => $studentId,
'report_date' => $date,
'type' => 'absent',
'semester' => 'Fall',
'school_year' => '2025-2026',
'status' => 'new',
]);
Sanctum::actingAs($parent);
$response = $this->patchJson('/api/v1/parents/attendance-reports/' . $reportId, [
'reason' => 'Family emergency',
]);
$response->assertOk();
$this->assertDatabaseHas('parent_attendance_reports', [
'id' => $reportId,
'reason' => 'Family emergency',
]);
}
private function seedParent(): User
{
$id = DB::table('users')->insertGetId([
'firstname' => 'Parent',
'lastname' => 'User',
'cellphone' => '1234567890',
'email' => 'parent.attendance@example.com',
'address_street' => '123 Street',
'city' => 'City',
'state' => 'ST',
'zip' => '12345',
'accept_school_policy' => 1,
'password' => bcrypt('password'),
'user_type' => 'primary',
'semester' => 'Fall',
'school_year' => '2025-2026',
'status' => 'Active',
]);
return User::query()->findOrFail($id);
}
private function seedStudent(int $parentId): int
{
return DB::table('students')->insertGetId([
'school_id' => 'S-300',
'firstname' => 'Evan',
'lastname' => 'Parent',
'dob' => '2014-09-01',
'age' => 11,
'gender' => 'Male',
'photo_consent' => 1,
'parent_id' => $parentId,
'year_of_registration' => '2025',
'school_year' => '2025-2026',
'semester' => 'Fall',
]);
}
private function seedClassSection(int $studentId): void
{
DB::table('classSection')->insert([
'class_id' => 1,
'class_section_id' => 10,
'class_section_name' => '1A',
'semester' => 'Fall',
'school_year' => '2025-2026',
]);
DB::table('student_class')->insert([
'student_id' => $studentId,
'class_section_id' => 10,
'semester' => 'Fall',
'school_year' => '2025-2026',
]);
}
private function seedConfig(): void
{
DB::table('configuration')->insert([
['config_key' => 'school_year', 'config_value' => '2025-2026'],
['config_key' => 'semester', 'config_value' => 'Fall'],
['config_key' => 'date_age_reference', 'config_value' => '2025-12-31'],
['config_key' => 'parent_report_cutoff', 'config_value' => '23:59'],
['config_key' => 'fall_semester_start', 'config_value' => '2025-09-01'],
['config_key' => 'spring_semester_start', 'config_value' => '2026-01-15'],
['config_key' => 'last_school_day', 'config_value' => '2026-06-15'],
]);
}
private function nextSunday(): string
{
$dt = new \DateTime('today');
if ((int) $dt->format('w') !== 0) {
$dt->modify('next sunday');
}
return $dt->format('Y-m-d');
}
}
@@ -0,0 +1,125 @@
<?php
namespace Tests\Feature\Api\V1\Parents;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class ParentControllerTest extends TestCase
{
use RefreshDatabase;
public function test_attendance_returns_rows(): void
{
$this->seedConfig();
$parent = $this->seedParent();
$studentId = DB::table('students')->insertGetId([
'school_id' => 'S-100',
'firstname' => 'Adam',
'lastname' => 'Parent',
'dob' => '2015-09-01',
'age' => 10,
'gender' => 'Male',
'photo_consent' => 1,
'parent_id' => $parent->id,
'year_of_registration' => '2025',
'school_year' => '2025-2026',
'semester' => 'Fall',
]);
DB::table('attendance_data')->insert([
'class_id' => 1,
'class_section_id' => 1,
'student_id' => $studentId,
'school_id' => '1',
'date' => '2025-10-01',
'status' => 'absent',
'reason' => 'Sick',
'semester' => 'Fall',
'school_year' => '2025-2026',
]);
Sanctum::actingAs($parent);
$response = $this->getJson('/api/v1/parents/attendance?school_year=2025-2026');
$response->assertOk();
$response->assertJson(['ok' => true]);
$response->assertJsonPath('attendance.0.status', 'absent');
}
public function test_registration_creates_student_and_contact(): void
{
$this->seedConfig();
$parent = $this->seedParent();
Sanctum::actingAs($parent);
$payload = [
'students' => [
[
'firstname' => 'Sara',
'lastname' => 'Parent',
'dob' => '2016-02-01',
'gender' => 'Female',
'registration_grade' => '2',
'photo_consent' => true,
'is_new' => true,
'allergies' => ['Peanuts'],
'medical_conditions' => ['Asthma'],
],
],
'emergency_contacts' => [
[
'name' => 'John Parent',
'cellphone' => '1234567890',
'email' => 'john.parent@example.com',
'relation' => 'Father',
],
],
];
$response = $this->postJson('/api/v1/parents/registration', $payload);
$response->assertStatus(201);
$response->assertJson(['ok' => true]);
$this->assertDatabaseHas('students', ['firstname' => 'Sara', 'parent_id' => $parent->id]);
$this->assertDatabaseHas('emergency_contacts', ['parent_id' => $parent->id, 'emergency_contact_name' => 'John Parent']);
}
private function seedParent(): User
{
$id = DB::table('users')->insertGetId([
'firstname' => 'Parent',
'lastname' => 'User',
'cellphone' => '1234567890',
'email' => 'parent@example.com',
'address_street' => '123 Street',
'city' => 'City',
'state' => 'ST',
'zip' => '12345',
'accept_school_policy' => 1,
'password' => bcrypt('password'),
'user_type' => 'primary',
'semester' => 'Fall',
'school_year' => '2025-2026',
'status' => 'Active',
]);
return User::query()->findOrFail($id);
}
private function seedConfig(): void
{
DB::table('configuration')->insert([
['config_key' => 'school_year', 'config_value' => '2025-2026'],
['config_key' => 'semester', 'config_value' => 'Fall'],
['config_key' => 'date_age_reference', 'config_value' => '2025-12-31'],
['config_key' => 'max_kids', 'config_value' => '5'],
['config_key' => 'max_emergency', 'config_value' => '5'],
['config_key' => 'enrollment_deadline', 'config_value' => '2025-09-01'],
]);
}
}
@@ -0,0 +1,108 @@
<?php
namespace Tests\Feature\Api\V1\Reports;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class SlipPrinterControllerTest extends TestCase
{
use RefreshDatabase;
public function test_preview_returns_text(): void
{
$this->seedConfig();
$user = $this->seedUser();
Sanctum::actingAs($user);
$response = $this->postJson('/api/v1/reports/slips/preview', [
'student_name' => 'Ali Student',
'school_year' => '2025-2026',
]);
$response->assertOk();
$response->assertJson(['ok' => true]);
$this->assertNotEmpty($response->json('text'));
}
public function test_print_creates_log_and_returns_pdf(): void
{
$this->seedConfig();
$user = $this->seedUser();
Sanctum::actingAs($user);
$response = $this->postJson('/api/v1/reports/slips/print', [
'student_name' => 'Nora Student',
'school_year' => '2025-2026',
'date' => '09/01/2025',
'time_in' => '8:10 AM',
'grade' => '3',
]);
$response->assertOk();
$this->assertSame('application/pdf', $response->headers->get('content-type'));
$this->assertDatabaseHas('late_slip_logs', [
'student_name' => 'Nora Student',
'school_year' => '2025-2026',
]);
}
public function test_logs_returns_rows(): void
{
$this->seedConfig();
$user = $this->seedUser();
DB::table('late_slip_logs')->insert([
'school_year' => '2025-2026',
'semester' => 'fall',
'student_name' => 'Log Student',
'slip_date' => '2025-09-01',
'time_in' => '08:00:00',
'grade' => '2',
'reason' => 'Late',
'admin_name' => 'Admin',
'printed_by' => $user->id,
'printed_at' => now(),
]);
Sanctum::actingAs($user);
$response = $this->getJson('/api/v1/reports/slips/logs?school_year=2025-2026');
$response->assertOk();
$response->assertJson(['ok' => true]);
$this->assertNotEmpty($response->json('logs'));
}
private function seedConfig(): void
{
DB::table('configuration')->insert([
['config_key' => 'school_year', 'config_value' => '2025-2026'],
['config_key' => 'semester', 'config_value' => 'Fall'],
]);
}
private function seedUser(): User
{
$userId = DB::table('users')->insertGetId([
'firstname' => 'Admin',
'lastname' => 'User',
'cellphone' => '9999999999',
'email' => 'slip.printer@example.com',
'address_street' => '123 Street',
'city' => 'City',
'state' => 'ST',
'zip' => '12345',
'accept_school_policy' => 1,
'password' => bcrypt('password'),
'user_type' => 'primary',
'semester' => 'Fall',
'school_year' => '2025-2026',
'status' => 'Active',
]);
return User::query()->findOrFail($userId);
}
}
@@ -0,0 +1,66 @@
<?php
namespace Tests\Feature\Api\V1\Settings;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class ConfigurationControllerTest extends TestCase
{
use RefreshDatabase;
public function test_index_returns_configs(): void
{
DB::table('configuration')->insert([
'config_key' => 'school_year',
'config_value' => '2025-2026',
]);
Sanctum::actingAs($this->seedUser());
$response = $this->getJson('/api/v1/configuration');
$response->assertOk();
$response->assertJson(['ok' => true]);
$this->assertNotEmpty($response->json('configs'));
}
public function test_store_creates_config(): void
{
Sanctum::actingAs($this->seedUser());
$response = $this->postJson('/api/v1/configuration', [
'config_key' => 'semester',
'config_value' => 'Fall',
]);
$response->assertStatus(201);
$this->assertDatabaseHas('configuration', [
'config_key' => 'semester',
'config_value' => 'Fall',
]);
}
private function seedUser(): User
{
$userId = DB::table('users')->insertGetId([
'firstname' => 'Admin',
'lastname' => 'User',
'cellphone' => '3333333333',
'email' => 'config@example.com',
'address_street' => '123 Street',
'city' => 'City',
'state' => 'ST',
'zip' => '12345',
'accept_school_policy' => 1,
'password' => bcrypt('password'),
'user_type' => 'primary',
'semester' => 'Fall',
'school_year' => '2025-2026',
'status' => 'Active',
]);
return User::query()->findOrFail($userId);
}
}
@@ -0,0 +1,128 @@
<?php
namespace Tests\Feature\Api\V1\Students;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class StudentControllerTest extends TestCase
{
use RefreshDatabase;
public function test_assign_and_remove_class(): void
{
$this->seedConfig();
$user = $this->seedUser();
$studentId = $this->seedStudent();
$classSectionId = $this->seedClassSection();
Sanctum::actingAs($user);
$assignResponse = $this->postJson('/api/v1/students/assign-class', [
'student_id' => $studentId,
'class_section_id' => [$classSectionId],
]);
$assignResponse->assertOk();
$this->assertDatabaseHas('student_class', [
'student_id' => $studentId,
'class_section_id' => $classSectionId,
]);
$removeResponse = $this->postJson('/api/v1/students/remove-class', [
'student_id' => $studentId,
'class_section_id' => $classSectionId,
]);
$removeResponse->assertOk();
$this->assertDatabaseMissing('student_class', [
'student_id' => $studentId,
'class_section_id' => $classSectionId,
]);
}
public function test_score_card_returns_rows(): void
{
$this->seedConfig();
$user = $this->seedUser();
$studentId = $this->seedStudent();
$this->seedClassSection();
DB::table('semester_scores')->insert([
'student_id' => $studentId,
'class_section_id' => 701,
'school_year' => '2025-2026',
'semester' => 'Fall',
'semester_score' => 90,
]);
Sanctum::actingAs($user);
$response = $this->getJson('/api/v1/students/' . $studentId . '/score-card');
$response->assertOk();
$response->assertJson(['ok' => true]);
$this->assertNotEmpty($response->json('rows'));
}
private function seedConfig(): void
{
DB::table('configuration')->insert([
['config_key' => 'school_year', 'config_value' => '2025-2026'],
['config_key' => 'semester', 'config_value' => 'Fall'],
]);
}
private function seedUser(): User
{
$userId = DB::table('users')->insertGetId([
'firstname' => 'Admin',
'lastname' => 'User',
'cellphone' => '7777777777',
'email' => 'student.controller@example.com',
'address_street' => '123 Street',
'city' => 'City',
'state' => 'ST',
'zip' => '12345',
'accept_school_policy' => 1,
'password' => bcrypt('password'),
'user_type' => 'primary',
'semester' => 'Fall',
'school_year' => '2025-2026',
'status' => 'Active',
]);
return User::query()->findOrFail($userId);
}
private function seedStudent(): int
{
return DB::table('students')->insertGetId([
'school_id' => 'S-900',
'firstname' => 'Omar',
'lastname' => 'Student',
'dob' => '2014-09-01',
'age' => 11,
'gender' => 'Male',
'photo_consent' => 1,
'parent_id' => 1,
'year_of_registration' => '2025',
'school_year' => '2025-2026',
'semester' => 'Fall',
]);
}
private function seedClassSection(): int
{
DB::table('classSection')->insert([
'class_id' => 1,
'class_section_id' => 701,
'class_section_name' => '7A',
'semester' => 'Fall',
'school_year' => '2025-2026',
]);
return 701;
}
}
@@ -0,0 +1,95 @@
<?php
namespace Tests\Feature\Api\V1\Subjects;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class SubjectCurriculumControllerTest extends TestCase
{
use RefreshDatabase;
public function test_index_returns_entries(): void
{
$user = $this->seedUser();
$classId = $this->seedClass();
$this->seedEntry($classId);
Sanctum::actingAs($user);
$response = $this->getJson('/api/v1/subjects/curriculum');
$response->assertOk();
$response->assertJson(['ok' => true]);
$this->assertNotEmpty($response->json('entries'));
}
public function test_store_creates_entry(): void
{
$user = $this->seedUser();
$classId = $this->seedClass();
Sanctum::actingAs($user);
$response = $this->postJson('/api/v1/subjects/curriculum', [
'class_id' => $classId,
'subject' => 'islamic',
'unit_number' => 1,
'unit_title' => 'Faith',
'chapter_name' => 'Intro',
]);
$response->assertStatus(201);
$this->assertDatabaseHas('subject_curriculum_items', [
'class_id' => $classId,
'chapter_name' => 'Intro',
]);
}
private function seedUser(): User
{
$userId = DB::table('users')->insertGetId([
'firstname' => 'Admin',
'lastname' => 'User',
'cellphone' => '1111111111',
'email' => 'curriculum@example.com',
'address_street' => '123 Street',
'city' => 'City',
'state' => 'ST',
'zip' => '12345',
'accept_school_policy' => 1,
'password' => bcrypt('password'),
'user_type' => 'primary',
'semester' => 'Fall',
'school_year' => '2025-2026',
'status' => 'Active',
]);
return User::query()->findOrFail($userId);
}
private function seedClass(): int
{
return DB::table('classes')->insertGetId([
'class_name' => 'Grade 2',
'schedule' => 'Sunday',
'capacity' => 25,
'semester' => 'Fall',
'school_year' => '2025-2026',
]);
}
private function seedEntry(int $classId): void
{
DB::table('subject_curriculum_items')->insert([
'class_id' => $classId,
'subject' => 'islamic',
'unit_number' => 1,
'unit_title' => 'Faith',
'chapter_name' => 'Intro',
'created_at' => now(),
'updated_at' => now(),
]);
}
}
@@ -0,0 +1,61 @@
<?php
namespace Tests\Feature\Api\V1\System;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class SchoolIdControllerTest extends TestCase
{
use RefreshDatabase;
public function test_generate_student_returns_id(): void
{
$user = User::factory()->create();
Sanctum::actingAs($user);
$response = $this->getJson('/api/v1/system/school-ids/student');
$response->assertOk();
$response->assertJson(['ok' => true]);
$response->assertJsonPath('result.type', 'student');
$this->assertNotEmpty($response->json('result.school_id'));
}
public function test_generate_user_returns_id(): void
{
$user = User::factory()->create();
Sanctum::actingAs($user);
$response = $this->getJson('/api/v1/system/school-ids/user');
$response->assertOk();
$response->assertJson(['ok' => true]);
$response->assertJsonPath('result.type', 'user');
$this->assertNotEmpty($response->json('result.school_id'));
}
public function test_assign_sets_user_school_id(): void
{
$user = User::factory()->create();
Sanctum::actingAs($user);
$target = User::factory()->create([
'school_id' => null,
]);
$response = $this->postJson('/api/v1/system/school-ids/assign', [
'user_id' => $target->id,
]);
$response->assertOk();
$response->assertJson(['ok' => true]);
$this->assertNotEmpty($response->json('result.school_id'));
$this->assertSame(
$response->json('result.school_id'),
User::query()->findOrFail($target->id)->school_id
);
}
}
@@ -0,0 +1,166 @@
<?php
namespace Tests\Feature\Api\V1\Teachers;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class TeacherControllerTest extends TestCase
{
use RefreshDatabase;
public function test_classes_returns_students_and_assignments(): void
{
$this->seedConfig();
$teacher = $this->seedTeacherUser();
$classSectionId = $this->seedClassSection();
$this->seedTeacherAssignment($teacher->id, $classSectionId);
$this->seedStudent($teacher->id, $classSectionId);
Sanctum::actingAs($teacher);
$response = $this->getJson('/api/v1/teachers/classes?class_section_id=' . $classSectionId);
$response->assertOk();
$response->assertJson(['ok' => true]);
$this->assertNotEmpty($response->json('students'));
$this->assertSame($classSectionId, $response->json('active_class_section_id'));
}
public function test_switch_class_returns_active_id(): void
{
$this->seedConfig();
$teacher = $this->seedTeacherUser();
$classSectionId = $this->seedClassSection();
$this->seedTeacherAssignment($teacher->id, $classSectionId);
Sanctum::actingAs($teacher);
$response = $this->postJson('/api/v1/teachers/classes/switch', [
'class_section_id' => $classSectionId,
]);
$response->assertOk();
$response->assertJsonPath('active_class_section_id', $classSectionId);
}
public function test_absence_submit_creates_staff_attendance(): void
{
Carbon::setTestNow(Carbon::parse('2025-10-01'));
$this->seedConfig();
$teacher = $this->seedTeacherUser();
Sanctum::actingAs($teacher);
$formResponse = $this->getJson('/api/v1/teachers/absence/form');
$formResponse->assertOk();
$date = $formResponse->json('available_dates.0');
$response = $this->postJson('/api/v1/teachers/absence', [
'dates' => [$date],
'reason' => 'Family event',
'reason_type' => 'personal',
]);
$response->assertOk();
$this->assertDatabaseHas('staff_attendance', [
'user_id' => $teacher->id,
'date' => $date,
'status' => 'absent',
]);
Carbon::setTestNow();
}
private function seedConfig(): void
{
DB::table('configuration')->insert([
['config_key' => 'school_year', 'config_value' => '2025-2026'],
['config_key' => 'semester', 'config_value' => 'Fall'],
['config_key' => 'fall_semester_start', 'config_value' => '2025-09-01'],
['config_key' => 'spring_semester_start', 'config_value' => '2026-01-15'],
['config_key' => 'last_school_day', 'config_value' => '2026-06-15'],
]);
}
private function seedTeacherUser(): User
{
$roleId = DB::table('roles')->insertGetId([
'name' => 'teacher',
]);
$userId = DB::table('users')->insertGetId([
'firstname' => 'Tara',
'lastname' => 'Teacher',
'cellphone' => '4444444444',
'email' => 'teacher.feature@example.com',
'address_street' => '123 Street',
'city' => 'City',
'state' => 'ST',
'zip' => '12345',
'accept_school_policy' => 1,
'password' => bcrypt('password'),
'user_type' => 'primary',
'semester' => 'Fall',
'school_year' => '2025-2026',
'status' => 'Active',
]);
DB::table('user_roles')->insert([
'user_id' => $userId,
'role_id' => $roleId,
]);
return User::query()->findOrFail($userId);
}
private function seedClassSection(): int
{
DB::table('classSection')->insert([
'class_id' => 1,
'class_section_id' => 301,
'class_section_name' => '3A',
'semester' => 'Fall',
'school_year' => '2025-2026',
]);
return 301;
}
private function seedTeacherAssignment(int $teacherId, int $classSectionId): void
{
DB::table('teacher_class')->insert([
'teacher_id' => $teacherId,
'class_section_id' => $classSectionId,
'position' => 'main',
'school_year' => '2025-2026',
'created_at' => now(),
'updated_at' => now(),
]);
}
private function seedStudent(int $parentId, int $classSectionId): void
{
$studentId = DB::table('students')->insertGetId([
'school_id' => 'S-600',
'firstname' => 'Kai',
'lastname' => 'Student',
'dob' => '2014-09-01',
'age' => 11,
'gender' => 'Male',
'photo_consent' => 1,
'parent_id' => $parentId,
'year_of_registration' => '2025',
'school_year' => '2025-2026',
'semester' => 'Fall',
]);
DB::table('student_class')->insert([
'student_id' => $studentId,
'class_section_id' => $classSectionId,
'semester' => 'Fall',
'school_year' => '2025-2026',
]);
}
}