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