Skip to content

Commit e1bd045

Browse files
author
Onischenko, Yaroslav(yonischenko)
committed
Merge pull request #614 from magento-troll/Troll-PR
[Troll] Bugfixes
2 parents 66a92cb + e018561 commit e1bd045

File tree

12 files changed

+200
-25
lines changed

12 files changed

+200
-25
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/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
]
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+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\CatalogSearch\Test\Unit\Model\Indexer\Fulltext\Plugin;
8+
9+
use Magento\Catalog\Model\Category as CategoryModel;
10+
use Magento\Catalog\Model\ResourceModel\Category as CategoryResourceModel;
11+
use Magento\CatalogSearch\Model\Indexer\Fulltext\Plugin\Category as CategoryPlugin;
12+
use Magento\Framework\DB\Adapter\AdapterInterface;
13+
use Magento\Framework\Indexer\IndexerInterface;
14+
use Magento\Framework\Indexer\IndexerRegistry;
15+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
16+
17+
class CategoryTest extends \PHPUnit_Framework_TestCase
18+
{
19+
/**
20+
* @var \PHPUnit_Framework_MockObject_MockObject|IndexerInterface
21+
*/
22+
protected $indexerMock;
23+
24+
/**
25+
* @var \PHPUnit_Framework_MockObject_MockObject|CategoryResourceModel
26+
*/
27+
protected $categoryResourceMock;
28+
29+
/**
30+
* @var \PHPUnit_Framework_MockObject_MockObject|CategoryModel
31+
*/
32+
protected $categoryMock;
33+
34+
/**
35+
* @var \Closure
36+
*/
37+
protected $proceed;
38+
39+
/**
40+
* @var IndexerRegistry|\PHPUnit_Framework_MockObject_MockObject
41+
*/
42+
protected $indexerRegistryMock;
43+
44+
/**
45+
* @var CategoryPlugin
46+
*/
47+
protected $model;
48+
49+
protected function setUp()
50+
{
51+
$this->categoryMock = $this->getMockBuilder(CategoryModel::class)
52+
->disableOriginalConstructor()
53+
->getMock();
54+
$this->categoryResourceMock = $this->getMockBuilder(CategoryResourceModel::class)
55+
->disableOriginalConstructor()
56+
->getMock();
57+
$connection = $this->getMockBuilder(AdapterInterface::class)
58+
->disableOriginalConstructor()
59+
->getMockForAbstractClass();
60+
$this->categoryResourceMock->method('getConnection')->willReturn($connection);
61+
62+
$this->indexerMock = $this->getMockBuilder(IndexerInterface::class)
63+
->disableOriginalConstructor()
64+
->setMethods(['getId', 'getState', '__wakeup'])
65+
->getMockForAbstractClass();
66+
$this->indexerRegistryMock = $this->getMockBuilder(IndexerRegistry::class)
67+
->disableOriginalConstructor()
68+
->setMethods(['get'])
69+
->getMock();
70+
71+
$this->proceed = function () {
72+
return $this->categoryResourceMock;
73+
};
74+
75+
$this->model = (new ObjectManager($this))->getObject(
76+
CategoryPlugin::class,
77+
['indexerRegistry' => $this->indexerRegistryMock]
78+
);
79+
}
80+
81+
public function testAfterSaveNonScheduled()
82+
{
83+
$this->categoryResourceMock->expects($this->once())->method('addCommitCallback');
84+
$this->assertEquals(
85+
$this->categoryResourceMock,
86+
$this->model->aroundSave($this->categoryResourceMock, $this->proceed, $this->categoryMock)
87+
);
88+
}
89+
90+
public function testAfterSaveScheduled()
91+
{
92+
$this->categoryResourceMock->expects($this->once())->method('addCommitCallback');
93+
$this->assertEquals(
94+
$this->categoryResourceMock,
95+
$this->model->aroundSave($this->categoryResourceMock, $this->proceed, $this->categoryMock)
96+
);
97+
}
98+
99+
protected function prepareIndexer()
100+
{
101+
$this->indexerRegistryMock->expects($this->once())
102+
->method('get')
103+
->with(\Magento\CatalogSearch\Model\Indexer\Fulltext::INDEXER_ID)
104+
->will($this->returnValue($this->indexerMock));
105+
}
106+
}

app/code/Magento/CatalogSearch/etc/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545
<type name="Magento\Catalog\Model\ResourceModel\Product">
4646
<plugin name="catalogsearchFulltextProduct" type="Magento\CatalogSearch\Model\Indexer\Fulltext\Plugin\Product"/>
4747
</type>
48+
<type name="Magento\Catalog\Model\ResourceModel\Category">
49+
<plugin name="catalogsearchFulltextCategory" type="Magento\CatalogSearch\Model\Indexer\Fulltext\Plugin\Category"/>
50+
</type>
4851
<type name="Magento\Catalog\Model\Product\Action">
4952
<plugin name="catalogsearchFulltextMassAction" type="Magento\CatalogSearch\Model\Indexer\Fulltext\Plugin\Product\Action"/>
5053
</type>

app/code/Magento/Sales/Model/ResourceModel/Report/Bestsellers.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ public function aggregate($from = null, $to = null)
132132
'product_id' => 'order_item.product_id',
133133
'product_name' => new \Zend_Db_Expr('MIN(order_item.name)'),
134134
'product_price' => new \Zend_Db_Expr(
135-
'MIN(order_item.base_price) * MIN(source_table.base_to_global_rate)'
135+
'MIN(IF(order_item_parent.base_price, order_item_parent.base_price, order_item.base_price))' .
136+
'* MIN(source_table.base_to_global_rate)'
136137
),
137138
'qty_ordered' => new \Zend_Db_Expr('SUM(order_item.qty_ordered)'),
138139
];
@@ -144,6 +145,10 @@ public function aggregate($from = null, $to = null)
144145
['order_item' => $this->getTable('sales_order_item')],
145146
'order_item.order_id = source_table.entity_id',
146147
[]
148+
)->joinLeft(
149+
['order_item_parent' => $this->getTable('sales_order_item')],
150+
'order_item.parent_item_id = order_item_parent.item_id',
151+
[]
147152
)->where(
148153
'source_table.state != ?',
149154
\Magento\Sales\Model\Order::STATE_CANCELED

0 commit comments

Comments
 (0)