Skip to content

Commit

Permalink
Merge pull request #32512 from owncloud/config-system-set-json
Browse files Browse the repository at this point in the history
Support json for system:config:set
  • Loading branch information
Vincent Petry authored Aug 31, 2018
2 parents e911f01 + 68b532c commit adf4233
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
12 changes: 11 additions & 1 deletion core/Command/Config/System/SetConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected function configure() {
'type',
null,
InputOption::VALUE_REQUIRED,
'Value type [string, integer, double, boolean].',
'Value type [string, integer, double, boolean, json].',
'string'
)
->addOption(
Expand Down Expand Up @@ -162,6 +162,16 @@ protected function castValue($value, $type) {
'readable-value' => ($value === '') ? 'empty string' : 'string ' . $value,
];

case 'json':
$decodedJson = \json_decode($value, true);
if ($decodedJson === null) {
throw new \InvalidArgumentException('Unable to parse value as json');
}
return [
'value' => $decodedJson,
'readable-value' => 'json ' . $value,
];

default:
throw new \InvalidArgumentException('Invalid type');
}
Expand Down
43 changes: 43 additions & 0 deletions tests/Core/Command/Config/System/SetConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,44 @@ public function testSetUpdateOnly($configNames, $existingData) {
$this->invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
}

public function setJsonData() {
return [
[['name'], '{"sub-key":"value"}', null, ['sub-key' => 'value']],
[['name'], '{"sub-key":"value"}', 'something', ['sub-key' => 'value']],
[['name'], '{"sub-key":"value"}', ['sub-key' => 'old-value', 'other-key' => 'will disappear'], ['sub-key' => 'value']],
[['name'], '[{"key1":"value1","key2":"value2"}]', null, [['key1' => 'value1', 'key2' => 'value2']]],
];
}

/**
* @dataProvider setJsonData
*
* @param array $configNames
* @param string $newValue
* @param mixed $existingData
* @param mixed $expectedValue
*/
public function testSetJson($configNames, $newValue, $existingData, $expectedValue) {
$this->systemConfig->expects($this->once())
->method('setValue')
->with($configNames[0], $expectedValue);
$this->systemConfig->method('getValue')
->with($configNames[0])
->willReturn($existingData);

$this->consoleInput->expects($this->once())
->method('getArgument')
->with('name')
->willReturn($configNames);
$this->consoleInput->method('getOption')
->will($this->returnValueMap([
['value', $newValue],
['type', 'json'],
]));

$this->invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
}

public function castValueProvider() {
return [
[null, 'string', ['value' => '', 'readable-value' => 'empty string']],
Expand All @@ -138,6 +176,9 @@ public function castValueProvider() {

['true', 'boolean', ['value' => true, 'readable-value' => 'boolean true']],
['false', 'bool', ['value' => false, 'readable-value' => 'boolean false']],

['{"config_key":"the-value"}', 'json', ['value' => ['config_key' => 'the-value'], 'readable-value' => 'json {"config_key":"the-value"}']],
['[{"key1":"value1","key2":"value2"}]', 'json', ['value' => [['key1' => 'value1', 'key2' => 'value2']], 'readable-value' => 'json [{"key1":"value1","key2":"value2"}]']],
];
}

Expand All @@ -159,6 +200,8 @@ public function castValueInvalidProvider() {
['76ggg', 'double'],
['true', 'float'],
['foobar', 'boolean'],
['invalid-json', 'json'],
['', 'json'],
];
}

Expand Down

0 comments on commit adf4233

Please sign in to comment.