OSDN Git Service

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