Skip to content

Commit 8468dc3

Browse files
committed
MAGETWO-60381: Disable fields functionality doesn't work for theme configuration
1 parent 3d6bdee commit 8468dc3

File tree

2 files changed

+276
-0
lines changed

2 files changed

+276
-0
lines changed

app/code/Magento/Theme/Model/Design/Config/DataProvider.php

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55
*/
66
namespace Magento\Theme\Model\Design\Config;
77

8+
use Magento\Framework\App\ObjectManager;
9+
use Magento\Store\Model\StoreManagerInterface;
810
use Magento\Theme\Model\ResourceModel\Design\Config\Collection;
911
use Magento\Theme\Model\ResourceModel\Design\Config\CollectionFactory;
1012
use Magento\Ui\DataProvider\AbstractDataProvider;
13+
use Magento\Config\Model\Config\Reader\Source\Deployed\SettingChecker;
14+
use Magento\Framework\App\RequestInterface;
1115

1216
class DataProvider extends AbstractDataProvider
1317
{
@@ -31,6 +35,21 @@ class DataProvider extends AbstractDataProvider
3135
*/
3236
private $metadataLoader;
3337

38+
/**
39+
* @var SettingChecker
40+
*/
41+
private $settingChecker;
42+
43+
/**
44+
* @var RequestInterface
45+
*/
46+
private $request;
47+
48+
/**
49+
* @var StoreManagerInterface
50+
*/
51+
private $storeManager;
52+
3453
/**
3554
* @param string $name
3655
* @param string $primaryFieldName
@@ -78,4 +97,104 @@ public function getData()
7897
$this->loadedData = $this->dataLoader->getData();
7998
return $this->loadedData;
8099
}
100+
101+
/**
102+
* {@inheritdoc}
103+
*/
104+
public function getMeta()
105+
{
106+
$meta = parent::getMeta();
107+
if (!isset($meta['other_settings']['children'])) {
108+
return $meta;
109+
}
110+
111+
$request = $this->getRequest()->getParams();
112+
if (!isset($request['scope'])) {
113+
return $meta;
114+
}
115+
116+
$scope = $request['scope'];
117+
$scopeCode = $this->getStringScopeCode(
118+
$scope,
119+
isset($request['scope_id']) ? $request['scope_id'] : null
120+
);
121+
122+
foreach ($meta['other_settings']['children'] as $settingGroupName => &$settingGroup) {
123+
foreach ($settingGroup['children'] as $fieldName => &$field) {
124+
$path = sprintf(
125+
'design/%s/%s',
126+
$settingGroupName,
127+
preg_replace('/^' . $settingGroupName . '_/', '', $fieldName)
128+
);
129+
$isReadOnly = $this->getSettingChecker()->isReadOnly(
130+
$path,
131+
$scope,
132+
$scopeCode
133+
);
134+
135+
if ($isReadOnly) {
136+
$field['arguments']['data']['config']['disabled'] = true;
137+
$field['arguments']['data']['config']['is_disable_inheritance'] = true;
138+
}
139+
}
140+
}
141+
142+
return $meta;
143+
}
144+
145+
/**
146+
* Retrieve Scope string code
147+
*
148+
* @param string $scope
149+
* @param integer $scopeId
150+
* @return string
151+
*/
152+
private function getStringScopeCode($scope, $scopeId = null)
153+
{
154+
$scopeCode = '';
155+
156+
if ($scope == 'stores') {
157+
$scopeCode = $this->getStoreManager()->getStore($scopeId)->getCode();
158+
} elseif ($scope == 'websites') {
159+
$scopeCode = $this->getStoreManager()->getWebsite($scopeId)->getCode();
160+
}
161+
162+
return $scopeCode;
163+
}
164+
165+
/**
166+
* @deprecated
167+
* @return SettingChecker
168+
*/
169+
private function getSettingChecker()
170+
{
171+
if ($this->settingChecker === null) {
172+
$this->settingChecker = ObjectManager::getInstance()->get(SettingChecker::class);
173+
}
174+
return $this->settingChecker;
175+
}
176+
177+
/**
178+
* @deprecated
179+
* @return RequestInterface
180+
*/
181+
private function getRequest()
182+
{
183+
if ($this->request === null) {
184+
$this->request = ObjectManager::getInstance()->get(RequestInterface::class);
185+
}
186+
return $this->request;
187+
}
188+
189+
/**
190+
* @deprecated
191+
* @return StoreManagerInterface
192+
*/
193+
private function getStoreManager()
194+
{
195+
if ($this->storeManager === null) {
196+
$this->storeManager = ObjectManager::getInstance()->get(StoreManagerInterface::class);
197+
}
198+
return $this->storeManager;
199+
}
81200
}

app/code/Magento/Theme/Test/Unit/Model/Design/Config/DataProviderTest.php

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
*/
66
namespace Magento\Theme\Test\Unit\Model\Design\Config;
77

8+
use Magento\Config\Model\Config\Reader\Source\Deployed\SettingChecker;
9+
use Magento\Framework\App\RequestInterface;
10+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
11+
use Magento\Store\Api\Data\StoreInterface;
12+
use Magento\Store\Model\StoreManagerInterface;
813
use Magento\Theme\Model\Design\Config\DataLoader;
914
use Magento\Theme\Model\Design\Config\DataProvider;
1015
use Magento\Theme\Model\Design\Config\MetadataLoader;
@@ -32,8 +37,29 @@ class DataProviderTest extends \PHPUnit_Framework_TestCase
3237
*/
3338
protected $collection;
3439

40+
/**
41+
* @var ObjectManager
42+
*/
43+
private $objectManager;
44+
45+
/**
46+
* @var RequestInterface|\PHPUnit_Framework_MockObject_MockObject
47+
*/
48+
private $requestMock;
49+
50+
/**
51+
* @var StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
52+
*/
53+
private $storeManagerMock;
54+
55+
/**
56+
* @var SettingChecker|\PHPUnit_Framework_MockObject_MockObject
57+
*/
58+
private $settingCheckerMock;
59+
3560
protected function setUp()
3661
{
62+
$this->objectManager = new ObjectManager($this);
3763
$this->dataLoader = $this->getMockBuilder('Magento\Theme\Model\Design\Config\DataProvider\DataLoader')
3864
->disableOriginalConstructor()
3965
->getMock();
@@ -57,6 +83,16 @@ protected function setUp()
5783
->method('create')
5884
->willReturn($this->collection);
5985

86+
$this->requestMock = $this->getMockBuilder(RequestInterface::class)
87+
->disableOriginalConstructor()
88+
->getMock();
89+
$this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)
90+
->disableOriginalConstructor()
91+
->getMock();
92+
$this->settingCheckerMock = $this->getMockBuilder(SettingChecker::class)
93+
->disableOriginalConstructor()
94+
->getMock();
95+
6096
$this->model = new DataProvider(
6197
'scope',
6298
'scope',
@@ -65,6 +101,21 @@ protected function setUp()
65101
$this->metadataLoader,
66102
$collectionFactory
67103
);
104+
$this->objectManager->setBackwardCompatibleProperty(
105+
$this->model,
106+
'request',
107+
$this->requestMock
108+
);
109+
$this->objectManager->setBackwardCompatibleProperty(
110+
$this->model,
111+
'storeManager',
112+
$this->storeManagerMock
113+
);
114+
$this->objectManager->setBackwardCompatibleProperty(
115+
$this->model,
116+
'settingChecker',
117+
$this->settingCheckerMock
118+
);
68119
}
69120

70121
public function testGetData()
@@ -79,4 +130,110 @@ public function testGetData()
79130

80131
$this->assertEquals($data, $this->model->getData());
81132
}
133+
134+
/**
135+
* @param array $inputMeta
136+
* @param array $expectedMeta
137+
* @param array $request
138+
* @dataProvider getMetaDataProvider
139+
*/
140+
public function testGetMeta(array $inputMeta, array $expectedMeta, array $request)
141+
{
142+
$store = $this->getMockBuilder(StoreInterface::class)
143+
->disableOriginalConstructor()
144+
->getMock();
145+
$store->expects($this->any())
146+
->method('getCode')
147+
->willReturn('store1');
148+
$this->requestMock->expects($this->any())
149+
->method('getParams')
150+
->willReturn($request);
151+
$this->storeManagerMock->expects($this->any())
152+
->method('getStore')
153+
->with(1)
154+
->willReturn($store);
155+
$this->settingCheckerMock->expects($this->any())
156+
->method('isReadOnly')
157+
->withConsecutive(
158+
['design/head/welcome', 'stores', 'store1'],
159+
['design/head/logo', 'stores', 'store1']
160+
)
161+
->willReturnOnConsecutiveCalls(
162+
true,
163+
false
164+
);
165+
166+
$this->objectManager->setBackwardCompatibleProperty(
167+
$this->model,
168+
'meta',
169+
$inputMeta
170+
);
171+
172+
$this->assertSame($expectedMeta, $this->model->getMeta());
173+
}
174+
175+
/**
176+
* @return array
177+
*/
178+
public function getMetaDataProvider()
179+
{
180+
return [
181+
[
182+
[
183+
'option1'
184+
],
185+
[
186+
'option1'
187+
],
188+
[
189+
'scope' => 'default'
190+
]
191+
],
192+
[
193+
[
194+
'other_settings' => [
195+
'children' => [
196+
'head' => [
197+
'children' => [
198+
'head_welcome' => [
199+
200+
],
201+
'head_logo' => [
202+
203+
]
204+
]
205+
]
206+
]
207+
]
208+
],
209+
[
210+
'other_settings' => [
211+
'children' => [
212+
'head' => [
213+
'children' => [
214+
'head_welcome' => [
215+
'arguments' => [
216+
'data' => [
217+
'config' => [
218+
'disabled' => true,
219+
'is_disable_inheritance' => true,
220+
]
221+
]
222+
]
223+
],
224+
'head_logo' => [
225+
226+
]
227+
]
228+
]
229+
]
230+
]
231+
],
232+
[
233+
'scope' => 'stores',
234+
'scope_id' => 1
235+
]
236+
]
237+
];
238+
}
82239
}

0 commit comments

Comments
 (0)