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.

timelog_custom_fields_visibility_test.rb 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2023 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_relative '../test_helper'
  19. class TimelogCustomFieldsVisibilityTest < Redmine::ControllerTest
  20. tests TimelogController
  21. fixtures :projects,
  22. :users, :email_addresses,
  23. :roles,
  24. :members,
  25. :member_roles,
  26. :issues, :issue_statuses,
  27. :trackers,
  28. :projects_trackers,
  29. :enabled_modules,
  30. :time_entries, :enumerations,
  31. :workflows,
  32. :custom_fields, :custom_values, :custom_fields_trackers
  33. def test_index_should_show_visible_custom_fields_only
  34. prepare_test_data
  35. @users_to_test.each do |user, fields|
  36. @request.session[:user_id] = user.id
  37. get :index, :params => {
  38. :project_id => 1,
  39. :issue_id => @issue.id,
  40. :c => (['hours'] + @fields.map{|f| "issue.cf_#{f.id}"})
  41. }
  42. @fields.each_with_index do |field, i|
  43. if fields.include?(field)
  44. assert_select 'td', {:text => "Value#{i}", :count => 1}, "User #{user.id} was not able to view #{field.name}"
  45. else
  46. assert_select 'td', {:text => "Value#{i}", :count => 0}, "User #{user.id} was able to view #{field.name}"
  47. end
  48. end
  49. end
  50. end
  51. def test_index_as_csv_should_show_visible_custom_fields_only
  52. prepare_test_data
  53. @users_to_test.each do |user, fields|
  54. @request.session[:user_id] = user.id
  55. get :index, :params => {
  56. :project_id => 1,
  57. :issue_id => @issue.id,
  58. :c => (['hours'] + @fields.map{|f| "issue.cf_#{f.id}"}),
  59. :format => 'csv'
  60. }
  61. @fields.each_with_index do |field, i|
  62. if fields.include?(field)
  63. assert_include "Value#{i}", response.body, "User #{user.id} was not able to view #{field.name} in CSV"
  64. else
  65. assert_not_include "Value#{i}", response.body, "User #{user.id} was able to view #{field.name} in CSV"
  66. end
  67. end
  68. end
  69. end
  70. def test_index_with_partial_custom_field_visibility_should_show_visible_custom_fields_only
  71. prepare_test_data
  72. Issue.delete_all
  73. TimeEntry.delete_all
  74. CustomValue.delete_all
  75. p1 = Project.generate!
  76. p2 = Project.generate!
  77. user = User.generate!
  78. User.add_to_project(user, p1, Role.where(:id => [1, 3]).to_a)
  79. User.add_to_project(user, p2, Role.where(:id => 3).to_a)
  80. TimeEntry.generate!(
  81. :issue => Issue.generate!(:project => p1, :tracker_id => 1,
  82. :custom_field_values => {@field2.id => 'ValueA'}))
  83. TimeEntry.generate!(
  84. :issue => Issue.generate!(:project => p2, :tracker_id => 1,
  85. :custom_field_values => {@field2.id => 'ValueB'}))
  86. TimeEntry.generate!(
  87. :issue => Issue.generate!(:project => p1, :tracker_id => 1,
  88. :custom_field_values => {@field2.id => 'ValueC'}))
  89. @request.session[:user_id] = user.id
  90. get :index, :params => {:c => ["hours", "issue.cf_#{@field2.id}"]}
  91. assert_select 'td', {:text => 'ValueA'}, "ValueA not found in:\n#{response.body}"
  92. assert_select 'td', :text => 'ValueB', :count => 0
  93. assert_select 'td', {:text => 'ValueC'}, "ValueC not found in:\n#{response.body}"
  94. get :index, :params => {:set_filter => '1', "issue.cf_#{@field2.id}" => '*', :c => ["issue.cf_#{@field2.id}"]}
  95. assert_select 'td', :text => "ValueA"
  96. assert_select 'td', :text => "ValueC"
  97. assert_select 'td', :text => "ValueB", :count => 0
  98. end
  99. def test_edit_should_not_show_custom_fields_not_visible_for_user
  100. time_entry_cf = TimeEntryCustomField.find(10)
  101. time_entry_cf.visible = false
  102. time_entry_cf.role_ids = [2]
  103. time_entry_cf.save!
  104. @request.session[:user_id] = 2
  105. get :edit, :params => {
  106. :id => 3,
  107. :project_id => 1
  108. }
  109. assert_response :success
  110. assert_select 'select#time_entry_custom_field_values_10', 0
  111. end
  112. private
  113. def prepare_test_data
  114. field_attributes = {:field_format => 'string', :is_for_all => true, :is_filter => true, :trackers => Tracker.all}
  115. @fields = []
  116. @fields << (@field1 = IssueCustomField.create!(field_attributes.merge(:name => 'Field 1', :visible => true)))
  117. @fields << (@field2 = IssueCustomField.create!(field_attributes.merge(:name => 'Field 2', :visible => false, :role_ids => [1, 2])))
  118. @fields << (@field3 = IssueCustomField.create!(field_attributes.merge(:name => 'Field 3', :visible => false, :role_ids => [1, 3])))
  119. @issue = Issue.generate!(
  120. :author_id => 1,
  121. :project_id => 1,
  122. :tracker_id => 1,
  123. :custom_field_values => {@field1.id => 'Value0', @field2.id => 'Value1', @field3.id => 'Value2'}
  124. )
  125. TimeEntry.generate!(:issue => @issue)
  126. @user_with_role_on_other_project = User.generate!
  127. User.add_to_project(@user_with_role_on_other_project, Project.find(2), Role.find(3))
  128. @users_to_test = {
  129. User.find(1) => [@field1, @field2, @field3],
  130. User.find(3) => [@field1, @field2],
  131. @user_with_role_on_other_project => [@field1], # should see field1 only on Project 1
  132. User.generate! => [@field1],
  133. User.anonymous => [@field1]
  134. }
  135. Member.where(:project_id => 1).each do |member|
  136. member.destroy unless @users_to_test.key?(member.principal)
  137. end
  138. end
  139. end