Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
6.0KB

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