##// END OF EJS Templates
Test failure (#23996)....
Jean-Philippe Lang -
r15588:ab22ec2a94bf
parent child
Show More
@@ -1,4757 +1,4757
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2016 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 IssuesControllerTest < Redmine::ControllerTest
21 21 fixtures :projects,
22 22 :users, :email_addresses, :user_preferences,
23 23 :roles,
24 24 :members,
25 25 :member_roles,
26 26 :issues,
27 27 :issue_statuses,
28 28 :issue_relations,
29 29 :versions,
30 30 :trackers,
31 31 :projects_trackers,
32 32 :issue_categories,
33 33 :enabled_modules,
34 34 :enumerations,
35 35 :attachments,
36 36 :workflows,
37 37 :custom_fields,
38 38 :custom_values,
39 39 :custom_fields_projects,
40 40 :custom_fields_trackers,
41 41 :time_entries,
42 42 :journals,
43 43 :journal_details,
44 44 :queries,
45 45 :repositories,
46 46 :changesets
47 47
48 48 include Redmine::I18n
49 49
50 50 def setup
51 51 User.current = nil
52 52 end
53 53
54 54 def test_index
55 55 with_settings :default_language => "en" do
56 56 get :index
57 57 assert_response :success
58 58
59 59 # links to visible issues
60 60 assert_select 'a[href="/issues/1"]', :text => /Cannot print recipes/
61 61 assert_select 'a[href="/issues/5"]', :text => /Subproject issue/
62 62 # private projects hidden
63 63 assert_select 'a[href="/issues/6"]', 0
64 64 assert_select 'a[href="/issues/4"]', 0
65 65 # project column
66 66 assert_select 'th', :text => /Project/
67 67 end
68 68 end
69 69
70 70 def test_index_should_not_list_issues_when_module_disabled
71 71 EnabledModule.where("name = 'issue_tracking' AND project_id = 1").delete_all
72 72 get :index
73 73 assert_response :success
74 74
75 75 assert_select 'a[href="/issues/1"]', 0
76 76 assert_select 'a[href="/issues/5"]', :text => /Subproject issue/
77 77 end
78 78
79 79 def test_index_should_list_visible_issues_only
80 80 get :index, :per_page => 100
81 81 assert_response :success
82 82
83 83 Issue.open.each do |issue|
84 84 assert_select "tr#issue-#{issue.id}", issue.visible? ? 1 : 0
85 85 end
86 86 end
87 87
88 88 def test_index_with_project
89 89 Setting.display_subprojects_issues = 0
90 90 get :index, :project_id => 1
91 91 assert_response :success
92 92
93 93 assert_select 'a[href="/issues/1"]', :text => /Cannot print recipes/
94 94 assert_select 'a[href="/issues/5"]', 0
95 95 end
96 96
97 97 def test_index_with_project_and_subprojects
98 98 Setting.display_subprojects_issues = 1
99 99 get :index, :project_id => 1
100 100 assert_response :success
101 101
102 102 assert_select 'a[href="/issues/1"]', :text => /Cannot print recipes/
103 103 assert_select 'a[href="/issues/5"]', :text => /Subproject issue/
104 104 assert_select 'a[href="/issues/6"]', 0
105 105 end
106 106
107 107 def test_index_with_project_and_subprojects_should_show_private_subprojects_with_permission
108 108 @request.session[:user_id] = 2
109 109 Setting.display_subprojects_issues = 1
110 110 get :index, :project_id => 1
111 111 assert_response :success
112 112
113 113 assert_select 'a[href="/issues/1"]', :text => /Cannot print recipes/
114 114 assert_select 'a[href="/issues/5"]', :text => /Subproject issue/
115 115 assert_select 'a[href="/issues/6"]', :text => /Issue of a private subproject/
116 116 end
117 117
118 118 def test_index_with_project_and_default_filter
119 119 get :index, :project_id => 1, :set_filter => 1
120 120 assert_response :success
121 121
122 122 # default filter
123 123 assert_query_filters [['status_id', 'o', '']]
124 124 end
125 125
126 126 def test_index_with_project_and_filter
127 127 get :index, :project_id => 1, :set_filter => 1,
128 128 :f => ['tracker_id'],
129 129 :op => {'tracker_id' => '='},
130 130 :v => {'tracker_id' => ['1']}
131 131 assert_response :success
132 132
133 133 assert_query_filters [['tracker_id', '=', '1']]
134 134 end
135 135
136 136 def test_index_with_short_filters
137 137 to_test = {
138 138 'status_id' => {
139 139 'o' => { :op => 'o', :values => [''] },
140 140 'c' => { :op => 'c', :values => [''] },
141 141 '7' => { :op => '=', :values => ['7'] },
142 142 '7|3|4' => { :op => '=', :values => ['7', '3', '4'] },
143 143 '=7' => { :op => '=', :values => ['7'] },
144 144 '!3' => { :op => '!', :values => ['3'] },
145 145 '!7|3|4' => { :op => '!', :values => ['7', '3', '4'] }},
146 146 'subject' => {
147 147 'This is a subject' => { :op => '=', :values => ['This is a subject'] },
148 148 'o' => { :op => '=', :values => ['o'] },
149 149 '~This is part of a subject' => { :op => '~', :values => ['This is part of a subject'] },
150 150 '!~This is part of a subject' => { :op => '!~', :values => ['This is part of a subject'] }},
151 151 'tracker_id' => {
152 152 '3' => { :op => '=', :values => ['3'] },
153 153 '=3' => { :op => '=', :values => ['3'] }},
154 154 'start_date' => {
155 155 '2011-10-12' => { :op => '=', :values => ['2011-10-12'] },
156 156 '=2011-10-12' => { :op => '=', :values => ['2011-10-12'] },
157 157 '>=2011-10-12' => { :op => '>=', :values => ['2011-10-12'] },
158 158 '<=2011-10-12' => { :op => '<=', :values => ['2011-10-12'] },
159 159 '><2011-10-01|2011-10-30' => { :op => '><', :values => ['2011-10-01', '2011-10-30'] },
160 160 '<t+2' => { :op => '<t+', :values => ['2'] },
161 161 '>t+2' => { :op => '>t+', :values => ['2'] },
162 162 't+2' => { :op => 't+', :values => ['2'] },
163 163 't' => { :op => 't', :values => [''] },
164 164 'w' => { :op => 'w', :values => [''] },
165 165 '>t-2' => { :op => '>t-', :values => ['2'] },
166 166 '<t-2' => { :op => '<t-', :values => ['2'] },
167 167 't-2' => { :op => 't-', :values => ['2'] }},
168 168 'created_on' => {
169 169 '>=2011-10-12' => { :op => '>=', :values => ['2011-10-12'] },
170 170 '<t-2' => { :op => '<t-', :values => ['2'] },
171 171 '>t-2' => { :op => '>t-', :values => ['2'] },
172 172 't-2' => { :op => 't-', :values => ['2'] }},
173 173 'cf_1' => {
174 174 'c' => { :op => '=', :values => ['c'] },
175 175 '!c' => { :op => '!', :values => ['c'] },
176 176 '!*' => { :op => '!*', :values => [''] },
177 177 '*' => { :op => '*', :values => [''] }},
178 178 'estimated_hours' => {
179 179 '=13.4' => { :op => '=', :values => ['13.4'] },
180 180 '>=45' => { :op => '>=', :values => ['45'] },
181 181 '<=125' => { :op => '<=', :values => ['125'] },
182 182 '><10.5|20.5' => { :op => '><', :values => ['10.5', '20.5'] },
183 183 '!*' => { :op => '!*', :values => [''] },
184 184 '*' => { :op => '*', :values => [''] }}
185 185 }
186 186
187 187 default_filter = { 'status_id' => {:operator => 'o', :values => [''] }}
188 188
189 189 to_test.each do |field, expression_and_expected|
190 190 expression_and_expected.each do |filter_expression, expected|
191 191
192 192 get :index, :set_filter => 1, field => filter_expression
193 193 assert_response :success
194 194
195 195 expected_with_default = default_filter.merge({field => {:operator => expected[:op], :values => expected[:values]}})
196 196 assert_query_filters expected_with_default.map {|f, v| [f, v[:operator], v[:values]]}
197 197 end
198 198 end
199 199 end
200 200
201 201 def test_index_with_project_and_empty_filters
202 202 get :index, :project_id => 1, :set_filter => 1, :fields => ['']
203 203 assert_response :success
204 204
205 205 # no filter
206 206 assert_query_filters []
207 207 end
208 208
209 209 def test_index_with_project_custom_field_filter
210 210 field = ProjectCustomField.create!(:name => 'Client', :is_filter => true, :field_format => 'string')
211 211 CustomValue.create!(:custom_field => field, :customized => Project.find(3), :value => 'Foo')
212 212 CustomValue.create!(:custom_field => field, :customized => Project.find(5), :value => 'Foo')
213 213 filter_name = "project.cf_#{field.id}"
214 214 @request.session[:user_id] = 1
215 215
216 216 get :index, :set_filter => 1,
217 217 :f => [filter_name],
218 218 :op => {filter_name => '='},
219 219 :v => {filter_name => ['Foo']},
220 220 :c => ['project']
221 221 assert_response :success
222 222
223 223 assert_equal [3, 5], issues_in_list.map(&:project_id).uniq.sort
224 224 end
225 225
226 226 def test_index_with_query
227 227 get :index, :project_id => 1, :query_id => 5
228 228 assert_response :success
229 229 end
230 230
231 231 def test_index_with_query_grouped_by_tracker
232 232 get :index, :project_id => 1, :query_id => 6
233 233 assert_response :success
234 234 assert_select 'tr.group span.count'
235 235 end
236 236
237 237 def test_index_with_query_grouped_and_sorted_by_category
238 238 get :index, :project_id => 1, :set_filter => 1, :group_by => "category", :sort => "category"
239 239 assert_response :success
240 240 assert_select 'tr.group span.count'
241 241 end
242 242
243 243 def test_index_with_query_grouped_and_sorted_by_fixed_version
244 244 get :index, :project_id => 1, :set_filter => 1, :group_by => "fixed_version", :sort => "fixed_version"
245 245 assert_response :success
246 246 assert_select 'tr.group span.count'
247 247 end
248 248
249 249 def test_index_with_query_grouped_and_sorted_by_fixed_version_in_reverse_order
250 250 get :index, :project_id => 1, :set_filter => 1, :group_by => "fixed_version", :sort => "fixed_version:desc"
251 251 assert_response :success
252 252 assert_select 'tr.group span.count'
253 253 end
254 254
255 255 def test_index_with_query_grouped_by_list_custom_field
256 256 get :index, :project_id => 1, :query_id => 9
257 257 assert_response :success
258 258 assert_select 'tr.group span.count'
259 259 end
260 260
261 261 def test_index_with_query_grouped_by_key_value_custom_field
262 262 cf = IssueCustomField.create!(:name => 'Key', :is_for_all => true, :tracker_ids => [1,2,3], :field_format => 'enumeration')
263 263 cf.enumerations << valueb = CustomFieldEnumeration.new(:name => 'Value B', :position => 1)
264 264 cf.enumerations << valuea = CustomFieldEnumeration.new(:name => 'Value A', :position => 2)
265 265 CustomValue.create!(:custom_field => cf, :customized => Issue.find(1), :value => valueb.id)
266 266 CustomValue.create!(:custom_field => cf, :customized => Issue.find(2), :value => valueb.id)
267 267 CustomValue.create!(:custom_field => cf, :customized => Issue.find(3), :value => valuea.id)
268 268 CustomValue.create!(:custom_field => cf, :customized => Issue.find(5), :value => '')
269 269
270 270 get :index, :project_id => 1, :set_filter => 1, :group_by => "cf_#{cf.id}"
271 271 assert_response :success
272 272
273 273 assert_select 'tr.group', 3
274 274 assert_select 'tr.group' do
275 275 assert_select 'span.name', :text => 'Value B'
276 276 assert_select 'span.count', :text => '2'
277 277 end
278 278 assert_select 'tr.group' do
279 279 assert_select 'span.name', :text => 'Value A'
280 280 assert_select 'span.count', :text => '1'
281 281 end
282 282 end
283 283
284 284 def test_index_with_query_grouped_by_user_custom_field
285 285 cf = IssueCustomField.create!(:name => 'User', :is_for_all => true, :tracker_ids => [1,2,3], :field_format => 'user')
286 286 CustomValue.create!(:custom_field => cf, :customized => Issue.find(1), :value => '2')
287 287 CustomValue.create!(:custom_field => cf, :customized => Issue.find(2), :value => '3')
288 288 CustomValue.create!(:custom_field => cf, :customized => Issue.find(3), :value => '3')
289 289 CustomValue.create!(:custom_field => cf, :customized => Issue.find(5), :value => '')
290 290
291 291 get :index, :project_id => 1, :set_filter => 1, :group_by => "cf_#{cf.id}"
292 292 assert_response :success
293 293
294 294 assert_select 'tr.group', 3
295 295 assert_select 'tr.group' do
296 296 assert_select 'a', :text => 'John Smith'
297 297 assert_select 'span.count', :text => '1'
298 298 end
299 299 assert_select 'tr.group' do
300 300 assert_select 'a', :text => 'Dave Lopper'
301 301 assert_select 'span.count', :text => '2'
302 302 end
303 303 end
304 304
305 305 def test_index_grouped_by_boolean_custom_field_should_distinguish_blank_and_false_values
306 306 cf = IssueCustomField.create!(:name => 'Bool', :is_for_all => true, :tracker_ids => [1,2,3], :field_format => 'bool')
307 307 CustomValue.create!(:custom_field => cf, :customized => Issue.find(1), :value => '1')
308 308 CustomValue.create!(:custom_field => cf, :customized => Issue.find(2), :value => '0')
309 309 CustomValue.create!(:custom_field => cf, :customized => Issue.find(3), :value => '')
310 310
311 311 with_settings :default_language => 'en' do
312 312 get :index, :project_id => 1, :set_filter => 1, :group_by => "cf_#{cf.id}"
313 313 assert_response :success
314 314 end
315 315
316 316 assert_select 'tr.group', 3
317 317 assert_select 'tr.group', :text => /Yes/
318 318 assert_select 'tr.group', :text => /No/
319 319 assert_select 'tr.group', :text => /blank/
320 320 end
321 321
322 322 def test_index_grouped_by_boolean_custom_field_with_false_group_in_first_position_should_show_the_group
323 323 cf = IssueCustomField.create!(:name => 'Bool', :is_for_all => true, :tracker_ids => [1,2,3], :field_format => 'bool', :is_filter => true)
324 324 CustomValue.create!(:custom_field => cf, :customized => Issue.find(1), :value => '0')
325 325 CustomValue.create!(:custom_field => cf, :customized => Issue.find(2), :value => '0')
326 326
327 327 with_settings :default_language => 'en' do
328 328 get :index, :project_id => 1, :set_filter => 1, "cf_#{cf.id}" => "*", :group_by => "cf_#{cf.id}"
329 329 assert_response :success
330 330 end
331 331
332 332 assert_equal [1, 2], issues_in_list.map(&:id).sort
333 333 assert_select 'tr.group', 1
334 334 assert_select 'tr.group', :text => /No/
335 335 end
336 336
337 337 def test_index_with_query_grouped_by_tracker_in_normal_order
338 338 3.times {|i| Issue.generate!(:tracker_id => (i + 1))}
339 339
340 340 get :index, :set_filter => 1, :group_by => 'tracker', :sort => 'id:desc'
341 341 assert_response :success
342 342
343 343 assert_equal ["Bug", "Feature request", "Support request"],
344 344 css_select("tr.issue td.tracker").map(&:text).uniq
345 345 end
346 346
347 347 def test_index_with_query_grouped_by_tracker_in_reverse_order
348 348 3.times {|i| Issue.generate!(:tracker_id => (i + 1))}
349 349
350 350 get :index, :set_filter => 1, :group_by => 'tracker', :c => ['tracker', 'subject'], :sort => 'id:desc,tracker:desc'
351 351 assert_response :success
352 352
353 353 assert_equal ["Bug", "Feature request", "Support request"].reverse,
354 354 css_select("tr.issue td.tracker").map(&:text).uniq
355 355 end
356 356
357 357 def test_index_with_query_id_and_project_id_should_set_session_query
358 358 get :index, :project_id => 1, :query_id => 4
359 359 assert_response :success
360 360 assert_kind_of Hash, session[:issue_query]
361 361 assert_equal 4, session[:issue_query][:id]
362 362 assert_equal 1, session[:issue_query][:project_id]
363 363 end
364 364
365 365 def test_index_with_invalid_query_id_should_respond_404
366 366 get :index, :project_id => 1, :query_id => 999
367 367 assert_response 404
368 368 end
369 369
370 370 def test_index_with_cross_project_query_in_session_should_show_project_issues
371 371 q = IssueQuery.create!(:name => "cross_project_query", :user_id => 2, :project => nil, :column_names => ['project'])
372 372 @request.session[:issue_query] = {:id => q.id, :project_id => 1}
373 373
374 374 with_settings :display_subprojects_issues => '0' do
375 375 get :index, :project_id => 1
376 376 end
377 377 assert_response :success
378 378
379 379 assert_select 'h2', :text => q.name
380 380 assert_equal ["eCookbook"], css_select("tr.issue td.project").map(&:text).uniq
381 381 end
382 382
383 383 def test_private_query_should_not_be_available_to_other_users
384 384 q = IssueQuery.create!(:name => "private", :user => User.find(2), :visibility => IssueQuery::VISIBILITY_PRIVATE, :project => nil)
385 385 @request.session[:user_id] = 3
386 386
387 387 get :index, :query_id => q.id
388 388 assert_response 403
389 389 end
390 390
391 391 def test_private_query_should_be_available_to_its_user
392 392 q = IssueQuery.create!(:name => "private", :user => User.find(2), :visibility => IssueQuery::VISIBILITY_PRIVATE, :project => nil)
393 393 @request.session[:user_id] = 2
394 394
395 395 get :index, :query_id => q.id
396 396 assert_response :success
397 397 end
398 398
399 399 def test_public_query_should_be_available_to_other_users
400 400 q = IssueQuery.create!(:name => "public", :user => User.find(2), :visibility => IssueQuery::VISIBILITY_PUBLIC, :project => nil)
401 401 @request.session[:user_id] = 3
402 402
403 403 get :index, :query_id => q.id
404 404 assert_response :success
405 405 end
406 406
407 407 def test_index_should_omit_page_param_in_export_links
408 408 get :index, :page => 2
409 409 assert_response :success
410 410 assert_select 'a.atom[href="/issues.atom"]'
411 411 assert_select 'a.csv[href="/issues.csv"]'
412 412 assert_select 'a.pdf[href="/issues.pdf"]'
413 413 assert_select 'form#csv-export-form[action="/issues.csv"]'
414 414 end
415 415
416 416 def test_index_should_not_warn_when_not_exceeding_export_limit
417 417 with_settings :issues_export_limit => 200 do
418 418 get :index
419 419 assert_select '#csv-export-options p.icon-warning', 0
420 420 end
421 421 end
422 422
423 423 def test_index_should_warn_when_exceeding_export_limit
424 424 with_settings :issues_export_limit => 2 do
425 425 get :index
426 426 assert_select '#csv-export-options p.icon-warning', :text => %r{limit: 2}
427 427 end
428 428 end
429 429
430 430 def test_index_should_include_query_params_as_hidden_fields_in_csv_export_form
431 431 get :index, :project_id => 1, :set_filter => "1", :tracker_id => "2", :sort => 'status', :c => ["status", "priority"]
432 432
433 433 assert_select '#csv-export-form[action=?]', '/projects/ecookbook/issues.csv'
434 434 assert_select '#csv-export-form[method=?]', 'get'
435 435
436 436 assert_select '#csv-export-form' do
437 437 assert_select 'input[name=?][value=?]', 'set_filter', '1'
438 438
439 439 assert_select 'input[name=?][value=?]', 'f[]', 'tracker_id'
440 440 assert_select 'input[name=?][value=?]', 'op[tracker_id]', '='
441 441 assert_select 'input[name=?][value=?]', 'v[tracker_id][]', '2'
442 442
443 443 assert_select 'input[name=?][value=?]', 'c[]', 'status'
444 444 assert_select 'input[name=?][value=?]', 'c[]', 'priority'
445 445
446 446 assert_select 'input[name=?][value=?]', 'sort', 'status'
447 447 end
448 448
449 449 get :index, :project_id => 1, :set_filter => "1", :f => []
450 450 assert_select '#csv-export-form input[name=?][value=?]', 'f[]', ''
451 451 end
452 452
453 453 def test_index_csv
454 454 get :index, :format => 'csv'
455 455 assert_response :success
456 456
457 457 assert_equal 'text/csv; header=present', @response.content_type
458 458 assert response.body.starts_with?("#,")
459 459 lines = response.body.chomp.split("\n")
460 460 # default columns + id and project
461 461 assert_equal Setting.issue_list_default_columns.size + 2, lines[0].split(',').size
462 462 end
463 463
464 464 def test_index_csv_with_project
465 465 get :index, :project_id => 1, :format => 'csv'
466 466 assert_response :success
467 467
468 468 assert_equal 'text/csv; header=present', @response.content_type
469 469 end
470 470
471 471 def test_index_csv_without_any_filters
472 472 @request.session[:user_id] = 1
473 473 Issue.create!(:project_id => 1, :tracker_id => 1, :status_id => 5, :subject => 'Closed issue', :author_id => 1)
474 474 get :index, :set_filter => 1, :f => [], :format => 'csv'
475 475 assert_response :success
476 476 # -1 for headers
477 477 assert_equal Issue.count, response.body.chomp.split("\n").size - 1
478 478 end
479 479
480 480 def test_index_csv_with_description
481 481 Issue.generate!(:description => 'test_index_csv_with_description')
482 482
483 483 with_settings :default_language => 'en' do
484 484 get :index, :format => 'csv', :csv => {:description => '1'}
485 485 assert_response :success
486 486 end
487 487
488 488 assert_equal 'text/csv; header=present', response.content_type
489 489 headers = response.body.chomp.split("\n").first.split(',')
490 490 assert_include 'Description', headers
491 491 assert_include 'test_index_csv_with_description', response.body
492 492 end
493 493
494 494 def test_index_csv_with_spent_time_column
495 495 issue = Issue.create!(:project_id => 1, :tracker_id => 1, :subject => 'test_index_csv_with_spent_time_column', :author_id => 2)
496 496 TimeEntry.create!(:project => issue.project, :issue => issue, :hours => 7.33, :user => User.find(2), :spent_on => Date.today)
497 497
498 498 get :index, :format => 'csv', :set_filter => '1', :c => %w(subject spent_hours)
499 499 assert_response :success
500 500 assert_equal 'text/csv; header=present', @response.content_type
501 501 lines = @response.body.chomp.split("\n")
502 502 assert_include "#{issue.id},#{issue.subject},7.33", lines
503 503 end
504 504
505 505 def test_index_csv_with_all_columns
506 506 get :index, :format => 'csv', :csv => {:columns => 'all'}
507 507 assert_response :success
508 508
509 509 assert_equal 'text/csv; header=present', @response.content_type
510 510 assert_match /\A#,/, response.body
511 511 lines = response.body.chomp.split("\n")
512 512 assert_equal IssueQuery.new.available_inline_columns.size, lines[0].split(',').size
513 513 end
514 514
515 515 def test_index_csv_with_multi_column_field
516 516 CustomField.find(1).update_attribute :multiple, true
517 517 issue = Issue.find(1)
518 518 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
519 519 issue.save!
520 520
521 521 get :index, :format => 'csv', :csv => {:columns => 'all'}
522 522 assert_response :success
523 523 lines = @response.body.chomp.split("\n")
524 524 assert lines.detect {|line| line.include?('"MySQL, Oracle"')}
525 525 end
526 526
527 527 def test_index_csv_should_format_float_custom_fields_with_csv_decimal_separator
528 528 field = IssueCustomField.create!(:name => 'Float', :is_for_all => true, :tracker_ids => [1], :field_format => 'float')
529 529 issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {field.id => '185.6'})
530 530
531 531 with_settings :default_language => 'fr' do
532 532 get :index, :format => 'csv', :csv => {:columns => 'all'}
533 533 assert_response :success
534 534 issue_line = response.body.chomp.split("\n").map {|line| line.split(';')}.detect {|line| line[0]==issue.id.to_s}
535 535 assert_include '185,60', issue_line
536 536 end
537 537
538 538 with_settings :default_language => 'en' do
539 539 get :index, :format => 'csv', :csv => {:columns => 'all'}
540 540 assert_response :success
541 541 issue_line = response.body.chomp.split("\n").map {|line| line.split(',')}.detect {|line| line[0]==issue.id.to_s}
542 542 assert_include '185.60', issue_line
543 543 end
544 544 end
545 545
546 546 def test_index_csv_should_fill_parent_column_with_parent_id
547 547 Issue.delete_all
548 548 parent = Issue.generate!
549 549 child = Issue.generate!(:parent_issue_id => parent.id)
550 550
551 551 with_settings :default_language => 'en' do
552 552 get :index, :format => 'csv', :c => %w(parent)
553 553 end
554 554 lines = response.body.split("\n")
555 555 assert_include "#{child.id},#{parent.id}", lines
556 556 end
557 557
558 558 def test_index_csv_big_5
559 559 with_settings :default_language => "zh-TW" do
560 560 str_utf8 = "\xe4\xb8\x80\xe6\x9c\x88".force_encoding('UTF-8')
561 561 str_big5 = "\xa4@\xa4\xeb".force_encoding('Big5')
562 562 issue = Issue.generate!(:subject => str_utf8)
563 563
564 564 get :index, :project_id => 1,
565 565 :f => ['subject'],
566 566 :op => '=', :values => [str_utf8],
567 567 :format => 'csv'
568 568 assert_equal 'text/csv; header=present', @response.content_type
569 569 lines = @response.body.chomp.split("\n")
570 570 header = lines[0]
571 571 status = "\xaa\xac\xbaA".force_encoding('Big5')
572 572 assert_include status, header
573 573 issue_line = lines.find {|l| l =~ /^#{issue.id},/}
574 574 assert_include str_big5, issue_line
575 575 end
576 576 end
577 577
578 578 def test_index_csv_cannot_convert_should_be_replaced_big_5
579 579 with_settings :default_language => "zh-TW" do
580 580 str_utf8 = "\xe4\xbb\xa5\xe5\x86\x85".force_encoding('UTF-8')
581 581 issue = Issue.generate!(:subject => str_utf8)
582 582
583 583 get :index, :project_id => 1,
584 584 :f => ['subject'],
585 585 :op => '=', :values => [str_utf8],
586 586 :c => ['status', 'subject'],
587 587 :format => 'csv',
588 588 :set_filter => 1
589 589 assert_equal 'text/csv; header=present', @response.content_type
590 590 lines = @response.body.chomp.split("\n")
591 591 header = lines[0]
592 592 issue_line = lines.find {|l| l =~ /^#{issue.id},/}
593 593 s1 = "\xaa\xac\xbaA".force_encoding('Big5') # status
594 594 assert header.include?(s1)
595 595 s2 = issue_line.split(",")[2]
596 596 s3 = "\xa5H?".force_encoding('Big5') # subject
597 597 assert_equal s3, s2
598 598 end
599 599 end
600 600
601 601 def test_index_csv_tw
602 602 with_settings :default_language => "zh-TW" do
603 603 str1 = "test_index_csv_tw"
604 604 issue = Issue.generate!(:subject => str1, :estimated_hours => '1234.5')
605 605
606 606 get :index, :project_id => 1,
607 607 :f => ['subject'],
608 608 :op => '=', :values => [str1],
609 609 :c => ['estimated_hours', 'subject'],
610 610 :format => 'csv',
611 611 :set_filter => 1
612 612 assert_equal 'text/csv; header=present', @response.content_type
613 613 lines = @response.body.chomp.split("\n")
614 614 assert_include "#{issue.id},1234.50,#{str1}", lines
615 615 end
616 616 end
617 617
618 618 def test_index_csv_fr
619 619 with_settings :default_language => "fr" do
620 620 str1 = "test_index_csv_fr"
621 621 issue = Issue.generate!(:subject => str1, :estimated_hours => '1234.5')
622 622
623 623 get :index, :project_id => 1,
624 624 :f => ['subject'],
625 625 :op => '=', :values => [str1],
626 626 :c => ['estimated_hours', 'subject'],
627 627 :format => 'csv',
628 628 :set_filter => 1
629 629 assert_equal 'text/csv; header=present', @response.content_type
630 630 lines = @response.body.chomp.split("\n")
631 631 assert_include "#{issue.id};1234,50;#{str1}", lines
632 632 end
633 633 end
634 634
635 635 def test_index_pdf
636 636 ["en", "zh", "zh-TW", "ja", "ko"].each do |lang|
637 637 with_settings :default_language => lang do
638 638
639 639 get :index
640 640 assert_response :success
641 641
642 642 get :index, :format => 'pdf'
643 643 assert_response :success
644 644 assert_equal 'application/pdf', @response.content_type
645 645
646 646 get :index, :project_id => 1, :format => 'pdf'
647 647 assert_response :success
648 648 assert_equal 'application/pdf', @response.content_type
649 649
650 650 get :index, :project_id => 1, :query_id => 6, :format => 'pdf'
651 651 assert_response :success
652 652 assert_equal 'application/pdf', @response.content_type
653 653 end
654 654 end
655 655 end
656 656
657 657 def test_index_pdf_with_query_grouped_by_list_custom_field
658 658 get :index, :project_id => 1, :query_id => 9, :format => 'pdf'
659 659 assert_response :success
660 660 assert_equal 'application/pdf', @response.content_type
661 661 end
662 662
663 663 def test_index_atom
664 664 get :index, :project_id => 'ecookbook', :format => 'atom'
665 665 assert_response :success
666 666 assert_equal 'application/atom+xml', response.content_type
667 667
668 668 assert_select 'feed' do
669 669 assert_select 'link[rel=self][href=?]', 'http://test.host/projects/ecookbook/issues.atom'
670 670 assert_select 'link[rel=alternate][href=?]', 'http://test.host/projects/ecookbook/issues'
671 671 assert_select 'entry link[href=?]', 'http://test.host/issues/1'
672 672 end
673 673 end
674 674
675 675 def test_index_should_include_back_url_input
676 676 get :index, :project_id => 'ecookbook', :foo => 'bar'
677 677 assert_response :success
678 678 assert_select 'input[name=back_url][value=?]', '/projects/ecookbook/issues?foo=bar'
679 679 end
680 680
681 681 def test_index_sort
682 682 get :index, :sort => 'tracker,id:desc'
683 683 assert_response :success
684 684
685 685 sort_params = @request.session['issues_index_sort']
686 686 assert sort_params.is_a?(String)
687 687 assert_equal 'tracker,id:desc', sort_params
688 688
689 689 assert_equal issues_in_list.sort_by {|issue| [issue.tracker.position, -issue.id]}, issues_in_list
690 690 assert_select 'table.issues.sort-by-tracker.sort-asc'
691 691 end
692 692
693 693 def test_index_sort_by_field_not_included_in_columns
694 694 with_settings :issue_list_default_columns => %w(subject author) do
695 695 get :index, :sort => 'tracker'
696 696 assert_response :success
697 697 end
698 698 end
699 699
700 700 def test_index_sort_by_assigned_to
701 701 get :index, :sort => 'assigned_to'
702 702 assert_response :success
703 703
704 704 assignees = issues_in_list.map(&:assigned_to).compact
705 705 assert_equal assignees.sort, assignees
706 706 assert_select 'table.issues.sort-by-assigned-to.sort-asc'
707 707 end
708 708
709 709 def test_index_sort_by_assigned_to_desc
710 710 get :index, :sort => 'assigned_to:desc'
711 711 assert_response :success
712 712
713 713 assignees = issues_in_list.map(&:assigned_to).compact
714 714 assert_equal assignees.sort.reverse, assignees
715 715 assert_select 'table.issues.sort-by-assigned-to.sort-desc'
716 716 end
717 717
718 718 def test_index_group_by_assigned_to
719 719 get :index, :group_by => 'assigned_to', :sort => 'priority'
720 720 assert_response :success
721 721 end
722 722
723 723 def test_index_sort_by_author
724 724 get :index, :sort => 'author', :c => ['author']
725 725 assert_response :success
726 726
727 727 authors = issues_in_list.map(&:author)
728 728 assert_equal authors.sort, authors
729 729 end
730 730
731 731 def test_index_sort_by_author_desc
732 732 get :index, :sort => 'author:desc'
733 733 assert_response :success
734 734
735 735 authors = issues_in_list.map(&:author)
736 736 assert_equal authors.sort.reverse, authors
737 737 end
738 738
739 739 def test_index_group_by_author
740 740 get :index, :group_by => 'author', :sort => 'priority'
741 741 assert_response :success
742 742 end
743 743
744 744 def test_index_sort_by_spent_hours
745 745 get :index, :sort => 'spent_hours:desc'
746 746 assert_response :success
747 747 hours = issues_in_list.map(&:spent_hours)
748 748 assert_equal hours.sort.reverse, hours
749 749 end
750 750
751 751 def test_index_sort_by_total_spent_hours
752 752 get :index, :sort => 'total_spent_hours:desc'
753 753 assert_response :success
754 754 hours = issues_in_list.map(&:total_spent_hours)
755 755 assert_equal hours.sort.reverse, hours
756 756 end
757 757
758 758 def test_index_sort_by_total_estimated_hours
759 759 get :index, :sort => 'total_estimated_hours:desc'
760 760 assert_response :success
761 761 hours = issues_in_list.map(&:total_estimated_hours)
762 762 assert_equal hours.sort.reverse, hours
763 763 end
764 764
765 765 def test_index_sort_by_user_custom_field
766 766 cf = IssueCustomField.create!(:name => 'User', :is_for_all => true, :tracker_ids => [1,2,3], :field_format => 'user')
767 767 CustomValue.create!(:custom_field => cf, :customized => Issue.find(1), :value => '2')
768 768 CustomValue.create!(:custom_field => cf, :customized => Issue.find(2), :value => '3')
769 769 CustomValue.create!(:custom_field => cf, :customized => Issue.find(3), :value => '3')
770 770 CustomValue.create!(:custom_field => cf, :customized => Issue.find(5), :value => '')
771 771
772 772 get :index, :project_id => 1, :set_filter => 1, :sort => "cf_#{cf.id},id"
773 773 assert_response :success
774 774
775 775 assert_equal [2, 3, 1], issues_in_list.select {|issue| issue.custom_field_value(cf).present?}.map(&:id)
776 776 end
777 777
778 778 def test_index_with_columns
779 779 columns = ['tracker', 'subject', 'assigned_to']
780 780 get :index, :set_filter => 1, :c => columns
781 781 assert_response :success
782 782
783 783 # query should use specified columns + id and checkbox
784 784 assert_select 'table.issues thead th', columns.size + 2
785 785
786 786 # columns should be stored in session
787 787 assert_kind_of Hash, session[:issue_query]
788 788 assert_kind_of Array, session[:issue_query][:column_names]
789 789 assert_equal columns, session[:issue_query][:column_names].map(&:to_s)
790 790
791 791 # ensure only these columns are kept in the selected columns list
792 792 assert_select 'select#selected_columns option' do
793 793 assert_select 'option', 3
794 794 assert_select 'option[value=tracker]'
795 795 assert_select 'option[value=project]', 0
796 796 end
797 797 end
798 798
799 799 def test_index_without_project_should_implicitly_add_project_column_to_default_columns
800 800 with_settings :issue_list_default_columns => ['tracker', 'subject', 'assigned_to'] do
801 801 get :index, :set_filter => 1
802 802 end
803 803
804 804 # query should use specified columns
805 805 assert_equal ["#", "Project", "Tracker", "Subject", "Assignee"], columns_in_issues_list
806 806 end
807 807
808 808 def test_index_without_project_and_explicit_default_columns_should_not_add_project_column
809 809 with_settings :issue_list_default_columns => ['tracker', 'subject', 'assigned_to'] do
810 810 columns = ['id', 'tracker', 'subject', 'assigned_to']
811 811 get :index, :set_filter => 1, :c => columns
812 812 end
813 813
814 814 # query should use specified columns
815 815 assert_equal ["#", "Tracker", "Subject", "Assignee"], columns_in_issues_list
816 816 end
817 817
818 818 def test_index_with_default_columns_should_respect_default_columns_order
819 819 columns = ['assigned_to', 'subject', 'status', 'tracker']
820 820 with_settings :issue_list_default_columns => columns do
821 821 get :index, :project_id => 1, :set_filter => 1
822 822
823 823 assert_equal ["#", "Assignee", "Subject", "Status", "Tracker"], columns_in_issues_list
824 824 end
825 825 end
826 826
827 827 def test_index_with_custom_field_column
828 828 columns = %w(tracker subject cf_2)
829 829 get :index, :set_filter => 1, :c => columns
830 830 assert_response :success
831 831
832 832 # query should use specified columns
833 833 assert_equal ["#", "Tracker", "Subject", "Searchable field"], columns_in_issues_list
834 834 assert_select 'table.issues td.cf_2.string'
835 835 end
836 836
837 837 def test_index_with_multi_custom_field_column
838 838 field = CustomField.find(1)
839 839 field.update_attribute :multiple, true
840 840 issue = Issue.find(1)
841 841 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
842 842 issue.save!
843 843
844 844 get :index, :set_filter => 1, :c => %w(tracker subject cf_1)
845 845 assert_response :success
846 846
847 847 assert_select 'table.issues td.cf_1', :text => 'MySQL, Oracle'
848 848 end
849 849
850 850 def test_index_with_multi_user_custom_field_column
851 851 field = IssueCustomField.create!(:name => 'Multi user', :field_format => 'user', :multiple => true,
852 852 :tracker_ids => [1], :is_for_all => true)
853 853 issue = Issue.find(1)
854 854 issue.custom_field_values = {field.id => ['2', '3']}
855 855 issue.save!
856 856
857 857 get :index, :set_filter => 1, :c => ['tracker', 'subject', "cf_#{field.id}"]
858 858 assert_response :success
859 859
860 860 assert_select "table.issues td.cf_#{field.id}" do
861 861 assert_select 'a', 2
862 862 assert_select 'a[href=?]', '/users/2', :text => 'John Smith'
863 863 assert_select 'a[href=?]', '/users/3', :text => 'Dave Lopper'
864 864 end
865 865 end
866 866
867 867 def test_index_with_date_column
868 868 with_settings :date_format => '%d/%m/%Y' do
869 869 Issue.find(1).update_attribute :start_date, '1987-08-24'
870 870 get :index, :set_filter => 1, :c => %w(start_date)
871 871 assert_select "table.issues td.start_date", :text => '24/08/1987'
872 872 end
873 873 end
874 874
875 875 def test_index_with_done_ratio_column
876 876 Issue.find(1).update_attribute :done_ratio, 40
877 877 get :index, :set_filter => 1, :c => %w(done_ratio)
878 878 assert_select 'table.issues td.done_ratio' do
879 879 assert_select 'table.progress' do
880 880 assert_select 'td.closed[style=?]', 'width: 40%;'
881 881 end
882 882 end
883 883 end
884 884
885 885 def test_index_with_spent_hours_column
886 886 Issue.expects(:load_visible_spent_hours).once
887 887 get :index, :set_filter => 1, :c => %w(subject spent_hours)
888 888 assert_select 'table.issues tr#issue-3 td.spent_hours', :text => '1.00'
889 889 end
890 890
891 891 def test_index_with_total_spent_hours_column
892 892 Issue.expects(:load_visible_total_spent_hours).once
893 893 get :index, :set_filter => 1, :c => %w(subject total_spent_hours)
894 894 assert_select 'table.issues tr#issue-3 td.total_spent_hours', :text => '1.00'
895 895 end
896 896
897 897 def test_index_with_total_estimated_hours_column
898 898 get :index, :set_filter => 1, :c => %w(subject total_estimated_hours)
899 899 assert_select 'table.issues td.total_estimated_hours'
900 900 end
901 901
902 902 def test_index_should_not_show_spent_hours_column_without_permission
903 903 Role.anonymous.remove_permission! :view_time_entries
904 904 get :index, :set_filter => 1, :c => %w(subject spent_hours)
905 905 assert_select 'td.spent_hours', 0
906 906 end
907 907
908 908 def test_index_with_fixed_version_column
909 909 get :index, :set_filter => 1, :c => %w(fixed_version)
910 910 assert_select 'table.issues td.fixed_version' do
911 911 assert_select 'a[href=?]', '/versions/2', :text => 'eCookbook - 1.0'
912 912 end
913 913 end
914 914
915 915 def test_index_with_relations_column
916 916 IssueRelation.delete_all
917 917 IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(1), :issue_to => Issue.find(7))
918 918 IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(8), :issue_to => Issue.find(1))
919 919 IssueRelation.create!(:relation_type => "blocks", :issue_from => Issue.find(1), :issue_to => Issue.find(11))
920 920 IssueRelation.create!(:relation_type => "blocks", :issue_from => Issue.find(12), :issue_to => Issue.find(2))
921 921
922 922 get :index, :set_filter => 1, :c => %w(subject relations)
923 923 assert_response :success
924 924 assert_select "tr#issue-1 td.relations" do
925 925 assert_select "span", 3
926 926 assert_select "span", :text => "Related to #7"
927 927 assert_select "span", :text => "Related to #8"
928 928 assert_select "span", :text => "Blocks #11"
929 929 end
930 930 assert_select "tr#issue-2 td.relations" do
931 931 assert_select "span", 1
932 932 assert_select "span", :text => "Blocked by #12"
933 933 end
934 934 assert_select "tr#issue-3 td.relations" do
935 935 assert_select "span", 0
936 936 end
937 937
938 938 get :index, :set_filter => 1, :c => %w(relations), :format => 'csv'
939 939 assert_response :success
940 940 assert_equal 'text/csv; header=present', response.content_type
941 941 lines = response.body.chomp.split("\n")
942 942 assert_include '1,"Related to #7, Related to #8, Blocks #11"', lines
943 943 assert_include '2,Blocked by #12', lines
944 944 assert_include '3,""', lines
945 945
946 946 get :index, :set_filter => 1, :c => %w(subject relations), :format => 'pdf'
947 947 assert_response :success
948 948 assert_equal 'application/pdf', response.content_type
949 949 end
950 950
951 951 def test_index_with_description_column
952 952 get :index, :set_filter => 1, :c => %w(subject description)
953 953
954 954 assert_select 'table.issues thead th', 3 # columns: chekbox + id + subject
955 955 assert_select 'td.description[colspan="3"]', :text => 'Unable to print recipes'
956 956
957 957 get :index, :set_filter => 1, :c => %w(subject description), :format => 'pdf'
958 958 assert_response :success
959 959 assert_equal 'application/pdf', response.content_type
960 960 end
961 961
962 962 def test_index_with_parent_column
963 963 Issue.delete_all
964 964 parent = Issue.generate!
965 965 child = Issue.generate!(:parent_issue_id => parent.id)
966 966
967 967 get :index, :c => %w(parent)
968 968
969 969 assert_select 'td.parent', :text => "#{parent.tracker} ##{parent.id}"
970 970 assert_select 'td.parent a[title=?]', parent.subject
971 971 end
972 972
973 973 def test_index_with_estimated_hours_total
974 974 Issue.delete_all
975 975 Issue.generate!(:estimated_hours => 5.5)
976 976 Issue.generate!(:estimated_hours => 1.1)
977 977
978 978 get :index, :t => %w(estimated_hours)
979 979 assert_response :success
980 980 assert_select '.query-totals'
981 981 assert_select '.total-for-estimated-hours span.value', :text => '6.60'
982 982 assert_select 'input[type=checkbox][name=?][value=estimated_hours][checked=checked]', 't[]'
983 983 end
984 984
985 985 def test_index_with_grouped_query_and_estimated_hours_total
986 986 Issue.delete_all
987 987 Issue.generate!(:estimated_hours => 5.5, :category_id => 1)
988 988 Issue.generate!(:estimated_hours => 2.3, :category_id => 1)
989 989 Issue.generate!(:estimated_hours => 1.1, :category_id => 2)
990 990 Issue.generate!(:estimated_hours => 4.6)
991 991
992 992 get :index, :t => %w(estimated_hours), :group_by => 'category'
993 993 assert_response :success
994 994 assert_select '.query-totals'
995 995 assert_select '.query-totals .total-for-estimated-hours span.value', :text => '13.50'
996 996 assert_select 'tr.group', :text => /Printing/ do
997 997 assert_select '.total-for-estimated-hours span.value', :text => '7.80'
998 998 end
999 999 assert_select 'tr.group', :text => /Recipes/ do
1000 1000 assert_select '.total-for-estimated-hours span.value', :text => '1.10'
1001 1001 end
1002 1002 assert_select 'tr.group', :text => /blank/ do
1003 1003 assert_select '.total-for-estimated-hours span.value', :text => '4.60'
1004 1004 end
1005 1005 end
1006 1006
1007 1007 def test_index_with_int_custom_field_total
1008 1008 field = IssueCustomField.generate!(:field_format => 'int', :is_for_all => true)
1009 1009 CustomValue.create!(:customized => Issue.find(1), :custom_field => field, :value => '2')
1010 1010 CustomValue.create!(:customized => Issue.find(2), :custom_field => field, :value => '7')
1011 1011
1012 1012 get :index, :t => ["cf_#{field.id}"]
1013 1013 assert_response :success
1014 1014 assert_select '.query-totals'
1015 1015 assert_select ".total-for-cf-#{field.id} span.value", :text => '9'
1016 1016 end
1017 1017
1018 1018 def test_index_totals_should_default_to_settings
1019 1019 with_settings :issue_list_default_totals => ['estimated_hours'] do
1020 1020 get :index
1021 1021 assert_response :success
1022 1022 assert_select '.total-for-estimated-hours span.value'
1023 1023 assert_select '.query-totals>span', 1
1024 1024 end
1025 1025 end
1026 1026
1027 1027 def test_index_send_html_if_query_is_invalid
1028 1028 get :index, :f => ['start_date'], :op => {:start_date => '='}
1029 1029 assert_equal 'text/html', @response.content_type
1030 1030 assert_select_error /Start date cannot be blank/i
1031 1031 end
1032 1032
1033 1033 def test_index_send_nothing_if_query_is_invalid
1034 1034 get :index, :f => ['start_date'], :op => {:start_date => '='}, :format => 'csv'
1035 1035 assert_equal 'text/csv', @response.content_type
1036 1036 assert @response.body.blank?
1037 1037 end
1038 1038
1039 1039 def test_index_should_include_new_issue_link
1040 1040 @request.session[:user_id] = 2
1041 1041 get :index, :project_id => 1
1042 1042 assert_select '#content a.new-issue[href="/projects/ecookbook/issues/new"]', :text => 'New issue'
1043 1043 end
1044 1044
1045 1045 def test_index_should_not_include_new_issue_link_for_project_without_trackers
1046 1046 Project.find(1).trackers.clear
1047 1047
1048 1048 @request.session[:user_id] = 2
1049 1049 get :index, :project_id => 1
1050 1050 assert_select '#content a.new-issue', 0
1051 1051 end
1052 1052
1053 1053 def test_index_should_not_include_new_issue_link_for_users_with_copy_issues_permission_only
1054 1054 role = Role.find(1)
1055 1055 role.remove_permission! :add_issues
1056 1056 role.add_permission! :copy_issues
1057 1057
1058 1058 @request.session[:user_id] = 2
1059 1059 get :index, :project_id => 1
1060 1060 assert_select '#content a.new-issue', 0
1061 1061 end
1062 1062
1063 1063 def test_index_without_project_should_include_new_issue_link
1064 1064 @request.session[:user_id] = 2
1065 1065 get :index
1066 1066 assert_select '#content a.new-issue[href="/issues/new"]', :text => 'New issue'
1067 1067 end
1068 1068
1069 1069 def test_index_should_not_include_new_issue_tab_when_disabled
1070 1070 with_settings :new_item_menu_tab => '0' do
1071 1071 @request.session[:user_id] = 2
1072 1072 get :index, :project_id => 1
1073 1073 assert_select '#main-menu a.new-issue', 0
1074 1074 end
1075 1075 end
1076 1076
1077 1077 def test_index_should_include_new_issue_tab_when_enabled
1078 1078 with_settings :new_item_menu_tab => '1' do
1079 1079 @request.session[:user_id] = 2
1080 1080 get :index, :project_id => 1
1081 1081 assert_select '#main-menu a.new-issue[href="/projects/ecookbook/issues/new"]', :text => 'New issue'
1082 1082 end
1083 1083 end
1084 1084
1085 1085 def test_new_should_have_new_issue_tab_as_current_menu_item
1086 1086 with_settings :new_item_menu_tab => '1' do
1087 1087 @request.session[:user_id] = 2
1088 1088 get :new, :project_id => 1
1089 1089 assert_select '#main-menu a.new-issue.selected'
1090 1090 end
1091 1091 end
1092 1092
1093 1093 def test_index_should_not_include_new_issue_tab_for_project_without_trackers
1094 1094 with_settings :new_item_menu_tab => '1' do
1095 1095 Project.find(1).trackers.clear
1096 1096
1097 1097 @request.session[:user_id] = 2
1098 1098 get :index, :project_id => 1
1099 1099 assert_select '#main-menu a.new-issue', 0
1100 1100 end
1101 1101 end
1102 1102
1103 1103 def test_index_should_not_include_new_issue_tab_for_users_with_copy_issues_permission_only
1104 1104 with_settings :new_item_menu_tab => '1' do
1105 1105 role = Role.find(1)
1106 1106 role.remove_permission! :add_issues
1107 1107 role.add_permission! :copy_issues
1108 1108
1109 1109 @request.session[:user_id] = 2
1110 1110 get :index, :project_id => 1
1111 1111 assert_select '#main-menu a.new-issue', 0
1112 1112 end
1113 1113 end
1114 1114
1115 1115 def test_show_by_anonymous
1116 1116 get :show, :id => 1
1117 1117 assert_response :success
1118 1118
1119 1119 assert_select 'div.issue div.description', :text => /Unable to print recipes/
1120 1120 # anonymous role is allowed to add a note
1121 1121 assert_select 'form#issue-form' do
1122 1122 assert_select 'fieldset' do
1123 1123 assert_select 'legend', :text => 'Notes'
1124 1124 assert_select 'textarea[name=?]', 'issue[notes]'
1125 1125 end
1126 1126 end
1127 1127 assert_select 'title', :text => "Bug #1: Cannot print recipes - eCookbook - Redmine"
1128 1128 end
1129 1129
1130 1130 def test_show_by_manager
1131 1131 @request.session[:user_id] = 2
1132 1132 get :show, :id => 1
1133 1133
1134 1134 assert_select 'a', :text => /Quote/
1135 1135 assert_select 'form#issue-form' do
1136 1136 assert_select 'fieldset' do
1137 1137 assert_select 'legend', :text => 'Change properties'
1138 1138 assert_select 'input[name=?]', 'issue[subject]'
1139 1139 end
1140 1140 assert_select 'fieldset' do
1141 1141 assert_select 'legend', :text => 'Log time'
1142 1142 assert_select 'input[name=?]', 'time_entry[hours]'
1143 1143 end
1144 1144 assert_select 'fieldset' do
1145 1145 assert_select 'legend', :text => 'Notes'
1146 1146 assert_select 'textarea[name=?]', 'issue[notes]'
1147 1147 end
1148 1148 end
1149 1149 end
1150 1150
1151 1151 def test_show_should_display_update_form
1152 1152 @request.session[:user_id] = 2
1153 1153 get :show, :id => 1
1154 1154 assert_response :success
1155 1155
1156 1156 assert_select 'form#issue-form' do
1157 1157 assert_select 'input[name=?]', 'issue[is_private]'
1158 1158 assert_select 'select[name=?]', 'issue[project_id]'
1159 1159 assert_select 'select[name=?]', 'issue[tracker_id]'
1160 1160 assert_select 'input[name=?]', 'issue[subject]'
1161 1161 assert_select 'textarea[name=?]', 'issue[description]'
1162 1162 assert_select 'select[name=?]', 'issue[status_id]'
1163 1163 assert_select 'select[name=?]', 'issue[priority_id]'
1164 1164 assert_select 'select[name=?]', 'issue[assigned_to_id]'
1165 1165 assert_select 'select[name=?]', 'issue[category_id]'
1166 1166 assert_select 'select[name=?]', 'issue[fixed_version_id]'
1167 1167 assert_select 'input[name=?]', 'issue[parent_issue_id]'
1168 1168 assert_select 'input[name=?]', 'issue[start_date]'
1169 1169 assert_select 'input[name=?]', 'issue[due_date]'
1170 1170 assert_select 'select[name=?]', 'issue[done_ratio]'
1171 1171 assert_select 'input[name=?]', 'issue[custom_field_values][2]'
1172 1172 assert_select 'input[name=?]', 'issue[watcher_user_ids][]', 0
1173 1173 assert_select 'textarea[name=?]', 'issue[notes]'
1174 1174 end
1175 1175 end
1176 1176
1177 1177 def test_show_should_display_update_form_with_minimal_permissions
1178 1178 Role.find(1).update_attribute :permissions, [:view_issues, :add_issue_notes]
1179 1179 WorkflowTransition.where(:role_id => 1).delete_all
1180 1180
1181 1181 @request.session[:user_id] = 2
1182 1182 get :show, :id => 1
1183 1183 assert_response :success
1184 1184
1185 1185 assert_select 'form#issue-form' do
1186 1186 assert_select 'input[name=?]', 'issue[is_private]', 0
1187 1187 assert_select 'select[name=?]', 'issue[project_id]', 0
1188 1188 assert_select 'select[name=?]', 'issue[tracker_id]', 0
1189 1189 assert_select 'input[name=?]', 'issue[subject]', 0
1190 1190 assert_select 'textarea[name=?]', 'issue[description]', 0
1191 1191 assert_select 'select[name=?]', 'issue[status_id]', 0
1192 1192 assert_select 'select[name=?]', 'issue[priority_id]', 0
1193 1193 assert_select 'select[name=?]', 'issue[assigned_to_id]', 0
1194 1194 assert_select 'select[name=?]', 'issue[category_id]', 0
1195 1195 assert_select 'select[name=?]', 'issue[fixed_version_id]', 0
1196 1196 assert_select 'input[name=?]', 'issue[parent_issue_id]', 0
1197 1197 assert_select 'input[name=?]', 'issue[start_date]', 0
1198 1198 assert_select 'input[name=?]', 'issue[due_date]', 0
1199 1199 assert_select 'select[name=?]', 'issue[done_ratio]', 0
1200 1200 assert_select 'input[name=?]', 'issue[custom_field_values][2]', 0
1201 1201 assert_select 'input[name=?]', 'issue[watcher_user_ids][]', 0
1202 1202 assert_select 'textarea[name=?]', 'issue[notes]'
1203 1203 end
1204 1204 end
1205 1205
1206 1206 def test_show_should_not_display_update_form_without_permissions
1207 1207 Role.find(1).update_attribute :permissions, [:view_issues]
1208 1208
1209 1209 @request.session[:user_id] = 2
1210 1210 get :show, :id => 1
1211 1211 assert_response :success
1212 1212
1213 1213 assert_select 'form#issue-form', 0
1214 1214 end
1215 1215
1216 1216 def test_update_form_should_not_display_inactive_enumerations
1217 1217 assert !IssuePriority.find(15).active?
1218 1218
1219 1219 @request.session[:user_id] = 2
1220 1220 get :show, :id => 1
1221 1221 assert_response :success
1222 1222
1223 1223 assert_select 'form#issue-form' do
1224 1224 assert_select 'select[name=?]', 'issue[priority_id]' do
1225 1225 assert_select 'option[value="4"]'
1226 1226 assert_select 'option[value="15"]', 0
1227 1227 end
1228 1228 end
1229 1229 end
1230 1230
1231 1231 def test_update_form_should_allow_attachment_upload
1232 1232 @request.session[:user_id] = 2
1233 1233 get :show, :id => 1
1234 1234
1235 1235 assert_select 'form#issue-form[method=post][enctype="multipart/form-data"]' do
1236 1236 assert_select 'input[type=file][name=?]', 'attachments[dummy][file]'
1237 1237 end
1238 1238 end
1239 1239
1240 1240 def test_show_should_deny_anonymous_access_without_permission
1241 1241 Role.anonymous.remove_permission!(:view_issues)
1242 1242 get :show, :id => 1
1243 1243 assert_response :redirect
1244 1244 end
1245 1245
1246 1246 def test_show_should_deny_anonymous_access_to_private_issue
1247 1247 Issue.where(:id => 1).update_all(["is_private = ?", true])
1248 1248 get :show, :id => 1
1249 1249 assert_response :redirect
1250 1250 end
1251 1251
1252 1252 def test_show_should_deny_non_member_access_without_permission
1253 1253 Role.non_member.remove_permission!(:view_issues)
1254 1254 @request.session[:user_id] = 9
1255 1255 get :show, :id => 1
1256 1256 assert_response 403
1257 1257 end
1258 1258
1259 1259 def test_show_should_deny_non_member_access_to_private_issue
1260 1260 Issue.where(:id => 1).update_all(["is_private = ?", true])
1261 1261 @request.session[:user_id] = 9
1262 1262 get :show, :id => 1
1263 1263 assert_response 403
1264 1264 end
1265 1265
1266 1266 def test_show_should_deny_member_access_without_permission
1267 1267 Role.find(1).remove_permission!(:view_issues)
1268 1268 @request.session[:user_id] = 2
1269 1269 get :show, :id => 1
1270 1270 assert_response 403
1271 1271 end
1272 1272
1273 1273 def test_show_should_deny_member_access_to_private_issue_without_permission
1274 1274 Issue.where(:id => 1).update_all(["is_private = ?", true])
1275 1275 @request.session[:user_id] = 3
1276 1276 get :show, :id => 1
1277 1277 assert_response 403
1278 1278 end
1279 1279
1280 1280 def test_show_should_allow_author_access_to_private_issue
1281 1281 Issue.where(:id => 1).update_all(["is_private = ?, author_id = 3", true])
1282 1282 @request.session[:user_id] = 3
1283 1283 get :show, :id => 1
1284 1284 assert_response :success
1285 1285 end
1286 1286
1287 1287 def test_show_should_allow_assignee_access_to_private_issue
1288 1288 Issue.where(:id => 1).update_all(["is_private = ?, assigned_to_id = 3", true])
1289 1289 @request.session[:user_id] = 3
1290 1290 get :show, :id => 1
1291 1291 assert_response :success
1292 1292 end
1293 1293
1294 1294 def test_show_should_allow_member_access_to_private_issue_with_permission
1295 1295 Issue.where(:id => 1).update_all(["is_private = ?", true])
1296 1296 User.find(3).roles_for_project(Project.find(1)).first.update_attribute :issues_visibility, 'all'
1297 1297 @request.session[:user_id] = 3
1298 1298 get :show, :id => 1
1299 1299 assert_response :success
1300 1300 end
1301 1301
1302 1302 def test_show_should_not_disclose_relations_to_invisible_issues
1303 1303 Setting.cross_project_issue_relations = '1'
1304 1304 IssueRelation.create!(:issue_from => Issue.find(1), :issue_to => Issue.find(2), :relation_type => 'relates')
1305 1305 # Relation to a private project issue
1306 1306 IssueRelation.create!(:issue_from => Issue.find(1), :issue_to => Issue.find(4), :relation_type => 'relates')
1307 1307
1308 1308 get :show, :id => 1
1309 1309 assert_response :success
1310 1310
1311 1311 assert_select 'div#relations' do
1312 1312 assert_select 'a', :text => /#2$/
1313 1313 assert_select 'a', :text => /#4$/, :count => 0
1314 1314 end
1315 1315 end
1316 1316
1317 1317 def test_show_should_list_subtasks
1318 1318 Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :parent_issue_id => 1, :subject => 'Child Issue')
1319 1319
1320 1320 get :show, :id => 1
1321 1321 assert_response :success
1322 1322
1323 1323 assert_select 'div#issue_tree' do
1324 1324 assert_select 'td.subject', :text => /Child Issue/
1325 1325 end
1326 1326 end
1327 1327
1328 1328 def test_show_should_list_parents
1329 1329 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :parent_issue_id => 1, :subject => 'Child Issue')
1330 1330
1331 1331 get :show, :id => issue.id
1332 1332 assert_response :success
1333 1333
1334 1334 assert_select 'div.subject' do
1335 1335 assert_select 'h3', 'Child Issue'
1336 1336 assert_select 'a[href="/issues/1"]'
1337 1337 end
1338 1338 end
1339 1339
1340 1340 def test_show_should_not_display_prev_next_links_without_query_in_session
1341 1341 get :show, :id => 1
1342 1342 assert_response :success
1343 1343
1344 1344 assert_select 'div.next-prev-links', 0
1345 1345 end
1346 1346
1347 1347 def test_show_should_display_prev_next_links_with_query_in_session
1348 1348 @request.session[:issue_query] = {:filters => {'status_id' => {:values => [''], :operator => 'o'}}, :project_id => nil}
1349 1349 @request.session['issues_index_sort'] = 'id'
1350 1350
1351 1351 with_settings :display_subprojects_issues => '0' do
1352 1352 get :show, :id => 3
1353 1353 end
1354 1354 assert_response :success
1355 1355
1356 1356 count = Issue.open.visible.count
1357 1357
1358 1358 # Previous and next issues for all projects
1359 1359 assert_select 'div.next-prev-links' do
1360 1360 assert_select 'a[href="/issues/2"]', :text => /Previous/
1361 1361 assert_select 'a[href="/issues/5"]', :text => /Next/
1362 1362 assert_select 'span.position', :text => "3 of #{count}"
1363 1363 end
1364 1364 end
1365 1365
1366 1366 def test_show_should_display_prev_next_links_with_saved_query_in_session
1367 1367 query = IssueQuery.create!(:name => 'test', :visibility => IssueQuery::VISIBILITY_PUBLIC, :user_id => 1,
1368 1368 :filters => {'status_id' => {:values => ['5'], :operator => '='}},
1369 1369 :sort_criteria => [['id', 'asc']])
1370 1370 @request.session[:issue_query] = {:id => query.id, :project_id => nil}
1371 1371
1372 1372 get :show, :id => 11
1373 1373 assert_response :success
1374 1374
1375 1375 # Previous and next issues for all projects
1376 1376 assert_select 'div.next-prev-links' do
1377 1377 assert_select 'a[href="/issues/8"]', :text => /Previous/
1378 1378 assert_select 'a[href="/issues/12"]', :text => /Next/
1379 1379 end
1380 1380 end
1381 1381
1382 1382 def test_show_should_display_prev_next_links_with_query_and_sort_on_association
1383 1383 @request.session[:issue_query] = {:filters => {'status_id' => {:values => [''], :operator => 'o'}}, :project_id => nil}
1384 1384
1385 1385 %w(project tracker status priority author assigned_to category fixed_version).each do |assoc_sort|
1386 1386 @request.session['issues_index_sort'] = assoc_sort
1387 1387
1388 1388 get :show, :id => 3
1389 1389 assert_response :success, "Wrong response status for #{assoc_sort} sort"
1390 1390
1391 1391 assert_select 'div.next-prev-links' do
1392 1392 assert_select 'a', :text => /(Previous|Next)/
1393 1393 end
1394 1394 end
1395 1395 end
1396 1396
1397 1397 def test_show_should_display_prev_next_links_with_project_query_in_session
1398 1398 @request.session[:issue_query] = {:filters => {'status_id' => {:values => [''], :operator => 'o'}}, :project_id => 1}
1399 1399 @request.session['issues_index_sort'] = 'id'
1400 1400
1401 1401 with_settings :display_subprojects_issues => '0' do
1402 1402 get :show, :id => 3
1403 1403 end
1404 1404 assert_response :success
1405 1405
1406 1406 # Previous and next issues inside project
1407 1407 assert_select 'div.next-prev-links' do
1408 1408 assert_select 'a[href="/issues/2"]', :text => /Previous/
1409 1409 assert_select 'a[href="/issues/7"]', :text => /Next/
1410 1410 end
1411 1411 end
1412 1412
1413 1413 def test_show_should_not_display_prev_link_for_first_issue
1414 1414 @request.session[:issue_query] = {:filters => {'status_id' => {:values => [''], :operator => 'o'}}, :project_id => 1}
1415 1415 @request.session['issues_index_sort'] = 'id'
1416 1416
1417 1417 with_settings :display_subprojects_issues => '0' do
1418 1418 get :show, :id => 1
1419 1419 end
1420 1420 assert_response :success
1421 1421
1422 1422 assert_select 'div.next-prev-links' do
1423 1423 assert_select 'a', :text => /Previous/, :count => 0
1424 1424 assert_select 'a[href="/issues/2"]', :text => /Next/
1425 1425 end
1426 1426 end
1427 1427
1428 1428 def test_show_should_not_display_prev_next_links_for_issue_not_in_query_results
1429 1429 @request.session[:issue_query] = {:filters => {'status_id' => {:values => [''], :operator => 'c'}}, :project_id => 1}
1430 1430 @request.session['issues_index_sort'] = 'id'
1431 1431
1432 1432 get :show, :id => 1
1433 1433 assert_response :success
1434 1434
1435 1435 assert_select 'a', :text => /Previous/, :count => 0
1436 1436 assert_select 'a', :text => /Next/, :count => 0
1437 1437 end
1438 1438
1439 1439 def test_show_show_should_display_prev_next_links_with_query_sort_by_user_custom_field
1440 1440 cf = IssueCustomField.create!(:name => 'User', :is_for_all => true, :tracker_ids => [1,2,3], :field_format => 'user')
1441 1441 CustomValue.create!(:custom_field => cf, :customized => Issue.find(1), :value => '2')
1442 1442 CustomValue.create!(:custom_field => cf, :customized => Issue.find(2), :value => '3')
1443 1443 CustomValue.create!(:custom_field => cf, :customized => Issue.find(3), :value => '3')
1444 1444 CustomValue.create!(:custom_field => cf, :customized => Issue.find(5), :value => '')
1445 1445
1446 1446 query = IssueQuery.create!(:name => 'test', :visibility => IssueQuery::VISIBILITY_PUBLIC, :user_id => 1, :filters => {},
1447 1447 :sort_criteria => [["cf_#{cf.id}", 'asc'], ['id', 'asc']])
1448 1448 @request.session[:issue_query] = {:id => query.id, :project_id => nil}
1449 1449
1450 1450 get :show, :id => 3
1451 1451 assert_response :success
1452 1452
1453 1453 assert_select 'div.next-prev-links' do
1454 1454 assert_select 'a[href="/issues/2"]', :text => /Previous/
1455 1455 assert_select 'a[href="/issues/1"]', :text => /Next/
1456 1456 end
1457 1457 end
1458 1458
1459 1459 def test_show_should_display_prev_next_links_when_request_has_previous_and_next_issue_ids_params
1460 1460 get :show, :id => 1, :prev_issue_id => 1, :next_issue_id => 3, :issue_position => 2, :issue_count => 4
1461 1461 assert_response :success
1462 1462
1463 1463 assert_select 'div.next-prev-links' do
1464 1464 assert_select 'a[href="/issues/1"]', :text => /Previous/
1465 1465 assert_select 'a[href="/issues/3"]', :text => /Next/
1466 1466 assert_select 'span.position', :text => "2 of 4"
1467 1467 end
1468 1468 end
1469 1469
1470 1470 def test_show_should_display_category_field_if_categories_are_defined
1471 1471 Issue.update_all :category_id => nil
1472 1472
1473 1473 get :show, :id => 1
1474 1474 assert_response :success
1475 1475 assert_select '.attributes .category'
1476 1476 end
1477 1477
1478 1478 def test_show_should_not_display_category_field_if_no_categories_are_defined
1479 1479 Project.find(1).issue_categories.delete_all
1480 1480
1481 1481 get :show, :id => 1
1482 1482 assert_response :success
1483 1483 assert_select 'table.attributes .category', 0
1484 1484 end
1485 1485
1486 1486 def test_show_should_display_link_to_the_assignee
1487 1487 get :show, :id => 2
1488 1488 assert_response :success
1489 1489 assert_select '.assigned-to' do
1490 1490 assert_select 'a[href="/users/3"]'
1491 1491 end
1492 1492 end
1493 1493
1494 1494 def test_show_should_display_visible_changesets_from_other_projects
1495 1495 project = Project.find(2)
1496 1496 issue = project.issues.first
1497 1497 issue.changeset_ids = [102]
1498 1498 issue.save!
1499 1499 # changesets from other projects should be displayed even if repository
1500 1500 # is disabled on issue's project
1501 1501 project.disable_module! :repository
1502 1502
1503 1503 @request.session[:user_id] = 2
1504 1504 get :show, :id => issue.id
1505 1505
1506 1506 assert_select 'a[href=?]', '/projects/ecookbook/repository/revisions/3'
1507 1507 end
1508 1508
1509 1509 def test_show_should_display_watchers
1510 1510 @request.session[:user_id] = 2
1511 1511 Issue.find(1).add_watcher User.find(2)
1512 1512
1513 1513 get :show, :id => 1
1514 1514 assert_select 'div#watchers ul' do
1515 1515 assert_select 'li' do
1516 1516 assert_select 'a[href="/users/2"]'
1517 1517 assert_select 'a[class*=delete]'
1518 1518 end
1519 1519 end
1520 1520 end
1521 1521
1522 1522 def test_show_should_display_watchers_with_gravatars
1523 1523 @request.session[:user_id] = 2
1524 1524 Issue.find(1).add_watcher User.find(2)
1525 1525
1526 1526 with_settings :gravatar_enabled => '1' do
1527 1527 get :show, :id => 1
1528 1528 end
1529 1529
1530 1530 assert_select 'div#watchers ul' do
1531 1531 assert_select 'li' do
1532 1532 assert_select 'img.gravatar'
1533 1533 assert_select 'a[href="/users/2"]'
1534 1534 assert_select 'a[class*=delete]'
1535 1535 end
1536 1536 end
1537 1537 end
1538 1538
1539 1539 def test_show_with_thumbnails_enabled_should_display_thumbnails
1540 1540 @request.session[:user_id] = 2
1541 1541
1542 1542 with_settings :thumbnails_enabled => '1' do
1543 1543 get :show, :id => 14
1544 1544 assert_response :success
1545 1545 end
1546 1546
1547 1547 assert_select 'div.thumbnails' do
1548 1548 assert_select 'a[href="/attachments/16/testfile.png"]' do
1549 1549 assert_select 'img[src="/attachments/thumbnail/16"]'
1550 1550 end
1551 1551 end
1552 1552 end
1553 1553
1554 1554 def test_show_with_thumbnails_disabled_should_not_display_thumbnails
1555 1555 @request.session[:user_id] = 2
1556 1556
1557 1557 with_settings :thumbnails_enabled => '0' do
1558 1558 get :show, :id => 14
1559 1559 assert_response :success
1560 1560 end
1561 1561
1562 1562 assert_select 'div.thumbnails', 0
1563 1563 end
1564 1564
1565 1565 def test_show_with_multi_custom_field
1566 1566 field = CustomField.find(1)
1567 1567 field.update_attribute :multiple, true
1568 1568 issue = Issue.find(1)
1569 1569 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
1570 1570 issue.save!
1571 1571
1572 1572 get :show, :id => 1
1573 1573 assert_response :success
1574 1574
1575 1575 assert_select ".cf_1 .value", :text => 'MySQL, Oracle'
1576 1576 end
1577 1577
1578 1578 def test_show_with_multi_user_custom_field
1579 1579 field = IssueCustomField.create!(:name => 'Multi user', :field_format => 'user', :multiple => true,
1580 1580 :tracker_ids => [1], :is_for_all => true)
1581 1581 issue = Issue.find(1)
1582 1582 issue.custom_field_values = {field.id => ['2', '3']}
1583 1583 issue.save!
1584 1584
1585 1585 get :show, :id => 1
1586 1586 assert_response :success
1587 1587
1588 1588 assert_select ".cf_#{field.id} .value", :text => 'Dave Lopper, John Smith' do
1589 1589 assert_select 'a', :text => 'Dave Lopper'
1590 1590 assert_select 'a', :text => 'John Smith'
1591 1591 end
1592 1592 end
1593 1593
1594 1594 def test_show_should_display_private_notes_with_permission_only
1595 1595 journal = Journal.create!(:journalized => Issue.find(2), :notes => 'Privates notes', :private_notes => true, :user_id => 1)
1596 1596 @request.session[:user_id] = 2
1597 1597
1598 1598 get :show, :id => 2
1599 1599 assert_response :success
1600 1600 assert_select "#change-#{journal.id}", 1
1601 1601
1602 1602 Role.find(1).remove_permission! :view_private_notes
1603 1603 get :show, :id => 2
1604 1604 assert_response :success
1605 1605 assert_select "#change-#{journal.id}", 0
1606 1606 end
1607 1607
1608 1608 def test_show_atom
1609 1609 get :show, :id => 2, :format => 'atom'
1610 1610 assert_response :success
1611 1611 assert_equal 'application/atom+xml', response.content_type
1612 1612 # Inline image
1613 1613 assert_select 'content', :text => Regexp.new(Regexp.quote('http://test.host/attachments/download/10'))
1614 1614 end
1615 1615
1616 1616 def test_show_export_to_pdf
1617 1617 issue = Issue.find(3)
1618 1618 assert issue.relations.select{|r| r.other_issue(issue).visible?}.present?
1619 1619 get :show, :id => 3, :format => 'pdf'
1620 1620 assert_response :success
1621 1621 assert_equal 'application/pdf', @response.content_type
1622 1622 assert @response.body.starts_with?('%PDF')
1623 1623 end
1624 1624
1625 1625 def test_export_to_pdf_with_utf8_u_fffd
1626 1626 # U+FFFD
1627 1627 s = "\xef\xbf\xbd"
1628 1628 s.force_encoding('UTF-8') if s.respond_to?(:force_encoding)
1629 1629 issue = Issue.generate!(:subject => s)
1630 1630 ["en", "zh", "zh-TW", "ja", "ko"].each do |lang|
1631 1631 with_settings :default_language => lang do
1632 1632 get :show, :id => issue.id, :format => 'pdf'
1633 1633 assert_response :success
1634 1634 assert_equal 'application/pdf', @response.content_type
1635 1635 assert @response.body.starts_with?('%PDF')
1636 1636 end
1637 1637 end
1638 1638 end
1639 1639
1640 1640 def test_show_export_to_pdf_with_ancestors
1641 1641 issue = Issue.generate!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'child', :parent_issue_id => 1)
1642 1642
1643 1643 get :show, :id => issue.id, :format => 'pdf'
1644 1644 assert_response :success
1645 1645 assert_equal 'application/pdf', @response.content_type
1646 1646 assert @response.body.starts_with?('%PDF')
1647 1647 end
1648 1648
1649 1649 def test_show_export_to_pdf_with_descendants
1650 1650 c1 = Issue.generate!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'child', :parent_issue_id => 1)
1651 1651 c2 = Issue.generate!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'child', :parent_issue_id => 1)
1652 1652 c3 = Issue.generate!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'child', :parent_issue_id => c1.id)
1653 1653
1654 1654 get :show, :id => 1, :format => 'pdf'
1655 1655 assert_response :success
1656 1656 assert_equal 'application/pdf', @response.content_type
1657 1657 assert @response.body.starts_with?('%PDF')
1658 1658 end
1659 1659
1660 1660 def test_show_export_to_pdf_with_journals
1661 1661 get :show, :id => 1, :format => 'pdf'
1662 1662 assert_response :success
1663 1663 assert_equal 'application/pdf', @response.content_type
1664 1664 assert @response.body.starts_with?('%PDF')
1665 1665 end
1666 1666
1667 1667 def test_show_export_to_pdf_with_changesets
1668 1668 [[100], [100, 101], [100, 101, 102]].each do |cs|
1669 1669 issue1 = Issue.find(3)
1670 1670 issue1.changesets = Changeset.find(cs)
1671 1671 issue1.save!
1672 1672 issue = Issue.find(3)
1673 1673 assert_equal issue.changesets.count, cs.size
1674 1674 get :show, :id => 3, :format => 'pdf'
1675 1675 assert_response :success
1676 1676 assert_equal 'application/pdf', @response.content_type
1677 1677 assert @response.body.starts_with?('%PDF')
1678 1678 end
1679 1679 end
1680 1680
1681 1681 def test_show_invalid_should_respond_with_404
1682 1682 get :show, :id => 999
1683 1683 assert_response 404
1684 1684 end
1685 1685
1686 1686 def test_show_on_active_project_should_display_edit_links
1687 1687 @request.session[:user_id] = 1
1688 1688
1689 1689 get :show, :id => 1
1690 1690 assert_response :success
1691 1691 assert_select 'a', :text => 'Edit'
1692 1692 assert_select 'a', :text => 'Delete'
1693 1693 end
1694 1694
1695 1695 def test_show_on_closed_project_should_not_display_edit_links
1696 1696 Issue.find(1).project.close
1697 1697 @request.session[:user_id] = 1
1698 1698
1699 1699 get :show, :id => 1
1700 1700 assert_response :success
1701 1701 assert_select 'a', :text => 'Edit', :count => 0
1702 1702 assert_select 'a', :text => 'Delete', :count => 0
1703 1703 end
1704 1704
1705 1705 def test_get_new
1706 1706 @request.session[:user_id] = 2
1707 1707 get :new, :project_id => 1, :tracker_id => 1
1708 1708 assert_response :success
1709 1709
1710 1710 assert_select 'form#issue-form[action=?]', '/projects/ecookbook/issues'
1711 1711 assert_select 'form#issue-form' do
1712 1712 assert_select 'input[name=?]', 'issue[is_private]'
1713 1713 assert_select 'select[name=?]', 'issue[project_id]', 0
1714 1714 assert_select 'select[name=?]', 'issue[tracker_id]'
1715 1715 assert_select 'input[name=?]', 'issue[subject]'
1716 1716 assert_select 'textarea[name=?]', 'issue[description]'
1717 1717 assert_select 'select[name=?]', 'issue[status_id]'
1718 1718 assert_select 'select[name=?]', 'issue[priority_id]'
1719 1719 assert_select 'select[name=?]', 'issue[assigned_to_id]'
1720 1720 assert_select 'select[name=?]', 'issue[category_id]'
1721 1721 assert_select 'select[name=?]', 'issue[fixed_version_id]'
1722 1722 assert_select 'input[name=?]', 'issue[parent_issue_id]'
1723 1723 assert_select 'input[name=?]', 'issue[start_date]'
1724 1724 assert_select 'input[name=?]', 'issue[due_date]'
1725 1725 assert_select 'select[name=?]', 'issue[done_ratio]'
1726 1726 assert_select 'input[name=?][value=?]', 'issue[custom_field_values][2]', 'Default string'
1727 1727 assert_select 'input[name=?]', 'issue[watcher_user_ids][]'
1728 1728 end
1729 1729
1730 1730 # Be sure we don't display inactive IssuePriorities
1731 1731 assert ! IssuePriority.find(15).active?
1732 1732 assert_select 'select[name=?]', 'issue[priority_id]' do
1733 1733 assert_select 'option[value="15"]', 0
1734 1734 end
1735 1735 end
1736 1736
1737 1737 def test_get_new_with_minimal_permissions
1738 1738 Role.find(1).update_attribute :permissions, [:add_issues]
1739 1739 WorkflowTransition.where(:role_id => 1).delete_all
1740 1740
1741 1741 @request.session[:user_id] = 2
1742 1742 get :new, :project_id => 1, :tracker_id => 1
1743 1743 assert_response :success
1744 1744
1745 1745 assert_select 'form#issue-form' do
1746 1746 assert_select 'input[name=?]', 'issue[is_private]', 0
1747 1747 assert_select 'select[name=?]', 'issue[project_id]', 0
1748 1748 assert_select 'select[name=?]', 'issue[tracker_id]'
1749 1749 assert_select 'input[name=?]', 'issue[subject]'
1750 1750 assert_select 'textarea[name=?]', 'issue[description]'
1751 1751 assert_select 'select[name=?]', 'issue[status_id]'
1752 1752 assert_select 'select[name=?]', 'issue[priority_id]'
1753 1753 assert_select 'select[name=?]', 'issue[assigned_to_id]'
1754 1754 assert_select 'select[name=?]', 'issue[category_id]'
1755 1755 assert_select 'select[name=?]', 'issue[fixed_version_id]'
1756 1756 assert_select 'input[name=?]', 'issue[parent_issue_id]', 0
1757 1757 assert_select 'input[name=?]', 'issue[start_date]'
1758 1758 assert_select 'input[name=?]', 'issue[due_date]'
1759 1759 assert_select 'select[name=?]', 'issue[done_ratio]'
1760 1760 assert_select 'input[name=?][value=?]', 'issue[custom_field_values][2]', 'Default string'
1761 1761 assert_select 'input[name=?]', 'issue[watcher_user_ids][]', 0
1762 1762 end
1763 1763 end
1764 1764
1765 1765 def test_new_without_project_id
1766 1766 @request.session[:user_id] = 2
1767 1767 get :new
1768 1768 assert_response :success
1769 1769
1770 1770 assert_select 'form#issue-form[action=?]', '/issues'
1771 1771 assert_select 'form#issue-form' do
1772 1772 assert_select 'select[name=?]', 'issue[project_id]'
1773 1773 end
1774 1774 end
1775 1775
1776 1776 def test_new_should_select_default_status
1777 1777 @request.session[:user_id] = 2
1778 1778
1779 1779 get :new, :project_id => 1
1780 1780 assert_response :success
1781 1781 assert_select 'select[name=?]', 'issue[status_id]' do
1782 1782 assert_select 'option[value="1"][selected=selected]'
1783 1783 end
1784 1784 assert_select 'input[name=was_default_status][value="1"]'
1785 1785 end
1786 1786
1787 1787 def test_new_should_propose_allowed_statuses
1788 1788 WorkflowTransition.delete_all
1789 1789 WorkflowTransition.create!(:tracker_id => 1, :role_id => 1, :old_status_id => 0, :new_status_id => 1)
1790 1790 WorkflowTransition.create!(:tracker_id => 1, :role_id => 1, :old_status_id => 0, :new_status_id => 3)
1791 1791 @request.session[:user_id] = 2
1792 1792
1793 1793 get :new, :project_id => 1
1794 1794 assert_response :success
1795 1795 assert_select 'select[name=?]', 'issue[status_id]' do
1796 1796 assert_select 'option[value="1"]'
1797 1797 assert_select 'option[value="3"]'
1798 1798 assert_select 'option', 2
1799 1799 assert_select 'option[value="1"][selected=selected]'
1800 1800 end
1801 1801 end
1802 1802
1803 1803 def test_new_should_propose_allowed_statuses_without_default_status_allowed
1804 1804 WorkflowTransition.delete_all
1805 1805 WorkflowTransition.create!(:tracker_id => 1, :role_id => 1, :old_status_id => 0, :new_status_id => 2)
1806 1806 assert_equal 1, Tracker.find(1).default_status_id
1807 1807 @request.session[:user_id] = 2
1808 1808
1809 1809 get :new, :project_id => 1
1810 1810 assert_response :success
1811 1811 assert_select 'select[name=?]', 'issue[status_id]' do
1812 1812 assert_select 'option[value="2"]'
1813 1813 assert_select 'option', 1
1814 1814 assert_select 'option[value="2"][selected=selected]'
1815 1815 end
1816 1816 end
1817 1817
1818 1818 def test_new_should_propose_allowed_trackers
1819 1819 role = Role.find(1)
1820 1820 role.set_permission_trackers 'add_issues', [1, 3]
1821 1821 role.save!
1822 1822 @request.session[:user_id] = 2
1823 1823
1824 1824 get :new, :project_id => 1
1825 1825 assert_response :success
1826 1826 assert_select 'select[name=?]', 'issue[tracker_id]' do
1827 1827 assert_select 'option', 2
1828 1828 assert_select 'option[value="1"]'
1829 1829 assert_select 'option[value="3"]'
1830 1830 end
1831 1831 end
1832 1832
1833 1833 def test_new_without_allowed_trackers_should_respond_with_403
1834 1834 role = Role.find(1)
1835 1835 role.set_permission_trackers 'add_issues', []
1836 1836 role.save!
1837 1837 @request.session[:user_id] = 2
1838 1838
1839 1839 get :new, :project_id => 1
1840 1840 assert_response 403
1841 1841 end
1842 1842
1843 1843 def test_new_without_projects_should_respond_with_403
1844 1844 Project.delete_all
1845 1845 @request.session[:user_id] = 2
1846 1846
1847 1847 get :new
1848 1848 assert_response 403
1849 1849 assert_select_error /no projects/
1850 1850 end
1851 1851
1852 1852 def test_new_without_enabled_trackers_on_projects_should_respond_with_403
1853 1853 Project.all.each {|p| p.trackers.clear }
1854 1854 @request.session[:user_id] = 2
1855 1855
1856 1856 get :new
1857 1857 assert_response 403
1858 1858 assert_select_error /no projects/
1859 1859 end
1860 1860
1861 1861 def test_new_should_preselect_default_version
1862 1862 version = Version.generate!(:project_id => 1)
1863 1863 Project.find(1).update_attribute :default_version_id, version.id
1864 1864 @request.session[:user_id] = 2
1865 1865
1866 1866 get :new, :project_id => 1
1867 1867 assert_response :success
1868 1868 assert_select 'select[name=?]', 'issue[fixed_version_id]' do
1869 1869 assert_select 'option[value=?][selected=selected]', version.id.to_s
1870 1870 end
1871 1871 end
1872 1872
1873 1873 def test_get_new_with_list_custom_field
1874 1874 @request.session[:user_id] = 2
1875 1875 get :new, :project_id => 1, :tracker_id => 1
1876 1876 assert_response :success
1877 1877
1878 1878 assert_select 'select.list_cf[name=?]', 'issue[custom_field_values][1]' do
1879 1879 assert_select 'option', 4
1880 1880 assert_select 'option[value=MySQL]', :text => 'MySQL'
1881 1881 end
1882 1882 end
1883 1883
1884 1884 def test_get_new_with_multi_custom_field
1885 1885 field = IssueCustomField.find(1)
1886 1886 field.update_attribute :multiple, true
1887 1887
1888 1888 @request.session[:user_id] = 2
1889 1889 get :new, :project_id => 1, :tracker_id => 1
1890 1890 assert_response :success
1891 1891
1892 1892 assert_select 'select[name=?][multiple=multiple]', 'issue[custom_field_values][1][]' do
1893 1893 assert_select 'option', 3
1894 1894 assert_select 'option[value=MySQL]', :text => 'MySQL'
1895 1895 end
1896 1896 assert_select 'input[name=?][type=hidden][value=?]', 'issue[custom_field_values][1][]', ''
1897 1897 end
1898 1898
1899 1899 def test_get_new_with_multi_user_custom_field
1900 1900 field = IssueCustomField.create!(:name => 'Multi user', :field_format => 'user', :multiple => true,
1901 1901 :tracker_ids => [1], :is_for_all => true)
1902 1902
1903 1903 @request.session[:user_id] = 2
1904 1904 get :new, :project_id => 1, :tracker_id => 1
1905 1905 assert_response :success
1906 1906
1907 1907 assert_select 'select[name=?][multiple=multiple]', "issue[custom_field_values][#{field.id}][]" do
1908 1908 assert_select 'option', Project.find(1).users.count
1909 1909 assert_select 'option[value="2"]', :text => 'John Smith'
1910 1910 end
1911 1911 assert_select 'input[name=?][type=hidden][value=?]', "issue[custom_field_values][#{field.id}][]", ''
1912 1912 end
1913 1913
1914 1914 def test_get_new_with_date_custom_field
1915 1915 field = IssueCustomField.create!(:name => 'Date', :field_format => 'date', :tracker_ids => [1], :is_for_all => true)
1916 1916
1917 1917 @request.session[:user_id] = 2
1918 1918 get :new, :project_id => 1, :tracker_id => 1
1919 1919 assert_response :success
1920 1920
1921 1921 assert_select 'input[name=?]', "issue[custom_field_values][#{field.id}]"
1922 1922 end
1923 1923
1924 1924 def test_get_new_with_text_custom_field
1925 1925 field = IssueCustomField.create!(:name => 'Text', :field_format => 'text', :tracker_ids => [1], :is_for_all => true)
1926 1926
1927 1927 @request.session[:user_id] = 2
1928 1928 get :new, :project_id => 1, :tracker_id => 1
1929 1929 assert_response :success
1930 1930
1931 1931 assert_select 'textarea[name=?]', "issue[custom_field_values][#{field.id}]"
1932 1932 end
1933 1933
1934 1934 def test_get_new_without_default_start_date_is_creation_date
1935 1935 with_settings :default_issue_start_date_to_creation_date => 0 do
1936 1936 @request.session[:user_id] = 2
1937 1937 get :new, :project_id => 1, :tracker_id => 1
1938 1938 assert_response :success
1939 1939 assert_select 'input[name=?]', 'issue[start_date]'
1940 1940 assert_select 'input[name=?][value]', 'issue[start_date]', 0
1941 1941 end
1942 1942 end
1943 1943
1944 1944 def test_get_new_with_default_start_date_is_creation_date
1945 1945 with_settings :default_issue_start_date_to_creation_date => 1 do
1946 1946 @request.session[:user_id] = 2
1947 1947 get :new, :project_id => 1, :tracker_id => 1
1948 1948 assert_response :success
1949 1949 assert_select 'input[name=?][value=?]', 'issue[start_date]',
1950 1950 Date.today.to_s
1951 1951 end
1952 1952 end
1953 1953
1954 1954 def test_get_new_form_should_allow_attachment_upload
1955 1955 @request.session[:user_id] = 2
1956 1956 get :new, :project_id => 1, :tracker_id => 1
1957 1957 assert_response :success
1958 1958
1959 1959 assert_select 'form[id=issue-form][method=post][enctype="multipart/form-data"]' do
1960 1960 assert_select 'input[name=?][type=file]', 'attachments[dummy][file]'
1961 1961 end
1962 1962 end
1963 1963
1964 1964 def test_get_new_should_prefill_the_form_from_params
1965 1965 @request.session[:user_id] = 2
1966 1966 get :new, :project_id => 1,
1967 1967 :issue => {:tracker_id => 3, :description => 'Prefilled', :custom_field_values => {'2' => 'Custom field value'}}
1968 1968
1969 1969 assert_select 'select[name=?]', 'issue[tracker_id]' do
1970 1970 assert_select 'option[value="3"][selected=selected]'
1971 1971 end
1972 1972 assert_select 'textarea[name=?]', 'issue[description]', :text => /Prefilled/
1973 1973 assert_select 'input[name=?][value=?]', 'issue[custom_field_values][2]', 'Custom field value'
1974 1974 end
1975 1975
1976 1976 def test_get_new_should_mark_required_fields
1977 1977 cf1 = IssueCustomField.create!(:name => 'Foo', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
1978 1978 cf2 = IssueCustomField.create!(:name => 'Bar', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
1979 1979 WorkflowPermission.delete_all
1980 1980 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => 'due_date', :rule => 'required')
1981 1981 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => cf2.id.to_s, :rule => 'required')
1982 1982 @request.session[:user_id] = 2
1983 1983
1984 1984 get :new, :project_id => 1
1985 1985 assert_response :success
1986 1986
1987 1987 assert_select 'label[for=issue_start_date]' do
1988 1988 assert_select 'span[class=required]', 0
1989 1989 end
1990 1990 assert_select 'label[for=issue_due_date]' do
1991 1991 assert_select 'span[class=required]'
1992 1992 end
1993 1993 assert_select 'label[for=?]', "issue_custom_field_values_#{cf1.id}" do
1994 1994 assert_select 'span[class=required]', 0
1995 1995 end
1996 1996 assert_select 'label[for=?]', "issue_custom_field_values_#{cf2.id}" do
1997 1997 assert_select 'span[class=required]'
1998 1998 end
1999 1999 end
2000 2000
2001 2001 def test_get_new_should_not_display_readonly_fields
2002 2002 cf1 = IssueCustomField.create!(:name => 'Foo', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
2003 2003 cf2 = IssueCustomField.create!(:name => 'Bar', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
2004 2004 WorkflowPermission.delete_all
2005 2005 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => 'due_date', :rule => 'readonly')
2006 2006 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => cf2.id.to_s, :rule => 'readonly')
2007 2007 @request.session[:user_id] = 2
2008 2008
2009 2009 get :new, :project_id => 1
2010 2010 assert_response :success
2011 2011
2012 2012 assert_select 'input[name=?]', 'issue[start_date]'
2013 2013 assert_select 'input[name=?]', 'issue[due_date]', 0
2014 2014 assert_select 'input[name=?]', "issue[custom_field_values][#{cf1.id}]"
2015 2015 assert_select 'input[name=?]', "issue[custom_field_values][#{cf2.id}]", 0
2016 2016 end
2017 2017
2018 2018 def test_new_with_tracker_set_as_readonly_should_accept_status
2019 2019 WorkflowPermission.delete_all
2020 2020 [1, 2].each do |status_id|
2021 2021 WorkflowPermission.create!(:tracker_id => 1, :old_status_id => status_id, :role_id => 1, :field_name => 'tracker_id', :rule => 'readonly')
2022 2022 end
2023 2023 @request.session[:user_id] = 2
2024 2024
2025 2025 get :new, :project_id => 1, :issue => {:status_id => 2}
2026 2026 assert_select 'select[name=?]', 'issue[tracker_id]', 0
2027 2027 assert_select 'select[name=?]', 'issue[status_id]' do
2028 2028 assert_select 'option[value=?][selected=selected]', '2'
2029 2029 end
2030 2030 end
2031 2031
2032 2032 def test_get_new_without_tracker_id
2033 2033 @request.session[:user_id] = 2
2034 2034 get :new, :project_id => 1
2035 2035 assert_response :success
2036 2036
2037 2037 assert_select 'select[name=?]', 'issue[tracker_id]' do
2038 2038 assert_select 'option[value=?][selected=selected]', Project.find(1).trackers.first.id.to_s
2039 2039 end
2040 2040 end
2041 2041
2042 2042 def test_get_new_with_no_default_status_should_display_an_error
2043 2043 @request.session[:user_id] = 2
2044 2044 IssueStatus.delete_all
2045 2045
2046 2046 get :new, :project_id => 1
2047 2047 assert_response 500
2048 2048 assert_select_error /No default issue/
2049 2049 end
2050 2050
2051 2051 def test_get_new_with_no_tracker_should_display_an_error
2052 2052 @request.session[:user_id] = 2
2053 2053 Tracker.delete_all
2054 2054
2055 2055 get :new, :project_id => 1
2056 2056 assert_response 500
2057 2057 assert_select_error /No tracker/
2058 2058 end
2059 2059
2060 2060 def test_new_with_invalid_project_id
2061 2061 @request.session[:user_id] = 1
2062 2062 get :new, :project_id => 'invalid'
2063 2063 assert_response 404
2064 2064 end
2065 2065
2066 2066 def test_new_with_parent_id_should_only_propose_valid_trackers
2067 2067 @request.session[:user_id] = 2
2068 2068 t = Tracker.find(3)
2069 2069 assert !t.disabled_core_fields.include?('parent_issue_id')
2070 2070
2071 2071 get :new, :project_id => 1, issue: { parent_issue_id: 1 }
2072 2072 assert_response :success
2073 2073 assert_select 'option', text: /#{t.name}/, count: 1
2074 2074
2075 2075 t.core_fields = Tracker::CORE_FIELDS - ['parent_issue_id']
2076 2076 t.save!
2077 2077 assert t.disabled_core_fields.include?('parent_issue_id')
2078 2078 get :new, :project_id => 1, issue: { parent_issue_id: 1 }
2079 2079 assert_response :success
2080 2080 assert_select 'option', text: /#{t.name}/, count: 0
2081 2081 end
2082 2082
2083 2083 def test_update_form_for_new_issue
2084 2084 @request.session[:user_id] = 2
2085 2085 xhr :post, :new, :project_id => 1,
2086 2086 :issue => {:tracker_id => 2,
2087 2087 :subject => 'This is the test_new issue',
2088 2088 :description => 'This is the description',
2089 2089 :priority_id => 5}
2090 2090 assert_response :success
2091 2091 assert_equal 'text/javascript', response.content_type
2092 2092 assert_include 'This is the test_new issue', response.body
2093 2093 end
2094 2094
2095 2095 def test_update_form_for_new_issue_should_propose_transitions_based_on_initial_status
2096 2096 @request.session[:user_id] = 2
2097 2097 WorkflowTransition.delete_all
2098 2098 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 0, :new_status_id => 2)
2099 2099 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 0, :new_status_id => 5)
2100 2100 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 5, :new_status_id => 4)
2101 2101
2102 2102 post :new, :project_id => 1,
2103 2103 :issue => {:tracker_id => 1,
2104 2104 :status_id => 5,
2105 2105 :subject => 'This is an issue'}
2106 2106
2107 2107 assert_select 'select[name=?]', 'issue[status_id]' do
2108 2108 assert_select 'option[value=?][selected=selected]', '5'
2109 2109 assert_select 'option[value=?]', '2'
2110 2110 assert_select 'option', :count => 2
2111 2111 end
2112 2112 end
2113 2113
2114 2114 def test_update_form_with_default_status_should_ignore_submitted_status_id_if_equals
2115 2115 @request.session[:user_id] = 2
2116 2116 tracker = Tracker.find(2)
2117 2117 tracker.update! :default_status_id => 2
2118 2118 tracker.generate_transitions! 2 => 1, :clear => true
2119 2119
2120 2120 post :new, :project_id => 1,
2121 2121 :issue => {:tracker_id => 2,
2122 2122 :status_id => 1},
2123 2123 :was_default_status => 1
2124 2124 assert_response :success
2125 2125
2126 2126 assert_select 'select[name=?]', 'issue[status_id]' do
2127 2127 assert_select 'option[value=?][selected=selected]', '2'
2128 2128 end
2129 2129 end
2130 2130
2131 2131 def test_update_form_for_new_issue_should_ignore_version_when_changing_project
2132 2132 version = Version.generate!(:project_id => 1)
2133 2133 Project.find(1).update_attribute :default_version_id, version.id
2134 2134 @request.session[:user_id] = 2
2135 2135
2136 2136 post :new, :issue => {:project_id => 1,
2137 2137 :fixed_version_id => ''},
2138 2138 :form_update_triggered_by => 'issue_project_id'
2139 2139 assert_response :success
2140 2140
2141 2141 assert_select 'select[name=?]', 'issue[project_id]' do
2142 2142 assert_select 'option[value=?][selected=selected]', '1'
2143 2143 end
2144 2144 assert_select 'select[name=?]', 'issue[fixed_version_id]' do
2145 2145 assert_select 'option[value=?][selected=selected]', version.id.to_s
2146 2146 end
2147 2147 end
2148 2148
2149 2149 def test_post_create
2150 2150 @request.session[:user_id] = 2
2151 2151 assert_difference 'Issue.count' do
2152 2152 assert_no_difference 'Journal.count' do
2153 2153 post :create, :project_id => 1,
2154 2154 :issue => {:tracker_id => 3,
2155 2155 :status_id => 2,
2156 2156 :subject => 'This is the test_new issue',
2157 2157 :description => 'This is the description',
2158 2158 :priority_id => 5,
2159 2159 :start_date => '2010-11-07',
2160 2160 :estimated_hours => '',
2161 2161 :custom_field_values => {'2' => 'Value for field 2'}}
2162 2162 end
2163 2163 end
2164 2164 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
2165 2165
2166 2166 issue = Issue.find_by_subject('This is the test_new issue')
2167 2167 assert_not_nil issue
2168 2168 assert_equal 2, issue.author_id
2169 2169 assert_equal 3, issue.tracker_id
2170 2170 assert_equal 2, issue.status_id
2171 2171 assert_equal Date.parse('2010-11-07'), issue.start_date
2172 2172 assert_nil issue.estimated_hours
2173 2173 v = issue.custom_values.where(:custom_field_id => 2).first
2174 2174 assert_not_nil v
2175 2175 assert_equal 'Value for field 2', v.value
2176 2176 end
2177 2177
2178 2178 def test_post_new_with_group_assignment
2179 2179 group = Group.find(11)
2180 2180 project = Project.find(1)
2181 2181 project.members << Member.new(:principal => group, :roles => [Role.givable.first])
2182 2182
2183 2183 with_settings :issue_group_assignment => '1' do
2184 2184 @request.session[:user_id] = 2
2185 2185 assert_difference 'Issue.count' do
2186 2186 post :create, :project_id => project.id,
2187 2187 :issue => {:tracker_id => 3,
2188 2188 :status_id => 1,
2189 2189 :subject => 'This is the test_new_with_group_assignment issue',
2190 2190 :assigned_to_id => group.id}
2191 2191 end
2192 2192 end
2193 2193 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
2194 2194
2195 2195 issue = Issue.find_by_subject('This is the test_new_with_group_assignment issue')
2196 2196 assert_not_nil issue
2197 2197 assert_equal group, issue.assigned_to
2198 2198 end
2199 2199
2200 2200 def test_post_create_without_start_date_and_default_start_date_is_not_creation_date
2201 2201 with_settings :default_issue_start_date_to_creation_date => 0 do
2202 2202 @request.session[:user_id] = 2
2203 2203 assert_difference 'Issue.count' do
2204 2204 post :create, :project_id => 1,
2205 2205 :issue => {:tracker_id => 3,
2206 2206 :status_id => 2,
2207 2207 :subject => 'This is the test_new issue',
2208 2208 :description => 'This is the description',
2209 2209 :priority_id => 5,
2210 2210 :estimated_hours => '',
2211 2211 :custom_field_values => {'2' => 'Value for field 2'}}
2212 2212 end
2213 2213 assert_redirected_to :controller => 'issues', :action => 'show',
2214 2214 :id => Issue.last.id
2215 2215 issue = Issue.find_by_subject('This is the test_new issue')
2216 2216 assert_not_nil issue
2217 2217 assert_nil issue.start_date
2218 2218 end
2219 2219 end
2220 2220
2221 2221 def test_post_create_without_start_date_and_default_start_date_is_creation_date
2222 2222 with_settings :default_issue_start_date_to_creation_date => 1 do
2223 2223 @request.session[:user_id] = 2
2224 2224 assert_difference 'Issue.count' do
2225 2225 post :create, :project_id => 1,
2226 2226 :issue => {:tracker_id => 3,
2227 2227 :status_id => 2,
2228 2228 :subject => 'This is the test_new issue',
2229 2229 :description => 'This is the description',
2230 2230 :priority_id => 5,
2231 2231 :estimated_hours => '',
2232 2232 :custom_field_values => {'2' => 'Value for field 2'}}
2233 2233 end
2234 2234 assert_redirected_to :controller => 'issues', :action => 'show',
2235 2235 :id => Issue.last.id
2236 2236 issue = Issue.find_by_subject('This is the test_new issue')
2237 2237 assert_not_nil issue
2238 2238 assert_equal Date.today, issue.start_date
2239 2239 end
2240 2240 end
2241 2241
2242 2242 def test_post_create_and_continue
2243 2243 @request.session[:user_id] = 2
2244 2244 assert_difference 'Issue.count' do
2245 2245 post :create, :project_id => 1,
2246 2246 :issue => {:tracker_id => 3, :subject => 'This is first issue', :priority_id => 5},
2247 2247 :continue => ''
2248 2248 end
2249 2249
2250 2250 issue = Issue.order('id DESC').first
2251 2251 assert_redirected_to :controller => 'issues', :action => 'new', :project_id => 'ecookbook', :issue => {:tracker_id => 3}
2252 2252 assert_not_nil flash[:notice], "flash was not set"
2253 2253 assert_select_in flash[:notice],
2254 2254 'a[href=?][title=?]', "/issues/#{issue.id}", "This is first issue", :text => "##{issue.id}"
2255 2255 end
2256 2256
2257 2257 def test_post_create_without_custom_fields_param
2258 2258 @request.session[:user_id] = 2
2259 2259 assert_difference 'Issue.count' do
2260 2260 post :create, :project_id => 1,
2261 2261 :issue => {:tracker_id => 1,
2262 2262 :subject => 'This is the test_new issue',
2263 2263 :description => 'This is the description',
2264 2264 :priority_id => 5}
2265 2265 end
2266 2266 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
2267 2267 end
2268 2268
2269 2269 def test_post_create_with_multi_custom_field
2270 2270 field = IssueCustomField.find_by_name('Database')
2271 2271 field.update_attribute(:multiple, true)
2272 2272
2273 2273 @request.session[:user_id] = 2
2274 2274 assert_difference 'Issue.count' do
2275 2275 post :create, :project_id => 1,
2276 2276 :issue => {:tracker_id => 1,
2277 2277 :subject => 'This is the test_new issue',
2278 2278 :description => 'This is the description',
2279 2279 :priority_id => 5,
2280 2280 :custom_field_values => {'1' => ['', 'MySQL', 'Oracle']}}
2281 2281 end
2282 2282 assert_response 302
2283 2283 issue = Issue.order('id DESC').first
2284 2284 assert_equal ['MySQL', 'Oracle'], issue.custom_field_value(1).sort
2285 2285 end
2286 2286
2287 2287 def test_post_create_with_empty_multi_custom_field
2288 2288 field = IssueCustomField.find_by_name('Database')
2289 2289 field.update_attribute(:multiple, true)
2290 2290
2291 2291 @request.session[:user_id] = 2
2292 2292 assert_difference 'Issue.count' do
2293 2293 post :create, :project_id => 1,
2294 2294 :issue => {:tracker_id => 1,
2295 2295 :subject => 'This is the test_new issue',
2296 2296 :description => 'This is the description',
2297 2297 :priority_id => 5,
2298 2298 :custom_field_values => {'1' => ['']}}
2299 2299 end
2300 2300 assert_response 302
2301 2301 issue = Issue.order('id DESC').first
2302 2302 assert_equal [''], issue.custom_field_value(1).sort
2303 2303 end
2304 2304
2305 2305 def test_post_create_with_multi_user_custom_field
2306 2306 field = IssueCustomField.create!(:name => 'Multi user', :field_format => 'user', :multiple => true,
2307 2307 :tracker_ids => [1], :is_for_all => true)
2308 2308
2309 2309 @request.session[:user_id] = 2
2310 2310 assert_difference 'Issue.count' do
2311 2311 post :create, :project_id => 1,
2312 2312 :issue => {:tracker_id => 1,
2313 2313 :subject => 'This is the test_new issue',
2314 2314 :description => 'This is the description',
2315 2315 :priority_id => 5,
2316 2316 :custom_field_values => {field.id.to_s => ['', '2', '3']}}
2317 2317 end
2318 2318 assert_response 302
2319 2319 issue = Issue.order('id DESC').first
2320 2320 assert_equal ['2', '3'], issue.custom_field_value(field).sort
2321 2321 end
2322 2322
2323 2323 def test_post_create_with_required_custom_field_and_without_custom_fields_param
2324 2324 field = IssueCustomField.find_by_name('Database')
2325 2325 field.update_attribute(:is_required, true)
2326 2326
2327 2327 @request.session[:user_id] = 2
2328 2328 assert_no_difference 'Issue.count' do
2329 2329 post :create, :project_id => 1,
2330 2330 :issue => {:tracker_id => 1,
2331 2331 :subject => 'This is the test_new issue',
2332 2332 :description => 'This is the description',
2333 2333 :priority_id => 5}
2334 2334 end
2335 2335 assert_response :success
2336 2336 assert_select_error /Database cannot be blank/
2337 2337 end
2338 2338
2339 2339 def test_create_should_validate_required_fields
2340 2340 cf1 = IssueCustomField.create!(:name => 'Foo', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
2341 2341 cf2 = IssueCustomField.create!(:name => 'Bar', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
2342 2342 WorkflowPermission.delete_all
2343 2343 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2, :role_id => 1, :field_name => 'due_date', :rule => 'required')
2344 2344 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2, :role_id => 1, :field_name => cf2.id.to_s, :rule => 'required')
2345 2345 @request.session[:user_id] = 2
2346 2346
2347 2347 assert_no_difference 'Issue.count' do
2348 2348 post :create, :project_id => 1, :issue => {
2349 2349 :tracker_id => 2,
2350 2350 :status_id => 1,
2351 2351 :subject => 'Test',
2352 2352 :start_date => '',
2353 2353 :due_date => '',
2354 2354 :custom_field_values => {cf1.id.to_s => '', cf2.id.to_s => ''}
2355 2355 }
2356 2356 assert_response :success
2357 2357 end
2358 2358
2359 2359 assert_select_error /Due date cannot be blank/i
2360 2360 assert_select_error /Bar cannot be blank/i
2361 2361 end
2362 2362
2363 2363 def test_create_should_validate_required_list_fields
2364 2364 cf1 = IssueCustomField.create!(:name => 'Foo', :field_format => 'list', :is_for_all => true, :tracker_ids => [1, 2], :multiple => false, :possible_values => ['a', 'b'])
2365 2365 cf2 = IssueCustomField.create!(:name => 'Bar', :field_format => 'list', :is_for_all => true, :tracker_ids => [1, 2], :multiple => true, :possible_values => ['a', 'b'])
2366 2366 WorkflowPermission.delete_all
2367 2367 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2, :role_id => 1, :field_name => cf1.id.to_s, :rule => 'required')
2368 2368 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2, :role_id => 1, :field_name => cf2.id.to_s, :rule => 'required')
2369 2369 @request.session[:user_id] = 2
2370 2370
2371 2371 assert_no_difference 'Issue.count' do
2372 2372 post :create, :project_id => 1, :issue => {
2373 2373 :tracker_id => 2,
2374 2374 :status_id => 1,
2375 2375 :subject => 'Test',
2376 2376 :start_date => '',
2377 2377 :due_date => '',
2378 2378 :custom_field_values => {cf1.id.to_s => '', cf2.id.to_s => ['']}
2379 2379 }
2380 2380 assert_response :success
2381 2381 end
2382 2382
2383 2383 assert_select_error /Foo cannot be blank/i
2384 2384 assert_select_error /Bar cannot be blank/i
2385 2385 end
2386 2386
2387 2387 def test_create_should_ignore_readonly_fields
2388 2388 cf1 = IssueCustomField.create!(:name => 'Foo', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
2389 2389 cf2 = IssueCustomField.create!(:name => 'Bar', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
2390 2390 WorkflowPermission.delete_all
2391 2391 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2, :role_id => 1, :field_name => 'due_date', :rule => 'readonly')
2392 2392 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2, :role_id => 1, :field_name => cf2.id.to_s, :rule => 'readonly')
2393 2393 @request.session[:user_id] = 2
2394 2394
2395 2395 assert_difference 'Issue.count' do
2396 2396 post :create, :project_id => 1, :issue => {
2397 2397 :tracker_id => 2,
2398 2398 :status_id => 1,
2399 2399 :subject => 'Test',
2400 2400 :start_date => '2012-07-14',
2401 2401 :due_date => '2012-07-16',
2402 2402 :custom_field_values => {cf1.id.to_s => 'value1', cf2.id.to_s => 'value2'}
2403 2403 }
2404 2404 assert_response 302
2405 2405 end
2406 2406
2407 2407 issue = Issue.order('id DESC').first
2408 2408 assert_equal Date.parse('2012-07-14'), issue.start_date
2409 2409 assert_nil issue.due_date
2410 2410 assert_equal 'value1', issue.custom_field_value(cf1)
2411 2411 assert_nil issue.custom_field_value(cf2)
2412 2412 end
2413 2413
2414 2414 def test_create_should_ignore_unallowed_trackers
2415 2415 role = Role.find(1)
2416 2416 role.set_permission_trackers :add_issues, [3]
2417 2417 role.save!
2418 2418 @request.session[:user_id] = 2
2419 2419
2420 2420 issue = new_record(Issue) do
2421 2421 post :create, :project_id => 1, :issue => {
2422 2422 :tracker_id => 1,
2423 2423 :status_id => 1,
2424 2424 :subject => 'Test'
2425 2425 }
2426 2426 assert_response 302
2427 2427 end
2428 2428 assert_equal 3, issue.tracker_id
2429 2429 end
2430 2430
2431 2431 def test_post_create_with_watchers
2432 2432 @request.session[:user_id] = 2
2433 2433 ActionMailer::Base.deliveries.clear
2434 2434
2435 2435 with_settings :notified_events => %w(issue_added) do
2436 2436 assert_difference 'Watcher.count', 2 do
2437 2437 post :create, :project_id => 1,
2438 2438 :issue => {:tracker_id => 1,
2439 2439 :subject => 'This is a new issue with watchers',
2440 2440 :description => 'This is the description',
2441 2441 :priority_id => 5,
2442 2442 :watcher_user_ids => ['2', '3']}
2443 2443 end
2444 2444 end
2445 2445 issue = Issue.find_by_subject('This is a new issue with watchers')
2446 2446 assert_not_nil issue
2447 2447 assert_redirected_to :controller => 'issues', :action => 'show', :id => issue
2448 2448
2449 2449 # Watchers added
2450 2450 assert_equal [2, 3], issue.watcher_user_ids.sort
2451 2451 assert issue.watched_by?(User.find(3))
2452 2452 # Watchers notified
2453 2453 mail = ActionMailer::Base.deliveries.last
2454 2454 assert_not_nil mail
2455 2455 assert [mail.bcc, mail.cc].flatten.include?(User.find(3).mail)
2456 2456 end
2457 2457
2458 2458 def test_post_create_subissue
2459 2459 @request.session[:user_id] = 2
2460 2460
2461 2461 assert_difference 'Issue.count' do
2462 2462 post :create, :project_id => 1,
2463 2463 :issue => {:tracker_id => 1,
2464 2464 :subject => 'This is a child issue',
2465 2465 :parent_issue_id => '2'}
2466 2466 assert_response 302
2467 2467 end
2468 2468 issue = Issue.order('id DESC').first
2469 2469 assert_equal Issue.find(2), issue.parent
2470 2470 end
2471 2471
2472 2472 def test_post_create_subissue_with_sharp_parent_id
2473 2473 @request.session[:user_id] = 2
2474 2474
2475 2475 assert_difference 'Issue.count' do
2476 2476 post :create, :project_id => 1,
2477 2477 :issue => {:tracker_id => 1,
2478 2478 :subject => 'This is a child issue',
2479 2479 :parent_issue_id => '#2'}
2480 2480 assert_response 302
2481 2481 end
2482 2482 issue = Issue.order('id DESC').first
2483 2483 assert_equal Issue.find(2), issue.parent
2484 2484 end
2485 2485
2486 2486 def test_post_create_subissue_with_non_visible_parent_id_should_not_validate
2487 2487 @request.session[:user_id] = 2
2488 2488
2489 2489 assert_no_difference 'Issue.count' do
2490 2490 post :create, :project_id => 1,
2491 2491 :issue => {:tracker_id => 1,
2492 2492 :subject => 'This is a child issue',
2493 2493 :parent_issue_id => '4'}
2494 2494
2495 2495 assert_response :success
2496 2496 assert_select 'input[name=?][value=?]', 'issue[parent_issue_id]', '4'
2497 2497 assert_select_error /Parent task is invalid/i
2498 2498 end
2499 2499 end
2500 2500
2501 2501 def test_post_create_subissue_with_non_numeric_parent_id_should_not_validate
2502 2502 @request.session[:user_id] = 2
2503 2503
2504 2504 assert_no_difference 'Issue.count' do
2505 2505 post :create, :project_id => 1,
2506 2506 :issue => {:tracker_id => 1,
2507 2507 :subject => 'This is a child issue',
2508 2508 :parent_issue_id => '01ABC'}
2509 2509
2510 2510 assert_response :success
2511 2511 assert_select 'input[name=?][value=?]', 'issue[parent_issue_id]', '01ABC'
2512 2512 assert_select_error /Parent task is invalid/i
2513 2513 end
2514 2514 end
2515 2515
2516 2516 def test_post_create_private
2517 2517 @request.session[:user_id] = 2
2518 2518
2519 2519 assert_difference 'Issue.count' do
2520 2520 post :create, :project_id => 1,
2521 2521 :issue => {:tracker_id => 1,
2522 2522 :subject => 'This is a private issue',
2523 2523 :is_private => '1'}
2524 2524 end
2525 2525 issue = Issue.order('id DESC').first
2526 2526 assert issue.is_private?
2527 2527 end
2528 2528
2529 2529 def test_post_create_private_with_set_own_issues_private_permission
2530 2530 role = Role.find(1)
2531 2531 role.remove_permission! :set_issues_private
2532 2532 role.add_permission! :set_own_issues_private
2533 2533
2534 2534 @request.session[:user_id] = 2
2535 2535
2536 2536 assert_difference 'Issue.count' do
2537 2537 post :create, :project_id => 1,
2538 2538 :issue => {:tracker_id => 1,
2539 2539 :subject => 'This is a private issue',
2540 2540 :is_private => '1'}
2541 2541 end
2542 2542 issue = Issue.order('id DESC').first
2543 2543 assert issue.is_private?
2544 2544 end
2545 2545
2546 2546 def test_create_without_project_id
2547 2547 @request.session[:user_id] = 2
2548 2548
2549 2549 assert_difference 'Issue.count' do
2550 2550 post :create,
2551 2551 :issue => {:project_id => 3,
2552 2552 :tracker_id => 2,
2553 2553 :subject => 'Foo'}
2554 2554 assert_response 302
2555 2555 end
2556 2556 issue = Issue.order('id DESC').first
2557 2557 assert_equal 3, issue.project_id
2558 2558 assert_equal 2, issue.tracker_id
2559 2559 end
2560 2560
2561 2561 def test_create_without_project_id_and_continue_should_redirect_without_project_id
2562 2562 @request.session[:user_id] = 2
2563 2563
2564 2564 assert_difference 'Issue.count' do
2565 2565 post :create,
2566 2566 :issue => {:project_id => 3,
2567 2567 :tracker_id => 2,
2568 2568 :subject => 'Foo'},
2569 2569 :continue => '1'
2570 2570 assert_redirected_to '/issues/new?issue%5Bproject_id%5D=3&issue%5Btracker_id%5D=2'
2571 2571 end
2572 2572 end
2573 2573
2574 2574 def test_create_without_project_id_should_be_denied_without_permission
2575 2575 Role.non_member.remove_permission! :add_issues
2576 2576 Role.anonymous.remove_permission! :add_issues
2577 2577 @request.session[:user_id] = 2
2578 2578
2579 2579 assert_no_difference 'Issue.count' do
2580 2580 post :create,
2581 2581 :issue => {:project_id => 3,
2582 2582 :tracker_id => 2,
2583 2583 :subject => 'Foo'}
2584 2584 assert_response 422
2585 2585 end
2586 2586 end
2587 2587
2588 2588 def test_create_without_project_id_with_failure_should_not_set_project
2589 2589 @request.session[:user_id] = 2
2590 2590
2591 2591 post :create,
2592 2592 :issue => {:project_id => 3,
2593 2593 :tracker_id => 2,
2594 2594 :subject => ''}
2595 2595 assert_response :success
2596 2596 # no project menu
2597 2597 assert_select '#main-menu', 0
2598 2598 end
2599 2599
2600 2600 def test_post_create_should_send_a_notification
2601 2601 ActionMailer::Base.deliveries.clear
2602 2602 @request.session[:user_id] = 2
2603 2603 with_settings :notified_events => %w(issue_added) do
2604 2604 assert_difference 'Issue.count' do
2605 2605 post :create, :project_id => 1,
2606 2606 :issue => {:tracker_id => 3,
2607 2607 :subject => 'This is the test_new issue',
2608 2608 :description => 'This is the description',
2609 2609 :priority_id => 5,
2610 2610 :estimated_hours => '',
2611 2611 :custom_field_values => {'2' => 'Value for field 2'}}
2612 2612 end
2613 2613 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
2614 2614
2615 2615 assert_equal 1, ActionMailer::Base.deliveries.size
2616 2616 end
2617 2617 end
2618 2618
2619 2619 def test_post_create_should_preserve_fields_values_on_validation_failure
2620 2620 @request.session[:user_id] = 2
2621 2621 post :create, :project_id => 1,
2622 2622 :issue => {:tracker_id => 1,
2623 2623 # empty subject
2624 2624 :subject => '',
2625 2625 :description => 'This is a description',
2626 2626 :priority_id => 6,
2627 2627 :custom_field_values => {'1' => 'Oracle', '2' => 'Value for field 2'}}
2628 2628 assert_response :success
2629 2629
2630 2630 assert_select 'textarea[name=?]', 'issue[description]', :text => 'This is a description'
2631 2631 assert_select 'select[name=?]', 'issue[priority_id]' do
2632 2632 assert_select 'option[value="6"][selected=selected]', :text => 'High'
2633 2633 end
2634 2634 # Custom fields
2635 2635 assert_select 'select[name=?]', 'issue[custom_field_values][1]' do
2636 2636 assert_select 'option[value=Oracle][selected=selected]', :text => 'Oracle'
2637 2637 end
2638 2638 assert_select 'input[name=?][value=?]', 'issue[custom_field_values][2]', 'Value for field 2'
2639 2639 end
2640 2640
2641 2641 def test_post_create_with_failure_should_preserve_watchers
2642 2642 assert !User.find(8).member_of?(Project.find(1))
2643 2643
2644 2644 @request.session[:user_id] = 2
2645 2645 post :create, :project_id => 1,
2646 2646 :issue => {:tracker_id => 1,
2647 2647 :watcher_user_ids => ['3', '8']}
2648 2648 assert_response :success
2649 2649
2650 2650 assert_select 'input[name=?][value="2"]:not(checked)', 'issue[watcher_user_ids][]'
2651 2651 assert_select 'input[name=?][value="3"][checked=checked]', 'issue[watcher_user_ids][]'
2652 2652 assert_select 'input[name=?][value="8"][checked=checked]', 'issue[watcher_user_ids][]'
2653 2653 end
2654 2654
2655 2655 def test_post_create_should_ignore_non_safe_attributes
2656 2656 @request.session[:user_id] = 2
2657 2657 assert_nothing_raised do
2658 2658 post :create, :project_id => 1, :issue => { :tracker => "A param can not be a Tracker" }
2659 2659 end
2660 2660 end
2661 2661
2662 2662 def test_post_create_with_attachment
2663 2663 set_tmp_attachments_directory
2664 2664 @request.session[:user_id] = 2
2665 2665
2666 2666 assert_difference 'Issue.count' do
2667 2667 assert_difference 'Attachment.count' do
2668 2668 assert_no_difference 'Journal.count' do
2669 2669 post :create, :project_id => 1,
2670 2670 :issue => { :tracker_id => '1', :subject => 'With attachment' },
2671 2671 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
2672 2672 end
2673 2673 end
2674 2674 end
2675 2675
2676 2676 issue = Issue.order('id DESC').first
2677 2677 attachment = Attachment.order('id DESC').first
2678 2678
2679 2679 assert_equal issue, attachment.container
2680 2680 assert_equal 2, attachment.author_id
2681 2681 assert_equal 'testfile.txt', attachment.filename
2682 2682 assert_equal 'text/plain', attachment.content_type
2683 2683 assert_equal 'test file', attachment.description
2684 2684 assert_equal 59, attachment.filesize
2685 2685 assert File.exists?(attachment.diskfile)
2686 2686 assert_equal 59, File.size(attachment.diskfile)
2687 2687 end
2688 2688
2689 2689 def test_post_create_with_attachment_should_notify_with_attachments
2690 2690 ActionMailer::Base.deliveries.clear
2691 2691 set_tmp_attachments_directory
2692 2692 @request.session[:user_id] = 2
2693 2693
2694 2694 with_settings :notified_events => %w(issue_added) do
2695 2695 assert_difference 'Issue.count' do
2696 2696 post :create, :project_id => 1,
2697 2697 :issue => { :tracker_id => '1', :subject => 'With attachment' },
2698 2698 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
2699 2699 end
2700 2700 end
2701 2701
2702 2702 assert_not_nil ActionMailer::Base.deliveries.last
2703 2703 assert_select_email do
2704 2704 assert_select 'a[href^=?]', 'http://localhost:3000/attachments/download', 'testfile.txt'
2705 2705 end
2706 2706 end
2707 2707
2708 2708 def test_post_create_with_failure_should_save_attachments
2709 2709 set_tmp_attachments_directory
2710 2710 @request.session[:user_id] = 2
2711 2711
2712 2712 assert_no_difference 'Issue.count' do
2713 2713 assert_difference 'Attachment.count' do
2714 2714 post :create, :project_id => 1,
2715 2715 :issue => { :tracker_id => '1', :subject => '' },
2716 2716 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
2717 2717 assert_response :success
2718 2718 end
2719 2719 end
2720 2720
2721 2721 attachment = Attachment.order('id DESC').first
2722 2722 assert_equal 'testfile.txt', attachment.filename
2723 2723 assert File.exists?(attachment.diskfile)
2724 2724 assert_nil attachment.container
2725 2725
2726 2726 assert_select 'input[name=?][value=?]', 'attachments[p0][token]', attachment.token
2727 2727 assert_select 'input[name=?][value=?]', 'attachments[p0][filename]', 'testfile.txt'
2728 2728 end
2729 2729
2730 2730 def test_post_create_with_failure_should_keep_saved_attachments
2731 2731 set_tmp_attachments_directory
2732 2732 attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 2)
2733 2733 @request.session[:user_id] = 2
2734 2734
2735 2735 assert_no_difference 'Issue.count' do
2736 2736 assert_no_difference 'Attachment.count' do
2737 2737 post :create, :project_id => 1,
2738 2738 :issue => { :tracker_id => '1', :subject => '' },
2739 2739 :attachments => {'p0' => {'token' => attachment.token}}
2740 2740 assert_response :success
2741 2741 end
2742 2742 end
2743 2743
2744 2744 assert_select 'input[name=?][value=?]', 'attachments[p0][token]', attachment.token
2745 2745 assert_select 'input[name=?][value=?]', 'attachments[p0][filename]', 'testfile.txt'
2746 2746 end
2747 2747
2748 2748 def test_post_create_should_attach_saved_attachments
2749 2749 set_tmp_attachments_directory
2750 2750 attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 2)
2751 2751 @request.session[:user_id] = 2
2752 2752
2753 2753 assert_difference 'Issue.count' do
2754 2754 assert_no_difference 'Attachment.count' do
2755 2755 post :create, :project_id => 1,
2756 2756 :issue => { :tracker_id => '1', :subject => 'Saved attachments' },
2757 2757 :attachments => {'p0' => {'token' => attachment.token}}
2758 2758 assert_response 302
2759 2759 end
2760 2760 end
2761 2761
2762 2762 issue = Issue.order('id DESC').first
2763 2763 assert_equal 1, issue.attachments.count
2764 2764
2765 2765 attachment.reload
2766 2766 assert_equal issue, attachment.container
2767 2767 end
2768 2768
2769 2769 def setup_without_workflow_privilege
2770 2770 WorkflowTransition.where(["role_id = ?", Role.anonymous.id]).delete_all
2771 2771 Role.anonymous.add_permission! :add_issues, :add_issue_notes
2772 2772 end
2773 2773 private :setup_without_workflow_privilege
2774 2774
2775 2775 test "without workflow privilege #new should propose default status only" do
2776 2776 setup_without_workflow_privilege
2777 2777 get :new, :project_id => 1
2778 2778 assert_response :success
2779 2779
2780 2780 assert_select 'select[name=?]', 'issue[status_id]' do
2781 2781 assert_select 'option', 1
2782 2782 assert_select 'option[value=?][selected=selected]', '1'
2783 2783 end
2784 2784 end
2785 2785
2786 2786 test "without workflow privilege #create should accept default status" do
2787 2787 setup_without_workflow_privilege
2788 2788 assert_difference 'Issue.count' do
2789 2789 post :create, :project_id => 1,
2790 2790 :issue => {:tracker_id => 1,
2791 2791 :subject => 'This is an issue',
2792 2792 :status_id => 1}
2793 2793 end
2794 2794 issue = Issue.order('id').last
2795 2795 assert_not_nil issue.default_status
2796 2796 assert_equal issue.default_status, issue.status
2797 2797 end
2798 2798
2799 2799 test "without workflow privilege #create should ignore unauthorized status" do
2800 2800 setup_without_workflow_privilege
2801 2801 assert_difference 'Issue.count' do
2802 2802 post :create, :project_id => 1,
2803 2803 :issue => {:tracker_id => 1,
2804 2804 :subject => 'This is an issue',
2805 2805 :status_id => 3}
2806 2806 end
2807 2807 issue = Issue.order('id').last
2808 2808 assert_not_nil issue.default_status
2809 2809 assert_equal issue.default_status, issue.status
2810 2810 end
2811 2811
2812 2812 test "without workflow privilege #update should ignore status change" do
2813 2813 setup_without_workflow_privilege
2814 2814 assert_difference 'Journal.count' do
2815 2815 put :update, :id => 1, :issue => {:status_id => 3, :notes => 'just trying'}
2816 2816 end
2817 2817 assert_equal 1, Issue.find(1).status_id
2818 2818 end
2819 2819
2820 2820 test "without workflow privilege #update ignore attributes changes" do
2821 2821 setup_without_workflow_privilege
2822 2822 assert_difference 'Journal.count' do
2823 2823 put :update, :id => 1,
2824 2824 :issue => {:subject => 'changed', :assigned_to_id => 2,
2825 2825 :notes => 'just trying'}
2826 2826 end
2827 2827 issue = Issue.find(1)
2828 2828 assert_equal "Cannot print recipes", issue.subject
2829 2829 assert_nil issue.assigned_to
2830 2830 end
2831 2831
2832 2832 def setup_with_workflow_privilege
2833 2833 WorkflowTransition.where(["role_id = ?", Role.anonymous.id]).delete_all
2834 2834 WorkflowTransition.create!(:role => Role.anonymous, :tracker_id => 1,
2835 2835 :old_status_id => 1, :new_status_id => 3)
2836 2836 WorkflowTransition.create!(:role => Role.anonymous, :tracker_id => 1,
2837 2837 :old_status_id => 1, :new_status_id => 4)
2838 2838 Role.anonymous.add_permission! :add_issues, :add_issue_notes
2839 2839 end
2840 2840 private :setup_with_workflow_privilege
2841 2841
2842 2842 def setup_with_workflow_privilege_and_edit_issues_permission
2843 2843 setup_with_workflow_privilege
2844 2844 Role.anonymous.add_permission! :add_issues, :edit_issues
2845 2845 end
2846 2846 private :setup_with_workflow_privilege_and_edit_issues_permission
2847 2847
2848 2848 test "with workflow privilege and :edit_issues permission should accept authorized status" do
2849 2849 setup_with_workflow_privilege_and_edit_issues_permission
2850 2850 assert_difference 'Journal.count' do
2851 2851 put :update, :id => 1, :issue => {:status_id => 3, :notes => 'just trying'}
2852 2852 end
2853 2853 assert_equal 3, Issue.find(1).status_id
2854 2854 end
2855 2855
2856 2856 test "with workflow privilege and :edit_issues permission should ignore unauthorized status" do
2857 2857 setup_with_workflow_privilege_and_edit_issues_permission
2858 2858 assert_difference 'Journal.count' do
2859 2859 put :update, :id => 1, :issue => {:status_id => 2, :notes => 'just trying'}
2860 2860 end
2861 2861 assert_equal 1, Issue.find(1).status_id
2862 2862 end
2863 2863
2864 2864 test "with workflow privilege and :edit_issues permission should accept authorized attributes changes" do
2865 2865 setup_with_workflow_privilege_and_edit_issues_permission
2866 2866 assert_difference 'Journal.count' do
2867 2867 put :update, :id => 1,
2868 2868 :issue => {:subject => 'changed', :assigned_to_id => 2,
2869 2869 :notes => 'just trying'}
2870 2870 end
2871 2871 issue = Issue.find(1)
2872 2872 assert_equal "changed", issue.subject
2873 2873 assert_equal 2, issue.assigned_to_id
2874 2874 end
2875 2875
2876 2876 def test_new_as_copy
2877 2877 orig = Issue.find(1)
2878 2878 @request.session[:user_id] = 2
2879 2879
2880 2880 get :new, :project_id => 1, :copy_from => orig.id
2881 2881 assert_response :success
2882 2882
2883 2883 assert_select 'form[id=issue-form][action="/projects/ecookbook/issues"]' do
2884 2884 assert_select 'select[name=?]', 'issue[project_id]' do
2885 2885 assert_select 'option[value="1"][selected=selected]', :text => 'eCookbook'
2886 2886 assert_select 'option[value="2"]:not([selected])', :text => 'OnlineStore'
2887 2887 end
2888 2888 assert_select 'input[name=?][value=?]', 'issue[subject]', orig.subject
2889 2889 assert_select 'input[name=copy_from][value="1"]'
2890 2890 end
2891 2891 end
2892 2892
2893 2893 def test_new_as_copy_without_add_issues_permission_should_not_propose_current_project_as_target
2894 2894 user = setup_user_with_copy_but_not_add_permission
2895 2895
2896 2896 @request.session[:user_id] = user.id
2897 2897 get :new, :project_id => 1, :copy_from => 1
2898 2898 assert_response :success
2899 2899
2900 2900 assert_select 'select[name=?]', 'issue[project_id]' do
2901 2901 assert_select 'option[value="1"]', 0
2902 2902 assert_select 'option[value="2"]', :text => 'OnlineStore'
2903 2903 end
2904 2904 end
2905 2905
2906 2906 def test_new_as_copy_with_attachments_should_show_copy_attachments_checkbox
2907 2907 @request.session[:user_id] = 2
2908 2908 issue = Issue.find(3)
2909 2909 assert issue.attachments.count > 0
2910 2910 get :new, :project_id => 1, :copy_from => 3
2911 2911
2912 2912 assert_select 'input[name=copy_attachments][type=checkbox][checked=checked][value="1"]'
2913 2913 end
2914 2914
2915 2915 def test_new_as_copy_without_attachments_should_not_show_copy_attachments_checkbox
2916 2916 @request.session[:user_id] = 2
2917 2917 issue = Issue.find(3)
2918 2918 issue.attachments.delete_all
2919 2919 get :new, :project_id => 1, :copy_from => 3
2920 2920
2921 2921 assert_select 'input[name=copy_attachments]', 0
2922 2922 end
2923 2923
2924 2924 def test_new_as_copy_should_preserve_parent_id
2925 2925 @request.session[:user_id] = 2
2926 2926 issue = Issue.generate!(:parent_issue_id => 2)
2927 2927 get :new, :project_id => 1, :copy_from => issue.id
2928 2928
2929 2929 assert_select 'input[name=?][value="2"]', 'issue[parent_issue_id]'
2930 2930 end
2931 2931
2932 2932 def test_new_as_copy_with_subtasks_should_show_copy_subtasks_checkbox
2933 2933 @request.session[:user_id] = 2
2934 2934 issue = Issue.generate_with_descendants!
2935 2935 get :new, :project_id => 1, :copy_from => issue.id
2936 2936
2937 2937 assert_select 'input[type=checkbox][name=copy_subtasks][checked=checked][value="1"]'
2938 2938 end
2939 2939
2940 2940 def test_new_as_copy_with_invalid_issue_should_respond_with_404
2941 2941 @request.session[:user_id] = 2
2942 2942 get :new, :project_id => 1, :copy_from => 99999
2943 2943 assert_response 404
2944 2944 end
2945 2945
2946 2946 def test_create_as_copy_on_different_project
2947 2947 @request.session[:user_id] = 2
2948 2948 assert_difference 'Issue.count' do
2949 2949 post :create, :project_id => 1, :copy_from => 1,
2950 2950 :issue => {:project_id => '2', :tracker_id => '3', :status_id => '1', :subject => 'Copy'}
2951 2951 end
2952 2952 issue = Issue.order('id DESC').first
2953 2953 assert_redirected_to "/issues/#{issue.id}"
2954 2954
2955 2955 assert_equal 2, issue.project_id
2956 2956 assert_equal 3, issue.tracker_id
2957 2957 assert_equal 'Copy', issue.subject
2958 2958 end
2959 2959
2960 2960 def test_create_as_copy_should_allow_status_to_be_set_to_default
2961 2961 copied = Issue.generate! :status_id => 2
2962 2962 assert_equal 2, copied.reload.status_id
2963 2963
2964 2964 @request.session[:user_id] = 2
2965 2965 assert_difference 'Issue.count' do
2966 2966 post :create, :project_id => 1, :copy_from => copied.id,
2967 2967 :issue => {:project_id => '1', :tracker_id => '1', :status_id => '1'},
2968 2968 :was_default_status => '1'
2969 2969 end
2970 2970 issue = Issue.order('id DESC').first
2971 2971 assert_equal 1, issue.status_id
2972 2972 end
2973 2973
2974 2974 def test_create_as_copy_should_copy_attachments
2975 2975 @request.session[:user_id] = 2
2976 2976 issue = Issue.find(3)
2977 2977 count = issue.attachments.count
2978 2978 assert count > 0
2979 2979 assert_difference 'Issue.count' do
2980 2980 assert_difference 'Attachment.count', count do
2981 2981 post :create, :project_id => 1, :copy_from => 3,
2982 2982 :issue => {:project_id => '1', :tracker_id => '3',
2983 2983 :status_id => '1', :subject => 'Copy with attachments'},
2984 2984 :copy_attachments => '1'
2985 2985 end
2986 2986 end
2987 2987 copy = Issue.order('id DESC').first
2988 2988 assert_equal count, copy.attachments.count
2989 2989 assert_equal issue.attachments.map(&:filename).sort, copy.attachments.map(&:filename).sort
2990 2990 end
2991 2991
2992 2992 def test_create_as_copy_without_copy_attachments_option_should_not_copy_attachments
2993 2993 @request.session[:user_id] = 2
2994 2994 issue = Issue.find(3)
2995 2995 count = issue.attachments.count
2996 2996 assert count > 0
2997 2997 assert_difference 'Issue.count' do
2998 2998 assert_no_difference 'Attachment.count' do
2999 2999 post :create, :project_id => 1, :copy_from => 3,
3000 3000 :issue => {:project_id => '1', :tracker_id => '3',
3001 3001 :status_id => '1', :subject => 'Copy with attachments'}
3002 3002 end
3003 3003 end
3004 3004 copy = Issue.order('id DESC').first
3005 3005 assert_equal 0, copy.attachments.count
3006 3006 end
3007 3007
3008 3008 def test_create_as_copy_with_attachments_should_also_add_new_files
3009 3009 @request.session[:user_id] = 2
3010 3010 issue = Issue.find(3)
3011 3011 count = issue.attachments.count
3012 3012 assert count > 0
3013 3013 assert_difference 'Issue.count' do
3014 3014 assert_difference 'Attachment.count', count + 1 do
3015 3015 post :create, :project_id => 1, :copy_from => 3,
3016 3016 :issue => {:project_id => '1', :tracker_id => '3',
3017 3017 :status_id => '1', :subject => 'Copy with attachments'},
3018 3018 :copy_attachments => '1',
3019 3019 :attachments => {'1' =>
3020 3020 {'file' => uploaded_test_file('testfile.txt', 'text/plain'),
3021 3021 'description' => 'test file'}}
3022 3022 end
3023 3023 end
3024 3024 copy = Issue.order('id DESC').first
3025 3025 assert_equal count + 1, copy.attachments.count
3026 3026 end
3027 3027
3028 3028 def test_create_as_copy_should_add_relation_with_copied_issue
3029 3029 @request.session[:user_id] = 2
3030 3030 assert_difference 'Issue.count' do
3031 3031 assert_difference 'IssueRelation.count' do
3032 3032 post :create, :project_id => 1, :copy_from => 1, :link_copy => '1',
3033 3033 :issue => {:project_id => '1', :tracker_id => '3',
3034 3034 :status_id => '1', :subject => 'Copy'}
3035 3035 end
3036 3036 end
3037 3037 copy = Issue.order('id DESC').first
3038 3038 assert_equal 1, copy.relations.size
3039 3039 end
3040 3040
3041 3041 def test_create_as_copy_should_allow_not_to_add_relation_with_copied_issue
3042 3042 @request.session[:user_id] = 2
3043 3043 assert_difference 'Issue.count' do
3044 3044 assert_no_difference 'IssueRelation.count' do
3045 3045 post :create, :project_id => 1, :copy_from => 1,
3046 3046 :issue => {:subject => 'Copy'}
3047 3047 end
3048 3048 end
3049 3049 end
3050 3050
3051 3051 def test_create_as_copy_should_always_add_relation_with_copied_issue_by_setting
3052 3052 with_settings :link_copied_issue => 'yes' do
3053 3053 @request.session[:user_id] = 2
3054 3054 assert_difference 'Issue.count' do
3055 3055 assert_difference 'IssueRelation.count' do
3056 3056 post :create, :project_id => 1, :copy_from => 1,
3057 3057 :issue => {:subject => 'Copy'}
3058 3058 end
3059 3059 end
3060 3060 end
3061 3061 end
3062 3062
3063 3063 def test_create_as_copy_should_never_add_relation_with_copied_issue_by_setting
3064 3064 with_settings :link_copied_issue => 'no' do
3065 3065 @request.session[:user_id] = 2
3066 3066 assert_difference 'Issue.count' do
3067 3067 assert_no_difference 'IssueRelation.count' do
3068 3068 post :create, :project_id => 1, :copy_from => 1, :link_copy => '1',
3069 3069 :issue => {:subject => 'Copy'}
3070 3070 end
3071 3071 end
3072 3072 end
3073 3073 end
3074 3074
3075 3075 def test_create_as_copy_should_copy_subtasks
3076 3076 @request.session[:user_id] = 2
3077 3077 issue = Issue.generate_with_descendants!
3078 3078 count = issue.descendants.count
3079 3079 assert_difference 'Issue.count', count + 1 do
3080 3080 post :create, :project_id => 1, :copy_from => issue.id,
3081 3081 :issue => {:project_id => '1', :tracker_id => '3',
3082 3082 :status_id => '1', :subject => 'Copy with subtasks'},
3083 3083 :copy_subtasks => '1'
3084 3084 end
3085 3085 copy = Issue.where(:parent_id => nil).order('id DESC').first
3086 3086 assert_equal count, copy.descendants.count
3087 3087 assert_equal issue.descendants.map(&:subject).sort, copy.descendants.map(&:subject).sort
3088 3088 end
3089 3089
3090 3090 def test_create_as_copy_to_a_different_project_should_copy_subtask_custom_fields
3091 3091 issue = Issue.generate! {|i| i.custom_field_values = {'2' => 'Foo'}}
3092 3092 child = Issue.generate!(:parent_issue_id => issue.id) {|i| i.custom_field_values = {'2' => 'Bar'}}
3093 3093 @request.session[:user_id] = 1
3094 3094
3095 3095 assert_difference 'Issue.count', 2 do
3096 3096 post :create, :project_id => 'ecookbook', :copy_from => issue.id,
3097 3097 :issue => {:project_id => '2', :tracker_id => 1, :status_id => '1',
3098 3098 :subject => 'Copy with subtasks', :custom_field_values => {'2' => 'Foo'}},
3099 3099 :copy_subtasks => '1'
3100 3100 end
3101 3101
3102 3102 child_copy, issue_copy = Issue.order(:id => :desc).limit(2).to_a
3103 3103 assert_equal 2, issue_copy.project_id
3104 3104 assert_equal 'Foo', issue_copy.custom_field_value(2)
3105 3105 assert_equal 'Bar', child_copy.custom_field_value(2)
3106 3106 end
3107 3107
3108 3108 def test_create_as_copy_without_copy_subtasks_option_should_not_copy_subtasks
3109 3109 @request.session[:user_id] = 2
3110 3110 issue = Issue.generate_with_descendants!
3111 3111 assert_difference 'Issue.count', 1 do
3112 3112 post :create, :project_id => 1, :copy_from => 3,
3113 3113 :issue => {:project_id => '1', :tracker_id => '3',
3114 3114 :status_id => '1', :subject => 'Copy with subtasks'}
3115 3115 end
3116 3116 copy = Issue.where(:parent_id => nil).order('id DESC').first
3117 3117 assert_equal 0, copy.descendants.count
3118 3118 end
3119 3119
3120 3120 def test_create_as_copy_with_failure
3121 3121 @request.session[:user_id] = 2
3122 3122 post :create, :project_id => 1, :copy_from => 1,
3123 3123 :issue => {:project_id => '2', :tracker_id => '3', :status_id => '1', :subject => ''}
3124 3124
3125 3125 assert_response :success
3126 3126
3127 3127 assert_select 'form#issue-form[action="/projects/ecookbook/issues"]' do
3128 3128 assert_select 'select[name=?]', 'issue[project_id]' do
3129 3129 assert_select 'option[value="1"]:not([selected])', :text => 'eCookbook'
3130 3130 assert_select 'option[value="2"][selected=selected]', :text => 'OnlineStore'
3131 3131 end
3132 3132 assert_select 'input[name=copy_from][value="1"]'
3133 3133 end
3134 3134 end
3135 3135
3136 3136 def test_create_as_copy_on_project_without_permission_should_ignore_target_project
3137 3137 @request.session[:user_id] = 2
3138 3138 assert !User.find(2).member_of?(Project.find(4))
3139 3139
3140 3140 assert_difference 'Issue.count' do
3141 3141 post :create, :project_id => 1, :copy_from => 1,
3142 3142 :issue => {:project_id => '4', :tracker_id => '3', :status_id => '1', :subject => 'Copy'}
3143 3143 end
3144 3144 issue = Issue.order('id DESC').first
3145 3145 assert_equal 1, issue.project_id
3146 3146 end
3147 3147
3148 3148 def test_get_edit
3149 3149 @request.session[:user_id] = 2
3150 3150 get :edit, :id => 1
3151 3151 assert_response :success
3152 3152
3153 3153 assert_select 'select[name=?]', 'issue[project_id]'
3154 3154 # Be sure we don't display inactive IssuePriorities
3155 3155 assert ! IssuePriority.find(15).active?
3156 3156 assert_select 'select[name=?]', 'issue[priority_id]' do
3157 3157 assert_select 'option[value="15"]', 0
3158 3158 end
3159 3159 end
3160 3160
3161 3161 def test_edit_should_hide_project_if_user_is_not_allowed_to_change_project
3162 3162 WorkflowPermission.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :field_name => 'project_id', :rule => 'readonly')
3163 3163
3164 3164 @request.session[:user_id] = 2
3165 3165 get :edit, :id => 1
3166 3166 assert_response :success
3167 3167 assert_select 'select[name=?]', 'issue[project_id]', 0
3168 3168 end
3169 3169
3170 3170 def test_edit_should_not_hide_project_when_user_changes_the_project_even_if_project_is_readonly_on_target_project
3171 3171 WorkflowPermission.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :field_name => 'project_id', :rule => 'readonly')
3172 3172 issue = Issue.generate!(:project_id => 2)
3173 3173
3174 3174 @request.session[:user_id] = 2
3175 3175 get :edit, :id => issue.id, :issue => {:project_id => 1}
3176 3176 assert_response :success
3177 3177 assert_select 'select[name=?]', 'issue[project_id]'
3178 3178 end
3179 3179
3180 3180 def test_get_edit_should_display_the_time_entry_form_with_log_time_permission
3181 3181 @request.session[:user_id] = 2
3182 3182 Role.find_by_name('Manager').update_attribute :permissions, [:view_issues, :edit_issues, :log_time]
3183 3183
3184 3184 get :edit, :id => 1
3185 3185 assert_select 'input[name=?]', 'time_entry[hours]'
3186 3186 end
3187 3187
3188 3188 def test_get_edit_should_not_display_the_time_entry_form_without_log_time_permission
3189 3189 @request.session[:user_id] = 2
3190 3190 Role.find_by_name('Manager').remove_permission! :log_time
3191 3191
3192 3192 get :edit, :id => 1
3193 3193 assert_select 'input[name=?]', 'time_entry[hours]', 0
3194 3194 end
3195 3195
3196 3196 def test_get_edit_with_params
3197 3197 @request.session[:user_id] = 2
3198 3198 get :edit, :id => 1, :issue => { :status_id => 5, :priority_id => 7 },
3199 3199 :time_entry => { :hours => '2.5', :comments => 'test_get_edit_with_params', :activity_id => 10 }
3200 3200 assert_response :success
3201 3201
3202 3202 assert_select 'select[name=?]', 'issue[status_id]' do
3203 3203 assert_select 'option[value="5"][selected=selected]', :text => 'Closed'
3204 3204 end
3205 3205
3206 3206 assert_select 'select[name=?]', 'issue[priority_id]' do
3207 3207 assert_select 'option[value="7"][selected=selected]', :text => 'Urgent'
3208 3208 end
3209 3209
3210 assert_select 'input[name=?][value="2.5"]', 'time_entry[hours]'
3210 assert_select 'input[name=?][value="2.50"]', 'time_entry[hours]'
3211 3211 assert_select 'select[name=?]', 'time_entry[activity_id]' do
3212 3212 assert_select 'option[value="10"][selected=selected]', :text => 'Development'
3213 3213 end
3214 3214 assert_select 'input[name=?][value=test_get_edit_with_params]', 'time_entry[comments]'
3215 3215 end
3216 3216
3217 3217 def test_get_edit_with_multi_custom_field
3218 3218 field = CustomField.find(1)
3219 3219 field.update_attribute :multiple, true
3220 3220 issue = Issue.find(1)
3221 3221 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
3222 3222 issue.save!
3223 3223
3224 3224 @request.session[:user_id] = 2
3225 3225 get :edit, :id => 1
3226 3226 assert_response :success
3227 3227
3228 3228 assert_select 'select[name=?][multiple=multiple]', 'issue[custom_field_values][1][]' do
3229 3229 assert_select 'option', 3
3230 3230 assert_select 'option[value=MySQL][selected=selected]'
3231 3231 assert_select 'option[value=Oracle][selected=selected]'
3232 3232 assert_select 'option[value=PostgreSQL]:not([selected])'
3233 3233 end
3234 3234 end
3235 3235
3236 3236 def test_update_form_for_existing_issue
3237 3237 @request.session[:user_id] = 2
3238 3238 xhr :patch, :edit, :id => 1,
3239 3239 :issue => {:tracker_id => 2,
3240 3240 :subject => 'This is the test_new issue',
3241 3241 :description => 'This is the description',
3242 3242 :priority_id => 5}
3243 3243 assert_response :success
3244 3244 assert_equal 'text/javascript', response.content_type
3245 3245
3246 3246 assert_include 'This is the test_new issue', response.body
3247 3247 end
3248 3248
3249 3249 def test_update_form_for_existing_issue_should_keep_issue_author
3250 3250 @request.session[:user_id] = 3
3251 3251 patch :edit, :id => 1, :issue => {:subject => 'Changed'}
3252 3252 assert_response :success
3253 3253
3254 3254 assert_equal User.find(2), Issue.find(1).author
3255 3255 end
3256 3256
3257 3257 def test_update_form_for_existing_issue_should_propose_transitions_based_on_initial_status
3258 3258 @request.session[:user_id] = 2
3259 3259 WorkflowTransition.delete_all
3260 3260 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 2, :new_status_id => 1)
3261 3261 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 2, :new_status_id => 5)
3262 3262 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 5, :new_status_id => 4)
3263 3263
3264 3264 patch :edit, :id => 2,
3265 3265 :issue => {:tracker_id => 2,
3266 3266 :status_id => 5,
3267 3267 :subject => 'This is an issue'}
3268 3268
3269 3269 assert_select 'select[name=?]', 'issue[status_id]' do
3270 3270 assert_select 'option[value="1"]'
3271 3271 assert_select 'option[value="2"]'
3272 3272 assert_select 'option[value="5"][selected=selected]'
3273 3273 assert_select 'option', 3
3274 3274 end
3275 3275 end
3276 3276
3277 3277 def test_update_form_for_existing_issue_with_project_change
3278 3278 @request.session[:user_id] = 2
3279 3279 patch :edit, :id => 1,
3280 3280 :issue => {:project_id => 2,
3281 3281 :tracker_id => 2,
3282 3282 :subject => 'This is the test_new issue',
3283 3283 :description => 'This is the description',
3284 3284 :priority_id => 5}
3285 3285 assert_response :success
3286 3286 assert_select 'select[name=?]', 'issue[project_id]' do
3287 3287 assert_select 'option[value="2"][selected=selected]'
3288 3288 end
3289 3289 assert_select 'select[name=?]', 'issue[tracker_id]' do
3290 3290 assert_select 'option[value="2"][selected=selected]'
3291 3291 end
3292 3292 assert_select 'input[name=?][value=?]', 'issue[subject]', 'This is the test_new issue'
3293 3293 end
3294 3294
3295 3295 def test_update_form_should_keep_category_with_same_when_changing_project
3296 3296 source = Project.generate!
3297 3297 target = Project.generate!
3298 3298 source_category = IssueCategory.create!(:name => 'Foo', :project => source)
3299 3299 target_category = IssueCategory.create!(:name => 'Foo', :project => target)
3300 3300 issue = Issue.generate!(:project => source, :category => source_category)
3301 3301
3302 3302 @request.session[:user_id] = 1
3303 3303 patch :edit, :id => issue.id,
3304 3304 :issue => {:project_id => target.id, :category_id => source_category.id}
3305 3305 assert_response :success
3306 3306
3307 3307 assert_select 'select[name=?]', 'issue[category_id]' do
3308 3308 assert_select 'option[value=?][selected=selected]', target_category.id.to_s
3309 3309 end
3310 3310 end
3311 3311
3312 3312 def test_update_form_should_propose_default_status_for_existing_issue
3313 3313 @request.session[:user_id] = 2
3314 3314 WorkflowTransition.delete_all
3315 3315 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 2, :new_status_id => 3)
3316 3316
3317 3317 patch :edit, :id => 2
3318 3318 assert_response :success
3319 3319 assert_select 'select[name=?]', 'issue[status_id]' do
3320 3320 assert_select 'option[value="2"]'
3321 3321 assert_select 'option[value="3"]'
3322 3322 assert_select 'option', 2
3323 3323 end
3324 3324 end
3325 3325
3326 3326 def test_put_update_without_custom_fields_param
3327 3327 @request.session[:user_id] = 2
3328 3328
3329 3329 issue = Issue.find(1)
3330 3330 assert_equal '125', issue.custom_value_for(2).value
3331 3331
3332 3332 assert_difference('Journal.count') do
3333 3333 assert_difference('JournalDetail.count') do
3334 3334 put :update, :id => 1, :issue => {:subject => 'New subject'}
3335 3335 end
3336 3336 end
3337 3337 assert_redirected_to :action => 'show', :id => '1'
3338 3338 issue.reload
3339 3339 assert_equal 'New subject', issue.subject
3340 3340 # Make sure custom fields were not cleared
3341 3341 assert_equal '125', issue.custom_value_for(2).value
3342 3342 end
3343 3343
3344 3344 def test_put_update_with_project_change
3345 3345 @request.session[:user_id] = 2
3346 3346 ActionMailer::Base.deliveries.clear
3347 3347
3348 3348 with_settings :notified_events => %w(issue_updated) do
3349 3349 assert_difference('Journal.count') do
3350 3350 assert_difference('JournalDetail.count', 3) do
3351 3351 put :update, :id => 1, :issue => {:project_id => '2',
3352 3352 :tracker_id => '1', # no change
3353 3353 :priority_id => '6',
3354 3354 :category_id => '3'
3355 3355 }
3356 3356 end
3357 3357 end
3358 3358 end
3359 3359 assert_redirected_to :action => 'show', :id => '1'
3360 3360 issue = Issue.find(1)
3361 3361 assert_equal 2, issue.project_id
3362 3362 assert_equal 1, issue.tracker_id
3363 3363 assert_equal 6, issue.priority_id
3364 3364 assert_equal 3, issue.category_id
3365 3365
3366 3366 mail = ActionMailer::Base.deliveries.last
3367 3367 assert_not_nil mail
3368 3368 assert mail.subject.starts_with?("[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}]")
3369 3369 assert_mail_body_match "Project changed from eCookbook to OnlineStore", mail
3370 3370 end
3371 3371
3372 3372 def test_put_update_trying_to_move_issue_to_project_without_tracker_should_not_error
3373 3373 target = Project.generate!(:tracker_ids => [])
3374 3374 assert target.trackers.empty?
3375 3375 issue = Issue.generate!
3376 3376 @request.session[:user_id] = 1
3377 3377
3378 3378 put :update, :id => issue.id, :issue => {:project_id => target.id}
3379 3379 assert_response 302
3380 3380 end
3381 3381
3382 3382 def test_put_update_with_tracker_change
3383 3383 @request.session[:user_id] = 2
3384 3384 ActionMailer::Base.deliveries.clear
3385 3385
3386 3386 with_settings :notified_events => %w(issue_updated) do
3387 3387 assert_difference('Journal.count') do
3388 3388 assert_difference('JournalDetail.count', 3) do
3389 3389 put :update, :id => 1, :issue => {:project_id => '1',
3390 3390 :tracker_id => '2',
3391 3391 :priority_id => '6'
3392 3392 }
3393 3393 end
3394 3394 end
3395 3395 end
3396 3396 assert_redirected_to :action => 'show', :id => '1'
3397 3397 issue = Issue.find(1)
3398 3398 assert_equal 1, issue.project_id
3399 3399 assert_equal 2, issue.tracker_id
3400 3400 assert_equal 6, issue.priority_id
3401 3401 assert_equal 1, issue.category_id
3402 3402
3403 3403 mail = ActionMailer::Base.deliveries.last
3404 3404 assert_not_nil mail
3405 3405 assert mail.subject.starts_with?("[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}]")
3406 3406 assert_mail_body_match "Tracker changed from Bug to Feature request", mail
3407 3407 end
3408 3408
3409 3409 def test_put_update_with_custom_field_change
3410 3410 @request.session[:user_id] = 2
3411 3411 issue = Issue.find(1)
3412 3412 assert_equal '125', issue.custom_value_for(2).value
3413 3413
3414 3414 with_settings :notified_events => %w(issue_updated) do
3415 3415 assert_difference('Journal.count') do
3416 3416 assert_difference('JournalDetail.count', 3) do
3417 3417 put :update, :id => 1, :issue => {:subject => 'Custom field change',
3418 3418 :priority_id => '6',
3419 3419 :category_id => '1', # no change
3420 3420 :custom_field_values => { '2' => 'New custom value' }
3421 3421 }
3422 3422 end
3423 3423 end
3424 3424 end
3425 3425 assert_redirected_to :action => 'show', :id => '1'
3426 3426 issue.reload
3427 3427 assert_equal 'New custom value', issue.custom_value_for(2).value
3428 3428
3429 3429 mail = ActionMailer::Base.deliveries.last
3430 3430 assert_not_nil mail
3431 3431 assert_mail_body_match "Searchable field changed from 125 to New custom value", mail
3432 3432 end
3433 3433
3434 3434 def test_put_update_with_multi_custom_field_change
3435 3435 field = CustomField.find(1)
3436 3436 field.update_attribute :multiple, true
3437 3437 issue = Issue.find(1)
3438 3438 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
3439 3439 issue.save!
3440 3440
3441 3441 @request.session[:user_id] = 2
3442 3442 assert_difference('Journal.count') do
3443 3443 assert_difference('JournalDetail.count', 3) do
3444 3444 put :update, :id => 1,
3445 3445 :issue => {
3446 3446 :subject => 'Custom field change',
3447 3447 :custom_field_values => { '1' => ['', 'Oracle', 'PostgreSQL'] }
3448 3448 }
3449 3449 end
3450 3450 end
3451 3451 assert_redirected_to :action => 'show', :id => '1'
3452 3452 assert_equal ['Oracle', 'PostgreSQL'], Issue.find(1).custom_field_value(1).sort
3453 3453 end
3454 3454
3455 3455 def test_put_update_with_status_and_assignee_change
3456 3456 issue = Issue.find(1)
3457 3457 assert_equal 1, issue.status_id
3458 3458 @request.session[:user_id] = 2
3459 3459
3460 3460 with_settings :notified_events => %w(issue_updated) do
3461 3461 assert_difference('TimeEntry.count', 0) do
3462 3462 put :update,
3463 3463 :id => 1,
3464 3464 :issue => { :status_id => 2, :assigned_to_id => 3, :notes => 'Assigned to dlopper' },
3465 3465 :time_entry => { :hours => '', :comments => '', :activity_id => TimeEntryActivity.first }
3466 3466 end
3467 3467 end
3468 3468 assert_redirected_to :action => 'show', :id => '1'
3469 3469 issue.reload
3470 3470 assert_equal 2, issue.status_id
3471 3471 j = Journal.order('id DESC').first
3472 3472 assert_equal 'Assigned to dlopper', j.notes
3473 3473 assert_equal 2, j.details.size
3474 3474
3475 3475 mail = ActionMailer::Base.deliveries.last
3476 3476 assert_mail_body_match "Status changed from New to Assigned", mail
3477 3477 # subject should contain the new status
3478 3478 assert mail.subject.include?("(#{ IssueStatus.find(2).name })")
3479 3479 end
3480 3480
3481 3481 def test_put_update_with_note_only
3482 3482 notes = 'Note added by IssuesControllerTest#test_update_with_note_only'
3483 3483
3484 3484 with_settings :notified_events => %w(issue_updated) do
3485 3485 # anonymous user
3486 3486 put :update,
3487 3487 :id => 1,
3488 3488 :issue => { :notes => notes }
3489 3489 end
3490 3490 assert_redirected_to :action => 'show', :id => '1'
3491 3491 j = Journal.order('id DESC').first
3492 3492 assert_equal notes, j.notes
3493 3493 assert_equal 0, j.details.size
3494 3494 assert_equal User.anonymous, j.user
3495 3495
3496 3496 mail = ActionMailer::Base.deliveries.last
3497 3497 assert_mail_body_match notes, mail
3498 3498 end
3499 3499
3500 3500 def test_put_update_with_private_note_only
3501 3501 notes = 'Private note'
3502 3502 @request.session[:user_id] = 2
3503 3503
3504 3504 assert_difference 'Journal.count' do
3505 3505 put :update, :id => 1, :issue => {:notes => notes, :private_notes => '1'}
3506 3506 assert_redirected_to :action => 'show', :id => '1'
3507 3507 end
3508 3508
3509 3509 j = Journal.order('id DESC').first
3510 3510 assert_equal notes, j.notes
3511 3511 assert_equal true, j.private_notes
3512 3512 end
3513 3513
3514 3514 def test_put_update_with_private_note_and_changes
3515 3515 notes = 'Private note'
3516 3516 @request.session[:user_id] = 2
3517 3517
3518 3518 assert_difference 'Journal.count', 2 do
3519 3519 put :update, :id => 1, :issue => {:subject => 'New subject', :notes => notes, :private_notes => '1'}
3520 3520 assert_redirected_to :action => 'show', :id => '1'
3521 3521 end
3522 3522
3523 3523 j = Journal.order('id DESC').first
3524 3524 assert_equal notes, j.notes
3525 3525 assert_equal true, j.private_notes
3526 3526 assert_equal 0, j.details.count
3527 3527
3528 3528 j = Journal.order('id DESC').offset(1).first
3529 3529 assert_nil j.notes
3530 3530 assert_equal false, j.private_notes
3531 3531 assert_equal 1, j.details.count
3532 3532 end
3533 3533
3534 3534 def test_put_update_with_note_and_spent_time
3535 3535 @request.session[:user_id] = 2
3536 3536 spent_hours_before = Issue.find(1).spent_hours
3537 3537 assert_difference('TimeEntry.count') do
3538 3538 put :update,
3539 3539 :id => 1,
3540 3540 :issue => { :notes => '2.5 hours added' },
3541 3541 :time_entry => { :hours => '2.5', :comments => 'test_put_update_with_note_and_spent_time', :activity_id => TimeEntryActivity.first.id }
3542 3542 end
3543 3543 assert_redirected_to :action => 'show', :id => '1'
3544 3544
3545 3545 issue = Issue.find(1)
3546 3546
3547 3547 j = Journal.order('id DESC').first
3548 3548 assert_equal '2.5 hours added', j.notes
3549 3549 assert_equal 0, j.details.size
3550 3550
3551 3551 t = issue.time_entries.find_by_comments('test_put_update_with_note_and_spent_time')
3552 3552 assert_not_nil t
3553 3553 assert_equal 2.5, t.hours
3554 3554 assert_equal spent_hours_before + 2.5, issue.spent_hours
3555 3555 end
3556 3556
3557 3557 def test_put_update_should_preserve_parent_issue_even_if_not_visible
3558 3558 parent = Issue.generate!(:project_id => 1, :is_private => true)
3559 3559 issue = Issue.generate!(:parent_issue_id => parent.id)
3560 3560 assert !parent.visible?(User.find(3))
3561 3561 @request.session[:user_id] = 3
3562 3562
3563 3563 get :edit, :id => issue.id
3564 3564 assert_select 'input[name=?][value=?]', 'issue[parent_issue_id]', parent.id.to_s
3565 3565
3566 3566 put :update, :id => issue.id, :issue => {:subject => 'New subject', :parent_issue_id => parent.id.to_s}
3567 3567 assert_response 302
3568 3568 assert_equal parent, issue.parent
3569 3569 end
3570 3570
3571 3571 def test_put_update_with_attachment_only
3572 3572 set_tmp_attachments_directory
3573 3573
3574 3574 # Delete all fixtured journals, a race condition can occur causing the wrong
3575 3575 # journal to get fetched in the next find.
3576 3576 Journal.delete_all
3577 3577
3578 3578 with_settings :notified_events => %w(issue_updated) do
3579 3579 # anonymous user
3580 3580 assert_difference 'Attachment.count' do
3581 3581 put :update, :id => 1,
3582 3582 :issue => {:notes => ''},
3583 3583 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
3584 3584 end
3585 3585 end
3586 3586
3587 3587 assert_redirected_to :action => 'show', :id => '1'
3588 3588 j = Issue.find(1).journals.reorder('id DESC').first
3589 3589 assert j.notes.blank?
3590 3590 assert_equal 1, j.details.size
3591 3591 assert_equal 'testfile.txt', j.details.first.value
3592 3592 assert_equal User.anonymous, j.user
3593 3593
3594 3594 attachment = Attachment.order('id DESC').first
3595 3595 assert_equal Issue.find(1), attachment.container
3596 3596 assert_equal User.anonymous, attachment.author
3597 3597 assert_equal 'testfile.txt', attachment.filename
3598 3598 assert_equal 'text/plain', attachment.content_type
3599 3599 assert_equal 'test file', attachment.description
3600 3600 assert_equal 59, attachment.filesize
3601 3601 assert File.exists?(attachment.diskfile)
3602 3602 assert_equal 59, File.size(attachment.diskfile)
3603 3603
3604 3604 mail = ActionMailer::Base.deliveries.last
3605 3605 assert_mail_body_match 'testfile.txt', mail
3606 3606 end
3607 3607
3608 3608 def test_put_update_with_failure_should_save_attachments
3609 3609 set_tmp_attachments_directory
3610 3610 @request.session[:user_id] = 2
3611 3611
3612 3612 assert_no_difference 'Journal.count' do
3613 3613 assert_difference 'Attachment.count' do
3614 3614 put :update, :id => 1,
3615 3615 :issue => { :subject => '' },
3616 3616 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
3617 3617 assert_response :success
3618 3618 end
3619 3619 end
3620 3620
3621 3621 attachment = Attachment.order('id DESC').first
3622 3622 assert_equal 'testfile.txt', attachment.filename
3623 3623 assert File.exists?(attachment.diskfile)
3624 3624 assert_nil attachment.container
3625 3625
3626 3626 assert_select 'input[name=?][value=?]', 'attachments[p0][token]', attachment.token
3627 3627 assert_select 'input[name=?][value=?]', 'attachments[p0][filename]', 'testfile.txt'
3628 3628 end
3629 3629
3630 3630 def test_put_update_with_failure_should_keep_saved_attachments
3631 3631 set_tmp_attachments_directory
3632 3632 attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 2)
3633 3633 @request.session[:user_id] = 2
3634 3634
3635 3635 assert_no_difference 'Journal.count' do
3636 3636 assert_no_difference 'Attachment.count' do
3637 3637 put :update, :id => 1,
3638 3638 :issue => { :subject => '' },
3639 3639 :attachments => {'p0' => {'token' => attachment.token}}
3640 3640 assert_response :success
3641 3641 end
3642 3642 end
3643 3643
3644 3644 assert_select 'input[name=?][value=?]', 'attachments[p0][token]', attachment.token
3645 3645 assert_select 'input[name=?][value=?]', 'attachments[p0][filename]', 'testfile.txt'
3646 3646 end
3647 3647
3648 3648 def test_put_update_should_attach_saved_attachments
3649 3649 set_tmp_attachments_directory
3650 3650 attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 2)
3651 3651 @request.session[:user_id] = 2
3652 3652
3653 3653 assert_difference 'Journal.count' do
3654 3654 assert_difference 'JournalDetail.count' do
3655 3655 assert_no_difference 'Attachment.count' do
3656 3656 put :update, :id => 1,
3657 3657 :issue => {:notes => 'Attachment added'},
3658 3658 :attachments => {'p0' => {'token' => attachment.token}}
3659 3659 assert_redirected_to '/issues/1'
3660 3660 end
3661 3661 end
3662 3662 end
3663 3663
3664 3664 attachment.reload
3665 3665 assert_equal Issue.find(1), attachment.container
3666 3666
3667 3667 journal = Journal.order('id DESC').first
3668 3668 assert_equal 1, journal.details.size
3669 3669 assert_equal 'testfile.txt', journal.details.first.value
3670 3670 end
3671 3671
3672 3672 def test_put_update_with_attachment_that_fails_to_save
3673 3673 set_tmp_attachments_directory
3674 3674
3675 3675 # anonymous user
3676 3676 with_settings :attachment_max_size => 0 do
3677 3677 put :update,
3678 3678 :id => 1,
3679 3679 :issue => {:notes => ''},
3680 3680 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
3681 3681 assert_redirected_to :action => 'show', :id => '1'
3682 3682 assert_equal '1 file(s) could not be saved.', flash[:warning]
3683 3683 end
3684 3684 end
3685 3685
3686 3686 def test_put_update_with_attachment_deletion_should_create_a_single_journal
3687 3687 set_tmp_attachments_directory
3688 3688 @request.session[:user_id] = 2
3689 3689
3690 3690 journal = new_record(Journal) do
3691 3691 assert_difference 'Attachment.count', -2 do
3692 3692 put :update,
3693 3693 :id => 3,
3694 3694 :issue => {
3695 3695 :notes => 'Removing attachments',
3696 3696 :deleted_attachment_ids => ['1', '5']
3697 3697 }
3698 3698 end
3699 3699 end
3700 3700 assert_equal 'Removing attachments', journal.notes
3701 3701 assert_equal 2, journal.details.count
3702 3702 end
3703 3703
3704 3704 def test_put_update_with_attachment_deletion_and_failure_should_preserve_selected_attachments
3705 3705 set_tmp_attachments_directory
3706 3706 @request.session[:user_id] = 2
3707 3707
3708 3708 assert_no_difference 'Journal.count' do
3709 3709 assert_no_difference 'Attachment.count' do
3710 3710 put :update,
3711 3711 :id => 3,
3712 3712 :issue => {
3713 3713 :subject => '',
3714 3714 :notes => 'Removing attachments',
3715 3715 :deleted_attachment_ids => ['1', '5']
3716 3716 }
3717 3717 end
3718 3718 end
3719 3719 assert_select 'input[name=?][value="1"][checked=checked]', 'issue[deleted_attachment_ids][]'
3720 3720 assert_select 'input[name=?][value="5"][checked=checked]', 'issue[deleted_attachment_ids][]'
3721 3721 assert_select 'input[name=?][value="6"]:not([checked])', 'issue[deleted_attachment_ids][]'
3722 3722 end
3723 3723
3724 3724 def test_put_update_with_no_change
3725 3725 issue = Issue.find(1)
3726 3726 issue.journals.clear
3727 3727 ActionMailer::Base.deliveries.clear
3728 3728
3729 3729 put :update,
3730 3730 :id => 1,
3731 3731 :issue => {:notes => ''}
3732 3732 assert_redirected_to :action => 'show', :id => '1'
3733 3733
3734 3734 issue.reload
3735 3735 assert issue.journals.empty?
3736 3736 # No email should be sent
3737 3737 assert ActionMailer::Base.deliveries.empty?
3738 3738 end
3739 3739
3740 3740 def test_put_update_should_send_a_notification
3741 3741 @request.session[:user_id] = 2
3742 3742 ActionMailer::Base.deliveries.clear
3743 3743 issue = Issue.find(1)
3744 3744 old_subject = issue.subject
3745 3745 new_subject = 'Subject modified by IssuesControllerTest#test_post_edit'
3746 3746
3747 3747 with_settings :notified_events => %w(issue_updated) do
3748 3748 put :update, :id => 1, :issue => {:subject => new_subject,
3749 3749 :priority_id => '6',
3750 3750 :category_id => '1' # no change
3751 3751 }
3752 3752 assert_equal 1, ActionMailer::Base.deliveries.size
3753 3753 end
3754 3754 end
3755 3755
3756 3756 def test_put_update_with_invalid_spent_time_hours_only
3757 3757 @request.session[:user_id] = 2
3758 3758 notes = 'Note added by IssuesControllerTest#test_post_edit_with_invalid_spent_time'
3759 3759
3760 3760 assert_no_difference('Journal.count') do
3761 3761 put :update,
3762 3762 :id => 1,
3763 3763 :issue => {:notes => notes},
3764 3764 :time_entry => {"comments"=>"", "activity_id"=>"", "hours"=>"2z"}
3765 3765 end
3766 3766 assert_response :success
3767 3767
3768 3768 assert_select_error /Activity cannot be blank/
3769 3769 assert_select 'textarea[name=?]', 'issue[notes]', :text => notes
3770 3770 assert_select 'input[name=?][value=?]', 'time_entry[hours]', '2z'
3771 3771 end
3772 3772
3773 3773 def test_put_update_with_invalid_spent_time_comments_only
3774 3774 @request.session[:user_id] = 2
3775 3775 notes = 'Note added by IssuesControllerTest#test_post_edit_with_invalid_spent_time'
3776 3776
3777 3777 assert_no_difference('Journal.count') do
3778 3778 put :update,
3779 3779 :id => 1,
3780 3780 :issue => {:notes => notes},
3781 3781 :time_entry => {"comments"=>"this is my comment", "activity_id"=>"", "hours"=>""}
3782 3782 end
3783 3783 assert_response :success
3784 3784
3785 3785 assert_select_error /Activity cannot be blank/
3786 3786 assert_select_error /Hours cannot be blank/
3787 3787 assert_select 'textarea[name=?]', 'issue[notes]', :text => notes
3788 3788 assert_select 'input[name=?][value=?]', 'time_entry[comments]', 'this is my comment'
3789 3789 end
3790 3790
3791 3791 def test_put_update_should_allow_fixed_version_to_be_set_to_a_subproject
3792 3792 issue = Issue.find(2)
3793 3793 @request.session[:user_id] = 2
3794 3794
3795 3795 put :update,
3796 3796 :id => issue.id,
3797 3797 :issue => {
3798 3798 :fixed_version_id => 4
3799 3799 }
3800 3800
3801 3801 assert_response :redirect
3802 3802 issue.reload
3803 3803 assert_equal 4, issue.fixed_version_id
3804 3804 assert_not_equal issue.project_id, issue.fixed_version.project_id
3805 3805 end
3806 3806
3807 3807 def test_put_update_should_redirect_back_using_the_back_url_parameter
3808 3808 issue = Issue.find(2)
3809 3809 @request.session[:user_id] = 2
3810 3810
3811 3811 put :update,
3812 3812 :id => issue.id,
3813 3813 :issue => {
3814 3814 :fixed_version_id => 4
3815 3815 },
3816 3816 :back_url => '/issues'
3817 3817
3818 3818 assert_response :redirect
3819 3819 assert_redirected_to '/issues'
3820 3820 end
3821 3821
3822 3822 def test_put_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host
3823 3823 issue = Issue.find(2)
3824 3824 @request.session[:user_id] = 2
3825 3825
3826 3826 put :update,
3827 3827 :id => issue.id,
3828 3828 :issue => {
3829 3829 :fixed_version_id => 4
3830 3830 },
3831 3831 :back_url => 'http://google.com'
3832 3832
3833 3833 assert_response :redirect
3834 3834 assert_redirected_to :controller => 'issues', :action => 'show', :id => issue.id
3835 3835 end
3836 3836
3837 3837 def test_put_update_should_redirect_with_previous_and_next_issue_ids_params
3838 3838 @request.session[:user_id] = 2
3839 3839
3840 3840 put :update, :id => 11,
3841 3841 :issue => {:status_id => 6, :notes => 'Notes'},
3842 3842 :prev_issue_id => 8,
3843 3843 :next_issue_id => 12,
3844 3844 :issue_position => 2,
3845 3845 :issue_count => 3
3846 3846
3847 3847 assert_redirected_to '/issues/11?issue_count=3&issue_position=2&next_issue_id=12&prev_issue_id=8'
3848 3848 end
3849 3849
3850 3850 def test_update_with_permission_on_tracker_should_be_allowed
3851 3851 role = Role.find(1)
3852 3852 role.set_permission_trackers :edit_issues, [1]
3853 3853 role.save!
3854 3854 issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :subject => 'Original subject')
3855 3855
3856 3856 @request.session[:user_id] = 2
3857 3857 put :update, :id => issue.id, :issue => {:subject => 'Changed subject'}
3858 3858 assert_response 302
3859 3859 assert_equal 'Changed subject', issue.reload.subject
3860 3860 end
3861 3861
3862 3862 def test_update_without_permission_on_tracker_should_be_denied
3863 3863 role = Role.find(1)
3864 3864 role.set_permission_trackers :edit_issues, [1]
3865 3865 role.save!
3866 3866 issue = Issue.generate!(:project_id => 1, :tracker_id => 2, :subject => 'Original subject')
3867 3867
3868 3868 @request.session[:user_id] = 2
3869 3869 put :update, :id => issue.id, :issue => {:subject => 'Changed subject'}
3870 3870 assert_response 302
3871 3871 assert_equal 'Original subject', issue.reload.subject
3872 3872 end
3873 3873
3874 3874 def test_get_bulk_edit
3875 3875 @request.session[:user_id] = 2
3876 3876 get :bulk_edit, :ids => [1, 3]
3877 3877 assert_response :success
3878 3878
3879 3879 assert_select 'ul#bulk-selection' do
3880 3880 assert_select 'li', 2
3881 3881 assert_select 'li a', :text => 'Bug #1'
3882 3882 end
3883 3883
3884 3884 assert_select 'form#bulk_edit_form[action=?]', '/issues/bulk_update' do
3885 3885 assert_select 'input[name=?]', 'ids[]', 2
3886 3886 assert_select 'input[name=?][value="1"][type=hidden]', 'ids[]'
3887 3887
3888 3888 assert_select 'select[name=?]', 'issue[project_id]'
3889 3889 assert_select 'input[name=?]', 'issue[parent_issue_id]'
3890 3890
3891 3891 # Project specific custom field, date type
3892 3892 field = CustomField.find(9)
3893 3893 assert !field.is_for_all?
3894 3894 assert_equal 'date', field.field_format
3895 3895 assert_select 'input[name=?]', 'issue[custom_field_values][9]'
3896 3896
3897 3897 # System wide custom field
3898 3898 assert CustomField.find(1).is_for_all?
3899 3899 assert_select 'select[name=?]', 'issue[custom_field_values][1]'
3900 3900
3901 3901 # Be sure we don't display inactive IssuePriorities
3902 3902 assert ! IssuePriority.find(15).active?
3903 3903 assert_select 'select[name=?]', 'issue[priority_id]' do
3904 3904 assert_select 'option[value="15"]', 0
3905 3905 end
3906 3906 end
3907 3907 end
3908 3908
3909 3909 def test_get_bulk_edit_on_different_projects
3910 3910 @request.session[:user_id] = 2
3911 3911 get :bulk_edit, :ids => [1, 2, 6]
3912 3912 assert_response :success
3913 3913
3914 3914 # Can not set issues from different projects as children of an issue
3915 3915 assert_select 'input[name=?]', 'issue[parent_issue_id]', 0
3916 3916
3917 3917 # Project specific custom field, date type
3918 3918 field = CustomField.find(9)
3919 3919 assert !field.is_for_all?
3920 3920 assert !field.project_ids.include?(Issue.find(6).project_id)
3921 3921 assert_select 'input[name=?]', 'issue[custom_field_values][9]', 0
3922 3922 end
3923 3923
3924 3924 def test_get_bulk_edit_with_user_custom_field
3925 3925 field = IssueCustomField.create!(:name => 'Tester', :field_format => 'user', :is_for_all => true, :tracker_ids => [1,2,3])
3926 3926
3927 3927 @request.session[:user_id] = 2
3928 3928 get :bulk_edit, :ids => [1, 2]
3929 3929 assert_response :success
3930 3930
3931 3931 assert_select 'select.user_cf[name=?]', "issue[custom_field_values][#{field.id}]" do
3932 3932 assert_select 'option', Project.find(1).users.count + 2 # "no change" + "none" options
3933 3933 end
3934 3934 end
3935 3935
3936 3936 def test_get_bulk_edit_with_version_custom_field
3937 3937 field = IssueCustomField.create!(:name => 'Affected version', :field_format => 'version', :is_for_all => true, :tracker_ids => [1,2,3])
3938 3938
3939 3939 @request.session[:user_id] = 2
3940 3940 get :bulk_edit, :ids => [1, 2]
3941 3941 assert_response :success
3942 3942
3943 3943 assert_select 'select.version_cf[name=?]', "issue[custom_field_values][#{field.id}]" do
3944 3944 assert_select 'option', Project.find(1).shared_versions.count + 2 # "no change" + "none" options
3945 3945 end
3946 3946 end
3947 3947
3948 3948 def test_get_bulk_edit_with_multi_custom_field
3949 3949 field = CustomField.find(1)
3950 3950 field.update_attribute :multiple, true
3951 3951
3952 3952 @request.session[:user_id] = 2
3953 3953 get :bulk_edit, :ids => [1, 3]
3954 3954 assert_response :success
3955 3955
3956 3956 assert_select 'select[name=?]', 'issue[custom_field_values][1][]' do
3957 3957 assert_select 'option', field.possible_values.size + 1 # "none" options
3958 3958 end
3959 3959 end
3960 3960
3961 3961 def test_bulk_edit_should_propose_to_clear_text_custom_fields
3962 3962 @request.session[:user_id] = 2
3963 3963 get :bulk_edit, :ids => [1, 3]
3964 3964 assert_response :success
3965 3965
3966 3966 assert_select 'input[name=?][value=?]', 'issue[custom_field_values][2]', '__none__'
3967 3967 end
3968 3968
3969 3969 def test_bulk_edit_should_only_propose_statuses_allowed_for_all_issues
3970 3970 WorkflowTransition.delete_all
3971 3971 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1,
3972 3972 :old_status_id => 1, :new_status_id => 1)
3973 3973 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1,
3974 3974 :old_status_id => 1, :new_status_id => 3)
3975 3975 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1,
3976 3976 :old_status_id => 1, :new_status_id => 4)
3977 3977 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2,
3978 3978 :old_status_id => 2, :new_status_id => 1)
3979 3979 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2,
3980 3980 :old_status_id => 2, :new_status_id => 3)
3981 3981 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2,
3982 3982 :old_status_id => 2, :new_status_id => 5)
3983 3983 @request.session[:user_id] = 2
3984 3984 get :bulk_edit, :ids => [1, 2]
3985 3985
3986 3986 assert_select 'select[name=?]', 'issue[status_id]' do
3987 3987 assert_select 'option[value=""]'
3988 3988 assert_select 'option[value="1"]'
3989 3989 assert_select 'option[value="3"]'
3990 3990 assert_select 'option', 3 # 2 statuses + "no change" option
3991 3991 end
3992 3992 end
3993 3993
3994 3994 def test_bulk_edit_should_propose_target_project_open_shared_versions
3995 3995 @request.session[:user_id] = 2
3996 3996 post :bulk_edit, :ids => [1, 2, 6], :issue => {:project_id => 1}
3997 3997 assert_response :success
3998 3998
3999 3999 expected_versions = Project.find(1).shared_versions.open.to_a.sort
4000 4000
4001 4001 assert_select 'select[name=?]', 'issue[fixed_version_id]' do
4002 4002 expected_versions.each do |version|
4003 4003 assert_select 'option[value=?]', version.id.to_s
4004 4004 end
4005 4005 assert_select 'option[value=""]'
4006 4006 assert_select 'option[value="none"]'
4007 4007 assert_select 'option', expected_versions.size + 2
4008 4008 end
4009 4009 end
4010 4010
4011 4011 def test_bulk_edit_should_propose_target_project_categories
4012 4012 @request.session[:user_id] = 2
4013 4013 post :bulk_edit, :ids => [1, 2, 6], :issue => {:project_id => 1}
4014 4014 assert_response :success
4015 4015
4016 4016 expected_categories = Project.find(1).issue_categories.sort
4017 4017
4018 4018 assert_select 'select[name=?]', 'issue[category_id]' do
4019 4019 expected_categories.each do |category|
4020 4020 assert_select 'option[value=?]', category.id.to_s
4021 4021 end
4022 4022 assert_select 'option[value=""]'
4023 4023 assert_select 'option[value="none"]'
4024 4024 assert_select 'option', expected_categories.size + 2
4025 4025 end
4026 4026 end
4027 4027
4028 4028 def test_bulk_edit_should_only_propose_issues_trackers_custom_fields
4029 4029 IssueCustomField.delete_all
4030 4030 field1 = IssueCustomField.generate!(:tracker_ids => [1], :is_for_all => true)
4031 4031 field2 = IssueCustomField.generate!(:tracker_ids => [2], :is_for_all => true)
4032 4032 @request.session[:user_id] = 2
4033 4033
4034 4034 issue_ids = Issue.where(:project_id => 1, :tracker_id => 1).limit(2).ids
4035 4035 get :bulk_edit, :ids => issue_ids
4036 4036 assert_response :success
4037 4037
4038 4038 assert_select 'input[name=?]', "issue[custom_field_values][#{field1.id}]"
4039 4039 assert_select 'input[name=?]', "issue[custom_field_values][#{field2.id}]", 0
4040 4040 end
4041 4041
4042 4042 def test_bulk_edit_should_propose_target_tracker_custom_fields
4043 4043 IssueCustomField.delete_all
4044 4044 field1 = IssueCustomField.generate!(:tracker_ids => [1], :is_for_all => true)
4045 4045 field2 = IssueCustomField.generate!(:tracker_ids => [2], :is_for_all => true)
4046 4046 @request.session[:user_id] = 2
4047 4047
4048 4048 issue_ids = Issue.where(:project_id => 1, :tracker_id => 1).limit(2).ids
4049 4049 get :bulk_edit, :ids => issue_ids, :issue => {:tracker_id => 2}
4050 4050 assert_response :success
4051 4051
4052 4052 assert_select 'input[name=?]', "issue[custom_field_values][#{field1.id}]", 0
4053 4053 assert_select 'input[name=?]', "issue[custom_field_values][#{field2.id}]"
4054 4054 end
4055 4055
4056 4056 def test_bulk_update
4057 4057 @request.session[:user_id] = 2
4058 4058 # update issues priority
4059 4059 post :bulk_update, :ids => [1, 2], :notes => 'Bulk editing',
4060 4060 :issue => {:priority_id => 7,
4061 4061 :assigned_to_id => '',
4062 4062 :custom_field_values => {'2' => ''}}
4063 4063
4064 4064 assert_response 302
4065 4065 # check that the issues were updated
4066 4066 assert_equal [7, 7], Issue.where(:id =>[1, 2]).collect {|i| i.priority.id}
4067 4067
4068 4068 issue = Issue.find(1)
4069 4069 journal = issue.journals.reorder('created_on DESC').first
4070 4070 assert_equal '125', issue.custom_value_for(2).value
4071 4071 assert_equal 'Bulk editing', journal.notes
4072 4072 assert_equal 1, journal.details.size
4073 4073 end
4074 4074
4075 4075 def test_bulk_update_with_group_assignee
4076 4076 group = Group.find(11)
4077 4077 project = Project.find(1)
4078 4078 project.members << Member.new(:principal => group, :roles => [Role.givable.first])
4079 4079
4080 4080 @request.session[:user_id] = 2
4081 4081 # update issues assignee
4082 4082 with_settings :issue_group_assignment => '1' do
4083 4083 post :bulk_update, :ids => [1, 2], :notes => 'Bulk editing',
4084 4084 :issue => {:priority_id => '',
4085 4085 :assigned_to_id => group.id,
4086 4086 :custom_field_values => {'2' => ''}}
4087 4087
4088 4088 assert_response 302
4089 4089 assert_equal [group, group], Issue.where(:id => [1, 2]).collect {|i| i.assigned_to}
4090 4090 end
4091 4091 end
4092 4092
4093 4093 def test_bulk_update_on_different_projects
4094 4094 @request.session[:user_id] = 2
4095 4095 # update issues priority
4096 4096 post :bulk_update, :ids => [1, 2, 6], :notes => 'Bulk editing',
4097 4097 :issue => {:priority_id => 7,
4098 4098 :assigned_to_id => '',
4099 4099 :custom_field_values => {'2' => ''}}
4100 4100
4101 4101 assert_response 302
4102 4102 # check that the issues were updated
4103 4103 assert_equal [7, 7, 7], Issue.find([1,2,6]).map(&:priority_id)
4104 4104
4105 4105 issue = Issue.find(1)
4106 4106 journal = issue.journals.reorder('created_on DESC').first
4107 4107 assert_equal '125', issue.custom_value_for(2).value
4108 4108 assert_equal 'Bulk editing', journal.notes
4109 4109 assert_equal 1, journal.details.size
4110 4110 end
4111 4111
4112 4112 def test_bulk_update_on_different_projects_without_rights
4113 4113 @request.session[:user_id] = 3
4114 4114 user = User.find(3)
4115 4115 action = { :controller => "issues", :action => "bulk_update" }
4116 4116 assert user.allowed_to?(action, Issue.find(1).project)
4117 4117 assert ! user.allowed_to?(action, Issue.find(6).project)
4118 4118 post :bulk_update, :ids => [1, 6], :notes => 'Bulk should fail',
4119 4119 :issue => {:priority_id => 7,
4120 4120 :assigned_to_id => '',
4121 4121 :custom_field_values => {'2' => ''}}
4122 4122 assert_response 403
4123 4123 assert_not_equal "Bulk should fail", Journal.last.notes
4124 4124 end
4125 4125
4126 4126 def test_bullk_update_should_send_a_notification
4127 4127 @request.session[:user_id] = 2
4128 4128 ActionMailer::Base.deliveries.clear
4129 4129 with_settings :notified_events => %w(issue_updated) do
4130 4130 post(:bulk_update,
4131 4131 {
4132 4132 :ids => [1, 2],
4133 4133 :notes => 'Bulk editing',
4134 4134 :issue => {
4135 4135 :priority_id => 7,
4136 4136 :assigned_to_id => '',
4137 4137 :custom_field_values => {'2' => ''}
4138 4138 }
4139 4139 })
4140 4140 assert_response 302
4141 4141 assert_equal 2, ActionMailer::Base.deliveries.size
4142 4142 end
4143 4143 end
4144 4144
4145 4145 def test_bulk_update_project
4146 4146 @request.session[:user_id] = 2
4147 4147 post :bulk_update, :ids => [1, 2], :issue => {:project_id => '2'}
4148 4148 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook'
4149 4149 # Issues moved to project 2
4150 4150 assert_equal 2, Issue.find(1).project_id
4151 4151 assert_equal 2, Issue.find(2).project_id
4152 4152 # No tracker change
4153 4153 assert_equal 1, Issue.find(1).tracker_id
4154 4154 assert_equal 2, Issue.find(2).tracker_id
4155 4155 end
4156 4156
4157 4157 def test_bulk_update_project_on_single_issue_should_follow_when_needed
4158 4158 @request.session[:user_id] = 2
4159 4159 post :bulk_update, :id => 1, :issue => {:project_id => '2'}, :follow => '1'
4160 4160 assert_redirected_to '/issues/1'
4161 4161 end
4162 4162
4163 4163 def test_bulk_update_project_on_multiple_issues_should_follow_when_needed
4164 4164 @request.session[:user_id] = 2
4165 4165 post :bulk_update, :id => [1, 2], :issue => {:project_id => '2'}, :follow => '1'
4166 4166 assert_redirected_to '/projects/onlinestore/issues'
4167 4167 end
4168 4168
4169 4169 def test_bulk_update_tracker
4170 4170 @request.session[:user_id] = 2
4171 4171 post :bulk_update, :ids => [1, 2], :issue => {:tracker_id => '2'}
4172 4172 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook'
4173 4173 assert_equal 2, Issue.find(1).tracker_id
4174 4174 assert_equal 2, Issue.find(2).tracker_id
4175 4175 end
4176 4176
4177 4177 def test_bulk_update_status
4178 4178 @request.session[:user_id] = 2
4179 4179 # update issues priority
4180 4180 post :bulk_update, :ids => [1, 2], :notes => 'Bulk editing status',
4181 4181 :issue => {:priority_id => '',
4182 4182 :assigned_to_id => '',
4183 4183 :status_id => '5'}
4184 4184
4185 4185 assert_response 302
4186 4186 issue = Issue.find(1)
4187 4187 assert issue.closed?
4188 4188 end
4189 4189
4190 4190 def test_bulk_update_priority
4191 4191 @request.session[:user_id] = 2
4192 4192 post :bulk_update, :ids => [1, 2], :issue => {:priority_id => 6}
4193 4193
4194 4194 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook'
4195 4195 assert_equal 6, Issue.find(1).priority_id
4196 4196 assert_equal 6, Issue.find(2).priority_id
4197 4197 end
4198 4198
4199 4199 def test_bulk_update_with_notes
4200 4200 @request.session[:user_id] = 2
4201 4201 post :bulk_update, :ids => [1, 2], :notes => 'Moving two issues'
4202 4202
4203 4203 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook'
4204 4204 assert_equal 'Moving two issues', Issue.find(1).journals.sort_by(&:id).last.notes
4205 4205 assert_equal 'Moving two issues', Issue.find(2).journals.sort_by(&:id).last.notes
4206 4206 end
4207 4207
4208 4208 def test_bulk_update_parent_id
4209 4209 IssueRelation.delete_all
4210 4210 @request.session[:user_id] = 2
4211 4211 post :bulk_update, :ids => [1, 3],
4212 4212 :notes => 'Bulk editing parent',
4213 4213 :issue => {:priority_id => '', :assigned_to_id => '',
4214 4214 :status_id => '', :parent_issue_id => '2'}
4215 4215 assert_response 302
4216 4216 parent = Issue.find(2)
4217 4217 assert_equal parent.id, Issue.find(1).parent_id
4218 4218 assert_equal parent.id, Issue.find(3).parent_id
4219 4219 assert_equal [1, 3], parent.children.collect(&:id).sort
4220 4220 end
4221 4221
4222 4222 def test_bulk_update_estimated_hours
4223 4223 @request.session[:user_id] = 2
4224 4224 post :bulk_update, :ids => [1, 2], :issue => {:estimated_hours => 4.25}
4225 4225
4226 4226 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook'
4227 4227 assert_equal 4.25, Issue.find(1).estimated_hours
4228 4228 assert_equal 4.25, Issue.find(2).estimated_hours
4229 4229 end
4230 4230
4231 4231 def test_bulk_update_custom_field
4232 4232 @request.session[:user_id] = 2
4233 4233 # update issues priority
4234 4234 post :bulk_update, :ids => [1, 2], :notes => 'Bulk editing custom field',
4235 4235 :issue => {:priority_id => '',
4236 4236 :assigned_to_id => '',
4237 4237 :custom_field_values => {'2' => '777'}}
4238 4238
4239 4239 assert_response 302
4240 4240
4241 4241 issue = Issue.find(1)
4242 4242 journal = issue.journals.reorder('created_on DESC').first
4243 4243 assert_equal '777', issue.custom_value_for(2).value
4244 4244 assert_equal 1, journal.details.size
4245 4245 assert_equal '125', journal.details.first.old_value
4246 4246 assert_equal '777', journal.details.first.value
4247 4247 end
4248 4248
4249 4249 def test_bulk_update_custom_field_to_blank
4250 4250 @request.session[:user_id] = 2
4251 4251 post :bulk_update, :ids => [1, 3], :notes => 'Bulk editing custom field',
4252 4252 :issue => {:priority_id => '',
4253 4253 :assigned_to_id => '',
4254 4254 :custom_field_values => {'1' => '__none__'}}
4255 4255 assert_response 302
4256 4256 assert_equal '', Issue.find(1).custom_field_value(1)
4257 4257 assert_equal '', Issue.find(3).custom_field_value(1)
4258 4258 end
4259 4259
4260 4260 def test_bulk_update_multi_custom_field
4261 4261 field = CustomField.find(1)
4262 4262 field.update_attribute :multiple, true
4263 4263
4264 4264 @request.session[:user_id] = 2
4265 4265 post :bulk_update, :ids => [1, 2, 3], :notes => 'Bulk editing multi custom field',
4266 4266 :issue => {:priority_id => '',
4267 4267 :assigned_to_id => '',
4268 4268 :custom_field_values => {'1' => ['MySQL', 'Oracle']}}
4269 4269
4270 4270 assert_response 302
4271 4271
4272 4272 assert_equal ['MySQL', 'Oracle'], Issue.find(1).custom_field_value(1).sort
4273 4273 assert_equal ['MySQL', 'Oracle'], Issue.find(3).custom_field_value(1).sort
4274 4274 # the custom field is not associated with the issue tracker
4275 4275 assert_nil Issue.find(2).custom_field_value(1)
4276 4276 end
4277 4277
4278 4278 def test_bulk_update_multi_custom_field_to_blank
4279 4279 field = CustomField.find(1)
4280 4280 field.update_attribute :multiple, true
4281 4281
4282 4282 @request.session[:user_id] = 2
4283 4283 post :bulk_update, :ids => [1, 3], :notes => 'Bulk editing multi custom field',
4284 4284 :issue => {:priority_id => '',
4285 4285 :assigned_to_id => '',
4286 4286 :custom_field_values => {'1' => ['__none__']}}
4287 4287 assert_response 302
4288 4288 assert_equal [''], Issue.find(1).custom_field_value(1)
4289 4289 assert_equal [''], Issue.find(3).custom_field_value(1)
4290 4290 end
4291 4291
4292 4292 def test_bulk_update_unassign
4293 4293 assert_not_nil Issue.find(2).assigned_to
4294 4294 @request.session[:user_id] = 2
4295 4295 # unassign issues
4296 4296 post :bulk_update, :ids => [1, 2], :notes => 'Bulk unassigning', :issue => {:assigned_to_id => 'none'}
4297 4297 assert_response 302
4298 4298 # check that the issues were updated
4299 4299 assert_nil Issue.find(2).assigned_to
4300 4300 end
4301 4301
4302 4302 def test_post_bulk_update_should_allow_fixed_version_to_be_set_to_a_subproject
4303 4303 @request.session[:user_id] = 2
4304 4304
4305 4305 post :bulk_update, :ids => [1,2], :issue => {:fixed_version_id => 4}
4306 4306
4307 4307 assert_response :redirect
4308 4308 issues = Issue.find([1,2])
4309 4309 issues.each do |issue|
4310 4310 assert_equal 4, issue.fixed_version_id
4311 4311 assert_not_equal issue.project_id, issue.fixed_version.project_id
4312 4312 end
4313 4313 end
4314 4314
4315 4315 def test_post_bulk_update_should_redirect_back_using_the_back_url_parameter
4316 4316 @request.session[:user_id] = 2
4317 4317 post :bulk_update, :ids => [1,2], :back_url => '/issues'
4318 4318
4319 4319 assert_response :redirect
4320 4320 assert_redirected_to '/issues'
4321 4321 end
4322 4322
4323 4323 def test_post_bulk_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host
4324 4324 @request.session[:user_id] = 2
4325 4325 post :bulk_update, :ids => [1,2], :back_url => 'http://google.com'
4326 4326
4327 4327 assert_response :redirect
4328 4328 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => Project.find(1).identifier
4329 4329 end
4330 4330
4331 4331 def test_bulk_update_with_all_failures_should_show_errors
4332 4332 @request.session[:user_id] = 2
4333 4333 post :bulk_update, :ids => [1, 2], :issue => {:start_date => 'foo'}
4334 4334 assert_response :success
4335 4335
4336 4336 assert_select '#errorExplanation span', :text => 'Failed to save 2 issue(s) on 2 selected: #1, #2.'
4337 4337 assert_select '#errorExplanation ul li', :text => 'Start date is not a valid date: #1, #2'
4338 4338 end
4339 4339
4340 4340 def test_bulk_update_with_some_failures_should_show_errors
4341 4341 issue1 = Issue.generate!(:start_date => '2013-05-12')
4342 4342 issue2 = Issue.generate!(:start_date => '2013-05-15')
4343 4343 issue3 = Issue.generate!
4344 4344 @request.session[:user_id] = 2
4345 4345 post :bulk_update, :ids => [issue1.id, issue2.id, issue3.id],
4346 4346 :issue => {:due_date => '2013-05-01'}
4347 4347 assert_response :success
4348 4348
4349 4349 assert_select '#errorExplanation span',
4350 4350 :text => "Failed to save 2 issue(s) on 3 selected: ##{issue1.id}, ##{issue2.id}."
4351 4351 assert_select '#errorExplanation ul li',
4352 4352 :text => "Due date must be greater than start date: ##{issue1.id}, ##{issue2.id}"
4353 4353
4354 4354 assert_select '#bulk-selection li', 2
4355 4355 end
4356 4356
4357 4357 def test_bulk_update_with_failure_should_preserved_form_values
4358 4358 @request.session[:user_id] = 2
4359 4359 post :bulk_update, :ids => [1, 2], :issue => {:tracker_id => '2', :start_date => 'foo'}
4360 4360 assert_response :success
4361 4361
4362 4362 assert_select 'select[name=?]', 'issue[tracker_id]' do
4363 4363 assert_select 'option[value="2"][selected=selected]'
4364 4364 end
4365 4365 assert_select 'input[name=?][value=?]', 'issue[start_date]', 'foo'
4366 4366 end
4367 4367
4368 4368 def test_get_bulk_copy
4369 4369 @request.session[:user_id] = 2
4370 4370 get :bulk_edit, :ids => [1, 2, 3], :copy => '1'
4371 4371 assert_response :success
4372 4372
4373 4373 assert_select '#bulk-selection li', 3
4374 4374
4375 4375 assert_select 'select[name=?]', 'issue[project_id]' do
4376 4376 assert_select 'option[value=""]'
4377 4377 end
4378 4378 assert_select 'input[name=copy_attachments]'
4379 4379 end
4380 4380
4381 4381 def test_get_bulk_copy_without_add_issues_permission_should_not_propose_current_project_as_target
4382 4382 user = setup_user_with_copy_but_not_add_permission
4383 4383 @request.session[:user_id] = user.id
4384 4384
4385 4385 get :bulk_edit, :ids => [1, 2, 3], :copy => '1'
4386 4386 assert_response :success
4387 4387
4388 4388 assert_select 'select[name=?]', 'issue[project_id]' do
4389 4389 assert_select 'option[value=""]', 0
4390 4390 assert_select 'option[value="2"]'
4391 4391 end
4392 4392 end
4393 4393
4394 4394 def test_bulk_copy_to_another_project
4395 4395 @request.session[:user_id] = 2
4396 4396 assert_difference 'Issue.count', 2 do
4397 4397 assert_no_difference 'Project.find(1).issues.count' do
4398 4398 post :bulk_update, :ids => [1, 2], :issue => {:project_id => '2'}, :copy => '1'
4399 4399 end
4400 4400 end
4401 4401 assert_redirected_to '/projects/ecookbook/issues'
4402 4402
4403 4403 copies = Issue.order('id DESC').limit(issues.size)
4404 4404 copies.each do |copy|
4405 4405 assert_equal 2, copy.project_id
4406 4406 end
4407 4407 end
4408 4408
4409 4409 def test_bulk_copy_without_add_issues_permission_should_be_allowed_on_project_with_permission
4410 4410 user = setup_user_with_copy_but_not_add_permission
4411 4411 @request.session[:user_id] = user.id
4412 4412
4413 4413 assert_difference 'Issue.count', 3 do
4414 4414 post :bulk_update, :ids => [1, 2, 3], :issue => {:project_id => '2'}, :copy => '1'
4415 4415 assert_response 302
4416 4416 end
4417 4417 end
4418 4418
4419 4419 def test_bulk_copy_on_same_project_without_add_issues_permission_should_be_denied
4420 4420 user = setup_user_with_copy_but_not_add_permission
4421 4421 @request.session[:user_id] = user.id
4422 4422
4423 4423 post :bulk_update, :ids => [1, 2, 3], :issue => {:project_id => ''}, :copy => '1'
4424 4424 assert_response 403
4425 4425 end
4426 4426
4427 4427 def test_bulk_copy_on_different_project_without_add_issues_permission_should_be_denied
4428 4428 user = setup_user_with_copy_but_not_add_permission
4429 4429 @request.session[:user_id] = user.id
4430 4430
4431 4431 post :bulk_update, :ids => [1, 2, 3], :issue => {:project_id => '1'}, :copy => '1'
4432 4432 assert_response 403
4433 4433 end
4434 4434
4435 4435 def test_bulk_copy_should_allow_not_changing_the_issue_attributes
4436 4436 @request.session[:user_id] = 2
4437 4437 issues = [
4438 4438 Issue.create!(:project_id => 1, :tracker_id => 1, :status_id => 1,
4439 4439 :priority_id => 2, :subject => 'issue 1', :author_id => 1,
4440 4440 :assigned_to_id => nil),
4441 4441 Issue.create!(:project_id => 2, :tracker_id => 3, :status_id => 2,
4442 4442 :priority_id => 1, :subject => 'issue 2', :author_id => 2,
4443 4443 :assigned_to_id => 3)
4444 4444 ]
4445 4445 assert_difference 'Issue.count', issues.size do
4446 4446 post :bulk_update, :ids => issues.map(&:id), :copy => '1',
4447 4447 :issue => {
4448 4448 :project_id => '', :tracker_id => '', :assigned_to_id => '',
4449 4449 :status_id => '', :start_date => '', :due_date => ''
4450 4450 }
4451 4451 end
4452 4452
4453 4453 copies = Issue.order('id DESC').limit(issues.size)
4454 4454 issues.each do |orig|
4455 4455 copy = copies.detect {|c| c.subject == orig.subject}
4456 4456 assert_not_nil copy
4457 4457 assert_equal orig.project_id, copy.project_id
4458 4458 assert_equal orig.tracker_id, copy.tracker_id
4459 4459 assert_equal orig.status_id, copy.status_id
4460 4460 assert_equal orig.assigned_to_id, copy.assigned_to_id
4461 4461 assert_equal orig.priority_id, copy.priority_id
4462 4462 end
4463 4463 end
4464 4464
4465 4465 def test_bulk_copy_should_allow_changing_the_issue_attributes
4466 4466 # Fixes random test failure with Mysql
4467 4467 # where Issue.where(:project_id => 2).limit(2).order('id desc')
4468 4468 # doesn't return the expected results
4469 4469 Issue.where("project_id=2").delete_all
4470 4470
4471 4471 @request.session[:user_id] = 2
4472 4472 assert_difference 'Issue.count', 2 do
4473 4473 assert_no_difference 'Project.find(1).issues.count' do
4474 4474 post :bulk_update, :ids => [1, 2], :copy => '1',
4475 4475 :issue => {
4476 4476 :project_id => '2', :tracker_id => '', :assigned_to_id => '2',
4477 4477 :status_id => '1', :start_date => '2009-12-01', :due_date => '2009-12-31'
4478 4478 }
4479 4479 end
4480 4480 end
4481 4481
4482 4482 copied_issues = Issue.where(:project_id => 2).limit(2).order('id desc').to_a
4483 4483 assert_equal 2, copied_issues.size
4484 4484 copied_issues.each do |issue|
4485 4485 assert_equal 2, issue.project_id, "Project is incorrect"
4486 4486 assert_equal 2, issue.assigned_to_id, "Assigned to is incorrect"
4487 4487 assert_equal 1, issue.status_id, "Status is incorrect"
4488 4488 assert_equal '2009-12-01', issue.start_date.to_s, "Start date is incorrect"
4489 4489 assert_equal '2009-12-31', issue.due_date.to_s, "Due date is incorrect"
4490 4490 end
4491 4491 end
4492 4492
4493 4493 def test_bulk_copy_should_allow_adding_a_note
4494 4494 @request.session[:user_id] = 2
4495 4495 assert_difference 'Issue.count', 1 do
4496 4496 post :bulk_update, :ids => [1], :copy => '1',
4497 4497 :notes => 'Copying one issue',
4498 4498 :issue => {
4499 4499 :project_id => '', :tracker_id => '', :assigned_to_id => '4',
4500 4500 :status_id => '3', :start_date => '2009-12-01', :due_date => '2009-12-31'
4501 4501 }
4502 4502 end
4503 4503 issue = Issue.order('id DESC').first
4504 4504 assert_equal 1, issue.journals.size
4505 4505 journal = issue.journals.first
4506 4506 assert_equal 'Copying one issue', journal.notes
4507 4507 end
4508 4508
4509 4509 def test_bulk_copy_should_allow_not_copying_the_attachments
4510 4510 attachment_count = Issue.find(3).attachments.size
4511 4511 assert attachment_count > 0
4512 4512 @request.session[:user_id] = 2
4513 4513
4514 4514 assert_difference 'Issue.count', 1 do
4515 4515 assert_no_difference 'Attachment.count' do
4516 4516 post :bulk_update, :ids => [3], :copy => '1', :copy_attachments => '0',
4517 4517 :issue => {
4518 4518 :project_id => ''
4519 4519 }
4520 4520 end
4521 4521 end
4522 4522 end
4523 4523
4524 4524 def test_bulk_copy_should_allow_copying_the_attachments
4525 4525 attachment_count = Issue.find(3).attachments.size
4526 4526 assert attachment_count > 0
4527 4527 @request.session[:user_id] = 2
4528 4528
4529 4529 assert_difference 'Issue.count', 1 do
4530 4530 assert_difference 'Attachment.count', attachment_count do
4531 4531 post :bulk_update, :ids => [3], :copy => '1', :copy_attachments => '1',
4532 4532 :issue => {
4533 4533 :project_id => ''
4534 4534 }
4535 4535 end
4536 4536 end
4537 4537 end
4538 4538
4539 4539 def test_bulk_copy_should_add_relations_with_copied_issues
4540 4540 @request.session[:user_id] = 2
4541 4541
4542 4542 assert_difference 'Issue.count', 2 do
4543 4543 assert_difference 'IssueRelation.count', 2 do
4544 4544 post :bulk_update, :ids => [1, 3], :copy => '1', :link_copy => '1',
4545 4545 :issue => {
4546 4546 :project_id => '1'
4547 4547 }
4548 4548 end
4549 4549 end
4550 4550 end
4551 4551
4552 4552 def test_bulk_copy_should_allow_not_copying_the_subtasks
4553 4553 issue = Issue.generate_with_descendants!
4554 4554 @request.session[:user_id] = 2
4555 4555
4556 4556 assert_difference 'Issue.count', 1 do
4557 4557 post :bulk_update, :ids => [issue.id], :copy => '1', :copy_subtasks => '0',
4558 4558 :issue => {
4559 4559 :project_id => ''
4560 4560 }
4561 4561 end
4562 4562 end
4563 4563
4564 4564 def test_bulk_copy_should_allow_copying_the_subtasks
4565 4565 issue = Issue.generate_with_descendants!
4566 4566 count = issue.descendants.count
4567 4567 @request.session[:user_id] = 2
4568 4568
4569 4569 assert_difference 'Issue.count', count+1 do
4570 4570 post :bulk_update, :ids => [issue.id], :copy => '1', :copy_subtasks => '1',
4571 4571 :issue => {
4572 4572 :project_id => ''
4573 4573 }
4574 4574 end
4575 4575 copy = Issue.where(:parent_id => nil).order("id DESC").first
4576 4576 assert_equal count, copy.descendants.count
4577 4577 end
4578 4578
4579 4579 def test_bulk_copy_should_not_copy_selected_subtasks_twice
4580 4580 issue = Issue.generate_with_descendants!
4581 4581 count = issue.descendants.count
4582 4582 @request.session[:user_id] = 2
4583 4583
4584 4584 assert_difference 'Issue.count', count+1 do
4585 4585 post :bulk_update, :ids => issue.self_and_descendants.map(&:id), :copy => '1', :copy_subtasks => '1',
4586 4586 :issue => {
4587 4587 :project_id => ''
4588 4588 }
4589 4589 end
4590 4590 copy = Issue.where(:parent_id => nil).order("id DESC").first
4591 4591 assert_equal count, copy.descendants.count
4592 4592 end
4593 4593
4594 4594 def test_bulk_copy_to_another_project_should_follow_when_needed
4595 4595 @request.session[:user_id] = 2
4596 4596 post :bulk_update, :ids => [1], :copy => '1', :issue => {:project_id => 2}, :follow => '1'
4597 4597 issue = Issue.order('id DESC').first
4598 4598 assert_redirected_to :controller => 'issues', :action => 'show', :id => issue
4599 4599 end
4600 4600
4601 4601 def test_bulk_copy_with_all_failures_should_display_errors
4602 4602 @request.session[:user_id] = 2
4603 4603 post :bulk_update, :ids => [1, 2], :copy => '1', :issue => {:start_date => 'foo'}
4604 4604
4605 4605 assert_response :success
4606 4606 end
4607 4607
4608 4608 def test_destroy_issue_with_no_time_entries
4609 4609 assert_nil TimeEntry.find_by_issue_id(2)
4610 4610 @request.session[:user_id] = 2
4611 4611
4612 4612 assert_difference 'Issue.count', -1 do
4613 4613 delete :destroy, :id => 2
4614 4614 end
4615 4615 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
4616 4616 assert_nil Issue.find_by_id(2)
4617 4617 end
4618 4618
4619 4619 def test_destroy_issues_with_time_entries
4620 4620 @request.session[:user_id] = 2
4621 4621
4622 4622 assert_no_difference 'Issue.count' do
4623 4623 delete :destroy, :ids => [1, 3]
4624 4624 end
4625 4625 assert_response :success
4626 4626
4627 4627 assert_select 'form' do
4628 4628 assert_select 'input[name=_method][value=delete]'
4629 4629 end
4630 4630 end
4631 4631
4632 4632 def test_destroy_issues_and_destroy_time_entries
4633 4633 @request.session[:user_id] = 2
4634 4634
4635 4635 assert_difference 'Issue.count', -2 do
4636 4636 assert_difference 'TimeEntry.count', -3 do
4637 4637 delete :destroy, :ids => [1, 3], :todo => 'destroy'
4638 4638 end
4639 4639 end
4640 4640 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
4641 4641 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
4642 4642 assert_nil TimeEntry.find_by_id([1, 2])
4643 4643 end
4644 4644
4645 4645 def test_destroy_issues_and_assign_time_entries_to_project
4646 4646 @request.session[:user_id] = 2
4647 4647
4648 4648 assert_difference 'Issue.count', -2 do
4649 4649 assert_no_difference 'TimeEntry.count' do
4650 4650 delete :destroy, :ids => [1, 3], :todo => 'nullify'
4651 4651 end
4652 4652 end
4653 4653 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
4654 4654 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
4655 4655 assert_nil TimeEntry.find(1).issue_id
4656 4656 assert_nil TimeEntry.find(2).issue_id
4657 4657 end
4658 4658
4659 4659 def test_destroy_issues_and_reassign_time_entries_to_another_issue
4660 4660 @request.session[:user_id] = 2
4661 4661
4662 4662 assert_difference 'Issue.count', -2 do
4663 4663 assert_no_difference 'TimeEntry.count' do
4664 4664 delete :destroy, :ids => [1, 3], :todo => 'reassign', :reassign_to_id => 2
4665 4665 end
4666 4666 end
4667 4667 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
4668 4668 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
4669 4669 assert_equal 2, TimeEntry.find(1).issue_id
4670 4670 assert_equal 2, TimeEntry.find(2).issue_id
4671 4671 end
4672 4672
4673 4673 def test_destroy_issues_and_reassign_time_entries_to_an_invalid_issue_should_fail
4674 4674 @request.session[:user_id] = 2
4675 4675
4676 4676 assert_no_difference 'Issue.count' do
4677 4677 assert_no_difference 'TimeEntry.count' do
4678 4678 # try to reassign time to an issue of another project
4679 4679 delete :destroy, :ids => [1, 3], :todo => 'reassign', :reassign_to_id => 4
4680 4680 end
4681 4681 end
4682 4682 assert_response :success
4683 4683 end
4684 4684
4685 4685 def test_destroy_issues_from_different_projects
4686 4686 @request.session[:user_id] = 2
4687 4687
4688 4688 assert_difference 'Issue.count', -3 do
4689 4689 delete :destroy, :ids => [1, 2, 6], :todo => 'destroy'
4690 4690 end
4691 4691 assert_redirected_to :controller => 'issues', :action => 'index'
4692 4692 assert !(Issue.find_by_id(1) || Issue.find_by_id(2) || Issue.find_by_id(6))
4693 4693 end
4694 4694
4695 4695 def test_destroy_parent_and_child_issues
4696 4696 parent = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'Parent Issue')
4697 4697 child = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'Child Issue', :parent_issue_id => parent.id)
4698 4698 assert child.is_descendant_of?(parent.reload)
4699 4699
4700 4700 @request.session[:user_id] = 2
4701 4701 assert_difference 'Issue.count', -2 do
4702 4702 delete :destroy, :ids => [parent.id, child.id], :todo => 'destroy'
4703 4703 end
4704 4704 assert_response 302
4705 4705 end
4706 4706
4707 4707 def test_destroy_invalid_should_respond_with_404
4708 4708 @request.session[:user_id] = 2
4709 4709 assert_no_difference 'Issue.count' do
4710 4710 delete :destroy, :id => 999
4711 4711 end
4712 4712 assert_response 404
4713 4713 end
4714 4714
4715 4715 def test_destroy_with_permission_on_tracker_should_be_allowed
4716 4716 role = Role.find(1)
4717 4717 role.set_permission_trackers :delete_issues, [1]
4718 4718 role.save!
4719 4719 issue = Issue.generate!(:project_id => 1, :tracker_id => 1)
4720 4720
4721 4721 @request.session[:user_id] = 2
4722 4722 assert_difference 'Issue.count', -1 do
4723 4723 delete :destroy, :id => issue.id
4724 4724 end
4725 4725 assert_response 302
4726 4726 end
4727 4727
4728 4728 def test_destroy_without_permission_on_tracker_should_be_denied
4729 4729 role = Role.find(1)
4730 4730 role.set_permission_trackers :delete_issues, [2]
4731 4731 role.save!
4732 4732 issue = Issue.generate!(:project_id => 1, :tracker_id => 1)
4733 4733
4734 4734 @request.session[:user_id] = 2
4735 4735 assert_no_difference 'Issue.count' do
4736 4736 delete :destroy, :id => issue.id
4737 4737 end
4738 4738 assert_response 403
4739 4739 end
4740 4740
4741 4741 def test_default_search_scope
4742 4742 get :index
4743 4743
4744 4744 assert_select 'div#quick-search form' do
4745 4745 assert_select 'input[name=issues][value="1"][type=hidden]'
4746 4746 end
4747 4747 end
4748 4748
4749 4749 def setup_user_with_copy_but_not_add_permission
4750 4750 Role.all.each {|r| r.remove_permission! :add_issues}
4751 4751 Role.find_by_name('Manager').add_permission! :add_issues
4752 4752 user = User.generate!
4753 4753 User.add_to_project(user, Project.find(1), Role.find_by_name('Developer'))
4754 4754 User.add_to_project(user, Project.find(2), Role.find_by_name('Manager'))
4755 4755 user
4756 4756 end
4757 4757 end
General Comments 0
You need to be logged in to leave comments. Login now