19 private $currentUrlRewritesRegenerator;
22 private $productUrlPathGenerator;
31 private $objectRegistry;
34 private $urlRewriteFactory;
40 private $mergeDataProvider;
43 private $urlRewriteFinder;
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(
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,
74 'productUrlPathGenerator' => $this->productUrlPathGenerator,
75 'urlRewriteFactory' => $this->urlRewriteFactory,
76 'mergeDataProviderFactory' => $mergeDataProviderFactory,
77 'urlRewriteFinder' => $this->urlRewriteFinder
84 $this->urlRewriteFinder->expects($this->once())->method(
'findAllByData')
86 $this->product->expects($this->once())->method(
'getData')->with(
'save_rewrites_history')
87 ->will($this->returnValue(
false));
91 $this->currentUrlRewritesRegenerator->generate(
'store_id', $this->product, $this->objectRegistry)
97 $this->urlRewriteFinder->expects($this->once())->method(
'findAllByData')
98 ->will($this->returnValue($this->getCurrentRewritesMocks([
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'));
108 $this->currentUrlRewritesRegenerator->generate(
'store_id', $this->product, $this->objectRegistry, 1)
114 $requestPath =
'autogenerated.html';
115 $targetPath =
'some-path.html';
120 $this->urlRewriteFinder->expects($this->once())->method(
'findAllByData')
121 ->will($this->returnValue($this->getCurrentRewritesMocks([
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));
150 $this->currentUrlRewritesRegenerator->generate(
$storeId, $this->product, $this->objectRegistry, 1)
157 $requestPath =
'autogenerated.html';
158 $targetPath =
'simple-product.html';
161 $metadata = [
'category_id' => 2,
'some_another_data' => 1];
163 $this->urlRewriteFinder->expects($this->once())->method(
'findAllByData')
164 ->will($this->returnValue($this->getCurrentRewritesMocks([
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));
193 $this->currentUrlRewritesRegenerator->generate(
$storeId, $this->product, $this->objectRegistry, 2)
199 $this->urlRewriteFinder->expects($this->once())->method(
'findAllByData')
200 ->will($this->returnValue($this->getCurrentRewritesMocks([
207 $this->productUrlPathGenerator->expects($this->once())->method(
'getUrlPathWithSuffix')
208 ->will($this->returnValue(
'same-path'));
212 $this->currentUrlRewritesRegenerator->generate(
'store_id', $this->product, $this->objectRegistry)
220 $requestPath =
'generate-for-custom-without-redirect-type.html';
221 $targetPath =
'custom-target-path.html';
224 $this->urlRewriteFinder->expects($this->once())->method(
'findAllByData')
225 ->will($this->returnValue($this->getCurrentRewritesMocks([
235 $this->productUrlPathGenerator->expects($this->never())->method(
'getUrlPathWithSuffix');
236 $this->product->expects($this->any())->method(
'getEntityId')->will($this->returnValue(
$productId));
250 $this->currentUrlRewritesRegenerator->generate(
$storeId, $this->product, $this->objectRegistry)
258 $requestPath =
'generate-for-custom-without-redirect-type.html';
259 $targetPath =
'generated-target-path.html';
262 $this->urlRewriteFinder->expects($this->once())->method(
'findAllByData')
263 ->will($this->returnValue($this->getCurrentRewritesMocks([
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));
280 $this->currentUrlRewritesRegenerator->generate(
$storeId, $this->product, $this->objectRegistry)
288 protected function getCurrentRewritesMocks($currentRewrites)
291 foreach ($currentRewrites as $urlRewrite) {
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));
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')
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());
testGenerationForCustomWithoutTargetPathGeneration()
prepareUrlRewriteMock( $storeId, $productId, $requestPath, $targetPath, $autoGenerated, $redirectType, $metadata, $description)
testIsAutogeneratedWithoutSaveRewriteHistory()
testGenerationForCustomWithTargetPathGeneration()
testSkipGenerationForCustom()
testIsAutogeneratedWithCategory()
testIsAutogeneratedWithoutCategory()
testSkipGenerationForAutogenerated()