UsernameTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace Tests\Unit\Lexer;
  3. use Tests\TestCase;
  4. use Illuminate\Foundation\Testing\WithFaker;
  5. use App\Util\Lexer\Autolink;
  6. use App\Util\Lexer\Extractor;
  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. }