Skip to content

Commit 2e07423

Browse files
committed
Merge remote-tracking branch 'github-magento/MC-16455-rebase' into EPAM-PR-83
2 parents c1ff2c8 + f3bc78e commit 2e07423

File tree

7 files changed

+97
-34
lines changed

7 files changed

+97
-34
lines changed

app/code/Magento/Catalog/Model/Category/DataProvider.php

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Catalog\Model\Category;
79

810
use Magento\Catalog\Api\Data\CategoryInterface;
@@ -20,6 +22,7 @@
2022
use Magento\Framework\Exception\NoSuchEntityException;
2123
use Magento\Framework\Filesystem;
2224
use Magento\Framework\Stdlib\ArrayManager;
25+
use Magento\Framework\Stdlib\ArrayUtils;
2326
use Magento\Store\Model\Store;
2427
use Magento\Store\Model\StoreManagerInterface;
2528
use Magento\Ui\Component\Form\Field;
@@ -28,10 +31,9 @@
2831
use Magento\Framework\AuthorizationInterface;
2932

3033
/**
31-
* Class DataProvider
34+
* Category form data provider.
3235
*
3336
* @api
34-
*
3537
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
3638
* @SuppressWarnings(PHPMD.TooManyFields)
3739
* @since 101.0.0
@@ -52,6 +54,7 @@ class DataProvider extends \Magento\Ui\DataProvider\ModifierPoolDataProvider
5254

5355
/**
5456
* EAV attribute properties to fetch from meta storage
57+
*
5558
* @var array
5659
* @since 101.0.0
5760
*/
@@ -143,6 +146,11 @@ class DataProvider extends \Magento\Ui\DataProvider\ModifierPoolDataProvider
143146
*/
144147
private $arrayManager;
145148

149+
/**
150+
* @var ArrayUtils
151+
*/
152+
private $arrayUtils;
153+
146154
/**
147155
* @var Filesystem
148156
*/
@@ -154,8 +162,6 @@ class DataProvider extends \Magento\Ui\DataProvider\ModifierPoolDataProvider
154162
private $auth;
155163

156164
/**
157-
* DataProvider constructor
158-
*
159165
* @param string $name
160166
* @param string $primaryFieldName
161167
* @param string $requestFieldName
@@ -170,6 +176,8 @@ class DataProvider extends \Magento\Ui\DataProvider\ModifierPoolDataProvider
170176
* @param array $data
171177
* @param PoolInterface|null $pool
172178
* @param AuthorizationInterface|null $auth
179+
* @param ArrayUtils|null $arrayUtils
180+
* @throws \Magento\Framework\Exception\LocalizedException
173181
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
174182
*/
175183
public function __construct(
@@ -186,7 +194,8 @@ public function __construct(
186194
array $meta = [],
187195
array $data = [],
188196
PoolInterface $pool = null,
189-
?AuthorizationInterface $auth = null
197+
?AuthorizationInterface $auth = null,
198+
?ArrayUtils $arrayUtils = null
190199
) {
191200
$this->eavValidationRules = $eavValidationRules;
192201
$this->collection = $categoryCollectionFactory->create();
@@ -197,6 +206,7 @@ public function __construct(
197206
$this->request = $request;
198207
$this->categoryFactory = $categoryFactory;
199208
$this->auth = $auth ?? ObjectManager::getInstance()->get(AuthorizationInterface::class);
209+
$this->arrayUtils = $arrayUtils ?? ObjectManager::getInstance()->get(ArrayUtils::class);
200210

201211
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data, $pool);
202212
}
@@ -226,7 +236,7 @@ public function getMeta()
226236
* @param array $meta
227237
* @return array
228238
*/
229-
private function addUseDefaultValueCheckbox(Category $category, array $meta)
239+
private function addUseDefaultValueCheckbox(Category $category, array $meta): array
230240
{
231241
/** @var EavAttributeInterface $attribute */
232242
foreach ($category->getAttributes() as $attribute) {
@@ -290,7 +300,7 @@ public function prepareMeta($meta)
290300
* @param array $fieldsMeta
291301
* @return array
292302
*/
293-
private function prepareFieldsMeta($fieldsMap, $fieldsMeta)
303+
private function prepareFieldsMeta(array $fieldsMap, array $fieldsMeta): array
294304
{
295305
$canEditDesign = $this->auth->isAllowed('Magento_Catalog::edit_category_design');
296306

@@ -350,6 +360,8 @@ public function getAttributesMeta(Type $entityType)
350360
{
351361
$meta = [];
352362
$attributes = $entityType->getAttributeCollection();
363+
$fields = $this->getFields();
364+
$category = $this->getCurrentCategory();
353365
/* @var EavAttribute $attribute */
354366
foreach ($attributes as $attribute) {
355367
$code = $attribute->getAttributeCode();
@@ -374,6 +386,16 @@ public function getAttributesMeta(Type $entityType)
374386

375387
$meta[$code]['scopeLabel'] = $this->getScopeLabel($attribute);
376388
$meta[$code]['componentType'] = Field::NAME;
389+
390+
// disable fields
391+
if ($category) {
392+
$attributeIsLocked = $category->isLockedAttribute($code);
393+
$meta[$code]['disabled'] = $attributeIsLocked;
394+
$hasUseConfigField = (bool) array_search('use_config.' . $code, $fields, true);
395+
if ($hasUseConfigField && $meta[$code]['disabled']) {
396+
$meta['use_config.' . $code]['disabled'] = true;
397+
}
398+
}
377399
}
378400

379401
$result = [];
@@ -505,7 +527,7 @@ protected function filterFields($categoryData)
505527
* @param array $categoryData
506528
* @return array
507529
*/
508-
private function convertValues($category, $categoryData)
530+
private function convertValues($category, $categoryData): array
509531
{
510532
foreach ($category->getAttributes() as $attributeCode => $attribute) {
511533
if (!isset($categoryData[$attributeCode])) {
@@ -616,13 +638,24 @@ protected function getFieldsMap()
616638
];
617639
}
618640

641+
/**
642+
* Return list of fields names.
643+
*
644+
* @return array
645+
*/
646+
private function getFields(): array
647+
{
648+
$fieldsMap = $this->getFieldsMap();
649+
return $this->arrayUtils->flatten($fieldsMap);
650+
}
651+
619652
/**
620653
* Retrieve scope overridden value
621654
*
622655
* @return ScopeOverriddenValue
623656
* @deprecated 101.1.0
624657
*/
625-
private function getScopeOverriddenValue()
658+
private function getScopeOverriddenValue(): ScopeOverriddenValue
626659
{
627660
if (null === $this->scopeOverriddenValue) {
628661
$this->scopeOverriddenValue = \Magento\Framework\App\ObjectManager::getInstance()->get(
@@ -639,7 +672,7 @@ private function getScopeOverriddenValue()
639672
* @return ArrayManager
640673
* @deprecated 101.1.0
641674
*/
642-
private function getArrayManager()
675+
private function getArrayManager(): ArrayManager
643676
{
644677
if (null === $this->arrayManager) {
645678
$this->arrayManager = \Magento\Framework\App\ObjectManager::getInstance()->get(
@@ -657,7 +690,7 @@ private function getArrayManager()
657690
*
658691
* @deprecated 101.1.0
659692
*/
660-
private function getFileInfo()
693+
private function getFileInfo(): FileInfo
661694
{
662695
if ($this->fileInfo === null) {
663696
$this->fileInfo = ObjectManager::getInstance()->get(FileInfo::class);

app/code/Magento/Catalog/Test/Mftf/Section/AdminCategoryProductsSection.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
<section name="AdminCategoryProductsSection">
1212
<element name="sectionHeader" type="button" selector="div[data-index='assign_products']" timeout="30"/>
1313
<element name="addProducts" type="button" selector="#catalog_category_add_product_tabs" timeout="30"/>
14+
<element name="addProductsDisabled" type="button" selector="#catalog_category_add_product_tabs[disabled]" timeout="30"/>
1415
</section>
1516
</sections>

app/code/Magento/Catalog/Test/Unit/Model/Category/DataProviderTest.php

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Magento\Store\Model\StoreManagerInterface;
1818
use Magento\Ui\DataProvider\EavValidationRules;
1919
use Magento\Ui\DataProvider\Modifier\PoolInterface;
20+
use Magento\Framework\Stdlib\ArrayUtils;
2021

2122
/**
2223
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -78,6 +79,14 @@ class DataProviderTest extends \PHPUnit\Framework\TestCase
7879
*/
7980
private $modifierPool;
8081

82+
/**
83+
* @var ArrayUtils|\PHPUnit_Framework_MockObject_MockObject
84+
*/
85+
private $arrayUtils;
86+
87+
/**
88+
* @inheritDoc
89+
*/
8190
protected function setUp()
8291
{
8392
$this->eavValidationRules = $this->getMockBuilder(EavValidationRules::class)
@@ -128,6 +137,10 @@ protected function setUp()
128137
->getMock();
129138

130139
$this->modifierPool = $this->getMockBuilder(PoolInterface::class)->getMockForAbstractClass();
140+
141+
$this->arrayUtils = $this->getMockBuilder(ArrayUtils::class)
142+
->setMethods(['flatten'])
143+
->disableOriginalConstructor()->getMock();
131144
}
132145

133146
/**
@@ -157,7 +170,8 @@ private function getModel()
157170
'eavConfig' => $this->eavConfig,
158171
'request' => $this->request,
159172
'categoryFactory' => $this->categoryFactory,
160-
'pool' => $this->modifierPool
173+
'pool' => $this->modifierPool,
174+
'arrayUtils' => $this->arrayUtils
161175
]
162176
);
163177

@@ -206,10 +220,12 @@ public function testGetDataNoFileExists()
206220
->getMock();
207221
$categoryMock->expects($this->exactly(2))
208222
->method('getData')
209-
->willReturnMap([
210-
['', null, $categoryData],
211-
['image', null, $categoryData['image']],
212-
]);
223+
->willReturnMap(
224+
[
225+
['', null, $categoryData],
226+
['image', null, $categoryData['image']],
227+
]
228+
);
213229
$categoryMock->expects($this->any())
214230
->method('getExistsStoreValueFlag')
215231
->with('url_key')
@@ -280,10 +296,12 @@ public function testGetData()
280296
->getMock();
281297
$categoryMock->expects($this->exactly(2))
282298
->method('getData')
283-
->willReturnMap([
284-
['', null, $categoryData],
285-
['image', null, $categoryData['image']],
286-
]);
299+
->willReturnMap(
300+
[
301+
['', null, $categoryData],
302+
['image', null, $categoryData['image']],
303+
]
304+
);
287305
$categoryMock->expects($this->any())
288306
->method('getExistsStoreValueFlag')
289307
->with('url_key')
@@ -331,10 +349,12 @@ public function testGetData()
331349

332350
public function testGetMetaWithoutParentInheritanceResolving()
333351
{
352+
$this->arrayUtils->expects($this->atLeastOnce())->method('flatten')->willReturn([1,3,3]);
353+
334354
$categoryMock = $this->getMockBuilder(\Magento\Catalog\Model\Category::class)
335355
->disableOriginalConstructor()
336356
->getMock();
337-
$this->registry->expects($this->once())
357+
$this->registry->expects($this->atLeastOnce())
338358
->method('registry')
339359
->with('category')
340360
->willReturn($categoryMock);

app/code/Magento/Catalog/Test/Unit/Ui/DataProvider/Product/Form/Modifier/CategoriesTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Categories;
1111
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
1212
use Magento\Catalog\Model\ResourceModel\Category\Collection as CategoryCollection;
13-
use Magento\Framework\App\CacheInterface;
1413
use Magento\Framework\DB\Helper as DbHelper;
1514
use Magento\Framework\UrlInterface;
1615
use Magento\Store\Model\Store;
@@ -161,7 +160,14 @@ public function testModifyMetaLocked($locked)
161160
->willReturnArgument(2);
162161

163162
$modifyMeta = $this->createModel()->modifyMeta($meta);
164-
$this->assertEquals($locked, $modifyMeta['arguments']['data']['config']['disabled']);
163+
$this->assertEquals(
164+
$locked,
165+
$modifyMeta['children']['category_ids']['arguments']['data']['config']['disabled']
166+
);
167+
$this->assertEquals(
168+
$locked,
169+
$modifyMeta['children']['create_category_button']['arguments']['data']['config']['disabled']
170+
);
165171
}
166172

167173
/**

app/code/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/Categories.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
* Data provider for categories field of product page
2424
*
2525
* @api
26-
*
2726
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2827
* @since 101.0.0
2928
*/
@@ -120,7 +119,7 @@ public function __construct(
120119
* @return CacheInterface
121120
* @deprecated 101.0.3
122121
*/
123-
private function getCacheManager()
122+
private function getCacheManager(): CacheInterface
124123
{
125124
if (!$this->cacheManager) {
126125
$this->cacheManager = ObjectManager::getInstance()
@@ -148,9 +147,9 @@ public function modifyMeta(array $meta)
148147
*
149148
* @return bool
150149
*/
151-
private function isAllowed()
150+
private function isAllowed(): bool
152151
{
153-
return $this->authorization->isAllowed('Magento_Catalog::categories');
152+
return (bool) $this->authorization->isAllowed('Magento_Catalog::categories');
154153
}
155154

156155
/**
@@ -234,6 +233,7 @@ protected function customizeCategoriesField(array $meta)
234233
$fieldCode = 'category_ids';
235234
$elementPath = $this->arrayManager->findPath($fieldCode, $meta, null, 'children');
236235
$containerPath = $this->arrayManager->findPath(static::CONTAINER_PREFIX . $fieldCode, $meta, null, 'children');
236+
$fieldIsDisabled = $this->locator->getProduct()->isLockedAttribute($fieldCode);
237237

238238
if (!$elementPath) {
239239
return $meta;
@@ -250,7 +250,6 @@ protected function customizeCategoriesField(array $meta)
250250
'componentType' => 'container',
251251
'component' => 'Magento_Ui/js/form/components/group',
252252
'scopeLabel' => __('[GLOBAL]'),
253-
'disabled' => $this->locator->getProduct()->isLockedAttribute($fieldCode),
254253
],
255254
],
256255
],
@@ -266,6 +265,7 @@ protected function customizeCategoriesField(array $meta)
266265
'chipsEnabled' => true,
267266
'disableLabel' => true,
268267
'levelsVisibility' => '1',
268+
'disabled' => $fieldIsDisabled,
269269
'elementTmpl' => 'ui/grid/filters/elements/ui-select',
270270
'options' => $this->getCategoriesTree(),
271271
'listens' => [
@@ -291,6 +291,7 @@ protected function customizeCategoriesField(array $meta)
291291
'formElement' => 'container',
292292
'additionalClasses' => 'admin__field-small',
293293
'componentType' => 'container',
294+
'disabled' => $fieldIsDisabled,
294295
'component' => 'Magento_Ui/js/form/components/button',
295296
'template' => 'ui/form/components/button/container',
296297
'actions' => [
@@ -320,11 +321,7 @@ protected function customizeCategoriesField(array $meta)
320321
]
321322
];
322323
}
323-
$meta = $this->arrayManager->merge(
324-
$containerPath,
325-
$meta,
326-
$value
327-
);
324+
$meta = $this->arrayManager->merge($containerPath, $meta, $value);
328325

329326
return $meta;
330327
}

0 commit comments

Comments
 (0)