"Fossies" - the Fresh Open Source Software Archive

Member "redmine-4.1.1/test/functional/workflows_controller_test.rb" (6 Apr 2020, 18903 Bytes) of package /linux/www/redmine-4.1.1.tar.gz:


As a special service "Fossies" has tried to format the requested text file into HTML format (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "workflows_controller_test.rb": 4.1.0_vs_4.1.1.

    1 # frozen_string_literal: true
    2 
    3 # Redmine - project management software
    4 # Copyright (C) 2006-2019  Jean-Philippe Lang
    5 #
    6 # This program is free software; you can redistribute it and/or
    7 # modify it under the terms of the GNU General Public License
    8 # as published by the Free Software Foundation; either version 2
    9 # of the License, or (at your option) any later version.
   10 #
   11 # This program is distributed in the hope that it will be useful,
   12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
   13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14 # GNU General Public License for more details.
   15 #
   16 # You should have received a copy of the GNU General Public License
   17 # along with this program; if not, write to the Free Software
   18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
   19 
   20 require File.expand_path('../../test_helper', __FILE__)
   21 
   22 class WorkflowsControllerTest < Redmine::ControllerTest
   23   fixtures :roles, :trackers, :workflows, :users, :issue_statuses, :custom_fields
   24 
   25   def setup
   26     User.current = nil
   27     @request.session[:user_id] = 1 # admin
   28   end
   29 
   30   def test_index
   31     get :index
   32     assert_response :success
   33 
   34     count = WorkflowTransition.where(:role_id => 1, :tracker_id => 2).count
   35     assert_select 'a[href=?]', '/workflows/edit?role_id=1&tracker_id=2', :content => count.to_s
   36   end
   37 
   38   def test_get_edit
   39     get :edit
   40     assert_response :success
   41   end
   42 
   43   def test_get_edit_with_role_and_tracker
   44     WorkflowTransition.delete_all
   45     WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 2, :new_status_id => 3)
   46     WorkflowTransition.create!(:role_id => 2, :tracker_id => 1, :old_status_id => 3, :new_status_id => 5)
   47 
   48     get :edit, :params => {:role_id => 2, :tracker_id => 1}
   49     assert_response :success
   50 
   51     # used status only
   52     statuses = IssueStatus.where(:id => [2, 3, 5]).sorted.pluck(:name)
   53     assert_equal(
   54       ["New issue"] + statuses,
   55       css_select('table.workflows.transitions-always tbody tr td:first').map(&:text).map(&:strip)
   56     )
   57     # allowed transitions
   58     assert_select 'input[type=checkbox][name=?][value="1"][checked=checked]', 'transitions[3][5][always]'
   59     # not allowed
   60     assert_select 'input[type=checkbox][name=?][value="1"]:not([checked=checked])', 'transitions[3][2][always]'
   61     # unused
   62     assert_select 'input[type=checkbox][name=?]', 'transitions[1][1][always]', 0
   63   end
   64 
   65   def test_get_edit_with_role_and_tracker_should_not_include_statuses_from_roles_without_workflow_permissions
   66     WorkflowTransition.delete_all
   67     WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 2, :new_status_id => 3)
   68 
   69     reporter = Role.find(3)
   70     reporter.remove_permission! :edit_issues
   71     reporter.remove_permission! :add_issues
   72     assert !reporter.consider_workflow?
   73     WorkflowTransition.create!(:role_id => 3, :tracker_id => 1, :old_status_id => 1, :new_status_id => 5)
   74 
   75     get :edit, :params => {:role_id => 2, :tracker_id => 1}
   76     assert_response :success
   77 
   78     # statuses 1 and 5 not displayed
   79     statuses = IssueStatus.where(:id => [2, 3]).sorted.pluck(:name)
   80     assert_equal(
   81       ["New issue"] + statuses,
   82       css_select('table.workflows.transitions-always tbody tr td:first').map(&:text).map(&:strip)
   83     )
   84   end
   85 
   86   def test_get_edit_should_include_allowed_statuses_for_new_issues
   87     WorkflowTransition.delete_all
   88     WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 0, :new_status_id => 1)
   89 
   90     get :edit, :params => {:role_id => 1, :tracker_id => 1}
   91     assert_response :success
   92     assert_select 'td', 'New issue'
   93     assert_select 'input[type=checkbox][name=?][value="1"][checked=checked]', 'transitions[0][1][always]'
   94   end
   95 
   96   def test_get_edit_with_all_roles_and_all_trackers
   97     get :edit, :params => {:role_id => 'all', :tracker_id => 'all'}
   98     assert_response :success
   99 
  100     assert_select 'select[name=?]', 'role_id[]' do
  101       assert_select 'option[selected=selected][value=all]'
  102     end
  103     assert_select 'select[name=?]', 'tracker_id[]' do
  104       assert_select 'option[selected=selected][value=all]'
  105     end
  106   end
  107 
  108   def test_get_edit_with_role_and_tracker_and_all_statuses
  109     WorkflowTransition.delete_all
  110 
  111     get :edit, :params => {:role_id => 2, :tracker_id => 1, :used_statuses_only => '0'}
  112     assert_response :success
  113 
  114     statuses = IssueStatus.all.sorted.pluck(:name)
  115     assert_equal(
  116       ["New issue"] + statuses,
  117       css_select('table.workflows.transitions-always tbody tr td:first').map(&:text).map(&:strip)
  118     )
  119     assert_select 'input[type=checkbox][name=?]', 'transitions[0][1][always]'
  120   end
  121 
  122   def test_get_edit_should_show_checked_disabled_transition_checkbox_between_same_statuses
  123     get :edit, :params => {:role_id => 2, :tracker_id => 1}
  124     assert_response :success
  125     assert_select 'table.workflows.transitions-always tbody tr:nth-child(2)' do
  126       assert_select 'td.name', :text => 'New'
  127       # assert that the td is enabled
  128       assert_select "td[title='New ยป New'][class=?]", 'enabled'
  129       # assert that the checkbox is disabled and checked
  130       assert_select "input[name='transitions[1][1][always]'][checked=?][disabled=?]", 'checked', 'disabled', 1
  131     end
  132   end
  133 
  134   def test_post_edit
  135     WorkflowTransition.delete_all
  136 
  137     post :edit, :params => {
  138       :role_id => 2,
  139       :tracker_id => 1,
  140       :transitions => {
  141         '4' => {'5' => {'always' => '1'}},
  142         '3' => {'1' => {'always' => '1'}, '2' => {'always' => '1'}}
  143       }
  144     }
  145     assert_response 302
  146 
  147     assert_equal 3, WorkflowTransition.where(:tracker_id => 1, :role_id => 2).count
  148     assert_not_nil  WorkflowTransition.where(:role_id => 2, :tracker_id => 1, :old_status_id => 3, :new_status_id => 2).first
  149     assert_nil      WorkflowTransition.where(:role_id => 2, :tracker_id => 1, :old_status_id => 5, :new_status_id => 4).first
  150   end
  151 
  152   def test_post_edit_with_allowed_statuses_for_new_issues
  153     WorkflowTransition.delete_all
  154 
  155     post :edit, :params => {
  156       :role_id => 2,
  157       :tracker_id => 1,
  158       :transitions => {
  159         '0' => {'1' => {'always' => '1'}, '2' => {'always' => '1'}}
  160       }
  161     }
  162     assert_response 302
  163 
  164     assert WorkflowTransition.where(:role_id => 2, :tracker_id => 1, :old_status_id => 0, :new_status_id => 1).any?
  165     assert WorkflowTransition.where(:role_id => 2, :tracker_id => 1, :old_status_id => 0, :new_status_id => 2).any?
  166     assert_equal 2, WorkflowTransition.where(:tracker_id => 1, :role_id => 2).count
  167   end
  168 
  169   def test_post_edit_with_additional_transitions
  170     WorkflowTransition.delete_all
  171 
  172     post :edit, :params => {
  173       :role_id => 2,
  174       :tracker_id => 1,
  175       :transitions => {
  176         '4' => {'5' => {'always' => '1', 'author' => '0', 'assignee' => '0'}},
  177         '3' => {'1' => {'always' => '0', 'author' => '1', 'assignee' => '0'},
  178                 '2' => {'always' => '0', 'author' => '0', 'assignee' => '1'},
  179                 '4' => {'always' => '0', 'author' => '1', 'assignee' => '1'}}
  180       }
  181     }
  182     assert_response 302
  183 
  184     assert_equal 4, WorkflowTransition.where(:tracker_id => 1, :role_id => 2).count
  185 
  186     w = WorkflowTransition.where(:role_id => 2, :tracker_id => 1, :old_status_id => 4, :new_status_id => 5).first
  187     assert ! w.author
  188     assert ! w.assignee
  189     w = WorkflowTransition.where(:role_id => 2, :tracker_id => 1, :old_status_id => 3, :new_status_id => 1).first
  190     assert w.author
  191     assert ! w.assignee
  192     w = WorkflowTransition.where(:role_id => 2, :tracker_id => 1, :old_status_id => 3, :new_status_id => 2).first
  193     assert ! w.author
  194     assert w.assignee
  195     w = WorkflowTransition.where(:role_id => 2, :tracker_id => 1, :old_status_id => 3, :new_status_id => 4).first
  196     assert w.author
  197     assert w.assignee
  198   end
  199 
  200   def test_get_permissions
  201     get :permissions
  202 
  203     assert_response :success
  204   end
  205 
  206   def test_get_permissions_with_role_and_tracker
  207     WorkflowPermission.delete_all
  208     WorkflowPermission.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 2, :field_name => 'assigned_to_id', :rule => 'required')
  209     WorkflowPermission.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 2, :field_name => 'fixed_version_id', :rule => 'required')
  210     WorkflowPermission.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 3, :field_name => 'fixed_version_id', :rule => 'readonly')
  211 
  212     get :permissions, :params => {:role_id => 1, :tracker_id => 2}
  213     assert_response :success
  214 
  215     assert_select 'input[name=?][value="1"]', 'role_id[]'
  216     assert_select 'input[name=?][value="2"]', 'tracker_id[]'
  217 
  218     # Required field
  219     assert_select 'select[name=?]', 'permissions[2][assigned_to_id]' do
  220       assert_select 'option[value=""]'
  221       assert_select 'option[value=""][selected=selected]', 0
  222       assert_select 'option[value=readonly]', :text => 'Read-only'
  223       assert_select 'option[value=readonly][selected=selected]', 0
  224       assert_select 'option[value=required]', :text => 'Required'
  225       assert_select 'option[value=required][selected=selected]'
  226     end
  227 
  228     # Read-only field
  229     assert_select 'select[name=?]', 'permissions[3][fixed_version_id]' do
  230       assert_select 'option[value=""]'
  231       assert_select 'option[value=""][selected=selected]', 0
  232       assert_select 'option[value=readonly]', :text => 'Read-only'
  233       assert_select 'option[value=readonly][selected=selected]'
  234       assert_select 'option[value=required]', :text => 'Required'
  235       assert_select 'option[value=required][selected=selected]', 0
  236     end
  237 
  238     # Other field
  239     assert_select 'select[name=?]', 'permissions[3][due_date]' do
  240       assert_select 'option[value=""]'
  241       assert_select 'option[value=""][selected=selected]', 0
  242       assert_select 'option[value=readonly]', :text => 'Read-only'
  243       assert_select 'option[value=readonly][selected=selected]', 0
  244       assert_select 'option[value=required]', :text => 'Required'
  245       assert_select 'option[value=required][selected=selected]', 0
  246     end
  247   end
  248 
  249   def test_get_permissions_with_required_custom_field_should_not_show_required_option
  250     cf = IssueCustomField.create!(:name => 'Foo', :field_format => 'string', :tracker_ids => [1], :is_required => true)
  251 
  252     get :permissions, :params => {:role_id => 1, :tracker_id => 1}
  253     assert_response :success
  254 
  255     # Custom field that is always required
  256     # The default option is "(Required)"
  257     assert_select 'select[name=?]', "permissions[3][#{cf.id}]" do
  258       assert_select 'option[value=""]'
  259       assert_select 'option[value=readonly]', :text => 'Read-only'
  260       assert_select 'option[value=required]', 0
  261     end
  262   end
  263 
  264   def test_get_permissions_should_disable_hidden_custom_fields
  265     cf1 = IssueCustomField.generate!(:tracker_ids => [1], :visible => true)
  266     cf2 = IssueCustomField.generate!(:tracker_ids => [1], :visible => false, :role_ids => [1])
  267     cf3 = IssueCustomField.generate!(:tracker_ids => [1], :visible => false, :role_ids => [1, 2])
  268 
  269     get :permissions, :params => {:role_id => 2, :tracker_id => 1}
  270     assert_response :success
  271 
  272     assert_select 'select[name=?]:not(.disabled)', "permissions[1][#{cf1.id}]"
  273     assert_select 'select[name=?]:not(.disabled)', "permissions[1][#{cf3.id}]"
  274 
  275     assert_select 'select[name=?][disabled=disabled]', "permissions[1][#{cf2.id}]" do
  276       assert_select 'option[value=""][selected=selected]', :text => 'Hidden'
  277     end
  278   end
  279 
  280   def test_get_permissions_with_missing_permissions_for_roles_should_default_to_no_change
  281     WorkflowPermission.delete_all
  282     WorkflowPermission.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 1, :field_name => 'assigned_to_id', :rule => 'required')
  283 
  284     get :permissions, :params => {:role_id => [1, 2], :tracker_id => 2}
  285     assert_response :success
  286 
  287     assert_select 'select[name=?]', 'permissions[1][assigned_to_id]' do
  288       assert_select 'option[selected]', 1
  289       assert_select 'option[selected][value=no_change]'
  290     end
  291   end
  292 
  293   def test_get_permissions_with_different_permissions_for_roles_should_default_to_no_change
  294     WorkflowPermission.delete_all
  295     WorkflowPermission.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 1, :field_name => 'assigned_to_id', :rule => 'required')
  296     WorkflowPermission.create!(:role_id => 2, :tracker_id => 2, :old_status_id => 1, :field_name => 'assigned_to_id', :rule => 'readonly')
  297 
  298     get :permissions, :params => {:role_id => [1, 2], :tracker_id => 2}
  299     assert_response :success
  300 
  301     assert_select 'select[name=?]', 'permissions[1][assigned_to_id]' do
  302       assert_select 'option[selected]', 1
  303       assert_select 'option[selected][value=no_change]'
  304     end
  305   end
  306 
  307   def test_get_permissions_with_same_permissions_for_roles_should_default_to_permission
  308     WorkflowPermission.delete_all
  309     WorkflowPermission.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 1, :field_name => 'assigned_to_id', :rule => 'required')
  310     WorkflowPermission.create!(:role_id => 2, :tracker_id => 2, :old_status_id => 1, :field_name => 'assigned_to_id', :rule => 'required')
  311 
  312     get :permissions, :params => {:role_id => [1, 2], :tracker_id => 2}
  313     assert_response :success
  314 
  315     assert_select 'select[name=?]', 'permissions[1][assigned_to_id]' do
  316       assert_select 'option[selected]', 1
  317       assert_select 'option[selected][value=required]'
  318     end
  319   end
  320 
  321   def test_get_permissions_with_role_and_tracker_and_all_statuses_should_show_all_statuses
  322     WorkflowTransition.delete_all
  323 
  324     get :permissions, :params => {:role_id => 1, :tracker_id => 2, :used_statuses_only => '0'}
  325     assert_response :success
  326 
  327     statuses = IssueStatus.all.sorted.pluck(:name)
  328     assert_equal(
  329       statuses,
  330       css_select('table.workflows.fields_permissions thead tr:nth-child(2) td:not(:first-child)').map(&:text).map(&:strip)
  331     )
  332   end
  333 
  334   def test_get_permissions_should_set_css_class
  335     WorkflowPermission.delete_all
  336     WorkflowPermission.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 1, :field_name => 'assigned_to_id', :rule => 'required')
  337     cf = IssueCustomField.create!(:name => 'Foo', :field_format => 'string', :tracker_ids => [2])
  338     WorkflowPermission.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 1, :field_name => cf.id, :rule => 'required')
  339 
  340     get :permissions, :params => {:role_id => 1, :tracker_id => 2}
  341     assert_response :success
  342     assert_select 'td.required > select[name=?]', 'permissions[1][assigned_to_id]'
  343     assert_select 'td.required > select[name=?]', "permissions[1][#{cf.id}]"
  344   end
  345 
  346   def test_post_permissions
  347     WorkflowPermission.delete_all
  348 
  349     post :permissions, :params => {
  350       :role_id => 1,
  351       :tracker_id => 2,
  352       :permissions => {
  353         '1' => {'assigned_to_id' => '', 'fixed_version_id' => 'required', 'due_date' => ''},
  354         '2' => {'assigned_to_id' => 'readonly', 'fixed_version_id' => 'readonly', 'due_date' => ''},
  355         '3' => {'assigned_to_id' => '',  'fixed_version_id' => '', 'due_date' => ''}
  356       }
  357     }
  358     assert_response 302
  359 
  360     workflows = WorkflowPermission.all
  361     assert_equal 3, workflows.size
  362     workflows.each do |workflow|
  363       assert_equal 1, workflow.role_id
  364       assert_equal 2, workflow.tracker_id
  365     end
  366     assert workflows.detect {|wf| wf.old_status_id == 2 && wf.field_name == 'assigned_to_id' && wf.rule == 'readonly'}
  367     assert workflows.detect {|wf| wf.old_status_id == 1 && wf.field_name == 'fixed_version_id' && wf.rule == 'required'}
  368     assert workflows.detect {|wf| wf.old_status_id == 2 && wf.field_name == 'fixed_version_id' && wf.rule == 'readonly'}
  369   end
  370 
  371   def test_get_copy
  372     get :copy
  373     assert_response :success
  374 
  375     assert_select 'select[name=source_tracker_id]' do
  376       assert_select 'option[value="1"]', :text => 'Bug'
  377     end
  378     assert_select 'select[name=source_role_id]' do
  379       assert_select 'option[value="2"]', :text => 'Developer'
  380     end
  381     assert_select 'select[name=?]', 'target_tracker_ids[]' do
  382       assert_select 'option[value="3"]', :text => 'Support request'
  383     end
  384     assert_select 'select[name=?]', 'target_role_ids[]' do
  385       assert_select 'option[value="1"]', :text => 'Manager'
  386     end
  387   end
  388 
  389   def test_post_copy_one_to_one
  390     source_transitions = status_transitions(:tracker_id => 1, :role_id => 2)
  391 
  392     post :copy, :params => {
  393       :source_tracker_id => '1', :source_role_id => '2',
  394       :target_tracker_ids => ['3'], :target_role_ids => ['1']
  395     }
  396     assert_response 302
  397     assert_equal source_transitions, status_transitions(:tracker_id => 3, :role_id => 1)
  398   end
  399 
  400   def test_post_copy_one_to_many
  401     source_transitions = status_transitions(:tracker_id => 1, :role_id => 2)
  402 
  403     post :copy, :params => {
  404       :source_tracker_id => '1', :source_role_id => '2',
  405       :target_tracker_ids => ['2', '3'], :target_role_ids => ['1', '3']
  406     }
  407     assert_response 302
  408     assert_equal source_transitions, status_transitions(:tracker_id => 2, :role_id => 1)
  409     assert_equal source_transitions, status_transitions(:tracker_id => 3, :role_id => 1)
  410     assert_equal source_transitions, status_transitions(:tracker_id => 2, :role_id => 3)
  411     assert_equal source_transitions, status_transitions(:tracker_id => 3, :role_id => 3)
  412   end
  413 
  414   def test_post_copy_many_to_many
  415     source_t2 = status_transitions(:tracker_id => 2, :role_id => 2)
  416     source_t3 = status_transitions(:tracker_id => 3, :role_id => 2)
  417 
  418     post :copy, :params => {
  419       :source_tracker_id => 'any', :source_role_id => '2',
  420       :target_tracker_ids => ['2', '3'], :target_role_ids => ['1', '3']
  421     }
  422     assert_response 302
  423     assert_equal source_t2, status_transitions(:tracker_id => 2, :role_id => 1)
  424     assert_equal source_t3, status_transitions(:tracker_id => 3, :role_id => 1)
  425     assert_equal source_t2, status_transitions(:tracker_id => 2, :role_id => 3)
  426     assert_equal source_t3, status_transitions(:tracker_id => 3, :role_id => 3)
  427   end
  428 
  429   def test_post_copy_with_incomplete_source_specification_should_fail
  430     assert_no_difference 'WorkflowRule.count' do
  431       post :copy, :params => {
  432         :source_tracker_id => '', :source_role_id => '2',
  433         :target_tracker_ids => ['2', '3'], :target_role_ids => ['1', '3']
  434       }
  435       assert_response 200
  436       assert_select 'div.flash.error', :text => 'Please select a source tracker or role'
  437     end
  438   end
  439 
  440   def test_post_copy_with_incomplete_target_specification_should_fail
  441     assert_no_difference 'WorkflowRule.count' do
  442       post :copy, :params => {
  443         :source_tracker_id => '1', :source_role_id => '2',
  444         :target_tracker_ids => ['2', '3']
  445       }
  446       assert_response 200
  447       assert_select 'div.flash.error', :text => 'Please select target tracker(s) and role(s)'
  448     end
  449   end
  450 
  451   # Returns an array of status transitions that can be compared
  452   def status_transitions(conditions)
  453     WorkflowTransition.
  454       where(conditions).
  455       order('tracker_id, role_id, old_status_id, new_status_id').
  456       collect {|w| [w.old_status, w.new_status_id]}
  457   end
  458 end