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

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