/** * 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) { foreach ($observer->getEvent()->getProducts() as $productId) { $product = $this->productRepository->getById($productId, false, $this->request->getParam('store_id', Store::DEFAULT_STORE_ID)); $this->urlPersist->deleteByData([UrlRewrite::ENTITY_ID => $product->getId(), UrlRewrite::ENTITY_TYPE => ProductUrlRewriteGenerator::ENTITY_TYPE]); $this->urlPersist->replace($this->productUrlRewriteGenerator->generate($product)); } }
/** * On delete handler to remove related url rewrites * * @param \Magento\Cms\Model\ResourceModel\Page $subject * @param \Closure $proceed * @param \Magento\Framework\Model\AbstractModel $page * @return \Magento\Cms\Model\ResourceModel\Page * * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function aroundDelete(\Magento\Cms\Model\ResourceModel\Page $subject, \Closure $proceed, \Magento\Framework\Model\AbstractModel $page) { $result = $proceed($page); if ($page->isDeleted()) { $this->urlPersist->deleteByData([UrlRewrite::ENTITY_ID => $page->getId(), UrlRewrite::ENTITY_TYPE => CmsPageUrlRewriteGenerator::ENTITY_TYPE]); } return $result; }
/** * Remove product urls from storage * * @param \Magento\Framework\Event\Observer $observer * @return void */ public function execute(\Magento\Framework\Event\Observer $observer) { /** @var \Magento\Catalog\Model\Product $product */ $product = $observer->getEvent()->getProduct(); if ($product->getId()) { $this->urlPersist->deleteByData([UrlRewrite::ENTITY_ID => $product->getId(), UrlRewrite::ENTITY_TYPE => ProductUrlRewriteGenerator::ENTITY_TYPE]); } }
/** * Generate urls for UrlRewrite and save it in storage * * @param \Magento\Framework\Event\Observer $observer * @return void */ public function execute(EventObserver $observer) { /** @var $cmsPage \Magento\Cms\Model\Page */ $cmsPage = $observer->getEvent()->getObject(); if ($cmsPage->dataHasChangedFor('identifier')) { $urls = $this->cmsPageUrlRewriteGenerator->generate($cmsPage); $this->urlPersist->replace($urls); } }
/** * Generate urls for UrlRewrite and save it in storage * * @param \Magento\Framework\Event\Observer $observer * @return void */ public function execute(EventObserver $observer) { /** @var $cmsPage \Magento\Cms\Model\Page */ $cmsPage = $observer->getEvent()->getObject(); if ($cmsPage->dataHasChangedFor('identifier') || $cmsPage->dataHasChangedFor('store_id')) { $urls = $this->cmsPageUrlRewriteGenerator->generate($cmsPage); $this->urlPersist->deleteByData([UrlRewrite::ENTITY_ID => $cmsPage->getId(), UrlRewrite::ENTITY_TYPE => CmsPageUrlRewriteGenerator::ENTITY_TYPE]); $this->urlPersist->replace($urls); } }
/** * 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 Product $product */ $product = $observer->getEvent()->getProduct(); if ($product->dataHasChangedFor('url_key') || $product->getIsChangedCategories() || $product->getIsChangedWebsites() || $product->dataHasChangedFor('visibility')) { $this->urlPersist->deleteByData([UrlRewrite::ENTITY_ID => $product->getId(), UrlRewrite::ENTITY_TYPE => ProductUrlRewriteGenerator::ENTITY_TYPE, UrlRewrite::REDIRECT_TYPE => 0, UrlRewrite::STORE_ID => $product->getStoreId()]); if ($product->isVisibleInSiteVisibility()) { $this->urlPersist->replace($this->productUrlRewriteGenerator->generate($product)); } } }
/** * 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 \Magento\Catalog\Model\Product $product */ $product = $observer->getEvent()->getProduct(); $isChangedWebsites = $product->getIsChangedWebsites(); if ($product->dataHasChangedFor('url_key') || $product->getIsChangedCategories() || $isChangedWebsites) { if ($isChangedWebsites) { $this->urlPersist->deleteByData([UrlRewrite::ENTITY_ID => $product->getId(), UrlRewrite::ENTITY_TYPE => ProductUrlRewriteGenerator::ENTITY_TYPE]); } $this->urlPersist->replace($this->productUrlRewriteGenerator->generate($product)); } }
/** * 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()->getCategory(); if ($category->getParentId() == Category::TREE_ROOT_ID) { return; } if ($category->dataHasChangedFor('url_key') || $category->getIsChangedProductList()) { $urlRewrites = array_merge($this->categoryUrlRewriteGenerator->generate($category), $this->urlRewriteHandler->generateProductUrlRewrites($category)); $this->urlPersist->replace($urlRewrites); } }
/** * @param \Magento\Framework\Event\Observer $observer * @return void */ public function execute(\Magento\Framework\Event\Observer $observer) { /** @var Category $category */ $category = $observer->getEvent()->getCategory(); if ($category->dataHasChangedFor('parent_id')) { $saveRewritesHistory = $this->scopeConfig->isSetFlag(UrlKeyRenderer::XML_PATH_SEO_SAVE_HISTORY, ScopeInterface::SCOPE_STORE, $category->getStoreId()); $category->setData('save_rewrites_history', $saveRewritesHistory); $urlRewrites = array_merge($this->categoryUrlRewriteGenerator->generate($category), $this->urlRewriteHandler->generateProductUrlRewrites($category)); $this->urlRewriteHandler->deleteCategoryRewritesForChildren($category); $this->urlPersist->replace($urlRewrites); } }
/** * @param \Magento\Store\Model\ResourceModel\Group $object * @param callable $proceed * @param AbstractModel $group * @return \Magento\Store\Model\ResourceModel\Group * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function aroundSave(\Magento\Store\Model\ResourceModel\Group $object, \Closure $proceed, AbstractModel $group) { $originGroup = $group; $result = $proceed($originGroup); if (!$group->isObjectNew() && ($group->dataHasChangedFor('website_id') || $group->dataHasChangedFor('root_category_id'))) { $this->storeManager->reinitStores(); foreach ($group->getStoreIds() as $storeId) { $this->urlPersist->deleteByData([UrlRewrite::STORE_ID => $storeId]); } $this->urlPersist->replace($this->generateCategoryUrls($group->getRootCategoryId(), $group->getStoreIds())); $this->urlPersist->replace($this->generateProductUrls($group->getWebsiteId(), $group->getOrigData('website_id'))); } return $result; }
/** * @param ImportProduct $import * @return void */ protected function clearProductUrls(ImportProduct $import) { $oldSku = $import->getOldSku(); while ($bunch = $import->getNextBunch()) { $idToDelete = []; foreach ($bunch as $rowNum => $rowData) { if ($import->validateRow($rowData, $rowNum) && ImportProduct::SCOPE_DEFAULT == $import->getRowScope($rowData)) { $idToDelete[] = $oldSku[$rowData[ImportProduct::COL_SKU]]['entity_id']; } } foreach ($idToDelete as $productId) { $this->urlPersist->deleteByData([UrlRewrite::ENTITY_ID => $productId, UrlRewrite::ENTITY_TYPE => ProductUrlRewriteGenerator::ENTITY_TYPE]); } } }
/** * Test for afterImportData() * Covers afterImportData() + protected methods used inside except related to generateUrls() ones. * generateUrls will be covered separately. * * @covers \Magento\CatalogUrlRewrite\Model\Product\Plugin\Import::afterImportData * @covers \Magento\CatalogUrlRewrite\Model\Product\Plugin\Import::_populateForUrlGeneration * @covers \Magento\CatalogUrlRewrite\Model\Product\Plugin\Import::isGlobalScope * @covers \Magento\CatalogUrlRewrite\Model\Product\Plugin\Import::populateGlobalProduct * @covers \Magento\CatalogUrlRewrite\Model\Product\Plugin\Import::addProductToImport * * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testAfterImportData() { $newSku = ['entity_id' => 'value']; $websiteId = 'websiteId value'; $productsCount = count($this->products); $websiteMock = $this->getMock('\\Magento\\Store\\Model\\Website', ['getStoreIds'], [], '', false); $storeIds = [1, Store::DEFAULT_STORE_ID]; $websiteMock->expects($this->once())->method('getStoreIds')->willReturn($storeIds); $this->storeManager->expects($this->once())->method('getWebsite')->with($websiteId)->willReturn($websiteMock); $this->importProduct->expects($this->exactly($productsCount))->method('getNewSku')->withConsecutive([$this->products[0][ImportProduct::COL_SKU]], [$this->products[1][ImportProduct::COL_SKU]])->willReturn($newSku); $this->importProduct->expects($this->exactly($productsCount))->method('getProductCategories')->withConsecutive([$this->products[0][ImportProduct::COL_SKU]], [$this->products[1][ImportProduct::COL_SKU]]); $getProductWebsitesCallsCount = $productsCount * 2; $this->importProduct->expects($this->exactly($getProductWebsitesCallsCount))->method('getProductWebsites')->willReturn([$newSku['entity_id'] => $websiteId]); $map = [[$this->products[0][ImportProduct::COL_STORE], $this->products[0][ImportProduct::COL_STORE]], [$this->products[1][ImportProduct::COL_STORE], $this->products[1][ImportProduct::COL_STORE]]]; $this->importProduct->expects($this->exactly(1))->method('getStoreIdByCode')->will($this->returnValueMap($map)); $product = $this->getMock('\\Magento\\Catalog\\Model\\Product', ['getId', 'setId', 'getSku', 'setStoreId', 'getStoreId'], [], '', false); $product->expects($this->exactly($productsCount))->method('setId')->with($newSku['entity_id']); $product->expects($this->any())->method('getId')->willReturn($newSku['entity_id']); $product->expects($this->exactly($productsCount))->method('getSku')->will($this->onConsecutiveCalls($this->products[0]['sku'], $this->products[1]['sku'])); $product->expects($this->exactly($productsCount))->method('getStoreId')->will($this->onConsecutiveCalls($this->products[0][ImportProduct::COL_STORE], $this->products[1][ImportProduct::COL_STORE])); $product->expects($this->once())->method('setStoreId')->with($this->products[1][ImportProduct::COL_STORE]); $this->catalogProductFactory->expects($this->exactly($productsCount))->method('create')->willReturn($product); $this->connection->expects($this->exactly(4))->method('quoteInto')->withConsecutive(['(store_id = ?', $storeIds[0]], [' AND entity_id = ?)', $newSku['entity_id']]); $productUrls = ['url 1', 'url 2']; $importMock = $this->getImportMock(['generateUrls', 'canonicalUrlRewriteGenerate', 'categoriesUrlRewriteGenerate', 'currentUrlRewritesRegenerate', 'cleanOverriddenUrlKey']); $importMock->expects($this->once())->method('generateUrls')->willReturn($productUrls); $this->urlPersist->expects($this->once())->method('replace')->with($productUrls); $importMock->afterImportData($this->observer); }
/** * Clear product urls. * * @param \Magento\Framework\Event\Observer $observer * @return void */ public function execute(\Magento\Framework\Event\Observer $observer) { if ($products = $observer->getEvent()->getBunch()) { $oldSku = $observer->getEvent()->getAdapter()->getOldSku(); $idToDelete = []; foreach ($products as $product) { if (!isset($oldSku[$product[ImportProduct::COL_SKU]])) { continue; } $productData = $oldSku[$product[ImportProduct::COL_SKU]]; $idToDelete[] = $productData['entity_id']; } if (!empty($idToDelete)) { $this->urlPersist->deleteByData([UrlRewrite::ENTITY_ID => $idToDelete, UrlRewrite::ENTITY_TYPE => ProductUrlRewriteGenerator::ENTITY_TYPE]); } } }
/** * @param Category $category * @return void */ public function deleteCategoryRewritesForChildren(Category $category) { $categoryIds = $this->childrenCategoriesProvider->getChildrenIds($category, true); $categoryIds[] = $category->getId(); foreach ($categoryIds as $categoryId) { $this->urlPersist->deleteByData([UrlRewrite::ENTITY_ID => $categoryId, UrlRewrite::ENTITY_TYPE => CategoryUrlRewriteGenerator::ENTITY_TYPE]); $this->urlPersist->deleteByData([UrlRewrite::METADATA => serialize(['category_id' => $categoryId]), UrlRewrite::ENTITY_TYPE => ProductUrlRewriteGenerator::ENTITY_TYPE]); } }
/** * @return void */ public function testExecuteWithoutDataChanged() { $this->pageMock->expects($this->any())->method('dataHasChangedFor')->willReturnMap([['identifier', false], ['store_id', false]]); $this->pageMock->expects($this->never())->method('getId'); $this->cmsPageUrlRewriteGeneratorMock->expects($this->never())->method('generate'); $this->urlPersistMock->expects($this->never())->method('deleteByData'); $this->urlPersistMock->expects($this->never())->method('replace'); $this->observer->execute($this->eventObserverMock); }
/** * @param bool $isChangedUrlKey * @param bool $isChangedVisibility * @param bool $isChangedWebsites * @param bool $isChangedCategories * @param bool $visibilityResult * @param int $expectedDeleteCount * @param int $expectedReplaceCount * * @dataProvider testUrlKeyDataProvider */ public function testExecuteUrlKey($isChangedUrlKey, $isChangedVisibility, $isChangedWebsites, $isChangedCategories, $visibilityResult, $expectedDeleteCount, $expectedReplaceCount) { $this->product->expects($this->any())->method('getStoreId')->will($this->returnValue(12)); $this->product->expects($this->any())->method('dataHasChangedFor')->will($this->returnValueMap([['visibility', $isChangedVisibility], ['url_key', $isChangedUrlKey]])); $this->product->expects($this->any())->method('getIsChangedWebsites')->will($this->returnValue($isChangedWebsites)); $this->product->expects($this->any())->method('getIsChangedCategories')->will($this->returnValue($isChangedCategories)); $this->urlPersist->expects($this->exactly($expectedDeleteCount))->method('deleteByData')->with([UrlRewrite::ENTITY_ID => $this->product->getId(), UrlRewrite::ENTITY_TYPE => ProductUrlRewriteGenerator::ENTITY_TYPE, UrlRewrite::REDIRECT_TYPE => 0, UrlRewrite::STORE_ID => $this->product->getStoreId()]); $this->product->expects($this->any())->method('isVisibleInSiteVisibility')->will($this->returnValue($visibilityResult)); $this->urlPersist->expects($this->exactly($expectedReplaceCount))->method('replace')->with([3 => 'rewrite']); $this->model->execute($this->observer); }
/** * Action after data import. * Save new url rewrites and remove old if exist. * * @param Observer $observer * * @return void */ public function execute(Observer $observer) { $this->import = $observer->getEvent()->getAdapter(); if ($products = $observer->getEvent()->getBunch()) { foreach ($products as $product) { $this->_populateForUrlGeneration($product); } $productUrls = $this->generateUrls(); if ($productUrls) { $this->urlPersist->replace($productUrls); } } }
/** * Test for afterImportData() * Covers afterImportData() + protected methods used inside * * @covers \Magento\CatalogUrlRewrite\Observer\AfterImportDataObserver::afterImportData * @covers \Magento\CatalogUrlRewrite\Observer\AfterImportDataObserver::_populateForUrlGeneration * @covers \Magento\CatalogUrlRewrite\Observer\AfterImportDataObserver::isGlobalScope * @covers \Magento\CatalogUrlRewrite\Observer\AfterImportDataObserver::populateGlobalProduct * @covers \Magento\CatalogUrlRewrite\Observer\AfterImportDataObserver::addProductToImport * * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testAfterImportData() { $newSku = [['entity_id' => 'value'], ['entity_id' => 'value3']]; $websiteId = 'websiteId value'; $productsCount = count($this->products); $websiteMock = $this->getMock('\\Magento\\Store\\Model\\Website', ['getStoreIds'], [], '', false); $storeIds = [1, Store::DEFAULT_STORE_ID]; $websiteMock->expects($this->once())->method('getStoreIds')->willReturn($storeIds); $this->storeManager->expects($this->once())->method('getWebsite')->with($websiteId)->willReturn($websiteMock); $this->importProduct->expects($this->exactly($productsCount))->method('getNewSku')->withConsecutive([$this->products[0][ImportProduct::COL_SKU]], [$this->products[1][ImportProduct::COL_SKU]])->will($this->onConsecutiveCalls($newSku[0], $newSku[1])); $this->importProduct->expects($this->exactly($productsCount))->method('getProductCategories')->withConsecutive([$this->products[0][ImportProduct::COL_SKU]], [$this->products[1][ImportProduct::COL_SKU]])->willReturn([]); $getProductWebsitesCallsCount = $productsCount * 2; $this->importProduct->expects($this->exactly($getProductWebsitesCallsCount))->method('getProductWebsites')->willReturnOnConsecutiveCalls([$newSku[0]['entity_id'] => $websiteId], [$newSku[0]['entity_id'] => $websiteId], [$newSku[1]['entity_id'] => $websiteId], [$newSku[1]['entity_id'] => $websiteId]); $map = [[$this->products[0][ImportProduct::COL_STORE], $this->products[0][ImportProduct::COL_STORE]], [$this->products[1][ImportProduct::COL_STORE], $this->products[1][ImportProduct::COL_STORE]]]; $this->importProduct->expects($this->exactly(1))->method('getStoreIdByCode')->will($this->returnValueMap($map)); $product = $this->getMock('\\Magento\\Catalog\\Model\\Product', ['getId', 'setId', 'getSku', 'setStoreId', 'getStoreId'], [], '', false); $product->expects($this->exactly($productsCount))->method('setId')->withConsecutive([$newSku[0]['entity_id']], [$newSku[1]['entity_id']]); $product->expects($this->any())->method('getId')->willReturnOnConsecutiveCalls($newSku[0]['entity_id'], $newSku[0]['entity_id'], $newSku[0]['entity_id'], $newSku[0]['entity_id'], $newSku[0]['entity_id'], $newSku[1]['entity_id'], $newSku[1]['entity_id'], $newSku[1]['entity_id']); $product->expects($this->exactly($productsCount))->method('getSku')->will($this->onConsecutiveCalls($this->products[0]['sku'], $this->products[1]['sku'])); $product->expects($this->exactly($productsCount))->method('getStoreId')->will($this->onConsecutiveCalls($this->products[0][ImportProduct::COL_STORE], $this->products[1][ImportProduct::COL_STORE])); $product->expects($this->exactly($productsCount))->method('setStoreId')->withConsecutive([$this->products[0][ImportProduct::COL_STORE]], [$this->products[1][ImportProduct::COL_STORE]]); $this->catalogProductFactory->expects($this->exactly($productsCount))->method('create')->willReturn($product); $this->connection->expects($this->exactly(4))->method('quoteInto')->withConsecutive(['(store_id = ?', $storeIds[0]], [' AND entity_id = ?)', $newSku[0]['entity_id']], ['(store_id = ?', $storeIds[0]], [' AND entity_id = ?)', $newSku[1]['entity_id']]); $this->connection->expects($this->once())->method('fetchAll')->willReturn([]); $this->select->expects($this->any())->method('from')->willReturnSelf(); $this->select->expects($this->any())->method('where')->willReturnSelf(); $this->urlFinder->expects($this->any())->method('findAllByData')->willReturn([]); $this->productUrlPathGenerator->expects($this->any())->method('getUrlPathWithSuffix')->willReturn('urlPathWithSuffix'); $this->productUrlPathGenerator->expects($this->any())->method('getUrlPath')->willReturn('urlPath'); $this->productUrlPathGenerator->expects($this->any())->method('getCanonicalUrlPath')->willReturn('canonicalUrlPath'); $this->urlRewrite->expects($this->any())->method('setStoreId')->willReturnSelf(); $this->urlRewrite->expects($this->any())->method('setEntityId')->willReturnSelf(); $this->urlRewrite->expects($this->any())->method('setEntityType')->willReturnSelf(); $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('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]; $this->urlPersist->expects($this->once())->method('replace')->with($productUrls); $this->import->execute($this->observer); }
/** * @param \Magento\Store\Model\ResourceModel\Store $object * @param callable $proceed * @param AbstractModel $store * @return mixed * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function aroundDelete(\Magento\Store\Model\ResourceModel\Store $object, \Closure $proceed, AbstractModel $store) { $result = $proceed($store); $this->urlPersist->deleteByData([UrlRewrite::STORE_ID => $store->getId()]); return $result; }
public function testAroundDeleteNegative() { $this->cmsPageMock->expects($this->once())->method('isDeleted')->willReturn(false); $this->urlPersistMock->expects($this->never())->method('deleteByData'); $this->assertEquals('URL Rewrite Result', $this->pageObject->aroundDelete($this->cmsPageResourceMock, $this->closureMock, $this->cmsPageMock)); }
/** * Remove url rewrites by categoryId * * @param int $categoryId * @return void */ protected function deleteRewritesForCategory($categoryId) { $this->urlPersist->deleteByData([UrlRewrite::ENTITY_ID => $categoryId, UrlRewrite::ENTITY_TYPE => CategoryUrlRewriteGenerator::ENTITY_TYPE]); $this->urlPersist->deleteByData([UrlRewrite::METADATA => serialize(['category_id' => $categoryId]), UrlRewrite::ENTITY_TYPE => ProductUrlRewriteGenerator::ENTITY_TYPE]); }