From 6e0f6d2dc88a027c4e9e516e67790e8ce73f4f22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marius=20B=C4=82LTEANU?= Date: Sun, 14 Jun 2026 00:40:31 +0300 Subject: [PATCH 1/2] Refactor context menus controller to namespaced sub-controllers (#44169). --- .../context_menus/base_controller.rb | 44 ++ .../context_menus/issues_controller.rb | 92 +++ .../context_menus/projects_controller.rb | 43 ++ .../context_menus/time_entries_controller.rb | 65 ++ .../context_menus/users_controller.rb | 45 ++ app/controllers/context_menus_controller.rb | 149 ---- config/routes.rb | 8 +- .../context_menus/issues_controller.rb | 448 +++++++++++++ .../context_menus/projects_controller.rb | 50 ++ .../context_menus/time_entries_controller.rb | 120 ++++ .../context_menus/users_controller.rb | 90 +++ .../context_menus_controller_test.rb | 634 ------------------ 12 files changed, 1001 insertions(+), 787 deletions(-) create mode 100644 app/controllers/context_menus/base_controller.rb create mode 100644 app/controllers/context_menus/issues_controller.rb create mode 100644 app/controllers/context_menus/projects_controller.rb create mode 100644 app/controllers/context_menus/time_entries_controller.rb create mode 100644 app/controllers/context_menus/users_controller.rb delete mode 100644 app/controllers/context_menus_controller.rb create mode 100644 test/functional/context_menus/issues_controller.rb create mode 100644 test/functional/context_menus/projects_controller.rb create mode 100644 test/functional/context_menus/time_entries_controller.rb create mode 100644 test/functional/context_menus/users_controller.rb delete mode 100644 test/functional/context_menus_controller_test.rb diff --git a/app/controllers/context_menus/base_controller.rb b/app/controllers/context_menus/base_controller.rb new file mode 100644 index 000000000..038e48a43 --- /dev/null +++ b/app/controllers/context_menus/base_controller.rb @@ -0,0 +1,44 @@ +# 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 ContextMenus + class BaseController < ApplicationController + layout false + helper :context_menus + helper_method :url_for + + def url_for(options = nil) + if options.is_a?(Hash) && options[:controller].present? + controller_name = options[:controller].to_s + unless controller_name.start_with?('/') + options = options.dup + options[:controller] = "/#{controller_name}" + end + end + super + end + + private + + def render_context_menu(template_name) + @back = back_url + render :template => "context_menus/#{template_name}" + end + end +end diff --git a/app/controllers/context_menus/issues_controller.rb b/app/controllers/context_menus/issues_controller.rb new file mode 100644 index 000000000..f08e7b7a1 --- /dev/null +++ b/app/controllers/context_menus/issues_controller.rb @@ -0,0 +1,92 @@ +# 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 ContextMenus + class IssuesController < BaseController + helper :watchers + helper :issues + + before_action :find_issues, :only => :index + + def index + issues + render_context_menu 'issues' + end + + private + + def issues + if @issues.size == 1 + @issue = @issues.first + end + @issue_ids = @issues.map(&:id).sort + + @allowed_statuses = @issues.map(&:new_statuses_allowed_to).reduce(:&) + + @can = { + :edit => @issues.all?(&:attributes_editable?), + :log_time => @issue&.time_loggable?, + :copy => User.current.allowed_to?(:copy_issues, @projects) && Issue.allowed_target_projects.any?, + :add_watchers => User.current.allowed_to?(:add_issue_watchers, @projects), + :delete => @issues.all?(&:deletable?), + :add_subtask => @issue && !@issue.closed? && User.current.allowed_to?(:manage_subtasks, @project) + } + + @assignables = @issues.map(&:assignable_users).reduce(:&) + @trackers = @projects.map {|p| Issue.allowed_target_trackers(p)}.reduce(:&) + @versions = @projects.map {|p| p.shared_versions.open}.reduce(:&) + + @priorities = IssuePriority.active.reverse + @back = back_url + begin + # Recognize the controller and action from the back_url to determine + # which view triggered the context menu. + if relative_url_root.present? && back_url&.starts_with?(relative_url_root) + normalized_back_url = back_url.delete_prefix(relative_url_root) + else + normalized_back_url = back_url + end + route = Rails.application.routes.recognize_path(normalized_back_url) + @include_delete = + [ + {controller: 'issues', action: 'index'}, + {controller: 'gantts', action: 'show'}, + {controller: 'calendars', action: 'show'} + ].any?(route.slice(:controller, :action)) + rescue ActionController::RoutingError + @include_delete = false + end + + @columns = params[:c] + + @options_by_custom_field = {} + if @can[:edit] + custom_fields = @issues.map(&:editable_custom_fields).reduce(:&).reject(&:multiple?).select {|field| field.format.bulk_edit_supported} + custom_fields.each do |field| + values = field.possible_values_options(@projects) + if values.present? + @options_by_custom_field[field] = values + end + end + end + + @safe_attributes = @issues.map(&:safe_attribute_names).reduce(:&) + end + end +end diff --git a/app/controllers/context_menus/projects_controller.rb b/app/controllers/context_menus/projects_controller.rb new file mode 100644 index 000000000..c39bf586f --- /dev/null +++ b/app/controllers/context_menus/projects_controller.rb @@ -0,0 +1,43 @@ +# 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 ContextMenus + class ProjectsController < BaseController + before_action :require_admin + before_action :find_projects + + def index + render_context_menu 'projects' + end + + private + + def find_projects + @projects = Project.where(id: params[:ids]).to_a + if @projects.empty? + render_404 + return + end + + if @projects.size == 1 + @project = @projects.first + end + end + end +end diff --git a/app/controllers/context_menus/time_entries_controller.rb b/app/controllers/context_menus/time_entries_controller.rb new file mode 100644 index 000000000..3e0fd1477 --- /dev/null +++ b/app/controllers/context_menus/time_entries_controller.rb @@ -0,0 +1,65 @@ +# 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 ContextMenus + class TimeEntriesController < BaseController + before_action :find_time_entries + + def index + @activities = @projects.map(&:activities).reduce(:&) + + edit_allowed = @time_entries.all? {|t| t.editable_by?(User.current)} + @can = {:edit => edit_allowed, :delete => edit_allowed} + @back = back_url + + @options_by_custom_field = {} + if @can[:edit] + custom_fields = @time_entries.map(&:editable_custom_fields).reduce(:&).reject(&:multiple?).select {|field| field.format.bulk_edit_supported} + custom_fields.each do |field| + values = field.possible_values_options(@projects) + if values.present? + @options_by_custom_field[field] = values + end + end + end + + render_context_menu 'time_entries' + end + + private + + def find_time_entries + @time_entries = TimeEntry.where(:id => params[:ids]). + preload(:project => :time_entry_activities). + preload(:user).to_a + + if @time_entries.blank? || !@time_entries.all?(&:visible?) + render_404; + return + end + + if @time_entries.size == 1 + @time_entry = @time_entries.first + end + + @projects = @time_entries.filter_map(&:project).uniq + @project = @projects.first if @projects.size == 1 + end + end +end diff --git a/app/controllers/context_menus/users_controller.rb b/app/controllers/context_menus/users_controller.rb new file mode 100644 index 000000000..2ca73496b --- /dev/null +++ b/app/controllers/context_menus/users_controller.rb @@ -0,0 +1,45 @@ +# 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 ContextMenus + class UsersController < BaseController + before_action :require_admin + before_action :find_users + + def index + @groups = Group.givable.sorted.to_a + @common_group_ids = Group.givable.joins(:groups_users).where(groups_users: { user_id: @users.map(&:id) }).distinct.pluck(:id).to_set + + render_context_menu 'users' + end + + private + + def find_users + @users = User.where(id: params[:id] || params[:ids]).to_a + raise ActiveRecord::RecordNotFound if @users.empty? + + if @users.size == 1 + @user = @users.first + end + rescue ActiveRecord::RecordNotFound + render_404 + end + end +end diff --git a/app/controllers/context_menus_controller.rb b/app/controllers/context_menus_controller.rb deleted file mode 100644 index 9932d5e6d..000000000 --- a/app/controllers/context_menus_controller.rb +++ /dev/null @@ -1,149 +0,0 @@ -# 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 ContextMenusController < ApplicationController - helper :watchers - helper :issues - - before_action :find_issues, :only => :issues - before_action :require_admin, :only => [:projects, :users] - - def issues - if @issues.size == 1 - @issue = @issues.first - end - @issue_ids = @issues.map(&:id).sort - - @allowed_statuses = @issues.map(&:new_statuses_allowed_to).reduce(:&) - - @can = { - :edit => @issues.all?(&:attributes_editable?), - :log_time => @issue&.time_loggable?, - :copy => User.current.allowed_to?(:copy_issues, @projects) && Issue.allowed_target_projects.any?, - :add_watchers => User.current.allowed_to?(:add_issue_watchers, @projects), - :delete => @issues.all?(&:deletable?), - :add_subtask => @issue && !@issue.closed? && User.current.allowed_to?(:manage_subtasks, @project) - } - - @assignables = @issues.map(&:assignable_users).reduce(:&) - @trackers = @projects.map {|p| Issue.allowed_target_trackers(p)}.reduce(:&) - @versions = @projects.map {|p| p.shared_versions.open}.reduce(:&) - - @priorities = IssuePriority.active.reverse - @back = back_url - begin - # Recognize the controller and action from the back_url to determine - # which view triggered the context menu. - if relative_url_root.present? && back_url&.starts_with?(relative_url_root) - normalized_back_url = back_url.delete_prefix(relative_url_root) - else - normalized_back_url = back_url - end - route = Rails.application.routes.recognize_path(normalized_back_url) - @include_delete = - [ - {controller: 'issues', action: 'index'}, - {controller: 'gantts', action: 'show'}, - {controller: 'calendars', action: 'show'} - ].any?(route.slice(:controller, :action)) - rescue ActionController::RoutingError - @include_delete = false - end - - @columns = params[:c] - - @options_by_custom_field = {} - if @can[:edit] - custom_fields = @issues.map(&:editable_custom_fields).reduce(:&).reject(&:multiple?).select {|field| field.format.bulk_edit_supported} - custom_fields.each do |field| - values = field.possible_values_options(@projects) - if values.present? - @options_by_custom_field[field] = values - end - end - end - - @safe_attributes = @issues.map(&:safe_attribute_names).reduce(:&) - render :layout => false - end - - def time_entries - @time_entries = TimeEntry.where(:id => params[:ids]). - preload(:project => :time_entry_activities). - preload(:user).to_a - - if @time_entries.blank? || !@time_entries.all?(&:visible?) - render_404; - return - end - - if @time_entries.size == 1 - @time_entry = @time_entries.first - end - - @projects = @time_entries.filter_map(&:project).uniq - @project = @projects.first if @projects.size == 1 - @activities = @projects.map(&:activities).reduce(:&) - - edit_allowed = @time_entries.all? {|t| t.editable_by?(User.current)} - @can = {:edit => edit_allowed, :delete => edit_allowed} - @back = back_url - - @options_by_custom_field = {} - if @can[:edit] - custom_fields = @time_entries.map(&:editable_custom_fields).reduce(:&).reject(&:multiple?).select {|field| field.format.bulk_edit_supported} - custom_fields.each do |field| - values = field.possible_values_options(@projects) - if values.present? - @options_by_custom_field[field] = values - end - end - end - - render :layout => false - end - - def projects - @projects = Project.where(id: params[:ids]).to_a - if @projects.empty? - render_404 - return - end - - if @projects.size == 1 - @project = @projects.first - end - render layout: false - end - - def users - @users = User.where(id: params[:ids]).to_a - - (render_404; return) unless @users.present? - if @users.size == 1 - @user = @users.first - end - - @groups = Group.givable.sorted.to_a - @common_group_ids = Group.givable.joins(:groups_users).where(groups_users: { user_id: @users.map(&:id) }).distinct.pluck(:id).to_set - @back = back_url - - render layout: false - end -end diff --git a/config/routes.rb b/config/routes.rb index 9edb2f855..ce15da420 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -58,7 +58,7 @@ Rails.application.routes.draw do match '/wiki_pages/auto_complete', :to => 'auto_completes#wiki_pages', :via => :get, :as => 'auto_complete_wiki_pages' # Misc issue routes. TODO: move into resources - match '/issues/context_menu', :to => 'context_menus#issues', :as => 'issues_context_menu', :via => [:get, :post] + match '/issues/context_menu', :to => 'context_menus/issues#index', :as => 'issues_context_menu', :via => [:get, :post] match '/issues/changes', :to => 'journals#index', :as => 'issue_changes', :via => :get match '/issues/:id/quoted', :to => 'journals#new', :id => /\d+/, :via => :post, :as => 'quoted_issue' @@ -117,7 +117,7 @@ Rails.application.routes.draw do match 'my/twofa/backup_codes', :controller => 'twofa_backup_codes', :action => 'show', :via => [:get] match 'users/:user_id/twofa/deactivate', :controller => 'twofa', :action => 'admin_deactivate', :via => :post - match '/users/context_menu', to: 'context_menus#users', as: :users_context_menu, via: [:get, :post] + match '/users/context_menu', to: 'context_menus/users#index', as: :users_context_menu, via: [:get, :post] resources :users do collection do delete 'bulk_destroy' @@ -258,7 +258,7 @@ Rails.application.routes.draw do post 'add_attachment', :on => :member end - match '/time_entries/context_menu', :to => 'context_menus#time_entries', :as => :time_entries_context_menu, :via => [:get, :post] + match '/time_entries/context_menu', :to => 'context_menus/time_entries#index', :as => :time_entries_context_menu, :via => [:get, :post] resources :time_entries, :controller => 'timelog', :except => :destroy do member do @@ -384,7 +384,7 @@ Rails.application.routes.draw do post 'admin/test_email', :to => 'admin#test_email', :as => 'test_email' post 'admin/default_configuration', :to => 'admin#default_configuration' - match '/admin/projects_context_menu', :to => 'context_menus#projects', :as => 'projects_context_menu', :via => [:get, :post] + match '/admin/projects_context_menu', :to => 'context_menus/projects#index', :as => 'projects_context_menu', :via => [:get, :post] resources :auth_sources do member do diff --git a/test/functional/context_menus/issues_controller.rb b/test/functional/context_menus/issues_controller.rb new file mode 100644 index 000000000..786ebef8c --- /dev/null +++ b/test/functional/context_menus/issues_controller.rb @@ -0,0 +1,448 @@ +# 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' + +module ContextMenus + class IssuesControllerTest < Redmine::ControllerTest + def test_context_menu_one_issue_should_link_to_issue_path + @request.session[:user_id] = 2 + get( + :index, + :params => { + :ids => [1], + :back_url => '/issues' + } + ) + assert_response :success + + assert_select 'a.icon-edit[href=?]', '/issues/1/edit', :text => 'Edit' + assert_select 'a.icon-copy-link[data-clipboard-text=?]', 'http://test.host/issues/1', :text => 'Copy link' + assert_select 'a.icon-copy[href=?]', '/projects/ecookbook/issues/1/copy', :text => 'Copy' + assert_select 'a.icon-del[href*=?]', 'ids%5B%5D=1', :text => 'Delete issue' + + # Statuses + assert_select 'a[href*=?][data-method="patch"]', 'issue%5Bstatus_id%5D=5', :text => 'Closed' + assert_select 'a[href*=?][data-method="patch"]', 'issue%5Bpriority_id%5D=8', :text => 'Immediate' + # No inactive priorities + assert_select 'a', :text => /Inactive Priority/, :count => 0 + # Versions + assert_select 'a[href*=?][data-method="patch"]', 'issue%5Bfixed_version_id%5D=3', :text => '2.0' + assert_select 'a[href*=?][data-method="patch"]', 'issue%5Bfixed_version_id%5D=4', :text => 'eCookbook Subproject 1 - 2.0' + # Assignees + assert_select 'a[href*=?][data-method="patch"]', 'issue%5Bassigned_to_id%5D=3', :text => 'Dave Lopper' + end + + def test_context_menu_multiple_issues_should_link_to_bulk_update_issues_path + @request.session[:user_id] = 2 + get :index, :params => { + :ids => [1, 2], + :back_url => '/projects/ecookbook/issues' + } + assert_response :success + + assert_select 'a.icon-edit[href=?]', '/issues/bulk_edit?ids%5B%5D=1&ids%5B%5D=2', :text => 'Bulk edit' + assert_select 'a.icon-copy[href=?]', '/issues/bulk_edit?copy=1&ids%5B%5D=1&ids%5B%5D=2', :text => 'Copy' + assert_select 'a.icon-del[href*=?]', 'ids%5B%5D=1&ids%5B%5D=2', :text => 'Delete issues' + + # Statuses + assert_select 'a[href*=?][data-method="patch"]', 'issue%5Bstatus_id%5D=5', :text => 'Closed' + assert_select 'a[href*=?][data-method="patch"]', 'issue%5Bpriority_id%5D=8', :text => 'Immediate' + # No inactive priorities + assert_select 'a', :text => /Inactive Priority/, :count => 0 + # Versions + assert_select 'a[href*=?][data-method="patch"]', 'issue%5Bfixed_version_id%5D=3', :text => '2.0' + assert_select 'a[href*=?][data-method="patch"]', 'issue%5Bfixed_version_id%5D=4', :text => 'eCookbook Subproject 1 - 2.0' + # Assignees + assert_select 'a[href*=?][data-method="patch"]', 'issue%5Bassigned_to_id%5D=3', :text => 'Dave Lopper' + end + + def test_context_menu_one_issue_by_anonymous + with_settings :default_language => 'en' do + get( + :index, + :params => { + :ids => [1], + :back_url => '/issues' + } + ) + assert_response :success + + assert_select 'a.icon-del.disabled[href="#"]', :text => 'Delete issue' + end + end + + def test_context_menu_multiple_issues_of_same_project + @request.session[:user_id] = 2 + get( + :index, + :params => { + :ids => [1, 2], + :back_url => '/issues' + } + ) + assert_response :success + + ids = [1, 2].map {|i| "ids%5B%5D=#{i}"}.join('&') + + assert_select 'a.icon-edit[href=?]', "/issues/bulk_edit?#{ids}", :text => 'Bulk edit' + # issue_id: '1,2', set_filter: 1, status_id: '*' + assert_select 'a.icon-copy-link[data-clipboard-text=?]', "http://test.host/projects/ecookbook/issues?issue_id=1%2C2&set_filter=1&status_id=%2A", :text => 'Copy link' + assert_select 'a.icon-copy[href=?]', "/issues/bulk_edit?copy=1&#{ids}", :text => 'Copy' + assert_select 'a.icon-del[href*=?]', ids, :text => 'Delete issues' + + assert_select 'a[href*=?]', 'issue%5Bstatus_id%5D=5', :text => 'Closed' + assert_select 'a[href*=?]', 'issue%5Bpriority_id%5D=8', :text => 'Immediate' + assert_select 'a[href*=?]', 'issue%5Bassigned_to_id%5D=3', :text => 'Dave Lopper' + end + + def test_context_menu_multiple_issues_of_different_projects + @request.session[:user_id] = 2 + get( + :index, + :params => { + :ids => [1, 2, 6], + :back_url => '/issues' + } + ) + assert_response :success + + ids = [1, 2, 6].map {|i| "ids%5B%5D=#{i}"}.join('&') + + assert_select 'a.icon-edit[href=?]', "/issues/bulk_edit?#{ids}", :text => 'Bulk edit' + # issue_id: '1,2,6', set_filter: 1, status_id: '*' + assert_select 'a.icon-copy-link[data-clipboard-text=?]', "http://test.host/issues?issue_id=1%2C2%2C6&set_filter=1&status_id=%2A", :text => 'Copy link' + assert_select 'a.icon-del[href*=?]', ids, :text => 'Delete issues' + + assert_select 'a[href*=?]', 'issue%5Bstatus_id%5D=5', :text => 'Closed' + assert_select 'a[href*=?]', 'issue%5Bpriority_id%5D=8', :text => 'Immediate' + assert_select 'a[href*=?]', 'issue%5Bassigned_to_id%5D=2', :text => 'John Smith' + end + + def test_context_menu_should_include_list_custom_fields + field = IssueCustomField.create!(:name => 'List', :field_format => 'list', + :possible_values => ['Foo', 'Bar'], :is_for_all => true, :tracker_ids => [1, 2, 3]) + @request.session[:user_id] = 2 + get( + :index, + :params => { + :ids => [1] + } + ) + assert_select "li.cf_#{field.id}" do + assert_select 'a[href="#"]', :text => 'List' + assert_select 'ul' do + assert_select 'a', 3 + assert_select 'a[href=?]', "/issues/1?ids%5B%5D=1&issue%5Bcustom_field_values%5D%5B#{field.id}%5D=Foo", :text => 'Foo' + assert_select 'a[href=?]', "/issues/1?ids%5B%5D=1&issue%5Bcustom_field_values%5D%5B#{field.id}%5D=__none__", :text => 'none' + end + end + end + + def test_context_menu_multiple_issues_should_include_list_custom_fields + field = IssueCustomField.create!(:name => 'List', :field_format => 'list', + :possible_values => ['Foo', 'Bar'], :is_for_all => true, :tracker_ids => [1, 2, 3]) + @request.session[:user_id] = 2 + get( + :index, + :params => { + :ids => [1, 2] + } + ) + assert_select "li.cf_#{field.id}" do + assert_select 'a[href="#"]', :text => 'List' + assert_select 'ul' do + assert_select 'a', 3 + assert_select 'a[href=?]', "/issues/bulk_update?ids%5B%5D=1&ids%5B%5D=2&issue%5Bcustom_field_values%5D%5B#{field.id}%5D=Foo", :text => 'Foo' + assert_select 'a[href=?]', "/issues/bulk_update?ids%5B%5D=1&ids%5B%5D=2&issue%5Bcustom_field_values%5D%5B#{field.id}%5D=__none__", :text => 'none' + end + end + end + + def test_context_menu_should_not_include_null_value_for_required_custom_fields + field = IssueCustomField.create!(:name => 'List', :is_required => true, :field_format => 'list', + :possible_values => ['Foo', 'Bar'], :is_for_all => true, :tracker_ids => [1, 2, 3]) + @request.session[:user_id] = 2 + get( + :index, + :params => { + :ids => [1, 2] + } + ) + assert_select "li.cf_#{field.id}" do + assert_select 'a[href="#"]', :text => 'List' + assert_select 'ul' do + assert_select 'a', 2 + assert_select 'a', :text => 'none', :count => 0 + end + end + end + + def test_context_menu_on_single_issue_should_select_current_custom_field_value + field = IssueCustomField.create!(:name => 'List', :field_format => 'list', + :possible_values => ['Foo', 'Bar'], :is_for_all => true, :tracker_ids => [1, 2, 3]) + issue = Issue.find(1) + issue.custom_field_values = {field.id => 'Bar'} + issue.save! + @request.session[:user_id] = 2 + get( + :index, + :params => { + :ids => [1] + } + ) + assert_select "li.cf_#{field.id}" do + assert_select 'a[href="#"]', :text => 'List' + assert_select 'ul' do + assert_select 'a', 3 + assert_select 'a.icon', :text => 'Bar' + end + end + end + + def test_context_menu_should_include_bool_custom_fields + field = IssueCustomField.create!(:name => 'Bool', :field_format => 'bool', + :is_for_all => true, :tracker_ids => [1, 2, 3]) + @request.session[:user_id] = 2 + get( + :index, + :params => { + :ids => [1] + } + ) + assert_select "li.cf_#{field.id}" do + assert_select 'a[href="#"]', :text => 'Bool' + assert_select 'ul' do + assert_select 'a', 3 + assert_select 'a[href=?]', "/issues/1?ids%5B%5D=1&issue%5Bcustom_field_values%5D%5B#{field.id}%5D=0", :text => 'No' + assert_select 'a[href=?]', "/issues/1?ids%5B%5D=1&issue%5Bcustom_field_values%5D%5B#{field.id}%5D=1", :text => 'Yes' + assert_select 'a[href=?]', "/issues/1?ids%5B%5D=1&issue%5Bcustom_field_values%5D%5B#{field.id}%5D=__none__", :text => 'none' + end + end + end + + def test_context_menu_should_include_user_custom_fields + field = IssueCustomField.create!(:name => 'User', :field_format => 'user', + :is_for_all => true, :tracker_ids => [1, 2, 3]) + @request.session[:user_id] = 2 + get( + :index, + :params => { + :ids => [1] + } + ) + assert_select "li.cf_#{field.id}" do + assert_select 'a[href="#"]', :text => 'User' + assert_select 'ul' do + assert_select 'a', Project.find(1).members.count + 2 # users + 'none' + 'me' + assert_select 'a[href=?]', "/issues/1?ids%5B%5D=1&issue%5Bcustom_field_values%5D%5B#{field.id}%5D=2", :text => 'John Smith' + assert_select 'a[href=?]', "/issues/1?ids%5B%5D=1&issue%5Bcustom_field_values%5D%5B#{field.id}%5D=__none__", :text => 'none' + end + end + end + + def test_context_menu_should_include_version_custom_fields + field = IssueCustomField.create!(:name => 'Version', :field_format => 'version', :is_for_all => true, :tracker_ids => [1, 2, 3]) + @request.session[:user_id] = 2 + get( + :index, + :params => { + :ids => [1] + } + ) + assert_select "li.cf_#{field.id}" do + assert_select 'a[href="#"]', :text => 'Version' + assert_select 'ul' do + assert_select 'a', Project.find(1).shared_versions.count + 1 + assert_select 'a[href=?]', "/issues/1?ids%5B%5D=1&issue%5Bcustom_field_values%5D%5B#{field.id}%5D=3", :text => '2.0' + assert_select 'a[href=?]', "/issues/1?ids%5B%5D=1&issue%5Bcustom_field_values%5D%5B#{field.id}%5D=__none__", :text => 'none' + end + end + end + + def test_context_menu_should_show_enabled_custom_fields_for_the_role_only + enabled_cf = + IssueCustomField.generate!( + :field_format => 'bool', :is_for_all => true, + :tracker_ids => [1], :visible => false, :role_ids => [1, 2] + ) + disabled_cf = + IssueCustomField.generate!( + :field_format => 'bool', :is_for_all => true, + :tracker_ids => [1], :visible => false, :role_ids => [2] + ) + issue = Issue.generate!(:project_id => 1, :tracker_id => 1) + + @request.session[:user_id] = 2 + get( + :index, + :params => { + :ids => [issue.id] + } + ) + assert_select "li.cf_#{enabled_cf.id}" + assert_select "li.cf_#{disabled_cf.id}", 0 + end + + def test_context_menu_by_assignable_user_should_include_assigned_to_me_link + @request.session[:user_id] = 2 + get( + :index, + :params => { + :ids => [1, 2] + } + ) + assert_response :success + + assert_select 'a[href=?]', '/issues/bulk_update?ids%5B%5D=1&ids%5B%5D=2&issue%5Bassigned_to_id%5D=2', :text => / me / + end + + def test_context_menu_should_propose_shared_versions_for_issues_from_different_projects + @request.session[:user_id] = 2 + version = Version.create!(:name => 'Shared', :sharing => 'system', :project_id => 1) + + get( + :index, + :params => { + :ids => [1, 4] + } + ) + assert_response :success + + assert_select 'a', :text => 'eCookbook - Shared' + end + + def test_context_menu_should_respect_five_percent_increments + with_settings :issue_done_ratio => 'issue_field', :issue_done_ratio_interval => 5 do + @request.session[:user_id] = 2 + get( + :index, + :params => { + :ids => [1, 2] + } + ) + assert_response :success + + assert_select 'a[href*=?]', '/issues/bulk_update?ids%5B%5D=1&ids%5B%5D=2&issue%5Bdone_ratio%5D=0', :text => '0%' + assert_select 'a[href*=?]', '/issues/bulk_update?ids%5B%5D=1&ids%5B%5D=2&issue%5Bdone_ratio%5D=5', :text => '5%' + assert_select 'a[href*=?]', '/issues/bulk_update?ids%5B%5D=1&ids%5B%5D=2&issue%5Bdone_ratio%5D=10', :text => '10%' + assert_select 'a[href*=?]', '/issues/bulk_update?ids%5B%5D=1&ids%5B%5D=2&issue%5Bdone_ratio%5D=55', :text => '55%' + assert_select 'a[href*=?]', '/issues/bulk_update?ids%5B%5D=1&ids%5B%5D=2&issue%5Bdone_ratio%5D=100', :text => '100%' + end + end + + def test_context_menu_should_include_add_subtask_link + @request.session[:user_id] = 2 + get( + :index, + :params => { + :ids => [1] + } + ) + assert_response :success + + assert_select 'a.icon-add[href=?]', '/projects/ecookbook/issues/new?issue%5Bparent_issue_id%5D=1&issue%5Btracker_id%5D=1', :text => 'Add subtask' + end + + def test_context_menu_with_closed_issue_should_not_include_add_subtask_link + @request.session[:user_id] = 2 + get( + :index, + :params => { + :ids => [8] + } + ) + assert_response :success + + assert_select 'a.icon-add', :text => 'Add subtask', :count => 0 + end + + def test_context_menu_multiple_issues_should_not_include_add_subtask_link + @request.session[:user_id] = 2 + get( + :index, + :params => { + :ids => [1, 2] + } + ) + assert_response :success + + assert_select 'a.icon-add', :text => 'Add subtask', :count => 0 + end + + def test_context_menu_with_issue_that_is_not_visible_should_fail + get( + :index, + :params => { + :ids => [1, 4] # issue 4 is not visible + } + ) + assert_response :found + end + + def test_should_respond_with_404_without_ids + get :index + assert_response :not_found + end + + def test_context_menu_should_include_delete_for_allowed_back_urls + @request.session[:user_id] = 2 + %w[ + /issues + /projects/ecookbook/issues/gantt + /projects/ecookbook/issues/calendar + ].each do |back_url| + get :index, :params => { :ids => [1], :back_url => back_url } + assert_response :success + assert_select 'a.icon-del', :text => /Delete/ + end + end + + def test_context_menu_with_suburi_should_include_delete_for_allowed_back_urls + @relative_url_root = Redmine::Utils.relative_url_root + Redmine::Utils.relative_url_root = '/redmine' + + @request.session[:user_id] = 2 + %w[ + /redmine/issues + /redmine/projects/ecookbook/issues/gantt + /redmine/projects/ecookbook/issues/calendar + ].each do |back_url| + get :index, :params => { :ids => [1], :back_url => back_url } + assert_response :success + assert_select 'a.icon-del', :text => /Delete/ + end + ensure + Redmine::Utils.relative_url_root = @relative_url_root + end + + def test_context_menu_should_not_include_delete_for_disallowed_back_urls + @request.session[:user_id] = 2 + %w[ + /issues/1 + /projects/ecookbook/roadmap + /not/a/real/path + ].each do |back_url| + get :index, :params => { :ids => [1], :back_url => back_url } + assert_response :success + assert_select 'a.icon-del', :count => 0 + end + end + end +end diff --git a/test/functional/context_menus/projects_controller.rb b/test/functional/context_menus/projects_controller.rb new file mode 100644 index 000000000..38d51a93e --- /dev/null +++ b/test/functional/context_menus/projects_controller.rb @@ -0,0 +1,50 @@ +# 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' + +module ContextMenus + class ProjectsControllerTest < Redmine::ControllerTest + def test_index_admin_user + @request.session[:user_id] = 1 + + get( + :index, + :params => { + :ids => [1, 2] + } + ) + + assert_response :success + end + + def test_index_not_admin_user + @request.session[:user_id] = 2 + + get( + :index, + :params => { + :ids => [1, 2] + } + ) + + assert_response :forbidden + end + end +end diff --git a/test/functional/context_menus/time_entries_controller.rb b/test/functional/context_menus/time_entries_controller.rb new file mode 100644 index 000000000..9df0e34d5 --- /dev/null +++ b/test/functional/context_menus/time_entries_controller.rb @@ -0,0 +1,120 @@ +# 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' + +module ContextMenus + class TimeEntriesControllerTest < Redmine::ControllerTest + def test_context_menu_for_one_time_entry + @request.session[:user_id] = 2 + get( + :index, + :params => { + :ids => [1] + } + ) + assert_response :success + + assert_select 'a:not(.disabled)', :text => 'Edit' + end + + def test_time_entries_context_menu + @request.session[:user_id] = 2 + get( + :index, + :params => { + :ids => [1, 2] + } + ) + assert_response :success + + assert_select 'a:not(.disabled)', :text => 'Bulk edit' + end + + def test_time_entries_context_menu_should_include_custom_fields + field = TimeEntryCustomField.generate!(:name => "Field", :field_format => "list", :possible_values => ["foo", "bar"]) + + @request.session[:user_id] = 2 + get( + :index, + :params => { + :ids => [1, 2] + } + ) + assert_response :success + + assert_select "li.cf_#{field.id}" do + assert_select 'a[href="#"]', :text => "Field" + assert_select 'ul' do + assert_select 'a', 3 + assert_select 'a[href=?]', "/time_entries/bulk_update?ids%5B%5D=1&ids%5B%5D=2&time_entry%5Bcustom_field_values%5D%5B#{field.id}%5D=foo", :text => 'foo' + assert_select 'a[href=?]', "/time_entries/bulk_update?ids%5B%5D=1&ids%5B%5D=2&time_entry%5Bcustom_field_values%5D%5B#{field.id}%5D=bar", :text => 'bar' + assert_select 'a[href=?]', "/time_entries/bulk_update?ids%5B%5D=1&ids%5B%5D=2&time_entry%5Bcustom_field_values%5D%5B#{field.id}%5D=__none__", :text => 'none' + end + end + end + + def test_time_entries_context_menu_with_time_entry_that_is_not_visible_should_fail + project = Project.find(2) + project.enable_module!(:time_tracking) + time_entry = TimeEntry.generate!(project: project) + + @request.session[:user_id] = 2 + + get( + :index, + :params => { + :ids => [1, 5, time_entry.id] + } + ) + + assert_response :not_found + end + + def test_time_entries_context_menu_with_edit_own_time_entries_permission + @request.session[:user_id] = 2 + Role.find_by_name('Manager').remove_permission! :edit_time_entries + Role.find_by_name('Manager').add_permission! :edit_own_time_entries + ids = (0..1).map {TimeEntry.generate!(:user => User.find(2)).id} + get( + :index, + :params => { + :ids => ids + } + ) + assert_response :success + + assert_select 'a:not(.disabled)', :text => 'Bulk edit' + end + + def test_time_entries_context_menu_without_edit_permission + @request.session[:user_id] = 2 + Role.find_by_name('Manager').remove_permission! :edit_time_entries + get( + :index, + :params => { + :ids => [1, 2] + } + ) + assert_response :success + + assert_select 'a.disabled', :text => 'Bulk edit' + end + end +end diff --git a/test/functional/context_menus/users_controller.rb b/test/functional/context_menus/users_controller.rb new file mode 100644 index 000000000..210b9a0cf --- /dev/null +++ b/test/functional/context_menus/users_controller.rb @@ -0,0 +1,90 @@ +# 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' + +module ContextMenus + class UsersControllerTest < Redmine::ControllerTest + def test_users_context_menu + @request.session[:user_id] = 1 # admin + get :index, :params => {:ids => [8]} + assert_response :success + + assert_select 'li.folder' do + assert_select 'a', :text => 'Add to group' + assert_select 'ul' do + assert_select 'a', :text => 'A Team' + end + end + # User 8 is in Group 10 + assert_select 'li.folder' do + assert_select 'a', :text => 'Remove from group' + assert_select 'a', :text => 'A Team' + end + end + + def test_users_context_menu_bulk + @request.session[:user_id] = 1 # admin + # Add user 2 to group 10 (user 8 is already there) + Group.find(10).users << User.find(2) + + get :index, :params => {:ids => [2, 8]} + assert_response :success + + assert_select 'li.folder' do + assert_select 'a', :text => 'Add to group' + assert_select 'ul' do + assert_select 'a', :text => 'A Team' + assert_select 'a', :text => 'B Team' + end + end + # Both users are in Group 10 + assert_select 'li.folder' do + assert_select 'a', :text => 'Remove from group' + assert_select 'a', :text => 'A Team' + end + end + + def test_users_context_menu_bulk_with_different_groups + @request.session[:user_id] = 1 # admin + # User 8 is in Group 10 + # Add User 2 to Group 11 + Group.find(11).users << User.find(2) + + get :index, :params => {:ids => [2, 8]} + assert_response :success + + # Both Group 10 and Group 11 should be in the Remove submenu + assert_select 'li.folder' do + assert_select 'a', :text => 'Remove from group' + assert_select 'ul' do + assert_select 'a', :text => 'A Team' + assert_select 'a', :text => 'B Team' + end + end + end + + def test_users_context_menu_without_permission + @request.session[:user_id] = 2 + + get :index, :params => {:ids => [8]} + assert_response :forbidden + end + end +end diff --git a/test/functional/context_menus_controller_test.rb b/test/functional/context_menus_controller_test.rb deleted file mode 100644 index 0db361328..000000000 --- a/test/functional/context_menus_controller_test.rb +++ /dev/null @@ -1,634 +0,0 @@ -# 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 ContextMenusControllerTest < Redmine::ControllerTest - def test_context_menu_one_issue_should_link_to_issue_path - @request.session[:user_id] = 2 - get( - :issues, - :params => { - :ids => [1], - :back_url => '/issues' - } - ) - assert_response :success - - assert_select 'a.icon-edit[href=?]', '/issues/1/edit', :text => 'Edit' - assert_select 'a.icon-copy-link[data-clipboard-text=?]', 'http://test.host/issues/1', :text => 'Copy link' - assert_select 'a.icon-copy[href=?]', '/projects/ecookbook/issues/1/copy', :text => 'Copy' - assert_select 'a.icon-del[href*=?]', 'ids%5B%5D=1', :text => 'Delete issue' - - # Statuses - assert_select 'a[href*=?][data-method="patch"]', 'issue%5Bstatus_id%5D=5', :text => 'Closed' - assert_select 'a[href*=?][data-method="patch"]', 'issue%5Bpriority_id%5D=8', :text => 'Immediate' - # No inactive priorities - assert_select 'a', :text => /Inactive Priority/, :count => 0 - # Versions - assert_select 'a[href*=?][data-method="patch"]', 'issue%5Bfixed_version_id%5D=3', :text => '2.0' - assert_select 'a[href*=?][data-method="patch"]', 'issue%5Bfixed_version_id%5D=4', :text => 'eCookbook Subproject 1 - 2.0' - # Assignees - assert_select 'a[href*=?][data-method="patch"]', 'issue%5Bassigned_to_id%5D=3', :text => 'Dave Lopper' - end - - def test_context_menu_multiple_issues_should_link_to_bulk_update_issues_path - @request.session[:user_id] = 2 - get :issues, :params => { - :ids => [1, 2], - :back_url => '/projects/ecookbook/issues' - } - assert_response :success - - assert_select 'a.icon-edit[href=?]', '/issues/bulk_edit?ids%5B%5D=1&ids%5B%5D=2', :text => 'Bulk edit' - assert_select 'a.icon-copy[href=?]', '/issues/bulk_edit?copy=1&ids%5B%5D=1&ids%5B%5D=2', :text => 'Copy' - assert_select 'a.icon-del[href*=?]', 'ids%5B%5D=1&ids%5B%5D=2', :text => 'Delete issues' - - # Statuses - assert_select 'a[href*=?][data-method="patch"]', 'issue%5Bstatus_id%5D=5', :text => 'Closed' - assert_select 'a[href*=?][data-method="patch"]', 'issue%5Bpriority_id%5D=8', :text => 'Immediate' - # No inactive priorities - assert_select 'a', :text => /Inactive Priority/, :count => 0 - # Versions - assert_select 'a[href*=?][data-method="patch"]', 'issue%5Bfixed_version_id%5D=3', :text => '2.0' - assert_select 'a[href*=?][data-method="patch"]', 'issue%5Bfixed_version_id%5D=4', :text => 'eCookbook Subproject 1 - 2.0' - # Assignees - assert_select 'a[href*=?][data-method="patch"]', 'issue%5Bassigned_to_id%5D=3', :text => 'Dave Lopper' - end - - def test_context_menu_one_issue_by_anonymous - with_settings :default_language => 'en' do - get( - :issues, - :params => { - :ids => [1], - :back_url => '/issues' - } - ) - assert_response :success - - assert_select 'a.icon-del.disabled[href="#"]', :text => 'Delete issue' - end - end - - def test_context_menu_multiple_issues_of_same_project - @request.session[:user_id] = 2 - get( - :issues, - :params => { - :ids => [1, 2], - :back_url => '/issues' - } - ) - assert_response :success - - ids = [1, 2].map {|i| "ids%5B%5D=#{i}"}.join('&') - - assert_select 'a.icon-edit[href=?]', "/issues/bulk_edit?#{ids}", :text => 'Bulk edit' - # issue_id: '1,2', set_filter: 1, status_id: '*' - assert_select 'a.icon-copy-link[data-clipboard-text=?]', "http://test.host/projects/ecookbook/issues?issue_id=1%2C2&set_filter=1&status_id=%2A", :text => 'Copy link' - assert_select 'a.icon-copy[href=?]', "/issues/bulk_edit?copy=1&#{ids}", :text => 'Copy' - assert_select 'a.icon-del[href*=?]', ids, :text => 'Delete issues' - - assert_select 'a[href*=?]', 'issue%5Bstatus_id%5D=5', :text => 'Closed' - assert_select 'a[href*=?]', 'issue%5Bpriority_id%5D=8', :text => 'Immediate' - assert_select 'a[href*=?]', 'issue%5Bassigned_to_id%5D=3', :text => 'Dave Lopper' - end - - def test_context_menu_multiple_issues_of_different_projects - @request.session[:user_id] = 2 - get( - :issues, - :params => { - :ids => [1, 2, 6], - :back_url => '/issues' - } - ) - assert_response :success - - ids = [1, 2, 6].map {|i| "ids%5B%5D=#{i}"}.join('&') - - assert_select 'a.icon-edit[href=?]', "/issues/bulk_edit?#{ids}", :text => 'Bulk edit' - # issue_id: '1,2,6', set_filter: 1, status_id: '*' - assert_select 'a.icon-copy-link[data-clipboard-text=?]', "http://test.host/issues?issue_id=1%2C2%2C6&set_filter=1&status_id=%2A", :text => 'Copy link' - assert_select 'a.icon-del[href*=?]', ids, :text => 'Delete issues' - - assert_select 'a[href*=?]', 'issue%5Bstatus_id%5D=5', :text => 'Closed' - assert_select 'a[href*=?]', 'issue%5Bpriority_id%5D=8', :text => 'Immediate' - assert_select 'a[href*=?]', 'issue%5Bassigned_to_id%5D=2', :text => 'John Smith' - end - - def test_context_menu_should_include_list_custom_fields - field = IssueCustomField.create!(:name => 'List', :field_format => 'list', - :possible_values => ['Foo', 'Bar'], :is_for_all => true, :tracker_ids => [1, 2, 3]) - @request.session[:user_id] = 2 - get( - :issues, - :params => { - :ids => [1] - } - ) - assert_select "li.cf_#{field.id}" do - assert_select 'a[href="#"]', :text => 'List' - assert_select 'ul' do - assert_select 'a', 3 - assert_select 'a[href=?]', "/issues/1?ids%5B%5D=1&issue%5Bcustom_field_values%5D%5B#{field.id}%5D=Foo", :text => 'Foo' - assert_select 'a[href=?]', "/issues/1?ids%5B%5D=1&issue%5Bcustom_field_values%5D%5B#{field.id}%5D=__none__", :text => 'none' - end - end - end - - def test_context_menu_multiple_issues_should_include_list_custom_fields - field = IssueCustomField.create!(:name => 'List', :field_format => 'list', - :possible_values => ['Foo', 'Bar'], :is_for_all => true, :tracker_ids => [1, 2, 3]) - @request.session[:user_id] = 2 - get( - :issues, - :params => { - :ids => [1, 2] - } - ) - assert_select "li.cf_#{field.id}" do - assert_select 'a[href="#"]', :text => 'List' - assert_select 'ul' do - assert_select 'a', 3 - assert_select 'a[href=?]', "/issues/bulk_update?ids%5B%5D=1&ids%5B%5D=2&issue%5Bcustom_field_values%5D%5B#{field.id}%5D=Foo", :text => 'Foo' - assert_select 'a[href=?]', "/issues/bulk_update?ids%5B%5D=1&ids%5B%5D=2&issue%5Bcustom_field_values%5D%5B#{field.id}%5D=__none__", :text => 'none' - end - end - end - - def test_context_menu_should_not_include_null_value_for_required_custom_fields - field = IssueCustomField.create!(:name => 'List', :is_required => true, :field_format => 'list', - :possible_values => ['Foo', 'Bar'], :is_for_all => true, :tracker_ids => [1, 2, 3]) - @request.session[:user_id] = 2 - get( - :issues, - :params => { - :ids => [1, 2] - } - ) - assert_select "li.cf_#{field.id}" do - assert_select 'a[href="#"]', :text => 'List' - assert_select 'ul' do - assert_select 'a', 2 - assert_select 'a', :text => 'none', :count => 0 - end - end - end - - def test_context_menu_on_single_issue_should_select_current_custom_field_value - field = IssueCustomField.create!(:name => 'List', :field_format => 'list', - :possible_values => ['Foo', 'Bar'], :is_for_all => true, :tracker_ids => [1, 2, 3]) - issue = Issue.find(1) - issue.custom_field_values = {field.id => 'Bar'} - issue.save! - @request.session[:user_id] = 2 - get( - :issues, - :params => { - :ids => [1] - } - ) - assert_select "li.cf_#{field.id}" do - assert_select 'a[href="#"]', :text => 'List' - assert_select 'ul' do - assert_select 'a', 3 - assert_select 'a.icon', :text => 'Bar' - end - end - end - - def test_context_menu_should_include_bool_custom_fields - field = IssueCustomField.create!(:name => 'Bool', :field_format => 'bool', - :is_for_all => true, :tracker_ids => [1, 2, 3]) - @request.session[:user_id] = 2 - get( - :issues, - :params => { - :ids => [1] - } - ) - assert_select "li.cf_#{field.id}" do - assert_select 'a[href="#"]', :text => 'Bool' - assert_select 'ul' do - assert_select 'a', 3 - assert_select 'a[href=?]', "/issues/1?ids%5B%5D=1&issue%5Bcustom_field_values%5D%5B#{field.id}%5D=0", :text => 'No' - assert_select 'a[href=?]', "/issues/1?ids%5B%5D=1&issue%5Bcustom_field_values%5D%5B#{field.id}%5D=1", :text => 'Yes' - assert_select 'a[href=?]', "/issues/1?ids%5B%5D=1&issue%5Bcustom_field_values%5D%5B#{field.id}%5D=__none__", :text => 'none' - end - end - end - - def test_context_menu_should_include_user_custom_fields - field = IssueCustomField.create!(:name => 'User', :field_format => 'user', - :is_for_all => true, :tracker_ids => [1, 2, 3]) - @request.session[:user_id] = 2 - get( - :issues, - :params => { - :ids => [1] - } - ) - assert_select "li.cf_#{field.id}" do - assert_select 'a[href="#"]', :text => 'User' - assert_select 'ul' do - assert_select 'a', Project.find(1).members.count + 2 # users + 'none' + 'me' - assert_select 'a[href=?]', "/issues/1?ids%5B%5D=1&issue%5Bcustom_field_values%5D%5B#{field.id}%5D=2", :text => 'John Smith' - assert_select 'a[href=?]', "/issues/1?ids%5B%5D=1&issue%5Bcustom_field_values%5D%5B#{field.id}%5D=__none__", :text => 'none' - end - end - end - - def test_context_menu_should_include_version_custom_fields - field = IssueCustomField.create!(:name => 'Version', :field_format => 'version', :is_for_all => true, :tracker_ids => [1, 2, 3]) - @request.session[:user_id] = 2 - get( - :issues, - :params => { - :ids => [1] - } - ) - assert_select "li.cf_#{field.id}" do - assert_select 'a[href="#"]', :text => 'Version' - assert_select 'ul' do - assert_select 'a', Project.find(1).shared_versions.count + 1 - assert_select 'a[href=?]', "/issues/1?ids%5B%5D=1&issue%5Bcustom_field_values%5D%5B#{field.id}%5D=3", :text => '2.0' - assert_select 'a[href=?]', "/issues/1?ids%5B%5D=1&issue%5Bcustom_field_values%5D%5B#{field.id}%5D=__none__", :text => 'none' - end - end - end - - def test_context_menu_should_show_enabled_custom_fields_for_the_role_only - enabled_cf = - IssueCustomField.generate!( - :field_format => 'bool', :is_for_all => true, - :tracker_ids => [1], :visible => false, :role_ids => [1, 2] - ) - disabled_cf = - IssueCustomField.generate!( - :field_format => 'bool', :is_for_all => true, - :tracker_ids => [1], :visible => false, :role_ids => [2] - ) - issue = Issue.generate!(:project_id => 1, :tracker_id => 1) - - @request.session[:user_id] = 2 - get( - :issues, - :params => { - :ids => [issue.id] - } - ) - assert_select "li.cf_#{enabled_cf.id}" - assert_select "li.cf_#{disabled_cf.id}", 0 - end - - def test_context_menu_by_assignable_user_should_include_assigned_to_me_link - @request.session[:user_id] = 2 - get( - :issues, - :params => { - :ids => [1, 2] - } - ) - assert_response :success - - assert_select 'a[href=?]', '/issues/bulk_update?ids%5B%5D=1&ids%5B%5D=2&issue%5Bassigned_to_id%5D=2', :text => / me / - end - - def test_context_menu_should_propose_shared_versions_for_issues_from_different_projects - @request.session[:user_id] = 2 - version = Version.create!(:name => 'Shared', :sharing => 'system', :project_id => 1) - - get( - :issues, - :params => { - :ids => [1, 4] - } - ) - assert_response :success - - assert_select 'a', :text => 'eCookbook - Shared' - end - - def test_context_menu_should_respect_five_percent_increments - with_settings :issue_done_ratio => 'issue_field', :issue_done_ratio_interval => 5 do - @request.session[:user_id] = 2 - get( - :issues, - :params => { - :ids => [1, 2] - } - ) - assert_response :success - - assert_select 'a[href*=?]', '/issues/bulk_update?ids%5B%5D=1&ids%5B%5D=2&issue%5Bdone_ratio%5D=0', :text => '0%' - assert_select 'a[href*=?]', '/issues/bulk_update?ids%5B%5D=1&ids%5B%5D=2&issue%5Bdone_ratio%5D=5', :text => '5%' - assert_select 'a[href*=?]', '/issues/bulk_update?ids%5B%5D=1&ids%5B%5D=2&issue%5Bdone_ratio%5D=10', :text => '10%' - assert_select 'a[href*=?]', '/issues/bulk_update?ids%5B%5D=1&ids%5B%5D=2&issue%5Bdone_ratio%5D=55', :text => '55%' - assert_select 'a[href*=?]', '/issues/bulk_update?ids%5B%5D=1&ids%5B%5D=2&issue%5Bdone_ratio%5D=100', :text => '100%' - end - end - - def test_context_menu_should_include_add_subtask_link - @request.session[:user_id] = 2 - get( - :issues, - :params => { - :ids => [1] - } - ) - assert_response :success - - assert_select 'a.icon-add[href=?]', '/projects/ecookbook/issues/new?issue%5Bparent_issue_id%5D=1&issue%5Btracker_id%5D=1', :text => 'Add subtask' - end - - def test_context_menu_with_closed_issue_should_not_include_add_subtask_link - @request.session[:user_id] = 2 - get( - :issues, - :params => { - :ids => [8] - } - ) - assert_response :success - - assert_select 'a.icon-add', :text => 'Add subtask', :count => 0 - end - - def test_context_menu_multiple_issues_should_not_include_add_subtask_link - @request.session[:user_id] = 2 - get( - :issues, - :params => { - :ids => [1, 2] - } - ) - assert_response :success - - assert_select 'a.icon-add', :text => 'Add subtask', :count => 0 - end - - def test_context_menu_with_issue_that_is_not_visible_should_fail - get( - :issues, - :params => { - :ids => [1, 4] # issue 4 is not visible - } - ) - assert_response :found - end - - def test_should_respond_with_404_without_ids - get :issues - assert_response :not_found - end - - def test_time_entries_context_menu - @request.session[:user_id] = 2 - get( - :time_entries, - :params => { - :ids => [1, 2] - } - ) - assert_response :success - - assert_select 'a:not(.disabled)', :text => 'Bulk edit' - end - - def test_context_menu_for_one_time_entry - @request.session[:user_id] = 2 - get( - :time_entries, - :params => { - :ids => [1] - } - ) - assert_response :success - - assert_select 'a:not(.disabled)', :text => 'Edit' - end - - def test_time_entries_context_menu_should_include_custom_fields - field = TimeEntryCustomField.generate!(:name => "Field", :field_format => "list", :possible_values => ["foo", "bar"]) - - @request.session[:user_id] = 2 - get( - :time_entries, - :params => { - :ids => [1, 2] - } - ) - assert_response :success - - assert_select "li.cf_#{field.id}" do - assert_select 'a[href="#"]', :text => "Field" - assert_select 'ul' do - assert_select 'a', 3 - assert_select 'a[href=?]', "/time_entries/bulk_update?ids%5B%5D=1&ids%5B%5D=2&time_entry%5Bcustom_field_values%5D%5B#{field.id}%5D=foo", :text => 'foo' - assert_select 'a[href=?]', "/time_entries/bulk_update?ids%5B%5D=1&ids%5B%5D=2&time_entry%5Bcustom_field_values%5D%5B#{field.id}%5D=bar", :text => 'bar' - assert_select 'a[href=?]', "/time_entries/bulk_update?ids%5B%5D=1&ids%5B%5D=2&time_entry%5Bcustom_field_values%5D%5B#{field.id}%5D=__none__", :text => 'none' - end - end - end - - def test_projects_context_menu_admin_user - @request.session[:user_id] = 1 - - get( - :projects, - :params => { - :ids => [1, 2] - } - ) - - assert_response :success - end - - def test_projects_context_menu_not_admin_user - @request.session[:user_id] = 2 - - get( - :projects, - :params => { - :ids => [1, 2] - } - ) - - assert_response :forbidden - end - - def test_time_entries_context_menu_with_time_entry_that_is_not_visible_should_fail - project = Project.find(2) - project.enable_module!(:time_tracking) - time_entry = TimeEntry.generate!(project: project) - - @request.session[:user_id] = 2 - - get( - :time_entries, - :params => { - :ids => [1, 5, time_entry.id] - } - ) - - assert_response :not_found - end - - def test_time_entries_context_menu_with_edit_own_time_entries_permission - @request.session[:user_id] = 2 - Role.find_by_name('Manager').remove_permission! :edit_time_entries - Role.find_by_name('Manager').add_permission! :edit_own_time_entries - ids = (0..1).map {TimeEntry.generate!(:user => User.find(2)).id} - get( - :time_entries, - :params => { - :ids => ids - } - ) - assert_response :success - - assert_select 'a:not(.disabled)', :text => 'Bulk edit' - end - - def test_time_entries_context_menu_without_edit_permission - @request.session[:user_id] = 2 - Role.find_by_name('Manager').remove_permission! :edit_time_entries - get( - :time_entries, - :params => { - :ids => [1, 2] - } - ) - assert_response :success - - assert_select 'a.disabled', :text => 'Bulk edit' - end - - def test_context_menu_should_include_delete_for_allowed_back_urls - @request.session[:user_id] = 2 - %w[ - /issues - /projects/ecookbook/issues/gantt - /projects/ecookbook/issues/calendar - ].each do |back_url| - get :issues, :params => { :ids => [1], :back_url => back_url } - assert_response :success - assert_select 'a.icon-del', :text => /Delete/ - end - end - - def test_context_menu_with_suburi_should_include_delete_for_allowed_back_urls - @relative_url_root = Redmine::Utils.relative_url_root - Redmine::Utils.relative_url_root = '/redmine' - - @request.session[:user_id] = 2 - %w[ - /redmine/issues - /redmine/projects/ecookbook/issues/gantt - /redmine/projects/ecookbook/issues/calendar - ].each do |back_url| - get :issues, :params => { :ids => [1], :back_url => back_url } - assert_response :success - assert_select 'a.icon-del', :text => /Delete/ - end - ensure - Redmine::Utils.relative_url_root = @relative_url_root - end - - def test_context_menu_should_not_include_delete_for_disallowed_back_urls - @request.session[:user_id] = 2 - %w[ - /issues/1 - /projects/ecookbook/roadmap - /not/a/real/path - ].each do |back_url| - get :issues, :params => { :ids => [1], :back_url => back_url } - assert_response :success - assert_select 'a.icon-del', :count => 0 - end - end - - def test_users_context_menu - @request.session[:user_id] = 1 # admin - get :users, :params => {:ids => [8]} - assert_response :success - - assert_select 'li.folder' do - assert_select 'a', :text => 'Add to group' - assert_select 'ul' do - assert_select 'a', :text => 'A Team' - end - end - # User 8 is in Group 10 - assert_select 'li.folder' do - assert_select 'a', :text => 'Remove from group' - assert_select 'a', :text => 'A Team' - end - end - - def test_users_context_menu_bulk - @request.session[:user_id] = 1 # admin - # Add user 2 to group 10 (user 8 is already there) - Group.find(10).users << User.find(2) - - get :users, :params => {:ids => [2, 8]} - assert_response :success - - assert_select 'li.folder' do - assert_select 'a', :text => 'Add to group' - assert_select 'ul' do - assert_select 'a', :text => 'A Team' - assert_select 'a', :text => 'B Team' - end - end - # Both users are in Group 10 - assert_select 'li.folder' do - assert_select 'a', :text => 'Remove from group' - assert_select 'a', :text => 'A Team' - end - end - - def test_users_context_menu_bulk_with_different_groups - @request.session[:user_id] = 1 # admin - # User 8 is in Group 10 - # Add User 2 to Group 11 - Group.find(11).users << User.find(2) - - get :users, :params => {:ids => [2, 8]} - assert_response :success - - # Both Group 10 and Group 11 should be in the Remove submenu - assert_select 'li.folder' do - assert_select 'a', :text => 'Remove from group' - assert_select 'ul' do - assert_select 'a', :text => 'A Team' - assert_select 'a', :text => 'B Team' - end - end - end - - def test_users_context_menu_without_permission - @request.session[:user_id] = 2 - - get :users, :params => {:ids => [8]} - assert_response :forbidden - end -end -- 2.50.1 (Apple Git-155)