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.

enumerations_controller_test.rb 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006- 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_relative '../test_helper'
  19. class EnumerationsControllerTest < Redmine::ControllerTest
  20. fixtures :enumerations, :issues, :users
  21. def setup
  22. @request.session[:user_id] = 1 # admin
  23. end
  24. def test_index
  25. get :index
  26. assert_response :success
  27. assert_select 'table.enumerations'
  28. end
  29. def test_index_should_require_admin
  30. @request.session[:user_id] = nil
  31. get :index
  32. assert_response 302
  33. end
  34. def test_new
  35. get(:new, :params => {:type => 'IssuePriority'})
  36. assert_response :success
  37. assert_select 'input[name=?][value=?]', 'enumeration[type]', 'IssuePriority'
  38. assert_select 'input[name=?]', 'enumeration[name]'
  39. end
  40. def test_new_with_invalid_type_should_respond_with_404
  41. get(:new, :params => {:type => 'UnknownType'})
  42. assert_response 404
  43. end
  44. def test_create
  45. assert_difference 'IssuePriority.count' do
  46. post(
  47. :create,
  48. :params => {
  49. :enumeration => {
  50. :type => 'IssuePriority',
  51. :name => 'Lowest'
  52. }
  53. }
  54. )
  55. end
  56. assert_redirected_to '/enumerations'
  57. e = IssuePriority.find_by_name('Lowest')
  58. assert_not_nil e
  59. end
  60. def test_create_with_custom_field_values
  61. custom_field = TimeEntryActivityCustomField.generate!
  62. assert_difference 'TimeEntryActivity.count' do
  63. post(
  64. :create,
  65. :params => {
  66. :enumeration => {
  67. :type => 'TimeEntryActivity',
  68. :name => 'Sample',
  69. :custom_field_values => {custom_field.id.to_s => "sample"}
  70. }
  71. }
  72. )
  73. end
  74. assert_redirected_to '/enumerations'
  75. assert_equal "sample", Enumeration.find_by(:name => 'Sample').custom_field_values.last.value
  76. end
  77. def test_create_with_multiple_select_list_custom_fields
  78. custom_field = IssuePriorityCustomField.generate!(:field_format => 'list', :multiple => true, :possible_values => ['1', '2', '3', '4'])
  79. assert_difference 'IssuePriority.count' do
  80. post(
  81. :create,
  82. :params => {
  83. :enumeration => {
  84. :type => 'IssuePriority',
  85. :name => 'Sample',
  86. :custom_field_values => {custom_field.id.to_s => ['1', '2']}
  87. }
  88. }
  89. )
  90. end
  91. assert_redirected_to '/enumerations'
  92. assert_equal ['1', '2'].sort, Enumeration.find_by(:name => 'Sample').custom_field_values.last.value.sort
  93. end
  94. def test_create_with_failure
  95. assert_no_difference 'IssuePriority.count' do
  96. post(
  97. :create,
  98. :params => {
  99. :enumeration => {
  100. :type => 'IssuePriority',
  101. :name => ''
  102. }
  103. }
  104. )
  105. end
  106. assert_response :success
  107. assert_select_error /name cannot be blank/i
  108. end
  109. def test_edit
  110. get(:edit, :params => {:id => 6})
  111. assert_response :success
  112. assert_select 'input[name=?][value=?]', 'enumeration[name]', 'High'
  113. end
  114. def test_edit_invalid_should_respond_with_404
  115. get(:edit, :params => {:id => 999})
  116. assert_response 404
  117. end
  118. def test_update
  119. assert_no_difference 'IssuePriority.count' do
  120. put(
  121. :update,
  122. :params => {
  123. :id => 6,
  124. :enumeration => {
  125. :type => 'IssuePriority',
  126. :name => 'New name'
  127. }
  128. }
  129. )
  130. end
  131. assert_redirected_to '/enumerations'
  132. e = IssuePriority.find(6)
  133. assert_equal 'New name', e.name
  134. end
  135. def test_update_with_failure
  136. assert_no_difference 'IssuePriority.count' do
  137. put(
  138. :update,
  139. :params => {
  140. :id => 6,
  141. :enumeration => {
  142. :type => 'IssuePriority',
  143. :name => ''
  144. }
  145. }
  146. )
  147. end
  148. assert_response :success
  149. assert_select_error /name cannot be blank/i
  150. end
  151. def test_update_position
  152. assert_equal 2, Enumeration.find(2).position
  153. put(
  154. :update,
  155. :params => {
  156. :id => 2,
  157. :enumeration => {
  158. :position => 1,
  159. }
  160. }
  161. )
  162. assert_response 302
  163. assert_equal 1, Enumeration.find(2).position
  164. end
  165. def test_update_custom_field_values
  166. custom_field = TimeEntryActivityCustomField.generate!
  167. enumeration = Enumeration.find(9)
  168. assert_nil enumeration.custom_field_values.last.value
  169. put(
  170. :update,
  171. :params => {
  172. :id => enumeration.id,
  173. :enumeration => {
  174. :custom_field_values => {custom_field.id.to_s => "sample"}
  175. }
  176. }
  177. )
  178. assert_response 302
  179. assert_equal "sample", enumeration.reload.custom_field_values.last.value
  180. end
  181. def test_destroy_enumeration_not_in_use
  182. assert_difference 'IssuePriority.count', -1 do
  183. delete(:destroy, :params => {:id => 7})
  184. end
  185. assert_redirected_to :controller => 'enumerations', :action => 'index'
  186. assert_nil Enumeration.find_by_id(7)
  187. end
  188. def test_destroy_enumeration_in_use
  189. assert_no_difference 'IssuePriority.count' do
  190. delete(:destroy, :params => {:id => 4})
  191. end
  192. assert_response :success
  193. assert_not_nil Enumeration.find_by_id(4)
  194. assert_select 'select[name=reassign_to_id]' do
  195. assert_select 'option[value="6"]', :text => 'High'
  196. end
  197. end
  198. def test_destroy_enumeration_in_use_with_reassignment
  199. issue = Issue.where(:priority_id => 4).first
  200. assert_difference 'IssuePriority.count', -1 do
  201. delete(:destroy, :params => {:id => 4, :reassign_to_id => 6})
  202. end
  203. assert_redirected_to :controller => 'enumerations', :action => 'index'
  204. assert_nil Enumeration.find_by_id(4)
  205. # check that the issue was reassign
  206. assert_equal 6, issue.reload.priority_id
  207. end
  208. def test_destroy_enumeration_in_use_with_blank_reassignment
  209. assert_no_difference 'IssuePriority.count' do
  210. delete(:destroy, :params => {:id => 4, :reassign_to_id => ''})
  211. end
  212. assert_response :success
  213. end
  214. end