discourse/spec/system/content_localization_language_switcher_spec.rb
Natalie Tay 736f23173e
FIX: User options take precedence over show-original anon cookie (#39520)
Repro:
  1. anonymous user browsing
  2. sets content language switcher to "Japanese"
  3. sees japanese
  4. clicks on "show original" in the post
  5. sees original
  6. logs in to their account
7. (their account should see Japanese - UNCHECKED user pref "Show
original content instead of translated content.")
  8. BUG: but they see original because of the anon cookie
  
This PR fixes the bug by only using the user option if they're logged
in.
2026-04-25 00:44:20 +08:00

196 lines
6.5 KiB
Ruby

# frozen_string_literal: true
describe "Content localization language switcher" do
let(:switcher_selector) { "button[data-identifier='language-switcher']" }
let(:toggle_localize_button_selector) { "button.btn-toggle-localized-content" }
let(:topic_list) { PageObjects::Components::TopicList.new }
let(:switcher) { PageObjects::Components::DMenu.new(switcher_selector) }
let(:language_switcher) { PageObjects::Components::LanguageSwitcher.new }
fab!(:japanese_user) { Fabricate(:user, locale: "ja") }
fab!(:topic) { Fabricate(:topic, title: "Life strategies from The Art of War", locale: "en") }
fab!(:post_1) do
Fabricate(
:post,
topic:,
locale: "en",
raw: "The masterpiece isn't just about military strategy",
)
end
fab!(:post_2) do
Fabricate(
:post,
topic:,
locale: "en",
raw: "The greatest victory is that which requires no battle",
)
end
before do
SiteSetting.default_locale = "en"
SiteSetting.content_localization_supported_locales = "es|ja"
SiteSetting.content_localization_enabled = true
SiteSetting.allow_user_locale = true
SiteSetting.set_locale_from_cookie = true
Fabricate(:topic_localization, topic:, locale: "ja", fancy_title: "孫子兵法からの人生戦略")
Fabricate(
:topic_localization,
topic:,
locale: "es",
fancy_title: "Estrategias de vida de El arte de la guerra",
)
Fabricate(:post_localization, post: post_1, locale: "ja", cooked: "傑作は単なる軍事戦略についてではありません")
Fabricate(
:post_localization,
post: post_1,
locale: "es",
cooked: "La obra maestra no se trata solo de estrategia militar",
)
end
it "only shows the language switcher based on enabled state and what is in target languages" do
SiteSetting.content_localization_enabled = false
SiteSetting.content_localization_language_switcher = "anonymous"
visit("/")
expect(page).to have_no_css(switcher_selector)
SiteSetting.content_localization_enabled = true
page.refresh
switcher.expand
expect(switcher).to have_content("English")
expect(switcher).to have_content("Japanese (日本語)")
expect(switcher).to have_content("Spanish (Español)")
SiteSetting.content_localization_supported_locales = "ja"
visit("/")
switcher.expand
expect(switcher).not_to have_content("Español")
end
it "only shows the language switcher if turned on for various types of users (anon, logged in)" do
SiteSetting.content_localization_language_switcher = "none"
visit("/")
expect(page).not_to have_css(switcher_selector)
SiteSetting.content_localization_language_switcher = "anonymous"
visit("/")
expect(page).to have_css(switcher_selector)
SiteSetting.content_localization_language_switcher = "all"
visit("/")
expect(page).to have_css(switcher_selector)
sign_in(japanese_user)
SiteSetting.content_localization_language_switcher = "none"
visit("/")
expect(page).not_to have_css(switcher_selector)
SiteSetting.content_localization_language_switcher = "anonymous"
visit("/")
expect(page).not_to have_css(switcher_selector)
SiteSetting.content_localization_language_switcher = "all"
visit("/")
expect(page).to have_css(switcher_selector)
end
it "displays the current language code on the trigger button" do
SiteSetting.content_localization_language_switcher = "all"
visit("/")
expect(page.find(switcher_selector)).to have_content("EN")
language_switcher.select_language("ja")
expect(page.find(switcher_selector)).to have_content("JA")
language_switcher.select_language("es")
expect(page.find(switcher_selector)).to have_content("ES")
end
it "shows localized content when switching languages (anon, logged in)" do
SiteSetting.content_localization_language_switcher = "all"
visit("/")
expect(topic_list).to have_content("Life strategies from The Art of War")
language_switcher.select_language("es")
expect(topic_list).to have_content("Estrategias de vida de El arte de la guerra")
I18n.with_locale("es") do
expect(page.find("#navigation-bar")).to have_content(I18n.t("js.filters.latest.title"))
end
sign_in(japanese_user)
visit("/")
expect(topic_list).to have_content("孫子兵法からの人生戦略")
I18n.with_locale("ja") do
expect(page.find("#navigation-bar")).to have_content(I18n.t("js.filters.latest.title"))
end
language_switcher.select_language("es")
expect(topic_list).to have_content("Estrategias de vida de El arte de la guerra")
I18n.with_locale("es") do
expect(page.find("#navigation-bar")).to have_content(I18n.t("js.filters.latest.title"))
end
end
it "resets localized content toggle after changing languages" do
SiteSetting.content_localization_language_switcher = "all"
visit("/t/#{topic.id}")
language_switcher.select_language("ja")
expect(topic_list).to have_content("孫子兵法からの人生戦略")
I18n.with_locale(:ja) do
expect(page.find(toggle_localize_button_selector)["title"]).to eq(
I18n.t("js.content_localization.toggle_localized.translated"),
)
end
page.find(toggle_localize_button_selector).click
expect(topic_list).to have_content("Life strategies from The Art of War")
I18n.with_locale(:ja) do
expect(page.find(toggle_localize_button_selector)["title"]).to eq(
I18n.t("js.content_localization.toggle_localized.not_translated"),
)
end
language_switcher.select_language("es")
expect(topic_list).to have_content("Estrategias de vida de El arte de la guerra")
I18n.with_locale("es") do
expect(page.find(toggle_localize_button_selector)["title"]).to eq(
I18n.t("js.content_localization.toggle_localized.translated"),
)
end
end
it "marks the current language as selected in the dropdown" do
SiteSetting.content_localization_language_switcher = "all"
visit("/")
switcher.expand
expect(page).to have_css("[data-menu-option-id='en'].--selected")
expect(page).to have_no_css("[data-menu-option-id='ja'].--selected")
expect(page).to have_no_css("[data-menu-option-id='es'].--selected")
switcher.collapse
language_switcher.select_language("ja")
switcher.expand
expect(page).to have_css("[data-menu-option-id='ja'].--selected")
expect(page).to have_no_css("[data-menu-option-id='en'].--selected")
expect(page).to have_no_css("[data-menu-option-id='es'].--selected")
end
end