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