Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

custom_fields_controller_test.rb 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2019 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 CustomFieldsControllerTest < Redmine::ControllerTest
  20. fixtures :custom_fields, :custom_values,
  21. :custom_fields_projects, :custom_fields_trackers,
  22. :roles, :users,
  23. :members, :member_roles,
  24. :groups_users,
  25. :trackers, :projects_trackers,
  26. :enabled_modules,
  27. :projects, :issues,
  28. :issue_statuses,
  29. :issue_categories,
  30. :enumerations,
  31. :workflows
  32. def setup
  33. @request.session[:user_id] = 1
  34. end
  35. def test_index
  36. get :index
  37. assert_response :success
  38. assert_select 'table.custom_fields'
  39. end
  40. def test_new_without_type_should_render_select_type
  41. get :new
  42. assert_response :success
  43. assert_select 'input[name=type]', CustomFieldsHelper::CUSTOM_FIELDS_TABS.size
  44. assert_select 'input[name=type][checked=checked]', 1
  45. end
  46. def test_new_should_work_for_each_customized_class_and_format
  47. custom_field_classes.each do |klass|
  48. Redmine::FieldFormat.formats_for_custom_field_class(klass).each do |format|
  49. get :new, :params => {
  50. :type => klass.name,
  51. :custom_field => {
  52. :field_format => format.name
  53. }
  54. }
  55. assert_response :success
  56. assert_select 'form#custom_field_form' do
  57. assert_select 'select[name=?]', 'custom_field[field_format]' do
  58. assert_select 'option[value=?][selected=selected]', format.name
  59. end
  60. assert_select 'input[type=hidden][name=type][value=?]', klass.name
  61. end
  62. end
  63. end
  64. end
  65. def test_new_should_have_string_default_format
  66. get :new, :params => {
  67. :type => 'IssueCustomField'
  68. }
  69. assert_response :success
  70. assert_select 'select[name=?]', 'custom_field[field_format]' do
  71. assert_select 'option[value=?][selected=selected]', 'string'
  72. end
  73. end
  74. def test_new_issue_custom_field
  75. get :new, :params => {
  76. :type => 'IssueCustomField'
  77. }
  78. assert_response :success
  79. assert_select 'form#custom_field_form' do
  80. assert_select 'select#custom_field_field_format[name=?]', 'custom_field[field_format]' do
  81. assert_select 'option[value=user]', :text => 'User'
  82. assert_select 'option[value=version]', :text => 'Version'
  83. end
  84. # Visibility
  85. assert_select 'input[type=radio][name=?]', 'custom_field[visible]', 2
  86. assert_select 'input[type=checkbox][name=?]', 'custom_field[role_ids][]', 3
  87. assert_select 'input[type=checkbox][name=?]', 'custom_field[project_ids][]', Project.count
  88. assert_select 'input[type=hidden][name=?]', 'custom_field[project_ids][]', 1
  89. assert_select 'input[type=hidden][name=type][value=IssueCustomField]'
  90. end
  91. end
  92. def test_new_time_entry_custom_field
  93. get :new, :params => {
  94. :type => 'TimeEntryCustomField'
  95. }
  96. assert_response :success
  97. assert_select 'form#custom_field_form' do
  98. assert_select 'select#custom_field_field_format[name=?]', 'custom_field[field_format]' do
  99. assert_select 'option[value=user]', :text => 'User'
  100. assert_select 'option[value=version]', :text => 'Version'
  101. end
  102. # Visibility
  103. assert_select 'input[type=radio][name=?]', 'custom_field[visible]', 2
  104. assert_select 'input[type=checkbox][name=?]', 'custom_field[role_ids][]', 3
  105. assert_select 'input[type=hidden][name=type][value=TimeEntryCustomField]'
  106. end
  107. end
  108. def test_new_project_custom_field
  109. get :new, :params => {
  110. :type => 'ProjectCustomField'
  111. }
  112. assert_response :success
  113. assert_select 'form#custom_field_form' do
  114. assert_select 'select#custom_field_field_format[name=?]', 'custom_field[field_format]' do
  115. assert_select 'option[value=user]', :text => 'User'
  116. assert_select 'option[value=version]', :text => 'Version'
  117. end
  118. # Visibility
  119. assert_select 'input[type=radio][name=?]', 'custom_field[visible]', 2
  120. assert_select 'input[type=checkbox][name=?]', 'custom_field[role_ids][]', 3
  121. assert_select 'input[type=hidden][name=type][value=ProjectCustomField]'
  122. end
  123. end
  124. def test_new_time_entry_custom_field_should_not_show_trackers_and_projects
  125. get :new, :params => {
  126. :type => 'TimeEntryCustomField'
  127. }
  128. assert_response :success
  129. assert_select 'form#custom_field_form' do
  130. assert_select 'input[name=?]', 'custom_field[tracker_ids][]', 0
  131. assert_select 'input[name=?]', 'custom_field[project_ids][]', 0
  132. end
  133. end
  134. def test_default_value_should_be_an_input_for_string_custom_field
  135. get :new, :params => {
  136. :type => 'IssueCustomField',
  137. :custom_field => {
  138. :field_format => 'string'
  139. }
  140. }
  141. assert_response :success
  142. assert_select 'input[name=?]', 'custom_field[default_value]'
  143. end
  144. def test_default_value_should_be_a_textarea_for_text_custom_field
  145. get :new, :params => {
  146. :type => 'IssueCustomField',
  147. :custom_field => {
  148. :field_format => 'text'
  149. }
  150. }
  151. assert_response :success
  152. assert_select 'textarea[name=?]', 'custom_field[default_value]'
  153. end
  154. def test_default_value_should_be_a_checkbox_for_bool_custom_field
  155. get :new, :params => {
  156. :type => 'IssueCustomField',
  157. :custom_field => {
  158. :field_format => 'bool'
  159. }
  160. }
  161. assert_response :success
  162. assert_select 'select[name=?]', 'custom_field[default_value]' do
  163. assert_select 'option', 3
  164. end
  165. end
  166. def test_default_value_should_not_be_present_for_user_custom_field
  167. get :new, :params => {
  168. :type => 'IssueCustomField',
  169. :custom_field => {
  170. :field_format => 'user'
  171. }
  172. }
  173. assert_response :success
  174. assert_select '[name=?]', 'custom_field[default_value]', 0
  175. end
  176. def test_setting_full_width_layout_shoul_be_present_only_for_long_text_issue_custom_field
  177. get :new, :params => {
  178. :type => 'IssueCustomField',
  179. :custom_field => {
  180. :field_format => 'text'
  181. }
  182. }
  183. assert_response :success
  184. assert_select '[name=?]', 'custom_field[full_width_layout]'
  185. get :new, :params => {
  186. :type => 'IssueCustomField',
  187. :custom_field => {
  188. :field_format => 'list'
  189. }
  190. }
  191. assert_response :success
  192. assert_select '[name=?]', 'custom_field[full_width_layout]', 0
  193. get :new, :params => {
  194. :type => 'TimeEntryCustomField',
  195. :custom_field => {
  196. :field_format => 'text'
  197. }
  198. }
  199. assert_response :success
  200. assert_select '[name=?]', 'custom_field[full_width_layout]', 0
  201. end
  202. def test_new_js
  203. get :new, :params => {
  204. :type => 'IssueCustomField',
  205. :custom_field => {
  206. :field_format => 'list'
  207. },
  208. :format => 'js'
  209. },
  210. :xhr => true
  211. assert_response :success
  212. assert_equal 'text/javascript', response.content_type
  213. assert_include '<option selected=\"selected\" value=\"list\">List<\/option>', response.body
  214. end
  215. def test_new_with_invalid_custom_field_class_should_render_select_type
  216. get :new, :params => {
  217. :type => 'UnknownCustomField'
  218. }
  219. assert_response :success
  220. assert_select 'input[type=radio][name=type]'
  221. end
  222. def test_create_list_custom_field
  223. field = new_record(IssueCustomField) do
  224. post :create, :params => {
  225. :type => "IssueCustomField",
  226. :custom_field => {
  227. :name => "test_post_new_list",
  228. :default_value => "",
  229. :min_length => "0",
  230. :searchable => "0",
  231. :regexp => "",
  232. :is_for_all => "1",
  233. :possible_values => "0.1\n0.2\n",
  234. :max_length => "0",
  235. :is_filter => "0",
  236. :is_required =>"0",
  237. :field_format => "list",
  238. :tracker_ids => ["1", ""]
  239. }
  240. }
  241. end
  242. assert_redirected_to "/custom_fields/#{field.id}/edit"
  243. assert_equal "test_post_new_list", field.name
  244. assert_equal ["0.1", "0.2"], field.possible_values
  245. assert_equal 1, field.trackers.size
  246. end
  247. def test_create_with_project_ids
  248. assert_difference 'CustomField.count' do
  249. post :create, :params => {
  250. :type => "IssueCustomField",
  251. :custom_field => {
  252. :name => "foo",
  253. :field_format => "string",
  254. :is_for_all => "0",
  255. :project_ids => ["1", "3", ""]
  256. }
  257. }
  258. assert_response 302
  259. end
  260. field = IssueCustomField.order("id desc").first
  261. assert_equal [1, 3], field.projects.map(&:id).sort
  262. end
  263. def test_create_with_continue_params
  264. assert_difference 'CustomField.count' do
  265. post :create, :params => {
  266. :type => 'IssueCustomField',
  267. :continue => 'Create and Continue',
  268. :custom_field => {
  269. :name => 'foo',
  270. :field_format => 'string'
  271. }
  272. }
  273. end
  274. assert_redirected_to '/custom_fields/new?type=IssueCustomField'
  275. end
  276. def test_create_with_failure
  277. assert_no_difference 'CustomField.count' do
  278. post :create, :params => {
  279. :type => "IssueCustomField",
  280. :custom_field => {
  281. :name => ''
  282. }
  283. }
  284. end
  285. assert_response :success
  286. assert_select_error /name cannot be blank/i
  287. end
  288. def test_create_without_type_should_render_select_type
  289. assert_no_difference 'CustomField.count' do
  290. post :create, :params => {
  291. :custom_field => {
  292. :name => ''
  293. }
  294. }
  295. end
  296. assert_response :success
  297. assert_select 'input[type=radio][name=type]'
  298. end
  299. def test_edit
  300. get :edit, :params => {
  301. :id => 1
  302. }
  303. assert_response :success
  304. assert_select 'input[name=?][value=?]', 'custom_field[name]', 'Database'
  305. end
  306. def test_edit_invalid_custom_field_should_render_404
  307. get :edit, :params => {
  308. :id => 99
  309. }
  310. assert_response 404
  311. end
  312. def test_update
  313. put :update, :params => {
  314. :id => 1,
  315. :custom_field => {
  316. :name => 'New name'
  317. }
  318. }
  319. assert_redirected_to '/custom_fields/1/edit'
  320. field = CustomField.find(1)
  321. assert_equal 'New name', field.name
  322. end
  323. def test_update_with_failure
  324. put :update, :params => {
  325. :id => 1,
  326. :custom_field => {
  327. :name => ''
  328. }
  329. }
  330. assert_response :success
  331. assert_select_error /name cannot be blank/i
  332. end
  333. def test_destroy
  334. custom_values_count = CustomValue.where(:custom_field_id => 1).count
  335. assert custom_values_count > 0
  336. assert_difference 'CustomField.count', -1 do
  337. assert_difference 'CustomValue.count', - custom_values_count do
  338. delete :destroy, :params => {
  339. :id => 1
  340. }
  341. end
  342. end
  343. assert_redirected_to '/custom_fields?tab=IssueCustomField'
  344. assert_nil CustomField.find_by_id(1)
  345. assert_nil CustomValue.find_by_custom_field_id(1)
  346. end
  347. def custom_field_classes
  348. files = Dir.glob(File.join(Rails.root, 'app/models/*_custom_field.rb')).map {|f| File.basename(f).sub(/\.rb$/, '') }
  349. classes = files.map(&:classify).map(&:constantize)
  350. assert classes.size > 0
  351. classes
  352. end
  353. end