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