mirror of
https://github.com/discourse/discourse.git
synced 2024-11-25 09:42:07 +08:00
FIX: Store special characters in permalink URL as encoded characters
see https://meta.discourse.org/t/permalink-not-working-with-cyrilics-symbols/301130
This commit is contained in:
parent
9061282515
commit
602ef2c819
|
@ -7,7 +7,7 @@ class Permalink < ActiveRecord::Base
|
|||
belongs_to :tag
|
||||
belongs_to :user
|
||||
|
||||
before_validation :normalize_url
|
||||
before_validation :normalize_url, :encode_url
|
||||
|
||||
validates :url, uniqueness: true
|
||||
|
||||
|
@ -66,10 +66,6 @@ class Permalink < ActiveRecord::Base
|
|||
find_by(url: normalize_url(url))
|
||||
end
|
||||
|
||||
def normalize_url
|
||||
self.url = Permalink.normalize_url(url) if url
|
||||
end
|
||||
|
||||
def target_url
|
||||
return relative_external_url if external_url
|
||||
return post.relative_url if post
|
||||
|
@ -80,10 +76,6 @@ class Permalink < ActiveRecord::Base
|
|||
nil
|
||||
end
|
||||
|
||||
def relative_external_url
|
||||
external_url.match?(%r{\A/[^/]}) ? "#{Discourse.base_path}#{external_url}" : external_url
|
||||
end
|
||||
|
||||
def self.filter_by(url = nil)
|
||||
permalinks =
|
||||
Permalink.includes(:topic, :post, :category, :tag, :user).order("permalinks.created_at desc")
|
||||
|
@ -92,6 +84,20 @@ class Permalink < ActiveRecord::Base
|
|||
permalinks.limit!(100)
|
||||
permalinks.to_a
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def normalize_url
|
||||
self.url = Permalink.normalize_url(url) if url
|
||||
end
|
||||
|
||||
def encode_url
|
||||
self.url = UrlHelper.encode(url) if url
|
||||
end
|
||||
|
||||
def relative_external_url
|
||||
external_url.match?(%r{\A/[^/]}) ? "#{Discourse.base_path}#{external_url}" : external_url
|
||||
end
|
||||
end
|
||||
|
||||
# == Schema Information
|
||||
|
|
|
@ -13,14 +13,40 @@ RSpec.describe Permalink do
|
|||
|
||||
describe "new record" do
|
||||
it "strips blanks" do
|
||||
permalink = described_class.create(url: " my/old/url ")
|
||||
permalink = described_class.create!(url: " my/old/url ")
|
||||
expect(permalink.url).to eq("my/old/url")
|
||||
end
|
||||
|
||||
it "removes leading slash" do
|
||||
permalink = described_class.create(url: "/my/old/url")
|
||||
permalink = described_class.create!(url: "/my/old/url")
|
||||
expect(permalink.url).to eq("my/old/url")
|
||||
end
|
||||
|
||||
it "checks for unique URL" do
|
||||
permalink = described_class.create(url: "/my/old/url")
|
||||
expect(permalink.errors[:url]).to be_empty
|
||||
|
||||
permalink = described_class.create(url: "/my/old/url")
|
||||
expect(permalink.errors[:url]).to be_present
|
||||
|
||||
permalink = described_class.create(url: "my/old/url")
|
||||
expect(permalink.errors[:url]).to be_present
|
||||
end
|
||||
|
||||
context "with special characters in URL" do
|
||||
it "percent encodes any special character" do
|
||||
permalink = described_class.create!(url: "/2022/10/03/привет-sam")
|
||||
expect(permalink.url).to eq("2022/10/03/%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82-sam")
|
||||
end
|
||||
|
||||
it "checks for unique URL" do
|
||||
permalink = described_class.create(url: "/2022/10/03/привет-sam")
|
||||
expect(permalink.errors[:url]).to be_empty
|
||||
|
||||
permalink = described_class.create(url: "/2022/10/03/привет-sam")
|
||||
expect(permalink.errors[:url]).to be_present
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "target_url" do
|
||||
|
|
Loading…
Reference in New Issue
Block a user