comparison .svn/pristine/a7/a7c522fa48845e70c7a7bc89a951a0055cb1f96f.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents 622f24f53b42
children
comparison
equal deleted inserted replaced
1297:0a574315af3e 1298:4f746d8966dd
1 # Redmine - project management software
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 require File.expand_path('../../test_helper', __FILE__)
19
20 class IssueTest < ActiveSupport::TestCase
21 fixtures :projects, :users, :members, :member_roles, :roles,
22 :groups_users,
23 :trackers, :projects_trackers,
24 :enabled_modules,
25 :versions,
26 :issue_statuses, :issue_categories, :issue_relations, :workflows,
27 :enumerations,
28 :issues, :journals, :journal_details,
29 :custom_fields, :custom_fields_projects, :custom_fields_trackers, :custom_values,
30 :time_entries
31
32 include Redmine::I18n
33
34 def teardown
35 User.current = nil
36 end
37
38 def test_initialize
39 issue = Issue.new
40
41 assert_nil issue.project_id
42 assert_nil issue.tracker_id
43 assert_nil issue.author_id
44 assert_nil issue.assigned_to_id
45 assert_nil issue.category_id
46
47 assert_equal IssueStatus.default, issue.status
48 assert_equal IssuePriority.default, issue.priority
49 end
50
51 def test_create
52 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3,
53 :status_id => 1, :priority => IssuePriority.all.first,
54 :subject => 'test_create',
55 :description => 'IssueTest#test_create', :estimated_hours => '1:30')
56 assert issue.save
57 issue.reload
58 assert_equal 1.5, issue.estimated_hours
59 end
60
61 def test_create_minimal
62 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3,
63 :status_id => 1, :priority => IssuePriority.all.first,
64 :subject => 'test_create')
65 assert issue.save
66 assert issue.description.nil?
67 assert_nil issue.estimated_hours
68 end
69
70 def test_start_date_format_should_be_validated
71 set_language_if_valid 'en'
72 ['2012', 'ABC', '2012-15-20'].each do |invalid_date|
73 issue = Issue.new(:start_date => invalid_date)
74 assert !issue.valid?
75 assert_include 'Start date is not a valid date', issue.errors.full_messages, "No error found for invalid date #{invalid_date}"
76 end
77 end
78
79 def test_due_date_format_should_be_validated
80 set_language_if_valid 'en'
81 ['2012', 'ABC', '2012-15-20'].each do |invalid_date|
82 issue = Issue.new(:due_date => invalid_date)
83 assert !issue.valid?
84 assert_include 'Due date is not a valid date', issue.errors.full_messages, "No error found for invalid date #{invalid_date}"
85 end
86 end
87
88 def test_due_date_lesser_than_start_date_should_not_validate
89 set_language_if_valid 'en'
90 issue = Issue.new(:start_date => '2012-10-06', :due_date => '2012-10-02')
91 assert !issue.valid?
92 assert_include 'Due date must be greater than start date', issue.errors.full_messages
93 end
94
95 def test_estimated_hours_should_be_validated
96 set_language_if_valid 'en'
97 ['-2'].each do |invalid|
98 issue = Issue.new(:estimated_hours => invalid)
99 assert !issue.valid?
100 assert_include 'Estimated time is invalid', issue.errors.full_messages
101 end
102 end
103
104 def test_create_with_required_custom_field
105 set_language_if_valid 'en'
106 field = IssueCustomField.find_by_name('Database')
107 field.update_attribute(:is_required, true)
108
109 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1,
110 :status_id => 1, :subject => 'test_create',
111 :description => 'IssueTest#test_create_with_required_custom_field')
112 assert issue.available_custom_fields.include?(field)
113 # No value for the custom field
114 assert !issue.save
115 assert_equal ["Database can't be blank"], issue.errors.full_messages
116 # Blank value
117 issue.custom_field_values = { field.id => '' }
118 assert !issue.save
119 assert_equal ["Database can't be blank"], issue.errors.full_messages
120 # Invalid value
121 issue.custom_field_values = { field.id => 'SQLServer' }
122 assert !issue.save
123 assert_equal ["Database is not included in the list"], issue.errors.full_messages
124 # Valid value
125 issue.custom_field_values = { field.id => 'PostgreSQL' }
126 assert issue.save
127 issue.reload
128 assert_equal 'PostgreSQL', issue.custom_value_for(field).value
129 end
130
131 def test_create_with_group_assignment
132 with_settings :issue_group_assignment => '1' do
133 assert Issue.new(:project_id => 2, :tracker_id => 1, :author_id => 1,
134 :subject => 'Group assignment',
135 :assigned_to_id => 11).save
136 issue = Issue.first(:order => 'id DESC')
137 assert_kind_of Group, issue.assigned_to
138 assert_equal Group.find(11), issue.assigned_to
139 end
140 end
141
142 def test_create_with_parent_issue_id
143 issue = Issue.new(:project_id => 1, :tracker_id => 1,
144 :author_id => 1, :subject => 'Group assignment',
145 :parent_issue_id => 1)
146 assert_save issue
147 assert_equal 1, issue.parent_issue_id
148 assert_equal Issue.find(1), issue.parent
149 end
150
151 def test_create_with_sharp_parent_issue_id
152 issue = Issue.new(:project_id => 1, :tracker_id => 1,
153 :author_id => 1, :subject => 'Group assignment',
154 :parent_issue_id => "#1")
155 assert_save issue
156 assert_equal 1, issue.parent_issue_id
157 assert_equal Issue.find(1), issue.parent
158 end
159
160 def test_create_with_invalid_parent_issue_id
161 set_language_if_valid 'en'
162 issue = Issue.new(:project_id => 1, :tracker_id => 1,
163 :author_id => 1, :subject => 'Group assignment',
164 :parent_issue_id => '01ABC')
165 assert !issue.save
166 assert_equal '01ABC', issue.parent_issue_id
167 assert_include 'Parent task is invalid', issue.errors.full_messages
168 end
169
170 def test_create_with_invalid_sharp_parent_issue_id
171 set_language_if_valid 'en'
172 issue = Issue.new(:project_id => 1, :tracker_id => 1,
173 :author_id => 1, :subject => 'Group assignment',
174 :parent_issue_id => '#01ABC')
175 assert !issue.save
176 assert_equal '#01ABC', issue.parent_issue_id
177 assert_include 'Parent task is invalid', issue.errors.full_messages
178 end
179
180 def assert_visibility_match(user, issues)
181 assert_equal issues.collect(&:id).sort, Issue.all.select {|issue| issue.visible?(user)}.collect(&:id).sort
182 end
183
184 def test_visible_scope_for_anonymous
185 # Anonymous user should see issues of public projects only
186 issues = Issue.visible(User.anonymous).all
187 assert issues.any?
188 assert_nil issues.detect {|issue| !issue.project.is_public?}
189 assert_nil issues.detect {|issue| issue.is_private?}
190 assert_visibility_match User.anonymous, issues
191 end
192
193 def test_visible_scope_for_anonymous_without_view_issues_permissions
194 # Anonymous user should not see issues without permission
195 Role.anonymous.remove_permission!(:view_issues)
196 issues = Issue.visible(User.anonymous).all
197 assert issues.empty?
198 assert_visibility_match User.anonymous, issues
199 end
200
201 def test_anonymous_should_not_see_private_issues_with_issues_visibility_set_to_default
202 assert Role.anonymous.update_attribute(:issues_visibility, 'default')
203 issue = Issue.generate!(:author => User.anonymous, :assigned_to => User.anonymous, :is_private => true)
204 assert_nil Issue.where(:id => issue.id).visible(User.anonymous).first
205 assert !issue.visible?(User.anonymous)
206 end
207
208 def test_anonymous_should_not_see_private_issues_with_issues_visibility_set_to_own
209 assert Role.anonymous.update_attribute(:issues_visibility, 'own')
210 issue = Issue.generate!(:author => User.anonymous, :assigned_to => User.anonymous, :is_private => true)
211 assert_nil Issue.where(:id => issue.id).visible(User.anonymous).first
212 assert !issue.visible?(User.anonymous)
213 end
214
215 def test_visible_scope_for_non_member
216 user = User.find(9)
217 assert user.projects.empty?
218 # Non member user should see issues of public projects only
219 issues = Issue.visible(user).all
220 assert issues.any?
221 assert_nil issues.detect {|issue| !issue.project.is_public?}
222 assert_nil issues.detect {|issue| issue.is_private?}
223 assert_visibility_match user, issues
224 end
225
226 def test_visible_scope_for_non_member_with_own_issues_visibility
227 Role.non_member.update_attribute :issues_visibility, 'own'
228 Issue.create!(:project_id => 1, :tracker_id => 1, :author_id => 9, :subject => 'Issue by non member')
229 user = User.find(9)
230
231 issues = Issue.visible(user).all
232 assert issues.any?
233 assert_nil issues.detect {|issue| issue.author != user}
234 assert_visibility_match user, issues
235 end
236
237 def test_visible_scope_for_non_member_without_view_issues_permissions
238 # Non member user should not see issues without permission
239 Role.non_member.remove_permission!(:view_issues)
240 user = User.find(9)
241 assert user.projects.empty?
242 issues = Issue.visible(user).all
243 assert issues.empty?
244 assert_visibility_match user, issues
245 end
246
247 def test_visible_scope_for_member
248 user = User.find(9)
249 # User should see issues of projects for which he has view_issues permissions only
250 Role.non_member.remove_permission!(:view_issues)
251 Member.create!(:principal => user, :project_id => 3, :role_ids => [2])
252 issues = Issue.visible(user).all
253 assert issues.any?
254 assert_nil issues.detect {|issue| issue.project_id != 3}
255 assert_nil issues.detect {|issue| issue.is_private?}
256 assert_visibility_match user, issues
257 end
258
259 def test_visible_scope_for_member_with_groups_should_return_assigned_issues
260 user = User.find(8)
261 assert user.groups.any?
262 Member.create!(:principal => user.groups.first, :project_id => 1, :role_ids => [2])
263 Role.non_member.remove_permission!(:view_issues)
264
265 issue = Issue.create(:project_id => 1, :tracker_id => 1, :author_id => 3,
266 :status_id => 1, :priority => IssuePriority.all.first,
267 :subject => 'Assignment test',
268 :assigned_to => user.groups.first,
269 :is_private => true)
270
271 Role.find(2).update_attribute :issues_visibility, 'default'
272 issues = Issue.visible(User.find(8)).all
273 assert issues.any?
274 assert issues.include?(issue)
275
276 Role.find(2).update_attribute :issues_visibility, 'own'
277 issues = Issue.visible(User.find(8)).all
278 assert issues.any?
279 assert issues.include?(issue)
280 end
281
282 def test_visible_scope_for_admin
283 user = User.find(1)
284 user.members.each(&:destroy)
285 assert user.projects.empty?
286 issues = Issue.visible(user).all
287 assert issues.any?
288 # Admin should see issues on private projects that he does not belong to
289 assert issues.detect {|issue| !issue.project.is_public?}
290 # Admin should see private issues of other users
291 assert issues.detect {|issue| issue.is_private? && issue.author != user}
292 assert_visibility_match user, issues
293 end
294
295 def test_visible_scope_with_project
296 project = Project.find(1)
297 issues = Issue.visible(User.find(2), :project => project).all
298 projects = issues.collect(&:project).uniq
299 assert_equal 1, projects.size
300 assert_equal project, projects.first
301 end
302
303 def test_visible_scope_with_project_and_subprojects
304 project = Project.find(1)
305 issues = Issue.visible(User.find(2), :project => project, :with_subprojects => true).all
306 projects = issues.collect(&:project).uniq
307 assert projects.size > 1
308 assert_equal [], projects.select {|p| !p.is_or_is_descendant_of?(project)}
309 end
310
311 def test_visible_and_nested_set_scopes
312 assert_equal 0, Issue.find(1).descendants.visible.all.size
313 end
314
315 def test_open_scope
316 issues = Issue.open.all
317 assert_nil issues.detect(&:closed?)
318 end
319
320 def test_open_scope_with_arg
321 issues = Issue.open(false).all
322 assert_equal issues, issues.select(&:closed?)
323 end
324
325 def test_fixed_version_scope_with_a_version_should_return_its_fixed_issues
326 version = Version.find(2)
327 assert version.fixed_issues.any?
328 assert_equal version.fixed_issues.to_a.sort, Issue.fixed_version(version).to_a.sort
329 end
330
331 def test_fixed_version_scope_with_empty_array_should_return_no_result
332 assert_equal 0, Issue.fixed_version([]).count
333 end
334
335 def test_errors_full_messages_should_include_custom_fields_errors
336 field = IssueCustomField.find_by_name('Database')
337
338 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1,
339 :status_id => 1, :subject => 'test_create',
340 :description => 'IssueTest#test_create_with_required_custom_field')
341 assert issue.available_custom_fields.include?(field)
342 # Invalid value
343 issue.custom_field_values = { field.id => 'SQLServer' }
344
345 assert !issue.valid?
346 assert_equal 1, issue.errors.full_messages.size
347 assert_equal "Database #{I18n.translate('activerecord.errors.messages.inclusion')}",
348 issue.errors.full_messages.first
349 end
350
351 def test_update_issue_with_required_custom_field
352 field = IssueCustomField.find_by_name('Database')
353 field.update_attribute(:is_required, true)
354
355 issue = Issue.find(1)
356 assert_nil issue.custom_value_for(field)
357 assert issue.available_custom_fields.include?(field)
358 # No change to custom values, issue can be saved
359 assert issue.save
360 # Blank value
361 issue.custom_field_values = { field.id => '' }
362 assert !issue.save
363 # Valid value
364 issue.custom_field_values = { field.id => 'PostgreSQL' }
365 assert issue.save
366 issue.reload
367 assert_equal 'PostgreSQL', issue.custom_value_for(field).value
368 end
369
370 def test_should_not_update_attributes_if_custom_fields_validation_fails
371 issue = Issue.find(1)
372 field = IssueCustomField.find_by_name('Database')
373 assert issue.available_custom_fields.include?(field)
374
375 issue.custom_field_values = { field.id => 'Invalid' }
376 issue.subject = 'Should be not be saved'
377 assert !issue.save
378
379 issue.reload
380 assert_equal "Can't print recipes", issue.subject
381 end
382
383 def test_should_not_recreate_custom_values_objects_on_update
384 field = IssueCustomField.find_by_name('Database')
385
386 issue = Issue.find(1)
387 issue.custom_field_values = { field.id => 'PostgreSQL' }
388 assert issue.save
389 custom_value = issue.custom_value_for(field)
390 issue.reload
391 issue.custom_field_values = { field.id => 'MySQL' }
392 assert issue.save
393 issue.reload
394 assert_equal custom_value.id, issue.custom_value_for(field).id
395 end
396
397 def test_should_not_update_custom_fields_on_changing_tracker_with_different_custom_fields
398 issue = Issue.create!(:project_id => 1, :tracker_id => 1, :author_id => 1,
399 :status_id => 1, :subject => 'Test',
400 :custom_field_values => {'2' => 'Test'})
401 assert !Tracker.find(2).custom_field_ids.include?(2)
402
403 issue = Issue.find(issue.id)
404 issue.attributes = {:tracker_id => 2, :custom_field_values => {'1' => ''}}
405
406 issue = Issue.find(issue.id)
407 custom_value = issue.custom_value_for(2)
408 assert_not_nil custom_value
409 assert_equal 'Test', custom_value.value
410 end
411
412 def test_assigning_tracker_id_should_reload_custom_fields_values
413 issue = Issue.new(:project => Project.find(1))
414 assert issue.custom_field_values.empty?
415 issue.tracker_id = 1
416 assert issue.custom_field_values.any?
417 end
418
419 def test_assigning_attributes_should_assign_project_and_tracker_first
420 seq = sequence('seq')
421 issue = Issue.new
422 issue.expects(:project_id=).in_sequence(seq)
423 issue.expects(:tracker_id=).in_sequence(seq)
424 issue.expects(:subject=).in_sequence(seq)
425 issue.attributes = {:tracker_id => 2, :project_id => 1, :subject => 'Test'}
426 end
427
428 def test_assigning_tracker_and_custom_fields_should_assign_custom_fields
429 attributes = ActiveSupport::OrderedHash.new
430 attributes['custom_field_values'] = { '1' => 'MySQL' }
431 attributes['tracker_id'] = '1'
432 issue = Issue.new(:project => Project.find(1))
433 issue.attributes = attributes
434 assert_equal 'MySQL', issue.custom_field_value(1)
435 end
436
437 def test_reload_should_reload_custom_field_values
438 issue = Issue.generate!
439 issue.custom_field_values = {'2' => 'Foo'}
440 issue.save!
441
442 issue = Issue.order('id desc').first
443 assert_equal 'Foo', issue.custom_field_value(2)
444
445 issue.custom_field_values = {'2' => 'Bar'}
446 assert_equal 'Bar', issue.custom_field_value(2)
447
448 issue.reload
449 assert_equal 'Foo', issue.custom_field_value(2)
450 end
451
452 def test_should_update_issue_with_disabled_tracker
453 p = Project.find(1)
454 issue = Issue.find(1)
455
456 p.trackers.delete(issue.tracker)
457 assert !p.trackers.include?(issue.tracker)
458
459 issue.reload
460 issue.subject = 'New subject'
461 assert issue.save
462 end
463
464 def test_should_not_set_a_disabled_tracker
465 p = Project.find(1)
466 p.trackers.delete(Tracker.find(2))
467
468 issue = Issue.find(1)
469 issue.tracker_id = 2
470 issue.subject = 'New subject'
471 assert !issue.save
472 assert_not_nil issue.errors[:tracker_id]
473 end
474
475 def test_category_based_assignment
476 issue = Issue.create(:project_id => 1, :tracker_id => 1, :author_id => 3,
477 :status_id => 1, :priority => IssuePriority.all.first,
478 :subject => 'Assignment test',
479 :description => 'Assignment test', :category_id => 1)
480 assert_equal IssueCategory.find(1).assigned_to, issue.assigned_to
481 end
482
483 def test_new_statuses_allowed_to
484 WorkflowTransition.delete_all
485 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1,
486 :old_status_id => 1, :new_status_id => 2,
487 :author => false, :assignee => false)
488 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1,
489 :old_status_id => 1, :new_status_id => 3,
490 :author => true, :assignee => false)
491 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1,
492 :new_status_id => 4, :author => false,
493 :assignee => true)
494 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1,
495 :old_status_id => 1, :new_status_id => 5,
496 :author => true, :assignee => true)
497 status = IssueStatus.find(1)
498 role = Role.find(1)
499 tracker = Tracker.find(1)
500 user = User.find(2)
501
502 issue = Issue.generate!(:tracker => tracker, :status => status,
503 :project_id => 1, :author_id => 1)
504 assert_equal [1, 2], issue.new_statuses_allowed_to(user).map(&:id)
505
506 issue = Issue.generate!(:tracker => tracker, :status => status,
507 :project_id => 1, :author => user)
508 assert_equal [1, 2, 3, 5], issue.new_statuses_allowed_to(user).map(&:id)
509
510 issue = Issue.generate!(:tracker => tracker, :status => status,
511 :project_id => 1, :author_id => 1,
512 :assigned_to => user)
513 assert_equal [1, 2, 4, 5], issue.new_statuses_allowed_to(user).map(&:id)
514
515 issue = Issue.generate!(:tracker => tracker, :status => status,
516 :project_id => 1, :author => user,
517 :assigned_to => user)
518 assert_equal [1, 2, 3, 4, 5], issue.new_statuses_allowed_to(user).map(&:id)
519 end
520
521 def test_new_statuses_allowed_to_should_return_all_transitions_for_admin
522 admin = User.find(1)
523 issue = Issue.find(1)
524 assert !admin.member_of?(issue.project)
525 expected_statuses = [issue.status] +
526 WorkflowTransition.find_all_by_old_status_id(
527 issue.status_id).map(&:new_status).uniq.sort
528 assert_equal expected_statuses, issue.new_statuses_allowed_to(admin)
529 end
530
531 def test_new_statuses_allowed_to_should_return_default_and_current_status_when_copying
532 issue = Issue.find(1).copy
533 assert_equal [1], issue.new_statuses_allowed_to(User.find(2)).map(&:id)
534
535 issue = Issue.find(2).copy
536 assert_equal [1, 2], issue.new_statuses_allowed_to(User.find(2)).map(&:id)
537 end
538
539 def test_safe_attributes_names_should_not_include_disabled_field
540 tracker = Tracker.new(:core_fields => %w(assigned_to_id fixed_version_id))
541
542 issue = Issue.new(:tracker => tracker)
543 assert_include 'tracker_id', issue.safe_attribute_names
544 assert_include 'status_id', issue.safe_attribute_names
545 assert_include 'subject', issue.safe_attribute_names
546 assert_include 'description', issue.safe_attribute_names
547 assert_include 'custom_field_values', issue.safe_attribute_names
548 assert_include 'custom_fields', issue.safe_attribute_names
549 assert_include 'lock_version', issue.safe_attribute_names
550
551 tracker.core_fields.each do |field|
552 assert_include field, issue.safe_attribute_names
553 end
554
555 tracker.disabled_core_fields.each do |field|
556 assert_not_include field, issue.safe_attribute_names
557 end
558 end
559
560 def test_safe_attributes_should_ignore_disabled_fields
561 tracker = Tracker.find(1)
562 tracker.core_fields = %w(assigned_to_id due_date)
563 tracker.save!
564
565 issue = Issue.new(:tracker => tracker)
566 issue.safe_attributes = {'start_date' => '2012-07-14', 'due_date' => '2012-07-14'}
567 assert_nil issue.start_date
568 assert_equal Date.parse('2012-07-14'), issue.due_date
569 end
570
571 def test_safe_attributes_should_accept_target_tracker_enabled_fields
572 source = Tracker.find(1)
573 source.core_fields = []
574 source.save!
575 target = Tracker.find(2)
576 target.core_fields = %w(assigned_to_id due_date)
577 target.save!
578
579 issue = Issue.new(:tracker => source)
580 issue.safe_attributes = {'tracker_id' => 2, 'due_date' => '2012-07-14'}
581 assert_equal target, issue.tracker
582 assert_equal Date.parse('2012-07-14'), issue.due_date
583 end
584
585 def test_safe_attributes_should_not_include_readonly_fields
586 WorkflowPermission.delete_all
587 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1,
588 :role_id => 1, :field_name => 'due_date',
589 :rule => 'readonly')
590 user = User.find(2)
591
592 issue = Issue.new(:project_id => 1, :tracker_id => 1)
593 assert_equal %w(due_date), issue.read_only_attribute_names(user)
594 assert_not_include 'due_date', issue.safe_attribute_names(user)
595
596 issue.send :safe_attributes=, {'start_date' => '2012-07-14', 'due_date' => '2012-07-14'}, user
597 assert_equal Date.parse('2012-07-14'), issue.start_date
598 assert_nil issue.due_date
599 end
600
601 def test_safe_attributes_should_not_include_readonly_custom_fields
602 cf1 = IssueCustomField.create!(:name => 'Writable field',
603 :field_format => 'string',
604 :is_for_all => true, :tracker_ids => [1])
605 cf2 = IssueCustomField.create!(:name => 'Readonly field',
606 :field_format => 'string',
607 :is_for_all => true, :tracker_ids => [1])
608 WorkflowPermission.delete_all
609 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1,
610 :role_id => 1, :field_name => cf2.id.to_s,
611 :rule => 'readonly')
612 user = User.find(2)
613 issue = Issue.new(:project_id => 1, :tracker_id => 1)
614 assert_equal [cf2.id.to_s], issue.read_only_attribute_names(user)
615 assert_not_include cf2.id.to_s, issue.safe_attribute_names(user)
616
617 issue.send :safe_attributes=, {'custom_field_values' => {
618 cf1.id.to_s => 'value1', cf2.id.to_s => 'value2'
619 }}, user
620 assert_equal 'value1', issue.custom_field_value(cf1)
621 assert_nil issue.custom_field_value(cf2)
622
623 issue.send :safe_attributes=, {'custom_fields' => [
624 {'id' => cf1.id.to_s, 'value' => 'valuea'},
625 {'id' => cf2.id.to_s, 'value' => 'valueb'}
626 ]}, user
627 assert_equal 'valuea', issue.custom_field_value(cf1)
628 assert_nil issue.custom_field_value(cf2)
629 end
630
631 def test_editable_custom_field_values_should_return_non_readonly_custom_values
632 cf1 = IssueCustomField.create!(:name => 'Writable field', :field_format => 'string',
633 :is_for_all => true, :tracker_ids => [1, 2])
634 cf2 = IssueCustomField.create!(:name => 'Readonly field', :field_format => 'string',
635 :is_for_all => true, :tracker_ids => [1, 2])
636 WorkflowPermission.delete_all
637 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1,
638 :field_name => cf2.id.to_s, :rule => 'readonly')
639 user = User.find(2)
640
641 issue = Issue.new(:project_id => 1, :tracker_id => 1)
642 values = issue.editable_custom_field_values(user)
643 assert values.detect {|value| value.custom_field == cf1}
644 assert_nil values.detect {|value| value.custom_field == cf2}
645
646 issue.tracker_id = 2
647 values = issue.editable_custom_field_values(user)
648 assert values.detect {|value| value.custom_field == cf1}
649 assert values.detect {|value| value.custom_field == cf2}
650 end
651
652 def test_safe_attributes_should_accept_target_tracker_writable_fields
653 WorkflowPermission.delete_all
654 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1,
655 :role_id => 1, :field_name => 'due_date',
656 :rule => 'readonly')
657 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2,
658 :role_id => 1, :field_name => 'start_date',
659 :rule => 'readonly')
660 user = User.find(2)
661
662 issue = Issue.new(:project_id => 1, :tracker_id => 1, :status_id => 1)
663
664 issue.send :safe_attributes=, {'start_date' => '2012-07-12',
665 'due_date' => '2012-07-14'}, user
666 assert_equal Date.parse('2012-07-12'), issue.start_date
667 assert_nil issue.due_date
668
669 issue.send :safe_attributes=, {'start_date' => '2012-07-15',
670 'due_date' => '2012-07-16',
671 'tracker_id' => 2}, user
672 assert_equal Date.parse('2012-07-12'), issue.start_date
673 assert_equal Date.parse('2012-07-16'), issue.due_date
674 end
675
676 def test_safe_attributes_should_accept_target_status_writable_fields
677 WorkflowPermission.delete_all
678 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1,
679 :role_id => 1, :field_name => 'due_date',
680 :rule => 'readonly')
681 WorkflowPermission.create!(:old_status_id => 2, :tracker_id => 1,
682 :role_id => 1, :field_name => 'start_date',
683 :rule => 'readonly')
684 user = User.find(2)
685
686 issue = Issue.new(:project_id => 1, :tracker_id => 1, :status_id => 1)
687
688 issue.send :safe_attributes=, {'start_date' => '2012-07-12',
689 'due_date' => '2012-07-14'},
690 user
691 assert_equal Date.parse('2012-07-12'), issue.start_date
692 assert_nil issue.due_date
693
694 issue.send :safe_attributes=, {'start_date' => '2012-07-15',
695 'due_date' => '2012-07-16',
696 'status_id' => 2},
697 user
698 assert_equal Date.parse('2012-07-12'), issue.start_date
699 assert_equal Date.parse('2012-07-16'), issue.due_date
700 end
701
702 def test_required_attributes_should_be_validated
703 cf = IssueCustomField.create!(:name => 'Foo', :field_format => 'string',
704 :is_for_all => true, :tracker_ids => [1, 2])
705
706 WorkflowPermission.delete_all
707 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1,
708 :role_id => 1, :field_name => 'due_date',
709 :rule => 'required')
710 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1,
711 :role_id => 1, :field_name => 'category_id',
712 :rule => 'required')
713 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1,
714 :role_id => 1, :field_name => cf.id.to_s,
715 :rule => 'required')
716
717 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2,
718 :role_id => 1, :field_name => 'start_date',
719 :rule => 'required')
720 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2,
721 :role_id => 1, :field_name => cf.id.to_s,
722 :rule => 'required')
723 user = User.find(2)
724
725 issue = Issue.new(:project_id => 1, :tracker_id => 1,
726 :status_id => 1, :subject => 'Required fields',
727 :author => user)
728 assert_equal [cf.id.to_s, "category_id", "due_date"],
729 issue.required_attribute_names(user).sort
730 assert !issue.save, "Issue was saved"
731 assert_equal ["Category can't be blank", "Due date can't be blank", "Foo can't be blank"],
732 issue.errors.full_messages.sort
733
734 issue.tracker_id = 2
735 assert_equal [cf.id.to_s, "start_date"], issue.required_attribute_names(user).sort
736 assert !issue.save, "Issue was saved"
737 assert_equal ["Foo can't be blank", "Start date can't be blank"],
738 issue.errors.full_messages.sort
739
740 issue.start_date = Date.today
741 issue.custom_field_values = {cf.id.to_s => 'bar'}
742 assert issue.save
743 end
744
745 def test_required_attribute_names_for_multiple_roles_should_intersect_rules
746 WorkflowPermission.delete_all
747 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1,
748 :role_id => 1, :field_name => 'due_date',
749 :rule => 'required')
750 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1,
751 :role_id => 1, :field_name => 'start_date',
752 :rule => 'required')
753 user = User.find(2)
754 member = Member.find(1)
755 issue = Issue.new(:project_id => 1, :tracker_id => 1, :status_id => 1)
756
757 assert_equal %w(due_date start_date), issue.required_attribute_names(user).sort
758
759 member.role_ids = [1, 2]
760 member.save!
761 assert_equal [], issue.required_attribute_names(user.reload)
762
763 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1,
764 :role_id => 2, :field_name => 'due_date',
765 :rule => 'required')
766 assert_equal %w(due_date), issue.required_attribute_names(user)
767
768 member.role_ids = [1, 2, 3]
769 member.save!
770 assert_equal [], issue.required_attribute_names(user.reload)
771
772 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1,
773 :role_id => 2, :field_name => 'due_date',
774 :rule => 'readonly')
775 # required + readonly => required
776 assert_equal %w(due_date), issue.required_attribute_names(user)
777 end
778
779 def test_read_only_attribute_names_for_multiple_roles_should_intersect_rules
780 WorkflowPermission.delete_all
781 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1,
782 :role_id => 1, :field_name => 'due_date',
783 :rule => 'readonly')
784 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1,
785 :role_id => 1, :field_name => 'start_date',
786 :rule => 'readonly')
787 user = User.find(2)
788 member = Member.find(1)
789 issue = Issue.new(:project_id => 1, :tracker_id => 1, :status_id => 1)
790
791 assert_equal %w(due_date start_date), issue.read_only_attribute_names(user).sort
792
793 member.role_ids = [1, 2]
794 member.save!
795 assert_equal [], issue.read_only_attribute_names(user.reload)
796
797 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1,
798 :role_id => 2, :field_name => 'due_date',
799 :rule => 'readonly')
800 assert_equal %w(due_date), issue.read_only_attribute_names(user)
801 end
802
803 def test_copy
804 issue = Issue.new.copy_from(1)
805 assert issue.copy?
806 assert issue.save
807 issue.reload
808 orig = Issue.find(1)
809 assert_equal orig.subject, issue.subject
810 assert_equal orig.tracker, issue.tracker
811 assert_equal "125", issue.custom_value_for(2).value
812 end
813
814 def test_copy_should_copy_status
815 orig = Issue.find(8)
816 assert orig.status != IssueStatus.default
817
818 issue = Issue.new.copy_from(orig)
819 assert issue.save
820 issue.reload
821 assert_equal orig.status, issue.status
822 end
823
824 def test_copy_should_add_relation_with_copied_issue
825 copied = Issue.find(1)
826 issue = Issue.new.copy_from(copied)
827 assert issue.save
828 issue.reload
829
830 assert_equal 1, issue.relations.size
831 relation = issue.relations.first
832 assert_equal 'copied_to', relation.relation_type
833 assert_equal copied, relation.issue_from
834 assert_equal issue, relation.issue_to
835 end
836
837 def test_copy_should_copy_subtasks
838 issue = Issue.generate_with_descendants!
839
840 copy = issue.reload.copy
841 copy.author = User.find(7)
842 assert_difference 'Issue.count', 1+issue.descendants.count do
843 assert copy.save
844 end
845 copy.reload
846 assert_equal %w(Child1 Child2), copy.children.map(&:subject).sort
847 child_copy = copy.children.detect {|c| c.subject == 'Child1'}
848 assert_equal %w(Child11), child_copy.children.map(&:subject).sort
849 assert_equal copy.author, child_copy.author
850 end
851
852 def test_copy_as_a_child_of_copied_issue_should_not_copy_itself
853 parent = Issue.generate!
854 child1 = Issue.generate!(:parent_issue_id => parent.id, :subject => 'Child 1')
855 child2 = Issue.generate!(:parent_issue_id => parent.id, :subject => 'Child 2')
856
857 copy = parent.reload.copy
858 copy.parent_issue_id = parent.id
859 copy.author = User.find(7)
860 assert_difference 'Issue.count', 3 do
861 assert copy.save
862 end
863 parent.reload
864 copy.reload
865 assert_equal parent, copy.parent
866 assert_equal 3, parent.children.count
867 assert_equal 5, parent.descendants.count
868 assert_equal 2, copy.children.count
869 assert_equal 2, copy.descendants.count
870 end
871
872 def test_copy_as_a_descendant_of_copied_issue_should_not_copy_itself
873 parent = Issue.generate!
874 child1 = Issue.generate!(:parent_issue_id => parent.id, :subject => 'Child 1')
875 child2 = Issue.generate!(:parent_issue_id => parent.id, :subject => 'Child 2')
876
877 copy = parent.reload.copy
878 copy.parent_issue_id = child1.id
879 copy.author = User.find(7)
880 assert_difference 'Issue.count', 3 do
881 assert copy.save
882 end
883 parent.reload
884 child1.reload
885 copy.reload
886 assert_equal child1, copy.parent
887 assert_equal 2, parent.children.count
888 assert_equal 5, parent.descendants.count
889 assert_equal 1, child1.children.count
890 assert_equal 3, child1.descendants.count
891 assert_equal 2, copy.children.count
892 assert_equal 2, copy.descendants.count
893 end
894
895 def test_copy_should_copy_subtasks_to_target_project
896 issue = Issue.generate_with_descendants!
897
898 copy = issue.copy(:project_id => 3)
899 assert_difference 'Issue.count', 1+issue.descendants.count do
900 assert copy.save
901 end
902 assert_equal [3], copy.reload.descendants.map(&:project_id).uniq
903 end
904
905 def test_copy_should_not_copy_subtasks_twice_when_saving_twice
906 issue = Issue.generate_with_descendants!
907
908 copy = issue.reload.copy
909 assert_difference 'Issue.count', 1+issue.descendants.count do
910 assert copy.save
911 assert copy.save
912 end
913 end
914
915 def test_should_not_call_after_project_change_on_creation
916 issue = Issue.new(:project_id => 1, :tracker_id => 1, :status_id => 1,
917 :subject => 'Test', :author_id => 1)
918 issue.expects(:after_project_change).never
919 issue.save!
920 end
921
922 def test_should_not_call_after_project_change_on_update
923 issue = Issue.find(1)
924 issue.project = Project.find(1)
925 issue.subject = 'No project change'
926 issue.expects(:after_project_change).never
927 issue.save!
928 end
929
930 def test_should_call_after_project_change_on_project_change
931 issue = Issue.find(1)
932 issue.project = Project.find(2)
933 issue.expects(:after_project_change).once
934 issue.save!
935 end
936
937 def test_adding_journal_should_update_timestamp
938 issue = Issue.find(1)
939 updated_on_was = issue.updated_on
940
941 issue.init_journal(User.first, "Adding notes")
942 assert_difference 'Journal.count' do
943 assert issue.save
944 end
945 issue.reload
946
947 assert_not_equal updated_on_was, issue.updated_on
948 end
949
950 def test_should_close_duplicates
951 # Create 3 issues
952 issue1 = Issue.generate!
953 issue2 = Issue.generate!
954 issue3 = Issue.generate!
955
956 # 2 is a dupe of 1
957 IssueRelation.create!(:issue_from => issue2, :issue_to => issue1,
958 :relation_type => IssueRelation::TYPE_DUPLICATES)
959 # And 3 is a dupe of 2
960 IssueRelation.create!(:issue_from => issue3, :issue_to => issue2,
961 :relation_type => IssueRelation::TYPE_DUPLICATES)
962 # And 3 is a dupe of 1 (circular duplicates)
963 IssueRelation.create!(:issue_from => issue3, :issue_to => issue1,
964 :relation_type => IssueRelation::TYPE_DUPLICATES)
965
966 assert issue1.reload.duplicates.include?(issue2)
967
968 # Closing issue 1
969 issue1.init_journal(User.first, "Closing issue1")
970 issue1.status = IssueStatus.where(:is_closed => true).first
971 assert issue1.save
972 # 2 and 3 should be also closed
973 assert issue2.reload.closed?
974 assert issue3.reload.closed?
975 end
976
977 def test_should_not_close_duplicated_issue
978 issue1 = Issue.generate!
979 issue2 = Issue.generate!
980
981 # 2 is a dupe of 1
982 IssueRelation.create(:issue_from => issue2, :issue_to => issue1,
983 :relation_type => IssueRelation::TYPE_DUPLICATES)
984 # 2 is a dup of 1 but 1 is not a duplicate of 2
985 assert !issue2.reload.duplicates.include?(issue1)
986
987 # Closing issue 2
988 issue2.init_journal(User.first, "Closing issue2")
989 issue2.status = IssueStatus.where(:is_closed => true).first
990 assert issue2.save
991 # 1 should not be also closed
992 assert !issue1.reload.closed?
993 end
994
995 def test_assignable_versions
996 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1,
997 :status_id => 1, :fixed_version_id => 1,
998 :subject => 'New issue')
999 assert_equal ['open'], issue.assignable_versions.collect(&:status).uniq
1000 end
1001
1002 def test_should_not_be_able_to_assign_a_new_issue_to_a_closed_version
1003 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1,
1004 :status_id => 1, :fixed_version_id => 1,
1005 :subject => 'New issue')
1006 assert !issue.save
1007 assert_not_nil issue.errors[:fixed_version_id]
1008 end
1009
1010 def test_should_not_be_able_to_assign_a_new_issue_to_a_locked_version
1011 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1,
1012 :status_id => 1, :fixed_version_id => 2,
1013 :subject => 'New issue')
1014 assert !issue.save
1015 assert_not_nil issue.errors[:fixed_version_id]
1016 end
1017
1018 def test_should_be_able_to_assign_a_new_issue_to_an_open_version
1019 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1,
1020 :status_id => 1, :fixed_version_id => 3,
1021 :subject => 'New issue')
1022 assert issue.save
1023 end
1024
1025 def test_should_be_able_to_update_an_issue_assigned_to_a_closed_version
1026 issue = Issue.find(11)
1027 assert_equal 'closed', issue.fixed_version.status
1028 issue.subject = 'Subject changed'
1029 assert issue.save
1030 end
1031
1032 def test_should_not_be_able_to_reopen_an_issue_assigned_to_a_closed_version
1033 issue = Issue.find(11)
1034 issue.status_id = 1
1035 assert !issue.save
1036 assert_not_nil issue.errors[:base]
1037 end
1038
1039 def test_should_be_able_to_reopen_and_reassign_an_issue_assigned_to_a_closed_version
1040 issue = Issue.find(11)
1041 issue.status_id = 1
1042 issue.fixed_version_id = 3
1043 assert issue.save
1044 end
1045
1046 def test_should_be_able_to_reopen_an_issue_assigned_to_a_locked_version
1047 issue = Issue.find(12)
1048 assert_equal 'locked', issue.fixed_version.status
1049 issue.status_id = 1
1050 assert issue.save
1051 end
1052
1053 def test_should_not_be_able_to_keep_unshared_version_when_changing_project
1054 issue = Issue.find(2)
1055 assert_equal 2, issue.fixed_version_id
1056 issue.project_id = 3
1057 assert_nil issue.fixed_version_id
1058 issue.fixed_version_id = 2
1059 assert !issue.save
1060 assert_include 'Target version is not included in the list', issue.errors.full_messages
1061 end
1062
1063 def test_should_keep_shared_version_when_changing_project
1064 Version.find(2).update_attribute :sharing, 'tree'
1065
1066 issue = Issue.find(2)
1067 assert_equal 2, issue.fixed_version_id
1068 issue.project_id = 3
1069 assert_equal 2, issue.fixed_version_id
1070 assert issue.save
1071 end
1072
1073 def test_allowed_target_projects_on_move_should_include_projects_with_issue_tracking_enabled
1074 assert_include Project.find(2), Issue.allowed_target_projects_on_move(User.find(2))
1075 end
1076
1077 def test_allowed_target_projects_on_move_should_not_include_projects_with_issue_tracking_disabled
1078 Project.find(2).disable_module! :issue_tracking
1079 assert_not_include Project.find(2), Issue.allowed_target_projects_on_move(User.find(2))
1080 end
1081
1082 def test_move_to_another_project_with_same_category
1083 issue = Issue.find(1)
1084 issue.project = Project.find(2)
1085 assert issue.save
1086 issue.reload
1087 assert_equal 2, issue.project_id
1088 # Category changes
1089 assert_equal 4, issue.category_id
1090 # Make sure time entries were move to the target project
1091 assert_equal 2, issue.time_entries.first.project_id
1092 end
1093
1094 def test_move_to_another_project_without_same_category
1095 issue = Issue.find(2)
1096 issue.project = Project.find(2)
1097 assert issue.save
1098 issue.reload
1099 assert_equal 2, issue.project_id
1100 # Category cleared
1101 assert_nil issue.category_id
1102 end
1103
1104 def test_move_to_another_project_should_clear_fixed_version_when_not_shared
1105 issue = Issue.find(1)
1106 issue.update_attribute(:fixed_version_id, 1)
1107 issue.project = Project.find(2)
1108 assert issue.save
1109 issue.reload
1110 assert_equal 2, issue.project_id
1111 # Cleared fixed_version
1112 assert_equal nil, issue.fixed_version
1113 end
1114
1115 def test_move_to_another_project_should_keep_fixed_version_when_shared_with_the_target_project
1116 issue = Issue.find(1)
1117 issue.update_attribute(:fixed_version_id, 4)
1118 issue.project = Project.find(5)
1119 assert issue.save
1120 issue.reload
1121 assert_equal 5, issue.project_id
1122 # Keep fixed_version
1123 assert_equal 4, issue.fixed_version_id
1124 end
1125
1126 def test_move_to_another_project_should_clear_fixed_version_when_not_shared_with_the_target_project
1127 issue = Issue.find(1)
1128 issue.update_attribute(:fixed_version_id, 1)
1129 issue.project = Project.find(5)
1130 assert issue.save
1131 issue.reload
1132 assert_equal 5, issue.project_id
1133 # Cleared fixed_version
1134 assert_equal nil, issue.fixed_version
1135 end
1136
1137 def test_move_to_another_project_should_keep_fixed_version_when_shared_systemwide
1138 issue = Issue.find(1)
1139 issue.update_attribute(:fixed_version_id, 7)
1140 issue.project = Project.find(2)
1141 assert issue.save
1142 issue.reload
1143 assert_equal 2, issue.project_id
1144 # Keep fixed_version
1145 assert_equal 7, issue.fixed_version_id
1146 end
1147
1148 def test_move_to_another_project_should_keep_parent_if_valid
1149 issue = Issue.find(1)
1150 issue.update_attribute(:parent_issue_id, 2)
1151 issue.project = Project.find(3)
1152 assert issue.save
1153 issue.reload
1154 assert_equal 2, issue.parent_id
1155 end
1156
1157 def test_move_to_another_project_should_clear_parent_if_not_valid
1158 issue = Issue.find(1)
1159 issue.update_attribute(:parent_issue_id, 2)
1160 issue.project = Project.find(2)
1161 assert issue.save
1162 issue.reload
1163 assert_nil issue.parent_id
1164 end
1165
1166 def test_move_to_another_project_with_disabled_tracker
1167 issue = Issue.find(1)
1168 target = Project.find(2)
1169 target.tracker_ids = [3]
1170 target.save
1171 issue.project = target
1172 assert issue.save
1173 issue.reload
1174 assert_equal 2, issue.project_id
1175 assert_equal 3, issue.tracker_id
1176 end
1177
1178 def test_copy_to_the_same_project
1179 issue = Issue.find(1)
1180 copy = issue.copy
1181 assert_difference 'Issue.count' do
1182 copy.save!
1183 end
1184 assert_kind_of Issue, copy
1185 assert_equal issue.project, copy.project
1186 assert_equal "125", copy.custom_value_for(2).value
1187 end
1188
1189 def test_copy_to_another_project_and_tracker
1190 issue = Issue.find(1)
1191 copy = issue.copy(:project_id => 3, :tracker_id => 2)
1192 assert_difference 'Issue.count' do
1193 copy.save!
1194 end
1195 copy.reload
1196 assert_kind_of Issue, copy
1197 assert_equal Project.find(3), copy.project
1198 assert_equal Tracker.find(2), copy.tracker
1199 # Custom field #2 is not associated with target tracker
1200 assert_nil copy.custom_value_for(2)
1201 end
1202
1203 test "#copy should not create a journal" do
1204 copy = Issue.find(1).copy(:project_id => 3, :tracker_id => 2, :assigned_to_id => 3)
1205 copy.save!
1206 assert_equal 0, copy.reload.journals.size
1207 end
1208
1209 test "#copy should allow assigned_to changes" do
1210 copy = Issue.find(1).copy(:project_id => 3, :tracker_id => 2, :assigned_to_id => 3)
1211 assert_equal 3, copy.assigned_to_id
1212 end
1213
1214 test "#copy should allow status changes" do
1215 copy = Issue.find(1).copy(:project_id => 3, :tracker_id => 2, :status_id => 2)
1216 assert_equal 2, copy.status_id
1217 end
1218
1219 test "#copy should allow start date changes" do
1220 date = Date.today
1221 copy = Issue.find(1).copy(:project_id => 3, :tracker_id => 2, :start_date => date)
1222 assert_equal date, copy.start_date
1223 end
1224
1225 test "#copy should allow due date changes" do
1226 date = Date.today
1227 copy = Issue.find(1).copy(:project_id => 3, :tracker_id => 2, :due_date => date)
1228 assert_equal date, copy.due_date
1229 end
1230
1231 test "#copy should set current user as author" do
1232 User.current = User.find(9)
1233 copy = Issue.find(1).copy(:project_id => 3, :tracker_id => 2)
1234 assert_equal User.current, copy.author
1235 end
1236
1237 test "#copy should create a journal with notes" do
1238 date = Date.today
1239 notes = "Notes added when copying"
1240 copy = Issue.find(1).copy(:project_id => 3, :tracker_id => 2, :start_date => date)
1241 copy.init_journal(User.current, notes)
1242 copy.save!
1243
1244 assert_equal 1, copy.journals.size
1245 journal = copy.journals.first
1246 assert_equal 0, journal.details.size
1247 assert_equal notes, journal.notes
1248 end
1249
1250 def test_valid_parent_project
1251 issue = Issue.find(1)
1252 issue_in_same_project = Issue.find(2)
1253 issue_in_child_project = Issue.find(5)
1254 issue_in_grandchild_project = Issue.generate!(:project_id => 6, :tracker_id => 1)
1255 issue_in_other_child_project = Issue.find(6)
1256 issue_in_different_tree = Issue.find(4)
1257
1258 with_settings :cross_project_subtasks => '' do
1259 assert_equal true, issue.valid_parent_project?(issue_in_same_project)
1260 assert_equal false, issue.valid_parent_project?(issue_in_child_project)
1261 assert_equal false, issue.valid_parent_project?(issue_in_grandchild_project)
1262 assert_equal false, issue.valid_parent_project?(issue_in_different_tree)
1263 end
1264
1265 with_settings :cross_project_subtasks => 'system' do
1266 assert_equal true, issue.valid_parent_project?(issue_in_same_project)
1267 assert_equal true, issue.valid_parent_project?(issue_in_child_project)
1268 assert_equal true, issue.valid_parent_project?(issue_in_different_tree)
1269 end
1270
1271 with_settings :cross_project_subtasks => 'tree' do
1272 assert_equal true, issue.valid_parent_project?(issue_in_same_project)
1273 assert_equal true, issue.valid_parent_project?(issue_in_child_project)
1274 assert_equal true, issue.valid_parent_project?(issue_in_grandchild_project)
1275 assert_equal false, issue.valid_parent_project?(issue_in_different_tree)
1276
1277 assert_equal true, issue_in_child_project.valid_parent_project?(issue_in_same_project)
1278 assert_equal true, issue_in_child_project.valid_parent_project?(issue_in_other_child_project)
1279 end
1280
1281 with_settings :cross_project_subtasks => 'descendants' do
1282 assert_equal true, issue.valid_parent_project?(issue_in_same_project)
1283 assert_equal false, issue.valid_parent_project?(issue_in_child_project)
1284 assert_equal false, issue.valid_parent_project?(issue_in_grandchild_project)
1285 assert_equal false, issue.valid_parent_project?(issue_in_different_tree)
1286
1287 assert_equal true, issue_in_child_project.valid_parent_project?(issue)
1288 assert_equal false, issue_in_child_project.valid_parent_project?(issue_in_other_child_project)
1289 end
1290 end
1291
1292 def test_recipients_should_include_previous_assignee
1293 user = User.find(3)
1294 user.members.update_all ["mail_notification = ?", false]
1295 user.update_attribute :mail_notification, 'only_assigned'
1296
1297 issue = Issue.find(2)
1298 issue.assigned_to = nil
1299 assert_include user.mail, issue.recipients
1300 issue.save!
1301 assert !issue.recipients.include?(user.mail)
1302 end
1303
1304 def test_recipients_should_not_include_users_that_cannot_view_the_issue
1305 issue = Issue.find(12)
1306 assert issue.recipients.include?(issue.author.mail)
1307 # copy the issue to a private project
1308 copy = issue.copy(:project_id => 5, :tracker_id => 2)
1309 # author is not a member of project anymore
1310 assert !copy.recipients.include?(copy.author.mail)
1311 end
1312
1313 def test_recipients_should_include_the_assigned_group_members
1314 group_member = User.generate!
1315 group = Group.generate!
1316 group.users << group_member
1317
1318 issue = Issue.find(12)
1319 issue.assigned_to = group
1320 assert issue.recipients.include?(group_member.mail)
1321 end
1322
1323 def test_watcher_recipients_should_not_include_users_that_cannot_view_the_issue
1324 user = User.find(3)
1325 issue = Issue.find(9)
1326 Watcher.create!(:user => user, :watchable => issue)
1327 assert issue.watched_by?(user)
1328 assert !issue.watcher_recipients.include?(user.mail)
1329 end
1330
1331 def test_issue_destroy
1332 Issue.find(1).destroy
1333 assert_nil Issue.find_by_id(1)
1334 assert_nil TimeEntry.find_by_issue_id(1)
1335 end
1336
1337 def test_destroying_a_deleted_issue_should_not_raise_an_error
1338 issue = Issue.find(1)
1339 Issue.find(1).destroy
1340
1341 assert_nothing_raised do
1342 assert_no_difference 'Issue.count' do
1343 issue.destroy
1344 end
1345 assert issue.destroyed?
1346 end
1347 end
1348
1349 def test_destroying_a_stale_issue_should_not_raise_an_error
1350 issue = Issue.find(1)
1351 Issue.find(1).update_attribute :subject, "Updated"
1352
1353 assert_nothing_raised do
1354 assert_difference 'Issue.count', -1 do
1355 issue.destroy
1356 end
1357 assert issue.destroyed?
1358 end
1359 end
1360
1361 def test_blocked
1362 blocked_issue = Issue.find(9)
1363 blocking_issue = Issue.find(10)
1364
1365 assert blocked_issue.blocked?
1366 assert !blocking_issue.blocked?
1367 end
1368
1369 def test_blocked_issues_dont_allow_closed_statuses
1370 blocked_issue = Issue.find(9)
1371
1372 allowed_statuses = blocked_issue.new_statuses_allowed_to(users(:users_002))
1373 assert !allowed_statuses.empty?
1374 closed_statuses = allowed_statuses.select {|st| st.is_closed?}
1375 assert closed_statuses.empty?
1376 end
1377
1378 def test_unblocked_issues_allow_closed_statuses
1379 blocking_issue = Issue.find(10)
1380
1381 allowed_statuses = blocking_issue.new_statuses_allowed_to(users(:users_002))
1382 assert !allowed_statuses.empty?
1383 closed_statuses = allowed_statuses.select {|st| st.is_closed?}
1384 assert !closed_statuses.empty?
1385 end
1386
1387 def test_reschedule_an_issue_without_dates
1388 with_settings :non_working_week_days => [] do
1389 issue = Issue.new(:start_date => nil, :due_date => nil)
1390 issue.reschedule_on '2012-10-09'.to_date
1391 assert_equal '2012-10-09'.to_date, issue.start_date
1392 assert_equal '2012-10-09'.to_date, issue.due_date
1393 end
1394
1395 with_settings :non_working_week_days => %w(6 7) do
1396 issue = Issue.new(:start_date => nil, :due_date => nil)
1397 issue.reschedule_on '2012-10-09'.to_date
1398 assert_equal '2012-10-09'.to_date, issue.start_date
1399 assert_equal '2012-10-09'.to_date, issue.due_date
1400
1401 issue = Issue.new(:start_date => nil, :due_date => nil)
1402 issue.reschedule_on '2012-10-13'.to_date
1403 assert_equal '2012-10-15'.to_date, issue.start_date
1404 assert_equal '2012-10-15'.to_date, issue.due_date
1405 end
1406 end
1407
1408 def test_reschedule_an_issue_with_start_date
1409 with_settings :non_working_week_days => [] do
1410 issue = Issue.new(:start_date => '2012-10-09', :due_date => nil)
1411 issue.reschedule_on '2012-10-13'.to_date
1412 assert_equal '2012-10-13'.to_date, issue.start_date
1413 assert_equal '2012-10-13'.to_date, issue.due_date
1414 end
1415
1416 with_settings :non_working_week_days => %w(6 7) do
1417 issue = Issue.new(:start_date => '2012-10-09', :due_date => nil)
1418 issue.reschedule_on '2012-10-11'.to_date
1419 assert_equal '2012-10-11'.to_date, issue.start_date
1420 assert_equal '2012-10-11'.to_date, issue.due_date
1421
1422 issue = Issue.new(:start_date => '2012-10-09', :due_date => nil)
1423 issue.reschedule_on '2012-10-13'.to_date
1424 assert_equal '2012-10-15'.to_date, issue.start_date
1425 assert_equal '2012-10-15'.to_date, issue.due_date
1426 end
1427 end
1428
1429 def test_reschedule_an_issue_with_start_and_due_dates
1430 with_settings :non_working_week_days => [] do
1431 issue = Issue.new(:start_date => '2012-10-09', :due_date => '2012-10-15')
1432 issue.reschedule_on '2012-10-13'.to_date
1433 assert_equal '2012-10-13'.to_date, issue.start_date
1434 assert_equal '2012-10-19'.to_date, issue.due_date
1435 end
1436
1437 with_settings :non_working_week_days => %w(6 7) do
1438 issue = Issue.new(:start_date => '2012-10-09', :due_date => '2012-10-19') # 8 working days
1439 issue.reschedule_on '2012-10-11'.to_date
1440 assert_equal '2012-10-11'.to_date, issue.start_date
1441 assert_equal '2012-10-23'.to_date, issue.due_date
1442
1443 issue = Issue.new(:start_date => '2012-10-09', :due_date => '2012-10-19')
1444 issue.reschedule_on '2012-10-13'.to_date
1445 assert_equal '2012-10-15'.to_date, issue.start_date
1446 assert_equal '2012-10-25'.to_date, issue.due_date
1447 end
1448 end
1449
1450 def test_rescheduling_an_issue_to_a_later_due_date_should_reschedule_following_issue
1451 issue1 = Issue.generate!(:start_date => '2012-10-15', :due_date => '2012-10-17')
1452 issue2 = Issue.generate!(:start_date => '2012-10-15', :due_date => '2012-10-17')
1453 IssueRelation.create!(:issue_from => issue1, :issue_to => issue2,
1454 :relation_type => IssueRelation::TYPE_PRECEDES)
1455 assert_equal Date.parse('2012-10-18'), issue2.reload.start_date
1456
1457 issue1.due_date = '2012-10-23'
1458 issue1.save!
1459 issue2.reload
1460 assert_equal Date.parse('2012-10-24'), issue2.start_date
1461 assert_equal Date.parse('2012-10-26'), issue2.due_date
1462 end
1463
1464 def test_rescheduling_an_issue_to_an_earlier_due_date_should_reschedule_following_issue
1465 issue1 = Issue.generate!(:start_date => '2012-10-15', :due_date => '2012-10-17')
1466 issue2 = Issue.generate!(:start_date => '2012-10-15', :due_date => '2012-10-17')
1467 IssueRelation.create!(:issue_from => issue1, :issue_to => issue2,
1468 :relation_type => IssueRelation::TYPE_PRECEDES)
1469 assert_equal Date.parse('2012-10-18'), issue2.reload.start_date
1470
1471 issue1.start_date = '2012-09-17'
1472 issue1.due_date = '2012-09-18'
1473 issue1.save!
1474 issue2.reload
1475 assert_equal Date.parse('2012-09-19'), issue2.start_date
1476 assert_equal Date.parse('2012-09-21'), issue2.due_date
1477 end
1478
1479 def test_rescheduling_reschedule_following_issue_earlier_should_consider_other_preceding_issues
1480 issue1 = Issue.generate!(:start_date => '2012-10-15', :due_date => '2012-10-17')
1481 issue2 = Issue.generate!(:start_date => '2012-10-15', :due_date => '2012-10-17')
1482 issue3 = Issue.generate!(:start_date => '2012-10-01', :due_date => '2012-10-02')
1483 IssueRelation.create!(:issue_from => issue1, :issue_to => issue2,
1484 :relation_type => IssueRelation::TYPE_PRECEDES)
1485 IssueRelation.create!(:issue_from => issue3, :issue_to => issue2,
1486 :relation_type => IssueRelation::TYPE_PRECEDES)
1487 assert_equal Date.parse('2012-10-18'), issue2.reload.start_date
1488
1489 issue1.start_date = '2012-09-17'
1490 issue1.due_date = '2012-09-18'
1491 issue1.save!
1492 issue2.reload
1493 # Issue 2 must start after Issue 3
1494 assert_equal Date.parse('2012-10-03'), issue2.start_date
1495 assert_equal Date.parse('2012-10-05'), issue2.due_date
1496 end
1497
1498 def test_rescheduling_a_stale_issue_should_not_raise_an_error
1499 with_settings :non_working_week_days => [] do
1500 stale = Issue.find(1)
1501 issue = Issue.find(1)
1502 issue.subject = "Updated"
1503 issue.save!
1504 date = 10.days.from_now.to_date
1505 assert_nothing_raised do
1506 stale.reschedule_on!(date)
1507 end
1508 assert_equal date, stale.reload.start_date
1509 end
1510 end
1511
1512 def test_child_issue_should_consider_parent_soonest_start_on_create
1513 set_language_if_valid 'en'
1514 issue1 = Issue.generate!(:start_date => '2012-10-15', :due_date => '2012-10-17')
1515 issue2 = Issue.generate!(:start_date => '2012-10-18', :due_date => '2012-10-20')
1516 IssueRelation.create!(:issue_from => issue1, :issue_to => issue2,
1517 :relation_type => IssueRelation::TYPE_PRECEDES)
1518 issue1.reload
1519 issue2.reload
1520 assert_equal Date.parse('2012-10-18'), issue2.start_date
1521
1522 child = Issue.new(:parent_issue_id => issue2.id, :start_date => '2012-10-16',
1523 :project_id => 1, :tracker_id => 1, :status_id => 1, :subject => 'Child', :author_id => 1)
1524 assert !child.valid?
1525 assert_include 'Start date is invalid', child.errors.full_messages
1526 assert_equal Date.parse('2012-10-18'), child.soonest_start
1527 child.start_date = '2012-10-18'
1528 assert child.save
1529 end
1530
1531 def test_setting_parent_to_a_dependent_issue_should_not_validate
1532 set_language_if_valid 'en'
1533 issue1 = Issue.generate!
1534 issue2 = Issue.generate!
1535 issue3 = Issue.generate!
1536 IssueRelation.create!(:issue_from => issue1, :issue_to => issue2, :relation_type => IssueRelation::TYPE_PRECEDES)
1537 IssueRelation.create!(:issue_from => issue3, :issue_to => issue1, :relation_type => IssueRelation::TYPE_PRECEDES)
1538 issue3.reload
1539 issue3.parent_issue_id = issue2.id
1540 assert !issue3.valid?
1541 assert_include 'Parent task is invalid', issue3.errors.full_messages
1542 end
1543
1544 def test_setting_parent_should_not_allow_circular_dependency
1545 set_language_if_valid 'en'
1546 issue1 = Issue.generate!
1547 issue2 = Issue.generate!
1548 IssueRelation.create!(:issue_from => issue1, :issue_to => issue2, :relation_type => IssueRelation::TYPE_PRECEDES)
1549 issue3 = Issue.generate!
1550 issue2.reload
1551 issue2.parent_issue_id = issue3.id
1552 issue2.save!
1553 issue4 = Issue.generate!
1554 IssueRelation.create!(:issue_from => issue3, :issue_to => issue4, :relation_type => IssueRelation::TYPE_PRECEDES)
1555 issue4.reload
1556 issue4.parent_issue_id = issue1.id
1557 assert !issue4.valid?
1558 assert_include 'Parent task is invalid', issue4.errors.full_messages
1559 end
1560
1561 def test_overdue
1562 assert Issue.new(:due_date => 1.day.ago.to_date).overdue?
1563 assert !Issue.new(:due_date => Date.today).overdue?
1564 assert !Issue.new(:due_date => 1.day.from_now.to_date).overdue?
1565 assert !Issue.new(:due_date => nil).overdue?
1566 assert !Issue.new(:due_date => 1.day.ago.to_date,
1567 :status => IssueStatus.where(:is_closed => true).first
1568 ).overdue?
1569 end
1570
1571 test "#behind_schedule? should be false if the issue has no start_date" do
1572 assert !Issue.new(:start_date => nil,
1573 :due_date => 1.day.from_now.to_date,
1574 :done_ratio => 0).behind_schedule?
1575 end
1576
1577 test "#behind_schedule? should be false if the issue has no end_date" do
1578 assert !Issue.new(:start_date => 1.day.from_now.to_date,
1579 :due_date => nil,
1580 :done_ratio => 0).behind_schedule?
1581 end
1582
1583 test "#behind_schedule? should be false if the issue has more done than it's calendar time" do
1584 assert !Issue.new(:start_date => 50.days.ago.to_date,
1585 :due_date => 50.days.from_now.to_date,
1586 :done_ratio => 90).behind_schedule?
1587 end
1588
1589 test "#behind_schedule? should be true if the issue hasn't been started at all" do
1590 assert Issue.new(:start_date => 1.day.ago.to_date,
1591 :due_date => 1.day.from_now.to_date,
1592 :done_ratio => 0).behind_schedule?
1593 end
1594
1595 test "#behind_schedule? should be true if the issue has used more calendar time than it's done ratio" do
1596 assert Issue.new(:start_date => 100.days.ago.to_date,
1597 :due_date => Date.today,
1598 :done_ratio => 90).behind_schedule?
1599 end
1600
1601 test "#assignable_users should be Users" do
1602 assert_kind_of User, Issue.find(1).assignable_users.first
1603 end
1604
1605 test "#assignable_users should include the issue author" do
1606 non_project_member = User.generate!
1607 issue = Issue.generate!(:author => non_project_member)
1608
1609 assert issue.assignable_users.include?(non_project_member)
1610 end
1611
1612 test "#assignable_users should include the current assignee" do
1613 user = User.generate!
1614 issue = Issue.generate!(:assigned_to => user)
1615 user.lock!
1616
1617 assert Issue.find(issue.id).assignable_users.include?(user)
1618 end
1619
1620 test "#assignable_users should not show the issue author twice" do
1621 assignable_user_ids = Issue.find(1).assignable_users.collect(&:id)
1622 assert_equal 2, assignable_user_ids.length
1623
1624 assignable_user_ids.each do |user_id|
1625 assert_equal 1, assignable_user_ids.select {|i| i == user_id}.length,
1626 "User #{user_id} appears more or less than once"
1627 end
1628 end
1629
1630 test "#assignable_users with issue_group_assignment should include groups" do
1631 issue = Issue.new(:project => Project.find(2))
1632
1633 with_settings :issue_group_assignment => '1' do
1634 assert_equal %w(Group User), issue.assignable_users.map {|a| a.class.name}.uniq.sort
1635 assert issue.assignable_users.include?(Group.find(11))
1636 end
1637 end
1638
1639 test "#assignable_users without issue_group_assignment should not include groups" do
1640 issue = Issue.new(:project => Project.find(2))
1641
1642 with_settings :issue_group_assignment => '0' do
1643 assert_equal %w(User), issue.assignable_users.map {|a| a.class.name}.uniq.sort
1644 assert !issue.assignable_users.include?(Group.find(11))
1645 end
1646 end
1647
1648 def test_create_should_send_email_notification
1649 ActionMailer::Base.deliveries.clear
1650 issue = Issue.new(:project_id => 1, :tracker_id => 1,
1651 :author_id => 3, :status_id => 1,
1652 :priority => IssuePriority.all.first,
1653 :subject => 'test_create', :estimated_hours => '1:30')
1654
1655 assert issue.save
1656 assert_equal 1, ActionMailer::Base.deliveries.size
1657 end
1658
1659 def test_stale_issue_should_not_send_email_notification
1660 ActionMailer::Base.deliveries.clear
1661 issue = Issue.find(1)
1662 stale = Issue.find(1)
1663
1664 issue.init_journal(User.find(1))
1665 issue.subject = 'Subjet update'
1666 assert issue.save
1667 assert_equal 1, ActionMailer::Base.deliveries.size
1668 ActionMailer::Base.deliveries.clear
1669
1670 stale.init_journal(User.find(1))
1671 stale.subject = 'Another subjet update'
1672 assert_raise ActiveRecord::StaleObjectError do
1673 stale.save
1674 end
1675 assert ActionMailer::Base.deliveries.empty?
1676 end
1677
1678 def test_journalized_description
1679 IssueCustomField.delete_all
1680
1681 i = Issue.first
1682 old_description = i.description
1683 new_description = "This is the new description"
1684
1685 i.init_journal(User.find(2))
1686 i.description = new_description
1687 assert_difference 'Journal.count', 1 do
1688 assert_difference 'JournalDetail.count', 1 do
1689 i.save!
1690 end
1691 end
1692
1693 detail = JournalDetail.first(:order => 'id DESC')
1694 assert_equal i, detail.journal.journalized
1695 assert_equal 'attr', detail.property
1696 assert_equal 'description', detail.prop_key
1697 assert_equal old_description, detail.old_value
1698 assert_equal new_description, detail.value
1699 end
1700
1701 def test_blank_descriptions_should_not_be_journalized
1702 IssueCustomField.delete_all
1703 Issue.update_all("description = NULL", "id=1")
1704
1705 i = Issue.find(1)
1706 i.init_journal(User.find(2))
1707 i.subject = "blank description"
1708 i.description = "\r\n"
1709
1710 assert_difference 'Journal.count', 1 do
1711 assert_difference 'JournalDetail.count', 1 do
1712 i.save!
1713 end
1714 end
1715 end
1716
1717 def test_journalized_multi_custom_field
1718 field = IssueCustomField.create!(:name => 'filter', :field_format => 'list',
1719 :is_filter => true, :is_for_all => true,
1720 :tracker_ids => [1],
1721 :possible_values => ['value1', 'value2', 'value3'],
1722 :multiple => true)
1723
1724 issue = Issue.create!(:project_id => 1, :tracker_id => 1,
1725 :subject => 'Test', :author_id => 1)
1726
1727 assert_difference 'Journal.count' do
1728 assert_difference 'JournalDetail.count' do
1729 issue.init_journal(User.first)
1730 issue.custom_field_values = {field.id => ['value1']}
1731 issue.save!
1732 end
1733 assert_difference 'JournalDetail.count' do
1734 issue.init_journal(User.first)
1735 issue.custom_field_values = {field.id => ['value1', 'value2']}
1736 issue.save!
1737 end
1738 assert_difference 'JournalDetail.count', 2 do
1739 issue.init_journal(User.first)
1740 issue.custom_field_values = {field.id => ['value3', 'value2']}
1741 issue.save!
1742 end
1743 assert_difference 'JournalDetail.count', 2 do
1744 issue.init_journal(User.first)
1745 issue.custom_field_values = {field.id => nil}
1746 issue.save!
1747 end
1748 end
1749 end
1750
1751 def test_description_eol_should_be_normalized
1752 i = Issue.new(:description => "CR \r LF \n CRLF \r\n")
1753 assert_equal "CR \r\n LF \r\n CRLF \r\n", i.description
1754 end
1755
1756 def test_saving_twice_should_not_duplicate_journal_details
1757 i = Issue.first
1758 i.init_journal(User.find(2), 'Some notes')
1759 # initial changes
1760 i.subject = 'New subject'
1761 i.done_ratio = i.done_ratio + 10
1762 assert_difference 'Journal.count' do
1763 assert i.save
1764 end
1765 # 1 more change
1766 i.priority = IssuePriority.where("id <> ?", i.priority_id).first
1767 assert_no_difference 'Journal.count' do
1768 assert_difference 'JournalDetail.count', 1 do
1769 i.save
1770 end
1771 end
1772 # no more change
1773 assert_no_difference 'Journal.count' do
1774 assert_no_difference 'JournalDetail.count' do
1775 i.save
1776 end
1777 end
1778 end
1779
1780 def test_all_dependent_issues
1781 IssueRelation.delete_all
1782 assert IssueRelation.create!(:issue_from => Issue.find(1),
1783 :issue_to => Issue.find(2),
1784 :relation_type => IssueRelation::TYPE_PRECEDES)
1785 assert IssueRelation.create!(:issue_from => Issue.find(2),
1786 :issue_to => Issue.find(3),
1787 :relation_type => IssueRelation::TYPE_PRECEDES)
1788 assert IssueRelation.create!(:issue_from => Issue.find(3),
1789 :issue_to => Issue.find(8),
1790 :relation_type => IssueRelation::TYPE_PRECEDES)
1791
1792 assert_equal [2, 3, 8], Issue.find(1).all_dependent_issues.collect(&:id).sort
1793 end
1794
1795 def test_all_dependent_issues_with_persistent_circular_dependency
1796 IssueRelation.delete_all
1797 assert IssueRelation.create!(:issue_from => Issue.find(1),
1798 :issue_to => Issue.find(2),
1799 :relation_type => IssueRelation::TYPE_PRECEDES)
1800 assert IssueRelation.create!(:issue_from => Issue.find(2),
1801 :issue_to => Issue.find(3),
1802 :relation_type => IssueRelation::TYPE_PRECEDES)
1803
1804 r = IssueRelation.create!(:issue_from => Issue.find(3),
1805 :issue_to => Issue.find(7),
1806 :relation_type => IssueRelation::TYPE_PRECEDES)
1807 IssueRelation.update_all("issue_to_id = 1", ["id = ?", r.id])
1808
1809 assert_equal [2, 3], Issue.find(1).all_dependent_issues.collect(&:id).sort
1810 end
1811
1812 def test_all_dependent_issues_with_persistent_multiple_circular_dependencies
1813 IssueRelation.delete_all
1814 assert IssueRelation.create!(:issue_from => Issue.find(1),
1815 :issue_to => Issue.find(2),
1816 :relation_type => IssueRelation::TYPE_RELATES)
1817 assert IssueRelation.create!(:issue_from => Issue.find(2),
1818 :issue_to => Issue.find(3),
1819 :relation_type => IssueRelation::TYPE_RELATES)
1820 assert IssueRelation.create!(:issue_from => Issue.find(3),
1821 :issue_to => Issue.find(8),
1822 :relation_type => IssueRelation::TYPE_RELATES)
1823
1824 r = IssueRelation.create!(:issue_from => Issue.find(8),
1825 :issue_to => Issue.find(7),
1826 :relation_type => IssueRelation::TYPE_RELATES)
1827 IssueRelation.update_all("issue_to_id = 2", ["id = ?", r.id])
1828
1829 r = IssueRelation.create!(:issue_from => Issue.find(3),
1830 :issue_to => Issue.find(7),
1831 :relation_type => IssueRelation::TYPE_RELATES)
1832 IssueRelation.update_all("issue_to_id = 1", ["id = ?", r.id])
1833
1834 assert_equal [2, 3, 8], Issue.find(1).all_dependent_issues.collect(&:id).sort
1835 end
1836
1837 test "#done_ratio should use the issue_status according to Setting.issue_done_ratio" do
1838 @issue = Issue.find(1)
1839 @issue_status = IssueStatus.find(1)
1840 @issue_status.update_attribute(:default_done_ratio, 50)
1841 @issue2 = Issue.find(2)
1842 @issue_status2 = IssueStatus.find(2)
1843 @issue_status2.update_attribute(:default_done_ratio, 0)
1844
1845 with_settings :issue_done_ratio => 'issue_field' do
1846 assert_equal 0, @issue.done_ratio
1847 assert_equal 30, @issue2.done_ratio
1848 end
1849
1850 with_settings :issue_done_ratio => 'issue_status' do
1851 assert_equal 50, @issue.done_ratio
1852 assert_equal 0, @issue2.done_ratio
1853 end
1854 end
1855
1856 test "#update_done_ratio_from_issue_status should update done_ratio according to Setting.issue_done_ratio" do
1857 @issue = Issue.find(1)
1858 @issue_status = IssueStatus.find(1)
1859 @issue_status.update_attribute(:default_done_ratio, 50)
1860 @issue2 = Issue.find(2)
1861 @issue_status2 = IssueStatus.find(2)
1862 @issue_status2.update_attribute(:default_done_ratio, 0)
1863
1864 with_settings :issue_done_ratio => 'issue_field' do
1865 @issue.update_done_ratio_from_issue_status
1866 @issue2.update_done_ratio_from_issue_status
1867
1868 assert_equal 0, @issue.read_attribute(:done_ratio)
1869 assert_equal 30, @issue2.read_attribute(:done_ratio)
1870 end
1871
1872 with_settings :issue_done_ratio => 'issue_status' do
1873 @issue.update_done_ratio_from_issue_status
1874 @issue2.update_done_ratio_from_issue_status
1875
1876 assert_equal 50, @issue.read_attribute(:done_ratio)
1877 assert_equal 0, @issue2.read_attribute(:done_ratio)
1878 end
1879 end
1880
1881 test "#by_tracker" do
1882 User.current = User.anonymous
1883 groups = Issue.by_tracker(Project.find(1))
1884 assert_equal 3, groups.size
1885 assert_equal 7, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1886 end
1887
1888 test "#by_version" do
1889 User.current = User.anonymous
1890 groups = Issue.by_version(Project.find(1))
1891 assert_equal 3, groups.size
1892 assert_equal 3, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1893 end
1894
1895 test "#by_priority" do
1896 User.current = User.anonymous
1897 groups = Issue.by_priority(Project.find(1))
1898 assert_equal 4, groups.size
1899 assert_equal 7, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1900 end
1901
1902 test "#by_category" do
1903 User.current = User.anonymous
1904 groups = Issue.by_category(Project.find(1))
1905 assert_equal 2, groups.size
1906 assert_equal 3, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1907 end
1908
1909 test "#by_assigned_to" do
1910 User.current = User.anonymous
1911 groups = Issue.by_assigned_to(Project.find(1))
1912 assert_equal 2, groups.size
1913 assert_equal 2, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1914 end
1915
1916 test "#by_author" do
1917 User.current = User.anonymous
1918 groups = Issue.by_author(Project.find(1))
1919 assert_equal 4, groups.size
1920 assert_equal 7, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1921 end
1922
1923 test "#by_subproject" do
1924 User.current = User.anonymous
1925 groups = Issue.by_subproject(Project.find(1))
1926 # Private descendant not visible
1927 assert_equal 1, groups.size
1928 assert_equal 2, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1929 end
1930
1931 def test_recently_updated_scope
1932 #should return the last updated issue
1933 assert_equal Issue.reorder("updated_on DESC").first, Issue.recently_updated.limit(1).first
1934 end
1935
1936 def test_on_active_projects_scope
1937 assert Project.find(2).archive
1938
1939 before = Issue.on_active_project.length
1940 # test inclusion to results
1941 issue = Issue.generate!(:tracker => Project.find(2).trackers.first)
1942 assert_equal before + 1, Issue.on_active_project.length
1943
1944 # Move to an archived project
1945 issue.project = Project.find(2)
1946 assert issue.save
1947 assert_equal before, Issue.on_active_project.length
1948 end
1949
1950 test "Issue#recipients should include project recipients" do
1951 issue = Issue.generate!
1952 assert issue.project.recipients.present?
1953 issue.project.recipients.each do |project_recipient|
1954 assert issue.recipients.include?(project_recipient)
1955 end
1956 end
1957
1958 test "Issue#recipients should include the author if the author is active" do
1959 issue = Issue.generate!(:author => User.generate!)
1960 assert issue.author, "No author set for Issue"
1961 assert issue.recipients.include?(issue.author.mail)
1962 end
1963
1964 test "Issue#recipients should include the assigned to user if the assigned to user is active" do
1965 issue = Issue.generate!(:assigned_to => User.generate!)
1966 assert issue.assigned_to, "No assigned_to set for Issue"
1967 assert issue.recipients.include?(issue.assigned_to.mail)
1968 end
1969
1970 test "Issue#recipients should not include users who opt out of all email" do
1971 issue = Issue.generate!(:author => User.generate!)
1972 issue.author.update_attribute(:mail_notification, :none)
1973 assert !issue.recipients.include?(issue.author.mail)
1974 end
1975
1976 test "Issue#recipients should not include the issue author if they are only notified of assigned issues" do
1977 issue = Issue.generate!(:author => User.generate!)
1978 issue.author.update_attribute(:mail_notification, :only_assigned)
1979 assert !issue.recipients.include?(issue.author.mail)
1980 end
1981
1982 test "Issue#recipients should not include the assigned user if they are only notified of owned issues" do
1983 issue = Issue.generate!(:assigned_to => User.generate!)
1984 issue.assigned_to.update_attribute(:mail_notification, :only_owner)
1985 assert !issue.recipients.include?(issue.assigned_to.mail)
1986 end
1987
1988 def test_last_journal_id_with_journals_should_return_the_journal_id
1989 assert_equal 2, Issue.find(1).last_journal_id
1990 end
1991
1992 def test_last_journal_id_without_journals_should_return_nil
1993 assert_nil Issue.find(3).last_journal_id
1994 end
1995
1996 def test_journals_after_should_return_journals_with_greater_id
1997 assert_equal [Journal.find(2)], Issue.find(1).journals_after('1')
1998 assert_equal [], Issue.find(1).journals_after('2')
1999 end
2000
2001 def test_journals_after_with_blank_arg_should_return_all_journals
2002 assert_equal [Journal.find(1), Journal.find(2)], Issue.find(1).journals_after('')
2003 end
2004
2005 def test_css_classes_should_include_tracker
2006 issue = Issue.new(:tracker => Tracker.find(2))
2007 classes = issue.css_classes.split(' ')
2008 assert_include 'tracker-2', classes
2009 end
2010
2011 def test_css_classes_should_include_priority
2012 issue = Issue.new(:priority => IssuePriority.find(8))
2013 classes = issue.css_classes.split(' ')
2014 assert_include 'priority-8', classes
2015 assert_include 'priority-highest', classes
2016 end
2017
2018 def test_save_attachments_with_hash_should_save_attachments_in_keys_order
2019 set_tmp_attachments_directory
2020 issue = Issue.generate!
2021 issue.save_attachments({
2022 'p0' => {'file' => mock_file_with_options(:original_filename => 'upload')},
2023 '3' => {'file' => mock_file_with_options(:original_filename => 'bar')},
2024 '1' => {'file' => mock_file_with_options(:original_filename => 'foo')}
2025 })
2026 issue.attach_saved_attachments
2027
2028 assert_equal 3, issue.reload.attachments.count
2029 assert_equal %w(upload foo bar), issue.attachments.map(&:filename)
2030 end
2031
2032 def test_closed_on_should_be_nil_when_creating_an_open_issue
2033 issue = Issue.generate!(:status_id => 1).reload
2034 assert !issue.closed?
2035 assert_nil issue.closed_on
2036 end
2037
2038 def test_closed_on_should_be_set_when_creating_a_closed_issue
2039 issue = Issue.generate!(:status_id => 5).reload
2040 assert issue.closed?
2041 assert_not_nil issue.closed_on
2042 assert_equal issue.updated_on, issue.closed_on
2043 assert_equal issue.created_on, issue.closed_on
2044 end
2045
2046 def test_closed_on_should_be_nil_when_updating_an_open_issue
2047 issue = Issue.find(1)
2048 issue.subject = 'Not closed yet'
2049 issue.save!
2050 issue.reload
2051 assert_nil issue.closed_on
2052 end
2053
2054 def test_closed_on_should_be_set_when_closing_an_open_issue
2055 issue = Issue.find(1)
2056 issue.subject = 'Now closed'
2057 issue.status_id = 5
2058 issue.save!
2059 issue.reload
2060 assert_not_nil issue.closed_on
2061 assert_equal issue.updated_on, issue.closed_on
2062 end
2063
2064 def test_closed_on_should_not_be_updated_when_updating_a_closed_issue
2065 issue = Issue.open(false).first
2066 was_closed_on = issue.closed_on
2067 assert_not_nil was_closed_on
2068 issue.subject = 'Updating a closed issue'
2069 issue.save!
2070 issue.reload
2071 assert_equal was_closed_on, issue.closed_on
2072 end
2073
2074 def test_closed_on_should_be_preserved_when_reopening_a_closed_issue
2075 issue = Issue.open(false).first
2076 was_closed_on = issue.closed_on
2077 assert_not_nil was_closed_on
2078 issue.subject = 'Reopening a closed issue'
2079 issue.status_id = 1
2080 issue.save!
2081 issue.reload
2082 assert !issue.closed?
2083 assert_equal was_closed_on, issue.closed_on
2084 end
2085
2086 def test_status_was_should_return_nil_for_new_issue
2087 issue = Issue.new
2088 assert_nil issue.status_was
2089 end
2090
2091 def test_status_was_should_return_status_before_change
2092 issue = Issue.find(1)
2093 issue.status = IssueStatus.find(2)
2094 assert_equal IssueStatus.find(1), issue.status_was
2095 end
2096
2097 def test_status_was_should_be_reset_on_save
2098 issue = Issue.find(1)
2099 issue.status = IssueStatus.find(2)
2100 assert_equal IssueStatus.find(1), issue.status_was
2101 assert issue.save!
2102 assert_equal IssueStatus.find(2), issue.status_was
2103 end
2104 end