2026-03-11 23:33:10 +00:00
|
|
|
<?php
|
|
|
|
|
|
2026-03-12 18:25:32 +00:00
|
|
|
use App\Models\Client;
|
|
|
|
|
use App\Models\Declaration;
|
2026-03-11 23:33:10 +00:00
|
|
|
use App\Models\User;
|
2026-03-12 18:25:32 +00:00
|
|
|
use App\Models\Workspace;
|
|
|
|
|
use App\Notifications\DeclarationMentionNotification;
|
2026-03-26 11:26:03 +01:00
|
|
|
use App\Notifications\DocumentUploadedNotification;
|
2026-03-11 23:33:10 +00:00
|
|
|
|
|
|
|
|
test('user can mark own notification as read', function () {
|
|
|
|
|
$user = User::factory()->create();
|
2026-03-12 18:25:32 +00:00
|
|
|
$workspace = Workspace::factory()->create();
|
2026-03-26 11:26:03 +01:00
|
|
|
$workspace->users()->attach($user, ['role' => 'owner', 'permissions' => json_encode([])]);
|
|
|
|
|
$user->update(['current_workspace_id' => $workspace->id]);
|
2026-03-12 18:25:32 +00:00
|
|
|
$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.',
|
2026-03-11 23:33:10 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
|
|
$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();
|
2026-03-12 18:25:32 +00:00
|
|
|
$workspace = Workspace::factory()->create();
|
2026-03-26 11:26:03 +01:00
|
|
|
$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]);
|
2026-03-12 18:25:32 +00:00
|
|
|
$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.',
|
2026-03-11 23:33:10 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
|
|
$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();
|
2026-03-12 18:25:32 +00:00
|
|
|
$workspace = Workspace::factory()->create();
|
2026-03-26 11:26:03 +01:00
|
|
|
$workspace->users()->attach($user, ['role' => 'owner', 'permissions' => json_encode([])]);
|
|
|
|
|
$user->update(['current_workspace_id' => $workspace->id]);
|
2026-03-12 18:25:32 +00:00
|
|
|
$client = Client::factory()->create(['workspace_id' => $workspace->id]);
|
2026-03-11 23:33:10 +00:00
|
|
|
|
|
|
|
|
for ($i = 0; $i < 3; $i++) {
|
2026-03-12 18:25:32 +00:00
|
|
|
$declaration = Declaration::factory()->create([
|
|
|
|
|
'workspace_id' => $workspace->id,
|
|
|
|
|
'client_id' => $client->id,
|
|
|
|
|
]);
|
2026-03-26 11:26:03 +01:00
|
|
|
$user->notify(new DocumentUploadedNotification($declaration, $client->id));
|
2026-03-11 23:33:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
expect($user->unreadNotifications()->count())->toBe(3);
|
|
|
|
|
|
|
|
|
|
$response = $this->actingAs($user)->post(route('notifications.readAll'));
|
|
|
|
|
$response->assertRedirect();
|
|
|
|
|
|
2026-03-26 11:26:03 +01:00
|
|
|
$user->refresh();
|
2026-03-11 23:33:10 +00:00
|
|
|
expect($user->unreadNotifications()->count())->toBe(0);
|
|
|
|
|
});
|