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