Project

General

Profile

Feature #42630 » Add-reaction-feature.patch

Katsuya HIDAKA, 2025-04-25 10:10

View differences:

app/assets/images/icons.svg
<path d="M19 15v6h3"/>
<path d="M11 21v-6l2.5 3l2.5 -3v6"/>
</symbol>
<symbol viewBox="0 0 24 24" stroke-linecap="round" stroke-linejoin="round" id="icon--thumb-up">
<path d="M7 11v8a1 1 0 0 1 -1 1h-2a1 1 0 0 1 -1 -1v-7a1 1 0 0 1 1 -1h3a4 4 0 0 0 4 -4v-1a2 2 0 0 1 4 0v5h3a2 2 0 0 1 2 2l-1 5a2 3 0 0 1 -2 2h-7a3 3 0 0 1 -3 -3"/>
</symbol>
<symbol viewBox="0 0 24 24" id="icon--thumb-up-filled">
<path d="M13 3a3 3 0 0 1 2.995 2.824l.005 .176v4h2a3 3 0 0 1 2.98 2.65l.015 .174l.005 .176l-.02 .196l-1.006 5.032c-.381 1.626 -1.502 2.796 -2.81 2.78l-.164 -.008h-8a1 1 0 0 1 -.993 -.883l-.007 -.117l.001 -9.536a1 1 0 0 1 .5 -.865a2.998 2.998 0 0 0 1.492 -2.397l.007 -.202v-1a3 3 0 0 1 3 -3z"/>
<path d="M5 10a1 1 0 0 1 .993 .883l.007 .117v9a1 1 0 0 1 -.883 .993l-.117 .007h-1a2 2 0 0 1 -1.995 -1.85l-.005 -.15v-7a2 2 0 0 1 1.85 -1.995l.15 -.005h1z"/>
</symbol>
<symbol viewBox="0 0 24 24" stroke-linecap="round" stroke-linejoin="round" id="icon--time">
<path d="M3 12a9 9 0 1 0 18 0a9 9 0 0 0 -18 0"/>
<path d="M12 7v5l3 3"/>
app/assets/javascripts/application-legacy.js
});
}
function setupHoverTooltips() {
$("[title]:not(.no-tooltip)").tooltip({
function setupHoverTooltips(container) {
$(container || 'body').find("[title]:not(.no-tooltip)").tooltip({
show: {
delay: 400
},
......
}
});
}
function removeHoverTooltips(container) {
$(container || 'body').find("[title]:not(.no-tooltip)").tooltip('destroy')
}
$(function() { setupHoverTooltips(); });
function inlineAutoComplete(element) {
app/assets/stylesheets/application.css
img.filecontent.image {background-image: url(/transparent.png);}
/* Reaction styles */
.reaction-button.reacted .icon-svg {
fill: #126fa7;
stroke: none;
}
.reaction-button.reacted:hover .icon-svg {
fill: #c61a1a;
}
.reaction-button .icon-label {
margin-left: 3px;
margin-bottom: -1px;
}
div.issue.details .reaction {
float: right;
font-size: 0.9em;
margin-top: 0.5em;
}
div.message .reaction {
float: right;
font-size: 0.9em;
}
div.news .reaction {
float: right;
font-size: 0.9em;
}
/* Custom JQuery styles */
.ui-autocomplete, .ui-menu {
border-radius: 2px;
app/controllers/messages_controller.rb
reorder("#{Message.table_name}.created_on ASC, #{Message.table_name}.id ASC").
limit(@reply_pages.per_page).
offset(@reply_pages.offset).
to_a
load_with_reactions
@reply = Message.new(:subject => "RE: #{@message.subject}")
render :action => "show", :layout => false if request.xhr?
app/controllers/news_controller.rb
end
def show
@comments = @news.comments.to_a
@comments = @news.comments.load_with_reactions
@comments.reverse! if User.current.wants_comments_in_reverse_order?
end
app/controllers/reactions_controller.rb
# frozen_string_literal: true
# Redmine - project management software
# Copyright (C) 2006- 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.
class ReactionsController < ApplicationController
before_action :check_enabled
before_action :require_login
before_action :set_object, :authorize_reactable
def create
@reaction = @object.reactions.find_or_create_by!(user: User.current)
end
def destroy
@reaction = @object.reactions.by(User.current).find_by(id: params[:id])
@reaction&.destroy
end
private
def check_enabled
render_403 unless Setting.reactions_enabled?
end
def set_object
object_type = params[:object_type]
unless Redmine::Reaction::REACTABLE_TYPES.include?(object_type)
render_403
return
end
@object = object_type.constantize.find(params[:object_id])
end
def authorize_reactable
render_403 unless @object.visible?(User.current)
end
end
app/helpers/issues_helper.rb
include Redmine::Export::PDF::IssuesPdfHelper
include IssueStatusesHelper
include QueriesHelper
include ReactionsHelper
def issue_list(issues, &)
ancestors = []
app/helpers/journals_helper.rb
module JournalsHelper
include Redmine::QuoteReply::Helper
include ReactionsHelper
# Returns the attachments of a journal that are displayed as thumbnails
def journal_thumbnail_attachments(journal)
......
end
if journal.notes.present?
links << reaction_button(journal)
if options[:reply_links]
url = quoted_issue_path(issue, :journal_id => journal, :journal_indice => indice)
links << quote_reply(url, "#journal-#{journal.id}-notes", icon_only: true)
app/helpers/messages_helper.rb
module MessagesHelper
include Redmine::QuoteReply::Helper
include ReactionsHelper
end
app/helpers/news_helper.rb
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
module NewsHelper
include ReactionsHelper
end
app/helpers/reactions_helper.rb
# frozen_string_literal: true
# Redmine - project management software
# Copyright (C) 2006- 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.
module ReactionsHelper
def reaction_button(object, reaction = nil)
return unless Setting.reactions_enabled? && object.visible?(User.current)
reaction ||= object.reaction_by(User.current)
count = object.reaction_count
user_names = object.reaction_user_names
tooltip = build_reaction_tooltip(user_names, count)
if User.current.logged?
if reaction&.persisted?
reaction_button_reacted(object, reaction, count, tooltip)
else
reaction_button_not_reacted(object, count, tooltip)
end
else
reaction_button_readonly(object, count, tooltip)
end
end
def reaction_id_for(object)
dom_id(object, :reaction)
end
private
def reaction_button_reacted(object, reaction, count, tooltip)
reaction_button_wrapper object do
link_to(
sprite_icon('thumb-up-filled', count),
reaction_path(reaction, object_type: object.class.name, object_id: object),
remote: true, method: :delete,
class: ['icon', 'reaction-button', 'reacted'],
title: tooltip
)
end
end
def reaction_button_not_reacted(object, count, tooltip)
reaction_button_wrapper object do
link_to(
sprite_icon('thumb-up', count),
reactions_path(object_type: object.class.name, object_id: object),
remote: true, method: :post,
class: 'icon reaction-button',
title: tooltip
)
end
end
def reaction_button_readonly(object, count, tooltip)
reaction_button_wrapper object do
tag.span(class: 'icon reaction-button', title: tooltip) do
sprite_icon('thumb-up', count)
end
end
end
def reaction_button_wrapper(object, &)
tag.span(data: { 'reaction-button-id': reaction_id_for(object) }, &)
end
def build_reaction_tooltip(user_names, count)
return if count.zero?
display_user_names = user_names.dup
if count > Redmine::Reaction::DISPLAY_REACTION_USERS_LIMIT
others = count - Redmine::Reaction::DISPLAY_REACTION_USERS_LIMIT
display_user_names << I18n.t(:reaction_text_x_other_users, count: others)
end
display_user_names.to_sentence(locale: I18n.locale)
end
end
app/models/comment.rb
class Comment < ApplicationRecord
include Redmine::SafeAttributes
include Redmine::Reaction::Reactable
belongs_to :commented, :polymorphic => true, :counter_cache => true
belongs_to :author, :class_name => 'User'
......
safe_attributes 'comments'
delegate :visible?, to: :commented
def comments=(arg)
self.content = arg
end
app/models/issue.rb
before_validation :clear_disabled_fields
before_save :set_parent_id
include Redmine::NestedSet::IssueNestedSet
include Redmine::Reaction::Reactable
belongs_to :project
belongs_to :tracker
......
result = journals.
preload(:details).
preload(:user => :email_address).
reorder(:created_on, :id).to_a
reorder(:created_on, :id).
load_with_reactions
result.each_with_index {|j, i| j.indice = i + 1}
app/models/journal.rb
class Journal < ApplicationRecord
include Redmine::SafeAttributes
include Redmine::Reaction::Reactable
belongs_to :journalized, :polymorphic => true
# added as a quick fix to allow eager loading of the polymorphic association
app/models/message.rb
class Message < ApplicationRecord
include Redmine::SafeAttributes
include Redmine::Reaction::Reactable
belongs_to :board
belongs_to :author, :class_name => 'User'
acts_as_tree :counter_cache => :replies_count, :order => "#{Message.table_name}.created_on ASC"
app/models/news.rb
class News < ApplicationRecord
include Redmine::SafeAttributes
include Redmine::Reaction::Reactable
belongs_to :project
belongs_to :author, :class_name => 'User'
has_many :comments, lambda {order("created_on")}, :as => :commented, :dependent => :delete_all
app/models/reaction.rb
# frozen_string_literal: true
# Redmine - project management software
# Copyright (C) 2006- 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.
class Reaction < ApplicationRecord
belongs_to :reactable, polymorphic: true
belongs_to :user
validates :reactable_type, inclusion: { in: Redmine::Reaction::REACTABLE_TYPES }
scope :by, ->(user) { where(user: user) }
# Returns a mapping of reactable IDs to an array of user names
#
# Returns:
# {
# 1 => ["Alice", "Bob"],
# 2 => ["Charlie"],
# ...
# }
def self.users_map_for_reactables(reactable_type, reactable_ids)
reactions = preload(:user)
.select(:reactable_id, :user_id)
.where(reactable_type: reactable_type, reactable_id: reactable_ids)
.order(id: :desc)
reactable_user_pairs = reactions.map do |reaction|
[reaction.reactable_id, reaction.user.name]
end
# Group by reactable_id and transform values to extract only user name
# [[1, "Alice"], [1, "Bob"], [2, "Charlie"], ...]
# =>
# { 1 => ["Alice", "Bob"], 2 => ["Charlie"], ...}
reactable_user_pairs
.group_by(&:first)
.transform_values { |pairs| pairs.map(&:last) }
end
end
app/models/user.rb
has_one :api_token, lambda {where "#{table.name}.action='api'"}, :class_name => 'Token'
has_one :email_address, lambda {where :is_default => true}, :autosave => true
has_many :email_addresses, :dependent => :delete_all
has_many :reactions, dependent: :delete_all
belongs_to :auth_source
scope :logged, lambda {where("#{User.table_name}.status <> #{STATUS_ANONYMOUS}")}
app/views/issues/show.html.erb
<div class="subject">
<%= render_issue_subject_with_tree(@issue) %>
</div>
<div class="reaction">
<%= reaction_button @issue %>
</div>
<p class="author">
<%= authoring @issue.created_on, @issue.author %>.
app/views/messages/show.html.erb
<h2><%= avatar(@topic.author) %><%= @topic.subject %></h2>
<div class="message">
<div class="reaction">
<%= reaction_button @topic %>
</div>
<p><span class="author"><%= authoring @topic.created_on, @topic.author %></span></p>
<div id="message_topic_wiki" class="wiki">
<%= textilizable(@topic, :content) %>
......
<% @replies.each do |message| %>
<div class="message reply" id="<%= "message-#{message.id}" %>">
<div class="contextual">
<%= reaction_button message %>
<%= quote_reply(
url_for(:action => 'quote', :id => message, :format => 'js'),
"#message-#{message.id} .wiki",
app/views/news/show.html.erb
</div>
<% end %>
<p><% unless @news.summary.blank? %><em><%= @news.summary %></em><br /><% end %>
<span class="author"><%= authoring @news.created_on, @news.author %></span></p>
<div class="wiki">
<%= textilizable(@news, :description) %>
<div class="news">
<div class="reaction">
<%= reaction_button @news %>
</div>
<p><% unless @news.summary.blank? %><em><%= @news.summary %></em><br /><% end %>
<span class="author"><%= authoring @news.created_on, @news.author %></span></p>
<div class="wiki">
<%= textilizable(@news, :description) %>
</div>
<%= link_to_attachments @news %>
</div>
<%= link_to_attachments @news %>
<br />
<div id="comments" style="margin-bottom:16px;">
......
<% @comments.each do |comment| %>
<% next if comment.new_record? %>
<div class="contextual">
<%= reaction_button comment %>
<%= link_to_if_authorized sprite_icon('del', l(:button_delete)), { :controller => 'comments', :action => 'destroy', :id => @news, :comment_id => comment},
:data => {:confirm => l(:text_are_you_sure)}, :method => :delete,
:title => l(:button_delete),
app/views/reactions/_replace_button.js.erb
(() => {
const button = $('[data-reaction-button-id=<%= reaction_id_for @object %>]');
removeHoverTooltips(button);
button.html($('<%=j reaction_button @object, @reaction %>').children());
setupHoverTooltips(button);
})();
app/views/reactions/create.js.erb
<%= render 'replace_button' %>
app/views/reactions/destroy.js.erb
<%= render 'replace_button' %>
app/views/settings/_general.html.erb
<p><%= setting_text_field :feeds_limit, :size => 6 %></p>
<p>
<%= setting_check_box :reactions_enabled %>
<em class="info">
<%= l(:reaction_text_enabling_reactions) %>
</em>
</p>
<%= call_hook(:view_settings_general_form) %>
</div>
config/icon_source.yml
- name: unwatch
svg: eye-off
- name: copy-pre-content
svg: clipboard
svg: clipboard
- name: thumb-up
svg: thumb-up
- name: thumb-up-filled
svg: thumb-up
style: filled
config/locales/en.yml
setting_twofa: Two-factor authentication
setting_related_issues_default_columns: Related and sub issues list defaults
setting_display_related_issues_table_headers: Show table headers
setting_reactions_enabled: Enable reactions
permission_add_project: Create project
permission_add_subprojects: Create subprojects
......
text_project_destroy_enter_identifier: "To confirm, please enter the project's identifier (%{identifier}) below."
field_name_or_email_or_login: Name, email or login
setting_wiki_tablesort_enabled: Javascript based table sorting in wiki content
reaction_text_enabling_reactions: "This enables reactions in issues, forums, and news."
reaction_text_x_other_users:
one: "1 other"
other: "%{count} others"
config/locales/ja.yml
setting_related_issues_default_columns: 関連するチケットと子チケットの一覧で表示する項目
setting_display_related_issues_table_headers: テーブルヘッダを表示
error_can_not_remove_role_reason_members_html: "<p>以下のプロジェクトにこのロールのメンバーがいます:<br>%{projects}</p>"
setting_reactions_enabled: リアクション機能を有効にする
reaction_text_enabling_reactions: "チケット、フォーラム、ニュースでリアクション機能を有効にします。"
reaction_text_x_other_users:
one: 他1人
other: "他%{count}人"
config/routes.rb
end
end
resources :reactions, only: [:create, :destroy]
get '/projects/:project_id/issues/gantt', :to => 'gantts#show', :as => 'project_gantt'
get '/issues/gantt', :to => 'gantts#show'
config/settings.yml
default: 1
wiki_tablesort_enabled:
default: 1
reactions_enabled:
default: 1
db/migrate/20250423065135_create_reactions.rb
class CreateReactions < ActiveRecord::Migration[7.2]
def change
create_table :reactions do |t|
t.references :reactable, polymorphic: true, null: false
t.references :user, null: false
t.timestamps null: false
end
add_index :reactions, [:reactable_type, :reactable_id, :user_id], unique: true
add_index :reactions, [:reactable_type, :reactable_id, :id]
end
end
lib/redmine/reaction.rb
# frozen_string_literal: true
# Redmine - project management software
# Copyright (C) 2006- 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.
module Redmine
module Reaction
# Types of objects that can have reactions
REACTABLE_TYPES = %w(Journal Issue Message News Comment)
# Maximum number of users to display in the reaction button tooltip
DISPLAY_REACTION_USERS_LIMIT = 10
module Reactable
extend ActiveSupport::Concern
included do
has_many :reactions, -> { order(id: :desc) }, as: :reactable, dependent: :delete_all
has_many :reaction_users, through: :reactions, source: :user
attr_writer :reaction_user_names, :reaction_count
end
class_methods do
def load_with_reactions
objects = all.to_a
return objects unless Setting.reactions_enabled?
object_users_map = ::Reaction.users_map_for_reactables(self.name, objects.map(&:id))
objects.each do |object|
all_user_names = object_users_map[object.id] || []
object.reaction_count = all_user_names.size
object.reaction_user_names = all_user_names.take(DISPLAY_REACTION_USERS_LIMIT)
end
objects
end
end
def reaction_user_names
@reaction_user_names || reaction_users.take(DISPLAY_REACTION_USERS_LIMIT).map(&:name)
end
def reaction_count
@reaction_count || reaction_users.size
end
def reaction_by(user)
if reactions.loaded?
reactions.find { _1.user_id == user.id }
else
reactions.find_by(user: user)
end
end
end
end
end
test/fixtures/reactions.yml
---
reaction_001:
id: 1
reactable_type: Issue
reactable_id: 1
user_id: 1
reaction_002:
id: 2
reactable_type: Issue
reactable_id: 1
user_id: 2
reaction_003:
id: 3
reactable_type: Issue
reactable_id: 1
user_id: 3
reaction_004:
id: 4
reactable_type: Journal
reactable_id: 1
user_id: 2
reaction_005:
id: 5
reactable_type: Issue
reactable_id: 6
user_id: 2
reaction_006:
id: 6
reactable_type: Journal
reactable_id: 4
user_id: 2
reaction_007:
id: 7
reactable_type: News
reactable_id: 1
user_id: 1
reaction_008:
id: 8
reactable_type: Comment
reactable_id: 1
user_id: 2
reaction_009:
id: 9
reactable_type: Message
reactable_id: 7
user_id: 2
reaction_010:
id: 10
reactable_type: News
reactable_id: 3
user_id: 2
test/functional/issues_controller_test.rb
assert_select 'span.badge.badge-private', text: 'Private'
end
def test_show_should_display_reactions
@request.session[:user_id] = nil
get :show, params: { id: 1 }
assert_response :success
assert_select 'span[data-reaction-button-id=reaction_issue_1] span.reaction-button' do
assert_select 'span.icon-label', '3'
end
assert_select 'span[data-reaction-button-id=reaction_journal_1] span.reaction-button'
assert_select 'span[data-reaction-button-id=reaction_journal_2] span.reaction-button'
# Should not display reactions when reactions feature is disabled.
Setting.reactions_enabled = false
get :show, params: { id: 1 }
assert_response :success
assert_select 'span[data-reaction-button-id]', false
end
def test_show_should_not_display_edit_attachment_icon_for_user_without_edit_issue_permission_on_tracker
role = Role.find(2)
role.set_permission_trackers 'edit_issues', [2, 3]
test/functional/messages_controller_test.rb
assert_select 'h3', {text: /Watchers \(\d*\)/, count: 0}
end
def test_show_should_display_reactions
@request.session[:user_id] = 2
get :show, params: { board_id: 1, id: 4 }
assert_response :success
assert_select 'span[data-reaction-button-id=reaction_message_4] a.reaction-button' do
assert_select 'svg use[href*=thumb-up]'
end
assert_select 'span[data-reaction-button-id=reaction_message_5] a.reaction-button'
assert_select 'span[data-reaction-button-id=reaction_message_6] a.reaction-button'
# Should not display reactions when reactions feature is disabled.
Setting.reactions_enabled = false
get :show, params: { board_id: 1, id: 4 }
assert_response :success
assert_select 'span[data-reaction-button-id]', false
end
def test_get_new
@request.session[:user_id] = 2
get(:new, :params => {:board_id => 1})
test/functional/news_controller_test.rb
assert_response :not_found
end
def test_show_should_display_reactions
@request.session[:user_id] = 1
get :show, params: { id: 1 }
assert_response :success
assert_select 'span[data-reaction-button-id=reaction_news_1] a.reaction-button.reacted'
assert_select 'span[data-reaction-button-id=reaction_comment_1] a.reaction-button'
# Should not display reactions when reactions feature is disabled.
Setting.reactions_enabled = false
get :show, params: { id: 1 }
assert_response :success
assert_select 'span[data-reaction-button-id]', false
end
def test_get_new_with_project_id
@request.session[:user_id] = 2
get(:new, :params => {:project_id => 1})
test/functional/reactions_controller_test.rb
# frozen_string_literal: true
# Redmine - project management software
# Copyright (C) 2006- 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_relative '../test_helper'
class ReactionsControllerTest < Redmine::ControllerTest
def setup
Setting.reactions_enabled = '1'
# jsmith
@request.session[:user_id] = users(:users_002).id
end
test 'create for issue' do
issue = issues(:issues_002)
assert_difference(
->{ Reaction.count } => 1,
->{ issue.reactions.by(users(:users_002)).count } => 1
) do
post :create, params: {
object_type: 'Issue',
object_id: issue.id
}, xhr: true
end
assert_response :success
end
test 'create for journal' do
journal = journals(:journals_005)
assert_difference(
->{ Reaction.count } => 1,
->{ journal.reactions.by(users(:users_002)).count } => 1
) do
post :create, params: {
object_type: 'Journal',
object_id: journal.id
}, xhr: true
end
assert_response :success
end
test 'create for news' do
news = news(:news_002)
assert_difference(
->{ Reaction.count } => 1,
->{ news.reactions.by(users(:users_002)).count } => 1
) do
post :create, params: {
object_type: 'News',
object_id: news.id
}, xhr: true
end
assert_response :success
end
test 'create reaction for comment' do
comment = comments(:comments_002)
assert_difference(
->{ Reaction.count } => 1,
->{ comment.reactions.by(users(:users_002)).count } => 1
) do
post :create, params: {
object_type: 'Comment',
object_id: comment.id
}, xhr: true
end
assert_response :success
end
test 'create for message' do
message = messages(:messages_001)
assert_difference(
->{ Reaction.count } => 1,
->{ message.reactions.by(users(:users_002)).count } => 1
) do
post :create, params: {
object_type: 'Message',
object_id: message.id
}, xhr: true
end
assert_response :success
end
test 'destroy for issue' do
reaction = reactions(:reaction_005)
assert_difference 'Reaction.count', -1 do
delete :destroy, params: {
id: reaction.id,
# Issue (id=6)
object_type: reaction.reactable_type,
object_id: reaction.reactable_id
}, xhr: true
end
assert_response :success
assert_not Reaction.exists?(reaction.id)
end
test 'destroy for journal' do
reaction = reactions(:reaction_006)
assert_difference 'Reaction.count', -1 do
delete :destroy, params: {
id: reaction.id,
object_type: reaction.reactable_type,
object_id: reaction.reactable_id
}, xhr: true
end
assert_response :success
assert_not Reaction.exists?(reaction.id)
end
test 'destroy for news' do
# For News(id=3)
reaction = reactions(:reaction_010)
assert_difference 'Reaction.count', -1 do
delete :destroy, params: {
id: reaction.id,
object_type: reaction.reactable_type,
object_id: reaction.reactable_id
}, xhr: true
end
assert_response :success
assert_not Reaction.exists?(reaction.id)
end
test 'destroy for comment' do
# For Comment(id=1)
reaction = reactions(:reaction_008)
assert_difference 'Reaction.count', -1 do
delete :destroy, params: {
id: reaction.id,
object_type: reaction.reactable_type,
object_id: reaction.reactable_id
}, xhr: true
end
assert_response :success
assert_not Reaction.exists?(reaction.id)
end
test 'destroy for message' do
reaction = reactions(:reaction_009)
assert_difference 'Reaction.count', -1 do
delete :destroy, params: {
id: reaction.id,
object_type: reaction.reactable_type,
object_id: reaction.reactable_id
}, xhr: true
end
assert_response :success
assert_not Reaction.exists?(reaction.id)
end
test 'create should respond with 403 when feature is disabled' do
Setting.reactions_enabled = '0'
# admin
@request.session[:user_id] = users(:users_001).id
assert_no_difference 'Reaction.count' do
post :create, params: {
object_type: 'Issue',
object_id: issues(:issues_002).id
}, xhr: true
end
assert_response :forbidden
end
test 'destroy should respond with 403 when feature is disabled' do
Setting.reactions_enabled = '0'
# admin
@request.session[:user_id] = users(:users_001).id
reaction = reactions(:reaction_001)
assert_no_difference 'Reaction.count' do
delete :destroy, params: {
id: reaction.id,
object_type: reaction.reactable_type,
object_id: reaction.reactable_id
}, xhr: true
end
assert_response :forbidden
end
test 'create by anonymous user should respond with 403' do
@request.session[:user_id] = nil
assert_no_difference 'Reaction.count' do
post :create, params: {
object_type: 'Issue',
# Issue(id=1) is an issue in a public project
object_id: issues(:issues_001).id
}, xhr: true
end
assert_response :unauthorized
end
test 'destroy by anonymous user should respond with 401' do
@request.session[:user_id] = nil
reaction = reactions(:reaction_002)
assert_no_difference 'Reaction.count' do
delete :destroy, params: {
id: reaction.id,
object_type: reaction.reactable_type,
object_id: reaction.reactable_id
}, xhr: true
end
assert_response :unauthorized
end
test 'create when reaction already exists should not create a new reaction and succeed' do
assert_no_difference 'Reaction.count' do
post :create, params: {
object_type: 'Comment',
# user(jsmith) has already reacted to Comment(id=1)
object_id: comments(:comments_001).id
}, xhr: true
end
assert_response :success
end
test 'destroy another user reaction should not destroy the reaction and succeed' do
# admin user's reaction
reaction = reactions(:reaction_001)
assert_no_difference 'Reaction.count' do
delete :destroy, params: {
id: reaction.id,
object_type: reaction.reactable_type,
object_id: reaction.reactable_id
}, xhr: true
end
assert_response :success
end
test 'destroy nonexistent reaction' do
# For Journal(id=4)
reaction = reactions(:reaction_006)
reaction.destroy!
assert_not Reaction.exists?(reaction.id)
assert_no_difference 'Reaction.count' do
delete :destroy, params: {
id: reaction.id,
object_type: reaction.reactable_type,
object_id: reaction.reactable_id
}, xhr: true
end
assert_response :success
end
test 'create with invalid object type should respond with 403' do
# admin
@request.session[:user_id] = users(:users_001).id
post :create, params: {
object_type: 'InvalidType',
object_id: 1
}, xhr: true
assert_response :forbidden
end
test 'create without permission to view should respond with 403' do
# dlopper
@request.session[:user_id] = users(:users_003).id
assert_no_difference 'Reaction.count' do
post :create, params: {
object_type: 'Issue',
# dlopper is not a member of the project where the issue (id=4) belongs.
object_id: issues(:issues_004).id
}, xhr: true
end
assert_response :forbidden
end
test 'destroy without permission to view should respond with 403' do
# dlopper
@request.session[:user_id] = users(:users_003).id
# For Issue(id=6)
reaction = reactions(:reaction_005)
assert_no_difference 'Reaction.count' do
delete :destroy, params: {
id: reaction.id,
object_type: reaction.reactable_type,
object_id: reaction.reactable_id
}, xhr: true
end
assert_response :forbidden
end
end
test/helpers/reactions_helper_test.rb
# frozen_string_literal: true
# Redmine - project management software
# Copyright (C) 2006- 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_relative '../test_helper'
class ReactionsHelperTest < ActionView::TestCase
include ReactionsHelper
setup do
User.current = users(:users_002)
Setting.reactions_enabled = '1'
end
test 'reaction_id_for generates a DOM id' do
assert_equal "reaction_issue_1", reaction_id_for(issues(:issues_001))
end
test 'reaction_button returns nil when feature is disabled' do
Setting.reactions_enabled = '0'
assert_nil reaction_button(issues(:issues_004))
end
test 'reaction_button returns nil when object not visible' do
User.current = users(:users_003)
assert_nil reaction_button(issues(:issues_004))
end
test 'reaction_button for anonymous users shows static icon' do
User.current = nil
result = reaction_button(journals(:journals_001))
assert_select_in result, 'span.reaction-button[title=?]', 'John Smith'
assert_select_in result, 'a.reaction-button', false
end
test 'reaction_button includes no tooltip when the object has no reactions' do
issue = issues(:issues_002) # Issue without reactions
result = reaction_button(issue)
assert_select_in result, 'span.reaction-button[title]', false
end
test 'reaction_button includes tooltip with all usernames when reactions are 10 or fewer' do
issue = issues(:issues_002)
reactions = build_reactions(10)
issue.reactions += reactions
result = with_locale 'en' do
reaction_button(issue)
end
# The tooltip should display usernames in order of newest reactions.
expected_tooltip = 'Bob9 Doe, Bob8 Doe, Bob7 Doe, Bob6 Doe, Bob5 Doe, ' \
'Bob4 Doe, Bob3 Doe, Bob2 Doe, Bob1 Doe, and Bob0 Doe'
assert_select_in result, 'a.reaction-button[title=?]', expected_tooltip
end
test 'reaction_button includes tooltip with 10 usernames and others count when reactions exceed 10' do
issue = issues(:issues_002)
reactions = build_reactions(11)
issue.reactions += reactions
result = with_locale 'en' do
reaction_button(issue)
end
expected_tooltip = 'Bob10 Doe, Bob9 Doe, Bob8 Doe, Bob7 Doe, Bob6 Doe, ' \
'Bob5 Doe, Bob4 Doe, Bob3 Doe, Bob2 Doe, Bob1 Doe, and 1 other'
assert_select_in result, 'a.reaction-button[title=?]', expected_tooltip
end
test 'reaction_button for reacted object' do
issue = issues(:issues_001)
reaction = issue.reactions.find_by(user: User.current)
result = with_locale('en') do
reaction_button(issue, reaction)
end
tooltip = 'Dave Lopper, John Smith, and Redmine Admin'
assert_select_in result, 'span[data-reaction-button-id=?]', 'reaction_issue_1' do
href = reaction_path(reaction, object_type: 'Issue', object_id: 1)
assert_select 'a.icon.reaction-button.reacted[href=?]', href do
assert_select 'use[href*=?]', 'thumb-up-filled'
assert_select 'span.icon-label', '3'
end
assert_select 'span.reaction-button', false
end
end
test 'reaction_button for non-reacted object' do
User.current = users(:users_004)
issue = issues(:issues_001)
reaction = issue.reactions.find_by(user: User.current)
result = with_locale('en') do
reaction_button(issue, reaction)
end
tooltip = 'Dave Lopper, John Smith, and Redmine Admin'
assert_select_in result, 'span[data-reaction-button-id=?]', 'reaction_issue_1' do
href = reactions_path(object_type: 'Issue', object_id: 1)
assert_select 'a.icon.reaction-button[href=?]', href do
assert_select 'use[href*=?]', 'thumb-up'
assert_select 'span.icon-label', '3'
end
assert_select 'span.reaction-button', false
end
end
private
def build_reactions(count)
Array.new(count) do |i|
Reaction.new(user: User.generate!(firstname: "Bob#{i}"))
end
end
end
test/system/reactions_test.rb
# frozen_string_literal: true
# Redmine - project management software
# Copyright (C) 2006- 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_relative '../application_system_test_case'
class ReactionsSystemTest < ApplicationSystemTestCase
def test_react_to_issue
log_user('jsmith', 'jsmith')
issue = issues(:issues_002)
with_settings(reactions_enabled: '1') do
visit '/issues/2'
reaction_button = find("div.issue.details [data-reaction-button-id=\"reaction_issue_#{issue.id}\"]")
assert_reaction_add_and_remove(reaction_button, issue)
end
end
def test_react_to_journal
log_user('jsmith', 'jsmith')
journal = journals(:journals_002)
with_settings(reactions_enabled: '1') do
visit '/issues/1'
reaction_button = find("[data-reaction-button-id=\"reaction_journal_#{journal.id}\"]")
assert_reaction_add_and_remove(reaction_button, journal.reload)
end
end
def test_react_to_forum_reply
log_user('jsmith', 'jsmith')
reply_message = messages(:messages_002) # reply to message_001
with_settings(reactions_enabled: '1') do
visit 'boards/1/topics/1'
reaction_button = find("[data-reaction-button-id=\"reaction_message_#{reply_message.id}\"]")
assert_reaction_add_and_remove(reaction_button, reply_message)
end
end
def test_react_to_forum_message
log_user('jsmith', 'jsmith')
message = messages(:messages_001)
with_settings(reactions_enabled: '1') do
visit 'boards/1/topics/1'
reaction_button = find("[data-reaction-button-id=\"reaction_message_#{message.id}\"]")
assert_reaction_add_and_remove(reaction_button, message)
end
end
def test_react_to_news
log_user('jsmith', 'jsmith')
with_settings(reactions_enabled: '1') do
visit '/news/2'
reaction_button = find("[data-reaction-button-id=\"reaction_news_2\"]")
assert_reaction_add_and_remove(reaction_button, news(:news_002))
end
end
def test_react_to_comment
log_user('jsmith', 'jsmith')
comment = comments(:comments_002)
with_settings(reactions_enabled: '1') do
visit '/news/1'
reaction_button = find("[data-reaction-button-id=\"reaction_comment_#{comment.id}\"]")
assert_reaction_add_and_remove(reaction_button, comment)
end
end
def test_reactions_disabled
log_user('jsmith', 'jsmith')
... This diff was truncated because it exceeds the maximum size that can be displayed.
(9-9/30)