Skip to content

Commit 1e8e460

Browse files
committed
MAGETWO-96921: Minor changes in Product Widget Chooser
1 parent d9259f4 commit 1e8e460

File tree

2 files changed

+120
-5
lines changed
  • app/code/Magento/Catalog

2 files changed

+120
-5
lines changed

app/code/Magento/Catalog/Controller/Adminhtml/Product/Widget/Chooser.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
<?php
22
/**
3-
*
43
* Copyright © Magento, Inc. All rights reserved.
54
* See COPYING.txt for license details.
65
*/
76
namespace Magento\Catalog\Controller\Adminhtml\Product\Widget;
87

8+
use Magento\Framework\Exception\NotFoundException;
9+
10+
/**
11+
* Chooser Product container Action.
12+
*/
913
class Chooser extends \Magento\Backend\App\Action
1014
{
1115
/**
@@ -25,28 +29,41 @@ class Chooser extends \Magento\Backend\App\Action
2529
*/
2630
protected $layoutFactory;
2731

32+
/**
33+
* @var \Magento\Framework\Escaper
34+
*/
35+
private $escaper;
36+
2837
/**
2938
* @param \Magento\Backend\App\Action\Context $context
3039
* @param \Magento\Framework\Controller\Result\RawFactory $resultRawFactory
3140
* @param \Magento\Framework\View\LayoutFactory $layoutFactory
41+
* @param \Magento\Framework\Escaper $escaper
3242
*/
3343
public function __construct(
3444
\Magento\Backend\App\Action\Context $context,
3545
\Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
36-
\Magento\Framework\View\LayoutFactory $layoutFactory
46+
\Magento\Framework\View\LayoutFactory $layoutFactory,
47+
\Magento\Framework\Escaper $escaper
3748
) {
3849
parent::__construct($context);
3950
$this->resultRawFactory = $resultRawFactory;
4051
$this->layoutFactory = $layoutFactory;
52+
$this->escaper = $escaper;
4153
}
4254

4355
/**
44-
* Chooser Source action
56+
* Chooser Source action.
4557
*
4658
* @return \Magento\Framework\Controller\Result\Raw
59+
* @throws \Magento\Framework\Exception\NotFoundException
4760
*/
4861
public function execute()
4962
{
63+
if (!$this->getRequest()->isPost()) {
64+
throw new NotFoundException(__('Page not found.'));
65+
}
66+
5067
$uniqId = $this->getRequest()->getParam('uniq_id');
5168
$massAction = $this->getRequest()->getParam('use_massaction', false);
5269
$productTypeId = $this->getRequest()->getParam('product_type_id', null);
@@ -57,7 +74,7 @@ public function execute()
5774
'',
5875
[
5976
'data' => [
60-
'id' => $uniqId,
77+
'id' => $this->escaper->escapeHtml($uniqId),
6178
'use_massaction' => $massAction,
6279
'product_type_id' => $productTypeId,
6380
'category_id' => $this->getRequest()->getParam('category_id'),
@@ -73,7 +90,7 @@ public function execute()
7390
'',
7491
[
7592
'data' => [
76-
'id' => $uniqId . 'Tree',
93+
'id' => $this->escaper->escapeHtml($uniqId) . 'Tree',
7794
'node_click_listener' => $productsGrid->getCategoryClickListenerJs(),
7895
'with_empty_node' => true,
7996
]
@@ -88,6 +105,7 @@ public function execute()
88105

89106
/** @var \Magento\Framework\Controller\Result\Raw $resultRaw */
90107
$resultRaw = $this->resultRawFactory->create();
108+
91109
return $resultRaw->setContents($html);
92110
}
93111
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Catalog\Test\Unit\Controller\Adminhtml\Product\Widget;
8+
9+
use Magento\Catalog\Controller\Adminhtml\Product\Widget\Chooser;
10+
use Magento\Framework\App\Action\Context;
11+
use Magento\Framework\Controller\Result\RawFactory;
12+
use Magento\Framework\View\LayoutFactory;
13+
use Magento\Framework\App\RequestInterface;
14+
use Magento\Framework\App\Request\Http;
15+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
16+
17+
/**
18+
* Unit tests for Magento\Catalog\Controller\Adminhtml\Product\Widget\Chooser.
19+
*/
20+
class ChooserTest extends \PHPUnit_Framework_TestCase
21+
{
22+
/**
23+
* @var Chooser
24+
*/
25+
private $controller;
26+
27+
/**
28+
* @var Context|\PHPUnit_Framework_MockObject_MockObject
29+
*/
30+
private $contextMock;
31+
32+
/**
33+
* @var RawFactory|\PHPUnit_Framework_MockObject_MockObject
34+
*/
35+
private $rawFactoryMock;
36+
37+
/**
38+
* @var LayoutFactory|\PHPUnit_Framework_MockObject_MockObject
39+
*/
40+
private $layoutFactoryMock;
41+
42+
/**
43+
* @var RequestInterface|\PHPUnit_Framework_MockObject_MockObject
44+
*/
45+
private $requestInterfaceMock;
46+
47+
/**
48+
* @var Http|\PHPUnit_Framework_MockObject_MockObject
49+
*/
50+
private $requestMock;
51+
52+
/**
53+
* @inheritdoc
54+
*/
55+
protected function setUp()
56+
{
57+
$objectManagerHelper = new ObjectManagerHelper($this);
58+
59+
$this->contextMock = $this->getMock(\Magento\Backend\App\Action\Context::class, [], [], '', false);
60+
$this->rawFactoryMock = $this->getMock(\Magento\Framework\Controller\Result\RawFactory::class);
61+
$this->layoutFactoryMock = $this->getMock(\Magento\Framework\View\LayoutFactory::class, [], [], '', false);
62+
$this->requestMock = $this->getMock(\Magento\Framework\App\Request\Http::class, [], [], '', false);
63+
$this->requestInterfaceMock = $this->getMockForAbstractClass(
64+
\Magento\Framework\App\RequestInterface::class,
65+
[],
66+
'',
67+
false,
68+
true,
69+
true,
70+
['isPost']
71+
);
72+
$this->contextMock->expects($this->once())->method('getRequest')->willReturn($this->requestMock);
73+
74+
$this->controller = $objectManagerHelper->getObject(
75+
\Magento\Catalog\Controller\Adminhtml\Product\Widget\Chooser::class,
76+
[
77+
'context' => $this->contextMock,
78+
'resultRawFactory' => $this->rawFactoryMock,
79+
'layoutFactory' => $this->layoutFactoryMock,
80+
]
81+
);
82+
}
83+
84+
/**
85+
* Check that error throws when request is not a POST.
86+
*
87+
* @return void
88+
* @expectedException \Magento\Framework\Exception\NotFoundException
89+
* @expectedExceptionMessage Page not found.
90+
*/
91+
public function testExecuteWithNonPostRequest()
92+
{
93+
$this->requestMock->expects($this->once())->method('isPost')->willReturn(false);
94+
95+
$this->controller->execute();
96+
}
97+
}

0 commit comments

Comments
 (0)