StatusLexerTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace Tests\Unit\Lexer;
  3. use App\Status;
  4. use App\Util\Lexer\Autolink;
  5. use App\Util\Lexer\Extractor;
  6. use PHPUnit\Framework\Attributes\Test;
  7. use Tests\TestCase;
  8. class StatusLexerTest extends TestCase
  9. {
  10. public $status;
  11. public $entities;
  12. public $autolink;
  13. public function setUp(): void
  14. {
  15. parent::setUp();
  16. $this->status = '@pixelfed hi, really like the website! #píxelfed';
  17. $this->entities = Extractor::create()->extract($this->status);
  18. $this->autolink = Autolink::create()->autolink($this->status);
  19. }
  20. #[Test]
  21. public function lexerExtractor()
  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. #[Test]
  55. public function autolink()
  56. {
  57. $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>';
  58. $this->assertEquals($this->autolink, $expected);
  59. }
  60. #[Test]
  61. public function remoteMention()
  62. {
  63. $expected = [
  64. 'hashtags' => [
  65. 'dansup',
  66. ],
  67. 'urls' => [],
  68. 'mentions' => [
  69. '@dansup@mstdn.io',
  70. 'test',
  71. ],
  72. 'replyto' => null,
  73. 'hashtags_with_indices' => [
  74. [
  75. 'hashtag' => 'dansup',
  76. 'indices' => [
  77. 0,
  78. 7,
  79. ],
  80. ],
  81. ],
  82. 'urls_with_indices' => [],
  83. 'mentions_with_indices' => [
  84. [
  85. 'screen_name' => '@dansup@mstdn.io',
  86. 'indices' => [
  87. 8,
  88. 24,
  89. ],
  90. ],
  91. [
  92. 'screen_name' => 'test',
  93. 'indices' => [
  94. 25,
  95. 30,
  96. ],
  97. ],
  98. ],
  99. ];
  100. $actual = Extractor::create()->extract('#dansup @dansup@mstdn.io @test');
  101. $this->assertEquals($actual, $expected);
  102. }
  103. #[Test]
  104. public function mentionLimit()
  105. {
  106. $text = '@test1 @test @test2 @test3 @test4 @test5 @test6 @test7 @test8 @test9 @test10 @test11 @test12 @test13 @test14 @test15 @test16 @test17 @test18 @test19 test post';
  107. $entities = Extractor::create()->extract($text);
  108. $count = count($entities['mentions']);
  109. $this->assertEquals(Status::MAX_MENTIONS, $count);
  110. }
  111. #[Test]
  112. public function hashtagLimit()
  113. {
  114. $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 #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';
  115. $entities = Extractor::create()->extract($text);
  116. $count = count($entities['hashtags']);
  117. $this->assertEquals(Status::MAX_HASHTAGS, $count);
  118. }
  119. #[Test]
  120. public function linkLimit()
  121. {
  122. $text = 'https://example.org https://example.net https://example.com https://example.com https://example.net';
  123. $entities = Extractor::create()->extract($text);
  124. $count = count($entities['urls']);
  125. $this->assertEquals(Status::MAX_LINKS, $count);
  126. }
  127. }