From f42ee80142e50ab0d7365a08d0b81b26e4603a02 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Thu, 29 Apr 2021 14:56:23 +0800 Subject: [PATCH 1/1] MDL-71343 h5p: Add unit test for helper::parse_js_array --- h5p/tests/helper_test.php | 43 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/h5p/tests/helper_test.php b/h5p/tests/helper_test.php index 8e20542a59..e7ed15c699 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,45 @@ 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 srray $expected + */ + public function test_parse_js_array(string $content, array $expected): void { + $this->assertEquals($expected, helper::parse_js_array($content)); + } + + + 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, + ], + ]; + } } -- 2.30.0