2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-03-03 23:54:20 +08:00
discourse/spec/models/form_template_spec.rb
Natalie Tay 9e99066b07
DEV: Expand top_tags, topic.tags, etc, to return an array of tag objects instead of tag names (#36678)
Currently in several endpoints, we return an array of strings for tags.
Our goal with this PR is to expand array tag name strings to an array of
tag objects.

#### before: Tags were returned as string arrays
```
{ "tags": ["support", "bug-report"] }
```

#### after: Tags are returned as object arrays
```
{ "tags": [{"id": 12, "name": "support", "slug": "support"}, {"id": 13, "name": "bug-report", "slug": "bug-report"}] }
```

This allows us to start referencing tags by their ids, and return more
information for a tag for future features.

This commit involves updating several areas:
- topic lists (/latest.json, /top.json, /c/:category/:id.json, etc, for
`top_tags`)
- tag chooser components (`MiniTagChooser`, `TagDrop`, etc)
- topic view (/t/:id.json)
- tag groups (/tag_groups.json, tags, parent_tag)
- category settings
- staff action logs
- synonyms
- ...

APIs that reference tags based on their names will still be supported
with a deprecation warning. Moving on, we will reference them using
their tag ids.
2026-02-02 10:03:02 +08:00

128 lines
4.5 KiB
Ruby

# frozen_string_literal: true
RSpec.describe FormTemplate, type: :model do
it "can't have duplicate names" do
Fabricate(:form_template, name: "Bug Report", template: "- type: input\n id: name")
t = Fabricate.build(:form_template, name: "Bug Report", template: "- type: input\n id: name")
expect(t.save).to eq(false)
t = Fabricate.build(:form_template, name: "Bug Report", template: "- type: input\n id: name")
expect(t.save).to eq(false)
expect(t.errors.full_messages.first).to include(I18n.t("errors.messages.taken"))
expect(described_class.count).to eq(1)
end
context "when length validations set in SiteSetting" do
before do
SiteSetting.max_form_template_content_length = 50
SiteSetting.max_form_template_title_length = 5
end
it "can't exceed max title length" do
t = Fabricate.build(:form_template, name: "Bug Report", template: "- type: input\n id: name")
expect(t.save).to eq(false)
expect(t.errors.full_messages.first).to include(
"Name #{I18n.t("errors.messages.too_long", count: SiteSetting.max_form_template_title_length)}",
)
end
it "can't exceed max content length" do
t =
Fabricate.build(
:form_template,
name: "Bug",
template: "- type: input\n id: name-that-is-really-long-to-make-the-template-longer",
)
expect(t.save).to eq(false)
expect(t.errors.full_messages.first).to include(
"Template #{I18n.t("errors.messages.too_long", count: SiteSetting.max_form_template_content_length)}",
)
end
it "should update validation limits when the site setting has been changed" do
SiteSetting.max_form_template_content_length = 100
SiteSetting.max_form_template_title_length = 100
t =
Fabricate.build(
:form_template,
name: "Bug Report",
template: "- type: input\n id: name-that-is-really-long-to-make-the-template-longer",
)
expect(t.save).to eq(true)
end
end
it "can't have an invalid yaml template" do
template = "- type: checkbox\nattributes; bad"
t = Fabricate.build(:form_template, name: "Feature Request", template: template)
expect(t.save).to eq(false)
expect(t.errors.full_messages.first).to include(I18n.t("form_templates.errors.invalid_yaml"))
end
it "must have a supported type" do
template = "- type: fancy\n id: something"
t = Fabricate.build(:form_template, name: "Fancy Template", template: template)
expect(t.save).to eq(false)
expect(t.errors.full_messages.first).to include(
I18n.t(
"form_templates.errors.invalid_type",
type: "fancy",
valid_types: FormTemplateYamlValidator::ALLOWED_TYPES.join(", "),
),
)
end
it "must have a type property" do
template = "- hello: world\n id: something"
t = Fabricate.build(:form_template, name: "Basic Template", template: template)
expect(t.save).to eq(false)
expect(t.errors.full_messages.first).to include(I18n.t("form_templates.errors.missing_type"))
end
it "must have a id property" do
template = "- type: checkbox"
t = Fabricate.build(:form_template, name: "Basic Template", template: template)
expect(t.save).to eq(false)
expect(t.errors.full_messages.first).to include(I18n.t("form_templates.errors.missing_id"))
end
describe "#process!" do
fab!(:admin)
fab!(:tag1, :tag)
fab!(:tag2, :tag)
fab!(:tag3, :tag)
fab!(:tag4, :tag)
fab!(:tag5, :tag)
fab!(:tag_group) { Fabricate(:tag_group, tags: [tag1, tag2, tag3]) }
fab!(:tag_group2) { Fabricate(:tag_group, tags: [tag4, tag5]) }
it "automatically adds tags choices to the template" do
template = <<~YAML
- type: tag-chooser
id: tag-chooser
tag_group: "#{tag_group.name}"
- type: tag-chooser
id: tag-chooser2
tag_group: "#{tag_group2.name}"
attributes:
multiple: true
YAML
form_template = Fabricate(:form_template, template: template)
guardian = Guardian.new(admin)
form_template.process!(guardian)
g1, g2 = YAML.safe_load(form_template.template)
expect(g1["choices"]).to contain_exactly(
{ "id" => tag1.id, "name" => tag1.name },
{ "id" => tag2.id, "name" => tag2.name },
{ "id" => tag3.id, "name" => tag3.name },
)
expect(g2["attributes"]["tag_group"]).to eq(tag_group2.name)
expect(g2["attributes"]["multiple"]).to eq(true)
end
end
end