Skip to content

Commit de12b55

Browse files
authored
ENGCOM-3725: MUI controller lacks JSON response, instead returns status 200 with empty body #19859
2 parents 18a20ae + b2b9063 commit de12b55

16 files changed

+281
-13
lines changed

app/code/Magento/Ui/Controller/Adminhtml/Index/Render.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,18 @@ public function execute()
8686

8787
$contentType = $this->contentTypeResolver->resolve($component->getContext());
8888
$this->getResponse()->setHeader('Content-Type', $contentType, true);
89+
} else {
90+
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
91+
$resultJson = $this->resultJsonFactory->create();
92+
$resultJson->setStatusHeader(
93+
\Zend\Http\Response::STATUS_CODE_403,
94+
\Zend\Http\AbstractMessage::VERSION_11,
95+
'Forbidden'
96+
);
97+
return $resultJson->setData([
98+
'error' => $this->escaper->escapeHtml('Forbidden'),
99+
'errorcode' => 403
100+
]);
89101
}
90102
} catch (\Magento\Framework\Exception\LocalizedException $e) {
91103
$this->logger->critical($e);

app/code/Magento/Ui/Test/Unit/Controller/Adminhtml/Index/RenderTest.php

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Ui\Test\Unit\Controller\Adminhtml\Index;
78

9+
use Magento\Framework\Controller\Result\Json;
10+
use Magento\Framework\Escaper;
11+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
12+
use Magento\Framework\View\Element\UiComponent\ContextInterface;
813
use Magento\Ui\Controller\Adminhtml\Index\Render;
914
use Magento\Ui\Model\UiComponentTypeResolver;
10-
use Magento\Framework\View\Element\UiComponent\ContextInterface;
11-
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
15+
use Zend\Http\AbstractMessage;
16+
use Zend\Http\Response;
1217

1318
/**
1419
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -97,6 +102,11 @@ class RenderTest extends \PHPUnit\Framework\TestCase
97102
*/
98103
private $loggerMock;
99104

105+
/**
106+
* @var Escaper|\PHPUnit_Framework_MockObject_MockObject
107+
*/
108+
private $escaperMock;
109+
100110
protected function setUp()
101111
{
102112
$this->requestMock = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
@@ -170,6 +180,10 @@ protected function setUp()
170180
$this->uiComponentTypeResolverMock = $this->getMockBuilder(UiComponentTypeResolver::class)
171181
->disableOriginalConstructor()
172182
->getMock();
183+
$this->escaperMock = $this->createMock(Escaper::class);
184+
$this->escaperMock->expects($this->any())
185+
->method('escapeHtml')
186+
->willReturnArgument(0);
173187

174188
$this->objectManagerHelper = new ObjectManagerHelper($this);
175189

@@ -181,6 +195,7 @@ protected function setUp()
181195
'contentTypeResolver' => $this->uiComponentTypeResolverMock,
182196
'resultJsonFactory' => $this->resultJsonFactoryMock,
183197
'logger' => $this->loggerMock,
198+
'escaper' => $this->escaperMock,
184199
]
185200
);
186201
}
@@ -201,7 +216,7 @@ public function testExecuteAjaxRequestException()
201216
->method('appendBody')
202217
->willThrowException(new \Exception('exception'));
203218

204-
$jsonResultMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Json::class)
219+
$jsonResultMock = $this->getMockBuilder(Json::class)
205220
->disableOriginalConstructor()
206221
->setMethods(['setData'])
207222
->getMock();
@@ -290,6 +305,34 @@ public function testExecuteAjaxRequestWithoutPermissions(array $dataProviderConf
290305
$name = 'test-name';
291306
$renderedData = '<html>data</html>';
292307

308+
if (false === $isAllowed) {
309+
$jsonResultMock = $this->getMockBuilder(Json::class)
310+
->disableOriginalConstructor()
311+
->setMethods(['setStatusHeader', 'setData'])
312+
->getMock();
313+
314+
$jsonResultMock->expects($this->at(0))
315+
->method('setStatusHeader')
316+
->with(
317+
Response::STATUS_CODE_403,
318+
AbstractMessage::VERSION_11,
319+
'Forbidden'
320+
)
321+
->willReturnSelf();
322+
323+
$jsonResultMock->expects($this->at(1))
324+
->method('setData')
325+
->with([
326+
'error' => 'Forbidden',
327+
'errorcode' => 403
328+
])
329+
->willReturnSelf();
330+
331+
$this->resultJsonFactoryMock->expects($this->any())
332+
->method('create')
333+
->willReturn($jsonResultMock);
334+
}
335+
293336
$this->requestMock->expects($this->any())
294337
->method('getParam')
295338
->with('namespace')

dev/tests/functional/tests/app/Magento/Backend/Test/Page/Adminhtml/Dashboard.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@
1717
<block name="accessDeniedBlock" class="Magento\Backend\Test\Block\Denied" locator="#anchor-content" strategy="css selector" />
1818
<block name="systemMessageDialog" class="Magento\AdminNotification\Test\Block\System\Messages" locator='.ui-popup-message .modal-inner-wrap' strategy="css selector" />
1919
<block name="applicationVersion" class="Magento\Backend\Test\Block\Version" locator="body" strategy="css selector" />
20+
<block name="modalMessage" class="Magento\Ui\Test\Block\Adminhtml\Modal" locator=".modal-popup>.modal-inner-wrap" strategy="css selector" />
2021
</page>
2122
</config>

dev/tests/functional/tests/app/Magento/User/Test/Constraint/AssertUserRoleRestrictedAccess.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Mtf\Client\BrowserInterface;
1111
use Magento\Mtf\Constraint\AbstractConstraint;
1212
use Magento\User\Test\Fixture\User;
13+
use Magento\User\Test\TestStep\LoginUserOnBackendWithErrorStep;
1314

1415
/**
1516
* Asserts that user has only related permissions.
@@ -18,6 +19,8 @@ class AssertUserRoleRestrictedAccess extends AbstractConstraint
1819
{
1920
const DENIED_ACCESS = 'Sorry, you need permissions to view this content.';
2021

22+
protected $loginStep = 'Magento\User\Test\TestStep\LoginUserOnBackendStep';
23+
2124
/**
2225
* Asserts that user has only related permissions.
2326
*
@@ -36,7 +39,7 @@ public function processAssert(
3639
$denyUrl
3740
) {
3841
$this->objectManager->create(
39-
\Magento\User\Test\TestStep\LoginUserOnBackendStep::class,
42+
$this->loginStep,
4043
['user' => $user]
4144
)->run();
4245

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\User\Test\Constraint;
10+
11+
/**
12+
* @inheritdoc
13+
*/
14+
class AssertUserRoleRestrictedAccessWithError extends AssertUserRoleRestrictedAccess
15+
{
16+
protected $loginStep = 'Magento\User\Test\TestStep\LoginUserOnBackendWithErrorStep';
17+
}

dev/tests/functional/tests/app/Magento/User/Test/Constraint/AssertUserSuccessLogin.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,20 @@
77
namespace Magento\User\Test\Constraint;
88

99
use Magento\Backend\Test\Page\Adminhtml\Dashboard;
10-
use Magento\User\Test\Fixture\User;
1110
use Magento\Mtf\Constraint\AbstractConstraint;
11+
use Magento\User\Test\Fixture\User;
12+
use Magento\User\Test\TestStep\LoginUserOnBackendStep;
1213

1314
/**
1415
* Verify whether customer has logged in to the Backend.
1516
*/
1617
class AssertUserSuccessLogin extends AbstractConstraint
1718
{
19+
/**
20+
* @var string
21+
*/
22+
protected $loginStep = LoginUserOnBackendStep::class;
23+
1824
/**
1925
* Verify whether customer has logged in to the Backend.
2026
*
@@ -25,7 +31,7 @@ class AssertUserSuccessLogin extends AbstractConstraint
2531
public function processAssert(User $user, Dashboard $dashboard)
2632
{
2733
$this->objectManager->create(
28-
\Magento\User\Test\TestStep\LoginUserOnBackendStep::class,
34+
$this->loginStep,
2935
['user' => $user]
3036
)->run();
3137
\PHPUnit\Framework\Assert::assertTrue(
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\User\Test\Constraint;
8+
9+
use Magento\User\Test\TestStep\LoginUserOnBackendWithErrorStep;
10+
11+
/**
12+
* Verify whether customer has logged in to the Backend with error alert.
13+
*/
14+
class AssertUserSuccessLoginWithError extends AssertUserSuccessLogin
15+
{
16+
/**
17+
* @var string
18+
*/
19+
protected $loginStep = LoginUserOnBackendWithErrorStep::class;
20+
}

dev/tests/functional/tests/app/Magento/User/Test/TestCase/UpdateAdminUserEntityTest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
<constraint name="Magento\User\Test\Constraint\AssertUserInGrid" />
3333
<constraint name="Magento\User\Test\Constraint\AssertUserSuccessLogOut" />
3434
<constraint name="Magento\User\Test\Constraint\AssertUserSuccessLogin" />
35-
<constraint name="Magento\User\Test\Constraint\AssertUserRoleRestrictedAccess" />
35+
<constraint name="Magento\User\Test\Constraint\AssertUserRoleRestrictedAccessWithError" />
3636
</variation>
3737
</testCase>
3838
</config>

dev/tests/functional/tests/app/Magento/User/Test/TestCase/UpdateAdminUserRoleEntityTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ public function testUpdateAdminUserRolesEntity(
121121
*/
122122
public function tearDown()
123123
{
124+
sleep(3);
125+
$modalMessage = $this->dashboard->getModalMessage();
126+
if ($modalMessage->isVisible()) {
127+
$modalMessage->acceptAlert();
128+
}
124129
$this->dashboard->getAdminPanelHeader()->logOut();
125130
}
126131
}

dev/tests/functional/tests/app/Magento/User/Test/TestCase/UpdateAdminUserRoleEntityTest.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
<constraint name="Magento\User\Test\Constraint\AssertRoleSuccessSaveMessage" />
3030
<constraint name="Magento\User\Test\Constraint\AssertRoleInGrid" />
3131
<constraint name="Magento\User\Test\Constraint\AssertUserSuccessLogOut" />
32-
<constraint name="Magento\User\Test\Constraint\AssertUserSuccessLogin" />
33-
<constraint name="Magento\User\Test\Constraint\AssertUserRoleRestrictedAccess" />
32+
<constraint name="Magento\User\Test\Constraint\AssertUserSuccessLoginWithError"/>
33+
<constraint name="Magento\User\Test\Constraint\AssertUserRoleRestrictedAccessWithError" />
3434
</variation>
3535
<variation name="UpdateAdminUserRoleEntityTestVariation3">
3636
<data name="user/dataset" xsi:type="string">custom_admin_with_default_role</data>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\User\Test\TestStep;
8+
9+
use Magento\Backend\Test\Page\Adminhtml\Dashboard;
10+
use Magento\Mtf\Client\BrowserInterface;
11+
use Magento\Mtf\TestStep\TestStepInterface;
12+
13+
/**
14+
* Close access error modal message.
15+
*/
16+
class CloseErrorAlertStep implements TestStepInterface
17+
{
18+
/**
19+
* @var Dashboard
20+
*/
21+
private $dashboard;
22+
23+
/**
24+
* @var BrowserInterface
25+
*/
26+
private $browser;
27+
28+
/**
29+
* @param Dashboard $dashboard
30+
* @param BrowserInterface $browser
31+
*/
32+
public function __construct(
33+
Dashboard $dashboard,
34+
BrowserInterface $browser
35+
) {
36+
$this->dashboard = $dashboard;
37+
$this->browser = $browser;
38+
}
39+
40+
/**
41+
* @inheritdoc
42+
*/
43+
public function run()
44+
{
45+
$modalMessage = $this->dashboard->getModalMessage();
46+
try {
47+
$this->browser->waitUntil(
48+
function () use ($modalMessage) {
49+
return $modalMessage->isVisible() ? true : null;
50+
}
51+
);
52+
$modalMessage->acceptAlert();
53+
} catch (\PHPUnit_Extensions_Selenium2TestCase_WebDriverException $e) {
54+
//There is no modal to accept.
55+
}
56+
}
57+
}

dev/tests/functional/tests/app/Magento/User/Test/TestStep/LoginUserOnBackendStep.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class LoginUserOnBackendStep implements TestStepInterface
5050
*
5151
* @var BrowserInterface
5252
*/
53-
private $browser;
53+
protected $browser;
5454

5555
/**
5656
* Array of error messages on admin login form.
@@ -108,8 +108,6 @@ public function run()
108108
}
109109
}
110110
}
111-
112-
$this->dashboard->getSystemMessageDialog()->closePopup();
113111
}
114112

115113
/**
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\User\Test\TestStep;
8+
9+
use Magento\Backend\Test\Page\AdminAuthLogin;
10+
use Magento\Backend\Test\Page\Adminhtml\Dashboard;
11+
use Magento\Mtf\Client\BrowserInterface;
12+
use Magento\User\Test\Fixture\User;
13+
14+
/**
15+
* Login user on backend with access error.
16+
*/
17+
class LoginUserOnBackendWithErrorStep extends LoginUserOnBackendStep
18+
{
19+
/**
20+
* @var CloseErrorAlertStep
21+
*/
22+
private $closeErrorAlertStep;
23+
24+
/**
25+
* @param LogoutUserOnBackendStep $logoutUserOnBackendStep
26+
* @param AdminAuthLogin $adminAuth
27+
* @param User $user
28+
* @param Dashboard $dashboard
29+
* @param BrowserInterface $browser
30+
*/
31+
public function __construct(
32+
LogoutUserOnBackendStep $logoutUserOnBackendStep,
33+
AdminAuthLogin $adminAuth,
34+
User $user,
35+
Dashboard $dashboard,
36+
BrowserInterface $browser,
37+
CloseErrorAlertStep $closeErrorAlertStep
38+
) {
39+
parent::__construct($logoutUserOnBackendStep, $adminAuth, $user, $dashboard, $browser);
40+
$this->closeErrorAlertStep = $closeErrorAlertStep;
41+
}
42+
43+
/**
44+
* Run step flow.
45+
*
46+
* @return void
47+
*/
48+
public function run()
49+
{
50+
parent::run();
51+
$this->closeErrorAlertStep->run();
52+
}
53+
}

dev/tests/functional/tests/app/Magento/User/Test/TestStep/LogoutUserOnBackendStep.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ public function __construct(AdminAuthLogin $adminAuth, Dashboard $dashboard)
4848
public function run()
4949
{
5050
$this->adminAuth->open();
51-
$this->dashboard->getSystemMessageDialog()->closePopup();
5251
$this->dashboard->getAdminPanelHeader()->logOut();
5352
}
5453
}

0 commit comments

Comments
 (0)