##// END OF EJS Templates
Don't duplicate users in Issue#assignable_users. From r4240...
Eric Davis -
r4127:73f12765a99b
parent child
Show More
@@ -1,866 +1,866
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_nested_set :scope => 'root_id'
35 acts_as_nested_set :scope => 'root_id'
36 acts_as_attachable :after_remove => :attachment_removed
36 acts_as_attachable :after_remove => :attachment_removed
37 acts_as_customizable
37 acts_as_customizable
38 acts_as_watchable
38 acts_as_watchable
39 acts_as_searchable :columns => ['subject', "#{table_name}.description", "#{Journal.table_name}.notes"],
39 acts_as_searchable :columns => ['subject', "#{table_name}.description", "#{Journal.table_name}.notes"],
40 :include => [:project, :journals],
40 :include => [:project, :journals],
41 # sort by id so that limited eager loading doesn't break with postgresql
41 # sort by id so that limited eager loading doesn't break with postgresql
42 :order_column => "#{table_name}.id"
42 :order_column => "#{table_name}.id"
43 acts_as_event :title => Proc.new {|o| "#{o.tracker.name} ##{o.id} (#{o.status}): #{o.subject}"},
43 acts_as_event :title => Proc.new {|o| "#{o.tracker.name} ##{o.id} (#{o.status}): #{o.subject}"},
44 :url => Proc.new {|o| {:controller => 'issues', :action => 'show', :id => o.id}},
44 :url => Proc.new {|o| {:controller => 'issues', :action => 'show', :id => o.id}},
45 :type => Proc.new {|o| 'issue' + (o.closed? ? ' closed' : '') }
45 :type => Proc.new {|o| 'issue' + (o.closed? ? ' closed' : '') }
46
46
47 acts_as_activity_provider :find_options => {:include => [:project, :author, :tracker]},
47 acts_as_activity_provider :find_options => {:include => [:project, :author, :tracker]},
48 :author_key => :author_id
48 :author_key => :author_id
49
49
50 DONE_RATIO_OPTIONS = %w(issue_field issue_status)
50 DONE_RATIO_OPTIONS = %w(issue_field issue_status)
51
51
52 attr_reader :current_journal
52 attr_reader :current_journal
53
53
54 validates_presence_of :subject, :priority, :project, :tracker, :author, :status
54 validates_presence_of :subject, :priority, :project, :tracker, :author, :status
55
55
56 validates_length_of :subject, :maximum => 255
56 validates_length_of :subject, :maximum => 255
57 validates_inclusion_of :done_ratio, :in => 0..100
57 validates_inclusion_of :done_ratio, :in => 0..100
58 validates_numericality_of :estimated_hours, :allow_nil => true
58 validates_numericality_of :estimated_hours, :allow_nil => true
59
59
60 named_scope :visible, lambda {|*args| { :include => :project,
60 named_scope :visible, lambda {|*args| { :include => :project,
61 :conditions => Project.allowed_to_condition(args.first || User.current, :view_issues) } }
61 :conditions => Project.allowed_to_condition(args.first || User.current, :view_issues) } }
62
62
63 named_scope :open, :conditions => ["#{IssueStatus.table_name}.is_closed = ?", false], :include => :status
63 named_scope :open, :conditions => ["#{IssueStatus.table_name}.is_closed = ?", false], :include => :status
64
64
65 named_scope :recently_updated, :order => "#{Issue.table_name}.updated_on DESC"
65 named_scope :recently_updated, :order => "#{Issue.table_name}.updated_on DESC"
66 named_scope :with_limit, lambda { |limit| { :limit => limit} }
66 named_scope :with_limit, lambda { |limit| { :limit => limit} }
67 named_scope :on_active_project, :include => [:status, :project, :tracker],
67 named_scope :on_active_project, :include => [:status, :project, :tracker],
68 :conditions => ["#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"]
68 :conditions => ["#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"]
69 named_scope :for_gantt, lambda {
69 named_scope :for_gantt, lambda {
70 {
70 {
71 :include => [:tracker, :status, :assigned_to, :priority, :project, :fixed_version],
71 :include => [:tracker, :status, :assigned_to, :priority, :project, :fixed_version],
72 :order => "#{Issue.table_name}.due_date ASC, #{Issue.table_name}.start_date ASC, #{Issue.table_name}.id ASC"
72 :order => "#{Issue.table_name}.due_date ASC, #{Issue.table_name}.start_date ASC, #{Issue.table_name}.id ASC"
73 }
73 }
74 }
74 }
75
75
76 named_scope :without_version, lambda {
76 named_scope :without_version, lambda {
77 {
77 {
78 :conditions => { :fixed_version_id => nil}
78 :conditions => { :fixed_version_id => nil}
79 }
79 }
80 }
80 }
81
81
82 named_scope :with_query, lambda {|query|
82 named_scope :with_query, lambda {|query|
83 {
83 {
84 :conditions => Query.merge_conditions(query.statement)
84 :conditions => Query.merge_conditions(query.statement)
85 }
85 }
86 }
86 }
87
87
88 before_create :default_assign
88 before_create :default_assign
89 before_save :reschedule_following_issues, :close_duplicates, :update_done_ratio_from_issue_status
89 before_save :reschedule_following_issues, :close_duplicates, :update_done_ratio_from_issue_status
90 after_save :update_nested_set_attributes, :update_parent_attributes, :create_journal
90 after_save :update_nested_set_attributes, :update_parent_attributes, :create_journal
91 after_destroy :destroy_children
91 after_destroy :destroy_children
92 after_destroy :update_parent_attributes
92 after_destroy :update_parent_attributes
93
93
94 # Returns true if usr or current user is allowed to view the issue
94 # Returns true if usr or current user is allowed to view the issue
95 def visible?(usr=nil)
95 def visible?(usr=nil)
96 (usr || User.current).allowed_to?(:view_issues, self.project)
96 (usr || User.current).allowed_to?(:view_issues, self.project)
97 end
97 end
98
98
99 def after_initialize
99 def after_initialize
100 if new_record?
100 if new_record?
101 # set default values for new records only
101 # set default values for new records only
102 self.status ||= IssueStatus.default
102 self.status ||= IssueStatus.default
103 self.priority ||= IssuePriority.default
103 self.priority ||= IssuePriority.default
104 end
104 end
105 end
105 end
106
106
107 # Overrides Redmine::Acts::Customizable::InstanceMethods#available_custom_fields
107 # Overrides Redmine::Acts::Customizable::InstanceMethods#available_custom_fields
108 def available_custom_fields
108 def available_custom_fields
109 (project && tracker) ? project.all_issue_custom_fields.select {|c| tracker.custom_fields.include? c } : []
109 (project && tracker) ? project.all_issue_custom_fields.select {|c| tracker.custom_fields.include? c } : []
110 end
110 end
111
111
112 def copy_from(arg)
112 def copy_from(arg)
113 issue = arg.is_a?(Issue) ? arg : Issue.visible.find(arg)
113 issue = arg.is_a?(Issue) ? arg : Issue.visible.find(arg)
114 self.attributes = issue.attributes.dup.except("id", "root_id", "parent_id", "lft", "rgt", "created_on", "updated_on")
114 self.attributes = issue.attributes.dup.except("id", "root_id", "parent_id", "lft", "rgt", "created_on", "updated_on")
115 self.custom_field_values = issue.custom_field_values.inject({}) {|h,v| h[v.custom_field_id] = v.value; h}
115 self.custom_field_values = issue.custom_field_values.inject({}) {|h,v| h[v.custom_field_id] = v.value; h}
116 self.status = issue.status
116 self.status = issue.status
117 self
117 self
118 end
118 end
119
119
120 # Moves/copies an issue to a new project and tracker
120 # Moves/copies an issue to a new project and tracker
121 # Returns the moved/copied issue on success, false on failure
121 # Returns the moved/copied issue on success, false on failure
122 def move_to_project(*args)
122 def move_to_project(*args)
123 ret = Issue.transaction do
123 ret = Issue.transaction do
124 move_to_project_without_transaction(*args) || raise(ActiveRecord::Rollback)
124 move_to_project_without_transaction(*args) || raise(ActiveRecord::Rollback)
125 end || false
125 end || false
126 end
126 end
127
127
128 def move_to_project_without_transaction(new_project, new_tracker = nil, options = {})
128 def move_to_project_without_transaction(new_project, new_tracker = nil, options = {})
129 options ||= {}
129 options ||= {}
130 issue = options[:copy] ? self.class.new.copy_from(self) : self
130 issue = options[:copy] ? self.class.new.copy_from(self) : self
131
131
132 if new_project && issue.project_id != new_project.id
132 if new_project && issue.project_id != new_project.id
133 # delete issue relations
133 # delete issue relations
134 unless Setting.cross_project_issue_relations?
134 unless Setting.cross_project_issue_relations?
135 issue.relations_from.clear
135 issue.relations_from.clear
136 issue.relations_to.clear
136 issue.relations_to.clear
137 end
137 end
138 # issue is moved to another project
138 # issue is moved to another project
139 # reassign to the category with same name if any
139 # reassign to the category with same name if any
140 new_category = issue.category.nil? ? nil : new_project.issue_categories.find_by_name(issue.category.name)
140 new_category = issue.category.nil? ? nil : new_project.issue_categories.find_by_name(issue.category.name)
141 issue.category = new_category
141 issue.category = new_category
142 # Keep the fixed_version if it's still valid in the new_project
142 # Keep the fixed_version if it's still valid in the new_project
143 unless new_project.shared_versions.include?(issue.fixed_version)
143 unless new_project.shared_versions.include?(issue.fixed_version)
144 issue.fixed_version = nil
144 issue.fixed_version = nil
145 end
145 end
146 issue.project = new_project
146 issue.project = new_project
147 if issue.parent && issue.parent.project_id != issue.project_id
147 if issue.parent && issue.parent.project_id != issue.project_id
148 issue.parent_issue_id = nil
148 issue.parent_issue_id = nil
149 end
149 end
150 end
150 end
151 if new_tracker
151 if new_tracker
152 issue.tracker = new_tracker
152 issue.tracker = new_tracker
153 issue.reset_custom_values!
153 issue.reset_custom_values!
154 end
154 end
155 if options[:copy]
155 if options[:copy]
156 issue.custom_field_values = self.custom_field_values.inject({}) {|h,v| h[v.custom_field_id] = v.value; h}
156 issue.custom_field_values = self.custom_field_values.inject({}) {|h,v| h[v.custom_field_id] = v.value; h}
157 issue.status = if options[:attributes] && options[:attributes][:status_id]
157 issue.status = if options[:attributes] && options[:attributes][:status_id]
158 IssueStatus.find_by_id(options[:attributes][:status_id])
158 IssueStatus.find_by_id(options[:attributes][:status_id])
159 else
159 else
160 self.status
160 self.status
161 end
161 end
162 end
162 end
163 # Allow bulk setting of attributes on the issue
163 # Allow bulk setting of attributes on the issue
164 if options[:attributes]
164 if options[:attributes]
165 issue.attributes = options[:attributes]
165 issue.attributes = options[:attributes]
166 end
166 end
167 if issue.save
167 if issue.save
168 unless options[:copy]
168 unless options[:copy]
169 # Manually update project_id on related time entries
169 # Manually update project_id on related time entries
170 TimeEntry.update_all("project_id = #{new_project.id}", {:issue_id => id})
170 TimeEntry.update_all("project_id = #{new_project.id}", {:issue_id => id})
171
171
172 issue.children.each do |child|
172 issue.children.each do |child|
173 unless child.move_to_project_without_transaction(new_project)
173 unless child.move_to_project_without_transaction(new_project)
174 # Move failed and transaction was rollback'd
174 # Move failed and transaction was rollback'd
175 return false
175 return false
176 end
176 end
177 end
177 end
178 end
178 end
179 else
179 else
180 return false
180 return false
181 end
181 end
182 issue
182 issue
183 end
183 end
184
184
185 def status_id=(sid)
185 def status_id=(sid)
186 self.status = nil
186 self.status = nil
187 write_attribute(:status_id, sid)
187 write_attribute(:status_id, sid)
188 end
188 end
189
189
190 def priority_id=(pid)
190 def priority_id=(pid)
191 self.priority = nil
191 self.priority = nil
192 write_attribute(:priority_id, pid)
192 write_attribute(:priority_id, pid)
193 end
193 end
194
194
195 def tracker_id=(tid)
195 def tracker_id=(tid)
196 self.tracker = nil
196 self.tracker = nil
197 result = write_attribute(:tracker_id, tid)
197 result = write_attribute(:tracker_id, tid)
198 @custom_field_values = nil
198 @custom_field_values = nil
199 result
199 result
200 end
200 end
201
201
202 # Overrides attributes= so that tracker_id gets assigned first
202 # Overrides attributes= so that tracker_id gets assigned first
203 def attributes_with_tracker_first=(new_attributes, *args)
203 def attributes_with_tracker_first=(new_attributes, *args)
204 return if new_attributes.nil?
204 return if new_attributes.nil?
205 new_tracker_id = new_attributes['tracker_id'] || new_attributes[:tracker_id]
205 new_tracker_id = new_attributes['tracker_id'] || new_attributes[:tracker_id]
206 if new_tracker_id
206 if new_tracker_id
207 self.tracker_id = new_tracker_id
207 self.tracker_id = new_tracker_id
208 end
208 end
209 send :attributes_without_tracker_first=, new_attributes, *args
209 send :attributes_without_tracker_first=, new_attributes, *args
210 end
210 end
211 # Do not redefine alias chain on reload (see #4838)
211 # Do not redefine alias chain on reload (see #4838)
212 alias_method_chain(:attributes=, :tracker_first) unless method_defined?(:attributes_without_tracker_first=)
212 alias_method_chain(:attributes=, :tracker_first) unless method_defined?(:attributes_without_tracker_first=)
213
213
214 def estimated_hours=(h)
214 def estimated_hours=(h)
215 write_attribute :estimated_hours, (h.is_a?(String) ? h.to_hours : h)
215 write_attribute :estimated_hours, (h.is_a?(String) ? h.to_hours : h)
216 end
216 end
217
217
218 SAFE_ATTRIBUTES = %w(
218 SAFE_ATTRIBUTES = %w(
219 tracker_id
219 tracker_id
220 status_id
220 status_id
221 parent_issue_id
221 parent_issue_id
222 category_id
222 category_id
223 assigned_to_id
223 assigned_to_id
224 priority_id
224 priority_id
225 fixed_version_id
225 fixed_version_id
226 subject
226 subject
227 description
227 description
228 start_date
228 start_date
229 due_date
229 due_date
230 done_ratio
230 done_ratio
231 estimated_hours
231 estimated_hours
232 custom_field_values
232 custom_field_values
233 lock_version
233 lock_version
234 ) unless const_defined?(:SAFE_ATTRIBUTES)
234 ) unless const_defined?(:SAFE_ATTRIBUTES)
235
235
236 # Safely sets attributes
236 # Safely sets attributes
237 # Should be called from controllers instead of #attributes=
237 # Should be called from controllers instead of #attributes=
238 # attr_accessible is too rough because we still want things like
238 # attr_accessible is too rough because we still want things like
239 # Issue.new(:project => foo) to work
239 # Issue.new(:project => foo) to work
240 # TODO: move workflow/permission checks from controllers to here
240 # TODO: move workflow/permission checks from controllers to here
241 def safe_attributes=(attrs, user=User.current)
241 def safe_attributes=(attrs, user=User.current)
242 return if attrs.nil?
242 return if attrs.nil?
243 attrs = attrs.reject {|k,v| !SAFE_ATTRIBUTES.include?(k)}
243 attrs = attrs.reject {|k,v| !SAFE_ATTRIBUTES.include?(k)}
244 if attrs['status_id']
244 if attrs['status_id']
245 unless new_statuses_allowed_to(user).collect(&:id).include?(attrs['status_id'].to_i)
245 unless new_statuses_allowed_to(user).collect(&:id).include?(attrs['status_id'].to_i)
246 attrs.delete('status_id')
246 attrs.delete('status_id')
247 end
247 end
248 end
248 end
249
249
250 unless leaf?
250 unless leaf?
251 attrs.reject! {|k,v| %w(priority_id done_ratio start_date due_date estimated_hours).include?(k)}
251 attrs.reject! {|k,v| %w(priority_id done_ratio start_date due_date estimated_hours).include?(k)}
252 end
252 end
253
253
254 if attrs.has_key?('parent_issue_id')
254 if attrs.has_key?('parent_issue_id')
255 if !user.allowed_to?(:manage_subtasks, project)
255 if !user.allowed_to?(:manage_subtasks, project)
256 attrs.delete('parent_issue_id')
256 attrs.delete('parent_issue_id')
257 elsif !attrs['parent_issue_id'].blank?
257 elsif !attrs['parent_issue_id'].blank?
258 attrs.delete('parent_issue_id') unless Issue.visible(user).exists?(attrs['parent_issue_id'])
258 attrs.delete('parent_issue_id') unless Issue.visible(user).exists?(attrs['parent_issue_id'])
259 end
259 end
260 end
260 end
261
261
262 self.attributes = attrs
262 self.attributes = attrs
263 end
263 end
264
264
265 def done_ratio
265 def done_ratio
266 if Issue.use_status_for_done_ratio? && status && status.default_done_ratio
266 if Issue.use_status_for_done_ratio? && status && status.default_done_ratio
267 status.default_done_ratio
267 status.default_done_ratio
268 else
268 else
269 read_attribute(:done_ratio)
269 read_attribute(:done_ratio)
270 end
270 end
271 end
271 end
272
272
273 def self.use_status_for_done_ratio?
273 def self.use_status_for_done_ratio?
274 Setting.issue_done_ratio == 'issue_status'
274 Setting.issue_done_ratio == 'issue_status'
275 end
275 end
276
276
277 def self.use_field_for_done_ratio?
277 def self.use_field_for_done_ratio?
278 Setting.issue_done_ratio == 'issue_field'
278 Setting.issue_done_ratio == 'issue_field'
279 end
279 end
280
280
281 def validate
281 def validate
282 if self.due_date.nil? && @attributes['due_date'] && !@attributes['due_date'].empty?
282 if self.due_date.nil? && @attributes['due_date'] && !@attributes['due_date'].empty?
283 errors.add :due_date, :not_a_date
283 errors.add :due_date, :not_a_date
284 end
284 end
285
285
286 if self.due_date and self.start_date and self.due_date < self.start_date
286 if self.due_date and self.start_date and self.due_date < self.start_date
287 errors.add :due_date, :greater_than_start_date
287 errors.add :due_date, :greater_than_start_date
288 end
288 end
289
289
290 if start_date && soonest_start && start_date < soonest_start
290 if start_date && soonest_start && start_date < soonest_start
291 errors.add :start_date, :invalid
291 errors.add :start_date, :invalid
292 end
292 end
293
293
294 if fixed_version
294 if fixed_version
295 if !assignable_versions.include?(fixed_version)
295 if !assignable_versions.include?(fixed_version)
296 errors.add :fixed_version_id, :inclusion
296 errors.add :fixed_version_id, :inclusion
297 elsif reopened? && fixed_version.closed?
297 elsif reopened? && fixed_version.closed?
298 errors.add_to_base I18n.t(:error_can_not_reopen_issue_on_closed_version)
298 errors.add_to_base I18n.t(:error_can_not_reopen_issue_on_closed_version)
299 end
299 end
300 end
300 end
301
301
302 # Checks that the issue can not be added/moved to a disabled tracker
302 # Checks that the issue can not be added/moved to a disabled tracker
303 if project && (tracker_id_changed? || project_id_changed?)
303 if project && (tracker_id_changed? || project_id_changed?)
304 unless project.trackers.include?(tracker)
304 unless project.trackers.include?(tracker)
305 errors.add :tracker_id, :inclusion
305 errors.add :tracker_id, :inclusion
306 end
306 end
307 end
307 end
308
308
309 # Checks parent issue assignment
309 # Checks parent issue assignment
310 if @parent_issue
310 if @parent_issue
311 if @parent_issue.project_id != project_id
311 if @parent_issue.project_id != project_id
312 errors.add :parent_issue_id, :not_same_project
312 errors.add :parent_issue_id, :not_same_project
313 elsif !new_record?
313 elsif !new_record?
314 # moving an existing issue
314 # moving an existing issue
315 if @parent_issue.root_id != root_id
315 if @parent_issue.root_id != root_id
316 # we can always move to another tree
316 # we can always move to another tree
317 elsif move_possible?(@parent_issue)
317 elsif move_possible?(@parent_issue)
318 # move accepted inside tree
318 # move accepted inside tree
319 else
319 else
320 errors.add :parent_issue_id, :not_a_valid_parent
320 errors.add :parent_issue_id, :not_a_valid_parent
321 end
321 end
322 end
322 end
323 end
323 end
324 end
324 end
325
325
326 # Set the done_ratio using the status if that setting is set. This will keep the done_ratios
326 # Set the done_ratio using the status if that setting is set. This will keep the done_ratios
327 # even if the user turns off the setting later
327 # even if the user turns off the setting later
328 def update_done_ratio_from_issue_status
328 def update_done_ratio_from_issue_status
329 if Issue.use_status_for_done_ratio? && status && status.default_done_ratio
329 if Issue.use_status_for_done_ratio? && status && status.default_done_ratio
330 self.done_ratio = status.default_done_ratio
330 self.done_ratio = status.default_done_ratio
331 end
331 end
332 end
332 end
333
333
334 def init_journal(user, notes = "")
334 def init_journal(user, notes = "")
335 @current_journal ||= Journal.new(:journalized => self, :user => user, :notes => notes)
335 @current_journal ||= Journal.new(:journalized => self, :user => user, :notes => notes)
336 @issue_before_change = self.clone
336 @issue_before_change = self.clone
337 @issue_before_change.status = self.status
337 @issue_before_change.status = self.status
338 @custom_values_before_change = {}
338 @custom_values_before_change = {}
339 self.custom_values.each {|c| @custom_values_before_change.store c.custom_field_id, c.value }
339 self.custom_values.each {|c| @custom_values_before_change.store c.custom_field_id, c.value }
340 # Make sure updated_on is updated when adding a note.
340 # Make sure updated_on is updated when adding a note.
341 updated_on_will_change!
341 updated_on_will_change!
342 @current_journal
342 @current_journal
343 end
343 end
344
344
345 # Return true if the issue is closed, otherwise false
345 # Return true if the issue is closed, otherwise false
346 def closed?
346 def closed?
347 self.status.is_closed?
347 self.status.is_closed?
348 end
348 end
349
349
350 # Return true if the issue is being reopened
350 # Return true if the issue is being reopened
351 def reopened?
351 def reopened?
352 if !new_record? && status_id_changed?
352 if !new_record? && status_id_changed?
353 status_was = IssueStatus.find_by_id(status_id_was)
353 status_was = IssueStatus.find_by_id(status_id_was)
354 status_new = IssueStatus.find_by_id(status_id)
354 status_new = IssueStatus.find_by_id(status_id)
355 if status_was && status_new && status_was.is_closed? && !status_new.is_closed?
355 if status_was && status_new && status_was.is_closed? && !status_new.is_closed?
356 return true
356 return true
357 end
357 end
358 end
358 end
359 false
359 false
360 end
360 end
361
361
362 # Return true if the issue is being closed
362 # Return true if the issue is being closed
363 def closing?
363 def closing?
364 if !new_record? && status_id_changed?
364 if !new_record? && status_id_changed?
365 status_was = IssueStatus.find_by_id(status_id_was)
365 status_was = IssueStatus.find_by_id(status_id_was)
366 status_new = IssueStatus.find_by_id(status_id)
366 status_new = IssueStatus.find_by_id(status_id)
367 if status_was && status_new && !status_was.is_closed? && status_new.is_closed?
367 if status_was && status_new && !status_was.is_closed? && status_new.is_closed?
368 return true
368 return true
369 end
369 end
370 end
370 end
371 false
371 false
372 end
372 end
373
373
374 # Returns true if the issue is overdue
374 # Returns true if the issue is overdue
375 def overdue?
375 def overdue?
376 !due_date.nil? && (due_date < Date.today) && !status.is_closed?
376 !due_date.nil? && (due_date < Date.today) && !status.is_closed?
377 end
377 end
378
378
379 # Is the amount of work done less than it should for the due date
379 # Is the amount of work done less than it should for the due date
380 def behind_schedule?
380 def behind_schedule?
381 return false if start_date.nil? || due_date.nil?
381 return false if start_date.nil? || due_date.nil?
382 done_date = start_date + ((due_date - start_date+1)* done_ratio/100).floor
382 done_date = start_date + ((due_date - start_date+1)* done_ratio/100).floor
383 return done_date <= Date.today
383 return done_date <= Date.today
384 end
384 end
385
385
386 # Does this issue have children?
386 # Does this issue have children?
387 def children?
387 def children?
388 !leaf?
388 !leaf?
389 end
389 end
390
390
391 # Users the issue can be assigned to
391 # Users the issue can be assigned to
392 def assignable_users
392 def assignable_users
393 users = project.assignable_users
393 users = project.assignable_users
394 users << author if author
394 users << author if author
395 users.sort
395 users.uniq.sort
396 end
396 end
397
397
398 # Versions that the issue can be assigned to
398 # Versions that the issue can be assigned to
399 def assignable_versions
399 def assignable_versions
400 @assignable_versions ||= (project.shared_versions.open + [Version.find_by_id(fixed_version_id_was)]).compact.uniq.sort
400 @assignable_versions ||= (project.shared_versions.open + [Version.find_by_id(fixed_version_id_was)]).compact.uniq.sort
401 end
401 end
402
402
403 # Returns true if this issue is blocked by another issue that is still open
403 # Returns true if this issue is blocked by another issue that is still open
404 def blocked?
404 def blocked?
405 !relations_to.detect {|ir| ir.relation_type == 'blocks' && !ir.issue_from.closed?}.nil?
405 !relations_to.detect {|ir| ir.relation_type == 'blocks' && !ir.issue_from.closed?}.nil?
406 end
406 end
407
407
408 # Returns an array of status that user is able to apply
408 # Returns an array of status that user is able to apply
409 def new_statuses_allowed_to(user, include_default=false)
409 def new_statuses_allowed_to(user, include_default=false)
410 statuses = status.find_new_statuses_allowed_to(user.roles_for_project(project), tracker)
410 statuses = status.find_new_statuses_allowed_to(user.roles_for_project(project), tracker)
411 statuses << status unless statuses.empty?
411 statuses << status unless statuses.empty?
412 statuses << IssueStatus.default if include_default
412 statuses << IssueStatus.default if include_default
413 statuses = statuses.uniq.sort
413 statuses = statuses.uniq.sort
414 blocked? ? statuses.reject {|s| s.is_closed?} : statuses
414 blocked? ? statuses.reject {|s| s.is_closed?} : statuses
415 end
415 end
416
416
417 # Returns the mail adresses of users that should be notified
417 # Returns the mail adresses of users that should be notified
418 def recipients
418 def recipients
419 notified = project.notified_users
419 notified = project.notified_users
420 # Author and assignee are always notified unless they have been
420 # Author and assignee are always notified unless they have been
421 # locked or don't want to be notified
421 # locked or don't want to be notified
422 notified << author if author && author.active? && author.notify_about?(self)
422 notified << author if author && author.active? && author.notify_about?(self)
423 notified << assigned_to if assigned_to && assigned_to.active? && assigned_to.notify_about?(self)
423 notified << assigned_to if assigned_to && assigned_to.active? && assigned_to.notify_about?(self)
424 notified.uniq!
424 notified.uniq!
425 # Remove users that can not view the issue
425 # Remove users that can not view the issue
426 notified.reject! {|user| !visible?(user)}
426 notified.reject! {|user| !visible?(user)}
427 notified.collect(&:mail)
427 notified.collect(&:mail)
428 end
428 end
429
429
430 # Returns the total number of hours spent on this issue and its descendants
430 # Returns the total number of hours spent on this issue and its descendants
431 #
431 #
432 # Example:
432 # Example:
433 # spent_hours => 0.0
433 # spent_hours => 0.0
434 # spent_hours => 50.2
434 # spent_hours => 50.2
435 def spent_hours
435 def spent_hours
436 @spent_hours ||= self_and_descendants.sum("#{TimeEntry.table_name}.hours", :include => :time_entries).to_f || 0.0
436 @spent_hours ||= self_and_descendants.sum("#{TimeEntry.table_name}.hours", :include => :time_entries).to_f || 0.0
437 end
437 end
438
438
439 def relations
439 def relations
440 (relations_from + relations_to).sort
440 (relations_from + relations_to).sort
441 end
441 end
442
442
443 def all_dependent_issues
443 def all_dependent_issues
444 dependencies = []
444 dependencies = []
445 relations_from.each do |relation|
445 relations_from.each do |relation|
446 dependencies << relation.issue_to
446 dependencies << relation.issue_to
447 dependencies += relation.issue_to.all_dependent_issues
447 dependencies += relation.issue_to.all_dependent_issues
448 end
448 end
449 dependencies
449 dependencies
450 end
450 end
451
451
452 # Returns an array of issues that duplicate this one
452 # Returns an array of issues that duplicate this one
453 def duplicates
453 def duplicates
454 relations_to.select {|r| r.relation_type == IssueRelation::TYPE_DUPLICATES}.collect {|r| r.issue_from}
454 relations_to.select {|r| r.relation_type == IssueRelation::TYPE_DUPLICATES}.collect {|r| r.issue_from}
455 end
455 end
456
456
457 # Returns the due date or the target due date if any
457 # Returns the due date or the target due date if any
458 # Used on gantt chart
458 # Used on gantt chart
459 def due_before
459 def due_before
460 due_date || (fixed_version ? fixed_version.effective_date : nil)
460 due_date || (fixed_version ? fixed_version.effective_date : nil)
461 end
461 end
462
462
463 # Returns the time scheduled for this issue.
463 # Returns the time scheduled for this issue.
464 #
464 #
465 # Example:
465 # Example:
466 # Start Date: 2/26/09, End Date: 3/04/09
466 # Start Date: 2/26/09, End Date: 3/04/09
467 # duration => 6
467 # duration => 6
468 def duration
468 def duration
469 (start_date && due_date) ? due_date - start_date : 0
469 (start_date && due_date) ? due_date - start_date : 0
470 end
470 end
471
471
472 def soonest_start
472 def soonest_start
473 @soonest_start ||= (
473 @soonest_start ||= (
474 relations_to.collect{|relation| relation.successor_soonest_start} +
474 relations_to.collect{|relation| relation.successor_soonest_start} +
475 ancestors.collect(&:soonest_start)
475 ancestors.collect(&:soonest_start)
476 ).compact.max
476 ).compact.max
477 end
477 end
478
478
479 def reschedule_after(date)
479 def reschedule_after(date)
480 return if date.nil?
480 return if date.nil?
481 if leaf?
481 if leaf?
482 if start_date.nil? || start_date < date
482 if start_date.nil? || start_date < date
483 self.start_date, self.due_date = date, date + duration
483 self.start_date, self.due_date = date, date + duration
484 save
484 save
485 end
485 end
486 else
486 else
487 leaves.each do |leaf|
487 leaves.each do |leaf|
488 leaf.reschedule_after(date)
488 leaf.reschedule_after(date)
489 end
489 end
490 end
490 end
491 end
491 end
492
492
493 def <=>(issue)
493 def <=>(issue)
494 if issue.nil?
494 if issue.nil?
495 -1
495 -1
496 elsif root_id != issue.root_id
496 elsif root_id != issue.root_id
497 (root_id || 0) <=> (issue.root_id || 0)
497 (root_id || 0) <=> (issue.root_id || 0)
498 else
498 else
499 (lft || 0) <=> (issue.lft || 0)
499 (lft || 0) <=> (issue.lft || 0)
500 end
500 end
501 end
501 end
502
502
503 def to_s
503 def to_s
504 "#{tracker} ##{id}: #{subject}"
504 "#{tracker} ##{id}: #{subject}"
505 end
505 end
506
506
507 # Returns a string of css classes that apply to the issue
507 # Returns a string of css classes that apply to the issue
508 def css_classes
508 def css_classes
509 s = "issue status-#{status.position} priority-#{priority.position}"
509 s = "issue status-#{status.position} priority-#{priority.position}"
510 s << ' closed' if closed?
510 s << ' closed' if closed?
511 s << ' overdue' if overdue?
511 s << ' overdue' if overdue?
512 s << ' created-by-me' if User.current.logged? && author_id == User.current.id
512 s << ' created-by-me' if User.current.logged? && author_id == User.current.id
513 s << ' assigned-to-me' if User.current.logged? && assigned_to_id == User.current.id
513 s << ' assigned-to-me' if User.current.logged? && assigned_to_id == User.current.id
514 s
514 s
515 end
515 end
516
516
517 # Saves an issue, time_entry, attachments, and a journal from the parameters
517 # Saves an issue, time_entry, attachments, and a journal from the parameters
518 # Returns false if save fails
518 # Returns false if save fails
519 def save_issue_with_child_records(params, existing_time_entry=nil)
519 def save_issue_with_child_records(params, existing_time_entry=nil)
520 Issue.transaction do
520 Issue.transaction do
521 if params[:time_entry] && params[:time_entry][:hours].present? && User.current.allowed_to?(:log_time, project)
521 if params[:time_entry] && params[:time_entry][:hours].present? && User.current.allowed_to?(:log_time, project)
522 @time_entry = existing_time_entry || TimeEntry.new
522 @time_entry = existing_time_entry || TimeEntry.new
523 @time_entry.project = project
523 @time_entry.project = project
524 @time_entry.issue = self
524 @time_entry.issue = self
525 @time_entry.user = User.current
525 @time_entry.user = User.current
526 @time_entry.spent_on = Date.today
526 @time_entry.spent_on = Date.today
527 @time_entry.attributes = params[:time_entry]
527 @time_entry.attributes = params[:time_entry]
528 self.time_entries << @time_entry
528 self.time_entries << @time_entry
529 end
529 end
530
530
531 if valid?
531 if valid?
532 attachments = Attachment.attach_files(self, params[:attachments])
532 attachments = Attachment.attach_files(self, params[:attachments])
533
533
534 attachments[:files].each {|a| @current_journal.details << JournalDetail.new(:property => 'attachment', :prop_key => a.id, :value => a.filename)}
534 attachments[:files].each {|a| @current_journal.details << JournalDetail.new(:property => 'attachment', :prop_key => a.id, :value => a.filename)}
535 # TODO: Rename hook
535 # TODO: Rename hook
536 Redmine::Hook.call_hook(:controller_issues_edit_before_save, { :params => params, :issue => self, :time_entry => @time_entry, :journal => @current_journal})
536 Redmine::Hook.call_hook(:controller_issues_edit_before_save, { :params => params, :issue => self, :time_entry => @time_entry, :journal => @current_journal})
537 begin
537 begin
538 if save
538 if save
539 # TODO: Rename hook
539 # TODO: Rename hook
540 Redmine::Hook.call_hook(:controller_issues_edit_after_save, { :params => params, :issue => self, :time_entry => @time_entry, :journal => @current_journal})
540 Redmine::Hook.call_hook(:controller_issues_edit_after_save, { :params => params, :issue => self, :time_entry => @time_entry, :journal => @current_journal})
541 else
541 else
542 raise ActiveRecord::Rollback
542 raise ActiveRecord::Rollback
543 end
543 end
544 rescue ActiveRecord::StaleObjectError
544 rescue ActiveRecord::StaleObjectError
545 attachments[:files].each(&:destroy)
545 attachments[:files].each(&:destroy)
546 errors.add_to_base l(:notice_locking_conflict)
546 errors.add_to_base l(:notice_locking_conflict)
547 raise ActiveRecord::Rollback
547 raise ActiveRecord::Rollback
548 end
548 end
549 end
549 end
550 end
550 end
551 end
551 end
552
552
553 # Unassigns issues from +version+ if it's no longer shared with issue's project
553 # Unassigns issues from +version+ if it's no longer shared with issue's project
554 def self.update_versions_from_sharing_change(version)
554 def self.update_versions_from_sharing_change(version)
555 # Update issues assigned to the version
555 # Update issues assigned to the version
556 update_versions(["#{Issue.table_name}.fixed_version_id = ?", version.id])
556 update_versions(["#{Issue.table_name}.fixed_version_id = ?", version.id])
557 end
557 end
558
558
559 # Unassigns issues from versions that are no longer shared
559 # Unassigns issues from versions that are no longer shared
560 # after +project+ was moved
560 # after +project+ was moved
561 def self.update_versions_from_hierarchy_change(project)
561 def self.update_versions_from_hierarchy_change(project)
562 moved_project_ids = project.self_and_descendants.reload.collect(&:id)
562 moved_project_ids = project.self_and_descendants.reload.collect(&:id)
563 # Update issues of the moved projects and issues assigned to a version of a moved project
563 # Update issues of the moved projects and issues assigned to a version of a moved project
564 Issue.update_versions(["#{Version.table_name}.project_id IN (?) OR #{Issue.table_name}.project_id IN (?)", moved_project_ids, moved_project_ids])
564 Issue.update_versions(["#{Version.table_name}.project_id IN (?) OR #{Issue.table_name}.project_id IN (?)", moved_project_ids, moved_project_ids])
565 end
565 end
566
566
567 def parent_issue_id=(arg)
567 def parent_issue_id=(arg)
568 parent_issue_id = arg.blank? ? nil : arg.to_i
568 parent_issue_id = arg.blank? ? nil : arg.to_i
569 if parent_issue_id && @parent_issue = Issue.find_by_id(parent_issue_id)
569 if parent_issue_id && @parent_issue = Issue.find_by_id(parent_issue_id)
570 @parent_issue.id
570 @parent_issue.id
571 else
571 else
572 @parent_issue = nil
572 @parent_issue = nil
573 nil
573 nil
574 end
574 end
575 end
575 end
576
576
577 def parent_issue_id
577 def parent_issue_id
578 if instance_variable_defined? :@parent_issue
578 if instance_variable_defined? :@parent_issue
579 @parent_issue.nil? ? nil : @parent_issue.id
579 @parent_issue.nil? ? nil : @parent_issue.id
580 else
580 else
581 parent_id
581 parent_id
582 end
582 end
583 end
583 end
584
584
585 # Extracted from the ReportsController.
585 # Extracted from the ReportsController.
586 def self.by_tracker(project)
586 def self.by_tracker(project)
587 count_and_group_by(:project => project,
587 count_and_group_by(:project => project,
588 :field => 'tracker_id',
588 :field => 'tracker_id',
589 :joins => Tracker.table_name)
589 :joins => Tracker.table_name)
590 end
590 end
591
591
592 def self.by_version(project)
592 def self.by_version(project)
593 count_and_group_by(:project => project,
593 count_and_group_by(:project => project,
594 :field => 'fixed_version_id',
594 :field => 'fixed_version_id',
595 :joins => Version.table_name)
595 :joins => Version.table_name)
596 end
596 end
597
597
598 def self.by_priority(project)
598 def self.by_priority(project)
599 count_and_group_by(:project => project,
599 count_and_group_by(:project => project,
600 :field => 'priority_id',
600 :field => 'priority_id',
601 :joins => IssuePriority.table_name)
601 :joins => IssuePriority.table_name)
602 end
602 end
603
603
604 def self.by_category(project)
604 def self.by_category(project)
605 count_and_group_by(:project => project,
605 count_and_group_by(:project => project,
606 :field => 'category_id',
606 :field => 'category_id',
607 :joins => IssueCategory.table_name)
607 :joins => IssueCategory.table_name)
608 end
608 end
609
609
610 def self.by_assigned_to(project)
610 def self.by_assigned_to(project)
611 count_and_group_by(:project => project,
611 count_and_group_by(:project => project,
612 :field => 'assigned_to_id',
612 :field => 'assigned_to_id',
613 :joins => User.table_name)
613 :joins => User.table_name)
614 end
614 end
615
615
616 def self.by_author(project)
616 def self.by_author(project)
617 count_and_group_by(:project => project,
617 count_and_group_by(:project => project,
618 :field => 'author_id',
618 :field => 'author_id',
619 :joins => User.table_name)
619 :joins => User.table_name)
620 end
620 end
621
621
622 def self.by_subproject(project)
622 def self.by_subproject(project)
623 ActiveRecord::Base.connection.select_all("select s.id as status_id,
623 ActiveRecord::Base.connection.select_all("select s.id as status_id,
624 s.is_closed as closed,
624 s.is_closed as closed,
625 i.project_id as project_id,
625 i.project_id as project_id,
626 count(i.id) as total
626 count(i.id) as total
627 from
627 from
628 #{Issue.table_name} i, #{IssueStatus.table_name} s
628 #{Issue.table_name} i, #{IssueStatus.table_name} s
629 where
629 where
630 i.status_id=s.id
630 i.status_id=s.id
631 and i.project_id IN (#{project.descendants.active.collect{|p| p.id}.join(',')})
631 and i.project_id IN (#{project.descendants.active.collect{|p| p.id}.join(',')})
632 group by s.id, s.is_closed, i.project_id") if project.descendants.active.any?
632 group by s.id, s.is_closed, i.project_id") if project.descendants.active.any?
633 end
633 end
634 # End ReportsController extraction
634 # End ReportsController extraction
635
635
636 # Returns an array of projects that current user can move issues to
636 # Returns an array of projects that current user can move issues to
637 def self.allowed_target_projects_on_move
637 def self.allowed_target_projects_on_move
638 projects = []
638 projects = []
639 if User.current.admin?
639 if User.current.admin?
640 # admin is allowed to move issues to any active (visible) project
640 # admin is allowed to move issues to any active (visible) project
641 projects = Project.visible.all
641 projects = Project.visible.all
642 elsif User.current.logged?
642 elsif User.current.logged?
643 if Role.non_member.allowed_to?(:move_issues)
643 if Role.non_member.allowed_to?(:move_issues)
644 projects = Project.visible.all
644 projects = Project.visible.all
645 else
645 else
646 User.current.memberships.each {|m| projects << m.project if m.roles.detect {|r| r.allowed_to?(:move_issues)}}
646 User.current.memberships.each {|m| projects << m.project if m.roles.detect {|r| r.allowed_to?(:move_issues)}}
647 end
647 end
648 end
648 end
649 projects
649 projects
650 end
650 end
651
651
652 private
652 private
653
653
654 def update_nested_set_attributes
654 def update_nested_set_attributes
655 if root_id.nil?
655 if root_id.nil?
656 # issue was just created
656 # issue was just created
657 self.root_id = (@parent_issue.nil? ? id : @parent_issue.root_id)
657 self.root_id = (@parent_issue.nil? ? id : @parent_issue.root_id)
658 set_default_left_and_right
658 set_default_left_and_right
659 Issue.update_all("root_id = #{root_id}, lft = #{lft}, rgt = #{rgt}", ["id = ?", id])
659 Issue.update_all("root_id = #{root_id}, lft = #{lft}, rgt = #{rgt}", ["id = ?", id])
660 if @parent_issue
660 if @parent_issue
661 move_to_child_of(@parent_issue)
661 move_to_child_of(@parent_issue)
662 end
662 end
663 reload
663 reload
664 elsif parent_issue_id != parent_id
664 elsif parent_issue_id != parent_id
665 former_parent_id = parent_id
665 former_parent_id = parent_id
666 # moving an existing issue
666 # moving an existing issue
667 if @parent_issue && @parent_issue.root_id == root_id
667 if @parent_issue && @parent_issue.root_id == root_id
668 # inside the same tree
668 # inside the same tree
669 move_to_child_of(@parent_issue)
669 move_to_child_of(@parent_issue)
670 else
670 else
671 # to another tree
671 # to another tree
672 unless root?
672 unless root?
673 move_to_right_of(root)
673 move_to_right_of(root)
674 reload
674 reload
675 end
675 end
676 old_root_id = root_id
676 old_root_id = root_id
677 self.root_id = (@parent_issue.nil? ? id : @parent_issue.root_id )
677 self.root_id = (@parent_issue.nil? ? id : @parent_issue.root_id )
678 target_maxright = nested_set_scope.maximum(right_column_name) || 0
678 target_maxright = nested_set_scope.maximum(right_column_name) || 0
679 offset = target_maxright + 1 - lft
679 offset = target_maxright + 1 - lft
680 Issue.update_all("root_id = #{root_id}, lft = lft + #{offset}, rgt = rgt + #{offset}",
680 Issue.update_all("root_id = #{root_id}, lft = lft + #{offset}, rgt = rgt + #{offset}",
681 ["root_id = ? AND lft >= ? AND rgt <= ? ", old_root_id, lft, rgt])
681 ["root_id = ? AND lft >= ? AND rgt <= ? ", old_root_id, lft, rgt])
682 self[left_column_name] = lft + offset
682 self[left_column_name] = lft + offset
683 self[right_column_name] = rgt + offset
683 self[right_column_name] = rgt + offset
684 if @parent_issue
684 if @parent_issue
685 move_to_child_of(@parent_issue)
685 move_to_child_of(@parent_issue)
686 end
686 end
687 end
687 end
688 reload
688 reload
689 # delete invalid relations of all descendants
689 # delete invalid relations of all descendants
690 self_and_descendants.each do |issue|
690 self_and_descendants.each do |issue|
691 issue.relations.each do |relation|
691 issue.relations.each do |relation|
692 relation.destroy unless relation.valid?
692 relation.destroy unless relation.valid?
693 end
693 end
694 end
694 end
695 # update former parent
695 # update former parent
696 recalculate_attributes_for(former_parent_id) if former_parent_id
696 recalculate_attributes_for(former_parent_id) if former_parent_id
697 end
697 end
698 remove_instance_variable(:@parent_issue) if instance_variable_defined?(:@parent_issue)
698 remove_instance_variable(:@parent_issue) if instance_variable_defined?(:@parent_issue)
699 end
699 end
700
700
701 def update_parent_attributes
701 def update_parent_attributes
702 recalculate_attributes_for(parent_id) if parent_id
702 recalculate_attributes_for(parent_id) if parent_id
703 end
703 end
704
704
705 def recalculate_attributes_for(issue_id)
705 def recalculate_attributes_for(issue_id)
706 if issue_id && p = Issue.find_by_id(issue_id)
706 if issue_id && p = Issue.find_by_id(issue_id)
707 # priority = highest priority of children
707 # priority = highest priority of children
708 if priority_position = p.children.maximum("#{IssuePriority.table_name}.position", :include => :priority)
708 if priority_position = p.children.maximum("#{IssuePriority.table_name}.position", :include => :priority)
709 p.priority = IssuePriority.find_by_position(priority_position)
709 p.priority = IssuePriority.find_by_position(priority_position)
710 end
710 end
711
711
712 # start/due dates = lowest/highest dates of children
712 # start/due dates = lowest/highest dates of children
713 p.start_date = p.children.minimum(:start_date)
713 p.start_date = p.children.minimum(:start_date)
714 p.due_date = p.children.maximum(:due_date)
714 p.due_date = p.children.maximum(:due_date)
715 if p.start_date && p.due_date && p.due_date < p.start_date
715 if p.start_date && p.due_date && p.due_date < p.start_date
716 p.start_date, p.due_date = p.due_date, p.start_date
716 p.start_date, p.due_date = p.due_date, p.start_date
717 end
717 end
718
718
719 # done ratio = weighted average ratio of leaves
719 # done ratio = weighted average ratio of leaves
720 unless Issue.use_status_for_done_ratio? && p.status && p.status.default_done_ratio
720 unless Issue.use_status_for_done_ratio? && p.status && p.status.default_done_ratio
721 leaves_count = p.leaves.count
721 leaves_count = p.leaves.count
722 if leaves_count > 0
722 if leaves_count > 0
723 average = p.leaves.average(:estimated_hours).to_f
723 average = p.leaves.average(:estimated_hours).to_f
724 if average == 0
724 if average == 0
725 average = 1
725 average = 1
726 end
726 end
727 done = p.leaves.sum("COALESCE(estimated_hours, #{average}) * (CASE WHEN is_closed = #{connection.quoted_true} THEN 100 ELSE COALESCE(done_ratio, 0) END)", :include => :status).to_f
727 done = p.leaves.sum("COALESCE(estimated_hours, #{average}) * (CASE WHEN is_closed = #{connection.quoted_true} THEN 100 ELSE COALESCE(done_ratio, 0) END)", :include => :status).to_f
728 progress = done / (average * leaves_count)
728 progress = done / (average * leaves_count)
729 p.done_ratio = progress.round
729 p.done_ratio = progress.round
730 end
730 end
731 end
731 end
732
732
733 # estimate = sum of leaves estimates
733 # estimate = sum of leaves estimates
734 p.estimated_hours = p.leaves.sum(:estimated_hours).to_f
734 p.estimated_hours = p.leaves.sum(:estimated_hours).to_f
735 p.estimated_hours = nil if p.estimated_hours == 0.0
735 p.estimated_hours = nil if p.estimated_hours == 0.0
736
736
737 # ancestors will be recursively updated
737 # ancestors will be recursively updated
738 p.save(false)
738 p.save(false)
739 end
739 end
740 end
740 end
741
741
742 def destroy_children
742 def destroy_children
743 unless leaf?
743 unless leaf?
744 children.each do |child|
744 children.each do |child|
745 child.destroy
745 child.destroy
746 end
746 end
747 end
747 end
748 end
748 end
749
749
750 # Update issues so their versions are not pointing to a
750 # Update issues so their versions are not pointing to a
751 # fixed_version that is not shared with the issue's project
751 # fixed_version that is not shared with the issue's project
752 def self.update_versions(conditions=nil)
752 def self.update_versions(conditions=nil)
753 # Only need to update issues with a fixed_version from
753 # Only need to update issues with a fixed_version from
754 # a different project and that is not systemwide shared
754 # a different project and that is not systemwide shared
755 Issue.all(:conditions => merge_conditions("#{Issue.table_name}.fixed_version_id IS NOT NULL" +
755 Issue.all(:conditions => merge_conditions("#{Issue.table_name}.fixed_version_id IS NOT NULL" +
756 " AND #{Issue.table_name}.project_id <> #{Version.table_name}.project_id" +
756 " AND #{Issue.table_name}.project_id <> #{Version.table_name}.project_id" +
757 " AND #{Version.table_name}.sharing <> 'system'",
757 " AND #{Version.table_name}.sharing <> 'system'",
758 conditions),
758 conditions),
759 :include => [:project, :fixed_version]
759 :include => [:project, :fixed_version]
760 ).each do |issue|
760 ).each do |issue|
761 next if issue.project.nil? || issue.fixed_version.nil?
761 next if issue.project.nil? || issue.fixed_version.nil?
762 unless issue.project.shared_versions.include?(issue.fixed_version)
762 unless issue.project.shared_versions.include?(issue.fixed_version)
763 issue.init_journal(User.current)
763 issue.init_journal(User.current)
764 issue.fixed_version = nil
764 issue.fixed_version = nil
765 issue.save
765 issue.save
766 end
766 end
767 end
767 end
768 end
768 end
769
769
770 # Callback on attachment deletion
770 # Callback on attachment deletion
771 def attachment_removed(obj)
771 def attachment_removed(obj)
772 journal = init_journal(User.current)
772 journal = init_journal(User.current)
773 journal.details << JournalDetail.new(:property => 'attachment',
773 journal.details << JournalDetail.new(:property => 'attachment',
774 :prop_key => obj.id,
774 :prop_key => obj.id,
775 :old_value => obj.filename)
775 :old_value => obj.filename)
776 journal.save
776 journal.save
777 end
777 end
778
778
779 # Default assignment based on category
779 # Default assignment based on category
780 def default_assign
780 def default_assign
781 if assigned_to.nil? && category && category.assigned_to
781 if assigned_to.nil? && category && category.assigned_to
782 self.assigned_to = category.assigned_to
782 self.assigned_to = category.assigned_to
783 end
783 end
784 end
784 end
785
785
786 # Updates start/due dates of following issues
786 # Updates start/due dates of following issues
787 def reschedule_following_issues
787 def reschedule_following_issues
788 if start_date_changed? || due_date_changed?
788 if start_date_changed? || due_date_changed?
789 relations_from.each do |relation|
789 relations_from.each do |relation|
790 relation.set_issue_to_dates
790 relation.set_issue_to_dates
791 end
791 end
792 end
792 end
793 end
793 end
794
794
795 # Closes duplicates if the issue is being closed
795 # Closes duplicates if the issue is being closed
796 def close_duplicates
796 def close_duplicates
797 if closing?
797 if closing?
798 duplicates.each do |duplicate|
798 duplicates.each do |duplicate|
799 # Reload is need in case the duplicate was updated by a previous duplicate
799 # Reload is need in case the duplicate was updated by a previous duplicate
800 duplicate.reload
800 duplicate.reload
801 # Don't re-close it if it's already closed
801 # Don't re-close it if it's already closed
802 next if duplicate.closed?
802 next if duplicate.closed?
803 # Same user and notes
803 # Same user and notes
804 if @current_journal
804 if @current_journal
805 duplicate.init_journal(@current_journal.user, @current_journal.notes)
805 duplicate.init_journal(@current_journal.user, @current_journal.notes)
806 end
806 end
807 duplicate.update_attribute :status, self.status
807 duplicate.update_attribute :status, self.status
808 end
808 end
809 end
809 end
810 end
810 end
811
811
812 # Saves the changes in a Journal
812 # Saves the changes in a Journal
813 # Called after_save
813 # Called after_save
814 def create_journal
814 def create_journal
815 if @current_journal
815 if @current_journal
816 # attributes changes
816 # attributes changes
817 (Issue.column_names - %w(id description root_id lft rgt lock_version created_on updated_on)).each {|c|
817 (Issue.column_names - %w(id description root_id lft rgt lock_version created_on updated_on)).each {|c|
818 @current_journal.details << JournalDetail.new(:property => 'attr',
818 @current_journal.details << JournalDetail.new(:property => 'attr',
819 :prop_key => c,
819 :prop_key => c,
820 :old_value => @issue_before_change.send(c),
820 :old_value => @issue_before_change.send(c),
821 :value => send(c)) unless send(c)==@issue_before_change.send(c)
821 :value => send(c)) unless send(c)==@issue_before_change.send(c)
822 }
822 }
823 # custom fields changes
823 # custom fields changes
824 custom_values.each {|c|
824 custom_values.each {|c|
825 next if (@custom_values_before_change[c.custom_field_id]==c.value ||
825 next if (@custom_values_before_change[c.custom_field_id]==c.value ||
826 (@custom_values_before_change[c.custom_field_id].blank? && c.value.blank?))
826 (@custom_values_before_change[c.custom_field_id].blank? && c.value.blank?))
827 @current_journal.details << JournalDetail.new(:property => 'cf',
827 @current_journal.details << JournalDetail.new(:property => 'cf',
828 :prop_key => c.custom_field_id,
828 :prop_key => c.custom_field_id,
829 :old_value => @custom_values_before_change[c.custom_field_id],
829 :old_value => @custom_values_before_change[c.custom_field_id],
830 :value => c.value)
830 :value => c.value)
831 }
831 }
832 @current_journal.save
832 @current_journal.save
833 # reset current journal
833 # reset current journal
834 init_journal @current_journal.user, @current_journal.notes
834 init_journal @current_journal.user, @current_journal.notes
835 end
835 end
836 end
836 end
837
837
838 # Query generator for selecting groups of issue counts for a project
838 # Query generator for selecting groups of issue counts for a project
839 # based on specific criteria
839 # based on specific criteria
840 #
840 #
841 # Options
841 # Options
842 # * project - Project to search in.
842 # * project - Project to search in.
843 # * field - String. Issue field to key off of in the grouping.
843 # * field - String. Issue field to key off of in the grouping.
844 # * joins - String. The table name to join against.
844 # * joins - String. The table name to join against.
845 def self.count_and_group_by(options)
845 def self.count_and_group_by(options)
846 project = options.delete(:project)
846 project = options.delete(:project)
847 select_field = options.delete(:field)
847 select_field = options.delete(:field)
848 joins = options.delete(:joins)
848 joins = options.delete(:joins)
849
849
850 where = "i.#{select_field}=j.id"
850 where = "i.#{select_field}=j.id"
851
851
852 ActiveRecord::Base.connection.select_all("select s.id as status_id,
852 ActiveRecord::Base.connection.select_all("select s.id as status_id,
853 s.is_closed as closed,
853 s.is_closed as closed,
854 j.id as #{select_field},
854 j.id as #{select_field},
855 count(i.id) as total
855 count(i.id) as total
856 from
856 from
857 #{Issue.table_name} i, #{IssueStatus.table_name} s, #{joins} j
857 #{Issue.table_name} i, #{IssueStatus.table_name} s, #{joins} j
858 where
858 where
859 i.status_id=s.id
859 i.status_id=s.id
860 and #{where}
860 and #{where}
861 and i.project_id=#{project.id}
861 and i.project_id=#{project.id}
862 group by s.id, s.is_closed, j.id")
862 group by s.id, s.is_closed, j.id")
863 end
863 end
864
864
865
865
866 end
866 end
@@ -1,796 +1,805
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(Project.find(2))
332 assert issue.move_to_project(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(Project.find(2))
343 assert issue.move_to_project(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(Project.find(2))
353 assert issue.move_to_project(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(Project.find(5))
363 assert issue.move_to_project(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(Project.find(5))
373 assert issue.move_to_project(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(Project.find(2))
383 assert issue.move_to_project(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
390 def test_move_to_another_project_with_disabled_tracker
391 issue = Issue.find(1)
391 issue = Issue.find(1)
392 target = Project.find(2)
392 target = Project.find(2)
393 target.tracker_ids = [3]
393 target.tracker_ids = [3]
394 target.save
394 target.save
395 assert_equal false, issue.move_to_project(target)
395 assert_equal false, issue.move_to_project(target)
396 issue.reload
396 issue.reload
397 assert_equal 1, issue.project_id
397 assert_equal 1, issue.project_id
398 end
398 end
399
399
400 def test_copy_to_the_same_project
400 def test_copy_to_the_same_project
401 issue = Issue.find(1)
401 issue = Issue.find(1)
402 copy = nil
402 copy = nil
403 assert_difference 'Issue.count' do
403 assert_difference 'Issue.count' do
404 copy = issue.move_to_project(issue.project, nil, :copy => true)
404 copy = issue.move_to_project(issue.project, nil, :copy => true)
405 end
405 end
406 assert_kind_of Issue, copy
406 assert_kind_of Issue, copy
407 assert_equal issue.project, copy.project
407 assert_equal issue.project, copy.project
408 assert_equal "125", copy.custom_value_for(2).value
408 assert_equal "125", copy.custom_value_for(2).value
409 end
409 end
410
410
411 def test_copy_to_another_project_and_tracker
411 def test_copy_to_another_project_and_tracker
412 issue = Issue.find(1)
412 issue = Issue.find(1)
413 copy = nil
413 copy = nil
414 assert_difference 'Issue.count' do
414 assert_difference 'Issue.count' do
415 copy = issue.move_to_project(Project.find(3), Tracker.find(2), :copy => true)
415 copy = issue.move_to_project(Project.find(3), Tracker.find(2), :copy => true)
416 end
416 end
417 copy.reload
417 copy.reload
418 assert_kind_of Issue, copy
418 assert_kind_of Issue, copy
419 assert_equal Project.find(3), copy.project
419 assert_equal Project.find(3), copy.project
420 assert_equal Tracker.find(2), copy.tracker
420 assert_equal Tracker.find(2), copy.tracker
421 # Custom field #2 is not associated with target tracker
421 # Custom field #2 is not associated with target tracker
422 assert_nil copy.custom_value_for(2)
422 assert_nil copy.custom_value_for(2)
423 end
423 end
424
424
425 context "#move_to_project" do
425 context "#move_to_project" do
426 context "as a copy" do
426 context "as a copy" do
427 setup do
427 setup do
428 @issue = Issue.find(1)
428 @issue = Issue.find(1)
429 @copy = nil
429 @copy = nil
430 end
430 end
431
431
432 should "allow assigned_to changes" do
432 should "allow assigned_to changes" do
433 @copy = @issue.move_to_project(Project.find(3), Tracker.find(2), {:copy => true, :attributes => {:assigned_to_id => 3}})
433 @copy = @issue.move_to_project(Project.find(3), Tracker.find(2), {:copy => true, :attributes => {:assigned_to_id => 3}})
434 assert_equal 3, @copy.assigned_to_id
434 assert_equal 3, @copy.assigned_to_id
435 end
435 end
436
436
437 should "allow status changes" do
437 should "allow status changes" do
438 @copy = @issue.move_to_project(Project.find(3), Tracker.find(2), {:copy => true, :attributes => {:status_id => 2}})
438 @copy = @issue.move_to_project(Project.find(3), Tracker.find(2), {:copy => true, :attributes => {:status_id => 2}})
439 assert_equal 2, @copy.status_id
439 assert_equal 2, @copy.status_id
440 end
440 end
441
441
442 should "allow start date changes" do
442 should "allow start date changes" do
443 date = Date.today
443 date = Date.today
444 @copy = @issue.move_to_project(Project.find(3), Tracker.find(2), {:copy => true, :attributes => {:start_date => date}})
444 @copy = @issue.move_to_project(Project.find(3), Tracker.find(2), {:copy => true, :attributes => {:start_date => date}})
445 assert_equal date, @copy.start_date
445 assert_equal date, @copy.start_date
446 end
446 end
447
447
448 should "allow due date changes" do
448 should "allow due date changes" do
449 date = Date.today
449 date = Date.today
450 @copy = @issue.move_to_project(Project.find(3), Tracker.find(2), {:copy => true, :attributes => {:due_date => date}})
450 @copy = @issue.move_to_project(Project.find(3), Tracker.find(2), {:copy => true, :attributes => {:due_date => date}})
451
451
452 assert_equal date, @copy.due_date
452 assert_equal date, @copy.due_date
453 end
453 end
454 end
454 end
455 end
455 end
456
456
457 def test_recipients_should_not_include_users_that_cannot_view_the_issue
457 def test_recipients_should_not_include_users_that_cannot_view_the_issue
458 issue = Issue.find(12)
458 issue = Issue.find(12)
459 assert issue.recipients.include?(issue.author.mail)
459 assert issue.recipients.include?(issue.author.mail)
460 # move the issue to a private project
460 # move the issue to a private project
461 copy = issue.move_to_project(Project.find(5), Tracker.find(2), :copy => true)
461 copy = issue.move_to_project(Project.find(5), Tracker.find(2), :copy => true)
462 # author is not a member of project anymore
462 # author is not a member of project anymore
463 assert !copy.recipients.include?(copy.author.mail)
463 assert !copy.recipients.include?(copy.author.mail)
464 end
464 end
465
465
466 def test_watcher_recipients_should_not_include_users_that_cannot_view_the_issue
466 def test_watcher_recipients_should_not_include_users_that_cannot_view_the_issue
467 user = User.find(3)
467 user = User.find(3)
468 issue = Issue.find(9)
468 issue = Issue.find(9)
469 Watcher.create!(:user => user, :watchable => issue)
469 Watcher.create!(:user => user, :watchable => issue)
470 assert issue.watched_by?(user)
470 assert issue.watched_by?(user)
471 assert !issue.watcher_recipients.include?(user.mail)
471 assert !issue.watcher_recipients.include?(user.mail)
472 end
472 end
473
473
474 def test_issue_destroy
474 def test_issue_destroy
475 Issue.find(1).destroy
475 Issue.find(1).destroy
476 assert_nil Issue.find_by_id(1)
476 assert_nil Issue.find_by_id(1)
477 assert_nil TimeEntry.find_by_issue_id(1)
477 assert_nil TimeEntry.find_by_issue_id(1)
478 end
478 end
479
479
480 def test_blocked
480 def test_blocked
481 blocked_issue = Issue.find(9)
481 blocked_issue = Issue.find(9)
482 blocking_issue = Issue.find(10)
482 blocking_issue = Issue.find(10)
483
483
484 assert blocked_issue.blocked?
484 assert blocked_issue.blocked?
485 assert !blocking_issue.blocked?
485 assert !blocking_issue.blocked?
486 end
486 end
487
487
488 def test_blocked_issues_dont_allow_closed_statuses
488 def test_blocked_issues_dont_allow_closed_statuses
489 blocked_issue = Issue.find(9)
489 blocked_issue = Issue.find(9)
490
490
491 allowed_statuses = blocked_issue.new_statuses_allowed_to(users(:users_002))
491 allowed_statuses = blocked_issue.new_statuses_allowed_to(users(:users_002))
492 assert !allowed_statuses.empty?
492 assert !allowed_statuses.empty?
493 closed_statuses = allowed_statuses.select {|st| st.is_closed?}
493 closed_statuses = allowed_statuses.select {|st| st.is_closed?}
494 assert closed_statuses.empty?
494 assert closed_statuses.empty?
495 end
495 end
496
496
497 def test_unblocked_issues_allow_closed_statuses
497 def test_unblocked_issues_allow_closed_statuses
498 blocking_issue = Issue.find(10)
498 blocking_issue = Issue.find(10)
499
499
500 allowed_statuses = blocking_issue.new_statuses_allowed_to(users(:users_002))
500 allowed_statuses = blocking_issue.new_statuses_allowed_to(users(:users_002))
501 assert !allowed_statuses.empty?
501 assert !allowed_statuses.empty?
502 closed_statuses = allowed_statuses.select {|st| st.is_closed?}
502 closed_statuses = allowed_statuses.select {|st| st.is_closed?}
503 assert !closed_statuses.empty?
503 assert !closed_statuses.empty?
504 end
504 end
505
505
506 def test_overdue
506 def test_overdue
507 assert Issue.new(:due_date => 1.day.ago.to_date).overdue?
507 assert Issue.new(:due_date => 1.day.ago.to_date).overdue?
508 assert !Issue.new(:due_date => Date.today).overdue?
508 assert !Issue.new(:due_date => Date.today).overdue?
509 assert !Issue.new(:due_date => 1.day.from_now.to_date).overdue?
509 assert !Issue.new(:due_date => 1.day.from_now.to_date).overdue?
510 assert !Issue.new(:due_date => nil).overdue?
510 assert !Issue.new(:due_date => nil).overdue?
511 assert !Issue.new(:due_date => 1.day.ago.to_date, :status => IssueStatus.find(:first, :conditions => {:is_closed => true})).overdue?
511 assert !Issue.new(:due_date => 1.day.ago.to_date, :status => IssueStatus.find(:first, :conditions => {:is_closed => true})).overdue?
512 end
512 end
513
513
514 context "#behind_schedule?" do
514 context "#behind_schedule?" do
515 should "be false if the issue has no start_date" do
515 should "be false if the issue has no start_date" do
516 assert !Issue.new(:start_date => nil, :due_date => 1.day.from_now.to_date, :done_ratio => 0).behind_schedule?
516 assert !Issue.new(:start_date => nil, :due_date => 1.day.from_now.to_date, :done_ratio => 0).behind_schedule?
517 end
517 end
518
518
519 should "be false if the issue has no end_date" do
519 should "be false if the issue has no end_date" do
520 assert !Issue.new(:start_date => 1.day.from_now.to_date, :due_date => nil, :done_ratio => 0).behind_schedule?
520 assert !Issue.new(:start_date => 1.day.from_now.to_date, :due_date => nil, :done_ratio => 0).behind_schedule?
521 end
521 end
522
522
523 should "be false if the issue has more done than it's calendar time" do
523 should "be false if the issue has more done than it's calendar time" do
524 assert !Issue.new(:start_date => 50.days.ago.to_date, :due_date => 50.days.from_now.to_date, :done_ratio => 90).behind_schedule?
524 assert !Issue.new(:start_date => 50.days.ago.to_date, :due_date => 50.days.from_now.to_date, :done_ratio => 90).behind_schedule?
525 end
525 end
526
526
527 should "be true if the issue hasn't been started at all" do
527 should "be true if the issue hasn't been started at all" do
528 assert Issue.new(:start_date => 1.day.ago.to_date, :due_date => 1.day.from_now.to_date, :done_ratio => 0).behind_schedule?
528 assert Issue.new(:start_date => 1.day.ago.to_date, :due_date => 1.day.from_now.to_date, :done_ratio => 0).behind_schedule?
529 end
529 end
530
530
531 should "be true if the issue has used more calendar time than it's done ratio" do
531 should "be true if the issue has used more calendar time than it's done ratio" do
532 assert Issue.new(:start_date => 100.days.ago.to_date, :due_date => Date.today, :done_ratio => 90).behind_schedule?
532 assert Issue.new(:start_date => 100.days.ago.to_date, :due_date => Date.today, :done_ratio => 90).behind_schedule?
533 end
533 end
534 end
534 end
535
535
536 context "#assignable_users" do
536 context "#assignable_users" do
537 should "be Users" do
537 should "be Users" do
538 assert_kind_of User, Issue.find(1).assignable_users.first
538 assert_kind_of User, Issue.find(1).assignable_users.first
539 end
539 end
540
540
541 should "include the issue author" do
541 should "include the issue author" do
542 project = Project.find(1)
542 project = Project.find(1)
543 non_project_member = User.generate!
543 non_project_member = User.generate!
544 issue = Issue.generate_for_project!(project, :author => non_project_member)
544 issue = Issue.generate_for_project!(project, :author => non_project_member)
545
545
546 assert issue.assignable_users.include?(non_project_member)
546 assert issue.assignable_users.include?(non_project_member)
547 end
547 end
548
549 should "not show the issue author twice" do
550 assignable_user_ids = Issue.find(1).assignable_users.collect(&:id)
551 assert_equal 2, assignable_user_ids.length
552
553 assignable_user_ids.each do |user_id|
554 assert_equal 1, assignable_user_ids.count(user_id), "User #{user_id} appears more or less than once"
555 end
556 end
548 end
557 end
549
558
550 def test_create_should_send_email_notification
559 def test_create_should_send_email_notification
551 ActionMailer::Base.deliveries.clear
560 ActionMailer::Base.deliveries.clear
552 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')
561 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')
553
562
554 assert issue.save
563 assert issue.save
555 assert_equal 1, ActionMailer::Base.deliveries.size
564 assert_equal 1, ActionMailer::Base.deliveries.size
556 end
565 end
557
566
558 def test_stale_issue_should_not_send_email_notification
567 def test_stale_issue_should_not_send_email_notification
559 ActionMailer::Base.deliveries.clear
568 ActionMailer::Base.deliveries.clear
560 issue = Issue.find(1)
569 issue = Issue.find(1)
561 stale = Issue.find(1)
570 stale = Issue.find(1)
562
571
563 issue.init_journal(User.find(1))
572 issue.init_journal(User.find(1))
564 issue.subject = 'Subjet update'
573 issue.subject = 'Subjet update'
565 assert issue.save
574 assert issue.save
566 assert_equal 1, ActionMailer::Base.deliveries.size
575 assert_equal 1, ActionMailer::Base.deliveries.size
567 ActionMailer::Base.deliveries.clear
576 ActionMailer::Base.deliveries.clear
568
577
569 stale.init_journal(User.find(1))
578 stale.init_journal(User.find(1))
570 stale.subject = 'Another subjet update'
579 stale.subject = 'Another subjet update'
571 assert_raise ActiveRecord::StaleObjectError do
580 assert_raise ActiveRecord::StaleObjectError do
572 stale.save
581 stale.save
573 end
582 end
574 assert ActionMailer::Base.deliveries.empty?
583 assert ActionMailer::Base.deliveries.empty?
575 end
584 end
576
585
577 def test_saving_twice_should_not_duplicate_journal_details
586 def test_saving_twice_should_not_duplicate_journal_details
578 i = Issue.find(:first)
587 i = Issue.find(:first)
579 i.init_journal(User.find(2), 'Some notes')
588 i.init_journal(User.find(2), 'Some notes')
580 # initial changes
589 # initial changes
581 i.subject = 'New subject'
590 i.subject = 'New subject'
582 i.done_ratio = i.done_ratio + 10
591 i.done_ratio = i.done_ratio + 10
583 assert_difference 'Journal.count' do
592 assert_difference 'Journal.count' do
584 assert i.save
593 assert i.save
585 end
594 end
586 # 1 more change
595 # 1 more change
587 i.priority = IssuePriority.find(:first, :conditions => ["id <> ?", i.priority_id])
596 i.priority = IssuePriority.find(:first, :conditions => ["id <> ?", i.priority_id])
588 assert_no_difference 'Journal.count' do
597 assert_no_difference 'Journal.count' do
589 assert_difference 'JournalDetail.count', 1 do
598 assert_difference 'JournalDetail.count', 1 do
590 i.save
599 i.save
591 end
600 end
592 end
601 end
593 # no more change
602 # no more change
594 assert_no_difference 'Journal.count' do
603 assert_no_difference 'Journal.count' do
595 assert_no_difference 'JournalDetail.count' do
604 assert_no_difference 'JournalDetail.count' do
596 i.save
605 i.save
597 end
606 end
598 end
607 end
599 end
608 end
600
609
601 context "#done_ratio" do
610 context "#done_ratio" do
602 setup do
611 setup do
603 @issue = Issue.find(1)
612 @issue = Issue.find(1)
604 @issue_status = IssueStatus.find(1)
613 @issue_status = IssueStatus.find(1)
605 @issue_status.update_attribute(:default_done_ratio, 50)
614 @issue_status.update_attribute(:default_done_ratio, 50)
606 @issue2 = Issue.find(2)
615 @issue2 = Issue.find(2)
607 @issue_status2 = IssueStatus.find(2)
616 @issue_status2 = IssueStatus.find(2)
608 @issue_status2.update_attribute(:default_done_ratio, 0)
617 @issue_status2.update_attribute(:default_done_ratio, 0)
609 end
618 end
610
619
611 context "with Setting.issue_done_ratio using the issue_field" do
620 context "with Setting.issue_done_ratio using the issue_field" do
612 setup do
621 setup do
613 Setting.issue_done_ratio = 'issue_field'
622 Setting.issue_done_ratio = 'issue_field'
614 end
623 end
615
624
616 should "read the issue's field" do
625 should "read the issue's field" do
617 assert_equal 0, @issue.done_ratio
626 assert_equal 0, @issue.done_ratio
618 assert_equal 30, @issue2.done_ratio
627 assert_equal 30, @issue2.done_ratio
619 end
628 end
620 end
629 end
621
630
622 context "with Setting.issue_done_ratio using the issue_status" do
631 context "with Setting.issue_done_ratio using the issue_status" do
623 setup do
632 setup do
624 Setting.issue_done_ratio = 'issue_status'
633 Setting.issue_done_ratio = 'issue_status'
625 end
634 end
626
635
627 should "read the Issue Status's default done ratio" do
636 should "read the Issue Status's default done ratio" do
628 assert_equal 50, @issue.done_ratio
637 assert_equal 50, @issue.done_ratio
629 assert_equal 0, @issue2.done_ratio
638 assert_equal 0, @issue2.done_ratio
630 end
639 end
631 end
640 end
632 end
641 end
633
642
634 context "#update_done_ratio_from_issue_status" do
643 context "#update_done_ratio_from_issue_status" do
635 setup do
644 setup do
636 @issue = Issue.find(1)
645 @issue = Issue.find(1)
637 @issue_status = IssueStatus.find(1)
646 @issue_status = IssueStatus.find(1)
638 @issue_status.update_attribute(:default_done_ratio, 50)
647 @issue_status.update_attribute(:default_done_ratio, 50)
639 @issue2 = Issue.find(2)
648 @issue2 = Issue.find(2)
640 @issue_status2 = IssueStatus.find(2)
649 @issue_status2 = IssueStatus.find(2)
641 @issue_status2.update_attribute(:default_done_ratio, 0)
650 @issue_status2.update_attribute(:default_done_ratio, 0)
642 end
651 end
643
652
644 context "with Setting.issue_done_ratio using the issue_field" do
653 context "with Setting.issue_done_ratio using the issue_field" do
645 setup do
654 setup do
646 Setting.issue_done_ratio = 'issue_field'
655 Setting.issue_done_ratio = 'issue_field'
647 end
656 end
648
657
649 should "not change the issue" do
658 should "not change the issue" do
650 @issue.update_done_ratio_from_issue_status
659 @issue.update_done_ratio_from_issue_status
651 @issue2.update_done_ratio_from_issue_status
660 @issue2.update_done_ratio_from_issue_status
652
661
653 assert_equal 0, @issue.read_attribute(:done_ratio)
662 assert_equal 0, @issue.read_attribute(:done_ratio)
654 assert_equal 30, @issue2.read_attribute(:done_ratio)
663 assert_equal 30, @issue2.read_attribute(:done_ratio)
655 end
664 end
656 end
665 end
657
666
658 context "with Setting.issue_done_ratio using the issue_status" do
667 context "with Setting.issue_done_ratio using the issue_status" do
659 setup do
668 setup do
660 Setting.issue_done_ratio = 'issue_status'
669 Setting.issue_done_ratio = 'issue_status'
661 end
670 end
662
671
663 should "change the issue's done ratio" do
672 should "change the issue's done ratio" do
664 @issue.update_done_ratio_from_issue_status
673 @issue.update_done_ratio_from_issue_status
665 @issue2.update_done_ratio_from_issue_status
674 @issue2.update_done_ratio_from_issue_status
666
675
667 assert_equal 50, @issue.read_attribute(:done_ratio)
676 assert_equal 50, @issue.read_attribute(:done_ratio)
668 assert_equal 0, @issue2.read_attribute(:done_ratio)
677 assert_equal 0, @issue2.read_attribute(:done_ratio)
669 end
678 end
670 end
679 end
671 end
680 end
672
681
673 test "#by_tracker" do
682 test "#by_tracker" do
674 groups = Issue.by_tracker(Project.find(1))
683 groups = Issue.by_tracker(Project.find(1))
675 assert_equal 3, groups.size
684 assert_equal 3, groups.size
676 assert_equal 7, groups.inject(0) {|sum, group| sum + group['total'].to_i}
685 assert_equal 7, groups.inject(0) {|sum, group| sum + group['total'].to_i}
677 end
686 end
678
687
679 test "#by_version" do
688 test "#by_version" do
680 groups = Issue.by_version(Project.find(1))
689 groups = Issue.by_version(Project.find(1))
681 assert_equal 3, groups.size
690 assert_equal 3, groups.size
682 assert_equal 3, groups.inject(0) {|sum, group| sum + group['total'].to_i}
691 assert_equal 3, groups.inject(0) {|sum, group| sum + group['total'].to_i}
683 end
692 end
684
693
685 test "#by_priority" do
694 test "#by_priority" do
686 groups = Issue.by_priority(Project.find(1))
695 groups = Issue.by_priority(Project.find(1))
687 assert_equal 4, groups.size
696 assert_equal 4, groups.size
688 assert_equal 7, groups.inject(0) {|sum, group| sum + group['total'].to_i}
697 assert_equal 7, groups.inject(0) {|sum, group| sum + group['total'].to_i}
689 end
698 end
690
699
691 test "#by_category" do
700 test "#by_category" do
692 groups = Issue.by_category(Project.find(1))
701 groups = Issue.by_category(Project.find(1))
693 assert_equal 2, groups.size
702 assert_equal 2, groups.size
694 assert_equal 3, groups.inject(0) {|sum, group| sum + group['total'].to_i}
703 assert_equal 3, groups.inject(0) {|sum, group| sum + group['total'].to_i}
695 end
704 end
696
705
697 test "#by_assigned_to" do
706 test "#by_assigned_to" do
698 groups = Issue.by_assigned_to(Project.find(1))
707 groups = Issue.by_assigned_to(Project.find(1))
699 assert_equal 2, groups.size
708 assert_equal 2, groups.size
700 assert_equal 2, groups.inject(0) {|sum, group| sum + group['total'].to_i}
709 assert_equal 2, groups.inject(0) {|sum, group| sum + group['total'].to_i}
701 end
710 end
702
711
703 test "#by_author" do
712 test "#by_author" do
704 groups = Issue.by_author(Project.find(1))
713 groups = Issue.by_author(Project.find(1))
705 assert_equal 4, groups.size
714 assert_equal 4, groups.size
706 assert_equal 7, groups.inject(0) {|sum, group| sum + group['total'].to_i}
715 assert_equal 7, groups.inject(0) {|sum, group| sum + group['total'].to_i}
707 end
716 end
708
717
709 test "#by_subproject" do
718 test "#by_subproject" do
710 groups = Issue.by_subproject(Project.find(1))
719 groups = Issue.by_subproject(Project.find(1))
711 assert_equal 2, groups.size
720 assert_equal 2, groups.size
712 assert_equal 5, groups.inject(0) {|sum, group| sum + group['total'].to_i}
721 assert_equal 5, groups.inject(0) {|sum, group| sum + group['total'].to_i}
713 end
722 end
714
723
715
724
716 context ".allowed_target_projects_on_move" do
725 context ".allowed_target_projects_on_move" do
717 should "return all active projects for admin users" do
726 should "return all active projects for admin users" do
718 User.current = User.find(1)
727 User.current = User.find(1)
719 assert_equal Project.active.count, Issue.allowed_target_projects_on_move.size
728 assert_equal Project.active.count, Issue.allowed_target_projects_on_move.size
720 end
729 end
721
730
722 should "return allowed projects for non admin users" do
731 should "return allowed projects for non admin users" do
723 User.current = User.find(2)
732 User.current = User.find(2)
724 Role.non_member.remove_permission! :move_issues
733 Role.non_member.remove_permission! :move_issues
725 assert_equal 3, Issue.allowed_target_projects_on_move.size
734 assert_equal 3, Issue.allowed_target_projects_on_move.size
726
735
727 Role.non_member.add_permission! :move_issues
736 Role.non_member.add_permission! :move_issues
728 assert_equal Project.active.count, Issue.allowed_target_projects_on_move.size
737 assert_equal Project.active.count, Issue.allowed_target_projects_on_move.size
729 end
738 end
730 end
739 end
731
740
732 def test_recently_updated_with_limit_scopes
741 def test_recently_updated_with_limit_scopes
733 #should return the last updated issue
742 #should return the last updated issue
734 assert_equal 1, Issue.recently_updated.with_limit(1).length
743 assert_equal 1, Issue.recently_updated.with_limit(1).length
735 assert_equal Issue.find(:first, :order => "updated_on DESC"), Issue.recently_updated.with_limit(1).first
744 assert_equal Issue.find(:first, :order => "updated_on DESC"), Issue.recently_updated.with_limit(1).first
736 end
745 end
737
746
738 def test_on_active_projects_scope
747 def test_on_active_projects_scope
739 assert Project.find(2).archive
748 assert Project.find(2).archive
740
749
741 before = Issue.on_active_project.length
750 before = Issue.on_active_project.length
742 # test inclusion to results
751 # test inclusion to results
743 issue = Issue.generate_for_project!(Project.find(1), :tracker => Project.find(2).trackers.first)
752 issue = Issue.generate_for_project!(Project.find(1), :tracker => Project.find(2).trackers.first)
744 assert_equal before + 1, Issue.on_active_project.length
753 assert_equal before + 1, Issue.on_active_project.length
745
754
746 # Move to an archived project
755 # Move to an archived project
747 issue.project = Project.find(2)
756 issue.project = Project.find(2)
748 assert issue.save
757 assert issue.save
749 assert_equal before, Issue.on_active_project.length
758 assert_equal before, Issue.on_active_project.length
750 end
759 end
751
760
752 context "Issue#recipients" do
761 context "Issue#recipients" do
753 setup do
762 setup do
754 @project = Project.find(1)
763 @project = Project.find(1)
755 @author = User.generate_with_protected!
764 @author = User.generate_with_protected!
756 @assignee = User.generate_with_protected!
765 @assignee = User.generate_with_protected!
757 @issue = Issue.generate_for_project!(@project, :assigned_to => @assignee, :author => @author)
766 @issue = Issue.generate_for_project!(@project, :assigned_to => @assignee, :author => @author)
758 end
767 end
759
768
760 should "include project recipients" do
769 should "include project recipients" do
761 assert @project.recipients.present?
770 assert @project.recipients.present?
762 @project.recipients.each do |project_recipient|
771 @project.recipients.each do |project_recipient|
763 assert @issue.recipients.include?(project_recipient)
772 assert @issue.recipients.include?(project_recipient)
764 end
773 end
765 end
774 end
766
775
767 should "include the author if the author is active" do
776 should "include the author if the author is active" do
768 assert @issue.author, "No author set for Issue"
777 assert @issue.author, "No author set for Issue"
769 assert @issue.recipients.include?(@issue.author.mail)
778 assert @issue.recipients.include?(@issue.author.mail)
770 end
779 end
771
780
772 should "include the assigned to user if the assigned to user is active" do
781 should "include the assigned to user if the assigned to user is active" do
773 assert @issue.assigned_to, "No assigned_to set for Issue"
782 assert @issue.assigned_to, "No assigned_to set for Issue"
774 assert @issue.recipients.include?(@issue.assigned_to.mail)
783 assert @issue.recipients.include?(@issue.assigned_to.mail)
775 end
784 end
776
785
777 should "not include users who opt out of all email" do
786 should "not include users who opt out of all email" do
778 @author.update_attribute(:mail_notification, :none)
787 @author.update_attribute(:mail_notification, :none)
779
788
780 assert !@issue.recipients.include?(@issue.author.mail)
789 assert !@issue.recipients.include?(@issue.author.mail)
781 end
790 end
782
791
783 should "not include the issue author if they are only notified of assigned issues" do
792 should "not include the issue author if they are only notified of assigned issues" do
784 @author.update_attribute(:mail_notification, :only_assigned)
793 @author.update_attribute(:mail_notification, :only_assigned)
785
794
786 assert !@issue.recipients.include?(@issue.author.mail)
795 assert !@issue.recipients.include?(@issue.author.mail)
787 end
796 end
788
797
789 should "not include the assigned user if they are only notified of owned issues" do
798 should "not include the assigned user if they are only notified of owned issues" do
790 @assignee.update_attribute(:mail_notification, :only_owner)
799 @assignee.update_attribute(:mail_notification, :only_owner)
791
800
792 assert !@issue.recipients.include?(@issue.assigned_to.mail)
801 assert !@issue.recipients.include?(@issue.assigned_to.mail)
793 end
802 end
794
803
795 end
804 end
796 end
805 end
General Comments 0
You need to be logged in to leave comments. Login now