FIX: verify filtered tags when checking for category minimum required tags

This commit is contained in:
Arpit Jalan 2018-04-14 23:20:43 +05:30
parent 18f50ca01a
commit 0183656631
2 changed files with 18 additions and 6 deletions

View File

@ -29,12 +29,6 @@ module DiscourseTagging
category = topic.category
tag_names = tag_names + old_tag_names if append
# validate minimum required tags for a category
if !guardian.is_staff? && category && category.minimum_required_tags > 0 && tag_names.length < category.minimum_required_tags
topic.errors[:base] << I18n.t("tags.minimum_required_tags", count: category.minimum_required_tags)
return false
end
if tag_names.present?
# guardian is explicitly nil cause we don't want to strip all
# staff tags that already passed validation
@ -54,8 +48,20 @@ module DiscourseTagging
end
end
# validate minimum required tags for a category
if !guardian.is_staff? && category && category.minimum_required_tags > 0 && tags.length < category.minimum_required_tags
topic.errors[:base] << I18n.t("tags.minimum_required_tags", count: category.minimum_required_tags)
return false
end
topic.tags = tags
else
# validate minimum required tags for a category
if !guardian.is_staff? && category && category.minimum_required_tags > 0
topic.errors[:base] << I18n.t("tags.minimum_required_tags", count: category.minimum_required_tags)
return false
end
topic.tags = []
end
topic.tags_changed = true

View File

@ -101,6 +101,12 @@ describe DiscourseTagging do
let(:category) { Fabricate(:category, minimum_required_tags: 2) }
let(:topic) { Fabricate(:topic, category: category) }
it 'when tags are not present' do
valid = DiscourseTagging.tag_topic_by_names(topic, Guardian.new(user), [])
expect(valid).to eq(false)
expect(topic.errors[:base]&.first).to eq(I18n.t("tags.minimum_required_tags", count: category.minimum_required_tags))
end
it 'when tags are less than minimum_required_tags' do
valid = DiscourseTagging.tag_topic_by_names(topic, Guardian.new(user), [tag1.name])
expect(valid).to eq(false)