1 defmodule Pleroma.FormatterTest do
2 alias Pleroma.Formatter
7 describe ".add_hashtag_links" do
8 test "turns hashtags into links" do
9 text = "I love #cofe and #2hu"
10 expected_text = "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>"
12 tags = Formatter.parse_tags(text)
13 assert expected_text == Formatter.add_hashtag_links({[], text}, tags) |> Formatter.finalize
17 describe ".add_links" do
18 test "turning urls into links" do
19 text = "Hey, check out https://www.youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla."
20 expected = "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>."
22 assert Formatter.add_links({[], text}) |> Formatter.finalize == expected
24 text = "https://mastodon.social/@lambadalambda"
25 expected = "<a href='https://mastodon.social/@lambadalambda'>https://mastodon.social/@lambadalambda</a>"
27 assert Formatter.add_links({[], text}) |> Formatter.finalize == expected
29 text = "@lambadalambda"
30 expected = "@lambadalambda"
32 assert Formatter.add_links({[], text}) |> Formatter.finalize == expected
34 text = "http://www.cs.vu.nl/~ast/intel/"
35 expected = "<a href='http://www.cs.vu.nl/~ast/intel/'>http://www.cs.vu.nl/~ast/intel/</a>"
37 assert Formatter.add_links({[], text}) |> Formatter.finalize == expected
39 text = "https://forum.zdoom.org/viewtopic.php?f=44&t=57087"
40 expected = "<a href='https://forum.zdoom.org/viewtopic.php?f=44&t=57087'>https://forum.zdoom.org/viewtopic.php?f=44&t=57087</a>"
42 assert Formatter.add_links({[], text}) |> Formatter.finalize == expected
44 text = "https://en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul"
45 expected = "<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>"
47 assert Formatter.add_links({[], text}) |> Formatter.finalize == expected
51 describe "add_user_links" do
52 test "gives a replacement for user links" do
53 text = "@gsimg According to @archaeme, that is @daggsy. Also hello @archaeme@archae.me"
54 gsimg = insert(:user, %{nickname: "gsimg"})
55 archaeme = insert(:user, %{nickname: "archaeme"})
56 archaeme_remote = insert(:user, %{nickname: "archaeme@archae.me"})
58 mentions = Pleroma.Formatter.parse_mentions(text)
60 {subs, text} = Formatter.add_user_links({[], text}, mentions)
62 assert length(subs) == 3
63 Enum.each(subs, fn({uuid, _}) -> assert String.contains?(text, uuid) end)
65 expected_text = "<span><a href='#{gsimg.ap_id}'>@<span>gsimg</span></a></span> According to <span><a href='#{archaeme.ap_id}'>@<span>archaeme</span></a></span>, that is @daggsy. Also hello <span><a href='#{archaeme_remote.ap_id}'>@<span>archaeme</span></a></span>"
67 assert expected_text == Formatter.finalize({subs, text})
71 describe ".parse_tags" do
72 test "parses tags in the text" do
73 text = "Here's a #Test. Maybe these are #working or not. What about #漢字? And #は。"
76 {"#working", "working"},
81 assert Formatter.parse_tags(text) == expected
85 test "it can parse mentions and return the relevant users" do
86 text = "@gsimg According to @archaeme, that is @daggsy. Also hello @archaeme@archae.me"
88 gsimg = insert(:user, %{nickname: "gsimg"})
89 archaeme = insert(:user, %{nickname: "archaeme"})
90 archaeme_remote = insert(:user, %{nickname: "archaeme@archae.me"})
94 {"@archaeme", archaeme},
95 {"@archaeme@archae.me", archaeme_remote},
98 assert Formatter.parse_mentions(text) == expected_result
101 test "it adds cool emoji" do
102 text = "I love :moominmamma:"
104 expected_result = "I love <img height='32px' width='32px' alt='moominmamma' title='moominmamma' src='/finmoji/128px/moominmamma-128.png' />"
106 assert Formatter.emojify(text) == expected_result
109 test "it returns the emoji used in the text" do
110 text = "I love :moominmamma:"
112 assert Formatter.get_emoji(text) == [{"moominmamma", "/finmoji/128px/moominmamma-128.png"}]