Skip to content

Commit

Permalink
#12259: Save and Duplicated product not working
Browse files Browse the repository at this point in the history
  • Loading branch information
p-bystritsky committed Dec 8, 2017
1 parent 321278b commit ac4dd33
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 12 deletions.
1 change: 1 addition & 0 deletions app/code/Magento/Catalog/Model/Product/Copier.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public function copy(\Magento\Catalog\Model\Product $product)
$duplicate->save();
$isDuplicateSaved = true;
} catch (\Magento\Framework\Exception\AlreadyExistsException $e) {
} catch (\Magento\UrlRewrite\Model\Exception\UrlAlreadyExistsException $e) {
}
} while (!$isDuplicateSaved);
$this->getOptionRepository()->duplicate($product, $duplicate);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Catalog\Model\Product;


use Magento\Catalog\Model\ProductRepository;

class CopierTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\Framework\ObjectManagerInterface
*/
private $objectManager;

/**
* @var \Magento\Catalog\Model\Product\Copier
*/
private $copier;

/**
* @var \Magento\Catalog\Model\ProductRepository
*/
private $productRepository;

/**
* Tests multiple duplication of the same product.
*
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
* @magentoAppArea adminhtml
* @magentoDbIsolation disabled
* @magentoAppIsolation enabled
*/
public function testDoubleCopy()
{
$product = $this->productRepository->get('simple');

$product1 = $this->copier->copy($product);
$this->assertEquals(
'simple-1',
$product1->getSku()
);
$this->assertEquals(
'simple-product-1',
$product1->getUrlKey()
);

$product2 = $this->copier->copy($product);
$this->assertEquals(
'simple-2',
$product2->getSku()
);
$this->assertEquals(
'simple-product-2',
$product2->getUrlKey()
);
}

protected function setUp()
{
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$this->copier = $this->objectManager->get(Copier::class);
$this->productRepository = $this->objectManager->get(ProductRepository::class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,21 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
use Magento\Framework\Exception\NoSuchEntityException;

\Magento\TestFramework\Helper\Bootstrap::getInstance()->getInstance()->reinitialize();
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\Framework\Registry;
use Magento\TestFramework\Helper\Bootstrap;

/** @var \Magento\Framework\Registry $registry */
$registry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Magento\Framework\Registry::class);
/** @var Registry $registry */
$registry = Bootstrap::getObjectManager()->get(Registry::class);

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

/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
$productRepository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
->get(\Magento\Catalog\Api\ProductRepositoryInterface::class);
try {
$product = $productRepository->get('simple', false, null, true);
$productRepository->delete($product);
} catch (NoSuchEntityException $e) {
}
/** @var Collection $productCollection */
$productCollection = Bootstrap::getObjectManager()->get(Product::class)->getCollection();
$productCollection->delete();

$registry->unregister('isSecureArea');
$registry->register('isSecureArea', false);

0 comments on commit ac4dd33

Please sign in to comment.