diff --git a/app/models/user.rb b/app/models/user.rb index c88ad5908233..4563f1086b92 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -406,7 +406,7 @@ def pref end def time_zone - @time_zone ||= (pref.time_zone.blank? ? nil : ActiveSupport::TimeZone[pref.time_zone]) + @time_zone ||= ActiveSupport::TimeZone[pref.time_zone] || ActiveSupport::TimeZone["Etc/UTC"] end def reload(*) diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 2cb717f4e12c..66d7db19d217 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -823,6 +823,39 @@ def build_user_double_with_expired_password(is_expired) end end + describe "#time_zone" do + let(:user) { build(:user, preferences:) } + + context "with an existing time zone in the prefs" do + let(:preferences) { { "time_zone" => "Europe/Athens" } } + + it "returns the matching ActiveSupport::TimeZone" do + expect(user.time_zone) + .to eql ActiveSupport::TimeZone["Europe/Athens"] + end + end + + context "with an invalid time zone" do + # Would need to be Etc/UTC or UTC to be valid + let(:preferences) { { "time_zone" => "utc" } } + + it "returns the utc ActiveSupport::TimeZone" do + expect(user.time_zone) + .to eql ActiveSupport::TimeZone["Etc/UTC"] + end + end + + context "without a time zone" do + # Would need to be Etc/UTC or UTC to be valid + let(:preferences) { {} } + + it "returns the utc ActiveSupport::TimeZone" do + expect(user.time_zone) + .to eql ActiveSupport::TimeZone["Etc/UTC"] + end + end + end + describe "#find_by_mail" do let!(:user1) { create(:user, mail: "foo+test@example.org") } let!(:user2) { create(:user, mail: "foo@example.org") }