238 lines
7.6 KiB
PHP
238 lines
7.6 KiB
PHP
<?php
|
||
|
||
namespace Tests\Feature\Api;
|
||
|
||
use App\Models\User;
|
||
use App\Models\Student;
|
||
use App\Models\StudentClass;
|
||
use App\Models\TeacherClass;
|
||
use App\Models\ClassSection;
|
||
use App\Models\Configuration;
|
||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||
use Laravel\Sanctum\Sanctum;
|
||
use PHPUnit\Framework\Attributes\Test;
|
||
use Tests\TestCase;
|
||
|
||
class AssignmentApiTest extends TestCase
|
||
{
|
||
use RefreshDatabase;
|
||
|
||
protected User $user;
|
||
|
||
protected function setUp(): void
|
||
{
|
||
parent::setUp();
|
||
|
||
// If your app uses Sanctum auth
|
||
$this->user = User::factory()->create();
|
||
Sanctum::actingAs($this->user);
|
||
|
||
// Seed config values used by AssignmentService
|
||
// Adjust columns if your configuration table schema differs
|
||
Configuration::query()->updateOrCreate(
|
||
['key' => 'semester'],
|
||
['value' => 'Fall']
|
||
);
|
||
|
||
Configuration::query()->updateOrCreate(
|
||
['key' => 'school_year'],
|
||
['value' => '2025-2026']
|
||
);
|
||
}
|
||
|
||
#[Test]
|
||
public function it_returns_assignment_sections_json(): void
|
||
{
|
||
$section = ClassSection::factory()->create([
|
||
'name' => 'Grade 5 - A', // or title depending on your schema
|
||
]);
|
||
|
||
$teacher = User::factory()->create([
|
||
'firstname' => 'Ahmad',
|
||
'lastname' => 'Ali',
|
||
]);
|
||
|
||
TeacherClass::query()->create([
|
||
'teacher_id' => $teacher->id,
|
||
'class_section_id' => $section->id,
|
||
'position' => 'main',
|
||
'semester' => 'Fall',
|
||
'school_year' => '2025-2026',
|
||
'description' => 'Main teacher assignment',
|
||
]);
|
||
|
||
$student = Student::factory()->create([
|
||
'firstname' => 'Sara',
|
||
'lastname' => 'Yousef',
|
||
'is_active' => 1,
|
||
'gender' => 'F',
|
||
'registration_grade' => '5',
|
||
'photo_consent' => 1,
|
||
'tuition_paid' => 1,
|
||
'school_id' => 'SCH-1001',
|
||
]);
|
||
|
||
StudentClass::query()->create([
|
||
'student_id' => $student->id,
|
||
'class_section_id' => $section->id,
|
||
'semester' => 'Fall',
|
||
'school_year' => '2025-2026',
|
||
'description' => 'Student assigned',
|
||
'is_active' => 1, // remove if your table doesn’t have this column
|
||
]);
|
||
|
||
$response = $this->getJson('/api/assignments');
|
||
|
||
$response->assertOk()
|
||
->assertJsonStructure([
|
||
'classSections' => [
|
||
'*' => [
|
||
'class_section_id',
|
||
'class_section_name',
|
||
'main_teachers',
|
||
'teacher_assistants',
|
||
'students',
|
||
'semester',
|
||
'school_year',
|
||
'description',
|
||
]
|
||
],
|
||
'schoolYears',
|
||
'selectedYear',
|
||
'selectedSemester',
|
||
'semester',
|
||
'school_year',
|
||
]);
|
||
|
||
$response->assertJsonPath('classSections.0.class_section_id', $section->id);
|
||
$response->assertJsonPath('classSections.0.main_teachers.0', 'Ahmad Ali');
|
||
$response->assertJsonPath('classSections.0.students.0.id', $student->id);
|
||
}
|
||
|
||
#[Test]
|
||
public function it_filters_assignments_by_school_year(): void
|
||
{
|
||
$section1 = ClassSection::factory()->create(['name' => 'Grade 4']);
|
||
$section2 = ClassSection::factory()->create(['name' => 'Grade 6']);
|
||
|
||
$teacher = User::factory()->create(['firstname' => 'Test', 'lastname' => 'Teacher']);
|
||
|
||
TeacherClass::query()->create([
|
||
'teacher_id' => $teacher->id,
|
||
'class_section_id' => $section1->id,
|
||
'position' => 'main',
|
||
'semester' => 'Fall',
|
||
'school_year' => '2024-2025',
|
||
]);
|
||
|
||
TeacherClass::query()->create([
|
||
'teacher_id' => $teacher->id,
|
||
'class_section_id' => $section2->id,
|
||
'position' => 'main',
|
||
'semester' => 'Fall',
|
||
'school_year' => '2025-2026',
|
||
]);
|
||
|
||
$response = $this->getJson('/api/assignments?school_year=2025-2026');
|
||
|
||
$response->assertOk();
|
||
|
||
$sections = $response->json('classSections');
|
||
$ids = collect($sections)->pluck('class_section_id')->all();
|
||
|
||
$this->assertContains($section2->id, $ids);
|
||
$this->assertNotContains($section1->id, $ids);
|
||
$response->assertJsonPath('selectedYear', '2025-2026');
|
||
}
|
||
|
||
#[Test]
|
||
public function it_creates_or_updates_student_assignment(): void
|
||
{
|
||
$student = Student::factory()->create(['is_active' => 1]);
|
||
$section = ClassSection::factory()->create();
|
||
|
||
$payload = [
|
||
'student_id' => $student->id,
|
||
'class_section_id' => $section->id,
|
||
'semester' => 'Spring',
|
||
'school_year' => '2025-2026',
|
||
'description' => 'Moved after placement review',
|
||
];
|
||
|
||
$response = $this->postJson('/api/assignments', $payload);
|
||
|
||
$response->assertCreated()
|
||
->assertJsonPath('message', 'Assignment saved successfully');
|
||
|
||
$this->assertDatabaseHas('student_class', [
|
||
'student_id' => $student->id,
|
||
'class_section_id' => $section->id,
|
||
'semester' => 'Spring',
|
||
'school_year' => '2025-2026',
|
||
]);
|
||
}
|
||
|
||
#[Test]
|
||
public function it_validates_required_fields_when_storing_assignment(): void
|
||
{
|
||
$response = $this->postJson('/api/assignments', []);
|
||
|
||
$response->assertStatus(422)
|
||
->assertJsonValidationErrors([
|
||
'student_id',
|
||
'class_section_id',
|
||
]);
|
||
}
|
||
|
||
#[Test]
|
||
public function store_is_idempotent_for_same_student_semester_and_year(): void
|
||
{
|
||
$student = Student::factory()->create(['is_active' => 1]);
|
||
$sectionA = ClassSection::factory()->create();
|
||
$sectionB = ClassSection::factory()->create();
|
||
|
||
// First create
|
||
$this->postJson('/api/assignments', [
|
||
'student_id' => $student->id,
|
||
'class_section_id' => $sectionA->id,
|
||
'semester' => 'Fall',
|
||
'school_year' => '2025-2026',
|
||
])->assertCreated();
|
||
|
||
// Second call same unique key should update (per service updateOrCreate)
|
||
$this->postJson('/api/assignments', [
|
||
'student_id' => $student->id,
|
||
'class_section_id' => $sectionB->id,
|
||
'semester' => 'Fall',
|
||
'school_year' => '2025-2026',
|
||
'description' => 'Reassigned',
|
||
])->assertCreated();
|
||
|
||
$this->assertEquals(
|
||
1,
|
||
StudentClass::query()
|
||
->where('student_id', $student->id)
|
||
->where('semester', 'Fall')
|
||
->where('school_year', '2025-2026')
|
||
->count()
|
||
);
|
||
|
||
$this->assertDatabaseHas('student_class', [
|
||
'student_id' => $student->id,
|
||
'class_section_id' => $sectionB->id,
|
||
'semester' => 'Fall',
|
||
'school_year' => '2025-2026',
|
||
]);
|
||
}
|
||
|
||
#[Test]
|
||
public function guest_cannot_access_assignment_api_when_auth_is_required(): void
|
||
{
|
||
auth()->guard('sanctum')->logout();
|
||
|
||
$response = $this->getJson('/api/assignments');
|
||
|
||
$response->assertStatus(401);
|
||
}
|
||
}
|