Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CurrentUrlRewritesRegeneratorTest.php
Go to the documentation of this file.
1 <?php
7 
12 
16 class CurrentUrlRewritesRegeneratorTest extends \PHPUnit\Framework\TestCase
17 {
19  private $currentUrlRewritesRegenerator;
20 
22  private $productUrlPathGenerator;
23 
25  private $product;
26 
28  private $category;
29 
31  private $objectRegistry;
32 
34  private $urlRewriteFactory;
35 
37  private $urlRewrite;
38 
40  private $mergeDataProvider;
41 
43  private $urlRewriteFinder;
44 
45  protected function setUp()
46  {
47  $this->urlRewriteFactory = $this->getMockBuilder(\Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory::class)
48  ->setMethods(['create'])
49  ->disableOriginalConstructor()->getMock();
50  $this->urlRewrite = $this->getMockBuilder(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class)
51  ->disableOriginalConstructor()->getMock();
52  $this->product = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
53  ->disableOriginalConstructor()->getMock();
54  $this->category = $this->getMockBuilder(\Magento\Catalog\Model\Category::class)
55  ->disableOriginalConstructor()->getMock();
56  $this->objectRegistry = $this->getMockBuilder(\Magento\CatalogUrlRewrite\Model\ObjectRegistry::class)
57  ->disableOriginalConstructor()->getMock();
58  $this->urlRewriteFinder = $this->getMockBuilder(\Magento\CatalogUrlRewrite\Model\Map\UrlRewriteFinder::class)
59  ->disableOriginalConstructor()->getMock();
60  $this->urlRewriteFactory->expects($this->once())->method('create')
61  ->willReturn($this->urlRewrite);
62  $this->productUrlPathGenerator = $this->getMockBuilder(
63  \Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator::class
64  )->disableOriginalConstructor()->getMock();
65  $mergeDataProviderFactory = $this->createPartialMock(
66  \Magento\UrlRewrite\Model\MergeDataProviderFactory::class,
67  ['create']
68  );
69  $this->mergeDataProvider = new \Magento\UrlRewrite\Model\MergeDataProvider();
70  $mergeDataProviderFactory->expects($this->once())->method('create')->willReturn($this->mergeDataProvider);
71  $this->currentUrlRewritesRegenerator = (new ObjectManager($this))->getObject(
72  \Magento\CatalogUrlRewrite\Model\Product\CurrentUrlRewritesRegenerator::class,
73  [
74  'productUrlPathGenerator' => $this->productUrlPathGenerator,
75  'urlRewriteFactory' => $this->urlRewriteFactory,
76  'mergeDataProviderFactory' => $mergeDataProviderFactory,
77  'urlRewriteFinder' => $this->urlRewriteFinder
78  ]
79  );
80  }
81 
83  {
84  $this->urlRewriteFinder->expects($this->once())->method('findAllByData')
85  ->will($this->returnValue($this->getCurrentRewritesMocks([[UrlRewrite::IS_AUTOGENERATED => 1]])));
86  $this->product->expects($this->once())->method('getData')->with('save_rewrites_history')
87  ->will($this->returnValue(false));
88 
89  $this->assertEquals(
90  [],
91  $this->currentUrlRewritesRegenerator->generate('store_id', $this->product, $this->objectRegistry)
92  );
93  }
94 
96  {
97  $this->urlRewriteFinder->expects($this->once())->method('findAllByData')
98  ->will($this->returnValue($this->getCurrentRewritesMocks([
100  ])));
101  $this->product->expects($this->once())->method('getData')->with('save_rewrites_history')
102  ->will($this->returnValue(true));
103  $this->productUrlPathGenerator->expects($this->once())->method('getUrlPathWithSuffix')
104  ->will($this->returnValue('same-path'));
105 
106  $this->assertEquals(
107  [],
108  $this->currentUrlRewritesRegenerator->generate('store_id', $this->product, $this->objectRegistry, 1)
109  );
110  }
111 
113  {
114  $requestPath = 'autogenerated.html';
115  $targetPath = 'some-path.html';
116  $autoGenerated = 1;
117  $storeId = 2;
118  $productId = 12;
119  $description = 'description';
120  $this->urlRewriteFinder->expects($this->once())->method('findAllByData')
121  ->will($this->returnValue($this->getCurrentRewritesMocks([
122  [
123  UrlRewrite::REQUEST_PATH => $requestPath,
124  UrlRewrite::TARGET_PATH => 'custom-target-path',
126  UrlRewrite::IS_AUTOGENERATED => $autoGenerated,
127  UrlRewrite::METADATA => [],
129  ],
130  ])));
131  $this->product->expects($this->any())->method('getEntityId')->will($this->returnValue($productId));
132  $this->product->expects($this->once())->method('getData')->with('save_rewrites_history')
133  ->will($this->returnValue(true));
134  $this->productUrlPathGenerator->expects($this->once())->method('getUrlPathWithSuffix')
135  ->will($this->returnValue($targetPath));
136 
137  $this->prepareUrlRewriteMock(
138  $storeId,
139  $productId,
140  $requestPath,
141  $targetPath,
142  0,
144  [],
146  );
147 
148  $this->assertEquals(
149  [$this->urlRewrite],
150  $this->currentUrlRewritesRegenerator->generate($storeId, $this->product, $this->objectRegistry, 1)
151  );
152  }
153 
155  {
156  $productId = 12;
157  $requestPath = 'autogenerated.html';
158  $targetPath = 'simple-product.html';
159  $autoGenerated = 1;
160  $storeId = 2;
161  $metadata = ['category_id' => 2, 'some_another_data' => 1];
162  $description = 'description';
163  $this->urlRewriteFinder->expects($this->once())->method('findAllByData')
164  ->will($this->returnValue($this->getCurrentRewritesMocks([
165  [
166  UrlRewrite::REQUEST_PATH => $requestPath,
167  UrlRewrite::TARGET_PATH => 'some-path.html',
169  UrlRewrite::IS_AUTOGENERATED => $autoGenerated,
170  UrlRewrite::METADATA => $metadata,
172  ],
173  ])));
174  $this->product->expects($this->any())->method('getEntityId')->will($this->returnValue($productId));
175  $this->product->expects($this->once())->method('getData')->with('save_rewrites_history')
176  ->will($this->returnValue(true));
177  $this->productUrlPathGenerator->expects($this->once())->method('getUrlPathWithSuffix')
178  ->will($this->returnValue($targetPath));
179  $this->objectRegistry->expects($this->once())->method('get')->will($this->returnValue($this->category));
180  $this->prepareUrlRewriteMock(
181  $storeId,
182  $productId,
183  $requestPath,
184  $targetPath,
185  0,
187  $metadata,
189  );
190 
191  $this->assertEquals(
192  [$this->urlRewrite],
193  $this->currentUrlRewritesRegenerator->generate($storeId, $this->product, $this->objectRegistry, 2)
194  );
195  }
196 
197  public function testSkipGenerationForCustom()
198  {
199  $this->urlRewriteFinder->expects($this->once())->method('findAllByData')
200  ->will($this->returnValue($this->getCurrentRewritesMocks([
201  [
203  UrlRewrite::REQUEST_PATH => 'same-path',
205  ],
206  ])));
207  $this->productUrlPathGenerator->expects($this->once())->method('getUrlPathWithSuffix')
208  ->will($this->returnValue('same-path'));
209 
210  $this->assertEquals(
211  [],
212  $this->currentUrlRewritesRegenerator->generate('store_id', $this->product, $this->objectRegistry)
213  );
214  }
215 
217  {
218  $storeId = 12;
219  $productId = 123;
220  $requestPath = 'generate-for-custom-without-redirect-type.html';
221  $targetPath = 'custom-target-path.html';
222  $autoGenerated = 0;
223  $description = 'description';
224  $this->urlRewriteFinder->expects($this->once())->method('findAllByData')
225  ->will($this->returnValue($this->getCurrentRewritesMocks([
226  [
227  UrlRewrite::REQUEST_PATH => $requestPath,
228  UrlRewrite::TARGET_PATH => $targetPath,
230  UrlRewrite::IS_AUTOGENERATED => $autoGenerated,
232  UrlRewrite::METADATA => [],
233  ],
234  ])));
235  $this->productUrlPathGenerator->expects($this->never())->method('getUrlPathWithSuffix');
236  $this->product->expects($this->any())->method('getEntityId')->will($this->returnValue($productId));
237  $this->prepareUrlRewriteMock(
238  $storeId,
239  $productId,
240  $requestPath,
241  $targetPath,
242  $autoGenerated,
243  0,
244  [],
246  );
247 
248  $this->assertEquals(
249  [$this->urlRewrite],
250  $this->currentUrlRewritesRegenerator->generate($storeId, $this->product, $this->objectRegistry)
251  );
252  }
253 
255  {
256  $storeId = 12;
257  $productId = 123;
258  $requestPath = 'generate-for-custom-without-redirect-type.html';
259  $targetPath = 'generated-target-path.html';
260  $autoGenerated = 0;
261  $description = 'description';
262  $this->urlRewriteFinder->expects($this->once())->method('findAllByData')
263  ->will($this->returnValue($this->getCurrentRewritesMocks([
264  [
265  UrlRewrite::REQUEST_PATH => $requestPath,
266  UrlRewrite::TARGET_PATH => 'custom-target-path.html',
267  UrlRewrite::REDIRECT_TYPE => 'code',
268  UrlRewrite::IS_AUTOGENERATED => $autoGenerated,
270  UrlRewrite::METADATA => [],
271  ],
272  ])));
273  $this->productUrlPathGenerator->expects($this->any())->method('getUrlPathWithSuffix')
274  ->will($this->returnValue($targetPath));
275  $this->product->expects($this->any())->method('getEntityId')->will($this->returnValue($productId));
276  $this->prepareUrlRewriteMock($storeId, $productId, $requestPath, $targetPath, 0, 'code', [], $description);
277 
278  $this->assertEquals(
279  [$this->urlRewrite],
280  $this->currentUrlRewritesRegenerator->generate($storeId, $this->product, $this->objectRegistry)
281  );
282  }
283 
288  protected function getCurrentRewritesMocks($currentRewrites)
289  {
290  $rewrites = [];
291  foreach ($currentRewrites as $urlRewrite) {
293  $url = $this->getMockBuilder(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class)
294  ->disableOriginalConstructor()->getMock();
295  foreach ($urlRewrite as $key => $value) {
296  $url->expects($this->any())
297  ->method('get' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key))))
298  ->will($this->returnValue($value));
299  }
300  $rewrites[] = $url;
301  }
302  return $rewrites;
303  }
304 
315  protected function prepareUrlRewriteMock(
316  $storeId,
317  $productId,
318  $requestPath,
319  $targetPath,
320  $autoGenerated,
321  $redirectType,
322  $metadata,
324  ) {
325  $this->urlRewrite->expects($this->any())->method('setStoreId')->with($storeId)
326  ->will($this->returnSelf());
327  $this->urlRewrite->expects($this->any())->method('setEntityId')->with($productId)
328  ->will($this->returnSelf());
329  $this->urlRewrite->expects($this->any())->method('setEntityType')
330  ->with(ProductUrlRewriteGenerator::ENTITY_TYPE)->will($this->returnSelf());
331  $this->urlRewrite->expects($this->any())->method('setRequestPath')->with($requestPath)
332  ->will($this->returnSelf());
333  $this->urlRewrite->expects($this->any())->method('setTargetPath')->with($targetPath)
334  ->will($this->returnSelf());
335  $this->urlRewrite->expects($this->any())->method('setIsAutogenerated')->with($autoGenerated)
336  ->will($this->returnSelf());
337  $this->urlRewrite->expects($this->any())->method('setRedirectType')->with($redirectType)
338  ->will($this->returnSelf());
339  $this->urlRewrite->expects($this->any())->method('setMetadata')->with($metadata)
340  ->will($this->returnSelf());
341  $this->urlRewriteFactory->expects($this->any())->method('create')->will($this->returnValue($this->urlRewrite));
342  $this->urlRewrite->expects($this->once())->method('setDescription')->with($description)
343  ->will($this->returnSelf());
344  }
345 }
prepareUrlRewriteMock( $storeId, $productId, $requestPath, $targetPath, $autoGenerated, $redirectType, $metadata, $description)
$value
Definition: gender.phtml:16