discourse/spec/jobs/regular/bulk_user_title_update_spec.rb
Martin Brennan 56d3e29a69
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.
2019-11-08 15:34:24 +10:00

51 lines
1.6 KiB
Ruby

# frozen_string_literal: true
require 'rails_helper'
describe Jobs::BulkUserTitleUpdate do
fab!(:badge) { Fabricate(:badge, name: 'Protector of the Realm', allow_title: true) }
fab!(:user) { Fabricate(:user) }
fab!(:other_user) { Fabricate(:user) }
describe 'update action' do
before do
BadgeGranter.grant(badge, user)
user.update(title: badge.name)
end
it 'updates the title of all users with the attached granted title badge id on their profile' do
execute_update
expect(user.reload.title).to eq('King of the Forum')
end
it 'does not set the title for any other users' do
execute_update
expect(other_user.reload.title).not_to eq('King of the Forum')
end
def execute_update
described_class.new.execute(new_title: 'King of the Forum', granted_badge_id: badge.id, action: described_class::UPDATE_ACTION)
end
end
describe 'reset action' do
let(:customized_badge_name) { 'Merit Badge' }
before do
TranslationOverride.upsert!(I18n.locale, Badge.i18n_key(badge.name), customized_badge_name)
BadgeGranter.grant(badge, user)
user.update(title: customized_badge_name)
end
it 'updates the title of all users back to the original badge name' do
expect(user.reload.title).to eq(customized_badge_name)
described_class.new.execute(granted_badge_id: badge.id, action: described_class::RESET_ACTION)
expect(user.reload.title).to eq('Protector of the Realm')
end
after do
TranslationOverride.revert!(I18n.locale, Badge.i18n_key(badge.name))
end
end
end