2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2025-10-03 17:21:20 +08:00
discourse/spec/lib/content_localization_spec.rb
Natalie Tay fe30ffa3f9
DEV: Remove 'experimental' prefix from settings (#33233)
This PR takes the localization features out of "experimental" to prep
for the announcement
- rename settings and gives them its own area
- `experimental_content_localization` to `content_localization_enabled`
- `experimental_content_localization_allowed_groups` to
`content_localization_allowed_groups`
- `experimental_content_localization_supported_locales` to
`content_localization_supported_locales`
- `experimental_anon_language_switcher` to
`content_localization_anon_language_switcher`
- migration
- related to https://github.com/discourse/discourse-ai/pull/1439

| screenshot 📸 |
|---|
| <img width="964" alt="Screenshot 2025-06-17 at 5 06 32 PM"
src="https://github.com/user-attachments/assets/9a8b2c38-c846-4fc9-8ddd-815c45cc3d0e"
/> |
2025-06-19 12:23:42 +08:00

125 lines
3.7 KiB
Ruby

# frozen_string_literal: true
describe ContentLocalization do
def create_scope(cookie: nil)
env = create_request_env.merge("HTTP_COOKIE" => cookie)
mock().tap { |m| m.stubs(:request).returns(ActionDispatch::Request.new(env)) }
end
describe ".show_original" do
it "returns true when cookie is present" do
scope = create_scope(cookie: ContentLocalization::SHOW_ORIGINAL_COOKIE)
expect(ContentLocalization.show_original?(scope)).to be true
end
it "returns false when cookie is absent" do
scope = create_scope
expect(ContentLocalization.show_original?(scope)).to be false
end
end
describe ".show_translated_post?" do
fab!(:post)
it "returns true when criteria met" do
SiteSetting.content_localization_enabled = true
post.update!(locale: "ja")
I18n.locale = "de"
scope = create_scope
expect(ContentLocalization.show_translated_post?(post, scope)).to be true
end
context "when criteria not met" do
before do
SiteSetting.content_localization_enabled = true
post.update!(locale: "ja")
I18n.locale = "de"
end
it "returns false when content_localization_enabled is false" do
SiteSetting.content_localization_enabled = false
scope = create_scope
expect(ContentLocalization.show_translated_post?(post, scope)).to be false
end
it "returns false when post raw is nil" do
post.update_columns(raw: "")
scope = create_scope
expect(ContentLocalization.show_translated_post?(post, scope)).to be false
end
it "returns false when post locale is nil" do
post.update!(locale: nil)
scope = create_scope
expect(ContentLocalization.show_translated_post?(post, scope)).to be false
end
it "returns false when post is in user locale" do
post.update!(locale: I18n.locale)
scope = create_scope
expect(ContentLocalization.show_translated_post?(post, scope)).to be false
end
it "returns false when show_original? is true" do
scope = create_scope(cookie: ContentLocalization::SHOW_ORIGINAL_COOKIE)
expect(ContentLocalization.show_translated_post?(post, scope)).to be false
end
end
end
describe ".show_translated_topic?" do
fab!(:topic)
it "returns true when criteria met" do
SiteSetting.content_localization_enabled = true
topic.update!(locale: "ja")
I18n.locale = "de"
scope = create_scope
expect(ContentLocalization.show_translated_topic?(topic, scope)).to be true
end
context "when criteria not met" do
before do
SiteSetting.content_localization_enabled = true
topic.update!(locale: "ja")
I18n.locale = "de"
end
it "returns false when content_localization_enabled is false" do
SiteSetting.content_localization_enabled = false
scope = create_scope
expect(ContentLocalization.show_translated_topic?(topic, scope)).to be false
end
it "returns false when topic locale is nil" do
topic.update!(locale: nil)
scope = create_scope
expect(ContentLocalization.show_translated_topic?(topic, scope)).to be false
end
it "returns false when topic is in user locale" do
topic.update!(locale: I18n.locale)
scope = create_scope
expect(ContentLocalization.show_translated_topic?(topic, scope)).to be false
end
it "returns false when show_original? is true" do
scope = create_scope(cookie: ContentLocalization::SHOW_ORIGINAL_COOKIE)
expect(ContentLocalization.show_translated_topic?(topic, scope)).to be false
end
end
end
end