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