discourse/spec/serializers/reviewable_score_serializer_spec.rb
Roman Rizzi a9d20610d4
FIX: Make score's reason link building more explicit (#14475)
We relied on backticks to identify and replace site setting names with links. Unfortunately, some translations don't follow this convention, breaking this feature.

Additionally, this lets us linkify `category settings` and `watched words` without using HTML in the translations.

You may notice that I split the texts we want to linkify into two groups. I did this on purpose to emphasize those that should be translated (regular_links) from those who don't (site_settings_link). If you can think of a better solution, I'm open to suggestions.
2021-10-04 16:55:09 -03:00

58 lines
2.0 KiB
Ruby

# frozen_string_literal: true
require 'rails_helper'
describe ReviewableScoreSerializer do
fab!(:reviewable) { Fabricate(:reviewable_flagged_post) }
fab!(:admin) { Fabricate(:admin) }
describe '#reason' do
context 'regular links' do
it 'adds a link for watched words' do
serialized = serialized_score('watched_word')
link_url = "#{Discourse.base_url}/admin/customize/watched_words"
watched_words_link = "<a href=\"#{link_url}\">#{I18n.t('reviewables.reasons.regular_links.watched_word')}</a>"
expect(serialized.reason).to include(watched_words_link)
end
it 'adds a link for category settings' do
category = Fabricate.build(:category)
reviewable.category = category
serialized = serialized_score('category')
link_url = "#{Discourse.base_url}/c/#{category.name}/edit/settings"
category_link = "<a href=\"#{link_url}\">#{I18n.t('reviewables.reasons.regular_links.category')}</a>"
expect(serialized.reason).to include(category_link)
end
end
context 'site setting links' do
reasons = %w[
post_count trust_level new_topics_unless_trust_level fast_typer auto_silence_regexp
staged must_approve_users invite_only email_spam suspect_user contains_media
]
reasons.each do |r|
it "addd a link to a site setting for the #{r} reason" do
serialized = serialized_score(r)
setting_name = I18n.t("reviewables.reasons.site_setting_links.#{r}")
link_url = "#{Discourse.base_url}/admin/site_settings/category/all_results?filter=#{setting_name}"
link = "<a href=\"#{link_url}\">#{setting_name.gsub('_', ' ')}</a>"
expect(serialized.reason).to include(link)
end
end
end
end
def serialized_score(reason)
score = ReviewableScore.new(
reviewable: reviewable,
reason: reason
)
described_class.new(score, scope: Guardian.new(admin), root: nil)
end
end