From 01509d40ad50b30488929bdea2862d0fffd4abce Mon Sep 17 00:00:00 2001 From: loic Date: Fri, 8 Feb 2019 14:37:09 +0100 Subject: [PATCH] parameterize updated taxon permalink #3086 add test for parameterize taxon permalink when taxon parent #3086 correct grammar on test description test parameterization on children taxon permalink #3086 --- core/app/models/spree/taxon.rb | 5 ++--- core/spec/models/spree/taxon_spec.rb | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/core/app/models/spree/taxon.rb b/core/app/models/spree/taxon.rb index 39fe094ad65..c3156dabefb 100644 --- a/core/app/models/spree/taxon.rb +++ b/core/app/models/spree/taxon.rb @@ -59,9 +59,8 @@ def seo_title # Sets this taxons permalink to a valid url encoded string based on its # name and its parents permalink (if present.) def set_permalink - permalink_tail = permalink.split('/').last if permalink.present? - permalink_tail ||= Spree::Config.taxon_url_parametizer_class.parameterize(name) - self.permalink_part = permalink_tail + permalink_tail = permalink.present? ? permalink.split('/').last : name + self.permalink_part = Spree::Config.taxon_url_parametizer_class.parameterize(permalink_tail) end # Update the permalink for this taxon and all children (if necessary) diff --git a/core/spec/models/spree/taxon_spec.rb b/core/spec/models/spree/taxon_spec.rb index 745f2abe28e..0d4e4f7255a 100644 --- a/core/spec/models/spree/taxon_spec.rb +++ b/core/spec/models/spree/taxon_spec.rb @@ -26,6 +26,14 @@ expect(taxon.permalink).to eql "ruby-on-rails" end + context "updating a taxon permalink" do + it 'parameterizes permalink correctly' do + taxon.save! + taxon.update_attributes(permalink: 'spécial&charactèrs') + expect(taxon.permalink).to eql "special-characters" + end + end + context "with parent taxon" do let(:parent) { FactoryBot.build(:taxon, permalink: "brands") } before { allow(taxon).to receive_messages parent: parent } @@ -41,6 +49,12 @@ expect(taxon.permalink).to eql "brands/rubyonrails" end + it 'parameterizes permalink correctly' do + taxon.save! + taxon.update_attributes(permalink_part: 'spécial&charactèrs') + expect(taxon.reload.permalink).to eql "brands/special-characters" + end + # Regression test for https://github.com/spree/spree/issues/3390 context "setting a new node sibling position via :child_index=" do let(:idx) { rand(0..100) } @@ -121,6 +135,20 @@ is_expected.to change{ taxon2_child.reload.permalink }.from('t/t2/t2_child').to('t/t1/foo/t2_child') end end + + context 'changing parent permalink with special characters ' do + subject do + -> { taxon2.update!(permalink: 'spécial&charactèrs') } + end + + it 'changes own permalink with parameterized characters' do + is_expected.to change{ taxon2.reload.permalink }.from('t/t2').to('t/special-characters') + end + + it 'changes child permalink with parameterized characters' do + is_expected.to change{ taxon2_child.reload.permalink }.from('t/t2/t2_child').to('t/special-characters/t2_child') + end + end end # Regression test for https://github.com/spree/spree/issues/2620