Skip to content

Commit 3df420d

Browse files
Michail Slabkoorlangur
authored andcommitted
Merge remote-tracking branch 'origin/MAGETWO-33625' into MAGETWO-31159-varnish
2 parents a69d1c1 + f84dd18 commit 3df420d

File tree

3 files changed

+155
-4
lines changed

3 files changed

+155
-4
lines changed

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

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

12+
/**
13+
* Product validate
14+
*
15+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
16+
*/
1217
class Validate extends \Magento\Catalog\Controller\Adminhtml\Product
1318
{
1419
/**
@@ -31,27 +36,33 @@ class Validate extends \Magento\Catalog\Controller\Adminhtml\Product
3136
*/
3237
protected $layoutFactory;
3338

39+
/** @var \Magento\Catalog\Model\ProductFactory */
40+
protected $productFactory;
41+
3442
/**
3543
* @param Action\Context $context
3644
* @param Builder $productBuilder
3745
* @param \Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter
3846
* @param \Magento\Catalog\Model\Product\Validator $productValidator
3947
* @param \Magento\Framework\Controller\Result\JSONFactory $resultJsonFactory
4048
* @param \Magento\Framework\View\LayoutFactory $layoutFactory
49+
* @param \Magento\Catalog\Model\ProductFactory $productFactory
4150
*/
4251
public function __construct(
4352
\Magento\Backend\App\Action\Context $context,
4453
Product\Builder $productBuilder,
4554
\Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter,
4655
\Magento\Catalog\Model\Product\Validator $productValidator,
4756
\Magento\Framework\Controller\Result\JSONFactory $resultJsonFactory,
48-
\Magento\Framework\View\LayoutFactory $layoutFactory
57+
\Magento\Framework\View\LayoutFactory $layoutFactory,
58+
\Magento\Catalog\Model\ProductFactory $productFactory
4959
) {
5060
$this->_dateFilter = $dateFilter;
5161
$this->productValidator = $productValidator;
5262
parent::__construct($context, $productBuilder);
5363
$this->resultJsonFactory = $resultJsonFactory;
5464
$this->layoutFactory = $layoutFactory;
65+
$this->productFactory = $productFactory;
5566
}
5667

5768
/**
@@ -73,13 +84,13 @@ public function execute()
7384
$productData['stock_data']['use_config_manage_stock'] = 0;
7485
}
7586
/* @var $product \Magento\Catalog\Model\Product */
76-
$product = $this->_objectManager->create('Magento\Catalog\Model\Product');
87+
$product = $this->productFactory->create();
7788
$product->setData('_edit_mode', true);
7889
$storeId = $this->getRequest()->getParam('store');
7990
if ($storeId) {
8091
$product->setStoreId($storeId);
8192
}
82-
$setId = $this->getRequest()->getParam('set');
93+
$setId = $this->getRequest()->getPost('set');
8394
if ($setId) {
8495
$product->setAttributeSetId($setId);
8596
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Catalog\Test\Unit\Controller\Adminhtml\Product;
7+
8+
use Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper;
9+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
10+
11+
class ValidateTest extends \Magento\Catalog\Test\Unit\Controller\Adminhtml\ProductTest
12+
{
13+
/** @var \Magento\Catalog\Controller\Adminhtml\Product\Validate */
14+
protected $action;
15+
/** @var \Magento\Backend\Model\View\Result\Page|\PHPUnit_Framework_MockObject_MockObject */
16+
protected $resultPage;
17+
/** @var \Magento\Backend\Model\View\Result\Forward|\PHPUnit_Framework_MockObject_MockObject */
18+
protected $resultForward;
19+
/** @var \Magento\Catalog\Controller\Adminhtml\Product\Builder|\PHPUnit_Framework_MockObject_MockObject */
20+
protected $productBuilder;
21+
/** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject */
22+
protected $product;
23+
/** @var \Magento\Backend\Model\View\Result\RedirectFactory|\PHPUnit_Framework_MockObject_MockObject */
24+
protected $resultRedirectFactory;
25+
/** @var \Magento\Backend\Model\View\Result\Redirect|\PHPUnit_Framework_MockObject_MockObject */
26+
protected $resultRedirect;
27+
/** @var Helper|\PHPUnit_Framework_MockObject_MockObject */
28+
protected $initializationHelper;
29+
/** @var \Magento\Catalog\Model\ProductFactory|\PHPUnit_Framework_MockObject_MockObject */
30+
protected $productFactory;
31+
/** @var \Magento\Framework\Controller\Result\JSON|\PHPUnit_Framework_MockObject_MockObject */
32+
protected $resultJson;
33+
/** @var \Magento\Framework\Controller\Result\JSONFactory|\PHPUnit_Framework_MockObject_MockObject */
34+
protected $resultJsonFactory;
35+
36+
protected function setUp()
37+
{
38+
$this->productBuilder = $this->getMock(
39+
'Magento\Catalog\Controller\Adminhtml\Product\Builder',
40+
['build'],
41+
[],
42+
'',
43+
false
44+
);
45+
$this->product = $this->getMockBuilder('Magento\Catalog\Model\Product')->disableOriginalConstructor()
46+
->setMethods([
47+
'addData', 'getSku', 'getTypeId', 'getStoreId', '__sleep', '__wakeup', 'getAttributes',
48+
'setAttributeSetId',
49+
])
50+
->getMock();
51+
$this->product->expects($this->any())->method('getTypeId')->will($this->returnValue('simple'));
52+
$this->product->expects($this->any())->method('getStoreId')->will($this->returnValue('1'));
53+
$this->product->expects($this->any())->method('getAttributes')->will($this->returnValue([]));
54+
$this->productBuilder->expects($this->any())->method('build')->will($this->returnValue($this->product));
55+
56+
$this->resultPage = $this->getMockBuilder('Magento\Backend\Model\View\Result\Page')
57+
->disableOriginalConstructor()
58+
->getMock();
59+
60+
$resultPageFactory = $this->getMockBuilder('Magento\Framework\View\Result\PageFactory')
61+
->disableOriginalConstructor()
62+
->setMethods(['create'])
63+
->getMock();
64+
$resultPageFactory->expects($this->any())->method('create')->willReturn($this->resultPage);
65+
66+
$this->resultForward = $this->getMockBuilder('Magento\Backend\Model\View\Result\Forward')
67+
->disableOriginalConstructor()
68+
->getMock();
69+
$resultForwardFactory = $this->getMockBuilder('Magento\Backend\Model\View\Result\ForwardFactory')
70+
->disableOriginalConstructor()
71+
->setMethods(['create'])
72+
->getMock();
73+
$resultForwardFactory->expects($this->any())
74+
->method('create')
75+
->willReturn($this->resultForward);
76+
$this->resultPage->expects($this->any())->method('getLayout')->willReturn($this->layout);
77+
$this->resultRedirectFactory = $this->getMock(
78+
'Magento\Backend\Model\View\Result\RedirectFactory',
79+
['create'],
80+
[],
81+
'',
82+
false
83+
);
84+
$this->resultRedirect = $this->getMock(
85+
'Magento\Backend\Model\View\Result\Redirect',
86+
[],
87+
[],
88+
'',
89+
false
90+
);
91+
$this->resultRedirectFactory->expects($this->any())->method('create')->willReturn($this->resultRedirect);
92+
93+
$this->initializationHelper = $this->getMock(
94+
'Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper',
95+
[],
96+
[],
97+
'',
98+
false
99+
);
100+
101+
$this->productFactory = $this->getMockBuilder('Magento\Catalog\Model\ProductFactory')
102+
->disableOriginalConstructor()
103+
->setMethods(['create'])
104+
->getMock();
105+
$this->productFactory->expects($this->any())->method('create')->willReturn($this->product);
106+
107+
$this->resultJson = $this->getMock('Magento\Framework\Controller\Result\JSON', [], [], '', false);
108+
$this->resultJsonFactory = $this->getMockBuilder('Magento\Framework\Controller\Result\JSONFactory')
109+
->disableOriginalConstructor()
110+
->setMethods(['create'])
111+
->getMock();
112+
$this->resultJsonFactory->expects($this->any())->method('create')->willReturn($this->resultJson);
113+
114+
$this->action = (new ObjectManagerHelper($this))->getObject(
115+
'Magento\Catalog\Controller\Adminhtml\Product\Validate',
116+
[
117+
'context' => $this->initContext(),
118+
'productBuilder' => $this->productBuilder,
119+
'resultPageFactory' => $resultPageFactory,
120+
'resultForwardFactory' => $resultForwardFactory,
121+
'resultRedirectFactory' => $this->resultRedirectFactory,
122+
'initializationHelper' => $this->initializationHelper,
123+
'resultJsonFactory' => $this->resultJsonFactory,
124+
'productFactory' => $this->productFactory,
125+
]
126+
);
127+
}
128+
129+
/**
130+
* @return void
131+
*/
132+
public function testAttributeSetIsObtainedFromPost()
133+
{
134+
$this->request->expects($this->any())->method('getPost')->willReturnMap([['set', null, 9]]);
135+
136+
$this->product->expects($this->once())->method('setAttributeSetId')->with(9);
137+
138+
$this->action->execute();
139+
}
140+
}

app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/ProductTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ protected function initContext()
4141
->setMethods(['add'])->disableOriginalConstructor()->getMock();
4242
$title->expects($this->any())->method('prepend')->withAnyParameters()->will($this->returnSelf());
4343
$requestInterfaceMock = $this->getMockBuilder('Magento\Framework\App\Request\Http')->setMethods(
44-
['getParam', 'getFullActionName', 'getPostValue']
44+
['getParam', 'getPost', 'getFullActionName', 'getPostValue']
4545
)->disableOriginalConstructor()->getMock();
4646

4747
$responseInterfaceMock = $this->getMockBuilder('Magento\Framework\App\ResponseInterface')->setMethods(

0 commit comments

Comments
 (0)