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.

user_preference_test.rb 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 UserPreferenceTest < ActiveSupport::TestCase
  20. fixtures :users, :user_preferences
  21. def setup
  22. User.current = nil
  23. end
  24. def test_hide_mail_should_default_to_true
  25. preference = UserPreference.new
  26. assert_equal true, preference.hide_mail
  27. end
  28. def test_hide_mail_should_default_to_false_with_setting
  29. with_settings :default_users_hide_mail => '0' do
  30. preference = UserPreference.new
  31. assert_equal false, preference.hide_mail
  32. end
  33. end
  34. def test_time_zone_should_default_to_setting
  35. with_settings :default_users_time_zone => 'Paris' do
  36. preference = UserPreference.new
  37. assert_equal 'Paris', preference.time_zone
  38. end
  39. end
  40. def test_no_self_notified_should_default_to_true
  41. preference = UserPreference.new
  42. assert_equal true, preference.no_self_notified
  43. end
  44. def test_no_self_notified_should_default_to_setting
  45. with_settings :default_users_no_self_notified => '0' do
  46. preference = UserPreference.new
  47. assert_equal false, preference.no_self_notified
  48. end
  49. end
  50. def test_create
  51. user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
  52. user.login = "newuser"
  53. user.password, user.password_confirmation = "password", "password"
  54. assert user.save
  55. assert_kind_of UserPreference, user.pref
  56. assert_kind_of Hash, user.pref.others
  57. assert user.pref.save
  58. end
  59. def test_update
  60. user = User.find(1)
  61. assert_equal true, user.pref.hide_mail
  62. user.pref['preftest'] = 'value'
  63. assert user.pref.save
  64. user.reload
  65. assert_equal 'value', user.pref['preftest']
  66. end
  67. def test_others_hash
  68. user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
  69. user.login = "newuser"
  70. user.password, user.password_confirmation = "password", "password"
  71. assert user.save
  72. assert_nil user.preference
  73. up = UserPreference.new(:user => user)
  74. assert_kind_of Hash, up.others
  75. up.others = nil
  76. assert_nil up.others
  77. assert up.save
  78. assert_kind_of Hash, up.others
  79. end
  80. def test_reading_value_from_nil_others_hash
  81. up = UserPreference.new(:user => User.new)
  82. up.others = nil
  83. assert_nil up.others
  84. assert_nil up[:foo]
  85. end
  86. def test_writing_value_to_nil_others_hash
  87. up = UserPreference.new(:user => User.new)
  88. up.others = nil
  89. assert_nil up.others
  90. up[:foo] = 'bar'
  91. assert_equal 'bar', up[:foo]
  92. end
  93. def test_removing_a_block_should_clear_its_settings
  94. up = User.find(2).pref
  95. up.my_page_layout = {'top' => ['news', 'documents']}
  96. up.my_page_settings = {'news' => {:foo => 'bar'}, 'documents' => {:baz => 'quz'}}
  97. up.save!
  98. up.remove_block 'news'
  99. up.save!
  100. assert_equal ['documents'], up.my_page_settings.keys
  101. end
  102. def test_toolbar_language_options_setter_should_remove_except_supported_languages
  103. up = User.find(2).pref
  104. # bar is not a supported language
  105. up.toolbar_language_options = 'ruby,cpp,bar,c'
  106. assert_equal 'ruby,cpp,c', up.toolbar_language_options
  107. end
  108. end