discourse/spec/requests/post_localizations_controller_spec.rb
Keegan George 6154fa6b45
FEATURE: Add translations to posts (#32564)
## 🔍 Overview
This update adds the ability for users to manually add translations to
specific posts. It adds a 🌐 icon on the post menu where you can click to
add translations for posts.

It also introduces a new site setting:
`content_localization_debug_allowed_groups` which is convenient when
debugging localized posts. It adds a globe icon in the post meta data
area along with a number of how many languages the post is translated
in. Hovering over the icon will show a tooltip with access to editing
and deleting the translated posts.

## 📸 Screenshots
<img width="1234" alt="Screenshot 2025-05-07 at 13 26 09"
src="https://github.com/user-attachments/assets/9d65374d-ee3e-4e8b-b171-b98db6f90f23"
/>
<img width="300" alt="Screenshot 2025-05-07 at 13 26 41"
src="https://github.com/user-attachments/assets/6ee9c5e6-16ed-4dab-97ec-9401804a4ac8"
/>
2025-05-08 10:40:36 -07:00

110 lines
3.1 KiB
Ruby

# frozen_string_literal: true
describe PostLocalizationsController do
fab!(:user)
fab!(:group)
fab!(:post_record) { Fabricate(:post, version: 100) }
let(:locale) { "ja" }
let(:raw) { "これは翻訳です。" }
before do
SiteSetting.experimental_content_localization = true
SiteSetting.experimental_content_localization_allowed_groups = group.id.to_s
group.add(user)
sign_in(user)
end
describe "#create_or_update" do
context "when localization does not exist" do
it "creates a new localization" do
expect {
post "/post_localizations/create_or_update.json",
params: {
post_id: post_record.id,
locale: locale,
raw: raw,
}
}.to change { PostLocalization.count }.by(1)
expect(response.status).to eq(201)
localization = PostLocalization.last
expect(localization).to have_attributes(
locale: locale,
raw: raw,
post_id: post_record.id,
post_version: post_record.version,
localizer_user_id: user.id,
)
end
end
context "when localization already exists" do
it "updates the existing localization" do
localization = Fabricate(:post_localization, post: post_record, locale: locale, raw: "古い翻訳")
new_user = Fabricate(:user, groups: [group])
sign_in(new_user)
expect {
post "/post_localizations/create_or_update.json",
params: {
post_id: post_record.id,
locale: locale,
raw: raw,
}
}.not_to change { PostLocalization.count }
expect(response.status).to eq(200)
localization.reload
expect(localization.raw).to eq(raw)
expect(localization.localizer_user_id).to eq(new_user.id)
end
end
it "returns forbidden if user is not in allowed group" do
group.remove(user)
post "/post_localizations/create_or_update.json",
params: {
post_id: post_record.id,
locale: locale,
raw: raw,
}
expect(response.status).to eq(403)
end
it "returns not found if post does not exist" do
post "/post_localizations/create_or_update.json",
params: {
post_id: -1,
locale: locale,
raw: raw,
}
expect(response.status).to eq(404)
end
end
describe "#destroy" do
it "destroys the localization" do
Fabricate(:post_localization, post: post_record, locale: locale)
expect {
delete "/post_localizations/destroy.json",
params: {
post_id: post_record.id,
locale: locale,
}
}.to change { PostLocalization.count }.by(-1)
expect(response.status).to eq(204)
end
it "returns 404 if localization is missing" do
delete "/post_localizations/destroy.json", params: { post_id: post_record.id, locale: "nope" }
expect(response.status).to eq(404)
end
end
end