Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PageTest.php
Go to the documentation of this file.
1 <?php
7 
14 class PageTest extends \PHPUnit\Framework\TestCase
15 {
19  protected $object;
20 
24  protected $actionMock;
25 
29  protected $pageFactoryMock;
30 
34  protected $pageMock;
35 
39  protected $storeManagerMock;
40 
44  protected $localeDateMock;
45 
49  protected $designMock;
50 
54  protected $pageConfigMock;
55 
59  protected $escaperMock;
60 
65 
69  protected $eventManagerMock;
70 
74  protected $urlBuilderMock;
75 
79  protected $storeMock;
80 
84  protected $resultPageMock;
85 
89  protected $layoutMock;
90 
95 
99  protected $blockMock;
100 
105 
110 
115 
119  private $httpRequestMock;
120 
124  protected function setUp()
125  {
126  $this->actionMock = $this->getMockBuilder(\Magento\Framework\App\Action\Action::class)
127  ->disableOriginalConstructor()
128  ->getMock();
129  $this->pageFactoryMock = $this->getMockBuilder(\Magento\Cms\Model\PageFactory::class)
130  ->disableOriginalConstructor()
131  ->setMethods(['create'])
132  ->getMock();
133  $this->pageMock = $this->getMockBuilder(\Magento\Cms\Model\Page::class)
134  ->disableOriginalConstructor()
135  ->setMethods(
136  [
137  'getId',
138  'setStoreId',
139  'load',
140  'getCustomThemeFrom',
141  'getCustomThemeTo',
142  'getCustomTheme',
143  'getPageLayout',
144  'getIdentifier',
145  'getCustomPageLayout',
146  'getCustomLayoutUpdateXml',
147  'getLayoutUpdateXml',
148  'getContentHeading',
149  ]
150  )
151  ->getMock();
152  $this->storeManagerMock = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
153  ->getMockForAbstractClass();
154  $this->localeDateMock = $this->getMockBuilder(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::class)
155  ->getMockForAbstractClass();
156  $this->designMock = $this->getMockBuilder(\Magento\Framework\View\DesignInterface::class)
157  ->getMockForAbstractClass();
158  $this->pageConfigMock = $this->getMockBuilder(\Magento\Framework\View\Page\Config::class)
159  ->disableOriginalConstructor()
160  ->getMock();
161  $this->escaperMock = $this->getMockBuilder(\Magento\Framework\Escaper::class)
162  ->disableOriginalConstructor()
163  ->getMock();
164  $this->eventManagerMock = $this->getMockBuilder(\Magento\Framework\Event\ManagerInterface::class)
165  ->getMockForAbstractClass();
166  $this->urlBuilderMock = $this->getMockBuilder(\Magento\Framework\UrlInterface::class)
167  ->getMockForAbstractClass();
168  $this->httpRequestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
169  ->getMockForAbstractClass();
170  $this->storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
171  ->disableOriginalConstructor()
172  ->getMock();
173  $this->resultPageMock = $this->getMockBuilder(\Magento\Framework\View\Result\Page::class)
174  ->disableOriginalConstructor()
175  ->getMock();
176  $this->layoutMock = $this->getMockBuilder(\Magento\Framework\View\LayoutInterface::class)
177  ->getMockForAbstractClass();
178  $this->layoutProcessorMock = $this->getMockBuilder(\Magento\Framework\View\Layout\ProcessorInterface::class)
179  ->getMockForAbstractClass();
180  $this->blockMock = $this->getMockBuilder(\Magento\Framework\View\Element\AbstractBlock::class)
181  ->setMethods(['setContentHeading'])
182  ->disableOriginalConstructor()
183  ->getMockForAbstractClass();
184  $this->messagesBlockMock = $this->getMockBuilder(\Magento\Framework\View\Element\Messages::class)
185  ->disableOriginalConstructor()
186  ->getMock();
187  $this->messageManagerMock = $this->getMockBuilder(\Magento\Framework\Message\ManagerInterface::class)
188  ->getMockForAbstractClass();
189  $this->messageCollectionMock = $this->getMockBuilder(\Magento\Framework\Message\Collection::class)
190  ->disableOriginalConstructor()
191  ->getMock();
192 
193  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
194  $context = $objectManager->getObject(
195  \Magento\Framework\App\Helper\Context::class,
196  [
197  'eventManager' => $this->eventManagerMock,
198  'urlBuilder' => $this->urlBuilderMock,
199  'httpRequest' => $this->httpRequestMock,
200  ]
201  );
202 
203  $this->resultPageFactory = $this->createMock(\Magento\Framework\View\Result\PageFactory::class);
204 
205  $this->object = $objectManager->getObject(
206  \Magento\Cms\Helper\Page::class,
207  [
208  'context' => $context,
209  'pageFactory' => $this->pageFactoryMock,
210  'page' => $this->pageMock,
211  'storeManager' => $this->storeManagerMock,
212  'localeDate' => $this->localeDateMock,
213  'design' => $this->designMock,
214  'pageConfig' => $this->pageConfigMock,
215  'escaper' => $this->escaperMock,
216  'messageManager' => $this->messageManagerMock,
217  'resultPageFactory' => $this->resultPageFactory
218  ]
219  );
220  }
221 
237  public function testPrepareResultPage(
238  $pageId,
239  $internalPageId,
240  $pageLoadResultIndex,
241  $customPageLayout,
242  $handle,
243  $customLayoutUpdateXml,
244  $layoutUpdate,
245  $expectedResult
246  ) {
247  $storeId = 321;
248  $customThemeFrom = 'customThemeFrom';
249  $customThemeTo = 'customThemeTo';
250  $isScopeDateInInterval = true;
251  $customTheme = 'customTheme';
252  $pageLayout = 'pageLayout';
253  $pageIdentifier = 111;
254  $layoutUpdateXml = 'layoutUpdateXml';
255  $contentHeading = 'contentHeading';
256  $escapedContentHeading = 'escapedContentHeading';
257  $pageLoadResultCollection = [
258  null,
260  ];
261 
262  $this->pageMock->expects($this->any())
263  ->method('getId')
264  ->willReturn($internalPageId);
265  $this->storeManagerMock->expects($this->any())
266  ->method('getStore')
267  ->willReturn($this->storeMock);
268  $this->storeMock->expects($this->any())
269  ->method('getId')
270  ->willReturn($storeId);
271  $this->pageMock->expects($this->any())
272  ->method('setStoreId')
273  ->with($storeId)
274  ->willReturnSelf();
275  $this->pageMock->expects($this->any())
276  ->method('load')
277  ->with($pageId)
278  ->willReturn($pageLoadResultCollection[$pageLoadResultIndex]);
279  $this->pageMock->expects($this->any())
280  ->method('getCustomThemeFrom')
281  ->willReturn($customThemeFrom);
282  $this->pageMock->expects($this->any())
283  ->method('getCustomThemeTo')
284  ->willReturn($customThemeTo);
285  $this->localeDateMock->expects($this->any())
286  ->method('isScopeDateInInterval')
287  ->with(null, $customThemeFrom, $customThemeTo)
288  ->willReturn($isScopeDateInInterval);
289  $this->pageMock->expects($this->any())
290  ->method('getCustomTheme')
291  ->willReturn($customTheme);
292  $this->designMock->expects($this->any())
293  ->method('setDesignTheme')
294  ->with($customTheme)
295  ->willReturnSelf();
296  $this->pageMock->expects($this->any())
297  ->method('getPageLayout')
298  ->willReturn($pageLayout);
299  $this->pageMock->expects($this->any())
300  ->method('getCustomPageLayout')
301  ->willReturn($customPageLayout);
302  $this->resultPageFactory->expects($this->any())->method('create')
303  ->will($this->returnValue($this->resultPageMock));
304  $this->resultPageMock->expects($this->any())
305  ->method('getConfig')
306  ->willReturn($this->pageConfigMock);
307  $this->pageConfigMock->expects($this->any())
308  ->method('setPageLayout')
309  ->with($handle)
310  ->willReturnSelf();
311  $this->resultPageMock->expects($this->any())
312  ->method('initLayout')
313  ->willReturnSelf();
314  $this->resultPageMock->expects($this->any())
315  ->method('getLayout')
316  ->willReturn($this->layoutMock);
317  $this->layoutMock->expects($this->any())
318  ->method('getUpdate')
319  ->willReturn($this->layoutProcessorMock);
320  $this->layoutProcessorMock->expects($this->any())
321  ->method('addHandle')
322  ->with('cms_page_view')
323  ->willReturnSelf();
324  $this->pageMock->expects($this->any())
325  ->method('getIdentifier')
326  ->willReturn($pageIdentifier);
327  $this->eventManagerMock->expects($this->any())
328  ->method('dispatch')
329  ->with(
330  'cms_page_render',
331  [
332  'page' => $this->pageMock,
333  'controller_action' => $this->actionMock,
334  'request' => $this->httpRequestMock,
335  ]
336  );
337  $this->pageMock->expects($this->any())
338  ->method('getCustomLayoutUpdateXml')
339  ->willReturn($customLayoutUpdateXml);
340  $this->pageMock->expects($this->any())
341  ->method('getLayoutUpdateXml')
342  ->willReturn($layoutUpdateXml);
343  $this->layoutProcessorMock->expects($this->any())
344  ->method('addUpdate')
345  ->with($layoutUpdate)
346  ->willReturnSelf();
347  $this->layoutMock->expects($this->any())
348  ->method('getBlock')
349  ->with('page_content_heading')
350  ->willReturn($this->blockMock);
351  $this->pageMock->expects($this->any())
352  ->method('getContentHeading')
353  ->willReturn($contentHeading);
354  $this->escaperMock->expects($this->any())
355  ->method('escapeHtml')
356  ->with($contentHeading)
357  ->willReturn($escapedContentHeading);
358  $this->blockMock->expects($this->any())
359  ->method('setContentHeading')
360  ->with($escapedContentHeading)
361  ->willReturnSelf();
362 
363  if ($expectedResult) {
364  $expectedResult = $this->resultPageMock;
365  }
366 
367  $this->assertSame(
368  $expectedResult,
369  $this->object->prepareResultPage($this->actionMock, $pageId)
370  );
371  }
372 
377  {
378  return [
379  'ids NOT EQUAL BUT page->load() NOT SUCCESSFUL' => [
380  'pageId' => 123,
381  'internalPageId' => 234,
382  'pageLoadResultIndex' => 0,
383  'customPageLayout' => 'DOES NOT MATTER',
384  'handle' => 'DOES NOT MATTER',
385  'customLayoutUpdateXml' => 'DOES NOT MATTER',
386  'layoutUpdate' => 'DOES NOT MATTER',
387  'expectedResult' => false,
388  ],
389  'page->load IS SUCCESSFUL BUT internalPageId IS EMPTY' => [
390  'pageId' => 123,
391  'internalPageId' => null,
392  'pageLoadResultIndex' => 1,
393  'customPageLayout' => 'DOES NOT MATTER',
394  'handle' => 'DOES NOT MATTER',
395  'customLayoutUpdateXml' => 'DOES NOT MATTER',
396  'layoutUpdate' => 'DOES NOT MATTER',
397  'expectedResult' => false,
398  ],
399  'getPageLayout() AND getLayoutUpdateXml() ARE USED' => [
400  'pageId' => 123,
401  'internalPageId' => 234,
402  'pageLoadResultIndex' => 1,
403  'customPageLayout' => 'empty',
404  'handle' => 'pageLayout',
405  'customLayoutUpdateXml' => '',
406  'layoutUpdate' => 'layoutUpdateXml',
407  'expectedResult' => true,
408  ],
409  'getCustomPageLayout() AND getCustomLayoutUpdateXml() ARE USED' => [
410  'pageId' => 123,
411  'internalPageId' => 234,
412  'pageLoadResultIndex' => 1,
413  'customPageLayout' => 'customPageLayout',
414  'handle' => 'customPageLayout',
415  'customLayoutUpdateXml' => 'customLayoutUpdateXml',
416  'layoutUpdate' => 'customLayoutUpdateXml',
417  'expectedResult' => true,
418  ]
419  ];
420  }
421 
431  public function testGetPageUrl(
432  $pageId,
433  $internalPageId,
434  $pageLoadResultIndex,
435  $expectedResult
436  ) {
437  $storeId = 321;
438  $pageIdentifier = 111;
439  $url = '/some/url';
440  $pageLoadResultCollection = [
441  null,
443  ];
444 
445  $this->pageFactoryMock->expects($this->any())
446  ->method('create')
447  ->willReturn($this->pageMock);
448  $this->pageMock->expects($this->any())
449  ->method('getId')
450  ->willReturn($internalPageId);
451  $this->storeManagerMock->expects($this->any())
452  ->method('getStore')
453  ->willReturn($this->storeMock);
454  $this->storeMock->expects($this->any())
455  ->method('getId')
456  ->willReturn($storeId);
457  $this->pageMock->expects($this->any())
458  ->method('setStoreId')
459  ->with($storeId)
460  ->willReturnSelf();
461  $this->pageMock->expects($this->any())
462  ->method('load')
463  ->with($pageId)
464  ->willReturn($pageLoadResultCollection[$pageLoadResultIndex]);
465  $this->pageMock->expects($this->any())
466  ->method('getIdentifier')
467  ->willReturn($pageIdentifier);
468  $this->urlBuilderMock->expects($this->any())
469  ->method('getUrl')
470  ->with(null, ['_direct' => $pageIdentifier])
471  ->willReturn($url);
472 
473  $this->assertEquals($expectedResult, $this->object->getPageUrl($pageId));
474  }
475 
479  public function getPageUrlDataProvider()
480  {
481  return [
482  'ids NOT EQUAL BUT page->load() NOT SUCCESSFUL' => [
483  'pageId' => 123,
484  'internalPageId' => null,
485  'pageLoadResultIndex' => 0,
486  'expectedResult' => null,
487  ],
488  'page->load() IS SUCCESSFUL BUT internalId IS EMPTY' => [
489  'pageId' => 123,
490  'internalPageId' => null,
491  'pageLoadResultIndex' => 1,
492  'expectedResult' => null,
493  ],
494  'SUCCESS' => [
495  'pageId' => 123,
496  'internalPageId' => 234,
497  'pageLoadResultIndex' => 1,
498  'expectedResult' => '/some/url',
499  ]
500  ];
501  }
502 }
$objectManager
Definition: bootstrap.php:17
testPrepareResultPage( $pageId, $internalPageId, $pageLoadResultIndex, $customPageLayout, $handle, $customLayoutUpdateXml, $layoutUpdate, $expectedResult)
Definition: PageTest.php:237
$handle
testGetPageUrl( $pageId, $internalPageId, $pageLoadResultIndex, $expectedResult)
Definition: PageTest.php:431