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.

workflows_controller_test.rb 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2011 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 'workflows_controller'
  19. # Re-raise errors caught by the controller.
  20. class WorkflowsController; def rescue_action(e) raise e end; end
  21. class WorkflowsControllerTest < ActionController::TestCase
  22. fixtures :roles, :trackers, :workflows, :users, :issue_statuses
  23. def setup
  24. @controller = WorkflowsController.new
  25. @request = ActionController::TestRequest.new
  26. @response = ActionController::TestResponse.new
  27. User.current = nil
  28. @request.session[:user_id] = 1 # admin
  29. end
  30. def test_index
  31. get :index
  32. assert_response :success
  33. assert_template 'index'
  34. count = Workflow.count(:all, :conditions => 'role_id = 1 AND tracker_id = 2')
  35. assert_tag :tag => 'a', :content => count.to_s,
  36. :attributes => { :href => '/workflows/edit?role_id=1&amp;tracker_id=2' }
  37. end
  38. def test_get_edit
  39. get :edit
  40. assert_response :success
  41. assert_template 'edit'
  42. assert_not_nil assigns(:roles)
  43. assert_not_nil assigns(:trackers)
  44. end
  45. def test_get_edit_with_role_and_tracker
  46. Workflow.delete_all
  47. Workflow.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 2, :new_status_id => 3)
  48. Workflow.create!(:role_id => 2, :tracker_id => 1, :old_status_id => 3, :new_status_id => 5)
  49. get :edit, :role_id => 2, :tracker_id => 1
  50. assert_response :success
  51. assert_template 'edit'
  52. # used status only
  53. assert_not_nil assigns(:statuses)
  54. assert_equal [2, 3, 5], assigns(:statuses).collect(&:id)
  55. # allowed transitions
  56. assert_tag :tag => 'input', :attributes => { :type => 'checkbox',
  57. :name => 'issue_status[3][5][]',
  58. :value => 'always',
  59. :checked => 'checked' }
  60. # not allowed
  61. assert_tag :tag => 'input', :attributes => { :type => 'checkbox',
  62. :name => 'issue_status[3][2][]',
  63. :value => 'always',
  64. :checked => nil }
  65. # unused
  66. assert_no_tag :tag => 'input', :attributes => { :type => 'checkbox',
  67. :name => 'issue_status[1][1][]' }
  68. end
  69. def test_get_edit_with_role_and_tracker_and_all_statuses
  70. Workflow.delete_all
  71. get :edit, :role_id => 2, :tracker_id => 1, :used_statuses_only => '0'
  72. assert_response :success
  73. assert_template 'edit'
  74. assert_not_nil assigns(:statuses)
  75. assert_equal IssueStatus.count, assigns(:statuses).size
  76. assert_tag :tag => 'input', :attributes => { :type => 'checkbox',
  77. :name => 'issue_status[1][1][]',
  78. :value => 'always',
  79. :checked => nil }
  80. end
  81. def test_post_edit
  82. post :edit, :role_id => 2, :tracker_id => 1,
  83. :issue_status => {
  84. '4' => {'5' => ['always']},
  85. '3' => {'1' => ['always'], '2' => ['always']}
  86. }
  87. assert_redirected_to '/workflows/edit?role_id=2&tracker_id=1'
  88. assert_equal 3, Workflow.count(:conditions => {:tracker_id => 1, :role_id => 2})
  89. assert_not_nil Workflow.find(:first, :conditions => {:role_id => 2, :tracker_id => 1, :old_status_id => 3, :new_status_id => 2})
  90. assert_nil Workflow.find(:first, :conditions => {:role_id => 2, :tracker_id => 1, :old_status_id => 5, :new_status_id => 4})
  91. end
  92. def test_post_edit_with_additional_transitions
  93. post :edit, :role_id => 2, :tracker_id => 1,
  94. :issue_status => {
  95. '4' => {'5' => ['always']},
  96. '3' => {'1' => ['author'], '2' => ['assignee'], '4' => ['author', 'assignee']}
  97. }
  98. assert_redirected_to '/workflows/edit?role_id=2&tracker_id=1'
  99. assert_equal 4, Workflow.count(:conditions => {:tracker_id => 1, :role_id => 2})
  100. w = Workflow.find(:first, :conditions => {:role_id => 2, :tracker_id => 1, :old_status_id => 4, :new_status_id => 5})
  101. assert ! w.author
  102. assert ! w.assignee
  103. w = Workflow.find(:first, :conditions => {:role_id => 2, :tracker_id => 1, :old_status_id => 3, :new_status_id => 1})
  104. assert w.author
  105. assert ! w.assignee
  106. w = Workflow.find(:first, :conditions => {:role_id => 2, :tracker_id => 1, :old_status_id => 3, :new_status_id => 2})
  107. assert ! w.author
  108. assert w.assignee
  109. w = Workflow.find(:first, :conditions => {:role_id => 2, :tracker_id => 1, :old_status_id => 3, :new_status_id => 4})
  110. assert w.author
  111. assert w.assignee
  112. end
  113. def test_clear_workflow
  114. assert Workflow.count(:conditions => {:tracker_id => 1, :role_id => 2}) > 0
  115. post :edit, :role_id => 2, :tracker_id => 1
  116. assert_equal 0, Workflow.count(:conditions => {:tracker_id => 1, :role_id => 2})
  117. end
  118. def test_get_copy
  119. get :copy
  120. assert_response :success
  121. assert_template 'copy'
  122. end
  123. def test_post_copy_one_to_one
  124. source_transitions = status_transitions(:tracker_id => 1, :role_id => 2)
  125. post :copy, :source_tracker_id => '1', :source_role_id => '2',
  126. :target_tracker_ids => ['3'], :target_role_ids => ['1']
  127. assert_response 302
  128. assert_equal source_transitions, status_transitions(:tracker_id => 3, :role_id => 1)
  129. end
  130. def test_post_copy_one_to_many
  131. source_transitions = status_transitions(:tracker_id => 1, :role_id => 2)
  132. post :copy, :source_tracker_id => '1', :source_role_id => '2',
  133. :target_tracker_ids => ['2', '3'], :target_role_ids => ['1', '3']
  134. assert_response 302
  135. assert_equal source_transitions, status_transitions(:tracker_id => 2, :role_id => 1)
  136. assert_equal source_transitions, status_transitions(:tracker_id => 3, :role_id => 1)
  137. assert_equal source_transitions, status_transitions(:tracker_id => 2, :role_id => 3)
  138. assert_equal source_transitions, status_transitions(:tracker_id => 3, :role_id => 3)
  139. end
  140. def test_post_copy_many_to_many
  141. source_t2 = status_transitions(:tracker_id => 2, :role_id => 2)
  142. source_t3 = status_transitions(:tracker_id => 3, :role_id => 2)
  143. post :copy, :source_tracker_id => 'any', :source_role_id => '2',
  144. :target_tracker_ids => ['2', '3'], :target_role_ids => ['1', '3']
  145. assert_response 302
  146. assert_equal source_t2, status_transitions(:tracker_id => 2, :role_id => 1)
  147. assert_equal source_t3, status_transitions(:tracker_id => 3, :role_id => 1)
  148. assert_equal source_t2, status_transitions(:tracker_id => 2, :role_id => 3)
  149. assert_equal source_t3, status_transitions(:tracker_id => 3, :role_id => 3)
  150. end
  151. # Returns an array of status transitions that can be compared
  152. def status_transitions(conditions)
  153. Workflow.find(:all, :conditions => conditions,
  154. :order => 'tracker_id, role_id, old_status_id, new_status_id').collect {|w| [w.old_status, w.new_status_id]}
  155. end
  156. end