FIX: Restoring backup didn't clear cached translation overrides

FIX: Restoring backup didn’t clear cached translation overrides

diff --git a/app/models/translation_override.rb b/app/models/translation_override.rb
index e51edda..e883a0f 100644
--- a/app/models/translation_override.rb
+++ b/app/models/translation_override.rb
@@ -38,6 +38,16 @@ class TranslationOverride < ActiveRecord::Base
     i18n_changed(locale, keys)
   end
 
+  def self.reload_all_overrides!
+    reload_locale!
+
+    overrides = TranslationOverride.pluck(:locale, :translation_key)
+    overrides = overrides.group_by(&:first).map { |k, a| [k, a.map(&:last)] }
+    overrides.each do |locale, keys|
+      clear_cached_keys!(locale, keys)
+    end
+  end
+
   def self.reload_locale!
     I18n.reload!
     ExtraLocalesController.clear_cache!
diff --git a/lib/backup_restore/restorer.rb b/lib/backup_restore/restorer.rb
index 7a2b37d..30f4c09 100644
--- a/lib/backup_restore/restorer.rb
+++ b/lib/backup_restore/restorer.rb
@@ -55,6 +55,7 @@ module BackupRestore
       clear_category_cache
       clear_emoji_cache
       clear_theme_cache
+      reload_translations
 
       @uploads_restorer.restore(@tmp_directory)
 
@@ -131,6 +132,11 @@ module BackupRestore
       Emoji.clear_cache
     end
 
+    def reload_translations
+      log "Reloading translations..."
+      TranslationOverride.reload_all_overrides!
+    end
+
     def notify_user
       if user = User.find_by_email(@user_info[:email])
         log "Notifying '#{user.username}' of the end of the restore..."
diff --git a/spec/models/translation_override_spec.rb b/spec/models/translation_override_spec.rb
index 879c76e..8dd4c8e 100644
--- a/spec/models/translation_override_spec.rb
+++ b/spec/models/translation_override_spec.rb
@@ -92,8 +92,10 @@ describe TranslationOverride do
   end
 
   context "site cache" do
-    def cached_value(guardian, types_name, name_key, attribute)
-      I18n.with_locale(:en) do
+    def cached_value(guardian, translation_key, locale:)
+      types_name, name_key, attribute = translation_key.split('.')
+
+      I18n.with_locale(locale) do
         json = Site.json_for(guardian)
 
         JSON.parse(json)[types_name]
@@ -101,33 +103,29 @@ describe TranslationOverride do
       end
     end
 
+    let!(:anon_guardian) { Guardian.new }
+    let!(:user_guardian) { Guardian.new(Fabricate(:user)) }
+
     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))
-
         I18n.locale = :de
 
         translation_keys.each do |translation_key|
           original_value = I18n.t(translation_key, locale: 'en')
-          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)
+          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)
 
           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')
+          expect(cached_value(user_guardian, translation_key, locale: 'en')).to eq('bar')
+          expect(cached_value(anon_guardian, translation_key, locale: 'en')).to eq('bar')
         end
 
         TranslationOverride.revert!('en', translation_keys)
 
         translation_keys.each do |translation_key|
           original_value = I18n.t(translation_key, locale: 'en')
-          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)
+          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)
         end
       end
     end
@@ -149,5 +147,34 @@ describe TranslationOverride do
 
       include_examples "resets site text"
     end
+
+    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, ["topics", "js.composer.emoji", "post_action_types.off_topic.description"])
+        TranslationOverride.revert!(:de, ["likes"])
+      end
+    end
   end
 end

GitHub sha: 6d5e9db8

1 Like