Skip to content

Commit 18bb3a7

Browse files
committed
Merge branch 'MAGETWO-99091' of https://github.com/magento-mpi/magento2ce into pr_2019_04_17
2 parents 7b790d0 + 14de170 commit 18bb3a7

File tree

2 files changed

+66
-48
lines changed

2 files changed

+66
-48
lines changed

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

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -154,38 +154,4 @@ public function modifyMetaLockedDataProvider()
154154
{
155155
return [[true], [false]];
156156
}
157-
158-
public function testModifyMetaWithCaching()
159-
{
160-
$this->arrayManagerMock->expects($this->exactly(2))
161-
->method('findPath')
162-
->willReturn(true);
163-
$cacheManager = $this->getMockBuilder(CacheInterface::class)
164-
->getMockForAbstractClass();
165-
$cacheManager->expects($this->once())
166-
->method('load')
167-
->with(Categories::CATEGORY_TREE_ID . '_');
168-
$cacheManager->expects($this->once())
169-
->method('save');
170-
171-
$modifier = $this->createModel();
172-
$cacheContextProperty = new \ReflectionProperty(
173-
Categories::class,
174-
'cacheManager'
175-
);
176-
$cacheContextProperty->setAccessible(true);
177-
$cacheContextProperty->setValue($modifier, $cacheManager);
178-
179-
$groupCode = 'test_group_code';
180-
$meta = [
181-
$groupCode => [
182-
'children' => [
183-
'category_ids' => [
184-
'sortOrder' => 10,
185-
],
186-
],
187-
],
188-
];
189-
$modifier->modifyMeta($meta);
190-
}
191157
}

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

Lines changed: 66 additions & 14 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\Ui\DataProvider\Product\Form\Modifier;
79

810
use Magento\Catalog\Model\Locator\LocatorInterface;
@@ -11,6 +13,7 @@
1113
use Magento\Framework\App\CacheInterface;
1214
use Magento\Framework\DB\Helper as DbHelper;
1315
use Magento\Catalog\Model\Category as CategoryModel;
16+
use Magento\Framework\Exception\LocalizedException;
1417
use Magento\Framework\Serialize\SerializerInterface;
1518
use Magento\Framework\UrlInterface;
1619
use Magento\Framework\Stdlib\ArrayManager;
@@ -202,6 +205,7 @@ protected function createNewCategoryModal(array $meta)
202205
*
203206
* @param array $meta
204207
* @return array
208+
* @throws LocalizedException
205209
* @since 101.0.0
206210
*/
207211
protected function customizeCategoriesField(array $meta)
@@ -306,20 +310,64 @@ protected function customizeCategoriesField(array $meta)
306310
*
307311
* @param string|null $filter
308312
* @return array
313+
* @throws LocalizedException
309314
* @since 101.0.0
310315
*/
311316
protected function getCategoriesTree($filter = null)
312317
{
313-
$categoryTree = $this->getCacheManager()->load(self::CATEGORY_TREE_ID . '_' . $filter);
314-
if ($categoryTree) {
315-
return $this->serializer->unserialize($categoryTree);
318+
$storeId = (int) $this->locator->getStore()->getId();
319+
320+
$cachedCategoriesTree = $this->getCacheManager()
321+
->load($this->getCategoriesTreeCacheId($storeId, (string) $filter));
322+
if (!empty($cachedCategoriesTree)) {
323+
return $this->serializer->unserialize($cachedCategoriesTree);
316324
}
317325

318-
$storeId = $this->locator->getStore()->getId();
326+
$categoriesTree = $this->retrieveCategoriesTree(
327+
$storeId,
328+
$this->retrieveShownCategoriesIds($storeId, (string) $filter)
329+
);
330+
331+
$this->getCacheManager()->save(
332+
$this->serializer->serialize($categoriesTree),
333+
$this->getCategoriesTreeCacheId($storeId, (string) $filter),
334+
[
335+
\Magento\Catalog\Model\Category::CACHE_TAG,
336+
\Magento\Framework\App\Cache\Type\Block::CACHE_TAG
337+
]
338+
);
339+
340+
return $categoriesTree;
341+
}
342+
343+
/**
344+
* Get cache id for categories tree.
345+
*
346+
* @param int $storeId
347+
* @param string $filter
348+
* @return string
349+
*/
350+
private function getCategoriesTreeCacheId(int $storeId, string $filter = '') : string
351+
{
352+
return self::CATEGORY_TREE_ID
353+
. '_' . (string) $storeId
354+
. '_' . $filter;
355+
}
356+
357+
/**
358+
* Retrieve filtered list of categories id.
359+
*
360+
* @param int $storeId
361+
* @param string $filter
362+
* @return array
363+
* @throws LocalizedException
364+
*/
365+
private function retrieveShownCategoriesIds(int $storeId, string $filter = '') : array
366+
{
319367
/* @var $matchingNamesCollection \Magento\Catalog\Model\ResourceModel\Category\Collection */
320368
$matchingNamesCollection = $this->categoryCollectionFactory->create();
321369

322-
if ($filter !== null) {
370+
if (!empty($filter)) {
323371
$matchingNamesCollection->addAttributeToFilter(
324372
'name',
325373
['like' => $this->dbHelper->addLikeEscape($filter, ['position' => 'any'])]
@@ -339,6 +387,19 @@ protected function getCategoriesTree($filter = null)
339387
}
340388
}
341389

390+
return $shownCategoriesIds;
391+
}
392+
393+
/**
394+
* Retrieve tree of categories with attributes.
395+
*
396+
* @param int $storeId
397+
* @param array $shownCategoriesIds
398+
* @return array
399+
* @throws LocalizedException
400+
*/
401+
private function retrieveCategoriesTree(int $storeId, array $shownCategoriesIds) : array
402+
{
342403
/* @var $collection \Magento\Catalog\Model\ResourceModel\Category\Collection */
343404
$collection = $this->categoryCollectionFactory->create();
344405

@@ -365,15 +426,6 @@ protected function getCategoriesTree($filter = null)
365426
$categoryById[$category->getParentId()]['optgroup'][] = &$categoryById[$category->getId()];
366427
}
367428

368-
$this->getCacheManager()->save(
369-
$this->serializer->serialize($categoryById[CategoryModel::TREE_ROOT_ID]['optgroup']),
370-
self::CATEGORY_TREE_ID . '_' . $filter,
371-
[
372-
\Magento\Catalog\Model\Category::CACHE_TAG,
373-
\Magento\Framework\App\Cache\Type\Block::CACHE_TAG
374-
]
375-
);
376-
377429
return $categoryById[CategoryModel::TREE_ROOT_ID]['optgroup'];
378430
}
379431
}

0 commit comments

Comments
 (0)