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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2012 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. require 'issue_categories_controller'
  19. # Re-raise errors caught by the controller.
  20. class IssueCategoriesController; def rescue_action(e) raise e end; end
  21. class IssueCategoriesControllerTest < ActionController::TestCase
  22. fixtures :projects, :users, :members, :member_roles, :roles, :enabled_modules, :issue_categories
  23. def setup
  24. @controller = IssueCategoriesController.new
  25. @request = ActionController::TestRequest.new
  26. @response = ActionController::TestResponse.new
  27. User.current = nil
  28. @request.session[:user_id] = 2
  29. end
  30. def test_new
  31. @request.session[:user_id] = 2 # manager
  32. get :new, :project_id => '1'
  33. assert_response :success
  34. assert_template 'new'
  35. assert_select 'input[name=?]', 'issue_category[name]'
  36. end
  37. def test_create
  38. @request.session[:user_id] = 2 # manager
  39. assert_difference 'IssueCategory.count' do
  40. post :create, :project_id => '1', :issue_category => {:name => 'New category'}
  41. end
  42. assert_redirected_to '/projects/ecookbook/settings/categories'
  43. category = IssueCategory.find_by_name('New category')
  44. assert_not_nil category
  45. assert_equal 1, category.project_id
  46. end
  47. def test_create_failure
  48. @request.session[:user_id] = 2
  49. post :create, :project_id => '1', :issue_category => {:name => ''}
  50. assert_response :success
  51. assert_template 'new'
  52. end
  53. def test_create_from_issue_form
  54. @request.session[:user_id] = 2 # manager
  55. assert_difference 'IssueCategory.count' do
  56. xhr :post, :create, :project_id => '1', :issue_category => {:name => 'New category'}
  57. end
  58. category = IssueCategory.first(:order => 'id DESC')
  59. assert_equal 'New category', category.name
  60. assert_response :success
  61. assert_select_rjs :replace, 'issue_category_id' do
  62. assert_select "option[value=#{category.id}][selected=selected]"
  63. end
  64. end
  65. def test_create_from_issue_form_with_failure
  66. @request.session[:user_id] = 2 # manager
  67. assert_no_difference 'IssueCategory.count' do
  68. xhr :post, :create, :project_id => '1', :issue_category => {:name => ''}
  69. end
  70. assert_response :success
  71. assert_select_rjs :replace_html, "ajax-modal" do
  72. assert_select "div#errorExplanation"
  73. end
  74. end
  75. def test_edit
  76. @request.session[:user_id] = 2
  77. get :edit, :id => 2
  78. assert_response :success
  79. assert_template 'edit'
  80. assert_select 'input[name=?][value=?]', 'issue_category[name]', 'Recipes'
  81. end
  82. def test_update
  83. assert_no_difference 'IssueCategory.count' do
  84. put :update, :id => 2, :issue_category => { :name => 'Testing' }
  85. end
  86. assert_redirected_to '/projects/ecookbook/settings/categories'
  87. assert_equal 'Testing', IssueCategory.find(2).name
  88. end
  89. def test_update_failure
  90. put :update, :id => 2, :issue_category => { :name => '' }
  91. assert_response :success
  92. assert_template 'edit'
  93. end
  94. def test_update_not_found
  95. put :update, :id => 97, :issue_category => { :name => 'Testing' }
  96. assert_response 404
  97. end
  98. def test_destroy_category_not_in_use
  99. delete :destroy, :id => 2
  100. assert_redirected_to '/projects/ecookbook/settings/categories'
  101. assert_nil IssueCategory.find_by_id(2)
  102. end
  103. def test_destroy_category_in_use
  104. delete :destroy, :id => 1
  105. assert_response :success
  106. assert_template 'destroy'
  107. assert_not_nil IssueCategory.find_by_id(1)
  108. end
  109. def test_destroy_category_in_use_with_reassignment
  110. issue = Issue.find(:first, :conditions => {:category_id => 1})
  111. delete :destroy, :id => 1, :todo => 'reassign', :reassign_to_id => 2
  112. assert_redirected_to '/projects/ecookbook/settings/categories'
  113. assert_nil IssueCategory.find_by_id(1)
  114. # check that the issue was reassign
  115. assert_equal 2, issue.reload.category_id
  116. end
  117. def test_destroy_category_in_use_without_reassignment
  118. issue = Issue.find(:first, :conditions => {:category_id => 1})
  119. delete :destroy, :id => 1, :todo => 'nullify'
  120. assert_redirected_to '/projects/ecookbook/settings/categories'
  121. assert_nil IssueCategory.find_by_id(1)
  122. # check that the issue category was nullified
  123. assert_nil issue.reload.category_id
  124. end
  125. end