2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe SiteSettings::DefaultsProvider do
|
2017-08-06 21:43:09 -04:00
|
|
|
let(:provider_local) { SiteSettings::LocalProcessProvider.new }
|
|
|
|
|
2018-05-25 00:29:11 -04:00
|
|
|
before { MessageBus.off }
|
|
|
|
|
|
|
|
after { MessageBus.on }
|
|
|
|
|
2017-08-06 21:43:09 -04:00
|
|
|
let(:settings) { new_settings(provider_local) }
|
|
|
|
|
|
|
|
describe ".db_all" do
|
|
|
|
it "can collect values from db" do
|
|
|
|
settings.provider.save("try_a", 1, SiteSetting.types[:integer])
|
|
|
|
settings.provider.save("try_b", 2, SiteSetting.types[:integer])
|
|
|
|
expect(settings.defaults.db_all.count).to eq 2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "expose default cache according to locale" do
|
2017-08-07 02:15:32 -04:00
|
|
|
before do
|
2017-08-06 21:43:09 -04:00
|
|
|
settings.setting(:test_override, "default", locale_default: { zh_CN: "cn" })
|
2018-09-11 07:20:42 -04:00
|
|
|
settings.setting(:test_boolean_override, true, locale_default: { zh_CN: false })
|
2017-08-06 21:43:09 -04:00
|
|
|
settings.setting(:test_default, "test", regex: '^\S+$')
|
|
|
|
settings.refresh!
|
|
|
|
end
|
|
|
|
|
|
|
|
describe ".all" do
|
2018-05-31 22:22:47 -04:00
|
|
|
it "returns all values according to locale" do
|
2018-09-11 07:20:42 -04:00
|
|
|
expect(settings.defaults.all).to eq(
|
|
|
|
test_override: "default",
|
|
|
|
test_default: "test",
|
|
|
|
test_boolean_override: true,
|
|
|
|
)
|
|
|
|
expect(settings.defaults.all("zh_CN")).to eq(
|
|
|
|
test_override: "cn",
|
|
|
|
test_default: "test",
|
|
|
|
test_boolean_override: false,
|
|
|
|
)
|
2017-08-06 21:43:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe ".get" do
|
|
|
|
it "returns the default value to a site setting" do
|
|
|
|
expect(settings.defaults.get(:test_override)).to eq "default"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "accepts a string as the parameters" do
|
|
|
|
expect(settings.defaults.get("test_override")).to eq "default"
|
|
|
|
end
|
|
|
|
|
2018-09-11 07:20:42 -04:00
|
|
|
it "returns the locale_default value if it exists" do
|
|
|
|
expect(settings.defaults.get(:test_override, :zh_CN)).to eq "cn"
|
|
|
|
expect(settings.defaults.get(:test_override, :de)).to eq "default"
|
|
|
|
expect(settings.defaults.get(:test_default, :zh_CN)).to eq "test"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the correct locale_default for boolean site settings" do
|
|
|
|
expect(settings.defaults.get(:test_boolean_override, :zh_CN)).to eq false
|
|
|
|
end
|
2017-08-06 21:43:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe ".set_regardless_of_locale" do
|
2022-04-06 17:17:20 -04:00
|
|
|
let(:val) { "env_overridden" }
|
2017-08-06 21:43:09 -04:00
|
|
|
|
|
|
|
it "sets the default value to a site setting regardless the locale" do
|
|
|
|
settings.defaults.set_regardless_of_locale(:test_override, val)
|
|
|
|
expect(settings.defaults.get(:test_override)).to eq val
|
2018-05-31 22:22:47 -04:00
|
|
|
expect(settings.defaults.get(:test_override, "zh_CN")).to eq val
|
2017-08-06 21:43:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "handles the string" do
|
|
|
|
settings.defaults.set_regardless_of_locale("test_override", val)
|
|
|
|
expect(settings.defaults.get(:test_override)).to eq val
|
|
|
|
end
|
|
|
|
|
|
|
|
it "converts the data type" do
|
|
|
|
settings.defaults.set_regardless_of_locale(:test_override, 1)
|
|
|
|
expect(settings.defaults.get(:test_override)).to eq "1"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises when the setting does not exists" do
|
|
|
|
expect { settings.defaults.set_regardless_of_locale(:not_exist, 1) }.to raise_error(
|
|
|
|
ArgumentError,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises when the value is not valid" do
|
|
|
|
expect {
|
|
|
|
settings.defaults.set_regardless_of_locale(:test_default, "regex will fail")
|
|
|
|
}.to raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe ".load_setting" do
|
2018-05-31 22:22:47 -04:00
|
|
|
it "adds a setting to the cache correctly" do
|
|
|
|
settings.defaults.load_setting("new_a", 1, zh_CN: 7)
|
2017-08-06 21:43:09 -04:00
|
|
|
expect(settings.defaults[:new_a]).to eq 1
|
2018-05-31 22:22:47 -04:00
|
|
|
expect(settings.defaults.get(:new_a, "zh_CN")).to eq 7
|
2017-08-06 21:43:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe ".has_setting?" do
|
|
|
|
before do
|
|
|
|
settings.setting(:r, 1)
|
|
|
|
settings.setting(:question?, 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns true when it's present in the cache" do
|
|
|
|
expect(settings.defaults.has_setting?(:r)).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it '"responds when the arg is string' do
|
|
|
|
expect(settings.defaults.has_setting?("r")).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it "default_locale always exists" do
|
2018-05-31 22:22:47 -04:00
|
|
|
expect(settings.defaults.has_setting?(:default_locale)).to be_truthy
|
2017-08-06 21:43:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns false when the key is not exist" do
|
|
|
|
expect(settings.defaults.has_setting?("no_key")).to be_falsey
|
|
|
|
end
|
|
|
|
|
|
|
|
it "checks name with question mark" do
|
|
|
|
expect(settings.defaults.has_setting?(:question)).to be_truthy
|
|
|
|
expect(settings.defaults.has_setting?("question")).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|