add notifications logic and add support of both JWT and Sanctum
This commit is contained in:
@@ -0,0 +1,309 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Api\V1\Notifications;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Services\Notifications\NotificationSendService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
use Tests\TestCase;
|
||||
|
||||
class NotificationControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_index_requires_authentication(): void
|
||||
{
|
||||
$response = $this->getJson('/api/v1/notifications');
|
||||
|
||||
$response->assertStatus(401);
|
||||
}
|
||||
|
||||
public function test_index_returns_notifications(): void
|
||||
{
|
||||
$user = $this->createUser();
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
DB::table('notifications')->insert([
|
||||
'id' => 1,
|
||||
'title' => 'Hello',
|
||||
'message' => 'World',
|
||||
'target_group' => 'parent',
|
||||
'delivery_channels' => json_encode(['in_app']),
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
|
||||
DB::table('user_notifications')->insert([
|
||||
'notification_id' => 1,
|
||||
'user_id' => 1,
|
||||
'is_read' => 0,
|
||||
]);
|
||||
|
||||
$response = $this->getJson('/api/v1/notifications');
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonPath('ok', true);
|
||||
$response->assertJsonStructure(['ok', 'notifications', 'pagination']);
|
||||
}
|
||||
|
||||
public function test_show_returns_notification(): void
|
||||
{
|
||||
$user = $this->createUser();
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
DB::table('notifications')->insert([
|
||||
'id' => 1,
|
||||
'title' => 'Hello',
|
||||
'message' => 'World',
|
||||
'target_group' => 'parent',
|
||||
'delivery_channels' => json_encode(['in_app']),
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
|
||||
DB::table('user_notifications')->insert([
|
||||
'notification_id' => 1,
|
||||
'user_id' => 1,
|
||||
'is_read' => 0,
|
||||
]);
|
||||
|
||||
$response = $this->getJson('/api/v1/notifications/1');
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonPath('ok', true);
|
||||
$response->assertJsonStructure(['ok', 'notification']);
|
||||
}
|
||||
|
||||
public function test_store_creates_notification(): void
|
||||
{
|
||||
$this->seedRole('parent');
|
||||
|
||||
DB::table('users')->insert([
|
||||
'id' => 2,
|
||||
'firstname' => 'Parent',
|
||||
'lastname' => 'User',
|
||||
'email' => 'parent@example.com',
|
||||
'status' => 'Active',
|
||||
]);
|
||||
|
||||
DB::table('user_roles')->insert([
|
||||
'user_id' => 2,
|
||||
'role_id' => 1,
|
||||
]);
|
||||
|
||||
$user = $this->createUser();
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = $this->postJson('/api/v1/notifications', [
|
||||
'title' => 'Announcement',
|
||||
'message' => 'Hello parents',
|
||||
'target_group' => 'parent',
|
||||
'channels' => ['in_app'],
|
||||
]);
|
||||
|
||||
$response->assertStatus(201);
|
||||
$response->assertJsonPath('ok', true);
|
||||
$this->assertDatabaseHas('notifications', [
|
||||
'title' => 'Announcement',
|
||||
]);
|
||||
$this->assertDatabaseHas('user_notifications', [
|
||||
'user_id' => 2,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_store_validates_payload(): void
|
||||
{
|
||||
$user = $this->createUser();
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = $this->postJson('/api/v1/notifications', [
|
||||
'title' => '',
|
||||
'message' => '',
|
||||
'target_group' => '',
|
||||
'channels' => ['invalid'],
|
||||
]);
|
||||
|
||||
$response->assertStatus(422);
|
||||
$response->assertJsonStructure(['message', 'errors']);
|
||||
}
|
||||
|
||||
public function test_store_handles_service_exception(): void
|
||||
{
|
||||
$this->mock(NotificationSendService::class, function ($mock) {
|
||||
$mock->shouldReceive('send')->andThrow(new \RuntimeException('boom'));
|
||||
});
|
||||
|
||||
$user = $this->createUser();
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = $this->postJson('/api/v1/notifications', [
|
||||
'title' => 'Announcement',
|
||||
'message' => 'Hello parents',
|
||||
'target_group' => 'parent',
|
||||
'channels' => ['in_app'],
|
||||
]);
|
||||
|
||||
$response->assertStatus(500);
|
||||
$response->assertJsonPath('ok', false);
|
||||
}
|
||||
|
||||
public function test_mark_read_updates_user_notification(): void
|
||||
{
|
||||
$user = $this->createUser();
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
DB::table('notifications')->insert([
|
||||
'id' => 1,
|
||||
'title' => 'Hello',
|
||||
'message' => 'World',
|
||||
'target_group' => 'parent',
|
||||
'delivery_channels' => json_encode(['in_app']),
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
|
||||
DB::table('user_notifications')->insert([
|
||||
'notification_id' => 1,
|
||||
'user_id' => 1,
|
||||
'is_read' => 0,
|
||||
]);
|
||||
|
||||
$response = $this->postJson('/api/v1/notifications/1/read');
|
||||
|
||||
$response->assertOk();
|
||||
$this->assertDatabaseHas('user_notifications', [
|
||||
'notification_id' => 1,
|
||||
'user_id' => 1,
|
||||
'is_read' => 1,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_active_and_deleted_lists(): void
|
||||
{
|
||||
$user = $this->createUser();
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
DB::table('notifications')->insert([
|
||||
'id' => 1,
|
||||
'title' => 'Active',
|
||||
'message' => 'Now',
|
||||
'target_group' => 'parent',
|
||||
'delivery_channels' => json_encode(['in_app']),
|
||||
'scheduled_at' => now()->subDay(),
|
||||
'expires_at' => now()->addDay(),
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
|
||||
DB::table('notifications')->insert([
|
||||
'id' => 2,
|
||||
'title' => 'Deleted',
|
||||
'message' => 'Old',
|
||||
'target_group' => 'parent',
|
||||
'delivery_channels' => json_encode(['in_app']),
|
||||
'deleted_at' => now(),
|
||||
'created_at' => now()->subDays(2),
|
||||
'updated_at' => now()->subDays(2),
|
||||
]);
|
||||
|
||||
$active = $this->getJson('/api/v1/notifications/active');
|
||||
$active->assertOk();
|
||||
$active->assertJsonPath('ok', true);
|
||||
|
||||
$deleted = $this->getJson('/api/v1/notifications/deleted');
|
||||
$deleted->assertOk();
|
||||
$deleted->assertJsonPath('ok', true);
|
||||
}
|
||||
|
||||
public function test_update_and_delete_notification(): void
|
||||
{
|
||||
$user = $this->createUser();
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
DB::table('notifications')->insert([
|
||||
'id' => 1,
|
||||
'title' => 'Old',
|
||||
'message' => 'Old',
|
||||
'target_group' => 'parent',
|
||||
'delivery_channels' => json_encode(['in_app']),
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
|
||||
$update = $this->patchJson('/api/v1/notifications/1', [
|
||||
'title' => 'New',
|
||||
]);
|
||||
$update->assertOk();
|
||||
$this->assertDatabaseHas('notifications', [
|
||||
'id' => 1,
|
||||
'title' => 'New',
|
||||
]);
|
||||
|
||||
$delete = $this->deleteJson('/api/v1/notifications/1');
|
||||
$delete->assertOk();
|
||||
$this->assertSoftDeleted('notifications', ['id' => 1]);
|
||||
}
|
||||
|
||||
public function test_restore_notification(): void
|
||||
{
|
||||
$user = $this->createUser();
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
DB::table('notifications')->insert([
|
||||
'id' => 1,
|
||||
'title' => 'Old',
|
||||
'message' => 'Old',
|
||||
'target_group' => 'parent',
|
||||
'delivery_channels' => json_encode(['in_app']),
|
||||
'deleted_at' => now(),
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
|
||||
$response = $this->postJson('/api/v1/notifications/1/restore');
|
||||
|
||||
$response->assertOk();
|
||||
$this->assertDatabaseHas('notifications', [
|
||||
'id' => 1,
|
||||
'deleted_at' => null,
|
||||
]);
|
||||
}
|
||||
|
||||
private function seedRole(string $name): void
|
||||
{
|
||||
DB::table('roles')->insert([
|
||||
'id' => 1,
|
||||
'name' => $name,
|
||||
'slug' => $name,
|
||||
'is_active' => 1,
|
||||
]);
|
||||
}
|
||||
|
||||
private function createUser(): User
|
||||
{
|
||||
DB::table('users')->insert([
|
||||
'id' => 1,
|
||||
'school_id' => 1,
|
||||
'firstname' => 'Admin',
|
||||
'lastname' => 'User',
|
||||
'cellphone' => '5555555555',
|
||||
'email' => 'admin@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',
|
||||
]);
|
||||
|
||||
return User::query()->findOrFail(1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Console;
|
||||
|
||||
use App\Services\Attendance\AttendanceAutoPublishJobService;
|
||||
use App\Services\Attendance\AttendanceDailySummaryService;
|
||||
use App\Services\Attendance\AttendanceSummaryRebuildService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Mockery;
|
||||
use Tests\TestCase;
|
||||
|
||||
class AttendanceCommandsTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_attendance_auto_publish_command_runs(): void
|
||||
{
|
||||
$service = Mockery::mock(AttendanceAutoPublishJobService::class);
|
||||
$service->shouldReceive('run')->once()->andReturn([
|
||||
'checked_at' => '2025-03-01 10:00:00',
|
||||
'timezone' => 'UTC',
|
||||
'published' => 0,
|
||||
]);
|
||||
|
||||
$this->app->instance(AttendanceAutoPublishJobService::class, $service);
|
||||
|
||||
$this->artisan('attendance:auto-publish')->assertExitCode(0);
|
||||
}
|
||||
|
||||
public function test_attendance_recalculate_summary_command_runs(): void
|
||||
{
|
||||
$service = Mockery::mock(AttendanceSummaryRebuildService::class);
|
||||
$service->shouldReceive('rebuild')->once()->andReturn(['inserted' => 3]);
|
||||
|
||||
$this->app->instance(AttendanceSummaryRebuildService::class, $service);
|
||||
|
||||
$this->artisan('attendance:recalculate-summary')->assertExitCode(0);
|
||||
}
|
||||
|
||||
public function test_attendance_absentees_summary_command_runs(): void
|
||||
{
|
||||
$service = Mockery::mock(AttendanceDailySummaryService::class);
|
||||
$service->shouldReceive('sendAbsenteesSummary')->once()->andReturn(2);
|
||||
|
||||
$this->app->instance(AttendanceDailySummaryService::class, $service);
|
||||
|
||||
$this->artisan('attendance:absentees-summary')->assertExitCode(0);
|
||||
}
|
||||
|
||||
public function test_attendance_lates_summary_command_runs(): void
|
||||
{
|
||||
$service = Mockery::mock(AttendanceDailySummaryService::class);
|
||||
$service->shouldReceive('sendLatesSummary')->once()->andReturn(1);
|
||||
|
||||
$this->app->instance(AttendanceDailySummaryService::class, $service);
|
||||
|
||||
$this->artisan('attendance:lates-summary')->assertExitCode(0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Console;
|
||||
|
||||
use App\Services\Payments\PaymentMissedCheckService;
|
||||
use App\Services\Payments\PaymentNotificationService;
|
||||
use App\Services\Payments\PaymentTestNotificationService;
|
||||
use App\Services\Payments\PaypalPaymentSyncService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Mockery;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PaymentsCommandsTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_check_missed_payments_command_runs(): void
|
||||
{
|
||||
$service = Mockery::mock(PaymentMissedCheckService::class);
|
||||
$service->shouldReceive('findUsersWithMissedPayments')->once()->andReturn([]);
|
||||
|
||||
$this->app->instance(PaymentMissedCheckService::class, $service);
|
||||
|
||||
$this->artisan('payments:check-missed')->assertExitCode(0);
|
||||
}
|
||||
|
||||
public function test_monthly_reminder_command_sends_to_email(): void
|
||||
{
|
||||
DB::table('users')->insert([
|
||||
'id' => 1,
|
||||
'firstname' => 'Parent',
|
||||
'lastname' => 'User',
|
||||
'email' => 'parent@example.com',
|
||||
'status' => 'Active',
|
||||
]);
|
||||
|
||||
$service = Mockery::mock(PaymentNotificationService::class);
|
||||
$service->shouldReceive('send')->once()->andReturn([
|
||||
'sent' => 1,
|
||||
'skipped' => 0,
|
||||
'failed' => 0,
|
||||
'details' => [],
|
||||
]);
|
||||
|
||||
$this->app->instance(PaymentNotificationService::class, $service);
|
||||
|
||||
$this->artisan('payments:monthly-reminder', ['--email' => 'parent@example.com'])->assertExitCode(0);
|
||||
}
|
||||
|
||||
public function test_send_test_payment_notification_command_runs(): void
|
||||
{
|
||||
$service = Mockery::mock(PaymentTestNotificationService::class);
|
||||
$service->shouldReceive('send')->once()->andReturn(['ok' => true]);
|
||||
|
||||
$this->app->instance(PaymentTestNotificationService::class, $service);
|
||||
|
||||
$this->artisan('payments:send-test', ['--email' => 'parent@example.com'])->assertExitCode(0);
|
||||
}
|
||||
|
||||
public function test_sync_paypal_payments_command_runs(): void
|
||||
{
|
||||
$service = Mockery::mock(PaypalPaymentSyncService::class);
|
||||
$service->shouldReceive('sync')->once()->andReturn([
|
||||
'mode' => 'DRY-RUN',
|
||||
'processed' => 0,
|
||||
'failed' => [],
|
||||
]);
|
||||
|
||||
$this->app->instance(PaypalPaymentSyncService::class, $service);
|
||||
|
||||
$this->artisan('payments:sync-paypal', ['--dry-run' => true])->assertExitCode(0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Console;
|
||||
|
||||
use App\Services\Auth\PasswordResetCleanupService;
|
||||
use App\Services\Notifications\NotificationCleanupService;
|
||||
use App\Services\System\ConfigUpdateService;
|
||||
use App\Services\Users\InactiveUserCleanupService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Mockery;
|
||||
use Tests\TestCase;
|
||||
|
||||
class SystemCommandsTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_cleanup_expired_notifications_command_runs(): void
|
||||
{
|
||||
$service = Mockery::mock(NotificationCleanupService::class);
|
||||
$service->shouldReceive('cleanupExpired')->once()->andReturn(2);
|
||||
|
||||
$this->app->instance(NotificationCleanupService::class, $service);
|
||||
|
||||
$this->artisan('notifications:cleanup')->assertExitCode(0);
|
||||
}
|
||||
|
||||
public function test_cleanup_password_resets_command_runs(): void
|
||||
{
|
||||
$service = Mockery::mock(PasswordResetCleanupService::class);
|
||||
$service->shouldReceive('cleanup')->once()->andReturn(1);
|
||||
|
||||
$this->app->instance(PasswordResetCleanupService::class, $service);
|
||||
|
||||
$this->artisan('cleanup:password-resets')->assertExitCode(0);
|
||||
}
|
||||
|
||||
public function test_config_update_command_runs(): void
|
||||
{
|
||||
$service = Mockery::mock(ConfigUpdateService::class);
|
||||
$service->shouldReceive('availableTasks')->andReturn(['enable_attendance_on']);
|
||||
$service->shouldReceive('runTask')->once()->andReturn(['ok' => true]);
|
||||
|
||||
$this->app->instance(ConfigUpdateService::class, $service);
|
||||
|
||||
$this->artisan('config:update', ['--task' => 'enable_attendance_on', '--tz' => 'UTC'])->assertExitCode(0);
|
||||
}
|
||||
|
||||
public function test_delete_inactive_users_command_runs(): void
|
||||
{
|
||||
$service = Mockery::mock(InactiveUserCleanupService::class);
|
||||
$service->shouldReceive('cleanup')->once()->andReturn([
|
||||
'deleted_users' => 0,
|
||||
'deleted_parents' => 0,
|
||||
'deleted_roles' => 0,
|
||||
]);
|
||||
|
||||
$this->app->instance(InactiveUserCleanupService::class, $service);
|
||||
|
||||
$this->artisan('users:delete-inactive-users')->assertExitCode(0);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user