Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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 <[email protected]>
  • Loading branch information
printminion-co authored and bromiesTM committed Dec 12, 2024
commit efa9c9db722d2668d4ccfe861c60f99c9bdc16cc
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
23 changes: 20 additions & 3 deletions apps/files/tests/Service/UserConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,29 @@ public function testThrowsInvalidArgumentExceptionForUnknownConfigKey(): void {
$userConfig->setConfig('unknown_key', true);
}

public function testSetsConfigSuccessfully(): void {
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