MDL-76362 core: Short-circuit strip_querystring on empty values

This commit is contained in:
Andrew Nicols 2023-01-06 22:36:54 +08:00
parent b0aa0b63e0
commit 19bedb8eba
2 changed files with 32 additions and 3 deletions

View File

@ -23,9 +23,7 @@
* @author T.J.Hunt@open.ac.uk
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
*/
class weblib_test extends advanced_testcase {
/**
* @covers ::format_string
*/
@ -1119,4 +1117,32 @@ EXPECTED;
],
];
}
/**
* Data provider for strip_querystring tests.
*
* @return array
*/
public function strip_querystring_provider(): array {
return [
'Null' => [null, ''],
'Empty string' => ['', ''],
'No querystring' => ['https://example.com', 'https://example.com'],
'Querystring' => ['https://example.com?foo=bar', 'https://example.com'],
'Querystring with fragment' => ['https://example.com?foo=bar#baz', 'https://example.com'],
'Querystring with fragment and path' => ['https://example.com/foo/bar?foo=bar#baz', 'https://example.com/foo/bar'],
];
}
/**
* Test the strip_querystring function with various exampels.
*
* @dataProvider strip_querystring_provider
* @param mixed $value
* @param mixed $expected
* @covers ::strip_querystring
*/
public function test_strip_querystring($value, $expected): void {
$this->assertEquals($expected, strip_querystring($value));
}
}

View File

@ -148,8 +148,11 @@ function addslashes_js($var) {
* @return string The remaining URL.
*/
function strip_querystring($url) {
if ($url === null || $url === '') {
return '';
}
if ($commapos = strpos($url ?? '', '?')) {
if ($commapos = strpos($url, '?')) {
return substr($url, 0, $commapos);
} else {
return $url;