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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 TimelogCustomFieldsVisibilityTest < Redmine::ControllerTest
  19. tests TimelogController
  20. fixtures :projects,
  21. :users, :email_addresses,
  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_values, :custom_fields_trackers
  32. def setup
  33. field_attributes = {:field_format => 'string', :is_for_all => true, :is_filter => 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. TimeEntry.generate!(:issue => @issue)
  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.keys.include?(member.principal)
  56. end
  57. end
  58. def test_index_should_show_visible_custom_fields_only
  59. @users_to_test.each do |user, fields|
  60. @request.session[:user_id] = user.id
  61. get :index, :params => {
  62. :project_id => 1,
  63. :issue_id => @issue.id,
  64. :c => (['hours'] + @fields.map{|f| "issue.cf_#{f.id}"})
  65. }
  66. @fields.each_with_index do |field, i|
  67. if fields.include?(field)
  68. assert_select 'td', {:text => "Value#{i}", :count => 1}, "User #{user.id} was not able to view #{field.name}"
  69. else
  70. assert_select 'td', {:text => "Value#{i}", :count => 0}, "User #{user.id} was able to view #{field.name}"
  71. end
  72. end
  73. end
  74. end
  75. def test_index_as_csv_should_show_visible_custom_fields_only
  76. @users_to_test.each do |user, fields|
  77. @request.session[:user_id] = user.id
  78. get :index, :params => {
  79. :project_id => 1,
  80. :issue_id => @issue.id,
  81. :c => (['hours'] + @fields.map{|f| "issue.cf_#{f.id}"}),
  82. :format => 'csv'
  83. }
  84. @fields.each_with_index do |field, i|
  85. if fields.include?(field)
  86. assert_include "Value#{i}", response.body, "User #{user.id} was not able to view #{field.name} in CSV"
  87. else
  88. assert_not_include "Value#{i}", response.body, "User #{user.id} was able to view #{field.name} in CSV"
  89. end
  90. end
  91. end
  92. end
  93. def test_index_with_partial_custom_field_visibility_should_show_visible_custom_fields_only
  94. Issue.delete_all
  95. TimeEntry.delete_all
  96. CustomValue.delete_all
  97. p1 = Project.generate!
  98. p2 = Project.generate!
  99. user = User.generate!
  100. User.add_to_project(user, p1, Role.where(:id => [1, 3]).to_a)
  101. User.add_to_project(user, p2, Role.where(:id => 3).to_a)
  102. TimeEntry.generate!(
  103. :issue => Issue.generate!(:project => p1, :tracker_id => 1,
  104. :custom_field_values => {@field2.id => 'ValueA'}))
  105. TimeEntry.generate!(
  106. :issue => Issue.generate!(:project => p2, :tracker_id => 1,
  107. :custom_field_values => {@field2.id => 'ValueB'}))
  108. TimeEntry.generate!(
  109. :issue => Issue.generate!(:project => p1, :tracker_id => 1,
  110. :custom_field_values => {@field2.id => 'ValueC'}))
  111. @request.session[:user_id] = user.id
  112. get :index, :params => {:c => ["hours", "issue.cf_#{@field2.id}"]}
  113. assert_select 'td', {:text => 'ValueA'}, "ValueA not found in:\n#{response.body}"
  114. assert_select 'td', :text => 'ValueB', :count => 0
  115. assert_select 'td', {:text => 'ValueC'}, "ValueC not found in:\n#{response.body}"
  116. get :index, :params => {:set_filter => '1', "issue.cf_#{@field2.id}" => '*', :c => ["issue.cf_#{@field2.id}"]}
  117. assert_select 'td', :text => "ValueA"
  118. assert_select 'td', :text => "ValueC"
  119. assert_select 'td', :text => "ValueB", :count => 0
  120. end
  121. end