##// END OF EJS Templates
Merged r3333 from trunk....
Jean-Philippe Lang -
r3220:3d7cb0f40e09
parent child
Show More
@@ -1,541 +1,542
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2008 Jean-Philippe Lang
2 # Copyright (C) 2006-2008 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
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
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.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 class IssuesController < ApplicationController
18 class IssuesController < ApplicationController
19 menu_item :new_issue, :only => :new
19 menu_item :new_issue, :only => :new
20 default_search_scope :issues
20 default_search_scope :issues
21
21
22 before_filter :find_issue, :only => [:show, :edit, :reply]
22 before_filter :find_issue, :only => [:show, :edit, :reply]
23 before_filter :find_issues, :only => [:bulk_edit, :move, :destroy]
23 before_filter :find_issues, :only => [:bulk_edit, :move, :destroy]
24 before_filter :find_project, :only => [:new, :update_form, :preview]
24 before_filter :find_project, :only => [:new, :update_form, :preview]
25 before_filter :authorize, :except => [:index, :changes, :gantt, :calendar, :preview, :context_menu]
25 before_filter :authorize, :except => [:index, :changes, :gantt, :calendar, :preview, :context_menu]
26 before_filter :find_optional_project, :only => [:index, :changes, :gantt, :calendar]
26 before_filter :find_optional_project, :only => [:index, :changes, :gantt, :calendar]
27 accept_key_auth :index, :show, :changes
27 accept_key_auth :index, :show, :changes
28
28
29 rescue_from Query::StatementInvalid, :with => :query_statement_invalid
29 rescue_from Query::StatementInvalid, :with => :query_statement_invalid
30
30
31 helper :journals
31 helper :journals
32 helper :projects
32 helper :projects
33 include ProjectsHelper
33 include ProjectsHelper
34 helper :custom_fields
34 helper :custom_fields
35 include CustomFieldsHelper
35 include CustomFieldsHelper
36 helper :issue_relations
36 helper :issue_relations
37 include IssueRelationsHelper
37 include IssueRelationsHelper
38 helper :watchers
38 helper :watchers
39 include WatchersHelper
39 include WatchersHelper
40 helper :attachments
40 helper :attachments
41 include AttachmentsHelper
41 include AttachmentsHelper
42 helper :queries
42 helper :queries
43 include QueriesHelper
43 helper :sort
44 helper :sort
44 include SortHelper
45 include SortHelper
45 include IssuesHelper
46 include IssuesHelper
46 helper :timelog
47 helper :timelog
47 include Redmine::Export::PDF
48 include Redmine::Export::PDF
48
49
49 verify :method => :post,
50 verify :method => :post,
50 :only => :destroy,
51 :only => :destroy,
51 :render => { :nothing => true, :status => :method_not_allowed }
52 :render => { :nothing => true, :status => :method_not_allowed }
52
53
53 def index
54 def index
54 retrieve_query
55 retrieve_query
55 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
56 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
56 sort_update({'id' => "#{Issue.table_name}.id"}.merge(@query.available_columns.inject({}) {|h, c| h[c.name.to_s] = c.sortable; h}))
57 sort_update({'id' => "#{Issue.table_name}.id"}.merge(@query.available_columns.inject({}) {|h, c| h[c.name.to_s] = c.sortable; h}))
57
58
58 if @query.valid?
59 if @query.valid?
59 limit = per_page_option
60 limit = per_page_option
60 respond_to do |format|
61 respond_to do |format|
61 format.html { }
62 format.html { }
62 format.atom { limit = Setting.feeds_limit.to_i }
63 format.atom { limit = Setting.feeds_limit.to_i }
63 format.csv { limit = Setting.issues_export_limit.to_i }
64 format.csv { limit = Setting.issues_export_limit.to_i }
64 format.pdf { limit = Setting.issues_export_limit.to_i }
65 format.pdf { limit = Setting.issues_export_limit.to_i }
65 end
66 end
66
67
67 @issue_count = @query.issue_count
68 @issue_count = @query.issue_count
68 @issue_pages = Paginator.new self, @issue_count, limit, params['page']
69 @issue_pages = Paginator.new self, @issue_count, limit, params['page']
69 @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version],
70 @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version],
70 :order => sort_clause,
71 :order => sort_clause,
71 :offset => @issue_pages.current.offset,
72 :offset => @issue_pages.current.offset,
72 :limit => limit)
73 :limit => limit)
73 @issue_count_by_group = @query.issue_count_by_group
74 @issue_count_by_group = @query.issue_count_by_group
74
75
75 respond_to do |format|
76 respond_to do |format|
76 format.html { render :template => 'issues/index.rhtml', :layout => !request.xhr? }
77 format.html { render :template => 'issues/index.rhtml', :layout => !request.xhr? }
77 format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") }
78 format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") }
78 format.csv { send_data(issues_to_csv(@issues, @project), :type => 'text/csv; header=present', :filename => 'export.csv') }
79 format.csv { send_data(issues_to_csv(@issues, @project), :type => 'text/csv; header=present', :filename => 'export.csv') }
79 format.pdf { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'export.pdf') }
80 format.pdf { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'export.pdf') }
80 end
81 end
81 else
82 else
82 # Send html if the query is not valid
83 # Send html if the query is not valid
83 render(:template => 'issues/index.rhtml', :layout => !request.xhr?)
84 render(:template => 'issues/index.rhtml', :layout => !request.xhr?)
84 end
85 end
85 rescue ActiveRecord::RecordNotFound
86 rescue ActiveRecord::RecordNotFound
86 render_404
87 render_404
87 end
88 end
88
89
89 def changes
90 def changes
90 retrieve_query
91 retrieve_query
91 sort_init 'id', 'desc'
92 sort_init 'id', 'desc'
92 sort_update({'id' => "#{Issue.table_name}.id"}.merge(@query.available_columns.inject({}) {|h, c| h[c.name.to_s] = c.sortable; h}))
93 sort_update({'id' => "#{Issue.table_name}.id"}.merge(@query.available_columns.inject({}) {|h, c| h[c.name.to_s] = c.sortable; h}))
93
94
94 if @query.valid?
95 if @query.valid?
95 @journals = @query.journals(:order => "#{Journal.table_name}.created_on DESC",
96 @journals = @query.journals(:order => "#{Journal.table_name}.created_on DESC",
96 :limit => 25)
97 :limit => 25)
97 end
98 end
98 @title = (@project ? @project.name : Setting.app_title) + ": " + (@query.new_record? ? l(:label_changes_details) : @query.name)
99 @title = (@project ? @project.name : Setting.app_title) + ": " + (@query.new_record? ? l(:label_changes_details) : @query.name)
99 render :layout => false, :content_type => 'application/atom+xml'
100 render :layout => false, :content_type => 'application/atom+xml'
100 rescue ActiveRecord::RecordNotFound
101 rescue ActiveRecord::RecordNotFound
101 render_404
102 render_404
102 end
103 end
103
104
104 def show
105 def show
105 @journals = @issue.journals.find(:all, :include => [:user, :details], :order => "#{Journal.table_name}.created_on ASC")
106 @journals = @issue.journals.find(:all, :include => [:user, :details], :order => "#{Journal.table_name}.created_on ASC")
106 @journals.each_with_index {|j,i| j.indice = i+1}
107 @journals.each_with_index {|j,i| j.indice = i+1}
107 @journals.reverse! if User.current.wants_comments_in_reverse_order?
108 @journals.reverse! if User.current.wants_comments_in_reverse_order?
108 @changesets = @issue.changesets
109 @changesets = @issue.changesets
109 @changesets.reverse! if User.current.wants_comments_in_reverse_order?
110 @changesets.reverse! if User.current.wants_comments_in_reverse_order?
110 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
111 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
111 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
112 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
112 @priorities = IssuePriority.all
113 @priorities = IssuePriority.all
113 @time_entry = TimeEntry.new
114 @time_entry = TimeEntry.new
114 respond_to do |format|
115 respond_to do |format|
115 format.html { render :template => 'issues/show.rhtml' }
116 format.html { render :template => 'issues/show.rhtml' }
116 format.atom { render :action => 'changes', :layout => false, :content_type => 'application/atom+xml' }
117 format.atom { render :action => 'changes', :layout => false, :content_type => 'application/atom+xml' }
117 format.pdf { send_data(issue_to_pdf(@issue), :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") }
118 format.pdf { send_data(issue_to_pdf(@issue), :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") }
118 end
119 end
119 end
120 end
120
121
121 # Add a new issue
122 # Add a new issue
122 # The new issue will be created from an existing one if copy_from parameter is given
123 # The new issue will be created from an existing one if copy_from parameter is given
123 def new
124 def new
124 @issue = Issue.new
125 @issue = Issue.new
125 @issue.copy_from(params[:copy_from]) if params[:copy_from]
126 @issue.copy_from(params[:copy_from]) if params[:copy_from]
126 @issue.project = @project
127 @issue.project = @project
127 # Tracker must be set before custom field values
128 # Tracker must be set before custom field values
128 @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first)
129 @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first)
129 if @issue.tracker.nil?
130 if @issue.tracker.nil?
130 render_error l(:error_no_tracker_in_project)
131 render_error l(:error_no_tracker_in_project)
131 return
132 return
132 end
133 end
133 if params[:issue].is_a?(Hash)
134 if params[:issue].is_a?(Hash)
134 @issue.attributes = params[:issue]
135 @issue.attributes = params[:issue]
135 @issue.watcher_user_ids = params[:issue]['watcher_user_ids'] if User.current.allowed_to?(:add_issue_watchers, @project)
136 @issue.watcher_user_ids = params[:issue]['watcher_user_ids'] if User.current.allowed_to?(:add_issue_watchers, @project)
136 end
137 end
137 @issue.author = User.current
138 @issue.author = User.current
138
139
139 default_status = IssueStatus.default
140 default_status = IssueStatus.default
140 unless default_status
141 unless default_status
141 render_error l(:error_no_default_issue_status)
142 render_error l(:error_no_default_issue_status)
142 return
143 return
143 end
144 end
144 @issue.status = default_status
145 @issue.status = default_status
145 @allowed_statuses = ([default_status] + default_status.find_new_statuses_allowed_to(User.current.roles_for_project(@project), @issue.tracker)).uniq
146 @allowed_statuses = ([default_status] + default_status.find_new_statuses_allowed_to(User.current.roles_for_project(@project), @issue.tracker)).uniq
146
147
147 if request.get? || request.xhr?
148 if request.get? || request.xhr?
148 @issue.start_date ||= Date.today
149 @issue.start_date ||= Date.today
149 else
150 else
150 requested_status = IssueStatus.find_by_id(params[:issue][:status_id])
151 requested_status = IssueStatus.find_by_id(params[:issue][:status_id])
151 # Check that the user is allowed to apply the requested status
152 # Check that the user is allowed to apply the requested status
152 @issue.status = (@allowed_statuses.include? requested_status) ? requested_status : default_status
153 @issue.status = (@allowed_statuses.include? requested_status) ? requested_status : default_status
153 call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue })
154 call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue })
154 if @issue.save
155 if @issue.save
155 attach_files(@issue, params[:attachments])
156 attach_files(@issue, params[:attachments])
156 flash[:notice] = l(:notice_successful_create)
157 flash[:notice] = l(:notice_successful_create)
157 call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
158 call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
158 redirect_to(params[:continue] ? { :action => 'new', :tracker_id => @issue.tracker } :
159 redirect_to(params[:continue] ? { :action => 'new', :tracker_id => @issue.tracker } :
159 { :action => 'show', :id => @issue })
160 { :action => 'show', :id => @issue })
160 return
161 return
161 end
162 end
162 end
163 end
163 @priorities = IssuePriority.all
164 @priorities = IssuePriority.all
164 render :layout => !request.xhr?
165 render :layout => !request.xhr?
165 end
166 end
166
167
167 # Attributes that can be updated on workflow transition (without :edit permission)
168 # Attributes that can be updated on workflow transition (without :edit permission)
168 # TODO: make it configurable (at least per role)
169 # TODO: make it configurable (at least per role)
169 UPDATABLE_ATTRS_ON_TRANSITION = %w(status_id assigned_to_id fixed_version_id done_ratio) unless const_defined?(:UPDATABLE_ATTRS_ON_TRANSITION)
170 UPDATABLE_ATTRS_ON_TRANSITION = %w(status_id assigned_to_id fixed_version_id done_ratio) unless const_defined?(:UPDATABLE_ATTRS_ON_TRANSITION)
170
171
171 def edit
172 def edit
172 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
173 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
173 @priorities = IssuePriority.all
174 @priorities = IssuePriority.all
174 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
175 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
175 @time_entry = TimeEntry.new
176 @time_entry = TimeEntry.new
176
177
177 @notes = params[:notes]
178 @notes = params[:notes]
178 journal = @issue.init_journal(User.current, @notes)
179 journal = @issue.init_journal(User.current, @notes)
179 # User can change issue attributes only if he has :edit permission or if a workflow transition is allowed
180 # User can change issue attributes only if he has :edit permission or if a workflow transition is allowed
180 if (@edit_allowed || !@allowed_statuses.empty?) && params[:issue]
181 if (@edit_allowed || !@allowed_statuses.empty?) && params[:issue]
181 attrs = params[:issue].dup
182 attrs = params[:issue].dup
182 attrs.delete_if {|k,v| !UPDATABLE_ATTRS_ON_TRANSITION.include?(k) } unless @edit_allowed
183 attrs.delete_if {|k,v| !UPDATABLE_ATTRS_ON_TRANSITION.include?(k) } unless @edit_allowed
183 attrs.delete(:status_id) unless @allowed_statuses.detect {|s| s.id.to_s == attrs[:status_id].to_s}
184 attrs.delete(:status_id) unless @allowed_statuses.detect {|s| s.id.to_s == attrs[:status_id].to_s}
184 @issue.attributes = attrs
185 @issue.attributes = attrs
185 end
186 end
186
187
187 if request.post?
188 if request.post?
188 @time_entry = TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => Date.today)
189 @time_entry = TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => Date.today)
189 @time_entry.attributes = params[:time_entry]
190 @time_entry.attributes = params[:time_entry]
190 if (@time_entry.hours.nil? || @time_entry.valid?) && @issue.valid?
191 if (@time_entry.hours.nil? || @time_entry.valid?) && @issue.valid?
191 attachments = attach_files(@issue, params[:attachments])
192 attachments = attach_files(@issue, params[:attachments])
192 attachments.each {|a| journal.details << JournalDetail.new(:property => 'attachment', :prop_key => a.id, :value => a.filename)}
193 attachments.each {|a| journal.details << JournalDetail.new(:property => 'attachment', :prop_key => a.id, :value => a.filename)}
193 call_hook(:controller_issues_edit_before_save, { :params => params, :issue => @issue, :time_entry => @time_entry, :journal => journal})
194 call_hook(:controller_issues_edit_before_save, { :params => params, :issue => @issue, :time_entry => @time_entry, :journal => journal})
194 if @issue.save
195 if @issue.save
195 # Log spend time
196 # Log spend time
196 if User.current.allowed_to?(:log_time, @project)
197 if User.current.allowed_to?(:log_time, @project)
197 @time_entry.save
198 @time_entry.save
198 end
199 end
199 if !journal.new_record?
200 if !journal.new_record?
200 # Only send notification if something was actually changed
201 # Only send notification if something was actually changed
201 flash[:notice] = l(:notice_successful_update)
202 flash[:notice] = l(:notice_successful_update)
202 end
203 end
203 call_hook(:controller_issues_edit_after_save, { :params => params, :issue => @issue, :time_entry => @time_entry, :journal => journal})
204 call_hook(:controller_issues_edit_after_save, { :params => params, :issue => @issue, :time_entry => @time_entry, :journal => journal})
204 redirect_back_or_default({:action => 'show', :id => @issue})
205 redirect_back_or_default({:action => 'show', :id => @issue})
205 end
206 end
206 end
207 end
207 end
208 end
208 rescue ActiveRecord::StaleObjectError
209 rescue ActiveRecord::StaleObjectError
209 # Optimistic locking exception
210 # Optimistic locking exception
210 flash.now[:error] = l(:notice_locking_conflict)
211 flash.now[:error] = l(:notice_locking_conflict)
211 # Remove the previously added attachments if issue was not updated
212 # Remove the previously added attachments if issue was not updated
212 attachments.each(&:destroy)
213 attachments.each(&:destroy)
213 end
214 end
214
215
215 def reply
216 def reply
216 journal = Journal.find(params[:journal_id]) if params[:journal_id]
217 journal = Journal.find(params[:journal_id]) if params[:journal_id]
217 if journal
218 if journal
218 user = journal.user
219 user = journal.user
219 text = journal.notes
220 text = journal.notes
220 else
221 else
221 user = @issue.author
222 user = @issue.author
222 text = @issue.description
223 text = @issue.description
223 end
224 end
224 content = "#{ll(Setting.default_language, :text_user_wrote, user)}\\n> "
225 content = "#{ll(Setting.default_language, :text_user_wrote, user)}\\n> "
225 content << text.to_s.strip.gsub(%r{<pre>((.|\s)*?)</pre>}m, '[...]').gsub('"', '\"').gsub(/(\r?\n|\r\n?)/, "\\n> ") + "\\n\\n"
226 content << text.to_s.strip.gsub(%r{<pre>((.|\s)*?)</pre>}m, '[...]').gsub('"', '\"').gsub(/(\r?\n|\r\n?)/, "\\n> ") + "\\n\\n"
226 render(:update) { |page|
227 render(:update) { |page|
227 page.<< "$('notes').value = \"#{content}\";"
228 page.<< "$('notes').value = \"#{content}\";"
228 page.show 'update'
229 page.show 'update'
229 page << "Form.Element.focus('notes');"
230 page << "Form.Element.focus('notes');"
230 page << "Element.scrollTo('update');"
231 page << "Element.scrollTo('update');"
231 page << "$('notes').scrollTop = $('notes').scrollHeight - $('notes').clientHeight;"
232 page << "$('notes').scrollTop = $('notes').scrollHeight - $('notes').clientHeight;"
232 }
233 }
233 end
234 end
234
235
235 # Bulk edit a set of issues
236 # Bulk edit a set of issues
236 def bulk_edit
237 def bulk_edit
237 if request.post?
238 if request.post?
238 tracker = params[:tracker_id].blank? ? nil : @project.trackers.find_by_id(params[:tracker_id])
239 tracker = params[:tracker_id].blank? ? nil : @project.trackers.find_by_id(params[:tracker_id])
239 status = params[:status_id].blank? ? nil : IssueStatus.find_by_id(params[:status_id])
240 status = params[:status_id].blank? ? nil : IssueStatus.find_by_id(params[:status_id])
240 priority = params[:priority_id].blank? ? nil : IssuePriority.find_by_id(params[:priority_id])
241 priority = params[:priority_id].blank? ? nil : IssuePriority.find_by_id(params[:priority_id])
241 assigned_to = (params[:assigned_to_id].blank? || params[:assigned_to_id] == 'none') ? nil : User.find_by_id(params[:assigned_to_id])
242 assigned_to = (params[:assigned_to_id].blank? || params[:assigned_to_id] == 'none') ? nil : User.find_by_id(params[:assigned_to_id])
242 category = (params[:category_id].blank? || params[:category_id] == 'none') ? nil : @project.issue_categories.find_by_id(params[:category_id])
243 category = (params[:category_id].blank? || params[:category_id] == 'none') ? nil : @project.issue_categories.find_by_id(params[:category_id])
243 fixed_version = (params[:fixed_version_id].blank? || params[:fixed_version_id] == 'none') ? nil : @project.shared_versions.find_by_id(params[:fixed_version_id])
244 fixed_version = (params[:fixed_version_id].blank? || params[:fixed_version_id] == 'none') ? nil : @project.shared_versions.find_by_id(params[:fixed_version_id])
244 custom_field_values = params[:custom_field_values] ? params[:custom_field_values].reject {|k,v| v.blank?} : nil
245 custom_field_values = params[:custom_field_values] ? params[:custom_field_values].reject {|k,v| v.blank?} : nil
245
246
246 unsaved_issue_ids = []
247 unsaved_issue_ids = []
247 @issues.each do |issue|
248 @issues.each do |issue|
248 journal = issue.init_journal(User.current, params[:notes])
249 journal = issue.init_journal(User.current, params[:notes])
249 issue.tracker = tracker if tracker
250 issue.tracker = tracker if tracker
250 issue.priority = priority if priority
251 issue.priority = priority if priority
251 issue.assigned_to = assigned_to if assigned_to || params[:assigned_to_id] == 'none'
252 issue.assigned_to = assigned_to if assigned_to || params[:assigned_to_id] == 'none'
252 issue.category = category if category || params[:category_id] == 'none'
253 issue.category = category if category || params[:category_id] == 'none'
253 issue.fixed_version = fixed_version if fixed_version || params[:fixed_version_id] == 'none'
254 issue.fixed_version = fixed_version if fixed_version || params[:fixed_version_id] == 'none'
254 issue.start_date = params[:start_date] unless params[:start_date].blank?
255 issue.start_date = params[:start_date] unless params[:start_date].blank?
255 issue.due_date = params[:due_date] unless params[:due_date].blank?
256 issue.due_date = params[:due_date] unless params[:due_date].blank?
256 issue.done_ratio = params[:done_ratio] unless params[:done_ratio].blank?
257 issue.done_ratio = params[:done_ratio] unless params[:done_ratio].blank?
257 issue.custom_field_values = custom_field_values if custom_field_values && !custom_field_values.empty?
258 issue.custom_field_values = custom_field_values if custom_field_values && !custom_field_values.empty?
258 call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue })
259 call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue })
259 # Don't save any change to the issue if the user is not authorized to apply the requested status
260 # Don't save any change to the issue if the user is not authorized to apply the requested status
260 unless (status.nil? || (issue.new_statuses_allowed_to(User.current).include?(status) && issue.status = status)) && issue.save
261 unless (status.nil? || (issue.new_statuses_allowed_to(User.current).include?(status) && issue.status = status)) && issue.save
261 # Keep unsaved issue ids to display them in flash error
262 # Keep unsaved issue ids to display them in flash error
262 unsaved_issue_ids << issue.id
263 unsaved_issue_ids << issue.id
263 end
264 end
264 end
265 end
265 if unsaved_issue_ids.empty?
266 if unsaved_issue_ids.empty?
266 flash[:notice] = l(:notice_successful_update) unless @issues.empty?
267 flash[:notice] = l(:notice_successful_update) unless @issues.empty?
267 else
268 else
268 flash[:error] = l(:notice_failed_to_save_issues, :count => unsaved_issue_ids.size,
269 flash[:error] = l(:notice_failed_to_save_issues, :count => unsaved_issue_ids.size,
269 :total => @issues.size,
270 :total => @issues.size,
270 :ids => '#' + unsaved_issue_ids.join(', #'))
271 :ids => '#' + unsaved_issue_ids.join(', #'))
271 end
272 end
272 redirect_back_or_default({:controller => 'issues', :action => 'index', :project_id => @project})
273 redirect_back_or_default({:controller => 'issues', :action => 'index', :project_id => @project})
273 return
274 return
274 end
275 end
275 @available_statuses = Workflow.available_statuses(@project)
276 @available_statuses = Workflow.available_statuses(@project)
276 @custom_fields = @project.all_issue_custom_fields
277 @custom_fields = @project.all_issue_custom_fields
277 end
278 end
278
279
279 def move
280 def move
280 @copy = params[:copy_options] && params[:copy_options][:copy]
281 @copy = params[:copy_options] && params[:copy_options][:copy]
281 @allowed_projects = []
282 @allowed_projects = []
282 # find projects to which the user is allowed to move the issue
283 # find projects to which the user is allowed to move the issue
283 if User.current.admin?
284 if User.current.admin?
284 # admin is allowed to move issues to any active (visible) project
285 # admin is allowed to move issues to any active (visible) project
285 @allowed_projects = Project.find(:all, :conditions => Project.visible_by(User.current))
286 @allowed_projects = Project.find(:all, :conditions => Project.visible_by(User.current))
286 else
287 else
287 User.current.memberships.each {|m| @allowed_projects << m.project if m.roles.detect {|r| r.allowed_to?(:move_issues)}}
288 User.current.memberships.each {|m| @allowed_projects << m.project if m.roles.detect {|r| r.allowed_to?(:move_issues)}}
288 end
289 end
289 @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:new_project_id]} if params[:new_project_id]
290 @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:new_project_id]} if params[:new_project_id]
290 @target_project ||= @project
291 @target_project ||= @project
291 @trackers = @target_project.trackers
292 @trackers = @target_project.trackers
292 @available_statuses = Workflow.available_statuses(@project)
293 @available_statuses = Workflow.available_statuses(@project)
293 if request.post?
294 if request.post?
294 new_tracker = params[:new_tracker_id].blank? ? nil : @target_project.trackers.find_by_id(params[:new_tracker_id])
295 new_tracker = params[:new_tracker_id].blank? ? nil : @target_project.trackers.find_by_id(params[:new_tracker_id])
295 unsaved_issue_ids = []
296 unsaved_issue_ids = []
296 moved_issues = []
297 moved_issues = []
297 @issues.each do |issue|
298 @issues.each do |issue|
298 changed_attributes = {}
299 changed_attributes = {}
299 [:assigned_to_id, :status_id, :start_date, :due_date].each do |valid_attribute|
300 [:assigned_to_id, :status_id, :start_date, :due_date].each do |valid_attribute|
300 unless params[valid_attribute].blank?
301 unless params[valid_attribute].blank?
301 changed_attributes[valid_attribute] = (params[valid_attribute] == 'none' ? nil : params[valid_attribute])
302 changed_attributes[valid_attribute] = (params[valid_attribute] == 'none' ? nil : params[valid_attribute])
302 end
303 end
303 end
304 end
304 issue.init_journal(User.current)
305 issue.init_journal(User.current)
305 if r = issue.move_to(@target_project, new_tracker, {:copy => @copy, :attributes => changed_attributes})
306 if r = issue.move_to(@target_project, new_tracker, {:copy => @copy, :attributes => changed_attributes})
306 moved_issues << r
307 moved_issues << r
307 else
308 else
308 unsaved_issue_ids << issue.id
309 unsaved_issue_ids << issue.id
309 end
310 end
310 end
311 end
311 if unsaved_issue_ids.empty?
312 if unsaved_issue_ids.empty?
312 flash[:notice] = l(:notice_successful_update) unless @issues.empty?
313 flash[:notice] = l(:notice_successful_update) unless @issues.empty?
313 else
314 else
314 flash[:error] = l(:notice_failed_to_save_issues, :count => unsaved_issue_ids.size,
315 flash[:error] = l(:notice_failed_to_save_issues, :count => unsaved_issue_ids.size,
315 :total => @issues.size,
316 :total => @issues.size,
316 :ids => '#' + unsaved_issue_ids.join(', #'))
317 :ids => '#' + unsaved_issue_ids.join(', #'))
317 end
318 end
318 if params[:follow]
319 if params[:follow]
319 if @issues.size == 1 && moved_issues.size == 1
320 if @issues.size == 1 && moved_issues.size == 1
320 redirect_to :controller => 'issues', :action => 'show', :id => moved_issues.first
321 redirect_to :controller => 'issues', :action => 'show', :id => moved_issues.first
321 else
322 else
322 redirect_to :controller => 'issues', :action => 'index', :project_id => (@target_project || @project)
323 redirect_to :controller => 'issues', :action => 'index', :project_id => (@target_project || @project)
323 end
324 end
324 else
325 else
325 redirect_to :controller => 'issues', :action => 'index', :project_id => @project
326 redirect_to :controller => 'issues', :action => 'index', :project_id => @project
326 end
327 end
327 return
328 return
328 end
329 end
329 render :layout => false if request.xhr?
330 render :layout => false if request.xhr?
330 end
331 end
331
332
332 def destroy
333 def destroy
333 @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f
334 @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f
334 if @hours > 0
335 if @hours > 0
335 case params[:todo]
336 case params[:todo]
336 when 'destroy'
337 when 'destroy'
337 # nothing to do
338 # nothing to do
338 when 'nullify'
339 when 'nullify'
339 TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues])
340 TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues])
340 when 'reassign'
341 when 'reassign'
341 reassign_to = @project.issues.find_by_id(params[:reassign_to_id])
342 reassign_to = @project.issues.find_by_id(params[:reassign_to_id])
342 if reassign_to.nil?
343 if reassign_to.nil?
343 flash.now[:error] = l(:error_issue_not_found_in_project)
344 flash.now[:error] = l(:error_issue_not_found_in_project)
344 return
345 return
345 else
346 else
346 TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues])
347 TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues])
347 end
348 end
348 else
349 else
349 # display the destroy form
350 # display the destroy form
350 return
351 return
351 end
352 end
352 end
353 end
353 @issues.each(&:destroy)
354 @issues.each(&:destroy)
354 redirect_to :action => 'index', :project_id => @project
355 redirect_to :action => 'index', :project_id => @project
355 end
356 end
356
357
357 def gantt
358 def gantt
358 @gantt = Redmine::Helpers::Gantt.new(params)
359 @gantt = Redmine::Helpers::Gantt.new(params)
359 retrieve_query
360 retrieve_query
360 if @query.valid?
361 if @query.valid?
361 events = []
362 events = []
362 # Issues that have start and due dates
363 # Issues that have start and due dates
363 events += @query.issues(:include => [:tracker, :assigned_to, :priority],
364 events += @query.issues(:include => [:tracker, :assigned_to, :priority],
364 :order => "start_date, due_date",
365 :order => "start_date, due_date",
365 :conditions => ["(((start_date>=? and start_date<=?) or (due_date>=? and due_date<=?) or (start_date<? and due_date>?)) and start_date is not null and due_date is not null)", @gantt.date_from, @gantt.date_to, @gantt.date_from, @gantt.date_to, @gantt.date_from, @gantt.date_to]
366 :conditions => ["(((start_date>=? and start_date<=?) or (due_date>=? and due_date<=?) or (start_date<? and due_date>?)) and start_date is not null and due_date is not null)", @gantt.date_from, @gantt.date_to, @gantt.date_from, @gantt.date_to, @gantt.date_from, @gantt.date_to]
366 )
367 )
367 # Issues that don't have a due date but that are assigned to a version with a date
368 # Issues that don't have a due date but that are assigned to a version with a date
368 events += @query.issues(:include => [:tracker, :assigned_to, :priority, :fixed_version],
369 events += @query.issues(:include => [:tracker, :assigned_to, :priority, :fixed_version],
369 :order => "start_date, effective_date",
370 :order => "start_date, effective_date",
370 :conditions => ["(((start_date>=? and start_date<=?) or (effective_date>=? and effective_date<=?) or (start_date<? and effective_date>?)) and start_date is not null and due_date is null and effective_date is not null)", @gantt.date_from, @gantt.date_to, @gantt.date_from, @gantt.date_to, @gantt.date_from, @gantt.date_to]
371 :conditions => ["(((start_date>=? and start_date<=?) or (effective_date>=? and effective_date<=?) or (start_date<? and effective_date>?)) and start_date is not null and due_date is null and effective_date is not null)", @gantt.date_from, @gantt.date_to, @gantt.date_from, @gantt.date_to, @gantt.date_from, @gantt.date_to]
371 )
372 )
372 # Versions
373 # Versions
373 events += @query.versions(:conditions => ["effective_date BETWEEN ? AND ?", @gantt.date_from, @gantt.date_to])
374 events += @query.versions(:conditions => ["effective_date BETWEEN ? AND ?", @gantt.date_from, @gantt.date_to])
374
375
375 @gantt.events = events
376 @gantt.events = events
376 end
377 end
377
378
378 basename = (@project ? "#{@project.identifier}-" : '') + 'gantt'
379 basename = (@project ? "#{@project.identifier}-" : '') + 'gantt'
379
380
380 respond_to do |format|
381 respond_to do |format|
381 format.html { render :template => "issues/gantt.rhtml", :layout => !request.xhr? }
382 format.html { render :template => "issues/gantt.rhtml", :layout => !request.xhr? }
382 format.png { send_data(@gantt.to_image, :disposition => 'inline', :type => 'image/png', :filename => "#{basename}.png") } if @gantt.respond_to?('to_image')
383 format.png { send_data(@gantt.to_image, :disposition => 'inline', :type => 'image/png', :filename => "#{basename}.png") } if @gantt.respond_to?('to_image')
383 format.pdf { send_data(gantt_to_pdf(@gantt, @project), :type => 'application/pdf', :filename => "#{basename}.pdf") }
384 format.pdf { send_data(gantt_to_pdf(@gantt, @project), :type => 'application/pdf', :filename => "#{basename}.pdf") }
384 end
385 end
385 end
386 end
386
387
387 def calendar
388 def calendar
388 if params[:year] and params[:year].to_i > 1900
389 if params[:year] and params[:year].to_i > 1900
389 @year = params[:year].to_i
390 @year = params[:year].to_i
390 if params[:month] and params[:month].to_i > 0 and params[:month].to_i < 13
391 if params[:month] and params[:month].to_i > 0 and params[:month].to_i < 13
391 @month = params[:month].to_i
392 @month = params[:month].to_i
392 end
393 end
393 end
394 end
394 @year ||= Date.today.year
395 @year ||= Date.today.year
395 @month ||= Date.today.month
396 @month ||= Date.today.month
396
397
397 @calendar = Redmine::Helpers::Calendar.new(Date.civil(@year, @month, 1), current_language, :month)
398 @calendar = Redmine::Helpers::Calendar.new(Date.civil(@year, @month, 1), current_language, :month)
398 retrieve_query
399 retrieve_query
399 if @query.valid?
400 if @query.valid?
400 events = []
401 events = []
401 events += @query.issues(:include => [:tracker, :assigned_to, :priority],
402 events += @query.issues(:include => [:tracker, :assigned_to, :priority],
402 :conditions => ["((start_date BETWEEN ? AND ?) OR (due_date BETWEEN ? AND ?))", @calendar.startdt, @calendar.enddt, @calendar.startdt, @calendar.enddt]
403 :conditions => ["((start_date BETWEEN ? AND ?) OR (due_date BETWEEN ? AND ?))", @calendar.startdt, @calendar.enddt, @calendar.startdt, @calendar.enddt]
403 )
404 )
404 events += @query.versions(:conditions => ["effective_date BETWEEN ? AND ?", @calendar.startdt, @calendar.enddt])
405 events += @query.versions(:conditions => ["effective_date BETWEEN ? AND ?", @calendar.startdt, @calendar.enddt])
405
406
406 @calendar.events = events
407 @calendar.events = events
407 end
408 end
408
409
409 render :layout => false if request.xhr?
410 render :layout => false if request.xhr?
410 end
411 end
411
412
412 def context_menu
413 def context_menu
413 @issues = Issue.find_all_by_id(params[:ids], :include => :project)
414 @issues = Issue.find_all_by_id(params[:ids], :include => :project)
414 if (@issues.size == 1)
415 if (@issues.size == 1)
415 @issue = @issues.first
416 @issue = @issues.first
416 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
417 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
417 end
418 end
418 projects = @issues.collect(&:project).compact.uniq
419 projects = @issues.collect(&:project).compact.uniq
419 @project = projects.first if projects.size == 1
420 @project = projects.first if projects.size == 1
420
421
421 @can = {:edit => (@project && User.current.allowed_to?(:edit_issues, @project)),
422 @can = {:edit => (@project && User.current.allowed_to?(:edit_issues, @project)),
422 :log_time => (@project && User.current.allowed_to?(:log_time, @project)),
423 :log_time => (@project && User.current.allowed_to?(:log_time, @project)),
423 :update => (@project && (User.current.allowed_to?(:edit_issues, @project) || (User.current.allowed_to?(:change_status, @project) && @allowed_statuses && !@allowed_statuses.empty?))),
424 :update => (@project && (User.current.allowed_to?(:edit_issues, @project) || (User.current.allowed_to?(:change_status, @project) && @allowed_statuses && !@allowed_statuses.empty?))),
424 :move => (@project && User.current.allowed_to?(:move_issues, @project)),
425 :move => (@project && User.current.allowed_to?(:move_issues, @project)),
425 :copy => (@issue && @project.trackers.include?(@issue.tracker) && User.current.allowed_to?(:add_issues, @project)),
426 :copy => (@issue && @project.trackers.include?(@issue.tracker) && User.current.allowed_to?(:add_issues, @project)),
426 :delete => (@project && User.current.allowed_to?(:delete_issues, @project))
427 :delete => (@project && User.current.allowed_to?(:delete_issues, @project))
427 }
428 }
428 if @project
429 if @project
429 @assignables = @project.assignable_users
430 @assignables = @project.assignable_users
430 @assignables << @issue.assigned_to if @issue && @issue.assigned_to && !@assignables.include?(@issue.assigned_to)
431 @assignables << @issue.assigned_to if @issue && @issue.assigned_to && !@assignables.include?(@issue.assigned_to)
431 @trackers = @project.trackers
432 @trackers = @project.trackers
432 end
433 end
433
434
434 @priorities = IssuePriority.all.reverse
435 @priorities = IssuePriority.all.reverse
435 @statuses = IssueStatus.find(:all, :order => 'position')
436 @statuses = IssueStatus.find(:all, :order => 'position')
436 @back = params[:back_url] || request.env['HTTP_REFERER']
437 @back = params[:back_url] || request.env['HTTP_REFERER']
437
438
438 render :layout => false
439 render :layout => false
439 end
440 end
440
441
441 def update_form
442 def update_form
442 if params[:id].blank?
443 if params[:id].blank?
443 @issue = Issue.new
444 @issue = Issue.new
444 @issue.project = @project
445 @issue.project = @project
445 else
446 else
446 @issue = @project.issues.visible.find(params[:id])
447 @issue = @project.issues.visible.find(params[:id])
447 end
448 end
448 @issue.attributes = params[:issue]
449 @issue.attributes = params[:issue]
449 @allowed_statuses = ([@issue.status] + @issue.status.find_new_statuses_allowed_to(User.current.roles_for_project(@project), @issue.tracker)).uniq
450 @allowed_statuses = ([@issue.status] + @issue.status.find_new_statuses_allowed_to(User.current.roles_for_project(@project), @issue.tracker)).uniq
450 @priorities = IssuePriority.all
451 @priorities = IssuePriority.all
451
452
452 render :partial => 'attributes'
453 render :partial => 'attributes'
453 end
454 end
454
455
455 def preview
456 def preview
456 @issue = @project.issues.find_by_id(params[:id]) unless params[:id].blank?
457 @issue = @project.issues.find_by_id(params[:id]) unless params[:id].blank?
457 @attachements = @issue.attachments if @issue
458 @attachements = @issue.attachments if @issue
458 @text = params[:notes] || (params[:issue] ? params[:issue][:description] : nil)
459 @text = params[:notes] || (params[:issue] ? params[:issue][:description] : nil)
459 render :partial => 'common/preview'
460 render :partial => 'common/preview'
460 end
461 end
461
462
462 private
463 private
463 def find_issue
464 def find_issue
464 @issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category])
465 @issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category])
465 @project = @issue.project
466 @project = @issue.project
466 rescue ActiveRecord::RecordNotFound
467 rescue ActiveRecord::RecordNotFound
467 render_404
468 render_404
468 end
469 end
469
470
470 # Filter for bulk operations
471 # Filter for bulk operations
471 def find_issues
472 def find_issues
472 @issues = Issue.find_all_by_id(params[:id] || params[:ids])
473 @issues = Issue.find_all_by_id(params[:id] || params[:ids])
473 raise ActiveRecord::RecordNotFound if @issues.empty?
474 raise ActiveRecord::RecordNotFound if @issues.empty?
474 projects = @issues.collect(&:project).compact.uniq
475 projects = @issues.collect(&:project).compact.uniq
475 if projects.size == 1
476 if projects.size == 1
476 @project = projects.first
477 @project = projects.first
477 else
478 else
478 # TODO: let users bulk edit/move/destroy issues from different projects
479 # TODO: let users bulk edit/move/destroy issues from different projects
479 render_error 'Can not bulk edit/move/destroy issues from different projects'
480 render_error 'Can not bulk edit/move/destroy issues from different projects'
480 return false
481 return false
481 end
482 end
482 rescue ActiveRecord::RecordNotFound
483 rescue ActiveRecord::RecordNotFound
483 render_404
484 render_404
484 end
485 end
485
486
486 def find_project
487 def find_project
487 @project = Project.find(params[:project_id])
488 @project = Project.find(params[:project_id])
488 rescue ActiveRecord::RecordNotFound
489 rescue ActiveRecord::RecordNotFound
489 render_404
490 render_404
490 end
491 end
491
492
492 def find_optional_project
493 def find_optional_project
493 @project = Project.find(params[:project_id]) unless params[:project_id].blank?
494 @project = Project.find(params[:project_id]) unless params[:project_id].blank?
494 allowed = User.current.allowed_to?({:controller => params[:controller], :action => params[:action]}, @project, :global => true)
495 allowed = User.current.allowed_to?({:controller => params[:controller], :action => params[:action]}, @project, :global => true)
495 allowed ? true : deny_access
496 allowed ? true : deny_access
496 rescue ActiveRecord::RecordNotFound
497 rescue ActiveRecord::RecordNotFound
497 render_404
498 render_404
498 end
499 end
499
500
500 # Retrieve query from session or build a new query
501 # Retrieve query from session or build a new query
501 def retrieve_query
502 def retrieve_query
502 if !params[:query_id].blank?
503 if !params[:query_id].blank?
503 cond = "project_id IS NULL"
504 cond = "project_id IS NULL"
504 cond << " OR project_id = #{@project.id}" if @project
505 cond << " OR project_id = #{@project.id}" if @project
505 @query = Query.find(params[:query_id], :conditions => cond)
506 @query = Query.find(params[:query_id], :conditions => cond)
506 @query.project = @project
507 @query.project = @project
507 session[:query] = {:id => @query.id, :project_id => @query.project_id}
508 session[:query] = {:id => @query.id, :project_id => @query.project_id}
508 sort_clear
509 sort_clear
509 else
510 else
510 if params[:set_filter] || session[:query].nil? || session[:query][:project_id] != (@project ? @project.id : nil)
511 if params[:set_filter] || session[:query].nil? || session[:query][:project_id] != (@project ? @project.id : nil)
511 # Give it a name, required to be valid
512 # Give it a name, required to be valid
512 @query = Query.new(:name => "_")
513 @query = Query.new(:name => "_")
513 @query.project = @project
514 @query.project = @project
514 if params[:fields] and params[:fields].is_a? Array
515 if params[:fields] and params[:fields].is_a? Array
515 params[:fields].each do |field|
516 params[:fields].each do |field|
516 @query.add_filter(field, params[:operators][field], params[:values][field])
517 @query.add_filter(field, params[:operators][field], params[:values][field])
517 end
518 end
518 else
519 else
519 @query.available_filters.keys.each do |field|
520 @query.available_filters.keys.each do |field|
520 @query.add_short_filter(field, params[field]) if params[field]
521 @query.add_short_filter(field, params[field]) if params[field]
521 end
522 end
522 end
523 end
523 @query.group_by = params[:group_by]
524 @query.group_by = params[:group_by]
524 @query.column_names = params[:query] && params[:query][:column_names]
525 @query.column_names = params[:query] && params[:query][:column_names]
525 session[:query] = {:project_id => @query.project_id, :filters => @query.filters, :group_by => @query.group_by, :column_names => @query.column_names}
526 session[:query] = {:project_id => @query.project_id, :filters => @query.filters, :group_by => @query.group_by, :column_names => @query.column_names}
526 else
527 else
527 @query = Query.find_by_id(session[:query][:id]) if session[:query][:id]
528 @query = Query.find_by_id(session[:query][:id]) if session[:query][:id]
528 @query ||= Query.new(:name => "_", :project => @project, :filters => session[:query][:filters], :group_by => session[:query][:group_by], :column_names => session[:query][:column_names])
529 @query ||= Query.new(:name => "_", :project => @project, :filters => session[:query][:filters], :group_by => session[:query][:group_by], :column_names => session[:query][:column_names])
529 @query.project = @project
530 @query.project = @project
530 end
531 end
531 end
532 end
532 end
533 end
533
534
534 # Rescues an invalid query statement. Just in case...
535 # Rescues an invalid query statement. Just in case...
535 def query_statement_invalid(exception)
536 def query_statement_invalid(exception)
536 logger.error "Query::StatementInvalid: #{exception.message}" if logger
537 logger.error "Query::StatementInvalid: #{exception.message}" if logger
537 session.delete(:query)
538 session.delete(:query)
538 sort_clear
539 sort_clear
539 render_error "An error occurred while executing the query and has been logged. Please report this error to your Redmine administrator."
540 render_error "An error occurred while executing the query and has been logged. Please report this error to your Redmine administrator."
540 end
541 end
541 end
542 end
@@ -1,487 +1,489
1 # encoding: utf-8
1 # encoding: utf-8
2 #
2 #
3 # Redmine - project management software
3 # Redmine - project management software
4 # Copyright (C) 2006-2009 Jean-Philippe Lang
4 # Copyright (C) 2006-2009 Jean-Philippe Lang
5 #
5 #
6 # This program is free software; you can redistribute it and/or
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
9 # of the License, or (at your option) any later version.
10 #
10 #
11 # This program is distributed in the hope that it will be useful,
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
14 # GNU General Public License for more details.
15 #
15 #
16 # You should have received a copy of the GNU General Public License
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
19
20 require 'iconv'
20 require 'iconv'
21 require 'rfpdf/fpdf'
21 require 'rfpdf/fpdf'
22 require 'rfpdf/chinese'
22 require 'rfpdf/chinese'
23
23
24 module Redmine
24 module Redmine
25 module Export
25 module Export
26 module PDF
26 module PDF
27 include ActionView::Helpers::TextHelper
27 include ActionView::Helpers::TextHelper
28 include ActionView::Helpers::NumberHelper
28 include ActionView::Helpers::NumberHelper
29
29
30 class IFPDF < FPDF
30 class IFPDF < FPDF
31 include Redmine::I18n
31 include Redmine::I18n
32 attr_accessor :footer_date
32 attr_accessor :footer_date
33
33
34 def initialize(lang)
34 def initialize(lang)
35 super()
35 super()
36 set_language_if_valid lang
36 set_language_if_valid lang
37 case current_language.to_s.downcase
37 case current_language.to_s.downcase
38 when 'ja'
38 when 'ja'
39 extend(PDF_Japanese)
39 extend(PDF_Japanese)
40 AddSJISFont()
40 AddSJISFont()
41 @font_for_content = 'SJIS'
41 @font_for_content = 'SJIS'
42 @font_for_footer = 'SJIS'
42 @font_for_footer = 'SJIS'
43 when 'zh'
43 when 'zh'
44 extend(PDF_Chinese)
44 extend(PDF_Chinese)
45 AddGBFont()
45 AddGBFont()
46 @font_for_content = 'GB'
46 @font_for_content = 'GB'
47 @font_for_footer = 'GB'
47 @font_for_footer = 'GB'
48 when 'zh-tw'
48 when 'zh-tw'
49 extend(PDF_Chinese)
49 extend(PDF_Chinese)
50 AddBig5Font()
50 AddBig5Font()
51 @font_for_content = 'Big5'
51 @font_for_content = 'Big5'
52 @font_for_footer = 'Big5'
52 @font_for_footer = 'Big5'
53 else
53 else
54 @font_for_content = 'Arial'
54 @font_for_content = 'Arial'
55 @font_for_footer = 'Helvetica'
55 @font_for_footer = 'Helvetica'
56 end
56 end
57 SetCreator(Redmine::Info.app_name)
57 SetCreator(Redmine::Info.app_name)
58 SetFont(@font_for_content)
58 SetFont(@font_for_content)
59 end
59 end
60
60
61 def SetFontStyle(style, size)
61 def SetFontStyle(style, size)
62 SetFont(@font_for_content, style, size)
62 SetFont(@font_for_content, style, size)
63 end
63 end
64
64
65 def SetTitle(txt)
65 def SetTitle(txt)
66 txt = begin
66 txt = begin
67 utf16txt = Iconv.conv('UTF-16BE', 'UTF-8', txt)
67 utf16txt = Iconv.conv('UTF-16BE', 'UTF-8', txt)
68 hextxt = "<FEFF" # FEFF is BOM
68 hextxt = "<FEFF" # FEFF is BOM
69 hextxt << utf16txt.unpack("C*").map {|x| sprintf("%02X",x) }.join
69 hextxt << utf16txt.unpack("C*").map {|x| sprintf("%02X",x) }.join
70 hextxt << ">"
70 hextxt << ">"
71 rescue
71 rescue
72 txt
72 txt
73 end || ''
73 end || ''
74 super(txt)
74 super(txt)
75 end
75 end
76
76
77 def textstring(s)
77 def textstring(s)
78 # Format a text string
78 # Format a text string
79 if s =~ /^</ # This means the string is hex-dumped.
79 if s =~ /^</ # This means the string is hex-dumped.
80 return s
80 return s
81 else
81 else
82 return '('+escape(s)+')'
82 return '('+escape(s)+')'
83 end
83 end
84 end
84 end
85
85
86 def Cell(w,h=0,txt='',border=0,ln=0,align='',fill=0,link='')
86 def Cell(w,h=0,txt='',border=0,ln=0,align='',fill=0,link='')
87 @ic ||= Iconv.new(l(:general_pdf_encoding), 'UTF-8')
87 @ic ||= Iconv.new(l(:general_pdf_encoding), 'UTF-8')
88 # these quotation marks are not correctly rendered in the pdf
88 # these quotation marks are not correctly rendered in the pdf
89 txt = txt.gsub(/[Ò€œÒ€�]/, '"') if txt
89 txt = txt.gsub(/[Ò€œÒ€�]/, '"') if txt
90 txt = begin
90 txt = begin
91 # 0x5c char handling
91 # 0x5c char handling
92 txtar = txt.split('\\')
92 txtar = txt.split('\\')
93 txtar << '' if txt[-1] == ?\\
93 txtar << '' if txt[-1] == ?\\
94 txtar.collect {|x| @ic.iconv(x)}.join('\\').gsub(/\\/, "\\\\\\\\")
94 txtar.collect {|x| @ic.iconv(x)}.join('\\').gsub(/\\/, "\\\\\\\\")
95 rescue
95 rescue
96 txt
96 txt
97 end || ''
97 end || ''
98 super w,h,txt,border,ln,align,fill,link
98 super w,h,txt,border,ln,align,fill,link
99 end
99 end
100
100
101 def Footer
101 def Footer
102 SetFont(@font_for_footer, 'I', 8)
102 SetFont(@font_for_footer, 'I', 8)
103 SetY(-15)
103 SetY(-15)
104 SetX(15)
104 SetX(15)
105 Cell(0, 5, @footer_date, 0, 0, 'L')
105 Cell(0, 5, @footer_date, 0, 0, 'L')
106 SetY(-15)
106 SetY(-15)
107 SetX(-30)
107 SetX(-30)
108 Cell(0, 5, PageNo().to_s + '/{nb}', 0, 0, 'C')
108 Cell(0, 5, PageNo().to_s + '/{nb}', 0, 0, 'C')
109 end
109 end
110 end
110 end
111
111
112 # Returns a PDF string of a list of issues
112 # Returns a PDF string of a list of issues
113 def issues_to_pdf(issues, project, query)
113 def issues_to_pdf(issues, project, query)
114 pdf = IFPDF.new(current_language)
114 pdf = IFPDF.new(current_language)
115 title = query.new_record? ? l(:label_issue_plural) : query.name
115 title = query.new_record? ? l(:label_issue_plural) : query.name
116 title = "#{project} - #{title}" if project
116 title = "#{project} - #{title}" if project
117 pdf.SetTitle(title)
117 pdf.SetTitle(title)
118 pdf.AliasNbPages
118 pdf.AliasNbPages
119 pdf.footer_date = format_date(Date.today)
119 pdf.footer_date = format_date(Date.today)
120 pdf.AddPage("L")
120 pdf.AddPage("L")
121
121
122 row_height = 6
122 row_height = 6
123 col_width = []
123 col_width = []
124 unless query.columns.empty?
124 unless query.columns.empty?
125 col_width = query.columns.collect {|column| column.name == :subject ? 4.0 : 1.0 }
125 col_width = query.columns.collect {|column| column.name == :subject ? 4.0 : 1.0 }
126 ratio = 262.0 / col_width.inject(0) {|s,w| s += w}
126 ratio = 262.0 / col_width.inject(0) {|s,w| s += w}
127 col_width = col_width.collect {|w| w * ratio}
127 col_width = col_width.collect {|w| w * ratio}
128 end
128 end
129
129
130 # title
130 # title
131 pdf.SetFontStyle('B',11)
131 pdf.SetFontStyle('B',11)
132 pdf.Cell(190,10, title)
132 pdf.Cell(190,10, title)
133 pdf.Ln
133 pdf.Ln
134
134
135 # headers
135 # headers
136 pdf.SetFontStyle('B',8)
136 pdf.SetFontStyle('B',8)
137 pdf.SetFillColor(230, 230, 230)
137 pdf.SetFillColor(230, 230, 230)
138 pdf.Cell(15, row_height, "#", 1, 0, 'L', 1)
138 pdf.Cell(15, row_height, "#", 1, 0, 'L', 1)
139 query.columns.each_with_index do |column, i|
139 query.columns.each_with_index do |column, i|
140 pdf.Cell(col_width[i], row_height, column.caption, 1, 0, 'L', 1)
140 pdf.Cell(col_width[i], row_height, column.caption, 1, 0, 'L', 1)
141 end
141 end
142 pdf.Ln
142 pdf.Ln
143
143
144 # rows
144 # rows
145 pdf.SetFontStyle('',8)
145 pdf.SetFontStyle('',8)
146 pdf.SetFillColor(255, 255, 255)
146 pdf.SetFillColor(255, 255, 255)
147 group = false
147 previous_group = false
148 issues.each do |issue|
148 issues.each do |issue|
149 if query.grouped? && issue.send(query.group_by) != group
149 if query.grouped? && (group = query.group_by_column.value(issue)) != previous_group
150 group = issue.send(query.group_by)
151 pdf.SetFontStyle('B',9)
150 pdf.SetFontStyle('B',9)
152 pdf.Cell(277, row_height, "#{group.blank? ? 'None' : group.to_s}", 1, 1, 'L')
151 pdf.Cell(277, row_height,
152 (group.blank? ? 'None' : group.to_s) + " (#{@issue_count_by_group[group]})",
153 1, 1, 'L')
153 pdf.SetFontStyle('',8)
154 pdf.SetFontStyle('',8)
155 previous_group = group
154 end
156 end
155 pdf.Cell(15, row_height, issue.id.to_s, 1, 0, 'L', 1)
157 pdf.Cell(15, row_height, issue.id.to_s, 1, 0, 'L', 1)
156 query.columns.each_with_index do |column, i|
158 query.columns.each_with_index do |column, i|
157 s = if column.is_a?(QueryCustomFieldColumn)
159 s = if column.is_a?(QueryCustomFieldColumn)
158 cv = issue.custom_values.detect {|v| v.custom_field_id == column.custom_field.id}
160 cv = issue.custom_values.detect {|v| v.custom_field_id == column.custom_field.id}
159 show_value(cv)
161 show_value(cv)
160 else
162 else
161 value = issue.send(column.name)
163 value = issue.send(column.name)
162 if value.is_a?(Date)
164 if value.is_a?(Date)
163 format_date(value)
165 format_date(value)
164 elsif value.is_a?(Time)
166 elsif value.is_a?(Time)
165 format_time(value)
167 format_time(value)
166 else
168 else
167 value
169 value
168 end
170 end
169 end
171 end
170 pdf.Cell(col_width[i], row_height, s.to_s, 1, 0, 'L', 1)
172 pdf.Cell(col_width[i], row_height, s.to_s, 1, 0, 'L', 1)
171 end
173 end
172 pdf.Ln
174 pdf.Ln
173 end
175 end
174 if issues.size == Setting.issues_export_limit.to_i
176 if issues.size == Setting.issues_export_limit.to_i
175 pdf.SetFontStyle('B',10)
177 pdf.SetFontStyle('B',10)
176 pdf.Cell(0, row_height, '...')
178 pdf.Cell(0, row_height, '...')
177 end
179 end
178 pdf.Output
180 pdf.Output
179 end
181 end
180
182
181 # Returns a PDF string of a single issue
183 # Returns a PDF string of a single issue
182 def issue_to_pdf(issue)
184 def issue_to_pdf(issue)
183 pdf = IFPDF.new(current_language)
185 pdf = IFPDF.new(current_language)
184 pdf.SetTitle("#{issue.project} - ##{issue.tracker} #{issue.id}")
186 pdf.SetTitle("#{issue.project} - ##{issue.tracker} #{issue.id}")
185 pdf.AliasNbPages
187 pdf.AliasNbPages
186 pdf.footer_date = format_date(Date.today)
188 pdf.footer_date = format_date(Date.today)
187 pdf.AddPage
189 pdf.AddPage
188
190
189 pdf.SetFontStyle('B',11)
191 pdf.SetFontStyle('B',11)
190 pdf.Cell(190,10, "#{issue.project} - #{issue.tracker} # #{issue.id}: #{issue.subject}")
192 pdf.Cell(190,10, "#{issue.project} - #{issue.tracker} # #{issue.id}: #{issue.subject}")
191 pdf.Ln
193 pdf.Ln
192
194
193 y0 = pdf.GetY
195 y0 = pdf.GetY
194
196
195 pdf.SetFontStyle('B',9)
197 pdf.SetFontStyle('B',9)
196 pdf.Cell(35,5, l(:field_status) + ":","LT")
198 pdf.Cell(35,5, l(:field_status) + ":","LT")
197 pdf.SetFontStyle('',9)
199 pdf.SetFontStyle('',9)
198 pdf.Cell(60,5, issue.status.to_s,"RT")
200 pdf.Cell(60,5, issue.status.to_s,"RT")
199 pdf.SetFontStyle('B',9)
201 pdf.SetFontStyle('B',9)
200 pdf.Cell(35,5, l(:field_priority) + ":","LT")
202 pdf.Cell(35,5, l(:field_priority) + ":","LT")
201 pdf.SetFontStyle('',9)
203 pdf.SetFontStyle('',9)
202 pdf.Cell(60,5, issue.priority.to_s,"RT")
204 pdf.Cell(60,5, issue.priority.to_s,"RT")
203 pdf.Ln
205 pdf.Ln
204
206
205 pdf.SetFontStyle('B',9)
207 pdf.SetFontStyle('B',9)
206 pdf.Cell(35,5, l(:field_author) + ":","L")
208 pdf.Cell(35,5, l(:field_author) + ":","L")
207 pdf.SetFontStyle('',9)
209 pdf.SetFontStyle('',9)
208 pdf.Cell(60,5, issue.author.to_s,"R")
210 pdf.Cell(60,5, issue.author.to_s,"R")
209 pdf.SetFontStyle('B',9)
211 pdf.SetFontStyle('B',9)
210 pdf.Cell(35,5, l(:field_category) + ":","L")
212 pdf.Cell(35,5, l(:field_category) + ":","L")
211 pdf.SetFontStyle('',9)
213 pdf.SetFontStyle('',9)
212 pdf.Cell(60,5, issue.category.to_s,"R")
214 pdf.Cell(60,5, issue.category.to_s,"R")
213 pdf.Ln
215 pdf.Ln
214
216
215 pdf.SetFontStyle('B',9)
217 pdf.SetFontStyle('B',9)
216 pdf.Cell(35,5, l(:field_created_on) + ":","L")
218 pdf.Cell(35,5, l(:field_created_on) + ":","L")
217 pdf.SetFontStyle('',9)
219 pdf.SetFontStyle('',9)
218 pdf.Cell(60,5, format_date(issue.created_on),"R")
220 pdf.Cell(60,5, format_date(issue.created_on),"R")
219 pdf.SetFontStyle('B',9)
221 pdf.SetFontStyle('B',9)
220 pdf.Cell(35,5, l(:field_assigned_to) + ":","L")
222 pdf.Cell(35,5, l(:field_assigned_to) + ":","L")
221 pdf.SetFontStyle('',9)
223 pdf.SetFontStyle('',9)
222 pdf.Cell(60,5, issue.assigned_to.to_s,"R")
224 pdf.Cell(60,5, issue.assigned_to.to_s,"R")
223 pdf.Ln
225 pdf.Ln
224
226
225 pdf.SetFontStyle('B',9)
227 pdf.SetFontStyle('B',9)
226 pdf.Cell(35,5, l(:field_updated_on) + ":","LB")
228 pdf.Cell(35,5, l(:field_updated_on) + ":","LB")
227 pdf.SetFontStyle('',9)
229 pdf.SetFontStyle('',9)
228 pdf.Cell(60,5, format_date(issue.updated_on),"RB")
230 pdf.Cell(60,5, format_date(issue.updated_on),"RB")
229 pdf.SetFontStyle('B',9)
231 pdf.SetFontStyle('B',9)
230 pdf.Cell(35,5, l(:field_due_date) + ":","LB")
232 pdf.Cell(35,5, l(:field_due_date) + ":","LB")
231 pdf.SetFontStyle('',9)
233 pdf.SetFontStyle('',9)
232 pdf.Cell(60,5, format_date(issue.due_date),"RB")
234 pdf.Cell(60,5, format_date(issue.due_date),"RB")
233 pdf.Ln
235 pdf.Ln
234
236
235 for custom_value in issue.custom_field_values
237 for custom_value in issue.custom_field_values
236 pdf.SetFontStyle('B',9)
238 pdf.SetFontStyle('B',9)
237 pdf.Cell(35,5, custom_value.custom_field.name + ":","L")
239 pdf.Cell(35,5, custom_value.custom_field.name + ":","L")
238 pdf.SetFontStyle('',9)
240 pdf.SetFontStyle('',9)
239 pdf.MultiCell(155,5, (show_value custom_value),"R")
241 pdf.MultiCell(155,5, (show_value custom_value),"R")
240 end
242 end
241
243
242 pdf.SetFontStyle('B',9)
244 pdf.SetFontStyle('B',9)
243 pdf.Cell(35,5, l(:field_subject) + ":","LTB")
245 pdf.Cell(35,5, l(:field_subject) + ":","LTB")
244 pdf.SetFontStyle('',9)
246 pdf.SetFontStyle('',9)
245 pdf.Cell(155,5, issue.subject,"RTB")
247 pdf.Cell(155,5, issue.subject,"RTB")
246 pdf.Ln
248 pdf.Ln
247
249
248 pdf.SetFontStyle('B',9)
250 pdf.SetFontStyle('B',9)
249 pdf.Cell(35,5, l(:field_description) + ":")
251 pdf.Cell(35,5, l(:field_description) + ":")
250 pdf.SetFontStyle('',9)
252 pdf.SetFontStyle('',9)
251 pdf.MultiCell(155,5, @issue.description,"BR")
253 pdf.MultiCell(155,5, @issue.description,"BR")
252
254
253 pdf.Line(pdf.GetX, y0, pdf.GetX, pdf.GetY)
255 pdf.Line(pdf.GetX, y0, pdf.GetX, pdf.GetY)
254 pdf.Line(pdf.GetX, pdf.GetY, 170, pdf.GetY)
256 pdf.Line(pdf.GetX, pdf.GetY, 170, pdf.GetY)
255 pdf.Ln
257 pdf.Ln
256
258
257 if issue.changesets.any? && User.current.allowed_to?(:view_changesets, issue.project)
259 if issue.changesets.any? && User.current.allowed_to?(:view_changesets, issue.project)
258 pdf.SetFontStyle('B',9)
260 pdf.SetFontStyle('B',9)
259 pdf.Cell(190,5, l(:label_associated_revisions), "B")
261 pdf.Cell(190,5, l(:label_associated_revisions), "B")
260 pdf.Ln
262 pdf.Ln
261 for changeset in issue.changesets
263 for changeset in issue.changesets
262 pdf.SetFontStyle('B',8)
264 pdf.SetFontStyle('B',8)
263 pdf.Cell(190,5, format_time(changeset.committed_on) + " - " + changeset.author.to_s)
265 pdf.Cell(190,5, format_time(changeset.committed_on) + " - " + changeset.author.to_s)
264 pdf.Ln
266 pdf.Ln
265 unless changeset.comments.blank?
267 unless changeset.comments.blank?
266 pdf.SetFontStyle('',8)
268 pdf.SetFontStyle('',8)
267 pdf.MultiCell(190,5, changeset.comments)
269 pdf.MultiCell(190,5, changeset.comments)
268 end
270 end
269 pdf.Ln
271 pdf.Ln
270 end
272 end
271 end
273 end
272
274
273 pdf.SetFontStyle('B',9)
275 pdf.SetFontStyle('B',9)
274 pdf.Cell(190,5, l(:label_history), "B")
276 pdf.Cell(190,5, l(:label_history), "B")
275 pdf.Ln
277 pdf.Ln
276 for journal in issue.journals.find(:all, :include => [:user, :details], :order => "#{Journal.table_name}.created_on ASC")
278 for journal in issue.journals.find(:all, :include => [:user, :details], :order => "#{Journal.table_name}.created_on ASC")
277 pdf.SetFontStyle('B',8)
279 pdf.SetFontStyle('B',8)
278 pdf.Cell(190,5, format_time(journal.created_on) + " - " + journal.user.name)
280 pdf.Cell(190,5, format_time(journal.created_on) + " - " + journal.user.name)
279 pdf.Ln
281 pdf.Ln
280 pdf.SetFontStyle('I',8)
282 pdf.SetFontStyle('I',8)
281 for detail in journal.details
283 for detail in journal.details
282 pdf.Cell(190,5, "- " + show_detail(detail, true))
284 pdf.Cell(190,5, "- " + show_detail(detail, true))
283 pdf.Ln
285 pdf.Ln
284 end
286 end
285 if journal.notes?
287 if journal.notes?
286 pdf.SetFontStyle('',8)
288 pdf.SetFontStyle('',8)
287 pdf.MultiCell(190,5, journal.notes)
289 pdf.MultiCell(190,5, journal.notes)
288 end
290 end
289 pdf.Ln
291 pdf.Ln
290 end
292 end
291
293
292 if issue.attachments.any?
294 if issue.attachments.any?
293 pdf.SetFontStyle('B',9)
295 pdf.SetFontStyle('B',9)
294 pdf.Cell(190,5, l(:label_attachment_plural), "B")
296 pdf.Cell(190,5, l(:label_attachment_plural), "B")
295 pdf.Ln
297 pdf.Ln
296 for attachment in issue.attachments
298 for attachment in issue.attachments
297 pdf.SetFontStyle('',8)
299 pdf.SetFontStyle('',8)
298 pdf.Cell(80,5, attachment.filename)
300 pdf.Cell(80,5, attachment.filename)
299 pdf.Cell(20,5, number_to_human_size(attachment.filesize),0,0,"R")
301 pdf.Cell(20,5, number_to_human_size(attachment.filesize),0,0,"R")
300 pdf.Cell(25,5, format_date(attachment.created_on),0,0,"R")
302 pdf.Cell(25,5, format_date(attachment.created_on),0,0,"R")
301 pdf.Cell(65,5, attachment.author.name,0,0,"R")
303 pdf.Cell(65,5, attachment.author.name,0,0,"R")
302 pdf.Ln
304 pdf.Ln
303 end
305 end
304 end
306 end
305 pdf.Output
307 pdf.Output
306 end
308 end
307
309
308 # Returns a PDF string of a gantt chart
310 # Returns a PDF string of a gantt chart
309 def gantt_to_pdf(gantt, project)
311 def gantt_to_pdf(gantt, project)
310 pdf = IFPDF.new(current_language)
312 pdf = IFPDF.new(current_language)
311 pdf.SetTitle("#{l(:label_gantt)} #{project}")
313 pdf.SetTitle("#{l(:label_gantt)} #{project}")
312 pdf.AliasNbPages
314 pdf.AliasNbPages
313 pdf.footer_date = format_date(Date.today)
315 pdf.footer_date = format_date(Date.today)
314 pdf.AddPage("L")
316 pdf.AddPage("L")
315 pdf.SetFontStyle('B',12)
317 pdf.SetFontStyle('B',12)
316 pdf.SetX(15)
318 pdf.SetX(15)
317 pdf.Cell(70, 20, project.to_s)
319 pdf.Cell(70, 20, project.to_s)
318 pdf.Ln
320 pdf.Ln
319 pdf.SetFontStyle('B',9)
321 pdf.SetFontStyle('B',9)
320
322
321 subject_width = 70
323 subject_width = 70
322 header_heigth = 5
324 header_heigth = 5
323
325
324 headers_heigth = header_heigth
326 headers_heigth = header_heigth
325 show_weeks = false
327 show_weeks = false
326 show_days = false
328 show_days = false
327
329
328 if gantt.months < 7
330 if gantt.months < 7
329 show_weeks = true
331 show_weeks = true
330 headers_heigth = 2*header_heigth
332 headers_heigth = 2*header_heigth
331 if gantt.months < 3
333 if gantt.months < 3
332 show_days = true
334 show_days = true
333 headers_heigth = 3*header_heigth
335 headers_heigth = 3*header_heigth
334 end
336 end
335 end
337 end
336
338
337 g_width = 210
339 g_width = 210
338 zoom = (g_width) / (gantt.date_to - gantt.date_from + 1)
340 zoom = (g_width) / (gantt.date_to - gantt.date_from + 1)
339 g_height = 120
341 g_height = 120
340 t_height = g_height + headers_heigth
342 t_height = g_height + headers_heigth
341
343
342 y_start = pdf.GetY
344 y_start = pdf.GetY
343
345
344 # Months headers
346 # Months headers
345 month_f = gantt.date_from
347 month_f = gantt.date_from
346 left = subject_width
348 left = subject_width
347 height = header_heigth
349 height = header_heigth
348 gantt.months.times do
350 gantt.months.times do
349 width = ((month_f >> 1) - month_f) * zoom
351 width = ((month_f >> 1) - month_f) * zoom
350 pdf.SetY(y_start)
352 pdf.SetY(y_start)
351 pdf.SetX(left)
353 pdf.SetX(left)
352 pdf.Cell(width, height, "#{month_f.year}-#{month_f.month}", "LTR", 0, "C")
354 pdf.Cell(width, height, "#{month_f.year}-#{month_f.month}", "LTR", 0, "C")
353 left = left + width
355 left = left + width
354 month_f = month_f >> 1
356 month_f = month_f >> 1
355 end
357 end
356
358
357 # Weeks headers
359 # Weeks headers
358 if show_weeks
360 if show_weeks
359 left = subject_width
361 left = subject_width
360 height = header_heigth
362 height = header_heigth
361 if gantt.date_from.cwday == 1
363 if gantt.date_from.cwday == 1
362 # gantt.date_from is monday
364 # gantt.date_from is monday
363 week_f = gantt.date_from
365 week_f = gantt.date_from
364 else
366 else
365 # find next monday after gantt.date_from
367 # find next monday after gantt.date_from
366 week_f = gantt.date_from + (7 - gantt.date_from.cwday + 1)
368 week_f = gantt.date_from + (7 - gantt.date_from.cwday + 1)
367 width = (7 - gantt.date_from.cwday + 1) * zoom-1
369 width = (7 - gantt.date_from.cwday + 1) * zoom-1
368 pdf.SetY(y_start + header_heigth)
370 pdf.SetY(y_start + header_heigth)
369 pdf.SetX(left)
371 pdf.SetX(left)
370 pdf.Cell(width + 1, height, "", "LTR")
372 pdf.Cell(width + 1, height, "", "LTR")
371 left = left + width+1
373 left = left + width+1
372 end
374 end
373 while week_f <= gantt.date_to
375 while week_f <= gantt.date_to
374 width = (week_f + 6 <= gantt.date_to) ? 7 * zoom : (gantt.date_to - week_f + 1) * zoom
376 width = (week_f + 6 <= gantt.date_to) ? 7 * zoom : (gantt.date_to - week_f + 1) * zoom
375 pdf.SetY(y_start + header_heigth)
377 pdf.SetY(y_start + header_heigth)
376 pdf.SetX(left)
378 pdf.SetX(left)
377 pdf.Cell(width, height, (width >= 5 ? week_f.cweek.to_s : ""), "LTR", 0, "C")
379 pdf.Cell(width, height, (width >= 5 ? week_f.cweek.to_s : ""), "LTR", 0, "C")
378 left = left + width
380 left = left + width
379 week_f = week_f+7
381 week_f = week_f+7
380 end
382 end
381 end
383 end
382
384
383 # Days headers
385 # Days headers
384 if show_days
386 if show_days
385 left = subject_width
387 left = subject_width
386 height = header_heigth
388 height = header_heigth
387 wday = gantt.date_from.cwday
389 wday = gantt.date_from.cwday
388 pdf.SetFontStyle('B',7)
390 pdf.SetFontStyle('B',7)
389 (gantt.date_to - gantt.date_from + 1).to_i.times do
391 (gantt.date_to - gantt.date_from + 1).to_i.times do
390 width = zoom
392 width = zoom
391 pdf.SetY(y_start + 2 * header_heigth)
393 pdf.SetY(y_start + 2 * header_heigth)
392 pdf.SetX(left)
394 pdf.SetX(left)
393 pdf.Cell(width, height, day_name(wday).first, "LTR", 0, "C")
395 pdf.Cell(width, height, day_name(wday).first, "LTR", 0, "C")
394 left = left + width
396 left = left + width
395 wday = wday + 1
397 wday = wday + 1
396 wday = 1 if wday > 7
398 wday = 1 if wday > 7
397 end
399 end
398 end
400 end
399
401
400 pdf.SetY(y_start)
402 pdf.SetY(y_start)
401 pdf.SetX(15)
403 pdf.SetX(15)
402 pdf.Cell(subject_width+g_width-15, headers_heigth, "", 1)
404 pdf.Cell(subject_width+g_width-15, headers_heigth, "", 1)
403
405
404 # Tasks
406 # Tasks
405 top = headers_heigth + y_start
407 top = headers_heigth + y_start
406 pdf.SetFontStyle('B',7)
408 pdf.SetFontStyle('B',7)
407 gantt.events.each do |i|
409 gantt.events.each do |i|
408 pdf.SetY(top)
410 pdf.SetY(top)
409 pdf.SetX(15)
411 pdf.SetX(15)
410
412
411 if i.is_a? Issue
413 if i.is_a? Issue
412 pdf.Cell(subject_width-15, 5, "#{i.tracker} #{i.id}: #{i.subject}".sub(/^(.{30}[^\s]*\s).*$/, '\1 (...)'), "LR")
414 pdf.Cell(subject_width-15, 5, "#{i.tracker} #{i.id}: #{i.subject}".sub(/^(.{30}[^\s]*\s).*$/, '\1 (...)'), "LR")
413 else
415 else
414 pdf.Cell(subject_width-15, 5, "#{l(:label_version)}: #{i.name}", "LR")
416 pdf.Cell(subject_width-15, 5, "#{l(:label_version)}: #{i.name}", "LR")
415 end
417 end
416
418
417 pdf.SetY(top)
419 pdf.SetY(top)
418 pdf.SetX(subject_width)
420 pdf.SetX(subject_width)
419 pdf.Cell(g_width, 5, "", "LR")
421 pdf.Cell(g_width, 5, "", "LR")
420
422
421 pdf.SetY(top+1.5)
423 pdf.SetY(top+1.5)
422
424
423 if i.is_a? Issue
425 if i.is_a? Issue
424 i_start_date = (i.start_date >= gantt.date_from ? i.start_date : gantt.date_from )
426 i_start_date = (i.start_date >= gantt.date_from ? i.start_date : gantt.date_from )
425 i_end_date = (i.due_before <= gantt.date_to ? i.due_before : gantt.date_to )
427 i_end_date = (i.due_before <= gantt.date_to ? i.due_before : gantt.date_to )
426
428
427 i_done_date = i.start_date + ((i.due_before - i.start_date+1)*i.done_ratio/100).floor
429 i_done_date = i.start_date + ((i.due_before - i.start_date+1)*i.done_ratio/100).floor
428 i_done_date = (i_done_date <= gantt.date_from ? gantt.date_from : i_done_date )
430 i_done_date = (i_done_date <= gantt.date_from ? gantt.date_from : i_done_date )
429 i_done_date = (i_done_date >= gantt.date_to ? gantt.date_to : i_done_date )
431 i_done_date = (i_done_date >= gantt.date_to ? gantt.date_to : i_done_date )
430
432
431 i_late_date = [i_end_date, Date.today].min if i_start_date < Date.today
433 i_late_date = [i_end_date, Date.today].min if i_start_date < Date.today
432
434
433 i_left = ((i_start_date - gantt.date_from)*zoom)
435 i_left = ((i_start_date - gantt.date_from)*zoom)
434 i_width = ((i_end_date - i_start_date + 1)*zoom)
436 i_width = ((i_end_date - i_start_date + 1)*zoom)
435 d_width = ((i_done_date - i_start_date)*zoom)
437 d_width = ((i_done_date - i_start_date)*zoom)
436 l_width = ((i_late_date - i_start_date+1)*zoom) if i_late_date
438 l_width = ((i_late_date - i_start_date+1)*zoom) if i_late_date
437 l_width ||= 0
439 l_width ||= 0
438
440
439 pdf.SetX(subject_width + i_left)
441 pdf.SetX(subject_width + i_left)
440 pdf.SetFillColor(200,200,200)
442 pdf.SetFillColor(200,200,200)
441 pdf.Cell(i_width, 2, "", 0, 0, "", 1)
443 pdf.Cell(i_width, 2, "", 0, 0, "", 1)
442
444
443 if l_width > 0
445 if l_width > 0
444 pdf.SetY(top+1.5)
446 pdf.SetY(top+1.5)
445 pdf.SetX(subject_width + i_left)
447 pdf.SetX(subject_width + i_left)
446 pdf.SetFillColor(255,100,100)
448 pdf.SetFillColor(255,100,100)
447 pdf.Cell(l_width, 2, "", 0, 0, "", 1)
449 pdf.Cell(l_width, 2, "", 0, 0, "", 1)
448 end
450 end
449 if d_width > 0
451 if d_width > 0
450 pdf.SetY(top+1.5)
452 pdf.SetY(top+1.5)
451 pdf.SetX(subject_width + i_left)
453 pdf.SetX(subject_width + i_left)
452 pdf.SetFillColor(100,100,255)
454 pdf.SetFillColor(100,100,255)
453 pdf.Cell(d_width, 2, "", 0, 0, "", 1)
455 pdf.Cell(d_width, 2, "", 0, 0, "", 1)
454 end
456 end
455
457
456 pdf.SetY(top+1.5)
458 pdf.SetY(top+1.5)
457 pdf.SetX(subject_width + i_left + i_width)
459 pdf.SetX(subject_width + i_left + i_width)
458 pdf.Cell(30, 2, "#{i.status} #{i.done_ratio}%")
460 pdf.Cell(30, 2, "#{i.status} #{i.done_ratio}%")
459 else
461 else
460 i_left = ((i.start_date - gantt.date_from)*zoom)
462 i_left = ((i.start_date - gantt.date_from)*zoom)
461
463
462 pdf.SetX(subject_width + i_left)
464 pdf.SetX(subject_width + i_left)
463 pdf.SetFillColor(50,200,50)
465 pdf.SetFillColor(50,200,50)
464 pdf.Cell(2, 2, "", 0, 0, "", 1)
466 pdf.Cell(2, 2, "", 0, 0, "", 1)
465
467
466 pdf.SetY(top+1.5)
468 pdf.SetY(top+1.5)
467 pdf.SetX(subject_width + i_left + 3)
469 pdf.SetX(subject_width + i_left + 3)
468 pdf.Cell(30, 2, "#{i.name}")
470 pdf.Cell(30, 2, "#{i.name}")
469 end
471 end
470
472
471 top = top + 5
473 top = top + 5
472 pdf.SetDrawColor(200, 200, 200)
474 pdf.SetDrawColor(200, 200, 200)
473 pdf.Line(15, top, subject_width+g_width, top)
475 pdf.Line(15, top, subject_width+g_width, top)
474 if pdf.GetY() > 180
476 if pdf.GetY() > 180
475 pdf.AddPage("L")
477 pdf.AddPage("L")
476 top = 20
478 top = 20
477 pdf.Line(15, top, subject_width+g_width, top)
479 pdf.Line(15, top, subject_width+g_width, top)
478 end
480 end
479 pdf.SetDrawColor(0, 0, 0)
481 pdf.SetDrawColor(0, 0, 0)
480 end
482 end
481
483
482 pdf.Line(15, top, subject_width+g_width, top)
484 pdf.Line(15, top, subject_width+g_width, top)
483 pdf.Output
485 pdf.Output
484 end
486 end
485 end
487 end
486 end
488 end
487 end
489 end
@@ -1,1330 +1,1338
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2008 Jean-Philippe Lang
2 # Copyright (C) 2006-2008 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
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
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.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.dirname(__FILE__) + '/../test_helper'
18 require File.dirname(__FILE__) + '/../test_helper'
19 require 'issues_controller'
19 require 'issues_controller'
20
20
21 # Re-raise errors caught by the controller.
21 # Re-raise errors caught by the controller.
22 class IssuesController; def rescue_action(e) raise e end; end
22 class IssuesController; def rescue_action(e) raise e end; end
23
23
24 class IssuesControllerTest < ActionController::TestCase
24 class IssuesControllerTest < ActionController::TestCase
25 fixtures :projects,
25 fixtures :projects,
26 :users,
26 :users,
27 :roles,
27 :roles,
28 :members,
28 :members,
29 :member_roles,
29 :member_roles,
30 :issues,
30 :issues,
31 :issue_statuses,
31 :issue_statuses,
32 :versions,
32 :versions,
33 :trackers,
33 :trackers,
34 :projects_trackers,
34 :projects_trackers,
35 :issue_categories,
35 :issue_categories,
36 :enabled_modules,
36 :enabled_modules,
37 :enumerations,
37 :enumerations,
38 :attachments,
38 :attachments,
39 :workflows,
39 :workflows,
40 :custom_fields,
40 :custom_fields,
41 :custom_values,
41 :custom_values,
42 :custom_fields_projects,
42 :custom_fields_projects,
43 :custom_fields_trackers,
43 :custom_fields_trackers,
44 :time_entries,
44 :time_entries,
45 :journals,
45 :journals,
46 :journal_details,
46 :journal_details,
47 :queries
47 :queries
48
48
49 def setup
49 def setup
50 @controller = IssuesController.new
50 @controller = IssuesController.new
51 @request = ActionController::TestRequest.new
51 @request = ActionController::TestRequest.new
52 @response = ActionController::TestResponse.new
52 @response = ActionController::TestResponse.new
53 User.current = nil
53 User.current = nil
54 end
54 end
55
55
56 def test_index_routing
56 def test_index_routing
57 assert_routing(
57 assert_routing(
58 {:method => :get, :path => '/issues'},
58 {:method => :get, :path => '/issues'},
59 :controller => 'issues', :action => 'index'
59 :controller => 'issues', :action => 'index'
60 )
60 )
61 end
61 end
62
62
63 def test_index
63 def test_index
64 Setting.default_language = 'en'
64 Setting.default_language = 'en'
65
65
66 get :index
66 get :index
67 assert_response :success
67 assert_response :success
68 assert_template 'index.rhtml'
68 assert_template 'index.rhtml'
69 assert_not_nil assigns(:issues)
69 assert_not_nil assigns(:issues)
70 assert_nil assigns(:project)
70 assert_nil assigns(:project)
71 assert_tag :tag => 'a', :content => /Can't print recipes/
71 assert_tag :tag => 'a', :content => /Can't print recipes/
72 assert_tag :tag => 'a', :content => /Subproject issue/
72 assert_tag :tag => 'a', :content => /Subproject issue/
73 # private projects hidden
73 # private projects hidden
74 assert_no_tag :tag => 'a', :content => /Issue of a private subproject/
74 assert_no_tag :tag => 'a', :content => /Issue of a private subproject/
75 assert_no_tag :tag => 'a', :content => /Issue on project 2/
75 assert_no_tag :tag => 'a', :content => /Issue on project 2/
76 # project column
76 # project column
77 assert_tag :tag => 'th', :content => /Project/
77 assert_tag :tag => 'th', :content => /Project/
78 end
78 end
79
79
80 def test_index_should_not_list_issues_when_module_disabled
80 def test_index_should_not_list_issues_when_module_disabled
81 EnabledModule.delete_all("name = 'issue_tracking' AND project_id = 1")
81 EnabledModule.delete_all("name = 'issue_tracking' AND project_id = 1")
82 get :index
82 get :index
83 assert_response :success
83 assert_response :success
84 assert_template 'index.rhtml'
84 assert_template 'index.rhtml'
85 assert_not_nil assigns(:issues)
85 assert_not_nil assigns(:issues)
86 assert_nil assigns(:project)
86 assert_nil assigns(:project)
87 assert_no_tag :tag => 'a', :content => /Can't print recipes/
87 assert_no_tag :tag => 'a', :content => /Can't print recipes/
88 assert_tag :tag => 'a', :content => /Subproject issue/
88 assert_tag :tag => 'a', :content => /Subproject issue/
89 end
89 end
90
90
91 def test_index_with_project_routing
91 def test_index_with_project_routing
92 assert_routing(
92 assert_routing(
93 {:method => :get, :path => '/projects/23/issues'},
93 {:method => :get, :path => '/projects/23/issues'},
94 :controller => 'issues', :action => 'index', :project_id => '23'
94 :controller => 'issues', :action => 'index', :project_id => '23'
95 )
95 )
96 end
96 end
97
97
98 def test_index_should_not_list_issues_when_module_disabled
98 def test_index_should_not_list_issues_when_module_disabled
99 EnabledModule.delete_all("name = 'issue_tracking' AND project_id = 1")
99 EnabledModule.delete_all("name = 'issue_tracking' AND project_id = 1")
100 get :index
100 get :index
101 assert_response :success
101 assert_response :success
102 assert_template 'index.rhtml'
102 assert_template 'index.rhtml'
103 assert_not_nil assigns(:issues)
103 assert_not_nil assigns(:issues)
104 assert_nil assigns(:project)
104 assert_nil assigns(:project)
105 assert_no_tag :tag => 'a', :content => /Can't print recipes/
105 assert_no_tag :tag => 'a', :content => /Can't print recipes/
106 assert_tag :tag => 'a', :content => /Subproject issue/
106 assert_tag :tag => 'a', :content => /Subproject issue/
107 end
107 end
108
108
109 def test_index_with_project_routing
109 def test_index_with_project_routing
110 assert_routing(
110 assert_routing(
111 {:method => :get, :path => 'projects/23/issues'},
111 {:method => :get, :path => 'projects/23/issues'},
112 :controller => 'issues', :action => 'index', :project_id => '23'
112 :controller => 'issues', :action => 'index', :project_id => '23'
113 )
113 )
114 end
114 end
115
115
116 def test_index_with_project
116 def test_index_with_project
117 Setting.display_subprojects_issues = 0
117 Setting.display_subprojects_issues = 0
118 get :index, :project_id => 1
118 get :index, :project_id => 1
119 assert_response :success
119 assert_response :success
120 assert_template 'index.rhtml'
120 assert_template 'index.rhtml'
121 assert_not_nil assigns(:issues)
121 assert_not_nil assigns(:issues)
122 assert_tag :tag => 'a', :content => /Can't print recipes/
122 assert_tag :tag => 'a', :content => /Can't print recipes/
123 assert_no_tag :tag => 'a', :content => /Subproject issue/
123 assert_no_tag :tag => 'a', :content => /Subproject issue/
124 end
124 end
125
125
126 def test_index_with_project_and_subprojects
126 def test_index_with_project_and_subprojects
127 Setting.display_subprojects_issues = 1
127 Setting.display_subprojects_issues = 1
128 get :index, :project_id => 1
128 get :index, :project_id => 1
129 assert_response :success
129 assert_response :success
130 assert_template 'index.rhtml'
130 assert_template 'index.rhtml'
131 assert_not_nil assigns(:issues)
131 assert_not_nil assigns(:issues)
132 assert_tag :tag => 'a', :content => /Can't print recipes/
132 assert_tag :tag => 'a', :content => /Can't print recipes/
133 assert_tag :tag => 'a', :content => /Subproject issue/
133 assert_tag :tag => 'a', :content => /Subproject issue/
134 assert_no_tag :tag => 'a', :content => /Issue of a private subproject/
134 assert_no_tag :tag => 'a', :content => /Issue of a private subproject/
135 end
135 end
136
136
137 def test_index_with_project_and_subprojects_should_show_private_subprojects
137 def test_index_with_project_and_subprojects_should_show_private_subprojects
138 @request.session[:user_id] = 2
138 @request.session[:user_id] = 2
139 Setting.display_subprojects_issues = 1
139 Setting.display_subprojects_issues = 1
140 get :index, :project_id => 1
140 get :index, :project_id => 1
141 assert_response :success
141 assert_response :success
142 assert_template 'index.rhtml'
142 assert_template 'index.rhtml'
143 assert_not_nil assigns(:issues)
143 assert_not_nil assigns(:issues)
144 assert_tag :tag => 'a', :content => /Can't print recipes/
144 assert_tag :tag => 'a', :content => /Can't print recipes/
145 assert_tag :tag => 'a', :content => /Subproject issue/
145 assert_tag :tag => 'a', :content => /Subproject issue/
146 assert_tag :tag => 'a', :content => /Issue of a private subproject/
146 assert_tag :tag => 'a', :content => /Issue of a private subproject/
147 end
147 end
148
148
149 def test_index_with_project_routing_formatted
149 def test_index_with_project_routing_formatted
150 assert_routing(
150 assert_routing(
151 {:method => :get, :path => 'projects/23/issues.pdf'},
151 {:method => :get, :path => 'projects/23/issues.pdf'},
152 :controller => 'issues', :action => 'index', :project_id => '23', :format => 'pdf'
152 :controller => 'issues', :action => 'index', :project_id => '23', :format => 'pdf'
153 )
153 )
154 assert_routing(
154 assert_routing(
155 {:method => :get, :path => 'projects/23/issues.atom'},
155 {:method => :get, :path => 'projects/23/issues.atom'},
156 :controller => 'issues', :action => 'index', :project_id => '23', :format => 'atom'
156 :controller => 'issues', :action => 'index', :project_id => '23', :format => 'atom'
157 )
157 )
158 end
158 end
159
159
160 def test_index_with_project_and_filter
160 def test_index_with_project_and_filter
161 get :index, :project_id => 1, :set_filter => 1
161 get :index, :project_id => 1, :set_filter => 1
162 assert_response :success
162 assert_response :success
163 assert_template 'index.rhtml'
163 assert_template 'index.rhtml'
164 assert_not_nil assigns(:issues)
164 assert_not_nil assigns(:issues)
165 end
165 end
166
166
167 def test_index_with_query
167 def test_index_with_query
168 get :index, :project_id => 1, :query_id => 5
168 get :index, :project_id => 1, :query_id => 5
169 assert_response :success
169 assert_response :success
170 assert_template 'index.rhtml'
170 assert_template 'index.rhtml'
171 assert_not_nil assigns(:issues)
171 assert_not_nil assigns(:issues)
172 assert_nil assigns(:issue_count_by_group)
172 assert_nil assigns(:issue_count_by_group)
173 end
173 end
174
174
175 def test_index_with_query_grouped_by_tracker
175 def test_index_with_query_grouped_by_tracker
176 get :index, :project_id => 1, :query_id => 6
176 get :index, :project_id => 1, :query_id => 6
177 assert_response :success
177 assert_response :success
178 assert_template 'index.rhtml'
178 assert_template 'index.rhtml'
179 assert_not_nil assigns(:issues)
179 assert_not_nil assigns(:issues)
180 assert_not_nil assigns(:issue_count_by_group)
180 assert_not_nil assigns(:issue_count_by_group)
181 end
181 end
182
182
183 def test_index_with_query_grouped_by_list_custom_field
183 def test_index_with_query_grouped_by_list_custom_field
184 get :index, :project_id => 1, :query_id => 9
184 get :index, :project_id => 1, :query_id => 9
185 assert_response :success
185 assert_response :success
186 assert_template 'index.rhtml'
186 assert_template 'index.rhtml'
187 assert_not_nil assigns(:issues)
187 assert_not_nil assigns(:issues)
188 assert_not_nil assigns(:issue_count_by_group)
188 assert_not_nil assigns(:issue_count_by_group)
189 end
189 end
190
190
191 def test_index_sort_by_field_not_included_in_columns
191 def test_index_sort_by_field_not_included_in_columns
192 Setting.issue_list_default_columns = %w(subject author)
192 Setting.issue_list_default_columns = %w(subject author)
193 get :index, :sort => 'tracker'
193 get :index, :sort => 'tracker'
194 end
194 end
195
195
196 def test_index_csv_with_project
196 def test_index_csv_with_project
197 Setting.default_language = 'en'
197 Setting.default_language = 'en'
198
198
199 get :index, :format => 'csv'
199 get :index, :format => 'csv'
200 assert_response :success
200 assert_response :success
201 assert_not_nil assigns(:issues)
201 assert_not_nil assigns(:issues)
202 assert_equal 'text/csv', @response.content_type
202 assert_equal 'text/csv', @response.content_type
203 assert @response.body.starts_with?("#,")
203 assert @response.body.starts_with?("#,")
204
204
205 get :index, :project_id => 1, :format => 'csv'
205 get :index, :project_id => 1, :format => 'csv'
206 assert_response :success
206 assert_response :success
207 assert_not_nil assigns(:issues)
207 assert_not_nil assigns(:issues)
208 assert_equal 'text/csv', @response.content_type
208 assert_equal 'text/csv', @response.content_type
209 end
209 end
210
210
211 def test_index_formatted
211 def test_index_formatted
212 assert_routing(
212 assert_routing(
213 {:method => :get, :path => 'issues.pdf'},
213 {:method => :get, :path => 'issues.pdf'},
214 :controller => 'issues', :action => 'index', :format => 'pdf'
214 :controller => 'issues', :action => 'index', :format => 'pdf'
215 )
215 )
216 assert_routing(
216 assert_routing(
217 {:method => :get, :path => 'issues.atom'},
217 {:method => :get, :path => 'issues.atom'},
218 :controller => 'issues', :action => 'index', :format => 'atom'
218 :controller => 'issues', :action => 'index', :format => 'atom'
219 )
219 )
220 end
220 end
221
221
222 def test_index_pdf
222 def test_index_pdf
223 get :index, :format => 'pdf'
223 get :index, :format => 'pdf'
224 assert_response :success
224 assert_response :success
225 assert_not_nil assigns(:issues)
225 assert_not_nil assigns(:issues)
226 assert_equal 'application/pdf', @response.content_type
226 assert_equal 'application/pdf', @response.content_type
227
227
228 get :index, :project_id => 1, :format => 'pdf'
228 get :index, :project_id => 1, :format => 'pdf'
229 assert_response :success
229 assert_response :success
230 assert_not_nil assigns(:issues)
230 assert_not_nil assigns(:issues)
231 assert_equal 'application/pdf', @response.content_type
231 assert_equal 'application/pdf', @response.content_type
232
232
233 get :index, :project_id => 1, :query_id => 6, :format => 'pdf'
233 get :index, :project_id => 1, :query_id => 6, :format => 'pdf'
234 assert_response :success
234 assert_response :success
235 assert_not_nil assigns(:issues)
235 assert_not_nil assigns(:issues)
236 assert_equal 'application/pdf', @response.content_type
236 assert_equal 'application/pdf', @response.content_type
237 end
237 end
238
238
239 def test_index_pdf_with_query_grouped_by_list_custom_field
240 get :index, :project_id => 1, :query_id => 9, :format => 'pdf'
241 assert_response :success
242 assert_not_nil assigns(:issues)
243 assert_not_nil assigns(:issue_count_by_group)
244 assert_equal 'application/pdf', @response.content_type
245 end
246
239 def test_index_sort
247 def test_index_sort
240 get :index, :sort => 'tracker,id:desc'
248 get :index, :sort => 'tracker,id:desc'
241 assert_response :success
249 assert_response :success
242
250
243 sort_params = @request.session['issues_index_sort']
251 sort_params = @request.session['issues_index_sort']
244 assert sort_params.is_a?(String)
252 assert sort_params.is_a?(String)
245 assert_equal 'tracker,id:desc', sort_params
253 assert_equal 'tracker,id:desc', sort_params
246
254
247 issues = assigns(:issues)
255 issues = assigns(:issues)
248 assert_not_nil issues
256 assert_not_nil issues
249 assert !issues.empty?
257 assert !issues.empty?
250 assert_equal issues.sort {|a,b| a.tracker == b.tracker ? b.id <=> a.id : a.tracker <=> b.tracker }.collect(&:id), issues.collect(&:id)
258 assert_equal issues.sort {|a,b| a.tracker == b.tracker ? b.id <=> a.id : a.tracker <=> b.tracker }.collect(&:id), issues.collect(&:id)
251 end
259 end
252
260
253 def test_index_with_columns
261 def test_index_with_columns
254 columns = ['tracker', 'subject', 'assigned_to']
262 columns = ['tracker', 'subject', 'assigned_to']
255 get :index, :set_filter => 1, :query => { 'column_names' => columns}
263 get :index, :set_filter => 1, :query => { 'column_names' => columns}
256 assert_response :success
264 assert_response :success
257
265
258 # query should use specified columns
266 # query should use specified columns
259 query = assigns(:query)
267 query = assigns(:query)
260 assert_kind_of Query, query
268 assert_kind_of Query, query
261 assert_equal columns, query.column_names.map(&:to_s)
269 assert_equal columns, query.column_names.map(&:to_s)
262
270
263 # columns should be stored in session
271 # columns should be stored in session
264 assert_kind_of Hash, session[:query]
272 assert_kind_of Hash, session[:query]
265 assert_kind_of Array, session[:query][:column_names]
273 assert_kind_of Array, session[:query][:column_names]
266 assert_equal columns, session[:query][:column_names].map(&:to_s)
274 assert_equal columns, session[:query][:column_names].map(&:to_s)
267 end
275 end
268
276
269 def test_gantt
277 def test_gantt
270 get :gantt, :project_id => 1
278 get :gantt, :project_id => 1
271 assert_response :success
279 assert_response :success
272 assert_template 'gantt.rhtml'
280 assert_template 'gantt.rhtml'
273 assert_not_nil assigns(:gantt)
281 assert_not_nil assigns(:gantt)
274 events = assigns(:gantt).events
282 events = assigns(:gantt).events
275 assert_not_nil events
283 assert_not_nil events
276 # Issue with start and due dates
284 # Issue with start and due dates
277 i = Issue.find(1)
285 i = Issue.find(1)
278 assert_not_nil i.due_date
286 assert_not_nil i.due_date
279 assert events.include?(Issue.find(1))
287 assert events.include?(Issue.find(1))
280 # Issue with without due date but targeted to a version with date
288 # Issue with without due date but targeted to a version with date
281 i = Issue.find(2)
289 i = Issue.find(2)
282 assert_nil i.due_date
290 assert_nil i.due_date
283 assert events.include?(i)
291 assert events.include?(i)
284 end
292 end
285
293
286 def test_cross_project_gantt
294 def test_cross_project_gantt
287 get :gantt
295 get :gantt
288 assert_response :success
296 assert_response :success
289 assert_template 'gantt.rhtml'
297 assert_template 'gantt.rhtml'
290 assert_not_nil assigns(:gantt)
298 assert_not_nil assigns(:gantt)
291 events = assigns(:gantt).events
299 events = assigns(:gantt).events
292 assert_not_nil events
300 assert_not_nil events
293 end
301 end
294
302
295 def test_gantt_export_to_pdf
303 def test_gantt_export_to_pdf
296 get :gantt, :project_id => 1, :format => 'pdf'
304 get :gantt, :project_id => 1, :format => 'pdf'
297 assert_response :success
305 assert_response :success
298 assert_equal 'application/pdf', @response.content_type
306 assert_equal 'application/pdf', @response.content_type
299 assert @response.body.starts_with?('%PDF')
307 assert @response.body.starts_with?('%PDF')
300 assert_not_nil assigns(:gantt)
308 assert_not_nil assigns(:gantt)
301 end
309 end
302
310
303 def test_cross_project_gantt_export_to_pdf
311 def test_cross_project_gantt_export_to_pdf
304 get :gantt, :format => 'pdf'
312 get :gantt, :format => 'pdf'
305 assert_response :success
313 assert_response :success
306 assert_equal 'application/pdf', @response.content_type
314 assert_equal 'application/pdf', @response.content_type
307 assert @response.body.starts_with?('%PDF')
315 assert @response.body.starts_with?('%PDF')
308 assert_not_nil assigns(:gantt)
316 assert_not_nil assigns(:gantt)
309 end
317 end
310
318
311 if Object.const_defined?(:Magick)
319 if Object.const_defined?(:Magick)
312 def test_gantt_image
320 def test_gantt_image
313 get :gantt, :project_id => 1, :format => 'png'
321 get :gantt, :project_id => 1, :format => 'png'
314 assert_response :success
322 assert_response :success
315 assert_equal 'image/png', @response.content_type
323 assert_equal 'image/png', @response.content_type
316 end
324 end
317 else
325 else
318 puts "RMagick not installed. Skipping tests !!!"
326 puts "RMagick not installed. Skipping tests !!!"
319 end
327 end
320
328
321 def test_calendar
329 def test_calendar
322 get :calendar, :project_id => 1
330 get :calendar, :project_id => 1
323 assert_response :success
331 assert_response :success
324 assert_template 'calendar'
332 assert_template 'calendar'
325 assert_not_nil assigns(:calendar)
333 assert_not_nil assigns(:calendar)
326 end
334 end
327
335
328 def test_cross_project_calendar
336 def test_cross_project_calendar
329 get :calendar
337 get :calendar
330 assert_response :success
338 assert_response :success
331 assert_template 'calendar'
339 assert_template 'calendar'
332 assert_not_nil assigns(:calendar)
340 assert_not_nil assigns(:calendar)
333 end
341 end
334
342
335 def test_changes
343 def test_changes
336 get :changes, :project_id => 1
344 get :changes, :project_id => 1
337 assert_response :success
345 assert_response :success
338 assert_not_nil assigns(:journals)
346 assert_not_nil assigns(:journals)
339 assert_equal 'application/atom+xml', @response.content_type
347 assert_equal 'application/atom+xml', @response.content_type
340 end
348 end
341
349
342 def test_show_routing
350 def test_show_routing
343 assert_routing(
351 assert_routing(
344 {:method => :get, :path => '/issues/64'},
352 {:method => :get, :path => '/issues/64'},
345 :controller => 'issues', :action => 'show', :id => '64'
353 :controller => 'issues', :action => 'show', :id => '64'
346 )
354 )
347 end
355 end
348
356
349 def test_show_routing_formatted
357 def test_show_routing_formatted
350 assert_routing(
358 assert_routing(
351 {:method => :get, :path => '/issues/2332.pdf'},
359 {:method => :get, :path => '/issues/2332.pdf'},
352 :controller => 'issues', :action => 'show', :id => '2332', :format => 'pdf'
360 :controller => 'issues', :action => 'show', :id => '2332', :format => 'pdf'
353 )
361 )
354 assert_routing(
362 assert_routing(
355 {:method => :get, :path => '/issues/23123.atom'},
363 {:method => :get, :path => '/issues/23123.atom'},
356 :controller => 'issues', :action => 'show', :id => '23123', :format => 'atom'
364 :controller => 'issues', :action => 'show', :id => '23123', :format => 'atom'
357 )
365 )
358 end
366 end
359
367
360 def test_show_by_anonymous
368 def test_show_by_anonymous
361 get :show, :id => 1
369 get :show, :id => 1
362 assert_response :success
370 assert_response :success
363 assert_template 'show.rhtml'
371 assert_template 'show.rhtml'
364 assert_not_nil assigns(:issue)
372 assert_not_nil assigns(:issue)
365 assert_equal Issue.find(1), assigns(:issue)
373 assert_equal Issue.find(1), assigns(:issue)
366
374
367 # anonymous role is allowed to add a note
375 # anonymous role is allowed to add a note
368 assert_tag :tag => 'form',
376 assert_tag :tag => 'form',
369 :descendant => { :tag => 'fieldset',
377 :descendant => { :tag => 'fieldset',
370 :child => { :tag => 'legend',
378 :child => { :tag => 'legend',
371 :content => /Notes/ } }
379 :content => /Notes/ } }
372 end
380 end
373
381
374 def test_show_by_manager
382 def test_show_by_manager
375 @request.session[:user_id] = 2
383 @request.session[:user_id] = 2
376 get :show, :id => 1
384 get :show, :id => 1
377 assert_response :success
385 assert_response :success
378
386
379 assert_tag :tag => 'form',
387 assert_tag :tag => 'form',
380 :descendant => { :tag => 'fieldset',
388 :descendant => { :tag => 'fieldset',
381 :child => { :tag => 'legend',
389 :child => { :tag => 'legend',
382 :content => /Change properties/ } },
390 :content => /Change properties/ } },
383 :descendant => { :tag => 'fieldset',
391 :descendant => { :tag => 'fieldset',
384 :child => { :tag => 'legend',
392 :child => { :tag => 'legend',
385 :content => /Log time/ } },
393 :content => /Log time/ } },
386 :descendant => { :tag => 'fieldset',
394 :descendant => { :tag => 'fieldset',
387 :child => { :tag => 'legend',
395 :child => { :tag => 'legend',
388 :content => /Notes/ } }
396 :content => /Notes/ } }
389 end
397 end
390
398
391 def test_show_should_deny_anonymous_access_without_permission
399 def test_show_should_deny_anonymous_access_without_permission
392 Role.anonymous.remove_permission!(:view_issues)
400 Role.anonymous.remove_permission!(:view_issues)
393 get :show, :id => 1
401 get :show, :id => 1
394 assert_response :redirect
402 assert_response :redirect
395 end
403 end
396
404
397 def test_show_should_deny_non_member_access_without_permission
405 def test_show_should_deny_non_member_access_without_permission
398 Role.non_member.remove_permission!(:view_issues)
406 Role.non_member.remove_permission!(:view_issues)
399 @request.session[:user_id] = 9
407 @request.session[:user_id] = 9
400 get :show, :id => 1
408 get :show, :id => 1
401 assert_response 403
409 assert_response 403
402 end
410 end
403
411
404 def test_show_should_deny_member_access_without_permission
412 def test_show_should_deny_member_access_without_permission
405 Role.find(1).remove_permission!(:view_issues)
413 Role.find(1).remove_permission!(:view_issues)
406 @request.session[:user_id] = 2
414 @request.session[:user_id] = 2
407 get :show, :id => 1
415 get :show, :id => 1
408 assert_response 403
416 assert_response 403
409 end
417 end
410
418
411 def test_show_should_not_disclose_relations_to_invisible_issues
419 def test_show_should_not_disclose_relations_to_invisible_issues
412 Setting.cross_project_issue_relations = '1'
420 Setting.cross_project_issue_relations = '1'
413 IssueRelation.create!(:issue_from => Issue.find(1), :issue_to => Issue.find(2), :relation_type => 'relates')
421 IssueRelation.create!(:issue_from => Issue.find(1), :issue_to => Issue.find(2), :relation_type => 'relates')
414 # Relation to a private project issue
422 # Relation to a private project issue
415 IssueRelation.create!(:issue_from => Issue.find(1), :issue_to => Issue.find(4), :relation_type => 'relates')
423 IssueRelation.create!(:issue_from => Issue.find(1), :issue_to => Issue.find(4), :relation_type => 'relates')
416
424
417 get :show, :id => 1
425 get :show, :id => 1
418 assert_response :success
426 assert_response :success
419
427
420 assert_tag :div, :attributes => { :id => 'relations' },
428 assert_tag :div, :attributes => { :id => 'relations' },
421 :descendant => { :tag => 'a', :content => /#2$/ }
429 :descendant => { :tag => 'a', :content => /#2$/ }
422 assert_no_tag :div, :attributes => { :id => 'relations' },
430 assert_no_tag :div, :attributes => { :id => 'relations' },
423 :descendant => { :tag => 'a', :content => /#4$/ }
431 :descendant => { :tag => 'a', :content => /#4$/ }
424 end
432 end
425
433
426 def test_show_atom
434 def test_show_atom
427 get :show, :id => 2, :format => 'atom'
435 get :show, :id => 2, :format => 'atom'
428 assert_response :success
436 assert_response :success
429 assert_template 'changes.rxml'
437 assert_template 'changes.rxml'
430 # Inline image
438 # Inline image
431 assert @response.body.include?("&lt;img src=\"http://test.host/attachments/download/10\" alt=\"\" /&gt;"), "Body did not match. Body: #{@response.body}"
439 assert @response.body.include?("&lt;img src=\"http://test.host/attachments/download/10\" alt=\"\" /&gt;"), "Body did not match. Body: #{@response.body}"
432 end
440 end
433
441
434 def test_new_routing
442 def test_new_routing
435 assert_routing(
443 assert_routing(
436 {:method => :get, :path => '/projects/1/issues/new'},
444 {:method => :get, :path => '/projects/1/issues/new'},
437 :controller => 'issues', :action => 'new', :project_id => '1'
445 :controller => 'issues', :action => 'new', :project_id => '1'
438 )
446 )
439 assert_recognizes(
447 assert_recognizes(
440 {:controller => 'issues', :action => 'new', :project_id => '1'},
448 {:controller => 'issues', :action => 'new', :project_id => '1'},
441 {:method => :post, :path => '/projects/1/issues'}
449 {:method => :post, :path => '/projects/1/issues'}
442 )
450 )
443 end
451 end
444
452
445 def test_show_export_to_pdf
453 def test_show_export_to_pdf
446 get :show, :id => 3, :format => 'pdf'
454 get :show, :id => 3, :format => 'pdf'
447 assert_response :success
455 assert_response :success
448 assert_equal 'application/pdf', @response.content_type
456 assert_equal 'application/pdf', @response.content_type
449 assert @response.body.starts_with?('%PDF')
457 assert @response.body.starts_with?('%PDF')
450 assert_not_nil assigns(:issue)
458 assert_not_nil assigns(:issue)
451 end
459 end
452
460
453 def test_get_new
461 def test_get_new
454 @request.session[:user_id] = 2
462 @request.session[:user_id] = 2
455 get :new, :project_id => 1, :tracker_id => 1
463 get :new, :project_id => 1, :tracker_id => 1
456 assert_response :success
464 assert_response :success
457 assert_template 'new'
465 assert_template 'new'
458
466
459 assert_tag :tag => 'input', :attributes => { :name => 'issue[custom_field_values][2]',
467 assert_tag :tag => 'input', :attributes => { :name => 'issue[custom_field_values][2]',
460 :value => 'Default string' }
468 :value => 'Default string' }
461 end
469 end
462
470
463 def test_get_new_without_tracker_id
471 def test_get_new_without_tracker_id
464 @request.session[:user_id] = 2
472 @request.session[:user_id] = 2
465 get :new, :project_id => 1
473 get :new, :project_id => 1
466 assert_response :success
474 assert_response :success
467 assert_template 'new'
475 assert_template 'new'
468
476
469 issue = assigns(:issue)
477 issue = assigns(:issue)
470 assert_not_nil issue
478 assert_not_nil issue
471 assert_equal Project.find(1).trackers.first, issue.tracker
479 assert_equal Project.find(1).trackers.first, issue.tracker
472 end
480 end
473
481
474 def test_get_new_with_no_default_status_should_display_an_error
482 def test_get_new_with_no_default_status_should_display_an_error
475 @request.session[:user_id] = 2
483 @request.session[:user_id] = 2
476 IssueStatus.delete_all
484 IssueStatus.delete_all
477
485
478 get :new, :project_id => 1
486 get :new, :project_id => 1
479 assert_response 500
487 assert_response 500
480 assert_not_nil flash[:error]
488 assert_not_nil flash[:error]
481 assert_tag :tag => 'div', :attributes => { :class => /error/ },
489 assert_tag :tag => 'div', :attributes => { :class => /error/ },
482 :content => /No default issue/
490 :content => /No default issue/
483 end
491 end
484
492
485 def test_get_new_with_no_tracker_should_display_an_error
493 def test_get_new_with_no_tracker_should_display_an_error
486 @request.session[:user_id] = 2
494 @request.session[:user_id] = 2
487 Tracker.delete_all
495 Tracker.delete_all
488
496
489 get :new, :project_id => 1
497 get :new, :project_id => 1
490 assert_response 500
498 assert_response 500
491 assert_not_nil flash[:error]
499 assert_not_nil flash[:error]
492 assert_tag :tag => 'div', :attributes => { :class => /error/ },
500 assert_tag :tag => 'div', :attributes => { :class => /error/ },
493 :content => /No tracker/
501 :content => /No tracker/
494 end
502 end
495
503
496 def test_update_new_form
504 def test_update_new_form
497 @request.session[:user_id] = 2
505 @request.session[:user_id] = 2
498 xhr :post, :update_form, :project_id => 1,
506 xhr :post, :update_form, :project_id => 1,
499 :issue => {:tracker_id => 2,
507 :issue => {:tracker_id => 2,
500 :subject => 'This is the test_new issue',
508 :subject => 'This is the test_new issue',
501 :description => 'This is the description',
509 :description => 'This is the description',
502 :priority_id => 5}
510 :priority_id => 5}
503 assert_response :success
511 assert_response :success
504 assert_template 'attributes'
512 assert_template 'attributes'
505
513
506 issue = assigns(:issue)
514 issue = assigns(:issue)
507 assert_kind_of Issue, issue
515 assert_kind_of Issue, issue
508 assert_equal 1, issue.project_id
516 assert_equal 1, issue.project_id
509 assert_equal 2, issue.tracker_id
517 assert_equal 2, issue.tracker_id
510 assert_equal 'This is the test_new issue', issue.subject
518 assert_equal 'This is the test_new issue', issue.subject
511 end
519 end
512
520
513 def test_post_new
521 def test_post_new
514 @request.session[:user_id] = 2
522 @request.session[:user_id] = 2
515 assert_difference 'Issue.count' do
523 assert_difference 'Issue.count' do
516 post :new, :project_id => 1,
524 post :new, :project_id => 1,
517 :issue => {:tracker_id => 3,
525 :issue => {:tracker_id => 3,
518 :subject => 'This is the test_new issue',
526 :subject => 'This is the test_new issue',
519 :description => 'This is the description',
527 :description => 'This is the description',
520 :priority_id => 5,
528 :priority_id => 5,
521 :estimated_hours => '',
529 :estimated_hours => '',
522 :custom_field_values => {'2' => 'Value for field 2'}}
530 :custom_field_values => {'2' => 'Value for field 2'}}
523 end
531 end
524 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
532 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
525
533
526 issue = Issue.find_by_subject('This is the test_new issue')
534 issue = Issue.find_by_subject('This is the test_new issue')
527 assert_not_nil issue
535 assert_not_nil issue
528 assert_equal 2, issue.author_id
536 assert_equal 2, issue.author_id
529 assert_equal 3, issue.tracker_id
537 assert_equal 3, issue.tracker_id
530 assert_nil issue.estimated_hours
538 assert_nil issue.estimated_hours
531 v = issue.custom_values.find(:first, :conditions => {:custom_field_id => 2})
539 v = issue.custom_values.find(:first, :conditions => {:custom_field_id => 2})
532 assert_not_nil v
540 assert_not_nil v
533 assert_equal 'Value for field 2', v.value
541 assert_equal 'Value for field 2', v.value
534 end
542 end
535
543
536 def test_post_new_and_continue
544 def test_post_new_and_continue
537 @request.session[:user_id] = 2
545 @request.session[:user_id] = 2
538 post :new, :project_id => 1,
546 post :new, :project_id => 1,
539 :issue => {:tracker_id => 3,
547 :issue => {:tracker_id => 3,
540 :subject => 'This is first issue',
548 :subject => 'This is first issue',
541 :priority_id => 5},
549 :priority_id => 5},
542 :continue => ''
550 :continue => ''
543 assert_redirected_to :controller => 'issues', :action => 'new', :tracker_id => 3
551 assert_redirected_to :controller => 'issues', :action => 'new', :tracker_id => 3
544 end
552 end
545
553
546 def test_post_new_without_custom_fields_param
554 def test_post_new_without_custom_fields_param
547 @request.session[:user_id] = 2
555 @request.session[:user_id] = 2
548 assert_difference 'Issue.count' do
556 assert_difference 'Issue.count' do
549 post :new, :project_id => 1,
557 post :new, :project_id => 1,
550 :issue => {:tracker_id => 1,
558 :issue => {:tracker_id => 1,
551 :subject => 'This is the test_new issue',
559 :subject => 'This is the test_new issue',
552 :description => 'This is the description',
560 :description => 'This is the description',
553 :priority_id => 5}
561 :priority_id => 5}
554 end
562 end
555 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
563 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
556 end
564 end
557
565
558 def test_post_new_with_required_custom_field_and_without_custom_fields_param
566 def test_post_new_with_required_custom_field_and_without_custom_fields_param
559 field = IssueCustomField.find_by_name('Database')
567 field = IssueCustomField.find_by_name('Database')
560 field.update_attribute(:is_required, true)
568 field.update_attribute(:is_required, true)
561
569
562 @request.session[:user_id] = 2
570 @request.session[:user_id] = 2
563 post :new, :project_id => 1,
571 post :new, :project_id => 1,
564 :issue => {:tracker_id => 1,
572 :issue => {:tracker_id => 1,
565 :subject => 'This is the test_new issue',
573 :subject => 'This is the test_new issue',
566 :description => 'This is the description',
574 :description => 'This is the description',
567 :priority_id => 5}
575 :priority_id => 5}
568 assert_response :success
576 assert_response :success
569 assert_template 'new'
577 assert_template 'new'
570 issue = assigns(:issue)
578 issue = assigns(:issue)
571 assert_not_nil issue
579 assert_not_nil issue
572 assert_equal I18n.translate('activerecord.errors.messages.invalid'), issue.errors.on(:custom_values)
580 assert_equal I18n.translate('activerecord.errors.messages.invalid'), issue.errors.on(:custom_values)
573 end
581 end
574
582
575 def test_post_new_with_watchers
583 def test_post_new_with_watchers
576 @request.session[:user_id] = 2
584 @request.session[:user_id] = 2
577 ActionMailer::Base.deliveries.clear
585 ActionMailer::Base.deliveries.clear
578
586
579 assert_difference 'Watcher.count', 2 do
587 assert_difference 'Watcher.count', 2 do
580 post :new, :project_id => 1,
588 post :new, :project_id => 1,
581 :issue => {:tracker_id => 1,
589 :issue => {:tracker_id => 1,
582 :subject => 'This is a new issue with watchers',
590 :subject => 'This is a new issue with watchers',
583 :description => 'This is the description',
591 :description => 'This is the description',
584 :priority_id => 5,
592 :priority_id => 5,
585 :watcher_user_ids => ['2', '3']}
593 :watcher_user_ids => ['2', '3']}
586 end
594 end
587 issue = Issue.find_by_subject('This is a new issue with watchers')
595 issue = Issue.find_by_subject('This is a new issue with watchers')
588 assert_not_nil issue
596 assert_not_nil issue
589 assert_redirected_to :controller => 'issues', :action => 'show', :id => issue
597 assert_redirected_to :controller => 'issues', :action => 'show', :id => issue
590
598
591 # Watchers added
599 # Watchers added
592 assert_equal [2, 3], issue.watcher_user_ids.sort
600 assert_equal [2, 3], issue.watcher_user_ids.sort
593 assert issue.watched_by?(User.find(3))
601 assert issue.watched_by?(User.find(3))
594 # Watchers notified
602 # Watchers notified
595 mail = ActionMailer::Base.deliveries.last
603 mail = ActionMailer::Base.deliveries.last
596 assert_kind_of TMail::Mail, mail
604 assert_kind_of TMail::Mail, mail
597 assert [mail.bcc, mail.cc].flatten.include?(User.find(3).mail)
605 assert [mail.bcc, mail.cc].flatten.include?(User.find(3).mail)
598 end
606 end
599
607
600 def test_post_new_should_send_a_notification
608 def test_post_new_should_send_a_notification
601 ActionMailer::Base.deliveries.clear
609 ActionMailer::Base.deliveries.clear
602 @request.session[:user_id] = 2
610 @request.session[:user_id] = 2
603 assert_difference 'Issue.count' do
611 assert_difference 'Issue.count' do
604 post :new, :project_id => 1,
612 post :new, :project_id => 1,
605 :issue => {:tracker_id => 3,
613 :issue => {:tracker_id => 3,
606 :subject => 'This is the test_new issue',
614 :subject => 'This is the test_new issue',
607 :description => 'This is the description',
615 :description => 'This is the description',
608 :priority_id => 5,
616 :priority_id => 5,
609 :estimated_hours => '',
617 :estimated_hours => '',
610 :custom_field_values => {'2' => 'Value for field 2'}}
618 :custom_field_values => {'2' => 'Value for field 2'}}
611 end
619 end
612 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
620 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
613
621
614 assert_equal 1, ActionMailer::Base.deliveries.size
622 assert_equal 1, ActionMailer::Base.deliveries.size
615 end
623 end
616
624
617 def test_post_should_preserve_fields_values_on_validation_failure
625 def test_post_should_preserve_fields_values_on_validation_failure
618 @request.session[:user_id] = 2
626 @request.session[:user_id] = 2
619 post :new, :project_id => 1,
627 post :new, :project_id => 1,
620 :issue => {:tracker_id => 1,
628 :issue => {:tracker_id => 1,
621 # empty subject
629 # empty subject
622 :subject => '',
630 :subject => '',
623 :description => 'This is a description',
631 :description => 'This is a description',
624 :priority_id => 6,
632 :priority_id => 6,
625 :custom_field_values => {'1' => 'Oracle', '2' => 'Value for field 2'}}
633 :custom_field_values => {'1' => 'Oracle', '2' => 'Value for field 2'}}
626 assert_response :success
634 assert_response :success
627 assert_template 'new'
635 assert_template 'new'
628
636
629 assert_tag :textarea, :attributes => { :name => 'issue[description]' },
637 assert_tag :textarea, :attributes => { :name => 'issue[description]' },
630 :content => 'This is a description'
638 :content => 'This is a description'
631 assert_tag :select, :attributes => { :name => 'issue[priority_id]' },
639 assert_tag :select, :attributes => { :name => 'issue[priority_id]' },
632 :child => { :tag => 'option', :attributes => { :selected => 'selected',
640 :child => { :tag => 'option', :attributes => { :selected => 'selected',
633 :value => '6' },
641 :value => '6' },
634 :content => 'High' }
642 :content => 'High' }
635 # Custom fields
643 # Custom fields
636 assert_tag :select, :attributes => { :name => 'issue[custom_field_values][1]' },
644 assert_tag :select, :attributes => { :name => 'issue[custom_field_values][1]' },
637 :child => { :tag => 'option', :attributes => { :selected => 'selected',
645 :child => { :tag => 'option', :attributes => { :selected => 'selected',
638 :value => 'Oracle' },
646 :value => 'Oracle' },
639 :content => 'Oracle' }
647 :content => 'Oracle' }
640 assert_tag :input, :attributes => { :name => 'issue[custom_field_values][2]',
648 assert_tag :input, :attributes => { :name => 'issue[custom_field_values][2]',
641 :value => 'Value for field 2'}
649 :value => 'Value for field 2'}
642 end
650 end
643
651
644 def test_copy_routing
652 def test_copy_routing
645 assert_routing(
653 assert_routing(
646 {:method => :get, :path => '/projects/world_domination/issues/567/copy'},
654 {:method => :get, :path => '/projects/world_domination/issues/567/copy'},
647 :controller => 'issues', :action => 'new', :project_id => 'world_domination', :copy_from => '567'
655 :controller => 'issues', :action => 'new', :project_id => 'world_domination', :copy_from => '567'
648 )
656 )
649 end
657 end
650
658
651 def test_copy_issue
659 def test_copy_issue
652 @request.session[:user_id] = 2
660 @request.session[:user_id] = 2
653 get :new, :project_id => 1, :copy_from => 1
661 get :new, :project_id => 1, :copy_from => 1
654 assert_template 'new'
662 assert_template 'new'
655 assert_not_nil assigns(:issue)
663 assert_not_nil assigns(:issue)
656 orig = Issue.find(1)
664 orig = Issue.find(1)
657 assert_equal orig.subject, assigns(:issue).subject
665 assert_equal orig.subject, assigns(:issue).subject
658 end
666 end
659
667
660 def test_edit_routing
668 def test_edit_routing
661 assert_routing(
669 assert_routing(
662 {:method => :get, :path => '/issues/1/edit'},
670 {:method => :get, :path => '/issues/1/edit'},
663 :controller => 'issues', :action => 'edit', :id => '1'
671 :controller => 'issues', :action => 'edit', :id => '1'
664 )
672 )
665 assert_recognizes( #TODO: use a PUT on the issue URI isntead, need to adjust form
673 assert_recognizes( #TODO: use a PUT on the issue URI isntead, need to adjust form
666 {:controller => 'issues', :action => 'edit', :id => '1'},
674 {:controller => 'issues', :action => 'edit', :id => '1'},
667 {:method => :post, :path => '/issues/1/edit'}
675 {:method => :post, :path => '/issues/1/edit'}
668 )
676 )
669 end
677 end
670
678
671 def test_get_edit
679 def test_get_edit
672 @request.session[:user_id] = 2
680 @request.session[:user_id] = 2
673 get :edit, :id => 1
681 get :edit, :id => 1
674 assert_response :success
682 assert_response :success
675 assert_template 'edit'
683 assert_template 'edit'
676 assert_not_nil assigns(:issue)
684 assert_not_nil assigns(:issue)
677 assert_equal Issue.find(1), assigns(:issue)
685 assert_equal Issue.find(1), assigns(:issue)
678 end
686 end
679
687
680 def test_get_edit_with_params
688 def test_get_edit_with_params
681 @request.session[:user_id] = 2
689 @request.session[:user_id] = 2
682 get :edit, :id => 1, :issue => { :status_id => 5, :priority_id => 7 }
690 get :edit, :id => 1, :issue => { :status_id => 5, :priority_id => 7 }
683 assert_response :success
691 assert_response :success
684 assert_template 'edit'
692 assert_template 'edit'
685
693
686 issue = assigns(:issue)
694 issue = assigns(:issue)
687 assert_not_nil issue
695 assert_not_nil issue
688
696
689 assert_equal 5, issue.status_id
697 assert_equal 5, issue.status_id
690 assert_tag :select, :attributes => { :name => 'issue[status_id]' },
698 assert_tag :select, :attributes => { :name => 'issue[status_id]' },
691 :child => { :tag => 'option',
699 :child => { :tag => 'option',
692 :content => 'Closed',
700 :content => 'Closed',
693 :attributes => { :selected => 'selected' } }
701 :attributes => { :selected => 'selected' } }
694
702
695 assert_equal 7, issue.priority_id
703 assert_equal 7, issue.priority_id
696 assert_tag :select, :attributes => { :name => 'issue[priority_id]' },
704 assert_tag :select, :attributes => { :name => 'issue[priority_id]' },
697 :child => { :tag => 'option',
705 :child => { :tag => 'option',
698 :content => 'Urgent',
706 :content => 'Urgent',
699 :attributes => { :selected => 'selected' } }
707 :attributes => { :selected => 'selected' } }
700 end
708 end
701
709
702 def test_update_edit_form
710 def test_update_edit_form
703 @request.session[:user_id] = 2
711 @request.session[:user_id] = 2
704 xhr :post, :update_form, :project_id => 1,
712 xhr :post, :update_form, :project_id => 1,
705 :id => 1,
713 :id => 1,
706 :issue => {:tracker_id => 2,
714 :issue => {:tracker_id => 2,
707 :subject => 'This is the test_new issue',
715 :subject => 'This is the test_new issue',
708 :description => 'This is the description',
716 :description => 'This is the description',
709 :priority_id => 5}
717 :priority_id => 5}
710 assert_response :success
718 assert_response :success
711 assert_template 'attributes'
719 assert_template 'attributes'
712
720
713 issue = assigns(:issue)
721 issue = assigns(:issue)
714 assert_kind_of Issue, issue
722 assert_kind_of Issue, issue
715 assert_equal 1, issue.id
723 assert_equal 1, issue.id
716 assert_equal 1, issue.project_id
724 assert_equal 1, issue.project_id
717 assert_equal 2, issue.tracker_id
725 assert_equal 2, issue.tracker_id
718 assert_equal 'This is the test_new issue', issue.subject
726 assert_equal 'This is the test_new issue', issue.subject
719 end
727 end
720
728
721 def test_reply_routing
729 def test_reply_routing
722 assert_routing(
730 assert_routing(
723 {:method => :post, :path => '/issues/1/quoted'},
731 {:method => :post, :path => '/issues/1/quoted'},
724 :controller => 'issues', :action => 'reply', :id => '1'
732 :controller => 'issues', :action => 'reply', :id => '1'
725 )
733 )
726 end
734 end
727
735
728 def test_reply_to_issue
736 def test_reply_to_issue
729 @request.session[:user_id] = 2
737 @request.session[:user_id] = 2
730 get :reply, :id => 1
738 get :reply, :id => 1
731 assert_response :success
739 assert_response :success
732 assert_select_rjs :show, "update"
740 assert_select_rjs :show, "update"
733 end
741 end
734
742
735 def test_reply_to_note
743 def test_reply_to_note
736 @request.session[:user_id] = 2
744 @request.session[:user_id] = 2
737 get :reply, :id => 1, :journal_id => 2
745 get :reply, :id => 1, :journal_id => 2
738 assert_response :success
746 assert_response :success
739 assert_select_rjs :show, "update"
747 assert_select_rjs :show, "update"
740 end
748 end
741
749
742 def test_post_edit_without_custom_fields_param
750 def test_post_edit_without_custom_fields_param
743 @request.session[:user_id] = 2
751 @request.session[:user_id] = 2
744 ActionMailer::Base.deliveries.clear
752 ActionMailer::Base.deliveries.clear
745
753
746 issue = Issue.find(1)
754 issue = Issue.find(1)
747 assert_equal '125', issue.custom_value_for(2).value
755 assert_equal '125', issue.custom_value_for(2).value
748 old_subject = issue.subject
756 old_subject = issue.subject
749 new_subject = 'Subject modified by IssuesControllerTest#test_post_edit'
757 new_subject = 'Subject modified by IssuesControllerTest#test_post_edit'
750
758
751 assert_difference('Journal.count') do
759 assert_difference('Journal.count') do
752 assert_difference('JournalDetail.count', 2) do
760 assert_difference('JournalDetail.count', 2) do
753 post :edit, :id => 1, :issue => {:subject => new_subject,
761 post :edit, :id => 1, :issue => {:subject => new_subject,
754 :priority_id => '6',
762 :priority_id => '6',
755 :category_id => '1' # no change
763 :category_id => '1' # no change
756 }
764 }
757 end
765 end
758 end
766 end
759 assert_redirected_to :action => 'show', :id => '1'
767 assert_redirected_to :action => 'show', :id => '1'
760 issue.reload
768 issue.reload
761 assert_equal new_subject, issue.subject
769 assert_equal new_subject, issue.subject
762 # Make sure custom fields were not cleared
770 # Make sure custom fields were not cleared
763 assert_equal '125', issue.custom_value_for(2).value
771 assert_equal '125', issue.custom_value_for(2).value
764
772
765 mail = ActionMailer::Base.deliveries.last
773 mail = ActionMailer::Base.deliveries.last
766 assert_kind_of TMail::Mail, mail
774 assert_kind_of TMail::Mail, mail
767 assert mail.subject.starts_with?("[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}]")
775 assert mail.subject.starts_with?("[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}]")
768 assert mail.body.include?("Subject changed from #{old_subject} to #{new_subject}")
776 assert mail.body.include?("Subject changed from #{old_subject} to #{new_subject}")
769 end
777 end
770
778
771 def test_post_edit_with_custom_field_change
779 def test_post_edit_with_custom_field_change
772 @request.session[:user_id] = 2
780 @request.session[:user_id] = 2
773 issue = Issue.find(1)
781 issue = Issue.find(1)
774 assert_equal '125', issue.custom_value_for(2).value
782 assert_equal '125', issue.custom_value_for(2).value
775
783
776 assert_difference('Journal.count') do
784 assert_difference('Journal.count') do
777 assert_difference('JournalDetail.count', 3) do
785 assert_difference('JournalDetail.count', 3) do
778 post :edit, :id => 1, :issue => {:subject => 'Custom field change',
786 post :edit, :id => 1, :issue => {:subject => 'Custom field change',
779 :priority_id => '6',
787 :priority_id => '6',
780 :category_id => '1', # no change
788 :category_id => '1', # no change
781 :custom_field_values => { '2' => 'New custom value' }
789 :custom_field_values => { '2' => 'New custom value' }
782 }
790 }
783 end
791 end
784 end
792 end
785 assert_redirected_to :action => 'show', :id => '1'
793 assert_redirected_to :action => 'show', :id => '1'
786 issue.reload
794 issue.reload
787 assert_equal 'New custom value', issue.custom_value_for(2).value
795 assert_equal 'New custom value', issue.custom_value_for(2).value
788
796
789 mail = ActionMailer::Base.deliveries.last
797 mail = ActionMailer::Base.deliveries.last
790 assert_kind_of TMail::Mail, mail
798 assert_kind_of TMail::Mail, mail
791 assert mail.body.include?("Searchable field changed from 125 to New custom value")
799 assert mail.body.include?("Searchable field changed from 125 to New custom value")
792 end
800 end
793
801
794 def test_post_edit_with_status_and_assignee_change
802 def test_post_edit_with_status_and_assignee_change
795 issue = Issue.find(1)
803 issue = Issue.find(1)
796 assert_equal 1, issue.status_id
804 assert_equal 1, issue.status_id
797 @request.session[:user_id] = 2
805 @request.session[:user_id] = 2
798 assert_difference('TimeEntry.count', 0) do
806 assert_difference('TimeEntry.count', 0) do
799 post :edit,
807 post :edit,
800 :id => 1,
808 :id => 1,
801 :issue => { :status_id => 2, :assigned_to_id => 3 },
809 :issue => { :status_id => 2, :assigned_to_id => 3 },
802 :notes => 'Assigned to dlopper',
810 :notes => 'Assigned to dlopper',
803 :time_entry => { :hours => '', :comments => '', :activity_id => TimeEntryActivity.first }
811 :time_entry => { :hours => '', :comments => '', :activity_id => TimeEntryActivity.first }
804 end
812 end
805 assert_redirected_to :action => 'show', :id => '1'
813 assert_redirected_to :action => 'show', :id => '1'
806 issue.reload
814 issue.reload
807 assert_equal 2, issue.status_id
815 assert_equal 2, issue.status_id
808 j = Journal.find(:first, :order => 'id DESC')
816 j = Journal.find(:first, :order => 'id DESC')
809 assert_equal 'Assigned to dlopper', j.notes
817 assert_equal 'Assigned to dlopper', j.notes
810 assert_equal 2, j.details.size
818 assert_equal 2, j.details.size
811
819
812 mail = ActionMailer::Base.deliveries.last
820 mail = ActionMailer::Base.deliveries.last
813 assert mail.body.include?("Status changed from New to Assigned")
821 assert mail.body.include?("Status changed from New to Assigned")
814 # subject should contain the new status
822 # subject should contain the new status
815 assert mail.subject.include?("(#{ IssueStatus.find(2).name })")
823 assert mail.subject.include?("(#{ IssueStatus.find(2).name })")
816 end
824 end
817
825
818 def test_post_edit_with_note_only
826 def test_post_edit_with_note_only
819 notes = 'Note added by IssuesControllerTest#test_update_with_note_only'
827 notes = 'Note added by IssuesControllerTest#test_update_with_note_only'
820 # anonymous user
828 # anonymous user
821 post :edit,
829 post :edit,
822 :id => 1,
830 :id => 1,
823 :notes => notes
831 :notes => notes
824 assert_redirected_to :action => 'show', :id => '1'
832 assert_redirected_to :action => 'show', :id => '1'
825 j = Journal.find(:first, :order => 'id DESC')
833 j = Journal.find(:first, :order => 'id DESC')
826 assert_equal notes, j.notes
834 assert_equal notes, j.notes
827 assert_equal 0, j.details.size
835 assert_equal 0, j.details.size
828 assert_equal User.anonymous, j.user
836 assert_equal User.anonymous, j.user
829
837
830 mail = ActionMailer::Base.deliveries.last
838 mail = ActionMailer::Base.deliveries.last
831 assert mail.body.include?(notes)
839 assert mail.body.include?(notes)
832 end
840 end
833
841
834 def test_post_edit_with_note_and_spent_time
842 def test_post_edit_with_note_and_spent_time
835 @request.session[:user_id] = 2
843 @request.session[:user_id] = 2
836 spent_hours_before = Issue.find(1).spent_hours
844 spent_hours_before = Issue.find(1).spent_hours
837 assert_difference('TimeEntry.count') do
845 assert_difference('TimeEntry.count') do
838 post :edit,
846 post :edit,
839 :id => 1,
847 :id => 1,
840 :notes => '2.5 hours added',
848 :notes => '2.5 hours added',
841 :time_entry => { :hours => '2.5', :comments => '', :activity_id => TimeEntryActivity.first }
849 :time_entry => { :hours => '2.5', :comments => '', :activity_id => TimeEntryActivity.first }
842 end
850 end
843 assert_redirected_to :action => 'show', :id => '1'
851 assert_redirected_to :action => 'show', :id => '1'
844
852
845 issue = Issue.find(1)
853 issue = Issue.find(1)
846
854
847 j = Journal.find(:first, :order => 'id DESC')
855 j = Journal.find(:first, :order => 'id DESC')
848 assert_equal '2.5 hours added', j.notes
856 assert_equal '2.5 hours added', j.notes
849 assert_equal 0, j.details.size
857 assert_equal 0, j.details.size
850
858
851 t = issue.time_entries.find(:first, :order => 'id DESC')
859 t = issue.time_entries.find(:first, :order => 'id DESC')
852 assert_not_nil t
860 assert_not_nil t
853 assert_equal 2.5, t.hours
861 assert_equal 2.5, t.hours
854 assert_equal spent_hours_before + 2.5, issue.spent_hours
862 assert_equal spent_hours_before + 2.5, issue.spent_hours
855 end
863 end
856
864
857 def test_post_edit_with_attachment_only
865 def test_post_edit_with_attachment_only
858 set_tmp_attachments_directory
866 set_tmp_attachments_directory
859
867
860 # Delete all fixtured journals, a race condition can occur causing the wrong
868 # Delete all fixtured journals, a race condition can occur causing the wrong
861 # journal to get fetched in the next find.
869 # journal to get fetched in the next find.
862 Journal.delete_all
870 Journal.delete_all
863
871
864 # anonymous user
872 # anonymous user
865 post :edit,
873 post :edit,
866 :id => 1,
874 :id => 1,
867 :notes => '',
875 :notes => '',
868 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
876 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
869 assert_redirected_to :action => 'show', :id => '1'
877 assert_redirected_to :action => 'show', :id => '1'
870 j = Issue.find(1).journals.find(:first, :order => 'id DESC')
878 j = Issue.find(1).journals.find(:first, :order => 'id DESC')
871 assert j.notes.blank?
879 assert j.notes.blank?
872 assert_equal 1, j.details.size
880 assert_equal 1, j.details.size
873 assert_equal 'testfile.txt', j.details.first.value
881 assert_equal 'testfile.txt', j.details.first.value
874 assert_equal User.anonymous, j.user
882 assert_equal User.anonymous, j.user
875
883
876 mail = ActionMailer::Base.deliveries.last
884 mail = ActionMailer::Base.deliveries.last
877 assert mail.body.include?('testfile.txt')
885 assert mail.body.include?('testfile.txt')
878 end
886 end
879
887
880 def test_post_edit_with_no_change
888 def test_post_edit_with_no_change
881 issue = Issue.find(1)
889 issue = Issue.find(1)
882 issue.journals.clear
890 issue.journals.clear
883 ActionMailer::Base.deliveries.clear
891 ActionMailer::Base.deliveries.clear
884
892
885 post :edit,
893 post :edit,
886 :id => 1,
894 :id => 1,
887 :notes => ''
895 :notes => ''
888 assert_redirected_to :action => 'show', :id => '1'
896 assert_redirected_to :action => 'show', :id => '1'
889
897
890 issue.reload
898 issue.reload
891 assert issue.journals.empty?
899 assert issue.journals.empty?
892 # No email should be sent
900 # No email should be sent
893 assert ActionMailer::Base.deliveries.empty?
901 assert ActionMailer::Base.deliveries.empty?
894 end
902 end
895
903
896 def test_post_edit_should_send_a_notification
904 def test_post_edit_should_send_a_notification
897 @request.session[:user_id] = 2
905 @request.session[:user_id] = 2
898 ActionMailer::Base.deliveries.clear
906 ActionMailer::Base.deliveries.clear
899 issue = Issue.find(1)
907 issue = Issue.find(1)
900 old_subject = issue.subject
908 old_subject = issue.subject
901 new_subject = 'Subject modified by IssuesControllerTest#test_post_edit'
909 new_subject = 'Subject modified by IssuesControllerTest#test_post_edit'
902
910
903 post :edit, :id => 1, :issue => {:subject => new_subject,
911 post :edit, :id => 1, :issue => {:subject => new_subject,
904 :priority_id => '6',
912 :priority_id => '6',
905 :category_id => '1' # no change
913 :category_id => '1' # no change
906 }
914 }
907 assert_equal 1, ActionMailer::Base.deliveries.size
915 assert_equal 1, ActionMailer::Base.deliveries.size
908 end
916 end
909
917
910 def test_post_edit_with_invalid_spent_time
918 def test_post_edit_with_invalid_spent_time
911 @request.session[:user_id] = 2
919 @request.session[:user_id] = 2
912 notes = 'Note added by IssuesControllerTest#test_post_edit_with_invalid_spent_time'
920 notes = 'Note added by IssuesControllerTest#test_post_edit_with_invalid_spent_time'
913
921
914 assert_no_difference('Journal.count') do
922 assert_no_difference('Journal.count') do
915 post :edit,
923 post :edit,
916 :id => 1,
924 :id => 1,
917 :notes => notes,
925 :notes => notes,
918 :time_entry => {"comments"=>"", "activity_id"=>"", "hours"=>"2z"}
926 :time_entry => {"comments"=>"", "activity_id"=>"", "hours"=>"2z"}
919 end
927 end
920 assert_response :success
928 assert_response :success
921 assert_template 'edit'
929 assert_template 'edit'
922
930
923 assert_tag :textarea, :attributes => { :name => 'notes' },
931 assert_tag :textarea, :attributes => { :name => 'notes' },
924 :content => notes
932 :content => notes
925 assert_tag :input, :attributes => { :name => 'time_entry[hours]', :value => "2z" }
933 assert_tag :input, :attributes => { :name => 'time_entry[hours]', :value => "2z" }
926 end
934 end
927
935
928 def test_post_edit_should_allow_fixed_version_to_be_set_to_a_subproject
936 def test_post_edit_should_allow_fixed_version_to_be_set_to_a_subproject
929 issue = Issue.find(2)
937 issue = Issue.find(2)
930 @request.session[:user_id] = 2
938 @request.session[:user_id] = 2
931
939
932 post :edit,
940 post :edit,
933 :id => issue.id,
941 :id => issue.id,
934 :issue => {
942 :issue => {
935 :fixed_version_id => 4
943 :fixed_version_id => 4
936 }
944 }
937
945
938 assert_response :redirect
946 assert_response :redirect
939 issue.reload
947 issue.reload
940 assert_equal 4, issue.fixed_version_id
948 assert_equal 4, issue.fixed_version_id
941 assert_not_equal issue.project_id, issue.fixed_version.project_id
949 assert_not_equal issue.project_id, issue.fixed_version.project_id
942 end
950 end
943
951
944 def test_post_edit_should_redirect_back_using_the_back_url_parameter
952 def test_post_edit_should_redirect_back_using_the_back_url_parameter
945 issue = Issue.find(2)
953 issue = Issue.find(2)
946 @request.session[:user_id] = 2
954 @request.session[:user_id] = 2
947
955
948 post :edit,
956 post :edit,
949 :id => issue.id,
957 :id => issue.id,
950 :issue => {
958 :issue => {
951 :fixed_version_id => 4
959 :fixed_version_id => 4
952 },
960 },
953 :back_url => '/issues'
961 :back_url => '/issues'
954
962
955 assert_response :redirect
963 assert_response :redirect
956 assert_redirected_to '/issues'
964 assert_redirected_to '/issues'
957 end
965 end
958
966
959 def test_post_edit_should_not_redirect_back_using_the_back_url_parameter_off_the_host
967 def test_post_edit_should_not_redirect_back_using_the_back_url_parameter_off_the_host
960 issue = Issue.find(2)
968 issue = Issue.find(2)
961 @request.session[:user_id] = 2
969 @request.session[:user_id] = 2
962
970
963 post :edit,
971 post :edit,
964 :id => issue.id,
972 :id => issue.id,
965 :issue => {
973 :issue => {
966 :fixed_version_id => 4
974 :fixed_version_id => 4
967 },
975 },
968 :back_url => 'http://google.com'
976 :back_url => 'http://google.com'
969
977
970 assert_response :redirect
978 assert_response :redirect
971 assert_redirected_to :controller => 'issues', :action => 'show', :id => issue.id
979 assert_redirected_to :controller => 'issues', :action => 'show', :id => issue.id
972 end
980 end
973
981
974 def test_get_bulk_edit
982 def test_get_bulk_edit
975 @request.session[:user_id] = 2
983 @request.session[:user_id] = 2
976 get :bulk_edit, :ids => [1, 2]
984 get :bulk_edit, :ids => [1, 2]
977 assert_response :success
985 assert_response :success
978 assert_template 'bulk_edit'
986 assert_template 'bulk_edit'
979
987
980 # Project specific custom field, date type
988 # Project specific custom field, date type
981 field = CustomField.find(9)
989 field = CustomField.find(9)
982 assert !field.is_for_all?
990 assert !field.is_for_all?
983 assert_equal 'date', field.field_format
991 assert_equal 'date', field.field_format
984 assert_tag :input, :attributes => {:name => 'custom_field_values[9]'}
992 assert_tag :input, :attributes => {:name => 'custom_field_values[9]'}
985
993
986 # System wide custom field
994 # System wide custom field
987 assert CustomField.find(1).is_for_all?
995 assert CustomField.find(1).is_for_all?
988 assert_tag :select, :attributes => {:name => 'custom_field_values[1]'}
996 assert_tag :select, :attributes => {:name => 'custom_field_values[1]'}
989 end
997 end
990
998
991 def test_bulk_edit
999 def test_bulk_edit
992 @request.session[:user_id] = 2
1000 @request.session[:user_id] = 2
993 # update issues priority
1001 # update issues priority
994 post :bulk_edit, :ids => [1, 2], :priority_id => 7,
1002 post :bulk_edit, :ids => [1, 2], :priority_id => 7,
995 :assigned_to_id => '',
1003 :assigned_to_id => '',
996 :custom_field_values => {'2' => ''},
1004 :custom_field_values => {'2' => ''},
997 :notes => 'Bulk editing'
1005 :notes => 'Bulk editing'
998 assert_response 302
1006 assert_response 302
999 # check that the issues were updated
1007 # check that the issues were updated
1000 assert_equal [7, 7], Issue.find_all_by_id([1, 2]).collect {|i| i.priority.id}
1008 assert_equal [7, 7], Issue.find_all_by_id([1, 2]).collect {|i| i.priority.id}
1001
1009
1002 issue = Issue.find(1)
1010 issue = Issue.find(1)
1003 journal = issue.journals.find(:first, :order => 'created_on DESC')
1011 journal = issue.journals.find(:first, :order => 'created_on DESC')
1004 assert_equal '125', issue.custom_value_for(2).value
1012 assert_equal '125', issue.custom_value_for(2).value
1005 assert_equal 'Bulk editing', journal.notes
1013 assert_equal 'Bulk editing', journal.notes
1006 assert_equal 1, journal.details.size
1014 assert_equal 1, journal.details.size
1007 end
1015 end
1008
1016
1009 def test_bullk_edit_should_send_a_notification
1017 def test_bullk_edit_should_send_a_notification
1010 @request.session[:user_id] = 2
1018 @request.session[:user_id] = 2
1011 ActionMailer::Base.deliveries.clear
1019 ActionMailer::Base.deliveries.clear
1012 post(:bulk_edit,
1020 post(:bulk_edit,
1013 {
1021 {
1014 :ids => [1, 2],
1022 :ids => [1, 2],
1015 :priority_id => 7,
1023 :priority_id => 7,
1016 :assigned_to_id => '',
1024 :assigned_to_id => '',
1017 :custom_field_values => {'2' => ''},
1025 :custom_field_values => {'2' => ''},
1018 :notes => 'Bulk editing'
1026 :notes => 'Bulk editing'
1019 })
1027 })
1020
1028
1021 assert_response 302
1029 assert_response 302
1022 assert_equal 2, ActionMailer::Base.deliveries.size
1030 assert_equal 2, ActionMailer::Base.deliveries.size
1023 end
1031 end
1024
1032
1025 def test_bulk_edit_status
1033 def test_bulk_edit_status
1026 @request.session[:user_id] = 2
1034 @request.session[:user_id] = 2
1027 # update issues priority
1035 # update issues priority
1028 post :bulk_edit, :ids => [1, 2], :priority_id => '',
1036 post :bulk_edit, :ids => [1, 2], :priority_id => '',
1029 :assigned_to_id => '',
1037 :assigned_to_id => '',
1030 :status_id => '5',
1038 :status_id => '5',
1031 :notes => 'Bulk editing status'
1039 :notes => 'Bulk editing status'
1032 assert_response 302
1040 assert_response 302
1033 issue = Issue.find(1)
1041 issue = Issue.find(1)
1034 assert issue.closed?
1042 assert issue.closed?
1035 end
1043 end
1036
1044
1037 def test_bulk_edit_custom_field
1045 def test_bulk_edit_custom_field
1038 @request.session[:user_id] = 2
1046 @request.session[:user_id] = 2
1039 # update issues priority
1047 # update issues priority
1040 post :bulk_edit, :ids => [1, 2], :priority_id => '',
1048 post :bulk_edit, :ids => [1, 2], :priority_id => '',
1041 :assigned_to_id => '',
1049 :assigned_to_id => '',
1042 :custom_field_values => {'2' => '777'},
1050 :custom_field_values => {'2' => '777'},
1043 :notes => 'Bulk editing custom field'
1051 :notes => 'Bulk editing custom field'
1044 assert_response 302
1052 assert_response 302
1045
1053
1046 issue = Issue.find(1)
1054 issue = Issue.find(1)
1047 journal = issue.journals.find(:first, :order => 'created_on DESC')
1055 journal = issue.journals.find(:first, :order => 'created_on DESC')
1048 assert_equal '777', issue.custom_value_for(2).value
1056 assert_equal '777', issue.custom_value_for(2).value
1049 assert_equal 1, journal.details.size
1057 assert_equal 1, journal.details.size
1050 assert_equal '125', journal.details.first.old_value
1058 assert_equal '125', journal.details.first.old_value
1051 assert_equal '777', journal.details.first.value
1059 assert_equal '777', journal.details.first.value
1052 end
1060 end
1053
1061
1054 def test_bulk_unassign
1062 def test_bulk_unassign
1055 assert_not_nil Issue.find(2).assigned_to
1063 assert_not_nil Issue.find(2).assigned_to
1056 @request.session[:user_id] = 2
1064 @request.session[:user_id] = 2
1057 # unassign issues
1065 # unassign issues
1058 post :bulk_edit, :ids => [1, 2], :notes => 'Bulk unassigning', :assigned_to_id => 'none'
1066 post :bulk_edit, :ids => [1, 2], :notes => 'Bulk unassigning', :assigned_to_id => 'none'
1059 assert_response 302
1067 assert_response 302
1060 # check that the issues were updated
1068 # check that the issues were updated
1061 assert_nil Issue.find(2).assigned_to
1069 assert_nil Issue.find(2).assigned_to
1062 end
1070 end
1063
1071
1064 def test_post_bulk_edit_should_allow_fixed_version_to_be_set_to_a_subproject
1072 def test_post_bulk_edit_should_allow_fixed_version_to_be_set_to_a_subproject
1065 @request.session[:user_id] = 2
1073 @request.session[:user_id] = 2
1066
1074
1067 post :bulk_edit,
1075 post :bulk_edit,
1068 :ids => [1,2],
1076 :ids => [1,2],
1069 :fixed_version_id => 4
1077 :fixed_version_id => 4
1070
1078
1071 assert_response :redirect
1079 assert_response :redirect
1072 issues = Issue.find([1,2])
1080 issues = Issue.find([1,2])
1073 issues.each do |issue|
1081 issues.each do |issue|
1074 assert_equal 4, issue.fixed_version_id
1082 assert_equal 4, issue.fixed_version_id
1075 assert_not_equal issue.project_id, issue.fixed_version.project_id
1083 assert_not_equal issue.project_id, issue.fixed_version.project_id
1076 end
1084 end
1077 end
1085 end
1078
1086
1079 def test_post_bulk_edit_should_redirect_back_using_the_back_url_parameter
1087 def test_post_bulk_edit_should_redirect_back_using_the_back_url_parameter
1080 @request.session[:user_id] = 2
1088 @request.session[:user_id] = 2
1081 post :bulk_edit, :ids => [1,2], :back_url => '/issues'
1089 post :bulk_edit, :ids => [1,2], :back_url => '/issues'
1082
1090
1083 assert_response :redirect
1091 assert_response :redirect
1084 assert_redirected_to '/issues'
1092 assert_redirected_to '/issues'
1085 end
1093 end
1086
1094
1087 def test_post_bulk_edit_should_not_redirect_back_using_the_back_url_parameter_off_the_host
1095 def test_post_bulk_edit_should_not_redirect_back_using_the_back_url_parameter_off_the_host
1088 @request.session[:user_id] = 2
1096 @request.session[:user_id] = 2
1089 post :bulk_edit, :ids => [1,2], :back_url => 'http://google.com'
1097 post :bulk_edit, :ids => [1,2], :back_url => 'http://google.com'
1090
1098
1091 assert_response :redirect
1099 assert_response :redirect
1092 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => Project.find(1).identifier
1100 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => Project.find(1).identifier
1093 end
1101 end
1094
1102
1095 def test_move_routing
1103 def test_move_routing
1096 assert_routing(
1104 assert_routing(
1097 {:method => :get, :path => '/issues/1/move'},
1105 {:method => :get, :path => '/issues/1/move'},
1098 :controller => 'issues', :action => 'move', :id => '1'
1106 :controller => 'issues', :action => 'move', :id => '1'
1099 )
1107 )
1100 assert_recognizes(
1108 assert_recognizes(
1101 {:controller => 'issues', :action => 'move', :id => '1'},
1109 {:controller => 'issues', :action => 'move', :id => '1'},
1102 {:method => :post, :path => '/issues/1/move'}
1110 {:method => :post, :path => '/issues/1/move'}
1103 )
1111 )
1104 end
1112 end
1105
1113
1106 def test_move_one_issue_to_another_project
1114 def test_move_one_issue_to_another_project
1107 @request.session[:user_id] = 2
1115 @request.session[:user_id] = 2
1108 post :move, :id => 1, :new_project_id => 2, :tracker_id => '', :assigned_to_id => '', :status_id => '', :start_date => '', :due_date => ''
1116 post :move, :id => 1, :new_project_id => 2, :tracker_id => '', :assigned_to_id => '', :status_id => '', :start_date => '', :due_date => ''
1109 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1117 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1110 assert_equal 2, Issue.find(1).project_id
1118 assert_equal 2, Issue.find(1).project_id
1111 end
1119 end
1112
1120
1113 def test_move_one_issue_to_another_project_should_follow_when_needed
1121 def test_move_one_issue_to_another_project_should_follow_when_needed
1114 @request.session[:user_id] = 2
1122 @request.session[:user_id] = 2
1115 post :move, :id => 1, :new_project_id => 2, :follow => '1'
1123 post :move, :id => 1, :new_project_id => 2, :follow => '1'
1116 assert_redirected_to '/issues/1'
1124 assert_redirected_to '/issues/1'
1117 end
1125 end
1118
1126
1119 def test_bulk_move_to_another_project
1127 def test_bulk_move_to_another_project
1120 @request.session[:user_id] = 2
1128 @request.session[:user_id] = 2
1121 post :move, :ids => [1, 2], :new_project_id => 2
1129 post :move, :ids => [1, 2], :new_project_id => 2
1122 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1130 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1123 # Issues moved to project 2
1131 # Issues moved to project 2
1124 assert_equal 2, Issue.find(1).project_id
1132 assert_equal 2, Issue.find(1).project_id
1125 assert_equal 2, Issue.find(2).project_id
1133 assert_equal 2, Issue.find(2).project_id
1126 # No tracker change
1134 # No tracker change
1127 assert_equal 1, Issue.find(1).tracker_id
1135 assert_equal 1, Issue.find(1).tracker_id
1128 assert_equal 2, Issue.find(2).tracker_id
1136 assert_equal 2, Issue.find(2).tracker_id
1129 end
1137 end
1130
1138
1131 def test_bulk_move_to_another_tracker
1139 def test_bulk_move_to_another_tracker
1132 @request.session[:user_id] = 2
1140 @request.session[:user_id] = 2
1133 post :move, :ids => [1, 2], :new_tracker_id => 2
1141 post :move, :ids => [1, 2], :new_tracker_id => 2
1134 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1142 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1135 assert_equal 2, Issue.find(1).tracker_id
1143 assert_equal 2, Issue.find(1).tracker_id
1136 assert_equal 2, Issue.find(2).tracker_id
1144 assert_equal 2, Issue.find(2).tracker_id
1137 end
1145 end
1138
1146
1139 def test_bulk_copy_to_another_project
1147 def test_bulk_copy_to_another_project
1140 @request.session[:user_id] = 2
1148 @request.session[:user_id] = 2
1141 assert_difference 'Issue.count', 2 do
1149 assert_difference 'Issue.count', 2 do
1142 assert_no_difference 'Project.find(1).issues.count' do
1150 assert_no_difference 'Project.find(1).issues.count' do
1143 post :move, :ids => [1, 2], :new_project_id => 2, :copy_options => {:copy => '1'}
1151 post :move, :ids => [1, 2], :new_project_id => 2, :copy_options => {:copy => '1'}
1144 end
1152 end
1145 end
1153 end
1146 assert_redirected_to 'projects/ecookbook/issues'
1154 assert_redirected_to 'projects/ecookbook/issues'
1147 end
1155 end
1148
1156
1149 context "#move via bulk copy" do
1157 context "#move via bulk copy" do
1150 should "allow not changing the issue's attributes" do
1158 should "allow not changing the issue's attributes" do
1151 @request.session[:user_id] = 2
1159 @request.session[:user_id] = 2
1152 issue_before_move = Issue.find(1)
1160 issue_before_move = Issue.find(1)
1153 assert_difference 'Issue.count', 1 do
1161 assert_difference 'Issue.count', 1 do
1154 assert_no_difference 'Project.find(1).issues.count' do
1162 assert_no_difference 'Project.find(1).issues.count' do
1155 post :move, :ids => [1], :new_project_id => 2, :copy_options => {:copy => '1'}, :new_tracker_id => '', :assigned_to_id => '', :status_id => '', :start_date => '', :due_date => ''
1163 post :move, :ids => [1], :new_project_id => 2, :copy_options => {:copy => '1'}, :new_tracker_id => '', :assigned_to_id => '', :status_id => '', :start_date => '', :due_date => ''
1156 end
1164 end
1157 end
1165 end
1158 issue_after_move = Issue.first(:order => 'id desc', :conditions => {:project_id => 2})
1166 issue_after_move = Issue.first(:order => 'id desc', :conditions => {:project_id => 2})
1159 assert_equal issue_before_move.tracker_id, issue_after_move.tracker_id
1167 assert_equal issue_before_move.tracker_id, issue_after_move.tracker_id
1160 assert_equal issue_before_move.status_id, issue_after_move.status_id
1168 assert_equal issue_before_move.status_id, issue_after_move.status_id
1161 assert_equal issue_before_move.assigned_to_id, issue_after_move.assigned_to_id
1169 assert_equal issue_before_move.assigned_to_id, issue_after_move.assigned_to_id
1162 end
1170 end
1163
1171
1164 should "allow changing the issue's attributes" do
1172 should "allow changing the issue's attributes" do
1165 @request.session[:user_id] = 2
1173 @request.session[:user_id] = 2
1166 assert_difference 'Issue.count', 2 do
1174 assert_difference 'Issue.count', 2 do
1167 assert_no_difference 'Project.find(1).issues.count' do
1175 assert_no_difference 'Project.find(1).issues.count' do
1168 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'
1176 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'
1169 end
1177 end
1170 end
1178 end
1171
1179
1172 copied_issues = Issue.all(:limit => 2, :order => 'id desc', :conditions => {:project_id => 2})
1180 copied_issues = Issue.all(:limit => 2, :order => 'id desc', :conditions => {:project_id => 2})
1173 assert_equal 2, copied_issues.size
1181 assert_equal 2, copied_issues.size
1174 copied_issues.each do |issue|
1182 copied_issues.each do |issue|
1175 assert_equal 2, issue.project_id, "Project is incorrect"
1183 assert_equal 2, issue.project_id, "Project is incorrect"
1176 assert_equal 4, issue.assigned_to_id, "Assigned to is incorrect"
1184 assert_equal 4, issue.assigned_to_id, "Assigned to is incorrect"
1177 assert_equal 3, issue.status_id, "Status is incorrect"
1185 assert_equal 3, issue.status_id, "Status is incorrect"
1178 assert_equal '2009-12-01', issue.start_date.to_s, "Start date is incorrect"
1186 assert_equal '2009-12-01', issue.start_date.to_s, "Start date is incorrect"
1179 assert_equal '2009-12-31', issue.due_date.to_s, "Due date is incorrect"
1187 assert_equal '2009-12-31', issue.due_date.to_s, "Due date is incorrect"
1180 end
1188 end
1181 end
1189 end
1182 end
1190 end
1183
1191
1184 def test_copy_to_another_project_should_follow_when_needed
1192 def test_copy_to_another_project_should_follow_when_needed
1185 @request.session[:user_id] = 2
1193 @request.session[:user_id] = 2
1186 post :move, :ids => [1], :new_project_id => 2, :copy_options => {:copy => '1'}, :follow => '1'
1194 post :move, :ids => [1], :new_project_id => 2, :copy_options => {:copy => '1'}, :follow => '1'
1187 issue = Issue.first(:order => 'id DESC')
1195 issue = Issue.first(:order => 'id DESC')
1188 assert_redirected_to :controller => 'issues', :action => 'show', :id => issue
1196 assert_redirected_to :controller => 'issues', :action => 'show', :id => issue
1189 end
1197 end
1190
1198
1191 def test_context_menu_one_issue
1199 def test_context_menu_one_issue
1192 @request.session[:user_id] = 2
1200 @request.session[:user_id] = 2
1193 get :context_menu, :ids => [1]
1201 get :context_menu, :ids => [1]
1194 assert_response :success
1202 assert_response :success
1195 assert_template 'context_menu'
1203 assert_template 'context_menu'
1196 assert_tag :tag => 'a', :content => 'Edit',
1204 assert_tag :tag => 'a', :content => 'Edit',
1197 :attributes => { :href => '/issues/1/edit',
1205 :attributes => { :href => '/issues/1/edit',
1198 :class => 'icon-edit' }
1206 :class => 'icon-edit' }
1199 assert_tag :tag => 'a', :content => 'Closed',
1207 assert_tag :tag => 'a', :content => 'Closed',
1200 :attributes => { :href => '/issues/1/edit?issue%5Bstatus_id%5D=5',
1208 :attributes => { :href => '/issues/1/edit?issue%5Bstatus_id%5D=5',
1201 :class => '' }
1209 :class => '' }
1202 assert_tag :tag => 'a', :content => 'Immediate',
1210 assert_tag :tag => 'a', :content => 'Immediate',
1203 :attributes => { :href => '/issues/bulk_edit?ids%5B%5D=1&amp;priority_id=8',
1211 :attributes => { :href => '/issues/bulk_edit?ids%5B%5D=1&amp;priority_id=8',
1204 :class => '' }
1212 :class => '' }
1205 # Versions
1213 # Versions
1206 assert_tag :tag => 'a', :content => '2.0',
1214 assert_tag :tag => 'a', :content => '2.0',
1207 :attributes => { :href => '/issues/bulk_edit?fixed_version_id=3&amp;ids%5B%5D=1',
1215 :attributes => { :href => '/issues/bulk_edit?fixed_version_id=3&amp;ids%5B%5D=1',
1208 :class => '' }
1216 :class => '' }
1209 assert_tag :tag => 'a', :content => 'eCookbook Subproject 1 - 2.0',
1217 assert_tag :tag => 'a', :content => 'eCookbook Subproject 1 - 2.0',
1210 :attributes => { :href => '/issues/bulk_edit?fixed_version_id=4&amp;ids%5B%5D=1',
1218 :attributes => { :href => '/issues/bulk_edit?fixed_version_id=4&amp;ids%5B%5D=1',
1211 :class => '' }
1219 :class => '' }
1212
1220
1213 assert_tag :tag => 'a', :content => 'Dave Lopper',
1221 assert_tag :tag => 'a', :content => 'Dave Lopper',
1214 :attributes => { :href => '/issues/bulk_edit?assigned_to_id=3&amp;ids%5B%5D=1',
1222 :attributes => { :href => '/issues/bulk_edit?assigned_to_id=3&amp;ids%5B%5D=1',
1215 :class => '' }
1223 :class => '' }
1216 assert_tag :tag => 'a', :content => 'Duplicate',
1224 assert_tag :tag => 'a', :content => 'Duplicate',
1217 :attributes => { :href => '/projects/ecookbook/issues/1/copy',
1225 :attributes => { :href => '/projects/ecookbook/issues/1/copy',
1218 :class => 'icon-duplicate' }
1226 :class => 'icon-duplicate' }
1219 assert_tag :tag => 'a', :content => 'Copy',
1227 assert_tag :tag => 'a', :content => 'Copy',
1220 :attributes => { :href => '/issues/move?copy_options%5Bcopy%5D=t&amp;ids%5B%5D=1',
1228 :attributes => { :href => '/issues/move?copy_options%5Bcopy%5D=t&amp;ids%5B%5D=1',
1221 :class => 'icon-copy' }
1229 :class => 'icon-copy' }
1222 assert_tag :tag => 'a', :content => 'Move',
1230 assert_tag :tag => 'a', :content => 'Move',
1223 :attributes => { :href => '/issues/move?ids%5B%5D=1',
1231 :attributes => { :href => '/issues/move?ids%5B%5D=1',
1224 :class => 'icon-move' }
1232 :class => 'icon-move' }
1225 assert_tag :tag => 'a', :content => 'Delete',
1233 assert_tag :tag => 'a', :content => 'Delete',
1226 :attributes => { :href => '/issues/destroy?ids%5B%5D=1',
1234 :attributes => { :href => '/issues/destroy?ids%5B%5D=1',
1227 :class => 'icon-del' }
1235 :class => 'icon-del' }
1228 end
1236 end
1229
1237
1230 def test_context_menu_one_issue_by_anonymous
1238 def test_context_menu_one_issue_by_anonymous
1231 get :context_menu, :ids => [1]
1239 get :context_menu, :ids => [1]
1232 assert_response :success
1240 assert_response :success
1233 assert_template 'context_menu'
1241 assert_template 'context_menu'
1234 assert_tag :tag => 'a', :content => 'Delete',
1242 assert_tag :tag => 'a', :content => 'Delete',
1235 :attributes => { :href => '#',
1243 :attributes => { :href => '#',
1236 :class => 'icon-del disabled' }
1244 :class => 'icon-del disabled' }
1237 end
1245 end
1238
1246
1239 def test_context_menu_multiple_issues_of_same_project
1247 def test_context_menu_multiple_issues_of_same_project
1240 @request.session[:user_id] = 2
1248 @request.session[:user_id] = 2
1241 get :context_menu, :ids => [1, 2]
1249 get :context_menu, :ids => [1, 2]
1242 assert_response :success
1250 assert_response :success
1243 assert_template 'context_menu'
1251 assert_template 'context_menu'
1244 assert_tag :tag => 'a', :content => 'Edit',
1252 assert_tag :tag => 'a', :content => 'Edit',
1245 :attributes => { :href => '/issues/bulk_edit?ids%5B%5D=1&amp;ids%5B%5D=2',
1253 :attributes => { :href => '/issues/bulk_edit?ids%5B%5D=1&amp;ids%5B%5D=2',
1246 :class => 'icon-edit' }
1254 :class => 'icon-edit' }
1247 assert_tag :tag => 'a', :content => 'Immediate',
1255 assert_tag :tag => 'a', :content => 'Immediate',
1248 :attributes => { :href => '/issues/bulk_edit?ids%5B%5D=1&amp;ids%5B%5D=2&amp;priority_id=8',
1256 :attributes => { :href => '/issues/bulk_edit?ids%5B%5D=1&amp;ids%5B%5D=2&amp;priority_id=8',
1249 :class => '' }
1257 :class => '' }
1250 assert_tag :tag => 'a', :content => 'Dave Lopper',
1258 assert_tag :tag => 'a', :content => 'Dave Lopper',
1251 :attributes => { :href => '/issues/bulk_edit?assigned_to_id=3&amp;ids%5B%5D=1&amp;ids%5B%5D=2',
1259 :attributes => { :href => '/issues/bulk_edit?assigned_to_id=3&amp;ids%5B%5D=1&amp;ids%5B%5D=2',
1252 :class => '' }
1260 :class => '' }
1253 assert_tag :tag => 'a', :content => 'Copy',
1261 assert_tag :tag => 'a', :content => 'Copy',
1254 :attributes => { :href => '/issues/move?copy_options%5Bcopy%5D=t&amp;ids%5B%5D=1&amp;ids%5B%5D=2',
1262 :attributes => { :href => '/issues/move?copy_options%5Bcopy%5D=t&amp;ids%5B%5D=1&amp;ids%5B%5D=2',
1255 :class => 'icon-copy' }
1263 :class => 'icon-copy' }
1256 assert_tag :tag => 'a', :content => 'Move',
1264 assert_tag :tag => 'a', :content => 'Move',
1257 :attributes => { :href => '/issues/move?ids%5B%5D=1&amp;ids%5B%5D=2',
1265 :attributes => { :href => '/issues/move?ids%5B%5D=1&amp;ids%5B%5D=2',
1258 :class => 'icon-move' }
1266 :class => 'icon-move' }
1259 assert_tag :tag => 'a', :content => 'Delete',
1267 assert_tag :tag => 'a', :content => 'Delete',
1260 :attributes => { :href => '/issues/destroy?ids%5B%5D=1&amp;ids%5B%5D=2',
1268 :attributes => { :href => '/issues/destroy?ids%5B%5D=1&amp;ids%5B%5D=2',
1261 :class => 'icon-del' }
1269 :class => 'icon-del' }
1262 end
1270 end
1263
1271
1264 def test_context_menu_multiple_issues_of_different_project
1272 def test_context_menu_multiple_issues_of_different_project
1265 @request.session[:user_id] = 2
1273 @request.session[:user_id] = 2
1266 get :context_menu, :ids => [1, 2, 4]
1274 get :context_menu, :ids => [1, 2, 4]
1267 assert_response :success
1275 assert_response :success
1268 assert_template 'context_menu'
1276 assert_template 'context_menu'
1269 assert_tag :tag => 'a', :content => 'Delete',
1277 assert_tag :tag => 'a', :content => 'Delete',
1270 :attributes => { :href => '#',
1278 :attributes => { :href => '#',
1271 :class => 'icon-del disabled' }
1279 :class => 'icon-del disabled' }
1272 end
1280 end
1273
1281
1274 def test_destroy_routing
1282 def test_destroy_routing
1275 assert_recognizes( #TODO: use DELETE on issue URI (need to change forms)
1283 assert_recognizes( #TODO: use DELETE on issue URI (need to change forms)
1276 {:controller => 'issues', :action => 'destroy', :id => '1'},
1284 {:controller => 'issues', :action => 'destroy', :id => '1'},
1277 {:method => :post, :path => '/issues/1/destroy'}
1285 {:method => :post, :path => '/issues/1/destroy'}
1278 )
1286 )
1279 end
1287 end
1280
1288
1281 def test_destroy_issue_with_no_time_entries
1289 def test_destroy_issue_with_no_time_entries
1282 assert_nil TimeEntry.find_by_issue_id(2)
1290 assert_nil TimeEntry.find_by_issue_id(2)
1283 @request.session[:user_id] = 2
1291 @request.session[:user_id] = 2
1284 post :destroy, :id => 2
1292 post :destroy, :id => 2
1285 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1293 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1286 assert_nil Issue.find_by_id(2)
1294 assert_nil Issue.find_by_id(2)
1287 end
1295 end
1288
1296
1289 def test_destroy_issues_with_time_entries
1297 def test_destroy_issues_with_time_entries
1290 @request.session[:user_id] = 2
1298 @request.session[:user_id] = 2
1291 post :destroy, :ids => [1, 3]
1299 post :destroy, :ids => [1, 3]
1292 assert_response :success
1300 assert_response :success
1293 assert_template 'destroy'
1301 assert_template 'destroy'
1294 assert_not_nil assigns(:hours)
1302 assert_not_nil assigns(:hours)
1295 assert Issue.find_by_id(1) && Issue.find_by_id(3)
1303 assert Issue.find_by_id(1) && Issue.find_by_id(3)
1296 end
1304 end
1297
1305
1298 def test_destroy_issues_and_destroy_time_entries
1306 def test_destroy_issues_and_destroy_time_entries
1299 @request.session[:user_id] = 2
1307 @request.session[:user_id] = 2
1300 post :destroy, :ids => [1, 3], :todo => 'destroy'
1308 post :destroy, :ids => [1, 3], :todo => 'destroy'
1301 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1309 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1302 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
1310 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
1303 assert_nil TimeEntry.find_by_id([1, 2])
1311 assert_nil TimeEntry.find_by_id([1, 2])
1304 end
1312 end
1305
1313
1306 def test_destroy_issues_and_assign_time_entries_to_project
1314 def test_destroy_issues_and_assign_time_entries_to_project
1307 @request.session[:user_id] = 2
1315 @request.session[:user_id] = 2
1308 post :destroy, :ids => [1, 3], :todo => 'nullify'
1316 post :destroy, :ids => [1, 3], :todo => 'nullify'
1309 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1317 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1310 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
1318 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
1311 assert_nil TimeEntry.find(1).issue_id
1319 assert_nil TimeEntry.find(1).issue_id
1312 assert_nil TimeEntry.find(2).issue_id
1320 assert_nil TimeEntry.find(2).issue_id
1313 end
1321 end
1314
1322
1315 def test_destroy_issues_and_reassign_time_entries_to_another_issue
1323 def test_destroy_issues_and_reassign_time_entries_to_another_issue
1316 @request.session[:user_id] = 2
1324 @request.session[:user_id] = 2
1317 post :destroy, :ids => [1, 3], :todo => 'reassign', :reassign_to_id => 2
1325 post :destroy, :ids => [1, 3], :todo => 'reassign', :reassign_to_id => 2
1318 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1326 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1319 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
1327 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
1320 assert_equal 2, TimeEntry.find(1).issue_id
1328 assert_equal 2, TimeEntry.find(1).issue_id
1321 assert_equal 2, TimeEntry.find(2).issue_id
1329 assert_equal 2, TimeEntry.find(2).issue_id
1322 end
1330 end
1323
1331
1324 def test_default_search_scope
1332 def test_default_search_scope
1325 get :index
1333 get :index
1326 assert_tag :div, :attributes => {:id => 'quick-search'},
1334 assert_tag :div, :attributes => {:id => 'quick-search'},
1327 :child => {:tag => 'form',
1335 :child => {:tag => 'form',
1328 :child => {:tag => 'input', :attributes => {:name => 'issues', :type => 'hidden', :value => '1'}}}
1336 :child => {:tag => 'input', :attributes => {:name => 'issues', :type => 'hidden', :value => '1'}}}
1329 end
1337 end
1330 end
1338 end
General Comments 0
You need to be logged in to leave comments. Login now