Skip to content

Commit 296877d

Browse files
committed
Merge remote-tracking branch 'mainline/develop' into troll-bugfixes
# Conflicts: # app/code/Magento/Catalog/Controller/Adminhtml/Product/Validate.php
2 parents 59cb1ba + 4e1a1bc commit 296877d

File tree

99 files changed

+2847
-3648
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+2847
-3648
lines changed

app/code/Magento/Catalog/Controller/Adminhtml/Product/Initialization/Helper.php

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,17 @@ public function __construct(
101101
}
102102

103103
/**
104-
* Initialize product before saving
104+
* Initialize product from data
105105
*
106106
* @param \Magento\Catalog\Model\Product $product
107+
* @param array $productData
107108
* @return \Magento\Catalog\Model\Product
108109
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
109110
* @SuppressWarnings(PHPMD.NPathComplexity)
110111
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
111112
*/
112-
public function initialize(\Magento\Catalog\Model\Product $product)
113+
public function initializeFromData(\Magento\Catalog\Model\Product $product, array $productData)
113114
{
114-
$productData = (array)$this->request->getPost('product', []);
115115
unset($productData['custom_attributes']);
116116
unset($productData['extension_attributes']);
117117

@@ -157,6 +157,12 @@ public function initialize(\Magento\Catalog\Model\Product $product)
157157
$inputFilter = new \Zend_Filter_Input($dateFieldFilters, [], $productData);
158158
$productData = $inputFilter->getUnescaped();
159159

160+
if (isset($productData['options'])) {
161+
$productOptions = $productData['options'];
162+
unset($productData['options']);
163+
} else {
164+
$productOptions = [];
165+
}
160166
$product->addData($productData);
161167

162168
if ($wasLockedMedia) {
@@ -183,15 +189,20 @@ public function initialize(\Magento\Catalog\Model\Product $product)
183189
/**
184190
* Initialize product options
185191
*/
186-
if (isset($productData['options']) && !$product->getOptionsReadonly()) {
192+
if ($productOptions && !$product->getOptionsReadonly()) {
187193
// mark custom options that should to fall back to default value
188194
$options = $this->mergeProductOptions(
189-
$productData['options'],
195+
$productOptions,
190196
$this->request->getPost('options_use_default')
191197
);
192198
$customOptions = [];
193199
foreach ($options as $customOptionData) {
194200
if (empty($customOptionData['is_delete'])) {
201+
if (isset($customOptionData['values'])) {
202+
$customOptionData['values'] = array_filter($customOptionData['values'], function ($valueData) {
203+
return empty($valueData['is_delete']);
204+
});
205+
}
195206
$customOption = $this->getCustomOptionFactory()->create(['data' => $customOptionData]);
196207
$customOption->setProductSku($product->getSku());
197208
$customOption->setOptionId(null);
@@ -207,6 +218,18 @@ public function initialize(\Magento\Catalog\Model\Product $product)
207218

208219
return $product;
209220
}
221+
222+
/**
223+
* Initialize product before saving
224+
*
225+
* @param \Magento\Catalog\Model\Product $product
226+
* @return \Magento\Catalog\Model\Product
227+
*/
228+
public function initialize(\Magento\Catalog\Model\Product $product)
229+
{
230+
$productData = $this->request->getPost('product', []);
231+
return $this->initializeFromData($product, $productData);
232+
}
210233

211234
/**
212235
* Setting product links

app/code/Magento/Catalog/Controller/Adminhtml/Product/NewAction.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use Magento\Backend\App\Action;
1010
use Magento\Catalog\Controller\Adminhtml\Product;
11+
use Magento\Framework\App\ObjectManager;
1112

1213
class NewAction extends \Magento\Catalog\Controller\Adminhtml\Product
1314
{
@@ -26,6 +27,11 @@ class NewAction extends \Magento\Catalog\Controller\Adminhtml\Product
2627
*/
2728
protected $resultForwardFactory;
2829

30+
/**
31+
* @var Initialization\Helper
32+
*/
33+
protected $initializationHelper;
34+
2935
/**
3036
* @param Action\Context $context
3137
* @param Builder $productBuilder
@@ -69,7 +75,7 @@ public function execute()
6975
if ($productData) {
7076
$stockData = isset($productData['stock_data']) ? $productData['stock_data'] : [];
7177
$productData['stock_data'] = $this->stockFilter->filter($stockData);
72-
$product->addData($productData);
78+
$product = $this->getInitializationHelper()->initializeFromData($product, $productData);
7379
}
7480

7581
$this->_eventManager->dispatch('catalog_product_new_action', ['product' => $product]);
@@ -92,4 +98,16 @@ public function execute()
9298

9399
return $resultPage;
94100
}
101+
102+
/**
103+
* @return Initialization\Helper
104+
* @deprecated
105+
*/
106+
protected function getInitializationHelper()
107+
{
108+
if (null === $this->initializationHelper) {
109+
$this->initializationHelper = ObjectManager::getInstance()->get(Initialization\Helper::class);
110+
}
111+
return $this->initializationHelper;
112+
}
95113
}

app/code/Magento/Catalog/Controller/Adminhtml/Product/Validate.php

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use Magento\Backend\App\Action;
1010
use Magento\Catalog\Controller\Adminhtml\Product;
11+
use Magento\Framework\App\ObjectManager;
1112

1213
/**
1314
* Product validate
@@ -42,9 +43,9 @@ class Validate extends \Magento\Catalog\Controller\Adminhtml\Product
4243
protected $productFactory;
4344

4445
/**
45-
* @var \Magento\Framework\Stdlib\DateTime\Filter\DateTime
46+
* @var Initialization\Helper
4647
*/
47-
private $dateTimeFilter;
48+
protected $initializationHelper;
4849

4950
/**
5051
* @param Action\Context $context
@@ -85,7 +86,7 @@ public function execute()
8586
$response->setError(false);
8687

8788
try {
88-
$productData = $this->getRequest()->getPost('product');
89+
$productData = $this->getRequest()->getPost('product', []);
8990

9091
if ($productData && !isset($productData['stock_data']['use_config_manage_stock'])) {
9192
$productData['stock_data']['use_config_manage_stock'] = 0;
@@ -109,19 +110,7 @@ public function execute()
109110
if ($productId) {
110111
$product->load($productId);
111112
}
112-
113-
$dateFieldFilters = [];
114-
$attributes = $product->getAttributes();
115-
foreach ($attributes as $attrKey => $attribute) {
116-
if ($attribute->getBackend()->getType() == 'datetime') {
117-
if (array_key_exists($attrKey, $productData) && $productData[$attrKey] != '') {
118-
$dateFieldFilters[$attrKey] = $this->getDateTimeFilter();
119-
}
120-
}
121-
}
122-
$inputFilter = new \Zend_Filter_Input($dateFieldFilters, [], $productData);
123-
$productData = $inputFilter->getUnescaped();
124-
$product->addData($productData);
113+
$product = $this->getInitializationHelper()->initializeFromData($product, $productData);
125114

126115
/* set restrictions for date ranges */
127116
$resource = $product->getResource();
@@ -149,16 +138,14 @@ public function execute()
149138
}
150139

151140
/**
152-
* @return \Magento\Framework\Stdlib\DateTime\Filter\DateTime
153-
*
141+
* @return Initialization\Helper
154142
* @deprecated
155143
*/
156-
private function getDateTimeFilter()
144+
protected function getInitializationHelper()
157145
{
158-
if ($this->dateTimeFilter === null) {
159-
$this->dateTimeFilter = \Magento\Framework\App\ObjectManager::getInstance()
160-
->get(\Magento\Framework\Stdlib\DateTime\Filter\DateTime::class);
146+
if (null === $this->initializationHelper) {
147+
$this->initializationHelper = ObjectManager::getInstance()->get(Initialization\Helper::class);
161148
}
162-
return $this->dateTimeFilter;
149+
return $this->initializationHelper;
163150
}
164151
}

app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Product/Initialization/HelperTest.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,6 @@ protected function setUp()
166166
->disableOriginalConstructor()
167167
->getMock();
168168

169-
$this->customOptionFactoryMock->expects($this->any())
170-
->method('create')
171-
->with(['data' => ['is_delete' => false]])
172-
->willReturn($this->customOptionMock);
173169
$this->productLinksMock->expects($this->any())
174170
->method('initializeLinks')
175171
->willReturn($this->productMock);
@@ -215,9 +211,13 @@ public function testInitialize()
215211
$this->customOptionMock->expects($this->once())
216212
->method('setOptionId');
217213

214+
$optionsData = [
215+
'option1' => ['is_delete' => true, 'name' => 'name1', 'price' => 'price1'],
216+
'option2' => ['is_delete' => false, 'name' => 'name1', 'price' => 'price1'],
217+
];
218218
$productData = [
219219
'stock_data' => ['stock_data'],
220-
'options' => ['option1' => ['is_delete' => true], 'option2' => ['is_delete' => false]]
220+
'options' => $optionsData,
221221
];
222222
$attributeNonDate = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class)
223223
->disableOriginalConstructor()
@@ -292,6 +292,7 @@ public function testInitialize()
292292

293293
$productData['category_ids'] = [];
294294
$productData['website_ids'] = [];
295+
unset($productData['options']);
295296

296297
$this->productMock->expects($this->once())
297298
->method('addData')
@@ -305,6 +306,11 @@ public function testInitialize()
305306
$this->productMock->expects($this->any())
306307
->method('getOptionsReadOnly')
307308
->willReturn(false);
309+
310+
$this->customOptionFactoryMock->expects($this->any())
311+
->method('create')
312+
->with(['data' => $optionsData['option2']])
313+
->willReturn($this->customOptionMock);
308314
$this->productMock->expects($this->once())
309315
->method('setOptions')
310316
->with([$this->customOptionMock]);

app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Product/NewActionTest.php

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,33 @@
66
*/
77
namespace Magento\Catalog\Test\Unit\Controller\Adminhtml\Product;
88

9+
use Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper;
10+
use Magento\Catalog\Controller\Adminhtml\Product\Initialization\StockDataFilter;
11+
use Magento\Catalog\Controller\Adminhtml\Product\NewAction;
12+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
13+
914
class NewActionTest extends \Magento\Catalog\Test\Unit\Controller\Adminhtml\ProductTest
1015
{
11-
/** @var \Magento\Catalog\Controller\Adminhtml\Product\NewAction */
16+
/** @var NewAction */
1217
protected $action;
18+
1319
/** @var \Magento\Backend\Model\View\Result\Page|\PHPUnit_Framework_MockObject_MockObject */
1420
protected $resultPage;
21+
1522
/** @var \Magento\Backend\Model\View\Result\Forward|\PHPUnit_Framework_MockObject_MockObject */
1623
protected $resultForward;
24+
1725
/** @var \Magento\Catalog\Controller\Adminhtml\Product\Builder|\PHPUnit_Framework_MockObject_MockObject */
1826
protected $productBuilder;
27+
1928
/** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject */
2029
protected $product;
2130

31+
/**
32+
* @var Helper|\PHPUnit_Framework_MockObject_MockObject
33+
*/
34+
protected $initializationHelper;
35+
2236
protected function setUp()
2337
{
2438
$this->productBuilder = $this->getMock(
@@ -56,14 +70,20 @@ protected function setUp()
5670
$resultForwardFactory->expects($this->any())
5771
->method('create')
5872
->willReturn($this->resultForward);
73+
$this->initializationHelper = $this->getMock(Helper::class, [], [], '', false);
74+
$stockDataFilter = $this->getMockBuilder(StockDataFilter::class)
75+
->disableOriginalConstructor()->getMock();
5976

60-
$this->action = new \Magento\Catalog\Controller\Adminhtml\Product\NewAction(
61-
$this->initContext(),
62-
$this->productBuilder,
63-
$this->getMockBuilder('Magento\Catalog\Controller\Adminhtml\Product\Initialization\StockDataFilter')
64-
->disableOriginalConstructor()->getMock(),
65-
$resultPageFactory,
66-
$resultForwardFactory
77+
$this->action = (new ObjectManager($this))->getObject(
78+
NewAction::class,
79+
[
80+
'context' => $this->initContext(),
81+
'productBuilder' => $this->productBuilder,
82+
'stockFilter' => $stockDataFilter,
83+
'resultPageFactory' => $resultPageFactory,
84+
'resultForwardFactory' => $resultForwardFactory,
85+
'initializationHelper' => $this->initializationHelper,
86+
]
6787
);
6888

6989
$this->resultPage->expects($this->atLeastOnce())
@@ -85,10 +105,15 @@ public function testExecuteObtainsProductDataFromSession()
85105
$this->action->getRequest()->expects($this->any())->method('getFullActionName')
86106
->willReturn('catalog_product_new');
87107

108+
$productData = ['name' => 'test-name', 'stock_data' => null];
88109
$this->session->expects($this->any())->method('getProductData')
89-
->willReturn(['product' => ['name' => 'test-name']]);
110+
->willReturn(['product' => $productData]);
90111

91-
$this->product->expects($this->once())->method('addData')->with(['name' => 'test-name', 'stock_data' => null]);
112+
$this->initializationHelper
113+
->expects($this->once())
114+
->method('initializeFromData')
115+
->with($this->product, $productData)
116+
->willReturn($this->product);
92117

93118
$this->action->execute();
94119
}

0 commit comments

Comments
 (0)