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