diff --git a/core/command/config/system/deleteconfig.php b/core/command/config/system/deleteconfig.php index e64ff32ac7..374f5ac69b 100644 --- a/core/command/config/system/deleteconfig.php +++ b/core/command/config/system/deleteconfig.php @@ -48,8 +48,8 @@ class DeleteConfig extends Base { ->setDescription('Delete a system config value') ->addArgument( 'name', - InputArgument::REQUIRED, - 'Name of the config to delete' + InputArgument::REQUIRED | InputArgument::IS_ARRAY, + 'Name of the config to delete, specify multiple for array parameter' ) ->addOption( 'error-if-not-exists', @@ -61,15 +61,57 @@ class DeleteConfig extends Base { } protected function execute(InputInterface $input, OutputInterface $output) { - $configName = $input->getArgument('name'); + $configNames = $input->getArgument('name'); + $configName = $configNames[0]; - if ($input->hasParameterOption('--error-if-not-exists') && !in_array($configName, $this->systemConfig->getKeys())) { - $output->writeln('System config ' . $configName . ' could not be deleted because it did not exist'); - return 1; + if (sizeof($configNames) > 1) { + if ($input->hasParameterOption('--error-if-not-exists') && !in_array($configName, $this->systemConfig->getKeys())) { + $output->writeln('System config ' . implode(' => ', $configNames) . ' could not be deleted because it did not exist'); + return 1; + } + + $value = $this->systemConfig->getValue($configName); + + try { + $value = $this->removeSubValue(array_slice($configNames, 1), $value, $input->hasParameterOption('--error-if-not-exists')); + } + catch (\UnexpectedValueException $e) { + $output->writeln('System config ' . implode(' => ', $configNames) . ' could not be deleted because it did not exist'); + return 1; + } + + $this->systemConfig->setValue($configName, $value); + $output->writeln('System config value ' . implode(' => ', $configNames) . ' deleted'); + return 0; + } else { + if ($input->hasParameterOption('--error-if-not-exists') && !in_array($configName, $this->systemConfig->getKeys())) { + $output->writeln('System config ' . $configName . ' could not be deleted because it did not exist'); + return 1; + } + + $this->systemConfig->deleteValue($configName); + $output->writeln('System config value ' . $configName . ' deleted'); + return 0; + } + } + + protected function removeSubValue($keys, $currentValue, $throwError) { + $nextKey = array_shift($keys); + + if (is_array($currentValue)) { + if (isset($currentValue[$nextKey])) { + if (empty($keys)) { + unset($currentValue[$nextKey]); + } else { + $currentValue[$nextKey] = $this->removeSubValue($keys, $currentValue[$nextKey], $throwError); + } + } else if ($throwError) { + throw new \UnexpectedValueException('Config parameter does not exist'); + } + } else if ($throwError) { + throw new \UnexpectedValueException('Config parameter does not exist'); } - $this->systemConfig->deleteValue($configName); - $output->writeln('System config value ' . $configName . ' deleted'); - return 0; + return $currentValue; } } diff --git a/core/command/config/system/setconfig.php b/core/command/config/system/setconfig.php index f05b8d6ead..45ba01e928 100644 --- a/core/command/config/system/setconfig.php +++ b/core/command/config/system/setconfig.php @@ -79,7 +79,7 @@ class SetConfig extends Base { $configValue = $this->castValue($input->getOption('value'), $input->getOption('type')); $updateOnly = $input->getOption('update-only'); - if (count($configNames) > 1) { + if (sizeof($configNames) > 1) { $existingValue = $this->systemConfig->getValue($configName); $newValue = $this->mergeArrayValue( diff --git a/tests/core/command/config/system/deleteconfigtest.php b/tests/core/command/config/system/deleteconfigtest.php index aa1f93274c..11bfb6ae7a 100644 --- a/tests/core/command/config/system/deleteconfigtest.php +++ b/tests/core/command/config/system/deleteconfigtest.php @@ -50,32 +50,31 @@ class DeleteConfigTest extends TestCase { $this->command = new DeleteConfig($systemConfig); } - public function deleteData() { return [ [ - 'name', + 'name1', true, true, 0, 'info', ], [ - 'name', + 'name2', true, false, 0, 'info', ], [ - 'name', + 'name3', false, false, 0, 'info', ], [ - 'name', + 'name4', false, true, 1, @@ -105,7 +104,7 @@ class DeleteConfigTest extends TestCase { $this->consoleInput->expects($this->once()) ->method('getArgument') ->with('name') - ->willReturn($configName); + ->willReturn([$configName]); $this->consoleInput->expects($this->any()) ->method('hasParameterOption') ->with('--error-if-not-exists') @@ -117,4 +116,103 @@ class DeleteConfigTest extends TestCase { $this->assertSame($expectedReturn, $this->invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput])); } + + public function deleteArrayData() { + return [ + [ + ['name', 'sub'], + true, + false, + true, + true, + 0, + 'info', + ], + [ + ['name', 'sub', '2sub'], + true, + false, + ['sub' => ['2sub' => 1], 'sub2' => false], + ['sub' => [], 'sub2' => false], + 0, + 'info', + ], + [ + ['name', 'sub3'], + true, + false, + ['sub' => ['2sub' => 1], 'sub2' => false], + ['sub' => ['2sub' => 1], 'sub2' => false], + 0, + 'info', + ], + [ + ['name', 'sub'], + false, + true, + true, + true, + 1, + 'error', + ], + [ + ['name', 'sub'], + true, + true, + true, + true, + 1, + 'error', + ], + [ + ['name', 'sub3'], + true, + true, + ['sub' => ['2sub' => 1], 'sub2' => false], + ['sub' => ['2sub' => 1], 'sub2' => false], + 1, + 'error', + ], + ]; + } + + /** + * @dataProvider deleteArrayData + * + * @param string[] $configNames + * @param bool $configKeyExists + * @param bool $checkIfKeyExists + * @param mixed $configValue + * @param mixed $updateValue + * @param int $expectedReturn + * @param string $expectedMessage + */ + public function testArrayDelete(array $configNames, $configKeyExists, $checkIfKeyExists, $configValue, $updateValue, $expectedReturn, $expectedMessage) { + $this->systemConfig->expects(($checkIfKeyExists) ? $this->once() : $this->never()) + ->method('getKeys') + ->willReturn($configKeyExists ? [$configNames[0]] : []); + + $this->systemConfig->expects(($configKeyExists) ? $this->once() : $this->never()) + ->method('getValue') + ->willReturn($configValue); + + $this->systemConfig->expects(($expectedReturn === 0) ? $this->once() : $this->never()) + ->method('setValue') + ->with($configNames[0], $updateValue); + + $this->consoleInput->expects($this->once()) + ->method('getArgument') + ->with('name') + ->willReturn($configNames); + $this->consoleInput->expects($this->any()) + ->method('hasParameterOption') + ->with('--error-if-not-exists') + ->willReturn($checkIfKeyExists); + + $this->consoleOutput->expects($this->any()) + ->method('writeln') + ->with($this->stringContains($expectedMessage)); + + $this->assertSame($expectedReturn, $this->invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput])); + } }