trypost/tests/Feature/Jobs/SendNotificationTest.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

96 lines
2.7 KiB
PHP

<?php
declare(strict_types=1);
use App\Enums\Notification\Channel;
use App\Enums\Notification\Type;
use App\Jobs\SendNotification;
use App\Mail\PostPublishFailed;
use App\Models\Notification;
use App\Models\Post;
use App\Models\User;
use App\Models\Workspace;
use Illuminate\Support\Facades\Mail;
beforeEach(function () {
Mail::fake();
$this->user = User::factory()->create();
$this->workspace = Workspace::factory()->create(['user_id' => $this->user->id]);
});
test('send notification creates in-app notification for in_app channel', function () {
(new SendNotification(
user: $this->user,
workspaceId: $this->workspace->id,
type: Type::PostFailed,
channel: Channel::InApp,
title: 'Test title',
body: 'Test body',
))->handle();
expect(Notification::count())->toBe(1);
$notification = Notification::first();
expect($notification->user_id)->toBe($this->user->id);
expect($notification->title)->toBe('Test title');
expect($notification->type)->toBe(Type::PostFailed);
Mail::assertNothingSent();
});
test('send notification sends email for email channel', function () {
$post = Post::factory()->create([
'workspace_id' => $this->workspace->id,
'user_id' => $this->user->id,
]);
(new SendNotification(
user: $this->user,
workspaceId: $this->workspace->id,
type: Type::PostFailed,
channel: Channel::Email,
title: 'Test title',
body: 'Test body',
mailable: new PostPublishFailed($post),
))->handle();
expect(Notification::count())->toBe(0);
Mail::assertQueued(PostPublishFailed::class);
});
test('send notification creates notification and sends email for both channel', function () {
$post = Post::factory()->create([
'workspace_id' => $this->workspace->id,
'user_id' => $this->user->id,
]);
(new SendNotification(
user: $this->user,
workspaceId: $this->workspace->id,
type: Type::PostFailed,
channel: Channel::Both,
title: 'Test title',
body: 'Test body',
mailable: new PostPublishFailed($post),
))->handle();
expect(Notification::count())->toBe(1);
Mail::assertQueued(PostPublishFailed::class);
});
test('send notification stores data json', function () {
(new SendNotification(
user: $this->user,
workspaceId: $this->workspace->id,
type: Type::PostFailed,
channel: Channel::InApp,
title: 'Test',
body: 'Body',
data: ['post_id' => 'abc-123'],
))->handle();
$notification = Notification::first();
expect($notification->data)->toBe(['post_id' => 'abc-123']);
});