JavascriptTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\PageCache\Test\Unit\Block;
  7. /**
  8. * @covers \Magento\PageCache\Block\Javascript
  9. */
  10. class JavascriptTest extends \PHPUnit\Framework\TestCase
  11. {
  12. const COOKIE_NAME = 'private_content_version';
  13. /**
  14. * @var \Magento\PageCache\Block\Javascript|\PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $blockJavascript;
  17. /**
  18. * @var \Magento\Framework\View\Element\Template\Context|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $contextMock;
  21. /**
  22. * @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $requestMock;
  25. /**
  26. * @var \Magento\Framework\View\LayoutInterface|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $layoutMock;
  29. /**
  30. * @var \Magento\Framework\View\Layout\ProcessorInterface|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $layoutUpdateMock;
  33. /**
  34. * @var \Magento\Framework\UrlInterface|\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. protected $urlBuilderMock;
  37. protected function setUp()
  38. {
  39. $this->contextMock = $this->getMockBuilder(\Magento\Framework\View\Element\Template\Context::class)
  40. ->disableOriginalConstructor()
  41. ->getMock();
  42. $this->requestMock = $this->createPartialMock(\Magento\Framework\App\RequestInterface::class, [
  43. 'getRouteName',
  44. 'getControllerName',
  45. 'getModuleName',
  46. 'getActionName',
  47. 'getRequestUri',
  48. 'getParam',
  49. 'setParams',
  50. 'getParams',
  51. 'setModuleName',
  52. 'isSecure',
  53. 'setActionName',
  54. 'setRequestUri',
  55. 'getCookie'
  56. ]);
  57. $this->layoutMock = $this->getMockBuilder(\Magento\Framework\View\LayoutInterface::class)
  58. ->disableOriginalConstructor()
  59. ->getMock();
  60. $this->layoutUpdateMock = $this->getMockBuilder(\Magento\Framework\View\Layout\ProcessorInterface::class)
  61. ->disableOriginalConstructor()
  62. ->getMock();
  63. $this->urlBuilderMock = $this->getMockBuilder(\Magento\Framework\UrlInterface::class)
  64. ->disableOriginalConstructor()
  65. ->getMock();
  66. $this->contextMock->expects($this->any())
  67. ->method('getRequest')
  68. ->willReturn($this->requestMock);
  69. $this->contextMock->expects($this->any())
  70. ->method('getLayout')
  71. ->willReturn($this->layoutMock);
  72. $this->contextMock->expects($this->any())
  73. ->method('getUrlBuilder')
  74. ->willReturn($this->urlBuilderMock);
  75. $this->layoutMock->expects($this->any())
  76. ->method('getUpdate')
  77. ->willReturn($this->layoutUpdateMock);
  78. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  79. $this->blockJavascript = $objectManager->getObject(
  80. \Magento\PageCache\Block\Javascript::class,
  81. [
  82. 'context' => $this->contextMock
  83. ]
  84. );
  85. }
  86. /**
  87. * @covers \Magento\PageCache\Block\Javascript::getScriptOptions
  88. * @param bool $isSecure
  89. * @param string $url
  90. * @param string $expectedResult
  91. * @dataProvider getScriptOptionsDataProvider
  92. */
  93. public function testGetScriptOptions($isSecure, $url, $expectedResult)
  94. {
  95. $handles = [
  96. 'some',
  97. 'handles',
  98. 'here'
  99. ];
  100. $this->requestMock->expects($this->once())
  101. ->method('isSecure')
  102. ->willReturn($isSecure);
  103. $this->requestMock->expects($this->once())
  104. ->method('getRouteName')
  105. ->will($this->returnValue('route'));
  106. $this->requestMock->expects($this->once())
  107. ->method('getControllerName')
  108. ->will($this->returnValue('controller'));
  109. $this->requestMock->expects($this->once())
  110. ->method('getActionName')
  111. ->will($this->returnValue('action'));
  112. $this->requestMock->expects($this->once())
  113. ->method('getRequestUri')
  114. ->will($this->returnValue('uri'));
  115. $this->urlBuilderMock->expects($this->once())
  116. ->method('getUrl')
  117. ->willReturn($url);
  118. $this->layoutUpdateMock->expects($this->once())
  119. ->method('getHandles')
  120. ->willReturn($handles);
  121. $this->assertRegExp($expectedResult, $this->blockJavascript->getScriptOptions());
  122. }
  123. /**
  124. * @return array
  125. */
  126. public function getScriptOptionsDataProvider()
  127. {
  128. return [
  129. 'http' => [
  130. 'isSecure' => false,
  131. 'url' => 'http://some-name.com/page_cache/block/render',
  132. 'expectedResult' => '~http:\\\\/\\\\/some-name\\.com.+\\["some","handles","here"\\]~'
  133. ],
  134. 'https' => [
  135. 'isSecure' => true,
  136. 'url' => 'https://some-name.com/page_cache/block/render',
  137. 'expectedResult' => '~https:\\\\/\\\\/some-name\\.com.+\\["some","handles","here"\\]~'
  138. ]
  139. ];
  140. }
  141. /**
  142. * @covers \Magento\PageCache\Block\Javascript::getScriptOptions
  143. * @param string $url
  144. * @param string $route
  145. * @param string $controller
  146. * @param string $action
  147. * @param string $uri
  148. * @param string $expectedResult
  149. * @dataProvider getScriptOptionsPrivateContentDataProvider
  150. */
  151. public function testGetScriptOptionsPrivateContent($url, $route, $controller, $action, $uri, $expectedResult)
  152. {
  153. $handles = [
  154. 'some',
  155. 'handles',
  156. 'here'
  157. ];
  158. $this->requestMock->expects($this->once())
  159. ->method('isSecure')
  160. ->willReturn(false);
  161. $this->requestMock->expects($this->once())
  162. ->method('getRouteName')
  163. ->will($this->returnValue($route));
  164. $this->requestMock->expects($this->once())
  165. ->method('getControllerName')
  166. ->will($this->returnValue($controller));
  167. $this->requestMock->expects($this->once())
  168. ->method('getActionName')
  169. ->will($this->returnValue($action));
  170. $this->requestMock->expects($this->once())
  171. ->method('getRequestUri')
  172. ->will($this->returnValue($uri));
  173. $this->urlBuilderMock->expects($this->once())
  174. ->method('getUrl')
  175. ->willReturn($url);
  176. $this->layoutUpdateMock->expects($this->once())
  177. ->method('getHandles')
  178. ->willReturn($handles);
  179. $this->assertRegExp($expectedResult, $this->blockJavascript->getScriptOptions());
  180. }
  181. /**
  182. * @return array
  183. */
  184. public function getScriptOptionsPrivateContentDataProvider()
  185. {
  186. // @codingStandardsIgnoreStart
  187. return [
  188. 'http' => [
  189. 'url' => 'http://some-name.com/page_cache/block/render',
  190. 'route' => 'route',
  191. 'controller' => 'controller',
  192. 'action' => 'action',
  193. 'uri' => 'uri',
  194. 'expectedResult' => '~"originalRequest":{"route":"route","controller":"controller","action":"action","uri":"uri"}~'
  195. ],
  196. ];
  197. //@codingStandardsIgnoreEnd
  198. }
  199. }