Skip to content

Commit 48cdaab

Browse files
committed
Comment Mentions: Added tests to cover back-end functionality
1 parent 4f76047 commit 48cdaab

File tree

7 files changed

+141
-7
lines changed

7 files changed

+141
-7
lines changed

app/Activity/Models/MentionHistory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@
1616
*/
1717
class MentionHistory extends Model
1818
{
19+
protected $table = 'mention_history';
1920
}

lang/en/notifications.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
'updated_page_subject' => 'Updated page: :pageName',
1212
'updated_page_intro' => 'A page has been updated in :appName:',
1313
'updated_page_debounce' => 'To prevent a mass of notifications, for a while you won\'t be sent notifications for further edits to this page by the same editor.',
14-
'comment_mention_subject' => 'You were mentioned in a comment on :pageName',
14+
'comment_mention_subject' => 'You have been mentioned in a comment on page: :pageName',
1515
'comment_mention_intro' => 'You were mentioned in a comment on :appName:',
1616

1717
'detail_page_name' => 'Page Name:',
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
<?php
22

3-
namespace Tests\Entity;
3+
namespace Activity;
44

5-
use BookStack\Activity\ActivityType;
65
use BookStack\Activity\Models\Comment;
7-
use BookStack\Entities\Models\Page;
86
use Tests\TestCase;
97

108
class CommentDisplayTest extends TestCase
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?php
2+
3+
namespace Tests\Activity;
4+
5+
use BookStack\Activity\Notifications\Messages\CommentMentionNotification;
6+
use BookStack\Permissions\Permission;
7+
use Illuminate\Support\Facades\Notification;
8+
use Tests\TestCase;
9+
10+
class CommentMentionTest extends TestCase
11+
{
12+
public function test_mentions_are_notified()
13+
{
14+
$userToMention = $this->users->viewer();
15+
$this->permissions->grantUserRolePermissions($userToMention, [Permission::ReceiveNotifications]);
16+
$editor = $this->users->editor();
17+
$page = $this->entities->pageWithinChapter();
18+
$notifications = Notification::fake();
19+
20+
$this->actingAs($editor)->post("/comment/{$page->id}", [
21+
'html' => '<p>Hello <a data-mention-user-id="' . $userToMention->id . '">@user</a></p>'
22+
])->assertOk();
23+
24+
$notifications->assertSentTo($userToMention, function (CommentMentionNotification $notification) use ($userToMention, $editor, $page) {
25+
$mail = $notification->toMail($userToMention);
26+
$mailContent = html_entity_decode(strip_tags($mail->render()), ENT_QUOTES);
27+
return $mail->subject === 'You have been mentioned in a comment on page: ' . $page->name
28+
&& str_contains($mailContent, 'View Comment')
29+
&& str_contains($mailContent, 'Page Name: ' . $page->name)
30+
&& str_contains($mailContent, 'Page Path: ' . $page->book->getShortName(24) . ' > ' . $page->chapter->getShortName(24))
31+
&& str_contains($mailContent, 'Commenter: ' . $editor->name)
32+
&& str_contains($mailContent, 'Comment: Hello @user');
33+
});
34+
}
35+
36+
public function test_mentions_are_not_notified_if_mentioned_by_same_user()
37+
{
38+
$editor = $this->users->editor();
39+
$this->permissions->grantUserRolePermissions($editor, [Permission::ReceiveNotifications]);
40+
$page = $this->entities->page();
41+
$notifications = Notification::fake();
42+
43+
$this->actingAs($editor)->post("/comment/{$page->id}", [
44+
'html' => '<p>Hello <a data-mention-user-id="' . $editor->id . '"></a></p>'
45+
])->assertOk();
46+
47+
$notifications->assertNothingSent();
48+
}
49+
50+
public function test_mentions_are_logged_to_the_database_even_if_not_notified()
51+
{
52+
$editor = $this->users->editor();
53+
$otherUser = $this->users->viewer();
54+
$this->permissions->grantUserRolePermissions($editor, [Permission::ReceiveNotifications]);
55+
$page = $this->entities->page();
56+
$notifications = Notification::fake();
57+
58+
$this->actingAs($editor)->post("/comment/{$page->id}", [
59+
'html' => '<p>Hello <a data-mention-user-id="' . $editor->id . '"></a> and <a data-mention-user-id="' . $otherUser->id . '"></a></p>'
60+
])->assertOk();
61+
62+
$notifications->assertNothingSent();
63+
64+
$comment = $page->comments()->latest()->first();
65+
$this->assertDatabaseHas('mention_history', [
66+
'mentionable_id' => $comment->id,
67+
'mentionable_type' => 'comment',
68+
'from_user_id' => $editor->id,
69+
'to_user_id' => $otherUser->id,
70+
]);
71+
$this->assertDatabaseHas('mention_history', [
72+
'mentionable_id' => $comment->id,
73+
'mentionable_type' => 'comment',
74+
'from_user_id' => $editor->id,
75+
'to_user_id' => $editor->id,
76+
]);
77+
}
78+
79+
public function test_comment_updates_will_send_notifications_only_if_mention_is_new()
80+
{
81+
$userToMention = $this->users->viewer();
82+
$this->permissions->grantUserRolePermissions($userToMention, [Permission::ReceiveNotifications]);
83+
$editor = $this->users->editor();
84+
$this->permissions->grantUserRolePermissions($editor, [Permission::CommentUpdateOwn]);
85+
$page = $this->entities->page();
86+
$notifications = Notification::fake();
87+
88+
$this->actingAs($editor)->post("/comment/{$page->id}", [
89+
'html' => '<p>Hello there</p>'
90+
])->assertOk();
91+
$comment = $page->comments()->latest()->first();
92+
93+
$notifications->assertNothingSent();
94+
95+
$this->put("/comment/{$comment->id}", [
96+
'html' => '<p>Hello <a data-mention-user-id="' . $userToMention->id . '"></a></p>'
97+
])->assertOk();
98+
99+
$notifications->assertSentTo($userToMention, CommentMentionNotification::class);
100+
$notifications->assertCount(1);
101+
102+
$this->put("/comment/{$comment->id}", [
103+
'html' => '<p>Hello again<a data-mention-user-id="' . $userToMention->id . '"></a></p>'
104+
])->assertOk();
105+
106+
$notifications->assertCount(1);
107+
}
108+
109+
public function test_notification_limited_to_those_with_view_permissions()
110+
{
111+
$userA = $this->users->newUser();
112+
$userB = $this->users->newUser();
113+
$this->permissions->grantUserRolePermissions($userA, [Permission::ReceiveNotifications]);
114+
$this->permissions->grantUserRolePermissions($userB, [Permission::ReceiveNotifications]);
115+
$notifications = Notification::fake();
116+
$page = $this->entities->page();
117+
118+
$this->permissions->disableEntityInheritedPermissions($page);
119+
$this->permissions->addEntityPermission($page, ['view'], $userA->roles()->first());
120+
121+
$this->asAdmin()->post("/comment/{$page->id}", [
122+
'html' => '<p>Hello <a data-mention-user-id="' . $userA->id . '"></a> and <a data-mention-user-id="' . $userB->id . '"></a></p>'
123+
])->assertOk();
124+
125+
$notifications->assertCount(1);
126+
$notifications->assertSentTo($userA, CommentMentionNotification::class);
127+
}
128+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Tests\Entity;
3+
namespace Activity;
44

55
use Tests\TestCase;
66

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
<?php
22

3-
namespace Tests\Entity;
3+
namespace Activity;
44

55
use BookStack\Activity\ActivityType;
66
use BookStack\Activity\Models\Comment;
7-
use BookStack\Entities\Models\Page;
87
use Tests\TestCase;
98

109
class CommentStoreTest extends TestCase

tests/User/UserMyAccountTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,11 +329,19 @@ public function test_notification_comment_options_only_exist_if_comments_active(
329329
$resp = $this->asEditor()->get('/my-account/notifications');
330330
$resp->assertSee('Notify upon comments');
331331
$resp->assertSee('Notify upon replies');
332+
$resp->assertSee('Notify when I\'m mentioned in a comment');
332333

333334
setting()->put('app-disable-comments', true);
334335

335336
$resp = $this->get('/my-account/notifications');
336337
$resp->assertDontSee('Notify upon comments');
337338
$resp->assertDontSee('Notify upon replies');
339+
$resp->assertDontSee('Notify when I\'m mentioned in a comment');
340+
}
341+
342+
public function test_notification_comment_mention_option_enabled_by_default()
343+
{
344+
$resp = $this->asEditor()->get('/my-account/notifications');
345+
$this->withHtml($resp)->assertElementExists('input[name="preferences[comment-mentions]"][value="true"]');
338346
}
339347
}

0 commit comments

Comments
 (0)