##// END OF EJS Templates
When copying issues, let the status be changed to default or left unchanged....
Jean-Philippe Lang -
r9270:09375960d69d
parent child
Show More

The requested changes are too big and content was truncated. Show full diff

@@ -1,435 +1,439
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 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, :create]
20 20 default_search_scope :issues
21 21
22 22 before_filter :find_issue, :only => [:show, :edit, :update]
23 23 before_filter :find_issues, :only => [:bulk_edit, :bulk_update, :destroy]
24 24 before_filter :find_project, :only => [:new, :create]
25 25 before_filter :authorize, :except => [:index]
26 26 before_filter :find_optional_project, :only => [:index]
27 27 before_filter :check_for_default_issue_status, :only => [:new, :create]
28 28 before_filter :build_new_issue_from_params, :only => [:new, :create]
29 29 accept_rss_auth :index, :show
30 30 accept_api_auth :index, :show, :create, :update, :destroy
31 31
32 32 rescue_from Query::StatementInvalid, :with => :query_statement_invalid
33 33
34 34 helper :journals
35 35 helper :projects
36 36 include ProjectsHelper
37 37 helper :custom_fields
38 38 include CustomFieldsHelper
39 39 helper :issue_relations
40 40 include IssueRelationsHelper
41 41 helper :watchers
42 42 include WatchersHelper
43 43 helper :attachments
44 44 include AttachmentsHelper
45 45 helper :queries
46 46 include QueriesHelper
47 47 helper :repositories
48 48 include RepositoriesHelper
49 49 helper :sort
50 50 include SortHelper
51 51 include IssuesHelper
52 52 helper :timelog
53 53 helper :gantt
54 54 include Redmine::Export::PDF
55 55
56 56 def index
57 57 retrieve_query
58 58 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
59 59 sort_update(@query.sortable_columns)
60 60
61 61 if @query.valid?
62 62 case params[:format]
63 63 when 'csv', 'pdf'
64 64 @limit = Setting.issues_export_limit.to_i
65 65 when 'atom'
66 66 @limit = Setting.feeds_limit.to_i
67 67 when 'xml', 'json'
68 68 @offset, @limit = api_offset_and_limit
69 69 else
70 70 @limit = per_page_option
71 71 end
72 72
73 73 @issue_count = @query.issue_count
74 74 @issue_pages = Paginator.new self, @issue_count, @limit, params['page']
75 75 @offset ||= @issue_pages.current.offset
76 76 @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version],
77 77 :order => sort_clause,
78 78 :offset => @offset,
79 79 :limit => @limit)
80 80 @issue_count_by_group = @query.issue_count_by_group
81 81
82 82 respond_to do |format|
83 83 format.html { render :template => 'issues/index', :layout => !request.xhr? }
84 84 format.api {
85 85 Issue.load_relations(@issues) if include_in_api_response?('relations')
86 86 }
87 87 format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") }
88 88 format.csv { send_data(issues_to_csv(@issues, @project, @query, params), :type => 'text/csv; header=present', :filename => 'export.csv') }
89 89 format.pdf { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'export.pdf') }
90 90 end
91 91 else
92 92 respond_to do |format|
93 93 format.html { render(:template => 'issues/index', :layout => !request.xhr?) }
94 94 format.any(:atom, :csv, :pdf) { render(:nothing => true) }
95 95 format.api { render_validation_errors(@query) }
96 96 end
97 97 end
98 98 rescue ActiveRecord::RecordNotFound
99 99 render_404
100 100 end
101 101
102 102 def show
103 103 @journals = @issue.journals.find(:all, :include => [:user, :details], :order => "#{Journal.table_name}.created_on ASC")
104 104 @journals.each_with_index {|j,i| j.indice = i+1}
105 105 @journals.reverse! if User.current.wants_comments_in_reverse_order?
106 106
107 107 @changesets = @issue.changesets.visible.all
108 108 @changesets.reverse! if User.current.wants_comments_in_reverse_order?
109 109
110 110 @relations = @issue.relations.select {|r| r.other_issue(@issue) && r.other_issue(@issue).visible? }
111 111 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
112 112 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
113 113 @priorities = IssuePriority.active
114 114 @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
115 115 respond_to do |format|
116 116 format.html {
117 117 retrieve_previous_and_next_issue_ids
118 118 render :template => 'issues/show'
119 119 }
120 120 format.api
121 121 format.atom { render :template => 'journals/index', :layout => false, :content_type => 'application/atom+xml' }
122 122 format.pdf { send_data(issue_to_pdf(@issue), :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") }
123 123 end
124 124 end
125 125
126 126 # Add a new issue
127 127 # The new issue will be created from an existing one if copy_from parameter is given
128 128 def new
129 129 respond_to do |format|
130 130 format.html { render :action => 'new', :layout => !request.xhr? }
131 131 format.js {
132 132 render(:update) { |page|
133 133 if params[:project_change]
134 134 page.replace_html 'all_attributes', :partial => 'form'
135 135 else
136 136 page.replace_html 'attributes', :partial => 'attributes'
137 137 end
138 138 m = User.current.allowed_to?(:log_time, @issue.project) ? 'show' : 'hide'
139 139 page << "if ($('log_time')) {Element.#{m}('log_time');}"
140 140 }
141 141 }
142 142 end
143 143 end
144 144
145 145 def create
146 146 call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue })
147 147 @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads]))
148 148 if @issue.save
149 149 call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
150 150 respond_to do |format|
151 151 format.html {
152 152 render_attachment_warning_if_needed(@issue)
153 153 flash[:notice] = l(:notice_issue_successful_create, :id => "<a href='#{issue_path(@issue)}'>##{@issue.id}</a>")
154 154 redirect_to(params[:continue] ? { :action => 'new', :project_id => @issue.project, :issue => {:tracker_id => @issue.tracker, :parent_issue_id => @issue.parent_issue_id}.reject {|k,v| v.nil?} } :
155 155 { :action => 'show', :id => @issue })
156 156 }
157 157 format.api { render :action => 'show', :status => :created, :location => issue_url(@issue) }
158 158 end
159 159 return
160 160 else
161 161 respond_to do |format|
162 162 format.html { render :action => 'new' }
163 163 format.api { render_validation_errors(@issue) }
164 164 end
165 165 end
166 166 end
167 167
168 168 def edit
169 169 return unless update_issue_from_params
170 170
171 171 respond_to do |format|
172 172 format.html { }
173 173 format.xml { }
174 174 end
175 175 end
176 176
177 177 def update
178 178 return unless update_issue_from_params
179 179 @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads]))
180 180 saved = false
181 181 begin
182 182 saved = @issue.save_issue_with_child_records(params, @time_entry)
183 183 rescue ActiveRecord::StaleObjectError
184 184 @conflict = true
185 185 if params[:last_journal_id]
186 186 if params[:last_journal_id].present?
187 187 last_journal_id = params[:last_journal_id].to_i
188 188 @conflict_journals = @issue.journals.all(:conditions => ["#{Journal.table_name}.id > ?", last_journal_id])
189 189 else
190 190 @conflict_journals = @issue.journals.all
191 191 end
192 192 end
193 193 end
194 194
195 195 if saved
196 196 render_attachment_warning_if_needed(@issue)
197 197 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
198 198
199 199 respond_to do |format|
200 200 format.html { redirect_back_or_default({:action => 'show', :id => @issue}) }
201 201 format.api { head :ok }
202 202 end
203 203 else
204 204 respond_to do |format|
205 205 format.html { render :action => 'edit' }
206 206 format.api { render_validation_errors(@issue) }
207 207 end
208 208 end
209 209 end
210 210
211 211 # Bulk edit/copy a set of issues
212 212 def bulk_edit
213 213 @issues.sort!
214 214 @copy = params[:copy].present?
215 215 @notes = params[:notes]
216 216
217 217 if User.current.allowed_to?(:move_issues, @projects)
218 218 @allowed_projects = Issue.allowed_target_projects_on_move
219 219 if params[:issue]
220 220 @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:issue][:project_id].to_s}
221 221 if @target_project
222 222 target_projects = [@target_project]
223 223 end
224 224 end
225 225 end
226 226 target_projects ||= @projects
227 227
228 @available_statuses = @issues.map(&:new_statuses_allowed_to).reduce(:&)
228 if @copy
229 @available_statuses = [IssueStatus.default]
230 else
231 @available_statuses = @issues.map(&:new_statuses_allowed_to).reduce(:&)
232 end
229 233 @custom_fields = target_projects.map{|p|p.all_issue_custom_fields}.reduce(:&)
230 234 @assignables = target_projects.map(&:assignable_users).reduce(:&)
231 235 @trackers = target_projects.map(&:trackers).reduce(:&)
232 236 @versions = target_projects.map {|p| p.shared_versions.open}.reduce(:&)
233 237 @categories = target_projects.map {|p| p.issue_categories}.reduce(:&)
234 238
235 239 @safe_attributes = @issues.map(&:safe_attribute_names).reduce(:&)
236 240 render :layout => false if request.xhr?
237 241 end
238 242
239 243 def bulk_update
240 244 @issues.sort!
241 245 @copy = params[:copy].present?
242 246 attributes = parse_params_for_bulk_issue_attributes(params)
243 247
244 248 unsaved_issue_ids = []
245 249 moved_issues = []
246 250 @issues.each do |issue|
247 251 issue.reload
248 252 if @copy
249 253 issue = issue.copy
250 254 end
251 255 journal = issue.init_journal(User.current, params[:notes])
252 256 issue.safe_attributes = attributes
253 257 call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue })
254 258 if issue.save
255 259 moved_issues << issue
256 260 else
257 261 # Keep unsaved issue ids to display them in flash error
258 262 unsaved_issue_ids << issue.id
259 263 end
260 264 end
261 265 set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids)
262 266
263 267 if params[:follow]
264 268 if @issues.size == 1 && moved_issues.size == 1
265 269 redirect_to :controller => 'issues', :action => 'show', :id => moved_issues.first
266 270 elsif moved_issues.map(&:project).uniq.size == 1
267 271 redirect_to :controller => 'issues', :action => 'index', :project_id => moved_issues.map(&:project).first
268 272 end
269 273 else
270 274 redirect_back_or_default({:controller => 'issues', :action => 'index', :project_id => @project})
271 275 end
272 276 end
273 277
274 278 def destroy
275 279 @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f
276 280 if @hours > 0
277 281 case params[:todo]
278 282 when 'destroy'
279 283 # nothing to do
280 284 when 'nullify'
281 285 TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues])
282 286 when 'reassign'
283 287 reassign_to = @project.issues.find_by_id(params[:reassign_to_id])
284 288 if reassign_to.nil?
285 289 flash.now[:error] = l(:error_issue_not_found_in_project)
286 290 return
287 291 else
288 292 TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues])
289 293 end
290 294 else
291 295 # display the destroy form if it's a user request
292 296 return unless api_request?
293 297 end
294 298 end
295 299 @issues.each do |issue|
296 300 begin
297 301 issue.reload.destroy
298 302 rescue ::ActiveRecord::RecordNotFound # raised by #reload if issue no longer exists
299 303 # nothing to do, issue was already deleted (eg. by a parent)
300 304 end
301 305 end
302 306 respond_to do |format|
303 307 format.html { redirect_back_or_default(:action => 'index', :project_id => @project) }
304 308 format.api { head :ok }
305 309 end
306 310 end
307 311
308 312 private
309 313 def find_issue
310 314 # Issue.visible.find(...) can not be used to redirect user to the login form
311 315 # if the issue actually exists but requires authentication
312 316 @issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category])
313 317 unless @issue.visible?
314 318 deny_access
315 319 return
316 320 end
317 321 @project = @issue.project
318 322 rescue ActiveRecord::RecordNotFound
319 323 render_404
320 324 end
321 325
322 326 def find_project
323 327 project_id = params[:project_id] || (params[:issue] && params[:issue][:project_id])
324 328 @project = Project.find(project_id)
325 329 rescue ActiveRecord::RecordNotFound
326 330 render_404
327 331 end
328 332
329 333 def retrieve_previous_and_next_issue_ids
330 334 retrieve_query_from_session
331 335 if @query
332 336 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
333 337 sort_update(@query.sortable_columns, 'issues_index_sort')
334 338 limit = 500
335 339 issue_ids = @query.issue_ids(:order => sort_clause, :limit => (limit + 1), :include => [:assigned_to, :tracker, :priority, :category, :fixed_version])
336 340 if (idx = issue_ids.index(@issue.id)) && idx < limit
337 341 if issue_ids.size < 500
338 342 @issue_position = idx + 1
339 343 @issue_count = issue_ids.size
340 344 end
341 345 @prev_issue_id = issue_ids[idx - 1] if idx > 0
342 346 @next_issue_id = issue_ids[idx + 1] if idx < (issue_ids.size - 1)
343 347 end
344 348 end
345 349 end
346 350
347 351 # Used by #edit and #update to set some common instance variables
348 352 # from the params
349 353 # TODO: Refactor, not everything in here is needed by #edit
350 354 def update_issue_from_params
351 355 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
352 356 @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
353 357 @time_entry.attributes = params[:time_entry]
354 358
355 359 @notes = params[:notes] || (params[:issue].present? ? params[:issue][:notes] : nil)
356 360 @issue.init_journal(User.current, @notes)
357 361
358 362 issue_attributes = params[:issue]
359 363 if issue_attributes && params[:conflict_resolution]
360 364 case params[:conflict_resolution]
361 365 when 'overwrite'
362 366 issue_attributes = issue_attributes.dup
363 367 issue_attributes.delete(:lock_version)
364 368 when 'add_notes'
365 369 issue_attributes = {}
366 370 when 'cancel'
367 371 redirect_to issue_path(@issue)
368 372 return false
369 373 end
370 374 end
371 375 @issue.safe_attributes = issue_attributes
372 376 @priorities = IssuePriority.active
373 377 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
374 378 true
375 379 end
376 380
377 381 # TODO: Refactor, lots of extra code in here
378 382 # TODO: Changing tracker on an existing issue should not trigger this
379 383 def build_new_issue_from_params
380 384 if params[:id].blank?
381 385 @issue = Issue.new
382 386 if params[:copy_from]
383 387 begin
384 388 @copy_from = Issue.visible.find(params[:copy_from])
385 389 @copy_attachments = params[:copy_attachments].present? || request.get?
386 390 @issue.copy_from(@copy_from, :attachments => @copy_attachments)
387 391 rescue ActiveRecord::RecordNotFound
388 392 render_404
389 393 return
390 394 end
391 395 end
392 396 @issue.project = @project
393 397 else
394 398 @issue = @project.issues.visible.find(params[:id])
395 399 end
396 400
397 401 @issue.project = @project
398 402 @issue.author = User.current
399 403 # Tracker must be set before custom field values
400 404 @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first)
401 405 if @issue.tracker.nil?
402 406 render_error l(:error_no_tracker_in_project)
403 407 return false
404 408 end
405 409 @issue.start_date ||= Date.today if Setting.default_issue_start_date_to_creation_date?
406 410 @issue.safe_attributes = params[:issue]
407 411
408 412 @priorities = IssuePriority.active
409 413 @allowed_statuses = @issue.new_statuses_allowed_to(User.current, true)
410 414 @available_watchers = (@issue.project.users.sort + @issue.watcher_users).uniq
411 415 end
412 416
413 417 def check_for_default_issue_status
414 418 if IssueStatus.default.nil?
415 419 render_error l(:error_no_default_issue_status)
416 420 return false
417 421 end
418 422 end
419 423
420 424 def parse_params_for_bulk_issue_attributes(params)
421 425 attributes = (params[:issue] || {}).reject {|k,v| v.blank?}
422 426 attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
423 427 if custom = attributes[:custom_field_values]
424 428 custom.reject! {|k,v| v.blank?}
425 429 custom.keys.each do |k|
426 430 if custom[k].is_a?(Array)
427 431 custom[k] << '' if custom[k].delete('__none__')
428 432 else
429 433 custom[k] = '' if custom[k] == '__none__'
430 434 end
431 435 end
432 436 end
433 437 attributes
434 438 end
435 439 end
@@ -1,1074 +1,1078
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 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 include Redmine::SafeAttributes
20 20
21 21 belongs_to :project
22 22 belongs_to :tracker
23 23 belongs_to :status, :class_name => 'IssueStatus', :foreign_key => 'status_id'
24 24 belongs_to :author, :class_name => 'User', :foreign_key => 'author_id'
25 25 belongs_to :assigned_to, :class_name => 'Principal', :foreign_key => 'assigned_to_id'
26 26 belongs_to :fixed_version, :class_name => 'Version', :foreign_key => 'fixed_version_id'
27 27 belongs_to :priority, :class_name => 'IssuePriority', :foreign_key => 'priority_id'
28 28 belongs_to :category, :class_name => 'IssueCategory', :foreign_key => 'category_id'
29 29
30 30 has_many :journals, :as => :journalized, :dependent => :destroy
31 31 has_many :time_entries, :dependent => :delete_all
32 32 has_and_belongs_to_many :changesets, :order => "#{Changeset.table_name}.committed_on ASC, #{Changeset.table_name}.id ASC"
33 33
34 34 has_many :relations_from, :class_name => 'IssueRelation', :foreign_key => 'issue_from_id', :dependent => :delete_all
35 35 has_many :relations_to, :class_name => 'IssueRelation', :foreign_key => 'issue_to_id', :dependent => :delete_all
36 36
37 37 acts_as_nested_set :scope => 'root_id', :dependent => :destroy
38 38 acts_as_attachable :after_add => :attachment_added, :after_remove => :attachment_removed
39 39 acts_as_customizable
40 40 acts_as_watchable
41 41 acts_as_searchable :columns => ['subject', "#{table_name}.description", "#{Journal.table_name}.notes"],
42 42 :include => [:project, :journals],
43 43 # sort by id so that limited eager loading doesn't break with postgresql
44 44 :order_column => "#{table_name}.id"
45 45 acts_as_event :title => Proc.new {|o| "#{o.tracker.name} ##{o.id} (#{o.status}): #{o.subject}"},
46 46 :url => Proc.new {|o| {:controller => 'issues', :action => 'show', :id => o.id}},
47 47 :type => Proc.new {|o| 'issue' + (o.closed? ? ' closed' : '') }
48 48
49 49 acts_as_activity_provider :find_options => {:include => [:project, :author, :tracker]},
50 50 :author_key => :author_id
51 51
52 52 DONE_RATIO_OPTIONS = %w(issue_field issue_status)
53 53
54 54 attr_reader :current_journal
55 55
56 56 validates_presence_of :subject, :priority, :project, :tracker, :author, :status
57 57
58 58 validates_length_of :subject, :maximum => 255
59 59 validates_inclusion_of :done_ratio, :in => 0..100
60 60 validates_numericality_of :estimated_hours, :allow_nil => true
61 61 validate :validate_issue
62 62
63 63 named_scope :visible, lambda {|*args| { :include => :project,
64 64 :conditions => Issue.visible_condition(args.shift || User.current, *args) } }
65 65
66 66 named_scope :open, lambda {|*args|
67 67 is_closed = args.size > 0 ? !args.first : false
68 68 {:conditions => ["#{IssueStatus.table_name}.is_closed = ?", is_closed], :include => :status}
69 69 }
70 70
71 71 named_scope :recently_updated, :order => "#{Issue.table_name}.updated_on DESC"
72 72 named_scope :with_limit, lambda { |limit| { :limit => limit} }
73 73 named_scope :on_active_project, :include => [:status, :project, :tracker],
74 74 :conditions => ["#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"]
75 75
76 76 before_create :default_assign
77 77 before_save :close_duplicates, :update_done_ratio_from_issue_status
78 78 after_save {|issue| issue.send :after_project_change if !issue.id_changed? && issue.project_id_changed?}
79 79 after_save :reschedule_following_issues, :update_nested_set_attributes, :update_parent_attributes, :create_journal
80 80 after_destroy :update_parent_attributes
81 81
82 82 # Returns a SQL conditions string used to find all issues visible by the specified user
83 83 def self.visible_condition(user, options={})
84 84 Project.allowed_to_condition(user, :view_issues, options) do |role, user|
85 85 case role.issues_visibility
86 86 when 'all'
87 87 nil
88 88 when 'default'
89 89 user_ids = [user.id] + user.groups.map(&:id)
90 90 "(#{table_name}.is_private = #{connection.quoted_false} OR #{table_name}.author_id = #{user.id} OR #{table_name}.assigned_to_id IN (#{user_ids.join(',')}))"
91 91 when 'own'
92 92 user_ids = [user.id] + user.groups.map(&:id)
93 93 "(#{table_name}.author_id = #{user.id} OR #{table_name}.assigned_to_id IN (#{user_ids.join(',')}))"
94 94 else
95 95 '1=0'
96 96 end
97 97 end
98 98 end
99 99
100 100 # Returns true if usr or current user is allowed to view the issue
101 101 def visible?(usr=nil)
102 102 (usr || User.current).allowed_to?(:view_issues, self.project) do |role, user|
103 103 case role.issues_visibility
104 104 when 'all'
105 105 true
106 106 when 'default'
107 107 !self.is_private? || self.author == user || user.is_or_belongs_to?(assigned_to)
108 108 when 'own'
109 109 self.author == user || user.is_or_belongs_to?(assigned_to)
110 110 else
111 111 false
112 112 end
113 113 end
114 114 end
115 115
116 116 def initialize(attributes=nil, *args)
117 117 super
118 118 if new_record?
119 119 # set default values for new records only
120 120 self.status ||= IssueStatus.default
121 121 self.priority ||= IssuePriority.default
122 122 self.watcher_user_ids = []
123 123 end
124 124 end
125 125
126 126 # Overrides Redmine::Acts::Customizable::InstanceMethods#available_custom_fields
127 127 def available_custom_fields
128 128 (project && tracker) ? (project.all_issue_custom_fields & tracker.custom_fields.all) : []
129 129 end
130 130
131 131 # Copies attributes from another issue, arg can be an id or an Issue
132 132 def copy_from(arg, options={})
133 133 issue = arg.is_a?(Issue) ? arg : Issue.visible.find(arg)
134 134 self.attributes = issue.attributes.dup.except("id", "root_id", "parent_id", "lft", "rgt", "created_on", "updated_on")
135 135 self.custom_field_values = issue.custom_field_values.inject({}) {|h,v| h[v.custom_field_id] = v.value; h}
136 136 self.status = issue.status
137 137 self.author = User.current
138 138 unless options[:attachments] == false
139 139 self.attachments = issue.attachments.map do |attachement|
140 140 attachement.copy(:container => self)
141 141 end
142 142 end
143 143 @copied_from = issue
144 144 self
145 145 end
146 146
147 147 # Returns an unsaved copy of the issue
148 148 def copy(attributes=nil)
149 149 copy = self.class.new.copy_from(self)
150 150 copy.attributes = attributes if attributes
151 151 copy
152 152 end
153 153
154 154 # Returns true if the issue is a copy
155 155 def copy?
156 156 @copied_from.present?
157 157 end
158 158
159 159 # Moves/copies an issue to a new project and tracker
160 160 # Returns the moved/copied issue on success, false on failure
161 161 def move_to_project(new_project, new_tracker=nil, options={})
162 162 ActiveSupport::Deprecation.warn "Issue#move_to_project is deprecated, use #project= instead."
163 163
164 164 if options[:copy]
165 165 issue = self.copy
166 166 else
167 167 issue = self
168 168 end
169 169
170 170 issue.init_journal(User.current, options[:notes])
171 171
172 172 # Preserve previous behaviour
173 173 # #move_to_project doesn't change tracker automatically
174 174 issue.send :project=, new_project, true
175 175 if new_tracker
176 176 issue.tracker = new_tracker
177 177 end
178 178 # Allow bulk setting of attributes on the issue
179 179 if options[:attributes]
180 180 issue.attributes = options[:attributes]
181 181 end
182 182
183 183 issue.save ? issue : false
184 184 end
185 185
186 186 def status_id=(sid)
187 187 self.status = nil
188 188 write_attribute(:status_id, sid)
189 189 end
190 190
191 191 def priority_id=(pid)
192 192 self.priority = nil
193 193 write_attribute(:priority_id, pid)
194 194 end
195 195
196 196 def category_id=(cid)
197 197 self.category = nil
198 198 write_attribute(:category_id, cid)
199 199 end
200 200
201 201 def fixed_version_id=(vid)
202 202 self.fixed_version = nil
203 203 write_attribute(:fixed_version_id, vid)
204 204 end
205 205
206 206 def tracker_id=(tid)
207 207 self.tracker = nil
208 208 result = write_attribute(:tracker_id, tid)
209 209 @custom_field_values = nil
210 210 result
211 211 end
212 212
213 213 def project_id=(project_id)
214 214 if project_id.to_s != self.project_id.to_s
215 215 self.project = (project_id.present? ? Project.find_by_id(project_id) : nil)
216 216 end
217 217 end
218 218
219 219 def project=(project, keep_tracker=false)
220 220 project_was = self.project
221 221 write_attribute(:project_id, project ? project.id : nil)
222 222 association_instance_set('project', project)
223 223 if project_was && project && project_was != project
224 224 unless keep_tracker || project.trackers.include?(tracker)
225 225 self.tracker = project.trackers.first
226 226 end
227 227 # Reassign to the category with same name if any
228 228 if category
229 229 self.category = project.issue_categories.find_by_name(category.name)
230 230 end
231 231 # Keep the fixed_version if it's still valid in the new_project
232 232 if fixed_version && fixed_version.project != project && !project.shared_versions.include?(fixed_version)
233 233 self.fixed_version = nil
234 234 end
235 235 if parent && parent.project_id != project_id
236 236 self.parent_issue_id = nil
237 237 end
238 238 @custom_field_values = nil
239 239 end
240 240 end
241 241
242 242 def description=(arg)
243 243 if arg.is_a?(String)
244 244 arg = arg.gsub(/(\r\n|\n|\r)/, "\r\n")
245 245 end
246 246 write_attribute(:description, arg)
247 247 end
248 248
249 249 # Overrides attributes= so that project and tracker get assigned first
250 250 def attributes_with_project_and_tracker_first=(new_attributes, *args)
251 251 return if new_attributes.nil?
252 252 attrs = new_attributes.dup
253 253 attrs.stringify_keys!
254 254
255 255 %w(project project_id tracker tracker_id).each do |attr|
256 256 if attrs.has_key?(attr)
257 257 send "#{attr}=", attrs.delete(attr)
258 258 end
259 259 end
260 260 send :attributes_without_project_and_tracker_first=, attrs, *args
261 261 end
262 262 # Do not redefine alias chain on reload (see #4838)
263 263 alias_method_chain(:attributes=, :project_and_tracker_first) unless method_defined?(:attributes_without_project_and_tracker_first=)
264 264
265 265 def estimated_hours=(h)
266 266 write_attribute :estimated_hours, (h.is_a?(String) ? h.to_hours : h)
267 267 end
268 268
269 269 safe_attributes 'project_id',
270 270 :if => lambda {|issue, user|
271 271 if issue.new_record?
272 272 issue.copy?
273 273 elsif user.allowed_to?(:move_issues, issue.project)
274 274 projects = Issue.allowed_target_projects_on_move(user)
275 275 projects.include?(issue.project) && projects.size > 1
276 276 end
277 277 }
278 278
279 279 safe_attributes 'tracker_id',
280 280 'status_id',
281 281 'category_id',
282 282 'assigned_to_id',
283 283 'priority_id',
284 284 'fixed_version_id',
285 285 'subject',
286 286 'description',
287 287 'start_date',
288 288 'due_date',
289 289 'done_ratio',
290 290 'estimated_hours',
291 291 'custom_field_values',
292 292 'custom_fields',
293 293 'lock_version',
294 294 :if => lambda {|issue, user| issue.new_record? || user.allowed_to?(:edit_issues, issue.project) }
295 295
296 296 safe_attributes 'status_id',
297 297 'assigned_to_id',
298 298 'fixed_version_id',
299 299 'done_ratio',
300 300 'lock_version',
301 301 :if => lambda {|issue, user| issue.new_statuses_allowed_to(user).any? }
302 302
303 303 safe_attributes 'watcher_user_ids',
304 304 :if => lambda {|issue, user| issue.new_record? && user.allowed_to?(:add_issue_watchers, issue.project)}
305 305
306 306 safe_attributes 'is_private',
307 307 :if => lambda {|issue, user|
308 308 user.allowed_to?(:set_issues_private, issue.project) ||
309 309 (issue.author == user && user.allowed_to?(:set_own_issues_private, issue.project))
310 310 }
311 311
312 312 safe_attributes 'parent_issue_id',
313 313 :if => lambda {|issue, user| (issue.new_record? || user.allowed_to?(:edit_issues, issue.project)) &&
314 314 user.allowed_to?(:manage_subtasks, issue.project)}
315 315
316 316 # Safely sets attributes
317 317 # Should be called from controllers instead of #attributes=
318 318 # attr_accessible is too rough because we still want things like
319 319 # Issue.new(:project => foo) to work
320 320 def safe_attributes=(attrs, user=User.current)
321 321 return unless attrs.is_a?(Hash)
322 322
323 323 # User can change issue attributes only if he has :edit permission or if a workflow transition is allowed
324 324 attrs = delete_unsafe_attributes(attrs, user)
325 325 return if attrs.empty?
326 326
327 327 # Project and Tracker must be set before since new_statuses_allowed_to depends on it.
328 328 if p = attrs.delete('project_id')
329 329 if allowed_target_projects(user).collect(&:id).include?(p.to_i)
330 330 self.project_id = p
331 331 end
332 332 end
333 333
334 334 if t = attrs.delete('tracker_id')
335 335 self.tracker_id = t
336 336 end
337 337
338 338 if attrs['status_id']
339 339 unless new_statuses_allowed_to(user).collect(&:id).include?(attrs['status_id'].to_i)
340 340 attrs.delete('status_id')
341 341 end
342 342 end
343 343
344 344 unless leaf?
345 345 attrs.reject! {|k,v| %w(priority_id done_ratio start_date due_date estimated_hours).include?(k)}
346 346 end
347 347
348 348 if attrs['parent_issue_id'].present?
349 349 attrs.delete('parent_issue_id') unless Issue.visible(user).exists?(attrs['parent_issue_id'].to_i)
350 350 end
351 351
352 352 # mass-assignment security bypass
353 353 self.send :attributes=, attrs, false
354 354 end
355 355
356 356 def done_ratio
357 357 if Issue.use_status_for_done_ratio? && status && status.default_done_ratio
358 358 status.default_done_ratio
359 359 else
360 360 read_attribute(:done_ratio)
361 361 end
362 362 end
363 363
364 364 def self.use_status_for_done_ratio?
365 365 Setting.issue_done_ratio == 'issue_status'
366 366 end
367 367
368 368 def self.use_field_for_done_ratio?
369 369 Setting.issue_done_ratio == 'issue_field'
370 370 end
371 371
372 372 def validate_issue
373 373 if self.due_date.nil? && @attributes['due_date'] && !@attributes['due_date'].empty?
374 374 errors.add :due_date, :not_a_date
375 375 end
376 376
377 377 if self.due_date and self.start_date and self.due_date < self.start_date
378 378 errors.add :due_date, :greater_than_start_date
379 379 end
380 380
381 381 if start_date && soonest_start && start_date < soonest_start
382 382 errors.add :start_date, :invalid
383 383 end
384 384
385 385 if fixed_version
386 386 if !assignable_versions.include?(fixed_version)
387 387 errors.add :fixed_version_id, :inclusion
388 388 elsif reopened? && fixed_version.closed?
389 389 errors.add :base, I18n.t(:error_can_not_reopen_issue_on_closed_version)
390 390 end
391 391 end
392 392
393 393 # Checks that the issue can not be added/moved to a disabled tracker
394 394 if project && (tracker_id_changed? || project_id_changed?)
395 395 unless project.trackers.include?(tracker)
396 396 errors.add :tracker_id, :inclusion
397 397 end
398 398 end
399 399
400 400 # Checks parent issue assignment
401 401 if @parent_issue
402 402 if @parent_issue.project_id != project_id
403 403 errors.add :parent_issue_id, :not_same_project
404 404 elsif !new_record?
405 405 # moving an existing issue
406 406 if @parent_issue.root_id != root_id
407 407 # we can always move to another tree
408 408 elsif move_possible?(@parent_issue)
409 409 # move accepted inside tree
410 410 else
411 411 errors.add :parent_issue_id, :not_a_valid_parent
412 412 end
413 413 end
414 414 end
415 415 end
416 416
417 417 # Set the done_ratio using the status if that setting is set. This will keep the done_ratios
418 418 # even if the user turns off the setting later
419 419 def update_done_ratio_from_issue_status
420 420 if Issue.use_status_for_done_ratio? && status && status.default_done_ratio
421 421 self.done_ratio = status.default_done_ratio
422 422 end
423 423 end
424 424
425 425 def init_journal(user, notes = "")
426 426 @current_journal ||= Journal.new(:journalized => self, :user => user, :notes => notes)
427 427 if new_record?
428 428 @current_journal.notify = false
429 429 else
430 430 @attributes_before_change = attributes.dup
431 431 @custom_values_before_change = {}
432 432 self.custom_field_values.each {|c| @custom_values_before_change.store c.custom_field_id, c.value }
433 433 end
434 434 # Make sure updated_on is updated when adding a note.
435 435 updated_on_will_change!
436 436 @current_journal
437 437 end
438 438
439 439 # Returns the id of the last journal or nil
440 440 def last_journal_id
441 441 if new_record?
442 442 nil
443 443 else
444 444 journals.first(:order => "#{Journal.table_name}.id DESC").try(:id)
445 445 end
446 446 end
447 447
448 448 # Return true if the issue is closed, otherwise false
449 449 def closed?
450 450 self.status.is_closed?
451 451 end
452 452
453 453 # Return true if the issue is being reopened
454 454 def reopened?
455 455 if !new_record? && status_id_changed?
456 456 status_was = IssueStatus.find_by_id(status_id_was)
457 457 status_new = IssueStatus.find_by_id(status_id)
458 458 if status_was && status_new && status_was.is_closed? && !status_new.is_closed?
459 459 return true
460 460 end
461 461 end
462 462 false
463 463 end
464 464
465 465 # Return true if the issue is being closed
466 466 def closing?
467 467 if !new_record? && status_id_changed?
468 468 status_was = IssueStatus.find_by_id(status_id_was)
469 469 status_new = IssueStatus.find_by_id(status_id)
470 470 if status_was && status_new && !status_was.is_closed? && status_new.is_closed?
471 471 return true
472 472 end
473 473 end
474 474 false
475 475 end
476 476
477 477 # Returns true if the issue is overdue
478 478 def overdue?
479 479 !due_date.nil? && (due_date < Date.today) && !status.is_closed?
480 480 end
481 481
482 482 # Is the amount of work done less than it should for the due date
483 483 def behind_schedule?
484 484 return false if start_date.nil? || due_date.nil?
485 485 done_date = start_date + ((due_date - start_date+1)* done_ratio/100).floor
486 486 return done_date <= Date.today
487 487 end
488 488
489 489 # Does this issue have children?
490 490 def children?
491 491 !leaf?
492 492 end
493 493
494 494 # Users the issue can be assigned to
495 495 def assignable_users
496 496 users = project.assignable_users
497 497 users << author if author
498 498 users << assigned_to if assigned_to
499 499 users.uniq.sort
500 500 end
501 501
502 502 # Versions that the issue can be assigned to
503 503 def assignable_versions
504 504 @assignable_versions ||= (project.shared_versions.open + [Version.find_by_id(fixed_version_id_was)]).compact.uniq.sort
505 505 end
506 506
507 507 # Returns true if this issue is blocked by another issue that is still open
508 508 def blocked?
509 509 !relations_to.detect {|ir| ir.relation_type == 'blocks' && !ir.issue_from.closed?}.nil?
510 510 end
511 511
512 512 # Returns an array of statuses that user is able to apply
513 513 def new_statuses_allowed_to(user=User.current, include_default=false)
514 initial_status = nil
515 if new_record?
516 initial_status = IssueStatus.default
517 elsif status_id_was
518 initial_status = IssueStatus.find_by_id(status_id_was)
514 if new_record? && @copied_from
515 [IssueStatus.default, @copied_from.status].compact.uniq.sort
516 else
517 initial_status = nil
518 if new_record?
519 initial_status = IssueStatus.default
520 elsif status_id_was
521 initial_status = IssueStatus.find_by_id(status_id_was)
522 end
523 initial_status ||= status
524
525 statuses = initial_status.find_new_statuses_allowed_to(
526 user.admin ? Role.all : user.roles_for_project(project),
527 tracker,
528 author == user,
529 assigned_to_id_changed? ? assigned_to_id_was == user.id : assigned_to_id == user.id
530 )
531 statuses << initial_status unless statuses.empty?
532 statuses << IssueStatus.default if include_default
533 statuses = statuses.compact.uniq.sort
534 blocked? ? statuses.reject {|s| s.is_closed?} : statuses
519 535 end
520 initial_status ||= status
521
522 statuses = initial_status.find_new_statuses_allowed_to(
523 user.admin ? Role.all : user.roles_for_project(project),
524 tracker,
525 author == user,
526 assigned_to_id_changed? ? assigned_to_id_was == user.id : assigned_to_id == user.id
527 )
528 statuses << initial_status unless statuses.empty?
529 statuses << IssueStatus.default if include_default
530 statuses = statuses.compact.uniq.sort
531 blocked? ? statuses.reject {|s| s.is_closed?} : statuses
532 536 end
533 537
534 538 def assigned_to_was
535 539 if assigned_to_id_changed? && assigned_to_id_was.present?
536 540 @assigned_to_was ||= User.find_by_id(assigned_to_id_was)
537 541 end
538 542 end
539 543
540 544 # Returns the mail adresses of users that should be notified
541 545 def recipients
542 546 notified = []
543 547 # Author and assignee are always notified unless they have been
544 548 # locked or don't want to be notified
545 549 notified << author if author
546 550 if assigned_to
547 551 notified += (assigned_to.is_a?(Group) ? assigned_to.users : [assigned_to])
548 552 end
549 553 if assigned_to_was
550 554 notified += (assigned_to_was.is_a?(Group) ? assigned_to_was.users : [assigned_to_was])
551 555 end
552 556 notified = notified.select {|u| u.active? && u.notify_about?(self)}
553 557
554 558 notified += project.notified_users
555 559 notified.uniq!
556 560 # Remove users that can not view the issue
557 561 notified.reject! {|user| !visible?(user)}
558 562 notified.collect(&:mail)
559 563 end
560 564
561 565 # Returns the number of hours spent on this issue
562 566 def spent_hours
563 567 @spent_hours ||= time_entries.sum(:hours) || 0
564 568 end
565 569
566 570 # Returns the total number of hours spent on this issue and its descendants
567 571 #
568 572 # Example:
569 573 # spent_hours => 0.0
570 574 # spent_hours => 50.2
571 575 def total_spent_hours
572 576 @total_spent_hours ||= self_and_descendants.sum("#{TimeEntry.table_name}.hours",
573 577 :joins => "LEFT JOIN #{TimeEntry.table_name} ON #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id").to_f || 0.0
574 578 end
575 579
576 580 def relations
577 581 @relations ||= (relations_from + relations_to).sort
578 582 end
579 583
580 584 # Preloads relations for a collection of issues
581 585 def self.load_relations(issues)
582 586 if issues.any?
583 587 relations = IssueRelation.all(:conditions => ["issue_from_id IN (:ids) OR issue_to_id IN (:ids)", {:ids => issues.map(&:id)}])
584 588 issues.each do |issue|
585 589 issue.instance_variable_set "@relations", relations.select {|r| r.issue_from_id == issue.id || r.issue_to_id == issue.id}
586 590 end
587 591 end
588 592 end
589 593
590 594 # Preloads visible spent time for a collection of issues
591 595 def self.load_visible_spent_hours(issues, user=User.current)
592 596 if issues.any?
593 597 hours_by_issue_id = TimeEntry.visible(user).sum(:hours, :group => :issue_id)
594 598 issues.each do |issue|
595 599 issue.instance_variable_set "@spent_hours", (hours_by_issue_id[issue.id] || 0)
596 600 end
597 601 end
598 602 end
599 603
600 604 # Finds an issue relation given its id.
601 605 def find_relation(relation_id)
602 606 IssueRelation.find(relation_id, :conditions => ["issue_to_id = ? OR issue_from_id = ?", id, id])
603 607 end
604 608
605 609 def all_dependent_issues(except=[])
606 610 except << self
607 611 dependencies = []
608 612 relations_from.each do |relation|
609 613 if relation.issue_to && !except.include?(relation.issue_to)
610 614 dependencies << relation.issue_to
611 615 dependencies += relation.issue_to.all_dependent_issues(except)
612 616 end
613 617 end
614 618 dependencies
615 619 end
616 620
617 621 # Returns an array of issues that duplicate this one
618 622 def duplicates
619 623 relations_to.select {|r| r.relation_type == IssueRelation::TYPE_DUPLICATES}.collect {|r| r.issue_from}
620 624 end
621 625
622 626 # Returns the due date or the target due date if any
623 627 # Used on gantt chart
624 628 def due_before
625 629 due_date || (fixed_version ? fixed_version.effective_date : nil)
626 630 end
627 631
628 632 # Returns the time scheduled for this issue.
629 633 #
630 634 # Example:
631 635 # Start Date: 2/26/09, End Date: 3/04/09
632 636 # duration => 6
633 637 def duration
634 638 (start_date && due_date) ? due_date - start_date : 0
635 639 end
636 640
637 641 def soonest_start
638 642 @soonest_start ||= (
639 643 relations_to.collect{|relation| relation.successor_soonest_start} +
640 644 ancestors.collect(&:soonest_start)
641 645 ).compact.max
642 646 end
643 647
644 648 def reschedule_after(date)
645 649 return if date.nil?
646 650 if leaf?
647 651 if start_date.nil? || start_date < date
648 652 self.start_date, self.due_date = date, date + duration
649 653 begin
650 654 save
651 655 rescue ActiveRecord::StaleObjectError
652 656 reload
653 657 self.start_date, self.due_date = date, date + duration
654 658 save
655 659 end
656 660 end
657 661 else
658 662 leaves.each do |leaf|
659 663 leaf.reschedule_after(date)
660 664 end
661 665 end
662 666 end
663 667
664 668 def <=>(issue)
665 669 if issue.nil?
666 670 -1
667 671 elsif root_id != issue.root_id
668 672 (root_id || 0) <=> (issue.root_id || 0)
669 673 else
670 674 (lft || 0) <=> (issue.lft || 0)
671 675 end
672 676 end
673 677
674 678 def to_s
675 679 "#{tracker} ##{id}: #{subject}"
676 680 end
677 681
678 682 # Returns a string of css classes that apply to the issue
679 683 def css_classes
680 684 s = "issue status-#{status.position} priority-#{priority.position}"
681 685 s << ' closed' if closed?
682 686 s << ' overdue' if overdue?
683 687 s << ' child' if child?
684 688 s << ' parent' unless leaf?
685 689 s << ' private' if is_private?
686 690 s << ' created-by-me' if User.current.logged? && author_id == User.current.id
687 691 s << ' assigned-to-me' if User.current.logged? && assigned_to_id == User.current.id
688 692 s
689 693 end
690 694
691 695 # Saves an issue and a time_entry from the parameters
692 696 def save_issue_with_child_records(params, existing_time_entry=nil)
693 697 Issue.transaction do
694 698 if params[:time_entry] && (params[:time_entry][:hours].present? || params[:time_entry][:comments].present?) && User.current.allowed_to?(:log_time, project)
695 699 @time_entry = existing_time_entry || TimeEntry.new
696 700 @time_entry.project = project
697 701 @time_entry.issue = self
698 702 @time_entry.user = User.current
699 703 @time_entry.spent_on = User.current.today
700 704 @time_entry.attributes = params[:time_entry]
701 705 self.time_entries << @time_entry
702 706 end
703 707
704 708 # TODO: Rename hook
705 709 Redmine::Hook.call_hook(:controller_issues_edit_before_save, { :params => params, :issue => self, :time_entry => @time_entry, :journal => @current_journal})
706 710 if save
707 711 # TODO: Rename hook
708 712 Redmine::Hook.call_hook(:controller_issues_edit_after_save, { :params => params, :issue => self, :time_entry => @time_entry, :journal => @current_journal})
709 713 else
710 714 raise ActiveRecord::Rollback
711 715 end
712 716 end
713 717 end
714 718
715 719 # Unassigns issues from +version+ if it's no longer shared with issue's project
716 720 def self.update_versions_from_sharing_change(version)
717 721 # Update issues assigned to the version
718 722 update_versions(["#{Issue.table_name}.fixed_version_id = ?", version.id])
719 723 end
720 724
721 725 # Unassigns issues from versions that are no longer shared
722 726 # after +project+ was moved
723 727 def self.update_versions_from_hierarchy_change(project)
724 728 moved_project_ids = project.self_and_descendants.reload.collect(&:id)
725 729 # Update issues of the moved projects and issues assigned to a version of a moved project
726 730 Issue.update_versions(["#{Version.table_name}.project_id IN (?) OR #{Issue.table_name}.project_id IN (?)", moved_project_ids, moved_project_ids])
727 731 end
728 732
729 733 def parent_issue_id=(arg)
730 734 parent_issue_id = arg.blank? ? nil : arg.to_i
731 735 if parent_issue_id && @parent_issue = Issue.find_by_id(parent_issue_id)
732 736 @parent_issue.id
733 737 else
734 738 @parent_issue = nil
735 739 nil
736 740 end
737 741 end
738 742
739 743 def parent_issue_id
740 744 if instance_variable_defined? :@parent_issue
741 745 @parent_issue.nil? ? nil : @parent_issue.id
742 746 else
743 747 parent_id
744 748 end
745 749 end
746 750
747 751 # Extracted from the ReportsController.
748 752 def self.by_tracker(project)
749 753 count_and_group_by(:project => project,
750 754 :field => 'tracker_id',
751 755 :joins => Tracker.table_name)
752 756 end
753 757
754 758 def self.by_version(project)
755 759 count_and_group_by(:project => project,
756 760 :field => 'fixed_version_id',
757 761 :joins => Version.table_name)
758 762 end
759 763
760 764 def self.by_priority(project)
761 765 count_and_group_by(:project => project,
762 766 :field => 'priority_id',
763 767 :joins => IssuePriority.table_name)
764 768 end
765 769
766 770 def self.by_category(project)
767 771 count_and_group_by(:project => project,
768 772 :field => 'category_id',
769 773 :joins => IssueCategory.table_name)
770 774 end
771 775
772 776 def self.by_assigned_to(project)
773 777 count_and_group_by(:project => project,
774 778 :field => 'assigned_to_id',
775 779 :joins => User.table_name)
776 780 end
777 781
778 782 def self.by_author(project)
779 783 count_and_group_by(:project => project,
780 784 :field => 'author_id',
781 785 :joins => User.table_name)
782 786 end
783 787
784 788 def self.by_subproject(project)
785 789 ActiveRecord::Base.connection.select_all("select s.id as status_id,
786 790 s.is_closed as closed,
787 791 #{Issue.table_name}.project_id as project_id,
788 792 count(#{Issue.table_name}.id) as total
789 793 from
790 794 #{Issue.table_name}, #{Project.table_name}, #{IssueStatus.table_name} s
791 795 where
792 796 #{Issue.table_name}.status_id=s.id
793 797 and #{Issue.table_name}.project_id = #{Project.table_name}.id
794 798 and #{visible_condition(User.current, :project => project, :with_subprojects => true)}
795 799 and #{Issue.table_name}.project_id <> #{project.id}
796 800 group by s.id, s.is_closed, #{Issue.table_name}.project_id") if project.descendants.active.any?
797 801 end
798 802 # End ReportsController extraction
799 803
800 804 # Returns an array of projects that user can assign the issue to
801 805 def allowed_target_projects(user=User.current)
802 806 if new_record?
803 807 Project.all(:conditions => Project.allowed_to_condition(user, :add_issues))
804 808 else
805 809 self.class.allowed_target_projects_on_move(user)
806 810 end
807 811 end
808 812
809 813 # Returns an array of projects that user can move issues to
810 814 def self.allowed_target_projects_on_move(user=User.current)
811 815 Project.all(:conditions => Project.allowed_to_condition(user, :move_issues))
812 816 end
813 817
814 818 private
815 819
816 820 def after_project_change
817 821 # Update project_id on related time entries
818 822 TimeEntry.update_all(["project_id = ?", project_id], {:issue_id => id})
819 823
820 824 # Delete issue relations
821 825 unless Setting.cross_project_issue_relations?
822 826 relations_from.clear
823 827 relations_to.clear
824 828 end
825 829
826 830 # Move subtasks
827 831 children.each do |child|
828 832 # Change project and keep project
829 833 child.send :project=, project, true
830 834 unless child.save
831 835 raise ActiveRecord::Rollback
832 836 end
833 837 end
834 838 end
835 839
836 840 def update_nested_set_attributes
837 841 if root_id.nil?
838 842 # issue was just created
839 843 self.root_id = (@parent_issue.nil? ? id : @parent_issue.root_id)
840 844 set_default_left_and_right
841 845 Issue.update_all("root_id = #{root_id}, lft = #{lft}, rgt = #{rgt}", ["id = ?", id])
842 846 if @parent_issue
843 847 move_to_child_of(@parent_issue)
844 848 end
845 849 reload
846 850 elsif parent_issue_id != parent_id
847 851 former_parent_id = parent_id
848 852 # moving an existing issue
849 853 if @parent_issue && @parent_issue.root_id == root_id
850 854 # inside the same tree
851 855 move_to_child_of(@parent_issue)
852 856 else
853 857 # to another tree
854 858 unless root?
855 859 move_to_right_of(root)
856 860 reload
857 861 end
858 862 old_root_id = root_id
859 863 self.root_id = (@parent_issue.nil? ? id : @parent_issue.root_id )
860 864 target_maxright = nested_set_scope.maximum(right_column_name) || 0
861 865 offset = target_maxright + 1 - lft
862 866 Issue.update_all("root_id = #{root_id}, lft = lft + #{offset}, rgt = rgt + #{offset}",
863 867 ["root_id = ? AND lft >= ? AND rgt <= ? ", old_root_id, lft, rgt])
864 868 self[left_column_name] = lft + offset
865 869 self[right_column_name] = rgt + offset
866 870 if @parent_issue
867 871 move_to_child_of(@parent_issue)
868 872 end
869 873 end
870 874 reload
871 875 # delete invalid relations of all descendants
872 876 self_and_descendants.each do |issue|
873 877 issue.relations.each do |relation|
874 878 relation.destroy unless relation.valid?
875 879 end
876 880 end
877 881 # update former parent
878 882 recalculate_attributes_for(former_parent_id) if former_parent_id
879 883 end
880 884 remove_instance_variable(:@parent_issue) if instance_variable_defined?(:@parent_issue)
881 885 end
882 886
883 887 def update_parent_attributes
884 888 recalculate_attributes_for(parent_id) if parent_id
885 889 end
886 890
887 891 def recalculate_attributes_for(issue_id)
888 892 if issue_id && p = Issue.find_by_id(issue_id)
889 893 # priority = highest priority of children
890 894 if priority_position = p.children.maximum("#{IssuePriority.table_name}.position", :joins => :priority)
891 895 p.priority = IssuePriority.find_by_position(priority_position)
892 896 end
893 897
894 898 # start/due dates = lowest/highest dates of children
895 899 p.start_date = p.children.minimum(:start_date)
896 900 p.due_date = p.children.maximum(:due_date)
897 901 if p.start_date && p.due_date && p.due_date < p.start_date
898 902 p.start_date, p.due_date = p.due_date, p.start_date
899 903 end
900 904
901 905 # done ratio = weighted average ratio of leaves
902 906 unless Issue.use_status_for_done_ratio? && p.status && p.status.default_done_ratio
903 907 leaves_count = p.leaves.count
904 908 if leaves_count > 0
905 909 average = p.leaves.average(:estimated_hours).to_f
906 910 if average == 0
907 911 average = 1
908 912 end
909 913 done = p.leaves.sum("COALESCE(estimated_hours, #{average}) * (CASE WHEN is_closed = #{connection.quoted_true} THEN 100 ELSE COALESCE(done_ratio, 0) END)", :joins => :status).to_f
910 914 progress = done / (average * leaves_count)
911 915 p.done_ratio = progress.round
912 916 end
913 917 end
914 918
915 919 # estimate = sum of leaves estimates
916 920 p.estimated_hours = p.leaves.sum(:estimated_hours).to_f
917 921 p.estimated_hours = nil if p.estimated_hours == 0.0
918 922
919 923 # ancestors will be recursively updated
920 924 p.save(false)
921 925 end
922 926 end
923 927
924 928 # Update issues so their versions are not pointing to a
925 929 # fixed_version that is not shared with the issue's project
926 930 def self.update_versions(conditions=nil)
927 931 # Only need to update issues with a fixed_version from
928 932 # a different project and that is not systemwide shared
929 933 Issue.scoped(:conditions => conditions).all(
930 934 :conditions => "#{Issue.table_name}.fixed_version_id IS NOT NULL" +
931 935 " AND #{Issue.table_name}.project_id <> #{Version.table_name}.project_id" +
932 936 " AND #{Version.table_name}.sharing <> 'system'",
933 937 :include => [:project, :fixed_version]
934 938 ).each do |issue|
935 939 next if issue.project.nil? || issue.fixed_version.nil?
936 940 unless issue.project.shared_versions.include?(issue.fixed_version)
937 941 issue.init_journal(User.current)
938 942 issue.fixed_version = nil
939 943 issue.save
940 944 end
941 945 end
942 946 end
943 947
944 948 # Callback on attachment deletion
945 949 def attachment_added(obj)
946 950 if @current_journal && !obj.new_record?
947 951 @current_journal.details << JournalDetail.new(:property => 'attachment', :prop_key => obj.id, :value => obj.filename)
948 952 end
949 953 end
950 954
951 955 # Callback on attachment deletion
952 956 def attachment_removed(obj)
953 957 if @current_journal && !obj.new_record?
954 958 @current_journal.details << JournalDetail.new(:property => 'attachment', :prop_key => obj.id, :old_value => obj.filename)
955 959 @current_journal.save
956 960 end
957 961 end
958 962
959 963 # Default assignment based on category
960 964 def default_assign
961 965 if assigned_to.nil? && category && category.assigned_to
962 966 self.assigned_to = category.assigned_to
963 967 end
964 968 end
965 969
966 970 # Updates start/due dates of following issues
967 971 def reschedule_following_issues
968 972 if start_date_changed? || due_date_changed?
969 973 relations_from.each do |relation|
970 974 relation.set_issue_to_dates
971 975 end
972 976 end
973 977 end
974 978
975 979 # Closes duplicates if the issue is being closed
976 980 def close_duplicates
977 981 if closing?
978 982 duplicates.each do |duplicate|
979 983 # Reload is need in case the duplicate was updated by a previous duplicate
980 984 duplicate.reload
981 985 # Don't re-close it if it's already closed
982 986 next if duplicate.closed?
983 987 # Same user and notes
984 988 if @current_journal
985 989 duplicate.init_journal(@current_journal.user, @current_journal.notes)
986 990 end
987 991 duplicate.update_attribute :status, self.status
988 992 end
989 993 end
990 994 end
991 995
992 996 # Saves the changes in a Journal
993 997 # Called after_save
994 998 def create_journal
995 999 if @current_journal
996 1000 # attributes changes
997 1001 if @attributes_before_change
998 1002 (Issue.column_names - %w(id root_id lft rgt lock_version created_on updated_on)).each {|c|
999 1003 before = @attributes_before_change[c]
1000 1004 after = send(c)
1001 1005 next if before == after || (before.blank? && after.blank?)
1002 1006 @current_journal.details << JournalDetail.new(:property => 'attr',
1003 1007 :prop_key => c,
1004 1008 :old_value => before,
1005 1009 :value => after)
1006 1010 }
1007 1011 end
1008 1012 if @custom_values_before_change
1009 1013 # custom fields changes
1010 1014 custom_field_values.each {|c|
1011 1015 before = @custom_values_before_change[c.custom_field_id]
1012 1016 after = c.value
1013 1017 next if before == after || (before.blank? && after.blank?)
1014 1018
1015 1019 if before.is_a?(Array) || after.is_a?(Array)
1016 1020 before = [before] unless before.is_a?(Array)
1017 1021 after = [after] unless after.is_a?(Array)
1018 1022
1019 1023 # values removed
1020 1024 (before - after).reject(&:blank?).each do |value|
1021 1025 @current_journal.details << JournalDetail.new(:property => 'cf',
1022 1026 :prop_key => c.custom_field_id,
1023 1027 :old_value => value,
1024 1028 :value => nil)
1025 1029 end
1026 1030 # values added
1027 1031 (after - before).reject(&:blank?).each do |value|
1028 1032 @current_journal.details << JournalDetail.new(:property => 'cf',
1029 1033 :prop_key => c.custom_field_id,
1030 1034 :old_value => nil,
1031 1035 :value => value)
1032 1036 end
1033 1037 else
1034 1038 @current_journal.details << JournalDetail.new(:property => 'cf',
1035 1039 :prop_key => c.custom_field_id,
1036 1040 :old_value => before,
1037 1041 :value => after)
1038 1042 end
1039 1043 }
1040 1044 end
1041 1045 @current_journal.save
1042 1046 # reset current journal
1043 1047 init_journal @current_journal.user, @current_journal.notes
1044 1048 end
1045 1049 end
1046 1050
1047 1051 # Query generator for selecting groups of issue counts for a project
1048 1052 # based on specific criteria
1049 1053 #
1050 1054 # Options
1051 1055 # * project - Project to search in.
1052 1056 # * field - String. Issue field to key off of in the grouping.
1053 1057 # * joins - String. The table name to join against.
1054 1058 def self.count_and_group_by(options)
1055 1059 project = options.delete(:project)
1056 1060 select_field = options.delete(:field)
1057 1061 joins = options.delete(:joins)
1058 1062
1059 1063 where = "#{Issue.table_name}.#{select_field}=j.id"
1060 1064
1061 1065 ActiveRecord::Base.connection.select_all("select s.id as status_id,
1062 1066 s.is_closed as closed,
1063 1067 j.id as #{select_field},
1064 1068 count(#{Issue.table_name}.id) as total
1065 1069 from
1066 1070 #{Issue.table_name}, #{Project.table_name}, #{IssueStatus.table_name} s, #{joins} j
1067 1071 where
1068 1072 #{Issue.table_name}.status_id=s.id
1069 1073 and #{where}
1070 1074 and #{Issue.table_name}.project_id=#{Project.table_name}.id
1071 1075 and #{visible_condition(User.current, :project => project)}
1072 1076 group by s.id, s.is_closed, j.id")
1073 1077 end
1074 1078 end
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
@@ -1,1255 +1,1263
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2012 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.expand_path('../../test_helper', __FILE__)
19 19
20 20 class IssueTest < ActiveSupport::TestCase
21 21 fixtures :projects, :users, :members, :member_roles, :roles,
22 22 :groups_users,
23 23 :trackers, :projects_trackers,
24 24 :enabled_modules,
25 25 :versions,
26 26 :issue_statuses, :issue_categories, :issue_relations, :workflows,
27 27 :enumerations,
28 28 :issues,
29 29 :custom_fields, :custom_fields_projects, :custom_fields_trackers, :custom_values,
30 30 :time_entries
31 31
32 32 include Redmine::I18n
33 33
34 34 def test_create
35 35 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3,
36 36 :status_id => 1, :priority => IssuePriority.all.first,
37 37 :subject => 'test_create',
38 38 :description => 'IssueTest#test_create', :estimated_hours => '1:30')
39 39 assert issue.save
40 40 issue.reload
41 41 assert_equal 1.5, issue.estimated_hours
42 42 end
43 43
44 44 def test_create_minimal
45 45 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3,
46 46 :status_id => 1, :priority => IssuePriority.all.first,
47 47 :subject => 'test_create')
48 48 assert issue.save
49 49 assert issue.description.nil?
50 50 end
51 51
52 52 def test_create_with_required_custom_field
53 53 set_language_if_valid 'en'
54 54 field = IssueCustomField.find_by_name('Database')
55 55 field.update_attribute(:is_required, true)
56 56
57 57 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1,
58 58 :status_id => 1, :subject => 'test_create',
59 59 :description => 'IssueTest#test_create_with_required_custom_field')
60 60 assert issue.available_custom_fields.include?(field)
61 61 # No value for the custom field
62 62 assert !issue.save
63 63 assert_equal ["Database can't be blank"], issue.errors.full_messages
64 64 # Blank value
65 65 issue.custom_field_values = { field.id => '' }
66 66 assert !issue.save
67 67 assert_equal ["Database can't be blank"], issue.errors.full_messages
68 68 # Invalid value
69 69 issue.custom_field_values = { field.id => 'SQLServer' }
70 70 assert !issue.save
71 71 assert_equal ["Database is not included in the list"], issue.errors.full_messages
72 72 # Valid value
73 73 issue.custom_field_values = { field.id => 'PostgreSQL' }
74 74 assert issue.save
75 75 issue.reload
76 76 assert_equal 'PostgreSQL', issue.custom_value_for(field).value
77 77 end
78 78
79 79 def test_create_with_group_assignment
80 80 with_settings :issue_group_assignment => '1' do
81 81 assert Issue.new(:project_id => 2, :tracker_id => 1, :author_id => 1,
82 82 :subject => 'Group assignment',
83 83 :assigned_to_id => 11).save
84 84 issue = Issue.first(:order => 'id DESC')
85 85 assert_kind_of Group, issue.assigned_to
86 86 assert_equal Group.find(11), issue.assigned_to
87 87 end
88 88 end
89 89
90 90 def assert_visibility_match(user, issues)
91 91 assert_equal issues.collect(&:id).sort, Issue.all.select {|issue| issue.visible?(user)}.collect(&:id).sort
92 92 end
93 93
94 94 def test_visible_scope_for_anonymous
95 95 # Anonymous user should see issues of public projects only
96 96 issues = Issue.visible(User.anonymous).all
97 97 assert issues.any?
98 98 assert_nil issues.detect {|issue| !issue.project.is_public?}
99 99 assert_nil issues.detect {|issue| issue.is_private?}
100 100 assert_visibility_match User.anonymous, issues
101 101 end
102 102
103 103 def test_visible_scope_for_anonymous_with_own_issues_visibility
104 104 Role.anonymous.update_attribute :issues_visibility, 'own'
105 105 Issue.create!(:project_id => 1, :tracker_id => 1,
106 106 :author_id => User.anonymous.id,
107 107 :subject => 'Issue by anonymous')
108 108
109 109 issues = Issue.visible(User.anonymous).all
110 110 assert issues.any?
111 111 assert_nil issues.detect {|issue| issue.author != User.anonymous}
112 112 assert_visibility_match User.anonymous, issues
113 113 end
114 114
115 115 def test_visible_scope_for_anonymous_without_view_issues_permissions
116 116 # Anonymous user should not see issues without permission
117 117 Role.anonymous.remove_permission!(:view_issues)
118 118 issues = Issue.visible(User.anonymous).all
119 119 assert issues.empty?
120 120 assert_visibility_match User.anonymous, issues
121 121 end
122 122
123 123 def test_visible_scope_for_non_member
124 124 user = User.find(9)
125 125 assert user.projects.empty?
126 126 # Non member user should see issues of public projects only
127 127 issues = Issue.visible(user).all
128 128 assert issues.any?
129 129 assert_nil issues.detect {|issue| !issue.project.is_public?}
130 130 assert_nil issues.detect {|issue| issue.is_private?}
131 131 assert_visibility_match user, issues
132 132 end
133 133
134 134 def test_visible_scope_for_non_member_with_own_issues_visibility
135 135 Role.non_member.update_attribute :issues_visibility, 'own'
136 136 Issue.create!(:project_id => 1, :tracker_id => 1, :author_id => 9, :subject => 'Issue by non member')
137 137 user = User.find(9)
138 138
139 139 issues = Issue.visible(user).all
140 140 assert issues.any?
141 141 assert_nil issues.detect {|issue| issue.author != user}
142 142 assert_visibility_match user, issues
143 143 end
144 144
145 145 def test_visible_scope_for_non_member_without_view_issues_permissions
146 146 # Non member user should not see issues without permission
147 147 Role.non_member.remove_permission!(:view_issues)
148 148 user = User.find(9)
149 149 assert user.projects.empty?
150 150 issues = Issue.visible(user).all
151 151 assert issues.empty?
152 152 assert_visibility_match user, issues
153 153 end
154 154
155 155 def test_visible_scope_for_member
156 156 user = User.find(9)
157 157 # User should see issues of projects for which he has view_issues permissions only
158 158 Role.non_member.remove_permission!(:view_issues)
159 159 Member.create!(:principal => user, :project_id => 3, :role_ids => [2])
160 160 issues = Issue.visible(user).all
161 161 assert issues.any?
162 162 assert_nil issues.detect {|issue| issue.project_id != 3}
163 163 assert_nil issues.detect {|issue| issue.is_private?}
164 164 assert_visibility_match user, issues
165 165 end
166 166
167 167 def test_visible_scope_for_member_with_groups_should_return_assigned_issues
168 168 user = User.find(8)
169 169 assert user.groups.any?
170 170 Member.create!(:principal => user.groups.first, :project_id => 1, :role_ids => [2])
171 171 Role.non_member.remove_permission!(:view_issues)
172 172
173 173 issue = Issue.create(:project_id => 1, :tracker_id => 1, :author_id => 3,
174 174 :status_id => 1, :priority => IssuePriority.all.first,
175 175 :subject => 'Assignment test',
176 176 :assigned_to => user.groups.first,
177 177 :is_private => true)
178 178
179 179 Role.find(2).update_attribute :issues_visibility, 'default'
180 180 issues = Issue.visible(User.find(8)).all
181 181 assert issues.any?
182 182 assert issues.include?(issue)
183 183
184 184 Role.find(2).update_attribute :issues_visibility, 'own'
185 185 issues = Issue.visible(User.find(8)).all
186 186 assert issues.any?
187 187 assert issues.include?(issue)
188 188 end
189 189
190 190 def test_visible_scope_for_admin
191 191 user = User.find(1)
192 192 user.members.each(&:destroy)
193 193 assert user.projects.empty?
194 194 issues = Issue.visible(user).all
195 195 assert issues.any?
196 196 # Admin should see issues on private projects that he does not belong to
197 197 assert issues.detect {|issue| !issue.project.is_public?}
198 198 # Admin should see private issues of other users
199 199 assert issues.detect {|issue| issue.is_private? && issue.author != user}
200 200 assert_visibility_match user, issues
201 201 end
202 202
203 203 def test_visible_scope_with_project
204 204 project = Project.find(1)
205 205 issues = Issue.visible(User.find(2), :project => project).all
206 206 projects = issues.collect(&:project).uniq
207 207 assert_equal 1, projects.size
208 208 assert_equal project, projects.first
209 209 end
210 210
211 211 def test_visible_scope_with_project_and_subprojects
212 212 project = Project.find(1)
213 213 issues = Issue.visible(User.find(2), :project => project, :with_subprojects => true).all
214 214 projects = issues.collect(&:project).uniq
215 215 assert projects.size > 1
216 216 assert_equal [], projects.select {|p| !p.is_or_is_descendant_of?(project)}
217 217 end
218 218
219 219 def test_visible_and_nested_set_scopes
220 220 assert_equal 0, Issue.find(1).descendants.visible.all.size
221 221 end
222 222
223 223 def test_open_scope
224 224 issues = Issue.open.all
225 225 assert_nil issues.detect(&:closed?)
226 226 end
227 227
228 228 def test_open_scope_with_arg
229 229 issues = Issue.open(false).all
230 230 assert_equal issues, issues.select(&:closed?)
231 231 end
232 232
233 233 def test_errors_full_messages_should_include_custom_fields_errors
234 234 field = IssueCustomField.find_by_name('Database')
235 235
236 236 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1,
237 237 :status_id => 1, :subject => 'test_create',
238 238 :description => 'IssueTest#test_create_with_required_custom_field')
239 239 assert issue.available_custom_fields.include?(field)
240 240 # Invalid value
241 241 issue.custom_field_values = { field.id => 'SQLServer' }
242 242
243 243 assert !issue.valid?
244 244 assert_equal 1, issue.errors.full_messages.size
245 245 assert_equal "Database #{I18n.translate('activerecord.errors.messages.inclusion')}",
246 246 issue.errors.full_messages.first
247 247 end
248 248
249 249 def test_update_issue_with_required_custom_field
250 250 field = IssueCustomField.find_by_name('Database')
251 251 field.update_attribute(:is_required, true)
252 252
253 253 issue = Issue.find(1)
254 254 assert_nil issue.custom_value_for(field)
255 255 assert issue.available_custom_fields.include?(field)
256 256 # No change to custom values, issue can be saved
257 257 assert issue.save
258 258 # Blank value
259 259 issue.custom_field_values = { field.id => '' }
260 260 assert !issue.save
261 261 # Valid value
262 262 issue.custom_field_values = { field.id => 'PostgreSQL' }
263 263 assert issue.save
264 264 issue.reload
265 265 assert_equal 'PostgreSQL', issue.custom_value_for(field).value
266 266 end
267 267
268 268 def test_should_not_update_attributes_if_custom_fields_validation_fails
269 269 issue = Issue.find(1)
270 270 field = IssueCustomField.find_by_name('Database')
271 271 assert issue.available_custom_fields.include?(field)
272 272
273 273 issue.custom_field_values = { field.id => 'Invalid' }
274 274 issue.subject = 'Should be not be saved'
275 275 assert !issue.save
276 276
277 277 issue.reload
278 278 assert_equal "Can't print recipes", issue.subject
279 279 end
280 280
281 281 def test_should_not_recreate_custom_values_objects_on_update
282 282 field = IssueCustomField.find_by_name('Database')
283 283
284 284 issue = Issue.find(1)
285 285 issue.custom_field_values = { field.id => 'PostgreSQL' }
286 286 assert issue.save
287 287 custom_value = issue.custom_value_for(field)
288 288 issue.reload
289 289 issue.custom_field_values = { field.id => 'MySQL' }
290 290 assert issue.save
291 291 issue.reload
292 292 assert_equal custom_value.id, issue.custom_value_for(field).id
293 293 end
294 294
295 295 def test_should_not_update_custom_fields_on_changing_tracker_with_different_custom_fields
296 296 issue = Issue.create!(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :subject => 'Test', :custom_field_values => {'2' => 'Test'})
297 297 assert !Tracker.find(2).custom_field_ids.include?(2)
298 298
299 299 issue = Issue.find(issue.id)
300 300 issue.attributes = {:tracker_id => 2, :custom_field_values => {'1' => ''}}
301 301
302 302 issue = Issue.find(issue.id)
303 303 custom_value = issue.custom_value_for(2)
304 304 assert_not_nil custom_value
305 305 assert_equal 'Test', custom_value.value
306 306 end
307 307
308 308 def test_assigning_tracker_id_should_reload_custom_fields_values
309 309 issue = Issue.new(:project => Project.find(1))
310 310 assert issue.custom_field_values.empty?
311 311 issue.tracker_id = 1
312 312 assert issue.custom_field_values.any?
313 313 end
314 314
315 315 def test_assigning_attributes_should_assign_project_and_tracker_first
316 316 seq = sequence('seq')
317 317 issue = Issue.new
318 318 issue.expects(:project_id=).in_sequence(seq)
319 319 issue.expects(:tracker_id=).in_sequence(seq)
320 320 issue.expects(:subject=).in_sequence(seq)
321 321 issue.attributes = {:tracker_id => 2, :project_id => 1, :subject => 'Test'}
322 322 end
323 323
324 324 def test_assigning_tracker_and_custom_fields_should_assign_custom_fields
325 325 attributes = ActiveSupport::OrderedHash.new
326 326 attributes['custom_field_values'] = { '1' => 'MySQL' }
327 327 attributes['tracker_id'] = '1'
328 328 issue = Issue.new(:project => Project.find(1))
329 329 issue.attributes = attributes
330 330 assert_equal 'MySQL', issue.custom_field_value(1)
331 331 end
332 332
333 333 def test_should_update_issue_with_disabled_tracker
334 334 p = Project.find(1)
335 335 issue = Issue.find(1)
336 336
337 337 p.trackers.delete(issue.tracker)
338 338 assert !p.trackers.include?(issue.tracker)
339 339
340 340 issue.reload
341 341 issue.subject = 'New subject'
342 342 assert issue.save
343 343 end
344 344
345 345 def test_should_not_set_a_disabled_tracker
346 346 p = Project.find(1)
347 347 p.trackers.delete(Tracker.find(2))
348 348
349 349 issue = Issue.find(1)
350 350 issue.tracker_id = 2
351 351 issue.subject = 'New subject'
352 352 assert !issue.save
353 353 assert_not_nil issue.errors[:tracker_id]
354 354 end
355 355
356 356 def test_category_based_assignment
357 357 issue = Issue.create(:project_id => 1, :tracker_id => 1, :author_id => 3,
358 358 :status_id => 1, :priority => IssuePriority.all.first,
359 359 :subject => 'Assignment test',
360 360 :description => 'Assignment test', :category_id => 1)
361 361 assert_equal IssueCategory.find(1).assigned_to, issue.assigned_to
362 362 end
363 363
364 364 def test_new_statuses_allowed_to
365 365 Workflow.delete_all
366 366
367 367 Workflow.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 2, :author => false, :assignee => false)
368 368 Workflow.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 3, :author => true, :assignee => false)
369 369 Workflow.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 4, :author => false, :assignee => true)
370 370 Workflow.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 5, :author => true, :assignee => true)
371 371 status = IssueStatus.find(1)
372 372 role = Role.find(1)
373 373 tracker = Tracker.find(1)
374 374 user = User.find(2)
375 375
376 376 issue = Issue.generate!(:tracker => tracker, :status => status, :project_id => 1)
377 377 assert_equal [1, 2], issue.new_statuses_allowed_to(user).map(&:id)
378 378
379 379 issue = Issue.generate!(:tracker => tracker, :status => status, :project_id => 1, :author => user)
380 380 assert_equal [1, 2, 3, 5], issue.new_statuses_allowed_to(user).map(&:id)
381 381
382 382 issue = Issue.generate!(:tracker => tracker, :status => status, :project_id => 1, :assigned_to => user)
383 383 assert_equal [1, 2, 4, 5], issue.new_statuses_allowed_to(user).map(&:id)
384 384
385 385 issue = Issue.generate!(:tracker => tracker, :status => status, :project_id => 1, :author => user, :assigned_to => user)
386 386 assert_equal [1, 2, 3, 4, 5], issue.new_statuses_allowed_to(user).map(&:id)
387 387 end
388 388
389 389 def test_new_statuses_allowed_to_should_return_all_transitions_for_admin
390 390 admin = User.find(1)
391 391 issue = Issue.find(1)
392 392 assert !admin.member_of?(issue.project)
393 393 expected_statuses = [issue.status] + Workflow.find_all_by_old_status_id(issue.status_id).map(&:new_status).uniq.sort
394 394
395 395 assert_equal expected_statuses, issue.new_statuses_allowed_to(admin)
396 396 end
397 397
398 def test_new_statuses_allowed_to_should_return_default_and_current_status_when_copying
399 issue = Issue.find(1).copy
400 assert_equal [1], issue.new_statuses_allowed_to(User.find(2)).map(&:id)
401
402 issue = Issue.find(2).copy
403 assert_equal [1, 2], issue.new_statuses_allowed_to(User.find(2)).map(&:id)
404 end
405
398 406 def test_copy
399 407 issue = Issue.new.copy_from(1)
400 408 assert issue.copy?
401 409 assert issue.save
402 410 issue.reload
403 411 orig = Issue.find(1)
404 412 assert_equal orig.subject, issue.subject
405 413 assert_equal orig.tracker, issue.tracker
406 414 assert_equal "125", issue.custom_value_for(2).value
407 415 end
408 416
409 417 def test_copy_should_copy_status
410 418 orig = Issue.find(8)
411 419 assert orig.status != IssueStatus.default
412 420
413 421 issue = Issue.new.copy_from(orig)
414 422 assert issue.save
415 423 issue.reload
416 424 assert_equal orig.status, issue.status
417 425 end
418 426
419 427 def test_should_not_call_after_project_change_on_creation
420 428 issue = Issue.new(:project_id => 1, :tracker_id => 1, :status_id => 1, :subject => 'Test', :author_id => 1)
421 429 issue.expects(:after_project_change).never
422 430 issue.save!
423 431 end
424 432
425 433 def test_should_not_call_after_project_change_on_update
426 434 issue = Issue.find(1)
427 435 issue.project = Project.find(1)
428 436 issue.subject = 'No project change'
429 437 issue.expects(:after_project_change).never
430 438 issue.save!
431 439 end
432 440
433 441 def test_should_call_after_project_change_on_project_change
434 442 issue = Issue.find(1)
435 443 issue.project = Project.find(2)
436 444 issue.expects(:after_project_change).once
437 445 issue.save!
438 446 end
439 447
440 448 def test_should_close_duplicates
441 449 # Create 3 issues
442 450 project = Project.find(1)
443 451 issue1 = Issue.generate_for_project!(project)
444 452 issue2 = Issue.generate_for_project!(project)
445 453 issue3 = Issue.generate_for_project!(project)
446 454
447 455 # 2 is a dupe of 1
448 456 IssueRelation.create!(:issue_from => issue2, :issue_to => issue1, :relation_type => IssueRelation::TYPE_DUPLICATES)
449 457 # And 3 is a dupe of 2
450 458 IssueRelation.create!(:issue_from => issue3, :issue_to => issue2, :relation_type => IssueRelation::TYPE_DUPLICATES)
451 459 # And 3 is a dupe of 1 (circular duplicates)
452 460 IssueRelation.create!(:issue_from => issue3, :issue_to => issue1, :relation_type => IssueRelation::TYPE_DUPLICATES)
453 461
454 462 assert issue1.reload.duplicates.include?(issue2)
455 463
456 464 # Closing issue 1
457 465 issue1.init_journal(User.find(:first), "Closing issue1")
458 466 issue1.status = IssueStatus.find :first, :conditions => {:is_closed => true}
459 467 assert issue1.save
460 468 # 2 and 3 should be also closed
461 469 assert issue2.reload.closed?
462 470 assert issue3.reload.closed?
463 471 end
464 472
465 473 def test_should_not_close_duplicated_issue
466 474 project = Project.find(1)
467 475 issue1 = Issue.generate_for_project!(project)
468 476 issue2 = Issue.generate_for_project!(project)
469 477
470 478 # 2 is a dupe of 1
471 479 IssueRelation.create(:issue_from => issue2, :issue_to => issue1, :relation_type => IssueRelation::TYPE_DUPLICATES)
472 480 # 2 is a dup of 1 but 1 is not a duplicate of 2
473 481 assert !issue2.reload.duplicates.include?(issue1)
474 482
475 483 # Closing issue 2
476 484 issue2.init_journal(User.find(:first), "Closing issue2")
477 485 issue2.status = IssueStatus.find :first, :conditions => {:is_closed => true}
478 486 assert issue2.save
479 487 # 1 should not be also closed
480 488 assert !issue1.reload.closed?
481 489 end
482 490
483 491 def test_assignable_versions
484 492 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :fixed_version_id => 1, :subject => 'New issue')
485 493 assert_equal ['open'], issue.assignable_versions.collect(&:status).uniq
486 494 end
487 495
488 496 def test_should_not_be_able_to_assign_a_new_issue_to_a_closed_version
489 497 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :fixed_version_id => 1, :subject => 'New issue')
490 498 assert !issue.save
491 499 assert_not_nil issue.errors[:fixed_version_id]
492 500 end
493 501
494 502 def test_should_not_be_able_to_assign_a_new_issue_to_a_locked_version
495 503 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :fixed_version_id => 2, :subject => 'New issue')
496 504 assert !issue.save
497 505 assert_not_nil issue.errors[:fixed_version_id]
498 506 end
499 507
500 508 def test_should_be_able_to_assign_a_new_issue_to_an_open_version
501 509 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :fixed_version_id => 3, :subject => 'New issue')
502 510 assert issue.save
503 511 end
504 512
505 513 def test_should_be_able_to_update_an_issue_assigned_to_a_closed_version
506 514 issue = Issue.find(11)
507 515 assert_equal 'closed', issue.fixed_version.status
508 516 issue.subject = 'Subject changed'
509 517 assert issue.save
510 518 end
511 519
512 520 def test_should_not_be_able_to_reopen_an_issue_assigned_to_a_closed_version
513 521 issue = Issue.find(11)
514 522 issue.status_id = 1
515 523 assert !issue.save
516 524 assert_not_nil issue.errors[:base]
517 525 end
518 526
519 527 def test_should_be_able_to_reopen_and_reassign_an_issue_assigned_to_a_closed_version
520 528 issue = Issue.find(11)
521 529 issue.status_id = 1
522 530 issue.fixed_version_id = 3
523 531 assert issue.save
524 532 end
525 533
526 534 def test_should_be_able_to_reopen_an_issue_assigned_to_a_locked_version
527 535 issue = Issue.find(12)
528 536 assert_equal 'locked', issue.fixed_version.status
529 537 issue.status_id = 1
530 538 assert issue.save
531 539 end
532 540
533 541 def test_allowed_target_projects_on_move_should_include_projects_with_issue_tracking_enabled
534 542 assert_include Project.find(2), Issue.allowed_target_projects_on_move(User.find(2))
535 543 end
536 544
537 545 def test_allowed_target_projects_on_move_should_not_include_projects_with_issue_tracking_disabled
538 546 Project.find(2).disable_module! :issue_tracking
539 547 assert_not_include Project.find(2), Issue.allowed_target_projects_on_move(User.find(2))
540 548 end
541 549
542 550 def test_move_to_another_project_with_same_category
543 551 issue = Issue.find(1)
544 552 issue.project = Project.find(2)
545 553 assert issue.save
546 554 issue.reload
547 555 assert_equal 2, issue.project_id
548 556 # Category changes
549 557 assert_equal 4, issue.category_id
550 558 # Make sure time entries were move to the target project
551 559 assert_equal 2, issue.time_entries.first.project_id
552 560 end
553 561
554 562 def test_move_to_another_project_without_same_category
555 563 issue = Issue.find(2)
556 564 issue.project = Project.find(2)
557 565 assert issue.save
558 566 issue.reload
559 567 assert_equal 2, issue.project_id
560 568 # Category cleared
561 569 assert_nil issue.category_id
562 570 end
563 571
564 572 def test_move_to_another_project_should_clear_fixed_version_when_not_shared
565 573 issue = Issue.find(1)
566 574 issue.update_attribute(:fixed_version_id, 1)
567 575 issue.project = Project.find(2)
568 576 assert issue.save
569 577 issue.reload
570 578 assert_equal 2, issue.project_id
571 579 # Cleared fixed_version
572 580 assert_equal nil, issue.fixed_version
573 581 end
574 582
575 583 def test_move_to_another_project_should_keep_fixed_version_when_shared_with_the_target_project
576 584 issue = Issue.find(1)
577 585 issue.update_attribute(:fixed_version_id, 4)
578 586 issue.project = Project.find(5)
579 587 assert issue.save
580 588 issue.reload
581 589 assert_equal 5, issue.project_id
582 590 # Keep fixed_version
583 591 assert_equal 4, issue.fixed_version_id
584 592 end
585 593
586 594 def test_move_to_another_project_should_clear_fixed_version_when_not_shared_with_the_target_project
587 595 issue = Issue.find(1)
588 596 issue.update_attribute(:fixed_version_id, 1)
589 597 issue.project = Project.find(5)
590 598 assert issue.save
591 599 issue.reload
592 600 assert_equal 5, issue.project_id
593 601 # Cleared fixed_version
594 602 assert_equal nil, issue.fixed_version
595 603 end
596 604
597 605 def test_move_to_another_project_should_keep_fixed_version_when_shared_systemwide
598 606 issue = Issue.find(1)
599 607 issue.update_attribute(:fixed_version_id, 7)
600 608 issue.project = Project.find(2)
601 609 assert issue.save
602 610 issue.reload
603 611 assert_equal 2, issue.project_id
604 612 # Keep fixed_version
605 613 assert_equal 7, issue.fixed_version_id
606 614 end
607 615
608 616 def test_move_to_another_project_with_disabled_tracker
609 617 issue = Issue.find(1)
610 618 target = Project.find(2)
611 619 target.tracker_ids = [3]
612 620 target.save
613 621 issue.project = target
614 622 assert issue.save
615 623 issue.reload
616 624 assert_equal 2, issue.project_id
617 625 assert_equal 3, issue.tracker_id
618 626 end
619 627
620 628 def test_copy_to_the_same_project
621 629 issue = Issue.find(1)
622 630 copy = issue.copy
623 631 assert_difference 'Issue.count' do
624 632 copy.save!
625 633 end
626 634 assert_kind_of Issue, copy
627 635 assert_equal issue.project, copy.project
628 636 assert_equal "125", copy.custom_value_for(2).value
629 637 end
630 638
631 639 def test_copy_to_another_project_and_tracker
632 640 issue = Issue.find(1)
633 641 copy = issue.copy(:project_id => 3, :tracker_id => 2)
634 642 assert_difference 'Issue.count' do
635 643 copy.save!
636 644 end
637 645 copy.reload
638 646 assert_kind_of Issue, copy
639 647 assert_equal Project.find(3), copy.project
640 648 assert_equal Tracker.find(2), copy.tracker
641 649 # Custom field #2 is not associated with target tracker
642 650 assert_nil copy.custom_value_for(2)
643 651 end
644 652
645 653 context "#copy" do
646 654 setup do
647 655 @issue = Issue.find(1)
648 656 end
649 657
650 658 should "not create a journal" do
651 659 copy = @issue.copy(:project_id => 3, :tracker_id => 2, :assigned_to_id => 3)
652 660 copy.save!
653 661 assert_equal 0, copy.reload.journals.size
654 662 end
655 663
656 664 should "allow assigned_to changes" do
657 665 copy = @issue.copy(:project_id => 3, :tracker_id => 2, :assigned_to_id => 3)
658 666 assert_equal 3, copy.assigned_to_id
659 667 end
660 668
661 669 should "allow status changes" do
662 670 copy = @issue.copy(:project_id => 3, :tracker_id => 2, :status_id => 2)
663 671 assert_equal 2, copy.status_id
664 672 end
665 673
666 674 should "allow start date changes" do
667 675 date = Date.today
668 676 copy = @issue.copy(:project_id => 3, :tracker_id => 2, :start_date => date)
669 677 assert_equal date, copy.start_date
670 678 end
671 679
672 680 should "allow due date changes" do
673 681 date = Date.today
674 682 copy = @issue.copy(:project_id => 3, :tracker_id => 2, :due_date => date)
675 683 assert_equal date, copy.due_date
676 684 end
677 685
678 686 should "set current user as author" do
679 687 User.current = User.find(9)
680 688 copy = @issue.copy(:project_id => 3, :tracker_id => 2)
681 689 assert_equal User.current, copy.author
682 690 end
683 691
684 692 should "create a journal with notes" do
685 693 date = Date.today
686 694 notes = "Notes added when copying"
687 695 copy = @issue.copy(:project_id => 3, :tracker_id => 2, :start_date => date)
688 696 copy.init_journal(User.current, notes)
689 697 copy.save!
690 698
691 699 assert_equal 1, copy.journals.size
692 700 journal = copy.journals.first
693 701 assert_equal 0, journal.details.size
694 702 assert_equal notes, journal.notes
695 703 end
696 704 end
697 705
698 706 def test_recipients_should_include_previous_assignee
699 707 user = User.find(3)
700 708 user.members.update_all ["mail_notification = ?", false]
701 709 user.update_attribute :mail_notification, 'only_assigned'
702 710
703 711 issue = Issue.find(2)
704 712 issue.assigned_to = nil
705 713 assert_include user.mail, issue.recipients
706 714 issue.save!
707 715 assert !issue.recipients.include?(user.mail)
708 716 end
709 717
710 718 def test_recipients_should_not_include_users_that_cannot_view_the_issue
711 719 issue = Issue.find(12)
712 720 assert issue.recipients.include?(issue.author.mail)
713 721 # copy the issue to a private project
714 722 copy = issue.copy(:project_id => 5, :tracker_id => 2)
715 723 # author is not a member of project anymore
716 724 assert !copy.recipients.include?(copy.author.mail)
717 725 end
718 726
719 727 def test_recipients_should_include_the_assigned_group_members
720 728 group_member = User.generate_with_protected!
721 729 group = Group.generate!
722 730 group.users << group_member
723 731
724 732 issue = Issue.find(12)
725 733 issue.assigned_to = group
726 734 assert issue.recipients.include?(group_member.mail)
727 735 end
728 736
729 737 def test_watcher_recipients_should_not_include_users_that_cannot_view_the_issue
730 738 user = User.find(3)
731 739 issue = Issue.find(9)
732 740 Watcher.create!(:user => user, :watchable => issue)
733 741 assert issue.watched_by?(user)
734 742 assert !issue.watcher_recipients.include?(user.mail)
735 743 end
736 744
737 745 def test_issue_destroy
738 746 Issue.find(1).destroy
739 747 assert_nil Issue.find_by_id(1)
740 748 assert_nil TimeEntry.find_by_issue_id(1)
741 749 end
742 750
743 751 def test_blocked
744 752 blocked_issue = Issue.find(9)
745 753 blocking_issue = Issue.find(10)
746 754
747 755 assert blocked_issue.blocked?
748 756 assert !blocking_issue.blocked?
749 757 end
750 758
751 759 def test_blocked_issues_dont_allow_closed_statuses
752 760 blocked_issue = Issue.find(9)
753 761
754 762 allowed_statuses = blocked_issue.new_statuses_allowed_to(users(:users_002))
755 763 assert !allowed_statuses.empty?
756 764 closed_statuses = allowed_statuses.select {|st| st.is_closed?}
757 765 assert closed_statuses.empty?
758 766 end
759 767
760 768 def test_unblocked_issues_allow_closed_statuses
761 769 blocking_issue = Issue.find(10)
762 770
763 771 allowed_statuses = blocking_issue.new_statuses_allowed_to(users(:users_002))
764 772 assert !allowed_statuses.empty?
765 773 closed_statuses = allowed_statuses.select {|st| st.is_closed?}
766 774 assert !closed_statuses.empty?
767 775 end
768 776
769 777 def test_rescheduling_an_issue_should_reschedule_following_issue
770 778 issue1 = Issue.create!(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :subject => '-', :start_date => Date.today, :due_date => Date.today + 2)
771 779 issue2 = Issue.create!(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :subject => '-', :start_date => Date.today, :due_date => Date.today + 2)
772 780 IssueRelation.create!(:issue_from => issue1, :issue_to => issue2, :relation_type => IssueRelation::TYPE_PRECEDES)
773 781 assert_equal issue1.due_date + 1, issue2.reload.start_date
774 782
775 783 issue1.due_date = Date.today + 5
776 784 issue1.save!
777 785 assert_equal issue1.due_date + 1, issue2.reload.start_date
778 786 end
779 787
780 788 def test_rescheduling_a_stale_issue_should_not_raise_an_error
781 789 stale = Issue.find(1)
782 790 issue = Issue.find(1)
783 791 issue.subject = "Updated"
784 792 issue.save!
785 793
786 794 date = 10.days.from_now.to_date
787 795 assert_nothing_raised do
788 796 stale.reschedule_after(date)
789 797 end
790 798 assert_equal date, stale.reload.start_date
791 799 end
792 800
793 801 def test_overdue
794 802 assert Issue.new(:due_date => 1.day.ago.to_date).overdue?
795 803 assert !Issue.new(:due_date => Date.today).overdue?
796 804 assert !Issue.new(:due_date => 1.day.from_now.to_date).overdue?
797 805 assert !Issue.new(:due_date => nil).overdue?
798 806 assert !Issue.new(:due_date => 1.day.ago.to_date, :status => IssueStatus.find(:first, :conditions => {:is_closed => true})).overdue?
799 807 end
800 808
801 809 context "#behind_schedule?" do
802 810 should "be false if the issue has no start_date" do
803 811 assert !Issue.new(:start_date => nil, :due_date => 1.day.from_now.to_date, :done_ratio => 0).behind_schedule?
804 812 end
805 813
806 814 should "be false if the issue has no end_date" do
807 815 assert !Issue.new(:start_date => 1.day.from_now.to_date, :due_date => nil, :done_ratio => 0).behind_schedule?
808 816 end
809 817
810 818 should "be false if the issue has more done than it's calendar time" do
811 819 assert !Issue.new(:start_date => 50.days.ago.to_date, :due_date => 50.days.from_now.to_date, :done_ratio => 90).behind_schedule?
812 820 end
813 821
814 822 should "be true if the issue hasn't been started at all" do
815 823 assert Issue.new(:start_date => 1.day.ago.to_date, :due_date => 1.day.from_now.to_date, :done_ratio => 0).behind_schedule?
816 824 end
817 825
818 826 should "be true if the issue has used more calendar time than it's done ratio" do
819 827 assert Issue.new(:start_date => 100.days.ago.to_date, :due_date => Date.today, :done_ratio => 90).behind_schedule?
820 828 end
821 829 end
822 830
823 831 context "#assignable_users" do
824 832 should "be Users" do
825 833 assert_kind_of User, Issue.find(1).assignable_users.first
826 834 end
827 835
828 836 should "include the issue author" do
829 837 project = Project.find(1)
830 838 non_project_member = User.generate!
831 839 issue = Issue.generate_for_project!(project, :author => non_project_member)
832 840
833 841 assert issue.assignable_users.include?(non_project_member)
834 842 end
835 843
836 844 should "include the current assignee" do
837 845 project = Project.find(1)
838 846 user = User.generate!
839 847 issue = Issue.generate_for_project!(project, :assigned_to => user)
840 848 user.lock!
841 849
842 850 assert Issue.find(issue.id).assignable_users.include?(user)
843 851 end
844 852
845 853 should "not show the issue author twice" do
846 854 assignable_user_ids = Issue.find(1).assignable_users.collect(&:id)
847 855 assert_equal 2, assignable_user_ids.length
848 856
849 857 assignable_user_ids.each do |user_id|
850 858 assert_equal 1, assignable_user_ids.select {|i| i == user_id}.length, "User #{user_id} appears more or less than once"
851 859 end
852 860 end
853 861
854 862 context "with issue_group_assignment" do
855 863 should "include groups" do
856 864 issue = Issue.new(:project => Project.find(2))
857 865
858 866 with_settings :issue_group_assignment => '1' do
859 867 assert_equal %w(Group User), issue.assignable_users.map {|a| a.class.name}.uniq.sort
860 868 assert issue.assignable_users.include?(Group.find(11))
861 869 end
862 870 end
863 871 end
864 872
865 873 context "without issue_group_assignment" do
866 874 should "not include groups" do
867 875 issue = Issue.new(:project => Project.find(2))
868 876
869 877 with_settings :issue_group_assignment => '0' do
870 878 assert_equal %w(User), issue.assignable_users.map {|a| a.class.name}.uniq.sort
871 879 assert !issue.assignable_users.include?(Group.find(11))
872 880 end
873 881 end
874 882 end
875 883 end
876 884
877 885 def test_create_should_send_email_notification
878 886 ActionMailer::Base.deliveries.clear
879 887 issue = Issue.new(:project_id => 1, :tracker_id => 1,
880 888 :author_id => 3, :status_id => 1,
881 889 :priority => IssuePriority.all.first,
882 890 :subject => 'test_create', :estimated_hours => '1:30')
883 891
884 892 assert issue.save
885 893 assert_equal 1, ActionMailer::Base.deliveries.size
886 894 end
887 895
888 896 def test_stale_issue_should_not_send_email_notification
889 897 ActionMailer::Base.deliveries.clear
890 898 issue = Issue.find(1)
891 899 stale = Issue.find(1)
892 900
893 901 issue.init_journal(User.find(1))
894 902 issue.subject = 'Subjet update'
895 903 assert issue.save
896 904 assert_equal 1, ActionMailer::Base.deliveries.size
897 905 ActionMailer::Base.deliveries.clear
898 906
899 907 stale.init_journal(User.find(1))
900 908 stale.subject = 'Another subjet update'
901 909 assert_raise ActiveRecord::StaleObjectError do
902 910 stale.save
903 911 end
904 912 assert ActionMailer::Base.deliveries.empty?
905 913 end
906 914
907 915 def test_journalized_description
908 916 IssueCustomField.delete_all
909 917
910 918 i = Issue.first
911 919 old_description = i.description
912 920 new_description = "This is the new description"
913 921
914 922 i.init_journal(User.find(2))
915 923 i.description = new_description
916 924 assert_difference 'Journal.count', 1 do
917 925 assert_difference 'JournalDetail.count', 1 do
918 926 i.save!
919 927 end
920 928 end
921 929
922 930 detail = JournalDetail.first(:order => 'id DESC')
923 931 assert_equal i, detail.journal.journalized
924 932 assert_equal 'attr', detail.property
925 933 assert_equal 'description', detail.prop_key
926 934 assert_equal old_description, detail.old_value
927 935 assert_equal new_description, detail.value
928 936 end
929 937
930 938 def test_blank_descriptions_should_not_be_journalized
931 939 IssueCustomField.delete_all
932 940 Issue.update_all("description = NULL", "id=1")
933 941
934 942 i = Issue.find(1)
935 943 i.init_journal(User.find(2))
936 944 i.subject = "blank description"
937 945 i.description = "\r\n"
938 946
939 947 assert_difference 'Journal.count', 1 do
940 948 assert_difference 'JournalDetail.count', 1 do
941 949 i.save!
942 950 end
943 951 end
944 952 end
945 953
946 954 def test_journalized_multi_custom_field
947 955 field = IssueCustomField.create!(:name => 'filter', :field_format => 'list', :is_filter => true, :is_for_all => true,
948 956 :tracker_ids => [1], :possible_values => ['value1', 'value2', 'value3'], :multiple => true)
949 957
950 958 issue = Issue.create!(:project_id => 1, :tracker_id => 1, :subject => 'Test', :author_id => 1)
951 959
952 960 assert_difference 'Journal.count' do
953 961 assert_difference 'JournalDetail.count' do
954 962 issue.init_journal(User.first)
955 963 issue.custom_field_values = {field.id => ['value1']}
956 964 issue.save!
957 965 end
958 966 assert_difference 'JournalDetail.count' do
959 967 issue.init_journal(User.first)
960 968 issue.custom_field_values = {field.id => ['value1', 'value2']}
961 969 issue.save!
962 970 end
963 971 assert_difference 'JournalDetail.count', 2 do
964 972 issue.init_journal(User.first)
965 973 issue.custom_field_values = {field.id => ['value3', 'value2']}
966 974 issue.save!
967 975 end
968 976 assert_difference 'JournalDetail.count', 2 do
969 977 issue.init_journal(User.first)
970 978 issue.custom_field_values = {field.id => nil}
971 979 issue.save!
972 980 end
973 981 end
974 982 end
975 983
976 984 def test_description_eol_should_be_normalized
977 985 i = Issue.new(:description => "CR \r LF \n CRLF \r\n")
978 986 assert_equal "CR \r\n LF \r\n CRLF \r\n", i.description
979 987 end
980 988
981 989 def test_saving_twice_should_not_duplicate_journal_details
982 990 i = Issue.find(:first)
983 991 i.init_journal(User.find(2), 'Some notes')
984 992 # initial changes
985 993 i.subject = 'New subject'
986 994 i.done_ratio = i.done_ratio + 10
987 995 assert_difference 'Journal.count' do
988 996 assert i.save
989 997 end
990 998 # 1 more change
991 999 i.priority = IssuePriority.find(:first, :conditions => ["id <> ?", i.priority_id])
992 1000 assert_no_difference 'Journal.count' do
993 1001 assert_difference 'JournalDetail.count', 1 do
994 1002 i.save
995 1003 end
996 1004 end
997 1005 # no more change
998 1006 assert_no_difference 'Journal.count' do
999 1007 assert_no_difference 'JournalDetail.count' do
1000 1008 i.save
1001 1009 end
1002 1010 end
1003 1011 end
1004 1012
1005 1013 def test_all_dependent_issues
1006 1014 IssueRelation.delete_all
1007 1015 assert IssueRelation.create!(:issue_from => Issue.find(1),
1008 1016 :issue_to => Issue.find(2),
1009 1017 :relation_type => IssueRelation::TYPE_PRECEDES)
1010 1018 assert IssueRelation.create!(:issue_from => Issue.find(2),
1011 1019 :issue_to => Issue.find(3),
1012 1020 :relation_type => IssueRelation::TYPE_PRECEDES)
1013 1021 assert IssueRelation.create!(:issue_from => Issue.find(3),
1014 1022 :issue_to => Issue.find(8),
1015 1023 :relation_type => IssueRelation::TYPE_PRECEDES)
1016 1024
1017 1025 assert_equal [2, 3, 8], Issue.find(1).all_dependent_issues.collect(&:id).sort
1018 1026 end
1019 1027
1020 1028 def test_all_dependent_issues_with_persistent_circular_dependency
1021 1029 IssueRelation.delete_all
1022 1030 assert IssueRelation.create!(:issue_from => Issue.find(1),
1023 1031 :issue_to => Issue.find(2),
1024 1032 :relation_type => IssueRelation::TYPE_PRECEDES)
1025 1033 assert IssueRelation.create!(:issue_from => Issue.find(2),
1026 1034 :issue_to => Issue.find(3),
1027 1035 :relation_type => IssueRelation::TYPE_PRECEDES)
1028 1036 # Validation skipping
1029 1037 assert IssueRelation.new(:issue_from => Issue.find(3),
1030 1038 :issue_to => Issue.find(1),
1031 1039 :relation_type => IssueRelation::TYPE_PRECEDES).save(false)
1032 1040
1033 1041 assert_equal [2, 3], Issue.find(1).all_dependent_issues.collect(&:id).sort
1034 1042 end
1035 1043
1036 1044 def test_all_dependent_issues_with_persistent_multiple_circular_dependencies
1037 1045 IssueRelation.delete_all
1038 1046 assert IssueRelation.create!(:issue_from => Issue.find(1),
1039 1047 :issue_to => Issue.find(2),
1040 1048 :relation_type => IssueRelation::TYPE_RELATES)
1041 1049 assert IssueRelation.create!(:issue_from => Issue.find(2),
1042 1050 :issue_to => Issue.find(3),
1043 1051 :relation_type => IssueRelation::TYPE_RELATES)
1044 1052 assert IssueRelation.create!(:issue_from => Issue.find(3),
1045 1053 :issue_to => Issue.find(8),
1046 1054 :relation_type => IssueRelation::TYPE_RELATES)
1047 1055 # Validation skipping
1048 1056 assert IssueRelation.new(:issue_from => Issue.find(8),
1049 1057 :issue_to => Issue.find(2),
1050 1058 :relation_type => IssueRelation::TYPE_RELATES).save(false)
1051 1059 assert IssueRelation.new(:issue_from => Issue.find(3),
1052 1060 :issue_to => Issue.find(1),
1053 1061 :relation_type => IssueRelation::TYPE_RELATES).save(false)
1054 1062
1055 1063 assert_equal [2, 3, 8], Issue.find(1).all_dependent_issues.collect(&:id).sort
1056 1064 end
1057 1065
1058 1066 context "#done_ratio" do
1059 1067 setup do
1060 1068 @issue = Issue.find(1)
1061 1069 @issue_status = IssueStatus.find(1)
1062 1070 @issue_status.update_attribute(:default_done_ratio, 50)
1063 1071 @issue2 = Issue.find(2)
1064 1072 @issue_status2 = IssueStatus.find(2)
1065 1073 @issue_status2.update_attribute(:default_done_ratio, 0)
1066 1074 end
1067 1075
1068 1076 teardown do
1069 1077 Setting.issue_done_ratio = 'issue_field'
1070 1078 end
1071 1079
1072 1080 context "with Setting.issue_done_ratio using the issue_field" do
1073 1081 setup do
1074 1082 Setting.issue_done_ratio = 'issue_field'
1075 1083 end
1076 1084
1077 1085 should "read the issue's field" do
1078 1086 assert_equal 0, @issue.done_ratio
1079 1087 assert_equal 30, @issue2.done_ratio
1080 1088 end
1081 1089 end
1082 1090
1083 1091 context "with Setting.issue_done_ratio using the issue_status" do
1084 1092 setup do
1085 1093 Setting.issue_done_ratio = 'issue_status'
1086 1094 end
1087 1095
1088 1096 should "read the Issue Status's default done ratio" do
1089 1097 assert_equal 50, @issue.done_ratio
1090 1098 assert_equal 0, @issue2.done_ratio
1091 1099 end
1092 1100 end
1093 1101 end
1094 1102
1095 1103 context "#update_done_ratio_from_issue_status" do
1096 1104 setup do
1097 1105 @issue = Issue.find(1)
1098 1106 @issue_status = IssueStatus.find(1)
1099 1107 @issue_status.update_attribute(:default_done_ratio, 50)
1100 1108 @issue2 = Issue.find(2)
1101 1109 @issue_status2 = IssueStatus.find(2)
1102 1110 @issue_status2.update_attribute(:default_done_ratio, 0)
1103 1111 end
1104 1112
1105 1113 context "with Setting.issue_done_ratio using the issue_field" do
1106 1114 setup do
1107 1115 Setting.issue_done_ratio = 'issue_field'
1108 1116 end
1109 1117
1110 1118 should "not change the issue" do
1111 1119 @issue.update_done_ratio_from_issue_status
1112 1120 @issue2.update_done_ratio_from_issue_status
1113 1121
1114 1122 assert_equal 0, @issue.read_attribute(:done_ratio)
1115 1123 assert_equal 30, @issue2.read_attribute(:done_ratio)
1116 1124 end
1117 1125 end
1118 1126
1119 1127 context "with Setting.issue_done_ratio using the issue_status" do
1120 1128 setup do
1121 1129 Setting.issue_done_ratio = 'issue_status'
1122 1130 end
1123 1131
1124 1132 should "change the issue's done ratio" do
1125 1133 @issue.update_done_ratio_from_issue_status
1126 1134 @issue2.update_done_ratio_from_issue_status
1127 1135
1128 1136 assert_equal 50, @issue.read_attribute(:done_ratio)
1129 1137 assert_equal 0, @issue2.read_attribute(:done_ratio)
1130 1138 end
1131 1139 end
1132 1140 end
1133 1141
1134 1142 test "#by_tracker" do
1135 1143 User.current = User.anonymous
1136 1144 groups = Issue.by_tracker(Project.find(1))
1137 1145 assert_equal 3, groups.size
1138 1146 assert_equal 7, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1139 1147 end
1140 1148
1141 1149 test "#by_version" do
1142 1150 User.current = User.anonymous
1143 1151 groups = Issue.by_version(Project.find(1))
1144 1152 assert_equal 3, groups.size
1145 1153 assert_equal 3, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1146 1154 end
1147 1155
1148 1156 test "#by_priority" do
1149 1157 User.current = User.anonymous
1150 1158 groups = Issue.by_priority(Project.find(1))
1151 1159 assert_equal 4, groups.size
1152 1160 assert_equal 7, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1153 1161 end
1154 1162
1155 1163 test "#by_category" do
1156 1164 User.current = User.anonymous
1157 1165 groups = Issue.by_category(Project.find(1))
1158 1166 assert_equal 2, groups.size
1159 1167 assert_equal 3, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1160 1168 end
1161 1169
1162 1170 test "#by_assigned_to" do
1163 1171 User.current = User.anonymous
1164 1172 groups = Issue.by_assigned_to(Project.find(1))
1165 1173 assert_equal 2, groups.size
1166 1174 assert_equal 2, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1167 1175 end
1168 1176
1169 1177 test "#by_author" do
1170 1178 User.current = User.anonymous
1171 1179 groups = Issue.by_author(Project.find(1))
1172 1180 assert_equal 4, groups.size
1173 1181 assert_equal 7, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1174 1182 end
1175 1183
1176 1184 test "#by_subproject" do
1177 1185 User.current = User.anonymous
1178 1186 groups = Issue.by_subproject(Project.find(1))
1179 1187 # Private descendant not visible
1180 1188 assert_equal 1, groups.size
1181 1189 assert_equal 2, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1182 1190 end
1183 1191
1184 1192 def test_recently_updated_with_limit_scopes
1185 1193 #should return the last updated issue
1186 1194 assert_equal 1, Issue.recently_updated.with_limit(1).length
1187 1195 assert_equal Issue.find(:first, :order => "updated_on DESC"), Issue.recently_updated.with_limit(1).first
1188 1196 end
1189 1197
1190 1198 def test_on_active_projects_scope
1191 1199 assert Project.find(2).archive
1192 1200
1193 1201 before = Issue.on_active_project.length
1194 1202 # test inclusion to results
1195 1203 issue = Issue.generate_for_project!(Project.find(1), :tracker => Project.find(2).trackers.first)
1196 1204 assert_equal before + 1, Issue.on_active_project.length
1197 1205
1198 1206 # Move to an archived project
1199 1207 issue.project = Project.find(2)
1200 1208 assert issue.save
1201 1209 assert_equal before, Issue.on_active_project.length
1202 1210 end
1203 1211
1204 1212 context "Issue#recipients" do
1205 1213 setup do
1206 1214 @project = Project.find(1)
1207 1215 @author = User.generate_with_protected!
1208 1216 @assignee = User.generate_with_protected!
1209 1217 @issue = Issue.generate_for_project!(@project, :assigned_to => @assignee, :author => @author)
1210 1218 end
1211 1219
1212 1220 should "include project recipients" do
1213 1221 assert @project.recipients.present?
1214 1222 @project.recipients.each do |project_recipient|
1215 1223 assert @issue.recipients.include?(project_recipient)
1216 1224 end
1217 1225 end
1218 1226
1219 1227 should "include the author if the author is active" do
1220 1228 assert @issue.author, "No author set for Issue"
1221 1229 assert @issue.recipients.include?(@issue.author.mail)
1222 1230 end
1223 1231
1224 1232 should "include the assigned to user if the assigned to user is active" do
1225 1233 assert @issue.assigned_to, "No assigned_to set for Issue"
1226 1234 assert @issue.recipients.include?(@issue.assigned_to.mail)
1227 1235 end
1228 1236
1229 1237 should "not include users who opt out of all email" do
1230 1238 @author.update_attribute(:mail_notification, :none)
1231 1239
1232 1240 assert !@issue.recipients.include?(@issue.author.mail)
1233 1241 end
1234 1242
1235 1243 should "not include the issue author if they are only notified of assigned issues" do
1236 1244 @author.update_attribute(:mail_notification, :only_assigned)
1237 1245
1238 1246 assert !@issue.recipients.include?(@issue.author.mail)
1239 1247 end
1240 1248
1241 1249 should "not include the assigned user if they are only notified of owned issues" do
1242 1250 @assignee.update_attribute(:mail_notification, :only_owner)
1243 1251
1244 1252 assert !@issue.recipients.include?(@issue.assigned_to.mail)
1245 1253 end
1246 1254 end
1247 1255
1248 1256 def test_last_journal_id_with_journals_should_return_the_journal_id
1249 1257 assert_equal 2, Issue.find(1).last_journal_id
1250 1258 end
1251 1259
1252 1260 def test_last_journal_id_without_journals_should_return_nil
1253 1261 assert_nil Issue.find(3).last_journal_id
1254 1262 end
1255 1263 end
General Comments 0
You need to be logged in to leave comments. Login now