Skip to content

[Forwardport] Rework for PR #16222 #17168

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

Closed
Closed
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
21 changes: 21 additions & 0 deletions app/code/Magento/Config/Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
*/
namespace Magento\Config\Model;

use Magento\Config\Model\Config\Reader\Source\Deployed\SettingChecker;
use Magento\Framework\App\ObjectManager;

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

/**
* @var SettingChecker|null
*/
private $settingChecker;

/**
* @param \Magento\Framework\App\Config\ReinitableConfigInterface $config
* @param \Magento\Framework\Event\ManagerInterface $eventManager
Expand All @@ -85,6 +93,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 SettingChecker|null $settingChecker
* @param array $data
*/
public function __construct(
Expand All @@ -95,6 +104,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 @@ -105,6 +115,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 @@ -229,6 +240,16 @@ protected function _processGroup(
}

foreach ($groupData['fields'] as $fieldId => $fieldData) {
$isReadOnly = $this->settingChecker->isReadOnly(
$groupPath . '/' . $fieldId,
$this->getScope(),
$this->getScopeCode()
);

if ($isReadOnly) {
continue;
}

$originalFieldId = $fieldId;
if ($group->shouldCloneFields() && isset($mappedFields[$fieldId])) {
$originalFieldId = $mappedFields[$fieldId];
Expand Down
48 changes: 46 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,41 @@ public function testSaveToCheckAdminSystemConfigChangedSectionEvent()
$this->_model->save();
}

public function testDoNotSaveReadOnlyFields()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why content of this method is different in original PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @ihor-sviziev
Thank you for review
In Magento 2.2 exist two additional calls of method Magento\Config\Model\Config\Structure\Element\Group::getElement (check https://github.com/phoenix128/magento2/blob/af78c2fe5a02e5f521f1c984f303063544ef71f6/app/code/Magento/Config/Model/Config.php line 229 and 231) unlike Magento 2.3 implementation.
So in M2.3, we expect this method being invoked once as I've implemented.
$this->_configStructure->expects($this->once())
->method('getElement')
->with('section/1')
->will($this->returnValue($group));

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this the only difference.

{
$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->once())
->method('getElement')
->with('section/1')
->will($this->returnValue($group));

$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