Skip to content

Commit ac4dd33

Browse files
committed
magento#12259: Save and Duplicated product not working
1 parent 321278b commit ac4dd33

File tree

3 files changed

+79
-12
lines changed

3 files changed

+79
-12
lines changed

app/code/Magento/Catalog/Model/Product/Copier.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public function copy(\Magento\Catalog\Model\Product $product)
8383
$duplicate->save();
8484
$isDuplicateSaved = true;
8585
} catch (\Magento\Framework\Exception\AlreadyExistsException $e) {
86+
} catch (\Magento\UrlRewrite\Model\Exception\UrlAlreadyExistsException $e) {
8687
}
8788
} while (!$isDuplicateSaved);
8889
$this->getOptionRepository()->duplicate($product, $duplicate);
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Catalog\Model\Product;
8+
9+
10+
use Magento\Catalog\Model\ProductRepository;
11+
12+
class CopierTest extends \PHPUnit\Framework\TestCase
13+
{
14+
/**
15+
* @var \Magento\Framework\ObjectManagerInterface
16+
*/
17+
private $objectManager;
18+
19+
/**
20+
* @var \Magento\Catalog\Model\Product\Copier
21+
*/
22+
private $copier;
23+
24+
/**
25+
* @var \Magento\Catalog\Model\ProductRepository
26+
*/
27+
private $productRepository;
28+
29+
/**
30+
* Tests multiple duplication of the same product.
31+
*
32+
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
33+
* @magentoAppArea adminhtml
34+
* @magentoDbIsolation disabled
35+
* @magentoAppIsolation enabled
36+
*/
37+
public function testDoubleCopy()
38+
{
39+
$product = $this->productRepository->get('simple');
40+
41+
$product1 = $this->copier->copy($product);
42+
$this->assertEquals(
43+
'simple-1',
44+
$product1->getSku()
45+
);
46+
$this->assertEquals(
47+
'simple-product-1',
48+
$product1->getUrlKey()
49+
);
50+
51+
$product2 = $this->copier->copy($product);
52+
$this->assertEquals(
53+
'simple-2',
54+
$product2->getSku()
55+
);
56+
$this->assertEquals(
57+
'simple-product-2',
58+
$product2->getUrlKey()
59+
);
60+
}
61+
62+
protected function setUp()
63+
{
64+
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
65+
$this->copier = $this->objectManager->get(Copier::class);
66+
$this->productRepository = $this->objectManager->get(ProductRepository::class);
67+
}
68+
}

dev/tests/integration/testsuite/Magento/Catalog/_files/product_simple_rollback.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,21 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
use Magento\Framework\Exception\NoSuchEntityException;
76

8-
\Magento\TestFramework\Helper\Bootstrap::getInstance()->getInstance()->reinitialize();
7+
use Magento\Catalog\Model\Product;
8+
use Magento\Catalog\Model\ResourceModel\Product\Collection;
9+
use Magento\Framework\Registry;
10+
use Magento\TestFramework\Helper\Bootstrap;
911

10-
/** @var \Magento\Framework\Registry $registry */
11-
$registry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Magento\Framework\Registry::class);
12+
/** @var Registry $registry */
13+
$registry = Bootstrap::getObjectManager()->get(Registry::class);
1214

1315
$registry->unregister('isSecureArea');
1416
$registry->register('isSecureArea', true);
1517

16-
/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
17-
$productRepository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
18-
->get(\Magento\Catalog\Api\ProductRepositoryInterface::class);
19-
try {
20-
$product = $productRepository->get('simple', false, null, true);
21-
$productRepository->delete($product);
22-
} catch (NoSuchEntityException $e) {
23-
}
18+
/** @var Collection $productCollection */
19+
$productCollection = Bootstrap::getObjectManager()->get(Product::class)->getCollection();
20+
$productCollection->delete();
21+
2422
$registry->unregister('isSecureArea');
2523
$registry->register('isSecureArea', false);

0 commit comments

Comments
 (0)