requestMock = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class) ->disableOriginalConstructor() ->getMock(); $this->responseMock = $this->getMockBuilder(\Magento\Framework\App\Response\Http::class) ->disableOriginalConstructor() ->getMock(); $this->contextMock = $this->getMockBuilder(\Magento\Backend\App\Action\Context::class) ->disableOriginalConstructor() ->getMock(); $this->uiFactoryMock = $this->getMockBuilder(\Magento\Framework\View\Element\UiComponentFactory::class) ->disableOriginalConstructor() ->getMock(); $this->authorizationMock = $this->getMockBuilder(\Magento\Framework\AuthorizationInterface::class) ->getMockForAbstractClass(); $this->sessionMock = $this->getMockBuilder(\Magento\Backend\Model\Session::class) ->disableOriginalConstructor() ->getMock(); $this->actionFlagMock = $this->getMockBuilder(\Magento\Framework\App\ActionFlag::class) ->disableOriginalConstructor() ->getMock(); $this->helperMock = $this->getMockBuilder(\Magento\Backend\Helper\Data::class) ->disableOriginalConstructor() ->getMock(); $this->uiComponentContextMock = $this->getMockForAbstractClass( ContextInterface::class ); $this->dataProviderMock = $this->getMockForAbstractClass( \Magento\Framework\View\Element\UiComponent\DataProvider\DataProviderInterface::class ); $this->uiComponentMock = $this->getMockForAbstractClass( \Magento\Framework\View\Element\UiComponentInterface::class, [], '', false, true, true, ['render'] ); $this->resultJsonFactoryMock = $this->getMockBuilder( \Magento\Framework\Controller\Result\JsonFactory::class ) ->disableOriginalConstructor() ->getMock(); $this->loggerMock = $this->getMockForAbstractClass(\Psr\Log\LoggerInterface::class); $this->contextMock->expects($this->any()) ->method('getRequest') ->willReturn($this->requestMock); $this->contextMock->expects($this->any()) ->method('getResponse') ->willReturn($this->responseMock); $this->contextMock->expects($this->any()) ->method('getAuthorization') ->willReturn($this->authorizationMock); $this->contextMock->expects($this->any()) ->method('getSession') ->willReturn($this->sessionMock); $this->contextMock->expects($this->any()) ->method('getActionFlag') ->willReturn($this->actionFlagMock); $this->contextMock->expects($this->any()) ->method('getHelper') ->willReturn($this->helperMock); $this->uiComponentContextMock->expects($this->once()) ->method('getDataProvider') ->willReturn($this->dataProviderMock); $this->uiComponentTypeResolverMock = $this->getMockBuilder(UiComponentTypeResolver::class) ->disableOriginalConstructor() ->getMock(); $this->objectManagerHelper = new ObjectManagerHelper($this); $this->render = $this->objectManagerHelper->getObject( \Magento\Ui\Controller\Adminhtml\Index\Render::class, [ 'context' => $this->contextMock, 'factory' => $this->uiFactoryMock, 'contentTypeResolver' => $this->uiComponentTypeResolverMock, 'resultJsonFactory' => $this->resultJsonFactoryMock, 'logger' => $this->loggerMock, ] ); } public function testExecuteAjaxRequestException() { $name = 'test-name'; $renderedData = 'data'; $this->requestMock->expects($this->any()) ->method('getParam') ->with('namespace') ->willReturn($name); $this->requestMock->expects($this->any()) ->method('getParams') ->willReturn([]); $this->responseMock->expects($this->once()) ->method('appendBody') ->willThrowException(new \Exception('exception')); $jsonResultMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Json::class) ->disableOriginalConstructor() ->setMethods(['setData']) ->getMock(); $this->resultJsonFactoryMock->expects($this->once()) ->method('create') ->willReturn($jsonResultMock); $jsonResultMock->expects($this->once()) ->method('setData') ->willReturnSelf(); $this->loggerMock->expects($this->once()) ->method('critical') ->willReturnSelf(); $this->dataProviderMock->expects($this->once()) ->method('getConfigData') ->willReturn([]); $this->uiComponentMock->expects($this->once()) ->method('render') ->willReturn($renderedData); $this->uiComponentMock->expects($this->once()) ->method('getChildComponents') ->willReturn([]); $this->uiComponentMock->expects($this->once()) ->method('getContext') ->willReturn($this->uiComponentContextMock); $this->uiFactoryMock->expects($this->once()) ->method('create') ->willReturn($this->uiComponentMock); $this->render->executeAjaxRequest(); } public function testExecuteAjaxRequest() { $name = 'test-name'; $renderedData = 'data'; $this->requestMock->expects($this->any()) ->method('getParam') ->with('namespace') ->willReturn($name); $this->requestMock->expects($this->any()) ->method('getParams') ->willReturn([]); $this->responseMock->expects($this->once()) ->method('appendBody') ->with($renderedData); $this->dataProviderMock->expects($this->once()) ->method('getConfigData') ->willReturn([]); $this->uiComponentMock->expects($this->once()) ->method('render') ->willReturn($renderedData); $this->uiComponentMock->expects($this->once()) ->method('getChildComponents') ->willReturn([]); $this->uiComponentMock->expects($this->any()) ->method('getContext') ->willReturn($this->uiComponentContextMock); $this->uiFactoryMock->expects($this->once()) ->method('create') ->willReturn($this->uiComponentMock); $this->uiComponentTypeResolverMock->expects($this->once()) ->method('resolve') ->with($this->uiComponentContextMock) ->willReturn('application/json'); $this->responseMock->expects($this->once())->method('setHeader') ->with('Content-Type', 'application/json', true); $this->render->executeAjaxRequest(); } /** * @param array $dataProviderConfig * @param bool|null $isAllowed * @param int $authCallCount * @dataProvider executeAjaxRequestWithoutPermissionsDataProvider */ public function testExecuteAjaxRequestWithoutPermissions(array $dataProviderConfig, $isAllowed, $authCallCount = 1) { $name = 'test-name'; $renderedData = 'data'; $this->requestMock->expects($this->any()) ->method('getParam') ->with('namespace') ->willReturn($name); $this->requestMock->expects($this->any()) ->method('getParams') ->willReturn([]); if ($isAllowed === false) { $this->requestMock->expects($this->once()) ->method('isAjax') ->willReturn(true); } $this->responseMock->expects($this->never()) ->method('setRedirect'); $this->responseMock->expects($this->any()) ->method('appendBody') ->with($renderedData); $this->dataProviderMock->expects($this->once()) ->method('getConfigData') ->willReturn($dataProviderConfig); $this->authorizationMock->expects($this->exactly($authCallCount)) ->method('isAllowed') ->with(isset($dataProviderConfig['aclResource']) ? $dataProviderConfig['aclResource'] : null) ->willReturn($isAllowed); $this->uiComponentMock->expects($this->any()) ->method('render') ->willReturn($renderedData); $this->uiComponentMock->expects($this->any()) ->method('getChildComponents') ->willReturn([]); $this->uiComponentMock->expects($this->any()) ->method('getContext') ->willReturn($this->uiComponentContextMock); $this->uiFactoryMock->expects($this->once()) ->method('create') ->willReturn($this->uiComponentMock); $this->render->executeAjaxRequest(); } /** * @return array */ public function executeAjaxRequestWithoutPermissionsDataProvider() { $aclResource = 'Magento_Test::index_index'; return [ [ 'dataProviderConfig' => ['aclResource' => $aclResource], 'isAllowed' => true ], [ 'dataProviderConfig' => ['aclResource' => $aclResource], 'isAllowed' => false ], [ 'dataProviderConfig' => [], 'isAllowed' => null, 'authCallCount' => 0 ], ]; } }