2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-05 08:04:23 +08:00
|
|
|
require "theme_settings_manager"
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe ThemeSettingsManager do
|
2019-05-07 11:12:20 +08:00
|
|
|
let!(:theme) { Fabricate(:theme) }
|
2018-03-05 08:04:23 +08:00
|
|
|
let(:theme_settings) do
|
|
|
|
yaml = File.read("#{Rails.root}/spec/fixtures/theme_settings/valid_settings.yaml")
|
|
|
|
theme.set_field(target: :settings, name: "yaml", value: yaml)
|
|
|
|
theme.save!
|
|
|
|
theme.settings
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_by_name(name)
|
|
|
|
theme_settings.find { |setting| setting.name == name }
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
describe "Enum" do
|
2018-03-05 08:04:23 +08:00
|
|
|
it "only accepts values from its choices" do
|
|
|
|
enum_setting = find_by_name(:enum_setting)
|
|
|
|
expect { enum_setting.value = "trust level 2" }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
expect { enum_setting.value = "trust level 0" }.not_to raise_error
|
|
|
|
|
|
|
|
enum_setting = find_by_name(:enum_setting_02)
|
|
|
|
expect { enum_setting.value = "10" }.not_to raise_error
|
|
|
|
|
|
|
|
enum_setting = find_by_name(:enum_setting_03)
|
|
|
|
expect { enum_setting.value = "10" }.not_to raise_error
|
|
|
|
expect { enum_setting.value = 1 }.not_to raise_error
|
|
|
|
expect { enum_setting.value = 15 }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
describe "Bool" do
|
2018-03-05 08:04:23 +08:00
|
|
|
it "is either true or false" do
|
|
|
|
bool_setting = find_by_name(:boolean_setting)
|
|
|
|
expect(bool_setting.value).to eq(true) # default
|
|
|
|
|
|
|
|
bool_setting.value = "true"
|
2018-12-21 01:13:05 +08:00
|
|
|
theme.reload
|
2018-03-05 08:04:23 +08:00
|
|
|
expect(bool_setting.value).to eq(true)
|
|
|
|
|
|
|
|
bool_setting.value = "falsse" # intentionally misspelled
|
2018-12-21 01:13:05 +08:00
|
|
|
theme.reload
|
2018-03-05 08:04:23 +08:00
|
|
|
expect(bool_setting.value).to eq(false)
|
|
|
|
|
|
|
|
bool_setting.value = true
|
2018-12-21 01:13:05 +08:00
|
|
|
theme.reload
|
2018-03-05 08:04:23 +08:00
|
|
|
expect(bool_setting.value).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
describe "Integer" do
|
2018-03-05 08:04:23 +08:00
|
|
|
it "is always an integer" do
|
|
|
|
int_setting = find_by_name(:integer_setting)
|
|
|
|
int_setting.value = 1.6
|
2018-12-21 01:13:05 +08:00
|
|
|
theme.reload
|
2018-03-05 08:04:23 +08:00
|
|
|
expect(int_setting.value).to eq(1)
|
|
|
|
|
|
|
|
int_setting.value = "4.3"
|
2018-12-21 01:13:05 +08:00
|
|
|
theme.reload
|
2018-03-05 08:04:23 +08:00
|
|
|
expect(int_setting.value).to eq(4)
|
|
|
|
|
|
|
|
int_setting.value = "10"
|
2018-12-21 01:13:05 +08:00
|
|
|
theme.reload
|
2018-03-05 08:04:23 +08:00
|
|
|
expect(int_setting.value).to eq(10)
|
|
|
|
|
|
|
|
int_setting.value = "text"
|
2018-12-21 01:13:05 +08:00
|
|
|
theme.reload
|
2018-03-05 08:04:23 +08:00
|
|
|
expect(int_setting.value).to eq(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can have min or max value" do
|
|
|
|
int_setting = find_by_name(:integer_setting_02)
|
|
|
|
expect { int_setting.value = 0 }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
expect { int_setting.value = 61 }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
|
|
|
|
int_setting.value = 60
|
2018-12-21 01:13:05 +08:00
|
|
|
theme.reload
|
2018-03-05 08:04:23 +08:00
|
|
|
expect(int_setting.value).to eq(60)
|
|
|
|
|
|
|
|
int_setting.value = 1
|
2018-12-21 01:13:05 +08:00
|
|
|
theme.reload
|
2018-03-05 08:04:23 +08:00
|
|
|
expect(int_setting.value).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
describe "Float" do
|
2018-03-05 08:04:23 +08:00
|
|
|
it "is always a float" do
|
|
|
|
float_setting = find_by_name(:float_setting)
|
|
|
|
float_setting.value = 1.615
|
2018-12-21 01:13:05 +08:00
|
|
|
theme.reload
|
2018-03-05 08:04:23 +08:00
|
|
|
expect(float_setting.value).to eq(1.615)
|
|
|
|
|
|
|
|
float_setting.value = "3.1415"
|
2018-12-21 01:13:05 +08:00
|
|
|
theme.reload
|
2018-03-05 08:04:23 +08:00
|
|
|
expect(float_setting.value).to eq(3.1415)
|
|
|
|
|
|
|
|
float_setting.value = 10
|
2018-12-21 01:13:05 +08:00
|
|
|
theme.reload
|
2018-03-05 08:04:23 +08:00
|
|
|
expect(float_setting.value).to eq(10)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can have min or max value" do
|
|
|
|
float_setting = find_by_name(:float_setting)
|
|
|
|
expect { float_setting.value = 1.4 }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
expect { float_setting.value = 10.01 }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
expect { float_setting.value = "text" }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
|
|
|
|
float_setting.value = 9.521
|
2018-12-21 01:13:05 +08:00
|
|
|
theme.reload
|
2018-03-05 08:04:23 +08:00
|
|
|
expect(float_setting.value).to eq(9.521)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
describe "String" do
|
2018-03-05 08:04:23 +08:00
|
|
|
it "can have min or max length" do
|
|
|
|
string_setting = find_by_name(:string_setting_02)
|
|
|
|
expect { string_setting.value = "a" }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
|
|
|
|
string_setting.value = "ab"
|
2018-12-21 01:13:05 +08:00
|
|
|
theme.reload
|
2018-03-05 08:04:23 +08:00
|
|
|
expect(string_setting.value).to eq("ab")
|
|
|
|
|
|
|
|
string_setting.value = "ab" * 10
|
2018-12-21 01:13:05 +08:00
|
|
|
theme.reload
|
2018-03-05 08:04:23 +08:00
|
|
|
expect(string_setting.value).to eq("ab" * 10)
|
|
|
|
|
|
|
|
expect { string_setting.value = ("a" * 21) }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
2019-02-05 22:14:53 +08:00
|
|
|
|
|
|
|
it "can be a textarea" do
|
|
|
|
expect(find_by_name(:string_setting_02).textarea).to eq(false)
|
|
|
|
expect(find_by_name(:string_setting_03).textarea).to eq(true)
|
|
|
|
end
|
2021-03-11 09:15:04 +08:00
|
|
|
|
|
|
|
it "supports json schema" do
|
|
|
|
expect(find_by_name(:string_setting_03).json_schema).to eq(false)
|
|
|
|
expect(find_by_name(:invalid_json_schema_setting).json_schema).to eq(false)
|
|
|
|
expect(find_by_name(:valid_json_schema_setting).json_schema).to be_truthy
|
|
|
|
end
|
2018-03-05 08:04:23 +08:00
|
|
|
end
|
2018-08-04 04:41:37 +08:00
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
describe "List" do
|
2018-08-04 04:41:37 +08:00
|
|
|
it "can have a list type" do
|
|
|
|
list_setting = find_by_name(:compact_list_setting)
|
|
|
|
expect(list_setting.list_type).to eq("compact")
|
|
|
|
end
|
|
|
|
end
|
2021-09-16 10:28:53 +08:00
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
describe "Upload" do
|
2021-09-16 10:28:53 +08:00
|
|
|
let!(:upload) { Fabricate(:upload) }
|
|
|
|
|
|
|
|
it "saves the upload id" do
|
|
|
|
upload_setting = find_by_name(:upload_setting)
|
|
|
|
upload_setting.value = upload.url
|
|
|
|
theme.reload
|
|
|
|
|
|
|
|
expect(
|
|
|
|
ThemeSetting.exists?(theme_id: theme.id, name: "upload_setting", value: upload.id.to_s),
|
|
|
|
).to be_truthy
|
|
|
|
end
|
|
|
|
|
2023-03-06 16:41:47 +08:00
|
|
|
describe "#value" do
|
|
|
|
context "when it's changed to a custom upload" do
|
|
|
|
it "returns CDN URL" do
|
|
|
|
upload_setting = find_by_name(:upload_setting)
|
|
|
|
upload_setting.value = upload.url
|
|
|
|
theme.reload
|
|
|
|
|
|
|
|
expect(upload_setting.value).to eq(Discourse.store.cdn_url(upload.url))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when there's a default upload" do
|
|
|
|
it "returns CDN URL" do
|
|
|
|
theme.set_field(
|
|
|
|
target: :common,
|
|
|
|
name: "default-upload",
|
|
|
|
type: :theme_upload_var,
|
|
|
|
upload_id: upload.id,
|
|
|
|
)
|
|
|
|
theme.save!
|
|
|
|
upload_setting = find_by_name(:upload_setting)
|
|
|
|
expect(upload_setting.value).to eq(Discourse.store.cdn_url(upload.url))
|
|
|
|
end
|
|
|
|
end
|
2021-09-16 10:28:53 +08:00
|
|
|
end
|
|
|
|
end
|
2018-03-05 08:04:23 +08:00
|
|
|
end
|