Skip to content

Commit

Permalink
Merge pull request #1655 from tvdeyen/fix-admin-locale-setting
Browse files Browse the repository at this point in the history
Ensure the admin locale is only set by available locales
  • Loading branch information
tvdeyen authored Oct 23, 2019
2 parents 411fc9d + 98ceeee commit 8c327a4
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 60 deletions.
2 changes: 1 addition & 1 deletion lib/alchemy/admin/locale.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def locale_change_needed?
#
def available_locale
locales = [params[:admin_locale], locale_from_user, locale_from_browser].compact.map(&:to_sym)
locales.detect { |locale| ::I18n.available_locales.include?(locale) }
locales.detect { |locale| Alchemy::I18n.available_locales.include?(locale) }
end

# Try to get the locale from user settings.
Expand Down
76 changes: 46 additions & 30 deletions spec/controllers/alchemy/admin/translations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,29 @@

before { authorize_user(dummy_user) }

it "should be possible to set the locale of the admin backend via params" do
get :index, params: {admin_locale: 'nl'}
expect(::I18n.locale).to eq(:nl)
end
context 'if locale is available' do
before do
expect(Alchemy::I18n).to receive(:available_locales).at_least(:once) do
[:de, :nl, :en]
end
end

it "should store the current locale in the session" do
get :index, params: {admin_locale: 'nl'}
expect(session[:alchemy_locale]).to eq(:nl)
end
it "should be possible to set the locale of the admin backend via params" do
get :index, params: {admin_locale: 'nl'}
expect(::I18n.locale).to eq(:nl)
end

it "should store the current locale in the session" do
get :index, params: {admin_locale: 'nl'}
expect(session[:alchemy_locale]).to eq(:nl)
end

it "should be possible to change the current locale in the session" do
get :index, params: {admin_locale: 'de'}
expect(session[:alchemy_locale]).to eq(:de)
get :index, params: {admin_locale: 'en'}
expect(session[:alchemy_locale]).to eq(:en)
it "should be possible to change the current locale in the session" do
get :index, params: {admin_locale: 'de'}
expect(session[:alchemy_locale]).to eq(:de)
get :index, params: {admin_locale: 'en'}
expect(session[:alchemy_locale]).to eq(:en)
end
end

context 'with unknown locale' do
Expand All @@ -43,31 +51,39 @@
context "if user has no preferred locale" do
let(:dummy_user) { mock_model(Alchemy.user_class, alchemy_roles: %w(admin), language: nil) }

it "should use the browsers language setting" do
request.headers['ACCEPT-LANGUAGE'] = 'es-ES'
get :index
expect(::I18n.locale).to eq(:es)
end
end

context "if user language is an instance of a model" do
let(:language) { create(:alchemy_language) }
let(:dummy_user) { mock_model(Alchemy.user_class, alchemy_roles: %w(admin), language: language) }
context 'if locale is available' do
before do
expect(Alchemy::I18n).to receive(:available_locales).at_least(:once) do
[:es, :nl]
end
end

context "if language doesn't return a valid locale symbol" do
it "should use the browsers language setting" do
request.headers['ACCEPT-LANGUAGE'] = 'es-ES'
get :index
expect(::I18n.locale).to eq(:es)
end
end

context "if language returns a valid locale symbol" do
before { allow(language).to receive(:to_sym).and_return(:nl) }
context "if user language is an instance of a model" do
let(:language) { create(:alchemy_language) }
let(:dummy_user) { mock_model(Alchemy.user_class, alchemy_roles: %w(admin), language: language) }

it "should use the locale of the user language" do
get :index
expect(::I18n.locale).to eq(:nl)
context "if language doesn't return a valid locale symbol" do
it "should use the browsers language setting" do
request.headers['ACCEPT-LANGUAGE'] = 'es-ES'
get :index
expect(::I18n.locale).to eq(:es)
end
end

context "if language returns a valid locale symbol" do
before { allow(language).to receive(:to_sym).and_return(:nl) }

it "should use the locale of the user language" do
get :index
expect(::I18n.locale).to eq(:nl)
end
end
end
end
end
Expand Down
File renamed without changes.
26 changes: 26 additions & 0 deletions spec/dummy/config/locales/alchemy.en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
en:
alchemy:
element_names:
gallery_picture: Gallery picture
content_names:
essence_boolean: EssenceBoolean
essence_date: EssenceDate
essence_file: EssenceFile
essence_html: EssenceHtml
essence_link: EssenceLink
essence_picture: EssencePicture
essence_richtext: EssenceRichtext
essence_select: EssenceSelect
essence_text: EssenceText
element_hints:
all_you_can_eat: This element shows all content types (Essences) and settings of an Element Alchemy comes with in a default installation.
content_hints:
essence_boolean: This content type (Essence) represents a simple true/false value
essence_date: This content type (Essence) represents a date value
essence_file: This content type (Essence) represents a downloadable file
essence_html: This content type (Essence) represents raw HTML output
essence_link: This content type (Essence) represents a link
essence_picture: This content type (Essence) represents a picture
essence_richtext: This content type (Essence) represents richtext
essence_select: This content type (Essence) represents values the editor can choose from
essence_text: This content type (Essence) represents a simple line of text
3 changes: 3 additions & 0 deletions spec/dummy/config/locales/alchemy.kl.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kl:
alchemy:
welcome_back_note: majQa' Dub
26 changes: 0 additions & 26 deletions spec/dummy/config/locales/en.yml
Original file line number Diff line number Diff line change
@@ -1,30 +1,4 @@
en:
alchemy:
element_names:
gallery_picture: Gallery picture
content_names:
essence_boolean: EssenceBoolean
essence_date: EssenceDate
essence_file: EssenceFile
essence_html: EssenceHtml
essence_link: EssenceLink
essence_picture: EssencePicture
essence_richtext: EssenceRichtext
essence_select: EssenceSelect
essence_text: EssenceText
element_hints:
all_you_can_eat: This element shows all content types (Essences) and settings of an Element Alchemy comes with in a default installation.
content_hints:
essence_boolean: This content type (Essence) represents a simple true/false value
essence_date: This content type (Essence) represents a date value
essence_file: This content type (Essence) represents a downloadable file
essence_html: This content type (Essence) represents raw HTML output
essence_link: This content type (Essence) represents a link
essence_picture: This content type (Essence) represents a picture
essence_richtext: This content type (Essence) represents richtext
essence_select: This content type (Essence) represents values the editor can choose from
essence_text: This content type (Essence) represents a simple line of text

activerecord:
models:
event:
Expand Down
2 changes: 0 additions & 2 deletions spec/dummy/config/locales/kl.yml

This file was deleted.

23 changes: 22 additions & 1 deletion spec/features/admin/locale_select_feature_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

RSpec.describe 'Locale select', type: :system do
let(:a_page) { create(:alchemy_page, :public) }

before do
allow(Alchemy::I18n).to receive(:translation_files).and_return ['alchemy.kl.yml', 'alchemy.jp.yml', 'alchemy.cz.yml']
authorize_user(:as_admin)
Expand All @@ -19,9 +20,29 @@
allow(Alchemy::I18n).to receive(:available_locales).and_return [:jp, :cz]
end

it "provides only that locales" do
it "provides only these locales" do
visit admin_dashboard_path
expect(page).to have_select('change_locale', options: ['Jp', 'Cz'])
end
end

describe 'user selects locale' do
context 'that is available' do
before do
allow(Alchemy::I18n).to receive(:available_locales).and_return [:kl]
end

it 'switches the locale' do
visit admin_dashboard_path(admin_locale: 'kl')
expect(page).to have_content("majQa' Dub")
end
end

context 'that is not available' do
it 'does not switch the locale' do
visit admin_dashboard_path(admin_locale: 'de')
expect(page).to have_content("Welcome back")
end
end
end
end

0 comments on commit 8c327a4

Please sign in to comment.