FIX: Handle tags with underscores correctly (#26839)

This commit is contained in:
Daniel Waterworth 2024-05-01 20:01:39 -05:00 committed by GitHub
parent 3c4a15a35f
commit 9f9c7f0a23
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 1 deletions

View File

@ -509,8 +509,8 @@ module DiscourseTagging
term = opts[:term] term = opts[:term]
if term.present? if term.present?
term = term.gsub("_", "\\_").downcase
builder_params[:cleaned_term] = term builder_params[:cleaned_term] = term
term = term.gsub("_", "\\_").downcase
if opts[:term_type] == DiscourseTagging.term_types[:starts_with] if opts[:term_type] == DiscourseTagging.term_types[:starts_with]
builder_params[:term] = "#{term}%" builder_params[:term] = "#{term}%"

View File

@ -426,6 +426,24 @@ RSpec.describe DiscourseTagging do
expect(tags).to contain_exactly(tag1.name, tag3.name) expect(tags).to contain_exactly(tag1.name, tag3.name)
end end
context "with tags with underscores" do
fab!(:tag_with_underscore) { Fabricate(:tag, name: "tag_1") }
fab!(:another_tag_with_underscore) do
Fabricate(:tag, name: "tag_1a", public_topic_count: 10)
end
it "puts the exact match at the start of the results" do
tags =
DiscourseTagging.filter_allowed_tags(
nil,
term: "tag_1",
order_search_results: true,
).map(&:name)
expect(tags).to eq(%w[tag_1 tag_1a])
end
end
context "with tag with colon" do context "with tag with colon" do
fab!(:tag_with_colon) { Fabricate(:tag, name: "with:colon") } fab!(:tag_with_colon) { Fabricate(:tag, name: "with:colon") }