trypost/tests/Feature/NotificationControllerTest.php
Paulo Castellano 09e37f2879 feat: notification system with SendNotification job, dialog UI, tests
Backend:
- Create notifications table (user_id, workspace_id, type, channel,
  title, body, data JSON, read_at, archived_at)
- Create Notification model with Type enum (post_failed,
  account_disconnected, invite_received, member_joined, member_removed)
  and Channel enum (email, in_app, both)
- Create SendNotification job: isolated from publish flow, handles
  saving in-app notification and sending email independently
- NotificationController: index (excludes archived, scoped to workspace),
  markAsRead, markAllAsRead, archiveAll
- Integrate with PublishToSocialPlatform (post failed/partial)
- Integrate with VerifyWorkspaceConnections (batch disconnection)
- Integrate with SocialAccount::markAsDisconnected (single disconnection)
- All use SendNotification::dispatch() instead of direct Mail::to()

Frontend:
- NotificationBell component in sidebar footer with unread badge
- Dialog with notification list, mark as read, mark all read, archive all
- Click navigates to relevant page (post edit, accounts)
- i18n for notifications UI (en, es, pt-BR)

Tests:
- 8 tests for NotificationController (auth, CRUD, workspace scoping)
- 4 tests for SendNotification job (channels, email, data storage)

All 745 tests passing.
2026-03-30 16:47:03 -03:00

132 lines
4 KiB
PHP

<?php
declare(strict_types=1);
use App\Enums\User\Setup;
use App\Models\Notification;
use App\Models\User;
use App\Models\Workspace;
beforeEach(function () {
$this->user = User::factory()->create(['setup' => Setup::Completed]);
$this->workspace = Workspace::factory()->create(['user_id' => $this->user->id]);
$this->workspace->members()->attach($this->user->id, ['role' => 'owner']);
$this->user->update(['current_workspace_id' => $this->workspace->id]);
});
test('notifications index requires authentication', function () {
$response = $this->getJson(route('app.notifications.index'));
$response->assertUnauthorized();
});
test('notifications index returns notifications and unread count', function () {
Notification::factory()->count(3)->create([
'user_id' => $this->user->id,
'workspace_id' => $this->workspace->id,
]);
Notification::factory()->read()->create([
'user_id' => $this->user->id,
'workspace_id' => $this->workspace->id,
]);
$response = $this->actingAs($this->user)->getJson(route('app.notifications.index'));
$response->assertOk();
$response->assertJsonCount(4, 'notifications');
$response->assertJsonPath('unread_count', 3);
});
test('notifications index excludes archived notifications', function () {
Notification::factory()->create([
'user_id' => $this->user->id,
'workspace_id' => $this->workspace->id,
]);
Notification::factory()->create([
'user_id' => $this->user->id,
'workspace_id' => $this->workspace->id,
'archived_at' => now(),
]);
$response = $this->actingAs($this->user)->getJson(route('app.notifications.index'));
$response->assertOk();
$response->assertJsonCount(1, 'notifications');
});
test('notifications index only shows current workspace notifications', function () {
Notification::factory()->create([
'user_id' => $this->user->id,
'workspace_id' => $this->workspace->id,
]);
$otherWorkspace = Workspace::factory()->create(['user_id' => $this->user->id]);
Notification::factory()->create([
'user_id' => $this->user->id,
'workspace_id' => $otherWorkspace->id,
]);
$response = $this->actingAs($this->user)->getJson(route('app.notifications.index'));
$response->assertOk();
$response->assertJsonCount(1, 'notifications');
});
test('mark notification as read', function () {
$notification = Notification::factory()->create([
'user_id' => $this->user->id,
'workspace_id' => $this->workspace->id,
]);
$response = $this->actingAs($this->user)->patchJson(route('app.notifications.read', $notification));
$response->assertOk();
expect($notification->fresh()->read_at)->not->toBeNull();
});
test('cannot mark another users notification as read', function () {
$otherUser = User::factory()->create();
$notification = Notification::factory()->create([
'user_id' => $otherUser->id,
'workspace_id' => $this->workspace->id,
]);
$response = $this->actingAs($this->user)->patchJson(route('app.notifications.read', $notification));
$response->assertForbidden();
});
test('mark all as read', function () {
Notification::factory()->count(3)->create([
'user_id' => $this->user->id,
'workspace_id' => $this->workspace->id,
]);
$response = $this->actingAs($this->user)->postJson(route('app.notifications.read-all'));
$response->assertOk();
$unread = Notification::where('user_id', $this->user->id)
->whereNull('read_at')
->count();
expect($unread)->toBe(0);
});
test('archive all notifications', function () {
Notification::factory()->count(3)->create([
'user_id' => $this->user->id,
'workspace_id' => $this->workspace->id,
]);
$response = $this->actingAs($this->user)->postJson(route('app.notifications.archive-all'));
$response->assertOk();
$active = Notification::where('user_id', $this->user->id)
->whereNull('archived_at')
->count();
expect($active)->toBe(0);
});