Skip to content

Commit b3a5c0a

Browse files
authored
Merge pull request #738 from magento-tsg/2.0-pr7
MAGETWO-62647: "Invalid option value" Error message is shown when delete option row without data MAGETWO-62680: Restricted Admin User mustn't see info from other store
2 parents ea6f29c + c627661 commit b3a5c0a

File tree

21 files changed

+706
-126
lines changed

21 files changed

+706
-126
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
* Copyright © 2016 Magento. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Catalog\Controller\Adminhtml\Product\Initialization;
78

9+
/**
10+
* Product initialization helper class
11+
*/
812
class Helper
913
{
1014
/**
@@ -71,7 +75,7 @@ public function __construct(
7175
*/
7276
public function initialize(\Magento\Catalog\Model\Product $product)
7377
{
74-
$productData = $this->request->getPost('product');
78+
$productData = $this->request->getPost('product', []);
7579
unset($productData['custom_attributes']);
7680
unset($productData['extension_attributes']);
7781

@@ -144,6 +148,13 @@ public function initialize(\Magento\Catalog\Model\Product $product)
144148
$productData['options'],
145149
$this->request->getPost('options_use_default')
146150
);
151+
foreach ($options as &$customOptionData) {
152+
if (isset($customOptionData['values'])) {
153+
$customOptionData['values'] = array_filter($customOptionData['values'], function ($valueData) {
154+
return !($valueData['option_type_id'] == '-1' && !empty($valueData['is_delete']));
155+
});
156+
}
157+
}
147158
$product->setProductOptions($options);
148159
}
149160

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
use Magento\Backend\App\Action;
1010
use Magento\Catalog\Controller\Adminhtml\Product;
1111

12+
/**
13+
* Class that handle new product creation
14+
*/
1215
class NewAction extends \Magento\Catalog\Controller\Adminhtml\Product
1316
{
1417
/**

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

Lines changed: 97 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,59 @@
33
* Copyright © 2016 Magento. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Catalog\Test\Unit\Controller\Adminhtml\Product\Initialization;
78

8-
use \Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper;
9+
use Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper;
10+
use Magento\Catalog\Controller\Adminhtml\Product\Initialization\StockDataFilter;
11+
use Magento\Catalog\Model\Product;
12+
use Magento\Catalog\Model\Product\Option;
13+
use Magento\Framework\App\RequestInterface;
14+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
15+
use Magento\Store\Model\StoreManagerInterface;
16+
use Magento\Catalog\Model\Product\Initialization\Helper\ProductLinks;
917

18+
/**
19+
* Class HelperTest
20+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
21+
* @SuppressWarnings(PHPMD.ExcessivePublicCount)
22+
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
23+
* @SuppressWarnings(PHPMD.TooManyFields)
24+
*/
1025
class HelperTest extends \PHPUnit_Framework_TestCase
1126
{
27+
/**
28+
* @var ObjectManager
29+
*/
30+
protected $objectManager;
31+
32+
/**
33+
* @var Helper
34+
*/
35+
protected $helper;
36+
1237
/**
1338
* @var \PHPUnit_Framework_MockObject_MockObject
1439
*/
1540
protected $requestMock;
1641

1742
/**
18-
* @var \PHPUnit_Framework_MockObject_MockObject
43+
* @var StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
1944
*/
2045
protected $storeManagerMock;
2146

2247
/**
23-
* @var \PHPUnit_Framework_MockObject_MockObject
48+
* @var StockDataFilter|\PHPUnit_Framework_MockObject_MockObject
2449
*/
2550
protected $stockFilterMock;
2651

2752
/**
28-
* @var \PHPUnit_Framework_MockObject_MockObject
53+
* @var ProductLinks|\PHPUnit_Framework_MockObject_MockObject
2954
*/
3055
protected $productLinksMock;
3156

3257
/**
33-
* @var \PHPUnit_Framework_MockObject_MockObject
58+
* @var Product|\PHPUnit_Framework_MockObject_MockObject
3459
*/
3560
protected $productMock;
3661

@@ -55,43 +80,45 @@ class HelperTest extends \PHPUnit_Framework_TestCase
5580
protected $websiteId = 1;
5681

5782
/**
58-
* @var \Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper
59-
*/
60-
protected $helper;
61-
62-
/**
63-
* @var \PHPUnit_Framework_MockObject_MockObject
83+
* @var \Magento\Backend\Helper\Js|\PHPUnit_Framework_MockObject_MockObject
6484
*/
6585
protected $jsHelperMock;
6686

6787
protected function setUp()
6888
{
69-
$this->requestMock = $this->getMock('Magento\Framework\App\Request\Http', [], [], '', false);
70-
$this->jsHelperMock = $this->getMock('Magento\Backend\Helper\Js', [], [], '', false);
71-
$this->storeMock = $this->getMock('Magento\Store\Model\Store', [], [], '', false);
72-
$this->websiteMock = $this->getMock('Magento\Store\Model\Website', [], [], '', false);
73-
$this->storeManagerMock = $this->getMock('Magento\Store\Model\StoreManagerInterface');
74-
$this->dateFilterMock = $this->getMock('\Magento\Framework\Stdlib\DateTime\Filter\Date', [], [], '', false);
75-
76-
$this->stockFilterMock = $this->getMock(
77-
'Magento\Catalog\Controller\Adminhtml\Product\Initialization\StockDataFilter',
89+
$this->objectManager = new ObjectManager($this);
90+
$this->requestMock = $this->getMockBuilder(RequestInterface::class)
91+
->setMethods(['getPost'])
92+
->getMockForAbstractClass();
93+
94+
$this->jsHelperMock = $this->getMock(\Magento\Backend\Helper\Js::class, [], [], '', false);
95+
$this->storeMock = $this->getMock(\Magento\Store\Model\Store::class, [], [], '', false);
96+
$this->websiteMock = $this->getMock(\Magento\Store\Model\Website::class, [], [], '', false);
97+
$this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)
98+
->getMockForAbstractClass();
99+
$this->dateFilterMock = $this->getMock(
100+
\Magento\Framework\Stdlib\DateTime\Filter\Date::class,
78101
[],
79102
[],
80103
'',
81104
false
82105
);
106+
107+
$this->stockFilterMock = $this->getMockBuilder(StockDataFilter::class)
108+
->disableOriginalConstructor()
109+
->getMock();
110+
83111
$this->productLinksMock = $this->getMock(
84-
'Magento\Catalog\Model\Product\Initialization\Helper\ProductLinks',
112+
\Magento\Catalog\Model\Product\Initialization\Helper\ProductLinks::class,
85113
[],
86114
[],
87115
'',
88116
false
89117
);
90118

91119
$this->productMock = $this->getMock(
92-
'Magento\Catalog\Model\Product',
120+
\Magento\Catalog\Model\Product::class,
93121
[
94-
'setData',
95122
'addData',
96123
'getId',
97124
'setWebsiteIds',
@@ -100,7 +127,6 @@ protected function setUp()
100127
'getAttributes',
101128
'unlockAttribute',
102129
'getOptionsReadOnly',
103-
'setProductOptions',
104130
'setCanSaveCustomOptions',
105131
'__sleep',
106132
'__wakeup'
@@ -109,6 +135,15 @@ protected function setUp()
109135
'',
110136
false
111137
);
138+
139+
$this->helper = $this->objectManager->getObject(Helper::class, [
140+
'request' => $this->requestMock,
141+
'storeManager' => $this->storeManagerMock,
142+
'stockFilter' => $this->stockFilterMock,
143+
'productLinks' => $this->productLinksMock,
144+
'jsHelper' => $this->jsHelperMock,
145+
'dateFilter' => $this->dateFilterMock,
146+
]);
112147
}
113148

114149
/**
@@ -117,6 +152,20 @@ protected function setUp()
117152
*/
118153
public function testInitialize()
119154
{
155+
$optionsData = [
156+
'option1' => ['is_delete' => false, 'name' => 'name1', 'price' => 'price1', 'option_id' => '13'],
157+
'option2' => ['is_delete' => false, 'name' => 'name1', 'price' => 'price1', 'option_id' => '14',
158+
'values' => [
159+
'value1' => ['option_type_id' => 1, 'is_delete' =>''],
160+
'value2' => ['option_type_id' => '-1', 'is_delete' =>'1']
161+
]
162+
],
163+
];
164+
$productData = [
165+
'stock_data' => ['stock_data'],
166+
'options' => $optionsData,
167+
];
168+
120169
$this->websiteMock->expects($this->once())
121170
->method('getId')
122171
->will($this->returnValue($this->websiteId));
@@ -130,28 +179,20 @@ public function testInitialize()
130179
->with(true)
131180
->will($this->returnValue($this->storeMock));
132181

133-
$this->jsHelperMock = $this->getMock('\Magento\Backend\Helper\Js', [], [], '', false);
134-
$this->helper = new Helper(
135-
$this->requestMock,
136-
$this->storeManagerMock,
137-
$this->stockFilterMock,
138-
$this->productLinksMock,
139-
$this->jsHelperMock,
140-
$this->dateFilterMock
141-
);
142-
143-
$productData = [
144-
'stock_data' => ['stock_data'],
145-
'options' => ['option1', 'option2']
146-
];
147-
148-
$attributeNonDate = $this->getMock('Magento\Catalog\Model\ResourceModel\Eav\Attribute', [], [], '', false);
149-
$attributeDate = $this->getMock('Magento\Catalog\Model\ResourceModel\Eav\Attribute', [], [], '', false);
182+
$attributeNonDate = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class)
183+
->disableOriginalConstructor()
184+
->getMock();
185+
$attributeDate = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class)
186+
->disableOriginalConstructor()
187+
->getMock();
150188

151189
$attributeNonDateBackEnd =
152-
$this->getMock('Magento\Eav\Model\Entity\Attribute\Backend\DefaultBackend', [], [], '', false);
153-
$attributeDateBackEnd =
154-
$this->getMock('Magento\Eav\Model\Entity\Attribute\Backend\Datetime', [], [], '', false);
190+
$this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\Backend\DefaultBackend::class)
191+
->disableOriginalConstructor()
192+
->getMock();
193+
$attributeDateBackEnd = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\Backend\Datetime::class)
194+
->disableOriginalConstructor()
195+
->getMock();
155196

156197
$attributeNonDate->expects($this->any())
157198
->method('getBackend')
@@ -177,6 +218,13 @@ public function testInitialize()
177218

178219
$useDefaults = ['attributeCode1', 'attributeCode2'];
179220

221+
$this->requestMock->expects($this->any())->method('getPost')->willReturnMap(
222+
[
223+
['product', [], $productData],
224+
['use_default', null, $useDefaults]
225+
]
226+
);
227+
180228
$this->requestMock->expects($this->at(0))
181229
->method('getPost')
182230
->with('product')
@@ -230,6 +278,7 @@ public function testInitialize()
230278

231279
$productData['category_ids'] = [];
232280
$productData['website_ids'] = [];
281+
233282
$this->productMock->expects($this->once())
234283
->method('addData')
235284
->with($productData);
@@ -242,15 +291,11 @@ public function testInitialize()
242291
->method('getOptionsReadOnly')
243292
->will($this->returnValue(false));
244293

245-
$this->productMock->expects($this->once())
246-
->method('setProductOptions')
247-
->with($productData['options']);
248-
249-
$this->productMock->expects($this->once())
250-
->method('setCanSaveCustomOptions')
251-
->with(true);
252-
253294
$this->assertEquals($this->productMock, $this->helper->initialize($this->productMock));
295+
296+
$productOptions = $this->productMock->getProductOptions();
297+
$this->assertTrue(2 == count($productOptions));
298+
$this->assertTrue(1 == count($productOptions['option2']['values']));
254299
}
255300

256301
/**
@@ -292,15 +337,6 @@ public function mergeProductOptionsDataProvider()
292337
*/
293338
public function testMergeProductOptions($productOptions, $defaultOptions, $expectedResults)
294339
{
295-
$this->jsHelperMock = $this->getMock('\Magento\Backend\Helper\Js', [], [], '', false);
296-
$this->helper = new Helper(
297-
$this->requestMock,
298-
$this->storeManagerMock,
299-
$this->stockFilterMock,
300-
$this->productLinksMock,
301-
$this->jsHelperMock,
302-
$this->dateFilterMock
303-
);
304340
$result = $this->helper->mergeProductOptions($productOptions, $defaultOptions);
305341
$this->assertEquals($expectedResults, $result);
306342
}

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44
* Copyright © 2016 Magento. All rights reserved.
55
* See COPYING.txt for license details.
66
*/
7+
78
namespace Magento\Catalog\Test\Unit\Controller\Adminhtml\Product;
89

10+
/**
11+
* Class to test new product creation
12+
*/
913
class NewActionTest extends \Magento\Catalog\Test\Unit\Controller\Adminhtml\ProductTest
1014
{
1115
/** @var \Magento\Catalog\Controller\Adminhtml\Product\NewAction */
@@ -22,46 +26,50 @@ class NewActionTest extends \Magento\Catalog\Test\Unit\Controller\Adminhtml\Prod
2226
protected function setUp()
2327
{
2428
$this->productBuilder = $this->getMock(
25-
'Magento\Catalog\Controller\Adminhtml\Product\Builder',
29+
\Magento\Catalog\Controller\Adminhtml\Product\Builder::class,
2630
['build'],
2731
[],
2832
'',
2933
false
3034
);
31-
$this->product = $this->getMockBuilder('Magento\Catalog\Model\Product')->disableOriginalConstructor()
35+
$this->product = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
36+
->disableOriginalConstructor()
3237
->setMethods(['addData', 'getTypeId', 'getStoreId', '__sleep', '__wakeup'])->getMock();
3338
$this->product->expects($this->any())->method('getTypeId')->will($this->returnValue('simple'));
3439
$this->product->expects($this->any())->method('getStoreId')->will($this->returnValue('1'));
3540
$this->productBuilder->expects($this->any())->method('build')->will($this->returnValue($this->product));
3641

37-
$this->resultPage = $this->getMockBuilder('Magento\Backend\Model\View\Result\Page')
42+
$this->resultPage = $this->getMockBuilder(\Magento\Backend\Model\View\Result\Page::class)
3843
->disableOriginalConstructor()
3944
->getMock();
4045

41-
$resultPageFactory = $this->getMockBuilder('Magento\Framework\View\Result\PageFactory')
46+
$resultPageFactory = $this->getMockBuilder(\Magento\Framework\View\Result\PageFactory::class)
4247
->disableOriginalConstructor()
4348
->setMethods(['create'])
4449
->getMock();
4550
$resultPageFactory->expects($this->atLeastOnce())
4651
->method('create')
4752
->willReturn($this->resultPage);
4853

49-
$this->resultForward = $this->getMockBuilder('Magento\Backend\Model\View\Result\Forward')
54+
$this->resultForward = $this->getMockBuilder(\Magento\Backend\Model\View\Result\Forward::class)
5055
->disableOriginalConstructor()
5156
->getMock();
52-
$resultForwardFactory = $this->getMockBuilder('Magento\Backend\Model\View\Result\ForwardFactory')
57+
$resultForwardFactory = $this->getMockBuilder(\Magento\Backend\Model\View\Result\ForwardFactory::class)
5358
->disableOriginalConstructor()
5459
->setMethods(['create'])
5560
->getMock();
5661
$resultForwardFactory->expects($this->any())
5762
->method('create')
5863
->willReturn($this->resultForward);
5964

65+
$stockFilter = $this->getMockBuilder(
66+
\Magento\Catalog\Controller\Adminhtml\Product\Initialization\StockDataFilter::class
67+
)->disableOriginalConstructor()->getMock();
68+
6069
$this->action = new \Magento\Catalog\Controller\Adminhtml\Product\NewAction(
6170
$this->initContext(),
6271
$this->productBuilder,
63-
$this->getMockBuilder('Magento\Catalog\Controller\Adminhtml\Product\Initialization\StockDataFilter')
64-
->disableOriginalConstructor()->getMock(),
72+
$stockFilter,
6573
$resultPageFactory,
6674
$resultForwardFactory
6775
);

0 commit comments

Comments
 (0)