/** * Generate list based on categories * * @param int $storeId * @param Product $product * @param ObjectRegistry $productCategories * @return UrlRewrite[] */ public function generate($storeId, Product $product, ObjectRegistry $productCategories) { $urls = []; foreach ($productCategories->getList() as $category) { $urls[] = $this->urlRewriteFactory->create()->setEntityType(ProductUrlRewriteGenerator::ENTITY_TYPE)->setEntityId($product->getId())->setRequestPath($this->productUrlPathGenerator->getUrlPathWithSuffix($product, $storeId, $category))->setTargetPath($this->productUrlPathGenerator->getCanonicalUrlPath($product, $category))->setStoreId($storeId)->setMetadata(['category_id' => $category->getId()]); } return $urls; }
public function testGetUrlPath() { /** @var $product \Magento\Catalog\Model\Product */ $product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Product'); $product->setUrlPath('product.html'); /** @var $category \Magento\Catalog\Model\Category */ $category = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Category', ['data' => ['url_path' => 'category', 'entity_id' => 5, 'path_ids' => [2, 3, 5]]]); $category->setOrigData(); $this->assertEquals('product.html', $this->urlPathGenerator->getUrlPath($product)); $this->assertEquals('category/product.html', $this->urlPathGenerator->getUrlPath($product, $category)); }
/** * @param UrlRewrite $url * @param Category $category * @return array */ protected function generateForCustom($url, $category) { $storeId = $url->getStoreId(); $productId = $url->getEntityId(); if (isset($this->products[$productId][$storeId])) { $product = $this->products[$productId][$storeId]; $targetPath = $url->getRedirectType() ? $this->productUrlPathGenerator->getUrlPathWithSuffix($product, $storeId, $category) : $url->getTargetPath(); if ($url->getRequestPath() === $targetPath) { return []; } return [ $this->urlRewriteFactory->create() ->setEntityType(ProductUrlRewriteGenerator::ENTITY_TYPE) ->setEntityId($productId) ->setRequestPath($url->getRequestPath()) ->setTargetPath($targetPath) ->setRedirectType($url->getRedirectType()) ->setStoreId($storeId) ->setDescription($url->getDescription()) ->setIsAutogenerated(0) ->setMetadata($url->getMetadata()) ]; } return []; }
/** * Cover categoriesUrlRewriteGenerate(). */ public function testCategoriesUrlRewriteGenerate() { $urlPathWithCategory = 'category/simple-product.html'; $storeId = 10; $productId = 'product_id'; $canonicalUrlPathWithCategory = 'canonical-path-with-category'; $product = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->disableOriginalConstructor()->getMock(); $productsByStores = [$storeId => $product]; $products = [$productId => $productsByStores]; $categoryCache = [$productId => [$this->categoryId]]; $this->setPropertyValue($this->import, 'products', $products); $this->setPropertyValue($this->import, 'categoryCache', $categoryCache); $this->setPropertyValue($this->import, 'import', $this->importProduct); $this->productUrlPathGenerator->expects($this->any())->method('getUrlPathWithSuffix')->will($this->returnValue($urlPathWithCategory)); $this->productUrlPathGenerator->expects($this->any())->method('getCanonicalUrlPath')->will($this->returnValue($canonicalUrlPathWithCategory)); $category = $this->getMock('Magento\\Catalog\\Model\\Category', [], [], '', false); $category->expects($this->any())->method('getId')->will($this->returnValue($this->categoryId)); $this->urlRewrite->expects($this->any())->method('setStoreId')->with($storeId)->will($this->returnSelf()); $this->urlRewrite->expects($this->any())->method('setEntityId')->with($productId)->will($this->returnSelf()); $this->urlRewrite->expects($this->any())->method('setEntityType')->with(ProductUrlRewriteGenerator::ENTITY_TYPE)->will($this->returnSelf()); $this->urlRewrite->expects($this->any())->method('setRequestPath')->with($urlPathWithCategory)->will($this->returnSelf()); $this->urlRewrite->expects($this->any())->method('setTargetPath')->with($canonicalUrlPathWithCategory)->will($this->returnSelf()); $this->urlRewrite->expects($this->any())->method('setMetadata')->with(['category_id' => $this->categoryId])->will($this->returnSelf()); $this->urlRewriteFactory->expects($this->any())->method('create')->willReturn($this->urlRewrite); $actualResult = $this->invokeMethod($this->import, 'categoriesUrlRewriteGenerate'); $this->assertEquals([$this->urlRewrite], $actualResult); }
/** * @return string */ protected function getCanonicalTargetPath() { $product = $this->_getProduct()->getId() ? $this->_getProduct() : null; $category = $this->_getCategory()->getId() ? $this->_getCategory() : null; return $product ? $this->productUrlPathGenerator->getCanonicalUrlPath($product, $category) : $this->categoryUrlPathGenerator->getCanonicalUrlPath($category); }
public function testGenerate() { $requestPath = 'simple-product.html'; $storeId = 10; $productId = 'product_id'; $targetPath = 'catalog/product/view/id/' . $productId; $this->product->expects($this->any())->method('getId')->will($this->returnValue($productId)); $this->productUrlPathGenerator->expects($this->any())->method('getUrlPathWithSuffix')->will($this->returnValue($requestPath)); $this->productUrlPathGenerator->expects($this->any())->method('getCanonicalUrlPath')->will($this->returnValue($targetPath)); $this->categoryRegistry->expects($this->any())->method('getList')->will($this->returnValue([])); $this->urlRewriteBuilder->expects($this->any())->method('setStoreId')->with($storeId)->will($this->returnSelf()); $this->urlRewriteBuilder->expects($this->any())->method('setEntityId')->with($productId)->will($this->returnSelf()); $this->urlRewriteBuilder->expects($this->any())->method('setEntityType')->with(ProductUrlRewriteGenerator::ENTITY_TYPE)->will($this->returnSelf()); $this->urlRewriteBuilder->expects($this->any())->method('setRequestPath')->with($requestPath)->will($this->returnSelf()); $this->urlRewriteBuilder->expects($this->any())->method('setTargetPath')->with($targetPath)->will($this->returnSelf()); $this->urlRewriteBuilder->expects($this->any())->method('create')->will($this->returnValue($this->urlRewrite)); $this->assertEquals([$this->urlRewrite], $this->canonicalUrlRewriteGenerator->generate($storeId, $this->product)); }
public function testGenerateCategories() { $urlPathWithCategory = 'category/simple-product.html'; $storeId = 10; $productId = 'product_id'; $canonicalUrlPathWithCategory = 'canonical-path-with-category'; $categoryId = 'category_id'; $this->product->expects($this->any())->method('getId')->will($this->returnValue($productId)); $this->productUrlPathGenerator->expects($this->any())->method('getUrlPathWithSuffix')->will($this->returnValue($urlPathWithCategory)); $this->productUrlPathGenerator->expects($this->any())->method('getCanonicalUrlPath')->will($this->returnValue($canonicalUrlPathWithCategory)); $category = $this->getMock('Magento\\Catalog\\Model\\Category', [], [], '', false); $category->expects($this->any())->method('getId')->will($this->returnValue($categoryId)); $this->categoryRegistry->expects($this->any())->method('getList')->will($this->returnValue([$category])); $this->urlRewriteBuilder->expects($this->any())->method('setStoreId')->with($storeId)->will($this->returnSelf()); $this->urlRewriteBuilder->expects($this->any())->method('setEntityId')->with($productId)->will($this->returnSelf()); $this->urlRewriteBuilder->expects($this->any())->method('setEntityType')->with(ProductUrlRewriteGenerator::ENTITY_TYPE)->will($this->returnSelf()); $this->urlRewriteBuilder->expects($this->any())->method('setRequestPath')->with($urlPathWithCategory)->will($this->returnSelf()); $this->urlRewriteBuilder->expects($this->any())->method('setTargetPath')->with($canonicalUrlPathWithCategory)->will($this->returnSelf()); $this->urlRewriteBuilder->expects($this->any())->method('setMetadata')->with(['category_id' => $categoryId])->will($this->returnSelf()); $this->urlRewriteBuilder->expects($this->any())->method('create')->will($this->returnValue($this->urlRewrite)); $this->assertEquals([$this->urlRewrite], $this->categoriesUrlRewriteGenerator->generate($storeId, $this->product, $this->categoryRegistry)); }
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)); }
/** * 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); }
/** * @param \Magento\Framework\Event\Observer $observer * @return void */ public function execute(\Magento\Framework\Event\Observer $observer) { /** @var Product $product */ $product = $observer->getEvent()->getProduct(); $product->setUrlKey($this->productUrlPathGenerator->getUrlKey($product)); }
public function testGetCanonicalUrlPathWithCategory() { $this->product->expects($this->once())->method('getId')->will($this->returnValue(1)); $this->category->expects($this->once())->method('getId')->will($this->returnValue(1)); $this->assertEquals('catalog/product/view/id/1/category/1', $this->productUrlPathGenerator->getCanonicalUrlPath($this->product, $this->category)); }
/** * @param \Magento\Catalog\Model\Product|null $product * @param \Magento\Catalog\Model\Category|null $category * @return string */ protected function getTargetPath($product = null, $category = null) { return $product ? $this->productUrlPathGenerator->getCanonicalUrlPath($product, $category) : $this->categoryUrlPathGenerator->getCanonicalUrlPath($category); }
/** * Generate list based on store view * * @param int $storeId * @param Product $product * @return UrlRewrite[] */ public function generate($storeId, Product $product) { return [$this->urlRewriteFactory->create()->setEntityType(ProductUrlRewriteGenerator::ENTITY_TYPE)->setEntityId($product->getId())->setRequestPath($this->productUrlPathGenerator->getUrlPathWithSuffix($product, $storeId))->setTargetPath($this->productUrlPathGenerator->getCanonicalUrlPath($product))->setStoreId($storeId)]; }
/** * @param Observer $observer * @return void */ public function invoke(Observer $observer) { /** @var Product $product */ $product = $observer->getEvent()->getProduct(); $product->setUrlKey($this->productUrlPathGenerator->generateUrlKey($product)); }