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 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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_version_custom_field
  125. get :new, :params => {
  126. :type => 'VersionCustomField'
  127. }
  128. assert_response :success
  129. assert_select 'form#custom_field_form' do
  130. assert_select 'select#custom_field_field_format[name=?]', 'custom_field[field_format]' do
  131. assert_select 'option[value=user]', :text => 'User'
  132. assert_select 'option[value=version]', :text => 'Version'
  133. end
  134. # Visibility
  135. assert_select 'input[type=radio][name=?]', 'custom_field[visible]', 2
  136. assert_select 'input[type=checkbox][name=?]', 'custom_field[role_ids][]', 3
  137. assert_select 'input[type=hidden][name=type][value=VersionCustomField]'
  138. end
  139. end
  140. def test_new_time_entry_custom_field_should_not_show_trackers_and_projects
  141. get :new, :params => {
  142. :type => 'TimeEntryCustomField'
  143. }
  144. assert_response :success
  145. assert_select 'form#custom_field_form' do
  146. assert_select 'input[name=?]', 'custom_field[tracker_ids][]', 0
  147. assert_select 'input[name=?]', 'custom_field[project_ids][]', 0
  148. end
  149. end
  150. def test_default_value_should_be_an_input_for_string_custom_field
  151. get :new, :params => {
  152. :type => 'IssueCustomField',
  153. :custom_field => {
  154. :field_format => 'string'
  155. }
  156. }
  157. assert_response :success
  158. assert_select 'input[name=?]', 'custom_field[default_value]'
  159. end
  160. def test_default_value_should_be_a_textarea_for_text_custom_field
  161. get :new, :params => {
  162. :type => 'IssueCustomField',
  163. :custom_field => {
  164. :field_format => 'text'
  165. }
  166. }
  167. assert_response :success
  168. assert_select 'textarea[name=?]', 'custom_field[default_value]'
  169. end
  170. def test_default_value_should_be_a_checkbox_for_bool_custom_field
  171. get :new, :params => {
  172. :type => 'IssueCustomField',
  173. :custom_field => {
  174. :field_format => 'bool'
  175. }
  176. }
  177. assert_response :success
  178. assert_select 'select[name=?]', 'custom_field[default_value]' do
  179. assert_select 'option', 3
  180. end
  181. end
  182. def test_default_value_should_not_be_present_for_user_custom_field
  183. get :new, :params => {
  184. :type => 'IssueCustomField',
  185. :custom_field => {
  186. :field_format => 'user'
  187. }
  188. }
  189. assert_response :success
  190. assert_select '[name=?]', 'custom_field[default_value]', 0
  191. end
  192. def test_setting_full_width_layout_shoul_be_present_only_for_long_text_issue_custom_field
  193. get :new, :params => {
  194. :type => 'IssueCustomField',
  195. :custom_field => {
  196. :field_format => 'text'
  197. }
  198. }
  199. assert_response :success
  200. assert_select '[name=?]', 'custom_field[full_width_layout]'
  201. get :new, :params => {
  202. :type => 'IssueCustomField',
  203. :custom_field => {
  204. :field_format => 'list'
  205. }
  206. }
  207. assert_response :success
  208. assert_select '[name=?]', 'custom_field[full_width_layout]', 0
  209. get :new, :params => {
  210. :type => 'TimeEntryCustomField',
  211. :custom_field => {
  212. :field_format => 'text'
  213. }
  214. }
  215. assert_response :success
  216. assert_select '[name=?]', 'custom_field[full_width_layout]', 0
  217. end
  218. def test_new_js
  219. get :new, :params => {
  220. :type => 'IssueCustomField',
  221. :custom_field => {
  222. :field_format => 'list'
  223. },
  224. :format => 'js'
  225. },
  226. :xhr => true
  227. assert_response :success
  228. assert_equal 'text/javascript', response.media_type
  229. assert_include '<option selected=\"selected\" value=\"list\">List<\/option>', response.body
  230. end
  231. def test_new_with_invalid_custom_field_class_should_render_select_type
  232. get :new, :params => {
  233. :type => 'UnknownCustomField'
  234. }
  235. assert_response :success
  236. assert_select 'input[type=radio][name=type]'
  237. end
  238. def test_create_list_custom_field
  239. field = new_record(IssueCustomField) do
  240. post :create, :params => {
  241. :type => "IssueCustomField",
  242. :custom_field => {
  243. :name => "test_post_new_list",
  244. :default_value => "",
  245. :min_length => "0",
  246. :searchable => "0",
  247. :regexp => "",
  248. :is_for_all => "1",
  249. :possible_values => "0.1\n0.2\n",
  250. :max_length => "0",
  251. :is_filter => "0",
  252. :is_required =>"0",
  253. :field_format => "list",
  254. :tracker_ids => ["1", ""]
  255. }
  256. }
  257. end
  258. assert_redirected_to "/custom_fields/#{field.id}/edit"
  259. assert_equal "test_post_new_list", field.name
  260. assert_equal ["0.1", "0.2"], field.possible_values
  261. assert_equal 1, field.trackers.size
  262. end
  263. def test_create_with_project_ids
  264. assert_difference 'CustomField.count' do
  265. post :create, :params => {
  266. :type => "IssueCustomField",
  267. :custom_field => {
  268. :name => "foo",
  269. :field_format => "string",
  270. :is_for_all => "0",
  271. :project_ids => ["1", "3", ""]
  272. }
  273. }
  274. assert_response 302
  275. end
  276. field = IssueCustomField.order("id desc").first
  277. assert_equal [1, 3], field.projects.map(&:id).sort
  278. end
  279. def test_create_with_continue_params
  280. assert_difference 'CustomField.count' do
  281. post :create, :params => {
  282. :type => 'IssueCustomField',
  283. :continue => 'Create and Continue',
  284. :custom_field => {
  285. :name => 'foo',
  286. :field_format => 'string'
  287. }
  288. }
  289. end
  290. assert_redirected_to '/custom_fields/new?type=IssueCustomField'
  291. end
  292. def test_create_with_failure
  293. assert_no_difference 'CustomField.count' do
  294. post :create, :params => {
  295. :type => "IssueCustomField",
  296. :custom_field => {
  297. :name => ''
  298. }
  299. }
  300. end
  301. assert_response :success
  302. assert_select_error /name cannot be blank/i
  303. end
  304. def test_create_without_type_should_render_select_type
  305. assert_no_difference 'CustomField.count' do
  306. post :create, :params => {
  307. :custom_field => {
  308. :name => ''
  309. }
  310. }
  311. end
  312. assert_response :success
  313. assert_select 'input[type=radio][name=type]'
  314. end
  315. def test_edit
  316. get :edit, :params => {
  317. :id => 1
  318. }
  319. assert_response :success
  320. assert_select 'input[name=?][value=?]', 'custom_field[name]', 'Database'
  321. end
  322. def test_edit_invalid_custom_field_should_render_404
  323. get :edit, :params => {
  324. :id => 99
  325. }
  326. assert_response 404
  327. end
  328. def test_update
  329. put :update, :params => {
  330. :id => 1,
  331. :custom_field => {
  332. :name => 'New name'
  333. }
  334. }
  335. assert_redirected_to '/custom_fields/1/edit'
  336. field = CustomField.find(1)
  337. assert_equal 'New name', field.name
  338. end
  339. def test_update_with_failure
  340. put :update, :params => {
  341. :id => 1,
  342. :custom_field => {
  343. :name => ''
  344. }
  345. }
  346. assert_response :success
  347. assert_select_error /name cannot be blank/i
  348. end
  349. def test_destroy
  350. custom_values_count = CustomValue.where(:custom_field_id => 1).count
  351. assert custom_values_count > 0
  352. assert_difference 'CustomField.count', -1 do
  353. assert_difference 'CustomValue.count', - custom_values_count do
  354. delete :destroy, :params => {
  355. :id => 1
  356. }
  357. end
  358. end
  359. assert_redirected_to '/custom_fields?tab=IssueCustomField'
  360. assert_nil CustomField.find_by_id(1)
  361. assert_nil CustomValue.find_by_custom_field_id(1)
  362. end
  363. def custom_field_classes
  364. files = Dir.glob(File.join(Rails.root, 'app/models/*_custom_field.rb')).map {|f| File.basename(f).sub(/\.rb$/, '') }
  365. classes = files.map(&:classify).map(&:constantize)
  366. assert classes.size > 0
  367. classes
  368. end
  369. end