/**
  * 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 testIsAutogeneratedWithCategory()
 {
     $productId = 12;
     $requestPath = 'autogenerated.html';
     $targetPath = 'simple-product.html';
     $storeId = 2;
     $metadata = ['category_id' => 2, 'some_another_data' => 1];
     $description = 'description';
     $this->urlFinder->expects($this->once())->method('findAllByData')->will($this->returnValue($this->getCurrentRewritesMocks([[UrlRewrite::REQUEST_PATH => $requestPath, UrlRewrite::TARGET_PATH => 'some-path.html', UrlRewrite::STORE_ID => $storeId, UrlRewrite::IS_AUTOGENERATED => 1, UrlRewrite::METADATA => $metadata, UrlRewrite::DESCRIPTION => $description]])));
     $this->product->expects($this->any())->method('getId')->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')->will($this->returnValue($targetPath));
     $this->objectRegistry->expects($this->once())->method('get')->will($this->returnValue($this->category));
     $this->prepareUrlRewriteMock($storeId, $productId, $requestPath, $targetPath, OptionProvider::PERMANENT, $metadata, $description);
     $this->assertEquals([$this->urlRewrite], $this->currentUrlRewritesRegenerator->generate($storeId, $this->product, $this->objectRegistry));
 }
Example #3
0
 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));
 }
 /**
  * @param UrlRewrite $url
  * @return Category|null|bool
  */
 protected function retrieveCategoryFromMetadata($url)
 {
     $metadata = $url->getMetadata();
     if (isset($metadata['category_id'])) {
         $category = $this->productCategories->get($metadata['category_id']);
         return $category === null ? false : $category;
     }
     return null;
 }
Example #5
0
 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 testGetList()
 {
     $this->assertEquals([1 => $this->object], $this->objectRegistry->getList());
 }