0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-06 13:08:40 +08:00
discourse/spec/lib/canonical_url_spec.rb
Natalie Tay 16919c4fd4
UX: Prevent incomplete crawler localization settings (#41297)
This PR makes sure crawler localization URLs only work when anonymous
users can resolve the locale from the URL param. This blocks enabling
the crawler param without that prerequisite and keeps hreflang,
canonical URLs, and crawler link rewriting behind the same guard.

/406195/4
2026-07-01 12:13:22 +08:00

89 lines
3.1 KiB
Ruby
Vendored

# frozen_string_literal: true
describe CanonicalURL::ControllerExtensions do
let(:host_class) do
Class.new do
def self.helper_method(*)
end
include CanonicalURL::ControllerExtensions
attr_accessor :params, :use_crawler_layout
def initialize(params = {})
@params = params
@use_crawler_layout = true
end
def use_crawler_layout?
@use_crawler_layout
end
end
end
describe "#append_content_localization_param" do
let(:instance) { host_class.new(Discourse::LOCALE_PARAM => "ja") }
before do
SiteSetting.allow_user_locale = true
SiteSetting.set_locale_from_param = true
SiteSetting.content_localization_enabled = true
SiteSetting.content_localization_crawler_param = true
SiteSetting.content_localization_supported_locales = "en|ja|es"
end
it "returns the url unchanged when content_localization_enabled is false" do
SiteSetting.content_localization_enabled = false
expect(instance.send(:append_content_localization_param, "/latest")).to eq("/latest")
end
it "returns the url unchanged when content_localization_crawler_param is false" do
SiteSetting.content_localization_crawler_param = false
expect(instance.send(:append_content_localization_param, "/latest")).to eq("/latest")
end
it "returns the url unchanged when set_locale_from_param is false" do
SiteSetting.set_locale_from_param = false
expect(instance.send(:append_content_localization_param, "/latest")).to eq("/latest")
end
it "returns the url unchanged for non-crawler requests" do
instance.use_crawler_layout = false
expect(instance.send(:append_content_localization_param, "/latest")).to eq("/latest")
end
it "returns the url unchanged when the locale param is absent" do
instance.params = {}
expect(instance.send(:append_content_localization_param, "/latest")).to eq("/latest")
end
it "returns the url unchanged when the locale is not in the supported list" do
instance.params = { Discourse::LOCALE_PARAM => "xyz" }
expect(instance.send(:append_content_localization_param, "/latest")).to eq("/latest")
end
it "appends the locale param to a url without query string" do
expect(instance.send(:append_content_localization_param, "/latest")).to eq(
"/latest?#{Discourse::LOCALE_PARAM}=ja",
)
end
it "appends the locale param to a url with an existing query string" do
expect(instance.send(:append_content_localization_param, "/latest?page=2")).to eq(
"/latest?page=2&#{Discourse::LOCALE_PARAM}=ja",
)
end
it "replaces an existing locale param instead of duplicating it" do
result =
instance.send(:append_content_localization_param, "/latest?#{Discourse::LOCALE_PARAM}=en")
expect(result).to eq("/latest?#{Discourse::LOCALE_PARAM}=ja")
end
it "preserves url fragments" do
expect(instance.send(:append_content_localization_param, "/t/slug/1#post_5")).to eq(
"/t/slug/1?#{Discourse::LOCALE_PARAM}=ja#post_5",
)
end
end
end