Skip to content

Commit

Permalink
Fix issue magento#14112
Browse files Browse the repository at this point in the history
- set null OptionId
- set null all ProductLink

to prevent overwrite of duplicated bundle product in table `catalog_product_bundle_selection`
  • Loading branch information
riccardougolini committed Jun 10, 2018
1 parent c62d922 commit 2a67fd7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
12 changes: 12 additions & 0 deletions app/code/Magento/Bundle/Model/Product/CopyConstructor/Bundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,22 @@ public function build(Product $product, Product $duplicate)
return;
}

/** @var \Magento\Bundle\Api\Data\OptionInterface[]|null $bundleOptions */
$bundleOptions = $product->getExtensionAttributes()->getBundleProductOptions() ?: [];

/** @var \Magento\Bundle\Api\Data\OptionInterface[]|null $duplicatedBundleOptions */
$duplicatedBundleOptions = [];

foreach ($bundleOptions as $key => $bundleOption) {
$duplicatedBundleOptions[$key] = clone $bundleOption;
$duplicatedBundleOptions[$key]->setOptionId(null);
/** @var \Magento\Bundle\Api\Data\LinkInterface[]|null $bundleSelections */
$bundleSelections = $duplicatedBundleOptions[$key]->getProductLinks();

/** @var \Magento\Bundle\Api\Data\LinkInterface $bundleSelection */
foreach($bundleSelections as $bundleSelection){
$bundleSelection->setSelectionId(null);
}
}
$duplicate->getExtensionAttributes()->setBundleProductOptions($duplicatedBundleOptions);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Magento\Bundle\Api\Data\BundleOptionInterface;
use Magento\Bundle\Model\Product\CopyConstructor\Bundle;
use Magento\Catalog\Api\Data\ProductExtensionInterface;
use Magento\Catalog\Api\Data\ProductLinkInterface;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Type;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
Expand Down Expand Up @@ -61,12 +62,8 @@ public function testBuildPositive()
->willReturn($extensionAttributesProduct);

$bundleOptions = [
$this->getMockBuilder(BundleOptionInterface::class)
->disableOriginalConstructor()
->getMockForAbstractClass(),
$this->getMockBuilder(BundleOptionInterface::class)
->disableOriginalConstructor()
->getMockForAbstractClass()
$this->createBundleOption(),
$this->createBundleOption()
];
$extensionAttributesProduct->expects($this->once())
->method('getBundleProductOptions')
Expand All @@ -86,7 +83,6 @@ public function testBuildPositive()
$extensionAttributesDuplicate->expects($this->once())
->method('setBundleProductOptions')
->withConsecutive([$bundleOptions]);

$this->model->build($product, $duplicate);
}

Expand Down Expand Up @@ -131,4 +127,32 @@ public function testBuildWithoutOptions()

$this->model->build($product, $duplicate);
}

/**
* @return BundleOptionInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private function createBundleOption()
{
/** @var \PHPUnit_Framework_MockObject_MockObject|BundleOptionInterface $bundleOptionMock */
$bundleOptionMock = $this->getMockBuilder(BundleOptionInterface::class)
->setMethods(['getProductLinks'])
->disableOriginalConstructor()
->getMockForAbstractClass();

/** @var \PHPUnit_Framework_MockObject_MockObject|ProductLinkInterface $productLinkMock */
$productLinkMock = $this->getMockBuilder(ProductLinkInterface::class)
->setMethods(['setSelectionId'])
->disableOriginalConstructor()
->getMockForAbstractClass();

$productLinkMock->expects($this->once())
->method('setSelectionId')
->with(null);

$bundleOptionMock->expects($this->once())
->method('getProductLinks')
->willReturn([$productLinkMock]);

return $bundleOptionMock;
}
}

0 comments on commit 2a67fd7

Please sign in to comment.