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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2021 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. require File.expand_path('../../test_helper', __FILE__)
  19. class EnumerationTest < ActiveSupport::TestCase
  20. fixtures :enumerations, :issues, :custom_fields, :custom_values
  21. def setup
  22. User.current = nil
  23. end
  24. def test_objects_count
  25. # low priority
  26. assert_equal 6, Enumeration.find(4).objects_count
  27. # urgent
  28. assert_equal 0, Enumeration.find(7).objects_count
  29. end
  30. def test_in_use
  31. # low priority
  32. assert Enumeration.find(4).in_use?
  33. # urgent
  34. assert !Enumeration.find(7).in_use?
  35. end
  36. def test_default
  37. e = Enumeration.default
  38. assert e.is_a?(Enumeration)
  39. assert e.is_default?
  40. assert e.active?
  41. assert_equal 'Default Enumeration', e.name
  42. end
  43. def test_default_non_active
  44. e = Enumeration.find(12)
  45. assert e.is_a?(Enumeration)
  46. assert e.is_default?
  47. assert e.active?
  48. e.update(:active => false)
  49. assert e.is_default?
  50. assert !e.active?
  51. end
  52. def test_create
  53. e = Enumeration.new(:name => 'Not default', :is_default => false)
  54. e.type = 'Enumeration'
  55. assert e.save
  56. assert_equal 'Default Enumeration', Enumeration.default.name
  57. end
  58. def test_create_as_default
  59. e = Enumeration.new(:name => 'Very urgent', :is_default => true)
  60. e.type = 'Enumeration'
  61. assert e.save
  62. assert_equal e, Enumeration.default
  63. end
  64. def test_update_default
  65. e = Enumeration.default
  66. e.update(:name => 'Changed', :is_default => true)
  67. assert_equal e, Enumeration.default
  68. end
  69. def test_update_default_to_non_default
  70. e = Enumeration.default
  71. e.update(:name => 'Changed', :is_default => false)
  72. assert_nil Enumeration.default
  73. end
  74. def test_change_default
  75. e = Enumeration.find_by_name('Default Enumeration')
  76. e.update(:name => 'Changed Enumeration', :is_default => true)
  77. assert_equal e, Enumeration.default
  78. end
  79. def test_destroy_with_reassign
  80. Enumeration.find(4).destroy(Enumeration.find(6))
  81. assert_not Issue.where(:priority_id => 4).exists?
  82. assert_equal 6, Enumeration.find(6).objects_count
  83. end
  84. def test_should_be_customizable
  85. assert Enumeration.included_modules.include?(Redmine::Acts::Customizable::InstanceMethods)
  86. end
  87. def test_should_belong_to_a_project
  88. association = Enumeration.reflect_on_association(:project)
  89. assert association, "No Project association found"
  90. assert_equal :belongs_to, association.macro
  91. end
  92. def test_should_act_as_tree
  93. enumeration = Enumeration.find(4)
  94. assert enumeration.respond_to?(:parent)
  95. assert enumeration.respond_to?(:children)
  96. end
  97. def test_is_override
  98. # Defaults to off
  99. enumeration = Enumeration.find(4)
  100. assert !enumeration.is_override?
  101. # Setup as an override
  102. enumeration.parent = Enumeration.find(5)
  103. assert enumeration.is_override?
  104. end
  105. def test_get_subclasses
  106. classes = Enumeration.get_subclasses
  107. assert_include IssuePriority, classes
  108. assert_include DocumentCategory, classes
  109. assert_include TimeEntryActivity, classes
  110. classes.each do |klass|
  111. assert_equal Enumeration, klass.superclass
  112. end
  113. end
  114. def test_list_should_be_scoped_for_each_type
  115. Enumeration.delete_all
  116. a = IssuePriority.create!(:name => 'A')
  117. b = IssuePriority.create!(:name => 'B')
  118. c = DocumentCategory.create!(:name => 'C')
  119. assert_equal [1, 2, 1], [a, b, c].map(&:reload).map(&:position)
  120. end
  121. def test_override_should_be_created_with_same_position_as_parent
  122. Enumeration.delete_all
  123. a = IssuePriority.create!(:name => 'A')
  124. b = IssuePriority.create!(:name => 'B')
  125. override = IssuePriority.create!(:name => 'BB', :parent_id => b.id)
  126. assert_equal [1, 2, 2], [a, b, override].map(&:reload).map(&:position)
  127. end
  128. def test_override_position_should_be_updated_with_parent_position
  129. Enumeration.delete_all
  130. a = IssuePriority.create!(:name => 'A')
  131. b = IssuePriority.create!(:name => 'B')
  132. override = IssuePriority.create!(:name => 'BB', :parent_id => b.id)
  133. b.position -= 1
  134. b.save!
  135. assert_equal [2, 1, 1], [a, b, override].map(&:reload).map(&:position)
  136. end
  137. def test_destroying_override_should_not_update_positions
  138. Enumeration.delete_all
  139. Issue.delete_all
  140. a = IssuePriority.create!(:name => 'A')
  141. b = IssuePriority.create!(:name => 'B')
  142. c = IssuePriority.create!(:name => 'C')
  143. override = IssuePriority.create!(:name => 'BB', :parent_id => b.id)
  144. assert_equal [1, 2, 3, 2], [a, b, c, override].map(&:reload).map(&:position)
  145. override.destroy
  146. assert_equal [1, 2, 3], [a, b, c].map(&:reload).map(&:position)
  147. end
  148. end