add school calendar logic

This commit is contained in:
root
2026-03-10 16:55:50 -04:00
parent 311bb93977
commit abebe0d9c0
25 changed files with 1358 additions and 750 deletions
@@ -0,0 +1,230 @@
<?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 SchoolCalendarControllerTest extends TestCase
{
use RefreshDatabase;
public function test_options_returns_event_types_and_defaults(): void
{
$this->seedConfig();
$user = $this->seedUser();
Sanctum::actingAs($user);
$response = $this->getJson('/api/v1/settings/school-calendar/options');
$response->assertOk();
$response->assertJsonPath('status', true);
$this->assertNotEmpty($response->json('data.event_types'));
$this->assertSame('2025-2026', $response->json('data.defaults.school_year'));
}
public function test_index_returns_events_and_meetings(): void
{
$this->seedConfig();
$user = $this->seedUser();
$this->seedEvent();
$this->seedMeeting();
Sanctum::actingAs($user);
$response = $this->getJson('/api/v1/settings/school-calendar/events?school_year=2025-2026&include_meetings=1');
$response->assertOk();
$response->assertJsonPath('status', true);
$this->assertNotEmpty($response->json('data.events'));
$this->assertArrayHasKey('extendedProps', $response->json('data.events.0'));
}
public function test_show_returns_event(): void
{
$this->seedConfig();
$user = $this->seedUser();
$eventId = $this->seedEvent();
Sanctum::actingAs($user);
$response = $this->getJson('/api/v1/settings/school-calendar/events/' . $eventId);
$response->assertOk();
$response->assertJsonPath('status', true);
$response->assertJsonPath('data.event.id', $eventId);
}
public function test_store_creates_event(): void
{
$this->seedConfig();
$user = $this->seedUser();
Sanctum::actingAs($user);
$payload = [
'title' => 'Calendar Event',
'date' => '2026-01-10',
'description' => 'Description',
'semester' => 'Fall',
'school_year' => '2025-2026',
];
$response = $this->postJson('/api/v1/settings/school-calendar/events', $payload);
$response->assertStatus(201);
$response->assertJsonPath('status', true);
$this->assertDatabaseHas('calendar_events', [
'title' => 'Calendar Event',
'school_year' => '2025-2026',
]);
}
public function test_update_updates_event(): void
{
$this->seedConfig();
$user = $this->seedUser();
$eventId = $this->seedEvent();
Sanctum::actingAs($user);
$response = $this->patchJson('/api/v1/settings/school-calendar/events/' . $eventId, [
'title' => 'Updated Event',
]);
$response->assertOk();
$response->assertJsonPath('status', true);
$this->assertDatabaseHas('calendar_events', [
'id' => $eventId,
'title' => 'Updated Event',
]);
}
public function test_destroy_deletes_event(): void
{
$this->seedConfig();
$user = $this->seedUser();
$eventId = $this->seedEvent();
Sanctum::actingAs($user);
$response = $this->deleteJson('/api/v1/settings/school-calendar/events/' . $eventId);
$response->assertOk();
$response->assertJsonPath('status', true);
$this->assertDatabaseMissing('calendar_events', ['id' => $eventId]);
}
public function test_store_validation_rejects_invalid_payload(): void
{
$this->seedConfig();
$user = $this->seedUser();
Sanctum::actingAs($user);
$response = $this->postJson('/api/v1/settings/school-calendar/events', [
'title' => 'No date',
]);
$response->assertStatus(422);
$response->assertJsonPath('message', 'Validation failed.');
}
public function test_store_returns_error_on_service_exception(): void
{
$this->seedConfig();
$user = $this->seedUser();
Sanctum::actingAs($user);
$this->app->bind(\App\Services\Settings\SchoolCalendar\SchoolCalendarMutationService::class, function () {
return new class {
public function create(array $payload, array $targets = []): array
{
throw new \RuntimeException('fail');
}
};
});
$response = $this->postJson('/api/v1/settings/school-calendar/events', [
'title' => 'Event',
'date' => '2026-01-10',
'semester' => 'Fall',
'school_year' => '2025-2026',
]);
$response->assertStatus(500);
$response->assertJsonPath('status', false);
}
public function test_requires_authentication(): void
{
$response = $this->getJson('/api/v1/settings/school-calendar/events');
$response->assertStatus(401);
}
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' => 'calendar@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 seedEvent(): int
{
return DB::table('calendar_events')->insertGetId([
'title' => 'Event',
'date' => '2026-01-01',
'description' => 'Desc',
'notify_parent' => 1,
'notify_admin' => 0,
'notify_teacher' => 0,
'no_school' => 0,
'semester' => 'Fall',
'school_year' => '2025-2026',
'notification_sent' => 0,
'created_at' => now(),
'updated_at' => now(),
]);
}
private function seedMeeting(): void
{
DB::table('parent_meeting_schedules')->insert([
'student_id' => 1,
'parent_user_id' => 1,
'parent_name' => 'Parent',
'student_name' => 'Student',
'class_section_name' => 'Grade 1',
'date' => '2026-01-02',
'time' => '10:00:00',
'notes' => 'Notes',
'semester' => 'Fall',
'school_year' => '2025-2026',
'status' => 'scheduled',
'created_by' => null,
'created_at' => now(),
'updated_at' => now(),
]);
}
}
@@ -0,0 +1,28 @@
<?php
namespace Tests\Unit\Resources\Settings;
use App\Http\Resources\Settings\SchoolCalendarEventResource;
use Tests\TestCase;
class SchoolCalendarEventResourceTest extends TestCase
{
public function test_resource_returns_expected_shape(): void
{
$resource = new SchoolCalendarEventResource([
'id' => 1,
'title' => 'Event',
'start' => '2026-01-01',
'description' => 'Desc',
'extendedProps' => ['no_school' => 0],
]);
$payload = $resource->toArray(request());
$this->assertArrayHasKey('id', $payload);
$this->assertArrayHasKey('title', $payload);
$this->assertArrayHasKey('start', $payload);
$this->assertArrayHasKey('description', $payload);
$this->assertArrayHasKey('extendedProps', $payload);
}
}
@@ -0,0 +1,18 @@
<?php
namespace Tests\Unit\Services\Settings\SchoolCalendar;
use App\Services\Settings\SchoolCalendar\SchoolCalendarContextService;
use Tests\TestCase;
class SchoolCalendarContextServiceTest extends TestCase
{
public function test_event_types_returns_list(): void
{
$service = new SchoolCalendarContextService();
$types = $service->eventTypes();
$this->assertNotEmpty($types);
$this->assertContains('Event', $types);
}
}
@@ -0,0 +1,26 @@
<?php
namespace Tests\Unit\Services\Settings\SchoolCalendar;
use App\Services\Settings\SchoolCalendar\SchoolCalendarFormatterService;
use Tests\TestCase;
class SchoolCalendarFormatterServiceTest extends TestCase
{
public function test_format_event_sets_background_for_no_school(): void
{
$service = new SchoolCalendarFormatterService();
$formatted = $service->formatEvent([
'id' => 1,
'title' => 'No School Day',
'date' => '2026-01-01',
'no_school' => 1,
'notify_parent' => 0,
'notify_teacher' => 0,
'notify_admin' => 0,
], 'parent');
$this->assertSame('#ffc107', $formatted['extendedProps']['backgroundColor']);
$this->assertSame('No School Day', $formatted['title']);
}
}
@@ -0,0 +1,57 @@
<?php
namespace Tests\Unit\Services\Settings\SchoolCalendar;
use App\Services\Settings\SchoolCalendar\SchoolCalendarMeetingService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
class SchoolCalendarMeetingServiceTest extends TestCase
{
use RefreshDatabase;
public function test_list_meetings_filters_for_parent(): void
{
DB::table('parent_meeting_schedules')->insert([
[
'student_id' => 1,
'parent_user_id' => 10,
'parent_name' => 'Parent',
'student_name' => 'Student',
'class_section_name' => 'Grade 1',
'date' => '2026-01-02',
'time' => '10:00:00',
'notes' => 'Notes',
'semester' => 'Fall',
'school_year' => '2025-2026',
'status' => 'scheduled',
'created_by' => null,
'created_at' => now(),
'updated_at' => now(),
],
[
'student_id' => 2,
'parent_user_id' => 11,
'parent_name' => 'Other Parent',
'student_name' => 'Other Student',
'class_section_name' => 'Grade 2',
'date' => '2026-01-03',
'time' => '11:00:00',
'notes' => 'Notes',
'semester' => 'Fall',
'school_year' => '2025-2026',
'status' => 'scheduled',
'created_by' => null,
'created_at' => now(),
'updated_at' => now(),
],
]);
$service = new SchoolCalendarMeetingService();
$rows = $service->listMeetings('2025-2026', 'parent', 10);
$this->assertCount(1, $rows);
$this->assertSame(10, $rows[0]['parent_user_id']);
}
}
@@ -0,0 +1,62 @@
<?php
namespace Tests\Unit\Services\Settings\SchoolCalendar;
use App\Models\CalendarEvent;
use App\Services\Settings\SchoolCalendar\SchoolCalendarContextService;
use App\Services\Settings\SchoolCalendar\SchoolCalendarMutationService;
use App\Services\Settings\SchoolCalendar\SchoolCalendarNotificationService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Mockery;
use Tests\TestCase;
class SchoolCalendarMutationServiceTest extends TestCase
{
use RefreshDatabase;
public function test_create_persists_event(): void
{
$context = new SchoolCalendarContextService();
$notifier = Mockery::mock(SchoolCalendarNotificationService::class);
$notifier->shouldReceive('notify')->andReturn([]);
$service = new SchoolCalendarMutationService($context, $notifier);
$result = $service->create([
'title' => 'New Event',
'date' => '2026-01-05',
'semester' => 'Fall',
'school_year' => '2025-2026',
], []);
$this->assertInstanceOf(CalendarEvent::class, $result['event']);
$this->assertDatabaseHas('calendar_events', ['title' => 'New Event']);
}
public function test_update_persists_changes(): void
{
$event = CalendarEvent::query()->create([
'title' => 'Old Event',
'date' => '2026-01-01',
'description' => '',
'notify_parent' => 0,
'notify_admin' => 0,
'notify_teacher' => 0,
'no_school' => 0,
'semester' => 'Fall',
'school_year' => '2025-2026',
'notification_sent' => 0,
]);
$context = new SchoolCalendarContextService();
$notifier = Mockery::mock(SchoolCalendarNotificationService::class);
$notifier->shouldReceive('notify')->andReturn([]);
$service = new SchoolCalendarMutationService($context, $notifier);
$service->update($event, ['title' => 'Updated Event'], []);
$this->assertDatabaseHas('calendar_events', [
'id' => $event->id,
'title' => 'Updated Event',
]);
}
}
@@ -0,0 +1,21 @@
<?php
namespace Tests\Unit\Services\Settings\SchoolCalendar;
use App\Services\EmailService;
use App\Services\Settings\SchoolCalendar\SchoolCalendarNotificationService;
use Mockery;
use Tests\TestCase;
class SchoolCalendarNotificationServiceTest extends TestCase
{
public function test_notify_returns_empty_when_no_targets(): void
{
$emailService = Mockery::mock(EmailService::class);
$service = new SchoolCalendarNotificationService($emailService);
$result = $service->notify(['title' => 'Event'], []);
$this->assertSame([], $result);
}
}
@@ -0,0 +1,94 @@
<?php
namespace Tests\Unit\Services\Settings\SchoolCalendar;
use App\Services\Settings\SchoolCalendar\SchoolCalendarQueryService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
class SchoolCalendarQueryServiceTest extends TestCase
{
use RefreshDatabase;
public function test_list_events_filters_by_school_year(): void
{
DB::table('calendar_events')->insert([
[
'title' => 'Event A',
'date' => '2026-01-01',
'description' => '',
'notify_parent' => 1,
'notify_admin' => 0,
'notify_teacher' => 0,
'no_school' => 0,
'semester' => 'Fall',
'school_year' => '2025-2026',
'notification_sent' => 0,
'created_at' => now(),
'updated_at' => now(),
],
[
'title' => 'Event B',
'date' => '2026-01-02',
'description' => '',
'notify_parent' => 1,
'notify_admin' => 0,
'notify_teacher' => 0,
'no_school' => 0,
'semester' => 'Fall',
'school_year' => '2026-2027',
'notification_sent' => 0,
'created_at' => now(),
'updated_at' => now(),
],
]);
$service = new SchoolCalendarQueryService();
$events = $service->listEvents(['school_year' => '2025-2026']);
$this->assertCount(1, $events);
$this->assertSame('Event A', $events->first()->title);
}
public function test_filter_events_for_audience_limits_visibility(): void
{
DB::table('calendar_events')->insert([
[
'title' => 'Parent Event',
'date' => '2026-01-01',
'description' => '',
'notify_parent' => 1,
'notify_admin' => 0,
'notify_teacher' => 0,
'no_school' => 0,
'semester' => 'Fall',
'school_year' => '2025-2026',
'notification_sent' => 0,
'created_at' => now(),
'updated_at' => now(),
],
[
'title' => 'Teacher Event',
'date' => '2026-01-02',
'description' => '',
'notify_parent' => 0,
'notify_admin' => 0,
'notify_teacher' => 1,
'no_school' => 0,
'semester' => 'Fall',
'school_year' => '2025-2026',
'notification_sent' => 0,
'created_at' => now(),
'updated_at' => now(),
],
]);
$service = new SchoolCalendarQueryService();
$events = $service->listEvents(['school_year' => '2025-2026']);
$filtered = $service->filterEventsForAudience($events, 'parent');
$this->assertCount(1, $filtered);
$this->assertSame('Parent Event', $filtered->first()->title);
}
}