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