Skip to content

Commit 20b78b2

Browse files
committed
MC-31042: MFTF automated test is flaky when importing CSV
1 parent b899606 commit 20b78b2

File tree

3 files changed

+71
-85
lines changed

3 files changed

+71
-85
lines changed

app/code/Magento/ImportExport/Test/Mftf/Test/AdminCheckDoubleImportOfProductsTest.xml

Lines changed: 0 additions & 73 deletions
This file was deleted.

dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/Import/ProductTest.php

Lines changed: 61 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@
1818
use Magento\CatalogInventory\Model\Stock;
1919
use Magento\CatalogInventory\Model\StockRegistry;
2020
use Magento\CatalogInventory\Model\StockRegistryStorage;
21+
use Magento\Framework\Api\SearchCriteria;
22+
use Magento\Framework\Api\SearchCriteriaBuilder;
2123
use Magento\Framework\App\Bootstrap;
2224
use Magento\Framework\App\Filesystem\DirectoryList;
2325
use Magento\Framework\App\ObjectManager;
2426
use Magento\Framework\DataObject;
2527
use Magento\Framework\Exception\NoSuchEntityException;
2628
use Magento\Framework\Filesystem;
2729
use Magento\Framework\Registry;
30+
use Magento\ImportExport\Helper\Data;
2831
use Magento\ImportExport\Model\Import;
2932
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;
3033
use Magento\ImportExport\Model\Import\Source\Csv;
@@ -79,6 +82,11 @@ class ProductTest extends \Magento\TestFramework\Indexer\TestCase
7982
*/
8083
private $logger;
8184

85+
/**
86+
* @var SearchCriteriaBuilder
87+
*/
88+
private $searchCriteriaBuilder;
89+
8290
/**
8391
* @var ProductRepositoryInterface
8492
*/
@@ -98,6 +106,7 @@ protected function setUp()
98106
['logger' => $this->logger]
99107
);
100108
$this->importedProducts = [];
109+
$this->searchCriteriaBuilder = $this->objectManager->get(SearchCriteriaBuilder::class);
101110
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
102111

103112
parent::setUp();
@@ -2680,35 +2689,46 @@ public function testImportWithBackordersDisabled(): void
26802689
}
26812690

26822691
/**
2683-
* Import file by providing import filename in parameters.
2692+
* Import file by providing import filename and bunch size.
26842693
*
26852694
* @param string $fileName
2686-
* @return void
2695+
* @param int $bunchSize
2696+
* @return bool
26872697
*/
2688-
private function importFile(string $fileName): void
2698+
private function importFile(string $fileName, int $bunchSize = 100): bool
26892699
{
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);
26912711
$directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
26922712
$source = $this->objectManager->create(
2693-
\Magento\ImportExport\Model\Import\Source\Csv::class,
2713+
Csv::class,
26942714
[
2695-
'file' => __DIR__ . '/_files/' . $fileName,
2715+
'file' => __DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . $fileName,
26962716
'directory' => $directory,
26972717
]
26982718
);
26992719
$errors = $this->_model->setParameters(
27002720
[
2701-
'behavior' => \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND,
2721+
'behavior' => Import::BEHAVIOR_APPEND,
27022722
'entity' => 'catalog_product',
2703-
\Magento\ImportExport\Model\Import::FIELDS_ENCLOSURE => 1,
2723+
Import::FIELDS_ENCLOSURE => 1,
27042724
]
27052725
)
2706-
->setSource($source)
2707-
->validateData();
2726+
->setSource($source)
2727+
->validateData();
27082728

2709-
$this->assertTrue($errors->getErrorsCount() == 0);
2729+
$this->assertTrue($errors->getErrorsCount() === 0);
27102730

2711-
$this->_model->importData();
2731+
return $this->_model->importData();
27122732
}
27132733

27142734
/**
@@ -3050,4 +3070,33 @@ public function testEmptyAttributeValueShouldBeIgnoredAfterUpdateProductByImport
30503070
$this->assertEquals('Varchar default value', $simpleProduct->getData('varchar_attribute'));
30513071
$this->assertEquals('Short description', $simpleProduct->getData('short_description'));
30523072
}
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+
}
30533102
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
sku,name,store_view_code,product_type,attribute_set_code,price,custom_options
2+
simple1,Simple 1,,simple,Default,100,"name=Test Option 1,type=field"
3+
simple1,Simple 1,fixturestore,simple,Default,,"name=Test Option 1,type=field"
4+
simple1,Simple 1,fixture_second_store,simple,Default,,"name=Test Option 1,type=field"
5+
simple2,Simple 2,,simple,Default,200,"name=Test Option 1,type=field"
6+
simple2,Simple 2,fixturestore,simple,Default,,"name=Test Option 1,type=field"
7+
simple2,Simple 2,fixture_second_store,simple,Default,,"name=Test Option 1,type=field"
8+
simple3,Simple 3,,simple,Default,300,"name=Test Option 1,type=field"
9+
simple3,Simple 3,fixturestore,simple,Default,,"name=Test Option 1,type=field"
10+
simple3,Simple 3,fixture_second_store,simple,Default,,"name=Test Option 1,type=field"

0 commit comments

Comments
 (0)