Skip to content

Commit b70d2ec

Browse files
Merge remote-tracking branch 'remotes/github/MAGETWO-60918' into EPAM-PR-59
2 parents 6a1773e + 58677ab commit b70d2ec

File tree

4 files changed

+161
-11
lines changed

4 files changed

+161
-11
lines changed

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,11 @@ protected function initCategories()
444444
if ($pathSize > 1) {
445445
$path = [];
446446
for ($i = 1; $i < $pathSize; $i++) {
447-
$name = $collection->getItemById($structure[$i])->getName();
448-
$path[] = $this->quoteCategoryDelimiter($name);
447+
$childCategory = $collection->getItemById($structure[$i]);
448+
if ($childCategory) {
449+
$name = $childCategory->getName();
450+
$path[] = $this->quoteCategoryDelimiter($name);
451+
}
449452
}
450453
$this->_rootCategories[$category->getId()] = array_shift($path);
451454
if ($pathSize > 2) {
@@ -673,8 +676,8 @@ protected function prepareLinks(array $productIds)
673676
/**
674677
* Update data row with information about categories. Return true, if data row was updated
675678
*
676-
* @param array &$dataRow
677-
* @param array &$rowCategories
679+
* @param array $dataRow
680+
* @param array $rowCategories
678681
* @param int $productId
679682
* @return bool
680683
*/
@@ -840,6 +843,7 @@ protected function paginateCollection($page, $pageSize)
840843
public function export()
841844
{
842845
//Execution time may be very long
846+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
843847
set_time_limit(0);
844848

845849
$writer = $this->getWriter();
@@ -963,6 +967,7 @@ protected function loadCollection(): array
963967
* @return array
964968
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
965969
* @SuppressWarnings(PHPMD.NPathComplexity)
970+
* phpcs:disable Generic.Metrics.NestingLevel
966971
*/
967972
protected function collectRawData()
968973
{
@@ -1057,6 +1062,7 @@ protected function collectRawData()
10571062

10581063
return $data;
10591064
}
1065+
//phpcs:enable Generic.Metrics.NestingLevel
10601066

10611067
/**
10621068
* Wrap values with double quotes if "Fields Enclosure" option is enabled

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

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
7+
declare(strict_types = 1);
8+
69
namespace Magento\CatalogImportExport\Model\Export;
710

811
/**
@@ -248,9 +251,11 @@ public function testExceptionInGetExportData()
248251
*/
249252
public function testExportWithFieldsEnclosure()
250253
{
251-
$this->model->setParameters([
252-
\Magento\ImportExport\Model\Export::FIELDS_ENCLOSURE => 1
253-
]);
254+
$this->model->setParameters(
255+
[
256+
\Magento\ImportExport\Model\Export::FIELDS_ENCLOSURE => 1
257+
]
258+
);
254259

255260
$this->model->setWriter(
256261
$this->objectManager->create(
@@ -278,11 +283,13 @@ public function testCategoryIdsFilter()
278283
)
279284
);
280285

281-
$this->model->setParameters([
282-
\Magento\ImportExport\Model\Export::FILTER_ELEMENT_GROUP => [
283-
'category_ids' => '2,13'
286+
$this->model->setParameters(
287+
[
288+
\Magento\ImportExport\Model\Export::FILTER_ELEMENT_GROUP => [
289+
'category_ids' => '2,13'
290+
]
284291
]
285-
]);
292+
);
286293

287294
$exportData = $this->model->export();
288295

@@ -292,6 +299,22 @@ public function testCategoryIdsFilter()
292299
$this->assertNotContains('Simple Product Not Visible On Storefront', $exportData);
293300
}
294301

302+
/**
303+
* Verify that export processed successfully with wrong category path
304+
*
305+
* @magentoDataFixture Magento/CatalogImportExport/_files/product_export_with_broken_categories_path.php
306+
*/
307+
public function testExportWithWrongCategoryPath()
308+
{
309+
$this->model->setWriter(
310+
$this->objectManager->create(
311+
\Magento\ImportExport\Model\Export\Adapter\Csv::class
312+
)
313+
);
314+
315+
$this->model->export();
316+
}
317+
295318
/**
296319
* Test 'hide from product page' export for non-default store.
297320
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
9+
10+
$defaultAttributeSet = $objectManager->get(Magento\Eav\Model\Config::class)
11+
->getEntityType('catalog_product')
12+
->getDefaultAttributeSetId();
13+
14+
$productRepository = $objectManager->create(
15+
\Magento\Catalog\Api\ProductRepositoryInterface::class
16+
);
17+
18+
$categoryLinkRepository = $objectManager->create(
19+
\Magento\Catalog\Api\CategoryLinkRepositoryInterface::class,
20+
[
21+
'productRepository' => $productRepository
22+
]
23+
);
24+
25+
/** @var Magento\Catalog\Api\CategoryLinkManagementInterface $linkManagement */
26+
$categoryLinkManagement = $objectManager->create(\Magento\Catalog\Api\CategoryLinkManagementInterface::class);
27+
$reflectionClass = new \ReflectionClass(get_class($categoryLinkManagement));
28+
$properties = [
29+
'productRepository' => $productRepository,
30+
'categoryLinkRepository' => $categoryLinkRepository
31+
];
32+
foreach ($properties as $key => $value) {
33+
if ($reflectionClass->hasProperty($key)) {
34+
$reflectionProperty = $reflectionClass->getProperty($key);
35+
$reflectionProperty->setAccessible(true);
36+
$reflectionProperty->setValue($categoryLinkManagement, $value);
37+
}
38+
}
39+
40+
/**
41+
* After installation system has two categories: root one with ID:1 and Default category with ID:2
42+
*/
43+
/** @var $category \Magento\Catalog\Model\Category */
44+
$category = $objectManager->create(\Magento\Catalog\Model\Category::class);
45+
$category->isObjectNew(true);
46+
$category->setId(3)
47+
->setName('Category 1')
48+
->setParentId(2)
49+
->setPath('1/2/3')
50+
->setLevel(2)
51+
->setAvailableSortBy('name')
52+
->setDefaultSortBy('name')
53+
->setIsActive(true)
54+
->setPosition(1)
55+
->save();
56+
57+
$category = $objectManager->create(\Magento\Catalog\Model\Category::class);
58+
$category->isObjectNew(true);
59+
$category->setId(5)
60+
->setName('Category 1.1')
61+
->setParentId(3)
62+
->setPath('1/2/3/4/5')
63+
->setLevel(4)
64+
->setAvailableSortBy('name')
65+
->setDefaultSortBy('name')
66+
->setIsActive(true)
67+
->setIsAnchor(true)
68+
->setPosition(1)
69+
->save();
70+
71+
/** @var $product \Magento\Catalog\Model\Product */
72+
$product = $objectManager->create(\Magento\Catalog\Model\Product::class);
73+
$product->isObjectNew(true);
74+
$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
75+
->setAttributeSetId($defaultAttributeSet)
76+
->setStoreId(1)
77+
->setWebsiteIds([1])
78+
->setName('Simple Product')
79+
->setSku('simple')
80+
->setPrice(10)
81+
->setWeight(18)
82+
->setStockData(['use_config_manage_stock' => 0])
83+
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
84+
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
85+
->save();
86+
87+
$categoryLinkManagement->assignProductToCategories(
88+
$product->getSku(),
89+
[5]
90+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
9+
/** @var \Magento\Framework\Registry $registry */
10+
$registry = $objectManager->get(\Magento\Framework\Registry::class);
11+
12+
$registry->unregister('isSecureArea');
13+
$registry->register('isSecureArea', true);
14+
15+
// Remove products
16+
/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
17+
$productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
18+
19+
$product = $productRepository->get('simple', false, null, true);
20+
$productRepository->delete($product);
21+
22+
//Remove categories
23+
/** @var Magento\Catalog\Model\ResourceModel\Category\Collection $collection */
24+
$collection = $objectManager->create(\Magento\Catalog\Model\ResourceModel\Category\Collection::class);
25+
foreach ($collection->addAttributeToFilter('level', ['in' => [2, 3, 4]]) as $category) {
26+
/** @var \Magento\Catalog\Model\Category $category */
27+
$category->delete();
28+
}
29+
30+
$registry->unregister('isSecureArea');
31+
$registry->register('isSecureArea', false);

0 commit comments

Comments
 (0)