Skip to content

Commit 27e439b

Browse files
committed
Merge branch 'MAGETWO-39255' of github.corp.magento.com:magento-firedrakes/magento2ce into MAGETWO-39255
2 parents f010928 + f32dff9 commit 27e439b

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

app/code/Magento/Catalog/Model/CategoryRepository.php

Lines changed: 21 additions & 1 deletion
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\Exception\StateException;
13+
use Magento\Catalog\Api\Data\CategoryInterface;
1314

1415
class CategoryRepository implements \Magento\Catalog\Api\CategoryRepositoryInterface
1516
{
@@ -33,6 +34,11 @@ class CategoryRepository implements \Magento\Catalog\Api\CategoryRepositoryInter
3334
*/
3435
protected $categoryResource;
3536

37+
/**
38+
* @var \Magento\Framework\Model\Entity\MetadataPool
39+
*/
40+
protected $metadataPool;
41+
3642
/**
3743
* List of fields that can used config values in case when value does not defined directly
3844
*
@@ -44,15 +50,18 @@ class CategoryRepository implements \Magento\Catalog\Api\CategoryRepositoryInter
4450
* @param \Magento\Catalog\Model\CategoryFactory $categoryFactory
4551
* @param \Magento\Catalog\Model\ResourceModel\Category $categoryResource
4652
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
53+
* @param \Magento\Framework\Model\Entity\MetadataPool $metadataPool
4754
*/
4855
public function __construct(
4956
\Magento\Catalog\Model\CategoryFactory $categoryFactory,
5057
\Magento\Catalog\Model\ResourceModel\Category $categoryResource,
51-
\Magento\Store\Model\StoreManagerInterface $storeManager
58+
\Magento\Store\Model\StoreManagerInterface $storeManager,
59+
\Magento\Framework\Model\Entity\MetadataPool $metadataPool
5260
) {
5361
$this->categoryFactory = $categoryFactory;
5462
$this->categoryResource = $categoryResource;
5563
$this->storeManager = $storeManager;
64+
$this->metadataPool = $metadataPool;
5665
}
5766

5867
/**
@@ -61,10 +70,21 @@ public function __construct(
6170
public function save(\Magento\Catalog\Api\Data\CategoryInterface $category)
6271
{
6372
$existingData = $category->toFlatArray();
73+
6474
/** 'available_sort_by' should be set separately because fields of array type are destroyed by toFlatArray() */
6575
$existingData['available_sort_by'] = $category->getAvailableSortBy();
76+
6677
if ($category->getId()) {
78+
$metadata = $this->metadataPool->getMetadata(
79+
CategoryInterface::class
80+
);
81+
6782
$existingCategory = $this->get($category->getId());
83+
84+
$existingData[$metadata->getLinkField()] = $existingCategory->getData(
85+
$metadata->getLinkField()
86+
);
87+
6888
if (isset($existingData['image']) && is_array($existingData['image'])) {
6989
$existingData['image_additional_data'] = $existingData['image'];
7090
unset($existingData['image']);

app/code/Magento/Catalog/Test/Unit/Model/CategoryRepositoryTest.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ class CategoryRepositoryTest extends \PHPUnit_Framework_TestCase
2727
*/
2828
protected $storeManagerMock;
2929

30+
/**
31+
* @var \Magento\Framework\Model\Entity\MetadataPool|\PHPUnit_Framework_MockObject_MockObject
32+
*/
33+
protected $metadataPoolMock;
34+
3035
protected function setUp()
3136
{
3237
$this->categoryFactoryMock = $this->getMock(
@@ -40,10 +45,34 @@ protected function setUp()
4045
$this->getMock('\Magento\Catalog\Model\ResourceModel\Category', [], [], '', false);
4146
$this->storeManagerMock = $this->getMock('\Magento\Store\Model\StoreManagerInterface');
4247

48+
$metadataMock = $this->getMock(
49+
'Magento\Framework\Model\Entity\EntityMetadata',
50+
[],
51+
[],
52+
'',
53+
false
54+
);
55+
$metadataMock->expects($this->any())
56+
->method('getLinkField')
57+
->willReturn('entity_id');
58+
59+
$this->metadataPoolMock = $this->getMock(
60+
'Magento\Framework\Model\Entity\MetadataPool',
61+
[],
62+
[],
63+
'',
64+
false
65+
);
66+
$this->metadataPoolMock->expects($this->any())
67+
->method('getMetadata')
68+
->with(\Magento\Catalog\Api\Data\CategoryInterface::class)
69+
->willReturn($metadataMock);
70+
4371
$this->model = new \Magento\Catalog\Model\CategoryRepository(
4472
$this->categoryFactoryMock,
4573
$this->categoryResourceMock,
46-
$this->storeManagerMock
74+
$this->storeManagerMock,
75+
$this->metadataPoolMock
4776
);
4877
}
4978

0 commit comments

Comments
 (0)