Skip to content

Commit a3b3f55

Browse files
author
Aliaksei Yakimovich2
committed
MAGETWO-60918: Fatal error on Import/Export page if deleted category ids exists in category path
- Added integrational test;
1 parent 15d6d74 commit a3b3f55

File tree

3 files changed

+155
-7
lines changed

3 files changed

+155
-7
lines changed

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,35 @@
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+
try {
20+
$product = $productRepository->get('simple', false, null, true);
21+
$productRepository->delete($product);
22+
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
23+
//Product already removed
24+
}
25+
26+
//Remove categories
27+
/** @var Magento\Catalog\Model\ResourceModel\Category\Collection $collection */
28+
$collection = $objectManager->create(\Magento\Catalog\Model\ResourceModel\Category\Collection::class);
29+
foreach ($collection->addAttributeToFilter('level', ['in' => [2, 3, 4]]) as $category) {
30+
/** @var \Magento\Catalog\Model\Category $category */
31+
$category->delete();
32+
}
33+
34+
$registry->unregister('isSecureArea');
35+
$registry->register('isSecureArea', false);

0 commit comments

Comments
 (0)