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 788d86d7a3
commit dbfb5eaa45
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
*/
@ -985,4 +983,32 @@ EXPECTED;
public function test_get_html_lang_attribute_value(string $langcode, string $expected): void {
$this->assertEquals($expected, get_html_lang_attribute_value($langcode));
}
/**
* 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;