OSDN Git Service

Added observers to watch model objects for mail delivery instead of calling Mailer.
[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 < Test::Unit::TestCase
25   fixtures :projects,
26            :users,
27            :roles,
28            :members,
29            :issues,
30            :issue_statuses,
31            :versions,
32            :trackers,
33            :projects_trackers,
34            :issue_categories,
35            :enabled_modules,
36            :enumerations,
37            :attachments,
38            :workflows,
39            :custom_fields,
40            :custom_values,
41            :custom_fields_trackers,
42            :time_entries,
43            :journals,
44            :journal_details
45   
46   def setup
47     @controller = IssuesController.new
48     @request    = ActionController::TestRequest.new
49     @response   = ActionController::TestResponse.new
50     User.current = nil
51   end
52   
53   def test_index_routing
54     assert_routing(
55       {:method => :get, :path => '/issues'},
56       :controller => 'issues', :action => 'index'
57     )
58   end
59
60   def test_index
61     Setting.default_language = 'en'
62     
63     get :index
64     assert_response :success
65     assert_template 'index.rhtml'
66     assert_not_nil assigns(:issues)
67     assert_nil assigns(:project)
68     assert_tag :tag => 'a', :content => /Can't print recipes/
69     assert_tag :tag => 'a', :content => /Subproject issue/
70     # private projects hidden
71     assert_no_tag :tag => 'a', :content => /Issue of a private subproject/
72     assert_no_tag :tag => 'a', :content => /Issue on project 2/
73     # project column
74     assert_tag :tag => 'th', :content => /Project/
75   end
76   
77   def test_index_should_not_list_issues_when_module_disabled
78     EnabledModule.delete_all("name = 'issue_tracking' AND project_id = 1")
79     get :index
80     assert_response :success
81     assert_template 'index.rhtml'
82     assert_not_nil assigns(:issues)
83     assert_nil assigns(:project)
84     assert_no_tag :tag => 'a', :content => /Can't print recipes/
85     assert_tag :tag => 'a', :content => /Subproject issue/
86   end
87
88   def test_index_with_project_routing
89     assert_routing(
90       {:method => :get, :path => '/projects/23/issues'},
91       :controller => 'issues', :action => 'index', :project_id => '23'
92     )
93   end
94   
95   def test_index_should_not_list_issues_when_module_disabled
96     EnabledModule.delete_all("name = 'issue_tracking' AND project_id = 1")
97     get :index
98     assert_response :success
99     assert_template 'index.rhtml'
100     assert_not_nil assigns(:issues)
101     assert_nil assigns(:project)
102     assert_no_tag :tag => 'a', :content => /Can't print recipes/
103     assert_tag :tag => 'a', :content => /Subproject issue/
104   end
105
106   def test_index_with_project_routing
107     assert_routing(
108       {:method => :get, :path => 'projects/23/issues'},
109       :controller => 'issues', :action => 'index', :project_id => '23'
110     )
111   end
112   
113   def test_index_with_project
114     Setting.display_subprojects_issues = 0
115     get :index, :project_id => 1
116     assert_response :success
117     assert_template 'index.rhtml'
118     assert_not_nil assigns(:issues)
119     assert_tag :tag => 'a', :content => /Can't print recipes/
120     assert_no_tag :tag => 'a', :content => /Subproject issue/
121   end
122   
123   def test_index_with_project_and_subprojects
124     Setting.display_subprojects_issues = 1
125     get :index, :project_id => 1
126     assert_response :success
127     assert_template 'index.rhtml'
128     assert_not_nil assigns(:issues)
129     assert_tag :tag => 'a', :content => /Can't print recipes/
130     assert_tag :tag => 'a', :content => /Subproject issue/
131     assert_no_tag :tag => 'a', :content => /Issue of a private subproject/
132   end
133   
134   def test_index_with_project_and_subprojects_should_show_private_subprojects
135     @request.session[:user_id] = 2
136     Setting.display_subprojects_issues = 1
137     get :index, :project_id => 1
138     assert_response :success
139     assert_template 'index.rhtml'
140     assert_not_nil assigns(:issues)
141     assert_tag :tag => 'a', :content => /Can't print recipes/
142     assert_tag :tag => 'a', :content => /Subproject issue/
143     assert_tag :tag => 'a', :content => /Issue of a private subproject/
144   end
145   
146   def test_index_with_project_routing_formatted
147     assert_routing(
148       {:method => :get, :path => 'projects/23/issues.pdf'},
149       :controller => 'issues', :action => 'index', :project_id => '23', :format => 'pdf'
150     )
151     assert_routing(
152       {:method => :get, :path => 'projects/23/issues.atom'},
153       :controller => 'issues', :action => 'index', :project_id => '23', :format => 'atom'
154     )
155   end
156   
157   def test_index_with_project_and_filter
158     get :index, :project_id => 1, :set_filter => 1
159     assert_response :success
160     assert_template 'index.rhtml'
161     assert_not_nil assigns(:issues)
162   end
163   
164   def test_index_csv_with_project
165     get :index, :format => 'csv'
166     assert_response :success
167     assert_not_nil assigns(:issues)
168     assert_equal 'text/csv', @response.content_type
169
170     get :index, :project_id => 1, :format => 'csv'
171     assert_response :success
172     assert_not_nil assigns(:issues)
173     assert_equal 'text/csv', @response.content_type
174   end
175   
176   def test_index_formatted
177     assert_routing(
178       {:method => :get, :path => 'issues.pdf'},
179       :controller => 'issues', :action => 'index', :format => 'pdf'
180     )
181     assert_routing(
182       {:method => :get, :path => 'issues.atom'},
183       :controller => 'issues', :action => 'index', :format => 'atom'
184     )
185   end
186   
187   def test_index_pdf
188     get :index, :format => 'pdf'
189     assert_response :success
190     assert_not_nil assigns(:issues)
191     assert_equal 'application/pdf', @response.content_type
192     
193     get :index, :project_id => 1, :format => 'pdf'
194     assert_response :success
195     assert_not_nil assigns(:issues)
196     assert_equal 'application/pdf', @response.content_type
197   end
198   
199   def test_index_sort
200     get :index, :sort => 'tracker,id:desc'
201     assert_response :success
202     
203     sort_params = @request.session['issues_index_sort']
204     assert sort_params.is_a?(String)
205     assert_equal 'tracker,id:desc', sort_params
206     
207     issues = assigns(:issues)
208     assert_not_nil issues
209     assert !issues.empty?
210     assert_equal issues.sort {|a,b| a.tracker == b.tracker ? b.id <=> a.id : a.tracker <=> b.tracker }.collect(&:id), issues.collect(&:id)
211   end
212
213   def test_gantt
214     get :gantt, :project_id => 1
215     assert_response :success
216     assert_template 'gantt.rhtml'
217     assert_not_nil assigns(:gantt)
218     events = assigns(:gantt).events
219     assert_not_nil events
220     # Issue with start and due dates
221     i = Issue.find(1)
222     assert_not_nil i.due_date
223     assert events.include?(Issue.find(1))
224     # Issue with without due date but targeted to a version with date
225     i = Issue.find(2)
226     assert_nil i.due_date
227     assert events.include?(i)
228   end
229
230   def test_cross_project_gantt
231     get :gantt
232     assert_response :success
233     assert_template 'gantt.rhtml'
234     assert_not_nil assigns(:gantt)
235     events = assigns(:gantt).events
236     assert_not_nil events
237   end
238
239   def test_gantt_export_to_pdf
240     get :gantt, :project_id => 1, :format => 'pdf'
241     assert_response :success
242     assert_equal 'application/pdf', @response.content_type
243     assert @response.body.starts_with?('%PDF')
244     assert_not_nil assigns(:gantt)
245   end
246
247   def test_cross_project_gantt_export_to_pdf
248     get :gantt, :format => 'pdf'
249     assert_response :success
250     assert_equal 'application/pdf', @response.content_type
251     assert @response.body.starts_with?('%PDF')
252     assert_not_nil assigns(:gantt)
253   end
254   
255   if Object.const_defined?(:Magick)
256     def test_gantt_image
257       get :gantt, :project_id => 1, :format => 'png'
258       assert_response :success
259       assert_equal 'image/png', @response.content_type
260     end
261   else
262     puts "RMagick not installed. Skipping tests !!!"
263   end
264   
265   def test_calendar
266     get :calendar, :project_id => 1
267     assert_response :success
268     assert_template 'calendar'
269     assert_not_nil assigns(:calendar)
270   end
271   
272   def test_cross_project_calendar
273     get :calendar
274     assert_response :success
275     assert_template 'calendar'
276     assert_not_nil assigns(:calendar)
277   end
278   
279   def test_changes
280     get :changes, :project_id => 1
281     assert_response :success
282     assert_not_nil assigns(:journals)
283     assert_equal 'application/atom+xml', @response.content_type
284   end
285   
286   def test_show_routing
287     assert_routing(
288       {:method => :get, :path => '/issues/64'},
289       :controller => 'issues', :action => 'show', :id => '64'
290     )
291   end
292   
293   def test_show_routing_formatted
294     assert_routing(
295       {:method => :get, :path => '/issues/2332.pdf'},
296       :controller => 'issues', :action => 'show', :id => '2332', :format => 'pdf'
297     )
298     assert_routing(
299       {:method => :get, :path => '/issues/23123.atom'},
300       :controller => 'issues', :action => 'show', :id => '23123', :format => 'atom'
301     )
302   end
303   
304   def test_show_by_anonymous
305     get :show, :id => 1
306     assert_response :success
307     assert_template 'show.rhtml'
308     assert_not_nil assigns(:issue)
309     assert_equal Issue.find(1), assigns(:issue)
310     
311     # anonymous role is allowed to add a note
312     assert_tag :tag => 'form',
313                :descendant => { :tag => 'fieldset',
314                                 :child => { :tag => 'legend', 
315                                             :content => /Notes/ } }
316   end
317   
318   def test_show_by_manager
319     @request.session[:user_id] = 2
320     get :show, :id => 1
321     assert_response :success
322     
323     assert_tag :tag => 'form',
324                :descendant => { :tag => 'fieldset',
325                                 :child => { :tag => 'legend', 
326                                             :content => /Change properties/ } },
327                :descendant => { :tag => 'fieldset',
328                                 :child => { :tag => 'legend', 
329                                             :content => /Log time/ } },
330                :descendant => { :tag => 'fieldset',
331                                 :child => { :tag => 'legend', 
332                                             :content => /Notes/ } }
333   end
334   
335   def test_show_should_not_disclose_relations_to_invisible_issues
336     Setting.cross_project_issue_relations = '1'
337     IssueRelation.create!(:issue_from => Issue.find(1), :issue_to => Issue.find(2), :relation_type => 'relates')
338     # Relation to a private project issue
339     IssueRelation.create!(:issue_from => Issue.find(1), :issue_to => Issue.find(4), :relation_type => 'relates')
340     
341     get :show, :id => 1
342     assert_response :success
343     
344     assert_tag :div, :attributes => { :id => 'relations' },
345                      :descendant => { :tag => 'a', :content => /#2$/ }
346     assert_no_tag :div, :attributes => { :id => 'relations' },
347                         :descendant => { :tag => 'a', :content => /#4$/ }
348   end
349   
350   def test_new_routing
351     assert_routing(
352       {:method => :get, :path => '/projects/1/issues/new'},
353       :controller => 'issues', :action => 'new', :project_id => '1'
354     )
355     assert_recognizes(
356       {:controller => 'issues', :action => 'new', :project_id => '1'},
357       {:method => :post, :path => '/projects/1/issues'}
358     )
359   end
360
361   def test_show_export_to_pdf
362     get :show, :id => 3, :format => 'pdf'
363     assert_response :success
364     assert_equal 'application/pdf', @response.content_type
365     assert @response.body.starts_with?('%PDF')
366     assert_not_nil assigns(:issue)
367   end
368
369   def test_get_new
370     @request.session[:user_id] = 2
371     get :new, :project_id => 1, :tracker_id => 1
372     assert_response :success
373     assert_template 'new'
374     
375     assert_tag :tag => 'input', :attributes => { :name => 'issue[custom_field_values][2]',
376                                                  :value => 'Default string' }
377   end
378
379   def test_get_new_without_tracker_id
380     @request.session[:user_id] = 2
381     get :new, :project_id => 1
382     assert_response :success
383     assert_template 'new'
384     
385     issue = assigns(:issue)
386     assert_not_nil issue
387     assert_equal Project.find(1).trackers.first, issue.tracker
388   end
389   
390   def test_get_new_with_no_default_status_should_display_an_error
391     @request.session[:user_id] = 2
392     IssueStatus.delete_all
393     
394     get :new, :project_id => 1
395     assert_response 500
396     assert_not_nil flash[:error]
397     assert_tag :tag => 'div', :attributes => { :class => /error/ },
398                               :content => /No default issue/
399   end
400   
401   def test_get_new_with_no_tracker_should_display_an_error
402     @request.session[:user_id] = 2
403     Tracker.delete_all
404     
405     get :new, :project_id => 1
406     assert_response 500
407     assert_not_nil flash[:error]
408     assert_tag :tag => 'div', :attributes => { :class => /error/ },
409                               :content => /No tracker/
410   end
411   
412   def test_update_new_form
413     @request.session[:user_id] = 2
414     xhr :post, :new, :project_id => 1,
415                      :issue => {:tracker_id => 2, 
416                                 :subject => 'This is the test_new issue',
417                                 :description => 'This is the description',
418                                 :priority_id => 5}
419     assert_response :success
420     assert_template 'new'
421   end 
422   
423   def test_post_new
424     @request.session[:user_id] = 2
425     post :new, :project_id => 1, 
426                :issue => {:tracker_id => 3,
427                           :subject => 'This is the test_new issue',
428                           :description => 'This is the description',
429                           :priority_id => 5,
430                           :estimated_hours => '',
431                           :custom_field_values => {'2' => 'Value for field 2'}}
432     assert_redirected_to :action => 'show'
433     
434     issue = Issue.find_by_subject('This is the test_new issue')
435     assert_not_nil issue
436     assert_equal 2, issue.author_id
437     assert_equal 3, issue.tracker_id
438     assert_nil issue.estimated_hours
439     v = issue.custom_values.find(:first, :conditions => {:custom_field_id => 2})
440     assert_not_nil v
441     assert_equal 'Value for field 2', v.value
442   end
443   
444   def test_post_new_and_continue
445     @request.session[:user_id] = 2
446     post :new, :project_id => 1, 
447                :issue => {:tracker_id => 3,
448                           :subject => 'This is first issue',
449                           :priority_id => 5},
450                :continue => ''
451     assert_redirected_to :controller => 'issues', :action => 'new', :tracker_id => 3
452   end
453   
454   def test_post_new_without_custom_fields_param
455     @request.session[:user_id] = 2
456     post :new, :project_id => 1, 
457                :issue => {:tracker_id => 1,
458                           :subject => 'This is the test_new issue',
459                           :description => 'This is the description',
460                           :priority_id => 5}
461     assert_redirected_to :action => 'show'
462   end
463
464   def test_post_new_with_required_custom_field_and_without_custom_fields_param
465     field = IssueCustomField.find_by_name('Database')
466     field.update_attribute(:is_required, true)
467
468     @request.session[:user_id] = 2
469     post :new, :project_id => 1, 
470                :issue => {:tracker_id => 1,
471                           :subject => 'This is the test_new issue',
472                           :description => 'This is the description',
473                           :priority_id => 5}
474     assert_response :success
475     assert_template 'new'
476     issue = assigns(:issue)
477     assert_not_nil issue
478     assert_equal I18n.translate('activerecord.errors.messages.invalid'), issue.errors.on(:custom_values)
479   end
480   
481   def test_post_new_with_watchers
482     @request.session[:user_id] = 2
483     ActionMailer::Base.deliveries.clear
484     
485     assert_difference 'Watcher.count', 2 do
486       post :new, :project_id => 1, 
487                  :issue => {:tracker_id => 1,
488                             :subject => 'This is a new issue with watchers',
489                             :description => 'This is the description',
490                             :priority_id => 5,
491                             :watcher_user_ids => ['2', '3']}
492     end
493     issue = Issue.find_by_subject('This is a new issue with watchers')
494     assert_not_nil issue
495     assert_redirected_to :controller => 'issues', :action => 'show', :id => issue
496     
497     # Watchers added
498     assert_equal [2, 3], issue.watcher_user_ids.sort
499     assert issue.watched_by?(User.find(3))
500     # Watchers notified
501     mail = ActionMailer::Base.deliveries.last
502     assert_kind_of TMail::Mail, mail
503     assert [mail.bcc, mail.cc].flatten.include?(User.find(3).mail)
504   end
505   
506   def test_post_new_should_send_a_notification
507     ActionMailer::Base.deliveries.clear
508     @request.session[:user_id] = 2
509     post :new, :project_id => 1, 
510                :issue => {:tracker_id => 3,
511                           :subject => 'This is the test_new issue',
512                           :description => 'This is the description',
513                           :priority_id => 5,
514                           :estimated_hours => '',
515                           :custom_field_values => {'2' => 'Value for field 2'}}
516     assert_redirected_to :action => 'show'
517     
518     assert_equal 1, ActionMailer::Base.deliveries.size
519   end
520   
521   def test_post_should_preserve_fields_values_on_validation_failure
522     @request.session[:user_id] = 2
523     post :new, :project_id => 1, 
524                :issue => {:tracker_id => 1,
525                           # empty subject
526                           :subject => '',
527                           :description => 'This is a description',
528                           :priority_id => 6,
529                           :custom_field_values => {'1' => 'Oracle', '2' => 'Value for field 2'}}
530     assert_response :success
531     assert_template 'new'
532     
533     assert_tag :textarea, :attributes => { :name => 'issue[description]' },
534                           :content => 'This is a description'
535     assert_tag :select, :attributes => { :name => 'issue[priority_id]' },
536                         :child => { :tag => 'option', :attributes => { :selected => 'selected',
537                                                                        :value => '6' },
538                                                       :content => 'High' }  
539     # Custom fields
540     assert_tag :select, :attributes => { :name => 'issue[custom_field_values][1]' },
541                         :child => { :tag => 'option', :attributes => { :selected => 'selected',
542                                                                        :value => 'Oracle' },
543                                                       :content => 'Oracle' }  
544     assert_tag :input, :attributes => { :name => 'issue[custom_field_values][2]',
545                                         :value => 'Value for field 2'}
546   end
547   
548   def test_copy_routing
549     assert_routing(
550       {:method => :get, :path => '/projects/world_domination/issues/567/copy'},
551       :controller => 'issues', :action => 'new', :project_id => 'world_domination', :copy_from => '567'
552     )
553   end
554   
555   def test_copy_issue
556     @request.session[:user_id] = 2
557     get :new, :project_id => 1, :copy_from => 1
558     assert_template 'new'
559     assert_not_nil assigns(:issue)
560     orig = Issue.find(1)
561     assert_equal orig.subject, assigns(:issue).subject
562   end
563   
564   def test_edit_routing
565     assert_routing(
566       {:method => :get, :path => '/issues/1/edit'},
567       :controller => 'issues', :action => 'edit', :id => '1'
568     )
569     assert_recognizes( #TODO: use a PUT on the issue URI isntead, need to adjust form
570       {:controller => 'issues', :action => 'edit', :id => '1'},
571       {:method => :post, :path => '/issues/1/edit'}
572     )
573   end
574   
575   def test_get_edit
576     @request.session[:user_id] = 2
577     get :edit, :id => 1
578     assert_response :success
579     assert_template 'edit'
580     assert_not_nil assigns(:issue)
581     assert_equal Issue.find(1), assigns(:issue)
582   end
583   
584   def test_get_edit_with_params
585     @request.session[:user_id] = 2
586     get :edit, :id => 1, :issue => { :status_id => 5, :priority_id => 7 }
587     assert_response :success
588     assert_template 'edit'
589     
590     issue = assigns(:issue)
591     assert_not_nil issue
592     
593     assert_equal 5, issue.status_id
594     assert_tag :select, :attributes => { :name => 'issue[status_id]' },
595                         :child => { :tag => 'option', 
596                                     :content => 'Closed',
597                                     :attributes => { :selected => 'selected' } }
598                                     
599     assert_equal 7, issue.priority_id
600     assert_tag :select, :attributes => { :name => 'issue[priority_id]' },
601                         :child => { :tag => 'option', 
602                                     :content => 'Urgent',
603                                     :attributes => { :selected => 'selected' } }
604   end
605   
606   def test_reply_routing
607     assert_routing(
608       {:method => :post, :path => '/issues/1/quoted'},
609       :controller => 'issues', :action => 'reply', :id => '1'
610     )
611   end
612   
613   def test_reply_to_issue
614     @request.session[:user_id] = 2
615     get :reply, :id => 1
616     assert_response :success
617     assert_select_rjs :show, "update"
618   end
619
620   def test_reply_to_note
621     @request.session[:user_id] = 2
622     get :reply, :id => 1, :journal_id => 2
623     assert_response :success
624     assert_select_rjs :show, "update"
625   end
626
627   def test_post_edit_without_custom_fields_param
628     @request.session[:user_id] = 2
629     ActionMailer::Base.deliveries.clear
630     
631     issue = Issue.find(1)
632     assert_equal '125', issue.custom_value_for(2).value
633     old_subject = issue.subject
634     new_subject = 'Subject modified by IssuesControllerTest#test_post_edit'
635     
636     assert_difference('Journal.count') do
637       assert_difference('JournalDetail.count', 2) do
638         post :edit, :id => 1, :issue => {:subject => new_subject,
639                                          :priority_id => '6',
640                                          :category_id => '1' # no change
641                                         }
642       end
643     end
644     assert_redirected_to :action => 'show', :id => '1'
645     issue.reload
646     assert_equal new_subject, issue.subject
647     # Make sure custom fields were not cleared
648     assert_equal '125', issue.custom_value_for(2).value
649     
650     mail = ActionMailer::Base.deliveries.last
651     assert_kind_of TMail::Mail, mail
652     assert mail.subject.starts_with?("[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}]")
653     assert mail.body.include?("Subject changed from #{old_subject} to #{new_subject}")
654   end
655   
656   def test_post_edit_with_custom_field_change
657     @request.session[:user_id] = 2
658     issue = Issue.find(1)
659     assert_equal '125', issue.custom_value_for(2).value
660     
661     assert_difference('Journal.count') do
662       assert_difference('JournalDetail.count', 3) do
663         post :edit, :id => 1, :issue => {:subject => 'Custom field change',
664                                          :priority_id => '6',
665                                          :category_id => '1', # no change
666                                          :custom_field_values => { '2' => 'New custom value' }
667                                         }
668       end
669     end
670     assert_redirected_to :action => 'show', :id => '1'
671     issue.reload
672     assert_equal 'New custom value', issue.custom_value_for(2).value
673     
674     mail = ActionMailer::Base.deliveries.last
675     assert_kind_of TMail::Mail, mail
676     assert mail.body.include?("Searchable field changed from 125 to New custom value")
677   end
678   
679   def test_post_edit_with_status_and_assignee_change
680     issue = Issue.find(1)
681     assert_equal 1, issue.status_id
682     @request.session[:user_id] = 2
683     assert_difference('TimeEntry.count', 0) do
684       post :edit,
685            :id => 1,
686            :issue => { :status_id => 2, :assigned_to_id => 3 },
687            :notes => 'Assigned to dlopper',
688            :time_entry => { :hours => '', :comments => '', :activity_id => Enumeration.activities.first }
689     end
690     assert_redirected_to :action => 'show', :id => '1'
691     issue.reload
692     assert_equal 2, issue.status_id
693     j = issue.journals.find(:first, :order => 'id DESC')
694     assert_equal 'Assigned to dlopper', j.notes
695     assert_equal 2, j.details.size
696     
697     mail = ActionMailer::Base.deliveries.last
698     assert mail.body.include?("Status changed from New to Assigned")
699   end
700   
701   def test_post_edit_with_note_only
702     notes = 'Note added by IssuesControllerTest#test_update_with_note_only'
703     # anonymous user
704     post :edit,
705          :id => 1,
706          :notes => notes
707     assert_redirected_to :action => 'show', :id => '1'
708     j = Issue.find(1).journals.find(:first, :order => 'id DESC')
709     assert_equal notes, j.notes
710     assert_equal 0, j.details.size
711     assert_equal User.anonymous, j.user
712     
713     mail = ActionMailer::Base.deliveries.last
714     assert mail.body.include?(notes)
715   end
716   
717   def test_post_edit_with_note_and_spent_time
718     @request.session[:user_id] = 2
719     spent_hours_before = Issue.find(1).spent_hours
720     assert_difference('TimeEntry.count') do
721       post :edit,
722            :id => 1,
723            :notes => '2.5 hours added',
724            :time_entry => { :hours => '2.5', :comments => '', :activity_id => Enumeration.activities.first }
725     end
726     assert_redirected_to :action => 'show', :id => '1'
727     
728     issue = Issue.find(1)
729     
730     j = issue.journals.find(:first, :order => 'id DESC')
731     assert_equal '2.5 hours added', j.notes
732     assert_equal 0, j.details.size
733     
734     t = issue.time_entries.find(:first, :order => 'id DESC')
735     assert_not_nil t
736     assert_equal 2.5, t.hours
737     assert_equal spent_hours_before + 2.5, issue.spent_hours
738   end
739   
740   def test_post_edit_with_attachment_only
741     set_tmp_attachments_directory
742     
743     # Delete all fixtured journals, a race condition can occur causing the wrong
744     # journal to get fetched in the next find.
745     Journal.delete_all
746
747     # anonymous user
748     post :edit,
749          :id => 1,
750          :notes => '',
751          :attachments => {'1' => {'file' => test_uploaded_file('testfile.txt', 'text/plain')}}
752     assert_redirected_to :action => 'show', :id => '1'
753     j = Issue.find(1).journals.find(:first, :order => 'id DESC')
754     assert j.notes.blank?
755     assert_equal 1, j.details.size
756     assert_equal 'testfile.txt', j.details.first.value
757     assert_equal User.anonymous, j.user
758     
759     mail = ActionMailer::Base.deliveries.last
760     assert mail.body.include?('testfile.txt')
761   end
762   
763   def test_post_edit_with_no_change
764     issue = Issue.find(1)
765     issue.journals.clear
766     ActionMailer::Base.deliveries.clear
767     
768     post :edit,
769          :id => 1,
770          :notes => ''
771     assert_redirected_to :action => 'show', :id => '1'
772     
773     issue.reload
774     assert issue.journals.empty?
775     # No email should be sent
776     assert ActionMailer::Base.deliveries.empty?
777   end
778
779   def test_post_edit_should_send_a_notification
780     @request.session[:user_id] = 2
781     ActionMailer::Base.deliveries.clear
782     issue = Issue.find(1)
783     old_subject = issue.subject
784     new_subject = 'Subject modified by IssuesControllerTest#test_post_edit'
785     
786     post :edit, :id => 1, :issue => {:subject => new_subject,
787                                      :priority_id => '6',
788                                      :category_id => '1' # no change
789                                     }
790     assert_equal 1, ActionMailer::Base.deliveries.size
791   end
792   
793   def test_post_edit_with_invalid_spent_time
794     @request.session[:user_id] = 2
795     notes = 'Note added by IssuesControllerTest#test_post_edit_with_invalid_spent_time'
796     
797     assert_no_difference('Journal.count') do
798       post :edit,
799            :id => 1,
800            :notes => notes,
801            :time_entry => {"comments"=>"", "activity_id"=>"", "hours"=>"2z"}
802     end
803     assert_response :success
804     assert_template 'edit'
805     
806     assert_tag :textarea, :attributes => { :name => 'notes' },
807                           :content => notes
808     assert_tag :input, :attributes => { :name => 'time_entry[hours]', :value => "2z" }
809   end
810
811   def test_bulk_edit
812     @request.session[:user_id] = 2
813     # update issues priority
814     post :bulk_edit, :ids => [1, 2], :priority_id => 7,
815                                      :assigned_to_id => '',
816                                      :custom_field_values => {'2' => ''},
817                                      :notes => 'Bulk editing'
818     assert_response 302
819     # check that the issues were updated
820     assert_equal [7, 7], Issue.find_all_by_id([1, 2]).collect {|i| i.priority.id}
821     
822     issue = Issue.find(1)
823     journal = issue.journals.find(:first, :order => 'created_on DESC')
824     assert_equal '125', issue.custom_value_for(2).value
825     assert_equal 'Bulk editing', journal.notes
826     assert_equal 1, journal.details.size
827   end
828
829   def test_bullk_edit_should_send_a_notification
830     @request.session[:user_id] = 2
831     ActionMailer::Base.deliveries.clear
832     post(:bulk_edit,
833          {
834            :ids => [1, 2],
835            :priority_id => 7,
836            :assigned_to_id => '',
837            :custom_field_values => {'2' => ''},
838            :notes => 'Bulk editing'
839          })
840
841     assert_response 302
842     assert_equal 2, ActionMailer::Base.deliveries.size
843   end
844
845   def test_bulk_edit_custom_field
846     @request.session[:user_id] = 2
847     # update issues priority
848     post :bulk_edit, :ids => [1, 2], :priority_id => '',
849                                      :assigned_to_id => '',
850                                      :custom_field_values => {'2' => '777'},
851                                      :notes => 'Bulk editing custom field'
852     assert_response 302
853     
854     issue = Issue.find(1)
855     journal = issue.journals.find(:first, :order => 'created_on DESC')
856     assert_equal '777', issue.custom_value_for(2).value
857     assert_equal 1, journal.details.size
858     assert_equal '125', journal.details.first.old_value
859     assert_equal '777', journal.details.first.value
860   end
861
862   def test_bulk_unassign
863     assert_not_nil Issue.find(2).assigned_to
864     @request.session[:user_id] = 2
865     # unassign issues
866     post :bulk_edit, :ids => [1, 2], :notes => 'Bulk unassigning', :assigned_to_id => 'none'
867     assert_response 302
868     # check that the issues were updated
869     assert_nil Issue.find(2).assigned_to
870   end
871   
872   def test_move_routing
873     assert_routing(
874       {:method => :get, :path => '/issues/1/move'},
875       :controller => 'issues', :action => 'move', :id => '1'
876     )
877     assert_recognizes(
878       {:controller => 'issues', :action => 'move', :id => '1'},
879       {:method => :post, :path => '/issues/1/move'}
880     )
881   end
882   
883   def test_move_one_issue_to_another_project
884     @request.session[:user_id] = 1
885     post :move, :id => 1, :new_project_id => 2
886     assert_redirected_to :action => 'index', :project_id => 'ecookbook'
887     assert_equal 2, Issue.find(1).project_id
888   end
889
890   def test_bulk_move_to_another_project
891     @request.session[:user_id] = 1
892     post :move, :ids => [1, 2], :new_project_id => 2
893     assert_redirected_to :action => 'index', :project_id => 'ecookbook'
894     # Issues moved to project 2
895     assert_equal 2, Issue.find(1).project_id
896     assert_equal 2, Issue.find(2).project_id
897     # No tracker change
898     assert_equal 1, Issue.find(1).tracker_id
899     assert_equal 2, Issue.find(2).tracker_id
900   end
901  
902   def test_bulk_move_to_another_tracker
903     @request.session[:user_id] = 1
904     post :move, :ids => [1, 2], :new_tracker_id => 2
905     assert_redirected_to :action => 'index', :project_id => 'ecookbook'
906     assert_equal 2, Issue.find(1).tracker_id
907     assert_equal 2, Issue.find(2).tracker_id
908   end
909
910   def test_bulk_copy_to_another_project
911     @request.session[:user_id] = 1
912     assert_difference 'Issue.count', 2 do
913       assert_no_difference 'Project.find(1).issues.count' do
914         post :move, :ids => [1, 2], :new_project_id => 2, :copy_options => {:copy => '1'}
915       end
916     end
917     assert_redirected_to 'projects/ecookbook/issues'
918   end
919   
920   def test_context_menu_one_issue
921     @request.session[:user_id] = 2
922     get :context_menu, :ids => [1]
923     assert_response :success
924     assert_template 'context_menu'
925     assert_tag :tag => 'a', :content => 'Edit',
926                             :attributes => { :href => '/issues/1/edit',
927                                              :class => 'icon-edit' }
928     assert_tag :tag => 'a', :content => 'Closed',
929                             :attributes => { :href => '/issues/1/edit?issue%5Bstatus_id%5D=5',
930                                              :class => '' }
931     assert_tag :tag => 'a', :content => 'Immediate',
932                             :attributes => { :href => '/issues/bulk_edit?ids%5B%5D=1&amp;priority_id=8',
933                                              :class => '' }
934     assert_tag :tag => 'a', :content => 'Dave Lopper',
935                             :attributes => { :href => '/issues/bulk_edit?assigned_to_id=3&amp;ids%5B%5D=1',
936                                              :class => '' }
937     assert_tag :tag => 'a', :content => 'Copy',
938                             :attributes => { :href => '/projects/ecookbook/issues/1/copy',
939                                              :class => 'icon-copy' }
940     assert_tag :tag => 'a', :content => 'Move',
941                             :attributes => { :href => '/issues/move?ids%5B%5D=1',
942                                              :class => 'icon-move' }
943     assert_tag :tag => 'a', :content => 'Delete',
944                             :attributes => { :href => '/issues/destroy?ids%5B%5D=1',
945                                              :class => 'icon-del' }
946   end
947
948   def test_context_menu_one_issue_by_anonymous
949     get :context_menu, :ids => [1]
950     assert_response :success
951     assert_template 'context_menu'
952     assert_tag :tag => 'a', :content => 'Delete',
953                             :attributes => { :href => '#',
954                                              :class => 'icon-del disabled' }
955   end
956   
957   def test_context_menu_multiple_issues_of_same_project
958     @request.session[:user_id] = 2
959     get :context_menu, :ids => [1, 2]
960     assert_response :success
961     assert_template 'context_menu'
962     assert_tag :tag => 'a', :content => 'Edit',
963                             :attributes => { :href => '/issues/bulk_edit?ids%5B%5D=1&amp;ids%5B%5D=2',
964                                              :class => 'icon-edit' }
965     assert_tag :tag => 'a', :content => 'Immediate',
966                             :attributes => { :href => '/issues/bulk_edit?ids%5B%5D=1&amp;ids%5B%5D=2&amp;priority_id=8',
967                                              :class => '' }
968     assert_tag :tag => 'a', :content => 'Dave Lopper',
969                             :attributes => { :href => '/issues/bulk_edit?assigned_to_id=3&amp;ids%5B%5D=1&amp;ids%5B%5D=2',
970                                              :class => '' }
971     assert_tag :tag => 'a', :content => 'Move',
972                             :attributes => { :href => '/issues/move?ids%5B%5D=1&amp;ids%5B%5D=2',
973                                              :class => 'icon-move' }
974     assert_tag :tag => 'a', :content => 'Delete',
975                             :attributes => { :href => '/issues/destroy?ids%5B%5D=1&amp;ids%5B%5D=2',
976                                              :class => 'icon-del' }
977   end
978
979   def test_context_menu_multiple_issues_of_different_project
980     @request.session[:user_id] = 2
981     get :context_menu, :ids => [1, 2, 4]
982     assert_response :success
983     assert_template 'context_menu'
984     assert_tag :tag => 'a', :content => 'Delete',
985                             :attributes => { :href => '#',
986                                              :class => 'icon-del disabled' }
987   end
988   
989   def test_destroy_routing
990     assert_recognizes( #TODO: use DELETE on issue URI (need to change forms)
991       {:controller => 'issues', :action => 'destroy', :id => '1'},
992       {:method => :post, :path => '/issues/1/destroy'}
993     )
994   end
995   
996   def test_destroy_issue_with_no_time_entries
997     assert_nil TimeEntry.find_by_issue_id(2)
998     @request.session[:user_id] = 2
999     post :destroy, :id => 2
1000     assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1001     assert_nil Issue.find_by_id(2)
1002   end
1003
1004   def test_destroy_issues_with_time_entries
1005     @request.session[:user_id] = 2
1006     post :destroy, :ids => [1, 3]
1007     assert_response :success
1008     assert_template 'destroy'
1009     assert_not_nil assigns(:hours)
1010     assert Issue.find_by_id(1) && Issue.find_by_id(3)
1011   end
1012
1013   def test_destroy_issues_and_destroy_time_entries
1014     @request.session[:user_id] = 2
1015     post :destroy, :ids => [1, 3], :todo => 'destroy'
1016     assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1017     assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
1018     assert_nil TimeEntry.find_by_id([1, 2])
1019   end
1020
1021   def test_destroy_issues_and_assign_time_entries_to_project
1022     @request.session[:user_id] = 2
1023     post :destroy, :ids => [1, 3], :todo => 'nullify'
1024     assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1025     assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
1026     assert_nil TimeEntry.find(1).issue_id
1027     assert_nil TimeEntry.find(2).issue_id
1028   end
1029   
1030   def test_destroy_issues_and_reassign_time_entries_to_another_issue
1031     @request.session[:user_id] = 2
1032     post :destroy, :ids => [1, 3], :todo => 'reassign', :reassign_to_id => 2
1033     assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1034     assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
1035     assert_equal 2, TimeEntry.find(1).issue_id
1036     assert_equal 2, TimeEntry.find(2).issue_id
1037   end
1038 end