Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RenderTest.php
Go to the documentation of this file.
1 <?php
7 
12 
17 class RenderTest extends \PHPUnit\Framework\TestCase
18 {
22  private $render;
23 
27  private $objectManagerHelper;
28 
32  private $requestMock;
33 
37  private $responseMock;
38 
42  private $uiFactoryMock;
43 
47  private $contextMock;
48 
52  private $authorizationMock;
53 
57  private $sessionMock;
58 
62  private $actionFlagMock;
63 
67  private $helperMock;
68 
72  private $uiComponentContextMock;
73 
78  private $dataProviderMock;
79 
83  private $uiComponentMock;
84 
88  private $uiComponentTypeResolverMock;
89 
93  private $resultJsonFactoryMock;
94 
98  private $loggerMock;
99 
100  protected function setUp()
101  {
102  $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
103  ->disableOriginalConstructor()
104  ->getMock();
105  $this->responseMock = $this->getMockBuilder(\Magento\Framework\App\Response\Http::class)
106  ->disableOriginalConstructor()
107  ->getMock();
108  $this->contextMock = $this->getMockBuilder(\Magento\Backend\App\Action\Context::class)
109  ->disableOriginalConstructor()
110  ->getMock();
111  $this->uiFactoryMock = $this->getMockBuilder(\Magento\Framework\View\Element\UiComponentFactory::class)
112  ->disableOriginalConstructor()
113  ->getMock();
114  $this->authorizationMock = $this->getMockBuilder(\Magento\Framework\AuthorizationInterface::class)
115  ->getMockForAbstractClass();
116  $this->sessionMock = $this->getMockBuilder(\Magento\Backend\Model\Session::class)
117  ->disableOriginalConstructor()
118  ->getMock();
119  $this->actionFlagMock = $this->getMockBuilder(\Magento\Framework\App\ActionFlag::class)
120  ->disableOriginalConstructor()
121  ->getMock();
122  $this->helperMock = $this->getMockBuilder(\Magento\Backend\Helper\Data::class)
123  ->disableOriginalConstructor()
124  ->getMock();
125  $this->uiComponentContextMock = $this->getMockForAbstractClass(
126  ContextInterface::class
127  );
128  $this->dataProviderMock = $this->getMockForAbstractClass(
129  \Magento\Framework\View\Element\UiComponent\DataProvider\DataProviderInterface::class
130  );
131  $this->uiComponentMock = $this->getMockForAbstractClass(
132  \Magento\Framework\View\Element\UiComponentInterface::class,
133  [],
134  '',
135  false,
136  true,
137  true,
138  ['render']
139  );
140 
141  $this->resultJsonFactoryMock = $this->getMockBuilder(
142  \Magento\Framework\Controller\Result\JsonFactory::class
143  )
144  ->disableOriginalConstructor()
145  ->getMock();
146 
147  $this->loggerMock = $this->getMockForAbstractClass(\Psr\Log\LoggerInterface::class);
148 
149  $this->contextMock->expects($this->any())
150  ->method('getRequest')
151  ->willReturn($this->requestMock);
152  $this->contextMock->expects($this->any())
153  ->method('getResponse')
154  ->willReturn($this->responseMock);
155  $this->contextMock->expects($this->any())
156  ->method('getAuthorization')
157  ->willReturn($this->authorizationMock);
158  $this->contextMock->expects($this->any())
159  ->method('getSession')
160  ->willReturn($this->sessionMock);
161  $this->contextMock->expects($this->any())
162  ->method('getActionFlag')
163  ->willReturn($this->actionFlagMock);
164  $this->contextMock->expects($this->any())
165  ->method('getHelper')
166  ->willReturn($this->helperMock);
167  $this->uiComponentContextMock->expects($this->once())
168  ->method('getDataProvider')
169  ->willReturn($this->dataProviderMock);
170  $this->uiComponentTypeResolverMock = $this->getMockBuilder(UiComponentTypeResolver::class)
171  ->disableOriginalConstructor()
172  ->getMock();
173 
174  $this->objectManagerHelper = new ObjectManagerHelper($this);
175 
176  $this->render = $this->objectManagerHelper->getObject(
177  \Magento\Ui\Controller\Adminhtml\Index\Render::class,
178  [
179  'context' => $this->contextMock,
180  'factory' => $this->uiFactoryMock,
181  'contentTypeResolver' => $this->uiComponentTypeResolverMock,
182  'resultJsonFactory' => $this->resultJsonFactoryMock,
183  'logger' => $this->loggerMock,
184  ]
185  );
186  }
187 
189  {
190  $name = 'test-name';
191  $renderedData = '<html>data</html>';
192 
193  $this->requestMock->expects($this->any())
194  ->method('getParam')
195  ->with('namespace')
196  ->willReturn($name);
197  $this->requestMock->expects($this->any())
198  ->method('getParams')
199  ->willReturn([]);
200  $this->responseMock->expects($this->once())
201  ->method('appendBody')
202  ->willThrowException(new \Exception('exception'));
203 
204  $jsonResultMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Json::class)
205  ->disableOriginalConstructor()
206  ->setMethods(['setData'])
207  ->getMock();
208 
209  $this->resultJsonFactoryMock->expects($this->once())
210  ->method('create')
211  ->willReturn($jsonResultMock);
212 
213  $jsonResultMock->expects($this->once())
214  ->method('setData')
215  ->willReturnSelf();
216 
217  $this->loggerMock->expects($this->once())
218  ->method('critical')
219  ->willReturnSelf();
220 
221  $this->dataProviderMock->expects($this->once())
222  ->method('getConfigData')
223  ->willReturn([]);
224 
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->once())
232  ->method('getContext')
233  ->willReturn($this->uiComponentContextMock);
234  $this->uiFactoryMock->expects($this->once())
235  ->method('create')
236  ->willReturn($this->uiComponentMock);
237 
238  $this->render->executeAjaxRequest();
239  }
240 
241  public function testExecuteAjaxRequest()
242  {
243  $name = 'test-name';
244  $renderedData = '<html>data</html>';
245 
246  $this->requestMock->expects($this->any())
247  ->method('getParam')
248  ->with('namespace')
249  ->willReturn($name);
250  $this->requestMock->expects($this->any())
251  ->method('getParams')
252  ->willReturn([]);
253  $this->responseMock->expects($this->once())
254  ->method('appendBody')
255  ->with($renderedData);
256  $this->dataProviderMock->expects($this->once())
257  ->method('getConfigData')
258  ->willReturn([]);
259 
260  $this->uiComponentMock->expects($this->once())
261  ->method('render')
262  ->willReturn($renderedData);
263  $this->uiComponentMock->expects($this->once())
264  ->method('getChildComponents')
265  ->willReturn([]);
266  $this->uiComponentMock->expects($this->any())
267  ->method('getContext')
268  ->willReturn($this->uiComponentContextMock);
269  $this->uiFactoryMock->expects($this->once())
270  ->method('create')
271  ->willReturn($this->uiComponentMock);
272  $this->uiComponentTypeResolverMock->expects($this->once())
273  ->method('resolve')
274  ->with($this->uiComponentContextMock)
275  ->willReturn('application/json');
276  $this->responseMock->expects($this->once())->method('setHeader')
277  ->with('Content-Type', 'application/json', true);
278 
279  $this->render->executeAjaxRequest();
280  }
281 
288  public function testExecuteAjaxRequestWithoutPermissions(array $dataProviderConfig, $isAllowed, $authCallCount = 1)
289  {
290  $name = 'test-name';
291  $renderedData = '<html>data</html>';
292 
293  $this->requestMock->expects($this->any())
294  ->method('getParam')
295  ->with('namespace')
296  ->willReturn($name);
297  $this->requestMock->expects($this->any())
298  ->method('getParams')
299  ->willReturn([]);
300  if ($isAllowed === false) {
301  $this->requestMock->expects($this->once())
302  ->method('isAjax')
303  ->willReturn(true);
304  }
305  $this->responseMock->expects($this->never())
306  ->method('setRedirect');
307  $this->responseMock->expects($this->any())
308  ->method('appendBody')
309  ->with($renderedData);
310 
311  $this->dataProviderMock->expects($this->once())
312  ->method('getConfigData')
313  ->willReturn($dataProviderConfig);
314 
315  $this->authorizationMock->expects($this->exactly($authCallCount))
316  ->method('isAllowed')
317  ->with(isset($dataProviderConfig['aclResource']) ? $dataProviderConfig['aclResource'] : null)
318  ->willReturn($isAllowed);
319 
320  $this->uiComponentMock->expects($this->any())
321  ->method('render')
322  ->willReturn($renderedData);
323  $this->uiComponentMock->expects($this->any())
324  ->method('getChildComponents')
325  ->willReturn([]);
326  $this->uiComponentMock->expects($this->any())
327  ->method('getContext')
328  ->willReturn($this->uiComponentContextMock);
329  $this->uiFactoryMock->expects($this->once())
330  ->method('create')
331  ->willReturn($this->uiComponentMock);
332 
333  $this->render->executeAjaxRequest();
334  }
335 
340  {
341  $aclResource = 'Magento_Test::index_index';
342  return [
343  [
344  'dataProviderConfig' => ['aclResource' => $aclResource],
345  'isAllowed' => true
346  ],
347  [
348  'dataProviderConfig' => ['aclResource' => $aclResource],
349  'isAllowed' => false
350  ],
351  [
352  'dataProviderConfig' => [],
353  'isAllowed' => null,
354  'authCallCount' => 0
355  ],
356  ];
357  }
358 }
return false
Definition: gallery.phtml:36
testExecuteAjaxRequestWithoutPermissions(array $dataProviderConfig, $isAllowed, $authCallCount=1)
Definition: RenderTest.php:288
$isAllowed
Definition: get.php:20
if(!isset($_GET['name'])) $name
Definition: log.php:14