RenderTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Ui\Test\Unit\Controller\Adminhtml\Index;
  7. use Magento\Ui\Controller\Adminhtml\Index\Render;
  8. use Magento\Ui\Model\UiComponentTypeResolver;
  9. use Magento\Framework\View\Element\UiComponent\ContextInterface;
  10. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  11. /**
  12. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  13. * @SuppressWarnings(PHPMD.TooManyFields)
  14. */
  15. class RenderTest extends \PHPUnit\Framework\TestCase
  16. {
  17. /**
  18. * @var Render
  19. */
  20. private $render;
  21. /**
  22. * @var ObjectManagerHelper
  23. */
  24. private $objectManagerHelper;
  25. /**
  26. * @var \PHPUnit_Framework_MockObject_MockObject
  27. */
  28. private $requestMock;
  29. /**
  30. * @var \PHPUnit_Framework_MockObject_MockObject
  31. */
  32. private $responseMock;
  33. /**
  34. * @var \PHPUnit_Framework_MockObject_MockObject
  35. */
  36. private $uiFactoryMock;
  37. /**
  38. * @var \Magento\Backend\App\Action\Context|\PHPUnit_Framework_MockObject_MockObject
  39. */
  40. private $contextMock;
  41. /**
  42. * @var \Magento\Framework\AuthorizationInterface|\PHPUnit_Framework_MockObject_MockObject
  43. */
  44. private $authorizationMock;
  45. /**
  46. * @var \Magento\Backend\Model\Session|\PHPUnit_Framework_MockObject_MockObject
  47. */
  48. private $sessionMock;
  49. /**
  50. * @var \Magento\Framework\App\ActionFlag|\PHPUnit_Framework_MockObject_MockObject
  51. */
  52. private $actionFlagMock;
  53. /**
  54. * @var \Magento\Backend\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
  55. */
  56. private $helperMock;
  57. /**
  58. * @var ContextInterface|\PHPUnit_Framework_MockObject_MockObject
  59. */
  60. private $uiComponentContextMock;
  61. /**
  62. * @var \Magento\Framework\View\Element\UiComponent\DataProvider\DataProviderInterface|
  63. * \PHPUnit_Framework_MockObject_MockObject
  64. */
  65. private $dataProviderMock;
  66. /**
  67. * @var \Magento\Framework\View\Element\UiComponentInterface|\PHPUnit_Framework_MockObject_MockObject
  68. */
  69. private $uiComponentMock;
  70. /**
  71. * @var \PHPUnit_Framework_MockObject_MockObject|UiComponentTypeResolver
  72. */
  73. private $uiComponentTypeResolverMock;
  74. /**
  75. * @var \Magento\Framework\Controller\Result\JsonFactory|\PHPUnit_Framework_MockObject_MockObject
  76. */
  77. private $resultJsonFactoryMock;
  78. /**
  79. * @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
  80. */
  81. private $loggerMock;
  82. protected function setUp()
  83. {
  84. $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
  85. ->disableOriginalConstructor()
  86. ->getMock();
  87. $this->responseMock = $this->getMockBuilder(\Magento\Framework\App\Response\Http::class)
  88. ->disableOriginalConstructor()
  89. ->getMock();
  90. $this->contextMock = $this->getMockBuilder(\Magento\Backend\App\Action\Context::class)
  91. ->disableOriginalConstructor()
  92. ->getMock();
  93. $this->uiFactoryMock = $this->getMockBuilder(\Magento\Framework\View\Element\UiComponentFactory::class)
  94. ->disableOriginalConstructor()
  95. ->getMock();
  96. $this->authorizationMock = $this->getMockBuilder(\Magento\Framework\AuthorizationInterface::class)
  97. ->getMockForAbstractClass();
  98. $this->sessionMock = $this->getMockBuilder(\Magento\Backend\Model\Session::class)
  99. ->disableOriginalConstructor()
  100. ->getMock();
  101. $this->actionFlagMock = $this->getMockBuilder(\Magento\Framework\App\ActionFlag::class)
  102. ->disableOriginalConstructor()
  103. ->getMock();
  104. $this->helperMock = $this->getMockBuilder(\Magento\Backend\Helper\Data::class)
  105. ->disableOriginalConstructor()
  106. ->getMock();
  107. $this->uiComponentContextMock = $this->getMockForAbstractClass(
  108. ContextInterface::class
  109. );
  110. $this->dataProviderMock = $this->getMockForAbstractClass(
  111. \Magento\Framework\View\Element\UiComponent\DataProvider\DataProviderInterface::class
  112. );
  113. $this->uiComponentMock = $this->getMockForAbstractClass(
  114. \Magento\Framework\View\Element\UiComponentInterface::class,
  115. [],
  116. '',
  117. false,
  118. true,
  119. true,
  120. ['render']
  121. );
  122. $this->resultJsonFactoryMock = $this->getMockBuilder(
  123. \Magento\Framework\Controller\Result\JsonFactory::class
  124. )
  125. ->disableOriginalConstructor()
  126. ->getMock();
  127. $this->loggerMock = $this->getMockForAbstractClass(\Psr\Log\LoggerInterface::class);
  128. $this->contextMock->expects($this->any())
  129. ->method('getRequest')
  130. ->willReturn($this->requestMock);
  131. $this->contextMock->expects($this->any())
  132. ->method('getResponse')
  133. ->willReturn($this->responseMock);
  134. $this->contextMock->expects($this->any())
  135. ->method('getAuthorization')
  136. ->willReturn($this->authorizationMock);
  137. $this->contextMock->expects($this->any())
  138. ->method('getSession')
  139. ->willReturn($this->sessionMock);
  140. $this->contextMock->expects($this->any())
  141. ->method('getActionFlag')
  142. ->willReturn($this->actionFlagMock);
  143. $this->contextMock->expects($this->any())
  144. ->method('getHelper')
  145. ->willReturn($this->helperMock);
  146. $this->uiComponentContextMock->expects($this->once())
  147. ->method('getDataProvider')
  148. ->willReturn($this->dataProviderMock);
  149. $this->uiComponentTypeResolverMock = $this->getMockBuilder(UiComponentTypeResolver::class)
  150. ->disableOriginalConstructor()
  151. ->getMock();
  152. $this->objectManagerHelper = new ObjectManagerHelper($this);
  153. $this->render = $this->objectManagerHelper->getObject(
  154. \Magento\Ui\Controller\Adminhtml\Index\Render::class,
  155. [
  156. 'context' => $this->contextMock,
  157. 'factory' => $this->uiFactoryMock,
  158. 'contentTypeResolver' => $this->uiComponentTypeResolverMock,
  159. 'resultJsonFactory' => $this->resultJsonFactoryMock,
  160. 'logger' => $this->loggerMock,
  161. ]
  162. );
  163. }
  164. public function testExecuteAjaxRequestException()
  165. {
  166. $name = 'test-name';
  167. $renderedData = '<html>data</html>';
  168. $this->requestMock->expects($this->any())
  169. ->method('getParam')
  170. ->with('namespace')
  171. ->willReturn($name);
  172. $this->requestMock->expects($this->any())
  173. ->method('getParams')
  174. ->willReturn([]);
  175. $this->responseMock->expects($this->once())
  176. ->method('appendBody')
  177. ->willThrowException(new \Exception('exception'));
  178. $jsonResultMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Json::class)
  179. ->disableOriginalConstructor()
  180. ->setMethods(['setData'])
  181. ->getMock();
  182. $this->resultJsonFactoryMock->expects($this->once())
  183. ->method('create')
  184. ->willReturn($jsonResultMock);
  185. $jsonResultMock->expects($this->once())
  186. ->method('setData')
  187. ->willReturnSelf();
  188. $this->loggerMock->expects($this->once())
  189. ->method('critical')
  190. ->willReturnSelf();
  191. $this->dataProviderMock->expects($this->once())
  192. ->method('getConfigData')
  193. ->willReturn([]);
  194. $this->uiComponentMock->expects($this->once())
  195. ->method('render')
  196. ->willReturn($renderedData);
  197. $this->uiComponentMock->expects($this->once())
  198. ->method('getChildComponents')
  199. ->willReturn([]);
  200. $this->uiComponentMock->expects($this->once())
  201. ->method('getContext')
  202. ->willReturn($this->uiComponentContextMock);
  203. $this->uiFactoryMock->expects($this->once())
  204. ->method('create')
  205. ->willReturn($this->uiComponentMock);
  206. $this->render->executeAjaxRequest();
  207. }
  208. public function testExecuteAjaxRequest()
  209. {
  210. $name = 'test-name';
  211. $renderedData = '<html>data</html>';
  212. $this->requestMock->expects($this->any())
  213. ->method('getParam')
  214. ->with('namespace')
  215. ->willReturn($name);
  216. $this->requestMock->expects($this->any())
  217. ->method('getParams')
  218. ->willReturn([]);
  219. $this->responseMock->expects($this->once())
  220. ->method('appendBody')
  221. ->with($renderedData);
  222. $this->dataProviderMock->expects($this->once())
  223. ->method('getConfigData')
  224. ->willReturn([]);
  225. $this->uiComponentMock->expects($this->once())
  226. ->method('render')
  227. ->willReturn($renderedData);
  228. $this->uiComponentMock->expects($this->once())
  229. ->method('getChildComponents')
  230. ->willReturn([]);
  231. $this->uiComponentMock->expects($this->any())
  232. ->method('getContext')
  233. ->willReturn($this->uiComponentContextMock);
  234. $this->uiFactoryMock->expects($this->once())
  235. ->method('create')
  236. ->willReturn($this->uiComponentMock);
  237. $this->uiComponentTypeResolverMock->expects($this->once())
  238. ->method('resolve')
  239. ->with($this->uiComponentContextMock)
  240. ->willReturn('application/json');
  241. $this->responseMock->expects($this->once())->method('setHeader')
  242. ->with('Content-Type', 'application/json', true);
  243. $this->render->executeAjaxRequest();
  244. }
  245. /**
  246. * @param array $dataProviderConfig
  247. * @param bool|null $isAllowed
  248. * @param int $authCallCount
  249. * @dataProvider executeAjaxRequestWithoutPermissionsDataProvider
  250. */
  251. public function testExecuteAjaxRequestWithoutPermissions(array $dataProviderConfig, $isAllowed, $authCallCount = 1)
  252. {
  253. $name = 'test-name';
  254. $renderedData = '<html>data</html>';
  255. $this->requestMock->expects($this->any())
  256. ->method('getParam')
  257. ->with('namespace')
  258. ->willReturn($name);
  259. $this->requestMock->expects($this->any())
  260. ->method('getParams')
  261. ->willReturn([]);
  262. if ($isAllowed === false) {
  263. $this->requestMock->expects($this->once())
  264. ->method('isAjax')
  265. ->willReturn(true);
  266. }
  267. $this->responseMock->expects($this->never())
  268. ->method('setRedirect');
  269. $this->responseMock->expects($this->any())
  270. ->method('appendBody')
  271. ->with($renderedData);
  272. $this->dataProviderMock->expects($this->once())
  273. ->method('getConfigData')
  274. ->willReturn($dataProviderConfig);
  275. $this->authorizationMock->expects($this->exactly($authCallCount))
  276. ->method('isAllowed')
  277. ->with(isset($dataProviderConfig['aclResource']) ? $dataProviderConfig['aclResource'] : null)
  278. ->willReturn($isAllowed);
  279. $this->uiComponentMock->expects($this->any())
  280. ->method('render')
  281. ->willReturn($renderedData);
  282. $this->uiComponentMock->expects($this->any())
  283. ->method('getChildComponents')
  284. ->willReturn([]);
  285. $this->uiComponentMock->expects($this->any())
  286. ->method('getContext')
  287. ->willReturn($this->uiComponentContextMock);
  288. $this->uiFactoryMock->expects($this->once())
  289. ->method('create')
  290. ->willReturn($this->uiComponentMock);
  291. $this->render->executeAjaxRequest();
  292. }
  293. /**
  294. * @return array
  295. */
  296. public function executeAjaxRequestWithoutPermissionsDataProvider()
  297. {
  298. $aclResource = 'Magento_Test::index_index';
  299. return [
  300. [
  301. 'dataProviderConfig' => ['aclResource' => $aclResource],
  302. 'isAllowed' => true
  303. ],
  304. [
  305. 'dataProviderConfig' => ['aclResource' => $aclResource],
  306. 'isAllowed' => false
  307. ],
  308. [
  309. 'dataProviderConfig' => [],
  310. 'isAllowed' => null,
  311. 'authCallCount' => 0
  312. ],
  313. ];
  314. }
  315. }