Skip to content

Commit bccc10d

Browse files
author
Anna Bukatar
committed
MP2E-86: After product import, Category product manual sorting has been changed
1 parent 39e1300 commit bccc10d

File tree

2 files changed

+102
-10
lines changed

2 files changed

+102
-10
lines changed

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

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,6 +1349,40 @@ protected function _saveProductAttributes(array $attributesData)
13491349
return $this;
13501350
}
13511351

1352+
/**
1353+
* Get data for updating product-category relations
1354+
*
1355+
* @param array $categoriesData
1356+
* @param string $tableName
1357+
* @return array
1358+
*/
1359+
private function getProductCategoriesDataSave(array $categoriesData, string $tableName): array
1360+
{
1361+
$delProductId = [];
1362+
$categoriesIn = [];
1363+
$minCategoryPosition = [];
1364+
foreach ($categoriesData as $delSku => $categories) {
1365+
$productId = $this->skuProcessor->getNewSku($delSku)['entity_id'];
1366+
$delProductId[] = $productId;
1367+
1368+
foreach (array_keys($categories) as $categoryId) {
1369+
//position new products before existing ones
1370+
if (!isset($minCategoryPosition[$categoryId])) {
1371+
$select = $this->_connection->select()
1372+
->from($tableName, ['position' => new \Zend_Db_Expr('MIN(position)')])
1373+
->where('category_id = ?', $categoryId);
1374+
$minCategoryPosition[$categoryId] = (int)$this->_connection->fetchOne($select);
1375+
}
1376+
$categoriesIn[] = [
1377+
'product_id' => $productId,
1378+
'category_id' => $categoryId,
1379+
'position' => --$minCategoryPosition[$categoryId]
1380+
];
1381+
}
1382+
}
1383+
return [$delProductId, $categoriesIn];
1384+
}
1385+
13521386
/**
13531387
* Save product categories.
13541388
*
@@ -1363,17 +1397,8 @@ protected function _saveProductCategories(array $categoriesData)
13631397
$tableName = $this->_resourceFactory->create()->getProductCategoryTable();
13641398
}
13651399
if ($categoriesData) {
1366-
$categoriesIn = [];
1367-
$delProductId = [];
1368-
1369-
foreach ($categoriesData as $delSku => $categories) {
1370-
$productId = $this->skuProcessor->getNewSku($delSku)['entity_id'];
1371-
$delProductId[] = $productId;
1400+
list($delProductId, $categoriesIn) = $this->getProductCategoriesDataSave($categoriesData, $tableName);
13721401

1373-
foreach (array_keys($categories) as $categoryId) {
1374-
$categoriesIn[] = ['product_id' => $productId, 'category_id' => $categoryId, 'position' => 0];
1375-
}
1376-
}
13771402
if (Import::BEHAVIOR_APPEND != $this->getBehavior()) {
13781403
$this->_connection->delete(
13791404
$tableName,

app/code/Magento/CatalogImportExport/Test/Unit/Model/Import/ProductTest.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use Magento\Framework\App\Filesystem\DirectoryList;
3434
use Magento\Framework\App\ResourceConnection;
3535
use Magento\Framework\DB\Adapter\AdapterInterface;
36+
use Magento\Framework\DB\Select;
3637
use Magento\Framework\EntityManager\EntityMetadata;
3738
use Magento\Framework\EntityManager\MetadataPool;
3839
use Magento\Framework\Event\ManagerInterface;
@@ -211,6 +212,9 @@ class ProductTest extends AbstractImportTestCase
211212
/** @var DriverFile|MockObject */
212213
private $driverFile;
213214

215+
/** @var Select|MockObject */
216+
protected $select;
217+
214218
/**
215219
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
216220
*/
@@ -481,6 +485,13 @@ protected function _parentObjectConstructor()
481485
$this->config->expects($this->any())->method('getEntityType')->with(self::ENTITY_TYPE_CODE)->willReturn($type);
482486

483487
$this->_connection = $this->getMockForAbstractClass(AdapterInterface::class);
488+
$this->select = $this->getMockBuilder(Select::class)
489+
->disableOriginalConstructor()
490+
->setMethods(['from', 'where'])
491+
->getMock();
492+
$this->select->expects($this->any())->method('from')->willReturnSelf();
493+
//$this->select->expects($this->any())->method('where')->willReturnSelf();
494+
$this->_connection->expects($this->any())->method('select')->willReturn($this->select);
484495
$this->resource->expects($this->any())->method('getConnection')->willReturn($this->_connection);
485496
return $this;
486497
}
@@ -1421,6 +1432,62 @@ function ($name) use ($throwException, $exception) {
14211432
);
14221433
}
14231434

1435+
/**
1436+
* Check that getProductCategoriesDataSave method will return array with product-category-position relations
1437+
* where new products positioned before existing
1438+
*
1439+
* @param array $categoriesData
1440+
* @param string $tableName
1441+
* @param array $result
1442+
* @dataProvider productCategoriesDataProvider
1443+
*/
1444+
public function testGetProductCategoriesDataSave(array $categoriesData, string $tableName, array $result)
1445+
{
1446+
$this->_connection->expects($this->at(1))->method('fetchOne')->willReturn('0');
1447+
$this->_connection->expects($this->at(3))->method('fetchOne')->willReturn('-2');
1448+
$this->skuProcessor->expects($this->at(0))->method('getNewSku')->willReturn(['entity_id' => 2]);
1449+
$this->skuProcessor->expects($this->at(1))->method('getNewSku')->willReturn(['entity_id' => 5]);
1450+
$actualResult = $this->invokeMethod(
1451+
$this->importProduct,
1452+
'getProductCategoriesDataSave',
1453+
[$categoriesData, $tableName]
1454+
);
1455+
$this->assertEquals($result, $actualResult);
1456+
}
1457+
1458+
/**
1459+
* Data provider for testGetProductCategoriesDataSave.
1460+
*
1461+
* @return array
1462+
*/
1463+
public function productCategoriesDataProvider()
1464+
{
1465+
return [
1466+
[
1467+
[
1468+
'simple_2' => [3 => true],
1469+
'simple_5' => [5 => true]
1470+
],
1471+
'catalog_category_product',
1472+
[
1473+
[2, 5],
1474+
[
1475+
[
1476+
'product_id' => 2,
1477+
'category_id' => 3,
1478+
'position' => -1
1479+
],
1480+
[
1481+
'product_id' => 5,
1482+
'category_id' => 5,
1483+
'position' => -3
1484+
]
1485+
]
1486+
]
1487+
]
1488+
];
1489+
}
1490+
14241491
/**
14251492
* Data provider for testFillUploaderObject.
14261493
*

0 commit comments

Comments
 (0)