2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-10-11 05:41:23 -04:00
|
|
|
require "rails_helper"
|
2014-08-28 15:09:36 -04:00
|
|
|
|
|
|
|
describe Permalink do
|
|
|
|
|
2015-07-15 01:32:35 -04:00
|
|
|
describe "normalization" do
|
|
|
|
it "correctly normalizes" do
|
|
|
|
normalizer = Permalink::Normalizer.new("/(\\/hello.*)\\?.*/\\1|/(\\/bye.*)\\?.*/\\1")
|
|
|
|
|
|
|
|
expect(normalizer.normalize("/hello?a=1")).to eq("/hello")
|
|
|
|
expect(normalizer.normalize("/bye?test=1")).to eq("/bye")
|
|
|
|
expect(normalizer.normalize("/bla?a=1")).to eq("/bla?a=1")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-28 15:09:36 -04:00
|
|
|
describe "new record" do
|
|
|
|
it "strips blanks" do
|
|
|
|
permalink = described_class.create(url: " my/old/url ")
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(permalink.url).to eq("my/old/url")
|
2014-08-28 15:09:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "removes leading slash" do
|
|
|
|
permalink = described_class.create(url: "/my/old/url")
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(permalink.url).to eq("my/old/url")
|
2014-08-28 15:09:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "target_url" do
|
|
|
|
|
|
|
|
let(:permalink) { Fabricate.build(:permalink) }
|
|
|
|
let(:topic) { Fabricate(:topic) }
|
|
|
|
let(:post) { Fabricate(:post, topic: topic) }
|
|
|
|
let(:category) { Fabricate(:category) }
|
2020-05-25 05:48:54 -04:00
|
|
|
let(:tag) { Fabricate(:tag) }
|
2014-08-28 15:09:36 -04:00
|
|
|
subject(:target_url) { permalink.target_url }
|
|
|
|
|
|
|
|
it "returns a topic url when topic_id is set" do
|
|
|
|
permalink.topic_id = topic.id
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(target_url).to eq(topic.relative_url)
|
2014-08-28 15:09:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns nil when topic_id is set but topic is not found" do
|
|
|
|
permalink.topic_id = 99999
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(target_url).to eq(nil)
|
2014-08-28 15:09:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns a post url when post_id is set" do
|
|
|
|
permalink.post_id = post.id
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(target_url).to eq(post.url)
|
2014-08-28 15:09:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns nil when post_id is set but post is not found" do
|
|
|
|
permalink.post_id = 99999
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(target_url).to eq(nil)
|
2014-08-28 15:09:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns a post url when post_id and topic_id are both set" do
|
|
|
|
permalink.post_id = post.id
|
|
|
|
permalink.topic_id = topic.id
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(target_url).to eq(post.url)
|
2014-08-28 15:09:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns a category url when category_id is set" do
|
|
|
|
permalink.category_id = category.id
|
2020-06-18 04:32:14 -04:00
|
|
|
expect(target_url).to eq("#{category.url}")
|
2014-08-28 15:09:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns nil when category_id is set but category is not found" do
|
|
|
|
permalink.category_id = 99999
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(target_url).to eq(nil)
|
2014-08-28 15:09:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns a post url when topic_id, post_id, and category_id are all set for some reason" do
|
|
|
|
permalink.post_id = post.id
|
|
|
|
permalink.topic_id = topic.id
|
|
|
|
permalink.category_id = category.id
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(target_url).to eq(post.url)
|
2014-08-28 15:09:36 -04:00
|
|
|
end
|
|
|
|
|
2020-05-25 05:48:54 -04:00
|
|
|
it "returns a tag url when tag_id is set" do
|
|
|
|
permalink.tag_id = tag.id
|
|
|
|
expect(target_url).to eq(tag.full_url)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns nil when tag_id is set but tag is not found" do
|
|
|
|
permalink.tag_id = 99999
|
|
|
|
expect(target_url).to eq(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns a post url when topic_id, post_id, category_id and tag_id are all set for some reason" do
|
|
|
|
permalink.post_id = post.id
|
|
|
|
permalink.topic_id = topic.id
|
|
|
|
permalink.category_id = category.id
|
|
|
|
permalink.tag_id = tag.id
|
|
|
|
expect(target_url).to eq(post.url)
|
|
|
|
end
|
|
|
|
|
2014-08-28 15:09:36 -04:00
|
|
|
it "returns nil when nothing is set" do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(target_url).to eq(nil)
|
2014-08-28 15:09:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|