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