From 27ea958dfead810b2ce22be0d227b3fe876c3980 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Thu, 29 Apr 2021 14:56:23 +0800 Subject: [PATCH 1/2] MDL-71343 core_h5p: Add unit test for helper::parse_js_array Signed-off-by: Rajneel Totaram --- h5p/tests/helper_test.php | 47 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/h5p/tests/helper_test.php b/h5p/tests/helper_test.php index a58f61a0a36..65b440849ab 100644 --- a/h5p/tests/helper_test.php +++ b/h5p/tests/helper_test.php @@ -36,7 +36,7 @@ use advanced_testcase; * @copyright 2019 Sara Arjona * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class helper_testcase extends \advanced_testcase { +class helper_test extends \advanced_testcase { /** * Test the behaviour of get_display_options(). @@ -379,4 +379,49 @@ class helper_testcase extends \advanced_testcase { $helperfile = helper::get_export_info('nofileexist.h5p', $url); $this->assertNull($helperfile); } + + /** + * Test the parse_js_array function with a range of content. + * + * @dataProvider parse_js_array_provider + * @param string $content + * @param array $expected + */ + public function test_parse_js_array(string $content, array $expected): void { + $this->assertEquals($expected, helper::parse_js_array($content)); + } + + /** + * Data provider for test_parse_js_array(). + * + * @return array + */ + public function parse_js_array_provider(): array { + $lines = [ + "{", + " missingTranslation: '[Missing translation :key]',", + " loading: 'Loading, please wait...',", + " selectLibrary: 'Select the library you wish to use for your content.',", + "}", + ]; + $expected = [ + 'missingTranslation' => '[Missing translation :key]', + 'loading' => 'Loading, please wait...', + 'selectLibrary' => 'Select the library you wish to use for your content.', + ]; + return [ + 'Strings with \n' => [ + implode("\n", $lines), + $expected, + ], + 'Strings with \r\n' => [ + implode("\r\n", $lines), + $expected, + ], + 'Strings with \r' => [ + implode("\r", $lines), + $expected, + ], + ]; + } } From 2d86d8701277b50d5b2ce2863ca2c2a22c3a4600 Mon Sep 17 00:00:00 2001 From: Rajneel Totaram Date: Thu, 29 Apr 2021 23:40:10 +1200 Subject: [PATCH 2/2] MDL-71343 core_h5p: Convert all line-endings to UNIX format --- h5p/classes/helper.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/h5p/classes/helper.php b/h5p/classes/helper.php index 2cf6598c0d9..d8651d2e6e8 100644 --- a/h5p/classes/helper.php +++ b/h5p/classes/helper.php @@ -421,6 +421,8 @@ class helper { * @return array The JS array converted to PHP array. */ public static function parse_js_array(string $jscontent): array { + // Convert all line-endings to UNIX format first. + $jscontent = str_replace(array("\r\n", "\r"), "\n", $jscontent); $jsarray = preg_split('/,\n\s+/', substr($jscontent, 0, -1)); $jsarray = preg_replace('~{?\\n~', '', $jsarray);