/** * @dataProvider getUrlDataProvider * @covers \Magento\Catalog\Model\Product\Url::getUrl * @covers \Magento\Catalog\Model\Product\Url::getUrlInStore * @covers \Magento\Catalog\Model\Product\Url::getProductUrl * * @param $getUrlMethod * @param $routePath * @param $requestPathProduct * @param $storeId * @param $categoryId * @param $routeParams * @param $routeParamsUrl * @param $productId * @param $productUrlKey * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function testGetUrl($getUrlMethod, $routePath, $requestPathProduct, $storeId, $categoryId, $routeParams, $routeParamsUrl, $productId, $productUrlKey) { $product = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->disableOriginalConstructor()->setMethods(['getStoreId', 'getEntityId', 'getId', 'getUrlKey', 'setRequestPath', 'hasUrlDataObject', 'getRequestPath', 'getCategoryId', 'getDoNotUseCategoryId', '__wakeup'])->getMock(); $product->expects($this->any())->method('getStoreId')->will($this->returnValue($storeId)); $product->expects($this->any())->method('getCategoryId')->will($this->returnValue($categoryId)); $product->expects($this->any())->method('getRequestPath')->will($this->returnValue($requestPathProduct)); $product->expects($this->any())->method('setRequestPath')->with(false)->will($this->returnSelf()); $product->expects($this->any())->method('getId')->will($this->returnValue($productId)); $product->expects($this->any())->method('getUrlKey')->will($this->returnValue($productUrlKey)); $this->url->expects($this->any())->method('setScope')->with($storeId)->will($this->returnSelf()); $this->url->expects($this->any())->method('getUrl')->with($routePath, $routeParamsUrl)->will($this->returnValue($requestPathProduct)); $this->urlFinder->expects($this->any())->method('findOneByData')->will($this->returnValue(false)); switch ($getUrlMethod) { case 'getUrl': $this->assertEquals($requestPathProduct, $this->model->getUrl($product, $routeParams)); break; case 'getUrlInStore': $this->assertEquals($requestPathProduct, $this->model->getUrlInStore($product, $routeParams)); break; case 'getProductUrl': $this->assertEquals($requestPathProduct, $this->model->getProductUrl($product, true)); $this->sidResolver->expects($this->once())->method('getUseSessionInUrl')->will($this->returnValue(true)); $this->assertEquals($requestPathProduct, $this->model->getProductUrl($product, null)); break; } }
/** * Set redirect url for store view based on request path info * * @param \Magento\Store\Block\Switcher $switcher * @param \Magento\Store\Model\Store $store * @param array $data * @return array * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function beforeGetTargetStorePostData(\Magento\Store\Block\Switcher $switcher, \Magento\Store\Model\Store $store, $data = []) { $urlRewrite = $this->urlFinder->findOneByData([UrlRewrite::TARGET_PATH => $this->trimSlashInPath($this->request->getPathInfo()), UrlRewrite::STORE_ID => $store->getId()]); if ($urlRewrite) { $data[ActionInterface::PARAM_NAME_URL_ENCODED] = $this->urlHelper->getEncodedUrl($this->trimSlashInPath($this->urlBuilder->getUrl($urlRewrite->getRequestPath()))); } return [$store, $data]; }
/** * Generate list based on current url rewrites * * @param int $storeId * @param \Magento\Catalog\Model\Category $category * @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] */ public function generate($storeId, Category $category) { $this->category = $category; $currentUrlRewrites = $this->urlFinder->findAllByData([UrlRewrite::STORE_ID => $storeId, UrlRewrite::ENTITY_ID => $category->getId(), UrlRewrite::ENTITY_TYPE => CategoryUrlRewriteGenerator::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)); } } return $urlRewrites; }
/** * Get Target Path * * @param \Magento\UrlRewrite\Model\UrlRewrite $model * @return string * @throws \Magento\Framework\Exception\LocalizedException */ protected function getTargetPath($model) { $targetPath = $this->getCanonicalTargetPath(); if ($model->getRedirectType() && !$model->getIsAutogenerated()) { $data = [UrlRewrite::ENTITY_ID => $model->getEntityId(), UrlRewrite::TARGET_PATH => $targetPath, UrlRewrite::ENTITY_TYPE => $model->getEntityType(), UrlRewrite::STORE_ID => $model->getStoreId()]; $rewrite = $this->urlFinder->findOneByData($data); if (!$rewrite) { $message = $model->getEntityType() === self::ENTITY_TYPE_PRODUCT ? __('The product you chose is not associated with the selected store or category.') : __('The category you chose is not associated with the selected store.'); throw new LocalizedException($message); } $targetPath = $rewrite->getRequestPath(); } return $targetPath; }
/** * Generate list based on current rewrites * * @param int $storeId * @param Product $product * @param ObjectRegistry $productCategories * @return UrlRewrite[] */ public function generate($storeId, Product $product, ObjectRegistry $productCategories) { $this->product = $product; $this->productCategories = $productCategories; $currentUrlRewrites = $this->urlFinder->findAllByData([UrlRewrite::STORE_ID => $storeId, UrlRewrite::ENTITY_ID => $this->product->getId(), UrlRewrite::ENTITY_TYPE => ProductUrlRewriteGenerator::ENTITY_TYPE]); $urlRewrites = []; foreach ($currentUrlRewrites as $currentUrlRewrite) { $category = $this->retrieveCategoryFromMetadata($currentUrlRewrite); if ($category === false) { continue; } $url = $currentUrlRewrite->getIsAutogenerated() ? $this->generateForAutogenerated($currentUrlRewrite, $storeId, $category) : $this->generateForCustom($currentUrlRewrite, $storeId, $category); $urlRewrites = array_merge($urlRewrites, $url); } $this->product = null; $this->productCategories = null; return $urlRewrites; }
public function testGetHrefWithForProductWithCategoryIdParameter() { $storeId = 15; $this->block->setData('id_path', ProductUrlRewriteGenerator::ENTITY_TYPE . '/entity_id/category_id'); $store = $this->getMock('Magento\\Store\\Model\\Store', ['getId', '__wakeUp'], [], '', false); $store->expects($this->any())->method('getId')->will($this->returnValue($storeId)); $this->storeManager->expects($this->any())->method('getStore')->will($this->returnValue($store)); $this->urlFinder->expects($this->once())->method('findOneByData')->with([UrlRewrite::ENTITY_ID => 'entity_id', UrlRewrite::ENTITY_TYPE => ProductUrlRewriteGenerator::ENTITY_TYPE, UrlRewrite::STORE_ID => $storeId, UrlRewrite::METADATA => ['category_id' => 'category_id']])->will($this->returnValue(false)); $this->block->getHref(); }
public function testMatch() { $this->storeManager->expects($this->any())->method('getStore')->will($this->returnValue($this->store)); $urlRewrite = $this->getMockBuilder('Magento\\UrlRewrite\\Service\\V1\\Data\\UrlRewrite')->disableOriginalConstructor()->getMock(); $urlRewrite->expects($this->any())->method('getRedirectType')->will($this->returnValue(0)); $urlRewrite->expects($this->any())->method('getTargetPath')->will($this->returnValue('target-path')); $this->urlFinder->expects($this->any())->method('findOneByData')->will($this->returnValue($urlRewrite)); $this->request->expects($this->once())->method('setPathInfo')->with('/target-path'); $this->actionFactory->expects($this->once())->method('create')->with('Magento\\Framework\\App\\Action\\Forward', ['request' => $this->request]); $this->router->match($this->request); }
/** * Retrieve Product URL using UrlDataObject * * @param \Magento\Catalog\Model\Product $product * @param array $params * @return string * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function getUrl(\Magento\Catalog\Model\Product $product, $params = []) { $routePath = ''; $routeParams = $params; $storeId = $product->getStoreId(); if (isset($params['_ignore_category'])) { unset($params['_ignore_category']); $categoryId = null; } else { $categoryId = $product->getCategoryId() && !$product->getDoNotUseCategoryId() ? $product->getCategoryId() : null; } if ($product->hasUrlDataObject()) { $requestPath = $product->getUrlDataObject()->getUrlRewrite(); $routeParams['_scope'] = $product->getUrlDataObject()->getStoreId(); } else { $requestPath = $product->getRequestPath(); if (empty($requestPath) && $requestPath !== false) { $filterData = [UrlRewrite::ENTITY_ID => $product->getId(), UrlRewrite::ENTITY_TYPE => \Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator::ENTITY_TYPE, UrlRewrite::STORE_ID => $storeId]; if ($categoryId) { $filterData[UrlRewrite::METADATA]['category_id'] = $categoryId; } $rewrite = $this->urlFinder->findOneByData($filterData); if ($rewrite) { $requestPath = $rewrite->getRequestPath(); $product->setRequestPath($requestPath); } else { $product->setRequestPath(false); } } } if (isset($routeParams['_scope'])) { $storeId = $this->_storeManager->getStore($routeParams['_scope'])->getId(); } if ($storeId != $this->_storeManager->getStore()->getId()) { $routeParams['_scope_to_url'] = true; } if (!empty($requestPath)) { $routeParams['_direct'] = $requestPath; } else { $routePath = 'catalog/product/view'; $routeParams['id'] = $product->getId(); $routeParams['s'] = $product->getUrlKey(); if ($categoryId) { $routeParams['category'] = $categoryId; } } // reset cached URL instance GET query params if (!isset($routeParams['_query'])) { $routeParams['_query'] = []; } return $this->getUrlInstance()->setScope($storeId)->getUrl($routePath, $routeParams); }
public function testGenerationForCustomWithTargetPathGeneration() { $storeId = 12; $productId = 123; $requestPath = 'generate-for-custom-without-redirect-type.html'; $targetPath = 'generated-target-path.html'; $description = 'description'; $this->urlFinder->expects($this->once())->method('findAllByData')->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::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->assertEquals([$this->urlRewrite], $this->currentUrlRewritesRegenerator->generate($storeId, $this->product, $this->objectRegistry)); }
/** * Prepare url using passed id path and return it * or return false if path was not found in url rewrites. * * @throws \RuntimeException * @return string|false * @SuppressWarnings(PHPMD.NPathComplexity) */ public function getHref() { if ($this->_href === null) { if (!$this->getData('id_path')) { throw new \RuntimeException('Parameter id_path is not set.'); } $rewriteData = $this->parseIdPath($this->getData('id_path')); $href = false; $store = $this->hasStoreId() ? $this->_storeManager->getStore($this->getStoreId()) : $this->_storeManager->getStore(); $filterData = [UrlRewrite::ENTITY_ID => $rewriteData[1], UrlRewrite::ENTITY_TYPE => $rewriteData[0], UrlRewrite::STORE_ID => $store->getId()]; if (!empty($rewriteData[2]) && $rewriteData[0] == ProductUrlRewriteGenerator::ENTITY_TYPE) { $filterData[UrlRewrite::METADATA]['category_id'] = $rewriteData[2]; } $rewrite = $this->urlFinder->findOneByData($filterData); if ($rewrite) { $href = $store->getUrl('', ['_direct' => $rewrite->getRequestPath()]); if (strpos($href, '___store') === false) { $href .= (strpos($href, '?') === false ? '?' : '&') . '___store=' . $store->getCode(); } } $this->_href = $href; } return $this->_href; }
/** * Cover currentUrlRewritesRegenerate(). * * @cover \Magento\CatalogUrlRewrite\Model\Product\Plugin\Import::generateForCustom */ public function testCurrentUrlRewritesRegenerateForCustomWithTargetPathGeneration() { $productId = 'entity id value'; $storeId = 'store id value'; $products = [$productId => [$storeId => 'value']]; $this->setPropertyValue($this->import, 'products', $products); $requestPath = 'generate-for-custom-without-redirect-type.html'; $targetPath = 'generated-target-path.html'; $description = 'description'; $this->urlFinder->expects($this->once())->method('findAllByData')->will($this->returnValue($this->currentUrlRewritesRegeneratorGetCurrentRewritesMocks([[UrlRewrite::REQUEST_PATH => $requestPath, UrlRewrite::TARGET_PATH => 'custom-target-path.html', UrlRewrite::REDIRECT_TYPE => 'code', UrlRewrite::IS_AUTOGENERATED => 0, UrlRewrite::DESCRIPTION => $description, UrlRewrite::METADATA => [], UrlRewrite::ENTITY_ID => $productId, UrlRewrite::STORE_ID => $storeId]]))); $this->productUrlPathGenerator->expects($this->any())->method('getUrlPathWithSuffix')->will($this->returnValue($targetPath)); $this->product->expects($this->any())->method('getId')->will($this->returnValue($productId)); $this->currentUrlRewritesRegeneratorPrepareUrlRewriteMock($storeId, $productId, $requestPath, $targetPath, 'code', [], $description); $actualResult = $this->import->currentUrlRewritesRegenerate(); $this->assertEquals([[$this->urlRewrite]], $actualResult); }
/** * Generate list based on current rewrites * * @return UrlRewrite[] */ protected function currentUrlRewritesRegenerate() { $currentUrlRewrites = $this->urlFinder->findAllByData([UrlRewrite::STORE_ID => array_keys($this->storesCache), UrlRewrite::ENTITY_ID => array_keys($this->products), UrlRewrite::ENTITY_TYPE => ProductUrlRewriteGenerator::ENTITY_TYPE]); $urlRewrites = []; foreach ($currentUrlRewrites as $currentUrlRewrite) { $category = $this->retrieveCategoryFromMetadata($currentUrlRewrite); if ($category === false) { continue; } $url = $currentUrlRewrite->getIsAutogenerated() ? $this->generateForAutogenerated($currentUrlRewrite, $category) : $this->generateForCustom($currentUrlRewrite, $category); $urlRewrites = array_merge($urlRewrites, $url); } $this->product = null; $this->productCategories = null; return $urlRewrites; }
/** * 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\UrlRewrite\Service\V1\Data\UrlRewrite[] $urls * @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] */ protected function filterUrls(array $urls) { $filteredUrls = []; /** @var UrlRewrite $url */ foreach ($urls as $url) { if ($this->isCorrectUrl($url)) { $filteredUrls[] = $url; } } $data = []; foreach ($filteredUrls as $url) { foreach ([UrlRewrite::REQUEST_PATH, UrlRewrite::STORE_ID] as $key) { $fieldValue = $url->getByKey($key); if (!isset($data[$key]) || !in_array($fieldValue, $data[$key])) { $data[$key][] = $fieldValue; } } } return $data ? $this->urlFinder->findAllByData($data) : []; }
/** * Update suffix for url rewrites * * @return $this */ protected function updateSuffixForUrlRewrites() { $map = [ProductUrlPathGenerator::XML_PATH_PRODUCT_URL_SUFFIX => ProductUrlRewriteGenerator::ENTITY_TYPE, CategoryUrlPathGenerator::XML_PATH_CATEGORY_URL_SUFFIX => CategoryUrlRewriteGenerator::ENTITY_TYPE]; if (!isset($map[$this->getPath()])) { return $this; } $dataFilter = [UrlRewrite::ENTITY_TYPE => $map[$this->getPath()]]; $storesIds = $this->getStoreIds(); if ($storesIds) { $dataFilter[UrlRewrite::STORE_ID] = $storesIds; } $entities = $this->urlFinder->findAllByData($dataFilter); $oldSuffixPattern = '~' . preg_quote($this->getOldValue()) . '$~'; $suffix = $this->getValue(); foreach ($entities as $urlRewrite) { $bind = $urlRewrite->getIsAutogenerated() ? [UrlRewrite::REQUEST_PATH => preg_replace($oldSuffixPattern, $suffix, $urlRewrite->getRequestPath())] : [UrlRewrite::TARGET_PATH => preg_replace($oldSuffixPattern, $suffix, $urlRewrite->getTargetPath())]; $this->connection->update(DbStorage::TABLE_NAME, $bind, $this->connection->quoteIdentifier(UrlRewrite::URL_REWRITE_ID) . ' = ' . $urlRewrite->getUrlRewriteId()); } return $this; }
/** * Get category url * * @return string */ public function getUrl() { $url = $this->_getData('url'); if ($url === null) { Profiler::start('REWRITE: ' . __METHOD__, ['group' => 'REWRITE', 'method' => __METHOD__]); if ($this->hasData('request_path') && $this->getRequestPath() != '') { $this->setData('url', $this->getUrlInstance()->getDirectUrl($this->getRequestPath())); Profiler::stop('REWRITE: ' . __METHOD__); return $this->getData('url'); } $rewrite = $this->urlFinder->findOneByData([UrlRewrite::ENTITY_ID => $this->getId(), UrlRewrite::ENTITY_TYPE => CategoryUrlRewriteGenerator::ENTITY_TYPE, UrlRewrite::STORE_ID => $this->getStoreId()]); if ($rewrite) { $this->setData('url', $this->getUrlInstance()->getDirectUrl($rewrite->getRequestPath())); Profiler::stop('REWRITE: ' . __METHOD__); return $this->getData('url'); } $this->setData('url', $this->getCategoryIdUrl()); Profiler::stop('REWRITE: ' . __METHOD__); return $this->getData('url'); } return $url; }
/** * @param string $requestPath * @param int $storeId * @return UrlRewrite|null */ protected function getRewrite($requestPath, $storeId) { return $this->urlFinder->findOneByData([UrlRewrite::REQUEST_PATH => trim($requestPath, '/'), UrlRewrite::STORE_ID => $storeId]); }