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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006- 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_relative '../test_helper'
  19. class SettingTest < ActiveSupport::TestCase
  20. fixtures :users
  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_setting_serialied_as_binary_should_be_loaded_as_utf8_encoded_strings
  96. yaml = <<~YAML
  97. ---
  98. - keywords: !binary |
  99. Zml4ZXMsY2xvc2VzLNC40YHQv9GA0LDQstC70LXQvdC+LNCz0L7RgtC+0LLQ
  100. vizRgdC00LXQu9Cw0L3QvixmaXhlZA==
  101. done_ratio: "100"
  102. status_id: "5"
  103. YAML
  104. Setting.commit_update_keywords = {}
  105. assert_equal 1, Setting.where(:name => 'commit_update_keywords').update_all(:value => yaml)
  106. Setting.clear_cache
  107. assert_equal 'UTF-8', Setting.commit_update_keywords.first['keywords'].encoding.name
  108. ensure
  109. Setting.where(:name => 'commit_update_keywords').delete_all
  110. Setting.clear_cache
  111. end
  112. def test_mail_from_format_should_be_validated
  113. with_locale 'en' do
  114. ['[Redmine app] <redmine@example.net>', 'redmine'].each do |invalid_mail_from|
  115. errors = Setting.set_all_from_params({:mail_from => invalid_mail_from})
  116. assert_includes errors, [:mail_from, 'is invalid']
  117. end
  118. ['Redmine app <redmine@example.net>', 'redmine@example.net', '<redmine@example.net>'].each do |valid_mail_from|
  119. errors = Setting.set_all_from_params({:mail_from => valid_mail_from})
  120. assert_nil errors
  121. end
  122. end
  123. end
  124. def test_default_text_formatting_for_new_installations_is_common_mark
  125. assert_equal 'common_mark', Setting.text_formatting
  126. end
  127. end