Skip to content

Commit 3d9aba7

Browse files
committed
Mentions: Added coverage for mentions search endpoint
1 parent 48cdaab commit 3d9aba7

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

app/Activity/Notifications/Handlers/CommentMentionNotificationHandler.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ class CommentMentionNotificationHandler extends BaseNotificationHandler
1919
{
2020
public function handle(Activity $activity, Loggable|string $detail, User $user): void
2121
{
22-
$page = $detail->entity;
23-
if (!($detail instanceof Comment) || !($page instanceof Page)) {
22+
if (!($detail instanceof Comment) || !($detail->entity instanceof Page)) {
2423
throw new \InvalidArgumentException("Detail for comment mention notifications must be a comment on a page");
2524
}
2625

26+
/** @var Page $page */
27+
$page = $detail->entity;
28+
2729
$parser = new MentionParser();
2830
$mentionedUserIds = $parser->parseUserIdsFromHtml($detail->html);
2931
$realMentionedUsers = User::whereIn('id', $mentionedUserIds)->get();
@@ -50,6 +52,9 @@ public function handle(Activity $activity, Loggable|string $detail, User $user):
5052
$this->sendNotificationToUserIds(CommentMentionNotification::class, $receivingNotificationsUserIds, $user, $detail, $page);
5153
}
5254

55+
/**
56+
* @param Collection<User> $mentionedUsers
57+
*/
5358
protected function logMentions(Collection $mentionedUsers, Comment $comment, User $fromUser): void
5459
{
5560
$mentions = [];

tests/User/UserSearchTest.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Tests\User;
44

5+
use BookStack\Permissions\Permission;
56
use BookStack\Users\Models\User;
67
use Tests\TestCase;
78

@@ -62,4 +63,70 @@ public function test_select_requires_logged_in_user()
6263
$resp = $this->get('/search/users/select?search=a');
6364
$this->assertPermissionError($resp);
6465
}
66+
67+
public function test_mentions_search_matches_by_name()
68+
{
69+
$viewer = $this->users->viewer();
70+
$editor = $this->users->editor();
71+
72+
$resp = $this->actingAs($editor)->get('/search/users/mention?search=' . urlencode($viewer->name));
73+
74+
$resp->assertOk();
75+
$resp->assertSee($viewer->name);
76+
$resp->assertDontSee($editor->name);
77+
}
78+
79+
public function test_mentions_search_does_not_match_by_email()
80+
{
81+
$viewer = $this->users->viewer();
82+
83+
$resp = $this->asEditor()->get('/search/users/mention?search=' . urlencode($viewer->email));
84+
85+
$resp->assertDontSee($viewer->name);
86+
}
87+
88+
public function test_mentions_search_requires_logged_in_user()
89+
{
90+
$this->setSettings(['app-public' => true]);
91+
$guest = $this->users->guest();
92+
$this->permissions->grantUserRolePermissions($guest, [Permission::CommentCreateAll, Permission::CommentUpdateAll]);
93+
94+
$resp = $this->get('/search/users/mention?search=a');
95+
$this->assertPermissionError($resp);
96+
}
97+
98+
public function test_mentions_search_requires_comment_create_or_update_permission()
99+
{
100+
$viewer = $this->users->viewer();
101+
$editor = $this->users->editor();
102+
103+
$resp = $this->actingAs($viewer)->get('/search/users/mention?search=' . urlencode($editor->name));
104+
$this->assertPermissionError($resp);
105+
106+
$this->permissions->grantUserRolePermissions($viewer, [Permission::CommentCreateAll]);
107+
108+
$resp = $this->actingAs($editor)->get('/search/users/mention?search=' . urlencode($viewer->name));
109+
$resp->assertOk();
110+
$resp->assertSee($viewer->name);
111+
112+
$this->permissions->removeUserRolePermissions($viewer, [Permission::CommentCreateAll]);
113+
$this->permissions->grantUserRolePermissions($viewer, [Permission::CommentUpdateAll]);
114+
115+
$resp = $this->actingAs($editor)->get('/search/users/mention?search=' . urlencode($viewer->name));
116+
$resp->assertOk();
117+
$resp->assertSee($viewer->name);
118+
}
119+
120+
public function test_mentions_search_shows_first_by_name_without_search()
121+
{
122+
/** @var User $firstUser */
123+
$firstUser = User::query()
124+
->orderBy('name', 'asc')
125+
->first();
126+
127+
$resp = $this->asEditor()->get('/search/users/mention');
128+
129+
$resp->assertOk();
130+
$this->withHtml($resp)->assertElementContains('a[data-id]:first-child', $firstUser->name);
131+
}
65132
}

0 commit comments

Comments
 (0)