FIX: prevents crash in discourse_tagging with empty term (#8548)

This commit is contained in:
Joffrey JAFFEUX 2019-12-17 10:55:06 +01:00 committed by GitHub
parent 6ab12ed96b
commit 81b4de39ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 4 deletions

View File

@ -222,7 +222,7 @@ module DiscourseTagging
distinct_clause = if opts[:order_popularity]
"DISTINCT ON (topic_count, name)"
elsif opts[:order_search_results]
elsif opts[:order_search_results] && opts[:term].present?
"DISTINCT ON (lower(name) = lower(:cleaned_term), topic_count, name)"
else
""
@ -278,9 +278,7 @@ module DiscourseTagging
term = opts[:term]
if term.present?
term = term.gsub("_", "\\_")
clean_tag(term)
term.downcase!
term = term.gsub("_", "\\_").downcase
builder.where("LOWER(name) LIKE :term")
builder_params[:cleaned_term] = term
builder_params[:term] = "%#{term}%"

View File

@ -129,6 +129,16 @@ describe DiscourseTagging do
end
end
context 'empty term' do
it "works with an empty term" do
tags = DiscourseTagging.filter_allowed_tags(Guardian.new(user),
term: '',
order_search_results: true
).map(&:name)
expect(sorted_tag_names(tags)).to eq(sorted_tag_names([tag1, tag2, tag3]))
end
end
context 'tag synonyms' do
fab!(:base_tag) { Fabricate(:tag, name: 'discourse') }
fab!(:synonym) { Fabricate(:tag, name: 'discource', target_tag: base_tag) }