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

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