##// END OF EJS Templates
Fixed: No validation errors when entering an invalid "Parent task" (#11979)....
Jean-Philippe Lang -
r10404:2b797fa82fdd
parent child
Show More
@@ -1,1354 +1,1360
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 :visible_journals,
32 32 :class_name => 'Journal',
33 33 :as => :journalized,
34 34 :conditions => Proc.new {
35 35 ["(#{Journal.table_name}.private_notes = ? OR (#{Project.allowed_to_condition(User.current, :view_private_notes)}))", false]
36 36 },
37 37 :readonly => true
38 38
39 39 has_many :time_entries, :dependent => :delete_all
40 40 has_and_belongs_to_many :changesets, :order => "#{Changeset.table_name}.committed_on ASC, #{Changeset.table_name}.id ASC"
41 41
42 42 has_many :relations_from, :class_name => 'IssueRelation', :foreign_key => 'issue_from_id', :dependent => :delete_all
43 43 has_many :relations_to, :class_name => 'IssueRelation', :foreign_key => 'issue_to_id', :dependent => :delete_all
44 44
45 45 acts_as_nested_set :scope => 'root_id', :dependent => :destroy
46 46 acts_as_attachable :after_add => :attachment_added, :after_remove => :attachment_removed
47 47 acts_as_customizable
48 48 acts_as_watchable
49 49 acts_as_searchable :columns => ['subject', "#{table_name}.description", "#{Journal.table_name}.notes"],
50 50 :include => [:project, :visible_journals],
51 51 # sort by id so that limited eager loading doesn't break with postgresql
52 52 :order_column => "#{table_name}.id"
53 53 acts_as_event :title => Proc.new {|o| "#{o.tracker.name} ##{o.id} (#{o.status}): #{o.subject}"},
54 54 :url => Proc.new {|o| {:controller => 'issues', :action => 'show', :id => o.id}},
55 55 :type => Proc.new {|o| 'issue' + (o.closed? ? ' closed' : '') }
56 56
57 57 acts_as_activity_provider :find_options => {:include => [:project, :author, :tracker]},
58 58 :author_key => :author_id
59 59
60 60 DONE_RATIO_OPTIONS = %w(issue_field issue_status)
61 61
62 62 attr_reader :current_journal
63 63 delegate :notes, :notes=, :private_notes, :private_notes=, :to => :current_journal, :allow_nil => true
64 64
65 65 validates_presence_of :subject, :priority, :project, :tracker, :author, :status
66 66
67 67 validates_length_of :subject, :maximum => 255
68 68 validates_inclusion_of :done_ratio, :in => 0..100
69 69 validates_numericality_of :estimated_hours, :allow_nil => true
70 70 validate :validate_issue, :validate_required_fields
71 71
72 72 scope :visible,
73 73 lambda {|*args| { :include => :project,
74 74 :conditions => Issue.visible_condition(args.shift || User.current, *args) } }
75 75
76 76 scope :open, lambda {|*args|
77 77 is_closed = args.size > 0 ? !args.first : false
78 78 {:conditions => ["#{IssueStatus.table_name}.is_closed = ?", is_closed], :include => :status}
79 79 }
80 80
81 81 scope :recently_updated, :order => "#{Issue.table_name}.updated_on DESC"
82 82 scope :on_active_project, :include => [:status, :project, :tracker],
83 83 :conditions => ["#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"]
84 84
85 85 before_create :default_assign
86 86 before_save :close_duplicates, :update_done_ratio_from_issue_status, :force_updated_on_change
87 87 after_save {|issue| issue.send :after_project_change if !issue.id_changed? && issue.project_id_changed?}
88 88 after_save :reschedule_following_issues, :update_nested_set_attributes, :update_parent_attributes, :create_journal
89 89 # Should be after_create but would be called before previous after_save callbacks
90 90 after_save :after_create_from_copy
91 91 after_destroy :update_parent_attributes
92 92
93 93 # Returns a SQL conditions string used to find all issues visible by the specified user
94 94 def self.visible_condition(user, options={})
95 95 Project.allowed_to_condition(user, :view_issues, options) do |role, user|
96 96 if user.logged?
97 97 case role.issues_visibility
98 98 when 'all'
99 99 nil
100 100 when 'default'
101 101 user_ids = [user.id] + user.groups.map(&:id)
102 102 "(#{table_name}.is_private = #{connection.quoted_false} OR #{table_name}.author_id = #{user.id} OR #{table_name}.assigned_to_id IN (#{user_ids.join(',')}))"
103 103 when 'own'
104 104 user_ids = [user.id] + user.groups.map(&:id)
105 105 "(#{table_name}.author_id = #{user.id} OR #{table_name}.assigned_to_id IN (#{user_ids.join(',')}))"
106 106 else
107 107 '1=0'
108 108 end
109 109 else
110 110 "(#{table_name}.is_private = #{connection.quoted_false})"
111 111 end
112 112 end
113 113 end
114 114
115 115 # Returns true if usr or current user is allowed to view the issue
116 116 def visible?(usr=nil)
117 117 (usr || User.current).allowed_to?(:view_issues, self.project) do |role, user|
118 118 if user.logged?
119 119 case role.issues_visibility
120 120 when 'all'
121 121 true
122 122 when 'default'
123 123 !self.is_private? || (self.author == user || user.is_or_belongs_to?(assigned_to))
124 124 when 'own'
125 125 self.author == user || user.is_or_belongs_to?(assigned_to)
126 126 else
127 127 false
128 128 end
129 129 else
130 130 !self.is_private?
131 131 end
132 132 end
133 133 end
134 134
135 135 def initialize(attributes=nil, *args)
136 136 super
137 137 if new_record?
138 138 # set default values for new records only
139 139 self.status ||= IssueStatus.default
140 140 self.priority ||= IssuePriority.default
141 141 self.watcher_user_ids = []
142 142 end
143 143 end
144 144
145 145 # AR#Persistence#destroy would raise and RecordNotFound exception
146 146 # if the issue was already deleted or updated (non matching lock_version).
147 147 # This is a problem when bulk deleting issues or deleting a project
148 148 # (because an issue may already be deleted if its parent was deleted
149 149 # first).
150 150 # The issue is reloaded by the nested_set before being deleted so
151 151 # the lock_version condition should not be an issue but we handle it.
152 152 def destroy
153 153 super
154 154 rescue ActiveRecord::RecordNotFound
155 155 # Stale or already deleted
156 156 begin
157 157 reload
158 158 rescue ActiveRecord::RecordNotFound
159 159 # The issue was actually already deleted
160 160 @destroyed = true
161 161 return freeze
162 162 end
163 163 # The issue was stale, retry to destroy
164 164 super
165 165 end
166 166
167 167 def reload(*args)
168 168 @workflow_rule_by_attribute = nil
169 169 @assignable_versions = nil
170 170 super
171 171 end
172 172
173 173 # Overrides Redmine::Acts::Customizable::InstanceMethods#available_custom_fields
174 174 def available_custom_fields
175 175 (project && tracker) ? (project.all_issue_custom_fields & tracker.custom_fields.all) : []
176 176 end
177 177
178 178 # Copies attributes from another issue, arg can be an id or an Issue
179 179 def copy_from(arg, options={})
180 180 issue = arg.is_a?(Issue) ? arg : Issue.visible.find(arg)
181 181 self.attributes = issue.attributes.dup.except("id", "root_id", "parent_id", "lft", "rgt", "created_on", "updated_on")
182 182 self.custom_field_values = issue.custom_field_values.inject({}) {|h,v| h[v.custom_field_id] = v.value; h}
183 183 self.status = issue.status
184 184 self.author = User.current
185 185 unless options[:attachments] == false
186 186 self.attachments = issue.attachments.map do |attachement|
187 187 attachement.copy(:container => self)
188 188 end
189 189 end
190 190 @copied_from = issue
191 191 @copy_options = options
192 192 self
193 193 end
194 194
195 195 # Returns an unsaved copy of the issue
196 196 def copy(attributes=nil, copy_options={})
197 197 copy = self.class.new.copy_from(self, copy_options)
198 198 copy.attributes = attributes if attributes
199 199 copy
200 200 end
201 201
202 202 # Returns true if the issue is a copy
203 203 def copy?
204 204 @copied_from.present?
205 205 end
206 206
207 207 # Moves/copies an issue to a new project and tracker
208 208 # Returns the moved/copied issue on success, false on failure
209 209 def move_to_project(new_project, new_tracker=nil, options={})
210 210 ActiveSupport::Deprecation.warn "Issue#move_to_project is deprecated, use #project= instead."
211 211
212 212 if options[:copy]
213 213 issue = self.copy
214 214 else
215 215 issue = self
216 216 end
217 217
218 218 issue.init_journal(User.current, options[:notes])
219 219
220 220 # Preserve previous behaviour
221 221 # #move_to_project doesn't change tracker automatically
222 222 issue.send :project=, new_project, true
223 223 if new_tracker
224 224 issue.tracker = new_tracker
225 225 end
226 226 # Allow bulk setting of attributes on the issue
227 227 if options[:attributes]
228 228 issue.attributes = options[:attributes]
229 229 end
230 230
231 231 issue.save ? issue : false
232 232 end
233 233
234 234 def status_id=(sid)
235 235 self.status = nil
236 236 result = write_attribute(:status_id, sid)
237 237 @workflow_rule_by_attribute = nil
238 238 result
239 239 end
240 240
241 241 def priority_id=(pid)
242 242 self.priority = nil
243 243 write_attribute(:priority_id, pid)
244 244 end
245 245
246 246 def category_id=(cid)
247 247 self.category = nil
248 248 write_attribute(:category_id, cid)
249 249 end
250 250
251 251 def fixed_version_id=(vid)
252 252 self.fixed_version = nil
253 253 write_attribute(:fixed_version_id, vid)
254 254 end
255 255
256 256 def tracker_id=(tid)
257 257 self.tracker = nil
258 258 result = write_attribute(:tracker_id, tid)
259 259 @custom_field_values = nil
260 260 @workflow_rule_by_attribute = nil
261 261 result
262 262 end
263 263
264 264 def project_id=(project_id)
265 265 if project_id.to_s != self.project_id.to_s
266 266 self.project = (project_id.present? ? Project.find_by_id(project_id) : nil)
267 267 end
268 268 end
269 269
270 270 def project=(project, keep_tracker=false)
271 271 project_was = self.project
272 272 write_attribute(:project_id, project ? project.id : nil)
273 273 association_instance_set('project', project)
274 274 if project_was && project && project_was != project
275 275 @assignable_versions = nil
276 276
277 277 unless keep_tracker || project.trackers.include?(tracker)
278 278 self.tracker = project.trackers.first
279 279 end
280 280 # Reassign to the category with same name if any
281 281 if category
282 282 self.category = project.issue_categories.find_by_name(category.name)
283 283 end
284 284 # Keep the fixed_version if it's still valid in the new_project
285 285 if fixed_version && fixed_version.project != project && !project.shared_versions.include?(fixed_version)
286 286 self.fixed_version = nil
287 287 end
288 288 # Clear the parent task if it's no longer valid
289 289 unless valid_parent_project?
290 290 self.parent_issue_id = nil
291 291 end
292 292 @custom_field_values = nil
293 293 end
294 294 end
295 295
296 296 def description=(arg)
297 297 if arg.is_a?(String)
298 298 arg = arg.gsub(/(\r\n|\n|\r)/, "\r\n")
299 299 end
300 300 write_attribute(:description, arg)
301 301 end
302 302
303 303 # Overrides assign_attributes so that project and tracker get assigned first
304 304 def assign_attributes_with_project_and_tracker_first(new_attributes, *args)
305 305 return if new_attributes.nil?
306 306 attrs = new_attributes.dup
307 307 attrs.stringify_keys!
308 308
309 309 %w(project project_id tracker tracker_id).each do |attr|
310 310 if attrs.has_key?(attr)
311 311 send "#{attr}=", attrs.delete(attr)
312 312 end
313 313 end
314 314 send :assign_attributes_without_project_and_tracker_first, attrs, *args
315 315 end
316 316 # Do not redefine alias chain on reload (see #4838)
317 317 alias_method_chain(:assign_attributes, :project_and_tracker_first) unless method_defined?(:assign_attributes_without_project_and_tracker_first)
318 318
319 319 def estimated_hours=(h)
320 320 write_attribute :estimated_hours, (h.is_a?(String) ? h.to_hours : h)
321 321 end
322 322
323 323 safe_attributes 'project_id',
324 324 :if => lambda {|issue, user|
325 325 if issue.new_record?
326 326 issue.copy?
327 327 elsif user.allowed_to?(:move_issues, issue.project)
328 328 projects = Issue.allowed_target_projects_on_move(user)
329 329 projects.include?(issue.project) && projects.size > 1
330 330 end
331 331 }
332 332
333 333 safe_attributes 'tracker_id',
334 334 'status_id',
335 335 'category_id',
336 336 'assigned_to_id',
337 337 'priority_id',
338 338 'fixed_version_id',
339 339 'subject',
340 340 'description',
341 341 'start_date',
342 342 'due_date',
343 343 'done_ratio',
344 344 'estimated_hours',
345 345 'custom_field_values',
346 346 'custom_fields',
347 347 'lock_version',
348 348 'notes',
349 349 :if => lambda {|issue, user| issue.new_record? || user.allowed_to?(:edit_issues, issue.project) }
350 350
351 351 safe_attributes 'status_id',
352 352 'assigned_to_id',
353 353 'fixed_version_id',
354 354 'done_ratio',
355 355 'lock_version',
356 356 'notes',
357 357 :if => lambda {|issue, user| issue.new_statuses_allowed_to(user).any? }
358 358
359 359 safe_attributes 'notes',
360 360 :if => lambda {|issue, user| user.allowed_to?(:add_issue_notes, issue.project)}
361 361
362 362 safe_attributes 'private_notes',
363 363 :if => lambda {|issue, user| !issue.new_record? && user.allowed_to?(:set_notes_private, issue.project)}
364 364
365 365 safe_attributes 'watcher_user_ids',
366 366 :if => lambda {|issue, user| issue.new_record? && user.allowed_to?(:add_issue_watchers, issue.project)}
367 367
368 368 safe_attributes 'is_private',
369 369 :if => lambda {|issue, user|
370 370 user.allowed_to?(:set_issues_private, issue.project) ||
371 371 (issue.author == user && user.allowed_to?(:set_own_issues_private, issue.project))
372 372 }
373 373
374 374 safe_attributes 'parent_issue_id',
375 375 :if => lambda {|issue, user| (issue.new_record? || user.allowed_to?(:edit_issues, issue.project)) &&
376 376 user.allowed_to?(:manage_subtasks, issue.project)}
377 377
378 378 def safe_attribute_names(user=nil)
379 379 names = super
380 380 names -= disabled_core_fields
381 381 names -= read_only_attribute_names(user)
382 382 names
383 383 end
384 384
385 385 # Safely sets attributes
386 386 # Should be called from controllers instead of #attributes=
387 387 # attr_accessible is too rough because we still want things like
388 388 # Issue.new(:project => foo) to work
389 389 def safe_attributes=(attrs, user=User.current)
390 390 return unless attrs.is_a?(Hash)
391 391
392 392 attrs = attrs.dup
393 393
394 394 # Project and Tracker must be set before since new_statuses_allowed_to depends on it.
395 395 if (p = attrs.delete('project_id')) && safe_attribute?('project_id')
396 396 if allowed_target_projects(user).collect(&:id).include?(p.to_i)
397 397 self.project_id = p
398 398 end
399 399 end
400 400
401 401 if (t = attrs.delete('tracker_id')) && safe_attribute?('tracker_id')
402 402 self.tracker_id = t
403 403 end
404 404
405 405 if (s = attrs.delete('status_id')) && safe_attribute?('status_id')
406 406 if new_statuses_allowed_to(user).collect(&:id).include?(s.to_i)
407 407 self.status_id = s
408 408 end
409 409 end
410 410
411 411 attrs = delete_unsafe_attributes(attrs, user)
412 412 return if attrs.empty?
413 413
414 414 unless leaf?
415 415 attrs.reject! {|k,v| %w(priority_id done_ratio start_date due_date estimated_hours).include?(k)}
416 416 end
417 417
418 418 if attrs['parent_issue_id'].present?
419 attrs.delete('parent_issue_id') unless Issue.visible(user).exists?(attrs['parent_issue_id'].to_i)
419 unless Issue.visible(user).exists?(attrs['parent_issue_id'].to_i)
420 @invalid_parent_issue_id = attrs.delete('parent_issue_id')
421 end
420 422 end
421 423
422 424 if attrs['custom_field_values'].present?
423 425 attrs['custom_field_values'] = attrs['custom_field_values'].reject {|k, v| read_only_attribute_names(user).include? k.to_s}
424 426 end
425 427
426 428 if attrs['custom_fields'].present?
427 429 attrs['custom_fields'] = attrs['custom_fields'].reject {|c| read_only_attribute_names(user).include? c['id'].to_s}
428 430 end
429 431
430 432 # mass-assignment security bypass
431 433 assign_attributes attrs, :without_protection => true
432 434 end
433 435
434 436 def disabled_core_fields
435 437 tracker ? tracker.disabled_core_fields : []
436 438 end
437 439
438 440 # Returns the custom_field_values that can be edited by the given user
439 441 def editable_custom_field_values(user=nil)
440 442 custom_field_values.reject do |value|
441 443 read_only_attribute_names(user).include?(value.custom_field_id.to_s)
442 444 end
443 445 end
444 446
445 447 # Returns the names of attributes that are read-only for user or the current user
446 448 # For users with multiple roles, the read-only fields are the intersection of
447 449 # read-only fields of each role
448 450 # The result is an array of strings where sustom fields are represented with their ids
449 451 #
450 452 # Examples:
451 453 # issue.read_only_attribute_names # => ['due_date', '2']
452 454 # issue.read_only_attribute_names(user) # => []
453 455 def read_only_attribute_names(user=nil)
454 456 workflow_rule_by_attribute(user).reject {|attr, rule| rule != 'readonly'}.keys
455 457 end
456 458
457 459 # Returns the names of required attributes for user or the current user
458 460 # For users with multiple roles, the required fields are the intersection of
459 461 # required fields of each role
460 462 # The result is an array of strings where sustom fields are represented with their ids
461 463 #
462 464 # Examples:
463 465 # issue.required_attribute_names # => ['due_date', '2']
464 466 # issue.required_attribute_names(user) # => []
465 467 def required_attribute_names(user=nil)
466 468 workflow_rule_by_attribute(user).reject {|attr, rule| rule != 'required'}.keys
467 469 end
468 470
469 471 # Returns true if the attribute is required for user
470 472 def required_attribute?(name, user=nil)
471 473 required_attribute_names(user).include?(name.to_s)
472 474 end
473 475
474 476 # Returns a hash of the workflow rule by attribute for the given user
475 477 #
476 478 # Examples:
477 479 # issue.workflow_rule_by_attribute # => {'due_date' => 'required', 'start_date' => 'readonly'}
478 480 def workflow_rule_by_attribute(user=nil)
479 481 return @workflow_rule_by_attribute if @workflow_rule_by_attribute && user.nil?
480 482
481 483 user_real = user || User.current
482 484 roles = user_real.admin ? Role.all : user_real.roles_for_project(project)
483 485 return {} if roles.empty?
484 486
485 487 result = {}
486 488 workflow_permissions = WorkflowPermission.where(:tracker_id => tracker_id, :old_status_id => status_id, :role_id => roles.map(&:id)).all
487 489 if workflow_permissions.any?
488 490 workflow_rules = workflow_permissions.inject({}) do |h, wp|
489 491 h[wp.field_name] ||= []
490 492 h[wp.field_name] << wp.rule
491 493 h
492 494 end
493 495 workflow_rules.each do |attr, rules|
494 496 next if rules.size < roles.size
495 497 uniq_rules = rules.uniq
496 498 if uniq_rules.size == 1
497 499 result[attr] = uniq_rules.first
498 500 else
499 501 result[attr] = 'required'
500 502 end
501 503 end
502 504 end
503 505 @workflow_rule_by_attribute = result if user.nil?
504 506 result
505 507 end
506 508 private :workflow_rule_by_attribute
507 509
508 510 def done_ratio
509 511 if Issue.use_status_for_done_ratio? && status && status.default_done_ratio
510 512 status.default_done_ratio
511 513 else
512 514 read_attribute(:done_ratio)
513 515 end
514 516 end
515 517
516 518 def self.use_status_for_done_ratio?
517 519 Setting.issue_done_ratio == 'issue_status'
518 520 end
519 521
520 522 def self.use_field_for_done_ratio?
521 523 Setting.issue_done_ratio == 'issue_field'
522 524 end
523 525
524 526 def validate_issue
525 527 if self.due_date.nil? && @attributes['due_date'] && !@attributes['due_date'].empty?
526 528 errors.add :due_date, :not_a_date
527 529 end
528 530
529 531 if self.due_date and self.start_date and self.due_date < self.start_date
530 532 errors.add :due_date, :greater_than_start_date
531 533 end
532 534
533 535 if start_date && soonest_start && start_date < soonest_start
534 536 errors.add :start_date, :invalid
535 537 end
536 538
537 539 if fixed_version
538 540 if !assignable_versions.include?(fixed_version)
539 541 errors.add :fixed_version_id, :inclusion
540 542 elsif reopened? && fixed_version.closed?
541 543 errors.add :base, I18n.t(:error_can_not_reopen_issue_on_closed_version)
542 544 end
543 545 end
544 546
545 547 # Checks that the issue can not be added/moved to a disabled tracker
546 548 if project && (tracker_id_changed? || project_id_changed?)
547 549 unless project.trackers.include?(tracker)
548 550 errors.add :tracker_id, :inclusion
549 551 end
550 552 end
551 553
552 554 # Checks parent issue assignment
553 if @parent_issue
555 if @invalid_parent_issue_id.present?
556 errors.add :parent_issue_id, :invalid
557 elsif @parent_issue
554 558 if !valid_parent_project?(@parent_issue)
555 559 errors.add :parent_issue_id, :invalid
556 560 elsif !new_record?
557 561 # moving an existing issue
558 562 if @parent_issue.root_id != root_id
559 563 # we can always move to another tree
560 564 elsif move_possible?(@parent_issue)
561 565 # move accepted inside tree
562 566 else
563 567 errors.add :parent_issue_id, :invalid
564 568 end
565 569 end
566 570 end
567 571 end
568 572
569 573 # Validates the issue against additional workflow requirements
570 574 def validate_required_fields
571 575 user = new_record? ? author : current_journal.try(:user)
572 576
573 577 required_attribute_names(user).each do |attribute|
574 578 if attribute =~ /^\d+$/
575 579 attribute = attribute.to_i
576 580 v = custom_field_values.detect {|v| v.custom_field_id == attribute }
577 581 if v && v.value.blank?
578 582 errors.add :base, v.custom_field.name + ' ' + l('activerecord.errors.messages.blank')
579 583 end
580 584 else
581 585 if respond_to?(attribute) && send(attribute).blank?
582 586 errors.add attribute, :blank
583 587 end
584 588 end
585 589 end
586 590 end
587 591
588 592 # Set the done_ratio using the status if that setting is set. This will keep the done_ratios
589 593 # even if the user turns off the setting later
590 594 def update_done_ratio_from_issue_status
591 595 if Issue.use_status_for_done_ratio? && status && status.default_done_ratio
592 596 self.done_ratio = status.default_done_ratio
593 597 end
594 598 end
595 599
596 600 def init_journal(user, notes = "")
597 601 @current_journal ||= Journal.new(:journalized => self, :user => user, :notes => notes)
598 602 if new_record?
599 603 @current_journal.notify = false
600 604 else
601 605 @attributes_before_change = attributes.dup
602 606 @custom_values_before_change = {}
603 607 self.custom_field_values.each {|c| @custom_values_before_change.store c.custom_field_id, c.value }
604 608 end
605 609 @current_journal
606 610 end
607 611
608 612 # Returns the id of the last journal or nil
609 613 def last_journal_id
610 614 if new_record?
611 615 nil
612 616 else
613 617 journals.maximum(:id)
614 618 end
615 619 end
616 620
617 621 # Returns a scope for journals that have an id greater than journal_id
618 622 def journals_after(journal_id)
619 623 scope = journals.reorder("#{Journal.table_name}.id ASC")
620 624 if journal_id.present?
621 625 scope = scope.where("#{Journal.table_name}.id > ?", journal_id.to_i)
622 626 end
623 627 scope
624 628 end
625 629
626 630 # Return true if the issue is closed, otherwise false
627 631 def closed?
628 632 self.status.is_closed?
629 633 end
630 634
631 635 # Return true if the issue is being reopened
632 636 def reopened?
633 637 if !new_record? && status_id_changed?
634 638 status_was = IssueStatus.find_by_id(status_id_was)
635 639 status_new = IssueStatus.find_by_id(status_id)
636 640 if status_was && status_new && status_was.is_closed? && !status_new.is_closed?
637 641 return true
638 642 end
639 643 end
640 644 false
641 645 end
642 646
643 647 # Return true if the issue is being closed
644 648 def closing?
645 649 if !new_record? && status_id_changed?
646 650 status_was = IssueStatus.find_by_id(status_id_was)
647 651 status_new = IssueStatus.find_by_id(status_id)
648 652 if status_was && status_new && !status_was.is_closed? && status_new.is_closed?
649 653 return true
650 654 end
651 655 end
652 656 false
653 657 end
654 658
655 659 # Returns true if the issue is overdue
656 660 def overdue?
657 661 !due_date.nil? && (due_date < Date.today) && !status.is_closed?
658 662 end
659 663
660 664 # Is the amount of work done less than it should for the due date
661 665 def behind_schedule?
662 666 return false if start_date.nil? || due_date.nil?
663 667 done_date = start_date + ((due_date - start_date+1)* done_ratio/100).floor
664 668 return done_date <= Date.today
665 669 end
666 670
667 671 # Does this issue have children?
668 672 def children?
669 673 !leaf?
670 674 end
671 675
672 676 # Users the issue can be assigned to
673 677 def assignable_users
674 678 users = project.assignable_users
675 679 users << author if author
676 680 users << assigned_to if assigned_to
677 681 users.uniq.sort
678 682 end
679 683
680 684 # Versions that the issue can be assigned to
681 685 def assignable_versions
682 686 return @assignable_versions if @assignable_versions
683 687
684 688 versions = project.shared_versions.open.all
685 689 if fixed_version
686 690 if fixed_version_id_changed?
687 691 # nothing to do
688 692 elsif project_id_changed?
689 693 if project.shared_versions.include?(fixed_version)
690 694 versions << fixed_version
691 695 end
692 696 else
693 697 versions << fixed_version
694 698 end
695 699 end
696 700 @assignable_versions = versions.uniq.sort
697 701 end
698 702
699 703 # Returns true if this issue is blocked by another issue that is still open
700 704 def blocked?
701 705 !relations_to.detect {|ir| ir.relation_type == 'blocks' && !ir.issue_from.closed?}.nil?
702 706 end
703 707
704 708 # Returns an array of statuses that user is able to apply
705 709 def new_statuses_allowed_to(user=User.current, include_default=false)
706 710 if new_record? && @copied_from
707 711 [IssueStatus.default, @copied_from.status].compact.uniq.sort
708 712 else
709 713 initial_status = nil
710 714 if new_record?
711 715 initial_status = IssueStatus.default
712 716 elsif status_id_was
713 717 initial_status = IssueStatus.find_by_id(status_id_was)
714 718 end
715 719 initial_status ||= status
716 720
717 721 statuses = initial_status.find_new_statuses_allowed_to(
718 722 user.admin ? Role.all : user.roles_for_project(project),
719 723 tracker,
720 724 author == user,
721 725 assigned_to_id_changed? ? assigned_to_id_was == user.id : assigned_to_id == user.id
722 726 )
723 727 statuses << initial_status unless statuses.empty?
724 728 statuses << IssueStatus.default if include_default
725 729 statuses = statuses.compact.uniq.sort
726 730 blocked? ? statuses.reject {|s| s.is_closed?} : statuses
727 731 end
728 732 end
729 733
730 734 def assigned_to_was
731 735 if assigned_to_id_changed? && assigned_to_id_was.present?
732 736 @assigned_to_was ||= User.find_by_id(assigned_to_id_was)
733 737 end
734 738 end
735 739
736 740 # Returns the users that should be notified
737 741 def notified_users
738 742 notified = []
739 743 # Author and assignee are always notified unless they have been
740 744 # locked or don't want to be notified
741 745 notified << author if author
742 746 if assigned_to
743 747 notified += (assigned_to.is_a?(Group) ? assigned_to.users : [assigned_to])
744 748 end
745 749 if assigned_to_was
746 750 notified += (assigned_to_was.is_a?(Group) ? assigned_to_was.users : [assigned_to_was])
747 751 end
748 752 notified = notified.select {|u| u.active? && u.notify_about?(self)}
749 753
750 754 notified += project.notified_users
751 755 notified.uniq!
752 756 # Remove users that can not view the issue
753 757 notified.reject! {|user| !visible?(user)}
754 758 notified
755 759 end
756 760
757 761 # Returns the email addresses that should be notified
758 762 def recipients
759 763 notified_users.collect(&:mail)
760 764 end
761 765
762 766 # Returns the number of hours spent on this issue
763 767 def spent_hours
764 768 @spent_hours ||= time_entries.sum(:hours) || 0
765 769 end
766 770
767 771 # Returns the total number of hours spent on this issue and its descendants
768 772 #
769 773 # Example:
770 774 # spent_hours => 0.0
771 775 # spent_hours => 50.2
772 776 def total_spent_hours
773 777 @total_spent_hours ||= self_and_descendants.sum("#{TimeEntry.table_name}.hours",
774 778 :joins => "LEFT JOIN #{TimeEntry.table_name} ON #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id").to_f || 0.0
775 779 end
776 780
777 781 def relations
778 782 @relations ||= IssueRelations.new(self, (relations_from + relations_to).sort)
779 783 end
780 784
781 785 # Preloads relations for a collection of issues
782 786 def self.load_relations(issues)
783 787 if issues.any?
784 788 relations = IssueRelation.all(:conditions => ["issue_from_id IN (:ids) OR issue_to_id IN (:ids)", {:ids => issues.map(&:id)}])
785 789 issues.each do |issue|
786 790 issue.instance_variable_set "@relations", relations.select {|r| r.issue_from_id == issue.id || r.issue_to_id == issue.id}
787 791 end
788 792 end
789 793 end
790 794
791 795 # Preloads visible spent time for a collection of issues
792 796 def self.load_visible_spent_hours(issues, user=User.current)
793 797 if issues.any?
794 798 hours_by_issue_id = TimeEntry.visible(user).sum(:hours, :group => :issue_id)
795 799 issues.each do |issue|
796 800 issue.instance_variable_set "@spent_hours", (hours_by_issue_id[issue.id] || 0)
797 801 end
798 802 end
799 803 end
800 804
801 805 # Preloads visible relations for a collection of issues
802 806 def self.load_visible_relations(issues, user=User.current)
803 807 if issues.any?
804 808 issue_ids = issues.map(&:id)
805 809 # Relations with issue_from in given issues and visible issue_to
806 810 relations_from = IssueRelation.includes(:issue_to => [:status, :project]).where(visible_condition(user)).where(:issue_from_id => issue_ids).all
807 811 # Relations with issue_to in given issues and visible issue_from
808 812 relations_to = IssueRelation.includes(:issue_from => [:status, :project]).where(visible_condition(user)).where(:issue_to_id => issue_ids).all
809 813
810 814 issues.each do |issue|
811 815 relations =
812 816 relations_from.select {|relation| relation.issue_from_id == issue.id} +
813 817 relations_to.select {|relation| relation.issue_to_id == issue.id}
814 818
815 819 issue.instance_variable_set "@relations", IssueRelations.new(issue, relations.sort)
816 820 end
817 821 end
818 822 end
819 823
820 824 # Finds an issue relation given its id.
821 825 def find_relation(relation_id)
822 826 IssueRelation.find(relation_id, :conditions => ["issue_to_id = ? OR issue_from_id = ?", id, id])
823 827 end
824 828
825 829 def all_dependent_issues(except=[])
826 830 except << self
827 831 dependencies = []
828 832 relations_from.each do |relation|
829 833 if relation.issue_to && !except.include?(relation.issue_to)
830 834 dependencies << relation.issue_to
831 835 dependencies += relation.issue_to.all_dependent_issues(except)
832 836 end
833 837 end
834 838 dependencies
835 839 end
836 840
837 841 # Returns an array of issues that duplicate this one
838 842 def duplicates
839 843 relations_to.select {|r| r.relation_type == IssueRelation::TYPE_DUPLICATES}.collect {|r| r.issue_from}
840 844 end
841 845
842 846 # Returns the due date or the target due date if any
843 847 # Used on gantt chart
844 848 def due_before
845 849 due_date || (fixed_version ? fixed_version.effective_date : nil)
846 850 end
847 851
848 852 # Returns the time scheduled for this issue.
849 853 #
850 854 # Example:
851 855 # Start Date: 2/26/09, End Date: 3/04/09
852 856 # duration => 6
853 857 def duration
854 858 (start_date && due_date) ? due_date - start_date : 0
855 859 end
856 860
857 861 def soonest_start
858 862 @soonest_start ||= (
859 863 relations_to.collect{|relation| relation.successor_soonest_start} +
860 864 ancestors.collect(&:soonest_start)
861 865 ).compact.max
862 866 end
863 867
864 868 def reschedule_after(date)
865 869 return if date.nil?
866 870 if leaf?
867 871 if start_date.nil? || start_date < date
868 872 self.start_date, self.due_date = date, date + duration
869 873 begin
870 874 save
871 875 rescue ActiveRecord::StaleObjectError
872 876 reload
873 877 self.start_date, self.due_date = date, date + duration
874 878 save
875 879 end
876 880 end
877 881 else
878 882 leaves.each do |leaf|
879 883 leaf.reschedule_after(date)
880 884 end
881 885 end
882 886 end
883 887
884 888 def <=>(issue)
885 889 if issue.nil?
886 890 -1
887 891 elsif root_id != issue.root_id
888 892 (root_id || 0) <=> (issue.root_id || 0)
889 893 else
890 894 (lft || 0) <=> (issue.lft || 0)
891 895 end
892 896 end
893 897
894 898 def to_s
895 899 "#{tracker} ##{id}: #{subject}"
896 900 end
897 901
898 902 # Returns a string of css classes that apply to the issue
899 903 def css_classes
900 904 s = "issue status-#{status_id} priority-#{priority_id}"
901 905 s << ' closed' if closed?
902 906 s << ' overdue' if overdue?
903 907 s << ' child' if child?
904 908 s << ' parent' unless leaf?
905 909 s << ' private' if is_private?
906 910 s << ' created-by-me' if User.current.logged? && author_id == User.current.id
907 911 s << ' assigned-to-me' if User.current.logged? && assigned_to_id == User.current.id
908 912 s
909 913 end
910 914
911 915 # Saves an issue and a time_entry from the parameters
912 916 def save_issue_with_child_records(params, existing_time_entry=nil)
913 917 Issue.transaction do
914 918 if params[:time_entry] && (params[:time_entry][:hours].present? || params[:time_entry][:comments].present?) && User.current.allowed_to?(:log_time, project)
915 919 @time_entry = existing_time_entry || TimeEntry.new
916 920 @time_entry.project = project
917 921 @time_entry.issue = self
918 922 @time_entry.user = User.current
919 923 @time_entry.spent_on = User.current.today
920 924 @time_entry.attributes = params[:time_entry]
921 925 self.time_entries << @time_entry
922 926 end
923 927
924 928 # TODO: Rename hook
925 929 Redmine::Hook.call_hook(:controller_issues_edit_before_save, { :params => params, :issue => self, :time_entry => @time_entry, :journal => @current_journal})
926 930 if save
927 931 # TODO: Rename hook
928 932 Redmine::Hook.call_hook(:controller_issues_edit_after_save, { :params => params, :issue => self, :time_entry => @time_entry, :journal => @current_journal})
929 933 else
930 934 raise ActiveRecord::Rollback
931 935 end
932 936 end
933 937 end
934 938
935 939 # Unassigns issues from +version+ if it's no longer shared with issue's project
936 940 def self.update_versions_from_sharing_change(version)
937 941 # Update issues assigned to the version
938 942 update_versions(["#{Issue.table_name}.fixed_version_id = ?", version.id])
939 943 end
940 944
941 945 # Unassigns issues from versions that are no longer shared
942 946 # after +project+ was moved
943 947 def self.update_versions_from_hierarchy_change(project)
944 948 moved_project_ids = project.self_and_descendants.reload.collect(&:id)
945 949 # Update issues of the moved projects and issues assigned to a version of a moved project
946 950 Issue.update_versions(["#{Version.table_name}.project_id IN (?) OR #{Issue.table_name}.project_id IN (?)", moved_project_ids, moved_project_ids])
947 951 end
948 952
949 953 def parent_issue_id=(arg)
950 parent_issue_id = arg.blank? ? nil : arg.to_i
951 if parent_issue_id && @parent_issue = Issue.find_by_id(parent_issue_id)
954 s = arg.to_s.strip.presence
955 if s && (m = s.match(%r{\A#?(\d+)\z})) && (@parent_issue = Issue.find_by_id(m[1]))
952 956 @parent_issue.id
953 957 else
954 958 @parent_issue = nil
955 nil
959 @invalid_parent_issue_id = arg
956 960 end
957 961 end
958 962
959 963 def parent_issue_id
960 if instance_variable_defined? :@parent_issue
964 if @invalid_parent_issue_id
965 @invalid_parent_issue_id
966 elsif instance_variable_defined? :@parent_issue
961 967 @parent_issue.nil? ? nil : @parent_issue.id
962 968 else
963 969 parent_id
964 970 end
965 971 end
966 972
967 973 # Returns true if issue's project is a valid
968 974 # parent issue project
969 975 def valid_parent_project?(issue=parent)
970 976 return true if issue.nil? || issue.project_id == project_id
971 977
972 978 case Setting.cross_project_subtasks
973 979 when 'system'
974 980 true
975 981 when 'tree'
976 982 issue.project.root == project.root
977 983 when 'hierarchy'
978 984 issue.project.is_or_is_ancestor_of?(project) || issue.project.is_descendant_of?(project)
979 985 when 'descendants'
980 986 issue.project.is_or_is_ancestor_of?(project)
981 987 else
982 988 false
983 989 end
984 990 end
985 991
986 992 # Extracted from the ReportsController.
987 993 def self.by_tracker(project)
988 994 count_and_group_by(:project => project,
989 995 :field => 'tracker_id',
990 996 :joins => Tracker.table_name)
991 997 end
992 998
993 999 def self.by_version(project)
994 1000 count_and_group_by(:project => project,
995 1001 :field => 'fixed_version_id',
996 1002 :joins => Version.table_name)
997 1003 end
998 1004
999 1005 def self.by_priority(project)
1000 1006 count_and_group_by(:project => project,
1001 1007 :field => 'priority_id',
1002 1008 :joins => IssuePriority.table_name)
1003 1009 end
1004 1010
1005 1011 def self.by_category(project)
1006 1012 count_and_group_by(:project => project,
1007 1013 :field => 'category_id',
1008 1014 :joins => IssueCategory.table_name)
1009 1015 end
1010 1016
1011 1017 def self.by_assigned_to(project)
1012 1018 count_and_group_by(:project => project,
1013 1019 :field => 'assigned_to_id',
1014 1020 :joins => User.table_name)
1015 1021 end
1016 1022
1017 1023 def self.by_author(project)
1018 1024 count_and_group_by(:project => project,
1019 1025 :field => 'author_id',
1020 1026 :joins => User.table_name)
1021 1027 end
1022 1028
1023 1029 def self.by_subproject(project)
1024 1030 ActiveRecord::Base.connection.select_all("select s.id as status_id,
1025 1031 s.is_closed as closed,
1026 1032 #{Issue.table_name}.project_id as project_id,
1027 1033 count(#{Issue.table_name}.id) as total
1028 1034 from
1029 1035 #{Issue.table_name}, #{Project.table_name}, #{IssueStatus.table_name} s
1030 1036 where
1031 1037 #{Issue.table_name}.status_id=s.id
1032 1038 and #{Issue.table_name}.project_id = #{Project.table_name}.id
1033 1039 and #{visible_condition(User.current, :project => project, :with_subprojects => true)}
1034 1040 and #{Issue.table_name}.project_id <> #{project.id}
1035 1041 group by s.id, s.is_closed, #{Issue.table_name}.project_id") if project.descendants.active.any?
1036 1042 end
1037 1043 # End ReportsController extraction
1038 1044
1039 1045 # Returns an array of projects that user can assign the issue to
1040 1046 def allowed_target_projects(user=User.current)
1041 1047 if new_record?
1042 1048 Project.all(:conditions => Project.allowed_to_condition(user, :add_issues))
1043 1049 else
1044 1050 self.class.allowed_target_projects_on_move(user)
1045 1051 end
1046 1052 end
1047 1053
1048 1054 # Returns an array of projects that user can move issues to
1049 1055 def self.allowed_target_projects_on_move(user=User.current)
1050 1056 Project.all(:conditions => Project.allowed_to_condition(user, :move_issues))
1051 1057 end
1052 1058
1053 1059 private
1054 1060
1055 1061 def after_project_change
1056 1062 # Update project_id on related time entries
1057 1063 TimeEntry.update_all(["project_id = ?", project_id], {:issue_id => id})
1058 1064
1059 1065 # Delete issue relations
1060 1066 unless Setting.cross_project_issue_relations?
1061 1067 relations_from.clear
1062 1068 relations_to.clear
1063 1069 end
1064 1070
1065 1071 # Move subtasks that were in the same project
1066 1072 children.each do |child|
1067 1073 next unless child.project_id == project_id_was
1068 1074 # Change project and keep project
1069 1075 child.send :project=, project, true
1070 1076 unless child.save
1071 1077 raise ActiveRecord::Rollback
1072 1078 end
1073 1079 end
1074 1080 end
1075 1081
1076 1082 # Callback for after the creation of an issue by copy
1077 1083 # * adds a "copied to" relation with the copied issue
1078 1084 # * copies subtasks from the copied issue
1079 1085 def after_create_from_copy
1080 1086 return unless copy? && !@after_create_from_copy_handled
1081 1087
1082 1088 if (@copied_from.project_id == project_id || Setting.cross_project_issue_relations?) && @copy_options[:link] != false
1083 1089 relation = IssueRelation.new(:issue_from => @copied_from, :issue_to => self, :relation_type => IssueRelation::TYPE_COPIED_TO)
1084 1090 unless relation.save
1085 1091 logger.error "Could not create relation while copying ##{@copied_from.id} to ##{id} due to validation errors: #{relation.errors.full_messages.join(', ')}" if logger
1086 1092 end
1087 1093 end
1088 1094
1089 1095 unless @copied_from.leaf? || @copy_options[:subtasks] == false
1090 1096 @copied_from.children.each do |child|
1091 1097 unless child.visible?
1092 1098 # Do not copy subtasks that are not visible to avoid potential disclosure of private data
1093 1099 logger.error "Subtask ##{child.id} was not copied during ##{@copied_from.id} copy because it is not visible to the current user" if logger
1094 1100 next
1095 1101 end
1096 1102 copy = Issue.new.copy_from(child, @copy_options)
1097 1103 copy.author = author
1098 1104 copy.project = project
1099 1105 copy.parent_issue_id = id
1100 1106 # Children subtasks are copied recursively
1101 1107 unless copy.save
1102 1108 logger.error "Could not copy subtask ##{child.id} while copying ##{@copied_from.id} to ##{id} due to validation errors: #{copy.errors.full_messages.join(', ')}" if logger
1103 1109 end
1104 1110 end
1105 1111 end
1106 1112 @after_create_from_copy_handled = true
1107 1113 end
1108 1114
1109 1115 def update_nested_set_attributes
1110 1116 if root_id.nil?
1111 1117 # issue was just created
1112 1118 self.root_id = (@parent_issue.nil? ? id : @parent_issue.root_id)
1113 1119 set_default_left_and_right
1114 1120 Issue.update_all("root_id = #{root_id}, lft = #{lft}, rgt = #{rgt}", ["id = ?", id])
1115 1121 if @parent_issue
1116 1122 move_to_child_of(@parent_issue)
1117 1123 end
1118 1124 reload
1119 1125 elsif parent_issue_id != parent_id
1120 1126 former_parent_id = parent_id
1121 1127 # moving an existing issue
1122 1128 if @parent_issue && @parent_issue.root_id == root_id
1123 1129 # inside the same tree
1124 1130 move_to_child_of(@parent_issue)
1125 1131 else
1126 1132 # to another tree
1127 1133 unless root?
1128 1134 move_to_right_of(root)
1129 1135 reload
1130 1136 end
1131 1137 old_root_id = root_id
1132 1138 self.root_id = (@parent_issue.nil? ? id : @parent_issue.root_id )
1133 1139 target_maxright = nested_set_scope.maximum(right_column_name) || 0
1134 1140 offset = target_maxright + 1 - lft
1135 1141 Issue.update_all("root_id = #{root_id}, lft = lft + #{offset}, rgt = rgt + #{offset}",
1136 1142 ["root_id = ? AND lft >= ? AND rgt <= ? ", old_root_id, lft, rgt])
1137 1143 self[left_column_name] = lft + offset
1138 1144 self[right_column_name] = rgt + offset
1139 1145 if @parent_issue
1140 1146 move_to_child_of(@parent_issue)
1141 1147 end
1142 1148 end
1143 1149 reload
1144 1150 # delete invalid relations of all descendants
1145 1151 self_and_descendants.each do |issue|
1146 1152 issue.relations.each do |relation|
1147 1153 relation.destroy unless relation.valid?
1148 1154 end
1149 1155 end
1150 1156 # update former parent
1151 1157 recalculate_attributes_for(former_parent_id) if former_parent_id
1152 1158 end
1153 1159 remove_instance_variable(:@parent_issue) if instance_variable_defined?(:@parent_issue)
1154 1160 end
1155 1161
1156 1162 def update_parent_attributes
1157 1163 recalculate_attributes_for(parent_id) if parent_id
1158 1164 end
1159 1165
1160 1166 def recalculate_attributes_for(issue_id)
1161 1167 if issue_id && p = Issue.find_by_id(issue_id)
1162 1168 # priority = highest priority of children
1163 1169 if priority_position = p.children.maximum("#{IssuePriority.table_name}.position", :joins => :priority)
1164 1170 p.priority = IssuePriority.find_by_position(priority_position)
1165 1171 end
1166 1172
1167 1173 # start/due dates = lowest/highest dates of children
1168 1174 p.start_date = p.children.minimum(:start_date)
1169 1175 p.due_date = p.children.maximum(:due_date)
1170 1176 if p.start_date && p.due_date && p.due_date < p.start_date
1171 1177 p.start_date, p.due_date = p.due_date, p.start_date
1172 1178 end
1173 1179
1174 1180 # done ratio = weighted average ratio of leaves
1175 1181 unless Issue.use_status_for_done_ratio? && p.status && p.status.default_done_ratio
1176 1182 leaves_count = p.leaves.count
1177 1183 if leaves_count > 0
1178 1184 average = p.leaves.average(:estimated_hours).to_f
1179 1185 if average == 0
1180 1186 average = 1
1181 1187 end
1182 1188 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
1183 1189 progress = done / (average * leaves_count)
1184 1190 p.done_ratio = progress.round
1185 1191 end
1186 1192 end
1187 1193
1188 1194 # estimate = sum of leaves estimates
1189 1195 p.estimated_hours = p.leaves.sum(:estimated_hours).to_f
1190 1196 p.estimated_hours = nil if p.estimated_hours == 0.0
1191 1197
1192 1198 # ancestors will be recursively updated
1193 1199 p.save(:validate => false)
1194 1200 end
1195 1201 end
1196 1202
1197 1203 # Update issues so their versions are not pointing to a
1198 1204 # fixed_version that is not shared with the issue's project
1199 1205 def self.update_versions(conditions=nil)
1200 1206 # Only need to update issues with a fixed_version from
1201 1207 # a different project and that is not systemwide shared
1202 1208 Issue.scoped(:conditions => conditions).all(
1203 1209 :conditions => "#{Issue.table_name}.fixed_version_id IS NOT NULL" +
1204 1210 " AND #{Issue.table_name}.project_id <> #{Version.table_name}.project_id" +
1205 1211 " AND #{Version.table_name}.sharing <> 'system'",
1206 1212 :include => [:project, :fixed_version]
1207 1213 ).each do |issue|
1208 1214 next if issue.project.nil? || issue.fixed_version.nil?
1209 1215 unless issue.project.shared_versions.include?(issue.fixed_version)
1210 1216 issue.init_journal(User.current)
1211 1217 issue.fixed_version = nil
1212 1218 issue.save
1213 1219 end
1214 1220 end
1215 1221 end
1216 1222
1217 1223 # Callback on file attachment
1218 1224 def attachment_added(obj)
1219 1225 if @current_journal && !obj.new_record?
1220 1226 @current_journal.details << JournalDetail.new(:property => 'attachment', :prop_key => obj.id, :value => obj.filename)
1221 1227 end
1222 1228 end
1223 1229
1224 1230 # Callback on attachment deletion
1225 1231 def attachment_removed(obj)
1226 1232 if @current_journal && !obj.new_record?
1227 1233 @current_journal.details << JournalDetail.new(:property => 'attachment', :prop_key => obj.id, :old_value => obj.filename)
1228 1234 @current_journal.save
1229 1235 end
1230 1236 end
1231 1237
1232 1238 # Default assignment based on category
1233 1239 def default_assign
1234 1240 if assigned_to.nil? && category && category.assigned_to
1235 1241 self.assigned_to = category.assigned_to
1236 1242 end
1237 1243 end
1238 1244
1239 1245 # Updates start/due dates of following issues
1240 1246 def reschedule_following_issues
1241 1247 if start_date_changed? || due_date_changed?
1242 1248 relations_from.each do |relation|
1243 1249 relation.set_issue_to_dates
1244 1250 end
1245 1251 end
1246 1252 end
1247 1253
1248 1254 # Closes duplicates if the issue is being closed
1249 1255 def close_duplicates
1250 1256 if closing?
1251 1257 duplicates.each do |duplicate|
1252 1258 # Reload is need in case the duplicate was updated by a previous duplicate
1253 1259 duplicate.reload
1254 1260 # Don't re-close it if it's already closed
1255 1261 next if duplicate.closed?
1256 1262 # Same user and notes
1257 1263 if @current_journal
1258 1264 duplicate.init_journal(@current_journal.user, @current_journal.notes)
1259 1265 end
1260 1266 duplicate.update_attribute :status, self.status
1261 1267 end
1262 1268 end
1263 1269 end
1264 1270
1265 1271 # Make sure updated_on is updated when adding a note
1266 1272 def force_updated_on_change
1267 1273 if @current_journal
1268 1274 self.updated_on = current_time_from_proper_timezone
1269 1275 end
1270 1276 end
1271 1277
1272 1278 # Saves the changes in a Journal
1273 1279 # Called after_save
1274 1280 def create_journal
1275 1281 if @current_journal
1276 1282 # attributes changes
1277 1283 if @attributes_before_change
1278 1284 (Issue.column_names - %w(id root_id lft rgt lock_version created_on updated_on)).each {|c|
1279 1285 before = @attributes_before_change[c]
1280 1286 after = send(c)
1281 1287 next if before == after || (before.blank? && after.blank?)
1282 1288 @current_journal.details << JournalDetail.new(:property => 'attr',
1283 1289 :prop_key => c,
1284 1290 :old_value => before,
1285 1291 :value => after)
1286 1292 }
1287 1293 end
1288 1294 if @custom_values_before_change
1289 1295 # custom fields changes
1290 1296 custom_field_values.each {|c|
1291 1297 before = @custom_values_before_change[c.custom_field_id]
1292 1298 after = c.value
1293 1299 next if before == after || (before.blank? && after.blank?)
1294 1300
1295 1301 if before.is_a?(Array) || after.is_a?(Array)
1296 1302 before = [before] unless before.is_a?(Array)
1297 1303 after = [after] unless after.is_a?(Array)
1298 1304
1299 1305 # values removed
1300 1306 (before - after).reject(&:blank?).each do |value|
1301 1307 @current_journal.details << JournalDetail.new(:property => 'cf',
1302 1308 :prop_key => c.custom_field_id,
1303 1309 :old_value => value,
1304 1310 :value => nil)
1305 1311 end
1306 1312 # values added
1307 1313 (after - before).reject(&:blank?).each do |value|
1308 1314 @current_journal.details << JournalDetail.new(:property => 'cf',
1309 1315 :prop_key => c.custom_field_id,
1310 1316 :old_value => nil,
1311 1317 :value => value)
1312 1318 end
1313 1319 else
1314 1320 @current_journal.details << JournalDetail.new(:property => 'cf',
1315 1321 :prop_key => c.custom_field_id,
1316 1322 :old_value => before,
1317 1323 :value => after)
1318 1324 end
1319 1325 }
1320 1326 end
1321 1327 @current_journal.save
1322 1328 # reset current journal
1323 1329 init_journal @current_journal.user, @current_journal.notes
1324 1330 end
1325 1331 end
1326 1332
1327 1333 # Query generator for selecting groups of issue counts for a project
1328 1334 # based on specific criteria
1329 1335 #
1330 1336 # Options
1331 1337 # * project - Project to search in.
1332 1338 # * field - String. Issue field to key off of in the grouping.
1333 1339 # * joins - String. The table name to join against.
1334 1340 def self.count_and_group_by(options)
1335 1341 project = options.delete(:project)
1336 1342 select_field = options.delete(:field)
1337 1343 joins = options.delete(:joins)
1338 1344
1339 1345 where = "#{Issue.table_name}.#{select_field}=j.id"
1340 1346
1341 1347 ActiveRecord::Base.connection.select_all("select s.id as status_id,
1342 1348 s.is_closed as closed,
1343 1349 j.id as #{select_field},
1344 1350 count(#{Issue.table_name}.id) as total
1345 1351 from
1346 1352 #{Issue.table_name}, #{Project.table_name}, #{IssueStatus.table_name} s, #{joins} j
1347 1353 where
1348 1354 #{Issue.table_name}.status_id=s.id
1349 1355 and #{where}
1350 1356 and #{Issue.table_name}.project_id=#{Project.table_name}.id
1351 1357 and #{visible_condition(User.current, :project => project)}
1352 1358 group by s.id, s.is_closed, j.id")
1353 1359 end
1354 1360 end
@@ -1,3779 +1,3797
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 require 'issues_controller'
20 20
21 21 class IssuesControllerTest < ActionController::TestCase
22 22 fixtures :projects,
23 23 :users,
24 24 :roles,
25 25 :members,
26 26 :member_roles,
27 27 :issues,
28 28 :issue_statuses,
29 29 :versions,
30 30 :trackers,
31 31 :projects_trackers,
32 32 :issue_categories,
33 33 :enabled_modules,
34 34 :enumerations,
35 35 :attachments,
36 36 :workflows,
37 37 :custom_fields,
38 38 :custom_values,
39 39 :custom_fields_projects,
40 40 :custom_fields_trackers,
41 41 :time_entries,
42 42 :journals,
43 43 :journal_details,
44 44 :queries,
45 45 :repositories,
46 46 :changesets
47 47
48 48 include Redmine::I18n
49 49
50 50 def setup
51 51 @controller = IssuesController.new
52 52 @request = ActionController::TestRequest.new
53 53 @response = ActionController::TestResponse.new
54 54 User.current = nil
55 55 end
56 56
57 57 def test_index
58 58 with_settings :default_language => "en" do
59 59 get :index
60 60 assert_response :success
61 61 assert_template 'index'
62 62 assert_not_nil assigns(:issues)
63 63 assert_nil assigns(:project)
64 64 assert_tag :tag => 'a', :content => /Can&#x27;t print recipes/
65 65 assert_tag :tag => 'a', :content => /Subproject issue/
66 66 # private projects hidden
67 67 assert_no_tag :tag => 'a', :content => /Issue of a private subproject/
68 68 assert_no_tag :tag => 'a', :content => /Issue on project 2/
69 69 # project column
70 70 assert_tag :tag => 'th', :content => /Project/
71 71 end
72 72 end
73 73
74 74 def test_index_should_not_list_issues_when_module_disabled
75 75 EnabledModule.delete_all("name = 'issue_tracking' AND project_id = 1")
76 76 get :index
77 77 assert_response :success
78 78 assert_template 'index'
79 79 assert_not_nil assigns(:issues)
80 80 assert_nil assigns(:project)
81 81 assert_no_tag :tag => 'a', :content => /Can&#x27;t print recipes/
82 82 assert_tag :tag => 'a', :content => /Subproject issue/
83 83 end
84 84
85 85 def test_index_should_list_visible_issues_only
86 86 get :index, :per_page => 100
87 87 assert_response :success
88 88 assert_not_nil assigns(:issues)
89 89 assert_nil assigns(:issues).detect {|issue| !issue.visible?}
90 90 end
91 91
92 92 def test_index_with_project
93 93 Setting.display_subprojects_issues = 0
94 94 get :index, :project_id => 1
95 95 assert_response :success
96 96 assert_template 'index'
97 97 assert_not_nil assigns(:issues)
98 98 assert_tag :tag => 'a', :content => /Can&#x27;t print recipes/
99 99 assert_no_tag :tag => 'a', :content => /Subproject issue/
100 100 end
101 101
102 102 def test_index_with_project_and_subprojects
103 103 Setting.display_subprojects_issues = 1
104 104 get :index, :project_id => 1
105 105 assert_response :success
106 106 assert_template 'index'
107 107 assert_not_nil assigns(:issues)
108 108 assert_tag :tag => 'a', :content => /Can&#x27;t print recipes/
109 109 assert_tag :tag => 'a', :content => /Subproject issue/
110 110 assert_no_tag :tag => 'a', :content => /Issue of a private subproject/
111 111 end
112 112
113 113 def test_index_with_project_and_subprojects_should_show_private_subprojects
114 114 @request.session[:user_id] = 2
115 115 Setting.display_subprojects_issues = 1
116 116 get :index, :project_id => 1
117 117 assert_response :success
118 118 assert_template 'index'
119 119 assert_not_nil assigns(:issues)
120 120 assert_tag :tag => 'a', :content => /Can&#x27;t print recipes/
121 121 assert_tag :tag => 'a', :content => /Subproject issue/
122 122 assert_tag :tag => 'a', :content => /Issue of a private subproject/
123 123 end
124 124
125 125 def test_index_with_project_and_default_filter
126 126 get :index, :project_id => 1, :set_filter => 1
127 127 assert_response :success
128 128 assert_template 'index'
129 129 assert_not_nil assigns(:issues)
130 130
131 131 query = assigns(:query)
132 132 assert_not_nil query
133 133 # default filter
134 134 assert_equal({'status_id' => {:operator => 'o', :values => ['']}}, query.filters)
135 135 end
136 136
137 137 def test_index_with_project_and_filter
138 138 get :index, :project_id => 1, :set_filter => 1,
139 139 :f => ['tracker_id'],
140 140 :op => {'tracker_id' => '='},
141 141 :v => {'tracker_id' => ['1']}
142 142 assert_response :success
143 143 assert_template 'index'
144 144 assert_not_nil assigns(:issues)
145 145
146 146 query = assigns(:query)
147 147 assert_not_nil query
148 148 assert_equal({'tracker_id' => {:operator => '=', :values => ['1']}}, query.filters)
149 149 end
150 150
151 151 def test_index_with_short_filters
152 152 to_test = {
153 153 'status_id' => {
154 154 'o' => { :op => 'o', :values => [''] },
155 155 'c' => { :op => 'c', :values => [''] },
156 156 '7' => { :op => '=', :values => ['7'] },
157 157 '7|3|4' => { :op => '=', :values => ['7', '3', '4'] },
158 158 '=7' => { :op => '=', :values => ['7'] },
159 159 '!3' => { :op => '!', :values => ['3'] },
160 160 '!7|3|4' => { :op => '!', :values => ['7', '3', '4'] }},
161 161 'subject' => {
162 162 'This is a subject' => { :op => '=', :values => ['This is a subject'] },
163 163 'o' => { :op => '=', :values => ['o'] },
164 164 '~This is part of a subject' => { :op => '~', :values => ['This is part of a subject'] },
165 165 '!~This is part of a subject' => { :op => '!~', :values => ['This is part of a subject'] }},
166 166 'tracker_id' => {
167 167 '3' => { :op => '=', :values => ['3'] },
168 168 '=3' => { :op => '=', :values => ['3'] }},
169 169 'start_date' => {
170 170 '2011-10-12' => { :op => '=', :values => ['2011-10-12'] },
171 171 '=2011-10-12' => { :op => '=', :values => ['2011-10-12'] },
172 172 '>=2011-10-12' => { :op => '>=', :values => ['2011-10-12'] },
173 173 '<=2011-10-12' => { :op => '<=', :values => ['2011-10-12'] },
174 174 '><2011-10-01|2011-10-30' => { :op => '><', :values => ['2011-10-01', '2011-10-30'] },
175 175 '<t+2' => { :op => '<t+', :values => ['2'] },
176 176 '>t+2' => { :op => '>t+', :values => ['2'] },
177 177 't+2' => { :op => 't+', :values => ['2'] },
178 178 't' => { :op => 't', :values => [''] },
179 179 'w' => { :op => 'w', :values => [''] },
180 180 '>t-2' => { :op => '>t-', :values => ['2'] },
181 181 '<t-2' => { :op => '<t-', :values => ['2'] },
182 182 't-2' => { :op => 't-', :values => ['2'] }},
183 183 'created_on' => {
184 184 '>=2011-10-12' => { :op => '>=', :values => ['2011-10-12'] },
185 185 '<t-2' => { :op => '<t-', :values => ['2'] },
186 186 '>t-2' => { :op => '>t-', :values => ['2'] },
187 187 't-2' => { :op => 't-', :values => ['2'] }},
188 188 'cf_1' => {
189 189 'c' => { :op => '=', :values => ['c'] },
190 190 '!c' => { :op => '!', :values => ['c'] },
191 191 '!*' => { :op => '!*', :values => [''] },
192 192 '*' => { :op => '*', :values => [''] }},
193 193 'estimated_hours' => {
194 194 '=13.4' => { :op => '=', :values => ['13.4'] },
195 195 '>=45' => { :op => '>=', :values => ['45'] },
196 196 '<=125' => { :op => '<=', :values => ['125'] },
197 197 '><10.5|20.5' => { :op => '><', :values => ['10.5', '20.5'] },
198 198 '!*' => { :op => '!*', :values => [''] },
199 199 '*' => { :op => '*', :values => [''] }}
200 200 }
201 201
202 202 default_filter = { 'status_id' => {:operator => 'o', :values => [''] }}
203 203
204 204 to_test.each do |field, expression_and_expected|
205 205 expression_and_expected.each do |filter_expression, expected|
206 206
207 207 get :index, :set_filter => 1, field => filter_expression
208 208
209 209 assert_response :success
210 210 assert_template 'index'
211 211 assert_not_nil assigns(:issues)
212 212
213 213 query = assigns(:query)
214 214 assert_not_nil query
215 215 assert query.has_filter?(field)
216 216 assert_equal(default_filter.merge({field => {:operator => expected[:op], :values => expected[:values]}}), query.filters)
217 217 end
218 218 end
219 219 end
220 220
221 221 def test_index_with_project_and_empty_filters
222 222 get :index, :project_id => 1, :set_filter => 1, :fields => ['']
223 223 assert_response :success
224 224 assert_template 'index'
225 225 assert_not_nil assigns(:issues)
226 226
227 227 query = assigns(:query)
228 228 assert_not_nil query
229 229 # no filter
230 230 assert_equal({}, query.filters)
231 231 end
232 232
233 233 def test_index_with_project_custom_field_filter
234 234 field = ProjectCustomField.create!(:name => 'Client', :is_filter => true, :field_format => 'string')
235 235 CustomValue.create!(:custom_field => field, :customized => Project.find(3), :value => 'Foo')
236 236 CustomValue.create!(:custom_field => field, :customized => Project.find(5), :value => 'Foo')
237 237 filter_name = "project.cf_#{field.id}"
238 238 @request.session[:user_id] = 1
239 239
240 240 get :index, :set_filter => 1,
241 241 :f => [filter_name],
242 242 :op => {filter_name => '='},
243 243 :v => {filter_name => ['Foo']}
244 244 assert_response :success
245 245 assert_template 'index'
246 246 assert_equal [3, 5], assigns(:issues).map(&:project_id).uniq.sort
247 247 end
248 248
249 249 def test_index_with_query
250 250 get :index, :project_id => 1, :query_id => 5
251 251 assert_response :success
252 252 assert_template 'index'
253 253 assert_not_nil assigns(:issues)
254 254 assert_nil assigns(:issue_count_by_group)
255 255 end
256 256
257 257 def test_index_with_query_grouped_by_tracker
258 258 get :index, :project_id => 1, :query_id => 6
259 259 assert_response :success
260 260 assert_template 'index'
261 261 assert_not_nil assigns(:issues)
262 262 assert_not_nil assigns(:issue_count_by_group)
263 263 end
264 264
265 265 def test_index_with_query_grouped_by_list_custom_field
266 266 get :index, :project_id => 1, :query_id => 9
267 267 assert_response :success
268 268 assert_template 'index'
269 269 assert_not_nil assigns(:issues)
270 270 assert_not_nil assigns(:issue_count_by_group)
271 271 end
272 272
273 273 def test_index_with_query_grouped_by_user_custom_field
274 274 cf = IssueCustomField.create!(:name => 'User', :is_for_all => true, :tracker_ids => [1,2,3], :field_format => 'user')
275 275 CustomValue.create!(:custom_field => cf, :customized => Issue.find(1), :value => '2')
276 276 CustomValue.create!(:custom_field => cf, :customized => Issue.find(2), :value => '3')
277 277 CustomValue.create!(:custom_field => cf, :customized => Issue.find(3), :value => '3')
278 278 CustomValue.create!(:custom_field => cf, :customized => Issue.find(5), :value => '')
279 279
280 280 get :index, :project_id => 1, :set_filter => 1, :group_by => "cf_#{cf.id}"
281 281 assert_response :success
282 282
283 283 assert_select 'tr.group', 3
284 284 assert_select 'tr.group' do
285 285 assert_select 'a', :text => 'John Smith'
286 286 assert_select 'span.count', :text => '1'
287 287 end
288 288 assert_select 'tr.group' do
289 289 assert_select 'a', :text => 'Dave Lopper'
290 290 assert_select 'span.count', :text => '2'
291 291 end
292 292 end
293 293
294 294 def test_index_with_query_id_and_project_id_should_set_session_query
295 295 get :index, :project_id => 1, :query_id => 4
296 296 assert_response :success
297 297 assert_kind_of Hash, session[:query]
298 298 assert_equal 4, session[:query][:id]
299 299 assert_equal 1, session[:query][:project_id]
300 300 end
301 301
302 302 def test_index_with_invalid_query_id_should_respond_404
303 303 get :index, :project_id => 1, :query_id => 999
304 304 assert_response 404
305 305 end
306 306
307 307 def test_index_with_cross_project_query_in_session_should_show_project_issues
308 308 q = Query.create!(:name => "test", :user_id => 2, :is_public => false, :project => nil)
309 309 @request.session[:query] = {:id => q.id, :project_id => 1}
310 310
311 311 with_settings :display_subprojects_issues => '0' do
312 312 get :index, :project_id => 1
313 313 end
314 314 assert_response :success
315 315 assert_not_nil assigns(:query)
316 316 assert_equal q.id, assigns(:query).id
317 317 assert_equal 1, assigns(:query).project_id
318 318 assert_equal [1], assigns(:issues).map(&:project_id).uniq
319 319 end
320 320
321 321 def test_private_query_should_not_be_available_to_other_users
322 322 q = Query.create!(:name => "private", :user => User.find(2), :is_public => false, :project => nil)
323 323 @request.session[:user_id] = 3
324 324
325 325 get :index, :query_id => q.id
326 326 assert_response 403
327 327 end
328 328
329 329 def test_private_query_should_be_available_to_its_user
330 330 q = Query.create!(:name => "private", :user => User.find(2), :is_public => false, :project => nil)
331 331 @request.session[:user_id] = 2
332 332
333 333 get :index, :query_id => q.id
334 334 assert_response :success
335 335 end
336 336
337 337 def test_public_query_should_be_available_to_other_users
338 338 q = Query.create!(:name => "private", :user => User.find(2), :is_public => true, :project => nil)
339 339 @request.session[:user_id] = 3
340 340
341 341 get :index, :query_id => q.id
342 342 assert_response :success
343 343 end
344 344
345 345 def test_index_should_omit_page_param_in_export_links
346 346 get :index, :page => 2
347 347 assert_response :success
348 348 assert_select 'a.atom[href=/issues.atom]'
349 349 assert_select 'a.csv[href=/issues.csv]'
350 350 assert_select 'a.pdf[href=/issues.pdf]'
351 351 assert_select 'form#csv-export-form[action=/issues.csv]'
352 352 end
353 353
354 354 def test_index_csv
355 355 get :index, :format => 'csv'
356 356 assert_response :success
357 357 assert_not_nil assigns(:issues)
358 358 assert_equal 'text/csv; header=present', @response.content_type
359 359 assert @response.body.starts_with?("#,")
360 360 lines = @response.body.chomp.split("\n")
361 361 assert_equal assigns(:query).columns.size + 1, lines[0].split(',').size
362 362 end
363 363
364 364 def test_index_csv_with_project
365 365 get :index, :project_id => 1, :format => 'csv'
366 366 assert_response :success
367 367 assert_not_nil assigns(:issues)
368 368 assert_equal 'text/csv; header=present', @response.content_type
369 369 end
370 370
371 371 def test_index_csv_with_description
372 372 get :index, :format => 'csv', :description => '1'
373 373 assert_response :success
374 374 assert_not_nil assigns(:issues)
375 375 assert_equal 'text/csv; header=present', @response.content_type
376 376 assert @response.body.starts_with?("#,")
377 377 lines = @response.body.chomp.split("\n")
378 378 assert_equal assigns(:query).columns.size + 2, lines[0].split(',').size
379 379 end
380 380
381 381 def test_index_csv_with_spent_time_column
382 382 issue = Issue.create!(:project_id => 1, :tracker_id => 1, :subject => 'test_index_csv_with_spent_time_column', :author_id => 2)
383 383 TimeEntry.create!(:project => issue.project, :issue => issue, :hours => 7.33, :user => User.find(2), :spent_on => Date.today)
384 384
385 385 get :index, :format => 'csv', :set_filter => '1', :c => %w(subject spent_hours)
386 386 assert_response :success
387 387 assert_equal 'text/csv; header=present', @response.content_type
388 388 lines = @response.body.chomp.split("\n")
389 389 assert_include "#{issue.id},#{issue.subject},7.33", lines
390 390 end
391 391
392 392 def test_index_csv_with_all_columns
393 393 get :index, :format => 'csv', :columns => 'all'
394 394 assert_response :success
395 395 assert_not_nil assigns(:issues)
396 396 assert_equal 'text/csv; header=present', @response.content_type
397 397 assert @response.body.starts_with?("#,")
398 398 lines = @response.body.chomp.split("\n")
399 399 assert_equal assigns(:query).available_columns.size + 1, lines[0].split(',').size
400 400 end
401 401
402 402 def test_index_csv_with_multi_column_field
403 403 CustomField.find(1).update_attribute :multiple, true
404 404 issue = Issue.find(1)
405 405 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
406 406 issue.save!
407 407
408 408 get :index, :format => 'csv', :columns => 'all'
409 409 assert_response :success
410 410 lines = @response.body.chomp.split("\n")
411 411 assert lines.detect {|line| line.include?('"MySQL, Oracle"')}
412 412 end
413 413
414 414 def test_index_csv_big_5
415 415 with_settings :default_language => "zh-TW" do
416 416 str_utf8 = "\xe4\xb8\x80\xe6\x9c\x88"
417 417 str_big5 = "\xa4@\xa4\xeb"
418 418 if str_utf8.respond_to?(:force_encoding)
419 419 str_utf8.force_encoding('UTF-8')
420 420 str_big5.force_encoding('Big5')
421 421 end
422 422 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3,
423 423 :status_id => 1, :priority => IssuePriority.all.first,
424 424 :subject => str_utf8)
425 425 assert issue.save
426 426
427 427 get :index, :project_id => 1,
428 428 :f => ['subject'],
429 429 :op => '=', :values => [str_utf8],
430 430 :format => 'csv'
431 431 assert_equal 'text/csv; header=present', @response.content_type
432 432 lines = @response.body.chomp.split("\n")
433 433 s1 = "\xaa\xac\xbaA"
434 434 if str_utf8.respond_to?(:force_encoding)
435 435 s1.force_encoding('Big5')
436 436 end
437 437 assert lines[0].include?(s1)
438 438 assert lines[1].include?(str_big5)
439 439 end
440 440 end
441 441
442 442 def test_index_csv_cannot_convert_should_be_replaced_big_5
443 443 with_settings :default_language => "zh-TW" do
444 444 str_utf8 = "\xe4\xbb\xa5\xe5\x86\x85"
445 445 if str_utf8.respond_to?(:force_encoding)
446 446 str_utf8.force_encoding('UTF-8')
447 447 end
448 448 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3,
449 449 :status_id => 1, :priority => IssuePriority.all.first,
450 450 :subject => str_utf8)
451 451 assert issue.save
452 452
453 453 get :index, :project_id => 1,
454 454 :f => ['subject'],
455 455 :op => '=', :values => [str_utf8],
456 456 :c => ['status', 'subject'],
457 457 :format => 'csv',
458 458 :set_filter => 1
459 459 assert_equal 'text/csv; header=present', @response.content_type
460 460 lines = @response.body.chomp.split("\n")
461 461 s1 = "\xaa\xac\xbaA" # status
462 462 if str_utf8.respond_to?(:force_encoding)
463 463 s1.force_encoding('Big5')
464 464 end
465 465 assert lines[0].include?(s1)
466 466 s2 = lines[1].split(",")[2]
467 467 if s1.respond_to?(:force_encoding)
468 468 s3 = "\xa5H?" # subject
469 469 s3.force_encoding('Big5')
470 470 assert_equal s3, s2
471 471 elsif RUBY_PLATFORM == 'java'
472 472 assert_equal "??", s2
473 473 else
474 474 assert_equal "\xa5H???", s2
475 475 end
476 476 end
477 477 end
478 478
479 479 def test_index_csv_tw
480 480 with_settings :default_language => "zh-TW" do
481 481 str1 = "test_index_csv_tw"
482 482 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3,
483 483 :status_id => 1, :priority => IssuePriority.all.first,
484 484 :subject => str1, :estimated_hours => '1234.5')
485 485 assert issue.save
486 486 assert_equal 1234.5, issue.estimated_hours
487 487
488 488 get :index, :project_id => 1,
489 489 :f => ['subject'],
490 490 :op => '=', :values => [str1],
491 491 :c => ['estimated_hours', 'subject'],
492 492 :format => 'csv',
493 493 :set_filter => 1
494 494 assert_equal 'text/csv; header=present', @response.content_type
495 495 lines = @response.body.chomp.split("\n")
496 496 assert_equal "#{issue.id},1234.50,#{str1}", lines[1]
497 497
498 498 str_tw = "Traditional Chinese (\xe7\xb9\x81\xe9\xab\x94\xe4\xb8\xad\xe6\x96\x87)"
499 499 if str_tw.respond_to?(:force_encoding)
500 500 str_tw.force_encoding('UTF-8')
501 501 end
502 502 assert_equal str_tw, l(:general_lang_name)
503 503 assert_equal ',', l(:general_csv_separator)
504 504 assert_equal '.', l(:general_csv_decimal_separator)
505 505 end
506 506 end
507 507
508 508 def test_index_csv_fr
509 509 with_settings :default_language => "fr" do
510 510 str1 = "test_index_csv_fr"
511 511 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3,
512 512 :status_id => 1, :priority => IssuePriority.all.first,
513 513 :subject => str1, :estimated_hours => '1234.5')
514 514 assert issue.save
515 515 assert_equal 1234.5, issue.estimated_hours
516 516
517 517 get :index, :project_id => 1,
518 518 :f => ['subject'],
519 519 :op => '=', :values => [str1],
520 520 :c => ['estimated_hours', 'subject'],
521 521 :format => 'csv',
522 522 :set_filter => 1
523 523 assert_equal 'text/csv; header=present', @response.content_type
524 524 lines = @response.body.chomp.split("\n")
525 525 assert_equal "#{issue.id};1234,50;#{str1}", lines[1]
526 526
527 527 str_fr = "Fran\xc3\xa7ais"
528 528 if str_fr.respond_to?(:force_encoding)
529 529 str_fr.force_encoding('UTF-8')
530 530 end
531 531 assert_equal str_fr, l(:general_lang_name)
532 532 assert_equal ';', l(:general_csv_separator)
533 533 assert_equal ',', l(:general_csv_decimal_separator)
534 534 end
535 535 end
536 536
537 537 def test_index_pdf
538 538 ["en", "zh", "zh-TW", "ja", "ko"].each do |lang|
539 539 with_settings :default_language => lang do
540 540
541 541 get :index
542 542 assert_response :success
543 543 assert_template 'index'
544 544
545 545 if lang == "ja"
546 546 if RUBY_PLATFORM != 'java'
547 547 assert_equal "CP932", l(:general_pdf_encoding)
548 548 end
549 549 if RUBY_PLATFORM == 'java' && l(:general_pdf_encoding) == "CP932"
550 550 next
551 551 end
552 552 end
553 553
554 554 get :index, :format => 'pdf'
555 555 assert_response :success
556 556 assert_not_nil assigns(:issues)
557 557 assert_equal 'application/pdf', @response.content_type
558 558
559 559 get :index, :project_id => 1, :format => 'pdf'
560 560 assert_response :success
561 561 assert_not_nil assigns(:issues)
562 562 assert_equal 'application/pdf', @response.content_type
563 563
564 564 get :index, :project_id => 1, :query_id => 6, :format => 'pdf'
565 565 assert_response :success
566 566 assert_not_nil assigns(:issues)
567 567 assert_equal 'application/pdf', @response.content_type
568 568 end
569 569 end
570 570 end
571 571
572 572 def test_index_pdf_with_query_grouped_by_list_custom_field
573 573 get :index, :project_id => 1, :query_id => 9, :format => 'pdf'
574 574 assert_response :success
575 575 assert_not_nil assigns(:issues)
576 576 assert_not_nil assigns(:issue_count_by_group)
577 577 assert_equal 'application/pdf', @response.content_type
578 578 end
579 579
580 580 def test_index_atom
581 581 get :index, :project_id => 'ecookbook', :format => 'atom'
582 582 assert_response :success
583 583 assert_template 'common/feed'
584 584
585 585 assert_tag :tag => 'link', :parent => {:tag => 'feed', :parent => nil },
586 586 :attributes => {:rel => 'self', :href => 'http://test.host/projects/ecookbook/issues.atom'}
587 587 assert_tag :tag => 'link', :parent => {:tag => 'feed', :parent => nil },
588 588 :attributes => {:rel => 'alternate', :href => 'http://test.host/projects/ecookbook/issues'}
589 589
590 590 assert_tag :tag => 'entry', :child => {
591 591 :tag => 'link',
592 592 :attributes => {:href => 'http://test.host/issues/1'}}
593 593 end
594 594
595 595 def test_index_sort
596 596 get :index, :sort => 'tracker,id:desc'
597 597 assert_response :success
598 598
599 599 sort_params = @request.session['issues_index_sort']
600 600 assert sort_params.is_a?(String)
601 601 assert_equal 'tracker,id:desc', sort_params
602 602
603 603 issues = assigns(:issues)
604 604 assert_not_nil issues
605 605 assert !issues.empty?
606 606 assert_equal issues.sort {|a,b| a.tracker == b.tracker ? b.id <=> a.id : a.tracker <=> b.tracker }.collect(&:id), issues.collect(&:id)
607 607 end
608 608
609 609 def test_index_sort_by_field_not_included_in_columns
610 610 Setting.issue_list_default_columns = %w(subject author)
611 611 get :index, :sort => 'tracker'
612 612 end
613 613
614 614 def test_index_sort_by_assigned_to
615 615 get :index, :sort => 'assigned_to'
616 616 assert_response :success
617 617 assignees = assigns(:issues).collect(&:assigned_to).compact
618 618 assert_equal assignees.sort, assignees
619 619 end
620 620
621 621 def test_index_sort_by_assigned_to_desc
622 622 get :index, :sort => 'assigned_to:desc'
623 623 assert_response :success
624 624 assignees = assigns(:issues).collect(&:assigned_to).compact
625 625 assert_equal assignees.sort.reverse, assignees
626 626 end
627 627
628 628 def test_index_group_by_assigned_to
629 629 get :index, :group_by => 'assigned_to', :sort => 'priority'
630 630 assert_response :success
631 631 end
632 632
633 633 def test_index_sort_by_author
634 634 get :index, :sort => 'author'
635 635 assert_response :success
636 636 authors = assigns(:issues).collect(&:author)
637 637 assert_equal authors.sort, authors
638 638 end
639 639
640 640 def test_index_sort_by_author_desc
641 641 get :index, :sort => 'author:desc'
642 642 assert_response :success
643 643 authors = assigns(:issues).collect(&:author)
644 644 assert_equal authors.sort.reverse, authors
645 645 end
646 646
647 647 def test_index_group_by_author
648 648 get :index, :group_by => 'author', :sort => 'priority'
649 649 assert_response :success
650 650 end
651 651
652 652 def test_index_sort_by_spent_hours
653 653 get :index, :sort => 'spent_hours:desc'
654 654 assert_response :success
655 655 hours = assigns(:issues).collect(&:spent_hours)
656 656 assert_equal hours.sort.reverse, hours
657 657 end
658 658
659 659 def test_index_sort_by_user_custom_field
660 660 cf = IssueCustomField.create!(:name => 'User', :is_for_all => true, :tracker_ids => [1,2,3], :field_format => 'user')
661 661 CustomValue.create!(:custom_field => cf, :customized => Issue.find(1), :value => '2')
662 662 CustomValue.create!(:custom_field => cf, :customized => Issue.find(2), :value => '3')
663 663 CustomValue.create!(:custom_field => cf, :customized => Issue.find(3), :value => '3')
664 664 CustomValue.create!(:custom_field => cf, :customized => Issue.find(5), :value => '')
665 665
666 666 get :index, :project_id => 1, :set_filter => 1, :sort => "cf_#{cf.id},id"
667 667 assert_response :success
668 668
669 669 assert_equal [2, 3, 1], assigns(:issues).select {|issue| issue.custom_field_value(cf).present?}.map(&:id)
670 670 end
671 671
672 672 def test_index_with_columns
673 673 columns = ['tracker', 'subject', 'assigned_to']
674 674 get :index, :set_filter => 1, :c => columns
675 675 assert_response :success
676 676
677 677 # query should use specified columns
678 678 query = assigns(:query)
679 679 assert_kind_of Query, query
680 680 assert_equal columns, query.column_names.map(&:to_s)
681 681
682 682 # columns should be stored in session
683 683 assert_kind_of Hash, session[:query]
684 684 assert_kind_of Array, session[:query][:column_names]
685 685 assert_equal columns, session[:query][:column_names].map(&:to_s)
686 686
687 687 # ensure only these columns are kept in the selected columns list
688 688 assert_tag :tag => 'select', :attributes => { :id => 'selected_columns' },
689 689 :children => { :count => 3 }
690 690 assert_no_tag :tag => 'option', :attributes => { :value => 'project' },
691 691 :parent => { :tag => 'select', :attributes => { :id => "selected_columns" } }
692 692 end
693 693
694 694 def test_index_without_project_should_implicitly_add_project_column_to_default_columns
695 695 Setting.issue_list_default_columns = ['tracker', 'subject', 'assigned_to']
696 696 get :index, :set_filter => 1
697 697
698 698 # query should use specified columns
699 699 query = assigns(:query)
700 700 assert_kind_of Query, query
701 701 assert_equal [:project, :tracker, :subject, :assigned_to], query.columns.map(&:name)
702 702 end
703 703
704 704 def test_index_without_project_and_explicit_default_columns_should_not_add_project_column
705 705 Setting.issue_list_default_columns = ['tracker', 'subject', 'assigned_to']
706 706 columns = ['tracker', 'subject', 'assigned_to']
707 707 get :index, :set_filter => 1, :c => columns
708 708
709 709 # query should use specified columns
710 710 query = assigns(:query)
711 711 assert_kind_of Query, query
712 712 assert_equal columns.map(&:to_sym), query.columns.map(&:name)
713 713 end
714 714
715 715 def test_index_with_custom_field_column
716 716 columns = %w(tracker subject cf_2)
717 717 get :index, :set_filter => 1, :c => columns
718 718 assert_response :success
719 719
720 720 # query should use specified columns
721 721 query = assigns(:query)
722 722 assert_kind_of Query, query
723 723 assert_equal columns, query.column_names.map(&:to_s)
724 724
725 725 assert_tag :td,
726 726 :attributes => {:class => 'cf_2 string'},
727 727 :ancestor => {:tag => 'table', :attributes => {:class => /issues/}}
728 728 end
729 729
730 730 def test_index_with_multi_custom_field_column
731 731 field = CustomField.find(1)
732 732 field.update_attribute :multiple, true
733 733 issue = Issue.find(1)
734 734 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
735 735 issue.save!
736 736
737 737 get :index, :set_filter => 1, :c => %w(tracker subject cf_1)
738 738 assert_response :success
739 739
740 740 assert_tag :td,
741 741 :attributes => {:class => /cf_1/},
742 742 :content => 'MySQL, Oracle'
743 743 end
744 744
745 745 def test_index_with_multi_user_custom_field_column
746 746 field = IssueCustomField.create!(:name => 'Multi user', :field_format => 'user', :multiple => true,
747 747 :tracker_ids => [1], :is_for_all => true)
748 748 issue = Issue.find(1)
749 749 issue.custom_field_values = {field.id => ['2', '3']}
750 750 issue.save!
751 751
752 752 get :index, :set_filter => 1, :c => ['tracker', 'subject', "cf_#{field.id}"]
753 753 assert_response :success
754 754
755 755 assert_tag :td,
756 756 :attributes => {:class => /cf_#{field.id}/},
757 757 :child => {:tag => 'a', :content => 'John Smith'}
758 758 end
759 759
760 760 def test_index_with_date_column
761 761 Issue.find(1).update_attribute :start_date, '1987-08-24'
762 762
763 763 with_settings :date_format => '%d/%m/%Y' do
764 764 get :index, :set_filter => 1, :c => %w(start_date)
765 765 assert_tag 'td', :attributes => {:class => /start_date/}, :content => '24/08/1987'
766 766 end
767 767 end
768 768
769 769 def test_index_with_done_ratio_column
770 770 Issue.find(1).update_attribute :done_ratio, 40
771 771
772 772 get :index, :set_filter => 1, :c => %w(done_ratio)
773 773 assert_tag 'td', :attributes => {:class => /done_ratio/},
774 774 :child => {:tag => 'table', :attributes => {:class => 'progress'},
775 775 :descendant => {:tag => 'td', :attributes => {:class => 'closed', :style => 'width: 40%;'}}
776 776 }
777 777 end
778 778
779 779 def test_index_with_spent_hours_column
780 780 get :index, :set_filter => 1, :c => %w(subject spent_hours)
781 781
782 782 assert_tag 'tr', :attributes => {:id => 'issue-3'},
783 783 :child => {
784 784 :tag => 'td', :attributes => {:class => /spent_hours/}, :content => '1.00'
785 785 }
786 786 end
787 787
788 788 def test_index_should_not_show_spent_hours_column_without_permission
789 789 Role.anonymous.remove_permission! :view_time_entries
790 790 get :index, :set_filter => 1, :c => %w(subject spent_hours)
791 791
792 792 assert_no_tag 'td', :attributes => {:class => /spent_hours/}
793 793 end
794 794
795 795 def test_index_with_fixed_version_column
796 796 get :index, :set_filter => 1, :c => %w(fixed_version)
797 797 assert_tag 'td', :attributes => {:class => /fixed_version/},
798 798 :child => {:tag => 'a', :content => '1.0', :attributes => {:href => '/versions/2'}}
799 799 end
800 800
801 801 def test_index_with_relations_column
802 802 IssueRelation.delete_all
803 803 IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(1), :issue_to => Issue.find(7))
804 804 IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(8), :issue_to => Issue.find(1))
805 805 IssueRelation.create!(:relation_type => "blocks", :issue_from => Issue.find(1), :issue_to => Issue.find(11))
806 806 IssueRelation.create!(:relation_type => "blocks", :issue_from => Issue.find(12), :issue_to => Issue.find(2))
807 807
808 808 get :index, :set_filter => 1, :c => %w(subject relations)
809 809 assert_response :success
810 810 assert_select "tr#issue-1 td.relations" do
811 811 assert_select "span", 3
812 812 assert_select "span", :text => "Related to #7"
813 813 assert_select "span", :text => "Related to #8"
814 814 assert_select "span", :text => "Blocks #11"
815 815 end
816 816 assert_select "tr#issue-2 td.relations" do
817 817 assert_select "span", 1
818 818 assert_select "span", :text => "Blocked by #12"
819 819 end
820 820 assert_select "tr#issue-3 td.relations" do
821 821 assert_select "span", 0
822 822 end
823 823
824 824 get :index, :set_filter => 1, :c => %w(relations), :format => 'csv'
825 825 assert_response :success
826 826 assert_equal 'text/csv; header=present', response.content_type
827 827 lines = response.body.chomp.split("\n")
828 828 assert_include '1,"Related to #7, Related to #8, Blocks #11"', lines
829 829 assert_include '2,Blocked by #12', lines
830 830 assert_include '3,""', lines
831 831
832 832 get :index, :set_filter => 1, :c => %w(subject relations), :format => 'pdf'
833 833 assert_response :success
834 834 assert_equal 'application/pdf', response.content_type
835 835 end
836 836
837 837 def test_index_send_html_if_query_is_invalid
838 838 get :index, :f => ['start_date'], :op => {:start_date => '='}
839 839 assert_equal 'text/html', @response.content_type
840 840 assert_template 'index'
841 841 end
842 842
843 843 def test_index_send_nothing_if_query_is_invalid
844 844 get :index, :f => ['start_date'], :op => {:start_date => '='}, :format => 'csv'
845 845 assert_equal 'text/csv', @response.content_type
846 846 assert @response.body.blank?
847 847 end
848 848
849 849 def test_show_by_anonymous
850 850 get :show, :id => 1
851 851 assert_response :success
852 852 assert_template 'show'
853 853 assert_not_nil assigns(:issue)
854 854 assert_equal Issue.find(1), assigns(:issue)
855 855
856 856 # anonymous role is allowed to add a note
857 857 assert_tag :tag => 'form',
858 858 :descendant => { :tag => 'fieldset',
859 859 :child => { :tag => 'legend',
860 860 :content => /Notes/ } }
861 861 assert_tag :tag => 'title',
862 862 :content => "Bug #1: Can&#x27;t print recipes - eCookbook - Redmine"
863 863 end
864 864
865 865 def test_show_by_manager
866 866 @request.session[:user_id] = 2
867 867 get :show, :id => 1
868 868 assert_response :success
869 869
870 870 assert_tag :tag => 'a',
871 871 :content => /Quote/
872 872
873 873 assert_tag :tag => 'form',
874 874 :descendant => { :tag => 'fieldset',
875 875 :child => { :tag => 'legend',
876 876 :content => /Change properties/ } },
877 877 :descendant => { :tag => 'fieldset',
878 878 :child => { :tag => 'legend',
879 879 :content => /Log time/ } },
880 880 :descendant => { :tag => 'fieldset',
881 881 :child => { :tag => 'legend',
882 882 :content => /Notes/ } }
883 883 end
884 884
885 885 def test_show_should_display_update_form
886 886 @request.session[:user_id] = 2
887 887 get :show, :id => 1
888 888 assert_response :success
889 889
890 890 assert_tag 'form', :attributes => {:id => 'issue-form'}
891 891 assert_tag 'input', :attributes => {:name => 'issue[is_private]'}
892 892 assert_tag 'select', :attributes => {:name => 'issue[project_id]'}
893 893 assert_tag 'select', :attributes => {:name => 'issue[tracker_id]'}
894 894 assert_tag 'input', :attributes => {:name => 'issue[subject]'}
895 895 assert_tag 'textarea', :attributes => {:name => 'issue[description]'}
896 896 assert_tag 'select', :attributes => {:name => 'issue[status_id]'}
897 897 assert_tag 'select', :attributes => {:name => 'issue[priority_id]'}
898 898 assert_tag 'select', :attributes => {:name => 'issue[assigned_to_id]'}
899 899 assert_tag 'select', :attributes => {:name => 'issue[category_id]'}
900 900 assert_tag 'select', :attributes => {:name => 'issue[fixed_version_id]'}
901 901 assert_tag 'input', :attributes => {:name => 'issue[parent_issue_id]'}
902 902 assert_tag 'input', :attributes => {:name => 'issue[start_date]'}
903 903 assert_tag 'input', :attributes => {:name => 'issue[due_date]'}
904 904 assert_tag 'select', :attributes => {:name => 'issue[done_ratio]'}
905 905 assert_tag 'input', :attributes => { :name => 'issue[custom_field_values][2]' }
906 906 assert_no_tag 'input', :attributes => {:name => 'issue[watcher_user_ids][]'}
907 907 assert_tag 'textarea', :attributes => {:name => 'issue[notes]'}
908 908 end
909 909
910 910 def test_show_should_display_update_form_with_minimal_permissions
911 911 Role.find(1).update_attribute :permissions, [:view_issues, :add_issue_notes]
912 912 WorkflowTransition.delete_all :role_id => 1
913 913
914 914 @request.session[:user_id] = 2
915 915 get :show, :id => 1
916 916 assert_response :success
917 917
918 918 assert_tag 'form', :attributes => {:id => 'issue-form'}
919 919 assert_no_tag 'input', :attributes => {:name => 'issue[is_private]'}
920 920 assert_no_tag 'select', :attributes => {:name => 'issue[project_id]'}
921 921 assert_no_tag 'select', :attributes => {:name => 'issue[tracker_id]'}
922 922 assert_no_tag 'input', :attributes => {:name => 'issue[subject]'}
923 923 assert_no_tag 'textarea', :attributes => {:name => 'issue[description]'}
924 924 assert_no_tag 'select', :attributes => {:name => 'issue[status_id]'}
925 925 assert_no_tag 'select', :attributes => {:name => 'issue[priority_id]'}
926 926 assert_no_tag 'select', :attributes => {:name => 'issue[assigned_to_id]'}
927 927 assert_no_tag 'select', :attributes => {:name => 'issue[category_id]'}
928 928 assert_no_tag 'select', :attributes => {:name => 'issue[fixed_version_id]'}
929 929 assert_no_tag 'input', :attributes => {:name => 'issue[parent_issue_id]'}
930 930 assert_no_tag 'input', :attributes => {:name => 'issue[start_date]'}
931 931 assert_no_tag 'input', :attributes => {:name => 'issue[due_date]'}
932 932 assert_no_tag 'select', :attributes => {:name => 'issue[done_ratio]'}
933 933 assert_no_tag 'input', :attributes => { :name => 'issue[custom_field_values][2]' }
934 934 assert_no_tag 'input', :attributes => {:name => 'issue[watcher_user_ids][]'}
935 935 assert_tag 'textarea', :attributes => {:name => 'issue[notes]'}
936 936 end
937 937
938 938 def test_show_should_display_update_form_with_workflow_permissions
939 939 Role.find(1).update_attribute :permissions, [:view_issues, :add_issue_notes]
940 940
941 941 @request.session[:user_id] = 2
942 942 get :show, :id => 1
943 943 assert_response :success
944 944
945 945 assert_tag 'form', :attributes => {:id => 'issue-form'}
946 946 assert_no_tag 'input', :attributes => {:name => 'issue[is_private]'}
947 947 assert_no_tag 'select', :attributes => {:name => 'issue[project_id]'}
948 948 assert_no_tag 'select', :attributes => {:name => 'issue[tracker_id]'}
949 949 assert_no_tag 'input', :attributes => {:name => 'issue[subject]'}
950 950 assert_no_tag 'textarea', :attributes => {:name => 'issue[description]'}
951 951 assert_tag 'select', :attributes => {:name => 'issue[status_id]'}
952 952 assert_no_tag 'select', :attributes => {:name => 'issue[priority_id]'}
953 953 assert_tag 'select', :attributes => {:name => 'issue[assigned_to_id]'}
954 954 assert_no_tag 'select', :attributes => {:name => 'issue[category_id]'}
955 955 assert_tag 'select', :attributes => {:name => 'issue[fixed_version_id]'}
956 956 assert_no_tag 'input', :attributes => {:name => 'issue[parent_issue_id]'}
957 957 assert_no_tag 'input', :attributes => {:name => 'issue[start_date]'}
958 958 assert_no_tag 'input', :attributes => {:name => 'issue[due_date]'}
959 959 assert_tag 'select', :attributes => {:name => 'issue[done_ratio]'}
960 960 assert_no_tag 'input', :attributes => { :name => 'issue[custom_field_values][2]' }
961 961 assert_no_tag 'input', :attributes => {:name => 'issue[watcher_user_ids][]'}
962 962 assert_tag 'textarea', :attributes => {:name => 'issue[notes]'}
963 963 end
964 964
965 965 def test_show_should_not_display_update_form_without_permissions
966 966 Role.find(1).update_attribute :permissions, [:view_issues]
967 967
968 968 @request.session[:user_id] = 2
969 969 get :show, :id => 1
970 970 assert_response :success
971 971
972 972 assert_no_tag 'form', :attributes => {:id => 'issue-form'}
973 973 end
974 974
975 975 def test_update_form_should_not_display_inactive_enumerations
976 976 @request.session[:user_id] = 2
977 977 get :show, :id => 1
978 978 assert_response :success
979 979
980 980 assert ! IssuePriority.find(15).active?
981 981 assert_no_tag :option, :attributes => {:value => '15'},
982 982 :parent => {:tag => 'select', :attributes => {:id => 'issue_priority_id'} }
983 983 end
984 984
985 985 def test_update_form_should_allow_attachment_upload
986 986 @request.session[:user_id] = 2
987 987 get :show, :id => 1
988 988
989 989 assert_tag :tag => 'form',
990 990 :attributes => {:id => 'issue-form', :method => 'post', :enctype => 'multipart/form-data'},
991 991 :descendant => {
992 992 :tag => 'input',
993 993 :attributes => {:type => 'file', :name => 'attachments[1][file]'}
994 994 }
995 995 end
996 996
997 997 def test_show_should_deny_anonymous_access_without_permission
998 998 Role.anonymous.remove_permission!(:view_issues)
999 999 get :show, :id => 1
1000 1000 assert_response :redirect
1001 1001 end
1002 1002
1003 1003 def test_show_should_deny_anonymous_access_to_private_issue
1004 1004 Issue.update_all(["is_private = ?", true], "id = 1")
1005 1005 get :show, :id => 1
1006 1006 assert_response :redirect
1007 1007 end
1008 1008
1009 1009 def test_show_should_deny_non_member_access_without_permission
1010 1010 Role.non_member.remove_permission!(:view_issues)
1011 1011 @request.session[:user_id] = 9
1012 1012 get :show, :id => 1
1013 1013 assert_response 403
1014 1014 end
1015 1015
1016 1016 def test_show_should_deny_non_member_access_to_private_issue
1017 1017 Issue.update_all(["is_private = ?", true], "id = 1")
1018 1018 @request.session[:user_id] = 9
1019 1019 get :show, :id => 1
1020 1020 assert_response 403
1021 1021 end
1022 1022
1023 1023 def test_show_should_deny_member_access_without_permission
1024 1024 Role.find(1).remove_permission!(:view_issues)
1025 1025 @request.session[:user_id] = 2
1026 1026 get :show, :id => 1
1027 1027 assert_response 403
1028 1028 end
1029 1029
1030 1030 def test_show_should_deny_member_access_to_private_issue_without_permission
1031 1031 Issue.update_all(["is_private = ?", true], "id = 1")
1032 1032 @request.session[:user_id] = 3
1033 1033 get :show, :id => 1
1034 1034 assert_response 403
1035 1035 end
1036 1036
1037 1037 def test_show_should_allow_author_access_to_private_issue
1038 1038 Issue.update_all(["is_private = ?, author_id = 3", true], "id = 1")
1039 1039 @request.session[:user_id] = 3
1040 1040 get :show, :id => 1
1041 1041 assert_response :success
1042 1042 end
1043 1043
1044 1044 def test_show_should_allow_assignee_access_to_private_issue
1045 1045 Issue.update_all(["is_private = ?, assigned_to_id = 3", true], "id = 1")
1046 1046 @request.session[:user_id] = 3
1047 1047 get :show, :id => 1
1048 1048 assert_response :success
1049 1049 end
1050 1050
1051 1051 def test_show_should_allow_member_access_to_private_issue_with_permission
1052 1052 Issue.update_all(["is_private = ?", true], "id = 1")
1053 1053 User.find(3).roles_for_project(Project.find(1)).first.update_attribute :issues_visibility, 'all'
1054 1054 @request.session[:user_id] = 3
1055 1055 get :show, :id => 1
1056 1056 assert_response :success
1057 1057 end
1058 1058
1059 1059 def test_show_should_not_disclose_relations_to_invisible_issues
1060 1060 Setting.cross_project_issue_relations = '1'
1061 1061 IssueRelation.create!(:issue_from => Issue.find(1), :issue_to => Issue.find(2), :relation_type => 'relates')
1062 1062 # Relation to a private project issue
1063 1063 IssueRelation.create!(:issue_from => Issue.find(1), :issue_to => Issue.find(4), :relation_type => 'relates')
1064 1064
1065 1065 get :show, :id => 1
1066 1066 assert_response :success
1067 1067
1068 1068 assert_tag :div, :attributes => { :id => 'relations' },
1069 1069 :descendant => { :tag => 'a', :content => /#2$/ }
1070 1070 assert_no_tag :div, :attributes => { :id => 'relations' },
1071 1071 :descendant => { :tag => 'a', :content => /#4$/ }
1072 1072 end
1073 1073
1074 1074 def test_show_should_list_subtasks
1075 1075 Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :parent_issue_id => 1, :subject => 'Child Issue')
1076 1076
1077 1077 get :show, :id => 1
1078 1078 assert_response :success
1079 1079 assert_tag 'div', :attributes => {:id => 'issue_tree'},
1080 1080 :descendant => {:tag => 'td', :content => /Child Issue/, :attributes => {:class => /subject/}}
1081 1081 end
1082 1082
1083 1083 def test_show_should_list_parents
1084 1084 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :parent_issue_id => 1, :subject => 'Child Issue')
1085 1085
1086 1086 get :show, :id => issue.id
1087 1087 assert_response :success
1088 1088 assert_tag 'div', :attributes => {:class => 'subject'},
1089 1089 :descendant => {:tag => 'h3', :content => 'Child Issue'}
1090 1090 assert_tag 'div', :attributes => {:class => 'subject'},
1091 1091 :descendant => {:tag => 'a', :attributes => {:href => '/issues/1'}}
1092 1092 end
1093 1093
1094 1094 def test_show_should_not_display_prev_next_links_without_query_in_session
1095 1095 get :show, :id => 1
1096 1096 assert_response :success
1097 1097 assert_nil assigns(:prev_issue_id)
1098 1098 assert_nil assigns(:next_issue_id)
1099 1099
1100 1100 assert_no_tag 'div', :attributes => {:class => /next-prev-links/}
1101 1101 end
1102 1102
1103 1103 def test_show_should_display_prev_next_links_with_query_in_session
1104 1104 @request.session[:query] = {:filters => {'status_id' => {:values => [''], :operator => 'o'}}, :project_id => nil}
1105 1105 @request.session['issues_index_sort'] = 'id'
1106 1106
1107 1107 with_settings :display_subprojects_issues => '0' do
1108 1108 get :show, :id => 3
1109 1109 end
1110 1110
1111 1111 assert_response :success
1112 1112 # Previous and next issues for all projects
1113 1113 assert_equal 2, assigns(:prev_issue_id)
1114 1114 assert_equal 5, assigns(:next_issue_id)
1115 1115
1116 1116 assert_tag 'div', :attributes => {:class => /next-prev-links/}
1117 1117 assert_tag 'a', :attributes => {:href => '/issues/2'}, :content => /Previous/
1118 1118 assert_tag 'a', :attributes => {:href => '/issues/5'}, :content => /Next/
1119 1119
1120 1120 count = Issue.open.visible.count
1121 1121 assert_tag 'span', :attributes => {:class => 'position'}, :content => "3 of #{count}"
1122 1122 end
1123 1123
1124 1124 def test_show_should_display_prev_next_links_with_saved_query_in_session
1125 1125 query = Query.create!(:name => 'test', :is_public => true, :user_id => 1,
1126 1126 :filters => {'status_id' => {:values => ['5'], :operator => '='}},
1127 1127 :sort_criteria => [['id', 'asc']])
1128 1128 @request.session[:query] = {:id => query.id, :project_id => nil}
1129 1129
1130 1130 get :show, :id => 11
1131 1131
1132 1132 assert_response :success
1133 1133 assert_equal query, assigns(:query)
1134 1134 # Previous and next issues for all projects
1135 1135 assert_equal 8, assigns(:prev_issue_id)
1136 1136 assert_equal 12, assigns(:next_issue_id)
1137 1137
1138 1138 assert_tag 'a', :attributes => {:href => '/issues/8'}, :content => /Previous/
1139 1139 assert_tag 'a', :attributes => {:href => '/issues/12'}, :content => /Next/
1140 1140 end
1141 1141
1142 1142 def test_show_should_display_prev_next_links_with_query_and_sort_on_association
1143 1143 @request.session[:query] = {:filters => {'status_id' => {:values => [''], :operator => 'o'}}, :project_id => nil}
1144 1144
1145 1145 %w(project tracker status priority author assigned_to category fixed_version).each do |assoc_sort|
1146 1146 @request.session['issues_index_sort'] = assoc_sort
1147 1147
1148 1148 get :show, :id => 3
1149 1149 assert_response :success, "Wrong response status for #{assoc_sort} sort"
1150 1150
1151 1151 assert_tag 'div', :attributes => {:class => /next-prev-links/}, :content => /Previous/
1152 1152 assert_tag 'div', :attributes => {:class => /next-prev-links/}, :content => /Next/
1153 1153 end
1154 1154 end
1155 1155
1156 1156 def test_show_should_display_prev_next_links_with_project_query_in_session
1157 1157 @request.session[:query] = {:filters => {'status_id' => {:values => [''], :operator => 'o'}}, :project_id => 1}
1158 1158 @request.session['issues_index_sort'] = 'id'
1159 1159
1160 1160 with_settings :display_subprojects_issues => '0' do
1161 1161 get :show, :id => 3
1162 1162 end
1163 1163
1164 1164 assert_response :success
1165 1165 # Previous and next issues inside project
1166 1166 assert_equal 2, assigns(:prev_issue_id)
1167 1167 assert_equal 7, assigns(:next_issue_id)
1168 1168
1169 1169 assert_tag 'a', :attributes => {:href => '/issues/2'}, :content => /Previous/
1170 1170 assert_tag 'a', :attributes => {:href => '/issues/7'}, :content => /Next/
1171 1171 end
1172 1172
1173 1173 def test_show_should_not_display_prev_link_for_first_issue
1174 1174 @request.session[:query] = {:filters => {'status_id' => {:values => [''], :operator => 'o'}}, :project_id => 1}
1175 1175 @request.session['issues_index_sort'] = 'id'
1176 1176
1177 1177 with_settings :display_subprojects_issues => '0' do
1178 1178 get :show, :id => 1
1179 1179 end
1180 1180
1181 1181 assert_response :success
1182 1182 assert_nil assigns(:prev_issue_id)
1183 1183 assert_equal 2, assigns(:next_issue_id)
1184 1184
1185 1185 assert_no_tag 'a', :content => /Previous/
1186 1186 assert_tag 'a', :attributes => {:href => '/issues/2'}, :content => /Next/
1187 1187 end
1188 1188
1189 1189 def test_show_should_not_display_prev_next_links_for_issue_not_in_query_results
1190 1190 @request.session[:query] = {:filters => {'status_id' => {:values => [''], :operator => 'c'}}, :project_id => 1}
1191 1191 @request.session['issues_index_sort'] = 'id'
1192 1192
1193 1193 get :show, :id => 1
1194 1194
1195 1195 assert_response :success
1196 1196 assert_nil assigns(:prev_issue_id)
1197 1197 assert_nil assigns(:next_issue_id)
1198 1198
1199 1199 assert_no_tag 'a', :content => /Previous/
1200 1200 assert_no_tag 'a', :content => /Next/
1201 1201 end
1202 1202
1203 1203 def test_show_show_should_display_prev_next_links_with_query_sort_by_user_custom_field
1204 1204 cf = IssueCustomField.create!(:name => 'User', :is_for_all => true, :tracker_ids => [1,2,3], :field_format => 'user')
1205 1205 CustomValue.create!(:custom_field => cf, :customized => Issue.find(1), :value => '2')
1206 1206 CustomValue.create!(:custom_field => cf, :customized => Issue.find(2), :value => '3')
1207 1207 CustomValue.create!(:custom_field => cf, :customized => Issue.find(3), :value => '3')
1208 1208 CustomValue.create!(:custom_field => cf, :customized => Issue.find(5), :value => '')
1209 1209
1210 1210 query = Query.create!(:name => 'test', :is_public => true, :user_id => 1, :filters => {},
1211 1211 :sort_criteria => [["cf_#{cf.id}", 'asc'], ['id', 'asc']])
1212 1212 @request.session[:query] = {:id => query.id, :project_id => nil}
1213 1213
1214 1214 get :show, :id => 3
1215 1215 assert_response :success
1216 1216
1217 1217 assert_equal 2, assigns(:prev_issue_id)
1218 1218 assert_equal 1, assigns(:next_issue_id)
1219 1219 end
1220 1220
1221 1221 def test_show_should_display_link_to_the_assignee
1222 1222 get :show, :id => 2
1223 1223 assert_response :success
1224 1224 assert_select '.assigned-to' do
1225 1225 assert_select 'a[href=/users/3]'
1226 1226 end
1227 1227 end
1228 1228
1229 1229 def test_show_should_display_visible_changesets_from_other_projects
1230 1230 project = Project.find(2)
1231 1231 issue = project.issues.first
1232 1232 issue.changeset_ids = [102]
1233 1233 issue.save!
1234 1234 project.disable_module! :repository
1235 1235
1236 1236 @request.session[:user_id] = 2
1237 1237 get :show, :id => issue.id
1238 1238 assert_tag 'a', :attributes => {:href => "/projects/ecookbook/repository/revisions/3"}
1239 1239 end
1240 1240
1241 1241 def test_show_should_display_watchers
1242 1242 @request.session[:user_id] = 2
1243 1243 Issue.find(1).add_watcher User.find(2)
1244 1244
1245 1245 get :show, :id => 1
1246 1246 assert_select 'div#watchers ul' do
1247 1247 assert_select 'li' do
1248 1248 assert_select 'a[href=/users/2]'
1249 1249 assert_select 'a img[alt=Delete]'
1250 1250 end
1251 1251 end
1252 1252 end
1253 1253
1254 1254 def test_show_should_display_watchers_with_gravatars
1255 1255 @request.session[:user_id] = 2
1256 1256 Issue.find(1).add_watcher User.find(2)
1257 1257
1258 1258 with_settings :gravatar_enabled => '1' do
1259 1259 get :show, :id => 1
1260 1260 end
1261 1261
1262 1262 assert_select 'div#watchers ul' do
1263 1263 assert_select 'li' do
1264 1264 assert_select 'img.gravatar'
1265 1265 assert_select 'a[href=/users/2]'
1266 1266 assert_select 'a img[alt=Delete]'
1267 1267 end
1268 1268 end
1269 1269 end
1270 1270
1271 1271 def test_show_with_thumbnails_enabled_should_display_thumbnails
1272 1272 @request.session[:user_id] = 2
1273 1273
1274 1274 with_settings :thumbnails_enabled => '1' do
1275 1275 get :show, :id => 14
1276 1276 assert_response :success
1277 1277 end
1278 1278
1279 1279 assert_select 'div.thumbnails' do
1280 1280 assert_select 'a[href=/attachments/16/testfile.png]' do
1281 1281 assert_select 'img[src=/attachments/thumbnail/16]'
1282 1282 end
1283 1283 end
1284 1284 end
1285 1285
1286 1286 def test_show_with_thumbnails_disabled_should_not_display_thumbnails
1287 1287 @request.session[:user_id] = 2
1288 1288
1289 1289 with_settings :thumbnails_enabled => '0' do
1290 1290 get :show, :id => 14
1291 1291 assert_response :success
1292 1292 end
1293 1293
1294 1294 assert_select 'div.thumbnails', 0
1295 1295 end
1296 1296
1297 1297 def test_show_with_multi_custom_field
1298 1298 field = CustomField.find(1)
1299 1299 field.update_attribute :multiple, true
1300 1300 issue = Issue.find(1)
1301 1301 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
1302 1302 issue.save!
1303 1303
1304 1304 get :show, :id => 1
1305 1305 assert_response :success
1306 1306
1307 1307 assert_tag :td, :content => 'MySQL, Oracle'
1308 1308 end
1309 1309
1310 1310 def test_show_with_multi_user_custom_field
1311 1311 field = IssueCustomField.create!(:name => 'Multi user', :field_format => 'user', :multiple => true,
1312 1312 :tracker_ids => [1], :is_for_all => true)
1313 1313 issue = Issue.find(1)
1314 1314 issue.custom_field_values = {field.id => ['2', '3']}
1315 1315 issue.save!
1316 1316
1317 1317 get :show, :id => 1
1318 1318 assert_response :success
1319 1319
1320 1320 # TODO: should display links
1321 1321 assert_tag :td, :content => 'Dave Lopper, John Smith'
1322 1322 end
1323 1323
1324 1324 def test_show_should_display_private_notes_with_permission_only
1325 1325 journal = Journal.create!(:journalized => Issue.find(2), :notes => 'Privates notes', :private_notes => true, :user_id => 1)
1326 1326 @request.session[:user_id] = 2
1327 1327
1328 1328 get :show, :id => 2
1329 1329 assert_response :success
1330 1330 assert_include journal, assigns(:journals)
1331 1331
1332 1332 Role.find(1).remove_permission! :view_private_notes
1333 1333 get :show, :id => 2
1334 1334 assert_response :success
1335 1335 assert_not_include journal, assigns(:journals)
1336 1336 end
1337 1337
1338 1338 def test_show_atom
1339 1339 get :show, :id => 2, :format => 'atom'
1340 1340 assert_response :success
1341 1341 assert_template 'journals/index'
1342 1342 # Inline image
1343 1343 assert_select 'content', :text => Regexp.new(Regexp.quote('http://test.host/attachments/download/10'))
1344 1344 end
1345 1345
1346 1346 def test_show_export_to_pdf
1347 1347 get :show, :id => 3, :format => 'pdf'
1348 1348 assert_response :success
1349 1349 assert_equal 'application/pdf', @response.content_type
1350 1350 assert @response.body.starts_with?('%PDF')
1351 1351 assert_not_nil assigns(:issue)
1352 1352 end
1353 1353
1354 1354 def test_show_export_to_pdf_with_ancestors
1355 1355 issue = Issue.generate!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'child', :parent_issue_id => 1)
1356 1356
1357 1357 get :show, :id => issue.id, :format => 'pdf'
1358 1358 assert_response :success
1359 1359 assert_equal 'application/pdf', @response.content_type
1360 1360 assert @response.body.starts_with?('%PDF')
1361 1361 end
1362 1362
1363 1363 def test_show_export_to_pdf_with_descendants
1364 1364 c1 = Issue.generate!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'child', :parent_issue_id => 1)
1365 1365 c2 = Issue.generate!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'child', :parent_issue_id => 1)
1366 1366 c3 = Issue.generate!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'child', :parent_issue_id => c1.id)
1367 1367
1368 1368 get :show, :id => 1, :format => 'pdf'
1369 1369 assert_response :success
1370 1370 assert_equal 'application/pdf', @response.content_type
1371 1371 assert @response.body.starts_with?('%PDF')
1372 1372 end
1373 1373
1374 1374 def test_show_export_to_pdf_with_journals
1375 1375 get :show, :id => 1, :format => 'pdf'
1376 1376 assert_response :success
1377 1377 assert_equal 'application/pdf', @response.content_type
1378 1378 assert @response.body.starts_with?('%PDF')
1379 1379 end
1380 1380
1381 1381 def test_show_export_to_pdf_with_changesets
1382 1382 Issue.find(3).changesets = Changeset.find_all_by_id(100, 101, 102)
1383 1383
1384 1384 get :show, :id => 3, :format => 'pdf'
1385 1385 assert_response :success
1386 1386 assert_equal 'application/pdf', @response.content_type
1387 1387 assert @response.body.starts_with?('%PDF')
1388 1388 end
1389 1389
1390 1390 def test_get_new
1391 1391 @request.session[:user_id] = 2
1392 1392 get :new, :project_id => 1, :tracker_id => 1
1393 1393 assert_response :success
1394 1394 assert_template 'new'
1395 1395
1396 1396 assert_tag 'input', :attributes => {:name => 'issue[is_private]'}
1397 1397 assert_no_tag 'select', :attributes => {:name => 'issue[project_id]'}
1398 1398 assert_tag 'select', :attributes => {:name => 'issue[tracker_id]'}
1399 1399 assert_tag 'input', :attributes => {:name => 'issue[subject]'}
1400 1400 assert_tag 'textarea', :attributes => {:name => 'issue[description]'}
1401 1401 assert_tag 'select', :attributes => {:name => 'issue[status_id]'}
1402 1402 assert_tag 'select', :attributes => {:name => 'issue[priority_id]'}
1403 1403 assert_tag 'select', :attributes => {:name => 'issue[assigned_to_id]'}
1404 1404 assert_tag 'select', :attributes => {:name => 'issue[category_id]'}
1405 1405 assert_tag 'select', :attributes => {:name => 'issue[fixed_version_id]'}
1406 1406 assert_tag 'input', :attributes => {:name => 'issue[parent_issue_id]'}
1407 1407 assert_tag 'input', :attributes => {:name => 'issue[start_date]'}
1408 1408 assert_tag 'input', :attributes => {:name => 'issue[due_date]'}
1409 1409 assert_tag 'select', :attributes => {:name => 'issue[done_ratio]'}
1410 1410 assert_tag 'input', :attributes => { :name => 'issue[custom_field_values][2]', :value => 'Default string' }
1411 1411 assert_tag 'input', :attributes => {:name => 'issue[watcher_user_ids][]'}
1412 1412
1413 1413 # Be sure we don't display inactive IssuePriorities
1414 1414 assert ! IssuePriority.find(15).active?
1415 1415 assert_no_tag :option, :attributes => {:value => '15'},
1416 1416 :parent => {:tag => 'select', :attributes => {:id => 'issue_priority_id'} }
1417 1417 end
1418 1418
1419 1419 def test_get_new_with_minimal_permissions
1420 1420 Role.find(1).update_attribute :permissions, [:add_issues]
1421 1421 WorkflowTransition.delete_all :role_id => 1
1422 1422
1423 1423 @request.session[:user_id] = 2
1424 1424 get :new, :project_id => 1, :tracker_id => 1
1425 1425 assert_response :success
1426 1426 assert_template 'new'
1427 1427
1428 1428 assert_no_tag 'input', :attributes => {:name => 'issue[is_private]'}
1429 1429 assert_no_tag 'select', :attributes => {:name => 'issue[project_id]'}
1430 1430 assert_tag 'select', :attributes => {:name => 'issue[tracker_id]'}
1431 1431 assert_tag 'input', :attributes => {:name => 'issue[subject]'}
1432 1432 assert_tag 'textarea', :attributes => {:name => 'issue[description]'}
1433 1433 assert_tag 'select', :attributes => {:name => 'issue[status_id]'}
1434 1434 assert_tag 'select', :attributes => {:name => 'issue[priority_id]'}
1435 1435 assert_tag 'select', :attributes => {:name => 'issue[assigned_to_id]'}
1436 1436 assert_tag 'select', :attributes => {:name => 'issue[category_id]'}
1437 1437 assert_tag 'select', :attributes => {:name => 'issue[fixed_version_id]'}
1438 1438 assert_no_tag 'input', :attributes => {:name => 'issue[parent_issue_id]'}
1439 1439 assert_tag 'input', :attributes => {:name => 'issue[start_date]'}
1440 1440 assert_tag 'input', :attributes => {:name => 'issue[due_date]'}
1441 1441 assert_tag 'select', :attributes => {:name => 'issue[done_ratio]'}
1442 1442 assert_tag 'input', :attributes => { :name => 'issue[custom_field_values][2]', :value => 'Default string' }
1443 1443 assert_no_tag 'input', :attributes => {:name => 'issue[watcher_user_ids][]'}
1444 1444 end
1445 1445
1446 1446 def test_get_new_with_list_custom_field
1447 1447 @request.session[:user_id] = 2
1448 1448 get :new, :project_id => 1, :tracker_id => 1
1449 1449 assert_response :success
1450 1450 assert_template 'new'
1451 1451
1452 1452 assert_tag 'select',
1453 1453 :attributes => {:name => 'issue[custom_field_values][1]', :class => 'list_cf'},
1454 1454 :children => {:count => 4},
1455 1455 :child => {:tag => 'option', :attributes => {:value => 'MySQL'}, :content => 'MySQL'}
1456 1456 end
1457 1457
1458 1458 def test_get_new_with_multi_custom_field
1459 1459 field = IssueCustomField.find(1)
1460 1460 field.update_attribute :multiple, true
1461 1461
1462 1462 @request.session[:user_id] = 2
1463 1463 get :new, :project_id => 1, :tracker_id => 1
1464 1464 assert_response :success
1465 1465 assert_template 'new'
1466 1466
1467 1467 assert_tag 'select',
1468 1468 :attributes => {:name => 'issue[custom_field_values][1][]', :multiple => 'multiple'},
1469 1469 :children => {:count => 3},
1470 1470 :child => {:tag => 'option', :attributes => {:value => 'MySQL'}, :content => 'MySQL'}
1471 1471 assert_tag 'input',
1472 1472 :attributes => {:name => 'issue[custom_field_values][1][]', :value => ''}
1473 1473 end
1474 1474
1475 1475 def test_get_new_with_multi_user_custom_field
1476 1476 field = IssueCustomField.create!(:name => 'Multi user', :field_format => 'user', :multiple => true,
1477 1477 :tracker_ids => [1], :is_for_all => true)
1478 1478
1479 1479 @request.session[:user_id] = 2
1480 1480 get :new, :project_id => 1, :tracker_id => 1
1481 1481 assert_response :success
1482 1482 assert_template 'new'
1483 1483
1484 1484 assert_tag 'select',
1485 1485 :attributes => {:name => "issue[custom_field_values][#{field.id}][]", :multiple => 'multiple'},
1486 1486 :children => {:count => Project.find(1).users.count},
1487 1487 :child => {:tag => 'option', :attributes => {:value => '2'}, :content => 'John Smith'}
1488 1488 assert_tag 'input',
1489 1489 :attributes => {:name => "issue[custom_field_values][#{field.id}][]", :value => ''}
1490 1490 end
1491 1491
1492 1492 def test_get_new_with_date_custom_field
1493 1493 field = IssueCustomField.create!(:name => 'Date', :field_format => 'date', :tracker_ids => [1], :is_for_all => true)
1494 1494
1495 1495 @request.session[:user_id] = 2
1496 1496 get :new, :project_id => 1, :tracker_id => 1
1497 1497 assert_response :success
1498 1498
1499 1499 assert_select 'input[name=?]', "issue[custom_field_values][#{field.id}]"
1500 1500 end
1501 1501
1502 1502 def test_get_new_with_text_custom_field
1503 1503 field = IssueCustomField.create!(:name => 'Text', :field_format => 'text', :tracker_ids => [1], :is_for_all => true)
1504 1504
1505 1505 @request.session[:user_id] = 2
1506 1506 get :new, :project_id => 1, :tracker_id => 1
1507 1507 assert_response :success
1508 1508
1509 1509 assert_select 'textarea[name=?]', "issue[custom_field_values][#{field.id}]"
1510 1510 end
1511 1511
1512 1512 def test_get_new_without_default_start_date_is_creation_date
1513 1513 Setting.default_issue_start_date_to_creation_date = 0
1514 1514
1515 1515 @request.session[:user_id] = 2
1516 1516 get :new, :project_id => 1, :tracker_id => 1
1517 1517 assert_response :success
1518 1518 assert_template 'new'
1519 1519
1520 1520 assert_tag :tag => 'input', :attributes => { :name => 'issue[start_date]',
1521 1521 :value => nil }
1522 1522 end
1523 1523
1524 1524 def test_get_new_with_default_start_date_is_creation_date
1525 1525 Setting.default_issue_start_date_to_creation_date = 1
1526 1526
1527 1527 @request.session[:user_id] = 2
1528 1528 get :new, :project_id => 1, :tracker_id => 1
1529 1529 assert_response :success
1530 1530 assert_template 'new'
1531 1531
1532 1532 assert_tag :tag => 'input', :attributes => { :name => 'issue[start_date]',
1533 1533 :value => Date.today.to_s }
1534 1534 end
1535 1535
1536 1536 def test_get_new_form_should_allow_attachment_upload
1537 1537 @request.session[:user_id] = 2
1538 1538 get :new, :project_id => 1, :tracker_id => 1
1539 1539
1540 1540 assert_select 'form[id=issue-form][method=post][enctype=multipart/form-data]' do
1541 1541 assert_select 'input[name=?][type=file]', 'attachments[1][file]'
1542 1542 assert_select 'input[name=?][maxlength=255]', 'attachments[1][description]'
1543 1543 end
1544 1544 end
1545 1545
1546 1546 def test_get_new_should_prefill_the_form_from_params
1547 1547 @request.session[:user_id] = 2
1548 1548 get :new, :project_id => 1,
1549 1549 :issue => {:tracker_id => 3, :description => 'Prefilled', :custom_field_values => {'2' => 'Custom field value'}}
1550 1550
1551 1551 issue = assigns(:issue)
1552 1552 assert_equal 3, issue.tracker_id
1553 1553 assert_equal 'Prefilled', issue.description
1554 1554 assert_equal 'Custom field value', issue.custom_field_value(2)
1555 1555
1556 1556 assert_tag 'select',
1557 1557 :attributes => {:name => 'issue[tracker_id]'},
1558 1558 :child => {:tag => 'option', :attributes => {:value => '3', :selected => 'selected'}}
1559 1559 assert_tag 'textarea',
1560 1560 :attributes => {:name => 'issue[description]'}, :content => "\nPrefilled"
1561 1561 assert_tag 'input',
1562 1562 :attributes => {:name => 'issue[custom_field_values][2]', :value => 'Custom field value'}
1563 1563 end
1564 1564
1565 1565 def test_get_new_should_mark_required_fields
1566 1566 cf1 = IssueCustomField.create!(:name => 'Foo', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
1567 1567 cf2 = IssueCustomField.create!(:name => 'Bar', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
1568 1568 WorkflowPermission.delete_all
1569 1569 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => 'due_date', :rule => 'required')
1570 1570 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => cf2.id.to_s, :rule => 'required')
1571 1571 @request.session[:user_id] = 2
1572 1572
1573 1573 get :new, :project_id => 1
1574 1574 assert_response :success
1575 1575 assert_template 'new'
1576 1576
1577 1577 assert_select 'label[for=issue_start_date]' do
1578 1578 assert_select 'span[class=required]', 0
1579 1579 end
1580 1580 assert_select 'label[for=issue_due_date]' do
1581 1581 assert_select 'span[class=required]'
1582 1582 end
1583 1583 assert_select 'label[for=?]', "issue_custom_field_values_#{cf1.id}" do
1584 1584 assert_select 'span[class=required]', 0
1585 1585 end
1586 1586 assert_select 'label[for=?]', "issue_custom_field_values_#{cf2.id}" do
1587 1587 assert_select 'span[class=required]'
1588 1588 end
1589 1589 end
1590 1590
1591 1591 def test_get_new_should_not_display_readonly_fields
1592 1592 cf1 = IssueCustomField.create!(:name => 'Foo', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
1593 1593 cf2 = IssueCustomField.create!(:name => 'Bar', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
1594 1594 WorkflowPermission.delete_all
1595 1595 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => 'due_date', :rule => 'readonly')
1596 1596 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => cf2.id.to_s, :rule => 'readonly')
1597 1597 @request.session[:user_id] = 2
1598 1598
1599 1599 get :new, :project_id => 1
1600 1600 assert_response :success
1601 1601 assert_template 'new'
1602 1602
1603 1603 assert_select 'input[name=?]', 'issue[start_date]'
1604 1604 assert_select 'input[name=?]', 'issue[due_date]', 0
1605 1605 assert_select 'input[name=?]', "issue[custom_field_values][#{cf1.id}]"
1606 1606 assert_select 'input[name=?]', "issue[custom_field_values][#{cf2.id}]", 0
1607 1607 end
1608 1608
1609 1609 def test_get_new_without_tracker_id
1610 1610 @request.session[:user_id] = 2
1611 1611 get :new, :project_id => 1
1612 1612 assert_response :success
1613 1613 assert_template 'new'
1614 1614
1615 1615 issue = assigns(:issue)
1616 1616 assert_not_nil issue
1617 1617 assert_equal Project.find(1).trackers.first, issue.tracker
1618 1618 end
1619 1619
1620 1620 def test_get_new_with_no_default_status_should_display_an_error
1621 1621 @request.session[:user_id] = 2
1622 1622 IssueStatus.delete_all
1623 1623
1624 1624 get :new, :project_id => 1
1625 1625 assert_response 500
1626 1626 assert_error_tag :content => /No default issue/
1627 1627 end
1628 1628
1629 1629 def test_get_new_with_no_tracker_should_display_an_error
1630 1630 @request.session[:user_id] = 2
1631 1631 Tracker.delete_all
1632 1632
1633 1633 get :new, :project_id => 1
1634 1634 assert_response 500
1635 1635 assert_error_tag :content => /No tracker/
1636 1636 end
1637 1637
1638 1638 def test_update_new_form
1639 1639 @request.session[:user_id] = 2
1640 1640 xhr :post, :new, :project_id => 1,
1641 1641 :issue => {:tracker_id => 2,
1642 1642 :subject => 'This is the test_new issue',
1643 1643 :description => 'This is the description',
1644 1644 :priority_id => 5}
1645 1645 assert_response :success
1646 1646 assert_template 'update_form'
1647 1647 assert_template 'form'
1648 1648 assert_equal 'text/javascript', response.content_type
1649 1649
1650 1650 issue = assigns(:issue)
1651 1651 assert_kind_of Issue, issue
1652 1652 assert_equal 1, issue.project_id
1653 1653 assert_equal 2, issue.tracker_id
1654 1654 assert_equal 'This is the test_new issue', issue.subject
1655 1655 end
1656 1656
1657 1657 def test_update_new_form_should_propose_transitions_based_on_initial_status
1658 1658 @request.session[:user_id] = 2
1659 1659 WorkflowTransition.delete_all
1660 1660 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 2)
1661 1661 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 5)
1662 1662 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 5, :new_status_id => 4)
1663 1663
1664 1664 xhr :post, :new, :project_id => 1,
1665 1665 :issue => {:tracker_id => 1,
1666 1666 :status_id => 5,
1667 1667 :subject => 'This is an issue'}
1668 1668
1669 1669 assert_equal 5, assigns(:issue).status_id
1670 1670 assert_equal [1,2,5], assigns(:allowed_statuses).map(&:id).sort
1671 1671 end
1672 1672
1673 1673 def test_post_create
1674 1674 @request.session[:user_id] = 2
1675 1675 assert_difference 'Issue.count' do
1676 1676 post :create, :project_id => 1,
1677 1677 :issue => {:tracker_id => 3,
1678 1678 :status_id => 2,
1679 1679 :subject => 'This is the test_new issue',
1680 1680 :description => 'This is the description',
1681 1681 :priority_id => 5,
1682 1682 :start_date => '2010-11-07',
1683 1683 :estimated_hours => '',
1684 1684 :custom_field_values => {'2' => 'Value for field 2'}}
1685 1685 end
1686 1686 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
1687 1687
1688 1688 issue = Issue.find_by_subject('This is the test_new issue')
1689 1689 assert_not_nil issue
1690 1690 assert_equal 2, issue.author_id
1691 1691 assert_equal 3, issue.tracker_id
1692 1692 assert_equal 2, issue.status_id
1693 1693 assert_equal Date.parse('2010-11-07'), issue.start_date
1694 1694 assert_nil issue.estimated_hours
1695 1695 v = issue.custom_values.find(:first, :conditions => {:custom_field_id => 2})
1696 1696 assert_not_nil v
1697 1697 assert_equal 'Value for field 2', v.value
1698 1698 end
1699 1699
1700 1700 def test_post_new_with_group_assignment
1701 1701 group = Group.find(11)
1702 1702 project = Project.find(1)
1703 1703 project.members << Member.new(:principal => group, :roles => [Role.givable.first])
1704 1704
1705 1705 with_settings :issue_group_assignment => '1' do
1706 1706 @request.session[:user_id] = 2
1707 1707 assert_difference 'Issue.count' do
1708 1708 post :create, :project_id => project.id,
1709 1709 :issue => {:tracker_id => 3,
1710 1710 :status_id => 1,
1711 1711 :subject => 'This is the test_new_with_group_assignment issue',
1712 1712 :assigned_to_id => group.id}
1713 1713 end
1714 1714 end
1715 1715 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
1716 1716
1717 1717 issue = Issue.find_by_subject('This is the test_new_with_group_assignment issue')
1718 1718 assert_not_nil issue
1719 1719 assert_equal group, issue.assigned_to
1720 1720 end
1721 1721
1722 1722 def test_post_create_without_start_date_and_default_start_date_is_not_creation_date
1723 1723 Setting.default_issue_start_date_to_creation_date = 0
1724 1724
1725 1725 @request.session[:user_id] = 2
1726 1726 assert_difference 'Issue.count' do
1727 1727 post :create, :project_id => 1,
1728 1728 :issue => {:tracker_id => 3,
1729 1729 :status_id => 2,
1730 1730 :subject => 'This is the test_new issue',
1731 1731 :description => 'This is the description',
1732 1732 :priority_id => 5,
1733 1733 :estimated_hours => '',
1734 1734 :custom_field_values => {'2' => 'Value for field 2'}}
1735 1735 end
1736 1736 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
1737 1737
1738 1738 issue = Issue.find_by_subject('This is the test_new issue')
1739 1739 assert_not_nil issue
1740 1740 assert_nil issue.start_date
1741 1741 end
1742 1742
1743 1743 def test_post_create_without_start_date_and_default_start_date_is_creation_date
1744 1744 Setting.default_issue_start_date_to_creation_date = 1
1745 1745
1746 1746 @request.session[:user_id] = 2
1747 1747 assert_difference 'Issue.count' do
1748 1748 post :create, :project_id => 1,
1749 1749 :issue => {:tracker_id => 3,
1750 1750 :status_id => 2,
1751 1751 :subject => 'This is the test_new issue',
1752 1752 :description => 'This is the description',
1753 1753 :priority_id => 5,
1754 1754 :estimated_hours => '',
1755 1755 :custom_field_values => {'2' => 'Value for field 2'}}
1756 1756 end
1757 1757 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
1758 1758
1759 1759 issue = Issue.find_by_subject('This is the test_new issue')
1760 1760 assert_not_nil issue
1761 1761 assert_equal Date.today, issue.start_date
1762 1762 end
1763 1763
1764 1764 def test_post_create_and_continue
1765 1765 @request.session[:user_id] = 2
1766 1766 assert_difference 'Issue.count' do
1767 1767 post :create, :project_id => 1,
1768 1768 :issue => {:tracker_id => 3, :subject => 'This is first issue', :priority_id => 5},
1769 1769 :continue => ''
1770 1770 end
1771 1771
1772 1772 issue = Issue.first(:order => 'id DESC')
1773 1773 assert_redirected_to :controller => 'issues', :action => 'new', :project_id => 'ecookbook', :issue => {:tracker_id => 3}
1774 1774 assert_not_nil flash[:notice], "flash was not set"
1775 1775 assert_include %|<a href="/issues/#{issue.id}" title="This is first issue">##{issue.id}</a>|, flash[:notice], "issue link not found in the flash message"
1776 1776 end
1777 1777
1778 1778 def test_post_create_without_custom_fields_param
1779 1779 @request.session[:user_id] = 2
1780 1780 assert_difference 'Issue.count' do
1781 1781 post :create, :project_id => 1,
1782 1782 :issue => {:tracker_id => 1,
1783 1783 :subject => 'This is the test_new issue',
1784 1784 :description => 'This is the description',
1785 1785 :priority_id => 5}
1786 1786 end
1787 1787 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
1788 1788 end
1789 1789
1790 1790 def test_post_create_with_multi_custom_field
1791 1791 field = IssueCustomField.find_by_name('Database')
1792 1792 field.update_attribute(:multiple, true)
1793 1793
1794 1794 @request.session[:user_id] = 2
1795 1795 assert_difference 'Issue.count' do
1796 1796 post :create, :project_id => 1,
1797 1797 :issue => {:tracker_id => 1,
1798 1798 :subject => 'This is the test_new issue',
1799 1799 :description => 'This is the description',
1800 1800 :priority_id => 5,
1801 1801 :custom_field_values => {'1' => ['', 'MySQL', 'Oracle']}}
1802 1802 end
1803 1803 assert_response 302
1804 1804 issue = Issue.first(:order => 'id DESC')
1805 1805 assert_equal ['MySQL', 'Oracle'], issue.custom_field_value(1).sort
1806 1806 end
1807 1807
1808 1808 def test_post_create_with_empty_multi_custom_field
1809 1809 field = IssueCustomField.find_by_name('Database')
1810 1810 field.update_attribute(:multiple, true)
1811 1811
1812 1812 @request.session[:user_id] = 2
1813 1813 assert_difference 'Issue.count' do
1814 1814 post :create, :project_id => 1,
1815 1815 :issue => {:tracker_id => 1,
1816 1816 :subject => 'This is the test_new issue',
1817 1817 :description => 'This is the description',
1818 1818 :priority_id => 5,
1819 1819 :custom_field_values => {'1' => ['']}}
1820 1820 end
1821 1821 assert_response 302
1822 1822 issue = Issue.first(:order => 'id DESC')
1823 1823 assert_equal [''], issue.custom_field_value(1).sort
1824 1824 end
1825 1825
1826 1826 def test_post_create_with_multi_user_custom_field
1827 1827 field = IssueCustomField.create!(:name => 'Multi user', :field_format => 'user', :multiple => true,
1828 1828 :tracker_ids => [1], :is_for_all => true)
1829 1829
1830 1830 @request.session[:user_id] = 2
1831 1831 assert_difference 'Issue.count' do
1832 1832 post :create, :project_id => 1,
1833 1833 :issue => {:tracker_id => 1,
1834 1834 :subject => 'This is the test_new issue',
1835 1835 :description => 'This is the description',
1836 1836 :priority_id => 5,
1837 1837 :custom_field_values => {field.id.to_s => ['', '2', '3']}}
1838 1838 end
1839 1839 assert_response 302
1840 1840 issue = Issue.first(:order => 'id DESC')
1841 1841 assert_equal ['2', '3'], issue.custom_field_value(field).sort
1842 1842 end
1843 1843
1844 1844 def test_post_create_with_required_custom_field_and_without_custom_fields_param
1845 1845 field = IssueCustomField.find_by_name('Database')
1846 1846 field.update_attribute(:is_required, true)
1847 1847
1848 1848 @request.session[:user_id] = 2
1849 1849 assert_no_difference 'Issue.count' do
1850 1850 post :create, :project_id => 1,
1851 1851 :issue => {:tracker_id => 1,
1852 1852 :subject => 'This is the test_new issue',
1853 1853 :description => 'This is the description',
1854 1854 :priority_id => 5}
1855 1855 end
1856 1856 assert_response :success
1857 1857 assert_template 'new'
1858 1858 issue = assigns(:issue)
1859 1859 assert_not_nil issue
1860 1860 assert_error_tag :content => /Database can&#x27;t be blank/
1861 1861 end
1862 1862
1863 1863 def test_create_should_validate_required_fields
1864 1864 cf1 = IssueCustomField.create!(:name => 'Foo', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
1865 1865 cf2 = IssueCustomField.create!(:name => 'Bar', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
1866 1866 WorkflowPermission.delete_all
1867 1867 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2, :role_id => 1, :field_name => 'due_date', :rule => 'required')
1868 1868 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2, :role_id => 1, :field_name => cf2.id.to_s, :rule => 'required')
1869 1869 @request.session[:user_id] = 2
1870 1870
1871 1871 assert_no_difference 'Issue.count' do
1872 1872 post :create, :project_id => 1, :issue => {
1873 1873 :tracker_id => 2,
1874 1874 :status_id => 1,
1875 1875 :subject => 'Test',
1876 1876 :start_date => '',
1877 1877 :due_date => '',
1878 1878 :custom_field_values => {cf1.id.to_s => '', cf2.id.to_s => ''}
1879 1879 }
1880 1880 assert_response :success
1881 1881 assert_template 'new'
1882 1882 end
1883 1883
1884 1884 assert_error_tag :content => /Due date can&#x27;t be blank/i
1885 1885 assert_error_tag :content => /Bar can&#x27;t be blank/i
1886 1886 end
1887 1887
1888 1888 def test_create_should_ignore_readonly_fields
1889 1889 cf1 = IssueCustomField.create!(:name => 'Foo', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
1890 1890 cf2 = IssueCustomField.create!(:name => 'Bar', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
1891 1891 WorkflowPermission.delete_all
1892 1892 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2, :role_id => 1, :field_name => 'due_date', :rule => 'readonly')
1893 1893 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2, :role_id => 1, :field_name => cf2.id.to_s, :rule => 'readonly')
1894 1894 @request.session[:user_id] = 2
1895 1895
1896 1896 assert_difference 'Issue.count' do
1897 1897 post :create, :project_id => 1, :issue => {
1898 1898 :tracker_id => 2,
1899 1899 :status_id => 1,
1900 1900 :subject => 'Test',
1901 1901 :start_date => '2012-07-14',
1902 1902 :due_date => '2012-07-16',
1903 1903 :custom_field_values => {cf1.id.to_s => 'value1', cf2.id.to_s => 'value2'}
1904 1904 }
1905 1905 assert_response 302
1906 1906 end
1907 1907
1908 1908 issue = Issue.first(:order => 'id DESC')
1909 1909 assert_equal Date.parse('2012-07-14'), issue.start_date
1910 1910 assert_nil issue.due_date
1911 1911 assert_equal 'value1', issue.custom_field_value(cf1)
1912 1912 assert_nil issue.custom_field_value(cf2)
1913 1913 end
1914 1914
1915 1915 def test_post_create_with_watchers
1916 1916 @request.session[:user_id] = 2
1917 1917 ActionMailer::Base.deliveries.clear
1918 1918
1919 1919 assert_difference 'Watcher.count', 2 do
1920 1920 post :create, :project_id => 1,
1921 1921 :issue => {:tracker_id => 1,
1922 1922 :subject => 'This is a new issue with watchers',
1923 1923 :description => 'This is the description',
1924 1924 :priority_id => 5,
1925 1925 :watcher_user_ids => ['2', '3']}
1926 1926 end
1927 1927 issue = Issue.find_by_subject('This is a new issue with watchers')
1928 1928 assert_not_nil issue
1929 1929 assert_redirected_to :controller => 'issues', :action => 'show', :id => issue
1930 1930
1931 1931 # Watchers added
1932 1932 assert_equal [2, 3], issue.watcher_user_ids.sort
1933 1933 assert issue.watched_by?(User.find(3))
1934 1934 # Watchers notified
1935 1935 mail = ActionMailer::Base.deliveries.last
1936 1936 assert_not_nil mail
1937 1937 assert [mail.bcc, mail.cc].flatten.include?(User.find(3).mail)
1938 1938 end
1939 1939
1940 1940 def test_post_create_subissue
1941 1941 @request.session[:user_id] = 2
1942 1942
1943 1943 assert_difference 'Issue.count' do
1944 1944 post :create, :project_id => 1,
1945 1945 :issue => {:tracker_id => 1,
1946 1946 :subject => 'This is a child issue',
1947 1947 :parent_issue_id => 2}
1948
1949 assert_response 302
1948 1950 end
1949 1951 issue = Issue.find_by_subject('This is a child issue')
1950 1952 assert_not_nil issue
1951 1953 assert_equal Issue.find(2), issue.parent
1952 1954 end
1953 1955
1954 def test_post_create_subissue_with_non_numeric_parent_id
1956 def test_post_create_subissue_with_non_visible_parent_id_should_not_validate
1955 1957 @request.session[:user_id] = 2
1956 1958
1957 assert_difference 'Issue.count' do
1959 assert_no_difference 'Issue.count' do
1958 1960 post :create, :project_id => 1,
1959 1961 :issue => {:tracker_id => 1,
1960 1962 :subject => 'This is a child issue',
1961 :parent_issue_id => 'ABC'}
1963 :parent_issue_id => '4'}
1964
1965 assert_response :success
1966 assert_select 'input[name=?][value=?]', 'issue[parent_issue_id]', '4'
1967 assert_error_tag :content => /Parent task is invalid/i
1968 end
1969 end
1970
1971 def test_post_create_subissue_with_non_numeric_parent_id_should_not_validate
1972 @request.session[:user_id] = 2
1973
1974 assert_no_difference 'Issue.count' do
1975 post :create, :project_id => 1,
1976 :issue => {:tracker_id => 1,
1977 :subject => 'This is a child issue',
1978 :parent_issue_id => '01ABC'}
1979
1980 assert_response :success
1981 assert_select 'input[name=?][value=?]', 'issue[parent_issue_id]', '01ABC'
1982 assert_error_tag :content => /Parent task is invalid/i
1962 1983 end
1963 issue = Issue.find_by_subject('This is a child issue')
1964 assert_not_nil issue
1965 assert_nil issue.parent
1966 1984 end
1967 1985
1968 1986 def test_post_create_private
1969 1987 @request.session[:user_id] = 2
1970 1988
1971 1989 assert_difference 'Issue.count' do
1972 1990 post :create, :project_id => 1,
1973 1991 :issue => {:tracker_id => 1,
1974 1992 :subject => 'This is a private issue',
1975 1993 :is_private => '1'}
1976 1994 end
1977 1995 issue = Issue.first(:order => 'id DESC')
1978 1996 assert issue.is_private?
1979 1997 end
1980 1998
1981 1999 def test_post_create_private_with_set_own_issues_private_permission
1982 2000 role = Role.find(1)
1983 2001 role.remove_permission! :set_issues_private
1984 2002 role.add_permission! :set_own_issues_private
1985 2003
1986 2004 @request.session[:user_id] = 2
1987 2005
1988 2006 assert_difference 'Issue.count' do
1989 2007 post :create, :project_id => 1,
1990 2008 :issue => {:tracker_id => 1,
1991 2009 :subject => 'This is a private issue',
1992 2010 :is_private => '1'}
1993 2011 end
1994 2012 issue = Issue.first(:order => 'id DESC')
1995 2013 assert issue.is_private?
1996 2014 end
1997 2015
1998 2016 def test_post_create_should_send_a_notification
1999 2017 ActionMailer::Base.deliveries.clear
2000 2018 @request.session[:user_id] = 2
2001 2019 assert_difference 'Issue.count' do
2002 2020 post :create, :project_id => 1,
2003 2021 :issue => {:tracker_id => 3,
2004 2022 :subject => 'This is the test_new issue',
2005 2023 :description => 'This is the description',
2006 2024 :priority_id => 5,
2007 2025 :estimated_hours => '',
2008 2026 :custom_field_values => {'2' => 'Value for field 2'}}
2009 2027 end
2010 2028 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
2011 2029
2012 2030 assert_equal 1, ActionMailer::Base.deliveries.size
2013 2031 end
2014 2032
2015 2033 def test_post_create_should_preserve_fields_values_on_validation_failure
2016 2034 @request.session[:user_id] = 2
2017 2035 post :create, :project_id => 1,
2018 2036 :issue => {:tracker_id => 1,
2019 2037 # empty subject
2020 2038 :subject => '',
2021 2039 :description => 'This is a description',
2022 2040 :priority_id => 6,
2023 2041 :custom_field_values => {'1' => 'Oracle', '2' => 'Value for field 2'}}
2024 2042 assert_response :success
2025 2043 assert_template 'new'
2026 2044
2027 2045 assert_tag :textarea, :attributes => { :name => 'issue[description]' },
2028 2046 :content => "\nThis is a description"
2029 2047 assert_tag :select, :attributes => { :name => 'issue[priority_id]' },
2030 2048 :child => { :tag => 'option', :attributes => { :selected => 'selected',
2031 2049 :value => '6' },
2032 2050 :content => 'High' }
2033 2051 # Custom fields
2034 2052 assert_tag :select, :attributes => { :name => 'issue[custom_field_values][1]' },
2035 2053 :child => { :tag => 'option', :attributes => { :selected => 'selected',
2036 2054 :value => 'Oracle' },
2037 2055 :content => 'Oracle' }
2038 2056 assert_tag :input, :attributes => { :name => 'issue[custom_field_values][2]',
2039 2057 :value => 'Value for field 2'}
2040 2058 end
2041 2059
2042 2060 def test_post_create_with_failure_should_preserve_watchers
2043 2061 assert !User.find(8).member_of?(Project.find(1))
2044 2062
2045 2063 @request.session[:user_id] = 2
2046 2064 post :create, :project_id => 1,
2047 2065 :issue => {:tracker_id => 1,
2048 2066 :watcher_user_ids => ['3', '8']}
2049 2067 assert_response :success
2050 2068 assert_template 'new'
2051 2069
2052 2070 assert_tag 'input', :attributes => {:name => 'issue[watcher_user_ids][]', :value => '2', :checked => nil}
2053 2071 assert_tag 'input', :attributes => {:name => 'issue[watcher_user_ids][]', :value => '3', :checked => 'checked'}
2054 2072 assert_tag 'input', :attributes => {:name => 'issue[watcher_user_ids][]', :value => '8', :checked => 'checked'}
2055 2073 end
2056 2074
2057 2075 def test_post_create_should_ignore_non_safe_attributes
2058 2076 @request.session[:user_id] = 2
2059 2077 assert_nothing_raised do
2060 2078 post :create, :project_id => 1, :issue => { :tracker => "A param can not be a Tracker" }
2061 2079 end
2062 2080 end
2063 2081
2064 2082 def test_post_create_with_attachment
2065 2083 set_tmp_attachments_directory
2066 2084 @request.session[:user_id] = 2
2067 2085
2068 2086 assert_difference 'Issue.count' do
2069 2087 assert_difference 'Attachment.count' do
2070 2088 post :create, :project_id => 1,
2071 2089 :issue => { :tracker_id => '1', :subject => 'With attachment' },
2072 2090 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
2073 2091 end
2074 2092 end
2075 2093
2076 2094 issue = Issue.first(:order => 'id DESC')
2077 2095 attachment = Attachment.first(:order => 'id DESC')
2078 2096
2079 2097 assert_equal issue, attachment.container
2080 2098 assert_equal 2, attachment.author_id
2081 2099 assert_equal 'testfile.txt', attachment.filename
2082 2100 assert_equal 'text/plain', attachment.content_type
2083 2101 assert_equal 'test file', attachment.description
2084 2102 assert_equal 59, attachment.filesize
2085 2103 assert File.exists?(attachment.diskfile)
2086 2104 assert_equal 59, File.size(attachment.diskfile)
2087 2105 end
2088 2106
2089 2107 def test_post_create_with_failure_should_save_attachments
2090 2108 set_tmp_attachments_directory
2091 2109 @request.session[:user_id] = 2
2092 2110
2093 2111 assert_no_difference 'Issue.count' do
2094 2112 assert_difference 'Attachment.count' do
2095 2113 post :create, :project_id => 1,
2096 2114 :issue => { :tracker_id => '1', :subject => '' },
2097 2115 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
2098 2116 assert_response :success
2099 2117 assert_template 'new'
2100 2118 end
2101 2119 end
2102 2120
2103 2121 attachment = Attachment.first(:order => 'id DESC')
2104 2122 assert_equal 'testfile.txt', attachment.filename
2105 2123 assert File.exists?(attachment.diskfile)
2106 2124 assert_nil attachment.container
2107 2125
2108 2126 assert_tag 'input', :attributes => {:name => 'attachments[p0][token]', :value => attachment.token}
2109 2127 assert_tag 'span', :content => /testfile.txt/
2110 2128 end
2111 2129
2112 2130 def test_post_create_with_failure_should_keep_saved_attachments
2113 2131 set_tmp_attachments_directory
2114 2132 attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 2)
2115 2133 @request.session[:user_id] = 2
2116 2134
2117 2135 assert_no_difference 'Issue.count' do
2118 2136 assert_no_difference 'Attachment.count' do
2119 2137 post :create, :project_id => 1,
2120 2138 :issue => { :tracker_id => '1', :subject => '' },
2121 2139 :attachments => {'p0' => {'token' => attachment.token}}
2122 2140 assert_response :success
2123 2141 assert_template 'new'
2124 2142 end
2125 2143 end
2126 2144
2127 2145 assert_tag 'input', :attributes => {:name => 'attachments[p0][token]', :value => attachment.token}
2128 2146 assert_tag 'span', :content => /testfile.txt/
2129 2147 end
2130 2148
2131 2149 def test_post_create_should_attach_saved_attachments
2132 2150 set_tmp_attachments_directory
2133 2151 attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 2)
2134 2152 @request.session[:user_id] = 2
2135 2153
2136 2154 assert_difference 'Issue.count' do
2137 2155 assert_no_difference 'Attachment.count' do
2138 2156 post :create, :project_id => 1,
2139 2157 :issue => { :tracker_id => '1', :subject => 'Saved attachments' },
2140 2158 :attachments => {'p0' => {'token' => attachment.token}}
2141 2159 assert_response 302
2142 2160 end
2143 2161 end
2144 2162
2145 2163 issue = Issue.first(:order => 'id DESC')
2146 2164 assert_equal 1, issue.attachments.count
2147 2165
2148 2166 attachment.reload
2149 2167 assert_equal issue, attachment.container
2150 2168 end
2151 2169
2152 2170 context "without workflow privilege" do
2153 2171 setup do
2154 2172 WorkflowTransition.delete_all(["role_id = ?", Role.anonymous.id])
2155 2173 Role.anonymous.add_permission! :add_issues, :add_issue_notes
2156 2174 end
2157 2175
2158 2176 context "#new" do
2159 2177 should "propose default status only" do
2160 2178 get :new, :project_id => 1
2161 2179 assert_response :success
2162 2180 assert_template 'new'
2163 2181 assert_tag :tag => 'select',
2164 2182 :attributes => {:name => 'issue[status_id]'},
2165 2183 :children => {:count => 1},
2166 2184 :child => {:tag => 'option', :attributes => {:value => IssueStatus.default.id.to_s}}
2167 2185 end
2168 2186
2169 2187 should "accept default status" do
2170 2188 assert_difference 'Issue.count' do
2171 2189 post :create, :project_id => 1,
2172 2190 :issue => {:tracker_id => 1,
2173 2191 :subject => 'This is an issue',
2174 2192 :status_id => 1}
2175 2193 end
2176 2194 issue = Issue.last(:order => 'id')
2177 2195 assert_equal IssueStatus.default, issue.status
2178 2196 end
2179 2197
2180 2198 should "ignore unauthorized status" do
2181 2199 assert_difference 'Issue.count' do
2182 2200 post :create, :project_id => 1,
2183 2201 :issue => {:tracker_id => 1,
2184 2202 :subject => 'This is an issue',
2185 2203 :status_id => 3}
2186 2204 end
2187 2205 issue = Issue.last(:order => 'id')
2188 2206 assert_equal IssueStatus.default, issue.status
2189 2207 end
2190 2208 end
2191 2209
2192 2210 context "#update" do
2193 2211 should "ignore status change" do
2194 2212 assert_difference 'Journal.count' do
2195 2213 put :update, :id => 1, :issue => {:status_id => 3, :notes => 'just trying'}
2196 2214 end
2197 2215 assert_equal 1, Issue.find(1).status_id
2198 2216 end
2199 2217
2200 2218 should "ignore attributes changes" do
2201 2219 assert_difference 'Journal.count' do
2202 2220 put :update, :id => 1, :issue => {:subject => 'changed', :assigned_to_id => 2, :notes => 'just trying'}
2203 2221 end
2204 2222 issue = Issue.find(1)
2205 2223 assert_equal "Can't print recipes", issue.subject
2206 2224 assert_nil issue.assigned_to
2207 2225 end
2208 2226 end
2209 2227 end
2210 2228
2211 2229 context "with workflow privilege" do
2212 2230 setup do
2213 2231 WorkflowTransition.delete_all(["role_id = ?", Role.anonymous.id])
2214 2232 WorkflowTransition.create!(:role => Role.anonymous, :tracker_id => 1, :old_status_id => 1, :new_status_id => 3)
2215 2233 WorkflowTransition.create!(:role => Role.anonymous, :tracker_id => 1, :old_status_id => 1, :new_status_id => 4)
2216 2234 Role.anonymous.add_permission! :add_issues, :add_issue_notes
2217 2235 end
2218 2236
2219 2237 context "#update" do
2220 2238 should "accept authorized status" do
2221 2239 assert_difference 'Journal.count' do
2222 2240 put :update, :id => 1, :issue => {:status_id => 3, :notes => 'just trying'}
2223 2241 end
2224 2242 assert_equal 3, Issue.find(1).status_id
2225 2243 end
2226 2244
2227 2245 should "ignore unauthorized status" do
2228 2246 assert_difference 'Journal.count' do
2229 2247 put :update, :id => 1, :issue => {:status_id => 2, :notes => 'just trying'}
2230 2248 end
2231 2249 assert_equal 1, Issue.find(1).status_id
2232 2250 end
2233 2251
2234 2252 should "accept authorized attributes changes" do
2235 2253 assert_difference 'Journal.count' do
2236 2254 put :update, :id => 1, :issue => {:assigned_to_id => 2, :notes => 'just trying'}
2237 2255 end
2238 2256 issue = Issue.find(1)
2239 2257 assert_equal 2, issue.assigned_to_id
2240 2258 end
2241 2259
2242 2260 should "ignore unauthorized attributes changes" do
2243 2261 assert_difference 'Journal.count' do
2244 2262 put :update, :id => 1, :issue => {:subject => 'changed', :notes => 'just trying'}
2245 2263 end
2246 2264 issue = Issue.find(1)
2247 2265 assert_equal "Can't print recipes", issue.subject
2248 2266 end
2249 2267 end
2250 2268
2251 2269 context "and :edit_issues permission" do
2252 2270 setup do
2253 2271 Role.anonymous.add_permission! :add_issues, :edit_issues
2254 2272 end
2255 2273
2256 2274 should "accept authorized status" do
2257 2275 assert_difference 'Journal.count' do
2258 2276 put :update, :id => 1, :issue => {:status_id => 3, :notes => 'just trying'}
2259 2277 end
2260 2278 assert_equal 3, Issue.find(1).status_id
2261 2279 end
2262 2280
2263 2281 should "ignore unauthorized status" do
2264 2282 assert_difference 'Journal.count' do
2265 2283 put :update, :id => 1, :issue => {:status_id => 2, :notes => 'just trying'}
2266 2284 end
2267 2285 assert_equal 1, Issue.find(1).status_id
2268 2286 end
2269 2287
2270 2288 should "accept authorized attributes changes" do
2271 2289 assert_difference 'Journal.count' do
2272 2290 put :update, :id => 1, :issue => {:subject => 'changed', :assigned_to_id => 2, :notes => 'just trying'}
2273 2291 end
2274 2292 issue = Issue.find(1)
2275 2293 assert_equal "changed", issue.subject
2276 2294 assert_equal 2, issue.assigned_to_id
2277 2295 end
2278 2296 end
2279 2297 end
2280 2298
2281 2299 def test_new_as_copy
2282 2300 @request.session[:user_id] = 2
2283 2301 get :new, :project_id => 1, :copy_from => 1
2284 2302
2285 2303 assert_response :success
2286 2304 assert_template 'new'
2287 2305
2288 2306 assert_not_nil assigns(:issue)
2289 2307 orig = Issue.find(1)
2290 2308 assert_equal 1, assigns(:issue).project_id
2291 2309 assert_equal orig.subject, assigns(:issue).subject
2292 2310 assert assigns(:issue).copy?
2293 2311
2294 2312 assert_tag 'form', :attributes => {:id => 'issue-form', :action => '/projects/ecookbook/issues'}
2295 2313 assert_tag 'select', :attributes => {:name => 'issue[project_id]'}
2296 2314 assert_tag 'select', :attributes => {:name => 'issue[project_id]'},
2297 2315 :child => {:tag => 'option', :attributes => {:value => '1', :selected => 'selected'}, :content => 'eCookbook'}
2298 2316 assert_tag 'select', :attributes => {:name => 'issue[project_id]'},
2299 2317 :child => {:tag => 'option', :attributes => {:value => '2', :selected => nil}, :content => 'OnlineStore'}
2300 2318 assert_tag 'input', :attributes => {:name => 'copy_from', :value => '1'}
2301 2319 end
2302 2320
2303 2321 def test_new_as_copy_with_attachments_should_show_copy_attachments_checkbox
2304 2322 @request.session[:user_id] = 2
2305 2323 issue = Issue.find(3)
2306 2324 assert issue.attachments.count > 0
2307 2325 get :new, :project_id => 1, :copy_from => 3
2308 2326
2309 2327 assert_tag 'input', :attributes => {:name => 'copy_attachments', :type => 'checkbox', :checked => 'checked', :value => '1'}
2310 2328 end
2311 2329
2312 2330 def test_new_as_copy_without_attachments_should_not_show_copy_attachments_checkbox
2313 2331 @request.session[:user_id] = 2
2314 2332 issue = Issue.find(3)
2315 2333 issue.attachments.delete_all
2316 2334 get :new, :project_id => 1, :copy_from => 3
2317 2335
2318 2336 assert_no_tag 'input', :attributes => {:name => 'copy_attachments', :type => 'checkbox', :checked => 'checked', :value => '1'}
2319 2337 end
2320 2338
2321 2339 def test_new_as_copy_with_subtasks_should_show_copy_subtasks_checkbox
2322 2340 @request.session[:user_id] = 2
2323 2341 issue = Issue.generate_with_descendants!
2324 2342 get :new, :project_id => 1, :copy_from => issue.id
2325 2343
2326 2344 assert_select 'input[type=checkbox][name=copy_subtasks][checked=checked][value=1]'
2327 2345 end
2328 2346
2329 2347 def test_new_as_copy_with_invalid_issue_should_respond_with_404
2330 2348 @request.session[:user_id] = 2
2331 2349 get :new, :project_id => 1, :copy_from => 99999
2332 2350 assert_response 404
2333 2351 end
2334 2352
2335 2353 def test_create_as_copy_on_different_project
2336 2354 @request.session[:user_id] = 2
2337 2355 assert_difference 'Issue.count' do
2338 2356 post :create, :project_id => 1, :copy_from => 1,
2339 2357 :issue => {:project_id => '2', :tracker_id => '3', :status_id => '1', :subject => 'Copy'}
2340 2358
2341 2359 assert_not_nil assigns(:issue)
2342 2360 assert assigns(:issue).copy?
2343 2361 end
2344 2362 issue = Issue.first(:order => 'id DESC')
2345 2363 assert_redirected_to "/issues/#{issue.id}"
2346 2364
2347 2365 assert_equal 2, issue.project_id
2348 2366 assert_equal 3, issue.tracker_id
2349 2367 assert_equal 'Copy', issue.subject
2350 2368 end
2351 2369
2352 2370 def test_create_as_copy_should_copy_attachments
2353 2371 @request.session[:user_id] = 2
2354 2372 issue = Issue.find(3)
2355 2373 count = issue.attachments.count
2356 2374 assert count > 0
2357 2375
2358 2376 assert_difference 'Issue.count' do
2359 2377 assert_difference 'Attachment.count', count do
2360 2378 assert_no_difference 'Journal.count' do
2361 2379 post :create, :project_id => 1, :copy_from => 3,
2362 2380 :issue => {:project_id => '1', :tracker_id => '3', :status_id => '1', :subject => 'Copy with attachments'},
2363 2381 :copy_attachments => '1'
2364 2382 end
2365 2383 end
2366 2384 end
2367 2385 copy = Issue.first(:order => 'id DESC')
2368 2386 assert_equal count, copy.attachments.count
2369 2387 assert_equal issue.attachments.map(&:filename).sort, copy.attachments.map(&:filename).sort
2370 2388 end
2371 2389
2372 2390 def test_create_as_copy_without_copy_attachments_option_should_not_copy_attachments
2373 2391 @request.session[:user_id] = 2
2374 2392 issue = Issue.find(3)
2375 2393 count = issue.attachments.count
2376 2394 assert count > 0
2377 2395
2378 2396 assert_difference 'Issue.count' do
2379 2397 assert_no_difference 'Attachment.count' do
2380 2398 assert_no_difference 'Journal.count' do
2381 2399 post :create, :project_id => 1, :copy_from => 3,
2382 2400 :issue => {:project_id => '1', :tracker_id => '3', :status_id => '1', :subject => 'Copy with attachments'}
2383 2401 end
2384 2402 end
2385 2403 end
2386 2404 copy = Issue.first(:order => 'id DESC')
2387 2405 assert_equal 0, copy.attachments.count
2388 2406 end
2389 2407
2390 2408 def test_create_as_copy_with_attachments_should_add_new_files
2391 2409 @request.session[:user_id] = 2
2392 2410 issue = Issue.find(3)
2393 2411 count = issue.attachments.count
2394 2412 assert count > 0
2395 2413
2396 2414 assert_difference 'Issue.count' do
2397 2415 assert_difference 'Attachment.count', count + 1 do
2398 2416 assert_no_difference 'Journal.count' do
2399 2417 post :create, :project_id => 1, :copy_from => 3,
2400 2418 :issue => {:project_id => '1', :tracker_id => '3', :status_id => '1', :subject => 'Copy with attachments'},
2401 2419 :copy_attachments => '1',
2402 2420 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
2403 2421 end
2404 2422 end
2405 2423 end
2406 2424 copy = Issue.first(:order => 'id DESC')
2407 2425 assert_equal count + 1, copy.attachments.count
2408 2426 end
2409 2427
2410 2428 def test_create_as_copy_should_add_relation_with_copied_issue
2411 2429 @request.session[:user_id] = 2
2412 2430
2413 2431 assert_difference 'Issue.count' do
2414 2432 assert_difference 'IssueRelation.count' do
2415 2433 post :create, :project_id => 1, :copy_from => 1,
2416 2434 :issue => {:project_id => '1', :tracker_id => '3', :status_id => '1', :subject => 'Copy'}
2417 2435 end
2418 2436 end
2419 2437 copy = Issue.first(:order => 'id DESC')
2420 2438 assert_equal 1, copy.relations.size
2421 2439 end
2422 2440
2423 2441 def test_create_as_copy_should_copy_subtasks
2424 2442 @request.session[:user_id] = 2
2425 2443 issue = Issue.generate_with_descendants!
2426 2444 count = issue.descendants.count
2427 2445
2428 2446 assert_difference 'Issue.count', count+1 do
2429 2447 assert_no_difference 'Journal.count' do
2430 2448 post :create, :project_id => 1, :copy_from => issue.id,
2431 2449 :issue => {:project_id => '1', :tracker_id => '3', :status_id => '1', :subject => 'Copy with subtasks'},
2432 2450 :copy_subtasks => '1'
2433 2451 end
2434 2452 end
2435 2453 copy = Issue.where(:parent_id => nil).first(:order => 'id DESC')
2436 2454 assert_equal count, copy.descendants.count
2437 2455 assert_equal issue.descendants.map(&:subject).sort, copy.descendants.map(&:subject).sort
2438 2456 end
2439 2457
2440 2458 def test_create_as_copy_without_copy_subtasks_option_should_not_copy_subtasks
2441 2459 @request.session[:user_id] = 2
2442 2460 issue = Issue.generate_with_descendants!
2443 2461
2444 2462 assert_difference 'Issue.count', 1 do
2445 2463 assert_no_difference 'Journal.count' do
2446 2464 post :create, :project_id => 1, :copy_from => 3,
2447 2465 :issue => {:project_id => '1', :tracker_id => '3', :status_id => '1', :subject => 'Copy with subtasks'}
2448 2466 end
2449 2467 end
2450 2468 copy = Issue.where(:parent_id => nil).first(:order => 'id DESC')
2451 2469 assert_equal 0, copy.descendants.count
2452 2470 end
2453 2471
2454 2472 def test_create_as_copy_with_failure
2455 2473 @request.session[:user_id] = 2
2456 2474 post :create, :project_id => 1, :copy_from => 1,
2457 2475 :issue => {:project_id => '2', :tracker_id => '3', :status_id => '1', :subject => ''}
2458 2476
2459 2477 assert_response :success
2460 2478 assert_template 'new'
2461 2479
2462 2480 assert_not_nil assigns(:issue)
2463 2481 assert assigns(:issue).copy?
2464 2482
2465 2483 assert_tag 'form', :attributes => {:id => 'issue-form', :action => '/projects/ecookbook/issues'}
2466 2484 assert_tag 'select', :attributes => {:name => 'issue[project_id]'}
2467 2485 assert_tag 'select', :attributes => {:name => 'issue[project_id]'},
2468 2486 :child => {:tag => 'option', :attributes => {:value => '1', :selected => nil}, :content => 'eCookbook'}
2469 2487 assert_tag 'select', :attributes => {:name => 'issue[project_id]'},
2470 2488 :child => {:tag => 'option', :attributes => {:value => '2', :selected => 'selected'}, :content => 'OnlineStore'}
2471 2489 assert_tag 'input', :attributes => {:name => 'copy_from', :value => '1'}
2472 2490 end
2473 2491
2474 2492 def test_create_as_copy_on_project_without_permission_should_ignore_target_project
2475 2493 @request.session[:user_id] = 2
2476 2494 assert !User.find(2).member_of?(Project.find(4))
2477 2495
2478 2496 assert_difference 'Issue.count' do
2479 2497 post :create, :project_id => 1, :copy_from => 1,
2480 2498 :issue => {:project_id => '4', :tracker_id => '3', :status_id => '1', :subject => 'Copy'}
2481 2499 end
2482 2500 issue = Issue.first(:order => 'id DESC')
2483 2501 assert_equal 1, issue.project_id
2484 2502 end
2485 2503
2486 2504 def test_get_edit
2487 2505 @request.session[:user_id] = 2
2488 2506 get :edit, :id => 1
2489 2507 assert_response :success
2490 2508 assert_template 'edit'
2491 2509 assert_not_nil assigns(:issue)
2492 2510 assert_equal Issue.find(1), assigns(:issue)
2493 2511
2494 2512 # Be sure we don't display inactive IssuePriorities
2495 2513 assert ! IssuePriority.find(15).active?
2496 2514 assert_no_tag :option, :attributes => {:value => '15'},
2497 2515 :parent => {:tag => 'select', :attributes => {:id => 'issue_priority_id'} }
2498 2516 end
2499 2517
2500 2518 def test_get_edit_should_display_the_time_entry_form_with_log_time_permission
2501 2519 @request.session[:user_id] = 2
2502 2520 Role.find_by_name('Manager').update_attribute :permissions, [:view_issues, :edit_issues, :log_time]
2503 2521
2504 2522 get :edit, :id => 1
2505 2523 assert_tag 'input', :attributes => {:name => 'time_entry[hours]'}
2506 2524 end
2507 2525
2508 2526 def test_get_edit_should_not_display_the_time_entry_form_without_log_time_permission
2509 2527 @request.session[:user_id] = 2
2510 2528 Role.find_by_name('Manager').remove_permission! :log_time
2511 2529
2512 2530 get :edit, :id => 1
2513 2531 assert_no_tag 'input', :attributes => {:name => 'time_entry[hours]'}
2514 2532 end
2515 2533
2516 2534 def test_get_edit_with_params
2517 2535 @request.session[:user_id] = 2
2518 2536 get :edit, :id => 1, :issue => { :status_id => 5, :priority_id => 7 },
2519 2537 :time_entry => { :hours => '2.5', :comments => 'test_get_edit_with_params', :activity_id => TimeEntryActivity.first.id }
2520 2538 assert_response :success
2521 2539 assert_template 'edit'
2522 2540
2523 2541 issue = assigns(:issue)
2524 2542 assert_not_nil issue
2525 2543
2526 2544 assert_equal 5, issue.status_id
2527 2545 assert_tag :select, :attributes => { :name => 'issue[status_id]' },
2528 2546 :child => { :tag => 'option',
2529 2547 :content => 'Closed',
2530 2548 :attributes => { :selected => 'selected' } }
2531 2549
2532 2550 assert_equal 7, issue.priority_id
2533 2551 assert_tag :select, :attributes => { :name => 'issue[priority_id]' },
2534 2552 :child => { :tag => 'option',
2535 2553 :content => 'Urgent',
2536 2554 :attributes => { :selected => 'selected' } }
2537 2555
2538 2556 assert_tag :input, :attributes => { :name => 'time_entry[hours]', :value => '2.5' }
2539 2557 assert_tag :select, :attributes => { :name => 'time_entry[activity_id]' },
2540 2558 :child => { :tag => 'option',
2541 2559 :attributes => { :selected => 'selected', :value => TimeEntryActivity.first.id } }
2542 2560 assert_tag :input, :attributes => { :name => 'time_entry[comments]', :value => 'test_get_edit_with_params' }
2543 2561 end
2544 2562
2545 2563 def test_get_edit_with_multi_custom_field
2546 2564 field = CustomField.find(1)
2547 2565 field.update_attribute :multiple, true
2548 2566 issue = Issue.find(1)
2549 2567 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
2550 2568 issue.save!
2551 2569
2552 2570 @request.session[:user_id] = 2
2553 2571 get :edit, :id => 1
2554 2572 assert_response :success
2555 2573 assert_template 'edit'
2556 2574
2557 2575 assert_tag 'select', :attributes => {:name => 'issue[custom_field_values][1][]', :multiple => 'multiple'}
2558 2576 assert_tag 'select', :attributes => {:name => 'issue[custom_field_values][1][]'},
2559 2577 :child => {:tag => 'option', :attributes => {:value => 'MySQL', :selected => 'selected'}}
2560 2578 assert_tag 'select', :attributes => {:name => 'issue[custom_field_values][1][]'},
2561 2579 :child => {:tag => 'option', :attributes => {:value => 'PostgreSQL', :selected => nil}}
2562 2580 assert_tag 'select', :attributes => {:name => 'issue[custom_field_values][1][]'},
2563 2581 :child => {:tag => 'option', :attributes => {:value => 'Oracle', :selected => 'selected'}}
2564 2582 end
2565 2583
2566 2584 def test_update_edit_form
2567 2585 @request.session[:user_id] = 2
2568 2586 xhr :put, :new, :project_id => 1,
2569 2587 :id => 1,
2570 2588 :issue => {:tracker_id => 2,
2571 2589 :subject => 'This is the test_new issue',
2572 2590 :description => 'This is the description',
2573 2591 :priority_id => 5}
2574 2592 assert_response :success
2575 2593 assert_equal 'text/javascript', response.content_type
2576 2594 assert_template 'update_form'
2577 2595 assert_template 'form'
2578 2596
2579 2597 issue = assigns(:issue)
2580 2598 assert_kind_of Issue, issue
2581 2599 assert_equal 1, issue.id
2582 2600 assert_equal 1, issue.project_id
2583 2601 assert_equal 2, issue.tracker_id
2584 2602 assert_equal 'This is the test_new issue', issue.subject
2585 2603 end
2586 2604
2587 2605 def test_update_edit_form_should_keep_issue_author
2588 2606 @request.session[:user_id] = 3
2589 2607 xhr :put, :new, :project_id => 1, :id => 1, :issue => {:subject => 'Changed'}
2590 2608 assert_response :success
2591 2609 assert_equal 'text/javascript', response.content_type
2592 2610
2593 2611 issue = assigns(:issue)
2594 2612 assert_equal User.find(2), issue.author
2595 2613 assert_equal 2, issue.author_id
2596 2614 assert_not_equal User.current, issue.author
2597 2615 end
2598 2616
2599 2617 def test_update_edit_form_should_propose_transitions_based_on_initial_status
2600 2618 @request.session[:user_id] = 2
2601 2619 WorkflowTransition.delete_all
2602 2620 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 2, :new_status_id => 1)
2603 2621 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 2, :new_status_id => 5)
2604 2622 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 5, :new_status_id => 4)
2605 2623
2606 2624 xhr :put, :new, :project_id => 1,
2607 2625 :id => 2,
2608 2626 :issue => {:tracker_id => 2,
2609 2627 :status_id => 5,
2610 2628 :subject => 'This is an issue'}
2611 2629
2612 2630 assert_equal 5, assigns(:issue).status_id
2613 2631 assert_equal [1,2,5], assigns(:allowed_statuses).map(&:id).sort
2614 2632 end
2615 2633
2616 2634 def test_update_edit_form_with_project_change
2617 2635 @request.session[:user_id] = 2
2618 2636 xhr :put, :new, :project_id => 1,
2619 2637 :id => 1,
2620 2638 :issue => {:project_id => 2,
2621 2639 :tracker_id => 2,
2622 2640 :subject => 'This is the test_new issue',
2623 2641 :description => 'This is the description',
2624 2642 :priority_id => 5}
2625 2643 assert_response :success
2626 2644 assert_template 'form'
2627 2645
2628 2646 issue = assigns(:issue)
2629 2647 assert_kind_of Issue, issue
2630 2648 assert_equal 1, issue.id
2631 2649 assert_equal 2, issue.project_id
2632 2650 assert_equal 2, issue.tracker_id
2633 2651 assert_equal 'This is the test_new issue', issue.subject
2634 2652 end
2635 2653
2636 2654 def test_put_update_without_custom_fields_param
2637 2655 @request.session[:user_id] = 2
2638 2656 ActionMailer::Base.deliveries.clear
2639 2657
2640 2658 issue = Issue.find(1)
2641 2659 assert_equal '125', issue.custom_value_for(2).value
2642 2660 old_subject = issue.subject
2643 2661 new_subject = 'Subject modified by IssuesControllerTest#test_post_edit'
2644 2662
2645 2663 assert_difference('Journal.count') do
2646 2664 assert_difference('JournalDetail.count', 2) do
2647 2665 put :update, :id => 1, :issue => {:subject => new_subject,
2648 2666 :priority_id => '6',
2649 2667 :category_id => '1' # no change
2650 2668 }
2651 2669 end
2652 2670 end
2653 2671 assert_redirected_to :action => 'show', :id => '1'
2654 2672 issue.reload
2655 2673 assert_equal new_subject, issue.subject
2656 2674 # Make sure custom fields were not cleared
2657 2675 assert_equal '125', issue.custom_value_for(2).value
2658 2676
2659 2677 mail = ActionMailer::Base.deliveries.last
2660 2678 assert_not_nil mail
2661 2679 assert mail.subject.starts_with?("[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}]")
2662 2680 assert_mail_body_match "Subject changed from #{old_subject} to #{new_subject}", mail
2663 2681 end
2664 2682
2665 2683 def test_put_update_with_project_change
2666 2684 @request.session[:user_id] = 2
2667 2685 ActionMailer::Base.deliveries.clear
2668 2686
2669 2687 assert_difference('Journal.count') do
2670 2688 assert_difference('JournalDetail.count', 3) do
2671 2689 put :update, :id => 1, :issue => {:project_id => '2',
2672 2690 :tracker_id => '1', # no change
2673 2691 :priority_id => '6',
2674 2692 :category_id => '3'
2675 2693 }
2676 2694 end
2677 2695 end
2678 2696 assert_redirected_to :action => 'show', :id => '1'
2679 2697 issue = Issue.find(1)
2680 2698 assert_equal 2, issue.project_id
2681 2699 assert_equal 1, issue.tracker_id
2682 2700 assert_equal 6, issue.priority_id
2683 2701 assert_equal 3, issue.category_id
2684 2702
2685 2703 mail = ActionMailer::Base.deliveries.last
2686 2704 assert_not_nil mail
2687 2705 assert mail.subject.starts_with?("[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}]")
2688 2706 assert_mail_body_match "Project changed from eCookbook to OnlineStore", mail
2689 2707 end
2690 2708
2691 2709 def test_put_update_with_tracker_change
2692 2710 @request.session[:user_id] = 2
2693 2711 ActionMailer::Base.deliveries.clear
2694 2712
2695 2713 assert_difference('Journal.count') do
2696 2714 assert_difference('JournalDetail.count', 2) do
2697 2715 put :update, :id => 1, :issue => {:project_id => '1',
2698 2716 :tracker_id => '2',
2699 2717 :priority_id => '6'
2700 2718 }
2701 2719 end
2702 2720 end
2703 2721 assert_redirected_to :action => 'show', :id => '1'
2704 2722 issue = Issue.find(1)
2705 2723 assert_equal 1, issue.project_id
2706 2724 assert_equal 2, issue.tracker_id
2707 2725 assert_equal 6, issue.priority_id
2708 2726 assert_equal 1, issue.category_id
2709 2727
2710 2728 mail = ActionMailer::Base.deliveries.last
2711 2729 assert_not_nil mail
2712 2730 assert mail.subject.starts_with?("[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}]")
2713 2731 assert_mail_body_match "Tracker changed from Bug to Feature request", mail
2714 2732 end
2715 2733
2716 2734 def test_put_update_with_custom_field_change
2717 2735 @request.session[:user_id] = 2
2718 2736 issue = Issue.find(1)
2719 2737 assert_equal '125', issue.custom_value_for(2).value
2720 2738
2721 2739 assert_difference('Journal.count') do
2722 2740 assert_difference('JournalDetail.count', 3) do
2723 2741 put :update, :id => 1, :issue => {:subject => 'Custom field change',
2724 2742 :priority_id => '6',
2725 2743 :category_id => '1', # no change
2726 2744 :custom_field_values => { '2' => 'New custom value' }
2727 2745 }
2728 2746 end
2729 2747 end
2730 2748 assert_redirected_to :action => 'show', :id => '1'
2731 2749 issue.reload
2732 2750 assert_equal 'New custom value', issue.custom_value_for(2).value
2733 2751
2734 2752 mail = ActionMailer::Base.deliveries.last
2735 2753 assert_not_nil mail
2736 2754 assert_mail_body_match "Searchable field changed from 125 to New custom value", mail
2737 2755 end
2738 2756
2739 2757 def test_put_update_with_multi_custom_field_change
2740 2758 field = CustomField.find(1)
2741 2759 field.update_attribute :multiple, true
2742 2760 issue = Issue.find(1)
2743 2761 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
2744 2762 issue.save!
2745 2763
2746 2764 @request.session[:user_id] = 2
2747 2765 assert_difference('Journal.count') do
2748 2766 assert_difference('JournalDetail.count', 3) do
2749 2767 put :update, :id => 1,
2750 2768 :issue => {
2751 2769 :subject => 'Custom field change',
2752 2770 :custom_field_values => { '1' => ['', 'Oracle', 'PostgreSQL'] }
2753 2771 }
2754 2772 end
2755 2773 end
2756 2774 assert_redirected_to :action => 'show', :id => '1'
2757 2775 assert_equal ['Oracle', 'PostgreSQL'], Issue.find(1).custom_field_value(1).sort
2758 2776 end
2759 2777
2760 2778 def test_put_update_with_status_and_assignee_change
2761 2779 issue = Issue.find(1)
2762 2780 assert_equal 1, issue.status_id
2763 2781 @request.session[:user_id] = 2
2764 2782 assert_difference('TimeEntry.count', 0) do
2765 2783 put :update,
2766 2784 :id => 1,
2767 2785 :issue => { :status_id => 2, :assigned_to_id => 3, :notes => 'Assigned to dlopper' },
2768 2786 :time_entry => { :hours => '', :comments => '', :activity_id => TimeEntryActivity.first }
2769 2787 end
2770 2788 assert_redirected_to :action => 'show', :id => '1'
2771 2789 issue.reload
2772 2790 assert_equal 2, issue.status_id
2773 2791 j = Journal.find(:first, :order => 'id DESC')
2774 2792 assert_equal 'Assigned to dlopper', j.notes
2775 2793 assert_equal 2, j.details.size
2776 2794
2777 2795 mail = ActionMailer::Base.deliveries.last
2778 2796 assert_mail_body_match "Status changed from New to Assigned", mail
2779 2797 # subject should contain the new status
2780 2798 assert mail.subject.include?("(#{ IssueStatus.find(2).name })")
2781 2799 end
2782 2800
2783 2801 def test_put_update_with_note_only
2784 2802 notes = 'Note added by IssuesControllerTest#test_update_with_note_only'
2785 2803 # anonymous user
2786 2804 put :update,
2787 2805 :id => 1,
2788 2806 :issue => { :notes => notes }
2789 2807 assert_redirected_to :action => 'show', :id => '1'
2790 2808 j = Journal.find(:first, :order => 'id DESC')
2791 2809 assert_equal notes, j.notes
2792 2810 assert_equal 0, j.details.size
2793 2811 assert_equal User.anonymous, j.user
2794 2812
2795 2813 mail = ActionMailer::Base.deliveries.last
2796 2814 assert_mail_body_match notes, mail
2797 2815 end
2798 2816
2799 2817 def test_put_update_with_private_note_only
2800 2818 notes = 'Private note'
2801 2819 @request.session[:user_id] = 2
2802 2820
2803 2821 assert_difference 'Journal.count' do
2804 2822 put :update, :id => 1, :issue => {:notes => notes, :private_notes => '1'}
2805 2823 assert_redirected_to :action => 'show', :id => '1'
2806 2824 end
2807 2825
2808 2826 j = Journal.order('id DESC').first
2809 2827 assert_equal notes, j.notes
2810 2828 assert_equal true, j.private_notes
2811 2829 end
2812 2830
2813 2831 def test_put_update_with_private_note_and_changes
2814 2832 notes = 'Private note'
2815 2833 @request.session[:user_id] = 2
2816 2834
2817 2835 assert_difference 'Journal.count', 2 do
2818 2836 put :update, :id => 1, :issue => {:subject => 'New subject', :notes => notes, :private_notes => '1'}
2819 2837 assert_redirected_to :action => 'show', :id => '1'
2820 2838 end
2821 2839
2822 2840 j = Journal.order('id DESC').first
2823 2841 assert_equal notes, j.notes
2824 2842 assert_equal true, j.private_notes
2825 2843 assert_equal 0, j.details.count
2826 2844
2827 2845 j = Journal.order('id DESC').offset(1).first
2828 2846 assert_nil j.notes
2829 2847 assert_equal false, j.private_notes
2830 2848 assert_equal 1, j.details.count
2831 2849 end
2832 2850
2833 2851 def test_put_update_with_note_and_spent_time
2834 2852 @request.session[:user_id] = 2
2835 2853 spent_hours_before = Issue.find(1).spent_hours
2836 2854 assert_difference('TimeEntry.count') do
2837 2855 put :update,
2838 2856 :id => 1,
2839 2857 :issue => { :notes => '2.5 hours added' },
2840 2858 :time_entry => { :hours => '2.5', :comments => 'test_put_update_with_note_and_spent_time', :activity_id => TimeEntryActivity.first.id }
2841 2859 end
2842 2860 assert_redirected_to :action => 'show', :id => '1'
2843 2861
2844 2862 issue = Issue.find(1)
2845 2863
2846 2864 j = Journal.find(:first, :order => 'id DESC')
2847 2865 assert_equal '2.5 hours added', j.notes
2848 2866 assert_equal 0, j.details.size
2849 2867
2850 2868 t = issue.time_entries.find_by_comments('test_put_update_with_note_and_spent_time')
2851 2869 assert_not_nil t
2852 2870 assert_equal 2.5, t.hours
2853 2871 assert_equal spent_hours_before + 2.5, issue.spent_hours
2854 2872 end
2855 2873
2856 2874 def test_put_update_with_attachment_only
2857 2875 set_tmp_attachments_directory
2858 2876
2859 2877 # Delete all fixtured journals, a race condition can occur causing the wrong
2860 2878 # journal to get fetched in the next find.
2861 2879 Journal.delete_all
2862 2880
2863 2881 # anonymous user
2864 2882 assert_difference 'Attachment.count' do
2865 2883 put :update, :id => 1,
2866 2884 :issue => {:notes => ''},
2867 2885 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
2868 2886 end
2869 2887
2870 2888 assert_redirected_to :action => 'show', :id => '1'
2871 2889 j = Issue.find(1).journals.find(:first, :order => 'id DESC')
2872 2890 assert j.notes.blank?
2873 2891 assert_equal 1, j.details.size
2874 2892 assert_equal 'testfile.txt', j.details.first.value
2875 2893 assert_equal User.anonymous, j.user
2876 2894
2877 2895 attachment = Attachment.first(:order => 'id DESC')
2878 2896 assert_equal Issue.find(1), attachment.container
2879 2897 assert_equal User.anonymous, attachment.author
2880 2898 assert_equal 'testfile.txt', attachment.filename
2881 2899 assert_equal 'text/plain', attachment.content_type
2882 2900 assert_equal 'test file', attachment.description
2883 2901 assert_equal 59, attachment.filesize
2884 2902 assert File.exists?(attachment.diskfile)
2885 2903 assert_equal 59, File.size(attachment.diskfile)
2886 2904
2887 2905 mail = ActionMailer::Base.deliveries.last
2888 2906 assert_mail_body_match 'testfile.txt', mail
2889 2907 end
2890 2908
2891 2909 def test_put_update_with_failure_should_save_attachments
2892 2910 set_tmp_attachments_directory
2893 2911 @request.session[:user_id] = 2
2894 2912
2895 2913 assert_no_difference 'Journal.count' do
2896 2914 assert_difference 'Attachment.count' do
2897 2915 put :update, :id => 1,
2898 2916 :issue => { :subject => '' },
2899 2917 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
2900 2918 assert_response :success
2901 2919 assert_template 'edit'
2902 2920 end
2903 2921 end
2904 2922
2905 2923 attachment = Attachment.first(:order => 'id DESC')
2906 2924 assert_equal 'testfile.txt', attachment.filename
2907 2925 assert File.exists?(attachment.diskfile)
2908 2926 assert_nil attachment.container
2909 2927
2910 2928 assert_tag 'input', :attributes => {:name => 'attachments[p0][token]', :value => attachment.token}
2911 2929 assert_tag 'span', :content => /testfile.txt/
2912 2930 end
2913 2931
2914 2932 def test_put_update_with_failure_should_keep_saved_attachments
2915 2933 set_tmp_attachments_directory
2916 2934 attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 2)
2917 2935 @request.session[:user_id] = 2
2918 2936
2919 2937 assert_no_difference 'Journal.count' do
2920 2938 assert_no_difference 'Attachment.count' do
2921 2939 put :update, :id => 1,
2922 2940 :issue => { :subject => '' },
2923 2941 :attachments => {'p0' => {'token' => attachment.token}}
2924 2942 assert_response :success
2925 2943 assert_template 'edit'
2926 2944 end
2927 2945 end
2928 2946
2929 2947 assert_tag 'input', :attributes => {:name => 'attachments[p0][token]', :value => attachment.token}
2930 2948 assert_tag 'span', :content => /testfile.txt/
2931 2949 end
2932 2950
2933 2951 def test_put_update_should_attach_saved_attachments
2934 2952 set_tmp_attachments_directory
2935 2953 attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 2)
2936 2954 @request.session[:user_id] = 2
2937 2955
2938 2956 assert_difference 'Journal.count' do
2939 2957 assert_difference 'JournalDetail.count' do
2940 2958 assert_no_difference 'Attachment.count' do
2941 2959 put :update, :id => 1,
2942 2960 :issue => {:notes => 'Attachment added'},
2943 2961 :attachments => {'p0' => {'token' => attachment.token}}
2944 2962 assert_redirected_to '/issues/1'
2945 2963 end
2946 2964 end
2947 2965 end
2948 2966
2949 2967 attachment.reload
2950 2968 assert_equal Issue.find(1), attachment.container
2951 2969
2952 2970 journal = Journal.first(:order => 'id DESC')
2953 2971 assert_equal 1, journal.details.size
2954 2972 assert_equal 'testfile.txt', journal.details.first.value
2955 2973 end
2956 2974
2957 2975 def test_put_update_with_attachment_that_fails_to_save
2958 2976 set_tmp_attachments_directory
2959 2977
2960 2978 # Delete all fixtured journals, a race condition can occur causing the wrong
2961 2979 # journal to get fetched in the next find.
2962 2980 Journal.delete_all
2963 2981
2964 2982 # Mock out the unsaved attachment
2965 2983 Attachment.any_instance.stubs(:create).returns(Attachment.new)
2966 2984
2967 2985 # anonymous user
2968 2986 put :update,
2969 2987 :id => 1,
2970 2988 :issue => {:notes => ''},
2971 2989 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
2972 2990 assert_redirected_to :action => 'show', :id => '1'
2973 2991 assert_equal '1 file(s) could not be saved.', flash[:warning]
2974 2992 end
2975 2993
2976 2994 def test_put_update_with_no_change
2977 2995 issue = Issue.find(1)
2978 2996 issue.journals.clear
2979 2997 ActionMailer::Base.deliveries.clear
2980 2998
2981 2999 put :update,
2982 3000 :id => 1,
2983 3001 :issue => {:notes => ''}
2984 3002 assert_redirected_to :action => 'show', :id => '1'
2985 3003
2986 3004 issue.reload
2987 3005 assert issue.journals.empty?
2988 3006 # No email should be sent
2989 3007 assert ActionMailer::Base.deliveries.empty?
2990 3008 end
2991 3009
2992 3010 def test_put_update_should_send_a_notification
2993 3011 @request.session[:user_id] = 2
2994 3012 ActionMailer::Base.deliveries.clear
2995 3013 issue = Issue.find(1)
2996 3014 old_subject = issue.subject
2997 3015 new_subject = 'Subject modified by IssuesControllerTest#test_post_edit'
2998 3016
2999 3017 put :update, :id => 1, :issue => {:subject => new_subject,
3000 3018 :priority_id => '6',
3001 3019 :category_id => '1' # no change
3002 3020 }
3003 3021 assert_equal 1, ActionMailer::Base.deliveries.size
3004 3022 end
3005 3023
3006 3024 def test_put_update_with_invalid_spent_time_hours_only
3007 3025 @request.session[:user_id] = 2
3008 3026 notes = 'Note added by IssuesControllerTest#test_post_edit_with_invalid_spent_time'
3009 3027
3010 3028 assert_no_difference('Journal.count') do
3011 3029 put :update,
3012 3030 :id => 1,
3013 3031 :issue => {:notes => notes},
3014 3032 :time_entry => {"comments"=>"", "activity_id"=>"", "hours"=>"2z"}
3015 3033 end
3016 3034 assert_response :success
3017 3035 assert_template 'edit'
3018 3036
3019 3037 assert_error_tag :descendant => {:content => /Activity can&#x27;t be blank/}
3020 3038 assert_tag :textarea, :attributes => { :name => 'issue[notes]' }, :content => "\n"+notes
3021 3039 assert_tag :input, :attributes => { :name => 'time_entry[hours]', :value => "2z" }
3022 3040 end
3023 3041
3024 3042 def test_put_update_with_invalid_spent_time_comments_only
3025 3043 @request.session[:user_id] = 2
3026 3044 notes = 'Note added by IssuesControllerTest#test_post_edit_with_invalid_spent_time'
3027 3045
3028 3046 assert_no_difference('Journal.count') do
3029 3047 put :update,
3030 3048 :id => 1,
3031 3049 :issue => {:notes => notes},
3032 3050 :time_entry => {"comments"=>"this is my comment", "activity_id"=>"", "hours"=>""}
3033 3051 end
3034 3052 assert_response :success
3035 3053 assert_template 'edit'
3036 3054
3037 3055 assert_error_tag :descendant => {:content => /Activity can&#x27;t be blank/}
3038 3056 assert_error_tag :descendant => {:content => /Hours can&#x27;t be blank/}
3039 3057 assert_tag :textarea, :attributes => { :name => 'issue[notes]' }, :content => "\n"+notes
3040 3058 assert_tag :input, :attributes => { :name => 'time_entry[comments]', :value => "this is my comment" }
3041 3059 end
3042 3060
3043 3061 def test_put_update_should_allow_fixed_version_to_be_set_to_a_subproject
3044 3062 issue = Issue.find(2)
3045 3063 @request.session[:user_id] = 2
3046 3064
3047 3065 put :update,
3048 3066 :id => issue.id,
3049 3067 :issue => {
3050 3068 :fixed_version_id => 4
3051 3069 }
3052 3070
3053 3071 assert_response :redirect
3054 3072 issue.reload
3055 3073 assert_equal 4, issue.fixed_version_id
3056 3074 assert_not_equal issue.project_id, issue.fixed_version.project_id
3057 3075 end
3058 3076
3059 3077 def test_put_update_should_redirect_back_using_the_back_url_parameter
3060 3078 issue = Issue.find(2)
3061 3079 @request.session[:user_id] = 2
3062 3080
3063 3081 put :update,
3064 3082 :id => issue.id,
3065 3083 :issue => {
3066 3084 :fixed_version_id => 4
3067 3085 },
3068 3086 :back_url => '/issues'
3069 3087
3070 3088 assert_response :redirect
3071 3089 assert_redirected_to '/issues'
3072 3090 end
3073 3091
3074 3092 def test_put_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host
3075 3093 issue = Issue.find(2)
3076 3094 @request.session[:user_id] = 2
3077 3095
3078 3096 put :update,
3079 3097 :id => issue.id,
3080 3098 :issue => {
3081 3099 :fixed_version_id => 4
3082 3100 },
3083 3101 :back_url => 'http://google.com'
3084 3102
3085 3103 assert_response :redirect
3086 3104 assert_redirected_to :controller => 'issues', :action => 'show', :id => issue.id
3087 3105 end
3088 3106
3089 3107 def test_get_bulk_edit
3090 3108 @request.session[:user_id] = 2
3091 3109 get :bulk_edit, :ids => [1, 2]
3092 3110 assert_response :success
3093 3111 assert_template 'bulk_edit'
3094 3112
3095 3113 assert_tag :select, :attributes => {:name => 'issue[project_id]'}
3096 3114 assert_tag :input, :attributes => {:name => 'issue[parent_issue_id]'}
3097 3115
3098 3116 # Project specific custom field, date type
3099 3117 field = CustomField.find(9)
3100 3118 assert !field.is_for_all?
3101 3119 assert_equal 'date', field.field_format
3102 3120 assert_tag :input, :attributes => {:name => 'issue[custom_field_values][9]'}
3103 3121
3104 3122 # System wide custom field
3105 3123 assert CustomField.find(1).is_for_all?
3106 3124 assert_tag :select, :attributes => {:name => 'issue[custom_field_values][1]'}
3107 3125
3108 3126 # Be sure we don't display inactive IssuePriorities
3109 3127 assert ! IssuePriority.find(15).active?
3110 3128 assert_no_tag :option, :attributes => {:value => '15'},
3111 3129 :parent => {:tag => 'select', :attributes => {:id => 'issue_priority_id'} }
3112 3130 end
3113 3131
3114 3132 def test_get_bulk_edit_on_different_projects
3115 3133 @request.session[:user_id] = 2
3116 3134 get :bulk_edit, :ids => [1, 2, 6]
3117 3135 assert_response :success
3118 3136 assert_template 'bulk_edit'
3119 3137
3120 3138 # Can not set issues from different projects as children of an issue
3121 3139 assert_no_tag :input, :attributes => {:name => 'issue[parent_issue_id]'}
3122 3140
3123 3141 # Project specific custom field, date type
3124 3142 field = CustomField.find(9)
3125 3143 assert !field.is_for_all?
3126 3144 assert !field.project_ids.include?(Issue.find(6).project_id)
3127 3145 assert_no_tag :input, :attributes => {:name => 'issue[custom_field_values][9]'}
3128 3146 end
3129 3147
3130 3148 def test_get_bulk_edit_with_user_custom_field
3131 3149 field = IssueCustomField.create!(:name => 'Tester', :field_format => 'user', :is_for_all => true)
3132 3150
3133 3151 @request.session[:user_id] = 2
3134 3152 get :bulk_edit, :ids => [1, 2]
3135 3153 assert_response :success
3136 3154 assert_template 'bulk_edit'
3137 3155
3138 3156 assert_tag :select,
3139 3157 :attributes => {:name => "issue[custom_field_values][#{field.id}]", :class => 'user_cf'},
3140 3158 :children => {
3141 3159 :only => {:tag => 'option'},
3142 3160 :count => Project.find(1).users.count + 2 # "no change" + "none" options
3143 3161 }
3144 3162 end
3145 3163
3146 3164 def test_get_bulk_edit_with_version_custom_field
3147 3165 field = IssueCustomField.create!(:name => 'Affected version', :field_format => 'version', :is_for_all => true)
3148 3166
3149 3167 @request.session[:user_id] = 2
3150 3168 get :bulk_edit, :ids => [1, 2]
3151 3169 assert_response :success
3152 3170 assert_template 'bulk_edit'
3153 3171
3154 3172 assert_tag :select,
3155 3173 :attributes => {:name => "issue[custom_field_values][#{field.id}]"},
3156 3174 :children => {
3157 3175 :only => {:tag => 'option'},
3158 3176 :count => Project.find(1).shared_versions.count + 2 # "no change" + "none" options
3159 3177 }
3160 3178 end
3161 3179
3162 3180 def test_get_bulk_edit_with_multi_custom_field
3163 3181 field = CustomField.find(1)
3164 3182 field.update_attribute :multiple, true
3165 3183
3166 3184 @request.session[:user_id] = 2
3167 3185 get :bulk_edit, :ids => [1, 2]
3168 3186 assert_response :success
3169 3187 assert_template 'bulk_edit'
3170 3188
3171 3189 assert_tag :select,
3172 3190 :attributes => {:name => "issue[custom_field_values][1][]"},
3173 3191 :children => {
3174 3192 :only => {:tag => 'option'},
3175 3193 :count => field.possible_values.size + 1 # "none" options
3176 3194 }
3177 3195 end
3178 3196
3179 3197 def test_bulk_edit_should_only_propose_statuses_allowed_for_all_issues
3180 3198 WorkflowTransition.delete_all
3181 3199 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 1)
3182 3200 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 3)
3183 3201 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 4)
3184 3202 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 2, :new_status_id => 1)
3185 3203 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 2, :new_status_id => 3)
3186 3204 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 2, :new_status_id => 5)
3187 3205 @request.session[:user_id] = 2
3188 3206 get :bulk_edit, :ids => [1, 2]
3189 3207
3190 3208 assert_response :success
3191 3209 statuses = assigns(:available_statuses)
3192 3210 assert_not_nil statuses
3193 3211 assert_equal [1, 3], statuses.map(&:id).sort
3194 3212
3195 3213 assert_tag 'select', :attributes => {:name => 'issue[status_id]'},
3196 3214 :children => {:count => 3} # 2 statuses + "no change" option
3197 3215 end
3198 3216
3199 3217 def test_bulk_edit_should_propose_target_project_open_shared_versions
3200 3218 @request.session[:user_id] = 2
3201 3219 post :bulk_edit, :ids => [1, 2, 6], :issue => {:project_id => 1}
3202 3220 assert_response :success
3203 3221 assert_template 'bulk_edit'
3204 3222 assert_equal Project.find(1).shared_versions.open.all.sort, assigns(:versions).sort
3205 3223 assert_tag 'select',
3206 3224 :attributes => {:name => 'issue[fixed_version_id]'},
3207 3225 :descendant => {:tag => 'option', :content => '2.0'}
3208 3226 end
3209 3227
3210 3228 def test_bulk_edit_should_propose_target_project_categories
3211 3229 @request.session[:user_id] = 2
3212 3230 post :bulk_edit, :ids => [1, 2, 6], :issue => {:project_id => 1}
3213 3231 assert_response :success
3214 3232 assert_template 'bulk_edit'
3215 3233 assert_equal Project.find(1).issue_categories.sort, assigns(:categories).sort
3216 3234 assert_tag 'select',
3217 3235 :attributes => {:name => 'issue[category_id]'},
3218 3236 :descendant => {:tag => 'option', :content => 'Recipes'}
3219 3237 end
3220 3238
3221 3239 def test_bulk_update
3222 3240 @request.session[:user_id] = 2
3223 3241 # update issues priority
3224 3242 post :bulk_update, :ids => [1, 2], :notes => 'Bulk editing',
3225 3243 :issue => {:priority_id => 7,
3226 3244 :assigned_to_id => '',
3227 3245 :custom_field_values => {'2' => ''}}
3228 3246
3229 3247 assert_response 302
3230 3248 # check that the issues were updated
3231 3249 assert_equal [7, 7], Issue.find_all_by_id([1, 2]).collect {|i| i.priority.id}
3232 3250
3233 3251 issue = Issue.find(1)
3234 3252 journal = issue.journals.find(:first, :order => 'created_on DESC')
3235 3253 assert_equal '125', issue.custom_value_for(2).value
3236 3254 assert_equal 'Bulk editing', journal.notes
3237 3255 assert_equal 1, journal.details.size
3238 3256 end
3239 3257
3240 3258 def test_bulk_update_with_group_assignee
3241 3259 group = Group.find(11)
3242 3260 project = Project.find(1)
3243 3261 project.members << Member.new(:principal => group, :roles => [Role.givable.first])
3244 3262
3245 3263 @request.session[:user_id] = 2
3246 3264 # update issues assignee
3247 3265 post :bulk_update, :ids => [1, 2], :notes => 'Bulk editing',
3248 3266 :issue => {:priority_id => '',
3249 3267 :assigned_to_id => group.id,
3250 3268 :custom_field_values => {'2' => ''}}
3251 3269
3252 3270 assert_response 302
3253 3271 assert_equal [group, group], Issue.find_all_by_id([1, 2]).collect {|i| i.assigned_to}
3254 3272 end
3255 3273
3256 3274 def test_bulk_update_on_different_projects
3257 3275 @request.session[:user_id] = 2
3258 3276 # update issues priority
3259 3277 post :bulk_update, :ids => [1, 2, 6], :notes => 'Bulk editing',
3260 3278 :issue => {:priority_id => 7,
3261 3279 :assigned_to_id => '',
3262 3280 :custom_field_values => {'2' => ''}}
3263 3281
3264 3282 assert_response 302
3265 3283 # check that the issues were updated
3266 3284 assert_equal [7, 7, 7], Issue.find([1,2,6]).map(&:priority_id)
3267 3285
3268 3286 issue = Issue.find(1)
3269 3287 journal = issue.journals.find(:first, :order => 'created_on DESC')
3270 3288 assert_equal '125', issue.custom_value_for(2).value
3271 3289 assert_equal 'Bulk editing', journal.notes
3272 3290 assert_equal 1, journal.details.size
3273 3291 end
3274 3292
3275 3293 def test_bulk_update_on_different_projects_without_rights
3276 3294 @request.session[:user_id] = 3
3277 3295 user = User.find(3)
3278 3296 action = { :controller => "issues", :action => "bulk_update" }
3279 3297 assert user.allowed_to?(action, Issue.find(1).project)
3280 3298 assert ! user.allowed_to?(action, Issue.find(6).project)
3281 3299 post :bulk_update, :ids => [1, 6], :notes => 'Bulk should fail',
3282 3300 :issue => {:priority_id => 7,
3283 3301 :assigned_to_id => '',
3284 3302 :custom_field_values => {'2' => ''}}
3285 3303 assert_response 403
3286 3304 assert_not_equal "Bulk should fail", Journal.last.notes
3287 3305 end
3288 3306
3289 3307 def test_bullk_update_should_send_a_notification
3290 3308 @request.session[:user_id] = 2
3291 3309 ActionMailer::Base.deliveries.clear
3292 3310 post(:bulk_update,
3293 3311 {
3294 3312 :ids => [1, 2],
3295 3313 :notes => 'Bulk editing',
3296 3314 :issue => {
3297 3315 :priority_id => 7,
3298 3316 :assigned_to_id => '',
3299 3317 :custom_field_values => {'2' => ''}
3300 3318 }
3301 3319 })
3302 3320
3303 3321 assert_response 302
3304 3322 assert_equal 2, ActionMailer::Base.deliveries.size
3305 3323 end
3306 3324
3307 3325 def test_bulk_update_project
3308 3326 @request.session[:user_id] = 2
3309 3327 post :bulk_update, :ids => [1, 2], :issue => {:project_id => '2'}
3310 3328 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook'
3311 3329 # Issues moved to project 2
3312 3330 assert_equal 2, Issue.find(1).project_id
3313 3331 assert_equal 2, Issue.find(2).project_id
3314 3332 # No tracker change
3315 3333 assert_equal 1, Issue.find(1).tracker_id
3316 3334 assert_equal 2, Issue.find(2).tracker_id
3317 3335 end
3318 3336
3319 3337 def test_bulk_update_project_on_single_issue_should_follow_when_needed
3320 3338 @request.session[:user_id] = 2
3321 3339 post :bulk_update, :id => 1, :issue => {:project_id => '2'}, :follow => '1'
3322 3340 assert_redirected_to '/issues/1'
3323 3341 end
3324 3342
3325 3343 def test_bulk_update_project_on_multiple_issues_should_follow_when_needed
3326 3344 @request.session[:user_id] = 2
3327 3345 post :bulk_update, :id => [1, 2], :issue => {:project_id => '2'}, :follow => '1'
3328 3346 assert_redirected_to '/projects/onlinestore/issues'
3329 3347 end
3330 3348
3331 3349 def test_bulk_update_tracker
3332 3350 @request.session[:user_id] = 2
3333 3351 post :bulk_update, :ids => [1, 2], :issue => {:tracker_id => '2'}
3334 3352 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook'
3335 3353 assert_equal 2, Issue.find(1).tracker_id
3336 3354 assert_equal 2, Issue.find(2).tracker_id
3337 3355 end
3338 3356
3339 3357 def test_bulk_update_status
3340 3358 @request.session[:user_id] = 2
3341 3359 # update issues priority
3342 3360 post :bulk_update, :ids => [1, 2], :notes => 'Bulk editing status',
3343 3361 :issue => {:priority_id => '',
3344 3362 :assigned_to_id => '',
3345 3363 :status_id => '5'}
3346 3364
3347 3365 assert_response 302
3348 3366 issue = Issue.find(1)
3349 3367 assert issue.closed?
3350 3368 end
3351 3369
3352 3370 def test_bulk_update_priority
3353 3371 @request.session[:user_id] = 2
3354 3372 post :bulk_update, :ids => [1, 2], :issue => {:priority_id => 6}
3355 3373
3356 3374 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook'
3357 3375 assert_equal 6, Issue.find(1).priority_id
3358 3376 assert_equal 6, Issue.find(2).priority_id
3359 3377 end
3360 3378
3361 3379 def test_bulk_update_with_notes
3362 3380 @request.session[:user_id] = 2
3363 3381 post :bulk_update, :ids => [1, 2], :notes => 'Moving two issues'
3364 3382
3365 3383 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook'
3366 3384 assert_equal 'Moving two issues', Issue.find(1).journals.sort_by(&:id).last.notes
3367 3385 assert_equal 'Moving two issues', Issue.find(2).journals.sort_by(&:id).last.notes
3368 3386 end
3369 3387
3370 3388 def test_bulk_update_parent_id
3371 3389 @request.session[:user_id] = 2
3372 3390 post :bulk_update, :ids => [1, 3],
3373 3391 :notes => 'Bulk editing parent',
3374 3392 :issue => {:priority_id => '', :assigned_to_id => '', :status_id => '', :parent_issue_id => '2'}
3375 3393
3376 3394 assert_response 302
3377 3395 parent = Issue.find(2)
3378 3396 assert_equal parent.id, Issue.find(1).parent_id
3379 3397 assert_equal parent.id, Issue.find(3).parent_id
3380 3398 assert_equal [1, 3], parent.children.collect(&:id).sort
3381 3399 end
3382 3400
3383 3401 def test_bulk_update_custom_field
3384 3402 @request.session[:user_id] = 2
3385 3403 # update issues priority
3386 3404 post :bulk_update, :ids => [1, 2], :notes => 'Bulk editing custom field',
3387 3405 :issue => {:priority_id => '',
3388 3406 :assigned_to_id => '',
3389 3407 :custom_field_values => {'2' => '777'}}
3390 3408
3391 3409 assert_response 302
3392 3410
3393 3411 issue = Issue.find(1)
3394 3412 journal = issue.journals.find(:first, :order => 'created_on DESC')
3395 3413 assert_equal '777', issue.custom_value_for(2).value
3396 3414 assert_equal 1, journal.details.size
3397 3415 assert_equal '125', journal.details.first.old_value
3398 3416 assert_equal '777', journal.details.first.value
3399 3417 end
3400 3418
3401 3419 def test_bulk_update_custom_field_to_blank
3402 3420 @request.session[:user_id] = 2
3403 3421 post :bulk_update, :ids => [1, 3], :notes => 'Bulk editing custom field',
3404 3422 :issue => {:priority_id => '',
3405 3423 :assigned_to_id => '',
3406 3424 :custom_field_values => {'1' => '__none__'}}
3407 3425 assert_response 302
3408 3426 assert_equal '', Issue.find(1).custom_field_value(1)
3409 3427 assert_equal '', Issue.find(3).custom_field_value(1)
3410 3428 end
3411 3429
3412 3430 def test_bulk_update_multi_custom_field
3413 3431 field = CustomField.find(1)
3414 3432 field.update_attribute :multiple, true
3415 3433
3416 3434 @request.session[:user_id] = 2
3417 3435 post :bulk_update, :ids => [1, 2, 3], :notes => 'Bulk editing multi custom field',
3418 3436 :issue => {:priority_id => '',
3419 3437 :assigned_to_id => '',
3420 3438 :custom_field_values => {'1' => ['MySQL', 'Oracle']}}
3421 3439
3422 3440 assert_response 302
3423 3441
3424 3442 assert_equal ['MySQL', 'Oracle'], Issue.find(1).custom_field_value(1).sort
3425 3443 assert_equal ['MySQL', 'Oracle'], Issue.find(3).custom_field_value(1).sort
3426 3444 # the custom field is not associated with the issue tracker
3427 3445 assert_nil Issue.find(2).custom_field_value(1)
3428 3446 end
3429 3447
3430 3448 def test_bulk_update_multi_custom_field_to_blank
3431 3449 field = CustomField.find(1)
3432 3450 field.update_attribute :multiple, true
3433 3451
3434 3452 @request.session[:user_id] = 2
3435 3453 post :bulk_update, :ids => [1, 3], :notes => 'Bulk editing multi custom field',
3436 3454 :issue => {:priority_id => '',
3437 3455 :assigned_to_id => '',
3438 3456 :custom_field_values => {'1' => ['__none__']}}
3439 3457 assert_response 302
3440 3458 assert_equal [''], Issue.find(1).custom_field_value(1)
3441 3459 assert_equal [''], Issue.find(3).custom_field_value(1)
3442 3460 end
3443 3461
3444 3462 def test_bulk_update_unassign
3445 3463 assert_not_nil Issue.find(2).assigned_to
3446 3464 @request.session[:user_id] = 2
3447 3465 # unassign issues
3448 3466 post :bulk_update, :ids => [1, 2], :notes => 'Bulk unassigning', :issue => {:assigned_to_id => 'none'}
3449 3467 assert_response 302
3450 3468 # check that the issues were updated
3451 3469 assert_nil Issue.find(2).assigned_to
3452 3470 end
3453 3471
3454 3472 def test_post_bulk_update_should_allow_fixed_version_to_be_set_to_a_subproject
3455 3473 @request.session[:user_id] = 2
3456 3474
3457 3475 post :bulk_update, :ids => [1,2], :issue => {:fixed_version_id => 4}
3458 3476
3459 3477 assert_response :redirect
3460 3478 issues = Issue.find([1,2])
3461 3479 issues.each do |issue|
3462 3480 assert_equal 4, issue.fixed_version_id
3463 3481 assert_not_equal issue.project_id, issue.fixed_version.project_id
3464 3482 end
3465 3483 end
3466 3484
3467 3485 def test_post_bulk_update_should_redirect_back_using_the_back_url_parameter
3468 3486 @request.session[:user_id] = 2
3469 3487 post :bulk_update, :ids => [1,2], :back_url => '/issues'
3470 3488
3471 3489 assert_response :redirect
3472 3490 assert_redirected_to '/issues'
3473 3491 end
3474 3492
3475 3493 def test_post_bulk_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host
3476 3494 @request.session[:user_id] = 2
3477 3495 post :bulk_update, :ids => [1,2], :back_url => 'http://google.com'
3478 3496
3479 3497 assert_response :redirect
3480 3498 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => Project.find(1).identifier
3481 3499 end
3482 3500
3483 3501 def test_bulk_update_with_failure_should_set_flash
3484 3502 @request.session[:user_id] = 2
3485 3503 Issue.update_all("subject = ''", "id = 2") # Make it invalid
3486 3504 post :bulk_update, :ids => [1, 2], :issue => {:priority_id => 6}
3487 3505
3488 3506 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook'
3489 3507 assert_equal 'Failed to save 1 issue(s) on 2 selected: #2.', flash[:error]
3490 3508 end
3491 3509
3492 3510 def test_get_bulk_copy
3493 3511 @request.session[:user_id] = 2
3494 3512 get :bulk_edit, :ids => [1, 2, 3], :copy => '1'
3495 3513 assert_response :success
3496 3514 assert_template 'bulk_edit'
3497 3515
3498 3516 issues = assigns(:issues)
3499 3517 assert_not_nil issues
3500 3518 assert_equal [1, 2, 3], issues.map(&:id).sort
3501 3519
3502 3520 assert_select 'input[name=copy_attachments]'
3503 3521 end
3504 3522
3505 3523 def test_bulk_copy_to_another_project
3506 3524 @request.session[:user_id] = 2
3507 3525 assert_difference 'Issue.count', 2 do
3508 3526 assert_no_difference 'Project.find(1).issues.count' do
3509 3527 post :bulk_update, :ids => [1, 2], :issue => {:project_id => '2'}, :copy => '1'
3510 3528 end
3511 3529 end
3512 3530 assert_redirected_to '/projects/ecookbook/issues'
3513 3531
3514 3532 copies = Issue.all(:order => 'id DESC', :limit => issues.size)
3515 3533 copies.each do |copy|
3516 3534 assert_equal 2, copy.project_id
3517 3535 end
3518 3536 end
3519 3537
3520 3538 def test_bulk_copy_should_allow_not_changing_the_issue_attributes
3521 3539 @request.session[:user_id] = 2
3522 3540 issues = [
3523 3541 Issue.create!(:project_id => 1, :tracker_id => 1, :status_id => 1, :priority_id => 2, :subject => 'issue 1', :author_id => 1, :assigned_to_id => nil),
3524 3542 Issue.create!(:project_id => 2, :tracker_id => 3, :status_id => 2, :priority_id => 1, :subject => 'issue 2', :author_id => 2, :assigned_to_id => 3)
3525 3543 ]
3526 3544
3527 3545 assert_difference 'Issue.count', issues.size do
3528 3546 post :bulk_update, :ids => issues.map(&:id), :copy => '1',
3529 3547 :issue => {
3530 3548 :project_id => '', :tracker_id => '', :assigned_to_id => '',
3531 3549 :status_id => '', :start_date => '', :due_date => ''
3532 3550 }
3533 3551 end
3534 3552
3535 3553 copies = Issue.all(:order => 'id DESC', :limit => issues.size)
3536 3554 issues.each do |orig|
3537 3555 copy = copies.detect {|c| c.subject == orig.subject}
3538 3556 assert_not_nil copy
3539 3557 assert_equal orig.project_id, copy.project_id
3540 3558 assert_equal orig.tracker_id, copy.tracker_id
3541 3559 assert_equal orig.status_id, copy.status_id
3542 3560 assert_equal orig.assigned_to_id, copy.assigned_to_id
3543 3561 assert_equal orig.priority_id, copy.priority_id
3544 3562 end
3545 3563 end
3546 3564
3547 3565 def test_bulk_copy_should_allow_changing_the_issue_attributes
3548 3566 # Fixes random test failure with Mysql
3549 3567 # where Issue.all(:limit => 2, :order => 'id desc', :conditions => {:project_id => 2})
3550 3568 # doesn't return the expected results
3551 3569 Issue.delete_all("project_id=2")
3552 3570
3553 3571 @request.session[:user_id] = 2
3554 3572 assert_difference 'Issue.count', 2 do
3555 3573 assert_no_difference 'Project.find(1).issues.count' do
3556 3574 post :bulk_update, :ids => [1, 2], :copy => '1',
3557 3575 :issue => {
3558 3576 :project_id => '2', :tracker_id => '', :assigned_to_id => '4',
3559 3577 :status_id => '1', :start_date => '2009-12-01', :due_date => '2009-12-31'
3560 3578 }
3561 3579 end
3562 3580 end
3563 3581
3564 3582 copied_issues = Issue.all(:limit => 2, :order => 'id desc', :conditions => {:project_id => 2})
3565 3583 assert_equal 2, copied_issues.size
3566 3584 copied_issues.each do |issue|
3567 3585 assert_equal 2, issue.project_id, "Project is incorrect"
3568 3586 assert_equal 4, issue.assigned_to_id, "Assigned to is incorrect"
3569 3587 assert_equal 1, issue.status_id, "Status is incorrect"
3570 3588 assert_equal '2009-12-01', issue.start_date.to_s, "Start date is incorrect"
3571 3589 assert_equal '2009-12-31', issue.due_date.to_s, "Due date is incorrect"
3572 3590 end
3573 3591 end
3574 3592
3575 3593 def test_bulk_copy_should_allow_adding_a_note
3576 3594 @request.session[:user_id] = 2
3577 3595 assert_difference 'Issue.count', 1 do
3578 3596 post :bulk_update, :ids => [1], :copy => '1',
3579 3597 :notes => 'Copying one issue',
3580 3598 :issue => {
3581 3599 :project_id => '', :tracker_id => '', :assigned_to_id => '4',
3582 3600 :status_id => '3', :start_date => '2009-12-01', :due_date => '2009-12-31'
3583 3601 }
3584 3602 end
3585 3603
3586 3604 issue = Issue.first(:order => 'id DESC')
3587 3605 assert_equal 1, issue.journals.size
3588 3606 journal = issue.journals.first
3589 3607 assert_equal 0, journal.details.size
3590 3608 assert_equal 'Copying one issue', journal.notes
3591 3609 end
3592 3610
3593 3611 def test_bulk_copy_should_allow_not_copying_the_attachments
3594 3612 attachment_count = Issue.find(3).attachments.size
3595 3613 assert attachment_count > 0
3596 3614 @request.session[:user_id] = 2
3597 3615
3598 3616 assert_difference 'Issue.count', 1 do
3599 3617 assert_no_difference 'Attachment.count' do
3600 3618 post :bulk_update, :ids => [3], :copy => '1',
3601 3619 :issue => {
3602 3620 :project_id => ''
3603 3621 }
3604 3622 end
3605 3623 end
3606 3624 end
3607 3625
3608 3626 def test_bulk_copy_should_allow_copying_the_attachments
3609 3627 attachment_count = Issue.find(3).attachments.size
3610 3628 assert attachment_count > 0
3611 3629 @request.session[:user_id] = 2
3612 3630
3613 3631 assert_difference 'Issue.count', 1 do
3614 3632 assert_difference 'Attachment.count', attachment_count do
3615 3633 post :bulk_update, :ids => [3], :copy => '1', :copy_attachments => '1',
3616 3634 :issue => {
3617 3635 :project_id => ''
3618 3636 }
3619 3637 end
3620 3638 end
3621 3639 end
3622 3640
3623 3641 def test_bulk_copy_should_add_relations_with_copied_issues
3624 3642 @request.session[:user_id] = 2
3625 3643
3626 3644 assert_difference 'Issue.count', 2 do
3627 3645 assert_difference 'IssueRelation.count', 2 do
3628 3646 post :bulk_update, :ids => [1, 3], :copy => '1',
3629 3647 :issue => {
3630 3648 :project_id => '1'
3631 3649 }
3632 3650 end
3633 3651 end
3634 3652 end
3635 3653
3636 3654 def test_bulk_copy_should_allow_not_copying_the_subtasks
3637 3655 issue = Issue.generate_with_descendants!
3638 3656 @request.session[:user_id] = 2
3639 3657
3640 3658 assert_difference 'Issue.count', 1 do
3641 3659 post :bulk_update, :ids => [issue.id], :copy => '1',
3642 3660 :issue => {
3643 3661 :project_id => ''
3644 3662 }
3645 3663 end
3646 3664 end
3647 3665
3648 3666 def test_bulk_copy_should_allow_copying_the_subtasks
3649 3667 issue = Issue.generate_with_descendants!
3650 3668 count = issue.descendants.count
3651 3669 @request.session[:user_id] = 2
3652 3670
3653 3671 assert_difference 'Issue.count', count+1 do
3654 3672 post :bulk_update, :ids => [issue.id], :copy => '1', :copy_subtasks => '1',
3655 3673 :issue => {
3656 3674 :project_id => ''
3657 3675 }
3658 3676 end
3659 3677 copy = Issue.where(:parent_id => nil).order("id DESC").first
3660 3678 assert_equal count, copy.descendants.count
3661 3679 end
3662 3680
3663 3681 def test_bulk_copy_should_not_copy_selected_subtasks_twice
3664 3682 issue = Issue.generate_with_descendants!
3665 3683 count = issue.descendants.count
3666 3684 @request.session[:user_id] = 2
3667 3685
3668 3686 assert_difference 'Issue.count', count+1 do
3669 3687 post :bulk_update, :ids => issue.self_and_descendants.map(&:id), :copy => '1', :copy_subtasks => '1',
3670 3688 :issue => {
3671 3689 :project_id => ''
3672 3690 }
3673 3691 end
3674 3692 copy = Issue.where(:parent_id => nil).order("id DESC").first
3675 3693 assert_equal count, copy.descendants.count
3676 3694 end
3677 3695
3678 3696 def test_bulk_copy_to_another_project_should_follow_when_needed
3679 3697 @request.session[:user_id] = 2
3680 3698 post :bulk_update, :ids => [1], :copy => '1', :issue => {:project_id => 2}, :follow => '1'
3681 3699 issue = Issue.first(:order => 'id DESC')
3682 3700 assert_redirected_to :controller => 'issues', :action => 'show', :id => issue
3683 3701 end
3684 3702
3685 3703 def test_destroy_issue_with_no_time_entries
3686 3704 assert_nil TimeEntry.find_by_issue_id(2)
3687 3705 @request.session[:user_id] = 2
3688 3706
3689 3707 assert_difference 'Issue.count', -1 do
3690 3708 delete :destroy, :id => 2
3691 3709 end
3692 3710 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
3693 3711 assert_nil Issue.find_by_id(2)
3694 3712 end
3695 3713
3696 3714 def test_destroy_issues_with_time_entries
3697 3715 @request.session[:user_id] = 2
3698 3716
3699 3717 assert_no_difference 'Issue.count' do
3700 3718 delete :destroy, :ids => [1, 3]
3701 3719 end
3702 3720 assert_response :success
3703 3721 assert_template 'destroy'
3704 3722 assert_not_nil assigns(:hours)
3705 3723 assert Issue.find_by_id(1) && Issue.find_by_id(3)
3706 3724 assert_tag 'form',
3707 3725 :descendant => {:tag => 'input', :attributes => {:name => '_method', :value => 'delete'}}
3708 3726 end
3709 3727
3710 3728 def test_destroy_issues_and_destroy_time_entries
3711 3729 @request.session[:user_id] = 2
3712 3730
3713 3731 assert_difference 'Issue.count', -2 do
3714 3732 assert_difference 'TimeEntry.count', -3 do
3715 3733 delete :destroy, :ids => [1, 3], :todo => 'destroy'
3716 3734 end
3717 3735 end
3718 3736 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
3719 3737 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
3720 3738 assert_nil TimeEntry.find_by_id([1, 2])
3721 3739 end
3722 3740
3723 3741 def test_destroy_issues_and_assign_time_entries_to_project
3724 3742 @request.session[:user_id] = 2
3725 3743
3726 3744 assert_difference 'Issue.count', -2 do
3727 3745 assert_no_difference 'TimeEntry.count' do
3728 3746 delete :destroy, :ids => [1, 3], :todo => 'nullify'
3729 3747 end
3730 3748 end
3731 3749 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
3732 3750 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
3733 3751 assert_nil TimeEntry.find(1).issue_id
3734 3752 assert_nil TimeEntry.find(2).issue_id
3735 3753 end
3736 3754
3737 3755 def test_destroy_issues_and_reassign_time_entries_to_another_issue
3738 3756 @request.session[:user_id] = 2
3739 3757
3740 3758 assert_difference 'Issue.count', -2 do
3741 3759 assert_no_difference 'TimeEntry.count' do
3742 3760 delete :destroy, :ids => [1, 3], :todo => 'reassign', :reassign_to_id => 2
3743 3761 end
3744 3762 end
3745 3763 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
3746 3764 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
3747 3765 assert_equal 2, TimeEntry.find(1).issue_id
3748 3766 assert_equal 2, TimeEntry.find(2).issue_id
3749 3767 end
3750 3768
3751 3769 def test_destroy_issues_from_different_projects
3752 3770 @request.session[:user_id] = 2
3753 3771
3754 3772 assert_difference 'Issue.count', -3 do
3755 3773 delete :destroy, :ids => [1, 2, 6], :todo => 'destroy'
3756 3774 end
3757 3775 assert_redirected_to :controller => 'issues', :action => 'index'
3758 3776 assert !(Issue.find_by_id(1) || Issue.find_by_id(2) || Issue.find_by_id(6))
3759 3777 end
3760 3778
3761 3779 def test_destroy_parent_and_child_issues
3762 3780 parent = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'Parent Issue')
3763 3781 child = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'Child Issue', :parent_issue_id => parent.id)
3764 3782 assert child.is_descendant_of?(parent.reload)
3765 3783
3766 3784 @request.session[:user_id] = 2
3767 3785 assert_difference 'Issue.count', -2 do
3768 3786 delete :destroy, :ids => [parent.id, child.id], :todo => 'destroy'
3769 3787 end
3770 3788 assert_response 302
3771 3789 end
3772 3790
3773 3791 def test_default_search_scope
3774 3792 get :index
3775 3793 assert_tag :div, :attributes => {:id => 'quick-search'},
3776 3794 :child => {:tag => 'form',
3777 3795 :child => {:tag => 'input', :attributes => {:name => 'issues', :type => 'hidden', :value => '1'}}}
3778 3796 end
3779 3797 end
@@ -1,1648 +1,1662
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, :journals, :journal_details,
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 teardown
35 35 User.current = nil
36 36 end
37 37
38 38 def test_create
39 39 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3,
40 40 :status_id => 1, :priority => IssuePriority.all.first,
41 41 :subject => 'test_create',
42 42 :description => 'IssueTest#test_create', :estimated_hours => '1:30')
43 43 assert issue.save
44 44 issue.reload
45 45 assert_equal 1.5, issue.estimated_hours
46 46 end
47 47
48 48 def test_create_minimal
49 49 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3,
50 50 :status_id => 1, :priority => IssuePriority.all.first,
51 51 :subject => 'test_create')
52 52 assert issue.save
53 53 assert issue.description.nil?
54 54 assert_nil issue.estimated_hours
55 55 end
56 56
57 57 def test_create_with_required_custom_field
58 58 set_language_if_valid 'en'
59 59 field = IssueCustomField.find_by_name('Database')
60 60 field.update_attribute(:is_required, true)
61 61
62 62 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1,
63 63 :status_id => 1, :subject => 'test_create',
64 64 :description => 'IssueTest#test_create_with_required_custom_field')
65 65 assert issue.available_custom_fields.include?(field)
66 66 # No value for the custom field
67 67 assert !issue.save
68 68 assert_equal ["Database can't be blank"], issue.errors.full_messages
69 69 # Blank value
70 70 issue.custom_field_values = { field.id => '' }
71 71 assert !issue.save
72 72 assert_equal ["Database can't be blank"], issue.errors.full_messages
73 73 # Invalid value
74 74 issue.custom_field_values = { field.id => 'SQLServer' }
75 75 assert !issue.save
76 76 assert_equal ["Database is not included in the list"], issue.errors.full_messages
77 77 # Valid value
78 78 issue.custom_field_values = { field.id => 'PostgreSQL' }
79 79 assert issue.save
80 80 issue.reload
81 81 assert_equal 'PostgreSQL', issue.custom_value_for(field).value
82 82 end
83 83
84 84 def test_create_with_group_assignment
85 85 with_settings :issue_group_assignment => '1' do
86 86 assert Issue.new(:project_id => 2, :tracker_id => 1, :author_id => 1,
87 87 :subject => 'Group assignment',
88 88 :assigned_to_id => 11).save
89 89 issue = Issue.first(:order => 'id DESC')
90 90 assert_kind_of Group, issue.assigned_to
91 91 assert_equal Group.find(11), issue.assigned_to
92 92 end
93 93 end
94 94
95 def test_create_with_parent_issue_id
96 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :subject => 'Group assignment', :parent_issue_id => 1)
97 assert_save issue
98 assert_equal 1, issue.parent_issue_id
99 assert_equal Issue.find(1), issue.parent
100 end
101
102 def test_create_with_invalid_parent_issue_id
103 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :subject => 'Group assignment', :parent_issue_id => '01ABC')
104 assert !issue.save
105 assert_equal '01ABC', issue.parent_issue_id
106 assert_include 'Parent task is invalid', issue.errors.full_messages
107 end
108
95 109 def assert_visibility_match(user, issues)
96 110 assert_equal issues.collect(&:id).sort, Issue.all.select {|issue| issue.visible?(user)}.collect(&:id).sort
97 111 end
98 112
99 113 def test_visible_scope_for_anonymous
100 114 # Anonymous user should see issues of public projects only
101 115 issues = Issue.visible(User.anonymous).all
102 116 assert issues.any?
103 117 assert_nil issues.detect {|issue| !issue.project.is_public?}
104 118 assert_nil issues.detect {|issue| issue.is_private?}
105 119 assert_visibility_match User.anonymous, issues
106 120 end
107 121
108 122 def test_visible_scope_for_anonymous_without_view_issues_permissions
109 123 # Anonymous user should not see issues without permission
110 124 Role.anonymous.remove_permission!(:view_issues)
111 125 issues = Issue.visible(User.anonymous).all
112 126 assert issues.empty?
113 127 assert_visibility_match User.anonymous, issues
114 128 end
115 129
116 130 def test_anonymous_should_not_see_private_issues_with_issues_visibility_set_to_default
117 131 assert Role.anonymous.update_attribute(:issues_visibility, 'default')
118 132 issue = Issue.generate!(:author => User.anonymous, :assigned_to => User.anonymous, :is_private => true)
119 133 assert_nil Issue.where(:id => issue.id).visible(User.anonymous).first
120 134 assert !issue.visible?(User.anonymous)
121 135 end
122 136
123 137 def test_anonymous_should_not_see_private_issues_with_issues_visibility_set_to_own
124 138 assert Role.anonymous.update_attribute(:issues_visibility, 'own')
125 139 issue = Issue.generate!(:author => User.anonymous, :assigned_to => User.anonymous, :is_private => true)
126 140 assert_nil Issue.where(:id => issue.id).visible(User.anonymous).first
127 141 assert !issue.visible?(User.anonymous)
128 142 end
129 143
130 144 def test_visible_scope_for_non_member
131 145 user = User.find(9)
132 146 assert user.projects.empty?
133 147 # Non member user should see issues of public projects only
134 148 issues = Issue.visible(user).all
135 149 assert issues.any?
136 150 assert_nil issues.detect {|issue| !issue.project.is_public?}
137 151 assert_nil issues.detect {|issue| issue.is_private?}
138 152 assert_visibility_match user, issues
139 153 end
140 154
141 155 def test_visible_scope_for_non_member_with_own_issues_visibility
142 156 Role.non_member.update_attribute :issues_visibility, 'own'
143 157 Issue.create!(:project_id => 1, :tracker_id => 1, :author_id => 9, :subject => 'Issue by non member')
144 158 user = User.find(9)
145 159
146 160 issues = Issue.visible(user).all
147 161 assert issues.any?
148 162 assert_nil issues.detect {|issue| issue.author != user}
149 163 assert_visibility_match user, issues
150 164 end
151 165
152 166 def test_visible_scope_for_non_member_without_view_issues_permissions
153 167 # Non member user should not see issues without permission
154 168 Role.non_member.remove_permission!(:view_issues)
155 169 user = User.find(9)
156 170 assert user.projects.empty?
157 171 issues = Issue.visible(user).all
158 172 assert issues.empty?
159 173 assert_visibility_match user, issues
160 174 end
161 175
162 176 def test_visible_scope_for_member
163 177 user = User.find(9)
164 178 # User should see issues of projects for which he has view_issues permissions only
165 179 Role.non_member.remove_permission!(:view_issues)
166 180 Member.create!(:principal => user, :project_id => 3, :role_ids => [2])
167 181 issues = Issue.visible(user).all
168 182 assert issues.any?
169 183 assert_nil issues.detect {|issue| issue.project_id != 3}
170 184 assert_nil issues.detect {|issue| issue.is_private?}
171 185 assert_visibility_match user, issues
172 186 end
173 187
174 188 def test_visible_scope_for_member_with_groups_should_return_assigned_issues
175 189 user = User.find(8)
176 190 assert user.groups.any?
177 191 Member.create!(:principal => user.groups.first, :project_id => 1, :role_ids => [2])
178 192 Role.non_member.remove_permission!(:view_issues)
179 193
180 194 issue = Issue.create(:project_id => 1, :tracker_id => 1, :author_id => 3,
181 195 :status_id => 1, :priority => IssuePriority.all.first,
182 196 :subject => 'Assignment test',
183 197 :assigned_to => user.groups.first,
184 198 :is_private => true)
185 199
186 200 Role.find(2).update_attribute :issues_visibility, 'default'
187 201 issues = Issue.visible(User.find(8)).all
188 202 assert issues.any?
189 203 assert issues.include?(issue)
190 204
191 205 Role.find(2).update_attribute :issues_visibility, 'own'
192 206 issues = Issue.visible(User.find(8)).all
193 207 assert issues.any?
194 208 assert issues.include?(issue)
195 209 end
196 210
197 211 def test_visible_scope_for_admin
198 212 user = User.find(1)
199 213 user.members.each(&:destroy)
200 214 assert user.projects.empty?
201 215 issues = Issue.visible(user).all
202 216 assert issues.any?
203 217 # Admin should see issues on private projects that he does not belong to
204 218 assert issues.detect {|issue| !issue.project.is_public?}
205 219 # Admin should see private issues of other users
206 220 assert issues.detect {|issue| issue.is_private? && issue.author != user}
207 221 assert_visibility_match user, issues
208 222 end
209 223
210 224 def test_visible_scope_with_project
211 225 project = Project.find(1)
212 226 issues = Issue.visible(User.find(2), :project => project).all
213 227 projects = issues.collect(&:project).uniq
214 228 assert_equal 1, projects.size
215 229 assert_equal project, projects.first
216 230 end
217 231
218 232 def test_visible_scope_with_project_and_subprojects
219 233 project = Project.find(1)
220 234 issues = Issue.visible(User.find(2), :project => project, :with_subprojects => true).all
221 235 projects = issues.collect(&:project).uniq
222 236 assert projects.size > 1
223 237 assert_equal [], projects.select {|p| !p.is_or_is_descendant_of?(project)}
224 238 end
225 239
226 240 def test_visible_and_nested_set_scopes
227 241 assert_equal 0, Issue.find(1).descendants.visible.all.size
228 242 end
229 243
230 244 def test_open_scope
231 245 issues = Issue.open.all
232 246 assert_nil issues.detect(&:closed?)
233 247 end
234 248
235 249 def test_open_scope_with_arg
236 250 issues = Issue.open(false).all
237 251 assert_equal issues, issues.select(&:closed?)
238 252 end
239 253
240 254 def test_errors_full_messages_should_include_custom_fields_errors
241 255 field = IssueCustomField.find_by_name('Database')
242 256
243 257 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1,
244 258 :status_id => 1, :subject => 'test_create',
245 259 :description => 'IssueTest#test_create_with_required_custom_field')
246 260 assert issue.available_custom_fields.include?(field)
247 261 # Invalid value
248 262 issue.custom_field_values = { field.id => 'SQLServer' }
249 263
250 264 assert !issue.valid?
251 265 assert_equal 1, issue.errors.full_messages.size
252 266 assert_equal "Database #{I18n.translate('activerecord.errors.messages.inclusion')}",
253 267 issue.errors.full_messages.first
254 268 end
255 269
256 270 def test_update_issue_with_required_custom_field
257 271 field = IssueCustomField.find_by_name('Database')
258 272 field.update_attribute(:is_required, true)
259 273
260 274 issue = Issue.find(1)
261 275 assert_nil issue.custom_value_for(field)
262 276 assert issue.available_custom_fields.include?(field)
263 277 # No change to custom values, issue can be saved
264 278 assert issue.save
265 279 # Blank value
266 280 issue.custom_field_values = { field.id => '' }
267 281 assert !issue.save
268 282 # Valid value
269 283 issue.custom_field_values = { field.id => 'PostgreSQL' }
270 284 assert issue.save
271 285 issue.reload
272 286 assert_equal 'PostgreSQL', issue.custom_value_for(field).value
273 287 end
274 288
275 289 def test_should_not_update_attributes_if_custom_fields_validation_fails
276 290 issue = Issue.find(1)
277 291 field = IssueCustomField.find_by_name('Database')
278 292 assert issue.available_custom_fields.include?(field)
279 293
280 294 issue.custom_field_values = { field.id => 'Invalid' }
281 295 issue.subject = 'Should be not be saved'
282 296 assert !issue.save
283 297
284 298 issue.reload
285 299 assert_equal "Can't print recipes", issue.subject
286 300 end
287 301
288 302 def test_should_not_recreate_custom_values_objects_on_update
289 303 field = IssueCustomField.find_by_name('Database')
290 304
291 305 issue = Issue.find(1)
292 306 issue.custom_field_values = { field.id => 'PostgreSQL' }
293 307 assert issue.save
294 308 custom_value = issue.custom_value_for(field)
295 309 issue.reload
296 310 issue.custom_field_values = { field.id => 'MySQL' }
297 311 assert issue.save
298 312 issue.reload
299 313 assert_equal custom_value.id, issue.custom_value_for(field).id
300 314 end
301 315
302 316 def test_should_not_update_custom_fields_on_changing_tracker_with_different_custom_fields
303 317 issue = Issue.create!(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :subject => 'Test', :custom_field_values => {'2' => 'Test'})
304 318 assert !Tracker.find(2).custom_field_ids.include?(2)
305 319
306 320 issue = Issue.find(issue.id)
307 321 issue.attributes = {:tracker_id => 2, :custom_field_values => {'1' => ''}}
308 322
309 323 issue = Issue.find(issue.id)
310 324 custom_value = issue.custom_value_for(2)
311 325 assert_not_nil custom_value
312 326 assert_equal 'Test', custom_value.value
313 327 end
314 328
315 329 def test_assigning_tracker_id_should_reload_custom_fields_values
316 330 issue = Issue.new(:project => Project.find(1))
317 331 assert issue.custom_field_values.empty?
318 332 issue.tracker_id = 1
319 333 assert issue.custom_field_values.any?
320 334 end
321 335
322 336 def test_assigning_attributes_should_assign_project_and_tracker_first
323 337 seq = sequence('seq')
324 338 issue = Issue.new
325 339 issue.expects(:project_id=).in_sequence(seq)
326 340 issue.expects(:tracker_id=).in_sequence(seq)
327 341 issue.expects(:subject=).in_sequence(seq)
328 342 issue.attributes = {:tracker_id => 2, :project_id => 1, :subject => 'Test'}
329 343 end
330 344
331 345 def test_assigning_tracker_and_custom_fields_should_assign_custom_fields
332 346 attributes = ActiveSupport::OrderedHash.new
333 347 attributes['custom_field_values'] = { '1' => 'MySQL' }
334 348 attributes['tracker_id'] = '1'
335 349 issue = Issue.new(:project => Project.find(1))
336 350 issue.attributes = attributes
337 351 assert_equal 'MySQL', issue.custom_field_value(1)
338 352 end
339 353
340 354 def test_should_update_issue_with_disabled_tracker
341 355 p = Project.find(1)
342 356 issue = Issue.find(1)
343 357
344 358 p.trackers.delete(issue.tracker)
345 359 assert !p.trackers.include?(issue.tracker)
346 360
347 361 issue.reload
348 362 issue.subject = 'New subject'
349 363 assert issue.save
350 364 end
351 365
352 366 def test_should_not_set_a_disabled_tracker
353 367 p = Project.find(1)
354 368 p.trackers.delete(Tracker.find(2))
355 369
356 370 issue = Issue.find(1)
357 371 issue.tracker_id = 2
358 372 issue.subject = 'New subject'
359 373 assert !issue.save
360 374 assert_not_nil issue.errors[:tracker_id]
361 375 end
362 376
363 377 def test_category_based_assignment
364 378 issue = Issue.create(:project_id => 1, :tracker_id => 1, :author_id => 3,
365 379 :status_id => 1, :priority => IssuePriority.all.first,
366 380 :subject => 'Assignment test',
367 381 :description => 'Assignment test', :category_id => 1)
368 382 assert_equal IssueCategory.find(1).assigned_to, issue.assigned_to
369 383 end
370 384
371 385 def test_new_statuses_allowed_to
372 386 WorkflowTransition.delete_all
373 387
374 388 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 2, :author => false, :assignee => false)
375 389 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 3, :author => true, :assignee => false)
376 390 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 4, :author => false, :assignee => true)
377 391 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 5, :author => true, :assignee => true)
378 392 status = IssueStatus.find(1)
379 393 role = Role.find(1)
380 394 tracker = Tracker.find(1)
381 395 user = User.find(2)
382 396
383 397 issue = Issue.generate!(:tracker => tracker, :status => status, :project_id => 1, :author_id => 1)
384 398 assert_equal [1, 2], issue.new_statuses_allowed_to(user).map(&:id)
385 399
386 400 issue = Issue.generate!(:tracker => tracker, :status => status, :project_id => 1, :author => user)
387 401 assert_equal [1, 2, 3, 5], issue.new_statuses_allowed_to(user).map(&:id)
388 402
389 403 issue = Issue.generate!(:tracker => tracker, :status => status, :project_id => 1, :author_id => 1, :assigned_to => user)
390 404 assert_equal [1, 2, 4, 5], issue.new_statuses_allowed_to(user).map(&:id)
391 405
392 406 issue = Issue.generate!(:tracker => tracker, :status => status, :project_id => 1, :author => user, :assigned_to => user)
393 407 assert_equal [1, 2, 3, 4, 5], issue.new_statuses_allowed_to(user).map(&:id)
394 408 end
395 409
396 410 def test_new_statuses_allowed_to_should_return_all_transitions_for_admin
397 411 admin = User.find(1)
398 412 issue = Issue.find(1)
399 413 assert !admin.member_of?(issue.project)
400 414 expected_statuses = [issue.status] + WorkflowTransition.find_all_by_old_status_id(issue.status_id).map(&:new_status).uniq.sort
401 415
402 416 assert_equal expected_statuses, issue.new_statuses_allowed_to(admin)
403 417 end
404 418
405 419 def test_new_statuses_allowed_to_should_return_default_and_current_status_when_copying
406 420 issue = Issue.find(1).copy
407 421 assert_equal [1], issue.new_statuses_allowed_to(User.find(2)).map(&:id)
408 422
409 423 issue = Issue.find(2).copy
410 424 assert_equal [1, 2], issue.new_statuses_allowed_to(User.find(2)).map(&:id)
411 425 end
412 426
413 427 def test_safe_attributes_names_should_not_include_disabled_field
414 428 tracker = Tracker.new(:core_fields => %w(assigned_to_id fixed_version_id))
415 429
416 430 issue = Issue.new(:tracker => tracker)
417 431 assert_include 'tracker_id', issue.safe_attribute_names
418 432 assert_include 'status_id', issue.safe_attribute_names
419 433 assert_include 'subject', issue.safe_attribute_names
420 434 assert_include 'description', issue.safe_attribute_names
421 435 assert_include 'custom_field_values', issue.safe_attribute_names
422 436 assert_include 'custom_fields', issue.safe_attribute_names
423 437 assert_include 'lock_version', issue.safe_attribute_names
424 438
425 439 tracker.core_fields.each do |field|
426 440 assert_include field, issue.safe_attribute_names
427 441 end
428 442
429 443 tracker.disabled_core_fields.each do |field|
430 444 assert_not_include field, issue.safe_attribute_names
431 445 end
432 446 end
433 447
434 448 def test_safe_attributes_should_ignore_disabled_fields
435 449 tracker = Tracker.find(1)
436 450 tracker.core_fields = %w(assigned_to_id due_date)
437 451 tracker.save!
438 452
439 453 issue = Issue.new(:tracker => tracker)
440 454 issue.safe_attributes = {'start_date' => '2012-07-14', 'due_date' => '2012-07-14'}
441 455 assert_nil issue.start_date
442 456 assert_equal Date.parse('2012-07-14'), issue.due_date
443 457 end
444 458
445 459 def test_safe_attributes_should_accept_target_tracker_enabled_fields
446 460 source = Tracker.find(1)
447 461 source.core_fields = []
448 462 source.save!
449 463 target = Tracker.find(2)
450 464 target.core_fields = %w(assigned_to_id due_date)
451 465 target.save!
452 466
453 467 issue = Issue.new(:tracker => source)
454 468 issue.safe_attributes = {'tracker_id' => 2, 'due_date' => '2012-07-14'}
455 469 assert_equal target, issue.tracker
456 470 assert_equal Date.parse('2012-07-14'), issue.due_date
457 471 end
458 472
459 473 def test_safe_attributes_should_not_include_readonly_fields
460 474 WorkflowPermission.delete_all
461 475 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => 'due_date', :rule => 'readonly')
462 476 user = User.find(2)
463 477
464 478 issue = Issue.new(:project_id => 1, :tracker_id => 1)
465 479 assert_equal %w(due_date), issue.read_only_attribute_names(user)
466 480 assert_not_include 'due_date', issue.safe_attribute_names(user)
467 481
468 482 issue.send :safe_attributes=, {'start_date' => '2012-07-14', 'due_date' => '2012-07-14'}, user
469 483 assert_equal Date.parse('2012-07-14'), issue.start_date
470 484 assert_nil issue.due_date
471 485 end
472 486
473 487 def test_safe_attributes_should_not_include_readonly_custom_fields
474 488 cf1 = IssueCustomField.create!(:name => 'Writable field', :field_format => 'string', :is_for_all => true, :tracker_ids => [1])
475 489 cf2 = IssueCustomField.create!(:name => 'Readonly field', :field_format => 'string', :is_for_all => true, :tracker_ids => [1])
476 490
477 491 WorkflowPermission.delete_all
478 492 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => cf2.id.to_s, :rule => 'readonly')
479 493 user = User.find(2)
480 494
481 495 issue = Issue.new(:project_id => 1, :tracker_id => 1)
482 496 assert_equal [cf2.id.to_s], issue.read_only_attribute_names(user)
483 497 assert_not_include cf2.id.to_s, issue.safe_attribute_names(user)
484 498
485 499 issue.send :safe_attributes=, {'custom_field_values' => {cf1.id.to_s => 'value1', cf2.id.to_s => 'value2'}}, user
486 500 assert_equal 'value1', issue.custom_field_value(cf1)
487 501 assert_nil issue.custom_field_value(cf2)
488 502
489 503 issue.send :safe_attributes=, {'custom_fields' => [{'id' => cf1.id.to_s, 'value' => 'valuea'}, {'id' => cf2.id.to_s, 'value' => 'valueb'}]}, user
490 504 assert_equal 'valuea', issue.custom_field_value(cf1)
491 505 assert_nil issue.custom_field_value(cf2)
492 506 end
493 507
494 508 def test_editable_custom_field_values_should_return_non_readonly_custom_values
495 509 cf1 = IssueCustomField.create!(:name => 'Writable field', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
496 510 cf2 = IssueCustomField.create!(:name => 'Readonly field', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
497 511
498 512 WorkflowPermission.delete_all
499 513 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => cf2.id.to_s, :rule => 'readonly')
500 514 user = User.find(2)
501 515
502 516 issue = Issue.new(:project_id => 1, :tracker_id => 1)
503 517 values = issue.editable_custom_field_values(user)
504 518 assert values.detect {|value| value.custom_field == cf1}
505 519 assert_nil values.detect {|value| value.custom_field == cf2}
506 520
507 521 issue.tracker_id = 2
508 522 values = issue.editable_custom_field_values(user)
509 523 assert values.detect {|value| value.custom_field == cf1}
510 524 assert values.detect {|value| value.custom_field == cf2}
511 525 end
512 526
513 527 def test_safe_attributes_should_accept_target_tracker_writable_fields
514 528 WorkflowPermission.delete_all
515 529 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => 'due_date', :rule => 'readonly')
516 530 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2, :role_id => 1, :field_name => 'start_date', :rule => 'readonly')
517 531 user = User.find(2)
518 532
519 533 issue = Issue.new(:project_id => 1, :tracker_id => 1, :status_id => 1)
520 534
521 535 issue.send :safe_attributes=, {'start_date' => '2012-07-12', 'due_date' => '2012-07-14'}, user
522 536 assert_equal Date.parse('2012-07-12'), issue.start_date
523 537 assert_nil issue.due_date
524 538
525 539 issue.send :safe_attributes=, {'start_date' => '2012-07-15', 'due_date' => '2012-07-16', 'tracker_id' => 2}, user
526 540 assert_equal Date.parse('2012-07-12'), issue.start_date
527 541 assert_equal Date.parse('2012-07-16'), issue.due_date
528 542 end
529 543
530 544 def test_safe_attributes_should_accept_target_status_writable_fields
531 545 WorkflowPermission.delete_all
532 546 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => 'due_date', :rule => 'readonly')
533 547 WorkflowPermission.create!(:old_status_id => 2, :tracker_id => 1, :role_id => 1, :field_name => 'start_date', :rule => 'readonly')
534 548 user = User.find(2)
535 549
536 550 issue = Issue.new(:project_id => 1, :tracker_id => 1, :status_id => 1)
537 551
538 552 issue.send :safe_attributes=, {'start_date' => '2012-07-12', 'due_date' => '2012-07-14'}, user
539 553 assert_equal Date.parse('2012-07-12'), issue.start_date
540 554 assert_nil issue.due_date
541 555
542 556 issue.send :safe_attributes=, {'start_date' => '2012-07-15', 'due_date' => '2012-07-16', 'status_id' => 2}, user
543 557 assert_equal Date.parse('2012-07-12'), issue.start_date
544 558 assert_equal Date.parse('2012-07-16'), issue.due_date
545 559 end
546 560
547 561 def test_required_attributes_should_be_validated
548 562 cf = IssueCustomField.create!(:name => 'Foo', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
549 563
550 564 WorkflowPermission.delete_all
551 565 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => 'due_date', :rule => 'required')
552 566 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => 'category_id', :rule => 'required')
553 567 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => cf.id.to_s, :rule => 'required')
554 568
555 569 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2, :role_id => 1, :field_name => 'start_date', :rule => 'required')
556 570 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2, :role_id => 1, :field_name => cf.id.to_s, :rule => 'required')
557 571 user = User.find(2)
558 572
559 573 issue = Issue.new(:project_id => 1, :tracker_id => 1, :status_id => 1, :subject => 'Required fields', :author => user)
560 574 assert_equal [cf.id.to_s, "category_id", "due_date"], issue.required_attribute_names(user).sort
561 575 assert !issue.save, "Issue was saved"
562 576 assert_equal ["Category can't be blank", "Due date can't be blank", "Foo can't be blank"], issue.errors.full_messages.sort
563 577
564 578 issue.tracker_id = 2
565 579 assert_equal [cf.id.to_s, "start_date"], issue.required_attribute_names(user).sort
566 580 assert !issue.save, "Issue was saved"
567 581 assert_equal ["Foo can't be blank", "Start date can't be blank"], issue.errors.full_messages.sort
568 582
569 583 issue.start_date = Date.today
570 584 issue.custom_field_values = {cf.id.to_s => 'bar'}
571 585 assert issue.save
572 586 end
573 587
574 588 def test_required_attribute_names_for_multiple_roles_should_intersect_rules
575 589 WorkflowPermission.delete_all
576 590 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => 'due_date', :rule => 'required')
577 591 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => 'start_date', :rule => 'required')
578 592 user = User.find(2)
579 593 member = Member.find(1)
580 594 issue = Issue.new(:project_id => 1, :tracker_id => 1, :status_id => 1)
581 595
582 596 assert_equal %w(due_date start_date), issue.required_attribute_names(user).sort
583 597
584 598 member.role_ids = [1, 2]
585 599 member.save!
586 600 assert_equal [], issue.required_attribute_names(user.reload)
587 601
588 602 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 2, :field_name => 'due_date', :rule => 'required')
589 603 assert_equal %w(due_date), issue.required_attribute_names(user)
590 604
591 605 member.role_ids = [1, 2, 3]
592 606 member.save!
593 607 assert_equal [], issue.required_attribute_names(user.reload)
594 608
595 609 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 2, :field_name => 'due_date', :rule => 'readonly')
596 610 # required + readonly => required
597 611 assert_equal %w(due_date), issue.required_attribute_names(user)
598 612 end
599 613
600 614 def test_read_only_attribute_names_for_multiple_roles_should_intersect_rules
601 615 WorkflowPermission.delete_all
602 616 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => 'due_date', :rule => 'readonly')
603 617 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => 'start_date', :rule => 'readonly')
604 618 user = User.find(2)
605 619 member = Member.find(1)
606 620 issue = Issue.new(:project_id => 1, :tracker_id => 1, :status_id => 1)
607 621
608 622 assert_equal %w(due_date start_date), issue.read_only_attribute_names(user).sort
609 623
610 624 member.role_ids = [1, 2]
611 625 member.save!
612 626 assert_equal [], issue.read_only_attribute_names(user.reload)
613 627
614 628 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 2, :field_name => 'due_date', :rule => 'readonly')
615 629 assert_equal %w(due_date), issue.read_only_attribute_names(user)
616 630 end
617 631
618 632 def test_copy
619 633 issue = Issue.new.copy_from(1)
620 634 assert issue.copy?
621 635 assert issue.save
622 636 issue.reload
623 637 orig = Issue.find(1)
624 638 assert_equal orig.subject, issue.subject
625 639 assert_equal orig.tracker, issue.tracker
626 640 assert_equal "125", issue.custom_value_for(2).value
627 641 end
628 642
629 643 def test_copy_should_copy_status
630 644 orig = Issue.find(8)
631 645 assert orig.status != IssueStatus.default
632 646
633 647 issue = Issue.new.copy_from(orig)
634 648 assert issue.save
635 649 issue.reload
636 650 assert_equal orig.status, issue.status
637 651 end
638 652
639 653 def test_copy_should_add_relation_with_copied_issue
640 654 copied = Issue.find(1)
641 655 issue = Issue.new.copy_from(copied)
642 656 assert issue.save
643 657 issue.reload
644 658
645 659 assert_equal 1, issue.relations.size
646 660 relation = issue.relations.first
647 661 assert_equal 'copied_to', relation.relation_type
648 662 assert_equal copied, relation.issue_from
649 663 assert_equal issue, relation.issue_to
650 664 end
651 665
652 666 def test_copy_should_copy_subtasks
653 667 issue = Issue.generate_with_descendants!
654 668
655 669 copy = issue.reload.copy
656 670 copy.author = User.find(7)
657 671 assert_difference 'Issue.count', 1+issue.descendants.count do
658 672 assert copy.save
659 673 end
660 674 copy.reload
661 675 assert_equal %w(Child1 Child2), copy.children.map(&:subject).sort
662 676 child_copy = copy.children.detect {|c| c.subject == 'Child1'}
663 677 assert_equal %w(Child11), child_copy.children.map(&:subject).sort
664 678 assert_equal copy.author, child_copy.author
665 679 end
666 680
667 681 def test_copy_should_copy_subtasks_to_target_project
668 682 issue = Issue.generate_with_descendants!
669 683
670 684 copy = issue.copy(:project_id => 3)
671 685 assert_difference 'Issue.count', 1+issue.descendants.count do
672 686 assert copy.save
673 687 end
674 688 assert_equal [3], copy.reload.descendants.map(&:project_id).uniq
675 689 end
676 690
677 691 def test_copy_should_not_copy_subtasks_twice_when_saving_twice
678 692 issue = Issue.generate_with_descendants!
679 693
680 694 copy = issue.reload.copy
681 695 assert_difference 'Issue.count', 1+issue.descendants.count do
682 696 assert copy.save
683 697 assert copy.save
684 698 end
685 699 end
686 700
687 701 def test_should_not_call_after_project_change_on_creation
688 702 issue = Issue.new(:project_id => 1, :tracker_id => 1, :status_id => 1, :subject => 'Test', :author_id => 1)
689 703 issue.expects(:after_project_change).never
690 704 issue.save!
691 705 end
692 706
693 707 def test_should_not_call_after_project_change_on_update
694 708 issue = Issue.find(1)
695 709 issue.project = Project.find(1)
696 710 issue.subject = 'No project change'
697 711 issue.expects(:after_project_change).never
698 712 issue.save!
699 713 end
700 714
701 715 def test_should_call_after_project_change_on_project_change
702 716 issue = Issue.find(1)
703 717 issue.project = Project.find(2)
704 718 issue.expects(:after_project_change).once
705 719 issue.save!
706 720 end
707 721
708 722 def test_adding_journal_should_update_timestamp
709 723 issue = Issue.find(1)
710 724 updated_on_was = issue.updated_on
711 725
712 726 issue.init_journal(User.first, "Adding notes")
713 727 assert_difference 'Journal.count' do
714 728 assert issue.save
715 729 end
716 730 issue.reload
717 731
718 732 assert_not_equal updated_on_was, issue.updated_on
719 733 end
720 734
721 735 def test_should_close_duplicates
722 736 # Create 3 issues
723 737 issue1 = Issue.generate!
724 738 issue2 = Issue.generate!
725 739 issue3 = Issue.generate!
726 740
727 741 # 2 is a dupe of 1
728 742 IssueRelation.create!(:issue_from => issue2, :issue_to => issue1, :relation_type => IssueRelation::TYPE_DUPLICATES)
729 743 # And 3 is a dupe of 2
730 744 IssueRelation.create!(:issue_from => issue3, :issue_to => issue2, :relation_type => IssueRelation::TYPE_DUPLICATES)
731 745 # And 3 is a dupe of 1 (circular duplicates)
732 746 IssueRelation.create!(:issue_from => issue3, :issue_to => issue1, :relation_type => IssueRelation::TYPE_DUPLICATES)
733 747
734 748 assert issue1.reload.duplicates.include?(issue2)
735 749
736 750 # Closing issue 1
737 751 issue1.init_journal(User.find(:first), "Closing issue1")
738 752 issue1.status = IssueStatus.find :first, :conditions => {:is_closed => true}
739 753 assert issue1.save
740 754 # 2 and 3 should be also closed
741 755 assert issue2.reload.closed?
742 756 assert issue3.reload.closed?
743 757 end
744 758
745 759 def test_should_not_close_duplicated_issue
746 760 issue1 = Issue.generate!
747 761 issue2 = Issue.generate!
748 762
749 763 # 2 is a dupe of 1
750 764 IssueRelation.create(:issue_from => issue2, :issue_to => issue1, :relation_type => IssueRelation::TYPE_DUPLICATES)
751 765 # 2 is a dup of 1 but 1 is not a duplicate of 2
752 766 assert !issue2.reload.duplicates.include?(issue1)
753 767
754 768 # Closing issue 2
755 769 issue2.init_journal(User.find(:first), "Closing issue2")
756 770 issue2.status = IssueStatus.find :first, :conditions => {:is_closed => true}
757 771 assert issue2.save
758 772 # 1 should not be also closed
759 773 assert !issue1.reload.closed?
760 774 end
761 775
762 776 def test_assignable_versions
763 777 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :fixed_version_id => 1, :subject => 'New issue')
764 778 assert_equal ['open'], issue.assignable_versions.collect(&:status).uniq
765 779 end
766 780
767 781 def test_should_not_be_able_to_assign_a_new_issue_to_a_closed_version
768 782 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :fixed_version_id => 1, :subject => 'New issue')
769 783 assert !issue.save
770 784 assert_not_nil issue.errors[:fixed_version_id]
771 785 end
772 786
773 787 def test_should_not_be_able_to_assign_a_new_issue_to_a_locked_version
774 788 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :fixed_version_id => 2, :subject => 'New issue')
775 789 assert !issue.save
776 790 assert_not_nil issue.errors[:fixed_version_id]
777 791 end
778 792
779 793 def test_should_be_able_to_assign_a_new_issue_to_an_open_version
780 794 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :fixed_version_id => 3, :subject => 'New issue')
781 795 assert issue.save
782 796 end
783 797
784 798 def test_should_be_able_to_update_an_issue_assigned_to_a_closed_version
785 799 issue = Issue.find(11)
786 800 assert_equal 'closed', issue.fixed_version.status
787 801 issue.subject = 'Subject changed'
788 802 assert issue.save
789 803 end
790 804
791 805 def test_should_not_be_able_to_reopen_an_issue_assigned_to_a_closed_version
792 806 issue = Issue.find(11)
793 807 issue.status_id = 1
794 808 assert !issue.save
795 809 assert_not_nil issue.errors[:base]
796 810 end
797 811
798 812 def test_should_be_able_to_reopen_and_reassign_an_issue_assigned_to_a_closed_version
799 813 issue = Issue.find(11)
800 814 issue.status_id = 1
801 815 issue.fixed_version_id = 3
802 816 assert issue.save
803 817 end
804 818
805 819 def test_should_be_able_to_reopen_an_issue_assigned_to_a_locked_version
806 820 issue = Issue.find(12)
807 821 assert_equal 'locked', issue.fixed_version.status
808 822 issue.status_id = 1
809 823 assert issue.save
810 824 end
811 825
812 826 def test_should_not_be_able_to_keep_unshared_version_when_changing_project
813 827 issue = Issue.find(2)
814 828 assert_equal 2, issue.fixed_version_id
815 829 issue.project_id = 3
816 830 assert_nil issue.fixed_version_id
817 831 issue.fixed_version_id = 2
818 832 assert !issue.save
819 833 assert_include 'Target version is not included in the list', issue.errors.full_messages
820 834 end
821 835
822 836 def test_should_keep_shared_version_when_changing_project
823 837 Version.find(2).update_attribute :sharing, 'tree'
824 838
825 839 issue = Issue.find(2)
826 840 assert_equal 2, issue.fixed_version_id
827 841 issue.project_id = 3
828 842 assert_equal 2, issue.fixed_version_id
829 843 assert issue.save
830 844 end
831 845
832 846 def test_allowed_target_projects_on_move_should_include_projects_with_issue_tracking_enabled
833 847 assert_include Project.find(2), Issue.allowed_target_projects_on_move(User.find(2))
834 848 end
835 849
836 850 def test_allowed_target_projects_on_move_should_not_include_projects_with_issue_tracking_disabled
837 851 Project.find(2).disable_module! :issue_tracking
838 852 assert_not_include Project.find(2), Issue.allowed_target_projects_on_move(User.find(2))
839 853 end
840 854
841 855 def test_move_to_another_project_with_same_category
842 856 issue = Issue.find(1)
843 857 issue.project = Project.find(2)
844 858 assert issue.save
845 859 issue.reload
846 860 assert_equal 2, issue.project_id
847 861 # Category changes
848 862 assert_equal 4, issue.category_id
849 863 # Make sure time entries were move to the target project
850 864 assert_equal 2, issue.time_entries.first.project_id
851 865 end
852 866
853 867 def test_move_to_another_project_without_same_category
854 868 issue = Issue.find(2)
855 869 issue.project = Project.find(2)
856 870 assert issue.save
857 871 issue.reload
858 872 assert_equal 2, issue.project_id
859 873 # Category cleared
860 874 assert_nil issue.category_id
861 875 end
862 876
863 877 def test_move_to_another_project_should_clear_fixed_version_when_not_shared
864 878 issue = Issue.find(1)
865 879 issue.update_attribute(:fixed_version_id, 1)
866 880 issue.project = Project.find(2)
867 881 assert issue.save
868 882 issue.reload
869 883 assert_equal 2, issue.project_id
870 884 # Cleared fixed_version
871 885 assert_equal nil, issue.fixed_version
872 886 end
873 887
874 888 def test_move_to_another_project_should_keep_fixed_version_when_shared_with_the_target_project
875 889 issue = Issue.find(1)
876 890 issue.update_attribute(:fixed_version_id, 4)
877 891 issue.project = Project.find(5)
878 892 assert issue.save
879 893 issue.reload
880 894 assert_equal 5, issue.project_id
881 895 # Keep fixed_version
882 896 assert_equal 4, issue.fixed_version_id
883 897 end
884 898
885 899 def test_move_to_another_project_should_clear_fixed_version_when_not_shared_with_the_target_project
886 900 issue = Issue.find(1)
887 901 issue.update_attribute(:fixed_version_id, 1)
888 902 issue.project = Project.find(5)
889 903 assert issue.save
890 904 issue.reload
891 905 assert_equal 5, issue.project_id
892 906 # Cleared fixed_version
893 907 assert_equal nil, issue.fixed_version
894 908 end
895 909
896 910 def test_move_to_another_project_should_keep_fixed_version_when_shared_systemwide
897 911 issue = Issue.find(1)
898 912 issue.update_attribute(:fixed_version_id, 7)
899 913 issue.project = Project.find(2)
900 914 assert issue.save
901 915 issue.reload
902 916 assert_equal 2, issue.project_id
903 917 # Keep fixed_version
904 918 assert_equal 7, issue.fixed_version_id
905 919 end
906 920
907 921 def test_move_to_another_project_should_keep_parent_if_valid
908 922 issue = Issue.find(1)
909 923 issue.update_attribute(:parent_issue_id, 2)
910 924 issue.project = Project.find(3)
911 925 assert issue.save
912 926 issue.reload
913 927 assert_equal 2, issue.parent_id
914 928 end
915 929
916 930 def test_move_to_another_project_should_clear_parent_if_not_valid
917 931 issue = Issue.find(1)
918 932 issue.update_attribute(:parent_issue_id, 2)
919 933 issue.project = Project.find(2)
920 934 assert issue.save
921 935 issue.reload
922 936 assert_nil issue.parent_id
923 937 end
924 938
925 939 def test_move_to_another_project_with_disabled_tracker
926 940 issue = Issue.find(1)
927 941 target = Project.find(2)
928 942 target.tracker_ids = [3]
929 943 target.save
930 944 issue.project = target
931 945 assert issue.save
932 946 issue.reload
933 947 assert_equal 2, issue.project_id
934 948 assert_equal 3, issue.tracker_id
935 949 end
936 950
937 951 def test_copy_to_the_same_project
938 952 issue = Issue.find(1)
939 953 copy = issue.copy
940 954 assert_difference 'Issue.count' do
941 955 copy.save!
942 956 end
943 957 assert_kind_of Issue, copy
944 958 assert_equal issue.project, copy.project
945 959 assert_equal "125", copy.custom_value_for(2).value
946 960 end
947 961
948 962 def test_copy_to_another_project_and_tracker
949 963 issue = Issue.find(1)
950 964 copy = issue.copy(:project_id => 3, :tracker_id => 2)
951 965 assert_difference 'Issue.count' do
952 966 copy.save!
953 967 end
954 968 copy.reload
955 969 assert_kind_of Issue, copy
956 970 assert_equal Project.find(3), copy.project
957 971 assert_equal Tracker.find(2), copy.tracker
958 972 # Custom field #2 is not associated with target tracker
959 973 assert_nil copy.custom_value_for(2)
960 974 end
961 975
962 976 context "#copy" do
963 977 setup do
964 978 @issue = Issue.find(1)
965 979 end
966 980
967 981 should "not create a journal" do
968 982 copy = @issue.copy(:project_id => 3, :tracker_id => 2, :assigned_to_id => 3)
969 983 copy.save!
970 984 assert_equal 0, copy.reload.journals.size
971 985 end
972 986
973 987 should "allow assigned_to changes" do
974 988 copy = @issue.copy(:project_id => 3, :tracker_id => 2, :assigned_to_id => 3)
975 989 assert_equal 3, copy.assigned_to_id
976 990 end
977 991
978 992 should "allow status changes" do
979 993 copy = @issue.copy(:project_id => 3, :tracker_id => 2, :status_id => 2)
980 994 assert_equal 2, copy.status_id
981 995 end
982 996
983 997 should "allow start date changes" do
984 998 date = Date.today
985 999 copy = @issue.copy(:project_id => 3, :tracker_id => 2, :start_date => date)
986 1000 assert_equal date, copy.start_date
987 1001 end
988 1002
989 1003 should "allow due date changes" do
990 1004 date = Date.today
991 1005 copy = @issue.copy(:project_id => 3, :tracker_id => 2, :due_date => date)
992 1006 assert_equal date, copy.due_date
993 1007 end
994 1008
995 1009 should "set current user as author" do
996 1010 User.current = User.find(9)
997 1011 copy = @issue.copy(:project_id => 3, :tracker_id => 2)
998 1012 assert_equal User.current, copy.author
999 1013 end
1000 1014
1001 1015 should "create a journal with notes" do
1002 1016 date = Date.today
1003 1017 notes = "Notes added when copying"
1004 1018 copy = @issue.copy(:project_id => 3, :tracker_id => 2, :start_date => date)
1005 1019 copy.init_journal(User.current, notes)
1006 1020 copy.save!
1007 1021
1008 1022 assert_equal 1, copy.journals.size
1009 1023 journal = copy.journals.first
1010 1024 assert_equal 0, journal.details.size
1011 1025 assert_equal notes, journal.notes
1012 1026 end
1013 1027 end
1014 1028
1015 1029 def test_valid_parent_project
1016 1030 issue = Issue.find(1)
1017 1031 issue_in_same_project = Issue.find(2)
1018 1032 issue_in_child_project = Issue.find(5)
1019 1033 issue_in_grandchild_project = Issue.generate!(:project_id => 6, :tracker_id => 1)
1020 1034 issue_in_other_child_project = Issue.find(6)
1021 1035 issue_in_different_tree = Issue.find(4)
1022 1036
1023 1037 with_settings :cross_project_subtasks => '' do
1024 1038 assert_equal true, issue.valid_parent_project?(issue_in_same_project)
1025 1039 assert_equal false, issue.valid_parent_project?(issue_in_child_project)
1026 1040 assert_equal false, issue.valid_parent_project?(issue_in_grandchild_project)
1027 1041 assert_equal false, issue.valid_parent_project?(issue_in_different_tree)
1028 1042 end
1029 1043
1030 1044 with_settings :cross_project_subtasks => 'system' do
1031 1045 assert_equal true, issue.valid_parent_project?(issue_in_same_project)
1032 1046 assert_equal true, issue.valid_parent_project?(issue_in_child_project)
1033 1047 assert_equal true, issue.valid_parent_project?(issue_in_different_tree)
1034 1048 end
1035 1049
1036 1050 with_settings :cross_project_subtasks => 'tree' do
1037 1051 assert_equal true, issue.valid_parent_project?(issue_in_same_project)
1038 1052 assert_equal true, issue.valid_parent_project?(issue_in_child_project)
1039 1053 assert_equal true, issue.valid_parent_project?(issue_in_grandchild_project)
1040 1054 assert_equal false, issue.valid_parent_project?(issue_in_different_tree)
1041 1055
1042 1056 assert_equal true, issue_in_child_project.valid_parent_project?(issue_in_same_project)
1043 1057 assert_equal true, issue_in_child_project.valid_parent_project?(issue_in_other_child_project)
1044 1058 end
1045 1059
1046 1060 with_settings :cross_project_subtasks => 'descendants' do
1047 1061 assert_equal true, issue.valid_parent_project?(issue_in_same_project)
1048 1062 assert_equal false, issue.valid_parent_project?(issue_in_child_project)
1049 1063 assert_equal false, issue.valid_parent_project?(issue_in_grandchild_project)
1050 1064 assert_equal false, issue.valid_parent_project?(issue_in_different_tree)
1051 1065
1052 1066 assert_equal true, issue_in_child_project.valid_parent_project?(issue)
1053 1067 assert_equal false, issue_in_child_project.valid_parent_project?(issue_in_other_child_project)
1054 1068 end
1055 1069 end
1056 1070
1057 1071 def test_recipients_should_include_previous_assignee
1058 1072 user = User.find(3)
1059 1073 user.members.update_all ["mail_notification = ?", false]
1060 1074 user.update_attribute :mail_notification, 'only_assigned'
1061 1075
1062 1076 issue = Issue.find(2)
1063 1077 issue.assigned_to = nil
1064 1078 assert_include user.mail, issue.recipients
1065 1079 issue.save!
1066 1080 assert !issue.recipients.include?(user.mail)
1067 1081 end
1068 1082
1069 1083 def test_recipients_should_not_include_users_that_cannot_view_the_issue
1070 1084 issue = Issue.find(12)
1071 1085 assert issue.recipients.include?(issue.author.mail)
1072 1086 # copy the issue to a private project
1073 1087 copy = issue.copy(:project_id => 5, :tracker_id => 2)
1074 1088 # author is not a member of project anymore
1075 1089 assert !copy.recipients.include?(copy.author.mail)
1076 1090 end
1077 1091
1078 1092 def test_recipients_should_include_the_assigned_group_members
1079 1093 group_member = User.generate!
1080 1094 group = Group.generate!
1081 1095 group.users << group_member
1082 1096
1083 1097 issue = Issue.find(12)
1084 1098 issue.assigned_to = group
1085 1099 assert issue.recipients.include?(group_member.mail)
1086 1100 end
1087 1101
1088 1102 def test_watcher_recipients_should_not_include_users_that_cannot_view_the_issue
1089 1103 user = User.find(3)
1090 1104 issue = Issue.find(9)
1091 1105 Watcher.create!(:user => user, :watchable => issue)
1092 1106 assert issue.watched_by?(user)
1093 1107 assert !issue.watcher_recipients.include?(user.mail)
1094 1108 end
1095 1109
1096 1110 def test_issue_destroy
1097 1111 Issue.find(1).destroy
1098 1112 assert_nil Issue.find_by_id(1)
1099 1113 assert_nil TimeEntry.find_by_issue_id(1)
1100 1114 end
1101 1115
1102 1116 def test_destroying_a_deleted_issue_should_not_raise_an_error
1103 1117 issue = Issue.find(1)
1104 1118 Issue.find(1).destroy
1105 1119
1106 1120 assert_nothing_raised do
1107 1121 assert_no_difference 'Issue.count' do
1108 1122 issue.destroy
1109 1123 end
1110 1124 assert issue.destroyed?
1111 1125 end
1112 1126 end
1113 1127
1114 1128 def test_destroying_a_stale_issue_should_not_raise_an_error
1115 1129 issue = Issue.find(1)
1116 1130 Issue.find(1).update_attribute :subject, "Updated"
1117 1131
1118 1132 assert_nothing_raised do
1119 1133 assert_difference 'Issue.count', -1 do
1120 1134 issue.destroy
1121 1135 end
1122 1136 assert issue.destroyed?
1123 1137 end
1124 1138 end
1125 1139
1126 1140 def test_blocked
1127 1141 blocked_issue = Issue.find(9)
1128 1142 blocking_issue = Issue.find(10)
1129 1143
1130 1144 assert blocked_issue.blocked?
1131 1145 assert !blocking_issue.blocked?
1132 1146 end
1133 1147
1134 1148 def test_blocked_issues_dont_allow_closed_statuses
1135 1149 blocked_issue = Issue.find(9)
1136 1150
1137 1151 allowed_statuses = blocked_issue.new_statuses_allowed_to(users(:users_002))
1138 1152 assert !allowed_statuses.empty?
1139 1153 closed_statuses = allowed_statuses.select {|st| st.is_closed?}
1140 1154 assert closed_statuses.empty?
1141 1155 end
1142 1156
1143 1157 def test_unblocked_issues_allow_closed_statuses
1144 1158 blocking_issue = Issue.find(10)
1145 1159
1146 1160 allowed_statuses = blocking_issue.new_statuses_allowed_to(users(:users_002))
1147 1161 assert !allowed_statuses.empty?
1148 1162 closed_statuses = allowed_statuses.select {|st| st.is_closed?}
1149 1163 assert !closed_statuses.empty?
1150 1164 end
1151 1165
1152 1166 def test_rescheduling_an_issue_should_reschedule_following_issue
1153 1167 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)
1154 1168 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)
1155 1169 IssueRelation.create!(:issue_from => issue1, :issue_to => issue2, :relation_type => IssueRelation::TYPE_PRECEDES)
1156 1170 assert_equal issue1.due_date + 1, issue2.reload.start_date
1157 1171
1158 1172 issue1.due_date = Date.today + 5
1159 1173 issue1.save!
1160 1174 assert_equal issue1.due_date + 1, issue2.reload.start_date
1161 1175 end
1162 1176
1163 1177 def test_rescheduling_a_stale_issue_should_not_raise_an_error
1164 1178 stale = Issue.find(1)
1165 1179 issue = Issue.find(1)
1166 1180 issue.subject = "Updated"
1167 1181 issue.save!
1168 1182
1169 1183 date = 10.days.from_now.to_date
1170 1184 assert_nothing_raised do
1171 1185 stale.reschedule_after(date)
1172 1186 end
1173 1187 assert_equal date, stale.reload.start_date
1174 1188 end
1175 1189
1176 1190 def test_overdue
1177 1191 assert Issue.new(:due_date => 1.day.ago.to_date).overdue?
1178 1192 assert !Issue.new(:due_date => Date.today).overdue?
1179 1193 assert !Issue.new(:due_date => 1.day.from_now.to_date).overdue?
1180 1194 assert !Issue.new(:due_date => nil).overdue?
1181 1195 assert !Issue.new(:due_date => 1.day.ago.to_date, :status => IssueStatus.find(:first, :conditions => {:is_closed => true})).overdue?
1182 1196 end
1183 1197
1184 1198 context "#behind_schedule?" do
1185 1199 should "be false if the issue has no start_date" do
1186 1200 assert !Issue.new(:start_date => nil, :due_date => 1.day.from_now.to_date, :done_ratio => 0).behind_schedule?
1187 1201 end
1188 1202
1189 1203 should "be false if the issue has no end_date" do
1190 1204 assert !Issue.new(:start_date => 1.day.from_now.to_date, :due_date => nil, :done_ratio => 0).behind_schedule?
1191 1205 end
1192 1206
1193 1207 should "be false if the issue has more done than it's calendar time" do
1194 1208 assert !Issue.new(:start_date => 50.days.ago.to_date, :due_date => 50.days.from_now.to_date, :done_ratio => 90).behind_schedule?
1195 1209 end
1196 1210
1197 1211 should "be true if the issue hasn't been started at all" do
1198 1212 assert Issue.new(:start_date => 1.day.ago.to_date, :due_date => 1.day.from_now.to_date, :done_ratio => 0).behind_schedule?
1199 1213 end
1200 1214
1201 1215 should "be true if the issue has used more calendar time than it's done ratio" do
1202 1216 assert Issue.new(:start_date => 100.days.ago.to_date, :due_date => Date.today, :done_ratio => 90).behind_schedule?
1203 1217 end
1204 1218 end
1205 1219
1206 1220 context "#assignable_users" do
1207 1221 should "be Users" do
1208 1222 assert_kind_of User, Issue.find(1).assignable_users.first
1209 1223 end
1210 1224
1211 1225 should "include the issue author" do
1212 1226 non_project_member = User.generate!
1213 1227 issue = Issue.generate!(:author => non_project_member)
1214 1228
1215 1229 assert issue.assignable_users.include?(non_project_member)
1216 1230 end
1217 1231
1218 1232 should "include the current assignee" do
1219 1233 user = User.generate!
1220 1234 issue = Issue.generate!(:assigned_to => user)
1221 1235 user.lock!
1222 1236
1223 1237 assert Issue.find(issue.id).assignable_users.include?(user)
1224 1238 end
1225 1239
1226 1240 should "not show the issue author twice" do
1227 1241 assignable_user_ids = Issue.find(1).assignable_users.collect(&:id)
1228 1242 assert_equal 2, assignable_user_ids.length
1229 1243
1230 1244 assignable_user_ids.each do |user_id|
1231 1245 assert_equal 1, assignable_user_ids.select {|i| i == user_id}.length, "User #{user_id} appears more or less than once"
1232 1246 end
1233 1247 end
1234 1248
1235 1249 context "with issue_group_assignment" do
1236 1250 should "include groups" do
1237 1251 issue = Issue.new(:project => Project.find(2))
1238 1252
1239 1253 with_settings :issue_group_assignment => '1' do
1240 1254 assert_equal %w(Group User), issue.assignable_users.map {|a| a.class.name}.uniq.sort
1241 1255 assert issue.assignable_users.include?(Group.find(11))
1242 1256 end
1243 1257 end
1244 1258 end
1245 1259
1246 1260 context "without issue_group_assignment" do
1247 1261 should "not include groups" do
1248 1262 issue = Issue.new(:project => Project.find(2))
1249 1263
1250 1264 with_settings :issue_group_assignment => '0' do
1251 1265 assert_equal %w(User), issue.assignable_users.map {|a| a.class.name}.uniq.sort
1252 1266 assert !issue.assignable_users.include?(Group.find(11))
1253 1267 end
1254 1268 end
1255 1269 end
1256 1270 end
1257 1271
1258 1272 def test_create_should_send_email_notification
1259 1273 ActionMailer::Base.deliveries.clear
1260 1274 issue = Issue.new(:project_id => 1, :tracker_id => 1,
1261 1275 :author_id => 3, :status_id => 1,
1262 1276 :priority => IssuePriority.all.first,
1263 1277 :subject => 'test_create', :estimated_hours => '1:30')
1264 1278
1265 1279 assert issue.save
1266 1280 assert_equal 1, ActionMailer::Base.deliveries.size
1267 1281 end
1268 1282
1269 1283 def test_stale_issue_should_not_send_email_notification
1270 1284 ActionMailer::Base.deliveries.clear
1271 1285 issue = Issue.find(1)
1272 1286 stale = Issue.find(1)
1273 1287
1274 1288 issue.init_journal(User.find(1))
1275 1289 issue.subject = 'Subjet update'
1276 1290 assert issue.save
1277 1291 assert_equal 1, ActionMailer::Base.deliveries.size
1278 1292 ActionMailer::Base.deliveries.clear
1279 1293
1280 1294 stale.init_journal(User.find(1))
1281 1295 stale.subject = 'Another subjet update'
1282 1296 assert_raise ActiveRecord::StaleObjectError do
1283 1297 stale.save
1284 1298 end
1285 1299 assert ActionMailer::Base.deliveries.empty?
1286 1300 end
1287 1301
1288 1302 def test_journalized_description
1289 1303 IssueCustomField.delete_all
1290 1304
1291 1305 i = Issue.first
1292 1306 old_description = i.description
1293 1307 new_description = "This is the new description"
1294 1308
1295 1309 i.init_journal(User.find(2))
1296 1310 i.description = new_description
1297 1311 assert_difference 'Journal.count', 1 do
1298 1312 assert_difference 'JournalDetail.count', 1 do
1299 1313 i.save!
1300 1314 end
1301 1315 end
1302 1316
1303 1317 detail = JournalDetail.first(:order => 'id DESC')
1304 1318 assert_equal i, detail.journal.journalized
1305 1319 assert_equal 'attr', detail.property
1306 1320 assert_equal 'description', detail.prop_key
1307 1321 assert_equal old_description, detail.old_value
1308 1322 assert_equal new_description, detail.value
1309 1323 end
1310 1324
1311 1325 def test_blank_descriptions_should_not_be_journalized
1312 1326 IssueCustomField.delete_all
1313 1327 Issue.update_all("description = NULL", "id=1")
1314 1328
1315 1329 i = Issue.find(1)
1316 1330 i.init_journal(User.find(2))
1317 1331 i.subject = "blank description"
1318 1332 i.description = "\r\n"
1319 1333
1320 1334 assert_difference 'Journal.count', 1 do
1321 1335 assert_difference 'JournalDetail.count', 1 do
1322 1336 i.save!
1323 1337 end
1324 1338 end
1325 1339 end
1326 1340
1327 1341 def test_journalized_multi_custom_field
1328 1342 field = IssueCustomField.create!(:name => 'filter', :field_format => 'list', :is_filter => true, :is_for_all => true,
1329 1343 :tracker_ids => [1], :possible_values => ['value1', 'value2', 'value3'], :multiple => true)
1330 1344
1331 1345 issue = Issue.create!(:project_id => 1, :tracker_id => 1, :subject => 'Test', :author_id => 1)
1332 1346
1333 1347 assert_difference 'Journal.count' do
1334 1348 assert_difference 'JournalDetail.count' do
1335 1349 issue.init_journal(User.first)
1336 1350 issue.custom_field_values = {field.id => ['value1']}
1337 1351 issue.save!
1338 1352 end
1339 1353 assert_difference 'JournalDetail.count' do
1340 1354 issue.init_journal(User.first)
1341 1355 issue.custom_field_values = {field.id => ['value1', 'value2']}
1342 1356 issue.save!
1343 1357 end
1344 1358 assert_difference 'JournalDetail.count', 2 do
1345 1359 issue.init_journal(User.first)
1346 1360 issue.custom_field_values = {field.id => ['value3', 'value2']}
1347 1361 issue.save!
1348 1362 end
1349 1363 assert_difference 'JournalDetail.count', 2 do
1350 1364 issue.init_journal(User.first)
1351 1365 issue.custom_field_values = {field.id => nil}
1352 1366 issue.save!
1353 1367 end
1354 1368 end
1355 1369 end
1356 1370
1357 1371 def test_description_eol_should_be_normalized
1358 1372 i = Issue.new(:description => "CR \r LF \n CRLF \r\n")
1359 1373 assert_equal "CR \r\n LF \r\n CRLF \r\n", i.description
1360 1374 end
1361 1375
1362 1376 def test_saving_twice_should_not_duplicate_journal_details
1363 1377 i = Issue.find(:first)
1364 1378 i.init_journal(User.find(2), 'Some notes')
1365 1379 # initial changes
1366 1380 i.subject = 'New subject'
1367 1381 i.done_ratio = i.done_ratio + 10
1368 1382 assert_difference 'Journal.count' do
1369 1383 assert i.save
1370 1384 end
1371 1385 # 1 more change
1372 1386 i.priority = IssuePriority.find(:first, :conditions => ["id <> ?", i.priority_id])
1373 1387 assert_no_difference 'Journal.count' do
1374 1388 assert_difference 'JournalDetail.count', 1 do
1375 1389 i.save
1376 1390 end
1377 1391 end
1378 1392 # no more change
1379 1393 assert_no_difference 'Journal.count' do
1380 1394 assert_no_difference 'JournalDetail.count' do
1381 1395 i.save
1382 1396 end
1383 1397 end
1384 1398 end
1385 1399
1386 1400 def test_all_dependent_issues
1387 1401 IssueRelation.delete_all
1388 1402 assert IssueRelation.create!(:issue_from => Issue.find(1),
1389 1403 :issue_to => Issue.find(2),
1390 1404 :relation_type => IssueRelation::TYPE_PRECEDES)
1391 1405 assert IssueRelation.create!(:issue_from => Issue.find(2),
1392 1406 :issue_to => Issue.find(3),
1393 1407 :relation_type => IssueRelation::TYPE_PRECEDES)
1394 1408 assert IssueRelation.create!(:issue_from => Issue.find(3),
1395 1409 :issue_to => Issue.find(8),
1396 1410 :relation_type => IssueRelation::TYPE_PRECEDES)
1397 1411
1398 1412 assert_equal [2, 3, 8], Issue.find(1).all_dependent_issues.collect(&:id).sort
1399 1413 end
1400 1414
1401 1415 def test_all_dependent_issues_with_persistent_circular_dependency
1402 1416 IssueRelation.delete_all
1403 1417 assert IssueRelation.create!(:issue_from => Issue.find(1),
1404 1418 :issue_to => Issue.find(2),
1405 1419 :relation_type => IssueRelation::TYPE_PRECEDES)
1406 1420 assert IssueRelation.create!(:issue_from => Issue.find(2),
1407 1421 :issue_to => Issue.find(3),
1408 1422 :relation_type => IssueRelation::TYPE_PRECEDES)
1409 1423
1410 1424 r = IssueRelation.create!(:issue_from => Issue.find(3),
1411 1425 :issue_to => Issue.find(7),
1412 1426 :relation_type => IssueRelation::TYPE_PRECEDES)
1413 1427 IssueRelation.update_all("issue_to_id = 1", ["id = ?", r.id])
1414 1428
1415 1429 assert_equal [2, 3], Issue.find(1).all_dependent_issues.collect(&:id).sort
1416 1430 end
1417 1431
1418 1432 def test_all_dependent_issues_with_persistent_multiple_circular_dependencies
1419 1433 IssueRelation.delete_all
1420 1434 assert IssueRelation.create!(:issue_from => Issue.find(1),
1421 1435 :issue_to => Issue.find(2),
1422 1436 :relation_type => IssueRelation::TYPE_RELATES)
1423 1437 assert IssueRelation.create!(:issue_from => Issue.find(2),
1424 1438 :issue_to => Issue.find(3),
1425 1439 :relation_type => IssueRelation::TYPE_RELATES)
1426 1440 assert IssueRelation.create!(:issue_from => Issue.find(3),
1427 1441 :issue_to => Issue.find(8),
1428 1442 :relation_type => IssueRelation::TYPE_RELATES)
1429 1443
1430 1444 r = IssueRelation.create!(:issue_from => Issue.find(8),
1431 1445 :issue_to => Issue.find(7),
1432 1446 :relation_type => IssueRelation::TYPE_RELATES)
1433 1447 IssueRelation.update_all("issue_to_id = 2", ["id = ?", r.id])
1434 1448
1435 1449 r = IssueRelation.create!(:issue_from => Issue.find(3),
1436 1450 :issue_to => Issue.find(7),
1437 1451 :relation_type => IssueRelation::TYPE_RELATES)
1438 1452 IssueRelation.update_all("issue_to_id = 1", ["id = ?", r.id])
1439 1453
1440 1454 assert_equal [2, 3, 8], Issue.find(1).all_dependent_issues.collect(&:id).sort
1441 1455 end
1442 1456
1443 1457 context "#done_ratio" do
1444 1458 setup do
1445 1459 @issue = Issue.find(1)
1446 1460 @issue_status = IssueStatus.find(1)
1447 1461 @issue_status.update_attribute(:default_done_ratio, 50)
1448 1462 @issue2 = Issue.find(2)
1449 1463 @issue_status2 = IssueStatus.find(2)
1450 1464 @issue_status2.update_attribute(:default_done_ratio, 0)
1451 1465 end
1452 1466
1453 1467 teardown do
1454 1468 Setting.issue_done_ratio = 'issue_field'
1455 1469 end
1456 1470
1457 1471 context "with Setting.issue_done_ratio using the issue_field" do
1458 1472 setup do
1459 1473 Setting.issue_done_ratio = 'issue_field'
1460 1474 end
1461 1475
1462 1476 should "read the issue's field" do
1463 1477 assert_equal 0, @issue.done_ratio
1464 1478 assert_equal 30, @issue2.done_ratio
1465 1479 end
1466 1480 end
1467 1481
1468 1482 context "with Setting.issue_done_ratio using the issue_status" do
1469 1483 setup do
1470 1484 Setting.issue_done_ratio = 'issue_status'
1471 1485 end
1472 1486
1473 1487 should "read the Issue Status's default done ratio" do
1474 1488 assert_equal 50, @issue.done_ratio
1475 1489 assert_equal 0, @issue2.done_ratio
1476 1490 end
1477 1491 end
1478 1492 end
1479 1493
1480 1494 context "#update_done_ratio_from_issue_status" do
1481 1495 setup do
1482 1496 @issue = Issue.find(1)
1483 1497 @issue_status = IssueStatus.find(1)
1484 1498 @issue_status.update_attribute(:default_done_ratio, 50)
1485 1499 @issue2 = Issue.find(2)
1486 1500 @issue_status2 = IssueStatus.find(2)
1487 1501 @issue_status2.update_attribute(:default_done_ratio, 0)
1488 1502 end
1489 1503
1490 1504 context "with Setting.issue_done_ratio using the issue_field" do
1491 1505 setup do
1492 1506 Setting.issue_done_ratio = 'issue_field'
1493 1507 end
1494 1508
1495 1509 should "not change the issue" do
1496 1510 @issue.update_done_ratio_from_issue_status
1497 1511 @issue2.update_done_ratio_from_issue_status
1498 1512
1499 1513 assert_equal 0, @issue.read_attribute(:done_ratio)
1500 1514 assert_equal 30, @issue2.read_attribute(:done_ratio)
1501 1515 end
1502 1516 end
1503 1517
1504 1518 context "with Setting.issue_done_ratio using the issue_status" do
1505 1519 setup do
1506 1520 Setting.issue_done_ratio = 'issue_status'
1507 1521 end
1508 1522
1509 1523 should "change the issue's done ratio" do
1510 1524 @issue.update_done_ratio_from_issue_status
1511 1525 @issue2.update_done_ratio_from_issue_status
1512 1526
1513 1527 assert_equal 50, @issue.read_attribute(:done_ratio)
1514 1528 assert_equal 0, @issue2.read_attribute(:done_ratio)
1515 1529 end
1516 1530 end
1517 1531 end
1518 1532
1519 1533 test "#by_tracker" do
1520 1534 User.current = User.anonymous
1521 1535 groups = Issue.by_tracker(Project.find(1))
1522 1536 assert_equal 3, groups.size
1523 1537 assert_equal 7, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1524 1538 end
1525 1539
1526 1540 test "#by_version" do
1527 1541 User.current = User.anonymous
1528 1542 groups = Issue.by_version(Project.find(1))
1529 1543 assert_equal 3, groups.size
1530 1544 assert_equal 3, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1531 1545 end
1532 1546
1533 1547 test "#by_priority" do
1534 1548 User.current = User.anonymous
1535 1549 groups = Issue.by_priority(Project.find(1))
1536 1550 assert_equal 4, groups.size
1537 1551 assert_equal 7, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1538 1552 end
1539 1553
1540 1554 test "#by_category" do
1541 1555 User.current = User.anonymous
1542 1556 groups = Issue.by_category(Project.find(1))
1543 1557 assert_equal 2, groups.size
1544 1558 assert_equal 3, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1545 1559 end
1546 1560
1547 1561 test "#by_assigned_to" do
1548 1562 User.current = User.anonymous
1549 1563 groups = Issue.by_assigned_to(Project.find(1))
1550 1564 assert_equal 2, groups.size
1551 1565 assert_equal 2, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1552 1566 end
1553 1567
1554 1568 test "#by_author" do
1555 1569 User.current = User.anonymous
1556 1570 groups = Issue.by_author(Project.find(1))
1557 1571 assert_equal 4, groups.size
1558 1572 assert_equal 7, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1559 1573 end
1560 1574
1561 1575 test "#by_subproject" do
1562 1576 User.current = User.anonymous
1563 1577 groups = Issue.by_subproject(Project.find(1))
1564 1578 # Private descendant not visible
1565 1579 assert_equal 1, groups.size
1566 1580 assert_equal 2, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1567 1581 end
1568 1582
1569 1583 def test_recently_updated_scope
1570 1584 #should return the last updated issue
1571 1585 assert_equal Issue.reorder("updated_on DESC").first, Issue.recently_updated.limit(1).first
1572 1586 end
1573 1587
1574 1588 def test_on_active_projects_scope
1575 1589 assert Project.find(2).archive
1576 1590
1577 1591 before = Issue.on_active_project.length
1578 1592 # test inclusion to results
1579 1593 issue = Issue.generate!(:tracker => Project.find(2).trackers.first)
1580 1594 assert_equal before + 1, Issue.on_active_project.length
1581 1595
1582 1596 # Move to an archived project
1583 1597 issue.project = Project.find(2)
1584 1598 assert issue.save
1585 1599 assert_equal before, Issue.on_active_project.length
1586 1600 end
1587 1601
1588 1602 context "Issue#recipients" do
1589 1603 setup do
1590 1604 @project = Project.find(1)
1591 1605 @author = User.generate!
1592 1606 @assignee = User.generate!
1593 1607 @issue = Issue.generate!(:project => @project, :assigned_to => @assignee, :author => @author)
1594 1608 end
1595 1609
1596 1610 should "include project recipients" do
1597 1611 assert @project.recipients.present?
1598 1612 @project.recipients.each do |project_recipient|
1599 1613 assert @issue.recipients.include?(project_recipient)
1600 1614 end
1601 1615 end
1602 1616
1603 1617 should "include the author if the author is active" do
1604 1618 assert @issue.author, "No author set for Issue"
1605 1619 assert @issue.recipients.include?(@issue.author.mail)
1606 1620 end
1607 1621
1608 1622 should "include the assigned to user if the assigned to user is active" do
1609 1623 assert @issue.assigned_to, "No assigned_to set for Issue"
1610 1624 assert @issue.recipients.include?(@issue.assigned_to.mail)
1611 1625 end
1612 1626
1613 1627 should "not include users who opt out of all email" do
1614 1628 @author.update_attribute(:mail_notification, :none)
1615 1629
1616 1630 assert !@issue.recipients.include?(@issue.author.mail)
1617 1631 end
1618 1632
1619 1633 should "not include the issue author if they are only notified of assigned issues" do
1620 1634 @author.update_attribute(:mail_notification, :only_assigned)
1621 1635
1622 1636 assert !@issue.recipients.include?(@issue.author.mail)
1623 1637 end
1624 1638
1625 1639 should "not include the assigned user if they are only notified of owned issues" do
1626 1640 @assignee.update_attribute(:mail_notification, :only_owner)
1627 1641
1628 1642 assert !@issue.recipients.include?(@issue.assigned_to.mail)
1629 1643 end
1630 1644 end
1631 1645
1632 1646 def test_last_journal_id_with_journals_should_return_the_journal_id
1633 1647 assert_equal 2, Issue.find(1).last_journal_id
1634 1648 end
1635 1649
1636 1650 def test_last_journal_id_without_journals_should_return_nil
1637 1651 assert_nil Issue.find(3).last_journal_id
1638 1652 end
1639 1653
1640 1654 def test_journals_after_should_return_journals_with_greater_id
1641 1655 assert_equal [Journal.find(2)], Issue.find(1).journals_after('1')
1642 1656 assert_equal [], Issue.find(1).journals_after('2')
1643 1657 end
1644 1658
1645 1659 def test_journals_after_with_blank_arg_should_return_all_journals
1646 1660 assert_equal [Journal.find(1), Journal.find(2)], Issue.find(1).journals_after('')
1647 1661 end
1648 1662 end
General Comments 0
You need to be logged in to leave comments. Login now