From b0f9afa5584c458edfea9fdb7524732330185010 Mon Sep 17 00:00:00 2001 From: Cristian Partica Date: Thu, 17 Nov 2016 12:14:18 -0600 Subject: [PATCH 01/39] MAGETWO-58924: SQL error wait timeout error when saving categories - improving performance using memory & mysql temporary tables maps --- .../Category/ChildrenUrlRewriteGenerator.php | 17 +- .../CurrentUrlRewritesRegenerator.php | 71 ++++--- .../Model/Category/Plugin/Storage.php | 9 +- .../Model/Category/Plugin/Store/Group.php | 2 +- .../CategoryBasedProductRewriteGenerator.php | 20 +- .../Model/CategoryUrlRewriteGenerator.php | 80 +++++--- .../Model/Map/CategoryUrlRewriteMap.php | 138 +++++++++++++ .../Model/Map/DataCategoryMap.php | 87 ++++++++ .../Model/Map/DataCategoryUrlRewriteMap.php | 128 ++++++++++++ .../Map/DataCategoryUsedInProductsMap.php | 92 +++++++++ .../Model/Map/DataMapInterface.php | 29 +++ .../Model/Map/DataMapPool.php | 73 +++++++ .../Model/Map/DataMapPoolInterface.php | 30 +++ .../Model/Map/DataProductMap.php | 97 +++++++++ .../Model/Map/DataProductUrlRewriteMap.php | 123 ++++++++++++ .../Model/Map/MapFactory.php | 45 +++++ .../Model/Map/MapFactoryInterface.php | 21 ++ .../Model/Map/MapInterface.php | 22 ++ .../CatalogUrlRewrite/Model/Map/MapPool.php | 49 +++++ .../Model/Map/MapPoolInterface.php | 23 +++ .../Model/Map/ProductUrlRewriteMap.php | 139 +++++++++++++ .../Product/CurrentUrlRewritesRegenerator.php | 87 ++++---- .../Model/ProductScopeRewriteGenerator.php | 63 ++++-- .../Model/ProductUrlRewriteGenerator.php | 36 ++-- .../Model/ResourceModel/Category/Product.php | 34 ++++ .../Observer/AfterImportDataObserver.php | 23 ++- ...rocessUrlRewriteInMemorySavingObserver.php | 75 +++++++ .../Observer/UrlRewriteHandler.php | 48 +++-- app/code/Magento/CatalogUrlRewrite/etc/di.xml | 3 + .../Magento/CatalogUrlRewrite/etc/events.xml | 2 +- ...oryProcessUrlRewriteSavingObserverTest.php | 189 ++++++++++++++++++ .../Framework/DB/TemporaryTableService.php | 87 ++++++++ 32 files changed, 1773 insertions(+), 169 deletions(-) create mode 100644 app/code/Magento/CatalogUrlRewrite/Model/Map/CategoryUrlRewriteMap.php create mode 100644 app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryMap.php create mode 100644 app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUrlRewriteMap.php create mode 100644 app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUsedInProductsMap.php create mode 100644 app/code/Magento/CatalogUrlRewrite/Model/Map/DataMapInterface.php create mode 100644 app/code/Magento/CatalogUrlRewrite/Model/Map/DataMapPool.php create mode 100644 app/code/Magento/CatalogUrlRewrite/Model/Map/DataMapPoolInterface.php create mode 100644 app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductMap.php create mode 100644 app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductUrlRewriteMap.php create mode 100644 app/code/Magento/CatalogUrlRewrite/Model/Map/MapFactory.php create mode 100644 app/code/Magento/CatalogUrlRewrite/Model/Map/MapFactoryInterface.php create mode 100644 app/code/Magento/CatalogUrlRewrite/Model/Map/MapInterface.php create mode 100644 app/code/Magento/CatalogUrlRewrite/Model/Map/MapPool.php create mode 100644 app/code/Magento/CatalogUrlRewrite/Model/Map/MapPoolInterface.php create mode 100644 app/code/Magento/CatalogUrlRewrite/Model/Map/ProductUrlRewriteMap.php create mode 100644 app/code/Magento/CatalogUrlRewrite/Observer/CategoryProcessUrlRewriteInMemorySavingObserver.php create mode 100644 dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/Observer/CategoryProcessUrlRewriteSavingObserverTest.php create mode 100644 lib/internal/Magento/Framework/DB/TemporaryTableService.php diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Category/ChildrenUrlRewriteGenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/Category/ChildrenUrlRewriteGenerator.php index d99a7af6408c3..7e24a510b0817 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Category/ChildrenUrlRewriteGenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Category/ChildrenUrlRewriteGenerator.php @@ -7,6 +7,7 @@ use Magento\Catalog\Model\Category; use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGeneratorFactory; +use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator; class ChildrenUrlRewriteGenerator { @@ -33,18 +34,22 @@ public function __construct( * * @param int $storeId * @param \Magento\Catalog\Model\Category $category + * @param int|null $rootCategoryId * @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] */ - public function generate($storeId, Category $category) + public function generate($storeId, Category $category, $rootCategoryId = null) { $urls = []; - foreach ($this->childrenCategoriesProvider->getChildren($category) as $childCategory) { + foreach ($this->childrenCategoriesProvider->getChildren($category, true) as $childCategory) { $childCategory->setStoreId($storeId); $childCategory->setData('save_rewrites_history', $category->getData('save_rewrites_history')); - $urls = array_merge( - $urls, - $this->categoryUrlRewriteGeneratorFactory->create()->generate($childCategory) - ); + /** @var CategoryUrlRewriteGenerator $categoryUrlRewriteGenerator */ + $categoryUrlRewriteGenerator = $this->categoryUrlRewriteGeneratorFactory->create(); + $urlRewrites = $categoryUrlRewriteGenerator->generate($childCategory, false, $rootCategoryId); + foreach ($urlRewrites as $url) { + $urls[$url->getRequestPath() . '_' . $url->getStoreId()] = $url; + } + unset($urlRewrites); } return $urls; } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php index 1bd4372d54e02..738cac8e4c578 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php @@ -12,6 +12,9 @@ use Magento\UrlRewrite\Model\UrlFinderInterface; use Magento\UrlRewrite\Service\V1\Data\UrlRewrite; use Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory; +use Magento\CatalogUrlRewrite\Model\Map\CategoryUrlRewriteMap; +use Magento\CatalogUrlRewrite\Model\Map\MapPoolInterface; +use Magento\Framework\App\ObjectManager; class CurrentUrlRewritesRegenerator { @@ -24,22 +27,25 @@ class CurrentUrlRewritesRegenerator /** @var UrlFinderInterface */ protected $urlFinder; - /** @var \Magento\Catalog\Model\Category */ - protected $category; + /** @var MapPoolInterface */ + private $mapPool; /** * @param \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator * @param \Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory $urlRewriteFactory * @param UrlFinderInterface $urlFinder + * @param MapPoolInterface|null $mapPool */ public function __construct( CategoryUrlPathGenerator $categoryUrlPathGenerator, UrlRewriteFactory $urlRewriteFactory, - UrlFinderInterface $urlFinder + UrlFinderInterface $urlFinder, + MapPoolInterface $mapPool = null ) { $this->categoryUrlPathGenerator = $categoryUrlPathGenerator; $this->urlRewriteFactory = $urlRewriteFactory; $this->urlFinder = $urlFinder; + $this->mapPool = $mapPool ?: ObjectManager::getInstance()->get(MapPoolInterface::class); } /** @@ -47,27 +53,40 @@ public function __construct( * * @param int $storeId * @param \Magento\Catalog\Model\Category $category + * @param int|null $rootCategoryId * @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] */ - public function generate($storeId, Category $category) + public function generate($storeId, Category $category, $rootCategoryId = null) { - $this->category = $category; + if ($rootCategoryId) { + $categoryUrlRewriteMap = $this->mapPool->getMap(CategoryUrlRewriteMap::class, $rootCategoryId); - $currentUrlRewrites = $this->urlFinder->findAllByData( - [ - UrlRewrite::STORE_ID => $storeId, - UrlRewrite::ENTITY_ID => $category->getId(), - UrlRewrite::ENTITY_TYPE => CategoryUrlRewriteGenerator::ENTITY_TYPE, - ] - ); + /** @var UrlRewrite[] $currentUrlRewrites */ + $currentUrlRewrites = $categoryUrlRewriteMap->getByIdentifiers( + [ + UrlRewrite::STORE_ID => $storeId, + UrlRewrite::ENTITY_ID => $category->getEntityId() + ] + ); + } else { + $currentUrlRewrites = $this->urlFinder->findAllByData( + [ + UrlRewrite::STORE_ID => $storeId, + UrlRewrite::ENTITY_ID => $category->getEntityId(), + UrlRewrite::ENTITY_TYPE => CategoryUrlRewriteMap::ENTITY_TYPE, + ] + ); + } $urlRewrites = []; foreach ($currentUrlRewrites as $rewrite) { - if ($rewrite->getIsAutogenerated()) { - $urlRewrites = array_merge($urlRewrites, $this->generateForAutogenerated($rewrite, $storeId)); - } else { - $urlRewrites = array_merge($urlRewrites, $this->generateForCustom($rewrite, $storeId)); + $urls = $rewrite->getIsAutogenerated() + ? $this->generateForAutogenerated($rewrite, $storeId, $category) + : $this->generateForCustom($rewrite, $storeId, $category); + foreach ($urls as $url) { + $urlRewrites[$url->getRequestPath() . '_' . $url->getStoreId()] = $url; } + unset($urls); } return $urlRewrites; } @@ -75,17 +94,18 @@ public function generate($storeId, Category $category) /** * @param \Magento\UrlRewrite\Service\V1\Data\UrlRewrite $url * @param int $storeId - * @return array + * @param Category $category + * @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] */ - protected function generateForAutogenerated($url, $storeId) + protected function generateForAutogenerated($url, $storeId, Category $category) { $urls = []; - if ($this->category->getData('save_rewrites_history')) { - $targetPath = $this->categoryUrlPathGenerator->getUrlPathWithSuffix($this->category, $storeId); + if ($category->getData('save_rewrites_history')) { + $targetPath = $this->categoryUrlPathGenerator->getUrlPathWithSuffix($category, $storeId); if ($url->getRequestPath() !== $targetPath) { $urls[$url->getRequestPath() . '_' . $storeId] = $this->urlRewriteFactory->create() ->setEntityType(CategoryUrlRewriteGenerator::ENTITY_TYPE) - ->setEntityId($this->category->getId()) + ->setEntityId($category->getEntityId()) ->setRequestPath($url->getRequestPath()) ->setTargetPath($targetPath) ->setRedirectType(OptionProvider::PERMANENT) @@ -99,18 +119,19 @@ protected function generateForAutogenerated($url, $storeId) /** * @param \Magento\UrlRewrite\Service\V1\Data\UrlRewrite $url * @param int $storeId - * @return array + * @param Category $category + * @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] */ - protected function generateForCustom($url, $storeId) + protected function generateForCustom($url, $storeId, Category $category) { $urls = []; $targetPath = !$url->getRedirectType() ? $url->getTargetPath() - : $this->categoryUrlPathGenerator->getUrlPathWithSuffix($this->category, $storeId); + : $this->categoryUrlPathGenerator->getUrlPathWithSuffix($category, $storeId); if ($url->getRequestPath() !== $targetPath) { $urls[$url->getRequestPath() . '_' . $storeId] = $this->urlRewriteFactory->create() ->setEntityType(CategoryUrlRewriteGenerator::ENTITY_TYPE) - ->setEntityId($this->category->getId()) + ->setEntityId($category->getEntityId()) ->setRequestPath($url->getRequestPath()) ->setTargetPath($targetPath) ->setRedirectType($url->getRedirectType()) diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Storage.php b/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Storage.php index 740c32762690e..b6c3be4f2489c 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Storage.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Storage.php @@ -62,14 +62,7 @@ public function afterReplace(StorageInterface $object, $result, array $urls) */ public function beforeDeleteByData(StorageInterface $object, array $data) { - $toRemove = []; - $records = $this->urlFinder->findAllByData($data); - foreach ($records as $record) { - $toRemove[] = $record->getUrlRewriteId(); - } - if ($toRemove) { - $this->productFactory->create()->getResource()->removeMultiple($toRemove); - } + $this->productFactory->create()->getResource()->removeMultipleByEntityStore($data); } /** diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Store/Group.php b/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Store/Group.php index 1b18385d4a6fd..d4e648ce03afb 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Store/Group.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Store/Group.php @@ -35,7 +35,7 @@ class Group /** @var ProductUrlRewriteGenerator */ protected $productUrlRewriteGenerator; - /** @var StoreManagerInterface */ + /** @var StoreManagerInterface */ protected $storeManager; /** diff --git a/app/code/Magento/CatalogUrlRewrite/Model/CategoryBasedProductRewriteGenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/CategoryBasedProductRewriteGenerator.php index 58025ad03f0b4..2fae585cf0009 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/CategoryBasedProductRewriteGenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/CategoryBasedProductRewriteGenerator.php @@ -7,11 +7,6 @@ use Magento\Catalog\Model\Category; use Magento\Catalog\Model\Product; -use Magento\CatalogUrlRewrite\Model\Product\CanonicalUrlRewriteGenerator; -use Magento\CatalogUrlRewrite\Model\Product\CategoriesUrlRewriteGenerator; -use Magento\CatalogUrlRewrite\Model\Product\CurrentUrlRewritesRegenerator; -use Magento\CatalogUrlRewrite\Model\Product\AnchorUrlRewriteGenerator; -use Magento\CatalogUrlRewrite\Service\V1\StoreViewService; use Magento\Store\Model\Store; use Magento\Catalog\Model\Product\Visibility; @@ -28,7 +23,7 @@ class CategoryBasedProductRewriteGenerator private $productScopeRewriteGenerator; /** - * @param ProductScopeRewriteGenerator $productUrlRewriteGenerator + * @param ProductScopeRewriteGenerator $productScopeRewriteGenerator */ public function __construct( ProductScopeRewriteGenerator $productScopeRewriteGenerator @@ -41,9 +36,10 @@ public function __construct( * * @param \Magento\Catalog\Model\Product $product * @param \Magento\Catalog\Model\Category $category + * @param int|null $rootCategoryId * @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] */ - public function generate(Product $product, Category $category) + public function generate(Product $product, Category $category, $rootCategoryId = null) { if ($product->getVisibility() == Visibility::VISIBILITY_NOT_VISIBLE) { return []; @@ -52,10 +48,14 @@ public function generate(Product $product, Category $category) $storeId = $product->getStoreId(); $urls = $this->productScopeRewriteGenerator->isGlobalScope($storeId) - ? $this->productScopeRewriteGenerator->generateForGlobalScope([$category], $product) - : $this->productScopeRewriteGenerator->generateForSpecificStoreView($storeId, [$category], $product); + ? $this->productScopeRewriteGenerator->generateForGlobalScope([$category], $product, $rootCategoryId) + : $this->productScopeRewriteGenerator->generateForSpecificStoreView( + $storeId, + [$category], + $product, + $rootCategoryId + ); - $this->product = null; return $urls; } } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGenerator.php index 166785b01c0c5..b0738f4616722 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGenerator.php @@ -21,9 +21,6 @@ class CategoryUrlRewriteGenerator /** @var StoreViewService */ protected $storeViewService; - /** @var \Magento\Catalog\Model\Category */ - protected $category; - /** @var \Magento\CatalogUrlRewrite\Model\Category\CanonicalUrlRewriteGenerator */ protected $canonicalUrlRewriteGenerator; @@ -60,37 +57,47 @@ public function __construct( } /** - * {@inheritdoc} + * @param \Magento\Catalog\Model\Category $category + * @param bool $overrideStoreUrls + * @param int|null $rootCategoryId + * @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] */ - public function generate($category, $overrideStoreUrls = false) + public function generate(Category $category, $overrideStoreUrls = false, $rootCategoryId = null) { - $this->category = $category; - $this->overrideStoreUrls = $overrideStoreUrls; + if ($rootCategoryId === null) { + $rootCategoryId = $category->getEntityId(); + } - $storeId = $this->category->getStoreId(); + $storeId = $category->getStoreId(); $urls = $this->isGlobalScope($storeId) - ? $this->generateForGlobalScope() - : $this->generateForSpecificStoreView($storeId); + ? $this->generateForGlobalScope($category, $rootCategoryId, $overrideStoreUrls) + : $this->generateForSpecificStoreView($category, $storeId, $rootCategoryId); - $this->category = null; return $urls; } /** * Generate list of urls for global scope * + * @param \Magento\Catalog\Model\Category $category + * @param bool $overrideStoreUrls + * @param int|null $rootCategoryId * @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] */ - protected function generateForGlobalScope() + protected function generateForGlobalScope(Category $category, $overrideStoreUrls, $rootCategoryId = null) { $urls = []; - $categoryId = $this->category->getId(); - foreach ($this->category->getStoreIds() as $storeId) { + $categoryId = $category->getId(); + foreach ($category->getStoreIds() as $storeId) { if (!$this->isGlobalScope($storeId) - && $this->isOverrideUrlsForStore($storeId, $categoryId) + && $this->isOverrideUrlsForStore($storeId, $categoryId, $overrideStoreUrls) ) { - $this->updateCategoryUrlForStore($storeId); - $urls = array_merge($urls, $this->generateForSpecificStoreView($storeId)); + $this->updateCategoryUrlForStore($category, $storeId); + $specificStoreArray = $this->generateForSpecificStoreView($category, $storeId, $rootCategoryId); + foreach ($specificStoreArray as $url) { + $urls[$url->getRequestPath() . '_' . $url->getStoreId()] = $url; + } + unset($specificStoreArray); } } return $urls; @@ -99,11 +106,12 @@ protected function generateForGlobalScope() /** * @param int $storeId * @param int $categoryId + * @param bool $overrideStoreUrls * @return bool */ - protected function isOverrideUrlsForStore($storeId, $categoryId) + protected function isOverrideUrlsForStore($storeId, $categoryId, $overrideStoreUrls) { - return $this->overrideStoreUrls || !$this->storeViewService->doesEntityHaveOverriddenUrlKeyForStore( + return $overrideStoreUrls || !$this->storeViewService->doesEntityHaveOverriddenUrlKeyForStore( $storeId, $categoryId, Category::ENTITY @@ -113,13 +121,14 @@ protected function isOverrideUrlsForStore($storeId, $categoryId) /** * Override url key and url path for category in specific Store * + * @param \Magento\Catalog\Model\Category $category * @param int $storeId * @return void */ - protected function updateCategoryUrlForStore($storeId) + protected function updateCategoryUrlForStore(Category $category, $storeId) { - $category = $this->categoryRepository->get($this->category->getId(), $storeId); - $this->category->addData(['url_key' => $category->getUrlKey(), 'url_path' => $category->getUrlPath()]); + $object = $this->categoryRepository->get($category->getId(), $storeId); + $category->addData(['url_key' => $object->getUrlKey(), 'url_path' => $object->getUrlPath()]); } /** @@ -136,16 +145,31 @@ protected function isGlobalScope($storeId) /** * Generate list of urls per store * + * @param \Magento\Catalog\Model\Category $category * @param string $storeId + * @param int|null $rootCategoryId * @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] */ - protected function generateForSpecificStoreView($storeId) + protected function generateForSpecificStoreView(Category $category, $storeId, $rootCategoryId = null) { - $urls = array_merge( - $this->canonicalUrlRewriteGenerator->generate($storeId, $this->category), - $this->childrenUrlRewriteGenerator->generate($storeId, $this->category), - $this->currentUrlRewritesRegenerator->generate($storeId, $this->category) - ); + $urls = []; + $canonicalUrl = $this->canonicalUrlRewriteGenerator->generate($storeId, $category); + foreach ($canonicalUrl as $url) { + $urls[$url->getRequestPath() . '_' . $url->getStoreId()] = $url; + } + unset($canonicalUrl); + + $childrenUrl = $this->childrenUrlRewriteGenerator->generate($storeId, $category, $rootCategoryId); + foreach ($childrenUrl as $url) { + $urls[$url->getRequestPath() . '_' . $url->getStoreId()] = $url; + } + unset($childrenUrl); + + $currentUrl = $this->currentUrlRewritesRegenerator->generate($storeId, $category, $rootCategoryId); + foreach ($currentUrl as $url) { + $urls[$url->getRequestPath() . '_' . $url->getStoreId()] = $url; + } + unset($currentUrl); return $urls; } } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/CategoryUrlRewriteMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/CategoryUrlRewriteMap.php new file mode 100644 index 0000000000000..52f4bf32ff410 --- /dev/null +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/CategoryUrlRewriteMap.php @@ -0,0 +1,138 @@ +dataMapPool = $dataMapPool; + $this->urlFinder = $urlFinder; + $this->connection = $connection; + $this->urlRewritePlaceholder = $urlRewriteFactory->create(); + $this->categoryId = $categoryId; + } + + /** + * Gets the results from a map by identifiers + * + * @param array $identifiers + * @return UrlRewrite[] + */ + public function getByIdentifiers($identifiers) + { + if ($this->categoryId) { + $array = $this->dataMapPool->getDataMap(DataCategoryUrlRewriteMap::class, $this->categoryId) + ->getData($this->categoryId); + $tableName = reset($array); + if (!empty($array)) { + if (isset($identifiers['store_id']) && isset($identifiers['entity_id'])) { + if (!is_array($identifiers['entity_id']) && !is_array($identifiers['store_id'])) { + $key = $identifiers['store_id'] . '_' . $identifiers['entity_id']; + $urlRewritesConnection = $this->connection->getConnection(); + $select = $urlRewritesConnection->select() + ->from(['e' => $tableName]) + ->where('hash_key = ?', $key); + return $this->arrayToUrlRewriteObject($urlRewritesConnection->fetchAll($select)); + } else if (is_array($identifiers['entity_id']) && is_array($identifiers['store_id'])) { + $urlRewritesConnection = $this->connection->getConnection(); + $select = $urlRewritesConnection->select() + ->from(['e' => $tableName]) + ->where( + $urlRewritesConnection->prepareSqlCondition( + 'store_id', + ['in' => $identifiers['store_id']] + ) + ) + ->where( + $urlRewritesConnection->prepareSqlCondition( + 'entity_id', + ['in' => $identifiers['entity_id']] + ) + ); + return $this->arrayToUrlRewriteObject($urlRewritesConnection->fetchAll($select)); + } + } + } + } + return $this->urlFinder->findAllByData($identifiers); + } + + /** + * Transform array values to url rewrite object values + * + * @param array $data + * @return UrlRewrite[] + */ + private function arrayToUrlRewriteObject($data) + { + foreach ($data as $key => $array) { + $data[$key] = $this->createUrlRewrite($array); + } + return $data; + } + + /** + * Clone url rewrite object + * + * @param array $data + * @return UrlRewrite + */ + private function createUrlRewrite($data) + { + $dataObject = clone $this->urlRewritePlaceholder; + $dataObject->setUrlRewriteId($data['url_rewrite_id']); + $dataObject->setEntityType($data['entity_type']); + $dataObject->setEntityId($data['entity_id']); + $dataObject->setRequestPath($data['request_path']); + $dataObject->setTargetPath($data['target_path']); + $dataObject->setRedirectType($data['redirect_type']); + $dataObject->setStoreId($data['store_id']); + $dataObject->setDescription($data['description']); + $dataObject->setIsAutogenerated($data['is_autogenerated']); + $dataObject->setMetadata($data['metadata']); + + return $dataObject; + } +} diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryMap.php new file mode 100644 index 0000000000000..77f675c4b38c2 --- /dev/null +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryMap.php @@ -0,0 +1,87 @@ +categoryFactory = $categoryFactory; + } + + /** + * Gets all data from a map identified by a category Id + * + * @param int $categoryId + * @return array + */ + public function getData($categoryId) + { + if (empty($this->data[$categoryId])) { + $this->data[$categoryId] = $this->queryData($categoryId); + } + return $this->data[$categoryId]; + } + + /** + * Queries the database and returns results + * + * @param int $categoryId + * @return array + */ + private function queryData($categoryId) + { + $category = $this->categoryFactory->create()->load($categoryId); + return $category->getResourceCollection() + ->addIdFilter($this->getAllCategoryChildrenIds($category)) + ->getAllIds(); + } + + /** + * Queries sub-categories ids from a category + * + * @param Category $category + * @return int[] + */ + private function getAllCategoryChildrenIds($category) + { + $connection = $category->getResource()->getConnection(); + $select = $connection->select() + ->from($category->getResource()->getEntityTable(), 'entity_id') + ->where($connection->quoteIdentifier('path') . ' LIKE :c_path'); + $bind = ['c_path' => $category->getPath() . '%']; + return $connection->fetchCol($select, $bind); + } + + /** + * Resets current map + * + * @param int $categoryId + * @return $this + */ + public function resetData($categoryId) + { + unset($this->data); + $this->data = []; + return $this; + } +} diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUrlRewriteMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUrlRewriteMap.php new file mode 100644 index 0000000000000..daed66df6c478 --- /dev/null +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUrlRewriteMap.php @@ -0,0 +1,128 @@ +connection = $connection; + $this->dataMapPool = $dataMapPool; + $this->urlRewriteFactory = $urlRewriteFactory; + $this->temporaryTableService = $temporaryTableService; + $this->urlRewritePlaceholder = $this->urlRewriteFactory->create(); + } + + /** + * Gets all data from a map identified by a category Id + * + * @param int $categoryId + * @return array + */ + public function getData($categoryId) + { + if (empty($this->data[$categoryId])) { + $this->data[$categoryId] = [$this->queryData($categoryId)]; + } + return $this->data[$categoryId]; + } + + /** + * Queries the database and returns the name of the temporary table where data is stored + * + * @param int $categoryId + * @return string + */ + private function queryData($categoryId) + { + $urlRewritesConnection = $this->connection->getConnection(); + $select = $urlRewritesConnection->select() + ->from( + ['e' => $this->connection->getTableName('url_rewrite')], + ['e.*', 'hash_key' => new \Zend_Db_Expr('CONCAT(e.store_id,\'_\', e.entity_id)')] + ) + ->where('entity_type = ?', self::ENTITY_TYPE) + ->where( + $urlRewritesConnection->prepareSqlCondition( + 'entity_id', + [ + 'in' => array_merge( + $this->dataMapPool->getDataMap(DataCategoryUsedInProductsMap::class, $categoryId) + ->getData($categoryId), + $this->dataMapPool->getDataMap(DataCategoryMap::class, $categoryId) + ->getData($categoryId) + ) + ] + ) + ); + $mapName = $this->temporaryTableService->createTemporaryTable( + $select, + $this->connection->getConnection(), + [ + 'PRIMARY' => ['url_rewrite_id'], + 'HASHKEY_ENTITY_STORE' => ['hash_key'], + 'ENTITY_STORE' => ['entity_id', 'store_id'] + ] + ); + return $mapName; + } + + /** + * Resets current map and it's dependencies + * + * @param int $categoryId + * @return $this + */ + public function resetData($categoryId) + { + $this->dataMapPool->resetDataMap(DataCategoryUsedInProductsMap::class, $categoryId); + $this->dataMapPool->resetDataMap(DataCategoryMap::class, $categoryId); + foreach ($this->data as $tableName) { + $this->temporaryTableService->dropTable(reset($tableName)); + } + unset($this->data); + $this->data = []; + return $this; + } +} diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUsedInProductsMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUsedInProductsMap.php new file mode 100644 index 0000000000000..e1e1308c6a8bf --- /dev/null +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUsedInProductsMap.php @@ -0,0 +1,92 @@ +connection = $connection; + $this->dataMapPool = $dataMapPool; + } + + /** + * Gets all data from a map identified by a category Id + * + * @param int $categoryId + * @return array + */ + public function getData($categoryId) + { + if (empty($this->data[$categoryId])) { + $this->data[$categoryId] = $this->queryData($categoryId); + } + return $this->data[$categoryId]; + } + + /** + * Queries the database and returns results + * + * @param int $categoryId + * @return array + */ + private function queryData($categoryId) + { + $productsLinkConnection = $this->connection->getConnection(); + $select = $productsLinkConnection->select() + ->from($this->connection->getTableName('catalog_category_product'), ['category_id']) + ->where( + $productsLinkConnection->prepareSqlCondition( + 'product_id', + ['in' => $this->dataMapPool->getDataMap(DataProductMap::class, $categoryId)->getData($categoryId)] + ) + ) + ->where( + $productsLinkConnection->prepareSqlCondition( + 'category_id', + ['nin' => $this->dataMapPool->getDataMap(DataCategoryMap::class, $categoryId)->getData($categoryId)] + ) + ) + ->group('category_id'); + + return $productsLinkConnection->fetchCol($select); + } + + /** + * Resets current map and it's dependencies + * + * @param int $categoryId + * @return $this + */ + public function resetData($categoryId) + { + $this->dataMapPool->resetDataMap(DataProductMap::class, $categoryId); + $this->dataMapPool->resetDataMap(DataCategoryMap::class, $categoryId); + unset($this->data); + $this->data = []; + return $this; + } +} diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataMapInterface.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataMapInterface.php new file mode 100644 index 0000000000000..72f5d2f60ba4e --- /dev/null +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataMapInterface.php @@ -0,0 +1,29 @@ +objectManager = $objectManager; + } + + /** + * Gets a map by instance and category Id + * + * @param string $instanceName + * @param int $categoryId + * @return DataMapInterface + */ + public function getDataMap($instanceName, $categoryId) + { + $key = $instanceName . '-' . $categoryId; + if (!isset($this->dataArray[$key])) { + $this->dataArray[$key] = $this->objectManager->create( + $instanceName, + [ + 'category' => $categoryId + ] + ); + } + return $this->dataArray[$key]; + } + + /** + * Resets a map by instance and category Id + * + * @param string $instanceName + * @param int $categoryId + * @return $this + */ + public function resetDataMap($instanceName, $categoryId) + { + $key = $instanceName . '-' . $categoryId; + if (isset($this->dataArray[$key])) { + $this->dataArray[$key]->resetData($categoryId); + unset($this->dataArray[$key]); + } + return $this; + } +} diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataMapPoolInterface.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataMapPoolInterface.php new file mode 100644 index 0000000000000..e44b5f57d0ac5 --- /dev/null +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataMapPoolInterface.php @@ -0,0 +1,30 @@ +collectionFactory = $collectionFactory; + $this->dataMapPool = $dataMapPool; + $this->connection = $connection; + } + + /** + * Gets all data from a map identified by a category Id + * + * @param int $categoryId + * @return array + */ + public function getData($categoryId) + { + if (empty($this->data[$categoryId])) { + $this->data[$categoryId] = $this->queryData($categoryId); + } + return $this->data[$categoryId]; + } + + /** + * Queries the database and returns results + * + * @param int $categoryId + * @return array + */ + private function queryData($categoryId) + { + $productsCollection = $this->collectionFactory->create(); + $productsCollection->getSelect() + ->joinInner( + ['cp' => $this->connection->getTableName('catalog_category_product')], + 'cp.product_id = e.entity_id', + [] + ) + ->where( + $productsCollection->getConnection()->prepareSqlCondition( + 'cp.category_id', + ['in' => $this->dataMapPool->getDataMap(DataCategoryMap::class, $categoryId)->getData($categoryId)] + ) + ) + ->group('e.entity_id'); + + return $productsCollection->getAllIds(); + } + + /** + * Resets current map and it's dependencies + * + * @param int $categoryId + * @return $this + */ + public function resetData($categoryId) + { + $this->dataMapPool->resetDataMap(DataCategoryMap::class, $categoryId); + unset($this->data); + $this->data = []; + return $this; + } +} diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductUrlRewriteMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductUrlRewriteMap.php new file mode 100644 index 0000000000000..36363220ff561 --- /dev/null +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductUrlRewriteMap.php @@ -0,0 +1,123 @@ +connection = $connection; + $this->dataMapPool = $dataMapPool; + $this->urlRewriteFactory = $urlRewriteFactory; + $this->temporaryTableService = $temporaryTableService; + $this->urlRewritePlaceholder = $this->urlRewriteFactory->create(); + } + + /** + * Gets all data from a map identified by a category Id + * + * @param int $categoryId + * @return array + */ + public function getData($categoryId) + { + if (empty($this->data[$categoryId])) { + $this->data[$categoryId] = [$this->queryData($categoryId)]; + } + return $this->data[$categoryId]; + } + + /** + * Queries the database and returns the name of the temporary table where data is stored + * + * @param int $categoryId + * @return string + */ + private function queryData($categoryId) + { + $urlRewritesConnection = $this->connection->getConnection(); + $select = $urlRewritesConnection->select() + ->from( + ['e' => $this->connection->getTableName('url_rewrite')], + ['e.*', 'hash_key' => new \Zend_Db_Expr('CONCAT(e.store_id,\'_\', e.entity_id)')] + ) + ->where('entity_type = ?', self::ENTITY_TYPE) + ->where( + $urlRewritesConnection->prepareSqlCondition( + 'entity_id', + [ + 'in' => $this->dataMapPool->getDataMap(DataProductMap::class, $categoryId) + ->getData($categoryId) + ] + ) + ); + $mapName = $this->temporaryTableService->createTemporaryTable( + $select, + $this->connection->getConnection(), + [ + 'PRIMARY' => ['url_rewrite_id'], + 'HASHKEY_ENTITY_STORE' => ['hash_key'], + 'ENTITY_STORE' => ['entity_id', 'store_id'] + ] + ); + return $mapName; + } + + /** + * Resets current map and it's dependencies + * + * @param int $categoryId + * @return $this + */ + public function resetData($categoryId) + { + $this->dataMapPool->resetDataMap(DataProductMap::class, $categoryId); + foreach ($this->data as $tableName) { + $this->temporaryTableService->dropTable(reset($tableName)); + } + unset($this->data); + $this->data = []; + return $this; + } +} diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/MapFactory.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/MapFactory.php new file mode 100644 index 0000000000000..e4b3392e96fe5 --- /dev/null +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/MapFactory.php @@ -0,0 +1,45 @@ +objectManager = $objectManager; + } + + /** + * Creates a client map + * + * @param string $instanceName + * @param int $categoryId + * @return MapInterface + */ + public function create($instanceName, $categoryId) + { + return $this->objectManager->create( + $instanceName, + ['categoryId' => $categoryId] + ); + } +} diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/MapFactoryInterface.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/MapFactoryInterface.php new file mode 100644 index 0000000000000..b2b3943ed38a7 --- /dev/null +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/MapFactoryInterface.php @@ -0,0 +1,21 @@ +clientMapFactory = $clientMapFactory; + } + + /** + * Gets map instance identified by category id + * + * @param string $instanceName + * @param int $categoryId + * @return MapInterface + */ + public function getMap($instanceName, $categoryId) + { + $key = $instanceName . '-' . $categoryId; + if (!isset($this->mapStoragePool[$key])) { + $this->mapStoragePool[$key] = $this->clientMapFactory->create($instanceName, $categoryId); + } + return $this->mapStoragePool[$key]; + } +} diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/MapPoolInterface.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/MapPoolInterface.php new file mode 100644 index 0000000000000..80e34c9088dc6 --- /dev/null +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/MapPoolInterface.php @@ -0,0 +1,23 @@ +dataMapPool = $dataMapPool; + $this->urlFinder = $urlFinder; + $this->connection = $connection; + $this->urlRewritePlaceholder = $urlRewriteFactory->create(); + $this->categoryId = $categoryId; + } + + /** + * Gets the results from a map by identifiers + * + * @param array $identifiers + * @return UrlRewrite[] + */ + public function getByIdentifiers($identifiers) + { + if ($this->categoryId) { + $array = $this->dataMapPool->getDataMap(DataProductUrlRewriteMap::class, $this->categoryId) + ->getData($this->categoryId); + $tableName = reset($array); + if (!empty($array)) { + if (isset($identifiers['store_id']) && isset($identifiers['entity_id'])) { + if (!is_array($identifiers['entity_id']) && !is_array($identifiers['store_id'])) { + $key = $identifiers['store_id'] . '_' . $identifiers['entity_id']; + $urlRewritesConnection = $this->connection->getConnection(); + $select = $urlRewritesConnection->select() + ->from(['e' => $tableName]) + ->where('hash_key = ?', $key); + return $this->arrayToUrlRewriteObject($urlRewritesConnection->fetchAll($select)); + } else if (is_array($identifiers['entity_id']) && is_array($identifiers['store_id'])) { + $urlRewritesConnection = $this->connection->getConnection(); + $select = $urlRewritesConnection->select() + ->from(['e' => $tableName]) + ->where( + $urlRewritesConnection->prepareSqlCondition( + 'store_id', + ['in' => $identifiers['store_id']] + ) + ) + ->where( + $urlRewritesConnection->prepareSqlCondition( + 'entity_id', + ['in' => $identifiers['entity_id']] + ) + ); + return $this->arrayToUrlRewriteObject($urlRewritesConnection->fetchAll($select)); + } + } + } + } + return $this->urlFinder->findAllByData($identifiers); + } + + /** + * Transform array values to url rewrite object values + * + * @param array $data + * @return UrlRewrite[] + */ + private function arrayToUrlRewriteObject($data) + { + foreach ($data as $key => $array) { + $data[$key] = $this->createUrlRewrite($array); + } + return $data; + } + + /** + * Clone url rewrite object + * + * @param array $data + * @return UrlRewrite + */ + private function createUrlRewrite($data) + { + $dataObject = clone $this->urlRewritePlaceholder; + $dataObject->setUrlRewriteId($data['url_rewrite_id']); + $dataObject->setEntityType($data['entity_type']); + $dataObject->setEntityId($data['entity_id']); + $dataObject->setRequestPath($data['request_path']); + $dataObject->setTargetPath($data['target_path']); + $dataObject->setRedirectType($data['redirect_type']); + $dataObject->setStoreId($data['store_id']); + $dataObject->setDescription($data['description']); + $dataObject->setIsAutogenerated($data['is_autogenerated']); + $dataObject->setMetadata($data['metadata']); + + return $dataObject; + } +} diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php index 089c6c5f2c1a6..38e35869531b8 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php @@ -14,19 +14,15 @@ use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator; use Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator; use Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory; -use Magento\Store\Model\StoreManagerInterface; +use Magento\CatalogUrlRewrite\Model\Map\ProductUrlRewriteMap; +use Magento\CatalogUrlRewrite\Model\Map\MapPoolInterface; +use Magento\Framework\App\ObjectManager; /** * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ class CurrentUrlRewritesRegenerator { - /** @var Product */ - protected $product; - - /** @var ObjectRegistry */ - protected $productCategories; - /** @var UrlFinderInterface */ protected $urlFinder; @@ -36,19 +32,25 @@ class CurrentUrlRewritesRegenerator /** @var UrlRewriteFactory */ protected $urlRewriteFactory; + /** @var MapPoolInterface */ + private $mapPool; + /** * @param UrlFinderInterface $urlFinder * @param ProductUrlPathGenerator $productUrlPathGenerator * @param UrlRewriteFactory $urlRewriteFactory + * @param MapPoolInterface|null $mapPool */ public function __construct( UrlFinderInterface $urlFinder, ProductUrlPathGenerator $productUrlPathGenerator, - UrlRewriteFactory $urlRewriteFactory + UrlRewriteFactory $urlRewriteFactory, + MapPoolInterface $mapPool = null ) { $this->urlFinder = $urlFinder; $this->productUrlPathGenerator = $productUrlPathGenerator; $this->urlRewriteFactory = $urlRewriteFactory; + $this->mapPool = $mapPool ?: ObjectManager::getInstance()->get(MapPoolInterface::class); } /** @@ -57,35 +59,45 @@ public function __construct( * @param int $storeId * @param Product $product * @param ObjectRegistry $productCategories + * @param int|null $rootCategoryId * @return UrlRewrite[] */ - public function generate($storeId, Product $product, ObjectRegistry $productCategories) + public function generate($storeId, Product $product, ObjectRegistry $productCategories, $rootCategoryId = null) { - $this->product = $product; - $this->productCategories = $productCategories; + if ($rootCategoryId) { + $productUrlRewriteMap = $this->mapPool->getMap(ProductUrlRewriteMap::class, $rootCategoryId); - $currentUrlRewrites = $this->urlFinder->findAllByData( - [ - UrlRewrite::STORE_ID => $storeId, - UrlRewrite::ENTITY_ID => $this->product->getId(), - UrlRewrite::ENTITY_TYPE => ProductUrlRewriteGenerator::ENTITY_TYPE, - ] - ); + $currentUrlRewrites = $productUrlRewriteMap->getByIdentifiers( + [ + UrlRewrite::STORE_ID => $storeId, + UrlRewrite::ENTITY_ID => $product->getEntityId() + ] + ); + } else { + $currentUrlRewrites = $this->urlFinder->findAllByData( + [ + UrlRewrite::STORE_ID => $storeId, + UrlRewrite::ENTITY_ID => $product->getEntityId(), + UrlRewrite::ENTITY_TYPE => ProductUrlRewriteMap::ENTITY_TYPE, + ] + ); + } $urlRewrites = []; foreach ($currentUrlRewrites as $currentUrlRewrite) { - $category = $this->retrieveCategoryFromMetadata($currentUrlRewrite); + $category = $this->retrieveCategoryFromMetadata($currentUrlRewrite, $productCategories); if ($category === false) { continue; } - $url = $currentUrlRewrite->getIsAutogenerated() - ? $this->generateForAutogenerated($currentUrlRewrite, $storeId, $category) - : $this->generateForCustom($currentUrlRewrite, $storeId, $category); - $urlRewrites = array_merge($urlRewrites, $url); + $urls = $currentUrlRewrite->getIsAutogenerated() + ? $this->generateForAutogenerated($currentUrlRewrite, $storeId, $category, $product) + : $this->generateForCustom($currentUrlRewrite, $storeId, $category, $product); + foreach ($urls as $url) { + $urlRewrites[$url->getRequestPath() . '_' . $url->getStoreId()] = $url; + } + unset($urls); } - $this->product = null; - $this->productCategories = null; return $urlRewrites; } @@ -93,21 +105,22 @@ public function generate($storeId, Product $product, ObjectRegistry $productCate * @param UrlRewrite $url * @param int $storeId * @param Category|null $category - * @return array + * @param Product $product + * @return UrlRewrite[] */ - protected function generateForAutogenerated($url, $storeId, $category) + protected function generateForAutogenerated(UrlRewrite $url, $storeId, $category, Product $product) { - if (!$this->product->getData('save_rewrites_history')) { + if (!$product->getData('save_rewrites_history')) { return []; } - $targetPath = $this->productUrlPathGenerator->getUrlPathWithSuffix($this->product, $storeId, $category); + $targetPath = $this->productUrlPathGenerator->getUrlPathWithSuffix($product, $storeId, $category); if ($url->getRequestPath() === $targetPath) { return []; } return [ $this->urlRewriteFactory->create() ->setEntityType(ProductUrlRewriteGenerator::ENTITY_TYPE) - ->setEntityId($this->product->getId()) + ->setEntityId($product->getEntityId()) ->setRequestPath($url->getRequestPath()) ->setTargetPath($targetPath) ->setRedirectType(OptionProvider::PERMANENT) @@ -122,12 +135,13 @@ protected function generateForAutogenerated($url, $storeId, $category) * @param UrlRewrite $url * @param int $storeId * @param Category|null $category - * @return array + * @param Product $product + * @return UrlRewrite[] */ - protected function generateForCustom($url, $storeId, $category) + protected function generateForCustom(UrlRewrite $url, $storeId, $category, Product $product) { $targetPath = $url->getRedirectType() - ? $this->productUrlPathGenerator->getUrlPathWithSuffix($this->product, $storeId, $category) + ? $this->productUrlPathGenerator->getUrlPathWithSuffix($product, $storeId, $category) : $url->getTargetPath(); if ($url->getRequestPath() === $targetPath) { return []; @@ -135,7 +149,7 @@ protected function generateForCustom($url, $storeId, $category) return [ $this->urlRewriteFactory->create() ->setEntityType(ProductUrlRewriteGenerator::ENTITY_TYPE) - ->setEntityId($this->product->getId()) + ->setEntityId($product->getEntityId()) ->setRequestPath($url->getRequestPath()) ->setTargetPath($targetPath) ->setRedirectType($url->getRedirectType()) @@ -148,13 +162,14 @@ protected function generateForCustom($url, $storeId, $category) /** * @param UrlRewrite $url + * @param ObjectRegistry $productCategories * @return Category|null|bool */ - protected function retrieveCategoryFromMetadata($url) + protected function retrieveCategoryFromMetadata(UrlRewrite $url, ObjectRegistry $productCategories) { $metadata = $url->getMetadata(); if (isset($metadata['category_id'])) { - $category = $this->productCategories->get($metadata['category_id']); + $category = $productCategories->get($metadata['category_id']); return $category === null ? false : $category; } return null; diff --git a/app/code/Magento/CatalogUrlRewrite/Model/ProductScopeRewriteGenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/ProductScopeRewriteGenerator.php index 94464ec05d824..6c52bc9892685 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/ProductScopeRewriteGenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/ProductScopeRewriteGenerator.php @@ -97,20 +97,27 @@ public function isGlobalScope($storeId) /** * Generate url rewrites for global scope * + * @param \Magento\Framework\Data\Collection|\Magento\Catalog\Model\Category[] $productCategories * @param Product $product - * @param \Magento\Framework\Data\Collection $productCategories + * @param int|null $rootCategoryId * @return array */ - public function generateForGlobalScope($productCategories, Product $product) + public function generateForGlobalScope($productCategories, Product $product, $rootCategoryId = null) { $urls = []; $productId = $product->getEntityId(); foreach ($product->getStoreIds() as $id) { - if (!$this->isGlobalScope($id) - && !$this->storeViewService->doesEntityHaveOverriddenUrlKeyForStore($id, $productId, Product::ENTITY) - ) { - $urls = array_merge($urls, $this->generateForSpecificStoreView($id, $productCategories, $product)); + if (!$this->isGlobalScope($id) && + !$this->storeViewService->doesEntityHaveOverriddenUrlKeyForStore( + $id, + $productId, + Product::ENTITY + )) { + $urls = array_merge( + $urls, + $this->generateForSpecificStoreView($id, $productCategories, $product, $rootCategoryId) + ); } } @@ -121,11 +128,12 @@ public function generateForGlobalScope($productCategories, Product $product) * Generate list of urls for specific store view * * @param int $storeId - * @param \Magento\Framework\Data\Collection $productCategories + * @param \Magento\Framework\Data\Collection|Category[] $productCategories * @param \Magento\Catalog\Model\Product $product + * @param int|null $rootCategoryId * @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] */ - public function generateForSpecificStoreView($storeId, $productCategories, Product $product) + public function generateForSpecificStoreView($storeId, $productCategories, Product $product, $rootCategoryId = null) { $categories = []; foreach ($productCategories as $category) { @@ -137,20 +145,37 @@ public function generateForSpecificStoreView($storeId, $productCategories, Produ /** * @var $urls \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] */ - $urls = array_merge( - $this->canonicalUrlRewriteGenerator->generate($storeId, $product), - $this->categoriesUrlRewriteGenerator->generate($storeId, $product, $productCategories), - $this->currentUrlRewritesRegenerator->generate($storeId, $product, $productCategories), - $this->anchorUrlRewriteGenerator->generate($storeId, $product, $productCategories) + $urls =[]; + $canonicalUrl = $this->canonicalUrlRewriteGenerator->generate($storeId, $product); + foreach ($canonicalUrl as $url) { + $urls[$url->getRequestPath() . '_' . $url->getStoreId()] = $url; + } + unset($canonicalUrl); + + $categories = $this->categoriesUrlRewriteGenerator->generate($storeId, $product, $productCategories); + foreach ($categories as $url) { + $urls[$url->getRequestPath() . '_' . $url->getStoreId()] = $url; + } + unset($categories); + + $currentUrl = $this->currentUrlRewritesRegenerator->generate( + $storeId, + $product, + $productCategories, + $rootCategoryId ); + foreach ($currentUrl as $url) { + $urls[$url->getRequestPath() . '_' . $url->getStoreId()] = $url; + } + unset($currentUrl); - /* Reduce duplicates. Last wins */ - $result = []; - foreach ($urls as $url) { - $result[$url->getTargetPath() . '-' . $url->getStoreId()] = $url; + $anchor = $this->anchorUrlRewriteGenerator->generate($storeId, $product, $productCategories); + foreach ($anchor as $url) { + $urls[$url->getRequestPath() . '_' . $url->getStoreId()] = $url; } - $this->productCategories = null; - return $result; + unset($anchor); + + return $urls; } /** diff --git a/app/code/Magento/CatalogUrlRewrite/Model/ProductUrlRewriteGenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/ProductUrlRewriteGenerator.php index f8f8991f95a04..c13dcee6bfa09 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/ProductUrlRewriteGenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/ProductUrlRewriteGenerator.php @@ -31,9 +31,6 @@ class ProductUrlRewriteGenerator */ protected $storeViewService; - /** @var \Magento\Catalog\Model\Product */ - protected $product; - /** * @deprecated * @var \Magento\CatalogUrlRewrite\Model\Product\CurrentUrlRewritesRegenerator @@ -119,26 +116,25 @@ private function getProductScopeRewriteGenerator() * Generate product url rewrites * * @param \Magento\Catalog\Model\Product $product + * @param int|null $rootCategoryId * @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] */ - public function generate(Product $product) + public function generate(Product $product, $rootCategoryId = null) { if ($product->getVisibility() == Visibility::VISIBILITY_NOT_VISIBLE) { return []; } - $this->product = $product; - $storeId = $this->product->getStoreId(); + $storeId = $product->getStoreId(); $productCategories = $product->getCategoryCollection() ->addAttributeToSelect('url_key') ->addAttributeToSelect('url_path'); $urls = $this->isGlobalScope($storeId) - ? $this->generateForGlobalScope($productCategories) - : $this->generateForSpecificStoreView($storeId, $productCategories); + ? $this->generateForGlobalScope($productCategories, $product, $rootCategoryId) + : $this->generateForSpecificStoreView($storeId, $productCategories, $product, $rootCategoryId); - $this->product = null; return $urls; } @@ -159,11 +155,17 @@ protected function isGlobalScope($storeId) * * @deprecated * @param \Magento\Framework\Data\Collection $productCategories + * @param \Magento\Catalog\Model\Product $product + * @param int|null $rootCategoryId * @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] */ - protected function generateForGlobalScope($productCategories) + protected function generateForGlobalScope($productCategories, Product $product, $rootCategoryId = null) { - return $this->getProductScopeRewriteGenerator()->generateForGlobalScope($productCategories, $this->product); + return $this->getProductScopeRewriteGenerator()->generateForGlobalScope( + $productCategories, + $product, + $rootCategoryId + ); } /** @@ -172,12 +174,18 @@ protected function generateForGlobalScope($productCategories) * @deprecated * @param int $storeId * @param \Magento\Framework\Data\Collection $productCategories + * @param Product $product + * @param int|null $rootCategoryId * @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] */ - protected function generateForSpecificStoreView($storeId, $productCategories) - { + protected function generateForSpecificStoreView( + $storeId, + $productCategories, + Product $product, + $rootCategoryId = null + ) { return $this->getProductScopeRewriteGenerator() - ->generateForSpecificStoreView($storeId, $productCategories, $this->product); + ->generateForSpecificStoreView($storeId, $productCategories, $product, $rootCategoryId); } /** diff --git a/app/code/Magento/CatalogUrlRewrite/Model/ResourceModel/Category/Product.php b/app/code/Magento/CatalogUrlRewrite/Model/ResourceModel/Category/Product.php index 189d5217b4040..0d4e1b0cc6200 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/ResourceModel/Category/Product.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/ResourceModel/Category/Product.php @@ -6,6 +6,7 @@ namespace Magento\CatalogUrlRewrite\Model\ResourceModel\Category; use Magento\Framework\Model\ResourceModel\Db\AbstractDb; +use Magento\UrlRewrite\Model\Storage\DbStorage; class Product extends AbstractDb { @@ -55,6 +56,8 @@ public function saveMultiple(array $insertData) } /** + * Removes data by primary key + * * @param array $removeData * @return int */ @@ -65,4 +68,35 @@ public function removeMultiple(array $removeData) ['url_rewrite_id in (?)' => $removeData] ); } + + /** + * Removes data by entities from url_rewrite table using a select + * + * @param array $removeData + * @return int + */ + public function removeMultipleByEntityStore(array $removeData) + { + return $this->getConnection()->delete( + $this->getTable(self::TABLE_NAME), + ['url_rewrite_id in (?)' => $this->prepareSelect($removeData)] + ); + } + + /** + * Prepare select statement for specific filter + * + * @param array $data + * @return \Magento\Framework\DB\Select + */ + protected function prepareSelect($data) + { + $select = $this->getConnection()->select(); + $select->from($this->getTable(DbStorage::TABLE_NAME), 'url_rewrite_id'); + + foreach ($data as $column => $value) { + $select->where($this->getConnection()->quoteIdentifier($column) . ' IN (?)', $value); + } + return $select; + } } diff --git a/app/code/Magento/CatalogUrlRewrite/Observer/AfterImportDataObserver.php b/app/code/Magento/CatalogUrlRewrite/Observer/AfterImportDataObserver.php index a1a9a4e9fccad..6b44a3148bb2c 100644 --- a/app/code/Magento/CatalogUrlRewrite/Observer/AfterImportDataObserver.php +++ b/app/code/Magento/CatalogUrlRewrite/Observer/AfterImportDataObserver.php @@ -20,6 +20,7 @@ use Magento\UrlRewrite\Model\UrlFinderInterface; use Magento\Framework\Event\ObserverInterface; use Magento\Catalog\Model\Product\Visibility; + /** * Class AfterImportDataObserver * @@ -261,19 +262,25 @@ protected function generateUrls() /** * @var $urls \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] */ - $urls = array_merge( - $this->canonicalUrlRewriteGenerate(), - $this->categoriesUrlRewriteGenerate(), - $this->currentUrlRewritesRegenerate() - ); - - /* Reduce duplicates. Last wins */ $result = []; + $urls = $this->canonicalUrlRewriteGenerate(); + foreach ($urls as $url) { + $result[$url->getRequestPath() . '_' . $url->getStoreId()] = $url; + } + unset($urls); + $urls = $this->categoriesUrlRewriteGenerate(); + foreach ($urls as $url) { + $result[$url->getRequestPath() . '_' . $url->getStoreId()] = $url; + } + unset($urls); + $urls = $this->currentUrlRewritesRegenerate(); foreach ($urls as $url) { - $result[$url->getTargetPath() . '-' . $url->getStoreId()] = $url; + $result[$url->getRequestPath() . '_' . $url->getStoreId()] = $url; } + unset($urls); $this->productCategories = null; + unset($this->products); $this->products = []; return $result; } diff --git a/app/code/Magento/CatalogUrlRewrite/Observer/CategoryProcessUrlRewriteInMemorySavingObserver.php b/app/code/Magento/CatalogUrlRewrite/Observer/CategoryProcessUrlRewriteInMemorySavingObserver.php new file mode 100644 index 0000000000000..b3855c3fafa61 --- /dev/null +++ b/app/code/Magento/CatalogUrlRewrite/Observer/CategoryProcessUrlRewriteInMemorySavingObserver.php @@ -0,0 +1,75 @@ +categoryUrlRewriteGenerator = $categoryUrlRewriteGenerator; + $this->urlRewriteHandler = $urlRewriteHandler; + $this->urlRewriteBunchReplacer = $urlRewriteBunchReplacer; + $this->dataMapPoolInterface = $dataMapPoolInterface; + } + + /** + * Generate urls for UrlRewrite and save it in storage + * + * @param \Magento\Framework\Event\Observer $observer + * @return void + */ + public function execute(\Magento\Framework\Event\Observer $observer) + { + /** @var Category $category */ + $category = $observer->getEvent()->getData('category'); + if ($category->getParentId() == Category::TREE_ROOT_ID) { + return; + } + if ($category->dataHasChangedFor('url_key') + || $category->dataHasChangedFor('is_anchor') + || $category->getIsChangedProductList() + ) { + $categoryUrlRewriteResult = $this->categoryUrlRewriteGenerator->generate($category); + $this->urlRewriteBunchReplacer->doBunchReplace($categoryUrlRewriteResult); + + $productUrlRewriteResult = $this->urlRewriteHandler->generateProductUrlRewrites($category); + $this->urlRewriteBunchReplacer->doBunchReplace($productUrlRewriteResult); + + $this->dataMapPoolInterface->resetDataMap(DataCategoryUrlRewriteMap::class, $category->getEntityId()); + $this->dataMapPoolInterface->resetDataMap(DataProductUrlRewriteMap::class, $category->getEntityId()); + } + } +} diff --git a/app/code/Magento/CatalogUrlRewrite/Observer/UrlRewriteHandler.php b/app/code/Magento/CatalogUrlRewrite/Observer/UrlRewriteHandler.php index ff914dc1adb20..6a49bb9e4c5e4 100644 --- a/app/code/Magento/CatalogUrlRewrite/Observer/UrlRewriteHandler.php +++ b/app/code/Magento/CatalogUrlRewrite/Observer/UrlRewriteHandler.php @@ -84,19 +84,35 @@ public function generateProductUrlRewrites(Category $category) foreach ($collection as $product) { $product->setStoreId($storeId); $product->setData('save_rewrites_history', $saveRewriteHistory); - $productUrls = array_merge($productUrls, $this->productUrlRewriteGenerator->generate($product)); + $urls = $this->productUrlRewriteGenerator->generate($product, $category->getEntityId()); + foreach ($urls as $url) { + $productUrls[$url->getRequestPath() . '_' . $url->getStoreId()] = $url; + } + unset($urls); } } else { - $productUrls = array_merge( - $productUrls, - $this->getCategoryProductsUrlRewrites($category, $storeId, $saveRewriteHistory) + $urls = $this->getCategoryProductsUrlRewrites( + $category, + $storeId, + $saveRewriteHistory, + $category->getEntityId() ); + foreach ($urls as $url) { + $productUrls[$url->getRequestPath() . '_' . $url->getStoreId()] = $url; + } + unset($urls); } foreach ($this->childrenCategoriesProvider->getChildren($category, true) as $childCategory) { - $productUrls = array_merge( - $productUrls, - $this->getCategoryProductsUrlRewrites($childCategory, $storeId, $saveRewriteHistory) + $urls = $this->getCategoryProductsUrlRewrites( + $childCategory, + $storeId, + $saveRewriteHistory, + $category->getEntityId() ); + foreach ($urls as $url) { + $productUrls[$url->getRequestPath() . '_' . $url->getStoreId()] = $url; + } + unset($urls); } return $productUrls; } @@ -105,10 +121,15 @@ public function generateProductUrlRewrites(Category $category) * @param Category $category * @param int $storeId * @param bool $saveRewriteHistory + * @param int|null $rootCategoryId * @return UrlRewrite[] */ - public function getCategoryProductsUrlRewrites(Category $category, $storeId, $saveRewriteHistory) - { + public function getCategoryProductsUrlRewrites( + Category $category, + $storeId, + $saveRewriteHistory, + $rootCategoryId = null + ) { /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection */ $productCollection = $category->getProductCollection() ->addAttributeToSelect('name') @@ -123,10 +144,11 @@ public function getCategoryProductsUrlRewrites(Category $category, $storeId, $sa $this->isSkippedProduct[] = $product->getId(); $product->setStoreId($storeId); $product->setData('save_rewrites_history', $saveRewriteHistory); - $productUrls = array_merge( - $productUrls, - $this->getCategoryBasedProductRewriteGenerator()->generate($product, $category) - ); + $urls = $this->getCategoryBasedProductRewriteGenerator()->generate($product, $category, $rootCategoryId); + foreach ($urls as $url) { + $productUrls[$url->getRequestPath() . '_' . $url->getStoreId()] = $url; + } + unset($urls); } return $productUrls; } diff --git a/app/code/Magento/CatalogUrlRewrite/etc/di.xml b/app/code/Magento/CatalogUrlRewrite/etc/di.xml index 9ea21bce36567..816db53a3f43e 100644 --- a/app/code/Magento/CatalogUrlRewrite/etc/di.xml +++ b/app/code/Magento/CatalogUrlRewrite/etc/di.xml @@ -6,6 +6,9 @@ */ --> + + + Magento\CatalogUrlRewrite\Model\Storage\DbStorage diff --git a/app/code/Magento/CatalogUrlRewrite/etc/events.xml b/app/code/Magento/CatalogUrlRewrite/etc/events.xml index 02c67bb500b66..b77bdc106c650 100644 --- a/app/code/Magento/CatalogUrlRewrite/etc/events.xml +++ b/app/code/Magento/CatalogUrlRewrite/etc/events.xml @@ -10,7 +10,7 @@ - + diff --git a/dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/Observer/CategoryProcessUrlRewriteSavingObserverTest.php b/dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/Observer/CategoryProcessUrlRewriteSavingObserverTest.php new file mode 100644 index 0000000000000..d5545e5bca453 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/Observer/CategoryProcessUrlRewriteSavingObserverTest.php @@ -0,0 +1,189 @@ +objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); + } + + /** + * @param array $filter + * @return array + */ + private function getActualResults(array $filter) + { + /** @var \Magento\UrlRewrite\Model\UrlFinderInterface $urlFinder */ + $urlFinder = $this->objectManager->get(\Magento\UrlRewrite\Model\UrlFinderInterface::class); + $actualResults = []; + foreach ($urlFinder->findAllByData($filter) as $url) { + $actualResults[] = [ + 'request_path' => $url->getRequestPath(), + 'target_path' => $url->getTargetPath(), + 'is_auto_generated' => (int)$url->getIsAutogenerated(), + 'redirect_type' => $url->getRedirectType() + ]; + } + return $actualResults; + } + + public function tearDown() + { + $category = $this->objectManager->create(\Magento\Catalog\Model\Category::class); + $category->load(3); + $category->delete(); + } + + /** + * @magentoDataFixture Magento/CatalogUrlRewrite/_files/categories.php + * @magentoDbIsolation enabled + * @magentoAppIsolation enabled + */ + public function testUrlKeyHasChanged() + { + $categoryFilter = [ + UrlRewrite::ENTITY_TYPE => 'category', + ]; + $expected = [ + [ + 'request_path' => "category-1.html", + 'target_path' => "catalog/category/view/id/3", + 'is_auto_generated' => 1, + 'redirect_type' => 0 + ], + [ + 'request_path' => "category-1/category-1-1.html", + 'target_path' => "catalog/category/view/id/4", + 'is_auto_generated' => 1, + 'redirect_type' => 0 + ], + [ + 'request_path' => "category-1/category-1-1/category-1-1-1.html", + 'target_path' => "catalog/category/view/id/5", + 'is_auto_generated' => 1, + 'redirect_type' => 0 + ], + [ + 'request_path' => "category-2.html", + 'target_path' => "catalog/category/view/id/6", + 'is_auto_generated' => 1, + 'redirect_type' => 0 + ] + + ]; + $actual = $this->getActualResults($categoryFilter); + foreach ($expected as $row) { + $this->assertContains($row, $actual); + } + /** @var \Magento\Catalog\Model\Category $category */ + $category = $this->objectManager->create(\Magento\Catalog\Model\Category::class); + $category->load(3); + $category->setData('save_rewrites_history', false); + $category->setUrlKey('new-url'); + $category->save(); + $expected = [ + [ + 'request_path' => "new-url.html", + 'target_path' => "catalog/category/view/id/3", + 'is_auto_generated' => 1, + 'redirect_type' => 0 + ], + [ + 'request_path' => "new-url/category-1-1.html", + 'target_path' => "catalog/category/view/id/4", + 'is_auto_generated' => 1, + 'redirect_type' => 0 + ], + [ + 'request_path' => "new-url/category-1-1/category-1-1-1.html", + 'target_path' => "catalog/category/view/id/5", + 'is_auto_generated' => 1, + 'redirect_type' => 0 + ], + [ + 'request_path' => "category-2.html", + 'target_path' => "catalog/category/view/id/6", + 'is_auto_generated' => 1, + 'redirect_type' => 0 + ] + + ]; + $actual = $this->getActualResults($categoryFilter); + foreach ($expected as $row) { + $this->assertContains($row, $actual); + } + } + + /** + * @magentoDataFixture Magento/CatalogUrlRewrite/_files/categories_with_products.php + * @magentoDbIsolation enabled + * @magentoAppIsolation enabled + */ + public function testIsAnchorHasChanged() + { + $categoryFilter = [ + UrlRewrite::ENTITY_TYPE => CategoryUrlRewriteGenerator::ENTITY_TYPE, + ]; + /** @var \Magento\Catalog\Model\Category $category */ + $category = $this->objectManager->create(\Magento\Catalog\Model\Category::class); + $category->load(3); + var_dump($category->getData('is_anchor')); + $category->setData('is_anchor', false); + $category->save(); + $expected = [ + [ + 'request_path' => "category-1.html", + 'target_path' => "catalog/category/view/id/3", + 'is_auto_generated' => 1, + 'redirect_type' => 0 + ], + [ + 'request_path' => "category-1/category-1-1.html", + 'target_path' => "catalog/category/view/id/4", + 'is_auto_generated' => 1, + 'redirect_type' => 0 + ], + [ + 'request_path' => "category-1/category-1-1/category-1-1-1.html", + 'target_path' => "catalog/category/view/id/5", + 'is_auto_generated' => 1, + 'redirect_type' => 0 + ], + [ + 'request_path' => "category-2.html", + 'target_path' => "catalog/category/view/id/6", + 'is_auto_generated' => 1, + 'redirect_type' => 0 + ] + + ]; + $actual = $this->getActualResults($categoryFilter); + foreach ($expected as $row) { + $this->assertContains($row, $actual); + } + } +} diff --git a/lib/internal/Magento/Framework/DB/TemporaryTableService.php b/lib/internal/Magento/Framework/DB/TemporaryTableService.php new file mode 100644 index 0000000000000..b946ae9d17bfe --- /dev/null +++ b/lib/internal/Magento/Framework/DB/TemporaryTableService.php @@ -0,0 +1,87 @@ + $columns) { + $renderedColumns = implode(',', array_map([$adapter, 'quoteIdentifier'], $columns)); + + $indexType = sprintf('INDEX %s USING HASH', $adapter->quoteIdentifier($indexName)); + + if ($indexName === 'PRIMARY') { + $indexType = 'PRIMARY KEY'; + } elseif (strpos($indexName, 'UNQ_') === 0) { + $indexType = sprintf('UNIQUE %s', $adapter->quoteIdentifier($indexName)); + } + + $indexStatements[] = sprintf('%s(%s)', $indexType, $renderedColumns); + } + + $statement = sprintf( + 'CREATE TEMPORARY TABLE %s %s ENGINE=%s IGNORE (%s)', + $adapter->quoteIdentifier($name), + $indexStatements ? '(' . implode(',', $indexStatements) . ')' : '', + $engine, + (string)$select + ); + + $adapter->query( + $statement, + $select->getBind() + ); + + $this->createdTables[$name] = $adapter; + + return $name; + } + + /** + * Method used to drop a table by name + * + * @param string $name + * @return bool + */ + public function dropTable($name) + { + if (!empty($this->createdTables)) { + if (isset($this->createdTables[$name]) && !empty($name)) { + $adapter = $this->createdTables[$name]; + $adapter->dropTemporaryTable($name); + unset($this->createdTables[$name]); + return true; + } + } + return false; + } +} From a593f29bf065a39cb5aaad4dc53fcab9ca891b9d Mon Sep 17 00:00:00 2001 From: Cristian Partica Date: Thu, 17 Nov 2016 18:19:02 -0600 Subject: [PATCH 02/39] MAGETWO-58924: SQL error wait timeout error when saving categories - fix code style - refactor usage of models in code replacing with repository --- .../Model/Category/Plugin/Storage.php | 16 ++-- .../Model/Map/CategoryUrlRewriteMap.php | 5 +- .../Model/Map/DataCategoryMap.php | 50 +++++++------ .../Model/Map/DataCategoryUrlRewriteMap.php | 17 +---- .../Map/DataCategoryUsedInProductsMap.php | 13 +--- .../Model/Map/DataMapInterface.php | 3 +- .../Model/Map/DataMapPool.php | 13 +--- .../Model/Map/DataMapPoolInterface.php | 2 +- .../Model/Map/DataProductMap.php | 11 +-- .../Model/Map/DataProductUrlRewriteMap.php | 19 +---- .../Model/Map/MapFactory.php | 6 +- .../CatalogUrlRewrite/Model/Map/MapPool.php | 6 +- .../Model/Map/ProductUrlRewriteMap.php | 5 +- .../Model/ResourceModel/Category/Product.php | 8 +- ...rocessUrlRewriteInMemorySavingObserver.php | 75 ------------------- ...ategoryProcessUrlRewriteSavingObserver.php | 59 ++++++--------- .../Magento/CatalogUrlRewrite/etc/events.xml | 2 +- ...oryProcessUrlRewriteSavingObserverTest.php | 1 - 18 files changed, 83 insertions(+), 228 deletions(-) delete mode 100644 app/code/Magento/CatalogUrlRewrite/Observer/CategoryProcessUrlRewriteInMemorySavingObserver.php diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Storage.php b/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Storage.php index b6c3be4f2489c..cb7a39e4ff16b 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Storage.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Storage.php @@ -5,30 +5,30 @@ */ namespace Magento\CatalogUrlRewrite\Model\Category\Plugin; -use Magento\CatalogUrlRewrite\Model\Category\ProductFactory; use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator; use Magento\UrlRewrite\Model\StorageInterface; use Magento\UrlRewrite\Model\UrlFinderInterface; use Magento\UrlRewrite\Service\V1\Data\UrlRewrite; +use Magento\CatalogUrlRewrite\Model\ResourceModel\Category\Product; class Storage { /** @var UrlFinderInterface */ protected $urlFinder; - /** @var ProductFactory */ - protected $productFactory; + /** @var Product */ + protected $productResource; /** * @param UrlFinderInterface $urlFinder - * @param ProductFactory $productFactory + * @param Product $productResource */ public function __construct( UrlFinderInterface $urlFinder, - ProductFactory $productFactory + Product $productResource ) { $this->urlFinder = $urlFinder; - $this->productFactory = $productFactory; + $this->productResource = $productResource; } /** @@ -50,7 +50,7 @@ public function afterReplace(StorageInterface $object, $result, array $urls) ]; } if ($toSave) { - $this->productFactory->create()->getResource()->saveMultiple($toSave); + $this->productResource->saveMultiple($toSave); } } @@ -62,7 +62,7 @@ public function afterReplace(StorageInterface $object, $result, array $urls) */ public function beforeDeleteByData(StorageInterface $object, array $data) { - $this->productFactory->create()->getResource()->removeMultipleByEntityStore($data); + $this->productResource->removeMultipleByFiler($data); } /** diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/CategoryUrlRewriteMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/CategoryUrlRewriteMap.php index 52f4bf32ff410..1742b673d3a6d 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/CategoryUrlRewriteMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/CategoryUrlRewriteMap.php @@ -55,10 +55,7 @@ public function __construct( } /** - * Gets the results from a map by identifiers - * - * @param array $identifiers - * @return UrlRewrite[] + * {@inheritdoc} */ public function getByIdentifiers($identifiers) { diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryMap.php index 77f675c4b38c2..3b1fe0281d0ff 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryMap.php @@ -5,8 +5,10 @@ */ namespace Magento\CatalogUrlRewrite\Model\Map; -use Magento\Catalog\Model\CategoryFactory; -use Magento\Catalog\Model\Category; +use Magento\Catalog\Model\ResourceModel\Category\Collection; +use Magento\Catalog\Model\ResourceModel\Category; +use Magento\Catalog\Model\CategoryRepository; +use Magento\Catalog\Api\Data\CategoryInterface; /** * Map that holds data for category ids and it's subcategories ids @@ -16,23 +18,32 @@ class DataCategoryMap implements DataMapInterface /** @var array */ private $data = []; - /** @var CategoryFactory */ - private $categoryFactory; + /** @var CategoryRepository */ + private $categoryRepository; + + /** @var Collection */ + private $collection; + + /** @var Category */ + private $categoryResource; /** - * @param CategoryFactory $categoryFactory + * @param CategoryRepository $categoryRepository + * @param Collection $collection + * @param Category $categoryResource */ public function __construct( - CategoryFactory $categoryFactory + CategoryRepository $categoryRepository, + Collection $collection, + Category $categoryResource ) { - $this->categoryFactory = $categoryFactory; + $this->categoryRepository = $categoryRepository; + $this->collection = $collection; + $this->categoryResource = $categoryResource; } /** - * Gets all data from a map identified by a category Id - * - * @param int $categoryId - * @return array + * {@inheritdoc} */ public function getData($categoryId) { @@ -50,38 +61,33 @@ public function getData($categoryId) */ private function queryData($categoryId) { - $category = $this->categoryFactory->create()->load($categoryId); - return $category->getResourceCollection() - ->addIdFilter($this->getAllCategoryChildrenIds($category)) + $category = $this->categoryRepository->get($categoryId); + return $this->collection->addIdFilter($this->getAllCategoryChildrenIds($category)) ->getAllIds(); } /** * Queries sub-categories ids from a category * - * @param Category $category + * @param CategoryInterface $category * @return int[] */ private function getAllCategoryChildrenIds($category) { - $connection = $category->getResource()->getConnection(); + $connection = $this->categoryResource->getConnection(); $select = $connection->select() - ->from($category->getResource()->getEntityTable(), 'entity_id') + ->from($this->categoryResource->getEntityTable(), 'entity_id') ->where($connection->quoteIdentifier('path') . ' LIKE :c_path'); $bind = ['c_path' => $category->getPath() . '%']; return $connection->fetchCol($select, $bind); } /** - * Resets current map - * - * @param int $categoryId - * @return $this + * {@inheritdoc} */ public function resetData($categoryId) { unset($this->data); $this->data = []; - return $this; } } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUrlRewriteMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUrlRewriteMap.php index daed66df6c478..864c307dabda6 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUrlRewriteMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUrlRewriteMap.php @@ -29,9 +29,6 @@ class DataCategoryUrlRewriteMap implements DataMapInterface /** @var ResourceConnection */ private $connection; - /** @var UrlRewriteFactory */ - private $urlRewriteFactory; - /** @var TemporaryTableService */ private $temporaryTableService; @@ -49,16 +46,12 @@ public function __construct( ) { $this->connection = $connection; $this->dataMapPool = $dataMapPool; - $this->urlRewriteFactory = $urlRewriteFactory; $this->temporaryTableService = $temporaryTableService; - $this->urlRewritePlaceholder = $this->urlRewriteFactory->create(); + $this->urlRewritePlaceholder = $urlRewriteFactory->create(); } /** - * Gets all data from a map identified by a category Id - * - * @param int $categoryId - * @return array + * {@inheritdoc} */ public function getData($categoryId) { @@ -109,10 +102,7 @@ private function queryData($categoryId) } /** - * Resets current map and it's dependencies - * - * @param int $categoryId - * @return $this + * {@inheritdoc} */ public function resetData($categoryId) { @@ -123,6 +113,5 @@ public function resetData($categoryId) } unset($this->data); $this->data = []; - return $this; } } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUsedInProductsMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUsedInProductsMap.php index e1e1308c6a8bf..06fec4bd180dd 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUsedInProductsMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUsedInProductsMap.php @@ -23,7 +23,7 @@ class DataCategoryUsedInProductsMap implements DataMapInterface /** * @param ResourceConnection $connection - * @param DataMapPoolInterface $dataMapPool, + * @param DataMapPoolInterface $dataMapPool */ public function __construct( ResourceConnection $connection, @@ -34,10 +34,7 @@ public function __construct( } /** - * Gets all data from a map identified by a category Id - * - * @param int $categoryId - * @return array + * {@inheritdoc} */ public function getData($categoryId) { @@ -76,10 +73,7 @@ private function queryData($categoryId) } /** - * Resets current map and it's dependencies - * - * @param int $categoryId - * @return $this + * {@inheritdoc} */ public function resetData($categoryId) { @@ -87,6 +81,5 @@ public function resetData($categoryId) $this->dataMapPool->resetDataMap(DataCategoryMap::class, $categoryId); unset($this->data); $this->data = []; - return $this; } } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataMapInterface.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataMapInterface.php index 72f5d2f60ba4e..35a966a86b4ed 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataMapInterface.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataMapInterface.php @@ -7,7 +7,6 @@ /** * Interface for a data map - * */ interface DataMapInterface { @@ -23,7 +22,7 @@ public function getData($categoryId); * Resets current map and it's dependencies * * @param int $categoryId - * @return $this + * @return void */ public function resetData($categoryId); } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataMapPool.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataMapPool.php index 2baf9d0836cd1..b35b65982e4eb 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataMapPool.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataMapPool.php @@ -34,11 +34,7 @@ public function __construct( } /** - * Gets a map by instance and category Id - * - * @param string $instanceName - * @param int $categoryId - * @return DataMapInterface + * {@inheritdoc} */ public function getDataMap($instanceName, $categoryId) { @@ -55,11 +51,7 @@ public function getDataMap($instanceName, $categoryId) } /** - * Resets a map by instance and category Id - * - * @param string $instanceName - * @param int $categoryId - * @return $this + * {@inheritdoc} */ public function resetDataMap($instanceName, $categoryId) { @@ -68,6 +60,5 @@ public function resetDataMap($instanceName, $categoryId) $this->dataArray[$key]->resetData($categoryId); unset($this->dataArray[$key]); } - return $this; } } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataMapPoolInterface.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataMapPoolInterface.php index e44b5f57d0ac5..b3f972f153207 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataMapPoolInterface.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataMapPoolInterface.php @@ -24,7 +24,7 @@ public function getDataMap($instanceName, $categoryId); * * @param string $instanceName * @param int $categoryId - * @return $this + * @return void */ public function resetDataMap($instanceName, $categoryId); } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductMap.php index b17d6782e8b66..e77d78b7169fa 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductMap.php @@ -26,7 +26,6 @@ class DataProductMap implements DataMapInterface private $connection; /** - * Constructor * @param CollectionFactory $collectionFactory * @param DataMapPoolInterface $dataMapPool * @param ResourceConnection $connection @@ -42,10 +41,7 @@ public function __construct( } /** - * Gets all data from a map identified by a category Id - * - * @param int $categoryId - * @return array + * {@inheritdoc} */ public function getData($categoryId) { @@ -82,10 +78,7 @@ private function queryData($categoryId) } /** - * Resets current map and it's dependencies - * - * @param int $categoryId - * @return $this + * {@inheritdoc} */ public function resetData($categoryId) { diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductUrlRewriteMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductUrlRewriteMap.php index 36363220ff561..351cfeb548aa1 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductUrlRewriteMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductUrlRewriteMap.php @@ -20,18 +20,12 @@ class DataProductUrlRewriteMap implements DataMapInterface /** @var string[] */ private $data = []; - /** @var UrlRewrite */ - private $urlRewritePlaceholder; - /** @var DataMapPoolInterface */ private $dataMapPool; /** @var ResourceConnection */ private $connection; - /** @var UrlRewriteFactory */ - private $urlRewriteFactory; - /** @var TemporaryTableService */ private $temporaryTableService; @@ -49,16 +43,11 @@ public function __construct( ) { $this->connection = $connection; $this->dataMapPool = $dataMapPool; - $this->urlRewriteFactory = $urlRewriteFactory; $this->temporaryTableService = $temporaryTableService; - $this->urlRewritePlaceholder = $this->urlRewriteFactory->create(); } /** - * Gets all data from a map identified by a category Id - * - * @param int $categoryId - * @return array + * {@inheritdoc} */ public function getData($categoryId) { @@ -105,10 +94,7 @@ private function queryData($categoryId) } /** - * Resets current map and it's dependencies - * - * @param int $categoryId - * @return $this + * {@inheritdoc} */ public function resetData($categoryId) { @@ -118,6 +104,5 @@ public function resetData($categoryId) } unset($this->data); $this->data = []; - return $this; } } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/MapFactory.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/MapFactory.php index e4b3392e96fe5..b19bfe1605fb0 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/MapFactory.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/MapFactory.php @@ -29,11 +29,7 @@ public function __construct( } /** - * Creates a client map - * - * @param string $instanceName - * @param int $categoryId - * @return MapInterface + * {@inheritdoc} */ public function create($instanceName, $categoryId) { diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/MapPool.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/MapPool.php index 8158125e6bc97..09e6c228053d5 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/MapPool.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/MapPool.php @@ -32,11 +32,7 @@ public function __construct( } /** - * Gets map instance identified by category id - * - * @param string $instanceName - * @param int $categoryId - * @return MapInterface + * {@inheritdoc} */ public function getMap($instanceName, $categoryId) { diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/ProductUrlRewriteMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/ProductUrlRewriteMap.php index 6c82c159deb1b..614bc2a716501 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/ProductUrlRewriteMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/ProductUrlRewriteMap.php @@ -56,10 +56,7 @@ public function __construct( } /** - * Gets the results from a map by identifiers - * - * @param array $identifiers - * @return UrlRewrite[] + * {@inheritdoc} */ public function getByIdentifiers($identifiers) { diff --git a/app/code/Magento/CatalogUrlRewrite/Model/ResourceModel/Category/Product.php b/app/code/Magento/CatalogUrlRewrite/Model/ResourceModel/Category/Product.php index 0d4e1b0cc6200..923d98ca13d44 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/ResourceModel/Category/Product.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/ResourceModel/Category/Product.php @@ -72,14 +72,14 @@ public function removeMultiple(array $removeData) /** * Removes data by entities from url_rewrite table using a select * - * @param array $removeData + * @param array $filter * @return int */ - public function removeMultipleByEntityStore(array $removeData) + public function removeMultipleByFiler(array $filter) { return $this->getConnection()->delete( $this->getTable(self::TABLE_NAME), - ['url_rewrite_id in (?)' => $this->prepareSelect($removeData)] + ['url_rewrite_id in (?)' => $this->prepareSelect($filter)] ); } @@ -89,7 +89,7 @@ public function removeMultipleByEntityStore(array $removeData) * @param array $data * @return \Magento\Framework\DB\Select */ - protected function prepareSelect($data) + private function prepareSelect($data) { $select = $this->getConnection()->select(); $select->from($this->getTable(DbStorage::TABLE_NAME), 'url_rewrite_id'); diff --git a/app/code/Magento/CatalogUrlRewrite/Observer/CategoryProcessUrlRewriteInMemorySavingObserver.php b/app/code/Magento/CatalogUrlRewrite/Observer/CategoryProcessUrlRewriteInMemorySavingObserver.php deleted file mode 100644 index b3855c3fafa61..0000000000000 --- a/app/code/Magento/CatalogUrlRewrite/Observer/CategoryProcessUrlRewriteInMemorySavingObserver.php +++ /dev/null @@ -1,75 +0,0 @@ -categoryUrlRewriteGenerator = $categoryUrlRewriteGenerator; - $this->urlRewriteHandler = $urlRewriteHandler; - $this->urlRewriteBunchReplacer = $urlRewriteBunchReplacer; - $this->dataMapPoolInterface = $dataMapPoolInterface; - } - - /** - * Generate urls for UrlRewrite and save it in storage - * - * @param \Magento\Framework\Event\Observer $observer - * @return void - */ - public function execute(\Magento\Framework\Event\Observer $observer) - { - /** @var Category $category */ - $category = $observer->getEvent()->getData('category'); - if ($category->getParentId() == Category::TREE_ROOT_ID) { - return; - } - if ($category->dataHasChangedFor('url_key') - || $category->dataHasChangedFor('is_anchor') - || $category->getIsChangedProductList() - ) { - $categoryUrlRewriteResult = $this->categoryUrlRewriteGenerator->generate($category); - $this->urlRewriteBunchReplacer->doBunchReplace($categoryUrlRewriteResult); - - $productUrlRewriteResult = $this->urlRewriteHandler->generateProductUrlRewrites($category); - $this->urlRewriteBunchReplacer->doBunchReplace($productUrlRewriteResult); - - $this->dataMapPoolInterface->resetDataMap(DataCategoryUrlRewriteMap::class, $category->getEntityId()); - $this->dataMapPoolInterface->resetDataMap(DataProductUrlRewriteMap::class, $category->getEntityId()); - } - } -} diff --git a/app/code/Magento/CatalogUrlRewrite/Observer/CategoryProcessUrlRewriteSavingObserver.php b/app/code/Magento/CatalogUrlRewrite/Observer/CategoryProcessUrlRewriteSavingObserver.php index fe86bc363b49c..2820f90994432 100644 --- a/app/code/Magento/CatalogUrlRewrite/Observer/CategoryProcessUrlRewriteSavingObserver.php +++ b/app/code/Magento/CatalogUrlRewrite/Observer/CategoryProcessUrlRewriteSavingObserver.php @@ -8,54 +8,41 @@ use Magento\Catalog\Model\Category; use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator; use Magento\CatalogUrlRewrite\Model\UrlRewriteBunchReplacer; -use Magento\Framework\App\ObjectManager; -use Magento\UrlRewrite\Model\UrlPersistInterface; use Magento\Framework\Event\ObserverInterface; +use Magento\CatalogUrlRewrite\Model\Map\DataMapPoolInterface; +use Magento\CatalogUrlRewrite\Model\Map\DataProductUrlRewriteMap; +use Magento\CatalogUrlRewrite\Model\Map\DataCategoryUrlRewriteMap; class CategoryProcessUrlRewriteSavingObserver implements ObserverInterface { /** @var CategoryUrlRewriteGenerator */ - protected $categoryUrlRewriteGenerator; + private $categoryUrlRewriteGenerator; - /** @var UrlPersistInterface */ - protected $urlPersist; - - /** - * @var UrlRewriteBunchReplacer - */ + /** @var UrlRewriteBunchReplacer */ private $urlRewriteBunchReplacer; /** @var UrlRewriteHandler */ - protected $urlRewriteHandler; + private $urlRewriteHandler; + + /** @var DataMapPoolInterface */ + private $dataMapPoolInterface; /** * @param CategoryUrlRewriteGenerator $categoryUrlRewriteGenerator - * @param UrlPersistInterface $urlPersist * @param UrlRewriteHandler $urlRewriteHandler + * @param UrlRewriteBunchReplacer $urlRewriteBunchReplacer + * @param DataMapPoolInterface $dataMapPoolInterface */ public function __construct( CategoryUrlRewriteGenerator $categoryUrlRewriteGenerator, - UrlPersistInterface $urlPersist, - UrlRewriteHandler $urlRewriteHandler + UrlRewriteHandler $urlRewriteHandler, + UrlRewriteBunchReplacer $urlRewriteBunchReplacer, + DataMapPoolInterface $dataMapPoolInterface ) { $this->categoryUrlRewriteGenerator = $categoryUrlRewriteGenerator; - $this->urlPersist = $urlPersist; $this->urlRewriteHandler = $urlRewriteHandler; - } - - /** - * Retrieve Url Rewrite Replacer based on bunches - * - * @deprecated - * @return UrlRewriteBunchReplacer - */ - private function getUrlRewriteBunchReplacer() - { - if (!$this->urlRewriteBunchReplacer) { - $this->urlRewriteBunchReplacer = ObjectManager::getInstance()->get(UrlRewriteBunchReplacer::class); - } - - return $this->urlRewriteBunchReplacer; + $this->urlRewriteBunchReplacer = $urlRewriteBunchReplacer; + $this->dataMapPoolInterface = $dataMapPoolInterface; } /** @@ -67,7 +54,7 @@ private function getUrlRewriteBunchReplacer() public function execute(\Magento\Framework\Event\Observer $observer) { /** @var Category $category */ - $category = $observer->getEvent()->getCategory(); + $category = $observer->getEvent()->getData('category'); if ($category->getParentId() == Category::TREE_ROOT_ID) { return; } @@ -75,12 +62,14 @@ public function execute(\Magento\Framework\Event\Observer $observer) || $category->dataHasChangedFor('is_anchor') || $category->getIsChangedProductList() ) { - $urlRewrites = array_merge( - $this->categoryUrlRewriteGenerator->generate($category), - $this->urlRewriteHandler->generateProductUrlRewrites($category) - ); + $categoryUrlRewriteResult = $this->categoryUrlRewriteGenerator->generate($category); + $this->urlRewriteBunchReplacer->doBunchReplace($categoryUrlRewriteResult); + + $productUrlRewriteResult = $this->urlRewriteHandler->generateProductUrlRewrites($category); + $this->urlRewriteBunchReplacer->doBunchReplace($productUrlRewriteResult); - $this->getUrlRewriteBunchReplacer()->doBunchReplace($urlRewrites); + $this->dataMapPoolInterface->resetDataMap(DataCategoryUrlRewriteMap::class, $category->getEntityId()); + $this->dataMapPoolInterface->resetDataMap(DataProductUrlRewriteMap::class, $category->getEntityId()); } } } diff --git a/app/code/Magento/CatalogUrlRewrite/etc/events.xml b/app/code/Magento/CatalogUrlRewrite/etc/events.xml index b77bdc106c650..02c67bb500b66 100644 --- a/app/code/Magento/CatalogUrlRewrite/etc/events.xml +++ b/app/code/Magento/CatalogUrlRewrite/etc/events.xml @@ -10,7 +10,7 @@ - + diff --git a/dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/Observer/CategoryProcessUrlRewriteSavingObserverTest.php b/dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/Observer/CategoryProcessUrlRewriteSavingObserverTest.php index d5545e5bca453..764b5f3352576 100644 --- a/dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/Observer/CategoryProcessUrlRewriteSavingObserverTest.php +++ b/dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/Observer/CategoryProcessUrlRewriteSavingObserverTest.php @@ -151,7 +151,6 @@ public function testIsAnchorHasChanged() /** @var \Magento\Catalog\Model\Category $category */ $category = $this->objectManager->create(\Magento\Catalog\Model\Category::class); $category->load(3); - var_dump($category->getData('is_anchor')); $category->setData('is_anchor', false); $category->save(); $expected = [ From f59118a3ae9666a8da35b081fcd91f32042ea4b1 Mon Sep 17 00:00:00 2001 From: Cristian Partica Date: Thu, 17 Nov 2016 22:52:07 -0600 Subject: [PATCH 03/39] MAGETWO-58924: SQL error wait timeout error when saving categories - add url rewrites array merger --- .../Category/ChildrenUrlRewriteGenerator.php | 19 ++--- .../CurrentUrlRewritesRegenerator.php | 36 ++++++---- .../Model/CategoryUrlRewriteGenerator.php | 44 +++++------- .../Product/CurrentUrlRewritesRegenerator.php | 71 ++++++++++--------- .../Model/ProductScopeRewriteGenerator.php | 63 ++++++++-------- .../Observer/AfterImportDataObserver.php | 34 ++++----- .../Observer/UrlRewriteHandler.php | 63 ++++++++-------- .../Magento/UrlRewrite/Model/ArrayMerger.php | 52 ++++++++++++++ app/code/Magento/UrlRewrite/etc/di.xml | 1 + 9 files changed, 210 insertions(+), 173 deletions(-) create mode 100644 app/code/Magento/UrlRewrite/Model/ArrayMerger.php diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Category/ChildrenUrlRewriteGenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/Category/ChildrenUrlRewriteGenerator.php index 7e24a510b0817..de5a610464482 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Category/ChildrenUrlRewriteGenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Category/ChildrenUrlRewriteGenerator.php @@ -8,6 +8,8 @@ use Magento\Catalog\Model\Category; use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGeneratorFactory; use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator; +use Magento\UrlRewrite\Model\ArrayMerger; +use Magento\Framework\App\ObjectManager; class ChildrenUrlRewriteGenerator { @@ -17,16 +19,22 @@ class ChildrenUrlRewriteGenerator /** @var \Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGeneratorFactory */ protected $categoryUrlRewriteGeneratorFactory; + /** @var \Magento\UrlRewrite\Model\ArrayMerger */ + private $arrayMerger; + /** * @param \Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider $childrenCategoriesProvider * @param \Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGeneratorFactory $categoryUrlRewriteGeneratorFactory + * @param ArrayMerger|null $arrayMerger */ public function __construct( ChildrenCategoriesProvider $childrenCategoriesProvider, - CategoryUrlRewriteGeneratorFactory $categoryUrlRewriteGeneratorFactory + CategoryUrlRewriteGeneratorFactory $categoryUrlRewriteGeneratorFactory, + ArrayMerger $arrayMerger = null ) { $this->childrenCategoriesProvider = $childrenCategoriesProvider; $this->categoryUrlRewriteGeneratorFactory = $categoryUrlRewriteGeneratorFactory; + $this->arrayMerger = $arrayMerger ?: ObjectManager::getInstance()->get(ArrayMerger::class); } /** @@ -39,18 +47,13 @@ public function __construct( */ public function generate($storeId, Category $category, $rootCategoryId = null) { - $urls = []; foreach ($this->childrenCategoriesProvider->getChildren($category, true) as $childCategory) { $childCategory->setStoreId($storeId); $childCategory->setData('save_rewrites_history', $category->getData('save_rewrites_history')); /** @var CategoryUrlRewriteGenerator $categoryUrlRewriteGenerator */ $categoryUrlRewriteGenerator = $this->categoryUrlRewriteGeneratorFactory->create(); - $urlRewrites = $categoryUrlRewriteGenerator->generate($childCategory, false, $rootCategoryId); - foreach ($urlRewrites as $url) { - $urls[$url->getRequestPath() . '_' . $url->getStoreId()] = $url; - } - unset($urlRewrites); + $this->arrayMerger->addData($categoryUrlRewriteGenerator->generate($childCategory, false, $rootCategoryId)); } - return $urls; + return $this->arrayMerger->getResetData(); } } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php index 738cac8e4c578..f83958551f9b5 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php @@ -15,6 +15,7 @@ use Magento\CatalogUrlRewrite\Model\Map\CategoryUrlRewriteMap; use Magento\CatalogUrlRewrite\Model\Map\MapPoolInterface; use Magento\Framework\App\ObjectManager; +use Magento\UrlRewrite\Model\ArrayMerger; class CurrentUrlRewritesRegenerator { @@ -30,22 +31,28 @@ class CurrentUrlRewritesRegenerator /** @var MapPoolInterface */ private $mapPool; + /** @var \Magento\UrlRewrite\Model\ArrayMerger */ + private $arrayMerger; + /** * @param \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator * @param \Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory $urlRewriteFactory * @param UrlFinderInterface $urlFinder * @param MapPoolInterface|null $mapPool + * @param \Magento\UrlRewrite\Model\ArrayMerger|null $arrayMerger */ public function __construct( CategoryUrlPathGenerator $categoryUrlPathGenerator, UrlRewriteFactory $urlRewriteFactory, UrlFinderInterface $urlFinder, - MapPoolInterface $mapPool = null + MapPoolInterface $mapPool = null, + ArrayMerger $arrayMerger = null ) { $this->categoryUrlPathGenerator = $categoryUrlPathGenerator; $this->urlRewriteFactory = $urlRewriteFactory; $this->urlFinder = $urlFinder; $this->mapPool = $mapPool ?: ObjectManager::getInstance()->get(MapPoolInterface::class); + $this->arrayMerger = $arrayMerger ?: ObjectManager::getInstance()->get(ArrayMerger::class); } /** @@ -78,17 +85,14 @@ public function generate($storeId, Category $category, $rootCategoryId = null) ); } - $urlRewrites = []; foreach ($currentUrlRewrites as $rewrite) { - $urls = $rewrite->getIsAutogenerated() + $this->arrayMerger->addData( + $rewrite->getIsAutogenerated() ? $this->generateForAutogenerated($rewrite, $storeId, $category) - : $this->generateForCustom($rewrite, $storeId, $category); - foreach ($urls as $url) { - $urlRewrites[$url->getRequestPath() . '_' . $url->getStoreId()] = $url; - } - unset($urls); + : $this->generateForCustom($rewrite, $storeId, $category) + ); } - return $urlRewrites; + return $this->arrayMerger->getResetData(); } /** @@ -99,11 +103,10 @@ public function generate($storeId, Category $category, $rootCategoryId = null) */ protected function generateForAutogenerated($url, $storeId, Category $category) { - $urls = []; if ($category->getData('save_rewrites_history')) { $targetPath = $this->categoryUrlPathGenerator->getUrlPathWithSuffix($category, $storeId); if ($url->getRequestPath() !== $targetPath) { - $urls[$url->getRequestPath() . '_' . $storeId] = $this->urlRewriteFactory->create() + $generatedUrl = $this->urlRewriteFactory->create() ->setEntityType(CategoryUrlRewriteGenerator::ENTITY_TYPE) ->setEntityId($category->getEntityId()) ->setRequestPath($url->getRequestPath()) @@ -111,9 +114,11 @@ protected function generateForAutogenerated($url, $storeId, Category $category) ->setRedirectType(OptionProvider::PERMANENT) ->setStoreId($storeId) ->setIsAutogenerated(0); + $this->arrayMerger->addData([$generatedUrl]); + return $this->arrayMerger->getResetData(); } } - return $urls; + return []; } /** @@ -124,12 +129,11 @@ protected function generateForAutogenerated($url, $storeId, Category $category) */ protected function generateForCustom($url, $storeId, Category $category) { - $urls = []; $targetPath = !$url->getRedirectType() ? $url->getTargetPath() : $this->categoryUrlPathGenerator->getUrlPathWithSuffix($category, $storeId); if ($url->getRequestPath() !== $targetPath) { - $urls[$url->getRequestPath() . '_' . $storeId] = $this->urlRewriteFactory->create() + $generatedUrl = $this->urlRewriteFactory->create() ->setEntityType(CategoryUrlRewriteGenerator::ENTITY_TYPE) ->setEntityId($category->getEntityId()) ->setRequestPath($url->getRequestPath()) @@ -139,7 +143,9 @@ protected function generateForCustom($url, $storeId, Category $category) ->setDescription($url->getDescription()) ->setIsAutogenerated(0) ->setMetadata($url->getMetadata()); + $this->arrayMerger->addData([$generatedUrl]); + return $this->arrayMerger->getResetData(); } - return $urls; + return []; } } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGenerator.php index b0738f4616722..b11b9b8cd28a5 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGenerator.php @@ -12,6 +12,8 @@ use Magento\CatalogUrlRewrite\Service\V1\StoreViewService; use Magento\Store\Model\Store; use Magento\Catalog\Api\CategoryRepositoryInterface; +use Magento\UrlRewrite\Model\ArrayMerger; +use Magento\Framework\App\ObjectManager; class CategoryUrlRewriteGenerator { @@ -30,6 +32,9 @@ class CategoryUrlRewriteGenerator /** @var \Magento\CatalogUrlRewrite\Model\Category\ChildrenUrlRewriteGenerator */ protected $childrenUrlRewriteGenerator; + /** @var \Magento\UrlRewrite\Model\ArrayMerger */ + private $arrayMerger; + /** * @var bool */ @@ -41,19 +46,22 @@ class CategoryUrlRewriteGenerator * @param \Magento\CatalogUrlRewrite\Model\Category\ChildrenUrlRewriteGenerator $childrenUrlRewriteGenerator * @param \Magento\CatalogUrlRewrite\Service\V1\StoreViewService $storeViewService * @param \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository + * @param \Magento\UrlRewrite\Model\ArrayMerger|null $arrayMerger */ public function __construct( CanonicalUrlRewriteGenerator $canonicalUrlRewriteGenerator, CurrentUrlRewritesRegenerator $currentUrlRewritesRegenerator, ChildrenUrlRewriteGenerator $childrenUrlRewriteGenerator, StoreViewService $storeViewService, - CategoryRepositoryInterface $categoryRepository + CategoryRepositoryInterface $categoryRepository, + ArrayMerger $arrayMerger = null ) { $this->storeViewService = $storeViewService; $this->canonicalUrlRewriteGenerator = $canonicalUrlRewriteGenerator; $this->childrenUrlRewriteGenerator = $childrenUrlRewriteGenerator; $this->currentUrlRewritesRegenerator = $currentUrlRewritesRegenerator; $this->categoryRepository = $categoryRepository; + $this->arrayMerger = $arrayMerger ?: ObjectManager::getInstance()->get(ArrayMerger::class); } /** @@ -86,21 +94,16 @@ public function generate(Category $category, $overrideStoreUrls = false, $rootCa */ protected function generateForGlobalScope(Category $category, $overrideStoreUrls, $rootCategoryId = null) { - $urls = []; $categoryId = $category->getId(); foreach ($category->getStoreIds() as $storeId) { if (!$this->isGlobalScope($storeId) && $this->isOverrideUrlsForStore($storeId, $categoryId, $overrideStoreUrls) ) { $this->updateCategoryUrlForStore($category, $storeId); - $specificStoreArray = $this->generateForSpecificStoreView($category, $storeId, $rootCategoryId); - foreach ($specificStoreArray as $url) { - $urls[$url->getRequestPath() . '_' . $url->getStoreId()] = $url; - } - unset($specificStoreArray); + $this->arrayMerger->addData($this->generateForSpecificStoreView($category, $storeId, $rootCategoryId)); } } - return $urls; + return $this->arrayMerger->getResetData(); } /** @@ -152,24 +155,11 @@ protected function isGlobalScope($storeId) */ protected function generateForSpecificStoreView(Category $category, $storeId, $rootCategoryId = null) { - $urls = []; - $canonicalUrl = $this->canonicalUrlRewriteGenerator->generate($storeId, $category); - foreach ($canonicalUrl as $url) { - $urls[$url->getRequestPath() . '_' . $url->getStoreId()] = $url; - } - unset($canonicalUrl); - - $childrenUrl = $this->childrenUrlRewriteGenerator->generate($storeId, $category, $rootCategoryId); - foreach ($childrenUrl as $url) { - $urls[$url->getRequestPath() . '_' . $url->getStoreId()] = $url; - } - unset($childrenUrl); - - $currentUrl = $this->currentUrlRewritesRegenerator->generate($storeId, $category, $rootCategoryId); - foreach ($currentUrl as $url) { - $urls[$url->getRequestPath() . '_' . $url->getStoreId()] = $url; - } - unset($currentUrl); - return $urls; + $this->arrayMerger->addData($this->canonicalUrlRewriteGenerator->generate($storeId, $category)); + $this->arrayMerger->addData($this->childrenUrlRewriteGenerator->generate($storeId, $category, $rootCategoryId)); + $this->arrayMerger->addData( + $this->currentUrlRewritesRegenerator->generate($storeId, $category, $rootCategoryId) + ); + return $this->arrayMerger->getResetData(); } } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php index 38e35869531b8..3603968022c36 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php @@ -17,6 +17,7 @@ use Magento\CatalogUrlRewrite\Model\Map\ProductUrlRewriteMap; use Magento\CatalogUrlRewrite\Model\Map\MapPoolInterface; use Magento\Framework\App\ObjectManager; +use Magento\UrlRewrite\Model\ArrayMerger; /** * @SuppressWarnings(PHPMD.CouplingBetweenObjects) @@ -35,22 +36,28 @@ class CurrentUrlRewritesRegenerator /** @var MapPoolInterface */ private $mapPool; + /** @var \Magento\UrlRewrite\Model\ArrayMerger */ + private $arrayMerger; + /** * @param UrlFinderInterface $urlFinder * @param ProductUrlPathGenerator $productUrlPathGenerator * @param UrlRewriteFactory $urlRewriteFactory * @param MapPoolInterface|null $mapPool + * @param \Magento\UrlRewrite\Model\ArrayMerger|null $arrayMerger */ public function __construct( UrlFinderInterface $urlFinder, ProductUrlPathGenerator $productUrlPathGenerator, UrlRewriteFactory $urlRewriteFactory, - MapPoolInterface $mapPool = null + MapPoolInterface $mapPool = null, + ArrayMerger $arrayMerger = null ) { $this->urlFinder = $urlFinder; $this->productUrlPathGenerator = $productUrlPathGenerator; $this->urlRewriteFactory = $urlRewriteFactory; $this->mapPool = $mapPool ?: ObjectManager::getInstance()->get(MapPoolInterface::class); + $this->arrayMerger = $arrayMerger ?: ObjectManager::getInstance()->get(ArrayMerger::class); } /** @@ -83,22 +90,19 @@ public function generate($storeId, Product $product, ObjectRegistry $productCate ); } - $urlRewrites = []; foreach ($currentUrlRewrites as $currentUrlRewrite) { $category = $this->retrieveCategoryFromMetadata($currentUrlRewrite, $productCategories); if ($category === false) { continue; } - $urls = $currentUrlRewrite->getIsAutogenerated() + $this->arrayMerger->addData( + $currentUrlRewrite->getIsAutogenerated() ? $this->generateForAutogenerated($currentUrlRewrite, $storeId, $category, $product) - : $this->generateForCustom($currentUrlRewrite, $storeId, $category, $product); - foreach ($urls as $url) { - $urlRewrites[$url->getRequestPath() . '_' . $url->getStoreId()] = $url; - } - unset($urls); + : $this->generateForCustom($currentUrlRewrite, $storeId, $category, $product) + ); } - return $urlRewrites; + return $this->arrayMerger->getResetData(); } /** @@ -110,25 +114,24 @@ public function generate($storeId, Product $product, ObjectRegistry $productCate */ protected function generateForAutogenerated(UrlRewrite $url, $storeId, $category, Product $product) { - if (!$product->getData('save_rewrites_history')) { - return []; - } - $targetPath = $this->productUrlPathGenerator->getUrlPathWithSuffix($product, $storeId, $category); - if ($url->getRequestPath() === $targetPath) { - return []; + if ($product->getData('save_rewrites_history')) { + $targetPath = $this->productUrlPathGenerator->getUrlPathWithSuffix($product, $storeId, $category); + if ($url->getRequestPath() !== $targetPath) { + $generatedUrl = $this->urlRewriteFactory->create() + ->setEntityType(ProductUrlRewriteGenerator::ENTITY_TYPE) + ->setEntityId($product->getEntityId()) + ->setRequestPath($url->getRequestPath()) + ->setTargetPath($targetPath) + ->setRedirectType(OptionProvider::PERMANENT) + ->setStoreId($storeId) + ->setDescription($url->getDescription()) + ->setIsAutogenerated(0) + ->setMetadata($url->getMetadata()); + $this->arrayMerger->addData([$generatedUrl]); + return $this->arrayMerger->getResetData(); + } } - return [ - $this->urlRewriteFactory->create() - ->setEntityType(ProductUrlRewriteGenerator::ENTITY_TYPE) - ->setEntityId($product->getEntityId()) - ->setRequestPath($url->getRequestPath()) - ->setTargetPath($targetPath) - ->setRedirectType(OptionProvider::PERMANENT) - ->setStoreId($storeId) - ->setDescription($url->getDescription()) - ->setIsAutogenerated(0) - ->setMetadata($url->getMetadata()) - ]; + return []; } /** @@ -143,11 +146,8 @@ protected function generateForCustom(UrlRewrite $url, $storeId, $category, Produ $targetPath = $url->getRedirectType() ? $this->productUrlPathGenerator->getUrlPathWithSuffix($product, $storeId, $category) : $url->getTargetPath(); - if ($url->getRequestPath() === $targetPath) { - return []; - } - return [ - $this->urlRewriteFactory->create() + if ($url->getRequestPath() !== $targetPath) { + $generatedUrl = $this->urlRewriteFactory->create() ->setEntityType(ProductUrlRewriteGenerator::ENTITY_TYPE) ->setEntityId($product->getEntityId()) ->setRequestPath($url->getRequestPath()) @@ -156,8 +156,11 @@ protected function generateForCustom(UrlRewrite $url, $storeId, $category, Produ ->setStoreId($storeId) ->setDescription($url->getDescription()) ->setIsAutogenerated(0) - ->setMetadata($url->getMetadata()) - ]; + ->setMetadata($url->getMetadata()); + $this->arrayMerger->addData([$generatedUrl]); + return $this->arrayMerger->getResetData(); + } + return []; } /** diff --git a/app/code/Magento/CatalogUrlRewrite/Model/ProductScopeRewriteGenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/ProductScopeRewriteGenerator.php index 6c52bc9892685..69918400fc4fd 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/ProductScopeRewriteGenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/ProductScopeRewriteGenerator.php @@ -14,6 +14,8 @@ use Magento\CatalogUrlRewrite\Service\V1\StoreViewService; use Magento\Store\Model\Store; use Magento\Store\Model\StoreManagerInterface; +use Magento\UrlRewrite\Model\ArrayMerger; +use Magento\Framework\App\ObjectManager; /** * Class ProductScopeRewriteGenerator @@ -56,6 +58,9 @@ class ProductScopeRewriteGenerator */ private $canonicalUrlRewriteGenerator; + /** @var \Magento\UrlRewrite\Model\ArrayMerger */ + private $arrayMerger; + /** * @param StoreViewService $storeViewService * @param StoreManagerInterface $storeManager @@ -64,6 +69,7 @@ class ProductScopeRewriteGenerator * @param CategoriesUrlRewriteGenerator $categoriesUrlRewriteGenerator * @param CurrentUrlRewritesRegenerator $currentUrlRewritesRegenerator * @param AnchorUrlRewriteGenerator $anchorUrlRewriteGenerator + * @param ArrayMerger|null $arrayMerger */ public function __construct( StoreViewService $storeViewService, @@ -72,7 +78,8 @@ public function __construct( CanonicalUrlRewriteGenerator $canonicalUrlRewriteGenerator, CategoriesUrlRewriteGenerator $categoriesUrlRewriteGenerator, CurrentUrlRewritesRegenerator $currentUrlRewritesRegenerator, - AnchorUrlRewriteGenerator $anchorUrlRewriteGenerator + AnchorUrlRewriteGenerator $anchorUrlRewriteGenerator, + ArrayMerger $arrayMerger = null ) { $this->storeViewService = $storeViewService; $this->storeManager = $storeManager; @@ -81,6 +88,7 @@ public function __construct( $this->categoriesUrlRewriteGenerator = $categoriesUrlRewriteGenerator; $this->currentUrlRewritesRegenerator = $currentUrlRewritesRegenerator; $this->anchorUrlRewriteGenerator = $anchorUrlRewriteGenerator; + $this->arrayMerger = $arrayMerger ?: ObjectManager::getInstance()->get(ArrayMerger::class); } /** @@ -114,14 +122,13 @@ public function generateForGlobalScope($productCategories, Product $product, $ro $productId, Product::ENTITY )) { - $urls = array_merge( - $urls, + $this->arrayMerger->addData( $this->generateForSpecificStoreView($id, $productCategories, $product, $rootCategoryId) ); } } - return $urls; + return $this->arrayMerger->getResetData(); } /** @@ -142,40 +149,26 @@ public function generateForSpecificStoreView($storeId, $productCategories, Produ } } $productCategories = $this->objectRegistryFactory->create(['entities' => $categories]); - /** - * @var $urls \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] - */ - $urls =[]; - $canonicalUrl = $this->canonicalUrlRewriteGenerator->generate($storeId, $product); - foreach ($canonicalUrl as $url) { - $urls[$url->getRequestPath() . '_' . $url->getStoreId()] = $url; - } - unset($canonicalUrl); - $categories = $this->categoriesUrlRewriteGenerator->generate($storeId, $product, $productCategories); - foreach ($categories as $url) { - $urls[$url->getRequestPath() . '_' . $url->getStoreId()] = $url; - } - unset($categories); - - $currentUrl = $this->currentUrlRewritesRegenerator->generate( - $storeId, - $product, - $productCategories, - $rootCategoryId + $this->arrayMerger->addData( + $this->canonicalUrlRewriteGenerator->generate($storeId, $product) + ); + $this->arrayMerger->addData( + $this->categoriesUrlRewriteGenerator->generate($storeId, $product, $productCategories) + ); + $this->arrayMerger->addData( + $this->currentUrlRewritesRegenerator->generate( + $storeId, + $product, + $productCategories, + $rootCategoryId + ) + ); + $this->arrayMerger->addData( + $this->anchorUrlRewriteGenerator->generate($storeId, $product, $productCategories) ); - foreach ($currentUrl as $url) { - $urls[$url->getRequestPath() . '_' . $url->getStoreId()] = $url; - } - unset($currentUrl); - - $anchor = $this->anchorUrlRewriteGenerator->generate($storeId, $product, $productCategories); - foreach ($anchor as $url) { - $urls[$url->getRequestPath() . '_' . $url->getStoreId()] = $url; - } - unset($anchor); - return $urls; + return $this->arrayMerger->getResetData(); } /** diff --git a/app/code/Magento/CatalogUrlRewrite/Observer/AfterImportDataObserver.php b/app/code/Magento/CatalogUrlRewrite/Observer/AfterImportDataObserver.php index 6b44a3148bb2c..314288a148592 100644 --- a/app/code/Magento/CatalogUrlRewrite/Observer/AfterImportDataObserver.php +++ b/app/code/Magento/CatalogUrlRewrite/Observer/AfterImportDataObserver.php @@ -20,6 +20,8 @@ use Magento\UrlRewrite\Model\UrlFinderInterface; use Magento\Framework\Event\ObserverInterface; use Magento\Catalog\Model\Product\Visibility; +use Magento\Framework\App\ObjectManager; +use Magento\UrlRewrite\Model\ArrayMerger; /** * Class AfterImportDataObserver @@ -97,6 +99,9 @@ class AfterImportDataObserver implements ObserverInterface 'visibility', ]; + /** @var \Magento\UrlRewrite\Model\ArrayMerger */ + private $arrayMerger; + /** * @param \Magento\Catalog\Model\ProductFactory $catalogProductFactory * @param \Magento\CatalogUrlRewrite\Model\ObjectRegistryFactory $objectRegistryFactory @@ -106,6 +111,7 @@ class AfterImportDataObserver implements ObserverInterface * @param UrlPersistInterface $urlPersist * @param UrlRewriteFactory $urlRewriteFactory * @param UrlFinderInterface $urlFinder + * @param \Magento\UrlRewrite\Model\ArrayMerger|null $arrayMerger * @throws \InvalidArgumentException * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ @@ -117,7 +123,8 @@ public function __construct( \Magento\Store\Model\StoreManagerInterface $storeManager, UrlPersistInterface $urlPersist, UrlRewriteFactory $urlRewriteFactory, - UrlFinderInterface $urlFinder + UrlFinderInterface $urlFinder, + ArrayMerger $arrayMerger = null ) { $this->urlPersist = $urlPersist; $this->catalogProductFactory = $catalogProductFactory; @@ -127,6 +134,7 @@ public function __construct( $this->storeManager = $storeManager; $this->urlRewriteFactory = $urlRewriteFactory; $this->urlFinder = $urlFinder; + $this->arrayMerger = $arrayMerger ?: ObjectManager::getInstance()->get(ArrayMerger::class); } /** @@ -259,30 +267,14 @@ protected function populateGlobalProduct($product) */ protected function generateUrls() { - /** - * @var $urls \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] - */ - $result = []; - $urls = $this->canonicalUrlRewriteGenerate(); - foreach ($urls as $url) { - $result[$url->getRequestPath() . '_' . $url->getStoreId()] = $url; - } - unset($urls); - $urls = $this->categoriesUrlRewriteGenerate(); - foreach ($urls as $url) { - $result[$url->getRequestPath() . '_' . $url->getStoreId()] = $url; - } - unset($urls); - $urls = $this->currentUrlRewritesRegenerate(); - foreach ($urls as $url) { - $result[$url->getRequestPath() . '_' . $url->getStoreId()] = $url; - } - unset($urls); + $this->arrayMerger->addData($this->canonicalUrlRewriteGenerate()); + $this->arrayMerger->addData($this->categoriesUrlRewriteGenerate()); + $this->arrayMerger->addData($this->currentUrlRewritesRegenerate()); $this->productCategories = null; unset($this->products); $this->products = []; - return $result; + return $this->arrayMerger->getResetData(); } /** diff --git a/app/code/Magento/CatalogUrlRewrite/Observer/UrlRewriteHandler.php b/app/code/Magento/CatalogUrlRewrite/Observer/UrlRewriteHandler.php index 6a49bb9e4c5e4..4b61b225a3c42 100644 --- a/app/code/Magento/CatalogUrlRewrite/Observer/UrlRewriteHandler.php +++ b/app/code/Magento/CatalogUrlRewrite/Observer/UrlRewriteHandler.php @@ -13,6 +13,7 @@ use Magento\Framework\Event\Observer as EventObserver; use Magento\UrlRewrite\Model\UrlPersistInterface; use Magento\UrlRewrite\Service\V1\Data\UrlRewrite; +use Magento\UrlRewrite\Model\ArrayMerger; class UrlRewriteHandler { @@ -39,25 +40,31 @@ class UrlRewriteHandler */ private $categoryBasedProductRewriteGenerator; + /** @var \Magento\UrlRewrite\Model\ArrayMerger */ + private $arrayMerger; + /** * @param \Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider $childrenCategoriesProvider * @param CategoryUrlRewriteGenerator $categoryUrlRewriteGenerator * @param ProductUrlRewriteGenerator $productUrlRewriteGenerator * @param UrlPersistInterface $urlPersist * @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory + * @param \Magento\UrlRewrite\Model\ArrayMerger|null $arrayMerger */ public function __construct( \Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider $childrenCategoriesProvider, CategoryUrlRewriteGenerator $categoryUrlRewriteGenerator, ProductUrlRewriteGenerator $productUrlRewriteGenerator, UrlPersistInterface $urlPersist, - \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory + \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory, + ArrayMerger $arrayMerger = null ) { $this->childrenCategoriesProvider = $childrenCategoriesProvider; $this->categoryUrlRewriteGenerator = $categoryUrlRewriteGenerator; $this->productUrlRewriteGenerator = $productUrlRewriteGenerator; $this->urlPersist = $urlPersist; $this->productCollectionFactory = $productCollectionFactory; + $this->arrayMerger = $arrayMerger ?: ObjectManager::getInstance()->get(ArrayMerger::class); } /** @@ -71,7 +78,6 @@ public function generateProductUrlRewrites(Category $category) $this->isSkippedProduct = []; $saveRewriteHistory = $category->getData('save_rewrites_history'); $storeId = $category->getStoreId(); - $productUrls = []; if ($category->getAffectedProductIds()) { $this->isSkippedProduct = $category->getAffectedProductIds(); $collection = $this->productCollectionFactory->create() @@ -84,37 +90,31 @@ public function generateProductUrlRewrites(Category $category) foreach ($collection as $product) { $product->setStoreId($storeId); $product->setData('save_rewrites_history', $saveRewriteHistory); - $urls = $this->productUrlRewriteGenerator->generate($product, $category->getEntityId()); - foreach ($urls as $url) { - $productUrls[$url->getRequestPath() . '_' . $url->getStoreId()] = $url; - } - unset($urls); + $this->arrayMerger->addData( + $this->productUrlRewriteGenerator->generate($product, $category->getEntityId()) + ); } } else { - $urls = $this->getCategoryProductsUrlRewrites( - $category, - $storeId, - $saveRewriteHistory, - $category->getEntityId() + $this->arrayMerger->addData( + $this->getCategoryProductsUrlRewrites( + $category, + $storeId, + $saveRewriteHistory, + $category->getEntityId() + ) ); - foreach ($urls as $url) { - $productUrls[$url->getRequestPath() . '_' . $url->getStoreId()] = $url; - } - unset($urls); } foreach ($this->childrenCategoriesProvider->getChildren($category, true) as $childCategory) { - $urls = $this->getCategoryProductsUrlRewrites( - $childCategory, - $storeId, - $saveRewriteHistory, - $category->getEntityId() + $this->arrayMerger->addData( + $this->getCategoryProductsUrlRewrites( + $childCategory, + $storeId, + $saveRewriteHistory, + $category->getEntityId() + ) ); - foreach ($urls as $url) { - $productUrls[$url->getRequestPath() . '_' . $url->getStoreId()] = $url; - } - unset($urls); } - return $productUrls; + return $this->arrayMerger->getResetData(); } /** @@ -136,7 +136,6 @@ public function getCategoryProductsUrlRewrites( ->addAttributeToSelect('visibility') ->addAttributeToSelect('url_key') ->addAttributeToSelect('url_path'); - $productUrls = []; foreach ($productCollection as $product) { if (in_array($product->getId(), $this->isSkippedProduct)) { continue; @@ -144,13 +143,11 @@ public function getCategoryProductsUrlRewrites( $this->isSkippedProduct[] = $product->getId(); $product->setStoreId($storeId); $product->setData('save_rewrites_history', $saveRewriteHistory); - $urls = $this->getCategoryBasedProductRewriteGenerator()->generate($product, $category, $rootCategoryId); - foreach ($urls as $url) { - $productUrls[$url->getRequestPath() . '_' . $url->getStoreId()] = $url; - } - unset($urls); + $this->arrayMerger->addData( + $this->getCategoryBasedProductRewriteGenerator()->generate($product, $category, $rootCategoryId) + ); } - return $productUrls; + return $this->arrayMerger->getResetData(); } /** diff --git a/app/code/Magento/UrlRewrite/Model/ArrayMerger.php b/app/code/Magento/UrlRewrite/Model/ArrayMerger.php new file mode 100644 index 0000000000000..50ac9c0e06882 --- /dev/null +++ b/app/code/Magento/UrlRewrite/Model/ArrayMerger.php @@ -0,0 +1,52 @@ +getRequestPath() . $separator . $urlRewrite->getStoreId(); + if ($key !== $separator) { + $this->data[$urlRewrite->getRequestPath() . $separator . $urlRewrite->getStoreId()] = $urlRewrite; + } else { + $this->data[] = $urlRewrite; + } + } + } + + /** + * Retrieves data + * + * @return UrlRewrite[] + */ + public function getResetData() + { + $result = $this->data; + unset($this->data); + $this->data = []; + return $result; + } +} diff --git a/app/code/Magento/UrlRewrite/etc/di.xml b/app/code/Magento/UrlRewrite/etc/di.xml index b7485a7ef2123..8e426647f90c8 100644 --- a/app/code/Magento/UrlRewrite/etc/di.xml +++ b/app/code/Magento/UrlRewrite/etc/di.xml @@ -9,4 +9,5 @@ + From 01df98778e358477c692402b2813f15547a2321f Mon Sep 17 00:00:00 2001 From: Cristian Partica Date: Wed, 30 Nov 2016 14:17:54 -0600 Subject: [PATCH 04/39] MAGETWO-58924: SQL error wait timeout error when saving categories - unifying class for urlRewrites to remove code duplication --- .../CurrentUrlRewritesRegenerator.php | 9 +- .../Model/Map/ProductUrlRewriteMap.php | 136 ------------------ ...oryUrlRewriteMap.php => UrlRewriteMap.php} | 74 +++++----- .../Product/CurrentUrlRewritesRegenerator.php | 9 +- .../Model/ProductScopeRewriteGenerator.php | 1 - 5 files changed, 52 insertions(+), 177 deletions(-) delete mode 100644 app/code/Magento/CatalogUrlRewrite/Model/Map/ProductUrlRewriteMap.php rename app/code/Magento/CatalogUrlRewrite/Model/Map/{CategoryUrlRewriteMap.php => UrlRewriteMap.php} (53%) diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php index f83958551f9b5..63d5451c05e37 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php @@ -12,7 +12,7 @@ use Magento\UrlRewrite\Model\UrlFinderInterface; use Magento\UrlRewrite\Service\V1\Data\UrlRewrite; use Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory; -use Magento\CatalogUrlRewrite\Model\Map\CategoryUrlRewriteMap; +use Magento\CatalogUrlRewrite\Model\Map\UrlRewriteMap; use Magento\CatalogUrlRewrite\Model\Map\MapPoolInterface; use Magento\Framework\App\ObjectManager; use Magento\UrlRewrite\Model\ArrayMerger; @@ -66,13 +66,14 @@ public function __construct( public function generate($storeId, Category $category, $rootCategoryId = null) { if ($rootCategoryId) { - $categoryUrlRewriteMap = $this->mapPool->getMap(CategoryUrlRewriteMap::class, $rootCategoryId); + $categoryUrlRewriteMap = $this->mapPool->getMap(UrlRewriteMap::class, $rootCategoryId); /** @var UrlRewrite[] $currentUrlRewrites */ $currentUrlRewrites = $categoryUrlRewriteMap->getByIdentifiers( [ UrlRewrite::STORE_ID => $storeId, - UrlRewrite::ENTITY_ID => $category->getEntityId() + UrlRewrite::ENTITY_ID => $category->getEntityId(), + UrlRewrite::ENTITY_TYPE => UrlRewriteMap::ENTITY_TYPE_CATEGORY, ] ); } else { @@ -80,7 +81,7 @@ public function generate($storeId, Category $category, $rootCategoryId = null) [ UrlRewrite::STORE_ID => $storeId, UrlRewrite::ENTITY_ID => $category->getEntityId(), - UrlRewrite::ENTITY_TYPE => CategoryUrlRewriteMap::ENTITY_TYPE, + UrlRewrite::ENTITY_TYPE => UrlRewriteMap::ENTITY_TYPE_CATEGORY, ] ); } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/ProductUrlRewriteMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/ProductUrlRewriteMap.php deleted file mode 100644 index 614bc2a716501..0000000000000 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/ProductUrlRewriteMap.php +++ /dev/null @@ -1,136 +0,0 @@ -dataMapPool = $dataMapPool; - $this->urlFinder = $urlFinder; - $this->connection = $connection; - $this->urlRewritePlaceholder = $urlRewriteFactory->create(); - $this->categoryId = $categoryId; - } - - /** - * {@inheritdoc} - */ - public function getByIdentifiers($identifiers) - { - if ($this->categoryId) { - $array = $this->dataMapPool->getDataMap(DataProductUrlRewriteMap::class, $this->categoryId) - ->getData($this->categoryId); - $tableName = reset($array); - if (!empty($array)) { - if (isset($identifiers['store_id']) && isset($identifiers['entity_id'])) { - if (!is_array($identifiers['entity_id']) && !is_array($identifiers['store_id'])) { - $key = $identifiers['store_id'] . '_' . $identifiers['entity_id']; - $urlRewritesConnection = $this->connection->getConnection(); - $select = $urlRewritesConnection->select() - ->from(['e' => $tableName]) - ->where('hash_key = ?', $key); - return $this->arrayToUrlRewriteObject($urlRewritesConnection->fetchAll($select)); - } else if (is_array($identifiers['entity_id']) && is_array($identifiers['store_id'])) { - $urlRewritesConnection = $this->connection->getConnection(); - $select = $urlRewritesConnection->select() - ->from(['e' => $tableName]) - ->where( - $urlRewritesConnection->prepareSqlCondition( - 'store_id', - ['in' => $identifiers['store_id']] - ) - ) - ->where( - $urlRewritesConnection->prepareSqlCondition( - 'entity_id', - ['in' => $identifiers['entity_id']] - ) - ); - return $this->arrayToUrlRewriteObject($urlRewritesConnection->fetchAll($select)); - } - } - } - } - return $this->urlFinder->findAllByData($identifiers); - } - - /** - * Transform array values to url rewrite object values - * - * @param array $data - * @return UrlRewrite[] - */ - private function arrayToUrlRewriteObject($data) - { - foreach ($data as $key => $array) { - $data[$key] = $this->createUrlRewrite($array); - } - return $data; - } - - /** - * Clone url rewrite object - * - * @param array $data - * @return UrlRewrite - */ - private function createUrlRewrite($data) - { - $dataObject = clone $this->urlRewritePlaceholder; - $dataObject->setUrlRewriteId($data['url_rewrite_id']); - $dataObject->setEntityType($data['entity_type']); - $dataObject->setEntityId($data['entity_id']); - $dataObject->setRequestPath($data['request_path']); - $dataObject->setTargetPath($data['target_path']); - $dataObject->setRedirectType($data['redirect_type']); - $dataObject->setStoreId($data['store_id']); - $dataObject->setDescription($data['description']); - $dataObject->setIsAutogenerated($data['is_autogenerated']); - $dataObject->setMetadata($data['metadata']); - - return $dataObject; - } -} diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/CategoryUrlRewriteMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/UrlRewriteMap.php similarity index 53% rename from app/code/Magento/CatalogUrlRewrite/Model/Map/CategoryUrlRewriteMap.php rename to app/code/Magento/CatalogUrlRewrite/Model/Map/UrlRewriteMap.php index 1742b673d3a6d..7920b8d564f78 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/CategoryUrlRewriteMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/UrlRewriteMap.php @@ -12,11 +12,12 @@ use Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory; /** - * Allows query to DataCategoryUrlRewriteMap class by identifiers + * Allows query to DataCategoryUrlRewriteMap and DataProductUrlRewriteMapclass or UrlFinderInterface by identifiers */ -class CategoryUrlRewriteMap implements MapInterface +class UrlRewriteMap implements MapInterface { - const ENTITY_TYPE = 'category'; + const ENTITY_TYPE_CATEGORY = 'category'; + const ENTITY_TYPE_PRODUCT = 'product'; /** @var DataMapPoolInterface */ private $dataMapPool; @@ -59,37 +60,46 @@ public function __construct( */ public function getByIdentifiers($identifiers) { - if ($this->categoryId) { - $array = $this->dataMapPool->getDataMap(DataCategoryUrlRewriteMap::class, $this->categoryId) - ->getData($this->categoryId); - $tableName = reset($array); + if ($this->categoryId + && isset($identifiers['entity_type']) + && isset($identifiers['store_id']) + && isset($identifiers['entity_id']) + ) { + $array = []; + if ($identifiers['entity_type'] === self::ENTITY_TYPE_PRODUCT) { + $array = $this->dataMapPool->getDataMap(DataProductUrlRewriteMap::class, $this->categoryId) + ->getData($this->categoryId); + } elseif ($identifiers['entity_type'] === self::ENTITY_TYPE_CATEGORY) { + $array = $this->dataMapPool->getDataMap(DataCategoryUrlRewriteMap::class, $this->categoryId) + ->getData($this->categoryId); + } + if (!empty($array)) { - if (isset($identifiers['store_id']) && isset($identifiers['entity_id'])) { - if (!is_array($identifiers['entity_id']) && !is_array($identifiers['store_id'])) { - $key = $identifiers['store_id'] . '_' . $identifiers['entity_id']; - $urlRewritesConnection = $this->connection->getConnection(); - $select = $urlRewritesConnection->select() - ->from(['e' => $tableName]) - ->where('hash_key = ?', $key); - return $this->arrayToUrlRewriteObject($urlRewritesConnection->fetchAll($select)); - } else if (is_array($identifiers['entity_id']) && is_array($identifiers['store_id'])) { - $urlRewritesConnection = $this->connection->getConnection(); - $select = $urlRewritesConnection->select() - ->from(['e' => $tableName]) - ->where( - $urlRewritesConnection->prepareSqlCondition( - 'store_id', - ['in' => $identifiers['store_id']] - ) + $tableName = reset($array); + if (!is_array($identifiers['entity_id']) && !is_array($identifiers['store_id'])) { + $key = $identifiers['store_id'] . '_' . $identifiers['entity_id']; + $urlRewritesConnection = $this->connection->getConnection(); + $select = $urlRewritesConnection->select() + ->from(['e' => $tableName]) + ->where('hash_key = ?', $key); + return $this->arrayToUrlRewriteObject($urlRewritesConnection->fetchAll($select)); + } else if (is_array($identifiers['entity_id']) && is_array($identifiers['store_id'])) { + $urlRewritesConnection = $this->connection->getConnection(); + $select = $urlRewritesConnection->select() + ->from(['e' => $tableName]) + ->where( + $urlRewritesConnection->prepareSqlCondition( + 'store_id', + ['in' => $identifiers['store_id']] + ) + ) + ->where( + $urlRewritesConnection->prepareSqlCondition( + 'entity_id', + ['in' => $identifiers['entity_id']] ) - ->where( - $urlRewritesConnection->prepareSqlCondition( - 'entity_id', - ['in' => $identifiers['entity_id']] - ) - ); - return $this->arrayToUrlRewriteObject($urlRewritesConnection->fetchAll($select)); - } + ); + return $this->arrayToUrlRewriteObject($urlRewritesConnection->fetchAll($select)); } } } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php index 3603968022c36..68b2189f962c4 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php @@ -14,7 +14,7 @@ use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator; use Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator; use Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory; -use Magento\CatalogUrlRewrite\Model\Map\ProductUrlRewriteMap; +use Magento\CatalogUrlRewrite\Model\Map\UrlRewriteMap; use Magento\CatalogUrlRewrite\Model\Map\MapPoolInterface; use Magento\Framework\App\ObjectManager; use Magento\UrlRewrite\Model\ArrayMerger; @@ -72,12 +72,13 @@ public function __construct( public function generate($storeId, Product $product, ObjectRegistry $productCategories, $rootCategoryId = null) { if ($rootCategoryId) { - $productUrlRewriteMap = $this->mapPool->getMap(ProductUrlRewriteMap::class, $rootCategoryId); + $productUrlRewriteMap = $this->mapPool->getMap(UrlRewriteMap::class, $rootCategoryId); $currentUrlRewrites = $productUrlRewriteMap->getByIdentifiers( [ UrlRewrite::STORE_ID => $storeId, - UrlRewrite::ENTITY_ID => $product->getEntityId() + UrlRewrite::ENTITY_ID => $product->getEntityId(), + UrlRewrite::ENTITY_TYPE => UrlRewriteMap::ENTITY_TYPE_PRODUCT, ] ); } else { @@ -85,7 +86,7 @@ public function generate($storeId, Product $product, ObjectRegistry $productCate [ UrlRewrite::STORE_ID => $storeId, UrlRewrite::ENTITY_ID => $product->getEntityId(), - UrlRewrite::ENTITY_TYPE => ProductUrlRewriteMap::ENTITY_TYPE, + UrlRewrite::ENTITY_TYPE => UrlRewriteMap::ENTITY_TYPE_PRODUCT, ] ); } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/ProductScopeRewriteGenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/ProductScopeRewriteGenerator.php index 69918400fc4fd..5a48297975203 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/ProductScopeRewriteGenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/ProductScopeRewriteGenerator.php @@ -112,7 +112,6 @@ public function isGlobalScope($storeId) */ public function generateForGlobalScope($productCategories, Product $product, $rootCategoryId = null) { - $urls = []; $productId = $product->getEntityId(); foreach ($product->getStoreIds() as $id) { From c36977433b66000dd0bcfd74cbd6bdffa94aec3f Mon Sep 17 00:00:00 2001 From: Cristian Partica Date: Wed, 30 Nov 2016 15:09:25 -0600 Subject: [PATCH 05/39] MAGETWO-58924: SQL error wait timeout error when saving categories - adding documentation to temporary table service --- .../Model/Map/DataCategoryUrlRewriteMap.php | 2 +- .../Model/Map/DataProductUrlRewriteMap.php | 2 +- .../Framework/DB/TemporaryTableService.php | 32 ++++++++++++++++--- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUrlRewriteMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUrlRewriteMap.php index 864c307dabda6..adf98daab89be 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUrlRewriteMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUrlRewriteMap.php @@ -89,7 +89,7 @@ private function queryData($categoryId) ] ) ); - $mapName = $this->temporaryTableService->createTemporaryTable( + $mapName = $this->temporaryTableService->createFromSelect( $select, $this->connection->getConnection(), [ diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductUrlRewriteMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductUrlRewriteMap.php index 351cfeb548aa1..2f502f47bec76 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductUrlRewriteMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductUrlRewriteMap.php @@ -81,7 +81,7 @@ private function queryData($categoryId) ] ) ); - $mapName = $this->temporaryTableService->createTemporaryTable( + $mapName = $this->temporaryTableService->createFromSelect( $select, $this->connection->getConnection(), [ diff --git a/lib/internal/Magento/Framework/DB/TemporaryTableService.php b/lib/internal/Magento/Framework/DB/TemporaryTableService.php index b946ae9d17bfe..b88ef678b26d1 100644 --- a/lib/internal/Magento/Framework/DB/TemporaryTableService.php +++ b/lib/internal/Magento/Framework/DB/TemporaryTableService.php @@ -8,6 +8,12 @@ use Magento\Framework\DB\Adapter\AdapterInterface; use Magento\Framework\DB\Select; +/** + * Class TemporaryTableService creates a temporary table in mysql from a Magento\Framework\DB\Select. + * Use this class to create an index with that that you want to query later for quick data access + * + * @api + */ class TemporaryTableService { /** @@ -16,19 +22,33 @@ class TemporaryTableService private $createdTables = []; /** - * Creates a temporary table from select + * Creates a temporary table from select removing duplicate rows if you have a union in your select + * This method should always be paired with dropTable to ensure cleanup + * Make sure you index your data so you can query it fast + * You can choose from memory or file table and provide indexes to ensure fast data query + * + * Example: createFromSelect( + * $selectObject, + * $this->resourceConnection->getConnection(), + * [ + * 'PRIMARY' => ['primary_id'], + * 'some_single_field_index' => ['field'], + * 'some_multiple_field_index' => ['field1', 'field2'], + * ] + * ) * * @param Select $select * @param AdapterInterface $adapter * @param array $indexes + * @param string $indexMethod * @param string $engine - * * @return string */ - public function createTemporaryTable( + public function createFromSelect( Select $select, AdapterInterface $adapter, array $indexes = [], + $indexMethod = 'HASH', $engine = 'INNODB' ) { $name = uniqid('tmp_select_' . crc32((string)$select)); @@ -37,7 +57,7 @@ public function createTemporaryTable( foreach ($indexes as $indexName => $columns) { $renderedColumns = implode(',', array_map([$adapter, 'quoteIdentifier'], $columns)); - $indexType = sprintf('INDEX %s USING HASH', $adapter->quoteIdentifier($indexName)); + $indexType = sprintf('INDEX %s USING %s', $adapter->quoteIdentifier($indexName), $indexMethod); if ($indexName === 'PRIMARY') { $indexType = 'PRIMARY KEY'; @@ -68,6 +88,10 @@ public function createTemporaryTable( /** * Method used to drop a table by name + * This class will hold all temporary table names in createdTables array so we can dispose them once we're finished + * + * Example: dropTable($previouslySavedTableName) + * where $previouslySavedTableName is a variable that you have to save when you use "createFromSelect" method * * @param string $name * @return bool From 40d0548b61313ade420c18157cd6f8ea7cd32d37 Mon Sep 17 00:00:00 2001 From: Cristian Partica Date: Thu, 1 Dec 2016 09:37:31 -0600 Subject: [PATCH 06/39] MAGETWO-58924: SQL error wait timeout error when saving categories - improving array merge & code optimization --- .../CurrentUrlRewritesRegenerator.php | 28 +++++++----------- .../Product/CurrentUrlRewritesRegenerator.php | 29 +++++++------------ .../Magento/UrlRewrite/Model/ArrayMerger.php | 3 +- 3 files changed, 21 insertions(+), 39 deletions(-) diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php index 63d5451c05e37..c9f8fd63d93a8 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php @@ -65,25 +65,19 @@ public function __construct( */ public function generate($storeId, Category $category, $rootCategoryId = null) { + $queryArray = [ + UrlRewrite::STORE_ID => $storeId, + UrlRewrite::ENTITY_ID => $category->getEntityId(), + UrlRewrite::ENTITY_TYPE => UrlRewriteMap::ENTITY_TYPE_CATEGORY + ]; + if ($rootCategoryId) { $categoryUrlRewriteMap = $this->mapPool->getMap(UrlRewriteMap::class, $rootCategoryId); /** @var UrlRewrite[] $currentUrlRewrites */ - $currentUrlRewrites = $categoryUrlRewriteMap->getByIdentifiers( - [ - UrlRewrite::STORE_ID => $storeId, - UrlRewrite::ENTITY_ID => $category->getEntityId(), - UrlRewrite::ENTITY_TYPE => UrlRewriteMap::ENTITY_TYPE_CATEGORY, - ] - ); + $currentUrlRewrites = $categoryUrlRewriteMap->getByIdentifiers($queryArray); } else { - $currentUrlRewrites = $this->urlFinder->findAllByData( - [ - UrlRewrite::STORE_ID => $storeId, - UrlRewrite::ENTITY_ID => $category->getEntityId(), - UrlRewrite::ENTITY_TYPE => UrlRewriteMap::ENTITY_TYPE_CATEGORY, - ] - ); + $currentUrlRewrites = $this->urlFinder->findAllByData($queryArray); } foreach ($currentUrlRewrites as $rewrite) { @@ -115,8 +109,7 @@ protected function generateForAutogenerated($url, $storeId, Category $category) ->setRedirectType(OptionProvider::PERMANENT) ->setStoreId($storeId) ->setIsAutogenerated(0); - $this->arrayMerger->addData([$generatedUrl]); - return $this->arrayMerger->getResetData(); + return [$generatedUrl]; } } return []; @@ -144,8 +137,7 @@ protected function generateForCustom($url, $storeId, Category $category) ->setDescription($url->getDescription()) ->setIsAutogenerated(0) ->setMetadata($url->getMetadata()); - $this->arrayMerger->addData([$generatedUrl]); - return $this->arrayMerger->getResetData(); + return [$generatedUrl]; } return []; } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php index 68b2189f962c4..eb1dc5b253d3f 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php @@ -71,24 +71,17 @@ public function __construct( */ public function generate($storeId, Product $product, ObjectRegistry $productCategories, $rootCategoryId = null) { + $queryArray = [ + UrlRewrite::STORE_ID => $storeId, + UrlRewrite::ENTITY_ID => $product->getEntityId(), + UrlRewrite::ENTITY_TYPE => UrlRewriteMap::ENTITY_TYPE_PRODUCT + ]; + if ($rootCategoryId) { $productUrlRewriteMap = $this->mapPool->getMap(UrlRewriteMap::class, $rootCategoryId); - - $currentUrlRewrites = $productUrlRewriteMap->getByIdentifiers( - [ - UrlRewrite::STORE_ID => $storeId, - UrlRewrite::ENTITY_ID => $product->getEntityId(), - UrlRewrite::ENTITY_TYPE => UrlRewriteMap::ENTITY_TYPE_PRODUCT, - ] - ); + $currentUrlRewrites = $productUrlRewriteMap->getByIdentifiers($queryArray); } else { - $currentUrlRewrites = $this->urlFinder->findAllByData( - [ - UrlRewrite::STORE_ID => $storeId, - UrlRewrite::ENTITY_ID => $product->getEntityId(), - UrlRewrite::ENTITY_TYPE => UrlRewriteMap::ENTITY_TYPE_PRODUCT, - ] - ); + $currentUrlRewrites = $this->urlFinder->findAllByData($queryArray); } foreach ($currentUrlRewrites as $currentUrlRewrite) { @@ -128,8 +121,7 @@ protected function generateForAutogenerated(UrlRewrite $url, $storeId, $category ->setDescription($url->getDescription()) ->setIsAutogenerated(0) ->setMetadata($url->getMetadata()); - $this->arrayMerger->addData([$generatedUrl]); - return $this->arrayMerger->getResetData(); + return [$generatedUrl]; } } return []; @@ -158,8 +150,7 @@ protected function generateForCustom(UrlRewrite $url, $storeId, $category, Produ ->setDescription($url->getDescription()) ->setIsAutogenerated(0) ->setMetadata($url->getMetadata()); - $this->arrayMerger->addData([$generatedUrl]); - return $this->arrayMerger->getResetData(); + return [$generatedUrl]; } return []; } diff --git a/app/code/Magento/UrlRewrite/Model/ArrayMerger.php b/app/code/Magento/UrlRewrite/Model/ArrayMerger.php index 50ac9c0e06882..5c00c47cd2666 100644 --- a/app/code/Magento/UrlRewrite/Model/ArrayMerger.php +++ b/app/code/Magento/UrlRewrite/Model/ArrayMerger.php @@ -12,7 +12,6 @@ */ class ArrayMerger { - /** * @var $rewritesArray[] */ @@ -38,7 +37,7 @@ public function addData($urlRewritesArray) } /** - * Retrieves data + * Returns the data added and resets the container to an empty array * * @return UrlRewrite[] */ From 8a925d99cb26e041a8a6b41c30fb212dfdbd3d54 Mon Sep 17 00:00:00 2001 From: Cristian Partica Date: Wed, 14 Dec 2016 16:35:41 -0600 Subject: [PATCH 07/39] MAGETWO-58924: SQL error wait timeout error when saving categories - refactoring data maps to unify mysql and data maps, and add query method - removing pool for client querier, we now just have one with specific select - refactoring array merger to UrlRewritesSet for better naming --- .../Category/ChildrenUrlRewriteGenerator.php | 19 ++--- .../CurrentUrlRewritesRegenerator.php | 55 +++++++-------- .../Model/Category/Plugin/Storage.php | 2 +- .../Model/CategoryUrlRewriteGenerator.php | 39 +++++++---- .../Model/Map/DataCategoryMap.php | 21 ++++-- .../Model/Map/DataCategoryUrlRewriteMap.php | 39 +++++++---- .../Map/DataCategoryUsedInProductsMap.php | 24 +++++-- .../Model/Map/DataMapInterface.php | 13 +++- .../Model/Map/DataProductMap.php | 22 ++++-- .../Model/Map/DataProductUrlRewriteMap.php | 37 +++++++--- .../Model/Map/MapFactory.php | 41 ----------- .../Model/Map/MapFactoryInterface.php | 21 ------ .../Model/Map/MapInterface.php | 22 ------ .../CatalogUrlRewrite/Model/Map/MapPool.php | 45 ------------ .../Model/Map/MapPoolInterface.php | 23 ------- .../Model/Map/UrlRewriteMap.php | 69 ++++++++++--------- .../Product/CurrentUrlRewritesRegenerator.php | 52 +++++++------- .../Model/ProductScopeRewriteGenerator.php | 30 ++++---- .../Model/ResourceModel/Category/Product.php | 2 +- .../Observer/AfterImportDataObserver.php | 23 ++++--- .../Observer/UrlRewriteHandler.php | 30 ++++---- .../{ArrayMerger.php => UrlRewritesSet.php} | 24 ++++--- 22 files changed, 300 insertions(+), 353 deletions(-) delete mode 100644 app/code/Magento/CatalogUrlRewrite/Model/Map/MapFactory.php delete mode 100644 app/code/Magento/CatalogUrlRewrite/Model/Map/MapFactoryInterface.php delete mode 100644 app/code/Magento/CatalogUrlRewrite/Model/Map/MapInterface.php delete mode 100644 app/code/Magento/CatalogUrlRewrite/Model/Map/MapPool.php delete mode 100644 app/code/Magento/CatalogUrlRewrite/Model/Map/MapPoolInterface.php rename app/code/Magento/UrlRewrite/Model/{ArrayMerger.php => UrlRewritesSet.php} (67%) diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Category/ChildrenUrlRewriteGenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/Category/ChildrenUrlRewriteGenerator.php index de5a610464482..8b247910cdac3 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Category/ChildrenUrlRewriteGenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Category/ChildrenUrlRewriteGenerator.php @@ -8,7 +8,7 @@ use Magento\Catalog\Model\Category; use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGeneratorFactory; use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator; -use Magento\UrlRewrite\Model\ArrayMerger; +use Magento\UrlRewrite\Model\UrlRewritesSet; use Magento\Framework\App\ObjectManager; class ChildrenUrlRewriteGenerator @@ -19,22 +19,22 @@ class ChildrenUrlRewriteGenerator /** @var \Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGeneratorFactory */ protected $categoryUrlRewriteGeneratorFactory; - /** @var \Magento\UrlRewrite\Model\ArrayMerger */ - private $arrayMerger; + /** @var \Magento\UrlRewrite\Model\UrlRewritesSet */ + private $urlRewritesSet; /** * @param \Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider $childrenCategoriesProvider * @param \Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGeneratorFactory $categoryUrlRewriteGeneratorFactory - * @param ArrayMerger|null $arrayMerger + * @param UrlRewritesSet|null $urlRewritesSet */ public function __construct( ChildrenCategoriesProvider $childrenCategoriesProvider, CategoryUrlRewriteGeneratorFactory $categoryUrlRewriteGeneratorFactory, - ArrayMerger $arrayMerger = null + UrlRewritesSet $urlRewritesSet = null ) { $this->childrenCategoriesProvider = $childrenCategoriesProvider; $this->categoryUrlRewriteGeneratorFactory = $categoryUrlRewriteGeneratorFactory; - $this->arrayMerger = $arrayMerger ?: ObjectManager::getInstance()->get(ArrayMerger::class); + $this->urlRewritesSet = $urlRewritesSet ?: ObjectManager::getInstance()->get(UrlRewritesSet::class); } /** @@ -52,8 +52,11 @@ public function generate($storeId, Category $category, $rootCategoryId = null) $childCategory->setData('save_rewrites_history', $category->getData('save_rewrites_history')); /** @var CategoryUrlRewriteGenerator $categoryUrlRewriteGenerator */ $categoryUrlRewriteGenerator = $this->categoryUrlRewriteGeneratorFactory->create(); - $this->arrayMerger->addData($categoryUrlRewriteGenerator->generate($childCategory, false, $rootCategoryId)); + $this->urlRewritesSet->merge($categoryUrlRewriteGenerator->generate($childCategory, false, $rootCategoryId)); } - return $this->arrayMerger->getResetData(); + + $result = $this->urlRewritesSet->getData(); + $this->urlRewritesSet->resetData(); + return $result; } } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php index c9f8fd63d93a8..3284666b7ea06 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php @@ -13,9 +13,8 @@ use Magento\UrlRewrite\Service\V1\Data\UrlRewrite; use Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory; use Magento\CatalogUrlRewrite\Model\Map\UrlRewriteMap; -use Magento\CatalogUrlRewrite\Model\Map\MapPoolInterface; use Magento\Framework\App\ObjectManager; -use Magento\UrlRewrite\Model\ArrayMerger; +use Magento\UrlRewrite\Model\UrlRewritesSet; class CurrentUrlRewritesRegenerator { @@ -25,34 +24,37 @@ class CurrentUrlRewritesRegenerator /** @var \Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory */ protected $urlRewriteFactory; - /** @var UrlFinderInterface */ + /** + * @var UrlFinderInterface + * @deprecated + */ protected $urlFinder; - /** @var MapPoolInterface */ - private $mapPool; + /** @var UrlRewriteMap */ + private $urlRewriteMap; - /** @var \Magento\UrlRewrite\Model\ArrayMerger */ - private $arrayMerger; + /** @var \Magento\UrlRewrite\Model\UrlRewritesSet */ + private $urlRewritesSet; /** * @param \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator * @param \Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory $urlRewriteFactory * @param UrlFinderInterface $urlFinder - * @param MapPoolInterface|null $mapPool - * @param \Magento\UrlRewrite\Model\ArrayMerger|null $arrayMerger + * @param UrlRewriteMap|null $urlRewriteMap + * @param \Magento\UrlRewrite\Model\UrlRewritesSet|null $urlRewritesSet */ public function __construct( CategoryUrlPathGenerator $categoryUrlPathGenerator, UrlRewriteFactory $urlRewriteFactory, UrlFinderInterface $urlFinder, - MapPoolInterface $mapPool = null, - ArrayMerger $arrayMerger = null + UrlRewriteMap $urlRewriteMap = null, + UrlRewritesSet $urlRewritesSet = null ) { $this->categoryUrlPathGenerator = $categoryUrlPathGenerator; $this->urlRewriteFactory = $urlRewriteFactory; $this->urlFinder = $urlFinder; - $this->mapPool = $mapPool ?: ObjectManager::getInstance()->get(MapPoolInterface::class); - $this->arrayMerger = $arrayMerger ?: ObjectManager::getInstance()->get(ArrayMerger::class); + $this->urlRewriteMap = $urlRewriteMap ?: ObjectManager::getInstance()->get(UrlRewriteMap::class); + $this->urlRewritesSet = $urlRewritesSet ?: ObjectManager::getInstance()->get(UrlRewritesSet::class); } /** @@ -65,29 +67,24 @@ public function __construct( */ public function generate($storeId, Category $category, $rootCategoryId = null) { - $queryArray = [ - UrlRewrite::STORE_ID => $storeId, - UrlRewrite::ENTITY_ID => $category->getEntityId(), - UrlRewrite::ENTITY_TYPE => UrlRewriteMap::ENTITY_TYPE_CATEGORY - ]; - - if ($rootCategoryId) { - $categoryUrlRewriteMap = $this->mapPool->getMap(UrlRewriteMap::class, $rootCategoryId); - - /** @var UrlRewrite[] $currentUrlRewrites */ - $currentUrlRewrites = $categoryUrlRewriteMap->getByIdentifiers($queryArray); - } else { - $currentUrlRewrites = $this->urlFinder->findAllByData($queryArray); - } + $currentUrlRewrites = $this->urlRewriteMap->getByIdentifiers( + $category->getEntityId(), + $storeId, + UrlRewriteMap::ENTITY_TYPE_CATEGORY, + $rootCategoryId + ); foreach ($currentUrlRewrites as $rewrite) { - $this->arrayMerger->addData( + $this->urlRewritesSet->merge( $rewrite->getIsAutogenerated() ? $this->generateForAutogenerated($rewrite, $storeId, $category) : $this->generateForCustom($rewrite, $storeId, $category) ); } - return $this->arrayMerger->getResetData(); + + $result = $this->urlRewritesSet->getData(); + $this->urlRewritesSet->resetData(); + return $result; } /** diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Storage.php b/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Storage.php index cb7a39e4ff16b..d5229da9ec86c 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Storage.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Storage.php @@ -62,7 +62,7 @@ public function afterReplace(StorageInterface $object, $result, array $urls) */ public function beforeDeleteByData(StorageInterface $object, array $data) { - $this->productResource->removeMultipleByFiler($data); + $this->productResource->removeMultipleByFilter($data); } /** diff --git a/app/code/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGenerator.php index b11b9b8cd28a5..ea7255893749e 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGenerator.php @@ -12,7 +12,7 @@ use Magento\CatalogUrlRewrite\Service\V1\StoreViewService; use Magento\Store\Model\Store; use Magento\Catalog\Api\CategoryRepositoryInterface; -use Magento\UrlRewrite\Model\ArrayMerger; +use Magento\UrlRewrite\Model\UrlRewritesSet; use Magento\Framework\App\ObjectManager; class CategoryUrlRewriteGenerator @@ -32,8 +32,8 @@ class CategoryUrlRewriteGenerator /** @var \Magento\CatalogUrlRewrite\Model\Category\ChildrenUrlRewriteGenerator */ protected $childrenUrlRewriteGenerator; - /** @var \Magento\UrlRewrite\Model\ArrayMerger */ - private $arrayMerger; + /** @var \Magento\UrlRewrite\Model\UrlRewritesSet */ + private $urlRewritesSet; /** * @var bool @@ -46,7 +46,7 @@ class CategoryUrlRewriteGenerator * @param \Magento\CatalogUrlRewrite\Model\Category\ChildrenUrlRewriteGenerator $childrenUrlRewriteGenerator * @param \Magento\CatalogUrlRewrite\Service\V1\StoreViewService $storeViewService * @param \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository - * @param \Magento\UrlRewrite\Model\ArrayMerger|null $arrayMerger + * @param \Magento\UrlRewrite\Model\UrlRewritesSet|null $urlRewritesSet */ public function __construct( CanonicalUrlRewriteGenerator $canonicalUrlRewriteGenerator, @@ -54,14 +54,14 @@ public function __construct( ChildrenUrlRewriteGenerator $childrenUrlRewriteGenerator, StoreViewService $storeViewService, CategoryRepositoryInterface $categoryRepository, - ArrayMerger $arrayMerger = null + UrlRewritesSet $urlRewritesSet = null ) { $this->storeViewService = $storeViewService; $this->canonicalUrlRewriteGenerator = $canonicalUrlRewriteGenerator; $this->childrenUrlRewriteGenerator = $childrenUrlRewriteGenerator; $this->currentUrlRewritesRegenerator = $currentUrlRewritesRegenerator; $this->categoryRepository = $categoryRepository; - $this->arrayMerger = $arrayMerger ?: ObjectManager::getInstance()->get(ArrayMerger::class); + $this->urlRewritesSet = $urlRewritesSet ?: ObjectManager::getInstance()->get(UrlRewritesSet::class); } /** @@ -78,7 +78,7 @@ public function generate(Category $category, $overrideStoreUrls = false, $rootCa $storeId = $category->getStoreId(); $urls = $this->isGlobalScope($storeId) - ? $this->generateForGlobalScope($category, $rootCategoryId, $overrideStoreUrls) + ? $this->generateForGlobalScope($category, $overrideStoreUrls, $rootCategoryId) : $this->generateForSpecificStoreView($category, $storeId, $rootCategoryId); return $urls; @@ -100,10 +100,12 @@ protected function generateForGlobalScope(Category $category, $overrideStoreUrls && $this->isOverrideUrlsForStore($storeId, $categoryId, $overrideStoreUrls) ) { $this->updateCategoryUrlForStore($category, $storeId); - $this->arrayMerger->addData($this->generateForSpecificStoreView($category, $storeId, $rootCategoryId)); + $this->urlRewritesSet->merge($this->generateForSpecificStoreView($category, $storeId, $rootCategoryId)); } } - return $this->arrayMerger->getResetData(); + $result = $this->urlRewritesSet->getData(); + $this->urlRewritesSet->resetData(); + return $result; } /** @@ -130,8 +132,13 @@ protected function isOverrideUrlsForStore($storeId, $categoryId, $overrideStoreU */ protected function updateCategoryUrlForStore(Category $category, $storeId) { - $object = $this->categoryRepository->get($category->getId(), $storeId); - $category->addData(['url_key' => $object->getUrlKey(), 'url_path' => $object->getUrlPath()]); + $categoryFromRepository = $this->categoryRepository->get($category->getId(), $storeId); + $category->addData( + [ + 'url_key' => $categoryFromRepository->getUrlKey(), + 'url_path' => $categoryFromRepository->getUrlPath() + ] + ); } /** @@ -155,11 +162,13 @@ protected function isGlobalScope($storeId) */ protected function generateForSpecificStoreView(Category $category, $storeId, $rootCategoryId = null) { - $this->arrayMerger->addData($this->canonicalUrlRewriteGenerator->generate($storeId, $category)); - $this->arrayMerger->addData($this->childrenUrlRewriteGenerator->generate($storeId, $category, $rootCategoryId)); - $this->arrayMerger->addData( + $this->urlRewritesSet->merge($this->canonicalUrlRewriteGenerator->generate($storeId, $category)); + $this->urlRewritesSet->merge($this->childrenUrlRewriteGenerator->generate($storeId, $category, $rootCategoryId)); + $this->urlRewritesSet->merge( $this->currentUrlRewritesRegenerator->generate($storeId, $category, $rootCategoryId) ); - return $this->arrayMerger->getResetData(); + $result = $this->urlRewritesSet->getData(); + $this->urlRewritesSet->resetData(); + return $result; } } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryMap.php index 3b1fe0281d0ff..90f78c0e294bb 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryMap.php @@ -45,21 +45,30 @@ public function __construct( /** * {@inheritdoc} */ - public function getData($categoryId) + public function getAllData($categoryId) { if (empty($this->data[$categoryId])) { - $this->data[$categoryId] = $this->queryData($categoryId); + $this->data[$categoryId] = $this->generateData($categoryId); } return $this->data[$categoryId]; } + /** + * {@inheritdoc} + */ + public function getData($categoryId, $criteria) + { + $this->getAllData($categoryId); + return $this->data[$categoryId][$criteria]; + } + /** * Queries the database and returns results * * @param int $categoryId * @return array */ - private function queryData($categoryId) + private function generateData($categoryId) { $category = $this->categoryRepository->get($categoryId); return $this->collection->addIdFilter($this->getAllCategoryChildrenIds($category)) @@ -87,7 +96,9 @@ private function getAllCategoryChildrenIds($category) */ public function resetData($categoryId) { - unset($this->data); - $this->data = []; + unset($this->data[$categoryId]); + if (empty($this->data)) { + $this->data = []; + } } } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUrlRewriteMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUrlRewriteMap.php index adf98daab89be..73933879045eb 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUrlRewriteMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUrlRewriteMap.php @@ -9,6 +9,7 @@ use Magento\UrlRewrite\Service\V1\Data\UrlRewrite; use Magento\Framework\App\ResourceConnection; use Magento\Framework\DB\TemporaryTableService; +use \Magento\Framework\DB\Select; /** * Map that holds data for category url rewrites entity @@ -18,7 +19,7 @@ class DataCategoryUrlRewriteMap implements DataMapInterface const ENTITY_TYPE = 'category'; /** @var string[] */ - private $data = []; + private $tableNames = []; /** @var UrlRewrite */ private $urlRewritePlaceholder; @@ -53,12 +54,26 @@ public function __construct( /** * {@inheritdoc} */ - public function getData($categoryId) + public function getAllData($categoryId) { - if (empty($this->data[$categoryId])) { - $this->data[$categoryId] = [$this->queryData($categoryId)]; + if (empty($this->tableNames[$categoryId])) { + $this->tableNames[$categoryId] = $this->generateData($categoryId); } - return $this->data[$categoryId]; + return $this->tableNames[$categoryId]; + } + + /** + * Gets data by criteria from a map identified by a category Id + * + * @param int $categoryId + * @param Select $criteria + * @return array + */ + public function getData($categoryId, $criteria) { + $this->getAllData($categoryId); + $urlRewritesConnection = $this->connection->getConnection(); + $criteria->from(['e' => $this->tableNames[$categoryId]]); + return $urlRewritesConnection->fetchAll($criteria); } /** @@ -67,7 +82,7 @@ public function getData($categoryId) * @param int $categoryId * @return string */ - private function queryData($categoryId) + private function generateData($categoryId) { $urlRewritesConnection = $this->connection->getConnection(); $select = $urlRewritesConnection->select() @@ -82,9 +97,9 @@ private function queryData($categoryId) [ 'in' => array_merge( $this->dataMapPool->getDataMap(DataCategoryUsedInProductsMap::class, $categoryId) - ->getData($categoryId), + ->getAllData($categoryId), $this->dataMapPool->getDataMap(DataCategoryMap::class, $categoryId) - ->getData($categoryId) + ->getAllData($categoryId) ) ] ) @@ -108,10 +123,10 @@ public function resetData($categoryId) { $this->dataMapPool->resetDataMap(DataCategoryUsedInProductsMap::class, $categoryId); $this->dataMapPool->resetDataMap(DataCategoryMap::class, $categoryId); - foreach ($this->data as $tableName) { - $this->temporaryTableService->dropTable(reset($tableName)); + $this->temporaryTableService->dropTable($this->tableNames[$categoryId]); + unset($this->tableNames[$categoryId]); + if (empty($this->tableNames)) { + $this->tableNames = []; } - unset($this->data); - $this->data = []; } } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUsedInProductsMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUsedInProductsMap.php index 06fec4bd180dd..021c6e0cc1ed3 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUsedInProductsMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUsedInProductsMap.php @@ -36,21 +36,29 @@ public function __construct( /** * {@inheritdoc} */ - public function getData($categoryId) + public function getAllData($categoryId) { if (empty($this->data[$categoryId])) { - $this->data[$categoryId] = $this->queryData($categoryId); + $this->data[$categoryId] = $this->generateData($categoryId); } return $this->data[$categoryId]; } + /** + * {@inheritdoc} + */ + public function getData($categoryId, $criteria) { + $this->getAllData($categoryId); + return $this->data[$categoryId][$criteria]; + } + /** * Queries the database and returns results * * @param int $categoryId * @return array */ - private function queryData($categoryId) + private function generateData($categoryId) { $productsLinkConnection = $this->connection->getConnection(); $select = $productsLinkConnection->select() @@ -58,13 +66,13 @@ private function queryData($categoryId) ->where( $productsLinkConnection->prepareSqlCondition( 'product_id', - ['in' => $this->dataMapPool->getDataMap(DataProductMap::class, $categoryId)->getData($categoryId)] + ['in' => $this->dataMapPool->getDataMap(DataProductMap::class, $categoryId)->getAllData($categoryId)] ) ) ->where( $productsLinkConnection->prepareSqlCondition( 'category_id', - ['nin' => $this->dataMapPool->getDataMap(DataCategoryMap::class, $categoryId)->getData($categoryId)] + ['nin' => $this->dataMapPool->getDataMap(DataCategoryMap::class, $categoryId)->getAllData($categoryId)] ) ) ->group('category_id'); @@ -79,7 +87,9 @@ public function resetData($categoryId) { $this->dataMapPool->resetDataMap(DataProductMap::class, $categoryId); $this->dataMapPool->resetDataMap(DataCategoryMap::class, $categoryId); - unset($this->data); - $this->data = []; + unset($this->data[$categoryId]); + if (empty($this->data)) { + $this->data = []; + } } } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataMapInterface.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataMapInterface.php index 35a966a86b4ed..379a2188b3c44 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataMapInterface.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataMapInterface.php @@ -5,6 +5,8 @@ */ namespace Magento\CatalogUrlRewrite\Model\Map; +use \Magento\Framework\DB\Select; + /** * Interface for a data map */ @@ -16,7 +18,16 @@ interface DataMapInterface * @param int $categoryId * @return array */ - public function getData($categoryId); + public function getAllData($categoryId); + + /** + * Gets data by criteria from a map identified by a category Id + * + * @param int $categoryId + * @param string|Select $criteria + * @return array + */ + public function getData($categoryId, $criteria); /** * Resets current map and it's dependencies diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductMap.php index e77d78b7169fa..9d0bb806ac0c0 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductMap.php @@ -43,21 +43,29 @@ public function __construct( /** * {@inheritdoc} */ - public function getData($categoryId) + public function getAllData($categoryId) { if (empty($this->data[$categoryId])) { - $this->data[$categoryId] = $this->queryData($categoryId); + $this->data[$categoryId] = $this->generateData($categoryId); } return $this->data[$categoryId]; } + /** + * {@inheritdoc} + */ + public function getData($categoryId, $criteria) { + $this->getAllData($categoryId); + return $this->data[$categoryId][$criteria]; + } + /** * Queries the database and returns results * * @param int $categoryId * @return array */ - private function queryData($categoryId) + private function generateData($categoryId) { $productsCollection = $this->collectionFactory->create(); $productsCollection->getSelect() @@ -69,7 +77,7 @@ private function queryData($categoryId) ->where( $productsCollection->getConnection()->prepareSqlCondition( 'cp.category_id', - ['in' => $this->dataMapPool->getDataMap(DataCategoryMap::class, $categoryId)->getData($categoryId)] + ['in' => $this->dataMapPool->getDataMap(DataCategoryMap::class, $categoryId)->getAllData($categoryId)] ) ) ->group('e.entity_id'); @@ -83,8 +91,8 @@ private function queryData($categoryId) public function resetData($categoryId) { $this->dataMapPool->resetDataMap(DataCategoryMap::class, $categoryId); - unset($this->data); - $this->data = []; - return $this; + if (empty($this->data)) { + $this->data = []; + } } } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductUrlRewriteMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductUrlRewriteMap.php index 2f502f47bec76..ffba4d164e895 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductUrlRewriteMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductUrlRewriteMap.php @@ -9,6 +9,7 @@ use Magento\UrlRewrite\Service\V1\Data\UrlRewrite; use Magento\Framework\App\ResourceConnection; use Magento\Framework\DB\TemporaryTableService; +use \Magento\Framework\DB\Select; /** * Map that holds data for category url rewrites entity @@ -18,7 +19,7 @@ class DataProductUrlRewriteMap implements DataMapInterface const ENTITY_TYPE = 'product'; /** @var string[] */ - private $data = []; + private $tableNames = []; /** @var DataMapPoolInterface */ private $dataMapPool; @@ -49,12 +50,26 @@ public function __construct( /** * {@inheritdoc} */ - public function getData($categoryId) + public function getAllData($categoryId) { - if (empty($this->data[$categoryId])) { - $this->data[$categoryId] = [$this->queryData($categoryId)]; + if (empty($this->tableNames[$categoryId])) { + $this->tableNames[$categoryId] = $this->generateData($categoryId); } - return $this->data[$categoryId]; + return $this->tableNames[$categoryId]; + } + + /** + * Gets data by criteria from a map identified by a category Id + * + * @param int $categoryId + * @param Select $criteria + * @return array + */ + public function getData($categoryId, $criteria) { + $this->getAllData($categoryId); + $urlRewritesConnection = $this->connection->getConnection(); + $criteria->from(['e' => $this->tableNames[$categoryId]]); + return $urlRewritesConnection->fetchAll($criteria); } /** @@ -63,7 +78,7 @@ public function getData($categoryId) * @param int $categoryId * @return string */ - private function queryData($categoryId) + private function generateData($categoryId) { $urlRewritesConnection = $this->connection->getConnection(); $select = $urlRewritesConnection->select() @@ -77,7 +92,7 @@ private function queryData($categoryId) 'entity_id', [ 'in' => $this->dataMapPool->getDataMap(DataProductMap::class, $categoryId) - ->getData($categoryId) + ->getAllData($categoryId) ] ) ); @@ -99,10 +114,10 @@ private function queryData($categoryId) public function resetData($categoryId) { $this->dataMapPool->resetDataMap(DataProductMap::class, $categoryId); - foreach ($this->data as $tableName) { - $this->temporaryTableService->dropTable(reset($tableName)); + $this->temporaryTableService->dropTable($this->tableNames[$categoryId]); + unset($this->tableNames[$categoryId]); + if (empty($this->tableNames)) { + $this->tableNames = []; } - unset($this->data); - $this->data = []; } } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/MapFactory.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/MapFactory.php deleted file mode 100644 index b19bfe1605fb0..0000000000000 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/MapFactory.php +++ /dev/null @@ -1,41 +0,0 @@ -objectManager = $objectManager; - } - - /** - * {@inheritdoc} - */ - public function create($instanceName, $categoryId) - { - return $this->objectManager->create( - $instanceName, - ['categoryId' => $categoryId] - ); - } -} diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/MapFactoryInterface.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/MapFactoryInterface.php deleted file mode 100644 index b2b3943ed38a7..0000000000000 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/MapFactoryInterface.php +++ /dev/null @@ -1,21 +0,0 @@ -clientMapFactory = $clientMapFactory; - } - - /** - * {@inheritdoc} - */ - public function getMap($instanceName, $categoryId) - { - $key = $instanceName . '-' . $categoryId; - if (!isset($this->mapStoragePool[$key])) { - $this->mapStoragePool[$key] = $this->clientMapFactory->create($instanceName, $categoryId); - } - return $this->mapStoragePool[$key]; - } -} diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/MapPoolInterface.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/MapPoolInterface.php deleted file mode 100644 index 80e34c9088dc6..0000000000000 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/MapPoolInterface.php +++ /dev/null @@ -1,23 +0,0 @@ -dataMapPool = $dataMapPool; $this->urlFinder = $urlFinder; $this->connection = $connection; $this->urlRewritePlaceholder = $urlRewriteFactory->create(); - $this->categoryId = $categoryId; } /** - * {@inheritdoc} + * Queries by identifiers from maps or falls-back to UrlFinderInterface + * + * @param int|int[] $entity_id + * @param int|int[] $store_id + * @param string $entity_type + * @param int|null $rootCategoryId + * @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] */ - public function getByIdentifiers($identifiers) + public function getByIdentifiers($entity_id, $store_id, $entity_type, $rootCategoryId = null) { - if ($this->categoryId - && isset($identifiers['entity_type']) - && isset($identifiers['store_id']) - && isset($identifiers['entity_id']) - ) { - $array = []; - if ($identifiers['entity_type'] === self::ENTITY_TYPE_PRODUCT) { - $array = $this->dataMapPool->getDataMap(DataProductUrlRewriteMap::class, $this->categoryId) - ->getData($this->categoryId); - } elseif ($identifiers['entity_type'] === self::ENTITY_TYPE_CATEGORY) { - $array = $this->dataMapPool->getDataMap(DataCategoryUrlRewriteMap::class, $this->categoryId) - ->getData($this->categoryId); + if ($rootCategoryId) { + $map = null; + if ($entity_type === self::ENTITY_TYPE_PRODUCT) { + $map = $this->dataMapPool->getDataMap(DataProductUrlRewriteMap::class, $rootCategoryId); + } elseif ($entity_type === self::ENTITY_TYPE_CATEGORY) { + $map = $this->dataMapPool->getDataMap(DataCategoryUrlRewriteMap::class, $rootCategoryId); } - if (!empty($array)) { - $tableName = reset($array); - if (!is_array($identifiers['entity_id']) && !is_array($identifiers['store_id'])) { - $key = $identifiers['store_id'] . '_' . $identifiers['entity_id']; + if ($map) { + $select = null; + if (!is_array($entity_id) && !is_array($store_id)) { + $key = $store_id . '_' . $entity_id; $urlRewritesConnection = $this->connection->getConnection(); $select = $urlRewritesConnection->select() - ->from(['e' => $tableName]) ->where('hash_key = ?', $key); - return $this->arrayToUrlRewriteObject($urlRewritesConnection->fetchAll($select)); - } else if (is_array($identifiers['entity_id']) && is_array($identifiers['store_id'])) { + } else if (is_array($entity_id) && is_array($store_id)) { $urlRewritesConnection = $this->connection->getConnection(); $select = $urlRewritesConnection->select() - ->from(['e' => $tableName]) ->where( $urlRewritesConnection->prepareSqlCondition( 'store_id', - ['in' => $identifiers['store_id']] + ['in' => $store_id] ) ) ->where( $urlRewritesConnection->prepareSqlCondition( 'entity_id', - ['in' => $identifiers['entity_id']] + ['in' => $entity_id] ) ); - return $this->arrayToUrlRewriteObject($urlRewritesConnection->fetchAll($select)); + } + if ($select) { + return $this->arrayToUrlRewriteObject($map->getData($rootCategoryId, $select)); } } + } - return $this->urlFinder->findAllByData($identifiers); + + return $this->urlFinder->findAllByData( + [ + UrlRewrite::STORE_ID => $store_id, + UrlRewrite::ENTITY_ID => $entity_id, + UrlRewrite::ENTITY_TYPE => $entity_type + ] + ); } /** diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php index eb1dc5b253d3f..ea19e5e021124 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php @@ -15,16 +15,18 @@ use Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator; use Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory; use Magento\CatalogUrlRewrite\Model\Map\UrlRewriteMap; -use Magento\CatalogUrlRewrite\Model\Map\MapPoolInterface; use Magento\Framework\App\ObjectManager; -use Magento\UrlRewrite\Model\ArrayMerger; +use Magento\UrlRewrite\Model\UrlRewritesSet; /** * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ class CurrentUrlRewritesRegenerator { - /** @var UrlFinderInterface */ + /** + * @var UrlFinderInterface + * @deprecated + */ protected $urlFinder; /** @var ProductUrlPathGenerator */ @@ -33,31 +35,31 @@ class CurrentUrlRewritesRegenerator /** @var UrlRewriteFactory */ protected $urlRewriteFactory; - /** @var MapPoolInterface */ - private $mapPool; + /** @var UrlRewriteMap */ + private $urlRewriteMap; - /** @var \Magento\UrlRewrite\Model\ArrayMerger */ - private $arrayMerger; + /** @var \Magento\UrlRewrite\Model\UrlRewritesSet */ + private $urlRewritesSet; /** * @param UrlFinderInterface $urlFinder * @param ProductUrlPathGenerator $productUrlPathGenerator * @param UrlRewriteFactory $urlRewriteFactory - * @param MapPoolInterface|null $mapPool - * @param \Magento\UrlRewrite\Model\ArrayMerger|null $arrayMerger + * @param UrlRewriteMap|null $urlRewriteMap + * @param \Magento\UrlRewrite\Model\UrlRewritesSet|null $urlRewritesSet */ public function __construct( UrlFinderInterface $urlFinder, ProductUrlPathGenerator $productUrlPathGenerator, UrlRewriteFactory $urlRewriteFactory, - MapPoolInterface $mapPool = null, - ArrayMerger $arrayMerger = null + UrlRewriteMap $urlRewriteMap = null, + UrlRewritesSet $urlRewritesSet = null ) { $this->urlFinder = $urlFinder; $this->productUrlPathGenerator = $productUrlPathGenerator; $this->urlRewriteFactory = $urlRewriteFactory; - $this->mapPool = $mapPool ?: ObjectManager::getInstance()->get(MapPoolInterface::class); - $this->arrayMerger = $arrayMerger ?: ObjectManager::getInstance()->get(ArrayMerger::class); + $this->urlRewriteMap = $urlRewriteMap ?: ObjectManager::getInstance()->get(UrlRewriteMap::class); + $this->urlRewritesSet = $urlRewritesSet ?: ObjectManager::getInstance()->get(UrlRewritesSet::class); } /** @@ -71,32 +73,28 @@ public function __construct( */ public function generate($storeId, Product $product, ObjectRegistry $productCategories, $rootCategoryId = null) { - $queryArray = [ - UrlRewrite::STORE_ID => $storeId, - UrlRewrite::ENTITY_ID => $product->getEntityId(), - UrlRewrite::ENTITY_TYPE => UrlRewriteMap::ENTITY_TYPE_PRODUCT - ]; - - if ($rootCategoryId) { - $productUrlRewriteMap = $this->mapPool->getMap(UrlRewriteMap::class, $rootCategoryId); - $currentUrlRewrites = $productUrlRewriteMap->getByIdentifiers($queryArray); - } else { - $currentUrlRewrites = $this->urlFinder->findAllByData($queryArray); - } + $currentUrlRewrites = $this->urlRewriteMap->getByIdentifiers( + $product->getEntityId(), + $storeId, + UrlRewriteMap::ENTITY_TYPE_PRODUCT, + $rootCategoryId + ); foreach ($currentUrlRewrites as $currentUrlRewrite) { $category = $this->retrieveCategoryFromMetadata($currentUrlRewrite, $productCategories); if ($category === false) { continue; } - $this->arrayMerger->addData( + $this->urlRewritesSet->merge( $currentUrlRewrite->getIsAutogenerated() ? $this->generateForAutogenerated($currentUrlRewrite, $storeId, $category, $product) : $this->generateForCustom($currentUrlRewrite, $storeId, $category, $product) ); } - return $this->arrayMerger->getResetData(); + $result = $this->urlRewritesSet->getData(); + $this->urlRewritesSet->resetData(); + return $result; } /** diff --git a/app/code/Magento/CatalogUrlRewrite/Model/ProductScopeRewriteGenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/ProductScopeRewriteGenerator.php index 5a48297975203..4ed01e4909c7f 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/ProductScopeRewriteGenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/ProductScopeRewriteGenerator.php @@ -14,7 +14,7 @@ use Magento\CatalogUrlRewrite\Service\V1\StoreViewService; use Magento\Store\Model\Store; use Magento\Store\Model\StoreManagerInterface; -use Magento\UrlRewrite\Model\ArrayMerger; +use Magento\UrlRewrite\Model\UrlRewritesSet; use Magento\Framework\App\ObjectManager; /** @@ -58,8 +58,8 @@ class ProductScopeRewriteGenerator */ private $canonicalUrlRewriteGenerator; - /** @var \Magento\UrlRewrite\Model\ArrayMerger */ - private $arrayMerger; + /** @var \Magento\UrlRewrite\Model\UrlRewritesSet */ + private $urlRewritesSet; /** * @param StoreViewService $storeViewService @@ -69,7 +69,7 @@ class ProductScopeRewriteGenerator * @param CategoriesUrlRewriteGenerator $categoriesUrlRewriteGenerator * @param CurrentUrlRewritesRegenerator $currentUrlRewritesRegenerator * @param AnchorUrlRewriteGenerator $anchorUrlRewriteGenerator - * @param ArrayMerger|null $arrayMerger + * @param UrlRewritesSet|null $urlRewritesSet */ public function __construct( StoreViewService $storeViewService, @@ -79,7 +79,7 @@ public function __construct( CategoriesUrlRewriteGenerator $categoriesUrlRewriteGenerator, CurrentUrlRewritesRegenerator $currentUrlRewritesRegenerator, AnchorUrlRewriteGenerator $anchorUrlRewriteGenerator, - ArrayMerger $arrayMerger = null + UrlRewritesSet $urlRewritesSet = null ) { $this->storeViewService = $storeViewService; $this->storeManager = $storeManager; @@ -88,7 +88,7 @@ public function __construct( $this->categoriesUrlRewriteGenerator = $categoriesUrlRewriteGenerator; $this->currentUrlRewritesRegenerator = $currentUrlRewritesRegenerator; $this->anchorUrlRewriteGenerator = $anchorUrlRewriteGenerator; - $this->arrayMerger = $arrayMerger ?: ObjectManager::getInstance()->get(ArrayMerger::class); + $this->urlRewritesSet = $urlRewritesSet ?: ObjectManager::getInstance()->get(UrlRewritesSet::class); } /** @@ -121,13 +121,15 @@ public function generateForGlobalScope($productCategories, Product $product, $ro $productId, Product::ENTITY )) { - $this->arrayMerger->addData( + $this->urlRewritesSet->merge( $this->generateForSpecificStoreView($id, $productCategories, $product, $rootCategoryId) ); } } - return $this->arrayMerger->getResetData(); + $result = $this->urlRewritesSet->getData(); + $this->urlRewritesSet->resetData(); + return $result; } /** @@ -149,13 +151,13 @@ public function generateForSpecificStoreView($storeId, $productCategories, Produ } $productCategories = $this->objectRegistryFactory->create(['entities' => $categories]); - $this->arrayMerger->addData( + $this->urlRewritesSet->merge( $this->canonicalUrlRewriteGenerator->generate($storeId, $product) ); - $this->arrayMerger->addData( + $this->urlRewritesSet->merge( $this->categoriesUrlRewriteGenerator->generate($storeId, $product, $productCategories) ); - $this->arrayMerger->addData( + $this->urlRewritesSet->merge( $this->currentUrlRewritesRegenerator->generate( $storeId, $product, @@ -163,11 +165,13 @@ public function generateForSpecificStoreView($storeId, $productCategories, Produ $rootCategoryId ) ); - $this->arrayMerger->addData( + $this->urlRewritesSet->merge( $this->anchorUrlRewriteGenerator->generate($storeId, $product, $productCategories) ); - return $this->arrayMerger->getResetData(); + $result = $this->urlRewritesSet->getData(); + $this->urlRewritesSet->resetData(); + return $result; } /** diff --git a/app/code/Magento/CatalogUrlRewrite/Model/ResourceModel/Category/Product.php b/app/code/Magento/CatalogUrlRewrite/Model/ResourceModel/Category/Product.php index 923d98ca13d44..1605ad2643113 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/ResourceModel/Category/Product.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/ResourceModel/Category/Product.php @@ -75,7 +75,7 @@ public function removeMultiple(array $removeData) * @param array $filter * @return int */ - public function removeMultipleByFiler(array $filter) + public function removeMultipleByFilter(array $filter) { return $this->getConnection()->delete( $this->getTable(self::TABLE_NAME), diff --git a/app/code/Magento/CatalogUrlRewrite/Observer/AfterImportDataObserver.php b/app/code/Magento/CatalogUrlRewrite/Observer/AfterImportDataObserver.php index 314288a148592..065293a704135 100644 --- a/app/code/Magento/CatalogUrlRewrite/Observer/AfterImportDataObserver.php +++ b/app/code/Magento/CatalogUrlRewrite/Observer/AfterImportDataObserver.php @@ -21,7 +21,7 @@ use Magento\Framework\Event\ObserverInterface; use Magento\Catalog\Model\Product\Visibility; use Magento\Framework\App\ObjectManager; -use Magento\UrlRewrite\Model\ArrayMerger; +use Magento\UrlRewrite\Model\UrlRewritesSet; /** * Class AfterImportDataObserver @@ -99,8 +99,8 @@ class AfterImportDataObserver implements ObserverInterface 'visibility', ]; - /** @var \Magento\UrlRewrite\Model\ArrayMerger */ - private $arrayMerger; + /** @var \Magento\UrlRewrite\Model\UrlRewritesSet */ + private $urlRewritesSet; /** * @param \Magento\Catalog\Model\ProductFactory $catalogProductFactory @@ -111,7 +111,7 @@ class AfterImportDataObserver implements ObserverInterface * @param UrlPersistInterface $urlPersist * @param UrlRewriteFactory $urlRewriteFactory * @param UrlFinderInterface $urlFinder - * @param \Magento\UrlRewrite\Model\ArrayMerger|null $arrayMerger + * @param \Magento\UrlRewrite\Model\UrlRewritesSet|null $urlRewritesSet * @throws \InvalidArgumentException * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ @@ -124,7 +124,7 @@ public function __construct( UrlPersistInterface $urlPersist, UrlRewriteFactory $urlRewriteFactory, UrlFinderInterface $urlFinder, - ArrayMerger $arrayMerger = null + UrlRewritesSet $urlRewritesSet = null ) { $this->urlPersist = $urlPersist; $this->catalogProductFactory = $catalogProductFactory; @@ -134,7 +134,7 @@ public function __construct( $this->storeManager = $storeManager; $this->urlRewriteFactory = $urlRewriteFactory; $this->urlFinder = $urlFinder; - $this->arrayMerger = $arrayMerger ?: ObjectManager::getInstance()->get(ArrayMerger::class); + $this->urlRewritesSet = $urlRewritesSet ?: ObjectManager::getInstance()->get(UrlRewritesSet::class); } /** @@ -267,14 +267,17 @@ protected function populateGlobalProduct($product) */ protected function generateUrls() { - $this->arrayMerger->addData($this->canonicalUrlRewriteGenerate()); - $this->arrayMerger->addData($this->categoriesUrlRewriteGenerate()); - $this->arrayMerger->addData($this->currentUrlRewritesRegenerate()); + $this->urlRewritesSet->merge($this->canonicalUrlRewriteGenerate()); + $this->urlRewritesSet->merge($this->categoriesUrlRewriteGenerate()); + $this->urlRewritesSet->merge($this->currentUrlRewritesRegenerate()); $this->productCategories = null; unset($this->products); $this->products = []; - return $this->arrayMerger->getResetData(); + + $result = $this->urlRewritesSet->getData(); + $this->urlRewritesSet->resetData(); + return $result; } /** diff --git a/app/code/Magento/CatalogUrlRewrite/Observer/UrlRewriteHandler.php b/app/code/Magento/CatalogUrlRewrite/Observer/UrlRewriteHandler.php index 4b61b225a3c42..92ceff1c434b1 100644 --- a/app/code/Magento/CatalogUrlRewrite/Observer/UrlRewriteHandler.php +++ b/app/code/Magento/CatalogUrlRewrite/Observer/UrlRewriteHandler.php @@ -13,7 +13,7 @@ use Magento\Framework\Event\Observer as EventObserver; use Magento\UrlRewrite\Model\UrlPersistInterface; use Magento\UrlRewrite\Service\V1\Data\UrlRewrite; -use Magento\UrlRewrite\Model\ArrayMerger; +use Magento\UrlRewrite\Model\UrlRewritesSet; class UrlRewriteHandler { @@ -40,8 +40,8 @@ class UrlRewriteHandler */ private $categoryBasedProductRewriteGenerator; - /** @var \Magento\UrlRewrite\Model\ArrayMerger */ - private $arrayMerger; + /** @var \Magento\UrlRewrite\Model\UrlRewritesSet */ + private $urlRewritesSet; /** * @param \Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider $childrenCategoriesProvider @@ -49,7 +49,7 @@ class UrlRewriteHandler * @param ProductUrlRewriteGenerator $productUrlRewriteGenerator * @param UrlPersistInterface $urlPersist * @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory - * @param \Magento\UrlRewrite\Model\ArrayMerger|null $arrayMerger + * @param \Magento\UrlRewrite\Model\UrlRewritesSet|null $urlRewritesSet */ public function __construct( \Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider $childrenCategoriesProvider, @@ -57,14 +57,14 @@ public function __construct( ProductUrlRewriteGenerator $productUrlRewriteGenerator, UrlPersistInterface $urlPersist, \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory, - ArrayMerger $arrayMerger = null + UrlRewritesSet $urlRewritesSet = null ) { $this->childrenCategoriesProvider = $childrenCategoriesProvider; $this->categoryUrlRewriteGenerator = $categoryUrlRewriteGenerator; $this->productUrlRewriteGenerator = $productUrlRewriteGenerator; $this->urlPersist = $urlPersist; $this->productCollectionFactory = $productCollectionFactory; - $this->arrayMerger = $arrayMerger ?: ObjectManager::getInstance()->get(ArrayMerger::class); + $this->urlRewritesSet = $urlRewritesSet ?: ObjectManager::getInstance()->get(UrlRewritesSet::class); } /** @@ -90,12 +90,12 @@ public function generateProductUrlRewrites(Category $category) foreach ($collection as $product) { $product->setStoreId($storeId); $product->setData('save_rewrites_history', $saveRewriteHistory); - $this->arrayMerger->addData( + $this->urlRewritesSet->merge( $this->productUrlRewriteGenerator->generate($product, $category->getEntityId()) ); } } else { - $this->arrayMerger->addData( + $this->urlRewritesSet->merge( $this->getCategoryProductsUrlRewrites( $category, $storeId, @@ -105,7 +105,7 @@ public function generateProductUrlRewrites(Category $category) ); } foreach ($this->childrenCategoriesProvider->getChildren($category, true) as $childCategory) { - $this->arrayMerger->addData( + $this->urlRewritesSet->merge( $this->getCategoryProductsUrlRewrites( $childCategory, $storeId, @@ -114,7 +114,10 @@ public function generateProductUrlRewrites(Category $category) ) ); } - return $this->arrayMerger->getResetData(); + + $result = $this->urlRewritesSet->getData(); + $this->urlRewritesSet->resetData(); + return $result; } /** @@ -143,11 +146,14 @@ public function getCategoryProductsUrlRewrites( $this->isSkippedProduct[] = $product->getId(); $product->setStoreId($storeId); $product->setData('save_rewrites_history', $saveRewriteHistory); - $this->arrayMerger->addData( + $this->urlRewritesSet->merge( $this->getCategoryBasedProductRewriteGenerator()->generate($product, $category, $rootCategoryId) ); } - return $this->arrayMerger->getResetData(); + + $result = $this->urlRewritesSet->getData(); + $this->urlRewritesSet->resetData(); + return $result; } /** diff --git a/app/code/Magento/UrlRewrite/Model/ArrayMerger.php b/app/code/Magento/UrlRewrite/Model/UrlRewritesSet.php similarity index 67% rename from app/code/Magento/UrlRewrite/Model/ArrayMerger.php rename to app/code/Magento/UrlRewrite/Model/UrlRewritesSet.php index 5c00c47cd2666..3233f76689ffd 100644 --- a/app/code/Magento/UrlRewrite/Model/ArrayMerger.php +++ b/app/code/Magento/UrlRewrite/Model/UrlRewritesSet.php @@ -8,9 +8,9 @@ use Magento\UrlRewrite\Service\V1\Data\UrlRewrite; /** - * Merges an array of Url Rewrites + * Removes duplicates for a set/array of Url Rewrites */ -class ArrayMerger +class UrlRewritesSet { /** * @var $rewritesArray[] @@ -18,12 +18,12 @@ class ArrayMerger private $data = []; /** - * Adds url rewrites to class data container merging with previous data by keys + * Adds url rewrites to class data container by removing duplicates by a unique key * * @param UrlRewrite[] $urlRewritesArray * @return void */ - public function addData($urlRewritesArray) + public function merge($urlRewritesArray) { $separator = '_'; foreach ($urlRewritesArray as $urlRewrite) { @@ -37,15 +37,23 @@ public function addData($urlRewritesArray) } /** - * Returns the data added and resets the container to an empty array + * Returns the data added to container * * @return UrlRewrite[] */ - public function getResetData() + public function getData() + { + return $this->data; + } + + /** + * Resets the container to an empty array + * + * @return void + */ + public function resetData() { - $result = $this->data; unset($this->data); $this->data = []; - return $result; } } From feab7fa095c8e82d73f8b1c7cbfcd1ad4def385a Mon Sep 17 00:00:00 2001 From: Cristian Partica Date: Thu, 15 Dec 2016 14:10:57 -0600 Subject: [PATCH 08/39] MAGETWO-58924: SQL error wait timeout error when saving categories - fix static tests - remove un-needed array of entities for mapping --- .../Category/ChildrenUrlRewriteGenerator.php | 4 +- .../Model/CategoryUrlRewriteGenerator.php | 8 +++- .../Model/Map/DataCategoryMap.php | 4 +- .../Model/Map/DataCategoryUrlRewriteMap.php | 12 +++-- .../Map/DataCategoryUsedInProductsMap.php | 22 ++++++--- .../Model/Map/DataMapInterface.php | 4 +- .../Model/Map/DataProductMap.php | 15 ++++-- .../Model/Map/DataProductUrlRewriteMap.php | 15 +++--- .../Model/Map/UrlRewriteMap.php | 48 +++++-------------- 9 files changed, 66 insertions(+), 66 deletions(-) diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Category/ChildrenUrlRewriteGenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/Category/ChildrenUrlRewriteGenerator.php index 8b247910cdac3..14215d6231041 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Category/ChildrenUrlRewriteGenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Category/ChildrenUrlRewriteGenerator.php @@ -52,7 +52,9 @@ public function generate($storeId, Category $category, $rootCategoryId = null) $childCategory->setData('save_rewrites_history', $category->getData('save_rewrites_history')); /** @var CategoryUrlRewriteGenerator $categoryUrlRewriteGenerator */ $categoryUrlRewriteGenerator = $this->categoryUrlRewriteGeneratorFactory->create(); - $this->urlRewritesSet->merge($categoryUrlRewriteGenerator->generate($childCategory, false, $rootCategoryId)); + $this->urlRewritesSet->merge( + $categoryUrlRewriteGenerator->generate($childCategory, false, $rootCategoryId) + ); } $result = $this->urlRewritesSet->getData(); diff --git a/app/code/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGenerator.php index ea7255893749e..8d8f4f2895ccb 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGenerator.php @@ -162,8 +162,12 @@ protected function isGlobalScope($storeId) */ protected function generateForSpecificStoreView(Category $category, $storeId, $rootCategoryId = null) { - $this->urlRewritesSet->merge($this->canonicalUrlRewriteGenerator->generate($storeId, $category)); - $this->urlRewritesSet->merge($this->childrenUrlRewriteGenerator->generate($storeId, $category, $rootCategoryId)); + $this->urlRewritesSet->merge( + $this->canonicalUrlRewriteGenerator->generate($storeId, $category) + ); + $this->urlRewritesSet->merge( + $this->childrenUrlRewriteGenerator->generate($storeId, $category, $rootCategoryId) + ); $this->urlRewritesSet->merge( $this->currentUrlRewritesRegenerator->generate($storeId, $category, $rootCategoryId) ); diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryMap.php index 90f78c0e294bb..3077e273d83ea 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryMap.php @@ -56,10 +56,10 @@ public function getAllData($categoryId) /** * {@inheritdoc} */ - public function getData($categoryId, $criteria) + public function getData($categoryId, $key) { $this->getAllData($categoryId); - return $this->data[$categoryId][$criteria]; + return $this->data[$categoryId][$key]; } /** diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUrlRewriteMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUrlRewriteMap.php index 73933879045eb..b46feb8e745bb 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUrlRewriteMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUrlRewriteMap.php @@ -66,14 +66,18 @@ public function getAllData($categoryId) * Gets data by criteria from a map identified by a category Id * * @param int $categoryId - * @param Select $criteria + * @param string $key * @return array */ - public function getData($categoryId, $criteria) { + public function getData($categoryId, $key) + { $this->getAllData($categoryId); $urlRewritesConnection = $this->connection->getConnection(); - $criteria->from(['e' => $this->tableNames[$categoryId]]); - return $urlRewritesConnection->fetchAll($criteria); + $select = $urlRewritesConnection->select() + ->from(['e' => $this->tableNames[$categoryId]]) + ->where('hash_key = ?', $key); + + return $urlRewritesConnection->fetchAll($select); } /** diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUsedInProductsMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUsedInProductsMap.php index 021c6e0cc1ed3..ba53475947ca5 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUsedInProductsMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUsedInProductsMap.php @@ -47,9 +47,10 @@ public function getAllData($categoryId) /** * {@inheritdoc} */ - public function getData($categoryId, $criteria) { + public function getData($categoryId, $key) + { $this->getAllData($categoryId); - return $this->data[$categoryId][$criteria]; + return $this->data[$categoryId][$key]; } /** @@ -66,16 +67,25 @@ private function generateData($categoryId) ->where( $productsLinkConnection->prepareSqlCondition( 'product_id', - ['in' => $this->dataMapPool->getDataMap(DataProductMap::class, $categoryId)->getAllData($categoryId)] + [ + 'in' => $this->dataMapPool->getDataMap( + DataProductMap::class, + $categoryId + )->getAllData($categoryId) + ] ) ) ->where( $productsLinkConnection->prepareSqlCondition( 'category_id', - ['nin' => $this->dataMapPool->getDataMap(DataCategoryMap::class, $categoryId)->getAllData($categoryId)] + [ + 'nin' => $this->dataMapPool->getDataMap( + DataCategoryMap::class, + $categoryId + )->getAllData($categoryId) + ] ) - ) - ->group('category_id'); + )->group('category_id'); return $productsLinkConnection->fetchCol($select); } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataMapInterface.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataMapInterface.php index 379a2188b3c44..49d36a58c9063 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataMapInterface.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataMapInterface.php @@ -24,10 +24,10 @@ public function getAllData($categoryId); * Gets data by criteria from a map identified by a category Id * * @param int $categoryId - * @param string|Select $criteria + * @param string $key * @return array */ - public function getData($categoryId, $criteria); + public function getData($categoryId, $key); /** * Resets current map and it's dependencies diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductMap.php index 9d0bb806ac0c0..af6bc764919cf 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductMap.php @@ -54,9 +54,10 @@ public function getAllData($categoryId) /** * {@inheritdoc} */ - public function getData($categoryId, $criteria) { + public function getData($categoryId, $key) + { $this->getAllData($categoryId); - return $this->data[$categoryId][$criteria]; + return $this->data[$categoryId][$key]; } /** @@ -77,10 +78,14 @@ private function generateData($categoryId) ->where( $productsCollection->getConnection()->prepareSqlCondition( 'cp.category_id', - ['in' => $this->dataMapPool->getDataMap(DataCategoryMap::class, $categoryId)->getAllData($categoryId)] + [ + 'in' => $this->dataMapPool->getDataMap( + DataCategoryMap::class, + $categoryId + )->getAllData($categoryId) + ] ) - ) - ->group('e.entity_id'); + )->group('e.entity_id'); return $productsCollection->getAllIds(); } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductUrlRewriteMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductUrlRewriteMap.php index ffba4d164e895..2729045df098e 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductUrlRewriteMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductUrlRewriteMap.php @@ -59,17 +59,16 @@ public function getAllData($categoryId) } /** - * Gets data by criteria from a map identified by a category Id - * - * @param int $categoryId - * @param Select $criteria - * @return array + * {@inheritdoc} */ - public function getData($categoryId, $criteria) { + public function getData($categoryId, $key) + { $this->getAllData($categoryId); $urlRewritesConnection = $this->connection->getConnection(); - $criteria->from(['e' => $this->tableNames[$categoryId]]); - return $urlRewritesConnection->fetchAll($criteria); + $select = $urlRewritesConnection->select() + ->from(['e' => $this->tableNames[$categoryId]]) + ->where('hash_key = ?', $key); + return $urlRewritesConnection->fetchAll($select); } /** diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/UrlRewriteMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/UrlRewriteMap.php index 4c3fa17c62714..078dfa61bfa80 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/UrlRewriteMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/UrlRewriteMap.php @@ -52,57 +52,33 @@ public function __construct( /** * Queries by identifiers from maps or falls-back to UrlFinderInterface * - * @param int|int[] $entity_id - * @param int|int[] $store_id - * @param string $entity_type + * @param int $entityId + * @param int $storeId + * @param string $entityType * @param int|null $rootCategoryId * @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] */ - public function getByIdentifiers($entity_id, $store_id, $entity_type, $rootCategoryId = null) + public function getByIdentifiers($entityId, $storeId, $entityType, $rootCategoryId = null) { - if ($rootCategoryId) { + if ($rootCategoryId && is_numeric($entityId) && is_numeric($storeId) && is_string($entityType)) { $map = null; - if ($entity_type === self::ENTITY_TYPE_PRODUCT) { + if ($entityType === self::ENTITY_TYPE_PRODUCT) { $map = $this->dataMapPool->getDataMap(DataProductUrlRewriteMap::class, $rootCategoryId); - } elseif ($entity_type === self::ENTITY_TYPE_CATEGORY) { + } elseif ($entityType === self::ENTITY_TYPE_CATEGORY) { $map = $this->dataMapPool->getDataMap(DataCategoryUrlRewriteMap::class, $rootCategoryId); } if ($map) { - $select = null; - if (!is_array($entity_id) && !is_array($store_id)) { - $key = $store_id . '_' . $entity_id; - $urlRewritesConnection = $this->connection->getConnection(); - $select = $urlRewritesConnection->select() - ->where('hash_key = ?', $key); - } else if (is_array($entity_id) && is_array($store_id)) { - $urlRewritesConnection = $this->connection->getConnection(); - $select = $urlRewritesConnection->select() - ->where( - $urlRewritesConnection->prepareSqlCondition( - 'store_id', - ['in' => $store_id] - ) - ) - ->where( - $urlRewritesConnection->prepareSqlCondition( - 'entity_id', - ['in' => $entity_id] - ) - ); - } - if ($select) { - return $this->arrayToUrlRewriteObject($map->getData($rootCategoryId, $select)); - } + $key = $storeId . '_' . $entityId; + return $this->arrayToUrlRewriteObject($map->getData($rootCategoryId, $key)); } - } return $this->urlFinder->findAllByData( [ - UrlRewrite::STORE_ID => $store_id, - UrlRewrite::ENTITY_ID => $entity_id, - UrlRewrite::ENTITY_TYPE => $entity_type + UrlRewrite::STORE_ID => $storeId, + UrlRewrite::ENTITY_ID => $entityId, + UrlRewrite::ENTITY_TYPE => $entityType ] ); } From 85509d0d2adccd4b3453a3dd02b4f5aca47d0582 Mon Sep 17 00:00:00 2001 From: Cristian Partica Date: Thu, 15 Dec 2016 14:40:40 -0600 Subject: [PATCH 09/39] MAGETWO-58924: SQL error wait timeout error when saving categories - fix static tests --- .../Model/Map/DataCategoryUrlRewriteMap.php | 1 + .../CatalogUrlRewrite/Model/Map/DataProductUrlRewriteMap.php | 5 +---- .../Magento/CatalogUrlRewrite/Model/Map/UrlRewriteMap.php | 2 +- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUrlRewriteMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUrlRewriteMap.php index b46feb8e745bb..06092a5b36624 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUrlRewriteMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUrlRewriteMap.php @@ -13,6 +13,7 @@ /** * Map that holds data for category url rewrites entity + * @SuppressWarnings(PHPCPD) */ class DataCategoryUrlRewriteMap implements DataMapInterface { diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductUrlRewriteMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductUrlRewriteMap.php index 2729045df098e..d9e87a3725ec9 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductUrlRewriteMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductUrlRewriteMap.php @@ -5,14 +5,13 @@ */ namespace Magento\CatalogUrlRewrite\Model\Map; -use Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory; -use Magento\UrlRewrite\Service\V1\Data\UrlRewrite; use Magento\Framework\App\ResourceConnection; use Magento\Framework\DB\TemporaryTableService; use \Magento\Framework\DB\Select; /** * Map that holds data for category url rewrites entity + * @SuppressWarnings(PHPCPD) */ class DataProductUrlRewriteMap implements DataMapInterface { @@ -33,13 +32,11 @@ class DataProductUrlRewriteMap implements DataMapInterface /** * @param ResourceConnection $connection * @param DataMapPoolInterface $dataMapPool, - * @param UrlRewriteFactory $urlRewriteFactory * @param TemporaryTableService $temporaryTableService, */ public function __construct( ResourceConnection $connection, DataMapPoolInterface $dataMapPool, - UrlRewriteFactory $urlRewriteFactory, TemporaryTableService $temporaryTableService ) { $this->connection = $connection; diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/UrlRewriteMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/UrlRewriteMap.php index 078dfa61bfa80..62f09ee4fae11 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/UrlRewriteMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/UrlRewriteMap.php @@ -12,7 +12,7 @@ use Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory; /** - * Allows query to DataCategoryUrlRewriteMap and DataProductUrlRewriteMapclass or UrlFinderInterface by identifiers + * Allows query to DataCategoryUrlRewriteMap and DataProductUrlRewriteMap class or UrlFinderInterface by identifiers */ class UrlRewriteMap { From ac1b034bf19cb319d74d7c96fc91a7c48e1f80de Mon Sep 17 00:00:00 2001 From: Cristian Partica Date: Thu, 15 Dec 2016 15:13:39 -0600 Subject: [PATCH 10/39] MAGETWO-58924: SQL error wait timeout error when saving categories - code refactor --- .../CurrentUrlRewritesRegenerator.php | 21 ++++++++++++------- .../Model/Map/UrlRewriteMap.php | 7 ------- .../Product/CurrentUrlRewritesRegenerator.php | 19 +++++++++++------ 3 files changed, 27 insertions(+), 20 deletions(-) diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php index 3284666b7ea06..486c7a7aa9439 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php @@ -10,8 +10,8 @@ use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator; use Magento\UrlRewrite\Model\OptionProvider; use Magento\UrlRewrite\Model\UrlFinderInterface; -use Magento\UrlRewrite\Service\V1\Data\UrlRewrite; use Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory; +use Magento\UrlRewrite\Service\V1\Data\UrlRewrite; use Magento\CatalogUrlRewrite\Model\Map\UrlRewriteMap; use Magento\Framework\App\ObjectManager; use Magento\UrlRewrite\Model\UrlRewritesSet; @@ -21,9 +21,15 @@ class CurrentUrlRewritesRegenerator /** @var \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator */ protected $categoryUrlPathGenerator; - /** @var \Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory */ + /** + * @var UrlRewriteFactory + * @deprecated + */ protected $urlRewriteFactory; + /** @var UrlRewrite */ + private $urlRewritePlaceholder; + /** * @var UrlFinderInterface * @deprecated @@ -52,6 +58,7 @@ public function __construct( ) { $this->categoryUrlPathGenerator = $categoryUrlPathGenerator; $this->urlRewriteFactory = $urlRewriteFactory; + $this->urlRewritePlaceholder = $urlRewriteFactory->create(); $this->urlFinder = $urlFinder; $this->urlRewriteMap = $urlRewriteMap ?: ObjectManager::getInstance()->get(UrlRewriteMap::class); $this->urlRewritesSet = $urlRewritesSet ?: ObjectManager::getInstance()->get(UrlRewritesSet::class); @@ -70,7 +77,7 @@ public function generate($storeId, Category $category, $rootCategoryId = null) $currentUrlRewrites = $this->urlRewriteMap->getByIdentifiers( $category->getEntityId(), $storeId, - UrlRewriteMap::ENTITY_TYPE_CATEGORY, + CategoryUrlRewriteGenerator::ENTITY_TYPE, $rootCategoryId ); @@ -98,8 +105,8 @@ protected function generateForAutogenerated($url, $storeId, Category $category) if ($category->getData('save_rewrites_history')) { $targetPath = $this->categoryUrlPathGenerator->getUrlPathWithSuffix($category, $storeId); if ($url->getRequestPath() !== $targetPath) { - $generatedUrl = $this->urlRewriteFactory->create() - ->setEntityType(CategoryUrlRewriteGenerator::ENTITY_TYPE) + $generatedUrl = clone $this->urlRewritePlaceholder; + $generatedUrl->setEntityType(CategoryUrlRewriteGenerator::ENTITY_TYPE) ->setEntityId($category->getEntityId()) ->setRequestPath($url->getRequestPath()) ->setTargetPath($targetPath) @@ -124,8 +131,8 @@ protected function generateForCustom($url, $storeId, Category $category) ? $url->getTargetPath() : $this->categoryUrlPathGenerator->getUrlPathWithSuffix($category, $storeId); if ($url->getRequestPath() !== $targetPath) { - $generatedUrl = $this->urlRewriteFactory->create() - ->setEntityType(CategoryUrlRewriteGenerator::ENTITY_TYPE) + $generatedUrl = clone $this->urlRewritePlaceholder; + $generatedUrl->setEntityType(CategoryUrlRewriteGenerator::ENTITY_TYPE) ->setEntityId($category->getEntityId()) ->setRequestPath($url->getRequestPath()) ->setTargetPath($targetPath) diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/UrlRewriteMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/UrlRewriteMap.php index 62f09ee4fae11..9c724807c9b53 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/UrlRewriteMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/UrlRewriteMap.php @@ -8,7 +8,6 @@ use Magento\Catalog\Model\Product; use Magento\UrlRewrite\Model\UrlFinderInterface; use Magento\UrlRewrite\Service\V1\Data\UrlRewrite; -use Magento\Framework\App\ResourceConnection; use Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory; /** @@ -25,27 +24,21 @@ class UrlRewriteMap /** @var UrlFinderInterface */ private $urlFinder; - /** @var ResourceConnection */ - private $connection; - /** @var UrlRewrite */ private $urlRewritePlaceholder; /** * @param DataMapPoolInterface $dataMapPool * @param UrlFinderInterface $urlFinder - * @param ResourceConnection $connection * @param UrlRewriteFactory $urlRewriteFactory */ public function __construct( DataMapPoolInterface $dataMapPool, UrlFinderInterface $urlFinder, - ResourceConnection $connection, UrlRewriteFactory $urlRewriteFactory ) { $this->dataMapPool = $dataMapPool; $this->urlFinder = $urlFinder; - $this->connection = $connection; $this->urlRewritePlaceholder = $urlRewriteFactory->create(); } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php index ea19e5e021124..760fa18c7f908 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php @@ -32,9 +32,15 @@ class CurrentUrlRewritesRegenerator /** @var ProductUrlPathGenerator */ protected $productUrlPathGenerator; - /** @var UrlRewriteFactory */ + /** + * @var UrlRewriteFactory + * @deprecated + */ protected $urlRewriteFactory; + /** @var UrlRewrite */ + private $urlRewritePlaceholder; + /** @var UrlRewriteMap */ private $urlRewriteMap; @@ -58,6 +64,7 @@ public function __construct( $this->urlFinder = $urlFinder; $this->productUrlPathGenerator = $productUrlPathGenerator; $this->urlRewriteFactory = $urlRewriteFactory; + $this->urlRewritePlaceholder = $urlRewriteFactory->create(); $this->urlRewriteMap = $urlRewriteMap ?: ObjectManager::getInstance()->get(UrlRewriteMap::class); $this->urlRewritesSet = $urlRewritesSet ?: ObjectManager::getInstance()->get(UrlRewritesSet::class); } @@ -76,7 +83,7 @@ public function generate($storeId, Product $product, ObjectRegistry $productCate $currentUrlRewrites = $this->urlRewriteMap->getByIdentifiers( $product->getEntityId(), $storeId, - UrlRewriteMap::ENTITY_TYPE_PRODUCT, + ProductUrlRewriteGenerator::ENTITY_TYPE, $rootCategoryId ); @@ -109,8 +116,8 @@ protected function generateForAutogenerated(UrlRewrite $url, $storeId, $category if ($product->getData('save_rewrites_history')) { $targetPath = $this->productUrlPathGenerator->getUrlPathWithSuffix($product, $storeId, $category); if ($url->getRequestPath() !== $targetPath) { - $generatedUrl = $this->urlRewriteFactory->create() - ->setEntityType(ProductUrlRewriteGenerator::ENTITY_TYPE) + $generatedUrl = clone $this->urlRewritePlaceholder; + $generatedUrl->setEntityType(ProductUrlRewriteGenerator::ENTITY_TYPE) ->setEntityId($product->getEntityId()) ->setRequestPath($url->getRequestPath()) ->setTargetPath($targetPath) @@ -138,8 +145,8 @@ protected function generateForCustom(UrlRewrite $url, $storeId, $category, Produ ? $this->productUrlPathGenerator->getUrlPathWithSuffix($product, $storeId, $category) : $url->getTargetPath(); if ($url->getRequestPath() !== $targetPath) { - $generatedUrl = $this->urlRewriteFactory->create() - ->setEntityType(ProductUrlRewriteGenerator::ENTITY_TYPE) + $generatedUrl = clone $this->urlRewritePlaceholder; + $generatedUrl->setEntityType(ProductUrlRewriteGenerator::ENTITY_TYPE) ->setEntityId($product->getEntityId()) ->setRequestPath($url->getRequestPath()) ->setTargetPath($targetPath) From 7152af74c5a1d497c43d0ba1980c66373666aff9 Mon Sep 17 00:00:00 2001 From: Cristian Partica Date: Thu, 15 Dec 2016 15:20:07 -0600 Subject: [PATCH 11/39] MAGETWO-58924: SQL error wait timeout error when saving categories - code refactor --- .../Model/Map/DataCategoryUrlRewriteMap.php | 37 +++++++++---------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUrlRewriteMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUrlRewriteMap.php index 06092a5b36624..01adfc6d3d5d3 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUrlRewriteMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUrlRewriteMap.php @@ -13,7 +13,6 @@ /** * Map that holds data for category url rewrites entity - * @SuppressWarnings(PHPCPD) */ class DataCategoryUrlRewriteMap implements DataMapInterface { @@ -63,24 +62,6 @@ public function getAllData($categoryId) return $this->tableNames[$categoryId]; } - /** - * Gets data by criteria from a map identified by a category Id - * - * @param int $categoryId - * @param string $key - * @return array - */ - public function getData($categoryId, $key) - { - $this->getAllData($categoryId); - $urlRewritesConnection = $this->connection->getConnection(); - $select = $urlRewritesConnection->select() - ->from(['e' => $this->tableNames[$categoryId]]) - ->where('hash_key = ?', $key); - - return $urlRewritesConnection->fetchAll($select); - } - /** * Queries the database and returns the name of the temporary table where data is stored * @@ -134,4 +115,22 @@ public function resetData($categoryId) $this->tableNames = []; } } + + /** + * Gets data by criteria from a map identified by a category Id + * + * @param int $categoryId + * @param string $key + * @return array + */ + public function getData($categoryId, $key) + { + $this->getAllData($categoryId); + $urlRewritesConnection = $this->connection->getConnection(); + $select = $urlRewritesConnection->select() + ->from(['e' => $this->tableNames[$categoryId]]) + ->where('hash_key = ?', $key); + + return $urlRewritesConnection->fetchAll($select); + } } From 0f70d37d3818ece26bba2f63f98cdde0f7e54657 Mon Sep 17 00:00:00 2001 From: Cristian Partica Date: Thu, 15 Dec 2016 16:29:21 -0600 Subject: [PATCH 12/39] MAGETWO-58924: SQL error wait timeout error when saving categories - cleaning up di.xml --- app/code/Magento/CatalogUrlRewrite/etc/di.xml | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/code/Magento/CatalogUrlRewrite/etc/di.xml b/app/code/Magento/CatalogUrlRewrite/etc/di.xml index 816db53a3f43e..877638026a58a 100644 --- a/app/code/Magento/CatalogUrlRewrite/etc/di.xml +++ b/app/code/Magento/CatalogUrlRewrite/etc/di.xml @@ -6,9 +6,7 @@ */ --> - - Magento\CatalogUrlRewrite\Model\Storage\DbStorage From 7a634376b9caf848f77b3dce3169fe5c946edde8 Mon Sep 17 00:00:00 2001 From: Cristian Partica Date: Mon, 19 Dec 2016 12:39:35 -0600 Subject: [PATCH 13/39] MAGETWO-58924: SQL error wait timeout error when saving categories - unit tests --- .../CurrentUrlRewritesRegenerator.php | 7 +- .../Model/Category/Plugin/Storage.php | 4 +- .../Model/Map/DataCategoryUrlRewriteMap.php | 13 +- .../Model/Map/DataProductMap.php | 1 + .../Product/CurrentUrlRewritesRegenerator.php | 7 +- .../ChildrenUrlRewriteGeneratorTest.php | 40 +++-- .../CurrentUrlRewritesRegeneratorTest.php | 100 +++++------ .../Model/Category/Plugin/StorageTest.php | 29 ++-- ...tegoryBasedProductRewriteGeneratorTest.php | 13 +- .../Model/CategoryUrlRewriteGeneratorTest.php | 79 ++++++--- .../Unit/Model/Map/DataCategoryMapTest.php | 111 +++++++++++++ .../Map/DataCategoryUrlRewriteMapTest.php | 136 +++++++++++++++ .../Map/DataCategoryUsedInProductsMapTest.php | 109 ++++++++++++ .../Test/Unit/Model/Map/DataMapPoolTest.php | 54 ++++++ .../Unit/Model/Map/DataProductMapTest.php | 122 ++++++++++++++ .../Map/DataProductUrlRewriteMapTest.php | 134 +++++++++++++++ .../Test/Unit/Model/Map/UrlRewriteMapTest.php | 156 ++++++++++++++++++ .../CurrentUrlRewritesRegeneratorTest.php | 98 ++++++----- .../ProductScopeRewriteGeneratorTest.php | 31 ++-- .../Model/ProductUrlRewriteGeneratorTest.php | 2 +- .../Observer/AfterImportDataObserverTest.php | 47 +++--- 21 files changed, 1099 insertions(+), 194 deletions(-) create mode 100644 app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryMapTest.php create mode 100644 app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryUrlRewriteMapTest.php create mode 100644 app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryUsedInProductsMapTest.php create mode 100644 app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataMapPoolTest.php create mode 100644 app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataProductMapTest.php create mode 100644 app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataProductUrlRewriteMapTest.php create mode 100644 app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/UrlRewriteMapTest.php diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php index 486c7a7aa9439..becdab0e8c19b 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php @@ -21,10 +21,7 @@ class CurrentUrlRewritesRegenerator /** @var \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator */ protected $categoryUrlPathGenerator; - /** - * @var UrlRewriteFactory - * @deprecated - */ + /** @var UrlRewriteFactory */ protected $urlRewriteFactory; /** @var UrlRewrite */ @@ -112,7 +109,7 @@ protected function generateForAutogenerated($url, $storeId, Category $category) ->setTargetPath($targetPath) ->setRedirectType(OptionProvider::PERMANENT) ->setStoreId($storeId) - ->setIsAutogenerated(0); + ->setIsAutogenerated(1); return [$generatedUrl]; } } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Storage.php b/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Storage.php index d5229da9ec86c..e3916f991d1ba 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Storage.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Storage.php @@ -14,10 +14,10 @@ class Storage { /** @var UrlFinderInterface */ - protected $urlFinder; + private $urlFinder; /** @var Product */ - protected $productResource; + private $productResource; /** * @param UrlFinderInterface $urlFinder diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUrlRewriteMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUrlRewriteMap.php index 01adfc6d3d5d3..de459f9681665 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUrlRewriteMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUrlRewriteMap.php @@ -59,7 +59,7 @@ public function getAllData($categoryId) if (empty($this->tableNames[$categoryId])) { $this->tableNames[$categoryId] = $this->generateData($categoryId); } - return $this->tableNames[$categoryId]; + return $this->getData($categoryId, ''); } /** @@ -125,11 +125,14 @@ public function resetData($categoryId) */ public function getData($categoryId, $key) { - $this->getAllData($categoryId); + if (!isset($this->tableNames[$categoryId])) { + $this->getAllData($categoryId); + } $urlRewritesConnection = $this->connection->getConnection(); - $select = $urlRewritesConnection->select() - ->from(['e' => $this->tableNames[$categoryId]]) - ->where('hash_key = ?', $key); + $select = $urlRewritesConnection->select()->from(['e' => $this->tableNames[$categoryId]]); + if (strlen($key) > 0) { + $select->where('hash_key = ?', $key); + } return $urlRewritesConnection->fetchAll($select); } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductMap.php index af6bc764919cf..315a0e35fd40e 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductMap.php @@ -96,6 +96,7 @@ private function generateData($categoryId) public function resetData($categoryId) { $this->dataMapPool->resetDataMap(DataCategoryMap::class, $categoryId); + unset($this->data[$categoryId]); if (empty($this->data)) { $this->data = []; } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php index 760fa18c7f908..94acb5b592ba2 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php @@ -32,10 +32,7 @@ class CurrentUrlRewritesRegenerator /** @var ProductUrlPathGenerator */ protected $productUrlPathGenerator; - /** - * @var UrlRewriteFactory - * @deprecated - */ + /** @var UrlRewriteFactory */ protected $urlRewriteFactory; /** @var UrlRewrite */ @@ -124,7 +121,7 @@ protected function generateForAutogenerated(UrlRewrite $url, $storeId, $category ->setRedirectType(OptionProvider::PERMANENT) ->setStoreId($storeId) ->setDescription($url->getDescription()) - ->setIsAutogenerated(0) + ->setIsAutogenerated(1) ->setMetadata($url->getMetadata()); return [$generatedUrl]; } diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/ChildrenUrlRewriteGeneratorTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/ChildrenUrlRewriteGeneratorTest.php index 1c0eb6a9e284b..03812de828d52 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/ChildrenUrlRewriteGeneratorTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/ChildrenUrlRewriteGeneratorTest.php @@ -10,19 +10,22 @@ class ChildrenUrlRewriteGeneratorTest extends \PHPUnit_Framework_TestCase { /** @var \Magento\CatalogUrlRewrite\Model\Category\ChildrenUrlRewriteGenerator */ - protected $childrenUrlRewriteGenerator; + private $childrenUrlRewriteGenerator; /** @var \PHPUnit_Framework_MockObject_MockObject */ - protected $category; + private $category; /** @var \PHPUnit_Framework_MockObject_MockObject */ - protected $childrenCategoriesProvider; + private $childrenCategoriesProvider; /** @var \PHPUnit_Framework_MockObject_MockObject */ - protected $categoryUrlRewriteGeneratorFactory; + private $categoryUrlRewriteGeneratorFactory; /** @var \PHPUnit_Framework_MockObject_MockObject */ - protected $categoryUrlRewriteGenerator; + private $categoryUrlRewriteGenerator; + + /** @var \PHPUnit_Framework_MockObject_MockObject */ + private $urlRewritesSet; protected function setUp() { @@ -37,18 +40,21 @@ protected function setUp() $this->categoryUrlRewriteGenerator = $this->getMockBuilder( \Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator::class )->disableOriginalConstructor()->getMock(); + $this->urlRewritesSet = new \Magento\UrlRewrite\Model\UrlRewritesSet; + $this->childrenUrlRewriteGenerator = (new ObjectManager($this))->getObject( \Magento\CatalogUrlRewrite\Model\Category\ChildrenUrlRewriteGenerator::class, [ 'childrenCategoriesProvider' => $this->childrenCategoriesProvider, - 'categoryUrlRewriteGeneratorFactory' => $this->categoryUrlRewriteGeneratorFactory + 'categoryUrlRewriteGeneratorFactory' => $this->categoryUrlRewriteGeneratorFactory, + 'urlRewritesSet' => $this->urlRewritesSet ] ); } public function testNoChildrenCategories() { - $this->childrenCategoriesProvider->expects($this->once())->method('getChildren')->with($this->category, false) + $this->childrenCategoriesProvider->expects($this->once())->method('getChildren')->with($this->category, true) ->will($this->returnValue([])); $this->assertEquals([], $this->childrenUrlRewriteGenerator->generate('store_id', $this->category)); @@ -64,18 +70,28 @@ public function testGenerate() $childCategory->expects($this->once())->method('setStoreId')->with($storeId); $childCategory->expects($this->once())->method('setData') ->with('save_rewrites_history', $saveRewritesHistory); - $this->childrenCategoriesProvider->expects($this->once())->method('getChildren')->with($this->category, false) + $this->childrenCategoriesProvider->expects($this->once())->method('getChildren')->with($this->category, true) ->will($this->returnValue([$childCategory])); $this->category->expects($this->any())->method('getData')->with('save_rewrites_history') ->will($this->returnValue($saveRewritesHistory)); $this->categoryUrlRewriteGeneratorFactory->expects($this->once())->method('create') ->will($this->returnValue($this->categoryUrlRewriteGenerator)); - $this->categoryUrlRewriteGenerator->expects($this->once())->method('generate')->with($childCategory) - ->will($this->returnValue([['url-1', 'url-2']])); + $url1 = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite(); + $url1->setRequestPath('category-1') + ->setStoreId(1); + $url2 = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite(); + $url2->setRequestPath('category-2') + ->setStoreId(2); + $url3 = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite(); + $url3->setRequestPath('category-1') + ->setStoreId(1); + $this->categoryUrlRewriteGenerator->expects($this->once())->method('generate') + ->with($childCategory, false, 1) + ->will($this->returnValue([$url1, $url2, $url3])); $this->assertEquals( - [['url-1', 'url-2']], - $this->childrenUrlRewriteGenerator->generate($storeId, $this->category) + ['category-1_1' => $url1, 'category-2_2' => $url2], + $this->childrenUrlRewriteGenerator->generate($storeId, $this->category, 1) ); } } diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/CurrentUrlRewritesRegeneratorTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/CurrentUrlRewritesRegeneratorTest.php index e786f970aac48..ad16a9885282c 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/CurrentUrlRewritesRegeneratorTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/CurrentUrlRewritesRegeneratorTest.php @@ -14,25 +14,25 @@ class CurrentUrlRewritesRegeneratorTest extends \PHPUnit_Framework_TestCase { /** @var \Magento\CatalogUrlRewrite\Model\Category\CurrentUrlRewritesRegenerator */ - protected $currentUrlRewritesRegenerator; - - /** @var \PHPUnit_Framework_MockObject_MockObject */ - protected $filter; - - /** @var \Magento\UrlRewrite\Model\UrlFinderInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $urlFinder; + private $currentUrlRewritesRegenerator; /** @var \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator|\PHPUnit_Framework_MockObject_MockObject */ - protected $categoryUrlPathGenerator; + private $categoryUrlPathGenerator; /** @var \Magento\Catalog\Model\Category|\PHPUnit_Framework_MockObject_MockObject */ - protected $category; + private $category; /** @var \Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory|\PHPUnit_Framework_MockObject_MockObject */ - protected $urlRewriteFactory; + private $urlRewriteFactory; /** @var \Magento\UrlRewrite\Service\V1\Data\UrlRewrite|\PHPUnit_Framework_MockObject_MockObject */ - protected $urlRewrite; + private $urlRewrite; + + /** @var \PHPUnit_Framework_MockObject_MockObject */ + private $urlRewritesSet; + + /** @var \PHPUnit_Framework_MockObject_MockObject */ + private $urlRewriteMap; protected function setUp() { @@ -43,41 +43,42 @@ protected function setUp() ->disableOriginalConstructor()->getMock(); $this->category = $this->getMockBuilder(\Magento\Catalog\Model\Category::class) ->disableOriginalConstructor()->getMock(); - $this->filter = $this->getMockBuilder(\Magento\UrlRewrite\Service\V1\Data\Filter::class) - ->disableOriginalConstructor()->getMock(); - $this->filter->expects($this->any())->method('setStoreId')->will($this->returnSelf()); - $this->filter->expects($this->any())->method('setEntityId')->will($this->returnSelf()); - $this->urlFinder = $this->getMockBuilder(\Magento\UrlRewrite\Model\UrlFinderInterface::class) - ->disableOriginalConstructor()->getMock(); - $this->categoryUrlPathGenerator = $this->getMockBuilder( + $this->categoryUrlPathGenerator = $this->getMockBuilder( \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator::class )->disableOriginalConstructor()->getMock(); - $this->currentUrlRewritesRegenerator = (new ObjectManager($this))->getObject( + $this->urlRewriteMap = $this->getMockBuilder(\Magento\CatalogUrlRewrite\Model\Map\UrlRewriteMap::class) + ->disableOriginalConstructor()->getMock(); + $this->urlRewriteFactory->expects($this->once())->method('create') + ->willReturn($this->urlRewrite); + + $this->urlRewritesSet = new \Magento\UrlRewrite\Model\UrlRewritesSet; + $this->currentUrlRewritesRegenerator = (new ObjectManager($this))->getObject( \Magento\CatalogUrlRewrite\Model\Category\CurrentUrlRewritesRegenerator::class, [ - 'urlFinder' => $this->urlFinder, 'categoryUrlPathGenerator' => $this->categoryUrlPathGenerator, - 'urlRewriteFactory' => $this->urlRewriteFactory + 'urlRewriteFactory' => $this->urlRewriteFactory, + 'urlRewritesSet' => $this->urlRewritesSet, + 'urlRewriteMap' => $this->urlRewriteMap ] ); } public function testIsAutogeneratedWithoutSaveRewriteHistory() { - $this->urlFinder->expects($this->once())->method('findAllByData') + $this->urlRewriteMap->expects($this->once())->method('getByIdentifiers') ->will($this->returnValue($this->getCurrentRewritesMocks([[UrlRewrite::IS_AUTOGENERATED => 1]]))); $this->category->expects($this->once())->method('getData')->with('save_rewrites_history') ->will($this->returnValue(false)); $this->assertEquals( [], - $this->currentUrlRewritesRegenerator->generate('store_id', $this->category) + $this->currentUrlRewritesRegenerator->generate('store_id', $this->category, $this->category) ); } public function testSkipGenerationForAutogenerated() { - $this->urlFinder->expects($this->once())->method('findAllByData') + $this->urlRewriteMap->expects($this->once())->method('getByIdentifiers') ->will( $this->returnValue( $this->getCurrentRewritesMocks( @@ -94,7 +95,7 @@ public function testSkipGenerationForAutogenerated() $this->assertEquals( [], - $this->currentUrlRewritesRegenerator->generate('store_id', $this->category) + $this->currentUrlRewritesRegenerator->generate('store_id', $this->category, $this->category) ); } @@ -104,7 +105,7 @@ public function testIsAutogenerated() $targetPath = 'some-path.html'; $storeId = 2; $categoryId = 12; - $this->urlFinder->expects($this->once())->method('findAllByData') + $this->urlRewriteMap->expects($this->once())->method('getByIdentifiers') ->will( $this->returnValue( $this->getCurrentRewritesMocks( @@ -120,23 +121,24 @@ public function testIsAutogenerated() ) ) ); - $this->category->expects($this->any())->method('getId')->will($this->returnValue($categoryId)); + + $this->category->expects($this->any())->method('getEntityId')->will($this->returnValue($categoryId)); $this->category->expects($this->once())->method('getData')->with('save_rewrites_history') ->will($this->returnValue(true)); $this->categoryUrlPathGenerator->expects($this->once())->method('getUrlPathWithSuffix') ->will($this->returnValue($targetPath)); - $this->prepareUrlRewriteMock($storeId, $categoryId, $requestPath, $targetPath, OptionProvider::PERMANENT); + $this->prepareUrlRewriteMock($storeId, $categoryId, $requestPath, $targetPath, OptionProvider::PERMANENT, 1); $this->assertEquals( ['autogenerated.html_2' => $this->urlRewrite], - $this->currentUrlRewritesRegenerator->generate($storeId, $this->category) + $this->currentUrlRewritesRegenerator->generate($storeId, $this->category, $this->category) ); } public function testSkipGenerationForCustom() { - $this->urlFinder->expects($this->once())->method('findAllByData') + $this->urlRewriteMap->expects($this->once())->method('getByIdentifiers') ->will( $this->returnValue( $this->getCurrentRewritesMocks( @@ -155,7 +157,7 @@ public function testSkipGenerationForCustom() $this->assertEquals( [], - $this->currentUrlRewritesRegenerator->generate('store_id', $this->category) + $this->currentUrlRewritesRegenerator->generate('store_id', $this->category, $this->category) ); } @@ -166,7 +168,7 @@ public function testGenerationForCustomWithoutTargetPathGeneration() $requestPath = 'generate-for-custom-without-redirect-type.html'; $targetPath = 'custom-target-path.html'; $description = 'description'; - $this->urlFinder->expects($this->once())->method('findAllByData') + $this->urlRewriteMap->expects($this->once())->method('getByIdentifiers') ->will( $this->returnValue( $this->getCurrentRewritesMocks( @@ -184,16 +186,14 @@ public function testGenerationForCustomWithoutTargetPathGeneration() ) ); $this->categoryUrlPathGenerator->expects($this->never())->method('getUrlPathWithSuffix'); - $this->category->expects($this->any())->method('getId')->will($this->returnValue($categoryId)); + $this->category->expects($this->any())->method('getEntityId')->will($this->returnValue($categoryId)); $this->urlRewrite->expects($this->once())->method('setDescription')->with($description) ->will($this->returnSelf()); - $this->urlRewriteFactory->expects($this->once())->method('create') - ->willReturn($this->urlRewrite); - $this->prepareUrlRewriteMock($storeId, $categoryId, $requestPath, $targetPath, 0); + $this->prepareUrlRewriteMock($storeId, $categoryId, $requestPath, $targetPath, 0, 0); $this->assertEquals( ['generate-for-custom-without-redirect-type.html_12' => $this->urlRewrite], - $this->currentUrlRewritesRegenerator->generate($storeId, $this->category) + $this->currentUrlRewritesRegenerator->generate($storeId, $this->category, $this->category) ); } @@ -204,7 +204,7 @@ public function testGenerationForCustomWithTargetPathGeneration() $requestPath = 'generate-for-custom-without-redirect-type.html'; $targetPath = 'generated-target-path.html'; $description = 'description'; - $this->urlFinder->expects($this->once())->method('findAllByData') + $this->urlRewriteMap->expects($this->once())->method('getByIdentifiers') ->will( $this->returnValue( $this->getCurrentRewritesMocks( @@ -223,16 +223,14 @@ public function testGenerationForCustomWithTargetPathGeneration() ); $this->categoryUrlPathGenerator->expects($this->any())->method('getUrlPathWithSuffix') ->will($this->returnValue($targetPath)); - $this->category->expects($this->any())->method('getId')->will($this->returnValue($categoryId)); + $this->category->expects($this->any())->method('getEntityId')->will($this->returnValue($categoryId)); $this->urlRewrite->expects($this->once())->method('setDescription')->with($description) ->will($this->returnSelf()); - $this->urlRewriteFactory->expects($this->once())->method('create') - ->willReturn($this->urlRewrite); - $this->prepareUrlRewriteMock($storeId, $categoryId, $requestPath, $targetPath, 'code'); + $this->prepareUrlRewriteMock($storeId, $categoryId, $requestPath, $targetPath, 'code', 0); $this->assertEquals( ['generate-for-custom-without-redirect-type.html_12' => $this->urlRewrite], - $this->currentUrlRewritesRegenerator->generate($storeId, $this->category) + $this->currentUrlRewritesRegenerator->generate($storeId, $this->category, $this->category) ); } @@ -263,9 +261,16 @@ protected function getCurrentRewritesMocks($currentRewrites) * @param mixed $requestPath * @param mixed $targetPath * @param mixed $redirectType + * @param int $isAutoGenerated */ - protected function prepareUrlRewriteMock($storeId, $categoryId, $requestPath, $targetPath, $redirectType) - { + protected function prepareUrlRewriteMock( + $storeId, + $categoryId, + $requestPath, + $targetPath, + $redirectType, + $isAutoGenerated + ) { $this->urlRewrite->expects($this->any())->method('setStoreId')->with($storeId) ->will($this->returnSelf()); $this->urlRewrite->expects($this->any())->method('setEntityId')->with($categoryId) @@ -276,11 +281,14 @@ protected function prepareUrlRewriteMock($storeId, $categoryId, $requestPath, $t ->will($this->returnSelf()); $this->urlRewrite->expects($this->any())->method('setTargetPath')->with($targetPath) ->will($this->returnSelf()); - $this->urlRewrite->expects($this->any())->method('setIsAutogenerated')->with(0) + $this->urlRewrite->expects($this->any())->method('setIsAutogenerated')->with($isAutoGenerated) ->will($this->returnSelf()); $this->urlRewrite->expects($this->any())->method('setRedirectType')->with($redirectType) ->will($this->returnSelf()); $this->urlRewrite->expects($this->any())->method('setMetadata')->with([])->will($this->returnSelf()); + $this->urlRewrite->expects($this->any())->method('getTargetPath')->willReturn($targetPath); + $this->urlRewrite->expects($this->any())->method('getRequestPath')->willReturn($requestPath); + $this->urlRewrite->expects($this->any())->method('getStoreId')->willReturn($storeId); $this->urlRewriteFactory->expects($this->any())->method('create')->will($this->returnValue($this->urlRewrite)); } } diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/Plugin/StorageTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/Plugin/StorageTest.php index 21ab961dcbdcb..a9f20544c1008 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/Plugin/StorageTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/Plugin/StorageTest.php @@ -6,7 +6,6 @@ namespace Magento\CatalogUrlRewrite\Test\Unit\Model\Category\Plugin; use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; -use Magento\CatalogUrlRewrite\Model\Category\ProductFactory; use Magento\UrlRewrite\Model\StorageInterface; use Magento\CatalogUrlRewrite\Model\Category\Plugin\Storage as CategoryStoragePlugin; use Magento\UrlRewrite\Service\V1\Data\UrlRewrite; @@ -24,11 +23,6 @@ class StorageTest extends \PHPUnit_Framework_TestCase */ private $plugin; - /** - * @var ProductFactory|\PHPUnit_Framework_MockObject_MockObject - */ - private $productFactory; - /** * @var UrlFinderInterface|\PHPUnit_Framework_MockObject_MockObject */ @@ -56,10 +50,6 @@ class StorageTest extends \PHPUnit_Framework_TestCase protected function setUp() { - $this->productFactory = $this->getMockBuilder(ProductFactory::class) - ->disableOriginalConstructor() - ->setMethods(['create']) - ->getMock(); $this->storage = $this->getMockBuilder(StorageInterface::class) ->getMockForAbstractClass(); $this->urlFinder = $this->getMockBuilder(UrlFinderInterface::class) @@ -78,12 +68,15 @@ protected function setUp() $this->plugin = (new ObjectManager($this))->getObject( CategoryStoragePlugin::class, [ - 'productFactory' => $this->productFactory, - 'urlFinder' => $this->urlFinder + 'urlFinder' => $this->urlFinder, + 'productResource' => $this->productResourceModel ] ); } + /** + * test AfterReplace method + */ public function testAfterReplace() { $this->urlRewrite->expects(static::any())->method('getMetadata')->willReturn(['category_id' => '5']); @@ -97,10 +90,18 @@ public function testAfterReplace() $this->urlFinder->expects(static::once())->method('findAllByData')->willReturn([$this->urlRewrite]); - $this->productFactory->expects(static::once())->method('create')->willReturn($this->product); - $this->product->expects(static::once())->method('getResource')->willReturn($this->productResourceModel); $this->productResourceModel->expects(static::once())->method('saveMultiple')->willReturnSelf(); $this->plugin->afterReplace($this->storage, null, $productUrls); } + + /** + * test BeforeDeleteByData method + */ + public function testBeforeDeleteByData() + { + $data = [1, 2, 3]; + $this->productResourceModel->expects(static::once())->method('removeMultipleByFilter')->with($data)->willReturnSelf(); + $this->plugin->beforeDeleteByData($this->storage, $data); + } } diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/CategoryBasedProductRewriteGeneratorTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/CategoryBasedProductRewriteGeneratorTest.php index c62475f3cc7cf..f3d1cb3a1471d 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/CategoryBasedProductRewriteGeneratorTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/CategoryBasedProductRewriteGeneratorTest.php @@ -12,12 +12,11 @@ /** * Class CategoryBasedProductRewriteGeneratorTest - * @package Magento\CatalogUrlRewrite\Test\Unit\Model */ class CategoryBasedProductRewriteGeneratorTest extends \PHPUnit_Framework_TestCase { /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var ProductScopeRewriteGenerator|\PHPUnit_Framework_MockObject_MockObject */ private $productScopeRewriteGeneratorMock; @@ -46,6 +45,7 @@ public function testGenerationWithGlobalScope() ->disableOriginalConstructor() ->getMock(); $storeId = 1; + $categoryId = 1; $urls = ['dummy-url.html']; $productMock->expects($this->once()) @@ -60,10 +60,10 @@ public function testGenerationWithGlobalScope() ->willReturn(true); $this->productScopeRewriteGeneratorMock->expects($this->once()) ->method('generateForGlobalScope') - ->with([$categoryMock], $productMock) + ->with([$categoryMock], $productMock, $categoryId) ->willReturn($urls); - $this->assertEquals($urls, $this->generator->generate($productMock, $categoryMock)); + $this->assertEquals($urls, $this->generator->generate($productMock, $categoryMock, $categoryId)); } public function testGenerationWithSpecificStore() @@ -75,6 +75,7 @@ public function testGenerationWithSpecificStore() ->disableOriginalConstructor() ->getMock(); $storeId = 1; + $categoryId = 1; $urls = ['dummy-url.html']; $productMock->expects($this->once()) @@ -89,9 +90,9 @@ public function testGenerationWithSpecificStore() ->willReturn(false); $this->productScopeRewriteGeneratorMock->expects($this->once()) ->method('generateForSpecificStoreView') - ->with($storeId, [$categoryMock], $productMock) + ->with($storeId, [$categoryMock], $productMock, $categoryId) ->willReturn($urls); - $this->assertEquals($urls, $this->generator->generate($productMock, $categoryMock)); + $this->assertEquals($urls, $this->generator->generate($productMock, $categoryMock, $categoryId)); } } diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/CategoryUrlRewriteGeneratorTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/CategoryUrlRewriteGeneratorTest.php index dca265754b5e6..e6c035334c808 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/CategoryUrlRewriteGeneratorTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/CategoryUrlRewriteGeneratorTest.php @@ -14,46 +14,50 @@ class CategoryUrlRewriteGeneratorTest extends \PHPUnit_Framework_TestCase { /** @var \PHPUnit_Framework_MockObject_MockObject */ - protected $canonicalUrlRewriteGenerator; + private $canonicalUrlRewriteGenerator; /** @var \PHPUnit_Framework_MockObject_MockObject */ - protected $currentUrlRewritesRegenerator; + private $currentUrlRewritesRegenerator; /** @var \PHPUnit_Framework_MockObject_MockObject */ - protected $childrenUrlRewriteGenerator; + private $childrenUrlRewriteGenerator; /** @var \Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator */ - protected $categoryUrlRewriteGenerator; + private $categoryUrlRewriteGenerator; /** @var \Magento\CatalogUrlRewrite\Service\V1\StoreViewService|\PHPUnit_Framework_MockObject_MockObject */ - protected $storeViewService; + private $storeViewService; /** @var \Magento\Catalog\Model\Category|\PHPUnit_Framework_MockObject_MockObject */ - protected $category; + private $category; /** @var \Magento\Catalog\Api\CategoryRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $categoryRepository; + private $categoryRepository; + + /** @var \PHPUnit_Framework_MockObject_MockObject */ + private $urlRewritesSet; /** * Test method */ protected function setUp() { - $this->currentUrlRewritesRegenerator = $this->getMockBuilder( + $this->currentUrlRewritesRegenerator = $this->getMockBuilder( \Magento\CatalogUrlRewrite\Model\Category\CurrentUrlRewritesRegenerator::class )->disableOriginalConstructor()->getMock(); - $this->canonicalUrlRewriteGenerator = $this->getMockBuilder( + $this->canonicalUrlRewriteGenerator = $this->getMockBuilder( \Magento\CatalogUrlRewrite\Model\Category\CanonicalUrlRewriteGenerator::class )->disableOriginalConstructor()->getMock(); - $this->childrenUrlRewriteGenerator = $this->getMockBuilder( + $this->childrenUrlRewriteGenerator = $this->getMockBuilder( \Magento\CatalogUrlRewrite\Model\Category\ChildrenUrlRewriteGenerator::class )->disableOriginalConstructor()->getMock(); $this->storeViewService = $this->getMockBuilder(\Magento\CatalogUrlRewrite\Service\V1\StoreViewService::class) ->disableOriginalConstructor()->getMock(); $this->category = $this->getMock(\Magento\Catalog\Model\Category::class, [], [], '', false); $this->categoryRepository = $this->getMock(\Magento\Catalog\Api\CategoryRepositoryInterface::class); + $this->urlRewritesSet = new \Magento\UrlRewrite\Model\UrlRewritesSet; - $this->categoryUrlRewriteGenerator = (new ObjectManager($this))->getObject( + $this->categoryUrlRewriteGenerator = (new ObjectManager($this))->getObject( \Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator::class, [ 'canonicalUrlRewriteGenerator' => $this->canonicalUrlRewriteGenerator, @@ -61,6 +65,7 @@ protected function setUp() 'currentUrlRewritesRegenerator' => $this->currentUrlRewritesRegenerator, 'storeViewService' => $this->storeViewService, 'categoryRepository' => $this->categoryRepository, + 'urlRewritesSet' => $this->urlRewritesSet ] ); } @@ -70,26 +75,32 @@ protected function setUp() */ public function testGenerationForGlobalScope() { + $categoryId = 1; $this->category->expects($this->any())->method('getStoreId')->will($this->returnValue(null)); $this->category->expects($this->any())->method('getStoreIds')->will($this->returnValue([1])); $this->storeViewService->expects($this->once())->method('doesEntityHaveOverriddenUrlKeyForStore') ->will($this->returnValue(false)); $canonical = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite(); - $canonical->setTargetPath('category-1') + $canonical->setRequestPath('category-1') ->setStoreId(1); $this->canonicalUrlRewriteGenerator->expects($this->any())->method('generate') - ->will($this->returnValue([$canonical])); - $children = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite(); - $children->setTargetPath('category-2') + ->will($this->returnValue(['category-1' => $canonical])); + $children1 = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite(); + $children1->setRequestPath('category-2') + ->setStoreId(2); + $children2 = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite(); + $children2->setRequestPath('category-22') ->setStoreId(2); $this->childrenUrlRewriteGenerator->expects($this->any())->method('generate') - ->will($this->returnValue([$children])); + ->with(1, $this->category, $categoryId) + ->will($this->returnValue(['category-2' => $children1, 'category-1' => $children2])); $current = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite(); - $current->setTargetPath('category-3') + $current->setRequestPath('category-3') ->setStoreId(3); $this->currentUrlRewritesRegenerator->expects($this->any())->method('generate') - ->will($this->returnValue([$current])); - $categoryForSpecificStore = $this->getMock( + ->with(1, $this->category, $categoryId) + ->will($this->returnValue(['category-3' => $current])); + $categoryForSpecificStore = $this->getMock( \Magento\Catalog\Model\Category::class, ['getUrlKey', 'getUrlPath'], [], @@ -99,8 +110,13 @@ public function testGenerationForGlobalScope() $this->categoryRepository->expects($this->once())->method('get')->willReturn($categoryForSpecificStore); $this->assertEquals( - [$canonical, $children, $current], - $this->categoryUrlRewriteGenerator->generate($this->category) + [ + 'category-1_1' => $canonical, + 'category-2_2' => $children1, + 'category-22_2' => $children2, + 'category-3_3' => $current + ], + $this->categoryUrlRewriteGenerator->generate($this->category, false, $categoryId) ); } @@ -112,7 +128,7 @@ public function testGenerationForSpecificStore() $this->category->expects($this->any())->method('getStoreId')->will($this->returnValue(1)); $this->category->expects($this->never())->method('getStoreIds'); $canonical = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite(); - $canonical->setTargetPath('category-1') + $canonical->setRequestPath('category-1') ->setStoreId(1); $this->canonicalUrlRewriteGenerator->expects($this->any())->method('generate') ->will($this->returnValue([$canonical])); @@ -121,7 +137,10 @@ public function testGenerationForSpecificStore() $this->currentUrlRewritesRegenerator->expects($this->any())->method('generate') ->will($this->returnValue([])); - $this->assertEquals([$canonical], $this->categoryUrlRewriteGenerator->generate($this->category)); + $this->assertEquals( + ['category-1_1' => $canonical], + $this->categoryUrlRewriteGenerator->generate($this->category, 1) + ); } /** @@ -135,4 +154,18 @@ public function testSkipGenerationForGlobalScope() $this->assertEquals([], $this->categoryUrlRewriteGenerator->generate($this->category)); } + + /** + * Test method + */ + public function testSkipGenerationForGlobalScopeWithCategory() + { + $this->category->expects($this->any())->method('getStoreIds')->will($this->returnValue([1, 2])); + $this->category->expects($this->any())->method('getEntityId')->will($this->returnValue(1)); + $this->category->expects($this->any())->method('getStoreId')->will($this->returnValue(false)); + $this->storeViewService->expects($this->exactly(2))->method('doesEntityHaveOverriddenUrlKeyForStore') + ->will($this->returnValue(true)); + + $this->assertEquals([], $this->categoryUrlRewriteGenerator->generate($this->category, false, 1)); + } } diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryMapTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryMapTest.php new file mode 100644 index 0000000000000..19b4b837c9bfc --- /dev/null +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryMapTest.php @@ -0,0 +1,111 @@ +categoryRepository = $this->getMock(CategoryRepository::class, [], [], '', false); + $this->collection = $this->getMock(Collection::class, ['addIdFilter', 'getAllIds'], [], '', false); + $this->categoryResource = $this->getMock( + CategoryResource::class, + ['getConnection', 'getEntityTable'], + [], + '', + false + ); + + $this->model = (new ObjectManager($this))->getObject( + DataCategoryMap::class, + [ + 'categoryRepository' => $this->categoryRepository, + 'collection' => $this->collection, + 'categoryResource' => $this->categoryResource + ] + ); + } + + /** + * Tests getAllData, getData and resetData functionality + */ + public function testGetAllData() + { + $categoryIds = ['1' => [1, 2, 3], '2' => [2, 3], '3' => 3]; + $categoryIdsOther = ['2' => [2, 3, 4]]; + + $categoryMock = $this->getMock(CategoryInterface::class, [], [], '', false); + $connectionAdapterMock = $this->getMock(AdapterInterface::class); + $selectMock = $this->getMock(Select::class, [], [], '', false); + + $this->categoryRepository->expects($this->any()) + ->method('get') + ->willReturn($categoryMock); + $categoryMock->expects($this->any()) + ->method('getResourceCollection') + ->willReturn($this->collection); + $this->collection->expects($this->any()) + ->method('addIdFilter') + ->willReturnSelf(); + $this->collection->expects($this->exactly(3)) + ->method('getAllIds') + ->willReturnOnConsecutiveCalls($categoryIds, $categoryIdsOther, $categoryIds); + $categoryMock->expects($this->any()) + ->method('getResource') + ->willReturn($this->categoryResource); + $this->categoryResource->expects($this->any()) + ->method('getConnection') + ->willReturn($connectionAdapterMock); + $this->categoryResource->expects($this->any()) + ->method('getEntityTable') + ->willReturn('category_entity'); + $connectionAdapterMock->expects($this->any()) + ->method('select') + ->willReturn($selectMock); + $selectMock->expects($this->any()) + ->method('from') + ->willReturnSelf(); + $selectMock->expects($this->any()) + ->method('where') + ->willReturnSelf(); + $connectionAdapterMock->expects($this->any()) + ->method('fetchCol') + ->willReturnOnConsecutiveCalls($categoryIds, $categoryIdsOther); + + $this->assertEquals($categoryIds, $this->model->getAllData(1)); + $this->assertEquals($categoryIds[2], $this->model->getData(1, 2)); + $this->assertEquals($categoryIdsOther, $this->model->getAllData(2)); + $this->assertEquals($categoryIdsOther[2], $this->model->getData(2, 2)); + $this->model->resetData(1); + $this->assertEquals($categoryIds[2], $this->model->getData(1, 2)); + $this->assertEquals($categoryIds, $this->model->getAllData(1)); + } +} diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryUrlRewriteMapTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryUrlRewriteMapTest.php new file mode 100644 index 0000000000000..68c97572776f7 --- /dev/null +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryUrlRewriteMapTest.php @@ -0,0 +1,136 @@ +dataMapPoolMock = $this->getMock(DataMapPoolInterface::class); + $this->dataCategoryMapMock = $this->getMock(DataProductMap::class, [], [], '', false); + $this->dataCategoryUsedInProductsMapMock = $this->getMock(DataCategoryUsedInProductsMap::class, [], [], '', false); + $this->temporaryTableServiceMock = $this->getMock(TemporaryTableService::class, [], [], '', false); + $this->urlRewriteFactoryMock = $this->getMock(UrlRewriteFactory::class, ['create'], [], '', false); + $this->urlRewritePlaceholderMock = $this->getMock(UrlRewrite::class, [], [], '', false); + $this->connectionMock = $this->getMock(ResourceConnection::class, [], [], '', false); + + $this->dataMapPoolMock->expects($this->any()) + ->method('getDataMap') + ->willReturnOnConsecutiveCalls($this->dataCategoryUsedInProductsMapMock, $this->dataCategoryMapMock); + + $this->urlRewriteFactoryMock->expects($this->any()) + ->method('create') + ->willReturn($this->urlRewritePlaceholderMock); + + $this->model = (new ObjectManager($this))->getObject( + DataCategoryUrlRewriteMap::class, + [ + 'connection' => $this->connectionMock, + 'dataMapPool' => $this->dataMapPoolMock, + 'temporaryTableService' => $this->temporaryTableServiceMock, + 'urlRewriteFactory' => $this->urlRewriteFactoryMock, + 'mapData' => [], + ] + ); + } + + /** + * Tests getAllData, getData and resetData functionality + */ + public function testGetAllData() + { + $productStoreIds = [ + '1' => ['store_id' => 1, 'category_id' => 1], + '2' => ['store_id' => 2, 'category_id' => 1], + '3' => ['store_id' => 3, 'category_id' => 1], + '4' => ['store_id' => 1, 'category_id' => 2], + '5' => ['store_id' => 2, 'category_id' => 2], + ]; + + $connectionMock = $this->getMock(AdapterInterface::class); + $selectMock = $this->getMock(Select::class, [], [], '', false); + + $this->connectionMock->expects($this->any()) + ->method('getConnection') + ->willReturn($connectionMock); + $connectionMock->expects($this->any()) + ->method('select') + ->willReturn($selectMock); + $connectionMock->expects($this->any()) + ->method('fetchAll') + ->willReturnOnConsecutiveCalls($productStoreIds, $productStoreIds[3]); + $selectMock->expects($this->any()) + ->method('from') + ->willReturnSelf(); + $selectMock->expects($this->any()) + ->method('joinInner') + ->willReturnSelf(); + $selectMock->expects($this->any()) + ->method('where') + ->willReturnSelf(); + $this->dataCategoryMapMock->expects($this->once()) + ->method('getAllData') + ->willReturn([]); + $this->dataCategoryUsedInProductsMapMock->expects($this->once()) + ->method('getAllData') + ->willReturn([]); + $this->temporaryTableServiceMock->expects($this->any()) + ->method('createFromSelect') + ->withConsecutive( + $selectMock, + $connectionMock, + [ + 'PRIMARY' => ['url_rewrite_id'], + 'HASHKEY_ENTITY_STORE' => ['hash_key'], + 'ENTITY_STORE' => ['entity_id', 'store_id'] + ] + ) + ->willReturn('tempTableName'); + + $this->assertEquals($productStoreIds, $this->model->getAllData(1)); + $this->assertEquals($productStoreIds[3], $this->model->getData(1, '3_1')); + } +} diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryUsedInProductsMapTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryUsedInProductsMapTest.php new file mode 100644 index 0000000000000..2ceeeab97e068 --- /dev/null +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryUsedInProductsMapTest.php @@ -0,0 +1,109 @@ +dataMapPoolMock = $this->getMock(DataMapPoolInterface::class); + $this->dataCategoryMapMock = $this->getMock(DataCategoryMap::class, [], [], '', false); + $this->dataProductMapMock = $this->getMock(DataProductMap::class, [], [], '', false); + $this->connectionMock = $this->getMock(ResourceConnection::class, [], [], '', false); + + $this->dataMapPoolMock->expects($this->any()) + ->method('getDataMap') + ->willReturnOnConsecutiveCalls( + $this->dataProductMapMock, + $this->dataCategoryMapMock, + $this->dataProductMapMock, + $this->dataCategoryMapMock, + $this->dataProductMapMock, + $this->dataCategoryMapMock + ); + + $this->model = (new ObjectManager($this))->getObject( + DataCategoryUsedInProductsMap::class, + [ + 'connection' => $this->connectionMock, + 'dataMapPool' => $this->dataMapPoolMock, + 'mapData' => [], + ] + ); + } + + /** + * Tests getAllData, getData and resetData functionality + */ + public function testGetAllData() + { + $categoryIds = ['1' => [1, 2, 3], '2' => [2, 3], '3' => 3]; + $categoryIdsOther = ['2' => [2, 3, 4]]; + + $connectionMock = $this->getMock(AdapterInterface::class); + $selectMock = $this->getMock(Select::class, [], [], '', false); + + $this->connectionMock->expects($this->any()) + ->method('getConnection') + ->willReturn($connectionMock); + $connectionMock->expects($this->any()) + ->method('select') + ->willReturn($selectMock); + $connectionMock->expects($this->any()) + ->method('fetchCol') + ->willReturnOnConsecutiveCalls($categoryIds, $categoryIdsOther, $categoryIds); + $selectMock->expects($this->any()) + ->method('from') + ->willReturnSelf(); + $selectMock->expects($this->any()) + ->method('joinInner') + ->willReturnSelf(); + $selectMock->expects($this->any()) + ->method('where') + ->willReturnSelf(); + $this->dataMapPoolMock->expects($this->at(4)) + ->method('resetDataMap') + ->with(DataProductMap::class, 1); + $this->dataMapPoolMock->expects($this->at(5)) + ->method('resetDataMap') + ->with(DataCategoryMap::class, 1); + + $this->assertEquals($categoryIds, $this->model->getAllData(1)); + $this->assertEquals($categoryIds[2], $this->model->getData(1, 2)); + $this->assertEquals($categoryIdsOther, $this->model->getAllData(2)); + $this->assertEquals($categoryIdsOther[2], $this->model->getData(2, 2)); + $this->model->resetData(1); + $this->assertEquals($categoryIds[2], $this->model->getData(1, 2)); + $this->assertEquals($categoryIds, $this->model->getAllData(1)); + } +} diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataMapPoolTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataMapPoolTest.php new file mode 100644 index 0000000000000..e76f42efd100c --- /dev/null +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataMapPoolTest.php @@ -0,0 +1,54 @@ +objectManagerMock = $this->getMock(ObjectManagerInterface::class); + + $this->model = (new ObjectManager($this))->getObject( + DataMapPool::class, + [ + 'objectManager' => $this->objectManagerMock, + ] + ); + } + + /** + * Tests getDataMap + */ + public function testGetDataMap() + { + $dataCategoryMapMock = $this->getMock(DataCategoryMap::class, [], [], '', false); + $dataProductMapMock = $this->getMock(DataProductMap::class, [], [], '', false); + $dataProductMapMockOtherCategory = $this->getMock(DataProductMap::class, [], [], '', false); + + $this->objectManagerMock->expects($this->any()) + ->method('create') + ->willReturnOnConsecutiveCalls($dataCategoryMapMock, $dataProductMapMock, $dataProductMapMockOtherCategory); + $this->assertEquals($dataCategoryMapMock, $this->model->getDataMap(DataCategoryMap::class, 1)); + $this->assertEquals($dataCategoryMapMock, $this->model->getDataMap(DataCategoryMap::class, 1)); + $this->assertEquals($dataProductMapMock, $this->model->getDataMap(DataProductMap::class, 1)); + $this->assertEquals($dataProductMapMockOtherCategory, $this->model->getDataMap(DataCategoryMap::class, 2)); + } +} diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataProductMapTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataProductMapTest.php new file mode 100644 index 0000000000000..d0418763223a2 --- /dev/null +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataProductMapTest.php @@ -0,0 +1,122 @@ +dataMapPoolMock = $this->getMock(DataMapPoolInterface::class); + $this->dataCategoryMapMock = $this->getMock(DataCategoryMap::class, [], [], '', false); + $this->collectionFactoryMock = $this->getMock(CollectionFactory::class, ['create'], [], '', false); + $this->productCollectionMock = $this->getMock( + ProductCollection::class, + ['getSelect', 'getConnection', 'getAllIds'], + [], + '', + false + ); + + $this->collectionFactoryMock->expects($this->any()) + ->method('create') + ->willReturn($this->productCollectionMock); + + $this->dataMapPoolMock->expects($this->any()) + ->method('getDataMap') + ->willReturn($this->dataCategoryMapMock); + + $this->model = (new ObjectManager($this))->getObject( + DataProductMap::class, + [ + 'collectionFactory' => $this->collectionFactoryMock, + 'dataMapPool' => $this->dataMapPoolMock, + 'mapData' => [], + ] + ); + } + + /** + * Tests getAllData, getData and resetData functionality + */ + public function testGetAllData() + { + $productIds = ['1' => [1, 2, 3], '2' => [2, 3], '3' => 3]; + $productIdsOther = ['2' => [2, 3, 4]]; + + $connectionMock = $this->getMock(AdapterInterface::class); + $selectMock = $this->getMock(Select::class, [], [], '', false); + + $this->productCollectionMock->expects($this->exactly(3)) + ->method('getAllIds') + ->willReturnOnConsecutiveCalls($productIds, $productIdsOther, $productIds); + $this->productCollectionMock->expects($this->any()) + ->method('getConnection') + ->willReturn($connectionMock); + $connectionMock->expects($this->any()) + ->method('getTableName') + ->willReturn($this->returnValue($this->returnArgument(0))); + $this->productCollectionMock->expects($this->any()) + ->method('getSelect') + ->willReturn($selectMock); + $selectMock->expects($this->any()) + ->method('from') + ->willReturnSelf(); + $selectMock->expects($this->any()) + ->method('joinInner') + ->willReturnSelf(); + $selectMock->expects($this->any()) + ->method('where') + ->willReturnSelf(); + $this->dataCategoryMapMock->expects($this->any()) + ->method('getAllData') + ->willReturn([]); + $this->dataMapPoolMock->expects($this->any()) + ->method('resetDataMap') + ->with(DataCategoryMap::class, 1); + $this->assertEquals($productIds, $this->model->getAllData(1)); + $this->assertEquals($productIds[2], $this->model->getData(1, 2)); + $this->assertEquals($productIdsOther, $this->model->getAllData(2)); + $this->assertEquals($productIdsOther[2], $this->model->getData(2, 2)); + $this->model->resetData(1); + $this->assertEquals($productIds[2], $this->model->getData(1, 2)); + $this->assertEquals($productIds, $this->model->getAllData(1)); + } +} diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataProductUrlRewriteMapTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataProductUrlRewriteMapTest.php new file mode 100644 index 0000000000000..9d46fbdae4ea4 --- /dev/null +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataProductUrlRewriteMapTest.php @@ -0,0 +1,134 @@ +dataMapPoolMock = $this->getMock(DataMapPoolInterface::class); + $this->dataProductMapMock = $this->getMock(DataProductMap::class, [], [], '', false); + $this->temporaryTableServiceMock = $this->getMock(TemporaryTableService::class, [], [], '', false); + $this->urlRewriteFactoryMock = $this->getMock(UrlRewriteFactory::class, ['create'], [], '', false); + $this->urlRewritePlaceholderMock = $this->getMock(UrlRewrite::class, [], [], '', false); + $this->connectionMock = $this->getMock(ResourceConnection::class, [], [], '', false); + + $this->dataMapPoolMock->expects($this->any()) + ->method('getDataMap') + ->willReturn($this->dataProductMapMock); + + $this->urlRewriteFactoryMock->expects($this->any()) + ->method('create') + ->willReturn($this->urlRewritePlaceholderMock); + + $this->model = (new ObjectManager($this))->getObject( + DataCategoryUrlRewriteMap::class, + [ + 'connection' => $this->connectionMock, + 'dataMapPool' => $this->dataMapPoolMock, + 'temporaryTableService' => $this->temporaryTableServiceMock, + 'urlRewriteFactory' => $this->urlRewriteFactoryMock, + 'mapData' => [], + ] + ); + } + + /** + * Tests getAllData, getData and resetData functionality + */ + public function testGetAllData() + { + $productStoreIds = [ + '1' => ['store_id' => 1, 'product_id' => 1], + '2' => ['store_id' => 2, 'product_id' => 1], + '3' => ['store_id' => 3, 'product_id' => 1], + '4' => ['store_id' => 1, 'product_id' => 2], + '5' => ['store_id' => 2, 'product_id' => 2], + ]; + + $productStoreIdsExpectedMap = [ + '1' => [1, 2, 3], + '2' => [1, 2], + ]; + + $connectionMock = $this->getMock(AdapterInterface::class); + $selectMock = $this->getMock(Select::class, [], [], '', false); + + $this->connectionMock->expects($this->any()) + ->method('getConnection') + ->willReturn($connectionMock); + $connectionMock->expects($this->any()) + ->method('select') + ->willReturn($selectMock); + $connectionMock->expects($this->any()) + ->method('fetchAll') + ->willReturnOnConsecutiveCalls($productStoreIds, $productStoreIdsExpectedMap); + $selectMock->expects($this->any()) + ->method('from') + ->willReturnSelf(); + $selectMock->expects($this->any()) + ->method('joinInner') + ->willReturnSelf(); + $selectMock->expects($this->any()) + ->method('where') + ->willReturnSelf(); + + $this->dataProductMapMock->expects($this->any()) + ->method('getAllData') + ->willReturn([]); + + $this->temporaryTableServiceMock->expects($this->any()) + ->method('createFromSelect') + ->withConsecutive( + $selectMock, + $connectionMock, + [ + 'PRIMARY' => ['url_rewrite_id'], + 'HASHKEY_ENTITY_STORE' => ['hash_key'], + 'ENTITY_STORE' => ['entity_id', 'store_id'] + ] + ) + ->willReturn('tempTableName'); + + $this->assertEquals($productStoreIds, $this->model->getAllData(1)); + $this->assertEquals($productStoreIdsExpectedMap, $this->model->getData(1, '3_1')); + } +} diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/UrlRewriteMapTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/UrlRewriteMapTest.php new file mode 100644 index 0000000000000..a90557cc86eb9 --- /dev/null +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/UrlRewriteMapTest.php @@ -0,0 +1,156 @@ +dataMapPoolMock = $this->getMock(DataMapPoolInterface::class); + $this->urlFinderMock = $this->getMock(UrlFinderInterface::class); + $this->urlRewriteFactoryMock = $this->getMock(UrlRewriteFactory::class, ['create'], [], '', false); + $this->urlRewritePlaceholderMock = new UrlRewrite(); + + + $this->urlRewriteFactoryMock->expects($this->any()) + ->method('create') + ->willReturn($this->urlRewritePlaceholderMock); + + $this->model = (new ObjectManager($this))->getObject( + UrlRewriteMap::class, + [ + 'dataMapPool' => $this->dataMapPoolMock, + 'urlFinder' => $this->urlFinderMock, + 'urlRewriteFactory' => $this->urlRewriteFactoryMock + ] + ); + } + + /** + * test getByIdentifiers using findAllByData + */ + function testGetByIdentifiersFallback() + { + $expected = [1, 2, 3]; + $this->dataMapPoolMock->expects($this->never()) + ->method('getDataMap'); + + $this->urlFinderMock->expects($this->exactly(7)) + ->method('findAllByData') + ->willReturn($expected); + + $this->assertEquals($expected, $this->model->getByIdentifiers(1, 1, UrlRewriteMap::ENTITY_TYPE_CATEGORY)); + $this->assertEquals($expected, $this->model->getByIdentifiers(1, 1, UrlRewriteMap::ENTITY_TYPE_PRODUCT)); + $this->assertEquals($expected, $this->model->getByIdentifiers('a', 1, UrlRewriteMap::ENTITY_TYPE_PRODUCT), 1); + $this->assertEquals($expected, $this->model->getByIdentifiers('a', 'a', UrlRewriteMap::ENTITY_TYPE_PRODUCT), 1); + $this->assertEquals($expected, $this->model->getByIdentifiers(1, 'a', UrlRewriteMap::ENTITY_TYPE_PRODUCT), 1); + $this->assertEquals($expected, $this->model->getByIdentifiers(1, 1, 'cms', 1)); + $this->assertEquals($expected, $this->model->getByIdentifiers(1, 1, 'cms')); + } + + /** + * test getByIdentifiers Product URL rewrites + */ + function testGetByIdentifiersProduct() + { + $data =[ + [ + 'url_rewrite_id' => '1', + 'entity_type' => 'product', + 'entity_id' => '3', + 'request_path' => 'request_path', + 'target_path' => 'target_path', + 'redirect_type' => 'redirect_type', + 'store_id' => '4', + 'description' => 'description', + 'is_autogenerated' => '1', + 'metadata' => '{}' + ] + ]; + + $dataProductMapMock = $this->getMock(DataProductUrlRewriteMap::class, [], [], '', false); + $this->dataMapPoolMock->expects($this->once()) + ->method('getDataMap') + ->with(DataProductUrlRewriteMap::class, 1) + ->willReturn($dataProductMapMock); + + $this->urlFinderMock->expects($this->never()) + ->method('findAllByData') + ->willReturn([]); + + $dataProductMapMock->expects($this->once()) + ->method('getData') + ->willReturn($data); + + $urlRewriteResultArray = $this->model->getByIdentifiers(1, 1, UrlRewriteMap::ENTITY_TYPE_PRODUCT, 1); + $this->assertEquals($data[0], $urlRewriteResultArray[0]->toArray()); + } + + /** + * test getByIdentifiers Category URL rewrites + */ + function testGetByIdentifiersCategory() + { + $data =[ + [ + 'url_rewrite_id' => '1', + 'entity_type' => 'category', + 'entity_id' => '3', + 'request_path' => 'request_path', + 'target_path' => 'target_path', + 'redirect_type' => 'redirect_type', + 'store_id' => '4', + 'description' => 'description', + 'is_autogenerated' => '1', + 'metadata' => '{}' + ] + ]; + + $dataCategoryMapMock = $this->getMock(DataCategoryUrlRewriteMap::class, [], [], '', false); + $this->dataMapPoolMock->expects($this->once()) + ->method('getDataMap') + ->with(DataCategoryUrlRewriteMap::class, 1) + ->willReturn($dataCategoryMapMock); + + $this->urlFinderMock->expects($this->never()) + ->method('findAllByData') + ->willReturn([]); + + $dataCategoryMapMock->expects($this->once()) + ->method('getData') + ->willReturn($data); + + $urlRewriteResultArray = $this->model->getByIdentifiers(1, 1, UrlRewriteMap::ENTITY_TYPE_CATEGORY, 1); + $this->assertEquals($data[0], $urlRewriteResultArray[0]->toArray()); + } +} diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Product/CurrentUrlRewritesRegeneratorTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Product/CurrentUrlRewritesRegeneratorTest.php index a633bb072555e..b7e1c541e476d 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Product/CurrentUrlRewritesRegeneratorTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Product/CurrentUrlRewritesRegeneratorTest.php @@ -16,31 +16,34 @@ class CurrentUrlRewritesRegeneratorTest extends \PHPUnit_Framework_TestCase { /** @var \Magento\CatalogUrlRewrite\Model\Product\CurrentUrlRewritesRegenerator */ - protected $currentUrlRewritesRegenerator; - - /** @var \PHPUnit_Framework_MockObject_MockObject */ - protected $filter; + private $currentUrlRewritesRegenerator; /** @var \Magento\UrlRewrite\Model\UrlFinderInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $urlFinder; + private $urlFinder; /** @var \Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator|\PHPUnit_Framework_MockObject_MockObject */ - protected $productUrlPathGenerator; + private $productUrlPathGenerator; /** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject */ - protected $product; + private $product; /** @var \Magento\Catalog\Model\Category|\PHPUnit_Framework_MockObject_MockObject */ - protected $category; + private $category; /** @var \Magento\CatalogUrlRewrite\Model\ObjectRegistry|\PHPUnit_Framework_MockObject_MockObject */ - protected $objectRegistry; + private $objectRegistry; /** @var \Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory|\PHPUnit_Framework_MockObject_MockObject */ - protected $urlRewriteFactory; + private $urlRewriteFactory; /** @var \Magento\UrlRewrite\Service\V1\Data\UrlRewrite|\PHPUnit_Framework_MockObject_MockObject */ - protected $urlRewrite; + private $urlRewrite; + + /** @var \PHPUnit_Framework_MockObject_MockObject */ + private $urlRewritesSet; + + /** @var \PHPUnit_Framework_MockObject_MockObject */ + private $urlRewriteMap; protected function setUp() { @@ -55,28 +58,28 @@ protected function setUp() ->disableOriginalConstructor()->getMock(); $this->objectRegistry = $this->getMockBuilder(\Magento\CatalogUrlRewrite\Model\ObjectRegistry::class) ->disableOriginalConstructor()->getMock(); - $this->filter = $this->getMockBuilder(\Magento\UrlRewrite\Service\V1\Data\Filter::class) - ->disableOriginalConstructor()->getMock(); - $this->filter->expects($this->any())->method('setStoreId')->will($this->returnSelf()); - $this->filter->expects($this->any())->method('setEntityId')->will($this->returnSelf()); - $this->urlFinder = $this->getMockBuilder(\Magento\UrlRewrite\Model\UrlFinderInterface::class) + $this->urlRewriteMap = $this->getMockBuilder(\Magento\CatalogUrlRewrite\Model\Map\UrlRewriteMap::class) ->disableOriginalConstructor()->getMock(); + $this->urlRewriteFactory->expects($this->once())->method('create') + ->willReturn($this->urlRewrite); $this->productUrlPathGenerator = $this->getMockBuilder( \Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator::class )->disableOriginalConstructor()->getMock(); + $this->urlRewritesSet = new \Magento\UrlRewrite\Model\UrlRewritesSet; $this->currentUrlRewritesRegenerator = (new ObjectManager($this))->getObject( \Magento\CatalogUrlRewrite\Model\Product\CurrentUrlRewritesRegenerator::class, [ - 'urlFinder' => $this->urlFinder, 'productUrlPathGenerator' => $this->productUrlPathGenerator, - 'urlRewriteFactory' => $this->urlRewriteFactory + 'urlRewriteFactory' => $this->urlRewriteFactory, + 'urlRewritesSet' => $this->urlRewritesSet, + 'urlRewriteMap' => $this->urlRewriteMap ] ); } public function testIsAutogeneratedWithoutSaveRewriteHistory() { - $this->urlFinder->expects($this->once())->method('findAllByData') + $this->urlRewriteMap->expects($this->once())->method('getByIdentifiers') ->will($this->returnValue($this->getCurrentRewritesMocks([[UrlRewrite::IS_AUTOGENERATED => 1]]))); $this->product->expects($this->once())->method('getData')->with('save_rewrites_history') ->will($this->returnValue(false)); @@ -89,7 +92,7 @@ public function testIsAutogeneratedWithoutSaveRewriteHistory() public function testSkipGenerationForAutogenerated() { - $this->urlFinder->expects($this->once())->method('findAllByData') + $this->urlRewriteMap->expects($this->once())->method('getByIdentifiers') ->will($this->returnValue($this->getCurrentRewritesMocks([ [UrlRewrite::IS_AUTOGENERATED => 1, UrlRewrite::REQUEST_PATH => 'same-path'], ]))); @@ -100,7 +103,7 @@ public function testSkipGenerationForAutogenerated() $this->assertEquals( [], - $this->currentUrlRewritesRegenerator->generate('store_id', $this->product, $this->objectRegistry) + $this->currentUrlRewritesRegenerator->generate('store_id', $this->product, $this->objectRegistry, 1) ); } @@ -108,21 +111,22 @@ public function testIsAutogeneratedWithoutCategory() { $requestPath = 'autogenerated.html'; $targetPath = 'some-path.html'; + $autoGenerated = 1; $storeId = 2; $productId = 12; $description = 'description'; - $this->urlFinder->expects($this->once())->method('findAllByData') + $this->urlRewriteMap->expects($this->once())->method('getByIdentifiers') ->will($this->returnValue($this->getCurrentRewritesMocks([ [ UrlRewrite::REQUEST_PATH => $requestPath, UrlRewrite::TARGET_PATH => 'custom-target-path', UrlRewrite::STORE_ID => $storeId, - UrlRewrite::IS_AUTOGENERATED => 1, + UrlRewrite::IS_AUTOGENERATED => $autoGenerated, UrlRewrite::METADATA => [], UrlRewrite::DESCRIPTION => $description, ], ]))); - $this->product->expects($this->any())->method('getId')->will($this->returnValue($productId)); + $this->product->expects($this->any())->method('getEntityId')->will($this->returnValue($productId)); $this->product->expects($this->once())->method('getData')->with('save_rewrites_history') ->will($this->returnValue(true)); $this->productUrlPathGenerator->expects($this->once())->method('getUrlPathWithSuffix') @@ -133,6 +137,7 @@ public function testIsAutogeneratedWithoutCategory() $productId, $requestPath, $targetPath, + $autoGenerated, OptionProvider::PERMANENT, [], $description @@ -140,7 +145,7 @@ public function testIsAutogeneratedWithoutCategory() $this->assertEquals( [$this->urlRewrite], - $this->currentUrlRewritesRegenerator->generate($storeId, $this->product, $this->objectRegistry) + $this->currentUrlRewritesRegenerator->generate($storeId, $this->product, $this->objectRegistry, 1) ); } @@ -149,21 +154,22 @@ public function testIsAutogeneratedWithCategory() $productId = 12; $requestPath = 'autogenerated.html'; $targetPath = 'simple-product.html'; + $autoGenerated = 1; $storeId = 2; $metadata = ['category_id' => 2, 'some_another_data' => 1]; $description = 'description'; - $this->urlFinder->expects($this->once())->method('findAllByData') + $this->urlRewriteMap->expects($this->once())->method('getByIdentifiers') ->will($this->returnValue($this->getCurrentRewritesMocks([ [ UrlRewrite::REQUEST_PATH => $requestPath, UrlRewrite::TARGET_PATH => 'some-path.html', UrlRewrite::STORE_ID => $storeId, - UrlRewrite::IS_AUTOGENERATED => 1, + UrlRewrite::IS_AUTOGENERATED => $autoGenerated, UrlRewrite::METADATA => $metadata, UrlRewrite::DESCRIPTION => $description, ], ]))); - $this->product->expects($this->any())->method('getId')->will($this->returnValue($productId)); + $this->product->expects($this->any())->method('getEntityId')->will($this->returnValue($productId)); $this->product->expects($this->once())->method('getData')->with('save_rewrites_history') ->will($this->returnValue(true)); $this->productUrlPathGenerator->expects($this->once())->method('getUrlPathWithSuffix') @@ -174,6 +180,7 @@ public function testIsAutogeneratedWithCategory() $productId, $requestPath, $targetPath, + $autoGenerated, OptionProvider::PERMANENT, $metadata, $description @@ -181,13 +188,13 @@ public function testIsAutogeneratedWithCategory() $this->assertEquals( [$this->urlRewrite], - $this->currentUrlRewritesRegenerator->generate($storeId, $this->product, $this->objectRegistry) + $this->currentUrlRewritesRegenerator->generate($storeId, $this->product, $this->objectRegistry, 2) ); } public function testSkipGenerationForCustom() { - $this->urlFinder->expects($this->once())->method('findAllByData') + $this->urlRewriteMap->expects($this->once())->method('getByIdentifiers') ->will($this->returnValue($this->getCurrentRewritesMocks([ [ UrlRewrite::IS_AUTOGENERATED => 0, @@ -210,21 +217,31 @@ public function testGenerationForCustomWithoutTargetPathGeneration() $productId = 123; $requestPath = 'generate-for-custom-without-redirect-type.html'; $targetPath = 'custom-target-path.html'; + $autoGenerated = 0; $description = 'description'; - $this->urlFinder->expects($this->once())->method('findAllByData') + $this->urlRewriteMap->expects($this->once())->method('getByIdentifiers') ->will($this->returnValue($this->getCurrentRewritesMocks([ [ UrlRewrite::REQUEST_PATH => $requestPath, UrlRewrite::TARGET_PATH => $targetPath, UrlRewrite::REDIRECT_TYPE => 0, - UrlRewrite::IS_AUTOGENERATED => 0, + UrlRewrite::IS_AUTOGENERATED => $autoGenerated, UrlRewrite::DESCRIPTION => $description, UrlRewrite::METADATA => [], ], ]))); $this->productUrlPathGenerator->expects($this->never())->method('getUrlPathWithSuffix'); - $this->product->expects($this->any())->method('getId')->will($this->returnValue($productId)); - $this->prepareUrlRewriteMock($storeId, $productId, $requestPath, $targetPath, 0, [], $description); + $this->product->expects($this->any())->method('getEntityId')->will($this->returnValue($productId)); + $this->prepareUrlRewriteMock( + $storeId, + $productId, + $requestPath, + $targetPath, + $autoGenerated, + 0, + [], + $description + ); $this->assertEquals( [$this->urlRewrite], @@ -238,22 +255,23 @@ public function testGenerationForCustomWithTargetPathGeneration() $productId = 123; $requestPath = 'generate-for-custom-without-redirect-type.html'; $targetPath = 'generated-target-path.html'; + $autoGenerated = 0; $description = 'description'; - $this->urlFinder->expects($this->once())->method('findAllByData') + $this->urlRewriteMap->expects($this->once())->method('getByIdentifiers') ->will($this->returnValue($this->getCurrentRewritesMocks([ [ UrlRewrite::REQUEST_PATH => $requestPath, UrlRewrite::TARGET_PATH => 'custom-target-path.html', UrlRewrite::REDIRECT_TYPE => 'code', - UrlRewrite::IS_AUTOGENERATED => 0, + UrlRewrite::IS_AUTOGENERATED => $autoGenerated, UrlRewrite::DESCRIPTION => $description, UrlRewrite::METADATA => [], ], ]))); $this->productUrlPathGenerator->expects($this->any())->method('getUrlPathWithSuffix') ->will($this->returnValue($targetPath)); - $this->product->expects($this->any())->method('getId')->will($this->returnValue($productId)); - $this->prepareUrlRewriteMock($storeId, $productId, $requestPath, $targetPath, 'code', [], $description); + $this->product->expects($this->any())->method('getEntityId')->will($this->returnValue($productId)); + $this->prepareUrlRewriteMock($storeId, $productId, $requestPath, $targetPath, 0, 'code', [], $description); $this->assertEquals( [$this->urlRewrite], @@ -287,6 +305,7 @@ protected function getCurrentRewritesMocks($currentRewrites) * @param mixed $productId * @param mixed $requestPath * @param mixed $targetPath + * @param mixed $autoGenerated * @param mixed $redirectType * @param mixed $metadata * @param mixed $description @@ -296,6 +315,7 @@ protected function prepareUrlRewriteMock( $productId, $requestPath, $targetPath, + $autoGenerated, $redirectType, $metadata, $description @@ -310,7 +330,7 @@ protected function prepareUrlRewriteMock( ->will($this->returnSelf()); $this->urlRewrite->expects($this->any())->method('setTargetPath')->with($targetPath) ->will($this->returnSelf()); - $this->urlRewrite->expects($this->any())->method('setIsAutogenerated')->with(0) + $this->urlRewrite->expects($this->any())->method('setIsAutogenerated')->with($autoGenerated) ->will($this->returnSelf()); $this->urlRewrite->expects($this->any())->method('setRedirectType')->with($redirectType) ->will($this->returnSelf()); diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/ProductScopeRewriteGeneratorTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/ProductScopeRewriteGeneratorTest.php index 99bb858e31b8f..a7f2642fd970e 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/ProductScopeRewriteGeneratorTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/ProductScopeRewriteGeneratorTest.php @@ -41,6 +41,9 @@ class ProductScopeRewriteGeneratorTest extends \PHPUnit_Framework_TestCase /** @var ProductScopeRewriteGenerator */ private $productScopeGenerator; + /** @var \PHPUnit_Framework_MockObject_MockObject */ + private $urlRewritesSet; + public function setUp() { $this->currentUrlRewritesRegenerator = $this->getMockBuilder( @@ -61,6 +64,7 @@ public function setUp() $this->storeViewService = $this->getMockBuilder(\Magento\CatalogUrlRewrite\Service\V1\StoreViewService::class) ->disableOriginalConstructor()->getMock(); $this->storeManager = $this->getMock(StoreManagerInterface::class); + $this->urlRewritesSet = new \Magento\UrlRewrite\Model\UrlRewritesSet; $this->productScopeGenerator = (new ObjectManager($this))->getObject( \Magento\CatalogUrlRewrite\Model\ProductScopeRewriteGenerator::class, @@ -72,6 +76,7 @@ public function setUp() 'objectRegistryFactory' => $this->objectRegistryFactory, 'storeViewService' => $this->storeViewService, 'storeManager' => $this->storeManager, + 'urlRewritesSet' => $this->urlRewritesSet ] ); } @@ -91,34 +96,34 @@ public function testGenerationForGlobalScope() ->willReturn(1); $this->initObjectRegistryFactory([]); $canonical = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite(); - $canonical->setTargetPath('category-1') + $canonical->setRequestPath('category-1') ->setStoreId(1); $this->canonicalUrlRewriteGenerator->expects($this->any())->method('generate') ->will($this->returnValue([$canonical])); $categories = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite(); - $categories->setTargetPath('category-2') + $categories->setRequestPath('category-2') ->setStoreId(2); $this->categoriesUrlRewriteGenerator->expects($this->any())->method('generate') ->will($this->returnValue([$categories])); $current = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite(); - $current->setTargetPath('category-3') + $current->setRequestPath('category-3') ->setStoreId(3); $this->currentUrlRewritesRegenerator->expects($this->any())->method('generate') ->will($this->returnValue([$current])); $anchorCategories = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite(); - $anchorCategories->setTargetPath('category-4') + $anchorCategories->setRequestPath('category-4') ->setStoreId(4); $this->anchorUrlRewriteGenerator->expects($this->any())->method('generate') ->will($this->returnValue([$anchorCategories])); $this->assertEquals( [ - 'category-1-1' => $canonical, - 'category-2-2' => $categories, - 'category-3-3' => $current, - 'category-4-4' => $anchorCategories + 'category-1_1' => $canonical, + 'category-2_2' => $categories, + 'category-3_3' => $current, + 'category-4_4' => $anchorCategories ], - $this->productScopeGenerator->generateForGlobalScope([$categoryMock], $product) + $this->productScopeGenerator->generateForGlobalScope([$categoryMock], $product, 1) ); } @@ -138,7 +143,7 @@ public function testGenerationForSpecificStore() $this->storeManager->expects($this->any())->method('getStore')->will($this->returnValue($store)); $this->initObjectRegistryFactory([$category]); $canonical = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite(); - $canonical->setTargetPath('category-1') + $canonical->setRequestPath('category-1') ->setStoreId(1); $this->canonicalUrlRewriteGenerator->expects($this->any())->method('generate') ->will($this->returnValue([$canonical])); @@ -150,8 +155,8 @@ public function testGenerationForSpecificStore() ->will($this->returnValue([])); $this->assertEquals( - ['category-1-1' => $canonical], - $this->productScopeGenerator->generateForSpecificStoreView(1, [$category], $product) + ['category-1_1' => $canonical], + $this->productScopeGenerator->generateForSpecificStoreView(1, [$category], $product, 1) ); } @@ -165,7 +170,7 @@ public function testSkipGenerationForGlobalScope() $this->storeViewService->expects($this->exactly(2))->method('doesEntityHaveOverriddenUrlKeyForStore') ->will($this->returnValue(true)); - $this->assertEquals([], $this->productScopeGenerator->generateForGlobalScope([], $product)); + $this->assertEquals([], $this->productScopeGenerator->generateForGlobalScope([], $product, 1)); } /** diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/ProductUrlRewriteGeneratorTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/ProductUrlRewriteGeneratorTest.php index d88171b90bbee..9d96da186daad 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/ProductUrlRewriteGeneratorTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/ProductUrlRewriteGeneratorTest.php @@ -129,6 +129,6 @@ public function testGenerate() $this->productScopeRewriteGenerator->expects($this->once()) ->method('generateForSpecificStoreView') ->willReturn($urls); - $this->assertEquals($urls, $this->productUrlRewriteGenerator->generate($productMock)); + $this->assertEquals($urls, $this->productUrlRewriteGenerator->generate($productMock, 1)); } } diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Observer/AfterImportDataObserverTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Observer/AfterImportDataObserverTest.php index 948ef99544bbd..271e4bf3d4fdb 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Observer/AfterImportDataObserverTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Observer/AfterImportDataObserverTest.php @@ -23,12 +23,12 @@ class AfterImportDataObserverTest extends \PHPUnit_Framework_TestCase /** * @var string */ - protected $categoryId = 10; + private $categoryId = 10; /** * @var \Magento\UrlRewrite\Model\UrlPersistInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $urlPersist; + private $urlPersist; /** * @var \Magento\UrlRewrite\Model\UrlFinderInterface|\PHPUnit_Framework_MockObject_MockObject @@ -38,12 +38,12 @@ class AfterImportDataObserverTest extends \PHPUnit_Framework_TestCase /** * @var \Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator|\PHPUnit_Framework_MockObject_MockObject */ - protected $productUrlRewriteGenerator; + private $productUrlRewriteGenerator; /** * @var \Magento\Catalog\Api\ProductRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $productRepository; + private $productRepository; /** * @var \Magento\CatalogImportExport\Model\Import\Product|\PHPUnit_Framework_MockObject_MockObject @@ -53,67 +53,65 @@ class AfterImportDataObserverTest extends \PHPUnit_Framework_TestCase /** * @var \Magento\Framework\Event\Observer|\PHPUnit_Framework_MockObject_MockObject */ - protected $observer; + private $observer; /** * @var \Magento\Framework\Event|\PHPUnit_Framework_MockObject_MockObject */ - protected $event; + private $event; /** * @var \Magento\Catalog\Model\ProductFactory|\PHPUnit_Framework_MockObject_MockObject */ - protected $catalogProductFactory; + private $catalogProductFactory; /** * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $storeManager; + private $storeManager; /** * @var \Magento\CatalogUrlRewrite\Model\ObjectRegistryFactory|\PHPUnit_Framework_MockObject_MockObject */ - protected $objectRegistryFactory; + private $objectRegistryFactory; /** * @var \Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator|\PHPUnit_Framework_MockObject_MockObject */ - protected $productUrlPathGenerator; + private $productUrlPathGenerator; /** * @var \Magento\CatalogUrlRewrite\Service\V1\StoreViewService|\PHPUnit_Framework_MockObject_MockObject */ - protected $storeViewService; + private $storeViewService; /** * @var \Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory|\PHPUnit_Framework_MockObject_MockObject */ - protected $urlRewriteFactory; + private $urlRewriteFactory; /** * @var \Magento\UrlRewrite\Service\V1\Data\UrlRewrite|\PHPUnit_Framework_MockObject_MockObject */ - protected $urlRewrite; + private $urlRewrite; /** * @var \Magento\CatalogUrlRewrite\Model\ObjectRegistry|\PHPUnit_Framework_MockObject_MockObject */ - protected $objectRegistry; - - /** - * @var \Magento\CatalogUrlRewrite\Observer\AfterImportDataObserver|\PHPUnit_Framework_MockObject_MockObject - */ - protected $importMock; + private $objectRegistry; /** * @var \Magento\CatalogUrlRewrite\Observer\AfterImportDataObserver */ - protected $import; + private $import; /** * @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject */ - protected $product; + private $product; + + /** @var \Magento\UrlRewrite\Model\UrlRewritesSet|\PHPUnit_Framework_MockObject_MockObject */ + private $urlRewritesSet; /** * Test products returned by getBunch method of event object. @@ -278,6 +276,7 @@ protected function setUp() ->expects($this->any()) ->method('getCategoryProcessor') ->willReturn($categoryProcessor); + $this->urlRewritesSet = new \Magento\UrlRewrite\Model\UrlRewritesSet; $this->objectManager = new ObjectManager($this); $this->import = $this->objectManager->getObject( @@ -291,6 +290,7 @@ protected function setUp() 'urlPersist' => $this->urlPersist, 'urlRewriteFactory' => $this->urlRewriteFactory, 'urlFinder' => $this->urlFinder, + 'urlRewritesSet' => $this->urlRewritesSet ] ); } @@ -433,14 +433,15 @@ public function testAfterImportData() $this->urlRewrite->expects($this->any())->method('setRequestPath')->willReturnSelf(); $this->urlRewrite->expects($this->any())->method('setTargetPath')->willReturnSelf(); $this->urlRewrite->expects($this->any())->method('getTargetPath')->willReturn('targetPath'); + $this->urlRewrite->expects($this->any())->method('getRequestPath')->willReturn('requestPath'); $this->urlRewrite->expects($this->any())->method('getStoreId') ->willReturnOnConsecutiveCalls(0, 'not global'); $this->urlRewriteFactory->expects($this->any())->method('create')->willReturn($this->urlRewrite); $productUrls = [ - 'targetPath-0' => $this->urlRewrite, - 'targetPath-not global' => $this->urlRewrite + 'requestPath_' => $this->urlRewrite, + 'requestPath_not global' => $this->urlRewrite ]; $this->urlPersist From 0571c03c5b351b3268da7d25bdcdac7de682c2d7 Mon Sep 17 00:00:00 2001 From: Cristian Partica Date: Mon, 19 Dec 2016 12:58:05 -0600 Subject: [PATCH 14/39] MAGETWO-58924: SQL error wait timeout error when saving categories - unit tests static fix --- .../Test/Unit/Model/Category/Plugin/StorageTest.php | 4 +++- .../Test/Unit/Model/Map/DataCategoryMapTest.php | 1 - .../Test/Unit/Model/Map/UrlRewriteMapTest.php | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/Plugin/StorageTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/Plugin/StorageTest.php index a9f20544c1008..5c068cc59b724 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/Plugin/StorageTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/Plugin/StorageTest.php @@ -101,7 +101,9 @@ public function testAfterReplace() public function testBeforeDeleteByData() { $data = [1, 2, 3]; - $this->productResourceModel->expects(static::once())->method('removeMultipleByFilter')->with($data)->willReturnSelf(); + $this->productResourceModel->expects(static::once()) + ->method('removeMultipleByFilter') + ->with($data)->willReturnSelf(); $this->plugin->beforeDeleteByData($this->storage, $data); } } diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryMapTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryMapTest.php index 19b4b837c9bfc..020d57ca47a27 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryMapTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryMapTest.php @@ -14,7 +14,6 @@ use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; use Magento\Framework\DB\Adapter\AdapterInterface; - /** * Class DataCategoryMapTest */ diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/UrlRewriteMapTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/UrlRewriteMapTest.php index a90557cc86eb9..aee53db63894d 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/UrlRewriteMapTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/UrlRewriteMapTest.php @@ -17,7 +17,7 @@ /** * Class DataProductUrlRewriteMapTest */ -class DataProductUrlRewriteMapTest extends \PHPUnit_Framework_TestCase +class UrlRewriteMapTest extends \PHPUnit_Framework_TestCase { /** @var DataMapPoolInterface|\PHPUnit_Framework_MockObject_MockObject */ private $dataMapPoolMock; From 3a29056b728a7697900226c154d58e64014ba22a Mon Sep 17 00:00:00 2001 From: Cristian Partica Date: Mon, 19 Dec 2016 13:02:12 -0600 Subject: [PATCH 15/39] MAGETWO-58924: SQL error wait timeout error when saving categories - unit tests static fix --- .../Model/Map/DataCategoryUrlRewriteMap.php | 8 -------- .../Model/Map/DataCategoryUrlRewriteMapTest.php | 16 +--------------- .../Model/Map/DataProductUrlRewriteMapTest.php | 15 --------------- 3 files changed, 1 insertion(+), 38 deletions(-) diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUrlRewriteMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUrlRewriteMap.php index de459f9681665..f8046fc31d17b 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUrlRewriteMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUrlRewriteMap.php @@ -5,8 +5,6 @@ */ namespace Magento\CatalogUrlRewrite\Model\Map; -use Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory; -use Magento\UrlRewrite\Service\V1\Data\UrlRewrite; use Magento\Framework\App\ResourceConnection; use Magento\Framework\DB\TemporaryTableService; use \Magento\Framework\DB\Select; @@ -21,9 +19,6 @@ class DataCategoryUrlRewriteMap implements DataMapInterface /** @var string[] */ private $tableNames = []; - /** @var UrlRewrite */ - private $urlRewritePlaceholder; - /** @var DataMapPoolInterface */ private $dataMapPool; @@ -36,19 +31,16 @@ class DataCategoryUrlRewriteMap implements DataMapInterface /** * @param ResourceConnection $connection * @param DataMapPoolInterface $dataMapPool, - * @param UrlRewriteFactory $urlRewriteFactory * @param TemporaryTableService $temporaryTableService, */ public function __construct( ResourceConnection $connection, DataMapPoolInterface $dataMapPool, - UrlRewriteFactory $urlRewriteFactory, TemporaryTableService $temporaryTableService ) { $this->connection = $connection; $this->dataMapPool = $dataMapPool; $this->temporaryTableService = $temporaryTableService; - $this->urlRewritePlaceholder = $urlRewriteFactory->create(); } /** diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryUrlRewriteMapTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryUrlRewriteMapTest.php index 68c97572776f7..5e4c1d9dd31c9 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryUrlRewriteMapTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryUrlRewriteMapTest.php @@ -15,8 +15,7 @@ use Magento\Framework\DB\Adapter\AdapterInterface; use Magento\Framework\App\ResourceConnection; use Magento\Framework\DB\TemporaryTableService; -use Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory; -use Magento\UrlRewrite\Service\V1\Data\UrlRewrite; + /** * Class DataCategoryUrlRewriteMapTest @@ -35,12 +34,6 @@ class DataCategoryUrlRewriteMapTest extends \PHPUnit_Framework_TestCase /** @var TemporaryTableService|\PHPUnit_Framework_MockObject_MockObject */ private $temporaryTableServiceMock; - /** @var UrlRewriteFactory|\PHPUnit_Framework_MockObject_MockObject */ - private $urlRewriteFactoryMock; - - /** @var UrlRewrite|\PHPUnit_Framework_MockObject_MockObject */ - private $urlRewritePlaceholderMock; - /** @var ResourceConnection|\PHPUnit_Framework_MockObject_MockObject */ private $connectionMock; @@ -53,25 +46,18 @@ protected function setUp() $this->dataCategoryMapMock = $this->getMock(DataProductMap::class, [], [], '', false); $this->dataCategoryUsedInProductsMapMock = $this->getMock(DataCategoryUsedInProductsMap::class, [], [], '', false); $this->temporaryTableServiceMock = $this->getMock(TemporaryTableService::class, [], [], '', false); - $this->urlRewriteFactoryMock = $this->getMock(UrlRewriteFactory::class, ['create'], [], '', false); - $this->urlRewritePlaceholderMock = $this->getMock(UrlRewrite::class, [], [], '', false); $this->connectionMock = $this->getMock(ResourceConnection::class, [], [], '', false); $this->dataMapPoolMock->expects($this->any()) ->method('getDataMap') ->willReturnOnConsecutiveCalls($this->dataCategoryUsedInProductsMapMock, $this->dataCategoryMapMock); - $this->urlRewriteFactoryMock->expects($this->any()) - ->method('create') - ->willReturn($this->urlRewritePlaceholderMock); - $this->model = (new ObjectManager($this))->getObject( DataCategoryUrlRewriteMap::class, [ 'connection' => $this->connectionMock, 'dataMapPool' => $this->dataMapPoolMock, 'temporaryTableService' => $this->temporaryTableServiceMock, - 'urlRewriteFactory' => $this->urlRewriteFactoryMock, 'mapData' => [], ] ); diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataProductUrlRewriteMapTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataProductUrlRewriteMapTest.php index 9d46fbdae4ea4..60f608d0a47ca 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataProductUrlRewriteMapTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataProductUrlRewriteMapTest.php @@ -13,8 +13,6 @@ use Magento\Framework\DB\Adapter\AdapterInterface; use Magento\Framework\App\ResourceConnection; use Magento\Framework\DB\TemporaryTableService; -use Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory; -use Magento\UrlRewrite\Service\V1\Data\UrlRewrite; /** * Class DataProductUrlRewriteMapTest @@ -30,12 +28,6 @@ class DataProductUrlRewriteMapTest extends \PHPUnit_Framework_TestCase /** @var TemporaryTableService|\PHPUnit_Framework_MockObject_MockObject */ private $temporaryTableServiceMock; - /** @var UrlRewriteFactory|\PHPUnit_Framework_MockObject_MockObject */ - private $urlRewriteFactoryMock; - - /** @var UrlRewrite|\PHPUnit_Framework_MockObject_MockObject */ - private $urlRewritePlaceholderMock; - /** @var ResourceConnection|\PHPUnit_Framework_MockObject_MockObject */ private $connectionMock; @@ -47,25 +39,18 @@ protected function setUp() $this->dataMapPoolMock = $this->getMock(DataMapPoolInterface::class); $this->dataProductMapMock = $this->getMock(DataProductMap::class, [], [], '', false); $this->temporaryTableServiceMock = $this->getMock(TemporaryTableService::class, [], [], '', false); - $this->urlRewriteFactoryMock = $this->getMock(UrlRewriteFactory::class, ['create'], [], '', false); - $this->urlRewritePlaceholderMock = $this->getMock(UrlRewrite::class, [], [], '', false); $this->connectionMock = $this->getMock(ResourceConnection::class, [], [], '', false); $this->dataMapPoolMock->expects($this->any()) ->method('getDataMap') ->willReturn($this->dataProductMapMock); - $this->urlRewriteFactoryMock->expects($this->any()) - ->method('create') - ->willReturn($this->urlRewritePlaceholderMock); - $this->model = (new ObjectManager($this))->getObject( DataCategoryUrlRewriteMap::class, [ 'connection' => $this->connectionMock, 'dataMapPool' => $this->dataMapPoolMock, 'temporaryTableService' => $this->temporaryTableServiceMock, - 'urlRewriteFactory' => $this->urlRewriteFactoryMock, 'mapData' => [], ] ); From 75481343de23af2b214c9bcaafecd171b83af1b2 Mon Sep 17 00:00:00 2001 From: Cristian Partica Date: Mon, 19 Dec 2016 16:17:43 -0600 Subject: [PATCH 16/39] MAGETWO-58924: SQL error wait timeout error when saving categories - unit tests static fix - using random from magento library --- .../Model/Map/DataCategoryUrlRewriteMapTest.php | 9 +++++++-- .../Product/CurrentUrlRewritesRegeneratorTest.php | 3 --- .../Framework/DB/TemporaryTableService.php | 15 ++++++++++++++- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryUrlRewriteMapTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryUrlRewriteMapTest.php index 5e4c1d9dd31c9..cf524eb7d4c3f 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryUrlRewriteMapTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryUrlRewriteMapTest.php @@ -16,7 +16,6 @@ use Magento\Framework\App\ResourceConnection; use Magento\Framework\DB\TemporaryTableService; - /** * Class DataCategoryUrlRewriteMapTest */ @@ -44,7 +43,13 @@ protected function setUp() { $this->dataMapPoolMock = $this->getMock(DataMapPoolInterface::class); $this->dataCategoryMapMock = $this->getMock(DataProductMap::class, [], [], '', false); - $this->dataCategoryUsedInProductsMapMock = $this->getMock(DataCategoryUsedInProductsMap::class, [], [], '', false); + $this->dataCategoryUsedInProductsMapMock = $this->getMock( + DataCategoryUsedInProductsMap::class, + [], + [], + '', + false + ); $this->temporaryTableServiceMock = $this->getMock(TemporaryTableService::class, [], [], '', false); $this->connectionMock = $this->getMock(ResourceConnection::class, [], [], '', false); diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Product/CurrentUrlRewritesRegeneratorTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Product/CurrentUrlRewritesRegeneratorTest.php index b7e1c541e476d..c8137f9440870 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Product/CurrentUrlRewritesRegeneratorTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Product/CurrentUrlRewritesRegeneratorTest.php @@ -18,9 +18,6 @@ class CurrentUrlRewritesRegeneratorTest extends \PHPUnit_Framework_TestCase /** @var \Magento\CatalogUrlRewrite\Model\Product\CurrentUrlRewritesRegenerator */ private $currentUrlRewritesRegenerator; - /** @var \Magento\UrlRewrite\Model\UrlFinderInterface|\PHPUnit_Framework_MockObject_MockObject */ - private $urlFinder; - /** @var \Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator|\PHPUnit_Framework_MockObject_MockObject */ private $productUrlPathGenerator; diff --git a/lib/internal/Magento/Framework/DB/TemporaryTableService.php b/lib/internal/Magento/Framework/DB/TemporaryTableService.php index b88ef678b26d1..2d1e95d7e99e9 100644 --- a/lib/internal/Magento/Framework/DB/TemporaryTableService.php +++ b/lib/internal/Magento/Framework/DB/TemporaryTableService.php @@ -16,11 +16,24 @@ */ class TemporaryTableService { + /** + * @var $random + */ + private $random; + /** * @var AdapterInterface[] */ private $createdTables = []; + /** + * @param \Magento\Framework\Math\Random $random + */ + public function __construct(\Magento\Framework\Math\Random $random) + { + $this->random = $random; + } + /** * Creates a temporary table from select removing duplicate rows if you have a union in your select * This method should always be paired with dropTable to ensure cleanup @@ -51,7 +64,7 @@ public function createFromSelect( $indexMethod = 'HASH', $engine = 'INNODB' ) { - $name = uniqid('tmp_select_' . crc32((string)$select)); + $name = $this->random->getUniqueHash('tmp_select_'); $indexStatements = []; foreach ($indexes as $indexName => $columns) { From b698e7eefff5924c7eb26fb0d7a587f60b0b8af4 Mon Sep 17 00:00:00 2001 From: Cristian Partica Date: Mon, 19 Dec 2016 16:43:15 -0600 Subject: [PATCH 17/39] MAGETWO-58924: SQL error wait timeout error when saving categories - unit tests static fix --- .../Test/Unit/Model/Map/UrlRewriteMapTest.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/UrlRewriteMapTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/UrlRewriteMapTest.php index aee53db63894d..1dcdc30ba9b31 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/UrlRewriteMapTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/UrlRewriteMapTest.php @@ -41,7 +41,6 @@ protected function setUp() $this->urlRewriteFactoryMock = $this->getMock(UrlRewriteFactory::class, ['create'], [], '', false); $this->urlRewritePlaceholderMock = new UrlRewrite(); - $this->urlRewriteFactoryMock->expects($this->any()) ->method('create') ->willReturn($this->urlRewritePlaceholderMock); @@ -59,7 +58,7 @@ protected function setUp() /** * test getByIdentifiers using findAllByData */ - function testGetByIdentifiersFallback() + public function testGetByIdentifiersFallback() { $expected = [1, 2, 3]; $this->dataMapPoolMock->expects($this->never()) @@ -81,7 +80,7 @@ function testGetByIdentifiersFallback() /** * test getByIdentifiers Product URL rewrites */ - function testGetByIdentifiersProduct() + public function testGetByIdentifiersProduct() { $data =[ [ @@ -119,7 +118,7 @@ function testGetByIdentifiersProduct() /** * test getByIdentifiers Category URL rewrites */ - function testGetByIdentifiersCategory() + public function testGetByIdentifiersCategory() { $data =[ [ From bbe7944b2df574f7b3df6a2767b2b28352b0befa Mon Sep 17 00:00:00 2001 From: Cristian Partica Date: Mon, 19 Dec 2016 17:52:27 -0600 Subject: [PATCH 18/39] MAGETWO-58924: SQL error wait timeout error when saving categories - fix integration test --- .../Model/CategoryUrlRewriteGeneratorTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGeneratorTest.php b/dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGeneratorTest.php index 93fe95f83cbc9..5d3ec05538502 100644 --- a/dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGeneratorTest.php +++ b/dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGeneratorTest.php @@ -76,12 +76,12 @@ public function testGenerateUrlRewritesWithSaveHistory() ['new-url.html', 'catalog/category/view/id/3', 1, 0], ['new-url/category-1-1.html', 'catalog/category/view/id/4', 1, 0], ['new-url/category-1-1/category-1-1-1.html', 'catalog/category/view/id/5', 1, 0], - ['category-1.html', 'new-url.html', 0, OptionProvider::PERMANENT], - ['category-1/category-1-1.html', 'new-url/category-1-1.html', 0, OptionProvider::PERMANENT], + ['category-1.html', 'new-url.html', 1, OptionProvider::PERMANENT], + ['category-1/category-1-1.html', 'new-url/category-1-1.html', 1, OptionProvider::PERMANENT], [ 'category-1/category-1-1/category-1-1-1.html', 'new-url/category-1-1/category-1-1-1.html', - 0, + 1, OptionProvider::PERMANENT ], ]; From 81730d558e72727487f0d46a362b947493895b9c Mon Sep 17 00:00:00 2001 From: Venkata Uppalapati Date: Mon, 19 Dec 2016 18:12:49 -0600 Subject: [PATCH 19/39] MAGETWO-58924: SQL error wait timeout error when saving categories - adding unit tests for UrlRewritesSet and TemporaryTableService --- .../Test/Unit/Model/UrlRewritesSetTest.php | 157 +++++++++++++++ .../Test/Unit/TemporaryTableServiceTest.php | 180 ++++++++++++++++++ 2 files changed, 337 insertions(+) create mode 100644 app/code/Magento/UrlRewrite/Test/Unit/Model/UrlRewritesSetTest.php create mode 100644 lib/internal/Magento/Framework/DB/Test/Unit/TemporaryTableServiceTest.php diff --git a/app/code/Magento/UrlRewrite/Test/Unit/Model/UrlRewritesSetTest.php b/app/code/Magento/UrlRewrite/Test/Unit/Model/UrlRewritesSetTest.php new file mode 100644 index 0000000000000..c52795f7b0fc3 --- /dev/null +++ b/app/code/Magento/UrlRewrite/Test/Unit/Model/UrlRewritesSetTest.php @@ -0,0 +1,157 @@ +urlRewritesSet = (new ObjectManager($this))->getObject( + UrlRewritesSet::class, + [] + ); + } + + /** + * Run test merge method + * + * @param array urlRewriteMockArray + * @param String expectedData + * @param int arrayCount + * @dataProvider getMergeTestParameters + * @return void + */ + public function testMerge($urlRewriteMockArray, $expectedData, $arrayCount) + { + $this->urlRewritesSet->merge($urlRewriteMockArray); + $this->assertEquals($expectedData, $this->urlRewritesSet->getData()); + $this->assertCount($arrayCount, $this->urlRewritesSet->getData()); + } + + /** + * Run test getData method when data is Empty + * + * @return void + */ + public function testGetDataWhenEmpty() + { + $this->assertEmpty($this->urlRewritesSet->getData()); + } + + /** + * Run test getData method when data is not empty + * + * @return void + */ + public function testGetDataWhenNotEmpty() + { + $data = new \ReflectionProperty($this->urlRewritesSet, 'data'); + $data->setAccessible(true); + $data->setValue($this->urlRewritesSet, [new UrlRewrite()]); + $data->setAccessible(false); + $this->assertNotEmpty($this->urlRewritesSet->getData()); + } + + /** + * Run test resetData method + */ + public function testResetData() + { + $data = new \ReflectionProperty($this->urlRewritesSet, 'data'); + $data->setAccessible(true); + $data->setValue($this->urlRewritesSet, [new UrlRewrite()]); + $data->setAccessible(false); + $this->urlRewritesSet->resetData(); + $this->assertEmpty($this->urlRewritesSet->getData()); + } + + /** + * + * @return array + */ + public function getMergeTestParameters() + { + $urlRewriteMock1 = $this->getMock(UrlRewrite::class, [], [], '', false); + + $requestPathForMock2 = 'magento.tst/products/simpleproduct2'; + $storeIdForMock2 = 'testStore2'; + $urlRewriteMock2 = $this->getMock(UrlRewrite::class, [], [], '', false); + + $urlRewriteMock2->expects($this->atLeastOnce()) + ->method('getRequestPath') + ->willReturn($requestPathForMock2); + + $urlRewriteMock2->expects($this->atLeastOnce()) + ->method('getStoreId') + ->willReturn($storeIdForMock2); + + $requestPathForMock3 = 'magento.tst/products/simpleproduct3'; + $storeIdForMock3 = 'testStore3'; + $urlRewriteMock3 = $this->getMock(UrlRewrite::class, [], [], '', false); + + $urlRewriteMock3->expects($this->atLeastOnce()) + ->method('getRequestPath') + ->willReturn($requestPathForMock3); + + $urlRewriteMock3->expects($this->atLeastOnce()) + ->method('getStoreId') + ->willReturn($storeIdForMock3); + + return [ + [ + [], + [], + 0 + ], + [ + [$urlRewriteMock1], + [$urlRewriteMock1], + 1 + ], + [ + [ + $urlRewriteMock1, + $urlRewriteMock2, + $urlRewriteMock2 + ], + [ + $urlRewriteMock1, + $requestPathForMock2 . '_' . $storeIdForMock2 => $urlRewriteMock2 + ], + 2 + ], + [ + [ + $urlRewriteMock1, + $urlRewriteMock2, + $urlRewriteMock3 + ], + [ + $urlRewriteMock1, + $requestPathForMock2 . '_' . $storeIdForMock2 => $urlRewriteMock2, + $requestPathForMock3 . '_' . $storeIdForMock3 => $urlRewriteMock3 + ], + 3 + ], + ]; + } + +} diff --git a/lib/internal/Magento/Framework/DB/Test/Unit/TemporaryTableServiceTest.php b/lib/internal/Magento/Framework/DB/Test/Unit/TemporaryTableServiceTest.php new file mode 100644 index 0000000000000..3218111e19967 --- /dev/null +++ b/lib/internal/Magento/Framework/DB/Test/Unit/TemporaryTableServiceTest.php @@ -0,0 +1,180 @@ +adapterMock = $this->getMock(AdapterInterface::class); + $this->selectMock = $this->getMock(Select::class, [], [], '', false); + $this->randomMock = $this->getMock(Random::class, [], [], '', false); + $this->temporaryTableService = (new ObjectManager($this))->getObject( + TemporaryTableService::class, + [ + 'random' => $this->randomMock + ] + ); + } + + /** + * Run test createFromSelect method + * + * @param array $indexes + * @param string $expectedSelect + * @dataProvider getCreateFromSelectTestParameters + * @return void + */ + public function testCreateFromSelect($indexes, $expectedSelect) + { + $selectString = 'select * from sometable'; + $random = 'random_table'; + + $this->randomMock->expects($this->once()) + ->method('getUniqueHash') + ->willReturn($random); + + $this->adapterMock->expects($this->once()) + ->method('query') + ->with($expectedSelect) + ->willReturnSelf(); + + $this->adapterMock->expects($this->once()) + ->method('query') + ->willReturnSelf(); + + $this->adapterMock->expects($this->any()) + ->method('quoteIdentifier') + ->willReturnArgument(0); + + $this->selectMock->expects($this->once()) + ->method('getBind') + ->willReturn(['bind']); + + $this->selectMock->expects($this->any()) + ->method('__toString') + ->willReturn($selectString); + + $this->assertEquals( + $random, + $this->temporaryTableService->createFromSelect( + $this->selectMock, + $this->adapterMock, + $indexes + ) + ); + } + + /** + * Run test dropTable method when createdTables array of TemporaryTableService is empty + * + * @return void + */ + public function testDropTableWhenCreatedTablesArrayIsEmpty() + { + $this->assertFalse($this->temporaryTableService->dropTable('tmp_select_table')); + } + + /** + * Run test dropTable method when data exists in createdTables array of TemporaryTableService + * + * @param string $tableName + * @param bool $assertion + * + * @dataProvider getDropTableTestParameters + * @return void + */ + public function testDropTableWhenCreatedTablesArrayNotEmpty($tableName, $assertion) + { + $createdTables = new \ReflectionProperty($this->temporaryTableService, 'createdTables'); + $createdTables->setAccessible(true); + $createdTables->setValue($this->temporaryTableService, ['tmp_select_table' => $this->adapterMock]); + $createdTables->setAccessible(false); + + $this->adapterMock->expects($this->any()) + ->method('dropTemporaryTable') + ->willReturn(true); + + $this->assertEquals($this->temporaryTableService->dropTable($tableName), $assertion); + } + + /** + * @return array + */ + public function getCreateFromSelectTestParameters() + { + return [ + [ + ['PRIMARY' => ['primary_column_name']], + 'CREATE TEMPORARY TABLE random_table (PRIMARY KEY(primary_column_name)) ENGINE=INNODB IGNORE ' + . '(select * from sometable)' + ], + [ + ['UNQ_INDX' => ['column1', 'column2']], + 'CREATE TEMPORARY TABLE random_table (UNIQUE UNQ_INDX(column1,column2)) ENGINE=INNODB IGNORE ' + . '(select * from sometable)' + ], + [ + ['OTH_INDX' => ['column3', 'column4']], + 'CREATE TEMPORARY TABLE random_table (INDEX OTH_INDX USING HASH(column3,column4)) ENGINE=INNODB IGNORE ' + . '(select * from sometable)' + ], + [ + [ + 'PRIMARY' => ['primary_column_name'], + 'OTH_INDX' => ['column3', 'column4'], + 'UNQ_INDX' => ['column1', 'column2'] + ], + 'CREATE TEMPORARY TABLE random_table ' + . '(PRIMARY KEY(primary_column_name),' + . 'INDEX OTH_INDX USING HASH(column3,column4),UNIQUE UNQ_INDX(column1,column2)) ENGINE=INNODB IGNORE ' + . '(select * from sometable)' + ], + ]; + } + + /** + * @return array + */ + public function getDropTableTestParameters() + { + return [ + ['tmp_select_table_1', false], + ['tmp_select_table', true], + ]; + } +} From 71487a7d558c4bf7f9757591ffb7c8252c697460 Mon Sep 17 00:00:00 2001 From: Cristian Partica Date: Mon, 19 Dec 2016 21:42:06 -0600 Subject: [PATCH 20/39] MAGETWO-58924: SQL error wait timeout error when saving categories - fix static test --- .../UrlRewrite/Test/Unit/Model/UrlRewritesSetTest.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/app/code/Magento/UrlRewrite/Test/Unit/Model/UrlRewritesSetTest.php b/app/code/Magento/UrlRewrite/Test/Unit/Model/UrlRewritesSetTest.php index c52795f7b0fc3..3b2195d489ea8 100644 --- a/app/code/Magento/UrlRewrite/Test/Unit/Model/UrlRewritesSetTest.php +++ b/app/code/Magento/UrlRewrite/Test/Unit/Model/UrlRewritesSetTest.php @@ -11,7 +11,6 @@ class UrlRewritesSetTest extends \PHPUnit_Framework_TestCase { - /** * @var UrlRewritesSet|\PHPUnit_Framework_MockObject_MockObject */ @@ -33,9 +32,9 @@ protected function setUp() /** * Run test merge method * - * @param array urlRewriteMockArray - * @param String expectedData - * @param int arrayCount + * @param array $urlRewriteMockArray + * @param String $expectedData + * @param int $arrayCount * @dataProvider getMergeTestParameters * @return void */ @@ -72,6 +71,8 @@ public function testGetDataWhenNotEmpty() /** * Run test resetData method + * + * @return void */ public function testResetData() { @@ -84,6 +85,7 @@ public function testResetData() } /** + * Data provider for testMerge * * @return array */ @@ -153,5 +155,4 @@ public function getMergeTestParameters() ], ]; } - } From f4fb3fcdb0e2c7c1d018dd6ac87d3b61fe0c6f99 Mon Sep 17 00:00:00 2001 From: Cristian Partica Date: Wed, 28 Dec 2016 17:07:47 -0600 Subject: [PATCH 21/39] MAGETWO-58924: SQL error wait timeout error when saving categories - use clone instead of non shared object because of performance improvement --- .../Category/ChildrenUrlRewriteGenerator.php | 19 ++++++----- .../CurrentUrlRewritesRegenerator.php | 21 ++++++------ .../Model/CategoryUrlRewriteGenerator.php | 30 +++++++++-------- .../Product/CurrentUrlRewritesRegenerator.php | 21 ++++++------ .../Model/ProductScopeRewriteGenerator.php | 32 +++++++++++-------- .../Observer/AfterImportDataObserver.php | 23 +++++++------ .../Observer/UrlRewriteHandler.php | 30 +++++++++-------- .../ChildrenUrlRewriteGeneratorTest.php | 10 +++++- .../CurrentUrlRewritesRegeneratorTest.php | 16 +++++++--- .../Model/CategoryUrlRewriteGeneratorTest.php | 10 +++++- .../CurrentUrlRewritesRegeneratorTest.php | 14 ++++++-- .../ProductScopeRewriteGeneratorTest.php | 10 +++++- .../Observer/AfterImportDataObserverTest.php | 10 +++++- app/code/Magento/UrlRewrite/etc/di.xml | 1 - .../Model/CategoryUrlRewriteGeneratorTest.php | 6 ++-- 15 files changed, 162 insertions(+), 91 deletions(-) diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Category/ChildrenUrlRewriteGenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/Category/ChildrenUrlRewriteGenerator.php index 14215d6231041..6080c14ee2469 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Category/ChildrenUrlRewriteGenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Category/ChildrenUrlRewriteGenerator.php @@ -8,7 +8,7 @@ use Magento\Catalog\Model\Category; use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGeneratorFactory; use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator; -use Magento\UrlRewrite\Model\UrlRewritesSet; +use Magento\UrlRewrite\Model\UrlRewritesSetFactory; use Magento\Framework\App\ObjectManager; class ChildrenUrlRewriteGenerator @@ -20,21 +20,23 @@ class ChildrenUrlRewriteGenerator protected $categoryUrlRewriteGeneratorFactory; /** @var \Magento\UrlRewrite\Model\UrlRewritesSet */ - private $urlRewritesSet; + private $urlRewritesSetPlaceHolder; /** * @param \Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider $childrenCategoriesProvider * @param \Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGeneratorFactory $categoryUrlRewriteGeneratorFactory - * @param UrlRewritesSet|null $urlRewritesSet + * @param \Magento\UrlRewrite\Model\UrlRewritesSetFactory|null $urlRewritesSetFactory */ public function __construct( ChildrenCategoriesProvider $childrenCategoriesProvider, CategoryUrlRewriteGeneratorFactory $categoryUrlRewriteGeneratorFactory, - UrlRewritesSet $urlRewritesSet = null + UrlRewritesSetFactory $urlRewritesSetFactory = null ) { $this->childrenCategoriesProvider = $childrenCategoriesProvider; $this->categoryUrlRewriteGeneratorFactory = $categoryUrlRewriteGeneratorFactory; - $this->urlRewritesSet = $urlRewritesSet ?: ObjectManager::getInstance()->get(UrlRewritesSet::class); + $urlRewritesSetFactory = $urlRewritesSetFactory ?: ObjectManager::getInstance() + ->get(UrlRewritesSetFactory::class); + $this->urlRewritesSetPlaceHolder = $urlRewritesSetFactory->create(); } /** @@ -47,18 +49,19 @@ public function __construct( */ public function generate($storeId, Category $category, $rootCategoryId = null) { + $urlRewritesSet = clone $this->urlRewritesSetPlaceHolder; foreach ($this->childrenCategoriesProvider->getChildren($category, true) as $childCategory) { $childCategory->setStoreId($storeId); $childCategory->setData('save_rewrites_history', $category->getData('save_rewrites_history')); /** @var CategoryUrlRewriteGenerator $categoryUrlRewriteGenerator */ $categoryUrlRewriteGenerator = $this->categoryUrlRewriteGeneratorFactory->create(); - $this->urlRewritesSet->merge( + $urlRewritesSet->merge( $categoryUrlRewriteGenerator->generate($childCategory, false, $rootCategoryId) ); } - $result = $this->urlRewritesSet->getData(); - $this->urlRewritesSet->resetData(); + $result = $urlRewritesSet->getData(); + $urlRewritesSet->resetData(); return $result; } } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php index becdab0e8c19b..6988efcabbba0 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php @@ -14,7 +14,7 @@ use Magento\UrlRewrite\Service\V1\Data\UrlRewrite; use Magento\CatalogUrlRewrite\Model\Map\UrlRewriteMap; use Magento\Framework\App\ObjectManager; -use Magento\UrlRewrite\Model\UrlRewritesSet; +use Magento\UrlRewrite\Model\UrlRewritesSetFactory; class CurrentUrlRewritesRegenerator { @@ -37,28 +37,30 @@ class CurrentUrlRewritesRegenerator private $urlRewriteMap; /** @var \Magento\UrlRewrite\Model\UrlRewritesSet */ - private $urlRewritesSet; + private $urlRewritesSetPlaceHolder; /** * @param \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator * @param \Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory $urlRewriteFactory * @param UrlFinderInterface $urlFinder * @param UrlRewriteMap|null $urlRewriteMap - * @param \Magento\UrlRewrite\Model\UrlRewritesSet|null $urlRewritesSet + * @param \Magento\UrlRewrite\Model\UrlRewritesSetFactory|null $urlRewritesSetFactory */ public function __construct( CategoryUrlPathGenerator $categoryUrlPathGenerator, UrlRewriteFactory $urlRewriteFactory, UrlFinderInterface $urlFinder, UrlRewriteMap $urlRewriteMap = null, - UrlRewritesSet $urlRewritesSet = null + UrlRewritesSetFactory $urlRewritesSetFactory = null ) { $this->categoryUrlPathGenerator = $categoryUrlPathGenerator; $this->urlRewriteFactory = $urlRewriteFactory; $this->urlRewritePlaceholder = $urlRewriteFactory->create(); $this->urlFinder = $urlFinder; $this->urlRewriteMap = $urlRewriteMap ?: ObjectManager::getInstance()->get(UrlRewriteMap::class); - $this->urlRewritesSet = $urlRewritesSet ?: ObjectManager::getInstance()->get(UrlRewritesSet::class); + $urlRewritesSetFactory = $urlRewritesSetFactory ?: ObjectManager::getInstance() + ->get(UrlRewritesSetFactory::class); + $this->urlRewritesSetPlaceHolder = $urlRewritesSetFactory->create(); } /** @@ -71,6 +73,7 @@ public function __construct( */ public function generate($storeId, Category $category, $rootCategoryId = null) { + $urlRewritesSet = clone $this->urlRewritesSetPlaceHolder; $currentUrlRewrites = $this->urlRewriteMap->getByIdentifiers( $category->getEntityId(), $storeId, @@ -79,15 +82,15 @@ public function generate($storeId, Category $category, $rootCategoryId = null) ); foreach ($currentUrlRewrites as $rewrite) { - $this->urlRewritesSet->merge( + $urlRewritesSet->merge( $rewrite->getIsAutogenerated() ? $this->generateForAutogenerated($rewrite, $storeId, $category) : $this->generateForCustom($rewrite, $storeId, $category) ); } - $result = $this->urlRewritesSet->getData(); - $this->urlRewritesSet->resetData(); + $result = $urlRewritesSet->getData(); + $urlRewritesSet->resetData(); return $result; } @@ -109,7 +112,7 @@ protected function generateForAutogenerated($url, $storeId, Category $category) ->setTargetPath($targetPath) ->setRedirectType(OptionProvider::PERMANENT) ->setStoreId($storeId) - ->setIsAutogenerated(1); + ->setIsAutogenerated(0); return [$generatedUrl]; } } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGenerator.php index 8d8f4f2895ccb..fcf434fdb53de 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGenerator.php @@ -12,8 +12,8 @@ use Magento\CatalogUrlRewrite\Service\V1\StoreViewService; use Magento\Store\Model\Store; use Magento\Catalog\Api\CategoryRepositoryInterface; -use Magento\UrlRewrite\Model\UrlRewritesSet; use Magento\Framework\App\ObjectManager; +use Magento\UrlRewrite\Model\UrlRewritesSetFactory; class CategoryUrlRewriteGenerator { @@ -33,7 +33,7 @@ class CategoryUrlRewriteGenerator protected $childrenUrlRewriteGenerator; /** @var \Magento\UrlRewrite\Model\UrlRewritesSet */ - private $urlRewritesSet; + private $urlRewritesSetPlaceHolder; /** * @var bool @@ -46,7 +46,7 @@ class CategoryUrlRewriteGenerator * @param \Magento\CatalogUrlRewrite\Model\Category\ChildrenUrlRewriteGenerator $childrenUrlRewriteGenerator * @param \Magento\CatalogUrlRewrite\Service\V1\StoreViewService $storeViewService * @param \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository - * @param \Magento\UrlRewrite\Model\UrlRewritesSet|null $urlRewritesSet + * @param \Magento\UrlRewrite\Model\UrlRewritesSetFactory|null $urlRewritesSetFactory */ public function __construct( CanonicalUrlRewriteGenerator $canonicalUrlRewriteGenerator, @@ -54,14 +54,16 @@ public function __construct( ChildrenUrlRewriteGenerator $childrenUrlRewriteGenerator, StoreViewService $storeViewService, CategoryRepositoryInterface $categoryRepository, - UrlRewritesSet $urlRewritesSet = null + UrlRewritesSetFactory $urlRewritesSetFactory = null ) { $this->storeViewService = $storeViewService; $this->canonicalUrlRewriteGenerator = $canonicalUrlRewriteGenerator; $this->childrenUrlRewriteGenerator = $childrenUrlRewriteGenerator; $this->currentUrlRewritesRegenerator = $currentUrlRewritesRegenerator; $this->categoryRepository = $categoryRepository; - $this->urlRewritesSet = $urlRewritesSet ?: ObjectManager::getInstance()->get(UrlRewritesSet::class); + $urlRewritesSetFactory = $urlRewritesSetFactory ?: ObjectManager::getInstance() + ->get(UrlRewritesSetFactory::class); + $this->urlRewritesSetPlaceHolder = $urlRewritesSetFactory->create(); } /** @@ -94,17 +96,18 @@ public function generate(Category $category, $overrideStoreUrls = false, $rootCa */ protected function generateForGlobalScope(Category $category, $overrideStoreUrls, $rootCategoryId = null) { + $urlRewritesSet = clone $this->urlRewritesSetPlaceHolder; $categoryId = $category->getId(); foreach ($category->getStoreIds() as $storeId) { if (!$this->isGlobalScope($storeId) && $this->isOverrideUrlsForStore($storeId, $categoryId, $overrideStoreUrls) ) { $this->updateCategoryUrlForStore($category, $storeId); - $this->urlRewritesSet->merge($this->generateForSpecificStoreView($category, $storeId, $rootCategoryId)); + $urlRewritesSet->merge($this->generateForSpecificStoreView($category, $storeId, $rootCategoryId)); } } - $result = $this->urlRewritesSet->getData(); - $this->urlRewritesSet->resetData(); + $result = $urlRewritesSet->getData(); + $urlRewritesSet->resetData(); return $result; } @@ -162,17 +165,18 @@ protected function isGlobalScope($storeId) */ protected function generateForSpecificStoreView(Category $category, $storeId, $rootCategoryId = null) { - $this->urlRewritesSet->merge( + $urlRewritesSet = clone $this->urlRewritesSetPlaceHolder; + $urlRewritesSet->merge( $this->canonicalUrlRewriteGenerator->generate($storeId, $category) ); - $this->urlRewritesSet->merge( + $urlRewritesSet->merge( $this->childrenUrlRewriteGenerator->generate($storeId, $category, $rootCategoryId) ); - $this->urlRewritesSet->merge( + $urlRewritesSet->merge( $this->currentUrlRewritesRegenerator->generate($storeId, $category, $rootCategoryId) ); - $result = $this->urlRewritesSet->getData(); - $this->urlRewritesSet->resetData(); + $result = $urlRewritesSet->getData(); + $urlRewritesSet->resetData(); return $result; } } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php index 94acb5b592ba2..1f85f5a3d2766 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php @@ -16,7 +16,7 @@ use Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory; use Magento\CatalogUrlRewrite\Model\Map\UrlRewriteMap; use Magento\Framework\App\ObjectManager; -use Magento\UrlRewrite\Model\UrlRewritesSet; +use Magento\UrlRewrite\Model\UrlRewritesSetFactory; /** * @SuppressWarnings(PHPMD.CouplingBetweenObjects) @@ -42,28 +42,30 @@ class CurrentUrlRewritesRegenerator private $urlRewriteMap; /** @var \Magento\UrlRewrite\Model\UrlRewritesSet */ - private $urlRewritesSet; + private $urlRewritesSetPlaceHolder; /** * @param UrlFinderInterface $urlFinder * @param ProductUrlPathGenerator $productUrlPathGenerator * @param UrlRewriteFactory $urlRewriteFactory * @param UrlRewriteMap|null $urlRewriteMap - * @param \Magento\UrlRewrite\Model\UrlRewritesSet|null $urlRewritesSet + * @param \Magento\UrlRewrite\Model\UrlRewritesSetFactory|null $urlRewritesSetFactory */ public function __construct( UrlFinderInterface $urlFinder, ProductUrlPathGenerator $productUrlPathGenerator, UrlRewriteFactory $urlRewriteFactory, UrlRewriteMap $urlRewriteMap = null, - UrlRewritesSet $urlRewritesSet = null + UrlRewritesSetFactory $urlRewritesSetFactory = null ) { $this->urlFinder = $urlFinder; $this->productUrlPathGenerator = $productUrlPathGenerator; $this->urlRewriteFactory = $urlRewriteFactory; $this->urlRewritePlaceholder = $urlRewriteFactory->create(); $this->urlRewriteMap = $urlRewriteMap ?: ObjectManager::getInstance()->get(UrlRewriteMap::class); - $this->urlRewritesSet = $urlRewritesSet ?: ObjectManager::getInstance()->get(UrlRewritesSet::class); + $urlRewritesSetFactory = $urlRewritesSetFactory ?: ObjectManager::getInstance() + ->get(UrlRewritesSetFactory::class); + $this->urlRewritesSetPlaceHolder = $urlRewritesSetFactory->create(); } /** @@ -77,6 +79,7 @@ public function __construct( */ public function generate($storeId, Product $product, ObjectRegistry $productCategories, $rootCategoryId = null) { + $urlRewritesSet = clone $this->urlRewritesSetPlaceHolder; $currentUrlRewrites = $this->urlRewriteMap->getByIdentifiers( $product->getEntityId(), $storeId, @@ -89,15 +92,15 @@ public function generate($storeId, Product $product, ObjectRegistry $productCate if ($category === false) { continue; } - $this->urlRewritesSet->merge( + $urlRewritesSet->merge( $currentUrlRewrite->getIsAutogenerated() ? $this->generateForAutogenerated($currentUrlRewrite, $storeId, $category, $product) : $this->generateForCustom($currentUrlRewrite, $storeId, $category, $product) ); } - $result = $this->urlRewritesSet->getData(); - $this->urlRewritesSet->resetData(); + $result = $urlRewritesSet->getData(); + $urlRewritesSet->resetData(); return $result; } @@ -121,7 +124,7 @@ protected function generateForAutogenerated(UrlRewrite $url, $storeId, $category ->setRedirectType(OptionProvider::PERMANENT) ->setStoreId($storeId) ->setDescription($url->getDescription()) - ->setIsAutogenerated(1) + ->setIsAutogenerated(0) ->setMetadata($url->getMetadata()); return [$generatedUrl]; } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/ProductScopeRewriteGenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/ProductScopeRewriteGenerator.php index 4ed01e4909c7f..cc64dc99962d3 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/ProductScopeRewriteGenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/ProductScopeRewriteGenerator.php @@ -14,7 +14,7 @@ use Magento\CatalogUrlRewrite\Service\V1\StoreViewService; use Magento\Store\Model\Store; use Magento\Store\Model\StoreManagerInterface; -use Magento\UrlRewrite\Model\UrlRewritesSet; +use Magento\UrlRewrite\Model\UrlRewritesSetFactory; use Magento\Framework\App\ObjectManager; /** @@ -59,7 +59,7 @@ class ProductScopeRewriteGenerator private $canonicalUrlRewriteGenerator; /** @var \Magento\UrlRewrite\Model\UrlRewritesSet */ - private $urlRewritesSet; + private $urlRewritesSetPlaceHolder; /** * @param StoreViewService $storeViewService @@ -69,7 +69,7 @@ class ProductScopeRewriteGenerator * @param CategoriesUrlRewriteGenerator $categoriesUrlRewriteGenerator * @param CurrentUrlRewritesRegenerator $currentUrlRewritesRegenerator * @param AnchorUrlRewriteGenerator $anchorUrlRewriteGenerator - * @param UrlRewritesSet|null $urlRewritesSet + * @param \Magento\UrlRewrite\Model\UrlRewritesSetFactory|null $urlRewritesSetFactory */ public function __construct( StoreViewService $storeViewService, @@ -79,7 +79,7 @@ public function __construct( CategoriesUrlRewriteGenerator $categoriesUrlRewriteGenerator, CurrentUrlRewritesRegenerator $currentUrlRewritesRegenerator, AnchorUrlRewriteGenerator $anchorUrlRewriteGenerator, - UrlRewritesSet $urlRewritesSet = null + UrlRewritesSetFactory $urlRewritesSetFactory = null ) { $this->storeViewService = $storeViewService; $this->storeManager = $storeManager; @@ -88,7 +88,9 @@ public function __construct( $this->categoriesUrlRewriteGenerator = $categoriesUrlRewriteGenerator; $this->currentUrlRewritesRegenerator = $currentUrlRewritesRegenerator; $this->anchorUrlRewriteGenerator = $anchorUrlRewriteGenerator; - $this->urlRewritesSet = $urlRewritesSet ?: ObjectManager::getInstance()->get(UrlRewritesSet::class); + $urlRewritesSetFactory = $urlRewritesSetFactory ?: ObjectManager::getInstance() + ->get(UrlRewritesSetFactory::class); + $this->urlRewritesSetPlaceHolder = $urlRewritesSetFactory->create(); } /** @@ -113,6 +115,7 @@ public function isGlobalScope($storeId) public function generateForGlobalScope($productCategories, Product $product, $rootCategoryId = null) { $productId = $product->getEntityId(); + $urlRewritesSet = clone $this->urlRewritesSetPlaceHolder; foreach ($product->getStoreIds() as $id) { if (!$this->isGlobalScope($id) && @@ -121,14 +124,14 @@ public function generateForGlobalScope($productCategories, Product $product, $ro $productId, Product::ENTITY )) { - $this->urlRewritesSet->merge( + $urlRewritesSet->merge( $this->generateForSpecificStoreView($id, $productCategories, $product, $rootCategoryId) ); } } - $result = $this->urlRewritesSet->getData(); - $this->urlRewritesSet->resetData(); + $result = $urlRewritesSet->getData(); + $urlRewritesSet->resetData(); return $result; } @@ -143,6 +146,7 @@ public function generateForGlobalScope($productCategories, Product $product, $ro */ public function generateForSpecificStoreView($storeId, $productCategories, Product $product, $rootCategoryId = null) { + $urlRewritesSet = clone $this->urlRewritesSetPlaceHolder; $categories = []; foreach ($productCategories as $category) { if ($this->isCategoryProperForGenerating($category, $storeId)) { @@ -151,13 +155,13 @@ public function generateForSpecificStoreView($storeId, $productCategories, Produ } $productCategories = $this->objectRegistryFactory->create(['entities' => $categories]); - $this->urlRewritesSet->merge( + $urlRewritesSet->merge( $this->canonicalUrlRewriteGenerator->generate($storeId, $product) ); - $this->urlRewritesSet->merge( + $urlRewritesSet->merge( $this->categoriesUrlRewriteGenerator->generate($storeId, $product, $productCategories) ); - $this->urlRewritesSet->merge( + $urlRewritesSet->merge( $this->currentUrlRewritesRegenerator->generate( $storeId, $product, @@ -165,12 +169,12 @@ public function generateForSpecificStoreView($storeId, $productCategories, Produ $rootCategoryId ) ); - $this->urlRewritesSet->merge( + $urlRewritesSet->merge( $this->anchorUrlRewriteGenerator->generate($storeId, $product, $productCategories) ); - $result = $this->urlRewritesSet->getData(); - $this->urlRewritesSet->resetData(); + $result = $urlRewritesSet->getData(); + $urlRewritesSet->resetData(); return $result; } diff --git a/app/code/Magento/CatalogUrlRewrite/Observer/AfterImportDataObserver.php b/app/code/Magento/CatalogUrlRewrite/Observer/AfterImportDataObserver.php index 065293a704135..2d15d4f627359 100644 --- a/app/code/Magento/CatalogUrlRewrite/Observer/AfterImportDataObserver.php +++ b/app/code/Magento/CatalogUrlRewrite/Observer/AfterImportDataObserver.php @@ -21,7 +21,7 @@ use Magento\Framework\Event\ObserverInterface; use Magento\Catalog\Model\Product\Visibility; use Magento\Framework\App\ObjectManager; -use Magento\UrlRewrite\Model\UrlRewritesSet; +use Magento\UrlRewrite\Model\UrlRewritesSetFactory; /** * Class AfterImportDataObserver @@ -100,7 +100,7 @@ class AfterImportDataObserver implements ObserverInterface ]; /** @var \Magento\UrlRewrite\Model\UrlRewritesSet */ - private $urlRewritesSet; + private $urlRewritesSetPlaceHolder; /** * @param \Magento\Catalog\Model\ProductFactory $catalogProductFactory @@ -111,7 +111,7 @@ class AfterImportDataObserver implements ObserverInterface * @param UrlPersistInterface $urlPersist * @param UrlRewriteFactory $urlRewriteFactory * @param UrlFinderInterface $urlFinder - * @param \Magento\UrlRewrite\Model\UrlRewritesSet|null $urlRewritesSet + * @param \Magento\UrlRewrite\Model\UrlRewritesSetFactory|null $urlRewritesSetFactory * @throws \InvalidArgumentException * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ @@ -124,7 +124,7 @@ public function __construct( UrlPersistInterface $urlPersist, UrlRewriteFactory $urlRewriteFactory, UrlFinderInterface $urlFinder, - UrlRewritesSet $urlRewritesSet = null + UrlRewritesSetFactory $urlRewritesSetFactory = null ) { $this->urlPersist = $urlPersist; $this->catalogProductFactory = $catalogProductFactory; @@ -134,7 +134,9 @@ public function __construct( $this->storeManager = $storeManager; $this->urlRewriteFactory = $urlRewriteFactory; $this->urlFinder = $urlFinder; - $this->urlRewritesSet = $urlRewritesSet ?: ObjectManager::getInstance()->get(UrlRewritesSet::class); + $urlRewritesSetFactory = $urlRewritesSetFactory ?: ObjectManager::getInstance() + ->get(UrlRewritesSetFactory::class); + $this->urlRewritesSetPlaceHolder = $urlRewritesSetFactory->create(); } /** @@ -267,16 +269,17 @@ protected function populateGlobalProduct($product) */ protected function generateUrls() { - $this->urlRewritesSet->merge($this->canonicalUrlRewriteGenerate()); - $this->urlRewritesSet->merge($this->categoriesUrlRewriteGenerate()); - $this->urlRewritesSet->merge($this->currentUrlRewritesRegenerate()); + $urlRewritesSet = clone $this->urlRewritesSetPlaceHolder; + $urlRewritesSet->merge($this->canonicalUrlRewriteGenerate()); + $urlRewritesSet->merge($this->categoriesUrlRewriteGenerate()); + $urlRewritesSet->merge($this->currentUrlRewritesRegenerate()); $this->productCategories = null; unset($this->products); $this->products = []; - $result = $this->urlRewritesSet->getData(); - $this->urlRewritesSet->resetData(); + $result = $urlRewritesSet->getData(); + $urlRewritesSet->resetData(); return $result; } diff --git a/app/code/Magento/CatalogUrlRewrite/Observer/UrlRewriteHandler.php b/app/code/Magento/CatalogUrlRewrite/Observer/UrlRewriteHandler.php index 92ceff1c434b1..4c52725e5e964 100644 --- a/app/code/Magento/CatalogUrlRewrite/Observer/UrlRewriteHandler.php +++ b/app/code/Magento/CatalogUrlRewrite/Observer/UrlRewriteHandler.php @@ -13,7 +13,7 @@ use Magento\Framework\Event\Observer as EventObserver; use Magento\UrlRewrite\Model\UrlPersistInterface; use Magento\UrlRewrite\Service\V1\Data\UrlRewrite; -use Magento\UrlRewrite\Model\UrlRewritesSet; +use Magento\UrlRewrite\Model\UrlRewritesSetFactory; class UrlRewriteHandler { @@ -41,7 +41,7 @@ class UrlRewriteHandler private $categoryBasedProductRewriteGenerator; /** @var \Magento\UrlRewrite\Model\UrlRewritesSet */ - private $urlRewritesSet; + private $urlRewritesSetPlaceHolder; /** * @param \Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider $childrenCategoriesProvider @@ -49,7 +49,7 @@ class UrlRewriteHandler * @param ProductUrlRewriteGenerator $productUrlRewriteGenerator * @param UrlPersistInterface $urlPersist * @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory - * @param \Magento\UrlRewrite\Model\UrlRewritesSet|null $urlRewritesSet + * @param \Magento\UrlRewrite\Model\UrlRewritesSetFactory|null $urlRewritesSetFactory */ public function __construct( \Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider $childrenCategoriesProvider, @@ -57,14 +57,16 @@ public function __construct( ProductUrlRewriteGenerator $productUrlRewriteGenerator, UrlPersistInterface $urlPersist, \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory, - UrlRewritesSet $urlRewritesSet = null + UrlRewritesSetFactory $urlRewritesSetFactory = null ) { $this->childrenCategoriesProvider = $childrenCategoriesProvider; $this->categoryUrlRewriteGenerator = $categoryUrlRewriteGenerator; $this->productUrlRewriteGenerator = $productUrlRewriteGenerator; $this->urlPersist = $urlPersist; $this->productCollectionFactory = $productCollectionFactory; - $this->urlRewritesSet = $urlRewritesSet ?: ObjectManager::getInstance()->get(UrlRewritesSet::class); + $urlRewritesSetFactory = $urlRewritesSetFactory ?: ObjectManager::getInstance() + ->get(UrlRewritesSetFactory::class); + $this->urlRewritesSetPlaceHolder = $urlRewritesSetFactory->create(); } /** @@ -75,6 +77,7 @@ public function __construct( */ public function generateProductUrlRewrites(Category $category) { + $urlRewritesSet = clone $this->urlRewritesSetPlaceHolder; $this->isSkippedProduct = []; $saveRewriteHistory = $category->getData('save_rewrites_history'); $storeId = $category->getStoreId(); @@ -90,12 +93,12 @@ public function generateProductUrlRewrites(Category $category) foreach ($collection as $product) { $product->setStoreId($storeId); $product->setData('save_rewrites_history', $saveRewriteHistory); - $this->urlRewritesSet->merge( + $urlRewritesSet->merge( $this->productUrlRewriteGenerator->generate($product, $category->getEntityId()) ); } } else { - $this->urlRewritesSet->merge( + $urlRewritesSet->merge( $this->getCategoryProductsUrlRewrites( $category, $storeId, @@ -105,7 +108,7 @@ public function generateProductUrlRewrites(Category $category) ); } foreach ($this->childrenCategoriesProvider->getChildren($category, true) as $childCategory) { - $this->urlRewritesSet->merge( + $urlRewritesSet->merge( $this->getCategoryProductsUrlRewrites( $childCategory, $storeId, @@ -115,8 +118,8 @@ public function generateProductUrlRewrites(Category $category) ); } - $result = $this->urlRewritesSet->getData(); - $this->urlRewritesSet->resetData(); + $result = $urlRewritesSet->getData(); + $urlRewritesSet->resetData(); return $result; } @@ -133,6 +136,7 @@ public function getCategoryProductsUrlRewrites( $saveRewriteHistory, $rootCategoryId = null ) { + $urlRewritesSet = clone $this->urlRewritesSetPlaceHolder; /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection */ $productCollection = $category->getProductCollection() ->addAttributeToSelect('name') @@ -146,13 +150,13 @@ public function getCategoryProductsUrlRewrites( $this->isSkippedProduct[] = $product->getId(); $product->setStoreId($storeId); $product->setData('save_rewrites_history', $saveRewriteHistory); - $this->urlRewritesSet->merge( + $urlRewritesSet->merge( $this->getCategoryBasedProductRewriteGenerator()->generate($product, $category, $rootCategoryId) ); } - $result = $this->urlRewritesSet->getData(); - $this->urlRewritesSet->resetData(); + $result = $urlRewritesSet->getData(); + $urlRewritesSet->resetData(); return $result; } diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/ChildrenUrlRewriteGeneratorTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/ChildrenUrlRewriteGeneratorTest.php index 03812de828d52..bb50afca69853 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/ChildrenUrlRewriteGeneratorTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/ChildrenUrlRewriteGeneratorTest.php @@ -40,14 +40,22 @@ protected function setUp() $this->categoryUrlRewriteGenerator = $this->getMockBuilder( \Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator::class )->disableOriginalConstructor()->getMock(); + $urlRewritesSetFactory = $this->getMock( + \Magento\UrlRewrite\Model\UrlRewritesSetFactory::class, + ['create'], + [], + '', + false + ); $this->urlRewritesSet = new \Magento\UrlRewrite\Model\UrlRewritesSet; + $urlRewritesSetFactory->expects($this->once())->method('create')->willReturn($this->urlRewritesSet); $this->childrenUrlRewriteGenerator = (new ObjectManager($this))->getObject( \Magento\CatalogUrlRewrite\Model\Category\ChildrenUrlRewriteGenerator::class, [ 'childrenCategoriesProvider' => $this->childrenCategoriesProvider, 'categoryUrlRewriteGeneratorFactory' => $this->categoryUrlRewriteGeneratorFactory, - 'urlRewritesSet' => $this->urlRewritesSet + 'urlRewritesSetFactory' => $urlRewritesSetFactory ] ); } diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/CurrentUrlRewritesRegeneratorTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/CurrentUrlRewritesRegeneratorTest.php index ad16a9885282c..cae82e325ccc5 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/CurrentUrlRewritesRegeneratorTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/CurrentUrlRewritesRegeneratorTest.php @@ -50,14 +50,22 @@ protected function setUp() ->disableOriginalConstructor()->getMock(); $this->urlRewriteFactory->expects($this->once())->method('create') ->willReturn($this->urlRewrite); - + $urlRewritesSetFactory = $this->getMock( + \Magento\UrlRewrite\Model\UrlRewritesSetFactory::class, + ['create'], + [], + '', + false + ); $this->urlRewritesSet = new \Magento\UrlRewrite\Model\UrlRewritesSet; - $this->currentUrlRewritesRegenerator = (new ObjectManager($this))->getObject( + $urlRewritesSetFactory->expects($this->once())->method('create')->willReturn($this->urlRewritesSet); + + $this->currentUrlRewritesRegenerator = (new ObjectManager($this))->getObject( \Magento\CatalogUrlRewrite\Model\Category\CurrentUrlRewritesRegenerator::class, [ 'categoryUrlPathGenerator' => $this->categoryUrlPathGenerator, 'urlRewriteFactory' => $this->urlRewriteFactory, - 'urlRewritesSet' => $this->urlRewritesSet, + 'urlRewritesSetFactory' => $urlRewritesSetFactory, 'urlRewriteMap' => $this->urlRewriteMap ] ); @@ -128,7 +136,7 @@ public function testIsAutogenerated() $this->categoryUrlPathGenerator->expects($this->once())->method('getUrlPathWithSuffix') ->will($this->returnValue($targetPath)); - $this->prepareUrlRewriteMock($storeId, $categoryId, $requestPath, $targetPath, OptionProvider::PERMANENT, 1); + $this->prepareUrlRewriteMock($storeId, $categoryId, $requestPath, $targetPath, OptionProvider::PERMANENT, 0); $this->assertEquals( ['autogenerated.html_2' => $this->urlRewrite], diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/CategoryUrlRewriteGeneratorTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/CategoryUrlRewriteGeneratorTest.php index e6c035334c808..9edbf862ebee7 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/CategoryUrlRewriteGeneratorTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/CategoryUrlRewriteGeneratorTest.php @@ -55,7 +55,15 @@ protected function setUp() ->disableOriginalConstructor()->getMock(); $this->category = $this->getMock(\Magento\Catalog\Model\Category::class, [], [], '', false); $this->categoryRepository = $this->getMock(\Magento\Catalog\Api\CategoryRepositoryInterface::class); + $urlRewritesSetFactory = $this->getMock( + \Magento\UrlRewrite\Model\UrlRewritesSetFactory::class, + ['create'], + [], + '', + false + ); $this->urlRewritesSet = new \Magento\UrlRewrite\Model\UrlRewritesSet; + $urlRewritesSetFactory->expects($this->once())->method('create')->willReturn($this->urlRewritesSet); $this->categoryUrlRewriteGenerator = (new ObjectManager($this))->getObject( \Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator::class, @@ -65,7 +73,7 @@ protected function setUp() 'currentUrlRewritesRegenerator' => $this->currentUrlRewritesRegenerator, 'storeViewService' => $this->storeViewService, 'categoryRepository' => $this->categoryRepository, - 'urlRewritesSet' => $this->urlRewritesSet + 'urlRewritesSetFactory' => $urlRewritesSetFactory ] ); } diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Product/CurrentUrlRewritesRegeneratorTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Product/CurrentUrlRewritesRegeneratorTest.php index c8137f9440870..f093145cea5fd 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Product/CurrentUrlRewritesRegeneratorTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Product/CurrentUrlRewritesRegeneratorTest.php @@ -62,13 +62,21 @@ protected function setUp() $this->productUrlPathGenerator = $this->getMockBuilder( \Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator::class )->disableOriginalConstructor()->getMock(); + $urlRewritesSetFactory = $this->getMock( + \Magento\UrlRewrite\Model\UrlRewritesSetFactory::class, + ['create'], + [], + '', + false + ); $this->urlRewritesSet = new \Magento\UrlRewrite\Model\UrlRewritesSet; + $urlRewritesSetFactory->expects($this->once())->method('create')->willReturn($this->urlRewritesSet); $this->currentUrlRewritesRegenerator = (new ObjectManager($this))->getObject( \Magento\CatalogUrlRewrite\Model\Product\CurrentUrlRewritesRegenerator::class, [ 'productUrlPathGenerator' => $this->productUrlPathGenerator, 'urlRewriteFactory' => $this->urlRewriteFactory, - 'urlRewritesSet' => $this->urlRewritesSet, + 'urlRewritesSetFactory' => $urlRewritesSetFactory, 'urlRewriteMap' => $this->urlRewriteMap ] ); @@ -134,7 +142,7 @@ public function testIsAutogeneratedWithoutCategory() $productId, $requestPath, $targetPath, - $autoGenerated, + 0, OptionProvider::PERMANENT, [], $description @@ -177,7 +185,7 @@ public function testIsAutogeneratedWithCategory() $productId, $requestPath, $targetPath, - $autoGenerated, + 0, OptionProvider::PERMANENT, $metadata, $description diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/ProductScopeRewriteGeneratorTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/ProductScopeRewriteGeneratorTest.php index a7f2642fd970e..f3be4ee454a92 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/ProductScopeRewriteGeneratorTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/ProductScopeRewriteGeneratorTest.php @@ -64,7 +64,15 @@ public function setUp() $this->storeViewService = $this->getMockBuilder(\Magento\CatalogUrlRewrite\Service\V1\StoreViewService::class) ->disableOriginalConstructor()->getMock(); $this->storeManager = $this->getMock(StoreManagerInterface::class); + $urlRewritesSetFactory = $this->getMock( + \Magento\UrlRewrite\Model\UrlRewritesSetFactory::class, + ['create'], + [], + '', + false + ); $this->urlRewritesSet = new \Magento\UrlRewrite\Model\UrlRewritesSet; + $urlRewritesSetFactory->expects($this->once())->method('create')->willReturn($this->urlRewritesSet); $this->productScopeGenerator = (new ObjectManager($this))->getObject( \Magento\CatalogUrlRewrite\Model\ProductScopeRewriteGenerator::class, @@ -76,7 +84,7 @@ public function setUp() 'objectRegistryFactory' => $this->objectRegistryFactory, 'storeViewService' => $this->storeViewService, 'storeManager' => $this->storeManager, - 'urlRewritesSet' => $this->urlRewritesSet + 'urlRewritesSetFactory' => $urlRewritesSetFactory ] ); } diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Observer/AfterImportDataObserverTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Observer/AfterImportDataObserverTest.php index 271e4bf3d4fdb..f9cc23bceb887 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Observer/AfterImportDataObserverTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Observer/AfterImportDataObserverTest.php @@ -276,7 +276,15 @@ protected function setUp() ->expects($this->any()) ->method('getCategoryProcessor') ->willReturn($categoryProcessor); + $urlRewritesSetFactory = $this->getMock( + \Magento\UrlRewrite\Model\UrlRewritesSetFactory::class, + ['create'], + [], + '', + false + ); $this->urlRewritesSet = new \Magento\UrlRewrite\Model\UrlRewritesSet; + $urlRewritesSetFactory->expects($this->once())->method('create')->willReturn($this->urlRewritesSet); $this->objectManager = new ObjectManager($this); $this->import = $this->objectManager->getObject( @@ -290,7 +298,7 @@ protected function setUp() 'urlPersist' => $this->urlPersist, 'urlRewriteFactory' => $this->urlRewriteFactory, 'urlFinder' => $this->urlFinder, - 'urlRewritesSet' => $this->urlRewritesSet + 'urlRewritesSetFactory' => $urlRewritesSetFactory ] ); } diff --git a/app/code/Magento/UrlRewrite/etc/di.xml b/app/code/Magento/UrlRewrite/etc/di.xml index 8e426647f90c8..b7485a7ef2123 100644 --- a/app/code/Magento/UrlRewrite/etc/di.xml +++ b/app/code/Magento/UrlRewrite/etc/di.xml @@ -9,5 +9,4 @@ - diff --git a/dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGeneratorTest.php b/dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGeneratorTest.php index 5d3ec05538502..93fe95f83cbc9 100644 --- a/dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGeneratorTest.php +++ b/dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGeneratorTest.php @@ -76,12 +76,12 @@ public function testGenerateUrlRewritesWithSaveHistory() ['new-url.html', 'catalog/category/view/id/3', 1, 0], ['new-url/category-1-1.html', 'catalog/category/view/id/4', 1, 0], ['new-url/category-1-1/category-1-1-1.html', 'catalog/category/view/id/5', 1, 0], - ['category-1.html', 'new-url.html', 1, OptionProvider::PERMANENT], - ['category-1/category-1-1.html', 'new-url/category-1-1.html', 1, OptionProvider::PERMANENT], + ['category-1.html', 'new-url.html', 0, OptionProvider::PERMANENT], + ['category-1/category-1-1.html', 'new-url/category-1-1.html', 0, OptionProvider::PERMANENT], [ 'category-1/category-1-1/category-1-1-1.html', 'new-url/category-1-1/category-1-1-1.html', - 1, + 0, OptionProvider::PERMANENT ], ]; From d694a5398c31982136e780a7262faf3346da501b Mon Sep 17 00:00:00 2001 From: Cristian Partica Date: Wed, 28 Dec 2016 17:21:06 -0600 Subject: [PATCH 22/39] MAGETWO-58924: SQL error wait timeout error when saving categories - reduce dependency coupling count --- .../CurrentUrlRewritesRegenerator.php | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php index 6988efcabbba0..9263f48a22ec7 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php @@ -6,14 +6,12 @@ namespace Magento\CatalogUrlRewrite\Model\Category; use Magento\Catalog\Model\Category; -use Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator; use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator; use Magento\UrlRewrite\Model\OptionProvider; use Magento\UrlRewrite\Model\UrlFinderInterface; use Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory; use Magento\UrlRewrite\Service\V1\Data\UrlRewrite; use Magento\CatalogUrlRewrite\Model\Map\UrlRewriteMap; -use Magento\Framework\App\ObjectManager; use Magento\UrlRewrite\Model\UrlRewritesSetFactory; class CurrentUrlRewritesRegenerator @@ -42,23 +40,24 @@ class CurrentUrlRewritesRegenerator /** * @param \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator * @param \Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory $urlRewriteFactory - * @param UrlFinderInterface $urlFinder - * @param UrlRewriteMap|null $urlRewriteMap + * @param \Magento\UrlRewrite\Model\UrlFinderInterface $urlFinder + * @param \Magento\CatalogUrlRewrite\Model\Map\UrlRewriteMap|null $urlRewriteMap * @param \Magento\UrlRewrite\Model\UrlRewritesSetFactory|null $urlRewritesSetFactory */ public function __construct( - CategoryUrlPathGenerator $categoryUrlPathGenerator, - UrlRewriteFactory $urlRewriteFactory, - UrlFinderInterface $urlFinder, - UrlRewriteMap $urlRewriteMap = null, - UrlRewritesSetFactory $urlRewritesSetFactory = null + \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator, + \Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory $urlRewriteFactory, + \Magento\UrlRewrite\Model\UrlFinderInterface $urlFinder, + \Magento\CatalogUrlRewrite\Model\Map\UrlRewriteMap $urlRewriteMap = null, + \Magento\UrlRewrite\Model\UrlRewritesSetFactory $urlRewritesSetFactory = null ) { $this->categoryUrlPathGenerator = $categoryUrlPathGenerator; $this->urlRewriteFactory = $urlRewriteFactory; $this->urlRewritePlaceholder = $urlRewriteFactory->create(); $this->urlFinder = $urlFinder; - $this->urlRewriteMap = $urlRewriteMap ?: ObjectManager::getInstance()->get(UrlRewriteMap::class); - $urlRewritesSetFactory = $urlRewritesSetFactory ?: ObjectManager::getInstance() + $this->urlRewriteMap = $urlRewriteMap ?: \Magento\Framework\App\ObjectManager::getInstance() + ->get(UrlRewriteMap::class); + $urlRewritesSetFactory = $urlRewritesSetFactory ?: \Magento\Framework\App\ObjectManager::getInstance() ->get(UrlRewritesSetFactory::class); $this->urlRewritesSetPlaceHolder = $urlRewritesSetFactory->create(); } From a9bbd362da5d2bd243d8616897c3408d1db16f4b Mon Sep 17 00:00:00 2001 From: Cristian Partica Date: Wed, 28 Dec 2016 17:33:02 -0600 Subject: [PATCH 23/39] MAGETWO-58924: SQL error wait timeout error when saving categories - reduce dependency coupling count --- .../CurrentUrlRewritesRegenerator.php | 26 +++++++------------ 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php index 9263f48a22ec7..ce1bae7df7fcd 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php @@ -5,33 +5,27 @@ */ namespace Magento\CatalogUrlRewrite\Model\Category; -use Magento\Catalog\Model\Category; use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator; use Magento\UrlRewrite\Model\OptionProvider; -use Magento\UrlRewrite\Model\UrlFinderInterface; -use Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory; -use Magento\UrlRewrite\Service\V1\Data\UrlRewrite; -use Magento\CatalogUrlRewrite\Model\Map\UrlRewriteMap; -use Magento\UrlRewrite\Model\UrlRewritesSetFactory; class CurrentUrlRewritesRegenerator { /** @var \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator */ protected $categoryUrlPathGenerator; - /** @var UrlRewriteFactory */ + /** @var \Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory */ protected $urlRewriteFactory; - /** @var UrlRewrite */ + /** @var \Magento\UrlRewrite\Service\V1\Data\UrlRewrite */ private $urlRewritePlaceholder; /** - * @var UrlFinderInterface + * @var \Magento\UrlRewrite\Model\UrlFinderInterface * @deprecated */ protected $urlFinder; - /** @var UrlRewriteMap */ + /** @var \Magento\CatalogUrlRewrite\Model\Map\UrlRewriteMap */ private $urlRewriteMap; /** @var \Magento\UrlRewrite\Model\UrlRewritesSet */ @@ -56,7 +50,7 @@ public function __construct( $this->urlRewritePlaceholder = $urlRewriteFactory->create(); $this->urlFinder = $urlFinder; $this->urlRewriteMap = $urlRewriteMap ?: \Magento\Framework\App\ObjectManager::getInstance() - ->get(UrlRewriteMap::class); + ->get(\Magento\CatalogUrlRewrite\Model\Map\UrlRewriteMap::class); $urlRewritesSetFactory = $urlRewritesSetFactory ?: \Magento\Framework\App\ObjectManager::getInstance() ->get(UrlRewritesSetFactory::class); $this->urlRewritesSetPlaceHolder = $urlRewritesSetFactory->create(); @@ -70,7 +64,7 @@ public function __construct( * @param int|null $rootCategoryId * @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] */ - public function generate($storeId, Category $category, $rootCategoryId = null) + public function generate($storeId, \Magento\Catalog\Model\Category $category, $rootCategoryId = null) { $urlRewritesSet = clone $this->urlRewritesSetPlaceHolder; $currentUrlRewrites = $this->urlRewriteMap->getByIdentifiers( @@ -96,10 +90,10 @@ public function generate($storeId, Category $category, $rootCategoryId = null) /** * @param \Magento\UrlRewrite\Service\V1\Data\UrlRewrite $url * @param int $storeId - * @param Category $category + * @param \Magento\Catalog\Model\Category $category * @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] */ - protected function generateForAutogenerated($url, $storeId, Category $category) + protected function generateForAutogenerated($url, $storeId, \Magento\Catalog\Model\Category $category) { if ($category->getData('save_rewrites_history')) { $targetPath = $this->categoryUrlPathGenerator->getUrlPathWithSuffix($category, $storeId); @@ -121,10 +115,10 @@ protected function generateForAutogenerated($url, $storeId, Category $category) /** * @param \Magento\UrlRewrite\Service\V1\Data\UrlRewrite $url * @param int $storeId - * @param Category $category + * @param \Magento\Catalog\Model\Category $category * @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] */ - protected function generateForCustom($url, $storeId, Category $category) + protected function generateForCustom($url, $storeId, \Magento\Catalog\Model\Category $category) { $targetPath = !$url->getRedirectType() ? $url->getTargetPath() From 5549750fafecbcc8367dce4a69bf5bffdb85f12e Mon Sep 17 00:00:00 2001 From: Cristian Partica Date: Wed, 28 Dec 2016 17:33:58 -0600 Subject: [PATCH 24/39] MAGETWO-58924: SQL error wait timeout error when saving categories - reduce dependency coupling count --- .../Model/Category/CurrentUrlRewritesRegenerator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php index ce1bae7df7fcd..c2adf7bb7a3ff 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php @@ -52,7 +52,7 @@ public function __construct( $this->urlRewriteMap = $urlRewriteMap ?: \Magento\Framework\App\ObjectManager::getInstance() ->get(\Magento\CatalogUrlRewrite\Model\Map\UrlRewriteMap::class); $urlRewritesSetFactory = $urlRewritesSetFactory ?: \Magento\Framework\App\ObjectManager::getInstance() - ->get(UrlRewritesSetFactory::class); + ->get(\Magento\UrlRewrite\Model\UrlRewritesSetFactory::class); $this->urlRewritesSetPlaceHolder = $urlRewritesSetFactory->create(); } From 6211154a481a64c1f61c407dbb113def2e50d614 Mon Sep 17 00:00:00 2001 From: Cristian Partica Date: Wed, 4 Jan 2017 15:14:08 -0600 Subject: [PATCH 25/39] MAGETWO-58924: SQL error wait timeout error when saving categories - fix year copyright --- .../Magento/CatalogUrlRewrite/Model/Map/DataCategoryMap.php | 2 +- .../CatalogUrlRewrite/Model/Map/DataCategoryUrlRewriteMap.php | 2 +- .../Model/Map/DataCategoryUsedInProductsMap.php | 2 +- .../Magento/CatalogUrlRewrite/Model/Map/DataMapInterface.php | 2 +- app/code/Magento/CatalogUrlRewrite/Model/Map/DataMapPool.php | 2 +- .../CatalogUrlRewrite/Model/Map/DataMapPoolInterface.php | 2 +- app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductMap.php | 2 +- .../CatalogUrlRewrite/Model/Map/DataProductUrlRewriteMap.php | 2 +- app/code/Magento/CatalogUrlRewrite/Model/Map/UrlRewriteMap.php | 2 +- .../Test/Unit/Model/Map/DataCategoryMapTest.php | 2 +- .../Test/Unit/Model/Map/DataCategoryUrlRewriteMapTest.php | 2 +- .../Test/Unit/Model/Map/DataCategoryUsedInProductsMapTest.php | 2 +- .../CatalogUrlRewrite/Test/Unit/Model/Map/DataMapPoolTest.php | 2 +- .../Test/Unit/Model/Map/DataProductMapTest.php | 2 +- .../Test/Unit/Model/Map/DataProductUrlRewriteMapTest.php | 2 +- .../CatalogUrlRewrite/Test/Unit/Model/Map/UrlRewriteMapTest.php | 2 +- app/code/Magento/UrlRewrite/Model/UrlRewritesSet.php | 2 +- .../Magento/UrlRewrite/Test/Unit/Model/UrlRewritesSetTest.php | 2 +- .../Observer/CategoryProcessUrlRewriteSavingObserverTest.php | 2 +- lib/internal/Magento/Framework/DB/TemporaryTableService.php | 2 +- .../Framework/DB/Test/Unit/TemporaryTableServiceTest.php | 2 +- 21 files changed, 21 insertions(+), 21 deletions(-) diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryMap.php index 3077e273d83ea..162513f5b9809 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryMap.php @@ -1,6 +1,6 @@ Date: Wed, 4 Jan 2017 16:40:36 -0600 Subject: [PATCH 26/39] MAGETWO-58924: SQL error wait timeout error when saving categories - fix issues with code review, add phpdocs and restore BiC, rename/remove methods --- .../Category/ChildrenUrlRewriteGenerator.php | 10 +++---- .../CurrentUrlRewritesRegenerator.php | 24 ++++++++------- .../Model/Category/Plugin/Storage.php | 2 +- .../Model/CategoryUrlRewriteGenerator.php | 19 +++++++----- .../Model/Map/UrlRewriteMap.php | 6 ++-- .../Product/CurrentUrlRewritesRegenerator.php | 30 ++++++++++++------- .../Model/ProductScopeRewriteGenerator.php | 16 ++++------ .../Model/ProductUrlRewriteGenerator.php | 6 ++++ .../Model/ResourceModel/Category/Product.php | 5 ++-- .../Observer/AfterImportDataObserver.php | 10 +++---- .../Observer/UrlRewriteHandler.php | 16 ++++------ .../Model/Category/Plugin/StorageTest.php | 2 +- .../Test/Unit/Model/Map/UrlRewriteMapTest.php | 6 ++-- .../UrlRewrite/Model/UrlRewritesSet.php | 19 ++++-------- .../Test/Unit/Model/UrlRewritesSetTest.php | 15 ---------- 15 files changed, 87 insertions(+), 99 deletions(-) diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Category/ChildrenUrlRewriteGenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/Category/ChildrenUrlRewriteGenerator.php index 6080c14ee2469..59353c595c4ba 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Category/ChildrenUrlRewriteGenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Category/ChildrenUrlRewriteGenerator.php @@ -20,7 +20,7 @@ class ChildrenUrlRewriteGenerator protected $categoryUrlRewriteGeneratorFactory; /** @var \Magento\UrlRewrite\Model\UrlRewritesSet */ - private $urlRewritesSetPlaceHolder; + private $urlRewritesSetPrototype; /** * @param \Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider $childrenCategoriesProvider @@ -36,7 +36,7 @@ public function __construct( $this->categoryUrlRewriteGeneratorFactory = $categoryUrlRewriteGeneratorFactory; $urlRewritesSetFactory = $urlRewritesSetFactory ?: ObjectManager::getInstance() ->get(UrlRewritesSetFactory::class); - $this->urlRewritesSetPlaceHolder = $urlRewritesSetFactory->create(); + $this->urlRewritesSetPrototype = $urlRewritesSetFactory->create(); } /** @@ -49,7 +49,7 @@ public function __construct( */ public function generate($storeId, Category $category, $rootCategoryId = null) { - $urlRewritesSet = clone $this->urlRewritesSetPlaceHolder; + $urlRewritesSet = clone $this->urlRewritesSetPrototype; foreach ($this->childrenCategoriesProvider->getChildren($category, true) as $childCategory) { $childCategory->setStoreId($storeId); $childCategory->setData('save_rewrites_history', $category->getData('save_rewrites_history')); @@ -60,8 +60,6 @@ public function generate($storeId, Category $category, $rootCategoryId = null) ); } - $result = $urlRewritesSet->getData(); - $urlRewritesSet->resetData(); - return $result; + return $urlRewritesSet->getData(); } } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php index c2adf7bb7a3ff..b9936fa73d5ba 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php @@ -17,7 +17,13 @@ class CurrentUrlRewritesRegenerator protected $urlRewriteFactory; /** @var \Magento\UrlRewrite\Service\V1\Data\UrlRewrite */ - private $urlRewritePlaceholder; + private $urlRewritePrototype; + + /** + * @var \Magento\Catalog\Model\Category + * @deprecated + */ + protected $category; /** * @var \Magento\UrlRewrite\Model\UrlFinderInterface @@ -29,7 +35,7 @@ class CurrentUrlRewritesRegenerator private $urlRewriteMap; /** @var \Magento\UrlRewrite\Model\UrlRewritesSet */ - private $urlRewritesSetPlaceHolder; + private $urlRewritesSetPrototype; /** * @param \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator @@ -47,13 +53,13 @@ public function __construct( ) { $this->categoryUrlPathGenerator = $categoryUrlPathGenerator; $this->urlRewriteFactory = $urlRewriteFactory; - $this->urlRewritePlaceholder = $urlRewriteFactory->create(); + $this->urlRewritePrototype = $urlRewriteFactory->create(); $this->urlFinder = $urlFinder; $this->urlRewriteMap = $urlRewriteMap ?: \Magento\Framework\App\ObjectManager::getInstance() ->get(\Magento\CatalogUrlRewrite\Model\Map\UrlRewriteMap::class); $urlRewritesSetFactory = $urlRewritesSetFactory ?: \Magento\Framework\App\ObjectManager::getInstance() ->get(\Magento\UrlRewrite\Model\UrlRewritesSetFactory::class); - $this->urlRewritesSetPlaceHolder = $urlRewritesSetFactory->create(); + $this->urlRewritesSetPrototype = $urlRewritesSetFactory->create(); } /** @@ -66,7 +72,7 @@ public function __construct( */ public function generate($storeId, \Magento\Catalog\Model\Category $category, $rootCategoryId = null) { - $urlRewritesSet = clone $this->urlRewritesSetPlaceHolder; + $urlRewritesSet = clone $this->urlRewritesSetPrototype; $currentUrlRewrites = $this->urlRewriteMap->getByIdentifiers( $category->getEntityId(), $storeId, @@ -82,9 +88,7 @@ public function generate($storeId, \Magento\Catalog\Model\Category $category, $r ); } - $result = $urlRewritesSet->getData(); - $urlRewritesSet->resetData(); - return $result; + return $urlRewritesSet->getData(); } /** @@ -98,7 +102,7 @@ protected function generateForAutogenerated($url, $storeId, \Magento\Catalog\Mod if ($category->getData('save_rewrites_history')) { $targetPath = $this->categoryUrlPathGenerator->getUrlPathWithSuffix($category, $storeId); if ($url->getRequestPath() !== $targetPath) { - $generatedUrl = clone $this->urlRewritePlaceholder; + $generatedUrl = clone $this->urlRewritePrototype; $generatedUrl->setEntityType(CategoryUrlRewriteGenerator::ENTITY_TYPE) ->setEntityId($category->getEntityId()) ->setRequestPath($url->getRequestPath()) @@ -124,7 +128,7 @@ protected function generateForCustom($url, $storeId, \Magento\Catalog\Model\Cate ? $url->getTargetPath() : $this->categoryUrlPathGenerator->getUrlPathWithSuffix($category, $storeId); if ($url->getRequestPath() !== $targetPath) { - $generatedUrl = clone $this->urlRewritePlaceholder; + $generatedUrl = clone $this->urlRewritePrototype; $generatedUrl->setEntityType(CategoryUrlRewriteGenerator::ENTITY_TYPE) ->setEntityId($category->getEntityId()) ->setRequestPath($url->getRequestPath()) diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Storage.php b/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Storage.php index e3916f991d1ba..be48210298231 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Storage.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Storage.php @@ -62,7 +62,7 @@ public function afterReplace(StorageInterface $object, $result, array $urls) */ public function beforeDeleteByData(StorageInterface $object, array $data) { - $this->productResource->removeMultipleByFilter($data); + $this->productResource->removeMultipleByProductCategory($data); } /** diff --git a/app/code/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGenerator.php index fcf434fdb53de..ff3e4198332e4 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGenerator.php @@ -23,6 +23,12 @@ class CategoryUrlRewriteGenerator /** @var StoreViewService */ protected $storeViewService; + /** + * @var \Magento\Catalog\Model\Category + * @deprecated + */ + protected $category; + /** @var \Magento\CatalogUrlRewrite\Model\Category\CanonicalUrlRewriteGenerator */ protected $canonicalUrlRewriteGenerator; @@ -33,7 +39,7 @@ class CategoryUrlRewriteGenerator protected $childrenUrlRewriteGenerator; /** @var \Magento\UrlRewrite\Model\UrlRewritesSet */ - private $urlRewritesSetPlaceHolder; + private $urlRewritesSetPrototype; /** * @var bool @@ -63,7 +69,7 @@ public function __construct( $this->categoryRepository = $categoryRepository; $urlRewritesSetFactory = $urlRewritesSetFactory ?: ObjectManager::getInstance() ->get(UrlRewritesSetFactory::class); - $this->urlRewritesSetPlaceHolder = $urlRewritesSetFactory->create(); + $this->urlRewritesSetPrototype = $urlRewritesSetFactory->create(); } /** @@ -96,7 +102,7 @@ public function generate(Category $category, $overrideStoreUrls = false, $rootCa */ protected function generateForGlobalScope(Category $category, $overrideStoreUrls, $rootCategoryId = null) { - $urlRewritesSet = clone $this->urlRewritesSetPlaceHolder; + $urlRewritesSet = clone $this->urlRewritesSetPrototype; $categoryId = $category->getId(); foreach ($category->getStoreIds() as $storeId) { if (!$this->isGlobalScope($storeId) @@ -107,7 +113,6 @@ protected function generateForGlobalScope(Category $category, $overrideStoreUrls } } $result = $urlRewritesSet->getData(); - $urlRewritesSet->resetData(); return $result; } @@ -165,7 +170,7 @@ protected function isGlobalScope($storeId) */ protected function generateForSpecificStoreView(Category $category, $storeId, $rootCategoryId = null) { - $urlRewritesSet = clone $this->urlRewritesSetPlaceHolder; + $urlRewritesSet = clone $this->urlRewritesSetPrototype; $urlRewritesSet->merge( $this->canonicalUrlRewriteGenerator->generate($storeId, $category) ); @@ -175,8 +180,6 @@ protected function generateForSpecificStoreView(Category $category, $storeId, $r $urlRewritesSet->merge( $this->currentUrlRewritesRegenerator->generate($storeId, $category, $rootCategoryId) ); - $result = $urlRewritesSet->getData(); - $urlRewritesSet->resetData(); - return $result; + return $urlRewritesSet->getData(); } } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/UrlRewriteMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/UrlRewriteMap.php index 17ec499f75363..70bb44c75b8f4 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/UrlRewriteMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/UrlRewriteMap.php @@ -25,7 +25,7 @@ class UrlRewriteMap private $urlFinder; /** @var UrlRewrite */ - private $urlRewritePlaceholder; + private $urlRewritePrototype; /** * @param DataMapPoolInterface $dataMapPool @@ -39,7 +39,7 @@ public function __construct( ) { $this->dataMapPool = $dataMapPool; $this->urlFinder = $urlFinder; - $this->urlRewritePlaceholder = $urlRewriteFactory->create(); + $this->urlRewritePrototype = $urlRewriteFactory->create(); } /** @@ -98,7 +98,7 @@ private function arrayToUrlRewriteObject($data) */ private function createUrlRewrite($data) { - $dataObject = clone $this->urlRewritePlaceholder; + $dataObject = clone $this->urlRewritePrototype; $dataObject->setUrlRewriteId($data['url_rewrite_id']); $dataObject->setEntityType($data['entity_type']); $dataObject->setEntityId($data['entity_id']); diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php index 1f85f5a3d2766..ddb91986f249d 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php @@ -23,6 +23,18 @@ */ class CurrentUrlRewritesRegenerator { + /** + * @var Product + * @deprecated + */ + protected $product; + + /** + * @var ObjectRegistry + * @deprecated + */ + protected $productCategories; + /** * @var UrlFinderInterface * @deprecated @@ -36,13 +48,13 @@ class CurrentUrlRewritesRegenerator protected $urlRewriteFactory; /** @var UrlRewrite */ - private $urlRewritePlaceholder; + private $urlRewritePrototype; /** @var UrlRewriteMap */ private $urlRewriteMap; /** @var \Magento\UrlRewrite\Model\UrlRewritesSet */ - private $urlRewritesSetPlaceHolder; + private $urlRewritesSetPrototype; /** * @param UrlFinderInterface $urlFinder @@ -61,11 +73,11 @@ public function __construct( $this->urlFinder = $urlFinder; $this->productUrlPathGenerator = $productUrlPathGenerator; $this->urlRewriteFactory = $urlRewriteFactory; - $this->urlRewritePlaceholder = $urlRewriteFactory->create(); + $this->urlRewritePrototype = $urlRewriteFactory->create(); $this->urlRewriteMap = $urlRewriteMap ?: ObjectManager::getInstance()->get(UrlRewriteMap::class); $urlRewritesSetFactory = $urlRewritesSetFactory ?: ObjectManager::getInstance() ->get(UrlRewritesSetFactory::class); - $this->urlRewritesSetPlaceHolder = $urlRewritesSetFactory->create(); + $this->urlRewritesSetPrototype = $urlRewritesSetFactory->create(); } /** @@ -79,7 +91,7 @@ public function __construct( */ public function generate($storeId, Product $product, ObjectRegistry $productCategories, $rootCategoryId = null) { - $urlRewritesSet = clone $this->urlRewritesSetPlaceHolder; + $urlRewritesSet = clone $this->urlRewritesSetPrototype; $currentUrlRewrites = $this->urlRewriteMap->getByIdentifiers( $product->getEntityId(), $storeId, @@ -99,9 +111,7 @@ public function generate($storeId, Product $product, ObjectRegistry $productCate ); } - $result = $urlRewritesSet->getData(); - $urlRewritesSet->resetData(); - return $result; + return $urlRewritesSet->getData(); } /** @@ -116,7 +126,7 @@ protected function generateForAutogenerated(UrlRewrite $url, $storeId, $category if ($product->getData('save_rewrites_history')) { $targetPath = $this->productUrlPathGenerator->getUrlPathWithSuffix($product, $storeId, $category); if ($url->getRequestPath() !== $targetPath) { - $generatedUrl = clone $this->urlRewritePlaceholder; + $generatedUrl = clone $this->urlRewritePrototype; $generatedUrl->setEntityType(ProductUrlRewriteGenerator::ENTITY_TYPE) ->setEntityId($product->getEntityId()) ->setRequestPath($url->getRequestPath()) @@ -145,7 +155,7 @@ protected function generateForCustom(UrlRewrite $url, $storeId, $category, Produ ? $this->productUrlPathGenerator->getUrlPathWithSuffix($product, $storeId, $category) : $url->getTargetPath(); if ($url->getRequestPath() !== $targetPath) { - $generatedUrl = clone $this->urlRewritePlaceholder; + $generatedUrl = clone $this->urlRewritePrototype; $generatedUrl->setEntityType(ProductUrlRewriteGenerator::ENTITY_TYPE) ->setEntityId($product->getEntityId()) ->setRequestPath($url->getRequestPath()) diff --git a/app/code/Magento/CatalogUrlRewrite/Model/ProductScopeRewriteGenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/ProductScopeRewriteGenerator.php index cc64dc99962d3..a586e8433f9d3 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/ProductScopeRewriteGenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/ProductScopeRewriteGenerator.php @@ -59,7 +59,7 @@ class ProductScopeRewriteGenerator private $canonicalUrlRewriteGenerator; /** @var \Magento\UrlRewrite\Model\UrlRewritesSet */ - private $urlRewritesSetPlaceHolder; + private $urlRewritesSetPrototype; /** * @param StoreViewService $storeViewService @@ -90,7 +90,7 @@ public function __construct( $this->anchorUrlRewriteGenerator = $anchorUrlRewriteGenerator; $urlRewritesSetFactory = $urlRewritesSetFactory ?: ObjectManager::getInstance() ->get(UrlRewritesSetFactory::class); - $this->urlRewritesSetPlaceHolder = $urlRewritesSetFactory->create(); + $this->urlRewritesSetPrototype = $urlRewritesSetFactory->create(); } /** @@ -115,7 +115,7 @@ public function isGlobalScope($storeId) public function generateForGlobalScope($productCategories, Product $product, $rootCategoryId = null) { $productId = $product->getEntityId(); - $urlRewritesSet = clone $this->urlRewritesSetPlaceHolder; + $urlRewritesSet = clone $this->urlRewritesSetPrototype; foreach ($product->getStoreIds() as $id) { if (!$this->isGlobalScope($id) && @@ -130,9 +130,7 @@ public function generateForGlobalScope($productCategories, Product $product, $ro } } - $result = $urlRewritesSet->getData(); - $urlRewritesSet->resetData(); - return $result; + return $urlRewritesSet->getData(); } /** @@ -146,7 +144,7 @@ public function generateForGlobalScope($productCategories, Product $product, $ro */ public function generateForSpecificStoreView($storeId, $productCategories, Product $product, $rootCategoryId = null) { - $urlRewritesSet = clone $this->urlRewritesSetPlaceHolder; + $urlRewritesSet = clone $this->urlRewritesSetPrototype; $categories = []; foreach ($productCategories as $category) { if ($this->isCategoryProperForGenerating($category, $storeId)) { @@ -173,9 +171,7 @@ public function generateForSpecificStoreView($storeId, $productCategories, Produ $this->anchorUrlRewriteGenerator->generate($storeId, $product, $productCategories) ); - $result = $urlRewritesSet->getData(); - $urlRewritesSet->resetData(); - return $result; + return $urlRewritesSet->getData(); } /** diff --git a/app/code/Magento/CatalogUrlRewrite/Model/ProductUrlRewriteGenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/ProductUrlRewriteGenerator.php index c13dcee6bfa09..1b1fc472a7ab4 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/ProductUrlRewriteGenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/ProductUrlRewriteGenerator.php @@ -31,6 +31,12 @@ class ProductUrlRewriteGenerator */ protected $storeViewService; + /** + * @var \Magento\Catalog\Model\Product + * @deprecated + */ + protected $product; + /** * @deprecated * @var \Magento\CatalogUrlRewrite\Model\Product\CurrentUrlRewritesRegenerator diff --git a/app/code/Magento/CatalogUrlRewrite/Model/ResourceModel/Category/Product.php b/app/code/Magento/CatalogUrlRewrite/Model/ResourceModel/Category/Product.php index 1605ad2643113..c90478af47676 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/ResourceModel/Category/Product.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/ResourceModel/Category/Product.php @@ -70,12 +70,13 @@ public function removeMultiple(array $removeData) } /** - * Removes data by entities from url_rewrite table using a select + * Removes multiple entities from url_rewrite table using entities from catalog_url_rewrite_product_category + * Example: $filter = ['category_id' => [1, 2, 3], 'product_id' => [1, 2, 3]] * * @param array $filter * @return int */ - public function removeMultipleByFilter(array $filter) + public function removeMultipleByProductCategory(array $filter) { return $this->getConnection()->delete( $this->getTable(self::TABLE_NAME), diff --git a/app/code/Magento/CatalogUrlRewrite/Observer/AfterImportDataObserver.php b/app/code/Magento/CatalogUrlRewrite/Observer/AfterImportDataObserver.php index 2d15d4f627359..a9307ddd14f30 100644 --- a/app/code/Magento/CatalogUrlRewrite/Observer/AfterImportDataObserver.php +++ b/app/code/Magento/CatalogUrlRewrite/Observer/AfterImportDataObserver.php @@ -100,7 +100,7 @@ class AfterImportDataObserver implements ObserverInterface ]; /** @var \Magento\UrlRewrite\Model\UrlRewritesSet */ - private $urlRewritesSetPlaceHolder; + private $urlRewritesSetPrototype; /** * @param \Magento\Catalog\Model\ProductFactory $catalogProductFactory @@ -136,7 +136,7 @@ public function __construct( $this->urlFinder = $urlFinder; $urlRewritesSetFactory = $urlRewritesSetFactory ?: ObjectManager::getInstance() ->get(UrlRewritesSetFactory::class); - $this->urlRewritesSetPlaceHolder = $urlRewritesSetFactory->create(); + $this->urlRewritesSetPrototype = $urlRewritesSetFactory->create(); } /** @@ -269,7 +269,7 @@ protected function populateGlobalProduct($product) */ protected function generateUrls() { - $urlRewritesSet = clone $this->urlRewritesSetPlaceHolder; + $urlRewritesSet = clone $this->urlRewritesSetPrototype; $urlRewritesSet->merge($this->canonicalUrlRewriteGenerate()); $urlRewritesSet->merge($this->categoriesUrlRewriteGenerate()); $urlRewritesSet->merge($this->currentUrlRewritesRegenerate()); @@ -278,9 +278,7 @@ protected function generateUrls() unset($this->products); $this->products = []; - $result = $urlRewritesSet->getData(); - $urlRewritesSet->resetData(); - return $result; + return $urlRewritesSet->getData(); } /** diff --git a/app/code/Magento/CatalogUrlRewrite/Observer/UrlRewriteHandler.php b/app/code/Magento/CatalogUrlRewrite/Observer/UrlRewriteHandler.php index 4c52725e5e964..ca5ae2b5beba5 100644 --- a/app/code/Magento/CatalogUrlRewrite/Observer/UrlRewriteHandler.php +++ b/app/code/Magento/CatalogUrlRewrite/Observer/UrlRewriteHandler.php @@ -41,7 +41,7 @@ class UrlRewriteHandler private $categoryBasedProductRewriteGenerator; /** @var \Magento\UrlRewrite\Model\UrlRewritesSet */ - private $urlRewritesSetPlaceHolder; + private $urlRewritesSetPrototype; /** * @param \Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider $childrenCategoriesProvider @@ -66,7 +66,7 @@ public function __construct( $this->productCollectionFactory = $productCollectionFactory; $urlRewritesSetFactory = $urlRewritesSetFactory ?: ObjectManager::getInstance() ->get(UrlRewritesSetFactory::class); - $this->urlRewritesSetPlaceHolder = $urlRewritesSetFactory->create(); + $this->urlRewritesSetPrototype = $urlRewritesSetFactory->create(); } /** @@ -77,7 +77,7 @@ public function __construct( */ public function generateProductUrlRewrites(Category $category) { - $urlRewritesSet = clone $this->urlRewritesSetPlaceHolder; + $urlRewritesSet = clone $this->urlRewritesSetPrototype; $this->isSkippedProduct = []; $saveRewriteHistory = $category->getData('save_rewrites_history'); $storeId = $category->getStoreId(); @@ -118,9 +118,7 @@ public function generateProductUrlRewrites(Category $category) ); } - $result = $urlRewritesSet->getData(); - $urlRewritesSet->resetData(); - return $result; + return $urlRewritesSet->getData(); } /** @@ -136,7 +134,7 @@ public function getCategoryProductsUrlRewrites( $saveRewriteHistory, $rootCategoryId = null ) { - $urlRewritesSet = clone $this->urlRewritesSetPlaceHolder; + $urlRewritesSet = clone $this->urlRewritesSetPrototype; /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection */ $productCollection = $category->getProductCollection() ->addAttributeToSelect('name') @@ -155,9 +153,7 @@ public function getCategoryProductsUrlRewrites( ); } - $result = $urlRewritesSet->getData(); - $urlRewritesSet->resetData(); - return $result; + return $urlRewritesSet->getData(); } /** diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/Plugin/StorageTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/Plugin/StorageTest.php index 5c068cc59b724..f2dad1e1035b5 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/Plugin/StorageTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/Plugin/StorageTest.php @@ -102,7 +102,7 @@ public function testBeforeDeleteByData() { $data = [1, 2, 3]; $this->productResourceModel->expects(static::once()) - ->method('removeMultipleByFilter') + ->method('removeMultipleByProductCategory') ->with($data)->willReturnSelf(); $this->plugin->beforeDeleteByData($this->storage, $data); } diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/UrlRewriteMapTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/UrlRewriteMapTest.php index a84fbc302bd9a..c6ea0f0ce444c 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/UrlRewriteMapTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/UrlRewriteMapTest.php @@ -26,7 +26,7 @@ class UrlRewriteMapTest extends \PHPUnit_Framework_TestCase private $urlRewriteFactoryMock; /** @var UrlRewrite|\PHPUnit_Framework_MockObject_MockObject */ - private $urlRewritePlaceholderMock; + private $urlRewritePrototypeMock; /** @var UrlFinderInterface|\PHPUnit_Framework_MockObject_MockObject */ private $urlFinderMock; @@ -39,11 +39,11 @@ protected function setUp() $this->dataMapPoolMock = $this->getMock(DataMapPoolInterface::class); $this->urlFinderMock = $this->getMock(UrlFinderInterface::class); $this->urlRewriteFactoryMock = $this->getMock(UrlRewriteFactory::class, ['create'], [], '', false); - $this->urlRewritePlaceholderMock = new UrlRewrite(); + $this->urlRewritePrototypeMock = new UrlRewrite(); $this->urlRewriteFactoryMock->expects($this->any()) ->method('create') - ->willReturn($this->urlRewritePlaceholderMock); + ->willReturn($this->urlRewritePrototypeMock); $this->model = (new ObjectManager($this))->getObject( UrlRewriteMap::class, diff --git a/app/code/Magento/UrlRewrite/Model/UrlRewritesSet.php b/app/code/Magento/UrlRewrite/Model/UrlRewritesSet.php index 480bfc5ddc354..184eb9486a235 100644 --- a/app/code/Magento/UrlRewrite/Model/UrlRewritesSet.php +++ b/app/code/Magento/UrlRewrite/Model/UrlRewritesSet.php @@ -8,7 +8,9 @@ use Magento\UrlRewrite\Service\V1\Data\UrlRewrite; /** - * Removes duplicates for a set/array of Url Rewrites + * This class is to be used as a container for new generated url rewrites by adding new ones using merge method + * Removes duplicates for a set/array of Url Rewrites based on the unique key of the url_rewrites table + * */ class UrlRewritesSet { @@ -23,13 +25,13 @@ class UrlRewritesSet * @param UrlRewrite[] $urlRewritesArray * @return void */ - public function merge($urlRewritesArray) + public function merge(array $urlRewritesArray) { $separator = '_'; foreach ($urlRewritesArray as $urlRewrite) { $key = $urlRewrite->getRequestPath() . $separator . $urlRewrite->getStoreId(); if ($key !== $separator) { - $this->data[$urlRewrite->getRequestPath() . $separator . $urlRewrite->getStoreId()] = $urlRewrite; + $this->data[$key] = $urlRewrite; } else { $this->data[] = $urlRewrite; } @@ -45,15 +47,4 @@ public function getData() { return $this->data; } - - /** - * Resets the container to an empty array - * - * @return void - */ - public function resetData() - { - unset($this->data); - $this->data = []; - } } diff --git a/app/code/Magento/UrlRewrite/Test/Unit/Model/UrlRewritesSetTest.php b/app/code/Magento/UrlRewrite/Test/Unit/Model/UrlRewritesSetTest.php index 131f8cbe1a739..ce5b944cb3b5f 100644 --- a/app/code/Magento/UrlRewrite/Test/Unit/Model/UrlRewritesSetTest.php +++ b/app/code/Magento/UrlRewrite/Test/Unit/Model/UrlRewritesSetTest.php @@ -69,21 +69,6 @@ public function testGetDataWhenNotEmpty() $this->assertNotEmpty($this->urlRewritesSet->getData()); } - /** - * Run test resetData method - * - * @return void - */ - public function testResetData() - { - $data = new \ReflectionProperty($this->urlRewritesSet, 'data'); - $data->setAccessible(true); - $data->setValue($this->urlRewritesSet, [new UrlRewrite()]); - $data->setAccessible(false); - $this->urlRewritesSet->resetData(); - $this->assertEmpty($this->urlRewritesSet->getData()); - } - /** * Data provider for testMerge * From 5f77a329fa08dd7faa03a760e351bb98c813ab7f Mon Sep 17 00:00:00 2001 From: Cristian Partica Date: Fri, 6 Jan 2017 17:56:50 -0600 Subject: [PATCH 27/39] MAGETWO-58924: SQL error wait timeout error when saving categories - fix issues with code review, add phpdocs and restore BiC, rename/remove methods --- .../CurrentUrlRewritesRegenerator.php | 10 +-- .../Model/CategoryUrlRewriteGenerator.php | 32 ++++---- ...ategoryMap.php => DataCategoryHashMap.php} | 23 +++--- ... => DataCategoryUrlRewriteDatabaseMap.php} | 55 ++++++------- ... => DataCategoryUsedInProductsHashMap.php} | 41 +++++----- .../Model/Map/DataMapPoolInterface.php | 30 -------- ...aProductMap.php => DataProductHashMap.php} | 35 ++++----- ...p => DataProductUrlRewriteDatabaseMap.php} | 49 ++++++------ .../Model/Map/DatabaseMapInterface.php | 38 +++++++++ .../{DataMapPool.php => DatabaseMapPool.php} | 22 ++++-- ...aMapInterface.php => HashMapInterface.php} | 11 ++- .../Model/Map/HashMapPool.php | 77 +++++++++++++++++++ ...UrlRewriteMap.php => UrlRewriteFinder.php} | 51 +++++++----- .../Product/CurrentUrlRewritesRegenerator.php | 24 +++--- ...ategoryProcessUrlRewriteSavingObserver.php | 58 +++++++++++--- .../CurrentUrlRewritesRegeneratorTest.php | 2 +- ...apTest.php => DataCategoryHashMapTest.php} | 10 +-- ...DataCategoryUrlRewriteDatabaseMapTest.php} | 36 ++++----- ...DataCategoryUsedInProductsHashMapTest.php} | 35 ++++----- ...MapTest.php => DataProductHashMapTest.php} | 27 ++++--- ... DataProductUrlRewriteDatabaseMapTest.php} | 35 ++++----- ...ataMapPoolTest.php => HashMapPoolTest.php} | 28 +++---- ...teMapTest.php => UrlRewriteFinderTest.php} | 48 ++++++------ .../CurrentUrlRewritesRegeneratorTest.php | 2 +- app/code/Magento/CatalogUrlRewrite/etc/di.xml | 1 - .../UrlRewrite/Model/UrlRewritesSet.php | 6 +- .../Test/Unit/Model/UrlRewritesSetTest.php | 14 ---- .../Framework/DB/TemporaryTableService.php | 29 +++---- 28 files changed, 475 insertions(+), 354 deletions(-) rename app/code/Magento/CatalogUrlRewrite/Model/Map/{DataCategoryMap.php => DataCategoryHashMap.php} (80%) rename app/code/Magento/CatalogUrlRewrite/Model/Map/{DataCategoryUrlRewriteMap.php => DataCategoryUrlRewriteDatabaseMap.php} (66%) rename app/code/Magento/CatalogUrlRewrite/Model/Map/{DataCategoryUsedInProductsMap.php => DataCategoryUsedInProductsHashMap.php} (66%) delete mode 100644 app/code/Magento/CatalogUrlRewrite/Model/Map/DataMapPoolInterface.php rename app/code/Magento/CatalogUrlRewrite/Model/Map/{DataProductMap.php => DataProductHashMap.php} (71%) rename app/code/Magento/CatalogUrlRewrite/Model/Map/{DataProductUrlRewriteMap.php => DataProductUrlRewriteDatabaseMap.php} (65%) create mode 100644 app/code/Magento/CatalogUrlRewrite/Model/Map/DatabaseMapInterface.php rename app/code/Magento/CatalogUrlRewrite/Model/Map/{DataMapPool.php => DatabaseMapPool.php} (69%) rename app/code/Magento/CatalogUrlRewrite/Model/Map/{DataMapInterface.php => HashMapInterface.php} (58%) create mode 100644 app/code/Magento/CatalogUrlRewrite/Model/Map/HashMapPool.php rename app/code/Magento/CatalogUrlRewrite/Model/Map/{UrlRewriteMap.php => UrlRewriteFinder.php} (64%) rename app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/{DataCategoryMapTest.php => DataCategoryHashMapTest.php} (93%) rename app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/{DataCategoryUrlRewriteMapTest.php => DataCategoryUrlRewriteDatabaseMapTest.php} (76%) rename app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/{DataCategoryUsedInProductsMapTest.php => DataCategoryUsedInProductsHashMapTest.php} (75%) rename app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/{DataProductMapTest.php => DataProductHashMapTest.php} (83%) rename app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/{DataProductUrlRewriteMapTest.php => DataProductUrlRewriteDatabaseMapTest.php} (73%) rename app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/{DataMapPoolTest.php => HashMapPoolTest.php} (63%) rename app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/{UrlRewriteMapTest.php => UrlRewriteFinderTest.php} (68%) diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php index b9936fa73d5ba..a44aee69b107f 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php @@ -31,7 +31,7 @@ class CurrentUrlRewritesRegenerator */ protected $urlFinder; - /** @var \Magento\CatalogUrlRewrite\Model\Map\UrlRewriteMap */ + /** @var \Magento\CatalogUrlRewrite\Model\Map\UrlRewriteFinder */ private $urlRewriteMap; /** @var \Magento\UrlRewrite\Model\UrlRewritesSet */ @@ -41,14 +41,14 @@ class CurrentUrlRewritesRegenerator * @param \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator * @param \Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory $urlRewriteFactory * @param \Magento\UrlRewrite\Model\UrlFinderInterface $urlFinder - * @param \Magento\CatalogUrlRewrite\Model\Map\UrlRewriteMap|null $urlRewriteMap + * @param \Magento\CatalogUrlRewrite\Model\Map\UrlRewriteFinder|null $urlRewriteMap * @param \Magento\UrlRewrite\Model\UrlRewritesSetFactory|null $urlRewritesSetFactory */ public function __construct( \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator, \Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory $urlRewriteFactory, \Magento\UrlRewrite\Model\UrlFinderInterface $urlFinder, - \Magento\CatalogUrlRewrite\Model\Map\UrlRewriteMap $urlRewriteMap = null, + \Magento\CatalogUrlRewrite\Model\Map\UrlRewriteFinder $urlRewriteMap = null, \Magento\UrlRewrite\Model\UrlRewritesSetFactory $urlRewritesSetFactory = null ) { $this->categoryUrlPathGenerator = $categoryUrlPathGenerator; @@ -56,7 +56,7 @@ public function __construct( $this->urlRewritePrototype = $urlRewriteFactory->create(); $this->urlFinder = $urlFinder; $this->urlRewriteMap = $urlRewriteMap ?: \Magento\Framework\App\ObjectManager::getInstance() - ->get(\Magento\CatalogUrlRewrite\Model\Map\UrlRewriteMap::class); + ->get(\Magento\CatalogUrlRewrite\Model\Map\UrlRewriteFinder::class); $urlRewritesSetFactory = $urlRewritesSetFactory ?: \Magento\Framework\App\ObjectManager::getInstance() ->get(\Magento\UrlRewrite\Model\UrlRewritesSetFactory::class); $this->urlRewritesSetPrototype = $urlRewritesSetFactory->create(); @@ -73,7 +73,7 @@ public function __construct( public function generate($storeId, \Magento\Catalog\Model\Category $category, $rootCategoryId = null) { $urlRewritesSet = clone $this->urlRewritesSetPrototype; - $currentUrlRewrites = $this->urlRewriteMap->getByIdentifiers( + $currentUrlRewrites = $this->urlRewriteMap->findAllByData( $category->getEntityId(), $storeId, CategoryUrlRewriteGenerator::ENTITY_TYPE, diff --git a/app/code/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGenerator.php index ff3e4198332e4..b3b40cb2aeb52 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGenerator.php @@ -87,7 +87,7 @@ public function generate(Category $category, $overrideStoreUrls = false, $rootCa $storeId = $category->getStoreId(); $urls = $this->isGlobalScope($storeId) ? $this->generateForGlobalScope($category, $overrideStoreUrls, $rootCategoryId) - : $this->generateForSpecificStoreView($category, $storeId, $rootCategoryId); + : $this->generateForSpecificStoreView($storeId, $category, $rootCategoryId); return $urls; } @@ -95,12 +95,12 @@ public function generate(Category $category, $overrideStoreUrls = false, $rootCa /** * Generate list of urls for global scope * - * @param \Magento\Catalog\Model\Category $category + * @param \Magento\Catalog\Model\Category|null $category * @param bool $overrideStoreUrls * @param int|null $rootCategoryId * @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] */ - protected function generateForGlobalScope(Category $category, $overrideStoreUrls, $rootCategoryId = null) + protected function generateForGlobalScope(Category $category = null, $overrideStoreUrls = false, $rootCategoryId = null) { $urlRewritesSet = clone $this->urlRewritesSetPrototype; $categoryId = $category->getId(); @@ -108,8 +108,8 @@ protected function generateForGlobalScope(Category $category, $overrideStoreUrls if (!$this->isGlobalScope($storeId) && $this->isOverrideUrlsForStore($storeId, $categoryId, $overrideStoreUrls) ) { - $this->updateCategoryUrlForStore($category, $storeId); - $urlRewritesSet->merge($this->generateForSpecificStoreView($category, $storeId, $rootCategoryId)); + $this->updateCategoryUrlForStore($storeId, $category); + $urlRewritesSet->merge($this->generateForSpecificStoreView($storeId, $category, $rootCategoryId)); } } $result = $urlRewritesSet->getData(); @@ -122,7 +122,7 @@ protected function generateForGlobalScope(Category $category, $overrideStoreUrls * @param bool $overrideStoreUrls * @return bool */ - protected function isOverrideUrlsForStore($storeId, $categoryId, $overrideStoreUrls) + protected function isOverrideUrlsForStore($storeId, $categoryId, $overrideStoreUrls = false) { return $overrideStoreUrls || !$this->storeViewService->doesEntityHaveOverriddenUrlKeyForStore( $storeId, @@ -134,19 +134,19 @@ protected function isOverrideUrlsForStore($storeId, $categoryId, $overrideStoreU /** * Override url key and url path for category in specific Store * - * @param \Magento\Catalog\Model\Category $category * @param int $storeId + * @param \Magento\Catalog\Model\Category|null $category * @return void */ - protected function updateCategoryUrlForStore(Category $category, $storeId) + protected function updateCategoryUrlForStore($storeId, Category $category = null) { $categoryFromRepository = $this->categoryRepository->get($category->getId(), $storeId); - $category->addData( - [ - 'url_key' => $categoryFromRepository->getUrlKey(), - 'url_path' => $categoryFromRepository->getUrlPath() - ] - ); + $category->addData( + [ + 'url_key' => $categoryFromRepository->getUrlKey(), + 'url_path' => $categoryFromRepository->getUrlPath() + ] + ); } /** @@ -163,12 +163,12 @@ protected function isGlobalScope($storeId) /** * Generate list of urls per store * - * @param \Magento\Catalog\Model\Category $category * @param string $storeId + * @param \Magento\Catalog\Model\Category|null $category * @param int|null $rootCategoryId * @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] */ - protected function generateForSpecificStoreView(Category $category, $storeId, $rootCategoryId = null) + protected function generateForSpecificStoreView($storeId, Category $category = null, $rootCategoryId = null) { $urlRewritesSet = clone $this->urlRewritesSetPrototype; $urlRewritesSet->merge( diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryHashMap.php similarity index 80% rename from app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryMap.php rename to app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryHashMap.php index 162513f5b9809..88c4ea813264d 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryHashMap.php @@ -11,12 +11,12 @@ use Magento\Catalog\Api\Data\CategoryInterface; /** - * Map that holds data for category ids and it's subcategories ids + * Map that holds data for category ids and its subcategories ids */ -class DataCategoryMap implements DataMapInterface +class DataCategoryHashMap implements HashMapInterface { - /** @var array */ - private $data = []; + /** @var int[] */ + private $hashMap = []; /** @var CategoryRepository */ private $categoryRepository; @@ -47,10 +47,10 @@ public function __construct( */ public function getAllData($categoryId) { - if (empty($this->data[$categoryId])) { - $this->data[$categoryId] = $this->generateData($categoryId); + if (!isset($this->hashMap[$categoryId])) { + $this->hashMap[$categoryId] = $this->generateData($categoryId); } - return $this->data[$categoryId]; + return $this->hashMap[$categoryId]; } /** @@ -59,7 +59,7 @@ public function getAllData($categoryId) public function getData($categoryId, $key) { $this->getAllData($categoryId); - return $this->data[$categoryId][$key]; + return $this->hashMap[$categoryId][$key]; } /** @@ -81,7 +81,7 @@ private function generateData($categoryId) * @param CategoryInterface $category * @return int[] */ - private function getAllCategoryChildrenIds($category) + private function getAllCategoryChildrenIds(CategoryInterface $category) { $connection = $this->categoryResource->getConnection(); $select = $connection->select() @@ -96,9 +96,6 @@ private function getAllCategoryChildrenIds($category) */ public function resetData($categoryId) { - unset($this->data[$categoryId]); - if (empty($this->data)) { - $this->data = []; - } + unset($this->hashMap[$categoryId]); } } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUrlRewriteMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUrlRewriteDatabaseMap.php similarity index 66% rename from app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUrlRewriteMap.php rename to app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUrlRewriteDatabaseMap.php index fd0bd1c9fc744..9d8894f6c5c96 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUrlRewriteMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUrlRewriteDatabaseMap.php @@ -7,19 +7,20 @@ use Magento\Framework\App\ResourceConnection; use Magento\Framework\DB\TemporaryTableService; -use \Magento\Framework\DB\Select; +use Magento\Framework\DB\Select; +use Magento\UrlRewrite\Model\UrlRewritesSet; /** * Map that holds data for category url rewrites entity */ -class DataCategoryUrlRewriteMap implements DataMapInterface +class DataCategoryUrlRewriteDatabaseMap implements DatabaseMapInterface { const ENTITY_TYPE = 'category'; /** @var string[] */ - private $tableNames = []; + private $createdTableAdapters = []; - /** @var DataMapPoolInterface */ + /** @var HashMapPool */ private $dataMapPool; /** @var ResourceConnection */ @@ -30,12 +31,12 @@ class DataCategoryUrlRewriteMap implements DataMapInterface /** * @param ResourceConnection $connection - * @param DataMapPoolInterface $dataMapPool, - * @param TemporaryTableService $temporaryTableService, + * @param HashMapPool $dataMapPool, + * @param TemporaryTableService $temporaryTableService */ public function __construct( ResourceConnection $connection, - DataMapPoolInterface $dataMapPool, + HashMapPool $dataMapPool, TemporaryTableService $temporaryTableService ) { $this->connection = $connection; @@ -44,14 +45,16 @@ public function __construct( } /** - * {@inheritdoc} + * Generates data from categoryId and stores it into a temporary table + * + * @param $categoryId + * @return void */ - public function getAllData($categoryId) + private function generateTableAdapter($categoryId) { - if (empty($this->tableNames[$categoryId])) { - $this->tableNames[$categoryId] = $this->generateData($categoryId); + if (!isset($this->createdTableAdapters[$categoryId])) { + $this->createdTableAdapters[$categoryId] = $this->generateData($categoryId); } - return $this->getData($categoryId, ''); } /** @@ -66,7 +69,10 @@ private function generateData($categoryId) $select = $urlRewritesConnection->select() ->from( ['e' => $this->connection->getTableName('url_rewrite')], - ['e.*', 'hash_key' => new \Zend_Db_Expr('CONCAT(e.store_id,\'_\', e.entity_id)')] + ['e.*', 'hash_key' => new \Zend_Db_Expr( + "CONCAT(e.store_id,'" . UrlRewritesSet::SEPARATOR . "', e.entity_id)" + ) + ] ) ->where('entity_type = ?', self::ENTITY_TYPE) ->where( @@ -74,9 +80,9 @@ private function generateData($categoryId) 'entity_id', [ 'in' => array_merge( - $this->dataMapPool->getDataMap(DataCategoryUsedInProductsMap::class, $categoryId) + $this->dataMapPool->getDataMap(DataCategoryUsedInProductsHashMap::class, $categoryId) ->getAllData($categoryId), - $this->dataMapPool->getDataMap(DataCategoryMap::class, $categoryId) + $this->dataMapPool->getDataMap(DataCategoryHashMap::class, $categoryId) ->getAllData($categoryId) ) ] @@ -97,14 +103,13 @@ private function generateData($categoryId) /** * {@inheritdoc} */ - public function resetData($categoryId) + public function destroyTableAdapter($categoryId) { - $this->dataMapPool->resetDataMap(DataCategoryUsedInProductsMap::class, $categoryId); - $this->dataMapPool->resetDataMap(DataCategoryMap::class, $categoryId); - $this->temporaryTableService->dropTable($this->tableNames[$categoryId]); - unset($this->tableNames[$categoryId]); - if (empty($this->tableNames)) { - $this->tableNames = []; + $this->dataMapPool->resetMap(DataCategoryUsedInProductsHashMap::class, $categoryId); + $this->dataMapPool->resetMap(DataCategoryHashMap::class, $categoryId); + if (isset($this->createdTableAdapters[$categoryId])) { + $this->temporaryTableService->dropTable($this->createdTableAdapters[$categoryId]); + unset($this->createdTableAdapters[$categoryId]); } } @@ -117,11 +122,9 @@ public function resetData($categoryId) */ public function getData($categoryId, $key) { - if (!isset($this->tableNames[$categoryId])) { - $this->getAllData($categoryId); - } + $this->generateTableAdapter($categoryId); $urlRewritesConnection = $this->connection->getConnection(); - $select = $urlRewritesConnection->select()->from(['e' => $this->tableNames[$categoryId]]); + $select = $urlRewritesConnection->select()->from(['e' => $this->createdTableAdapters[$categoryId]]); if (strlen($key) > 0) { $select->where('hash_key = ?', $key); } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUsedInProductsMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUsedInProductsHashMap.php similarity index 66% rename from app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUsedInProductsMap.php rename to app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUsedInProductsHashMap.php index a4d07675816ce..9f142ef565249 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUsedInProductsMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUsedInProductsHashMap.php @@ -10,27 +10,27 @@ /** * Map that holds data for categories used by products found in root category */ -class DataCategoryUsedInProductsMap implements DataMapInterface +class DataCategoryUsedInProductsHashMap implements HashMapInterface { - /** @var array */ - private $data = []; + /** @var int[] */ + private $hashMap = []; - /** @var DataMapPoolInterface */ - private $dataMapPool; + /** @var HashMapPool */ + private $hashMapPool; /** @var ResourceConnection */ private $connection; /** * @param ResourceConnection $connection - * @param DataMapPoolInterface $dataMapPool + * @param HashMapPool $hashMapPool */ public function __construct( ResourceConnection $connection, - DataMapPoolInterface $dataMapPool + HashMapPool $hashMapPool ) { $this->connection = $connection; - $this->dataMapPool = $dataMapPool; + $this->hashMapPool = $hashMapPool; } /** @@ -38,10 +38,10 @@ public function __construct( */ public function getAllData($categoryId) { - if (empty($this->data[$categoryId])) { - $this->data[$categoryId] = $this->generateData($categoryId); + if (!isset($this->hashMap[$categoryId])) { + $this->hashMap[$categoryId] = $this->generateData($categoryId); } - return $this->data[$categoryId]; + return $this->hashMap[$categoryId]; } /** @@ -50,7 +50,7 @@ public function getAllData($categoryId) public function getData($categoryId, $key) { $this->getAllData($categoryId); - return $this->data[$categoryId][$key]; + return $this->hashMap[$categoryId][$key]; } /** @@ -68,8 +68,8 @@ private function generateData($categoryId) $productsLinkConnection->prepareSqlCondition( 'product_id', [ - 'in' => $this->dataMapPool->getDataMap( - DataProductMap::class, + 'in' => $this->hashMapPool->getDataMap( + DataProductHashMap::class, $categoryId )->getAllData($categoryId) ] @@ -79,8 +79,8 @@ private function generateData($categoryId) $productsLinkConnection->prepareSqlCondition( 'category_id', [ - 'nin' => $this->dataMapPool->getDataMap( - DataCategoryMap::class, + 'nin' => $this->hashMapPool->getDataMap( + DataCategoryHashMap::class, $categoryId )->getAllData($categoryId) ] @@ -95,11 +95,8 @@ private function generateData($categoryId) */ public function resetData($categoryId) { - $this->dataMapPool->resetDataMap(DataProductMap::class, $categoryId); - $this->dataMapPool->resetDataMap(DataCategoryMap::class, $categoryId); - unset($this->data[$categoryId]); - if (empty($this->data)) { - $this->data = []; - } + $this->hashMapPool->resetMap(DataProductHashMap::class, $categoryId); + $this->hashMapPool->resetMap(DataCategoryHashMap::class, $categoryId); + unset($this->hashMap[$categoryId]); } } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataMapPoolInterface.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataMapPoolInterface.php deleted file mode 100644 index b3a30303a907e..0000000000000 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataMapPoolInterface.php +++ /dev/null @@ -1,30 +0,0 @@ -collectionFactory = $collectionFactory; - $this->dataMapPool = $dataMapPool; + $this->hashMapPool = $hashMapPool; $this->connection = $connection; } @@ -45,10 +45,10 @@ public function __construct( */ public function getAllData($categoryId) { - if (empty($this->data[$categoryId])) { - $this->data[$categoryId] = $this->generateData($categoryId); + if (!isset($this->hashMap[$categoryId])) { + $this->hashMap[$categoryId] = $this->generateData($categoryId); } - return $this->data[$categoryId]; + return $this->hashMap[$categoryId]; } /** @@ -57,7 +57,7 @@ public function getAllData($categoryId) public function getData($categoryId, $key) { $this->getAllData($categoryId); - return $this->data[$categoryId][$key]; + return $this->hashMap[$categoryId][$key]; } /** @@ -79,8 +79,8 @@ private function generateData($categoryId) $productsCollection->getConnection()->prepareSqlCondition( 'cp.category_id', [ - 'in' => $this->dataMapPool->getDataMap( - DataCategoryMap::class, + 'in' => $this->hashMapPool->getDataMap( + DataCategoryHashMap::class, $categoryId )->getAllData($categoryId) ] @@ -95,10 +95,7 @@ private function generateData($categoryId) */ public function resetData($categoryId) { - $this->dataMapPool->resetDataMap(DataCategoryMap::class, $categoryId); - unset($this->data[$categoryId]); - if (empty($this->data)) { - $this->data = []; - } + $this->hashMapPool->resetMap(DataCategoryHashMap::class, $categoryId); + unset($this->hashMap[$categoryId]); } } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductUrlRewriteMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductUrlRewriteDatabaseMap.php similarity index 65% rename from app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductUrlRewriteMap.php rename to app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductUrlRewriteDatabaseMap.php index 31f172584e3a3..609841a2a87ce 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductUrlRewriteMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductUrlRewriteDatabaseMap.php @@ -7,20 +7,21 @@ use Magento\Framework\App\ResourceConnection; use Magento\Framework\DB\TemporaryTableService; -use \Magento\Framework\DB\Select; +use Magento\Framework\DB\Select; +use Magento\UrlRewrite\Model\UrlRewritesSet; /** * Map that holds data for category url rewrites entity * @SuppressWarnings(PHPCPD) */ -class DataProductUrlRewriteMap implements DataMapInterface +class DataProductUrlRewriteDatabaseMap implements DatabaseMapInterface { const ENTITY_TYPE = 'product'; /** @var string[] */ - private $tableNames = []; + private $createdTableAdapters = []; - /** @var DataMapPoolInterface */ + /** @var HashMapPool */ private $dataMapPool; /** @var ResourceConnection */ @@ -31,12 +32,12 @@ class DataProductUrlRewriteMap implements DataMapInterface /** * @param ResourceConnection $connection - * @param DataMapPoolInterface $dataMapPool, - * @param TemporaryTableService $temporaryTableService, + * @param HashMapPool $dataMapPool, + * @param TemporaryTableService $temporaryTableService */ public function __construct( ResourceConnection $connection, - DataMapPoolInterface $dataMapPool, + HashMapPool $dataMapPool, TemporaryTableService $temporaryTableService ) { $this->connection = $connection; @@ -45,14 +46,16 @@ public function __construct( } /** - * {@inheritdoc} + * Generates data from categoryId and stores it into a temporary table + * + * @param $categoryId + * @return void */ - public function getAllData($categoryId) + private function generateTableAdapter($categoryId) { - if (empty($this->tableNames[$categoryId])) { - $this->tableNames[$categoryId] = $this->generateData($categoryId); + if (!isset($this->createdTableAdapters[$categoryId])) { + $this->createdTableAdapters[$categoryId] = $this->generateData($categoryId); } - return $this->tableNames[$categoryId]; } /** @@ -60,10 +63,10 @@ public function getAllData($categoryId) */ public function getData($categoryId, $key) { - $this->getAllData($categoryId); + $this->generateTableAdapter($categoryId); $urlRewritesConnection = $this->connection->getConnection(); $select = $urlRewritesConnection->select() - ->from(['e' => $this->tableNames[$categoryId]]) + ->from(['e' => $this->createdTableAdapters[$categoryId]]) ->where('hash_key = ?', $key); return $urlRewritesConnection->fetchAll($select); } @@ -80,14 +83,17 @@ private function generateData($categoryId) $select = $urlRewritesConnection->select() ->from( ['e' => $this->connection->getTableName('url_rewrite')], - ['e.*', 'hash_key' => new \Zend_Db_Expr('CONCAT(e.store_id,\'_\', e.entity_id)')] + ['e.*', 'hash_key' => new \Zend_Db_Expr( + "CONCAT(e.store_id,'" . UrlRewritesSet::SEPARATOR . "', e.entity_id)" + ) + ] ) ->where('entity_type = ?', self::ENTITY_TYPE) ->where( $urlRewritesConnection->prepareSqlCondition( 'entity_id', [ - 'in' => $this->dataMapPool->getDataMap(DataProductMap::class, $categoryId) + 'in' => $this->dataMapPool->getDataMap(DataProductHashMap::class, $categoryId) ->getAllData($categoryId) ] ) @@ -107,13 +113,12 @@ private function generateData($categoryId) /** * {@inheritdoc} */ - public function resetData($categoryId) + public function destroyTableAdapter($categoryId) { - $this->dataMapPool->resetDataMap(DataProductMap::class, $categoryId); - $this->temporaryTableService->dropTable($this->tableNames[$categoryId]); - unset($this->tableNames[$categoryId]); - if (empty($this->tableNames)) { - $this->tableNames = []; + $this->dataMapPool->resetMap(DataProductHashMap::class, $categoryId); + if (isset($this->createdTableAdapters[$categoryId])) { + $this->temporaryTableService->dropTable($this->createdTableAdapters[$categoryId]); + unset($this->createdTableAdapters[$categoryId]); } } } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DatabaseMapInterface.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DatabaseMapInterface.php new file mode 100644 index 0000000000000..6090baf3d9011 --- /dev/null +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DatabaseMapInterface.php @@ -0,0 +1,38 @@ +dataArray[$key])) { - $this->dataArray[$key]->resetData($categoryId); + $this->dataArray[$key]->destroyTableAdapter($categoryId); unset($this->dataArray[$key]); } } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataMapInterface.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/HashMapInterface.php similarity index 58% rename from app/code/Magento/CatalogUrlRewrite/Model/Map/DataMapInterface.php rename to app/code/Magento/CatalogUrlRewrite/Model/Map/HashMapInterface.php index 97d4d3ab64650..ec23104f5d817 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataMapInterface.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/HashMapInterface.php @@ -8,9 +8,14 @@ use \Magento\Framework\DB\Select; /** - * Interface for a data map + * Interface for a hash data map + * It is used for classes tht would build hash maps and store them into memory + * The initialization is done transparently whenever getAllData or getData is called + * The map, upon initialization, might have a dependency on some other DataMapInterfaces + * The map has to free memory after we're done using it + * We need to destroy those maps too when calling resetData */ -interface DataMapInterface +interface HashMapInterface { /** * Gets all data from a map identified by a category Id @@ -30,7 +35,7 @@ public function getAllData($categoryId); public function getData($categoryId, $key); /** - * Resets current map and it's dependencies + * Resets current map by freeing memory and also to its dependencies * * @param int $categoryId * @return void diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/HashMapPool.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/HashMapPool.php new file mode 100644 index 0000000000000..7c424cd773857 --- /dev/null +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/HashMapPool.php @@ -0,0 +1,77 @@ +objectManager = $objectManager; + } + + /** + * Gets a map by instance and category Id + * + * @param string $instanceName + * @param int $categoryId + * @return HashMapInterface + * @throws \Exception + */ + public function getDataMap($instanceName, $categoryId) + { + $key = $instanceName . '-' . $categoryId; + $reflectionClass = new \ReflectionClass($instanceName); + if (!$reflectionClass->implementsInterface(HashMapInterface::class)) { + throw new \Exception($instanceName . ' does not implement interface ' . HashMapInterface::class); + } + if (!isset($this->dataArray[$key])) { + $this->dataArray[$key] = $this->objectManager->create( + $instanceName, + [ + 'category' => $categoryId + ] + ); + } + return $this->dataArray[$key]; + } + + /** + * Resets data in a hash map by instance name and category Id + * + * @param string $instanceName + * @param int $categoryId + * @return void + */ + public function resetMap($instanceName, $categoryId) + { + $key = $instanceName . '-' . $categoryId; + if (isset($this->dataArray[$key])) { + $this->dataArray[$key]->resetData($categoryId); + unset($this->dataArray[$key]); + } + } +} diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/UrlRewriteMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/UrlRewriteFinder.php similarity index 64% rename from app/code/Magento/CatalogUrlRewrite/Model/Map/UrlRewriteMap.php rename to app/code/Magento/CatalogUrlRewrite/Model/Map/UrlRewriteFinder.php index 70bb44c75b8f4..52bf538aa3425 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/UrlRewriteMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/UrlRewriteFinder.php @@ -11,15 +11,16 @@ use Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory; /** - * Allows query to DataCategoryUrlRewriteMap and DataProductUrlRewriteMap class or UrlFinderInterface by identifiers + * Allows query to Category and Product UrlRewrite Database Map or UrlFinderInterface by identifiers + * */ -class UrlRewriteMap +class UrlRewriteFinder { const ENTITY_TYPE_CATEGORY = 'category'; const ENTITY_TYPE_PRODUCT = 'product'; - /** @var DataMapPoolInterface */ - private $dataMapPool; + /** @var DatabaseMapPool */ + private $databaseMapPool; /** @var UrlFinderInterface */ private $urlFinder; @@ -27,18 +28,27 @@ class UrlRewriteMap /** @var UrlRewrite */ private $urlRewritePrototype; + /** @var array */ + private $urlRewriteClassNames; + /** - * @param DataMapPoolInterface $dataMapPool + * @param DatabaseMapPool $databaseMapPool * @param UrlFinderInterface $urlFinder * @param UrlRewriteFactory $urlRewriteFactory + * @param string[] $urlRewriteClassNames */ public function __construct( - DataMapPoolInterface $dataMapPool, + DatabaseMapPool $databaseMapPool, UrlFinderInterface $urlFinder, - UrlRewriteFactory $urlRewriteFactory + UrlRewriteFactory $urlRewriteFactory, + array $urlRewriteClassNames = [ + self::ENTITY_TYPE_PRODUCT => DataProductUrlRewriteDatabaseMap::class, + self::ENTITY_TYPE_CATEGORY => DataCategoryUrlRewriteDatabaseMap::class + ] ) { - $this->dataMapPool = $dataMapPool; + $this->databaseMapPool = $databaseMapPool; $this->urlFinder = $urlFinder; + $this->urlRewriteClassNames = $urlRewriteClassNames; $this->urlRewritePrototype = $urlRewriteFactory->create(); } @@ -51,16 +61,15 @@ public function __construct( * @param int|null $rootCategoryId * @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] */ - public function getByIdentifiers($entityId, $storeId, $entityType, $rootCategoryId = null) + public function findAllByData($entityId, $storeId, $entityType, $rootCategoryId = null) { - if ($rootCategoryId && is_numeric($entityId) && is_numeric($storeId) && is_string($entityType)) { - $map = null; - if ($entityType === self::ENTITY_TYPE_PRODUCT) { - $map = $this->dataMapPool->getDataMap(DataProductUrlRewriteMap::class, $rootCategoryId); - } elseif ($entityType === self::ENTITY_TYPE_CATEGORY) { - $map = $this->dataMapPool->getDataMap(DataCategoryUrlRewriteMap::class, $rootCategoryId); - } - + if ($rootCategoryId + && is_numeric($entityId) + && is_numeric($storeId) + && is_string($entityType) + && isset($this->urlRewriteClassNames[$entityType]) + ) { + $map = $this->databaseMapPool->getDataMap($this->urlRewriteClassNames[$entityType], $rootCategoryId); if ($map) { $key = $storeId . '_' . $entityId; return $this->arrayToUrlRewriteObject($map->getData($rootCategoryId, $key)); @@ -77,12 +86,12 @@ public function getByIdentifiers($entityId, $storeId, $entityType, $rootCategory } /** - * Transform array values to url rewrite object values + * Transfer array values to url rewrite object values * * @param array $data * @return UrlRewrite[] */ - private function arrayToUrlRewriteObject($data) + private function arrayToUrlRewriteObject(array $data) { foreach ($data as $key => $array) { $data[$key] = $this->createUrlRewrite($array); @@ -91,12 +100,12 @@ private function arrayToUrlRewriteObject($data) } /** - * Clone url rewrite object + * Creates url rewrite object and sets $data to its properties by key->value * * @param array $data * @return UrlRewrite */ - private function createUrlRewrite($data) + private function createUrlRewrite(array $data) { $dataObject = clone $this->urlRewritePrototype; $dataObject->setUrlRewriteId($data['url_rewrite_id']); diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php index ddb91986f249d..9f49646e4e4ce 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php @@ -14,7 +14,7 @@ use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator; use Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator; use Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory; -use Magento\CatalogUrlRewrite\Model\Map\UrlRewriteMap; +use Magento\CatalogUrlRewrite\Model\Map\UrlRewriteFinder; use Magento\Framework\App\ObjectManager; use Magento\UrlRewrite\Model\UrlRewritesSetFactory; @@ -50,7 +50,7 @@ class CurrentUrlRewritesRegenerator /** @var UrlRewrite */ private $urlRewritePrototype; - /** @var UrlRewriteMap */ + /** @var UrlRewriteFinder */ private $urlRewriteMap; /** @var \Magento\UrlRewrite\Model\UrlRewritesSet */ @@ -60,21 +60,21 @@ class CurrentUrlRewritesRegenerator * @param UrlFinderInterface $urlFinder * @param ProductUrlPathGenerator $productUrlPathGenerator * @param UrlRewriteFactory $urlRewriteFactory - * @param UrlRewriteMap|null $urlRewriteMap + * @param UrlRewriteFinder|null $urlRewriteMap * @param \Magento\UrlRewrite\Model\UrlRewritesSetFactory|null $urlRewritesSetFactory */ public function __construct( UrlFinderInterface $urlFinder, ProductUrlPathGenerator $productUrlPathGenerator, UrlRewriteFactory $urlRewriteFactory, - UrlRewriteMap $urlRewriteMap = null, + UrlRewriteFinder $urlRewriteMap = null, UrlRewritesSetFactory $urlRewritesSetFactory = null ) { $this->urlFinder = $urlFinder; $this->productUrlPathGenerator = $productUrlPathGenerator; $this->urlRewriteFactory = $urlRewriteFactory; $this->urlRewritePrototype = $urlRewriteFactory->create(); - $this->urlRewriteMap = $urlRewriteMap ?: ObjectManager::getInstance()->get(UrlRewriteMap::class); + $this->urlRewriteMap = $urlRewriteMap ?: ObjectManager::getInstance()->get(UrlRewriteFinder::class); $urlRewritesSetFactory = $urlRewritesSetFactory ?: ObjectManager::getInstance() ->get(UrlRewritesSetFactory::class); $this->urlRewritesSetPrototype = $urlRewritesSetFactory->create(); @@ -92,7 +92,7 @@ public function __construct( public function generate($storeId, Product $product, ObjectRegistry $productCategories, $rootCategoryId = null) { $urlRewritesSet = clone $this->urlRewritesSetPrototype; - $currentUrlRewrites = $this->urlRewriteMap->getByIdentifiers( + $currentUrlRewrites = $this->urlRewriteMap->findAllByData( $product->getEntityId(), $storeId, ProductUrlRewriteGenerator::ENTITY_TYPE, @@ -118,10 +118,10 @@ public function generate($storeId, Product $product, ObjectRegistry $productCate * @param UrlRewrite $url * @param int $storeId * @param Category|null $category - * @param Product $product + * @param Product|null $product * @return UrlRewrite[] */ - protected function generateForAutogenerated(UrlRewrite $url, $storeId, $category, Product $product) + protected function generateForAutogenerated($url, $storeId, $category, $product = null) { if ($product->getData('save_rewrites_history')) { $targetPath = $this->productUrlPathGenerator->getUrlPathWithSuffix($product, $storeId, $category); @@ -146,10 +146,10 @@ protected function generateForAutogenerated(UrlRewrite $url, $storeId, $category * @param UrlRewrite $url * @param int $storeId * @param Category|null $category - * @param Product $product + * @param Product|null $product * @return UrlRewrite[] */ - protected function generateForCustom(UrlRewrite $url, $storeId, $category, Product $product) + protected function generateForCustom($url, $storeId, $category, $product = null) { $targetPath = $url->getRedirectType() ? $this->productUrlPathGenerator->getUrlPathWithSuffix($product, $storeId, $category) @@ -172,10 +172,10 @@ protected function generateForCustom(UrlRewrite $url, $storeId, $category, Produ /** * @param UrlRewrite $url - * @param ObjectRegistry $productCategories + * @param ObjectRegistry|null $productCategories * @return Category|null|bool */ - protected function retrieveCategoryFromMetadata(UrlRewrite $url, ObjectRegistry $productCategories) + protected function retrieveCategoryFromMetadata($url, ObjectRegistry $productCategories = null) { $metadata = $url->getMetadata(); if (isset($metadata['category_id'])) { diff --git a/app/code/Magento/CatalogUrlRewrite/Observer/CategoryProcessUrlRewriteSavingObserver.php b/app/code/Magento/CatalogUrlRewrite/Observer/CategoryProcessUrlRewriteSavingObserver.php index 2820f90994432..71a68ef340aca 100644 --- a/app/code/Magento/CatalogUrlRewrite/Observer/CategoryProcessUrlRewriteSavingObserver.php +++ b/app/code/Magento/CatalogUrlRewrite/Observer/CategoryProcessUrlRewriteSavingObserver.php @@ -9,9 +9,9 @@ use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator; use Magento\CatalogUrlRewrite\Model\UrlRewriteBunchReplacer; use Magento\Framework\Event\ObserverInterface; -use Magento\CatalogUrlRewrite\Model\Map\DataMapPoolInterface; -use Magento\CatalogUrlRewrite\Model\Map\DataProductUrlRewriteMap; -use Magento\CatalogUrlRewrite\Model\Map\DataCategoryUrlRewriteMap; +use Magento\CatalogUrlRewrite\Model\Map\DatabaseMapPool; +use Magento\CatalogUrlRewrite\Model\Map\DataCategoryUrlRewriteDatabaseMap; +use Magento\CatalogUrlRewrite\Model\Map\DataProductUrlRewriteDatabaseMap; class CategoryProcessUrlRewriteSavingObserver implements ObserverInterface { @@ -24,25 +24,34 @@ class CategoryProcessUrlRewriteSavingObserver implements ObserverInterface /** @var UrlRewriteHandler */ private $urlRewriteHandler; - /** @var DataMapPoolInterface */ - private $dataMapPoolInterface; + /** @var DatabaseMapPool */ + private $databaseMapPool; + + /** @var string[] */ + private $dataUrlRewriteClassNames; /** * @param CategoryUrlRewriteGenerator $categoryUrlRewriteGenerator * @param UrlRewriteHandler $urlRewriteHandler * @param UrlRewriteBunchReplacer $urlRewriteBunchReplacer - * @param DataMapPoolInterface $dataMapPoolInterface + * @param DatabaseMapPool $databaseMapPool + * @param string[] $dataUrlRewriteClassNames */ public function __construct( CategoryUrlRewriteGenerator $categoryUrlRewriteGenerator, UrlRewriteHandler $urlRewriteHandler, UrlRewriteBunchReplacer $urlRewriteBunchReplacer, - DataMapPoolInterface $dataMapPoolInterface + DatabaseMapPool $databaseMapPool, + array $dataUrlRewriteClassNames = [ + DataCategoryUrlRewriteDatabaseMap::class, + DataProductUrlRewriteDatabaseMap::class + ] ) { $this->categoryUrlRewriteGenerator = $categoryUrlRewriteGenerator; $this->urlRewriteHandler = $urlRewriteHandler; $this->urlRewriteBunchReplacer = $urlRewriteBunchReplacer; - $this->dataMapPoolInterface = $dataMapPoolInterface; + $this->databaseMapPool = $databaseMapPool; + $this->dataUrlRewriteClassNames = $dataUrlRewriteClassNames; } /** @@ -62,14 +71,43 @@ public function execute(\Magento\Framework\Event\Observer $observer) || $category->dataHasChangedFor('is_anchor') || $category->getIsChangedProductList() ) { + $this->initializeUrlRewritesDataMaps($category); + $categoryUrlRewriteResult = $this->categoryUrlRewriteGenerator->generate($category); $this->urlRewriteBunchReplacer->doBunchReplace($categoryUrlRewriteResult); $productUrlRewriteResult = $this->urlRewriteHandler->generateProductUrlRewrites($category); $this->urlRewriteBunchReplacer->doBunchReplace($productUrlRewriteResult); - $this->dataMapPoolInterface->resetDataMap(DataCategoryUrlRewriteMap::class, $category->getEntityId()); - $this->dataMapPoolInterface->resetDataMap(DataProductUrlRewriteMap::class, $category->getEntityId()); + $this->resetUrlRewritesDataMaps($category); + } + } + + /** + * Initializes data maps to be further used + * + * @param Category $category + * @return void + */ + private function initializeUrlRewritesDataMaps($category) + { + foreach ($this->dataUrlRewriteClassNames as $className) { + $this->databaseMapPool->getDataMap($className, $category->getEntityId()); + } + + } + + /** + * Resets used data maps to free up memory and temporary tables + * + * @param Category $category + * @return void + */ + private function resetUrlRewritesDataMaps($category) + { + foreach ($this->dataUrlRewriteClassNames as $className) { + $this->databaseMapPool->resetMap($className, $category->getEntityId()); } + } } diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/CurrentUrlRewritesRegeneratorTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/CurrentUrlRewritesRegeneratorTest.php index cae82e325ccc5..b70c3d33a1581 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/CurrentUrlRewritesRegeneratorTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/CurrentUrlRewritesRegeneratorTest.php @@ -46,7 +46,7 @@ protected function setUp() $this->categoryUrlPathGenerator = $this->getMockBuilder( \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator::class )->disableOriginalConstructor()->getMock(); - $this->urlRewriteMap = $this->getMockBuilder(\Magento\CatalogUrlRewrite\Model\Map\UrlRewriteMap::class) + $this->urlRewriteMap = $this->getMockBuilder(\Magento\CatalogUrlRewrite\Model\Map\UrlRewriteFinder::class) ->disableOriginalConstructor()->getMock(); $this->urlRewriteFactory->expects($this->once())->method('create') ->willReturn($this->urlRewrite); diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryMapTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryHashMapTest.php similarity index 93% rename from app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryMapTest.php rename to app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryHashMapTest.php index c0d3d1a0d7586..bdca177eee3f1 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryMapTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryHashMapTest.php @@ -10,14 +10,14 @@ use Magento\Framework\DB\Select; use Magento\Catalog\Model\CategoryRepository; use Magento\Catalog\Api\Data\CategoryInterface; -use Magento\CatalogUrlRewrite\Model\Map\DataCategoryMap; +use Magento\CatalogUrlRewrite\Model\Map\DataCategoryHashMap; use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; use Magento\Framework\DB\Adapter\AdapterInterface; /** - * Class DataCategoryMapTest + * Class DataCategoryHashMapTest */ -class DataCategoryMapTest extends \PHPUnit_Framework_TestCase +class DataCategoryHashMapTest extends \PHPUnit_Framework_TestCase { /** @var CategoryRepository|\PHPUnit_Framework_MockObject_MockObject */ private $categoryRepository; @@ -28,7 +28,7 @@ class DataCategoryMapTest extends \PHPUnit_Framework_TestCase /** @var CategoryResource|\PHPUnit_Framework_MockObject_MockObject */ private $categoryResource; - /** @var DataCategoryMap|\PHPUnit_Framework_MockObject_MockObject */ + /** @var DataCategoryHashMap|\PHPUnit_Framework_MockObject_MockObject */ private $model; protected function setUp() @@ -44,7 +44,7 @@ protected function setUp() ); $this->model = (new ObjectManager($this))->getObject( - DataCategoryMap::class, + DataCategoryHashMap::class, [ 'categoryRepository' => $this->categoryRepository, 'collection' => $this->collection, diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryUrlRewriteMapTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryUrlRewriteDatabaseMapTest.php similarity index 76% rename from app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryUrlRewriteMapTest.php rename to app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryUrlRewriteDatabaseMapTest.php index 7018323e00765..396e00310fe45 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryUrlRewriteMapTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryUrlRewriteDatabaseMapTest.php @@ -6,28 +6,28 @@ namespace Magento\CatalogUrlRewrite\Test\Unit\Model\Map; use Magento\Framework\DB\Select; -use Magento\CatalogUrlRewrite\Model\Map\DataMapPoolInterface; -use Magento\CatalogUrlRewrite\Model\Map\DataProductMap; -use Magento\CatalogUrlRewrite\Model\Map\DataCategoryMap; -use Magento\CatalogUrlRewrite\Model\Map\DataCategoryUsedInProductsMap; -use Magento\CatalogUrlRewrite\Model\Map\DataCategoryUrlRewriteMap; +use Magento\CatalogUrlRewrite\Model\Map\HashMapPool; +use Magento\CatalogUrlRewrite\Model\Map\DataProductHashMap; +use Magento\CatalogUrlRewrite\Model\Map\DataCategoryHashMap; +use Magento\CatalogUrlRewrite\Model\Map\DataCategoryUsedInProductsHashMap; +use Magento\CatalogUrlRewrite\Model\Map\DataCategoryUrlRewriteDatabaseMap; use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; use Magento\Framework\DB\Adapter\AdapterInterface; use Magento\Framework\App\ResourceConnection; use Magento\Framework\DB\TemporaryTableService; /** - * Class DataCategoryUrlRewriteMapTest + * Class DataCategoryUrlRewriteDatabaseMapTest */ -class DataCategoryUrlRewriteMapTest extends \PHPUnit_Framework_TestCase +class DataCategoryUrlRewriteDatabaseMapTest extends \PHPUnit_Framework_TestCase { - /** @var DataMapPoolInterface|\PHPUnit_Framework_MockObject_MockObject */ + /** @var HashMapPool|\PHPUnit_Framework_MockObject_MockObject */ private $dataMapPoolMock; - /** @var DataCategoryMap|\PHPUnit_Framework_MockObject_MockObject */ + /** @var DataCategoryHashMap|\PHPUnit_Framework_MockObject_MockObject */ private $dataCategoryMapMock; - /** @var DataCategoryUsedInProductsMap|\PHPUnit_Framework_MockObject_MockObject */ + /** @var DataCategoryUsedInProductsHashMap|\PHPUnit_Framework_MockObject_MockObject */ private $dataCategoryUsedInProductsMapMock; /** @var TemporaryTableService|\PHPUnit_Framework_MockObject_MockObject */ @@ -36,15 +36,15 @@ class DataCategoryUrlRewriteMapTest extends \PHPUnit_Framework_TestCase /** @var ResourceConnection|\PHPUnit_Framework_MockObject_MockObject */ private $connectionMock; - /** @var DataCategoryUrlRewriteMap|\PHPUnit_Framework_MockObject_MockObject */ + /** @var DataCategoryUrlRewriteDatabaseMap|\PHPUnit_Framework_MockObject_MockObject */ private $model; protected function setUp() { - $this->dataMapPoolMock = $this->getMock(DataMapPoolInterface::class); - $this->dataCategoryMapMock = $this->getMock(DataProductMap::class, [], [], '', false); + $this->dataMapPoolMock = $this->getMock(HashMapPool::class, [], [], '', false); + $this->dataCategoryMapMock = $this->getMock(DataProductHashMap::class, [], [], '', false); $this->dataCategoryUsedInProductsMapMock = $this->getMock( - DataCategoryUsedInProductsMap::class, + DataCategoryUsedInProductsHashMap::class, [], [], '', @@ -58,12 +58,11 @@ protected function setUp() ->willReturnOnConsecutiveCalls($this->dataCategoryUsedInProductsMapMock, $this->dataCategoryMapMock); $this->model = (new ObjectManager($this))->getObject( - DataCategoryUrlRewriteMap::class, + DataCategoryUrlRewriteDatabaseMap::class, [ 'connection' => $this->connectionMock, 'dataMapPool' => $this->dataMapPoolMock, - 'temporaryTableService' => $this->temporaryTableServiceMock, - 'mapData' => [], + 'temporaryTableService' => $this->temporaryTableServiceMock ] ); } @@ -92,7 +91,7 @@ public function testGetAllData() ->willReturn($selectMock); $connectionMock->expects($this->any()) ->method('fetchAll') - ->willReturnOnConsecutiveCalls($productStoreIds, $productStoreIds[3]); + ->willReturn($productStoreIds[3]); $selectMock->expects($this->any()) ->method('from') ->willReturnSelf(); @@ -121,7 +120,6 @@ public function testGetAllData() ) ->willReturn('tempTableName'); - $this->assertEquals($productStoreIds, $this->model->getAllData(1)); $this->assertEquals($productStoreIds[3], $this->model->getData(1, '3_1')); } } diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryUsedInProductsMapTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryUsedInProductsHashMapTest.php similarity index 75% rename from app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryUsedInProductsMapTest.php rename to app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryUsedInProductsHashMapTest.php index 16c0e4e8156ba..0647c30b43f10 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryUsedInProductsMapTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryUsedInProductsHashMapTest.php @@ -6,39 +6,39 @@ namespace Magento\CatalogUrlRewrite\Test\Unit\Model\Map; use Magento\Framework\DB\Select; -use Magento\CatalogUrlRewrite\Model\Map\DataMapPoolInterface; -use Magento\CatalogUrlRewrite\Model\Map\DataProductMap; -use Magento\CatalogUrlRewrite\Model\Map\DataCategoryMap; -use Magento\CatalogUrlRewrite\Model\Map\DataCategoryUsedInProductsMap; +use Magento\CatalogUrlRewrite\Model\Map\HashMapPool; +use Magento\CatalogUrlRewrite\Model\Map\DataProductHashMap; +use Magento\CatalogUrlRewrite\Model\Map\DataCategoryHashMap; +use Magento\CatalogUrlRewrite\Model\Map\DataCategoryUsedInProductsHashMap; use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; use Magento\Framework\DB\Adapter\AdapterInterface; use Magento\Framework\App\ResourceConnection; /** - * Class DataCategoryUsedInProductsMapTest + * Class DataCategoryUsedInProductsHashMapTest */ -class DataCategoryUsedInProductsMapTest extends \PHPUnit_Framework_TestCase +class DataCategoryUsedInProductsHashMapTest extends \PHPUnit_Framework_TestCase { - /** @var DataMapPoolInterface|\PHPUnit_Framework_MockObject_MockObject */ + /** @var HashMapPool|\PHPUnit_Framework_MockObject_MockObject */ private $dataMapPoolMock; - /** @var DataCategoryMap|\PHPUnit_Framework_MockObject_MockObject */ + /** @var DataCategoryHashMap|\PHPUnit_Framework_MockObject_MockObject */ private $dataCategoryMapMock; - /** @var DataProductMap|\PHPUnit_Framework_MockObject_MockObject */ + /** @var DataProductHashMap|\PHPUnit_Framework_MockObject_MockObject */ private $dataProductMapMock; /** @var ResourceConnection|\PHPUnit_Framework_MockObject_MockObject */ private $connectionMock; - /** @var DataCategoryUsedInProductsMap|\PHPUnit_Framework_MockObject_MockObject */ + /** @var DataCategoryUsedInProductsHashMap|\PHPUnit_Framework_MockObject_MockObject */ private $model; protected function setUp() { - $this->dataMapPoolMock = $this->getMock(DataMapPoolInterface::class); - $this->dataCategoryMapMock = $this->getMock(DataCategoryMap::class, [], [], '', false); - $this->dataProductMapMock = $this->getMock(DataProductMap::class, [], [], '', false); + $this->dataMapPoolMock = $this->getMock(HashMapPool::class, [], [], '', false); + $this->dataCategoryMapMock = $this->getMock(DataCategoryHashMap::class, [], [], '', false); + $this->dataProductMapMock = $this->getMock(DataProductHashMap::class, [], [], '', false); $this->connectionMock = $this->getMock(ResourceConnection::class, [], [], '', false); $this->dataMapPoolMock->expects($this->any()) @@ -53,11 +53,10 @@ protected function setUp() ); $this->model = (new ObjectManager($this))->getObject( - DataCategoryUsedInProductsMap::class, + DataCategoryUsedInProductsHashMap::class, [ 'connection' => $this->connectionMock, - 'dataMapPool' => $this->dataMapPoolMock, - 'mapData' => [], + 'hashMapPool' => $this->dataMapPoolMock ] ); } @@ -93,10 +92,10 @@ public function testGetAllData() ->willReturnSelf(); $this->dataMapPoolMock->expects($this->at(4)) ->method('resetDataMap') - ->with(DataProductMap::class, 1); + ->with(DataProductHashMap::class, 1); $this->dataMapPoolMock->expects($this->at(5)) ->method('resetDataMap') - ->with(DataCategoryMap::class, 1); + ->with(DataCategoryHashMap::class, 1); $this->assertEquals($categoryIds, $this->model->getAllData(1)); $this->assertEquals($categoryIds[2], $this->model->getData(1, 2)); diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataProductMapTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataProductHashMapTest.php similarity index 83% rename from app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataProductMapTest.php rename to app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataProductHashMapTest.php index ae0aab97ae8fe..024c3ee491116 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataProductMapTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataProductHashMapTest.php @@ -9,9 +9,9 @@ use Magento\Catalog\Model\ResourceModel\Category as CategoryResource; use Magento\Framework\DB\Select; use Magento\Catalog\Model\ProductRepository; -use Magento\CatalogUrlRewrite\Model\Map\DataMapPoolInterface; -use Magento\CatalogUrlRewrite\Model\Map\DataProductMap; -use Magento\CatalogUrlRewrite\Model\Map\DataCategoryMap; +use Magento\CatalogUrlRewrite\Model\Map\HashMapPool; +use Magento\CatalogUrlRewrite\Model\Map\DataProductHashMap; +use Magento\CatalogUrlRewrite\Model\Map\DataCategoryHashMap; use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; use Magento\Framework\DB\Adapter\AdapterInterface; use Magento\Framework\App\ResourceConnection; @@ -19,14 +19,14 @@ use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection; /** - * Class DataProductMapTest + * Class DataProductHashMapTest */ -class DataProductMapTest extends \PHPUnit_Framework_TestCase +class DataProductHashMapTest extends \PHPUnit_Framework_TestCase { - /** @var DataMapPoolInterface|\PHPUnit_Framework_MockObject_MockObject */ + /** @var HashMapPool|\PHPUnit_Framework_MockObject_MockObject */ private $dataMapPoolMock; - /** @var DataCategoryMap|\PHPUnit_Framework_MockObject_MockObject */ + /** @var DataCategoryHashMap|\PHPUnit_Framework_MockObject_MockObject */ private $dataCategoryMapMock; /** @@ -39,13 +39,13 @@ class DataProductMapTest extends \PHPUnit_Framework_TestCase */ private $productCollectionMock; - /** @var DataProductMap|\PHPUnit_Framework_MockObject_MockObject */ + /** @var DataProductHashMap|\PHPUnit_Framework_MockObject_MockObject */ private $model; protected function setUp() { - $this->dataMapPoolMock = $this->getMock(DataMapPoolInterface::class); - $this->dataCategoryMapMock = $this->getMock(DataCategoryMap::class, [], [], '', false); + $this->dataMapPoolMock = $this->getMock(HashMapPool::class, [], [], '', false); + $this->dataCategoryMapMock = $this->getMock(DataCategoryHashMap::class, [], [], '', false); $this->collectionFactoryMock = $this->getMock(CollectionFactory::class, ['create'], [], '', false); $this->productCollectionMock = $this->getMock( ProductCollection::class, @@ -64,11 +64,10 @@ protected function setUp() ->willReturn($this->dataCategoryMapMock); $this->model = (new ObjectManager($this))->getObject( - DataProductMap::class, + DataProductHashMap::class, [ 'collectionFactory' => $this->collectionFactoryMock, - 'dataMapPool' => $this->dataMapPoolMock, - 'mapData' => [], + 'hashMapPool' => $this->dataMapPoolMock ] ); } @@ -110,7 +109,7 @@ public function testGetAllData() ->willReturn([]); $this->dataMapPoolMock->expects($this->any()) ->method('resetDataMap') - ->with(DataCategoryMap::class, 1); + ->with(DataCategoryHashMap::class, 1); $this->assertEquals($productIds, $this->model->getAllData(1)); $this->assertEquals($productIds[2], $this->model->getData(1, 2)); $this->assertEquals($productIdsOther, $this->model->getAllData(2)); diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataProductUrlRewriteMapTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataProductUrlRewriteDatabaseMapTest.php similarity index 73% rename from app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataProductUrlRewriteMapTest.php rename to app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataProductUrlRewriteDatabaseMapTest.php index cf52346e112cc..5999b884a47b7 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataProductUrlRewriteMapTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataProductUrlRewriteDatabaseMapTest.php @@ -6,23 +6,23 @@ namespace Magento\CatalogUrlRewrite\Test\Unit\Model\Map; use Magento\Framework\DB\Select; -use Magento\CatalogUrlRewrite\Model\Map\DataMapPoolInterface; -use Magento\CatalogUrlRewrite\Model\Map\DataProductMap; -use Magento\CatalogUrlRewrite\Model\Map\DataCategoryUrlRewriteMap; +use Magento\CatalogUrlRewrite\Model\Map\HashMapPool; +use Magento\CatalogUrlRewrite\Model\Map\DataProductHashMap; +use Magento\CatalogUrlRewrite\Model\Map\DataProductUrlRewriteDatabaseMap; use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; use Magento\Framework\DB\Adapter\AdapterInterface; use Magento\Framework\App\ResourceConnection; use Magento\Framework\DB\TemporaryTableService; /** - * Class DataProductUrlRewriteMapTest + * Class DataProductUrlRewriteDatabaseMapTest */ -class DataProductUrlRewriteMapTest extends \PHPUnit_Framework_TestCase +class DataProductUrlRewriteDatabaseMapTest extends \PHPUnit_Framework_TestCase { - /** @var DataMapPoolInterface|\PHPUnit_Framework_MockObject_MockObject */ + /** @var HashMapPool|\PHPUnit_Framework_MockObject_MockObject */ private $dataMapPoolMock; - /** @var DataProductMap|\PHPUnit_Framework_MockObject_MockObject */ + /** @var DataProductHashMap|\PHPUnit_Framework_MockObject_MockObject */ private $dataProductMapMock; /** @var TemporaryTableService|\PHPUnit_Framework_MockObject_MockObject */ @@ -31,13 +31,13 @@ class DataProductUrlRewriteMapTest extends \PHPUnit_Framework_TestCase /** @var ResourceConnection|\PHPUnit_Framework_MockObject_MockObject */ private $connectionMock; - /** @var DataProductMap|\PHPUnit_Framework_MockObject_MockObject */ + /** @var DataProductUrlRewriteDatabaseMap|\PHPUnit_Framework_MockObject_MockObject */ private $model; protected function setUp() { - $this->dataMapPoolMock = $this->getMock(DataMapPoolInterface::class); - $this->dataProductMapMock = $this->getMock(DataProductMap::class, [], [], '', false); + $this->dataMapPoolMock = $this->getMock(HashMapPool::class, [], [], '', false); + $this->dataProductMapMock = $this->getMock(DataProductHashMap::class, [], [], '', false); $this->temporaryTableServiceMock = $this->getMock(TemporaryTableService::class, [], [], '', false); $this->connectionMock = $this->getMock(ResourceConnection::class, [], [], '', false); @@ -46,12 +46,11 @@ protected function setUp() ->willReturn($this->dataProductMapMock); $this->model = (new ObjectManager($this))->getObject( - DataCategoryUrlRewriteMap::class, + DataProductUrlRewriteDatabaseMap::class, [ 'connection' => $this->connectionMock, 'dataMapPool' => $this->dataMapPoolMock, - 'temporaryTableService' => $this->temporaryTableServiceMock, - 'mapData' => [], + 'temporaryTableService' => $this->temporaryTableServiceMock ] ); } @@ -69,11 +68,6 @@ public function testGetAllData() '5' => ['store_id' => 2, 'product_id' => 2], ]; - $productStoreIdsExpectedMap = [ - '1' => [1, 2, 3], - '2' => [1, 2], - ]; - $connectionMock = $this->getMock(AdapterInterface::class); $selectMock = $this->getMock(Select::class, [], [], '', false); @@ -85,7 +79,7 @@ public function testGetAllData() ->willReturn($selectMock); $connectionMock->expects($this->any()) ->method('fetchAll') - ->willReturnOnConsecutiveCalls($productStoreIds, $productStoreIdsExpectedMap); + ->willReturn($productStoreIds[3]); $selectMock->expects($this->any()) ->method('from') ->willReturnSelf(); @@ -113,7 +107,6 @@ public function testGetAllData() ) ->willReturn('tempTableName'); - $this->assertEquals($productStoreIds, $this->model->getAllData(1)); - $this->assertEquals($productStoreIdsExpectedMap, $this->model->getData(1, '3_1')); + $this->assertEquals($productStoreIds[3], $this->model->getData(1, '3_1')); } } diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataMapPoolTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/HashMapPoolTest.php similarity index 63% rename from app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataMapPoolTest.php rename to app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/HashMapPoolTest.php index 0bfe13a83677d..213e56595e968 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataMapPoolTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/HashMapPoolTest.php @@ -6,20 +6,20 @@ namespace Magento\CatalogUrlRewrite\Test\Unit\Model\Map; use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; -use Magento\CatalogUrlRewrite\Model\Map\DataMapPool; -use Magento\CatalogUrlRewrite\Model\Map\DataCategoryMap; -use Magento\CatalogUrlRewrite\Model\Map\DataProductMap; +use Magento\CatalogUrlRewrite\Model\Map\HashMapPool; +use Magento\CatalogUrlRewrite\Model\Map\DataCategoryHashMap; +use Magento\CatalogUrlRewrite\Model\Map\DataProductHashMap; use Magento\Framework\ObjectManagerInterface; /** - * Class DataMapPoolTest + * Class HashMapPoolTest */ -class DataMapPoolTest extends \PHPUnit_Framework_TestCase +class HashMapPoolTest extends \PHPUnit_Framework_TestCase { /** @var ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject */ private $objectManagerMock; - /** @var DataMapPool|\PHPUnit_Framework_MockObject_MockObject */ + /** @var HashMapPool|\PHPUnit_Framework_MockObject_MockObject */ private $model; protected function setUp() @@ -27,7 +27,7 @@ protected function setUp() $this->objectManagerMock = $this->getMock(ObjectManagerInterface::class); $this->model = (new ObjectManager($this))->getObject( - DataMapPool::class, + HashMapPool::class, [ 'objectManager' => $this->objectManagerMock, ] @@ -39,16 +39,16 @@ protected function setUp() */ public function testGetDataMap() { - $dataCategoryMapMock = $this->getMock(DataCategoryMap::class, [], [], '', false); - $dataProductMapMock = $this->getMock(DataProductMap::class, [], [], '', false); - $dataProductMapMockOtherCategory = $this->getMock(DataProductMap::class, [], [], '', false); + $dataCategoryMapMock = $this->getMock(DataCategoryHashMap::class, [], [], '', false); + $dataProductMapMock = $this->getMock(DataProductHashMap::class, [], [], '', false); + $dataProductMapMockOtherCategory = $this->getMock(DataProductHashMap::class, [], [], '', false); $this->objectManagerMock->expects($this->any()) ->method('create') ->willReturnOnConsecutiveCalls($dataCategoryMapMock, $dataProductMapMock, $dataProductMapMockOtherCategory); - $this->assertEquals($dataCategoryMapMock, $this->model->getDataMap(DataCategoryMap::class, 1)); - $this->assertEquals($dataCategoryMapMock, $this->model->getDataMap(DataCategoryMap::class, 1)); - $this->assertEquals($dataProductMapMock, $this->model->getDataMap(DataProductMap::class, 1)); - $this->assertEquals($dataProductMapMockOtherCategory, $this->model->getDataMap(DataCategoryMap::class, 2)); + $this->assertEquals($dataCategoryMapMock, $this->model->getDataMap(DataCategoryHashMap::class, 1)); + $this->assertEquals($dataCategoryMapMock, $this->model->getDataMap(DataCategoryHashMap::class, 1)); + $this->assertEquals($dataProductMapMock, $this->model->getDataMap(DataProductHashMap::class, 1)); + $this->assertEquals($dataProductMapMockOtherCategory, $this->model->getDataMap(DataCategoryHashMap::class, 2)); } } diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/UrlRewriteMapTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/UrlRewriteFinderTest.php similarity index 68% rename from app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/UrlRewriteMapTest.php rename to app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/UrlRewriteFinderTest.php index c6ea0f0ce444c..6b79d5e2f8a99 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/UrlRewriteMapTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/UrlRewriteFinderTest.php @@ -6,20 +6,20 @@ namespace Magento\CatalogUrlRewrite\Test\Unit\Model\Map; use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; -use Magento\CatalogUrlRewrite\Model\Map\UrlRewriteMap; -use Magento\CatalogUrlRewrite\Model\Map\DataMapPoolInterface; +use Magento\CatalogUrlRewrite\Model\Map\UrlRewriteFinder; +use Magento\CatalogUrlRewrite\Model\Map\DatabaseMapPool; use Magento\UrlRewrite\Model\UrlFinderInterface; use Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory; use Magento\UrlRewrite\Service\V1\Data\UrlRewrite; -use Magento\CatalogUrlRewrite\Model\Map\DataProductUrlRewriteMap; -use Magento\CatalogUrlRewrite\Model\Map\DataCategoryUrlRewriteMap; +use Magento\CatalogUrlRewrite\Model\Map\DataCategoryUrlRewriteDatabaseMap; +use Magento\CatalogUrlRewrite\Model\Map\DataProductUrlRewriteDatabaseMap; /** - * Class DataProductUrlRewriteMapTest + * Class UrlRewriteFinderTest */ -class UrlRewriteMapTest extends \PHPUnit_Framework_TestCase +class UrlRewriteFinderTest extends \PHPUnit_Framework_TestCase { - /** @var DataMapPoolInterface|\PHPUnit_Framework_MockObject_MockObject */ + /** @var DatabaseMapPool|\PHPUnit_Framework_MockObject_MockObject */ private $dataMapPoolMock; /** @var UrlRewriteFactory|\PHPUnit_Framework_MockObject_MockObject */ @@ -31,12 +31,12 @@ class UrlRewriteMapTest extends \PHPUnit_Framework_TestCase /** @var UrlFinderInterface|\PHPUnit_Framework_MockObject_MockObject */ private $urlFinderMock; - /** @var UrlRewriteMap|\PHPUnit_Framework_MockObject_MockObject */ + /** @var UrlRewriteFinder|\PHPUnit_Framework_MockObject_MockObject */ private $model; protected function setUp() { - $this->dataMapPoolMock = $this->getMock(DataMapPoolInterface::class); + $this->dataMapPoolMock = $this->getMock(DatabaseMapPool::class, [], [], '', false); $this->urlFinderMock = $this->getMock(UrlFinderInterface::class); $this->urlRewriteFactoryMock = $this->getMock(UrlRewriteFactory::class, ['create'], [], '', false); $this->urlRewritePrototypeMock = new UrlRewrite(); @@ -46,9 +46,9 @@ protected function setUp() ->willReturn($this->urlRewritePrototypeMock); $this->model = (new ObjectManager($this))->getObject( - UrlRewriteMap::class, + UrlRewriteFinder::class, [ - 'dataMapPool' => $this->dataMapPoolMock, + 'databaseMapPool' => $this->dataMapPoolMock, 'urlFinder' => $this->urlFinderMock, 'urlRewriteFactory' => $this->urlRewriteFactoryMock ] @@ -68,13 +68,13 @@ public function testGetByIdentifiersFallback() ->method('findAllByData') ->willReturn($expected); - $this->assertEquals($expected, $this->model->getByIdentifiers(1, 1, UrlRewriteMap::ENTITY_TYPE_CATEGORY)); - $this->assertEquals($expected, $this->model->getByIdentifiers(1, 1, UrlRewriteMap::ENTITY_TYPE_PRODUCT)); - $this->assertEquals($expected, $this->model->getByIdentifiers('a', 1, UrlRewriteMap::ENTITY_TYPE_PRODUCT), 1); - $this->assertEquals($expected, $this->model->getByIdentifiers('a', 'a', UrlRewriteMap::ENTITY_TYPE_PRODUCT), 1); - $this->assertEquals($expected, $this->model->getByIdentifiers(1, 'a', UrlRewriteMap::ENTITY_TYPE_PRODUCT), 1); - $this->assertEquals($expected, $this->model->getByIdentifiers(1, 1, 'cms', 1)); - $this->assertEquals($expected, $this->model->getByIdentifiers(1, 1, 'cms')); + $this->assertEquals($expected, $this->model->findAllByData(1, 1, UrlRewriteFinder::ENTITY_TYPE_CATEGORY)); + $this->assertEquals($expected, $this->model->findAllByData(1, 1, UrlRewriteFinder::ENTITY_TYPE_PRODUCT)); + $this->assertEquals($expected, $this->model->findAllByData('a', 1, UrlRewriteFinder::ENTITY_TYPE_PRODUCT), 1); + $this->assertEquals($expected, $this->model->findAllByData('a', 'a', UrlRewriteFinder::ENTITY_TYPE_PRODUCT), 1); + $this->assertEquals($expected, $this->model->findAllByData(1, 'a', UrlRewriteFinder::ENTITY_TYPE_PRODUCT), 1); + $this->assertEquals($expected, $this->model->findAllByData(1, 1, 'cms', 1)); + $this->assertEquals($expected, $this->model->findAllByData(1, 1, 'cms')); } /** @@ -97,10 +97,10 @@ public function testGetByIdentifiersProduct() ] ]; - $dataProductMapMock = $this->getMock(DataProductUrlRewriteMap::class, [], [], '', false); + $dataProductMapMock = $this->getMock(DataProductUrlRewriteDatabaseMap::class, [], [], '', false); $this->dataMapPoolMock->expects($this->once()) ->method('getDataMap') - ->with(DataProductUrlRewriteMap::class, 1) + ->with(DataProductUrlRewriteDatabaseMap::class, 1) ->willReturn($dataProductMapMock); $this->urlFinderMock->expects($this->never()) @@ -111,7 +111,7 @@ public function testGetByIdentifiersProduct() ->method('getData') ->willReturn($data); - $urlRewriteResultArray = $this->model->getByIdentifiers(1, 1, UrlRewriteMap::ENTITY_TYPE_PRODUCT, 1); + $urlRewriteResultArray = $this->model->findAllByData(1, 1, UrlRewriteFinder::ENTITY_TYPE_PRODUCT, 1); $this->assertEquals($data[0], $urlRewriteResultArray[0]->toArray()); } @@ -135,10 +135,10 @@ public function testGetByIdentifiersCategory() ] ]; - $dataCategoryMapMock = $this->getMock(DataCategoryUrlRewriteMap::class, [], [], '', false); + $dataCategoryMapMock = $this->getMock(DataCategoryUrlRewriteDatabaseMap::class, [], [], '', false); $this->dataMapPoolMock->expects($this->once()) ->method('getDataMap') - ->with(DataCategoryUrlRewriteMap::class, 1) + ->with(DataCategoryUrlRewriteDatabaseMap::class, 1) ->willReturn($dataCategoryMapMock); $this->urlFinderMock->expects($this->never()) @@ -149,7 +149,7 @@ public function testGetByIdentifiersCategory() ->method('getData') ->willReturn($data); - $urlRewriteResultArray = $this->model->getByIdentifiers(1, 1, UrlRewriteMap::ENTITY_TYPE_CATEGORY, 1); + $urlRewriteResultArray = $this->model->findAllByData(1, 1, UrlRewriteFinder::ENTITY_TYPE_CATEGORY, 1); $this->assertEquals($data[0], $urlRewriteResultArray[0]->toArray()); } } diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Product/CurrentUrlRewritesRegeneratorTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Product/CurrentUrlRewritesRegeneratorTest.php index f093145cea5fd..61fe9643f0ed7 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Product/CurrentUrlRewritesRegeneratorTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Product/CurrentUrlRewritesRegeneratorTest.php @@ -55,7 +55,7 @@ protected function setUp() ->disableOriginalConstructor()->getMock(); $this->objectRegistry = $this->getMockBuilder(\Magento\CatalogUrlRewrite\Model\ObjectRegistry::class) ->disableOriginalConstructor()->getMock(); - $this->urlRewriteMap = $this->getMockBuilder(\Magento\CatalogUrlRewrite\Model\Map\UrlRewriteMap::class) + $this->urlRewriteMap = $this->getMockBuilder(\Magento\CatalogUrlRewrite\Model\Map\UrlRewriteFinder::class) ->disableOriginalConstructor()->getMock(); $this->urlRewriteFactory->expects($this->once())->method('create') ->willReturn($this->urlRewrite); diff --git a/app/code/Magento/CatalogUrlRewrite/etc/di.xml b/app/code/Magento/CatalogUrlRewrite/etc/di.xml index 877638026a58a..9ea21bce36567 100644 --- a/app/code/Magento/CatalogUrlRewrite/etc/di.xml +++ b/app/code/Magento/CatalogUrlRewrite/etc/di.xml @@ -6,7 +6,6 @@ */ --> - Magento\CatalogUrlRewrite\Model\Storage\DbStorage diff --git a/app/code/Magento/UrlRewrite/Model/UrlRewritesSet.php b/app/code/Magento/UrlRewrite/Model/UrlRewritesSet.php index 184eb9486a235..b395a922d6511 100644 --- a/app/code/Magento/UrlRewrite/Model/UrlRewritesSet.php +++ b/app/code/Magento/UrlRewrite/Model/UrlRewritesSet.php @@ -14,6 +14,7 @@ */ class UrlRewritesSet { + const SEPARATOR = '_'; /** * @var $rewritesArray[] */ @@ -27,10 +28,9 @@ class UrlRewritesSet */ public function merge(array $urlRewritesArray) { - $separator = '_'; foreach ($urlRewritesArray as $urlRewrite) { - $key = $urlRewrite->getRequestPath() . $separator . $urlRewrite->getStoreId(); - if ($key !== $separator) { + $key = $urlRewrite->getRequestPath() . self::SEPARATOR . $urlRewrite->getStoreId(); + if ($key !== self::SEPARATOR) { $this->data[$key] = $urlRewrite; } else { $this->data[] = $urlRewrite; diff --git a/app/code/Magento/UrlRewrite/Test/Unit/Model/UrlRewritesSetTest.php b/app/code/Magento/UrlRewrite/Test/Unit/Model/UrlRewritesSetTest.php index ce5b944cb3b5f..aaabb1499b7f1 100644 --- a/app/code/Magento/UrlRewrite/Test/Unit/Model/UrlRewritesSetTest.php +++ b/app/code/Magento/UrlRewrite/Test/Unit/Model/UrlRewritesSetTest.php @@ -55,20 +55,6 @@ public function testGetDataWhenEmpty() $this->assertEmpty($this->urlRewritesSet->getData()); } - /** - * Run test getData method when data is not empty - * - * @return void - */ - public function testGetDataWhenNotEmpty() - { - $data = new \ReflectionProperty($this->urlRewritesSet, 'data'); - $data->setAccessible(true); - $data->setValue($this->urlRewritesSet, [new UrlRewrite()]); - $data->setAccessible(false); - $this->assertNotEmpty($this->urlRewritesSet->getData()); - } - /** * Data provider for testMerge * diff --git a/lib/internal/Magento/Framework/DB/TemporaryTableService.php b/lib/internal/Magento/Framework/DB/TemporaryTableService.php index 19ad906b84c42..36223d5b06021 100644 --- a/lib/internal/Magento/Framework/DB/TemporaryTableService.php +++ b/lib/internal/Magento/Framework/DB/TemporaryTableService.php @@ -24,7 +24,7 @@ class TemporaryTableService /** * @var AdapterInterface[] */ - private $createdTables = []; + private $createdTableAdapters = []; /** * @param \Magento\Framework\Math\Random $random @@ -46,9 +46,10 @@ public function __construct(\Magento\Framework\Math\Random $random) * [ * 'PRIMARY' => ['primary_id'], * 'some_single_field_index' => ['field'], - * 'some_multiple_field_index' => ['field1', 'field2'], + * 'UNQ_some_multiple_field_index' => ['field1', 'field2'], * ] * ) + * Note that indexes names with UNQ_ prefix, will be created as unique * * @param Select $select * @param AdapterInterface $adapter @@ -70,7 +71,7 @@ public function createFromSelect( foreach ($indexes as $indexName => $columns) { $renderedColumns = implode(',', array_map([$adapter, 'quoteIdentifier'], $columns)); - $indexType = sprintf('INDEX %s USING %s', $adapter->quoteIdentifier($indexName), $indexMethod); + $indexType = sprintf('INDEX %s USING %s', $adapter->quoteIdentifier($indexName), "{$indexMethod}"); if ($indexName === 'PRIMARY') { $indexType = 'PRIMARY KEY'; @@ -85,8 +86,8 @@ public function createFromSelect( 'CREATE TEMPORARY TABLE %s %s ENGINE=%s IGNORE (%s)', $adapter->quoteIdentifier($name), $indexStatements ? '(' . implode(',', $indexStatements) . ')' : '', - $engine, - (string)$select + "{$engine}", + "{$select}" ); $adapter->query( @@ -94,28 +95,30 @@ public function createFromSelect( $select->getBind() ); - $this->createdTables[$name] = $adapter; + $this->createdTableAdapters[$name] = $adapter; return $name; } /** * Method used to drop a table by name - * This class will hold all temporary table names in createdTables array so we can dispose them once we're finished + * This class will hold all temporary table names in createdTableAdapters array + * so we can dispose them once we're finished * - * Example: dropTable($previouslySavedTableName) - * where $previouslySavedTableName is a variable that you have to save when you use "createFromSelect" method + * Example: dropTable($name) + * where $name is a variable that holds the name for a previously created temporary table + * by using "createFromSelect" method * * @param string $name * @return bool */ public function dropTable($name) { - if (!empty($this->createdTables)) { - if (isset($this->createdTables[$name]) && !empty($name)) { - $adapter = $this->createdTables[$name]; + if (!empty($this->createdTableAdapters)) { + if (isset($this->createdTableAdapters[$name]) && !empty($name)) { + $adapter = $this->createdTableAdapters[$name]; $adapter->dropTemporaryTable($name); - unset($this->createdTables[$name]); + unset($this->createdTableAdapters[$name]); return true; } } From ebccc5b2b72c16e65b20626357350a62c3d35581 Mon Sep 17 00:00:00 2001 From: Cristian Partica Date: Mon, 9 Jan 2017 10:16:37 -0600 Subject: [PATCH 28/39] MAGETWO-58924: SQL error wait timeout error when saving categories - fix issues with code review, add phpdocs for renaming and fixing unit tests --- .../Category/ChildrenUrlRewriteGenerator.php | 22 +++++------ .../CurrentUrlRewritesRegenerator.php | 38 +++++++++--------- .../Model/CategoryUrlRewriteGenerator.php | 39 ++++++++++--------- .../Model/Map/DataCategoryHashMap.php | 2 +- .../Map/DataCategoryUrlRewriteDatabaseMap.php | 24 ++++++------ .../Map/DataCategoryUsedInProductsHashMap.php | 2 +- .../Model/Map/DataProductHashMap.php | 2 +- .../Map/DataProductUrlRewriteDatabaseMap.php | 20 +++++----- .../Model/Map/DatabaseMapInterface.php | 2 +- .../Model/Map/DatabaseMapPool.php | 2 +- .../Model/Map/HashMapInterface.php | 2 +- .../Model/Map/HashMapPool.php | 4 +- .../Model/Map/UrlRewriteFinder.php | 8 ++-- .../Product/CurrentUrlRewritesRegenerator.php | 32 +++++++-------- .../Model/ProductScopeRewriteGenerator.php | 34 ++++++++-------- .../Model/ProductUrlRewriteGenerator.php | 8 ++-- .../Observer/AfterImportDataObserver.php | 26 ++++++------- ...ategoryProcessUrlRewriteSavingObserver.php | 6 +-- .../Observer/UrlRewriteHandler.php | 32 +++++++-------- .../ChildrenUrlRewriteGeneratorTest.php | 12 +++--- .../CurrentUrlRewritesRegeneratorTest.php | 30 +++++++------- .../Model/CategoryUrlRewriteGeneratorTest.php | 12 +++--- .../Model/Map/DataCategoryHashMapTest.php | 2 +- .../DataCategoryUrlRewriteDatabaseMapTest.php | 10 ++--- .../DataCategoryUsedInProductsHashMapTest.php | 18 ++++----- .../Unit/Model/Map/DataProductHashMapTest.php | 14 +++---- .../DataProductUrlRewriteDatabaseMapTest.php | 10 ++--- .../Test/Unit/Model/Map/HashMapPoolTest.php | 2 +- .../Unit/Model/Map/UrlRewriteFinderTest.php | 20 +++++----- .../CurrentUrlRewritesRegeneratorTest.php | 32 +++++++-------- .../ProductScopeRewriteGeneratorTest.php | 12 +++--- .../Observer/AfterImportDataObserverTest.php | 16 ++++---- ...lRewritesSet.php => MergeDataProvider.php} | 5 ++- ...sSetTest.php => MergeDataProviderTest.php} | 10 ++--- ...oryProcessUrlRewriteSavingObserverTest.php | 2 +- .../Framework/DB/TemporaryTableService.php | 2 +- .../Test/Unit/TemporaryTableServiceTest.php | 10 ++--- 37 files changed, 264 insertions(+), 260 deletions(-) rename app/code/Magento/UrlRewrite/Model/{UrlRewritesSet.php => MergeDataProvider.php} (93%) rename app/code/Magento/UrlRewrite/Test/Unit/Model/{UrlRewritesSetTest.php => MergeDataProviderTest.php} (92%) diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Category/ChildrenUrlRewriteGenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/Category/ChildrenUrlRewriteGenerator.php index 9db93af19af4a..920da739c8eb8 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Category/ChildrenUrlRewriteGenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Category/ChildrenUrlRewriteGenerator.php @@ -8,7 +8,7 @@ use Magento\Catalog\Model\Category; use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGeneratorFactory; use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator; -use Magento\UrlRewrite\Model\UrlRewritesSetFactory; +use Magento\UrlRewrite\Model\MergeDataProviderFactory; use Magento\Framework\App\ObjectManager; class ChildrenUrlRewriteGenerator @@ -19,24 +19,24 @@ class ChildrenUrlRewriteGenerator /** @var \Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGeneratorFactory */ protected $categoryUrlRewriteGeneratorFactory; - /** @var \Magento\UrlRewrite\Model\UrlRewritesSet */ - private $urlRewritesSetPrototype; + /** @var \Magento\UrlRewrite\Model\MergeDataProvider */ + private $mergeDataProviderPrototype; /** * @param \Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider $childrenCategoriesProvider * @param \Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGeneratorFactory $categoryUrlRewriteGeneratorFactory - * @param \Magento\UrlRewrite\Model\UrlRewritesSetFactory|null $urlRewritesSetFactory + * @param \Magento\UrlRewrite\Model\MergeDataProviderFactory|null $mergeDataProviderFactory */ public function __construct( ChildrenCategoriesProvider $childrenCategoriesProvider, CategoryUrlRewriteGeneratorFactory $categoryUrlRewriteGeneratorFactory, - UrlRewritesSetFactory $urlRewritesSetFactory = null + MergeDataProviderFactory $mergeDataProviderFactory = null ) { $this->childrenCategoriesProvider = $childrenCategoriesProvider; $this->categoryUrlRewriteGeneratorFactory = $categoryUrlRewriteGeneratorFactory; - $urlRewritesSetFactory = $urlRewritesSetFactory ?: ObjectManager::getInstance() - ->get(UrlRewritesSetFactory::class); - $this->urlRewritesSetPrototype = $urlRewritesSetFactory->create(); + $mergeDataProviderFactory = $mergeDataProviderFactory ?: ObjectManager::getInstance() + ->get(MergeDataProviderFactory::class); + $this->mergeDataProviderPrototype = $mergeDataProviderFactory->create(); } /** @@ -49,17 +49,17 @@ public function __construct( */ public function generate($storeId, Category $category, $rootCategoryId = null) { - $urlRewritesSet = clone $this->urlRewritesSetPrototype; + $mergeDataProvider = clone $this->mergeDataProviderPrototype; foreach ($this->childrenCategoriesProvider->getChildren($category, true) as $childCategory) { $childCategory->setStoreId($storeId); $childCategory->setData('save_rewrites_history', $category->getData('save_rewrites_history')); /** @var CategoryUrlRewriteGenerator $categoryUrlRewriteGenerator */ $categoryUrlRewriteGenerator = $this->categoryUrlRewriteGeneratorFactory->create(); - $urlRewritesSet->merge( + $mergeDataProvider->merge( $categoryUrlRewriteGenerator->generate($childCategory, false, $rootCategoryId) ); } - return $urlRewritesSet->getData(); + return $mergeDataProvider->getData(); } } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php index 5f3d543771567..eb3fd3954bd6e 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php @@ -32,34 +32,34 @@ class CurrentUrlRewritesRegenerator protected $urlFinder; /** @var \Magento\CatalogUrlRewrite\Model\Map\UrlRewriteFinder */ - private $urlRewriteMap; + private $urlRewriteFinder; - /** @var \Magento\UrlRewrite\Model\UrlRewritesSet */ - private $urlRewritesSetPrototype; + /** @var \Magento\UrlRewrite\Model\MergeDataProvider */ + private $mergeDataProviderPrototype; /** * @param \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator * @param \Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory $urlRewriteFactory * @param \Magento\UrlRewrite\Model\UrlFinderInterface $urlFinder - * @param \Magento\CatalogUrlRewrite\Model\Map\UrlRewriteFinder|null $urlRewriteMap - * @param \Magento\UrlRewrite\Model\UrlRewritesSetFactory|null $urlRewritesSetFactory + * @param \Magento\CatalogUrlRewrite\Model\Map\UrlRewriteFinder|null $urlRewriteFinder + * @param \Magento\UrlRewrite\Model\MergeDataProviderFactory|null $mergeDataProviderFactory */ public function __construct( \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator, \Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory $urlRewriteFactory, \Magento\UrlRewrite\Model\UrlFinderInterface $urlFinder, - \Magento\CatalogUrlRewrite\Model\Map\UrlRewriteFinder $urlRewriteMap = null, - \Magento\UrlRewrite\Model\UrlRewritesSetFactory $urlRewritesSetFactory = null + \Magento\CatalogUrlRewrite\Model\Map\UrlRewriteFinder $urlRewriteFinder = null, + \Magento\UrlRewrite\Model\MergeDataProviderFactory $mergeDataProviderFactory = null ) { $this->categoryUrlPathGenerator = $categoryUrlPathGenerator; $this->urlRewriteFactory = $urlRewriteFactory; $this->urlRewritePrototype = $urlRewriteFactory->create(); $this->urlFinder = $urlFinder; - $this->urlRewriteMap = $urlRewriteMap ?: \Magento\Framework\App\ObjectManager::getInstance() + $this->urlRewriteFinder = $urlRewriteFinder ?: \Magento\Framework\App\ObjectManager::getInstance() ->get(\Magento\CatalogUrlRewrite\Model\Map\UrlRewriteFinder::class); - $urlRewritesSetFactory = $urlRewritesSetFactory ?: \Magento\Framework\App\ObjectManager::getInstance() - ->get(\Magento\UrlRewrite\Model\UrlRewritesSetFactory::class); - $this->urlRewritesSetPrototype = $urlRewritesSetFactory->create(); + $mergeDataProviderFactory = $mergeDataProviderFactory ?: \Magento\Framework\App\ObjectManager::getInstance() + ->get(\Magento\UrlRewrite\Model\MergeDataProviderFactory::class); + $this->mergeDataProviderPrototype = $mergeDataProviderFactory->create(); } /** @@ -72,8 +72,8 @@ public function __construct( */ public function generate($storeId, \Magento\Catalog\Model\Category $category, $rootCategoryId = null) { - $urlRewritesSet = clone $this->urlRewritesSetPrototype; - $currentUrlRewrites = $this->urlRewriteMap->findAllByData( + $mergeDataProvider = clone $this->mergeDataProviderPrototype; + $currentUrlRewrites = $this->urlRewriteFinder->findAllByData( $category->getEntityId(), $storeId, CategoryUrlRewriteGenerator::ENTITY_TYPE, @@ -81,23 +81,23 @@ public function generate($storeId, \Magento\Catalog\Model\Category $category, $r ); foreach ($currentUrlRewrites as $rewrite) { - $urlRewritesSet->merge( + $mergeDataProvider->merge( $rewrite->getIsAutogenerated() ? $this->generateForAutogenerated($rewrite, $storeId, $category) : $this->generateForCustom($rewrite, $storeId, $category) ); } - return $urlRewritesSet->getData(); + return $mergeDataProvider->getData(); } /** * @param \Magento\UrlRewrite\Service\V1\Data\UrlRewrite $url * @param int $storeId - * @param \Magento\Catalog\Model\Category $category + * @param \Magento\Catalog\Model\Category|null $category * @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] */ - protected function generateForAutogenerated($url, $storeId, \Magento\Catalog\Model\Category $category) + protected function generateForAutogenerated($url, $storeId, \Magento\Catalog\Model\Category $category = null) { if ($category->getData('save_rewrites_history')) { $targetPath = $this->categoryUrlPathGenerator->getUrlPathWithSuffix($category, $storeId); @@ -119,10 +119,10 @@ protected function generateForAutogenerated($url, $storeId, \Magento\Catalog\Mod /** * @param \Magento\UrlRewrite\Service\V1\Data\UrlRewrite $url * @param int $storeId - * @param \Magento\Catalog\Model\Category $category + * @param \Magento\Catalog\Model\Category|null $category * @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] */ - protected function generateForCustom($url, $storeId, \Magento\Catalog\Model\Category $category) + protected function generateForCustom($url, $storeId, \Magento\Catalog\Model\Category $category = null) { $targetPath = !$url->getRedirectType() ? $url->getTargetPath() diff --git a/app/code/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGenerator.php index 5eb72ff560b1d..f95c72515c46a 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGenerator.php @@ -13,7 +13,7 @@ use Magento\Store\Model\Store; use Magento\Catalog\Api\CategoryRepositoryInterface; use Magento\Framework\App\ObjectManager; -use Magento\UrlRewrite\Model\UrlRewritesSetFactory; +use Magento\UrlRewrite\Model\MergeDataProviderFactory; class CategoryUrlRewriteGenerator { @@ -38,8 +38,8 @@ class CategoryUrlRewriteGenerator /** @var \Magento\CatalogUrlRewrite\Model\Category\ChildrenUrlRewriteGenerator */ protected $childrenUrlRewriteGenerator; - /** @var \Magento\UrlRewrite\Model\UrlRewritesSet */ - private $urlRewritesSetPrototype; + /** @var \Magento\UrlRewrite\Model\MergeDataProvider */ + private $mergeDataProviderPrototype; /** * @var bool @@ -52,7 +52,7 @@ class CategoryUrlRewriteGenerator * @param \Magento\CatalogUrlRewrite\Model\Category\ChildrenUrlRewriteGenerator $childrenUrlRewriteGenerator * @param \Magento\CatalogUrlRewrite\Service\V1\StoreViewService $storeViewService * @param \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository - * @param \Magento\UrlRewrite\Model\UrlRewritesSetFactory|null $urlRewritesSetFactory + * @param \Magento\UrlRewrite\Model\MergeDataProviderFactory|null $mergeDataProviderFactory */ public function __construct( CanonicalUrlRewriteGenerator $canonicalUrlRewriteGenerator, @@ -60,16 +60,16 @@ public function __construct( ChildrenUrlRewriteGenerator $childrenUrlRewriteGenerator, StoreViewService $storeViewService, CategoryRepositoryInterface $categoryRepository, - UrlRewritesSetFactory $urlRewritesSetFactory = null + MergeDataProviderFactory $mergeDataProviderFactory = null ) { $this->storeViewService = $storeViewService; $this->canonicalUrlRewriteGenerator = $canonicalUrlRewriteGenerator; $this->childrenUrlRewriteGenerator = $childrenUrlRewriteGenerator; $this->currentUrlRewritesRegenerator = $currentUrlRewritesRegenerator; $this->categoryRepository = $categoryRepository; - $urlRewritesSetFactory = $urlRewritesSetFactory ?: ObjectManager::getInstance() - ->get(UrlRewritesSetFactory::class); - $this->urlRewritesSetPrototype = $urlRewritesSetFactory->create(); + $mergeDataProviderFactory = $mergeDataProviderFactory ?: ObjectManager::getInstance() + ->get(MergeDataProviderFactory::class); + $this->mergeDataProviderPrototype = $mergeDataProviderFactory->create(); } /** @@ -100,19 +100,22 @@ public function generate(Category $category, $overrideStoreUrls = false, $rootCa * @param int|null $rootCategoryId * @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] */ - protected function generateForGlobalScope(Category $category = null, $overrideStoreUrls = false, $rootCategoryId = null) - { - $urlRewritesSet = clone $this->urlRewritesSetPrototype; + protected function generateForGlobalScope( + Category $category = null, + $overrideStoreUrls = false, + $rootCategoryId = null + ) { + $mergeDataProvider = clone $this->mergeDataProviderPrototype; $categoryId = $category->getId(); foreach ($category->getStoreIds() as $storeId) { if (!$this->isGlobalScope($storeId) && $this->isOverrideUrlsForStore($storeId, $categoryId, $overrideStoreUrls) ) { $this->updateCategoryUrlForStore($storeId, $category); - $urlRewritesSet->merge($this->generateForSpecificStoreView($storeId, $category, $rootCategoryId)); + $mergeDataProvider->merge($this->generateForSpecificStoreView($storeId, $category, $rootCategoryId)); } } - $result = $urlRewritesSet->getData(); + $result = $mergeDataProvider->getData(); return $result; } @@ -170,16 +173,16 @@ protected function isGlobalScope($storeId) */ protected function generateForSpecificStoreView($storeId, Category $category = null, $rootCategoryId = null) { - $urlRewritesSet = clone $this->urlRewritesSetPrototype; - $urlRewritesSet->merge( + $mergeDataProvider = clone $this->mergeDataProviderPrototype; + $mergeDataProvider->merge( $this->canonicalUrlRewriteGenerator->generate($storeId, $category) ); - $urlRewritesSet->merge( + $mergeDataProvider->merge( $this->childrenUrlRewriteGenerator->generate($storeId, $category, $rootCategoryId) ); - $urlRewritesSet->merge( + $mergeDataProvider->merge( $this->currentUrlRewritesRegenerator->generate($storeId, $category, $rootCategoryId) ); - return $urlRewritesSet->getData(); + return $mergeDataProvider->getData(); } } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryHashMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryHashMap.php index 88c4ea813264d..acb8c4a897113 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryHashMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryHashMap.php @@ -1,6 +1,6 @@ connection = $connection; - $this->dataMapPool = $dataMapPool; + $this->hashMapPool = $hashMapPool; $this->temporaryTableService = $temporaryTableService; } /** * Generates data from categoryId and stores it into a temporary table * - * @param $categoryId + * @param int $categoryId * @return void */ private function generateTableAdapter($categoryId) @@ -70,7 +70,7 @@ private function generateData($categoryId) ->from( ['e' => $this->connection->getTableName('url_rewrite')], ['e.*', 'hash_key' => new \Zend_Db_Expr( - "CONCAT(e.store_id,'" . UrlRewritesSet::SEPARATOR . "', e.entity_id)" + "CONCAT(e.store_id,'" . MergeDataProvider::SEPARATOR . "', e.entity_id)" ) ] ) @@ -80,9 +80,9 @@ private function generateData($categoryId) 'entity_id', [ 'in' => array_merge( - $this->dataMapPool->getDataMap(DataCategoryUsedInProductsHashMap::class, $categoryId) + $this->hashMapPool->getDataMap(DataCategoryUsedInProductsHashMap::class, $categoryId) ->getAllData($categoryId), - $this->dataMapPool->getDataMap(DataCategoryHashMap::class, $categoryId) + $this->hashMapPool->getDataMap(DataCategoryHashMap::class, $categoryId) ->getAllData($categoryId) ) ] @@ -105,8 +105,8 @@ private function generateData($categoryId) */ public function destroyTableAdapter($categoryId) { - $this->dataMapPool->resetMap(DataCategoryUsedInProductsHashMap::class, $categoryId); - $this->dataMapPool->resetMap(DataCategoryHashMap::class, $categoryId); + $this->hashMapPool->resetMap(DataCategoryUsedInProductsHashMap::class, $categoryId); + $this->hashMapPool->resetMap(DataCategoryHashMap::class, $categoryId); if (isset($this->createdTableAdapters[$categoryId])) { $this->temporaryTableService->dropTable($this->createdTableAdapters[$categoryId]); unset($this->createdTableAdapters[$categoryId]); diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUsedInProductsHashMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUsedInProductsHashMap.php index 9f142ef565249..0a3a99f8a0e7c 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUsedInProductsHashMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUsedInProductsHashMap.php @@ -1,6 +1,6 @@ connection = $connection; - $this->dataMapPool = $dataMapPool; + $this->hashMapPool = $hashMapPool; $this->temporaryTableService = $temporaryTableService; } /** * Generates data from categoryId and stores it into a temporary table * - * @param $categoryId + * @param int $categoryId * @return void */ private function generateTableAdapter($categoryId) @@ -84,7 +84,7 @@ private function generateData($categoryId) ->from( ['e' => $this->connection->getTableName('url_rewrite')], ['e.*', 'hash_key' => new \Zend_Db_Expr( - "CONCAT(e.store_id,'" . UrlRewritesSet::SEPARATOR . "', e.entity_id)" + "CONCAT(e.store_id,'" . MergeDataProvider::SEPARATOR . "', e.entity_id)" ) ] ) @@ -93,7 +93,7 @@ private function generateData($categoryId) $urlRewritesConnection->prepareSqlCondition( 'entity_id', [ - 'in' => $this->dataMapPool->getDataMap(DataProductHashMap::class, $categoryId) + 'in' => $this->hashMapPool->getDataMap(DataProductHashMap::class, $categoryId) ->getAllData($categoryId) ] ) @@ -115,7 +115,7 @@ private function generateData($categoryId) */ public function destroyTableAdapter($categoryId) { - $this->dataMapPool->resetMap(DataProductHashMap::class, $categoryId); + $this->hashMapPool->resetMap(DataProductHashMap::class, $categoryId); if (isset($this->createdTableAdapters[$categoryId])) { $this->temporaryTableService->dropTable($this->createdTableAdapters[$categoryId]); unset($this->createdTableAdapters[$categoryId]); diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DatabaseMapInterface.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DatabaseMapInterface.php index 6090baf3d9011..32f81e246156f 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DatabaseMapInterface.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DatabaseMapInterface.php @@ -1,6 +1,6 @@ implementsInterface(HashMapInterface::class)) { - throw new \Exception($instanceName . ' does not implement interface ' . HashMapInterface::class); + throw new \Exception($instanceName . ' does not implement interface ' . HashMapInterface::class); } if (!isset($this->dataArray[$key])) { $this->dataArray[$key] = $this->objectManager->create( diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/UrlRewriteFinder.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/UrlRewriteFinder.php index 52bf538aa3425..dfa8d87644ad6 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/UrlRewriteFinder.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/UrlRewriteFinder.php @@ -1,6 +1,6 @@ DataProductUrlRewriteDatabaseMap::class, - self::ENTITY_TYPE_CATEGORY => DataCategoryUrlRewriteDatabaseMap::class + $urlRewriteClassNames = [ + self::ENTITY_TYPE_PRODUCT => DataProductUrlRewriteDatabaseMap::class, + self::ENTITY_TYPE_CATEGORY => DataCategoryUrlRewriteDatabaseMap::class ] ) { $this->databaseMapPool = $databaseMapPool; diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php index 979e3007eba65..b801a84cc4820 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php @@ -16,7 +16,7 @@ use Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory; use Magento\CatalogUrlRewrite\Model\Map\UrlRewriteFinder; use Magento\Framework\App\ObjectManager; -use Magento\UrlRewrite\Model\UrlRewritesSetFactory; +use Magento\UrlRewrite\Model\MergeDataProviderFactory; /** * @SuppressWarnings(PHPMD.CouplingBetweenObjects) @@ -51,33 +51,33 @@ class CurrentUrlRewritesRegenerator private $urlRewritePrototype; /** @var UrlRewriteFinder */ - private $urlRewriteMap; + private $urlRewriteFinder; - /** @var \Magento\UrlRewrite\Model\UrlRewritesSet */ - private $urlRewritesSetPrototype; + /** @var \Magento\UrlRewrite\Model\MergeDataProvider */ + private $mergeDataProviderPrototype; /** * @param UrlFinderInterface $urlFinder * @param ProductUrlPathGenerator $productUrlPathGenerator * @param UrlRewriteFactory $urlRewriteFactory - * @param UrlRewriteFinder|null $urlRewriteMap - * @param \Magento\UrlRewrite\Model\UrlRewritesSetFactory|null $urlRewritesSetFactory + * @param UrlRewriteFinder|null $urlRewriteFinder + * @param \Magento\UrlRewrite\Model\MergeDataProviderFactory|null $mergeDataProviderFactory */ public function __construct( UrlFinderInterface $urlFinder, ProductUrlPathGenerator $productUrlPathGenerator, UrlRewriteFactory $urlRewriteFactory, - UrlRewriteFinder $urlRewriteMap = null, - UrlRewritesSetFactory $urlRewritesSetFactory = null + UrlRewriteFinder $urlRewriteFinder = null, + MergeDataProviderFactory $mergeDataProviderFactory = null ) { $this->urlFinder = $urlFinder; $this->productUrlPathGenerator = $productUrlPathGenerator; $this->urlRewriteFactory = $urlRewriteFactory; $this->urlRewritePrototype = $urlRewriteFactory->create(); - $this->urlRewriteMap = $urlRewriteMap ?: ObjectManager::getInstance()->get(UrlRewriteFinder::class); - $urlRewritesSetFactory = $urlRewritesSetFactory ?: ObjectManager::getInstance() - ->get(UrlRewritesSetFactory::class); - $this->urlRewritesSetPrototype = $urlRewritesSetFactory->create(); + $this->urlRewriteFinder = $urlRewriteFinder ?: ObjectManager::getInstance()->get(UrlRewriteFinder::class); + $mergeDataProviderFactory = $mergeDataProviderFactory ?: ObjectManager::getInstance() + ->get(MergeDataProviderFactory::class); + $this->mergeDataProviderPrototype = $mergeDataProviderFactory->create(); } /** @@ -91,8 +91,8 @@ public function __construct( */ public function generate($storeId, Product $product, ObjectRegistry $productCategories, $rootCategoryId = null) { - $urlRewritesSet = clone $this->urlRewritesSetPrototype; - $currentUrlRewrites = $this->urlRewriteMap->findAllByData( + $mergeDataProvider = clone $this->mergeDataProviderPrototype; + $currentUrlRewrites = $this->urlRewriteFinder->findAllByData( $product->getEntityId(), $storeId, ProductUrlRewriteGenerator::ENTITY_TYPE, @@ -104,14 +104,14 @@ public function generate($storeId, Product $product, ObjectRegistry $productCate if ($category === false) { continue; } - $urlRewritesSet->merge( + $mergeDataProvider->merge( $currentUrlRewrite->getIsAutogenerated() ? $this->generateForAutogenerated($currentUrlRewrite, $storeId, $category, $product) : $this->generateForCustom($currentUrlRewrite, $storeId, $category, $product) ); } - return $urlRewritesSet->getData(); + return $mergeDataProvider->getData(); } /** diff --git a/app/code/Magento/CatalogUrlRewrite/Model/ProductScopeRewriteGenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/ProductScopeRewriteGenerator.php index 3dc0b6572d52d..4df9fde0f2638 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/ProductScopeRewriteGenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/ProductScopeRewriteGenerator.php @@ -14,7 +14,7 @@ use Magento\CatalogUrlRewrite\Service\V1\StoreViewService; use Magento\Store\Model\Store; use Magento\Store\Model\StoreManagerInterface; -use Magento\UrlRewrite\Model\UrlRewritesSetFactory; +use Magento\UrlRewrite\Model\MergeDataProviderFactory; use Magento\Framework\App\ObjectManager; /** @@ -58,8 +58,8 @@ class ProductScopeRewriteGenerator */ private $canonicalUrlRewriteGenerator; - /** @var \Magento\UrlRewrite\Model\UrlRewritesSet */ - private $urlRewritesSetPrototype; + /** @var \Magento\UrlRewrite\Model\MergeDataProvider */ + private $mergeDataProviderPrototype; /** * @param StoreViewService $storeViewService @@ -69,7 +69,7 @@ class ProductScopeRewriteGenerator * @param CategoriesUrlRewriteGenerator $categoriesUrlRewriteGenerator * @param CurrentUrlRewritesRegenerator $currentUrlRewritesRegenerator * @param AnchorUrlRewriteGenerator $anchorUrlRewriteGenerator - * @param \Magento\UrlRewrite\Model\UrlRewritesSetFactory|null $urlRewritesSetFactory + * @param \Magento\UrlRewrite\Model\MergeDataProviderFactory|null $mergeDataProviderFactory */ public function __construct( StoreViewService $storeViewService, @@ -79,7 +79,7 @@ public function __construct( CategoriesUrlRewriteGenerator $categoriesUrlRewriteGenerator, CurrentUrlRewritesRegenerator $currentUrlRewritesRegenerator, AnchorUrlRewriteGenerator $anchorUrlRewriteGenerator, - UrlRewritesSetFactory $urlRewritesSetFactory = null + MergeDataProviderFactory $mergeDataProviderFactory = null ) { $this->storeViewService = $storeViewService; $this->storeManager = $storeManager; @@ -88,9 +88,9 @@ public function __construct( $this->categoriesUrlRewriteGenerator = $categoriesUrlRewriteGenerator; $this->currentUrlRewritesRegenerator = $currentUrlRewritesRegenerator; $this->anchorUrlRewriteGenerator = $anchorUrlRewriteGenerator; - $urlRewritesSetFactory = $urlRewritesSetFactory ?: ObjectManager::getInstance() - ->get(UrlRewritesSetFactory::class); - $this->urlRewritesSetPrototype = $urlRewritesSetFactory->create(); + $mergeDataProviderFactory = $mergeDataProviderFactory ?: ObjectManager::getInstance() + ->get(MergeDataProviderFactory::class); + $this->mergeDataProviderPrototype = $mergeDataProviderFactory->create(); } /** @@ -115,7 +115,7 @@ public function isGlobalScope($storeId) public function generateForGlobalScope($productCategories, Product $product, $rootCategoryId = null) { $productId = $product->getEntityId(); - $urlRewritesSet = clone $this->urlRewritesSetPrototype; + $mergeDataProvider = clone $this->mergeDataProviderPrototype; foreach ($product->getStoreIds() as $id) { if (!$this->isGlobalScope($id) && @@ -124,13 +124,13 @@ public function generateForGlobalScope($productCategories, Product $product, $ro $productId, Product::ENTITY )) { - $urlRewritesSet->merge( + $mergeDataProvider->merge( $this->generateForSpecificStoreView($id, $productCategories, $product, $rootCategoryId) ); } } - return $urlRewritesSet->getData(); + return $mergeDataProvider->getData(); } /** @@ -144,7 +144,7 @@ public function generateForGlobalScope($productCategories, Product $product, $ro */ public function generateForSpecificStoreView($storeId, $productCategories, Product $product, $rootCategoryId = null) { - $urlRewritesSet = clone $this->urlRewritesSetPrototype; + $mergeDataProvider = clone $this->mergeDataProviderPrototype; $categories = []; foreach ($productCategories as $category) { if ($this->isCategoryProperForGenerating($category, $storeId)) { @@ -153,13 +153,13 @@ public function generateForSpecificStoreView($storeId, $productCategories, Produ } $productCategories = $this->objectRegistryFactory->create(['entities' => $categories]); - $urlRewritesSet->merge( + $mergeDataProvider->merge( $this->canonicalUrlRewriteGenerator->generate($storeId, $product) ); - $urlRewritesSet->merge( + $mergeDataProvider->merge( $this->categoriesUrlRewriteGenerator->generate($storeId, $product, $productCategories) ); - $urlRewritesSet->merge( + $mergeDataProvider->merge( $this->currentUrlRewritesRegenerator->generate( $storeId, $product, @@ -167,11 +167,11 @@ public function generateForSpecificStoreView($storeId, $productCategories, Produ $rootCategoryId ) ); - $urlRewritesSet->merge( + $mergeDataProvider->merge( $this->anchorUrlRewriteGenerator->generate($storeId, $product, $productCategories) ); - return $urlRewritesSet->getData(); + return $mergeDataProvider->getData(); } /** diff --git a/app/code/Magento/CatalogUrlRewrite/Model/ProductUrlRewriteGenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/ProductUrlRewriteGenerator.php index 777a10bc0503d..a45b33bcc52a2 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/ProductUrlRewriteGenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/ProductUrlRewriteGenerator.php @@ -161,11 +161,11 @@ protected function isGlobalScope($storeId) * * @deprecated * @param \Magento\Framework\Data\Collection $productCategories - * @param \Magento\Catalog\Model\Product $product + * @param \Magento\Catalog\Model\Product|null $product * @param int|null $rootCategoryId * @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] */ - protected function generateForGlobalScope($productCategories, Product $product, $rootCategoryId = null) + protected function generateForGlobalScope($productCategories, $product = null, $rootCategoryId = null) { return $this->getProductScopeRewriteGenerator()->generateForGlobalScope( $productCategories, @@ -180,14 +180,14 @@ protected function generateForGlobalScope($productCategories, Product $product, * @deprecated * @param int $storeId * @param \Magento\Framework\Data\Collection $productCategories - * @param Product $product + * @param Product|null $product * @param int|null $rootCategoryId * @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] */ protected function generateForSpecificStoreView( $storeId, $productCategories, - Product $product, + $product = null, $rootCategoryId = null ) { return $this->getProductScopeRewriteGenerator() diff --git a/app/code/Magento/CatalogUrlRewrite/Observer/AfterImportDataObserver.php b/app/code/Magento/CatalogUrlRewrite/Observer/AfterImportDataObserver.php index 6781619b45e3b..27053c21cffa3 100644 --- a/app/code/Magento/CatalogUrlRewrite/Observer/AfterImportDataObserver.php +++ b/app/code/Magento/CatalogUrlRewrite/Observer/AfterImportDataObserver.php @@ -21,7 +21,7 @@ use Magento\Framework\Event\ObserverInterface; use Magento\Catalog\Model\Product\Visibility; use Magento\Framework\App\ObjectManager; -use Magento\UrlRewrite\Model\UrlRewritesSetFactory; +use Magento\UrlRewrite\Model\MergeDataProviderFactory; /** * Class AfterImportDataObserver @@ -99,8 +99,8 @@ class AfterImportDataObserver implements ObserverInterface 'visibility', ]; - /** @var \Magento\UrlRewrite\Model\UrlRewritesSet */ - private $urlRewritesSetPrototype; + /** @var \Magento\UrlRewrite\Model\MergeDataProvider */ + private $mergeDataProviderPrototype; /** * @param \Magento\Catalog\Model\ProductFactory $catalogProductFactory @@ -111,7 +111,7 @@ class AfterImportDataObserver implements ObserverInterface * @param UrlPersistInterface $urlPersist * @param UrlRewriteFactory $urlRewriteFactory * @param UrlFinderInterface $urlFinder - * @param \Magento\UrlRewrite\Model\UrlRewritesSetFactory|null $urlRewritesSetFactory + * @param \Magento\UrlRewrite\Model\MergeDataProviderFactory|null $mergeDataProviderFactory * @throws \InvalidArgumentException * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ @@ -124,7 +124,7 @@ public function __construct( UrlPersistInterface $urlPersist, UrlRewriteFactory $urlRewriteFactory, UrlFinderInterface $urlFinder, - UrlRewritesSetFactory $urlRewritesSetFactory = null + MergeDataProviderFactory $mergeDataProviderFactory = null ) { $this->urlPersist = $urlPersist; $this->catalogProductFactory = $catalogProductFactory; @@ -134,9 +134,9 @@ public function __construct( $this->storeManager = $storeManager; $this->urlRewriteFactory = $urlRewriteFactory; $this->urlFinder = $urlFinder; - $urlRewritesSetFactory = $urlRewritesSetFactory ?: ObjectManager::getInstance() - ->get(UrlRewritesSetFactory::class); - $this->urlRewritesSetPrototype = $urlRewritesSetFactory->create(); + $mergeDataProviderFactory = $mergeDataProviderFactory ?: ObjectManager::getInstance() + ->get(MergeDataProviderFactory::class); + $this->mergeDataProviderPrototype = $mergeDataProviderFactory->create(); } /** @@ -269,16 +269,16 @@ protected function populateGlobalProduct($product) */ protected function generateUrls() { - $urlRewritesSet = clone $this->urlRewritesSetPrototype; - $urlRewritesSet->merge($this->canonicalUrlRewriteGenerate()); - $urlRewritesSet->merge($this->categoriesUrlRewriteGenerate()); - $urlRewritesSet->merge($this->currentUrlRewritesRegenerate()); + $mergeDataProvider = clone $this->mergeDataProviderPrototype; + $mergeDataProvider->merge($this->canonicalUrlRewriteGenerate()); + $mergeDataProvider->merge($this->categoriesUrlRewriteGenerate()); + $mergeDataProvider->merge($this->currentUrlRewritesRegenerate()); $this->productCategories = null; unset($this->products); $this->products = []; - return $urlRewritesSet->getData(); + return $mergeDataProvider->getData(); } /** diff --git a/app/code/Magento/CatalogUrlRewrite/Observer/CategoryProcessUrlRewriteSavingObserver.php b/app/code/Magento/CatalogUrlRewrite/Observer/CategoryProcessUrlRewriteSavingObserver.php index 0911bdb8a2d5b..b864285b06a9f 100644 --- a/app/code/Magento/CatalogUrlRewrite/Observer/CategoryProcessUrlRewriteSavingObserver.php +++ b/app/code/Magento/CatalogUrlRewrite/Observer/CategoryProcessUrlRewriteSavingObserver.php @@ -42,9 +42,9 @@ public function __construct( UrlRewriteHandler $urlRewriteHandler, UrlRewriteBunchReplacer $urlRewriteBunchReplacer, DatabaseMapPool $databaseMapPool, - array $dataUrlRewriteClassNames = [ - DataCategoryUrlRewriteDatabaseMap::class, - DataProductUrlRewriteDatabaseMap::class + $dataUrlRewriteClassNames = [ + DataCategoryUrlRewriteDatabaseMap::class, + DataProductUrlRewriteDatabaseMap::class ] ) { $this->categoryUrlRewriteGenerator = $categoryUrlRewriteGenerator; diff --git a/app/code/Magento/CatalogUrlRewrite/Observer/UrlRewriteHandler.php b/app/code/Magento/CatalogUrlRewrite/Observer/UrlRewriteHandler.php index 028c86b046eff..464824e7eefe8 100644 --- a/app/code/Magento/CatalogUrlRewrite/Observer/UrlRewriteHandler.php +++ b/app/code/Magento/CatalogUrlRewrite/Observer/UrlRewriteHandler.php @@ -13,7 +13,7 @@ use Magento\Framework\Event\Observer as EventObserver; use Magento\UrlRewrite\Model\UrlPersistInterface; use Magento\UrlRewrite\Service\V1\Data\UrlRewrite; -use Magento\UrlRewrite\Model\UrlRewritesSetFactory; +use Magento\UrlRewrite\Model\MergeDataProviderFactory; class UrlRewriteHandler { @@ -40,8 +40,8 @@ class UrlRewriteHandler */ private $categoryBasedProductRewriteGenerator; - /** @var \Magento\UrlRewrite\Model\UrlRewritesSet */ - private $urlRewritesSetPrototype; + /** @var \Magento\UrlRewrite\Model\MergeDataProvider */ + private $mergeDataProviderPrototype; /** * @param \Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider $childrenCategoriesProvider @@ -49,7 +49,7 @@ class UrlRewriteHandler * @param ProductUrlRewriteGenerator $productUrlRewriteGenerator * @param UrlPersistInterface $urlPersist * @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory - * @param \Magento\UrlRewrite\Model\UrlRewritesSetFactory|null $urlRewritesSetFactory + * @param \Magento\UrlRewrite\Model\MergeDataProviderFactory|null $mergeDataProviderFactory */ public function __construct( \Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider $childrenCategoriesProvider, @@ -57,16 +57,16 @@ public function __construct( ProductUrlRewriteGenerator $productUrlRewriteGenerator, UrlPersistInterface $urlPersist, \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory, - UrlRewritesSetFactory $urlRewritesSetFactory = null + MergeDataProviderFactory $mergeDataProviderFactory = null ) { $this->childrenCategoriesProvider = $childrenCategoriesProvider; $this->categoryUrlRewriteGenerator = $categoryUrlRewriteGenerator; $this->productUrlRewriteGenerator = $productUrlRewriteGenerator; $this->urlPersist = $urlPersist; $this->productCollectionFactory = $productCollectionFactory; - $urlRewritesSetFactory = $urlRewritesSetFactory ?: ObjectManager::getInstance() - ->get(UrlRewritesSetFactory::class); - $this->urlRewritesSetPrototype = $urlRewritesSetFactory->create(); + $mergeDataProviderFactory = $mergeDataProviderFactory ?: ObjectManager::getInstance() + ->get(MergeDataProviderFactory::class); + $this->mergeDataProviderPrototype = $mergeDataProviderFactory->create(); } /** @@ -77,7 +77,7 @@ public function __construct( */ public function generateProductUrlRewrites(Category $category) { - $urlRewritesSet = clone $this->urlRewritesSetPrototype; + $mergeDataProvider = clone $this->mergeDataProviderPrototype; $this->isSkippedProduct = []; $saveRewriteHistory = $category->getData('save_rewrites_history'); $storeId = $category->getStoreId(); @@ -93,12 +93,12 @@ public function generateProductUrlRewrites(Category $category) foreach ($collection as $product) { $product->setStoreId($storeId); $product->setData('save_rewrites_history', $saveRewriteHistory); - $urlRewritesSet->merge( + $mergeDataProvider->merge( $this->productUrlRewriteGenerator->generate($product, $category->getEntityId()) ); } } else { - $urlRewritesSet->merge( + $mergeDataProvider->merge( $this->getCategoryProductsUrlRewrites( $category, $storeId, @@ -108,7 +108,7 @@ public function generateProductUrlRewrites(Category $category) ); } foreach ($this->childrenCategoriesProvider->getChildren($category, true) as $childCategory) { - $urlRewritesSet->merge( + $mergeDataProvider->merge( $this->getCategoryProductsUrlRewrites( $childCategory, $storeId, @@ -118,7 +118,7 @@ public function generateProductUrlRewrites(Category $category) ); } - return $urlRewritesSet->getData(); + return $mergeDataProvider->getData(); } /** @@ -134,7 +134,7 @@ public function getCategoryProductsUrlRewrites( $saveRewriteHistory, $rootCategoryId = null ) { - $urlRewritesSet = clone $this->urlRewritesSetPrototype; + $mergeDataProvider = clone $this->mergeDataProviderPrototype; /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection */ $productCollection = $category->getProductCollection() ->addAttributeToSelect('name') @@ -148,12 +148,12 @@ public function getCategoryProductsUrlRewrites( $this->isSkippedProduct[] = $product->getId(); $product->setStoreId($storeId); $product->setData('save_rewrites_history', $saveRewriteHistory); - $urlRewritesSet->merge( + $mergeDataProvider->merge( $this->getCategoryBasedProductRewriteGenerator()->generate($product, $category, $rootCategoryId) ); } - return $urlRewritesSet->getData(); + return $mergeDataProvider->getData(); } /** diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/ChildrenUrlRewriteGeneratorTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/ChildrenUrlRewriteGeneratorTest.php index 99553f814104d..f41d35f7688f3 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/ChildrenUrlRewriteGeneratorTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/ChildrenUrlRewriteGeneratorTest.php @@ -25,7 +25,7 @@ class ChildrenUrlRewriteGeneratorTest extends \PHPUnit_Framework_TestCase private $categoryUrlRewriteGenerator; /** @var \PHPUnit_Framework_MockObject_MockObject */ - private $urlRewritesSet; + private $mergeDataProvider; protected function setUp() { @@ -40,22 +40,22 @@ protected function setUp() $this->categoryUrlRewriteGenerator = $this->getMockBuilder( \Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator::class )->disableOriginalConstructor()->getMock(); - $urlRewritesSetFactory = $this->getMock( - \Magento\UrlRewrite\Model\UrlRewritesSetFactory::class, + $mergeDataProviderFactory = $this->getMock( + \Magento\UrlRewrite\Model\MergeDataProviderFactory::class, ['create'], [], '', false ); - $this->urlRewritesSet = new \Magento\UrlRewrite\Model\UrlRewritesSet; - $urlRewritesSetFactory->expects($this->once())->method('create')->willReturn($this->urlRewritesSet); + $this->mergeDataProvider = new \Magento\UrlRewrite\Model\MergeDataProvider; + $mergeDataProviderFactory->expects($this->once())->method('create')->willReturn($this->mergeDataProvider); $this->childrenUrlRewriteGenerator = (new ObjectManager($this))->getObject( \Magento\CatalogUrlRewrite\Model\Category\ChildrenUrlRewriteGenerator::class, [ 'childrenCategoriesProvider' => $this->childrenCategoriesProvider, 'categoryUrlRewriteGeneratorFactory' => $this->categoryUrlRewriteGeneratorFactory, - 'urlRewritesSetFactory' => $urlRewritesSetFactory + 'mergeDataProviderFactory' => $mergeDataProviderFactory ] ); } diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/CurrentUrlRewritesRegeneratorTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/CurrentUrlRewritesRegeneratorTest.php index 8a5bb18a6f30e..483df828fb044 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/CurrentUrlRewritesRegeneratorTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/CurrentUrlRewritesRegeneratorTest.php @@ -29,10 +29,10 @@ class CurrentUrlRewritesRegeneratorTest extends \PHPUnit_Framework_TestCase private $urlRewrite; /** @var \PHPUnit_Framework_MockObject_MockObject */ - private $urlRewritesSet; + private $mergeDataProvider; /** @var \PHPUnit_Framework_MockObject_MockObject */ - private $urlRewriteMap; + private $urlRewriteFinder; protected function setUp() { @@ -46,34 +46,34 @@ protected function setUp() $this->categoryUrlPathGenerator = $this->getMockBuilder( \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator::class )->disableOriginalConstructor()->getMock(); - $this->urlRewriteMap = $this->getMockBuilder(\Magento\CatalogUrlRewrite\Model\Map\UrlRewriteFinder::class) + $this->urlRewriteFinder = $this->getMockBuilder(\Magento\CatalogUrlRewrite\Model\Map\UrlRewriteFinder::class) ->disableOriginalConstructor()->getMock(); $this->urlRewriteFactory->expects($this->once())->method('create') ->willReturn($this->urlRewrite); - $urlRewritesSetFactory = $this->getMock( - \Magento\UrlRewrite\Model\UrlRewritesSetFactory::class, + $mergeDataProviderFactory = $this->getMock( + \Magento\UrlRewrite\Model\MergeDataProviderFactory::class, ['create'], [], '', false ); - $this->urlRewritesSet = new \Magento\UrlRewrite\Model\UrlRewritesSet; - $urlRewritesSetFactory->expects($this->once())->method('create')->willReturn($this->urlRewritesSet); + $this->mergeDataProvider = new \Magento\UrlRewrite\Model\MergeDataProvider; + $mergeDataProviderFactory->expects($this->once())->method('create')->willReturn($this->mergeDataProvider); $this->currentUrlRewritesRegenerator = (new ObjectManager($this))->getObject( \Magento\CatalogUrlRewrite\Model\Category\CurrentUrlRewritesRegenerator::class, [ 'categoryUrlPathGenerator' => $this->categoryUrlPathGenerator, 'urlRewriteFactory' => $this->urlRewriteFactory, - 'urlRewritesSetFactory' => $urlRewritesSetFactory, - 'urlRewriteMap' => $this->urlRewriteMap + 'mergeDataProviderFactory' => $mergeDataProviderFactory, + 'urlRewriteFinder' => $this->urlRewriteFinder ] ); } public function testIsAutogeneratedWithoutSaveRewriteHistory() { - $this->urlRewriteMap->expects($this->once())->method('getByIdentifiers') + $this->urlRewriteFinder->expects($this->once())->method('findAllByData') ->will($this->returnValue($this->getCurrentRewritesMocks([[UrlRewrite::IS_AUTOGENERATED => 1]]))); $this->category->expects($this->once())->method('getData')->with('save_rewrites_history') ->will($this->returnValue(false)); @@ -86,7 +86,7 @@ public function testIsAutogeneratedWithoutSaveRewriteHistory() public function testSkipGenerationForAutogenerated() { - $this->urlRewriteMap->expects($this->once())->method('getByIdentifiers') + $this->urlRewriteFinder->expects($this->once())->method('findAllByData') ->will( $this->returnValue( $this->getCurrentRewritesMocks( @@ -113,7 +113,7 @@ public function testIsAutogenerated() $targetPath = 'some-path.html'; $storeId = 2; $categoryId = 12; - $this->urlRewriteMap->expects($this->once())->method('getByIdentifiers') + $this->urlRewriteFinder->expects($this->once())->method('findAllByData') ->will( $this->returnValue( $this->getCurrentRewritesMocks( @@ -146,7 +146,7 @@ public function testIsAutogenerated() public function testSkipGenerationForCustom() { - $this->urlRewriteMap->expects($this->once())->method('getByIdentifiers') + $this->urlRewriteFinder->expects($this->once())->method('findAllByData') ->will( $this->returnValue( $this->getCurrentRewritesMocks( @@ -176,7 +176,7 @@ public function testGenerationForCustomWithoutTargetPathGeneration() $requestPath = 'generate-for-custom-without-redirect-type.html'; $targetPath = 'custom-target-path.html'; $description = 'description'; - $this->urlRewriteMap->expects($this->once())->method('getByIdentifiers') + $this->urlRewriteFinder->expects($this->once())->method('findAllByData') ->will( $this->returnValue( $this->getCurrentRewritesMocks( @@ -212,7 +212,7 @@ public function testGenerationForCustomWithTargetPathGeneration() $requestPath = 'generate-for-custom-without-redirect-type.html'; $targetPath = 'generated-target-path.html'; $description = 'description'; - $this->urlRewriteMap->expects($this->once())->method('getByIdentifiers') + $this->urlRewriteFinder->expects($this->once())->method('findAllByData') ->will( $this->returnValue( $this->getCurrentRewritesMocks( diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/CategoryUrlRewriteGeneratorTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/CategoryUrlRewriteGeneratorTest.php index 7cef92d7c9af4..c3a2ebb47b156 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/CategoryUrlRewriteGeneratorTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/CategoryUrlRewriteGeneratorTest.php @@ -35,7 +35,7 @@ class CategoryUrlRewriteGeneratorTest extends \PHPUnit_Framework_TestCase private $categoryRepository; /** @var \PHPUnit_Framework_MockObject_MockObject */ - private $urlRewritesSet; + private $mergeDataProvider; /** * Test method @@ -55,15 +55,15 @@ protected function setUp() ->disableOriginalConstructor()->getMock(); $this->category = $this->getMock(\Magento\Catalog\Model\Category::class, [], [], '', false); $this->categoryRepository = $this->getMock(\Magento\Catalog\Api\CategoryRepositoryInterface::class); - $urlRewritesSetFactory = $this->getMock( - \Magento\UrlRewrite\Model\UrlRewritesSetFactory::class, + $mergeDataProviderFactory = $this->getMock( + \Magento\UrlRewrite\Model\MergeDataProviderFactory::class, ['create'], [], '', false ); - $this->urlRewritesSet = new \Magento\UrlRewrite\Model\UrlRewritesSet; - $urlRewritesSetFactory->expects($this->once())->method('create')->willReturn($this->urlRewritesSet); + $this->mergeDataProvider = new \Magento\UrlRewrite\Model\MergeDataProvider; + $mergeDataProviderFactory->expects($this->once())->method('create')->willReturn($this->mergeDataProvider); $this->categoryUrlRewriteGenerator = (new ObjectManager($this))->getObject( \Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator::class, @@ -73,7 +73,7 @@ protected function setUp() 'currentUrlRewritesRegenerator' => $this->currentUrlRewritesRegenerator, 'storeViewService' => $this->storeViewService, 'categoryRepository' => $this->categoryRepository, - 'urlRewritesSetFactory' => $urlRewritesSetFactory + 'mergeDataProviderFactory' => $mergeDataProviderFactory ] ); } diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryHashMapTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryHashMapTest.php index bdca177eee3f1..1df5391f9d91d 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryHashMapTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryHashMapTest.php @@ -1,6 +1,6 @@ dataMapPoolMock = $this->getMock(HashMapPool::class, [], [], '', false); + $this->hashMapPoolMock = $this->getMock(HashMapPool::class, [], [], '', false); $this->dataCategoryMapMock = $this->getMock(DataProductHashMap::class, [], [], '', false); $this->dataCategoryUsedInProductsMapMock = $this->getMock( DataCategoryUsedInProductsHashMap::class, @@ -53,7 +53,7 @@ protected function setUp() $this->temporaryTableServiceMock = $this->getMock(TemporaryTableService::class, [], [], '', false); $this->connectionMock = $this->getMock(ResourceConnection::class, [], [], '', false); - $this->dataMapPoolMock->expects($this->any()) + $this->hashMapPoolMock->expects($this->any()) ->method('getDataMap') ->willReturnOnConsecutiveCalls($this->dataCategoryUsedInProductsMapMock, $this->dataCategoryMapMock); @@ -61,7 +61,7 @@ protected function setUp() DataCategoryUrlRewriteDatabaseMap::class, [ 'connection' => $this->connectionMock, - 'dataMapPool' => $this->dataMapPoolMock, + 'hashMapPool' => $this->hashMapPoolMock, 'temporaryTableService' => $this->temporaryTableServiceMock ] ); diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryUsedInProductsHashMapTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryUsedInProductsHashMapTest.php index 0647c30b43f10..74353d3a924f7 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryUsedInProductsHashMapTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryUsedInProductsHashMapTest.php @@ -1,6 +1,6 @@ dataMapPoolMock = $this->getMock(HashMapPool::class, [], [], '', false); + $this->hashMapPoolMock = $this->getMock(HashMapPool::class, [], [], '', false); $this->dataCategoryMapMock = $this->getMock(DataCategoryHashMap::class, [], [], '', false); $this->dataProductMapMock = $this->getMock(DataProductHashMap::class, [], [], '', false); $this->connectionMock = $this->getMock(ResourceConnection::class, [], [], '', false); - $this->dataMapPoolMock->expects($this->any()) + $this->hashMapPoolMock->expects($this->any()) ->method('getDataMap') ->willReturnOnConsecutiveCalls( $this->dataProductMapMock, @@ -56,7 +56,7 @@ protected function setUp() DataCategoryUsedInProductsHashMap::class, [ 'connection' => $this->connectionMock, - 'hashMapPool' => $this->dataMapPoolMock + 'hashMapPool' => $this->hashMapPoolMock ] ); } @@ -90,11 +90,11 @@ public function testGetAllData() $selectMock->expects($this->any()) ->method('where') ->willReturnSelf(); - $this->dataMapPoolMock->expects($this->at(4)) - ->method('resetDataMap') + $this->hashMapPoolMock->expects($this->at(4)) + ->method('resetMap') ->with(DataProductHashMap::class, 1); - $this->dataMapPoolMock->expects($this->at(5)) - ->method('resetDataMap') + $this->hashMapPoolMock->expects($this->at(5)) + ->method('resetMap') ->with(DataCategoryHashMap::class, 1); $this->assertEquals($categoryIds, $this->model->getAllData(1)); diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataProductHashMapTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataProductHashMapTest.php index 024c3ee491116..53e4d22597769 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataProductHashMapTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataProductHashMapTest.php @@ -1,6 +1,6 @@ dataMapPoolMock = $this->getMock(HashMapPool::class, [], [], '', false); + $this->hashMapPoolMock = $this->getMock(HashMapPool::class, [], [], '', false); $this->dataCategoryMapMock = $this->getMock(DataCategoryHashMap::class, [], [], '', false); $this->collectionFactoryMock = $this->getMock(CollectionFactory::class, ['create'], [], '', false); $this->productCollectionMock = $this->getMock( @@ -59,7 +59,7 @@ protected function setUp() ->method('create') ->willReturn($this->productCollectionMock); - $this->dataMapPoolMock->expects($this->any()) + $this->hashMapPoolMock->expects($this->any()) ->method('getDataMap') ->willReturn($this->dataCategoryMapMock); @@ -67,7 +67,7 @@ protected function setUp() DataProductHashMap::class, [ 'collectionFactory' => $this->collectionFactoryMock, - 'hashMapPool' => $this->dataMapPoolMock + 'hashMapPool' => $this->hashMapPoolMock ] ); } @@ -107,8 +107,8 @@ public function testGetAllData() $this->dataCategoryMapMock->expects($this->any()) ->method('getAllData') ->willReturn([]); - $this->dataMapPoolMock->expects($this->any()) - ->method('resetDataMap') + $this->hashMapPoolMock->expects($this->any()) + ->method('resetMap') ->with(DataCategoryHashMap::class, 1); $this->assertEquals($productIds, $this->model->getAllData(1)); $this->assertEquals($productIds[2], $this->model->getData(1, 2)); diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataProductUrlRewriteDatabaseMapTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataProductUrlRewriteDatabaseMapTest.php index 5999b884a47b7..d283c6d3f0410 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataProductUrlRewriteDatabaseMapTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataProductUrlRewriteDatabaseMapTest.php @@ -1,6 +1,6 @@ dataMapPoolMock = $this->getMock(HashMapPool::class, [], [], '', false); + $this->hashMapPoolMock = $this->getMock(HashMapPool::class, [], [], '', false); $this->dataProductMapMock = $this->getMock(DataProductHashMap::class, [], [], '', false); $this->temporaryTableServiceMock = $this->getMock(TemporaryTableService::class, [], [], '', false); $this->connectionMock = $this->getMock(ResourceConnection::class, [], [], '', false); - $this->dataMapPoolMock->expects($this->any()) + $this->hashMapPoolMock->expects($this->any()) ->method('getDataMap') ->willReturn($this->dataProductMapMock); @@ -49,7 +49,7 @@ protected function setUp() DataProductUrlRewriteDatabaseMap::class, [ 'connection' => $this->connectionMock, - 'dataMapPool' => $this->dataMapPoolMock, + 'hashMapPool' => $this->hashMapPoolMock, 'temporaryTableService' => $this->temporaryTableServiceMock ] ); diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/HashMapPoolTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/HashMapPoolTest.php index 213e56595e968..d9351b4e426de 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/HashMapPoolTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/HashMapPoolTest.php @@ -1,6 +1,6 @@ dataMapPoolMock = $this->getMock(DatabaseMapPool::class, [], [], '', false); + $this->databaseMapPoolMock = $this->getMock(DatabaseMapPool::class, [], [], '', false); $this->urlFinderMock = $this->getMock(UrlFinderInterface::class); $this->urlRewriteFactoryMock = $this->getMock(UrlRewriteFactory::class, ['create'], [], '', false); $this->urlRewritePrototypeMock = new UrlRewrite(); @@ -48,7 +48,7 @@ protected function setUp() $this->model = (new ObjectManager($this))->getObject( UrlRewriteFinder::class, [ - 'databaseMapPool' => $this->dataMapPoolMock, + 'databaseMapPool' => $this->databaseMapPoolMock, 'urlFinder' => $this->urlFinderMock, 'urlRewriteFactory' => $this->urlRewriteFactoryMock ] @@ -56,12 +56,12 @@ protected function setUp() } /** - * test getByIdentifiers using findAllByData + * test findAllByData using urlFinder */ public function testGetByIdentifiersFallback() { $expected = [1, 2, 3]; - $this->dataMapPoolMock->expects($this->never()) + $this->databaseMapPoolMock->expects($this->never()) ->method('getDataMap'); $this->urlFinderMock->expects($this->exactly(7)) @@ -78,7 +78,7 @@ public function testGetByIdentifiersFallback() } /** - * test getByIdentifiers Product URL rewrites + * test findAllByData Product URL rewrites */ public function testGetByIdentifiersProduct() { @@ -98,7 +98,7 @@ public function testGetByIdentifiersProduct() ]; $dataProductMapMock = $this->getMock(DataProductUrlRewriteDatabaseMap::class, [], [], '', false); - $this->dataMapPoolMock->expects($this->once()) + $this->databaseMapPoolMock->expects($this->once()) ->method('getDataMap') ->with(DataProductUrlRewriteDatabaseMap::class, 1) ->willReturn($dataProductMapMock); @@ -116,7 +116,7 @@ public function testGetByIdentifiersProduct() } /** - * test getByIdentifiers Category URL rewrites + * test findAllByData Category URL rewrites */ public function testGetByIdentifiersCategory() { @@ -136,7 +136,7 @@ public function testGetByIdentifiersCategory() ]; $dataCategoryMapMock = $this->getMock(DataCategoryUrlRewriteDatabaseMap::class, [], [], '', false); - $this->dataMapPoolMock->expects($this->once()) + $this->databaseMapPoolMock->expects($this->once()) ->method('getDataMap') ->with(DataCategoryUrlRewriteDatabaseMap::class, 1) ->willReturn($dataCategoryMapMock); diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Product/CurrentUrlRewritesRegeneratorTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Product/CurrentUrlRewritesRegeneratorTest.php index e96e6a66d6cc7..b5515c2c25459 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Product/CurrentUrlRewritesRegeneratorTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Product/CurrentUrlRewritesRegeneratorTest.php @@ -37,10 +37,10 @@ class CurrentUrlRewritesRegeneratorTest extends \PHPUnit_Framework_TestCase private $urlRewrite; /** @var \PHPUnit_Framework_MockObject_MockObject */ - private $urlRewritesSet; + private $mergeDataProvider; /** @var \PHPUnit_Framework_MockObject_MockObject */ - private $urlRewriteMap; + private $urlRewriteFinder; protected function setUp() { @@ -55,36 +55,36 @@ protected function setUp() ->disableOriginalConstructor()->getMock(); $this->objectRegistry = $this->getMockBuilder(\Magento\CatalogUrlRewrite\Model\ObjectRegistry::class) ->disableOriginalConstructor()->getMock(); - $this->urlRewriteMap = $this->getMockBuilder(\Magento\CatalogUrlRewrite\Model\Map\UrlRewriteFinder::class) + $this->urlRewriteFinder = $this->getMockBuilder(\Magento\CatalogUrlRewrite\Model\Map\UrlRewriteFinder::class) ->disableOriginalConstructor()->getMock(); $this->urlRewriteFactory->expects($this->once())->method('create') ->willReturn($this->urlRewrite); $this->productUrlPathGenerator = $this->getMockBuilder( \Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator::class )->disableOriginalConstructor()->getMock(); - $urlRewritesSetFactory = $this->getMock( - \Magento\UrlRewrite\Model\UrlRewritesSetFactory::class, + $mergeDataProviderFactory = $this->getMock( + \Magento\UrlRewrite\Model\MergeDataProviderFactory::class, ['create'], [], '', false ); - $this->urlRewritesSet = new \Magento\UrlRewrite\Model\UrlRewritesSet; - $urlRewritesSetFactory->expects($this->once())->method('create')->willReturn($this->urlRewritesSet); + $this->mergeDataProvider = new \Magento\UrlRewrite\Model\MergeDataProvider; + $mergeDataProviderFactory->expects($this->once())->method('create')->willReturn($this->mergeDataProvider); $this->currentUrlRewritesRegenerator = (new ObjectManager($this))->getObject( \Magento\CatalogUrlRewrite\Model\Product\CurrentUrlRewritesRegenerator::class, [ 'productUrlPathGenerator' => $this->productUrlPathGenerator, 'urlRewriteFactory' => $this->urlRewriteFactory, - 'urlRewritesSetFactory' => $urlRewritesSetFactory, - 'urlRewriteMap' => $this->urlRewriteMap + 'mergeDataProviderFactory' => $mergeDataProviderFactory, + 'urlRewriteFinder' => $this->urlRewriteFinder ] ); } public function testIsAutogeneratedWithoutSaveRewriteHistory() { - $this->urlRewriteMap->expects($this->once())->method('getByIdentifiers') + $this->urlRewriteFinder->expects($this->once())->method('findAllByData') ->will($this->returnValue($this->getCurrentRewritesMocks([[UrlRewrite::IS_AUTOGENERATED => 1]]))); $this->product->expects($this->once())->method('getData')->with('save_rewrites_history') ->will($this->returnValue(false)); @@ -97,7 +97,7 @@ public function testIsAutogeneratedWithoutSaveRewriteHistory() public function testSkipGenerationForAutogenerated() { - $this->urlRewriteMap->expects($this->once())->method('getByIdentifiers') + $this->urlRewriteFinder->expects($this->once())->method('findAllByData') ->will($this->returnValue($this->getCurrentRewritesMocks([ [UrlRewrite::IS_AUTOGENERATED => 1, UrlRewrite::REQUEST_PATH => 'same-path'], ]))); @@ -120,7 +120,7 @@ public function testIsAutogeneratedWithoutCategory() $storeId = 2; $productId = 12; $description = 'description'; - $this->urlRewriteMap->expects($this->once())->method('getByIdentifiers') + $this->urlRewriteFinder->expects($this->once())->method('findAllByData') ->will($this->returnValue($this->getCurrentRewritesMocks([ [ UrlRewrite::REQUEST_PATH => $requestPath, @@ -163,7 +163,7 @@ public function testIsAutogeneratedWithCategory() $storeId = 2; $metadata = ['category_id' => 2, 'some_another_data' => 1]; $description = 'description'; - $this->urlRewriteMap->expects($this->once())->method('getByIdentifiers') + $this->urlRewriteFinder->expects($this->once())->method('findAllByData') ->will($this->returnValue($this->getCurrentRewritesMocks([ [ UrlRewrite::REQUEST_PATH => $requestPath, @@ -199,7 +199,7 @@ public function testIsAutogeneratedWithCategory() public function testSkipGenerationForCustom() { - $this->urlRewriteMap->expects($this->once())->method('getByIdentifiers') + $this->urlRewriteFinder->expects($this->once())->method('findAllByData') ->will($this->returnValue($this->getCurrentRewritesMocks([ [ UrlRewrite::IS_AUTOGENERATED => 0, @@ -224,7 +224,7 @@ public function testGenerationForCustomWithoutTargetPathGeneration() $targetPath = 'custom-target-path.html'; $autoGenerated = 0; $description = 'description'; - $this->urlRewriteMap->expects($this->once())->method('getByIdentifiers') + $this->urlRewriteFinder->expects($this->once())->method('findAllByData') ->will($this->returnValue($this->getCurrentRewritesMocks([ [ UrlRewrite::REQUEST_PATH => $requestPath, @@ -262,7 +262,7 @@ public function testGenerationForCustomWithTargetPathGeneration() $targetPath = 'generated-target-path.html'; $autoGenerated = 0; $description = 'description'; - $this->urlRewriteMap->expects($this->once())->method('getByIdentifiers') + $this->urlRewriteFinder->expects($this->once())->method('findAllByData') ->will($this->returnValue($this->getCurrentRewritesMocks([ [ UrlRewrite::REQUEST_PATH => $requestPath, diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/ProductScopeRewriteGeneratorTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/ProductScopeRewriteGeneratorTest.php index a670b96b3fa5d..15d93a5026f32 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/ProductScopeRewriteGeneratorTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/ProductScopeRewriteGeneratorTest.php @@ -42,7 +42,7 @@ class ProductScopeRewriteGeneratorTest extends \PHPUnit_Framework_TestCase private $productScopeGenerator; /** @var \PHPUnit_Framework_MockObject_MockObject */ - private $urlRewritesSet; + private $mergeDataProvider; public function setUp() { @@ -64,15 +64,15 @@ public function setUp() $this->storeViewService = $this->getMockBuilder(\Magento\CatalogUrlRewrite\Service\V1\StoreViewService::class) ->disableOriginalConstructor()->getMock(); $this->storeManager = $this->getMock(StoreManagerInterface::class); - $urlRewritesSetFactory = $this->getMock( - \Magento\UrlRewrite\Model\UrlRewritesSetFactory::class, + $mergeDataProviderFactory = $this->getMock( + \Magento\UrlRewrite\Model\MergeDataProviderFactory::class, ['create'], [], '', false ); - $this->urlRewritesSet = new \Magento\UrlRewrite\Model\UrlRewritesSet; - $urlRewritesSetFactory->expects($this->once())->method('create')->willReturn($this->urlRewritesSet); + $this->mergeDataProvider = new \Magento\UrlRewrite\Model\MergeDataProvider; + $mergeDataProviderFactory->expects($this->once())->method('create')->willReturn($this->mergeDataProvider); $this->productScopeGenerator = (new ObjectManager($this))->getObject( \Magento\CatalogUrlRewrite\Model\ProductScopeRewriteGenerator::class, @@ -84,7 +84,7 @@ public function setUp() 'objectRegistryFactory' => $this->objectRegistryFactory, 'storeViewService' => $this->storeViewService, 'storeManager' => $this->storeManager, - 'urlRewritesSetFactory' => $urlRewritesSetFactory + 'mergeDataProviderFactory' => $mergeDataProviderFactory ] ); } diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Observer/AfterImportDataObserverTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Observer/AfterImportDataObserverTest.php index eaaf460008cd7..069fa1a859a79 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Observer/AfterImportDataObserverTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Observer/AfterImportDataObserverTest.php @@ -110,8 +110,8 @@ class AfterImportDataObserverTest extends \PHPUnit_Framework_TestCase */ private $product; - /** @var \Magento\UrlRewrite\Model\UrlRewritesSet|\PHPUnit_Framework_MockObject_MockObject */ - private $urlRewritesSet; + /** @var \Magento\UrlRewrite\Model\MergeDataProvider|\PHPUnit_Framework_MockObject_MockObject */ + private $mergeDataProvider; /** * Test products returned by getBunch method of event object. @@ -276,15 +276,15 @@ protected function setUp() ->expects($this->any()) ->method('getCategoryProcessor') ->willReturn($categoryProcessor); - $urlRewritesSetFactory = $this->getMock( - \Magento\UrlRewrite\Model\UrlRewritesSetFactory::class, + $mergeDataProviderFactory = $this->getMock( + \Magento\UrlRewrite\Model\MergeDataProviderFactory::class, ['create'], [], '', false ); - $this->urlRewritesSet = new \Magento\UrlRewrite\Model\UrlRewritesSet; - $urlRewritesSetFactory->expects($this->once())->method('create')->willReturn($this->urlRewritesSet); + $this->mergeDataProvider = new \Magento\UrlRewrite\Model\MergeDataProvider; + $mergeDataProviderFactory->expects($this->once())->method('create')->willReturn($this->mergeDataProvider); $this->objectManager = new ObjectManager($this); $this->import = $this->objectManager->getObject( @@ -298,7 +298,7 @@ protected function setUp() 'urlPersist' => $this->urlPersist, 'urlRewriteFactory' => $this->urlRewriteFactory, 'urlFinder' => $this->urlFinder, - 'urlRewritesSetFactory' => $urlRewritesSetFactory + 'mergeDataProviderFactory' => $mergeDataProviderFactory ] ); } @@ -448,7 +448,7 @@ public function testAfterImportData() $this->urlRewriteFactory->expects($this->any())->method('create')->willReturn($this->urlRewrite); $productUrls = [ - 'requestPath_' => $this->urlRewrite, + 'requestPath_0' => $this->urlRewrite, 'requestPath_not global' => $this->urlRewrite ]; diff --git a/app/code/Magento/UrlRewrite/Model/UrlRewritesSet.php b/app/code/Magento/UrlRewrite/Model/MergeDataProvider.php similarity index 93% rename from app/code/Magento/UrlRewrite/Model/UrlRewritesSet.php rename to app/code/Magento/UrlRewrite/Model/MergeDataProvider.php index b395a922d6511..f37e6cbde7d75 100644 --- a/app/code/Magento/UrlRewrite/Model/UrlRewritesSet.php +++ b/app/code/Magento/UrlRewrite/Model/MergeDataProvider.php @@ -1,6 +1,6 @@ urlRewritesSet = (new ObjectManager($this))->getObject( - UrlRewritesSet::class, + MergeDataProvider::class, [] ); } diff --git a/dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/Observer/CategoryProcessUrlRewriteSavingObserverTest.php b/dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/Observer/CategoryProcessUrlRewriteSavingObserverTest.php index 9f92a2400cf6c..101ce61c779dc 100644 --- a/dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/Observer/CategoryProcessUrlRewriteSavingObserverTest.php +++ b/dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/Observer/CategoryProcessUrlRewriteSavingObserverTest.php @@ -1,6 +1,6 @@ temporaryTableService, 'createdTables'); - $createdTables->setAccessible(true); - $createdTables->setValue($this->temporaryTableService, ['tmp_select_table' => $this->adapterMock]); - $createdTables->setAccessible(false); + $createdTableAdapters = new \ReflectionProperty($this->temporaryTableService, 'createdTableAdapters'); + $createdTableAdapters->setAccessible(true); + $createdTableAdapters->setValue($this->temporaryTableService, ['tmp_select_table' => $this->adapterMock]); + $createdTableAdapters->setAccessible(false); $this->adapterMock->expects($this->any()) ->method('dropTemporaryTable') From fec72085e230c66e90d0b12134f9e79c515b13b7 Mon Sep 17 00:00:00 2001 From: Cristian Partica Date: Mon, 9 Jan 2017 22:38:52 -0600 Subject: [PATCH 29/39] MAGETWO-58924: SQL error wait timeout error when saving categories - fix issues with code review, add phpdocs for renaming and fixing unit tests --- .../Category/ChildrenUrlRewriteGenerator.php | 5 +- .../CurrentUrlRewritesRegenerator.php | 7 +- .../Model/CategoryUrlRewriteGenerator.php | 5 +- .../Model/Map/DataCategoryHashMap.php | 22 +++---- .../Map/DataCategoryUrlRewriteDatabaseMap.php | 3 +- .../Map/DataCategoryUsedInProductsHashMap.php | 65 ++++++++++--------- .../Model/Map/DataProductHashMap.php | 51 +++++++-------- .../Map/DataProductUrlRewriteDatabaseMap.php | 3 +- .../Model/Map/DatabaseMapInterface.php | 4 +- .../Model/Map/DatabaseMapPool.php | 5 ++ .../Model/Map/HashMapInterface.php | 5 +- .../Model/Map/HashMapPool.php | 9 +-- .../Model/Map/UrlRewriteFinder.php | 17 ++--- .../Product/CurrentUrlRewritesRegenerator.php | 5 +- .../Model/ProductScopeRewriteGenerator.php | 5 +- .../Observer/AfterImportDataObserver.php | 5 +- ...ategoryProcessUrlRewriteSavingObserver.php | 17 +---- .../Observer/UrlRewriteHandler.php | 5 +- .../Model/CategoryUrlPathGeneratorTest.php | 3 +- .../Test/Unit/Model/Map/HashMapPoolTest.php | 16 ++++- .../Unit/Model/Map/UrlRewriteFinderTest.php | 8 ++- app/code/Magento/CatalogUrlRewrite/etc/di.xml | 8 +++ .../Test/Unit/Model/MergeDataProviderTest.php | 6 +- .../Framework/DB/TemporaryTableService.php | 8 ++- 24 files changed, 162 insertions(+), 125 deletions(-) diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Category/ChildrenUrlRewriteGenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/Category/ChildrenUrlRewriteGenerator.php index 920da739c8eb8..de647a34619df 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Category/ChildrenUrlRewriteGenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Category/ChildrenUrlRewriteGenerator.php @@ -34,8 +34,9 @@ public function __construct( ) { $this->childrenCategoriesProvider = $childrenCategoriesProvider; $this->categoryUrlRewriteGeneratorFactory = $categoryUrlRewriteGeneratorFactory; - $mergeDataProviderFactory = $mergeDataProviderFactory ?: ObjectManager::getInstance() - ->get(MergeDataProviderFactory::class); + if (!isset($mergeDataProviderFactory)) { + $mergeDataProviderFactory = ObjectManager::getInstance()->get(MergeDataProviderFactory::class); + } $this->mergeDataProviderPrototype = $mergeDataProviderFactory->create(); } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php index eb3fd3954bd6e..c994b42464147 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Category/CurrentUrlRewritesRegenerator.php @@ -57,8 +57,11 @@ public function __construct( $this->urlFinder = $urlFinder; $this->urlRewriteFinder = $urlRewriteFinder ?: \Magento\Framework\App\ObjectManager::getInstance() ->get(\Magento\CatalogUrlRewrite\Model\Map\UrlRewriteFinder::class); - $mergeDataProviderFactory = $mergeDataProviderFactory ?: \Magento\Framework\App\ObjectManager::getInstance() - ->get(\Magento\UrlRewrite\Model\MergeDataProviderFactory::class); + if (!isset($mergeDataProviderFactory)) { + $mergeDataProviderFactory = \Magento\Framework\App\ObjectManager::getInstance()->get( + \Magento\UrlRewrite\Model\MergeDataProviderFactory::class + ); + } $this->mergeDataProviderPrototype = $mergeDataProviderFactory->create(); } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGenerator.php index f95c72515c46a..e565760f33448 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGenerator.php @@ -67,8 +67,9 @@ public function __construct( $this->childrenUrlRewriteGenerator = $childrenUrlRewriteGenerator; $this->currentUrlRewritesRegenerator = $currentUrlRewritesRegenerator; $this->categoryRepository = $categoryRepository; - $mergeDataProviderFactory = $mergeDataProviderFactory ?: ObjectManager::getInstance() - ->get(MergeDataProviderFactory::class); + if (!isset($mergeDataProviderFactory)) { + $mergeDataProviderFactory = ObjectManager::getInstance()->get(MergeDataProviderFactory::class); + } $this->mergeDataProviderPrototype = $mergeDataProviderFactory->create(); } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryHashMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryHashMap.php index acb8c4a897113..616e120ebafe4 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryHashMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryHashMap.php @@ -47,10 +47,7 @@ public function __construct( */ public function getAllData($categoryId) { - if (!isset($this->hashMap[$categoryId])) { - $this->hashMap[$categoryId] = $this->generateData($categoryId); - } - return $this->hashMap[$categoryId]; + return $this->generateData($categoryId); } /** @@ -58,25 +55,28 @@ public function getAllData($categoryId) */ public function getData($categoryId, $key) { - $this->getAllData($categoryId); - return $this->hashMap[$categoryId][$key]; + $categorySpecificData = $this->generateData($categoryId); + return $categorySpecificData[$key]; } /** - * Queries the database and returns results + * Returns an array of categories ids that includes category identified by $categoryId and all its subcategories * * @param int $categoryId * @return array */ private function generateData($categoryId) { - $category = $this->categoryRepository->get($categoryId); - return $this->collection->addIdFilter($this->getAllCategoryChildrenIds($category)) - ->getAllIds(); + if (!isset($this->hashMap[$categoryId])) { + $category = $this->categoryRepository->get($categoryId); + $this->hashMap[$categoryId] = $this->collection->addIdFilter($this->getAllCategoryChildrenIds($category)) + ->getAllIds(); + } + return $this->hashMap[$categoryId]; } /** - * Queries sub-categories ids from a category + * Queries the database for sub-categories ids from a category * * @param CategoryInterface $category * @return int[] diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUrlRewriteDatabaseMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUrlRewriteDatabaseMap.php index bcc82ce8e44a2..032ed5e13023e 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUrlRewriteDatabaseMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUrlRewriteDatabaseMap.php @@ -58,7 +58,8 @@ private function generateTableAdapter($categoryId) } /** - * Queries the database and returns the name of the temporary table where data is stored + * Queries the database for all category url rewrites that are affected by the category identified by $categoryId + * It returns the name of the temporary table where the resulting data is stored * * @param int $categoryId * @return string diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUsedInProductsHashMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUsedInProductsHashMap.php index 0a3a99f8a0e7c..8864226d069d2 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUsedInProductsHashMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUsedInProductsHashMap.php @@ -38,10 +38,7 @@ public function __construct( */ public function getAllData($categoryId) { - if (!isset($this->hashMap[$categoryId])) { - $this->hashMap[$categoryId] = $this->generateData($categoryId); - } - return $this->hashMap[$categoryId]; + return $this->generateData($categoryId); } /** @@ -49,45 +46,49 @@ public function getAllData($categoryId) */ public function getData($categoryId, $key) { - $this->getAllData($categoryId); - return $this->hashMap[$categoryId][$key]; + $categorySpecificData = $this->generateData($categoryId); + return $categorySpecificData[$key]; } /** - * Queries the database and returns results + * Returns an array of product ids for all DataProductHashMap list, + * that occur in other categories not part of DataCategoryHashMap list * * @param int $categoryId * @return array */ private function generateData($categoryId) { - $productsLinkConnection = $this->connection->getConnection(); - $select = $productsLinkConnection->select() - ->from($this->connection->getTableName('catalog_category_product'), ['category_id']) - ->where( - $productsLinkConnection->prepareSqlCondition( - 'product_id', - [ - 'in' => $this->hashMapPool->getDataMap( - DataProductHashMap::class, - $categoryId - )->getAllData($categoryId) - ] - ) - ) - ->where( - $productsLinkConnection->prepareSqlCondition( - 'category_id', - [ - 'nin' => $this->hashMapPool->getDataMap( - DataCategoryHashMap::class, - $categoryId - )->getAllData($categoryId) - ] + if (!isset($this->hashMap[$categoryId])) { + $productsLinkConnection = $this->connection->getConnection(); + $select = $productsLinkConnection->select() + ->from($this->connection->getTableName('catalog_category_product'), ['category_id']) + ->where( + $productsLinkConnection->prepareSqlCondition( + 'product_id', + [ + 'in' => $this->hashMapPool->getDataMap( + DataProductHashMap::class, + $categoryId + )->getAllData($categoryId) + ] + ) ) - )->group('category_id'); + ->where( + $productsLinkConnection->prepareSqlCondition( + 'category_id', + [ + 'nin' => $this->hashMapPool->getDataMap( + DataCategoryHashMap::class, + $categoryId + )->getAllData($categoryId) + ] + ) + )->group('category_id'); - return $productsLinkConnection->fetchCol($select); + $this->hashMap[$categoryId] = $productsLinkConnection->fetchCol($select); + } + return $this->hashMap[$categoryId]; } /** diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductHashMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductHashMap.php index 74731a7d40e20..a20b837e4096a 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductHashMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductHashMap.php @@ -45,10 +45,7 @@ public function __construct( */ public function getAllData($categoryId) { - if (!isset($this->hashMap[$categoryId])) { - $this->hashMap[$categoryId] = $this->generateData($categoryId); - } - return $this->hashMap[$categoryId]; + return $this->generateData($categoryId); } /** @@ -56,38 +53,40 @@ public function getAllData($categoryId) */ public function getData($categoryId, $key) { - $this->getAllData($categoryId); - return $this->hashMap[$categoryId][$key]; + $categorySpecificData = $this->generateData($categoryId); + return $categorySpecificData[$key]; } /** - * Queries the database and returns results + * Returns an array of ids of all visible products and assigned to a category and all its subcategories * * @param int $categoryId * @return array */ private function generateData($categoryId) { - $productsCollection = $this->collectionFactory->create(); - $productsCollection->getSelect() - ->joinInner( - ['cp' => $this->connection->getTableName('catalog_category_product')], - 'cp.product_id = e.entity_id', - [] - ) - ->where( - $productsCollection->getConnection()->prepareSqlCondition( - 'cp.category_id', - [ - 'in' => $this->hashMapPool->getDataMap( - DataCategoryHashMap::class, - $categoryId - )->getAllData($categoryId) - ] + if (!isset($this->hashMap[$categoryId])) { + $productsCollection = $this->collectionFactory->create(); + $productsCollection->getSelect() + ->joinInner( + ['cp' => $this->connection->getTableName('catalog_category_product')], + 'cp.product_id = e.entity_id', + [] ) - )->group('e.entity_id'); - - return $productsCollection->getAllIds(); + ->where( + $productsCollection->getConnection()->prepareSqlCondition( + 'cp.category_id', + [ + 'in' => $this->hashMapPool->getDataMap( + DataCategoryHashMap::class, + $categoryId + )->getAllData($categoryId) + ] + ) + )->group('e.entity_id'); + $this->hashMap[$categoryId] = $productsCollection->getAllIds(); + } + return $this->hashMap[$categoryId]; } /** diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductUrlRewriteDatabaseMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductUrlRewriteDatabaseMap.php index bca04d727aefe..c369e2d62a6c2 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductUrlRewriteDatabaseMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductUrlRewriteDatabaseMap.php @@ -72,7 +72,8 @@ public function getData($categoryId, $key) } /** - * Queries the database and returns the name of the temporary table where data is stored + * Queries the database for all category url rewrites that are affected by the category identified by $categoryId + * It returns the name of the temporary table where the resulting data is stored * * @param int $categoryId * @return string diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DatabaseMapInterface.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DatabaseMapInterface.php index 32f81e246156f..c2a9a89a33fb5 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DatabaseMapInterface.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DatabaseMapInterface.php @@ -5,10 +5,11 @@ */ namespace Magento\CatalogUrlRewrite\Model\Map; -use \Magento\Framework\DB\Select; +use Magento\Framework\DB\Select; /** * Interface for a mysql data type of a map + * * Is used to get data by a unique key from a temporary table in mysql to prevent memory usage * It internally holds the knowledge the creation of the actual data and it initializes itself when we call getData * We should always call destroyTableAdapter when we don't need anymore the temporary tables @@ -17,6 +18,7 @@ interface DatabaseMapInterface { /** * Gets data by key from a map identified by a category Id + * * The key is a unique identifier that matches the values of the index used to build the temporary table * * Example "1_2" where ids would correspond to store_id entity_id diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DatabaseMapPool.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DatabaseMapPool.php index 5dbe1ed12b861..30732013eace4 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DatabaseMapPool.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DatabaseMapPool.php @@ -50,6 +50,11 @@ public function getDataMap($instanceName, $categoryId) 'category' => $categoryId ] ); + if (!$this->dataArray[$key] instanceof DatabaseMapInterface) { + throw new \InvalidArgumentException( + $instanceName . ' does not implement interface ' . DatabaseMapInterface::class + ); + } } return $this->dataArray[$key]; } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/HashMapInterface.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/HashMapInterface.php index 3e806fcbd805d..341cdb1a4fdb1 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/HashMapInterface.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/HashMapInterface.php @@ -5,11 +5,12 @@ */ namespace Magento\CatalogUrlRewrite\Model\Map; -use \Magento\Framework\DB\Select; +use Magento\Framework\DB\Select; /** * Interface for a hash data map - * It is used for classes tht would build hash maps and store them into memory + * + * It is used for classes that will build hash maps and store them into memory * The initialization is done transparently whenever getAllData or getData is called * The map, upon initialization, might have a dependency on some other DataMapInterfaces * The map has to free memory after we're done using it diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/HashMapPool.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/HashMapPool.php index 74ede5393cf67..bc6be808d4d9a 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/HashMapPool.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/HashMapPool.php @@ -44,10 +44,6 @@ public function __construct( public function getDataMap($instanceName, $categoryId) { $key = $instanceName . '-' . $categoryId; - $reflectionClass = new \ReflectionClass($instanceName); - if (!$reflectionClass->implementsInterface(HashMapInterface::class)) { - throw new \Exception($instanceName . ' does not implement interface ' . HashMapInterface::class); - } if (!isset($this->dataArray[$key])) { $this->dataArray[$key] = $this->objectManager->create( $instanceName, @@ -55,6 +51,11 @@ public function getDataMap($instanceName, $categoryId) 'category' => $categoryId ] ); + if (!$this->dataArray[$key] instanceof HashMapInterface) { + throw new \InvalidArgumentException( + $instanceName . ' does not implement interface ' . HashMapInterface::class + ); + } } return $this->dataArray[$key]; } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/UrlRewriteFinder.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/UrlRewriteFinder.php index dfa8d87644ad6..bcadfd848e883 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/UrlRewriteFinder.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/UrlRewriteFinder.php @@ -11,8 +11,11 @@ use Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory; /** - * Allows query to Category and Product UrlRewrite Database Map or UrlFinderInterface by identifiers + * Finds specific queried url rewrites identified by specific fields * + * A group of identifiers specifies a query consumed by the client to retrieve existing url rewrites from the database + * Clients will query a map of DatabaseMapInterface type through this class resulting into a set of url rewrites results + * Each map type will fallback to a UrlFinderInterface by identifiers for unmapped values */ class UrlRewriteFinder { @@ -29,7 +32,7 @@ class UrlRewriteFinder private $urlRewritePrototype; /** @var array */ - private $urlRewriteClassNames; + private $urlRewriteClassNames = []; /** * @param DatabaseMapPool $databaseMapPool @@ -41,10 +44,7 @@ public function __construct( DatabaseMapPool $databaseMapPool, UrlFinderInterface $urlFinder, UrlRewriteFactory $urlRewriteFactory, - $urlRewriteClassNames = [ - self::ENTITY_TYPE_PRODUCT => DataProductUrlRewriteDatabaseMap::class, - self::ENTITY_TYPE_CATEGORY => DataCategoryUrlRewriteDatabaseMap::class - ] + array $urlRewriteClassNames = [] ) { $this->databaseMapPool = $databaseMapPool; $this->urlFinder = $urlFinder; @@ -53,7 +53,8 @@ public function __construct( } /** - * Queries by identifiers from maps or falls-back to UrlFinderInterface + * Retrieves existing url rewrites filtered by identifiers from prebuild database maps + * This method will fall-back to by using UrlFinderInterface when map type is not found in configured list * * @param int $entityId * @param int $storeId @@ -86,7 +87,7 @@ public function findAllByData($entityId, $storeId, $entityType, $rootCategoryId } /** - * Transfer array values to url rewrite object values + * Transfers an array values to url rewrite object values * * @param array $data * @return UrlRewrite[] diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php index b801a84cc4820..dee95f236841e 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php @@ -75,8 +75,9 @@ public function __construct( $this->urlRewriteFactory = $urlRewriteFactory; $this->urlRewritePrototype = $urlRewriteFactory->create(); $this->urlRewriteFinder = $urlRewriteFinder ?: ObjectManager::getInstance()->get(UrlRewriteFinder::class); - $mergeDataProviderFactory = $mergeDataProviderFactory ?: ObjectManager::getInstance() - ->get(MergeDataProviderFactory::class); + if (!isset($mergeDataProviderFactory)) { + $mergeDataProviderFactory = ObjectManager::getInstance()->get(MergeDataProviderFactory::class); + } $this->mergeDataProviderPrototype = $mergeDataProviderFactory->create(); } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/ProductScopeRewriteGenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/ProductScopeRewriteGenerator.php index 4df9fde0f2638..6e357459de8d3 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/ProductScopeRewriteGenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/ProductScopeRewriteGenerator.php @@ -88,8 +88,9 @@ public function __construct( $this->categoriesUrlRewriteGenerator = $categoriesUrlRewriteGenerator; $this->currentUrlRewritesRegenerator = $currentUrlRewritesRegenerator; $this->anchorUrlRewriteGenerator = $anchorUrlRewriteGenerator; - $mergeDataProviderFactory = $mergeDataProviderFactory ?: ObjectManager::getInstance() - ->get(MergeDataProviderFactory::class); + if (!isset($mergeDataProviderFactory)) { + $mergeDataProviderFactory = ObjectManager::getInstance()->get(MergeDataProviderFactory::class); + } $this->mergeDataProviderPrototype = $mergeDataProviderFactory->create(); } diff --git a/app/code/Magento/CatalogUrlRewrite/Observer/AfterImportDataObserver.php b/app/code/Magento/CatalogUrlRewrite/Observer/AfterImportDataObserver.php index 27053c21cffa3..f76db7bef44d5 100644 --- a/app/code/Magento/CatalogUrlRewrite/Observer/AfterImportDataObserver.php +++ b/app/code/Magento/CatalogUrlRewrite/Observer/AfterImportDataObserver.php @@ -134,8 +134,9 @@ public function __construct( $this->storeManager = $storeManager; $this->urlRewriteFactory = $urlRewriteFactory; $this->urlFinder = $urlFinder; - $mergeDataProviderFactory = $mergeDataProviderFactory ?: ObjectManager::getInstance() - ->get(MergeDataProviderFactory::class); + if (!isset($mergeDataProviderFactory)) { + $mergeDataProviderFactory = ObjectManager::getInstance()->get(MergeDataProviderFactory::class); + } $this->mergeDataProviderPrototype = $mergeDataProviderFactory->create(); } diff --git a/app/code/Magento/CatalogUrlRewrite/Observer/CategoryProcessUrlRewriteSavingObserver.php b/app/code/Magento/CatalogUrlRewrite/Observer/CategoryProcessUrlRewriteSavingObserver.php index b864285b06a9f..dd0250049a426 100644 --- a/app/code/Magento/CatalogUrlRewrite/Observer/CategoryProcessUrlRewriteSavingObserver.php +++ b/app/code/Magento/CatalogUrlRewrite/Observer/CategoryProcessUrlRewriteSavingObserver.php @@ -71,32 +71,17 @@ public function execute(\Magento\Framework\Event\Observer $observer) || $category->dataHasChangedFor('is_anchor') || $category->getIsChangedProductList() ) { - $this->initializeUrlRewritesDataMaps($category); - $categoryUrlRewriteResult = $this->categoryUrlRewriteGenerator->generate($category); $this->urlRewriteBunchReplacer->doBunchReplace($categoryUrlRewriteResult); $productUrlRewriteResult = $this->urlRewriteHandler->generateProductUrlRewrites($category); $this->urlRewriteBunchReplacer->doBunchReplace($productUrlRewriteResult); + //frees memory for maps that are self-initialized in multiple classes that were called by the generators $this->resetUrlRewritesDataMaps($category); } } - /** - * Initializes data maps to be further used - * - * @param Category $category - * @return void - */ - private function initializeUrlRewritesDataMaps($category) - { - foreach ($this->dataUrlRewriteClassNames as $className) { - $this->databaseMapPool->getDataMap($className, $category->getEntityId()); - } - - } - /** * Resets used data maps to free up memory and temporary tables * diff --git a/app/code/Magento/CatalogUrlRewrite/Observer/UrlRewriteHandler.php b/app/code/Magento/CatalogUrlRewrite/Observer/UrlRewriteHandler.php index 464824e7eefe8..727dd744cd8a7 100644 --- a/app/code/Magento/CatalogUrlRewrite/Observer/UrlRewriteHandler.php +++ b/app/code/Magento/CatalogUrlRewrite/Observer/UrlRewriteHandler.php @@ -64,8 +64,9 @@ public function __construct( $this->productUrlRewriteGenerator = $productUrlRewriteGenerator; $this->urlPersist = $urlPersist; $this->productCollectionFactory = $productCollectionFactory; - $mergeDataProviderFactory = $mergeDataProviderFactory ?: ObjectManager::getInstance() - ->get(MergeDataProviderFactory::class); + if (!isset($mergeDataProviderFactory)) { + $mergeDataProviderFactory = $mergeDataProviderFactory = ObjectManager::getInstance()->get(MergeDataProviderFactory::class); + } $this->mergeDataProviderPrototype = $mergeDataProviderFactory->create(); } diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/CategoryUrlPathGeneratorTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/CategoryUrlPathGeneratorTest.php index d5abe69ff7977..f36799f789fbd 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/CategoryUrlPathGeneratorTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/CategoryUrlPathGeneratorTest.php @@ -5,8 +5,7 @@ */ namespace Magento\CatalogUrlRewrite\Test\Unit\Model; -use \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator; - +use Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator; use Magento\Catalog\Model\Category; use Magento\Store\Model\ScopeInterface; use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/HashMapPoolTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/HashMapPoolTest.php index d9351b4e426de..78b114f60da85 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/HashMapPoolTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/HashMapPoolTest.php @@ -47,8 +47,22 @@ public function testGetDataMap() ->method('create') ->willReturnOnConsecutiveCalls($dataCategoryMapMock, $dataProductMapMock, $dataProductMapMockOtherCategory); $this->assertEquals($dataCategoryMapMock, $this->model->getDataMap(DataCategoryHashMap::class, 1)); - $this->assertEquals($dataCategoryMapMock, $this->model->getDataMap(DataCategoryHashMap::class, 1)); + $this->assertSame($dataCategoryMapMock, $this->model->getDataMap(DataCategoryHashMap::class, 1)); $this->assertEquals($dataProductMapMock, $this->model->getDataMap(DataProductHashMap::class, 1)); $this->assertEquals($dataProductMapMockOtherCategory, $this->model->getDataMap(DataCategoryHashMap::class, 2)); } + + /** + * Tests getDataMap with exception + */ + public function testGetDataMapException() + { + $nonInterface = $this->getMock(HashMapPool::class, [], [], '', false); + + $this->objectManagerMock->expects($this->any()) + ->method('create') + ->willReturn($nonInterface); + $this->setExpectedException(\InvalidArgumentException::class); + $this->model->getDataMap(HashMapPool::class, 1); + } } diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/UrlRewriteFinderTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/UrlRewriteFinderTest.php index 2a8149960c6d4..b0c4500dab3e0 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/UrlRewriteFinderTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/UrlRewriteFinderTest.php @@ -45,12 +45,18 @@ protected function setUp() ->method('create') ->willReturn($this->urlRewritePrototypeMock); + $urlRewriteClassesNamesArray = [ + UrlRewriteFinder::ENTITY_TYPE_PRODUCT => DataProductUrlRewriteDatabaseMap::class, + UrlRewriteFinder::ENTITY_TYPE_CATEGORY => DataCategoryUrlRewriteDatabaseMap::class + ]; + $this->model = (new ObjectManager($this))->getObject( UrlRewriteFinder::class, [ 'databaseMapPool' => $this->databaseMapPoolMock, 'urlFinder' => $this->urlFinderMock, - 'urlRewriteFactory' => $this->urlRewriteFactoryMock + 'urlRewriteFactory' => $this->urlRewriteFactoryMock, + 'urlRewriteClassNames' => $urlRewriteClassesNamesArray ] ); } diff --git a/app/code/Magento/CatalogUrlRewrite/etc/di.xml b/app/code/Magento/CatalogUrlRewrite/etc/di.xml index 5a8974fc52da4..7f0fc718083ec 100644 --- a/app/code/Magento/CatalogUrlRewrite/etc/di.xml +++ b/app/code/Magento/CatalogUrlRewrite/etc/di.xml @@ -23,4 +23,12 @@ + + + + Magento\CatalogUrlRewrite\Model\Map\DataProductUrlRewriteDatabaseMap + Magento\CatalogUrlRewrite\Model\Map\DataCategoryUrlRewriteDatabaseMap + + + diff --git a/app/code/Magento/UrlRewrite/Test/Unit/Model/MergeDataProviderTest.php b/app/code/Magento/UrlRewrite/Test/Unit/Model/MergeDataProviderTest.php index 3e57c7176b734..76aa5d368be4c 100644 --- a/app/code/Magento/UrlRewrite/Test/Unit/Model/MergeDataProviderTest.php +++ b/app/code/Magento/UrlRewrite/Test/Unit/Model/MergeDataProviderTest.php @@ -35,7 +35,7 @@ protected function setUp() * @param array $urlRewriteMockArray * @param String $expectedData * @param int $arrayCount - * @dataProvider getMergeTestParameters + * @dataProvider mergeDataProvider * @return void */ public function testMerge($urlRewriteMockArray, $expectedData, $arrayCount) @@ -56,11 +56,11 @@ public function testGetDataWhenEmpty() } /** - * Data provider for testMerge + * Data provider for testMerge * * @return array */ - public function getMergeTestParameters() + public function mergeDataProvider() { $urlRewriteMock1 = $this->getMock(UrlRewrite::class, [], [], '', false); diff --git a/lib/internal/Magento/Framework/DB/TemporaryTableService.php b/lib/internal/Magento/Framework/DB/TemporaryTableService.php index afc4d88463888..62d1a0d65cfb5 100644 --- a/lib/internal/Magento/Framework/DB/TemporaryTableService.php +++ b/lib/internal/Magento/Framework/DB/TemporaryTableService.php @@ -71,7 +71,11 @@ public function createFromSelect( foreach ($indexes as $indexName => $columns) { $renderedColumns = implode(',', array_map([$adapter, 'quoteIdentifier'], $columns)); - $indexType = sprintf('INDEX %s USING %s', $adapter->quoteIdentifier($indexName), "{$indexMethod}"); + $indexType = sprintf( + 'INDEX %s USING %s', + $adapter->quoteIdentifier($indexName), + $indexMethod + ); if ($indexName === 'PRIMARY') { $indexType = 'PRIMARY KEY'; @@ -86,7 +90,7 @@ public function createFromSelect( 'CREATE TEMPORARY TABLE %s %s ENGINE=%s IGNORE (%s)', $adapter->quoteIdentifier($name), $indexStatements ? '(' . implode(',', $indexStatements) . ')' : '', - "{$engine}", + $adapter->quoteIdentifier($engine), "{$select}" ); From 1287421f66c59c5b995a2a62d7126c676b9d92d4 Mon Sep 17 00:00:00 2001 From: Cristian Partica Date: Tue, 10 Jan 2017 13:43:12 -0600 Subject: [PATCH 30/39] MAGETWO-58924: SQL error wait timeout error when saving categories - fix issues with code review, add phpdocs for renaming and fixing unit tests --- .../Model/Map/DataCategoryHashMap.php | 31 +++++------- .../Map/DataCategoryUsedInProductsHashMap.php | 31 +++++------- .../Model/Map/DataProductHashMap.php | 31 +++++------- .../Model/Map/DatabaseMapPool.php | 5 +- .../Model/Map/HashMapPool.php | 5 +- .../Test/Unit/Model/Map/HashMapPoolTest.php | 31 ++++++++++-- app/etc/di.xml | 13 +++++ .../Framework/DB/TemporaryTableService.php | 47 ++++++++++++++++--- .../Test/Unit/TemporaryTableServiceTest.php | 35 ++++++++++++-- 9 files changed, 155 insertions(+), 74 deletions(-) diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryHashMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryHashMap.php index 616e120ebafe4..d432767e92689 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryHashMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryHashMap.php @@ -42,30 +42,13 @@ public function __construct( $this->categoryResource = $categoryResource; } - /** - * {@inheritdoc} - */ - public function getAllData($categoryId) - { - return $this->generateData($categoryId); - } - - /** - * {@inheritdoc} - */ - public function getData($categoryId, $key) - { - $categorySpecificData = $this->generateData($categoryId); - return $categorySpecificData[$key]; - } - /** * Returns an array of categories ids that includes category identified by $categoryId and all its subcategories * * @param int $categoryId * @return array */ - private function generateData($categoryId) + public function getAllData($categoryId) { if (!isset($this->hashMap[$categoryId])) { $category = $this->categoryRepository->get($categoryId); @@ -75,6 +58,18 @@ private function generateData($categoryId) return $this->hashMap[$categoryId]; } + /** + * {@inheritdoc} + */ + public function getData($categoryId, $key) + { + $categorySpecificData = $this->getAllData($categoryId); + if (isset($categorySpecificData[$key])) { + return $categorySpecificData[$key]; + } + return []; + } + /** * Queries the database for sub-categories ids from a category * diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUsedInProductsHashMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUsedInProductsHashMap.php index 8864226d069d2..65200ba1f413d 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUsedInProductsHashMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryUsedInProductsHashMap.php @@ -33,23 +33,6 @@ public function __construct( $this->hashMapPool = $hashMapPool; } - /** - * {@inheritdoc} - */ - public function getAllData($categoryId) - { - return $this->generateData($categoryId); - } - - /** - * {@inheritdoc} - */ - public function getData($categoryId, $key) - { - $categorySpecificData = $this->generateData($categoryId); - return $categorySpecificData[$key]; - } - /** * Returns an array of product ids for all DataProductHashMap list, * that occur in other categories not part of DataCategoryHashMap list @@ -57,7 +40,7 @@ public function getData($categoryId, $key) * @param int $categoryId * @return array */ - private function generateData($categoryId) + public function getAllData($categoryId) { if (!isset($this->hashMap[$categoryId])) { $productsLinkConnection = $this->connection->getConnection(); @@ -91,6 +74,18 @@ private function generateData($categoryId) return $this->hashMap[$categoryId]; } + /** + * {@inheritdoc} + */ + public function getData($categoryId, $key) + { + $categorySpecificData = $this->getAllData($categoryId); + if (isset($categorySpecificData[$key])) { + return $categorySpecificData[$key]; + } + return []; + } + /** * {@inheritdoc} */ diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductHashMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductHashMap.php index a20b837e4096a..4b107043459a3 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductHashMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductHashMap.php @@ -40,30 +40,13 @@ public function __construct( $this->connection = $connection; } - /** - * {@inheritdoc} - */ - public function getAllData($categoryId) - { - return $this->generateData($categoryId); - } - - /** - * {@inheritdoc} - */ - public function getData($categoryId, $key) - { - $categorySpecificData = $this->generateData($categoryId); - return $categorySpecificData[$key]; - } - /** * Returns an array of ids of all visible products and assigned to a category and all its subcategories * * @param int $categoryId * @return array */ - private function generateData($categoryId) + public function getAllData($categoryId) { if (!isset($this->hashMap[$categoryId])) { $productsCollection = $this->collectionFactory->create(); @@ -89,6 +72,18 @@ private function generateData($categoryId) return $this->hashMap[$categoryId]; } + /** + * {@inheritdoc} + */ + public function getData($categoryId, $key) + { + $categorySpecificData = $this->getAllData($categoryId); + if (isset($categorySpecificData[$key])) { + return $categorySpecificData[$key]; + } + return []; + } + /** * {@inheritdoc} */ diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DatabaseMapPool.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DatabaseMapPool.php index 30732013eace4..bb94c1972a17c 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DatabaseMapPool.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DatabaseMapPool.php @@ -44,17 +44,18 @@ public function getDataMap($instanceName, $categoryId) { $key = $instanceName . '-' . $categoryId; if (!isset($this->dataArray[$key])) { - $this->dataArray[$key] = $this->objectManager->create( + $instance = $this->objectManager->create( $instanceName, [ 'category' => $categoryId ] ); - if (!$this->dataArray[$key] instanceof DatabaseMapInterface) { + if (!$instance instanceof DatabaseMapInterface) { throw new \InvalidArgumentException( $instanceName . ' does not implement interface ' . DatabaseMapInterface::class ); } + $this->dataArray[$key] = $instance; } return $this->dataArray[$key]; } diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/HashMapPool.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/HashMapPool.php index bc6be808d4d9a..6606812a61995 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/HashMapPool.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/HashMapPool.php @@ -45,17 +45,18 @@ public function getDataMap($instanceName, $categoryId) { $key = $instanceName . '-' . $categoryId; if (!isset($this->dataArray[$key])) { - $this->dataArray[$key] = $this->objectManager->create( + $instance = $this->objectManager->create( $instanceName, [ 'category' => $categoryId ] ); - if (!$this->dataArray[$key] instanceof HashMapInterface) { + if (!$instance instanceof HashMapInterface) { throw new \InvalidArgumentException( $instanceName . ' does not implement interface ' . HashMapInterface::class ); } + $this->dataArray[$key] = $instance; } return $this->dataArray[$key]; } diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/HashMapPoolTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/HashMapPoolTest.php index 78b114f60da85..eef5e05e4f642 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/HashMapPoolTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/HashMapPoolTest.php @@ -9,6 +9,7 @@ use Magento\CatalogUrlRewrite\Model\Map\HashMapPool; use Magento\CatalogUrlRewrite\Model\Map\DataCategoryHashMap; use Magento\CatalogUrlRewrite\Model\Map\DataProductHashMap; +use Magento\CatalogUrlRewrite\Model\Map\DataCategoryUsedInProductsHashMap; use Magento\Framework\ObjectManagerInterface; /** @@ -41,15 +42,35 @@ public function testGetDataMap() { $dataCategoryMapMock = $this->getMock(DataCategoryHashMap::class, [], [], '', false); $dataProductMapMock = $this->getMock(DataProductHashMap::class, [], [], '', false); - $dataProductMapMockOtherCategory = $this->getMock(DataProductHashMap::class, [], [], '', false); + $dataProductMapMockOtherCategory = $this->getMock(DataCategoryUsedInProductsHashMap::class, [], [], '', false); $this->objectManagerMock->expects($this->any()) ->method('create') - ->willReturnOnConsecutiveCalls($dataCategoryMapMock, $dataProductMapMock, $dataProductMapMockOtherCategory); - $this->assertEquals($dataCategoryMapMock, $this->model->getDataMap(DataCategoryHashMap::class, 1)); + ->willReturnMap( + [ + [ + DataCategoryHashMap::class, + ['category' => 1], + $dataCategoryMapMock + ], + [ + DataProductHashMap::class, + ['category' => 1], + $dataProductMapMock + ], + [ + DataCategoryUsedInProductsHashMap::class, + ['category' => 2], + $dataProductMapMockOtherCategory + ] + ] + ); $this->assertSame($dataCategoryMapMock, $this->model->getDataMap(DataCategoryHashMap::class, 1)); - $this->assertEquals($dataProductMapMock, $this->model->getDataMap(DataProductHashMap::class, 1)); - $this->assertEquals($dataProductMapMockOtherCategory, $this->model->getDataMap(DataCategoryHashMap::class, 2)); + $this->assertSame($dataProductMapMock, $this->model->getDataMap(DataProductHashMap::class, 1)); + $this->assertSame( + $dataProductMapMockOtherCategory, + $this->model->getDataMap(DataCategoryUsedInProductsHashMap::class, 2) + ); } /** diff --git a/app/etc/di.xml b/app/etc/di.xml index 3a5533fd825ce..a7c38ae1246b6 100755 --- a/app/etc/di.xml +++ b/app/etc/di.xml @@ -1221,4 +1221,17 @@ + + + + HASH + BTREE + + + INNODB + MEMORY + MYISAM + + + diff --git a/lib/internal/Magento/Framework/DB/TemporaryTableService.php b/lib/internal/Magento/Framework/DB/TemporaryTableService.php index 62d1a0d65cfb5..a2c6f6d46515f 100644 --- a/lib/internal/Magento/Framework/DB/TemporaryTableService.php +++ b/lib/internal/Magento/Framework/DB/TemporaryTableService.php @@ -16,8 +16,21 @@ */ class TemporaryTableService { + CONST HASH = 'HASH'; + CONST INNODB = 'INNODB'; + + /** + * @var string[] + */ + private $allowedIndexMethods; + /** - * @var $random + * @var string[] + */ + private $allowedEngines; + + /** + * @var \Magento\Framework\Math\Random */ private $random; @@ -28,10 +41,17 @@ class TemporaryTableService /** * @param \Magento\Framework\Math\Random $random + * @param string[] $allowedIndexMethods + * @param string[] $allowedEngines */ - public function __construct(\Magento\Framework\Math\Random $random) - { + public function __construct( + \Magento\Framework\Math\Random $random, + $allowedIndexMethods = [self::HASH], + $allowedEngines = [self::INNODB] + ) { $this->random = $random; + $this->allowedIndexMethods = $allowedIndexMethods; + $this->allowedEngines = $allowedEngines; } /** @@ -55,16 +75,29 @@ public function __construct(\Magento\Framework\Math\Random $random) * @param AdapterInterface $adapter * @param array $indexes * @param string $indexMethod - * @param string $engine + * @param string $dbEngine * @return string + * @throws \InvalidArgumentException */ public function createFromSelect( Select $select, AdapterInterface $adapter, array $indexes = [], - $indexMethod = 'HASH', - $engine = 'INNODB' + $indexMethod = self::HASH, + $dbEngine = self::INNODB ) { + if (!in_array($indexMethod, $this->allowedIndexMethods)) { + throw new \InvalidArgumentException( + sprintf('indexMethod must be of type %s', implode(',', $this->allowedIndexMethods)) + ); + } + + if (!in_array($dbEngine, $this->allowedEngines)) { + throw new \InvalidArgumentException( + sprintf('dbEngine must be of type %s', implode(',', $this->allowedEngines)) + ); + } + $name = $this->random->getUniqueHash('tmp_select_'); $indexStatements = []; @@ -90,7 +123,7 @@ public function createFromSelect( 'CREATE TEMPORARY TABLE %s %s ENGINE=%s IGNORE (%s)', $adapter->quoteIdentifier($name), $indexStatements ? '(' . implode(',', $indexStatements) . ')' : '', - $adapter->quoteIdentifier($engine), + $adapter->quoteIdentifier($dbEngine), "{$select}" ); diff --git a/lib/internal/Magento/Framework/DB/Test/Unit/TemporaryTableServiceTest.php b/lib/internal/Magento/Framework/DB/Test/Unit/TemporaryTableServiceTest.php index 4105fefb5785b..68ccad5e3855d 100644 --- a/lib/internal/Magento/Framework/DB/Test/Unit/TemporaryTableServiceTest.php +++ b/lib/internal/Magento/Framework/DB/Test/Unit/TemporaryTableServiceTest.php @@ -51,12 +51,39 @@ protected function setUp() ); } + /** + * Run test createFromSelect method + * + * @return void + */ + public function testCreateFromSelectWithException() + { + $this->setExpectedException(\InvalidArgumentException::class); + $random = 'random_table'; + $indexes = [ + ['PRIMARY' => ['primary_column_name']], + 'CREATE TEMPORARY TABLE random_table (PRIMARY KEY(primary_column_name)) ENGINE=INNODB IGNORE ' + . '(select * from sometable)' + ]; + + $this->assertEquals( + $random, + $this->temporaryTableService->createFromSelect( + $this->selectMock, + $this->adapterMock, + $indexes, + TemporaryTableService::HASH . "Other", + TemporaryTableService::INNODB . "Other" + ) + ); + } + /** * Run test createFromSelect method * * @param array $indexes * @param string $expectedSelect - * @dataProvider getCreateFromSelectTestParameters + * @dataProvider createFromSelectDataProvider * @return void */ public function testCreateFromSelect($indexes, $expectedSelect) @@ -115,7 +142,7 @@ public function testDropTableWhenCreatedTablesArrayIsEmpty() * @param string $tableName * @param bool $assertion * - * @dataProvider getDropTableTestParameters + * @dataProvider dropTableWhenCreatedTablesArrayNotEmptyDataProvider * @return void */ public function testDropTableWhenCreatedTablesArrayNotEmpty($tableName, $assertion) @@ -135,7 +162,7 @@ public function testDropTableWhenCreatedTablesArrayNotEmpty($tableName, $asserti /** * @return array */ - public function getCreateFromSelectTestParameters() + public function createFromSelectDataProvider() { return [ [ @@ -170,7 +197,7 @@ public function getCreateFromSelectTestParameters() /** * @return array */ - public function getDropTableTestParameters() + public function dropTableWhenCreatedTablesArrayNotEmptyDataProvider() { return [ ['tmp_select_table_1', false], From f1e86dfba5aefe84a1e7ae28020e2c3c725327a3 Mon Sep 17 00:00:00 2001 From: Cristian Partica Date: Tue, 10 Jan 2017 15:12:46 -0600 Subject: [PATCH 31/39] MAGETWO-58924: SQL error wait timeout error when saving categories - fix static issues --- .../Observer/UrlRewriteHandler.php | 2 +- .../Test/Unit/Model/Map/HashMapPoolTest.php | 20 +++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/app/code/Magento/CatalogUrlRewrite/Observer/UrlRewriteHandler.php b/app/code/Magento/CatalogUrlRewrite/Observer/UrlRewriteHandler.php index 727dd744cd8a7..73b7c2b8167bb 100644 --- a/app/code/Magento/CatalogUrlRewrite/Observer/UrlRewriteHandler.php +++ b/app/code/Magento/CatalogUrlRewrite/Observer/UrlRewriteHandler.php @@ -65,7 +65,7 @@ public function __construct( $this->urlPersist = $urlPersist; $this->productCollectionFactory = $productCollectionFactory; if (!isset($mergeDataProviderFactory)) { - $mergeDataProviderFactory = $mergeDataProviderFactory = ObjectManager::getInstance()->get(MergeDataProviderFactory::class); + $mergeDataProviderFactory = ObjectManager::getInstance()->get(MergeDataProviderFactory::class); } $this->mergeDataProviderPrototype = $mergeDataProviderFactory->create(); } diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/HashMapPoolTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/HashMapPoolTest.php index eef5e05e4f642..616a1d9b524d5 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/HashMapPoolTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/HashMapPoolTest.php @@ -48,21 +48,21 @@ public function testGetDataMap() ->method('create') ->willReturnMap( [ - [ - DataCategoryHashMap::class, - ['category' => 1], - $dataCategoryMapMock - ], + [ + DataCategoryHashMap::class, + ['category' => 1], + $dataCategoryMapMock + ], [ DataProductHashMap::class, ['category' => 1], $dataProductMapMock ], - [ - DataCategoryUsedInProductsHashMap::class, - ['category' => 2], - $dataProductMapMockOtherCategory - ] + [ + DataCategoryUsedInProductsHashMap::class, + ['category' => 2], + $dataProductMapMockOtherCategory + ] ] ); $this->assertSame($dataCategoryMapMock, $this->model->getDataMap(DataCategoryHashMap::class, 1)); From 364f9e00bac280d768dd660cca4767ac54ede227 Mon Sep 17 00:00:00 2001 From: Cristian Partica Date: Tue, 10 Jan 2017 15:20:08 -0600 Subject: [PATCH 32/39] MAGETWO-58924: SQL error wait timeout error when saving categories - fix static issues --- .../Framework/DB/TemporaryTableService.php | 16 ++++++++-------- .../DB/Test/Unit/TemporaryTableServiceTest.php | 8 +++++--- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/lib/internal/Magento/Framework/DB/TemporaryTableService.php b/lib/internal/Magento/Framework/DB/TemporaryTableService.php index a2c6f6d46515f..7eabdf1af926c 100644 --- a/lib/internal/Magento/Framework/DB/TemporaryTableService.php +++ b/lib/internal/Magento/Framework/DB/TemporaryTableService.php @@ -16,8 +16,8 @@ */ class TemporaryTableService { - CONST HASH = 'HASH'; - CONST INNODB = 'INNODB'; + const INDEX_METHOD_HASH = 'HASH'; + const INDEX_METHOD_INNODB = 'INNODB'; /** * @var string[] @@ -46,8 +46,8 @@ class TemporaryTableService */ public function __construct( \Magento\Framework\Math\Random $random, - $allowedIndexMethods = [self::HASH], - $allowedEngines = [self::INNODB] + $allowedIndexMethods = [], + $allowedEngines = [] ) { $this->random = $random; $this->allowedIndexMethods = $allowedIndexMethods; @@ -83,18 +83,18 @@ public function createFromSelect( Select $select, AdapterInterface $adapter, array $indexes = [], - $indexMethod = self::HASH, - $dbEngine = self::INNODB + $indexMethod = self::INDEX_METHOD_HASH, + $dbEngine = self::INDEX_METHOD_INNODB ) { if (!in_array($indexMethod, $this->allowedIndexMethods)) { throw new \InvalidArgumentException( - sprintf('indexMethod must be of type %s', implode(',', $this->allowedIndexMethods)) + sprintf('indexMethod must be one of %s', implode(',', $this->allowedIndexMethods)) ); } if (!in_array($dbEngine, $this->allowedEngines)) { throw new \InvalidArgumentException( - sprintf('dbEngine must be of type %s', implode(',', $this->allowedEngines)) + sprintf('dbEngine must be one of %s', implode(',', $this->allowedEngines)) ); } diff --git a/lib/internal/Magento/Framework/DB/Test/Unit/TemporaryTableServiceTest.php b/lib/internal/Magento/Framework/DB/Test/Unit/TemporaryTableServiceTest.php index 68ccad5e3855d..960e5eb07e94a 100644 --- a/lib/internal/Magento/Framework/DB/Test/Unit/TemporaryTableServiceTest.php +++ b/lib/internal/Magento/Framework/DB/Test/Unit/TemporaryTableServiceTest.php @@ -46,7 +46,9 @@ protected function setUp() $this->temporaryTableService = (new ObjectManager($this))->getObject( TemporaryTableService::class, [ - 'random' => $this->randomMock + 'random' => $this->randomMock, + 'allowedIndexMethods' => ['HASH'], + 'allowedEngines' => ['INNODB'] ] ); } @@ -72,8 +74,8 @@ public function testCreateFromSelectWithException() $this->selectMock, $this->adapterMock, $indexes, - TemporaryTableService::HASH . "Other", - TemporaryTableService::INNODB . "Other" + TemporaryTableService::INDEX_METHOD_HASH . "Other", + TemporaryTableService::INDEX_METHOD_INNODB . "Other" ) ); } From 79a92845fffd8a93fc95399b08985dcf04a7272f Mon Sep 17 00:00:00 2001 From: Cristian Partica Date: Tue, 10 Jan 2017 15:49:05 -0600 Subject: [PATCH 33/39] MAGETWO-58924: SQL error wait timeout error when saving categories - fix static issues --- .../CatalogUrlRewrite/Test/Unit/Model/Map/HashMapPoolTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/HashMapPoolTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/HashMapPoolTest.php index 616a1d9b524d5..b9efdb01f2e1d 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/HashMapPoolTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/HashMapPoolTest.php @@ -64,7 +64,7 @@ public function testGetDataMap() $dataProductMapMockOtherCategory ] ] - ); + ); $this->assertSame($dataCategoryMapMock, $this->model->getDataMap(DataCategoryHashMap::class, 1)); $this->assertSame($dataProductMapMock, $this->model->getDataMap(DataProductHashMap::class, 1)); $this->assertSame( From 2893de73de2d95c98f6873979a2b0d1470a78da9 Mon Sep 17 00:00:00 2001 From: Cristian Partica Date: Tue, 10 Jan 2017 16:04:41 -0600 Subject: [PATCH 34/39] MAGETWO-58924: SQL error wait timeout error when saving categories - fix static issues --- lib/internal/Magento/Framework/DB/TemporaryTableService.php | 4 ++-- .../Framework/DB/Test/Unit/TemporaryTableServiceTest.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/internal/Magento/Framework/DB/TemporaryTableService.php b/lib/internal/Magento/Framework/DB/TemporaryTableService.php index 7eabdf1af926c..36a96f69ad67c 100644 --- a/lib/internal/Magento/Framework/DB/TemporaryTableService.php +++ b/lib/internal/Magento/Framework/DB/TemporaryTableService.php @@ -17,7 +17,7 @@ class TemporaryTableService { const INDEX_METHOD_HASH = 'HASH'; - const INDEX_METHOD_INNODB = 'INNODB'; + const DB_ENGINE_INNODB = 'INNODB'; /** * @var string[] @@ -84,7 +84,7 @@ public function createFromSelect( AdapterInterface $adapter, array $indexes = [], $indexMethod = self::INDEX_METHOD_HASH, - $dbEngine = self::INDEX_METHOD_INNODB + $dbEngine = self::DB_ENGINE_INNODB ) { if (!in_array($indexMethod, $this->allowedIndexMethods)) { throw new \InvalidArgumentException( diff --git a/lib/internal/Magento/Framework/DB/Test/Unit/TemporaryTableServiceTest.php b/lib/internal/Magento/Framework/DB/Test/Unit/TemporaryTableServiceTest.php index 960e5eb07e94a..620a197386fde 100644 --- a/lib/internal/Magento/Framework/DB/Test/Unit/TemporaryTableServiceTest.php +++ b/lib/internal/Magento/Framework/DB/Test/Unit/TemporaryTableServiceTest.php @@ -75,7 +75,7 @@ public function testCreateFromSelectWithException() $this->adapterMock, $indexes, TemporaryTableService::INDEX_METHOD_HASH . "Other", - TemporaryTableService::INDEX_METHOD_INNODB . "Other" + TemporaryTableService::DB_ENGINE_INNODB . "Other" ) ); } From bef74960ad542671d8143c5cb24eaa2a990c60c9 Mon Sep 17 00:00:00 2001 From: Cristian Partica Date: Wed, 11 Jan 2017 11:53:38 -0600 Subject: [PATCH 35/39] MAGETWO-58924: SQL error wait timeout error when saving categories - fix travis build for names --- app/code/Magento/UrlRewrite/Model/MergeDataProvider.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/UrlRewrite/Model/MergeDataProvider.php b/app/code/Magento/UrlRewrite/Model/MergeDataProvider.php index f37e6cbde7d75..20ecf0f52c057 100644 --- a/app/code/Magento/UrlRewrite/Model/MergeDataProvider.php +++ b/app/code/Magento/UrlRewrite/Model/MergeDataProvider.php @@ -5,7 +5,7 @@ */ namespace Magento\UrlRewrite\Model; -use Magento\UrlRewrite\Service\V1\Data\UrlRewrite; +use Magento\UrlRewrite\Service\V1\Data\UrlRewrite as UrlRewriteService; /** * This class is to be used as a container for new generated url rewrites by adding new ones using merge method @@ -24,7 +24,7 @@ class MergeDataProvider /** * Adds url rewrites to class data container by removing duplicates by a unique key * - * @param UrlRewrite[] $urlRewritesArray + * @param UrlRewriteService[] $urlRewritesArray * @return void */ public function merge(array $urlRewritesArray) @@ -42,7 +42,7 @@ public function merge(array $urlRewritesArray) /** * Returns the data added to container * - * @return UrlRewrite[] + * @return UrlRewriteService[] */ public function getData() { From 1cdfdbd5aaf37615d0af821b11ad8e8fdcf6ae35 Mon Sep 17 00:00:00 2001 From: Cristian Partica Date: Thu, 12 Jan 2017 12:38:24 -0600 Subject: [PATCH 36/39] MAGETWO-58924: SQL error wait timeout error when saving categories - fix sample data --- .../Model/Map/DataCategoryHashMap.php | 10 +--------- .../Unit/Model/Map/DataCategoryHashMapTest.php | 17 +---------------- 2 files changed, 2 insertions(+), 25 deletions(-) diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryHashMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryHashMap.php index d432767e92689..ece8b2033951d 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryHashMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryHashMap.php @@ -5,7 +5,6 @@ */ namespace Magento\CatalogUrlRewrite\Model\Map; -use Magento\Catalog\Model\ResourceModel\Category\Collection; use Magento\Catalog\Model\ResourceModel\Category; use Magento\Catalog\Model\CategoryRepository; use Magento\Catalog\Api\Data\CategoryInterface; @@ -21,24 +20,18 @@ class DataCategoryHashMap implements HashMapInterface /** @var CategoryRepository */ private $categoryRepository; - /** @var Collection */ - private $collection; - /** @var Category */ private $categoryResource; /** * @param CategoryRepository $categoryRepository - * @param Collection $collection * @param Category $categoryResource */ public function __construct( CategoryRepository $categoryRepository, - Collection $collection, Category $categoryResource ) { $this->categoryRepository = $categoryRepository; - $this->collection = $collection; $this->categoryResource = $categoryResource; } @@ -52,8 +45,7 @@ public function getAllData($categoryId) { if (!isset($this->hashMap[$categoryId])) { $category = $this->categoryRepository->get($categoryId); - $this->hashMap[$categoryId] = $this->collection->addIdFilter($this->getAllCategoryChildrenIds($category)) - ->getAllIds(); + $this->hashMap[$categoryId] = $this->getAllCategoryChildrenIds($category); } return $this->hashMap[$categoryId]; } diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryHashMapTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryHashMapTest.php index 1df5391f9d91d..1aa6276742b34 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryHashMapTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryHashMapTest.php @@ -5,7 +5,6 @@ */ namespace Magento\CatalogUrlRewrite\Test\Unit\Model\Map; -use Magento\Catalog\Model\ResourceModel\Category\Collection; use Magento\Catalog\Model\ResourceModel\Category as CategoryResource; use Magento\Framework\DB\Select; use Magento\Catalog\Model\CategoryRepository; @@ -22,9 +21,6 @@ class DataCategoryHashMapTest extends \PHPUnit_Framework_TestCase /** @var CategoryRepository|\PHPUnit_Framework_MockObject_MockObject */ private $categoryRepository; - /** @var Collection|\PHPUnit_Framework_MockObject_MockObject */ - private $collection; - /** @var CategoryResource|\PHPUnit_Framework_MockObject_MockObject */ private $categoryResource; @@ -34,7 +30,6 @@ class DataCategoryHashMapTest extends \PHPUnit_Framework_TestCase protected function setUp() { $this->categoryRepository = $this->getMock(CategoryRepository::class, [], [], '', false); - $this->collection = $this->getMock(Collection::class, ['addIdFilter', 'getAllIds'], [], '', false); $this->categoryResource = $this->getMock( CategoryResource::class, ['getConnection', 'getEntityTable'], @@ -47,7 +42,6 @@ protected function setUp() DataCategoryHashMap::class, [ 'categoryRepository' => $this->categoryRepository, - 'collection' => $this->collection, 'categoryResource' => $this->categoryResource ] ); @@ -68,15 +62,6 @@ public function testGetAllData() $this->categoryRepository->expects($this->any()) ->method('get') ->willReturn($categoryMock); - $categoryMock->expects($this->any()) - ->method('getResourceCollection') - ->willReturn($this->collection); - $this->collection->expects($this->any()) - ->method('addIdFilter') - ->willReturnSelf(); - $this->collection->expects($this->exactly(3)) - ->method('getAllIds') - ->willReturnOnConsecutiveCalls($categoryIds, $categoryIdsOther, $categoryIds); $categoryMock->expects($this->any()) ->method('getResource') ->willReturn($this->categoryResource); @@ -97,7 +82,7 @@ public function testGetAllData() ->willReturnSelf(); $connectionAdapterMock->expects($this->any()) ->method('fetchCol') - ->willReturnOnConsecutiveCalls($categoryIds, $categoryIdsOther); + ->willReturnOnConsecutiveCalls($categoryIds, $categoryIdsOther, $categoryIds); $this->assertEquals($categoryIds, $this->model->getAllData(1)); $this->assertEquals($categoryIds[2], $this->model->getData(1, 2)); From 1326845ed555eb115fe3f18423a593b6644f6be0 Mon Sep 17 00:00:00 2001 From: Cristian Partica Date: Thu, 12 Jan 2017 13:31:27 -0600 Subject: [PATCH 37/39] MAGETWO-58924: SQL error wait timeout error when saving categories - fix semantic BIC --- .../CatalogUrlRewrite/Model/CategoryUrlRewriteGenerator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGenerator.php index e565760f33448..84ecb5b8a53e8 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGenerator.php @@ -79,7 +79,7 @@ public function __construct( * @param int|null $rootCategoryId * @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] */ - public function generate(Category $category, $overrideStoreUrls = false, $rootCategoryId = null) + public function generate($category, $overrideStoreUrls = false, $rootCategoryId = null) { if ($rootCategoryId === null) { $rootCategoryId = $category->getEntityId(); From 7891dddbbfb633424d409f341abd458105f510cb Mon Sep 17 00:00:00 2001 From: Cristian Partica Date: Thu, 12 Jan 2017 14:24:41 -0600 Subject: [PATCH 38/39] MAGETWO-58924: SQL error wait timeout error when saving categories - fix CHI --- .../Model/Map/DataProductUrlRewriteDatabaseMap.php | 1 - 1 file changed, 1 deletion(-) diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductUrlRewriteDatabaseMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductUrlRewriteDatabaseMap.php index c369e2d62a6c2..19c701d9445c6 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductUrlRewriteDatabaseMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataProductUrlRewriteDatabaseMap.php @@ -12,7 +12,6 @@ /** * Map that holds data for category url rewrites entity - * @SuppressWarnings(PHPCPD) */ class DataProductUrlRewriteDatabaseMap implements DatabaseMapInterface { From ef10316fa00cfc9df980718c97ec25b3b5bd1091 Mon Sep 17 00:00:00 2001 From: Cristian Partica Date: Fri, 13 Jan 2017 10:44:50 -0600 Subject: [PATCH 39/39] MAGETWO-58924: SQL error wait timeout error when saving categories - fix singleton resource --- .../Model/Map/DataCategoryHashMap.php | 17 +++++++++-------- .../Unit/Model/Map/DataCategoryHashMapTest.php | 17 +++++++++++++---- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryHashMap.php b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryHashMap.php index ece8b2033951d..668517f46d897 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryHashMap.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Map/DataCategoryHashMap.php @@ -5,7 +5,7 @@ */ namespace Magento\CatalogUrlRewrite\Model\Map; -use Magento\Catalog\Model\ResourceModel\Category; +use Magento\Catalog\Model\ResourceModel\CategoryFactory; use Magento\Catalog\Model\CategoryRepository; use Magento\Catalog\Api\Data\CategoryInterface; @@ -20,19 +20,19 @@ class DataCategoryHashMap implements HashMapInterface /** @var CategoryRepository */ private $categoryRepository; - /** @var Category */ - private $categoryResource; + /** @var CategoryFactory */ + private $categoryResourceFactory; /** * @param CategoryRepository $categoryRepository - * @param Category $categoryResource + * @param CategoryFactory $categoryResourceFactory */ public function __construct( CategoryRepository $categoryRepository, - Category $categoryResource + CategoryFactory $categoryResourceFactory ) { $this->categoryRepository = $categoryRepository; - $this->categoryResource = $categoryResource; + $this->categoryResourceFactory = $categoryResourceFactory; } /** @@ -70,9 +70,10 @@ public function getData($categoryId, $key) */ private function getAllCategoryChildrenIds(CategoryInterface $category) { - $connection = $this->categoryResource->getConnection(); + $categoryResource = $this->categoryResourceFactory->create(); + $connection = $categoryResource->getConnection(); $select = $connection->select() - ->from($this->categoryResource->getEntityTable(), 'entity_id') + ->from($categoryResource->getEntityTable(), 'entity_id') ->where($connection->quoteIdentifier('path') . ' LIKE :c_path'); $bind = ['c_path' => $category->getPath() . '%']; return $connection->fetchCol($select, $bind); diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryHashMapTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryHashMapTest.php index 1aa6276742b34..52d65710fed69 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryHashMapTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/DataCategoryHashMapTest.php @@ -5,7 +5,8 @@ */ namespace Magento\CatalogUrlRewrite\Test\Unit\Model\Map; -use Magento\Catalog\Model\ResourceModel\Category as CategoryResource; +use Magento\Catalog\Model\ResourceModel\CategoryFactory; +use Magento\Catalog\Model\ResourceModel\Category; use Magento\Framework\DB\Select; use Magento\Catalog\Model\CategoryRepository; use Magento\Catalog\Api\Data\CategoryInterface; @@ -21,7 +22,10 @@ class DataCategoryHashMapTest extends \PHPUnit_Framework_TestCase /** @var CategoryRepository|\PHPUnit_Framework_MockObject_MockObject */ private $categoryRepository; - /** @var CategoryResource|\PHPUnit_Framework_MockObject_MockObject */ + /** @var CategoryResourceFactory|\PHPUnit_Framework_MockObject_MockObject */ + private $categoryResourceFactory; + + /** @var Category|\PHPUnit_Framework_MockObject_MockObject */ private $categoryResource; /** @var DataCategoryHashMap|\PHPUnit_Framework_MockObject_MockObject */ @@ -30,19 +34,24 @@ class DataCategoryHashMapTest extends \PHPUnit_Framework_TestCase protected function setUp() { $this->categoryRepository = $this->getMock(CategoryRepository::class, [], [], '', false); + $this->categoryResourceFactory = $this->getMock(CategoryFactory::class, ['create'], [], '', false); $this->categoryResource = $this->getMock( - CategoryResource::class, + Category::class, ['getConnection', 'getEntityTable'], [], '', false ); + $this->categoryResourceFactory->expects($this->any()) + ->method('create') + ->willReturn($this->categoryResource); + $this->model = (new ObjectManager($this))->getObject( DataCategoryHashMap::class, [ 'categoryRepository' => $this->categoryRepository, - 'categoryResource' => $this->categoryResource + 'categoryResourceFactory' => $this->categoryResourceFactory ] ); }