Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Add more model and API specs for translations (#12043)

Łukasz Adamczyk8ae3d7f3c721
diff --git a/api/spec/requests/spree/api/v2/storefront/products_spec.rb b/api/spec/requests/spree/api/v2/storefront/products_spec.rb
index 6e3e2b9548..ba6b44e3e6 100644
--- a/api/spec/requests/spree/api/v2/storefront/products_spec.rb
+++ b/api/spec/requests/spree/api/v2/storefront/products_spec.rb
@@ -321,6 +321,60 @@ describe 'API V2 Storefront Products Spec', type: :request do
expect(json_response['data'].first).to have_id(product_with_property.id.to_s)
end
end
+
+ context 'with locale set to polish' do
+ let!(:store) do
+ default_store = Spree::Store.default
+ default_store.default_locale = 'en'
+ default_store.supported_locales = 'en,pl'
+ default_store.save
+ default_store
+ end
+
+ let!(:product_localized) do
+ product = create(:product, stores: [store])
+ Mobility.with_locale(:pl) { product.update!(name: 'Produkt Superowy') }
+
+ product
+ end
+
+ let!(:property_localized) do
+ property = create(:property)
+ presentation = option_type.presentation
+
+ Mobility.with_locale(:pl) { property.update!(presentation: "#{presentation} PL") }
+
+ property
+ end
+
+ let!(:product_property_localized) do
+ product_property = create(:product_property, property: property_localized, product: product_localized)
+ value = product_property.value
+
+ Mobility.with_locale(:pl) { product_property.update!(value: "#{value} PL") }
+
+ product_property
+ end
+
+ before do
+ get "/api/v2/storefront/products?filter[properties][#{property_localized.name}]=#{product_property_localized.value_pl}&include=product_properties&locale=pl"
+ end
+
+ it 'returns products with specified properties in polish' do
+ expect(json_response['data'].first).to have_id(product_localized.id.to_s)
+ expect(json_response['data'].first).to have_attribute(:name).with_value('Produkt Superowy')
+ expect(json_response['data'].first).to have_attribute(:localized_slugs).with_value(
+ 'en' => product_localized.slug_en,
+ 'pl' => product_localized.slug_pl
+ )
+
+ expect(json_response['included']).to include(have_type('product_property').and(have_attribute(:name).with_value(property_localized.name)))
+ expect(json_response['included']).to include(have_type('product_property').and(have_attribute(:description).with_value(property_localized.presentation_pl)))
+
+ expect(json_response['included']).to include(have_type('product_property').and(have_attribute(:value).with_value(product_property_localized.value_pl)))
+ expect(json_response['included']).to include(have_type('product_property').and(have_attribute(:filter_param).with_value(product_property_localized.filter_param)))
+ end
+ end
end
context 'with specified multiple filters' do
diff --git a/api/spec/requests/spree/api/v2/storefront/stores_spec.rb b/api/spec/requests/spree/api/v2/storefront/stores_spec.rb
index 8d6e4ad412..bc6653cad7 100644
--- a/api/spec/requests/spree/api/v2/storefront/stores_spec.rb
+++ b/api/spec/requests/spree/api/v2/storefront/stores_spec.rb
@@ -44,6 +44,38 @@ describe 'Storefront API v2 Stores spec', type: :request do
end
end
+ context 'with locale set to pl' do
+ let!(:store) do
+ localized_store = create(:store, default_country: create(:country))
+ localized_store.supported_locales = 'en,pl'
+ localized_store.save
+
+ Mobility.with_locale(:pl) do
+ localized_store.update(
+ name: 'Test Store PL',
+ meta_description: 'Meta Desc PL',
+ meta_keywords: 'meta, keywords, pl',
+ seo_title: 'SEO Title PL'
+ )
+ end
+
+ localized_store
+ end
+
+ before do
+ Spree::Store.default.update!(supported_locales: 'en,pl')
+
+ get "/api/v2/storefront/stores/#{store.code}?locale=pl"
+ end
+
+ it 'return store with translated attributes' do
+ expect(json_response['data']).to have_attribute(:name).with_value('Test Store PL')
+ expect(json_response['data']).to have_attribute(:meta_description).with_value('Meta Desc PL')
+ expect(json_response['data']).to have_attribute(:meta_keywords).with_value('meta, keywords, pl')
+ expect(json_response['data']).to have_attribute(:seo_title).with_value('SEO Title PL')
+ end
+ end
+
context 'with invalid code param' do
before { get '/api/v2/storefront/stores/XX' }
diff --git a/api/spec/requests/spree/api/v2/storefront/taxons_spec.rb b/api/spec/requests/spree/api/v2/storefront/taxons_spec.rb
index 5b299f5982..4c8fb5c4bd 100644
--- a/api/spec/requests/spree/api/v2/storefront/taxons_spec.rb
+++ b/api/spec/requests/spree/api/v2/storefront/taxons_spec.rb
@@ -44,6 +44,59 @@ describe 'Taxons Spec', type: :request do
end
end
+ context 'with locale set to pl' do
+ let!(:default_store) do
+ store = taxonomy.store
+ store.supported_locales = 'en,pl'
+ store.save
+ store
+ end
+
+ let!(:taxonomy) do
+ taxonomy_localized = create(:taxonomy)
+ taxonomy_root_localized = taxonomy_localized.root
+
+ name = taxonomy_localized.name
+
+ Mobility.with_locale(:pl) do
+ taxonomy_localized.update!(name: "Localized Taxonomy PL")
+ taxonomy_root_localized.update!(name: "Localized Taxonomy PL")
+ end
+
+ taxonomy_localized
+ end
+
+ let!(:taxons) do
+ taxons_localized = create_list(:taxon, 2, taxonomy: taxonomy, parent: taxonomy.root)
+ taxons_localized.each_with_index do |taxon, index|
+ name = taxon.name
+ Mobility.with_locale(:pl) { taxon.update!(name: "Localized Taxon #{index + 1} PL") }
+ end
+
+ taxons_localized
+ end
+
+ before { get '/api/v2/storefront/taxons?locale=pl' }
+
+ it_behaves_like 'returns 200 HTTP status'
+
+ it 'returns all taxons' do
+ expect(json_response['data'].size).to eq(3)
+
+ expect(json_response['data']).to include(have_type('taxon').and(have_attribute(:name).with_value("Localized Taxonomy PL")))
+ expect(json_response['data']).to include(have_type('taxon').and(have_attribute(:pretty_name).with_value("Localized Taxonomy PL")))
+ expect(json_response['data']).to include(have_type('taxon').and(have_attribute(:permalink).with_value('localized-taxonomy-pl')))
+
+ expect(json_response['data']).to include(have_type('taxon').and(have_attribute(:name).with_value("Localized Taxon 1 PL")))
+ expect(json_response['data']).to include(have_type('taxon').and(have_attribute(:pretty_name).with_value("Localized Taxonomy PL -> Localized Taxon 1 PL")))
+ expect(json_response['data']).to include(have_type('taxon').and(have_attribute(:permalink).with_value('localized-taxonomy-pl/localized-taxon-1-pl')))
+
+ expect(json_response['data']).to include(have_type('taxon').and(have_attribute(:name).with_value("Localized Taxon 2 PL")))
+ expect(json_response['data']).to include(have_type('taxon').and(have_attribute(:pretty_name).with_value("Localized Taxonomy PL -> Localized Taxon 2 PL")))
+ expect(json_response['data']).to include(have_type('taxon').and(have_attribute(:permalink).with_value('localized-taxonomy-pl/localized-taxon-2-pl')))
+ end
+ end
+
context 'by roots' do
before { get '/api/v2/storefront/taxons?filter[roots]=true' }
diff --git a/core/spec/models/spree/option_type_spec.rb b/core/spec/models/spree/option_type_spec.rb
index e9cba06e9e..20123738fb 100644
--- a/core/spec/models/spree/option_type_spec.rb
+++ b/core/spec/models/spree/option_type_spec.rb
@@ -3,6 +3,25 @@ require 'spec_helper'
describe Spree::OptionType, type: :model do
it_behaves_like 'metadata'
+ describe 'translations' do
+ let!(:option_type) { create(:option_type, name: 'size', presentation: 'Size') }
+
+ before do
+ Mobility.with_locale(:pl) do
+ option_type.update!(presentation: 'Rozmiar')
+ end
+ end
+
+ let(:option_type_pl_translation) { option_type.translations.find_by(locale: 'pl') }
+
+ it 'translates option type fields' do
+ expect(option_type.presentation).to eq('Size')
+
+ expect(option_type_pl_translation).to be_present
+ expect(option_type_pl_translation.presentation).to eq('Rozmiar')
+ end
+ end
+
context 'touching' do
it 'touches a product' do
product_option_type = create(:product_option_type)
diff --git a/core/spec/models/spree/option_value_spec.rb b/core/spec/models/spree/option_value_spec.rb
index 47f084027b..31aa53ca36 100644
--- a/core/spec/models/spree/option_value_spec.rb
+++ b/core/spec/models/spree/option_value_spec.rb
@@ -3,6 +3,25 @@ require 'spec_helper'
describe Spree::OptionValue, type: :model do
it_behaves_like 'metadata'
+ describe 'translations' do
+ let!(:option_value) { create(:option_value, name: 'red', presentation: 'Red') }
+
+ before do
+ Mobility.with_locale(:pl) do
+ option_value.update!(presentation: 'Czerwony')
+ end
+ end
+
+ let(:option_value_pl_translation) { option_value.translations.find_by(locale: 'pl') }
+
+ it 'translates option value fields' do
+ expect(option_value.presentation).to eq('Red')
+
+ expect(option_value_pl_translation).to be_present
+ expect(option_value_pl_translation.presentation).to eq('Czerwony')
+ end
+ end
+
context 'touching' do
it 'touches a variant' do
variant = create(:variant)
diff --git a/core/spec/models/spree/product_property_spec.rb b/core/spec/models/spree/product_property_spec.rb
index 5fc468ac64..13e88d563b 100644
--- a/core/spec/models/spree/product_property_spec.rb
+++ b/core/spec/models/spree/product_property_spec.rb
@@ -21,6 +21,25 @@ describe Spree::ProductProperty, type: :model do
end
end
+ describe 'translations' do
+ let!(:product_property) { create(:product_property, value: 'EN value') }
+
+ before do
+ Mobility.with_locale(:pl) do
+ product_property.update!(value: 'PL value')
+ end
+ end
+
+ let(:product_property_pl_translation) { product_property.translations.find_by(locale: 'pl') }
+
+ it 'translates product property fields' do
+ expect(product_property.value).to eq('EN value')
+
+ expect(product_property_pl_translation).to be_present
+ expect(product_property_pl_translation.value).to eq('PL value')
+ end
+ end
+
context 'touching' do
let(:product_property) { create(:product_property) }
diff --git a/core/spec/models/spree/product_spec.rb b/core/spec/models/spree/product_spec.rb
index 9e731bafcb..f405626563 100644
--- a/core/spec/models/spree/product_spec.rb
+++ b/core/spec/models/spree/product_spec.rb
@@ -391,6 +391,36 @@ describe Spree::Product, type: :model do
expect(product.reload.total_on_hand).to eq corrent_total_on_hand
end
end
+
+ context 'when using another locale' do
+ before do
+ product.update!(name: 'EN name')
+
+ Mobility.with_locale(:pl) do
+ product.update!(
+ name: 'PL name',
+ description: 'PL description',
+ meta_title: 'PL meta title',
+ meta_description: 'PL meta description',
+ meta_keywords: 'PL meta keywords'
+ )
+ end
+ end
+
+ let(:product_pl_translation) { product.translations.find_by(locale: 'pl') }
+
+ it 'translates product fields' do
+ expect(product.name).to eq('EN name')
+
+ expect(product_pl_translation).to be_present
+ expect(product_pl_translation.name).to eq('PL name')
+ expect(product_pl_translation.slug).to eq('pl-name')
+ expect(product_pl_translation.description).to eq('PL description')
+ expect(product_pl_translation.meta_title).to eq('PL meta title')
+ expect(product_pl_translation.meta_description).to eq('PL meta description')
+ expect(product_pl_translation.meta_keywords).to eq('PL meta keywords')
+ end
+ end
end
context 'properties' do
diff --git a/core/spec/models/spree/property_spec.rb b/core/spec/models/spree/property_spec.rb
index 7edffe1509..9fe275ede9 100644
--- a/core/spec/models/spree/property_spec.rb
+++ b/core/spec/models/spree/property_spec.rb
@@ -5,6 +5,25 @@ describe Spree::Property, type: :model do
it_behaves_like 'metadata'
+ describe 'translations' do
+ let!(:property) { create(:property, name: 'brand', presentation: 'Brand') }
+
+ before do
+ Mobility.with_locale(:pl) do
+ property.update!(presentation: 'Marka')
+ end
+ end
+
+ let(:property_pl_translation) { property.translations.find_by(locale: 'pl') }
+
+ it 'translates property fields' do
+ expect(property.presentation).to eq('Brand')
+
+ expect(property_pl_translation).to be_present
+ expect(property_pl_translation.presentation).to eq('Marka')
+ end
+ end
+
context 'setting filter param' do
subject { build(:property, name: 'Brand Name') }
diff --git a/core/spec/models/spree/store_spec.rb b/core/spec/models/spree/store_spec.rb
index 0e885f959f..e68ed1040e 100644
--- a/core/spec/models/spree/store_spec.rb
+++ b/core/spec/models/spree/store_spec.rb
@@ -160,6 +160,29 @@ describe Spree::Store, type: :model do
end
end
+ describe 'translations' do
+ let!(:store) { create(:store, name: 'Store EN') }
+
+ before do
+ Mobility.with_locale(:pl) do
+ store.update!(
+ name: 'Store PL',
+ description: 'PL description'
+ )
+ end
+ end
+
+ let(:store_pl_translation) { store.translations.find_by(locale: 'pl') }
+
+ it 'translates store fields' do
+ expect(store.name).to eq('Store EN')
+
+ expect(store_pl_translation).to be_present
+ expect(store_pl_translation.name).to eq('Store PL')
+ expect(store_pl_translation.description).to eq('PL description')
+ end
+ end
+
describe '.by_url' do
let!(:store) { create(:store, url: "website1.com\nwww.subdomain.com") }
let!(:store_2) { create(:store, url: 'freethewhales.com') }
diff --git a/core/spec/models/spree/taxon_spec.rb b/core/spec/models/spree/taxon_spec.rb
index d97164f66a..a69e3df440 100644
--- a/core/spec/models/spree/taxon_spec.rb
+++ b/core/spec/models/spree/taxon_spec.rb
@@ -39,6 +39,33 @@ describe Spree::Taxon, type: :model do
end
end
+ context 'when using another locale' do
+ before do
+ root_taxon = taxon.taxonomy.root
+ taxon.update!(name: 'EN name', parent: taxon.taxonomy.root)
+
+ Mobility.with_locale(:pl) do
+ root_taxon.update!(name: 'PL taxonomy')
+
+ taxon.update!(
+ name: 'PL name',
+ description: 'PL description'
+ )
+ end
+ end
+
+ let(:taxon_pl_translation) { taxon.translations.find_by(locale: 'pl') }
+
+ it 'translates taxon fields' do
+ expect(taxon.name).to eq('EN name')
+
+ expect(taxon_pl_translation).to be_present
+ expect(taxon_pl_translation.name).to eq('PL name')
+ expect(taxon_pl_translation.permalink).to eq('pl-taxonomy/pl-name')
+ expect(taxon_pl_translation.description).to eq('PL description')
+ end
+ end
+
context 'set_permalink' do
it 'sets permalink correctly when no parent present' do
taxon.set_permalink