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