2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe TranslationOverride do
|
2022-07-27 12:14:14 -04:00
|
|
|
describe "Validations" do
|
2017-06-15 04:08:23 -04:00
|
|
|
describe "#value" do
|
|
|
|
before do
|
2021-03-24 15:58:24 -04:00
|
|
|
I18n.backend.store_translations(
|
|
|
|
I18n.locale,
|
2024-05-24 10:15:53 -04:00
|
|
|
{ user_notifications: { user_did_something: "%{first} %{second}" } },
|
2021-03-24 15:58:24 -04:00
|
|
|
)
|
|
|
|
|
2019-10-17 12:34:07 -04:00
|
|
|
I18n.backend.store_translations(
|
|
|
|
:en,
|
|
|
|
something: {
|
|
|
|
one: "%{key1} %{key2}",
|
|
|
|
other: "%{key3} %{key4}",
|
|
|
|
},
|
|
|
|
)
|
2017-06-15 04:08:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "when interpolation keys are missing" do
|
|
|
|
it "should not be valid" do
|
|
|
|
translation_override =
|
2024-05-24 10:15:53 -04:00
|
|
|
TranslationOverride.upsert!(
|
|
|
|
I18n.locale,
|
|
|
|
"user_notifications.user_did_something",
|
|
|
|
"%{key} %{omg}",
|
|
|
|
)
|
2017-06-15 04:08:23 -04:00
|
|
|
|
|
|
|
expect(translation_override.errors.full_messages).to include(
|
|
|
|
I18n.t(
|
2017-11-20 03:10:06 -05:00
|
|
|
"activerecord.errors.models.translation_overrides.attributes.value.invalid_interpolation_keys",
|
|
|
|
keys: "key, omg",
|
2023-02-20 04:31:02 -05:00
|
|
|
count: 2,
|
2023-01-09 06:18:21 -05:00
|
|
|
),
|
2017-06-15 04:08:23 -04:00
|
|
|
)
|
|
|
|
end
|
2017-11-20 03:10:06 -05:00
|
|
|
|
2021-03-24 15:58:24 -04:00
|
|
|
context "when custom interpolation keys are included" do
|
|
|
|
%w[
|
|
|
|
user_notifications.user_did_something
|
|
|
|
user_notifications.only_reply_by_email
|
|
|
|
user_notifications.only_reply_by_email_pm
|
|
|
|
user_notifications.reply_by_email
|
|
|
|
user_notifications.reply_by_email_pm
|
|
|
|
user_notifications.visit_link_to_respond
|
|
|
|
user_notifications.visit_link_to_respond_pm
|
|
|
|
].each do |i18n_key|
|
|
|
|
it "should validate keys for #{i18n_key}" do
|
|
|
|
interpolation_key_names =
|
|
|
|
described_class::ALLOWED_CUSTOM_INTERPOLATION_KEYS.find do |keys, _|
|
|
|
|
keys.include?("user_notifications.user_")
|
|
|
|
end
|
2023-01-09 06:18:21 -05:00
|
|
|
|
2021-03-24 15:58:24 -04:00
|
|
|
string_with_interpolation_keys =
|
|
|
|
interpolation_key_names.map { |x| "%{#{x}}" }.join(" ")
|
2023-01-09 06:18:21 -05:00
|
|
|
|
2021-03-24 15:58:24 -04:00
|
|
|
translation_override =
|
|
|
|
TranslationOverride.upsert!(
|
|
|
|
I18n.locale,
|
|
|
|
i18n_key,
|
|
|
|
"#{string_with_interpolation_keys} %{something}",
|
2023-01-09 06:18:21 -05:00
|
|
|
)
|
|
|
|
|
2017-11-20 03:10:06 -05:00
|
|
|
expect(translation_override.errors.full_messages).to include(
|
2023-01-09 06:18:21 -05:00
|
|
|
I18n.t(
|
2021-03-24 15:58:24 -04:00
|
|
|
"activerecord.errors.models.translation_overrides.attributes.value.invalid_interpolation_keys",
|
|
|
|
keys: "something",
|
2023-02-20 04:31:02 -05:00
|
|
|
count: 1,
|
2023-01-09 06:18:21 -05:00
|
|
|
),
|
2021-03-24 15:58:24 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should validate keys that shouldn't be used outside of user_notifications" do
|
|
|
|
I18n.backend.store_translations(:en, "not_a_notification" => "Test %{key1}")
|
2017-11-20 03:10:06 -05:00
|
|
|
translation_override =
|
|
|
|
TranslationOverride.upsert!(
|
|
|
|
I18n.locale,
|
2021-03-24 15:58:24 -04:00
|
|
|
"not_a_notification",
|
2022-04-06 17:17:20 -04:00
|
|
|
"Overridden %{key1} %{topic_title_url_encoded}",
|
2023-01-09 06:18:21 -05:00
|
|
|
)
|
2017-11-20 03:10:06 -05:00
|
|
|
expect(translation_override.errors.full_messages).to include(
|
2023-01-09 06:18:21 -05:00
|
|
|
I18n.t(
|
2021-03-24 15:58:24 -04:00
|
|
|
"activerecord.errors.models.translation_overrides.attributes.value.invalid_interpolation_keys",
|
|
|
|
keys: "topic_title_url_encoded",
|
2023-02-20 04:31:02 -05:00
|
|
|
count: 1,
|
2023-01-09 06:18:21 -05:00
|
|
|
),
|
2017-11-20 03:10:06 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2017-06-15 04:08:23 -04:00
|
|
|
end
|
2019-10-17 12:34:07 -04:00
|
|
|
|
2023-02-17 16:38:21 -05:00
|
|
|
describe "with valid custom interpolation keys" do
|
|
|
|
it "works" do
|
|
|
|
translation_override =
|
|
|
|
TranslationOverride.upsert!(
|
|
|
|
I18n.locale,
|
|
|
|
"system_messages.welcome_user.text_body_template",
|
|
|
|
"Hello %{name} %{username} %{name_or_username} and welcome to %{site_name}!",
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(translation_override.errors).to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-17 12:34:07 -04:00
|
|
|
describe "pluralized keys" do
|
|
|
|
describe "valid keys" do
|
|
|
|
it "converts zero to other" do
|
|
|
|
translation_override =
|
|
|
|
TranslationOverride.upsert!(I18n.locale, "something.zero", "%{key3} %{key4} hello")
|
|
|
|
expect(translation_override.errors.full_messages).to eq([])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "converts two to other" do
|
|
|
|
translation_override =
|
|
|
|
TranslationOverride.upsert!(I18n.locale, "something.two", "%{key3} %{key4} hello")
|
|
|
|
expect(translation_override.errors.full_messages).to eq([])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "converts few to other" do
|
|
|
|
translation_override =
|
|
|
|
TranslationOverride.upsert!(I18n.locale, "something.few", "%{key3} %{key4} hello")
|
|
|
|
expect(translation_override.errors.full_messages).to eq([])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "converts many to other" do
|
|
|
|
translation_override =
|
|
|
|
TranslationOverride.upsert!(I18n.locale, "something.many", "%{key3} %{key4} hello")
|
|
|
|
expect(translation_override.errors.full_messages).to eq([])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "invalid keys" do
|
|
|
|
it "does not transform 'tonz'" do
|
2024-05-24 10:15:53 -04:00
|
|
|
allow_missing_translations do
|
|
|
|
translation_override =
|
|
|
|
TranslationOverride.upsert!(I18n.locale, "something.tonz", "%{key3} %{key4} hello")
|
|
|
|
expect(translation_override.errors.full_messages).to include(
|
|
|
|
I18n.t(
|
|
|
|
"activerecord.errors.models.translation_overrides.attributes.value.invalid_interpolation_keys",
|
|
|
|
keys: "key3, key4",
|
|
|
|
count: 2,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
end
|
2019-10-17 12:34:07 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-06-15 04:08:23 -04:00
|
|
|
end
|
2024-07-25 10:56:08 -04:00
|
|
|
|
|
|
|
describe "MessageFormat translations" do
|
|
|
|
subject(:override) do
|
|
|
|
described_class.new(
|
|
|
|
translation_key: "admin_js.admin.user.delete_all_posts_confirm_MF",
|
|
|
|
locale: "en",
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it do
|
|
|
|
is_expected.to allow_value(
|
|
|
|
"This has {COUNT, plural, one{one member} other{# members}}.",
|
|
|
|
).for(:value).against(:base)
|
|
|
|
end
|
|
|
|
it do
|
|
|
|
is_expected.not_to allow_value(
|
|
|
|
"This has {COUNT, plural, one{one member} many{# members} other{# members}}.",
|
|
|
|
).for(:value).with_message(/plural case many is not valid/, against: :base)
|
|
|
|
end
|
|
|
|
it do
|
|
|
|
is_expected.not_to allow_value("This has {COUNT, ").for(:value).with_message(
|
|
|
|
/invalid syntax/,
|
|
|
|
against: :base,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2017-06-15 04:08:23 -04:00
|
|
|
end
|
2016-04-08 14:49:50 -04:00
|
|
|
|
|
|
|
it "upserts values" do
|
2024-05-24 10:15:53 -04:00
|
|
|
I18n.backend.store_translations(:en, { some: { key: "initial value" } })
|
2016-04-08 14:49:50 -04:00
|
|
|
TranslationOverride.upsert!("en", "some.key", "some value")
|
|
|
|
|
|
|
|
ovr = TranslationOverride.where(locale: "en", translation_key: "some.key").first
|
|
|
|
expect(ovr).to be_present
|
|
|
|
expect(ovr.value).to eq("some value")
|
|
|
|
end
|
|
|
|
|
2021-10-27 10:33:07 -04:00
|
|
|
it "sanitizes values before upsert" do
|
2022-08-09 14:43:42 -04:00
|
|
|
xss = "<a target='blank' href='%{path}'>Click here</a> <script>alert('TEST');</script>"
|
2021-10-27 10:33:07 -04:00
|
|
|
|
2022-08-09 14:43:42 -04:00
|
|
|
TranslationOverride.upsert!("en", "js.themes.error_caused_by", xss)
|
2021-10-27 10:33:07 -04:00
|
|
|
|
2022-08-09 14:43:42 -04:00
|
|
|
ovr =
|
|
|
|
TranslationOverride.where(locale: "en", translation_key: "js.themes.error_caused_by").first
|
2021-10-27 10:33:07 -04:00
|
|
|
expect(ovr).to be_present
|
2022-08-09 14:43:42 -04:00
|
|
|
expect(ovr.value).to eq("<a href=\"%{path}\">Click here</a> alert('TEST');")
|
2021-10-27 10:33:07 -04:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
describe "site cache" do
|
2020-05-18 12:45:47 -04:00
|
|
|
def cached_value(guardian, translation_key, locale:)
|
|
|
|
types_name, name_key, attribute = translation_key.split(".")
|
|
|
|
|
|
|
|
I18n.with_locale(locale) do
|
2020-05-18 12:05:34 -04:00
|
|
|
json = Site.json_for(guardian)
|
2018-03-16 16:46:49 -04:00
|
|
|
|
2020-05-18 12:05:34 -04:00
|
|
|
JSON.parse(json)[types_name].find { |x| x["name_key"] == name_key }[attribute]
|
|
|
|
end
|
2018-03-16 16:46:49 -04:00
|
|
|
end
|
|
|
|
|
2020-05-18 12:45:47 -04:00
|
|
|
let!(:anon_guardian) { Guardian.new }
|
|
|
|
let!(:user_guardian) { Guardian.new(Fabricate(:user)) }
|
|
|
|
|
2018-03-16 16:46:49 -04:00
|
|
|
shared_examples "resets site text" do
|
|
|
|
it "resets the site cache when translations of post_action_types are changed" do
|
2020-05-18 12:05:34 -04:00
|
|
|
I18n.locale = :de
|
|
|
|
|
|
|
|
translation_keys.each do |translation_key|
|
|
|
|
original_value = I18n.t(translation_key, locale: "en")
|
2020-05-18 12:45:47 -04:00
|
|
|
expect(cached_value(user_guardian, translation_key, locale: "en")).to eq(original_value)
|
|
|
|
expect(cached_value(anon_guardian, translation_key, locale: "en")).to eq(original_value)
|
2020-05-18 12:05:34 -04:00
|
|
|
|
|
|
|
TranslationOverride.upsert!("en", translation_key, "bar")
|
2020-05-18 12:45:47 -04:00
|
|
|
expect(cached_value(user_guardian, translation_key, locale: "en")).to eq("bar")
|
|
|
|
expect(cached_value(anon_guardian, translation_key, locale: "en")).to eq("bar")
|
2020-05-18 12:05:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
TranslationOverride.revert!("en", translation_keys)
|
2018-03-16 16:46:49 -04:00
|
|
|
|
2020-05-18 12:05:34 -04:00
|
|
|
translation_keys.each do |translation_key|
|
|
|
|
original_value = I18n.t(translation_key, locale: "en")
|
2020-05-18 12:45:47 -04:00
|
|
|
expect(cached_value(user_guardian, translation_key, locale: "en")).to eq(original_value)
|
|
|
|
expect(cached_value(anon_guardian, translation_key, locale: "en")).to eq(original_value)
|
2020-05-18 12:05:34 -04:00
|
|
|
end
|
2018-03-16 16:46:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with post_action_types" do
|
2020-05-18 12:05:34 -04:00
|
|
|
let(:translation_keys) { ["post_action_types.off_topic.description"] }
|
2018-03-16 16:46:49 -04:00
|
|
|
|
|
|
|
include_examples "resets site text"
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with topic_flag_types" do
|
2020-05-18 12:05:34 -04:00
|
|
|
let(:translation_keys) { ["topic_flag_types.spam.description"] }
|
|
|
|
|
|
|
|
include_examples "resets site text"
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with multiple keys" do
|
2020-05-18 12:05:34 -04:00
|
|
|
let(:translation_keys) do
|
|
|
|
%w[post_action_types.off_topic.description topic_flag_types.spam.description]
|
2023-01-09 06:18:21 -05:00
|
|
|
end
|
2018-03-16 16:46:49 -04:00
|
|
|
|
|
|
|
include_examples "resets site text"
|
|
|
|
end
|
2020-05-18 12:45:47 -04:00
|
|
|
|
|
|
|
describe "#reload_all_overrides!" do
|
|
|
|
it "correctly reloads all translation overrides" do
|
|
|
|
original_en_topics = I18n.t("topics", locale: :en)
|
|
|
|
original_en_emoji = I18n.t("js.composer.emoji", locale: :en)
|
|
|
|
original_en_offtopic_description =
|
|
|
|
I18n.t("post_action_types.off_topic.description", locale: :en)
|
|
|
|
original_de_likes = I18n.t("likes", locale: :de)
|
|
|
|
|
|
|
|
TranslationOverride.create!(locale: "en", translation_key: "topics", value: "Threads")
|
|
|
|
TranslationOverride.create!(
|
|
|
|
locale: "en",
|
|
|
|
translation_key: "js.composer.emoji",
|
|
|
|
value: "Smilies",
|
|
|
|
)
|
|
|
|
TranslationOverride.create!(
|
|
|
|
locale: "en",
|
|
|
|
translation_key: "post_action_types.off_topic.description",
|
|
|
|
value: "Overridden description",
|
|
|
|
)
|
|
|
|
TranslationOverride.create!(
|
|
|
|
locale: "de",
|
|
|
|
translation_key: "likes",
|
|
|
|
value: "„Gefällt mir“-Angaben",
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(I18n.t("topics", locale: :en)).to eq(original_en_topics)
|
|
|
|
expect(I18n.t("js.composer.emoji", locale: :en)).to eq(original_en_emoji)
|
|
|
|
expect(
|
|
|
|
cached_value(anon_guardian, "post_action_types.off_topic.description", locale: :en),
|
|
|
|
).to eq(original_en_offtopic_description)
|
|
|
|
expect(I18n.t("likes", locale: :de)).to eq(original_de_likes)
|
|
|
|
|
|
|
|
TranslationOverride.reload_all_overrides!
|
|
|
|
|
|
|
|
expect(I18n.t("topics", locale: :en)).to eq("Threads")
|
|
|
|
expect(I18n.t("js.composer.emoji", locale: :en)).to eq("Smilies")
|
|
|
|
expect(
|
|
|
|
cached_value(anon_guardian, "post_action_types.off_topic.description", locale: :en),
|
|
|
|
).to eq("Overridden description")
|
|
|
|
expect(I18n.t("likes", locale: :de)).to eq("„Gefällt mir“-Angaben")
|
|
|
|
|
|
|
|
TranslationOverride.revert!(
|
|
|
|
:en,
|
|
|
|
%w[topics js.composer.emoji post_action_types.off_topic.description],
|
|
|
|
)
|
|
|
|
TranslationOverride.revert!(:de, ["likes"])
|
|
|
|
end
|
|
|
|
end
|
2018-03-16 16:46:49 -04:00
|
|
|
end
|
2023-07-09 22:06:40 -04:00
|
|
|
|
2023-07-14 04:52:39 -04:00
|
|
|
describe "#original_translation_deleted?" do
|
|
|
|
context "when the original translation still exists" do
|
|
|
|
fab!(:translation) { Fabricate(:translation_override, translation_key: "title") }
|
|
|
|
|
|
|
|
it { expect(translation.original_translation_deleted?).to eq(false) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the original translation has been turned into a nested key" do
|
|
|
|
fab!(:translation) { Fabricate(:translation_override, translation_key: "title") }
|
|
|
|
|
|
|
|
before { translation.update_attribute("translation_key", "dates") }
|
|
|
|
|
|
|
|
it { expect(translation.original_translation_deleted?).to eq(true) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the original translation no longer exists" do
|
2024-05-24 10:15:53 -04:00
|
|
|
fab!(:translation) do
|
|
|
|
allow_missing_translations { Fabricate(:translation_override, translation_key: "foo.bar") }
|
|
|
|
end
|
2023-07-14 04:52:39 -04:00
|
|
|
|
|
|
|
it { expect(translation.original_translation_deleted?).to eq(true) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-07-09 22:06:40 -04:00
|
|
|
describe "#original_translation_updated?" do
|
|
|
|
context "when the translation is up to date" do
|
|
|
|
fab!(:translation) { Fabricate(:translation_override, translation_key: "title") }
|
|
|
|
|
|
|
|
it { expect(translation.original_translation_updated?).to eq(false) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the translation is outdated" do
|
|
|
|
fab!(:translation) do
|
|
|
|
Fabricate(:translation_override, translation_key: "title", original_translation: "outdated")
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(translation.original_translation_updated?).to eq(true) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when we can't tell because the translation is too old" do
|
|
|
|
fab!(:translation) do
|
|
|
|
Fabricate(:translation_override, translation_key: "title", original_translation: nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(translation.original_translation_updated?).to eq(false) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "invalid_interpolation_keys" do
|
|
|
|
fab!(:translation) do
|
|
|
|
Fabricate(
|
|
|
|
:translation_override,
|
|
|
|
translation_key: "system_messages.welcome_user.subject_template",
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "picks out invalid keys and ignores known and custom keys" do
|
|
|
|
translation.update_attribute("value", "Hello, %{name}! Welcome to %{site_name}. %{foo}")
|
|
|
|
|
|
|
|
expect(translation.invalid_interpolation_keys).to contain_exactly("foo")
|
|
|
|
end
|
|
|
|
end
|
2024-07-25 10:56:08 -04:00
|
|
|
|
|
|
|
describe "#message_format?" do
|
|
|
|
subject(:override) { described_class.new(translation_key: key) }
|
|
|
|
|
|
|
|
context "when override is for a MessageFormat translation" do
|
|
|
|
let(:key) { "admin_js.admin.user.delete_all_posts_confirm_MF" }
|
|
|
|
|
|
|
|
it { is_expected.to be_a_message_format }
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when override is not for a MessageFormat translation" do
|
|
|
|
let(:key) { "admin_js.type_to_filter" }
|
|
|
|
|
|
|
|
it { is_expected.not_to be_a_message_format }
|
|
|
|
end
|
|
|
|
end
|
2024-07-26 05:22:01 -04:00
|
|
|
|
|
|
|
describe "#make_up_to_date!" do
|
|
|
|
fab!(:override) { Fabricate(:translation_override, translation_key: "js.posts_likes_MF") }
|
|
|
|
|
|
|
|
context "when override is not outdated" do
|
|
|
|
it "does nothing" do
|
|
|
|
expect { override.make_up_to_date! }.not_to change { override.reload.attributes }
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns a falsy value" do
|
|
|
|
expect(override.make_up_to_date!).to be_falsy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when override is outdated" do
|
|
|
|
before { override.update_columns(status: :outdated, value: "{ Invalid MF syntax") }
|
|
|
|
|
|
|
|
it "updates its original translation to match the current default" do
|
|
|
|
expect { override.make_up_to_date! }.to change { override.reload.original_translation }.to(
|
|
|
|
I18n.t("js.posts_likes_MF"),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "sets its status to 'up_to_date'" do
|
|
|
|
expect { override.make_up_to_date! }.to change { override.reload.up_to_date? }.to(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns a truthy value" do
|
|
|
|
expect(override.make_up_to_date!).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-04-08 14:49:50 -04:00
|
|
|
end
|