1 # Redmine - project management software
2 # Copyright (C) 2006-2008 Jean-Philippe Lang
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.
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.
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.
18 require File.dirname(__FILE__) + '/../test_helper'
19 require 'issues_controller'
21 # Re-raise errors caught by the controller.
22 class IssuesController; def rescue_action(e) raise e end; end
24 class IssuesControllerTest < ActionController::TestCase
42 :custom_fields_projects,
43 :custom_fields_trackers,
50 @controller = IssuesController.new
51 @request = ActionController::TestRequest.new
52 @response = ActionController::TestResponse.new
57 Setting.default_language = 'en'
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/
70 assert_tag :tag => 'th', :content => /Project/
73 def test_index_should_not_list_issues_when_module_disabled
74 EnabledModule.delete_all("name = 'issue_tracking' AND project_id = 1")
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/
84 def test_index_should_not_list_issues_when_module_disabled
85 EnabledModule.delete_all("name = 'issue_tracking' AND project_id = 1")
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/
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/
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/
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/
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)
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)
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)
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)
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'
164 def test_index_csv_with_project
165 Setting.default_language = 'en'
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?("#,")
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
180 get :index, :format => 'pdf'
181 assert_response :success
182 assert_not_nil assigns(:issues)
183 assert_equal 'application/pdf', @response.content_type
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
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
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
205 get :index, :sort => 'tracker,id:desc'
206 assert_response :success
208 sort_params = @request.session['issues_index_sort']
209 assert sort_params.is_a?(String)
210 assert_equal 'tracker,id:desc', sort_params
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)
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
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)
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)
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
241 def test_show_by_anonymous
243 assert_response :success
244 assert_template 'show.rhtml'
245 assert_not_nil assigns(:issue)
246 assert_equal Issue.find(1), assigns(:issue)
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/ } }
255 def test_show_by_manager
256 @request.session[:user_id] = 2
258 assert_response :success
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/ } }
272 def test_show_should_deny_anonymous_access_without_permission
273 Role.anonymous.remove_permission!(:view_issues)
275 assert_response :redirect
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
285 def test_show_should_deny_member_access_without_permission
286 Role.find(1).remove_permission!(:view_issues)
287 @request.session[:user_id] = 2
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')
299 assert_response :success
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$/ }
308 get :show, :id => 2, :format => 'atom'
309 assert_response :success
310 assert_template 'changes.rxml'
312 assert_select 'content', :text => Regexp.new(Regexp.quote('http://test.host/attachments/download/10'))
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)
324 @request.session[:user_id] = 2
325 get :new, :project_id => 1, :tracker_id => 1
326 assert_response :success
327 assert_template 'new'
329 assert_tag :tag => 'input', :attributes => { :name => 'issue[custom_field_values][2]',
330 :value => 'Default string' }
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'
339 issue = assigns(:issue)
341 assert_equal Project.find(1).trackers.first, issue.tracker
344 def test_get_new_with_no_default_status_should_display_an_error
345 @request.session[:user_id] = 2
346 IssueStatus.delete_all
348 get :new, :project_id => 1
350 assert_not_nil flash[:error]
351 assert_tag :tag => 'div', :attributes => { :class => /error/ },
352 :content => /No default issue/
355 def test_get_new_with_no_tracker_should_display_an_error
356 @request.session[:user_id] = 2
359 get :new, :project_id => 1
361 assert_not_nil flash[:error]
362 assert_tag :tag => 'div', :attributes => { :class => /error/ },
363 :content => /No tracker/
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',
373 assert_response :success
374 assert_template 'attributes'
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
384 @request.session[:user_id] = 2
385 assert_difference 'Issue.count' do
386 post :create, :project_id => 1,
387 :issue => {:tracker_id => 3,
389 :subject => 'This is the test_new issue',
390 :description => 'This is the description',
392 :estimated_hours => '',
393 :custom_field_values => {'2' => 'Value for field 2'}}
395 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
397 issue = Issue.find_by_subject('This is the test_new 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})
405 assert_equal 'Value for field 2', v.value
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',
415 assert_redirected_to :controller => 'issues', :action => 'new', :issue => {:tracker_id => 3}
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',
427 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
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)
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',
440 assert_response :success
441 assert_template 'new'
442 issue = assigns(:issue)
444 assert_equal I18n.translate('activerecord.errors.messages.invalid'), issue.errors.on(:custom_values)
447 def test_post_create_with_watchers
448 @request.session[:user_id] = 2
449 ActionMailer::Base.deliveries.clear
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',
457 :watcher_user_ids => ['2', '3']}
459 issue = Issue.find_by_subject('This is a new issue with watchers')
461 assert_redirected_to :controller => 'issues', :action => 'show', :id => issue
464 assert_equal [2, 3], issue.watcher_user_ids.sort
465 assert issue.watched_by?(User.find(3))
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)
472 def test_post_create_subissue
473 @request.session[:user_id] = 2
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}
481 issue = Issue.find_by_subject('This is a child issue')
483 assert_equal Issue.find(2), issue.parent
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',
495 :estimated_hours => '',
496 :custom_field_values => {'2' => 'Value for field 2'}}
498 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
500 assert_equal 1, ActionMailer::Base.deliveries.size
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,
509 :description => 'This is a description',
511 :custom_field_values => {'1' => 'Oracle', '2' => 'Value for field 2'}}
512 assert_response :success
513 assert_template 'new'
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',
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'}
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" }
537 context "without workflow privilege" do
539 Workflow.delete_all(["role_id = ?", Role.anonymous.id])
540 Role.anonymous.add_permission! :add_issues
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}}
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',
561 issue = Issue.last(:order => 'id')
562 assert_equal IssueStatus.default, issue.status
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',
572 issue = Issue.last(:order => 'id')
573 assert_equal IssueStatus.default, issue.status
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)
584 assert_equal orig.subject, assigns(:issue).subject
588 @request.session[:user_id] = 2
590 assert_response :success
591 assert_template 'edit'
592 assert_not_nil assigns(:issue)
593 assert_equal Issue.find(1), assigns(:issue)
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'
602 issue = assigns(:issue)
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' } }
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' } }
618 def test_update_edit_form
619 @request.session[:user_id] = 2
620 xhr :post, :update_form, :project_id => 1,
622 :issue => {:tracker_id => 2,
623 :subject => 'This is the test_new issue',
624 :description => 'This is the description',
626 assert_response :success
627 assert_template 'attributes'
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
637 def test_reply_to_issue
638 @request.session[:user_id] = 2
640 assert_response :success
641 assert_select_rjs :show, "update"
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"
651 def test_update_using_invalid_http_verbs
652 @request.session[:user_id] = 2
653 subject = 'Updated by an invalid http verb'
655 get :update, :id => 1, :issue => {:subject => subject}
656 assert_not_equal subject, Issue.find(1).subject
658 post :update, :id => 1, :issue => {:subject => subject}
659 assert_not_equal subject, Issue.find(1).subject
661 delete :update, :id => 1, :issue => {:subject => subject}
662 assert_not_equal subject, Issue.find(1).subject
665 def test_put_update_without_custom_fields_param
666 @request.session[:user_id] = 2
667 ActionMailer::Base.deliveries.clear
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'
674 assert_difference('Journal.count') do
675 assert_difference('JournalDetail.count', 2) do
676 put :update, :id => 1, :issue => {:subject => new_subject,
678 :category_id => '1' # no change
682 assert_redirected_to :action => 'show', :id => '1'
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
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}")
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
699 assert_difference('Journal.count') do
700 assert_difference('JournalDetail.count', 3) do
701 put :update, :id => 1, :issue => {:subject => 'Custom field change',
703 :category_id => '1', # no change
704 :custom_field_values => { '2' => 'New custom value' }
708 assert_redirected_to :action => 'show', :id => '1'
710 assert_equal 'New custom value', issue.custom_value_for(2).value
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")
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
724 :issue => { :status_id => 2, :assigned_to_id => 3 },
725 :notes => 'Assigned to dlopper',
726 :time_entry => { :hours => '', :comments => '', :activity_id => TimeEntryActivity.first }
728 assert_redirected_to :action => 'show', :id => '1'
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
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 })")
741 def test_put_update_with_note_only
742 notes = 'Note added by IssuesControllerTest#test_update_with_note_only'
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
753 mail = ActionMailer::Base.deliveries.last
754 assert mail.body.include?(notes)
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
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 }
766 assert_redirected_to :action => 'show', :id => '1'
768 issue = Issue.find(1)
770 j = Journal.find(:first, :order => 'id DESC')
771 assert_equal '2.5 hours added', j.notes
772 assert_equal 0, j.details.size
774 t = issue.time_entries.find_by_comments('test_put_update_with_note_and_spent_time')
776 assert_equal 2.5, t.hours
777 assert_equal spent_hours_before + 2.5, issue.spent_hours
780 def test_put_update_with_attachment_only
781 set_tmp_attachments_directory
783 # Delete all fixtured journals, a race condition can occur causing the wrong
784 # journal to get fetched in the next find.
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
799 mail = ActionMailer::Base.deliveries.last
800 assert mail.body.include?('testfile.txt')
803 def test_put_update_with_attachment_that_fails_to_save
804 set_tmp_attachments_directory
806 # Delete all fixtured journals, a race condition can occur causing the wrong
807 # journal to get fetched in the next find.
810 # Mock out the unsaved attachment
811 Attachment.any_instance.stubs(:create).returns(Attachment.new)
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]
821 end if Object.const_defined?(:Mocha)
823 def test_put_update_with_no_change
824 issue = Issue.find(1)
826 ActionMailer::Base.deliveries.clear
831 assert_redirected_to :action => 'show', :id => '1'
834 assert issue.journals.empty?
835 # No email should be sent
836 assert ActionMailer::Base.deliveries.empty?
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'
846 put :update, :id => 1, :issue => {:subject => new_subject,
848 :category_id => '1' # no change
850 assert_equal 1, ActionMailer::Base.deliveries.size
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'
857 assert_no_difference('Journal.count') do
861 :time_entry => {"comments"=>"", "activity_id"=>"", "hours"=>"2z"}
863 assert_response :success
864 assert_template 'edit'
866 assert_tag :textarea, :attributes => { :name => 'notes' },
868 assert_tag :input, :attributes => { :name => 'time_entry[hours]', :value => "2z" }
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
878 :fixed_version_id => 4
881 assert_response :redirect
883 assert_equal 4, issue.fixed_version_id
884 assert_not_equal issue.project_id, issue.fixed_version.project_id
887 def test_put_update_should_redirect_back_using_the_back_url_parameter
888 issue = Issue.find(2)
889 @request.session[:user_id] = 2
894 :fixed_version_id => 4
896 :back_url => '/issues'
898 assert_response :redirect
899 assert_redirected_to '/issues'
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
909 :fixed_version_id => 4
911 :back_url => 'http://google.com'
913 assert_response :redirect
914 assert_redirected_to :controller => 'issues', :action => 'show', :id => issue.id
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'
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]'}
929 # System wide custom field
930 assert CustomField.find(1).is_for_all?
931 assert_tag :select, :attributes => {:name => 'issue[custom_field_values][1]'}
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' => ''}}
943 # check that the issues were updated
944 assert_equal [7, 7], Issue.find_all_by_id([1, 2]).collect {|i| i.priority.id}
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
953 def test_bullk_edit_should_send_a_notification
954 @request.session[:user_id] = 2
955 ActionMailer::Base.deliveries.clear
959 :notes => 'Bulk editing',
962 :assigned_to_id => '',
963 :custom_field_values => {'2' => ''}
968 assert_equal 2, ActionMailer::Base.deliveries.size
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 => '',
980 issue = Issue.find(1)
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'}}
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
1002 def test_bulk_unassign
1003 assert_not_nil Issue.find(2).assigned_to
1004 @request.session[:user_id] = 2
1006 post :bulk_edit, :ids => [1, 2], :notes => 'Bulk unassigning', :issue => {:assigned_to_id => 'none'}
1008 # check that the issues were updated
1009 assert_nil Issue.find(2).assigned_to
1012 def test_post_bulk_edit_should_allow_fixed_version_to_be_set_to_a_subproject
1013 @request.session[:user_id] = 2
1015 post :bulk_edit, :ids => [1,2], :issue => {:fixed_version_id => 4}
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
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'
1029 assert_response :redirect
1030 assert_redirected_to '/issues'
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'
1037 assert_response :redirect
1038 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => Project.find(1).identifier
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',
1052 assert_tag :tag => 'a', :content => 'Immediate',
1053 :attributes => { :href => '/issues/bulk_edit?ids%5B%5D=1&issue%5Bpriority_id%5D=8',
1056 assert_tag :tag => 'a', :content => '2.0',
1057 :attributes => { :href => '/issues/bulk_edit?ids%5B%5D=1&issue%5Bfixed_version_id%5D=3',
1059 assert_tag :tag => 'a', :content => 'eCookbook Subproject 1 - 2.0',
1060 :attributes => { :href => '/issues/bulk_edit?ids%5B%5D=1&issue%5Bfixed_version_id%5D=4',
1063 assert_tag :tag => 'a', :content => 'Dave Lopper',
1064 :attributes => { :href => '/issues/bulk_edit?ids%5B%5D=1&issue%5Bassigned_to_id%5D=3',
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&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' }
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' }
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&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&ids%5B%5D=2&issue%5Bpriority_id%5D=8',
1100 assert_tag :tag => 'a', :content => 'Dave Lopper',
1101 :attributes => { :href => '/issues/bulk_edit?ids%5B%5D=1&ids%5B%5D=2&issue%5Bassigned_to_id%5D=3',
1103 assert_tag :tag => 'a', :content => 'Copy',
1104 :attributes => { :href => '/issues/move/new?copy_options%5Bcopy%5D=t&ids%5B%5D=1&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&ids%5B%5D=2',
1108 :class => 'icon-move' }
1109 assert_tag :tag => 'a', :content => 'Delete',
1110 :attributes => { :href => '/issues/destroy?ids%5B%5D=1&ids%5B%5D=2',
1111 :class => 'icon-del' }
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' }
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)
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)
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/}
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))
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)
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)
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])
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
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
1197 def test_default_search_scope
1199 assert_tag :div, :attributes => {:id => 'quick-search'},
1200 :child => {:tag => 'form',
1201 :child => {:tag => 'input', :attributes => {:name => 'issues', :type => 'hidden', :value => '1'}}}