Skip to content

Commit cf27019

Browse files
printminion-cobromiesTM
authored andcommitted
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>
1 parent e8e09e8 commit cf27019

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

apps/files/lib/Service/UserConfig.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,12 @@ public function setConfig(string $key, $value): void {
115115
throw new \InvalidArgumentException('Unknown config key');
116116
}
117117

118-
if (!in_array($value, $this->getAllowedConfigValues($key))) {
118+
$isBoolValue = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
119+
if ($isBoolValue !== null) {
120+
$value = $isBoolValue;
121+
}
122+
123+
if (!in_array($value, $this->getAllowedConfigValues($key), true)) {
119124
throw new \InvalidArgumentException('Invalid config value');
120125
}
121126

apps/files/tests/Service/UserConfigTest.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,37 @@ public function testThrowsInvalidArgumentExceptionForUnknownConfigKey(): void {
112112
$userConfig->setConfig('unknown_key', true);
113113
}
114114

115-
public function testSetsConfigSuccessfully(): void {
115+
public function testThrowsInvalidArgumentExceptionForInvalidConfigValue(): void {
116+
$this->expectException(\InvalidArgumentException::class);
117+
$this->expectExceptionMessage('Invalid config value');
118+
119+
$userConfig = new UserConfig($this->configMock, $this->userSessionMock);
120+
$userConfig->setConfig('crop_image_previews', 'foo');
121+
}
122+
123+
public static function validBoolConfigValues(): array {
124+
return [
125+
['true', '1'],
126+
['false', '0'],
127+
['1', '1'],
128+
['0', '0'],
129+
['yes', '1'],
130+
['no', '0'],
131+
[true, '1'],
132+
[false, '0'],
133+
];
134+
}
135+
136+
/**
137+
* @dataProvider validBoolConfigValues
138+
*/
139+
public function testSetsConfigWithBooleanValuesSuccessfully($boolValue, $expectedValue): void {
116140
$this->configMock->expects($this->once())
117141
->method('setUserValue')
118-
->with($this->userUID, Application::APP_ID, 'crop_image_previews', '1');
142+
->with($this->userUID, Application::APP_ID, 'crop_image_previews', $expectedValue);
143+
119144
$userConfig = new UserConfig($this->configMock, $this->userSessionMock);
120-
$userConfig->setConfig('crop_image_previews', true);
145+
$userConfig->setConfig('crop_image_previews', $boolValue);
121146
}
122147

123148
public function testGetsConfigsWithDefaultValuesSuccessfully(): void {

0 commit comments

Comments
 (0)