comparison test/unit/issue_test.rb @ 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 1 # Redmine - project management software
2 # Copyright (C) 2006-2012 Jean-Philippe Lang 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 # 3 #
4 # This program is free software; you can redistribute it and/or 4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License 5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2 6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version. 7 # of the License, or (at your option) any later version.
33 33
34 def teardown 34 def teardown
35 User.current = nil 35 User.current = nil
36 end 36 end
37 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
38 def test_create 51 def test_create
39 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3, 52 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3,
40 :status_id => 1, :priority => IssuePriority.all.first, 53 :status_id => 1, :priority => IssuePriority.all.first,
41 :subject => 'test_create', 54 :subject => 'test_create',
42 :description => 'IssueTest#test_create', :estimated_hours => '1:30') 55 :description => 'IssueTest#test_create', :estimated_hours => '1:30')
75 def test_due_date_lesser_than_start_date_should_not_validate 88 def test_due_date_lesser_than_start_date_should_not_validate
76 set_language_if_valid 'en' 89 set_language_if_valid 'en'
77 issue = Issue.new(:start_date => '2012-10-06', :due_date => '2012-10-02') 90 issue = Issue.new(:start_date => '2012-10-06', :due_date => '2012-10-02')
78 assert !issue.valid? 91 assert !issue.valid?
79 assert_include 'Due date must be greater than start date', issue.errors.full_messages 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
80 end 102 end
81 103
82 def test_create_with_required_custom_field 104 def test_create_with_required_custom_field
83 set_language_if_valid 'en' 105 set_language_if_valid 'en'
84 field = IssueCustomField.find_by_name('Database') 106 field = IssueCustomField.find_by_name('Database')
298 def test_open_scope_with_arg 320 def test_open_scope_with_arg
299 issues = Issue.open(false).all 321 issues = Issue.open(false).all
300 assert_equal issues, issues.select(&:closed?) 322 assert_equal issues, issues.select(&:closed?)
301 end 323 end
302 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
303 def test_errors_full_messages_should_include_custom_fields_errors 335 def test_errors_full_messages_should_include_custom_fields_errors
304 field = IssueCustomField.find_by_name('Database') 336 field = IssueCustomField.find_by_name('Database')
305 337
306 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, 338 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1,
307 :status_id => 1, :subject => 'test_create', 339 :status_id => 1, :subject => 'test_create',
398 attributes['custom_field_values'] = { '1' => 'MySQL' } 430 attributes['custom_field_values'] = { '1' => 'MySQL' }
399 attributes['tracker_id'] = '1' 431 attributes['tracker_id'] = '1'
400 issue = Issue.new(:project => Project.find(1)) 432 issue = Issue.new(:project => Project.find(1))
401 issue.attributes = attributes 433 issue.attributes = attributes
402 assert_equal 'MySQL', issue.custom_field_value(1) 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)
403 end 450 end
404 451
405 def test_should_update_issue_with_disabled_tracker 452 def test_should_update_issue_with_disabled_tracker
406 p = Project.find(1) 453 p = Project.find(1)
407 issue = Issue.find(1) 454 issue = Issue.find(1)
800 child_copy = copy.children.detect {|c| c.subject == 'Child1'} 847 child_copy = copy.children.detect {|c| c.subject == 'Child1'}
801 assert_equal %w(Child11), child_copy.children.map(&:subject).sort 848 assert_equal %w(Child11), child_copy.children.map(&:subject).sort
802 assert_equal copy.author, child_copy.author 849 assert_equal copy.author, child_copy.author
803 end 850 end
804 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
805 def test_copy_should_copy_subtasks_to_target_project 895 def test_copy_should_copy_subtasks_to_target_project
806 issue = Issue.generate_with_descendants! 896 issue = Issue.generate_with_descendants!
807 897
808 copy = issue.copy(:project_id => 3) 898 copy = issue.copy(:project_id => 3)
809 assert_difference 'Issue.count', 1+issue.descendants.count do 899 assert_difference 'Issue.count', 1+issue.descendants.count do
874 :relation_type => IssueRelation::TYPE_DUPLICATES) 964 :relation_type => IssueRelation::TYPE_DUPLICATES)
875 965
876 assert issue1.reload.duplicates.include?(issue2) 966 assert issue1.reload.duplicates.include?(issue2)
877 967
878 # Closing issue 1 968 # Closing issue 1
879 issue1.init_journal(User.find(:first), "Closing issue1") 969 issue1.init_journal(User.first, "Closing issue1")
880 issue1.status = IssueStatus.find :first, :conditions => {:is_closed => true} 970 issue1.status = IssueStatus.where(:is_closed => true).first
881 assert issue1.save 971 assert issue1.save
882 # 2 and 3 should be also closed 972 # 2 and 3 should be also closed
883 assert issue2.reload.closed? 973 assert issue2.reload.closed?
884 assert issue3.reload.closed? 974 assert issue3.reload.closed?
885 end 975 end
893 :relation_type => IssueRelation::TYPE_DUPLICATES) 983 :relation_type => IssueRelation::TYPE_DUPLICATES)
894 # 2 is a dup of 1 but 1 is not a duplicate of 2 984 # 2 is a dup of 1 but 1 is not a duplicate of 2
895 assert !issue2.reload.duplicates.include?(issue1) 985 assert !issue2.reload.duplicates.include?(issue1)
896 986
897 # Closing issue 2 987 # Closing issue 2
898 issue2.init_journal(User.find(:first), "Closing issue2") 988 issue2.init_journal(User.first, "Closing issue2")
899 issue2.status = IssueStatus.find :first, :conditions => {:is_closed => true} 989 issue2.status = IssueStatus.where(:is_closed => true).first
900 assert issue2.save 990 assert issue2.save
901 # 1 should not be also closed 991 # 1 should not be also closed
902 assert !issue1.reload.closed? 992 assert !issue1.reload.closed?
903 end 993 end
904 994
1108 assert_equal Tracker.find(2), copy.tracker 1198 assert_equal Tracker.find(2), copy.tracker
1109 # Custom field #2 is not associated with target tracker 1199 # Custom field #2 is not associated with target tracker
1110 assert_nil copy.custom_value_for(2) 1200 assert_nil copy.custom_value_for(2)
1111 end 1201 end
1112 1202
1113 context "#copy" do 1203 test "#copy should not create a journal" do
1114 setup do 1204 copy = Issue.find(1).copy(:project_id => 3, :tracker_id => 2, :assigned_to_id => 3)
1115 @issue = Issue.find(1) 1205 copy.save!
1116 end 1206 assert_equal 0, copy.reload.journals.size
1117 1207 end
1118 should "not create a journal" do 1208
1119 copy = @issue.copy(:project_id => 3, :tracker_id => 2, :assigned_to_id => 3) 1209 test "#copy should allow assigned_to changes" do
1120 copy.save! 1210 copy = Issue.find(1).copy(:project_id => 3, :tracker_id => 2, :assigned_to_id => 3)
1121 assert_equal 0, copy.reload.journals.size 1211 assert_equal 3, copy.assigned_to_id
1122 end 1212 end
1123 1213
1124 should "allow assigned_to changes" do 1214 test "#copy should allow status changes" do
1125 copy = @issue.copy(:project_id => 3, :tracker_id => 2, :assigned_to_id => 3) 1215 copy = Issue.find(1).copy(:project_id => 3, :tracker_id => 2, :status_id => 2)
1126 assert_equal 3, copy.assigned_to_id 1216 assert_equal 2, copy.status_id
1127 end 1217 end
1128 1218
1129 should "allow status changes" do 1219 test "#copy should allow start date changes" do
1130 copy = @issue.copy(:project_id => 3, :tracker_id => 2, :status_id => 2) 1220 date = Date.today
1131 assert_equal 2, copy.status_id 1221 copy = Issue.find(1).copy(:project_id => 3, :tracker_id => 2, :start_date => date)
1132 end 1222 assert_equal date, copy.start_date
1133 1223 end
1134 should "allow start date changes" do 1224
1135 date = Date.today 1225 test "#copy should allow due date changes" do
1136 copy = @issue.copy(:project_id => 3, :tracker_id => 2, :start_date => date) 1226 date = Date.today
1137 assert_equal date, copy.start_date 1227 copy = Issue.find(1).copy(:project_id => 3, :tracker_id => 2, :due_date => date)
1138 end 1228 assert_equal date, copy.due_date
1139 1229 end
1140 should "allow due date changes" do 1230
1141 date = Date.today 1231 test "#copy should set current user as author" do
1142 copy = @issue.copy(:project_id => 3, :tracker_id => 2, :due_date => date) 1232 User.current = User.find(9)
1143 assert_equal date, copy.due_date 1233 copy = Issue.find(1).copy(:project_id => 3, :tracker_id => 2)
1144 end 1234 assert_equal User.current, copy.author
1145 1235 end
1146 should "set current user as author" do 1236
1147 User.current = User.find(9) 1237 test "#copy should create a journal with notes" do
1148 copy = @issue.copy(:project_id => 3, :tracker_id => 2) 1238 date = Date.today
1149 assert_equal User.current, copy.author 1239 notes = "Notes added when copying"
1150 end 1240 copy = Issue.find(1).copy(:project_id => 3, :tracker_id => 2, :start_date => date)
1151 1241 copy.init_journal(User.current, notes)
1152 should "create a journal with notes" do 1242 copy.save!
1153 date = Date.today 1243
1154 notes = "Notes added when copying" 1244 assert_equal 1, copy.journals.size
1155 copy = @issue.copy(:project_id => 3, :tracker_id => 2, :start_date => date) 1245 journal = copy.journals.first
1156 copy.init_journal(User.current, notes) 1246 assert_equal 0, journal.details.size
1157 copy.save! 1247 assert_equal notes, journal.notes
1158
1159 assert_equal 1, copy.journals.size
1160 journal = copy.journals.first
1161 assert_equal 0, journal.details.size
1162 assert_equal notes, journal.notes
1163 end
1164 end 1248 end
1165 1249
1166 def test_valid_parent_project 1250 def test_valid_parent_project
1167 issue = Issue.find(1) 1251 issue = Issue.find(1)
1168 issue_in_same_project = Issue.find(2) 1252 issue_in_same_project = Issue.find(2)
1423 end 1507 end
1424 assert_equal date, stale.reload.start_date 1508 assert_equal date, stale.reload.start_date
1425 end 1509 end
1426 end 1510 end
1427 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
1428 def test_overdue 1561 def test_overdue
1429 assert Issue.new(:due_date => 1.day.ago.to_date).overdue? 1562 assert Issue.new(:due_date => 1.day.ago.to_date).overdue?
1430 assert !Issue.new(:due_date => Date.today).overdue? 1563 assert !Issue.new(:due_date => Date.today).overdue?
1431 assert !Issue.new(:due_date => 1.day.from_now.to_date).overdue? 1564 assert !Issue.new(:due_date => 1.day.from_now.to_date).overdue?
1432 assert !Issue.new(:due_date => nil).overdue? 1565 assert !Issue.new(:due_date => nil).overdue?
1433 assert !Issue.new(:due_date => 1.day.ago.to_date, 1566 assert !Issue.new(:due_date => 1.day.ago.to_date,
1434 :status => IssueStatus.find(:first, 1567 :status => IssueStatus.where(:is_closed => true).first
1435 :conditions => {:is_closed => true})
1436 ).overdue? 1568 ).overdue?
1437 end 1569 end
1438 1570
1439 context "#behind_schedule?" do 1571 test "#behind_schedule? should be false if the issue has no start_date" do
1440 should "be false if the issue has no start_date" do 1572 assert !Issue.new(:start_date => nil,
1441 assert !Issue.new(:start_date => nil, 1573 :due_date => 1.day.from_now.to_date,
1442 :due_date => 1.day.from_now.to_date, 1574 :done_ratio => 0).behind_schedule?
1443 :done_ratio => 0).behind_schedule? 1575 end
1444 end 1576
1445 1577 test "#behind_schedule? should be false if the issue has no end_date" do
1446 should "be false if the issue has no end_date" do 1578 assert !Issue.new(:start_date => 1.day.from_now.to_date,
1447 assert !Issue.new(:start_date => 1.day.from_now.to_date, 1579 :due_date => nil,
1448 :due_date => nil, 1580 :done_ratio => 0).behind_schedule?
1449 :done_ratio => 0).behind_schedule? 1581 end
1450 end 1582
1451 1583 test "#behind_schedule? should be false if the issue has more done than it's calendar time" do
1452 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,
1453 assert !Issue.new(:start_date => 50.days.ago.to_date, 1585 :due_date => 50.days.from_now.to_date,
1454 :due_date => 50.days.from_now.to_date, 1586 :done_ratio => 90).behind_schedule?
1455 :done_ratio => 90).behind_schedule? 1587 end
1456 end 1588
1457 1589 test "#behind_schedule? should be true if the issue hasn't been started at all" do
1458 should "be true if the issue hasn't been started at all" do 1590 assert Issue.new(:start_date => 1.day.ago.to_date,
1459 assert Issue.new(:start_date => 1.day.ago.to_date, 1591 :due_date => 1.day.from_now.to_date,
1460 :due_date => 1.day.from_now.to_date, 1592 :done_ratio => 0).behind_schedule?
1461 :done_ratio => 0).behind_schedule? 1593 end
1462 end 1594
1463 1595 test "#behind_schedule? should be true if the issue has used more calendar time than it's done ratio" do
1464 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,
1465 assert Issue.new(:start_date => 100.days.ago.to_date, 1597 :due_date => Date.today,
1466 :due_date => Date.today, 1598 :done_ratio => 90).behind_schedule?
1467 :done_ratio => 90).behind_schedule? 1599 end
1468 end 1600
1469 end 1601 test "#assignable_users should be Users" do
1470 1602 assert_kind_of User, Issue.find(1).assignable_users.first
1471 context "#assignable_users" do 1603 end
1472 should "be Users" do 1604
1473 assert_kind_of User, Issue.find(1).assignable_users.first 1605 test "#assignable_users should include the issue author" do
1474 end 1606 non_project_member = User.generate!
1475 1607 issue = Issue.generate!(:author => non_project_member)
1476 should "include the issue author" do 1608
1477 non_project_member = User.generate! 1609 assert issue.assignable_users.include?(non_project_member)
1478 issue = Issue.generate!(:author => non_project_member) 1610 end
1479 1611
1480 assert issue.assignable_users.include?(non_project_member) 1612 test "#assignable_users should include the current assignee" do
1481 end 1613 user = User.generate!
1482 1614 issue = Issue.generate!(:assigned_to => user)
1483 should "include the current assignee" do 1615 user.lock!
1484 user = User.generate! 1616
1485 issue = Issue.generate!(:assigned_to => user) 1617 assert Issue.find(issue.id).assignable_users.include?(user)
1486 user.lock! 1618 end
1487 1619
1488 assert Issue.find(issue.id).assignable_users.include?(user) 1620 test "#assignable_users should not show the issue author twice" do
1489 end 1621 assignable_user_ids = Issue.find(1).assignable_users.collect(&:id)
1490 1622 assert_equal 2, assignable_user_ids.length
1491 should "not show the issue author twice" do 1623
1492 assignable_user_ids = Issue.find(1).assignable_users.collect(&:id) 1624 assignable_user_ids.each do |user_id|
1493 assert_equal 2, assignable_user_ids.length 1625 assert_equal 1, assignable_user_ids.select {|i| i == user_id}.length,
1494 1626 "User #{user_id} appears more or less than once"
1495 assignable_user_ids.each do |user_id| 1627 end
1496 assert_equal 1, assignable_user_ids.select {|i| i == user_id}.length, 1628 end
1497 "User #{user_id} appears more or less than once" 1629
1498 end 1630 test "#assignable_users with issue_group_assignment should include groups" do
1499 end 1631 issue = Issue.new(:project => Project.find(2))
1500 1632
1501 context "with issue_group_assignment" do 1633 with_settings :issue_group_assignment => '1' do
1502 should "include groups" do 1634 assert_equal %w(Group User), issue.assignable_users.map {|a| a.class.name}.uniq.sort
1503 issue = Issue.new(:project => Project.find(2)) 1635 assert issue.assignable_users.include?(Group.find(11))
1504 1636 end
1505 with_settings :issue_group_assignment => '1' do 1637 end
1506 assert_equal %w(Group User), issue.assignable_users.map {|a| a.class.name}.uniq.sort 1638
1507 assert issue.assignable_users.include?(Group.find(11)) 1639 test "#assignable_users without issue_group_assignment should not include groups" do
1508 end 1640 issue = Issue.new(:project => Project.find(2))
1509 end 1641
1510 end 1642 with_settings :issue_group_assignment => '0' do
1511 1643 assert_equal %w(User), issue.assignable_users.map {|a| a.class.name}.uniq.sort
1512 context "without issue_group_assignment" do 1644 assert !issue.assignable_users.include?(Group.find(11))
1513 should "not include groups" do
1514 issue = Issue.new(:project => Project.find(2))
1515
1516 with_settings :issue_group_assignment => '0' do
1517 assert_equal %w(User), issue.assignable_users.map {|a| a.class.name}.uniq.sort
1518 assert !issue.assignable_users.include?(Group.find(11))
1519 end
1520 end
1521 end 1645 end
1522 end 1646 end
1523 1647
1524 def test_create_should_send_email_notification 1648 def test_create_should_send_email_notification
1525 ActionMailer::Base.deliveries.clear 1649 ActionMailer::Base.deliveries.clear
1628 i = Issue.new(:description => "CR \r LF \n CRLF \r\n") 1752 i = Issue.new(:description => "CR \r LF \n CRLF \r\n")
1629 assert_equal "CR \r\n LF \r\n CRLF \r\n", i.description 1753 assert_equal "CR \r\n LF \r\n CRLF \r\n", i.description
1630 end 1754 end
1631 1755
1632 def test_saving_twice_should_not_duplicate_journal_details 1756 def test_saving_twice_should_not_duplicate_journal_details
1633 i = Issue.find(:first) 1757 i = Issue.first
1634 i.init_journal(User.find(2), 'Some notes') 1758 i.init_journal(User.find(2), 'Some notes')
1635 # initial changes 1759 # initial changes
1636 i.subject = 'New subject' 1760 i.subject = 'New subject'
1637 i.done_ratio = i.done_ratio + 10 1761 i.done_ratio = i.done_ratio + 10
1638 assert_difference 'Journal.count' do 1762 assert_difference 'Journal.count' do
1639 assert i.save 1763 assert i.save
1640 end 1764 end
1641 # 1 more change 1765 # 1 more change
1642 i.priority = IssuePriority.find(:first, :conditions => ["id <> ?", i.priority_id]) 1766 i.priority = IssuePriority.where("id <> ?", i.priority_id).first
1643 assert_no_difference 'Journal.count' do 1767 assert_no_difference 'Journal.count' do
1644 assert_difference 'JournalDetail.count', 1 do 1768 assert_difference 'JournalDetail.count', 1 do
1645 i.save 1769 i.save
1646 end 1770 end
1647 end 1771 end
1708 IssueRelation.update_all("issue_to_id = 1", ["id = ?", r.id]) 1832 IssueRelation.update_all("issue_to_id = 1", ["id = ?", r.id])
1709 1833
1710 assert_equal [2, 3, 8], Issue.find(1).all_dependent_issues.collect(&:id).sort 1834 assert_equal [2, 3, 8], Issue.find(1).all_dependent_issues.collect(&:id).sort
1711 end 1835 end
1712 1836
1713 context "#done_ratio" do 1837 test "#done_ratio should use the issue_status according to Setting.issue_done_ratio" do
1714 setup do 1838 @issue = Issue.find(1)
1715 @issue = Issue.find(1) 1839 @issue_status = IssueStatus.find(1)
1716 @issue_status = IssueStatus.find(1) 1840 @issue_status.update_attribute(:default_done_ratio, 50)
1717 @issue_status.update_attribute(:default_done_ratio, 50) 1841 @issue2 = Issue.find(2)
1718 @issue2 = Issue.find(2) 1842 @issue_status2 = IssueStatus.find(2)
1719 @issue_status2 = IssueStatus.find(2) 1843 @issue_status2.update_attribute(:default_done_ratio, 0)
1720 @issue_status2.update_attribute(:default_done_ratio, 0) 1844
1721 end 1845 with_settings :issue_done_ratio => 'issue_field' do
1722 1846 assert_equal 0, @issue.done_ratio
1723 teardown do 1847 assert_equal 30, @issue2.done_ratio
1724 Setting.issue_done_ratio = 'issue_field' 1848 end
1725 end 1849
1726 1850 with_settings :issue_done_ratio => 'issue_status' do
1727 context "with Setting.issue_done_ratio using the issue_field" do 1851 assert_equal 50, @issue.done_ratio
1728 setup do 1852 assert_equal 0, @issue2.done_ratio
1729 Setting.issue_done_ratio = 'issue_field' 1853 end
1730 end 1854 end
1731 1855
1732 should "read the issue's field" do 1856 test "#update_done_ratio_from_issue_status should update done_ratio according to Setting.issue_done_ratio" do
1733 assert_equal 0, @issue.done_ratio 1857 @issue = Issue.find(1)
1734 assert_equal 30, @issue2.done_ratio 1858 @issue_status = IssueStatus.find(1)
1735 end 1859 @issue_status.update_attribute(:default_done_ratio, 50)
1736 end 1860 @issue2 = Issue.find(2)
1737 1861 @issue_status2 = IssueStatus.find(2)
1738 context "with Setting.issue_done_ratio using the issue_status" do 1862 @issue_status2.update_attribute(:default_done_ratio, 0)
1739 setup do 1863
1740 Setting.issue_done_ratio = 'issue_status' 1864 with_settings :issue_done_ratio => 'issue_field' do
1741 end 1865 @issue.update_done_ratio_from_issue_status
1742 1866 @issue2.update_done_ratio_from_issue_status
1743 should "read the Issue Status's default done ratio" do 1867
1744 assert_equal 50, @issue.done_ratio 1868 assert_equal 0, @issue.read_attribute(:done_ratio)
1745 assert_equal 0, @issue2.done_ratio 1869 assert_equal 30, @issue2.read_attribute(:done_ratio)
1746 end 1870 end
1747 end 1871
1748 end 1872 with_settings :issue_done_ratio => 'issue_status' do
1749 1873 @issue.update_done_ratio_from_issue_status
1750 context "#update_done_ratio_from_issue_status" do 1874 @issue2.update_done_ratio_from_issue_status
1751 setup do 1875
1752 @issue = Issue.find(1) 1876 assert_equal 50, @issue.read_attribute(:done_ratio)
1753 @issue_status = IssueStatus.find(1) 1877 assert_equal 0, @issue2.read_attribute(:done_ratio)
1754 @issue_status.update_attribute(:default_done_ratio, 50)
1755 @issue2 = Issue.find(2)
1756 @issue_status2 = IssueStatus.find(2)
1757 @issue_status2.update_attribute(:default_done_ratio, 0)
1758 end
1759
1760 context "with Setting.issue_done_ratio using the issue_field" do
1761 setup do
1762 Setting.issue_done_ratio = 'issue_field'
1763 end
1764
1765 should "not change the issue" do
1766 @issue.update_done_ratio_from_issue_status
1767 @issue2.update_done_ratio_from_issue_status
1768
1769 assert_equal 0, @issue.read_attribute(:done_ratio)
1770 assert_equal 30, @issue2.read_attribute(:done_ratio)
1771 end
1772 end
1773
1774 context "with Setting.issue_done_ratio using the issue_status" do
1775 setup do
1776 Setting.issue_done_ratio = 'issue_status'
1777 end
1778
1779 should "change the issue's done ratio" do
1780 @issue.update_done_ratio_from_issue_status
1781 @issue2.update_done_ratio_from_issue_status
1782
1783 assert_equal 50, @issue.read_attribute(:done_ratio)
1784 assert_equal 0, @issue2.read_attribute(:done_ratio)
1785 end
1786 end 1878 end
1787 end 1879 end
1788 1880
1789 test "#by_tracker" do 1881 test "#by_tracker" do
1790 User.current = User.anonymous 1882 User.current = User.anonymous
1853 issue.project = Project.find(2) 1945 issue.project = Project.find(2)
1854 assert issue.save 1946 assert issue.save
1855 assert_equal before, Issue.on_active_project.length 1947 assert_equal before, Issue.on_active_project.length
1856 end 1948 end
1857 1949
1858 context "Issue#recipients" do 1950 test "Issue#recipients should include project recipients" do
1859 setup do 1951 issue = Issue.generate!
1860 @project = Project.find(1) 1952 assert issue.project.recipients.present?
1861 @author = User.generate! 1953 issue.project.recipients.each do |project_recipient|
1862 @assignee = User.generate! 1954 assert issue.recipients.include?(project_recipient)
1863 @issue = Issue.generate!(:project => @project, :assigned_to => @assignee, :author => @author) 1955 end
1864 end 1956 end
1865 1957
1866 should "include project recipients" do 1958 test "Issue#recipients should include the author if the author is active" do
1867 assert @project.recipients.present? 1959 issue = Issue.generate!(:author => User.generate!)
1868 @project.recipients.each do |project_recipient| 1960 assert issue.author, "No author set for Issue"
1869 assert @issue.recipients.include?(project_recipient) 1961 assert issue.recipients.include?(issue.author.mail)
1870 end 1962 end
1871 end 1963
1872 1964 test "Issue#recipients should include the assigned to user if the assigned to user is active" do
1873 should "include the author if the author is active" do 1965 issue = Issue.generate!(:assigned_to => User.generate!)
1874 assert @issue.author, "No author set for Issue" 1966 assert issue.assigned_to, "No assigned_to set for Issue"
1875 assert @issue.recipients.include?(@issue.author.mail) 1967 assert issue.recipients.include?(issue.assigned_to.mail)
1876 end 1968 end
1877 1969
1878 should "include the assigned to user if the assigned to user is active" do 1970 test "Issue#recipients should not include users who opt out of all email" do
1879 assert @issue.assigned_to, "No assigned_to set for Issue" 1971 issue = Issue.generate!(:author => User.generate!)
1880 assert @issue.recipients.include?(@issue.assigned_to.mail) 1972 issue.author.update_attribute(:mail_notification, :none)
1881 end 1973 assert !issue.recipients.include?(issue.author.mail)
1882 1974 end
1883 should "not include users who opt out of all email" do 1975
1884 @author.update_attribute(:mail_notification, :none) 1976 test "Issue#recipients should not include the issue author if they are only notified of assigned issues" do
1885 1977 issue = Issue.generate!(:author => User.generate!)
1886 assert !@issue.recipients.include?(@issue.author.mail) 1978 issue.author.update_attribute(:mail_notification, :only_assigned)
1887 end 1979 assert !issue.recipients.include?(issue.author.mail)
1888 1980 end
1889 should "not include the issue author if they are only notified of assigned issues" do 1981
1890 @author.update_attribute(:mail_notification, :only_assigned) 1982 test "Issue#recipients should not include the assigned user if they are only notified of owned issues" do
1891 1983 issue = Issue.generate!(:assigned_to => User.generate!)
1892 assert !@issue.recipients.include?(@issue.author.mail) 1984 issue.assigned_to.update_attribute(:mail_notification, :only_owner)
1893 end 1985 assert !issue.recipients.include?(issue.assigned_to.mail)
1894
1895 should "not include the assigned user if they are only notified of owned issues" do
1896 @assignee.update_attribute(:mail_notification, :only_owner)
1897
1898 assert !@issue.recipients.include?(@issue.assigned_to.mail)
1899 end
1900 end 1986 end
1901 1987
1902 def test_last_journal_id_with_journals_should_return_the_journal_id 1988 def test_last_journal_id_with_journals_should_return_the_journal_id
1903 assert_equal 2, Issue.find(1).last_journal_id 1989 assert_equal 2, Issue.find(1).last_journal_id
1904 end 1990 end
1912 assert_equal [], Issue.find(1).journals_after('2') 1998 assert_equal [], Issue.find(1).journals_after('2')
1913 end 1999 end
1914 2000
1915 def test_journals_after_with_blank_arg_should_return_all_journals 2001 def test_journals_after_with_blank_arg_should_return_all_journals
1916 assert_equal [Journal.find(1), Journal.find(2)], Issue.find(1).journals_after('') 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
1917 end 2009 end
1918 2010
1919 def test_css_classes_should_include_priority 2011 def test_css_classes_should_include_priority
1920 issue = Issue.new(:priority => IssuePriority.find(8)) 2012 issue = Issue.new(:priority => IssuePriority.find(8))
1921 classes = issue.css_classes.split(' ') 2013 classes = issue.css_classes.split(' ')
1934 issue.attach_saved_attachments 2026 issue.attach_saved_attachments
1935 2027
1936 assert_equal 3, issue.reload.attachments.count 2028 assert_equal 3, issue.reload.attachments.count
1937 assert_equal %w(upload foo bar), issue.attachments.map(&:filename) 2029 assert_equal %w(upload foo bar), issue.attachments.map(&:filename)
1938 end 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
1939 end 2104 end