Files
alrahma_sunday_school_api/tests/Feature/Api/V1/Incidents/IncidentControllerTest.php
T
2026-03-09 02:52:13 -04:00

159 lines
4.8 KiB
PHP

<?php
namespace Tests\Feature\Api\V1\Incidents;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class IncidentControllerTest extends TestCase
{
use RefreshDatabase;
public function test_current_returns_incidents_and_grades(): void
{
DB::table('configuration')->insert([
['id' => 1, 'config_key' => 'school_year', 'config_value' => '2025-2026'],
['id' => 2, 'config_key' => 'semester', 'config_value' => 'Fall'],
]);
$user = $this->seedUser();
DB::table('classSection')->insert([
'id' => 1,
'class_section_id' => 1,
'class_section_name' => '1A',
'class_id' => 1,
'semester' => 'Fall',
'school_year' => '2025-2026',
]);
DB::table('students')->insert([
'id' => 10,
'parent_id' => 99,
'firstname' => 'Kid',
'lastname' => 'Tester',
'school_id' => '1',
'age' => 10,
'gender' => 'M',
'photo_consent' => 1,
'year_of_registration' => '2025',
'is_active' => 1,
]);
DB::table('student_class')->insert([
'id' => 1,
'student_id' => 10,
'class_section_id' => 1,
'school_year' => '2025-2026',
'semester' => 'Fall',
'is_event_only' => 0,
]);
DB::table('current_incident')->insert([
'id' => 1,
'student_id' => 10,
'student_name' => 'Kid Tester',
'grade' => '1',
'incident' => 'behavior',
'incident_datetime' => '2025-01-01 10:00:00',
'incident_state' => 'Open',
'updated_by_open' => $user->id,
'open_description' => 'Initial',
'semester' => 'Fall',
'school_year' => '2025-2026',
]);
Sanctum::actingAs($user);
$response = $this->getJson('/api/v1/incidents/current');
$response->assertOk();
$response->assertJson(['ok' => true]);
$response->assertJsonCount(1, 'incidents');
$response->assertJsonCount(1, 'grades');
$response->assertJsonPath('incidents.0.updated_by_open_name', 'Test User');
}
public function test_store_and_close_moves_incident_to_history(): void
{
DB::table('configuration')->insert([
['id' => 1, 'config_key' => 'school_year', 'config_value' => '2025-2026'],
['id' => 2, 'config_key' => 'semester', 'config_value' => 'Fall'],
]);
$user = $this->seedUser();
DB::table('students')->insert([
'id' => 11,
'parent_id' => 99,
'firstname' => 'Kid',
'lastname' => 'Closer',
'school_id' => '1',
'age' => 11,
'gender' => 'F',
'photo_consent' => 1,
'year_of_registration' => '2025',
'is_active' => 1,
]);
Sanctum::actingAs($user);
$storeResponse = $this->postJson('/api/v1/incidents', [
'student_id' => 11,
'grade' => '1',
'incident' => 'behavior',
'description' => 'Needs attention',
]);
$storeResponse->assertOk();
$storeResponse->assertJson(['ok' => true, 'created' => true]);
$currentId = $storeResponse->json('incident_id');
$closeResponse = $this->postJson('/api/v1/incidents/' . $currentId . '/close', [
'state_description' => 'Resolved',
'action_taken' => 'Spoke with student',
]);
$closeResponse->assertOk();
$closeResponse->assertJson(['ok' => true]);
$historyId = $closeResponse->json('incident_id');
$this->assertDatabaseHas('incident', [
'id' => $historyId,
'student_id' => 11,
'incident_state' => 'Closed',
]);
$this->assertDatabaseMissing('current_incident', ['id' => $currentId]);
}
private function seedUser(): User
{
DB::table('users')->insert([
'id' => 1,
'school_id' => 1,
'firstname' => 'Test',
'lastname' => 'User',
'cellphone' => '5555555555',
'email' => 'test@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',
]);
return User::query()->findOrFail(1);
}
}