@@ -1,1079 +1,1084 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2012 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | class Issue < ActiveRecord::Base |
|
19 | 19 | include Redmine::SafeAttributes |
|
20 | 20 | |
|
21 | 21 | belongs_to :project |
|
22 | 22 | belongs_to :tracker |
|
23 | 23 | belongs_to :status, :class_name => 'IssueStatus', :foreign_key => 'status_id' |
|
24 | 24 | belongs_to :author, :class_name => 'User', :foreign_key => 'author_id' |
|
25 | 25 | belongs_to :assigned_to, :class_name => 'Principal', :foreign_key => 'assigned_to_id' |
|
26 | 26 | belongs_to :fixed_version, :class_name => 'Version', :foreign_key => 'fixed_version_id' |
|
27 | 27 | belongs_to :priority, :class_name => 'IssuePriority', :foreign_key => 'priority_id' |
|
28 | 28 | belongs_to :category, :class_name => 'IssueCategory', :foreign_key => 'category_id' |
|
29 | 29 | |
|
30 | 30 | has_many :journals, :as => :journalized, :dependent => :destroy |
|
31 | 31 | has_many :time_entries, :dependent => :delete_all |
|
32 | 32 | has_and_belongs_to_many :changesets, :order => "#{Changeset.table_name}.committed_on ASC, #{Changeset.table_name}.id ASC" |
|
33 | 33 | |
|
34 | 34 | has_many :relations_from, :class_name => 'IssueRelation', :foreign_key => 'issue_from_id', :dependent => :delete_all |
|
35 | 35 | has_many :relations_to, :class_name => 'IssueRelation', :foreign_key => 'issue_to_id', :dependent => :delete_all |
|
36 | 36 | |
|
37 | 37 | acts_as_nested_set :scope => 'root_id', :dependent => :destroy |
|
38 | 38 | acts_as_attachable :after_add => :attachment_added, :after_remove => :attachment_removed |
|
39 | 39 | acts_as_customizable |
|
40 | 40 | acts_as_watchable |
|
41 | 41 | acts_as_searchable :columns => ['subject', "#{table_name}.description", "#{Journal.table_name}.notes"], |
|
42 | 42 | :include => [:project, :journals], |
|
43 | 43 | # sort by id so that limited eager loading doesn't break with postgresql |
|
44 | 44 | :order_column => "#{table_name}.id" |
|
45 | 45 | acts_as_event :title => Proc.new {|o| "#{o.tracker.name} ##{o.id} (#{o.status}): #{o.subject}"}, |
|
46 | 46 | :url => Proc.new {|o| {:controller => 'issues', :action => 'show', :id => o.id}}, |
|
47 | 47 | :type => Proc.new {|o| 'issue' + (o.closed? ? ' closed' : '') } |
|
48 | 48 | |
|
49 | 49 | acts_as_activity_provider :find_options => {:include => [:project, :author, :tracker]}, |
|
50 | 50 | :author_key => :author_id |
|
51 | 51 | |
|
52 | 52 | DONE_RATIO_OPTIONS = %w(issue_field issue_status) |
|
53 | 53 | |
|
54 | 54 | attr_reader :current_journal |
|
55 | 55 | |
|
56 | 56 | validates_presence_of :subject, :priority, :project, :tracker, :author, :status |
|
57 | 57 | |
|
58 | 58 | validates_length_of :subject, :maximum => 255 |
|
59 | 59 | validates_inclusion_of :done_ratio, :in => 0..100 |
|
60 | 60 | validates_numericality_of :estimated_hours, :allow_nil => true |
|
61 | 61 | validate :validate_issue |
|
62 | 62 | |
|
63 | 63 | scope :visible, |
|
64 | 64 | lambda {|*args| { :include => :project, |
|
65 | 65 | :conditions => Issue.visible_condition(args.shift || User.current, *args) } } |
|
66 | 66 | |
|
67 | 67 | scope :open, lambda {|*args| |
|
68 | 68 | is_closed = args.size > 0 ? !args.first : false |
|
69 | 69 | {:conditions => ["#{IssueStatus.table_name}.is_closed = ?", is_closed], :include => :status} |
|
70 | 70 | } |
|
71 | 71 | |
|
72 | 72 | scope :recently_updated, :order => "#{Issue.table_name}.updated_on DESC" |
|
73 | 73 | scope :with_limit, lambda { |limit| { :limit => limit} } |
|
74 | 74 | scope :on_active_project, :include => [:status, :project, :tracker], |
|
75 | 75 | :conditions => ["#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"] |
|
76 | 76 | |
|
77 | 77 | before_create :default_assign |
|
78 | before_save :close_duplicates, :update_done_ratio_from_issue_status | |
|
78 | before_save :close_duplicates, :update_done_ratio_from_issue_status, :force_updated_on_change | |
|
79 | 79 | after_save {|issue| issue.send :after_project_change if !issue.id_changed? && issue.project_id_changed?} |
|
80 | 80 | after_save :reschedule_following_issues, :update_nested_set_attributes, :update_parent_attributes, :create_journal |
|
81 | 81 | after_destroy :update_parent_attributes |
|
82 | 82 | |
|
83 | 83 | # Returns a SQL conditions string used to find all issues visible by the specified user |
|
84 | 84 | def self.visible_condition(user, options={}) |
|
85 | 85 | Project.allowed_to_condition(user, :view_issues, options) do |role, user| |
|
86 | 86 | case role.issues_visibility |
|
87 | 87 | when 'all' |
|
88 | 88 | nil |
|
89 | 89 | when 'default' |
|
90 | 90 | user_ids = [user.id] + user.groups.map(&:id) |
|
91 | 91 | "(#{table_name}.is_private = #{connection.quoted_false} OR #{table_name}.author_id = #{user.id} OR #{table_name}.assigned_to_id IN (#{user_ids.join(',')}))" |
|
92 | 92 | when 'own' |
|
93 | 93 | user_ids = [user.id] + user.groups.map(&:id) |
|
94 | 94 | "(#{table_name}.author_id = #{user.id} OR #{table_name}.assigned_to_id IN (#{user_ids.join(',')}))" |
|
95 | 95 | else |
|
96 | 96 | '1=0' |
|
97 | 97 | end |
|
98 | 98 | end |
|
99 | 99 | end |
|
100 | 100 | |
|
101 | 101 | # Returns true if usr or current user is allowed to view the issue |
|
102 | 102 | def visible?(usr=nil) |
|
103 | 103 | (usr || User.current).allowed_to?(:view_issues, self.project) do |role, user| |
|
104 | 104 | case role.issues_visibility |
|
105 | 105 | when 'all' |
|
106 | 106 | true |
|
107 | 107 | when 'default' |
|
108 | 108 | !self.is_private? || self.author == user || user.is_or_belongs_to?(assigned_to) |
|
109 | 109 | when 'own' |
|
110 | 110 | self.author == user || user.is_or_belongs_to?(assigned_to) |
|
111 | 111 | else |
|
112 | 112 | false |
|
113 | 113 | end |
|
114 | 114 | end |
|
115 | 115 | end |
|
116 | 116 | |
|
117 | 117 | def initialize(attributes=nil, *args) |
|
118 | 118 | super |
|
119 | 119 | if new_record? |
|
120 | 120 | # set default values for new records only |
|
121 | 121 | self.status ||= IssueStatus.default |
|
122 | 122 | self.priority ||= IssuePriority.default |
|
123 | 123 | self.watcher_user_ids = [] |
|
124 | 124 | end |
|
125 | 125 | end |
|
126 | 126 | |
|
127 | 127 | # Overrides Redmine::Acts::Customizable::InstanceMethods#available_custom_fields |
|
128 | 128 | def available_custom_fields |
|
129 | 129 | (project && tracker) ? (project.all_issue_custom_fields & tracker.custom_fields.all) : [] |
|
130 | 130 | end |
|
131 | 131 | |
|
132 | 132 | # Copies attributes from another issue, arg can be an id or an Issue |
|
133 | 133 | def copy_from(arg, options={}) |
|
134 | 134 | issue = arg.is_a?(Issue) ? arg : Issue.visible.find(arg) |
|
135 | 135 | self.attributes = issue.attributes.dup.except("id", "root_id", "parent_id", "lft", "rgt", "created_on", "updated_on") |
|
136 | 136 | self.custom_field_values = issue.custom_field_values.inject({}) {|h,v| h[v.custom_field_id] = v.value; h} |
|
137 | 137 | self.status = issue.status |
|
138 | 138 | self.author = User.current |
|
139 | 139 | unless options[:attachments] == false |
|
140 | 140 | self.attachments = issue.attachments.map do |attachement| |
|
141 | 141 | attachement.copy(:container => self) |
|
142 | 142 | end |
|
143 | 143 | end |
|
144 | 144 | @copied_from = issue |
|
145 | 145 | self |
|
146 | 146 | end |
|
147 | 147 | |
|
148 | 148 | # Returns an unsaved copy of the issue |
|
149 | 149 | def copy(attributes=nil, copy_options={}) |
|
150 | 150 | copy = self.class.new.copy_from(self, copy_options) |
|
151 | 151 | copy.attributes = attributes if attributes |
|
152 | 152 | copy |
|
153 | 153 | end |
|
154 | 154 | |
|
155 | 155 | # Returns true if the issue is a copy |
|
156 | 156 | def copy? |
|
157 | 157 | @copied_from.present? |
|
158 | 158 | end |
|
159 | 159 | |
|
160 | 160 | # Moves/copies an issue to a new project and tracker |
|
161 | 161 | # Returns the moved/copied issue on success, false on failure |
|
162 | 162 | def move_to_project(new_project, new_tracker=nil, options={}) |
|
163 | 163 | ActiveSupport::Deprecation.warn "Issue#move_to_project is deprecated, use #project= instead." |
|
164 | 164 | |
|
165 | 165 | if options[:copy] |
|
166 | 166 | issue = self.copy |
|
167 | 167 | else |
|
168 | 168 | issue = self |
|
169 | 169 | end |
|
170 | 170 | |
|
171 | 171 | issue.init_journal(User.current, options[:notes]) |
|
172 | 172 | |
|
173 | 173 | # Preserve previous behaviour |
|
174 | 174 | # #move_to_project doesn't change tracker automatically |
|
175 | 175 | issue.send :project=, new_project, true |
|
176 | 176 | if new_tracker |
|
177 | 177 | issue.tracker = new_tracker |
|
178 | 178 | end |
|
179 | 179 | # Allow bulk setting of attributes on the issue |
|
180 | 180 | if options[:attributes] |
|
181 | 181 | issue.attributes = options[:attributes] |
|
182 | 182 | end |
|
183 | 183 | |
|
184 | 184 | issue.save ? issue : false |
|
185 | 185 | end |
|
186 | 186 | |
|
187 | 187 | def status_id=(sid) |
|
188 | 188 | self.status = nil |
|
189 | 189 | write_attribute(:status_id, sid) |
|
190 | 190 | end |
|
191 | 191 | |
|
192 | 192 | def priority_id=(pid) |
|
193 | 193 | self.priority = nil |
|
194 | 194 | write_attribute(:priority_id, pid) |
|
195 | 195 | end |
|
196 | 196 | |
|
197 | 197 | def category_id=(cid) |
|
198 | 198 | self.category = nil |
|
199 | 199 | write_attribute(:category_id, cid) |
|
200 | 200 | end |
|
201 | 201 | |
|
202 | 202 | def fixed_version_id=(vid) |
|
203 | 203 | self.fixed_version = nil |
|
204 | 204 | write_attribute(:fixed_version_id, vid) |
|
205 | 205 | end |
|
206 | 206 | |
|
207 | 207 | def tracker_id=(tid) |
|
208 | 208 | self.tracker = nil |
|
209 | 209 | result = write_attribute(:tracker_id, tid) |
|
210 | 210 | @custom_field_values = nil |
|
211 | 211 | result |
|
212 | 212 | end |
|
213 | 213 | |
|
214 | 214 | def project_id=(project_id) |
|
215 | 215 | if project_id.to_s != self.project_id.to_s |
|
216 | 216 | self.project = (project_id.present? ? Project.find_by_id(project_id) : nil) |
|
217 | 217 | end |
|
218 | 218 | end |
|
219 | 219 | |
|
220 | 220 | def project=(project, keep_tracker=false) |
|
221 | 221 | project_was = self.project |
|
222 | 222 | write_attribute(:project_id, project ? project.id : nil) |
|
223 | 223 | association_instance_set('project', project) |
|
224 | 224 | if project_was && project && project_was != project |
|
225 | 225 | unless keep_tracker || project.trackers.include?(tracker) |
|
226 | 226 | self.tracker = project.trackers.first |
|
227 | 227 | end |
|
228 | 228 | # Reassign to the category with same name if any |
|
229 | 229 | if category |
|
230 | 230 | self.category = project.issue_categories.find_by_name(category.name) |
|
231 | 231 | end |
|
232 | 232 | # Keep the fixed_version if it's still valid in the new_project |
|
233 | 233 | if fixed_version && fixed_version.project != project && !project.shared_versions.include?(fixed_version) |
|
234 | 234 | self.fixed_version = nil |
|
235 | 235 | end |
|
236 | 236 | if parent && parent.project_id != project_id |
|
237 | 237 | self.parent_issue_id = nil |
|
238 | 238 | end |
|
239 | 239 | @custom_field_values = nil |
|
240 | 240 | end |
|
241 | 241 | end |
|
242 | 242 | |
|
243 | 243 | def description=(arg) |
|
244 | 244 | if arg.is_a?(String) |
|
245 | 245 | arg = arg.gsub(/(\r\n|\n|\r)/, "\r\n") |
|
246 | 246 | end |
|
247 | 247 | write_attribute(:description, arg) |
|
248 | 248 | end |
|
249 | 249 | |
|
250 | 250 | # Overrides assign_attributes so that project and tracker get assigned first |
|
251 | 251 | def assign_attributes_with_project_and_tracker_first(new_attributes, *args) |
|
252 | 252 | return if new_attributes.nil? |
|
253 | 253 | attrs = new_attributes.dup |
|
254 | 254 | attrs.stringify_keys! |
|
255 | 255 | |
|
256 | 256 | %w(project project_id tracker tracker_id).each do |attr| |
|
257 | 257 | if attrs.has_key?(attr) |
|
258 | 258 | send "#{attr}=", attrs.delete(attr) |
|
259 | 259 | end |
|
260 | 260 | end |
|
261 | 261 | send :assign_attributes_without_project_and_tracker_first, attrs, *args |
|
262 | 262 | end |
|
263 | 263 | # Do not redefine alias chain on reload (see #4838) |
|
264 | 264 | alias_method_chain(:assign_attributes, :project_and_tracker_first) unless method_defined?(:assign_attributes_without_project_and_tracker_first) |
|
265 | 265 | |
|
266 | 266 | def estimated_hours=(h) |
|
267 | 267 | write_attribute :estimated_hours, (h.is_a?(String) ? h.to_hours : h) |
|
268 | 268 | end |
|
269 | 269 | |
|
270 | 270 | safe_attributes 'project_id', |
|
271 | 271 | :if => lambda {|issue, user| |
|
272 | 272 | if issue.new_record? |
|
273 | 273 | issue.copy? |
|
274 | 274 | elsif user.allowed_to?(:move_issues, issue.project) |
|
275 | 275 | projects = Issue.allowed_target_projects_on_move(user) |
|
276 | 276 | projects.include?(issue.project) && projects.size > 1 |
|
277 | 277 | end |
|
278 | 278 | } |
|
279 | 279 | |
|
280 | 280 | safe_attributes 'tracker_id', |
|
281 | 281 | 'status_id', |
|
282 | 282 | 'category_id', |
|
283 | 283 | 'assigned_to_id', |
|
284 | 284 | 'priority_id', |
|
285 | 285 | 'fixed_version_id', |
|
286 | 286 | 'subject', |
|
287 | 287 | 'description', |
|
288 | 288 | 'start_date', |
|
289 | 289 | 'due_date', |
|
290 | 290 | 'done_ratio', |
|
291 | 291 | 'estimated_hours', |
|
292 | 292 | 'custom_field_values', |
|
293 | 293 | 'custom_fields', |
|
294 | 294 | 'lock_version', |
|
295 | 295 | :if => lambda {|issue, user| issue.new_record? || user.allowed_to?(:edit_issues, issue.project) } |
|
296 | 296 | |
|
297 | 297 | safe_attributes 'status_id', |
|
298 | 298 | 'assigned_to_id', |
|
299 | 299 | 'fixed_version_id', |
|
300 | 300 | 'done_ratio', |
|
301 | 301 | 'lock_version', |
|
302 | 302 | :if => lambda {|issue, user| issue.new_statuses_allowed_to(user).any? } |
|
303 | 303 | |
|
304 | 304 | safe_attributes 'watcher_user_ids', |
|
305 | 305 | :if => lambda {|issue, user| issue.new_record? && user.allowed_to?(:add_issue_watchers, issue.project)} |
|
306 | 306 | |
|
307 | 307 | safe_attributes 'is_private', |
|
308 | 308 | :if => lambda {|issue, user| |
|
309 | 309 | user.allowed_to?(:set_issues_private, issue.project) || |
|
310 | 310 | (issue.author == user && user.allowed_to?(:set_own_issues_private, issue.project)) |
|
311 | 311 | } |
|
312 | 312 | |
|
313 | 313 | safe_attributes 'parent_issue_id', |
|
314 | 314 | :if => lambda {|issue, user| (issue.new_record? || user.allowed_to?(:edit_issues, issue.project)) && |
|
315 | 315 | user.allowed_to?(:manage_subtasks, issue.project)} |
|
316 | 316 | |
|
317 | 317 | # Safely sets attributes |
|
318 | 318 | # Should be called from controllers instead of #attributes= |
|
319 | 319 | # attr_accessible is too rough because we still want things like |
|
320 | 320 | # Issue.new(:project => foo) to work |
|
321 | 321 | def safe_attributes=(attrs, user=User.current) |
|
322 | 322 | return unless attrs.is_a?(Hash) |
|
323 | 323 | |
|
324 | 324 | # User can change issue attributes only if he has :edit permission or if a workflow transition is allowed |
|
325 | 325 | attrs = delete_unsafe_attributes(attrs, user) |
|
326 | 326 | return if attrs.empty? |
|
327 | 327 | |
|
328 | 328 | # Project and Tracker must be set before since new_statuses_allowed_to depends on it. |
|
329 | 329 | if p = attrs.delete('project_id') |
|
330 | 330 | if allowed_target_projects(user).collect(&:id).include?(p.to_i) |
|
331 | 331 | self.project_id = p |
|
332 | 332 | end |
|
333 | 333 | end |
|
334 | 334 | |
|
335 | 335 | if t = attrs.delete('tracker_id') |
|
336 | 336 | self.tracker_id = t |
|
337 | 337 | end |
|
338 | 338 | |
|
339 | 339 | if attrs['status_id'] |
|
340 | 340 | unless new_statuses_allowed_to(user).collect(&:id).include?(attrs['status_id'].to_i) |
|
341 | 341 | attrs.delete('status_id') |
|
342 | 342 | end |
|
343 | 343 | end |
|
344 | 344 | |
|
345 | 345 | unless leaf? |
|
346 | 346 | attrs.reject! {|k,v| %w(priority_id done_ratio start_date due_date estimated_hours).include?(k)} |
|
347 | 347 | end |
|
348 | 348 | |
|
349 | 349 | if attrs['parent_issue_id'].present? |
|
350 | 350 | attrs.delete('parent_issue_id') unless Issue.visible(user).exists?(attrs['parent_issue_id'].to_i) |
|
351 | 351 | end |
|
352 | 352 | |
|
353 | 353 | # mass-assignment security bypass |
|
354 | 354 | assign_attributes attrs, :without_protection => true |
|
355 | 355 | end |
|
356 | 356 | |
|
357 | 357 | def done_ratio |
|
358 | 358 | if Issue.use_status_for_done_ratio? && status && status.default_done_ratio |
|
359 | 359 | status.default_done_ratio |
|
360 | 360 | else |
|
361 | 361 | read_attribute(:done_ratio) |
|
362 | 362 | end |
|
363 | 363 | end |
|
364 | 364 | |
|
365 | 365 | def self.use_status_for_done_ratio? |
|
366 | 366 | Setting.issue_done_ratio == 'issue_status' |
|
367 | 367 | end |
|
368 | 368 | |
|
369 | 369 | def self.use_field_for_done_ratio? |
|
370 | 370 | Setting.issue_done_ratio == 'issue_field' |
|
371 | 371 | end |
|
372 | 372 | |
|
373 | 373 | def validate_issue |
|
374 | 374 | if self.due_date.nil? && @attributes['due_date'] && !@attributes['due_date'].empty? |
|
375 | 375 | errors.add :due_date, :not_a_date |
|
376 | 376 | end |
|
377 | 377 | |
|
378 | 378 | if self.due_date and self.start_date and self.due_date < self.start_date |
|
379 | 379 | errors.add :due_date, :greater_than_start_date |
|
380 | 380 | end |
|
381 | 381 | |
|
382 | 382 | if start_date && soonest_start && start_date < soonest_start |
|
383 | 383 | errors.add :start_date, :invalid |
|
384 | 384 | end |
|
385 | 385 | |
|
386 | 386 | if fixed_version |
|
387 | 387 | if !assignable_versions.include?(fixed_version) |
|
388 | 388 | errors.add :fixed_version_id, :inclusion |
|
389 | 389 | elsif reopened? && fixed_version.closed? |
|
390 | 390 | errors.add :base, I18n.t(:error_can_not_reopen_issue_on_closed_version) |
|
391 | 391 | end |
|
392 | 392 | end |
|
393 | 393 | |
|
394 | 394 | # Checks that the issue can not be added/moved to a disabled tracker |
|
395 | 395 | if project && (tracker_id_changed? || project_id_changed?) |
|
396 | 396 | unless project.trackers.include?(tracker) |
|
397 | 397 | errors.add :tracker_id, :inclusion |
|
398 | 398 | end |
|
399 | 399 | end |
|
400 | 400 | |
|
401 | 401 | # Checks parent issue assignment |
|
402 | 402 | if @parent_issue |
|
403 | 403 | if @parent_issue.project_id != project_id |
|
404 | 404 | errors.add :parent_issue_id, :not_same_project |
|
405 | 405 | elsif !new_record? |
|
406 | 406 | # moving an existing issue |
|
407 | 407 | if @parent_issue.root_id != root_id |
|
408 | 408 | # we can always move to another tree |
|
409 | 409 | elsif move_possible?(@parent_issue) |
|
410 | 410 | # move accepted inside tree |
|
411 | 411 | else |
|
412 | 412 | errors.add :parent_issue_id, :not_a_valid_parent |
|
413 | 413 | end |
|
414 | 414 | end |
|
415 | 415 | end |
|
416 | 416 | end |
|
417 | 417 | |
|
418 | 418 | # Set the done_ratio using the status if that setting is set. This will keep the done_ratios |
|
419 | 419 | # even if the user turns off the setting later |
|
420 | 420 | def update_done_ratio_from_issue_status |
|
421 | 421 | if Issue.use_status_for_done_ratio? && status && status.default_done_ratio |
|
422 | 422 | self.done_ratio = status.default_done_ratio |
|
423 | 423 | end |
|
424 | 424 | end |
|
425 | 425 | |
|
426 | 426 | def init_journal(user, notes = "") |
|
427 | 427 | @current_journal ||= Journal.new(:journalized => self, :user => user, :notes => notes) |
|
428 | 428 | if new_record? |
|
429 | 429 | @current_journal.notify = false |
|
430 | 430 | else |
|
431 | 431 | @attributes_before_change = attributes.dup |
|
432 | 432 | @custom_values_before_change = {} |
|
433 | 433 | self.custom_field_values.each {|c| @custom_values_before_change.store c.custom_field_id, c.value } |
|
434 | 434 | end |
|
435 | # Make sure updated_on is updated when adding a note. | |
|
436 | updated_on_will_change! | |
|
437 | 435 | @current_journal |
|
438 | 436 | end |
|
439 | 437 | |
|
440 | 438 | # Returns the id of the last journal or nil |
|
441 | 439 | def last_journal_id |
|
442 | 440 | if new_record? |
|
443 | 441 | nil |
|
444 | 442 | else |
|
445 | 443 | journals.first(:order => "#{Journal.table_name}.id DESC").try(:id) |
|
446 | 444 | end |
|
447 | 445 | end |
|
448 | 446 | |
|
449 | 447 | # Return true if the issue is closed, otherwise false |
|
450 | 448 | def closed? |
|
451 | 449 | self.status.is_closed? |
|
452 | 450 | end |
|
453 | 451 | |
|
454 | 452 | # Return true if the issue is being reopened |
|
455 | 453 | def reopened? |
|
456 | 454 | if !new_record? && status_id_changed? |
|
457 | 455 | status_was = IssueStatus.find_by_id(status_id_was) |
|
458 | 456 | status_new = IssueStatus.find_by_id(status_id) |
|
459 | 457 | if status_was && status_new && status_was.is_closed? && !status_new.is_closed? |
|
460 | 458 | return true |
|
461 | 459 | end |
|
462 | 460 | end |
|
463 | 461 | false |
|
464 | 462 | end |
|
465 | 463 | |
|
466 | 464 | # Return true if the issue is being closed |
|
467 | 465 | def closing? |
|
468 | 466 | if !new_record? && status_id_changed? |
|
469 | 467 | status_was = IssueStatus.find_by_id(status_id_was) |
|
470 | 468 | status_new = IssueStatus.find_by_id(status_id) |
|
471 | 469 | if status_was && status_new && !status_was.is_closed? && status_new.is_closed? |
|
472 | 470 | return true |
|
473 | 471 | end |
|
474 | 472 | end |
|
475 | 473 | false |
|
476 | 474 | end |
|
477 | 475 | |
|
478 | 476 | # Returns true if the issue is overdue |
|
479 | 477 | def overdue? |
|
480 | 478 | !due_date.nil? && (due_date < Date.today) && !status.is_closed? |
|
481 | 479 | end |
|
482 | 480 | |
|
483 | 481 | # Is the amount of work done less than it should for the due date |
|
484 | 482 | def behind_schedule? |
|
485 | 483 | return false if start_date.nil? || due_date.nil? |
|
486 | 484 | done_date = start_date + ((due_date - start_date+1)* done_ratio/100).floor |
|
487 | 485 | return done_date <= Date.today |
|
488 | 486 | end |
|
489 | 487 | |
|
490 | 488 | # Does this issue have children? |
|
491 | 489 | def children? |
|
492 | 490 | !leaf? |
|
493 | 491 | end |
|
494 | 492 | |
|
495 | 493 | # Users the issue can be assigned to |
|
496 | 494 | def assignable_users |
|
497 | 495 | users = project.assignable_users |
|
498 | 496 | users << author if author |
|
499 | 497 | users << assigned_to if assigned_to |
|
500 | 498 | users.uniq.sort |
|
501 | 499 | end |
|
502 | 500 | |
|
503 | 501 | # Versions that the issue can be assigned to |
|
504 | 502 | def assignable_versions |
|
505 | 503 | @assignable_versions ||= (project.shared_versions.open + [Version.find_by_id(fixed_version_id_was)]).compact.uniq.sort |
|
506 | 504 | end |
|
507 | 505 | |
|
508 | 506 | # Returns true if this issue is blocked by another issue that is still open |
|
509 | 507 | def blocked? |
|
510 | 508 | !relations_to.detect {|ir| ir.relation_type == 'blocks' && !ir.issue_from.closed?}.nil? |
|
511 | 509 | end |
|
512 | 510 | |
|
513 | 511 | # Returns an array of statuses that user is able to apply |
|
514 | 512 | def new_statuses_allowed_to(user=User.current, include_default=false) |
|
515 | 513 | if new_record? && @copied_from |
|
516 | 514 | [IssueStatus.default, @copied_from.status].compact.uniq.sort |
|
517 | 515 | else |
|
518 | 516 | initial_status = nil |
|
519 | 517 | if new_record? |
|
520 | 518 | initial_status = IssueStatus.default |
|
521 | 519 | elsif status_id_was |
|
522 | 520 | initial_status = IssueStatus.find_by_id(status_id_was) |
|
523 | 521 | end |
|
524 | 522 | initial_status ||= status |
|
525 | 523 | |
|
526 | 524 | statuses = initial_status.find_new_statuses_allowed_to( |
|
527 | 525 | user.admin ? Role.all : user.roles_for_project(project), |
|
528 | 526 | tracker, |
|
529 | 527 | author == user, |
|
530 | 528 | assigned_to_id_changed? ? assigned_to_id_was == user.id : assigned_to_id == user.id |
|
531 | 529 | ) |
|
532 | 530 | statuses << initial_status unless statuses.empty? |
|
533 | 531 | statuses << IssueStatus.default if include_default |
|
534 | 532 | statuses = statuses.compact.uniq.sort |
|
535 | 533 | blocked? ? statuses.reject {|s| s.is_closed?} : statuses |
|
536 | 534 | end |
|
537 | 535 | end |
|
538 | 536 | |
|
539 | 537 | def assigned_to_was |
|
540 | 538 | if assigned_to_id_changed? && assigned_to_id_was.present? |
|
541 | 539 | @assigned_to_was ||= User.find_by_id(assigned_to_id_was) |
|
542 | 540 | end |
|
543 | 541 | end |
|
544 | 542 | |
|
545 | 543 | # Returns the mail adresses of users that should be notified |
|
546 | 544 | def recipients |
|
547 | 545 | notified = [] |
|
548 | 546 | # Author and assignee are always notified unless they have been |
|
549 | 547 | # locked or don't want to be notified |
|
550 | 548 | notified << author if author |
|
551 | 549 | if assigned_to |
|
552 | 550 | notified += (assigned_to.is_a?(Group) ? assigned_to.users : [assigned_to]) |
|
553 | 551 | end |
|
554 | 552 | if assigned_to_was |
|
555 | 553 | notified += (assigned_to_was.is_a?(Group) ? assigned_to_was.users : [assigned_to_was]) |
|
556 | 554 | end |
|
557 | 555 | notified = notified.select {|u| u.active? && u.notify_about?(self)} |
|
558 | 556 | |
|
559 | 557 | notified += project.notified_users |
|
560 | 558 | notified.uniq! |
|
561 | 559 | # Remove users that can not view the issue |
|
562 | 560 | notified.reject! {|user| !visible?(user)} |
|
563 | 561 | notified.collect(&:mail) |
|
564 | 562 | end |
|
565 | 563 | |
|
566 | 564 | # Returns the number of hours spent on this issue |
|
567 | 565 | def spent_hours |
|
568 | 566 | @spent_hours ||= time_entries.sum(:hours) || 0 |
|
569 | 567 | end |
|
570 | 568 | |
|
571 | 569 | # Returns the total number of hours spent on this issue and its descendants |
|
572 | 570 | # |
|
573 | 571 | # Example: |
|
574 | 572 | # spent_hours => 0.0 |
|
575 | 573 | # spent_hours => 50.2 |
|
576 | 574 | def total_spent_hours |
|
577 | 575 | @total_spent_hours ||= self_and_descendants.sum("#{TimeEntry.table_name}.hours", |
|
578 | 576 | :joins => "LEFT JOIN #{TimeEntry.table_name} ON #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id").to_f || 0.0 |
|
579 | 577 | end |
|
580 | 578 | |
|
581 | 579 | def relations |
|
582 | 580 | @relations ||= (relations_from + relations_to).sort |
|
583 | 581 | end |
|
584 | 582 | |
|
585 | 583 | # Preloads relations for a collection of issues |
|
586 | 584 | def self.load_relations(issues) |
|
587 | 585 | if issues.any? |
|
588 | 586 | relations = IssueRelation.all(:conditions => ["issue_from_id IN (:ids) OR issue_to_id IN (:ids)", {:ids => issues.map(&:id)}]) |
|
589 | 587 | issues.each do |issue| |
|
590 | 588 | issue.instance_variable_set "@relations", relations.select {|r| r.issue_from_id == issue.id || r.issue_to_id == issue.id} |
|
591 | 589 | end |
|
592 | 590 | end |
|
593 | 591 | end |
|
594 | 592 | |
|
595 | 593 | # Preloads visible spent time for a collection of issues |
|
596 | 594 | def self.load_visible_spent_hours(issues, user=User.current) |
|
597 | 595 | if issues.any? |
|
598 | 596 | hours_by_issue_id = TimeEntry.visible(user).sum(:hours, :group => :issue_id) |
|
599 | 597 | issues.each do |issue| |
|
600 | 598 | issue.instance_variable_set "@spent_hours", (hours_by_issue_id[issue.id] || 0) |
|
601 | 599 | end |
|
602 | 600 | end |
|
603 | 601 | end |
|
604 | 602 | |
|
605 | 603 | # Finds an issue relation given its id. |
|
606 | 604 | def find_relation(relation_id) |
|
607 | 605 | IssueRelation.find(relation_id, :conditions => ["issue_to_id = ? OR issue_from_id = ?", id, id]) |
|
608 | 606 | end |
|
609 | 607 | |
|
610 | 608 | def all_dependent_issues(except=[]) |
|
611 | 609 | except << self |
|
612 | 610 | dependencies = [] |
|
613 | 611 | relations_from.each do |relation| |
|
614 | 612 | if relation.issue_to && !except.include?(relation.issue_to) |
|
615 | 613 | dependencies << relation.issue_to |
|
616 | 614 | dependencies += relation.issue_to.all_dependent_issues(except) |
|
617 | 615 | end |
|
618 | 616 | end |
|
619 | 617 | dependencies |
|
620 | 618 | end |
|
621 | 619 | |
|
622 | 620 | # Returns an array of issues that duplicate this one |
|
623 | 621 | def duplicates |
|
624 | 622 | relations_to.select {|r| r.relation_type == IssueRelation::TYPE_DUPLICATES}.collect {|r| r.issue_from} |
|
625 | 623 | end |
|
626 | 624 | |
|
627 | 625 | # Returns the due date or the target due date if any |
|
628 | 626 | # Used on gantt chart |
|
629 | 627 | def due_before |
|
630 | 628 | due_date || (fixed_version ? fixed_version.effective_date : nil) |
|
631 | 629 | end |
|
632 | 630 | |
|
633 | 631 | # Returns the time scheduled for this issue. |
|
634 | 632 | # |
|
635 | 633 | # Example: |
|
636 | 634 | # Start Date: 2/26/09, End Date: 3/04/09 |
|
637 | 635 | # duration => 6 |
|
638 | 636 | def duration |
|
639 | 637 | (start_date && due_date) ? due_date - start_date : 0 |
|
640 | 638 | end |
|
641 | 639 | |
|
642 | 640 | def soonest_start |
|
643 | 641 | @soonest_start ||= ( |
|
644 | 642 | relations_to.collect{|relation| relation.successor_soonest_start} + |
|
645 | 643 | ancestors.collect(&:soonest_start) |
|
646 | 644 | ).compact.max |
|
647 | 645 | end |
|
648 | 646 | |
|
649 | 647 | def reschedule_after(date) |
|
650 | 648 | return if date.nil? |
|
651 | 649 | if leaf? |
|
652 | 650 | if start_date.nil? || start_date < date |
|
653 | 651 | self.start_date, self.due_date = date, date + duration |
|
654 | 652 | begin |
|
655 | 653 | save |
|
656 | 654 | rescue ActiveRecord::StaleObjectError |
|
657 | 655 | reload |
|
658 | 656 | self.start_date, self.due_date = date, date + duration |
|
659 | 657 | save |
|
660 | 658 | end |
|
661 | 659 | end |
|
662 | 660 | else |
|
663 | 661 | leaves.each do |leaf| |
|
664 | 662 | leaf.reschedule_after(date) |
|
665 | 663 | end |
|
666 | 664 | end |
|
667 | 665 | end |
|
668 | 666 | |
|
669 | 667 | def <=>(issue) |
|
670 | 668 | if issue.nil? |
|
671 | 669 | -1 |
|
672 | 670 | elsif root_id != issue.root_id |
|
673 | 671 | (root_id || 0) <=> (issue.root_id || 0) |
|
674 | 672 | else |
|
675 | 673 | (lft || 0) <=> (issue.lft || 0) |
|
676 | 674 | end |
|
677 | 675 | end |
|
678 | 676 | |
|
679 | 677 | def to_s |
|
680 | 678 | "#{tracker} ##{id}: #{subject}" |
|
681 | 679 | end |
|
682 | 680 | |
|
683 | 681 | # Returns a string of css classes that apply to the issue |
|
684 | 682 | def css_classes |
|
685 | 683 | s = "issue status-#{status.position} priority-#{priority.position}" |
|
686 | 684 | s << ' closed' if closed? |
|
687 | 685 | s << ' overdue' if overdue? |
|
688 | 686 | s << ' child' if child? |
|
689 | 687 | s << ' parent' unless leaf? |
|
690 | 688 | s << ' private' if is_private? |
|
691 | 689 | s << ' created-by-me' if User.current.logged? && author_id == User.current.id |
|
692 | 690 | s << ' assigned-to-me' if User.current.logged? && assigned_to_id == User.current.id |
|
693 | 691 | s |
|
694 | 692 | end |
|
695 | 693 | |
|
696 | 694 | # Saves an issue and a time_entry from the parameters |
|
697 | 695 | def save_issue_with_child_records(params, existing_time_entry=nil) |
|
698 | 696 | Issue.transaction do |
|
699 | 697 | if params[:time_entry] && (params[:time_entry][:hours].present? || params[:time_entry][:comments].present?) && User.current.allowed_to?(:log_time, project) |
|
700 | 698 | @time_entry = existing_time_entry || TimeEntry.new |
|
701 | 699 | @time_entry.project = project |
|
702 | 700 | @time_entry.issue = self |
|
703 | 701 | @time_entry.user = User.current |
|
704 | 702 | @time_entry.spent_on = User.current.today |
|
705 | 703 | @time_entry.attributes = params[:time_entry] |
|
706 | 704 | self.time_entries << @time_entry |
|
707 | 705 | end |
|
708 | 706 | |
|
709 | 707 | # TODO: Rename hook |
|
710 | 708 | Redmine::Hook.call_hook(:controller_issues_edit_before_save, { :params => params, :issue => self, :time_entry => @time_entry, :journal => @current_journal}) |
|
711 | 709 | if save |
|
712 | 710 | # TODO: Rename hook |
|
713 | 711 | Redmine::Hook.call_hook(:controller_issues_edit_after_save, { :params => params, :issue => self, :time_entry => @time_entry, :journal => @current_journal}) |
|
714 | 712 | else |
|
715 | 713 | raise ActiveRecord::Rollback |
|
716 | 714 | end |
|
717 | 715 | end |
|
718 | 716 | end |
|
719 | 717 | |
|
720 | 718 | # Unassigns issues from +version+ if it's no longer shared with issue's project |
|
721 | 719 | def self.update_versions_from_sharing_change(version) |
|
722 | 720 | # Update issues assigned to the version |
|
723 | 721 | update_versions(["#{Issue.table_name}.fixed_version_id = ?", version.id]) |
|
724 | 722 | end |
|
725 | 723 | |
|
726 | 724 | # Unassigns issues from versions that are no longer shared |
|
727 | 725 | # after +project+ was moved |
|
728 | 726 | def self.update_versions_from_hierarchy_change(project) |
|
729 | 727 | moved_project_ids = project.self_and_descendants.reload.collect(&:id) |
|
730 | 728 | # Update issues of the moved projects and issues assigned to a version of a moved project |
|
731 | 729 | Issue.update_versions(["#{Version.table_name}.project_id IN (?) OR #{Issue.table_name}.project_id IN (?)", moved_project_ids, moved_project_ids]) |
|
732 | 730 | end |
|
733 | 731 | |
|
734 | 732 | def parent_issue_id=(arg) |
|
735 | 733 | parent_issue_id = arg.blank? ? nil : arg.to_i |
|
736 | 734 | if parent_issue_id && @parent_issue = Issue.find_by_id(parent_issue_id) |
|
737 | 735 | @parent_issue.id |
|
738 | 736 | else |
|
739 | 737 | @parent_issue = nil |
|
740 | 738 | nil |
|
741 | 739 | end |
|
742 | 740 | end |
|
743 | 741 | |
|
744 | 742 | def parent_issue_id |
|
745 | 743 | if instance_variable_defined? :@parent_issue |
|
746 | 744 | @parent_issue.nil? ? nil : @parent_issue.id |
|
747 | 745 | else |
|
748 | 746 | parent_id |
|
749 | 747 | end |
|
750 | 748 | end |
|
751 | 749 | |
|
752 | 750 | # Extracted from the ReportsController. |
|
753 | 751 | def self.by_tracker(project) |
|
754 | 752 | count_and_group_by(:project => project, |
|
755 | 753 | :field => 'tracker_id', |
|
756 | 754 | :joins => Tracker.table_name) |
|
757 | 755 | end |
|
758 | 756 | |
|
759 | 757 | def self.by_version(project) |
|
760 | 758 | count_and_group_by(:project => project, |
|
761 | 759 | :field => 'fixed_version_id', |
|
762 | 760 | :joins => Version.table_name) |
|
763 | 761 | end |
|
764 | 762 | |
|
765 | 763 | def self.by_priority(project) |
|
766 | 764 | count_and_group_by(:project => project, |
|
767 | 765 | :field => 'priority_id', |
|
768 | 766 | :joins => IssuePriority.table_name) |
|
769 | 767 | end |
|
770 | 768 | |
|
771 | 769 | def self.by_category(project) |
|
772 | 770 | count_and_group_by(:project => project, |
|
773 | 771 | :field => 'category_id', |
|
774 | 772 | :joins => IssueCategory.table_name) |
|
775 | 773 | end |
|
776 | 774 | |
|
777 | 775 | def self.by_assigned_to(project) |
|
778 | 776 | count_and_group_by(:project => project, |
|
779 | 777 | :field => 'assigned_to_id', |
|
780 | 778 | :joins => User.table_name) |
|
781 | 779 | end |
|
782 | 780 | |
|
783 | 781 | def self.by_author(project) |
|
784 | 782 | count_and_group_by(:project => project, |
|
785 | 783 | :field => 'author_id', |
|
786 | 784 | :joins => User.table_name) |
|
787 | 785 | end |
|
788 | 786 | |
|
789 | 787 | def self.by_subproject(project) |
|
790 | 788 | ActiveRecord::Base.connection.select_all("select s.id as status_id, |
|
791 | 789 | s.is_closed as closed, |
|
792 | 790 | #{Issue.table_name}.project_id as project_id, |
|
793 | 791 | count(#{Issue.table_name}.id) as total |
|
794 | 792 | from |
|
795 | 793 | #{Issue.table_name}, #{Project.table_name}, #{IssueStatus.table_name} s |
|
796 | 794 | where |
|
797 | 795 | #{Issue.table_name}.status_id=s.id |
|
798 | 796 | and #{Issue.table_name}.project_id = #{Project.table_name}.id |
|
799 | 797 | and #{visible_condition(User.current, :project => project, :with_subprojects => true)} |
|
800 | 798 | and #{Issue.table_name}.project_id <> #{project.id} |
|
801 | 799 | group by s.id, s.is_closed, #{Issue.table_name}.project_id") if project.descendants.active.any? |
|
802 | 800 | end |
|
803 | 801 | # End ReportsController extraction |
|
804 | 802 | |
|
805 | 803 | # Returns an array of projects that user can assign the issue to |
|
806 | 804 | def allowed_target_projects(user=User.current) |
|
807 | 805 | if new_record? |
|
808 | 806 | Project.all(:conditions => Project.allowed_to_condition(user, :add_issues)) |
|
809 | 807 | else |
|
810 | 808 | self.class.allowed_target_projects_on_move(user) |
|
811 | 809 | end |
|
812 | 810 | end |
|
813 | 811 | |
|
814 | 812 | # Returns an array of projects that user can move issues to |
|
815 | 813 | def self.allowed_target_projects_on_move(user=User.current) |
|
816 | 814 | Project.all(:conditions => Project.allowed_to_condition(user, :move_issues)) |
|
817 | 815 | end |
|
818 | 816 | |
|
819 | 817 | private |
|
820 | 818 | |
|
821 | 819 | def after_project_change |
|
822 | 820 | # Update project_id on related time entries |
|
823 | 821 | TimeEntry.update_all(["project_id = ?", project_id], {:issue_id => id}) |
|
824 | 822 | |
|
825 | 823 | # Delete issue relations |
|
826 | 824 | unless Setting.cross_project_issue_relations? |
|
827 | 825 | relations_from.clear |
|
828 | 826 | relations_to.clear |
|
829 | 827 | end |
|
830 | 828 | |
|
831 | 829 | # Move subtasks |
|
832 | 830 | children.each do |child| |
|
833 | 831 | # Change project and keep project |
|
834 | 832 | child.send :project=, project, true |
|
835 | 833 | unless child.save |
|
836 | 834 | raise ActiveRecord::Rollback |
|
837 | 835 | end |
|
838 | 836 | end |
|
839 | 837 | end |
|
840 | 838 | |
|
841 | 839 | def update_nested_set_attributes |
|
842 | 840 | if root_id.nil? |
|
843 | 841 | # issue was just created |
|
844 | 842 | self.root_id = (@parent_issue.nil? ? id : @parent_issue.root_id) |
|
845 | 843 | set_default_left_and_right |
|
846 | 844 | Issue.update_all("root_id = #{root_id}, lft = #{lft}, rgt = #{rgt}", ["id = ?", id]) |
|
847 | 845 | if @parent_issue |
|
848 | 846 | move_to_child_of(@parent_issue) |
|
849 | 847 | end |
|
850 | 848 | reload |
|
851 | 849 | elsif parent_issue_id != parent_id |
|
852 | 850 | former_parent_id = parent_id |
|
853 | 851 | # moving an existing issue |
|
854 | 852 | if @parent_issue && @parent_issue.root_id == root_id |
|
855 | 853 | # inside the same tree |
|
856 | 854 | move_to_child_of(@parent_issue) |
|
857 | 855 | else |
|
858 | 856 | # to another tree |
|
859 | 857 | unless root? |
|
860 | 858 | move_to_right_of(root) |
|
861 | 859 | reload |
|
862 | 860 | end |
|
863 | 861 | old_root_id = root_id |
|
864 | 862 | self.root_id = (@parent_issue.nil? ? id : @parent_issue.root_id ) |
|
865 | 863 | target_maxright = nested_set_scope.maximum(right_column_name) || 0 |
|
866 | 864 | offset = target_maxright + 1 - lft |
|
867 | 865 | Issue.update_all("root_id = #{root_id}, lft = lft + #{offset}, rgt = rgt + #{offset}", |
|
868 | 866 | ["root_id = ? AND lft >= ? AND rgt <= ? ", old_root_id, lft, rgt]) |
|
869 | 867 | self[left_column_name] = lft + offset |
|
870 | 868 | self[right_column_name] = rgt + offset |
|
871 | 869 | if @parent_issue |
|
872 | 870 | move_to_child_of(@parent_issue) |
|
873 | 871 | end |
|
874 | 872 | end |
|
875 | 873 | reload |
|
876 | 874 | # delete invalid relations of all descendants |
|
877 | 875 | self_and_descendants.each do |issue| |
|
878 | 876 | issue.relations.each do |relation| |
|
879 | 877 | relation.destroy unless relation.valid? |
|
880 | 878 | end |
|
881 | 879 | end |
|
882 | 880 | # update former parent |
|
883 | 881 | recalculate_attributes_for(former_parent_id) if former_parent_id |
|
884 | 882 | end |
|
885 | 883 | remove_instance_variable(:@parent_issue) if instance_variable_defined?(:@parent_issue) |
|
886 | 884 | end |
|
887 | 885 | |
|
888 | 886 | def update_parent_attributes |
|
889 | 887 | recalculate_attributes_for(parent_id) if parent_id |
|
890 | 888 | end |
|
891 | 889 | |
|
892 | 890 | def recalculate_attributes_for(issue_id) |
|
893 | 891 | if issue_id && p = Issue.find_by_id(issue_id) |
|
894 | 892 | # priority = highest priority of children |
|
895 | 893 | if priority_position = p.children.maximum("#{IssuePriority.table_name}.position", :joins => :priority) |
|
896 | 894 | p.priority = IssuePriority.find_by_position(priority_position) |
|
897 | 895 | end |
|
898 | 896 | |
|
899 | 897 | # start/due dates = lowest/highest dates of children |
|
900 | 898 | p.start_date = p.children.minimum(:start_date) |
|
901 | 899 | p.due_date = p.children.maximum(:due_date) |
|
902 | 900 | if p.start_date && p.due_date && p.due_date < p.start_date |
|
903 | 901 | p.start_date, p.due_date = p.due_date, p.start_date |
|
904 | 902 | end |
|
905 | 903 | |
|
906 | 904 | # done ratio = weighted average ratio of leaves |
|
907 | 905 | unless Issue.use_status_for_done_ratio? && p.status && p.status.default_done_ratio |
|
908 | 906 | leaves_count = p.leaves.count |
|
909 | 907 | if leaves_count > 0 |
|
910 | 908 | average = p.leaves.average(:estimated_hours).to_f |
|
911 | 909 | if average == 0 |
|
912 | 910 | average = 1 |
|
913 | 911 | end |
|
914 | 912 | done = p.leaves.sum("COALESCE(estimated_hours, #{average}) * (CASE WHEN is_closed = #{connection.quoted_true} THEN 100 ELSE COALESCE(done_ratio, 0) END)", :joins => :status).to_f |
|
915 | 913 | progress = done / (average * leaves_count) |
|
916 | 914 | p.done_ratio = progress.round |
|
917 | 915 | end |
|
918 | 916 | end |
|
919 | 917 | |
|
920 | 918 | # estimate = sum of leaves estimates |
|
921 | 919 | p.estimated_hours = p.leaves.sum(:estimated_hours).to_f |
|
922 | 920 | p.estimated_hours = nil if p.estimated_hours == 0.0 |
|
923 | 921 | |
|
924 | 922 | # ancestors will be recursively updated |
|
925 | 923 | p.save(:validate => false) |
|
926 | 924 | end |
|
927 | 925 | end |
|
928 | 926 | |
|
929 | 927 | # Update issues so their versions are not pointing to a |
|
930 | 928 | # fixed_version that is not shared with the issue's project |
|
931 | 929 | def self.update_versions(conditions=nil) |
|
932 | 930 | # Only need to update issues with a fixed_version from |
|
933 | 931 | # a different project and that is not systemwide shared |
|
934 | 932 | Issue.scoped(:conditions => conditions).all( |
|
935 | 933 | :conditions => "#{Issue.table_name}.fixed_version_id IS NOT NULL" + |
|
936 | 934 | " AND #{Issue.table_name}.project_id <> #{Version.table_name}.project_id" + |
|
937 | 935 | " AND #{Version.table_name}.sharing <> 'system'", |
|
938 | 936 | :include => [:project, :fixed_version] |
|
939 | 937 | ).each do |issue| |
|
940 | 938 | next if issue.project.nil? || issue.fixed_version.nil? |
|
941 | 939 | unless issue.project.shared_versions.include?(issue.fixed_version) |
|
942 | 940 | issue.init_journal(User.current) |
|
943 | 941 | issue.fixed_version = nil |
|
944 | 942 | issue.save |
|
945 | 943 | end |
|
946 | 944 | end |
|
947 | 945 | end |
|
948 | 946 | |
|
949 | 947 | # Callback on attachment deletion |
|
950 | 948 | def attachment_added(obj) |
|
951 | 949 | if @current_journal && !obj.new_record? |
|
952 | 950 | @current_journal.details << JournalDetail.new(:property => 'attachment', :prop_key => obj.id, :value => obj.filename) |
|
953 | 951 | end |
|
954 | 952 | end |
|
955 | 953 | |
|
956 | 954 | # Callback on attachment deletion |
|
957 | 955 | def attachment_removed(obj) |
|
958 | 956 | if @current_journal && !obj.new_record? |
|
959 | 957 | @current_journal.details << JournalDetail.new(:property => 'attachment', :prop_key => obj.id, :old_value => obj.filename) |
|
960 | 958 | @current_journal.save |
|
961 | 959 | end |
|
962 | 960 | end |
|
963 | 961 | |
|
964 | 962 | # Default assignment based on category |
|
965 | 963 | def default_assign |
|
966 | 964 | if assigned_to.nil? && category && category.assigned_to |
|
967 | 965 | self.assigned_to = category.assigned_to |
|
968 | 966 | end |
|
969 | 967 | end |
|
970 | 968 | |
|
971 | 969 | # Updates start/due dates of following issues |
|
972 | 970 | def reschedule_following_issues |
|
973 | 971 | if start_date_changed? || due_date_changed? |
|
974 | 972 | relations_from.each do |relation| |
|
975 | 973 | relation.set_issue_to_dates |
|
976 | 974 | end |
|
977 | 975 | end |
|
978 | 976 | end |
|
979 | 977 | |
|
980 | 978 | # Closes duplicates if the issue is being closed |
|
981 | 979 | def close_duplicates |
|
982 | 980 | if closing? |
|
983 | 981 | duplicates.each do |duplicate| |
|
984 | 982 | # Reload is need in case the duplicate was updated by a previous duplicate |
|
985 | 983 | duplicate.reload |
|
986 | 984 | # Don't re-close it if it's already closed |
|
987 | 985 | next if duplicate.closed? |
|
988 | 986 | # Same user and notes |
|
989 | 987 | if @current_journal |
|
990 | 988 | duplicate.init_journal(@current_journal.user, @current_journal.notes) |
|
991 | 989 | end |
|
992 | 990 | duplicate.update_attribute :status, self.status |
|
993 | 991 | end |
|
994 | 992 | end |
|
995 | 993 | end |
|
996 | 994 | |
|
995 | # Make sure updated_on is updated when adding a note | |
|
996 | def force_updated_on_change | |
|
997 | if @current_journal | |
|
998 | self.updated_on = current_time_from_proper_timezone | |
|
999 | end | |
|
1000 | end | |
|
1001 | ||
|
997 | 1002 | # Saves the changes in a Journal |
|
998 | 1003 | # Called after_save |
|
999 | 1004 | def create_journal |
|
1000 | 1005 | if @current_journal |
|
1001 | 1006 | # attributes changes |
|
1002 | 1007 | if @attributes_before_change |
|
1003 | 1008 | (Issue.column_names - %w(id root_id lft rgt lock_version created_on updated_on)).each {|c| |
|
1004 | 1009 | before = @attributes_before_change[c] |
|
1005 | 1010 | after = send(c) |
|
1006 | 1011 | next if before == after || (before.blank? && after.blank?) |
|
1007 | 1012 | @current_journal.details << JournalDetail.new(:property => 'attr', |
|
1008 | 1013 | :prop_key => c, |
|
1009 | 1014 | :old_value => before, |
|
1010 | 1015 | :value => after) |
|
1011 | 1016 | } |
|
1012 | 1017 | end |
|
1013 | 1018 | if @custom_values_before_change |
|
1014 | 1019 | # custom fields changes |
|
1015 | 1020 | custom_field_values.each {|c| |
|
1016 | 1021 | before = @custom_values_before_change[c.custom_field_id] |
|
1017 | 1022 | after = c.value |
|
1018 | 1023 | next if before == after || (before.blank? && after.blank?) |
|
1019 | 1024 | |
|
1020 | 1025 | if before.is_a?(Array) || after.is_a?(Array) |
|
1021 | 1026 | before = [before] unless before.is_a?(Array) |
|
1022 | 1027 | after = [after] unless after.is_a?(Array) |
|
1023 | 1028 | |
|
1024 | 1029 | # values removed |
|
1025 | 1030 | (before - after).reject(&:blank?).each do |value| |
|
1026 | 1031 | @current_journal.details << JournalDetail.new(:property => 'cf', |
|
1027 | 1032 | :prop_key => c.custom_field_id, |
|
1028 | 1033 | :old_value => value, |
|
1029 | 1034 | :value => nil) |
|
1030 | 1035 | end |
|
1031 | 1036 | # values added |
|
1032 | 1037 | (after - before).reject(&:blank?).each do |value| |
|
1033 | 1038 | @current_journal.details << JournalDetail.new(:property => 'cf', |
|
1034 | 1039 | :prop_key => c.custom_field_id, |
|
1035 | 1040 | :old_value => nil, |
|
1036 | 1041 | :value => value) |
|
1037 | 1042 | end |
|
1038 | 1043 | else |
|
1039 | 1044 | @current_journal.details << JournalDetail.new(:property => 'cf', |
|
1040 | 1045 | :prop_key => c.custom_field_id, |
|
1041 | 1046 | :old_value => before, |
|
1042 | 1047 | :value => after) |
|
1043 | 1048 | end |
|
1044 | 1049 | } |
|
1045 | 1050 | end |
|
1046 | 1051 | @current_journal.save |
|
1047 | 1052 | # reset current journal |
|
1048 | 1053 | init_journal @current_journal.user, @current_journal.notes |
|
1049 | 1054 | end |
|
1050 | 1055 | end |
|
1051 | 1056 | |
|
1052 | 1057 | # Query generator for selecting groups of issue counts for a project |
|
1053 | 1058 | # based on specific criteria |
|
1054 | 1059 | # |
|
1055 | 1060 | # Options |
|
1056 | 1061 | # * project - Project to search in. |
|
1057 | 1062 | # * field - String. Issue field to key off of in the grouping. |
|
1058 | 1063 | # * joins - String. The table name to join against. |
|
1059 | 1064 | def self.count_and_group_by(options) |
|
1060 | 1065 | project = options.delete(:project) |
|
1061 | 1066 | select_field = options.delete(:field) |
|
1062 | 1067 | joins = options.delete(:joins) |
|
1063 | 1068 | |
|
1064 | 1069 | where = "#{Issue.table_name}.#{select_field}=j.id" |
|
1065 | 1070 | |
|
1066 | 1071 | ActiveRecord::Base.connection.select_all("select s.id as status_id, |
|
1067 | 1072 | s.is_closed as closed, |
|
1068 | 1073 | j.id as #{select_field}, |
|
1069 | 1074 | count(#{Issue.table_name}.id) as total |
|
1070 | 1075 | from |
|
1071 | 1076 | #{Issue.table_name}, #{Project.table_name}, #{IssueStatus.table_name} s, #{joins} j |
|
1072 | 1077 | where |
|
1073 | 1078 | #{Issue.table_name}.status_id=s.id |
|
1074 | 1079 | and #{where} |
|
1075 | 1080 | and #{Issue.table_name}.project_id=#{Project.table_name}.id |
|
1076 | 1081 | and #{visible_condition(User.current, :project => project)} |
|
1077 | 1082 | group by s.id, s.is_closed, j.id") |
|
1078 | 1083 | end |
|
1079 | 1084 | end |
@@ -1,1267 +1,1280 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2012 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | require File.expand_path('../../test_helper', __FILE__) |
|
19 | 19 | |
|
20 | 20 | class IssueTest < ActiveSupport::TestCase |
|
21 | 21 | fixtures :projects, :users, :members, :member_roles, :roles, |
|
22 | 22 | :groups_users, |
|
23 | 23 | :trackers, :projects_trackers, |
|
24 | 24 | :enabled_modules, |
|
25 | 25 | :versions, |
|
26 | 26 | :issue_statuses, :issue_categories, :issue_relations, :workflows, |
|
27 | 27 | :enumerations, |
|
28 | 28 | :issues, |
|
29 | 29 | :custom_fields, :custom_fields_projects, :custom_fields_trackers, :custom_values, |
|
30 | 30 | :time_entries |
|
31 | 31 | |
|
32 | 32 | include Redmine::I18n |
|
33 | 33 | |
|
34 | 34 | def test_create |
|
35 | 35 | issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3, |
|
36 | 36 | :status_id => 1, :priority => IssuePriority.all.first, |
|
37 | 37 | :subject => 'test_create', |
|
38 | 38 | :description => 'IssueTest#test_create', :estimated_hours => '1:30') |
|
39 | 39 | assert issue.save |
|
40 | 40 | issue.reload |
|
41 | 41 | assert_equal 1.5, issue.estimated_hours |
|
42 | 42 | end |
|
43 | 43 | |
|
44 | 44 | def test_create_minimal |
|
45 | 45 | issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3, |
|
46 | 46 | :status_id => 1, :priority => IssuePriority.all.first, |
|
47 | 47 | :subject => 'test_create') |
|
48 | 48 | assert issue.save |
|
49 | 49 | assert issue.description.nil? |
|
50 | 50 | end |
|
51 | 51 | |
|
52 | 52 | def test_create_with_required_custom_field |
|
53 | 53 | set_language_if_valid 'en' |
|
54 | 54 | field = IssueCustomField.find_by_name('Database') |
|
55 | 55 | field.update_attribute(:is_required, true) |
|
56 | 56 | |
|
57 | 57 | issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, |
|
58 | 58 | :status_id => 1, :subject => 'test_create', |
|
59 | 59 | :description => 'IssueTest#test_create_with_required_custom_field') |
|
60 | 60 | assert issue.available_custom_fields.include?(field) |
|
61 | 61 | # No value for the custom field |
|
62 | 62 | assert !issue.save |
|
63 | 63 | assert_equal ["Database can't be blank"], issue.errors.full_messages |
|
64 | 64 | # Blank value |
|
65 | 65 | issue.custom_field_values = { field.id => '' } |
|
66 | 66 | assert !issue.save |
|
67 | 67 | assert_equal ["Database can't be blank"], issue.errors.full_messages |
|
68 | 68 | # Invalid value |
|
69 | 69 | issue.custom_field_values = { field.id => 'SQLServer' } |
|
70 | 70 | assert !issue.save |
|
71 | 71 | assert_equal ["Database is not included in the list"], issue.errors.full_messages |
|
72 | 72 | # Valid value |
|
73 | 73 | issue.custom_field_values = { field.id => 'PostgreSQL' } |
|
74 | 74 | assert issue.save |
|
75 | 75 | issue.reload |
|
76 | 76 | assert_equal 'PostgreSQL', issue.custom_value_for(field).value |
|
77 | 77 | end |
|
78 | 78 | |
|
79 | 79 | def test_create_with_group_assignment |
|
80 | 80 | with_settings :issue_group_assignment => '1' do |
|
81 | 81 | assert Issue.new(:project_id => 2, :tracker_id => 1, :author_id => 1, |
|
82 | 82 | :subject => 'Group assignment', |
|
83 | 83 | :assigned_to_id => 11).save |
|
84 | 84 | issue = Issue.first(:order => 'id DESC') |
|
85 | 85 | assert_kind_of Group, issue.assigned_to |
|
86 | 86 | assert_equal Group.find(11), issue.assigned_to |
|
87 | 87 | end |
|
88 | 88 | end |
|
89 | 89 | |
|
90 | 90 | def assert_visibility_match(user, issues) |
|
91 | 91 | assert_equal issues.collect(&:id).sort, Issue.all.select {|issue| issue.visible?(user)}.collect(&:id).sort |
|
92 | 92 | end |
|
93 | 93 | |
|
94 | 94 | def test_visible_scope_for_anonymous |
|
95 | 95 | # Anonymous user should see issues of public projects only |
|
96 | 96 | issues = Issue.visible(User.anonymous).all |
|
97 | 97 | assert issues.any? |
|
98 | 98 | assert_nil issues.detect {|issue| !issue.project.is_public?} |
|
99 | 99 | assert_nil issues.detect {|issue| issue.is_private?} |
|
100 | 100 | assert_visibility_match User.anonymous, issues |
|
101 | 101 | end |
|
102 | 102 | |
|
103 | 103 | def test_visible_scope_for_anonymous_with_own_issues_visibility |
|
104 | 104 | Role.anonymous.update_attribute :issues_visibility, 'own' |
|
105 | 105 | Issue.create!(:project_id => 1, :tracker_id => 1, |
|
106 | 106 | :author_id => User.anonymous.id, |
|
107 | 107 | :subject => 'Issue by anonymous') |
|
108 | 108 | |
|
109 | 109 | issues = Issue.visible(User.anonymous).all |
|
110 | 110 | assert issues.any? |
|
111 | 111 | assert_nil issues.detect {|issue| issue.author != User.anonymous} |
|
112 | 112 | assert_visibility_match User.anonymous, issues |
|
113 | 113 | end |
|
114 | 114 | |
|
115 | 115 | def test_visible_scope_for_anonymous_without_view_issues_permissions |
|
116 | 116 | # Anonymous user should not see issues without permission |
|
117 | 117 | Role.anonymous.remove_permission!(:view_issues) |
|
118 | 118 | issues = Issue.visible(User.anonymous).all |
|
119 | 119 | assert issues.empty? |
|
120 | 120 | assert_visibility_match User.anonymous, issues |
|
121 | 121 | end |
|
122 | 122 | |
|
123 | 123 | def test_visible_scope_for_non_member |
|
124 | 124 | user = User.find(9) |
|
125 | 125 | assert user.projects.empty? |
|
126 | 126 | # Non member user should see issues of public projects only |
|
127 | 127 | issues = Issue.visible(user).all |
|
128 | 128 | assert issues.any? |
|
129 | 129 | assert_nil issues.detect {|issue| !issue.project.is_public?} |
|
130 | 130 | assert_nil issues.detect {|issue| issue.is_private?} |
|
131 | 131 | assert_visibility_match user, issues |
|
132 | 132 | end |
|
133 | 133 | |
|
134 | 134 | def test_visible_scope_for_non_member_with_own_issues_visibility |
|
135 | 135 | Role.non_member.update_attribute :issues_visibility, 'own' |
|
136 | 136 | Issue.create!(:project_id => 1, :tracker_id => 1, :author_id => 9, :subject => 'Issue by non member') |
|
137 | 137 | user = User.find(9) |
|
138 | 138 | |
|
139 | 139 | issues = Issue.visible(user).all |
|
140 | 140 | assert issues.any? |
|
141 | 141 | assert_nil issues.detect {|issue| issue.author != user} |
|
142 | 142 | assert_visibility_match user, issues |
|
143 | 143 | end |
|
144 | 144 | |
|
145 | 145 | def test_visible_scope_for_non_member_without_view_issues_permissions |
|
146 | 146 | # Non member user should not see issues without permission |
|
147 | 147 | Role.non_member.remove_permission!(:view_issues) |
|
148 | 148 | user = User.find(9) |
|
149 | 149 | assert user.projects.empty? |
|
150 | 150 | issues = Issue.visible(user).all |
|
151 | 151 | assert issues.empty? |
|
152 | 152 | assert_visibility_match user, issues |
|
153 | 153 | end |
|
154 | 154 | |
|
155 | 155 | def test_visible_scope_for_member |
|
156 | 156 | user = User.find(9) |
|
157 | 157 | # User should see issues of projects for which he has view_issues permissions only |
|
158 | 158 | Role.non_member.remove_permission!(:view_issues) |
|
159 | 159 | Member.create!(:principal => user, :project_id => 3, :role_ids => [2]) |
|
160 | 160 | issues = Issue.visible(user).all |
|
161 | 161 | assert issues.any? |
|
162 | 162 | assert_nil issues.detect {|issue| issue.project_id != 3} |
|
163 | 163 | assert_nil issues.detect {|issue| issue.is_private?} |
|
164 | 164 | assert_visibility_match user, issues |
|
165 | 165 | end |
|
166 | 166 | |
|
167 | 167 | def test_visible_scope_for_member_with_groups_should_return_assigned_issues |
|
168 | 168 | user = User.find(8) |
|
169 | 169 | assert user.groups.any? |
|
170 | 170 | Member.create!(:principal => user.groups.first, :project_id => 1, :role_ids => [2]) |
|
171 | 171 | Role.non_member.remove_permission!(:view_issues) |
|
172 | 172 | |
|
173 | 173 | issue = Issue.create(:project_id => 1, :tracker_id => 1, :author_id => 3, |
|
174 | 174 | :status_id => 1, :priority => IssuePriority.all.first, |
|
175 | 175 | :subject => 'Assignment test', |
|
176 | 176 | :assigned_to => user.groups.first, |
|
177 | 177 | :is_private => true) |
|
178 | 178 | |
|
179 | 179 | Role.find(2).update_attribute :issues_visibility, 'default' |
|
180 | 180 | issues = Issue.visible(User.find(8)).all |
|
181 | 181 | assert issues.any? |
|
182 | 182 | assert issues.include?(issue) |
|
183 | 183 | |
|
184 | 184 | Role.find(2).update_attribute :issues_visibility, 'own' |
|
185 | 185 | issues = Issue.visible(User.find(8)).all |
|
186 | 186 | assert issues.any? |
|
187 | 187 | assert issues.include?(issue) |
|
188 | 188 | end |
|
189 | 189 | |
|
190 | 190 | def test_visible_scope_for_admin |
|
191 | 191 | user = User.find(1) |
|
192 | 192 | user.members.each(&:destroy) |
|
193 | 193 | assert user.projects.empty? |
|
194 | 194 | issues = Issue.visible(user).all |
|
195 | 195 | assert issues.any? |
|
196 | 196 | # Admin should see issues on private projects that he does not belong to |
|
197 | 197 | assert issues.detect {|issue| !issue.project.is_public?} |
|
198 | 198 | # Admin should see private issues of other users |
|
199 | 199 | assert issues.detect {|issue| issue.is_private? && issue.author != user} |
|
200 | 200 | assert_visibility_match user, issues |
|
201 | 201 | end |
|
202 | 202 | |
|
203 | 203 | def test_visible_scope_with_project |
|
204 | 204 | project = Project.find(1) |
|
205 | 205 | issues = Issue.visible(User.find(2), :project => project).all |
|
206 | 206 | projects = issues.collect(&:project).uniq |
|
207 | 207 | assert_equal 1, projects.size |
|
208 | 208 | assert_equal project, projects.first |
|
209 | 209 | end |
|
210 | 210 | |
|
211 | 211 | def test_visible_scope_with_project_and_subprojects |
|
212 | 212 | project = Project.find(1) |
|
213 | 213 | issues = Issue.visible(User.find(2), :project => project, :with_subprojects => true).all |
|
214 | 214 | projects = issues.collect(&:project).uniq |
|
215 | 215 | assert projects.size > 1 |
|
216 | 216 | assert_equal [], projects.select {|p| !p.is_or_is_descendant_of?(project)} |
|
217 | 217 | end |
|
218 | 218 | |
|
219 | 219 | def test_visible_and_nested_set_scopes |
|
220 | 220 | assert_equal 0, Issue.find(1).descendants.visible.all.size |
|
221 | 221 | end |
|
222 | 222 | |
|
223 | 223 | def test_open_scope |
|
224 | 224 | issues = Issue.open.all |
|
225 | 225 | assert_nil issues.detect(&:closed?) |
|
226 | 226 | end |
|
227 | 227 | |
|
228 | 228 | def test_open_scope_with_arg |
|
229 | 229 | issues = Issue.open(false).all |
|
230 | 230 | assert_equal issues, issues.select(&:closed?) |
|
231 | 231 | end |
|
232 | 232 | |
|
233 | 233 | def test_errors_full_messages_should_include_custom_fields_errors |
|
234 | 234 | field = IssueCustomField.find_by_name('Database') |
|
235 | 235 | |
|
236 | 236 | issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, |
|
237 | 237 | :status_id => 1, :subject => 'test_create', |
|
238 | 238 | :description => 'IssueTest#test_create_with_required_custom_field') |
|
239 | 239 | assert issue.available_custom_fields.include?(field) |
|
240 | 240 | # Invalid value |
|
241 | 241 | issue.custom_field_values = { field.id => 'SQLServer' } |
|
242 | 242 | |
|
243 | 243 | assert !issue.valid? |
|
244 | 244 | assert_equal 1, issue.errors.full_messages.size |
|
245 | 245 | assert_equal "Database #{I18n.translate('activerecord.errors.messages.inclusion')}", |
|
246 | 246 | issue.errors.full_messages.first |
|
247 | 247 | end |
|
248 | 248 | |
|
249 | 249 | def test_update_issue_with_required_custom_field |
|
250 | 250 | field = IssueCustomField.find_by_name('Database') |
|
251 | 251 | field.update_attribute(:is_required, true) |
|
252 | 252 | |
|
253 | 253 | issue = Issue.find(1) |
|
254 | 254 | assert_nil issue.custom_value_for(field) |
|
255 | 255 | assert issue.available_custom_fields.include?(field) |
|
256 | 256 | # No change to custom values, issue can be saved |
|
257 | 257 | assert issue.save |
|
258 | 258 | # Blank value |
|
259 | 259 | issue.custom_field_values = { field.id => '' } |
|
260 | 260 | assert !issue.save |
|
261 | 261 | # Valid value |
|
262 | 262 | issue.custom_field_values = { field.id => 'PostgreSQL' } |
|
263 | 263 | assert issue.save |
|
264 | 264 | issue.reload |
|
265 | 265 | assert_equal 'PostgreSQL', issue.custom_value_for(field).value |
|
266 | 266 | end |
|
267 | 267 | |
|
268 | 268 | def test_should_not_update_attributes_if_custom_fields_validation_fails |
|
269 | 269 | issue = Issue.find(1) |
|
270 | 270 | field = IssueCustomField.find_by_name('Database') |
|
271 | 271 | assert issue.available_custom_fields.include?(field) |
|
272 | 272 | |
|
273 | 273 | issue.custom_field_values = { field.id => 'Invalid' } |
|
274 | 274 | issue.subject = 'Should be not be saved' |
|
275 | 275 | assert !issue.save |
|
276 | 276 | |
|
277 | 277 | issue.reload |
|
278 | 278 | assert_equal "Can't print recipes", issue.subject |
|
279 | 279 | end |
|
280 | 280 | |
|
281 | 281 | def test_should_not_recreate_custom_values_objects_on_update |
|
282 | 282 | field = IssueCustomField.find_by_name('Database') |
|
283 | 283 | |
|
284 | 284 | issue = Issue.find(1) |
|
285 | 285 | issue.custom_field_values = { field.id => 'PostgreSQL' } |
|
286 | 286 | assert issue.save |
|
287 | 287 | custom_value = issue.custom_value_for(field) |
|
288 | 288 | issue.reload |
|
289 | 289 | issue.custom_field_values = { field.id => 'MySQL' } |
|
290 | 290 | assert issue.save |
|
291 | 291 | issue.reload |
|
292 | 292 | assert_equal custom_value.id, issue.custom_value_for(field).id |
|
293 | 293 | end |
|
294 | 294 | |
|
295 | 295 | def test_should_not_update_custom_fields_on_changing_tracker_with_different_custom_fields |
|
296 | 296 | issue = Issue.create!(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :subject => 'Test', :custom_field_values => {'2' => 'Test'}) |
|
297 | 297 | assert !Tracker.find(2).custom_field_ids.include?(2) |
|
298 | 298 | |
|
299 | 299 | issue = Issue.find(issue.id) |
|
300 | 300 | issue.attributes = {:tracker_id => 2, :custom_field_values => {'1' => ''}} |
|
301 | 301 | |
|
302 | 302 | issue = Issue.find(issue.id) |
|
303 | 303 | custom_value = issue.custom_value_for(2) |
|
304 | 304 | assert_not_nil custom_value |
|
305 | 305 | assert_equal 'Test', custom_value.value |
|
306 | 306 | end |
|
307 | 307 | |
|
308 | 308 | def test_assigning_tracker_id_should_reload_custom_fields_values |
|
309 | 309 | issue = Issue.new(:project => Project.find(1)) |
|
310 | 310 | assert issue.custom_field_values.empty? |
|
311 | 311 | issue.tracker_id = 1 |
|
312 | 312 | assert issue.custom_field_values.any? |
|
313 | 313 | end |
|
314 | 314 | |
|
315 | 315 | def test_assigning_attributes_should_assign_project_and_tracker_first |
|
316 | 316 | seq = sequence('seq') |
|
317 | 317 | issue = Issue.new |
|
318 | 318 | issue.expects(:project_id=).in_sequence(seq) |
|
319 | 319 | issue.expects(:tracker_id=).in_sequence(seq) |
|
320 | 320 | issue.expects(:subject=).in_sequence(seq) |
|
321 | 321 | issue.attributes = {:tracker_id => 2, :project_id => 1, :subject => 'Test'} |
|
322 | 322 | end |
|
323 | 323 | |
|
324 | 324 | def test_assigning_tracker_and_custom_fields_should_assign_custom_fields |
|
325 | 325 | attributes = ActiveSupport::OrderedHash.new |
|
326 | 326 | attributes['custom_field_values'] = { '1' => 'MySQL' } |
|
327 | 327 | attributes['tracker_id'] = '1' |
|
328 | 328 | issue = Issue.new(:project => Project.find(1)) |
|
329 | 329 | issue.attributes = attributes |
|
330 | 330 | assert_equal 'MySQL', issue.custom_field_value(1) |
|
331 | 331 | end |
|
332 | 332 | |
|
333 | 333 | def test_should_update_issue_with_disabled_tracker |
|
334 | 334 | p = Project.find(1) |
|
335 | 335 | issue = Issue.find(1) |
|
336 | 336 | |
|
337 | 337 | p.trackers.delete(issue.tracker) |
|
338 | 338 | assert !p.trackers.include?(issue.tracker) |
|
339 | 339 | |
|
340 | 340 | issue.reload |
|
341 | 341 | issue.subject = 'New subject' |
|
342 | 342 | assert issue.save |
|
343 | 343 | end |
|
344 | 344 | |
|
345 | 345 | def test_should_not_set_a_disabled_tracker |
|
346 | 346 | p = Project.find(1) |
|
347 | 347 | p.trackers.delete(Tracker.find(2)) |
|
348 | 348 | |
|
349 | 349 | issue = Issue.find(1) |
|
350 | 350 | issue.tracker_id = 2 |
|
351 | 351 | issue.subject = 'New subject' |
|
352 | 352 | assert !issue.save |
|
353 | 353 | assert_not_nil issue.errors[:tracker_id] |
|
354 | 354 | end |
|
355 | 355 | |
|
356 | 356 | def test_category_based_assignment |
|
357 | 357 | issue = Issue.create(:project_id => 1, :tracker_id => 1, :author_id => 3, |
|
358 | 358 | :status_id => 1, :priority => IssuePriority.all.first, |
|
359 | 359 | :subject => 'Assignment test', |
|
360 | 360 | :description => 'Assignment test', :category_id => 1) |
|
361 | 361 | assert_equal IssueCategory.find(1).assigned_to, issue.assigned_to |
|
362 | 362 | end |
|
363 | 363 | |
|
364 | 364 | def test_new_statuses_allowed_to |
|
365 | 365 | Workflow.delete_all |
|
366 | 366 | |
|
367 | 367 | Workflow.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 2, :author => false, :assignee => false) |
|
368 | 368 | Workflow.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 3, :author => true, :assignee => false) |
|
369 | 369 | Workflow.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 4, :author => false, :assignee => true) |
|
370 | 370 | Workflow.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 5, :author => true, :assignee => true) |
|
371 | 371 | status = IssueStatus.find(1) |
|
372 | 372 | role = Role.find(1) |
|
373 | 373 | tracker = Tracker.find(1) |
|
374 | 374 | user = User.find(2) |
|
375 | 375 | |
|
376 | 376 | issue = Issue.generate!(:tracker => tracker, :status => status, :project_id => 1, :author_id => 1) |
|
377 | 377 | assert_equal [1, 2], issue.new_statuses_allowed_to(user).map(&:id) |
|
378 | 378 | |
|
379 | 379 | issue = Issue.generate!(:tracker => tracker, :status => status, :project_id => 1, :author => user) |
|
380 | 380 | assert_equal [1, 2, 3, 5], issue.new_statuses_allowed_to(user).map(&:id) |
|
381 | 381 | |
|
382 | 382 | issue = Issue.generate!(:tracker => tracker, :status => status, :project_id => 1, :author_id => 1, :assigned_to => user) |
|
383 | 383 | assert_equal [1, 2, 4, 5], issue.new_statuses_allowed_to(user).map(&:id) |
|
384 | 384 | |
|
385 | 385 | issue = Issue.generate!(:tracker => tracker, :status => status, :project_id => 1, :author => user, :assigned_to => user) |
|
386 | 386 | assert_equal [1, 2, 3, 4, 5], issue.new_statuses_allowed_to(user).map(&:id) |
|
387 | 387 | end |
|
388 | 388 | |
|
389 | 389 | def test_new_statuses_allowed_to_should_return_all_transitions_for_admin |
|
390 | 390 | admin = User.find(1) |
|
391 | 391 | issue = Issue.find(1) |
|
392 | 392 | assert !admin.member_of?(issue.project) |
|
393 | 393 | expected_statuses = [issue.status] + Workflow.find_all_by_old_status_id(issue.status_id).map(&:new_status).uniq.sort |
|
394 | 394 | |
|
395 | 395 | assert_equal expected_statuses, issue.new_statuses_allowed_to(admin) |
|
396 | 396 | end |
|
397 | 397 | |
|
398 | 398 | def test_new_statuses_allowed_to_should_return_default_and_current_status_when_copying |
|
399 | 399 | issue = Issue.find(1).copy |
|
400 | 400 | assert_equal [1], issue.new_statuses_allowed_to(User.find(2)).map(&:id) |
|
401 | 401 | |
|
402 | 402 | issue = Issue.find(2).copy |
|
403 | 403 | assert_equal [1, 2], issue.new_statuses_allowed_to(User.find(2)).map(&:id) |
|
404 | 404 | end |
|
405 | 405 | |
|
406 | 406 | def test_copy |
|
407 | 407 | issue = Issue.new.copy_from(1) |
|
408 | 408 | assert issue.copy? |
|
409 | 409 | assert issue.save |
|
410 | 410 | issue.reload |
|
411 | 411 | orig = Issue.find(1) |
|
412 | 412 | assert_equal orig.subject, issue.subject |
|
413 | 413 | assert_equal orig.tracker, issue.tracker |
|
414 | 414 | assert_equal "125", issue.custom_value_for(2).value |
|
415 | 415 | end |
|
416 | 416 | |
|
417 | 417 | def test_copy_should_copy_status |
|
418 | 418 | orig = Issue.find(8) |
|
419 | 419 | assert orig.status != IssueStatus.default |
|
420 | 420 | |
|
421 | 421 | issue = Issue.new.copy_from(orig) |
|
422 | 422 | assert issue.save |
|
423 | 423 | issue.reload |
|
424 | 424 | assert_equal orig.status, issue.status |
|
425 | 425 | end |
|
426 | 426 | |
|
427 | 427 | def test_should_not_call_after_project_change_on_creation |
|
428 | 428 | issue = Issue.new(:project_id => 1, :tracker_id => 1, :status_id => 1, :subject => 'Test', :author_id => 1) |
|
429 | 429 | issue.expects(:after_project_change).never |
|
430 | 430 | issue.save! |
|
431 | 431 | end |
|
432 | 432 | |
|
433 | 433 | def test_should_not_call_after_project_change_on_update |
|
434 | 434 | issue = Issue.find(1) |
|
435 | 435 | issue.project = Project.find(1) |
|
436 | 436 | issue.subject = 'No project change' |
|
437 | 437 | issue.expects(:after_project_change).never |
|
438 | 438 | issue.save! |
|
439 | 439 | end |
|
440 | 440 | |
|
441 | 441 | def test_should_call_after_project_change_on_project_change |
|
442 | 442 | issue = Issue.find(1) |
|
443 | 443 | issue.project = Project.find(2) |
|
444 | 444 | issue.expects(:after_project_change).once |
|
445 | 445 | issue.save! |
|
446 | 446 | end |
|
447 | 447 | |
|
448 | def test_adding_journal_should_update_timestamp | |
|
449 | issue = Issue.find(1) | |
|
450 | updated_on_was = issue.updated_on | |
|
451 | ||
|
452 | issue.init_journal(User.first, "Adding notes") | |
|
453 | assert_difference 'Journal.count' do | |
|
454 | assert issue.save | |
|
455 | end | |
|
456 | issue.reload | |
|
457 | ||
|
458 | assert_not_equal updated_on_was, issue.updated_on | |
|
459 | end | |
|
460 | ||
|
448 | 461 | def test_should_close_duplicates |
|
449 | 462 | # Create 3 issues |
|
450 | 463 | project = Project.find(1) |
|
451 | 464 | issue1 = Issue.generate_for_project!(project) |
|
452 | 465 | issue2 = Issue.generate_for_project!(project) |
|
453 | 466 | issue3 = Issue.generate_for_project!(project) |
|
454 | 467 | |
|
455 | 468 | # 2 is a dupe of 1 |
|
456 | 469 | IssueRelation.create!(:issue_from => issue2, :issue_to => issue1, :relation_type => IssueRelation::TYPE_DUPLICATES) |
|
457 | 470 | # And 3 is a dupe of 2 |
|
458 | 471 | IssueRelation.create!(:issue_from => issue3, :issue_to => issue2, :relation_type => IssueRelation::TYPE_DUPLICATES) |
|
459 | 472 | # And 3 is a dupe of 1 (circular duplicates) |
|
460 | 473 | IssueRelation.create!(:issue_from => issue3, :issue_to => issue1, :relation_type => IssueRelation::TYPE_DUPLICATES) |
|
461 | 474 | |
|
462 | 475 | assert issue1.reload.duplicates.include?(issue2) |
|
463 | 476 | |
|
464 | 477 | # Closing issue 1 |
|
465 | 478 | issue1.init_journal(User.find(:first), "Closing issue1") |
|
466 | 479 | issue1.status = IssueStatus.find :first, :conditions => {:is_closed => true} |
|
467 | 480 | assert issue1.save |
|
468 | 481 | # 2 and 3 should be also closed |
|
469 | 482 | assert issue2.reload.closed? |
|
470 | 483 | assert issue3.reload.closed? |
|
471 | 484 | end |
|
472 | 485 | |
|
473 | 486 | def test_should_not_close_duplicated_issue |
|
474 | 487 | project = Project.find(1) |
|
475 | 488 | issue1 = Issue.generate_for_project!(project) |
|
476 | 489 | issue2 = Issue.generate_for_project!(project) |
|
477 | 490 | |
|
478 | 491 | # 2 is a dupe of 1 |
|
479 | 492 | IssueRelation.create(:issue_from => issue2, :issue_to => issue1, :relation_type => IssueRelation::TYPE_DUPLICATES) |
|
480 | 493 | # 2 is a dup of 1 but 1 is not a duplicate of 2 |
|
481 | 494 | assert !issue2.reload.duplicates.include?(issue1) |
|
482 | 495 | |
|
483 | 496 | # Closing issue 2 |
|
484 | 497 | issue2.init_journal(User.find(:first), "Closing issue2") |
|
485 | 498 | issue2.status = IssueStatus.find :first, :conditions => {:is_closed => true} |
|
486 | 499 | assert issue2.save |
|
487 | 500 | # 1 should not be also closed |
|
488 | 501 | assert !issue1.reload.closed? |
|
489 | 502 | end |
|
490 | 503 | |
|
491 | 504 | def test_assignable_versions |
|
492 | 505 | issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :fixed_version_id => 1, :subject => 'New issue') |
|
493 | 506 | assert_equal ['open'], issue.assignable_versions.collect(&:status).uniq |
|
494 | 507 | end |
|
495 | 508 | |
|
496 | 509 | def test_should_not_be_able_to_assign_a_new_issue_to_a_closed_version |
|
497 | 510 | issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :fixed_version_id => 1, :subject => 'New issue') |
|
498 | 511 | assert !issue.save |
|
499 | 512 | assert_not_nil issue.errors[:fixed_version_id] |
|
500 | 513 | end |
|
501 | 514 | |
|
502 | 515 | def test_should_not_be_able_to_assign_a_new_issue_to_a_locked_version |
|
503 | 516 | issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :fixed_version_id => 2, :subject => 'New issue') |
|
504 | 517 | assert !issue.save |
|
505 | 518 | assert_not_nil issue.errors[:fixed_version_id] |
|
506 | 519 | end |
|
507 | 520 | |
|
508 | 521 | def test_should_be_able_to_assign_a_new_issue_to_an_open_version |
|
509 | 522 | issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :fixed_version_id => 3, :subject => 'New issue') |
|
510 | 523 | assert issue.save |
|
511 | 524 | end |
|
512 | 525 | |
|
513 | 526 | def test_should_be_able_to_update_an_issue_assigned_to_a_closed_version |
|
514 | 527 | issue = Issue.find(11) |
|
515 | 528 | assert_equal 'closed', issue.fixed_version.status |
|
516 | 529 | issue.subject = 'Subject changed' |
|
517 | 530 | assert issue.save |
|
518 | 531 | end |
|
519 | 532 | |
|
520 | 533 | def test_should_not_be_able_to_reopen_an_issue_assigned_to_a_closed_version |
|
521 | 534 | issue = Issue.find(11) |
|
522 | 535 | issue.status_id = 1 |
|
523 | 536 | assert !issue.save |
|
524 | 537 | assert_not_nil issue.errors[:base] |
|
525 | 538 | end |
|
526 | 539 | |
|
527 | 540 | def test_should_be_able_to_reopen_and_reassign_an_issue_assigned_to_a_closed_version |
|
528 | 541 | issue = Issue.find(11) |
|
529 | 542 | issue.status_id = 1 |
|
530 | 543 | issue.fixed_version_id = 3 |
|
531 | 544 | assert issue.save |
|
532 | 545 | end |
|
533 | 546 | |
|
534 | 547 | def test_should_be_able_to_reopen_an_issue_assigned_to_a_locked_version |
|
535 | 548 | issue = Issue.find(12) |
|
536 | 549 | assert_equal 'locked', issue.fixed_version.status |
|
537 | 550 | issue.status_id = 1 |
|
538 | 551 | assert issue.save |
|
539 | 552 | end |
|
540 | 553 | |
|
541 | 554 | def test_allowed_target_projects_on_move_should_include_projects_with_issue_tracking_enabled |
|
542 | 555 | assert_include Project.find(2), Issue.allowed_target_projects_on_move(User.find(2)) |
|
543 | 556 | end |
|
544 | 557 | |
|
545 | 558 | def test_allowed_target_projects_on_move_should_not_include_projects_with_issue_tracking_disabled |
|
546 | 559 | Project.find(2).disable_module! :issue_tracking |
|
547 | 560 | assert_not_include Project.find(2), Issue.allowed_target_projects_on_move(User.find(2)) |
|
548 | 561 | end |
|
549 | 562 | |
|
550 | 563 | def test_move_to_another_project_with_same_category |
|
551 | 564 | issue = Issue.find(1) |
|
552 | 565 | issue.project = Project.find(2) |
|
553 | 566 | assert issue.save |
|
554 | 567 | issue.reload |
|
555 | 568 | assert_equal 2, issue.project_id |
|
556 | 569 | # Category changes |
|
557 | 570 | assert_equal 4, issue.category_id |
|
558 | 571 | # Make sure time entries were move to the target project |
|
559 | 572 | assert_equal 2, issue.time_entries.first.project_id |
|
560 | 573 | end |
|
561 | 574 | |
|
562 | 575 | def test_move_to_another_project_without_same_category |
|
563 | 576 | issue = Issue.find(2) |
|
564 | 577 | issue.project = Project.find(2) |
|
565 | 578 | assert issue.save |
|
566 | 579 | issue.reload |
|
567 | 580 | assert_equal 2, issue.project_id |
|
568 | 581 | # Category cleared |
|
569 | 582 | assert_nil issue.category_id |
|
570 | 583 | end |
|
571 | 584 | |
|
572 | 585 | def test_move_to_another_project_should_clear_fixed_version_when_not_shared |
|
573 | 586 | issue = Issue.find(1) |
|
574 | 587 | issue.update_attribute(:fixed_version_id, 1) |
|
575 | 588 | issue.project = Project.find(2) |
|
576 | 589 | assert issue.save |
|
577 | 590 | issue.reload |
|
578 | 591 | assert_equal 2, issue.project_id |
|
579 | 592 | # Cleared fixed_version |
|
580 | 593 | assert_equal nil, issue.fixed_version |
|
581 | 594 | end |
|
582 | 595 | |
|
583 | 596 | def test_move_to_another_project_should_keep_fixed_version_when_shared_with_the_target_project |
|
584 | 597 | issue = Issue.find(1) |
|
585 | 598 | issue.update_attribute(:fixed_version_id, 4) |
|
586 | 599 | issue.project = Project.find(5) |
|
587 | 600 | assert issue.save |
|
588 | 601 | issue.reload |
|
589 | 602 | assert_equal 5, issue.project_id |
|
590 | 603 | # Keep fixed_version |
|
591 | 604 | assert_equal 4, issue.fixed_version_id |
|
592 | 605 | end |
|
593 | 606 | |
|
594 | 607 | def test_move_to_another_project_should_clear_fixed_version_when_not_shared_with_the_target_project |
|
595 | 608 | issue = Issue.find(1) |
|
596 | 609 | issue.update_attribute(:fixed_version_id, 1) |
|
597 | 610 | issue.project = Project.find(5) |
|
598 | 611 | assert issue.save |
|
599 | 612 | issue.reload |
|
600 | 613 | assert_equal 5, issue.project_id |
|
601 | 614 | # Cleared fixed_version |
|
602 | 615 | assert_equal nil, issue.fixed_version |
|
603 | 616 | end |
|
604 | 617 | |
|
605 | 618 | def test_move_to_another_project_should_keep_fixed_version_when_shared_systemwide |
|
606 | 619 | issue = Issue.find(1) |
|
607 | 620 | issue.update_attribute(:fixed_version_id, 7) |
|
608 | 621 | issue.project = Project.find(2) |
|
609 | 622 | assert issue.save |
|
610 | 623 | issue.reload |
|
611 | 624 | assert_equal 2, issue.project_id |
|
612 | 625 | # Keep fixed_version |
|
613 | 626 | assert_equal 7, issue.fixed_version_id |
|
614 | 627 | end |
|
615 | 628 | |
|
616 | 629 | def test_move_to_another_project_with_disabled_tracker |
|
617 | 630 | issue = Issue.find(1) |
|
618 | 631 | target = Project.find(2) |
|
619 | 632 | target.tracker_ids = [3] |
|
620 | 633 | target.save |
|
621 | 634 | issue.project = target |
|
622 | 635 | assert issue.save |
|
623 | 636 | issue.reload |
|
624 | 637 | assert_equal 2, issue.project_id |
|
625 | 638 | assert_equal 3, issue.tracker_id |
|
626 | 639 | end |
|
627 | 640 | |
|
628 | 641 | def test_copy_to_the_same_project |
|
629 | 642 | issue = Issue.find(1) |
|
630 | 643 | copy = issue.copy |
|
631 | 644 | assert_difference 'Issue.count' do |
|
632 | 645 | copy.save! |
|
633 | 646 | end |
|
634 | 647 | assert_kind_of Issue, copy |
|
635 | 648 | assert_equal issue.project, copy.project |
|
636 | 649 | assert_equal "125", copy.custom_value_for(2).value |
|
637 | 650 | end |
|
638 | 651 | |
|
639 | 652 | def test_copy_to_another_project_and_tracker |
|
640 | 653 | issue = Issue.find(1) |
|
641 | 654 | copy = issue.copy(:project_id => 3, :tracker_id => 2) |
|
642 | 655 | assert_difference 'Issue.count' do |
|
643 | 656 | copy.save! |
|
644 | 657 | end |
|
645 | 658 | copy.reload |
|
646 | 659 | assert_kind_of Issue, copy |
|
647 | 660 | assert_equal Project.find(3), copy.project |
|
648 | 661 | assert_equal Tracker.find(2), copy.tracker |
|
649 | 662 | # Custom field #2 is not associated with target tracker |
|
650 | 663 | assert_nil copy.custom_value_for(2) |
|
651 | 664 | end |
|
652 | 665 | |
|
653 | 666 | context "#copy" do |
|
654 | 667 | setup do |
|
655 | 668 | @issue = Issue.find(1) |
|
656 | 669 | end |
|
657 | 670 | |
|
658 | 671 | should "not create a journal" do |
|
659 | 672 | copy = @issue.copy(:project_id => 3, :tracker_id => 2, :assigned_to_id => 3) |
|
660 | 673 | copy.save! |
|
661 | 674 | assert_equal 0, copy.reload.journals.size |
|
662 | 675 | end |
|
663 | 676 | |
|
664 | 677 | should "allow assigned_to changes" do |
|
665 | 678 | copy = @issue.copy(:project_id => 3, :tracker_id => 2, :assigned_to_id => 3) |
|
666 | 679 | assert_equal 3, copy.assigned_to_id |
|
667 | 680 | end |
|
668 | 681 | |
|
669 | 682 | should "allow status changes" do |
|
670 | 683 | copy = @issue.copy(:project_id => 3, :tracker_id => 2, :status_id => 2) |
|
671 | 684 | assert_equal 2, copy.status_id |
|
672 | 685 | end |
|
673 | 686 | |
|
674 | 687 | should "allow start date changes" do |
|
675 | 688 | date = Date.today |
|
676 | 689 | copy = @issue.copy(:project_id => 3, :tracker_id => 2, :start_date => date) |
|
677 | 690 | assert_equal date, copy.start_date |
|
678 | 691 | end |
|
679 | 692 | |
|
680 | 693 | should "allow due date changes" do |
|
681 | 694 | date = Date.today |
|
682 | 695 | copy = @issue.copy(:project_id => 3, :tracker_id => 2, :due_date => date) |
|
683 | 696 | assert_equal date, copy.due_date |
|
684 | 697 | end |
|
685 | 698 | |
|
686 | 699 | should "set current user as author" do |
|
687 | 700 | User.current = User.find(9) |
|
688 | 701 | copy = @issue.copy(:project_id => 3, :tracker_id => 2) |
|
689 | 702 | assert_equal User.current, copy.author |
|
690 | 703 | end |
|
691 | 704 | |
|
692 | 705 | should "create a journal with notes" do |
|
693 | 706 | date = Date.today |
|
694 | 707 | notes = "Notes added when copying" |
|
695 | 708 | copy = @issue.copy(:project_id => 3, :tracker_id => 2, :start_date => date) |
|
696 | 709 | copy.init_journal(User.current, notes) |
|
697 | 710 | copy.save! |
|
698 | 711 | |
|
699 | 712 | assert_equal 1, copy.journals.size |
|
700 | 713 | journal = copy.journals.first |
|
701 | 714 | assert_equal 0, journal.details.size |
|
702 | 715 | assert_equal notes, journal.notes |
|
703 | 716 | end |
|
704 | 717 | end |
|
705 | 718 | |
|
706 | 719 | def test_recipients_should_include_previous_assignee |
|
707 | 720 | user = User.find(3) |
|
708 | 721 | user.members.update_all ["mail_notification = ?", false] |
|
709 | 722 | user.update_attribute :mail_notification, 'only_assigned' |
|
710 | 723 | |
|
711 | 724 | issue = Issue.find(2) |
|
712 | 725 | issue.assigned_to = nil |
|
713 | 726 | assert_include user.mail, issue.recipients |
|
714 | 727 | issue.save! |
|
715 | 728 | assert !issue.recipients.include?(user.mail) |
|
716 | 729 | end |
|
717 | 730 | |
|
718 | 731 | def test_recipients_should_not_include_users_that_cannot_view_the_issue |
|
719 | 732 | issue = Issue.find(12) |
|
720 | 733 | assert issue.recipients.include?(issue.author.mail) |
|
721 | 734 | # copy the issue to a private project |
|
722 | 735 | copy = issue.copy(:project_id => 5, :tracker_id => 2) |
|
723 | 736 | # author is not a member of project anymore |
|
724 | 737 | assert !copy.recipients.include?(copy.author.mail) |
|
725 | 738 | end |
|
726 | 739 | |
|
727 | 740 | def test_recipients_should_include_the_assigned_group_members |
|
728 | 741 | group_member = User.generate! |
|
729 | 742 | group = Group.generate! |
|
730 | 743 | group.users << group_member |
|
731 | 744 | |
|
732 | 745 | issue = Issue.find(12) |
|
733 | 746 | issue.assigned_to = group |
|
734 | 747 | assert issue.recipients.include?(group_member.mail) |
|
735 | 748 | end |
|
736 | 749 | |
|
737 | 750 | def test_watcher_recipients_should_not_include_users_that_cannot_view_the_issue |
|
738 | 751 | user = User.find(3) |
|
739 | 752 | issue = Issue.find(9) |
|
740 | 753 | Watcher.create!(:user => user, :watchable => issue) |
|
741 | 754 | assert issue.watched_by?(user) |
|
742 | 755 | assert !issue.watcher_recipients.include?(user.mail) |
|
743 | 756 | end |
|
744 | 757 | |
|
745 | 758 | def test_issue_destroy |
|
746 | 759 | Issue.find(1).destroy |
|
747 | 760 | assert_nil Issue.find_by_id(1) |
|
748 | 761 | assert_nil TimeEntry.find_by_issue_id(1) |
|
749 | 762 | end |
|
750 | 763 | |
|
751 | 764 | def test_blocked |
|
752 | 765 | blocked_issue = Issue.find(9) |
|
753 | 766 | blocking_issue = Issue.find(10) |
|
754 | 767 | |
|
755 | 768 | assert blocked_issue.blocked? |
|
756 | 769 | assert !blocking_issue.blocked? |
|
757 | 770 | end |
|
758 | 771 | |
|
759 | 772 | def test_blocked_issues_dont_allow_closed_statuses |
|
760 | 773 | blocked_issue = Issue.find(9) |
|
761 | 774 | |
|
762 | 775 | allowed_statuses = blocked_issue.new_statuses_allowed_to(users(:users_002)) |
|
763 | 776 | assert !allowed_statuses.empty? |
|
764 | 777 | closed_statuses = allowed_statuses.select {|st| st.is_closed?} |
|
765 | 778 | assert closed_statuses.empty? |
|
766 | 779 | end |
|
767 | 780 | |
|
768 | 781 | def test_unblocked_issues_allow_closed_statuses |
|
769 | 782 | blocking_issue = Issue.find(10) |
|
770 | 783 | |
|
771 | 784 | allowed_statuses = blocking_issue.new_statuses_allowed_to(users(:users_002)) |
|
772 | 785 | assert !allowed_statuses.empty? |
|
773 | 786 | closed_statuses = allowed_statuses.select {|st| st.is_closed?} |
|
774 | 787 | assert !closed_statuses.empty? |
|
775 | 788 | end |
|
776 | 789 | |
|
777 | 790 | def test_rescheduling_an_issue_should_reschedule_following_issue |
|
778 | 791 | issue1 = Issue.create!(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :subject => '-', :start_date => Date.today, :due_date => Date.today + 2) |
|
779 | 792 | issue2 = Issue.create!(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :subject => '-', :start_date => Date.today, :due_date => Date.today + 2) |
|
780 | 793 | IssueRelation.create!(:issue_from => issue1, :issue_to => issue2, :relation_type => IssueRelation::TYPE_PRECEDES) |
|
781 | 794 | assert_equal issue1.due_date + 1, issue2.reload.start_date |
|
782 | 795 | |
|
783 | 796 | issue1.due_date = Date.today + 5 |
|
784 | 797 | issue1.save! |
|
785 | 798 | assert_equal issue1.due_date + 1, issue2.reload.start_date |
|
786 | 799 | end |
|
787 | 800 | |
|
788 | 801 | def test_rescheduling_a_stale_issue_should_not_raise_an_error |
|
789 | 802 | stale = Issue.find(1) |
|
790 | 803 | issue = Issue.find(1) |
|
791 | 804 | issue.subject = "Updated" |
|
792 | 805 | issue.save! |
|
793 | 806 | |
|
794 | 807 | date = 10.days.from_now.to_date |
|
795 | 808 | assert_nothing_raised do |
|
796 | 809 | stale.reschedule_after(date) |
|
797 | 810 | end |
|
798 | 811 | assert_equal date, stale.reload.start_date |
|
799 | 812 | end |
|
800 | 813 | |
|
801 | 814 | def test_overdue |
|
802 | 815 | assert Issue.new(:due_date => 1.day.ago.to_date).overdue? |
|
803 | 816 | assert !Issue.new(:due_date => Date.today).overdue? |
|
804 | 817 | assert !Issue.new(:due_date => 1.day.from_now.to_date).overdue? |
|
805 | 818 | assert !Issue.new(:due_date => nil).overdue? |
|
806 | 819 | assert !Issue.new(:due_date => 1.day.ago.to_date, :status => IssueStatus.find(:first, :conditions => {:is_closed => true})).overdue? |
|
807 | 820 | end |
|
808 | 821 | |
|
809 | 822 | context "#behind_schedule?" do |
|
810 | 823 | should "be false if the issue has no start_date" do |
|
811 | 824 | assert !Issue.new(:start_date => nil, :due_date => 1.day.from_now.to_date, :done_ratio => 0).behind_schedule? |
|
812 | 825 | end |
|
813 | 826 | |
|
814 | 827 | should "be false if the issue has no end_date" do |
|
815 | 828 | assert !Issue.new(:start_date => 1.day.from_now.to_date, :due_date => nil, :done_ratio => 0).behind_schedule? |
|
816 | 829 | end |
|
817 | 830 | |
|
818 | 831 | should "be false if the issue has more done than it's calendar time" do |
|
819 | 832 | assert !Issue.new(:start_date => 50.days.ago.to_date, :due_date => 50.days.from_now.to_date, :done_ratio => 90).behind_schedule? |
|
820 | 833 | end |
|
821 | 834 | |
|
822 | 835 | should "be true if the issue hasn't been started at all" do |
|
823 | 836 | assert Issue.new(:start_date => 1.day.ago.to_date, :due_date => 1.day.from_now.to_date, :done_ratio => 0).behind_schedule? |
|
824 | 837 | end |
|
825 | 838 | |
|
826 | 839 | should "be true if the issue has used more calendar time than it's done ratio" do |
|
827 | 840 | assert Issue.new(:start_date => 100.days.ago.to_date, :due_date => Date.today, :done_ratio => 90).behind_schedule? |
|
828 | 841 | end |
|
829 | 842 | end |
|
830 | 843 | |
|
831 | 844 | context "#assignable_users" do |
|
832 | 845 | should "be Users" do |
|
833 | 846 | assert_kind_of User, Issue.find(1).assignable_users.first |
|
834 | 847 | end |
|
835 | 848 | |
|
836 | 849 | should "include the issue author" do |
|
837 | 850 | project = Project.find(1) |
|
838 | 851 | non_project_member = User.generate! |
|
839 | 852 | issue = Issue.generate_for_project!(project, :author => non_project_member) |
|
840 | 853 | |
|
841 | 854 | assert issue.assignable_users.include?(non_project_member) |
|
842 | 855 | end |
|
843 | 856 | |
|
844 | 857 | should "include the current assignee" do |
|
845 | 858 | project = Project.find(1) |
|
846 | 859 | user = User.generate! |
|
847 | 860 | issue = Issue.generate_for_project!(project, :assigned_to => user) |
|
848 | 861 | user.lock! |
|
849 | 862 | |
|
850 | 863 | assert Issue.find(issue.id).assignable_users.include?(user) |
|
851 | 864 | end |
|
852 | 865 | |
|
853 | 866 | should "not show the issue author twice" do |
|
854 | 867 | assignable_user_ids = Issue.find(1).assignable_users.collect(&:id) |
|
855 | 868 | assert_equal 2, assignable_user_ids.length |
|
856 | 869 | |
|
857 | 870 | assignable_user_ids.each do |user_id| |
|
858 | 871 | assert_equal 1, assignable_user_ids.select {|i| i == user_id}.length, "User #{user_id} appears more or less than once" |
|
859 | 872 | end |
|
860 | 873 | end |
|
861 | 874 | |
|
862 | 875 | context "with issue_group_assignment" do |
|
863 | 876 | should "include groups" do |
|
864 | 877 | issue = Issue.new(:project => Project.find(2)) |
|
865 | 878 | |
|
866 | 879 | with_settings :issue_group_assignment => '1' do |
|
867 | 880 | assert_equal %w(Group User), issue.assignable_users.map {|a| a.class.name}.uniq.sort |
|
868 | 881 | assert issue.assignable_users.include?(Group.find(11)) |
|
869 | 882 | end |
|
870 | 883 | end |
|
871 | 884 | end |
|
872 | 885 | |
|
873 | 886 | context "without issue_group_assignment" do |
|
874 | 887 | should "not include groups" do |
|
875 | 888 | issue = Issue.new(:project => Project.find(2)) |
|
876 | 889 | |
|
877 | 890 | with_settings :issue_group_assignment => '0' do |
|
878 | 891 | assert_equal %w(User), issue.assignable_users.map {|a| a.class.name}.uniq.sort |
|
879 | 892 | assert !issue.assignable_users.include?(Group.find(11)) |
|
880 | 893 | end |
|
881 | 894 | end |
|
882 | 895 | end |
|
883 | 896 | end |
|
884 | 897 | |
|
885 | 898 | def test_create_should_send_email_notification |
|
886 | 899 | ActionMailer::Base.deliveries.clear |
|
887 | 900 | issue = Issue.new(:project_id => 1, :tracker_id => 1, |
|
888 | 901 | :author_id => 3, :status_id => 1, |
|
889 | 902 | :priority => IssuePriority.all.first, |
|
890 | 903 | :subject => 'test_create', :estimated_hours => '1:30') |
|
891 | 904 | |
|
892 | 905 | assert issue.save |
|
893 | 906 | assert_equal 1, ActionMailer::Base.deliveries.size |
|
894 | 907 | end |
|
895 | 908 | |
|
896 | 909 | def test_stale_issue_should_not_send_email_notification |
|
897 | 910 | ActionMailer::Base.deliveries.clear |
|
898 | 911 | issue = Issue.find(1) |
|
899 | 912 | stale = Issue.find(1) |
|
900 | 913 | |
|
901 | 914 | issue.init_journal(User.find(1)) |
|
902 | 915 | issue.subject = 'Subjet update' |
|
903 | 916 | assert issue.save |
|
904 | 917 | assert_equal 1, ActionMailer::Base.deliveries.size |
|
905 | 918 | ActionMailer::Base.deliveries.clear |
|
906 | 919 | |
|
907 | 920 | stale.init_journal(User.find(1)) |
|
908 | 921 | stale.subject = 'Another subjet update' |
|
909 | 922 | assert_raise ActiveRecord::StaleObjectError do |
|
910 | 923 | stale.save |
|
911 | 924 | end |
|
912 | 925 | assert ActionMailer::Base.deliveries.empty? |
|
913 | 926 | end |
|
914 | 927 | |
|
915 | 928 | def test_journalized_description |
|
916 | 929 | IssueCustomField.delete_all |
|
917 | 930 | |
|
918 | 931 | i = Issue.first |
|
919 | 932 | old_description = i.description |
|
920 | 933 | new_description = "This is the new description" |
|
921 | 934 | |
|
922 | 935 | i.init_journal(User.find(2)) |
|
923 | 936 | i.description = new_description |
|
924 | 937 | assert_difference 'Journal.count', 1 do |
|
925 | 938 | assert_difference 'JournalDetail.count', 1 do |
|
926 | 939 | i.save! |
|
927 | 940 | end |
|
928 | 941 | end |
|
929 | 942 | |
|
930 | 943 | detail = JournalDetail.first(:order => 'id DESC') |
|
931 | 944 | assert_equal i, detail.journal.journalized |
|
932 | 945 | assert_equal 'attr', detail.property |
|
933 | 946 | assert_equal 'description', detail.prop_key |
|
934 | 947 | assert_equal old_description, detail.old_value |
|
935 | 948 | assert_equal new_description, detail.value |
|
936 | 949 | end |
|
937 | 950 | |
|
938 | 951 | def test_blank_descriptions_should_not_be_journalized |
|
939 | 952 | IssueCustomField.delete_all |
|
940 | 953 | Issue.update_all("description = NULL", "id=1") |
|
941 | 954 | |
|
942 | 955 | i = Issue.find(1) |
|
943 | 956 | i.init_journal(User.find(2)) |
|
944 | 957 | i.subject = "blank description" |
|
945 | 958 | i.description = "\r\n" |
|
946 | 959 | |
|
947 | 960 | assert_difference 'Journal.count', 1 do |
|
948 | 961 | assert_difference 'JournalDetail.count', 1 do |
|
949 | 962 | i.save! |
|
950 | 963 | end |
|
951 | 964 | end |
|
952 | 965 | end |
|
953 | 966 | |
|
954 | 967 | def test_journalized_multi_custom_field |
|
955 | 968 | field = IssueCustomField.create!(:name => 'filter', :field_format => 'list', :is_filter => true, :is_for_all => true, |
|
956 | 969 | :tracker_ids => [1], :possible_values => ['value1', 'value2', 'value3'], :multiple => true) |
|
957 | 970 | |
|
958 | 971 | issue = Issue.create!(:project_id => 1, :tracker_id => 1, :subject => 'Test', :author_id => 1) |
|
959 | 972 | |
|
960 | 973 | assert_difference 'Journal.count' do |
|
961 | 974 | assert_difference 'JournalDetail.count' do |
|
962 | 975 | issue.init_journal(User.first) |
|
963 | 976 | issue.custom_field_values = {field.id => ['value1']} |
|
964 | 977 | issue.save! |
|
965 | 978 | end |
|
966 | 979 | assert_difference 'JournalDetail.count' do |
|
967 | 980 | issue.init_journal(User.first) |
|
968 | 981 | issue.custom_field_values = {field.id => ['value1', 'value2']} |
|
969 | 982 | issue.save! |
|
970 | 983 | end |
|
971 | 984 | assert_difference 'JournalDetail.count', 2 do |
|
972 | 985 | issue.init_journal(User.first) |
|
973 | 986 | issue.custom_field_values = {field.id => ['value3', 'value2']} |
|
974 | 987 | issue.save! |
|
975 | 988 | end |
|
976 | 989 | assert_difference 'JournalDetail.count', 2 do |
|
977 | 990 | issue.init_journal(User.first) |
|
978 | 991 | issue.custom_field_values = {field.id => nil} |
|
979 | 992 | issue.save! |
|
980 | 993 | end |
|
981 | 994 | end |
|
982 | 995 | end |
|
983 | 996 | |
|
984 | 997 | def test_description_eol_should_be_normalized |
|
985 | 998 | i = Issue.new(:description => "CR \r LF \n CRLF \r\n") |
|
986 | 999 | assert_equal "CR \r\n LF \r\n CRLF \r\n", i.description |
|
987 | 1000 | end |
|
988 | 1001 | |
|
989 | 1002 | def test_saving_twice_should_not_duplicate_journal_details |
|
990 | 1003 | i = Issue.find(:first) |
|
991 | 1004 | i.init_journal(User.find(2), 'Some notes') |
|
992 | 1005 | # initial changes |
|
993 | 1006 | i.subject = 'New subject' |
|
994 | 1007 | i.done_ratio = i.done_ratio + 10 |
|
995 | 1008 | assert_difference 'Journal.count' do |
|
996 | 1009 | assert i.save |
|
997 | 1010 | end |
|
998 | 1011 | # 1 more change |
|
999 | 1012 | i.priority = IssuePriority.find(:first, :conditions => ["id <> ?", i.priority_id]) |
|
1000 | 1013 | assert_no_difference 'Journal.count' do |
|
1001 | 1014 | assert_difference 'JournalDetail.count', 1 do |
|
1002 | 1015 | i.save |
|
1003 | 1016 | end |
|
1004 | 1017 | end |
|
1005 | 1018 | # no more change |
|
1006 | 1019 | assert_no_difference 'Journal.count' do |
|
1007 | 1020 | assert_no_difference 'JournalDetail.count' do |
|
1008 | 1021 | i.save |
|
1009 | 1022 | end |
|
1010 | 1023 | end |
|
1011 | 1024 | end |
|
1012 | 1025 | |
|
1013 | 1026 | def test_all_dependent_issues |
|
1014 | 1027 | IssueRelation.delete_all |
|
1015 | 1028 | assert IssueRelation.create!(:issue_from => Issue.find(1), |
|
1016 | 1029 | :issue_to => Issue.find(2), |
|
1017 | 1030 | :relation_type => IssueRelation::TYPE_PRECEDES) |
|
1018 | 1031 | assert IssueRelation.create!(:issue_from => Issue.find(2), |
|
1019 | 1032 | :issue_to => Issue.find(3), |
|
1020 | 1033 | :relation_type => IssueRelation::TYPE_PRECEDES) |
|
1021 | 1034 | assert IssueRelation.create!(:issue_from => Issue.find(3), |
|
1022 | 1035 | :issue_to => Issue.find(8), |
|
1023 | 1036 | :relation_type => IssueRelation::TYPE_PRECEDES) |
|
1024 | 1037 | |
|
1025 | 1038 | assert_equal [2, 3, 8], Issue.find(1).all_dependent_issues.collect(&:id).sort |
|
1026 | 1039 | end |
|
1027 | 1040 | |
|
1028 | 1041 | def test_all_dependent_issues_with_persistent_circular_dependency |
|
1029 | 1042 | IssueRelation.delete_all |
|
1030 | 1043 | assert IssueRelation.create!(:issue_from => Issue.find(1), |
|
1031 | 1044 | :issue_to => Issue.find(2), |
|
1032 | 1045 | :relation_type => IssueRelation::TYPE_PRECEDES) |
|
1033 | 1046 | assert IssueRelation.create!(:issue_from => Issue.find(2), |
|
1034 | 1047 | :issue_to => Issue.find(3), |
|
1035 | 1048 | :relation_type => IssueRelation::TYPE_PRECEDES) |
|
1036 | 1049 | |
|
1037 | 1050 | r = IssueRelation.create!(:issue_from => Issue.find(3), |
|
1038 | 1051 | :issue_to => Issue.find(7), |
|
1039 | 1052 | :relation_type => IssueRelation::TYPE_PRECEDES) |
|
1040 | 1053 | IssueRelation.update_all("issue_to_id = 1", ["id = ?", r.id]) |
|
1041 | 1054 | |
|
1042 | 1055 | assert_equal [2, 3], Issue.find(1).all_dependent_issues.collect(&:id).sort |
|
1043 | 1056 | end |
|
1044 | 1057 | |
|
1045 | 1058 | def test_all_dependent_issues_with_persistent_multiple_circular_dependencies |
|
1046 | 1059 | IssueRelation.delete_all |
|
1047 | 1060 | assert IssueRelation.create!(:issue_from => Issue.find(1), |
|
1048 | 1061 | :issue_to => Issue.find(2), |
|
1049 | 1062 | :relation_type => IssueRelation::TYPE_RELATES) |
|
1050 | 1063 | assert IssueRelation.create!(:issue_from => Issue.find(2), |
|
1051 | 1064 | :issue_to => Issue.find(3), |
|
1052 | 1065 | :relation_type => IssueRelation::TYPE_RELATES) |
|
1053 | 1066 | assert IssueRelation.create!(:issue_from => Issue.find(3), |
|
1054 | 1067 | :issue_to => Issue.find(8), |
|
1055 | 1068 | :relation_type => IssueRelation::TYPE_RELATES) |
|
1056 | 1069 | |
|
1057 | 1070 | r = IssueRelation.create!(:issue_from => Issue.find(8), |
|
1058 | 1071 | :issue_to => Issue.find(7), |
|
1059 | 1072 | :relation_type => IssueRelation::TYPE_RELATES) |
|
1060 | 1073 | IssueRelation.update_all("issue_to_id = 2", ["id = ?", r.id]) |
|
1061 | 1074 | |
|
1062 | 1075 | r = IssueRelation.create!(:issue_from => Issue.find(3), |
|
1063 | 1076 | :issue_to => Issue.find(7), |
|
1064 | 1077 | :relation_type => IssueRelation::TYPE_RELATES) |
|
1065 | 1078 | IssueRelation.update_all("issue_to_id = 1", ["id = ?", r.id]) |
|
1066 | 1079 | |
|
1067 | 1080 | assert_equal [2, 3, 8], Issue.find(1).all_dependent_issues.collect(&:id).sort |
|
1068 | 1081 | end |
|
1069 | 1082 | |
|
1070 | 1083 | context "#done_ratio" do |
|
1071 | 1084 | setup do |
|
1072 | 1085 | @issue = Issue.find(1) |
|
1073 | 1086 | @issue_status = IssueStatus.find(1) |
|
1074 | 1087 | @issue_status.update_attribute(:default_done_ratio, 50) |
|
1075 | 1088 | @issue2 = Issue.find(2) |
|
1076 | 1089 | @issue_status2 = IssueStatus.find(2) |
|
1077 | 1090 | @issue_status2.update_attribute(:default_done_ratio, 0) |
|
1078 | 1091 | end |
|
1079 | 1092 | |
|
1080 | 1093 | teardown do |
|
1081 | 1094 | Setting.issue_done_ratio = 'issue_field' |
|
1082 | 1095 | end |
|
1083 | 1096 | |
|
1084 | 1097 | context "with Setting.issue_done_ratio using the issue_field" do |
|
1085 | 1098 | setup do |
|
1086 | 1099 | Setting.issue_done_ratio = 'issue_field' |
|
1087 | 1100 | end |
|
1088 | 1101 | |
|
1089 | 1102 | should "read the issue's field" do |
|
1090 | 1103 | assert_equal 0, @issue.done_ratio |
|
1091 | 1104 | assert_equal 30, @issue2.done_ratio |
|
1092 | 1105 | end |
|
1093 | 1106 | end |
|
1094 | 1107 | |
|
1095 | 1108 | context "with Setting.issue_done_ratio using the issue_status" do |
|
1096 | 1109 | setup do |
|
1097 | 1110 | Setting.issue_done_ratio = 'issue_status' |
|
1098 | 1111 | end |
|
1099 | 1112 | |
|
1100 | 1113 | should "read the Issue Status's default done ratio" do |
|
1101 | 1114 | assert_equal 50, @issue.done_ratio |
|
1102 | 1115 | assert_equal 0, @issue2.done_ratio |
|
1103 | 1116 | end |
|
1104 | 1117 | end |
|
1105 | 1118 | end |
|
1106 | 1119 | |
|
1107 | 1120 | context "#update_done_ratio_from_issue_status" do |
|
1108 | 1121 | setup do |
|
1109 | 1122 | @issue = Issue.find(1) |
|
1110 | 1123 | @issue_status = IssueStatus.find(1) |
|
1111 | 1124 | @issue_status.update_attribute(:default_done_ratio, 50) |
|
1112 | 1125 | @issue2 = Issue.find(2) |
|
1113 | 1126 | @issue_status2 = IssueStatus.find(2) |
|
1114 | 1127 | @issue_status2.update_attribute(:default_done_ratio, 0) |
|
1115 | 1128 | end |
|
1116 | 1129 | |
|
1117 | 1130 | context "with Setting.issue_done_ratio using the issue_field" do |
|
1118 | 1131 | setup do |
|
1119 | 1132 | Setting.issue_done_ratio = 'issue_field' |
|
1120 | 1133 | end |
|
1121 | 1134 | |
|
1122 | 1135 | should "not change the issue" do |
|
1123 | 1136 | @issue.update_done_ratio_from_issue_status |
|
1124 | 1137 | @issue2.update_done_ratio_from_issue_status |
|
1125 | 1138 | |
|
1126 | 1139 | assert_equal 0, @issue.read_attribute(:done_ratio) |
|
1127 | 1140 | assert_equal 30, @issue2.read_attribute(:done_ratio) |
|
1128 | 1141 | end |
|
1129 | 1142 | end |
|
1130 | 1143 | |
|
1131 | 1144 | context "with Setting.issue_done_ratio using the issue_status" do |
|
1132 | 1145 | setup do |
|
1133 | 1146 | Setting.issue_done_ratio = 'issue_status' |
|
1134 | 1147 | end |
|
1135 | 1148 | |
|
1136 | 1149 | should "change the issue's done ratio" do |
|
1137 | 1150 | @issue.update_done_ratio_from_issue_status |
|
1138 | 1151 | @issue2.update_done_ratio_from_issue_status |
|
1139 | 1152 | |
|
1140 | 1153 | assert_equal 50, @issue.read_attribute(:done_ratio) |
|
1141 | 1154 | assert_equal 0, @issue2.read_attribute(:done_ratio) |
|
1142 | 1155 | end |
|
1143 | 1156 | end |
|
1144 | 1157 | end |
|
1145 | 1158 | |
|
1146 | 1159 | test "#by_tracker" do |
|
1147 | 1160 | User.current = User.anonymous |
|
1148 | 1161 | groups = Issue.by_tracker(Project.find(1)) |
|
1149 | 1162 | assert_equal 3, groups.size |
|
1150 | 1163 | assert_equal 7, groups.inject(0) {|sum, group| sum + group['total'].to_i} |
|
1151 | 1164 | end |
|
1152 | 1165 | |
|
1153 | 1166 | test "#by_version" do |
|
1154 | 1167 | User.current = User.anonymous |
|
1155 | 1168 | groups = Issue.by_version(Project.find(1)) |
|
1156 | 1169 | assert_equal 3, groups.size |
|
1157 | 1170 | assert_equal 3, groups.inject(0) {|sum, group| sum + group['total'].to_i} |
|
1158 | 1171 | end |
|
1159 | 1172 | |
|
1160 | 1173 | test "#by_priority" do |
|
1161 | 1174 | User.current = User.anonymous |
|
1162 | 1175 | groups = Issue.by_priority(Project.find(1)) |
|
1163 | 1176 | assert_equal 4, groups.size |
|
1164 | 1177 | assert_equal 7, groups.inject(0) {|sum, group| sum + group['total'].to_i} |
|
1165 | 1178 | end |
|
1166 | 1179 | |
|
1167 | 1180 | test "#by_category" do |
|
1168 | 1181 | User.current = User.anonymous |
|
1169 | 1182 | groups = Issue.by_category(Project.find(1)) |
|
1170 | 1183 | assert_equal 2, groups.size |
|
1171 | 1184 | assert_equal 3, groups.inject(0) {|sum, group| sum + group['total'].to_i} |
|
1172 | 1185 | end |
|
1173 | 1186 | |
|
1174 | 1187 | test "#by_assigned_to" do |
|
1175 | 1188 | User.current = User.anonymous |
|
1176 | 1189 | groups = Issue.by_assigned_to(Project.find(1)) |
|
1177 | 1190 | assert_equal 2, groups.size |
|
1178 | 1191 | assert_equal 2, groups.inject(0) {|sum, group| sum + group['total'].to_i} |
|
1179 | 1192 | end |
|
1180 | 1193 | |
|
1181 | 1194 | test "#by_author" do |
|
1182 | 1195 | User.current = User.anonymous |
|
1183 | 1196 | groups = Issue.by_author(Project.find(1)) |
|
1184 | 1197 | assert_equal 4, groups.size |
|
1185 | 1198 | assert_equal 7, groups.inject(0) {|sum, group| sum + group['total'].to_i} |
|
1186 | 1199 | end |
|
1187 | 1200 | |
|
1188 | 1201 | test "#by_subproject" do |
|
1189 | 1202 | User.current = User.anonymous |
|
1190 | 1203 | groups = Issue.by_subproject(Project.find(1)) |
|
1191 | 1204 | # Private descendant not visible |
|
1192 | 1205 | assert_equal 1, groups.size |
|
1193 | 1206 | assert_equal 2, groups.inject(0) {|sum, group| sum + group['total'].to_i} |
|
1194 | 1207 | end |
|
1195 | 1208 | |
|
1196 | 1209 | def test_recently_updated_with_limit_scopes |
|
1197 | 1210 | #should return the last updated issue |
|
1198 | 1211 | assert_equal 1, Issue.recently_updated.with_limit(1).length |
|
1199 | 1212 | assert_equal Issue.find(:first, :order => "updated_on DESC"), Issue.recently_updated.with_limit(1).first |
|
1200 | 1213 | end |
|
1201 | 1214 | |
|
1202 | 1215 | def test_on_active_projects_scope |
|
1203 | 1216 | assert Project.find(2).archive |
|
1204 | 1217 | |
|
1205 | 1218 | before = Issue.on_active_project.length |
|
1206 | 1219 | # test inclusion to results |
|
1207 | 1220 | issue = Issue.generate_for_project!(Project.find(1), :tracker => Project.find(2).trackers.first) |
|
1208 | 1221 | assert_equal before + 1, Issue.on_active_project.length |
|
1209 | 1222 | |
|
1210 | 1223 | # Move to an archived project |
|
1211 | 1224 | issue.project = Project.find(2) |
|
1212 | 1225 | assert issue.save |
|
1213 | 1226 | assert_equal before, Issue.on_active_project.length |
|
1214 | 1227 | end |
|
1215 | 1228 | |
|
1216 | 1229 | context "Issue#recipients" do |
|
1217 | 1230 | setup do |
|
1218 | 1231 | @project = Project.find(1) |
|
1219 | 1232 | @author = User.generate! |
|
1220 | 1233 | @assignee = User.generate! |
|
1221 | 1234 | @issue = Issue.generate_for_project!(@project, :assigned_to => @assignee, :author => @author) |
|
1222 | 1235 | end |
|
1223 | 1236 | |
|
1224 | 1237 | should "include project recipients" do |
|
1225 | 1238 | assert @project.recipients.present? |
|
1226 | 1239 | @project.recipients.each do |project_recipient| |
|
1227 | 1240 | assert @issue.recipients.include?(project_recipient) |
|
1228 | 1241 | end |
|
1229 | 1242 | end |
|
1230 | 1243 | |
|
1231 | 1244 | should "include the author if the author is active" do |
|
1232 | 1245 | assert @issue.author, "No author set for Issue" |
|
1233 | 1246 | assert @issue.recipients.include?(@issue.author.mail) |
|
1234 | 1247 | end |
|
1235 | 1248 | |
|
1236 | 1249 | should "include the assigned to user if the assigned to user is active" do |
|
1237 | 1250 | assert @issue.assigned_to, "No assigned_to set for Issue" |
|
1238 | 1251 | assert @issue.recipients.include?(@issue.assigned_to.mail) |
|
1239 | 1252 | end |
|
1240 | 1253 | |
|
1241 | 1254 | should "not include users who opt out of all email" do |
|
1242 | 1255 | @author.update_attribute(:mail_notification, :none) |
|
1243 | 1256 | |
|
1244 | 1257 | assert !@issue.recipients.include?(@issue.author.mail) |
|
1245 | 1258 | end |
|
1246 | 1259 | |
|
1247 | 1260 | should "not include the issue author if they are only notified of assigned issues" do |
|
1248 | 1261 | @author.update_attribute(:mail_notification, :only_assigned) |
|
1249 | 1262 | |
|
1250 | 1263 | assert !@issue.recipients.include?(@issue.author.mail) |
|
1251 | 1264 | end |
|
1252 | 1265 | |
|
1253 | 1266 | should "not include the assigned user if they are only notified of owned issues" do |
|
1254 | 1267 | @assignee.update_attribute(:mail_notification, :only_owner) |
|
1255 | 1268 | |
|
1256 | 1269 | assert !@issue.recipients.include?(@issue.assigned_to.mail) |
|
1257 | 1270 | end |
|
1258 | 1271 | end |
|
1259 | 1272 | |
|
1260 | 1273 | def test_last_journal_id_with_journals_should_return_the_journal_id |
|
1261 | 1274 | assert_equal 2, Issue.find(1).last_journal_id |
|
1262 | 1275 | end |
|
1263 | 1276 | |
|
1264 | 1277 | def test_last_journal_id_without_journals_should_return_nil |
|
1265 | 1278 | assert_nil Issue.find(3).last_journal_id |
|
1266 | 1279 | end |
|
1267 | 1280 | end |
General Comments 0
You need to be logged in to leave comments.
Login now