OSDN Git Service

Bulk edit refactoring.
[redminele/redmine.git] / test / functional / issues_controller_test.rb
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_gantt
235     get :gantt, :project_id => 1
236     assert_response :success
237     assert_template 'gantt.rhtml'
238     assert_not_nil assigns(:gantt)
239     events = assigns(:gantt).events
240     assert_not_nil events
241     # Issue with start and due dates
242     i = Issue.find(1)
243     assert_not_nil i.due_date
244     assert events.include?(Issue.find(1))
245     # Issue with without due date but targeted to a version with date
246     i = Issue.find(2)
247     assert_nil i.due_date
248     assert events.include?(i)
249   end
250
251   def test_cross_project_gantt
252     get :gantt
253     assert_response :success
254     assert_template 'gantt.rhtml'
255     assert_not_nil assigns(:gantt)
256     events = assigns(:gantt).events
257     assert_not_nil events
258   end
259
260   def test_gantt_export_to_pdf
261     get :gantt, :project_id => 1, :format => 'pdf'
262     assert_response :success
263     assert_equal 'application/pdf', @response.content_type
264     assert @response.body.starts_with?('%PDF')
265     assert_not_nil assigns(:gantt)
266   end
267
268   def test_cross_project_gantt_export_to_pdf
269     get :gantt, :format => 'pdf'
270     assert_response :success
271     assert_equal 'application/pdf', @response.content_type
272     assert @response.body.starts_with?('%PDF')
273     assert_not_nil assigns(:gantt)
274   end
275   
276   if Object.const_defined?(:Magick)
277     def test_gantt_image
278       get :gantt, :project_id => 1, :format => 'png'
279       assert_response :success
280       assert_equal 'image/png', @response.content_type
281     end
282   else
283     puts "RMagick not installed. Skipping tests !!!"
284   end
285   
286   def test_calendar
287     get :calendar, :project_id => 1
288     assert_response :success
289     assert_template 'calendar'
290     assert_not_nil assigns(:calendar)
291   end
292   
293   def test_cross_project_calendar
294     get :calendar
295     assert_response :success
296     assert_template 'calendar'
297     assert_not_nil assigns(:calendar)
298   end
299   
300   def test_changes
301     get :changes, :project_id => 1
302     assert_response :success
303     assert_not_nil assigns(:journals)
304     assert_equal 'application/atom+xml', @response.content_type
305   end
306   
307   def test_show_by_anonymous
308     get :show, :id => 1
309     assert_response :success
310     assert_template 'show.rhtml'
311     assert_not_nil assigns(:issue)
312     assert_equal Issue.find(1), assigns(:issue)
313     
314     # anonymous role is allowed to add a note
315     assert_tag :tag => 'form',
316                :descendant => { :tag => 'fieldset',
317                                 :child => { :tag => 'legend', 
318                                             :content => /Notes/ } }
319   end
320   
321   def test_show_by_manager
322     @request.session[:user_id] = 2
323     get :show, :id => 1
324     assert_response :success
325     
326     assert_tag :tag => 'form',
327                :descendant => { :tag => 'fieldset',
328                                 :child => { :tag => 'legend', 
329                                             :content => /Change properties/ } },
330                :descendant => { :tag => 'fieldset',
331                                 :child => { :tag => 'legend', 
332                                             :content => /Log time/ } },
333                :descendant => { :tag => 'fieldset',
334                                 :child => { :tag => 'legend', 
335                                             :content => /Notes/ } }
336   end
337   
338   def test_show_should_deny_anonymous_access_without_permission
339     Role.anonymous.remove_permission!(:view_issues)
340     get :show, :id => 1
341     assert_response :redirect
342   end
343   
344   def test_show_should_deny_non_member_access_without_permission
345     Role.non_member.remove_permission!(:view_issues)
346     @request.session[:user_id] = 9
347     get :show, :id => 1
348     assert_response 403
349   end
350   
351   def test_show_should_deny_member_access_without_permission
352     Role.find(1).remove_permission!(:view_issues)
353     @request.session[:user_id] = 2
354     get :show, :id => 1
355     assert_response 403
356   end
357   
358   def test_show_should_not_disclose_relations_to_invisible_issues
359     Setting.cross_project_issue_relations = '1'
360     IssueRelation.create!(:issue_from => Issue.find(1), :issue_to => Issue.find(2), :relation_type => 'relates')
361     # Relation to a private project issue
362     IssueRelation.create!(:issue_from => Issue.find(1), :issue_to => Issue.find(4), :relation_type => 'relates')
363     
364     get :show, :id => 1
365     assert_response :success
366     
367     assert_tag :div, :attributes => { :id => 'relations' },
368                      :descendant => { :tag => 'a', :content => /#2$/ }
369     assert_no_tag :div, :attributes => { :id => 'relations' },
370                         :descendant => { :tag => 'a', :content => /#4$/ }
371   end
372   
373   def test_show_atom
374     get :show, :id => 2, :format => 'atom'
375     assert_response :success
376     assert_template 'changes.rxml'
377     # Inline image
378     assert @response.body.include?("&lt;img src=\"http://test.host/attachments/download/10\" alt=\"\" /&gt;"), "Body did not match. Body: #{@response.body}"
379   end
380   
381   def test_show_export_to_pdf
382     get :show, :id => 3, :format => 'pdf'
383     assert_response :success
384     assert_equal 'application/pdf', @response.content_type
385     assert @response.body.starts_with?('%PDF')
386     assert_not_nil assigns(:issue)
387   end
388
389   def test_get_new
390     @request.session[:user_id] = 2
391     get :new, :project_id => 1, :tracker_id => 1
392     assert_response :success
393     assert_template 'new'
394     
395     assert_tag :tag => 'input', :attributes => { :name => 'issue[custom_field_values][2]',
396                                                  :value => 'Default string' }
397   end
398
399   def test_get_new_without_tracker_id
400     @request.session[:user_id] = 2
401     get :new, :project_id => 1
402     assert_response :success
403     assert_template 'new'
404     
405     issue = assigns(:issue)
406     assert_not_nil issue
407     assert_equal Project.find(1).trackers.first, issue.tracker
408   end
409   
410   def test_get_new_with_no_default_status_should_display_an_error
411     @request.session[:user_id] = 2
412     IssueStatus.delete_all
413     
414     get :new, :project_id => 1
415     assert_response 500
416     assert_not_nil flash[:error]
417     assert_tag :tag => 'div', :attributes => { :class => /error/ },
418                               :content => /No default issue/
419   end
420   
421   def test_get_new_with_no_tracker_should_display_an_error
422     @request.session[:user_id] = 2
423     Tracker.delete_all
424     
425     get :new, :project_id => 1
426     assert_response 500
427     assert_not_nil flash[:error]
428     assert_tag :tag => 'div', :attributes => { :class => /error/ },
429                               :content => /No tracker/
430   end
431   
432   def test_update_new_form
433     @request.session[:user_id] = 2
434     xhr :post, :update_form, :project_id => 1,
435                      :issue => {:tracker_id => 2, 
436                                 :subject => 'This is the test_new issue',
437                                 :description => 'This is the description',
438                                 :priority_id => 5}
439     assert_response :success
440     assert_template 'attributes'
441     
442     issue = assigns(:issue)
443     assert_kind_of Issue, issue
444     assert_equal 1, issue.project_id
445     assert_equal 2, issue.tracker_id
446     assert_equal 'This is the test_new issue', issue.subject
447   end
448   
449   def test_post_new
450     @request.session[:user_id] = 2
451     assert_difference 'Issue.count' do
452       post :new, :project_id => 1, 
453                  :issue => {:tracker_id => 3,
454                             :subject => 'This is the test_new issue',
455                             :description => 'This is the description',
456                             :priority_id => 5,
457                             :estimated_hours => '',
458                             :custom_field_values => {'2' => 'Value for field 2'}}
459     end
460     assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
461     
462     issue = Issue.find_by_subject('This is the test_new issue')
463     assert_not_nil issue
464     assert_equal 2, issue.author_id
465     assert_equal 3, issue.tracker_id
466     assert_nil issue.estimated_hours
467     v = issue.custom_values.find(:first, :conditions => {:custom_field_id => 2})
468     assert_not_nil v
469     assert_equal 'Value for field 2', v.value
470   end
471   
472   def test_post_new_and_continue
473     @request.session[:user_id] = 2
474     post :new, :project_id => 1, 
475                :issue => {:tracker_id => 3,
476                           :subject => 'This is first issue',
477                           :priority_id => 5},
478                :continue => ''
479     assert_redirected_to :controller => 'issues', :action => 'new', :tracker_id => 3
480   end
481   
482   def test_post_new_without_custom_fields_param
483     @request.session[:user_id] = 2
484     assert_difference 'Issue.count' do
485       post :new, :project_id => 1, 
486                  :issue => {:tracker_id => 1,
487                             :subject => 'This is the test_new issue',
488                             :description => 'This is the description',
489                             :priority_id => 5}
490     end
491     assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
492   end
493
494   def test_post_new_with_required_custom_field_and_without_custom_fields_param
495     field = IssueCustomField.find_by_name('Database')
496     field.update_attribute(:is_required, true)
497
498     @request.session[:user_id] = 2
499     post :new, :project_id => 1, 
500                :issue => {:tracker_id => 1,
501                           :subject => 'This is the test_new issue',
502                           :description => 'This is the description',
503                           :priority_id => 5}
504     assert_response :success
505     assert_template 'new'
506     issue = assigns(:issue)
507     assert_not_nil issue
508     assert_equal I18n.translate('activerecord.errors.messages.invalid'), issue.errors.on(:custom_values)
509   end
510   
511   def test_post_new_with_watchers
512     @request.session[:user_id] = 2
513     ActionMailer::Base.deliveries.clear
514     
515     assert_difference 'Watcher.count', 2 do
516       post :new, :project_id => 1, 
517                  :issue => {:tracker_id => 1,
518                             :subject => 'This is a new issue with watchers',
519                             :description => 'This is the description',
520                             :priority_id => 5,
521                             :watcher_user_ids => ['2', '3']}
522     end
523     issue = Issue.find_by_subject('This is a new issue with watchers')
524     assert_not_nil issue
525     assert_redirected_to :controller => 'issues', :action => 'show', :id => issue
526     
527     # Watchers added
528     assert_equal [2, 3], issue.watcher_user_ids.sort
529     assert issue.watched_by?(User.find(3))
530     # Watchers notified
531     mail = ActionMailer::Base.deliveries.last
532     assert_kind_of TMail::Mail, mail
533     assert [mail.bcc, mail.cc].flatten.include?(User.find(3).mail)
534   end
535   
536   def test_post_new_should_send_a_notification
537     ActionMailer::Base.deliveries.clear
538     @request.session[:user_id] = 2
539     assert_difference 'Issue.count' do
540       post :new, :project_id => 1, 
541                  :issue => {:tracker_id => 3,
542                             :subject => 'This is the test_new issue',
543                             :description => 'This is the description',
544                             :priority_id => 5,
545                             :estimated_hours => '',
546                             :custom_field_values => {'2' => 'Value for field 2'}}
547     end
548     assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
549     
550     assert_equal 1, ActionMailer::Base.deliveries.size
551   end
552   
553   def test_post_should_preserve_fields_values_on_validation_failure
554     @request.session[:user_id] = 2
555     post :new, :project_id => 1, 
556                :issue => {:tracker_id => 1,
557                           # empty subject
558                           :subject => '',
559                           :description => 'This is a description',
560                           :priority_id => 6,
561                           :custom_field_values => {'1' => 'Oracle', '2' => 'Value for field 2'}}
562     assert_response :success
563     assert_template 'new'
564     
565     assert_tag :textarea, :attributes => { :name => 'issue[description]' },
566                           :content => 'This is a description'
567     assert_tag :select, :attributes => { :name => 'issue[priority_id]' },
568                         :child => { :tag => 'option', :attributes => { :selected => 'selected',
569                                                                        :value => '6' },
570                                                       :content => 'High' }  
571     # Custom fields
572     assert_tag :select, :attributes => { :name => 'issue[custom_field_values][1]' },
573                         :child => { :tag => 'option', :attributes => { :selected => 'selected',
574                                                                        :value => 'Oracle' },
575                                                       :content => 'Oracle' }  
576     assert_tag :input, :attributes => { :name => 'issue[custom_field_values][2]',
577                                         :value => 'Value for field 2'}
578   end
579   
580   def test_post_new_should_ignore_non_safe_attributes
581     @request.session[:user_id] = 2
582     assert_nothing_raised do
583       post :new, :project_id => 1, :issue => { :tracker => "A param can not be a Tracker" }
584     end
585   end
586   
587   def test_copy_issue
588     @request.session[:user_id] = 2
589     get :new, :project_id => 1, :copy_from => 1
590     assert_template 'new'
591     assert_not_nil assigns(:issue)
592     orig = Issue.find(1)
593     assert_equal orig.subject, assigns(:issue).subject
594   end
595   
596   def test_get_edit
597     @request.session[:user_id] = 2
598     get :edit, :id => 1
599     assert_response :success
600     assert_template 'edit'
601     assert_not_nil assigns(:issue)
602     assert_equal Issue.find(1), assigns(:issue)
603   end
604   
605   def test_get_edit_with_params
606     @request.session[:user_id] = 2
607     get :edit, :id => 1, :issue => { :status_id => 5, :priority_id => 7 }
608     assert_response :success
609     assert_template 'edit'
610     
611     issue = assigns(:issue)
612     assert_not_nil issue
613     
614     assert_equal 5, issue.status_id
615     assert_tag :select, :attributes => { :name => 'issue[status_id]' },
616                         :child => { :tag => 'option', 
617                                     :content => 'Closed',
618                                     :attributes => { :selected => 'selected' } }
619                                     
620     assert_equal 7, issue.priority_id
621     assert_tag :select, :attributes => { :name => 'issue[priority_id]' },
622                         :child => { :tag => 'option', 
623                                     :content => 'Urgent',
624                                     :attributes => { :selected => 'selected' } }
625   end
626
627   def test_update_edit_form
628     @request.session[:user_id] = 2
629     xhr :post, :update_form, :project_id => 1,
630                              :id => 1,
631                              :issue => {:tracker_id => 2, 
632                                         :subject => 'This is the test_new issue',
633                                         :description => 'This is the description',
634                                         :priority_id => 5}
635     assert_response :success
636     assert_template 'attributes'
637     
638     issue = assigns(:issue)
639     assert_kind_of Issue, issue
640     assert_equal 1, issue.id
641     assert_equal 1, issue.project_id
642     assert_equal 2, issue.tracker_id
643     assert_equal 'This is the test_new issue', issue.subject
644   end
645   
646   def test_reply_to_issue
647     @request.session[:user_id] = 2
648     get :reply, :id => 1
649     assert_response :success
650     assert_select_rjs :show, "update"
651   end
652
653   def test_reply_to_note
654     @request.session[:user_id] = 2
655     get :reply, :id => 1, :journal_id => 2
656     assert_response :success
657     assert_select_rjs :show, "update"
658   end
659
660   def test_post_edit_without_custom_fields_param
661     @request.session[:user_id] = 2
662     ActionMailer::Base.deliveries.clear
663     
664     issue = Issue.find(1)
665     assert_equal '125', issue.custom_value_for(2).value
666     old_subject = issue.subject
667     new_subject = 'Subject modified by IssuesControllerTest#test_post_edit'
668     
669     assert_difference('Journal.count') do
670       assert_difference('JournalDetail.count', 2) do
671         post :edit, :id => 1, :issue => {:subject => new_subject,
672                                          :priority_id => '6',
673                                          :category_id => '1' # no change
674                                         }
675       end
676     end
677     assert_redirected_to :action => 'show', :id => '1'
678     issue.reload
679     assert_equal new_subject, issue.subject
680     # Make sure custom fields were not cleared
681     assert_equal '125', issue.custom_value_for(2).value
682     
683     mail = ActionMailer::Base.deliveries.last
684     assert_kind_of TMail::Mail, mail
685     assert mail.subject.starts_with?("[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}]")
686     assert mail.body.include?("Subject changed from #{old_subject} to #{new_subject}")
687   end
688   
689   def test_post_edit_with_custom_field_change
690     @request.session[:user_id] = 2
691     issue = Issue.find(1)
692     assert_equal '125', issue.custom_value_for(2).value
693     
694     assert_difference('Journal.count') do
695       assert_difference('JournalDetail.count', 3) do
696         post :edit, :id => 1, :issue => {:subject => 'Custom field change',
697                                          :priority_id => '6',
698                                          :category_id => '1', # no change
699                                          :custom_field_values => { '2' => 'New custom value' }
700                                         }
701       end
702     end
703     assert_redirected_to :action => 'show', :id => '1'
704     issue.reload
705     assert_equal 'New custom value', issue.custom_value_for(2).value
706     
707     mail = ActionMailer::Base.deliveries.last
708     assert_kind_of TMail::Mail, mail
709     assert mail.body.include?("Searchable field changed from 125 to New custom value")
710   end
711   
712   def test_post_edit_with_status_and_assignee_change
713     issue = Issue.find(1)
714     assert_equal 1, issue.status_id
715     @request.session[:user_id] = 2
716     assert_difference('TimeEntry.count', 0) do
717       post :edit,
718            :id => 1,
719            :issue => { :status_id => 2, :assigned_to_id => 3 },
720            :notes => 'Assigned to dlopper',
721            :time_entry => { :hours => '', :comments => '', :activity_id => TimeEntryActivity.first }
722     end
723     assert_redirected_to :action => 'show', :id => '1'
724     issue.reload
725     assert_equal 2, issue.status_id
726     j = Journal.find(:first, :order => 'id DESC')
727     assert_equal 'Assigned to dlopper', j.notes
728     assert_equal 2, j.details.size
729     
730     mail = ActionMailer::Base.deliveries.last
731     assert mail.body.include?("Status changed from New to Assigned")
732     # subject should contain the new status
733     assert mail.subject.include?("(#{ IssueStatus.find(2).name })")
734   end
735   
736   def test_post_edit_with_note_only
737     notes = 'Note added by IssuesControllerTest#test_update_with_note_only'
738     # anonymous user
739     post :edit,
740          :id => 1,
741          :notes => notes
742     assert_redirected_to :action => 'show', :id => '1'
743     j = Journal.find(:first, :order => 'id DESC')
744     assert_equal notes, j.notes
745     assert_equal 0, j.details.size
746     assert_equal User.anonymous, j.user
747     
748     mail = ActionMailer::Base.deliveries.last
749     assert mail.body.include?(notes)
750   end
751   
752   def test_post_edit_with_note_and_spent_time
753     @request.session[:user_id] = 2
754     spent_hours_before = Issue.find(1).spent_hours
755     assert_difference('TimeEntry.count') do
756       post :edit,
757            :id => 1,
758            :notes => '2.5 hours added',
759            :time_entry => { :hours => '2.5', :comments => '', :activity_id => TimeEntryActivity.first }
760     end
761     assert_redirected_to :action => 'show', :id => '1'
762     
763     issue = Issue.find(1)
764     
765     j = Journal.find(:first, :order => 'id DESC')
766     assert_equal '2.5 hours added', j.notes
767     assert_equal 0, j.details.size
768     
769     t = issue.time_entries.find(:first, :order => 'id DESC')
770     assert_not_nil t
771     assert_equal 2.5, t.hours
772     assert_equal spent_hours_before + 2.5, issue.spent_hours
773   end
774   
775   def test_post_edit_with_attachment_only
776     set_tmp_attachments_directory
777     
778     # Delete all fixtured journals, a race condition can occur causing the wrong
779     # journal to get fetched in the next find.
780     Journal.delete_all
781
782     # anonymous user
783     post :edit,
784          :id => 1,
785          :notes => '',
786          :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
787     assert_redirected_to :action => 'show', :id => '1'
788     j = Issue.find(1).journals.find(:first, :order => 'id DESC')
789     assert j.notes.blank?
790     assert_equal 1, j.details.size
791     assert_equal 'testfile.txt', j.details.first.value
792     assert_equal User.anonymous, j.user
793     
794     mail = ActionMailer::Base.deliveries.last
795     assert mail.body.include?('testfile.txt')
796   end
797   
798   def test_post_edit_with_no_change
799     issue = Issue.find(1)
800     issue.journals.clear
801     ActionMailer::Base.deliveries.clear
802     
803     post :edit,
804          :id => 1,
805          :notes => ''
806     assert_redirected_to :action => 'show', :id => '1'
807     
808     issue.reload
809     assert issue.journals.empty?
810     # No email should be sent
811     assert ActionMailer::Base.deliveries.empty?
812   end
813
814   def test_post_edit_should_send_a_notification
815     @request.session[:user_id] = 2
816     ActionMailer::Base.deliveries.clear
817     issue = Issue.find(1)
818     old_subject = issue.subject
819     new_subject = 'Subject modified by IssuesControllerTest#test_post_edit'
820     
821     post :edit, :id => 1, :issue => {:subject => new_subject,
822                                      :priority_id => '6',
823                                      :category_id => '1' # no change
824                                     }
825     assert_equal 1, ActionMailer::Base.deliveries.size
826   end
827   
828   def test_post_edit_with_invalid_spent_time
829     @request.session[:user_id] = 2
830     notes = 'Note added by IssuesControllerTest#test_post_edit_with_invalid_spent_time'
831     
832     assert_no_difference('Journal.count') do
833       post :edit,
834            :id => 1,
835            :notes => notes,
836            :time_entry => {"comments"=>"", "activity_id"=>"", "hours"=>"2z"}
837     end
838     assert_response :success
839     assert_template 'edit'
840     
841     assert_tag :textarea, :attributes => { :name => 'notes' },
842                           :content => notes
843     assert_tag :input, :attributes => { :name => 'time_entry[hours]', :value => "2z" }
844   end
845   
846   def test_post_edit_should_allow_fixed_version_to_be_set_to_a_subproject
847     issue = Issue.find(2)
848     @request.session[:user_id] = 2
849
850     post :edit,
851          :id => issue.id,
852          :issue => {
853            :fixed_version_id => 4
854          }
855
856     assert_response :redirect
857     issue.reload
858     assert_equal 4, issue.fixed_version_id
859     assert_not_equal issue.project_id, issue.fixed_version.project_id
860   end
861
862   def test_post_edit_should_redirect_back_using_the_back_url_parameter
863     issue = Issue.find(2)
864     @request.session[:user_id] = 2
865
866     post :edit,
867          :id => issue.id,
868          :issue => {
869            :fixed_version_id => 4
870          },
871          :back_url => '/issues'
872
873     assert_response :redirect
874     assert_redirected_to '/issues'
875   end
876   
877   def test_post_edit_should_not_redirect_back_using_the_back_url_parameter_off_the_host
878     issue = Issue.find(2)
879     @request.session[:user_id] = 2
880
881     post :edit,
882          :id => issue.id,
883          :issue => {
884            :fixed_version_id => 4
885          },
886          :back_url => 'http://google.com'
887
888     assert_response :redirect
889     assert_redirected_to :controller => 'issues', :action => 'show', :id => issue.id
890   end
891   
892   def test_get_bulk_edit
893     @request.session[:user_id] = 2
894     get :bulk_edit, :ids => [1, 2]
895     assert_response :success
896     assert_template 'bulk_edit'
897     
898     # Project specific custom field, date type
899     field = CustomField.find(9)
900     assert !field.is_for_all?
901     assert_equal 'date', field.field_format
902     assert_tag :input, :attributes => {:name => 'issue[custom_field_values][9]'}
903     
904     # System wide custom field
905     assert CustomField.find(1).is_for_all?
906     assert_tag :select, :attributes => {:name => 'issue[custom_field_values][1]'}
907   end
908
909   def test_bulk_edit
910     @request.session[:user_id] = 2
911     # update issues priority
912     post :bulk_edit, :ids => [1, 2], :notes => 'Bulk editing',
913                                      :issue => {:priority_id => 7,
914                                                 :assigned_to_id => '',
915                                                 :custom_field_values => {'2' => ''}}
916                                      
917     assert_response 302
918     # check that the issues were updated
919     assert_equal [7, 7], Issue.find_all_by_id([1, 2]).collect {|i| i.priority.id}
920     
921     issue = Issue.find(1)
922     journal = issue.journals.find(:first, :order => 'created_on DESC')
923     assert_equal '125', issue.custom_value_for(2).value
924     assert_equal 'Bulk editing', journal.notes
925     assert_equal 1, journal.details.size
926   end
927
928   def test_bullk_edit_should_send_a_notification
929     @request.session[:user_id] = 2
930     ActionMailer::Base.deliveries.clear
931     post(:bulk_edit,
932          {
933            :ids => [1, 2],
934            :notes => 'Bulk editing',
935            :issue => {
936              :priority_id => 7,
937              :assigned_to_id => '',
938              :custom_field_values => {'2' => ''}
939            }
940          })
941
942     assert_response 302
943     assert_equal 2, ActionMailer::Base.deliveries.size
944   end
945
946   def test_bulk_edit_status
947     @request.session[:user_id] = 2
948     # update issues priority
949     post :bulk_edit, :ids => [1, 2], :notes => 'Bulk editing status',
950                                      :issue => {:priority_id => '',
951                                                 :assigned_to_id => '',
952                                                 :status_id => '5'}
953                                      
954     assert_response 302
955     issue = Issue.find(1)
956     assert issue.closed?
957   end
958
959   def test_bulk_edit_custom_field
960     @request.session[:user_id] = 2
961     # update issues priority
962     post :bulk_edit, :ids => [1, 2], :notes => 'Bulk editing custom field',
963                                      :issue => {:priority_id => '',
964                                                 :assigned_to_id => '',
965                                                 :custom_field_values => {'2' => '777'}}
966                                      
967     assert_response 302
968     
969     issue = Issue.find(1)
970     journal = issue.journals.find(:first, :order => 'created_on DESC')
971     assert_equal '777', issue.custom_value_for(2).value
972     assert_equal 1, journal.details.size
973     assert_equal '125', journal.details.first.old_value
974     assert_equal '777', journal.details.first.value
975   end
976
977   def test_bulk_unassign
978     assert_not_nil Issue.find(2).assigned_to
979     @request.session[:user_id] = 2
980     # unassign issues
981   post :bulk_edit, :ids => [1, 2], :notes => 'Bulk unassigning', :issue => {:assigned_to_id => 'none'}
982     assert_response 302
983     # check that the issues were updated
984     assert_nil Issue.find(2).assigned_to
985   end
986   
987   def test_post_bulk_edit_should_allow_fixed_version_to_be_set_to_a_subproject
988     @request.session[:user_id] = 2
989
990     post :bulk_edit, :ids => [1,2], :issue => {:fixed_version_id => 4}
991
992     assert_response :redirect
993     issues = Issue.find([1,2])
994     issues.each do |issue|
995       assert_equal 4, issue.fixed_version_id
996       assert_not_equal issue.project_id, issue.fixed_version.project_id
997     end
998   end
999
1000   def test_post_bulk_edit_should_redirect_back_using_the_back_url_parameter
1001     @request.session[:user_id] = 2
1002     post :bulk_edit, :ids => [1,2], :back_url => '/issues'
1003
1004     assert_response :redirect
1005     assert_redirected_to '/issues'
1006   end
1007
1008   def test_post_bulk_edit_should_not_redirect_back_using_the_back_url_parameter_off_the_host
1009     @request.session[:user_id] = 2
1010     post :bulk_edit, :ids => [1,2], :back_url => 'http://google.com'
1011
1012     assert_response :redirect
1013     assert_redirected_to :controller => 'issues', :action => 'index', :project_id => Project.find(1).identifier
1014   end
1015
1016   def test_move_one_issue_to_another_project
1017     @request.session[:user_id] = 2
1018     post :move, :id => 1, :new_project_id => 2, :tracker_id => '', :assigned_to_id => '', :status_id => '', :start_date => '', :due_date => ''
1019     assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1020     assert_equal 2, Issue.find(1).project_id
1021   end
1022
1023   def test_move_one_issue_to_another_project_should_follow_when_needed
1024     @request.session[:user_id] = 2
1025     post :move, :id => 1, :new_project_id => 2, :follow => '1'
1026     assert_redirected_to '/issues/1'
1027   end
1028
1029   def test_bulk_move_to_another_project
1030     @request.session[:user_id] = 2
1031     post :move, :ids => [1, 2], :new_project_id => 2
1032     assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1033     # Issues moved to project 2
1034     assert_equal 2, Issue.find(1).project_id
1035     assert_equal 2, Issue.find(2).project_id
1036     # No tracker change
1037     assert_equal 1, Issue.find(1).tracker_id
1038     assert_equal 2, Issue.find(2).tracker_id
1039   end
1040  
1041   def test_bulk_move_to_another_tracker
1042     @request.session[:user_id] = 2
1043     post :move, :ids => [1, 2], :new_tracker_id => 2
1044     assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1045     assert_equal 2, Issue.find(1).tracker_id
1046     assert_equal 2, Issue.find(2).tracker_id
1047   end
1048
1049   def test_bulk_copy_to_another_project
1050     @request.session[:user_id] = 2
1051     assert_difference 'Issue.count', 2 do
1052       assert_no_difference 'Project.find(1).issues.count' do
1053         post :move, :ids => [1, 2], :new_project_id => 2, :copy_options => {:copy => '1'}
1054       end
1055     end
1056     assert_redirected_to 'projects/ecookbook/issues'
1057   end
1058
1059   context "#move via bulk copy" do
1060     should "allow not changing the issue's attributes" do
1061       @request.session[:user_id] = 2
1062       issue_before_move = Issue.find(1)
1063       assert_difference 'Issue.count', 1 do
1064         assert_no_difference 'Project.find(1).issues.count' do
1065           post :move, :ids => [1], :new_project_id => 2, :copy_options => {:copy => '1'}, :new_tracker_id => '', :assigned_to_id => '', :status_id => '', :start_date => '', :due_date => ''
1066         end
1067       end
1068       issue_after_move = Issue.first(:order => 'id desc', :conditions => {:project_id => 2})
1069       assert_equal issue_before_move.tracker_id, issue_after_move.tracker_id
1070       assert_equal issue_before_move.status_id, issue_after_move.status_id
1071       assert_equal issue_before_move.assigned_to_id, issue_after_move.assigned_to_id
1072     end
1073     
1074     should "allow changing the issue's attributes" do
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'}, :new_tracker_id => '', :assigned_to_id => 4, :status_id => 3, :start_date => '2009-12-01', :due_date => '2009-12-31'
1079         end
1080       end
1081
1082       copied_issues = Issue.all(:limit => 2, :order => 'id desc', :conditions => {:project_id => 2})
1083       assert_equal 2, copied_issues.size
1084       copied_issues.each do |issue|
1085         assert_equal 2, issue.project_id, "Project is incorrect"
1086         assert_equal 4, issue.assigned_to_id, "Assigned to is incorrect"
1087         assert_equal 3, issue.status_id, "Status is incorrect"
1088         assert_equal '2009-12-01', issue.start_date.to_s, "Start date is incorrect"
1089         assert_equal '2009-12-31', issue.due_date.to_s, "Due date is incorrect"
1090       end
1091     end
1092   end
1093   
1094   def test_copy_to_another_project_should_follow_when_needed
1095     @request.session[:user_id] = 2
1096     post :move, :ids => [1], :new_project_id => 2, :copy_options => {:copy => '1'}, :follow => '1'
1097     issue = Issue.first(:order => 'id DESC')
1098     assert_redirected_to :controller => 'issues', :action => 'show', :id => issue
1099   end
1100   
1101   def test_context_menu_one_issue
1102     @request.session[:user_id] = 2
1103     get :context_menu, :ids => [1]
1104     assert_response :success
1105     assert_template 'context_menu'
1106     assert_tag :tag => 'a', :content => 'Edit',
1107                             :attributes => { :href => '/issues/1/edit',
1108                                              :class => 'icon-edit' }
1109     assert_tag :tag => 'a', :content => 'Closed',
1110                             :attributes => { :href => '/issues/1/edit?issue%5Bstatus_id%5D=5',
1111                                              :class => '' }
1112     assert_tag :tag => 'a', :content => 'Immediate',
1113                             :attributes => { :href => '/issues/bulk_edit?ids%5B%5D=1&amp;priority_id=8',
1114                                              :class => '' }
1115     # Versions
1116     assert_tag :tag => 'a', :content => '2.0',
1117                             :attributes => { :href => '/issues/bulk_edit?fixed_version_id=3&amp;ids%5B%5D=1',
1118                                              :class => '' }
1119     assert_tag :tag => 'a', :content => 'eCookbook Subproject 1 - 2.0',
1120                             :attributes => { :href => '/issues/bulk_edit?fixed_version_id=4&amp;ids%5B%5D=1',
1121                                              :class => '' }
1122
1123     assert_tag :tag => 'a', :content => 'Dave Lopper',
1124                             :attributes => { :href => '/issues/bulk_edit?assigned_to_id=3&amp;ids%5B%5D=1',
1125                                              :class => '' }
1126     assert_tag :tag => 'a', :content => 'Duplicate',
1127                             :attributes => { :href => '/projects/ecookbook/issues/1/copy',
1128                                              :class => 'icon-duplicate' }
1129     assert_tag :tag => 'a', :content => 'Copy',
1130                             :attributes => { :href => '/issues/move?copy_options%5Bcopy%5D=t&amp;ids%5B%5D=1',
1131                                              :class => 'icon-copy' }
1132     assert_tag :tag => 'a', :content => 'Move',
1133                             :attributes => { :href => '/issues/move?ids%5B%5D=1',
1134                                              :class => 'icon-move' }
1135     assert_tag :tag => 'a', :content => 'Delete',
1136                             :attributes => { :href => '/issues/destroy?ids%5B%5D=1',
1137                                              :class => 'icon-del' }
1138   end
1139
1140   def test_context_menu_one_issue_by_anonymous
1141     get :context_menu, :ids => [1]
1142     assert_response :success
1143     assert_template 'context_menu'
1144     assert_tag :tag => 'a', :content => 'Delete',
1145                             :attributes => { :href => '#',
1146                                              :class => 'icon-del disabled' }
1147   end
1148   
1149   def test_context_menu_multiple_issues_of_same_project
1150     @request.session[:user_id] = 2
1151     get :context_menu, :ids => [1, 2]
1152     assert_response :success
1153     assert_template 'context_menu'
1154     assert_tag :tag => 'a', :content => 'Edit',
1155                             :attributes => { :href => '/issues/bulk_edit?ids%5B%5D=1&amp;ids%5B%5D=2',
1156                                              :class => 'icon-edit' }
1157     assert_tag :tag => 'a', :content => 'Immediate',
1158                             :attributes => { :href => '/issues/bulk_edit?ids%5B%5D=1&amp;ids%5B%5D=2&amp;priority_id=8',
1159                                              :class => '' }
1160     assert_tag :tag => 'a', :content => 'Dave Lopper',
1161                             :attributes => { :href => '/issues/bulk_edit?assigned_to_id=3&amp;ids%5B%5D=1&amp;ids%5B%5D=2',
1162                                              :class => '' }
1163     assert_tag :tag => 'a', :content => 'Copy',
1164                             :attributes => { :href => '/issues/move?copy_options%5Bcopy%5D=t&amp;ids%5B%5D=1&amp;ids%5B%5D=2',
1165                                              :class => 'icon-copy' }
1166     assert_tag :tag => 'a', :content => 'Move',
1167                             :attributes => { :href => '/issues/move?ids%5B%5D=1&amp;ids%5B%5D=2',
1168                                              :class => 'icon-move' }
1169     assert_tag :tag => 'a', :content => 'Delete',
1170                             :attributes => { :href => '/issues/destroy?ids%5B%5D=1&amp;ids%5B%5D=2',
1171                                              :class => 'icon-del' }
1172   end
1173
1174   def test_context_menu_multiple_issues_of_different_project
1175     @request.session[:user_id] = 2
1176     get :context_menu, :ids => [1, 2, 4]
1177     assert_response :success
1178     assert_template 'context_menu'
1179     assert_tag :tag => 'a', :content => 'Delete',
1180                             :attributes => { :href => '#',
1181                                              :class => 'icon-del disabled' }
1182   end
1183   
1184   def test_destroy_issue_with_no_time_entries
1185     assert_nil TimeEntry.find_by_issue_id(2)
1186     @request.session[:user_id] = 2
1187     post :destroy, :id => 2
1188     assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1189     assert_nil Issue.find_by_id(2)
1190   end
1191
1192   def test_destroy_issues_with_time_entries
1193     @request.session[:user_id] = 2
1194     post :destroy, :ids => [1, 3]
1195     assert_response :success
1196     assert_template 'destroy'
1197     assert_not_nil assigns(:hours)
1198     assert Issue.find_by_id(1) && Issue.find_by_id(3)
1199   end
1200
1201   def test_destroy_issues_and_destroy_time_entries
1202     @request.session[:user_id] = 2
1203     post :destroy, :ids => [1, 3], :todo => 'destroy'
1204     assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1205     assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
1206     assert_nil TimeEntry.find_by_id([1, 2])
1207   end
1208
1209   def test_destroy_issues_and_assign_time_entries_to_project
1210     @request.session[:user_id] = 2
1211     post :destroy, :ids => [1, 3], :todo => 'nullify'
1212     assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1213     assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
1214     assert_nil TimeEntry.find(1).issue_id
1215     assert_nil TimeEntry.find(2).issue_id
1216   end
1217   
1218   def test_destroy_issues_and_reassign_time_entries_to_another_issue
1219     @request.session[:user_id] = 2
1220     post :destroy, :ids => [1, 3], :todo => 'reassign', :reassign_to_id => 2
1221     assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1222     assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
1223     assert_equal 2, TimeEntry.find(1).issue_id
1224     assert_equal 2, TimeEntry.find(2).issue_id
1225   end
1226   
1227   def test_default_search_scope
1228     get :index
1229     assert_tag :div, :attributes => {:id => 'quick-search'},
1230                      :child => {:tag => 'form',
1231                                 :child => {:tag => 'input', :attributes => {:name => 'issues', :type => 'hidden', :value => '1'}}}
1232   end
1233 end