From 69205cb1e5b97465ffda8a7cc4672a44ce7c8366 Mon Sep 17 00:00:00 2001 From: Ted Johansson Date: Fri, 24 May 2024 22:15:53 +0800 Subject: [PATCH] DEV: Catch missing translations during test runs (#26258) This configuration makes it so that a missing translation will raise an error during test execution. Better discover there than after deploy. --- app/models/user_email.rb | 2 +- config/environments/test.rb | 3 ++ config/locales/server.en.yml | 2 + ...50728210202_migrate_old_moderator_posts.rb | 2 + lib/archetype.rb | 5 ++ lib/freedom_patches/translate_accelerator.rb | 3 +- ...n_discourse_automation_automations_spec.rb | 21 +++++++- .../serializers/automation_serializer_spec.rb | 13 +++++ .../automation/spec/system/smoke_test_spec.rb | 28 ++++++++++ .../spec/triggers/stalled_wiki_spec.rb | 13 +++++ .../track_selector_spec.rb | 4 +- spec/jobs/check_translation_overrides_spec.rb | 4 +- .../regular/bulk_user_title_update_spec.rb | 4 ++ spec/lib/archetype_spec.rb | 3 +- .../translate_accelerator_spec.rb | 16 +++--- spec/lib/js_locale_helper_spec.rb | 45 ++++++++-------- spec/lib/plugin/instance_spec.rb | 2 + spec/lib/site_setting_extension_spec.rb | 2 + spec/models/incoming_links_report_spec.rb | 2 +- spec/models/translation_override_spec.rb | 34 +++++++----- spec/models/user_spec.rb | 2 +- spec/rails_helper.rb | 1 + .../admin/site_texts_controller_spec.rb | 52 ++++++++++--------- .../requests/extra_locales_controller_spec.rb | 2 + spec/requests/users_controller_spec.rb | 5 +- spec/script/import_scripts/base_spec.rb | 5 +- spec/services/badge_granter_spec.rb | 8 +++ .../translation_overrides_spec.rb | 2 + spec/support/i18n_helpers.rb | 10 ++++ 29 files changed, 219 insertions(+), 76 deletions(-) create mode 100644 spec/support/i18n_helpers.rb diff --git a/app/models/user_email.rb b/app/models/user_email.rb index ff175a9bfd9..036f1f07582 100644 --- a/app/models/user_email.rb +++ b/app/models/user_email.rb @@ -69,7 +69,7 @@ class UserEmail < ActiveRecord::Base self.errors.add( :user_id, I18n.t( - "active_record.errors.model.user_email.attributes.user_id.reassigning_primary_email", + "activerecord.errors.models.user_email.attributes.user_id.reassigning_primary_email", ), ) end diff --git a/config/environments/test.rb b/config/environments/test.rb index d59858c23c9..7f6bd425fdf 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -63,6 +63,9 @@ Discourse::Application.configure do }, ] + # Catch missing translations during test runs. + config.i18n.raise_on_missing_translations = true + config.after_initialize do ActiveRecord::LogSubscriber.backtrace_cleaner.add_silencer do |line| line =~ %r{lib/freedom_patches} diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 103a023d5c7..c68cbe0fafb 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -556,6 +556,7 @@ en: private_message_abbrev: "Msg" rss_description: + hot: "Hot topics" latest: "Latest topics" top: "Top topics" top_all: "All time top topics" @@ -1472,6 +1473,7 @@ en: description: "External sources that have linked to this site the most." top_referred_topics: title: "Top Referred Topics" + xaxis: "" labels: num_clicks: "Clicks" topic: "Topic" diff --git a/db/migrate/20150728210202_migrate_old_moderator_posts.rb b/db/migrate/20150728210202_migrate_old_moderator_posts.rb index 95ffb413efb..37f58715274 100644 --- a/db/migrate/20150728210202_migrate_old_moderator_posts.rb +++ b/db/migrate/20150728210202_migrate_old_moderator_posts.rb @@ -10,6 +10,7 @@ class MigrateOldModeratorPosts < ActiveRecord::Migration[4.2] end def up + Rails.application.config.i18n.raise_on_missing_translations = false migrate_key("closed.enabled") migrate_key("closed.disabled") migrate_key("archived.enabled") @@ -18,5 +19,6 @@ class MigrateOldModeratorPosts < ActiveRecord::Migration[4.2] migrate_key("pinned.disabled") migrate_key("pinned_globally.enabled") migrate_key("pinned_globally.disabled") + Rails.application.config.i18n.raise_on_missing_translations = true end end diff --git a/lib/archetype.rb b/lib/archetype.rb index a9380d59d98..6d8b3619018 100644 --- a/lib/archetype.rb +++ b/lib/archetype.rb @@ -36,6 +36,11 @@ class Archetype @archetypes[name] = Archetype.new(name, options) end + def self.deregister(name) + @archetypes ||= {} + @archetypes.delete(name) + end + # default archetypes register "regular" register "private_message" diff --git a/lib/freedom_patches/translate_accelerator.rb b/lib/freedom_patches/translate_accelerator.rb index 4aa394d3dc4..79c2b9e1615 100644 --- a/lib/freedom_patches/translate_accelerator.rb +++ b/lib/freedom_patches/translate_accelerator.rb @@ -122,7 +122,8 @@ module I18n dup_options = nil if options dup_options = options.dup - should_raise = dup_options.delete(:raise) + should_raise = + dup_options.delete(:raise) || Rails.application.config.i18n.raise_on_missing_translations locale = dup_options.delete(:locale) end diff --git a/plugins/automation/spec/requests/admin_discourse_automation_automations_spec.rb b/plugins/automation/spec/requests/admin_discourse_automation_automations_spec.rb index ea1ef62fdce..28c7e139f42 100644 --- a/plugins/automation/spec/requests/admin_discourse_automation_automations_spec.rb +++ b/plugins/automation/spec/requests/admin_discourse_automation_automations_spec.rb @@ -3,7 +3,26 @@ describe DiscourseAutomation::AdminAutomationsController do fab!(:automation) - before { SiteSetting.discourse_automation_enabled = true } + before do + SiteSetting.discourse_automation_enabled = true + I18n.backend.store_translations( + :en, + { + discourse_automation: { + scriptables: { + something_about_us: { + title: "Something about us.", + description: "We rock!", + }, + }, + triggerables: { + title: "Triggerables", + description: "Triggerables", + }, + }, + }, + ) + end describe "#show" do context "when logged in as an admin" do diff --git a/plugins/automation/spec/serializers/automation_serializer_spec.rb b/plugins/automation/spec/serializers/automation_serializer_spec.rb index 86fa98aa488..df807b5f2fb 100644 --- a/plugins/automation/spec/serializers/automation_serializer_spec.rb +++ b/plugins/automation/spec/serializers/automation_serializer_spec.rb @@ -41,6 +41,19 @@ describe DiscourseAutomation::AutomationSerializer do DiscourseAutomation::Scriptable.add("foo") do field :bar, component: :text, triggerable: DiscourseAutomation::Triggers::TOPIC end + I18n.backend.store_translations( + :en, + { + discourse_automation: { + scriptables: { + foo: { + title: "Something about us.", + description: "We rock!", + }, + }, + }, + }, + ) end context "when automation is not using the specific trigger" do diff --git a/plugins/automation/spec/system/smoke_test_spec.rb b/plugins/automation/spec/system/smoke_test_spec.rb index 30f7488c520..1de8ec3720b 100644 --- a/plugins/automation/spec/system/smoke_test_spec.rb +++ b/plugins/automation/spec/system/smoke_test_spec.rb @@ -6,6 +6,34 @@ describe "DiscourseAutomation | smoke test", type: :system, js: true do fab!(:badge) { Fabricate(:badge, name: "badge") } before do + I18n.backend.store_translations( + :en, + { + discourse_automation: { + scriptables: { + test: { + title: "Test", + description: "Test", + }, + something_about_us: { + title: "Something about us.", + description: "We rock!", + }, + nothing_about_us: { + title: "Nothing about us.", + description: "We don't rock!", + }, + }, + triggerables: { + title: "Triggerable", + description: "Triggerable", + user_first_logged_in: { + description: "User first logged in.", + }, + }, + }, + }, + ) SiteSetting.discourse_automation_enabled = true sign_in(admin) end diff --git a/plugins/automation/spec/triggers/stalled_wiki_spec.rb b/plugins/automation/spec/triggers/stalled_wiki_spec.rb index 0ad2cca482a..340f4e9579b 100644 --- a/plugins/automation/spec/triggers/stalled_wiki_spec.rb +++ b/plugins/automation/spec/triggers/stalled_wiki_spec.rb @@ -23,6 +23,19 @@ describe "StalledWiki" do before do automation.upsert_field!("stalled_after", "choices", { value: "PT10H" }, target: "trigger") automation.upsert_field!("retriggered_after", "choices", { value: "PT1H" }, target: "trigger") + I18n.backend.store_translations( + :en, + { + discourse_automation: { + scriptables: { + something_about_us: { + title: "Something about us.", + description: "We rock!", + }, + }, + }, + }, + ) end it "supports manual triggering" do diff --git a/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/track_selector_spec.rb b/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/track_selector_spec.rb index 7759a74cb24..4a346568fab 100644 --- a/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/track_selector_spec.rb +++ b/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/track_selector_spec.rb @@ -615,7 +615,7 @@ RSpec.describe DiscourseNarrativeBot::TrackSelector do it "should not trigger the bot" do post.update!( raw: - "`@discobot #{I18n.t("discourse_narrative_bot.track_selector.reset_trigger")} #{I18n.t(DiscourseNarrativeBot::NewUserNarrative.reset_trigger)}`", + "`@discobot #{I18n.t("discourse_narrative_bot.track_selector.reset_trigger")} #{DiscourseNarrativeBot::NewUserNarrative.reset_trigger}`", ) expect { described_class.new(:reply, user, post_id: post.id).select }.to_not change { @@ -750,7 +750,7 @@ RSpec.describe DiscourseNarrativeBot::TrackSelector do user: Fabricate(:user), topic: topic, raw: - "@discobot #{I18n.t("discourse_narrative_bot.track_selector.reset_trigger")} #{I18n.t(DiscourseNarrativeBot::NewUserNarrative.reset_trigger)}", + "@discobot #{I18n.t("discourse_narrative_bot.track_selector.reset_trigger")} #{DiscourseNarrativeBot::NewUserNarrative.reset_trigger}", ) user diff --git a/spec/jobs/check_translation_overrides_spec.rb b/spec/jobs/check_translation_overrides_spec.rb index 2bd0e63dd44..fd776d144c6 100644 --- a/spec/jobs/check_translation_overrides_spec.rb +++ b/spec/jobs/check_translation_overrides_spec.rb @@ -2,7 +2,9 @@ RSpec.describe Jobs::CheckTranslationOverrides do fab!(:up_to_date_translation) { Fabricate(:translation_override, translation_key: "title") } - fab!(:deprecated_translation) { Fabricate(:translation_override, translation_key: "foo.bar") } + fab!(:deprecated_translation) do + allow_missing_translations { Fabricate(:translation_override, translation_key: "foo.bar") } + end fab!(:outdated_translation) do Fabricate(:translation_override, translation_key: "posts", original_translation: "outdated") end diff --git a/spec/jobs/regular/bulk_user_title_update_spec.rb b/spec/jobs/regular/bulk_user_title_update_spec.rb index d43c12ecab5..fef11fd140f 100644 --- a/spec/jobs/regular/bulk_user_title_update_spec.rb +++ b/spec/jobs/regular/bulk_user_title_update_spec.rb @@ -34,6 +34,10 @@ RSpec.describe Jobs::BulkUserTitleUpdate do let(:customized_badge_name) { "Merit Badge" } before do + I18n.backend.store_translations( + :en, + { badges: { protector_of_the_realm: { name: "Protector of the Realm" } } }, + ) TranslationOverride.upsert!(I18n.locale, Badge.i18n_key(badge.name), customized_badge_name) BadgeGranter.grant(badge, user) user.update(title: customized_badge_name) diff --git a/spec/lib/archetype_spec.rb b/spec/lib/archetype_spec.rb index 98430120729..2f27d4e500e 100644 --- a/spec/lib/archetype_spec.rb +++ b/spec/lib/archetype_spec.rb @@ -25,12 +25,13 @@ RSpec.describe Archetype do end end - describe "registerĀ an archetype" do + describe "register an archetype" do it "has one more element" do @list = Archetype.list.dup Archetype.register("glados") expect(Archetype.list.size).to eq(@list.size + 1) expect(Archetype.list.find { |a| a.id == "glados" }).to be_present + Archetype.deregister("glados") end end end diff --git a/spec/lib/freedom_patches/translate_accelerator_spec.rb b/spec/lib/freedom_patches/translate_accelerator_spec.rb index c7a019ed554..0c9d3ac0c60 100644 --- a/spec/lib/freedom_patches/translate_accelerator_spec.rb +++ b/spec/lib/freedom_patches/translate_accelerator_spec.rb @@ -23,16 +23,20 @@ RSpec.describe "translate accelerator" do I18n::MissingTranslationData, ) - orig = I18n.t("i_am_an_unknown_key99") + allow_missing_translations do + orig = I18n.t("i_am_an_unknown_key99") - expect(I18n.t("i_am_an_unknown_key99").object_id).to eq(orig.object_id) - expect(I18n.t("i_am_an_unknown_key99")).to eq("Translation missing: en.i_am_an_unknown_key99") + expect(I18n.t("i_am_an_unknown_key99").object_id).to eq(orig.object_id) + expect(I18n.t("i_am_an_unknown_key99")).to eq("Translation missing: en.i_am_an_unknown_key99") + end end it "has the same 'translation missing' message as upstream" do - expect(I18n.t("this_key_does_not_exist")).to eq( - I18n.translate_no_cache("this_key_does_not_exist"), - ) + allow_missing_translations do + expect(I18n.t("this_key_does_not_exist")).to eq( + I18n.translate_no_cache("this_key_does_not_exist"), + ) + end end it "returns the correct language" do diff --git a/spec/lib/js_locale_helper_spec.rb b/spec/lib/js_locale_helper_spec.rb index 66498886b6b..7d3cd760232 100644 --- a/spec/lib/js_locale_helper_spec.rb +++ b/spec/lib/js_locale_helper_spec.rb @@ -236,31 +236,32 @@ RSpec.describe JsLocaleHelper do end it "correctly evaluates message formats in en fallback" do - JsLocaleHelper.set_translations("en", "en" => { "js" => { "something_MF" => "en mf" } }) + allow_missing_translations do + JsLocaleHelper.set_translations("en", "en" => { "js" => { "something_MF" => "en mf" } }) + JsLocaleHelper.set_translations("de", "de" => { "js" => { "something_MF" => "de mf" } }) - JsLocaleHelper.set_translations("de", "de" => { "js" => { "something_MF" => "de mf" } }) + TranslationOverride.upsert!("en", "js.something_MF", <<~MF.strip) + There { + UNREAD, plural, + =0 {are no} + one {is one unread} + other {are # unread} + } + MF - TranslationOverride.upsert!("en", "js.something_MF", <<~MF.strip) - There { - UNREAD, plural, - =0 {are no} - one {is one unread} - other {are # unread} - } - MF + v8_ctx.eval(JsLocaleHelper.output_locale("de")) + v8_ctx.eval(JsLocaleHelper.output_client_overrides("de")) + v8_ctx.eval(<<~JS) + for (let [key, value] of Object.entries(I18n._mfOverrides || {})) { + key = key.replace(/^[a-z_]*js\./, ""); + I18n._compiledMFs[key] = value; + } + JS - v8_ctx.eval(JsLocaleHelper.output_locale("de")) - v8_ctx.eval(JsLocaleHelper.output_client_overrides("de")) - v8_ctx.eval(<<~JS) - for (let [key, value] of Object.entries(I18n._mfOverrides || {})) { - key = key.replace(/^[a-z_]*js\./, ""); - I18n._compiledMFs[key] = value; - } - JS - - expect(v8_ctx.eval("I18n.messageFormat('something_MF', { UNREAD: 1 })")).to eq( - "There is one unread", - ) + expect(v8_ctx.eval("I18n.messageFormat('something_MF', { UNREAD: 1 })")).to eq( + "There is one unread", + ) + end end LocaleSiteSetting.values.each do |locale| diff --git a/spec/lib/plugin/instance_spec.rb b/spec/lib/plugin/instance_spec.rb index 4488bb45207..eb69b610340 100644 --- a/spec/lib/plugin/instance_spec.rb +++ b/spec/lib/plugin/instance_spec.rb @@ -17,6 +17,8 @@ RSpec.describe Plugin::Instance do some_ruby TEXT + around { |example| allow_missing_translations(&example) } + after { DiscoursePluginRegistry.reset! } # NOTE: sample_plugin_site_settings.yml is always loaded in tests in site_setting.rb diff --git a/spec/lib/site_setting_extension_spec.rb b/spec/lib/site_setting_extension_spec.rb index d8625a4c799..99d0dacabe6 100644 --- a/spec/lib/site_setting_extension_spec.rb +++ b/spec/lib/site_setting_extension_spec.rb @@ -330,6 +330,8 @@ RSpec.describe SiteSettingExtension do describe "string setting with regex" do it "Supports custom validation errors" do + I18n.backend.store_translations(:en, { oops: "oops" }) + settings.setting(:test_str, "bob", regex: "hi", regex_error: "oops") settings.refresh! diff --git a/spec/models/incoming_links_report_spec.rb b/spec/models/incoming_links_report_spec.rb index 9712b125d29..251dd32201e 100644 --- a/spec/models/incoming_links_report_spec.rb +++ b/spec/models/incoming_links_report_spec.rb @@ -282,7 +282,7 @@ RSpec.describe IncomingLinksReport do it "returns localized titles" do stub_empty_referred_topics_data expect(top_referred_topics[:title]).to be_present - expect(top_referred_topics[:xaxis]).to be_present + expect(top_referred_topics[:xaxis]).to be_blank expect(top_referred_topics[:ytitles]).to be_present expect(top_referred_topics[:ytitles][:num_clicks]).to be_present end diff --git a/spec/models/translation_override_spec.rb b/spec/models/translation_override_spec.rb index 54e0caceafc..d9080f89f79 100644 --- a/spec/models/translation_override_spec.rb +++ b/spec/models/translation_override_spec.rb @@ -6,7 +6,7 @@ RSpec.describe TranslationOverride do before do I18n.backend.store_translations( I18n.locale, - "user_notifications.user_did_something" => "%{first} %{second}", + { user_notifications: { user_did_something: "%{first} %{second}" } }, ) I18n.backend.store_translations( @@ -21,7 +21,11 @@ RSpec.describe TranslationOverride do describe "when interpolation keys are missing" do it "should not be valid" do translation_override = - TranslationOverride.upsert!(I18n.locale, "some_key", "%{key} %{omg}") + TranslationOverride.upsert!( + I18n.locale, + "user_notifications.user_did_something", + "%{key} %{omg}", + ) expect(translation_override.errors.full_messages).to include( I18n.t( @@ -129,15 +133,17 @@ RSpec.describe TranslationOverride do describe "invalid keys" do it "does not transform 'tonz'" 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, - ), - ) + 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 end end end @@ -145,6 +151,7 @@ RSpec.describe TranslationOverride do end it "upserts values" do + I18n.backend.store_translations(:en, { some: { key: "initial value" } }) TranslationOverride.upsert!("en", "some.key", "some value") ovr = TranslationOverride.where(locale: "en", translation_key: "some.key").first @@ -164,6 +171,7 @@ RSpec.describe TranslationOverride do end it "stores js for a message format key" do + I18n.backend.store_translations(:en, { some: { key_MF: "initial value" } }) TranslationOverride.upsert!( "ru", "some.key_MF", @@ -300,7 +308,9 @@ RSpec.describe TranslationOverride do end context "when the original translation no longer exists" do - fab!(:translation) { Fabricate(:translation_override, translation_key: "foo.bar") } + fab!(:translation) do + allow_missing_translations { Fabricate(:translation_override, translation_key: "foo.bar") } + end it { expect(translation.original_translation_deleted?).to eq(true) } end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 49423bd4eb8..1c0cc5e2125 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -2746,7 +2746,7 @@ RSpec.describe User do end describe "#title=" do - fab!(:badge) { Fabricate(:badge, name: "Badge", allow_title: false) } + fab!(:badge) { Badge.find_by(name: "Welcome") } it "sets granted_title_badge_id correctly" do BadgeGranter.grant(badge, user) diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 5db7091bd3f..74fac5d4bdc 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -204,6 +204,7 @@ RSpec.configure do |config| config.include FastImageHelpers config.include WithServiceHelper config.include ServiceMatchers + config.include I18nHelpers config.mock_framework = :mocha config.order = "random" diff --git a/spec/requests/admin/site_texts_controller_spec.rb b/spec/requests/admin/site_texts_controller_spec.rb index 2c9bae2e45d..02c6b15aeaf 100644 --- a/spec/requests/admin/site_texts_controller_spec.rb +++ b/spec/requests/admin/site_texts_controller_spec.rb @@ -108,33 +108,35 @@ RSpec.describe Admin::SiteTextsController do end it "does not return overrides for keys that do not exist in English" do - SiteSetting.default_locale = :ru - TranslationOverride.create!( - locale: :ru, - translation_key: "missing_plural_key.one", - value: "ONE", - ) - TranslationOverride.create!( - locale: :ru, - translation_key: "another_missing_key", - value: "foo", - ) + allow_missing_translations do + SiteSetting.default_locale = :ru + TranslationOverride.create!( + locale: :ru, + translation_key: "missing_plural_key.one", + value: "ONE", + ) + TranslationOverride.create!( + locale: :ru, + translation_key: "another_missing_key", + value: "foo", + ) - get "/admin/customize/site_texts.json", - params: { - q: "missing_plural_key", - locale: default_locale, - } - expect(response.status).to eq(200) - expect(response.parsed_body["site_texts"]).to be_empty + get "/admin/customize/site_texts.json", + params: { + q: "missing_plural_key", + locale: default_locale, + } + expect(response.status).to eq(200) + expect(response.parsed_body["site_texts"]).to be_empty - get "/admin/customize/site_texts.json", - params: { - q: "another_missing_key", - locale: default_locale, - } - expect(response.status).to eq(200) - expect(response.parsed_body["site_texts"]).to be_empty + get "/admin/customize/site_texts.json", + params: { + q: "another_missing_key", + locale: default_locale, + } + expect(response.status).to eq(200) + expect(response.parsed_body["site_texts"]).to be_empty + end end it "returns site text from fallback locale if current locale doesn't have a translation" do diff --git a/spec/requests/extra_locales_controller_spec.rb b/spec/requests/extra_locales_controller_spec.rb index 35d554b6a83..a9573510905 100644 --- a/spec/requests/extra_locales_controller_spec.rb +++ b/spec/requests/extra_locales_controller_spec.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true RSpec.describe ExtraLocalesController do + around { |example| allow_missing_translations(&example) } + describe "#show" do it "won't work with a weird parameter" do get "/extra-locales/-invalid..character!!" diff --git a/spec/requests/users_controller_spec.rb b/spec/requests/users_controller_spec.rb index 49e186ab392..b43f104a37d 100644 --- a/spec/requests/users_controller_spec.rb +++ b/spec/requests/users_controller_spec.rb @@ -3172,7 +3172,10 @@ RSpec.describe UsersController do fab!(:badge) { Fabricate(:badge, name: "Demogorgon", allow_title: true) } let(:user_badge) { BadgeGranter.grant(badge, user1) } - before { TranslationOverride.upsert!("en", "badges.demogorgon.name", "Boss") } + before do + I18n.backend.store_translations(:en, { badges: { demogorgon: { name: "D'Artagnan" } } }) + TranslationOverride.upsert!("en", "badges.demogorgon.name", "Boss") + end after { TranslationOverride.revert!("en", ["badges.demogorgon.name"]) } diff --git a/spec/script/import_scripts/base_spec.rb b/spec/script/import_scripts/base_spec.rb index e7de2e82558..2be030210af 100644 --- a/spec/script/import_scripts/base_spec.rb +++ b/spec/script/import_scripts/base_spec.rb @@ -3,7 +3,10 @@ require_relative "../../../script/import_scripts/base" RSpec.describe ImportScripts::Base do - before { STDOUT.stubs(:write) } + before do + I18n.backend.store_translations(:en, { test: "Test" }) + STDOUT.stubs(:write) + end class MockSpecImporter < ImportScripts::Base def initialize(data) diff --git a/spec/services/badge_granter_spec.rb b/spec/services/badge_granter_spec.rb index 91ecf6b4c43..5c95418e587 100644 --- a/spec/services/badge_granter_spec.rb +++ b/spec/services/badge_granter_spec.rb @@ -330,6 +330,10 @@ RSpec.describe BadgeGranter do let(:customized_badge_name) { "Merit Badge" } before do + I18n.backend.store_translations( + :en, + { badges: { Badge.i18n_name(badge.name) => { name: "Badge 0" } } }, + ) TranslationOverride.upsert!(I18n.locale, Badge.i18n_key(badge.name), customized_badge_name) end @@ -381,6 +385,10 @@ RSpec.describe BadgeGranter do it "removes custom badge titles" do custom_badge_title = "this is a badge title" + I18n.backend.store_translations( + :en, + { badges: { Badge.i18n_name(badge.name) => { name: "Badge 0" } } }, + ) TranslationOverride.create!( translation_key: badge.translation_key, value: custom_badge_title, diff --git a/spec/services/problem_check/translation_overrides_spec.rb b/spec/services/problem_check/translation_overrides_spec.rb index 88f3dadc907..e3efdba75aa 100644 --- a/spec/services/problem_check/translation_overrides_spec.rb +++ b/spec/services/problem_check/translation_overrides_spec.rb @@ -3,6 +3,8 @@ RSpec.describe ProblemCheck::TranslationOverrides do subject(:check) { described_class.new } + around { |example| allow_missing_translations(&example) } + describe ".call" do before { Fabricate(:translation_override, status: status) } diff --git a/spec/support/i18n_helpers.rb b/spec/support/i18n_helpers.rb new file mode 100644 index 00000000000..4e178d59042 --- /dev/null +++ b/spec/support/i18n_helpers.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +module I18nHelpers + def allow_missing_translations + Rails.application.config.i18n.raise_on_missing_translations = false + yield + ensure + Rails.application.config.i18n.raise_on_missing_translations = true + end +end