Skip to content

Commit

Permalink
Merge pull request #7926 from magento-performance/ACPT-757&ACPT-773&A…
Browse files Browse the repository at this point in the history
…CPT-747&ACPT-748&ACPT-675&ACPT-671&ACPT-746
  • Loading branch information
adifucan authored Dec 4, 2022
2 parents 78dd065 + c9c60a4 commit 528ed74
Show file tree
Hide file tree
Showing 31 changed files with 1,354 additions and 955 deletions.
883 changes: 491 additions & 392 deletions app/code/Magento/CatalogImportExport/Model/Import/Product.php

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions app/code/Magento/CatalogImportExport/Model/Import/Product/Skip.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogImportExport\Model\Import\Product;

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Phrase;

class Skip extends LocalizedException
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public function saveData()
}
}
}
$this->links->saveLinksData($linksData);
$this->links->saveLinksData($linksData, $this->_entityModel);
}
return $this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
namespace Magento\GroupedImportExport\Model\Import\Product\Type\Grouped;

use Magento\CatalogImportExport\Model\Import\Product as ProductImport;
use Magento\Framework\App\ResourceConnection;

/**
Expand All @@ -24,6 +25,8 @@ class Links

/**
* @var \Magento\ImportExport\Model\ImportFactory
* @deprecated
* @see no longer used
*/
protected $importFactory;

Expand Down Expand Up @@ -55,16 +58,19 @@ public function __construct(
}

/**
* Saves the linksData to database
*
* @param array $linksData
* @param ProductImport $productImport
* @return void
*/
public function saveLinksData($linksData)
public function saveLinksData(array $linksData, ProductImport $productImport)
{
$mainTable = $this->productLink->getMainTable();
$relationTable = $this->productLink->getTable('catalog_product_relation');
// save links and relations
if ($linksData['product_ids']) {
$this->deleteOldLinks(array_keys($linksData['product_ids']));
$this->deleteOldLinks(array_keys($linksData['product_ids']), $productImport);
$mainData = [];
foreach ($linksData['relation'] as $productData) {
$mainData[] = [
Expand All @@ -76,7 +82,6 @@ public function saveLinksData($linksData)
$this->connection->insertOnDuplicate($mainTable, $mainData);
$this->connection->insertOnDuplicate($relationTable, $linksData['relation']);
}

$attributes = $this->getAttributes();
// save positions and default quantity
if ($linksData['attr_product_ids']) {
Expand Down Expand Up @@ -107,13 +112,16 @@ public function saveLinksData($linksData)
}

/**
* Deletes all the product links in database that are linked to productIds
*
* @param array $productIds
* @param ProductImport $productImport
* @throws \Magento\Framework\Exception\LocalizedException
* @return void
*/
protected function deleteOldLinks($productIds)
protected function deleteOldLinks($productIds, ProductImport $productImport)
{
if ($this->getBehavior() != \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND) {
if ($this->getBehavior($productImport) != \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND) {
$this->connection->delete(
$this->productLink->getMainTable(),
$this->connection->quoteInto(
Expand All @@ -125,6 +133,8 @@ protected function deleteOldLinks($productIds)
}

/**
* Gets all the attributes from database for the Grouped Link Type
*
* @return array
*/
public function getAttributes()
Expand All @@ -145,6 +155,8 @@ public function getAttributes()
}

/**
* Returns the integer id for Link Type
*
* @return int
*/
protected function getLinkTypeId()
Expand All @@ -155,12 +167,15 @@ protected function getLinkTypeId()
/**
* Retrieve model behavior
*
* @param ProductImport $productImport
* @return string
*/
protected function getBehavior()
protected function getBehavior(ProductImport $productImport)
{
if ($this->behavior === null) {
$this->behavior = $this->importFactory->create()->getDataSourceModel()->getBehavior();
$ids = $productImport->getIds();
$dataSourceModel = $productImport->getDataSourceModel();
$this->behavior = $dataSourceModel->getBehavior($ids);
}
return $this->behavior;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
namespace Magento\GroupedImportExport\Test\Unit\Model\Import\Product\Type\Grouped;

use Magento\Catalog\Model\ResourceModel\Product\Link;
use Magento\CatalogImportExport\Model\Import\Product as ProductImport;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\DB\Adapter\Pdo\Mysql;
use Magento\Framework\DB\Select;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
use Magento\GroupedImportExport\Model\Import\Product\Type\Grouped\Links;
use Magento\ImportExport\Model\Import;
use Magento\ImportExport\Model\ImportFactory;
use Magento\ImportExport\Model\ResourceModel\Import\Data;
use PHPUnit\Framework\MockObject\MockObject;
Expand All @@ -28,20 +28,20 @@ class LinksTest extends TestCase
/** @var ObjectManagerHelper */
protected $objectManagerHelper;

/** @var Link|MockObject */
/** @var Link&MockObject */
protected $link;

/** @var ResourceConnection|MockObject */
/** @var ResourceConnection&MockObject */
protected $resource;

/** @var Mysql */
protected $connection;

/** @var ImportFactory|MockObject */
/** @var ImportFactory&MockObject */
protected $importFactory;

/** @var Import|MockObject */
protected $import;
/** @var ProductImport&MockObject */
protected $productImport;

protected function setUp(): void
{
Expand All @@ -52,11 +52,7 @@ protected function setUp(): void
->expects($this->once())
->method('getConnection')
->willReturn($this->connection);

$this->import = $this->createMock(Import::class);
$this->importFactory = $this->createPartialMock(ImportFactory::class, ['create']);
$this->importFactory->expects($this->any())->method('create')->willReturn($this->import);

$this->objectManagerHelper = new ObjectManagerHelper($this);
$this->links = $this->objectManagerHelper->getObject(
Links::class,
Expand All @@ -66,6 +62,8 @@ protected function setUp(): void
'importFactory' => $this->importFactory
]
);
$this->productImport = $this->createMock(ProductImport::class);
$this->productImport->expects($this->any())->method('getIds')->willReturn([]);
}

/**
Expand Down Expand Up @@ -95,7 +93,7 @@ public function testSaveLinksDataNoProductsAttrs($linksData)
$attributes = $this->attributesDataProvider();
$this->processAttributeGetter($attributes[2]['dbAttributes']);
$this->connection->expects($this->exactly(2))->method('insertOnDuplicate');
$this->links->saveLinksData($linksData);
$this->links->saveLinksData($linksData, $this->productImport);
}

/**
Expand Down Expand Up @@ -125,8 +123,7 @@ public function testSaveLinksDataWithProductsAttrs($linksData)
$this->link->expects($this->exactly(2))->method('getAttributeTypeTable')->willReturn(
'table_name'
);

$this->links->saveLinksData($linksData);
$this->links->saveLinksData($linksData, $this->productImport);
}

/**
Expand Down Expand Up @@ -197,6 +194,6 @@ protected function processBehaviorGetter($behavior)
{
$dataSource = $this->createMock(Data::class);
$dataSource->expects($this->once())->method('getBehavior')->willReturn($behavior);
$this->import->expects($this->once())->method('getDataSourceModel')->willReturn($dataSource);
$this->productImport->expects($this->any())->method('getDataSourceModel')->willReturn($dataSource);
}
}
13 changes: 10 additions & 3 deletions app/code/Magento/ImportExport/Block/Adminhtml/Import/Edit/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ protected function _prepareForm()
);
$fieldsets[$behaviorCode] = $fieldset;
}

// fieldset for file uploading
$fieldset = $form->addFieldset(
'upload_file_fieldset',
Expand Down Expand Up @@ -255,11 +254,19 @@ protected function _prepareForm()
),
]
);
$fieldset->addField(
Import::FIELD_IMPORT_IDS,
'hidden',
[
'name' => Import::FIELD_IMPORT_IDS,
'label' => __('Import id'),
'title' => __('Import id'),
'value' => '',
]
);
$fieldsets['upload'] = $fieldset;

$form->setUseContainer(true);
$this->setForm($form);

return parent::_prepareForm();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ public function execute()
//phpcs:disable Magento2.Security.Superglobal
if ($data) {
// common actions
$resultBlock->addAction(
'show',
'import_validation_container'
);

$resultBlock->addAction('show', 'import_validation_container');
$import = $this->getImport()->setData($data);
try {
$source = $import->uploadFileAndGetSource();
$this->processValidationResult($import->validateSource($source), $resultBlock);
$ids = $import->getValidatedIds();
if (count($ids) > 0) {
$resultBlock->addAction('value', Import::FIELD_IMPORT_IDS, $ids);
}
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$resultBlock->addError($e->getMessage());
} catch (\Exception $e) {
Expand Down Expand Up @@ -117,7 +117,6 @@ private function processValidationResult($validationResult, $resultBlock)
* Provides import model.
*
* @return Import
* @deprecated 100.1.0
*/
private function getImport()
{
Expand Down
28 changes: 25 additions & 3 deletions app/code/Magento/ImportExport/Model/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
use Magento\ImportExport\Model\Import\ConfigInterface;
use Magento\ImportExport\Model\Import\Entity\AbstractEntity;
use Magento\ImportExport\Model\Import\Entity\Factory;
use Magento\ImportExport\Model\Import\EntityInterface;
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingError;
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;
use Magento\ImportExport\Model\Import\Source\Base64EncodedCsvData;
use Magento\ImportExport\Model\ResourceModel\Import\Data;
use Magento\ImportExport\Model\Source\Import\AbstractBehavior;
use Magento\ImportExport\Model\Source\Import\Behavior\Factory as BehaviorFactory;
Expand Down Expand Up @@ -98,6 +98,11 @@ class Import extends AbstractModel
*/
public const FIELD_EMPTY_ATTRIBUTE_VALUE_CONSTANT = '_import_empty_attribute_value_constant';

/**
* Id of the `importexport_importdata` row after validation.
*/
public const FIELD_IMPORT_IDS = '_import_ids';

/**
* Allow multiple values wrapping in double quotes for additional attributes.
*/
Expand All @@ -118,7 +123,7 @@ class Import extends AbstractModel
public const IMPORT_DIR = 'import/';

/**
* @var AbstractEntity|ImportAbstractEntity
* @var EntityInterface
*/
protected $_entityAdapter;

Expand Down Expand Up @@ -272,7 +277,7 @@ public function __construct(
* Create instance of entity adapter and return it
*
* @throws LocalizedException
* @return AbstractEntity|ImportAbstractEntity
* @return EntityInterface
*/
protected function _getEntityAdapter()
{
Expand Down Expand Up @@ -476,6 +481,13 @@ public function getWorkingDir()
public function importSource()
{
$ids = $this->_getEntityAdapter()->getIds();
if (empty($ids)) {
$idsFromPostData = $this->getData(self::FIELD_IMPORT_IDS);
if (null !== $idsFromPostData && '' !== $idsFromPostData) {
$ids = explode(",", $idsFromPostData);
$this->_getEntityAdapter()->setIds($ids);
}
}
$this->setData('entity', $this->getDataSourceModel()->getEntityTypeCode($ids));
$this->setData('behavior', $this->getDataSourceModel()->getBehavior($ids));

Expand Down Expand Up @@ -852,4 +864,14 @@ public function getDeletedItemsCount()
{
return $this->_getEntityAdapter()->getDeletedItemsCount();
}

/**
* Retrieve Ids of Validated Rows
*
* @return int[]
*/
public function getValidatedIds() : array
{
return $this->_getEntityAdapter()->getIds() ?? [];
}
}
Loading

0 comments on commit 528ed74

Please sign in to comment.