Skip to content

Commit c6bcd39

Browse files
author
Yaroslav Onischenko
committed
Merge remote-tracking branch 'mainline/develop' into develop
2 parents c3d4843 + e1bd045 commit c6bcd39

File tree

23 files changed

+437
-68
lines changed

23 files changed

+437
-68
lines changed

app/code/Magento/Catalog/Controller/Adminhtml/Category/Save.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,15 @@ protected function _filterCategoryPostData(array $rawData)
8282
$data = $rawData;
8383
// @todo It is a workaround to prevent saving this data in category model and it has to be refactored in future
8484
if (isset($data['image']) && is_array($data['image'])) {
85-
$data['image_additional_data'] = $data['image'];
86-
unset($data['image']);
85+
if (!empty($data['image']['delete'])) {
86+
$data['image'] = null;
87+
} else {
88+
if (isset($data['image'][0]['name']) && isset($data['image'][0]['tmp_name'])) {
89+
$data['image'] = $data['image'][0]['name'];
90+
} else {
91+
unset($data['image']);
92+
}
93+
}
8794
}
8895
return $data;
8996
}

app/code/Magento/Catalog/Model/Category/Attribute/Backend/Image.php

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -94,26 +94,14 @@ private function getImageUploader()
9494
*/
9595
public function afterSave($object)
9696
{
97-
$value = $object->getData($this->getAttribute()->getName() . '_additional_data');
97+
$image = $object->getData($this->getAttribute()->getName(), null);
9898

99-
if (is_array($value) && !empty($value['delete'])) {
100-
$object->setData($this->getAttribute()->getName(), '');
101-
$this->getAttribute()->getEntity()->saveAttribute($object, $this->getAttribute()->getName());
102-
return $this;
103-
}
104-
105-
if (isset($value[0]['name']) && isset($value[0]['tmp_name'])) {
99+
if ($image !== null) {
106100
try {
107-
$result = $this->getImageUploader()->moveFileFromTmp($value[0]['name']);
108-
109-
$object->setData($this->getAttribute()->getName(), $result);
110-
$this->getAttribute()->getEntity()->saveAttribute($object, $this->getAttribute()->getName());
101+
$this->getImageUploader()->moveFileFromTmp($image);
111102
} catch (\Exception $e) {
112103
$this->_logger->critical($e);
113104
}
114-
} elseif (isset($value[0]['name'])) {
115-
$object->setData($this->getAttribute()->getName(), $value[0]['name']);
116-
$this->getAttribute()->getEntity()->saveAttribute($object, $this->getAttribute()->getName());
117105
}
118106
return $this;
119107
}

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,15 @@ public function save(\Magento\Catalog\Api\Data\CategoryInterface $category)
9191
);
9292

9393
if (isset($existingData['image']) && is_array($existingData['image'])) {
94-
$existingData['image_additional_data'] = $existingData['image'];
95-
unset($existingData['image']);
94+
if (!empty($existingData['image']['delete'])) {
95+
$existingData['image'] = null;
96+
} else {
97+
if (isset($existingData['image'][0]['name']) && isset($existingData['image'][0]['tmp_name'])) {
98+
$existingData['image'] = $existingData['image'][0]['name'];
99+
} else {
100+
unset($existingData['image']);
101+
}
102+
}
96103
}
97104
} else {
98105
$parentId = $category->getParentId() ?: $this->storeManager->getStore()->getRootCategoryId();

app/code/Magento/Catalog/Model/Indexer/Product/Price/AbstractAction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ protected function _reindexRows($changedIds = [])
390390
$pairs = $this->_connection->fetchPairs($select);
391391
foreach ($pairs as $productId => $productType) {
392392
if (!in_array($productId, $changedIds)) {
393-
$changedIds[] = $productId;
393+
$changedIds[] = (string) $productId;
394394
$byType[$productType][$productId] = $productId;
395395
$compositeIds[$productId] = $productId;
396396
}

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/Controller/Adminhtml/Category/SaveTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ public function testExecute($categoryId, $storeId, $parentId)
433433
->method('getPostValue')
434434
->willReturn($postData);
435435
$addData = $postData;
436-
$addData['image_additional_data'] = ['delete' => true];
436+
$addData['image'] = ['delete' => true];
437437
$categoryMock->expects($this->once())
438438
->method('addData')
439439
->with($addData);

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ public function filterExtraFieldsOnUpdateCategoryDataProvider()
173173
['level' => '1', 'path' => '1/2', 'image' => ['categoryImage'], 'name' => 'category'],
174174
[
175175
'store_id' => 1,
176-
'image_additional_data' => ['categoryImage'],
177176
'name' => 'category',
178177
'entity_id' => null
179178
]

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);
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\CatalogSearch\Model\Indexer\Fulltext\Plugin;
8+
9+
use Magento\Catalog\Model\ResourceModel\Category as ResourceCategory;
10+
use Magento\Framework\Model\AbstractModel;
11+
12+
/**
13+
* Class Category
14+
* @package Magento\CatalogSearch\Model\Indexer\Fulltext\Plugin
15+
*/
16+
class Category extends AbstractPlugin
17+
{
18+
/**
19+
* Reindex on product save
20+
*
21+
* @param ResourceCategory $resourceCategory
22+
* @param \Closure $proceed
23+
* @param AbstractModel $category
24+
* @return ResourceCategory
25+
* @throws \Exception
26+
*/
27+
public function aroundSave(ResourceCategory $resourceCategory, \Closure $proceed, AbstractModel $category)
28+
{
29+
return $this->addCommitCallback($resourceCategory, $proceed, $category);
30+
}
31+
32+
/**
33+
* @param ResourceCategory $resourceCategory
34+
* @param \Closure $proceed
35+
* @param AbstractModel $category
36+
* @return ResourceCategory
37+
* @throws \Exception
38+
*/
39+
private function addCommitCallback(ResourceCategory $resourceCategory, \Closure $proceed, AbstractModel $category)
40+
{
41+
try {
42+
$resourceCategory->beginTransaction();
43+
$result = $proceed($category);
44+
$resourceCategory->addCommitCallback(function () use ($category) {
45+
$affectedProducts = $category->getAffectedProductIds();
46+
if (is_array($affectedProducts)) {
47+
$this->reindexList($affectedProducts);
48+
}
49+
});
50+
$resourceCategory->commit();
51+
} catch (\Exception $e) {
52+
$resourceCategory->rollBack();
53+
throw $e;
54+
}
55+
56+
return $result;
57+
}
58+
}

0 commit comments

Comments
 (0)