StatusLexerTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace Tests\Unit\Lexer;
  3. use Tests\TestCase;
  4. use Illuminate\Foundation\Testing\WithFaker;
  5. use Illuminate\Foundation\Testing\RefreshDatabase;
  6. use App\Util\Lexer\Autolink;
  7. use App\Util\Lexer\Extractor;
  8. use App\Status;
  9. class StatusLexerTest extends TestCase
  10. {
  11. public $status;
  12. public $entities;
  13. public $autolink;
  14. public function setUp(): void
  15. {
  16. parent::setUp();
  17. $this->status = "@pixelfed hi, really like the website! #píxelfed";
  18. $this->entities = Extractor::create()->extract($this->status);
  19. $this->autolink = Autolink::create()->autolink($this->status);
  20. }
  21. public function testLexerExtractor()
  22. {
  23. $expected = [
  24. "hashtags" => [
  25. "píxelfed",
  26. ],
  27. "urls" => [],
  28. "mentions" => [
  29. "pixelfed",
  30. ],
  31. "replyto" => "pixelfed",
  32. "hashtags_with_indices" => [
  33. [
  34. "hashtag" => "píxelfed",
  35. "indices" => [
  36. 39,
  37. 48,
  38. ],
  39. ],
  40. ],
  41. "urls_with_indices" => [],
  42. "mentions_with_indices" => [
  43. [
  44. "screen_name" => "pixelfed",
  45. "indices" => [
  46. 0,
  47. 9,
  48. ],
  49. ]
  50. ]
  51. ];
  52. $this->assertEquals($this->entities, $expected);
  53. }
  54. public function testAutolink()
  55. {
  56. $expected = '<a class="u-url mention" href="https://pixelfed.dev/pixelfed" rel="external nofollow noopener" target="_blank">@pixelfed</a> hi, really like the website! <a href="https://pixelfed.dev/discover/tags/píxelfed?src=hash" title="#píxelfed" class="u-url hashtag" rel="external nofollow noopener">#píxelfed</a>';
  57. $this->assertEquals($this->autolink, $expected);
  58. }
  59. /** @test **/
  60. public function remoteMention()
  61. {
  62. $expected = [
  63. "hashtags" => [
  64. "dansup",
  65. ],
  66. "urls" => [],
  67. "mentions" => [
  68. "@dansup@mstdn.io",
  69. "test",
  70. ],
  71. "replyto" => null,
  72. "hashtags_with_indices" => [
  73. [
  74. "hashtag" => "dansup",
  75. "indices" => [
  76. 0,
  77. 7,
  78. ],
  79. ],
  80. ],
  81. "urls_with_indices" => [],
  82. "mentions_with_indices" => [
  83. [
  84. "screen_name" => "@dansup@mstdn.io",
  85. "indices" => [
  86. 8,
  87. 24,
  88. ],
  89. ],
  90. [
  91. "screen_name" => "test",
  92. "indices" => [
  93. 25,
  94. 30,
  95. ],
  96. ],
  97. ],
  98. ];
  99. $actual = Extractor::create()->extract('#dansup @dansup@mstdn.io @test');
  100. $this->assertEquals($actual, $expected);
  101. }
  102. /** @test **/
  103. public function mentionLimit()
  104. {
  105. $text = '@test1 @test @test2 @test3 @test4 @test5 test post';
  106. $entities = Extractor::create()->extract($text);
  107. $count = count($entities['mentions']);
  108. $this->assertEquals($count, Status::MAX_MENTIONS);
  109. }
  110. /** @test **/
  111. public function hashtagLimit()
  112. {
  113. $text = '#hashtag0 #hashtag1 #hashtag2 #hashtag3 #hashtag4 #hashtag5 #hashtag6 #hashtag7 #hashtag8 #hashtag9 #hashtag10 #hashtag11 #hashtag12 #hashtag13 #hashtag14 #hashtag15 #hashtag16 #hashtag17 #hashtag18 #hashtag19 #hashtag20 #hashtag21 #hashtag22 #hashtag23 #hashtag24 #hashtag25 #hashtag26 #hashtag27 #hashtag28 #hashtag29 #hashtag30 #hashtag31';
  114. $entities = Extractor::create()->extract($text);
  115. $count = count($entities['hashtags']);
  116. $this->assertEquals($count, Status::MAX_HASHTAGS);
  117. }
  118. /** @test **/
  119. public function linkLimit()
  120. {
  121. $text = 'https://example.org https://example.net https://example.com';
  122. $entities = Extractor::create()->extract($text);
  123. $count = count($entities['urls']);
  124. $this->assertEquals($count, Status::MAX_LINKS);
  125. }
  126. }