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.

Fix taxon permalink unique constraint violation on rename (#13884)

Kenta Ishizakidd544436ee13
diff --git a/spree/core/app/models/spree/taxon.rb b/spree/core/app/models/spree/taxon.rb
index 287f47b98f..305e581624 100644
--- a/spree/core/app/models/spree/taxon.rb
+++ b/spree/core/app/models/spree/taxon.rb
@@ -72,10 +72,9 @@ module Spree
#
# Callbacks
#
- before_validation :set_permalink, on: :create, if: :name
+ before_validation :set_permalink, if: :name
before_validation :copy_taxonomy_from_parent
before_save :set_pretty_name
- before_save :set_permalink
after_save :touch_ancestors_and_taxonomy
after_update :sync_taxonomy_name
after_touch :touch_ancestors_and_taxonomy
diff --git a/spree/core/spec/models/spree/taxon_spec.rb b/spree/core/spec/models/spree/taxon_spec.rb
index fb1f6d5bbb..cbcfdf72fc 100644
--- a/spree/core/spec/models/spree/taxon_spec.rb
+++ b/spree/core/spec/models/spree/taxon_spec.rb
@@ -253,6 +253,30 @@ describe Spree::Taxon, type: :model do
end
end
+ # Regression test for #13395
+ describe 'permalink uniqueness after normalization' do
+ let!(:taxonomy) { create(:taxonomy, store: store) }
+ let(:parent) { taxonomy.root }
+
+ it 'returns a validation error instead of raising RecordNotUnique when normalized permalink conflicts' do
+ create(:taxon, name: 'Foo Bar', taxonomy: taxonomy, parent: parent)
+
+ sibling = create(:taxon, name: 'Other', taxonomy: taxonomy, parent: parent)
+ sibling.permalink = "#{parent.permalink}/Foo Bar"
+
+ expect { sibling.save }.not_to raise_error
+ expect(sibling.errors[:permalink]).to be_present
+ end
+
+ it 'normalizes permalink before validation on update' do
+ taxon = create(:taxon, name: 'Test', taxonomy: taxonomy, parent: parent)
+ taxon.permalink = "#{parent.permalink}/Héllo Wörld"
+ taxon.valid?
+
+ expect(taxon.permalink).to eq("#{parent.permalink}/hello-world")
+ end
+ end
+
# Regression test for #2620
context 'creating a child node using first_or_create' do
let!(:taxonomy) { create(:taxonomy, store: store) }