tag aliases: fix bug in alias resolution.

Bug: Searching for an aliased tag returns an empty page instead of
showing the results for the real tag.

Cause: The query parsing code runs the search string through
`.mb_chars.downcase` before calling `TagAlias.to_aliased`, so the input
to `to_aliased` is actually a ActiveSupport::Multibyte::Chars object.
This breaks the `aliases[name]` hash lookup because `name` is not a
plain string.

Fixup for c7bcce429.
This commit is contained in:
evazion
2019-08-12 18:10:49 -05:00
parent a71899559a
commit b50b7f2a91
3 changed files with 12 additions and 2 deletions

View File

@@ -44,8 +44,8 @@ class TagAlias < TagRelationship
end
def self.to_aliased(names)
names = Array(names)
return names if names.empty?
names = Array(names).map(&:to_s)
return [] if names.empty?
aliases = active.where(antecedent_name: names).map { |ta| [ta.antecedent_name, ta.consequent_name] }.to_h
names.map { |name| aliases[name] || name }
end

View File

@@ -2407,6 +2407,15 @@ class PostTest < ActiveSupport::TestCase
assert_tag_match([], "filesize:1048000")
end
should "resolve aliases to the actual tag" do
create(:tag_alias, antecedent_name: "kitten", consequent_name: "cat")
post1 = create(:post, tag_string: "cat")
post2 = create(:post, tag_string: "dog")
assert_tag_match([post1], "kitten")
assert_tag_match([post2], "-kitten")
end
should "fail for more than 6 tags" do
post1 = FactoryBot.create(:post, :rating => "s")

View File

@@ -111,6 +111,7 @@ class TagAliasTest < ActiveSupport::TestCase
ta = create(:tag_alias, antecedent_name: "aaa", consequent_name: "bbb")
assert_equal(["bbb"], TagAlias.to_aliased("aaa"))
assert_equal(["bbb"], TagAlias.to_aliased("aaa".mb_chars))
assert_equal(["bbb", "ccc"], TagAlias.to_aliased(["aaa", "ccc"]))
assert_equal(["ccc", "bbb"], TagAlias.to_aliased(["ccc", "bbb"]))
assert_equal(["bbb", "bbb"], TagAlias.to_aliased(["aaa", "aaa"]))