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