PageTest.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Test\Unit\Result;
  7. use Magento\Framework\View\Page\Config as PageConfig;
  8. use Magento\Framework\View\EntitySpecificHandlesList;
  9. /**
  10. * Result Page Test
  11. *
  12. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  13. */
  14. class PageTest extends \PHPUnit\Framework\TestCase
  15. {
  16. /**
  17. * @var \Magento\Framework\View\Result\Page
  18. */
  19. private $page;
  20. /**
  21. * @var \Magento\Framework\View\Element\Template\Context|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. private $context;
  24. /**
  25. * @var \Magento\Framework\App\Request\Http|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. private $request;
  28. /**
  29. * @var \Magento\Framework\View\Layout|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. private $layout;
  32. /**
  33. * @var \Magento\Framework\View\Model\Layout\Merge|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. private $layoutMerge;
  36. /**
  37. * @var \Magento\Framework\View\Page\Config|\PHPUnit_Framework_MockObject_MockObject
  38. */
  39. private $pageConfig;
  40. /**
  41. * @var \Magento\Framework\Translate\InlineInterface|\PHPUnit_Framework_MockObject_MockObject
  42. */
  43. private $translateInline;
  44. /**
  45. * @var \Magento\Framework\View\Page\Config\Renderer|\PHPUnit_Framework_MockObject_MockObject
  46. */
  47. private $pageConfigRenderer;
  48. /**
  49. * @var \Magento\Framework\View\FileSystem|\PHPUnit_Framework_MockObject_MockObject
  50. */
  51. private $viewFileSystem;
  52. /**
  53. * @var \Magento\Framework\View\LayoutFactory|\PHPUnit_Framework_MockObject_MockObject
  54. */
  55. private $layoutFactory;
  56. /** @var \PHPUnit_Framework_MockObject_MockObject|EntitySpecificHandlesList */
  57. private $entitySpecificHandlesListMock;
  58. protected function setUp()
  59. {
  60. $this->layout = $this->getMockBuilder(\Magento\Framework\View\Layout::class)
  61. ->setMethods(['addHandle', 'getUpdate', 'isLayoutDefined'])
  62. ->disableOriginalConstructor()
  63. ->getMock();
  64. $this->layoutFactory = $this->getMockBuilder(\Magento\Framework\View\LayoutFactory::class)
  65. ->disableOriginalConstructor()
  66. ->getMock();
  67. $this->layoutFactory->expects($this->any())->method('create')->will($this->returnValue($this->layout));
  68. $this->layoutMerge = $this->getMockBuilder(\Magento\Framework\View\Model\Layout\Merge::class)
  69. ->disableOriginalConstructor()
  70. ->getMock();
  71. $this->layout->expects($this->any())
  72. ->method('getUpdate')
  73. ->will($this->returnValue($this->layoutMerge));
  74. $this->request = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
  75. ->disableOriginalConstructor()
  76. ->getMock();
  77. $this->pageConfig = $this->getMockBuilder(\Magento\Framework\View\Page\Config::class)
  78. ->disableOriginalConstructor()
  79. ->getMock();
  80. $this->viewFileSystem = $this->getMockBuilder(\Magento\Framework\View\FileSystem::class)
  81. ->disableOriginalConstructor()
  82. ->getMock();
  83. $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  84. $this->context = $objectManagerHelper->getObject(
  85. \Magento\Framework\View\Element\Template\Context::class,
  86. [
  87. 'layout' => $this->layout,
  88. 'request' => $this->request,
  89. 'viewFileSystem' => $this->viewFileSystem,
  90. 'pageConfig' => $this->pageConfig
  91. ]
  92. );
  93. $this->translateInline = $this->createMock(\Magento\Framework\Translate\InlineInterface::class);
  94. $this->pageConfigRenderer = $this->getMockBuilder(\Magento\Framework\View\Page\Config\Renderer::class)
  95. ->disableOriginalConstructor()
  96. ->getMock();
  97. $pageConfigRendererFactory = $this->getMockBuilder(\Magento\Framework\View\Page\Config\RendererFactory::class)
  98. ->disableOriginalConstructor()
  99. ->setMethods(['create'])
  100. ->getMock();
  101. $pageConfigRendererFactory->expects($this->once())
  102. ->method('create')
  103. ->with(['pageConfig' => $this->pageConfig])
  104. ->willReturn($this->pageConfigRenderer);
  105. $this->entitySpecificHandlesListMock = $this->createMock(EntitySpecificHandlesList::class);
  106. $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  107. $this->page = $objectManagerHelper->getObject(
  108. \Magento\Framework\View\Result\Page::class,
  109. [
  110. 'isIsolated' => true,
  111. 'layoutFactory' => $this->layoutFactory,
  112. 'context' => $this->context,
  113. 'translateInline' => $this->translateInline,
  114. 'pageConfigRendererFactory' => $pageConfigRendererFactory,
  115. 'entitySpecificHandlesList' => $this->entitySpecificHandlesListMock
  116. ]
  117. );
  118. }
  119. public function testInitLayout()
  120. {
  121. $handleDefault = 'default';
  122. $fullActionName = 'full_action_name';
  123. $this->request->expects($this->any())
  124. ->method('getFullActionName')
  125. ->will($this->returnValue($fullActionName));
  126. $this->layoutMerge->expects($this->at(0))
  127. ->method('addHandle')
  128. ->with($handleDefault)
  129. ->willReturnSelf();
  130. $this->layoutMerge->expects($this->at(1))
  131. ->method('addHandle')
  132. ->with($fullActionName)
  133. ->willReturnSelf();
  134. $this->layoutMerge->expects($this->at(2))
  135. ->method('isLayoutDefined')
  136. ->willReturn(false);
  137. $this->assertEquals($this->page, $this->page->initLayout());
  138. }
  139. public function testInitLayoutLayoutDefined()
  140. {
  141. $handleDefault = 'default';
  142. $fullActionName = 'full_action_name';
  143. $this->request->expects($this->any())
  144. ->method('getFullActionName')
  145. ->will($this->returnValue($fullActionName));
  146. $this->layoutMerge->expects($this->at(0))
  147. ->method('addHandle')
  148. ->with($handleDefault)
  149. ->willReturnSelf();
  150. $this->layoutMerge->expects($this->at(1))
  151. ->method('addHandle')
  152. ->with($fullActionName)
  153. ->willReturnSelf();
  154. $this->layoutMerge->expects($this->at(2))
  155. ->method('isLayoutDefined')
  156. ->willReturn(true);
  157. $this->layoutMerge->expects($this->at(3))
  158. ->method('removeHandle')
  159. ->with($handleDefault)
  160. ->willReturnSelf();
  161. $this->assertEquals($this->page, $this->page->initLayout());
  162. }
  163. public function testGetConfig()
  164. {
  165. $this->assertEquals($this->pageConfig, $this->page->getConfig());
  166. }
  167. public function testGetDefaultLayoutHandle()
  168. {
  169. $fullActionName = 'Full_Action_Name';
  170. $expectedFullActionName = 'full_action_name';
  171. $this->request->expects($this->any())
  172. ->method('getFullActionName')
  173. ->will($this->returnValue($fullActionName));
  174. $this->assertEquals($expectedFullActionName, $this->page->getDefaultLayoutHandle());
  175. }
  176. public function testAddPageLayoutHandles()
  177. {
  178. $fullActionName = 'Full_Action_Name';
  179. $defaultHandle = null;
  180. $parameters = [
  181. 'key_one' => 'val_one',
  182. 'key_two' => 'val_two',
  183. ];
  184. $expected = [
  185. 'full_action_name',
  186. 'full_action_name_key_one_val_one',
  187. 'full_action_name_key_two_val_two',
  188. ];
  189. $this->request->expects($this->any())
  190. ->method('getFullActionName')
  191. ->will($this->returnValue($fullActionName));
  192. $this->layoutMerge->expects($this->any())
  193. ->method('addHandle')
  194. ->with($expected)
  195. ->willReturnSelf();
  196. $this->entitySpecificHandlesListMock->expects($this->at(0))
  197. ->method('addHandle')->with('full_action_name_key_one_val_one');
  198. $this->entitySpecificHandlesListMock->expects($this->at(1))
  199. ->method('addHandle')->with('full_action_name_key_two_val_two');
  200. $this->page->addPageLayoutHandles($parameters, $defaultHandle);
  201. }
  202. public function testAddPageLayoutHandlesNotEntitySpecific()
  203. {
  204. $fullActionName = 'Full_Action_Name';
  205. $defaultHandle = null;
  206. $parameters = [
  207. 'key_one' => 'val_one',
  208. 'key_two' => 'val_two',
  209. ];
  210. $expected = [
  211. 'full_action_name',
  212. 'full_action_name_key_one_val_one',
  213. 'full_action_name_key_two_val_two',
  214. ];
  215. $this->request->expects($this->any())
  216. ->method('getFullActionName')
  217. ->will($this->returnValue($fullActionName));
  218. $this->layoutMerge->expects($this->any())
  219. ->method('addHandle')
  220. ->with($expected)
  221. ->willReturnSelf();
  222. $this->entitySpecificHandlesListMock->expects($this->never())->method('addHandle');
  223. $this->page->addPageLayoutHandles($parameters, $defaultHandle, false);
  224. }
  225. public function testAddPageLayoutHandlesWithDefaultHandle()
  226. {
  227. $defaultHandle = 'default_handle';
  228. $parameters = [
  229. 'key_one' => 'val_one',
  230. 'key_two' => 'val_two',
  231. ];
  232. $expected = [
  233. 'default_handle',
  234. 'default_handle_key_one_val_one',
  235. 'default_handle_key_two_val_two',
  236. ];
  237. $this->request->expects($this->never())
  238. ->method('getFullActionName');
  239. $this->layoutMerge->expects($this->any())
  240. ->method('addHandle')
  241. ->with($expected)
  242. ->willReturnSelf();
  243. $this->page->addPageLayoutHandles($parameters, $defaultHandle);
  244. }
  245. }