2020-11-08 04:24:20 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
describe CustomWizard::CustomField do
|
2021-09-07 14:06:13 +02:00
|
|
|
let(:custom_field_json) { get_wizard_fixture("custom_field/custom_fields") }
|
2024-10-16 13:52:03 +02:00
|
|
|
let(:custom_field_subscription_json) do
|
|
|
|
get_wizard_fixture("custom_field/subscription_custom_fields")
|
2020-12-04 08:05:56 +01:00
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2024-10-16 13:52:03 +02:00
|
|
|
before { CustomWizard::CustomField.invalidate_cache }
|
|
|
|
|
2020-11-08 04:24:20 +01:00
|
|
|
it "saves custom field records" do
|
2024-10-16 13:52:03 +02:00
|
|
|
custom_field_json["custom_fields"].each do |field_json|
|
2020-11-08 04:24:20 +01:00
|
|
|
custom_field = CustomWizard::CustomField.new(nil, field_json)
|
|
|
|
expect(custom_field.save).to eq(true)
|
|
|
|
expect(
|
2024-10-16 13:52:03 +02:00
|
|
|
PluginStoreRow.where(
|
|
|
|
"
|
2020-11-08 04:24:20 +01:00
|
|
|
plugin_name = '#{CustomWizard::CustomField::NAMESPACE}' AND
|
|
|
|
key = '#{custom_field.name}' AND
|
2024-10-16 13:52:03 +02:00
|
|
|
value::jsonb = '#{field_json.except("name").to_json}'::jsonb
|
|
|
|
",
|
|
|
|
).exists?,
|
2020-11-08 04:24:20 +01:00
|
|
|
).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-08 04:24:20 +01:00
|
|
|
it "updates existing custom field records" do
|
2024-10-16 13:52:03 +02:00
|
|
|
custom_field_json["custom_fields"].each do |field_json|
|
2020-11-08 04:24:20 +01:00
|
|
|
CustomWizard::CustomField.new(nil, field_json).save
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2024-10-16 13:52:03 +02:00
|
|
|
updated_field_json = custom_field_json["custom_fields"][0]
|
|
|
|
updated_field_json["serializers"] = ["topic_view"]
|
2020-11-10 01:56:11 +01:00
|
|
|
existing_field = CustomWizard::CustomField.find_by_name(updated_field_json["name"])
|
2020-11-08 04:24:20 +01:00
|
|
|
updated_field = CustomWizard::CustomField.new(existing_field.id, updated_field_json)
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-08 04:24:20 +01:00
|
|
|
expect(updated_field.save).to eq(true)
|
|
|
|
expect(
|
2024-10-16 13:52:03 +02:00
|
|
|
PluginStoreRow.where(
|
|
|
|
"
|
2020-11-08 04:24:20 +01:00
|
|
|
plugin_name = '#{CustomWizard::CustomField::NAMESPACE}' AND
|
|
|
|
key = '#{updated_field.name}' AND
|
2024-10-16 13:52:03 +02:00
|
|
|
value::jsonb = '#{updated_field_json.except("name").to_json}'::jsonb
|
|
|
|
",
|
|
|
|
).exists?,
|
2020-11-08 04:24:20 +01:00
|
|
|
).to eq(true)
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-08 04:24:20 +01:00
|
|
|
context "validation" do
|
2021-06-08 13:39:49 +02:00
|
|
|
it "does not save without required attributes" do
|
2024-10-16 13:52:03 +02:00
|
|
|
invalid_field_json = custom_field_json["custom_fields"].first
|
|
|
|
invalid_field_json["klass"] = nil
|
2021-06-08 13:39:49 +02:00
|
|
|
|
|
|
|
custom_field = CustomWizard::CustomField.new(nil, invalid_field_json)
|
|
|
|
expect(custom_field.save).to eq(false)
|
|
|
|
expect(custom_field.valid?).to eq(false)
|
|
|
|
expect(custom_field.errors.full_messages.first).to eq(
|
2024-10-16 13:52:03 +02:00
|
|
|
I18n.t("wizard.custom_field.error.required_attribute", attr: "klass"),
|
2021-06-08 13:39:49 +02:00
|
|
|
)
|
|
|
|
expect(
|
|
|
|
PluginStoreRow.where(
|
|
|
|
plugin_name: CustomWizard::CustomField::NAMESPACE,
|
2024-10-16 13:52:03 +02:00
|
|
|
key: custom_field.name,
|
|
|
|
).exists?,
|
2021-06-08 13:39:49 +02:00
|
|
|
).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does save without optional attributes" do
|
2024-10-16 13:52:03 +02:00
|
|
|
field_json = custom_field_json["custom_fields"].first
|
|
|
|
field_json["serializers"] = nil
|
2021-06-08 13:39:49 +02:00
|
|
|
|
|
|
|
custom_field = CustomWizard::CustomField.new(nil, field_json)
|
|
|
|
expect(custom_field.save).to eq(true)
|
|
|
|
expect(custom_field.valid?).to eq(true)
|
|
|
|
expect(
|
2024-10-16 13:52:03 +02:00
|
|
|
PluginStoreRow.where(
|
|
|
|
"
|
2021-06-08 13:39:49 +02:00
|
|
|
plugin_name = '#{CustomWizard::CustomField::NAMESPACE}' AND
|
|
|
|
key = '#{custom_field.name}' AND
|
2024-10-16 13:52:03 +02:00
|
|
|
value::jsonb = '#{field_json.except("name").to_json}'::jsonb
|
|
|
|
",
|
|
|
|
).exists?,
|
2021-06-08 13:39:49 +02:00
|
|
|
).to eq(true)
|
|
|
|
end
|
|
|
|
|
2020-11-08 04:24:20 +01:00
|
|
|
it "does not save with an unsupported class" do
|
2024-10-16 13:52:03 +02:00
|
|
|
invalid_field_json = custom_field_json["custom_fields"].first
|
|
|
|
invalid_field_json["klass"] = "user"
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-08 04:24:20 +01:00
|
|
|
custom_field = CustomWizard::CustomField.new(nil, invalid_field_json)
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-08 04:24:20 +01:00
|
|
|
expect(custom_field.save).to eq(false)
|
|
|
|
expect(custom_field.valid?).to eq(false)
|
|
|
|
expect(custom_field.errors.full_messages.first).to eq(
|
2024-10-16 13:52:03 +02:00
|
|
|
I18n.t("wizard.custom_field.error.unsupported_class", class: "user"),
|
2020-11-08 04:24:20 +01:00
|
|
|
)
|
|
|
|
expect(
|
|
|
|
PluginStoreRow.where(
|
|
|
|
plugin_name: CustomWizard::CustomField::NAMESPACE,
|
2024-10-16 13:52:03 +02:00
|
|
|
key: custom_field.name,
|
|
|
|
).exists?,
|
2020-11-08 04:24:20 +01:00
|
|
|
).to eq(false)
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-08 04:24:20 +01:00
|
|
|
it "does not save with an unsupported serializer" do
|
2024-10-16 13:52:03 +02:00
|
|
|
invalid_field_json = custom_field_json["custom_fields"].first
|
|
|
|
invalid_field_json["klass"] = "post"
|
|
|
|
invalid_field_json["serializers"] = %w[post post_revision]
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-08 04:24:20 +01:00
|
|
|
custom_field = CustomWizard::CustomField.new(nil, invalid_field_json)
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-08 04:24:20 +01:00
|
|
|
expect(custom_field.save).to eq(false)
|
|
|
|
expect(custom_field.valid?).to eq(false)
|
|
|
|
expect(custom_field.errors.full_messages.first).to eq(
|
2024-10-16 13:52:03 +02:00
|
|
|
I18n.t(
|
|
|
|
"wizard.custom_field.error.unsupported_serializers",
|
2021-09-07 14:06:13 +02:00
|
|
|
class: "post",
|
2024-10-16 13:52:03 +02:00
|
|
|
serializers: "post_revision",
|
|
|
|
),
|
2020-11-08 04:24:20 +01:00
|
|
|
)
|
|
|
|
expect(
|
|
|
|
PluginStoreRow.where(
|
|
|
|
plugin_name: CustomWizard::CustomField::NAMESPACE,
|
2024-10-16 13:52:03 +02:00
|
|
|
key: custom_field.name,
|
|
|
|
).exists?,
|
2020-11-08 04:24:20 +01:00
|
|
|
).to eq(false)
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-08 04:24:20 +01:00
|
|
|
it "does not save with an unsupported type" do
|
2024-10-16 13:52:03 +02:00
|
|
|
invalid_field_json = custom_field_json["custom_fields"].first
|
|
|
|
invalid_field_json["type"] = "bigint"
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-08 04:24:20 +01:00
|
|
|
custom_field = CustomWizard::CustomField.new(nil, invalid_field_json)
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-08 04:24:20 +01:00
|
|
|
expect(custom_field.save).to eq(false)
|
|
|
|
expect(custom_field.valid?).to eq(false)
|
|
|
|
expect(custom_field.errors.full_messages.first).to eq(
|
2024-10-16 13:52:03 +02:00
|
|
|
I18n.t("wizard.custom_field.error.unsupported_type", type: "bigint"),
|
2020-11-08 04:24:20 +01:00
|
|
|
)
|
|
|
|
expect(
|
|
|
|
PluginStoreRow.where(
|
|
|
|
plugin_name: CustomWizard::CustomField::NAMESPACE,
|
2024-10-16 13:52:03 +02:00
|
|
|
key: custom_field.name,
|
|
|
|
).exists?,
|
2020-11-08 04:24:20 +01:00
|
|
|
).to eq(false)
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-08 04:24:20 +01:00
|
|
|
it "does not save with a short field name" do
|
2024-10-16 13:52:03 +02:00
|
|
|
invalid_field_json = custom_field_json["custom_fields"].first
|
|
|
|
invalid_field_json["name"] = "cf"
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-08 04:24:20 +01:00
|
|
|
custom_field = CustomWizard::CustomField.new(nil, invalid_field_json)
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-08 04:24:20 +01:00
|
|
|
expect(custom_field.save).to eq(false)
|
|
|
|
expect(custom_field.valid?).to eq(false)
|
|
|
|
expect(custom_field.errors.full_messages.first).to eq(
|
2024-10-16 13:52:03 +02:00
|
|
|
I18n.t("wizard.custom_field.error.name_too_short", name: "cf"),
|
2020-11-08 04:24:20 +01:00
|
|
|
)
|
|
|
|
expect(
|
|
|
|
PluginStoreRow.where(
|
|
|
|
plugin_name: CustomWizard::CustomField::NAMESPACE,
|
2024-10-16 13:52:03 +02:00
|
|
|
key: custom_field.name,
|
|
|
|
).exists?,
|
2020-11-08 04:24:20 +01:00
|
|
|
).to eq(false)
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-08 04:24:20 +01:00
|
|
|
it "does not save with an existing name if new" do
|
2024-10-16 13:52:03 +02:00
|
|
|
custom_field_json["custom_fields"].each do |field_json|
|
2020-11-08 04:24:20 +01:00
|
|
|
CustomWizard::CustomField.new(nil, field_json).save
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2024-10-16 13:52:03 +02:00
|
|
|
first_field_json = custom_field_json["custom_fields"][0]
|
2020-11-08 04:24:20 +01:00
|
|
|
custom_field = CustomWizard::CustomField.new(nil, first_field_json)
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-08 04:24:20 +01:00
|
|
|
expect(custom_field.save).to eq(false)
|
|
|
|
expect(custom_field.valid?).to eq(false)
|
|
|
|
expect(custom_field.errors.full_messages.first).to eq(
|
2024-10-16 13:52:03 +02:00
|
|
|
I18n.t("wizard.custom_field.error.name_already_taken", name: "topic_field_1"),
|
2020-11-08 04:24:20 +01:00
|
|
|
)
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-08 04:24:20 +01:00
|
|
|
it "does not save with an invalid name" do
|
2024-10-16 13:52:03 +02:00
|
|
|
invalid_field_json = custom_field_json["custom_fields"].first
|
|
|
|
invalid_field_json["name"] = ["invalid_name"]
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-08 04:24:20 +01:00
|
|
|
custom_field = CustomWizard::CustomField.new(nil, invalid_field_json)
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-08 04:24:20 +01:00
|
|
|
expect(custom_field.save).to eq(false)
|
|
|
|
expect(custom_field.valid?).to eq(false)
|
|
|
|
expect(custom_field.errors.full_messages.first).to eq(
|
2024-10-16 13:52:03 +02:00
|
|
|
I18n.t("wizard.custom_field.error.name_invalid", name: ["invalid_name"]),
|
2020-11-08 04:24:20 +01:00
|
|
|
)
|
|
|
|
expect(
|
|
|
|
PluginStoreRow.where(
|
|
|
|
plugin_name: CustomWizard::CustomField::NAMESPACE,
|
2024-10-16 13:52:03 +02:00
|
|
|
key: custom_field.name,
|
|
|
|
).exists?,
|
2020-11-08 04:24:20 +01:00
|
|
|
).to eq(false)
|
|
|
|
end
|
2021-09-07 14:11:50 +02:00
|
|
|
|
2021-09-24 11:58:42 +02:00
|
|
|
it "does not save subscription field types without a subscription" do
|
2024-10-16 13:52:03 +02:00
|
|
|
subscription_field_json = custom_field_subscription_json["custom_fields"].first
|
2021-09-24 11:58:42 +02:00
|
|
|
custom_field = CustomWizard::CustomField.new(nil, subscription_field_json)
|
2021-09-07 14:06:13 +02:00
|
|
|
|
|
|
|
expect(custom_field.save).to eq(false)
|
|
|
|
expect(custom_field.valid?).to eq(false)
|
|
|
|
expect(custom_field.errors.full_messages.first).to eq(
|
2024-10-16 13:52:03 +02:00
|
|
|
I18n.t("wizard.custom_field.error.subscription_type", type: "json"),
|
2021-09-07 14:06:13 +02:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-09-24 11:58:42 +02:00
|
|
|
it "does not save subscription field classes without a subscription" do
|
2024-10-16 13:52:03 +02:00
|
|
|
subscription_field_json = custom_field_subscription_json["custom_fields"].second
|
2021-09-24 11:58:42 +02:00
|
|
|
custom_field = CustomWizard::CustomField.new(nil, subscription_field_json)
|
2021-09-07 14:06:13 +02:00
|
|
|
|
|
|
|
expect(custom_field.save).to eq(false)
|
|
|
|
expect(custom_field.valid?).to eq(false)
|
|
|
|
expect(custom_field.errors.full_messages.first).to eq(
|
2024-10-16 13:52:03 +02:00
|
|
|
I18n.t("wizard.custom_field.error.subscription_type", type: "category"),
|
2021-09-07 14:06:13 +02:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-09-24 11:58:42 +02:00
|
|
|
context "with a subscription" do
|
2024-10-16 13:52:03 +02:00
|
|
|
before { enable_subscription("business") }
|
2021-09-07 14:06:13 +02:00
|
|
|
|
2021-09-24 11:58:42 +02:00
|
|
|
it "saves subscription field types" do
|
2024-10-16 13:52:03 +02:00
|
|
|
subscription_field_json = custom_field_subscription_json["custom_fields"].first
|
2021-09-24 11:58:42 +02:00
|
|
|
custom_field = CustomWizard::CustomField.new(nil, subscription_field_json)
|
2021-09-07 14:06:13 +02:00
|
|
|
|
|
|
|
expect(custom_field.save).to eq(true)
|
|
|
|
expect(custom_field.valid?).to eq(true)
|
|
|
|
end
|
|
|
|
|
2021-09-24 11:58:42 +02:00
|
|
|
it "saves subscription field classes" do
|
2024-10-16 13:52:03 +02:00
|
|
|
subscription_field_json = custom_field_subscription_json["custom_fields"].second
|
2021-09-24 11:58:42 +02:00
|
|
|
custom_field = CustomWizard::CustomField.new(nil, subscription_field_json)
|
2021-09-07 14:06:13 +02:00
|
|
|
|
|
|
|
expect(custom_field.save).to eq(true)
|
|
|
|
expect(custom_field.valid?).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
2020-11-08 04:24:20 +01:00
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-08 04:24:20 +01:00
|
|
|
context "lists" do
|
|
|
|
before do
|
2024-10-16 13:52:03 +02:00
|
|
|
custom_field_json["custom_fields"].each do |field_json|
|
2020-11-08 04:24:20 +01:00
|
|
|
CustomWizard::CustomField.new(nil, field_json).save
|
|
|
|
end
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2021-09-07 14:06:13 +02:00
|
|
|
it "saved custom field records" do
|
|
|
|
expect(CustomWizard::CustomField.list.length).to eq(2)
|
2020-11-08 04:24:20 +01:00
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2021-09-07 14:06:13 +02:00
|
|
|
it "saved custom field records by attribute value" do
|
2024-10-16 13:52:03 +02:00
|
|
|
expect(CustomWizard::CustomField.list_by(:klass, "topic").length).to eq(1)
|
2020-11-08 04:24:20 +01:00
|
|
|
end
|
2021-06-08 13:39:49 +02:00
|
|
|
|
2021-09-07 14:06:13 +02:00
|
|
|
it "saved custom field records by optional values" do
|
2024-10-16 13:52:03 +02:00
|
|
|
field_json = custom_field_json["custom_fields"].first
|
|
|
|
field_json["serializers"] = nil
|
2021-06-08 13:39:49 +02:00
|
|
|
|
|
|
|
custom_field = CustomWizard::CustomField.new(nil, field_json)
|
2024-10-16 13:52:03 +02:00
|
|
|
expect(CustomWizard::CustomField.list_by(:serializers, ["post"]).length).to eq(0)
|
2021-06-08 13:39:49 +02:00
|
|
|
end
|
|
|
|
|
2023-11-25 11:35:21 +01:00
|
|
|
it "custom field records added by other plugins " do
|
2022-05-31 13:16:28 +02:00
|
|
|
expect(CustomWizard::CustomField.external_list.length).to be > 2
|
2021-06-08 13:39:49 +02:00
|
|
|
end
|
|
|
|
|
2023-11-25 11:35:21 +01:00
|
|
|
it "all custom field records" do
|
2022-05-31 13:16:28 +02:00
|
|
|
expect(CustomWizard::CustomField.full_list.length).to be > 2
|
2021-06-08 13:39:49 +02:00
|
|
|
end
|
2020-11-08 04:24:20 +01:00
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-12-01 08:20:02 +01:00
|
|
|
it "is enabled if there are custom fields" do
|
2024-10-16 13:52:03 +02:00
|
|
|
custom_field_json["custom_fields"].each do |field_json|
|
2020-12-01 08:20:02 +01:00
|
|
|
CustomWizard::CustomField.new(nil, field_json).save
|
|
|
|
end
|
|
|
|
expect(CustomWizard::CustomField.enabled?).to eq(true)
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-12-01 08:20:02 +01:00
|
|
|
it "is not enabled if there are no custom fields" do
|
|
|
|
expect(CustomWizard::CustomField.enabled?).to eq(false)
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
end
|