From 900388856496f56ff2c6dea6ee63d8c49dccde50 Mon Sep 17 00:00:00 2001 From: David Lambauer Date: Thu, 13 Oct 2022 10:43:28 +0200 Subject: [PATCH] :sparkles: Added possibility to delete configs (#53) --- Model/Processor/ImportProcessor.php | 12 ++++++++++++ Test/Unit/Model/Processor/ImportProcessorTest.php | 8 +++++++- docs/config-import.md | 11 ++++++++++- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/Model/Processor/ImportProcessor.php b/Model/Processor/ImportProcessor.php index cd07587..4f45ffb 100644 --- a/Model/Processor/ImportProcessor.php +++ b/Model/Processor/ImportProcessor.php @@ -16,6 +16,7 @@ class ImportProcessor extends AbstractProcessor implements ImportProcessorInterface { + private const DELETE_CONFIG_FLAG = '!!DELETE'; /** * @var WriterInterface */ @@ -81,6 +82,17 @@ public function process() foreach ($configurations as $configPath => $configValues) { $scopeConfigValues = $this->transformConfigToScopeConfig($configPath, $configValues); foreach ($scopeConfigValues as $scopeConfigValue) { + if ($scopeConfigValue['value'] === self::DELETE_CONFIG_FLAG) { + $this->configWriter->delete( + $configPath, + $scopeConfigValue['scope'], + $this->scopeConverter->convert($scopeConfigValue['scope_id'], $scopeConfigValue['scope']) + ); + $this->getOutput()->writeln(sprintf('%s => %s', $configPath, 'DELETED')); + $valuesSet++; + continue; + } + $this->configWriter->save( $configPath, $scopeConfigValue['value'], diff --git a/Test/Unit/Model/Processor/ImportProcessorTest.php b/Test/Unit/Model/Processor/ImportProcessorTest.php index 7666e21..3f3ff24 100644 --- a/Test/Unit/Model/Processor/ImportProcessorTest.php +++ b/Test/Unit/Model/Processor/ImportProcessorTest.php @@ -121,6 +121,11 @@ public function process(): void 0 => 'ABC', ], ], + 'test/config/custom_field_to_be_deleted' => [ + 'default' => [ + 0 => '!!DELETE', + ], + ] ]; $readerMock = $this->getMockBuilder(YamlReader::class) @@ -128,8 +133,9 @@ public function process(): void ->getMock(); $readerMock->expects($this->once())->method('parse')->willReturn($parseResult); - $this->scopeValidatorMock->expects($this->once())->method('validate')->willReturn(true); + $this->scopeValidatorMock->expects($this->exactly(2))->method('validate')->willReturn(true); $this->configWriterMock->expects($this->once())->method('save'); + $this->configWriterMock->expects($this->once())->method('delete'); $processor = new ImportProcessor($this->configWriterMock, $this->scopeValidatorMock, $this->scopeConverterMock); $processor->setOutput($this->outputMock); diff --git a/docs/config-import.md b/docs/config-import.md index 4d3d36e..c8d3b0f 100644 --- a/docs/config-import.md +++ b/docs/config-import.md @@ -2,7 +2,6 @@ Import configuration values in Magento in an automated way instead of manually clicking through the store configuration. - ## Usage ```bash @@ -73,6 +72,16 @@ vendorx/general/api_key: You can then set the environment variable `VENDORX_API_KEY` in your CI/CD configuration to the secret API key. +### Delete Config + +Sometimes, it might be helpful to be able to delete certain config values and get back to the default behavior. To do so, your config value has to be a magic-ish string. + +```yaml +vendorx/general/api_key: + default: + 0: "!!DELETE" +``` + ### Recursive folder setup If you choose to store your configuration files in subdirectories, e.g. per vendor, the recommended folder setup should look like this: