MDL-87941 core: handle param validation of boolean type false.

This commit is contained in:
Paul Holden
2026-02-12 17:34:20 +00:00
parent dbf9b2bc1f
commit 3e7fd69d3f
2 changed files with 21 additions and 0 deletions
+4
View File
@@ -615,6 +615,10 @@ enum param: string {
} else if (!is_numeric($param) || !preg_match('/^[\+-]?[0-9]*\.?[0-9]*(e[-+]?[0-9]+)?$/i', (string)$param)) {
throw new invalid_parameter_exception($debuginfo);
}
} else if ($this->canonical() === self::BOOL) {
if ($param != $cleaned) {
throw new invalid_parameter_exception($debuginfo);
}
} else if ((string) $param !== (string) $cleaned) {
// Conversion to string is usually lossless.
throw new invalid_parameter_exception($debuginfo);
+17
View File
@@ -421,6 +421,17 @@ final class moodlelib_test extends \advanced_testcase {
$this->assertSame('', clean_param(null, PARAM_ALPHAEXT));
}
/**
* @covers \core\param
* @covers \clean_param
*/
public function test_clean_param_bool(): void {
$this->assertSame(0, clean_param(false, PARAM_BOOL));
$this->assertSame(0, clean_param(0, PARAM_BOOL));
$this->assertSame(1, clean_param(true, PARAM_BOOL));
$this->assertSame(1, clean_param(1, PARAM_BOOL));
}
/**
* @covers \core\param
* @covers \clean_param
@@ -948,6 +959,12 @@ final class moodlelib_test extends \advanced_testcase {
validate_param('.1e+10', PARAM_FLOAT);
validate_param('1E-1', PARAM_FLOAT);
// Make sure bools do not cause exceptions.
validate_param(false, PARAM_BOOL);
validate_param(0, PARAM_BOOL);
validate_param(true, PARAM_BOOL);
validate_param(1, PARAM_BOOL);
try {
$param = validate_param('1,2', PARAM_FLOAT);
$this->fail('invalid_parameter_exception expected');