123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Ui\Test\Unit\Controller\Adminhtml\Index;
- use Magento\Ui\Controller\Adminhtml\Index\Render;
- use Magento\Ui\Model\UiComponentTypeResolver;
- use Magento\Framework\View\Element\UiComponent\ContextInterface;
- use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- * @SuppressWarnings(PHPMD.TooManyFields)
- */
- class RenderTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var Render
- */
- private $render;
- /**
- * @var ObjectManagerHelper
- */
- private $objectManagerHelper;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- private $requestMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- private $responseMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- private $uiFactoryMock;
- /**
- * @var \Magento\Backend\App\Action\Context|\PHPUnit_Framework_MockObject_MockObject
- */
- private $contextMock;
- /**
- * @var \Magento\Framework\AuthorizationInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $authorizationMock;
- /**
- * @var \Magento\Backend\Model\Session|\PHPUnit_Framework_MockObject_MockObject
- */
- private $sessionMock;
- /**
- * @var \Magento\Framework\App\ActionFlag|\PHPUnit_Framework_MockObject_MockObject
- */
- private $actionFlagMock;
- /**
- * @var \Magento\Backend\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
- */
- private $helperMock;
- /**
- * @var ContextInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $uiComponentContextMock;
- /**
- * @var \Magento\Framework\View\Element\UiComponent\DataProvider\DataProviderInterface|
- * \PHPUnit_Framework_MockObject_MockObject
- */
- private $dataProviderMock;
- /**
- * @var \Magento\Framework\View\Element\UiComponentInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $uiComponentMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject|UiComponentTypeResolver
- */
- private $uiComponentTypeResolverMock;
- /**
- * @var \Magento\Framework\Controller\Result\JsonFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- private $resultJsonFactoryMock;
- /**
- * @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $loggerMock;
- protected function setUp()
- {
- $this->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 = '<html>data</html>';
- $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 = '<html>data</html>';
- $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 = '<html>data</html>';
- $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
- ],
- ];
- }
- }
|