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 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 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_auto_watch_on_should_default_to_setting
  51. preference = UserPreference.new
  52. assert_equal %w[issue_created issue_contributed_to], preference.auto_watch_on
  53. end
  54. def test_create
  55. user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
  56. user.login = "newuser"
  57. user.password, user.password_confirmation = "password", "password"
  58. assert user.save
  59. assert_kind_of UserPreference, user.pref
  60. assert_kind_of Hash, user.pref.others
  61. assert user.pref.save
  62. end
  63. def test_update
  64. user = User.find(1)
  65. assert_equal true, user.pref.hide_mail
  66. user.pref['preftest'] = 'value'
  67. assert user.pref.save
  68. user.reload
  69. assert_equal 'value', user.pref['preftest']
  70. end
  71. def test_others_hash
  72. user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
  73. user.login = "newuser"
  74. user.password, user.password_confirmation = "password", "password"
  75. assert user.save
  76. assert_nil user.preference
  77. up = UserPreference.new(:user => user)
  78. assert_kind_of Hash, up.others
  79. up.others = nil
  80. assert_nil up.others
  81. assert up.save
  82. assert_kind_of Hash, up.others
  83. end
  84. def test_reading_value_from_nil_others_hash
  85. up = UserPreference.new(:user => User.new)
  86. up.others = nil
  87. assert_nil up.others
  88. assert_nil up[:foo]
  89. end
  90. def test_writing_value_to_nil_others_hash
  91. up = UserPreference.new(:user => User.new)
  92. up.others = nil
  93. assert_nil up.others
  94. up[:foo] = 'bar'
  95. assert_equal 'bar', up[:foo]
  96. end
  97. def test_removing_a_block_should_clear_its_settings
  98. up = User.find(2).pref
  99. up.my_page_layout = {'top' => ['news', 'documents']}
  100. up.my_page_settings = {'news' => {:foo => 'bar'}, 'documents' => {:baz => 'quz'}}
  101. up.save!
  102. up.remove_block 'news'
  103. up.save!
  104. assert_equal ['documents'], up.my_page_settings.keys
  105. end
  106. def test_toolbar_language_options_setter_should_remove_except_supported_languages
  107. up = User.find(2).pref
  108. # bar is not a supported language
  109. up.toolbar_language_options = 'ruby,cpp,bar,c'
  110. assert_equal 'ruby,cpp,c', up.toolbar_language_options
  111. end
  112. end