Skip to content

Rework for PR #16222 . #16393

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions app/code/Magento/Config/Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
*/
namespace Magento\Config\Model;

use Magento\Config\Model\Config\Reader\Source\Deployed\SettingChecker;
use Magento\Config\Model\Config\Structure\Element\Group;
use Magento\Config\Model\Config\Structure\Element\Field;
use Magento\Framework\App\ObjectManager;

/**
* Backend config model
Expand Down Expand Up @@ -80,6 +82,11 @@ class Config extends \Magento\Framework\DataObject
*/
protected $_storeManager;

/**
* @var Config\Reader\Source\Deployed\SettingChecker
*/
private $settingChecker;

/**
* @param \Magento\Framework\App\Config\ReinitableConfigInterface $config
* @param \Magento\Framework\Event\ManagerInterface $eventManager
Expand All @@ -88,6 +95,7 @@ class Config extends \Magento\Framework\DataObject
* @param \Magento\Config\Model\Config\Loader $configLoader
* @param \Magento\Framework\App\Config\ValueFactory $configValueFactory
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param Config\Reader\Source\Deployed\SettingChecker|null $settingChecker
* @param array $data
*/
public function __construct(
Expand All @@ -98,6 +106,7 @@ public function __construct(
\Magento\Config\Model\Config\Loader $configLoader,
\Magento\Framework\App\Config\ValueFactory $configValueFactory,
\Magento\Store\Model\StoreManagerInterface $storeManager,
SettingChecker $settingChecker = null,
array $data = []
) {
parent::__construct($data);
Expand All @@ -108,6 +117,7 @@ public function __construct(
$this->_configLoader = $configLoader;
$this->_configValueFactory = $configValueFactory;
$this->_storeManager = $storeManager;
$this->settingChecker = $settingChecker ?: ObjectManager::getInstance()->get(SettingChecker::class);
}

/**
Expand Down Expand Up @@ -351,6 +361,16 @@ protected function _processGroup(
// use extra memory
$fieldsetData = [];
foreach ($groupData['fields'] as $fieldId => $fieldData) {
$isReadOnly = $this->settingChecker->isReadOnly(
$groupPath . '/' . $fieldId,
$this->getScope(),
$this->getScopeCode()
);

if ($isReadOnly) {
continue;
}

$field = $this->getField($sectionPath, $groupId, $fieldId);
/** @var \Magento\Framework\App\Config\ValueInterface $backendModel */
$backendModel = $field->hasBackendModel()
Expand Down
56 changes: 54 additions & 2 deletions app/code/Magento/Config/Test/Unit/Model/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ class ConfigTest extends \PHPUnit\Framework\TestCase
*/
protected $_configStructure;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $_settingsChecker;

protected function setUp()
{
$this->_eventManagerMock = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
Expand All @@ -79,7 +84,7 @@ protected function setUp()

$this->_transFactoryMock = $this->createPartialMock(
\Magento\Framework\DB\TransactionFactory::class,
['create']
['create', 'addObject']
);
$this->_appConfigMock = $this->createMock(\Magento\Framework\App\Config\ReinitableConfigInterface::class);
$this->_configLoaderMock = $this->createPartialMock(
Expand All @@ -90,14 +95,18 @@ protected function setUp()

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

$this->_settingsChecker = $this
->createMock(\Magento\Config\Model\Config\Reader\Source\Deployed\SettingChecker::class);

$this->_model = new \Magento\Config\Model\Config(
$this->_appConfigMock,
$this->_eventManagerMock,
$this->_configStructure,
$this->_transFactoryMock,
$this->_configLoaderMock,
$this->_dataFactoryMock,
$this->_storeManager
$this->_storeManager,
$this->_settingsChecker
);
}

Expand Down Expand Up @@ -149,6 +158,49 @@ public function testSaveToCheckAdminSystemConfigChangedSectionEvent()
$this->_model->save();
}

public function testDoNotSaveReadOnlyFields()
{
$transactionMock = $this->createMock(\Magento\Framework\DB\Transaction::class);
$this->_transFactoryMock->expects($this->any())->method('create')->will($this->returnValue($transactionMock));

$this->_settingsChecker->expects($this->any())->method('isReadOnly')->will($this->returnValue(true));
$this->_configLoaderMock->expects($this->any())->method('getConfigByPath')->will($this->returnValue([]));

$this->_model->setGroups(['1' => ['fields' => ['key' => ['data']]]]);
$this->_model->setSection('section');

$group = $this->createMock(\Magento\Config\Model\Config\Structure\Element\Group::class);
$group->method('getPath')->willReturn('section/1');

$field = $this->createMock(\Magento\Config\Model\Config\Structure\Element\Field::class);
$field->method('getGroupPath')->willReturn('section/1');
$field->method('getId')->willReturn('key');

$this->_configStructure->expects($this->at(0))
->method('getElement')
->with('section/1')
->will($this->returnValue($group));
$this->_configStructure->expects($this->at(1))
->method('getElement')
->with('section/1')
->will($this->returnValue($group));
$this->_configStructure->expects($this->at(2))
->method('getElement')
->with('section/1/key')
->will($this->returnValue($field));

$backendModel = $this->createPartialMock(
\Magento\Framework\App\Config\Value::class,
['addData']
);
$this->_dataFactoryMock->expects($this->any())->method('create')->will($this->returnValue($backendModel));

$this->_transFactoryMock->expects($this->never())->method('addObject');
$backendModel->expects($this->never())->method('addData');

$this->_model->save();
}

public function testSaveToCheckScopeDataSet()
{
$transactionMock = $this->createMock(\Magento\Framework\DB\Transaction::class);
Expand Down