FIX: Badge and user title interaction fixes (#8282)

* Fix user title logic when badge name customized
* Fix an issue where a user's title was not considered a badge granted title when the user used a badge for their title and the badge name was customized. this affected the effectiveness of revoke_ungranted_titles! which only operates on badge_granted_titles.
* When a user's title is set now it is considered a badge_granted_title if the badge name OR the badge custom name from TranslationOverride is the same as the title
* When a user's badge is revoked we now also revoke their title if the user's title matches the badge name OR the badge custom name from TranslationOverride
* Add a user history log when the title is revoked to remove confusion about why titles are revoked
* Add granted_title_badge_id to user_profile, now when we set badge_granted_title on a user profile when updating a user's title based on a badge, we also remember which badge matched the title
* When badge name (or custom text) changes update titles of users in a background job
* When the name of a badge changes, or in the case of system badges when their custom translation text changes, then we need to update the title of all corresponding users who have a badge_granted_title and matching granted_title_badge_id. In the case of system badges we need to first get the proper badge ID based on the translation key e.g. badges.regular.name
* Add migration to backfill all granted_title_badge_ids for both normal badge name titles and titles using custom badge text.
This commit is contained in:
Martin Brennan
2019-11-08 15:34:24 +10:00
committed by GitHub
parent 64b4a7ba45
commit 56d3e29a69
19 changed files with 397 additions and 14 deletions

View File

@@ -153,6 +153,29 @@ describe Admin::BadgesController do
expect(badge.name).to eq('123456')
expect(badge.query).to eq(sql)
end
context 'when there is a user with a title granted using the badge' do
fab!(:user_with_badge_title) { Fabricate(:active_user) }
fab!(:badge) { Fabricate(:badge, name: 'Oathbreaker', allow_title: true) }
before do
BadgeGranter.grant(badge, user_with_badge_title)
user_with_badge_title.update(title: 'Oathbreaker')
end
it 'updates the user title in a job' do
Jobs.expects(:enqueue).with(
:bulk_user_title_update,
new_title: 'Shieldbearer',
granted_badge_id: badge.id,
action: Jobs::BulkUserTitleUpdate::UPDATE_ACTION
)
put "/admin/badges/#{badge.id}.json", params: {
name: "Shieldbearer"
}
end
end
end
end
end

View File

@@ -415,6 +415,37 @@ RSpec.describe Admin::SiteTextsController do
json = ::JSON.parse(response.body)
expect(json['site_text']['value']).to_not eq(ru_mf_text)
end
context 'when updating a translation override for a system badge' do
fab!(:user_with_badge_title) { Fabricate(:active_user) }
let(:badge) { Badge.find(Badge::Regular) }
before do
BadgeGranter.grant(badge, user_with_badge_title)
user_with_badge_title.update(title: 'Regular')
end
it 'updates matching user titles to the override text in a job' do
Jobs.expects(:enqueue).with(
:bulk_user_title_update,
new_title: 'Terminator',
granted_badge_id: badge.id,
action: Jobs::BulkUserTitleUpdate::UPDATE_ACTION
)
put '/admin/customize/site_texts/badges.regular.name.json', params: {
site_text: { value: 'Terminator' }
}
Jobs.expects(:enqueue).with(
:bulk_user_title_update,
granted_badge_id: badge.id,
action: Jobs::BulkUserTitleUpdate::RESET_ACTION
)
# Revert
delete "/admin/customize/site_texts/badges.regular.name.json"
end
end
end
context "reseeding" do

View File

@@ -1911,11 +1911,17 @@ describe UsersController do
expect(user.reload.title).to eq(badge.display_name)
expect(user.user_profile.badge_granted_title).to eq(true)
expect(user.user_profile.granted_title_badge_id).to eq(badge.id)
user.title = "testing"
user.save
badge.update allow_title: false
put "/u/#{user.username}/preferences/badge_title.json", params: { user_badge_id: user_badge.id }
user.reload
user.user_profile.reload
expect(user.title).to eq('')
expect(user.user_profile.badge_granted_title).to eq(false)
expect(user.user_profile.granted_title_badge_id).to eq(nil)
end
context "with overrided name" do