2019-07-09 15:44:02 -04:00
|
|
|
<?php
|
|
|
|
/**
|
2024-01-02 15:57:26 -05:00
|
|
|
* @copyright Copyright (C) 2010-2024, the Friendica project
|
2022-01-06 18:30:59 -05:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
2019-07-09 15:44:02 -04:00
|
|
|
* Created by PhpStorm.
|
|
|
|
* User: benlo
|
|
|
|
* Date: 25/03/19
|
|
|
|
* Time: 21:36
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Friendica\Test\src\Content;
|
|
|
|
|
|
|
|
use Friendica\Content\Smilies;
|
2023-11-25 10:29:39 -05:00
|
|
|
use Friendica\Core\Hook;
|
2021-11-28 06:59:42 -05:00
|
|
|
use Friendica\DI;
|
2020-10-18 14:31:57 -04:00
|
|
|
use Friendica\Network\HTTPException\InternalServerErrorException;
|
2021-11-28 06:59:42 -05:00
|
|
|
use Friendica\Test\FixtureTest;
|
2019-07-09 15:44:02 -04:00
|
|
|
|
2021-11-28 06:59:42 -05:00
|
|
|
class SmiliesTest extends FixtureTest
|
2019-07-09 15:44:02 -04:00
|
|
|
{
|
2021-04-01 17:04:30 -04:00
|
|
|
protected function setUp(): void
|
2019-07-09 15:44:02 -04:00
|
|
|
{
|
|
|
|
parent::setUp();
|
2021-11-28 06:59:42 -05:00
|
|
|
|
|
|
|
DI::config()->set('system', 'no_smilies', false);
|
2023-11-25 10:29:39 -05:00
|
|
|
|
|
|
|
Hook::register('smilie', 'tests/Util/SmileyWhitespaceAddon.php', 'add_test_unicode_smilies');
|
|
|
|
Hook::loadHooks();
|
2019-07-09 15:44:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function dataLinks()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
/** @see https://github.com/friendica/friendica/pull/6933 */
|
|
|
|
'bug-6933-1' => [
|
|
|
|
'data' => '<code>/</code>',
|
|
|
|
'smilies' => ['texts' => [], 'icons' => []],
|
|
|
|
'expected' => '<code>/</code>',
|
|
|
|
],
|
|
|
|
'bug-6933-2' => [
|
|
|
|
'data' => '<code>code</code>',
|
|
|
|
'smilies' => ['texts' => [], 'icons' => []],
|
|
|
|
'expected' => '<code>code</code>',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test replace smilies in different texts
|
2020-10-18 14:31:57 -04:00
|
|
|
*
|
2019-07-09 15:44:02 -04:00
|
|
|
* @dataProvider dataLinks
|
|
|
|
*
|
|
|
|
* @param string $text Test string
|
|
|
|
* @param array $smilies List of smilies to replace
|
|
|
|
* @param string $expected Expected result
|
2020-10-18 14:31:57 -04:00
|
|
|
*
|
|
|
|
* @throws InternalServerErrorException
|
2019-07-09 15:44:02 -04:00
|
|
|
*/
|
2020-10-18 14:31:57 -04:00
|
|
|
public function testReplaceFromArray(string $text, array $smilies, string $expected)
|
2019-07-09 15:44:02 -04:00
|
|
|
{
|
|
|
|
$output = Smilies::replaceFromArray($text, $smilies);
|
2020-10-17 08:19:57 -04:00
|
|
|
self::assertEquals($expected, $output);
|
2019-07-09 15:44:02 -04:00
|
|
|
}
|
2023-08-29 22:16:09 -04:00
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
2023-11-15 10:53:38 -05:00
|
|
|
|
|
|
|
|
|
|
|
public function dataReplace(): array
|
|
|
|
{
|
2023-11-16 00:31:31 -05:00
|
|
|
$data = [
|
2023-11-15 10:53:38 -05:00
|
|
|
'simple-1' => [
|
|
|
|
'expected' => 'alt=":-p"',
|
|
|
|
'body' => ':-p',
|
|
|
|
],
|
|
|
|
'simple-1' => [
|
|
|
|
'expected' => 'alt=":-p"',
|
|
|
|
'body' => ' :-p ',
|
|
|
|
],
|
|
|
|
'word-boundary-1' => [
|
|
|
|
'expected' => ':-pppp',
|
|
|
|
'body' => ':-pppp',
|
|
|
|
],
|
|
|
|
'word-boundary-2' => [
|
|
|
|
'expected' => '~friendicaca',
|
|
|
|
'body' => '~friendicaca',
|
|
|
|
],
|
|
|
|
'symbol-boundary-1' => [
|
2023-11-16 00:31:31 -05:00
|
|
|
'expected' => 'alt=":-p"',
|
2023-11-15 10:53:38 -05:00
|
|
|
'body' => '(:-p)',
|
|
|
|
],
|
|
|
|
'hearts-1' => [
|
|
|
|
'expected' => '❤ (❤) ❤',
|
|
|
|
'body' => '<3 (<3) <3',
|
|
|
|
],
|
|
|
|
'hearts-8' => [
|
|
|
|
'expected' => '(❤❤❤❤❤❤❤❤)',
|
|
|
|
'body' => '(<33333333)',
|
|
|
|
],
|
|
|
|
'no-hearts-1' => [
|
|
|
|
'expected' => '(<30)',
|
|
|
|
'body' => '(<30)',
|
|
|
|
],
|
|
|
|
'no-hearts-2' => [
|
|
|
|
'expected' => '(3<33)',
|
|
|
|
'body' => '(3<33)',
|
|
|
|
],
|
2023-11-25 10:29:39 -05:00
|
|
|
'space' => [
|
|
|
|
'expected' => 'alt="smiley-heart"',
|
|
|
|
'body' => ':smiley heart 333:',
|
|
|
|
],
|
|
|
|
'substitution-1' => [
|
|
|
|
'expected' => '🔥',
|
|
|
|
'body' => '⽕',
|
|
|
|
],
|
|
|
|
'substitution-2' => [
|
|
|
|
'expected' => '🤗',
|
|
|
|
'body' => ':hugging face:',
|
|
|
|
],
|
|
|
|
'substitution-3' => [
|
|
|
|
'expected' => '🤭',
|
|
|
|
'body' => ':face with hand over mouth:',
|
|
|
|
],
|
|
|
|
'mixed' => [
|
|
|
|
'expected' => '🔥 🤭 invalid:hugging face: 🤗',
|
|
|
|
'body' => '⽕ :face with hand over mouth: invalid:hugging face: :hugging face:',
|
|
|
|
],
|
2023-11-15 10:53:38 -05:00
|
|
|
];
|
2023-11-16 00:31:31 -05:00
|
|
|
foreach ([':-[', ':-D', 'o.O'] as $emoji) {
|
|
|
|
foreach (['A', '_', ':', '-'] as $prefix) {
|
|
|
|
foreach (['', ' ', 'A', ':', '-'] as $suffix) {
|
|
|
|
$no_smile = ($prefix !== '' && ctype_alnum($prefix)) || ($suffix !== '' && ctype_alnum($suffix));
|
|
|
|
$s = $prefix . $emoji . $suffix;
|
|
|
|
$data[] = [
|
|
|
|
'expected' => $no_smile ? $s : 'alt="' . $emoji . '"',
|
|
|
|
'body' => $s,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $data;
|
2023-11-15 10:53:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataReplace
|
|
|
|
*
|
|
|
|
* @param string $expected
|
|
|
|
* @param string $body
|
|
|
|
*/
|
|
|
|
public function testReplace(string $expected, string $body)
|
|
|
|
{
|
|
|
|
$result = Smilies::replace($body);
|
|
|
|
$this->assertStringContainsString($expected, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataExtractUsedSmilies(): array
|
|
|
|
{
|
|
|
|
return [
|
2023-11-16 00:31:31 -05:00
|
|
|
'symbols' => [
|
|
|
|
'expected' => ['p', 'heart', 'embarrassed', 'kiss'],
|
|
|
|
'body' => ':-p <3 ":-[:-"',
|
|
|
|
'normalized' => ':p: :heart: ":embarrassed::kiss:',
|
|
|
|
],
|
2023-11-15 10:53:38 -05:00
|
|
|
'single-smiley' => [
|
|
|
|
'expected' => ['like'],
|
|
|
|
'body' => ':like',
|
|
|
|
'normalized' => ':like:',
|
|
|
|
],
|
|
|
|
'multiple-smilies' => [
|
|
|
|
'expected' => ['like', 'dislike'],
|
|
|
|
'body' => ':like :dislike',
|
|
|
|
'normalized' => ':like: :dislike:',
|
|
|
|
],
|
|
|
|
'nosmile' => [
|
|
|
|
'expected' => [],
|
|
|
|
'body' => '[nosmile] :like :like',
|
|
|
|
'normalized' => '[nosmile] :like :like'
|
|
|
|
],
|
|
|
|
'in-code' => [
|
|
|
|
'expected' => [],
|
|
|
|
'body' => '[code]:like :like :like[/code]',
|
|
|
|
'normalized' => '[code]:like :like :like[/code]'
|
|
|
|
],
|
|
|
|
'~friendica' => [
|
|
|
|
'expected' => ['friendica'],
|
|
|
|
'body' => '~friendica',
|
|
|
|
'normalized' => ':friendica:'
|
|
|
|
],
|
2023-11-25 10:29:39 -05:00
|
|
|
'space' => [
|
|
|
|
'expected' => ['smileyheart333'],
|
|
|
|
'body' => ':smiley heart 333:',
|
|
|
|
'normalized' => ':smileyheart333:'
|
|
|
|
],
|
|
|
|
'substitution-1' => [
|
|
|
|
'expected' => [],
|
|
|
|
'body' => '⽕',
|
|
|
|
'normalized' => '🔥',
|
|
|
|
],
|
|
|
|
'substitution-2' => [
|
|
|
|
'expected' => [],
|
|
|
|
'body' => ':hugging face:',
|
|
|
|
'normalized' => '🤗',
|
|
|
|
],
|
|
|
|
'substitution-3' => [
|
|
|
|
'expected' => [],
|
|
|
|
'body' => ':face with hand over mouth:',
|
|
|
|
'normalized' => '🤭',
|
|
|
|
],
|
|
|
|
'mixed' => [
|
|
|
|
'expected' => [],
|
|
|
|
'body' => '⽕ :face with hand over mouth: invalid:hugging face: :hugging face:',
|
|
|
|
'normalized' => '🔥 🤭 invalid:hugging face: 🤗',
|
|
|
|
],
|
2023-11-15 10:53:38 -05:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataExtractUsedSmilies
|
|
|
|
*
|
|
|
|
* @param array $expected
|
|
|
|
* @param string $body
|
|
|
|
* @param stirng $normalized
|
|
|
|
*/
|
|
|
|
public function testExtractUsedSmilies(array $expected, string $body, string $normalized)
|
|
|
|
{
|
2023-11-16 00:31:31 -05:00
|
|
|
$extracted = Smilies::extractUsedSmilies($body, $converted);
|
|
|
|
$expected = array_fill_keys($expected, true);
|
|
|
|
$this->assertEquals($normalized, $converted);
|
|
|
|
foreach (array_keys($extracted) as $shortcode) {
|
|
|
|
$this->assertArrayHasKey($shortcode, $expected);
|
2023-11-15 10:53:38 -05:00
|
|
|
}
|
2023-11-16 00:31:31 -05:00
|
|
|
$this->assertEquals(count($expected), count($extracted));
|
2023-11-15 10:53:38 -05:00
|
|
|
}
|
2019-07-09 15:44:02 -04:00
|
|
|
}
|