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.

issue_categories_controller_test.rb 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2023 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 IssueCategoriesControllerTest < Redmine::ControllerTest
  20. fixtures :projects, :users, :members, :member_roles, :roles, :enabled_modules, :issue_categories,
  21. :issues
  22. def setup
  23. User.current = nil
  24. @request.session[:user_id] = 2
  25. end
  26. def test_new
  27. @request.session[:user_id] = 2 # manager
  28. get(:new, :params => {:project_id => '1'})
  29. assert_response :success
  30. assert_select 'input[name=?]', 'issue_category[name]'
  31. end
  32. def test_new_from_issue_form
  33. @request.session[:user_id] = 2 # manager
  34. get(
  35. :new,
  36. :params => {
  37. :project_id => '1'
  38. },
  39. :xhr => true
  40. )
  41. assert_response :success
  42. assert_equal 'text/javascript', response.media_type
  43. end
  44. def test_create
  45. @request.session[:user_id] = 2 # manager
  46. assert_difference 'IssueCategory.count' do
  47. post(
  48. :create,
  49. :params => {
  50. :project_id => '1',
  51. :issue_category => {
  52. :name => 'New category'
  53. }
  54. }
  55. )
  56. end
  57. assert_redirected_to '/projects/ecookbook/settings/categories'
  58. category = IssueCategory.find_by_name('New category')
  59. assert_not_nil category
  60. assert_equal 1, category.project_id
  61. end
  62. def test_create_failure
  63. @request.session[:user_id] = 2
  64. post(
  65. :create,
  66. :params => {
  67. :project_id => '1',
  68. :issue_category => {
  69. :name => ''
  70. }
  71. }
  72. )
  73. assert_response :success
  74. assert_select_error /Name cannot be blank/i
  75. end
  76. def test_create_from_issue_form
  77. @request.session[:user_id] = 2 # manager
  78. assert_difference 'IssueCategory.count' do
  79. post(
  80. :create,
  81. :params => {
  82. :project_id => '1',
  83. :issue_category => {
  84. :name => 'New category'
  85. }
  86. },
  87. :xhr => true
  88. )
  89. end
  90. category = IssueCategory.order('id DESC').first
  91. assert_equal 'New category', category.name
  92. assert_response :success
  93. assert_equal 'text/javascript', response.media_type
  94. end
  95. def test_create_from_issue_form_with_failure
  96. @request.session[:user_id] = 2 # manager
  97. assert_no_difference 'IssueCategory.count' do
  98. post(
  99. :create,
  100. :params => {
  101. :project_id => '1',
  102. :issue_category => {
  103. :name => ''
  104. }
  105. },
  106. :xhr => true
  107. )
  108. end
  109. assert_response :success
  110. assert_equal 'text/javascript', response.media_type
  111. assert_include 'Name cannot be blank', response.body
  112. end
  113. def test_edit
  114. @request.session[:user_id] = 2
  115. get(:edit, :params => {:id => 2})
  116. assert_response :success
  117. assert_select 'input[name=?][value=?]', 'issue_category[name]', 'Recipes'
  118. end
  119. def test_update
  120. assert_no_difference 'IssueCategory.count' do
  121. put(
  122. :update,
  123. :params => {
  124. :id => 2,
  125. :issue_category => {
  126. :name => 'Testing'
  127. }
  128. }
  129. )
  130. end
  131. assert_redirected_to '/projects/ecookbook/settings/categories'
  132. assert_equal 'Testing', IssueCategory.find(2).name
  133. end
  134. def test_update_failure
  135. put(
  136. :update,
  137. :params => {
  138. :id => 2,
  139. :issue_category => {
  140. :name => ''
  141. }
  142. }
  143. )
  144. assert_response :success
  145. assert_select_error /Name cannot be blank/i
  146. end
  147. def test_update_not_found
  148. put(
  149. :update,
  150. :params => {
  151. :id => 97,
  152. :issue_category => {
  153. :name => 'Testing'
  154. }
  155. }
  156. )
  157. assert_response 404
  158. end
  159. def test_destroy_category_not_in_use
  160. delete(:destroy, :params => {:id => 2})
  161. assert_redirected_to '/projects/ecookbook/settings/categories'
  162. assert_nil IssueCategory.find_by_id(2)
  163. end
  164. def test_destroy_category_in_use
  165. delete(:destroy, :params => {:id => 1})
  166. assert_response :success
  167. assert_not_nil IssueCategory.find_by_id(1)
  168. assert_select 'select[name=?]', 'reassign_to_id'
  169. end
  170. def test_destroy_category_in_use_with_reassignment
  171. issue = Issue.where(:category_id => 1).first
  172. delete(
  173. :destroy,
  174. :params => {
  175. :id => 1,
  176. :todo => 'reassign',
  177. :reassign_to_id => 2
  178. }
  179. )
  180. assert_redirected_to '/projects/ecookbook/settings/categories'
  181. assert_nil IssueCategory.find_by_id(1)
  182. # check that the issue was reassign
  183. assert_equal 2, issue.reload.category_id
  184. end
  185. def test_destroy_category_in_use_without_reassignment
  186. issue = Issue.where(:category_id => 1).first
  187. delete(
  188. :destroy,
  189. :params => {
  190. :id => 1,
  191. :todo => 'nullify'
  192. }
  193. )
  194. assert_redirected_to '/projects/ecookbook/settings/categories'
  195. assert_nil IssueCategory.find_by_id(1)
  196. # check that the issue category was nullified
  197. assert_nil issue.reload.category_id
  198. end
  199. end