NOW MANAGED ON CODEBERG
Comment Mentions: Added tests to cover back-end functionality
Dan Brown48cdaab690bb
diff --git a/app/Activity/Models/MentionHistory.php b/app/Activity/Models/MentionHistory.phpindex bfa242df5..7386a4d74 100644--- a/app/Activity/Models/MentionHistory.php+++ b/app/Activity/Models/MentionHistory.php@@ -16,4 +16,5 @@ use Illuminate\Support\Carbon;*/class MentionHistory extends Model{+ protected $table = 'mention_history';}diff --git a/lang/en/notifications.php b/lang/en/notifications.phpindex 9d6d94574..563ac24e8 100644--- a/lang/en/notifications.php+++ b/lang/en/notifications.php@@ -11,7 +11,7 @@ return ['updated_page_subject' => 'Updated page: :pageName','updated_page_intro' => 'A page has been updated in :appName:','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.',- 'comment_mention_subject' => 'You were mentioned in a comment on :pageName',+ 'comment_mention_subject' => 'You have been mentioned in a comment on page: :pageName','comment_mention_intro' => 'You were mentioned in a comment on :appName:','detail_page_name' => 'Page Name:',diff --git a/tests/Entity/CommentDisplayTest.php b/tests/Activity/CommentDisplayTest.phpsimilarity index 98%rename from tests/Entity/CommentDisplayTest.phprename to tests/Activity/CommentDisplayTest.phpindex 80664890a..01a4643be 100644--- a/tests/Entity/CommentDisplayTest.php+++ b/tests/Activity/CommentDisplayTest.php@@ -1,10 +1,8 @@<?php-namespace Tests\Entity;+namespace Activity;-use BookStack\Activity\ActivityType;use BookStack\Activity\Models\Comment;-use BookStack\Entities\Models\Page;use Tests\TestCase;class CommentDisplayTest extends TestCasediff --git a/tests/Activity/CommentMentionTest.php b/tests/Activity/CommentMentionTest.phpnew file mode 100644index 000000000..57a36895e--- /dev/null+++ b/tests/Activity/CommentMentionTest.php@@ -0,0 +1,128 @@+<?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);+ return $mail->subject === 'You have been mentioned in a comment on page: ' . $page->name+ && 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);+ }+}diff --git a/tests/Entity/CommentSettingTest.php b/tests/Activity/CommentSettingTest.phpsimilarity index 96%rename from tests/Entity/CommentSettingTest.phprename to tests/Activity/CommentSettingTest.phpindex 7de457441..ad82d9b70 100644--- a/tests/Entity/CommentSettingTest.php+++ b/tests/Activity/CommentSettingTest.php@@ -1,6 +1,6 @@<?php-namespace Tests\Entity;+namespace Activity;use Tests\TestCase;diff --git a/tests/Entity/CommentStoreTest.php b/tests/Activity/CommentStoreTest.phpsimilarity index 99%rename from tests/Entity/CommentStoreTest.phprename to tests/Activity/CommentStoreTest.phpindex c4c959c29..1c9e5aa5f 100644--- a/tests/Entity/CommentStoreTest.php+++ b/tests/Activity/CommentStoreTest.php@@ -1,10 +1,9 @@<?php-namespace Tests\Entity;+namespace Activity;use BookStack\Activity\ActivityType;use BookStack\Activity\Models\Comment;-use BookStack\Entities\Models\Page;use Tests\TestCase;class CommentStoreTest extends TestCasediff --git a/tests/User/UserMyAccountTest.php b/tests/User/UserMyAccountTest.phpindex e1b40dadd..dbb53b061 100644--- a/tests/User/UserMyAccountTest.php+++ b/tests/User/UserMyAccountTest.php@@ -329,11 +329,19 @@ class UserMyAccountTest extends TestCase$resp = $this->asEditor()->get('/my-account/notifications');$resp->assertSee('Notify upon comments');$resp->assertSee('Notify upon replies');+ $resp->assertSee('Notify when I\'m mentioned in a comment');setting()->put('app-disable-comments', true);$resp = $this->get('/my-account/notifications');$resp->assertDontSee('Notify upon comments');$resp->assertDontSee('Notify upon replies');+ $resp->assertDontSee('Notify when I\'m mentioned in a comment');+ }++ public function test_notification_comment_mention_option_enabled_by_default()+ {+ $resp = $this->asEditor()->get('/my-account/notifications');+ $this->withHtml($resp)->assertElementExists('input[name="preferences[comment-mentions]"][value="true"]');}}