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

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