##// END OF EJS Templates
Fixed: error while moving an issue to a project with disabled tracker with SQLite3 (#5049)....
Jean-Philippe Lang -
r3452:3dc4dbe30279
parent child
Show More
@@ -1,614 +1,614
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 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 belongs_to :project
20 20 belongs_to :tracker
21 21 belongs_to :status, :class_name => 'IssueStatus', :foreign_key => 'status_id'
22 22 belongs_to :author, :class_name => 'User', :foreign_key => 'author_id'
23 23 belongs_to :assigned_to, :class_name => 'User', :foreign_key => 'assigned_to_id'
24 24 belongs_to :fixed_version, :class_name => 'Version', :foreign_key => 'fixed_version_id'
25 25 belongs_to :priority, :class_name => 'IssuePriority', :foreign_key => 'priority_id'
26 26 belongs_to :category, :class_name => 'IssueCategory', :foreign_key => 'category_id'
27 27
28 28 has_many :journals, :as => :journalized, :dependent => :destroy
29 29 has_many :time_entries, :dependent => :delete_all
30 30 has_and_belongs_to_many :changesets, :order => "#{Changeset.table_name}.committed_on ASC, #{Changeset.table_name}.id ASC"
31 31
32 32 has_many :relations_from, :class_name => 'IssueRelation', :foreign_key => 'issue_from_id', :dependent => :delete_all
33 33 has_many :relations_to, :class_name => 'IssueRelation', :foreign_key => 'issue_to_id', :dependent => :delete_all
34 34
35 35 acts_as_attachable :after_remove => :attachment_removed
36 36 acts_as_customizable
37 37 acts_as_watchable
38 38 acts_as_searchable :columns => ['subject', "#{table_name}.description", "#{Journal.table_name}.notes"],
39 39 :include => [:project, :journals],
40 40 # sort by id so that limited eager loading doesn't break with postgresql
41 41 :order_column => "#{table_name}.id"
42 42 acts_as_event :title => Proc.new {|o| "#{o.tracker.name} ##{o.id} (#{o.status}): #{o.subject}"},
43 43 :url => Proc.new {|o| {:controller => 'issues', :action => 'show', :id => o.id}},
44 44 :type => Proc.new {|o| 'issue' + (o.closed? ? ' closed' : '') }
45 45
46 46 acts_as_activity_provider :find_options => {:include => [:project, :author, :tracker]},
47 47 :author_key => :author_id
48 48
49 49 DONE_RATIO_OPTIONS = %w(issue_field issue_status)
50 50
51 51 attr_reader :current_journal
52 52
53 53 validates_presence_of :subject, :priority, :project, :tracker, :author, :status
54 54
55 55 validates_length_of :subject, :maximum => 255
56 56 validates_inclusion_of :done_ratio, :in => 0..100
57 57 validates_numericality_of :estimated_hours, :allow_nil => true
58 58
59 59 named_scope :visible, lambda {|*args| { :include => :project,
60 60 :conditions => Project.allowed_to_condition(args.first || User.current, :view_issues) } }
61 61
62 62 named_scope :open, :conditions => ["#{IssueStatus.table_name}.is_closed = ?", false], :include => :status
63 63
64 64 named_scope :recently_updated, :order => "#{self.table_name}.updated_on DESC"
65 65 named_scope :with_limit, lambda { |limit| { :limit => limit} }
66 66 named_scope :on_active_project, :include => [:status, :project, :tracker],
67 67 :conditions => ["#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"]
68 68
69 69 before_create :default_assign
70 70 before_save :reschedule_following_issues, :close_duplicates, :update_done_ratio_from_issue_status
71 71 after_save :create_journal
72 72
73 73 # Returns true if usr or current user is allowed to view the issue
74 74 def visible?(usr=nil)
75 75 (usr || User.current).allowed_to?(:view_issues, self.project)
76 76 end
77 77
78 78 def after_initialize
79 79 if new_record?
80 80 # set default values for new records only
81 81 self.status ||= IssueStatus.default
82 82 self.priority ||= IssuePriority.default
83 83 end
84 84 end
85 85
86 86 # Overrides Redmine::Acts::Customizable::InstanceMethods#available_custom_fields
87 87 def available_custom_fields
88 88 (project && tracker) ? project.all_issue_custom_fields.select {|c| tracker.custom_fields.include? c } : []
89 89 end
90 90
91 91 def copy_from(arg)
92 92 issue = arg.is_a?(Issue) ? arg : Issue.find(arg)
93 93 self.attributes = issue.attributes.dup.except("id", "created_on", "updated_on")
94 94 self.custom_values = issue.custom_values.collect {|v| v.clone}
95 95 self.status = issue.status
96 96 self
97 97 end
98 98
99 99 # Moves/copies an issue to a new project and tracker
100 100 # Returns the moved/copied issue on success, false on failure
101 101 def move_to(new_project, new_tracker = nil, options = {})
102 102 options ||= {}
103 103 issue = options[:copy] ? self.clone : self
104 transaction do
104 ret = Issue.transaction do
105 105 if new_project && issue.project_id != new_project.id
106 106 # delete issue relations
107 107 unless Setting.cross_project_issue_relations?
108 108 issue.relations_from.clear
109 109 issue.relations_to.clear
110 110 end
111 111 # issue is moved to another project
112 112 # reassign to the category with same name if any
113 113 new_category = issue.category.nil? ? nil : new_project.issue_categories.find_by_name(issue.category.name)
114 114 issue.category = new_category
115 115 # Keep the fixed_version if it's still valid in the new_project
116 116 unless new_project.shared_versions.include?(issue.fixed_version)
117 117 issue.fixed_version = nil
118 118 end
119 119 issue.project = new_project
120 120 end
121 121 if new_tracker
122 122 issue.tracker = new_tracker
123 123 end
124 124 if options[:copy]
125 125 issue.custom_field_values = self.custom_field_values.inject({}) {|h,v| h[v.custom_field_id] = v.value; h}
126 126 issue.status = if options[:attributes] && options[:attributes][:status_id]
127 127 IssueStatus.find_by_id(options[:attributes][:status_id])
128 128 else
129 129 self.status
130 130 end
131 131 end
132 132 # Allow bulk setting of attributes on the issue
133 133 if options[:attributes]
134 134 issue.attributes = options[:attributes]
135 135 end
136 136 if issue.save
137 137 unless options[:copy]
138 138 # Manually update project_id on related time entries
139 139 TimeEntry.update_all("project_id = #{new_project.id}", {:issue_id => id})
140 140 end
141 true
141 142 else
142 Issue.connection.rollback_db_transaction
143 return false
143 raise ActiveRecord::Rollback
144 144 end
145 145 end
146 return issue
146 ret ? issue : false
147 147 end
148 148
149 149 def priority_id=(pid)
150 150 self.priority = nil
151 151 write_attribute(:priority_id, pid)
152 152 end
153 153
154 154 def tracker_id=(tid)
155 155 self.tracker = nil
156 156 result = write_attribute(:tracker_id, tid)
157 157 @custom_field_values = nil
158 158 result
159 159 end
160 160
161 161 # Overrides attributes= so that tracker_id gets assigned first
162 162 def attributes_with_tracker_first=(new_attributes, *args)
163 163 return if new_attributes.nil?
164 164 new_tracker_id = new_attributes['tracker_id'] || new_attributes[:tracker_id]
165 165 if new_tracker_id
166 166 self.tracker_id = new_tracker_id
167 167 end
168 168 send :attributes_without_tracker_first=, new_attributes, *args
169 169 end
170 170 # Do not redefine alias chain on reload (see #4838)
171 171 alias_method_chain(:attributes=, :tracker_first) unless method_defined?(:attributes_without_tracker_first=)
172 172
173 173 def estimated_hours=(h)
174 174 write_attribute :estimated_hours, (h.is_a?(String) ? h.to_hours : h)
175 175 end
176 176
177 177 SAFE_ATTRIBUTES = %w(
178 178 tracker_id
179 179 status_id
180 180 category_id
181 181 assigned_to_id
182 182 priority_id
183 183 fixed_version_id
184 184 subject
185 185 description
186 186 start_date
187 187 due_date
188 188 done_ratio
189 189 estimated_hours
190 190 custom_field_values
191 191 ) unless const_defined?(:SAFE_ATTRIBUTES)
192 192
193 193 # Safely sets attributes
194 194 # Should be called from controllers instead of #attributes=
195 195 # attr_accessible is too rough because we still want things like
196 196 # Issue.new(:project => foo) to work
197 197 # TODO: move workflow/permission checks from controllers to here
198 198 def safe_attributes=(attrs, user=User.current)
199 199 return if attrs.nil?
200 200 attrs = attrs.reject {|k,v| !SAFE_ATTRIBUTES.include?(k)}
201 201 if attrs['status_id']
202 202 unless new_statuses_allowed_to(user).collect(&:id).include?(attrs['status_id'].to_i)
203 203 attrs.delete('status_id')
204 204 end
205 205 end
206 206 self.attributes = attrs
207 207 end
208 208
209 209 def done_ratio
210 210 if Issue.use_status_for_done_ratio? && status && status.default_done_ratio?
211 211 status.default_done_ratio
212 212 else
213 213 read_attribute(:done_ratio)
214 214 end
215 215 end
216 216
217 217 def self.use_status_for_done_ratio?
218 218 Setting.issue_done_ratio == 'issue_status'
219 219 end
220 220
221 221 def self.use_field_for_done_ratio?
222 222 Setting.issue_done_ratio == 'issue_field'
223 223 end
224 224
225 225 def validate
226 226 if self.due_date.nil? && @attributes['due_date'] && !@attributes['due_date'].empty?
227 227 errors.add :due_date, :not_a_date
228 228 end
229 229
230 230 if self.due_date and self.start_date and self.due_date < self.start_date
231 231 errors.add :due_date, :greater_than_start_date
232 232 end
233 233
234 234 if start_date && soonest_start && start_date < soonest_start
235 235 errors.add :start_date, :invalid
236 236 end
237 237
238 238 if fixed_version
239 239 if !assignable_versions.include?(fixed_version)
240 240 errors.add :fixed_version_id, :inclusion
241 241 elsif reopened? && fixed_version.closed?
242 242 errors.add_to_base I18n.t(:error_can_not_reopen_issue_on_closed_version)
243 243 end
244 244 end
245 245
246 246 # Checks that the issue can not be added/moved to a disabled tracker
247 247 if project && (tracker_id_changed? || project_id_changed?)
248 248 unless project.trackers.include?(tracker)
249 249 errors.add :tracker_id, :inclusion
250 250 end
251 251 end
252 252 end
253 253
254 254 # Set the done_ratio using the status if that setting is set. This will keep the done_ratios
255 255 # even if the user turns off the setting later
256 256 def update_done_ratio_from_issue_status
257 257 if Issue.use_status_for_done_ratio? && status && status.default_done_ratio?
258 258 self.done_ratio = status.default_done_ratio
259 259 end
260 260 end
261 261
262 262 def init_journal(user, notes = "")
263 263 @current_journal ||= Journal.new(:journalized => self, :user => user, :notes => notes)
264 264 @issue_before_change = self.clone
265 265 @issue_before_change.status = self.status
266 266 @custom_values_before_change = {}
267 267 self.custom_values.each {|c| @custom_values_before_change.store c.custom_field_id, c.value }
268 268 # Make sure updated_on is updated when adding a note.
269 269 updated_on_will_change!
270 270 @current_journal
271 271 end
272 272
273 273 # Return true if the issue is closed, otherwise false
274 274 def closed?
275 275 self.status.is_closed?
276 276 end
277 277
278 278 # Return true if the issue is being reopened
279 279 def reopened?
280 280 if !new_record? && status_id_changed?
281 281 status_was = IssueStatus.find_by_id(status_id_was)
282 282 status_new = IssueStatus.find_by_id(status_id)
283 283 if status_was && status_new && status_was.is_closed? && !status_new.is_closed?
284 284 return true
285 285 end
286 286 end
287 287 false
288 288 end
289 289
290 290 # Return true if the issue is being closed
291 291 def closing?
292 292 if !new_record? && status_id_changed?
293 293 status_was = IssueStatus.find_by_id(status_id_was)
294 294 status_new = IssueStatus.find_by_id(status_id)
295 295 if status_was && status_new && !status_was.is_closed? && status_new.is_closed?
296 296 return true
297 297 end
298 298 end
299 299 false
300 300 end
301 301
302 302 # Returns true if the issue is overdue
303 303 def overdue?
304 304 !due_date.nil? && (due_date < Date.today) && !status.is_closed?
305 305 end
306 306
307 307 # Users the issue can be assigned to
308 308 def assignable_users
309 309 project.assignable_users
310 310 end
311 311
312 312 # Versions that the issue can be assigned to
313 313 def assignable_versions
314 314 @assignable_versions ||= (project.shared_versions.open + [Version.find_by_id(fixed_version_id_was)]).compact.uniq.sort
315 315 end
316 316
317 317 # Returns true if this issue is blocked by another issue that is still open
318 318 def blocked?
319 319 !relations_to.detect {|ir| ir.relation_type == 'blocks' && !ir.issue_from.closed?}.nil?
320 320 end
321 321
322 322 # Returns an array of status that user is able to apply
323 323 def new_statuses_allowed_to(user, include_default=false)
324 324 statuses = status.find_new_statuses_allowed_to(user.roles_for_project(project), tracker)
325 325 statuses << status unless statuses.empty?
326 326 statuses << IssueStatus.default if include_default
327 327 statuses = statuses.uniq.sort
328 328 blocked? ? statuses.reject {|s| s.is_closed?} : statuses
329 329 end
330 330
331 331 # Returns the mail adresses of users that should be notified
332 332 def recipients
333 333 notified = project.notified_users
334 334 # Author and assignee are always notified unless they have been locked
335 335 notified << author if author && author.active?
336 336 notified << assigned_to if assigned_to && assigned_to.active?
337 337 notified.uniq!
338 338 # Remove users that can not view the issue
339 339 notified.reject! {|user| !visible?(user)}
340 340 notified.collect(&:mail)
341 341 end
342 342
343 343 # Returns the total number of hours spent on this issue.
344 344 #
345 345 # Example:
346 346 # spent_hours => 0
347 347 # spent_hours => 50
348 348 def spent_hours
349 349 @spent_hours ||= time_entries.sum(:hours) || 0
350 350 end
351 351
352 352 def relations
353 353 (relations_from + relations_to).sort
354 354 end
355 355
356 356 def all_dependent_issues
357 357 dependencies = []
358 358 relations_from.each do |relation|
359 359 dependencies << relation.issue_to
360 360 dependencies += relation.issue_to.all_dependent_issues
361 361 end
362 362 dependencies
363 363 end
364 364
365 365 # Returns an array of issues that duplicate this one
366 366 def duplicates
367 367 relations_to.select {|r| r.relation_type == IssueRelation::TYPE_DUPLICATES}.collect {|r| r.issue_from}
368 368 end
369 369
370 370 # Returns the due date or the target due date if any
371 371 # Used on gantt chart
372 372 def due_before
373 373 due_date || (fixed_version ? fixed_version.effective_date : nil)
374 374 end
375 375
376 376 # Returns the time scheduled for this issue.
377 377 #
378 378 # Example:
379 379 # Start Date: 2/26/09, End Date: 3/04/09
380 380 # duration => 6
381 381 def duration
382 382 (start_date && due_date) ? due_date - start_date : 0
383 383 end
384 384
385 385 def soonest_start
386 386 @soonest_start ||= relations_to.collect{|relation| relation.successor_soonest_start}.compact.min
387 387 end
388 388
389 389 def to_s
390 390 "#{tracker} ##{id}: #{subject}"
391 391 end
392 392
393 393 # Returns a string of css classes that apply to the issue
394 394 def css_classes
395 395 s = "issue status-#{status.position} priority-#{priority.position}"
396 396 s << ' closed' if closed?
397 397 s << ' overdue' if overdue?
398 398 s << ' created-by-me' if User.current.logged? && author_id == User.current.id
399 399 s << ' assigned-to-me' if User.current.logged? && assigned_to_id == User.current.id
400 400 s
401 401 end
402 402
403 403 # Saves an issue, time_entry, attachments, and a journal from the parameters
404 404 def save_issue_with_child_records(params, existing_time_entry=nil)
405 405 if params[:time_entry] && params[:time_entry][:hours].present? && User.current.allowed_to?(:log_time, project)
406 406 @time_entry = existing_time_entry || TimeEntry.new
407 407 @time_entry.project = project
408 408 @time_entry.issue = self
409 409 @time_entry.user = User.current
410 410 @time_entry.spent_on = Date.today
411 411 @time_entry.attributes = params[:time_entry]
412 412 self.time_entries << @time_entry
413 413 end
414 414
415 415 if valid?
416 416 attachments = Attachment.attach_files(self, params[:attachments])
417 417
418 418 attachments[:files].each {|a| @current_journal.details << JournalDetail.new(:property => 'attachment', :prop_key => a.id, :value => a.filename)}
419 419 # TODO: Rename hook
420 420 Redmine::Hook.call_hook(:controller_issues_edit_before_save, { :params => params, :issue => self, :time_entry => @time_entry, :journal => @current_journal})
421 421 if save
422 422 # TODO: Rename hook
423 423 Redmine::Hook.call_hook(:controller_issues_edit_after_save, { :params => params, :issue => self, :time_entry => @time_entry, :journal => @current_journal})
424 424 return true
425 425 end
426 426 end
427 427 # failure, returns false
428 428
429 429 end
430 430
431 431 # Unassigns issues from +version+ if it's no longer shared with issue's project
432 432 def self.update_versions_from_sharing_change(version)
433 433 # Update issues assigned to the version
434 434 update_versions(["#{Issue.table_name}.fixed_version_id = ?", version.id])
435 435 end
436 436
437 437 # Unassigns issues from versions that are no longer shared
438 438 # after +project+ was moved
439 439 def self.update_versions_from_hierarchy_change(project)
440 440 moved_project_ids = project.self_and_descendants.reload.collect(&:id)
441 441 # Update issues of the moved projects and issues assigned to a version of a moved project
442 442 Issue.update_versions(["#{Version.table_name}.project_id IN (?) OR #{Issue.table_name}.project_id IN (?)", moved_project_ids, moved_project_ids])
443 443 end
444 444
445 445 # Extracted from the ReportsController.
446 446 def self.by_tracker(project)
447 447 count_and_group_by(:project => project,
448 448 :field => 'tracker_id',
449 449 :joins => Tracker.table_name)
450 450 end
451 451
452 452 def self.by_version(project)
453 453 count_and_group_by(:project => project,
454 454 :field => 'fixed_version_id',
455 455 :joins => Version.table_name)
456 456 end
457 457
458 458 def self.by_priority(project)
459 459 count_and_group_by(:project => project,
460 460 :field => 'priority_id',
461 461 :joins => IssuePriority.table_name)
462 462 end
463 463
464 464 def self.by_category(project)
465 465 count_and_group_by(:project => project,
466 466 :field => 'category_id',
467 467 :joins => IssueCategory.table_name)
468 468 end
469 469
470 470 def self.by_assigned_to(project)
471 471 count_and_group_by(:project => project,
472 472 :field => 'assigned_to_id',
473 473 :joins => User.table_name)
474 474 end
475 475
476 476 def self.by_author(project)
477 477 count_and_group_by(:project => project,
478 478 :field => 'author_id',
479 479 :joins => User.table_name)
480 480 end
481 481
482 482 def self.by_subproject(project)
483 483 ActiveRecord::Base.connection.select_all("select s.id as status_id,
484 484 s.is_closed as closed,
485 485 i.project_id as project_id,
486 486 count(i.id) as total
487 487 from
488 488 #{Issue.table_name} i, #{IssueStatus.table_name} s
489 489 where
490 490 i.status_id=s.id
491 491 and i.project_id IN (#{project.descendants.active.collect{|p| p.id}.join(',')})
492 492 group by s.id, s.is_closed, i.project_id") if project.descendants.active.any?
493 493 end
494 494 # End ReportsController extraction
495 495
496 496 private
497 497
498 498 # Update issues so their versions are not pointing to a
499 499 # fixed_version that is not shared with the issue's project
500 500 def self.update_versions(conditions=nil)
501 501 # Only need to update issues with a fixed_version from
502 502 # a different project and that is not systemwide shared
503 503 Issue.all(:conditions => merge_conditions("#{Issue.table_name}.fixed_version_id IS NOT NULL" +
504 504 " AND #{Issue.table_name}.project_id <> #{Version.table_name}.project_id" +
505 505 " AND #{Version.table_name}.sharing <> 'system'",
506 506 conditions),
507 507 :include => [:project, :fixed_version]
508 508 ).each do |issue|
509 509 next if issue.project.nil? || issue.fixed_version.nil?
510 510 unless issue.project.shared_versions.include?(issue.fixed_version)
511 511 issue.init_journal(User.current)
512 512 issue.fixed_version = nil
513 513 issue.save
514 514 end
515 515 end
516 516 end
517 517
518 518 # Callback on attachment deletion
519 519 def attachment_removed(obj)
520 520 journal = init_journal(User.current)
521 521 journal.details << JournalDetail.new(:property => 'attachment',
522 522 :prop_key => obj.id,
523 523 :old_value => obj.filename)
524 524 journal.save
525 525 end
526 526
527 527 # Default assignment based on category
528 528 def default_assign
529 529 if assigned_to.nil? && category && category.assigned_to
530 530 self.assigned_to = category.assigned_to
531 531 end
532 532 end
533 533
534 534 # Updates start/due dates of following issues
535 535 def reschedule_following_issues
536 536 if start_date_changed? || due_date_changed?
537 537 relations_from.each do |relation|
538 538 relation.set_issue_to_dates
539 539 end
540 540 end
541 541 end
542 542
543 543 # Closes duplicates if the issue is being closed
544 544 def close_duplicates
545 545 if closing?
546 546 duplicates.each do |duplicate|
547 547 # Reload is need in case the duplicate was updated by a previous duplicate
548 548 duplicate.reload
549 549 # Don't re-close it if it's already closed
550 550 next if duplicate.closed?
551 551 # Same user and notes
552 552 if @current_journal
553 553 duplicate.init_journal(@current_journal.user, @current_journal.notes)
554 554 end
555 555 duplicate.update_attribute :status, self.status
556 556 end
557 557 end
558 558 end
559 559
560 560 # Saves the changes in a Journal
561 561 # Called after_save
562 562 def create_journal
563 563 if @current_journal
564 564 # attributes changes
565 565 (Issue.column_names - %w(id description lock_version created_on updated_on)).each {|c|
566 566 @current_journal.details << JournalDetail.new(:property => 'attr',
567 567 :prop_key => c,
568 568 :old_value => @issue_before_change.send(c),
569 569 :value => send(c)) unless send(c)==@issue_before_change.send(c)
570 570 }
571 571 # custom fields changes
572 572 custom_values.each {|c|
573 573 next if (@custom_values_before_change[c.custom_field_id]==c.value ||
574 574 (@custom_values_before_change[c.custom_field_id].blank? && c.value.blank?))
575 575 @current_journal.details << JournalDetail.new(:property => 'cf',
576 576 :prop_key => c.custom_field_id,
577 577 :old_value => @custom_values_before_change[c.custom_field_id],
578 578 :value => c.value)
579 579 }
580 580 @current_journal.save
581 581 # reset current journal
582 582 init_journal @current_journal.user, @current_journal.notes
583 583 end
584 584 end
585 585
586 586 # Query generator for selecting groups of issue counts for a project
587 587 # based on specific criteria
588 588 #
589 589 # Options
590 590 # * project - Project to search in.
591 591 # * field - String. Issue field to key off of in the grouping.
592 592 # * joins - String. The table name to join against.
593 593 def self.count_and_group_by(options)
594 594 project = options.delete(:project)
595 595 select_field = options.delete(:field)
596 596 joins = options.delete(:joins)
597 597
598 598 where = "i.#{select_field}=j.id"
599 599
600 600 ActiveRecord::Base.connection.select_all("select s.id as status_id,
601 601 s.is_closed as closed,
602 602 j.id as #{select_field},
603 603 count(i.id) as total
604 604 from
605 605 #{Issue.table_name} i, #{IssueStatus.table_name} s, #{joins} as j
606 606 where
607 607 i.status_id=s.id
608 608 and #{where}
609 609 and i.project_id=#{project.id}
610 610 group by s.id, s.is_closed, j.id")
611 611 end
612 612
613 613
614 614 end
@@ -1,679 +1,689
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 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.dirname(__FILE__) + '/../test_helper'
19 19
20 20 class IssueTest < ActiveSupport::TestCase
21 21 fixtures :projects, :users, :members, :member_roles, :roles,
22 22 :trackers, :projects_trackers,
23 23 :enabled_modules,
24 24 :versions,
25 25 :issue_statuses, :issue_categories, :issue_relations, :workflows,
26 26 :enumerations,
27 27 :issues,
28 28 :custom_fields, :custom_fields_projects, :custom_fields_trackers, :custom_values,
29 29 :time_entries
30 30
31 31 def test_create
32 32 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3, :status_id => 1, :priority => IssuePriority.all.first, :subject => 'test_create', :description => 'IssueTest#test_create', :estimated_hours => '1:30')
33 33 assert issue.save
34 34 issue.reload
35 35 assert_equal 1.5, issue.estimated_hours
36 36 end
37 37
38 38 def test_create_minimal
39 39 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3, :status_id => 1, :priority => IssuePriority.all.first, :subject => 'test_create')
40 40 assert issue.save
41 41 assert issue.description.nil?
42 42 end
43 43
44 44 def test_create_with_required_custom_field
45 45 field = IssueCustomField.find_by_name('Database')
46 46 field.update_attribute(:is_required, true)
47 47
48 48 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :subject => 'test_create', :description => 'IssueTest#test_create_with_required_custom_field')
49 49 assert issue.available_custom_fields.include?(field)
50 50 # No value for the custom field
51 51 assert !issue.save
52 52 assert_equal I18n.translate('activerecord.errors.messages.invalid'), issue.errors.on(:custom_values)
53 53 # Blank value
54 54 issue.custom_field_values = { field.id => '' }
55 55 assert !issue.save
56 56 assert_equal I18n.translate('activerecord.errors.messages.invalid'), issue.errors.on(:custom_values)
57 57 # Invalid value
58 58 issue.custom_field_values = { field.id => 'SQLServer' }
59 59 assert !issue.save
60 60 assert_equal I18n.translate('activerecord.errors.messages.invalid'), issue.errors.on(:custom_values)
61 61 # Valid value
62 62 issue.custom_field_values = { field.id => 'PostgreSQL' }
63 63 assert issue.save
64 64 issue.reload
65 65 assert_equal 'PostgreSQL', issue.custom_value_for(field).value
66 66 end
67 67
68 68 def test_visible_scope_for_anonymous
69 69 # Anonymous user should see issues of public projects only
70 70 issues = Issue.visible(User.anonymous).all
71 71 assert issues.any?
72 72 assert_nil issues.detect {|issue| !issue.project.is_public?}
73 73 # Anonymous user should not see issues without permission
74 74 Role.anonymous.remove_permission!(:view_issues)
75 75 issues = Issue.visible(User.anonymous).all
76 76 assert issues.empty?
77 77 end
78 78
79 79 def test_visible_scope_for_user
80 80 user = User.find(9)
81 81 assert user.projects.empty?
82 82 # Non member user should see issues of public projects only
83 83 issues = Issue.visible(user).all
84 84 assert issues.any?
85 85 assert_nil issues.detect {|issue| !issue.project.is_public?}
86 86 # Non member user should not see issues without permission
87 87 Role.non_member.remove_permission!(:view_issues)
88 88 user.reload
89 89 issues = Issue.visible(user).all
90 90 assert issues.empty?
91 91 # User should see issues of projects for which he has view_issues permissions only
92 92 Member.create!(:principal => user, :project_id => 2, :role_ids => [1])
93 93 user.reload
94 94 issues = Issue.visible(user).all
95 95 assert issues.any?
96 96 assert_nil issues.detect {|issue| issue.project_id != 2}
97 97 end
98 98
99 99 def test_visible_scope_for_admin
100 100 user = User.find(1)
101 101 user.members.each(&:destroy)
102 102 assert user.projects.empty?
103 103 issues = Issue.visible(user).all
104 104 assert issues.any?
105 105 # Admin should see issues on private projects that he does not belong to
106 106 assert issues.detect {|issue| !issue.project.is_public?}
107 107 end
108 108
109 109 def test_errors_full_messages_should_include_custom_fields_errors
110 110 field = IssueCustomField.find_by_name('Database')
111 111
112 112 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :subject => 'test_create', :description => 'IssueTest#test_create_with_required_custom_field')
113 113 assert issue.available_custom_fields.include?(field)
114 114 # Invalid value
115 115 issue.custom_field_values = { field.id => 'SQLServer' }
116 116
117 117 assert !issue.valid?
118 118 assert_equal 1, issue.errors.full_messages.size
119 119 assert_equal "Database #{I18n.translate('activerecord.errors.messages.inclusion')}", issue.errors.full_messages.first
120 120 end
121 121
122 122 def test_update_issue_with_required_custom_field
123 123 field = IssueCustomField.find_by_name('Database')
124 124 field.update_attribute(:is_required, true)
125 125
126 126 issue = Issue.find(1)
127 127 assert_nil issue.custom_value_for(field)
128 128 assert issue.available_custom_fields.include?(field)
129 129 # No change to custom values, issue can be saved
130 130 assert issue.save
131 131 # Blank value
132 132 issue.custom_field_values = { field.id => '' }
133 133 assert !issue.save
134 134 # Valid value
135 135 issue.custom_field_values = { field.id => 'PostgreSQL' }
136 136 assert issue.save
137 137 issue.reload
138 138 assert_equal 'PostgreSQL', issue.custom_value_for(field).value
139 139 end
140 140
141 141 def test_should_not_update_attributes_if_custom_fields_validation_fails
142 142 issue = Issue.find(1)
143 143 field = IssueCustomField.find_by_name('Database')
144 144 assert issue.available_custom_fields.include?(field)
145 145
146 146 issue.custom_field_values = { field.id => 'Invalid' }
147 147 issue.subject = 'Should be not be saved'
148 148 assert !issue.save
149 149
150 150 issue.reload
151 151 assert_equal "Can't print recipes", issue.subject
152 152 end
153 153
154 154 def test_should_not_recreate_custom_values_objects_on_update
155 155 field = IssueCustomField.find_by_name('Database')
156 156
157 157 issue = Issue.find(1)
158 158 issue.custom_field_values = { field.id => 'PostgreSQL' }
159 159 assert issue.save
160 160 custom_value = issue.custom_value_for(field)
161 161 issue.reload
162 162 issue.custom_field_values = { field.id => 'MySQL' }
163 163 assert issue.save
164 164 issue.reload
165 165 assert_equal custom_value.id, issue.custom_value_for(field).id
166 166 end
167 167
168 168 def test_assigning_tracker_id_should_reload_custom_fields_values
169 169 issue = Issue.new(:project => Project.find(1))
170 170 assert issue.custom_field_values.empty?
171 171 issue.tracker_id = 1
172 172 assert issue.custom_field_values.any?
173 173 end
174 174
175 175 def test_assigning_attributes_should_assign_tracker_id_first
176 176 attributes = ActiveSupport::OrderedHash.new
177 177 attributes['custom_field_values'] = { '1' => 'MySQL' }
178 178 attributes['tracker_id'] = '1'
179 179 issue = Issue.new(:project => Project.find(1))
180 180 issue.attributes = attributes
181 181 assert_not_nil issue.custom_value_for(1)
182 182 assert_equal 'MySQL', issue.custom_value_for(1).value
183 183 end
184 184
185 185 def test_should_update_issue_with_disabled_tracker
186 186 p = Project.find(1)
187 187 issue = Issue.find(1)
188 188
189 189 p.trackers.delete(issue.tracker)
190 190 assert !p.trackers.include?(issue.tracker)
191 191
192 192 issue.reload
193 193 issue.subject = 'New subject'
194 194 assert issue.save
195 195 end
196 196
197 197 def test_should_not_set_a_disabled_tracker
198 198 p = Project.find(1)
199 199 p.trackers.delete(Tracker.find(2))
200 200
201 201 issue = Issue.find(1)
202 202 issue.tracker_id = 2
203 203 issue.subject = 'New subject'
204 204 assert !issue.save
205 205 assert_not_nil issue.errors.on(:tracker_id)
206 206 end
207 207
208 208 def test_category_based_assignment
209 209 issue = Issue.create(:project_id => 1, :tracker_id => 1, :author_id => 3, :status_id => 1, :priority => IssuePriority.all.first, :subject => 'Assignment test', :description => 'Assignment test', :category_id => 1)
210 210 assert_equal IssueCategory.find(1).assigned_to, issue.assigned_to
211 211 end
212 212
213 213 def test_copy
214 214 issue = Issue.new.copy_from(1)
215 215 assert issue.save
216 216 issue.reload
217 217 orig = Issue.find(1)
218 218 assert_equal orig.subject, issue.subject
219 219 assert_equal orig.tracker, issue.tracker
220 220 assert_equal "125", issue.custom_value_for(2).value
221 221 end
222 222
223 223 def test_copy_should_copy_status
224 224 orig = Issue.find(8)
225 225 assert orig.status != IssueStatus.default
226 226
227 227 issue = Issue.new.copy_from(orig)
228 228 assert issue.save
229 229 issue.reload
230 230 assert_equal orig.status, issue.status
231 231 end
232 232
233 233 def test_should_close_duplicates
234 234 # Create 3 issues
235 235 issue1 = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :priority => IssuePriority.all.first, :subject => 'Duplicates test', :description => 'Duplicates test')
236 236 assert issue1.save
237 237 issue2 = issue1.clone
238 238 assert issue2.save
239 239 issue3 = issue1.clone
240 240 assert issue3.save
241 241
242 242 # 2 is a dupe of 1
243 243 IssueRelation.create(:issue_from => issue2, :issue_to => issue1, :relation_type => IssueRelation::TYPE_DUPLICATES)
244 244 # And 3 is a dupe of 2
245 245 IssueRelation.create(:issue_from => issue3, :issue_to => issue2, :relation_type => IssueRelation::TYPE_DUPLICATES)
246 246 # And 3 is a dupe of 1 (circular duplicates)
247 247 IssueRelation.create(:issue_from => issue3, :issue_to => issue1, :relation_type => IssueRelation::TYPE_DUPLICATES)
248 248
249 249 assert issue1.reload.duplicates.include?(issue2)
250 250
251 251 # Closing issue 1
252 252 issue1.init_journal(User.find(:first), "Closing issue1")
253 253 issue1.status = IssueStatus.find :first, :conditions => {:is_closed => true}
254 254 assert issue1.save
255 255 # 2 and 3 should be also closed
256 256 assert issue2.reload.closed?
257 257 assert issue3.reload.closed?
258 258 end
259 259
260 260 def test_should_not_close_duplicated_issue
261 261 # Create 3 issues
262 262 issue1 = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :priority => IssuePriority.all.first, :subject => 'Duplicates test', :description => 'Duplicates test')
263 263 assert issue1.save
264 264 issue2 = issue1.clone
265 265 assert issue2.save
266 266
267 267 # 2 is a dupe of 1
268 268 IssueRelation.create(:issue_from => issue2, :issue_to => issue1, :relation_type => IssueRelation::TYPE_DUPLICATES)
269 269 # 2 is a dup of 1 but 1 is not a duplicate of 2
270 270 assert !issue2.reload.duplicates.include?(issue1)
271 271
272 272 # Closing issue 2
273 273 issue2.init_journal(User.find(:first), "Closing issue2")
274 274 issue2.status = IssueStatus.find :first, :conditions => {:is_closed => true}
275 275 assert issue2.save
276 276 # 1 should not be also closed
277 277 assert !issue1.reload.closed?
278 278 end
279 279
280 280 def test_assignable_versions
281 281 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :fixed_version_id => 1, :subject => 'New issue')
282 282 assert_equal ['open'], issue.assignable_versions.collect(&:status).uniq
283 283 end
284 284
285 285 def test_should_not_be_able_to_assign_a_new_issue_to_a_closed_version
286 286 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :fixed_version_id => 1, :subject => 'New issue')
287 287 assert !issue.save
288 288 assert_not_nil issue.errors.on(:fixed_version_id)
289 289 end
290 290
291 291 def test_should_not_be_able_to_assign_a_new_issue_to_a_locked_version
292 292 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :fixed_version_id => 2, :subject => 'New issue')
293 293 assert !issue.save
294 294 assert_not_nil issue.errors.on(:fixed_version_id)
295 295 end
296 296
297 297 def test_should_be_able_to_assign_a_new_issue_to_an_open_version
298 298 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :fixed_version_id => 3, :subject => 'New issue')
299 299 assert issue.save
300 300 end
301 301
302 302 def test_should_be_able_to_update_an_issue_assigned_to_a_closed_version
303 303 issue = Issue.find(11)
304 304 assert_equal 'closed', issue.fixed_version.status
305 305 issue.subject = 'Subject changed'
306 306 assert issue.save
307 307 end
308 308
309 309 def test_should_not_be_able_to_reopen_an_issue_assigned_to_a_closed_version
310 310 issue = Issue.find(11)
311 311 issue.status_id = 1
312 312 assert !issue.save
313 313 assert_not_nil issue.errors.on_base
314 314 end
315 315
316 316 def test_should_be_able_to_reopen_and_reassign_an_issue_assigned_to_a_closed_version
317 317 issue = Issue.find(11)
318 318 issue.status_id = 1
319 319 issue.fixed_version_id = 3
320 320 assert issue.save
321 321 end
322 322
323 323 def test_should_be_able_to_reopen_an_issue_assigned_to_a_locked_version
324 324 issue = Issue.find(12)
325 325 assert_equal 'locked', issue.fixed_version.status
326 326 issue.status_id = 1
327 327 assert issue.save
328 328 end
329 329
330 330 def test_move_to_another_project_with_same_category
331 331 issue = Issue.find(1)
332 332 assert issue.move_to(Project.find(2))
333 333 issue.reload
334 334 assert_equal 2, issue.project_id
335 335 # Category changes
336 336 assert_equal 4, issue.category_id
337 337 # Make sure time entries were move to the target project
338 338 assert_equal 2, issue.time_entries.first.project_id
339 339 end
340 340
341 341 def test_move_to_another_project_without_same_category
342 342 issue = Issue.find(2)
343 343 assert issue.move_to(Project.find(2))
344 344 issue.reload
345 345 assert_equal 2, issue.project_id
346 346 # Category cleared
347 347 assert_nil issue.category_id
348 348 end
349 349
350 350 def test_move_to_another_project_should_clear_fixed_version_when_not_shared
351 351 issue = Issue.find(1)
352 352 issue.update_attribute(:fixed_version_id, 1)
353 353 assert issue.move_to(Project.find(2))
354 354 issue.reload
355 355 assert_equal 2, issue.project_id
356 356 # Cleared fixed_version
357 357 assert_equal nil, issue.fixed_version
358 358 end
359 359
360 360 def test_move_to_another_project_should_keep_fixed_version_when_shared_with_the_target_project
361 361 issue = Issue.find(1)
362 362 issue.update_attribute(:fixed_version_id, 4)
363 363 assert issue.move_to(Project.find(5))
364 364 issue.reload
365 365 assert_equal 5, issue.project_id
366 366 # Keep fixed_version
367 367 assert_equal 4, issue.fixed_version_id
368 368 end
369 369
370 370 def test_move_to_another_project_should_clear_fixed_version_when_not_shared_with_the_target_project
371 371 issue = Issue.find(1)
372 372 issue.update_attribute(:fixed_version_id, 1)
373 373 assert issue.move_to(Project.find(5))
374 374 issue.reload
375 375 assert_equal 5, issue.project_id
376 376 # Cleared fixed_version
377 377 assert_equal nil, issue.fixed_version
378 378 end
379 379
380 380 def test_move_to_another_project_should_keep_fixed_version_when_shared_systemwide
381 381 issue = Issue.find(1)
382 382 issue.update_attribute(:fixed_version_id, 7)
383 383 assert issue.move_to(Project.find(2))
384 384 issue.reload
385 385 assert_equal 2, issue.project_id
386 386 # Keep fixed_version
387 387 assert_equal 7, issue.fixed_version_id
388 388 end
389 389
390 def test_move_to_another_project_with_disabled_tracker
391 issue = Issue.find(1)
392 target = Project.find(2)
393 target.tracker_ids = [3]
394 target.save
395 assert_equal false, issue.move_to(target)
396 issue.reload
397 assert_equal 1, issue.project_id
398 end
399
390 400 def test_copy_to_the_same_project
391 401 issue = Issue.find(1)
392 402 copy = nil
393 403 assert_difference 'Issue.count' do
394 404 copy = issue.move_to(issue.project, nil, :copy => true)
395 405 end
396 406 assert_kind_of Issue, copy
397 407 assert_equal issue.project, copy.project
398 408 assert_equal "125", copy.custom_value_for(2).value
399 409 end
400 410
401 411 def test_copy_to_another_project_and_tracker
402 412 issue = Issue.find(1)
403 413 copy = nil
404 414 assert_difference 'Issue.count' do
405 415 copy = issue.move_to(Project.find(3), Tracker.find(2), :copy => true)
406 416 end
407 417 assert_kind_of Issue, copy
408 418 assert_equal Project.find(3), copy.project
409 419 assert_equal Tracker.find(2), copy.tracker
410 420 # Custom field #2 is not associated with target tracker
411 421 assert_nil copy.custom_value_for(2)
412 422 end
413 423
414 424 context "#move_to" do
415 425 context "as a copy" do
416 426 setup do
417 427 @issue = Issue.find(1)
418 428 @copy = nil
419 429 end
420 430
421 431 should "allow assigned_to changes" do
422 432 @copy = @issue.move_to(Project.find(3), Tracker.find(2), {:copy => true, :attributes => {:assigned_to_id => 3}})
423 433 assert_equal 3, @copy.assigned_to_id
424 434 end
425 435
426 436 should "allow status changes" do
427 437 @copy = @issue.move_to(Project.find(3), Tracker.find(2), {:copy => true, :attributes => {:status_id => 2}})
428 438 assert_equal 2, @copy.status_id
429 439 end
430 440
431 441 should "allow start date changes" do
432 442 date = Date.today
433 443 @copy = @issue.move_to(Project.find(3), Tracker.find(2), {:copy => true, :attributes => {:start_date => date}})
434 444 assert_equal date, @copy.start_date
435 445 end
436 446
437 447 should "allow due date changes" do
438 448 date = Date.today
439 449 @copy = @issue.move_to(Project.find(3), Tracker.find(2), {:copy => true, :attributes => {:due_date => date}})
440 450
441 451 assert_equal date, @copy.due_date
442 452 end
443 453 end
444 454 end
445 455
446 456 def test_recipients_should_not_include_users_that_cannot_view_the_issue
447 457 issue = Issue.find(12)
448 458 assert issue.recipients.include?(issue.author.mail)
449 459 # move the issue to a private project
450 460 copy = issue.move_to(Project.find(5), Tracker.find(2), :copy => true)
451 461 # author is not a member of project anymore
452 462 assert !copy.recipients.include?(copy.author.mail)
453 463 end
454 464
455 465 def test_watcher_recipients_should_not_include_users_that_cannot_view_the_issue
456 466 user = User.find(3)
457 467 issue = Issue.find(9)
458 468 Watcher.create!(:user => user, :watchable => issue)
459 469 assert issue.watched_by?(user)
460 470 assert !issue.watcher_recipients.include?(user.mail)
461 471 end
462 472
463 473 def test_issue_destroy
464 474 Issue.find(1).destroy
465 475 assert_nil Issue.find_by_id(1)
466 476 assert_nil TimeEntry.find_by_issue_id(1)
467 477 end
468 478
469 479 def test_blocked
470 480 blocked_issue = Issue.find(9)
471 481 blocking_issue = Issue.find(10)
472 482
473 483 assert blocked_issue.blocked?
474 484 assert !blocking_issue.blocked?
475 485 end
476 486
477 487 def test_blocked_issues_dont_allow_closed_statuses
478 488 blocked_issue = Issue.find(9)
479 489
480 490 allowed_statuses = blocked_issue.new_statuses_allowed_to(users(:users_002))
481 491 assert !allowed_statuses.empty?
482 492 closed_statuses = allowed_statuses.select {|st| st.is_closed?}
483 493 assert closed_statuses.empty?
484 494 end
485 495
486 496 def test_unblocked_issues_allow_closed_statuses
487 497 blocking_issue = Issue.find(10)
488 498
489 499 allowed_statuses = blocking_issue.new_statuses_allowed_to(users(:users_002))
490 500 assert !allowed_statuses.empty?
491 501 closed_statuses = allowed_statuses.select {|st| st.is_closed?}
492 502 assert !closed_statuses.empty?
493 503 end
494 504
495 505 def test_overdue
496 506 assert Issue.new(:due_date => 1.day.ago.to_date).overdue?
497 507 assert !Issue.new(:due_date => Date.today).overdue?
498 508 assert !Issue.new(:due_date => 1.day.from_now.to_date).overdue?
499 509 assert !Issue.new(:due_date => nil).overdue?
500 510 assert !Issue.new(:due_date => 1.day.ago.to_date, :status => IssueStatus.find(:first, :conditions => {:is_closed => true})).overdue?
501 511 end
502 512
503 513 def test_assignable_users
504 514 assert_kind_of User, Issue.find(1).assignable_users.first
505 515 end
506 516
507 517 def test_create_should_send_email_notification
508 518 ActionMailer::Base.deliveries.clear
509 519 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3, :status_id => 1, :priority => IssuePriority.all.first, :subject => 'test_create', :estimated_hours => '1:30')
510 520
511 521 assert issue.save
512 522 assert_equal 1, ActionMailer::Base.deliveries.size
513 523 end
514 524
515 525 def test_stale_issue_should_not_send_email_notification
516 526 ActionMailer::Base.deliveries.clear
517 527 issue = Issue.find(1)
518 528 stale = Issue.find(1)
519 529
520 530 issue.init_journal(User.find(1))
521 531 issue.subject = 'Subjet update'
522 532 assert issue.save
523 533 assert_equal 1, ActionMailer::Base.deliveries.size
524 534 ActionMailer::Base.deliveries.clear
525 535
526 536 stale.init_journal(User.find(1))
527 537 stale.subject = 'Another subjet update'
528 538 assert_raise ActiveRecord::StaleObjectError do
529 539 stale.save
530 540 end
531 541 assert ActionMailer::Base.deliveries.empty?
532 542 end
533 543
534 544 def test_saving_twice_should_not_duplicate_journal_details
535 545 i = Issue.find(:first)
536 546 i.init_journal(User.find(2), 'Some notes')
537 547 # initial changes
538 548 i.subject = 'New subject'
539 549 i.done_ratio = i.done_ratio + 10
540 550 assert_difference 'Journal.count' do
541 551 assert i.save
542 552 end
543 553 # 1 more change
544 554 i.priority = IssuePriority.find(:first, :conditions => ["id <> ?", i.priority_id])
545 555 assert_no_difference 'Journal.count' do
546 556 assert_difference 'JournalDetail.count', 1 do
547 557 i.save
548 558 end
549 559 end
550 560 # no more change
551 561 assert_no_difference 'Journal.count' do
552 562 assert_no_difference 'JournalDetail.count' do
553 563 i.save
554 564 end
555 565 end
556 566 end
557 567
558 568 context "#done_ratio" do
559 569 setup do
560 570 @issue = Issue.find(1)
561 571 @issue_status = IssueStatus.find(1)
562 572 @issue_status.update_attribute(:default_done_ratio, 50)
563 573 end
564 574
565 575 context "with Setting.issue_done_ratio using the issue_field" do
566 576 setup do
567 577 Setting.issue_done_ratio = 'issue_field'
568 578 end
569 579
570 580 should "read the issue's field" do
571 581 assert_equal 0, @issue.done_ratio
572 582 end
573 583 end
574 584
575 585 context "with Setting.issue_done_ratio using the issue_status" do
576 586 setup do
577 587 Setting.issue_done_ratio = 'issue_status'
578 588 end
579 589
580 590 should "read the Issue Status's default done ratio" do
581 591 assert_equal 50, @issue.done_ratio
582 592 end
583 593 end
584 594 end
585 595
586 596 context "#update_done_ratio_from_issue_status" do
587 597 setup do
588 598 @issue = Issue.find(1)
589 599 @issue_status = IssueStatus.find(1)
590 600 @issue_status.update_attribute(:default_done_ratio, 50)
591 601 end
592 602
593 603 context "with Setting.issue_done_ratio using the issue_field" do
594 604 setup do
595 605 Setting.issue_done_ratio = 'issue_field'
596 606 end
597 607
598 608 should "not change the issue" do
599 609 @issue.update_done_ratio_from_issue_status
600 610
601 611 assert_equal 0, @issue.done_ratio
602 612 end
603 613 end
604 614
605 615 context "with Setting.issue_done_ratio using the issue_status" do
606 616 setup do
607 617 Setting.issue_done_ratio = 'issue_status'
608 618 end
609 619
610 620 should "not change the issue's done ratio" do
611 621 @issue.update_done_ratio_from_issue_status
612 622
613 623 assert_equal 50, @issue.done_ratio
614 624 end
615 625 end
616 626 end
617 627
618 628 test "#by_tracker" do
619 629 groups = Issue.by_tracker(Project.find(1))
620 630 assert_equal 3, groups.size
621 631 assert_equal 7, groups.inject(0) {|sum, group| sum + group['total'].to_i}
622 632 end
623 633
624 634 test "#by_version" do
625 635 groups = Issue.by_version(Project.find(1))
626 636 assert_equal 3, groups.size
627 637 assert_equal 3, groups.inject(0) {|sum, group| sum + group['total'].to_i}
628 638 end
629 639
630 640 test "#by_priority" do
631 641 groups = Issue.by_priority(Project.find(1))
632 642 assert_equal 4, groups.size
633 643 assert_equal 7, groups.inject(0) {|sum, group| sum + group['total'].to_i}
634 644 end
635 645
636 646 test "#by_category" do
637 647 groups = Issue.by_category(Project.find(1))
638 648 assert_equal 2, groups.size
639 649 assert_equal 3, groups.inject(0) {|sum, group| sum + group['total'].to_i}
640 650 end
641 651
642 652 test "#by_assigned_to" do
643 653 groups = Issue.by_assigned_to(Project.find(1))
644 654 assert_equal 2, groups.size
645 655 assert_equal 2, groups.inject(0) {|sum, group| sum + group['total'].to_i}
646 656 end
647 657
648 658 test "#by_author" do
649 659 groups = Issue.by_author(Project.find(1))
650 660 assert_equal 4, groups.size
651 661 assert_equal 7, groups.inject(0) {|sum, group| sum + group['total'].to_i}
652 662 end
653 663
654 664 test "#by_subproject" do
655 665 groups = Issue.by_subproject(Project.find(1))
656 666 assert_equal 2, groups.size
657 667 assert_equal 5, groups.inject(0) {|sum, group| sum + group['total'].to_i}
658 668 end
659 669
660 670 def test_recently_updated_with_limit_scopes
661 671 #should return the last updated issue
662 672 assert_equal 1, Issue.recently_updated.with_limit(1).length
663 673 assert_equal Issue.find(:first, :order => "updated_on DESC"), Issue.recently_updated.with_limit(1).first
664 674 end
665 675
666 676 def test_on_active_projects_scope
667 677 assert Project.find(2).archive
668 678
669 679 before = Issue.on_active_project.length
670 680 # test inclusion to results
671 681 issue = Issue.generate_for_project!(Project.find(1), :tracker => Project.find(2).trackers.first)
672 682 assert_equal before + 1, Issue.on_active_project.length
673 683
674 684 # Move to an archived project
675 685 issue.project = Project.find(2)
676 686 assert issue.save
677 687 assert_equal before, Issue.on_active_project.length
678 688 end
679 689 end
General Comments 0
You need to be logged in to leave comments. Login now