11 use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
28 private $categoryId = 10;
43 private $productUrlRewriteGenerator;
48 private $productRepository;
68 private $catalogProductFactory;
73 private $storeManager;
78 private $objectRegistryFactory;
83 private $productUrlPathGenerator;
88 private $storeViewService;
93 private $urlRewriteFactory;
103 private $objectRegistry;
118 private $mergeDataProvider;
123 private $categoryCollectionFactory;
133 'url_key' =>
'value1',
138 'url_key' =>
'value3',
139 ImportProduct::COL_STORE =>
'not global',
156 $this->importProduct = $this->createPartialMock(\
Magento\CatalogImportExport\Model\
Import\Product::class, [
158 'getProductCategories',
159 'getProductWebsites',
161 'getCategoryProcessor',
163 $this->catalogProductFactory = $this->createPartialMock(\
Magento\Catalog\Model\ProductFactory::class, [
166 $this->storeManager = $this
170 ->disableOriginalConstructor()
174 ->getMockForAbstractClass();
175 $this->
event = $this->createPartialMock(\
Magento\Framework\Event::class, [
'getAdapter',
'getBunch']);
176 $this->
event->expects($this->any())->method(
'getAdapter')->willReturn($this->importProduct);
177 $this->
event->expects($this->any())->method(
'getBunch')->willReturn($this->products);
178 $this->observer = $this->createPartialMock(\
Magento\Framework\
Event\Observer::class, [
'getEvent']);
179 $this->observer->expects($this->any())->method(
'getEvent')->willReturn($this->event);
180 $this->urlPersist = $this->getMockBuilder(\
Magento\
UrlRewrite\Model\UrlPersistInterface::class)
181 ->disableOriginalConstructor()
183 $this->productUrlRewriteGenerator =
184 $this->getMockBuilder(\
Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator::class)
185 ->disableOriginalConstructor()
186 ->setMethods([
'generate'])
188 $this->productRepository = $this->getMockBuilder(\
Magento\Catalog\Api\ProductRepositoryInterface::class)
189 ->disableOriginalConstructor()
192 $this->objectRegistryFactory = $this->createMock(\
Magento\CatalogUrlRewrite\Model\ObjectRegistryFactory::class);
193 $this->productUrlPathGenerator = $this->createMock(
194 \
Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator::class
196 $this->storeViewService = $this->createMock(\
Magento\CatalogUrlRewrite\Service\V1\StoreViewService::class);
197 $this->urlRewriteFactory = $this->createPartialMock(
203 $this->urlFinder = $this
208 ->disableOriginalConstructor()
209 ->getMockForAbstractClass();
211 $this->urlRewrite = $this
213 ->disableOriginalConstructor()
216 $this->product = $this
217 ->getMockBuilder(\
Magento\Catalog\Model\Product::class)
218 ->disableOriginalConstructor()
221 $this->objectRegistry = $this
222 ->getMockBuilder(\
Magento\CatalogUrlRewrite\Model\ObjectRegistry::class)
223 ->disableOriginalConstructor()
226 $mergeDataProviderFactory = $this->createPartialMock(
230 $this->mergeDataProvider = new \Magento\UrlRewrite\Model\MergeDataProvider();
231 $mergeDataProviderFactory->expects($this->once())->method(
'create')->willReturn($this->mergeDataProvider);
233 $this->categoryCollectionFactory = $this->getMockBuilder(CategoryCollectionFactory::class)
234 ->setMethods([
'create'])
235 ->disableOriginalConstructor()
239 $this->
import = $this->objectManager->getObject(
242 'catalogProductFactory' => $this->catalogProductFactory,
243 'objectRegistryFactory' => $this->objectRegistryFactory,
244 'productUrlPathGenerator' => $this->productUrlPathGenerator,
245 'storeViewService' => $this->storeViewService,
246 'storeManager'=> $this->storeManager,
247 'urlPersist' => $this->urlPersist,
248 'urlRewriteFactory' => $this->urlRewriteFactory,
249 'urlFinder' => $this->urlFinder,
250 'mergeDataProviderFactory' => $mergeDataProviderFactory,
251 'categoryCollectionFactory' => $this->categoryCollectionFactory
269 $newSku = [[
'entity_id' =>
'value'], [
'entity_id' =>
'value3']];
271 $productsCount = count($this->products);
272 $websiteMock = $this->createPartialMock(\
Magento\
Store\Model\Website::class, [
277 ->expects($this->once())
278 ->method(
'getStoreIds')
279 ->willReturn($storeIds);
281 ->expects($this->once())
282 ->method(
'getWebsite')
284 ->willReturn($websiteMock);
286 ->expects($this->exactly($productsCount))
287 ->method(
'getNewSku')
289 [$this->products[0][ImportProduct::COL_SKU]],
290 [$this->products[1][ImportProduct::COL_SKU]]
292 ->will($this->onConsecutiveCalls($newSku[0], $newSku[1]));
294 ->expects($this->exactly($productsCount))
295 ->method(
'getProductCategories')
297 [$this->products[0][ImportProduct::COL_SKU]],
298 [$this->products[1][ImportProduct::COL_SKU]]
300 $getProductWebsitesCallsCount = $productsCount*2;
302 ->expects($this->exactly($getProductWebsitesCallsCount))
303 ->method(
'getProductWebsites')
304 ->willReturnOnConsecutiveCalls(
311 [$this->products[0][ImportProduct::COL_STORE], $this->products[0][ImportProduct::COL_STORE]],
312 [$this->products[1][ImportProduct::COL_STORE], $this->products[1][ImportProduct::COL_STORE]]
315 ->expects($this->exactly(1))
316 ->method(
'getStoreIdByCode')
317 ->will($this->returnValueMap(
$map));
318 $product = $this->createPartialMock(\
Magento\Catalog\Model\Product::class, [
326 ->expects($this->exactly($productsCount))
328 ->withConsecutive([$newSku[0][
'entity_id']], [$newSku[1][
'entity_id']]);
330 ->expects($this->any())
332 ->willReturnOnConsecutiveCalls(
333 $newSku[0][
'entity_id'],
334 $newSku[0][
'entity_id'],
335 $newSku[0][
'entity_id'],
336 $newSku[0][
'entity_id'],
337 $newSku[1][
'entity_id'],
338 $newSku[1][
'entity_id'],
339 $newSku[1][
'entity_id']
342 ->expects($this->exactly($productsCount))
344 ->will($this->onConsecutiveCalls(
345 $this->products[0][
'sku'],
346 $this->products[1][
'sku']
349 ->expects($this->exactly($productsCount))
350 ->method(
'getStoreId')
351 ->will($this->onConsecutiveCalls(
352 $this->products[0][ImportProduct::COL_STORE],
353 $this->products[1][ImportProduct::COL_STORE]
356 ->expects($this->exactly($productsCount))
357 ->method(
'setStoreId')
359 [$this->products[0][ImportProduct::COL_STORE]],
360 [$this->products[1][ImportProduct::COL_STORE]]
362 $this->catalogProductFactory
363 ->expects($this->exactly($productsCount))
365 ->willReturn($product);
367 $this->urlFinder->expects($this->any())->method(
'findAllByData')->willReturn([]);
369 $this->productUrlPathGenerator->expects($this->any())->method(
'getUrlPathWithSuffix')
370 ->willReturn(
'urlPathWithSuffix');
371 $this->productUrlPathGenerator->expects($this->any())->method(
'getUrlPath')
372 ->willReturn(
'urlPath');
373 $this->productUrlPathGenerator->expects($this->any())->method(
'getCanonicalUrlPath')
374 ->willReturn(
'canonicalUrlPath');
376 $this->urlRewrite->expects($this->any())->method(
'setStoreId')->willReturnSelf();
377 $this->urlRewrite->expects($this->any())->method(
'setEntityId')->willReturnSelf();
378 $this->urlRewrite->expects($this->any())->method(
'setEntityType')->willReturnSelf();
379 $this->urlRewrite->expects($this->any())->method(
'setRequestPath')->willReturnSelf();
380 $this->urlRewrite->expects($this->any())->method(
'setTargetPath')->willReturnSelf();
381 $this->urlRewrite->expects($this->any())->method(
'getTargetPath')->willReturn(
'targetPath');
382 $this->urlRewrite->expects($this->any())->method(
'getRequestPath')->willReturn(
'requestPath');
383 $this->urlRewrite->expects($this->any())->method(
'getStoreId')
384 ->willReturnOnConsecutiveCalls(0,
'not global');
386 $this->urlRewriteFactory->expects($this->any())->method(
'create')->willReturn($this->urlRewrite);
389 'requestPath_0' => $this->urlRewrite,
390 'requestPath_not global' => $this->urlRewrite
394 ->expects($this->once())
396 ->with($productUrls);
398 $this->
import->execute($this->observer);
407 $requestPath =
'simple-product.html';
410 ->getMockBuilder(\
Magento\Catalog\Model\Product::class)
411 ->disableOriginalConstructor()
413 $productsByStores = [
$storeId => $product];
418 $targetPath =
'catalog/product/view/id/' .
$productId;
421 $this->productUrlPathGenerator
422 ->expects($this->once())
423 ->method(
'getUrlPathWithSuffix')
424 ->will($this->returnValue($requestPath));
425 $this->productUrlPathGenerator
426 ->expects($this->once())
427 ->method(
'getUrlPath')
428 ->will($this->returnValue(
'urlPath'));
429 $this->productUrlPathGenerator
430 ->expects($this->once())
431 ->method(
'getCanonicalUrlPath')
432 ->will($this->returnValue($targetPath));
434 ->expects($this->once())
435 ->method(
'setStoreId')
437 ->will($this->returnSelf());
439 ->expects($this->once())
440 ->method(
'setEntityId')
442 ->will($this->returnSelf());
444 ->expects($this->once())
445 ->method(
'setEntityType')
447 ->will($this->returnSelf());
449 ->expects($this->once())
450 ->method(
'setRequestPath')
452 ->will($this->returnSelf());
454 ->expects($this->once())
455 ->method(
'setTargetPath')
457 ->will($this->returnSelf());
458 $this->urlRewriteFactory
459 ->expects($this->once())
461 ->willReturn($this->urlRewrite);
463 $actualResult = $this->
invokeMethod($this->
import,
'canonicalUrlRewriteGenerate');
480 ->getMockBuilder(\
Magento\Catalog\Model\Product::class)
481 ->disableOriginalConstructor()
483 $productsByStores = [
$storeId => $product];
490 $this->productUrlPathGenerator
491 ->expects($this->once())
492 ->method(
'getUrlPath')
493 ->will($this->returnValue(
''));
494 $this->urlRewriteFactory
495 ->expects($this->never())
498 $actualResult = $this->
invokeMethod($this->
import,
'canonicalUrlRewriteGenerate');
499 $this->assertEquals([], $actualResult);
508 $urlPathWithCategory =
'category/simple-product.html';
511 $canonicalUrlPathWithCategory =
'canonical-path-with-category';
513 ->getMockBuilder(\
Magento\Catalog\Model\Product::class)
514 ->disableOriginalConstructor()
516 $productsByStores = [
530 $this->productUrlPathGenerator
531 ->expects($this->any())
532 ->method(
'getUrlPathWithSuffix')
533 ->will($this->returnValue($urlPathWithCategory));
534 $this->productUrlPathGenerator
535 ->expects($this->any())
536 ->method(
'getCanonicalUrlPath')
537 ->will($this->returnValue($canonicalUrlPathWithCategory));
540 ->expects($this->any())
542 ->will($this->returnValue($this->categoryId));
545 ->disableOriginalConstructor()
548 ->method(
'addIdFilter')
549 ->with([$this->categoryId])
552 ->method(
'setStoreId')
556 ->method(
'addAttributeToSelect')
563 ->method(
'getFirstItem')
566 $this->categoryCollectionFactory->expects($this->once())
571 ->expects($this->any())
572 ->method(
'setStoreId')
574 ->will($this->returnSelf());
576 ->expects($this->any())
577 ->method(
'setEntityId')
579 ->will($this->returnSelf());
581 ->expects($this->any())
582 ->method(
'setEntityType')
584 ->will($this->returnSelf());
586 ->expects($this->any())
587 ->method(
'setRequestPath')
588 ->with($urlPathWithCategory)
589 ->will($this->returnSelf());
591 ->expects($this->any())
592 ->method(
'setTargetPath')
593 ->with($canonicalUrlPathWithCategory)
594 ->will($this->returnSelf());
596 ->expects($this->any())
597 ->method(
'setMetadata')
598 ->with([
'category_id' => $this->categoryId])
599 ->will($this->returnSelf());
600 $this->urlRewriteFactory
601 ->expects($this->any())
603 ->willReturn($this->urlRewrite);
605 $actualResult = $this->
invokeMethod($this->
import,
'categoriesUrlRewriteGenerate');
622 $reflection = new \ReflectionClass(get_class($object));
623 $reflectionProperty = $reflection->getProperty($property);
624 $reflectionProperty->setAccessible(
true);
625 $reflectionProperty->setValue($object,
$value);
634 protected function invokeMethod($object, $methodName, array $parameters = [])
636 $reflection = new \ReflectionClass(get_class($object));
637 $method = $reflection->getMethod($methodName);
640 return $method->invokeArgs($object, $parameters);
661 $this->urlRewrite->expects($this->any())->method(
'setStoreId')->with(
$storeId)
662 ->will($this->returnSelf());
663 $this->urlRewrite->expects($this->any())->method(
'setEntityId')->with(
$productId)
664 ->will($this->returnSelf());
665 $this->urlRewrite->expects($this->any())->method(
'setEntityType')
667 $this->urlRewrite->expects($this->any())->method(
'setRequestPath')->with($requestPath)
668 ->will($this->returnSelf());
669 $this->urlRewrite->expects($this->any())->method(
'setTargetPath')->with($targetPath)
670 ->will($this->returnSelf());
671 $this->urlRewrite->expects($this->any())->method(
'setIsAutogenerated')->with(0)
672 ->will($this->returnSelf());
673 $this->urlRewrite->expects($this->any())->method(
'setRedirectType')->with($redirectType)
674 ->will($this->returnSelf());
675 $this->urlRewrite->expects($this->any())->method(
'setMetadata')->with($metadata)
676 ->will($this->returnSelf());
677 $this->urlRewriteFactory->expects($this->any())->method(
'create')->will($this->returnValue($this->urlRewrite));
678 $this->urlRewrite->expects($this->once())->method(
'setDescription')->with(
$description)
679 ->will($this->returnSelf());
686 protected function currentUrlRewritesRegeneratorGetCurrentRewritesMocks($currentRewrites)
689 foreach ($currentRewrites as $urlRewrite) {
694 ->disableOriginalConstructor()->getMock();
695 foreach ($urlRewrite as $key =>
$value) {
696 $url->expects($this->any())
697 ->method(
'get' . str_replace(
' ',
'', ucwords(str_replace(
'_',
' ', $key))))
698 ->will($this->returnValue(
$value));
invokeMethod($object, $methodName, array $parameters=[])
setPropertyValue($object, $property, $value)
currentUrlRewritesRegeneratorPrepareUrlRewriteMock( $storeId, $productId, $requestPath, $targetPath, $redirectType, $metadata, $description)
testCategoriesUrlRewriteGenerate()
testCanonicalUrlRewriteGenerateWithUrlPath()
testCanonicalUrlRewriteGenerateWithEmptyUrlPath()