2019-04-29 20:27:42 -04:00
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
|
RSpec.describe ExtraLocalesController do
|
2024-05-24 10:15:53 -04:00
|
|
|
|
around { |example| allow_missing_translations(&example) }
|
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
|
describe "#show" do
|
2019-08-20 06:38:46 -04:00
|
|
|
|
it "won't work with a weird parameter" do
|
|
|
|
|
get "/extra-locales/-invalid..character!!"
|
|
|
|
|
expect(response.status).to eq(404)
|
2018-01-08 18:23:49 -05:00
|
|
|
|
end
|
|
|
|
|
|
2016-08-25 16:33:29 -04:00
|
|
|
|
it "needs a valid bundle" do
|
2018-06-06 05:45:29 -04:00
|
|
|
|
get "/extra-locales/made-up-bundle"
|
|
|
|
|
expect(response.status).to eq(403)
|
2016-08-25 16:33:29 -04:00
|
|
|
|
end
|
|
|
|
|
|
2019-08-20 06:38:46 -04:00
|
|
|
|
it "requires staff access" do
|
|
|
|
|
get "/extra-locales/admin"
|
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
|
|
|
|
|
|
get "/extra-locales/wizard"
|
|
|
|
|
expect(response.status).to eq(403)
|
2016-08-25 16:33:29 -04:00
|
|
|
|
end
|
2016-09-28 16:26:36 -04:00
|
|
|
|
|
2019-11-11 16:30:31 -05:00
|
|
|
|
it "requires a valid version" do
|
|
|
|
|
get "/extra-locales/overrides", params: { v: "a" }
|
|
|
|
|
expect(response.status).to eq(400)
|
|
|
|
|
|
|
|
|
|
get "/extra-locales/overrides?v[foo]=1"
|
|
|
|
|
expect(response.status).to eq(400)
|
|
|
|
|
end
|
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
|
context "when logged in as a moderator" do
|
2019-08-20 06:38:46 -04:00
|
|
|
|
let(:moderator) { Fabricate(:moderator) }
|
|
|
|
|
before { sign_in(moderator) }
|
|
|
|
|
|
2019-11-04 20:15:44 -05:00
|
|
|
|
it "caches for 1 year if version is provided and it matches current hash" do
|
2019-08-20 06:38:46 -04:00
|
|
|
|
get "/extra-locales/admin", params: { v: ExtraLocalesController.bundle_js_hash("admin") }
|
|
|
|
|
expect(response.status).to eq(200)
|
2019-11-04 20:15:44 -05:00
|
|
|
|
expect(response.headers["Cache-Control"]).to eq("max-age=31556952, public, immutable")
|
2019-05-23 15:23:31 -04:00
|
|
|
|
end
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
2019-08-20 06:38:46 -04:00
|
|
|
|
it "does not cache at all if version is invalid" do
|
|
|
|
|
get "/extra-locales/admin", params: { v: "a" * 32 }
|
|
|
|
|
expect(response.status).to eq(200)
|
2019-11-04 20:15:44 -05:00
|
|
|
|
expect(response.headers["Cache-Control"]).not_to include("max-age", "public", "immutable")
|
2019-05-23 15:23:31 -04:00
|
|
|
|
end
|
2016-09-28 16:26:36 -04:00
|
|
|
|
|
2024-06-17 12:21:04 -04:00
|
|
|
|
it "doesn’t generate the bundle twice" do
|
|
|
|
|
described_class.expects(:bundle_js).returns("JS").once
|
|
|
|
|
get "/extra-locales/admin", params: { v: "a" * 32 }
|
|
|
|
|
end
|
|
|
|
|
|
2019-08-20 06:38:46 -04:00
|
|
|
|
context "with plugin" do
|
|
|
|
|
before do
|
|
|
|
|
JsLocaleHelper.clear_cache!
|
|
|
|
|
JsLocaleHelper
|
|
|
|
|
.expects(:plugin_translations)
|
2021-01-20 15:32:22 -05:00
|
|
|
|
.with(any_of("en", "en_GB"))
|
2019-08-20 06:38:46 -04:00
|
|
|
|
.returns(
|
|
|
|
|
"admin_js" => {
|
|
|
|
|
"admin" => {
|
|
|
|
|
"site_settings" => {
|
|
|
|
|
"categories" => {
|
2021-05-20 21:43:47 -04:00
|
|
|
|
"github_badges" => "GitHub Badges",
|
2023-01-09 06:18:21 -05:00
|
|
|
|
},
|
2019-08-20 06:38:46 -04:00
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2023-01-09 06:18:21 -05:00
|
|
|
|
)
|
2019-08-20 06:38:46 -04:00
|
|
|
|
.at_least_once
|
|
|
|
|
end
|
2019-05-23 15:23:31 -04:00
|
|
|
|
|
2019-08-20 06:38:46 -04:00
|
|
|
|
after { JsLocaleHelper.clear_cache! }
|
|
|
|
|
|
|
|
|
|
it "includes plugin translations" do
|
|
|
|
|
get "/extra-locales/admin"
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
expect(response.body.include?("github_badges")).to eq(true)
|
|
|
|
|
end
|
2019-05-23 15:23:31 -04:00
|
|
|
|
end
|
2016-09-28 16:26:36 -04:00
|
|
|
|
end
|
2019-11-04 20:15:44 -05:00
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
|
context "with overridden translations" do
|
2019-11-04 20:15:44 -05:00
|
|
|
|
after { I18n.reload! }
|
|
|
|
|
|
|
|
|
|
it "works for anonymous users" do
|
|
|
|
|
TranslationOverride.upsert!(I18n.locale, "js.some_key", "client-side translation")
|
|
|
|
|
|
|
|
|
|
get "/extra-locales/overrides",
|
|
|
|
|
params: {
|
|
|
|
|
v: ExtraLocalesController.bundle_js_hash("overrides"),
|
|
|
|
|
}
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
expect(response.headers["Cache-Control"]).to eq("max-age=31556952, public, immutable")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "returns nothing when there are not overridden translations" do
|
|
|
|
|
get "/extra-locales/overrides"
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
expect(response.body).to be_empty
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context "with translations" do
|
|
|
|
|
it "returns the correct translations" do
|
|
|
|
|
TranslationOverride.upsert!(I18n.locale, "js.some_key", "client-side translation")
|
|
|
|
|
TranslationOverride.upsert!(
|
|
|
|
|
I18n.locale,
|
|
|
|
|
"js.client_MF",
|
|
|
|
|
"{NUM_RESULTS, plural, one {1 result} other {many} }",
|
|
|
|
|
)
|
|
|
|
|
TranslationOverride.upsert!(I18n.locale, "admin_js.another_key", "admin client js")
|
|
|
|
|
TranslationOverride.upsert!(I18n.locale, "server.some_key", "server-side translation")
|
|
|
|
|
TranslationOverride.upsert!(
|
|
|
|
|
I18n.locale,
|
|
|
|
|
"server.some_MF",
|
|
|
|
|
"{NUM_RESULTS, plural, one {1 result} other {many} }",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
get "/extra-locales/overrides"
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
expect(response.body).to_not include("server.some_key", "server.some_MF")
|
|
|
|
|
|
|
|
|
|
ctx = MiniRacer::Context.new
|
|
|
|
|
ctx.eval("I18n = {};")
|
|
|
|
|
ctx.eval(response.body)
|
|
|
|
|
|
2021-12-16 15:53:50 -05:00
|
|
|
|
expect(ctx.eval("I18n._overrides['#{I18n.locale}']['js.some_key']")).to eq(
|
|
|
|
|
"client-side translation",
|
|
|
|
|
)
|
|
|
|
|
expect(ctx.eval("I18n._overrides['#{I18n.locale}']['js.client_MF'] === undefined")).to eq(
|
|
|
|
|
true,
|
|
|
|
|
)
|
|
|
|
|
expect(ctx.eval("I18n._overrides['#{I18n.locale}']['admin_js.another_key']")).to eq(
|
|
|
|
|
"admin client js",
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "returns overrides from fallback locale" do
|
|
|
|
|
TranslationOverride.upsert!(:en, "js.some_key", "some key (en)")
|
|
|
|
|
TranslationOverride.upsert!(:fr, "js.some_key", "some key (fr)")
|
|
|
|
|
TranslationOverride.upsert!(:en, "js.only_en", "only English")
|
|
|
|
|
TranslationOverride.upsert!(:fr, "js.only_fr", "only French")
|
|
|
|
|
TranslationOverride.upsert!(
|
|
|
|
|
:en,
|
|
|
|
|
"js.some_client_MF",
|
|
|
|
|
"{NUM_RESULTS, plural, one {1 result} other {many} }",
|
|
|
|
|
)
|
|
|
|
|
TranslationOverride.upsert!(
|
2023-01-09 06:18:21 -05:00
|
|
|
|
:fr,
|
2021-12-16 15:53:50 -05:00
|
|
|
|
"js.some_client_MF",
|
|
|
|
|
"{NUM_RESULTS, plural, one {1 result} other {many} }",
|
|
|
|
|
)
|
|
|
|
|
TranslationOverride.upsert!(
|
2023-01-09 06:18:21 -05:00
|
|
|
|
:en,
|
2021-12-16 15:53:50 -05:00
|
|
|
|
"js.only_en_MF",
|
|
|
|
|
"{NUM_RESULTS, plural, one {1 result} other {many} }",
|
|
|
|
|
)
|
|
|
|
|
TranslationOverride.upsert!(
|
|
|
|
|
:fr,
|
|
|
|
|
"js.only_fr_MF",
|
|
|
|
|
"{NUM_RESULTS, plural, one {1 result} other {many} }",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
SiteSetting.allow_user_locale = true
|
|
|
|
|
user = Fabricate(:user, locale: :fr)
|
|
|
|
|
sign_in(user)
|
|
|
|
|
|
|
|
|
|
get "/extra-locales/overrides"
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
|
|
ctx = MiniRacer::Context.new
|
|
|
|
|
ctx.eval("I18n = {};")
|
|
|
|
|
ctx.eval(response.body)
|
|
|
|
|
|
|
|
|
|
overrides = ctx.eval("I18n._overrides")
|
|
|
|
|
expect(overrides.keys).to contain_exactly("en", "fr")
|
|
|
|
|
expect(overrides["en"]).to eq({ "js.only_en" => "only English" })
|
|
|
|
|
expect(overrides["fr"]).to eq(
|
|
|
|
|
{ "js.some_key" => "some key (fr)", "js.only_fr" => "only French" },
|
|
|
|
|
)
|
2019-11-04 20:15:44 -05:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2024-06-17 12:21:04 -04:00
|
|
|
|
|
|
|
|
|
context "when requesting MessageFormat translations" do
|
|
|
|
|
before { JsLocaleHelper.stubs(:output_MF).with("en").returns("MF_TRANSLATIONS") }
|
|
|
|
|
|
|
|
|
|
it "returns the translations properly" do
|
|
|
|
|
get "/extra-locales/mf"
|
|
|
|
|
expect(response.body).to eq("MF_TRANSLATIONS")
|
|
|
|
|
end
|
|
|
|
|
end
|
2016-08-25 16:33:29 -04:00
|
|
|
|
end
|
2019-05-23 15:41:48 -04:00
|
|
|
|
|
|
|
|
|
describe ".bundle_js_hash" do
|
|
|
|
|
it "doesn't call bundle_js more than once for the same locale and bundle" do
|
|
|
|
|
I18n.locale = :de
|
|
|
|
|
ExtraLocalesController.expects(:bundle_js).with("admin").returns("admin_js DE").once
|
|
|
|
|
expected_hash_de = Digest::MD5.hexdigest("admin_js DE")
|
|
|
|
|
|
|
|
|
|
expect(ExtraLocalesController.bundle_js_hash("admin")).to eq(expected_hash_de)
|
|
|
|
|
expect(ExtraLocalesController.bundle_js_hash("admin")).to eq(expected_hash_de)
|
|
|
|
|
|
|
|
|
|
I18n.locale = :fr
|
|
|
|
|
ExtraLocalesController.expects(:bundle_js).with("admin").returns("admin_js FR").once
|
|
|
|
|
expected_hash_fr = Digest::MD5.hexdigest("admin_js FR")
|
|
|
|
|
|
|
|
|
|
expect(ExtraLocalesController.bundle_js_hash("admin")).to eq(expected_hash_fr)
|
|
|
|
|
expect(ExtraLocalesController.bundle_js_hash("admin")).to eq(expected_hash_fr)
|
|
|
|
|
|
|
|
|
|
I18n.locale = :de
|
|
|
|
|
expect(ExtraLocalesController.bundle_js_hash("admin")).to eq(expected_hash_de)
|
|
|
|
|
|
|
|
|
|
ExtraLocalesController.expects(:bundle_js).with("wizard").returns("wizard_js DE").once
|
|
|
|
|
expected_hash_de = Digest::MD5.hexdigest("wizard_js DE")
|
|
|
|
|
|
|
|
|
|
expect(ExtraLocalesController.bundle_js_hash("wizard")).to eq(expected_hash_de)
|
|
|
|
|
expect(ExtraLocalesController.bundle_js_hash("wizard")).to eq(expected_hash_de)
|
|
|
|
|
end
|
|
|
|
|
end
|
2019-11-04 20:15:44 -05:00
|
|
|
|
|
|
|
|
|
describe ".client_overrides_exist?" do
|
|
|
|
|
after do
|
|
|
|
|
I18n.reload!
|
|
|
|
|
ExtraLocalesController.clear_cache!
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "returns false if there are no client-side translation overrides" do
|
|
|
|
|
expect(ExtraLocalesController.client_overrides_exist?).to eq(false)
|
|
|
|
|
|
|
|
|
|
TranslationOverride.upsert!(I18n.locale, "server.some_key", "server-side translation")
|
|
|
|
|
expect(ExtraLocalesController.client_overrides_exist?).to eq(false)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "returns true if there are client-side translation overrides" do
|
|
|
|
|
expect(ExtraLocalesController.client_overrides_exist?).to eq(false)
|
|
|
|
|
|
|
|
|
|
TranslationOverride.upsert!(I18n.locale, "js.some_key", "client-side translation")
|
|
|
|
|
expect(ExtraLocalesController.client_overrides_exist?).to eq(true)
|
|
|
|
|
end
|
|
|
|
|
end
|
2024-06-17 12:21:04 -04:00
|
|
|
|
|
|
|
|
|
describe ".bundle_js_with_hash" do
|
|
|
|
|
before { described_class.stubs(:bundle_js).with("admin").returns("JS") }
|
|
|
|
|
|
|
|
|
|
it "returns both JS and its hash for a given bundle" do
|
|
|
|
|
expect(described_class.bundle_js_with_hash("admin")).to eq(
|
|
|
|
|
["JS", Digest::MD5.hexdigest("JS")],
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
end
|
2016-08-25 16:33:29 -04:00
|
|
|
|
end
|