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

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