##// END OF EJS Templates
Backported r3683 from trunk....
Jean-Philippe Lang -
r3603:f6d2a4c29f15
parent child
Show More
@@ -1,548 +1,541
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 include QueriesHelper
44 44 helper :sort
45 45 include SortHelper
46 46 include IssuesHelper
47 47 helper :timelog
48 48 include Redmine::Export::PDF
49 49
50 50 verify :method => :post,
51 51 :only => :destroy,
52 52 :render => { :nothing => true, :status => :method_not_allowed }
53 53
54 54 def index
55 55 retrieve_query
56 56 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
57 57 sort_update({'id' => "#{Issue.table_name}.id"}.merge(@query.available_columns.inject({}) {|h, c| h[c.name.to_s] = c.sortable; h}))
58 58
59 59 if @query.valid?
60 60 limit = case params[:format]
61 61 when 'csv', 'pdf'
62 62 Setting.issues_export_limit.to_i
63 63 when 'atom'
64 64 Setting.feeds_limit.to_i
65 65 else
66 66 per_page_option
67 67 end
68 68
69 69 @issue_count = @query.issue_count
70 70 @issue_pages = Paginator.new self, @issue_count, limit, params['page']
71 71 @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version],
72 72 :order => sort_clause,
73 73 :offset => @issue_pages.current.offset,
74 74 :limit => limit)
75 75 @issue_count_by_group = @query.issue_count_by_group
76 76
77 77 respond_to do |format|
78 78 format.html { render :template => 'issues/index.rhtml', :layout => !request.xhr? }
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.visible.all
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.atom { render :action => 'changes', :layout => false, :content_type => 'application/atom+xml' }
119 119 format.pdf { send_data(issue_to_pdf(@issue), :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") }
120 120 end
121 121 end
122 122
123 123 # Add a new issue
124 124 # The new issue will be created from an existing one if copy_from parameter is given
125 125 def new
126 126 @issue = Issue.new
127 127 @issue.copy_from(params[:copy_from]) if params[:copy_from]
128 128 @issue.project = @project
129 129 # Tracker must be set before custom field values
130 130 @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first)
131 131 if @issue.tracker.nil?
132 132 render_error l(:error_no_tracker_in_project)
133 133 return
134 134 end
135 135 if params[:issue].is_a?(Hash)
136 136 @issue.attributes = params[:issue]
137 137 @issue.watcher_user_ids = params[:issue]['watcher_user_ids'] if User.current.allowed_to?(:add_issue_watchers, @project)
138 138 end
139 139 @issue.author = User.current
140 140
141 141 default_status = IssueStatus.default
142 142 unless default_status
143 143 render_error l(:error_no_default_issue_status)
144 144 return
145 145 end
146 146 @issue.status = default_status
147 147 @allowed_statuses = ([default_status] + default_status.find_new_statuses_allowed_to(User.current.roles_for_project(@project), @issue.tracker)).uniq
148 148
149 149 if request.get? || request.xhr?
150 150 @issue.start_date ||= Date.today
151 151 else
152 152 requested_status = IssueStatus.find_by_id(params[:issue][:status_id])
153 153 # Check that the user is allowed to apply the requested status
154 154 @issue.status = (@allowed_statuses.include? requested_status) ? requested_status : default_status
155 155 call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue })
156 156 if @issue.save
157 157 attach_files(@issue, params[:attachments])
158 158 flash[:notice] = l(:notice_successful_create)
159 159 call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
160 160 redirect_to(params[:continue] ? { :action => 'new', :tracker_id => @issue.tracker } :
161 161 { :action => 'show', :id => @issue })
162 162 return
163 163 end
164 164 end
165 165 @priorities = IssuePriority.all
166 166 render :layout => !request.xhr?
167 167 end
168 168
169 169 # Attributes that can be updated on workflow transition (without :edit permission)
170 170 # TODO: make it configurable (at least per role)
171 171 UPDATABLE_ATTRS_ON_TRANSITION = %w(status_id assigned_to_id fixed_version_id done_ratio) unless const_defined?(:UPDATABLE_ATTRS_ON_TRANSITION)
172 172
173 173 def edit
174 174 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
175 175 @priorities = IssuePriority.all
176 176 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
177 177 @time_entry = TimeEntry.new
178 178
179 179 @notes = params[:notes]
180 180 journal = @issue.init_journal(User.current, @notes)
181 181 # User can change issue attributes only if he has :edit permission or if a workflow transition is allowed
182 182 if (@edit_allowed || !@allowed_statuses.empty?) && params[:issue]
183 183 attrs = params[:issue].dup
184 184 attrs.delete_if {|k,v| !UPDATABLE_ATTRS_ON_TRANSITION.include?(k) } unless @edit_allowed
185 185 attrs.delete(:status_id) unless @allowed_statuses.detect {|s| s.id.to_s == attrs[:status_id].to_s}
186 186 @issue.attributes = attrs
187 187 end
188 188
189 189 if request.post?
190 190 @time_entry = TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => Date.today)
191 191 @time_entry.attributes = params[:time_entry]
192 192 if (@time_entry.hours.nil? || @time_entry.valid?) && @issue.valid?
193 193 attachments = attach_files(@issue, params[:attachments])
194 194 attachments.each {|a| journal.details << JournalDetail.new(:property => 'attachment', :prop_key => a.id, :value => a.filename)}
195 195 call_hook(:controller_issues_edit_before_save, { :params => params, :issue => @issue, :time_entry => @time_entry, :journal => journal})
196 196 if @issue.save
197 197 # Log spend time
198 198 if User.current.allowed_to?(:log_time, @project)
199 199 @time_entry.save
200 200 end
201 201 if !journal.new_record?
202 202 # Only send notification if something was actually changed
203 203 flash[:notice] = l(:notice_successful_update)
204 204 end
205 205 call_hook(:controller_issues_edit_after_save, { :params => params, :issue => @issue, :time_entry => @time_entry, :journal => journal})
206 206 redirect_back_or_default({:action => 'show', :id => @issue})
207 207 end
208 208 end
209 209 end
210 210 rescue ActiveRecord::StaleObjectError
211 211 # Optimistic locking exception
212 212 flash.now[:error] = l(:notice_locking_conflict)
213 213 # Remove the previously added attachments if issue was not updated
214 214 attachments.each(&:destroy)
215 215 end
216 216
217 217 def reply
218 218 journal = Journal.find(params[:journal_id]) if params[:journal_id]
219 219 if journal
220 220 user = journal.user
221 221 text = journal.notes
222 222 else
223 223 user = @issue.author
224 224 text = @issue.description
225 225 end
226 226 # Replaces pre blocks with [...]
227 227 text = text.to_s.strip.gsub(%r{<pre>((.|\s)*?)</pre>}m, '[...]')
228 228 content = "#{ll(Setting.default_language, :text_user_wrote, user)}\n> "
229 229 content << text.gsub(/(\r?\n|\r\n?)/, "\n> ") + "\n\n"
230 230
231 231 render(:update) { |page|
232 232 page.<< "$('notes').value = \"#{escape_javascript content}\";"
233 233 page.show 'update'
234 234 page << "Form.Element.focus('notes');"
235 235 page << "Element.scrollTo('update');"
236 236 page << "$('notes').scrollTop = $('notes').scrollHeight - $('notes').clientHeight;"
237 237 }
238 238 end
239 239
240 240 # Bulk edit a set of issues
241 241 def bulk_edit
242 242 if request.post?
243 243 tracker = params[:tracker_id].blank? ? nil : @project.trackers.find_by_id(params[:tracker_id])
244 244 status = params[:status_id].blank? ? nil : IssueStatus.find_by_id(params[:status_id])
245 245 priority = params[:priority_id].blank? ? nil : IssuePriority.find_by_id(params[:priority_id])
246 246 assigned_to = (params[:assigned_to_id].blank? || params[:assigned_to_id] == 'none') ? nil : User.find_by_id(params[:assigned_to_id])
247 247 category = (params[:category_id].blank? || params[:category_id] == 'none') ? nil : @project.issue_categories.find_by_id(params[:category_id])
248 248 fixed_version = (params[:fixed_version_id].blank? || params[:fixed_version_id] == 'none') ? nil : @project.shared_versions.find_by_id(params[:fixed_version_id])
249 249 custom_field_values = params[:custom_field_values] ? params[:custom_field_values].reject {|k,v| v.blank?} : nil
250 250
251 251 unsaved_issue_ids = []
252 252 @issues.each do |issue|
253 253 journal = issue.init_journal(User.current, params[:notes])
254 254 issue.tracker = tracker if tracker
255 255 issue.priority = priority if priority
256 256 issue.assigned_to = assigned_to if assigned_to || params[:assigned_to_id] == 'none'
257 257 issue.category = category if category || params[:category_id] == 'none'
258 258 issue.fixed_version = fixed_version if fixed_version || params[:fixed_version_id] == 'none'
259 259 issue.start_date = params[:start_date] unless params[:start_date].blank?
260 260 issue.due_date = params[:due_date] unless params[:due_date].blank?
261 261 issue.done_ratio = params[:done_ratio] unless params[:done_ratio].blank?
262 262 issue.custom_field_values = custom_field_values if custom_field_values && !custom_field_values.empty?
263 263 call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue })
264 264 # Don't save any change to the issue if the user is not authorized to apply the requested status
265 265 unless (status.nil? || (issue.new_statuses_allowed_to(User.current).include?(status) && issue.status = status)) && issue.save
266 266 # Keep unsaved issue ids to display them in flash error
267 267 unsaved_issue_ids << issue.id
268 268 end
269 269 end
270 270 if unsaved_issue_ids.empty?
271 271 flash[:notice] = l(:notice_successful_update) unless @issues.empty?
272 272 else
273 273 flash[:error] = l(:notice_failed_to_save_issues, :count => unsaved_issue_ids.size,
274 274 :total => @issues.size,
275 275 :ids => '#' + unsaved_issue_ids.join(', #'))
276 276 end
277 277 redirect_back_or_default({:controller => 'issues', :action => 'index', :project_id => @project})
278 278 return
279 279 end
280 280 @available_statuses = Workflow.available_statuses(@project)
281 281 @custom_fields = @project.all_issue_custom_fields
282 282 end
283 283
284 284 def move
285 285 @copy = params[:copy_options] && params[:copy_options][:copy]
286 @allowed_projects = []
287 # find projects to which the user is allowed to move the issue
288 if User.current.admin?
289 # admin is allowed to move issues to any active (visible) project
290 @allowed_projects = Project.find(:all, :conditions => Project.visible_by(User.current))
291 else
292 User.current.memberships.each {|m| @allowed_projects << m.project if m.roles.detect {|r| r.allowed_to?(:move_issues)}}
293 end
286 @allowed_projects = Issue.allowed_target_projects_on_move
294 287 @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:new_project_id]} if params[:new_project_id]
295 288 @target_project ||= @project
296 289 @trackers = @target_project.trackers
297 290 @available_statuses = Workflow.available_statuses(@project)
298 291 if request.post?
299 292 new_tracker = params[:new_tracker_id].blank? ? nil : @target_project.trackers.find_by_id(params[:new_tracker_id])
300 293 unsaved_issue_ids = []
301 294 moved_issues = []
302 295 @issues.each do |issue|
303 296 changed_attributes = {}
304 297 [:assigned_to_id, :status_id, :start_date, :due_date].each do |valid_attribute|
305 298 unless params[valid_attribute].blank?
306 299 changed_attributes[valid_attribute] = (params[valid_attribute] == 'none' ? nil : params[valid_attribute])
307 300 end
308 301 end
309 302 issue.init_journal(User.current)
310 303 if r = issue.move_to(@target_project, new_tracker, {:copy => @copy, :attributes => changed_attributes})
311 304 moved_issues << r
312 305 else
313 306 unsaved_issue_ids << issue.id
314 307 end
315 308 end
316 309 if unsaved_issue_ids.empty?
317 310 flash[:notice] = l(:notice_successful_update) unless @issues.empty?
318 311 else
319 312 flash[:error] = l(:notice_failed_to_save_issues, :count => unsaved_issue_ids.size,
320 313 :total => @issues.size,
321 314 :ids => '#' + unsaved_issue_ids.join(', #'))
322 315 end
323 316 if params[:follow]
324 317 if @issues.size == 1 && moved_issues.size == 1
325 318 redirect_to :controller => 'issues', :action => 'show', :id => moved_issues.first
326 319 else
327 320 redirect_to :controller => 'issues', :action => 'index', :project_id => (@target_project || @project)
328 321 end
329 322 else
330 323 redirect_to :controller => 'issues', :action => 'index', :project_id => @project
331 324 end
332 325 return
333 326 end
334 327 render :layout => false if request.xhr?
335 328 end
336 329
337 330 def destroy
338 331 @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f
339 332 if @hours > 0
340 333 case params[:todo]
341 334 when 'destroy'
342 335 # nothing to do
343 336 when 'nullify'
344 337 TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues])
345 338 when 'reassign'
346 339 reassign_to = @project.issues.find_by_id(params[:reassign_to_id])
347 340 if reassign_to.nil?
348 341 flash.now[:error] = l(:error_issue_not_found_in_project)
349 342 return
350 343 else
351 344 TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues])
352 345 end
353 346 else
354 347 # display the destroy form
355 348 return
356 349 end
357 350 end
358 351 @issues.each(&:destroy)
359 352 redirect_to :action => 'index', :project_id => @project
360 353 end
361 354
362 355 def gantt
363 356 @gantt = Redmine::Helpers::Gantt.new(params)
364 357 retrieve_query
365 358 @query.group_by = nil
366 359 if @query.valid?
367 360 events = []
368 361 # Issues that have start and due dates
369 362 events += @query.issues(:include => [:tracker, :assigned_to, :priority],
370 363 :order => "start_date, due_date",
371 364 :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]
372 365 )
373 366 # Issues that don't have a due date but that are assigned to a version with a date
374 367 events += @query.issues(:include => [:tracker, :assigned_to, :priority, :fixed_version],
375 368 :order => "start_date, effective_date",
376 369 :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]
377 370 )
378 371 # Versions
379 372 events += @query.versions(:conditions => ["effective_date BETWEEN ? AND ?", @gantt.date_from, @gantt.date_to])
380 373
381 374 @gantt.events = events
382 375 end
383 376
384 377 basename = (@project ? "#{@project.identifier}-" : '') + 'gantt'
385 378
386 379 respond_to do |format|
387 380 format.html { render :template => "issues/gantt.rhtml", :layout => !request.xhr? }
388 381 format.png { send_data(@gantt.to_image, :disposition => 'inline', :type => 'image/png', :filename => "#{basename}.png") } if @gantt.respond_to?('to_image')
389 382 format.pdf { send_data(gantt_to_pdf(@gantt, @project), :type => 'application/pdf', :filename => "#{basename}.pdf") }
390 383 end
391 384 end
392 385
393 386 def calendar
394 387 if params[:year] and params[:year].to_i > 1900
395 388 @year = params[:year].to_i
396 389 if params[:month] and params[:month].to_i > 0 and params[:month].to_i < 13
397 390 @month = params[:month].to_i
398 391 end
399 392 end
400 393 @year ||= Date.today.year
401 394 @month ||= Date.today.month
402 395
403 396 @calendar = Redmine::Helpers::Calendar.new(Date.civil(@year, @month, 1), current_language, :month)
404 397 retrieve_query
405 398 @query.group_by = nil
406 399 if @query.valid?
407 400 events = []
408 401 events += @query.issues(:include => [:tracker, :assigned_to, :priority],
409 402 :conditions => ["((start_date BETWEEN ? AND ?) OR (due_date BETWEEN ? AND ?))", @calendar.startdt, @calendar.enddt, @calendar.startdt, @calendar.enddt]
410 403 )
411 404 events += @query.versions(:conditions => ["effective_date BETWEEN ? AND ?", @calendar.startdt, @calendar.enddt])
412 405
413 406 @calendar.events = events
414 407 end
415 408
416 409 render :layout => false if request.xhr?
417 410 end
418 411
419 412 def context_menu
420 413 @issues = Issue.find_all_by_id(params[:ids], :include => :project)
421 414 if (@issues.size == 1)
422 415 @issue = @issues.first
423 416 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
424 417 end
425 418 projects = @issues.collect(&:project).compact.uniq
426 419 @project = projects.first if projects.size == 1
427 420
428 421 @can = {:edit => (@project && User.current.allowed_to?(:edit_issues, @project)),
429 422 :log_time => (@project && User.current.allowed_to?(:log_time, @project)),
430 423 :update => (@project && (User.current.allowed_to?(:edit_issues, @project) || (User.current.allowed_to?(:change_status, @project) && @allowed_statuses && !@allowed_statuses.empty?))),
431 424 :move => (@project && User.current.allowed_to?(:move_issues, @project)),
432 425 :copy => (@issue && @project.trackers.include?(@issue.tracker) && User.current.allowed_to?(:add_issues, @project)),
433 426 :delete => (@project && User.current.allowed_to?(:delete_issues, @project))
434 427 }
435 428 if @project
436 429 @assignables = @project.assignable_users
437 430 @assignables << @issue.assigned_to if @issue && @issue.assigned_to && !@assignables.include?(@issue.assigned_to)
438 431 @trackers = @project.trackers
439 432 end
440 433
441 434 @priorities = IssuePriority.all.reverse
442 435 @statuses = IssueStatus.find(:all, :order => 'position')
443 436 @back = params[:back_url] || request.env['HTTP_REFERER']
444 437
445 438 render :layout => false
446 439 end
447 440
448 441 def update_form
449 442 if params[:id].blank?
450 443 @issue = Issue.new
451 444 @issue.project = @project
452 445 else
453 446 @issue = @project.issues.visible.find(params[:id])
454 447 end
455 448 @issue.attributes = params[:issue]
456 449 @allowed_statuses = ([@issue.status] + @issue.status.find_new_statuses_allowed_to(User.current.roles_for_project(@project), @issue.tracker)).uniq
457 450 @priorities = IssuePriority.all
458 451
459 452 render :partial => 'attributes'
460 453 end
461 454
462 455 def preview
463 456 @issue = @project.issues.find_by_id(params[:id]) unless params[:id].blank?
464 457 @attachements = @issue.attachments if @issue
465 458 @text = params[:notes] || (params[:issue] ? params[:issue][:description] : nil)
466 459 render :partial => 'common/preview'
467 460 end
468 461
469 462 private
470 463 def find_issue
471 464 @issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category])
472 465 @project = @issue.project
473 466 rescue ActiveRecord::RecordNotFound
474 467 render_404
475 468 end
476 469
477 470 # Filter for bulk operations
478 471 def find_issues
479 472 @issues = Issue.find_all_by_id(params[:id] || params[:ids])
480 473 raise ActiveRecord::RecordNotFound if @issues.empty?
481 474 projects = @issues.collect(&:project).compact.uniq
482 475 if projects.size == 1
483 476 @project = projects.first
484 477 else
485 478 # TODO: let users bulk edit/move/destroy issues from different projects
486 479 render_error 'Can not bulk edit/move/destroy issues from different projects'
487 480 return false
488 481 end
489 482 rescue ActiveRecord::RecordNotFound
490 483 render_404
491 484 end
492 485
493 486 def find_project
494 487 @project = Project.find(params[:project_id])
495 488 rescue ActiveRecord::RecordNotFound
496 489 render_404
497 490 end
498 491
499 492 def find_optional_project
500 493 @project = Project.find(params[:project_id]) unless params[:project_id].blank?
501 494 allowed = User.current.allowed_to?({:controller => params[:controller], :action => params[:action]}, @project, :global => true)
502 495 allowed ? true : deny_access
503 496 rescue ActiveRecord::RecordNotFound
504 497 render_404
505 498 end
506 499
507 500 # Retrieve query from session or build a new query
508 501 def retrieve_query
509 502 if !params[:query_id].blank?
510 503 cond = "project_id IS NULL"
511 504 cond << " OR project_id = #{@project.id}" if @project
512 505 @query = Query.find(params[:query_id], :conditions => cond)
513 506 @query.project = @project
514 507 session[:query] = {:id => @query.id, :project_id => @query.project_id}
515 508 sort_clear
516 509 else
517 510 if params[:set_filter] || session[:query].nil? || session[:query][:project_id] != (@project ? @project.id : nil)
518 511 # Give it a name, required to be valid
519 512 @query = Query.new(:name => "_")
520 513 @query.project = @project
521 514 if params[:fields] and params[:fields].is_a? Array
522 515 params[:fields].each do |field|
523 516 @query.add_filter(field, params[:operators][field], params[:values][field])
524 517 end
525 518 else
526 519 @query.available_filters.keys.each do |field|
527 520 @query.add_short_filter(field, params[field]) if params[field]
528 521 end
529 522 end
530 523 @query.group_by = params[:group_by]
531 524 @query.column_names = params[:query] && params[:query][:column_names]
532 525 session[:query] = {:project_id => @query.project_id, :filters => @query.filters, :group_by => @query.group_by, :column_names => @query.column_names}
533 526 else
534 527 @query = Query.find_by_id(session[:query][:id]) if session[:query][:id]
535 528 @query ||= Query.new(:name => "_", :project => @project, :filters => session[:query][:filters], :group_by => session[:query][:group_by], :column_names => session[:query][:column_names])
536 529 @query.project = @project
537 530 end
538 531 end
539 532 end
540 533
541 534 # Rescues an invalid query statement. Just in case...
542 535 def query_statement_invalid(exception)
543 536 logger.error "Query::StatementInvalid: #{exception.message}" if logger
544 537 session.delete(:query)
545 538 sort_clear
546 539 render_error "An error occurred while executing the query and has been logged. Please report this error to your Redmine administrator."
547 540 end
548 541 end
@@ -1,446 +1,462
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 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 Issue < ActiveRecord::Base
19 19 belongs_to :project
20 20 belongs_to :tracker
21 21 belongs_to :status, :class_name => 'IssueStatus', :foreign_key => 'status_id'
22 22 belongs_to :author, :class_name => 'User', :foreign_key => 'author_id'
23 23 belongs_to :assigned_to, :class_name => 'User', :foreign_key => 'assigned_to_id'
24 24 belongs_to :fixed_version, :class_name => 'Version', :foreign_key => 'fixed_version_id'
25 25 belongs_to :priority, :class_name => 'IssuePriority', :foreign_key => 'priority_id'
26 26 belongs_to :category, :class_name => 'IssueCategory', :foreign_key => 'category_id'
27 27
28 28 has_many :journals, :as => :journalized, :dependent => :destroy
29 29 has_many :time_entries, :dependent => :delete_all
30 30 has_and_belongs_to_many :changesets, :order => "#{Changeset.table_name}.committed_on ASC, #{Changeset.table_name}.id ASC"
31 31
32 32 has_many :relations_from, :class_name => 'IssueRelation', :foreign_key => 'issue_from_id', :dependent => :delete_all
33 33 has_many :relations_to, :class_name => 'IssueRelation', :foreign_key => 'issue_to_id', :dependent => :delete_all
34 34
35 35 acts_as_attachable :after_remove => :attachment_removed
36 36 acts_as_customizable
37 37 acts_as_watchable
38 38 acts_as_searchable :columns => ['subject', "#{table_name}.description", "#{Journal.table_name}.notes"],
39 39 :include => [:project, :journals],
40 40 # sort by id so that limited eager loading doesn't break with postgresql
41 41 :order_column => "#{table_name}.id"
42 42 acts_as_event :title => Proc.new {|o| "#{o.tracker.name} ##{o.id} (#{o.status}): #{o.subject}"},
43 43 :url => Proc.new {|o| {:controller => 'issues', :action => 'show', :id => o.id}},
44 44 :type => Proc.new {|o| 'issue' + (o.closed? ? ' closed' : '') }
45 45
46 46 acts_as_activity_provider :find_options => {:include => [:project, :author, :tracker]},
47 47 :author_key => :author_id
48 48
49 49 DONE_RATIO_OPTIONS = %w(issue_field issue_status)
50 50
51 51 validates_presence_of :subject, :priority, :project, :tracker, :author, :status
52 52 validates_length_of :subject, :maximum => 255
53 53 validates_inclusion_of :done_ratio, :in => 0..100
54 54 validates_numericality_of :estimated_hours, :allow_nil => true
55 55
56 56 named_scope :visible, lambda {|*args| { :include => :project,
57 57 :conditions => Project.allowed_to_condition(args.first || User.current, :view_issues) } }
58 58
59 59 named_scope :open, :conditions => ["#{IssueStatus.table_name}.is_closed = ?", false], :include => :status
60 60
61 61 before_save :update_done_ratio_from_issue_status
62 62 after_save :create_journal
63 63
64 64 # Returns true if usr or current user is allowed to view the issue
65 65 def visible?(usr=nil)
66 66 (usr || User.current).allowed_to?(:view_issues, self.project)
67 67 end
68 68
69 69 def after_initialize
70 70 if new_record?
71 71 # set default values for new records only
72 72 self.status ||= IssueStatus.default
73 73 self.priority ||= IssuePriority.default
74 74 end
75 75 end
76 76
77 77 # Overrides Redmine::Acts::Customizable::InstanceMethods#available_custom_fields
78 78 def available_custom_fields
79 79 (project && tracker) ? project.all_issue_custom_fields.select {|c| tracker.custom_fields.include? c } : []
80 80 end
81 81
82 82 def copy_from(arg)
83 83 issue = arg.is_a?(Issue) ? arg : Issue.visible.find(arg)
84 84 self.attributes = issue.attributes.dup.except("id", "created_on", "updated_on")
85 85 self.custom_values = issue.custom_values.collect {|v| v.clone}
86 86 self.status = issue.status
87 87 self
88 88 end
89 89
90 90 # Moves/copies an issue to a new project and tracker
91 91 # Returns the moved/copied issue on success, false on failure
92 92 def move_to(new_project, new_tracker = nil, options = {})
93 93 options ||= {}
94 94 issue = options[:copy] ? self.clone : self
95 95 ret = Issue.transaction do
96 96 if new_project && issue.project_id != new_project.id
97 97 # delete issue relations
98 98 unless Setting.cross_project_issue_relations?
99 99 issue.relations_from.clear
100 100 issue.relations_to.clear
101 101 end
102 102 # issue is moved to another project
103 103 # reassign to the category with same name if any
104 104 new_category = issue.category.nil? ? nil : new_project.issue_categories.find_by_name(issue.category.name)
105 105 issue.category = new_category
106 106 # Keep the fixed_version if it's still valid in the new_project
107 107 unless new_project.shared_versions.include?(issue.fixed_version)
108 108 issue.fixed_version = nil
109 109 end
110 110 issue.project = new_project
111 111 end
112 112 if new_tracker
113 113 issue.tracker = new_tracker
114 114 end
115 115 if options[:copy]
116 116 issue.custom_field_values = self.custom_field_values.inject({}) {|h,v| h[v.custom_field_id] = v.value; h}
117 117 issue.status = if options[:attributes] && options[:attributes][:status_id]
118 118 IssueStatus.find_by_id(options[:attributes][:status_id])
119 119 else
120 120 self.status
121 121 end
122 122 end
123 123 # Allow bulk setting of attributes on the issue
124 124 if options[:attributes]
125 125 issue.attributes = options[:attributes]
126 126 end
127 127 if issue.save
128 128 unless options[:copy]
129 129 # Manually update project_id on related time entries
130 130 TimeEntry.update_all("project_id = #{new_project.id}", {:issue_id => id})
131 131 end
132 132 true
133 133 else
134 134 raise ActiveRecord::Rollback
135 135 end
136 136 end
137 137 ret ? issue : false
138 138 end
139 139
140 140 def priority_id=(pid)
141 141 self.priority = nil
142 142 write_attribute(:priority_id, pid)
143 143 end
144 144
145 145 def tracker_id=(tid)
146 146 self.tracker = nil
147 147 write_attribute(:tracker_id, tid)
148 148 result = write_attribute(:tracker_id, tid)
149 149 @custom_field_values = nil
150 150 result
151 151 end
152 152
153 153 # Overrides attributes= so that tracker_id gets assigned first
154 154 def attributes_with_tracker_first=(new_attributes, *args)
155 155 return if new_attributes.nil?
156 156 new_tracker_id = new_attributes['tracker_id'] || new_attributes[:tracker_id]
157 157 if new_tracker_id
158 158 self.tracker_id = new_tracker_id
159 159 end
160 160 send :attributes_without_tracker_first=, new_attributes, *args
161 161 end
162 162 # Do not redefine alias chain on reload (see #4838)
163 163 alias_method_chain(:attributes=, :tracker_first) unless method_defined?(:attributes_without_tracker_first=)
164 164
165 165 def estimated_hours=(h)
166 166 write_attribute :estimated_hours, (h.is_a?(String) ? h.to_hours : h)
167 167 end
168 168
169 169 def done_ratio
170 170 if Issue.use_status_for_done_ratio? && status && status.default_done_ratio?
171 171 status.default_done_ratio
172 172 else
173 173 read_attribute(:done_ratio)
174 174 end
175 175 end
176 176
177 177 def self.use_status_for_done_ratio?
178 178 Setting.issue_done_ratio == 'issue_status'
179 179 end
180 180
181 181 def self.use_field_for_done_ratio?
182 182 Setting.issue_done_ratio == 'issue_field'
183 183 end
184 184
185 185 def validate
186 186 if self.due_date.nil? && @attributes['due_date'] && !@attributes['due_date'].empty?
187 187 errors.add :due_date, :not_a_date
188 188 end
189 189
190 190 if self.due_date and self.start_date and self.due_date < self.start_date
191 191 errors.add :due_date, :greater_than_start_date
192 192 end
193 193
194 194 if start_date && soonest_start && start_date < soonest_start
195 195 errors.add :start_date, :invalid
196 196 end
197 197
198 198 if fixed_version
199 199 if !assignable_versions.include?(fixed_version)
200 200 errors.add :fixed_version_id, :inclusion
201 201 elsif reopened? && fixed_version.closed?
202 202 errors.add_to_base I18n.t(:error_can_not_reopen_issue_on_closed_version)
203 203 end
204 204 end
205 205
206 206 # Checks that the issue can not be added/moved to a disabled tracker
207 207 if project && (tracker_id_changed? || project_id_changed?)
208 208 unless project.trackers.include?(tracker)
209 209 errors.add :tracker_id, :inclusion
210 210 end
211 211 end
212 212 end
213 213
214 214 def before_create
215 215 # default assignment based on category
216 216 if assigned_to.nil? && category && category.assigned_to
217 217 self.assigned_to = category.assigned_to
218 218 end
219 219 end
220 220
221 221 # Set the done_ratio using the status if that setting is set. This will keep the done_ratios
222 222 # even if the user turns off the setting later
223 223 def update_done_ratio_from_issue_status
224 224 if Issue.use_status_for_done_ratio? && status && status.default_done_ratio?
225 225 self.done_ratio = status.default_done_ratio
226 226 end
227 227 end
228 228
229 229 def after_save
230 230 # Reload is needed in order to get the right status
231 231 reload
232 232
233 233 # Update start/due dates of following issues
234 234 relations_from.each(&:set_issue_to_dates)
235 235
236 236 # Close duplicates if the issue was closed
237 237 if @issue_before_change && !@issue_before_change.closed? && self.closed?
238 238 duplicates.each do |duplicate|
239 239 # Reload is need in case the duplicate was updated by a previous duplicate
240 240 duplicate.reload
241 241 # Don't re-close it if it's already closed
242 242 next if duplicate.closed?
243 243 # Same user and notes
244 244 duplicate.init_journal(@current_journal.user, @current_journal.notes)
245 245 duplicate.update_attribute :status, self.status
246 246 end
247 247 end
248 248 end
249 249
250 250 def init_journal(user, notes = "")
251 251 @current_journal ||= Journal.new(:journalized => self, :user => user, :notes => notes)
252 252 @issue_before_change = self.clone
253 253 @issue_before_change.status = self.status
254 254 @custom_values_before_change = {}
255 255 self.custom_values.each {|c| @custom_values_before_change.store c.custom_field_id, c.value }
256 256 # Make sure updated_on is updated when adding a note.
257 257 updated_on_will_change!
258 258 @current_journal
259 259 end
260 260
261 261 # Return true if the issue is closed, otherwise false
262 262 def closed?
263 263 self.status.is_closed?
264 264 end
265 265
266 266 # Return true if the issue is being reopened
267 267 def reopened?
268 268 if !new_record? && status_id_changed?
269 269 status_was = IssueStatus.find_by_id(status_id_was)
270 270 status_new = IssueStatus.find_by_id(status_id)
271 271 if status_was && status_new && status_was.is_closed? && !status_new.is_closed?
272 272 return true
273 273 end
274 274 end
275 275 false
276 276 end
277 277
278 278 # Returns true if the issue is overdue
279 279 def overdue?
280 280 !due_date.nil? && (due_date < Date.today) && !status.is_closed?
281 281 end
282 282
283 283 # Users the issue can be assigned to
284 284 def assignable_users
285 285 project.assignable_users
286 286 end
287 287
288 288 # Versions that the issue can be assigned to
289 289 def assignable_versions
290 290 @assignable_versions ||= (project.shared_versions.open + [Version.find_by_id(fixed_version_id_was)]).compact.uniq.sort
291 291 end
292 292
293 293 # Returns true if this issue is blocked by another issue that is still open
294 294 def blocked?
295 295 !relations_to.detect {|ir| ir.relation_type == 'blocks' && !ir.issue_from.closed?}.nil?
296 296 end
297 297
298 298 # Returns an array of status that user is able to apply
299 299 def new_statuses_allowed_to(user)
300 300 statuses = status.find_new_statuses_allowed_to(user.roles_for_project(project), tracker)
301 301 statuses << status unless statuses.empty?
302 302 statuses = statuses.uniq.sort
303 303 blocked? ? statuses.reject {|s| s.is_closed?} : statuses
304 304 end
305 305
306 306 # Returns the mail adresses of users that should be notified
307 307 def recipients
308 308 notified = project.notified_users
309 309 # Author and assignee are always notified unless they have been locked
310 310 notified << author if author && author.active?
311 311 notified << assigned_to if assigned_to && assigned_to.active?
312 312 notified.uniq!
313 313 # Remove users that can not view the issue
314 314 notified.reject! {|user| !visible?(user)}
315 315 notified.collect(&:mail)
316 316 end
317 317
318 318 # Returns the total number of hours spent on this issue.
319 319 #
320 320 # Example:
321 321 # spent_hours => 0
322 322 # spent_hours => 50
323 323 def spent_hours
324 324 @spent_hours ||= time_entries.sum(:hours) || 0
325 325 end
326 326
327 327 def relations
328 328 (relations_from + relations_to).sort
329 329 end
330 330
331 331 def all_dependent_issues
332 332 dependencies = []
333 333 relations_from.each do |relation|
334 334 dependencies << relation.issue_to
335 335 dependencies += relation.issue_to.all_dependent_issues
336 336 end
337 337 dependencies
338 338 end
339 339
340 340 # Returns an array of issues that duplicate this one
341 341 def duplicates
342 342 relations_to.select {|r| r.relation_type == IssueRelation::TYPE_DUPLICATES}.collect {|r| r.issue_from}
343 343 end
344 344
345 345 # Returns the due date or the target due date if any
346 346 # Used on gantt chart
347 347 def due_before
348 348 due_date || (fixed_version ? fixed_version.effective_date : nil)
349 349 end
350 350
351 351 # Returns the time scheduled for this issue.
352 352 #
353 353 # Example:
354 354 # Start Date: 2/26/09, End Date: 3/04/09
355 355 # duration => 6
356 356 def duration
357 357 (start_date && due_date) ? due_date - start_date : 0
358 358 end
359 359
360 360 def soonest_start
361 361 @soonest_start ||= relations_to.collect{|relation| relation.successor_soonest_start}.compact.min
362 362 end
363 363
364 364 def to_s
365 365 "#{tracker} ##{id}: #{subject}"
366 366 end
367 367
368 368 # Returns a string of css classes that apply to the issue
369 369 def css_classes
370 370 s = "issue status-#{status.position} priority-#{priority.position}"
371 371 s << ' closed' if closed?
372 372 s << ' overdue' if overdue?
373 373 s << ' created-by-me' if User.current.logged? && author_id == User.current.id
374 374 s << ' assigned-to-me' if User.current.logged? && assigned_to_id == User.current.id
375 375 s
376 376 end
377 377
378 378 # Unassigns issues from +version+ if it's no longer shared with issue's project
379 379 def self.update_versions_from_sharing_change(version)
380 380 # Update issues assigned to the version
381 381 update_versions(["#{Issue.table_name}.fixed_version_id = ?", version.id])
382 382 end
383 383
384 384 # Unassigns issues from versions that are no longer shared
385 385 # after +project+ was moved
386 386 def self.update_versions_from_hierarchy_change(project)
387 387 moved_project_ids = project.self_and_descendants.reload.collect(&:id)
388 388 # Update issues of the moved projects and issues assigned to a version of a moved project
389 389 Issue.update_versions(["#{Version.table_name}.project_id IN (?) OR #{Issue.table_name}.project_id IN (?)", moved_project_ids, moved_project_ids])
390 390 end
391 391
392 # Returns an array of projects that current user can move issues to
393 def self.allowed_target_projects_on_move
394 projects = []
395 if User.current.admin?
396 # admin is allowed to move issues to any active (visible) project
397 projects = Project.visible.all
398 elsif User.current.logged?
399 if Role.non_member.allowed_to?(:move_issues)
400 projects = Project.visible.all
401 else
402 User.current.memberships.each {|m| projects << m.project if m.roles.detect {|r| r.allowed_to?(:move_issues)}}
403 end
404 end
405 projects
406 end
407
392 408 private
393 409
394 410 # Update issues so their versions are not pointing to a
395 411 # fixed_version that is not shared with the issue's project
396 412 def self.update_versions(conditions=nil)
397 413 # Only need to update issues with a fixed_version from
398 414 # a different project and that is not systemwide shared
399 415 Issue.all(:conditions => merge_conditions("#{Issue.table_name}.fixed_version_id IS NOT NULL" +
400 416 " AND #{Issue.table_name}.project_id <> #{Version.table_name}.project_id" +
401 417 " AND #{Version.table_name}.sharing <> 'system'",
402 418 conditions),
403 419 :include => [:project, :fixed_version]
404 420 ).each do |issue|
405 421 next if issue.project.nil? || issue.fixed_version.nil?
406 422 unless issue.project.shared_versions.include?(issue.fixed_version)
407 423 issue.init_journal(User.current)
408 424 issue.fixed_version = nil
409 425 issue.save
410 426 end
411 427 end
412 428 end
413 429
414 430 # Callback on attachment deletion
415 431 def attachment_removed(obj)
416 432 journal = init_journal(User.current)
417 433 journal.details << JournalDetail.new(:property => 'attachment',
418 434 :prop_key => obj.id,
419 435 :old_value => obj.filename)
420 436 journal.save
421 437 end
422 438
423 439 # Saves the changes in a Journal
424 440 # Called after_save
425 441 def create_journal
426 442 if @current_journal
427 443 # attributes changes
428 444 (Issue.column_names - %w(id description lock_version created_on updated_on)).each {|c|
429 445 @current_journal.details << JournalDetail.new(:property => 'attr',
430 446 :prop_key => c,
431 447 :old_value => @issue_before_change.send(c),
432 448 :value => send(c)) unless send(c)==@issue_before_change.send(c)
433 449 }
434 450 # custom fields changes
435 451 custom_values.each {|c|
436 452 next if (@custom_values_before_change[c.custom_field_id]==c.value ||
437 453 (@custom_values_before_change[c.custom_field_id].blank? && c.value.blank?))
438 454 @current_journal.details << JournalDetail.new(:property => 'cf',
439 455 :prop_key => c.custom_field_id,
440 456 :old_value => @custom_values_before_change[c.custom_field_id],
441 457 :value => c.value)
442 458 }
443 459 @current_journal.save
444 460 end
445 461 end
446 462 end
@@ -1,602 +1,618
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 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
20 20 class IssueTest < ActiveSupport::TestCase
21 21 fixtures :projects, :users, :members, :member_roles, :roles,
22 22 :trackers, :projects_trackers,
23 23 :versions,
24 24 :issue_statuses, :issue_categories, :issue_relations, :workflows,
25 25 :enumerations,
26 26 :issues,
27 27 :custom_fields, :custom_fields_projects, :custom_fields_trackers, :custom_values,
28 28 :time_entries
29 29
30 30 def test_create
31 31 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3, :status_id => 1, :priority => IssuePriority.all.first, :subject => 'test_create', :description => 'IssueTest#test_create', :estimated_hours => '1:30')
32 32 assert issue.save
33 33 issue.reload
34 34 assert_equal 1.5, issue.estimated_hours
35 35 end
36 36
37 37 def test_create_minimal
38 38 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3, :status_id => 1, :priority => IssuePriority.all.first, :subject => 'test_create')
39 39 assert issue.save
40 40 assert issue.description.nil?
41 41 end
42 42
43 43 def test_create_with_required_custom_field
44 44 field = IssueCustomField.find_by_name('Database')
45 45 field.update_attribute(:is_required, true)
46 46
47 47 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :subject => 'test_create', :description => 'IssueTest#test_create_with_required_custom_field')
48 48 assert issue.available_custom_fields.include?(field)
49 49 # No value for the custom field
50 50 assert !issue.save
51 51 assert_equal I18n.translate('activerecord.errors.messages.invalid'), issue.errors.on(:custom_values)
52 52 # Blank value
53 53 issue.custom_field_values = { field.id => '' }
54 54 assert !issue.save
55 55 assert_equal I18n.translate('activerecord.errors.messages.invalid'), issue.errors.on(:custom_values)
56 56 # Invalid value
57 57 issue.custom_field_values = { field.id => 'SQLServer' }
58 58 assert !issue.save
59 59 assert_equal I18n.translate('activerecord.errors.messages.invalid'), issue.errors.on(:custom_values)
60 60 # Valid value
61 61 issue.custom_field_values = { field.id => 'PostgreSQL' }
62 62 assert issue.save
63 63 issue.reload
64 64 assert_equal 'PostgreSQL', issue.custom_value_for(field).value
65 65 end
66 66
67 67 def test_visible_scope_for_anonymous
68 68 # Anonymous user should see issues of public projects only
69 69 issues = Issue.visible(User.anonymous).all
70 70 assert issues.any?
71 71 assert_nil issues.detect {|issue| !issue.project.is_public?}
72 72 # Anonymous user should not see issues without permission
73 73 Role.anonymous.remove_permission!(:view_issues)
74 74 issues = Issue.visible(User.anonymous).all
75 75 assert issues.empty?
76 76 end
77 77
78 78 def test_visible_scope_for_user
79 79 user = User.find(9)
80 80 assert user.projects.empty?
81 81 # Non member user should see issues of public projects only
82 82 issues = Issue.visible(user).all
83 83 assert issues.any?
84 84 assert_nil issues.detect {|issue| !issue.project.is_public?}
85 85 # Non member user should not see issues without permission
86 86 Role.non_member.remove_permission!(:view_issues)
87 87 user.reload
88 88 issues = Issue.visible(user).all
89 89 assert issues.empty?
90 90 # User should see issues of projects for which he has view_issues permissions only
91 91 Member.create!(:principal => user, :project_id => 2, :role_ids => [1])
92 92 user.reload
93 93 issues = Issue.visible(user).all
94 94 assert issues.any?
95 95 assert_nil issues.detect {|issue| issue.project_id != 2}
96 96 end
97 97
98 98 def test_visible_scope_for_admin
99 99 user = User.find(1)
100 100 user.members.each(&:destroy)
101 101 assert user.projects.empty?
102 102 issues = Issue.visible(user).all
103 103 assert issues.any?
104 104 # Admin should see issues on private projects that he does not belong to
105 105 assert issues.detect {|issue| !issue.project.is_public?}
106 106 end
107 107
108 108 def test_errors_full_messages_should_include_custom_fields_errors
109 109 field = IssueCustomField.find_by_name('Database')
110 110
111 111 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :subject => 'test_create', :description => 'IssueTest#test_create_with_required_custom_field')
112 112 assert issue.available_custom_fields.include?(field)
113 113 # Invalid value
114 114 issue.custom_field_values = { field.id => 'SQLServer' }
115 115
116 116 assert !issue.valid?
117 117 assert_equal 1, issue.errors.full_messages.size
118 118 assert_equal "Database #{I18n.translate('activerecord.errors.messages.inclusion')}", issue.errors.full_messages.first
119 119 end
120 120
121 121 def test_update_issue_with_required_custom_field
122 122 field = IssueCustomField.find_by_name('Database')
123 123 field.update_attribute(:is_required, true)
124 124
125 125 issue = Issue.find(1)
126 126 assert_nil issue.custom_value_for(field)
127 127 assert issue.available_custom_fields.include?(field)
128 128 # No change to custom values, issue can be saved
129 129 assert issue.save
130 130 # Blank value
131 131 issue.custom_field_values = { field.id => '' }
132 132 assert !issue.save
133 133 # Valid value
134 134 issue.custom_field_values = { field.id => 'PostgreSQL' }
135 135 assert issue.save
136 136 issue.reload
137 137 assert_equal 'PostgreSQL', issue.custom_value_for(field).value
138 138 end
139 139
140 140 def test_should_not_update_attributes_if_custom_fields_validation_fails
141 141 issue = Issue.find(1)
142 142 field = IssueCustomField.find_by_name('Database')
143 143 assert issue.available_custom_fields.include?(field)
144 144
145 145 issue.custom_field_values = { field.id => 'Invalid' }
146 146 issue.subject = 'Should be not be saved'
147 147 assert !issue.save
148 148
149 149 issue.reload
150 150 assert_equal "Can't print recipes", issue.subject
151 151 end
152 152
153 153 def test_should_not_recreate_custom_values_objects_on_update
154 154 field = IssueCustomField.find_by_name('Database')
155 155
156 156 issue = Issue.find(1)
157 157 issue.custom_field_values = { field.id => 'PostgreSQL' }
158 158 assert issue.save
159 159 custom_value = issue.custom_value_for(field)
160 160 issue.reload
161 161 issue.custom_field_values = { field.id => 'MySQL' }
162 162 assert issue.save
163 163 issue.reload
164 164 assert_equal custom_value.id, issue.custom_value_for(field).id
165 165 end
166 166
167 167 def test_assigning_tracker_id_should_reload_custom_fields_values
168 168 issue = Issue.new(:project => Project.find(1))
169 169 assert issue.custom_field_values.empty?
170 170 issue.tracker_id = 1
171 171 assert issue.custom_field_values.any?
172 172 end
173 173
174 174 def test_assigning_attributes_should_assign_tracker_id_first
175 175 attributes = ActiveSupport::OrderedHash.new
176 176 attributes['custom_field_values'] = { '1' => 'MySQL' }
177 177 attributes['tracker_id'] = '1'
178 178 issue = Issue.new(:project => Project.find(1))
179 179 issue.attributes = attributes
180 180 assert_not_nil issue.custom_value_for(1)
181 181 assert_equal 'MySQL', issue.custom_value_for(1).value
182 182 end
183 183
184 184 def test_should_update_issue_with_disabled_tracker
185 185 p = Project.find(1)
186 186 issue = Issue.find(1)
187 187
188 188 p.trackers.delete(issue.tracker)
189 189 assert !p.trackers.include?(issue.tracker)
190 190
191 191 issue.reload
192 192 issue.subject = 'New subject'
193 193 assert issue.save
194 194 end
195 195
196 196 def test_should_not_set_a_disabled_tracker
197 197 p = Project.find(1)
198 198 p.trackers.delete(Tracker.find(2))
199 199
200 200 issue = Issue.find(1)
201 201 issue.tracker_id = 2
202 202 issue.subject = 'New subject'
203 203 assert !issue.save
204 204 assert_not_nil issue.errors.on(:tracker_id)
205 205 end
206 206
207 207 def test_category_based_assignment
208 208 issue = Issue.create(:project_id => 1, :tracker_id => 1, :author_id => 3, :status_id => 1, :priority => IssuePriority.all.first, :subject => 'Assignment test', :description => 'Assignment test', :category_id => 1)
209 209 assert_equal IssueCategory.find(1).assigned_to, issue.assigned_to
210 210 end
211 211
212 212 def test_copy
213 213 issue = Issue.new.copy_from(1)
214 214 assert issue.save
215 215 issue.reload
216 216 orig = Issue.find(1)
217 217 assert_equal orig.subject, issue.subject
218 218 assert_equal orig.tracker, issue.tracker
219 219 assert_equal "125", issue.custom_value_for(2).value
220 220 end
221 221
222 222 def test_copy_should_copy_status
223 223 orig = Issue.find(8)
224 224 assert orig.status != IssueStatus.default
225 225
226 226 issue = Issue.new.copy_from(orig)
227 227 assert issue.save
228 228 issue.reload
229 229 assert_equal orig.status, issue.status
230 230 end
231 231
232 232 def test_should_close_duplicates
233 233 # Create 3 issues
234 234 issue1 = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :priority => IssuePriority.all.first, :subject => 'Duplicates test', :description => 'Duplicates test')
235 235 assert issue1.save
236 236 issue2 = issue1.clone
237 237 assert issue2.save
238 238 issue3 = issue1.clone
239 239 assert issue3.save
240 240
241 241 # 2 is a dupe of 1
242 242 IssueRelation.create(:issue_from => issue2, :issue_to => issue1, :relation_type => IssueRelation::TYPE_DUPLICATES)
243 243 # And 3 is a dupe of 2
244 244 IssueRelation.create(:issue_from => issue3, :issue_to => issue2, :relation_type => IssueRelation::TYPE_DUPLICATES)
245 245 # And 3 is a dupe of 1 (circular duplicates)
246 246 IssueRelation.create(:issue_from => issue3, :issue_to => issue1, :relation_type => IssueRelation::TYPE_DUPLICATES)
247 247
248 248 assert issue1.reload.duplicates.include?(issue2)
249 249
250 250 # Closing issue 1
251 251 issue1.init_journal(User.find(:first), "Closing issue1")
252 252 issue1.status = IssueStatus.find :first, :conditions => {:is_closed => true}
253 253 assert issue1.save
254 254 # 2 and 3 should be also closed
255 255 assert issue2.reload.closed?
256 256 assert issue3.reload.closed?
257 257 end
258 258
259 259 def test_should_not_close_duplicated_issue
260 260 # Create 3 issues
261 261 issue1 = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :priority => IssuePriority.all.first, :subject => 'Duplicates test', :description => 'Duplicates test')
262 262 assert issue1.save
263 263 issue2 = issue1.clone
264 264 assert issue2.save
265 265
266 266 # 2 is a dupe of 1
267 267 IssueRelation.create(:issue_from => issue2, :issue_to => issue1, :relation_type => IssueRelation::TYPE_DUPLICATES)
268 268 # 2 is a dup of 1 but 1 is not a duplicate of 2
269 269 assert !issue2.reload.duplicates.include?(issue1)
270 270
271 271 # Closing issue 2
272 272 issue2.init_journal(User.find(:first), "Closing issue2")
273 273 issue2.status = IssueStatus.find :first, :conditions => {:is_closed => true}
274 274 assert issue2.save
275 275 # 1 should not be also closed
276 276 assert !issue1.reload.closed?
277 277 end
278 278
279 279 def test_assignable_versions
280 280 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :fixed_version_id => 1, :subject => 'New issue')
281 281 assert_equal ['open'], issue.assignable_versions.collect(&:status).uniq
282 282 end
283 283
284 284 def test_should_not_be_able_to_assign_a_new_issue_to_a_closed_version
285 285 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :fixed_version_id => 1, :subject => 'New issue')
286 286 assert !issue.save
287 287 assert_not_nil issue.errors.on(:fixed_version_id)
288 288 end
289 289
290 290 def test_should_not_be_able_to_assign_a_new_issue_to_a_locked_version
291 291 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :fixed_version_id => 2, :subject => 'New issue')
292 292 assert !issue.save
293 293 assert_not_nil issue.errors.on(:fixed_version_id)
294 294 end
295 295
296 296 def test_should_be_able_to_assign_a_new_issue_to_an_open_version
297 297 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :fixed_version_id => 3, :subject => 'New issue')
298 298 assert issue.save
299 299 end
300 300
301 301 def test_should_be_able_to_update_an_issue_assigned_to_a_closed_version
302 302 issue = Issue.find(11)
303 303 assert_equal 'closed', issue.fixed_version.status
304 304 issue.subject = 'Subject changed'
305 305 assert issue.save
306 306 end
307 307
308 308 def test_should_not_be_able_to_reopen_an_issue_assigned_to_a_closed_version
309 309 issue = Issue.find(11)
310 310 issue.status_id = 1
311 311 assert !issue.save
312 312 assert_not_nil issue.errors.on_base
313 313 end
314 314
315 315 def test_should_be_able_to_reopen_and_reassign_an_issue_assigned_to_a_closed_version
316 316 issue = Issue.find(11)
317 317 issue.status_id = 1
318 318 issue.fixed_version_id = 3
319 319 assert issue.save
320 320 end
321 321
322 322 def test_should_be_able_to_reopen_an_issue_assigned_to_a_locked_version
323 323 issue = Issue.find(12)
324 324 assert_equal 'locked', issue.fixed_version.status
325 325 issue.status_id = 1
326 326 assert issue.save
327 327 end
328 328
329 329 def test_move_to_another_project_with_same_category
330 330 issue = Issue.find(1)
331 331 assert issue.move_to(Project.find(2))
332 332 issue.reload
333 333 assert_equal 2, issue.project_id
334 334 # Category changes
335 335 assert_equal 4, issue.category_id
336 336 # Make sure time entries were move to the target project
337 337 assert_equal 2, issue.time_entries.first.project_id
338 338 end
339 339
340 340 def test_move_to_another_project_without_same_category
341 341 issue = Issue.find(2)
342 342 assert issue.move_to(Project.find(2))
343 343 issue.reload
344 344 assert_equal 2, issue.project_id
345 345 # Category cleared
346 346 assert_nil issue.category_id
347 347 end
348 348
349 349 def test_move_to_another_project_should_clear_fixed_version_when_not_shared
350 350 issue = Issue.find(1)
351 351 issue.update_attribute(:fixed_version_id, 1)
352 352 assert issue.move_to(Project.find(2))
353 353 issue.reload
354 354 assert_equal 2, issue.project_id
355 355 # Cleared fixed_version
356 356 assert_equal nil, issue.fixed_version
357 357 end
358 358
359 359 def test_move_to_another_project_should_keep_fixed_version_when_shared_with_the_target_project
360 360 issue = Issue.find(1)
361 361 issue.update_attribute(:fixed_version_id, 4)
362 362 assert issue.move_to(Project.find(5))
363 363 issue.reload
364 364 assert_equal 5, issue.project_id
365 365 # Keep fixed_version
366 366 assert_equal 4, issue.fixed_version_id
367 367 end
368 368
369 369 def test_move_to_another_project_should_clear_fixed_version_when_not_shared_with_the_target_project
370 370 issue = Issue.find(1)
371 371 issue.update_attribute(:fixed_version_id, 1)
372 372 assert issue.move_to(Project.find(5))
373 373 issue.reload
374 374 assert_equal 5, issue.project_id
375 375 # Cleared fixed_version
376 376 assert_equal nil, issue.fixed_version
377 377 end
378 378
379 379 def test_move_to_another_project_should_keep_fixed_version_when_shared_systemwide
380 380 issue = Issue.find(1)
381 381 issue.update_attribute(:fixed_version_id, 7)
382 382 assert issue.move_to(Project.find(2))
383 383 issue.reload
384 384 assert_equal 2, issue.project_id
385 385 # Keep fixed_version
386 386 assert_equal 7, issue.fixed_version_id
387 387 end
388 388
389 389 def test_move_to_another_project_with_disabled_tracker
390 390 issue = Issue.find(1)
391 391 target = Project.find(2)
392 392 target.tracker_ids = [3]
393 393 target.save
394 394 assert_equal false, issue.move_to(target)
395 395 issue.reload
396 396 assert_equal 1, issue.project_id
397 397 end
398 398
399 399 def test_copy_to_the_same_project
400 400 issue = Issue.find(1)
401 401 copy = nil
402 402 assert_difference 'Issue.count' do
403 403 copy = issue.move_to(issue.project, nil, :copy => true)
404 404 end
405 405 assert_kind_of Issue, copy
406 406 assert_equal issue.project, copy.project
407 407 assert_equal "125", copy.custom_value_for(2).value
408 408 end
409 409
410 410 def test_copy_to_another_project_and_tracker
411 411 issue = Issue.find(1)
412 412 copy = nil
413 413 assert_difference 'Issue.count' do
414 414 copy = issue.move_to(Project.find(3), Tracker.find(2), :copy => true)
415 415 end
416 416 assert_kind_of Issue, copy
417 417 assert_equal Project.find(3), copy.project
418 418 assert_equal Tracker.find(2), copy.tracker
419 419 # Custom field #2 is not associated with target tracker
420 420 assert_nil copy.custom_value_for(2)
421 421 end
422 422
423 423 context "#move_to" do
424 424 context "as a copy" do
425 425 setup do
426 426 @issue = Issue.find(1)
427 427 @copy = nil
428 428 end
429 429
430 430 should "allow assigned_to changes" do
431 431 @copy = @issue.move_to(Project.find(3), Tracker.find(2), {:copy => true, :attributes => {:assigned_to_id => 3}})
432 432 assert_equal 3, @copy.assigned_to_id
433 433 end
434 434
435 435 should "allow status changes" do
436 436 @copy = @issue.move_to(Project.find(3), Tracker.find(2), {:copy => true, :attributes => {:status_id => 2}})
437 437 assert_equal 2, @copy.status_id
438 438 end
439 439
440 440 should "allow start date changes" do
441 441 date = Date.today
442 442 @copy = @issue.move_to(Project.find(3), Tracker.find(2), {:copy => true, :attributes => {:start_date => date}})
443 443 assert_equal date, @copy.start_date
444 444 end
445 445
446 446 should "allow due date changes" do
447 447 date = Date.today
448 448 @copy = @issue.move_to(Project.find(3), Tracker.find(2), {:copy => true, :attributes => {:due_date => date}})
449 449
450 450 assert_equal date, @copy.due_date
451 451 end
452 452 end
453 453 end
454 454
455 455 def test_recipients_should_not_include_users_that_cannot_view_the_issue
456 456 issue = Issue.find(12)
457 457 assert issue.recipients.include?(issue.author.mail)
458 458 # move the issue to a private project
459 459 copy = issue.move_to(Project.find(5), Tracker.find(2), :copy => true)
460 460 # author is not a member of project anymore
461 461 assert !copy.recipients.include?(copy.author.mail)
462 462 end
463 463
464 464 def test_watcher_recipients_should_not_include_users_that_cannot_view_the_issue
465 465 user = User.find(3)
466 466 issue = Issue.find(9)
467 467 Watcher.create!(:user => user, :watchable => issue)
468 468 assert issue.watched_by?(user)
469 469 assert !issue.watcher_recipients.include?(user.mail)
470 470 end
471 471
472 472 def test_issue_destroy
473 473 Issue.find(1).destroy
474 474 assert_nil Issue.find_by_id(1)
475 475 assert_nil TimeEntry.find_by_issue_id(1)
476 476 end
477 477
478 478 def test_blocked
479 479 blocked_issue = Issue.find(9)
480 480 blocking_issue = Issue.find(10)
481 481
482 482 assert blocked_issue.blocked?
483 483 assert !blocking_issue.blocked?
484 484 end
485 485
486 486 def test_blocked_issues_dont_allow_closed_statuses
487 487 blocked_issue = Issue.find(9)
488 488
489 489 allowed_statuses = blocked_issue.new_statuses_allowed_to(users(:users_002))
490 490 assert !allowed_statuses.empty?
491 491 closed_statuses = allowed_statuses.select {|st| st.is_closed?}
492 492 assert closed_statuses.empty?
493 493 end
494 494
495 495 def test_unblocked_issues_allow_closed_statuses
496 496 blocking_issue = Issue.find(10)
497 497
498 498 allowed_statuses = blocking_issue.new_statuses_allowed_to(users(:users_002))
499 499 assert !allowed_statuses.empty?
500 500 closed_statuses = allowed_statuses.select {|st| st.is_closed?}
501 501 assert !closed_statuses.empty?
502 502 end
503 503
504 504 def test_overdue
505 505 assert Issue.new(:due_date => 1.day.ago.to_date).overdue?
506 506 assert !Issue.new(:due_date => Date.today).overdue?
507 507 assert !Issue.new(:due_date => 1.day.from_now.to_date).overdue?
508 508 assert !Issue.new(:due_date => nil).overdue?
509 509 assert !Issue.new(:due_date => 1.day.ago.to_date, :status => IssueStatus.find(:first, :conditions => {:is_closed => true})).overdue?
510 510 end
511 511
512 512 def test_assignable_users
513 513 assert_kind_of User, Issue.find(1).assignable_users.first
514 514 end
515 515
516 516 def test_create_should_send_email_notification
517 517 ActionMailer::Base.deliveries.clear
518 518 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3, :status_id => 1, :priority => IssuePriority.all.first, :subject => 'test_create', :estimated_hours => '1:30')
519 519
520 520 assert issue.save
521 521 assert_equal 1, ActionMailer::Base.deliveries.size
522 522 end
523 523
524 524 def test_stale_issue_should_not_send_email_notification
525 525 ActionMailer::Base.deliveries.clear
526 526 issue = Issue.find(1)
527 527 stale = Issue.find(1)
528 528
529 529 issue.init_journal(User.find(1))
530 530 issue.subject = 'Subjet update'
531 531 assert issue.save
532 532 assert_equal 1, ActionMailer::Base.deliveries.size
533 533 ActionMailer::Base.deliveries.clear
534 534
535 535 stale.init_journal(User.find(1))
536 536 stale.subject = 'Another subjet update'
537 537 assert_raise ActiveRecord::StaleObjectError do
538 538 stale.save
539 539 end
540 540 assert ActionMailer::Base.deliveries.empty?
541 541 end
542 542
543 543 context "#done_ratio" do
544 544 setup do
545 545 @issue = Issue.find(1)
546 546 @issue_status = IssueStatus.find(1)
547 547 @issue_status.update_attribute(:default_done_ratio, 50)
548 548 end
549 549
550 550 context "with Setting.issue_done_ratio using the issue_field" do
551 551 setup do
552 552 Setting.issue_done_ratio = 'issue_field'
553 553 end
554 554
555 555 should "read the issue's field" do
556 556 assert_equal 0, @issue.done_ratio
557 557 end
558 558 end
559 559
560 560 context "with Setting.issue_done_ratio using the issue_status" do
561 561 setup do
562 562 Setting.issue_done_ratio = 'issue_status'
563 563 end
564 564
565 565 should "read the Issue Status's default done ratio" do
566 566 assert_equal 50, @issue.done_ratio
567 567 end
568 568 end
569 569 end
570 570
571 571 context "#update_done_ratio_from_issue_status" do
572 572 setup do
573 573 @issue = Issue.find(1)
574 574 @issue_status = IssueStatus.find(1)
575 575 @issue_status.update_attribute(:default_done_ratio, 50)
576 576 end
577 577
578 578 context "with Setting.issue_done_ratio using the issue_field" do
579 579 setup do
580 580 Setting.issue_done_ratio = 'issue_field'
581 581 end
582 582
583 583 should "not change the issue" do
584 584 @issue.update_done_ratio_from_issue_status
585 585
586 586 assert_equal 0, @issue.done_ratio
587 587 end
588 588 end
589 589
590 590 context "with Setting.issue_done_ratio using the issue_status" do
591 591 setup do
592 592 Setting.issue_done_ratio = 'issue_status'
593 593 end
594 594
595 595 should "not change the issue's done ratio" do
596 596 @issue.update_done_ratio_from_issue_status
597 597
598 598 assert_equal 50, @issue.done_ratio
599 599 end
600 600 end
601 601 end
602
603 context ".allowed_target_projects_on_move" do
604 should "return all active projects for admin users" do
605 User.current = User.find(1)
606 assert_equal Project.active.count, Issue.allowed_target_projects_on_move.size
607 end
608
609 should "return allowed projects for non admin users" do
610 User.current = User.find(2)
611 Role.non_member.remove_permission! :move_issues
612 assert_equal 3, Issue.allowed_target_projects_on_move.size
613
614 Role.non_member.add_permission! :move_issues
615 assert_equal Project.active.count, Issue.allowed_target_projects_on_move.size
616 end
617 end
602 618 end
General Comments 0
You need to be logged in to leave comments. Login now