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.

setting_test.rb 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. # encoding: utf-8
  2. #
  3. # Redmine - project management software
  4. # Copyright (C) 2006-2017 Jean-Philippe Lang
  5. #
  6. # This program is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU General Public License
  8. # as published by the Free Software Foundation; either version 2
  9. # of the License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. require File.expand_path('../../test_helper', __FILE__)
  20. class SettingTest < ActiveSupport::TestCase
  21. def setup
  22. User.current = nil
  23. end
  24. def teardown
  25. Setting.delete_all
  26. Setting.clear_cache
  27. end
  28. def test_read_default
  29. Setting.delete_all
  30. Setting.clear_cache
  31. assert_equal "Redmine", Setting.app_title
  32. assert Setting.self_registration?
  33. assert !Setting.login_required?
  34. end
  35. def test_update
  36. Setting.app_title = "My title"
  37. assert_equal "My title", Setting.app_title
  38. # make sure db has been updated (INSERT)
  39. assert_equal "My title", Setting.find_by_name('app_title').value
  40. Setting.app_title = "My other title"
  41. assert_equal "My other title", Setting.app_title
  42. # make sure db has been updated (UPDATE)
  43. assert_equal "My other title", Setting.find_by_name('app_title').value
  44. end
  45. def test_setting_with_int_format_should_accept_numeric_only
  46. with_settings :session_timeout => 30 do
  47. Setting.session_timeout = 'foo'
  48. assert_equal "30", Setting.session_timeout
  49. Setting.session_timeout = 40
  50. assert_equal "40", Setting.session_timeout
  51. end
  52. end
  53. def test_setting_with_invalid_name_should_be_valid
  54. setting = Setting.new(:name => "does_not_exist", :value => "should_not_be_allowed")
  55. assert !setting.save
  56. end
  57. def test_serialized_setting
  58. Setting.notified_events = ['issue_added', 'issue_updated', 'news_added']
  59. assert_equal ['issue_added', 'issue_updated', 'news_added'], Setting.notified_events
  60. assert_equal ['issue_added', 'issue_updated', 'news_added'], Setting.find_by_name('notified_events').value
  61. end
  62. def test_setting_should_be_reloaded_after_clear_cache
  63. Setting.app_title = "My title"
  64. assert_equal "My title", Setting.app_title
  65. s = Setting.find_by_name("app_title")
  66. s.value = 'New title'
  67. s.save!
  68. assert_equal "My title", Setting.app_title
  69. Setting.clear_cache
  70. assert_equal "New title", Setting.app_title
  71. end
  72. def test_per_page_options_array_should_be_an_empty_array_when_setting_is_blank
  73. with_settings :per_page_options => nil do
  74. assert_equal [], Setting.per_page_options_array
  75. end
  76. with_settings :per_page_options => '' do
  77. assert_equal [], Setting.per_page_options_array
  78. end
  79. end
  80. def test_per_page_options_array_should_be_an_array_of_integers
  81. with_settings :per_page_options => '10, 25, 50' do
  82. assert_equal [10, 25, 50], Setting.per_page_options_array
  83. end
  84. end
  85. def test_per_page_options_array_should_omit_non_numerial_values
  86. with_settings :per_page_options => 'a, 25, 50' do
  87. assert_equal [25, 50], Setting.per_page_options_array
  88. end
  89. end
  90. def test_per_page_options_array_should_be_sorted
  91. with_settings :per_page_options => '25, 10, 50' do
  92. assert_equal [10, 25, 50], Setting.per_page_options_array
  93. end
  94. end
  95. def test_mail_from_address
  96. mail_from_strings = [
  97. 'joe@example.com',
  98. '<joe@example.com>',
  99. 'Joe Bloggs <joe@example.com>',
  100. 'display_name@example.com <joe@example.com>',
  101. 'joe@example.com (Joe Bloggs)',
  102. 'joe@example.com (display_name@example.com)'
  103. ]
  104. mail_from_strings.each do |from_value|
  105. with_settings :mail_from => from_value do
  106. assert_equal 'joe@example.com', Setting.mail_from_address
  107. end
  108. end
  109. end
  110. def test_setting_serialied_as_binary_should_be_loaded_as_utf8_encoded_strings
  111. yaml = <<-YAML
  112. ---
  113. - keywords: !binary |
  114. Zml4ZXMsY2xvc2VzLNC40YHQv9GA0LDQstC70LXQvdC+LNCz0L7RgtC+0LLQ
  115. vizRgdC00LXQu9Cw0L3QvixmaXhlZA==
  116. done_ratio: "100"
  117. status_id: "5"
  118. YAML
  119. Setting.commit_update_keywords = {}
  120. assert_equal 1, Setting.where(:name => 'commit_update_keywords').update_all(:value => yaml)
  121. Setting.clear_cache
  122. assert_equal 'UTF-8', Setting.commit_update_keywords.first['keywords'].encoding.name
  123. ensure
  124. Setting.where(:name => 'commit_update_keywords').delete_all
  125. Setting.clear_cache
  126. end
  127. end