PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/Magento/PageCache/Test/Unit/Block/JavascriptTest.php

https://gitlab.com/crazybutterfly815/magento2
PHP | 217 lines | 155 code | 18 blank | 44 comment | 0 complexity | 4d16c7f8cb66299e5cbb9a96a559fa07 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. 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->getMock(
  43. \Magento\Framework\App\RequestInterface::class,
  44. [
  45. 'getRouteName',
  46. 'getControllerName',
  47. 'getModuleName',
  48. 'getActionName',
  49. 'getRequestUri',
  50. 'getParam',
  51. 'setParams',
  52. 'getParams',
  53. 'setModuleName',
  54. 'isSecure',
  55. 'setActionName',
  56. 'setRequestUri',
  57. 'getCookie'
  58. ],
  59. [],
  60. '',
  61. false
  62. );
  63. $this->layoutMock = $this->getMockBuilder(\Magento\Framework\View\LayoutInterface::class)
  64. ->disableOriginalConstructor()
  65. ->getMock();
  66. $this->layoutUpdateMock = $this->getMockBuilder(\Magento\Framework\View\Layout\ProcessorInterface::class)
  67. ->disableOriginalConstructor()
  68. ->getMock();
  69. $this->urlBuilderMock = $this->getMockBuilder(\Magento\Framework\UrlInterface::class)
  70. ->disableOriginalConstructor()
  71. ->getMock();
  72. $this->contextMock->expects($this->any())
  73. ->method('getRequest')
  74. ->willReturn($this->requestMock);
  75. $this->contextMock->expects($this->any())
  76. ->method('getLayout')
  77. ->willReturn($this->layoutMock);
  78. $this->contextMock->expects($this->any())
  79. ->method('getUrlBuilder')
  80. ->willReturn($this->urlBuilderMock);
  81. $this->layoutMock->expects($this->any())
  82. ->method('getUpdate')
  83. ->willReturn($this->layoutUpdateMock);
  84. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  85. $this->blockJavascript = $objectManager->getObject(
  86. \Magento\PageCache\Block\Javascript::class,
  87. [
  88. 'context' => $this->contextMock
  89. ]
  90. );
  91. }
  92. /**
  93. * @covers \Magento\PageCache\Block\Javascript::getScriptOptions
  94. * @param bool $isSecure
  95. * @param string $url
  96. * @param string $expectedResult
  97. * @dataProvider getScriptOptionsDataProvider
  98. */
  99. public function testGetScriptOptions($isSecure, $url, $expectedResult)
  100. {
  101. $handles = [
  102. 'some',
  103. 'handles',
  104. 'here'
  105. ];
  106. $this->requestMock->expects($this->once())
  107. ->method('isSecure')
  108. ->willReturn($isSecure);
  109. $this->requestMock->expects($this->once())
  110. ->method('getRouteName')
  111. ->will($this->returnValue('route'));
  112. $this->requestMock->expects($this->once())
  113. ->method('getControllerName')
  114. ->will($this->returnValue('controller'));
  115. $this->requestMock->expects($this->once())
  116. ->method('getActionName')
  117. ->will($this->returnValue('action'));
  118. $this->requestMock->expects($this->once())
  119. ->method('getRequestUri')
  120. ->will($this->returnValue('uri'));
  121. $this->urlBuilderMock->expects($this->once())
  122. ->method('getUrl')
  123. ->willReturn($url);
  124. $this->layoutUpdateMock->expects($this->once())
  125. ->method('getHandles')
  126. ->willReturn($handles);
  127. $this->assertRegExp($expectedResult, $this->blockJavascript->getScriptOptions());
  128. }
  129. public function getScriptOptionsDataProvider()
  130. {
  131. return [
  132. 'http' => [
  133. 'isSecure' => false,
  134. 'url' => 'http://some-name.com/page_cache/block/render',
  135. 'expectedResult' => '~http:\\\\/\\\\/some-name\\.com.+\\["some","handles","here"\\]~'
  136. ],
  137. 'https' => [
  138. 'isSecure' => true,
  139. 'url' => 'https://some-name.com/page_cache/block/render',
  140. 'expectedResult' => '~https:\\\\/\\\\/some-name\\.com.+\\["some","handles","here"\\]~'
  141. ]
  142. ];
  143. }
  144. /**
  145. * @covers \Magento\PageCache\Block\Javascript::getScriptOptions
  146. * @param string $url
  147. * @param string $route
  148. * @param string $controller
  149. * @param string $action
  150. * @param string $uri
  151. * @param string $expectedResult
  152. * @dataProvider getScriptOptionsPrivateContentDataProvider
  153. */
  154. public function testGetScriptOptionsPrivateContent($url, $route, $controller, $action, $uri, $expectedResult)
  155. {
  156. $handles = [
  157. 'some',
  158. 'handles',
  159. 'here'
  160. ];
  161. $this->requestMock->expects($this->once())
  162. ->method('isSecure')
  163. ->willReturn(false);
  164. $this->requestMock->expects($this->once())
  165. ->method('getRouteName')
  166. ->will($this->returnValue($route));
  167. $this->requestMock->expects($this->once())
  168. ->method('getControllerName')
  169. ->will($this->returnValue($controller));
  170. $this->requestMock->expects($this->once())
  171. ->method('getActionName')
  172. ->will($this->returnValue($action));
  173. $this->requestMock->expects($this->once())
  174. ->method('getRequestUri')
  175. ->will($this->returnValue($uri));
  176. $this->urlBuilderMock->expects($this->once())
  177. ->method('getUrl')
  178. ->willReturn($url);
  179. $this->layoutUpdateMock->expects($this->once())
  180. ->method('getHandles')
  181. ->willReturn($handles);
  182. $this->assertRegExp($expectedResult, $this->blockJavascript->getScriptOptions());
  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. }