Skip to content

Commit 0df640e

Browse files
committed
MC-31008: MFTF automated test is flaky when importing CSV
1 parent cde4f67 commit 0df640e

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();
@@ -2681,35 +2690,46 @@ public function testImportWithBackordersDisabled(): void
26812690
}
26822691

26832692
/**
2684-
* Import file by providing import filename in parameters.
2693+
* Import file by providing import filename and bunch size.
26852694
*
26862695
* @param string $fileName
2687-
* @return void
2696+
* @param int $bunchSize
2697+
* @return bool
26882698
*/
2689-
private function importFile(string $fileName): void
2699+
private function importFile(string $fileName, int $bunchSize = 100): bool
26902700
{
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);
26922712
$directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
26932713
$source = $this->objectManager->create(
2694-
\Magento\ImportExport\Model\Import\Source\Csv::class,
2714+
Csv::class,
26952715
[
2696-
'file' => __DIR__ . '/_files/' . $fileName,
2716+
'file' => __DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . $fileName,
26972717
'directory' => $directory,
26982718
]
26992719
);
27002720
$errors = $this->_model->setParameters(
27012721
[
2702-
'behavior' => \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND,
2722+
'behavior' => Import::BEHAVIOR_APPEND,
27032723
'entity' => 'catalog_product',
2704-
\Magento\ImportExport\Model\Import::FIELDS_ENCLOSURE => 1,
2724+
Import::FIELDS_ENCLOSURE => 1,
27052725
]
27062726
)
2707-
->setSource($source)
2708-
->validateData();
2727+
->setSource($source)
2728+
->validateData();
27092729

2710-
$this->assertTrue($errors->getErrorsCount() == 0);
2730+
$this->assertTrue($errors->getErrorsCount() === 0);
27112731

2712-
$this->_model->importData();
2732+
return $this->_model->importData();
27132733
}
27142734

27152735
/**
@@ -3051,4 +3071,33 @@ public function testEmptyAttributeValueShouldBeIgnoredAfterUpdateProductByImport
30513071
$this->assertEquals('Varchar default value', $simpleProduct->getData('varchar_attribute'));
30523072
$this->assertEquals('Short description', $simpleProduct->getData('short_description'));
30533073
}
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+
}
30543103
}
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)