Skip to content

Commit f84dd18

Browse files
committed
MAGETWO-33625: Attribute value uniqueness isn't checked for custom product template
1 parent b32bd91 commit f84dd18

File tree

2 files changed

+147
-3
lines changed
  • app/code/Magento/Catalog/Controller/Adminhtml/Product
  • dev/tests/unit/testsuite/Magento/Catalog/Controller/Adminhtml/Product

2 files changed

+147
-3
lines changed

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,33 @@ class Validate extends \Magento\Catalog\Controller\Adminhtml\Product
3131
*/
3232
protected $layoutFactory;
3333

34+
/** @var \Magento\Catalog\Model\ProductFactory */
35+
protected $productFactory;
36+
3437
/**
3538
* @param Action\Context $context
3639
* @param Builder $productBuilder
3740
* @param \Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter
3841
* @param \Magento\Catalog\Model\Product\Validator $productValidator
3942
* @param \Magento\Framework\Controller\Result\JSONFactory $resultJsonFactory
4043
* @param \Magento\Framework\View\LayoutFactory $layoutFactory
44+
* @param \Magento\Catalog\Model\ProductFactory $productFactory
4145
*/
4246
public function __construct(
4347
\Magento\Backend\App\Action\Context $context,
4448
Product\Builder $productBuilder,
4549
\Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter,
4650
\Magento\Catalog\Model\Product\Validator $productValidator,
4751
\Magento\Framework\Controller\Result\JSONFactory $resultJsonFactory,
48-
\Magento\Framework\View\LayoutFactory $layoutFactory
52+
\Magento\Framework\View\LayoutFactory $layoutFactory,
53+
\Magento\Catalog\Model\ProductFactory $productFactory
4954
) {
5055
$this->_dateFilter = $dateFilter;
5156
$this->productValidator = $productValidator;
5257
parent::__construct($context, $productBuilder);
5358
$this->resultJsonFactory = $resultJsonFactory;
5459
$this->layoutFactory = $layoutFactory;
60+
$this->productFactory = $productFactory;
5561
}
5662

5763
/**
@@ -73,13 +79,13 @@ public function execute()
7379
$productData['stock_data']['use_config_manage_stock'] = 0;
7480
}
7581
/* @var $product \Magento\Catalog\Model\Product */
76-
$product = $this->_objectManager->create('Magento\Catalog\Model\Product');
82+
$product = $this->productFactory->create();
7783
$product->setData('_edit_mode', true);
7884
$storeId = $this->getRequest()->getParam('store');
7985
if ($storeId) {
8086
$product->setStoreId($storeId);
8187
}
82-
$setId = $this->getRequest()->getParam('set');
88+
$setId = $this->getRequest()->getPost('set');
8389
if ($setId) {
8490
$product->setAttributeSetId($setId);
8591
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Catalog\Controller\Adminhtml\Product;
7+
8+
use Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper;
9+
use Magento\TestFramework\Helper\ObjectManager as ObjectManagerHelper;
10+
11+
class ValidateTest extends \Magento\Catalog\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+
public function testAttributeSetIsObtainedFromPost()
130+
{
131+
$this->request->expects($this->any())->method('getParam')->willReturnMap([['set', null, 4]]);
132+
$this->request->expects($this->any())->method('getPost')->willReturnMap([['set', null, 9]]);
133+
134+
$this->product->expects($this->once())->method('setAttributeSetId')->with(9);
135+
136+
$this->action->execute();
137+
}
138+
}

0 commit comments

Comments
 (0)