From 2778acb2f2086d6cfe8484156c858dc7f4f94826 Mon Sep 17 00:00:00 2001 From: Erik Kalinak Date: Tue, 5 Mar 2019 18:47:00 +0100 Subject: [PATCH 1/2] Fixed category names in product edit for multi language stores. --- .../Ui/DataProvider/Product/Form/Modifier/Categories.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/Categories.php b/app/code/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/Categories.php index 681435851fbde..a6e6d876f535b 100644 --- a/app/code/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/Categories.php +++ b/app/code/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/Categories.php @@ -310,12 +310,14 @@ protected function customizeCategoriesField(array $meta) */ protected function getCategoriesTree($filter = null) { - $categoryTree = $this->getCacheManager()->load(self::CATEGORY_TREE_ID . '_' . $filter); + $storeId = $this->locator->getStore()->getId(); + $cacheKey = self::CATEGORY_TREE_ID . '_' . $storeId . '_' . $filter; + + $categoryTree = $this->getCacheManager()->load($cacheKey); if ($categoryTree) { return $this->serializer->unserialize($categoryTree); } - $storeId = $this->locator->getStore()->getId(); /* @var $matchingNamesCollection \Magento\Catalog\Model\ResourceModel\Category\Collection */ $matchingNamesCollection = $this->categoryCollectionFactory->create(); @@ -367,7 +369,7 @@ protected function getCategoriesTree($filter = null) $this->getCacheManager()->save( $this->serializer->serialize($categoryById[CategoryModel::TREE_ROOT_ID]['optgroup']), - self::CATEGORY_TREE_ID . '_' . $filter, + $cacheKey, [ \Magento\Catalog\Model\Category::CACHE_TAG, \Magento\Framework\App\Cache\Type\Block::CACHE_TAG From 39d84be10f70bd9d23b85bbdc56c5b9eef83942a Mon Sep 17 00:00:00 2001 From: Erik Kalinak Date: Fri, 26 Apr 2019 10:29:17 +0200 Subject: [PATCH 2/2] Re-added test for category tree, removed already merged solution for category tree caching and replaced with simpler one. --- .../Product/Form/Modifier/CategoriesTest.php | 34 +++++++++++++++++++ .../Product/Form/Modifier/Categories.php | 19 ++--------- 2 files changed, 37 insertions(+), 16 deletions(-) diff --git a/app/code/Magento/Catalog/Test/Unit/Ui/DataProvider/Product/Form/Modifier/CategoriesTest.php b/app/code/Magento/Catalog/Test/Unit/Ui/DataProvider/Product/Form/Modifier/CategoriesTest.php index a2d81854607a0..e81b8c6f53332 100644 --- a/app/code/Magento/Catalog/Test/Unit/Ui/DataProvider/Product/Form/Modifier/CategoriesTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Ui/DataProvider/Product/Form/Modifier/CategoriesTest.php @@ -154,4 +154,38 @@ public function modifyMetaLockedDataProvider() { return [[true], [false]]; } + + public function testModifyMetaWithCaching() + { + $this->arrayManagerMock->expects($this->exactly(2)) + ->method('findPath') + ->willReturn(true); + $cacheManager = $this->getMockBuilder(CacheInterface::class) + ->getMockForAbstractClass(); + $cacheManager->expects($this->once()) + ->method('load') + ->with(Categories::CATEGORY_TREE_ID . '_'); + $cacheManager->expects($this->once()) + ->method('save'); + + $modifier = $this->createModel(); + $cacheContextProperty = new \ReflectionProperty( + Categories::class, + 'cacheManager' + ); + $cacheContextProperty->setAccessible(true); + $cacheContextProperty->setValue($modifier, $cacheManager); + + $groupCode = 'test_group_code'; + $meta = [ + $groupCode => [ + 'children' => [ + 'category_ids' => [ + 'sortOrder' => 10, + ], + ], + ], + ]; + $modifier->modifyMeta($meta); + } } diff --git a/app/code/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/Categories.php b/app/code/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/Categories.php index e3d548855ce72..ba9863191a6d1 100644 --- a/app/code/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/Categories.php +++ b/app/code/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/Categories.php @@ -3,8 +3,6 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ -declare(strict_types=1); - namespace Magento\Catalog\Ui\DataProvider\Product\Form\Modifier; use Magento\Catalog\Model\Locator\LocatorInterface; @@ -316,7 +314,9 @@ protected function customizeCategoriesField(array $meta) protected function getCategoriesTree($filter = null) { $storeId = $this->locator->getStore()->getId(); - $cacheKey = self::CATEGORY_TREE_ID . '_' . $storeId . '_' . $filter; + $cacheKey = $storeId + ? self::CATEGORY_TREE_ID . '_' . $storeId . '_' . $filter + : self::CATEGORY_TREE_ID . '_' . $filter; $categoryTree = $this->getCacheManager()->load($cacheKey); if ($categoryTree) { @@ -346,19 +346,6 @@ protected function getCategoriesTree($filter = null) } } - return $shownCategoriesIds; - } - - /** - * Retrieve tree of categories with attributes. - * - * @param int $storeId - * @param array $shownCategoriesIds - * @return array|null - * @throws LocalizedException - */ - private function retrieveCategoriesTree(int $storeId, array $shownCategoriesIds) : ?array - { /* @var $collection \Magento\Catalog\Model\ResourceModel\Category\Collection */ $collection = $this->categoryCollectionFactory->create();