Files
alrahma_sunday_school_api/tests/Feature/Api/V1/Parents/ParentControllerTest.php
T
root 02ba2fe6b6
API CI/CD / Validate (composer + pint) (push) Successful in 3m8s
API CI/CD / Build frontend assets (push) Successful in 1m2s
API CI/CD / Test (PHPUnit) (push) Failing after 6m5s
API CI/CD / Security audit (push) Failing after 52s
API CI/CD / Deploy to shared hosting (PHP) (push) Has been skipped
fix parent pages and teachers and admin
2026-07-09 13:56:22 -04:00

203 lines
6.7 KiB
PHP

<?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-02',
'status' => 'present',
'reason' => null,
'semester' => 'Fall',
'school_year' => '2025-2026',
],
[
'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]);
$this->assertCount(1, $response->json('attendance'));
$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']);
}
public function test_statement_returns_parent_account_and_invoice_lines(): void
{
$this->seedConfig();
$parent = $this->seedParent();
DB::table('parent_accounts')->insert([
'parent_id' => $parent->id,
'school_year' => '2025-2026',
'opening_balance' => 25,
'current_balance' => 125,
'created_at' => now(),
'updated_at' => now(),
]);
DB::table('invoices')->insert([
'parent_id' => $parent->id,
'invoice_number' => 'INV-2025-001',
'total_amount' => 150,
'paid_amount' => 50,
'balance' => 100,
'has_discount' => 0,
'issue_date' => now(),
'due_date' => now()->addWeek(),
'status' => 'unpaid',
'description' => 'Tuition invoice',
'school_year' => '2025-2026',
'semester' => 'Fall',
'created_at' => now(),
'updated_at' => now(),
]);
Sanctum::actingAs($parent);
$response = $this->getJson('/api/v1/parents/statements?school_year=2025-2026');
$response->assertOk()
->assertJsonPath('ok', true)
->assertJsonPath('data.parent_id', $parent->id)
->assertJsonPath('data.parent_name', 'Parent User')
->assertJsonPath('data.school_year', '2025-2026')
->assertJsonPath('data.opening_balance', 25)
->assertJsonPath('data.current_balance', 125)
->assertJsonPath('data.lines.0.label', 'Tuition invoice')
->assertJsonPath('data.lines.0.amount', 100)
->assertJsonPath('data.lines.0.source_school_year', '2025-2026');
}
private function seedParent(): User
{
$roleId = DB::table('roles')->insertGetId([
'name' => 'parent',
'slug' => 'parent',
'is_active' => 1,
'created_at' => now(),
'updated_at' => now(),
]);
$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',
'is_verified' => 1,
'is_suspended' => 0,
]);
DB::table('user_roles')->insert([
'user_id' => $id,
'role_id' => $roleId,
'created_at' => now(),
'updated_at' => now(),
]);
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'],
]);
}
}