##// END OF EJS Templates
Makes all workflow transitions defined for all roles available to administrators (#2323)....
Jean-Philippe Lang -
r8587:ffbd3c0522a0
parent child
Show More
@@ -1,1052 +1,1052
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 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
62 62
63 63 named_scope :visible, lambda {|*args| { :include => :project,
64 64 :conditions => Issue.visible_condition(args.shift || User.current, *args) } }
65 65
66 66 named_scope :open, lambda {|*args|
67 67 is_closed = args.size > 0 ? !args.first : false
68 68 {:conditions => ["#{IssueStatus.table_name}.is_closed = ?", is_closed], :include => :status}
69 69 }
70 70
71 71 named_scope :recently_updated, :order => "#{Issue.table_name}.updated_on DESC"
72 72 named_scope :with_limit, lambda { |limit| { :limit => limit} }
73 73 named_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
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 after_destroy :update_parent_attributes
81 81
82 82 # Returns a SQL conditions string used to find all issues visible by the specified user
83 83 def self.visible_condition(user, options={})
84 84 Project.allowed_to_condition(user, :view_issues, options) do |role, user|
85 85 case role.issues_visibility
86 86 when 'all'
87 87 nil
88 88 when 'default'
89 89 user_ids = [user.id] + user.groups.map(&:id)
90 90 "(#{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 91 when 'own'
92 92 user_ids = [user.id] + user.groups.map(&:id)
93 93 "(#{table_name}.author_id = #{user.id} OR #{table_name}.assigned_to_id IN (#{user_ids.join(',')}))"
94 94 else
95 95 '1=0'
96 96 end
97 97 end
98 98 end
99 99
100 100 # Returns true if usr or current user is allowed to view the issue
101 101 def visible?(usr=nil)
102 102 (usr || User.current).allowed_to?(:view_issues, self.project) do |role, user|
103 103 case role.issues_visibility
104 104 when 'all'
105 105 true
106 106 when 'default'
107 107 !self.is_private? || self.author == user || user.is_or_belongs_to?(assigned_to)
108 108 when 'own'
109 109 self.author == user || user.is_or_belongs_to?(assigned_to)
110 110 else
111 111 false
112 112 end
113 113 end
114 114 end
115 115
116 116 def initialize(attributes=nil, *args)
117 117 super
118 118 if new_record?
119 119 # set default values for new records only
120 120 self.status ||= IssueStatus.default
121 121 self.priority ||= IssuePriority.default
122 122 self.watcher_user_ids = []
123 123 end
124 124 end
125 125
126 126 # Overrides Redmine::Acts::Customizable::InstanceMethods#available_custom_fields
127 127 def available_custom_fields
128 128 (project && tracker) ? (project.all_issue_custom_fields & tracker.custom_fields.all) : []
129 129 end
130 130
131 131 # Copies attributes from another issue, arg can be an id or an Issue
132 132 def copy_from(arg, options={})
133 133 issue = arg.is_a?(Issue) ? arg : Issue.visible.find(arg)
134 134 self.attributes = issue.attributes.dup.except("id", "root_id", "parent_id", "lft", "rgt", "created_on", "updated_on")
135 135 self.custom_field_values = issue.custom_field_values.inject({}) {|h,v| h[v.custom_field_id] = v.value; h}
136 136 self.status = issue.status
137 137 self.author = User.current
138 138 unless options[:attachments] == false
139 139 self.attachments = issue.attachments.map do |attachement|
140 140 attachement.copy(:container => self)
141 141 end
142 142 end
143 143 @copied_from = issue
144 144 self
145 145 end
146 146
147 147 # Returns an unsaved copy of the issue
148 148 def copy(attributes=nil)
149 149 copy = self.class.new.copy_from(self)
150 150 copy.attributes = attributes if attributes
151 151 copy
152 152 end
153 153
154 154 # Returns true if the issue is a copy
155 155 def copy?
156 156 @copied_from.present?
157 157 end
158 158
159 159 # Moves/copies an issue to a new project and tracker
160 160 # Returns the moved/copied issue on success, false on failure
161 161 def move_to_project(new_project, new_tracker=nil, options={})
162 162 ActiveSupport::Deprecation.warn "Issue#move_to_project is deprecated, use #project= instead."
163 163
164 164 if options[:copy]
165 165 issue = self.copy
166 166 else
167 167 issue = self
168 168 end
169 169
170 170 issue.init_journal(User.current, options[:notes])
171 171
172 172 # Preserve previous behaviour
173 173 # #move_to_project doesn't change tracker automatically
174 174 issue.send :project=, new_project, true
175 175 if new_tracker
176 176 issue.tracker = new_tracker
177 177 end
178 178 # Allow bulk setting of attributes on the issue
179 179 if options[:attributes]
180 180 issue.attributes = options[:attributes]
181 181 end
182 182
183 183 issue.save ? issue : false
184 184 end
185 185
186 186 def status_id=(sid)
187 187 self.status = nil
188 188 write_attribute(:status_id, sid)
189 189 end
190 190
191 191 def priority_id=(pid)
192 192 self.priority = nil
193 193 write_attribute(:priority_id, pid)
194 194 end
195 195
196 196 def category_id=(cid)
197 197 self.category = nil
198 198 write_attribute(:category_id, cid)
199 199 end
200 200
201 201 def fixed_version_id=(vid)
202 202 self.fixed_version = nil
203 203 write_attribute(:fixed_version_id, vid)
204 204 end
205 205
206 206 def tracker_id=(tid)
207 207 self.tracker = nil
208 208 result = write_attribute(:tracker_id, tid)
209 209 @custom_field_values = nil
210 210 result
211 211 end
212 212
213 213 def project_id=(project_id)
214 214 if project_id.to_s != self.project_id.to_s
215 215 self.project = (project_id.present? ? Project.find_by_id(project_id) : nil)
216 216 end
217 217 end
218 218
219 219 def project=(project, keep_tracker=false)
220 220 project_was = self.project
221 221 write_attribute(:project_id, project ? project.id : nil)
222 222 association_instance_set('project', project)
223 223 if project_was && project && project_was != project
224 224 unless keep_tracker || project.trackers.include?(tracker)
225 225 self.tracker = project.trackers.first
226 226 end
227 227 # Reassign to the category with same name if any
228 228 if category
229 229 self.category = project.issue_categories.find_by_name(category.name)
230 230 end
231 231 # Keep the fixed_version if it's still valid in the new_project
232 232 if fixed_version && fixed_version.project != project && !project.shared_versions.include?(fixed_version)
233 233 self.fixed_version = nil
234 234 end
235 235 if parent && parent.project_id != project_id
236 236 self.parent_issue_id = nil
237 237 end
238 238 @custom_field_values = nil
239 239 end
240 240 end
241 241
242 242 def description=(arg)
243 243 if arg.is_a?(String)
244 244 arg = arg.gsub(/(\r\n|\n|\r)/, "\r\n")
245 245 end
246 246 write_attribute(:description, arg)
247 247 end
248 248
249 249 # Overrides attributes= so that project and tracker get assigned first
250 250 def attributes_with_project_and_tracker_first=(new_attributes, *args)
251 251 return if new_attributes.nil?
252 252 attrs = new_attributes.dup
253 253 attrs.stringify_keys!
254 254
255 255 %w(project project_id tracker tracker_id).each do |attr|
256 256 if attrs.has_key?(attr)
257 257 send "#{attr}=", attrs.delete(attr)
258 258 end
259 259 end
260 260 send :attributes_without_project_and_tracker_first=, attrs, *args
261 261 end
262 262 # Do not redefine alias chain on reload (see #4838)
263 263 alias_method_chain(:attributes=, :project_and_tracker_first) unless method_defined?(:attributes_without_project_and_tracker_first=)
264 264
265 265 def estimated_hours=(h)
266 266 write_attribute :estimated_hours, (h.is_a?(String) ? h.to_hours : h)
267 267 end
268 268
269 269 safe_attributes 'project_id',
270 270 :if => lambda {|issue, user|
271 271 if issue.new_record?
272 272 issue.copy?
273 273 elsif user.allowed_to?(:move_issues, issue.project)
274 274 projects = Issue.allowed_target_projects_on_move(user)
275 275 projects.include?(issue.project) && projects.size > 1
276 276 end
277 277 }
278 278
279 279 safe_attributes 'tracker_id',
280 280 'status_id',
281 281 'category_id',
282 282 'assigned_to_id',
283 283 'priority_id',
284 284 'fixed_version_id',
285 285 'subject',
286 286 'description',
287 287 'start_date',
288 288 'due_date',
289 289 'done_ratio',
290 290 'estimated_hours',
291 291 'custom_field_values',
292 292 'custom_fields',
293 293 'lock_version',
294 294 :if => lambda {|issue, user| issue.new_record? || user.allowed_to?(:edit_issues, issue.project) }
295 295
296 296 safe_attributes 'status_id',
297 297 'assigned_to_id',
298 298 'fixed_version_id',
299 299 'done_ratio',
300 300 'lock_version',
301 301 :if => lambda {|issue, user| issue.new_statuses_allowed_to(user).any? }
302 302
303 303 safe_attributes 'watcher_user_ids',
304 304 :if => lambda {|issue, user| issue.new_record? && user.allowed_to?(:add_issue_watchers, issue.project)}
305 305
306 306 safe_attributes 'is_private',
307 307 :if => lambda {|issue, user|
308 308 user.allowed_to?(:set_issues_private, issue.project) ||
309 309 (issue.author == user && user.allowed_to?(:set_own_issues_private, issue.project))
310 310 }
311 311
312 312 safe_attributes 'parent_issue_id',
313 313 :if => lambda {|issue, user| (issue.new_record? || user.allowed_to?(:edit_issues, issue.project)) &&
314 314 user.allowed_to?(:manage_subtasks, issue.project)}
315 315
316 316 # Safely sets attributes
317 317 # Should be called from controllers instead of #attributes=
318 318 # attr_accessible is too rough because we still want things like
319 319 # Issue.new(:project => foo) to work
320 320 def safe_attributes=(attrs, user=User.current)
321 321 return unless attrs.is_a?(Hash)
322 322
323 323 # User can change issue attributes only if he has :edit permission or if a workflow transition is allowed
324 324 attrs = delete_unsafe_attributes(attrs, user)
325 325 return if attrs.empty?
326 326
327 327 # Project and Tracker must be set before since new_statuses_allowed_to depends on it.
328 328 if p = attrs.delete('project_id')
329 329 if allowed_target_projects(user).collect(&:id).include?(p.to_i)
330 330 self.project_id = p
331 331 end
332 332 end
333 333
334 334 if t = attrs.delete('tracker_id')
335 335 self.tracker_id = t
336 336 end
337 337
338 338 if attrs['status_id']
339 339 unless new_statuses_allowed_to(user).collect(&:id).include?(attrs['status_id'].to_i)
340 340 attrs.delete('status_id')
341 341 end
342 342 end
343 343
344 344 unless leaf?
345 345 attrs.reject! {|k,v| %w(priority_id done_ratio start_date due_date estimated_hours).include?(k)}
346 346 end
347 347
348 348 if attrs['parent_issue_id'].present?
349 349 attrs.delete('parent_issue_id') unless Issue.visible(user).exists?(attrs['parent_issue_id'].to_i)
350 350 end
351 351
352 352 # mass-assignment security bypass
353 353 self.send :attributes=, attrs, false
354 354 end
355 355
356 356 def done_ratio
357 357 if Issue.use_status_for_done_ratio? && status && status.default_done_ratio
358 358 status.default_done_ratio
359 359 else
360 360 read_attribute(:done_ratio)
361 361 end
362 362 end
363 363
364 364 def self.use_status_for_done_ratio?
365 365 Setting.issue_done_ratio == 'issue_status'
366 366 end
367 367
368 368 def self.use_field_for_done_ratio?
369 369 Setting.issue_done_ratio == 'issue_field'
370 370 end
371 371
372 372 def validate_issue
373 373 if self.due_date.nil? && @attributes['due_date'] && !@attributes['due_date'].empty?
374 374 errors.add :due_date, :not_a_date
375 375 end
376 376
377 377 if self.due_date and self.start_date and self.due_date < self.start_date
378 378 errors.add :due_date, :greater_than_start_date
379 379 end
380 380
381 381 if start_date && soonest_start && start_date < soonest_start
382 382 errors.add :start_date, :invalid
383 383 end
384 384
385 385 if fixed_version
386 386 if !assignable_versions.include?(fixed_version)
387 387 errors.add :fixed_version_id, :inclusion
388 388 elsif reopened? && fixed_version.closed?
389 389 errors.add :base, I18n.t(:error_can_not_reopen_issue_on_closed_version)
390 390 end
391 391 end
392 392
393 393 # Checks that the issue can not be added/moved to a disabled tracker
394 394 if project && (tracker_id_changed? || project_id_changed?)
395 395 unless project.trackers.include?(tracker)
396 396 errors.add :tracker_id, :inclusion
397 397 end
398 398 end
399 399
400 400 # Checks parent issue assignment
401 401 if @parent_issue
402 402 if @parent_issue.project_id != project_id
403 403 errors.add :parent_issue_id, :not_same_project
404 404 elsif !new_record?
405 405 # moving an existing issue
406 406 if @parent_issue.root_id != root_id
407 407 # we can always move to another tree
408 408 elsif move_possible?(@parent_issue)
409 409 # move accepted inside tree
410 410 else
411 411 errors.add :parent_issue_id, :not_a_valid_parent
412 412 end
413 413 end
414 414 end
415 415 end
416 416
417 417 # Set the done_ratio using the status if that setting is set. This will keep the done_ratios
418 418 # even if the user turns off the setting later
419 419 def update_done_ratio_from_issue_status
420 420 if Issue.use_status_for_done_ratio? && status && status.default_done_ratio
421 421 self.done_ratio = status.default_done_ratio
422 422 end
423 423 end
424 424
425 425 def init_journal(user, notes = "")
426 426 @current_journal ||= Journal.new(:journalized => self, :user => user, :notes => notes)
427 427 if new_record?
428 428 @current_journal.notify = false
429 429 else
430 430 @attributes_before_change = attributes.dup
431 431 @custom_values_before_change = {}
432 432 self.custom_values.each {|c| @custom_values_before_change.store c.custom_field_id, c.value }
433 433 end
434 434 # Make sure updated_on is updated when adding a note.
435 435 updated_on_will_change!
436 436 @current_journal
437 437 end
438 438
439 439 # Return true if the issue is closed, otherwise false
440 440 def closed?
441 441 self.status.is_closed?
442 442 end
443 443
444 444 # Return true if the issue is being reopened
445 445 def reopened?
446 446 if !new_record? && status_id_changed?
447 447 status_was = IssueStatus.find_by_id(status_id_was)
448 448 status_new = IssueStatus.find_by_id(status_id)
449 449 if status_was && status_new && status_was.is_closed? && !status_new.is_closed?
450 450 return true
451 451 end
452 452 end
453 453 false
454 454 end
455 455
456 456 # Return true if the issue is being closed
457 457 def closing?
458 458 if !new_record? && status_id_changed?
459 459 status_was = IssueStatus.find_by_id(status_id_was)
460 460 status_new = IssueStatus.find_by_id(status_id)
461 461 if status_was && status_new && !status_was.is_closed? && status_new.is_closed?
462 462 return true
463 463 end
464 464 end
465 465 false
466 466 end
467 467
468 468 # Returns true if the issue is overdue
469 469 def overdue?
470 470 !due_date.nil? && (due_date < Date.today) && !status.is_closed?
471 471 end
472 472
473 473 # Is the amount of work done less than it should for the due date
474 474 def behind_schedule?
475 475 return false if start_date.nil? || due_date.nil?
476 476 done_date = start_date + ((due_date - start_date+1)* done_ratio/100).floor
477 477 return done_date <= Date.today
478 478 end
479 479
480 480 # Does this issue have children?
481 481 def children?
482 482 !leaf?
483 483 end
484 484
485 485 # Users the issue can be assigned to
486 486 def assignable_users
487 487 users = project.assignable_users
488 488 users << author if author
489 489 users << assigned_to if assigned_to
490 490 users.uniq.sort
491 491 end
492 492
493 493 # Versions that the issue can be assigned to
494 494 def assignable_versions
495 495 @assignable_versions ||= (project.shared_versions.open + [Version.find_by_id(fixed_version_id_was)]).compact.uniq.sort
496 496 end
497 497
498 498 # Returns true if this issue is blocked by another issue that is still open
499 499 def blocked?
500 500 !relations_to.detect {|ir| ir.relation_type == 'blocks' && !ir.issue_from.closed?}.nil?
501 501 end
502 502
503 503 # Returns an array of status that user is able to apply
504 504 def new_statuses_allowed_to(user, include_default=false)
505 505 statuses = status.find_new_statuses_allowed_to(
506 user.roles_for_project(project),
506 user.admin ? Role.all : user.roles_for_project(project),
507 507 tracker,
508 508 author == user,
509 509 assigned_to_id_changed? ? assigned_to_id_was == user.id : assigned_to_id == user.id
510 510 )
511 511 statuses << status unless statuses.empty?
512 512 statuses << IssueStatus.default if include_default
513 513 statuses = statuses.uniq.sort
514 514 blocked? ? statuses.reject {|s| s.is_closed?} : statuses
515 515 end
516 516
517 517 def assigned_to_was
518 518 if assigned_to_id_changed? && assigned_to_id_was.present?
519 519 @assigned_to_was ||= User.find_by_id(assigned_to_id_was)
520 520 end
521 521 end
522 522
523 523 # Returns the mail adresses of users that should be notified
524 524 def recipients
525 525 notified = []
526 526 # Author and assignee are always notified unless they have been
527 527 # locked or don't want to be notified
528 528 notified << author if author
529 529 if assigned_to
530 530 notified += (assigned_to.is_a?(Group) ? assigned_to.users : [assigned_to])
531 531 end
532 532 if assigned_to_was
533 533 notified += (assigned_to_was.is_a?(Group) ? assigned_to_was.users : [assigned_to_was])
534 534 end
535 535 notified = notified.select {|u| u.active? && u.notify_about?(self)}
536 536
537 537 notified += project.notified_users
538 538 notified.uniq!
539 539 # Remove users that can not view the issue
540 540 notified.reject! {|user| !visible?(user)}
541 541 notified.collect(&:mail)
542 542 end
543 543
544 544 # Returns the number of hours spent on this issue
545 545 def spent_hours
546 546 @spent_hours ||= time_entries.sum(:hours) || 0
547 547 end
548 548
549 549 # Returns the total number of hours spent on this issue and its descendants
550 550 #
551 551 # Example:
552 552 # spent_hours => 0.0
553 553 # spent_hours => 50.2
554 554 def total_spent_hours
555 555 @total_spent_hours ||= self_and_descendants.sum("#{TimeEntry.table_name}.hours",
556 556 :joins => "LEFT JOIN #{TimeEntry.table_name} ON #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id").to_f || 0.0
557 557 end
558 558
559 559 def relations
560 560 @relations ||= (relations_from + relations_to).sort
561 561 end
562 562
563 563 # Preloads relations for a collection of issues
564 564 def self.load_relations(issues)
565 565 if issues.any?
566 566 relations = IssueRelation.all(:conditions => ["issue_from_id IN (:ids) OR issue_to_id IN (:ids)", {:ids => issues.map(&:id)}])
567 567 issues.each do |issue|
568 568 issue.instance_variable_set "@relations", relations.select {|r| r.issue_from_id == issue.id || r.issue_to_id == issue.id}
569 569 end
570 570 end
571 571 end
572 572
573 573 # Preloads visible spent time for a collection of issues
574 574 def self.load_visible_spent_hours(issues, user=User.current)
575 575 if issues.any?
576 576 hours_by_issue_id = TimeEntry.visible(user).sum(:hours, :group => :issue_id)
577 577 issues.each do |issue|
578 578 issue.instance_variable_set "@spent_hours", (hours_by_issue_id[issue.id] || 0)
579 579 end
580 580 end
581 581 end
582 582
583 583 # Finds an issue relation given its id.
584 584 def find_relation(relation_id)
585 585 IssueRelation.find(relation_id, :conditions => ["issue_to_id = ? OR issue_from_id = ?", id, id])
586 586 end
587 587
588 588 def all_dependent_issues(except=[])
589 589 except << self
590 590 dependencies = []
591 591 relations_from.each do |relation|
592 592 if relation.issue_to && !except.include?(relation.issue_to)
593 593 dependencies << relation.issue_to
594 594 dependencies += relation.issue_to.all_dependent_issues(except)
595 595 end
596 596 end
597 597 dependencies
598 598 end
599 599
600 600 # Returns an array of issues that duplicate this one
601 601 def duplicates
602 602 relations_to.select {|r| r.relation_type == IssueRelation::TYPE_DUPLICATES}.collect {|r| r.issue_from}
603 603 end
604 604
605 605 # Returns the due date or the target due date if any
606 606 # Used on gantt chart
607 607 def due_before
608 608 due_date || (fixed_version ? fixed_version.effective_date : nil)
609 609 end
610 610
611 611 # Returns the time scheduled for this issue.
612 612 #
613 613 # Example:
614 614 # Start Date: 2/26/09, End Date: 3/04/09
615 615 # duration => 6
616 616 def duration
617 617 (start_date && due_date) ? due_date - start_date : 0
618 618 end
619 619
620 620 def soonest_start
621 621 @soonest_start ||= (
622 622 relations_to.collect{|relation| relation.successor_soonest_start} +
623 623 ancestors.collect(&:soonest_start)
624 624 ).compact.max
625 625 end
626 626
627 627 def reschedule_after(date)
628 628 return if date.nil?
629 629 if leaf?
630 630 if start_date.nil? || start_date < date
631 631 self.start_date, self.due_date = date, date + duration
632 632 save
633 633 end
634 634 else
635 635 leaves.each do |leaf|
636 636 leaf.reschedule_after(date)
637 637 end
638 638 end
639 639 end
640 640
641 641 def <=>(issue)
642 642 if issue.nil?
643 643 -1
644 644 elsif root_id != issue.root_id
645 645 (root_id || 0) <=> (issue.root_id || 0)
646 646 else
647 647 (lft || 0) <=> (issue.lft || 0)
648 648 end
649 649 end
650 650
651 651 def to_s
652 652 "#{tracker} ##{id}: #{subject}"
653 653 end
654 654
655 655 # Returns a string of css classes that apply to the issue
656 656 def css_classes
657 657 s = "issue status-#{status.position} priority-#{priority.position}"
658 658 s << ' closed' if closed?
659 659 s << ' overdue' if overdue?
660 660 s << ' child' if child?
661 661 s << ' parent' unless leaf?
662 662 s << ' private' if is_private?
663 663 s << ' created-by-me' if User.current.logged? && author_id == User.current.id
664 664 s << ' assigned-to-me' if User.current.logged? && assigned_to_id == User.current.id
665 665 s
666 666 end
667 667
668 668 # Saves an issue, time_entry, attachments, and a journal from the parameters
669 669 # Returns false if save fails
670 670 def save_issue_with_child_records(params, existing_time_entry=nil)
671 671 Issue.transaction do
672 672 if params[:time_entry] && (params[:time_entry][:hours].present? || params[:time_entry][:comments].present?) && User.current.allowed_to?(:log_time, project)
673 673 @time_entry = existing_time_entry || TimeEntry.new
674 674 @time_entry.project = project
675 675 @time_entry.issue = self
676 676 @time_entry.user = User.current
677 677 @time_entry.spent_on = User.current.today
678 678 @time_entry.attributes = params[:time_entry]
679 679 self.time_entries << @time_entry
680 680 end
681 681
682 682 if valid?
683 683 attachments = Attachment.attach_files(self, params[:attachments])
684 684 # TODO: Rename hook
685 685 Redmine::Hook.call_hook(:controller_issues_edit_before_save, { :params => params, :issue => self, :time_entry => @time_entry, :journal => @current_journal})
686 686 begin
687 687 if save
688 688 # TODO: Rename hook
689 689 Redmine::Hook.call_hook(:controller_issues_edit_after_save, { :params => params, :issue => self, :time_entry => @time_entry, :journal => @current_journal})
690 690 else
691 691 raise ActiveRecord::Rollback
692 692 end
693 693 rescue ActiveRecord::StaleObjectError
694 694 attachments[:files].each(&:destroy)
695 695 errors.add :base, l(:notice_locking_conflict)
696 696 raise ActiveRecord::Rollback
697 697 end
698 698 end
699 699 end
700 700 end
701 701
702 702 # Unassigns issues from +version+ if it's no longer shared with issue's project
703 703 def self.update_versions_from_sharing_change(version)
704 704 # Update issues assigned to the version
705 705 update_versions(["#{Issue.table_name}.fixed_version_id = ?", version.id])
706 706 end
707 707
708 708 # Unassigns issues from versions that are no longer shared
709 709 # after +project+ was moved
710 710 def self.update_versions_from_hierarchy_change(project)
711 711 moved_project_ids = project.self_and_descendants.reload.collect(&:id)
712 712 # Update issues of the moved projects and issues assigned to a version of a moved project
713 713 Issue.update_versions(["#{Version.table_name}.project_id IN (?) OR #{Issue.table_name}.project_id IN (?)", moved_project_ids, moved_project_ids])
714 714 end
715 715
716 716 def parent_issue_id=(arg)
717 717 parent_issue_id = arg.blank? ? nil : arg.to_i
718 718 if parent_issue_id && @parent_issue = Issue.find_by_id(parent_issue_id)
719 719 @parent_issue.id
720 720 else
721 721 @parent_issue = nil
722 722 nil
723 723 end
724 724 end
725 725
726 726 def parent_issue_id
727 727 if instance_variable_defined? :@parent_issue
728 728 @parent_issue.nil? ? nil : @parent_issue.id
729 729 else
730 730 parent_id
731 731 end
732 732 end
733 733
734 734 # Extracted from the ReportsController.
735 735 def self.by_tracker(project)
736 736 count_and_group_by(:project => project,
737 737 :field => 'tracker_id',
738 738 :joins => Tracker.table_name)
739 739 end
740 740
741 741 def self.by_version(project)
742 742 count_and_group_by(:project => project,
743 743 :field => 'fixed_version_id',
744 744 :joins => Version.table_name)
745 745 end
746 746
747 747 def self.by_priority(project)
748 748 count_and_group_by(:project => project,
749 749 :field => 'priority_id',
750 750 :joins => IssuePriority.table_name)
751 751 end
752 752
753 753 def self.by_category(project)
754 754 count_and_group_by(:project => project,
755 755 :field => 'category_id',
756 756 :joins => IssueCategory.table_name)
757 757 end
758 758
759 759 def self.by_assigned_to(project)
760 760 count_and_group_by(:project => project,
761 761 :field => 'assigned_to_id',
762 762 :joins => User.table_name)
763 763 end
764 764
765 765 def self.by_author(project)
766 766 count_and_group_by(:project => project,
767 767 :field => 'author_id',
768 768 :joins => User.table_name)
769 769 end
770 770
771 771 def self.by_subproject(project)
772 772 ActiveRecord::Base.connection.select_all("select s.id as status_id,
773 773 s.is_closed as closed,
774 774 #{Issue.table_name}.project_id as project_id,
775 775 count(#{Issue.table_name}.id) as total
776 776 from
777 777 #{Issue.table_name}, #{Project.table_name}, #{IssueStatus.table_name} s
778 778 where
779 779 #{Issue.table_name}.status_id=s.id
780 780 and #{Issue.table_name}.project_id = #{Project.table_name}.id
781 781 and #{visible_condition(User.current, :project => project, :with_subprojects => true)}
782 782 and #{Issue.table_name}.project_id <> #{project.id}
783 783 group by s.id, s.is_closed, #{Issue.table_name}.project_id") if project.descendants.active.any?
784 784 end
785 785 # End ReportsController extraction
786 786
787 787 # Returns an array of projects that user can assign the issue to
788 788 def allowed_target_projects(user=User.current)
789 789 if new_record?
790 790 Project.all(:conditions => Project.allowed_to_condition(user, :add_issues))
791 791 else
792 792 self.class.allowed_target_projects_on_move(user)
793 793 end
794 794 end
795 795
796 796 # Returns an array of projects that user can move issues to
797 797 def self.allowed_target_projects_on_move(user=User.current)
798 798 projects = []
799 799 if user.admin?
800 800 # admin is allowed to move issues to any active (visible) project
801 801 projects = Project.visible(user).all
802 802 elsif user.logged?
803 803 if Role.non_member.allowed_to?(:move_issues)
804 804 projects = Project.visible(user).all
805 805 else
806 806 user.memberships.each {|m| projects << m.project if m.roles.detect {|r| r.allowed_to?(:move_issues)}}
807 807 end
808 808 end
809 809 projects
810 810 end
811 811
812 812 private
813 813
814 814 def after_project_change
815 815 # Update project_id on related time entries
816 816 TimeEntry.update_all(["project_id = ?", project_id], {:issue_id => id})
817 817
818 818 # Delete issue relations
819 819 unless Setting.cross_project_issue_relations?
820 820 relations_from.clear
821 821 relations_to.clear
822 822 end
823 823
824 824 # Move subtasks
825 825 children.each do |child|
826 826 # Change project and keep project
827 827 child.send :project=, project, true
828 828 unless child.save
829 829 raise ActiveRecord::Rollback
830 830 end
831 831 end
832 832 end
833 833
834 834 def update_nested_set_attributes
835 835 if root_id.nil?
836 836 # issue was just created
837 837 self.root_id = (@parent_issue.nil? ? id : @parent_issue.root_id)
838 838 set_default_left_and_right
839 839 Issue.update_all("root_id = #{root_id}, lft = #{lft}, rgt = #{rgt}", ["id = ?", id])
840 840 if @parent_issue
841 841 move_to_child_of(@parent_issue)
842 842 end
843 843 reload
844 844 elsif parent_issue_id != parent_id
845 845 former_parent_id = parent_id
846 846 # moving an existing issue
847 847 if @parent_issue && @parent_issue.root_id == root_id
848 848 # inside the same tree
849 849 move_to_child_of(@parent_issue)
850 850 else
851 851 # to another tree
852 852 unless root?
853 853 move_to_right_of(root)
854 854 reload
855 855 end
856 856 old_root_id = root_id
857 857 self.root_id = (@parent_issue.nil? ? id : @parent_issue.root_id )
858 858 target_maxright = nested_set_scope.maximum(right_column_name) || 0
859 859 offset = target_maxright + 1 - lft
860 860 Issue.update_all("root_id = #{root_id}, lft = lft + #{offset}, rgt = rgt + #{offset}",
861 861 ["root_id = ? AND lft >= ? AND rgt <= ? ", old_root_id, lft, rgt])
862 862 self[left_column_name] = lft + offset
863 863 self[right_column_name] = rgt + offset
864 864 if @parent_issue
865 865 move_to_child_of(@parent_issue)
866 866 end
867 867 end
868 868 reload
869 869 # delete invalid relations of all descendants
870 870 self_and_descendants.each do |issue|
871 871 issue.relations.each do |relation|
872 872 relation.destroy unless relation.valid?
873 873 end
874 874 end
875 875 # update former parent
876 876 recalculate_attributes_for(former_parent_id) if former_parent_id
877 877 end
878 878 remove_instance_variable(:@parent_issue) if instance_variable_defined?(:@parent_issue)
879 879 end
880 880
881 881 def update_parent_attributes
882 882 recalculate_attributes_for(parent_id) if parent_id
883 883 end
884 884
885 885 def recalculate_attributes_for(issue_id)
886 886 if issue_id && p = Issue.find_by_id(issue_id)
887 887 # priority = highest priority of children
888 888 if priority_position = p.children.maximum("#{IssuePriority.table_name}.position", :joins => :priority)
889 889 p.priority = IssuePriority.find_by_position(priority_position)
890 890 end
891 891
892 892 # start/due dates = lowest/highest dates of children
893 893 p.start_date = p.children.minimum(:start_date)
894 894 p.due_date = p.children.maximum(:due_date)
895 895 if p.start_date && p.due_date && p.due_date < p.start_date
896 896 p.start_date, p.due_date = p.due_date, p.start_date
897 897 end
898 898
899 899 # done ratio = weighted average ratio of leaves
900 900 unless Issue.use_status_for_done_ratio? && p.status && p.status.default_done_ratio
901 901 leaves_count = p.leaves.count
902 902 if leaves_count > 0
903 903 average = p.leaves.average(:estimated_hours).to_f
904 904 if average == 0
905 905 average = 1
906 906 end
907 907 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
908 908 progress = done / (average * leaves_count)
909 909 p.done_ratio = progress.round
910 910 end
911 911 end
912 912
913 913 # estimate = sum of leaves estimates
914 914 p.estimated_hours = p.leaves.sum(:estimated_hours).to_f
915 915 p.estimated_hours = nil if p.estimated_hours == 0.0
916 916
917 917 # ancestors will be recursively updated
918 918 p.save(false)
919 919 end
920 920 end
921 921
922 922 # Update issues so their versions are not pointing to a
923 923 # fixed_version that is not shared with the issue's project
924 924 def self.update_versions(conditions=nil)
925 925 # Only need to update issues with a fixed_version from
926 926 # a different project and that is not systemwide shared
927 927 Issue.scoped(:conditions => conditions).all(
928 928 :conditions => "#{Issue.table_name}.fixed_version_id IS NOT NULL" +
929 929 " AND #{Issue.table_name}.project_id <> #{Version.table_name}.project_id" +
930 930 " AND #{Version.table_name}.sharing <> 'system'",
931 931 :include => [:project, :fixed_version]
932 932 ).each do |issue|
933 933 next if issue.project.nil? || issue.fixed_version.nil?
934 934 unless issue.project.shared_versions.include?(issue.fixed_version)
935 935 issue.init_journal(User.current)
936 936 issue.fixed_version = nil
937 937 issue.save
938 938 end
939 939 end
940 940 end
941 941
942 942 # Callback on attachment deletion
943 943 def attachment_added(obj)
944 944 if @current_journal && !obj.new_record?
945 945 @current_journal.details << JournalDetail.new(:property => 'attachment', :prop_key => obj.id, :value => obj.filename)
946 946 end
947 947 end
948 948
949 949 # Callback on attachment deletion
950 950 def attachment_removed(obj)
951 951 journal = init_journal(User.current)
952 952 journal.details << JournalDetail.new(:property => 'attachment',
953 953 :prop_key => obj.id,
954 954 :old_value => obj.filename)
955 955 journal.save
956 956 end
957 957
958 958 # Default assignment based on category
959 959 def default_assign
960 960 if assigned_to.nil? && category && category.assigned_to
961 961 self.assigned_to = category.assigned_to
962 962 end
963 963 end
964 964
965 965 # Updates start/due dates of following issues
966 966 def reschedule_following_issues
967 967 if start_date_changed? || due_date_changed?
968 968 relations_from.each do |relation|
969 969 relation.set_issue_to_dates
970 970 end
971 971 end
972 972 end
973 973
974 974 # Closes duplicates if the issue is being closed
975 975 def close_duplicates
976 976 if closing?
977 977 duplicates.each do |duplicate|
978 978 # Reload is need in case the duplicate was updated by a previous duplicate
979 979 duplicate.reload
980 980 # Don't re-close it if it's already closed
981 981 next if duplicate.closed?
982 982 # Same user and notes
983 983 if @current_journal
984 984 duplicate.init_journal(@current_journal.user, @current_journal.notes)
985 985 end
986 986 duplicate.update_attribute :status, self.status
987 987 end
988 988 end
989 989 end
990 990
991 991 # Saves the changes in a Journal
992 992 # Called after_save
993 993 def create_journal
994 994 if @current_journal
995 995 # attributes changes
996 996 if @attributes_before_change
997 997 (Issue.column_names - %w(id root_id lft rgt lock_version created_on updated_on)).each {|c|
998 998 before = @attributes_before_change[c]
999 999 after = send(c)
1000 1000 next if before == after || (before.blank? && after.blank?)
1001 1001 @current_journal.details << JournalDetail.new(:property => 'attr',
1002 1002 :prop_key => c,
1003 1003 :old_value => before,
1004 1004 :value => after)
1005 1005 }
1006 1006 end
1007 1007 if @custom_values_before_change
1008 1008 # custom fields changes
1009 1009 custom_values.each {|c|
1010 1010 before = @custom_values_before_change[c.custom_field_id]
1011 1011 after = c.value
1012 1012 next if before == after || (before.blank? && after.blank?)
1013 1013 @current_journal.details << JournalDetail.new(:property => 'cf',
1014 1014 :prop_key => c.custom_field_id,
1015 1015 :old_value => before,
1016 1016 :value => after)
1017 1017 }
1018 1018 end
1019 1019 @current_journal.save
1020 1020 # reset current journal
1021 1021 init_journal @current_journal.user, @current_journal.notes
1022 1022 end
1023 1023 end
1024 1024
1025 1025 # Query generator for selecting groups of issue counts for a project
1026 1026 # based on specific criteria
1027 1027 #
1028 1028 # Options
1029 1029 # * project - Project to search in.
1030 1030 # * field - String. Issue field to key off of in the grouping.
1031 1031 # * joins - String. The table name to join against.
1032 1032 def self.count_and_group_by(options)
1033 1033 project = options.delete(:project)
1034 1034 select_field = options.delete(:field)
1035 1035 joins = options.delete(:joins)
1036 1036
1037 1037 where = "#{Issue.table_name}.#{select_field}=j.id"
1038 1038
1039 1039 ActiveRecord::Base.connection.select_all("select s.id as status_id,
1040 1040 s.is_closed as closed,
1041 1041 j.id as #{select_field},
1042 1042 count(#{Issue.table_name}.id) as total
1043 1043 from
1044 1044 #{Issue.table_name}, #{Project.table_name}, #{IssueStatus.table_name} s, #{joins} j
1045 1045 where
1046 1046 #{Issue.table_name}.status_id=s.id
1047 1047 and #{where}
1048 1048 and #{Issue.table_name}.project_id=#{Project.table_name}.id
1049 1049 and #{visible_condition(User.current, :project => project)}
1050 1050 group by s.id, s.is_closed, j.id")
1051 1051 end
1052 1052 end
@@ -1,1204 +1,1213
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.expand_path('../../test_helper', __FILE__)
19 19
20 20 class IssueTest < ActiveSupport::TestCase
21 21 fixtures :projects, :users, :members, :member_roles, :roles,
22 22 :groups_users,
23 23 :trackers, :projects_trackers,
24 24 :enabled_modules,
25 25 :versions,
26 26 :issue_statuses, :issue_categories, :issue_relations, :workflows,
27 27 :enumerations,
28 28 :issues,
29 29 :custom_fields, :custom_fields_projects, :custom_fields_trackers, :custom_values,
30 30 :time_entries
31 31
32 32 def test_create
33 33 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3,
34 34 :status_id => 1, :priority => IssuePriority.all.first,
35 35 :subject => 'test_create',
36 36 :description => 'IssueTest#test_create', :estimated_hours => '1:30')
37 37 assert issue.save
38 38 issue.reload
39 39 assert_equal 1.5, issue.estimated_hours
40 40 end
41 41
42 42 def test_create_minimal
43 43 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3,
44 44 :status_id => 1, :priority => IssuePriority.all.first,
45 45 :subject => 'test_create')
46 46 assert issue.save
47 47 assert issue.description.nil?
48 48 end
49 49
50 50 def test_create_with_required_custom_field
51 51 field = IssueCustomField.find_by_name('Database')
52 52 field.update_attribute(:is_required, true)
53 53
54 54 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1,
55 55 :status_id => 1, :subject => 'test_create',
56 56 :description => 'IssueTest#test_create_with_required_custom_field')
57 57 assert issue.available_custom_fields.include?(field)
58 58 # No value for the custom field
59 59 assert !issue.save
60 60 assert_equal I18n.translate('activerecord.errors.messages.invalid'),
61 61 issue.errors[:custom_values].to_s
62 62 # Blank value
63 63 issue.custom_field_values = { field.id => '' }
64 64 assert !issue.save
65 65 assert_equal I18n.translate('activerecord.errors.messages.invalid'),
66 66 issue.errors[:custom_values].to_s
67 67 # Invalid value
68 68 issue.custom_field_values = { field.id => 'SQLServer' }
69 69 assert !issue.save
70 70 assert_equal I18n.translate('activerecord.errors.messages.invalid'),
71 71 issue.errors[:custom_values].to_s
72 72 # Valid value
73 73 issue.custom_field_values = { field.id => 'PostgreSQL' }
74 74 assert issue.save
75 75 issue.reload
76 76 assert_equal 'PostgreSQL', issue.custom_value_for(field).value
77 77 end
78 78
79 79 def test_create_with_group_assignment
80 80 with_settings :issue_group_assignment => '1' do
81 81 assert Issue.new(:project_id => 2, :tracker_id => 1, :author_id => 1,
82 82 :subject => 'Group assignment',
83 83 :assigned_to_id => 11).save
84 84 issue = Issue.first(:order => 'id DESC')
85 85 assert_kind_of Group, issue.assigned_to
86 86 assert_equal Group.find(11), issue.assigned_to
87 87 end
88 88 end
89 89
90 90 def assert_visibility_match(user, issues)
91 91 assert_equal issues.collect(&:id).sort, Issue.all.select {|issue| issue.visible?(user)}.collect(&:id).sort
92 92 end
93 93
94 94 def test_visible_scope_for_anonymous
95 95 # Anonymous user should see issues of public projects only
96 96 issues = Issue.visible(User.anonymous).all
97 97 assert issues.any?
98 98 assert_nil issues.detect {|issue| !issue.project.is_public?}
99 99 assert_nil issues.detect {|issue| issue.is_private?}
100 100 assert_visibility_match User.anonymous, issues
101 101 end
102 102
103 103 def test_visible_scope_for_anonymous_with_own_issues_visibility
104 104 Role.anonymous.update_attribute :issues_visibility, 'own'
105 105 Issue.create!(:project_id => 1, :tracker_id => 1,
106 106 :author_id => User.anonymous.id,
107 107 :subject => 'Issue by anonymous')
108 108
109 109 issues = Issue.visible(User.anonymous).all
110 110 assert issues.any?
111 111 assert_nil issues.detect {|issue| issue.author != User.anonymous}
112 112 assert_visibility_match User.anonymous, issues
113 113 end
114 114
115 115 def test_visible_scope_for_anonymous_without_view_issues_permissions
116 116 # Anonymous user should not see issues without permission
117 117 Role.anonymous.remove_permission!(:view_issues)
118 118 issues = Issue.visible(User.anonymous).all
119 119 assert issues.empty?
120 120 assert_visibility_match User.anonymous, issues
121 121 end
122 122
123 123 def test_visible_scope_for_non_member
124 124 user = User.find(9)
125 125 assert user.projects.empty?
126 126 # Non member user should see issues of public projects only
127 127 issues = Issue.visible(user).all
128 128 assert issues.any?
129 129 assert_nil issues.detect {|issue| !issue.project.is_public?}
130 130 assert_nil issues.detect {|issue| issue.is_private?}
131 131 assert_visibility_match user, issues
132 132 end
133 133
134 134 def test_visible_scope_for_non_member_with_own_issues_visibility
135 135 Role.non_member.update_attribute :issues_visibility, 'own'
136 136 Issue.create!(:project_id => 1, :tracker_id => 1, :author_id => 9, :subject => 'Issue by non member')
137 137 user = User.find(9)
138 138
139 139 issues = Issue.visible(user).all
140 140 assert issues.any?
141 141 assert_nil issues.detect {|issue| issue.author != user}
142 142 assert_visibility_match user, issues
143 143 end
144 144
145 145 def test_visible_scope_for_non_member_without_view_issues_permissions
146 146 # Non member user should not see issues without permission
147 147 Role.non_member.remove_permission!(:view_issues)
148 148 user = User.find(9)
149 149 assert user.projects.empty?
150 150 issues = Issue.visible(user).all
151 151 assert issues.empty?
152 152 assert_visibility_match user, issues
153 153 end
154 154
155 155 def test_visible_scope_for_member
156 156 user = User.find(9)
157 157 # User should see issues of projects for which he has view_issues permissions only
158 158 Role.non_member.remove_permission!(:view_issues)
159 159 Member.create!(:principal => user, :project_id => 3, :role_ids => [2])
160 160 issues = Issue.visible(user).all
161 161 assert issues.any?
162 162 assert_nil issues.detect {|issue| issue.project_id != 3}
163 163 assert_nil issues.detect {|issue| issue.is_private?}
164 164 assert_visibility_match user, issues
165 165 end
166 166
167 167 def test_visible_scope_for_member_with_groups_should_return_assigned_issues
168 168 user = User.find(8)
169 169 assert user.groups.any?
170 170 Member.create!(:principal => user.groups.first, :project_id => 1, :role_ids => [2])
171 171 Role.non_member.remove_permission!(:view_issues)
172 172
173 173 issue = Issue.create(:project_id => 1, :tracker_id => 1, :author_id => 3,
174 174 :status_id => 1, :priority => IssuePriority.all.first,
175 175 :subject => 'Assignment test',
176 176 :assigned_to => user.groups.first,
177 177 :is_private => true)
178 178
179 179 Role.find(2).update_attribute :issues_visibility, 'default'
180 180 issues = Issue.visible(User.find(8)).all
181 181 assert issues.any?
182 182 assert issues.include?(issue)
183 183
184 184 Role.find(2).update_attribute :issues_visibility, 'own'
185 185 issues = Issue.visible(User.find(8)).all
186 186 assert issues.any?
187 187 assert issues.include?(issue)
188 188 end
189 189
190 190 def test_visible_scope_for_admin
191 191 user = User.find(1)
192 192 user.members.each(&:destroy)
193 193 assert user.projects.empty?
194 194 issues = Issue.visible(user).all
195 195 assert issues.any?
196 196 # Admin should see issues on private projects that he does not belong to
197 197 assert issues.detect {|issue| !issue.project.is_public?}
198 198 # Admin should see private issues of other users
199 199 assert issues.detect {|issue| issue.is_private? && issue.author != user}
200 200 assert_visibility_match user, issues
201 201 end
202 202
203 203 def test_visible_scope_with_project
204 204 project = Project.find(1)
205 205 issues = Issue.visible(User.find(2), :project => project).all
206 206 projects = issues.collect(&:project).uniq
207 207 assert_equal 1, projects.size
208 208 assert_equal project, projects.first
209 209 end
210 210
211 211 def test_visible_scope_with_project_and_subprojects
212 212 project = Project.find(1)
213 213 issues = Issue.visible(User.find(2), :project => project, :with_subprojects => true).all
214 214 projects = issues.collect(&:project).uniq
215 215 assert projects.size > 1
216 216 assert_equal [], projects.select {|p| !p.is_or_is_descendant_of?(project)}
217 217 end
218 218
219 219 def test_visible_and_nested_set_scopes
220 220 assert_equal 0, Issue.find(1).descendants.visible.all.size
221 221 end
222 222
223 223 def test_open_scope
224 224 issues = Issue.open.all
225 225 assert_nil issues.detect(&:closed?)
226 226 end
227 227
228 228 def test_open_scope_with_arg
229 229 issues = Issue.open(false).all
230 230 assert_equal issues, issues.select(&:closed?)
231 231 end
232 232
233 233 def test_errors_full_messages_should_include_custom_fields_errors
234 234 field = IssueCustomField.find_by_name('Database')
235 235
236 236 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1,
237 237 :status_id => 1, :subject => 'test_create',
238 238 :description => 'IssueTest#test_create_with_required_custom_field')
239 239 assert issue.available_custom_fields.include?(field)
240 240 # Invalid value
241 241 issue.custom_field_values = { field.id => 'SQLServer' }
242 242
243 243 assert !issue.valid?
244 244 assert_equal 1, issue.errors.full_messages.size
245 245 assert_equal "Database #{I18n.translate('activerecord.errors.messages.inclusion')}",
246 246 issue.errors.full_messages.first
247 247 end
248 248
249 249 def test_update_issue_with_required_custom_field
250 250 field = IssueCustomField.find_by_name('Database')
251 251 field.update_attribute(:is_required, true)
252 252
253 253 issue = Issue.find(1)
254 254 assert_nil issue.custom_value_for(field)
255 255 assert issue.available_custom_fields.include?(field)
256 256 # No change to custom values, issue can be saved
257 257 assert issue.save
258 258 # Blank value
259 259 issue.custom_field_values = { field.id => '' }
260 260 assert !issue.save
261 261 # Valid value
262 262 issue.custom_field_values = { field.id => 'PostgreSQL' }
263 263 assert issue.save
264 264 issue.reload
265 265 assert_equal 'PostgreSQL', issue.custom_value_for(field).value
266 266 end
267 267
268 268 def test_should_not_update_attributes_if_custom_fields_validation_fails
269 269 issue = Issue.find(1)
270 270 field = IssueCustomField.find_by_name('Database')
271 271 assert issue.available_custom_fields.include?(field)
272 272
273 273 issue.custom_field_values = { field.id => 'Invalid' }
274 274 issue.subject = 'Should be not be saved'
275 275 assert !issue.save
276 276
277 277 issue.reload
278 278 assert_equal "Can't print recipes", issue.subject
279 279 end
280 280
281 281 def test_should_not_recreate_custom_values_objects_on_update
282 282 field = IssueCustomField.find_by_name('Database')
283 283
284 284 issue = Issue.find(1)
285 285 issue.custom_field_values = { field.id => 'PostgreSQL' }
286 286 assert issue.save
287 287 custom_value = issue.custom_value_for(field)
288 288 issue.reload
289 289 issue.custom_field_values = { field.id => 'MySQL' }
290 290 assert issue.save
291 291 issue.reload
292 292 assert_equal custom_value.id, issue.custom_value_for(field).id
293 293 end
294 294
295 295 def test_should_not_update_custom_fields_on_changing_tracker_with_different_custom_fields
296 296 issue = Issue.create!(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :subject => 'Test', :custom_field_values => {'2' => 'Test'})
297 297 assert !Tracker.find(2).custom_field_ids.include?(2)
298 298
299 299 issue = Issue.find(issue.id)
300 300 issue.attributes = {:tracker_id => 2, :custom_field_values => {'1' => ''}}
301 301
302 302 issue = Issue.find(issue.id)
303 303 custom_value = issue.custom_value_for(2)
304 304 assert_not_nil custom_value
305 305 assert_equal 'Test', custom_value.value
306 306 end
307 307
308 308 def test_assigning_tracker_id_should_reload_custom_fields_values
309 309 issue = Issue.new(:project => Project.find(1))
310 310 assert issue.custom_field_values.empty?
311 311 issue.tracker_id = 1
312 312 assert issue.custom_field_values.any?
313 313 end
314 314
315 315 def test_assigning_attributes_should_assign_project_and_tracker_first
316 316 seq = sequence('seq')
317 317 issue = Issue.new
318 318 issue.expects(:project_id=).in_sequence(seq)
319 319 issue.expects(:tracker_id=).in_sequence(seq)
320 320 issue.expects(:subject=).in_sequence(seq)
321 321 issue.attributes = {:tracker_id => 2, :project_id => 1, :subject => 'Test'}
322 322 end
323 323
324 324 def test_assigning_tracker_and_custom_fields_should_assign_custom_fields
325 325 attributes = ActiveSupport::OrderedHash.new
326 326 attributes['custom_field_values'] = { '1' => 'MySQL' }
327 327 attributes['tracker_id'] = '1'
328 328 issue = Issue.new(:project => Project.find(1))
329 329 issue.attributes = attributes
330 330 assert_not_nil issue.custom_value_for(1)
331 331 assert_equal 'MySQL', issue.custom_value_for(1).value
332 332 end
333 333
334 334 def test_should_update_issue_with_disabled_tracker
335 335 p = Project.find(1)
336 336 issue = Issue.find(1)
337 337
338 338 p.trackers.delete(issue.tracker)
339 339 assert !p.trackers.include?(issue.tracker)
340 340
341 341 issue.reload
342 342 issue.subject = 'New subject'
343 343 assert issue.save
344 344 end
345 345
346 346 def test_should_not_set_a_disabled_tracker
347 347 p = Project.find(1)
348 348 p.trackers.delete(Tracker.find(2))
349 349
350 350 issue = Issue.find(1)
351 351 issue.tracker_id = 2
352 352 issue.subject = 'New subject'
353 353 assert !issue.save
354 354 assert_not_nil issue.errors[:tracker_id]
355 355 end
356 356
357 357 def test_category_based_assignment
358 358 issue = Issue.create(:project_id => 1, :tracker_id => 1, :author_id => 3,
359 359 :status_id => 1, :priority => IssuePriority.all.first,
360 360 :subject => 'Assignment test',
361 361 :description => 'Assignment test', :category_id => 1)
362 362 assert_equal IssueCategory.find(1).assigned_to, issue.assigned_to
363 363 end
364 364
365 365 def test_new_statuses_allowed_to
366 366 Workflow.delete_all
367 367
368 368 Workflow.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 2, :author => false, :assignee => false)
369 369 Workflow.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 3, :author => true, :assignee => false)
370 370 Workflow.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 4, :author => false, :assignee => true)
371 371 Workflow.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 5, :author => true, :assignee => true)
372 372 status = IssueStatus.find(1)
373 373 role = Role.find(1)
374 374 tracker = Tracker.find(1)
375 375 user = User.find(2)
376 376
377 377 issue = Issue.generate!(:tracker => tracker, :status => status, :project_id => 1)
378 378 assert_equal [1, 2], issue.new_statuses_allowed_to(user).map(&:id)
379 379
380 380 issue = Issue.generate!(:tracker => tracker, :status => status, :project_id => 1, :author => user)
381 381 assert_equal [1, 2, 3, 5], issue.new_statuses_allowed_to(user).map(&:id)
382 382
383 383 issue = Issue.generate!(:tracker => tracker, :status => status, :project_id => 1, :assigned_to => user)
384 384 assert_equal [1, 2, 4, 5], issue.new_statuses_allowed_to(user).map(&:id)
385 385
386 386 issue = Issue.generate!(:tracker => tracker, :status => status, :project_id => 1, :author => user, :assigned_to => user)
387 387 assert_equal [1, 2, 3, 4, 5], issue.new_statuses_allowed_to(user).map(&:id)
388 388 end
389 389
390 def test_new_statuses_allowed_to_should_return_all_transitions_for_admin
391 admin = User.find(1)
392 issue = Issue.find(1)
393 assert !admin.member_of?(issue.project)
394 expected_statuses = [issue.status] + Workflow.find_all_by_old_status_id(issue.status_id).map(&:new_status).uniq.sort
395
396 assert_equal expected_statuses, issue.new_statuses_allowed_to(admin)
397 end
398
390 399 def test_copy
391 400 issue = Issue.new.copy_from(1)
392 401 assert issue.copy?
393 402 assert issue.save
394 403 issue.reload
395 404 orig = Issue.find(1)
396 405 assert_equal orig.subject, issue.subject
397 406 assert_equal orig.tracker, issue.tracker
398 407 assert_equal "125", issue.custom_value_for(2).value
399 408 end
400 409
401 410 def test_copy_should_copy_status
402 411 orig = Issue.find(8)
403 412 assert orig.status != IssueStatus.default
404 413
405 414 issue = Issue.new.copy_from(orig)
406 415 assert issue.save
407 416 issue.reload
408 417 assert_equal orig.status, issue.status
409 418 end
410 419
411 420 def test_should_not_call_after_project_change_on_creation
412 421 issue = Issue.new(:project_id => 1, :tracker_id => 1, :status_id => 1, :subject => 'Test', :author_id => 1)
413 422 issue.expects(:after_project_change).never
414 423 issue.save!
415 424 end
416 425
417 426 def test_should_not_call_after_project_change_on_update
418 427 issue = Issue.find(1)
419 428 issue.project = Project.find(1)
420 429 issue.subject = 'No project change'
421 430 issue.expects(:after_project_change).never
422 431 issue.save!
423 432 end
424 433
425 434 def test_should_call_after_project_change_on_project_change
426 435 issue = Issue.find(1)
427 436 issue.project = Project.find(2)
428 437 issue.expects(:after_project_change).once
429 438 issue.save!
430 439 end
431 440
432 441 def test_should_close_duplicates
433 442 # Create 3 issues
434 443 project = Project.find(1)
435 444 issue1 = Issue.generate_for_project!(project)
436 445 issue2 = Issue.generate_for_project!(project)
437 446 issue3 = Issue.generate_for_project!(project)
438 447
439 448 # 2 is a dupe of 1
440 449 IssueRelation.create!(:issue_from => issue2, :issue_to => issue1, :relation_type => IssueRelation::TYPE_DUPLICATES)
441 450 # And 3 is a dupe of 2
442 451 IssueRelation.create!(:issue_from => issue3, :issue_to => issue2, :relation_type => IssueRelation::TYPE_DUPLICATES)
443 452 # And 3 is a dupe of 1 (circular duplicates)
444 453 IssueRelation.create!(:issue_from => issue3, :issue_to => issue1, :relation_type => IssueRelation::TYPE_DUPLICATES)
445 454
446 455 assert issue1.reload.duplicates.include?(issue2)
447 456
448 457 # Closing issue 1
449 458 issue1.init_journal(User.find(:first), "Closing issue1")
450 459 issue1.status = IssueStatus.find :first, :conditions => {:is_closed => true}
451 460 assert issue1.save
452 461 # 2 and 3 should be also closed
453 462 assert issue2.reload.closed?
454 463 assert issue3.reload.closed?
455 464 end
456 465
457 466 def test_should_not_close_duplicated_issue
458 467 project = Project.find(1)
459 468 issue1 = Issue.generate_for_project!(project)
460 469 issue2 = Issue.generate_for_project!(project)
461 470
462 471 # 2 is a dupe of 1
463 472 IssueRelation.create(:issue_from => issue2, :issue_to => issue1, :relation_type => IssueRelation::TYPE_DUPLICATES)
464 473 # 2 is a dup of 1 but 1 is not a duplicate of 2
465 474 assert !issue2.reload.duplicates.include?(issue1)
466 475
467 476 # Closing issue 2
468 477 issue2.init_journal(User.find(:first), "Closing issue2")
469 478 issue2.status = IssueStatus.find :first, :conditions => {:is_closed => true}
470 479 assert issue2.save
471 480 # 1 should not be also closed
472 481 assert !issue1.reload.closed?
473 482 end
474 483
475 484 def test_assignable_versions
476 485 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :fixed_version_id => 1, :subject => 'New issue')
477 486 assert_equal ['open'], issue.assignable_versions.collect(&:status).uniq
478 487 end
479 488
480 489 def test_should_not_be_able_to_assign_a_new_issue_to_a_closed_version
481 490 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :fixed_version_id => 1, :subject => 'New issue')
482 491 assert !issue.save
483 492 assert_not_nil issue.errors[:fixed_version_id]
484 493 end
485 494
486 495 def test_should_not_be_able_to_assign_a_new_issue_to_a_locked_version
487 496 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :fixed_version_id => 2, :subject => 'New issue')
488 497 assert !issue.save
489 498 assert_not_nil issue.errors[:fixed_version_id]
490 499 end
491 500
492 501 def test_should_be_able_to_assign_a_new_issue_to_an_open_version
493 502 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :fixed_version_id => 3, :subject => 'New issue')
494 503 assert issue.save
495 504 end
496 505
497 506 def test_should_be_able_to_update_an_issue_assigned_to_a_closed_version
498 507 issue = Issue.find(11)
499 508 assert_equal 'closed', issue.fixed_version.status
500 509 issue.subject = 'Subject changed'
501 510 assert issue.save
502 511 end
503 512
504 513 def test_should_not_be_able_to_reopen_an_issue_assigned_to_a_closed_version
505 514 issue = Issue.find(11)
506 515 issue.status_id = 1
507 516 assert !issue.save
508 517 assert_not_nil issue.errors[:base]
509 518 end
510 519
511 520 def test_should_be_able_to_reopen_and_reassign_an_issue_assigned_to_a_closed_version
512 521 issue = Issue.find(11)
513 522 issue.status_id = 1
514 523 issue.fixed_version_id = 3
515 524 assert issue.save
516 525 end
517 526
518 527 def test_should_be_able_to_reopen_an_issue_assigned_to_a_locked_version
519 528 issue = Issue.find(12)
520 529 assert_equal 'locked', issue.fixed_version.status
521 530 issue.status_id = 1
522 531 assert issue.save
523 532 end
524 533
525 534 def test_move_to_another_project_with_same_category
526 535 issue = Issue.find(1)
527 536 issue.project = Project.find(2)
528 537 assert issue.save
529 538 issue.reload
530 539 assert_equal 2, issue.project_id
531 540 # Category changes
532 541 assert_equal 4, issue.category_id
533 542 # Make sure time entries were move to the target project
534 543 assert_equal 2, issue.time_entries.first.project_id
535 544 end
536 545
537 546 def test_move_to_another_project_without_same_category
538 547 issue = Issue.find(2)
539 548 issue.project = Project.find(2)
540 549 assert issue.save
541 550 issue.reload
542 551 assert_equal 2, issue.project_id
543 552 # Category cleared
544 553 assert_nil issue.category_id
545 554 end
546 555
547 556 def test_move_to_another_project_should_clear_fixed_version_when_not_shared
548 557 issue = Issue.find(1)
549 558 issue.update_attribute(:fixed_version_id, 1)
550 559 issue.project = Project.find(2)
551 560 assert issue.save
552 561 issue.reload
553 562 assert_equal 2, issue.project_id
554 563 # Cleared fixed_version
555 564 assert_equal nil, issue.fixed_version
556 565 end
557 566
558 567 def test_move_to_another_project_should_keep_fixed_version_when_shared_with_the_target_project
559 568 issue = Issue.find(1)
560 569 issue.update_attribute(:fixed_version_id, 4)
561 570 issue.project = Project.find(5)
562 571 assert issue.save
563 572 issue.reload
564 573 assert_equal 5, issue.project_id
565 574 # Keep fixed_version
566 575 assert_equal 4, issue.fixed_version_id
567 576 end
568 577
569 578 def test_move_to_another_project_should_clear_fixed_version_when_not_shared_with_the_target_project
570 579 issue = Issue.find(1)
571 580 issue.update_attribute(:fixed_version_id, 1)
572 581 issue.project = Project.find(5)
573 582 assert issue.save
574 583 issue.reload
575 584 assert_equal 5, issue.project_id
576 585 # Cleared fixed_version
577 586 assert_equal nil, issue.fixed_version
578 587 end
579 588
580 589 def test_move_to_another_project_should_keep_fixed_version_when_shared_systemwide
581 590 issue = Issue.find(1)
582 591 issue.update_attribute(:fixed_version_id, 7)
583 592 issue.project = Project.find(2)
584 593 assert issue.save
585 594 issue.reload
586 595 assert_equal 2, issue.project_id
587 596 # Keep fixed_version
588 597 assert_equal 7, issue.fixed_version_id
589 598 end
590 599
591 600 def test_move_to_another_project_with_disabled_tracker
592 601 issue = Issue.find(1)
593 602 target = Project.find(2)
594 603 target.tracker_ids = [3]
595 604 target.save
596 605 issue.project = target
597 606 assert issue.save
598 607 issue.reload
599 608 assert_equal 2, issue.project_id
600 609 assert_equal 3, issue.tracker_id
601 610 end
602 611
603 612 def test_copy_to_the_same_project
604 613 issue = Issue.find(1)
605 614 copy = issue.copy
606 615 assert_difference 'Issue.count' do
607 616 copy.save!
608 617 end
609 618 assert_kind_of Issue, copy
610 619 assert_equal issue.project, copy.project
611 620 assert_equal "125", copy.custom_value_for(2).value
612 621 end
613 622
614 623 def test_copy_to_another_project_and_tracker
615 624 issue = Issue.find(1)
616 625 copy = issue.copy(:project_id => 3, :tracker_id => 2)
617 626 assert_difference 'Issue.count' do
618 627 copy.save!
619 628 end
620 629 copy.reload
621 630 assert_kind_of Issue, copy
622 631 assert_equal Project.find(3), copy.project
623 632 assert_equal Tracker.find(2), copy.tracker
624 633 # Custom field #2 is not associated with target tracker
625 634 assert_nil copy.custom_value_for(2)
626 635 end
627 636
628 637 context "#copy" do
629 638 setup do
630 639 @issue = Issue.find(1)
631 640 end
632 641
633 642 should "not create a journal" do
634 643 copy = @issue.copy(:project_id => 3, :tracker_id => 2, :assigned_to_id => 3)
635 644 copy.save!
636 645 assert_equal 0, copy.reload.journals.size
637 646 end
638 647
639 648 should "allow assigned_to changes" do
640 649 copy = @issue.copy(:project_id => 3, :tracker_id => 2, :assigned_to_id => 3)
641 650 assert_equal 3, copy.assigned_to_id
642 651 end
643 652
644 653 should "allow status changes" do
645 654 copy = @issue.copy(:project_id => 3, :tracker_id => 2, :status_id => 2)
646 655 assert_equal 2, copy.status_id
647 656 end
648 657
649 658 should "allow start date changes" do
650 659 date = Date.today
651 660 copy = @issue.copy(:project_id => 3, :tracker_id => 2, :start_date => date)
652 661 assert_equal date, copy.start_date
653 662 end
654 663
655 664 should "allow due date changes" do
656 665 date = Date.today
657 666 copy = @issue.copy(:project_id => 3, :tracker_id => 2, :due_date => date)
658 667 assert_equal date, copy.due_date
659 668 end
660 669
661 670 should "set current user as author" do
662 671 User.current = User.find(9)
663 672 copy = @issue.copy(:project_id => 3, :tracker_id => 2)
664 673 assert_equal User.current, copy.author
665 674 end
666 675
667 676 should "create a journal with notes" do
668 677 date = Date.today
669 678 notes = "Notes added when copying"
670 679 copy = @issue.copy(:project_id => 3, :tracker_id => 2, :start_date => date)
671 680 copy.init_journal(User.current, notes)
672 681 copy.save!
673 682
674 683 assert_equal 1, copy.journals.size
675 684 journal = copy.journals.first
676 685 assert_equal 0, journal.details.size
677 686 assert_equal notes, journal.notes
678 687 end
679 688 end
680 689
681 690 def test_recipients_should_include_previous_assignee
682 691 user = User.find(3)
683 692 user.members.update_all ["mail_notification = ?", false]
684 693 user.update_attribute :mail_notification, 'only_assigned'
685 694
686 695 issue = Issue.find(2)
687 696 issue.assigned_to = nil
688 697 assert_include user.mail, issue.recipients
689 698 issue.save!
690 699 assert !issue.recipients.include?(user.mail)
691 700 end
692 701
693 702 def test_recipients_should_not_include_users_that_cannot_view_the_issue
694 703 issue = Issue.find(12)
695 704 assert issue.recipients.include?(issue.author.mail)
696 705 # copy the issue to a private project
697 706 copy = issue.copy(:project_id => 5, :tracker_id => 2)
698 707 # author is not a member of project anymore
699 708 assert !copy.recipients.include?(copy.author.mail)
700 709 end
701 710
702 711 def test_recipients_should_include_the_assigned_group_members
703 712 group_member = User.generate_with_protected!
704 713 group = Group.generate!
705 714 group.users << group_member
706 715
707 716 issue = Issue.find(12)
708 717 issue.assigned_to = group
709 718 assert issue.recipients.include?(group_member.mail)
710 719 end
711 720
712 721 def test_watcher_recipients_should_not_include_users_that_cannot_view_the_issue
713 722 user = User.find(3)
714 723 issue = Issue.find(9)
715 724 Watcher.create!(:user => user, :watchable => issue)
716 725 assert issue.watched_by?(user)
717 726 assert !issue.watcher_recipients.include?(user.mail)
718 727 end
719 728
720 729 def test_issue_destroy
721 730 Issue.find(1).destroy
722 731 assert_nil Issue.find_by_id(1)
723 732 assert_nil TimeEntry.find_by_issue_id(1)
724 733 end
725 734
726 735 def test_blocked
727 736 blocked_issue = Issue.find(9)
728 737 blocking_issue = Issue.find(10)
729 738
730 739 assert blocked_issue.blocked?
731 740 assert !blocking_issue.blocked?
732 741 end
733 742
734 743 def test_blocked_issues_dont_allow_closed_statuses
735 744 blocked_issue = Issue.find(9)
736 745
737 746 allowed_statuses = blocked_issue.new_statuses_allowed_to(users(:users_002))
738 747 assert !allowed_statuses.empty?
739 748 closed_statuses = allowed_statuses.select {|st| st.is_closed?}
740 749 assert closed_statuses.empty?
741 750 end
742 751
743 752 def test_unblocked_issues_allow_closed_statuses
744 753 blocking_issue = Issue.find(10)
745 754
746 755 allowed_statuses = blocking_issue.new_statuses_allowed_to(users(:users_002))
747 756 assert !allowed_statuses.empty?
748 757 closed_statuses = allowed_statuses.select {|st| st.is_closed?}
749 758 assert !closed_statuses.empty?
750 759 end
751 760
752 761 def test_rescheduling_an_issue_should_reschedule_following_issue
753 762 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)
754 763 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)
755 764 IssueRelation.create!(:issue_from => issue1, :issue_to => issue2, :relation_type => IssueRelation::TYPE_PRECEDES)
756 765 assert_equal issue1.due_date + 1, issue2.reload.start_date
757 766
758 767 issue1.due_date = Date.today + 5
759 768 issue1.save!
760 769 assert_equal issue1.due_date + 1, issue2.reload.start_date
761 770 end
762 771
763 772 def test_overdue
764 773 assert Issue.new(:due_date => 1.day.ago.to_date).overdue?
765 774 assert !Issue.new(:due_date => Date.today).overdue?
766 775 assert !Issue.new(:due_date => 1.day.from_now.to_date).overdue?
767 776 assert !Issue.new(:due_date => nil).overdue?
768 777 assert !Issue.new(:due_date => 1.day.ago.to_date, :status => IssueStatus.find(:first, :conditions => {:is_closed => true})).overdue?
769 778 end
770 779
771 780 context "#behind_schedule?" do
772 781 should "be false if the issue has no start_date" do
773 782 assert !Issue.new(:start_date => nil, :due_date => 1.day.from_now.to_date, :done_ratio => 0).behind_schedule?
774 783 end
775 784
776 785 should "be false if the issue has no end_date" do
777 786 assert !Issue.new(:start_date => 1.day.from_now.to_date, :due_date => nil, :done_ratio => 0).behind_schedule?
778 787 end
779 788
780 789 should "be false if the issue has more done than it's calendar time" do
781 790 assert !Issue.new(:start_date => 50.days.ago.to_date, :due_date => 50.days.from_now.to_date, :done_ratio => 90).behind_schedule?
782 791 end
783 792
784 793 should "be true if the issue hasn't been started at all" do
785 794 assert Issue.new(:start_date => 1.day.ago.to_date, :due_date => 1.day.from_now.to_date, :done_ratio => 0).behind_schedule?
786 795 end
787 796
788 797 should "be true if the issue has used more calendar time than it's done ratio" do
789 798 assert Issue.new(:start_date => 100.days.ago.to_date, :due_date => Date.today, :done_ratio => 90).behind_schedule?
790 799 end
791 800 end
792 801
793 802 context "#assignable_users" do
794 803 should "be Users" do
795 804 assert_kind_of User, Issue.find(1).assignable_users.first
796 805 end
797 806
798 807 should "include the issue author" do
799 808 project = Project.find(1)
800 809 non_project_member = User.generate!
801 810 issue = Issue.generate_for_project!(project, :author => non_project_member)
802 811
803 812 assert issue.assignable_users.include?(non_project_member)
804 813 end
805 814
806 815 should "include the current assignee" do
807 816 project = Project.find(1)
808 817 user = User.generate!
809 818 issue = Issue.generate_for_project!(project, :assigned_to => user)
810 819 user.lock!
811 820
812 821 assert Issue.find(issue.id).assignable_users.include?(user)
813 822 end
814 823
815 824 should "not show the issue author twice" do
816 825 assignable_user_ids = Issue.find(1).assignable_users.collect(&:id)
817 826 assert_equal 2, assignable_user_ids.length
818 827
819 828 assignable_user_ids.each do |user_id|
820 829 assert_equal 1, assignable_user_ids.select {|i| i == user_id}.length, "User #{user_id} appears more or less than once"
821 830 end
822 831 end
823 832
824 833 context "with issue_group_assignment" do
825 834 should "include groups" do
826 835 issue = Issue.new(:project => Project.find(2))
827 836
828 837 with_settings :issue_group_assignment => '1' do
829 838 assert_equal %w(Group User), issue.assignable_users.map {|a| a.class.name}.uniq.sort
830 839 assert issue.assignable_users.include?(Group.find(11))
831 840 end
832 841 end
833 842 end
834 843
835 844 context "without issue_group_assignment" do
836 845 should "not include groups" do
837 846 issue = Issue.new(:project => Project.find(2))
838 847
839 848 with_settings :issue_group_assignment => '0' do
840 849 assert_equal %w(User), issue.assignable_users.map {|a| a.class.name}.uniq.sort
841 850 assert !issue.assignable_users.include?(Group.find(11))
842 851 end
843 852 end
844 853 end
845 854 end
846 855
847 856 def test_create_should_send_email_notification
848 857 ActionMailer::Base.deliveries.clear
849 858 issue = Issue.new(:project_id => 1, :tracker_id => 1,
850 859 :author_id => 3, :status_id => 1,
851 860 :priority => IssuePriority.all.first,
852 861 :subject => 'test_create', :estimated_hours => '1:30')
853 862
854 863 assert issue.save
855 864 assert_equal 1, ActionMailer::Base.deliveries.size
856 865 end
857 866
858 867 def test_stale_issue_should_not_send_email_notification
859 868 ActionMailer::Base.deliveries.clear
860 869 issue = Issue.find(1)
861 870 stale = Issue.find(1)
862 871
863 872 issue.init_journal(User.find(1))
864 873 issue.subject = 'Subjet update'
865 874 assert issue.save
866 875 assert_equal 1, ActionMailer::Base.deliveries.size
867 876 ActionMailer::Base.deliveries.clear
868 877
869 878 stale.init_journal(User.find(1))
870 879 stale.subject = 'Another subjet update'
871 880 assert_raise ActiveRecord::StaleObjectError do
872 881 stale.save
873 882 end
874 883 assert ActionMailer::Base.deliveries.empty?
875 884 end
876 885
877 886 def test_journalized_description
878 887 IssueCustomField.delete_all
879 888
880 889 i = Issue.first
881 890 old_description = i.description
882 891 new_description = "This is the new description"
883 892
884 893 i.init_journal(User.find(2))
885 894 i.description = new_description
886 895 assert_difference 'Journal.count', 1 do
887 896 assert_difference 'JournalDetail.count', 1 do
888 897 i.save!
889 898 end
890 899 end
891 900
892 901 detail = JournalDetail.first(:order => 'id DESC')
893 902 assert_equal i, detail.journal.journalized
894 903 assert_equal 'attr', detail.property
895 904 assert_equal 'description', detail.prop_key
896 905 assert_equal old_description, detail.old_value
897 906 assert_equal new_description, detail.value
898 907 end
899 908
900 909 def test_blank_descriptions_should_not_be_journalized
901 910 IssueCustomField.delete_all
902 911 Issue.update_all("description = NULL", "id=1")
903 912
904 913 i = Issue.find(1)
905 914 i.init_journal(User.find(2))
906 915 i.subject = "blank description"
907 916 i.description = "\r\n"
908 917
909 918 assert_difference 'Journal.count', 1 do
910 919 assert_difference 'JournalDetail.count', 1 do
911 920 i.save!
912 921 end
913 922 end
914 923 end
915 924
916 925 def test_description_eol_should_be_normalized
917 926 i = Issue.new(:description => "CR \r LF \n CRLF \r\n")
918 927 assert_equal "CR \r\n LF \r\n CRLF \r\n", i.description
919 928 end
920 929
921 930 def test_saving_twice_should_not_duplicate_journal_details
922 931 i = Issue.find(:first)
923 932 i.init_journal(User.find(2), 'Some notes')
924 933 # initial changes
925 934 i.subject = 'New subject'
926 935 i.done_ratio = i.done_ratio + 10
927 936 assert_difference 'Journal.count' do
928 937 assert i.save
929 938 end
930 939 # 1 more change
931 940 i.priority = IssuePriority.find(:first, :conditions => ["id <> ?", i.priority_id])
932 941 assert_no_difference 'Journal.count' do
933 942 assert_difference 'JournalDetail.count', 1 do
934 943 i.save
935 944 end
936 945 end
937 946 # no more change
938 947 assert_no_difference 'Journal.count' do
939 948 assert_no_difference 'JournalDetail.count' do
940 949 i.save
941 950 end
942 951 end
943 952 end
944 953
945 954 def test_all_dependent_issues
946 955 IssueRelation.delete_all
947 956 assert IssueRelation.create!(:issue_from => Issue.find(1),
948 957 :issue_to => Issue.find(2),
949 958 :relation_type => IssueRelation::TYPE_PRECEDES)
950 959 assert IssueRelation.create!(:issue_from => Issue.find(2),
951 960 :issue_to => Issue.find(3),
952 961 :relation_type => IssueRelation::TYPE_PRECEDES)
953 962 assert IssueRelation.create!(:issue_from => Issue.find(3),
954 963 :issue_to => Issue.find(8),
955 964 :relation_type => IssueRelation::TYPE_PRECEDES)
956 965
957 966 assert_equal [2, 3, 8], Issue.find(1).all_dependent_issues.collect(&:id).sort
958 967 end
959 968
960 969 def test_all_dependent_issues_with_persistent_circular_dependency
961 970 IssueRelation.delete_all
962 971 assert IssueRelation.create!(:issue_from => Issue.find(1),
963 972 :issue_to => Issue.find(2),
964 973 :relation_type => IssueRelation::TYPE_PRECEDES)
965 974 assert IssueRelation.create!(:issue_from => Issue.find(2),
966 975 :issue_to => Issue.find(3),
967 976 :relation_type => IssueRelation::TYPE_PRECEDES)
968 977 # Validation skipping
969 978 assert IssueRelation.new(:issue_from => Issue.find(3),
970 979 :issue_to => Issue.find(1),
971 980 :relation_type => IssueRelation::TYPE_PRECEDES).save(false)
972 981
973 982 assert_equal [2, 3], Issue.find(1).all_dependent_issues.collect(&:id).sort
974 983 end
975 984
976 985 def test_all_dependent_issues_with_persistent_multiple_circular_dependencies
977 986 IssueRelation.delete_all
978 987 assert IssueRelation.create!(:issue_from => Issue.find(1),
979 988 :issue_to => Issue.find(2),
980 989 :relation_type => IssueRelation::TYPE_RELATES)
981 990 assert IssueRelation.create!(:issue_from => Issue.find(2),
982 991 :issue_to => Issue.find(3),
983 992 :relation_type => IssueRelation::TYPE_RELATES)
984 993 assert IssueRelation.create!(:issue_from => Issue.find(3),
985 994 :issue_to => Issue.find(8),
986 995 :relation_type => IssueRelation::TYPE_RELATES)
987 996 # Validation skipping
988 997 assert IssueRelation.new(:issue_from => Issue.find(8),
989 998 :issue_to => Issue.find(2),
990 999 :relation_type => IssueRelation::TYPE_RELATES).save(false)
991 1000 assert IssueRelation.new(:issue_from => Issue.find(3),
992 1001 :issue_to => Issue.find(1),
993 1002 :relation_type => IssueRelation::TYPE_RELATES).save(false)
994 1003
995 1004 assert_equal [2, 3, 8], Issue.find(1).all_dependent_issues.collect(&:id).sort
996 1005 end
997 1006
998 1007 context "#done_ratio" do
999 1008 setup do
1000 1009 @issue = Issue.find(1)
1001 1010 @issue_status = IssueStatus.find(1)
1002 1011 @issue_status.update_attribute(:default_done_ratio, 50)
1003 1012 @issue2 = Issue.find(2)
1004 1013 @issue_status2 = IssueStatus.find(2)
1005 1014 @issue_status2.update_attribute(:default_done_ratio, 0)
1006 1015 end
1007 1016
1008 1017 teardown do
1009 1018 Setting.issue_done_ratio = 'issue_field'
1010 1019 end
1011 1020
1012 1021 context "with Setting.issue_done_ratio using the issue_field" do
1013 1022 setup do
1014 1023 Setting.issue_done_ratio = 'issue_field'
1015 1024 end
1016 1025
1017 1026 should "read the issue's field" do
1018 1027 assert_equal 0, @issue.done_ratio
1019 1028 assert_equal 30, @issue2.done_ratio
1020 1029 end
1021 1030 end
1022 1031
1023 1032 context "with Setting.issue_done_ratio using the issue_status" do
1024 1033 setup do
1025 1034 Setting.issue_done_ratio = 'issue_status'
1026 1035 end
1027 1036
1028 1037 should "read the Issue Status's default done ratio" do
1029 1038 assert_equal 50, @issue.done_ratio
1030 1039 assert_equal 0, @issue2.done_ratio
1031 1040 end
1032 1041 end
1033 1042 end
1034 1043
1035 1044 context "#update_done_ratio_from_issue_status" do
1036 1045 setup do
1037 1046 @issue = Issue.find(1)
1038 1047 @issue_status = IssueStatus.find(1)
1039 1048 @issue_status.update_attribute(:default_done_ratio, 50)
1040 1049 @issue2 = Issue.find(2)
1041 1050 @issue_status2 = IssueStatus.find(2)
1042 1051 @issue_status2.update_attribute(:default_done_ratio, 0)
1043 1052 end
1044 1053
1045 1054 context "with Setting.issue_done_ratio using the issue_field" do
1046 1055 setup do
1047 1056 Setting.issue_done_ratio = 'issue_field'
1048 1057 end
1049 1058
1050 1059 should "not change the issue" do
1051 1060 @issue.update_done_ratio_from_issue_status
1052 1061 @issue2.update_done_ratio_from_issue_status
1053 1062
1054 1063 assert_equal 0, @issue.read_attribute(:done_ratio)
1055 1064 assert_equal 30, @issue2.read_attribute(:done_ratio)
1056 1065 end
1057 1066 end
1058 1067
1059 1068 context "with Setting.issue_done_ratio using the issue_status" do
1060 1069 setup do
1061 1070 Setting.issue_done_ratio = 'issue_status'
1062 1071 end
1063 1072
1064 1073 should "change the issue's done ratio" do
1065 1074 @issue.update_done_ratio_from_issue_status
1066 1075 @issue2.update_done_ratio_from_issue_status
1067 1076
1068 1077 assert_equal 50, @issue.read_attribute(:done_ratio)
1069 1078 assert_equal 0, @issue2.read_attribute(:done_ratio)
1070 1079 end
1071 1080 end
1072 1081 end
1073 1082
1074 1083 test "#by_tracker" do
1075 1084 User.current = User.anonymous
1076 1085 groups = Issue.by_tracker(Project.find(1))
1077 1086 assert_equal 3, groups.size
1078 1087 assert_equal 7, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1079 1088 end
1080 1089
1081 1090 test "#by_version" do
1082 1091 User.current = User.anonymous
1083 1092 groups = Issue.by_version(Project.find(1))
1084 1093 assert_equal 3, groups.size
1085 1094 assert_equal 3, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1086 1095 end
1087 1096
1088 1097 test "#by_priority" do
1089 1098 User.current = User.anonymous
1090 1099 groups = Issue.by_priority(Project.find(1))
1091 1100 assert_equal 4, groups.size
1092 1101 assert_equal 7, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1093 1102 end
1094 1103
1095 1104 test "#by_category" do
1096 1105 User.current = User.anonymous
1097 1106 groups = Issue.by_category(Project.find(1))
1098 1107 assert_equal 2, groups.size
1099 1108 assert_equal 3, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1100 1109 end
1101 1110
1102 1111 test "#by_assigned_to" do
1103 1112 User.current = User.anonymous
1104 1113 groups = Issue.by_assigned_to(Project.find(1))
1105 1114 assert_equal 2, groups.size
1106 1115 assert_equal 2, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1107 1116 end
1108 1117
1109 1118 test "#by_author" do
1110 1119 User.current = User.anonymous
1111 1120 groups = Issue.by_author(Project.find(1))
1112 1121 assert_equal 4, groups.size
1113 1122 assert_equal 7, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1114 1123 end
1115 1124
1116 1125 test "#by_subproject" do
1117 1126 User.current = User.anonymous
1118 1127 groups = Issue.by_subproject(Project.find(1))
1119 1128 # Private descendant not visible
1120 1129 assert_equal 1, groups.size
1121 1130 assert_equal 2, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1122 1131 end
1123 1132
1124 1133 context ".allowed_target_projects_on_move" do
1125 1134 should "return all active projects for admin users" do
1126 1135 User.current = User.find(1)
1127 1136 assert_equal Project.active.count, Issue.allowed_target_projects_on_move.size
1128 1137 end
1129 1138
1130 1139 should "return allowed projects for non admin users" do
1131 1140 User.current = User.find(2)
1132 1141 Role.non_member.remove_permission! :move_issues
1133 1142 assert_equal 3, Issue.allowed_target_projects_on_move.size
1134 1143
1135 1144 Role.non_member.add_permission! :move_issues
1136 1145 assert_equal Project.active.count, Issue.allowed_target_projects_on_move.size
1137 1146 end
1138 1147 end
1139 1148
1140 1149 def test_recently_updated_with_limit_scopes
1141 1150 #should return the last updated issue
1142 1151 assert_equal 1, Issue.recently_updated.with_limit(1).length
1143 1152 assert_equal Issue.find(:first, :order => "updated_on DESC"), Issue.recently_updated.with_limit(1).first
1144 1153 end
1145 1154
1146 1155 def test_on_active_projects_scope
1147 1156 assert Project.find(2).archive
1148 1157
1149 1158 before = Issue.on_active_project.length
1150 1159 # test inclusion to results
1151 1160 issue = Issue.generate_for_project!(Project.find(1), :tracker => Project.find(2).trackers.first)
1152 1161 assert_equal before + 1, Issue.on_active_project.length
1153 1162
1154 1163 # Move to an archived project
1155 1164 issue.project = Project.find(2)
1156 1165 assert issue.save
1157 1166 assert_equal before, Issue.on_active_project.length
1158 1167 end
1159 1168
1160 1169 context "Issue#recipients" do
1161 1170 setup do
1162 1171 @project = Project.find(1)
1163 1172 @author = User.generate_with_protected!
1164 1173 @assignee = User.generate_with_protected!
1165 1174 @issue = Issue.generate_for_project!(@project, :assigned_to => @assignee, :author => @author)
1166 1175 end
1167 1176
1168 1177 should "include project recipients" do
1169 1178 assert @project.recipients.present?
1170 1179 @project.recipients.each do |project_recipient|
1171 1180 assert @issue.recipients.include?(project_recipient)
1172 1181 end
1173 1182 end
1174 1183
1175 1184 should "include the author if the author is active" do
1176 1185 assert @issue.author, "No author set for Issue"
1177 1186 assert @issue.recipients.include?(@issue.author.mail)
1178 1187 end
1179 1188
1180 1189 should "include the assigned to user if the assigned to user is active" do
1181 1190 assert @issue.assigned_to, "No assigned_to set for Issue"
1182 1191 assert @issue.recipients.include?(@issue.assigned_to.mail)
1183 1192 end
1184 1193
1185 1194 should "not include users who opt out of all email" do
1186 1195 @author.update_attribute(:mail_notification, :none)
1187 1196
1188 1197 assert !@issue.recipients.include?(@issue.author.mail)
1189 1198 end
1190 1199
1191 1200 should "not include the issue author if they are only notified of assigned issues" do
1192 1201 @author.update_attribute(:mail_notification, :only_assigned)
1193 1202
1194 1203 assert !@issue.recipients.include?(@issue.author.mail)
1195 1204 end
1196 1205
1197 1206 should "not include the assigned user if they are only notified of owned issues" do
1198 1207 @assignee.update_attribute(:mail_notification, :only_owner)
1199 1208
1200 1209 assert !@issue.recipients.include?(@issue.assigned_to.mail)
1201 1210 end
1202 1211
1203 1212 end
1204 1213 end
General Comments 0
You need to be logged in to leave comments. Login now