2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-03-05 15:27:34 +08:00
discourse/spec/serializers/post_revision_serializer_spec.rb
Natalie Tay 235c673fe8
FEATURE: Localize language names (#33790)
This PR adds localized language names to settings. The language names
are localized in the frontend, not the backend, due to setting
initialization complexity.

This change affects these areas:
- `SiteSetting.available_locales` 
- this "setting" is a lookup table to get language names. use
`languageNameLookup` service to get the name for a locale
- it returns an object that looks like the following, then gets
re-hydrated with client localized values when initializing the
`siteSettingService` in the frontend.
  ```
  [
{"native_name":"اللغة العربية","value":"ar","name":"languages.ar.name"},
    ...
  ]  
  ```

- `SiteSetting.default_locale` 
- this is a single-value `enum` setting that has always been hardcoded.
This caused quite an issue as it is not initialized the same way as
other site settings in the yml file. It has always relied on reading
directly from a `names.yml` file to load native language names, thus
bypassing the need for I18n to be initialized from the backend. A new
locale_enum type has been introduced for this setting, and any future
settings.
  
- `SiteSetting.content_localization_supported_locales` - this is a
`enum_list` setting,
  - enum_list is introduced, leveraging both `list` and `enum`
  
- theme translations

- site texts

- Wizard's default_locale choices 
- it was set up from the backend using `LocaleSiteSetting.value`. This
proved problematic, as a Japanese user would be getting the locales in
English because the values are initialized using English even without
memoization
- therefore we're now initializing the choices in the frontend using
`available_locales` as defined above
  
- content localization meta data
- post language in the composer, localization composer, post history
modal, post language tooltip, language switcher



/t/151409
2025-07-29 11:48:45 +08:00

179 lines
5.2 KiB
Ruby

# frozen_string_literal: true
RSpec.describe PostRevisionSerializer do
fab!(:post) { Fabricate(:post, version: 2) }
context "with secured categories" do
fab!(:group)
fab!(:private_category) { Fabricate(:private_category, group: group) }
fab!(:post_revision) do
Fabricate(
:post_revision,
post: post,
modifications: {
"category_id" => [private_category.id, post.topic.category_id],
},
)
end
it "returns category changes to staff" do
json =
PostRevisionSerializer.new(
post_revision,
scope: Guardian.new(Fabricate(:admin)),
root: false,
).as_json
expect(json[:category_id_changes][:previous]).to eq(private_category.id)
expect(json[:category_id_changes][:current]).to eq(post.topic.category_id)
end
it "does not return all category changes to non-staff" do
json =
PostRevisionSerializer.new(
post_revision,
scope: Guardian.new(Fabricate(:user)),
root: false,
).as_json
expect(json[:category_id_changes][:previous]).to eq(nil)
expect(json[:category_id_changes][:current]).to eq(post.topic.category_id)
end
end
it "handles tags not being an array" do
pr = Fabricate(:post_revision, post: post, modifications: { "tags" => ["[]", ""] })
json =
PostRevisionSerializer.new(pr, scope: Guardian.new(Fabricate(:user)), root: false).as_json
expect(json[:tags_changes][:previous]).to eq("[]")
expect(json[:tags_changes][:current]).to eq([])
end
context "with hidden tags" do
fab!(:public_tag) { Fabricate(:tag, name: "public") }
fab!(:public_tag2) { Fabricate(:tag, name: "visible") }
fab!(:hidden_tag) { Fabricate(:tag, name: "hidden") }
fab!(:hidden_tag2) { Fabricate(:tag, name: "secret") }
fab!(:staff_tag_group) do
Fabricate(
:tag_group,
permissions: {
"staff" => 1,
},
tag_names: [hidden_tag.name, hidden_tag2.name],
)
end
let(:post_revision) do
Fabricate(
:post_revision,
post: post,
modifications: {
"tags" => [%w[public hidden], %w[visible hidden]],
},
)
end
let(:post_revision2) do
Fabricate(
:post_revision,
post: post,
modifications: {
"tags" => [%w[visible hidden secret], %w[visible hidden]],
},
)
end
before do
SiteSetting.tagging_enabled = true
post.topic.tags = [public_tag2, hidden_tag]
end
it "returns all tag changes to staff" do
json =
PostRevisionSerializer.new(
post_revision,
scope: Guardian.new(Fabricate(:admin)),
root: false,
).as_json
expect(json[:tags_changes][:previous]).to contain_exactly(public_tag.name, hidden_tag.name)
expect(json[:tags_changes][:current]).to contain_exactly(public_tag2.name, hidden_tag.name)
end
it "does not return hidden tags to non-staff" do
json =
PostRevisionSerializer.new(
post_revision,
scope: Guardian.new(Fabricate(:user)),
root: false,
).as_json
expect(json[:tags_changes][:previous]).to contain_exactly(public_tag.name)
expect(json[:tags_changes][:current]).to contain_exactly(public_tag2.name)
end
it "does not show tag modifications if changes are not visible to the user" do
json =
PostRevisionSerializer.new(
post_revision2,
scope: Guardian.new(Fabricate(:user)),
root: false,
).as_json
expect(json[:tags_changes]).to_not be_present
end
end
context "when some tracked topic fields are associations" do
let(:serializer) { described_class.new(post_revision, scope: guardian, root: false) }
let(:post_revision) { Fabricate(:post_revision, post:) }
let(:guardian) { Discourse.system_user.guardian }
before do
allow(PostRevisor).to receive(:tracked_topic_fields).and_wrap_original do |original_method|
original_method.call.merge(allowed_users: -> {}, allowed_groups: -> {})
end
end
it "skips them" do
expect { serializer.as_json }.not_to raise_error
end
end
describe "post locale edits" do
it "returns the locale changes when set to nothing" do
post_revision =
Fabricate(:post_revision, post: post, modifications: { "locale" => ["ja", ""] })
json =
PostRevisionSerializer.new(
post_revision,
scope: Guardian.new(Fabricate(:user)),
root: false,
).as_json
expect(json[:locale_changes][:previous]).to eq("ja")
expect(json[:locale_changes][:current]).to eq(nil)
end
it "returns the locale changes when set from nothing to something" do
post.update!(locale: "ja")
post_revision =
Fabricate(:post_revision, post: post, modifications: { "locale" => ["", "ja"] })
json =
PostRevisionSerializer.new(
post_revision,
scope: Guardian.new(Fabricate(:user)),
root: false,
).as_json
expect(json[:locale_changes][:previous]).to eq(nil)
expect(json[:locale_changes][:current]).to eq("ja")
end
end
end