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.

custom_fields_controller_test.rb 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2016 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 CustomFieldsControllerTest < Redmine::ControllerTest
  19. fixtures :custom_fields, :custom_values,
  20. :custom_fields_projects, :custom_fields_trackers,
  21. :roles, :users,
  22. :members, :member_roles,
  23. :groups_users,
  24. :trackers, :projects_trackers,
  25. :enabled_modules,
  26. :projects, :issues,
  27. :issue_statuses,
  28. :issue_categories,
  29. :enumerations,
  30. :workflows
  31. def setup
  32. @request.session[:user_id] = 1
  33. end
  34. def test_index
  35. get :index
  36. assert_response :success
  37. assert_select 'table.custom_fields'
  38. end
  39. def test_new_without_type_should_render_select_type
  40. get :new
  41. assert_response :success
  42. assert_select 'input[name=type]', CustomFieldsHelper::CUSTOM_FIELDS_TABS.size
  43. assert_select 'input[name=type][checked=checked]', 1
  44. end
  45. def test_new_should_work_for_each_customized_class_and_format
  46. custom_field_classes.each do |klass|
  47. Redmine::FieldFormat.formats_for_custom_field_class(klass).each do |format|
  48. get :new, :type => klass.name, :custom_field => {:field_format => format.name}
  49. assert_response :success
  50. assert_select 'form#custom_field_form' do
  51. assert_select 'select[name=?]', 'custom_field[field_format]' do
  52. assert_select 'option[value=?][selected=selected]', format.name
  53. end
  54. assert_select 'input[type=hidden][name=type][value=?]', klass.name
  55. end
  56. end
  57. end
  58. end
  59. def test_new_should_have_string_default_format
  60. get :new, :type => 'IssueCustomField'
  61. assert_response :success
  62. assert_select 'select[name=?]', 'custom_field[field_format]' do
  63. assert_select 'option[value=?][selected=selected]', 'string'
  64. end
  65. end
  66. def test_new_issue_custom_field
  67. get :new, :type => 'IssueCustomField'
  68. assert_response :success
  69. assert_select 'form#custom_field_form' do
  70. assert_select 'select#custom_field_field_format[name=?]', 'custom_field[field_format]' do
  71. assert_select 'option[value=user]', :text => 'User'
  72. assert_select 'option[value=version]', :text => 'Version'
  73. end
  74. assert_select 'input[type=checkbox][name=?]', 'custom_field[project_ids][]', Project.count
  75. assert_select 'input[type=hidden][name=?]', 'custom_field[project_ids][]', 1
  76. assert_select 'input[type=hidden][name=type][value=IssueCustomField]'
  77. end
  78. end
  79. def test_new_time_entry_custom_field_should_not_show_trackers_and_projects
  80. get :new, :type => 'TimeEntryCustomField'
  81. assert_response :success
  82. assert_select 'form#custom_field_form' do
  83. assert_select 'input[name=?]', 'custom_field[tracker_ids][]', 0
  84. assert_select 'input[name=?]', 'custom_field[project_ids][]', 0
  85. end
  86. end
  87. def test_default_value_should_be_an_input_for_string_custom_field
  88. get :new, :type => 'IssueCustomField', :custom_field => {:field_format => 'string'}
  89. assert_response :success
  90. assert_select 'input[name=?]', 'custom_field[default_value]'
  91. end
  92. def test_default_value_should_be_a_textarea_for_text_custom_field
  93. get :new, :type => 'IssueCustomField', :custom_field => {:field_format => 'text'}
  94. assert_response :success
  95. assert_select 'textarea[name=?]', 'custom_field[default_value]'
  96. end
  97. def test_default_value_should_be_a_checkbox_for_bool_custom_field
  98. get :new, :type => 'IssueCustomField', :custom_field => {:field_format => 'bool'}
  99. assert_response :success
  100. assert_select 'select[name=?]', 'custom_field[default_value]' do
  101. assert_select 'option', 3
  102. end
  103. end
  104. def test_default_value_should_not_be_present_for_user_custom_field
  105. get :new, :type => 'IssueCustomField', :custom_field => {:field_format => 'user'}
  106. assert_response :success
  107. assert_select '[name=?]', 'custom_field[default_value]', 0
  108. end
  109. def test_setting_full_width_layout_shoul_be_present_only_for_long_text_issue_custom_field
  110. get :new, :type => 'IssueCustomField', :custom_field => {:field_format => 'text'}
  111. assert_response :success
  112. assert_select '[name=?]', 'custom_field[full_width_layout]'
  113. get :new, :type => 'IssueCustomField', :custom_field => {:field_format => 'list'}
  114. assert_response :success
  115. assert_select '[name=?]', 'custom_field[full_width_layout]', 0
  116. get :new, :type => 'TimeEntryCustomField', :custom_field => {:field_format => 'text'}
  117. assert_response :success
  118. assert_select '[name=?]', 'custom_field[full_width_layout]', 0
  119. end
  120. def test_new_js
  121. xhr :get, :new, :type => 'IssueCustomField', :custom_field => {:field_format => 'list'}, :format => 'js'
  122. assert_response :success
  123. assert_equal 'text/javascript', response.content_type
  124. assert_include '<option selected=\"selected\" value=\"list\">List<\/option>', response.body
  125. end
  126. def test_new_with_invalid_custom_field_class_should_render_select_type
  127. get :new, :type => 'UnknownCustomField'
  128. assert_response :success
  129. assert_select 'input[type=radio][name=type]'
  130. end
  131. def test_create_list_custom_field
  132. field = new_record(IssueCustomField) do
  133. post :create, :type => "IssueCustomField",
  134. :custom_field => {:name => "test_post_new_list",
  135. :default_value => "",
  136. :min_length => "0",
  137. :searchable => "0",
  138. :regexp => "",
  139. :is_for_all => "1",
  140. :possible_values => "0.1\n0.2\n",
  141. :max_length => "0",
  142. :is_filter => "0",
  143. :is_required =>"0",
  144. :field_format => "list",
  145. :tracker_ids => ["1", ""]}
  146. end
  147. assert_redirected_to "/custom_fields/#{field.id}/edit"
  148. assert_equal "test_post_new_list", field.name
  149. assert_equal ["0.1", "0.2"], field.possible_values
  150. assert_equal 1, field.trackers.size
  151. end
  152. def test_create_with_project_ids
  153. assert_difference 'CustomField.count' do
  154. post :create, :type => "IssueCustomField", :custom_field => {
  155. :name => "foo", :field_format => "string", :is_for_all => "0", :project_ids => ["1", "3", ""]
  156. }
  157. assert_response 302
  158. end
  159. field = IssueCustomField.order("id desc").first
  160. assert_equal [1, 3], field.projects.map(&:id).sort
  161. end
  162. def test_create_with_failure
  163. assert_no_difference 'CustomField.count' do
  164. post :create, :type => "IssueCustomField", :custom_field => {:name => ''}
  165. end
  166. assert_response :success
  167. assert_select_error /name cannot be blank/i
  168. end
  169. def test_create_without_type_should_render_select_type
  170. assert_no_difference 'CustomField.count' do
  171. post :create, :custom_field => {:name => ''}
  172. end
  173. assert_response :success
  174. assert_select 'input[type=radio][name=type]'
  175. end
  176. def test_edit
  177. get :edit, :id => 1
  178. assert_response :success
  179. assert_select 'input[name=?][value=?]', 'custom_field[name]', 'Database'
  180. end
  181. def test_edit_invalid_custom_field_should_render_404
  182. get :edit, :id => 99
  183. assert_response 404
  184. end
  185. def test_update
  186. put :update, :id => 1, :custom_field => {:name => 'New name'}
  187. assert_redirected_to '/custom_fields/1/edit'
  188. field = CustomField.find(1)
  189. assert_equal 'New name', field.name
  190. end
  191. def test_update_with_failure
  192. put :update, :id => 1, :custom_field => {:name => ''}
  193. assert_response :success
  194. assert_select_error /name cannot be blank/i
  195. end
  196. def test_destroy
  197. custom_values_count = CustomValue.where(:custom_field_id => 1).count
  198. assert custom_values_count > 0
  199. assert_difference 'CustomField.count', -1 do
  200. assert_difference 'CustomValue.count', - custom_values_count do
  201. delete :destroy, :id => 1
  202. end
  203. end
  204. assert_redirected_to '/custom_fields?tab=IssueCustomField'
  205. assert_nil CustomField.find_by_id(1)
  206. assert_nil CustomValue.find_by_custom_field_id(1)
  207. end
  208. def custom_field_classes
  209. files = Dir.glob(File.join(Rails.root, 'app/models/*_custom_field.rb')).map {|f| File.basename(f).sub(/\.rb$/, '') }
  210. classes = files.map(&:classify).map(&:constantize)
  211. assert classes.size > 0
  212. classes
  213. end
  214. end