169 lines
4.7 KiB
PHP
169 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Api\Admin;
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Tests\TestCase;
|
|
|
|
class AdminNotificationApiTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
protected function createAdmin(): User
|
|
{
|
|
return User::factory()->create([
|
|
'role' => 'administrator',
|
|
'firstname' => 'Super',
|
|
'lastname' => 'Admin',
|
|
'email' => 'superadmin@example.com',
|
|
]);
|
|
}
|
|
|
|
protected function seedRoleDataForAdmins(): array
|
|
{
|
|
// roles + user_roles used by AdminNotificationService::fetchAdminNotificationUsers()
|
|
DB::table('roles')->insert([
|
|
['id' => 1, 'name' => 'administrator'],
|
|
['id' => 2, 'name' => 'teacher'],
|
|
]);
|
|
|
|
$adminA = User::factory()->create([
|
|
'firstname' => 'Nour',
|
|
'lastname' => 'Ali',
|
|
'email' => 'nour@example.com',
|
|
'role' => 'administrator',
|
|
]);
|
|
|
|
$adminB = User::factory()->create([
|
|
'firstname' => 'Sara',
|
|
'lastname' => 'Omar',
|
|
'email' => 'sara@example.com',
|
|
'role' => 'administrator',
|
|
]);
|
|
|
|
$teacher = User::factory()->create([
|
|
'firstname' => 'Teacher',
|
|
'lastname' => 'User',
|
|
'email' => 'teacher@example.com',
|
|
'role' => 'teacher',
|
|
]);
|
|
|
|
DB::table('user_roles')->insert([
|
|
['user_id' => $adminA->id, 'role_id' => 1, 'deleted_at' => null],
|
|
['user_id' => $adminB->id, 'role_id' => 1, 'deleted_at' => null],
|
|
['user_id' => $teacher->id, 'role_id' => 2, 'deleted_at' => null],
|
|
]);
|
|
|
|
return [$adminA, $adminB, $teacher];
|
|
}
|
|
|
|
/** @test */
|
|
public function admin_can_get_notification_alerts_payload(): void
|
|
{
|
|
$actingAdmin = $this->createAdmin();
|
|
$this->seedRoleDataForAdmins();
|
|
|
|
$response = $this->actingAs($actingAdmin, 'sanctum')
|
|
->getJson('/api/admin/notifications/alerts');
|
|
|
|
$response->assertOk()
|
|
->assertJsonStructure([
|
|
'admins',
|
|
'subjects',
|
|
'assigned_subjects',
|
|
'table_ready',
|
|
]);
|
|
}
|
|
|
|
/** @test */
|
|
public function admin_can_save_notification_subjects(): void
|
|
{
|
|
$actingAdmin = $this->createAdmin();
|
|
[$adminA, $adminB] = $this->seedRoleDataForAdmins();
|
|
|
|
$payload = [
|
|
'subjects' => [
|
|
$adminA->id => [
|
|
'finance' => true,
|
|
'attendance' => true,
|
|
],
|
|
$adminB->id => [
|
|
'general',
|
|
'events',
|
|
],
|
|
],
|
|
];
|
|
|
|
$response = $this->actingAs($actingAdmin, 'sanctum')
|
|
->postJson('/api/admin/notifications/subjects', $payload);
|
|
|
|
// If table is missing in your migrations, service returns 422 (ok=false)
|
|
if ($response->status() === 422) {
|
|
$response->assertJson([
|
|
'ok' => false,
|
|
]);
|
|
return;
|
|
}
|
|
|
|
$response->assertOk()
|
|
->assertJson([
|
|
'ok' => true,
|
|
]);
|
|
|
|
$this->assertDatabaseHas('admin_notification_subjects', [
|
|
'admin_id' => $adminA->id,
|
|
'subject' => 'finance',
|
|
]);
|
|
}
|
|
|
|
/** @test */
|
|
public function admin_can_get_print_recipients_payload(): void
|
|
{
|
|
$actingAdmin = $this->createAdmin();
|
|
$this->seedRoleDataForAdmins();
|
|
|
|
$response = $this->actingAs($actingAdmin, 'sanctum')
|
|
->getJson('/api/admin/notifications/print-recipients');
|
|
|
|
$response->assertOk()
|
|
->assertJsonStructure([
|
|
'admins',
|
|
'assigned',
|
|
'table_ready',
|
|
]);
|
|
}
|
|
|
|
/** @test */
|
|
public function admin_can_save_print_recipients(): void
|
|
{
|
|
$actingAdmin = $this->createAdmin();
|
|
[$adminA, $adminB] = $this->seedRoleDataForAdmins();
|
|
|
|
$payload = [
|
|
'notify' => [
|
|
(string)$adminA->id => true,
|
|
(string)$adminB->id => true,
|
|
],
|
|
];
|
|
|
|
$response = $this->actingAs($actingAdmin, 'sanctum')
|
|
->postJson('/api/admin/notifications/print-recipients', $payload);
|
|
|
|
if ($response->status() === 422) {
|
|
$response->assertJson(['ok' => false]);
|
|
return;
|
|
}
|
|
|
|
$response->assertOk()
|
|
->assertJson([
|
|
'ok' => true,
|
|
]);
|
|
|
|
$this->assertDatabaseHas('admin_notification_subjects', [
|
|
'admin_id' => $adminA->id,
|
|
'subject' => 'print_requests',
|
|
]);
|
|
}
|
|
} |