##// END OF EJS Templates
Closed issue are not overdue, fixes r2140 (#2337)....
Jean-Philippe Lang -
r2357:b11a1d852c37
parent child
Show More
@@ -1,292 +1,292
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 class Issue < ActiveRecord::Base
18 class Issue < ActiveRecord::Base
19 belongs_to :project
19 belongs_to :project
20 belongs_to :tracker
20 belongs_to :tracker
21 belongs_to :status, :class_name => 'IssueStatus', :foreign_key => 'status_id'
21 belongs_to :status, :class_name => 'IssueStatus', :foreign_key => 'status_id'
22 belongs_to :author, :class_name => 'User', :foreign_key => 'author_id'
22 belongs_to :author, :class_name => 'User', :foreign_key => 'author_id'
23 belongs_to :assigned_to, :class_name => 'User', :foreign_key => 'assigned_to_id'
23 belongs_to :assigned_to, :class_name => 'User', :foreign_key => 'assigned_to_id'
24 belongs_to :fixed_version, :class_name => 'Version', :foreign_key => 'fixed_version_id'
24 belongs_to :fixed_version, :class_name => 'Version', :foreign_key => 'fixed_version_id'
25 belongs_to :priority, :class_name => 'Enumeration', :foreign_key => 'priority_id'
25 belongs_to :priority, :class_name => 'Enumeration', :foreign_key => 'priority_id'
26 belongs_to :category, :class_name => 'IssueCategory', :foreign_key => 'category_id'
26 belongs_to :category, :class_name => 'IssueCategory', :foreign_key => 'category_id'
27
27
28 has_many :journals, :as => :journalized, :dependent => :destroy
28 has_many :journals, :as => :journalized, :dependent => :destroy
29 has_many :time_entries, :dependent => :delete_all
29 has_many :time_entries, :dependent => :delete_all
30 has_and_belongs_to_many :changesets, :order => "#{Changeset.table_name}.committed_on ASC, #{Changeset.table_name}.id ASC"
30 has_and_belongs_to_many :changesets, :order => "#{Changeset.table_name}.committed_on ASC, #{Changeset.table_name}.id ASC"
31
31
32 has_many :relations_from, :class_name => 'IssueRelation', :foreign_key => 'issue_from_id', :dependent => :delete_all
32 has_many :relations_from, :class_name => 'IssueRelation', :foreign_key => 'issue_from_id', :dependent => :delete_all
33 has_many :relations_to, :class_name => 'IssueRelation', :foreign_key => 'issue_to_id', :dependent => :delete_all
33 has_many :relations_to, :class_name => 'IssueRelation', :foreign_key => 'issue_to_id', :dependent => :delete_all
34
34
35 acts_as_attachable :after_remove => :attachment_removed
35 acts_as_attachable :after_remove => :attachment_removed
36 acts_as_customizable
36 acts_as_customizable
37 acts_as_watchable
37 acts_as_watchable
38 acts_as_searchable :columns => ['subject', "#{table_name}.description", "#{Journal.table_name}.notes"],
38 acts_as_searchable :columns => ['subject', "#{table_name}.description", "#{Journal.table_name}.notes"],
39 :include => [:project, :journals],
39 :include => [:project, :journals],
40 # sort by id so that limited eager loading doesn't break with postgresql
40 # sort by id so that limited eager loading doesn't break with postgresql
41 :order_column => "#{table_name}.id"
41 :order_column => "#{table_name}.id"
42 acts_as_event :title => Proc.new {|o| "#{o.tracker.name} ##{o.id}: #{o.subject}"},
42 acts_as_event :title => Proc.new {|o| "#{o.tracker.name} ##{o.id}: #{o.subject}"},
43 :url => Proc.new {|o| {:controller => 'issues', :action => 'show', :id => o.id}},
43 :url => Proc.new {|o| {:controller => 'issues', :action => 'show', :id => o.id}},
44 :type => Proc.new {|o| 'issue' + (o.closed? ? ' closed' : '') }
44 :type => Proc.new {|o| 'issue' + (o.closed? ? ' closed' : '') }
45
45
46 acts_as_activity_provider :find_options => {:include => [:project, :author, :tracker]},
46 acts_as_activity_provider :find_options => {:include => [:project, :author, :tracker]},
47 :author_key => :author_id
47 :author_key => :author_id
48
48
49 validates_presence_of :subject, :priority, :project, :tracker, :author, :status
49 validates_presence_of :subject, :priority, :project, :tracker, :author, :status
50 validates_length_of :subject, :maximum => 255
50 validates_length_of :subject, :maximum => 255
51 validates_inclusion_of :done_ratio, :in => 0..100
51 validates_inclusion_of :done_ratio, :in => 0..100
52 validates_numericality_of :estimated_hours, :allow_nil => true
52 validates_numericality_of :estimated_hours, :allow_nil => true
53
53
54 named_scope :visible, lambda {|*args| { :include => :project,
54 named_scope :visible, lambda {|*args| { :include => :project,
55 :conditions => Project.allowed_to_condition(args.first || User.current, :view_issues) } }
55 :conditions => Project.allowed_to_condition(args.first || User.current, :view_issues) } }
56
56
57 # Returns true if usr or current user is allowed to view the issue
57 # Returns true if usr or current user is allowed to view the issue
58 def visible?(usr=nil)
58 def visible?(usr=nil)
59 (usr || User.current).allowed_to?(:view_issues, self.project)
59 (usr || User.current).allowed_to?(:view_issues, self.project)
60 end
60 end
61
61
62 def after_initialize
62 def after_initialize
63 if new_record?
63 if new_record?
64 # set default values for new records only
64 # set default values for new records only
65 self.status ||= IssueStatus.default
65 self.status ||= IssueStatus.default
66 self.priority ||= Enumeration.default('IPRI')
66 self.priority ||= Enumeration.default('IPRI')
67 end
67 end
68 end
68 end
69
69
70 # Overrides Redmine::Acts::Customizable::InstanceMethods#available_custom_fields
70 # Overrides Redmine::Acts::Customizable::InstanceMethods#available_custom_fields
71 def available_custom_fields
71 def available_custom_fields
72 (project && tracker) ? project.all_issue_custom_fields.select {|c| tracker.custom_fields.include? c } : []
72 (project && tracker) ? project.all_issue_custom_fields.select {|c| tracker.custom_fields.include? c } : []
73 end
73 end
74
74
75 def copy_from(arg)
75 def copy_from(arg)
76 issue = arg.is_a?(Issue) ? arg : Issue.find(arg)
76 issue = arg.is_a?(Issue) ? arg : Issue.find(arg)
77 self.attributes = issue.attributes.dup
77 self.attributes = issue.attributes.dup
78 self.custom_values = issue.custom_values.collect {|v| v.clone}
78 self.custom_values = issue.custom_values.collect {|v| v.clone}
79 self
79 self
80 end
80 end
81
81
82 # Moves/copies an issue to a new project and tracker
82 # Moves/copies an issue to a new project and tracker
83 # Returns the moved/copied issue on success, false on failure
83 # Returns the moved/copied issue on success, false on failure
84 def move_to(new_project, new_tracker = nil, options = {})
84 def move_to(new_project, new_tracker = nil, options = {})
85 options ||= {}
85 options ||= {}
86 issue = options[:copy] ? self.clone : self
86 issue = options[:copy] ? self.clone : self
87 transaction do
87 transaction do
88 if new_project && issue.project_id != new_project.id
88 if new_project && issue.project_id != new_project.id
89 # delete issue relations
89 # delete issue relations
90 unless Setting.cross_project_issue_relations?
90 unless Setting.cross_project_issue_relations?
91 issue.relations_from.clear
91 issue.relations_from.clear
92 issue.relations_to.clear
92 issue.relations_to.clear
93 end
93 end
94 # issue is moved to another project
94 # issue is moved to another project
95 # reassign to the category with same name if any
95 # reassign to the category with same name if any
96 new_category = issue.category.nil? ? nil : new_project.issue_categories.find_by_name(issue.category.name)
96 new_category = issue.category.nil? ? nil : new_project.issue_categories.find_by_name(issue.category.name)
97 issue.category = new_category
97 issue.category = new_category
98 issue.fixed_version = nil
98 issue.fixed_version = nil
99 issue.project = new_project
99 issue.project = new_project
100 end
100 end
101 if new_tracker
101 if new_tracker
102 issue.tracker = new_tracker
102 issue.tracker = new_tracker
103 end
103 end
104 if options[:copy]
104 if options[:copy]
105 issue.custom_field_values = self.custom_field_values.inject({}) {|h,v| h[v.custom_field_id] = v.value; h}
105 issue.custom_field_values = self.custom_field_values.inject({}) {|h,v| h[v.custom_field_id] = v.value; h}
106 issue.status = self.status
106 issue.status = self.status
107 end
107 end
108 if issue.save
108 if issue.save
109 unless options[:copy]
109 unless options[:copy]
110 # Manually update project_id on related time entries
110 # Manually update project_id on related time entries
111 TimeEntry.update_all("project_id = #{new_project.id}", {:issue_id => id})
111 TimeEntry.update_all("project_id = #{new_project.id}", {:issue_id => id})
112 end
112 end
113 else
113 else
114 Issue.connection.rollback_db_transaction
114 Issue.connection.rollback_db_transaction
115 return false
115 return false
116 end
116 end
117 end
117 end
118 return issue
118 return issue
119 end
119 end
120
120
121 def priority_id=(pid)
121 def priority_id=(pid)
122 self.priority = nil
122 self.priority = nil
123 write_attribute(:priority_id, pid)
123 write_attribute(:priority_id, pid)
124 end
124 end
125
125
126 def estimated_hours=(h)
126 def estimated_hours=(h)
127 write_attribute :estimated_hours, (h.is_a?(String) ? h.to_hours : h)
127 write_attribute :estimated_hours, (h.is_a?(String) ? h.to_hours : h)
128 end
128 end
129
129
130 def validate
130 def validate
131 if self.due_date.nil? && @attributes['due_date'] && !@attributes['due_date'].empty?
131 if self.due_date.nil? && @attributes['due_date'] && !@attributes['due_date'].empty?
132 errors.add :due_date, :activerecord_error_not_a_date
132 errors.add :due_date, :activerecord_error_not_a_date
133 end
133 end
134
134
135 if self.due_date and self.start_date and self.due_date < self.start_date
135 if self.due_date and self.start_date and self.due_date < self.start_date
136 errors.add :due_date, :activerecord_error_greater_than_start_date
136 errors.add :due_date, :activerecord_error_greater_than_start_date
137 end
137 end
138
138
139 if start_date && soonest_start && start_date < soonest_start
139 if start_date && soonest_start && start_date < soonest_start
140 errors.add :start_date, :activerecord_error_invalid
140 errors.add :start_date, :activerecord_error_invalid
141 end
141 end
142 end
142 end
143
143
144 def validate_on_create
144 def validate_on_create
145 errors.add :tracker_id, :activerecord_error_invalid unless project.trackers.include?(tracker)
145 errors.add :tracker_id, :activerecord_error_invalid unless project.trackers.include?(tracker)
146 end
146 end
147
147
148 def before_create
148 def before_create
149 # default assignment based on category
149 # default assignment based on category
150 if assigned_to.nil? && category && category.assigned_to
150 if assigned_to.nil? && category && category.assigned_to
151 self.assigned_to = category.assigned_to
151 self.assigned_to = category.assigned_to
152 end
152 end
153 end
153 end
154
154
155 def before_save
155 def before_save
156 if @current_journal
156 if @current_journal
157 # attributes changes
157 # attributes changes
158 (Issue.column_names - %w(id description)).each {|c|
158 (Issue.column_names - %w(id description)).each {|c|
159 @current_journal.details << JournalDetail.new(:property => 'attr',
159 @current_journal.details << JournalDetail.new(:property => 'attr',
160 :prop_key => c,
160 :prop_key => c,
161 :old_value => @issue_before_change.send(c),
161 :old_value => @issue_before_change.send(c),
162 :value => send(c)) unless send(c)==@issue_before_change.send(c)
162 :value => send(c)) unless send(c)==@issue_before_change.send(c)
163 }
163 }
164 # custom fields changes
164 # custom fields changes
165 custom_values.each {|c|
165 custom_values.each {|c|
166 next if (@custom_values_before_change[c.custom_field_id]==c.value ||
166 next if (@custom_values_before_change[c.custom_field_id]==c.value ||
167 (@custom_values_before_change[c.custom_field_id].blank? && c.value.blank?))
167 (@custom_values_before_change[c.custom_field_id].blank? && c.value.blank?))
168 @current_journal.details << JournalDetail.new(:property => 'cf',
168 @current_journal.details << JournalDetail.new(:property => 'cf',
169 :prop_key => c.custom_field_id,
169 :prop_key => c.custom_field_id,
170 :old_value => @custom_values_before_change[c.custom_field_id],
170 :old_value => @custom_values_before_change[c.custom_field_id],
171 :value => c.value)
171 :value => c.value)
172 }
172 }
173 @current_journal.save
173 @current_journal.save
174 end
174 end
175 # Save the issue even if the journal is not saved (because empty)
175 # Save the issue even if the journal is not saved (because empty)
176 true
176 true
177 end
177 end
178
178
179 def after_save
179 def after_save
180 # Reload is needed in order to get the right status
180 # Reload is needed in order to get the right status
181 reload
181 reload
182
182
183 # Update start/due dates of following issues
183 # Update start/due dates of following issues
184 relations_from.each(&:set_issue_to_dates)
184 relations_from.each(&:set_issue_to_dates)
185
185
186 # Close duplicates if the issue was closed
186 # Close duplicates if the issue was closed
187 if @issue_before_change && !@issue_before_change.closed? && self.closed?
187 if @issue_before_change && !@issue_before_change.closed? && self.closed?
188 duplicates.each do |duplicate|
188 duplicates.each do |duplicate|
189 # Reload is need in case the duplicate was updated by a previous duplicate
189 # Reload is need in case the duplicate was updated by a previous duplicate
190 duplicate.reload
190 duplicate.reload
191 # Don't re-close it if it's already closed
191 # Don't re-close it if it's already closed
192 next if duplicate.closed?
192 next if duplicate.closed?
193 # Same user and notes
193 # Same user and notes
194 duplicate.init_journal(@current_journal.user, @current_journal.notes)
194 duplicate.init_journal(@current_journal.user, @current_journal.notes)
195 duplicate.update_attribute :status, self.status
195 duplicate.update_attribute :status, self.status
196 end
196 end
197 end
197 end
198 end
198 end
199
199
200 def init_journal(user, notes = "")
200 def init_journal(user, notes = "")
201 @current_journal ||= Journal.new(:journalized => self, :user => user, :notes => notes)
201 @current_journal ||= Journal.new(:journalized => self, :user => user, :notes => notes)
202 @issue_before_change = self.clone
202 @issue_before_change = self.clone
203 @issue_before_change.status = self.status
203 @issue_before_change.status = self.status
204 @custom_values_before_change = {}
204 @custom_values_before_change = {}
205 self.custom_values.each {|c| @custom_values_before_change.store c.custom_field_id, c.value }
205 self.custom_values.each {|c| @custom_values_before_change.store c.custom_field_id, c.value }
206 # Make sure updated_on is updated when adding a note.
206 # Make sure updated_on is updated when adding a note.
207 updated_on_will_change!
207 updated_on_will_change!
208 @current_journal
208 @current_journal
209 end
209 end
210
210
211 # Return true if the issue is closed, otherwise false
211 # Return true if the issue is closed, otherwise false
212 def closed?
212 def closed?
213 self.status.is_closed?
213 self.status.is_closed?
214 end
214 end
215
215
216 # Returns true if the issue is overdue
216 # Returns true if the issue is overdue
217 def overdue?
217 def overdue?
218 !due_date.nil? && (due_date < Date.today)
218 !due_date.nil? && (due_date < Date.today) && !status.is_closed?
219 end
219 end
220
220
221 # Users the issue can be assigned to
221 # Users the issue can be assigned to
222 def assignable_users
222 def assignable_users
223 project.assignable_users
223 project.assignable_users
224 end
224 end
225
225
226 # Returns an array of status that user is able to apply
226 # Returns an array of status that user is able to apply
227 def new_statuses_allowed_to(user)
227 def new_statuses_allowed_to(user)
228 statuses = status.find_new_statuses_allowed_to(user.role_for_project(project), tracker)
228 statuses = status.find_new_statuses_allowed_to(user.role_for_project(project), tracker)
229 statuses << status unless statuses.empty?
229 statuses << status unless statuses.empty?
230 statuses.uniq.sort
230 statuses.uniq.sort
231 end
231 end
232
232
233 # Returns the mail adresses of users that should be notified for the issue
233 # Returns the mail adresses of users that should be notified for the issue
234 def recipients
234 def recipients
235 recipients = project.recipients
235 recipients = project.recipients
236 # Author and assignee are always notified unless they have been locked
236 # Author and assignee are always notified unless they have been locked
237 recipients << author.mail if author && author.active?
237 recipients << author.mail if author && author.active?
238 recipients << assigned_to.mail if assigned_to && assigned_to.active?
238 recipients << assigned_to.mail if assigned_to && assigned_to.active?
239 recipients.compact.uniq
239 recipients.compact.uniq
240 end
240 end
241
241
242 def spent_hours
242 def spent_hours
243 @spent_hours ||= time_entries.sum(:hours) || 0
243 @spent_hours ||= time_entries.sum(:hours) || 0
244 end
244 end
245
245
246 def relations
246 def relations
247 (relations_from + relations_to).sort
247 (relations_from + relations_to).sort
248 end
248 end
249
249
250 def all_dependent_issues
250 def all_dependent_issues
251 dependencies = []
251 dependencies = []
252 relations_from.each do |relation|
252 relations_from.each do |relation|
253 dependencies << relation.issue_to
253 dependencies << relation.issue_to
254 dependencies += relation.issue_to.all_dependent_issues
254 dependencies += relation.issue_to.all_dependent_issues
255 end
255 end
256 dependencies
256 dependencies
257 end
257 end
258
258
259 # Returns an array of issues that duplicate this one
259 # Returns an array of issues that duplicate this one
260 def duplicates
260 def duplicates
261 relations_to.select {|r| r.relation_type == IssueRelation::TYPE_DUPLICATES}.collect {|r| r.issue_from}
261 relations_to.select {|r| r.relation_type == IssueRelation::TYPE_DUPLICATES}.collect {|r| r.issue_from}
262 end
262 end
263
263
264 # Returns the due date or the target due date if any
264 # Returns the due date or the target due date if any
265 # Used on gantt chart
265 # Used on gantt chart
266 def due_before
266 def due_before
267 due_date || (fixed_version ? fixed_version.effective_date : nil)
267 due_date || (fixed_version ? fixed_version.effective_date : nil)
268 end
268 end
269
269
270 def duration
270 def duration
271 (start_date && due_date) ? due_date - start_date : 0
271 (start_date && due_date) ? due_date - start_date : 0
272 end
272 end
273
273
274 def soonest_start
274 def soonest_start
275 @soonest_start ||= relations_to.collect{|relation| relation.successor_soonest_start}.compact.min
275 @soonest_start ||= relations_to.collect{|relation| relation.successor_soonest_start}.compact.min
276 end
276 end
277
277
278 def to_s
278 def to_s
279 "#{tracker} ##{id}: #{subject}"
279 "#{tracker} ##{id}: #{subject}"
280 end
280 end
281
281
282 private
282 private
283
283
284 # Callback on attachment deletion
284 # Callback on attachment deletion
285 def attachment_removed(obj)
285 def attachment_removed(obj)
286 journal = init_journal(User.current)
286 journal = init_journal(User.current)
287 journal.details << JournalDetail.new(:property => 'attachment',
287 journal.details << JournalDetail.new(:property => 'attachment',
288 :prop_key => obj.id,
288 :prop_key => obj.id,
289 :old_value => obj.filename)
289 :old_value => obj.filename)
290 journal.save
290 journal.save
291 end
291 end
292 end
292 end
@@ -1,230 +1,231
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.dirname(__FILE__) + '/../test_helper'
18 require File.dirname(__FILE__) + '/../test_helper'
19
19
20 class IssueTest < Test::Unit::TestCase
20 class IssueTest < Test::Unit::TestCase
21 fixtures :projects, :users, :members,
21 fixtures :projects, :users, :members,
22 :trackers, :projects_trackers,
22 :trackers, :projects_trackers,
23 :issue_statuses, :issue_categories,
23 :issue_statuses, :issue_categories,
24 :enumerations,
24 :enumerations,
25 :issues,
25 :issues,
26 :custom_fields, :custom_fields_projects, :custom_fields_trackers, :custom_values,
26 :custom_fields, :custom_fields_projects, :custom_fields_trackers, :custom_values,
27 :time_entries
27 :time_entries
28
28
29 def test_create
29 def test_create
30 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3, :status_id => 1, :priority => Enumeration.get_values('IPRI').first, :subject => 'test_create', :description => 'IssueTest#test_create', :estimated_hours => '1:30')
30 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3, :status_id => 1, :priority => Enumeration.get_values('IPRI').first, :subject => 'test_create', :description => 'IssueTest#test_create', :estimated_hours => '1:30')
31 assert issue.save
31 assert issue.save
32 issue.reload
32 issue.reload
33 assert_equal 1.5, issue.estimated_hours
33 assert_equal 1.5, issue.estimated_hours
34 end
34 end
35
35
36 def test_create_minimal
36 def test_create_minimal
37 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3, :status_id => 1, :priority => Enumeration.get_values('IPRI').first, :subject => 'test_create')
37 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3, :status_id => 1, :priority => Enumeration.get_values('IPRI').first, :subject => 'test_create')
38 assert issue.save
38 assert issue.save
39 assert issue.description.nil?
39 assert issue.description.nil?
40 end
40 end
41
41
42 def test_create_with_required_custom_field
42 def test_create_with_required_custom_field
43 field = IssueCustomField.find_by_name('Database')
43 field = IssueCustomField.find_by_name('Database')
44 field.update_attribute(:is_required, true)
44 field.update_attribute(:is_required, true)
45
45
46 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')
46 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')
47 assert issue.available_custom_fields.include?(field)
47 assert issue.available_custom_fields.include?(field)
48 # No value for the custom field
48 # No value for the custom field
49 assert !issue.save
49 assert !issue.save
50 assert_equal 'activerecord_error_invalid', issue.errors.on(:custom_values)
50 assert_equal 'activerecord_error_invalid', issue.errors.on(:custom_values)
51 # Blank value
51 # Blank value
52 issue.custom_field_values = { field.id => '' }
52 issue.custom_field_values = { field.id => '' }
53 assert !issue.save
53 assert !issue.save
54 assert_equal 'activerecord_error_invalid', issue.errors.on(:custom_values)
54 assert_equal 'activerecord_error_invalid', issue.errors.on(:custom_values)
55 # Invalid value
55 # Invalid value
56 issue.custom_field_values = { field.id => 'SQLServer' }
56 issue.custom_field_values = { field.id => 'SQLServer' }
57 assert !issue.save
57 assert !issue.save
58 assert_equal 'activerecord_error_invalid', issue.errors.on(:custom_values)
58 assert_equal 'activerecord_error_invalid', issue.errors.on(:custom_values)
59 # Valid value
59 # Valid value
60 issue.custom_field_values = { field.id => 'PostgreSQL' }
60 issue.custom_field_values = { field.id => 'PostgreSQL' }
61 assert issue.save
61 assert issue.save
62 issue.reload
62 issue.reload
63 assert_equal 'PostgreSQL', issue.custom_value_for(field).value
63 assert_equal 'PostgreSQL', issue.custom_value_for(field).value
64 end
64 end
65
65
66 def test_update_issue_with_required_custom_field
66 def test_update_issue_with_required_custom_field
67 field = IssueCustomField.find_by_name('Database')
67 field = IssueCustomField.find_by_name('Database')
68 field.update_attribute(:is_required, true)
68 field.update_attribute(:is_required, true)
69
69
70 issue = Issue.find(1)
70 issue = Issue.find(1)
71 assert_nil issue.custom_value_for(field)
71 assert_nil issue.custom_value_for(field)
72 assert issue.available_custom_fields.include?(field)
72 assert issue.available_custom_fields.include?(field)
73 # No change to custom values, issue can be saved
73 # No change to custom values, issue can be saved
74 assert issue.save
74 assert issue.save
75 # Blank value
75 # Blank value
76 issue.custom_field_values = { field.id => '' }
76 issue.custom_field_values = { field.id => '' }
77 assert !issue.save
77 assert !issue.save
78 # Valid value
78 # Valid value
79 issue.custom_field_values = { field.id => 'PostgreSQL' }
79 issue.custom_field_values = { field.id => 'PostgreSQL' }
80 assert issue.save
80 assert issue.save
81 issue.reload
81 issue.reload
82 assert_equal 'PostgreSQL', issue.custom_value_for(field).value
82 assert_equal 'PostgreSQL', issue.custom_value_for(field).value
83 end
83 end
84
84
85 def test_should_not_update_attributes_if_custom_fields_validation_fails
85 def test_should_not_update_attributes_if_custom_fields_validation_fails
86 issue = Issue.find(1)
86 issue = Issue.find(1)
87 field = IssueCustomField.find_by_name('Database')
87 field = IssueCustomField.find_by_name('Database')
88 assert issue.available_custom_fields.include?(field)
88 assert issue.available_custom_fields.include?(field)
89
89
90 issue.custom_field_values = { field.id => 'Invalid' }
90 issue.custom_field_values = { field.id => 'Invalid' }
91 issue.subject = 'Should be not be saved'
91 issue.subject = 'Should be not be saved'
92 assert !issue.save
92 assert !issue.save
93
93
94 issue.reload
94 issue.reload
95 assert_equal "Can't print recipes", issue.subject
95 assert_equal "Can't print recipes", issue.subject
96 end
96 end
97
97
98 def test_should_not_recreate_custom_values_objects_on_update
98 def test_should_not_recreate_custom_values_objects_on_update
99 field = IssueCustomField.find_by_name('Database')
99 field = IssueCustomField.find_by_name('Database')
100
100
101 issue = Issue.find(1)
101 issue = Issue.find(1)
102 issue.custom_field_values = { field.id => 'PostgreSQL' }
102 issue.custom_field_values = { field.id => 'PostgreSQL' }
103 assert issue.save
103 assert issue.save
104 custom_value = issue.custom_value_for(field)
104 custom_value = issue.custom_value_for(field)
105 issue.reload
105 issue.reload
106 issue.custom_field_values = { field.id => 'MySQL' }
106 issue.custom_field_values = { field.id => 'MySQL' }
107 assert issue.save
107 assert issue.save
108 issue.reload
108 issue.reload
109 assert_equal custom_value.id, issue.custom_value_for(field).id
109 assert_equal custom_value.id, issue.custom_value_for(field).id
110 end
110 end
111
111
112 def test_category_based_assignment
112 def test_category_based_assignment
113 issue = Issue.create(:project_id => 1, :tracker_id => 1, :author_id => 3, :status_id => 1, :priority => Enumeration.get_values('IPRI').first, :subject => 'Assignment test', :description => 'Assignment test', :category_id => 1)
113 issue = Issue.create(:project_id => 1, :tracker_id => 1, :author_id => 3, :status_id => 1, :priority => Enumeration.get_values('IPRI').first, :subject => 'Assignment test', :description => 'Assignment test', :category_id => 1)
114 assert_equal IssueCategory.find(1).assigned_to, issue.assigned_to
114 assert_equal IssueCategory.find(1).assigned_to, issue.assigned_to
115 end
115 end
116
116
117 def test_copy
117 def test_copy
118 issue = Issue.new.copy_from(1)
118 issue = Issue.new.copy_from(1)
119 assert issue.save
119 assert issue.save
120 issue.reload
120 issue.reload
121 orig = Issue.find(1)
121 orig = Issue.find(1)
122 assert_equal orig.subject, issue.subject
122 assert_equal orig.subject, issue.subject
123 assert_equal orig.tracker, issue.tracker
123 assert_equal orig.tracker, issue.tracker
124 assert_equal orig.custom_values.first.value, issue.custom_values.first.value
124 assert_equal orig.custom_values.first.value, issue.custom_values.first.value
125 end
125 end
126
126
127 def test_should_close_duplicates
127 def test_should_close_duplicates
128 # Create 3 issues
128 # Create 3 issues
129 issue1 = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :priority => Enumeration.get_values('IPRI').first, :subject => 'Duplicates test', :description => 'Duplicates test')
129 issue1 = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :priority => Enumeration.get_values('IPRI').first, :subject => 'Duplicates test', :description => 'Duplicates test')
130 assert issue1.save
130 assert issue1.save
131 issue2 = issue1.clone
131 issue2 = issue1.clone
132 assert issue2.save
132 assert issue2.save
133 issue3 = issue1.clone
133 issue3 = issue1.clone
134 assert issue3.save
134 assert issue3.save
135
135
136 # 2 is a dupe of 1
136 # 2 is a dupe of 1
137 IssueRelation.create(:issue_from => issue2, :issue_to => issue1, :relation_type => IssueRelation::TYPE_DUPLICATES)
137 IssueRelation.create(:issue_from => issue2, :issue_to => issue1, :relation_type => IssueRelation::TYPE_DUPLICATES)
138 # And 3 is a dupe of 2
138 # And 3 is a dupe of 2
139 IssueRelation.create(:issue_from => issue3, :issue_to => issue2, :relation_type => IssueRelation::TYPE_DUPLICATES)
139 IssueRelation.create(:issue_from => issue3, :issue_to => issue2, :relation_type => IssueRelation::TYPE_DUPLICATES)
140 # And 3 is a dupe of 1 (circular duplicates)
140 # And 3 is a dupe of 1 (circular duplicates)
141 IssueRelation.create(:issue_from => issue3, :issue_to => issue1, :relation_type => IssueRelation::TYPE_DUPLICATES)
141 IssueRelation.create(:issue_from => issue3, :issue_to => issue1, :relation_type => IssueRelation::TYPE_DUPLICATES)
142
142
143 assert issue1.reload.duplicates.include?(issue2)
143 assert issue1.reload.duplicates.include?(issue2)
144
144
145 # Closing issue 1
145 # Closing issue 1
146 issue1.init_journal(User.find(:first), "Closing issue1")
146 issue1.init_journal(User.find(:first), "Closing issue1")
147 issue1.status = IssueStatus.find :first, :conditions => {:is_closed => true}
147 issue1.status = IssueStatus.find :first, :conditions => {:is_closed => true}
148 assert issue1.save
148 assert issue1.save
149 # 2 and 3 should be also closed
149 # 2 and 3 should be also closed
150 assert issue2.reload.closed?
150 assert issue2.reload.closed?
151 assert issue3.reload.closed?
151 assert issue3.reload.closed?
152 end
152 end
153
153
154 def test_should_not_close_duplicated_issue
154 def test_should_not_close_duplicated_issue
155 # Create 3 issues
155 # Create 3 issues
156 issue1 = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :priority => Enumeration.get_values('IPRI').first, :subject => 'Duplicates test', :description => 'Duplicates test')
156 issue1 = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :priority => Enumeration.get_values('IPRI').first, :subject => 'Duplicates test', :description => 'Duplicates test')
157 assert issue1.save
157 assert issue1.save
158 issue2 = issue1.clone
158 issue2 = issue1.clone
159 assert issue2.save
159 assert issue2.save
160
160
161 # 2 is a dupe of 1
161 # 2 is a dupe of 1
162 IssueRelation.create(:issue_from => issue2, :issue_to => issue1, :relation_type => IssueRelation::TYPE_DUPLICATES)
162 IssueRelation.create(:issue_from => issue2, :issue_to => issue1, :relation_type => IssueRelation::TYPE_DUPLICATES)
163 # 2 is a dup of 1 but 1 is not a duplicate of 2
163 # 2 is a dup of 1 but 1 is not a duplicate of 2
164 assert !issue2.reload.duplicates.include?(issue1)
164 assert !issue2.reload.duplicates.include?(issue1)
165
165
166 # Closing issue 2
166 # Closing issue 2
167 issue2.init_journal(User.find(:first), "Closing issue2")
167 issue2.init_journal(User.find(:first), "Closing issue2")
168 issue2.status = IssueStatus.find :first, :conditions => {:is_closed => true}
168 issue2.status = IssueStatus.find :first, :conditions => {:is_closed => true}
169 assert issue2.save
169 assert issue2.save
170 # 1 should not be also closed
170 # 1 should not be also closed
171 assert !issue1.reload.closed?
171 assert !issue1.reload.closed?
172 end
172 end
173
173
174 def test_move_to_another_project_with_same_category
174 def test_move_to_another_project_with_same_category
175 issue = Issue.find(1)
175 issue = Issue.find(1)
176 assert issue.move_to(Project.find(2))
176 assert issue.move_to(Project.find(2))
177 issue.reload
177 issue.reload
178 assert_equal 2, issue.project_id
178 assert_equal 2, issue.project_id
179 # Category changes
179 # Category changes
180 assert_equal 4, issue.category_id
180 assert_equal 4, issue.category_id
181 # Make sure time entries were move to the target project
181 # Make sure time entries were move to the target project
182 assert_equal 2, issue.time_entries.first.project_id
182 assert_equal 2, issue.time_entries.first.project_id
183 end
183 end
184
184
185 def test_move_to_another_project_without_same_category
185 def test_move_to_another_project_without_same_category
186 issue = Issue.find(2)
186 issue = Issue.find(2)
187 assert issue.move_to(Project.find(2))
187 assert issue.move_to(Project.find(2))
188 issue.reload
188 issue.reload
189 assert_equal 2, issue.project_id
189 assert_equal 2, issue.project_id
190 # Category cleared
190 # Category cleared
191 assert_nil issue.category_id
191 assert_nil issue.category_id
192 end
192 end
193
193
194 def test_copy_to_the_same_project
194 def test_copy_to_the_same_project
195 issue = Issue.find(1)
195 issue = Issue.find(1)
196 copy = nil
196 copy = nil
197 assert_difference 'Issue.count' do
197 assert_difference 'Issue.count' do
198 copy = issue.move_to(issue.project, nil, :copy => true)
198 copy = issue.move_to(issue.project, nil, :copy => true)
199 end
199 end
200 assert_kind_of Issue, copy
200 assert_kind_of Issue, copy
201 assert_equal issue.project, copy.project
201 assert_equal issue.project, copy.project
202 assert_equal "125", copy.custom_value_for(2).value
202 assert_equal "125", copy.custom_value_for(2).value
203 end
203 end
204
204
205 def test_copy_to_another_project_and_tracker
205 def test_copy_to_another_project_and_tracker
206 issue = Issue.find(1)
206 issue = Issue.find(1)
207 copy = nil
207 copy = nil
208 assert_difference 'Issue.count' do
208 assert_difference 'Issue.count' do
209 copy = issue.move_to(Project.find(3), Tracker.find(2), :copy => true)
209 copy = issue.move_to(Project.find(3), Tracker.find(2), :copy => true)
210 end
210 end
211 assert_kind_of Issue, copy
211 assert_kind_of Issue, copy
212 assert_equal Project.find(3), copy.project
212 assert_equal Project.find(3), copy.project
213 assert_equal Tracker.find(2), copy.tracker
213 assert_equal Tracker.find(2), copy.tracker
214 # Custom field #2 is not associated with target tracker
214 # Custom field #2 is not associated with target tracker
215 assert_nil copy.custom_value_for(2)
215 assert_nil copy.custom_value_for(2)
216 end
216 end
217
217
218 def test_issue_destroy
218 def test_issue_destroy
219 Issue.find(1).destroy
219 Issue.find(1).destroy
220 assert_nil Issue.find_by_id(1)
220 assert_nil Issue.find_by_id(1)
221 assert_nil TimeEntry.find_by_issue_id(1)
221 assert_nil TimeEntry.find_by_issue_id(1)
222 end
222 end
223
223
224 def test_overdue
224 def test_overdue
225 assert Issue.new(:due_date => 1.day.ago.to_date).overdue?
225 assert Issue.new(:due_date => 1.day.ago.to_date).overdue?
226 assert !Issue.new(:due_date => Date.today).overdue?
226 assert !Issue.new(:due_date => Date.today).overdue?
227 assert !Issue.new(:due_date => 1.day.from_now.to_date).overdue?
227 assert !Issue.new(:due_date => 1.day.from_now.to_date).overdue?
228 assert !Issue.new(:due_date => nil).overdue?
228 assert !Issue.new(:due_date => nil).overdue?
229 assert !Issue.new(:due_date => 1.day.ago.to_date, :status => IssueStatus.find(:first, :conditions => {:is_closed => true})).overdue?
229 end
230 end
230 end
231 end
General Comments 0
You need to be logged in to leave comments. Login now