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