fix: copy SetConfig.php and SetConfigTest.php from master to backport branch

Signed-off-by: yemkareems <yemkareems@gmail.com>
This commit is contained in:
yemkareems 2025-08-26 19:40:56 +05:30
parent 43e38a120f
commit bc64573fb5
No known key found for this signature in database
GPG key ID: 4293DA00B9478934
2 changed files with 79 additions and 4 deletions

View file

@ -81,6 +81,80 @@ class SetConfig extends Base {
return 0;
}
/**
* @param string $value
* @param string $type
* @return mixed
* @throws \InvalidArgumentException
*/
protected function castValue($value, $type) {
switch ($type) {
case 'integer':
case 'int':
if (!is_numeric($value)) {
throw new \InvalidArgumentException('Non-numeric value specified');
}
return [
'value' => (int)$value,
'readable-value' => 'integer ' . (int)$value,
];
case 'double':
case 'float':
if (!is_numeric($value)) {
throw new \InvalidArgumentException('Non-numeric value specified');
}
return [
'value' => (float)$value,
'readable-value' => 'double ' . (float)$value,
];
case 'boolean':
case 'bool':
$value = strtolower($value);
switch ($value) {
case 'true':
return [
'value' => true,
'readable-value' => 'boolean ' . $value,
];
case 'false':
return [
'value' => false,
'readable-value' => 'boolean ' . $value,
];
default:
throw new \InvalidArgumentException('Unable to parse value as boolean');
}
// no break
case 'null':
return [
'value' => null,
'readable-value' => 'null',
];
case 'string':
$value = (string)$value;
return [
'value' => $value,
'readable-value' => ($value === '') ? 'empty string' : 'string ' . $value,
];
case 'json':
$value = json_decode($value, true);
return [
'value' => $value,
'readable-value' => 'json ' . json_encode($value),
];
default:
throw new \InvalidArgumentException('Invalid type');
}
}
/**
* @param array $configNames
* @param mixed $existingValues

View file

@ -1,4 +1,5 @@
<?php
/**
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
@ -56,7 +57,7 @@ class SetConfigTest extends TestCase {
* @param mixed $existingData
* @param mixed $expectedValue
*/
public function testSet($configNames, $newValue, $existingData, $expectedValue): void {
public function testSet($configNames, $newValue, $existingData, $expectedValue) {
$this->systemConfig->expects($this->once())
->method('setValue')
->with($configNames[0], $expectedValue);
@ -89,7 +90,7 @@ class SetConfigTest extends TestCase {
/**
* @dataProvider setUpdateOnlyProvider
*/
public function testSetUpdateOnly($configNames, $existingData): void {
public function testSetUpdateOnly($configNames, $existingData) {
$this->expectException(\UnexpectedValueException::class);
$this->systemConfig->expects($this->never())
@ -136,7 +137,7 @@ class SetConfigTest extends TestCase {
/**
* @dataProvider castValueProvider
*/
public function testCastValue($value, $type, $expectedValue): void {
public function testCastValue($value, $type, $expectedValue) {
$this->assertSame($expectedValue,
$this->invokePrivate($this->command, 'castValue', [$value, $type])
);
@ -157,7 +158,7 @@ class SetConfigTest extends TestCase {
/**
* @dataProvider castValueInvalidProvider
*/
public function testCastValueInvalid($value, $type): void {
public function testCastValueInvalid($value, $type) {
$this->expectException(\InvalidArgumentException::class);
$this->invokePrivate($this->command, 'castValue', [$value, $type]);