mirror of
https://github.com/discourse/discourse.git
synced 2025-06-01 03:45:06 +08:00
FEATURE: add slug geneartion options
This commit is contained in:

committed by
fantasticfears

parent
2344aa2fdd
commit
b772ff6e13
@ -169,22 +169,54 @@ describe Category do
|
||||
end
|
||||
|
||||
describe 'non-english characters' do
|
||||
let(:category) { Fabricate(:category, name: "测试") }
|
||||
context 'uses ascii slug generator' do
|
||||
before do
|
||||
SiteSetting.slug_generation_method = 'ascii'
|
||||
@category = Fabricate(:category, name: "测试")
|
||||
end
|
||||
after { @category.destroy }
|
||||
|
||||
it "creates a blank slug, this is OK." do
|
||||
expect(category.slug).to be_blank
|
||||
expect(category.slug_for_url).to eq("#{category.id}-category")
|
||||
it "creates a blank slug" do
|
||||
expect(@category.slug).to be_blank
|
||||
expect(@category.slug_for_url).to eq("#{@category.id}-category")
|
||||
end
|
||||
end
|
||||
|
||||
it "creates a localized slug if default locale is zh_CN" do
|
||||
SiteSetting.default_locale = 'zh_CN'
|
||||
expect(category.slug).to_not be_blank
|
||||
expect(category.slug_for_url).to eq("ce-shi")
|
||||
context 'uses none slug generator' do
|
||||
before do
|
||||
SiteSetting.slug_generation_method = 'none'
|
||||
@category = Fabricate(:category, name: "测试")
|
||||
end
|
||||
after do
|
||||
SiteSetting.slug_generation_method = 'ascii'
|
||||
@category.destroy
|
||||
end
|
||||
|
||||
it "creates a blank slug" do
|
||||
expect(@category.slug).to be_blank
|
||||
expect(@category.slug_for_url).to eq("#{@category.id}-category")
|
||||
end
|
||||
end
|
||||
|
||||
context 'uses encoded slug generator' do
|
||||
before do
|
||||
SiteSetting.slug_generation_method = 'encoded'
|
||||
@category = Fabricate(:category, name: "测试")
|
||||
end
|
||||
after do
|
||||
SiteSetting.slug_generation_method = 'ascii'
|
||||
@category.destroy
|
||||
end
|
||||
|
||||
it "creates a slug" do
|
||||
expect(@category.slug).to eq("%E6%B5%8B%E8%AF%95")
|
||||
expect(@category.slug_for_url).to eq("%E6%B5%8B%E8%AF%95")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'slug would be a number' do
|
||||
let(:category) { Fabricate(:category, name: "2") }
|
||||
let(:category) { Fabricate.build(:category, name: "2") }
|
||||
|
||||
it 'creates a blank slug' do
|
||||
expect(category.slug).to be_blank
|
||||
@ -193,21 +225,25 @@ describe Category do
|
||||
end
|
||||
|
||||
describe 'custom slug can be provided' do
|
||||
it 'has the custom value' do
|
||||
c = Fabricate(:category, name: "Cats", slug: "cats-category")
|
||||
expect(c.slug).to eq("cats-category")
|
||||
end
|
||||
it 'can be sanitized' do
|
||||
@c = Fabricate(:category, name: "Fun Cats", slug: "fun-cats")
|
||||
@cat = Fabricate(:category, name: "love cats", slug: "love-cats")
|
||||
|
||||
it 'and be sanitized' do
|
||||
c = Fabricate(:category, name: 'Cats', slug: ' invalid slug')
|
||||
expect(c.slug).to eq('invalid-slug')
|
||||
end
|
||||
@c.slug = ' invalid slug'
|
||||
@c.save
|
||||
expect(@c.slug).to eq('invalid-slug')
|
||||
|
||||
it 'fails if custom slug is duplicate with existing' do
|
||||
c1 = Fabricate(:category, name: "Cats", slug: "cats")
|
||||
c2 = Fabricate.build(:category, name: "More Cats", slug: "cats")
|
||||
expect(c2).to_not be_valid
|
||||
expect(c2.errors[:slug]).to be_present
|
||||
c = Fabricate.build(:category, name: "More Fun Cats", slug: "love-cats")
|
||||
expect(c).not_to be_valid
|
||||
expect(c.errors[:slug]).to be_present
|
||||
|
||||
@cat.slug = "#{@c.id}-category"
|
||||
expect(@cat).not_to be_valid
|
||||
expect(@cat.errors[:slug]).to be_present
|
||||
|
||||
@cat.slug = "#{@cat.id}-category"
|
||||
expect(@cat).to be_valid
|
||||
expect(@cat.errors[:slug]).not_to be_present
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -12,28 +12,64 @@ describe Topic do
|
||||
it { is_expected.to rate_limit }
|
||||
|
||||
context 'slug' do
|
||||
|
||||
let(:title) { "hello world topic" }
|
||||
let(:slug) { "hello-world-slug" }
|
||||
let(:slug) { "hello-world-topic" }
|
||||
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)
|
||||
it "returns a Slug for a title" do
|
||||
Slug.expects(:for).with(title, 'topic').returns(slug)
|
||||
expect(Fabricate.build(:topic, title: title).slug).to eq(slug)
|
||||
end
|
||||
|
||||
context 'for cjk characters' do
|
||||
let(:title) { "熱帶風暴畫眉" }
|
||||
let(:slug) { "%E7%86%B1%E5%B8%B6%E9%A2%A8%E6%9A%B4%E7%95%AB%E7%9C%89" }
|
||||
it "returns encoded Slug for a title" do
|
||||
Slug.expects(:for).with(title, 'topic').returns(slug)
|
||||
expect(Fabricate.build(:topic, title: title).slug).to eq(slug)
|
||||
end
|
||||
end
|
||||
|
||||
context 'for numbers' do
|
||||
let(:title) { "123456789" }
|
||||
let(:slug) { "topic" }
|
||||
it 'generates default slug' do
|
||||
Slug.expects(:for).with(title, 'topic').returns("topic")
|
||||
expect(Fabricate.build(:topic, title: title).slug).to eq("topic")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
let(:chinese_title) { "习近平:中企承建港口电站等助斯里兰卡发展" }
|
||||
let(:chinese_slug) { "xi-jin-ping-zhong-qi-cheng-jian-gang-kou-dian-zhan-deng-zhu-si-li-lan-qia-fa-zhan" }
|
||||
context 'none generator' do
|
||||
before { SiteSetting.slug_generation_method = 'none' }
|
||||
after { SiteSetting.slug_generation_method = 'ascii' }
|
||||
let(:title) { "熱帶風暴畫眉" }
|
||||
let(:slug) { "topic" }
|
||||
|
||||
it "returns a symbolized slug for a chinese title" do
|
||||
SiteSetting.default_locale = 'zh_CN'
|
||||
expect(Fabricate.build(:topic, title: chinese_title).slug).to eq(chinese_slug)
|
||||
it "returns a Slug for a title" do
|
||||
Slug.expects(:for).with(title, 'topic').returns('topic')
|
||||
expect(Fabricate.build(:topic, title: title).slug).to eq(slug)
|
||||
end
|
||||
end
|
||||
|
||||
it "returns 'topic' when the slug is empty (say, non-english chars)" do
|
||||
Slug.expects(:for).with(title).returns("")
|
||||
expect(Fabricate.build(:topic, title: title).slug).to eq("topic")
|
||||
end
|
||||
context '#ascii_generator' do
|
||||
before { SiteSetting.slug_generation_method = 'ascii' }
|
||||
it "returns a Slug for a title" do
|
||||
Slug.expects(:for).with(title, 'topic').returns(slug)
|
||||
expect(Fabricate.build(:topic, title: title).slug).to eq(slug)
|
||||
end
|
||||
|
||||
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, 'topic').returns("topic")
|
||||
expect(Fabricate.build(:topic, title: title).slug).to eq("topic")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "updating a title to be shorter" do
|
||||
|
Reference in New Issue
Block a user