create(); $target = User::factory()->create(); $workspace = Workspace::factory()->create(); $workspace->users()->attach($sender, ['role' => $role]); $workspace->users()->attach($target, ['role' => 'member']); $client = Client::factory()->create(['workspace_id' => $workspace->id]); $folder = Folder::factory()->create([ 'workspace_id' => $workspace->id, 'client_id' => $client->id, ]); return [$sender, $target, $workspace, $folder]; } test('owner can mention a workspace user', function () { Notification::fake(); [$sender, $target, $workspace, $folder] = setupFolderMentionScenario('owner'); session(['current_workspace_id' => $workspace->id]); $response = $this->actingAs($sender)->post(route('folders.mentions.store', $folder), [ 'user_id' => $target->id, 'message' => 'Please check this folder.', ]); $response->assertRedirect(); Notification::assertSentTo($target, FolderMentionNotification::class); }); test('manager can mention a workspace user', function () { Notification::fake(); [$sender, $target, $workspace, $folder] = setupFolderMentionScenario('manager'); session(['current_workspace_id' => $workspace->id]); $response = $this->actingAs($sender)->post(route('folders.mentions.store', $folder), [ 'user_id' => $target->id, 'message' => 'Please check this folder.', ]); $response->assertRedirect(); Notification::assertSentTo($target, FolderMentionNotification::class); }); test('member cannot mention a workspace user', function () { Notification::fake(); [$sender, $target, $workspace, $folder] = setupFolderMentionScenario('member'); session(['current_workspace_id' => $workspace->id]); $response = $this->actingAs($sender)->post(route('folders.mentions.store', $folder), [ 'user_id' => $target->id, 'message' => 'Please check this folder.', ]); $response->assertForbidden(); Notification::assertNothingSent(); }); test('cannot mention user from another workspace', function () { Notification::fake(); [$sender, , $workspace, $folder] = setupFolderMentionScenario('owner'); $outsider = User::factory()->create(); session(['current_workspace_id' => $workspace->id]); $response = $this->actingAs($sender)->post(route('folders.mentions.store', $folder), [ 'user_id' => $outsider->id, 'message' => 'Hello', ]); $response->assertSessionHasErrors('user_id'); Notification::assertNothingSent(); }); test('notification is persisted in database', function () { [$sender, $target, $workspace, $folder] = setupFolderMentionScenario('owner'); session(['current_workspace_id' => $workspace->id]); $this->actingAs($sender)->post(route('folders.mentions.store', $folder), [ 'user_id' => $target->id, 'message' => 'Check this.', ]); expect($target->notifications()->count())->toBe(1); $notif = $target->notifications()->first(); expect($notif->data['folder_id'])->toBe($folder->id); expect($notif->data['message'])->toBe('Check this.'); });