Merge branch 'bugfix/html-webfinger-host-meta' into 'develop'
[akkoma] / test / formatter_test.exs
1 defmodule Pleroma.FormatterTest do
2 alias Pleroma.Formatter
3 use Pleroma.DataCase
4
5 import Pleroma.Factory
6
7 describe ".add_hashtag_links" do
8 test "turns hashtags into links" do
9 text = "I love #cofe and #2hu"
10
11 expected_text =
12 "I love <a href='http://localhost:4001/tag/cofe' rel='tag'>#cofe</a> and <a href='http://localhost:4001/tag/2hu' rel='tag'>#2hu</a>"
13
14 tags = Formatter.parse_tags(text)
15
16 assert expected_text ==
17 Formatter.add_hashtag_links({[], text}, tags) |> Formatter.finalize()
18 end
19 end
20
21 describe ".add_links" do
22 test "turning urls into links" do
23 text = "Hey, check out https://www.youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla."
24
25 expected =
26 "Hey, check out <a href='https://www.youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla'>https://www.youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla</a>."
27
28 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
29
30 text = "https://mastodon.social/@lambadalambda"
31
32 expected =
33 "<a href='https://mastodon.social/@lambadalambda'>https://mastodon.social/@lambadalambda</a>"
34
35 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
36
37 text = "@lambadalambda"
38 expected = "@lambadalambda"
39
40 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
41
42 text = "http://www.cs.vu.nl/~ast/intel/"
43 expected = "<a href='http://www.cs.vu.nl/~ast/intel/'>http://www.cs.vu.nl/~ast/intel/</a>"
44
45 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
46
47 text = "https://forum.zdoom.org/viewtopic.php?f=44&t=57087"
48
49 expected =
50 "<a href='https://forum.zdoom.org/viewtopic.php?f=44&t=57087'>https://forum.zdoom.org/viewtopic.php?f=44&t=57087</a>"
51
52 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
53
54 text = "https://en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul"
55
56 expected =
57 "<a href='https://en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul'>https://en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul</a>"
58
59 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
60
61 text = "https://www.google.co.jp/search?q=Nasim+Aghdam"
62
63 expected =
64 "<a href='https://www.google.co.jp/search?q=Nasim+Aghdam'>https://www.google.co.jp/search?q=Nasim+Aghdam</a>"
65
66 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
67 end
68 end
69
70 describe "add_user_links" do
71 test "gives a replacement for user links" do
72 text = "@gsimg According to @archaeme, that is @daggsy. Also hello @archaeme@archae.me"
73 gsimg = insert(:user, %{nickname: "gsimg"})
74
75 archaeme =
76 insert(:user, %{
77 nickname: "archaeme",
78 info: %{"source_data" => %{"url" => "https://archeme/@archaeme"}}
79 })
80
81 archaeme_remote = insert(:user, %{nickname: "archaeme@archae.me"})
82
83 mentions = Pleroma.Formatter.parse_mentions(text)
84
85 {subs, text} = Formatter.add_user_links({[], text}, mentions)
86
87 assert length(subs) == 3
88 Enum.each(subs, fn {uuid, _} -> assert String.contains?(text, uuid) end)
89
90 expected_text =
91 "<span><a href='#{gsimg.ap_id}'>@<span>gsimg</span></a></span> According to <span><a href='#{
92 "https://archeme/@archaeme"
93 }'>@<span>archaeme</span></a></span>, that is @daggsy. Also hello <span><a href='#{
94 archaeme_remote.ap_id
95 }'>@<span>archaeme</span></a></span>"
96
97 assert expected_text == Formatter.finalize({subs, text})
98 end
99 end
100
101 describe ".parse_tags" do
102 test "parses tags in the text" do
103 text = "Here's a #Test. Maybe these are #working or not. What about #漢字? And #は。"
104
105 expected = [
106 {"#Test", "test"},
107 {"#working", "working"},
108 {"#漢字", "漢字"},
109 {"#は", "は"}
110 ]
111
112 assert Formatter.parse_tags(text) == expected
113 end
114 end
115
116 test "it can parse mentions and return the relevant users" do
117 text = "@gsimg According to @archaeme, that is @daggsy. Also hello @archaeme@archae.me"
118
119 gsimg = insert(:user, %{nickname: "gsimg"})
120 archaeme = insert(:user, %{nickname: "archaeme"})
121 archaeme_remote = insert(:user, %{nickname: "archaeme@archae.me"})
122
123 expected_result = [
124 {"@gsimg", gsimg},
125 {"@archaeme", archaeme},
126 {"@archaeme@archae.me", archaeme_remote}
127 ]
128
129 assert Formatter.parse_mentions(text) == expected_result
130 end
131
132 test "it adds cool emoji" do
133 text = "I love :moominmamma:"
134
135 expected_result =
136 "I love <img height='32px' width='32px' alt='moominmamma' title='moominmamma' src='/finmoji/128px/moominmamma-128.png' />"
137
138 assert Formatter.emojify(text) == expected_result
139 end
140
141 test "it returns the emoji used in the text" do
142 text = "I love :moominmamma:"
143
144 assert Formatter.get_emoji(text) == [{"moominmamma", "/finmoji/128px/moominmamma-128.png"}]
145 end
146 end