0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-09 21:45:25 +08:00
discourse/spec/lib/guardian/localization_guardian_spec.rb
Natalie Tay af11f8992c
FEATURE: Localizable /about page fields via its settings page (#41123)
The /about page (unlike /guidelines or /tos) is not backed by a topic,
but instead via a bunch of site settings.

If it were backed by a topic, localization would be easy as it can just
use TopicLocalizations. We also can't swap it out to use a topic, since
there are so many fields on the about page that can't be safely
contained in a topic.

----------

This PR adds SiteSettingLocalization, allowing new attributes in site
settings config for localization.

For now this is wired into the About page settings flow:
  - admins can select a non-default locale on /admin/config/about
  - only translatable About fields are shown for that locale
  - localized About values are used on /about and /about.json
- company_url can be set manually per locale, but is excluded from
automatic translation
- ~~AI backfill can translate eligible site setting localizations~~ no
backfill (added in
https://github.com/discourse/discourse/pull/41123/commits/386c655d2f44aca54440c069bd06ea8007793d84,
removed in
https://github.com/discourse/discourse/pull/41123/commits/6f8ec9cc43dca4195bda638b1ce980eb68a59941.
about page has very non automa-ble fields

This intentionally keeps the scope narrow. It does not make every site
setting localizable, and any other setting can be localized by adding to
that allowlist.

Some screenshots

|page | 📸  | 
|--|--|
| eng setting | <img width="1278" height="871" alt="Screenshot
2026-06-23 at 11 00 34 PM"
src="https://github.com/user-attachments/assets/59431464-fb05-41d9-8c4f-fd9aae7fb45f"
/>
| ja setting | <img width="1278" height="871" alt="Screenshot 2026-06-23
at 11 01 20 PM"
src="https://github.com/user-attachments/assets/73cd2878-f59a-4dc4-a76d-2f46b74a6eed"
/>
| ja anon about page | <img width="1278" height="871" alt="Screenshot
2026-06-23 at 11 01 48 PM"
src="https://github.com/user-attachments/assets/02464a5f-9ca0-4519-9521-be41498c1967"
/>
2026-06-26 20:41:08 +08:00

216 lines
8.8 KiB
Ruby
Vendored

# frozen_string_literal: true
describe LocalizationGuardian do
fab!(:user)
fab!(:admin)
fab!(:moderator)
fab!(:post) { Fabricate(:post, user: user) }
fab!(:other_user_post, :post)
fab!(:topic) { Fabricate(:topic, user: user) }
fab!(:other_user_topic, :topic)
before { SiteSetting.content_localization_enabled = true }
describe "#can_localize_content?" do
it "returns false when content localization is disabled" do
SiteSetting.content_localization_enabled = false
expect(Guardian.new(admin).can_localize_content?).to eq(false)
end
it "returns true for users in allowed groups" do
SiteSetting.content_localization_allowed_groups = "#{Group::AUTO_GROUPS[:admins]}"
expect(Guardian.new(admin).can_localize_content?).to eq(true)
end
it "returns false for users not in allowed groups" do
SiteSetting.content_localization_allowed_groups = "#{Group::AUTO_GROUPS[:admins]}"
expect(Guardian.new(user).can_localize_content?).to eq(false)
end
end
describe "#can_localize_site_settings?" do
it "returns false when content localization is disabled" do
SiteSetting.content_localization_enabled = false
expect(Guardian.new(admin).can_localize_site_settings?).to eq(false)
end
it "returns true for admins when content localization is enabled" do
expect(Guardian.new(admin).can_localize_site_settings?).to eq(true)
end
it "returns false for regular users" do
SiteSetting.content_localization_allowed_groups = "#{Group::AUTO_GROUPS[:everyone]}"
expect(Guardian.new(user).can_localize_site_settings?).to eq(false)
end
end
describe "#can_localize_post?" do
it "returns false when content localization is disabled" do
SiteSetting.content_localization_enabled = false
expect(Guardian.new(user).can_localize_post?(post)).to eq(false)
end
context "when user cannot see the post" do
fab!(:private_category) { Fabricate(:private_category, group: Fabricate(:group)) }
fab!(:private_topic) { Fabricate(:topic, category: private_category) }
fab!(:private_post) { Fabricate(:post, topic: private_topic) }
before do
SiteSetting.content_localization_allowed_groups = "#{Group::AUTO_GROUPS[:everyone]}"
end
it "returns false for posts in private categories the user cannot access" do
expect(Guardian.new(user).can_localize_post?(private_post)).to eq(false)
expect(Guardian.new(user).can_localize_post?(private_post.id)).to eq(false)
end
it "returns false for posts in private messages the user is not part of" do
pm = Fabricate(:private_message_topic)
pm_post = Fabricate(:post, topic: pm)
expect(Guardian.new(user).can_localize_post?(pm_post)).to eq(false)
expect(Guardian.new(user).can_localize_post?(pm_post.id)).to eq(false)
end
it "returns false for non-existent posts" do
expect(Guardian.new(user).can_localize_post?(999_999_999)).to eq(false)
end
end
context "when user is in allowed groups" do
before { SiteSetting.content_localization_allowed_groups = "#{Group::AUTO_GROUPS[:admins]}" }
it "returns true for any post" do
expect(Guardian.new(admin).can_localize_post?(post)).to eq(true)
expect(Guardian.new(admin).can_localize_post?(post.id)).to eq(true)
expect(Guardian.new(admin).can_localize_post?(other_user_post)).to eq(true)
expect(Guardian.new(admin).can_localize_post?(other_user_post.id)).to eq(true)
end
end
context "when author localization is enabled" do
before do
SiteSetting.content_localization_allowed_groups = "#{Group::AUTO_GROUPS[:admins]}"
SiteSetting.content_localization_allow_author_localization = true
end
it "returns true when user is the post author" do
expect(Guardian.new(user).can_localize_post?(post)).to eq(true)
expect(Guardian.new(user).can_localize_post?(post.id)).to eq(true)
end
it "returns false when user is not the post author" do
expect(Guardian.new(user).can_localize_post?(other_user_post)).to eq(false)
expect(Guardian.new(user).can_localize_post?(other_user_post.id)).to eq(false)
end
it "returns true for users in allowed groups regardless of authorship" do
expect(Guardian.new(admin).can_localize_post?(post)).to eq(true)
expect(Guardian.new(admin).can_localize_post?(post.id)).to eq(true)
expect(Guardian.new(admin).can_localize_post?(other_user_post)).to eq(true)
expect(Guardian.new(admin).can_localize_post?(other_user_post.id)).to eq(true)
end
end
context "when author localization is disabled" do
before do
SiteSetting.content_localization_allowed_groups = "#{Group::AUTO_GROUPS[:admins]}"
SiteSetting.content_localization_allow_author_localization = false
end
it "returns false for post authors not in allowed groups" do
expect(Guardian.new(user).can_localize_post?(post)).to eq(false)
expect(Guardian.new(user).can_localize_post?(post.id)).to eq(false)
end
it "returns true for users in allowed groups" do
expect(Guardian.new(admin).can_localize_post?(post)).to eq(true)
expect(Guardian.new(admin).can_localize_post?(post.id)).to eq(true)
end
end
end
describe "#can_localize_topic?" do
it "returns false when content localization is disabled" do
SiteSetting.content_localization_enabled = false
expect(Guardian.new(user).can_localize_topic?(topic)).to eq(false)
end
context "when user cannot see the topic" do
fab!(:private_category) { Fabricate(:private_category, group: Fabricate(:group)) }
fab!(:private_topic) { Fabricate(:topic, category: private_category) }
before do
SiteSetting.content_localization_allowed_groups = "#{Group::AUTO_GROUPS[:everyone]}"
end
it "returns false for topics in private categories the user cannot access" do
expect(Guardian.new(user).can_localize_topic?(private_topic)).to eq(false)
expect(Guardian.new(user).can_localize_topic?(private_topic.id)).to eq(false)
end
it "returns false for private messages the user is not part of" do
pm = Fabricate(:private_message_topic)
expect(Guardian.new(user).can_localize_topic?(pm)).to eq(false)
expect(Guardian.new(user).can_localize_topic?(pm.id)).to eq(false)
end
it "returns false for non-existent topics" do
expect(Guardian.new(user).can_localize_topic?(999_999_999)).to eq(false)
end
end
context "when user is in allowed groups" do
before { SiteSetting.content_localization_allowed_groups = "#{Group::AUTO_GROUPS[:admins]}" }
it "returns true for any topic" do
expect(Guardian.new(admin).can_localize_topic?(topic)).to eq(true)
expect(Guardian.new(admin).can_localize_topic?(topic.id)).to eq(true)
expect(Guardian.new(admin).can_localize_topic?(other_user_topic)).to eq(true)
expect(Guardian.new(admin).can_localize_topic?(other_user_topic.id)).to eq(true)
end
end
context "when author localization is enabled" do
before do
SiteSetting.content_localization_allowed_groups = "#{Group::AUTO_GROUPS[:admins]}"
SiteSetting.content_localization_allow_author_localization = true
end
it "returns true when user is the topic author" do
expect(Guardian.new(user).can_localize_topic?(topic)).to eq(true)
expect(Guardian.new(user).can_localize_topic?(topic.id)).to eq(true)
end
it "returns false when user is not the topic author" do
expect(Guardian.new(user).can_localize_topic?(other_user_topic)).to eq(false)
expect(Guardian.new(user).can_localize_topic?(other_user_topic.id)).to eq(false)
end
it "returns true for users in allowed groups regardless of authorship" do
expect(Guardian.new(admin).can_localize_topic?(topic)).to eq(true)
expect(Guardian.new(admin).can_localize_topic?(topic.id)).to eq(true)
expect(Guardian.new(admin).can_localize_topic?(other_user_topic)).to eq(true)
expect(Guardian.new(admin).can_localize_topic?(other_user_topic.id)).to eq(true)
end
end
context "when author localization is disabled" do
before do
SiteSetting.content_localization_allowed_groups = "#{Group::AUTO_GROUPS[:admins]}"
SiteSetting.content_localization_allow_author_localization = false
end
it "returns false for topic authors not in allowed groups" do
expect(Guardian.new(user).can_localize_topic?(topic)).to eq(false)
expect(Guardian.new(user).can_localize_topic?(topic.id)).to eq(false)
end
it "returns true for users in allowed groups" do
expect(Guardian.new(admin).can_localize_topic?(topic)).to eq(true)
expect(Guardian.new(admin).can_localize_topic?(topic.id)).to eq(true)
end
end
end
end