1
0
Fork 1
mirror of https://github.com/pixelfed/pixelfed.git synced 2024-12-30 11:44:59 +00:00
pixelfed/tests/Unit/Lexer/StatusLexerTest.php

140 lines
4.6 KiB
PHP
Raw Normal View History

2018-11-29 05:47:39 +00:00
<?php
namespace Tests\Unit\Lexer;
2022-12-21 21:21:07 +00:00
use App\Status;
2018-11-29 05:47:39 +00:00
use App\Util\Lexer\Autolink;
use App\Util\Lexer\Extractor;
2022-12-21 21:21:07 +00:00
use Tests\TestCase;
2018-11-29 05:47:39 +00:00
class StatusLexerTest extends TestCase
{
public $status;
public $entities;
2022-12-21 21:21:07 +00:00
public $autolink;
2018-11-29 05:47:39 +00:00
2019-02-27 20:04:40 +00:00
public function setUp(): void
2018-11-29 05:47:39 +00:00
{
parent::setUp();
2022-12-21 21:21:07 +00:00
$this->status = '@pixelfed hi, really like the website! #píxelfed';
$this->entities = Extractor::create()->extract($this->status);
$this->autolink = Autolink::create()->autolink($this->status);
2018-11-29 05:47:39 +00:00
}
public function testLexerExtractor()
{
$expected = [
2022-12-21 21:21:07 +00:00
'hashtags' => [
'píxelfed',
],
'urls' => [],
'mentions' => [
'pixelfed',
],
'replyto' => 'pixelfed',
'hashtags_with_indices' => [
[
'hashtag' => 'píxelfed',
'indices' => [
39,
48,
],
],
],
'urls_with_indices' => [],
'mentions_with_indices' => [
[
'screen_name' => 'pixelfed',
'indices' => [
0,
9,
2018-11-29 05:47:39 +00:00
],
2022-12-21 21:21:07 +00:00
]
]
2018-11-29 05:47:39 +00:00
];
$this->assertEquals($this->entities, $expected);
}
public function testAutolink()
{
2019-05-28 02:57:08 +00:00
$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>';
2018-11-29 05:47:39 +00:00
$this->assertEquals($this->autolink, $expected);
}
2019-05-21 01:43:38 +00:00
2022-12-21 21:21:07 +00:00
/** @test * */
2019-05-21 01:43:38 +00:00
public function remoteMention()
{
$expected = [
2022-12-21 21:21:07 +00:00
'hashtags' => [
'dansup',
2019-05-21 01:43:38 +00:00
],
2022-12-21 21:21:07 +00:00
'urls' => [],
'mentions' => [
'@dansup@mstdn.io',
'test',
2019-05-21 01:43:38 +00:00
],
2022-12-21 21:21:07 +00:00
'replyto' => null,
'hashtags_with_indices' => [
2019-05-21 01:43:38 +00:00
[
2022-12-21 21:21:07 +00:00
'hashtag' => 'dansup',
'indices' => [
2019-05-21 01:43:38 +00:00
0,
7,
],
],
],
2022-12-21 21:21:07 +00:00
'urls_with_indices' => [],
'mentions_with_indices' => [
2019-05-21 01:43:38 +00:00
[
2022-12-21 21:21:07 +00:00
'screen_name' => '@dansup@mstdn.io',
'indices' => [
2019-05-21 01:43:38 +00:00
8,
24,
],
],
[
2022-12-21 21:21:07 +00:00
'screen_name' => 'test',
'indices' => [
2019-05-21 01:43:38 +00:00
25,
30,
],
],
],
];
$actual = Extractor::create()->extract('#dansup @dansup@mstdn.io @test');
$this->assertEquals($actual, $expected);
}
2019-06-02 03:42:06 +00:00
2022-12-21 21:21:07 +00:00
/** @test * */
2019-06-02 03:42:06 +00:00
public function mentionLimit()
{
2023-05-27 13:27:35 +00:00
$text = '@test1 @test @test2 @test3 @test4 @test5 @test6 @test7 @test8 @test9 @test10 @test11 @test12 @test13 @test14 @test15 @test16 @test17 @test18 @test19 test post';
2019-06-02 03:42:06 +00:00
$entities = Extractor::create()->extract($text);
$count = count($entities['mentions']);
2023-05-27 13:27:35 +00:00
$this->assertEquals(Status::MAX_MENTIONS, $count);
2019-06-02 03:42:06 +00:00
}
2022-12-21 21:21:07 +00:00
/** @test * */
2019-06-02 03:42:06 +00:00
public function hashtagLimit()
{
2023-05-27 13:27:35 +00:00
$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';
2019-06-02 03:42:06 +00:00
$entities = Extractor::create()->extract($text);
$count = count($entities['hashtags']);
2023-05-27 13:27:35 +00:00
$this->assertEquals(Status::MAX_HASHTAGS, $count);
2019-06-02 03:42:06 +00:00
}
2022-12-21 21:21:07 +00:00
/** @test * */
2019-06-02 03:42:06 +00:00
public function linkLimit()
{
2023-05-27 13:27:35 +00:00
$text = 'https://example.org https://example.net https://example.com https://example.com https://example.net';
2019-06-02 03:42:06 +00:00
$entities = Extractor::create()->extract($text);
$count = count($entities['urls']);
2023-05-27 13:27:35 +00:00
$this->assertEquals(Status::MAX_LINKS, $count);
2019-06-02 03:42:06 +00:00
}
2018-11-29 05:47:39 +00:00
}