From 401fbdbfe7214eb74988c2b73efba43b556231a7 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Fri, 27 Oct 2017 11:02:12 +0800 Subject: [PATCH] FIX: `Topic#title` were being parameterized when encoded slug is enabled. --- lib/slug.rb | 21 +++++++++++---------- spec/models/topic_spec.rb | 20 +++++++++++++------- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/lib/slug.rb b/lib/slug.rb index 725cf1633cd..dc95095b7b1 100644 --- a/lib/slug.rb +++ b/lib/slug.rb @@ -25,11 +25,12 @@ module Slug private - def self.prettify_slug(slug, max_length: MAX_LENGTH) - slug.tr!("_", "-") - slug = slug.squeeze('-') # squeeze continuous dashes to prettify slug - slug.gsub!(/\A-+|-+\z/, '') # remove possible trailing and preceding dashes - slug.truncate(max_length, omission: '') + def self.prettify_slug(slug, max_length:) + slug + .tr("_", "-") + .truncate(max_length, omission: '') + .squeeze('-') # squeeze continuous dashes to prettify slug + .gsub(/\A-+|-+\z/, '') # remove possible trailing and preceding dashes end def self.ascii_generator(string) @@ -40,11 +41,11 @@ module Slug # This generator will sanitize almost all special characters, # including reserved characters from RFC3986. # See also URI::REGEXP::PATTERN. - string.strip! - string.gsub!(/\s+/, '-') - string.gsub!(CHAR_FILTER_REGEXP, '') - string.downcase! if downcase - string + string = string.strip + .gsub(/\s+/, '-') + .gsub(CHAR_FILTER_REGEXP, '') + + downcase ? string.downcase : string end def self.none_generator(string) diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rb index 1cea61ad4b0..23021afbb68 100644 --- a/spec/models/topic_spec.rb +++ b/spec/models/topic_spec.rb @@ -123,21 +123,25 @@ describe Topic do context 'slug' do let(:title) { "hello world topic" } let(:slug) { "hello-world-topic" } + let!(:expected_title) { title.dup } + let!(:expected_slug) { slug.dup } + let(:topic) { Fabricate.build(:topic, title: title) } + context 'encoded generator' do before { SiteSetting.slug_generation_method = 'encoded' } - after { SiteSetting.slug_generation_method = 'ascii' } it "returns a Slug for a title" do - Slug.expects(:for).with(title).returns(slug) - expect(Fabricate.build(:topic, title: title).slug).to eq(slug) + expect(topic.title).to eq(expected_title) + expect(topic.slug).to eq(expected_slug) end context 'for cjk characters' do let(:title) { "熱帶風暴畫眉" } - let(:slug) { "熱帶風暴畫眉" } + let!(:expected_title) { title.dup } + it "returns encoded Slug for a title" do - Slug.expects(:for).with(title).returns(slug) - expect(Fabricate.build(:topic, title: title).slug).to eq(slug) + expect(topic.title).to eq(expected_title) + expect(topic.slug).to eq(expected_title) end end @@ -153,7 +157,7 @@ describe Topic do context 'none generator' do before { SiteSetting.slug_generation_method = 'none' } - after { SiteSetting.slug_generation_method = 'ascii' } + let(:title) { "熱帶風暴畫眉" } let(:slug) { "topic" } @@ -165,6 +169,7 @@ describe Topic do context '#ascii_generator' do before { SiteSetting.slug_generation_method = 'ascii' } + it "returns a Slug for a title" do Slug.expects(:for).with(title).returns(slug) expect(Fabricate.build(:topic, title: title).slug).to eq(slug) @@ -173,6 +178,7 @@ describe Topic do context 'for cjk characters' do let(:title) { "熱帶風暴畫眉" } let(:slug) { 'topic' } + it "returns 'topic' when the slug is empty (say, non-latin characters)" do Slug.expects(:for).with(title).returns("topic") expect(Fabricate.build(:topic, title: title).slug).to eq("topic")