Skip to content

Commit 7be5373

Browse files
author
Stanislav Idolov
authored
ENGCOM-2127: Rework for PR #16222 . #16393
2 parents bc779f8 + 681f80a commit 7be5373

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

app/code/Magento/Config/Model/Config.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
*/
66
namespace Magento\Config\Model;
77

8+
use Magento\Config\Model\Config\Reader\Source\Deployed\SettingChecker;
89
use Magento\Config\Model\Config\Structure\Element\Group;
910
use Magento\Config\Model\Config\Structure\Element\Field;
11+
use Magento\Framework\App\ObjectManager;
1012

1113
/**
1214
* Backend config model
@@ -80,6 +82,11 @@ class Config extends \Magento\Framework\DataObject
8082
*/
8183
protected $_storeManager;
8284

85+
/**
86+
* @var Config\Reader\Source\Deployed\SettingChecker
87+
*/
88+
private $settingChecker;
89+
8390
/**
8491
* @param \Magento\Framework\App\Config\ReinitableConfigInterface $config
8592
* @param \Magento\Framework\Event\ManagerInterface $eventManager
@@ -88,6 +95,7 @@ class Config extends \Magento\Framework\DataObject
8895
* @param \Magento\Config\Model\Config\Loader $configLoader
8996
* @param \Magento\Framework\App\Config\ValueFactory $configValueFactory
9097
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
98+
* @param Config\Reader\Source\Deployed\SettingChecker|null $settingChecker
9199
* @param array $data
92100
*/
93101
public function __construct(
@@ -98,6 +106,7 @@ public function __construct(
98106
\Magento\Config\Model\Config\Loader $configLoader,
99107
\Magento\Framework\App\Config\ValueFactory $configValueFactory,
100108
\Magento\Store\Model\StoreManagerInterface $storeManager,
109+
SettingChecker $settingChecker = null,
101110
array $data = []
102111
) {
103112
parent::__construct($data);
@@ -108,6 +117,7 @@ public function __construct(
108117
$this->_configLoader = $configLoader;
109118
$this->_configValueFactory = $configValueFactory;
110119
$this->_storeManager = $storeManager;
120+
$this->settingChecker = $settingChecker ?: ObjectManager::getInstance()->get(SettingChecker::class);
111121
}
112122

113123
/**
@@ -351,6 +361,16 @@ protected function _processGroup(
351361
// use extra memory
352362
$fieldsetData = [];
353363
foreach ($groupData['fields'] as $fieldId => $fieldData) {
364+
$isReadOnly = $this->settingChecker->isReadOnly(
365+
$groupPath . '/' . $fieldId,
366+
$this->getScope(),
367+
$this->getScopeCode()
368+
);
369+
370+
if ($isReadOnly) {
371+
continue;
372+
}
373+
354374
$field = $this->getField($sectionPath, $groupId, $fieldId);
355375
/** @var \Magento\Framework\App\Config\ValueInterface $backendModel */
356376
$backendModel = $field->hasBackendModel()

app/code/Magento/Config/Test/Unit/Model/ConfigTest.php

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ class ConfigTest extends \PHPUnit\Framework\TestCase
6060
*/
6161
protected $_configStructure;
6262

63+
/**
64+
* @var \PHPUnit_Framework_MockObject_MockObject
65+
*/
66+
private $_settingsChecker;
67+
6368
protected function setUp()
6469
{
6570
$this->_eventManagerMock = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
@@ -79,7 +84,7 @@ protected function setUp()
7984

8085
$this->_transFactoryMock = $this->createPartialMock(
8186
\Magento\Framework\DB\TransactionFactory::class,
82-
['create']
87+
['create', 'addObject']
8388
);
8489
$this->_appConfigMock = $this->createMock(\Magento\Framework\App\Config\ReinitableConfigInterface::class);
8590
$this->_configLoaderMock = $this->createPartialMock(
@@ -90,14 +95,18 @@ protected function setUp()
9095

9196
$this->_storeManager = $this->getMockForAbstractClass(\Magento\Store\Model\StoreManagerInterface::class);
9297

98+
$this->_settingsChecker = $this
99+
->createMock(\Magento\Config\Model\Config\Reader\Source\Deployed\SettingChecker::class);
100+
93101
$this->_model = new \Magento\Config\Model\Config(
94102
$this->_appConfigMock,
95103
$this->_eventManagerMock,
96104
$this->_configStructure,
97105
$this->_transFactoryMock,
98106
$this->_configLoaderMock,
99107
$this->_dataFactoryMock,
100-
$this->_storeManager
108+
$this->_storeManager,
109+
$this->_settingsChecker
101110
);
102111
}
103112

@@ -149,6 +158,49 @@ public function testSaveToCheckAdminSystemConfigChangedSectionEvent()
149158
$this->_model->save();
150159
}
151160

161+
public function testDoNotSaveReadOnlyFields()
162+
{
163+
$transactionMock = $this->createMock(\Magento\Framework\DB\Transaction::class);
164+
$this->_transFactoryMock->expects($this->any())->method('create')->will($this->returnValue($transactionMock));
165+
166+
$this->_settingsChecker->expects($this->any())->method('isReadOnly')->will($this->returnValue(true));
167+
$this->_configLoaderMock->expects($this->any())->method('getConfigByPath')->will($this->returnValue([]));
168+
169+
$this->_model->setGroups(['1' => ['fields' => ['key' => ['data']]]]);
170+
$this->_model->setSection('section');
171+
172+
$group = $this->createMock(\Magento\Config\Model\Config\Structure\Element\Group::class);
173+
$group->method('getPath')->willReturn('section/1');
174+
175+
$field = $this->createMock(\Magento\Config\Model\Config\Structure\Element\Field::class);
176+
$field->method('getGroupPath')->willReturn('section/1');
177+
$field->method('getId')->willReturn('key');
178+
179+
$this->_configStructure->expects($this->at(0))
180+
->method('getElement')
181+
->with('section/1')
182+
->will($this->returnValue($group));
183+
$this->_configStructure->expects($this->at(1))
184+
->method('getElement')
185+
->with('section/1')
186+
->will($this->returnValue($group));
187+
$this->_configStructure->expects($this->at(2))
188+
->method('getElement')
189+
->with('section/1/key')
190+
->will($this->returnValue($field));
191+
192+
$backendModel = $this->createPartialMock(
193+
\Magento\Framework\App\Config\Value::class,
194+
['addData']
195+
);
196+
$this->_dataFactoryMock->expects($this->any())->method('create')->will($this->returnValue($backendModel));
197+
198+
$this->_transFactoryMock->expects($this->never())->method('addObject');
199+
$backendModel->expects($this->never())->method('addData');
200+
201+
$this->_model->save();
202+
}
203+
152204
public function testSaveToCheckScopeDataSet()
153205
{
154206
$transactionMock = $this->createMock(\Magento\Framework\DB\Transaction::class);

0 commit comments

Comments
 (0)