2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-11-07 04:47:26 +08:00
|
|
|
require "site_settings/yaml_loader"
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe SiteSettings::YamlLoader do
|
2013-11-07 04:47:26 +08:00
|
|
|
class Receiver
|
|
|
|
attr_reader :settings, :client_settings, :categories
|
|
|
|
|
|
|
|
def load_yaml(file_arg)
|
|
|
|
SiteSettings::YamlLoader
|
|
|
|
.new(file_arg)
|
|
|
|
.load { |category, name, default, opts| setting(category, name, default, opts) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def setting(category, name, default = nil, opts = {})
|
|
|
|
@settings ||= []
|
2017-08-07 09:43:09 +08:00
|
|
|
@client_settings ||= []
|
2013-11-07 04:47:26 +08:00
|
|
|
@settings << name
|
|
|
|
@categories ||= []
|
|
|
|
@categories << category
|
|
|
|
@categories.uniq!
|
2017-08-07 09:43:09 +08:00
|
|
|
@client_settings << name if opts.has_key?(:client)
|
2013-11-07 04:47:26 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
let!(:receiver) { Receiver.new }
|
|
|
|
let(:simple) { "#{Rails.root}/spec/fixtures/site_settings/simple.yml" }
|
|
|
|
let(:client) { "#{Rails.root}/spec/fixtures/site_settings/client.yml" }
|
|
|
|
let(:enum) { "#{Rails.root}/spec/fixtures/site_settings/enum.yml" }
|
|
|
|
let(:enum_client) { "#{Rails.root}/spec/fixtures/site_settings/enum_client.yml" }
|
2017-08-07 09:43:09 +08:00
|
|
|
let(:deprecated_env) { "#{Rails.root}/spec/fixtures/site_settings/deprecated_env.yml" }
|
|
|
|
let(:deprecated_hidden) { "#{Rails.root}/spec/fixtures/site_settings/deprecated_hidden.yml" }
|
|
|
|
let(:locale_default) { "#{Rails.root}/spec/fixtures/site_settings/locale_default.yml" }
|
2017-08-04 02:19:02 +08:00
|
|
|
let(:nil_default) { "#{Rails.root}/spec/fixtures/site_settings/nil_default.yml" }
|
2013-11-07 04:47:26 +08:00
|
|
|
|
|
|
|
it "loads simple settings" do
|
|
|
|
receiver.expects(:setting).with("category1", "title", "My Site", {}).once
|
|
|
|
receiver.expects(:setting).with("category1", "contact_email", "webmaster@example.com", {}).once
|
2015-11-25 03:28:42 +08:00
|
|
|
receiver.expects(:setting).with("category2", "editing_grace_period", true, {}).once
|
2013-11-07 04:47:26 +08:00
|
|
|
receiver.expects(:setting).with("category3", "reply_by_email_address", "", {}).once
|
|
|
|
receiver.load_yaml(simple)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can take a File argument" do
|
|
|
|
receiver.expects(:setting).at_least_once
|
|
|
|
receiver.load_yaml(File.new(simple))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "maintains order of categories" do
|
|
|
|
receiver.load_yaml(simple)
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(receiver.categories).to eq(%w[category1 category2 category3])
|
2013-11-07 04:47:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "can load client settings" do
|
2022-11-02 17:47:59 +08:00
|
|
|
receiver.expects(:setting).with("category1", "title", "Discourse", { client: true })
|
|
|
|
receiver.expects(:setting).with("category2", "tos_url", "", { client: true })
|
|
|
|
receiver.expects(:setting).with("category2", "must_approve_users", false, { client: true })
|
2013-11-07 04:47:26 +08:00
|
|
|
receiver.load_yaml(client)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can load enum settings" do
|
2022-11-02 17:47:59 +08:00
|
|
|
receiver.expects(:setting).with(
|
|
|
|
"email",
|
|
|
|
"default_email_digest_frequency",
|
|
|
|
7,
|
|
|
|
{ enum: "DigestEmailSiteSetting" },
|
|
|
|
)
|
2013-11-07 04:47:26 +08:00
|
|
|
receiver.load_yaml(enum)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can load enum client settings" do
|
2017-08-07 09:43:09 +08:00
|
|
|
receiver
|
|
|
|
.expects(:setting)
|
|
|
|
.with do |category, name, default, opts|
|
|
|
|
category == ("basics") && name == ("default_locale") && default == ("en") &&
|
|
|
|
opts[:enum] == ("LocaleSiteSetting") && opts[:client] == true
|
2013-11-07 04:47:26 +08:00
|
|
|
end
|
|
|
|
receiver.load_yaml(enum_client)
|
|
|
|
end
|
|
|
|
|
2017-08-04 02:19:02 +08:00
|
|
|
it "raises invalid parameter when default value is not present" do
|
|
|
|
expect { receiver.load_yaml(nil_default) }.to raise_error(StandardError)
|
|
|
|
end
|
|
|
|
|
2017-08-07 09:43:09 +08:00
|
|
|
it "can load settings with locale default" do
|
2022-11-02 17:47:59 +08:00
|
|
|
receiver.expects(:setting).with(
|
|
|
|
"search",
|
|
|
|
"min_search_term_length",
|
|
|
|
3,
|
|
|
|
{ min: 2, client: true, locale_default: { zh_CN: 2, zh_TW: 2 } },
|
|
|
|
)
|
2017-08-07 09:43:09 +08:00
|
|
|
receiver.load_yaml(locale_default)
|
2013-11-07 04:47:26 +08:00
|
|
|
end
|
|
|
|
end
|