From ba74ba1c702e7a122328094341e659c2baf9fd3d Mon Sep 17 00:00:00 2001
From: Marius Balteanu <marius.balteanu@zitec.com>
Date: Wed, 23 Feb 2022 21:16:18 +0000
Subject: Allow users to be mentioned using @ in issues and wiki pages
 (#13919):

* the user must have add watchers permission on that object in order to mention other users
* mentioned user will receive a notification email
* only visible users who can view the object can be mentioned



git-svn-id: http://svn.redmine.org/redmine/trunk@21435 e93f8b46-1217-0410-a6f0-8f06a7374b81
---
 test/unit/journal_test.rb                      |   8 ++
 test/unit/lib/redmine/acts/mentionable_test.rb | 146 +++++++++++++++++++++++++
 test/unit/mailer_test.rb                       |  75 +++++++++++++
 3 files changed, 229 insertions(+)
 create mode 100644 test/unit/lib/redmine/acts/mentionable_test.rb

(limited to 'test/unit')

diff --git a/test/unit/journal_test.rb b/test/unit/journal_test.rb
index 0e0177917..f9c269291 100644
--- a/test/unit/journal_test.rb
+++ b/test/unit/journal_test.rb
@@ -236,4 +236,12 @@ class JournalTest < ActiveSupport::TestCase
       assert_equal "image#{i}.png", attachment.filename
     end
   end
+
+  def test_notified_mentions_should_not_include_users_who_cannot_view_private_notes
+    journal = Journal.generate!(journalized: Issue.find(2), user: User.find(1), private_notes: true, notes: 'Hello @dlopper, @jsmith and @admin.')
+
+    # User "dlopper" has "Developer" role on project "eCookbook"
+    # Role "Developer" does not have the "View private notes" permission
+    assert_equal [1, 2], journal.notified_mentions.map(&:id)
+  end
 end
diff --git a/test/unit/lib/redmine/acts/mentionable_test.rb b/test/unit/lib/redmine/acts/mentionable_test.rb
new file mode 100644
index 000000000..9badcd6d2
--- /dev/null
+++ b/test/unit/lib/redmine/acts/mentionable_test.rb
@@ -0,0 +1,146 @@
+# frozen_string_literal: true
+
+# Redmine - project management software
+# Copyright (C) 2006-2022  Jean-Philippe Lang
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+
+require File.expand_path('../../../../../test_helper', __FILE__)
+
+class Redmine::Acts::MentionableTest < ActiveSupport::TestCase
+  fixtures :projects, :users, :email_addresses, :members, :member_roles, :roles,
+         :groups_users,
+         :trackers, :projects_trackers,
+         :enabled_modules,
+         :issue_statuses, :issue_categories, :issue_relations, :workflows,
+         :enumerations,
+         :issues
+
+  def test_mentioned_users_with_user_mention
+    issue = Issue.generate!(project_id: 1, description: '@dlopper')
+
+    assert_equal [User.find(3)], issue.mentioned_users
+  end
+
+  def test_mentioned_users_with_multiple_mentions
+    issue = Issue.generate!(project_id: 1, description: 'Hello @dlopper, @jsmith.')
+
+    assert_equal [User.find(2), User.find(3)], issue.mentioned_users
+  end
+
+  def test_mentioned_users_should_not_mention_same_user_multiple_times
+    issue = Issue.generate!(project_id: 1, description: '@dlopper @jsmith @dlopper')
+
+    assert_equal [User.find(2), User.find(3)], issue.mentioned_users
+  end
+
+  def test_mentioned_users_should_include_only_active_users
+    # disable dlopper account
+    user = User.find(3)
+    user.status = User::STATUS_LOCKED
+    user.save
+
+    issue = Issue.generate!(project_id: 1, description: '@dlopper @jsmith')
+
+    assert_equal [User.find(2)], issue.mentioned_users
+  end
+
+  def test_mentioned_users_should_include_only_visible_users
+    User.current = nil
+    Role.non_member.update! users_visibility: 'members_of_visible_projects'
+    Role.anonymous.update! users_visibility: 'members_of_visible_projects'
+    user = User.generate!
+
+    issue = Issue.generate!(project_id: 1, description: "@jsmith @#{user.login}")
+
+    assert_equal [User.find(2)], issue.mentioned_users
+  end
+
+  def test_mentioned_users_should_not_include_mentioned_users_in_existing_content
+    issue = Issue.generate!(project_id: 1, description: 'Hello @dlopper')
+
+    assert issue.save
+    assert_equal [User.find(3)], issue.mentioned_users
+
+    issue.description = 'Hello @dlopper and @jsmith'
+    issue.save
+
+    assert_equal [User.find(2)], issue.mentioned_users
+  end
+
+  def test_mentioned_users_should_not_include_users_wrapped_in_pre_tags_for_textile
+    description = <<~STR
+      <pre>
+      Hello @jsmith
+      </pre>
+    STR
+
+    with_settings text_formatting: 'textile' do
+      issue = Issue.generate!(project_id: 1, description: description)
+
+      assert_equal [], issue.mentioned_users
+    end
+  end
+
+  def test_mentioned_users_should_not_include_users_wrapped_in_pre_tags_for_markdown
+    description = <<~STR
+      ```
+      Hello @jsmith
+      ```
+    STR
+
+    with_settings text_formatting: 'markdown' do
+      issue = Issue.generate!(project_id: 1, description: description)
+
+      assert_equal [], issue.mentioned_users
+    end
+  end
+
+  def test_mentioned_users_should_not_include_users_wrapped_in_pre_tags_for_common_mark
+    description = <<~STR
+      ```
+      Hello @jsmith
+      ```
+    STR
+
+    with_settings text_formatting: 'common_mark' do
+      issue = Issue.generate!(project_id: 1, description: description)
+
+      assert_equal [], issue.mentioned_users
+    end
+  end
+
+  def test_notified_mentions
+    issue = Issue.generate!(project_id: 1, description: 'Hello @dlopper, @jsmith.')
+
+    assert_equal [User.find(2), User.find(3)], issue.notified_mentions
+  end
+
+  def test_notified_mentions_should_not_include_users_who_out_of_all_email
+    User.find(3).update!(mail_notification: :none)
+    issue = Issue.generate!(project_id: 1, description: "Hello @dlopper, @jsmith.")
+
+    assert_equal [User.find(2)], issue.notified_mentions
+  end
+
+  def test_notified_mentions_should_not_include_users_who_cannot_view_the_object
+    user = User.find(3)
+
+    # User dlopper does not have access to project "Private child of eCookbook"
+    issue = Issue.generate!(project_id: 5, description: "Hello @dlopper, @jsmith.")
+
+    assert !issue.notified_mentions.include?(user)
+  end
+end
diff --git a/test/unit/mailer_test.rb b/test/unit/mailer_test.rb
index 7ee682c06..3e214b47d 100644
--- a/test/unit/mailer_test.rb
+++ b/test/unit/mailer_test.rb
@@ -464,6 +464,19 @@ class MailerTest < ActiveSupport::TestCase
     assert_not_include user.mail, recipients
   end
 
+  def test_issue_add_should_notify_mentioned_users_in_issue_description
+    User.find(1).mail_notification = 'only_my_events'
+
+    issue = Issue.generate!(project_id: 1, description: 'Hello @dlopper and @admin.')
+
+    assert Mailer.deliver_issue_add(issue)
+    # @jsmith and @dlopper are members of the project
+    # admin is mentioned
+    # @dlopper won't receive duplicated notifications
+    assert_equal 3, ActionMailer::Base.deliveries.size
+    assert_include User.find(1).mail, recipients
+  end
+
   def test_issue_add_should_include_enabled_fields
     issue = Issue.find(2)
     assert Mailer.deliver_issue_add(issue)
@@ -608,6 +621,39 @@ class MailerTest < ActiveSupport::TestCase
     end
   end
 
+  def test_issue_edit_should_notify_mentioned_users_in_issue_updated_description
+    User.find(1).mail_notification = 'only_my_events'
+
+    issue = Issue.find(3)
+    issue.init_journal(User.current)
+    issue.update(description: "Hello @admin")
+    journal = issue.journals.last
+
+    ActionMailer::Base.deliveries.clear
+    Mailer.deliver_issue_edit(journal)
+
+    # @jsmith and @dlopper are members of the project
+    # admin is mentioned in the updated description
+    # @dlopper won't receive duplicated notifications
+    assert_equal 3, ActionMailer::Base.deliveries.size
+    assert_include User.find(1).mail, recipients
+  end
+
+  def test_issue_edit_should_notify_mentioned_users_in_notes
+    User.find(1).mail_notification = 'only_my_events'
+
+    journal = Journal.generate!(journalized: Issue.find(3), user: User.find(1), notes: 'Hello @admin.')
+
+    ActionMailer::Base.deliveries.clear
+    Mailer.deliver_issue_edit(journal)
+
+    # @jsmith and @dlopper are members of the project
+    # admin is mentioned in the notes
+    # @dlopper won't receive duplicated notifications
+    assert_equal 3, ActionMailer::Base.deliveries.size
+    assert_include User.find(1).mail, recipients
+  end
+
   def test_issue_should_send_email_notification_with_suppress_empty_fields
     ActionMailer::Base.deliveries.clear
     with_settings :notified_events => %w(issue_added) do
@@ -703,6 +749,20 @@ class MailerTest < ActiveSupport::TestCase
     end
   end
 
+  def test_wiki_content_added_should_notify_mentioned_users_in_content
+    content = WikiContent.new(text: 'Hello @admin.', author_id: 1, page_id: 1)
+    content.save!
+
+    ActionMailer::Base.deliveries.clear
+    Mailer.deliver_wiki_content_added(content)
+
+    # @jsmith and @dlopper are members of the project
+    # admin is mentioned in the notes
+    # @dlopper won't receive duplicated notifications
+    assert_equal 3, ActionMailer::Base.deliveries.size
+    assert_include User.find(1).mail, recipients
+  end
+
   def test_wiki_content_updated
     content = WikiContent.find(1)
     assert Mailer.deliver_wiki_content_updated(content)
@@ -713,6 +773,21 @@ class MailerTest < ActiveSupport::TestCase
     end
   end
 
+  def test_wiki_content_updated_should_notify_mentioned_users_in_updated_content
+    content = WikiContent.find(1)
+    content.update(text: 'Hello @admin.')
+    content.save!
+
+    ActionMailer::Base.deliveries.clear
+    Mailer.deliver_wiki_content_updated(content)
+
+    # @jsmith and @dlopper are members of the project
+    # admin is mentioned in the notes
+    # @dlopper won't receive duplicated notifications
+    assert_equal 3, ActionMailer::Base.deliveries.size
+    assert_include User.find(1).mail, recipients
+  end
+
   def test_register
     token = Token.find(1)
     assert Mailer.deliver_register(token.user, token)
-- 
cgit v1.2.3