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