18
18
use Magento \CatalogInventory \Model \Stock ;
19
19
use Magento \CatalogInventory \Model \StockRegistry ;
20
20
use Magento \CatalogInventory \Model \StockRegistryStorage ;
21
+ use Magento \Framework \Api \SearchCriteria ;
22
+ use Magento \Framework \Api \SearchCriteriaBuilder ;
21
23
use Magento \Framework \App \Bootstrap ;
22
24
use Magento \Framework \App \Filesystem \DirectoryList ;
23
25
use Magento \Framework \App \ObjectManager ;
24
26
use Magento \Framework \DataObject ;
25
27
use Magento \Framework \Exception \NoSuchEntityException ;
26
28
use Magento \Framework \Filesystem ;
27
29
use Magento \Framework \Registry ;
30
+ use Magento \ImportExport \Helper \Data ;
28
31
use Magento \ImportExport \Model \Import ;
29
32
use Magento \ImportExport \Model \Import \ErrorProcessing \ProcessingErrorAggregatorInterface ;
30
33
use Magento \ImportExport \Model \Import \Source \Csv ;
@@ -79,6 +82,11 @@ class ProductTest extends \Magento\TestFramework\Indexer\TestCase
79
82
*/
80
83
private $ logger ;
81
84
85
+ /**
86
+ * @var SearchCriteriaBuilder
87
+ */
88
+ private $ searchCriteriaBuilder ;
89
+
82
90
/**
83
91
* @var ProductRepositoryInterface
84
92
*/
@@ -98,6 +106,7 @@ protected function setUp()
98
106
['logger ' => $ this ->logger ]
99
107
);
100
108
$ this ->importedProducts = [];
109
+ $ this ->searchCriteriaBuilder = $ this ->objectManager ->get (SearchCriteriaBuilder::class);
101
110
$ this ->productRepository = $ this ->objectManager ->get (ProductRepositoryInterface::class);
102
111
103
112
parent ::setUp ();
@@ -2680,35 +2689,46 @@ public function testImportWithBackordersDisabled(): void
2680
2689
}
2681
2690
2682
2691
/**
2683
- * Import file by providing import filename in parameters .
2692
+ * Import file by providing import filename and bunch size .
2684
2693
*
2685
2694
* @param string $fileName
2686
- * @return void
2695
+ * @param int $bunchSize
2696
+ * @return bool
2687
2697
*/
2688
- private function importFile (string $ fileName ): void
2698
+ private function importFile (string $ fileName, int $ bunchSize = 100 ): bool
2689
2699
{
2690
- $ filesystem = $ this ->objectManager ->create (\Magento \Framework \Filesystem::class);
2700
+ $ importExportData = $ this ->getMockBuilder (Data::class)
2701
+ ->disableOriginalConstructor ()
2702
+ ->getMock ();
2703
+ $ importExportData ->expects ($ this ->atLeastOnce ())
2704
+ ->method ('getBunchSize ' )
2705
+ ->willReturn ($ bunchSize );
2706
+ $ this ->_model = $ this ->objectManager ->create (
2707
+ ImportProduct::class,
2708
+ ['importExportData ' => $ importExportData ]
2709
+ );
2710
+ $ filesystem = $ this ->objectManager ->create (Filesystem::class);
2691
2711
$ directory = $ filesystem ->getDirectoryWrite (DirectoryList::ROOT );
2692
2712
$ source = $ this ->objectManager ->create (
2693
- \ Magento \ ImportExport \ Model \ Import \ Source \ Csv::class,
2713
+ Csv::class,
2694
2714
[
2695
- 'file ' => __DIR__ . ' / _files/ ' . $ fileName ,
2715
+ 'file ' => __DIR__ . DIRECTORY_SEPARATOR . ' _files ' . DIRECTORY_SEPARATOR . $ fileName ,
2696
2716
'directory ' => $ directory ,
2697
2717
]
2698
2718
);
2699
2719
$ errors = $ this ->_model ->setParameters (
2700
2720
[
2701
- 'behavior ' => \ Magento \ ImportExport \ Model \ Import::BEHAVIOR_APPEND ,
2721
+ 'behavior ' => Import::BEHAVIOR_APPEND ,
2702
2722
'entity ' => 'catalog_product ' ,
2703
- \ Magento \ ImportExport \ Model \ Import::FIELDS_ENCLOSURE => 1 ,
2723
+ Import::FIELDS_ENCLOSURE => 1 ,
2704
2724
]
2705
2725
)
2706
- ->setSource ($ source )
2707
- ->validateData ();
2726
+ ->setSource ($ source )
2727
+ ->validateData ();
2708
2728
2709
- $ this ->assertTrue ($ errors ->getErrorsCount () == 0 );
2729
+ $ this ->assertTrue ($ errors ->getErrorsCount () === 0 );
2710
2730
2711
- $ this ->_model ->importData ();
2731
+ return $ this ->_model ->importData ();
2712
2732
}
2713
2733
2714
2734
/**
@@ -3050,4 +3070,33 @@ public function testEmptyAttributeValueShouldBeIgnoredAfterUpdateProductByImport
3050
3070
$ this ->assertEquals ('Varchar default value ' , $ simpleProduct ->getData ('varchar_attribute ' ));
3051
3071
$ this ->assertEquals ('Short description ' , $ simpleProduct ->getData ('short_description ' ));
3052
3072
}
3073
+
3074
+ /**
3075
+ * Checks possibility to double importing products using the same import file.
3076
+ *
3077
+ * Bunch size is using to test importing the same product that will be chunk to different bunches.
3078
+ * Example:
3079
+ * - first bunch
3080
+ * product-sku,default-store
3081
+ * product-sku,second-store
3082
+ * - second bunch
3083
+ * product-sku,third-store
3084
+ *
3085
+ * @magentoDbIsolation disabled
3086
+ * @magentoDataFixture Magento/Store/_files/core_fixturestore.php
3087
+ * @magentoDataFixture Magento/Store/_files/second_store.php
3088
+ */
3089
+ public function testCheckDoubleImportOfProducts ()
3090
+ {
3091
+ /** @var SearchCriteria $searchCriteria */
3092
+ $ searchCriteria = $ this ->searchCriteriaBuilder ->create ();
3093
+
3094
+ $ this ->assertEquals (true , $ this ->importFile ('products_with_two_store_views.csv ' , 2 ));
3095
+ $ productsAfterFirstImport = $ this ->productRepository ->getList ($ searchCriteria )->getItems ();
3096
+ $ this ->assertEquals (3 , count ($ productsAfterFirstImport ));
3097
+
3098
+ $ this ->assertEquals (true , $ this ->importFile ('products_with_two_store_views.csv ' , 2 ));
3099
+ $ productsAfterSecondImport = $ this ->productRepository ->getList ($ searchCriteria )->getItems ();
3100
+ $ this ->assertEquals (3 , count ($ productsAfterSecondImport ));
3101
+ }
3053
3102
}
0 commit comments