2018-05-19 14:15:57 +08:00
|
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
|
|
RSpec.describe Admin::SiteTextsController do
|
|
|
|
|
let(:admin) { Fabricate(:admin) }
|
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
|
|
|
|
|
|
after do
|
|
|
|
|
TranslationOverride.delete_all
|
|
|
|
|
I18n.reload!
|
|
|
|
|
end
|
|
|
|
|
|
2018-06-11 12:59:21 +08:00
|
|
|
|
it "is a subclass of AdminController" do
|
|
|
|
|
expect(Admin::SiteTextsController < Admin::AdminController).to eq(true)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context "when not logged in as an admin" do
|
2018-05-19 14:15:57 +08:00
|
|
|
|
it "raises an error if you aren't logged in" do
|
2018-05-21 09:49:46 +08:00
|
|
|
|
put '/admin/customize/site_texts/some_key.json', params: {
|
2018-05-19 14:15:57 +08:00
|
|
|
|
site_text: { value: 'foo' }
|
2018-05-21 09:49:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-19 14:15:57 +08:00
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "raises an error if you aren't an admin" do
|
|
|
|
|
sign_in(user)
|
2018-05-21 09:49:46 +08:00
|
|
|
|
|
2018-06-11 12:59:21 +08:00
|
|
|
|
put "/admin/customize/site_texts/some_key.json", params: {
|
2018-05-19 14:15:57 +08:00
|
|
|
|
site_text: { value: 'foo' }
|
2018-05-21 09:49:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-19 14:15:57 +08:00
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
|
end
|
2018-06-11 12:59:21 +08:00
|
|
|
|
end
|
|
|
|
|
|
2018-11-10 08:17:07 +08:00
|
|
|
|
context "when logged in as admin" do
|
2018-06-11 12:59:21 +08:00
|
|
|
|
before do
|
|
|
|
|
sign_in(admin)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe '#index' do
|
|
|
|
|
it 'returns json' do
|
2018-11-10 08:17:07 +08:00
|
|
|
|
get "/admin/customize/site_texts.json", params: { q: 'title' }
|
2018-06-11 12:59:21 +08:00
|
|
|
|
expect(response.status).to eq(200)
|
2018-11-10 08:17:07 +08:00
|
|
|
|
expect(JSON.parse(response.body)['site_texts']).to include(include("id" => "title"))
|
2018-06-11 12:59:21 +08:00
|
|
|
|
end
|
2018-11-10 08:17:07 +08:00
|
|
|
|
|
|
|
|
|
it 'normalizes quotes during search' do
|
|
|
|
|
value = %q|“That’s a ‘magic’ sock.”|
|
|
|
|
|
put "/admin/customize/site_texts/title.json", params: { site_text: { value: value } }
|
|
|
|
|
|
|
|
|
|
[
|
|
|
|
|
%q|That's a 'magic' sock.|,
|
|
|
|
|
%q|That’s a ‘magic’ sock.|,
|
|
|
|
|
%q|“That's a 'magic' sock.”|,
|
|
|
|
|
%q|"That's a 'magic' sock."|,
|
|
|
|
|
%q|«That's a 'magic' sock.»|,
|
|
|
|
|
%q|„That’s a ‚magic‘ sock.“|
|
|
|
|
|
].each do |search_term|
|
|
|
|
|
get "/admin/customize/site_texts.json", params: { q: search_term }
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
expect(JSON.parse(response.body)['site_texts']).to include(include("id" => "title", "value" => value))
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'normalizes ellipsis' do
|
|
|
|
|
value = "Loading Discussion…"
|
|
|
|
|
put "/admin/customize/site_texts/embed.loading.json", params: { site_text: { value: value } }
|
|
|
|
|
|
|
|
|
|
[
|
|
|
|
|
"Loading Discussion",
|
|
|
|
|
"Loading Discussion...",
|
|
|
|
|
"Loading Discussion…"
|
|
|
|
|
].each do |search_term|
|
|
|
|
|
get "/admin/customize/site_texts.json", params: { q: search_term }
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
expect(JSON.parse(response.body)['site_texts']).to include(include("id" => "embed.loading", "value" => value))
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-06-11 12:59:21 +08:00
|
|
|
|
end
|
2018-05-19 14:15:57 +08:00
|
|
|
|
|
2018-06-11 12:59:21 +08:00
|
|
|
|
describe '#show' do
|
|
|
|
|
it 'returns a site text for a key that exists' do
|
|
|
|
|
get "/admin/customize/site_texts/js.topic.list.json"
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
|
|
json = ::JSON.parse(response.body)
|
|
|
|
|
|
|
|
|
|
site_text = json['site_text']
|
|
|
|
|
|
|
|
|
|
expect(site_text['id']).to eq('js.topic.list')
|
|
|
|
|
expect(site_text['value']).to eq(I18n.t("js.topic.list"))
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'returns not found for missing keys' do
|
|
|
|
|
get "/admin/customize/site_texts/made_up_no_key_exists.json"
|
|
|
|
|
expect(response.status).to eq(404)
|
2018-05-19 14:15:57 +08:00
|
|
|
|
end
|
2018-06-11 12:59:21 +08:00
|
|
|
|
end
|
2018-05-19 14:15:57 +08:00
|
|
|
|
|
2018-06-11 12:59:21 +08:00
|
|
|
|
describe '#update & #revert' do
|
2018-05-19 14:15:57 +08:00
|
|
|
|
it "returns 'not found' when an unknown key is used" do
|
2018-05-21 09:49:46 +08:00
|
|
|
|
put '/admin/customize/site_texts/some_key.json', params: {
|
2018-05-19 14:15:57 +08:00
|
|
|
|
site_text: { value: 'foo' }
|
2018-05-21 09:49:46 +08:00
|
|
|
|
}
|
2018-05-19 14:15:57 +08:00
|
|
|
|
|
2018-05-21 09:49:46 +08:00
|
|
|
|
expect(response.status).to eq(404)
|
2018-05-19 14:15:57 +08:00
|
|
|
|
|
2018-05-21 09:49:46 +08:00
|
|
|
|
json = JSON.parse(response.body)
|
2018-05-19 14:15:57 +08:00
|
|
|
|
expect(json['error_type']).to eq('not_found')
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "works as expectd with correct keys" do
|
2018-05-21 13:25:56 +08:00
|
|
|
|
put '/admin/customize/site_texts/login_required.welcome_message.json', params: {
|
2018-05-19 14:15:57 +08:00
|
|
|
|
site_text: { value: 'foo' }
|
2018-05-21 09:49:46 +08:00
|
|
|
|
}
|
2018-05-19 14:15:57 +08:00
|
|
|
|
|
2018-05-21 09:49:46 +08:00
|
|
|
|
expect(response.status).to eq(200)
|
2018-05-19 14:15:57 +08:00
|
|
|
|
|
|
|
|
|
json = ::JSON.parse(response.body)
|
|
|
|
|
site_text = json['site_text']
|
|
|
|
|
|
2018-05-21 13:25:56 +08:00
|
|
|
|
expect(site_text['id']).to eq('login_required.welcome_message')
|
2018-05-19 14:15:57 +08:00
|
|
|
|
expect(site_text['value']).to eq('foo')
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "does not update restricted keys" do
|
2018-05-21 09:49:46 +08:00
|
|
|
|
put '/admin/customize/site_texts/user_notifications.confirm_old_email.title.json', params: {
|
2018-05-19 14:15:57 +08:00
|
|
|
|
site_text: { value: 'foo' }
|
2018-05-21 09:49:46 +08:00
|
|
|
|
}
|
2018-05-19 14:15:57 +08:00
|
|
|
|
|
2018-05-21 09:49:46 +08:00
|
|
|
|
expect(response.status).to eq(404)
|
2018-05-19 14:15:57 +08:00
|
|
|
|
|
|
|
|
|
json = ::JSON.parse(response.body)
|
|
|
|
|
expect(json['error_type']).to eq('not_found')
|
|
|
|
|
end
|
2018-06-11 12:59:21 +08:00
|
|
|
|
|
|
|
|
|
it "returns the right error message" do
|
|
|
|
|
I18n.backend.store_translations(:en, some_key: '%{first} %{second}')
|
|
|
|
|
|
|
|
|
|
put "/admin/customize/site_texts/some_key.json", params: {
|
|
|
|
|
site_text: { value: 'hello %{key} %{omg}' }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
expect(response.status).to eq(422)
|
|
|
|
|
|
|
|
|
|
body = JSON.parse(response.body)
|
|
|
|
|
|
|
|
|
|
expect(body['message']).to eq(I18n.t(
|
|
|
|
|
'activerecord.errors.models.translation_overrides.attributes.value.invalid_interpolation_keys',
|
|
|
|
|
keys: 'key, omg'
|
|
|
|
|
))
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'logs the change' do
|
|
|
|
|
original_title = I18n.t(:title)
|
|
|
|
|
|
|
|
|
|
put "/admin/customize/site_texts/title.json", params: {
|
|
|
|
|
site_text: { value: 'yay' }
|
|
|
|
|
}
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
|
|
log = UserHistory.last
|
|
|
|
|
|
|
|
|
|
expect(log.previous_value).to eq(original_title)
|
|
|
|
|
expect(log.new_value).to eq('yay')
|
|
|
|
|
expect(log.action).to eq(UserHistory.actions[:change_site_text])
|
|
|
|
|
|
|
|
|
|
delete "/admin/customize/site_texts/title.json"
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
|
|
log = UserHistory.last
|
|
|
|
|
|
|
|
|
|
expect(log.previous_value).to eq('yay')
|
|
|
|
|
expect(log.new_value).to eq(original_title)
|
|
|
|
|
expect(log.action).to eq(UserHistory.actions[:change_site_text])
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'updates and reverts the key' do
|
|
|
|
|
orig_title = I18n.t(:title)
|
|
|
|
|
|
|
|
|
|
put "/admin/customize/site_texts/title.json", params: { site_text: { value: 'hello' } }
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
|
|
json = ::JSON.parse(response.body)
|
|
|
|
|
|
|
|
|
|
site_text = json['site_text']
|
|
|
|
|
|
|
|
|
|
expect(site_text['id']).to eq('title')
|
|
|
|
|
expect(site_text['value']).to eq('hello')
|
|
|
|
|
|
|
|
|
|
# Revert
|
|
|
|
|
delete "/admin/customize/site_texts/title.json"
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
|
|
json = ::JSON.parse(response.body)
|
|
|
|
|
|
|
|
|
|
site_text = json['site_text']
|
|
|
|
|
|
|
|
|
|
expect(site_text['id']).to eq('title')
|
|
|
|
|
expect(site_text['value']).to eq(orig_title)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'returns site texts for the correct locale' do
|
|
|
|
|
SiteSetting.default_locale = :ru
|
|
|
|
|
|
|
|
|
|
ru_title = 'title ru'
|
|
|
|
|
ru_mf_text = 'ru {NUM_RESULTS, plural, one {1 result} other {many} }'
|
|
|
|
|
|
|
|
|
|
put "/admin/customize/site_texts/title.json", params: { site_text: { value: ru_title } }
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
put "/admin/customize/site_texts/js.topic.read_more_MF.json", params: { site_text: { value: ru_mf_text } }
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
|
|
get "/admin/customize/site_texts/title.json"
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
json = ::JSON.parse(response.body)
|
|
|
|
|
expect(json['site_text']['value']).to eq(ru_title)
|
|
|
|
|
|
|
|
|
|
get "/admin/customize/site_texts/js.topic.read_more_MF.json"
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
json = ::JSON.parse(response.body)
|
|
|
|
|
expect(json['site_text']['value']).to eq(ru_mf_text)
|
|
|
|
|
|
|
|
|
|
SiteSetting.default_locale = :en
|
|
|
|
|
|
|
|
|
|
get "/admin/customize/site_texts/title.json"
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
json = ::JSON.parse(response.body)
|
|
|
|
|
expect(json['site_text']['value']).to_not eq(ru_title)
|
|
|
|
|
|
|
|
|
|
get "/admin/customize/site_texts/js.topic.read_more_MF.json"
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
json = ::JSON.parse(response.body)
|
|
|
|
|
expect(json['site_text']['value']).to_not eq(ru_mf_text)
|
|
|
|
|
end
|
2018-05-19 14:15:57 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|