fix(files): strict check of default values

Not ok: in_array("foo", [true, false]); // returns true
ok: in_array("foo", [true, false], true); // returns false

Signed-off-by: Misha M.-Kupriyanov <kupriyanov@strato.de>
This commit is contained in:
Misha M.-Kupriyanov 2024-12-03 15:58:59 +01:00 committed by Kai Henseler
parent 26925dcb67
commit 49d10edb4d
2 changed files with 34 additions and 4 deletions

View file

@ -115,7 +115,12 @@ class UserConfig {
throw new \InvalidArgumentException('Unknown config key');
}
if (!in_array($value, $this->getAllowedConfigValues($key))) {
$isBoolValue = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
if ($isBoolValue !== null) {
$value = $isBoolValue;
}
if (!in_array($value, $this->getAllowedConfigValues($key), true)) {
throw new \InvalidArgumentException('Invalid config value');
}

View file

@ -112,12 +112,37 @@ class UserConfigTest extends \Test\TestCase {
$userConfig->setConfig('unknown_key', true);
}
public function testSetsConfigSuccessfully(): void {
public function testThrowsInvalidArgumentExceptionForInvalidConfigValue(): void {
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid config value');
$userConfig = new UserConfig($this->configMock, $this->userSessionMock);
$userConfig->setConfig('crop_image_previews', 'foo');
}
public static function validBoolConfigValues(): array {
return [
['true', '1'],
['false', '0'],
['1', '1'],
['0', '0'],
['yes', '1'],
['no', '0'],
[true, '1'],
[false, '0'],
];
}
/**
* @dataProvider validBoolConfigValues
*/
public function testSetsConfigWithBooleanValuesSuccessfully($boolValue, $expectedValue): void {
$this->configMock->expects($this->once())
->method('setUserValue')
->with($this->userUID, Application::APP_ID, 'crop_image_previews', '1');
->with($this->userUID, Application::APP_ID, 'crop_image_previews', $expectedValue);
$userConfig = new UserConfig($this->configMock, $this->userSessionMock);
$userConfig->setConfig('crop_image_previews', true);
$userConfig->setConfig('crop_image_previews', $boolValue);
}
public function testGetsConfigsWithDefaultValuesSuccessfully(): void {