Skip to content

Commit 528ed74

Browse files
authored
Merge pull request #7926 from magento-performance/ACPT-757&ACPT-773&ACPT-747&ACPT-748&ACPT-675&ACPT-671&ACPT-746
2 parents 78dd065 + c9c60a4 commit 528ed74

File tree

31 files changed

+1354
-955
lines changed

31 files changed

+1354
-955
lines changed

app/code/Magento/CatalogImportExport/Model/Import/Product.php

Lines changed: 491 additions & 392 deletions
Large diffs are not rendered by default.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogImportExport\Model\Import\Product;
9+
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Framework\Phrase;
12+
13+
class Skip extends LocalizedException
14+
{
15+
}

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 & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
namespace Magento\GroupedImportExport\Test\Unit\Model\Import\Product\Type\Grouped;
1010

1111
use Magento\Catalog\Model\ResourceModel\Product\Link;
12+
use Magento\CatalogImportExport\Model\Import\Product as ProductImport;
1213
use Magento\Framework\App\ResourceConnection;
1314
use Magento\Framework\DB\Adapter\Pdo\Mysql;
1415
use Magento\Framework\DB\Select;
1516
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
1617
use Magento\GroupedImportExport\Model\Import\Product\Type\Grouped\Links;
17-
use Magento\ImportExport\Model\Import;
1818
use Magento\ImportExport\Model\ImportFactory;
1919
use Magento\ImportExport\Model\ResourceModel\Import\Data;
2020
use PHPUnit\Framework\MockObject\MockObject;
@@ -28,20 +28,20 @@ class LinksTest extends TestCase
2828
/** @var ObjectManagerHelper */
2929
protected $objectManagerHelper;
3030

31-
/** @var Link|MockObject */
31+
/** @var Link&MockObject */
3232
protected $link;
3333

34-
/** @var ResourceConnection|MockObject */
34+
/** @var ResourceConnection&MockObject */
3535
protected $resource;
3636

3737
/** @var Mysql */
3838
protected $connection;
3939

40-
/** @var ImportFactory|MockObject */
40+
/** @var ImportFactory&MockObject */
4141
protected $importFactory;
4242

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

4646
protected function setUp(): void
4747
{
@@ -52,11 +52,7 @@ protected function setUp(): void
5252
->expects($this->once())
5353
->method('getConnection')
5454
->willReturn($this->connection);
55-
56-
$this->import = $this->createMock(Import::class);
5755
$this->importFactory = $this->createPartialMock(ImportFactory::class, ['create']);
58-
$this->importFactory->expects($this->any())->method('create')->willReturn($this->import);
59-
6056
$this->objectManagerHelper = new ObjectManagerHelper($this);
6157
$this->links = $this->objectManagerHelper->getObject(
6258
Links::class,
@@ -66,6 +62,8 @@ protected function setUp(): void
6662
'importFactory' => $this->importFactory
6763
]
6864
);
65+
$this->productImport = $this->createMock(ProductImport::class);
66+
$this->productImport->expects($this->any())->method('getIds')->willReturn([]);
6967
}
7068

7169
/**
@@ -95,7 +93,7 @@ public function testSaveLinksDataNoProductsAttrs($linksData)
9593
$attributes = $this->attributesDataProvider();
9694
$this->processAttributeGetter($attributes[2]['dbAttributes']);
9795
$this->connection->expects($this->exactly(2))->method('insertOnDuplicate');
98-
$this->links->saveLinksData($linksData);
96+
$this->links->saveLinksData($linksData, $this->productImport);
9997
}
10098

10199
/**
@@ -125,8 +123,7 @@ public function testSaveLinksDataWithProductsAttrs($linksData)
125123
$this->link->expects($this->exactly(2))->method('getAttributeTypeTable')->willReturn(
126124
'table_name'
127125
);
128-
129-
$this->links->saveLinksData($linksData);
126+
$this->links->saveLinksData($linksData, $this->productImport);
130127
}
131128

132129
/**
@@ -197,6 +194,6 @@ protected function processBehaviorGetter($behavior)
197194
{
198195
$dataSource = $this->createMock(Data::class);
199196
$dataSource->expects($this->once())->method('getBehavior')->willReturn($behavior);
200-
$this->import->expects($this->once())->method('getDataSourceModel')->willReturn($dataSource);
197+
$this->productImport->expects($this->any())->method('getDataSourceModel')->willReturn($dataSource);
201198
}
202199
}

app/code/Magento/ImportExport/Block/Adminhtml/Import/Edit/Form.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,6 @@ protected function _prepareForm()
209209
);
210210
$fieldsets[$behaviorCode] = $fieldset;
211211
}
212-
213212
// fieldset for file uploading
214213
$fieldset = $form->addFieldset(
215214
'upload_file_fieldset',
@@ -255,11 +254,19 @@ protected function _prepareForm()
255254
),
256255
]
257256
);
257+
$fieldset->addField(
258+
Import::FIELD_IMPORT_IDS,
259+
'hidden',
260+
[
261+
'name' => Import::FIELD_IMPORT_IDS,
262+
'label' => __('Import id'),
263+
'title' => __('Import id'),
264+
'value' => '',
265+
]
266+
);
258267
$fieldsets['upload'] = $fieldset;
259-
260268
$form->setUseContainer(true);
261269
$this->setForm($form);
262-
263270
return parent::_prepareForm();
264271
}
265272

app/code/Magento/ImportExport/Controller/Adminhtml/Import/Validate.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ public function execute()
4242
//phpcs:disable Magento2.Security.Superglobal
4343
if ($data) {
4444
// common actions
45-
$resultBlock->addAction(
46-
'show',
47-
'import_validation_container'
48-
);
49-
45+
$resultBlock->addAction('show', 'import_validation_container');
5046
$import = $this->getImport()->setData($data);
5147
try {
5248
$source = $import->uploadFileAndGetSource();
5349
$this->processValidationResult($import->validateSource($source), $resultBlock);
50+
$ids = $import->getValidatedIds();
51+
if (count($ids) > 0) {
52+
$resultBlock->addAction('value', Import::FIELD_IMPORT_IDS, $ids);
53+
}
5454
} catch (\Magento\Framework\Exception\LocalizedException $e) {
5555
$resultBlock->addError($e->getMessage());
5656
} catch (\Exception $e) {
@@ -117,7 +117,6 @@ private function processValidationResult($validationResult, $resultBlock)
117117
* Provides import model.
118118
*
119119
* @return Import
120-
* @deprecated 100.1.0
121120
*/
122121
private function getImport()
123122
{

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
use Magento\ImportExport\Model\Import\ConfigInterface;
2929
use Magento\ImportExport\Model\Import\Entity\AbstractEntity;
3030
use Magento\ImportExport\Model\Import\Entity\Factory;
31+
use Magento\ImportExport\Model\Import\EntityInterface;
3132
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingError;
3233
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;
33-
use Magento\ImportExport\Model\Import\Source\Base64EncodedCsvData;
3434
use Magento\ImportExport\Model\ResourceModel\Import\Data;
3535
use Magento\ImportExport\Model\Source\Import\AbstractBehavior;
3636
use Magento\ImportExport\Model\Source\Import\Behavior\Factory as BehaviorFactory;
@@ -98,6 +98,11 @@ class Import extends AbstractModel
9898
*/
9999
public const FIELD_EMPTY_ATTRIBUTE_VALUE_CONSTANT = '_import_empty_attribute_value_constant';
100100

101+
/**
102+
* Id of the `importexport_importdata` row after validation.
103+
*/
104+
public const FIELD_IMPORT_IDS = '_import_ids';
105+
101106
/**
102107
* Allow multiple values wrapping in double quotes for additional attributes.
103108
*/
@@ -118,7 +123,7 @@ class Import extends AbstractModel
118123
public const IMPORT_DIR = 'import/';
119124

120125
/**
121-
* @var AbstractEntity|ImportAbstractEntity
126+
* @var EntityInterface
122127
*/
123128
protected $_entityAdapter;
124129

@@ -272,7 +277,7 @@ public function __construct(
272277
* Create instance of entity adapter and return it
273278
*
274279
* @throws LocalizedException
275-
* @return AbstractEntity|ImportAbstractEntity
280+
* @return EntityInterface
276281
*/
277282
protected function _getEntityAdapter()
278283
{
@@ -476,6 +481,13 @@ public function getWorkingDir()
476481
public function importSource()
477482
{
478483
$ids = $this->_getEntityAdapter()->getIds();
484+
if (empty($ids)) {
485+
$idsFromPostData = $this->getData(self::FIELD_IMPORT_IDS);
486+
if (null !== $idsFromPostData && '' !== $idsFromPostData) {
487+
$ids = explode(",", $idsFromPostData);
488+
$this->_getEntityAdapter()->setIds($ids);
489+
}
490+
}
479491
$this->setData('entity', $this->getDataSourceModel()->getEntityTypeCode($ids));
480492
$this->setData('behavior', $this->getDataSourceModel()->getBehavior($ids));
481493

@@ -852,4 +864,14 @@ public function getDeletedItemsCount()
852864
{
853865
return $this->_getEntityAdapter()->getDeletedItemsCount();
854866
}
867+
868+
/**
869+
* Retrieve Ids of Validated Rows
870+
*
871+
* @return int[]
872+
*/
873+
public function getValidatedIds() : array
874+
{
875+
return $this->_getEntityAdapter()->getIds() ?? [];
876+
}
855877
}

0 commit comments

Comments
 (0)