FIX: Add word boundaries to replace and tag watched words (#13405)

The generated regular expressions did not contain \b which matched
every text that contained the word, even if it was only a substring of
a word.

For example, if "art" was a watched word a post containing word
"artist" matched.
This commit is contained in:
Bianca Nenciu
2021-06-18 18:54:06 +03:00
committed by GitHub
parent 4afd8f9bdf
commit 74f7295631
8 changed files with 38 additions and 18 deletions

View File

@@ -502,13 +502,21 @@ describe PostCreator do
end
context "without regular expressions" do
it "works" do
it "works with many tags" do
Fabricate(:watched_word, action: WatchedWord.actions[:tag], word: "HELLO", replacement: "greetings , hey")
@post = creator.create
expect(@post.topic.tags.map(&:name)).to match_array(['greetings', 'hey'])
end
it "works with overlapping words" do
Fabricate(:watched_word, action: WatchedWord.actions[:tag], word: "art", replacement: "about-art")
Fabricate(:watched_word, action: WatchedWord.actions[:tag], word: "artist*", replacement: "about-artists")
post = PostCreator.new(user, title: "hello world topic", raw: "this is topic abour artists", archetype_id: 1).create
expect(post.topic.tags.map(&:name)).to match_array(['about-artists'])
end
it "does not treat as regular expressions" do
Fabricate(:watched_word, action: WatchedWord.actions[:tag], word: "he(llo|y)", replacement: "greetings , hey")

View File

@@ -1420,6 +1420,10 @@ HTML
expect(PrettyText.cook("Lorem ipsum dolor sittt amet")).to match_html(<<~HTML)
<p>Lorem ipsum something else amet</p>
HTML
expect(PrettyText.cook("Lorem ipsum xdolor sit amet")).to match_html(<<~HTML)
<p>Lorem ipsum xdolor sit amet</p>
HTML
end
it "replaces words with links" do