You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

search_custom_fields_visibility_test.rb 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2021 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. require File.expand_path('../../test_helper', __FILE__)
  19. class SearchCustomFieldsVisibilityTest < Redmine::ControllerTest
  20. tests SearchController
  21. fixtures :projects,
  22. :users,
  23. :roles,
  24. :members,
  25. :member_roles,
  26. :issue_statuses,
  27. :trackers,
  28. :projects_trackers,
  29. :enabled_modules,
  30. :enumerations,
  31. :workflows,
  32. :custom_fields, :custom_fields_trackers
  33. def setup
  34. field_attributes = {:field_format => 'string', :is_for_all => true, :is_filter => true, :searchable => true, :trackers => Tracker.all}
  35. @fields = []
  36. @fields << (@field1 = IssueCustomField.create!(field_attributes.merge(:name => 'Field 1', :visible => true)))
  37. @fields << (@field2 = IssueCustomField.create!(field_attributes.merge(:name => 'Field 2', :visible => false, :role_ids => [1, 2])))
  38. @fields << (@field3 = IssueCustomField.create!(field_attributes.merge(:name => 'Field 3', :visible => false, :role_ids => [1, 3])))
  39. @issue = Issue.generate!(
  40. :author_id => 1,
  41. :project_id => 1,
  42. :tracker_id => 1,
  43. :custom_field_values => {@field1.id => 'Value0', @field2.id => 'Value1', @field3.id => 'Value2'}
  44. )
  45. @user_with_role_on_other_project = User.generate!
  46. User.add_to_project(@user_with_role_on_other_project, Project.find(2), Role.find(3))
  47. @users_to_test = {
  48. User.find(1) => [@field1, @field2, @field3],
  49. User.find(3) => [@field1, @field2],
  50. @user_with_role_on_other_project => [@field1], # should see field1 only on Project 1
  51. User.generate! => [@field1],
  52. User.anonymous => [@field1]
  53. }
  54. Member.where(:project_id => 1).each do |member|
  55. member.destroy unless @users_to_test.key?(member.principal)
  56. end
  57. end
  58. def test_search_should_search_visible_custom_fields_only
  59. @users_to_test.each do |user, fields|
  60. @request.session[:user_id] = user.id
  61. @fields.each_with_index do |field, i|
  62. get :index, :params => {:q => "value#{i}"}
  63. assert_response :success
  64. # we should get a result only if the custom field is visible
  65. if fields.include?(field)
  66. assert_select '#search-results dt', 1
  67. else
  68. assert_select '#search-results dt', 0
  69. end
  70. end
  71. end
  72. end
  73. end