示例#1
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));
 }
 /**
  * Generate list of urls for specific store view
  *
  * @param int $storeId
  * @param \Magento\Framework\Data\Collection $productCategories
  * @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[]
  */
 protected function generateForSpecificStoreView($storeId, $productCategories)
 {
     $categories = [];
     foreach ($productCategories as $category) {
         if ($this->isCategoryProperForGenerating($category, $storeId)) {
             $categories[] = $category;
         }
     }
     $this->productCategories = $this->objectRegistryFactory->create(['entities' => $categories]);
     /**
      * @var $urls \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[]
      */
     $urls = array_merge($this->canonicalUrlRewriteGenerator->generate($storeId, $this->product), $this->categoriesUrlRewriteGenerator->generate($storeId, $this->product, $this->productCategories), $this->currentUrlRewritesRegenerator->generate($storeId, $this->product, $this->productCategories));
     /* Reduce duplicates. Last wins */
     $result = [];
     foreach ($urls as $url) {
         $result[$url->getTargetPath() . '-' . $url->getStoreId()] = $url;
     }
     $this->productCategories = null;
     return $result;
 }