33 * Copyright © Magento, Inc. All rights reserved.
44 * See COPYING.txt for license details.
55 */
6+
67namespace 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 ;
813use Magento \Ui \Controller \Adminhtml \Index \Render ;
914use 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 ' )
0 commit comments