A platform for community discussion. Free, open, simple.
FIX: Validate user locale in effective_locale method (#35870)
Rafael dos Santos Silvaaba5b3f9438d
diff --git a/app/models/user.rb b/app/models/user.rbindex 2c5317e84ae..9a14fddfbf4 100644--- a/app/models/user.rb+++ b/app/models/user.rb@@ -484,7 +484,7 @@ class User < ActiveRecord::Baseenddef effective_locale- if SiteSetting.allow_user_locale && self.locale.present?+ if SiteSetting.allow_user_locale && self.locale.present? && I18n.locale_available?(self.locale)self.localeelseSiteSetting.default_localediff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rbindex 606d75ee058..052e8330dd4 100644--- a/spec/models/user_spec.rb+++ b/spec/models/user_spec.rb@@ -3749,4 +3749,29 @@ RSpec.describe User do)endend++ describe "#effective_locale" do+ before { SiteSetting.allow_user_locale = true }++ it "returns the user's locale when it is valid" do+ user.update!(locale: "fr")+ expect(user.effective_locale).to eq("fr")+ end++ it "returns the default locale when user locale is invalid" do+ user.update_columns(locale: "invalid")+ expect(user.effective_locale).to eq(SiteSetting.default_locale)+ end++ it "returns the default locale when user locale is blank" do+ user.update!(locale: nil)+ expect(user.effective_locale).to eq(SiteSetting.default_locale)+ end++ it "returns the default locale when allow_user_locale is disabled" do+ SiteSetting.allow_user_locale = false+ user.update!(locale: "fr")+ expect(user.effective_locale).to eq(SiteSetting.default_locale)+ end+ endend