UsernameTest.php 8.2 KB

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