]> git.mxchange.org Git - friendica.git/commitdiff
Add unit tests for Smilies::isEmojiPost
authorHypolite Petovan <hypolite@mrpetovan.com>
Wed, 30 Aug 2023 02:16:09 +0000 (22:16 -0400)
committerHypolite Petovan <hypolite@mrpetovan.com>
Wed, 30 Aug 2023 02:16:09 +0000 (22:16 -0400)
- Current implementation is failing tests with emojis including the zero-width-joiner character, encoded on 3 bytes only.

src/Content/Smilies.php
tests/src/Content/SmiliesTest.php

index b7abbd8e5fa125eda653decc01f32d018700dcaa..664a4089ce9722f07d35067eb553d8c4d31798a0 100644 (file)
@@ -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));
index a886f1ac0137e320a73f7c4c2f879e05005b4c1f..38eb743e854294c8f601c80084c40afa7c918339 100644 (file)
@@ -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' => '&nbsp;',
+                       ],
+                       'HTML entity else' => [
+                               'expected' => false,
+                               'body' => '&deg;',
+                       ],
+                       'emojis+HTML whitespace' => [
+                               'expected' => true,
+                               'body' => '๐Ÿ‘€&nbsp;๐Ÿคท',
+                       ],
+                       'emojis+HTML else' => [
+                               'expected' => false,
+                               'body' => '๐Ÿ‘€&lt;๐Ÿคท',
+                       ],
+                       'zwj' => [
+                               'expected' => true,
+                               'body' => '๐Ÿ‘จโ€๐Ÿ‘จโ€๐Ÿ‘งโ€',
+                       ],
+                       'zwj+whitespace' => [
+                               'expected' => true,
+                               'body' => ' ๐Ÿ‘จโ€๐Ÿ‘จโ€๐Ÿ‘งโ€ ',
+                       ],
+                       'zwj+HTML whitespace' => [
+                               'expected' => true,
+                               'body' => '&nbsp;๐Ÿ‘จโ€๐Ÿ‘จโ€๐Ÿ‘งโ€&nbsp;',
+                       ],
+               ];
+       }
+
+       /**
+        * @dataProvider dataIsEmojiPost
+        *
+        * @param bool   $expected
+        * @param string $body
+        * @return void
+        */
+       public function testIsEmojiPost(bool $expected, string $body)
+       {
+               $this->assertEquals($expected, Smilies::isEmojiPost($body));
+       }
 }