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