diff --git a/app/controllers/concerns/alchemy/admin/current_language.rb b/app/controllers/concerns/alchemy/admin/current_language.rb index 4cfee7ee1b..f68604a63d 100644 --- a/app/controllers/concerns/alchemy/admin/current_language.rb +++ b/app/controllers/concerns/alchemy/admin/current_language.rb @@ -13,10 +13,10 @@ module CurrentLanguage def load_current_language @current_language = if session[:alchemy_language_id].present? - if current_alchemy_site.languages.where(id: session[:alchemy_language_id]).exists? + if Current.site.languages.where(id: session[:alchemy_language_id]).exists? set_alchemy_language(session[:alchemy_language_id]) else - current_alchemy_site.default_language + Current.site.default_language end else Current.language diff --git a/lib/alchemy/test_support/current_language_shared_examples.rb b/lib/alchemy/test_support/current_language_shared_examples.rb new file mode 100644 index 0000000000..09f61c05ab --- /dev/null +++ b/lib/alchemy/test_support/current_language_shared_examples.rb @@ -0,0 +1,48 @@ +# frozen_string_literal: true + +RSpec.shared_examples_for "a controller that loads current language" do |args| + context "when session has current language id key" do + let!(:site_1) { create(:alchemy_site) } + let!(:site_1_default_language) { create :alchemy_language, site: site_1, default: true } + let!(:another_site_1_language) { create :alchemy_language, site: site_1, code: :de } + let(:site_2) { create :alchemy_site, host: 'another.host', languages: [build(:alchemy_language, code: :en), build(:alchemy_language, code: :de)] } + + before { session[:alchemy_language_id] = another_site_1_language.id } + + context "when language ID in session is associated with the current site" do + it "sets @current_language" do + get :index, params: { site_id: site_1.id } + expect(assigns(:current_language)).to eq(another_site_1_language) + end + end + + context "when language ID in session is not associated with the current site" do + it "sets @current_language to the current site default language" do + get :index, params: { site_id: site_2.id } + expect(assigns(:current_language)).to eq(site_2.default_language) + end + + it "does not change the language ID in session" do + expect { get :index, params: { site_id: site_2.id } }.not_to change { session[:alchemy_language_id] } + end + end + + context "when no language ID in session" do + before { session[:alchemy_language_id] = nil } + + it "sets @current_language to language language" do + get :index, params: { site_id: site_2.id } + expect(assigns(:current_language)).to eq site_2.default_language + end + end + + context "when no language to set" do + it "shows flash warning with redirect" do + Alchemy::Language.destroy_all + get :index, params: { site_id: site_1.id } + expect(flash[:warning]).to eq Alchemy.t("Please create a language first.") + expect(response).to redirect_to admin_languages_path + end + end + end +end diff --git a/spec/controllers/alchemy/admin/layoutpages_controller_spec.rb b/spec/controllers/alchemy/admin/layoutpages_controller_spec.rb index f344edb188..29b4d707a9 100644 --- a/spec/controllers/alchemy/admin/layoutpages_controller_spec.rb +++ b/spec/controllers/alchemy/admin/layoutpages_controller_spec.rb @@ -10,6 +10,8 @@ module Alchemy authorize_user(:as_admin) end + it_behaves_like "a controller that loads current language" + describe "#index" do context "with no language present" do it "redirects to the languages admin" do diff --git a/spec/controllers/alchemy/admin/nodes_controller_spec.rb b/spec/controllers/alchemy/admin/nodes_controller_spec.rb index 530a604610..c2523225b6 100644 --- a/spec/controllers/alchemy/admin/nodes_controller_spec.rb +++ b/spec/controllers/alchemy/admin/nodes_controller_spec.rb @@ -10,6 +10,8 @@ module Alchemy authorize_user(:as_admin) end + it_behaves_like "a controller that loads current language" + describe "#index" do context "if no language is present" do it "redirects to the language admin" do diff --git a/spec/controllers/alchemy/admin/pages_controller_spec.rb b/spec/controllers/alchemy/admin/pages_controller_spec.rb index cbb248ad44..2c201a9800 100644 --- a/spec/controllers/alchemy/admin/pages_controller_spec.rb +++ b/spec/controllers/alchemy/admin/pages_controller_spec.rb @@ -10,6 +10,8 @@ authorize_user(:as_admin) end + it_behaves_like "a controller that loads current language" + describe "#index" do let!(:page) { create(:alchemy_page) } @@ -45,6 +47,8 @@ end end end + + end describe "#destroy" do diff --git a/spec/controllers/alchemy/admin/pictures_controller_spec.rb b/spec/controllers/alchemy/admin/pictures_controller_spec.rb index ea546feabc..ac5e8dda8d 100644 --- a/spec/controllers/alchemy/admin/pictures_controller_spec.rb +++ b/spec/controllers/alchemy/admin/pictures_controller_spec.rb @@ -26,6 +26,8 @@ module Alchemy authorize_user(:as_admin) end + it_behaves_like "a controller that loads current language" + let!(:language) { create(:alchemy_language) } describe "#index" do diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index deb1819860..46d7f33096 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -31,6 +31,7 @@ require "alchemy/test_support/shared_contexts" require "alchemy/test_support/shared_link_tab_examples" require "alchemy/test_support/shared_uploader_examples" +require "alchemy/test_support/current_language_shared_examples" require_relative "support/calculation_examples" require_relative "support/hint_examples"