Skip to content

A platform for community discussion. Free, open, simple.

FIX: include new tags in validation if user can create one. (#14744)

Vinoth Kannana2851b5d4cdf
diff --git a/lib/topic_creator.rb b/lib/topic_creator.rb
index a250ef69fbf..1211c84fe78 100644
--- a/lib/topic_creator.rb
+++ b/lib/topic_creator.rb
@@ -26,11 +26,13 @@ class TopicCreator
category = find_category
if category.present? && guardian.can_tag?(topic)
- tags = @opts[:tags].present? ? Tag.where(name: @opts[:tags]) : (@opts[:tags] || [])
+ tags = @opts[:tags].presence || []
+ existing_tags = tags.present? ? Tag.where(name: tags) : []
+ valid_tags = guardian.can_create_tag? ? tags : existing_tags
# both add to topic.errors
- DiscourseTagging.validate_min_required_tags_for_category(guardian, topic, category, tags)
- DiscourseTagging.validate_required_tags_from_group(guardian, topic, category, tags)
+ DiscourseTagging.validate_min_required_tags_for_category(guardian, topic, category, valid_tags)
+ DiscourseTagging.validate_required_tags_from_group(guardian, topic, category, existing_tags)
end
DiscourseEvent.trigger(:after_validate_topic, topic, self)
diff --git a/spec/components/topic_creator_spec.rb b/spec/components/topic_creator_spec.rb
index a624e7d5ef8..433462a4c21 100644
--- a/spec/components/topic_creator_spec.rb
+++ b/spec/components/topic_creator_spec.rb
@@ -140,6 +140,12 @@ describe TopicCreator do
expect(topic.tags.length).to eq(2)
end
+ it "minimum_required_tags is satisfying for new tags if user can create" do
+ topic = TopicCreator.create(user, Guardian.new(user), valid_attrs.merge(tags: ["new tag", "another tag"], category: category.id))
+ expect(topic).to be_valid
+ expect(topic.tags.length).to eq(2)
+ end
+
it "lets new user create a topic if they don't have sufficient trust level to tag topics" do
SiteSetting.min_trust_level_to_tag_topics = 1
new_user = Fabricate(:newuser)