OSDN Git Service

Fixes Issue#save_issue_with_child_records so that time entry do not get saved if...
[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_select 'content', :text => Regexp.new(Regexp.quote('http://test.host/attachments/download/10'))
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                             :status_id => 2,
455                             :subject => 'This is the test_new issue',
456                             :description => 'This is the description',
457                             :priority_id => 5,
458                             :estimated_hours => '',
459                             :custom_field_values => {'2' => 'Value for field 2'}}
460     end
461     assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
462     
463     issue = Issue.find_by_subject('This is the test_new issue')
464     assert_not_nil issue
465     assert_equal 2, issue.author_id
466     assert_equal 3, issue.tracker_id
467     assert_equal 2, issue.status_id
468     assert_nil issue.estimated_hours
469     v = issue.custom_values.find(:first, :conditions => {:custom_field_id => 2})
470     assert_not_nil v
471     assert_equal 'Value for field 2', v.value
472   end
473   
474   def test_post_new_and_continue
475     @request.session[:user_id] = 2
476     post :new, :project_id => 1, 
477                :issue => {:tracker_id => 3,
478                           :subject => 'This is first issue',
479                           :priority_id => 5},
480                :continue => ''
481     assert_redirected_to :controller => 'issues', :action => 'new', :issue => {:tracker_id => 3}
482   end
483   
484   def test_post_new_without_custom_fields_param
485     @request.session[:user_id] = 2
486     assert_difference 'Issue.count' do
487       post :new, :project_id => 1, 
488                  :issue => {:tracker_id => 1,
489                             :subject => 'This is the test_new issue',
490                             :description => 'This is the description',
491                             :priority_id => 5}
492     end
493     assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
494   end
495
496   def test_post_new_with_required_custom_field_and_without_custom_fields_param
497     field = IssueCustomField.find_by_name('Database')
498     field.update_attribute(:is_required, true)
499
500     @request.session[:user_id] = 2
501     post :new, :project_id => 1, 
502                :issue => {:tracker_id => 1,
503                           :subject => 'This is the test_new issue',
504                           :description => 'This is the description',
505                           :priority_id => 5}
506     assert_response :success
507     assert_template 'new'
508     issue = assigns(:issue)
509     assert_not_nil issue
510     assert_equal I18n.translate('activerecord.errors.messages.invalid'), issue.errors.on(:custom_values)
511   end
512   
513   def test_post_new_with_watchers
514     @request.session[:user_id] = 2
515     ActionMailer::Base.deliveries.clear
516     
517     assert_difference 'Watcher.count', 2 do
518       post :new, :project_id => 1, 
519                  :issue => {:tracker_id => 1,
520                             :subject => 'This is a new issue with watchers',
521                             :description => 'This is the description',
522                             :priority_id => 5,
523                             :watcher_user_ids => ['2', '3']}
524     end
525     issue = Issue.find_by_subject('This is a new issue with watchers')
526     assert_not_nil issue
527     assert_redirected_to :controller => 'issues', :action => 'show', :id => issue
528     
529     # Watchers added
530     assert_equal [2, 3], issue.watcher_user_ids.sort
531     assert issue.watched_by?(User.find(3))
532     # Watchers notified
533     mail = ActionMailer::Base.deliveries.last
534     assert_kind_of TMail::Mail, mail
535     assert [mail.bcc, mail.cc].flatten.include?(User.find(3).mail)
536   end
537   
538   def test_post_new_subissue
539     @request.session[:user_id] = 2
540     
541     assert_difference 'Issue.count' do
542       post :new, :project_id => 1, 
543                  :issue => {:tracker_id => 1,
544                             :subject => 'This is a child issue',
545                             :parent_issue_id => 2}
546     end
547     issue = Issue.find_by_subject('This is a child issue')
548     assert_not_nil issue
549     assert_equal Issue.find(2), issue.parent
550   end
551   
552   def test_post_new_should_send_a_notification
553     ActionMailer::Base.deliveries.clear
554     @request.session[:user_id] = 2
555     assert_difference 'Issue.count' do
556       post :new, :project_id => 1, 
557                  :issue => {:tracker_id => 3,
558                             :subject => 'This is the test_new issue',
559                             :description => 'This is the description',
560                             :priority_id => 5,
561                             :estimated_hours => '',
562                             :custom_field_values => {'2' => 'Value for field 2'}}
563     end
564     assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
565     
566     assert_equal 1, ActionMailer::Base.deliveries.size
567   end
568   
569   def test_post_should_preserve_fields_values_on_validation_failure
570     @request.session[:user_id] = 2
571     post :new, :project_id => 1, 
572                :issue => {:tracker_id => 1,
573                           # empty subject
574                           :subject => '',
575                           :description => 'This is a description',
576                           :priority_id => 6,
577                           :custom_field_values => {'1' => 'Oracle', '2' => 'Value for field 2'}}
578     assert_response :success
579     assert_template 'new'
580     
581     assert_tag :textarea, :attributes => { :name => 'issue[description]' },
582                           :content => 'This is a description'
583     assert_tag :select, :attributes => { :name => 'issue[priority_id]' },
584                         :child => { :tag => 'option', :attributes => { :selected => 'selected',
585                                                                        :value => '6' },
586                                                       :content => 'High' }  
587     # Custom fields
588     assert_tag :select, :attributes => { :name => 'issue[custom_field_values][1]' },
589                         :child => { :tag => 'option', :attributes => { :selected => 'selected',
590                                                                        :value => 'Oracle' },
591                                                       :content => 'Oracle' }  
592     assert_tag :input, :attributes => { :name => 'issue[custom_field_values][2]',
593                                         :value => 'Value for field 2'}
594   end
595   
596   def test_post_new_should_ignore_non_safe_attributes
597     @request.session[:user_id] = 2
598     assert_nothing_raised do
599       post :new, :project_id => 1, :issue => { :tracker => "A param can not be a Tracker" }
600     end
601   end
602   
603   context "without workflow privilege" do
604     setup do
605       Workflow.delete_all(["role_id = ?", Role.anonymous.id])
606       Role.anonymous.add_permission! :add_issues
607     end
608     
609     context "#new" do
610       should "propose default status only" do
611         get :new, :project_id => 1
612         assert_response :success
613         assert_template 'new'
614         assert_tag :tag => 'select',
615           :attributes => {:name => 'issue[status_id]'},
616           :children => {:count => 1},
617           :child => {:tag => 'option', :attributes => {:value => IssueStatus.default.id.to_s}}
618       end
619       
620       should "accept default status" do
621         assert_difference 'Issue.count' do
622           post :new, :project_id => 1, 
623                      :issue => {:tracker_id => 1,
624                                 :subject => 'This is an issue',
625                                 :status_id => 1}
626         end
627         issue = Issue.last(:order => 'id')
628         assert_equal IssueStatus.default, issue.status
629       end
630       
631       should "ignore unauthorized status" do
632         assert_difference 'Issue.count' do
633           post :new, :project_id => 1, 
634                      :issue => {:tracker_id => 1,
635                                 :subject => 'This is an issue',
636                                 :status_id => 3}
637         end
638         issue = Issue.last(:order => 'id')
639         assert_equal IssueStatus.default, issue.status
640       end
641     end
642   end
643   
644   def test_copy_issue
645     @request.session[:user_id] = 2
646     get :new, :project_id => 1, :copy_from => 1
647     assert_template 'new'
648     assert_not_nil assigns(:issue)
649     orig = Issue.find(1)
650     assert_equal orig.subject, assigns(:issue).subject
651   end
652   
653   def test_get_edit
654     @request.session[:user_id] = 2
655     get :edit, :id => 1
656     assert_response :success
657     assert_template 'edit'
658     assert_not_nil assigns(:issue)
659     assert_equal Issue.find(1), assigns(:issue)
660   end
661   
662   def test_get_edit_with_params
663     @request.session[:user_id] = 2
664     get :edit, :id => 1, :issue => { :status_id => 5, :priority_id => 7 }
665     assert_response :success
666     assert_template 'edit'
667     
668     issue = assigns(:issue)
669     assert_not_nil issue
670     
671     assert_equal 5, issue.status_id
672     assert_tag :select, :attributes => { :name => 'issue[status_id]' },
673                         :child => { :tag => 'option', 
674                                     :content => 'Closed',
675                                     :attributes => { :selected => 'selected' } }
676                                     
677     assert_equal 7, issue.priority_id
678     assert_tag :select, :attributes => { :name => 'issue[priority_id]' },
679                         :child => { :tag => 'option', 
680                                     :content => 'Urgent',
681                                     :attributes => { :selected => 'selected' } }
682   end
683
684   def test_update_edit_form
685     @request.session[:user_id] = 2
686     xhr :post, :update_form, :project_id => 1,
687                              :id => 1,
688                              :issue => {:tracker_id => 2, 
689                                         :subject => 'This is the test_new issue',
690                                         :description => 'This is the description',
691                                         :priority_id => 5}
692     assert_response :success
693     assert_template 'attributes'
694     
695     issue = assigns(:issue)
696     assert_kind_of Issue, issue
697     assert_equal 1, issue.id
698     assert_equal 1, issue.project_id
699     assert_equal 2, issue.tracker_id
700     assert_equal 'This is the test_new issue', issue.subject
701   end
702   
703   def test_reply_to_issue
704     @request.session[:user_id] = 2
705     get :reply, :id => 1
706     assert_response :success
707     assert_select_rjs :show, "update"
708   end
709
710   def test_reply_to_note
711     @request.session[:user_id] = 2
712     get :reply, :id => 1, :journal_id => 2
713     assert_response :success
714     assert_select_rjs :show, "update"
715   end
716
717   def test_update_using_invalid_http_verbs
718     @request.session[:user_id] = 2
719     subject = 'Updated by an invalid http verb'
720
721     get :update, :id => 1, :issue => {:subject => subject}
722     assert_not_equal subject, Issue.find(1).subject
723
724     post :update, :id => 1, :issue => {:subject => subject}
725     assert_not_equal subject, Issue.find(1).subject
726
727     delete :update, :id => 1, :issue => {:subject => subject}
728     assert_not_equal subject, Issue.find(1).subject
729   end
730
731   def test_put_update_without_custom_fields_param
732     @request.session[:user_id] = 2
733     ActionMailer::Base.deliveries.clear
734     
735     issue = Issue.find(1)
736     assert_equal '125', issue.custom_value_for(2).value
737     old_subject = issue.subject
738     new_subject = 'Subject modified by IssuesControllerTest#test_post_edit'
739     
740     assert_difference('Journal.count') do
741       assert_difference('JournalDetail.count', 2) do
742         put :update, :id => 1, :issue => {:subject => new_subject,
743                                          :priority_id => '6',
744                                          :category_id => '1' # no change
745                                         }
746       end
747     end
748     assert_redirected_to :action => 'show', :id => '1'
749     issue.reload
750     assert_equal new_subject, issue.subject
751     # Make sure custom fields were not cleared
752     assert_equal '125', issue.custom_value_for(2).value
753     
754     mail = ActionMailer::Base.deliveries.last
755     assert_kind_of TMail::Mail, mail
756     assert mail.subject.starts_with?("[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}]")
757     assert mail.body.include?("Subject changed from #{old_subject} to #{new_subject}")
758   end
759   
760   def test_put_update_with_custom_field_change
761     @request.session[:user_id] = 2
762     issue = Issue.find(1)
763     assert_equal '125', issue.custom_value_for(2).value
764     
765     assert_difference('Journal.count') do
766       assert_difference('JournalDetail.count', 3) do
767         put :update, :id => 1, :issue => {:subject => 'Custom field change',
768                                          :priority_id => '6',
769                                          :category_id => '1', # no change
770                                          :custom_field_values => { '2' => 'New custom value' }
771                                         }
772       end
773     end
774     assert_redirected_to :action => 'show', :id => '1'
775     issue.reload
776     assert_equal 'New custom value', issue.custom_value_for(2).value
777     
778     mail = ActionMailer::Base.deliveries.last
779     assert_kind_of TMail::Mail, mail
780     assert mail.body.include?("Searchable field changed from 125 to New custom value")
781   end
782   
783   def test_put_update_with_status_and_assignee_change
784     issue = Issue.find(1)
785     assert_equal 1, issue.status_id
786     @request.session[:user_id] = 2
787     assert_difference('TimeEntry.count', 0) do
788       put :update,
789            :id => 1,
790            :issue => { :status_id => 2, :assigned_to_id => 3 },
791            :notes => 'Assigned to dlopper',
792            :time_entry => { :hours => '', :comments => '', :activity_id => TimeEntryActivity.first }
793     end
794     assert_redirected_to :action => 'show', :id => '1'
795     issue.reload
796     assert_equal 2, issue.status_id
797     j = Journal.find(:first, :order => 'id DESC')
798     assert_equal 'Assigned to dlopper', j.notes
799     assert_equal 2, j.details.size
800     
801     mail = ActionMailer::Base.deliveries.last
802     assert mail.body.include?("Status changed from New to Assigned")
803     # subject should contain the new status
804     assert mail.subject.include?("(#{ IssueStatus.find(2).name })")
805   end
806   
807   def test_put_update_with_note_only
808     notes = 'Note added by IssuesControllerTest#test_update_with_note_only'
809     # anonymous user
810     put :update,
811          :id => 1,
812          :notes => notes
813     assert_redirected_to :action => 'show', :id => '1'
814     j = Journal.find(:first, :order => 'id DESC')
815     assert_equal notes, j.notes
816     assert_equal 0, j.details.size
817     assert_equal User.anonymous, j.user
818     
819     mail = ActionMailer::Base.deliveries.last
820     assert mail.body.include?(notes)
821   end
822   
823   def test_put_update_with_note_and_spent_time
824     @request.session[:user_id] = 2
825     spent_hours_before = Issue.find(1).spent_hours
826     assert_difference('TimeEntry.count') do
827       put :update,
828            :id => 1,
829            :notes => '2.5 hours added',
830            :time_entry => { :hours => '2.5', :comments => 'test_put_update_with_note_and_spent_time', :activity_id => TimeEntryActivity.first }
831     end
832     assert_redirected_to :action => 'show', :id => '1'
833     
834     issue = Issue.find(1)
835     
836     j = Journal.find(:first, :order => 'id DESC')
837     assert_equal '2.5 hours added', j.notes
838     assert_equal 0, j.details.size
839     
840     t = issue.time_entries.find_by_comments('test_put_update_with_note_and_spent_time')
841     assert_not_nil t
842     assert_equal 2.5, t.hours
843     assert_equal spent_hours_before + 2.5, issue.spent_hours
844   end
845   
846   def test_put_update_with_attachment_only
847     set_tmp_attachments_directory
848     
849     # Delete all fixtured journals, a race condition can occur causing the wrong
850     # journal to get fetched in the next find.
851     Journal.delete_all
852
853     # anonymous user
854     put :update,
855          :id => 1,
856          :notes => '',
857          :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
858     assert_redirected_to :action => 'show', :id => '1'
859     j = Issue.find(1).journals.find(:first, :order => 'id DESC')
860     assert j.notes.blank?
861     assert_equal 1, j.details.size
862     assert_equal 'testfile.txt', j.details.first.value
863     assert_equal User.anonymous, j.user
864     
865     mail = ActionMailer::Base.deliveries.last
866     assert mail.body.include?('testfile.txt')
867   end
868
869   def test_put_update_with_attachment_that_fails_to_save
870     set_tmp_attachments_directory
871     
872     # Delete all fixtured journals, a race condition can occur causing the wrong
873     # journal to get fetched in the next find.
874     Journal.delete_all
875
876     # Mock out the unsaved attachment
877     Attachment.any_instance.stubs(:create).returns(Attachment.new)
878     
879     # anonymous user
880     put :update,
881          :id => 1,
882          :notes => '',
883          :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
884     assert_redirected_to :action => 'show', :id => '1'
885     assert_equal '1 file(s) could not be saved.', flash[:warning]
886
887   end if Object.const_defined?(:Mocha)
888
889   def test_put_update_with_no_change
890     issue = Issue.find(1)
891     issue.journals.clear
892     ActionMailer::Base.deliveries.clear
893     
894     put :update,
895          :id => 1,
896          :notes => ''
897     assert_redirected_to :action => 'show', :id => '1'
898     
899     issue.reload
900     assert issue.journals.empty?
901     # No email should be sent
902     assert ActionMailer::Base.deliveries.empty?
903   end
904
905   def test_put_update_should_send_a_notification
906     @request.session[:user_id] = 2
907     ActionMailer::Base.deliveries.clear
908     issue = Issue.find(1)
909     old_subject = issue.subject
910     new_subject = 'Subject modified by IssuesControllerTest#test_post_edit'
911     
912     put :update, :id => 1, :issue => {:subject => new_subject,
913                                      :priority_id => '6',
914                                      :category_id => '1' # no change
915                                     }
916     assert_equal 1, ActionMailer::Base.deliveries.size
917   end
918   
919   def test_put_update_with_invalid_spent_time
920     @request.session[:user_id] = 2
921     notes = 'Note added by IssuesControllerTest#test_post_edit_with_invalid_spent_time'
922     
923     assert_no_difference('Journal.count') do
924       put :update,
925            :id => 1,
926            :notes => notes,
927            :time_entry => {"comments"=>"", "activity_id"=>"", "hours"=>"2z"}
928     end
929     assert_response :success
930     assert_template 'edit'
931     
932     assert_tag :textarea, :attributes => { :name => 'notes' },
933                           :content => notes
934     assert_tag :input, :attributes => { :name => 'time_entry[hours]', :value => "2z" }
935   end
936   
937   def test_put_update_should_allow_fixed_version_to_be_set_to_a_subproject
938     issue = Issue.find(2)
939     @request.session[:user_id] = 2
940
941     put :update,
942          :id => issue.id,
943          :issue => {
944            :fixed_version_id => 4
945          }
946
947     assert_response :redirect
948     issue.reload
949     assert_equal 4, issue.fixed_version_id
950     assert_not_equal issue.project_id, issue.fixed_version.project_id
951   end
952
953   def test_put_update_should_redirect_back_using_the_back_url_parameter
954     issue = Issue.find(2)
955     @request.session[:user_id] = 2
956
957     put :update,
958          :id => issue.id,
959          :issue => {
960            :fixed_version_id => 4
961          },
962          :back_url => '/issues'
963
964     assert_response :redirect
965     assert_redirected_to '/issues'
966   end
967   
968   def test_put_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host
969     issue = Issue.find(2)
970     @request.session[:user_id] = 2
971
972     put :update,
973          :id => issue.id,
974          :issue => {
975            :fixed_version_id => 4
976          },
977          :back_url => 'http://google.com'
978
979     assert_response :redirect
980     assert_redirected_to :controller => 'issues', :action => 'show', :id => issue.id
981   end
982   
983   def test_put_update_stale_issue
984     issue = Issue.find(2)
985     @request.session[:user_id] = 2
986
987     assert_no_difference 'Journal.count' do
988       assert_no_difference 'TimeEntry.count' do
989         assert_no_difference 'Attachment.count' do
990           put :update,
991                 :id => issue.id,
992                 :issue => {
993                   :fixed_version_id => 4,
994                   :lock_version => (issue.lock_version - 1)
995                 },
996                 :notes => '',
997                 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}},
998                 :time_entry => { :hours => '2.5', :comments => '', :activity_id => TimeEntryActivity.first }
999         end
1000       end
1001     end
1002     
1003     assert_response :success
1004     assert_template 'edit'
1005     assert_tag :tag => 'div', :attributes => { :id => 'errorExplanation' },
1006                               :content => /Data has been updated by another user/
1007   end
1008   
1009   def test_get_bulk_edit
1010     @request.session[:user_id] = 2
1011     get :bulk_edit, :ids => [1, 2]
1012     assert_response :success
1013     assert_template 'bulk_edit'
1014     
1015     # Project specific custom field, date type
1016     field = CustomField.find(9)
1017     assert !field.is_for_all?
1018     assert_equal 'date', field.field_format
1019     assert_tag :input, :attributes => {:name => 'issue[custom_field_values][9]'}
1020     
1021     # System wide custom field
1022     assert CustomField.find(1).is_for_all?
1023     assert_tag :select, :attributes => {:name => 'issue[custom_field_values][1]'}
1024   end
1025
1026   def test_bulk_edit
1027     @request.session[:user_id] = 2
1028     # update issues priority
1029     post :bulk_edit, :ids => [1, 2], :notes => 'Bulk editing',
1030                                      :issue => {:priority_id => 7,
1031                                                 :assigned_to_id => '',
1032                                                 :custom_field_values => {'2' => ''}}
1033                                      
1034     assert_response 302
1035     # check that the issues were updated
1036     assert_equal [7, 7], Issue.find_all_by_id([1, 2]).collect {|i| i.priority.id}
1037     
1038     issue = Issue.find(1)
1039     journal = issue.journals.find(:first, :order => 'created_on DESC')
1040     assert_equal '125', issue.custom_value_for(2).value
1041     assert_equal 'Bulk editing', journal.notes
1042     assert_equal 1, journal.details.size
1043   end
1044
1045   def test_bullk_edit_should_send_a_notification
1046     @request.session[:user_id] = 2
1047     ActionMailer::Base.deliveries.clear
1048     post(:bulk_edit,
1049          {
1050            :ids => [1, 2],
1051            :notes => 'Bulk editing',
1052            :issue => {
1053              :priority_id => 7,
1054              :assigned_to_id => '',
1055              :custom_field_values => {'2' => ''}
1056            }
1057          })
1058
1059     assert_response 302
1060     assert_equal 2, ActionMailer::Base.deliveries.size
1061   end
1062
1063   def test_bulk_edit_status
1064     @request.session[:user_id] = 2
1065     # update issues priority
1066     post :bulk_edit, :ids => [1, 2], :notes => 'Bulk editing status',
1067                                      :issue => {:priority_id => '',
1068                                                 :assigned_to_id => '',
1069                                                 :status_id => '5'}
1070                                      
1071     assert_response 302
1072     issue = Issue.find(1)
1073     assert issue.closed?
1074   end
1075
1076   def test_bulk_edit_custom_field
1077     @request.session[:user_id] = 2
1078     # update issues priority
1079     post :bulk_edit, :ids => [1, 2], :notes => 'Bulk editing custom field',
1080                                      :issue => {:priority_id => '',
1081                                                 :assigned_to_id => '',
1082                                                 :custom_field_values => {'2' => '777'}}
1083                                      
1084     assert_response 302
1085     
1086     issue = Issue.find(1)
1087     journal = issue.journals.find(:first, :order => 'created_on DESC')
1088     assert_equal '777', issue.custom_value_for(2).value
1089     assert_equal 1, journal.details.size
1090     assert_equal '125', journal.details.first.old_value
1091     assert_equal '777', journal.details.first.value
1092   end
1093
1094   def test_bulk_unassign
1095     assert_not_nil Issue.find(2).assigned_to
1096     @request.session[:user_id] = 2
1097     # unassign issues
1098   post :bulk_edit, :ids => [1, 2], :notes => 'Bulk unassigning', :issue => {:assigned_to_id => 'none'}
1099     assert_response 302
1100     # check that the issues were updated
1101     assert_nil Issue.find(2).assigned_to
1102   end
1103   
1104   def test_post_bulk_edit_should_allow_fixed_version_to_be_set_to_a_subproject
1105     @request.session[:user_id] = 2
1106
1107     post :bulk_edit, :ids => [1,2], :issue => {:fixed_version_id => 4}
1108
1109     assert_response :redirect
1110     issues = Issue.find([1,2])
1111     issues.each do |issue|
1112       assert_equal 4, issue.fixed_version_id
1113       assert_not_equal issue.project_id, issue.fixed_version.project_id
1114     end
1115   end
1116
1117   def test_post_bulk_edit_should_redirect_back_using_the_back_url_parameter
1118     @request.session[:user_id] = 2
1119     post :bulk_edit, :ids => [1,2], :back_url => '/issues'
1120
1121     assert_response :redirect
1122     assert_redirected_to '/issues'
1123   end
1124
1125   def test_post_bulk_edit_should_not_redirect_back_using_the_back_url_parameter_off_the_host
1126     @request.session[:user_id] = 2
1127     post :bulk_edit, :ids => [1,2], :back_url => 'http://google.com'
1128
1129     assert_response :redirect
1130     assert_redirected_to :controller => 'issues', :action => 'index', :project_id => Project.find(1).identifier
1131   end
1132
1133   def test_move_one_issue_to_another_project
1134     @request.session[:user_id] = 2
1135     post :move, :id => 1, :new_project_id => 2, :tracker_id => '', :assigned_to_id => '', :status_id => '', :start_date => '', :due_date => ''
1136     assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1137     assert_equal 2, Issue.find(1).project_id
1138   end
1139
1140   def test_move_one_issue_to_another_project_should_follow_when_needed
1141     @request.session[:user_id] = 2
1142     post :move, :id => 1, :new_project_id => 2, :follow => '1'
1143     assert_redirected_to '/issues/1'
1144   end
1145
1146   def test_bulk_move_to_another_project
1147     @request.session[:user_id] = 2
1148     post :move, :ids => [1, 2], :new_project_id => 2
1149     assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1150     # Issues moved to project 2
1151     assert_equal 2, Issue.find(1).project_id
1152     assert_equal 2, Issue.find(2).project_id
1153     # No tracker change
1154     assert_equal 1, Issue.find(1).tracker_id
1155     assert_equal 2, Issue.find(2).tracker_id
1156   end
1157  
1158   def test_bulk_move_to_another_tracker
1159     @request.session[:user_id] = 2
1160     post :move, :ids => [1, 2], :new_tracker_id => 2
1161     assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1162     assert_equal 2, Issue.find(1).tracker_id
1163     assert_equal 2, Issue.find(2).tracker_id
1164   end
1165
1166   def test_bulk_copy_to_another_project
1167     @request.session[:user_id] = 2
1168     assert_difference 'Issue.count', 2 do
1169       assert_no_difference 'Project.find(1).issues.count' do
1170         post :move, :ids => [1, 2], :new_project_id => 2, :copy_options => {:copy => '1'}
1171       end
1172     end
1173     assert_redirected_to 'projects/ecookbook/issues'
1174   end
1175
1176   context "#move via bulk copy" do
1177     should "allow not changing the issue's attributes" do
1178       @request.session[:user_id] = 2
1179       issue_before_move = Issue.find(1)
1180       assert_difference 'Issue.count', 1 do
1181         assert_no_difference 'Project.find(1).issues.count' do
1182           post :move, :ids => [1], :new_project_id => 2, :copy_options => {:copy => '1'}, :new_tracker_id => '', :assigned_to_id => '', :status_id => '', :start_date => '', :due_date => ''
1183         end
1184       end
1185       issue_after_move = Issue.first(:order => 'id desc', :conditions => {:project_id => 2})
1186       assert_equal issue_before_move.tracker_id, issue_after_move.tracker_id
1187       assert_equal issue_before_move.status_id, issue_after_move.status_id
1188       assert_equal issue_before_move.assigned_to_id, issue_after_move.assigned_to_id
1189     end
1190     
1191     should "allow changing the issue's attributes" do
1192       @request.session[:user_id] = 2
1193       assert_difference 'Issue.count', 2 do
1194         assert_no_difference 'Project.find(1).issues.count' do
1195           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'
1196         end
1197       end
1198
1199       copied_issues = Issue.all(:limit => 2, :order => 'id desc', :conditions => {:project_id => 2})
1200       assert_equal 2, copied_issues.size
1201       copied_issues.each do |issue|
1202         assert_equal 2, issue.project_id, "Project is incorrect"
1203         assert_equal 4, issue.assigned_to_id, "Assigned to is incorrect"
1204         assert_equal 3, issue.status_id, "Status is incorrect"
1205         assert_equal '2009-12-01', issue.start_date.to_s, "Start date is incorrect"
1206         assert_equal '2009-12-31', issue.due_date.to_s, "Due date is incorrect"
1207       end
1208     end
1209   end
1210   
1211   def test_copy_to_another_project_should_follow_when_needed
1212     @request.session[:user_id] = 2
1213     post :move, :ids => [1], :new_project_id => 2, :copy_options => {:copy => '1'}, :follow => '1'
1214     issue = Issue.first(:order => 'id DESC')
1215     assert_redirected_to :controller => 'issues', :action => 'show', :id => issue
1216   end
1217   
1218   def test_context_menu_one_issue
1219     @request.session[:user_id] = 2
1220     get :context_menu, :ids => [1]
1221     assert_response :success
1222     assert_template 'context_menu'
1223     assert_tag :tag => 'a', :content => 'Edit',
1224                             :attributes => { :href => '/issues/1/edit',
1225                                              :class => 'icon-edit' }
1226     assert_tag :tag => 'a', :content => 'Closed',
1227                             :attributes => { :href => '/issues/1/edit?issue%5Bstatus_id%5D=5',
1228                                              :class => '' }
1229     assert_tag :tag => 'a', :content => 'Immediate',
1230                             :attributes => { :href => '/issues/bulk_edit?ids%5B%5D=1&amp;issue%5Bpriority_id%5D=8',
1231                                              :class => '' }
1232     # Versions
1233     assert_tag :tag => 'a', :content => '2.0',
1234                             :attributes => { :href => '/issues/bulk_edit?ids%5B%5D=1&amp;issue%5Bfixed_version_id%5D=3',
1235                                              :class => '' }
1236     assert_tag :tag => 'a', :content => 'eCookbook Subproject 1 - 2.0',
1237                             :attributes => { :href => '/issues/bulk_edit?ids%5B%5D=1&amp;issue%5Bfixed_version_id%5D=4',
1238                                              :class => '' }
1239
1240     assert_tag :tag => 'a', :content => 'Dave Lopper',
1241                             :attributes => { :href => '/issues/bulk_edit?ids%5B%5D=1&amp;issue%5Bassigned_to_id%5D=3',
1242                                              :class => '' }
1243     assert_tag :tag => 'a', :content => 'Duplicate',
1244                             :attributes => { :href => '/projects/ecookbook/issues/1/copy',
1245                                              :class => 'icon-duplicate' }
1246     assert_tag :tag => 'a', :content => 'Copy',
1247                             :attributes => { :href => '/issues/move?copy_options%5Bcopy%5D=t&amp;ids%5B%5D=1',
1248                                              :class => 'icon-copy' }
1249     assert_tag :tag => 'a', :content => 'Move',
1250                             :attributes => { :href => '/issues/move?ids%5B%5D=1',
1251                                              :class => 'icon-move' }
1252     assert_tag :tag => 'a', :content => 'Delete',
1253                             :attributes => { :href => '/issues/destroy?ids%5B%5D=1',
1254                                              :class => 'icon-del' }
1255   end
1256
1257   def test_context_menu_one_issue_by_anonymous
1258     get :context_menu, :ids => [1]
1259     assert_response :success
1260     assert_template 'context_menu'
1261     assert_tag :tag => 'a', :content => 'Delete',
1262                             :attributes => { :href => '#',
1263                                              :class => 'icon-del disabled' }
1264   end
1265   
1266   def test_context_menu_multiple_issues_of_same_project
1267     @request.session[:user_id] = 2
1268     get :context_menu, :ids => [1, 2]
1269     assert_response :success
1270     assert_template 'context_menu'
1271     assert_tag :tag => 'a', :content => 'Edit',
1272                             :attributes => { :href => '/issues/bulk_edit?ids%5B%5D=1&amp;ids%5B%5D=2',
1273                                              :class => 'icon-edit' }
1274     assert_tag :tag => 'a', :content => 'Immediate',
1275                             :attributes => { :href => '/issues/bulk_edit?ids%5B%5D=1&amp;ids%5B%5D=2&amp;issue%5Bpriority_id%5D=8',
1276                                              :class => '' }
1277     assert_tag :tag => 'a', :content => 'Dave Lopper',
1278                             :attributes => { :href => '/issues/bulk_edit?ids%5B%5D=1&amp;ids%5B%5D=2&amp;issue%5Bassigned_to_id%5D=3',
1279                                              :class => '' }
1280     assert_tag :tag => 'a', :content => 'Copy',
1281                             :attributes => { :href => '/issues/move?copy_options%5Bcopy%5D=t&amp;ids%5B%5D=1&amp;ids%5B%5D=2',
1282                                              :class => 'icon-copy' }
1283     assert_tag :tag => 'a', :content => 'Move',
1284                             :attributes => { :href => '/issues/move?ids%5B%5D=1&amp;ids%5B%5D=2',
1285                                              :class => 'icon-move' }
1286     assert_tag :tag => 'a', :content => 'Delete',
1287                             :attributes => { :href => '/issues/destroy?ids%5B%5D=1&amp;ids%5B%5D=2',
1288                                              :class => 'icon-del' }
1289   end
1290
1291   def test_context_menu_multiple_issues_of_different_project
1292     @request.session[:user_id] = 2
1293     get :context_menu, :ids => [1, 2, 4]
1294     assert_response :success
1295     assert_template 'context_menu'
1296     assert_tag :tag => 'a', :content => 'Delete',
1297                             :attributes => { :href => '#',
1298                                              :class => 'icon-del disabled' }
1299   end
1300   
1301   def test_preview_new_issue
1302     @request.session[:user_id] = 2
1303     post :preview, :project_id => '1', :issue => {:description => 'Foo'}
1304     assert_response :success
1305     assert_template 'preview'
1306     assert_not_nil assigns(:description)
1307   end
1308                               
1309   def test_preview_notes
1310     @request.session[:user_id] = 2
1311     post :preview, :project_id => '1', :id => 1, :issue => {:description => Issue.find(1).description}, :notes => 'Foo'
1312     assert_response :success
1313     assert_template 'preview'
1314     assert_not_nil assigns(:notes)
1315   end
1316
1317   def test_auto_complete_routing
1318     assert_routing(
1319       {:method => :get, :path => '/issues/auto_complete'},
1320       :controller => 'issues', :action => 'auto_complete'
1321     )
1322   end
1323   
1324   def test_auto_complete_should_not_be_case_sensitive
1325     get :auto_complete, :project_id => 'ecookbook', :q => 'ReCiPe'
1326     assert_response :success
1327     assert_not_nil assigns(:issues)
1328     assert assigns(:issues).detect {|issue| issue.subject.match /recipe/}
1329   end
1330   
1331   def test_auto_complete_should_return_issue_with_given_id
1332     get :auto_complete, :project_id => 'subproject1', :q => '13'
1333     assert_response :success
1334     assert_not_nil assigns(:issues)
1335     assert assigns(:issues).include?(Issue.find(13))
1336   end
1337   
1338   def test_destroy_routing
1339     assert_recognizes( #TODO: use DELETE on issue URI (need to change forms)
1340       {:controller => 'issues', :action => 'destroy', :id => '1'},
1341       {:method => :post, :path => '/issues/1/destroy'}
1342     )
1343   end
1344   
1345   def test_destroy_issue_with_no_time_entries
1346     assert_nil TimeEntry.find_by_issue_id(2)
1347     @request.session[:user_id] = 2
1348     post :destroy, :id => 2
1349     assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1350     assert_nil Issue.find_by_id(2)
1351   end
1352
1353   def test_destroy_issues_with_time_entries
1354     @request.session[:user_id] = 2
1355     post :destroy, :ids => [1, 3]
1356     assert_response :success
1357     assert_template 'destroy'
1358     assert_not_nil assigns(:hours)
1359     assert Issue.find_by_id(1) && Issue.find_by_id(3)
1360   end
1361
1362   def test_destroy_issues_and_destroy_time_entries
1363     @request.session[:user_id] = 2
1364     post :destroy, :ids => [1, 3], :todo => 'destroy'
1365     assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1366     assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
1367     assert_nil TimeEntry.find_by_id([1, 2])
1368   end
1369
1370   def test_destroy_issues_and_assign_time_entries_to_project
1371     @request.session[:user_id] = 2
1372     post :destroy, :ids => [1, 3], :todo => 'nullify'
1373     assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1374     assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
1375     assert_nil TimeEntry.find(1).issue_id
1376     assert_nil TimeEntry.find(2).issue_id
1377   end
1378   
1379   def test_destroy_issues_and_reassign_time_entries_to_another_issue
1380     @request.session[:user_id] = 2
1381     post :destroy, :ids => [1, 3], :todo => 'reassign', :reassign_to_id => 2
1382     assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1383     assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
1384     assert_equal 2, TimeEntry.find(1).issue_id
1385     assert_equal 2, TimeEntry.find(2).issue_id
1386   end
1387   
1388   def test_default_search_scope
1389     get :index
1390     assert_tag :div, :attributes => {:id => 'quick-search'},
1391                      :child => {:tag => 'form',
1392                                 :child => {:tag => 'input', :attributes => {:name => 'issues', :type => 'hidden', :value => '1'}}}
1393   end
1394 end