Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AfterImportDataObserverTest.php
Go to the documentation of this file.
1 <?php
2 
9 
11 use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
16 
23 class AfterImportDataObserverTest extends \PHPUnit\Framework\TestCase
24 {
28  private $categoryId = 10;
29 
33  private $urlPersist;
34 
38  protected $urlFinder;
39 
43  private $productUrlRewriteGenerator;
44 
48  private $productRepository;
49 
53  protected $importProduct;
54 
58  private $observer;
59 
63  private $event;
64 
68  private $catalogProductFactory;
69 
73  private $storeManager;
74 
78  private $objectRegistryFactory;
79 
83  private $productUrlPathGenerator;
84 
88  private $storeViewService;
89 
93  private $urlRewriteFactory;
94 
98  private $urlRewrite;
99 
103  private $objectRegistry;
104 
108  private $import;
109 
113  private $product;
114 
118  private $mergeDataProvider;
119 
123  private $categoryCollectionFactory;
124 
130  protected $products = [
131  [
132  'sku' => 'sku',
133  'url_key' => 'value1',
134  ImportProduct::COL_STORE => Store::DEFAULT_STORE_ID,
135  ],
136  [
137  'sku' => 'sku3',
138  'url_key' => 'value3',
139  ImportProduct::COL_STORE => 'not global',
140  ],
141  ];
142 
146  protected $objectManager;
147 
154  protected function setUp()
155  {
156  $this->importProduct = $this->createPartialMock(\Magento\CatalogImportExport\Model\Import\Product::class, [
157  'getNewSku',
158  'getProductCategories',
159  'getProductWebsites',
160  'getStoreIdByCode',
161  'getCategoryProcessor',
162  ]);
163  $this->catalogProductFactory = $this->createPartialMock(\Magento\Catalog\Model\ProductFactory::class, [
164  'create',
165  ]);
166  $this->storeManager = $this
167  ->getMockBuilder(
168  \Magento\Store\Model\StoreManagerInterface::class
169  )
170  ->disableOriginalConstructor()
171  ->setMethods([
172  'getWebsite',
173  ])
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()
182  ->getMock();
183  $this->productUrlRewriteGenerator =
184  $this->getMockBuilder(\Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator::class)
185  ->disableOriginalConstructor()
186  ->setMethods(['generate'])
187  ->getMock();
188  $this->productRepository = $this->getMockBuilder(\Magento\Catalog\Api\ProductRepositoryInterface::class)
189  ->disableOriginalConstructor()
190  ->getMock();
191 
192  $this->objectRegistryFactory = $this->createMock(\Magento\CatalogUrlRewrite\Model\ObjectRegistryFactory::class);
193  $this->productUrlPathGenerator = $this->createMock(
194  \Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator::class
195  );
196  $this->storeViewService = $this->createMock(\Magento\CatalogUrlRewrite\Service\V1\StoreViewService::class);
197  $this->urlRewriteFactory = $this->createPartialMock(
198  \Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory::class,
199  [
200  'create',
201  ]
202  );
203  $this->urlFinder = $this
204  ->getMockBuilder(\Magento\UrlRewrite\Model\UrlFinderInterface::class)
205  ->setMethods([
206  'findAllByData',
207  ])
208  ->disableOriginalConstructor()
209  ->getMockForAbstractClass();
210 
211  $this->urlRewrite = $this
212  ->getMockBuilder(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class)
213  ->disableOriginalConstructor()
214  ->getMock();
215 
216  $this->product = $this
217  ->getMockBuilder(\Magento\Catalog\Model\Product::class)
218  ->disableOriginalConstructor()
219  ->getMock();
220 
221  $this->objectRegistry = $this
222  ->getMockBuilder(\Magento\CatalogUrlRewrite\Model\ObjectRegistry::class)
223  ->disableOriginalConstructor()
224  ->getMock();
225 
226  $mergeDataProviderFactory = $this->createPartialMock(
227  \Magento\UrlRewrite\Model\MergeDataProviderFactory::class,
228  ['create']
229  );
230  $this->mergeDataProvider = new \Magento\UrlRewrite\Model\MergeDataProvider();
231  $mergeDataProviderFactory->expects($this->once())->method('create')->willReturn($this->mergeDataProvider);
232 
233  $this->categoryCollectionFactory = $this->getMockBuilder(CategoryCollectionFactory::class)
234  ->setMethods(['create'])
235  ->disableOriginalConstructor()
236  ->getMock();
237 
238  $this->objectManager = new ObjectManager($this);
239  $this->import = $this->objectManager->getObject(
240  \Magento\CatalogUrlRewrite\Observer\AfterImportDataObserver::class,
241  [
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
252  ]
253  );
254  }
255 
267  public function testAfterImportData()
268  {
269  $newSku = [['entity_id' => 'value'], ['entity_id' => 'value3']];
270  $websiteId = 'websiteId value';
271  $productsCount = count($this->products);
272  $websiteMock = $this->createPartialMock(\Magento\Store\Model\Website::class, [
273  'getStoreIds',
274  ]);
275  $storeIds = [1, Store::DEFAULT_STORE_ID];
276  $websiteMock
277  ->expects($this->once())
278  ->method('getStoreIds')
279  ->willReturn($storeIds);
280  $this->storeManager
281  ->expects($this->once())
282  ->method('getWebsite')
283  ->with($websiteId)
284  ->willReturn($websiteMock);
285  $this->importProduct
286  ->expects($this->exactly($productsCount))
287  ->method('getNewSku')
288  ->withConsecutive(
289  [$this->products[0][ImportProduct::COL_SKU]],
290  [$this->products[1][ImportProduct::COL_SKU]]
291  )
292  ->will($this->onConsecutiveCalls($newSku[0], $newSku[1]));
293  $this->importProduct
294  ->expects($this->exactly($productsCount))
295  ->method('getProductCategories')
296  ->withConsecutive(
297  [$this->products[0][ImportProduct::COL_SKU]],
298  [$this->products[1][ImportProduct::COL_SKU]]
299  )->willReturn([]);
300  $getProductWebsitesCallsCount = $productsCount*2;
301  $this->importProduct
302  ->expects($this->exactly($getProductWebsitesCallsCount))
303  ->method('getProductWebsites')
304  ->willReturnOnConsecutiveCalls(
305  [$newSku[0]['entity_id'] => $websiteId],
306  [$newSku[0]['entity_id'] => $websiteId],
307  [$newSku[1]['entity_id'] => $websiteId],
308  [$newSku[1]['entity_id'] => $websiteId]
309  );
310  $map = [
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]]
313  ];
314  $this->importProduct
315  ->expects($this->exactly(1))
316  ->method('getStoreIdByCode')
317  ->will($this->returnValueMap($map));
318  $product = $this->createPartialMock(\Magento\Catalog\Model\Product::class, [
319  'getId',
320  'setId',
321  'getSku',
322  'setStoreId',
323  'getStoreId',
324  ]);
325  $product
326  ->expects($this->exactly($productsCount))
327  ->method('setId')
328  ->withConsecutive([$newSku[0]['entity_id']], [$newSku[1]['entity_id']]);
329  $product
330  ->expects($this->any())
331  ->method('getId')
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']
340  );
341  $product
342  ->expects($this->exactly($productsCount))
343  ->method('getSku')
344  ->will($this->onConsecutiveCalls(
345  $this->products[0]['sku'],
346  $this->products[1]['sku']
347  ));
348  $product
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]
354  ));
355  $product
356  ->expects($this->exactly($productsCount))
357  ->method('setStoreId')
358  ->withConsecutive(
359  [$this->products[0][ImportProduct::COL_STORE]],
360  [$this->products[1][ImportProduct::COL_STORE]]
361  );
362  $this->catalogProductFactory
363  ->expects($this->exactly($productsCount))
364  ->method('create')
365  ->willReturn($product);
366 
367  $this->urlFinder->expects($this->any())->method('findAllByData')->willReturn([]);
368 
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');
375 
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');
385 
386  $this->urlRewriteFactory->expects($this->any())->method('create')->willReturn($this->urlRewrite);
387 
388  $productUrls = [
389  'requestPath_0' => $this->urlRewrite,
390  'requestPath_not global' => $this->urlRewrite
391  ];
392 
393  $this->urlPersist
394  ->expects($this->once())
395  ->method('replace')
396  ->with($productUrls);
397 
398  $this->import->execute($this->observer);
399  }
400 
405  {
406  $productId = 'product_id';
407  $requestPath = 'simple-product.html';
408  $storeId = 10;
409  $product = $this
410  ->getMockBuilder(\Magento\Catalog\Model\Product::class)
411  ->disableOriginalConstructor()
412  ->getMock();
413  $productsByStores = [$storeId => $product];
414  $products = [
415  $productId => $productsByStores,
416  ];
417 
418  $targetPath = 'catalog/product/view/id/' . $productId;
419  $this->setPropertyValue($this->import, 'products', $products);
420 
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));
433  $this->urlRewrite
434  ->expects($this->once())
435  ->method('setStoreId')
436  ->with($storeId)
437  ->will($this->returnSelf());
438  $this->urlRewrite
439  ->expects($this->once())
440  ->method('setEntityId')
441  ->with($productId)
442  ->will($this->returnSelf());
443  $this->urlRewrite
444  ->expects($this->once())
445  ->method('setEntityType')
447  ->will($this->returnSelf());
448  $this->urlRewrite
449  ->expects($this->once())
450  ->method('setRequestPath')
451  ->with($requestPath)
452  ->will($this->returnSelf());
453  $this->urlRewrite
454  ->expects($this->once())
455  ->method('setTargetPath')
456  ->with($targetPath)
457  ->will($this->returnSelf());
458  $this->urlRewriteFactory
459  ->expects($this->once())
460  ->method('create')
461  ->willReturn($this->urlRewrite);
462 
463  $actualResult = $this->invokeMethod($this->import, 'canonicalUrlRewriteGenerate');
464  $this->assertEquals(
465  [
466  $this->urlRewrite,
467  ],
468  $actualResult
469  );
470  }
471 
476  {
477  $productId = 'product_id';
478  $storeId = 10;
479  $product = $this
480  ->getMockBuilder(\Magento\Catalog\Model\Product::class)
481  ->disableOriginalConstructor()
482  ->getMock();
483  $productsByStores = [$storeId => $product];
484  $products = [
485  $productId => $productsByStores,
486  ];
487 
488  $this->setPropertyValue($this->import, 'products', $products);
489 
490  $this->productUrlPathGenerator
491  ->expects($this->once())
492  ->method('getUrlPath')
493  ->will($this->returnValue(''));
494  $this->urlRewriteFactory
495  ->expects($this->never())
496  ->method('create');
497 
498  $actualResult = $this->invokeMethod($this->import, 'canonicalUrlRewriteGenerate');
499  $this->assertEquals([], $actualResult);
500  }
501 
507  {
508  $urlPathWithCategory = 'category/simple-product.html';
509  $storeId = 10;
510  $productId = 'product_id';
511  $canonicalUrlPathWithCategory = 'canonical-path-with-category';
512  $product = $this
513  ->getMockBuilder(\Magento\Catalog\Model\Product::class)
514  ->disableOriginalConstructor()
515  ->getMock();
516  $productsByStores = [
517  $storeId => $product,
518  ];
519  $products = [
520  $productId => $productsByStores,
521  ];
522  $categoryCache = [
523  $productId => [$this->categoryId],
524  ];
525 
526  $this->setPropertyValue($this->import, 'products', $products);
527  $this->setPropertyValue($this->import, 'categoryCache', $categoryCache);
528  $this->setPropertyValue($this->import, 'import', $this->importProduct);
529 
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));
538  $category = $this->createMock(\Magento\Catalog\Model\Category::class);
539  $category
540  ->expects($this->any())
541  ->method('getId')
542  ->will($this->returnValue($this->categoryId));
543 
544  $categoryCollection = $this->getMockBuilder(CategoryCollection::class)
545  ->disableOriginalConstructor()
546  ->getMock();
547  $categoryCollection->expects($this->once())
548  ->method('addIdFilter')
549  ->with([$this->categoryId])
550  ->willReturnSelf();
551  $categoryCollection->expects($this->once())
552  ->method('setStoreId')
553  ->with($storeId)
554  ->willReturnSelf();
555  $categoryCollection->expects($this->exactly(3))
556  ->method('addAttributeToSelect')
557  ->withConsecutive(
558  ['name'],
559  ['url_key'],
560  ['url_path']
561  )->willReturnSelf();
562  $categoryCollection->expects($this->once())
563  ->method('getFirstItem')
564  ->willReturn($category);
565 
566  $this->categoryCollectionFactory->expects($this->once())
567  ->method('create')
568  ->willReturn($categoryCollection);
569 
570  $this->urlRewrite
571  ->expects($this->any())
572  ->method('setStoreId')
573  ->with($storeId)
574  ->will($this->returnSelf());
575  $this->urlRewrite
576  ->expects($this->any())
577  ->method('setEntityId')
578  ->with($productId)
579  ->will($this->returnSelf());
580  $this->urlRewrite
581  ->expects($this->any())
582  ->method('setEntityType')
584  ->will($this->returnSelf());
585  $this->urlRewrite
586  ->expects($this->any())
587  ->method('setRequestPath')
588  ->with($urlPathWithCategory)
589  ->will($this->returnSelf());
590  $this->urlRewrite
591  ->expects($this->any())
592  ->method('setTargetPath')
593  ->with($canonicalUrlPathWithCategory)
594  ->will($this->returnSelf());
595  $this->urlRewrite
596  ->expects($this->any())
597  ->method('setMetadata')
598  ->with(['category_id' => $this->categoryId])
599  ->will($this->returnSelf());
600  $this->urlRewriteFactory
601  ->expects($this->any())
602  ->method('create')
603  ->willReturn($this->urlRewrite);
604 
605  $actualResult = $this->invokeMethod($this->import, 'categoriesUrlRewriteGenerate');
606  $this->assertEquals(
607  [
608  $this->urlRewrite,
609  ],
610  $actualResult
611  );
612  }
613 
620  protected function setPropertyValue($object, $property, $value)
621  {
622  $reflection = new \ReflectionClass(get_class($object));
623  $reflectionProperty = $reflection->getProperty($property);
624  $reflectionProperty->setAccessible(true);
625  $reflectionProperty->setValue($object, $value);
626  }
627 
634  protected function invokeMethod($object, $methodName, array $parameters = [])
635  {
636  $reflection = new \ReflectionClass(get_class($object));
637  $method = $reflection->getMethod($methodName);
638  $method->setAccessible(true);
639 
640  return $method->invokeArgs($object, $parameters);
641  }
642 
653  $storeId,
654  $productId,
655  $requestPath,
656  $targetPath,
657  $redirectType,
658  $metadata,
660  ) {
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')
666  ->with(ProductUrlRewriteGenerator::ENTITY_TYPE)->will($this->returnSelf());
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());
680  }
681 
686  protected function currentUrlRewritesRegeneratorGetCurrentRewritesMocks($currentRewrites)
687  {
688  $rewrites = [];
689  foreach ($currentRewrites as $urlRewrite) {
693  $url = $this->getMockBuilder(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class)
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));
699  }
700  $rewrites[] = $url;
701  }
702  return $rewrites;
703  }
704 }
currentUrlRewritesRegeneratorPrepareUrlRewriteMock( $storeId, $productId, $requestPath, $targetPath, $redirectType, $metadata, $description)
$value
Definition: gender.phtml:16
$method
Definition: info.phtml:13