Skip to content

Commit a9f545c

Browse files
MAGETWO-69430: Magento allows to save not valid data using config:set command
1 parent 3b5769e commit a9f545c

File tree

2 files changed

+24
-30
lines changed

2 files changed

+24
-30
lines changed

app/code/Magento/Config/Console/Command/ConfigSet/LockProcessor.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,24 @@ public function process($path, $value, $scope, $scopeCode)
8181
$backendModel = $this->preparedValueFactory->create($path, $value, $scope, $scopeCode);
8282

8383
if ($backendModel instanceof Value) {
84-
$resourceModel = $backendModel->getResource();
85-
$resourceModel->save($backendModel);
84+
/**
85+
* Temporary solution until Magento introduce unified interface
86+
* for storing system configuration into database and configuration files.
87+
*/
88+
$backendModel->validateBeforeSave();
89+
$backendModel->beforeSave();
8690

8791
$value = $backendModel->getValue();
8892

93+
$backendModel->afterSave();
94+
95+
/**
96+
* Because FS does not support transactions,
97+
* we'll write value just after all validations are triggered.
98+
*/
8999
$this->deploymentConfigWriter->saveConfig(
90-
[ConfigFilePool::APP_ENV => $this->arrayManager->set($configPath, [], $value)]
100+
[ConfigFilePool::APP_ENV => $this->arrayManager->set($configPath, [], $value)],
101+
false
91102
);
92103
}
93104
} catch (\Exception $exception) {

app/code/Magento/Config/Test/Unit/Console/Command/ConfigSet/LockProcessorTest.php

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use Magento\Framework\App\DeploymentConfig;
1414
use Magento\Framework\Config\File\ConfigFilePool;
1515
use Magento\Framework\Exception\FileSystemException;
16-
use Magento\Framework\Model\ResourceModel\AbstractResource;
1716
use Magento\Framework\Stdlib\ArrayManager;
1817
use Magento\Store\Model\ScopeInterface;
1918
use PHPUnit_Framework_MockObject_MockObject as Mock;
@@ -56,11 +55,6 @@ class LockProcessorTest extends \PHPUnit_Framework_TestCase
5655
*/
5756
private $valueMock;
5857

59-
/**
60-
* @var AbstractResource|Mock
61-
*/
62-
private $resourceMock;
63-
6458
/**
6559
* @inheritdoc
6660
*/
@@ -79,13 +73,9 @@ protected function setUp()
7973
->disableOriginalConstructor()
8074
->getMock();
8175
$this->valueMock = $this->getMockBuilder(Value::class)
82-
->setMethods(['save', 'getResource', 'setValue', 'getValue', 'afterSave'])
76+
->setMethods(['validateBeforeSave', 'beforeSave', 'setValue', 'getValue', 'afterSave'])
8377
->disableOriginalConstructor()
8478
->getMock();
85-
$this->resourceMock = $this->getMockBuilder(AbstractResource::class)
86-
->setMethods(['save'])
87-
->disableOriginalConstructor()
88-
->getMockForAbstractClass();
8979

9080
$this->model = new LockProcessor(
9181
$this->preparedValueFactory,
@@ -130,12 +120,6 @@ public function testProcess($path, $value, $scope, $scopeCode)
130120
$this->valueMock->expects($this->once())
131121
->method('getValue')
132122
->willReturn($value);
133-
$this->valueMock->expects($this->once())
134-
->method('getResource')
135-
->willReturn($this->resourceMock);
136-
$this->resourceMock->expects($this->once())
137-
->method('save')
138-
->with($this->valueMock);
139123
$this->deploymentConfigWriterMock->expects($this->once())
140124
->method('saveConfig')
141125
->with(
@@ -154,6 +138,12 @@ public function testProcess($path, $value, $scope, $scopeCode)
154138
],
155139
false
156140
);
141+
$this->valueMock->expects($this->once())
142+
->method('validateBeforeSave');
143+
$this->valueMock->expects($this->once())
144+
->method('beforeSave');
145+
$this->valueMock->expects($this->once())
146+
->method('afterSave');
157147

158148
$this->model->process($path, $value, $scope, $scopeCode);
159149
}
@@ -185,12 +175,6 @@ public function testProcessNotReadableFs()
185175
$this->valueMock->expects($this->once())
186176
->method('getValue')
187177
->willReturn($value);
188-
$this->valueMock->expects($this->once())
189-
->method('getResource')
190-
->willReturn($this->resourceMock);
191-
$this->resourceMock->expects($this->once())
192-
->method('save')
193-
->with($this->valueMock);
194178
$this->configPathResolver->expects($this->once())
195179
->method('resolve')
196180
->willReturn('system/default/test/test/test');
@@ -223,10 +207,9 @@ public function testCustomException()
223207
$this->arrayManagerMock->expects($this->never())
224208
->method('set');
225209
$this->valueMock->expects($this->once())
226-
->method('getResource')
227-
->willReturn($this->resourceMock);
228-
$this->resourceMock->expects($this->once())
229-
->method('save')
210+
->method('getValue');
211+
$this->valueMock->expects($this->once())
212+
->method('afterSave')
230213
->willThrowException(new \Exception('Invalid values'));
231214
$this->deploymentConfigWriterMock->expects($this->never())
232215
->method('saveConfig');

0 commit comments

Comments
 (0)