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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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 WorkflowsControllerTest < Redmine::ControllerTest
  20. fixtures :roles, :trackers, :workflows, :users, :issue_statuses, :custom_fields
  21. def setup
  22. User.current = nil
  23. @request.session[:user_id] = 1 # admin
  24. end
  25. def test_index
  26. get :index
  27. assert_response :success
  28. count = WorkflowTransition.where(:role_id => 1, :tracker_id => 2).count
  29. assert_select 'a[href=?]', '/workflows/edit?role_id=1&tracker_id=2', :content => count.to_s
  30. end
  31. def test_get_edit
  32. get :edit
  33. assert_response :success
  34. end
  35. def test_get_edit_with_role_and_tracker
  36. WorkflowTransition.delete_all
  37. WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 2, :new_status_id => 3)
  38. WorkflowTransition.create!(:role_id => 2, :tracker_id => 1, :old_status_id => 3, :new_status_id => 5)
  39. get :edit, :params => {:role_id => 2, :tracker_id => 1}
  40. assert_response :success
  41. # used status only
  42. statuses = IssueStatus.where(:id => [2, 3, 5]).sorted.pluck(:name)
  43. assert_equal ["New issue"] + statuses,
  44. css_select('table.workflows.transitions-always tbody tr td:first').map(&:text).map(&:strip)
  45. # allowed transitions
  46. assert_select 'input[type=checkbox][name=?][value="1"][checked=checked]', 'transitions[3][5][always]'
  47. # not allowed
  48. assert_select 'input[type=checkbox][name=?][value="1"]:not([checked=checked])', 'transitions[3][2][always]'
  49. # unused
  50. assert_select 'input[type=checkbox][name=?]', 'transitions[1][1][always]', 0
  51. end
  52. def test_get_edit_with_role_and_tracker_should_not_include_statuses_from_roles_without_workflow_permissions
  53. WorkflowTransition.delete_all
  54. WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 2, :new_status_id => 3)
  55. reporter = Role.find(3)
  56. reporter.remove_permission! :edit_issues
  57. reporter.remove_permission! :add_issues
  58. assert !reporter.consider_workflow?
  59. WorkflowTransition.create!(:role_id => 3, :tracker_id => 1, :old_status_id => 1, :new_status_id => 5)
  60. get :edit, :params => {:role_id => 2, :tracker_id => 1}
  61. assert_response :success
  62. # statuses 1 and 5 not displayed
  63. statuses = IssueStatus.where(:id => [2, 3]).sorted.pluck(:name)
  64. assert_equal ["New issue"] + statuses,
  65. css_select('table.workflows.transitions-always tbody tr td:first').map(&:text).map(&:strip)
  66. end
  67. def test_get_edit_should_include_allowed_statuses_for_new_issues
  68. WorkflowTransition.delete_all
  69. WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 0, :new_status_id => 1)
  70. get :edit, :params => {:role_id => 1, :tracker_id => 1}
  71. assert_response :success
  72. assert_select 'td', 'New issue'
  73. assert_select 'input[type=checkbox][name=?][value="1"][checked=checked]', 'transitions[0][1][always]'
  74. end
  75. def test_get_edit_with_all_roles_and_all_trackers
  76. get :edit, :params => {:role_id => 'all', :tracker_id => 'all'}
  77. assert_response :success
  78. assert_select 'select[name=?][multiple=multiple]', 'role_id[]' do
  79. assert_select 'option[selected=selected]', Role.all.select(&:consider_workflow?).count
  80. end
  81. assert_select 'select[name=?]', 'tracker_id[]' do
  82. assert_select 'option[selected=selected][value=all]'
  83. end
  84. end
  85. def test_get_edit_with_role_and_tracker_and_all_statuses
  86. WorkflowTransition.delete_all
  87. get :edit, :params => {:role_id => 2, :tracker_id => 1, :used_statuses_only => '0'}
  88. assert_response :success
  89. statuses = IssueStatus.all.sorted.pluck(:name)
  90. assert_equal ["New issue"] + statuses,
  91. css_select('table.workflows.transitions-always tbody tr td:first').map(&:text).map(&:strip)
  92. assert_select 'input[type=checkbox][name=?]', 'transitions[0][1][always]'
  93. end
  94. def test_get_edit_should_show_checked_disabled_transition_checkbox_between_same_statuses
  95. get :edit, :params => {:role_id => 2, :tracker_id => 1}
  96. assert_response :success
  97. assert_select 'table.workflows.transitions-always tbody tr:nth-child(2)' do
  98. assert_select 'td.name', :text => 'New'
  99. # assert that the td is enabled
  100. assert_select "td[title='New » New'][class=?]", 'enabled'
  101. # assert that the checkbox is disabled and checked
  102. assert_select "input[name='transitions[1][1][always]'][checked=?][disabled=?]", 'checked', 'disabled', 1
  103. end
  104. end
  105. def test_post_edit
  106. WorkflowTransition.delete_all
  107. post :edit, :params => {
  108. :role_id => 2,
  109. :tracker_id => 1,
  110. :transitions => {
  111. '4' => {'5' => {'always' => '1'}},
  112. '3' => {'1' => {'always' => '1'}, '2' => {'always' => '1'}}
  113. }
  114. }
  115. assert_response 302
  116. assert_equal 3, WorkflowTransition.where(:tracker_id => 1, :role_id => 2).count
  117. assert_not_nil WorkflowTransition.where(:role_id => 2, :tracker_id => 1, :old_status_id => 3, :new_status_id => 2).first
  118. assert_nil WorkflowTransition.where(:role_id => 2, :tracker_id => 1, :old_status_id => 5, :new_status_id => 4).first
  119. end
  120. def test_post_edit_with_allowed_statuses_for_new_issues
  121. WorkflowTransition.delete_all
  122. post :edit, :params => {
  123. :role_id => 2,
  124. :tracker_id => 1,
  125. :transitions => {
  126. '0' => {'1' => {'always' => '1'}, '2' => {'always' => '1'}}
  127. }
  128. }
  129. assert_response 302
  130. assert WorkflowTransition.where(:role_id => 2, :tracker_id => 1, :old_status_id => 0, :new_status_id => 1).any?
  131. assert WorkflowTransition.where(:role_id => 2, :tracker_id => 1, :old_status_id => 0, :new_status_id => 2).any?
  132. assert_equal 2, WorkflowTransition.where(:tracker_id => 1, :role_id => 2).count
  133. end
  134. def test_post_edit_with_additional_transitions
  135. WorkflowTransition.delete_all
  136. post :edit, :params => {
  137. :role_id => 2,
  138. :tracker_id => 1,
  139. :transitions => {
  140. '4' => {'5' => {'always' => '1', 'author' => '0', 'assignee' => '0'}},
  141. '3' => {'1' => {'always' => '0', 'author' => '1', 'assignee' => '0'},
  142. '2' => {'always' => '0', 'author' => '0', 'assignee' => '1'},
  143. '4' => {'always' => '0', 'author' => '1', 'assignee' => '1'}}
  144. }
  145. }
  146. assert_response 302
  147. assert_equal 4, WorkflowTransition.where(:tracker_id => 1, :role_id => 2).count
  148. w = WorkflowTransition.where(:role_id => 2, :tracker_id => 1, :old_status_id => 4, :new_status_id => 5).first
  149. assert ! w.author
  150. assert ! w.assignee
  151. w = WorkflowTransition.where(:role_id => 2, :tracker_id => 1, :old_status_id => 3, :new_status_id => 1).first
  152. assert w.author
  153. assert ! w.assignee
  154. w = WorkflowTransition.where(:role_id => 2, :tracker_id => 1, :old_status_id => 3, :new_status_id => 2).first
  155. assert ! w.author
  156. assert w.assignee
  157. w = WorkflowTransition.where(:role_id => 2, :tracker_id => 1, :old_status_id => 3, :new_status_id => 4).first
  158. assert w.author
  159. assert w.assignee
  160. end
  161. def test_get_permissions
  162. get :permissions
  163. assert_response :success
  164. end
  165. def test_get_permissions_with_role_and_tracker
  166. WorkflowPermission.delete_all
  167. WorkflowPermission.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 2, :field_name => 'assigned_to_id', :rule => 'required')
  168. WorkflowPermission.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 2, :field_name => 'fixed_version_id', :rule => 'required')
  169. WorkflowPermission.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 3, :field_name => 'fixed_version_id', :rule => 'readonly')
  170. get :permissions, :params => {:role_id => 1, :tracker_id => 2}
  171. assert_response :success
  172. assert_select 'input[name=?][value="1"]', 'role_id[]'
  173. assert_select 'input[name=?][value="2"]', 'tracker_id[]'
  174. # Required field
  175. assert_select 'select[name=?]', 'permissions[2][assigned_to_id]' do
  176. assert_select 'option[value=""]'
  177. assert_select 'option[value=""][selected=selected]', 0
  178. assert_select 'option[value=readonly]', :text => 'Read-only'
  179. assert_select 'option[value=readonly][selected=selected]', 0
  180. assert_select 'option[value=required]', :text => 'Required'
  181. assert_select 'option[value=required][selected=selected]'
  182. end
  183. # Read-only field
  184. assert_select 'select[name=?]', 'permissions[3][fixed_version_id]' do
  185. assert_select 'option[value=""]'
  186. assert_select 'option[value=""][selected=selected]', 0
  187. assert_select 'option[value=readonly]', :text => 'Read-only'
  188. assert_select 'option[value=readonly][selected=selected]'
  189. assert_select 'option[value=required]', :text => 'Required'
  190. assert_select 'option[value=required][selected=selected]', 0
  191. end
  192. # Other field
  193. assert_select 'select[name=?]', 'permissions[3][due_date]' do
  194. assert_select 'option[value=""]'
  195. assert_select 'option[value=""][selected=selected]', 0
  196. assert_select 'option[value=readonly]', :text => 'Read-only'
  197. assert_select 'option[value=readonly][selected=selected]', 0
  198. assert_select 'option[value=required]', :text => 'Required'
  199. assert_select 'option[value=required][selected=selected]', 0
  200. end
  201. end
  202. def test_get_permissions_with_required_custom_field_should_not_show_required_option
  203. cf = IssueCustomField.create!(:name => 'Foo', :field_format => 'string', :tracker_ids => [1], :is_required => true)
  204. get :permissions, :params => {:role_id => 1, :tracker_id => 1}
  205. assert_response :success
  206. # Custom field that is always required
  207. # The default option is "(Required)"
  208. assert_select 'select[name=?]', "permissions[3][#{cf.id}]" do
  209. assert_select 'option[value=""]'
  210. assert_select 'option[value=readonly]', :text => 'Read-only'
  211. assert_select 'option[value=required]', 0
  212. end
  213. end
  214. def test_get_permissions_should_disable_hidden_custom_fields
  215. cf1 = IssueCustomField.generate!(:tracker_ids => [1], :visible => true)
  216. cf2 = IssueCustomField.generate!(:tracker_ids => [1], :visible => false, :role_ids => [1])
  217. cf3 = IssueCustomField.generate!(:tracker_ids => [1], :visible => false, :role_ids => [1, 2])
  218. get :permissions, :params => {:role_id => 2, :tracker_id => 1}
  219. assert_response :success
  220. assert_select 'select[name=?]:not(.disabled)', "permissions[1][#{cf1.id}]"
  221. assert_select 'select[name=?]:not(.disabled)', "permissions[1][#{cf3.id}]"
  222. assert_select 'select[name=?][disabled=disabled]', "permissions[1][#{cf2.id}]" do
  223. assert_select 'option[value=""][selected=selected]', :text => 'Hidden'
  224. end
  225. end
  226. def test_get_permissions_with_missing_permissions_for_roles_should_default_to_no_change
  227. WorkflowPermission.delete_all
  228. WorkflowPermission.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 1, :field_name => 'assigned_to_id', :rule => 'required')
  229. get :permissions, :params => {:role_id => [1, 2], :tracker_id => 2}
  230. assert_response :success
  231. assert_select 'select[name=?]', 'permissions[1][assigned_to_id]' do
  232. assert_select 'option[selected]', 1
  233. assert_select 'option[selected][value=no_change]'
  234. end
  235. end
  236. def test_get_permissions_with_different_permissions_for_roles_should_default_to_no_change
  237. WorkflowPermission.delete_all
  238. WorkflowPermission.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 1, :field_name => 'assigned_to_id', :rule => 'required')
  239. WorkflowPermission.create!(:role_id => 2, :tracker_id => 2, :old_status_id => 1, :field_name => 'assigned_to_id', :rule => 'readonly')
  240. get :permissions, :params => {:role_id => [1, 2], :tracker_id => 2}
  241. assert_response :success
  242. assert_select 'select[name=?]', 'permissions[1][assigned_to_id]' do
  243. assert_select 'option[selected]', 1
  244. assert_select 'option[selected][value=no_change]'
  245. end
  246. end
  247. def test_get_permissions_with_same_permissions_for_roles_should_default_to_permission
  248. WorkflowPermission.delete_all
  249. WorkflowPermission.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 1, :field_name => 'assigned_to_id', :rule => 'required')
  250. WorkflowPermission.create!(:role_id => 2, :tracker_id => 2, :old_status_id => 1, :field_name => 'assigned_to_id', :rule => 'required')
  251. get :permissions, :params => {:role_id => [1, 2], :tracker_id => 2}
  252. assert_response :success
  253. assert_select 'select[name=?]', 'permissions[1][assigned_to_id]' do
  254. assert_select 'option[selected]', 1
  255. assert_select 'option[selected][value=required]'
  256. end
  257. end
  258. def test_get_permissions_with_role_and_tracker_and_all_statuses_should_show_all_statuses
  259. WorkflowTransition.delete_all
  260. get :permissions, :params => {:role_id => 1, :tracker_id => 2, :used_statuses_only => '0'}
  261. assert_response :success
  262. statuses = IssueStatus.all.sorted.pluck(:name)
  263. assert_equal statuses,
  264. css_select('table.workflows.fields_permissions thead tr:nth-child(2) td:not(:first-child)').map(&:text).map(&:strip)
  265. end
  266. def test_get_permissions_should_set_css_class
  267. WorkflowPermission.delete_all
  268. WorkflowPermission.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 1, :field_name => 'assigned_to_id', :rule => 'required')
  269. cf = IssueCustomField.create!(:name => 'Foo', :field_format => 'string', :tracker_ids => [2])
  270. WorkflowPermission.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 1, :field_name => cf.id, :rule => 'required')
  271. get :permissions, :params => {:role_id => 1, :tracker_id => 2}
  272. assert_response :success
  273. assert_select 'td.required > select[name=?]', 'permissions[1][assigned_to_id]'
  274. assert_select 'td.required > select[name=?]', "permissions[1][#{cf.id}]"
  275. end
  276. def test_post_permissions
  277. WorkflowPermission.delete_all
  278. post :permissions, :params => {
  279. :role_id => 1,
  280. :tracker_id => 2,
  281. :permissions => {
  282. '1' => {'assigned_to_id' => '', 'fixed_version_id' => 'required', 'due_date' => ''},
  283. '2' => {'assigned_to_id' => 'readonly', 'fixed_version_id' => 'readonly', 'due_date' => ''},
  284. '3' => {'assigned_to_id' => '', 'fixed_version_id' => '', 'due_date' => ''}
  285. }
  286. }
  287. assert_response 302
  288. workflows = WorkflowPermission.all
  289. assert_equal 3, workflows.size
  290. workflows.each do |workflow|
  291. assert_equal 1, workflow.role_id
  292. assert_equal 2, workflow.tracker_id
  293. end
  294. assert workflows.detect {|wf| wf.old_status_id == 2 && wf.field_name == 'assigned_to_id' && wf.rule == 'readonly'}
  295. assert workflows.detect {|wf| wf.old_status_id == 1 && wf.field_name == 'fixed_version_id' && wf.rule == 'required'}
  296. assert workflows.detect {|wf| wf.old_status_id == 2 && wf.field_name == 'fixed_version_id' && wf.rule == 'readonly'}
  297. end
  298. def test_get_copy
  299. get :copy
  300. assert_response :success
  301. assert_select 'select[name=source_tracker_id]' do
  302. assert_select 'option[value="1"]', :text => 'Bug'
  303. end
  304. assert_select 'select[name=source_role_id]' do
  305. assert_select 'option[value="2"]', :text => 'Developer'
  306. end
  307. assert_select 'select[name=?]', 'target_tracker_ids[]' do
  308. assert_select 'option[value="3"]', :text => 'Support request'
  309. end
  310. assert_select 'select[name=?]', 'target_role_ids[]' do
  311. assert_select 'option[value="1"]', :text => 'Manager'
  312. end
  313. end
  314. def test_post_copy_one_to_one
  315. source_transitions = status_transitions(:tracker_id => 1, :role_id => 2)
  316. post :copy, :params => {
  317. :source_tracker_id => '1', :source_role_id => '2',
  318. :target_tracker_ids => ['3'], :target_role_ids => ['1']
  319. }
  320. assert_response 302
  321. assert_equal source_transitions, status_transitions(:tracker_id => 3, :role_id => 1)
  322. end
  323. def test_post_copy_one_to_many
  324. source_transitions = status_transitions(:tracker_id => 1, :role_id => 2)
  325. post :copy, :params => {
  326. :source_tracker_id => '1', :source_role_id => '2',
  327. :target_tracker_ids => ['2', '3'], :target_role_ids => ['1', '3']
  328. }
  329. assert_response 302
  330. assert_equal source_transitions, status_transitions(:tracker_id => 2, :role_id => 1)
  331. assert_equal source_transitions, status_transitions(:tracker_id => 3, :role_id => 1)
  332. assert_equal source_transitions, status_transitions(:tracker_id => 2, :role_id => 3)
  333. assert_equal source_transitions, status_transitions(:tracker_id => 3, :role_id => 3)
  334. end
  335. def test_post_copy_many_to_many
  336. source_t2 = status_transitions(:tracker_id => 2, :role_id => 2)
  337. source_t3 = status_transitions(:tracker_id => 3, :role_id => 2)
  338. post :copy, :params => {
  339. :source_tracker_id => 'any', :source_role_id => '2',
  340. :target_tracker_ids => ['2', '3'], :target_role_ids => ['1', '3']
  341. }
  342. assert_response 302
  343. assert_equal source_t2, status_transitions(:tracker_id => 2, :role_id => 1)
  344. assert_equal source_t3, status_transitions(:tracker_id => 3, :role_id => 1)
  345. assert_equal source_t2, status_transitions(:tracker_id => 2, :role_id => 3)
  346. assert_equal source_t3, status_transitions(:tracker_id => 3, :role_id => 3)
  347. end
  348. def test_post_copy_with_incomplete_source_specification_should_fail
  349. assert_no_difference 'WorkflowRule.count' do
  350. post :copy, :params => {
  351. :source_tracker_id => '', :source_role_id => '2',
  352. :target_tracker_ids => ['2', '3'], :target_role_ids => ['1', '3']
  353. }
  354. assert_response 200
  355. assert_select 'div.flash.error', :text => 'Please select a source tracker or role'
  356. end
  357. end
  358. def test_post_copy_with_incomplete_target_specification_should_fail
  359. assert_no_difference 'WorkflowRule.count' do
  360. post :copy, :params => {
  361. :source_tracker_id => '1', :source_role_id => '2',
  362. :target_tracker_ids => ['2', '3']
  363. }
  364. assert_response 200
  365. assert_select 'div.flash.error', :text => 'Please select target tracker(s) and role(s)'
  366. end
  367. end
  368. # Returns an array of status transitions that can be compared
  369. def status_transitions(conditions)
  370. WorkflowTransition.
  371. where(conditions).
  372. order('tracker_id, role_id, old_status_id, new_status_id').
  373. collect {|w| [w.old_status, w.new_status_id]}
  374. end
  375. end