annotate test/functional/workflows_controller_test.rb @ 1295:622f24f53b42 redmine-2.3

Update to Redmine SVN revision 11972 on 2.3-stable branch
author Chris Cannam
date Fri, 14 Jun 2013 09:02:21 +0100
parents 433d4f72a19b
children
rev   line source
Chris@0 1 # Redmine - project management software
Chris@1295 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
Chris@0 3 #
Chris@0 4 # This program is free software; you can redistribute it and/or
Chris@0 5 # modify it under the terms of the GNU General Public License
Chris@0 6 # as published by the Free Software Foundation; either version 2
Chris@0 7 # of the License, or (at your option) any later version.
Chris@909 8 #
Chris@0 9 # This program is distributed in the hope that it will be useful,
Chris@0 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 12 # GNU General Public License for more details.
Chris@909 13 #
Chris@0 14 # You should have received a copy of the GNU General Public License
Chris@0 15 # along with this program; if not, write to the Free Software
Chris@0 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 17
Chris@119 18 require File.expand_path('../../test_helper', __FILE__)
Chris@0 19
Chris@0 20 class WorkflowsControllerTest < ActionController::TestCase
Chris@0 21 fixtures :roles, :trackers, :workflows, :users, :issue_statuses
Chris@909 22
Chris@0 23 def setup
Chris@0 24 User.current = nil
Chris@0 25 @request.session[:user_id] = 1 # admin
Chris@0 26 end
Chris@909 27
Chris@0 28 def test_index
Chris@0 29 get :index
Chris@0 30 assert_response :success
Chris@0 31 assert_template 'index'
Chris@909 32
Chris@1115 33 count = WorkflowTransition.count(:all, :conditions => 'role_id = 1 AND tracker_id = 2')
Chris@0 34 assert_tag :tag => 'a', :content => count.to_s,
Chris@0 35 :attributes => { :href => '/workflows/edit?role_id=1&amp;tracker_id=2' }
Chris@0 36 end
Chris@909 37
Chris@0 38 def test_get_edit
Chris@0 39 get :edit
Chris@0 40 assert_response :success
Chris@0 41 assert_template 'edit'
Chris@0 42 assert_not_nil assigns(:roles)
Chris@0 43 assert_not_nil assigns(:trackers)
Chris@0 44 end
Chris@909 45
Chris@0 46 def test_get_edit_with_role_and_tracker
Chris@1115 47 WorkflowTransition.delete_all
Chris@1115 48 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 2, :new_status_id => 3)
Chris@1115 49 WorkflowTransition.create!(:role_id => 2, :tracker_id => 1, :old_status_id => 3, :new_status_id => 5)
Chris@909 50
Chris@0 51 get :edit, :role_id => 2, :tracker_id => 1
Chris@0 52 assert_response :success
Chris@0 53 assert_template 'edit'
Chris@909 54
Chris@0 55 # used status only
Chris@0 56 assert_not_nil assigns(:statuses)
Chris@0 57 assert_equal [2, 3, 5], assigns(:statuses).collect(&:id)
Chris@909 58
Chris@0 59 # allowed transitions
Chris@0 60 assert_tag :tag => 'input', :attributes => { :type => 'checkbox',
Chris@245 61 :name => 'issue_status[3][5][]',
Chris@245 62 :value => 'always',
Chris@0 63 :checked => 'checked' }
Chris@0 64 # not allowed
Chris@0 65 assert_tag :tag => 'input', :attributes => { :type => 'checkbox',
Chris@245 66 :name => 'issue_status[3][2][]',
Chris@245 67 :value => 'always',
Chris@0 68 :checked => nil }
Chris@0 69 # unused
Chris@0 70 assert_no_tag :tag => 'input', :attributes => { :type => 'checkbox',
Chris@245 71 :name => 'issue_status[1][1][]' }
Chris@0 72 end
Chris@909 73
Chris@0 74 def test_get_edit_with_role_and_tracker_and_all_statuses
Chris@1115 75 WorkflowTransition.delete_all
Chris@909 76
Chris@0 77 get :edit, :role_id => 2, :tracker_id => 1, :used_statuses_only => '0'
Chris@0 78 assert_response :success
Chris@0 79 assert_template 'edit'
Chris@909 80
Chris@0 81 assert_not_nil assigns(:statuses)
Chris@0 82 assert_equal IssueStatus.count, assigns(:statuses).size
Chris@909 83
Chris@0 84 assert_tag :tag => 'input', :attributes => { :type => 'checkbox',
Chris@245 85 :name => 'issue_status[1][1][]',
Chris@245 86 :value => 'always',
Chris@0 87 :checked => nil }
Chris@0 88 end
Chris@909 89
Chris@0 90 def test_post_edit
Chris@245 91 post :edit, :role_id => 2, :tracker_id => 1,
Chris@245 92 :issue_status => {
Chris@245 93 '4' => {'5' => ['always']},
Chris@245 94 '3' => {'1' => ['always'], '2' => ['always']}
Chris@245 95 }
Chris@0 96 assert_redirected_to '/workflows/edit?role_id=2&tracker_id=1'
Chris@909 97
Chris@1295 98 assert_equal 3, WorkflowTransition.where(:tracker_id => 1, :role_id => 2).count
Chris@1295 99 assert_not_nil WorkflowTransition.where(:role_id => 2, :tracker_id => 1, :old_status_id => 3, :new_status_id => 2).first
Chris@1295 100 assert_nil WorkflowTransition.where(:role_id => 2, :tracker_id => 1, :old_status_id => 5, :new_status_id => 4).first
Chris@0 101 end
Chris@909 102
Chris@245 103 def test_post_edit_with_additional_transitions
Chris@245 104 post :edit, :role_id => 2, :tracker_id => 1,
Chris@245 105 :issue_status => {
Chris@245 106 '4' => {'5' => ['always']},
Chris@245 107 '3' => {'1' => ['author'], '2' => ['assignee'], '4' => ['author', 'assignee']}
Chris@245 108 }
Chris@245 109 assert_redirected_to '/workflows/edit?role_id=2&tracker_id=1'
Chris@909 110
Chris@1295 111 assert_equal 4, WorkflowTransition.where(:tracker_id => 1, :role_id => 2).count
Chris@909 112
Chris@1295 113 w = WorkflowTransition.where(:role_id => 2, :tracker_id => 1, :old_status_id => 4, :new_status_id => 5).first
Chris@245 114 assert ! w.author
Chris@245 115 assert ! w.assignee
Chris@1295 116 w = WorkflowTransition.where(:role_id => 2, :tracker_id => 1, :old_status_id => 3, :new_status_id => 1).first
Chris@245 117 assert w.author
Chris@245 118 assert ! w.assignee
Chris@1295 119 w = WorkflowTransition.where(:role_id => 2, :tracker_id => 1, :old_status_id => 3, :new_status_id => 2).first
Chris@245 120 assert ! w.author
Chris@245 121 assert w.assignee
Chris@1295 122 w = WorkflowTransition.where(:role_id => 2, :tracker_id => 1, :old_status_id => 3, :new_status_id => 4).first
Chris@245 123 assert w.author
Chris@245 124 assert w.assignee
Chris@245 125 end
Chris@909 126
Chris@0 127 def test_clear_workflow
Chris@1115 128 assert WorkflowTransition.count(:conditions => {:tracker_id => 1, :role_id => 2}) > 0
Chris@0 129
Chris@0 130 post :edit, :role_id => 2, :tracker_id => 1
Chris@1115 131 assert_equal 0, WorkflowTransition.count(:conditions => {:tracker_id => 1, :role_id => 2})
Chris@1115 132 end
Chris@1115 133
Chris@1115 134 def test_get_permissions
Chris@1115 135 get :permissions
Chris@1115 136
Chris@1115 137 assert_response :success
Chris@1115 138 assert_template 'permissions'
Chris@1115 139 assert_not_nil assigns(:roles)
Chris@1115 140 assert_not_nil assigns(:trackers)
Chris@1115 141 end
Chris@1115 142
Chris@1115 143 def test_get_permissions_with_role_and_tracker
Chris@1115 144 WorkflowPermission.delete_all
Chris@1115 145 WorkflowPermission.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 2, :field_name => 'assigned_to_id', :rule => 'required')
Chris@1115 146 WorkflowPermission.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 2, :field_name => 'fixed_version_id', :rule => 'required')
Chris@1115 147 WorkflowPermission.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 3, :field_name => 'fixed_version_id', :rule => 'readonly')
Chris@1115 148
Chris@1115 149 get :permissions, :role_id => 1, :tracker_id => 2
Chris@1115 150 assert_response :success
Chris@1115 151 assert_template 'permissions'
Chris@1115 152
Chris@1115 153 assert_select 'input[name=role_id][value=1]'
Chris@1115 154 assert_select 'input[name=tracker_id][value=2]'
Chris@1115 155
Chris@1115 156 # Required field
Chris@1115 157 assert_select 'select[name=?]', 'permissions[assigned_to_id][2]' do
Chris@1115 158 assert_select 'option[value=]'
Chris@1115 159 assert_select 'option[value=][selected=selected]', 0
Chris@1115 160 assert_select 'option[value=readonly]', :text => 'Read-only'
Chris@1115 161 assert_select 'option[value=readonly][selected=selected]', 0
Chris@1115 162 assert_select 'option[value=required]', :text => 'Required'
Chris@1115 163 assert_select 'option[value=required][selected=selected]'
Chris@1115 164 end
Chris@1115 165
Chris@1115 166 # Read-only field
Chris@1115 167 assert_select 'select[name=?]', 'permissions[fixed_version_id][3]' do
Chris@1115 168 assert_select 'option[value=]'
Chris@1115 169 assert_select 'option[value=][selected=selected]', 0
Chris@1115 170 assert_select 'option[value=readonly]', :text => 'Read-only'
Chris@1115 171 assert_select 'option[value=readonly][selected=selected]'
Chris@1115 172 assert_select 'option[value=required]', :text => 'Required'
Chris@1115 173 assert_select 'option[value=required][selected=selected]', 0
Chris@1115 174 end
Chris@1115 175
Chris@1115 176 # Other field
Chris@1115 177 assert_select 'select[name=?]', 'permissions[due_date][3]' do
Chris@1115 178 assert_select 'option[value=]'
Chris@1115 179 assert_select 'option[value=][selected=selected]', 0
Chris@1115 180 assert_select 'option[value=readonly]', :text => 'Read-only'
Chris@1115 181 assert_select 'option[value=readonly][selected=selected]', 0
Chris@1115 182 assert_select 'option[value=required]', :text => 'Required'
Chris@1115 183 assert_select 'option[value=required][selected=selected]', 0
Chris@1115 184 end
Chris@1115 185 end
Chris@1115 186
Chris@1115 187 def test_get_permissions_with_required_custom_field_should_not_show_required_option
Chris@1115 188 cf = IssueCustomField.create!(:name => 'Foo', :field_format => 'string', :tracker_ids => [1], :is_required => true)
Chris@1115 189
Chris@1115 190 get :permissions, :role_id => 1, :tracker_id => 1
Chris@1115 191 assert_response :success
Chris@1115 192 assert_template 'permissions'
Chris@1115 193
Chris@1115 194 # Custom field that is always required
Chris@1115 195 # The default option is "(Required)"
Chris@1115 196 assert_select 'select[name=?]', "permissions[#{cf.id}][3]" do
Chris@1115 197 assert_select 'option[value=]'
Chris@1115 198 assert_select 'option[value=readonly]', :text => 'Read-only'
Chris@1115 199 assert_select 'option[value=required]', 0
Chris@1115 200 end
Chris@1115 201 end
Chris@1115 202
Chris@1115 203 def test_get_permissions_with_role_and_tracker_and_all_statuses
Chris@1115 204 WorkflowTransition.delete_all
Chris@1115 205
Chris@1115 206 get :permissions, :role_id => 1, :tracker_id => 2, :used_statuses_only => '0'
Chris@1115 207 assert_response :success
Chris@1115 208 assert_equal IssueStatus.sorted.all, assigns(:statuses)
Chris@1115 209 end
Chris@1115 210
Chris@1115 211 def test_post_permissions
Chris@1115 212 WorkflowPermission.delete_all
Chris@1115 213
Chris@1115 214 post :permissions, :role_id => 1, :tracker_id => 2, :permissions => {
Chris@1115 215 'assigned_to_id' => {'1' => '', '2' => 'readonly', '3' => ''},
Chris@1115 216 'fixed_version_id' => {'1' => 'required', '2' => 'readonly', '3' => ''},
Chris@1115 217 'due_date' => {'1' => '', '2' => '', '3' => ''},
Chris@1115 218 }
Chris@1115 219 assert_redirected_to '/workflows/permissions?role_id=1&tracker_id=2'
Chris@1115 220
Chris@1115 221 workflows = WorkflowPermission.all
Chris@1115 222 assert_equal 3, workflows.size
Chris@1115 223 workflows.each do |workflow|
Chris@1115 224 assert_equal 1, workflow.role_id
Chris@1115 225 assert_equal 2, workflow.tracker_id
Chris@1115 226 end
Chris@1115 227 assert workflows.detect {|wf| wf.old_status_id == 2 && wf.field_name == 'assigned_to_id' && wf.rule == 'readonly'}
Chris@1115 228 assert workflows.detect {|wf| wf.old_status_id == 1 && wf.field_name == 'fixed_version_id' && wf.rule == 'required'}
Chris@1115 229 assert workflows.detect {|wf| wf.old_status_id == 2 && wf.field_name == 'fixed_version_id' && wf.rule == 'readonly'}
Chris@1115 230 end
Chris@1115 231
Chris@1115 232 def test_post_permissions_should_clear_permissions
Chris@1115 233 WorkflowPermission.delete_all
Chris@1115 234 WorkflowPermission.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 2, :field_name => 'assigned_to_id', :rule => 'required')
Chris@1115 235 WorkflowPermission.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 2, :field_name => 'fixed_version_id', :rule => 'required')
Chris@1115 236 wf1 = WorkflowPermission.create!(:role_id => 1, :tracker_id => 3, :old_status_id => 2, :field_name => 'fixed_version_id', :rule => 'required')
Chris@1115 237 wf2 = WorkflowPermission.create!(:role_id => 2, :tracker_id => 2, :old_status_id => 3, :field_name => 'fixed_version_id', :rule => 'readonly')
Chris@1115 238
Chris@1115 239 post :permissions, :role_id => 1, :tracker_id => 2
Chris@1115 240 assert_redirected_to '/workflows/permissions?role_id=1&tracker_id=2'
Chris@1115 241
Chris@1115 242 workflows = WorkflowPermission.all
Chris@1115 243 assert_equal 2, workflows.size
Chris@1115 244 assert wf1.reload
Chris@1115 245 assert wf2.reload
Chris@0 246 end
Chris@909 247
Chris@0 248 def test_get_copy
Chris@0 249 get :copy
Chris@0 250 assert_response :success
Chris@0 251 assert_template 'copy'
Chris@1115 252 assert_select 'select[name=source_tracker_id]' do
Chris@1115 253 assert_select 'option[value=1]', :text => 'Bug'
Chris@1115 254 end
Chris@1115 255 assert_select 'select[name=source_role_id]' do
Chris@1115 256 assert_select 'option[value=2]', :text => 'Developer'
Chris@1115 257 end
Chris@1115 258 assert_select 'select[name=?]', 'target_tracker_ids[]' do
Chris@1115 259 assert_select 'option[value=3]', :text => 'Support request'
Chris@1115 260 end
Chris@1115 261 assert_select 'select[name=?]', 'target_role_ids[]' do
Chris@1115 262 assert_select 'option[value=1]', :text => 'Manager'
Chris@1115 263 end
Chris@0 264 end
Chris@909 265
Chris@0 266 def test_post_copy_one_to_one
Chris@0 267 source_transitions = status_transitions(:tracker_id => 1, :role_id => 2)
Chris@909 268
Chris@0 269 post :copy, :source_tracker_id => '1', :source_role_id => '2',
Chris@0 270 :target_tracker_ids => ['3'], :target_role_ids => ['1']
Chris@0 271 assert_response 302
Chris@0 272 assert_equal source_transitions, status_transitions(:tracker_id => 3, :role_id => 1)
Chris@0 273 end
Chris@909 274
Chris@0 275 def test_post_copy_one_to_many
Chris@0 276 source_transitions = status_transitions(:tracker_id => 1, :role_id => 2)
Chris@909 277
Chris@0 278 post :copy, :source_tracker_id => '1', :source_role_id => '2',
Chris@0 279 :target_tracker_ids => ['2', '3'], :target_role_ids => ['1', '3']
Chris@0 280 assert_response 302
Chris@0 281 assert_equal source_transitions, status_transitions(:tracker_id => 2, :role_id => 1)
Chris@0 282 assert_equal source_transitions, status_transitions(:tracker_id => 3, :role_id => 1)
Chris@0 283 assert_equal source_transitions, status_transitions(:tracker_id => 2, :role_id => 3)
Chris@0 284 assert_equal source_transitions, status_transitions(:tracker_id => 3, :role_id => 3)
Chris@0 285 end
Chris@909 286
Chris@0 287 def test_post_copy_many_to_many
Chris@0 288 source_t2 = status_transitions(:tracker_id => 2, :role_id => 2)
Chris@0 289 source_t3 = status_transitions(:tracker_id => 3, :role_id => 2)
Chris@909 290
Chris@0 291 post :copy, :source_tracker_id => 'any', :source_role_id => '2',
Chris@0 292 :target_tracker_ids => ['2', '3'], :target_role_ids => ['1', '3']
Chris@0 293 assert_response 302
Chris@0 294 assert_equal source_t2, status_transitions(:tracker_id => 2, :role_id => 1)
Chris@0 295 assert_equal source_t3, status_transitions(:tracker_id => 3, :role_id => 1)
Chris@0 296 assert_equal source_t2, status_transitions(:tracker_id => 2, :role_id => 3)
Chris@0 297 assert_equal source_t3, status_transitions(:tracker_id => 3, :role_id => 3)
Chris@0 298 end
Chris@909 299
Chris@1295 300 def test_post_copy_with_incomplete_source_specification_should_fail
Chris@1295 301 assert_no_difference 'WorkflowRule.count' do
Chris@1295 302 post :copy,
Chris@1295 303 :source_tracker_id => '', :source_role_id => '2',
Chris@1295 304 :target_tracker_ids => ['2', '3'], :target_role_ids => ['1', '3']
Chris@1295 305 assert_response 200
Chris@1295 306 assert_select 'div.flash.error', :text => 'Please select a source tracker or role'
Chris@1295 307 end
Chris@1295 308 end
Chris@1295 309
Chris@1295 310 def test_post_copy_with_incomplete_target_specification_should_fail
Chris@1295 311 assert_no_difference 'WorkflowRule.count' do
Chris@1295 312 post :copy,
Chris@1295 313 :source_tracker_id => '1', :source_role_id => '2',
Chris@1295 314 :target_tracker_ids => ['2', '3']
Chris@1295 315 assert_response 200
Chris@1295 316 assert_select 'div.flash.error', :text => 'Please select target tracker(s) and role(s)'
Chris@1295 317 end
Chris@1295 318 end
Chris@1295 319
Chris@0 320 # Returns an array of status transitions that can be compared
Chris@0 321 def status_transitions(conditions)
Chris@1295 322 WorkflowTransition.
Chris@1295 323 where(conditions).
Chris@1295 324 order('tracker_id, role_id, old_status_id, new_status_id').
Chris@1295 325 all.
Chris@1295 326 collect {|w| [w.old_status, w.new_status_id]}
Chris@0 327 end
Chris@0 328 end