You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

enumeration_test.rb 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 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. require File.expand_path('../../test_helper', __FILE__)
  18. class EnumerationTest < ActiveSupport::TestCase
  19. fixtures :enumerations, :issues, :custom_fields, :custom_values
  20. def test_objects_count
  21. # low priority
  22. assert_equal 6, Enumeration.find(4).objects_count
  23. # urgent
  24. assert_equal 0, Enumeration.find(7).objects_count
  25. end
  26. def test_in_use
  27. # low priority
  28. assert Enumeration.find(4).in_use?
  29. # urgent
  30. assert !Enumeration.find(7).in_use?
  31. end
  32. def test_default
  33. e = Enumeration.default
  34. assert e.is_a?(Enumeration)
  35. assert e.is_default?
  36. assert e.active?
  37. assert_equal 'Default Enumeration', e.name
  38. end
  39. def test_default_non_active
  40. e = Enumeration.find(12)
  41. assert e.is_a?(Enumeration)
  42. assert e.is_default?
  43. assert e.active?
  44. e.update_attributes(:active => false)
  45. assert e.is_default?
  46. assert !e.active?
  47. end
  48. def test_create
  49. e = Enumeration.new(:name => 'Not default', :is_default => false)
  50. e.type = 'Enumeration'
  51. assert e.save
  52. assert_equal 'Default Enumeration', Enumeration.default.name
  53. end
  54. def test_create_as_default
  55. e = Enumeration.new(:name => 'Very urgent', :is_default => true)
  56. e.type = 'Enumeration'
  57. assert e.save
  58. assert_equal e, Enumeration.default
  59. end
  60. def test_update_default
  61. e = Enumeration.default
  62. e.update_attributes(:name => 'Changed', :is_default => true)
  63. assert_equal e, Enumeration.default
  64. end
  65. def test_update_default_to_non_default
  66. e = Enumeration.default
  67. e.update_attributes(:name => 'Changed', :is_default => false)
  68. assert_nil Enumeration.default
  69. end
  70. def test_change_default
  71. e = Enumeration.find_by_name('Default Enumeration')
  72. e.update_attributes(:name => 'Changed Enumeration', :is_default => true)
  73. assert_equal e, Enumeration.default
  74. end
  75. def test_destroy_with_reassign
  76. Enumeration.find(4).destroy(Enumeration.find(6))
  77. assert_nil Issue.where(:priority_id => 4).first
  78. assert_equal 6, Enumeration.find(6).objects_count
  79. end
  80. def test_should_be_customizable
  81. assert Enumeration.included_modules.include?(Redmine::Acts::Customizable::InstanceMethods)
  82. end
  83. def test_should_belong_to_a_project
  84. association = Enumeration.reflect_on_association(:project)
  85. assert association, "No Project association found"
  86. assert_equal :belongs_to, association.macro
  87. end
  88. def test_should_act_as_tree
  89. enumeration = Enumeration.find(4)
  90. assert enumeration.respond_to?(:parent)
  91. assert enumeration.respond_to?(:children)
  92. end
  93. def test_is_override
  94. # Defaults to off
  95. enumeration = Enumeration.find(4)
  96. assert !enumeration.is_override?
  97. # Setup as an override
  98. enumeration.parent = Enumeration.find(5)
  99. assert enumeration.is_override?
  100. end
  101. def test_get_subclasses
  102. classes = Enumeration.get_subclasses
  103. assert_include IssuePriority, classes
  104. assert_include DocumentCategory, classes
  105. assert_include TimeEntryActivity, classes
  106. classes.each do |klass|
  107. assert_equal Enumeration, klass.superclass
  108. end
  109. end
  110. def test_list_should_be_scoped_for_each_type
  111. Enumeration.delete_all
  112. a = IssuePriority.create!(:name => 'A')
  113. b = IssuePriority.create!(:name => 'B')
  114. c = DocumentCategory.create!(:name => 'C')
  115. assert_equal [1, 2, 1], [a, b, c].map(&:reload).map(&:position)
  116. end
  117. def test_override_should_be_created_with_same_position_as_parent
  118. Enumeration.delete_all
  119. a = IssuePriority.create!(:name => 'A')
  120. b = IssuePriority.create!(:name => 'B')
  121. override = IssuePriority.create!(:name => 'BB', :parent_id => b.id)
  122. assert_equal [1, 2, 2], [a, b, override].map(&:reload).map(&:position)
  123. end
  124. def test_override_position_should_be_updated_with_parent_position
  125. Enumeration.delete_all
  126. a = IssuePriority.create!(:name => 'A')
  127. b = IssuePriority.create!(:name => 'B')
  128. override = IssuePriority.create!(:name => 'BB', :parent_id => b.id)
  129. b.position -= 1
  130. b.save!
  131. assert_equal [2, 1, 1], [a, b, override].map(&:reload).map(&:position)
  132. end
  133. def test_destroying_override_should_not_update_positions
  134. Enumeration.delete_all
  135. Issue.delete_all
  136. a = IssuePriority.create!(:name => 'A')
  137. b = IssuePriority.create!(:name => 'B')
  138. c = IssuePriority.create!(:name => 'C')
  139. override = IssuePriority.create!(:name => 'BB', :parent_id => b.id)
  140. assert_equal [1, 2, 3, 2], [a, b, c, override].map(&:reload).map(&:position)
  141. override.destroy
  142. assert_equal [1, 2, 3], [a, b, c].map(&:reload).map(&:position)
  143. end
  144. end