Skip to content

Commit

Permalink
fix(files): strict check of default values
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
printminion-co authored and bromiesTM committed Dec 13, 2024
1 parent 31908e2 commit 8869c73
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
7 changes: 6 additions & 1 deletion apps/files/lib/Service/UserConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,12 @@ public function setConfig(string $key, $value): void {
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');
}

Expand Down
31 changes: 28 additions & 3 deletions apps/files/tests/Service/UserConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,37 @@ public function testThrowsInvalidArgumentExceptionForUnknownConfigKey(): void {
$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 {
Expand Down

0 comments on commit 8869c73

Please sign in to comment.