MDL-87941 core: handle param validation of boolean type false.
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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');
|
||||
|
||||
Reference in New Issue
Block a user