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

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