2016-04-08 14:49:50 -04:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe TranslationOverride do
|
2017-06-15 04:08:23 -04:00
|
|
|
context 'validations' do
|
|
|
|
describe '#value' do
|
|
|
|
before do
|
2017-06-21 22:23:31 -04:00
|
|
|
I18n.backend.store_translations(:en, some_key: '%{first} %{second}')
|
2017-06-15 04:08:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when interpolation keys are missing' do
|
|
|
|
it 'should not be valid' do
|
|
|
|
translation_override = TranslationOverride.upsert!(
|
2017-11-20 03:10:06 -05:00
|
|
|
I18n.locale, 'some_key', '%{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'
|
2017-06-15 04:08:23 -04:00
|
|
|
))
|
|
|
|
end
|
2017-11-20 03:10:06 -05:00
|
|
|
|
|
|
|
context 'when custom interpolation keys are included' do
|
|
|
|
it 'should be valid' do
|
|
|
|
translation_override = TranslationOverride.upsert!(
|
|
|
|
I18n.locale,
|
|
|
|
'some_key',
|
|
|
|
"#{described_class::CUSTOM_INTERPOLATION_KEYS_WHITELIST['user_notifications.user_'].join(", ")} %{something}"
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(translation_override.errors.full_messages).to include(I18n.t(
|
|
|
|
'activerecord.errors.models.translation_overrides.attributes.value.invalid_interpolation_keys',
|
|
|
|
keys: 'something'
|
|
|
|
))
|
|
|
|
end
|
|
|
|
end
|
2017-06-15 04:08:23 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-04-08 14:49:50 -04:00
|
|
|
|
|
|
|
it "upserts values" do
|
|
|
|
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
|
|
|
|
|
|
|
|
it "stores js for a message format key" do
|
2018-02-07 06:47:01 -05:00
|
|
|
TranslationOverride.upsert!('ru', 'some.key_MF', '{NUM_RESULTS, plural, one {1 result} other {many} }')
|
2016-04-08 14:49:50 -04:00
|
|
|
|
2018-02-07 06:47:01 -05:00
|
|
|
ovr = TranslationOverride.where(locale: 'ru', translation_key: 'some.key_MF').first
|
2016-04-08 14:49:50 -04:00
|
|
|
expect(ovr).to be_present
|
2018-02-07 06:47:01 -05:00
|
|
|
expect(ovr.compiled_js).to start_with('function')
|
|
|
|
expect(ovr.compiled_js).to_not match(/Invalid Format/i)
|
2016-04-08 14:49:50 -04:00
|
|
|
end
|
|
|
|
|
2018-03-16 16:46:49 -04:00
|
|
|
context "site cache" do
|
|
|
|
def cached_value(guardian, types_name, name_key, attribute)
|
|
|
|
json = Site.json_for(guardian)
|
|
|
|
|
|
|
|
JSON.parse(json)[types_name]
|
|
|
|
.find { |x| x['name_key'] == name_key }[attribute]
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples "resets site text" do
|
|
|
|
it "resets the site cache when translations of post_action_types are changed" do
|
|
|
|
anon_guardian = Guardian.new
|
|
|
|
user_guardian = Guardian.new(Fabricate(:user))
|
|
|
|
original_value = I18n.t(translation_key)
|
|
|
|
types_name, name_key, attribute = translation_key.split('.')
|
|
|
|
|
|
|
|
expect(cached_value(user_guardian, types_name, name_key, attribute)).to eq(original_value)
|
|
|
|
expect(cached_value(anon_guardian, types_name, name_key, attribute)).to eq(original_value)
|
|
|
|
|
|
|
|
TranslationOverride.upsert!('en', translation_key, 'bar')
|
|
|
|
expect(cached_value(user_guardian, types_name, name_key, attribute)).to eq('bar')
|
|
|
|
expect(cached_value(anon_guardian, types_name, name_key, attribute)).to eq('bar')
|
|
|
|
|
|
|
|
TranslationOverride.revert!('en', translation_key)
|
|
|
|
expect(cached_value(user_guardian, types_name, name_key, attribute)).to eq(original_value)
|
|
|
|
expect(cached_value(anon_guardian, types_name, name_key, attribute)).to eq(original_value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "post_action_types" do
|
|
|
|
let(:translation_key) { 'post_action_types.off_topic.description' }
|
|
|
|
|
|
|
|
include_examples "resets site text"
|
|
|
|
end
|
|
|
|
|
|
|
|
context "topic_flag_types" do
|
|
|
|
let(:translation_key) { 'topic_flag_types.spam.description' }
|
|
|
|
|
|
|
|
include_examples "resets site text"
|
|
|
|
end
|
|
|
|
end
|
2016-04-08 14:49:50 -04:00
|
|
|
end
|