Skip to content

Commit 805c498

Browse files
committed
MAGETWO-65261: Merge branch 'develop' of github.com:magento/magento2ce into MAGETWO-65261-PR-8484
2 parents 5752815 + 07f4e70 commit 805c498

File tree

13 files changed

+614
-62
lines changed

13 files changed

+614
-62
lines changed

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

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Magento\ImportExport\Model\Import\Entity\AbstractEntity;
1919
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingError;
2020
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;
21+
use Magento\Catalog\Model\Config as CatalogConfig;
2122

2223
/**
2324
* Import entity product model
@@ -657,6 +658,13 @@ class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
657658
*/
658659
private $multiLineSeparatorForRegexp;
659660

661+
/**
662+
* Catalog config.
663+
*
664+
* @var CatalogConfig
665+
*/
666+
private $catalogConfig;
667+
660668
/**
661669
* @param \Magento\Framework\Json\Helper\Data $jsonHelper
662670
* @param \Magento\ImportExport\Helper\Data $importExportData
@@ -695,6 +703,7 @@ class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
695703
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
696704
* @param array $data
697705
* @param array $dateAttrCodes
706+
* @param CatalogConfig $catalogConfig
698707
* @throws \Magento\Framework\Exception\LocalizedException
699708
*
700709
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
@@ -737,7 +746,8 @@ public function __construct(
737746
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
738747
\Magento\Catalog\Model\Product\Url $productUrl,
739748
array $data = [],
740-
array $dateAttrCodes = []
749+
array $dateAttrCodes = [],
750+
CatalogConfig $catalogConfig = null
741751
) {
742752
$this->_eventManager = $eventManager;
743753
$this->stockRegistry = $stockRegistry;
@@ -767,6 +777,9 @@ public function __construct(
767777
$this->scopeConfig = $scopeConfig;
768778
$this->productUrl = $productUrl;
769779
$this->dateAttrCodes = array_merge($this->dateAttrCodes, $dateAttrCodes);
780+
$this->catalogConfig = $catalogConfig ?: \Magento\Framework\App\ObjectManager::getInstance()
781+
->get(CatalogConfig::class);
782+
770783
parent::__construct(
771784
$jsonHelper,
772785
$importExportData,
@@ -1330,7 +1343,7 @@ public function saveProductEntity(array $entityRowsIn, array $entityRowsUp)
13301343
$entityTable = $this->_resourceFactory->create()->getEntityTable();
13311344
}
13321345
if ($entityRowsUp) {
1333-
$this->_connection->insertOnDuplicate($entityTable, $entityRowsUp, ['updated_at']);
1346+
$this->_connection->insertOnDuplicate($entityTable, $entityRowsUp, ['updated_at', 'attribute_set_id']);
13341347
}
13351348
if ($entityRowsIn) {
13361349
$this->_connection->insertMultiple($entityTable, $entityRowsIn);
@@ -1563,9 +1576,29 @@ protected function _saveProducts()
15631576
// 1. Entity phase
15641577
if (isset($this->_oldSku[$rowSku])) {
15651578
// existing row
1579+
if (isset($rowData['attribute_set_code'])) {
1580+
$attributeSetId = $this->catalogConfig->getAttributeSetId(
1581+
$this->getEntityTypeId(),
1582+
$rowData['attribute_set_code']
1583+
);
1584+
1585+
// wrong attribute_set_code was received
1586+
if (!$attributeSetId) {
1587+
throw new \Magento\Framework\Exception\LocalizedException(
1588+
__(
1589+
'Wrong attribute set code "%1", please correct it and try again.',
1590+
$rowData['attribute_set_code']
1591+
)
1592+
);
1593+
}
1594+
} else {
1595+
$attributeSetId = $this->skuProcessor->getNewSku($rowSku)['attr_set_id'];
1596+
}
1597+
15661598
$entityRowsUp[] = [
15671599
'updated_at' => (new \DateTime())->format(DateTime::DATETIME_PHP_FORMAT),
1568-
$this->getProductEntityLinkField() => $this->_oldSku[$rowSku][$this->getProductEntityLinkField()],
1600+
'attribute_set_id' => $attributeSetId,
1601+
$this->getProductEntityLinkField() => $this->_oldSku[$rowSku][$this->getProductEntityLinkField()]
15691602
];
15701603
} else {
15711604
if (!$productLimit || $productsQty < $productLimit) {

app/code/Magento/CatalogUrlRewrite/Observer/AfterImportDataObserver.php

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@
66
namespace Magento\CatalogUrlRewrite\Observer;
77

88
use Magento\Catalog\Model\Category;
9-
use Magento\Catalog\Model\Product;
9+
use Magento\Catalog\Model\Product\Visibility;
10+
use Magento\Catalog\Model\ResourceModel\Category\Collection as CategoryCollection;
11+
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
1012
use Magento\CatalogImportExport\Model\Import\Product as ImportProduct;
1113
use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator;
14+
use Magento\Framework\App\ObjectManager;
1215
use Magento\Framework\Event\Observer;
13-
use Magento\Framework\App\ResourceConnection;
16+
use Magento\Framework\Event\ObserverInterface;
1417
use Magento\ImportExport\Model\Import as ImportExport;
1518
use Magento\Store\Model\Store;
19+
use Magento\UrlRewrite\Model\MergeDataProviderFactory;
20+
use Magento\UrlRewrite\Model\OptionProvider;
21+
use Magento\UrlRewrite\Model\UrlFinderInterface;
1622
use Magento\UrlRewrite\Model\UrlPersistInterface;
1723
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
1824
use Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory;
19-
use Magento\UrlRewrite\Model\OptionProvider;
20-
use Magento\UrlRewrite\Model\UrlFinderInterface;
21-
use Magento\Framework\Event\ObserverInterface;
22-
use Magento\Catalog\Model\Product\Visibility;
23-
use Magento\Framework\App\ObjectManager;
24-
use Magento\UrlRewrite\Model\MergeDataProviderFactory;
2525

2626
/**
2727
* Class AfterImportDataObserver
@@ -102,6 +102,20 @@ class AfterImportDataObserver implements ObserverInterface
102102
/** @var \Magento\UrlRewrite\Model\MergeDataProvider */
103103
private $mergeDataProviderPrototype;
104104

105+
/**
106+
* Factory for creating category collection.
107+
*
108+
* @var CategoryCollectionFactory
109+
*/
110+
private $categoryCollectionFactory;
111+
112+
/**
113+
* Array of invoked categories during url rewrites generation.
114+
*
115+
* @var array
116+
*/
117+
private $categoriesCache = [];
118+
105119
/**
106120
* @param \Magento\Catalog\Model\ProductFactory $catalogProductFactory
107121
* @param \Magento\CatalogUrlRewrite\Model\ObjectRegistryFactory $objectRegistryFactory
@@ -112,7 +126,7 @@ class AfterImportDataObserver implements ObserverInterface
112126
* @param UrlRewriteFactory $urlRewriteFactory
113127
* @param UrlFinderInterface $urlFinder
114128
* @param \Magento\UrlRewrite\Model\MergeDataProviderFactory|null $mergeDataProviderFactory
115-
* @throws \InvalidArgumentException
129+
* @param CategoryCollectionFactory|null $categoryCollectionFactory
116130
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
117131
*/
118132
public function __construct(
@@ -124,7 +138,8 @@ public function __construct(
124138
UrlPersistInterface $urlPersist,
125139
UrlRewriteFactory $urlRewriteFactory,
126140
UrlFinderInterface $urlFinder,
127-
MergeDataProviderFactory $mergeDataProviderFactory = null
141+
MergeDataProviderFactory $mergeDataProviderFactory = null,
142+
CategoryCollectionFactory $categoryCollectionFactory = null
128143
) {
129144
$this->urlPersist = $urlPersist;
130145
$this->catalogProductFactory = $catalogProductFactory;
@@ -138,6 +153,8 @@ public function __construct(
138153
$mergeDataProviderFactory = ObjectManager::getInstance()->get(MergeDataProviderFactory::class);
139154
}
140155
$this->mergeDataProviderPrototype = $mergeDataProviderFactory->create();
156+
$this->categoryCollectionFactory = $categoryCollectionFactory ?:
157+
ObjectManager::getInstance()->get(CategoryCollectionFactory::class);
141158
}
142159

143160
/**
@@ -318,7 +335,7 @@ protected function canonicalUrlRewriteGenerate()
318335
}
319336

320337
/**
321-
* Generate list based on categories
338+
* Generate list based on categories.
322339
*
323340
* @return UrlRewrite[]
324341
*/
@@ -328,7 +345,7 @@ protected function categoriesUrlRewriteGenerate()
328345
foreach ($this->products as $productId => $productsByStores) {
329346
foreach ($productsByStores as $storeId => $product) {
330347
foreach ($this->categoryCache[$productId] as $categoryId) {
331-
$category = $this->import->getCategoryProcessor()->getCategoryById($categoryId);
348+
$category = $this->getCategoryById($categoryId, $storeId);
332349
if ($category->getParentId() == Category::TREE_ROOT_ID) {
333350
continue;
334351
}
@@ -481,4 +498,27 @@ protected function isCategoryProperForGenerating($category, $storeId)
481498
$this->acceptableCategories[$storeId][$category->getId()] = $acceptable;
482499
return $acceptable;
483500
}
501+
502+
/**
503+
* Get category by id considering store scope.
504+
*
505+
* @param int $categoryId
506+
* @param int $storeId
507+
* @return Category|\Magento\Framework\DataObject
508+
*/
509+
private function getCategoryById($categoryId, $storeId)
510+
{
511+
if (!isset($this->categoriesCache[$categoryId][$storeId])) {
512+
/** @var CategoryCollection $categoryCollection */
513+
$categoryCollection = $this->categoryCollectionFactory->create();
514+
$categoryCollection->addIdFilter([$categoryId])
515+
->setStoreId($storeId)
516+
->addAttributeToSelect('name')
517+
->addAttributeToSelect('url_key')
518+
->addAttributeToSelect('url_path');
519+
$this->categoriesCache[$categoryId][$storeId] = $categoryCollection->getFirstItem();
520+
}
521+
522+
return $this->categoriesCache[$categoryId][$storeId];
523+
}
484524
}

app/code/Magento/CatalogUrlRewrite/Test/Unit/Observer/AfterImportDataObserverTest.php

Lines changed: 47 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77

88
namespace Magento\CatalogUrlRewrite\Test\Unit\Observer;
99

10-
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
10+
use Magento\Catalog\Model\ResourceModel\Category\Collection as CategoryCollection;
11+
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
1112
use Magento\CatalogImportExport\Model\Import\Product as ImportProduct;
12-
use Magento\Store\Model\Store;
1313
use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator;
14+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
15+
use Magento\Store\Model\Store;
1416

1517
/**
1618
* Class AfterImportDataObserverTest
@@ -110,9 +112,16 @@ class AfterImportDataObserverTest extends \PHPUnit_Framework_TestCase
110112
*/
111113
private $product;
112114

113-
/** @var \Magento\UrlRewrite\Model\MergeDataProvider|\PHPUnit_Framework_MockObject_MockObject */
115+
/**
116+
* @var \Magento\UrlRewrite\Model\MergeDataProvider|\PHPUnit_Framework_MockObject_MockObject
117+
*/
114118
private $mergeDataProvider;
115119

120+
/**
121+
* @var CategoryCollectionFactory|\PHPUnit_Framework_MockObject_MockObject
122+
*/
123+
private $categoryCollectionFactory;
124+
116125
/**
117126
* Test products returned by getBunch method of event object.
118127
*
@@ -245,37 +254,6 @@ protected function setUp()
245254
->disableOriginalConstructor()
246255
->getMock();
247256

248-
$categoryProcessor = $this->getMock(
249-
\Magento\CatalogImportExport\Model\Import\Product\CategoryProcessor::class,
250-
[
251-
'getCategoryById',
252-
],
253-
[],
254-
'',
255-
false
256-
);
257-
$category = $this->getMock(
258-
\Magento\Catalog\Model\Category::class,
259-
[
260-
'getId',
261-
],
262-
[],
263-
'',
264-
false
265-
);
266-
$category
267-
->expects($this->any())
268-
->method('getId')
269-
->willReturn($this->categoryId);
270-
$categoryProcessor
271-
->expects($this->any())
272-
->method('getCategoryById')
273-
->with($this->categoryId)
274-
->willReturn($category);
275-
$this->importProduct
276-
->expects($this->any())
277-
->method('getCategoryProcessor')
278-
->willReturn($categoryProcessor);
279257
$mergeDataProviderFactory = $this->getMock(
280258
\Magento\UrlRewrite\Model\MergeDataProviderFactory::class,
281259
['create'],
@@ -286,6 +264,11 @@ protected function setUp()
286264
$this->mergeDataProvider = new \Magento\UrlRewrite\Model\MergeDataProvider;
287265
$mergeDataProviderFactory->expects($this->once())->method('create')->willReturn($this->mergeDataProvider);
288266

267+
$this->categoryCollectionFactory = $this->getMockBuilder(CategoryCollectionFactory::class)
268+
->setMethods(['create'])
269+
->disableOriginalConstructor()
270+
->getMock();
271+
289272
$this->objectManager = new ObjectManager($this);
290273
$this->import = $this->objectManager->getObject(
291274
\Magento\CatalogUrlRewrite\Observer\AfterImportDataObserver::class,
@@ -298,7 +281,8 @@ protected function setUp()
298281
'urlPersist' => $this->urlPersist,
299282
'urlRewriteFactory' => $this->urlRewriteFactory,
300283
'urlFinder' => $this->urlFinder,
301-
'mergeDataProviderFactory' => $mergeDataProviderFactory
284+
'mergeDataProviderFactory' => $mergeDataProviderFactory,
285+
'categoryCollectionFactory' => $this->categoryCollectionFactory
302286
]
303287
);
304288
}
@@ -563,6 +547,7 @@ public function testCanonicalUrlRewriteGenerateWithEmptyUrlPath()
563547

564548
/**
565549
* Cover categoriesUrlRewriteGenerate().
550+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
566551
*/
567552
public function testCategoriesUrlRewriteGenerate()
568553
{
@@ -601,6 +586,33 @@ public function testCategoriesUrlRewriteGenerate()
601586
->expects($this->any())
602587
->method('getId')
603588
->will($this->returnValue($this->categoryId));
589+
590+
$categoryCollection = $this->getMockBuilder(CategoryCollection::class)
591+
->disableOriginalConstructor()
592+
->getMock();
593+
$categoryCollection->expects($this->once())
594+
->method('addIdFilter')
595+
->with([$this->categoryId])
596+
->willReturnSelf();
597+
$categoryCollection->expects($this->once())
598+
->method('setStoreId')
599+
->with($storeId)
600+
->willReturnSelf();
601+
$categoryCollection->expects($this->exactly(3))
602+
->method('addAttributeToSelect')
603+
->withConsecutive(
604+
['name'],
605+
['url_key'],
606+
['url_path']
607+
)->willReturnSelf();
608+
$categoryCollection->expects($this->once())
609+
->method('getFirstItem')
610+
->willReturn($category);
611+
612+
$this->categoryCollectionFactory->expects($this->once())
613+
->method('create')
614+
->willReturn($categoryCollection);
615+
604616
$this->urlRewrite
605617
->expects($this->any())
606618
->method('setStoreId')

0 commit comments

Comments
 (0)