Files
L-Ami-Fiduciaire/tests/Feature/Notification/NotificationControllerTest.php
Saad Zoubir 1ab3cfc445 feat: add notification infrastructure with database channel, enum, and notification classes (Story 3.1)
Set up Laravel notification system with NotificationType enum (5 types),
NudgeNotification, DocumentUploadedNotification, and DeclarationOverdueNotification
classes with database + mail channels. Add email templates, infrastructure tests,
and fix existing NotificationController tests for workspace compatibility.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-26 11:26:03 +01:00

87 lines
3.1 KiB
PHP

<?php
use App\Models\Client;
use App\Models\Declaration;
use App\Models\User;
use App\Models\Workspace;
use App\Notifications\DeclarationMentionNotification;
use App\Notifications\DocumentUploadedNotification;
test('user can mark own notification as read', function () {
$user = User::factory()->create();
$workspace = Workspace::factory()->create();
$workspace->users()->attach($user, ['role' => 'owner', 'permissions' => json_encode([])]);
$user->update(['current_workspace_id' => $workspace->id]);
$client = Client::factory()->create(['workspace_id' => $workspace->id]);
$declaration = Declaration::factory()->create([
'workspace_id' => $workspace->id,
'client_id' => $client->id,
]);
$mentionedBy = User::factory()->create();
$user->notify(new DeclarationMentionNotification(
$declaration,
$mentionedBy,
'Please review.',
));
$notification = $user->notifications()->first();
expect($notification->read_at)->toBeNull();
$response = $this->actingAs($user)->post(route('notifications.read', $notification->id));
$response->assertRedirect();
$notification->refresh();
expect($notification->read_at)->not->toBeNull();
});
test('cannot mark another user notification as read', function () {
$user = User::factory()->create();
$other = User::factory()->create();
$workspace = Workspace::factory()->create();
$workspace->users()->attach($user, ['role' => 'owner', 'permissions' => json_encode([])]);
$workspace->users()->attach($other, ['role' => 'worker', 'permissions' => json_encode([])]);
$user->update(['current_workspace_id' => $workspace->id]);
$client = Client::factory()->create(['workspace_id' => $workspace->id]);
$declaration = Declaration::factory()->create([
'workspace_id' => $workspace->id,
'client_id' => $client->id,
]);
$mentionedBy = User::factory()->create();
$other->notify(new DeclarationMentionNotification(
$declaration,
$mentionedBy,
'Hey.',
));
$notification = $other->notifications()->first();
$response = $this->actingAs($user)->post(route('notifications.read', $notification->id));
$response->assertNotFound();
});
test('user can mark all notifications as read', function () {
$user = User::factory()->create();
$workspace = Workspace::factory()->create();
$workspace->users()->attach($user, ['role' => 'owner', 'permissions' => json_encode([])]);
$user->update(['current_workspace_id' => $workspace->id]);
$client = Client::factory()->create(['workspace_id' => $workspace->id]);
for ($i = 0; $i < 3; $i++) {
$declaration = Declaration::factory()->create([
'workspace_id' => $workspace->id,
'client_id' => $client->id,
]);
$user->notify(new DocumentUploadedNotification($declaration, $client->id));
}
expect($user->unreadNotifications()->count())->toBe(3);
$response = $this->actingAs($user)->post(route('notifications.readAll'));
$response->assertRedirect();
$user->refresh();
expect($user->unreadNotifications()->count())->toBe(0);
});