2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2013-04-04 12:59:44 -04:00
|
|
|
|
2015-08-10 10:34:40 -04:00
|
|
|
describe Admin::SiteTextsController do
|
2013-04-04 12:59:44 -04:00
|
|
|
|
|
|
|
it "is a subclass of AdminController" do
|
2015-08-10 10:34:40 -04:00
|
|
|
expect(Admin::SiteTextsController < Admin::AdminController).to eq(true)
|
2013-04-04 12:59:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'while logged in as an admin' do
|
|
|
|
before do
|
|
|
|
@user = log_in(:admin)
|
|
|
|
end
|
|
|
|
|
2015-11-23 16:45:05 -05:00
|
|
|
context '.index' do
|
|
|
|
it 'returns json' do
|
2017-08-31 00:06:56 -04:00
|
|
|
get :index, params: { q: 'title' }, format: :json
|
2015-11-23 16:45:05 -05:00
|
|
|
expect(response).to be_success
|
|
|
|
expect(::JSON.parse(response.body)).to be_present
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-04-04 12:59:44 -04:00
|
|
|
context '.show' do
|
2015-11-23 16:45:05 -05:00
|
|
|
it 'returns a site text for a key that exists' do
|
2017-08-31 00:06:56 -04:00
|
|
|
get :show, params: { id: 'title' }, format: :json
|
2015-11-23 16:45:05 -05:00
|
|
|
expect(response).to be_success
|
|
|
|
|
|
|
|
json = ::JSON.parse(response.body)
|
|
|
|
expect(json).to be_present
|
|
|
|
|
|
|
|
site_text = json['site_text']
|
|
|
|
expect(site_text).to be_present
|
|
|
|
|
|
|
|
expect(site_text['id']).to eq('title')
|
|
|
|
expect(site_text['value']).to eq(I18n.t(:title))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns not found for missing keys' do
|
2017-08-31 00:06:56 -04:00
|
|
|
get :show, params: { id: 'made_up_no_key_exists' }, format: :json
|
2015-11-23 16:45:05 -05:00
|
|
|
expect(response).not_to be_success
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-15 04:08:23 -04:00
|
|
|
context '#update and #revert' do
|
|
|
|
after do
|
|
|
|
TranslationOverride.delete_all
|
|
|
|
I18n.reload!
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'failure' do
|
|
|
|
before do
|
2017-06-21 22:23:31 -04:00
|
|
|
I18n.backend.store_translations(:en, some_key: '%{first} %{second}')
|
2017-06-15 04:08:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the right error message' do
|
2017-08-31 00:06:56 -04:00
|
|
|
put :update, params: {
|
2017-11-20 03:10:06 -05:00
|
|
|
id: 'some_key', site_text: { value: 'hello %{key} %{omg}' }
|
2017-08-31 00:06:56 -04:00
|
|
|
}, format: :json
|
2017-06-15 04:08:23 -04:00
|
|
|
|
|
|
|
expect(response.status).to eq(422)
|
|
|
|
|
|
|
|
body = JSON.parse(response.body)
|
|
|
|
|
|
|
|
expect(body['message']).to eq(I18n.t(
|
2017-11-20 03:10:06 -05:00
|
|
|
'activerecord.errors.models.translation_overrides.attributes.value.invalid_interpolation_keys',
|
|
|
|
keys: 'key, omg'
|
2017-06-15 04:08:23 -04:00
|
|
|
))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-23 16:45:05 -05:00
|
|
|
it 'updates and reverts the key' do
|
|
|
|
orig_title = I18n.t(:title)
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
put :update, params: { id: 'title', site_text: { value: 'hello' } }, format: :json
|
2015-11-23 16:45:05 -05:00
|
|
|
expect(response).to be_success
|
|
|
|
|
|
|
|
json = ::JSON.parse(response.body)
|
|
|
|
expect(json).to be_present
|
|
|
|
|
|
|
|
site_text = json['site_text']
|
|
|
|
expect(site_text).to be_present
|
2013-04-04 12:59:44 -04:00
|
|
|
|
2015-11-23 16:45:05 -05:00
|
|
|
expect(site_text['id']).to eq('title')
|
|
|
|
expect(site_text['value']).to eq('hello')
|
|
|
|
|
|
|
|
# Revert
|
2017-08-31 00:06:56 -04:00
|
|
|
put :revert, params: { id: 'title' }, format: :json
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_success
|
2015-11-23 16:45:05 -05:00
|
|
|
|
|
|
|
json = ::JSON.parse(response.body)
|
|
|
|
expect(json).to be_present
|
|
|
|
|
|
|
|
site_text = json['site_text']
|
|
|
|
expect(site_text).to be_present
|
|
|
|
|
|
|
|
expect(site_text['id']).to eq('title')
|
|
|
|
expect(site_text['value']).to eq(orig_title)
|
2013-04-04 12:59:44 -04:00
|
|
|
end
|
|
|
|
|
2015-11-23 16:45:05 -05:00
|
|
|
it 'returns not found for missing keys' do
|
2017-08-31 00:06:56 -04:00
|
|
|
put :update, params: {
|
|
|
|
id: 'made_up_no_key_exists', site_text: { value: 'hello' }
|
|
|
|
}, format: :json
|
|
|
|
|
2015-11-23 16:45:05 -05:00
|
|
|
expect(response).not_to be_success
|
2013-04-04 12:59:44 -04:00
|
|
|
end
|
2015-12-18 08:31:04 -05:00
|
|
|
|
|
|
|
it 'logs the change' do
|
2017-06-15 04:08:23 -04:00
|
|
|
original_title = I18n.t(:title)
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
put :update, params: {
|
|
|
|
id: 'title', site_text: { value: 'yay' }
|
|
|
|
}, format: :json
|
2017-06-15 04:08:23 -04:00
|
|
|
|
|
|
|
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])
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
put :revert, params: { id: 'title' }, format: :json
|
2017-06-15 04:08:23 -04:00
|
|
|
|
|
|
|
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])
|
2015-12-18 08:31:04 -05:00
|
|
|
end
|
2018-02-07 10:57:08 -05:00
|
|
|
|
|
|
|
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 :update, params: { id: 'title', site_text: { value: ru_title } }, format: :json
|
|
|
|
put :update, params: { id: 'js.topic.read_more_MF', site_text: { value: ru_mf_text } }, format: :json
|
|
|
|
|
|
|
|
get :show, params: { id: 'title' }, format: :json
|
|
|
|
json = ::JSON.parse(response.body)
|
|
|
|
expect(json['site_text']['value']).to eq(ru_title)
|
|
|
|
|
|
|
|
get :show, params: { id: 'js.topic.read_more_MF' }, format: :json
|
|
|
|
json = ::JSON.parse(response.body)
|
|
|
|
expect(json['site_text']['value']).to eq(ru_mf_text)
|
|
|
|
|
|
|
|
SiteSetting.default_locale = :en
|
|
|
|
|
|
|
|
get :show, params: { id: 'title' }, format: :json
|
|
|
|
json = ::JSON.parse(response.body)
|
|
|
|
expect(json['site_text']['value']).to_not eq(ru_title)
|
|
|
|
|
|
|
|
get :show, params: { id: 'js.topic.read_more_MF' }, format: :json
|
|
|
|
json = ::JSON.parse(response.body)
|
|
|
|
expect(json['site_text']['value']).to_not eq(ru_mf_text)
|
|
|
|
end
|
2013-04-04 12:59:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|