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 ();
@@ -2681,35 +2690,46 @@ public function testImportWithBackordersDisabled(): void
2681
2690
}
2682
2691
2683
2692
/**
2684
- * Import file by providing import filename in parameters .
2693
+ * Import file by providing import filename and bunch size .
2685
2694
*
2686
2695
* @param string $fileName
2687
- * @return void
2696
+ * @param int $bunchSize
2697
+ * @return bool
2688
2698
*/
2689
- private function importFile (string $ fileName ): void
2699
+ private function importFile (string $ fileName, int $ bunchSize = 100 ): bool
2690
2700
{
2691
- $ filesystem = $ this ->objectManager ->create (\Magento \Framework \Filesystem::class);
2701
+ $ importExportData = $ this ->getMockBuilder (Data::class)
2702
+ ->disableOriginalConstructor ()
2703
+ ->getMock ();
2704
+ $ importExportData ->expects ($ this ->atLeastOnce ())
2705
+ ->method ('getBunchSize ' )
2706
+ ->willReturn ($ bunchSize );
2707
+ $ this ->_model = $ this ->objectManager ->create (
2708
+ ImportProduct::class,
2709
+ ['importExportData ' => $ importExportData ]
2710
+ );
2711
+ $ filesystem = $ this ->objectManager ->create (Filesystem::class);
2692
2712
$ directory = $ filesystem ->getDirectoryWrite (DirectoryList::ROOT );
2693
2713
$ source = $ this ->objectManager ->create (
2694
- \ Magento \ ImportExport \ Model \ Import \ Source \ Csv::class,
2714
+ Csv::class,
2695
2715
[
2696
- 'file ' => __DIR__ . ' / _files/ ' . $ fileName ,
2716
+ 'file ' => __DIR__ . DIRECTORY_SEPARATOR . ' _files ' . DIRECTORY_SEPARATOR . $ fileName ,
2697
2717
'directory ' => $ directory ,
2698
2718
]
2699
2719
);
2700
2720
$ errors = $ this ->_model ->setParameters (
2701
2721
[
2702
- 'behavior ' => \ Magento \ ImportExport \ Model \ Import::BEHAVIOR_APPEND ,
2722
+ 'behavior ' => Import::BEHAVIOR_APPEND ,
2703
2723
'entity ' => 'catalog_product ' ,
2704
- \ Magento \ ImportExport \ Model \ Import::FIELDS_ENCLOSURE => 1 ,
2724
+ Import::FIELDS_ENCLOSURE => 1 ,
2705
2725
]
2706
2726
)
2707
- ->setSource ($ source )
2708
- ->validateData ();
2727
+ ->setSource ($ source )
2728
+ ->validateData ();
2709
2729
2710
- $ this ->assertTrue ($ errors ->getErrorsCount () == 0 );
2730
+ $ this ->assertTrue ($ errors ->getErrorsCount () === 0 );
2711
2731
2712
- $ this ->_model ->importData ();
2732
+ return $ this ->_model ->importData ();
2713
2733
}
2714
2734
2715
2735
/**
@@ -3051,4 +3071,33 @@ public function testEmptyAttributeValueShouldBeIgnoredAfterUpdateProductByImport
3051
3071
$ this ->assertEquals ('Varchar default value ' , $ simpleProduct ->getData ('varchar_attribute ' ));
3052
3072
$ this ->assertEquals ('Short description ' , $ simpleProduct ->getData ('short_description ' ));
3053
3073
}
3074
+
3075
+ /**
3076
+ * Checks possibility to double importing products using the same import file.
3077
+ *
3078
+ * Bunch size is using to test importing the same product that will be chunk to different bunches.
3079
+ * Example:
3080
+ * - first bunch
3081
+ * product-sku,default-store
3082
+ * product-sku,second-store
3083
+ * - second bunch
3084
+ * product-sku,third-store
3085
+ *
3086
+ * @magentoDbIsolation disabled
3087
+ * @magentoDataFixture Magento/Store/_files/core_fixturestore.php
3088
+ * @magentoDataFixture Magento/Store/_files/second_store.php
3089
+ */
3090
+ public function testCheckDoubleImportOfProducts ()
3091
+ {
3092
+ /** @var SearchCriteria $searchCriteria */
3093
+ $ searchCriteria = $ this ->searchCriteriaBuilder ->create ();
3094
+
3095
+ $ this ->assertEquals (true , $ this ->importFile ('products_with_two_store_views.csv ' , 2 ));
3096
+ $ productsAfterFirstImport = $ this ->productRepository ->getList ($ searchCriteria )->getItems ();
3097
+ $ this ->assertEquals (3 , count ($ productsAfterFirstImport ));
3098
+
3099
+ $ this ->assertEquals (true , $ this ->importFile ('products_with_two_store_views.csv ' , 2 ));
3100
+ $ productsAfterSecondImport = $ this ->productRepository ->getList ($ searchCriteria )->getItems ();
3101
+ $ this ->assertEquals (3 , count ($ productsAfterSecondImport ));
3102
+ }
3054
3103
}
0 commit comments