diff --git a/src/Entities.php b/src/Entities.php index 36adab3..c841bc2 100644 --- a/src/Entities.php +++ b/src/Entities.php @@ -387,6 +387,7 @@ final class Entities "spoiler" => $allowTelegramTags ? '' : '', "custom_emoji" => $allowTelegramTags ? '' : '', "text_mention" => $allowTelegramTags ? '' : '', + default => '', }; $offset += $length; $insertions[$offset] = match ($entity['type']) { @@ -401,6 +402,7 @@ final class Entities "spoiler" => $allowTelegramTags ? '' : '', "custom_emoji" => $allowTelegramTags ? "" : '', "text_mention" => $allowTelegramTags ? '' : '', + default => '', } . ($insertions[$offset] ?? ''); } \ksort($insertions); diff --git a/tests/EntitiesTest.php b/tests/EntitiesTest.php index ec769c0..91ec175 100644 --- a/tests/EntitiesTest.php +++ b/tests/EntitiesTest.php @@ -10,6 +10,7 @@ use Amp\Http\Client\Request; use AssertionError; use danog\TelegramEntities\Entities; use danog\TelegramEntities\EntityTools; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; /** @internal */ @@ -79,6 +80,241 @@ class EntitiesTest extends TestCase $this->assertEmpty($test->entities); $this->assertSame('|', $test->message); } + + #[DataProvider('provideHtmlEntities')] + public function testToHtml(string $message, string $htmlTg, string $htmlNoTg, array $entities): void + { + $e = new Entities($message, $entities); + $this->assertEquals($htmlTg, $e->toHTML(true)); + $this->assertEquals($htmlNoTg, $e->toHTML(false)); + $this->assertEquals($htmlNoTg, $e->toHTML()); + } + public static function provideHtmlEntities(): iterable + { + yield [ + 'test', + 'test', + 'test', + [[ + 'type' => 'bank_card', + 'offset' => 0, + 'length' => 4, + ]] + ]; + yield [ + 'test', + '
test
', + '
test
', + [[ + 'type' => 'block_quote', + 'offset' => 0, + 'length' => 4, + ]] + ]; + yield [ + 'test', + 'test', + 'test', + [[ + 'type' => 'bold', + 'offset' => 0, + 'length' => 4, + ]] + ]; + yield [ + 'test', + 'test', + 'test', + [[ + 'type' => 'bold', + 'offset' => 0, + 'length' => 4, + ], [ + 'type' => 'italic', + 'offset' => 1, + 'length' => 2, + ]] + ]; + yield [ + 'test', + 'test', + 'test', + [[ + 'type' => 'bot_command', + 'offset' => 0, + 'length' => 4, + ]] + ]; + + yield [ + 'test', + 'test', + 'test', + [[ + 'type' => 'cashtag', + 'offset' => 0, + 'length' => 4, + ]] + ]; + + yield [ + 'test', + 'test', + 'test', + [[ + 'type' => 'code', + 'offset' => 0, + 'length' => 4, + ]] + ]; + + yield [ + 'test', + 'test', + 'test', + [[ + 'type' => 'custom_emoji', + 'offset' => 0, + 'length' => 4, + 'custom_emoji_id' => 12345, + ]] + ]; + + yield [ + 'test', + 'test', + 'test', + [[ + 'type' => 'email', + 'offset' => 0, + 'length' => 4, + ]] + ]; + + yield [ + 'test', + 'test', + 'test', + [[ + 'type' => 'hashtag', + 'offset' => 0, + 'length' => 4, + ]] + ]; + + yield [ + 'test', + 'test', + 'test', + [[ + 'type' => 'italic', + 'offset' => 0, + 'length' => 4, + ]] + ]; + + yield [ + 'test', + 'test', + 'test', + [[ + 'type' => 'text_mention', + 'offset' => 0, + 'length' => 4, + 'user' => ['id' => 12345] + ]] + ]; + + yield [ + '@test', + '@test', + '@test', + [[ + 'type' => 'mention', + 'offset' => 0, + 'length' => 5, + ]] + ]; + + yield [ + 'test', + 'test', + 'test', + [[ + 'type' => 'phone_number', + 'offset' => 0, + 'length' => 4, + ]] + ]; + + yield [ + 'test', + '
test
', + '
test
', + [[ + 'type' => 'pre', + 'offset' => 0, + 'length' => 4, + 'language' => 'language', + ]] + ]; + + yield [ + 'test', + 'test', + 'test', + [[ + 'type' => 'spoiler', + 'offset' => 0, + 'length' => 4, + ]] + ]; + + yield [ + 'test', + 'test', + 'test', + [[ + 'type' => 'strikethrough', + 'offset' => 0, + 'length' => 4, + ]] + ]; + + yield [ + 'test', + 'test', + 'test', + [[ + 'type' => 'text_link', + 'offset' => 0, + 'length' => 4, + 'url' => 'https://google.com', + ]] + ]; + + yield [ + 'test', + 'test', + 'test', + [[ + 'type' => 'underline', + 'offset' => 0, + 'length' => 4, + ]] + ]; + + yield [ + 'test', + 'test', + 'test', + [[ + 'type' => 'url', + 'offset' => 0, + 'length' => 4, + ]] + ]; + } private function testEntitiesInner(string $mode, string $html, string $bare, array $entities, ?string $htmlReverse = null): void { $result = self::render(message: $html, parse_mode: $mode);