Skip to content

Commit 8ca1d0c

Browse files
committed
Merge remote-tracking branch 'goinc/MAGETWO-51951' into pr-613
2 parents 2f91db8 + 5c1b8d4 commit 8ca1d0c

File tree

4 files changed

+85
-24
lines changed

4 files changed

+85
-24
lines changed

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
*/
88
namespace Magento\Catalog\Model\Product;
99

10+
use Magento\Catalog\Api\Data\ProductInterface;
11+
1012
class Copier
1113
{
1214
/**
@@ -24,6 +26,11 @@ class Copier
2426
*/
2527
protected $productFactory;
2628

29+
/**
30+
* @var \Magento\Framework\EntityManager\MetadataPool
31+
*/
32+
protected $metadataPool;
33+
2734
/**
2835
* @param CopyConstructorInterface $copyConstructor
2936
* @param \Magento\Catalog\Model\ProductFactory $productFactory
@@ -73,14 +80,18 @@ public function copy(\Magento\Catalog\Model\Product $product)
7380
} catch (\Magento\Framework\Exception\AlreadyExistsException $e) {
7481
}
7582
} while (!$isDuplicateSaved);
76-
7783
$this->getOptionRepository()->duplicate($product, $duplicate);
78-
$product->getResource()->duplicate($product->getEntityId(), $duplicate->getEntityId());
84+
$metadata = $this->getMetadataPool()->getMetadata(ProductInterface::class);
85+
$product->getResource()->duplicate(
86+
$product->getData($metadata->getLinkField()),
87+
$duplicate->getData($metadata->getLinkField())
88+
);
7989
return $duplicate;
8090
}
8191

8292
/**
8393
* @return Option\Repository
94+
* @deprecated
8495
*/
8596
private function getOptionRepository()
8697
{
@@ -90,4 +101,17 @@ private function getOptionRepository()
90101
}
91102
return $this->optionRepository;
92103
}
104+
105+
/**
106+
* @return \Magento\Framework\EntityManager\MetadataPool
107+
* @deprecated
108+
*/
109+
private function getMetadataPool()
110+
{
111+
if (null === $this->metadataPool) {
112+
$this->metadataPool = \Magento\Framework\App\ObjectManager::getInstance()
113+
->get('Magento\Framework\EntityManager\MetadataPool');
114+
}
115+
return $this->metadataPool;
116+
}
93117
}

app/code/Magento/Catalog/Model/Product/Option/Repository.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Framework\Exception\CouldNotSaveException;
1111
use Magento\Framework\Exception\NoSuchEntityException;
1212
use Magento\Framework\EntityManager\MetadataPool;
13+
use Magento\Framework\EntityManager\HydratorPool;
1314

1415
/**
1516
* Class Repository
@@ -42,6 +43,11 @@ class Repository implements \Magento\Catalog\Api\ProductCustomOptionRepositoryIn
4243
*/
4344
protected $metadataPool;
4445

46+
/**
47+
* @var HydratorPool
48+
*/
49+
protected $hydratorPool;
50+
4551
/**
4652
* @var Converter
4753
*/
@@ -113,10 +119,12 @@ public function duplicate(
113119
\Magento\Catalog\Api\Data\ProductInterface $product,
114120
\Magento\Catalog\Api\Data\ProductInterface $duplicate
115121
) {
122+
$hydrator = $this->getHydratorPool()->getHydrator(ProductInterface::class);
123+
$metadata = $this->getMetadataPool()->getMetadata(ProductInterface::class);
116124
return $this->optionResource->duplicate(
117125
$this->getOptionFactory()->create([]),
118-
$product->getId(),
119-
$duplicate->getId()
126+
$hydrator->extract($product)[$metadata->getLinkField()],
127+
$hydrator->extract($duplicate)[$metadata->getLinkField()]
120128
);
121129
}
122130

@@ -189,6 +197,7 @@ protected function markRemovedValues($newValues, $originalValues)
189197

190198
/**
191199
* @return \Magento\Catalog\Model\Product\OptionFactory
200+
* @deprecated
192201
*/
193202
private function getOptionFactory()
194203
{
@@ -201,6 +210,7 @@ private function getOptionFactory()
201210

202211
/**
203212
* @return \Magento\Catalog\Model\ResourceModel\Product\Option\CollectionFactory
213+
* @deprecated
204214
*/
205215
private function getCollectionFactory()
206216
{
@@ -213,6 +223,7 @@ private function getCollectionFactory()
213223

214224
/**
215225
* @return \Magento\Framework\EntityManager\MetadataPool
226+
* @deprecated
216227
*/
217228
private function getMetadataPool()
218229
{
@@ -222,4 +233,17 @@ private function getMetadataPool()
222233
}
223234
return $this->metadataPool;
224235
}
236+
237+
/**
238+
* @return \Magento\Framework\EntityManager\HydratorPool
239+
* @deprecated
240+
*/
241+
private function getHydratorPool()
242+
{
243+
if (null === $this->hydratorPool) {
244+
$this->hydratorPool = \Magento\Framework\App\ObjectManager::getInstance()
245+
->get('Magento\Framework\EntityManager\HydratorPool');
246+
}
247+
return $this->hydratorPool;
248+
}
225249
}

app/code/Magento/Catalog/Test/Unit/Model/Product/CopierTest.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ class CopierTest extends \PHPUnit_Framework_TestCase
3434
*/
3535
protected $productMock;
3636

37+
/**
38+
* @var \PHPUnit_Framework_MockObject_MockObject
39+
*/
40+
protected $metadata;
41+
3742
protected function setUp()
3843
{
3944
$this->copyConstructorMock = $this->getMock('\Magento\Catalog\Model\Product\CopyConstructorInterface');
@@ -54,22 +59,33 @@ protected function setUp()
5459
$this->optionRepositoryMock;
5560
$this->productMock = $this->getMock('\Magento\Catalog\Model\Product', [], [], '', false);
5661
$this->productMock->expects($this->any())->method('getEntityId')->willReturn(1);
57-
$this->productMock->expects($this->any())->method('getData')->will($this->returnValue('product data'));
5862

63+
$this->metadata = $this->getMockBuilder('Magento\Framework\EntityManager\EntityMetadata')
64+
->disableOriginalConstructor()
65+
->getMock();
66+
$metadataPool = $this->getMockBuilder('Magento\Framework\EntityManager\MetadataPool')
67+
->disableOriginalConstructor()
68+
->getMock();
69+
$metadataPool->expects($this->any())->method('getMetadata')->willReturn($this->metadata);
5970
$this->_model = new Copier(
6071
$this->copyConstructorMock,
6172
$this->productFactoryMock
6273
);
6374

6475
$this->setProperties($this->_model, [
65-
'optionRepository' => $this->optionRepositoryMock
76+
'optionRepository' => $this->optionRepositoryMock,
77+
'metadataPool' => $metadataPool,
6678
]);
6779
}
6880

6981
public function testCopy()
7082
{
7183
$this->productMock->expects($this->atLeastOnce())->method('getWebsiteIds');
7284
$this->productMock->expects($this->atLeastOnce())->method('getCategoryIds');
85+
$this->productMock->expects($this->any())->method('getData')->willReturnMap([
86+
['', null, 'product data'],
87+
['linkField', null, '1'],
88+
]);
7389

7490
$resourceMock = $this->getMock('\Magento\Catalog\Model\ResourceModel\Product', [], [], '', false);
7591
$this->productMock->expects($this->once())->method('getResource')->will($this->returnValue($resourceMock));
@@ -80,6 +96,7 @@ public function testCopy()
8096
'__wakeup',
8197
'setData',
8298
'setOptions',
99+
'getData',
83100
'setIsDuplicate',
84101
'setOriginalId',
85102
'setStatus',
@@ -123,8 +140,12 @@ public function testCopy()
123140
$duplicateMock->expects($this->once())->method('getUrlKey')->willReturn('urk-key-1');
124141
$duplicateMock->expects($this->once())->method('setUrlKey')->with('urk-key-2');
125142
$duplicateMock->expects($this->once())->method('save');
126-
$duplicateMock->expects($this->any())->method('getEntityId')->willReturn(2);
127-
$this->optionRepositoryMock->expects($this->once())
143+
144+
$this->metadata->expects($this->any())->method('getLinkField')->willReturn('linkField');
145+
146+
$duplicateMock->expects($this->any())->method('getData')->willReturnMap([
147+
['linkField', null, '2'],
148+
]); $this->optionRepositoryMock->expects($this->once())
128149
->method('duplicate')
129150
->with($this->productMock, $duplicateMock);
130151
$resourceMock->expects($this->once())->method('duplicate')->with(1, 2);

dev/tests/integration/testsuite/Magento/Catalog/Model/Product/Attribute/Backend/SkuTest.php

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,18 @@ public function testGenerateUniqueSkuNotExistingProduct($product)
3838
}
3939

4040
/**
41-
* @param $product \Magento\Catalog\Model\Product
42-
* @dataProvider uniqueLongSkuDataProvider
41+
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
4342
* @magentoAppArea adminhtml
4443
* @magentoDbIsolation enabled
4544
*/
46-
public function testGenerateUniqueLongSku($product)
45+
public function testGenerateUniqueLongSku()
4746
{
47+
$repository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
48+
'Magento\Catalog\Model\ProductRepository'
49+
);
50+
$product = $repository->get('simple');
51+
$product->setSku('0123456789012345678901234567890123456789012345678901234567890123');
52+
4853
/** @var \Magento\Catalog\Model\Product\Copier $copier */
4954
$copier = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
5055
'Magento\Catalog\Model\Product\Copier'
@@ -66,19 +71,6 @@ public function uniqueSkuDataProvider()
6671
return [[$product]];
6772
}
6873

69-
/**
70-
* Returns simple product
71-
*
72-
* @return array
73-
*/
74-
public function uniqueLongSkuDataProvider()
75-
{
76-
$product = $this->_getProduct();
77-
$product->setSku('0123456789012345678901234567890123456789012345678901234567890123');
78-
//strlen === 64
79-
return [[$product]];
80-
}
81-
8274
/**
8375
* Get product form data provider
8476
*

0 commit comments

Comments
 (0)