Skip to content

Commit 4211a39

Browse files
ENGCOM-8250: ISSUE-27397 - Fix phrase handling in widget validation #28379
- Merge Pull Request #28379 from zaximus84/magento2:ISSUE-27397-widget-validation-phrase-handling - Merged commits: 1. f20b05b 2. 1e4ef87 3. 3d82fe3 4. 0f1c459 5. d0e8bda
2 parents 5977749 + d0e8bda commit 4211a39

File tree

2 files changed

+164
-4
lines changed

2 files changed

+164
-4
lines changed

app/code/Magento/Widget/Controller/Adminhtml/Widget/Instance/Validate.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66
*/
77
namespace Magento\Widget\Controller\Adminhtml\Widget\Instance;
88

9-
class Validate extends \Magento\Widget\Controller\Adminhtml\Widget\Instance
9+
use Magento\Framework\App\Action\HttpPostActionInterface;
10+
use Magento\Framework\DataObject;
11+
use Magento\Framework\Phrase;
12+
use Magento\Widget\Controller\Adminhtml\Widget\Instance;
13+
14+
class Validate extends Instance implements HttpPostActionInterface
1015
{
1116
/**
1217
* Validate action
@@ -15,12 +20,12 @@ class Validate extends \Magento\Widget\Controller\Adminhtml\Widget\Instance
1520
*/
1621
public function execute()
1722
{
18-
$response = new \Magento\Framework\DataObject();
23+
$response = new DataObject();
1924
$response->setError(false);
2025
$widgetInstance = $this->_initWidgetInstance();
2126
$result = $widgetInstance->validate();
22-
if ($result !== true && is_string($result)) {
23-
$this->messageManager->addError($result);
27+
if ($result !== true && (is_string($result) || $result instanceof Phrase)) {
28+
$this->messageManager->addErrorMessage((string) $result);
2429
$this->_view->getLayout()->initMessages();
2530
$response->setError(true);
2631
$response->setHtmlMessage($this->_view->getLayout()->getMessagesBlock()->getGroupedHtml());
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Widget\Test\Unit\Controller\Adminhtml\Widget\Instance;
9+
10+
use Magento\Backend\App\Action\Context;
11+
use Magento\Framework\App\RequestInterface;
12+
use Magento\Framework\App\ResponseInterface;
13+
use Magento\Framework\App\ViewInterface;
14+
use Magento\Framework\Message\ManagerInterface;
15+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
16+
use Magento\Framework\View\Element\Messages;
17+
use Magento\Framework\View\Layout;
18+
use Magento\Framework\View\LayoutInterface;
19+
use Magento\Widget\Controller\Adminhtml\Widget\Instance\Validate;
20+
use Magento\Widget\Model\Widget\Instance;
21+
use Magento\Widget\Model\Widget\InstanceFactory;
22+
use PHPUnit\Framework\MockObject\MockObject;
23+
use PHPUnit\Framework\TestCase;
24+
25+
/**
26+
* Test for \Magento\Widget\Controller\Adminhtml\Widget\Instance\Validate.
27+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
28+
*/
29+
class ValidateTest extends TestCase
30+
{
31+
private $errorMessage = 'We cannot create the widget instance because it is missing required information.';
32+
33+
/**
34+
* @var Validate
35+
*/
36+
private $model;
37+
38+
/**
39+
* @var Layout|MockObject
40+
*/
41+
private $layout;
42+
43+
/**
44+
* @var ManagerInterface|MockObject
45+
*/
46+
private $messageManagerMock;
47+
48+
/**
49+
* @var MockObject
50+
*/
51+
private $responseMock;
52+
53+
/**
54+
* @var MockObject
55+
*/
56+
private $widgetMock;
57+
58+
/**
59+
* @var Messages|MockObject
60+
*/
61+
private $messagesBlock;
62+
63+
/**
64+
* @inheritDoc
65+
*/
66+
protected function setUp(): void
67+
{
68+
$objectManager = new ObjectManager($this);
69+
70+
$request = $this->getMockForAbstractClass(RequestInterface::class);
71+
$this->messageManagerMock = $this->createMock(ManagerInterface::class);
72+
$viewMock = $this->createMock(ViewInterface::class);
73+
$layoutMock = $this->getMockBuilder(LayoutInterface::class)
74+
->disableOriginalConstructor()
75+
->addMethods(['initMessages'])
76+
->getMockForAbstractClass();
77+
$this->messagesBlock = $this->createMock(Messages::class);
78+
$layoutMock->method('getMessagesBlock')->willReturn($this->messagesBlock);
79+
$viewMock->method('getLayout')->willReturn($layoutMock);
80+
$this->responseMock = $this->getMockBuilder(ResponseInterface::class)
81+
->disableOriginalConstructor()
82+
->addMethods(['representJson'])
83+
->getMockForAbstractClass();
84+
85+
$context = $this->createMock(Context::class);
86+
$context->method('getRequest')->willReturn($request);
87+
$context->method('getMessageManager')->willReturn($this->messageManagerMock);
88+
$context->method('getView')->willReturn($viewMock);
89+
$context->method('getResponse')->willReturn($this->responseMock);
90+
91+
$this->widgetMock = $this->getMockBuilder(Instance::class)
92+
->disableOriginalConstructor()
93+
->onlyMethods(['setType', 'setCode', 'getType'])
94+
->addMethods(['setThemeId', 'getThemeId'])
95+
->getMock();
96+
$this->widgetMock->method('setType')->willReturnSelf();
97+
$this->widgetMock->method('setCode')->willReturnSelf();
98+
$this->widgetMock->method('setThemeId')->willReturnSelf();
99+
$widgetFactoryMock = $this->createMock(InstanceFactory::class);
100+
$widgetFactoryMock->method('create')->willReturn($this->widgetMock);
101+
102+
$this->model = $objectManager->getObject(
103+
Validate::class,
104+
[
105+
'widgetFactory' => $widgetFactoryMock,
106+
'context' => $context,
107+
'layout' => $this->layout
108+
]
109+
);
110+
}
111+
112+
/**
113+
* Test execute
114+
*
115+
* @return void
116+
*/
117+
public function testExecute(): void
118+
{
119+
$this->widgetMock->expects($this->once())
120+
->method('getThemeId')
121+
->willReturn(777);
122+
$this->widgetMock->expects($this->once())
123+
->method('getType')
124+
->willReturn('some type');
125+
126+
$this->messageManagerMock->expects($this->never())
127+
->method('addErrorMessage')
128+
->with($this->errorMessage);
129+
$this->responseMock->expects($this->once())
130+
->method('representJson')
131+
->with(json_encode(['error' => false]));
132+
133+
$this->model->execute();
134+
}
135+
136+
/**
137+
* Test execute with Phrase object
138+
*
139+
* @return void
140+
*/
141+
public function testExecutePhraseObject(): void
142+
{
143+
$this->messageManagerMock->expects($this->once())
144+
->method('addErrorMessage')
145+
->with($this->errorMessage);
146+
$this->messagesBlock->expects($this->once())
147+
->method('getGroupedHtml')
148+
->willReturn($this->errorMessage);
149+
$this->responseMock->expects($this->once())
150+
->method('representJson')
151+
->with(json_encode(['error' => true, 'html_message' => $this->errorMessage]));
152+
153+
$this->model->execute();
154+
}
155+
}

0 commit comments

Comments
 (0)