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.0KB

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