comparison test/functional/.svn/text-base/issues_controller_test.rb.svn-base @ 0:513646585e45

* Import Redmine trunk SVN rev 3859
author Chris Cannam
date Fri, 23 Jul 2010 15:52:44 +0100
parents
children 1d32c0a0efbf
comparison
equal deleted inserted replaced
-1:000000000000 0:513646585e45
1 # Redmine - project management software
2 # Copyright (C) 2006-2008 Jean-Philippe Lang
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 require File.dirname(__FILE__) + '/../test_helper'
19 require 'issues_controller'
20
21 # Re-raise errors caught by the controller.
22 class IssuesController; def rescue_action(e) raise e end; end
23
24 class IssuesControllerTest < ActionController::TestCase
25 fixtures :projects,
26 :users,
27 :roles,
28 :members,
29 :member_roles,
30 :issues,
31 :issue_statuses,
32 :versions,
33 :trackers,
34 :projects_trackers,
35 :issue_categories,
36 :enabled_modules,
37 :enumerations,
38 :attachments,
39 :workflows,
40 :custom_fields,
41 :custom_values,
42 :custom_fields_projects,
43 :custom_fields_trackers,
44 :time_entries,
45 :journals,
46 :journal_details,
47 :queries
48
49 def setup
50 @controller = IssuesController.new
51 @request = ActionController::TestRequest.new
52 @response = ActionController::TestResponse.new
53 User.current = nil
54 end
55
56 def test_index
57 Setting.default_language = 'en'
58
59 get :index
60 assert_response :success
61 assert_template 'index.rhtml'
62 assert_not_nil assigns(:issues)
63 assert_nil assigns(:project)
64 assert_tag :tag => 'a', :content => /Can't print recipes/
65 assert_tag :tag => 'a', :content => /Subproject issue/
66 # private projects hidden
67 assert_no_tag :tag => 'a', :content => /Issue of a private subproject/
68 assert_no_tag :tag => 'a', :content => /Issue on project 2/
69 # project column
70 assert_tag :tag => 'th', :content => /Project/
71 end
72
73 def test_index_should_not_list_issues_when_module_disabled
74 EnabledModule.delete_all("name = 'issue_tracking' AND project_id = 1")
75 get :index
76 assert_response :success
77 assert_template 'index.rhtml'
78 assert_not_nil assigns(:issues)
79 assert_nil assigns(:project)
80 assert_no_tag :tag => 'a', :content => /Can't print recipes/
81 assert_tag :tag => 'a', :content => /Subproject issue/
82 end
83
84 def test_index_should_not_list_issues_when_module_disabled
85 EnabledModule.delete_all("name = 'issue_tracking' AND project_id = 1")
86 get :index
87 assert_response :success
88 assert_template 'index.rhtml'
89 assert_not_nil assigns(:issues)
90 assert_nil assigns(:project)
91 assert_no_tag :tag => 'a', :content => /Can't print recipes/
92 assert_tag :tag => 'a', :content => /Subproject issue/
93 end
94
95 def test_index_with_project
96 Setting.display_subprojects_issues = 0
97 get :index, :project_id => 1
98 assert_response :success
99 assert_template 'index.rhtml'
100 assert_not_nil assigns(:issues)
101 assert_tag :tag => 'a', :content => /Can't print recipes/
102 assert_no_tag :tag => 'a', :content => /Subproject issue/
103 end
104
105 def test_index_with_project_and_subprojects
106 Setting.display_subprojects_issues = 1
107 get :index, :project_id => 1
108 assert_response :success
109 assert_template 'index.rhtml'
110 assert_not_nil assigns(:issues)
111 assert_tag :tag => 'a', :content => /Can't print recipes/
112 assert_tag :tag => 'a', :content => /Subproject issue/
113 assert_no_tag :tag => 'a', :content => /Issue of a private subproject/
114 end
115
116 def test_index_with_project_and_subprojects_should_show_private_subprojects
117 @request.session[:user_id] = 2
118 Setting.display_subprojects_issues = 1
119 get :index, :project_id => 1
120 assert_response :success
121 assert_template 'index.rhtml'
122 assert_not_nil assigns(:issues)
123 assert_tag :tag => 'a', :content => /Can't print recipes/
124 assert_tag :tag => 'a', :content => /Subproject issue/
125 assert_tag :tag => 'a', :content => /Issue of a private subproject/
126 end
127
128 def test_index_with_project_and_filter
129 get :index, :project_id => 1, :set_filter => 1
130 assert_response :success
131 assert_template 'index.rhtml'
132 assert_not_nil assigns(:issues)
133 end
134
135 def test_index_with_query
136 get :index, :project_id => 1, :query_id => 5
137 assert_response :success
138 assert_template 'index.rhtml'
139 assert_not_nil assigns(:issues)
140 assert_nil assigns(:issue_count_by_group)
141 end
142
143 def test_index_with_query_grouped_by_tracker
144 get :index, :project_id => 1, :query_id => 6
145 assert_response :success
146 assert_template 'index.rhtml'
147 assert_not_nil assigns(:issues)
148 assert_not_nil assigns(:issue_count_by_group)
149 end
150
151 def test_index_with_query_grouped_by_list_custom_field
152 get :index, :project_id => 1, :query_id => 9
153 assert_response :success
154 assert_template 'index.rhtml'
155 assert_not_nil assigns(:issues)
156 assert_not_nil assigns(:issue_count_by_group)
157 end
158
159 def test_index_sort_by_field_not_included_in_columns
160 Setting.issue_list_default_columns = %w(subject author)
161 get :index, :sort => 'tracker'
162 end
163
164 def test_index_csv_with_project
165 Setting.default_language = 'en'
166
167 get :index, :format => 'csv'
168 assert_response :success
169 assert_not_nil assigns(:issues)
170 assert_equal 'text/csv', @response.content_type
171 assert @response.body.starts_with?("#,")
172
173 get :index, :project_id => 1, :format => 'csv'
174 assert_response :success
175 assert_not_nil assigns(:issues)
176 assert_equal 'text/csv', @response.content_type
177 end
178
179 def test_index_pdf
180 get :index, :format => 'pdf'
181 assert_response :success
182 assert_not_nil assigns(:issues)
183 assert_equal 'application/pdf', @response.content_type
184
185 get :index, :project_id => 1, :format => 'pdf'
186 assert_response :success
187 assert_not_nil assigns(:issues)
188 assert_equal 'application/pdf', @response.content_type
189
190 get :index, :project_id => 1, :query_id => 6, :format => 'pdf'
191 assert_response :success
192 assert_not_nil assigns(:issues)
193 assert_equal 'application/pdf', @response.content_type
194 end
195
196 def test_index_pdf_with_query_grouped_by_list_custom_field
197 get :index, :project_id => 1, :query_id => 9, :format => 'pdf'
198 assert_response :success
199 assert_not_nil assigns(:issues)
200 assert_not_nil assigns(:issue_count_by_group)
201 assert_equal 'application/pdf', @response.content_type
202 end
203
204 def test_index_sort
205 get :index, :sort => 'tracker,id:desc'
206 assert_response :success
207
208 sort_params = @request.session['issues_index_sort']
209 assert sort_params.is_a?(String)
210 assert_equal 'tracker,id:desc', sort_params
211
212 issues = assigns(:issues)
213 assert_not_nil issues
214 assert !issues.empty?
215 assert_equal issues.sort {|a,b| a.tracker == b.tracker ? b.id <=> a.id : a.tracker <=> b.tracker }.collect(&:id), issues.collect(&:id)
216 end
217
218 def test_index_with_columns
219 columns = ['tracker', 'subject', 'assigned_to']
220 get :index, :set_filter => 1, :query => { 'column_names' => columns}
221 assert_response :success
222
223 # query should use specified columns
224 query = assigns(:query)
225 assert_kind_of Query, query
226 assert_equal columns, query.column_names.map(&:to_s)
227
228 # columns should be stored in session
229 assert_kind_of Hash, session[:query]
230 assert_kind_of Array, session[:query][:column_names]
231 assert_equal columns, session[:query][:column_names].map(&:to_s)
232 end
233
234 def test_changes
235 get :changes, :project_id => 1
236 assert_response :success
237 assert_not_nil assigns(:journals)
238 assert_equal 'application/atom+xml', @response.content_type
239 end
240
241 def test_show_by_anonymous
242 get :show, :id => 1
243 assert_response :success
244 assert_template 'show.rhtml'
245 assert_not_nil assigns(:issue)
246 assert_equal Issue.find(1), assigns(:issue)
247
248 # anonymous role is allowed to add a note
249 assert_tag :tag => 'form',
250 :descendant => { :tag => 'fieldset',
251 :child => { :tag => 'legend',
252 :content => /Notes/ } }
253 end
254
255 def test_show_by_manager
256 @request.session[:user_id] = 2
257 get :show, :id => 1
258 assert_response :success
259
260 assert_tag :tag => 'form',
261 :descendant => { :tag => 'fieldset',
262 :child => { :tag => 'legend',
263 :content => /Change properties/ } },
264 :descendant => { :tag => 'fieldset',
265 :child => { :tag => 'legend',
266 :content => /Log time/ } },
267 :descendant => { :tag => 'fieldset',
268 :child => { :tag => 'legend',
269 :content => /Notes/ } }
270 end
271
272 def test_show_should_deny_anonymous_access_without_permission
273 Role.anonymous.remove_permission!(:view_issues)
274 get :show, :id => 1
275 assert_response :redirect
276 end
277
278 def test_show_should_deny_non_member_access_without_permission
279 Role.non_member.remove_permission!(:view_issues)
280 @request.session[:user_id] = 9
281 get :show, :id => 1
282 assert_response 403
283 end
284
285 def test_show_should_deny_member_access_without_permission
286 Role.find(1).remove_permission!(:view_issues)
287 @request.session[:user_id] = 2
288 get :show, :id => 1
289 assert_response 403
290 end
291
292 def test_show_should_not_disclose_relations_to_invisible_issues
293 Setting.cross_project_issue_relations = '1'
294 IssueRelation.create!(:issue_from => Issue.find(1), :issue_to => Issue.find(2), :relation_type => 'relates')
295 # Relation to a private project issue
296 IssueRelation.create!(:issue_from => Issue.find(1), :issue_to => Issue.find(4), :relation_type => 'relates')
297
298 get :show, :id => 1
299 assert_response :success
300
301 assert_tag :div, :attributes => { :id => 'relations' },
302 :descendant => { :tag => 'a', :content => /#2$/ }
303 assert_no_tag :div, :attributes => { :id => 'relations' },
304 :descendant => { :tag => 'a', :content => /#4$/ }
305 end
306
307 def test_show_atom
308 get :show, :id => 2, :format => 'atom'
309 assert_response :success
310 assert_template 'changes.rxml'
311 # Inline image
312 assert_select 'content', :text => Regexp.new(Regexp.quote('http://test.host/attachments/download/10'))
313 end
314
315 def test_show_export_to_pdf
316 get :show, :id => 3, :format => 'pdf'
317 assert_response :success
318 assert_equal 'application/pdf', @response.content_type
319 assert @response.body.starts_with?('%PDF')
320 assert_not_nil assigns(:issue)
321 end
322
323 def test_get_new
324 @request.session[:user_id] = 2
325 get :new, :project_id => 1, :tracker_id => 1
326 assert_response :success
327 assert_template 'new'
328
329 assert_tag :tag => 'input', :attributes => { :name => 'issue[custom_field_values][2]',
330 :value => 'Default string' }
331 end
332
333 def test_get_new_without_tracker_id
334 @request.session[:user_id] = 2
335 get :new, :project_id => 1
336 assert_response :success
337 assert_template 'new'
338
339 issue = assigns(:issue)
340 assert_not_nil issue
341 assert_equal Project.find(1).trackers.first, issue.tracker
342 end
343
344 def test_get_new_with_no_default_status_should_display_an_error
345 @request.session[:user_id] = 2
346 IssueStatus.delete_all
347
348 get :new, :project_id => 1
349 assert_response 500
350 assert_not_nil flash[:error]
351 assert_tag :tag => 'div', :attributes => { :class => /error/ },
352 :content => /No default issue/
353 end
354
355 def test_get_new_with_no_tracker_should_display_an_error
356 @request.session[:user_id] = 2
357 Tracker.delete_all
358
359 get :new, :project_id => 1
360 assert_response 500
361 assert_not_nil flash[:error]
362 assert_tag :tag => 'div', :attributes => { :class => /error/ },
363 :content => /No tracker/
364 end
365
366 def test_update_new_form
367 @request.session[:user_id] = 2
368 xhr :post, :update_form, :project_id => 1,
369 :issue => {:tracker_id => 2,
370 :subject => 'This is the test_new issue',
371 :description => 'This is the description',
372 :priority_id => 5}
373 assert_response :success
374 assert_template 'attributes'
375
376 issue = assigns(:issue)
377 assert_kind_of Issue, issue
378 assert_equal 1, issue.project_id
379 assert_equal 2, issue.tracker_id
380 assert_equal 'This is the test_new issue', issue.subject
381 end
382
383 def test_post_create
384 @request.session[:user_id] = 2
385 assert_difference 'Issue.count' do
386 post :create, :project_id => 1,
387 :issue => {:tracker_id => 3,
388 :status_id => 2,
389 :subject => 'This is the test_new issue',
390 :description => 'This is the description',
391 :priority_id => 5,
392 :estimated_hours => '',
393 :custom_field_values => {'2' => 'Value for field 2'}}
394 end
395 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
396
397 issue = Issue.find_by_subject('This is the test_new issue')
398 assert_not_nil issue
399 assert_equal 2, issue.author_id
400 assert_equal 3, issue.tracker_id
401 assert_equal 2, issue.status_id
402 assert_nil issue.estimated_hours
403 v = issue.custom_values.find(:first, :conditions => {:custom_field_id => 2})
404 assert_not_nil v
405 assert_equal 'Value for field 2', v.value
406 end
407
408 def test_post_create_and_continue
409 @request.session[:user_id] = 2
410 post :create, :project_id => 1,
411 :issue => {:tracker_id => 3,
412 :subject => 'This is first issue',
413 :priority_id => 5},
414 :continue => ''
415 assert_redirected_to :controller => 'issues', :action => 'new', :issue => {:tracker_id => 3}
416 end
417
418 def test_post_create_without_custom_fields_param
419 @request.session[:user_id] = 2
420 assert_difference 'Issue.count' do
421 post :create, :project_id => 1,
422 :issue => {:tracker_id => 1,
423 :subject => 'This is the test_new issue',
424 :description => 'This is the description',
425 :priority_id => 5}
426 end
427 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
428 end
429
430 def test_post_create_with_required_custom_field_and_without_custom_fields_param
431 field = IssueCustomField.find_by_name('Database')
432 field.update_attribute(:is_required, true)
433
434 @request.session[:user_id] = 2
435 post :create, :project_id => 1,
436 :issue => {:tracker_id => 1,
437 :subject => 'This is the test_new issue',
438 :description => 'This is the description',
439 :priority_id => 5}
440 assert_response :success
441 assert_template 'new'
442 issue = assigns(:issue)
443 assert_not_nil issue
444 assert_equal I18n.translate('activerecord.errors.messages.invalid'), issue.errors.on(:custom_values)
445 end
446
447 def test_post_create_with_watchers
448 @request.session[:user_id] = 2
449 ActionMailer::Base.deliveries.clear
450
451 assert_difference 'Watcher.count', 2 do
452 post :create, :project_id => 1,
453 :issue => {:tracker_id => 1,
454 :subject => 'This is a new issue with watchers',
455 :description => 'This is the description',
456 :priority_id => 5,
457 :watcher_user_ids => ['2', '3']}
458 end
459 issue = Issue.find_by_subject('This is a new issue with watchers')
460 assert_not_nil issue
461 assert_redirected_to :controller => 'issues', :action => 'show', :id => issue
462
463 # Watchers added
464 assert_equal [2, 3], issue.watcher_user_ids.sort
465 assert issue.watched_by?(User.find(3))
466 # Watchers notified
467 mail = ActionMailer::Base.deliveries.last
468 assert_kind_of TMail::Mail, mail
469 assert [mail.bcc, mail.cc].flatten.include?(User.find(3).mail)
470 end
471
472 def test_post_create_subissue
473 @request.session[:user_id] = 2
474
475 assert_difference 'Issue.count' do
476 post :create, :project_id => 1,
477 :issue => {:tracker_id => 1,
478 :subject => 'This is a child issue',
479 :parent_issue_id => 2}
480 end
481 issue = Issue.find_by_subject('This is a child issue')
482 assert_not_nil issue
483 assert_equal Issue.find(2), issue.parent
484 end
485
486 def test_post_create_should_send_a_notification
487 ActionMailer::Base.deliveries.clear
488 @request.session[:user_id] = 2
489 assert_difference 'Issue.count' do
490 post :create, :project_id => 1,
491 :issue => {:tracker_id => 3,
492 :subject => 'This is the test_new issue',
493 :description => 'This is the description',
494 :priority_id => 5,
495 :estimated_hours => '',
496 :custom_field_values => {'2' => 'Value for field 2'}}
497 end
498 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
499
500 assert_equal 1, ActionMailer::Base.deliveries.size
501 end
502
503 def test_post_create_should_preserve_fields_values_on_validation_failure
504 @request.session[:user_id] = 2
505 post :create, :project_id => 1,
506 :issue => {:tracker_id => 1,
507 # empty subject
508 :subject => '',
509 :description => 'This is a description',
510 :priority_id => 6,
511 :custom_field_values => {'1' => 'Oracle', '2' => 'Value for field 2'}}
512 assert_response :success
513 assert_template 'new'
514
515 assert_tag :textarea, :attributes => { :name => 'issue[description]' },
516 :content => 'This is a description'
517 assert_tag :select, :attributes => { :name => 'issue[priority_id]' },
518 :child => { :tag => 'option', :attributes => { :selected => 'selected',
519 :value => '6' },
520 :content => 'High' }
521 # Custom fields
522 assert_tag :select, :attributes => { :name => 'issue[custom_field_values][1]' },
523 :child => { :tag => 'option', :attributes => { :selected => 'selected',
524 :value => 'Oracle' },
525 :content => 'Oracle' }
526 assert_tag :input, :attributes => { :name => 'issue[custom_field_values][2]',
527 :value => 'Value for field 2'}
528 end
529
530 def test_post_create_should_ignore_non_safe_attributes
531 @request.session[:user_id] = 2
532 assert_nothing_raised do
533 post :create, :project_id => 1, :issue => { :tracker => "A param can not be a Tracker" }
534 end
535 end
536
537 context "without workflow privilege" do
538 setup do
539 Workflow.delete_all(["role_id = ?", Role.anonymous.id])
540 Role.anonymous.add_permission! :add_issues
541 end
542
543 context "#new" do
544 should "propose default status only" do
545 get :new, :project_id => 1
546 assert_response :success
547 assert_template 'new'
548 assert_tag :tag => 'select',
549 :attributes => {:name => 'issue[status_id]'},
550 :children => {:count => 1},
551 :child => {:tag => 'option', :attributes => {:value => IssueStatus.default.id.to_s}}
552 end
553
554 should "accept default status" do
555 assert_difference 'Issue.count' do
556 post :create, :project_id => 1,
557 :issue => {:tracker_id => 1,
558 :subject => 'This is an issue',
559 :status_id => 1}
560 end
561 issue = Issue.last(:order => 'id')
562 assert_equal IssueStatus.default, issue.status
563 end
564
565 should "ignore unauthorized status" do
566 assert_difference 'Issue.count' do
567 post :create, :project_id => 1,
568 :issue => {:tracker_id => 1,
569 :subject => 'This is an issue',
570 :status_id => 3}
571 end
572 issue = Issue.last(:order => 'id')
573 assert_equal IssueStatus.default, issue.status
574 end
575 end
576 end
577
578 def test_copy_issue
579 @request.session[:user_id] = 2
580 get :new, :project_id => 1, :copy_from => 1
581 assert_template 'new'
582 assert_not_nil assigns(:issue)
583 orig = Issue.find(1)
584 assert_equal orig.subject, assigns(:issue).subject
585 end
586
587 def test_get_edit
588 @request.session[:user_id] = 2
589 get :edit, :id => 1
590 assert_response :success
591 assert_template 'edit'
592 assert_not_nil assigns(:issue)
593 assert_equal Issue.find(1), assigns(:issue)
594 end
595
596 def test_get_edit_with_params
597 @request.session[:user_id] = 2
598 get :edit, :id => 1, :issue => { :status_id => 5, :priority_id => 7 }
599 assert_response :success
600 assert_template 'edit'
601
602 issue = assigns(:issue)
603 assert_not_nil issue
604
605 assert_equal 5, issue.status_id
606 assert_tag :select, :attributes => { :name => 'issue[status_id]' },
607 :child => { :tag => 'option',
608 :content => 'Closed',
609 :attributes => { :selected => 'selected' } }
610
611 assert_equal 7, issue.priority_id
612 assert_tag :select, :attributes => { :name => 'issue[priority_id]' },
613 :child => { :tag => 'option',
614 :content => 'Urgent',
615 :attributes => { :selected => 'selected' } }
616 end
617
618 def test_update_edit_form
619 @request.session[:user_id] = 2
620 xhr :post, :update_form, :project_id => 1,
621 :id => 1,
622 :issue => {:tracker_id => 2,
623 :subject => 'This is the test_new issue',
624 :description => 'This is the description',
625 :priority_id => 5}
626 assert_response :success
627 assert_template 'attributes'
628
629 issue = assigns(:issue)
630 assert_kind_of Issue, issue
631 assert_equal 1, issue.id
632 assert_equal 1, issue.project_id
633 assert_equal 2, issue.tracker_id
634 assert_equal 'This is the test_new issue', issue.subject
635 end
636
637 def test_reply_to_issue
638 @request.session[:user_id] = 2
639 get :reply, :id => 1
640 assert_response :success
641 assert_select_rjs :show, "update"
642 end
643
644 def test_reply_to_note
645 @request.session[:user_id] = 2
646 get :reply, :id => 1, :journal_id => 2
647 assert_response :success
648 assert_select_rjs :show, "update"
649 end
650
651 def test_update_using_invalid_http_verbs
652 @request.session[:user_id] = 2
653 subject = 'Updated by an invalid http verb'
654
655 get :update, :id => 1, :issue => {:subject => subject}
656 assert_not_equal subject, Issue.find(1).subject
657
658 post :update, :id => 1, :issue => {:subject => subject}
659 assert_not_equal subject, Issue.find(1).subject
660
661 delete :update, :id => 1, :issue => {:subject => subject}
662 assert_not_equal subject, Issue.find(1).subject
663 end
664
665 def test_put_update_without_custom_fields_param
666 @request.session[:user_id] = 2
667 ActionMailer::Base.deliveries.clear
668
669 issue = Issue.find(1)
670 assert_equal '125', issue.custom_value_for(2).value
671 old_subject = issue.subject
672 new_subject = 'Subject modified by IssuesControllerTest#test_post_edit'
673
674 assert_difference('Journal.count') do
675 assert_difference('JournalDetail.count', 2) do
676 put :update, :id => 1, :issue => {:subject => new_subject,
677 :priority_id => '6',
678 :category_id => '1' # no change
679 }
680 end
681 end
682 assert_redirected_to :action => 'show', :id => '1'
683 issue.reload
684 assert_equal new_subject, issue.subject
685 # Make sure custom fields were not cleared
686 assert_equal '125', issue.custom_value_for(2).value
687
688 mail = ActionMailer::Base.deliveries.last
689 assert_kind_of TMail::Mail, mail
690 assert mail.subject.starts_with?("[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}]")
691 assert mail.body.include?("Subject changed from #{old_subject} to #{new_subject}")
692 end
693
694 def test_put_update_with_custom_field_change
695 @request.session[:user_id] = 2
696 issue = Issue.find(1)
697 assert_equal '125', issue.custom_value_for(2).value
698
699 assert_difference('Journal.count') do
700 assert_difference('JournalDetail.count', 3) do
701 put :update, :id => 1, :issue => {:subject => 'Custom field change',
702 :priority_id => '6',
703 :category_id => '1', # no change
704 :custom_field_values => { '2' => 'New custom value' }
705 }
706 end
707 end
708 assert_redirected_to :action => 'show', :id => '1'
709 issue.reload
710 assert_equal 'New custom value', issue.custom_value_for(2).value
711
712 mail = ActionMailer::Base.deliveries.last
713 assert_kind_of TMail::Mail, mail
714 assert mail.body.include?("Searchable field changed from 125 to New custom value")
715 end
716
717 def test_put_update_with_status_and_assignee_change
718 issue = Issue.find(1)
719 assert_equal 1, issue.status_id
720 @request.session[:user_id] = 2
721 assert_difference('TimeEntry.count', 0) do
722 put :update,
723 :id => 1,
724 :issue => { :status_id => 2, :assigned_to_id => 3 },
725 :notes => 'Assigned to dlopper',
726 :time_entry => { :hours => '', :comments => '', :activity_id => TimeEntryActivity.first }
727 end
728 assert_redirected_to :action => 'show', :id => '1'
729 issue.reload
730 assert_equal 2, issue.status_id
731 j = Journal.find(:first, :order => 'id DESC')
732 assert_equal 'Assigned to dlopper', j.notes
733 assert_equal 2, j.details.size
734
735 mail = ActionMailer::Base.deliveries.last
736 assert mail.body.include?("Status changed from New to Assigned")
737 # subject should contain the new status
738 assert mail.subject.include?("(#{ IssueStatus.find(2).name })")
739 end
740
741 def test_put_update_with_note_only
742 notes = 'Note added by IssuesControllerTest#test_update_with_note_only'
743 # anonymous user
744 put :update,
745 :id => 1,
746 :notes => notes
747 assert_redirected_to :action => 'show', :id => '1'
748 j = Journal.find(:first, :order => 'id DESC')
749 assert_equal notes, j.notes
750 assert_equal 0, j.details.size
751 assert_equal User.anonymous, j.user
752
753 mail = ActionMailer::Base.deliveries.last
754 assert mail.body.include?(notes)
755 end
756
757 def test_put_update_with_note_and_spent_time
758 @request.session[:user_id] = 2
759 spent_hours_before = Issue.find(1).spent_hours
760 assert_difference('TimeEntry.count') do
761 put :update,
762 :id => 1,
763 :notes => '2.5 hours added',
764 :time_entry => { :hours => '2.5', :comments => 'test_put_update_with_note_and_spent_time', :activity_id => TimeEntryActivity.first.id }
765 end
766 assert_redirected_to :action => 'show', :id => '1'
767
768 issue = Issue.find(1)
769
770 j = Journal.find(:first, :order => 'id DESC')
771 assert_equal '2.5 hours added', j.notes
772 assert_equal 0, j.details.size
773
774 t = issue.time_entries.find_by_comments('test_put_update_with_note_and_spent_time')
775 assert_not_nil t
776 assert_equal 2.5, t.hours
777 assert_equal spent_hours_before + 2.5, issue.spent_hours
778 end
779
780 def test_put_update_with_attachment_only
781 set_tmp_attachments_directory
782
783 # Delete all fixtured journals, a race condition can occur causing the wrong
784 # journal to get fetched in the next find.
785 Journal.delete_all
786
787 # anonymous user
788 put :update,
789 :id => 1,
790 :notes => '',
791 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
792 assert_redirected_to :action => 'show', :id => '1'
793 j = Issue.find(1).journals.find(:first, :order => 'id DESC')
794 assert j.notes.blank?
795 assert_equal 1, j.details.size
796 assert_equal 'testfile.txt', j.details.first.value
797 assert_equal User.anonymous, j.user
798
799 mail = ActionMailer::Base.deliveries.last
800 assert mail.body.include?('testfile.txt')
801 end
802
803 def test_put_update_with_attachment_that_fails_to_save
804 set_tmp_attachments_directory
805
806 # Delete all fixtured journals, a race condition can occur causing the wrong
807 # journal to get fetched in the next find.
808 Journal.delete_all
809
810 # Mock out the unsaved attachment
811 Attachment.any_instance.stubs(:create).returns(Attachment.new)
812
813 # anonymous user
814 put :update,
815 :id => 1,
816 :notes => '',
817 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
818 assert_redirected_to :action => 'show', :id => '1'
819 assert_equal '1 file(s) could not be saved.', flash[:warning]
820
821 end if Object.const_defined?(:Mocha)
822
823 def test_put_update_with_no_change
824 issue = Issue.find(1)
825 issue.journals.clear
826 ActionMailer::Base.deliveries.clear
827
828 put :update,
829 :id => 1,
830 :notes => ''
831 assert_redirected_to :action => 'show', :id => '1'
832
833 issue.reload
834 assert issue.journals.empty?
835 # No email should be sent
836 assert ActionMailer::Base.deliveries.empty?
837 end
838
839 def test_put_update_should_send_a_notification
840 @request.session[:user_id] = 2
841 ActionMailer::Base.deliveries.clear
842 issue = Issue.find(1)
843 old_subject = issue.subject
844 new_subject = 'Subject modified by IssuesControllerTest#test_post_edit'
845
846 put :update, :id => 1, :issue => {:subject => new_subject,
847 :priority_id => '6',
848 :category_id => '1' # no change
849 }
850 assert_equal 1, ActionMailer::Base.deliveries.size
851 end
852
853 def test_put_update_with_invalid_spent_time
854 @request.session[:user_id] = 2
855 notes = 'Note added by IssuesControllerTest#test_post_edit_with_invalid_spent_time'
856
857 assert_no_difference('Journal.count') do
858 put :update,
859 :id => 1,
860 :notes => notes,
861 :time_entry => {"comments"=>"", "activity_id"=>"", "hours"=>"2z"}
862 end
863 assert_response :success
864 assert_template 'edit'
865
866 assert_tag :textarea, :attributes => { :name => 'notes' },
867 :content => notes
868 assert_tag :input, :attributes => { :name => 'time_entry[hours]', :value => "2z" }
869 end
870
871 def test_put_update_should_allow_fixed_version_to_be_set_to_a_subproject
872 issue = Issue.find(2)
873 @request.session[:user_id] = 2
874
875 put :update,
876 :id => issue.id,
877 :issue => {
878 :fixed_version_id => 4
879 }
880
881 assert_response :redirect
882 issue.reload
883 assert_equal 4, issue.fixed_version_id
884 assert_not_equal issue.project_id, issue.fixed_version.project_id
885 end
886
887 def test_put_update_should_redirect_back_using_the_back_url_parameter
888 issue = Issue.find(2)
889 @request.session[:user_id] = 2
890
891 put :update,
892 :id => issue.id,
893 :issue => {
894 :fixed_version_id => 4
895 },
896 :back_url => '/issues'
897
898 assert_response :redirect
899 assert_redirected_to '/issues'
900 end
901
902 def test_put_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host
903 issue = Issue.find(2)
904 @request.session[:user_id] = 2
905
906 put :update,
907 :id => issue.id,
908 :issue => {
909 :fixed_version_id => 4
910 },
911 :back_url => 'http://google.com'
912
913 assert_response :redirect
914 assert_redirected_to :controller => 'issues', :action => 'show', :id => issue.id
915 end
916
917 def test_get_bulk_edit
918 @request.session[:user_id] = 2
919 get :bulk_edit, :ids => [1, 2]
920 assert_response :success
921 assert_template 'bulk_edit'
922
923 # Project specific custom field, date type
924 field = CustomField.find(9)
925 assert !field.is_for_all?
926 assert_equal 'date', field.field_format
927 assert_tag :input, :attributes => {:name => 'issue[custom_field_values][9]'}
928
929 # System wide custom field
930 assert CustomField.find(1).is_for_all?
931 assert_tag :select, :attributes => {:name => 'issue[custom_field_values][1]'}
932 end
933
934 def test_bulk_edit
935 @request.session[:user_id] = 2
936 # update issues priority
937 post :bulk_edit, :ids => [1, 2], :notes => 'Bulk editing',
938 :issue => {:priority_id => 7,
939 :assigned_to_id => '',
940 :custom_field_values => {'2' => ''}}
941
942 assert_response 302
943 # check that the issues were updated
944 assert_equal [7, 7], Issue.find_all_by_id([1, 2]).collect {|i| i.priority.id}
945
946 issue = Issue.find(1)
947 journal = issue.journals.find(:first, :order => 'created_on DESC')
948 assert_equal '125', issue.custom_value_for(2).value
949 assert_equal 'Bulk editing', journal.notes
950 assert_equal 1, journal.details.size
951 end
952
953 def test_bullk_edit_should_send_a_notification
954 @request.session[:user_id] = 2
955 ActionMailer::Base.deliveries.clear
956 post(:bulk_edit,
957 {
958 :ids => [1, 2],
959 :notes => 'Bulk editing',
960 :issue => {
961 :priority_id => 7,
962 :assigned_to_id => '',
963 :custom_field_values => {'2' => ''}
964 }
965 })
966
967 assert_response 302
968 assert_equal 2, ActionMailer::Base.deliveries.size
969 end
970
971 def test_bulk_edit_status
972 @request.session[:user_id] = 2
973 # update issues priority
974 post :bulk_edit, :ids => [1, 2], :notes => 'Bulk editing status',
975 :issue => {:priority_id => '',
976 :assigned_to_id => '',
977 :status_id => '5'}
978
979 assert_response 302
980 issue = Issue.find(1)
981 assert issue.closed?
982 end
983
984 def test_bulk_edit_custom_field
985 @request.session[:user_id] = 2
986 # update issues priority
987 post :bulk_edit, :ids => [1, 2], :notes => 'Bulk editing custom field',
988 :issue => {:priority_id => '',
989 :assigned_to_id => '',
990 :custom_field_values => {'2' => '777'}}
991
992 assert_response 302
993
994 issue = Issue.find(1)
995 journal = issue.journals.find(:first, :order => 'created_on DESC')
996 assert_equal '777', issue.custom_value_for(2).value
997 assert_equal 1, journal.details.size
998 assert_equal '125', journal.details.first.old_value
999 assert_equal '777', journal.details.first.value
1000 end
1001
1002 def test_bulk_unassign
1003 assert_not_nil Issue.find(2).assigned_to
1004 @request.session[:user_id] = 2
1005 # unassign issues
1006 post :bulk_edit, :ids => [1, 2], :notes => 'Bulk unassigning', :issue => {:assigned_to_id => 'none'}
1007 assert_response 302
1008 # check that the issues were updated
1009 assert_nil Issue.find(2).assigned_to
1010 end
1011
1012 def test_post_bulk_edit_should_allow_fixed_version_to_be_set_to_a_subproject
1013 @request.session[:user_id] = 2
1014
1015 post :bulk_edit, :ids => [1,2], :issue => {:fixed_version_id => 4}
1016
1017 assert_response :redirect
1018 issues = Issue.find([1,2])
1019 issues.each do |issue|
1020 assert_equal 4, issue.fixed_version_id
1021 assert_not_equal issue.project_id, issue.fixed_version.project_id
1022 end
1023 end
1024
1025 def test_post_bulk_edit_should_redirect_back_using_the_back_url_parameter
1026 @request.session[:user_id] = 2
1027 post :bulk_edit, :ids => [1,2], :back_url => '/issues'
1028
1029 assert_response :redirect
1030 assert_redirected_to '/issues'
1031 end
1032
1033 def test_post_bulk_edit_should_not_redirect_back_using_the_back_url_parameter_off_the_host
1034 @request.session[:user_id] = 2
1035 post :bulk_edit, :ids => [1,2], :back_url => 'http://google.com'
1036
1037 assert_response :redirect
1038 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => Project.find(1).identifier
1039 end
1040
1041 def test_move_one_issue_to_another_project
1042 @request.session[:user_id] = 2
1043 post :move, :id => 1, :new_project_id => 2, :tracker_id => '', :assigned_to_id => '', :status_id => '', :start_date => '', :due_date => ''
1044 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1045 assert_equal 2, Issue.find(1).project_id
1046 end
1047
1048 def test_move_one_issue_to_another_project_should_follow_when_needed
1049 @request.session[:user_id] = 2
1050 post :move, :id => 1, :new_project_id => 2, :follow => '1'
1051 assert_redirected_to '/issues/1'
1052 end
1053
1054 def test_bulk_move_to_another_project
1055 @request.session[:user_id] = 2
1056 post :move, :ids => [1, 2], :new_project_id => 2
1057 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1058 # Issues moved to project 2
1059 assert_equal 2, Issue.find(1).project_id
1060 assert_equal 2, Issue.find(2).project_id
1061 # No tracker change
1062 assert_equal 1, Issue.find(1).tracker_id
1063 assert_equal 2, Issue.find(2).tracker_id
1064 end
1065
1066 def test_bulk_move_to_another_tracker
1067 @request.session[:user_id] = 2
1068 post :move, :ids => [1, 2], :new_tracker_id => 2
1069 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1070 assert_equal 2, Issue.find(1).tracker_id
1071 assert_equal 2, Issue.find(2).tracker_id
1072 end
1073
1074 def test_bulk_copy_to_another_project
1075 @request.session[:user_id] = 2
1076 assert_difference 'Issue.count', 2 do
1077 assert_no_difference 'Project.find(1).issues.count' do
1078 post :move, :ids => [1, 2], :new_project_id => 2, :copy_options => {:copy => '1'}
1079 end
1080 end
1081 assert_redirected_to 'projects/ecookbook/issues'
1082 end
1083
1084 context "#move via bulk copy" do
1085 should "allow not changing the issue's attributes" do
1086 @request.session[:user_id] = 2
1087 issue_before_move = Issue.find(1)
1088 assert_difference 'Issue.count', 1 do
1089 assert_no_difference 'Project.find(1).issues.count' do
1090 post :move, :ids => [1], :new_project_id => 2, :copy_options => {:copy => '1'}, :new_tracker_id => '', :assigned_to_id => '', :status_id => '', :start_date => '', :due_date => ''
1091 end
1092 end
1093 issue_after_move = Issue.first(:order => 'id desc', :conditions => {:project_id => 2})
1094 assert_equal issue_before_move.tracker_id, issue_after_move.tracker_id
1095 assert_equal issue_before_move.status_id, issue_after_move.status_id
1096 assert_equal issue_before_move.assigned_to_id, issue_after_move.assigned_to_id
1097 end
1098
1099 should "allow changing the issue's attributes" do
1100 # Fixes random test failure with Mysql
1101 # where Issue.all(:limit => 2, :order => 'id desc', :conditions => {:project_id => 2}) doesn't return the expected results
1102 Issue.delete_all("project_id=2")
1103
1104 @request.session[:user_id] = 2
1105 assert_difference 'Issue.count', 2 do
1106 assert_no_difference 'Project.find(1).issues.count' do
1107 post :move, :ids => [1, 2], :new_project_id => 2, :copy_options => {:copy => '1'}, :new_tracker_id => '', :assigned_to_id => 4, :status_id => 3, :start_date => '2009-12-01', :due_date => '2009-12-31'
1108 end
1109 end
1110
1111 copied_issues = Issue.all(:limit => 2, :order => 'id desc', :conditions => {:project_id => 2})
1112 assert_equal 2, copied_issues.size
1113 copied_issues.each do |issue|
1114 assert_equal 2, issue.project_id, "Project is incorrect"
1115 assert_equal 4, issue.assigned_to_id, "Assigned to is incorrect"
1116 assert_equal 3, issue.status_id, "Status is incorrect"
1117 assert_equal '2009-12-01', issue.start_date.to_s, "Start date is incorrect"
1118 assert_equal '2009-12-31', issue.due_date.to_s, "Due date is incorrect"
1119 end
1120 end
1121 end
1122
1123 def test_copy_to_another_project_should_follow_when_needed
1124 @request.session[:user_id] = 2
1125 post :move, :ids => [1], :new_project_id => 2, :copy_options => {:copy => '1'}, :follow => '1'
1126 issue = Issue.first(:order => 'id DESC')
1127 assert_redirected_to :controller => 'issues', :action => 'show', :id => issue
1128 end
1129
1130 def test_context_menu_one_issue
1131 @request.session[:user_id] = 2
1132 get :context_menu, :ids => [1]
1133 assert_response :success
1134 assert_template 'context_menu'
1135 assert_tag :tag => 'a', :content => 'Edit',
1136 :attributes => { :href => '/issues/1/edit',
1137 :class => 'icon-edit' }
1138 assert_tag :tag => 'a', :content => 'Closed',
1139 :attributes => { :href => '/issues/1/edit?issue%5Bstatus_id%5D=5',
1140 :class => '' }
1141 assert_tag :tag => 'a', :content => 'Immediate',
1142 :attributes => { :href => '/issues/bulk_edit?ids%5B%5D=1&amp;issue%5Bpriority_id%5D=8',
1143 :class => '' }
1144 # Versions
1145 assert_tag :tag => 'a', :content => '2.0',
1146 :attributes => { :href => '/issues/bulk_edit?ids%5B%5D=1&amp;issue%5Bfixed_version_id%5D=3',
1147 :class => '' }
1148 assert_tag :tag => 'a', :content => 'eCookbook Subproject 1 - 2.0',
1149 :attributes => { :href => '/issues/bulk_edit?ids%5B%5D=1&amp;issue%5Bfixed_version_id%5D=4',
1150 :class => '' }
1151
1152 assert_tag :tag => 'a', :content => 'Dave Lopper',
1153 :attributes => { :href => '/issues/bulk_edit?ids%5B%5D=1&amp;issue%5Bassigned_to_id%5D=3',
1154 :class => '' }
1155 assert_tag :tag => 'a', :content => 'Duplicate',
1156 :attributes => { :href => '/projects/ecookbook/issues/1/copy',
1157 :class => 'icon-duplicate' }
1158 assert_tag :tag => 'a', :content => 'Copy',
1159 :attributes => { :href => '/issues/move?copy_options%5Bcopy%5D=t&amp;ids%5B%5D=1',
1160 :class => 'icon-copy' }
1161 assert_tag :tag => 'a', :content => 'Move',
1162 :attributes => { :href => '/issues/move?ids%5B%5D=1',
1163 :class => 'icon-move' }
1164 assert_tag :tag => 'a', :content => 'Delete',
1165 :attributes => { :href => '/issues/destroy?ids%5B%5D=1',
1166 :class => 'icon-del' }
1167 end
1168
1169 def test_context_menu_one_issue_by_anonymous
1170 get :context_menu, :ids => [1]
1171 assert_response :success
1172 assert_template 'context_menu'
1173 assert_tag :tag => 'a', :content => 'Delete',
1174 :attributes => { :href => '#',
1175 :class => 'icon-del disabled' }
1176 end
1177
1178 def test_context_menu_multiple_issues_of_same_project
1179 @request.session[:user_id] = 2
1180 get :context_menu, :ids => [1, 2]
1181 assert_response :success
1182 assert_template 'context_menu'
1183 assert_tag :tag => 'a', :content => 'Edit',
1184 :attributes => { :href => '/issues/bulk_edit?ids%5B%5D=1&amp;ids%5B%5D=2',
1185 :class => 'icon-edit' }
1186 assert_tag :tag => 'a', :content => 'Immediate',
1187 :attributes => { :href => '/issues/bulk_edit?ids%5B%5D=1&amp;ids%5B%5D=2&amp;issue%5Bpriority_id%5D=8',
1188 :class => '' }
1189 assert_tag :tag => 'a', :content => 'Dave Lopper',
1190 :attributes => { :href => '/issues/bulk_edit?ids%5B%5D=1&amp;ids%5B%5D=2&amp;issue%5Bassigned_to_id%5D=3',
1191 :class => '' }
1192 assert_tag :tag => 'a', :content => 'Copy',
1193 :attributes => { :href => '/issues/move?copy_options%5Bcopy%5D=t&amp;ids%5B%5D=1&amp;ids%5B%5D=2',
1194 :class => 'icon-copy' }
1195 assert_tag :tag => 'a', :content => 'Move',
1196 :attributes => { :href => '/issues/move?ids%5B%5D=1&amp;ids%5B%5D=2',
1197 :class => 'icon-move' }
1198 assert_tag :tag => 'a', :content => 'Delete',
1199 :attributes => { :href => '/issues/destroy?ids%5B%5D=1&amp;ids%5B%5D=2',
1200 :class => 'icon-del' }
1201 end
1202
1203 def test_context_menu_multiple_issues_of_different_project
1204 @request.session[:user_id] = 2
1205 get :context_menu, :ids => [1, 2, 4]
1206 assert_response :success
1207 assert_template 'context_menu'
1208 assert_tag :tag => 'a', :content => 'Delete',
1209 :attributes => { :href => '#',
1210 :class => 'icon-del disabled' }
1211 end
1212
1213 def test_preview_new_issue
1214 @request.session[:user_id] = 2
1215 post :preview, :project_id => '1', :issue => {:description => 'Foo'}
1216 assert_response :success
1217 assert_template 'preview'
1218 assert_not_nil assigns(:description)
1219 end
1220
1221 def test_preview_notes
1222 @request.session[:user_id] = 2
1223 post :preview, :project_id => '1', :id => 1, :issue => {:description => Issue.find(1).description}, :notes => 'Foo'
1224 assert_response :success
1225 assert_template 'preview'
1226 assert_not_nil assigns(:notes)
1227 end
1228
1229 def test_auto_complete_should_not_be_case_sensitive
1230 get :auto_complete, :project_id => 'ecookbook', :q => 'ReCiPe'
1231 assert_response :success
1232 assert_not_nil assigns(:issues)
1233 assert assigns(:issues).detect {|issue| issue.subject.match /recipe/}
1234 end
1235
1236 def test_auto_complete_should_return_issue_with_given_id
1237 get :auto_complete, :project_id => 'subproject1', :q => '13'
1238 assert_response :success
1239 assert_not_nil assigns(:issues)
1240 assert assigns(:issues).include?(Issue.find(13))
1241 end
1242
1243 def test_destroy_issue_with_no_time_entries
1244 assert_nil TimeEntry.find_by_issue_id(2)
1245 @request.session[:user_id] = 2
1246 post :destroy, :id => 2
1247 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1248 assert_nil Issue.find_by_id(2)
1249 end
1250
1251 def test_destroy_issues_with_time_entries
1252 @request.session[:user_id] = 2
1253 post :destroy, :ids => [1, 3]
1254 assert_response :success
1255 assert_template 'destroy'
1256 assert_not_nil assigns(:hours)
1257 assert Issue.find_by_id(1) && Issue.find_by_id(3)
1258 end
1259
1260 def test_destroy_issues_and_destroy_time_entries
1261 @request.session[:user_id] = 2
1262 post :destroy, :ids => [1, 3], :todo => 'destroy'
1263 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1264 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
1265 assert_nil TimeEntry.find_by_id([1, 2])
1266 end
1267
1268 def test_destroy_issues_and_assign_time_entries_to_project
1269 @request.session[:user_id] = 2
1270 post :destroy, :ids => [1, 3], :todo => 'nullify'
1271 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1272 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
1273 assert_nil TimeEntry.find(1).issue_id
1274 assert_nil TimeEntry.find(2).issue_id
1275 end
1276
1277 def test_destroy_issues_and_reassign_time_entries_to_another_issue
1278 @request.session[:user_id] = 2
1279 post :destroy, :ids => [1, 3], :todo => 'reassign', :reassign_to_id => 2
1280 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1281 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
1282 assert_equal 2, TimeEntry.find(1).issue_id
1283 assert_equal 2, TimeEntry.find(2).issue_id
1284 end
1285
1286 def test_default_search_scope
1287 get :index
1288 assert_tag :div, :attributes => {:id => 'quick-search'},
1289 :child => {:tag => 'form',
1290 :child => {:tag => 'input', :attributes => {:name => 'issues', :type => 'hidden', :value => '1'}}}
1291 end
1292 end