FIX: Allow users to change title in locales other than English

This commit is contained in:
Gerhard Schlager 2020-01-08 18:01:46 +01:00
parent 5eda44f8f2
commit 6d6ffe5131
2 changed files with 24 additions and 4 deletions

View File

@ -312,7 +312,13 @@ class Guardian
return false if title.nil? return false if title.nil?
return true if title.empty? # A title set to '(none)' in the UI is an empty string return true if title.empty? # A title set to '(none)' in the UI is an empty string
return false if user != @user return false if user != @user
return true if user.badges.where(name: title, allow_title: true).exists?
return true if user.badges
.where(allow_title: true)
.pluck(:name)
.map { |name| Badge.display_name(name) }
.include?(title)
user.groups.where(title: title).exists? user.groups.where(title: title).exists?
end end

View File

@ -1746,10 +1746,24 @@ describe UsersController do
end end
context 'a locale is chosen that differs from I18n.locale' do context 'a locale is chosen that differs from I18n.locale' do
before do
SiteSetting.allow_user_locale = true
end
it "updates the user's locale" do it "updates the user's locale" do
I18n.stubs(:locale).returns('fr') I18n.locale = :fr
put "/u/#{user.username}.json", params: { locale: :fa_IR } put "/u/#{user.username}.json", params: { locale: :fa_IR }
expect(User.find_by(username: user.username).locale).to eq('fa_IR') expect(user.reload.locale).to eq('fa_IR')
end
it "updates the title" do
user.update!(locale: :fr)
user.change_trust_level!(TrustLevel[4])
BadgeGranter.process_queue!
leader_title = I18n.t("badges.leader.name", locale: :fr)
put "/u/#{user.username}.json", params: { title: leader_title }
expect(user.reload.title).to eq(leader_title)
end end
end end
@ -1951,7 +1965,7 @@ describe UsersController do
expect(response.status).to eq(200) expect(response.status).to eq(200)
end end
context "with overrided name" do context "with overridden name" do
fab!(:badge) { Fabricate(:badge, name: 'Demogorgon', allow_title: true) } fab!(:badge) { Fabricate(:badge, name: 'Demogorgon', allow_title: true) }
let(:user_badge) { BadgeGranter.grant(badge, user) } let(:user_badge) { BadgeGranter.grant(badge, user) }