From 2870f42ca2d5c1fc75e6ce32ed5d2839e8e51425 Mon Sep 17 00:00:00 2001 From: Philipp Holzer Date: Wed, 23 Oct 2019 00:14:47 +0200 Subject: [PATCH] Move bb_translate_video - To new Class BBCode\Video - Adding tests - Make BaseObject::getClass() public --- include/text.php | 16 -------- mod/item.php | 7 ++-- src/BaseObject.php | 2 +- src/Content/Text/BBCode/Video.php | 32 +++++++++++++++ tests/src/Content/Text/BBCode/VideoTest.php | 43 +++++++++++++++++++++ 5 files changed, 80 insertions(+), 20 deletions(-) create mode 100644 src/Content/Text/BBCode/Video.php create mode 100644 tests/src/Content/Text/BBCode/VideoTest.php diff --git a/include/text.php b/include/text.php index 0935c5e74..6eb1e46ff 100644 --- a/include/text.php +++ b/include/text.php @@ -223,22 +223,6 @@ function return_bytes($size_str) { } } -function bb_translate_video($s) { - - $matches = null; - $r = preg_match_all("/\[video\](.*?)\[\/video\]/ism",$s,$matches,PREG_SET_ORDER); - if ($r) { - foreach ($matches as $mtch) { - if ((stristr($mtch[1], 'youtube')) || (stristr($mtch[1], 'youtu.be'))) { - $s = str_replace($mtch[0], '[youtube]' . $mtch[1] . '[/youtube]', $s); - } elseif (stristr($mtch[1], 'vimeo')) { - $s = str_replace($mtch[0], '[vimeo]' . $mtch[1] . '[/vimeo]', $s); - } - } - } - return $s; -} - /// @TODO Rewrite this function is_a_date_arg($s) { $i = intval($s); diff --git a/mod/item.php b/mod/item.php index 7c8ebee4a..c9a33cc20 100644 --- a/mod/item.php +++ b/mod/item.php @@ -24,8 +24,8 @@ use Friendica\Core\Hook; use Friendica\Core\L10n; use Friendica\Core\Logger; use Friendica\Core\Protocol; -use Friendica\Core\System; use Friendica\Core\Session; +use Friendica\Core\System; use Friendica\Core\Worker; use Friendica\Database\DBA; use Friendica\Model\Attach; @@ -499,8 +499,9 @@ function item_post(App $a) { $objecttype = ACTIVITY_OBJ_BOOKMARK; } - $body = bb_translate_video($body); - + /** @var BBCode\Video $bbCodeVideo */ + $bbCodeVideo = \Friendica\BaseObject::getClass(BBCode\Video::class); + $body = $bbCodeVideo->transform($body); // Fold multi-line [code] sequences $body = preg_replace('/\[\/code\]\s*\[code\]/ism', "\n", $body); diff --git a/src/BaseObject.php b/src/BaseObject.php index 996824f4a..204818845 100644 --- a/src/BaseObject.php +++ b/src/BaseObject.php @@ -54,7 +54,7 @@ class BaseObject * * @throws InternalServerErrorException */ - protected static function getClass(string $name) + public static function getClass(string $name) { if (empty(self::$dice)) { throw new InternalServerErrorException('DICE isn\'t initialized.'); diff --git a/src/Content/Text/BBCode/Video.php b/src/Content/Text/BBCode/Video.php new file mode 100644 index 000000000..b73ddce0b --- /dev/null +++ b/src/Content/Text/BBCode/Video.php @@ -0,0 +1,32 @@ + [ + 'input' => '[video]https://youtube.link/4523[/video]', + 'assert' => '[youtube]https://youtube.link/4523[/youtube]', + ], + 'youtu.be' => [ + 'input' => '[video]https://youtu.be.link/4523[/video]', + 'assert' => '[youtube]https://youtu.be.link/4523[/youtube]', + ], + 'vimeo' => [ + 'input' => '[video]https://vimeo.link/2343[/video]', + 'assert' => '[vimeo]https://vimeo.link/2343[/vimeo]', + ], + 'mixed' => [ + 'input' => '[video]https://vimeo.link/2343[/video] With other [b]string[/b] [video]https://youtu.be/blaa[/video]', + 'assert' => '[vimeo]https://vimeo.link/2343[/vimeo] With other [b]string[/b] [youtube]https://youtu.be/blaa[/youtube]', + ] + ]; + } + + /** + * Test if the BBCode is successfully transformed for video links + * + * @dataProvider dataVideo + */ + public function testTransform(string $input, string $assert) + { + $bbCodeVideo = new Video(); + + $this->assertEquals($assert, $bbCodeVideo->transform($input)); + } +}