Skip to content

Commit 0587310

Browse files
author
Viktor Kopin
committed
MC-37665: Updating a category through the REST API will uncheck "Use Default Value" on a bunch of attributes
1 parent ac9e041 commit 0587310

File tree

4 files changed

+54
-35
lines changed

4 files changed

+54
-35
lines changed

app/code/Magento/Catalog/Model/CategoryRepository.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ class CategoryRepository implements \Magento\Catalog\Api\CategoryRepositoryInter
7171
* @param CategoryFactory $categoryFactory
7272
* @param CategoryResource $categoryResource
7373
* @param StoreManagerInterface $storeManager
74-
* @param PopulateWithValues $populateWithValues
74+
* @param PopulateWithValues|null $populateWithValues
7575
*/
7676
public function __construct(
7777
CategoryFactory $categoryFactory,
7878
CategoryResource $categoryResource,
7979
StoreManagerInterface $storeManager,
80-
PopulateWithValues $populateWithValues
80+
?PopulateWithValues $populateWithValues
8181
) {
8282
$this->categoryFactory = $categoryFactory;
8383
$this->categoryResource = $categoryResource;

app/code/Magento/Catalog/Model/CategoryRepository/PopulateWithValues.php

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -69,30 +69,46 @@ public function __construct(
6969
public function execute(CategoryInterface $category, array $existingData): void
7070
{
7171
$storeId = $existingData['store_id'];
72-
$overriddenValues = array_filter($category->getData(), function ($key) use ($category, $storeId) {
73-
/** @var Category $category */
74-
return $this->scopeOverriddenValue->containsValue(
75-
CategoryInterface::class,
76-
$category,
77-
$key,
78-
$storeId
79-
);
80-
}, ARRAY_FILTER_USE_KEY);
72+
$overriddenValues = array_filter(
73+
$category->getData(),
74+
function ($key) use ($category, $storeId) {
75+
/** @var Category $category */
76+
return $this->scopeOverriddenValue->containsValue(
77+
CategoryInterface::class,
78+
$category,
79+
$key,
80+
$storeId
81+
);
82+
},
83+
ARRAY_FILTER_USE_KEY
84+
);
8185
$defaultValues = array_diff_key($category->getData(), $overriddenValues);
82-
array_walk($defaultValues, function (&$value, $key) {
83-
$attributes = $this->getAttributes();
84-
if (isset($attributes[$key]) && !$attributes[$key]->isStatic()) {
85-
$value = null;
86+
array_walk(
87+
$defaultValues,
88+
function (&$value, $key) {
89+
$attributes = $this->getAttributes();
90+
if (isset($attributes[$key]) && !$attributes[$key]->isStatic()) {
91+
$value = null;
92+
}
8693
}
87-
});
94+
);
8895
$category->addData($defaultValues);
8996
$category->addData($existingData);
90-
$useDefaultAttributes = array_filter($category->getData(), function ($attributeValue) {
91-
return null === $attributeValue;
92-
});
93-
$category->setData('use_default', array_map(function () {
94-
return true;
95-
}, $useDefaultAttributes));
97+
$useDefaultAttributes = array_filter(
98+
$category->getData(),
99+
function ($attributeValue) {
100+
return null === $attributeValue;
101+
}
102+
);
103+
$category->setData(
104+
'use_default',
105+
array_map(
106+
function () {
107+
return true;
108+
},
109+
$useDefaultAttributes
110+
)
111+
);
96112
}
97113

98114
/**

app/code/Magento/Catalog/Test/Unit/Model/CategoryRepositoryTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,13 @@ class CategoryRepositoryTest extends TestCase
6565
protected $metadataPoolMock;
6666

6767
/**
68-
* @var MockObject
68+
* @var PopulateWithValues|MockObject
6969
*/
70-
protected $populateWithValuesMock;
70+
private $populateWithValuesMock;
7171

72+
/**
73+
* @inheridoc
74+
*/
7275
protected function setUp(): void
7376
{
7477
$this->categoryFactoryMock = $this->createPartialMock(
@@ -102,7 +105,7 @@ protected function setUp(): void
102105

103106
$this->populateWithValuesMock = $this
104107
->getMockBuilder(PopulateWithValues::class)
105-
->setMethods(['execute'])
108+
->onlyMethods(['execute'])
106109
->disableOriginalConstructor()
107110
->getMock();
108111

dev/tests/api-functional/testsuite/Magento/Catalog/Api/CategoryRepositoryTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Authorization\Model\RulesFactory;
1313
use Magento\Catalog\Api\Data\CategoryInterface;
1414
use Magento\Catalog\Model\Attribute\ScopeOverriddenValue;
15+
use Magento\Catalog\Model\Category;
1516
use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator;
1617
use Magento\Integration\Api\AdminTokenServiceInterface;
1718
use Magento\Store\Model\Store;
@@ -193,7 +194,7 @@ public function testDeleteSystemOrRoot()
193194
public function deleteSystemOrRootDataProvider()
194195
{
195196
return [
196-
[\Magento\Catalog\Model\Category::TREE_ROOT_ID],
197+
[Category::TREE_ROOT_ID],
197198
[2] //Default root category
198199
];
199200
}
@@ -216,8 +217,8 @@ public function testUpdate()
216217
];
217218
$result = $this->updateCategory($categoryId, $categoryData);
218219
$this->assertEquals($categoryId, $result['id']);
219-
/** @var \Magento\Catalog\Model\Category $model */
220-
$model = Bootstrap::getObjectManager()->get(\Magento\Catalog\Model\Category::class);
220+
/** @var Category $model */
221+
$model = Bootstrap::getObjectManager()->get(Category::class);
221222
$category = $model->load($categoryId);
222223
$this->assertFalse((bool)$category->getIsActive(), 'Category "is_active" must equal to false');
223224
$this->assertEquals("Update Category Test", $category->getName());
@@ -244,8 +245,8 @@ public function testUpdateWithDefaultSortByAttribute()
244245
];
245246
$result = $this->updateCategory($categoryId, $categoryData);
246247
$this->assertEquals($categoryId, $result['id']);
247-
/** @var \Magento\Catalog\Model\Category $model */
248-
$model = Bootstrap::getObjectManager()->get(\Magento\Catalog\Model\Category::class);
248+
/** @var Category $model */
249+
$model = Bootstrap::getObjectManager()->get(Category::class);
249250
$category = $model->load($categoryId);
250251
$this->assertTrue((bool)$category->getIsActive(), 'Category "is_active" must equal to true');
251252
$this->assertEquals("Update Category Test With default_sort_by Attribute", $category->getName());
@@ -288,8 +289,8 @@ public function testUpdateUrlKey()
288289
];
289290
$result = $this->updateCategory($categoryId, $categoryData);
290291
$this->assertEquals($categoryId, $result['id']);
291-
/** @var \Magento\Catalog\Model\Category $model */
292-
$model = Bootstrap::getObjectManager()->get(\Magento\Catalog\Model\Category::class);
292+
/** @var Category $model */
293+
$model = Bootstrap::getObjectManager()->get(Category::class);
293294
$category = $model->load($categoryId);
294295
$this->assertEquals("Update Category Test New Name", $category->getName());
295296

@@ -564,7 +565,6 @@ public function testSaveDesign(): void
564565
/**
565566
* Check if repository does not override default values for attributes out of request
566567
*
567-
* @throws \Exception
568568
* @magentoApiDataFixture Magento/Catalog/_files/category.php
569569
*/
570570
public function testUpdateScopeAttribute()
@@ -576,8 +576,8 @@ public function testUpdateScopeAttribute()
576576
$result = $this->updateCategoryForSpecificStore($categoryId, $categoryData);
577577
$this->assertEquals($categoryId, $result['id']);
578578

579-
/** @var \Magento\Catalog\Model\Category $model */
580-
$model = Bootstrap::getObjectManager()->get(\Magento\Catalog\Model\Category::class);
579+
/** @var Category $model */
580+
$model = Bootstrap::getObjectManager()->get(Category::class);
581581
$category = $model->load($categoryId);
582582

583583
/** @var ScopeOverriddenValue $scopeOverriddenValue */

0 commit comments

Comments
 (0)