##// END OF EJS Templates
Negative estimated hours should not be valid (#12735)....
Jean-Philippe Lang -
r10895:45c93340b78d
parent child
Show More
@@ -1,1395 +1,1395
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 class Issue < ActiveRecord::Base
19 19 include Redmine::SafeAttributes
20 20 include Redmine::Utils::DateCalculation
21 21
22 22 belongs_to :project
23 23 belongs_to :tracker
24 24 belongs_to :status, :class_name => 'IssueStatus', :foreign_key => 'status_id'
25 25 belongs_to :author, :class_name => 'User', :foreign_key => 'author_id'
26 26 belongs_to :assigned_to, :class_name => 'Principal', :foreign_key => 'assigned_to_id'
27 27 belongs_to :fixed_version, :class_name => 'Version', :foreign_key => 'fixed_version_id'
28 28 belongs_to :priority, :class_name => 'IssuePriority', :foreign_key => 'priority_id'
29 29 belongs_to :category, :class_name => 'IssueCategory', :foreign_key => 'category_id'
30 30
31 31 has_many :journals, :as => :journalized, :dependent => :destroy
32 32 has_many :visible_journals,
33 33 :class_name => 'Journal',
34 34 :as => :journalized,
35 35 :conditions => Proc.new {
36 36 ["(#{Journal.table_name}.private_notes = ? OR (#{Project.allowed_to_condition(User.current, :view_private_notes)}))", false]
37 37 },
38 38 :readonly => true
39 39
40 40 has_many :time_entries, :dependent => :delete_all
41 41 has_and_belongs_to_many :changesets, :order => "#{Changeset.table_name}.committed_on ASC, #{Changeset.table_name}.id ASC"
42 42
43 43 has_many :relations_from, :class_name => 'IssueRelation', :foreign_key => 'issue_from_id', :dependent => :delete_all
44 44 has_many :relations_to, :class_name => 'IssueRelation', :foreign_key => 'issue_to_id', :dependent => :delete_all
45 45
46 46 acts_as_nested_set :scope => 'root_id', :dependent => :destroy
47 47 acts_as_attachable :after_add => :attachment_added, :after_remove => :attachment_removed
48 48 acts_as_customizable
49 49 acts_as_watchable
50 50 acts_as_searchable :columns => ['subject', "#{table_name}.description", "#{Journal.table_name}.notes"],
51 51 :include => [:project, :visible_journals],
52 52 # sort by id so that limited eager loading doesn't break with postgresql
53 53 :order_column => "#{table_name}.id"
54 54 acts_as_event :title => Proc.new {|o| "#{o.tracker.name} ##{o.id} (#{o.status}): #{o.subject}"},
55 55 :url => Proc.new {|o| {:controller => 'issues', :action => 'show', :id => o.id}},
56 56 :type => Proc.new {|o| 'issue' + (o.closed? ? ' closed' : '') }
57 57
58 58 acts_as_activity_provider :find_options => {:include => [:project, :author, :tracker]},
59 59 :author_key => :author_id
60 60
61 61 DONE_RATIO_OPTIONS = %w(issue_field issue_status)
62 62
63 63 attr_reader :current_journal
64 64 delegate :notes, :notes=, :private_notes, :private_notes=, :to => :current_journal, :allow_nil => true
65 65
66 66 validates_presence_of :subject, :priority, :project, :tracker, :author, :status
67 67
68 68 validates_length_of :subject, :maximum => 255
69 69 validates_inclusion_of :done_ratio, :in => 0..100
70 validates_numericality_of :estimated_hours, :allow_nil => true
70 validates :estimated_hours, :numericality => {:greater_than_or_equal_to => 0, :allow_nil => true, :message => :invalid}
71 71 validates :start_date, :date => true
72 72 validates :due_date, :date => true
73 73 validate :validate_issue, :validate_required_fields
74 74
75 75 scope :visible, lambda {|*args|
76 76 includes(:project).where(Issue.visible_condition(args.shift || User.current, *args))
77 77 }
78 78
79 79 scope :open, lambda {|*args|
80 80 is_closed = args.size > 0 ? !args.first : false
81 81 includes(:status).where("#{IssueStatus.table_name}.is_closed = ?", is_closed)
82 82 }
83 83
84 84 scope :recently_updated, lambda { order("#{Issue.table_name}.updated_on DESC") }
85 85 scope :on_active_project, lambda {
86 86 includes(:status, :project, :tracker).where("#{Project.table_name}.status = ?", Project::STATUS_ACTIVE)
87 87 }
88 88
89 89 before_create :default_assign
90 90 before_save :close_duplicates, :update_done_ratio_from_issue_status, :force_updated_on_change
91 91 after_save {|issue| issue.send :after_project_change if !issue.id_changed? && issue.project_id_changed?}
92 92 after_save :reschedule_following_issues, :update_nested_set_attributes, :update_parent_attributes, :create_journal
93 93 # Should be after_create but would be called before previous after_save callbacks
94 94 after_save :after_create_from_copy
95 95 after_destroy :update_parent_attributes
96 96
97 97 # Returns a SQL conditions string used to find all issues visible by the specified user
98 98 def self.visible_condition(user, options={})
99 99 Project.allowed_to_condition(user, :view_issues, options) do |role, user|
100 100 if user.logged?
101 101 case role.issues_visibility
102 102 when 'all'
103 103 nil
104 104 when 'default'
105 105 user_ids = [user.id] + user.groups.map(&:id)
106 106 "(#{table_name}.is_private = #{connection.quoted_false} OR #{table_name}.author_id = #{user.id} OR #{table_name}.assigned_to_id IN (#{user_ids.join(',')}))"
107 107 when 'own'
108 108 user_ids = [user.id] + user.groups.map(&:id)
109 109 "(#{table_name}.author_id = #{user.id} OR #{table_name}.assigned_to_id IN (#{user_ids.join(',')}))"
110 110 else
111 111 '1=0'
112 112 end
113 113 else
114 114 "(#{table_name}.is_private = #{connection.quoted_false})"
115 115 end
116 116 end
117 117 end
118 118
119 119 # Returns true if usr or current user is allowed to view the issue
120 120 def visible?(usr=nil)
121 121 (usr || User.current).allowed_to?(:view_issues, self.project) do |role, user|
122 122 if user.logged?
123 123 case role.issues_visibility
124 124 when 'all'
125 125 true
126 126 when 'default'
127 127 !self.is_private? || (self.author == user || user.is_or_belongs_to?(assigned_to))
128 128 when 'own'
129 129 self.author == user || user.is_or_belongs_to?(assigned_to)
130 130 else
131 131 false
132 132 end
133 133 else
134 134 !self.is_private?
135 135 end
136 136 end
137 137 end
138 138
139 139 # Returns true if user or current user is allowed to edit or add a note to the issue
140 140 def editable?(user=User.current)
141 141 user.allowed_to?(:edit_issues, project) || user.allowed_to?(:add_issue_notes, project)
142 142 end
143 143
144 144 def initialize(attributes=nil, *args)
145 145 super
146 146 if new_record?
147 147 # set default values for new records only
148 148 self.status ||= IssueStatus.default
149 149 self.priority ||= IssuePriority.default
150 150 self.watcher_user_ids = []
151 151 end
152 152 end
153 153
154 154 # AR#Persistence#destroy would raise and RecordNotFound exception
155 155 # if the issue was already deleted or updated (non matching lock_version).
156 156 # This is a problem when bulk deleting issues or deleting a project
157 157 # (because an issue may already be deleted if its parent was deleted
158 158 # first).
159 159 # The issue is reloaded by the nested_set before being deleted so
160 160 # the lock_version condition should not be an issue but we handle it.
161 161 def destroy
162 162 super
163 163 rescue ActiveRecord::RecordNotFound
164 164 # Stale or already deleted
165 165 begin
166 166 reload
167 167 rescue ActiveRecord::RecordNotFound
168 168 # The issue was actually already deleted
169 169 @destroyed = true
170 170 return freeze
171 171 end
172 172 # The issue was stale, retry to destroy
173 173 super
174 174 end
175 175
176 176 def reload(*args)
177 177 @workflow_rule_by_attribute = nil
178 178 @assignable_versions = nil
179 179 super
180 180 end
181 181
182 182 # Overrides Redmine::Acts::Customizable::InstanceMethods#available_custom_fields
183 183 def available_custom_fields
184 184 (project && tracker) ? (project.all_issue_custom_fields & tracker.custom_fields.all) : []
185 185 end
186 186
187 187 # Copies attributes from another issue, arg can be an id or an Issue
188 188 def copy_from(arg, options={})
189 189 issue = arg.is_a?(Issue) ? arg : Issue.visible.find(arg)
190 190 self.attributes = issue.attributes.dup.except("id", "root_id", "parent_id", "lft", "rgt", "created_on", "updated_on")
191 191 self.custom_field_values = issue.custom_field_values.inject({}) {|h,v| h[v.custom_field_id] = v.value; h}
192 192 self.status = issue.status
193 193 self.author = User.current
194 194 unless options[:attachments] == false
195 195 self.attachments = issue.attachments.map do |attachement|
196 196 attachement.copy(:container => self)
197 197 end
198 198 end
199 199 @copied_from = issue
200 200 @copy_options = options
201 201 self
202 202 end
203 203
204 204 # Returns an unsaved copy of the issue
205 205 def copy(attributes=nil, copy_options={})
206 206 copy = self.class.new.copy_from(self, copy_options)
207 207 copy.attributes = attributes if attributes
208 208 copy
209 209 end
210 210
211 211 # Returns true if the issue is a copy
212 212 def copy?
213 213 @copied_from.present?
214 214 end
215 215
216 216 # Moves/copies an issue to a new project and tracker
217 217 # Returns the moved/copied issue on success, false on failure
218 218 def move_to_project(new_project, new_tracker=nil, options={})
219 219 ActiveSupport::Deprecation.warn "Issue#move_to_project is deprecated, use #project= instead."
220 220
221 221 if options[:copy]
222 222 issue = self.copy
223 223 else
224 224 issue = self
225 225 end
226 226
227 227 issue.init_journal(User.current, options[:notes])
228 228
229 229 # Preserve previous behaviour
230 230 # #move_to_project doesn't change tracker automatically
231 231 issue.send :project=, new_project, true
232 232 if new_tracker
233 233 issue.tracker = new_tracker
234 234 end
235 235 # Allow bulk setting of attributes on the issue
236 236 if options[:attributes]
237 237 issue.attributes = options[:attributes]
238 238 end
239 239
240 240 issue.save ? issue : false
241 241 end
242 242
243 243 def status_id=(sid)
244 244 self.status = nil
245 245 result = write_attribute(:status_id, sid)
246 246 @workflow_rule_by_attribute = nil
247 247 result
248 248 end
249 249
250 250 def priority_id=(pid)
251 251 self.priority = nil
252 252 write_attribute(:priority_id, pid)
253 253 end
254 254
255 255 def category_id=(cid)
256 256 self.category = nil
257 257 write_attribute(:category_id, cid)
258 258 end
259 259
260 260 def fixed_version_id=(vid)
261 261 self.fixed_version = nil
262 262 write_attribute(:fixed_version_id, vid)
263 263 end
264 264
265 265 def tracker_id=(tid)
266 266 self.tracker = nil
267 267 result = write_attribute(:tracker_id, tid)
268 268 @custom_field_values = nil
269 269 @workflow_rule_by_attribute = nil
270 270 result
271 271 end
272 272
273 273 def project_id=(project_id)
274 274 if project_id.to_s != self.project_id.to_s
275 275 self.project = (project_id.present? ? Project.find_by_id(project_id) : nil)
276 276 end
277 277 end
278 278
279 279 def project=(project, keep_tracker=false)
280 280 project_was = self.project
281 281 write_attribute(:project_id, project ? project.id : nil)
282 282 association_instance_set('project', project)
283 283 if project_was && project && project_was != project
284 284 @assignable_versions = nil
285 285
286 286 unless keep_tracker || project.trackers.include?(tracker)
287 287 self.tracker = project.trackers.first
288 288 end
289 289 # Reassign to the category with same name if any
290 290 if category
291 291 self.category = project.issue_categories.find_by_name(category.name)
292 292 end
293 293 # Keep the fixed_version if it's still valid in the new_project
294 294 if fixed_version && fixed_version.project != project && !project.shared_versions.include?(fixed_version)
295 295 self.fixed_version = nil
296 296 end
297 297 # Clear the parent task if it's no longer valid
298 298 unless valid_parent_project?
299 299 self.parent_issue_id = nil
300 300 end
301 301 @custom_field_values = nil
302 302 end
303 303 end
304 304
305 305 def description=(arg)
306 306 if arg.is_a?(String)
307 307 arg = arg.gsub(/(\r\n|\n|\r)/, "\r\n")
308 308 end
309 309 write_attribute(:description, arg)
310 310 end
311 311
312 312 # Overrides assign_attributes so that project and tracker get assigned first
313 313 def assign_attributes_with_project_and_tracker_first(new_attributes, *args)
314 314 return if new_attributes.nil?
315 315 attrs = new_attributes.dup
316 316 attrs.stringify_keys!
317 317
318 318 %w(project project_id tracker tracker_id).each do |attr|
319 319 if attrs.has_key?(attr)
320 320 send "#{attr}=", attrs.delete(attr)
321 321 end
322 322 end
323 323 send :assign_attributes_without_project_and_tracker_first, attrs, *args
324 324 end
325 325 # Do not redefine alias chain on reload (see #4838)
326 326 alias_method_chain(:assign_attributes, :project_and_tracker_first) unless method_defined?(:assign_attributes_without_project_and_tracker_first)
327 327
328 328 def estimated_hours=(h)
329 329 write_attribute :estimated_hours, (h.is_a?(String) ? h.to_hours : h)
330 330 end
331 331
332 332 safe_attributes 'project_id',
333 333 :if => lambda {|issue, user|
334 334 if issue.new_record?
335 335 issue.copy?
336 336 elsif user.allowed_to?(:move_issues, issue.project)
337 337 projects = Issue.allowed_target_projects_on_move(user)
338 338 projects.include?(issue.project) && projects.size > 1
339 339 end
340 340 }
341 341
342 342 safe_attributes 'tracker_id',
343 343 'status_id',
344 344 'category_id',
345 345 'assigned_to_id',
346 346 'priority_id',
347 347 'fixed_version_id',
348 348 'subject',
349 349 'description',
350 350 'start_date',
351 351 'due_date',
352 352 'done_ratio',
353 353 'estimated_hours',
354 354 'custom_field_values',
355 355 'custom_fields',
356 356 'lock_version',
357 357 'notes',
358 358 :if => lambda {|issue, user| issue.new_record? || user.allowed_to?(:edit_issues, issue.project) }
359 359
360 360 safe_attributes 'status_id',
361 361 'assigned_to_id',
362 362 'fixed_version_id',
363 363 'done_ratio',
364 364 'lock_version',
365 365 'notes',
366 366 :if => lambda {|issue, user| issue.new_statuses_allowed_to(user).any? }
367 367
368 368 safe_attributes 'notes',
369 369 :if => lambda {|issue, user| user.allowed_to?(:add_issue_notes, issue.project)}
370 370
371 371 safe_attributes 'private_notes',
372 372 :if => lambda {|issue, user| !issue.new_record? && user.allowed_to?(:set_notes_private, issue.project)}
373 373
374 374 safe_attributes 'watcher_user_ids',
375 375 :if => lambda {|issue, user| issue.new_record? && user.allowed_to?(:add_issue_watchers, issue.project)}
376 376
377 377 safe_attributes 'is_private',
378 378 :if => lambda {|issue, user|
379 379 user.allowed_to?(:set_issues_private, issue.project) ||
380 380 (issue.author == user && user.allowed_to?(:set_own_issues_private, issue.project))
381 381 }
382 382
383 383 safe_attributes 'parent_issue_id',
384 384 :if => lambda {|issue, user| (issue.new_record? || user.allowed_to?(:edit_issues, issue.project)) &&
385 385 user.allowed_to?(:manage_subtasks, issue.project)}
386 386
387 387 def safe_attribute_names(user=nil)
388 388 names = super
389 389 names -= disabled_core_fields
390 390 names -= read_only_attribute_names(user)
391 391 names
392 392 end
393 393
394 394 # Safely sets attributes
395 395 # Should be called from controllers instead of #attributes=
396 396 # attr_accessible is too rough because we still want things like
397 397 # Issue.new(:project => foo) to work
398 398 def safe_attributes=(attrs, user=User.current)
399 399 return unless attrs.is_a?(Hash)
400 400
401 401 attrs = attrs.dup
402 402
403 403 # Project and Tracker must be set before since new_statuses_allowed_to depends on it.
404 404 if (p = attrs.delete('project_id')) && safe_attribute?('project_id')
405 405 if allowed_target_projects(user).collect(&:id).include?(p.to_i)
406 406 self.project_id = p
407 407 end
408 408 end
409 409
410 410 if (t = attrs.delete('tracker_id')) && safe_attribute?('tracker_id')
411 411 self.tracker_id = t
412 412 end
413 413
414 414 if (s = attrs.delete('status_id')) && safe_attribute?('status_id')
415 415 if new_statuses_allowed_to(user).collect(&:id).include?(s.to_i)
416 416 self.status_id = s
417 417 end
418 418 end
419 419
420 420 attrs = delete_unsafe_attributes(attrs, user)
421 421 return if attrs.empty?
422 422
423 423 unless leaf?
424 424 attrs.reject! {|k,v| %w(priority_id done_ratio start_date due_date estimated_hours).include?(k)}
425 425 end
426 426
427 427 if attrs['parent_issue_id'].present?
428 428 s = attrs['parent_issue_id'].to_s
429 429 unless (m = s.match(%r{\A#?(\d+)\z})) && Issue.visible(user).exists?(m[1])
430 430 @invalid_parent_issue_id = attrs.delete('parent_issue_id')
431 431 end
432 432 end
433 433
434 434 if attrs['custom_field_values'].present?
435 435 attrs['custom_field_values'] = attrs['custom_field_values'].reject {|k, v| read_only_attribute_names(user).include? k.to_s}
436 436 end
437 437
438 438 if attrs['custom_fields'].present?
439 439 attrs['custom_fields'] = attrs['custom_fields'].reject {|c| read_only_attribute_names(user).include? c['id'].to_s}
440 440 end
441 441
442 442 # mass-assignment security bypass
443 443 assign_attributes attrs, :without_protection => true
444 444 end
445 445
446 446 def disabled_core_fields
447 447 tracker ? tracker.disabled_core_fields : []
448 448 end
449 449
450 450 # Returns the custom_field_values that can be edited by the given user
451 451 def editable_custom_field_values(user=nil)
452 452 custom_field_values.reject do |value|
453 453 read_only_attribute_names(user).include?(value.custom_field_id.to_s)
454 454 end
455 455 end
456 456
457 457 # Returns the names of attributes that are read-only for user or the current user
458 458 # For users with multiple roles, the read-only fields are the intersection of
459 459 # read-only fields of each role
460 460 # The result is an array of strings where sustom fields are represented with their ids
461 461 #
462 462 # Examples:
463 463 # issue.read_only_attribute_names # => ['due_date', '2']
464 464 # issue.read_only_attribute_names(user) # => []
465 465 def read_only_attribute_names(user=nil)
466 466 workflow_rule_by_attribute(user).reject {|attr, rule| rule != 'readonly'}.keys
467 467 end
468 468
469 469 # Returns the names of required attributes for user or the current user
470 470 # For users with multiple roles, the required fields are the intersection of
471 471 # required fields of each role
472 472 # The result is an array of strings where sustom fields are represented with their ids
473 473 #
474 474 # Examples:
475 475 # issue.required_attribute_names # => ['due_date', '2']
476 476 # issue.required_attribute_names(user) # => []
477 477 def required_attribute_names(user=nil)
478 478 workflow_rule_by_attribute(user).reject {|attr, rule| rule != 'required'}.keys
479 479 end
480 480
481 481 # Returns true if the attribute is required for user
482 482 def required_attribute?(name, user=nil)
483 483 required_attribute_names(user).include?(name.to_s)
484 484 end
485 485
486 486 # Returns a hash of the workflow rule by attribute for the given user
487 487 #
488 488 # Examples:
489 489 # issue.workflow_rule_by_attribute # => {'due_date' => 'required', 'start_date' => 'readonly'}
490 490 def workflow_rule_by_attribute(user=nil)
491 491 return @workflow_rule_by_attribute if @workflow_rule_by_attribute && user.nil?
492 492
493 493 user_real = user || User.current
494 494 roles = user_real.admin ? Role.all : user_real.roles_for_project(project)
495 495 return {} if roles.empty?
496 496
497 497 result = {}
498 498 workflow_permissions = WorkflowPermission.where(:tracker_id => tracker_id, :old_status_id => status_id, :role_id => roles.map(&:id)).all
499 499 if workflow_permissions.any?
500 500 workflow_rules = workflow_permissions.inject({}) do |h, wp|
501 501 h[wp.field_name] ||= []
502 502 h[wp.field_name] << wp.rule
503 503 h
504 504 end
505 505 workflow_rules.each do |attr, rules|
506 506 next if rules.size < roles.size
507 507 uniq_rules = rules.uniq
508 508 if uniq_rules.size == 1
509 509 result[attr] = uniq_rules.first
510 510 else
511 511 result[attr] = 'required'
512 512 end
513 513 end
514 514 end
515 515 @workflow_rule_by_attribute = result if user.nil?
516 516 result
517 517 end
518 518 private :workflow_rule_by_attribute
519 519
520 520 def done_ratio
521 521 if Issue.use_status_for_done_ratio? && status && status.default_done_ratio
522 522 status.default_done_ratio
523 523 else
524 524 read_attribute(:done_ratio)
525 525 end
526 526 end
527 527
528 528 def self.use_status_for_done_ratio?
529 529 Setting.issue_done_ratio == 'issue_status'
530 530 end
531 531
532 532 def self.use_field_for_done_ratio?
533 533 Setting.issue_done_ratio == 'issue_field'
534 534 end
535 535
536 536 def validate_issue
537 537 if due_date && start_date && due_date < start_date
538 538 errors.add :due_date, :greater_than_start_date
539 539 end
540 540
541 541 if start_date && soonest_start && start_date < soonest_start
542 542 errors.add :start_date, :invalid
543 543 end
544 544
545 545 if fixed_version
546 546 if !assignable_versions.include?(fixed_version)
547 547 errors.add :fixed_version_id, :inclusion
548 548 elsif reopened? && fixed_version.closed?
549 549 errors.add :base, I18n.t(:error_can_not_reopen_issue_on_closed_version)
550 550 end
551 551 end
552 552
553 553 # Checks that the issue can not be added/moved to a disabled tracker
554 554 if project && (tracker_id_changed? || project_id_changed?)
555 555 unless project.trackers.include?(tracker)
556 556 errors.add :tracker_id, :inclusion
557 557 end
558 558 end
559 559
560 560 # Checks parent issue assignment
561 561 if @invalid_parent_issue_id.present?
562 562 errors.add :parent_issue_id, :invalid
563 563 elsif @parent_issue
564 564 if !valid_parent_project?(@parent_issue)
565 565 errors.add :parent_issue_id, :invalid
566 566 elsif !new_record?
567 567 # moving an existing issue
568 568 if @parent_issue.root_id != root_id
569 569 # we can always move to another tree
570 570 elsif move_possible?(@parent_issue)
571 571 # move accepted inside tree
572 572 else
573 573 errors.add :parent_issue_id, :invalid
574 574 end
575 575 end
576 576 end
577 577 end
578 578
579 579 # Validates the issue against additional workflow requirements
580 580 def validate_required_fields
581 581 user = new_record? ? author : current_journal.try(:user)
582 582
583 583 required_attribute_names(user).each do |attribute|
584 584 if attribute =~ /^\d+$/
585 585 attribute = attribute.to_i
586 586 v = custom_field_values.detect {|v| v.custom_field_id == attribute }
587 587 if v && v.value.blank?
588 588 errors.add :base, v.custom_field.name + ' ' + l('activerecord.errors.messages.blank')
589 589 end
590 590 else
591 591 if respond_to?(attribute) && send(attribute).blank?
592 592 errors.add attribute, :blank
593 593 end
594 594 end
595 595 end
596 596 end
597 597
598 598 # Set the done_ratio using the status if that setting is set. This will keep the done_ratios
599 599 # even if the user turns off the setting later
600 600 def update_done_ratio_from_issue_status
601 601 if Issue.use_status_for_done_ratio? && status && status.default_done_ratio
602 602 self.done_ratio = status.default_done_ratio
603 603 end
604 604 end
605 605
606 606 def init_journal(user, notes = "")
607 607 @current_journal ||= Journal.new(:journalized => self, :user => user, :notes => notes)
608 608 if new_record?
609 609 @current_journal.notify = false
610 610 else
611 611 @attributes_before_change = attributes.dup
612 612 @custom_values_before_change = {}
613 613 self.custom_field_values.each {|c| @custom_values_before_change.store c.custom_field_id, c.value }
614 614 end
615 615 @current_journal
616 616 end
617 617
618 618 # Returns the id of the last journal or nil
619 619 def last_journal_id
620 620 if new_record?
621 621 nil
622 622 else
623 623 journals.maximum(:id)
624 624 end
625 625 end
626 626
627 627 # Returns a scope for journals that have an id greater than journal_id
628 628 def journals_after(journal_id)
629 629 scope = journals.reorder("#{Journal.table_name}.id ASC")
630 630 if journal_id.present?
631 631 scope = scope.where("#{Journal.table_name}.id > ?", journal_id.to_i)
632 632 end
633 633 scope
634 634 end
635 635
636 636 # Return true if the issue is closed, otherwise false
637 637 def closed?
638 638 self.status.is_closed?
639 639 end
640 640
641 641 # Return true if the issue is being reopened
642 642 def reopened?
643 643 if !new_record? && status_id_changed?
644 644 status_was = IssueStatus.find_by_id(status_id_was)
645 645 status_new = IssueStatus.find_by_id(status_id)
646 646 if status_was && status_new && status_was.is_closed? && !status_new.is_closed?
647 647 return true
648 648 end
649 649 end
650 650 false
651 651 end
652 652
653 653 # Return true if the issue is being closed
654 654 def closing?
655 655 if !new_record? && status_id_changed?
656 656 status_was = IssueStatus.find_by_id(status_id_was)
657 657 status_new = IssueStatus.find_by_id(status_id)
658 658 if status_was && status_new && !status_was.is_closed? && status_new.is_closed?
659 659 return true
660 660 end
661 661 end
662 662 false
663 663 end
664 664
665 665 # Returns true if the issue is overdue
666 666 def overdue?
667 667 !due_date.nil? && (due_date < Date.today) && !status.is_closed?
668 668 end
669 669
670 670 # Is the amount of work done less than it should for the due date
671 671 def behind_schedule?
672 672 return false if start_date.nil? || due_date.nil?
673 673 done_date = start_date + ((due_date - start_date+1)* done_ratio/100).floor
674 674 return done_date <= Date.today
675 675 end
676 676
677 677 # Does this issue have children?
678 678 def children?
679 679 !leaf?
680 680 end
681 681
682 682 # Users the issue can be assigned to
683 683 def assignable_users
684 684 users = project.assignable_users
685 685 users << author if author
686 686 users << assigned_to if assigned_to
687 687 users.uniq.sort
688 688 end
689 689
690 690 # Versions that the issue can be assigned to
691 691 def assignable_versions
692 692 return @assignable_versions if @assignable_versions
693 693
694 694 versions = project.shared_versions.open.all
695 695 if fixed_version
696 696 if fixed_version_id_changed?
697 697 # nothing to do
698 698 elsif project_id_changed?
699 699 if project.shared_versions.include?(fixed_version)
700 700 versions << fixed_version
701 701 end
702 702 else
703 703 versions << fixed_version
704 704 end
705 705 end
706 706 @assignable_versions = versions.uniq.sort
707 707 end
708 708
709 709 # Returns true if this issue is blocked by another issue that is still open
710 710 def blocked?
711 711 !relations_to.detect {|ir| ir.relation_type == 'blocks' && !ir.issue_from.closed?}.nil?
712 712 end
713 713
714 714 # Returns an array of statuses that user is able to apply
715 715 def new_statuses_allowed_to(user=User.current, include_default=false)
716 716 if new_record? && @copied_from
717 717 [IssueStatus.default, @copied_from.status].compact.uniq.sort
718 718 else
719 719 initial_status = nil
720 720 if new_record?
721 721 initial_status = IssueStatus.default
722 722 elsif status_id_was
723 723 initial_status = IssueStatus.find_by_id(status_id_was)
724 724 end
725 725 initial_status ||= status
726 726
727 727 statuses = initial_status.find_new_statuses_allowed_to(
728 728 user.admin ? Role.all : user.roles_for_project(project),
729 729 tracker,
730 730 author == user,
731 731 assigned_to_id_changed? ? assigned_to_id_was == user.id : assigned_to_id == user.id
732 732 )
733 733 statuses << initial_status unless statuses.empty?
734 734 statuses << IssueStatus.default if include_default
735 735 statuses = statuses.compact.uniq.sort
736 736 blocked? ? statuses.reject {|s| s.is_closed?} : statuses
737 737 end
738 738 end
739 739
740 740 def assigned_to_was
741 741 if assigned_to_id_changed? && assigned_to_id_was.present?
742 742 @assigned_to_was ||= User.find_by_id(assigned_to_id_was)
743 743 end
744 744 end
745 745
746 746 # Returns the users that should be notified
747 747 def notified_users
748 748 notified = []
749 749 # Author and assignee are always notified unless they have been
750 750 # locked or don't want to be notified
751 751 notified << author if author
752 752 if assigned_to
753 753 notified += (assigned_to.is_a?(Group) ? assigned_to.users : [assigned_to])
754 754 end
755 755 if assigned_to_was
756 756 notified += (assigned_to_was.is_a?(Group) ? assigned_to_was.users : [assigned_to_was])
757 757 end
758 758 notified = notified.select {|u| u.active? && u.notify_about?(self)}
759 759
760 760 notified += project.notified_users
761 761 notified.uniq!
762 762 # Remove users that can not view the issue
763 763 notified.reject! {|user| !visible?(user)}
764 764 notified
765 765 end
766 766
767 767 # Returns the email addresses that should be notified
768 768 def recipients
769 769 notified_users.collect(&:mail)
770 770 end
771 771
772 772 # Returns the number of hours spent on this issue
773 773 def spent_hours
774 774 @spent_hours ||= time_entries.sum(:hours) || 0
775 775 end
776 776
777 777 # Returns the total number of hours spent on this issue and its descendants
778 778 #
779 779 # Example:
780 780 # spent_hours => 0.0
781 781 # spent_hours => 50.2
782 782 def total_spent_hours
783 783 @total_spent_hours ||= self_and_descendants.sum("#{TimeEntry.table_name}.hours",
784 784 :joins => "LEFT JOIN #{TimeEntry.table_name} ON #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id").to_f || 0.0
785 785 end
786 786
787 787 def relations
788 788 @relations ||= IssueRelation::Relations.new(self, (relations_from + relations_to).sort)
789 789 end
790 790
791 791 # Preloads relations for a collection of issues
792 792 def self.load_relations(issues)
793 793 if issues.any?
794 794 relations = IssueRelation.all(:conditions => ["issue_from_id IN (:ids) OR issue_to_id IN (:ids)", {:ids => issues.map(&:id)}])
795 795 issues.each do |issue|
796 796 issue.instance_variable_set "@relations", relations.select {|r| r.issue_from_id == issue.id || r.issue_to_id == issue.id}
797 797 end
798 798 end
799 799 end
800 800
801 801 # Preloads visible spent time for a collection of issues
802 802 def self.load_visible_spent_hours(issues, user=User.current)
803 803 if issues.any?
804 804 hours_by_issue_id = TimeEntry.visible(user).sum(:hours, :group => :issue_id)
805 805 issues.each do |issue|
806 806 issue.instance_variable_set "@spent_hours", (hours_by_issue_id[issue.id] || 0)
807 807 end
808 808 end
809 809 end
810 810
811 811 # Preloads visible relations for a collection of issues
812 812 def self.load_visible_relations(issues, user=User.current)
813 813 if issues.any?
814 814 issue_ids = issues.map(&:id)
815 815 # Relations with issue_from in given issues and visible issue_to
816 816 relations_from = IssueRelation.includes(:issue_to => [:status, :project]).where(visible_condition(user)).where(:issue_from_id => issue_ids).all
817 817 # Relations with issue_to in given issues and visible issue_from
818 818 relations_to = IssueRelation.includes(:issue_from => [:status, :project]).where(visible_condition(user)).where(:issue_to_id => issue_ids).all
819 819
820 820 issues.each do |issue|
821 821 relations =
822 822 relations_from.select {|relation| relation.issue_from_id == issue.id} +
823 823 relations_to.select {|relation| relation.issue_to_id == issue.id}
824 824
825 825 issue.instance_variable_set "@relations", IssueRelation::Relations.new(issue, relations.sort)
826 826 end
827 827 end
828 828 end
829 829
830 830 # Finds an issue relation given its id.
831 831 def find_relation(relation_id)
832 832 IssueRelation.find(relation_id, :conditions => ["issue_to_id = ? OR issue_from_id = ?", id, id])
833 833 end
834 834
835 835 def all_dependent_issues(except=[])
836 836 except << self
837 837 dependencies = []
838 838 relations_from.each do |relation|
839 839 if relation.issue_to && !except.include?(relation.issue_to)
840 840 dependencies << relation.issue_to
841 841 dependencies += relation.issue_to.all_dependent_issues(except)
842 842 end
843 843 end
844 844 dependencies
845 845 end
846 846
847 847 # Returns an array of issues that duplicate this one
848 848 def duplicates
849 849 relations_to.select {|r| r.relation_type == IssueRelation::TYPE_DUPLICATES}.collect {|r| r.issue_from}
850 850 end
851 851
852 852 # Returns the due date or the target due date if any
853 853 # Used on gantt chart
854 854 def due_before
855 855 due_date || (fixed_version ? fixed_version.effective_date : nil)
856 856 end
857 857
858 858 # Returns the time scheduled for this issue.
859 859 #
860 860 # Example:
861 861 # Start Date: 2/26/09, End Date: 3/04/09
862 862 # duration => 6
863 863 def duration
864 864 (start_date && due_date) ? due_date - start_date : 0
865 865 end
866 866
867 867 # Returns the duration in working days
868 868 def working_duration
869 869 (start_date && due_date) ? working_days(start_date, due_date) : 0
870 870 end
871 871
872 872 def soonest_start(reload=false)
873 873 @soonest_start = nil if reload
874 874 @soonest_start ||= (
875 875 relations_to(reload).collect{|relation| relation.successor_soonest_start} +
876 876 ancestors.collect(&:soonest_start)
877 877 ).compact.max
878 878 end
879 879
880 880 # Sets start_date on the given date or the next working day
881 881 # and changes due_date to keep the same working duration.
882 882 def reschedule_on(date)
883 883 wd = working_duration
884 884 date = next_working_date(date)
885 885 self.start_date = date
886 886 self.due_date = add_working_days(date, wd)
887 887 end
888 888
889 889 # Reschedules the issue on the given date or the next working day and saves the record.
890 890 # If the issue is a parent task, this is done by rescheduling its subtasks.
891 891 def reschedule_on!(date)
892 892 return if date.nil?
893 893 if leaf?
894 894 if start_date.nil? || start_date != date
895 895 if start_date && start_date > date
896 896 # Issue can not be moved earlier than its soonest start date
897 897 date = [soonest_start(true), date].compact.max
898 898 end
899 899 reschedule_on(date)
900 900 begin
901 901 save
902 902 rescue ActiveRecord::StaleObjectError
903 903 reload
904 904 reschedule_on(date)
905 905 save
906 906 end
907 907 end
908 908 else
909 909 leaves.each do |leaf|
910 910 if leaf.start_date
911 911 # Only move subtask if it starts at the same date as the parent
912 912 # or if it starts before the given date
913 913 if start_date == leaf.start_date || date > leaf.start_date
914 914 leaf.reschedule_on!(date)
915 915 end
916 916 else
917 917 leaf.reschedule_on!(date)
918 918 end
919 919 end
920 920 end
921 921 end
922 922
923 923 def <=>(issue)
924 924 if issue.nil?
925 925 -1
926 926 elsif root_id != issue.root_id
927 927 (root_id || 0) <=> (issue.root_id || 0)
928 928 else
929 929 (lft || 0) <=> (issue.lft || 0)
930 930 end
931 931 end
932 932
933 933 def to_s
934 934 "#{tracker} ##{id}: #{subject}"
935 935 end
936 936
937 937 # Returns a string of css classes that apply to the issue
938 938 def css_classes
939 939 s = "issue status-#{status_id} #{priority.try(:css_classes)}"
940 940 s << ' closed' if closed?
941 941 s << ' overdue' if overdue?
942 942 s << ' child' if child?
943 943 s << ' parent' unless leaf?
944 944 s << ' private' if is_private?
945 945 s << ' created-by-me' if User.current.logged? && author_id == User.current.id
946 946 s << ' assigned-to-me' if User.current.logged? && assigned_to_id == User.current.id
947 947 s
948 948 end
949 949
950 950 # Saves an issue and a time_entry from the parameters
951 951 def save_issue_with_child_records(params, existing_time_entry=nil)
952 952 Issue.transaction do
953 953 if params[:time_entry] && (params[:time_entry][:hours].present? || params[:time_entry][:comments].present?) && User.current.allowed_to?(:log_time, project)
954 954 @time_entry = existing_time_entry || TimeEntry.new
955 955 @time_entry.project = project
956 956 @time_entry.issue = self
957 957 @time_entry.user = User.current
958 958 @time_entry.spent_on = User.current.today
959 959 @time_entry.attributes = params[:time_entry]
960 960 self.time_entries << @time_entry
961 961 end
962 962
963 963 # TODO: Rename hook
964 964 Redmine::Hook.call_hook(:controller_issues_edit_before_save, { :params => params, :issue => self, :time_entry => @time_entry, :journal => @current_journal})
965 965 if save
966 966 # TODO: Rename hook
967 967 Redmine::Hook.call_hook(:controller_issues_edit_after_save, { :params => params, :issue => self, :time_entry => @time_entry, :journal => @current_journal})
968 968 else
969 969 raise ActiveRecord::Rollback
970 970 end
971 971 end
972 972 end
973 973
974 974 # Unassigns issues from +version+ if it's no longer shared with issue's project
975 975 def self.update_versions_from_sharing_change(version)
976 976 # Update issues assigned to the version
977 977 update_versions(["#{Issue.table_name}.fixed_version_id = ?", version.id])
978 978 end
979 979
980 980 # Unassigns issues from versions that are no longer shared
981 981 # after +project+ was moved
982 982 def self.update_versions_from_hierarchy_change(project)
983 983 moved_project_ids = project.self_and_descendants.reload.collect(&:id)
984 984 # Update issues of the moved projects and issues assigned to a version of a moved project
985 985 Issue.update_versions(["#{Version.table_name}.project_id IN (?) OR #{Issue.table_name}.project_id IN (?)", moved_project_ids, moved_project_ids])
986 986 end
987 987
988 988 def parent_issue_id=(arg)
989 989 s = arg.to_s.strip.presence
990 990 if s && (m = s.match(%r{\A#?(\d+)\z})) && (@parent_issue = Issue.find_by_id(m[1]))
991 991 @parent_issue.id
992 992 else
993 993 @parent_issue = nil
994 994 @invalid_parent_issue_id = arg
995 995 end
996 996 end
997 997
998 998 def parent_issue_id
999 999 if @invalid_parent_issue_id
1000 1000 @invalid_parent_issue_id
1001 1001 elsif instance_variable_defined? :@parent_issue
1002 1002 @parent_issue.nil? ? nil : @parent_issue.id
1003 1003 else
1004 1004 parent_id
1005 1005 end
1006 1006 end
1007 1007
1008 1008 # Returns true if issue's project is a valid
1009 1009 # parent issue project
1010 1010 def valid_parent_project?(issue=parent)
1011 1011 return true if issue.nil? || issue.project_id == project_id
1012 1012
1013 1013 case Setting.cross_project_subtasks
1014 1014 when 'system'
1015 1015 true
1016 1016 when 'tree'
1017 1017 issue.project.root == project.root
1018 1018 when 'hierarchy'
1019 1019 issue.project.is_or_is_ancestor_of?(project) || issue.project.is_descendant_of?(project)
1020 1020 when 'descendants'
1021 1021 issue.project.is_or_is_ancestor_of?(project)
1022 1022 else
1023 1023 false
1024 1024 end
1025 1025 end
1026 1026
1027 1027 # Extracted from the ReportsController.
1028 1028 def self.by_tracker(project)
1029 1029 count_and_group_by(:project => project,
1030 1030 :field => 'tracker_id',
1031 1031 :joins => Tracker.table_name)
1032 1032 end
1033 1033
1034 1034 def self.by_version(project)
1035 1035 count_and_group_by(:project => project,
1036 1036 :field => 'fixed_version_id',
1037 1037 :joins => Version.table_name)
1038 1038 end
1039 1039
1040 1040 def self.by_priority(project)
1041 1041 count_and_group_by(:project => project,
1042 1042 :field => 'priority_id',
1043 1043 :joins => IssuePriority.table_name)
1044 1044 end
1045 1045
1046 1046 def self.by_category(project)
1047 1047 count_and_group_by(:project => project,
1048 1048 :field => 'category_id',
1049 1049 :joins => IssueCategory.table_name)
1050 1050 end
1051 1051
1052 1052 def self.by_assigned_to(project)
1053 1053 count_and_group_by(:project => project,
1054 1054 :field => 'assigned_to_id',
1055 1055 :joins => User.table_name)
1056 1056 end
1057 1057
1058 1058 def self.by_author(project)
1059 1059 count_and_group_by(:project => project,
1060 1060 :field => 'author_id',
1061 1061 :joins => User.table_name)
1062 1062 end
1063 1063
1064 1064 def self.by_subproject(project)
1065 1065 ActiveRecord::Base.connection.select_all("select s.id as status_id,
1066 1066 s.is_closed as closed,
1067 1067 #{Issue.table_name}.project_id as project_id,
1068 1068 count(#{Issue.table_name}.id) as total
1069 1069 from
1070 1070 #{Issue.table_name}, #{Project.table_name}, #{IssueStatus.table_name} s
1071 1071 where
1072 1072 #{Issue.table_name}.status_id=s.id
1073 1073 and #{Issue.table_name}.project_id = #{Project.table_name}.id
1074 1074 and #{visible_condition(User.current, :project => project, :with_subprojects => true)}
1075 1075 and #{Issue.table_name}.project_id <> #{project.id}
1076 1076 group by s.id, s.is_closed, #{Issue.table_name}.project_id") if project.descendants.active.any?
1077 1077 end
1078 1078 # End ReportsController extraction
1079 1079
1080 1080 # Returns an array of projects that user can assign the issue to
1081 1081 def allowed_target_projects(user=User.current)
1082 1082 if new_record?
1083 1083 Project.all(:conditions => Project.allowed_to_condition(user, :add_issues))
1084 1084 else
1085 1085 self.class.allowed_target_projects_on_move(user)
1086 1086 end
1087 1087 end
1088 1088
1089 1089 # Returns an array of projects that user can move issues to
1090 1090 def self.allowed_target_projects_on_move(user=User.current)
1091 1091 Project.all(:conditions => Project.allowed_to_condition(user, :move_issues))
1092 1092 end
1093 1093
1094 1094 private
1095 1095
1096 1096 def after_project_change
1097 1097 # Update project_id on related time entries
1098 1098 TimeEntry.update_all(["project_id = ?", project_id], {:issue_id => id})
1099 1099
1100 1100 # Delete issue relations
1101 1101 unless Setting.cross_project_issue_relations?
1102 1102 relations_from.clear
1103 1103 relations_to.clear
1104 1104 end
1105 1105
1106 1106 # Move subtasks that were in the same project
1107 1107 children.each do |child|
1108 1108 next unless child.project_id == project_id_was
1109 1109 # Change project and keep project
1110 1110 child.send :project=, project, true
1111 1111 unless child.save
1112 1112 raise ActiveRecord::Rollback
1113 1113 end
1114 1114 end
1115 1115 end
1116 1116
1117 1117 # Callback for after the creation of an issue by copy
1118 1118 # * adds a "copied to" relation with the copied issue
1119 1119 # * copies subtasks from the copied issue
1120 1120 def after_create_from_copy
1121 1121 return unless copy? && !@after_create_from_copy_handled
1122 1122
1123 1123 if (@copied_from.project_id == project_id || Setting.cross_project_issue_relations?) && @copy_options[:link] != false
1124 1124 relation = IssueRelation.new(:issue_from => @copied_from, :issue_to => self, :relation_type => IssueRelation::TYPE_COPIED_TO)
1125 1125 unless relation.save
1126 1126 logger.error "Could not create relation while copying ##{@copied_from.id} to ##{id} due to validation errors: #{relation.errors.full_messages.join(', ')}" if logger
1127 1127 end
1128 1128 end
1129 1129
1130 1130 unless @copied_from.leaf? || @copy_options[:subtasks] == false
1131 1131 @copied_from.children.each do |child|
1132 1132 unless child.visible?
1133 1133 # Do not copy subtasks that are not visible to avoid potential disclosure of private data
1134 1134 logger.error "Subtask ##{child.id} was not copied during ##{@copied_from.id} copy because it is not visible to the current user" if logger
1135 1135 next
1136 1136 end
1137 1137 copy = Issue.new.copy_from(child, @copy_options)
1138 1138 copy.author = author
1139 1139 copy.project = project
1140 1140 copy.parent_issue_id = id
1141 1141 # Children subtasks are copied recursively
1142 1142 unless copy.save
1143 1143 logger.error "Could not copy subtask ##{child.id} while copying ##{@copied_from.id} to ##{id} due to validation errors: #{copy.errors.full_messages.join(', ')}" if logger
1144 1144 end
1145 1145 end
1146 1146 end
1147 1147 @after_create_from_copy_handled = true
1148 1148 end
1149 1149
1150 1150 def update_nested_set_attributes
1151 1151 if root_id.nil?
1152 1152 # issue was just created
1153 1153 self.root_id = (@parent_issue.nil? ? id : @parent_issue.root_id)
1154 1154 set_default_left_and_right
1155 1155 Issue.update_all("root_id = #{root_id}, lft = #{lft}, rgt = #{rgt}", ["id = ?", id])
1156 1156 if @parent_issue
1157 1157 move_to_child_of(@parent_issue)
1158 1158 end
1159 1159 reload
1160 1160 elsif parent_issue_id != parent_id
1161 1161 former_parent_id = parent_id
1162 1162 # moving an existing issue
1163 1163 if @parent_issue && @parent_issue.root_id == root_id
1164 1164 # inside the same tree
1165 1165 move_to_child_of(@parent_issue)
1166 1166 else
1167 1167 # to another tree
1168 1168 unless root?
1169 1169 move_to_right_of(root)
1170 1170 reload
1171 1171 end
1172 1172 old_root_id = root_id
1173 1173 self.root_id = (@parent_issue.nil? ? id : @parent_issue.root_id )
1174 1174 target_maxright = nested_set_scope.maximum(right_column_name) || 0
1175 1175 offset = target_maxright + 1 - lft
1176 1176 Issue.update_all("root_id = #{root_id}, lft = lft + #{offset}, rgt = rgt + #{offset}",
1177 1177 ["root_id = ? AND lft >= ? AND rgt <= ? ", old_root_id, lft, rgt])
1178 1178 self[left_column_name] = lft + offset
1179 1179 self[right_column_name] = rgt + offset
1180 1180 if @parent_issue
1181 1181 move_to_child_of(@parent_issue)
1182 1182 end
1183 1183 end
1184 1184 reload
1185 1185 # delete invalid relations of all descendants
1186 1186 self_and_descendants.each do |issue|
1187 1187 issue.relations.each do |relation|
1188 1188 relation.destroy unless relation.valid?
1189 1189 end
1190 1190 end
1191 1191 # update former parent
1192 1192 recalculate_attributes_for(former_parent_id) if former_parent_id
1193 1193 end
1194 1194 remove_instance_variable(:@parent_issue) if instance_variable_defined?(:@parent_issue)
1195 1195 end
1196 1196
1197 1197 def update_parent_attributes
1198 1198 recalculate_attributes_for(parent_id) if parent_id
1199 1199 end
1200 1200
1201 1201 def recalculate_attributes_for(issue_id)
1202 1202 if issue_id && p = Issue.find_by_id(issue_id)
1203 1203 # priority = highest priority of children
1204 1204 if priority_position = p.children.maximum("#{IssuePriority.table_name}.position", :joins => :priority)
1205 1205 p.priority = IssuePriority.find_by_position(priority_position)
1206 1206 end
1207 1207
1208 1208 # start/due dates = lowest/highest dates of children
1209 1209 p.start_date = p.children.minimum(:start_date)
1210 1210 p.due_date = p.children.maximum(:due_date)
1211 1211 if p.start_date && p.due_date && p.due_date < p.start_date
1212 1212 p.start_date, p.due_date = p.due_date, p.start_date
1213 1213 end
1214 1214
1215 1215 # done ratio = weighted average ratio of leaves
1216 1216 unless Issue.use_status_for_done_ratio? && p.status && p.status.default_done_ratio
1217 1217 leaves_count = p.leaves.count
1218 1218 if leaves_count > 0
1219 1219 average = p.leaves.average(:estimated_hours).to_f
1220 1220 if average == 0
1221 1221 average = 1
1222 1222 end
1223 1223 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
1224 1224 progress = done / (average * leaves_count)
1225 1225 p.done_ratio = progress.round
1226 1226 end
1227 1227 end
1228 1228
1229 1229 # estimate = sum of leaves estimates
1230 1230 p.estimated_hours = p.leaves.sum(:estimated_hours).to_f
1231 1231 p.estimated_hours = nil if p.estimated_hours == 0.0
1232 1232
1233 1233 # ancestors will be recursively updated
1234 1234 p.save(:validate => false)
1235 1235 end
1236 1236 end
1237 1237
1238 1238 # Update issues so their versions are not pointing to a
1239 1239 # fixed_version that is not shared with the issue's project
1240 1240 def self.update_versions(conditions=nil)
1241 1241 # Only need to update issues with a fixed_version from
1242 1242 # a different project and that is not systemwide shared
1243 1243 Issue.scoped(:conditions => conditions).all(
1244 1244 :conditions => "#{Issue.table_name}.fixed_version_id IS NOT NULL" +
1245 1245 " AND #{Issue.table_name}.project_id <> #{Version.table_name}.project_id" +
1246 1246 " AND #{Version.table_name}.sharing <> 'system'",
1247 1247 :include => [:project, :fixed_version]
1248 1248 ).each do |issue|
1249 1249 next if issue.project.nil? || issue.fixed_version.nil?
1250 1250 unless issue.project.shared_versions.include?(issue.fixed_version)
1251 1251 issue.init_journal(User.current)
1252 1252 issue.fixed_version = nil
1253 1253 issue.save
1254 1254 end
1255 1255 end
1256 1256 end
1257 1257
1258 1258 # Callback on file attachment
1259 1259 def attachment_added(obj)
1260 1260 if @current_journal && !obj.new_record?
1261 1261 @current_journal.details << JournalDetail.new(:property => 'attachment', :prop_key => obj.id, :value => obj.filename)
1262 1262 end
1263 1263 end
1264 1264
1265 1265 # Callback on attachment deletion
1266 1266 def attachment_removed(obj)
1267 1267 if @current_journal && !obj.new_record?
1268 1268 @current_journal.details << JournalDetail.new(:property => 'attachment', :prop_key => obj.id, :old_value => obj.filename)
1269 1269 @current_journal.save
1270 1270 end
1271 1271 end
1272 1272
1273 1273 # Default assignment based on category
1274 1274 def default_assign
1275 1275 if assigned_to.nil? && category && category.assigned_to
1276 1276 self.assigned_to = category.assigned_to
1277 1277 end
1278 1278 end
1279 1279
1280 1280 # Updates start/due dates of following issues
1281 1281 def reschedule_following_issues
1282 1282 if start_date_changed? || due_date_changed?
1283 1283 relations_from.each do |relation|
1284 1284 relation.set_issue_to_dates
1285 1285 end
1286 1286 end
1287 1287 end
1288 1288
1289 1289 # Closes duplicates if the issue is being closed
1290 1290 def close_duplicates
1291 1291 if closing?
1292 1292 duplicates.each do |duplicate|
1293 1293 # Reload is need in case the duplicate was updated by a previous duplicate
1294 1294 duplicate.reload
1295 1295 # Don't re-close it if it's already closed
1296 1296 next if duplicate.closed?
1297 1297 # Same user and notes
1298 1298 if @current_journal
1299 1299 duplicate.init_journal(@current_journal.user, @current_journal.notes)
1300 1300 end
1301 1301 duplicate.update_attribute :status, self.status
1302 1302 end
1303 1303 end
1304 1304 end
1305 1305
1306 1306 # Make sure updated_on is updated when adding a note
1307 1307 def force_updated_on_change
1308 1308 if @current_journal
1309 1309 self.updated_on = current_time_from_proper_timezone
1310 1310 end
1311 1311 end
1312 1312
1313 1313 # Saves the changes in a Journal
1314 1314 # Called after_save
1315 1315 def create_journal
1316 1316 if @current_journal
1317 1317 # attributes changes
1318 1318 if @attributes_before_change
1319 1319 (Issue.column_names - %w(id root_id lft rgt lock_version created_on updated_on)).each {|c|
1320 1320 before = @attributes_before_change[c]
1321 1321 after = send(c)
1322 1322 next if before == after || (before.blank? && after.blank?)
1323 1323 @current_journal.details << JournalDetail.new(:property => 'attr',
1324 1324 :prop_key => c,
1325 1325 :old_value => before,
1326 1326 :value => after)
1327 1327 }
1328 1328 end
1329 1329 if @custom_values_before_change
1330 1330 # custom fields changes
1331 1331 custom_field_values.each {|c|
1332 1332 before = @custom_values_before_change[c.custom_field_id]
1333 1333 after = c.value
1334 1334 next if before == after || (before.blank? && after.blank?)
1335 1335
1336 1336 if before.is_a?(Array) || after.is_a?(Array)
1337 1337 before = [before] unless before.is_a?(Array)
1338 1338 after = [after] unless after.is_a?(Array)
1339 1339
1340 1340 # values removed
1341 1341 (before - after).reject(&:blank?).each do |value|
1342 1342 @current_journal.details << JournalDetail.new(:property => 'cf',
1343 1343 :prop_key => c.custom_field_id,
1344 1344 :old_value => value,
1345 1345 :value => nil)
1346 1346 end
1347 1347 # values added
1348 1348 (after - before).reject(&:blank?).each do |value|
1349 1349 @current_journal.details << JournalDetail.new(:property => 'cf',
1350 1350 :prop_key => c.custom_field_id,
1351 1351 :old_value => nil,
1352 1352 :value => value)
1353 1353 end
1354 1354 else
1355 1355 @current_journal.details << JournalDetail.new(:property => 'cf',
1356 1356 :prop_key => c.custom_field_id,
1357 1357 :old_value => before,
1358 1358 :value => after)
1359 1359 end
1360 1360 }
1361 1361 end
1362 1362 @current_journal.save
1363 1363 # reset current journal
1364 1364 init_journal @current_journal.user, @current_journal.notes
1365 1365 end
1366 1366 end
1367 1367
1368 1368 # Query generator for selecting groups of issue counts for a project
1369 1369 # based on specific criteria
1370 1370 #
1371 1371 # Options
1372 1372 # * project - Project to search in.
1373 1373 # * field - String. Issue field to key off of in the grouping.
1374 1374 # * joins - String. The table name to join against.
1375 1375 def self.count_and_group_by(options)
1376 1376 project = options.delete(:project)
1377 1377 select_field = options.delete(:field)
1378 1378 joins = options.delete(:joins)
1379 1379
1380 1380 where = "#{Issue.table_name}.#{select_field}=j.id"
1381 1381
1382 1382 ActiveRecord::Base.connection.select_all("select s.id as status_id,
1383 1383 s.is_closed as closed,
1384 1384 j.id as #{select_field},
1385 1385 count(#{Issue.table_name}.id) as total
1386 1386 from
1387 1387 #{Issue.table_name}, #{Project.table_name}, #{IssueStatus.table_name} s, #{joins} j
1388 1388 where
1389 1389 #{Issue.table_name}.status_id=s.id
1390 1390 and #{where}
1391 1391 and #{Issue.table_name}.project_id=#{Project.table_name}.id
1392 1392 and #{visible_condition(User.current, :project => project)}
1393 1393 group by s.id, s.is_closed, j.id")
1394 1394 end
1395 1395 end
@@ -1,1938 +1,1947
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, :journals, :journal_details,
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 teardown
35 35 User.current = nil
36 36 end
37 37
38 38 def test_create
39 39 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3,
40 40 :status_id => 1, :priority => IssuePriority.all.first,
41 41 :subject => 'test_create',
42 42 :description => 'IssueTest#test_create', :estimated_hours => '1:30')
43 43 assert issue.save
44 44 issue.reload
45 45 assert_equal 1.5, issue.estimated_hours
46 46 end
47 47
48 48 def test_create_minimal
49 49 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3,
50 50 :status_id => 1, :priority => IssuePriority.all.first,
51 51 :subject => 'test_create')
52 52 assert issue.save
53 53 assert issue.description.nil?
54 54 assert_nil issue.estimated_hours
55 55 end
56 56
57 57 def test_start_date_format_should_be_validated
58 58 set_language_if_valid 'en'
59 59 ['2012', 'ABC', '2012-15-20'].each do |invalid_date|
60 60 issue = Issue.new(:start_date => invalid_date)
61 61 assert !issue.valid?
62 62 assert_include 'Start date is not a valid date', issue.errors.full_messages, "No error found for invalid date #{invalid_date}"
63 63 end
64 64 end
65 65
66 66 def test_due_date_format_should_be_validated
67 67 set_language_if_valid 'en'
68 68 ['2012', 'ABC', '2012-15-20'].each do |invalid_date|
69 69 issue = Issue.new(:due_date => invalid_date)
70 70 assert !issue.valid?
71 71 assert_include 'Due date is not a valid date', issue.errors.full_messages, "No error found for invalid date #{invalid_date}"
72 72 end
73 73 end
74 74
75 75 def test_due_date_lesser_than_start_date_should_not_validate
76 76 set_language_if_valid 'en'
77 77 issue = Issue.new(:start_date => '2012-10-06', :due_date => '2012-10-02')
78 78 assert !issue.valid?
79 79 assert_include 'Due date must be greater than start date', issue.errors.full_messages
80 80 end
81 81
82 def test_estimated_hours_should_be_validated
83 set_language_if_valid 'en'
84 ['-2'].each do |invalid|
85 issue = Issue.new(:estimated_hours => invalid)
86 assert !issue.valid?
87 assert_include 'Estimated time is invalid', issue.errors.full_messages
88 end
89 end
90
82 91 def test_create_with_required_custom_field
83 92 set_language_if_valid 'en'
84 93 field = IssueCustomField.find_by_name('Database')
85 94 field.update_attribute(:is_required, true)
86 95
87 96 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1,
88 97 :status_id => 1, :subject => 'test_create',
89 98 :description => 'IssueTest#test_create_with_required_custom_field')
90 99 assert issue.available_custom_fields.include?(field)
91 100 # No value for the custom field
92 101 assert !issue.save
93 102 assert_equal ["Database can't be blank"], issue.errors.full_messages
94 103 # Blank value
95 104 issue.custom_field_values = { field.id => '' }
96 105 assert !issue.save
97 106 assert_equal ["Database can't be blank"], issue.errors.full_messages
98 107 # Invalid value
99 108 issue.custom_field_values = { field.id => 'SQLServer' }
100 109 assert !issue.save
101 110 assert_equal ["Database is not included in the list"], issue.errors.full_messages
102 111 # Valid value
103 112 issue.custom_field_values = { field.id => 'PostgreSQL' }
104 113 assert issue.save
105 114 issue.reload
106 115 assert_equal 'PostgreSQL', issue.custom_value_for(field).value
107 116 end
108 117
109 118 def test_create_with_group_assignment
110 119 with_settings :issue_group_assignment => '1' do
111 120 assert Issue.new(:project_id => 2, :tracker_id => 1, :author_id => 1,
112 121 :subject => 'Group assignment',
113 122 :assigned_to_id => 11).save
114 123 issue = Issue.first(:order => 'id DESC')
115 124 assert_kind_of Group, issue.assigned_to
116 125 assert_equal Group.find(11), issue.assigned_to
117 126 end
118 127 end
119 128
120 129 def test_create_with_parent_issue_id
121 130 issue = Issue.new(:project_id => 1, :tracker_id => 1,
122 131 :author_id => 1, :subject => 'Group assignment',
123 132 :parent_issue_id => 1)
124 133 assert_save issue
125 134 assert_equal 1, issue.parent_issue_id
126 135 assert_equal Issue.find(1), issue.parent
127 136 end
128 137
129 138 def test_create_with_sharp_parent_issue_id
130 139 issue = Issue.new(:project_id => 1, :tracker_id => 1,
131 140 :author_id => 1, :subject => 'Group assignment',
132 141 :parent_issue_id => "#1")
133 142 assert_save issue
134 143 assert_equal 1, issue.parent_issue_id
135 144 assert_equal Issue.find(1), issue.parent
136 145 end
137 146
138 147 def test_create_with_invalid_parent_issue_id
139 148 set_language_if_valid 'en'
140 149 issue = Issue.new(:project_id => 1, :tracker_id => 1,
141 150 :author_id => 1, :subject => 'Group assignment',
142 151 :parent_issue_id => '01ABC')
143 152 assert !issue.save
144 153 assert_equal '01ABC', issue.parent_issue_id
145 154 assert_include 'Parent task is invalid', issue.errors.full_messages
146 155 end
147 156
148 157 def test_create_with_invalid_sharp_parent_issue_id
149 158 set_language_if_valid 'en'
150 159 issue = Issue.new(:project_id => 1, :tracker_id => 1,
151 160 :author_id => 1, :subject => 'Group assignment',
152 161 :parent_issue_id => '#01ABC')
153 162 assert !issue.save
154 163 assert_equal '#01ABC', issue.parent_issue_id
155 164 assert_include 'Parent task is invalid', issue.errors.full_messages
156 165 end
157 166
158 167 def assert_visibility_match(user, issues)
159 168 assert_equal issues.collect(&:id).sort, Issue.all.select {|issue| issue.visible?(user)}.collect(&:id).sort
160 169 end
161 170
162 171 def test_visible_scope_for_anonymous
163 172 # Anonymous user should see issues of public projects only
164 173 issues = Issue.visible(User.anonymous).all
165 174 assert issues.any?
166 175 assert_nil issues.detect {|issue| !issue.project.is_public?}
167 176 assert_nil issues.detect {|issue| issue.is_private?}
168 177 assert_visibility_match User.anonymous, issues
169 178 end
170 179
171 180 def test_visible_scope_for_anonymous_without_view_issues_permissions
172 181 # Anonymous user should not see issues without permission
173 182 Role.anonymous.remove_permission!(:view_issues)
174 183 issues = Issue.visible(User.anonymous).all
175 184 assert issues.empty?
176 185 assert_visibility_match User.anonymous, issues
177 186 end
178 187
179 188 def test_anonymous_should_not_see_private_issues_with_issues_visibility_set_to_default
180 189 assert Role.anonymous.update_attribute(:issues_visibility, 'default')
181 190 issue = Issue.generate!(:author => User.anonymous, :assigned_to => User.anonymous, :is_private => true)
182 191 assert_nil Issue.where(:id => issue.id).visible(User.anonymous).first
183 192 assert !issue.visible?(User.anonymous)
184 193 end
185 194
186 195 def test_anonymous_should_not_see_private_issues_with_issues_visibility_set_to_own
187 196 assert Role.anonymous.update_attribute(:issues_visibility, 'own')
188 197 issue = Issue.generate!(:author => User.anonymous, :assigned_to => User.anonymous, :is_private => true)
189 198 assert_nil Issue.where(:id => issue.id).visible(User.anonymous).first
190 199 assert !issue.visible?(User.anonymous)
191 200 end
192 201
193 202 def test_visible_scope_for_non_member
194 203 user = User.find(9)
195 204 assert user.projects.empty?
196 205 # Non member user should see issues of public projects only
197 206 issues = Issue.visible(user).all
198 207 assert issues.any?
199 208 assert_nil issues.detect {|issue| !issue.project.is_public?}
200 209 assert_nil issues.detect {|issue| issue.is_private?}
201 210 assert_visibility_match user, issues
202 211 end
203 212
204 213 def test_visible_scope_for_non_member_with_own_issues_visibility
205 214 Role.non_member.update_attribute :issues_visibility, 'own'
206 215 Issue.create!(:project_id => 1, :tracker_id => 1, :author_id => 9, :subject => 'Issue by non member')
207 216 user = User.find(9)
208 217
209 218 issues = Issue.visible(user).all
210 219 assert issues.any?
211 220 assert_nil issues.detect {|issue| issue.author != user}
212 221 assert_visibility_match user, issues
213 222 end
214 223
215 224 def test_visible_scope_for_non_member_without_view_issues_permissions
216 225 # Non member user should not see issues without permission
217 226 Role.non_member.remove_permission!(:view_issues)
218 227 user = User.find(9)
219 228 assert user.projects.empty?
220 229 issues = Issue.visible(user).all
221 230 assert issues.empty?
222 231 assert_visibility_match user, issues
223 232 end
224 233
225 234 def test_visible_scope_for_member
226 235 user = User.find(9)
227 236 # User should see issues of projects for which he has view_issues permissions only
228 237 Role.non_member.remove_permission!(:view_issues)
229 238 Member.create!(:principal => user, :project_id => 3, :role_ids => [2])
230 239 issues = Issue.visible(user).all
231 240 assert issues.any?
232 241 assert_nil issues.detect {|issue| issue.project_id != 3}
233 242 assert_nil issues.detect {|issue| issue.is_private?}
234 243 assert_visibility_match user, issues
235 244 end
236 245
237 246 def test_visible_scope_for_member_with_groups_should_return_assigned_issues
238 247 user = User.find(8)
239 248 assert user.groups.any?
240 249 Member.create!(:principal => user.groups.first, :project_id => 1, :role_ids => [2])
241 250 Role.non_member.remove_permission!(:view_issues)
242 251
243 252 issue = Issue.create(:project_id => 1, :tracker_id => 1, :author_id => 3,
244 253 :status_id => 1, :priority => IssuePriority.all.first,
245 254 :subject => 'Assignment test',
246 255 :assigned_to => user.groups.first,
247 256 :is_private => true)
248 257
249 258 Role.find(2).update_attribute :issues_visibility, 'default'
250 259 issues = Issue.visible(User.find(8)).all
251 260 assert issues.any?
252 261 assert issues.include?(issue)
253 262
254 263 Role.find(2).update_attribute :issues_visibility, 'own'
255 264 issues = Issue.visible(User.find(8)).all
256 265 assert issues.any?
257 266 assert issues.include?(issue)
258 267 end
259 268
260 269 def test_visible_scope_for_admin
261 270 user = User.find(1)
262 271 user.members.each(&:destroy)
263 272 assert user.projects.empty?
264 273 issues = Issue.visible(user).all
265 274 assert issues.any?
266 275 # Admin should see issues on private projects that he does not belong to
267 276 assert issues.detect {|issue| !issue.project.is_public?}
268 277 # Admin should see private issues of other users
269 278 assert issues.detect {|issue| issue.is_private? && issue.author != user}
270 279 assert_visibility_match user, issues
271 280 end
272 281
273 282 def test_visible_scope_with_project
274 283 project = Project.find(1)
275 284 issues = Issue.visible(User.find(2), :project => project).all
276 285 projects = issues.collect(&:project).uniq
277 286 assert_equal 1, projects.size
278 287 assert_equal project, projects.first
279 288 end
280 289
281 290 def test_visible_scope_with_project_and_subprojects
282 291 project = Project.find(1)
283 292 issues = Issue.visible(User.find(2), :project => project, :with_subprojects => true).all
284 293 projects = issues.collect(&:project).uniq
285 294 assert projects.size > 1
286 295 assert_equal [], projects.select {|p| !p.is_or_is_descendant_of?(project)}
287 296 end
288 297
289 298 def test_visible_and_nested_set_scopes
290 299 assert_equal 0, Issue.find(1).descendants.visible.all.size
291 300 end
292 301
293 302 def test_open_scope
294 303 issues = Issue.open.all
295 304 assert_nil issues.detect(&:closed?)
296 305 end
297 306
298 307 def test_open_scope_with_arg
299 308 issues = Issue.open(false).all
300 309 assert_equal issues, issues.select(&:closed?)
301 310 end
302 311
303 312 def test_errors_full_messages_should_include_custom_fields_errors
304 313 field = IssueCustomField.find_by_name('Database')
305 314
306 315 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1,
307 316 :status_id => 1, :subject => 'test_create',
308 317 :description => 'IssueTest#test_create_with_required_custom_field')
309 318 assert issue.available_custom_fields.include?(field)
310 319 # Invalid value
311 320 issue.custom_field_values = { field.id => 'SQLServer' }
312 321
313 322 assert !issue.valid?
314 323 assert_equal 1, issue.errors.full_messages.size
315 324 assert_equal "Database #{I18n.translate('activerecord.errors.messages.inclusion')}",
316 325 issue.errors.full_messages.first
317 326 end
318 327
319 328 def test_update_issue_with_required_custom_field
320 329 field = IssueCustomField.find_by_name('Database')
321 330 field.update_attribute(:is_required, true)
322 331
323 332 issue = Issue.find(1)
324 333 assert_nil issue.custom_value_for(field)
325 334 assert issue.available_custom_fields.include?(field)
326 335 # No change to custom values, issue can be saved
327 336 assert issue.save
328 337 # Blank value
329 338 issue.custom_field_values = { field.id => '' }
330 339 assert !issue.save
331 340 # Valid value
332 341 issue.custom_field_values = { field.id => 'PostgreSQL' }
333 342 assert issue.save
334 343 issue.reload
335 344 assert_equal 'PostgreSQL', issue.custom_value_for(field).value
336 345 end
337 346
338 347 def test_should_not_update_attributes_if_custom_fields_validation_fails
339 348 issue = Issue.find(1)
340 349 field = IssueCustomField.find_by_name('Database')
341 350 assert issue.available_custom_fields.include?(field)
342 351
343 352 issue.custom_field_values = { field.id => 'Invalid' }
344 353 issue.subject = 'Should be not be saved'
345 354 assert !issue.save
346 355
347 356 issue.reload
348 357 assert_equal "Can't print recipes", issue.subject
349 358 end
350 359
351 360 def test_should_not_recreate_custom_values_objects_on_update
352 361 field = IssueCustomField.find_by_name('Database')
353 362
354 363 issue = Issue.find(1)
355 364 issue.custom_field_values = { field.id => 'PostgreSQL' }
356 365 assert issue.save
357 366 custom_value = issue.custom_value_for(field)
358 367 issue.reload
359 368 issue.custom_field_values = { field.id => 'MySQL' }
360 369 assert issue.save
361 370 issue.reload
362 371 assert_equal custom_value.id, issue.custom_value_for(field).id
363 372 end
364 373
365 374 def test_should_not_update_custom_fields_on_changing_tracker_with_different_custom_fields
366 375 issue = Issue.create!(:project_id => 1, :tracker_id => 1, :author_id => 1,
367 376 :status_id => 1, :subject => 'Test',
368 377 :custom_field_values => {'2' => 'Test'})
369 378 assert !Tracker.find(2).custom_field_ids.include?(2)
370 379
371 380 issue = Issue.find(issue.id)
372 381 issue.attributes = {:tracker_id => 2, :custom_field_values => {'1' => ''}}
373 382
374 383 issue = Issue.find(issue.id)
375 384 custom_value = issue.custom_value_for(2)
376 385 assert_not_nil custom_value
377 386 assert_equal 'Test', custom_value.value
378 387 end
379 388
380 389 def test_assigning_tracker_id_should_reload_custom_fields_values
381 390 issue = Issue.new(:project => Project.find(1))
382 391 assert issue.custom_field_values.empty?
383 392 issue.tracker_id = 1
384 393 assert issue.custom_field_values.any?
385 394 end
386 395
387 396 def test_assigning_attributes_should_assign_project_and_tracker_first
388 397 seq = sequence('seq')
389 398 issue = Issue.new
390 399 issue.expects(:project_id=).in_sequence(seq)
391 400 issue.expects(:tracker_id=).in_sequence(seq)
392 401 issue.expects(:subject=).in_sequence(seq)
393 402 issue.attributes = {:tracker_id => 2, :project_id => 1, :subject => 'Test'}
394 403 end
395 404
396 405 def test_assigning_tracker_and_custom_fields_should_assign_custom_fields
397 406 attributes = ActiveSupport::OrderedHash.new
398 407 attributes['custom_field_values'] = { '1' => 'MySQL' }
399 408 attributes['tracker_id'] = '1'
400 409 issue = Issue.new(:project => Project.find(1))
401 410 issue.attributes = attributes
402 411 assert_equal 'MySQL', issue.custom_field_value(1)
403 412 end
404 413
405 414 def test_should_update_issue_with_disabled_tracker
406 415 p = Project.find(1)
407 416 issue = Issue.find(1)
408 417
409 418 p.trackers.delete(issue.tracker)
410 419 assert !p.trackers.include?(issue.tracker)
411 420
412 421 issue.reload
413 422 issue.subject = 'New subject'
414 423 assert issue.save
415 424 end
416 425
417 426 def test_should_not_set_a_disabled_tracker
418 427 p = Project.find(1)
419 428 p.trackers.delete(Tracker.find(2))
420 429
421 430 issue = Issue.find(1)
422 431 issue.tracker_id = 2
423 432 issue.subject = 'New subject'
424 433 assert !issue.save
425 434 assert_not_nil issue.errors[:tracker_id]
426 435 end
427 436
428 437 def test_category_based_assignment
429 438 issue = Issue.create(:project_id => 1, :tracker_id => 1, :author_id => 3,
430 439 :status_id => 1, :priority => IssuePriority.all.first,
431 440 :subject => 'Assignment test',
432 441 :description => 'Assignment test', :category_id => 1)
433 442 assert_equal IssueCategory.find(1).assigned_to, issue.assigned_to
434 443 end
435 444
436 445 def test_new_statuses_allowed_to
437 446 WorkflowTransition.delete_all
438 447 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1,
439 448 :old_status_id => 1, :new_status_id => 2,
440 449 :author => false, :assignee => false)
441 450 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1,
442 451 :old_status_id => 1, :new_status_id => 3,
443 452 :author => true, :assignee => false)
444 453 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1,
445 454 :new_status_id => 4, :author => false,
446 455 :assignee => true)
447 456 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1,
448 457 :old_status_id => 1, :new_status_id => 5,
449 458 :author => true, :assignee => true)
450 459 status = IssueStatus.find(1)
451 460 role = Role.find(1)
452 461 tracker = Tracker.find(1)
453 462 user = User.find(2)
454 463
455 464 issue = Issue.generate!(:tracker => tracker, :status => status,
456 465 :project_id => 1, :author_id => 1)
457 466 assert_equal [1, 2], issue.new_statuses_allowed_to(user).map(&:id)
458 467
459 468 issue = Issue.generate!(:tracker => tracker, :status => status,
460 469 :project_id => 1, :author => user)
461 470 assert_equal [1, 2, 3, 5], issue.new_statuses_allowed_to(user).map(&:id)
462 471
463 472 issue = Issue.generate!(:tracker => tracker, :status => status,
464 473 :project_id => 1, :author_id => 1,
465 474 :assigned_to => user)
466 475 assert_equal [1, 2, 4, 5], issue.new_statuses_allowed_to(user).map(&:id)
467 476
468 477 issue = Issue.generate!(:tracker => tracker, :status => status,
469 478 :project_id => 1, :author => user,
470 479 :assigned_to => user)
471 480 assert_equal [1, 2, 3, 4, 5], issue.new_statuses_allowed_to(user).map(&:id)
472 481 end
473 482
474 483 def test_new_statuses_allowed_to_should_return_all_transitions_for_admin
475 484 admin = User.find(1)
476 485 issue = Issue.find(1)
477 486 assert !admin.member_of?(issue.project)
478 487 expected_statuses = [issue.status] +
479 488 WorkflowTransition.find_all_by_old_status_id(
480 489 issue.status_id).map(&:new_status).uniq.sort
481 490 assert_equal expected_statuses, issue.new_statuses_allowed_to(admin)
482 491 end
483 492
484 493 def test_new_statuses_allowed_to_should_return_default_and_current_status_when_copying
485 494 issue = Issue.find(1).copy
486 495 assert_equal [1], issue.new_statuses_allowed_to(User.find(2)).map(&:id)
487 496
488 497 issue = Issue.find(2).copy
489 498 assert_equal [1, 2], issue.new_statuses_allowed_to(User.find(2)).map(&:id)
490 499 end
491 500
492 501 def test_safe_attributes_names_should_not_include_disabled_field
493 502 tracker = Tracker.new(:core_fields => %w(assigned_to_id fixed_version_id))
494 503
495 504 issue = Issue.new(:tracker => tracker)
496 505 assert_include 'tracker_id', issue.safe_attribute_names
497 506 assert_include 'status_id', issue.safe_attribute_names
498 507 assert_include 'subject', issue.safe_attribute_names
499 508 assert_include 'description', issue.safe_attribute_names
500 509 assert_include 'custom_field_values', issue.safe_attribute_names
501 510 assert_include 'custom_fields', issue.safe_attribute_names
502 511 assert_include 'lock_version', issue.safe_attribute_names
503 512
504 513 tracker.core_fields.each do |field|
505 514 assert_include field, issue.safe_attribute_names
506 515 end
507 516
508 517 tracker.disabled_core_fields.each do |field|
509 518 assert_not_include field, issue.safe_attribute_names
510 519 end
511 520 end
512 521
513 522 def test_safe_attributes_should_ignore_disabled_fields
514 523 tracker = Tracker.find(1)
515 524 tracker.core_fields = %w(assigned_to_id due_date)
516 525 tracker.save!
517 526
518 527 issue = Issue.new(:tracker => tracker)
519 528 issue.safe_attributes = {'start_date' => '2012-07-14', 'due_date' => '2012-07-14'}
520 529 assert_nil issue.start_date
521 530 assert_equal Date.parse('2012-07-14'), issue.due_date
522 531 end
523 532
524 533 def test_safe_attributes_should_accept_target_tracker_enabled_fields
525 534 source = Tracker.find(1)
526 535 source.core_fields = []
527 536 source.save!
528 537 target = Tracker.find(2)
529 538 target.core_fields = %w(assigned_to_id due_date)
530 539 target.save!
531 540
532 541 issue = Issue.new(:tracker => source)
533 542 issue.safe_attributes = {'tracker_id' => 2, 'due_date' => '2012-07-14'}
534 543 assert_equal target, issue.tracker
535 544 assert_equal Date.parse('2012-07-14'), issue.due_date
536 545 end
537 546
538 547 def test_safe_attributes_should_not_include_readonly_fields
539 548 WorkflowPermission.delete_all
540 549 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1,
541 550 :role_id => 1, :field_name => 'due_date',
542 551 :rule => 'readonly')
543 552 user = User.find(2)
544 553
545 554 issue = Issue.new(:project_id => 1, :tracker_id => 1)
546 555 assert_equal %w(due_date), issue.read_only_attribute_names(user)
547 556 assert_not_include 'due_date', issue.safe_attribute_names(user)
548 557
549 558 issue.send :safe_attributes=, {'start_date' => '2012-07-14', 'due_date' => '2012-07-14'}, user
550 559 assert_equal Date.parse('2012-07-14'), issue.start_date
551 560 assert_nil issue.due_date
552 561 end
553 562
554 563 def test_safe_attributes_should_not_include_readonly_custom_fields
555 564 cf1 = IssueCustomField.create!(:name => 'Writable field',
556 565 :field_format => 'string',
557 566 :is_for_all => true, :tracker_ids => [1])
558 567 cf2 = IssueCustomField.create!(:name => 'Readonly field',
559 568 :field_format => 'string',
560 569 :is_for_all => true, :tracker_ids => [1])
561 570 WorkflowPermission.delete_all
562 571 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1,
563 572 :role_id => 1, :field_name => cf2.id.to_s,
564 573 :rule => 'readonly')
565 574 user = User.find(2)
566 575 issue = Issue.new(:project_id => 1, :tracker_id => 1)
567 576 assert_equal [cf2.id.to_s], issue.read_only_attribute_names(user)
568 577 assert_not_include cf2.id.to_s, issue.safe_attribute_names(user)
569 578
570 579 issue.send :safe_attributes=, {'custom_field_values' => {
571 580 cf1.id.to_s => 'value1', cf2.id.to_s => 'value2'
572 581 }}, user
573 582 assert_equal 'value1', issue.custom_field_value(cf1)
574 583 assert_nil issue.custom_field_value(cf2)
575 584
576 585 issue.send :safe_attributes=, {'custom_fields' => [
577 586 {'id' => cf1.id.to_s, 'value' => 'valuea'},
578 587 {'id' => cf2.id.to_s, 'value' => 'valueb'}
579 588 ]}, user
580 589 assert_equal 'valuea', issue.custom_field_value(cf1)
581 590 assert_nil issue.custom_field_value(cf2)
582 591 end
583 592
584 593 def test_editable_custom_field_values_should_return_non_readonly_custom_values
585 594 cf1 = IssueCustomField.create!(:name => 'Writable field', :field_format => 'string',
586 595 :is_for_all => true, :tracker_ids => [1, 2])
587 596 cf2 = IssueCustomField.create!(:name => 'Readonly field', :field_format => 'string',
588 597 :is_for_all => true, :tracker_ids => [1, 2])
589 598 WorkflowPermission.delete_all
590 599 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1,
591 600 :field_name => cf2.id.to_s, :rule => 'readonly')
592 601 user = User.find(2)
593 602
594 603 issue = Issue.new(:project_id => 1, :tracker_id => 1)
595 604 values = issue.editable_custom_field_values(user)
596 605 assert values.detect {|value| value.custom_field == cf1}
597 606 assert_nil values.detect {|value| value.custom_field == cf2}
598 607
599 608 issue.tracker_id = 2
600 609 values = issue.editable_custom_field_values(user)
601 610 assert values.detect {|value| value.custom_field == cf1}
602 611 assert values.detect {|value| value.custom_field == cf2}
603 612 end
604 613
605 614 def test_safe_attributes_should_accept_target_tracker_writable_fields
606 615 WorkflowPermission.delete_all
607 616 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1,
608 617 :role_id => 1, :field_name => 'due_date',
609 618 :rule => 'readonly')
610 619 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2,
611 620 :role_id => 1, :field_name => 'start_date',
612 621 :rule => 'readonly')
613 622 user = User.find(2)
614 623
615 624 issue = Issue.new(:project_id => 1, :tracker_id => 1, :status_id => 1)
616 625
617 626 issue.send :safe_attributes=, {'start_date' => '2012-07-12',
618 627 'due_date' => '2012-07-14'}, user
619 628 assert_equal Date.parse('2012-07-12'), issue.start_date
620 629 assert_nil issue.due_date
621 630
622 631 issue.send :safe_attributes=, {'start_date' => '2012-07-15',
623 632 'due_date' => '2012-07-16',
624 633 'tracker_id' => 2}, user
625 634 assert_equal Date.parse('2012-07-12'), issue.start_date
626 635 assert_equal Date.parse('2012-07-16'), issue.due_date
627 636 end
628 637
629 638 def test_safe_attributes_should_accept_target_status_writable_fields
630 639 WorkflowPermission.delete_all
631 640 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1,
632 641 :role_id => 1, :field_name => 'due_date',
633 642 :rule => 'readonly')
634 643 WorkflowPermission.create!(:old_status_id => 2, :tracker_id => 1,
635 644 :role_id => 1, :field_name => 'start_date',
636 645 :rule => 'readonly')
637 646 user = User.find(2)
638 647
639 648 issue = Issue.new(:project_id => 1, :tracker_id => 1, :status_id => 1)
640 649
641 650 issue.send :safe_attributes=, {'start_date' => '2012-07-12',
642 651 'due_date' => '2012-07-14'},
643 652 user
644 653 assert_equal Date.parse('2012-07-12'), issue.start_date
645 654 assert_nil issue.due_date
646 655
647 656 issue.send :safe_attributes=, {'start_date' => '2012-07-15',
648 657 'due_date' => '2012-07-16',
649 658 'status_id' => 2},
650 659 user
651 660 assert_equal Date.parse('2012-07-12'), issue.start_date
652 661 assert_equal Date.parse('2012-07-16'), issue.due_date
653 662 end
654 663
655 664 def test_required_attributes_should_be_validated
656 665 cf = IssueCustomField.create!(:name => 'Foo', :field_format => 'string',
657 666 :is_for_all => true, :tracker_ids => [1, 2])
658 667
659 668 WorkflowPermission.delete_all
660 669 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1,
661 670 :role_id => 1, :field_name => 'due_date',
662 671 :rule => 'required')
663 672 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1,
664 673 :role_id => 1, :field_name => 'category_id',
665 674 :rule => 'required')
666 675 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1,
667 676 :role_id => 1, :field_name => cf.id.to_s,
668 677 :rule => 'required')
669 678
670 679 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2,
671 680 :role_id => 1, :field_name => 'start_date',
672 681 :rule => 'required')
673 682 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2,
674 683 :role_id => 1, :field_name => cf.id.to_s,
675 684 :rule => 'required')
676 685 user = User.find(2)
677 686
678 687 issue = Issue.new(:project_id => 1, :tracker_id => 1,
679 688 :status_id => 1, :subject => 'Required fields',
680 689 :author => user)
681 690 assert_equal [cf.id.to_s, "category_id", "due_date"],
682 691 issue.required_attribute_names(user).sort
683 692 assert !issue.save, "Issue was saved"
684 693 assert_equal ["Category can't be blank", "Due date can't be blank", "Foo can't be blank"],
685 694 issue.errors.full_messages.sort
686 695
687 696 issue.tracker_id = 2
688 697 assert_equal [cf.id.to_s, "start_date"], issue.required_attribute_names(user).sort
689 698 assert !issue.save, "Issue was saved"
690 699 assert_equal ["Foo can't be blank", "Start date can't be blank"],
691 700 issue.errors.full_messages.sort
692 701
693 702 issue.start_date = Date.today
694 703 issue.custom_field_values = {cf.id.to_s => 'bar'}
695 704 assert issue.save
696 705 end
697 706
698 707 def test_required_attribute_names_for_multiple_roles_should_intersect_rules
699 708 WorkflowPermission.delete_all
700 709 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1,
701 710 :role_id => 1, :field_name => 'due_date',
702 711 :rule => 'required')
703 712 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1,
704 713 :role_id => 1, :field_name => 'start_date',
705 714 :rule => 'required')
706 715 user = User.find(2)
707 716 member = Member.find(1)
708 717 issue = Issue.new(:project_id => 1, :tracker_id => 1, :status_id => 1)
709 718
710 719 assert_equal %w(due_date start_date), issue.required_attribute_names(user).sort
711 720
712 721 member.role_ids = [1, 2]
713 722 member.save!
714 723 assert_equal [], issue.required_attribute_names(user.reload)
715 724
716 725 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1,
717 726 :role_id => 2, :field_name => 'due_date',
718 727 :rule => 'required')
719 728 assert_equal %w(due_date), issue.required_attribute_names(user)
720 729
721 730 member.role_ids = [1, 2, 3]
722 731 member.save!
723 732 assert_equal [], issue.required_attribute_names(user.reload)
724 733
725 734 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1,
726 735 :role_id => 2, :field_name => 'due_date',
727 736 :rule => 'readonly')
728 737 # required + readonly => required
729 738 assert_equal %w(due_date), issue.required_attribute_names(user)
730 739 end
731 740
732 741 def test_read_only_attribute_names_for_multiple_roles_should_intersect_rules
733 742 WorkflowPermission.delete_all
734 743 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1,
735 744 :role_id => 1, :field_name => 'due_date',
736 745 :rule => 'readonly')
737 746 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1,
738 747 :role_id => 1, :field_name => 'start_date',
739 748 :rule => 'readonly')
740 749 user = User.find(2)
741 750 member = Member.find(1)
742 751 issue = Issue.new(:project_id => 1, :tracker_id => 1, :status_id => 1)
743 752
744 753 assert_equal %w(due_date start_date), issue.read_only_attribute_names(user).sort
745 754
746 755 member.role_ids = [1, 2]
747 756 member.save!
748 757 assert_equal [], issue.read_only_attribute_names(user.reload)
749 758
750 759 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1,
751 760 :role_id => 2, :field_name => 'due_date',
752 761 :rule => 'readonly')
753 762 assert_equal %w(due_date), issue.read_only_attribute_names(user)
754 763 end
755 764
756 765 def test_copy
757 766 issue = Issue.new.copy_from(1)
758 767 assert issue.copy?
759 768 assert issue.save
760 769 issue.reload
761 770 orig = Issue.find(1)
762 771 assert_equal orig.subject, issue.subject
763 772 assert_equal orig.tracker, issue.tracker
764 773 assert_equal "125", issue.custom_value_for(2).value
765 774 end
766 775
767 776 def test_copy_should_copy_status
768 777 orig = Issue.find(8)
769 778 assert orig.status != IssueStatus.default
770 779
771 780 issue = Issue.new.copy_from(orig)
772 781 assert issue.save
773 782 issue.reload
774 783 assert_equal orig.status, issue.status
775 784 end
776 785
777 786 def test_copy_should_add_relation_with_copied_issue
778 787 copied = Issue.find(1)
779 788 issue = Issue.new.copy_from(copied)
780 789 assert issue.save
781 790 issue.reload
782 791
783 792 assert_equal 1, issue.relations.size
784 793 relation = issue.relations.first
785 794 assert_equal 'copied_to', relation.relation_type
786 795 assert_equal copied, relation.issue_from
787 796 assert_equal issue, relation.issue_to
788 797 end
789 798
790 799 def test_copy_should_copy_subtasks
791 800 issue = Issue.generate_with_descendants!
792 801
793 802 copy = issue.reload.copy
794 803 copy.author = User.find(7)
795 804 assert_difference 'Issue.count', 1+issue.descendants.count do
796 805 assert copy.save
797 806 end
798 807 copy.reload
799 808 assert_equal %w(Child1 Child2), copy.children.map(&:subject).sort
800 809 child_copy = copy.children.detect {|c| c.subject == 'Child1'}
801 810 assert_equal %w(Child11), child_copy.children.map(&:subject).sort
802 811 assert_equal copy.author, child_copy.author
803 812 end
804 813
805 814 def test_copy_should_copy_subtasks_to_target_project
806 815 issue = Issue.generate_with_descendants!
807 816
808 817 copy = issue.copy(:project_id => 3)
809 818 assert_difference 'Issue.count', 1+issue.descendants.count do
810 819 assert copy.save
811 820 end
812 821 assert_equal [3], copy.reload.descendants.map(&:project_id).uniq
813 822 end
814 823
815 824 def test_copy_should_not_copy_subtasks_twice_when_saving_twice
816 825 issue = Issue.generate_with_descendants!
817 826
818 827 copy = issue.reload.copy
819 828 assert_difference 'Issue.count', 1+issue.descendants.count do
820 829 assert copy.save
821 830 assert copy.save
822 831 end
823 832 end
824 833
825 834 def test_should_not_call_after_project_change_on_creation
826 835 issue = Issue.new(:project_id => 1, :tracker_id => 1, :status_id => 1,
827 836 :subject => 'Test', :author_id => 1)
828 837 issue.expects(:after_project_change).never
829 838 issue.save!
830 839 end
831 840
832 841 def test_should_not_call_after_project_change_on_update
833 842 issue = Issue.find(1)
834 843 issue.project = Project.find(1)
835 844 issue.subject = 'No project change'
836 845 issue.expects(:after_project_change).never
837 846 issue.save!
838 847 end
839 848
840 849 def test_should_call_after_project_change_on_project_change
841 850 issue = Issue.find(1)
842 851 issue.project = Project.find(2)
843 852 issue.expects(:after_project_change).once
844 853 issue.save!
845 854 end
846 855
847 856 def test_adding_journal_should_update_timestamp
848 857 issue = Issue.find(1)
849 858 updated_on_was = issue.updated_on
850 859
851 860 issue.init_journal(User.first, "Adding notes")
852 861 assert_difference 'Journal.count' do
853 862 assert issue.save
854 863 end
855 864 issue.reload
856 865
857 866 assert_not_equal updated_on_was, issue.updated_on
858 867 end
859 868
860 869 def test_should_close_duplicates
861 870 # Create 3 issues
862 871 issue1 = Issue.generate!
863 872 issue2 = Issue.generate!
864 873 issue3 = Issue.generate!
865 874
866 875 # 2 is a dupe of 1
867 876 IssueRelation.create!(:issue_from => issue2, :issue_to => issue1,
868 877 :relation_type => IssueRelation::TYPE_DUPLICATES)
869 878 # And 3 is a dupe of 2
870 879 IssueRelation.create!(:issue_from => issue3, :issue_to => issue2,
871 880 :relation_type => IssueRelation::TYPE_DUPLICATES)
872 881 # And 3 is a dupe of 1 (circular duplicates)
873 882 IssueRelation.create!(:issue_from => issue3, :issue_to => issue1,
874 883 :relation_type => IssueRelation::TYPE_DUPLICATES)
875 884
876 885 assert issue1.reload.duplicates.include?(issue2)
877 886
878 887 # Closing issue 1
879 888 issue1.init_journal(User.first, "Closing issue1")
880 889 issue1.status = IssueStatus.where(:is_closed => true).first
881 890 assert issue1.save
882 891 # 2 and 3 should be also closed
883 892 assert issue2.reload.closed?
884 893 assert issue3.reload.closed?
885 894 end
886 895
887 896 def test_should_not_close_duplicated_issue
888 897 issue1 = Issue.generate!
889 898 issue2 = Issue.generate!
890 899
891 900 # 2 is a dupe of 1
892 901 IssueRelation.create(:issue_from => issue2, :issue_to => issue1,
893 902 :relation_type => IssueRelation::TYPE_DUPLICATES)
894 903 # 2 is a dup of 1 but 1 is not a duplicate of 2
895 904 assert !issue2.reload.duplicates.include?(issue1)
896 905
897 906 # Closing issue 2
898 907 issue2.init_journal(User.first, "Closing issue2")
899 908 issue2.status = IssueStatus.where(:is_closed => true).first
900 909 assert issue2.save
901 910 # 1 should not be also closed
902 911 assert !issue1.reload.closed?
903 912 end
904 913
905 914 def test_assignable_versions
906 915 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1,
907 916 :status_id => 1, :fixed_version_id => 1,
908 917 :subject => 'New issue')
909 918 assert_equal ['open'], issue.assignable_versions.collect(&:status).uniq
910 919 end
911 920
912 921 def test_should_not_be_able_to_assign_a_new_issue_to_a_closed_version
913 922 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1,
914 923 :status_id => 1, :fixed_version_id => 1,
915 924 :subject => 'New issue')
916 925 assert !issue.save
917 926 assert_not_nil issue.errors[:fixed_version_id]
918 927 end
919 928
920 929 def test_should_not_be_able_to_assign_a_new_issue_to_a_locked_version
921 930 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1,
922 931 :status_id => 1, :fixed_version_id => 2,
923 932 :subject => 'New issue')
924 933 assert !issue.save
925 934 assert_not_nil issue.errors[:fixed_version_id]
926 935 end
927 936
928 937 def test_should_be_able_to_assign_a_new_issue_to_an_open_version
929 938 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1,
930 939 :status_id => 1, :fixed_version_id => 3,
931 940 :subject => 'New issue')
932 941 assert issue.save
933 942 end
934 943
935 944 def test_should_be_able_to_update_an_issue_assigned_to_a_closed_version
936 945 issue = Issue.find(11)
937 946 assert_equal 'closed', issue.fixed_version.status
938 947 issue.subject = 'Subject changed'
939 948 assert issue.save
940 949 end
941 950
942 951 def test_should_not_be_able_to_reopen_an_issue_assigned_to_a_closed_version
943 952 issue = Issue.find(11)
944 953 issue.status_id = 1
945 954 assert !issue.save
946 955 assert_not_nil issue.errors[:base]
947 956 end
948 957
949 958 def test_should_be_able_to_reopen_and_reassign_an_issue_assigned_to_a_closed_version
950 959 issue = Issue.find(11)
951 960 issue.status_id = 1
952 961 issue.fixed_version_id = 3
953 962 assert issue.save
954 963 end
955 964
956 965 def test_should_be_able_to_reopen_an_issue_assigned_to_a_locked_version
957 966 issue = Issue.find(12)
958 967 assert_equal 'locked', issue.fixed_version.status
959 968 issue.status_id = 1
960 969 assert issue.save
961 970 end
962 971
963 972 def test_should_not_be_able_to_keep_unshared_version_when_changing_project
964 973 issue = Issue.find(2)
965 974 assert_equal 2, issue.fixed_version_id
966 975 issue.project_id = 3
967 976 assert_nil issue.fixed_version_id
968 977 issue.fixed_version_id = 2
969 978 assert !issue.save
970 979 assert_include 'Target version is not included in the list', issue.errors.full_messages
971 980 end
972 981
973 982 def test_should_keep_shared_version_when_changing_project
974 983 Version.find(2).update_attribute :sharing, 'tree'
975 984
976 985 issue = Issue.find(2)
977 986 assert_equal 2, issue.fixed_version_id
978 987 issue.project_id = 3
979 988 assert_equal 2, issue.fixed_version_id
980 989 assert issue.save
981 990 end
982 991
983 992 def test_allowed_target_projects_on_move_should_include_projects_with_issue_tracking_enabled
984 993 assert_include Project.find(2), Issue.allowed_target_projects_on_move(User.find(2))
985 994 end
986 995
987 996 def test_allowed_target_projects_on_move_should_not_include_projects_with_issue_tracking_disabled
988 997 Project.find(2).disable_module! :issue_tracking
989 998 assert_not_include Project.find(2), Issue.allowed_target_projects_on_move(User.find(2))
990 999 end
991 1000
992 1001 def test_move_to_another_project_with_same_category
993 1002 issue = Issue.find(1)
994 1003 issue.project = Project.find(2)
995 1004 assert issue.save
996 1005 issue.reload
997 1006 assert_equal 2, issue.project_id
998 1007 # Category changes
999 1008 assert_equal 4, issue.category_id
1000 1009 # Make sure time entries were move to the target project
1001 1010 assert_equal 2, issue.time_entries.first.project_id
1002 1011 end
1003 1012
1004 1013 def test_move_to_another_project_without_same_category
1005 1014 issue = Issue.find(2)
1006 1015 issue.project = Project.find(2)
1007 1016 assert issue.save
1008 1017 issue.reload
1009 1018 assert_equal 2, issue.project_id
1010 1019 # Category cleared
1011 1020 assert_nil issue.category_id
1012 1021 end
1013 1022
1014 1023 def test_move_to_another_project_should_clear_fixed_version_when_not_shared
1015 1024 issue = Issue.find(1)
1016 1025 issue.update_attribute(:fixed_version_id, 1)
1017 1026 issue.project = Project.find(2)
1018 1027 assert issue.save
1019 1028 issue.reload
1020 1029 assert_equal 2, issue.project_id
1021 1030 # Cleared fixed_version
1022 1031 assert_equal nil, issue.fixed_version
1023 1032 end
1024 1033
1025 1034 def test_move_to_another_project_should_keep_fixed_version_when_shared_with_the_target_project
1026 1035 issue = Issue.find(1)
1027 1036 issue.update_attribute(:fixed_version_id, 4)
1028 1037 issue.project = Project.find(5)
1029 1038 assert issue.save
1030 1039 issue.reload
1031 1040 assert_equal 5, issue.project_id
1032 1041 # Keep fixed_version
1033 1042 assert_equal 4, issue.fixed_version_id
1034 1043 end
1035 1044
1036 1045 def test_move_to_another_project_should_clear_fixed_version_when_not_shared_with_the_target_project
1037 1046 issue = Issue.find(1)
1038 1047 issue.update_attribute(:fixed_version_id, 1)
1039 1048 issue.project = Project.find(5)
1040 1049 assert issue.save
1041 1050 issue.reload
1042 1051 assert_equal 5, issue.project_id
1043 1052 # Cleared fixed_version
1044 1053 assert_equal nil, issue.fixed_version
1045 1054 end
1046 1055
1047 1056 def test_move_to_another_project_should_keep_fixed_version_when_shared_systemwide
1048 1057 issue = Issue.find(1)
1049 1058 issue.update_attribute(:fixed_version_id, 7)
1050 1059 issue.project = Project.find(2)
1051 1060 assert issue.save
1052 1061 issue.reload
1053 1062 assert_equal 2, issue.project_id
1054 1063 # Keep fixed_version
1055 1064 assert_equal 7, issue.fixed_version_id
1056 1065 end
1057 1066
1058 1067 def test_move_to_another_project_should_keep_parent_if_valid
1059 1068 issue = Issue.find(1)
1060 1069 issue.update_attribute(:parent_issue_id, 2)
1061 1070 issue.project = Project.find(3)
1062 1071 assert issue.save
1063 1072 issue.reload
1064 1073 assert_equal 2, issue.parent_id
1065 1074 end
1066 1075
1067 1076 def test_move_to_another_project_should_clear_parent_if_not_valid
1068 1077 issue = Issue.find(1)
1069 1078 issue.update_attribute(:parent_issue_id, 2)
1070 1079 issue.project = Project.find(2)
1071 1080 assert issue.save
1072 1081 issue.reload
1073 1082 assert_nil issue.parent_id
1074 1083 end
1075 1084
1076 1085 def test_move_to_another_project_with_disabled_tracker
1077 1086 issue = Issue.find(1)
1078 1087 target = Project.find(2)
1079 1088 target.tracker_ids = [3]
1080 1089 target.save
1081 1090 issue.project = target
1082 1091 assert issue.save
1083 1092 issue.reload
1084 1093 assert_equal 2, issue.project_id
1085 1094 assert_equal 3, issue.tracker_id
1086 1095 end
1087 1096
1088 1097 def test_copy_to_the_same_project
1089 1098 issue = Issue.find(1)
1090 1099 copy = issue.copy
1091 1100 assert_difference 'Issue.count' do
1092 1101 copy.save!
1093 1102 end
1094 1103 assert_kind_of Issue, copy
1095 1104 assert_equal issue.project, copy.project
1096 1105 assert_equal "125", copy.custom_value_for(2).value
1097 1106 end
1098 1107
1099 1108 def test_copy_to_another_project_and_tracker
1100 1109 issue = Issue.find(1)
1101 1110 copy = issue.copy(:project_id => 3, :tracker_id => 2)
1102 1111 assert_difference 'Issue.count' do
1103 1112 copy.save!
1104 1113 end
1105 1114 copy.reload
1106 1115 assert_kind_of Issue, copy
1107 1116 assert_equal Project.find(3), copy.project
1108 1117 assert_equal Tracker.find(2), copy.tracker
1109 1118 # Custom field #2 is not associated with target tracker
1110 1119 assert_nil copy.custom_value_for(2)
1111 1120 end
1112 1121
1113 1122 context "#copy" do
1114 1123 setup do
1115 1124 @issue = Issue.find(1)
1116 1125 end
1117 1126
1118 1127 should "not create a journal" do
1119 1128 copy = @issue.copy(:project_id => 3, :tracker_id => 2, :assigned_to_id => 3)
1120 1129 copy.save!
1121 1130 assert_equal 0, copy.reload.journals.size
1122 1131 end
1123 1132
1124 1133 should "allow assigned_to changes" do
1125 1134 copy = @issue.copy(:project_id => 3, :tracker_id => 2, :assigned_to_id => 3)
1126 1135 assert_equal 3, copy.assigned_to_id
1127 1136 end
1128 1137
1129 1138 should "allow status changes" do
1130 1139 copy = @issue.copy(:project_id => 3, :tracker_id => 2, :status_id => 2)
1131 1140 assert_equal 2, copy.status_id
1132 1141 end
1133 1142
1134 1143 should "allow start date changes" do
1135 1144 date = Date.today
1136 1145 copy = @issue.copy(:project_id => 3, :tracker_id => 2, :start_date => date)
1137 1146 assert_equal date, copy.start_date
1138 1147 end
1139 1148
1140 1149 should "allow due date changes" do
1141 1150 date = Date.today
1142 1151 copy = @issue.copy(:project_id => 3, :tracker_id => 2, :due_date => date)
1143 1152 assert_equal date, copy.due_date
1144 1153 end
1145 1154
1146 1155 should "set current user as author" do
1147 1156 User.current = User.find(9)
1148 1157 copy = @issue.copy(:project_id => 3, :tracker_id => 2)
1149 1158 assert_equal User.current, copy.author
1150 1159 end
1151 1160
1152 1161 should "create a journal with notes" do
1153 1162 date = Date.today
1154 1163 notes = "Notes added when copying"
1155 1164 copy = @issue.copy(:project_id => 3, :tracker_id => 2, :start_date => date)
1156 1165 copy.init_journal(User.current, notes)
1157 1166 copy.save!
1158 1167
1159 1168 assert_equal 1, copy.journals.size
1160 1169 journal = copy.journals.first
1161 1170 assert_equal 0, journal.details.size
1162 1171 assert_equal notes, journal.notes
1163 1172 end
1164 1173 end
1165 1174
1166 1175 def test_valid_parent_project
1167 1176 issue = Issue.find(1)
1168 1177 issue_in_same_project = Issue.find(2)
1169 1178 issue_in_child_project = Issue.find(5)
1170 1179 issue_in_grandchild_project = Issue.generate!(:project_id => 6, :tracker_id => 1)
1171 1180 issue_in_other_child_project = Issue.find(6)
1172 1181 issue_in_different_tree = Issue.find(4)
1173 1182
1174 1183 with_settings :cross_project_subtasks => '' do
1175 1184 assert_equal true, issue.valid_parent_project?(issue_in_same_project)
1176 1185 assert_equal false, issue.valid_parent_project?(issue_in_child_project)
1177 1186 assert_equal false, issue.valid_parent_project?(issue_in_grandchild_project)
1178 1187 assert_equal false, issue.valid_parent_project?(issue_in_different_tree)
1179 1188 end
1180 1189
1181 1190 with_settings :cross_project_subtasks => 'system' do
1182 1191 assert_equal true, issue.valid_parent_project?(issue_in_same_project)
1183 1192 assert_equal true, issue.valid_parent_project?(issue_in_child_project)
1184 1193 assert_equal true, issue.valid_parent_project?(issue_in_different_tree)
1185 1194 end
1186 1195
1187 1196 with_settings :cross_project_subtasks => 'tree' do
1188 1197 assert_equal true, issue.valid_parent_project?(issue_in_same_project)
1189 1198 assert_equal true, issue.valid_parent_project?(issue_in_child_project)
1190 1199 assert_equal true, issue.valid_parent_project?(issue_in_grandchild_project)
1191 1200 assert_equal false, issue.valid_parent_project?(issue_in_different_tree)
1192 1201
1193 1202 assert_equal true, issue_in_child_project.valid_parent_project?(issue_in_same_project)
1194 1203 assert_equal true, issue_in_child_project.valid_parent_project?(issue_in_other_child_project)
1195 1204 end
1196 1205
1197 1206 with_settings :cross_project_subtasks => 'descendants' do
1198 1207 assert_equal true, issue.valid_parent_project?(issue_in_same_project)
1199 1208 assert_equal false, issue.valid_parent_project?(issue_in_child_project)
1200 1209 assert_equal false, issue.valid_parent_project?(issue_in_grandchild_project)
1201 1210 assert_equal false, issue.valid_parent_project?(issue_in_different_tree)
1202 1211
1203 1212 assert_equal true, issue_in_child_project.valid_parent_project?(issue)
1204 1213 assert_equal false, issue_in_child_project.valid_parent_project?(issue_in_other_child_project)
1205 1214 end
1206 1215 end
1207 1216
1208 1217 def test_recipients_should_include_previous_assignee
1209 1218 user = User.find(3)
1210 1219 user.members.update_all ["mail_notification = ?", false]
1211 1220 user.update_attribute :mail_notification, 'only_assigned'
1212 1221
1213 1222 issue = Issue.find(2)
1214 1223 issue.assigned_to = nil
1215 1224 assert_include user.mail, issue.recipients
1216 1225 issue.save!
1217 1226 assert !issue.recipients.include?(user.mail)
1218 1227 end
1219 1228
1220 1229 def test_recipients_should_not_include_users_that_cannot_view_the_issue
1221 1230 issue = Issue.find(12)
1222 1231 assert issue.recipients.include?(issue.author.mail)
1223 1232 # copy the issue to a private project
1224 1233 copy = issue.copy(:project_id => 5, :tracker_id => 2)
1225 1234 # author is not a member of project anymore
1226 1235 assert !copy.recipients.include?(copy.author.mail)
1227 1236 end
1228 1237
1229 1238 def test_recipients_should_include_the_assigned_group_members
1230 1239 group_member = User.generate!
1231 1240 group = Group.generate!
1232 1241 group.users << group_member
1233 1242
1234 1243 issue = Issue.find(12)
1235 1244 issue.assigned_to = group
1236 1245 assert issue.recipients.include?(group_member.mail)
1237 1246 end
1238 1247
1239 1248 def test_watcher_recipients_should_not_include_users_that_cannot_view_the_issue
1240 1249 user = User.find(3)
1241 1250 issue = Issue.find(9)
1242 1251 Watcher.create!(:user => user, :watchable => issue)
1243 1252 assert issue.watched_by?(user)
1244 1253 assert !issue.watcher_recipients.include?(user.mail)
1245 1254 end
1246 1255
1247 1256 def test_issue_destroy
1248 1257 Issue.find(1).destroy
1249 1258 assert_nil Issue.find_by_id(1)
1250 1259 assert_nil TimeEntry.find_by_issue_id(1)
1251 1260 end
1252 1261
1253 1262 def test_destroying_a_deleted_issue_should_not_raise_an_error
1254 1263 issue = Issue.find(1)
1255 1264 Issue.find(1).destroy
1256 1265
1257 1266 assert_nothing_raised do
1258 1267 assert_no_difference 'Issue.count' do
1259 1268 issue.destroy
1260 1269 end
1261 1270 assert issue.destroyed?
1262 1271 end
1263 1272 end
1264 1273
1265 1274 def test_destroying_a_stale_issue_should_not_raise_an_error
1266 1275 issue = Issue.find(1)
1267 1276 Issue.find(1).update_attribute :subject, "Updated"
1268 1277
1269 1278 assert_nothing_raised do
1270 1279 assert_difference 'Issue.count', -1 do
1271 1280 issue.destroy
1272 1281 end
1273 1282 assert issue.destroyed?
1274 1283 end
1275 1284 end
1276 1285
1277 1286 def test_blocked
1278 1287 blocked_issue = Issue.find(9)
1279 1288 blocking_issue = Issue.find(10)
1280 1289
1281 1290 assert blocked_issue.blocked?
1282 1291 assert !blocking_issue.blocked?
1283 1292 end
1284 1293
1285 1294 def test_blocked_issues_dont_allow_closed_statuses
1286 1295 blocked_issue = Issue.find(9)
1287 1296
1288 1297 allowed_statuses = blocked_issue.new_statuses_allowed_to(users(:users_002))
1289 1298 assert !allowed_statuses.empty?
1290 1299 closed_statuses = allowed_statuses.select {|st| st.is_closed?}
1291 1300 assert closed_statuses.empty?
1292 1301 end
1293 1302
1294 1303 def test_unblocked_issues_allow_closed_statuses
1295 1304 blocking_issue = Issue.find(10)
1296 1305
1297 1306 allowed_statuses = blocking_issue.new_statuses_allowed_to(users(:users_002))
1298 1307 assert !allowed_statuses.empty?
1299 1308 closed_statuses = allowed_statuses.select {|st| st.is_closed?}
1300 1309 assert !closed_statuses.empty?
1301 1310 end
1302 1311
1303 1312 def test_reschedule_an_issue_without_dates
1304 1313 with_settings :non_working_week_days => [] do
1305 1314 issue = Issue.new(:start_date => nil, :due_date => nil)
1306 1315 issue.reschedule_on '2012-10-09'.to_date
1307 1316 assert_equal '2012-10-09'.to_date, issue.start_date
1308 1317 assert_equal '2012-10-09'.to_date, issue.due_date
1309 1318 end
1310 1319
1311 1320 with_settings :non_working_week_days => %w(6 7) do
1312 1321 issue = Issue.new(:start_date => nil, :due_date => nil)
1313 1322 issue.reschedule_on '2012-10-09'.to_date
1314 1323 assert_equal '2012-10-09'.to_date, issue.start_date
1315 1324 assert_equal '2012-10-09'.to_date, issue.due_date
1316 1325
1317 1326 issue = Issue.new(:start_date => nil, :due_date => nil)
1318 1327 issue.reschedule_on '2012-10-13'.to_date
1319 1328 assert_equal '2012-10-15'.to_date, issue.start_date
1320 1329 assert_equal '2012-10-15'.to_date, issue.due_date
1321 1330 end
1322 1331 end
1323 1332
1324 1333 def test_reschedule_an_issue_with_start_date
1325 1334 with_settings :non_working_week_days => [] do
1326 1335 issue = Issue.new(:start_date => '2012-10-09', :due_date => nil)
1327 1336 issue.reschedule_on '2012-10-13'.to_date
1328 1337 assert_equal '2012-10-13'.to_date, issue.start_date
1329 1338 assert_equal '2012-10-13'.to_date, issue.due_date
1330 1339 end
1331 1340
1332 1341 with_settings :non_working_week_days => %w(6 7) do
1333 1342 issue = Issue.new(:start_date => '2012-10-09', :due_date => nil)
1334 1343 issue.reschedule_on '2012-10-11'.to_date
1335 1344 assert_equal '2012-10-11'.to_date, issue.start_date
1336 1345 assert_equal '2012-10-11'.to_date, issue.due_date
1337 1346
1338 1347 issue = Issue.new(:start_date => '2012-10-09', :due_date => nil)
1339 1348 issue.reschedule_on '2012-10-13'.to_date
1340 1349 assert_equal '2012-10-15'.to_date, issue.start_date
1341 1350 assert_equal '2012-10-15'.to_date, issue.due_date
1342 1351 end
1343 1352 end
1344 1353
1345 1354 def test_reschedule_an_issue_with_start_and_due_dates
1346 1355 with_settings :non_working_week_days => [] do
1347 1356 issue = Issue.new(:start_date => '2012-10-09', :due_date => '2012-10-15')
1348 1357 issue.reschedule_on '2012-10-13'.to_date
1349 1358 assert_equal '2012-10-13'.to_date, issue.start_date
1350 1359 assert_equal '2012-10-19'.to_date, issue.due_date
1351 1360 end
1352 1361
1353 1362 with_settings :non_working_week_days => %w(6 7) do
1354 1363 issue = Issue.new(:start_date => '2012-10-09', :due_date => '2012-10-19') # 8 working days
1355 1364 issue.reschedule_on '2012-10-11'.to_date
1356 1365 assert_equal '2012-10-11'.to_date, issue.start_date
1357 1366 assert_equal '2012-10-23'.to_date, issue.due_date
1358 1367
1359 1368 issue = Issue.new(:start_date => '2012-10-09', :due_date => '2012-10-19')
1360 1369 issue.reschedule_on '2012-10-13'.to_date
1361 1370 assert_equal '2012-10-15'.to_date, issue.start_date
1362 1371 assert_equal '2012-10-25'.to_date, issue.due_date
1363 1372 end
1364 1373 end
1365 1374
1366 1375 def test_rescheduling_an_issue_to_a_later_due_date_should_reschedule_following_issue
1367 1376 issue1 = Issue.generate!(:start_date => '2012-10-15', :due_date => '2012-10-17')
1368 1377 issue2 = Issue.generate!(:start_date => '2012-10-15', :due_date => '2012-10-17')
1369 1378 IssueRelation.create!(:issue_from => issue1, :issue_to => issue2,
1370 1379 :relation_type => IssueRelation::TYPE_PRECEDES)
1371 1380 assert_equal Date.parse('2012-10-18'), issue2.reload.start_date
1372 1381
1373 1382 issue1.due_date = '2012-10-23'
1374 1383 issue1.save!
1375 1384 issue2.reload
1376 1385 assert_equal Date.parse('2012-10-24'), issue2.start_date
1377 1386 assert_equal Date.parse('2012-10-26'), issue2.due_date
1378 1387 end
1379 1388
1380 1389 def test_rescheduling_an_issue_to_an_earlier_due_date_should_reschedule_following_issue
1381 1390 issue1 = Issue.generate!(:start_date => '2012-10-15', :due_date => '2012-10-17')
1382 1391 issue2 = Issue.generate!(:start_date => '2012-10-15', :due_date => '2012-10-17')
1383 1392 IssueRelation.create!(:issue_from => issue1, :issue_to => issue2,
1384 1393 :relation_type => IssueRelation::TYPE_PRECEDES)
1385 1394 assert_equal Date.parse('2012-10-18'), issue2.reload.start_date
1386 1395
1387 1396 issue1.start_date = '2012-09-17'
1388 1397 issue1.due_date = '2012-09-18'
1389 1398 issue1.save!
1390 1399 issue2.reload
1391 1400 assert_equal Date.parse('2012-09-19'), issue2.start_date
1392 1401 assert_equal Date.parse('2012-09-21'), issue2.due_date
1393 1402 end
1394 1403
1395 1404 def test_rescheduling_reschedule_following_issue_earlier_should_consider_other_preceding_issues
1396 1405 issue1 = Issue.generate!(:start_date => '2012-10-15', :due_date => '2012-10-17')
1397 1406 issue2 = Issue.generate!(:start_date => '2012-10-15', :due_date => '2012-10-17')
1398 1407 issue3 = Issue.generate!(:start_date => '2012-10-01', :due_date => '2012-10-02')
1399 1408 IssueRelation.create!(:issue_from => issue1, :issue_to => issue2,
1400 1409 :relation_type => IssueRelation::TYPE_PRECEDES)
1401 1410 IssueRelation.create!(:issue_from => issue3, :issue_to => issue2,
1402 1411 :relation_type => IssueRelation::TYPE_PRECEDES)
1403 1412 assert_equal Date.parse('2012-10-18'), issue2.reload.start_date
1404 1413
1405 1414 issue1.start_date = '2012-09-17'
1406 1415 issue1.due_date = '2012-09-18'
1407 1416 issue1.save!
1408 1417 issue2.reload
1409 1418 # Issue 2 must start after Issue 3
1410 1419 assert_equal Date.parse('2012-10-03'), issue2.start_date
1411 1420 assert_equal Date.parse('2012-10-05'), issue2.due_date
1412 1421 end
1413 1422
1414 1423 def test_rescheduling_a_stale_issue_should_not_raise_an_error
1415 1424 with_settings :non_working_week_days => [] do
1416 1425 stale = Issue.find(1)
1417 1426 issue = Issue.find(1)
1418 1427 issue.subject = "Updated"
1419 1428 issue.save!
1420 1429 date = 10.days.from_now.to_date
1421 1430 assert_nothing_raised do
1422 1431 stale.reschedule_on!(date)
1423 1432 end
1424 1433 assert_equal date, stale.reload.start_date
1425 1434 end
1426 1435 end
1427 1436
1428 1437 def test_overdue
1429 1438 assert Issue.new(:due_date => 1.day.ago.to_date).overdue?
1430 1439 assert !Issue.new(:due_date => Date.today).overdue?
1431 1440 assert !Issue.new(:due_date => 1.day.from_now.to_date).overdue?
1432 1441 assert !Issue.new(:due_date => nil).overdue?
1433 1442 assert !Issue.new(:due_date => 1.day.ago.to_date,
1434 1443 :status => IssueStatus.where(:is_closed => true).first
1435 1444 ).overdue?
1436 1445 end
1437 1446
1438 1447 context "#behind_schedule?" do
1439 1448 should "be false if the issue has no start_date" do
1440 1449 assert !Issue.new(:start_date => nil,
1441 1450 :due_date => 1.day.from_now.to_date,
1442 1451 :done_ratio => 0).behind_schedule?
1443 1452 end
1444 1453
1445 1454 should "be false if the issue has no end_date" do
1446 1455 assert !Issue.new(:start_date => 1.day.from_now.to_date,
1447 1456 :due_date => nil,
1448 1457 :done_ratio => 0).behind_schedule?
1449 1458 end
1450 1459
1451 1460 should "be false if the issue has more done than it's calendar time" do
1452 1461 assert !Issue.new(:start_date => 50.days.ago.to_date,
1453 1462 :due_date => 50.days.from_now.to_date,
1454 1463 :done_ratio => 90).behind_schedule?
1455 1464 end
1456 1465
1457 1466 should "be true if the issue hasn't been started at all" do
1458 1467 assert Issue.new(:start_date => 1.day.ago.to_date,
1459 1468 :due_date => 1.day.from_now.to_date,
1460 1469 :done_ratio => 0).behind_schedule?
1461 1470 end
1462 1471
1463 1472 should "be true if the issue has used more calendar time than it's done ratio" do
1464 1473 assert Issue.new(:start_date => 100.days.ago.to_date,
1465 1474 :due_date => Date.today,
1466 1475 :done_ratio => 90).behind_schedule?
1467 1476 end
1468 1477 end
1469 1478
1470 1479 context "#assignable_users" do
1471 1480 should "be Users" do
1472 1481 assert_kind_of User, Issue.find(1).assignable_users.first
1473 1482 end
1474 1483
1475 1484 should "include the issue author" do
1476 1485 non_project_member = User.generate!
1477 1486 issue = Issue.generate!(:author => non_project_member)
1478 1487
1479 1488 assert issue.assignable_users.include?(non_project_member)
1480 1489 end
1481 1490
1482 1491 should "include the current assignee" do
1483 1492 user = User.generate!
1484 1493 issue = Issue.generate!(:assigned_to => user)
1485 1494 user.lock!
1486 1495
1487 1496 assert Issue.find(issue.id).assignable_users.include?(user)
1488 1497 end
1489 1498
1490 1499 should "not show the issue author twice" do
1491 1500 assignable_user_ids = Issue.find(1).assignable_users.collect(&:id)
1492 1501 assert_equal 2, assignable_user_ids.length
1493 1502
1494 1503 assignable_user_ids.each do |user_id|
1495 1504 assert_equal 1, assignable_user_ids.select {|i| i == user_id}.length,
1496 1505 "User #{user_id} appears more or less than once"
1497 1506 end
1498 1507 end
1499 1508
1500 1509 context "with issue_group_assignment" do
1501 1510 should "include groups" do
1502 1511 issue = Issue.new(:project => Project.find(2))
1503 1512
1504 1513 with_settings :issue_group_assignment => '1' do
1505 1514 assert_equal %w(Group User), issue.assignable_users.map {|a| a.class.name}.uniq.sort
1506 1515 assert issue.assignable_users.include?(Group.find(11))
1507 1516 end
1508 1517 end
1509 1518 end
1510 1519
1511 1520 context "without issue_group_assignment" do
1512 1521 should "not include groups" do
1513 1522 issue = Issue.new(:project => Project.find(2))
1514 1523
1515 1524 with_settings :issue_group_assignment => '0' do
1516 1525 assert_equal %w(User), issue.assignable_users.map {|a| a.class.name}.uniq.sort
1517 1526 assert !issue.assignable_users.include?(Group.find(11))
1518 1527 end
1519 1528 end
1520 1529 end
1521 1530 end
1522 1531
1523 1532 def test_create_should_send_email_notification
1524 1533 ActionMailer::Base.deliveries.clear
1525 1534 issue = Issue.new(:project_id => 1, :tracker_id => 1,
1526 1535 :author_id => 3, :status_id => 1,
1527 1536 :priority => IssuePriority.all.first,
1528 1537 :subject => 'test_create', :estimated_hours => '1:30')
1529 1538
1530 1539 assert issue.save
1531 1540 assert_equal 1, ActionMailer::Base.deliveries.size
1532 1541 end
1533 1542
1534 1543 def test_stale_issue_should_not_send_email_notification
1535 1544 ActionMailer::Base.deliveries.clear
1536 1545 issue = Issue.find(1)
1537 1546 stale = Issue.find(1)
1538 1547
1539 1548 issue.init_journal(User.find(1))
1540 1549 issue.subject = 'Subjet update'
1541 1550 assert issue.save
1542 1551 assert_equal 1, ActionMailer::Base.deliveries.size
1543 1552 ActionMailer::Base.deliveries.clear
1544 1553
1545 1554 stale.init_journal(User.find(1))
1546 1555 stale.subject = 'Another subjet update'
1547 1556 assert_raise ActiveRecord::StaleObjectError do
1548 1557 stale.save
1549 1558 end
1550 1559 assert ActionMailer::Base.deliveries.empty?
1551 1560 end
1552 1561
1553 1562 def test_journalized_description
1554 1563 IssueCustomField.delete_all
1555 1564
1556 1565 i = Issue.first
1557 1566 old_description = i.description
1558 1567 new_description = "This is the new description"
1559 1568
1560 1569 i.init_journal(User.find(2))
1561 1570 i.description = new_description
1562 1571 assert_difference 'Journal.count', 1 do
1563 1572 assert_difference 'JournalDetail.count', 1 do
1564 1573 i.save!
1565 1574 end
1566 1575 end
1567 1576
1568 1577 detail = JournalDetail.first(:order => 'id DESC')
1569 1578 assert_equal i, detail.journal.journalized
1570 1579 assert_equal 'attr', detail.property
1571 1580 assert_equal 'description', detail.prop_key
1572 1581 assert_equal old_description, detail.old_value
1573 1582 assert_equal new_description, detail.value
1574 1583 end
1575 1584
1576 1585 def test_blank_descriptions_should_not_be_journalized
1577 1586 IssueCustomField.delete_all
1578 1587 Issue.update_all("description = NULL", "id=1")
1579 1588
1580 1589 i = Issue.find(1)
1581 1590 i.init_journal(User.find(2))
1582 1591 i.subject = "blank description"
1583 1592 i.description = "\r\n"
1584 1593
1585 1594 assert_difference 'Journal.count', 1 do
1586 1595 assert_difference 'JournalDetail.count', 1 do
1587 1596 i.save!
1588 1597 end
1589 1598 end
1590 1599 end
1591 1600
1592 1601 def test_journalized_multi_custom_field
1593 1602 field = IssueCustomField.create!(:name => 'filter', :field_format => 'list',
1594 1603 :is_filter => true, :is_for_all => true,
1595 1604 :tracker_ids => [1],
1596 1605 :possible_values => ['value1', 'value2', 'value3'],
1597 1606 :multiple => true)
1598 1607
1599 1608 issue = Issue.create!(:project_id => 1, :tracker_id => 1,
1600 1609 :subject => 'Test', :author_id => 1)
1601 1610
1602 1611 assert_difference 'Journal.count' do
1603 1612 assert_difference 'JournalDetail.count' do
1604 1613 issue.init_journal(User.first)
1605 1614 issue.custom_field_values = {field.id => ['value1']}
1606 1615 issue.save!
1607 1616 end
1608 1617 assert_difference 'JournalDetail.count' do
1609 1618 issue.init_journal(User.first)
1610 1619 issue.custom_field_values = {field.id => ['value1', 'value2']}
1611 1620 issue.save!
1612 1621 end
1613 1622 assert_difference 'JournalDetail.count', 2 do
1614 1623 issue.init_journal(User.first)
1615 1624 issue.custom_field_values = {field.id => ['value3', 'value2']}
1616 1625 issue.save!
1617 1626 end
1618 1627 assert_difference 'JournalDetail.count', 2 do
1619 1628 issue.init_journal(User.first)
1620 1629 issue.custom_field_values = {field.id => nil}
1621 1630 issue.save!
1622 1631 end
1623 1632 end
1624 1633 end
1625 1634
1626 1635 def test_description_eol_should_be_normalized
1627 1636 i = Issue.new(:description => "CR \r LF \n CRLF \r\n")
1628 1637 assert_equal "CR \r\n LF \r\n CRLF \r\n", i.description
1629 1638 end
1630 1639
1631 1640 def test_saving_twice_should_not_duplicate_journal_details
1632 1641 i = Issue.first
1633 1642 i.init_journal(User.find(2), 'Some notes')
1634 1643 # initial changes
1635 1644 i.subject = 'New subject'
1636 1645 i.done_ratio = i.done_ratio + 10
1637 1646 assert_difference 'Journal.count' do
1638 1647 assert i.save
1639 1648 end
1640 1649 # 1 more change
1641 1650 i.priority = IssuePriority.where("id <> ?", i.priority_id).first
1642 1651 assert_no_difference 'Journal.count' do
1643 1652 assert_difference 'JournalDetail.count', 1 do
1644 1653 i.save
1645 1654 end
1646 1655 end
1647 1656 # no more change
1648 1657 assert_no_difference 'Journal.count' do
1649 1658 assert_no_difference 'JournalDetail.count' do
1650 1659 i.save
1651 1660 end
1652 1661 end
1653 1662 end
1654 1663
1655 1664 def test_all_dependent_issues
1656 1665 IssueRelation.delete_all
1657 1666 assert IssueRelation.create!(:issue_from => Issue.find(1),
1658 1667 :issue_to => Issue.find(2),
1659 1668 :relation_type => IssueRelation::TYPE_PRECEDES)
1660 1669 assert IssueRelation.create!(:issue_from => Issue.find(2),
1661 1670 :issue_to => Issue.find(3),
1662 1671 :relation_type => IssueRelation::TYPE_PRECEDES)
1663 1672 assert IssueRelation.create!(:issue_from => Issue.find(3),
1664 1673 :issue_to => Issue.find(8),
1665 1674 :relation_type => IssueRelation::TYPE_PRECEDES)
1666 1675
1667 1676 assert_equal [2, 3, 8], Issue.find(1).all_dependent_issues.collect(&:id).sort
1668 1677 end
1669 1678
1670 1679 def test_all_dependent_issues_with_persistent_circular_dependency
1671 1680 IssueRelation.delete_all
1672 1681 assert IssueRelation.create!(:issue_from => Issue.find(1),
1673 1682 :issue_to => Issue.find(2),
1674 1683 :relation_type => IssueRelation::TYPE_PRECEDES)
1675 1684 assert IssueRelation.create!(:issue_from => Issue.find(2),
1676 1685 :issue_to => Issue.find(3),
1677 1686 :relation_type => IssueRelation::TYPE_PRECEDES)
1678 1687
1679 1688 r = IssueRelation.create!(:issue_from => Issue.find(3),
1680 1689 :issue_to => Issue.find(7),
1681 1690 :relation_type => IssueRelation::TYPE_PRECEDES)
1682 1691 IssueRelation.update_all("issue_to_id = 1", ["id = ?", r.id])
1683 1692
1684 1693 assert_equal [2, 3], Issue.find(1).all_dependent_issues.collect(&:id).sort
1685 1694 end
1686 1695
1687 1696 def test_all_dependent_issues_with_persistent_multiple_circular_dependencies
1688 1697 IssueRelation.delete_all
1689 1698 assert IssueRelation.create!(:issue_from => Issue.find(1),
1690 1699 :issue_to => Issue.find(2),
1691 1700 :relation_type => IssueRelation::TYPE_RELATES)
1692 1701 assert IssueRelation.create!(:issue_from => Issue.find(2),
1693 1702 :issue_to => Issue.find(3),
1694 1703 :relation_type => IssueRelation::TYPE_RELATES)
1695 1704 assert IssueRelation.create!(:issue_from => Issue.find(3),
1696 1705 :issue_to => Issue.find(8),
1697 1706 :relation_type => IssueRelation::TYPE_RELATES)
1698 1707
1699 1708 r = IssueRelation.create!(:issue_from => Issue.find(8),
1700 1709 :issue_to => Issue.find(7),
1701 1710 :relation_type => IssueRelation::TYPE_RELATES)
1702 1711 IssueRelation.update_all("issue_to_id = 2", ["id = ?", r.id])
1703 1712
1704 1713 r = IssueRelation.create!(:issue_from => Issue.find(3),
1705 1714 :issue_to => Issue.find(7),
1706 1715 :relation_type => IssueRelation::TYPE_RELATES)
1707 1716 IssueRelation.update_all("issue_to_id = 1", ["id = ?", r.id])
1708 1717
1709 1718 assert_equal [2, 3, 8], Issue.find(1).all_dependent_issues.collect(&:id).sort
1710 1719 end
1711 1720
1712 1721 context "#done_ratio" do
1713 1722 setup do
1714 1723 @issue = Issue.find(1)
1715 1724 @issue_status = IssueStatus.find(1)
1716 1725 @issue_status.update_attribute(:default_done_ratio, 50)
1717 1726 @issue2 = Issue.find(2)
1718 1727 @issue_status2 = IssueStatus.find(2)
1719 1728 @issue_status2.update_attribute(:default_done_ratio, 0)
1720 1729 end
1721 1730
1722 1731 teardown do
1723 1732 Setting.issue_done_ratio = 'issue_field'
1724 1733 end
1725 1734
1726 1735 context "with Setting.issue_done_ratio using the issue_field" do
1727 1736 setup do
1728 1737 Setting.issue_done_ratio = 'issue_field'
1729 1738 end
1730 1739
1731 1740 should "read the issue's field" do
1732 1741 assert_equal 0, @issue.done_ratio
1733 1742 assert_equal 30, @issue2.done_ratio
1734 1743 end
1735 1744 end
1736 1745
1737 1746 context "with Setting.issue_done_ratio using the issue_status" do
1738 1747 setup do
1739 1748 Setting.issue_done_ratio = 'issue_status'
1740 1749 end
1741 1750
1742 1751 should "read the Issue Status's default done ratio" do
1743 1752 assert_equal 50, @issue.done_ratio
1744 1753 assert_equal 0, @issue2.done_ratio
1745 1754 end
1746 1755 end
1747 1756 end
1748 1757
1749 1758 context "#update_done_ratio_from_issue_status" do
1750 1759 setup do
1751 1760 @issue = Issue.find(1)
1752 1761 @issue_status = IssueStatus.find(1)
1753 1762 @issue_status.update_attribute(:default_done_ratio, 50)
1754 1763 @issue2 = Issue.find(2)
1755 1764 @issue_status2 = IssueStatus.find(2)
1756 1765 @issue_status2.update_attribute(:default_done_ratio, 0)
1757 1766 end
1758 1767
1759 1768 context "with Setting.issue_done_ratio using the issue_field" do
1760 1769 setup do
1761 1770 Setting.issue_done_ratio = 'issue_field'
1762 1771 end
1763 1772
1764 1773 should "not change the issue" do
1765 1774 @issue.update_done_ratio_from_issue_status
1766 1775 @issue2.update_done_ratio_from_issue_status
1767 1776
1768 1777 assert_equal 0, @issue.read_attribute(:done_ratio)
1769 1778 assert_equal 30, @issue2.read_attribute(:done_ratio)
1770 1779 end
1771 1780 end
1772 1781
1773 1782 context "with Setting.issue_done_ratio using the issue_status" do
1774 1783 setup do
1775 1784 Setting.issue_done_ratio = 'issue_status'
1776 1785 end
1777 1786
1778 1787 should "change the issue's done ratio" do
1779 1788 @issue.update_done_ratio_from_issue_status
1780 1789 @issue2.update_done_ratio_from_issue_status
1781 1790
1782 1791 assert_equal 50, @issue.read_attribute(:done_ratio)
1783 1792 assert_equal 0, @issue2.read_attribute(:done_ratio)
1784 1793 end
1785 1794 end
1786 1795 end
1787 1796
1788 1797 test "#by_tracker" do
1789 1798 User.current = User.anonymous
1790 1799 groups = Issue.by_tracker(Project.find(1))
1791 1800 assert_equal 3, groups.size
1792 1801 assert_equal 7, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1793 1802 end
1794 1803
1795 1804 test "#by_version" do
1796 1805 User.current = User.anonymous
1797 1806 groups = Issue.by_version(Project.find(1))
1798 1807 assert_equal 3, groups.size
1799 1808 assert_equal 3, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1800 1809 end
1801 1810
1802 1811 test "#by_priority" do
1803 1812 User.current = User.anonymous
1804 1813 groups = Issue.by_priority(Project.find(1))
1805 1814 assert_equal 4, groups.size
1806 1815 assert_equal 7, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1807 1816 end
1808 1817
1809 1818 test "#by_category" do
1810 1819 User.current = User.anonymous
1811 1820 groups = Issue.by_category(Project.find(1))
1812 1821 assert_equal 2, groups.size
1813 1822 assert_equal 3, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1814 1823 end
1815 1824
1816 1825 test "#by_assigned_to" do
1817 1826 User.current = User.anonymous
1818 1827 groups = Issue.by_assigned_to(Project.find(1))
1819 1828 assert_equal 2, groups.size
1820 1829 assert_equal 2, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1821 1830 end
1822 1831
1823 1832 test "#by_author" do
1824 1833 User.current = User.anonymous
1825 1834 groups = Issue.by_author(Project.find(1))
1826 1835 assert_equal 4, groups.size
1827 1836 assert_equal 7, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1828 1837 end
1829 1838
1830 1839 test "#by_subproject" do
1831 1840 User.current = User.anonymous
1832 1841 groups = Issue.by_subproject(Project.find(1))
1833 1842 # Private descendant not visible
1834 1843 assert_equal 1, groups.size
1835 1844 assert_equal 2, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1836 1845 end
1837 1846
1838 1847 def test_recently_updated_scope
1839 1848 #should return the last updated issue
1840 1849 assert_equal Issue.reorder("updated_on DESC").first, Issue.recently_updated.limit(1).first
1841 1850 end
1842 1851
1843 1852 def test_on_active_projects_scope
1844 1853 assert Project.find(2).archive
1845 1854
1846 1855 before = Issue.on_active_project.length
1847 1856 # test inclusion to results
1848 1857 issue = Issue.generate!(:tracker => Project.find(2).trackers.first)
1849 1858 assert_equal before + 1, Issue.on_active_project.length
1850 1859
1851 1860 # Move to an archived project
1852 1861 issue.project = Project.find(2)
1853 1862 assert issue.save
1854 1863 assert_equal before, Issue.on_active_project.length
1855 1864 end
1856 1865
1857 1866 context "Issue#recipients" do
1858 1867 setup do
1859 1868 @project = Project.find(1)
1860 1869 @author = User.generate!
1861 1870 @assignee = User.generate!
1862 1871 @issue = Issue.generate!(:project => @project, :assigned_to => @assignee, :author => @author)
1863 1872 end
1864 1873
1865 1874 should "include project recipients" do
1866 1875 assert @project.recipients.present?
1867 1876 @project.recipients.each do |project_recipient|
1868 1877 assert @issue.recipients.include?(project_recipient)
1869 1878 end
1870 1879 end
1871 1880
1872 1881 should "include the author if the author is active" do
1873 1882 assert @issue.author, "No author set for Issue"
1874 1883 assert @issue.recipients.include?(@issue.author.mail)
1875 1884 end
1876 1885
1877 1886 should "include the assigned to user if the assigned to user is active" do
1878 1887 assert @issue.assigned_to, "No assigned_to set for Issue"
1879 1888 assert @issue.recipients.include?(@issue.assigned_to.mail)
1880 1889 end
1881 1890
1882 1891 should "not include users who opt out of all email" do
1883 1892 @author.update_attribute(:mail_notification, :none)
1884 1893
1885 1894 assert !@issue.recipients.include?(@issue.author.mail)
1886 1895 end
1887 1896
1888 1897 should "not include the issue author if they are only notified of assigned issues" do
1889 1898 @author.update_attribute(:mail_notification, :only_assigned)
1890 1899
1891 1900 assert !@issue.recipients.include?(@issue.author.mail)
1892 1901 end
1893 1902
1894 1903 should "not include the assigned user if they are only notified of owned issues" do
1895 1904 @assignee.update_attribute(:mail_notification, :only_owner)
1896 1905
1897 1906 assert !@issue.recipients.include?(@issue.assigned_to.mail)
1898 1907 end
1899 1908 end
1900 1909
1901 1910 def test_last_journal_id_with_journals_should_return_the_journal_id
1902 1911 assert_equal 2, Issue.find(1).last_journal_id
1903 1912 end
1904 1913
1905 1914 def test_last_journal_id_without_journals_should_return_nil
1906 1915 assert_nil Issue.find(3).last_journal_id
1907 1916 end
1908 1917
1909 1918 def test_journals_after_should_return_journals_with_greater_id
1910 1919 assert_equal [Journal.find(2)], Issue.find(1).journals_after('1')
1911 1920 assert_equal [], Issue.find(1).journals_after('2')
1912 1921 end
1913 1922
1914 1923 def test_journals_after_with_blank_arg_should_return_all_journals
1915 1924 assert_equal [Journal.find(1), Journal.find(2)], Issue.find(1).journals_after('')
1916 1925 end
1917 1926
1918 1927 def test_css_classes_should_include_priority
1919 1928 issue = Issue.new(:priority => IssuePriority.find(8))
1920 1929 classes = issue.css_classes.split(' ')
1921 1930 assert_include 'priority-8', classes
1922 1931 assert_include 'priority-highest', classes
1923 1932 end
1924 1933
1925 1934 def test_save_attachments_with_hash_should_save_attachments_in_keys_order
1926 1935 set_tmp_attachments_directory
1927 1936 issue = Issue.generate!
1928 1937 issue.save_attachments({
1929 1938 'p0' => {'file' => mock_file_with_options(:original_filename => 'upload')},
1930 1939 '3' => {'file' => mock_file_with_options(:original_filename => 'bar')},
1931 1940 '1' => {'file' => mock_file_with_options(:original_filename => 'foo')}
1932 1941 })
1933 1942 issue.attach_saved_attachments
1934 1943
1935 1944 assert_equal 3, issue.reload.attachments.count
1936 1945 assert_equal %w(upload foo bar), issue.attachments.map(&:filename)
1937 1946 end
1938 1947 end
General Comments 0
You need to be logged in to leave comments. Login now