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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2019 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_create
  45. user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
  46. user.login = "newuser"
  47. user.password, user.password_confirmation = "password", "password"
  48. assert user.save
  49. assert_kind_of UserPreference, user.pref
  50. assert_kind_of Hash, user.pref.others
  51. assert user.pref.save
  52. end
  53. def test_update
  54. user = User.find(1)
  55. assert_equal true, user.pref.hide_mail
  56. user.pref['preftest'] = 'value'
  57. assert user.pref.save
  58. user.reload
  59. assert_equal 'value', user.pref['preftest']
  60. end
  61. def test_others_hash
  62. user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
  63. user.login = "newuser"
  64. user.password, user.password_confirmation = "password", "password"
  65. assert user.save
  66. assert_nil user.preference
  67. up = UserPreference.new(:user => user)
  68. assert_kind_of Hash, up.others
  69. up.others = nil
  70. assert_nil up.others
  71. assert up.save
  72. assert_kind_of Hash, up.others
  73. end
  74. def test_reading_value_from_nil_others_hash
  75. up = UserPreference.new(:user => User.new)
  76. up.others = nil
  77. assert_nil up.others
  78. assert_nil up[:foo]
  79. end
  80. def test_writing_value_to_nil_others_hash
  81. up = UserPreference.new(:user => User.new)
  82. up.others = nil
  83. assert_nil up.others
  84. up[:foo] = 'bar'
  85. assert_equal 'bar', up[:foo]
  86. end
  87. def test_removing_a_block_should_clear_its_settings
  88. up = User.find(2).pref
  89. up.my_page_layout = {'top' => ['news', 'documents']}
  90. up.my_page_settings = {'news' => {:foo => 'bar'}, 'documents' => {:baz => 'quz'}}
  91. up.save!
  92. up.remove_block 'news'
  93. up.save!
  94. assert_equal ['documents'], up.my_page_settings.keys
  95. end
  96. end