From 36ee4974ef447570731f593c3363ddd3f380b8d4 Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Tue, 23 Jun 2020 10:46:07 +0200 Subject: [PATCH 1/2] Create default language in host app's locale Our default language factory will create a page in German. That's not great for apps that do not have German as an `available_locale`, as Alchemy now validates the language_code of the language. This change adds a new trait "german" to satisfy our existing test suite, but creates by default a language in the host apps first available locale. --- .../test_support/factories/language_factory.rb | 11 ++++++++--- spec/controllers/alchemy/pages_controller_spec.rb | 10 +++++----- spec/features/admin/languages_features_spec.rb | 2 +- spec/helpers/alchemy/pages_helper_spec.rb | 4 ++-- spec/libraries/configuration_methods_spec.rb | 2 +- spec/models/alchemy/language_spec.rb | 2 +- spec/models/alchemy/page_spec.rb | 2 +- spec/models/alchemy/site_spec.rb | 2 +- 8 files changed, 20 insertions(+), 15 deletions(-) diff --git a/lib/alchemy/test_support/factories/language_factory.rb b/lib/alchemy/test_support/factories/language_factory.rb index 8255b2c665..3bbc4d47e5 100644 --- a/lib/alchemy/test_support/factories/language_factory.rb +++ b/lib/alchemy/test_support/factories/language_factory.rb @@ -5,8 +5,8 @@ FactoryBot.define do factory :alchemy_language, class: "Alchemy::Language" do - name { "Deutsch" } - code { "de" } + name { "Your Language" } + code { ::I18n.available_locales.first.to_s } default { true } frontpage_name { "Intro" } page_layout { Alchemy::Config.get(:default_language)["page_layout"] } @@ -25,7 +25,12 @@ trait :english do name { "English" } code { "en" } - frontpage_name { "Intro" } + default { false } + end + + trait :german do + name { "Deutsch" } + code { "de" } default { false } end end diff --git a/spec/controllers/alchemy/pages_controller_spec.rb b/spec/controllers/alchemy/pages_controller_spec.rb index 5625b9643b..1a01297168 100644 --- a/spec/controllers/alchemy/pages_controller_spec.rb +++ b/spec/controllers/alchemy/pages_controller_spec.rb @@ -138,13 +138,13 @@ module Alchemy end context "requesting non default locale" do - let!(:english) do - create(:alchemy_language, :english, default: false) + let!(:klingon) do + create(:alchemy_language, :klingon, default: false) end let!(:start_page) do create :alchemy_page, :language_root, - language: english, + language: klingon, name: "Start Page" end @@ -153,12 +153,12 @@ module Alchemy end it "loads the root page of that language" do - get :index, params: { locale: "en" } + get :index, params: { locale: "kl" } expect(assigns(:page)).to eq(start_page) end it "sets @root_page to root page of that language" do - get :index, params: { locale: "en" } + get :index, params: { locale: "kl" } expect(assigns(:root_page)).to eq(start_page) end end diff --git a/spec/features/admin/languages_features_spec.rb b/spec/features/admin/languages_features_spec.rb index 16a01c1636..acc051d9d2 100644 --- a/spec/features/admin/languages_features_spec.rb +++ b/spec/features/admin/languages_features_spec.rb @@ -51,7 +51,7 @@ end describe "editing an language" do - let!(:language) { create(:alchemy_language) } + let!(:language) { create(:alchemy_language, :german) } context "when selected locale has multiple matching locale files" do before do diff --git a/spec/helpers/alchemy/pages_helper_spec.rb b/spec/helpers/alchemy/pages_helper_spec.rb index b2ded236e7..3d93d5f8e9 100644 --- a/spec/helpers/alchemy/pages_helper_spec.rb +++ b/spec/helpers/alchemy/pages_helper_spec.rb @@ -207,13 +207,13 @@ module Alchemy context "with options[:reverse]" do context "set to false" do it "should render the language links in an ascending order" do - expect(helper.language_links(reverse: false)).to have_selector("a.de + a.kl") + expect(helper.language_links(reverse: false)).to have_selector("a.kl + a.en") end end context "set to true" do it "should render the language links in a descending order" do - expect(helper.language_links(reverse: true)).to have_selector("a.kl + a.de") + expect(helper.language_links(reverse: true)).to have_selector("a.en + a.kl") end end end diff --git a/spec/libraries/configuration_methods_spec.rb b/spec/libraries/configuration_methods_spec.rb index 5d01745be4..c2b88e2e50 100644 --- a/spec/libraries/configuration_methods_spec.rb +++ b/spec/libraries/configuration_methods_spec.rb @@ -25,7 +25,7 @@ class SomeController < ActionController::Base end context "if more than one language is present" do - let!(:german) { create(:alchemy_language) } + let!(:german) { create(:alchemy_language, :german, default: true) } let!(:english) { create(:alchemy_language, :english) } subject { controller.prefix_locale?(args) } diff --git a/spec/models/alchemy/language_spec.rb b/spec/models/alchemy/language_spec.rb index ef05b25e68..40c2d8b945 100644 --- a/spec/models/alchemy/language_spec.rb +++ b/spec/models/alchemy/language_spec.rb @@ -280,7 +280,7 @@ module Alchemy end before do - expect(::I18n).to receive(:available_locales) do + expect(::I18n).to receive(:available_locales).twice do [:de, :'de-at', :'en-uk'] end end diff --git a/spec/models/alchemy/page_spec.rb b/spec/models/alchemy/page_spec.rb index bc8135ad47..4bae635e31 100644 --- a/spec/models/alchemy/page_spec.rb +++ b/spec/models/alchemy/page_spec.rb @@ -4,7 +4,7 @@ module Alchemy describe Page do - let(:language) { create(:alchemy_language, :english, default: true) } + let(:language) { create(:alchemy_language, :german, default: true) } let(:klingon) { create(:alchemy_language, :klingon) } let(:language_root) { create(:alchemy_page, :language_root) } let(:page) { mock_model(Page, page_layout: "foo") } diff --git a/spec/models/alchemy/site_spec.rb b/spec/models/alchemy/site_spec.rb index 994d426a80..3e3e29dfbc 100644 --- a/spec/models/alchemy/site_spec.rb +++ b/spec/models/alchemy/site_spec.rb @@ -180,7 +180,7 @@ module Alchemy end let!(:other_language) do - create(:alchemy_language, :english, default: false, site: site) + create(:alchemy_language, :german, default: false, site: site) end subject(:site_default_language) do From 9565b37c309f0bba1b26694041f78436c4a30eeb Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Tue, 23 Jun 2020 11:53:00 +0200 Subject: [PATCH 2/2] Reload before checking for related EssenceNodes The most recent version of AwesomeNestedSet stops reloading `self` when saving. This leads to `self` having inaccurate values for `lft` and `rgt` in our spec, making `node.self_and_descendants` sometimes not find all children. Luckily our specs found this. --- spec/models/alchemy/node_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/models/alchemy/node_spec.rb b/spec/models/alchemy/node_spec.rb index 3042e50d02..c74bc6aeb0 100644 --- a/spec/models/alchemy/node_spec.rb +++ b/spec/models/alchemy/node_spec.rb @@ -143,7 +143,7 @@ module Alchemy let!(:parent_node) { create(:alchemy_node, children: [node]) } it "does not destroy the node and children either but adds an error" do - parent_node.destroy + parent_node.reload.destroy expect(parent_node).not_to be_destroyed expect(parent_node.errors.full_messages).to eq(["This menu item is in use inside an Alchemy element on the following pages: #{page.name}."]) end