From 059a1112824e19cb9267c170c890a583c018f874 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Tue, 29 Aug 2023 22:16:09 -0400 Subject: [PATCH] Add unit tests for Smilies::isEmojiPost - Current implementation is failing tests with emojis including the zero-width-joiner character, encoded on 3 bytes only. --- src/Content/Smilies.php | 3 +- tests/src/Content/SmiliesTest.php | 71 +++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/src/Content/Smilies.php b/src/Content/Smilies.php index b7abbd8e5f..664a4089ce 100644 --- a/src/Content/Smilies.php +++ b/src/Content/Smilies.php @@ -289,11 +289,12 @@ class Smilies /** * Checks if the body only contains 4 byte unicode characters. * - * @param string $body + * @param string $body Possibly-HTML post body * @return boolean */ public static function isEmojiPost(string $body): bool { + // Strips all whitespace $conv = preg_replace('#\s#u', '', html_entity_decode($body)); // Emojis are always 4 byte Unicode characters return (!empty($conv) && (strlen($conv) / mb_strlen($conv) == 4)); diff --git a/tests/src/Content/SmiliesTest.php b/tests/src/Content/SmiliesTest.php index a886f1ac01..38eb743e85 100644 --- a/tests/src/Content/SmiliesTest.php +++ b/tests/src/Content/SmiliesTest.php @@ -72,4 +72,75 @@ class SmiliesTest extends FixtureTest $output = Smilies::replaceFromArray($text, $smilies); self::assertEquals($expected, $output); } + + public function dataIsEmojiPost(): array + { + return [ + 'emoji' => [ + 'expected' => true, + 'body' => '๐Ÿ‘€', + ], + 'emojis' => [ + 'expected' => true, + 'body' => '๐Ÿ‘€๐Ÿคท', + ], + 'emoji+whitespace' => [ + 'expected' => true, + 'body' => ' ๐Ÿ‘€ ', + ], + 'empty' => [ + 'expected' => false, + 'body' => '', + ], + 'whitespace' => [ + 'expected' => false, + 'body' => ' + ', + ], + 'emoji+ASCII' => [ + 'expected' => false, + 'body' => '๐Ÿคทa', + ], + 'HTML entity whitespace' => [ + 'expected' => false, + 'body' => ' ', + ], + 'HTML entity else' => [ + 'expected' => false, + 'body' => '°', + ], + 'emojis+HTML whitespace' => [ + 'expected' => true, + 'body' => '๐Ÿ‘€ ๐Ÿคท', + ], + 'emojis+HTML else' => [ + 'expected' => false, + 'body' => '๐Ÿ‘€<๐Ÿคท', + ], + 'zwj' => [ + 'expected' => true, + 'body' => '๐Ÿ‘จโ€๐Ÿ‘จโ€๐Ÿ‘งโ€', + ], + 'zwj+whitespace' => [ + 'expected' => true, + 'body' => ' ๐Ÿ‘จโ€๐Ÿ‘จโ€๐Ÿ‘งโ€ ', + ], + 'zwj+HTML whitespace' => [ + 'expected' => true, + 'body' => ' ๐Ÿ‘จโ€๐Ÿ‘จโ€๐Ÿ‘งโ€ ', + ], + ]; + } + + /** + * @dataProvider dataIsEmojiPost + * + * @param bool $expected + * @param string $body + * @return void + */ + public function testIsEmojiPost(bool $expected, string $body) + { + $this->assertEquals($expected, Smilies::isEmojiPost($body)); + } }