UsernameTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. namespace Tests\Unit\Lexer;
  3. use App\Util\Lexer\Autolink;
  4. use App\Util\Lexer\Extractor;
  5. use Tests\TestCase;
  6. class UsernameTest extends TestCase
  7. {
  8. /** @test * */
  9. public function genericUsername()
  10. {
  11. $username = '@dansup';
  12. $entities = Extractor::create()->extract($username);
  13. $autolink = Autolink::create()->autolink($username);
  14. $expectedAutolink = '<a class="u-url mention" href="https://pixelfed.dev/dansup" rel="external nofollow noopener" target="_blank">@dansup</a>';
  15. $expectedEntity = [
  16. 'hashtags' => [],
  17. 'urls' => [],
  18. 'mentions' => [
  19. 'dansup',
  20. ],
  21. 'replyto' => 'dansup',
  22. 'hashtags_with_indices' => [],
  23. 'urls_with_indices' => [],
  24. 'mentions_with_indices' => [
  25. [
  26. 'screen_name' => 'dansup',
  27. 'indices' => [
  28. 0,
  29. 7,
  30. ],
  31. ],
  32. ],
  33. ];
  34. $this->assertEquals($expectedAutolink, $autolink);
  35. $this->assertEquals($expectedEntity, $entities);
  36. }
  37. /** @test * */
  38. public function usernameWithPeriod()
  39. {
  40. $username = '@dansup.two';
  41. $autolink = Autolink::create()->autolink($username);
  42. $entities = Extractor::create()->extract($username);
  43. $expectedAutolink = '<a class="u-url mention" href="https://pixelfed.dev/dansup.two" rel="external nofollow noopener" target="_blank">@dansup.two</a>';
  44. $expectedEntity = [
  45. 'hashtags' => [],
  46. 'urls' => [],
  47. 'mentions' => [
  48. 'dansup.two',
  49. ],
  50. 'replyto' => 'dansup.two',
  51. 'hashtags_with_indices' => [],
  52. 'urls_with_indices' => [],
  53. 'mentions_with_indices' => [
  54. [
  55. 'screen_name' => 'dansup.two',
  56. 'indices' => [
  57. 0,
  58. 11,
  59. ],
  60. ],
  61. ],
  62. ];
  63. $this->assertEquals($expectedAutolink, $autolink);
  64. $this->assertEquals($expectedEntity, $entities);
  65. }
  66. /** @test * */
  67. public function usernameWithDash()
  68. {
  69. $username = '@dansup-too';
  70. $autolink = Autolink::create()->autolink($username);
  71. $entities = Extractor::create()->extract($username);
  72. $expectedAutolink = '<a class="u-url mention" href="https://pixelfed.dev/dansup-too" rel="external nofollow noopener" target="_blank">@dansup-too</a>';
  73. $expectedEntity = [
  74. 'hashtags' => [],
  75. 'urls' => [],
  76. 'mentions' => [
  77. 'dansup-too',
  78. ],
  79. 'replyto' => 'dansup-too',
  80. 'hashtags_with_indices' => [],
  81. 'urls_with_indices' => [],
  82. 'mentions_with_indices' => [
  83. [
  84. 'screen_name' => 'dansup-too',
  85. 'indices' => [
  86. 0,
  87. 11,
  88. ],
  89. ],
  90. ],
  91. ];
  92. $this->assertEquals($expectedAutolink, $autolink);
  93. $this->assertEquals($expectedEntity, $entities);
  94. }
  95. /** @test * */
  96. public function usernameWithUnderscore()
  97. {
  98. $username = '@dansup_too';
  99. $autolink = Autolink::create()->autolink($username);
  100. $entities = Extractor::create()->extract($username);
  101. $expectedAutolink = '<a class="u-url mention" href="https://pixelfed.dev/dansup_too" rel="external nofollow noopener" target="_blank">@dansup_too</a>';
  102. $expectedEntity = [
  103. 'hashtags' => [],
  104. 'urls' => [],
  105. 'mentions' => [
  106. 'dansup_too',
  107. ],
  108. 'replyto' => 'dansup_too',
  109. 'hashtags_with_indices' => [],
  110. 'urls_with_indices' => [],
  111. 'mentions_with_indices' => [
  112. [
  113. 'screen_name' => 'dansup_too',
  114. 'indices' => [
  115. 0,
  116. 11,
  117. ],
  118. ],
  119. ],
  120. ];
  121. $this->assertEquals($expectedAutolink, $autolink);
  122. $this->assertEquals($expectedEntity, $entities);
  123. }
  124. /** @test * */
  125. public function multipleMentions()
  126. {
  127. $text = 'hello @dansup and @pixelfed.team from @username_underscore';
  128. $autolink = Autolink::create()->autolink($text);
  129. $entities = Extractor::create()->extract($text);
  130. $expectedAutolink = 'hello <a class="u-url mention" href="https://pixelfed.dev/dansup" rel="external nofollow noopener" target="_blank">@dansup</a> and <a class="u-url mention" href="https://pixelfed.dev/pixelfed.team" rel="external nofollow noopener" target="_blank">@pixelfed.team</a> from <a class="u-url mention" href="https://pixelfed.dev/username_underscore" rel="external nofollow noopener" target="_blank">@username_underscore</a>';
  131. $expectedEntity = [
  132. 'hashtags' => [],
  133. 'urls' => [],
  134. 'mentions' => [
  135. 'dansup',
  136. 'pixelfed.team',
  137. 'username_underscore',
  138. ],
  139. 'replyto' => null,
  140. 'hashtags_with_indices' => [],
  141. 'urls_with_indices' => [],
  142. 'mentions_with_indices' => [
  143. [
  144. 'screen_name' => 'dansup',
  145. 'indices' => [
  146. 6,
  147. 13,
  148. ],
  149. ],
  150. [
  151. 'screen_name' => 'pixelfed.team',
  152. 'indices' => [
  153. 18,
  154. 32,
  155. ],
  156. ],
  157. [
  158. 'screen_name' => 'username_underscore',
  159. 'indices' => [
  160. 38,
  161. 58,
  162. ],
  163. ],
  164. ],
  165. ];
  166. $this->assertEquals($expectedAutolink, $autolink);
  167. $this->assertEquals($expectedEntity, $entities);
  168. }
  169. /** @test * */
  170. public function germanUmlatsAutolink()
  171. {
  172. $mentions = "@März and @königin and @Glück";
  173. $autolink = Autolink::create()->autolink($mentions);
  174. $expectedAutolink = '<a class="u-url mention" href="https://pixelfed.dev/März" rel="external nofollow noopener" target="_blank">@März</a> and <a class="u-url mention" href="https://pixelfed.dev/königin" rel="external nofollow noopener" target="_blank">@königin</a> and <a class="u-url mention" href="https://pixelfed.dev/Glück" rel="external nofollow noopener" target="_blank">@Glück</a>';
  175. $this->assertEquals($expectedAutolink, $autolink);
  176. }
  177. /** @test * */
  178. public function germanUmlatsExtractor()
  179. {
  180. $mentions = "@März and @königin and @Glück";
  181. $entities = Extractor::create()->extract($mentions);
  182. $expectedEntity = [
  183. "hashtags" => [],
  184. "urls" => [],
  185. "mentions" => [
  186. "märz",
  187. "königin",
  188. "glück",
  189. ],
  190. "replyto" => null,
  191. "hashtags_with_indices" => [],
  192. "urls_with_indices" => [],
  193. "mentions_with_indices" => [
  194. [
  195. "screen_name" => "März",
  196. "indices" => [
  197. 0,
  198. 5,
  199. ],
  200. ],
  201. [
  202. "screen_name" => "königin",
  203. "indices" => [
  204. 10,
  205. 18,
  206. ],
  207. ],
  208. [
  209. "screen_name" => "Glück",
  210. "indices" => [
  211. 23,
  212. 29,
  213. ],
  214. ],
  215. ],
  216. ];
  217. $this->assertEquals($expectedEntity, $entities);
  218. }
  219. /** @test * */
  220. public function germanUmlatsWebfingerAutolink()
  221. {
  222. $mentions = "hello @märz@example.org!";
  223. $autolink = Autolink::create()->autolink($mentions);
  224. $expectedAutolink = 'hello <a class="u-url list-slug" href="https://pixelfed.dev/@märz@example.org" rel="external nofollow noopener" target="_blank">@märz@example.org</a>!';
  225. $this->assertEquals($expectedAutolink, $autolink);
  226. }
  227. }