You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

workflows_controller_test.rb 14 kB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2013 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 WorkflowsControllerTest < ActionController::TestCase
  19. fixtures :roles, :trackers, :workflows, :users, :issue_statuses
  20. def setup
  21. User.current = nil
  22. @request.session[:user_id] = 1 # admin
  23. end
  24. def test_index
  25. get :index
  26. assert_response :success
  27. assert_template 'index'
  28. count = WorkflowTransition.count(:all, :conditions => 'role_id = 1 AND tracker_id = 2')
  29. assert_tag :tag => 'a', :content => count.to_s,
  30. :attributes => { :href => '/workflows/edit?role_id=1&amp;tracker_id=2' }
  31. end
  32. def test_get_edit
  33. get :edit
  34. assert_response :success
  35. assert_template 'edit'
  36. assert_not_nil assigns(:roles)
  37. assert_not_nil assigns(:trackers)
  38. end
  39. def test_get_edit_with_role_and_tracker
  40. WorkflowTransition.delete_all
  41. WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 2, :new_status_id => 3)
  42. WorkflowTransition.create!(:role_id => 2, :tracker_id => 1, :old_status_id => 3, :new_status_id => 5)
  43. get :edit, :role_id => 2, :tracker_id => 1
  44. assert_response :success
  45. assert_template 'edit'
  46. # used status only
  47. assert_not_nil assigns(:statuses)
  48. assert_equal [2, 3, 5], assigns(:statuses).collect(&:id)
  49. # allowed transitions
  50. assert_tag :tag => 'input', :attributes => { :type => 'checkbox',
  51. :name => 'issue_status[3][5][]',
  52. :value => 'always',
  53. :checked => 'checked' }
  54. # not allowed
  55. assert_tag :tag => 'input', :attributes => { :type => 'checkbox',
  56. :name => 'issue_status[3][2][]',
  57. :value => 'always',
  58. :checked => nil }
  59. # unused
  60. assert_no_tag :tag => 'input', :attributes => { :type => 'checkbox',
  61. :name => 'issue_status[1][1][]' }
  62. end
  63. def test_get_edit_with_role_and_tracker_and_all_statuses
  64. WorkflowTransition.delete_all
  65. get :edit, :role_id => 2, :tracker_id => 1, :used_statuses_only => '0'
  66. assert_response :success
  67. assert_template 'edit'
  68. assert_not_nil assigns(:statuses)
  69. assert_equal IssueStatus.count, assigns(:statuses).size
  70. assert_tag :tag => 'input', :attributes => { :type => 'checkbox',
  71. :name => 'issue_status[1][1][]',
  72. :value => 'always',
  73. :checked => nil }
  74. end
  75. def test_post_edit
  76. post :edit, :role_id => 2, :tracker_id => 1,
  77. :issue_status => {
  78. '4' => {'5' => ['always']},
  79. '3' => {'1' => ['always'], '2' => ['always']}
  80. }
  81. assert_redirected_to '/workflows/edit?role_id=2&tracker_id=1'
  82. assert_equal 3, WorkflowTransition.where(:tracker_id => 1, :role_id => 2).count
  83. assert_not_nil WorkflowTransition.where(:role_id => 2, :tracker_id => 1, :old_status_id => 3, :new_status_id => 2).first
  84. assert_nil WorkflowTransition.where(:role_id => 2, :tracker_id => 1, :old_status_id => 5, :new_status_id => 4).first
  85. end
  86. def test_post_edit_with_additional_transitions
  87. post :edit, :role_id => 2, :tracker_id => 1,
  88. :issue_status => {
  89. '4' => {'5' => ['always']},
  90. '3' => {'1' => ['author'], '2' => ['assignee'], '4' => ['author', 'assignee']}
  91. }
  92. assert_redirected_to '/workflows/edit?role_id=2&tracker_id=1'
  93. assert_equal 4, WorkflowTransition.where(:tracker_id => 1, :role_id => 2).count
  94. w = WorkflowTransition.where(:role_id => 2, :tracker_id => 1, :old_status_id => 4, :new_status_id => 5).first
  95. assert ! w.author
  96. assert ! w.assignee
  97. w = WorkflowTransition.where(:role_id => 2, :tracker_id => 1, :old_status_id => 3, :new_status_id => 1).first
  98. assert w.author
  99. assert ! w.assignee
  100. w = WorkflowTransition.where(:role_id => 2, :tracker_id => 1, :old_status_id => 3, :new_status_id => 2).first
  101. assert ! w.author
  102. assert w.assignee
  103. w = WorkflowTransition.where(:role_id => 2, :tracker_id => 1, :old_status_id => 3, :new_status_id => 4).first
  104. assert w.author
  105. assert w.assignee
  106. end
  107. def test_clear_workflow
  108. assert WorkflowTransition.count(:conditions => {:tracker_id => 1, :role_id => 2}) > 0
  109. post :edit, :role_id => 2, :tracker_id => 1
  110. assert_equal 0, WorkflowTransition.count(:conditions => {:tracker_id => 1, :role_id => 2})
  111. end
  112. def test_get_permissions
  113. get :permissions
  114. assert_response :success
  115. assert_template 'permissions'
  116. assert_not_nil assigns(:roles)
  117. assert_not_nil assigns(:trackers)
  118. end
  119. def test_get_permissions_with_role_and_tracker
  120. WorkflowPermission.delete_all
  121. WorkflowPermission.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 2, :field_name => 'assigned_to_id', :rule => 'required')
  122. WorkflowPermission.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 2, :field_name => 'fixed_version_id', :rule => 'required')
  123. WorkflowPermission.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 3, :field_name => 'fixed_version_id', :rule => 'readonly')
  124. get :permissions, :role_id => 1, :tracker_id => 2
  125. assert_response :success
  126. assert_template 'permissions'
  127. assert_select 'input[name=role_id][value=1]'
  128. assert_select 'input[name=tracker_id][value=2]'
  129. # Required field
  130. assert_select 'select[name=?]', 'permissions[assigned_to_id][2]' do
  131. assert_select 'option[value=]'
  132. assert_select 'option[value=][selected=selected]', 0
  133. assert_select 'option[value=readonly]', :text => 'Read-only'
  134. assert_select 'option[value=readonly][selected=selected]', 0
  135. assert_select 'option[value=required]', :text => 'Required'
  136. assert_select 'option[value=required][selected=selected]'
  137. end
  138. # Read-only field
  139. assert_select 'select[name=?]', 'permissions[fixed_version_id][3]' do
  140. assert_select 'option[value=]'
  141. assert_select 'option[value=][selected=selected]', 0
  142. assert_select 'option[value=readonly]', :text => 'Read-only'
  143. assert_select 'option[value=readonly][selected=selected]'
  144. assert_select 'option[value=required]', :text => 'Required'
  145. assert_select 'option[value=required][selected=selected]', 0
  146. end
  147. # Other field
  148. assert_select 'select[name=?]', 'permissions[due_date][3]' do
  149. assert_select 'option[value=]'
  150. assert_select 'option[value=][selected=selected]', 0
  151. assert_select 'option[value=readonly]', :text => 'Read-only'
  152. assert_select 'option[value=readonly][selected=selected]', 0
  153. assert_select 'option[value=required]', :text => 'Required'
  154. assert_select 'option[value=required][selected=selected]', 0
  155. end
  156. end
  157. def test_get_permissions_with_required_custom_field_should_not_show_required_option
  158. cf = IssueCustomField.create!(:name => 'Foo', :field_format => 'string', :tracker_ids => [1], :is_required => true)
  159. get :permissions, :role_id => 1, :tracker_id => 1
  160. assert_response :success
  161. assert_template 'permissions'
  162. # Custom field that is always required
  163. # The default option is "(Required)"
  164. assert_select 'select[name=?]', "permissions[#{cf.id}][3]" do
  165. assert_select 'option[value=]'
  166. assert_select 'option[value=readonly]', :text => 'Read-only'
  167. assert_select 'option[value=required]', 0
  168. end
  169. end
  170. def test_get_permissions_with_role_and_tracker_and_all_statuses
  171. WorkflowTransition.delete_all
  172. get :permissions, :role_id => 1, :tracker_id => 2, :used_statuses_only => '0'
  173. assert_response :success
  174. assert_equal IssueStatus.sorted.all, assigns(:statuses)
  175. end
  176. def test_post_permissions
  177. WorkflowPermission.delete_all
  178. post :permissions, :role_id => 1, :tracker_id => 2, :permissions => {
  179. 'assigned_to_id' => {'1' => '', '2' => 'readonly', '3' => ''},
  180. 'fixed_version_id' => {'1' => 'required', '2' => 'readonly', '3' => ''},
  181. 'due_date' => {'1' => '', '2' => '', '3' => ''},
  182. }
  183. assert_redirected_to '/workflows/permissions?role_id=1&tracker_id=2'
  184. workflows = WorkflowPermission.all
  185. assert_equal 3, workflows.size
  186. workflows.each do |workflow|
  187. assert_equal 1, workflow.role_id
  188. assert_equal 2, workflow.tracker_id
  189. end
  190. assert workflows.detect {|wf| wf.old_status_id == 2 && wf.field_name == 'assigned_to_id' && wf.rule == 'readonly'}
  191. assert workflows.detect {|wf| wf.old_status_id == 1 && wf.field_name == 'fixed_version_id' && wf.rule == 'required'}
  192. assert workflows.detect {|wf| wf.old_status_id == 2 && wf.field_name == 'fixed_version_id' && wf.rule == 'readonly'}
  193. end
  194. def test_post_permissions_should_clear_permissions
  195. WorkflowPermission.delete_all
  196. WorkflowPermission.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 2, :field_name => 'assigned_to_id', :rule => 'required')
  197. WorkflowPermission.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 2, :field_name => 'fixed_version_id', :rule => 'required')
  198. wf1 = WorkflowPermission.create!(:role_id => 1, :tracker_id => 3, :old_status_id => 2, :field_name => 'fixed_version_id', :rule => 'required')
  199. wf2 = WorkflowPermission.create!(:role_id => 2, :tracker_id => 2, :old_status_id => 3, :field_name => 'fixed_version_id', :rule => 'readonly')
  200. post :permissions, :role_id => 1, :tracker_id => 2
  201. assert_redirected_to '/workflows/permissions?role_id=1&tracker_id=2'
  202. workflows = WorkflowPermission.all
  203. assert_equal 2, workflows.size
  204. assert wf1.reload
  205. assert wf2.reload
  206. end
  207. def test_get_copy
  208. get :copy
  209. assert_response :success
  210. assert_template 'copy'
  211. assert_select 'select[name=source_tracker_id]' do
  212. assert_select 'option[value=1]', :text => 'Bug'
  213. end
  214. assert_select 'select[name=source_role_id]' do
  215. assert_select 'option[value=2]', :text => 'Developer'
  216. end
  217. assert_select 'select[name=?]', 'target_tracker_ids[]' do
  218. assert_select 'option[value=3]', :text => 'Support request'
  219. end
  220. assert_select 'select[name=?]', 'target_role_ids[]' do
  221. assert_select 'option[value=1]', :text => 'Manager'
  222. end
  223. end
  224. def test_post_copy_one_to_one
  225. source_transitions = status_transitions(:tracker_id => 1, :role_id => 2)
  226. post :copy, :source_tracker_id => '1', :source_role_id => '2',
  227. :target_tracker_ids => ['3'], :target_role_ids => ['1']
  228. assert_response 302
  229. assert_equal source_transitions, status_transitions(:tracker_id => 3, :role_id => 1)
  230. end
  231. def test_post_copy_one_to_many
  232. source_transitions = status_transitions(:tracker_id => 1, :role_id => 2)
  233. post :copy, :source_tracker_id => '1', :source_role_id => '2',
  234. :target_tracker_ids => ['2', '3'], :target_role_ids => ['1', '3']
  235. assert_response 302
  236. assert_equal source_transitions, status_transitions(:tracker_id => 2, :role_id => 1)
  237. assert_equal source_transitions, status_transitions(:tracker_id => 3, :role_id => 1)
  238. assert_equal source_transitions, status_transitions(:tracker_id => 2, :role_id => 3)
  239. assert_equal source_transitions, status_transitions(:tracker_id => 3, :role_id => 3)
  240. end
  241. def test_post_copy_many_to_many
  242. source_t2 = status_transitions(:tracker_id => 2, :role_id => 2)
  243. source_t3 = status_transitions(:tracker_id => 3, :role_id => 2)
  244. post :copy, :source_tracker_id => 'any', :source_role_id => '2',
  245. :target_tracker_ids => ['2', '3'], :target_role_ids => ['1', '3']
  246. assert_response 302
  247. assert_equal source_t2, status_transitions(:tracker_id => 2, :role_id => 1)
  248. assert_equal source_t3, status_transitions(:tracker_id => 3, :role_id => 1)
  249. assert_equal source_t2, status_transitions(:tracker_id => 2, :role_id => 3)
  250. assert_equal source_t3, status_transitions(:tracker_id => 3, :role_id => 3)
  251. end
  252. def test_post_copy_with_incomplete_source_specification_should_fail
  253. assert_no_difference 'WorkflowRule.count' do
  254. post :copy,
  255. :source_tracker_id => '', :source_role_id => '2',
  256. :target_tracker_ids => ['2', '3'], :target_role_ids => ['1', '3']
  257. assert_response 200
  258. assert_select 'div.flash.error', :text => 'Please select a source tracker or role'
  259. end
  260. end
  261. def test_post_copy_with_incomplete_target_specification_should_fail
  262. assert_no_difference 'WorkflowRule.count' do
  263. post :copy,
  264. :source_tracker_id => '1', :source_role_id => '2',
  265. :target_tracker_ids => ['2', '3']
  266. assert_response 200
  267. assert_select 'div.flash.error', :text => 'Please select target tracker(s) and role(s)'
  268. end
  269. end
  270. # Returns an array of status transitions that can be compared
  271. def status_transitions(conditions)
  272. WorkflowTransition.
  273. where(conditions).
  274. order('tracker_id, role_id, old_status_id, new_status_id').
  275. all.
  276. collect {|w| [w.old_status, w.new_status_id]}
  277. end
  278. end

软件技术创新是当今国家整体创新能力的重要基础和发展引擎。近年来,大众参与的软件创新与创业活动已经成为网络时代软件开发和应用的新形态,正在快速改变着全球软件创新模式和软件产业格局。 ...