add all controllers logic
This commit is contained in:
@@ -0,0 +1,136 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Api\V1\Attendance;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
use Tests\TestCase;
|
||||
|
||||
class LateSlipLogsControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_index_requires_admin(): void
|
||||
{
|
||||
$user = $this->createUser('teacher');
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = $this->getJson('/api/v1/attendance/late-slip-logs');
|
||||
|
||||
$response->assertForbidden();
|
||||
}
|
||||
|
||||
public function test_index_returns_logs(): void
|
||||
{
|
||||
$admin = $this->createUser('admin');
|
||||
Sanctum::actingAs($admin);
|
||||
|
||||
DB::table('late_slip_logs')->insert([
|
||||
'school_year' => '2025-2026',
|
||||
'semester' => 'Fall',
|
||||
'student_name' => 'Student One',
|
||||
'slip_date' => '2025-09-01',
|
||||
'time_in' => '08:10:00',
|
||||
'grade' => '3',
|
||||
'reason' => 'Traffic',
|
||||
'admin_name' => 'Admin User',
|
||||
]);
|
||||
|
||||
$response = $this->getJson('/api/v1/attendance/late-slip-logs?school_year=2025-2026');
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonPath('data.logs.0.student_name', 'Student One');
|
||||
$response->assertJsonStructure(['data' => ['meta']]);
|
||||
}
|
||||
|
||||
public function test_show_returns_log(): void
|
||||
{
|
||||
$admin = $this->createUser('admin');
|
||||
Sanctum::actingAs($admin);
|
||||
|
||||
DB::table('late_slip_logs')->insert([
|
||||
'school_year' => '2025-2026',
|
||||
'semester' => 'Fall',
|
||||
'student_name' => 'Student Two',
|
||||
'slip_date' => '2025-09-02',
|
||||
'time_in' => '08:15:00',
|
||||
'grade' => '2',
|
||||
'reason' => 'Bus delay',
|
||||
'admin_name' => 'Admin User',
|
||||
]);
|
||||
|
||||
$response = $this->getJson('/api/v1/attendance/late-slip-logs/1');
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonPath('data.log.student_name', 'Student Two');
|
||||
}
|
||||
|
||||
public function test_destroy_deletes_log(): void
|
||||
{
|
||||
$admin = $this->createUser('admin');
|
||||
Sanctum::actingAs($admin);
|
||||
|
||||
DB::table('late_slip_logs')->insert([
|
||||
'school_year' => '2025-2026',
|
||||
'semester' => 'Fall',
|
||||
'student_name' => 'Student Three',
|
||||
'slip_date' => '2025-09-03',
|
||||
'time_in' => '08:20:00',
|
||||
'grade' => '1',
|
||||
'reason' => 'Weather',
|
||||
'admin_name' => 'Admin User',
|
||||
]);
|
||||
|
||||
$response = $this->deleteJson('/api/v1/attendance/late-slip-logs/1');
|
||||
|
||||
$response->assertOk();
|
||||
$this->assertDatabaseMissing('late_slip_logs', [
|
||||
'id' => 1,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_index_validation_rejects_bad_dates(): void
|
||||
{
|
||||
$admin = $this->createUser('admin');
|
||||
Sanctum::actingAs($admin);
|
||||
|
||||
$response = $this->getJson('/api/v1/attendance/late-slip-logs?date_from=not-a-date');
|
||||
|
||||
$response->assertStatus(422);
|
||||
$response->assertJsonStructure(['message', 'errors']);
|
||||
}
|
||||
|
||||
private function createUser(string $roleName): User
|
||||
{
|
||||
$roleId = DB::table('roles')->insertGetId([
|
||||
'name' => $roleName,
|
||||
'priority' => 1,
|
||||
'is_active' => 1,
|
||||
]);
|
||||
|
||||
$user = User::query()->create([
|
||||
'firstname' => 'Test',
|
||||
'lastname' => 'User',
|
||||
'email' => $roleName . '@example.com',
|
||||
'cellphone' => '5555555555',
|
||||
'address_street' => '123 Main',
|
||||
'city' => 'City',
|
||||
'state' => 'ST',
|
||||
'zip' => '12345',
|
||||
'accept_school_policy' => 1,
|
||||
'status' => 'Active',
|
||||
'password' => bcrypt('secret'),
|
||||
'semester' => 'Fall',
|
||||
'school_year' => '2025-2026',
|
||||
]);
|
||||
|
||||
DB::table('user_roles')->insert([
|
||||
'user_id' => $user->id,
|
||||
'role_id' => $roleId,
|
||||
]);
|
||||
|
||||
return $user;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Api\V1\Auth;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
use Tests\TestCase;
|
||||
|
||||
class IpBanControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_index_requires_admin(): void
|
||||
{
|
||||
$user = $this->createUser('teacher');
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = $this->getJson('/api/v1/ip-bans');
|
||||
|
||||
$response->assertForbidden();
|
||||
}
|
||||
|
||||
public function test_index_returns_bans(): void
|
||||
{
|
||||
$admin = $this->createUser('admin');
|
||||
Sanctum::actingAs($admin);
|
||||
|
||||
DB::table('ip_attempts')->insert([
|
||||
'ip_address' => '10.0.0.1',
|
||||
'attempts' => 5,
|
||||
'blocked_until' => now('UTC')->addHours(2),
|
||||
]);
|
||||
|
||||
$response = $this->getJson('/api/v1/ip-bans?status=active');
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonPath('data.bans.0.ip_address', '10.0.0.1');
|
||||
$response->assertJsonStructure(['data' => ['meta']]);
|
||||
}
|
||||
|
||||
public function test_show_returns_ban(): void
|
||||
{
|
||||
$admin = $this->createUser('admin');
|
||||
Sanctum::actingAs($admin);
|
||||
|
||||
DB::table('ip_attempts')->insert([
|
||||
'ip_address' => '10.0.0.2',
|
||||
'attempts' => 3,
|
||||
'blocked_until' => now('UTC')->addHours(1),
|
||||
]);
|
||||
|
||||
$response = $this->getJson('/api/v1/ip-bans/1');
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonPath('data.ban.ip_address', '10.0.0.2');
|
||||
}
|
||||
|
||||
public function test_ban_creates_block(): void
|
||||
{
|
||||
$admin = $this->createUser('admin');
|
||||
Sanctum::actingAs($admin);
|
||||
|
||||
DB::table('ip_attempts')->insert([
|
||||
'ip_address' => '10.0.0.3',
|
||||
'attempts' => 1,
|
||||
]);
|
||||
|
||||
$response = $this->postJson('/api/v1/ip-bans/ban', [
|
||||
'ip' => '10.0.0.3',
|
||||
'hours' => 12,
|
||||
]);
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonPath('data.ban.ip_address', '10.0.0.3');
|
||||
$this->assertDatabaseHas('ip_attempts', [
|
||||
'ip_address' => '10.0.0.3',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_unban_single(): void
|
||||
{
|
||||
$admin = $this->createUser('admin');
|
||||
Sanctum::actingAs($admin);
|
||||
|
||||
DB::table('ip_attempts')->insert([
|
||||
'ip_address' => '10.0.0.4',
|
||||
'attempts' => 5,
|
||||
'blocked_until' => now('UTC')->addHours(2),
|
||||
]);
|
||||
|
||||
$response = $this->postJson('/api/v1/ip-bans/unban', [
|
||||
'ip' => '10.0.0.4',
|
||||
]);
|
||||
|
||||
$response->assertOk();
|
||||
$this->assertDatabaseHas('ip_attempts', [
|
||||
'ip_address' => '10.0.0.4',
|
||||
'blocked_until' => null,
|
||||
'attempts' => 0,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_unban_all(): void
|
||||
{
|
||||
$admin = $this->createUser('admin');
|
||||
Sanctum::actingAs($admin);
|
||||
|
||||
DB::table('ip_attempts')->insert([
|
||||
[
|
||||
'ip_address' => '10.0.0.5',
|
||||
'attempts' => 5,
|
||||
'blocked_until' => now('UTC')->addHours(2),
|
||||
],
|
||||
[
|
||||
'ip_address' => '10.0.0.6',
|
||||
'attempts' => 2,
|
||||
'blocked_until' => now('UTC')->addHours(2),
|
||||
],
|
||||
]);
|
||||
|
||||
$response = $this->postJson('/api/v1/ip-bans/unban', [
|
||||
'all' => true,
|
||||
]);
|
||||
|
||||
$response->assertOk();
|
||||
$this->assertDatabaseHas('ip_attempts', [
|
||||
'ip_address' => '10.0.0.5',
|
||||
'blocked_until' => null,
|
||||
]);
|
||||
$this->assertDatabaseHas('ip_attempts', [
|
||||
'ip_address' => '10.0.0.6',
|
||||
'blocked_until' => null,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_ban_validation_requires_ip_or_id(): void
|
||||
{
|
||||
$admin = $this->createUser('admin');
|
||||
Sanctum::actingAs($admin);
|
||||
|
||||
$response = $this->postJson('/api/v1/ip-bans/ban', [
|
||||
'hours' => 12,
|
||||
]);
|
||||
|
||||
$response->assertStatus(422);
|
||||
$response->assertJsonStructure(['message', 'errors']);
|
||||
}
|
||||
|
||||
private function createUser(string $roleName): User
|
||||
{
|
||||
$roleId = DB::table('roles')->insertGetId([
|
||||
'name' => $roleName,
|
||||
'priority' => 1,
|
||||
'is_active' => 1,
|
||||
]);
|
||||
|
||||
$user = User::query()->create([
|
||||
'firstname' => 'Test',
|
||||
'lastname' => 'User',
|
||||
'email' => $roleName . '@example.com',
|
||||
'cellphone' => '5555555555',
|
||||
'address_street' => '123 Main',
|
||||
'city' => 'City',
|
||||
'state' => 'ST',
|
||||
'zip' => '12345',
|
||||
'accept_school_policy' => 1,
|
||||
'status' => 'Active',
|
||||
'password' => bcrypt('secret'),
|
||||
'semester' => 'Fall',
|
||||
'school_year' => '2025-2026',
|
||||
]);
|
||||
|
||||
DB::table('user_roles')->insert([
|
||||
'user_id' => $user->id,
|
||||
'role_id' => $roleId,
|
||||
]);
|
||||
|
||||
return $user;
|
||||
}
|
||||
}
|
||||
@@ -39,6 +39,8 @@ class RegisterControllerTest extends TestCase
|
||||
'is_active' => 1,
|
||||
]);
|
||||
|
||||
$captcha = $this->getJson('/api/v1/auth/register/captcha')->json('captcha');
|
||||
|
||||
$payload = [
|
||||
'firstname' => 'Parent',
|
||||
'lastname' => 'User',
|
||||
@@ -52,13 +54,12 @@ class RegisterControllerTest extends TestCase
|
||||
'state' => 'CT',
|
||||
'zip' => '12345',
|
||||
'accept_school_policy' => 1,
|
||||
'captcha' => 'ABCD',
|
||||
'captcha' => $captcha,
|
||||
'is_parent' => 1,
|
||||
'no_second_parent_info' => 1,
|
||||
];
|
||||
|
||||
$response = $this->withSession(['captcha_answer' => 'ABCD'])
|
||||
->postJson('/api/v1/auth/register', $payload);
|
||||
$response = $this->postJson('/api/v1/auth/register', $payload);
|
||||
|
||||
$response->assertCreated();
|
||||
$response->assertJsonPath('ok', true);
|
||||
|
||||
@@ -1,123 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Api\V1\Auth;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
use Tests\TestCase;
|
||||
|
||||
class SessionTimeoutControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_config_returns_timeout_values(): void
|
||||
{
|
||||
$user = $this->seedUser();
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = $this->getJson('/api/v1/session/timeout-config');
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonPath('status', true);
|
||||
$this->assertArrayHasKey('timeout', $response->json('data.config'));
|
||||
$this->assertArrayHasKey('keep_alive_url', $response->json('data.config'));
|
||||
}
|
||||
|
||||
public function test_check_timeout_returns_active_status(): void
|
||||
{
|
||||
$user = $this->seedUser();
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = $this
|
||||
->withSession(['last_activity' => time() - 10])
|
||||
->getJson('/api/v1/session/check-timeout');
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonPath('status', true);
|
||||
$response->assertJsonPath('data.session.status', 'active');
|
||||
}
|
||||
|
||||
public function test_check_timeout_returns_warning_status(): void
|
||||
{
|
||||
$user = $this->seedUser();
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = $this
|
||||
->withSession(['last_activity' => time() - 1000])
|
||||
->getJson('/api/v1/session/check-timeout');
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonPath('status', true);
|
||||
$response->assertJsonPath('data.session.status', 'warning');
|
||||
}
|
||||
|
||||
public function test_check_timeout_returns_expired_status(): void
|
||||
{
|
||||
$user = $this->seedUser();
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = $this
|
||||
->withSession(['last_activity' => time() - 5000])
|
||||
->getJson('/api/v1/session/check-timeout');
|
||||
|
||||
$response->assertStatus(401);
|
||||
$response->assertJsonPath('status', false);
|
||||
$response->assertJsonPath('errors.status', 'expired');
|
||||
}
|
||||
|
||||
public function test_ping_updates_activity(): void
|
||||
{
|
||||
$user = $this->seedUser();
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = $this
|
||||
->withSession(['last_activity' => time() - 10])
|
||||
->postJson('/api/v1/session/ping');
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonPath('status', true);
|
||||
$response->assertJsonPath('data.session.status', 'active');
|
||||
}
|
||||
|
||||
public function test_ping_returns_expired_when_missing_activity(): void
|
||||
{
|
||||
$user = $this->seedUser();
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = $this->postJson('/api/v1/session/ping');
|
||||
|
||||
$response->assertStatus(401);
|
||||
$response->assertJsonPath('status', false);
|
||||
}
|
||||
|
||||
public function test_requires_authentication(): void
|
||||
{
|
||||
$response = $this->getJson('/api/v1/session/timeout-config');
|
||||
|
||||
$response->assertStatus(401);
|
||||
}
|
||||
|
||||
private function seedUser(): User
|
||||
{
|
||||
$userId = DB::table('users')->insertGetId([
|
||||
'firstname' => 'Admin',
|
||||
'lastname' => 'User',
|
||||
'cellphone' => '9999999999',
|
||||
'email' => 'session@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);
|
||||
}
|
||||
}
|
||||
@@ -23,11 +23,14 @@ class ClassPreparationControllerTest extends TestCase
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJson([
|
||||
'ok' => true,
|
||||
'schoolYear' => '2025-2026',
|
||||
'semester' => 'Fall',
|
||||
'status' => true,
|
||||
'message' => 'Success',
|
||||
'data' => [
|
||||
'schoolYear' => '2025-2026',
|
||||
'semester' => 'Fall',
|
||||
],
|
||||
]);
|
||||
$this->assertNotEmpty($response->json('results'));
|
||||
$this->assertNotEmpty($response->json('data.results'));
|
||||
}
|
||||
|
||||
public function test_mark_printed_creates_snapshots(): void
|
||||
@@ -40,12 +43,12 @@ class ClassPreparationControllerTest extends TestCase
|
||||
$response = $this->postJson('/api/v1/class-prep/mark-printed', [
|
||||
'school_year' => '2025-2026',
|
||||
'semester' => 'Fall',
|
||||
'class_section_ids' => [101],
|
||||
'class_section_ids' => ['101'],
|
||||
]);
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJson([
|
||||
'ok' => true,
|
||||
'status' => true,
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('class_preparation_log', [
|
||||
@@ -54,6 +57,66 @@ class ClassPreparationControllerTest extends TestCase
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_mark_printed_rejects_invalid_payload(): void
|
||||
{
|
||||
$user = $this->createUser();
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = $this->postJson('/api/v1/class-prep/mark-printed', [
|
||||
'class_section_ids' => [],
|
||||
]);
|
||||
|
||||
$response->assertStatus(422);
|
||||
$response->assertJsonStructure(['message', 'errors']);
|
||||
}
|
||||
|
||||
public function test_save_adjustments_updates_rows(): void
|
||||
{
|
||||
$this->seedPrepData();
|
||||
|
||||
$user = $this->createUser();
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = $this->postJson('/api/v1/class-prep/adjustments', [
|
||||
'class_section_id' => '101',
|
||||
'school_year' => '2025-2026',
|
||||
'adjustments' => [
|
||||
'Small Table' => 2,
|
||||
],
|
||||
]);
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJson([
|
||||
'status' => true,
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('class_prep_adjustments', [
|
||||
'class_section_id' => '101',
|
||||
'school_year' => '2025-2026',
|
||||
'item_name' => 'Small Table',
|
||||
'adjustment' => 2,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_print_logs_prep_items(): void
|
||||
{
|
||||
$this->seedPrepData();
|
||||
|
||||
$user = $this->createUser();
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = $this->postJson('/api/v1/class-prep/print/101/2025-2026');
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonPath('status', true);
|
||||
$response->assertJsonPath('data.print.class_section_id', '101');
|
||||
|
||||
$this->assertDatabaseHas('class_preparation_log', [
|
||||
'class_section_id' => 101,
|
||||
'school_year' => '2025-2026',
|
||||
]);
|
||||
}
|
||||
|
||||
private function seedPrepData(): void
|
||||
{
|
||||
DB::table('configuration')->insert([
|
||||
|
||||
@@ -0,0 +1,282 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Api\V1\Classes;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ClassSectionControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_index_returns_sections(): void
|
||||
{
|
||||
$user = $this->createUser();
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
DB::table('classes')->insert([
|
||||
'class_name' => 'Class 1',
|
||||
'schedule' => 'Monday',
|
||||
'capacity' => 30,
|
||||
'semester' => 'Fall',
|
||||
'school_year' => '2025-2026',
|
||||
]);
|
||||
|
||||
DB::table('classSection')->insert([
|
||||
'class_id' => 1,
|
||||
'class_section_id' => 101,
|
||||
'class_section_name' => '1-A',
|
||||
'semester' => 'Fall',
|
||||
'school_year' => '2025-2026',
|
||||
]);
|
||||
|
||||
$response = $this->getJson('/api/v1/class-sections');
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonPath('data.sections.0.class_section_name', '1-A');
|
||||
}
|
||||
|
||||
public function test_store_requires_admin(): void
|
||||
{
|
||||
$user = $this->createUser();
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = $this->postJson('/api/v1/class-sections', [
|
||||
'class_id' => 1,
|
||||
'class_section_id' => 101,
|
||||
'class_section_name' => '1-A',
|
||||
'semester' => 'Fall',
|
||||
'school_year' => '2025-2026',
|
||||
]);
|
||||
|
||||
$response->assertForbidden();
|
||||
}
|
||||
|
||||
public function test_store_creates_class_section(): void
|
||||
{
|
||||
$admin = $this->createUser('admin');
|
||||
Sanctum::actingAs($admin);
|
||||
|
||||
DB::table('classes')->insert([
|
||||
'class_name' => 'Class 1',
|
||||
'schedule' => 'Monday',
|
||||
'capacity' => 30,
|
||||
'semester' => 'Fall',
|
||||
'school_year' => '2025-2026',
|
||||
]);
|
||||
|
||||
$response = $this->postJson('/api/v1/class-sections', [
|
||||
'class_id' => 1,
|
||||
'class_section_id' => 101,
|
||||
'class_section_name' => '1-A',
|
||||
'semester' => 'Fall',
|
||||
'school_year' => '2025-2026',
|
||||
]);
|
||||
|
||||
$response->assertCreated();
|
||||
$this->assertDatabaseHas('classSection', [
|
||||
'class_section_id' => 101,
|
||||
'class_section_name' => '1-A',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_update_requires_admin(): void
|
||||
{
|
||||
DB::table('classSection')->insert([
|
||||
'class_id' => 1,
|
||||
'class_section_id' => 101,
|
||||
'class_section_name' => '1-A',
|
||||
'semester' => 'Fall',
|
||||
'school_year' => '2025-2026',
|
||||
]);
|
||||
|
||||
$user = $this->createUser();
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = $this->putJson('/api/v1/class-sections/1', [
|
||||
'class_section_name' => '1-B',
|
||||
]);
|
||||
|
||||
$response->assertForbidden();
|
||||
}
|
||||
|
||||
public function test_show_returns_section(): void
|
||||
{
|
||||
DB::table('classSection')->insert([
|
||||
'class_id' => 1,
|
||||
'class_section_id' => 101,
|
||||
'class_section_name' => '1-A',
|
||||
'semester' => 'Fall',
|
||||
'school_year' => '2025-2026',
|
||||
]);
|
||||
|
||||
$user = $this->createUser();
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = $this->getJson('/api/v1/class-sections/1');
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonPath('data.section.class_section_name', '1-A');
|
||||
}
|
||||
|
||||
public function test_update_modifies_class_section(): void
|
||||
{
|
||||
DB::table('classSection')->insert([
|
||||
'class_id' => 1,
|
||||
'class_section_id' => 101,
|
||||
'class_section_name' => '1-A',
|
||||
'semester' => 'Fall',
|
||||
'school_year' => '2025-2026',
|
||||
]);
|
||||
|
||||
$admin = $this->createUser('admin');
|
||||
Sanctum::actingAs($admin);
|
||||
|
||||
$response = $this->putJson('/api/v1/class-sections/1', [
|
||||
'class_section_name' => '1-B',
|
||||
]);
|
||||
|
||||
$response->assertOk();
|
||||
$this->assertDatabaseHas('classSection', [
|
||||
'id' => 1,
|
||||
'class_section_name' => '1-B',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_destroy_deletes_class_section(): void
|
||||
{
|
||||
DB::table('classSection')->insert([
|
||||
'class_id' => 1,
|
||||
'class_section_id' => 101,
|
||||
'class_section_name' => '1-A',
|
||||
'semester' => 'Fall',
|
||||
'school_year' => '2025-2026',
|
||||
]);
|
||||
|
||||
$admin = $this->createUser('admin');
|
||||
Sanctum::actingAs($admin);
|
||||
|
||||
$response = $this->deleteJson('/api/v1/class-sections/1');
|
||||
|
||||
$response->assertOk();
|
||||
$this->assertDatabaseMissing('classSection', [
|
||||
'id' => 1,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_attendance_returns_students(): void
|
||||
{
|
||||
DB::table('configuration')->insert([
|
||||
['config_key' => 'school_year', 'config_value' => '2025-2026'],
|
||||
['config_key' => 'semester', 'config_value' => 'Fall'],
|
||||
]);
|
||||
|
||||
DB::table('classSection')->insert([
|
||||
'class_id' => 1,
|
||||
'class_section_id' => 101,
|
||||
'class_section_name' => '1-A',
|
||||
'semester' => 'Fall',
|
||||
'school_year' => '2025-2026',
|
||||
]);
|
||||
|
||||
DB::table('students')->insert([
|
||||
'school_id' => 'S-1',
|
||||
'firstname' => 'Student',
|
||||
'lastname' => 'One',
|
||||
'age' => 8,
|
||||
'gender' => 'Male',
|
||||
'photo_consent' => 1,
|
||||
'parent_id' => 1,
|
||||
'year_of_registration' => '2025',
|
||||
'school_year' => '2025-2026',
|
||||
'semester' => 'Fall',
|
||||
'is_active' => 1,
|
||||
]);
|
||||
|
||||
DB::table('student_class')->insert([
|
||||
'student_id' => 1,
|
||||
'class_section_id' => 101,
|
||||
'semester' => 'Fall',
|
||||
'school_year' => '2025-2026',
|
||||
]);
|
||||
|
||||
$teacher = $this->createUser();
|
||||
Sanctum::actingAs($teacher);
|
||||
|
||||
$response = $this->getJson('/api/v1/class-sections/101/attendance');
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonPath('data.attendance.class_section_id', 101);
|
||||
$response->assertJsonCount(1, 'data.attendance.students');
|
||||
}
|
||||
|
||||
public function test_seed_defaults_creates_classes(): void
|
||||
{
|
||||
DB::table('configuration')->insert([
|
||||
['config_key' => 'school_year', 'config_value' => '2025-2026'],
|
||||
['config_key' => 'semester', 'config_value' => 'Fall'],
|
||||
]);
|
||||
|
||||
$admin = $this->createUser('admin');
|
||||
Sanctum::actingAs($admin);
|
||||
|
||||
$response = $this->postJson('/api/v1/class-sections/seed-defaults');
|
||||
|
||||
$response->assertOk();
|
||||
$this->assertDatabaseHas('classes', [
|
||||
'class_name' => 'Class 1',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_validation_rejects_invalid_payload(): void
|
||||
{
|
||||
$admin = $this->createUser('admin');
|
||||
Sanctum::actingAs($admin);
|
||||
|
||||
$response = $this->postJson('/api/v1/class-sections', [
|
||||
'class_id' => 'bad',
|
||||
]);
|
||||
|
||||
$response->assertStatus(422);
|
||||
$response->assertJsonStructure(['message', 'errors']);
|
||||
}
|
||||
|
||||
private function createUser(string $roleName = 'teacher'): User
|
||||
{
|
||||
DB::table('roles')->insert([
|
||||
'id' => 1,
|
||||
'name' => $roleName,
|
||||
'priority' => 1,
|
||||
]);
|
||||
|
||||
DB::table('users')->insert([
|
||||
'school_id' => 1,
|
||||
'firstname' => 'Test',
|
||||
'lastname' => 'User',
|
||||
'cellphone' => '5555555555',
|
||||
'email' => $roleName . '@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',
|
||||
]);
|
||||
|
||||
DB::table('user_roles')->insert([
|
||||
'user_id' => 1,
|
||||
'role_id' => 1,
|
||||
]);
|
||||
|
||||
return User::query()->findOrFail(1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Api\V1\Frontend;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
use Tests\TestCase;
|
||||
|
||||
class FrontendControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_index_returns_page(): void
|
||||
{
|
||||
$response = $this->getJson('/api/v1/frontend');
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonPath('data.page.page', 'index');
|
||||
}
|
||||
|
||||
public function test_fetch_user_requires_auth(): void
|
||||
{
|
||||
$response = $this->getJson('/api/v1/frontend/me');
|
||||
|
||||
$response->assertStatus(401);
|
||||
}
|
||||
|
||||
public function test_fetch_user_returns_profile(): void
|
||||
{
|
||||
$user = $this->createUser('parent');
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = $this->getJson('/api/v1/frontend/me');
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonPath('data.user.firstname', 'Test');
|
||||
}
|
||||
|
||||
private function createUser(string $roleName): User
|
||||
{
|
||||
$roleId = DB::table('roles')->insertGetId([
|
||||
'name' => $roleName,
|
||||
'priority' => 1,
|
||||
'is_active' => 1,
|
||||
]);
|
||||
|
||||
$user = User::query()->create([
|
||||
'firstname' => 'Test',
|
||||
'lastname' => 'User',
|
||||
'email' => $roleName . '@example.com',
|
||||
'cellphone' => '5555555555',
|
||||
'address_street' => '123 Main',
|
||||
'city' => 'City',
|
||||
'state' => 'ST',
|
||||
'zip' => '12345',
|
||||
'accept_school_policy' => 1,
|
||||
'status' => 'Active',
|
||||
'password' => bcrypt('secret'),
|
||||
'semester' => 'Fall',
|
||||
'school_year' => '2025-2026',
|
||||
]);
|
||||
|
||||
DB::table('user_roles')->insert([
|
||||
'user_id' => $user->id,
|
||||
'role_id' => $roleId,
|
||||
]);
|
||||
|
||||
return $user;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Api\V1\Frontend;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
use Tests\TestCase;
|
||||
|
||||
class InfoIconControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_profile_icon_returns_initials(): void
|
||||
{
|
||||
$user = User::query()->create([
|
||||
'firstname' => 'John',
|
||||
'lastname' => 'Doe',
|
||||
'email' => 'john@example.com',
|
||||
'cellphone' => '5555555555',
|
||||
'address_street' => '123 Main',
|
||||
'city' => 'City',
|
||||
'state' => 'ST',
|
||||
'zip' => '12345',
|
||||
'accept_school_policy' => 1,
|
||||
'status' => 'Active',
|
||||
'password' => bcrypt('secret'),
|
||||
'semester' => 'Fall',
|
||||
'school_year' => '2025-2026',
|
||||
]);
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = $this->getJson('/api/v1/info-icon/profile');
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonPath('data.profile.user_initials', 'JD');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Api\V1\Frontend;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
use Tests\TestCase;
|
||||
|
||||
class LandingPageControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_index_returns_admin_dashboard(): void
|
||||
{
|
||||
$user = $this->createUser('admin');
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = $this->getJson('/api/v1/landing');
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonPath('data.landing.role', 'admin');
|
||||
}
|
||||
|
||||
public function test_teacher_dashboard_returns_summary(): void
|
||||
{
|
||||
$user = $this->createUser('teacher');
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = $this->getJson('/api/v1/landing/teacher');
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonPath('data.landing.dashboard', 'teacher');
|
||||
$response->assertJsonStructure(['data' => ['landing' => ['summary']]]);
|
||||
}
|
||||
|
||||
public function test_parent_dashboard_returns_summary(): void
|
||||
{
|
||||
$user = $this->createUser('parent');
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = $this->getJson('/api/v1/landing/parent');
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonPath('data.landing.dashboard', 'parent');
|
||||
}
|
||||
|
||||
private function createUser(string $roleName): User
|
||||
{
|
||||
$roleId = DB::table('roles')->insertGetId([
|
||||
'name' => $roleName,
|
||||
'priority' => 1,
|
||||
'is_active' => 1,
|
||||
]);
|
||||
|
||||
$user = User::query()->create([
|
||||
'firstname' => 'Test',
|
||||
'lastname' => 'User',
|
||||
'email' => $roleName . '@example.com',
|
||||
'cellphone' => '5555555555',
|
||||
'address_street' => '123 Main',
|
||||
'city' => 'City',
|
||||
'state' => 'ST',
|
||||
'zip' => '12345',
|
||||
'accept_school_policy' => 1,
|
||||
'status' => 'Active',
|
||||
'password' => bcrypt('secret'),
|
||||
'semester' => 'Fall',
|
||||
'school_year' => '2025-2026',
|
||||
'user_type' => 'primary',
|
||||
]);
|
||||
|
||||
DB::table('user_roles')->insert([
|
||||
'user_id' => $user->id,
|
||||
'role_id' => $roleId,
|
||||
]);
|
||||
|
||||
return $user;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Api\V1\Frontend;
|
||||
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PageControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_privacy_returns_content(): void
|
||||
{
|
||||
$dir = public_path('html');
|
||||
File::ensureDirectoryExists($dir);
|
||||
File::put($dir . '/privacy_policy.html', '<h1>Privacy</h1>');
|
||||
|
||||
$response = $this->getJson('/api/v1/pages/privacy');
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonPath('data.page.type', 'privacy_policy');
|
||||
$this->assertStringContainsString('Privacy', $response->json('data.page.content'));
|
||||
}
|
||||
|
||||
public function test_help_missing_returns_error(): void
|
||||
{
|
||||
$dir = public_path('html');
|
||||
File::ensureDirectoryExists($dir);
|
||||
File::delete($dir . '/help_center.html');
|
||||
|
||||
$response = $this->getJson('/api/v1/pages/help');
|
||||
|
||||
$response->assertStatus(404);
|
||||
}
|
||||
|
||||
public function test_submit_contact_persists(): void
|
||||
{
|
||||
DB::table('configuration')->insert([
|
||||
['config_key' => 'school_year', 'config_value' => '2025-2026'],
|
||||
['config_key' => 'semester', 'config_value' => 'Fall'],
|
||||
]);
|
||||
|
||||
$payload = [
|
||||
'email' => 'contact@example.com',
|
||||
'message' => 'Hello there, this is a test message.',
|
||||
];
|
||||
|
||||
$response = $this->postJson('/api/v1/pages/contact', $payload);
|
||||
|
||||
$response->assertStatus(201);
|
||||
$this->assertDatabaseHas('contactus', [
|
||||
'subject' => 'Contact Us Form Submission',
|
||||
'semester' => 'Fall',
|
||||
'school_year' => '2025-2026',
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,236 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Api\V1\Messaging;
|
||||
|
||||
use App\Models\Message;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
use Tests\TestCase;
|
||||
|
||||
class MessagesControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_inbox_requires_auth(): void
|
||||
{
|
||||
$response = $this->getJson('/api/v1/messages/inbox');
|
||||
|
||||
$response->assertStatus(401);
|
||||
}
|
||||
|
||||
public function test_store_creates_message(): void
|
||||
{
|
||||
DB::table('configuration')->insert([
|
||||
['config_key' => 'school_year', 'config_value' => '2025-2026'],
|
||||
['config_key' => 'semester', 'config_value' => 'Fall'],
|
||||
]);
|
||||
|
||||
$sender = $this->createUser('parent', 'sender@example.com');
|
||||
$recipient = $this->createUser('teacher', 'recipient@example.com');
|
||||
Sanctum::actingAs($sender);
|
||||
|
||||
$payload = [
|
||||
'recipient_id' => $recipient->id,
|
||||
'subject' => 'Hello',
|
||||
'message' => 'Test message',
|
||||
];
|
||||
|
||||
$response = $this->postJson('/api/v1/messages', $payload);
|
||||
|
||||
$response->assertStatus(201);
|
||||
$this->assertDatabaseHas('messages', [
|
||||
'sender_id' => $sender->id,
|
||||
'recipient_id' => $recipient->id,
|
||||
'subject' => 'Hello',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_show_marks_read(): void
|
||||
{
|
||||
$sender = $this->createUser('parent', 'sender2@example.com');
|
||||
$recipient = $this->createUser('teacher', 'recipient2@example.com');
|
||||
|
||||
$message = Message::query()->create([
|
||||
'sender_id' => $sender->id,
|
||||
'recipient_id' => $recipient->id,
|
||||
'subject' => 'Subject',
|
||||
'message' => 'Body',
|
||||
'sent_datetime' => now(),
|
||||
'read_status' => 0,
|
||||
'message_number' => 'MSG-123',
|
||||
'priority' => 'normal',
|
||||
'status' => 'sent',
|
||||
'semester' => 'Fall',
|
||||
'school_year' => '2025-2026',
|
||||
]);
|
||||
|
||||
Sanctum::actingAs($recipient);
|
||||
|
||||
$response = $this->getJson('/api/v1/messages/' . $message->id);
|
||||
|
||||
$response->assertOk();
|
||||
$this->assertDatabaseHas('messages', [
|
||||
'id' => $message->id,
|
||||
'read_status' => 1,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_update_changes_status(): void
|
||||
{
|
||||
$sender = $this->createUser('parent', 'sender3@example.com');
|
||||
$recipient = $this->createUser('teacher', 'recipient3@example.com');
|
||||
|
||||
$message = Message::query()->create([
|
||||
'sender_id' => $sender->id,
|
||||
'recipient_id' => $recipient->id,
|
||||
'subject' => 'Subject',
|
||||
'message' => 'Body',
|
||||
'sent_datetime' => now(),
|
||||
'read_status' => 0,
|
||||
'message_number' => 'MSG-124',
|
||||
'priority' => 'normal',
|
||||
'status' => 'sent',
|
||||
'semester' => 'Fall',
|
||||
'school_year' => '2025-2026',
|
||||
]);
|
||||
|
||||
Sanctum::actingAs($sender);
|
||||
|
||||
$response = $this->patchJson('/api/v1/messages/' . $message->id, [
|
||||
'status' => 'trashed',
|
||||
]);
|
||||
|
||||
$response->assertOk();
|
||||
$this->assertDatabaseHas('messages', [
|
||||
'id' => $message->id,
|
||||
'status' => 'trashed',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_destroy_marks_trashed(): void
|
||||
{
|
||||
$sender = $this->createUser('parent', 'sender4@example.com');
|
||||
$recipient = $this->createUser('teacher', 'recipient4@example.com');
|
||||
|
||||
$message = Message::query()->create([
|
||||
'sender_id' => $sender->id,
|
||||
'recipient_id' => $recipient->id,
|
||||
'subject' => 'Subject',
|
||||
'message' => 'Body',
|
||||
'sent_datetime' => now(),
|
||||
'read_status' => 0,
|
||||
'message_number' => 'MSG-125',
|
||||
'priority' => 'normal',
|
||||
'status' => 'sent',
|
||||
'semester' => 'Fall',
|
||||
'school_year' => '2025-2026',
|
||||
]);
|
||||
|
||||
Sanctum::actingAs($sender);
|
||||
|
||||
$response = $this->deleteJson('/api/v1/messages/' . $message->id);
|
||||
|
||||
$response->assertOk();
|
||||
$this->assertDatabaseHas('messages', [
|
||||
'id' => $message->id,
|
||||
'status' => 'trashed',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_recipients_teacher_returns_list(): void
|
||||
{
|
||||
$teacher = $this->createUser('teacher', 'teacher@example.com');
|
||||
Sanctum::actingAs($teacher);
|
||||
|
||||
DB::table('teacher_class')->insert([
|
||||
'teacher_id' => $teacher->id,
|
||||
'class_section_id' => 1,
|
||||
'position' => 'main',
|
||||
'school_year' => '2025-2026',
|
||||
'semester' => 'Fall',
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
|
||||
$response = $this->getJson('/api/v1/messages/recipients/teacher');
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonPath('data.recipients.0.id', $teacher->id);
|
||||
}
|
||||
|
||||
public function test_validation_rejects_missing_subject(): void
|
||||
{
|
||||
$sender = $this->createUser('parent', 'sender5@example.com');
|
||||
$recipient = $this->createUser('teacher', 'recipient5@example.com');
|
||||
Sanctum::actingAs($sender);
|
||||
|
||||
$response = $this->postJson('/api/v1/messages', [
|
||||
'recipient_id' => $recipient->id,
|
||||
'message' => 'Body',
|
||||
]);
|
||||
|
||||
$response->assertStatus(422);
|
||||
$response->assertJsonStructure(['message', 'errors']);
|
||||
}
|
||||
|
||||
public function test_show_forbidden_for_other_users(): void
|
||||
{
|
||||
$sender = $this->createUser('parent', 'sender6@example.com');
|
||||
$recipient = $this->createUser('teacher', 'recipient6@example.com');
|
||||
$other = $this->createUser('student', 'other@example.com');
|
||||
|
||||
$message = Message::query()->create([
|
||||
'sender_id' => $sender->id,
|
||||
'recipient_id' => $recipient->id,
|
||||
'subject' => 'Subject',
|
||||
'message' => 'Body',
|
||||
'sent_datetime' => now(),
|
||||
'read_status' => 0,
|
||||
'message_number' => 'MSG-126',
|
||||
'priority' => 'normal',
|
||||
'status' => 'sent',
|
||||
'semester' => 'Fall',
|
||||
'school_year' => '2025-2026',
|
||||
]);
|
||||
|
||||
Sanctum::actingAs($other);
|
||||
|
||||
$response = $this->getJson('/api/v1/messages/' . $message->id);
|
||||
|
||||
$response->assertForbidden();
|
||||
}
|
||||
|
||||
private function createUser(string $roleName, string $email): User
|
||||
{
|
||||
$roleId = DB::table('roles')->insertGetId([
|
||||
'name' => $roleName,
|
||||
'priority' => 1,
|
||||
'is_active' => 1,
|
||||
]);
|
||||
|
||||
$user = User::query()->create([
|
||||
'firstname' => 'Test',
|
||||
'lastname' => 'User',
|
||||
'email' => $email,
|
||||
'cellphone' => '5555555555',
|
||||
'address_street' => '123 Main',
|
||||
'city' => 'City',
|
||||
'state' => 'ST',
|
||||
'zip' => '12345',
|
||||
'accept_school_policy' => 1,
|
||||
'status' => 'Active',
|
||||
'password' => bcrypt('secret'),
|
||||
'semester' => 'Fall',
|
||||
'school_year' => '2025-2026',
|
||||
]);
|
||||
|
||||
DB::table('user_roles')->insert([
|
||||
'user_id' => $user->id,
|
||||
'role_id' => $roleId,
|
||||
]);
|
||||
|
||||
return $user;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Api\V1\Settings;
|
||||
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PolicyControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_school_policy_endpoint_returns_payload(): void
|
||||
{
|
||||
$response = $this->getJson('/api/v1/policies/school');
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonStructure(['data' => ['policy' => ['type', 'title', 'format', 'content', 'updated_at']]]);
|
||||
}
|
||||
|
||||
public function test_picture_policy_endpoint_returns_payload(): void
|
||||
{
|
||||
$response = $this->getJson('/api/v1/policies/picture');
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonStructure(['data' => ['policy' => ['type', 'title', 'format', 'content', 'updated_at']]]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,169 @@
|
||||
<?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 PreferencesControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_show_returns_defaults(): void
|
||||
{
|
||||
$user = $this->createUser('teacher');
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = $this->getJson('/api/v1/preferences');
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonPath('data.preferences.receive_email_notifications', true);
|
||||
$response->assertJsonStructure(['data' => ['options' => ['style_options', 'menu_options']]]);
|
||||
}
|
||||
|
||||
public function test_store_creates_preferences(): void
|
||||
{
|
||||
$user = $this->createUser('teacher');
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = $this->postJson('/api/v1/preferences', [
|
||||
'receive_email_notifications' => true,
|
||||
'receive_sms_notifications' => false,
|
||||
'theme' => 'dark',
|
||||
'language' => 'en',
|
||||
'style_color' => 'blue',
|
||||
'menu_color' => 'white',
|
||||
]);
|
||||
|
||||
$response->assertOk();
|
||||
$this->assertDatabaseHas('user_preferences', [
|
||||
'user_id' => $user->id,
|
||||
'notification_email' => 1,
|
||||
'notification_sms' => 0,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_list_requires_admin(): void
|
||||
{
|
||||
$user = $this->createUser('teacher');
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = $this->getJson('/api/v1/preferences/list');
|
||||
|
||||
$response->assertForbidden();
|
||||
}
|
||||
|
||||
public function test_list_returns_rows_for_admin(): void
|
||||
{
|
||||
$admin = $this->createUser('admin');
|
||||
Sanctum::actingAs($admin);
|
||||
|
||||
DB::table('user_preferences')->insert([
|
||||
'user_id' => $admin->id,
|
||||
'notification_email' => 1,
|
||||
'notification_sms' => 1,
|
||||
'theme' => 'light',
|
||||
'language' => 'en',
|
||||
]);
|
||||
|
||||
$response = $this->getJson('/api/v1/preferences/list');
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonPath('data.preferences.0.user_id', $admin->id);
|
||||
}
|
||||
|
||||
public function test_update_for_user_by_admin(): void
|
||||
{
|
||||
$admin = $this->createUser('admin');
|
||||
$user = $this->createUser('teacher', 2);
|
||||
Sanctum::actingAs($admin);
|
||||
|
||||
$response = $this->putJson('/api/v1/preferences/2', [
|
||||
'receive_sms_notifications' => false,
|
||||
]);
|
||||
|
||||
$response->assertOk();
|
||||
$this->assertDatabaseHas('user_preferences', [
|
||||
'user_id' => $user->id,
|
||||
'notification_sms' => 0,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_destroy_requires_admin(): void
|
||||
{
|
||||
$user = $this->createUser('teacher');
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = $this->deleteJson('/api/v1/preferences/' . $user->id);
|
||||
|
||||
$response->assertForbidden();
|
||||
}
|
||||
|
||||
public function test_destroy_deletes_preferences(): void
|
||||
{
|
||||
$admin = $this->createUser('admin');
|
||||
Sanctum::actingAs($admin);
|
||||
|
||||
DB::table('user_preferences')->insert([
|
||||
'user_id' => $admin->id,
|
||||
'notification_email' => 1,
|
||||
'notification_sms' => 1,
|
||||
]);
|
||||
|
||||
$response = $this->deleteJson('/api/v1/preferences/' . $admin->id);
|
||||
|
||||
$response->assertOk();
|
||||
$this->assertDatabaseMissing('user_preferences', [
|
||||
'user_id' => $admin->id,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_store_validates_payload(): void
|
||||
{
|
||||
$user = $this->createUser('teacher');
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = $this->postJson('/api/v1/preferences', [
|
||||
'theme' => 'invalid',
|
||||
]);
|
||||
|
||||
$response->assertStatus(422);
|
||||
$response->assertJsonStructure(['message', 'errors']);
|
||||
}
|
||||
|
||||
private function createUser(string $roleName, int $id = 1): User
|
||||
{
|
||||
$roleId = DB::table('roles')->insertGetId([
|
||||
'name' => $roleName,
|
||||
'priority' => 1,
|
||||
'is_active' => 1,
|
||||
]);
|
||||
|
||||
DB::table('users')->insert([
|
||||
'id' => $id,
|
||||
'firstname' => 'Test',
|
||||
'lastname' => 'User',
|
||||
'email' => $roleName . $id . '@example.com',
|
||||
'cellphone' => '5555555555',
|
||||
'address_street' => '123 Main',
|
||||
'city' => 'City',
|
||||
'state' => 'ST',
|
||||
'zip' => '12345',
|
||||
'accept_school_policy' => 1,
|
||||
'status' => 'Active',
|
||||
'password' => bcrypt('secret'),
|
||||
'semester' => 'Fall',
|
||||
'school_year' => '2025-2026',
|
||||
]);
|
||||
|
||||
DB::table('user_roles')->insert([
|
||||
'user_id' => $id,
|
||||
'role_id' => $roleId,
|
||||
]);
|
||||
|
||||
return User::query()->findOrFail($id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Api\V1\Settings;
|
||||
|
||||
use App\Models\Setting;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
use Tests\TestCase;
|
||||
|
||||
class SettingsControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_index_requires_admin(): void
|
||||
{
|
||||
$user = $this->createUser('teacher');
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = $this->getJson('/api/v1/settings');
|
||||
|
||||
$response->assertForbidden();
|
||||
}
|
||||
|
||||
public function test_index_returns_settings(): void
|
||||
{
|
||||
$admin = $this->createUser('admin');
|
||||
Sanctum::actingAs($admin);
|
||||
|
||||
$response = $this->getJson('/api/v1/settings');
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonStructure(['data' => ['settings' => ['site_name', 'administrator_email', 'timezone']]]);
|
||||
}
|
||||
|
||||
public function test_update_validation_rejects_bad_email(): void
|
||||
{
|
||||
$admin = $this->createUser('admin');
|
||||
Sanctum::actingAs($admin);
|
||||
|
||||
$response = $this->patchJson('/api/v1/settings', [
|
||||
'administrator_email' => 'not-an-email',
|
||||
]);
|
||||
|
||||
$response->assertStatus(422);
|
||||
$response->assertJsonStructure(['message', 'errors']);
|
||||
}
|
||||
|
||||
public function test_update_persists_settings(): void
|
||||
{
|
||||
$admin = $this->createUser('admin');
|
||||
Sanctum::actingAs($admin);
|
||||
|
||||
$response = $this->patchJson('/api/v1/settings', [
|
||||
'site_name' => 'Alrahma',
|
||||
'timezone' => 'UTC',
|
||||
'administrator_email' => 'admin@example.com',
|
||||
]);
|
||||
|
||||
$response->assertOk();
|
||||
$this->assertDatabaseHas('settings', [
|
||||
'name' => 'Alrahma',
|
||||
'timezone' => 'UTC',
|
||||
]);
|
||||
$this->assertDatabaseHas('configuration', [
|
||||
'config_key' => 'administrator_email',
|
||||
'config_value' => 'admin@example.com',
|
||||
]);
|
||||
}
|
||||
|
||||
private function createUser(string $roleName): User
|
||||
{
|
||||
$roleId = DB::table('roles')->insertGetId([
|
||||
'name' => $roleName,
|
||||
'priority' => 1,
|
||||
'is_active' => 1,
|
||||
]);
|
||||
|
||||
$user = User::query()->create([
|
||||
'firstname' => 'Test',
|
||||
'lastname' => 'User',
|
||||
'email' => $roleName . '@example.com',
|
||||
'cellphone' => '5555555555',
|
||||
'address_street' => '123 Main',
|
||||
'city' => 'City',
|
||||
'state' => 'ST',
|
||||
'zip' => '12345',
|
||||
'accept_school_policy' => 1,
|
||||
'status' => 'Active',
|
||||
'password' => bcrypt('secret'),
|
||||
'semester' => 'Fall',
|
||||
'school_year' => '2025-2026',
|
||||
]);
|
||||
|
||||
DB::table('user_roles')->insert([
|
||||
'user_id' => $user->id,
|
||||
'role_id' => $roleId,
|
||||
]);
|
||||
|
||||
return $user;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,169 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Api\V1\Staff;
|
||||
|
||||
use App\Models\Staff;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
use Tests\TestCase;
|
||||
|
||||
class StaffControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_index_requires_admin(): void
|
||||
{
|
||||
$user = $this->createUser('teacher');
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = $this->getJson('/api/v1/staff');
|
||||
|
||||
$response->assertForbidden();
|
||||
}
|
||||
|
||||
public function test_index_returns_staff(): void
|
||||
{
|
||||
$admin = $this->createUser('admin');
|
||||
Sanctum::actingAs($admin);
|
||||
|
||||
$staffUser = $this->createUser('teacher', 'staff@example.com');
|
||||
Staff::query()->create([
|
||||
'user_id' => $staffUser->id,
|
||||
'firstname' => 'Staff',
|
||||
'lastname' => 'User',
|
||||
'email' => 'staff@example.com',
|
||||
'phone' => '5555555555',
|
||||
'role_name' => 'teacher',
|
||||
'active_role' => 'teacher',
|
||||
'school_year' => '2025-2026',
|
||||
]);
|
||||
|
||||
$response = $this->getJson('/api/v1/staff');
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonPath('data.staff.0.email', 'staff@example.com');
|
||||
}
|
||||
|
||||
public function test_store_creates_staff(): void
|
||||
{
|
||||
$admin = $this->createUser('admin');
|
||||
Sanctum::actingAs($admin);
|
||||
|
||||
$user = $this->createUser('teacher', 'newstaff@example.com');
|
||||
|
||||
$response = $this->postJson('/api/v1/staff', [
|
||||
'user_id' => $user->id,
|
||||
'firstname' => 'New',
|
||||
'lastname' => 'Staff',
|
||||
'email' => 'newstaff@example.com',
|
||||
'phone' => '5555555555',
|
||||
'role_name' => 'teacher',
|
||||
'school_year' => '2025-2026',
|
||||
]);
|
||||
|
||||
$response->assertStatus(201);
|
||||
$this->assertDatabaseHas('staff', [
|
||||
'user_id' => $user->id,
|
||||
'email' => 'newstaff@example.com',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_store_validation(): void
|
||||
{
|
||||
$admin = $this->createUser('admin');
|
||||
Sanctum::actingAs($admin);
|
||||
|
||||
$response = $this->postJson('/api/v1/staff', [
|
||||
'firstname' => 'New',
|
||||
]);
|
||||
|
||||
$response->assertStatus(422);
|
||||
}
|
||||
|
||||
public function test_update_modifies_staff(): void
|
||||
{
|
||||
$admin = $this->createUser('admin');
|
||||
Sanctum::actingAs($admin);
|
||||
|
||||
$user = $this->createUser('teacher', 'updatestaff@example.com');
|
||||
$staff = Staff::query()->create([
|
||||
'user_id' => $user->id,
|
||||
'firstname' => 'Old',
|
||||
'lastname' => 'Name',
|
||||
'email' => 'updatestaff@example.com',
|
||||
'phone' => '5555555555',
|
||||
'role_name' => 'teacher',
|
||||
'active_role' => 'teacher',
|
||||
'school_year' => '2025-2026',
|
||||
]);
|
||||
|
||||
$response = $this->patchJson('/api/v1/staff/' . $staff->id, [
|
||||
'firstname' => 'Updated',
|
||||
]);
|
||||
|
||||
$response->assertOk();
|
||||
$this->assertDatabaseHas('staff', [
|
||||
'id' => $staff->id,
|
||||
'firstname' => 'Updated',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_destroy_deletes_staff(): void
|
||||
{
|
||||
$admin = $this->createUser('admin');
|
||||
Sanctum::actingAs($admin);
|
||||
|
||||
$user = $this->createUser('teacher', 'deletestaff@example.com');
|
||||
$staff = Staff::query()->create([
|
||||
'user_id' => $user->id,
|
||||
'firstname' => 'Delete',
|
||||
'lastname' => 'Staff',
|
||||
'email' => 'deletestaff@example.com',
|
||||
'phone' => '5555555555',
|
||||
'role_name' => 'teacher',
|
||||
'active_role' => 'teacher',
|
||||
'school_year' => '2025-2026',
|
||||
]);
|
||||
|
||||
$response = $this->deleteJson('/api/v1/staff/' . $staff->id);
|
||||
|
||||
$response->assertOk();
|
||||
$this->assertDatabaseMissing('staff', [
|
||||
'id' => $staff->id,
|
||||
]);
|
||||
}
|
||||
|
||||
private function createUser(string $roleName, ?string $email = null): User
|
||||
{
|
||||
$roleId = DB::table('roles')->insertGetId([
|
||||
'name' => $roleName,
|
||||
'priority' => 1,
|
||||
'is_active' => 1,
|
||||
]);
|
||||
|
||||
$user = User::query()->create([
|
||||
'firstname' => 'Test',
|
||||
'lastname' => 'User',
|
||||
'email' => $email ?? ($roleName . '@example.com'),
|
||||
'cellphone' => '5555555555',
|
||||
'address_street' => '123 Main',
|
||||
'city' => 'City',
|
||||
'state' => 'ST',
|
||||
'zip' => '12345',
|
||||
'accept_school_policy' => 1,
|
||||
'status' => 'Active',
|
||||
'password' => bcrypt('secret'),
|
||||
'semester' => 'Fall',
|
||||
'school_year' => '2025-2026',
|
||||
]);
|
||||
|
||||
DB::table('user_roles')->insert([
|
||||
'user_id' => $user->id,
|
||||
'role_id' => $roleId,
|
||||
]);
|
||||
|
||||
return $user;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Api\V1\Support;
|
||||
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ContactControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_send_requires_valid_payload(): void
|
||||
{
|
||||
$response = $this->postJson('/api/v1/contact', [
|
||||
'email' => 'not-an-email',
|
||||
]);
|
||||
|
||||
$response->assertStatus(422);
|
||||
}
|
||||
|
||||
public function test_send_returns_success(): void
|
||||
{
|
||||
$response = $this->postJson('/api/v1/contact', [
|
||||
'name' => 'Test User',
|
||||
'email' => 'test@example.com',
|
||||
'subject' => 'Hello',
|
||||
'message' => 'This is a test message.',
|
||||
]);
|
||||
|
||||
$response->assertStatus(201);
|
||||
$response->assertJsonStructure(['data' => ['contact' => ['email_sent', 'recipient']]]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Api\V1\Support;
|
||||
|
||||
use App\Models\SupportRequest;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
use Tests\TestCase;
|
||||
|
||||
class SupportControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_index_requires_auth(): void
|
||||
{
|
||||
$response = $this->getJson('/api/v1/support');
|
||||
|
||||
$response->assertStatus(401);
|
||||
}
|
||||
|
||||
public function test_index_returns_requests(): void
|
||||
{
|
||||
$user = $this->createUser('parent');
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
SupportRequest::query()->create([
|
||||
'user_id' => $user->id,
|
||||
'subject' => 'Help',
|
||||
'message' => 'Need assistance',
|
||||
'status' => 'open',
|
||||
'semester' => 'Fall',
|
||||
'school_year' => '2025-2026',
|
||||
]);
|
||||
|
||||
$response = $this->getJson('/api/v1/support');
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonPath('data.requests.0.subject', 'Help');
|
||||
}
|
||||
|
||||
public function test_store_validation(): void
|
||||
{
|
||||
$user = $this->createUser('parent');
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = $this->postJson('/api/v1/support', [
|
||||
'subject' => 'Hi',
|
||||
]);
|
||||
|
||||
$response->assertStatus(422);
|
||||
}
|
||||
|
||||
public function test_store_creates_request(): void
|
||||
{
|
||||
DB::table('configuration')->insert([
|
||||
['config_key' => 'school_year', 'config_value' => '2025-2026'],
|
||||
['config_key' => 'semester', 'config_value' => 'Fall'],
|
||||
]);
|
||||
|
||||
$user = $this->createUser('parent');
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = $this->postJson('/api/v1/support', [
|
||||
'subject' => 'Help',
|
||||
'message' => 'Need assistance please',
|
||||
]);
|
||||
|
||||
$response->assertStatus(201);
|
||||
$this->assertDatabaseHas('support_requests', [
|
||||
'user_id' => $user->id,
|
||||
'subject' => 'Help',
|
||||
]);
|
||||
}
|
||||
|
||||
private function createUser(string $roleName): User
|
||||
{
|
||||
$roleId = DB::table('roles')->insertGetId([
|
||||
'name' => $roleName,
|
||||
'priority' => 1,
|
||||
'is_active' => 1,
|
||||
]);
|
||||
|
||||
$user = User::query()->create([
|
||||
'firstname' => 'Test',
|
||||
'lastname' => 'User',
|
||||
'email' => $roleName . '@example.com',
|
||||
'cellphone' => '5555555555',
|
||||
'address_street' => '123 Main',
|
||||
'city' => 'City',
|
||||
'state' => 'ST',
|
||||
'zip' => '12345',
|
||||
'accept_school_policy' => 1,
|
||||
'status' => 'Active',
|
||||
'password' => bcrypt('secret'),
|
||||
'semester' => 'Fall',
|
||||
'school_year' => '2025-2026',
|
||||
]);
|
||||
|
||||
DB::table('user_roles')->insert([
|
||||
'user_id' => $user->id,
|
||||
'role_id' => $roleId,
|
||||
]);
|
||||
|
||||
return $user;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Api\V1\System;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
use Tests\TestCase;
|
||||
|
||||
class DashboardControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_route_requires_authentication(): void
|
||||
{
|
||||
$response = $this->getJson('/api/v1/dashboard/route');
|
||||
|
||||
$response->assertStatus(401);
|
||||
}
|
||||
|
||||
public function test_route_returns_dashboard_for_role(): void
|
||||
{
|
||||
$roleId = DB::table('roles')->insertGetId([
|
||||
'name' => 'administrator',
|
||||
'dashboard_route' => '/administrator/dashboard',
|
||||
'priority' => 1,
|
||||
'is_active' => 1,
|
||||
]);
|
||||
|
||||
$user = User::query()->create([
|
||||
'firstname' => 'Admin',
|
||||
'lastname' => 'User',
|
||||
'email' => 'admin@example.com',
|
||||
'cellphone' => '5555555555',
|
||||
'address_street' => '123 Main',
|
||||
'city' => 'City',
|
||||
'state' => 'ST',
|
||||
'zip' => '12345',
|
||||
'accept_school_policy' => 1,
|
||||
'status' => 'Active',
|
||||
'password' => bcrypt('secret'),
|
||||
'semester' => 'Fall',
|
||||
'school_year' => '2025-2026',
|
||||
]);
|
||||
|
||||
DB::table('user_roles')->insert([
|
||||
'user_id' => $user->id,
|
||||
'role_id' => $roleId,
|
||||
]);
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = $this->getJson('/api/v1/dashboard/route');
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonPath('data.dashboard.route', '/administrator/dashboard');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Api\V1\System;
|
||||
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class DatabaseHealthControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_db_check_returns_ok(): void
|
||||
{
|
||||
$response = $this->getJson('/api/v1/system/db-check');
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonPath('data.ok', true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Api\V1\System;
|
||||
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class HealthControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_health_returns_payload(): void
|
||||
{
|
||||
$response = $this->getJson('/api/v1/health');
|
||||
|
||||
$this->assertContains($response->status(), [200, 503]);
|
||||
$response->assertJsonStructure([
|
||||
'status',
|
||||
'message',
|
||||
'data' => [
|
||||
'health' => [
|
||||
'ok',
|
||||
'paths',
|
||||
'database',
|
||||
'write_path',
|
||||
'timestamp',
|
||||
],
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Api\V1\System;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Tests\TestCase;
|
||||
|
||||
class NavBuilderControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
private function makeAdminUser(): User
|
||||
{
|
||||
$roleId = DB::table('roles')->insertGetId([
|
||||
'name' => 'admin',
|
||||
'priority' => 1,
|
||||
]);
|
||||
|
||||
$user = User::query()->create([
|
||||
'firstname' => 'Admin',
|
||||
'lastname' => 'User',
|
||||
'email' => 'admin@example.com',
|
||||
'cellphone' => '5555555555',
|
||||
'address_street' => '123 Main',
|
||||
'city' => 'City',
|
||||
'state' => 'CT',
|
||||
'zip' => '12345',
|
||||
'accept_school_policy' => 1,
|
||||
'status' => 'Active',
|
||||
'password' => 'hashed',
|
||||
'semester' => 'Fall',
|
||||
]);
|
||||
|
||||
DB::table('user_roles')->insert([
|
||||
'user_id' => $user->id,
|
||||
'role_id' => $roleId,
|
||||
]);
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
public function test_menu_returns_items_for_roles(): void
|
||||
{
|
||||
$roleId = DB::table('roles')->insertGetId([
|
||||
'name' => 'parent',
|
||||
'priority' => 1,
|
||||
]);
|
||||
|
||||
$user = User::query()->create([
|
||||
'firstname' => 'Parent',
|
||||
'lastname' => 'User',
|
||||
'email' => 'parent@example.com',
|
||||
'cellphone' => '5555555555',
|
||||
'address_street' => '123 Main',
|
||||
'city' => 'City',
|
||||
'state' => 'CT',
|
||||
'zip' => '12345',
|
||||
'accept_school_policy' => 1,
|
||||
'status' => 'Active',
|
||||
'password' => 'hashed',
|
||||
'semester' => 'Fall',
|
||||
]);
|
||||
|
||||
DB::table('user_roles')->insert([
|
||||
'user_id' => $user->id,
|
||||
'role_id' => $roleId,
|
||||
]);
|
||||
|
||||
$navId = DB::table('nav_items')->insertGetId([
|
||||
'label' => 'Home',
|
||||
'url' => 'home',
|
||||
'sort_order' => 1,
|
||||
'is_enabled' => 1,
|
||||
]);
|
||||
|
||||
DB::table('role_nav_items')->insert([
|
||||
'role_id' => $roleId,
|
||||
'nav_item_id' => $navId,
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($user, 'sanctum')->getJson('/api/v1/nav-builder/menu');
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonPath('data.items.0.label', 'Home');
|
||||
}
|
||||
|
||||
public function test_data_requires_admin(): void
|
||||
{
|
||||
$user = $this->makeAdminUser();
|
||||
|
||||
$response = $this->actingAs($user, 'sanctum')->getJson('/api/v1/nav-builder/data');
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonStructure(['data' => ['data' => ['items', 'roles', 'parentOptions']]]);
|
||||
}
|
||||
|
||||
public function test_store_creates_nav_item(): void
|
||||
{
|
||||
$user = $this->makeAdminUser();
|
||||
|
||||
$response = $this->actingAs($user, 'sanctum')->postJson('/api/v1/nav-builder', [
|
||||
'label' => 'Dashboard',
|
||||
'url' => 'dashboard',
|
||||
'sort_order' => 1,
|
||||
'is_enabled' => true,
|
||||
'roles' => [],
|
||||
]);
|
||||
|
||||
$response->assertCreated();
|
||||
$this->assertDatabaseHas('nav_items', ['label' => 'Dashboard']);
|
||||
}
|
||||
|
||||
public function test_delete_removes_nav_item(): void
|
||||
{
|
||||
$user = $this->makeAdminUser();
|
||||
|
||||
$navId = DB::table('nav_items')->insertGetId([
|
||||
'label' => 'Delete',
|
||||
'url' => 'delete',
|
||||
'sort_order' => 1,
|
||||
'is_enabled' => 1,
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($user, 'sanctum')->deleteJson("/api/v1/nav-builder/{$navId}");
|
||||
|
||||
$response->assertOk();
|
||||
$this->assertDatabaseMissing('nav_items', ['id' => $navId]);
|
||||
}
|
||||
|
||||
public function test_reorder_updates_items(): void
|
||||
{
|
||||
$user = $this->makeAdminUser();
|
||||
|
||||
$navId = DB::table('nav_items')->insertGetId([
|
||||
'label' => 'Sort',
|
||||
'url' => 'sort',
|
||||
'sort_order' => 1,
|
||||
'is_enabled' => 1,
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($user, 'sanctum')->postJson('/api/v1/nav-builder/reorder', [
|
||||
'orders' => [$navId => 9],
|
||||
]);
|
||||
|
||||
$response->assertOk();
|
||||
$this->assertDatabaseHas('nav_items', ['id' => $navId, 'sort_order' => 9]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Api\V1\Ui;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
use Tests\TestCase;
|
||||
|
||||
class UiControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_style_requires_auth(): void
|
||||
{
|
||||
$response = $this->postJson('/api/v1/ui/style', [
|
||||
'accent' => 'blue',
|
||||
]);
|
||||
|
||||
$response->assertStatus(401);
|
||||
}
|
||||
|
||||
public function test_style_updates_preferences(): void
|
||||
{
|
||||
$user = $this->createUser('parent');
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = $this->postJson('/api/v1/ui/style', [
|
||||
'accent' => 'blue',
|
||||
'menu' => 'custom',
|
||||
'menu_bg' => '#112233',
|
||||
'menu_text' => '#ffffff',
|
||||
'menu_mode' => 'dark',
|
||||
]);
|
||||
|
||||
$response->assertOk();
|
||||
$this->assertDatabaseHas('user_preferences', [
|
||||
'user_id' => $user->id,
|
||||
'style_color' => 'blue',
|
||||
'menu_color' => 'custom',
|
||||
]);
|
||||
}
|
||||
|
||||
private function createUser(string $roleName): User
|
||||
{
|
||||
$roleId = DB::table('roles')->insertGetId([
|
||||
'name' => $roleName,
|
||||
'priority' => 1,
|
||||
'is_active' => 1,
|
||||
]);
|
||||
|
||||
$user = User::query()->create([
|
||||
'firstname' => 'Test',
|
||||
'lastname' => 'User',
|
||||
'email' => $roleName . '@example.com',
|
||||
'cellphone' => '5555555555',
|
||||
'address_street' => '123 Main',
|
||||
'city' => 'City',
|
||||
'state' => 'ST',
|
||||
'zip' => '12345',
|
||||
'accept_school_policy' => 1,
|
||||
'status' => 'Active',
|
||||
'password' => bcrypt('secret'),
|
||||
'semester' => 'Fall',
|
||||
'school_year' => '2025-2026',
|
||||
]);
|
||||
|
||||
DB::table('user_roles')->insert([
|
||||
'user_id' => $user->id,
|
||||
'role_id' => $roleId,
|
||||
]);
|
||||
|
||||
return $user;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Api\V1\Utilities;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PhoneFormatterControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_format_returns_formatted_phone(): void
|
||||
{
|
||||
$user = $this->createUser();
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = $this->postJson('/api/v1/utilities/phone/format', [
|
||||
'number' => '(555) 123-4567',
|
||||
]);
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonPath('data.phone.formatted', '555-123-4567');
|
||||
$response->assertJsonPath('data.phone.is_valid', true);
|
||||
}
|
||||
|
||||
public function test_format_rejects_invalid_number(): void
|
||||
{
|
||||
$user = $this->createUser();
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = $this->postJson('/api/v1/utilities/phone/format', [
|
||||
'number' => '123',
|
||||
]);
|
||||
|
||||
$response->assertStatus(422);
|
||||
}
|
||||
|
||||
public function test_format_validates_payload(): void
|
||||
{
|
||||
$user = $this->createUser();
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = $this->postJson('/api/v1/utilities/phone/format', []);
|
||||
|
||||
$response->assertStatus(422);
|
||||
$response->assertJsonStructure(['message', 'errors']);
|
||||
}
|
||||
|
||||
private function createUser(): User
|
||||
{
|
||||
$roleId = DB::table('roles')->insertGetId([
|
||||
'name' => 'admin',
|
||||
'priority' => 1,
|
||||
'is_active' => 1,
|
||||
]);
|
||||
|
||||
$user = User::query()->create([
|
||||
'firstname' => 'Test',
|
||||
'lastname' => 'User',
|
||||
'email' => 'admin@example.com',
|
||||
'cellphone' => '5555555555',
|
||||
'address_street' => '123 Main',
|
||||
'city' => 'City',
|
||||
'state' => 'ST',
|
||||
'zip' => '12345',
|
||||
'accept_school_policy' => 1,
|
||||
'status' => 'Active',
|
||||
'password' => bcrypt('secret'),
|
||||
'semester' => 'Fall',
|
||||
'school_year' => '2025-2026',
|
||||
]);
|
||||
|
||||
DB::table('user_roles')->insert([
|
||||
'user_id' => $user->id,
|
||||
'role_id' => $roleId,
|
||||
]);
|
||||
|
||||
return $user;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user