Merge pull request #8270 from artoscinote/ma_SCI_11597

Fix search for special characters and add specs [SCI-11597]
This commit is contained in:
Martin Artnik 2025-02-24 15:23:46 +01:00 committed by GitHub
commit fd88a5c2d3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 69 additions and 3 deletions

View file

@ -206,11 +206,10 @@ module SearchableModel
new_phrase.map! { |t| "#{t}:*" } unless exact_match
new_phrase = sanitize_sql_like(new_phrase.join('&').tr('\'', '"'))
else
new_phrase = sanitize_sql_like(Regexp.escape(new_phrase))
new_phrase = exact_match ? "(^|\\s)#{new_phrase}(\\s|$)" : "%#{new_phrase}%"
new_phrase = exact_match ? "(^|\\s)#{Regexp.escape(new_phrase)}(\\s|$)" : "%#{sanitize_sql_like(new_phrase)}%"
end
["t#{i}".to_sym, new_phrase]
[:"t#{i}", new_phrase]
end).to_h
)
end

View file

@ -0,0 +1,67 @@
# frozen_string_literal: true
require 'rails_helper'
SEARCH_SPECIAL_CHARACTER_TEST_NAMES = %w(
NameWithNoSpecialCharacters
NameWith_Underscore
NameWith@At
NameWith.Dot
NameWith-Minus
NameWith+Plus
NameWith&Ampersand
NameWith;Semicolon
NameWith:Colon
NameWith,Comma
NameWith$Dollar
NameWith/Slash
NameWith=Equals
NameWith~Tilde
NameWith^Caret
NameWith°Degree
NameWith`Backtick
NameWith"DoubleQuote
NameWith'SingleQuote
NameWith*Asterisk
NameWith?QuestionMark
NameWith%Percent
NameWith\Backslash
NameWith[SquareBracket]
NameWith{CurlyBracket}
NameWith(Parenthesis)
NameWith|Pipe
NameWith!Exclamation
).freeze
describe SearchableModel, type: :concern do
let!(:user) { create :user }
let!(:team) { create :team, created_by: user }
let!(:projects) do
SEARCH_SPECIAL_CHARACTER_TEST_NAMES.map do |name|
Project.create!(name: name, team: user.teams.first, created_by: user)
end
end
it '#where_attributes_like_boolean finds all projects by name' do
SEARCH_SPECIAL_CHARACTER_TEST_NAMES.each do |name|
expect(Project.where_attributes_like_boolean(:name, "#{name} OR something")&.first&.name).to eq name
puts "Found #{name}"
end
end
it '#where_attributes_like_boolean finds all projects by exact name' do
SEARCH_SPECIAL_CHARACTER_TEST_NAMES.filter { |n| n != 'NameWith"DoubleQuote' }.each do |name|
expect(Project.where_attributes_like_boolean(:name, "\"#{name}\"")&.first&.name).to eq name
expect(Project.where_attributes_like_boolean(:name, "\"#{name} not exact\"").count).to eq 0
puts "Found #{name}"
end
end
it '#where_attributes_like finds all projects by name ' do
SEARCH_SPECIAL_CHARACTER_TEST_NAMES.each do |name|
expect(Project.where_attributes_like(:name, name)&.first&.name).to eq name
puts "Found #{name}"
end
end
end