Skip to content

Commit 8da94cb

Browse files
ACPT-820
Fixing static, integration, and unit test failures
1 parent c1b596e commit 8da94cb

File tree

12 files changed

+142
-196
lines changed

12 files changed

+142
-196
lines changed

app/code/Magento/GroupedImportExport/Model/Import/Product/Type/Grouped.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public function saveData()
143143
}
144144
}
145145
}
146-
$this->links->saveLinksData($linksData);
146+
$this->links->saveLinksData($linksData, $this->_entityModel);
147147
}
148148
return $this;
149149
}

app/code/Magento/GroupedImportExport/Model/Import/Product/Type/Grouped/Links.php

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\GroupedImportExport\Model\Import\Product\Type\Grouped;
77

8+
use Magento\CatalogImportExport\Model\Import\Product as ProductImport;
89
use Magento\Framework\App\ResourceConnection;
910

1011
/**
@@ -24,6 +25,8 @@ class Links
2425

2526
/**
2627
* @var \Magento\ImportExport\Model\ImportFactory
28+
* @deprecated
29+
* @see no longer used
2730
*/
2831
protected $importFactory;
2932

@@ -55,16 +58,19 @@ public function __construct(
5558
}
5659

5760
/**
61+
* Saves the linksData to database
62+
*
5863
* @param array $linksData
64+
* @param ProductImport $productImport
5965
* @return void
6066
*/
61-
public function saveLinksData($linksData)
67+
public function saveLinksData(array $linksData, ProductImport $productImport)
6268
{
6369
$mainTable = $this->productLink->getMainTable();
6470
$relationTable = $this->productLink->getTable('catalog_product_relation');
6571
// save links and relations
6672
if ($linksData['product_ids']) {
67-
$this->deleteOldLinks(array_keys($linksData['product_ids']));
73+
$this->deleteOldLinks(array_keys($linksData['product_ids']), $productImport);
6874
$mainData = [];
6975
foreach ($linksData['relation'] as $productData) {
7076
$mainData[] = [
@@ -76,7 +82,6 @@ public function saveLinksData($linksData)
7682
$this->connection->insertOnDuplicate($mainTable, $mainData);
7783
$this->connection->insertOnDuplicate($relationTable, $linksData['relation']);
7884
}
79-
8085
$attributes = $this->getAttributes();
8186
// save positions and default quantity
8287
if ($linksData['attr_product_ids']) {
@@ -107,13 +112,16 @@ public function saveLinksData($linksData)
107112
}
108113

109114
/**
115+
* Deletes all the product links in database that are linked to productIds
116+
*
110117
* @param array $productIds
118+
* @param ProductImport $productImport
111119
* @throws \Magento\Framework\Exception\LocalizedException
112120
* @return void
113121
*/
114-
protected function deleteOldLinks($productIds)
122+
protected function deleteOldLinks($productIds, ProductImport $productImport)
115123
{
116-
if ($this->getBehavior() != \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND) {
124+
if ($this->getBehavior($productImport) != \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND) {
117125
$this->connection->delete(
118126
$this->productLink->getMainTable(),
119127
$this->connection->quoteInto(
@@ -125,6 +133,8 @@ protected function deleteOldLinks($productIds)
125133
}
126134

127135
/**
136+
* Gets all the attributes from database for the Grouped Link Type
137+
*
128138
* @return array
129139
*/
130140
public function getAttributes()
@@ -145,6 +155,8 @@ public function getAttributes()
145155
}
146156

147157
/**
158+
* Returns the integer id for Link Type
159+
*
148160
* @return int
149161
*/
150162
protected function getLinkTypeId()
@@ -155,12 +167,15 @@ protected function getLinkTypeId()
155167
/**
156168
* Retrieve model behavior
157169
*
170+
* @param ProductImport $productImport
158171
* @return string
159172
*/
160-
protected function getBehavior()
173+
protected function getBehavior(ProductImport $productImport)
161174
{
162175
if ($this->behavior === null) {
163-
$this->behavior = $this->importFactory->create()->getDataSourceModel()->getBehavior();
176+
$ids = $productImport->getIds();
177+
$dataSourceModel = $productImport->getDataSourceModel();
178+
$this->behavior = $dataSourceModel->getBehavior($ids);
164179
}
165180
return $this->behavior;
166181
}

app/code/Magento/GroupedImportExport/Test/Unit/Model/Import/Product/Type/Grouped/LinksTest.php

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Magento\ImportExport\Model\ResourceModel\Import\Data;
2020
use PHPUnit\Framework\MockObject\MockObject;
2121
use PHPUnit\Framework\TestCase;
22+
use Magento\CatalogImportExport\Model\Import\Product as ProductImport;
2223

2324
class LinksTest extends TestCase
2425
{
@@ -28,20 +29,20 @@ class LinksTest extends TestCase
2829
/** @var ObjectManagerHelper */
2930
protected $objectManagerHelper;
3031

31-
/** @var Link|MockObject */
32+
/** @var Link&MockObject */
3233
protected $link;
3334

34-
/** @var ResourceConnection|MockObject */
35+
/** @var ResourceConnection&MockObject */
3536
protected $resource;
3637

3738
/** @var Mysql */
3839
protected $connection;
3940

40-
/** @var ImportFactory|MockObject */
41+
/** @var ImportFactory&MockObject */
4142
protected $importFactory;
4243

43-
/** @var Import|MockObject */
44-
protected $import;
44+
/** @var ProductImport&MockObject */
45+
protected $productImport;
4546

4647
protected function setUp(): void
4748
{
@@ -52,11 +53,7 @@ protected function setUp(): void
5253
->expects($this->once())
5354
->method('getConnection')
5455
->willReturn($this->connection);
55-
56-
$this->import = $this->createMock(Import::class);
5756
$this->importFactory = $this->createPartialMock(ImportFactory::class, ['create']);
58-
$this->importFactory->expects($this->any())->method('create')->willReturn($this->import);
59-
6057
$this->objectManagerHelper = new ObjectManagerHelper($this);
6158
$this->links = $this->objectManagerHelper->getObject(
6259
Links::class,
@@ -66,6 +63,8 @@ protected function setUp(): void
6663
'importFactory' => $this->importFactory
6764
]
6865
);
66+
$this->productImport = $this->createMock(ProductImport::class);
67+
$this->productImport->expects($this->any())->method('getIds')->willReturn(null);
6968
}
7069

7170
/**
@@ -95,7 +94,7 @@ public function testSaveLinksDataNoProductsAttrs($linksData)
9594
$attributes = $this->attributesDataProvider();
9695
$this->processAttributeGetter($attributes[2]['dbAttributes']);
9796
$this->connection->expects($this->exactly(2))->method('insertOnDuplicate');
98-
$this->links->saveLinksData($linksData);
97+
$this->links->saveLinksData($linksData, $this->productImport);
9998
}
10099

101100
/**
@@ -125,8 +124,7 @@ public function testSaveLinksDataWithProductsAttrs($linksData)
125124
$this->link->expects($this->exactly(2))->method('getAttributeTypeTable')->willReturn(
126125
'table_name'
127126
);
128-
129-
$this->links->saveLinksData($linksData);
127+
$this->links->saveLinksData($linksData, $this->productImport);
130128
}
131129

132130
/**
@@ -197,6 +195,6 @@ protected function processBehaviorGetter($behavior)
197195
{
198196
$dataSource = $this->createMock(Data::class);
199197
$dataSource->expects($this->once())->method('getBehavior')->willReturn($behavior);
200-
$this->import->expects($this->once())->method('getDataSourceModel')->willReturn($dataSource);
198+
$this->productImport->expects($this->any())->method('getDataSourceModel')->willReturn($dataSource);
201199
}
202200
}

app/code/Magento/ImportExport/Model/Import.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ public function importSource()
484484
if (empty($ids)) {
485485
$idsFromPostData = $this->getData(self::FIELD_IMPORT_IDS);
486486
if (null !== $idsFromPostData && '' !== $idsFromPostData) {
487-
$ids = explode(",", $idsFromPostData);;
487+
$ids = explode(",", $idsFromPostData);
488488
}
489489
}
490490
$this->setData('entity', $this->getDataSourceModel()->getEntityTypeCode($ids));

app/code/Magento/ImportExport/Model/Import/Entity/AbstractEntity.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,4 +917,14 @@ public function setIds(array $ids)
917917
{
918918
$this->ids = $ids;
919919
}
920+
921+
/**
922+
* Gets the currently used DataSourceModel
923+
*
924+
* @return array
925+
*/
926+
public function getDataSourceModel()
927+
{
928+
return $this->_dataSourceModel;
929+
}
920930
}

app/code/Magento/ImportExport/Model/ResourceModel/Import/Data.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public function cleanProcessedBunches()
125125
{
126126
$this->getConnection()->delete(
127127
$this->getMainTable(),
128-
'is_processed = 1 OR TIMESTAMPADD(DAY, 1, updated_at) < NOW() '
128+
'is_processed = 1 OR TIMESTAMPADD(DAY, 1, updated_at) < CURRENT_TIMESTAMP() '
129129
);
130130
}
131131

dev/tests/integration/testsuite/Magento/BundleImportExport/Model/Import/Product/Type/BundleTest.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,6 @@ class BundleTest extends \Magento\TestFramework\Indexer\TestCase
3939
*/
4040
private const TEST_PRODUCT_TYPE = 'bundle';
4141

42-
/**
43-
* @var \Magento\CatalogImportExport\Model\Import\Product
44-
*/
45-
protected $model;
46-
4742
/**
4843
* @var \Magento\Framework\ObjectManagerInterface
4944
*/
@@ -77,7 +72,6 @@ public static function setUpBeforeClass(): void
7772
protected function setUp(): void
7873
{
7974
$this->objectManager = Bootstrap::getObjectManager();
80-
$this->model = $this->objectManager->create(\Magento\CatalogImportExport\Model\Import\Product::class);
8175
}
8276

8377
/**
@@ -397,15 +391,15 @@ private function doImport(
397391
bool $validateOnly = false
398392
): ProcessingErrorAggregatorInterface {
399393
/** @var Filesystem $filesystem */
400-
$filesystem =$this->objectManager->create(Filesystem::class);
394+
$filesystem = $this->objectManager->create(Filesystem::class);
401395
$directoryWrite = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
402396
$source = ImportAdapter::findAdapterFor($file, $directoryWrite);
403-
$errors = $this->model
404-
->setParameters(['behavior' => $behavior, 'entity' => 'catalog_product'])
405-
->setSource($source)
406-
->validateData();
397+
$model = $this->objectManager->create(\Magento\CatalogImportExport\Model\Import\Product::class);
398+
$model->setParameters(['behavior' => $behavior, 'entity' => 'catalog_product']);
399+
$model->setSource($source);
400+
$errors = $model->validateData();
407401
if (!$validateOnly && !$errors->getAllErrors()) {
408-
$this->model->importData();
402+
$model->importData();
409403
}
410404
return $errors;
411405
}

dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/AbstractProductExportImportTestCase.php

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,6 @@ private function exportProducts(Product $exportProduct = null)
408408
{
409409
$csvfile = uniqid('importexport_') . '.csv';
410410
$this->csvFile = $csvfile;
411-
412411
$exportProduct = $exportProduct ?: $this->objectManager->create(
413412
Product::class
414413
);
@@ -419,10 +418,8 @@ private function exportProducts(Product $exportProduct = null)
419418
$exportProduct->setWriter($writer);
420419
$content = $exportProduct->export();
421420
$this->assertNotEmpty($content);
422-
423421
$directory = $this->fileSystem->getDirectoryWrite(DirectoryList::VAR_IMPORT_EXPORT);
424422
$directory->getDriver()->filePutContents($directory->getAbsolutePath($csvfile), $content);
425-
426423
return $csvfile;
427424
}
428425

@@ -447,15 +444,12 @@ private function importProducts(string $csvfile, string $behavior): void
447444
'directory' => $directory
448445
]
449446
);
450-
451447
$appParams = \Magento\TestFramework\Helper\Bootstrap::getInstance()->getBootstrap()
452448
->getApplication()
453449
->getInitParams()[Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS];
454450
$mediaDirectory = $this->fileSystem->getDirectoryWrite(DirectoryList::MEDIA);
455-
456451
$mediaDir = $mediaDirectory->getDriver() instanceof File ?
457452
$appParams[DirectoryList::MEDIA][DirectoryList::PATH] : 'media';
458-
459453
$mediaDirectory->create('catalog/product');
460454
$mediaDirectory->create('import');
461455
$importModel->setParameters(
@@ -466,17 +460,13 @@ private function importProducts(string $csvfile, string $behavior): void
466460
$uploader = $importModel->getUploader();
467461
$this->assertTrue($uploader->setDestDir($mediaDir . '/catalog/product'));
468462
$this->assertTrue($uploader->setTmpDir($mediaDir . '/import'));
469-
470-
$errors = $importModel->setParameters(
471-
[
472-
'behavior' => $behavior,
473-
'entity' => 'catalog_product',
474-
]
475-
)->setSource(
476-
$source
477-
)->validateData();
463+
$importModel->setParameters([
464+
'behavior' => $behavior,
465+
'entity' => 'catalog_product',
466+
]);
467+
$importModel->setSource($source);
468+
$errors = $importModel->validateData();
478469
$errorMessage = $this->extractErrorMessage($errors->getAllErrors());
479-
480470
$this->assertEmpty(
481471
$errorMessage,
482472
'Product import from file ' . $csvfile . ' validation errors: ' . $errorMessage
@@ -502,7 +492,6 @@ private function extractErrorMessage(array $errors): string
502492
foreach ($errors as $error) {
503493
$errorMessage = "\n" . $error->getErrorMessage();
504494
}
505-
506495
return $errorMessage;
507496
}
508497

0 commit comments

Comments
 (0)