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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 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, :params => {
  49. :type => klass.name,
  50. :custom_field => {
  51. :field_format => format.name
  52. }
  53. }
  54. assert_response :success
  55. assert_select 'form#custom_field_form' do
  56. assert_select 'select[name=?]', 'custom_field[field_format]' do
  57. assert_select 'option[value=?][selected=selected]', format.name
  58. end
  59. assert_select 'input[type=hidden][name=type][value=?]', klass.name
  60. end
  61. end
  62. end
  63. end
  64. def test_new_should_have_string_default_format
  65. get :new, :params => {
  66. :type => 'IssueCustomField'
  67. }
  68. assert_response :success
  69. assert_select 'select[name=?]', 'custom_field[field_format]' do
  70. assert_select 'option[value=?][selected=selected]', 'string'
  71. end
  72. end
  73. def test_new_issue_custom_field
  74. get :new, :params => {
  75. :type => 'IssueCustomField'
  76. }
  77. assert_response :success
  78. assert_select 'form#custom_field_form' do
  79. assert_select 'select#custom_field_field_format[name=?]', 'custom_field[field_format]' do
  80. assert_select 'option[value=user]', :text => 'User'
  81. assert_select 'option[value=version]', :text => 'Version'
  82. end
  83. assert_select 'input[type=checkbox][name=?]', 'custom_field[project_ids][]', Project.count
  84. assert_select 'input[type=hidden][name=?]', 'custom_field[project_ids][]', 1
  85. assert_select 'input[type=hidden][name=type][value=IssueCustomField]'
  86. end
  87. end
  88. def test_new_time_entry_custom_field_should_not_show_trackers_and_projects
  89. get :new, :params => {
  90. :type => 'TimeEntryCustomField'
  91. }
  92. assert_response :success
  93. assert_select 'form#custom_field_form' do
  94. assert_select 'input[name=?]', 'custom_field[tracker_ids][]', 0
  95. assert_select 'input[name=?]', 'custom_field[project_ids][]', 0
  96. end
  97. end
  98. def test_default_value_should_be_an_input_for_string_custom_field
  99. get :new, :params => {
  100. :type => 'IssueCustomField',
  101. :custom_field => {
  102. :field_format => 'string'
  103. }
  104. }
  105. assert_response :success
  106. assert_select 'input[name=?]', 'custom_field[default_value]'
  107. end
  108. def test_default_value_should_be_a_textarea_for_text_custom_field
  109. get :new, :params => {
  110. :type => 'IssueCustomField',
  111. :custom_field => {
  112. :field_format => 'text'
  113. }
  114. }
  115. assert_response :success
  116. assert_select 'textarea[name=?]', 'custom_field[default_value]'
  117. end
  118. def test_default_value_should_be_a_checkbox_for_bool_custom_field
  119. get :new, :params => {
  120. :type => 'IssueCustomField',
  121. :custom_field => {
  122. :field_format => 'bool'
  123. }
  124. }
  125. assert_response :success
  126. assert_select 'select[name=?]', 'custom_field[default_value]' do
  127. assert_select 'option', 3
  128. end
  129. end
  130. def test_default_value_should_not_be_present_for_user_custom_field
  131. get :new, :params => {
  132. :type => 'IssueCustomField',
  133. :custom_field => {
  134. :field_format => 'user'
  135. }
  136. }
  137. assert_response :success
  138. assert_select '[name=?]', 'custom_field[default_value]', 0
  139. end
  140. def test_setting_full_width_layout_shoul_be_present_only_for_long_text_issue_custom_field
  141. get :new, :params => {
  142. :type => 'IssueCustomField',
  143. :custom_field => {
  144. :field_format => 'text'
  145. }
  146. }
  147. assert_response :success
  148. assert_select '[name=?]', 'custom_field[full_width_layout]'
  149. get :new, :params => {
  150. :type => 'IssueCustomField',
  151. :custom_field => {
  152. :field_format => 'list'
  153. }
  154. }
  155. assert_response :success
  156. assert_select '[name=?]', 'custom_field[full_width_layout]', 0
  157. get :new, :params => {
  158. :type => 'TimeEntryCustomField',
  159. :custom_field => {
  160. :field_format => 'text'
  161. }
  162. }
  163. assert_response :success
  164. assert_select '[name=?]', 'custom_field[full_width_layout]', 0
  165. end
  166. def test_new_js
  167. get :new, :params => {
  168. :type => 'IssueCustomField',
  169. :custom_field => {
  170. :field_format => 'list'
  171. },
  172. :format => 'js'
  173. },
  174. :xhr => true
  175. assert_response :success
  176. assert_equal 'text/javascript', response.content_type
  177. assert_include '<option selected=\"selected\" value=\"list\">List<\/option>', response.body
  178. end
  179. def test_new_with_invalid_custom_field_class_should_render_select_type
  180. get :new, :params => {
  181. :type => 'UnknownCustomField'
  182. }
  183. assert_response :success
  184. assert_select 'input[type=radio][name=type]'
  185. end
  186. def test_create_list_custom_field
  187. field = new_record(IssueCustomField) do
  188. post :create, :params => {
  189. :type => "IssueCustomField",
  190. :custom_field => {
  191. :name => "test_post_new_list",
  192. :default_value => "",
  193. :min_length => "0",
  194. :searchable => "0",
  195. :regexp => "",
  196. :is_for_all => "1",
  197. :possible_values => "0.1\n0.2\n",
  198. :max_length => "0",
  199. :is_filter => "0",
  200. :is_required =>"0",
  201. :field_format => "list",
  202. :tracker_ids => ["1", ""]
  203. }
  204. }
  205. end
  206. assert_redirected_to "/custom_fields/#{field.id}/edit"
  207. assert_equal "test_post_new_list", field.name
  208. assert_equal ["0.1", "0.2"], field.possible_values
  209. assert_equal 1, field.trackers.size
  210. end
  211. def test_create_with_project_ids
  212. assert_difference 'CustomField.count' do
  213. post :create, :params => {
  214. :type => "IssueCustomField",
  215. :custom_field => {
  216. :name => "foo",
  217. :field_format => "string",
  218. :is_for_all => "0",
  219. :project_ids => ["1", "3", ""]
  220. }
  221. }
  222. assert_response 302
  223. end
  224. field = IssueCustomField.order("id desc").first
  225. assert_equal [1, 3], field.projects.map(&:id).sort
  226. end
  227. def test_create_with_failure
  228. assert_no_difference 'CustomField.count' do
  229. post :create, :params => {
  230. :type => "IssueCustomField",
  231. :custom_field => {
  232. :name => ''
  233. }
  234. }
  235. end
  236. assert_response :success
  237. assert_select_error /name cannot be blank/i
  238. end
  239. def test_create_without_type_should_render_select_type
  240. assert_no_difference 'CustomField.count' do
  241. post :create, :params => {
  242. :custom_field => {
  243. :name => ''
  244. }
  245. }
  246. end
  247. assert_response :success
  248. assert_select 'input[type=radio][name=type]'
  249. end
  250. def test_edit
  251. get :edit, :params => {
  252. :id => 1
  253. }
  254. assert_response :success
  255. assert_select 'input[name=?][value=?]', 'custom_field[name]', 'Database'
  256. end
  257. def test_edit_invalid_custom_field_should_render_404
  258. get :edit, :params => {
  259. :id => 99
  260. }
  261. assert_response 404
  262. end
  263. def test_update
  264. put :update, :params => {
  265. :id => 1,
  266. :custom_field => {
  267. :name => 'New name'
  268. }
  269. }
  270. assert_redirected_to '/custom_fields/1/edit'
  271. field = CustomField.find(1)
  272. assert_equal 'New name', field.name
  273. end
  274. def test_update_with_failure
  275. put :update, :params => {
  276. :id => 1,
  277. :custom_field => {
  278. :name => ''
  279. }
  280. }
  281. assert_response :success
  282. assert_select_error /name cannot be blank/i
  283. end
  284. def test_destroy
  285. custom_values_count = CustomValue.where(:custom_field_id => 1).count
  286. assert custom_values_count > 0
  287. assert_difference 'CustomField.count', -1 do
  288. assert_difference 'CustomValue.count', - custom_values_count do
  289. delete :destroy, :params => {
  290. :id => 1
  291. }
  292. end
  293. end
  294. assert_redirected_to '/custom_fields?tab=IssueCustomField'
  295. assert_nil CustomField.find_by_id(1)
  296. assert_nil CustomValue.find_by_custom_field_id(1)
  297. end
  298. def custom_field_classes
  299. files = Dir.glob(File.join(Rails.root, 'app/models/*_custom_field.rb')).map {|f| File.basename(f).sub(/\.rb$/, '') }
  300. classes = files.map(&:classify).map(&:constantize)
  301. assert classes.size > 0
  302. classes
  303. end
  304. end