StatusLexerTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 Tests\TestCase;
  7. class StatusLexerTest extends TestCase
  8. {
  9. public $status;
  10. public $entities;
  11. public $autolink;
  12. public function setUp(): void
  13. {
  14. parent::setUp();
  15. $this->status = '@pixelfed hi, really like the website! #píxelfed';
  16. $this->entities = Extractor::create()->extract($this->status);
  17. $this->autolink = Autolink::create()->autolink($this->status);
  18. }
  19. public function testLexerExtractor()
  20. {
  21. $expected = [
  22. 'hashtags' => [
  23. 'píxelfed',
  24. ],
  25. 'urls' => [],
  26. 'mentions' => [
  27. 'pixelfed',
  28. ],
  29. 'replyto' => 'pixelfed',
  30. 'hashtags_with_indices' => [
  31. [
  32. 'hashtag' => 'píxelfed',
  33. 'indices' => [
  34. 39,
  35. 48,
  36. ],
  37. ],
  38. ],
  39. 'urls_with_indices' => [],
  40. 'mentions_with_indices' => [
  41. [
  42. 'screen_name' => 'pixelfed',
  43. 'indices' => [
  44. 0,
  45. 9,
  46. ],
  47. ]
  48. ]
  49. ];
  50. $this->assertEquals($this->entities, $expected);
  51. }
  52. public function testAutolink()
  53. {
  54. $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>';
  55. $this->assertEquals($this->autolink, $expected);
  56. }
  57. /** @test * */
  58. public function remoteMention()
  59. {
  60. $expected = [
  61. 'hashtags' => [
  62. 'dansup',
  63. ],
  64. 'urls' => [],
  65. 'mentions' => [
  66. '@dansup@mstdn.io',
  67. 'test',
  68. ],
  69. 'replyto' => null,
  70. 'hashtags_with_indices' => [
  71. [
  72. 'hashtag' => 'dansup',
  73. 'indices' => [
  74. 0,
  75. 7,
  76. ],
  77. ],
  78. ],
  79. 'urls_with_indices' => [],
  80. 'mentions_with_indices' => [
  81. [
  82. 'screen_name' => '@dansup@mstdn.io',
  83. 'indices' => [
  84. 8,
  85. 24,
  86. ],
  87. ],
  88. [
  89. 'screen_name' => 'test',
  90. 'indices' => [
  91. 25,
  92. 30,
  93. ],
  94. ],
  95. ],
  96. ];
  97. $actual = Extractor::create()->extract('#dansup @dansup@mstdn.io @test');
  98. $this->assertEquals($actual, $expected);
  99. }
  100. /** @test * */
  101. public function mentionLimit()
  102. {
  103. $text = '@test1 @test @test2 @test3 @test4 @test5 @test6 @test7 @test8 @test9 @test10 @test11 @test12 @test13 @test14 @test15 @test16 @test17 @test18 @test19 test post';
  104. $entities = Extractor::create()->extract($text);
  105. $count = count($entities['mentions']);
  106. $this->assertEquals(Status::MAX_MENTIONS, $count);
  107. }
  108. /** @test * */
  109. public function hashtagLimit()
  110. {
  111. $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';
  112. $entities = Extractor::create()->extract($text);
  113. $count = count($entities['hashtags']);
  114. $this->assertEquals(Status::MAX_HASHTAGS, $count);
  115. }
  116. /** @test * */
  117. public function linkLimit()
  118. {
  119. $text = 'https://example.org https://example.net https://example.com https://example.com https://example.net';
  120. $entities = Extractor::create()->extract($text);
  121. $count = count($entities['urls']);
  122. $this->assertEquals(Status::MAX_LINKS, $count);
  123. }
  124. }