Skip to content

Commit 2fab1a0

Browse files
author
Alex Bomko
committed
Merge remote-tracking branch 'origin/MAGETWO-52037' into develop
2 parents 584f92b + 3673873 commit 2fab1a0

File tree

3 files changed

+71
-5
lines changed

3 files changed

+71
-5
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1270,7 +1270,7 @@ protected function _saveProductCategories(array $categoriesData)
12701270
);
12711271
}
12721272
if ($categoriesIn) {
1273-
$this->_connection->insertOnDuplicate($tableName, $categoriesIn, ['position']);
1273+
$this->_connection->insertOnDuplicate($tableName, $categoriesIn, ['product_id', 'category_id']);
12741274
}
12751275
}
12761276
return $this;

dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/Import/ProductTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
*/
1515
namespace Magento\CatalogImportExport\Model\Import;
1616

17+
use Magento\Catalog\Api\ProductRepositoryInterface;
18+
use Magento\Catalog\Model\Category;
1719
use Magento\Framework\App\Bootstrap;
1820
use Magento\Framework\App\Filesystem\DirectoryList;
1921
use Magento\ImportExport\Model\Import;
@@ -887,6 +889,70 @@ public function testProductCategories($fixture, $separator)
887889
$this->assertTrue(count($categories) == 2);
888890
}
889891

892+
/**
893+
* @magentoAppArea adminhtml
894+
* @magentoDbIsolation enabled
895+
* @magentoAppIsolation enabled
896+
* @magentoDataFixture Magento/Catalog/_files/multiple_products.php
897+
* @magentoDataFixture Magento/Catalog/_files/category.php
898+
*/
899+
public function testProductPositionInCategory()
900+
{
901+
/* @var \Magento\Catalog\Model\ResourceModel\Category\Collection $collection */
902+
$collection = $this->objectManager->create(\Magento\Catalog\Model\ResourceModel\Category\Collection::class);
903+
$collection->addNameToResult()->load();
904+
/** @var Category $category */
905+
$category = $collection->getItemByColumnValue('name', 'Category 1');
906+
907+
/** @var ProductRepositoryInterface $productRepository */
908+
$productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
909+
910+
$categoryProducts = [];
911+
$i = 51;
912+
foreach (['simple1', 'simple2', 'simple3'] as $sku) {
913+
$categoryProducts[$productRepository->get($sku)->getId()] = $i++;
914+
}
915+
$category->setPostedProducts($categoryProducts);
916+
$category->save();
917+
918+
$filesystem = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
919+
\Magento\Framework\Filesystem::class
920+
);
921+
922+
$directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
923+
$source = $this->objectManager->create(
924+
\Magento\ImportExport\Model\Import\Source\Csv::class,
925+
[
926+
'file' => __DIR__ . '/_files/products_to_import.csv',
927+
'directory' => $directory
928+
]
929+
);
930+
$errors = $this->_model->setSource(
931+
$source
932+
)->setParameters(
933+
[
934+
'behavior' => \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND,
935+
'entity' => 'catalog_product'
936+
]
937+
)->validateData();
938+
939+
$this->assertTrue($errors->getErrorsCount() == 0);
940+
$this->_model->importData();
941+
942+
/** @var \Magento\Framework\App\ResourceConnection $resourceConnection */
943+
$resourceConnection = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
944+
\Magento\Framework\App\ResourceConnection::class
945+
);
946+
$tableName = $resourceConnection->getTableName('catalog_category_product');
947+
$select = $resourceConnection->getConnection()->select()->from($tableName)
948+
->where('category_id = ?', $category->getId());
949+
$items = $resourceConnection->getConnection()->fetchAll($select);
950+
$this->assertCount(3, $items);
951+
foreach ($items as $item) {
952+
$this->assertGreaterThan(50, $item['position']);
953+
}
954+
}
955+
890956
/**
891957
* @return array
892958
*/
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
sku,product_type,store_view_code,name,price,attribute_set_code
2-
simple1,simple,,"simple 1",25,Default
3-
simple2,simple,,"simple 2",34,Default
4-
simple3,simple,,"simple 3",58,Default
1+
sku,product_type,store_view_code,name,price,attribute_set_code,categories
2+
simple1,simple,,"simple 1",25,Default,"Default Category/Category 1"
3+
simple2,simple,,"simple 2",34,Default,"Default Category/Category 1"
4+
simple3,simple,,"simple 3",58,Default,"Default Category/Category 1"

0 commit comments

Comments
 (0)