Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProductScopeRewriteGeneratorTest.php
Go to the documentation of this file.
1 <?php
7 
12 
18 class ProductScopeRewriteGeneratorTest extends \PHPUnit\Framework\TestCase
19 {
21  private $canonicalUrlRewriteGenerator;
22 
24  private $currentUrlRewritesRegenerator;
25 
27  private $categoriesUrlRewriteGenerator;
28 
30  private $anchorUrlRewriteGenerator;
31 
33  private $storeViewService;
34 
36  private $objectRegistryFactory;
37 
39  private $storeManager;
40 
42  private $productScopeGenerator;
43 
45  private $mergeDataProvider;
46 
48  private $serializer;
49 
51  private $categoryMock;
52 
53  public function setUp()
54  {
55  $this->serializer = $this->createMock(\Magento\Framework\Serialize\Serializer\Json::class);
56  $this->serializer->expects($this->any())
57  ->method('serialize')
58  ->willReturnCallback(
59  function ($value) {
60  return json_encode($value);
61  }
62  );
63  $this->serializer->expects($this->any())
64  ->method('unserialize')
65  ->willReturnCallback(
66  function ($value) {
67  return json_decode($value, true);
68  }
69  );
70 
71  $this->currentUrlRewritesRegenerator = $this->getMockBuilder(
72  \Magento\CatalogUrlRewrite\Model\Product\CurrentUrlRewritesRegenerator::class
73  )->disableOriginalConstructor()->getMock();
74  $this->canonicalUrlRewriteGenerator = $this->getMockBuilder(
75  \Magento\CatalogUrlRewrite\Model\Product\CanonicalUrlRewriteGenerator::class
76  )->disableOriginalConstructor()->getMock();
77  $this->categoriesUrlRewriteGenerator = $this->getMockBuilder(
78  \Magento\CatalogUrlRewrite\Model\Product\CategoriesUrlRewriteGenerator::class
79  )->disableOriginalConstructor()->getMock();
80  $this->anchorUrlRewriteGenerator = $this->getMockBuilder(
81  \Magento\CatalogUrlRewrite\Model\Product\AnchorUrlRewriteGenerator::class
82  )->disableOriginalConstructor()->getMock();
83  $this->objectRegistryFactory = $this->getMockBuilder(
84  \Magento\CatalogUrlRewrite\Model\ObjectRegistryFactory::class
85  )->disableOriginalConstructor()->setMethods(['create'])->getMock();
86  $this->storeViewService = $this->getMockBuilder(\Magento\CatalogUrlRewrite\Service\V1\StoreViewService::class)
87  ->disableOriginalConstructor()->getMock();
88  $this->storeManager = $this->createMock(StoreManagerInterface::class);
89  $storeRootCategoryId = 2;
90  $store = $this->getMockBuilder(\Magento\Store\Model\Store::class)->disableOriginalConstructor()->getMock();
91  $store->expects($this->any())->method('getRootCategoryId')->will($this->returnValue($storeRootCategoryId));
92  $this->storeManager->expects($this->any())->method('getStore')->will($this->returnValue($store));
93  $mergeDataProviderFactory = $this->createPartialMock(
94  \Magento\UrlRewrite\Model\MergeDataProviderFactory::class,
95  ['create']
96  );
97  $this->mergeDataProvider = new \Magento\UrlRewrite\Model\MergeDataProvider();
98  $mergeDataProviderFactory->expects($this->once())->method('create')->willReturn($this->mergeDataProvider);
99 
100  $this->productScopeGenerator = (new ObjectManager($this))->getObject(
101  \Magento\CatalogUrlRewrite\Model\ProductScopeRewriteGenerator::class,
102  [
103  'canonicalUrlRewriteGenerator' => $this->canonicalUrlRewriteGenerator,
104  'categoriesUrlRewriteGenerator' => $this->categoriesUrlRewriteGenerator,
105  'currentUrlRewritesRegenerator' => $this->currentUrlRewritesRegenerator,
106  'anchorUrlRewriteGenerator' => $this->anchorUrlRewriteGenerator,
107  'objectRegistryFactory' => $this->objectRegistryFactory,
108  'storeViewService' => $this->storeViewService,
109  'storeManager' => $this->storeManager,
110  'mergeDataProviderFactory' => $mergeDataProviderFactory
111  ]
112  );
113  $this->categoryMock = $this->getMockBuilder(Category::class)->disableOriginalConstructor()->getMock();
114  }
115 
117  {
118  $product = $this->createMock(\Magento\Catalog\Model\Product::class);
119  $product->expects($this->any())->method('getStoreId')->will($this->returnValue(null));
120  $product->expects($this->any())->method('getStoreIds')->will($this->returnValue([1]));
121  $this->storeViewService->expects($this->once())->method('doesEntityHaveOverriddenUrlKeyForStore')
122  ->will($this->returnValue(false));
123  $this->initObjectRegistryFactory([]);
124  $canonical = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite([], $this->serializer);
125  $canonical->setRequestPath('category-1')
126  ->setStoreId(1);
127  $this->canonicalUrlRewriteGenerator->expects($this->any())->method('generate')
128  ->will($this->returnValue([$canonical]));
129  $categories = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite([], $this->serializer);
130  $categories->setRequestPath('category-2')
131  ->setStoreId(2);
132  $this->categoriesUrlRewriteGenerator->expects($this->any())->method('generate')
133  ->will($this->returnValue([$categories]));
134  $current = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite([], $this->serializer);
135  $current->setRequestPath('category-3')
136  ->setStoreId(3);
137  $this->currentUrlRewritesRegenerator->expects($this->any())->method('generate')
138  ->will($this->returnValue([$current]));
139  $this->currentUrlRewritesRegenerator->expects($this->any())->method('generateAnchor')
140  ->will($this->returnValue([$current]));
141  $anchorCategories = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite([], $this->serializer);
142  $anchorCategories->setRequestPath('category-4')
143  ->setStoreId(4);
144  $this->anchorUrlRewriteGenerator->expects($this->any())->method('generate')
145  ->will($this->returnValue([$anchorCategories]));
146 
147  $this->assertEquals(
148  [
149  'category-1_1' => $canonical,
150  'category-2_2' => $categories,
151  'category-3_3' => $current,
152  'category-4_4' => $anchorCategories
153  ],
154  $this->productScopeGenerator->generateForGlobalScope([$this->categoryMock], $product, 1)
155  );
156  }
157 
159  {
160  $storeRootCategoryId = 2;
161  $category_id = 4;
162  $product = $this->createMock(\Magento\Catalog\Model\Product::class);
163  $product->expects($this->any())->method('getStoreId')->will($this->returnValue(1));
164  $product->expects($this->never())->method('getStoreIds');
165  $this->categoryMock->expects($this->any())->method('getParentIds')
166  ->will($this->returnValue(['root-id', $storeRootCategoryId]));
167  $this->categoryMock->expects($this->any())->method('getId')->will($this->returnValue($category_id));
168  $this->initObjectRegistryFactory([$this->categoryMock]);
169  $canonical = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite([], $this->serializer);
170  $canonical->setRequestPath('category-1')
171  ->setStoreId(1);
172  $this->canonicalUrlRewriteGenerator->expects($this->any())->method('generate')
173  ->will($this->returnValue([$canonical]));
174  $this->categoriesUrlRewriteGenerator->expects($this->any())->method('generate')
175  ->will($this->returnValue([]));
176  $this->currentUrlRewritesRegenerator->expects($this->any())->method('generate')
177  ->will($this->returnValue([]));
178  $this->currentUrlRewritesRegenerator->expects($this->any())->method('generateAnchor')
179  ->will($this->returnValue([]));
180  $this->anchorUrlRewriteGenerator->expects($this->any())->method('generate')
181  ->will($this->returnValue([]));
182 
183  $this->assertEquals(
184  ['category-1_1' => $canonical],
185  $this->productScopeGenerator->generateForSpecificStoreView(1, [$this->categoryMock], $product, 1)
186  );
187  }
188 
193  {
194  $product = $this->createMock(\Magento\Catalog\Model\Product::class);
195  $product->expects($this->any())->method('getStoreIds')->will($this->returnValue([1, 2]));
196  $this->storeViewService->expects($this->exactly(2))->method('doesEntityHaveOverriddenUrlKeyForStore')
197  ->will($this->returnValue(true));
198 
199  $this->assertEquals([], $this->productScopeGenerator->generateForGlobalScope([], $product, 1));
200  }
201 
205  protected function initObjectRegistryFactory($entities)
206  {
207  $objectRegistry = $this->getMockBuilder(\Magento\CatalogUrlRewrite\Model\ObjectRegistry::class)
208  ->disableOriginalConstructor()->getMock();
209  $this->objectRegistryFactory->expects($this->any())->method('create')
210  ->with(['entities' => $entities])
211  ->will($this->returnValue($objectRegistry));
212  }
213 
221  public function testIsCategoryProperForGenerating($parentIds, $expectedResult)
222  {
223  $storeId = 1;
224  $this->categoryMock->expects(self::any())->method('getParentIds')->willReturn($parentIds);
225  $result = $this->productScopeGenerator->isCategoryProperForGenerating(
226  $this->categoryMock,
227  $storeId
228  );
229  self::assertEquals(
230  $expectedResult,
231  $result
232  );
233  }
234 
241  {
242  return [
243  [['0'], false],
244  [['1'], false],
245  [['1', '2'], true],
246  [['1', '3'], false],
247  ];
248  }
249 }
$value
Definition: gender.phtml:16
$categories