DEV: Add test for link watched words (#13251)

DEV: Add test for link watched words (#13251)

diff --git a/app/models/watched_word.rb b/app/models/watched_word.rb
index ecc239a..9251d7d 100644
--- a/app/models/watched_word.rb
+++ b/app/models/watched_word.rb
@@ -20,7 +20,7 @@ class WatchedWord < ActiveRecord::Base
   before_validation do
     self.word = self.class.normalize_word(self.word)
     if self.action == WatchedWord.actions[:link] && !(self.replacement =~ /^https?:\/\//)
-      self.replacement = "#{Discourse.base_url}#{self.replacement.starts_with?("/") ? "" : "/"}#{self.replacement}"
+      self.replacement = "#{Discourse.base_url}#{self.replacement&.starts_with?("/") ? "" : "/"}#{self.replacement}"
     end
   end
 
diff --git a/spec/models/watched_word_spec.rb b/spec/models/watched_word_spec.rb
index e58a943..a2d425a 100644
--- a/spec/models/watched_word_spec.rb
+++ b/spec/models/watched_word_spec.rb
@@ -90,5 +90,16 @@ describe WatchedWord do
         expect(w.id).to eq(existing.id)
       }.to_not change { described_class.count }
     end
+
+    it "replaces link with absolute URL" do
+      word = Fabricate(:watched_word, action: described_class.actions[:link], word: "meta1")
+      expect(word.replacement).to eq("http://test.localhost/")
+
+      word = Fabricate(:watched_word, action: described_class.actions[:link], word: "meta2", replacement: "test")
+      expect(word.replacement).to eq("http://test.localhost/test")
+
+      word = Fabricate(:watched_word, action: described_class.actions[:link], word: "meta3", replacement: "/test")
+      expect(word.replacement).to eq("http://test.localhost/test")
+    end
   end
 end

GitHub sha: 648d2fd7

This commit appears in #13251 which was approved by ZogStriP. It was merged by SamSaffron.