From e66f04e61d034a65a5c1a6b0cb4b8c1cc3b055c5 Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Thu, 6 Jul 2023 21:10:11 +0100 Subject: [PATCH 1/2] MDL-69187 files: normalize retrieval of mimetypes from groups array. Ensure that the following both return consistently, so that filepicker form elements behave the same for each when defining "accepted_types": '.html, .txt' ['.html', '.txt'] --- lib/filelib.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/filelib.php b/lib/filelib.php index 7c50999b30e..ad5fb962c63 100644 --- a/lib/filelib.php +++ b/lib/filelib.php @@ -2068,9 +2068,12 @@ function get_mimetype_description($obj, $capitalise=false) { */ function file_get_typegroup($element, $groups) { static $cached = array(); + + // Turn groups into a list. if (!is_array($groups)) { - $groups = array($groups); + $groups = preg_split('/[\s,;:"\']+/', $groups, -1, PREG_SPLIT_NO_EMPTY); } + if (!array_key_exists($element, $cached)) { $cached[$element] = array(); } From 1e89462692a231091bc98205d81b9b7796e72e1e Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Tue, 22 Aug 2023 22:47:32 +0800 Subject: [PATCH 2/2] MDL-69187 core_file: Unit test for file_get_typegroup string types --- lib/tests/filelib_test.php | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/lib/tests/filelib_test.php b/lib/tests/filelib_test.php index 32dc34535c9..d2fb2eaf1d6 100644 --- a/lib/tests/filelib_test.php +++ b/lib/tests/filelib_test.php @@ -2010,6 +2010,42 @@ EOF; ['noclean' => true, 'context' => $syscontext], $syscontext, 'core', 'some', 1); $this->assertSame($text, $result->some); } + + /** + * Tests for file_get_typegroup to check that both arrays, and string values are accepted. + * + * @dataProvider file_get_typegroup_provider + * @param string|array $group + * @param string $expected + */ + public function test_file_get_typegroup( + string|array $group, + string $expected, + ): void { + $result = file_get_typegroup('type', $group); + $this->assertContains($expected, $result); + } + + public static function file_get_typegroup_provider(): array { + return [ + 'Array of values' => [ + ['.html', '.htm'], + 'text/html', + ], + 'String of comma-separated values' => [ + '.html, .htm', + 'text/html', + ], + 'String of colon-separated values' => [ + '.html : .htm', + 'text/html', + ], + 'String of semi-colon-separated values' => [ + '.html ; .htm', + 'text/html', + ], + ]; + } } /**