031e499819
API CI/CD / Validate (composer + pint) (push) Successful in 3m10s
API CI/CD / Test (PHPUnit) (push) Failing after 6m49s
API CI/CD / Build frontend assets (push) Successful in 1m3s
API CI/CD / Security audit (push) Failing after 1m0s
API CI/CD / Deploy to shared hosting (PHP) (push) Has been skipped
184 lines
5.2 KiB
PHP
184 lines
5.2 KiB
PHP
<?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.'?school_year=2025-2026', [
|
|
'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.'?school_year=2025-2026');
|
|
|
|
$response->assertOk();
|
|
$this->assertDatabaseMissing('staff', [
|
|
'id' => $staff->id,
|
|
]);
|
|
}
|
|
|
|
private function createUser(string $roleName, ?string $email = null): User
|
|
{
|
|
DB::table('school_years')->updateOrInsert(
|
|
['name' => '2025-2026'],
|
|
[
|
|
'start_date' => '2025-09-01',
|
|
'end_date' => '2026-06-30',
|
|
'status' => 'active',
|
|
'is_current' => 1,
|
|
'updated_at' => now(),
|
|
'created_at' => now(),
|
|
],
|
|
);
|
|
|
|
$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',
|
|
'is_verified' => 1,
|
|
'is_suspended' => 0,
|
|
'password' => bcrypt('secret'),
|
|
'semester' => 'Fall',
|
|
'school_year' => '2025-2026',
|
|
]);
|
|
|
|
DB::table('user_roles')->insert([
|
|
'user_id' => $user->id,
|
|
'role_id' => $roleId,
|
|
]);
|
|
|
|
return $user;
|
|
}
|
|
}
|