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