Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
JavascriptTest.php
Go to the documentation of this file.
1 <?php
7 
11 class JavascriptTest extends \PHPUnit\Framework\TestCase
12 {
13  const COOKIE_NAME = 'private_content_version';
14 
18  protected $blockJavascript;
19 
23  protected $contextMock;
24 
28  protected $requestMock;
29 
33  protected $layoutMock;
34 
38  protected $layoutUpdateMock;
39 
43  protected $urlBuilderMock;
44 
45  protected function setUp()
46  {
47  $this->contextMock = $this->getMockBuilder(\Magento\Framework\View\Element\Template\Context::class)
48  ->disableOriginalConstructor()
49  ->getMock();
50  $this->requestMock = $this->createPartialMock(\Magento\Framework\App\RequestInterface::class, [
51  'getRouteName',
52  'getControllerName',
53  'getModuleName',
54  'getActionName',
55  'getRequestUri',
56  'getParam',
57  'setParams',
58  'getParams',
59  'setModuleName',
60  'isSecure',
61  'setActionName',
62  'setRequestUri',
63  'getCookie'
64  ]);
65  $this->layoutMock = $this->getMockBuilder(\Magento\Framework\View\LayoutInterface::class)
66  ->disableOriginalConstructor()
67  ->getMock();
68  $this->layoutUpdateMock = $this->getMockBuilder(\Magento\Framework\View\Layout\ProcessorInterface::class)
69  ->disableOriginalConstructor()
70  ->getMock();
71  $this->urlBuilderMock = $this->getMockBuilder(\Magento\Framework\UrlInterface::class)
72  ->disableOriginalConstructor()
73  ->getMock();
74  $this->contextMock->expects($this->any())
75  ->method('getRequest')
76  ->willReturn($this->requestMock);
77  $this->contextMock->expects($this->any())
78  ->method('getLayout')
79  ->willReturn($this->layoutMock);
80  $this->contextMock->expects($this->any())
81  ->method('getUrlBuilder')
82  ->willReturn($this->urlBuilderMock);
83  $this->layoutMock->expects($this->any())
84  ->method('getUpdate')
85  ->willReturn($this->layoutUpdateMock);
86  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
87  $this->blockJavascript = $objectManager->getObject(
88  \Magento\PageCache\Block\Javascript::class,
89  [
90  'context' => $this->contextMock
91  ]
92  );
93  }
94 
102  public function testGetScriptOptions($isSecure, $url, $expectedResult)
103  {
104  $handles = [
105  'some',
106  'handles',
107  'here'
108  ];
109  $this->requestMock->expects($this->once())
110  ->method('isSecure')
111  ->willReturn($isSecure);
112  $this->requestMock->expects($this->once())
113  ->method('getRouteName')
114  ->will($this->returnValue('route'));
115  $this->requestMock->expects($this->once())
116  ->method('getControllerName')
117  ->will($this->returnValue('controller'));
118  $this->requestMock->expects($this->once())
119  ->method('getActionName')
120  ->will($this->returnValue('action'));
121  $this->requestMock->expects($this->once())
122  ->method('getRequestUri')
123  ->will($this->returnValue('uri'));
124  $this->urlBuilderMock->expects($this->once())
125  ->method('getUrl')
126  ->willReturn($url);
127  $this->layoutUpdateMock->expects($this->once())
128  ->method('getHandles')
129  ->willReturn($handles);
130  $this->assertRegExp($expectedResult, $this->blockJavascript->getScriptOptions());
131  }
132 
137  {
138  return [
139  'http' => [
140  'isSecure' => false,
141  'url' => 'http://some-name.com/page_cache/block/render',
142  'expectedResult' => '~http:\\\\/\\\\/some-name\\.com.+\\["some","handles","here"\\]~'
143  ],
144  'https' => [
145  'isSecure' => true,
146  'url' => 'https://some-name.com/page_cache/block/render',
147  'expectedResult' => '~https:\\\\/\\\\/some-name\\.com.+\\["some","handles","here"\\]~'
148  ]
149  ];
150  }
151 
162  public function testGetScriptOptionsPrivateContent($url, $route, $controller, $action, $uri, $expectedResult)
163  {
164  $handles = [
165  'some',
166  'handles',
167  'here'
168  ];
169  $this->requestMock->expects($this->once())
170  ->method('isSecure')
171  ->willReturn(false);
172 
173  $this->requestMock->expects($this->once())
174  ->method('getRouteName')
175  ->will($this->returnValue($route));
176 
177  $this->requestMock->expects($this->once())
178  ->method('getControllerName')
179  ->will($this->returnValue($controller));
180 
181  $this->requestMock->expects($this->once())
182  ->method('getActionName')
183  ->will($this->returnValue($action));
184 
185  $this->requestMock->expects($this->once())
186  ->method('getRequestUri')
187  ->will($this->returnValue($uri));
188 
189  $this->urlBuilderMock->expects($this->once())
190  ->method('getUrl')
191  ->willReturn($url);
192 
193  $this->layoutUpdateMock->expects($this->once())
194  ->method('getHandles')
195  ->willReturn($handles);
196  $this->assertRegExp($expectedResult, $this->blockJavascript->getScriptOptions());
197  }
198 
203  {
204  // @codingStandardsIgnoreStart
205  return [
206  'http' => [
207  'url' => 'http://some-name.com/page_cache/block/render',
208  'route' => 'route',
209  'controller' => 'controller',
210  'action' => 'action',
211  'uri' => 'uri',
212  'expectedResult' => '~"originalRequest":{"route":"route","controller":"controller","action":"action","uri":"uri"}~'
213  ],
214  ];
215  //@codingStandardsIgnoreEnd
216  }
217 }
testGetScriptOptions($isSecure, $url, $expectedResult)
$objectManager
Definition: bootstrap.php:17
testGetScriptOptionsPrivateContent($url, $route, $controller, $action, $uri, $expectedResult)
$controller
Definition: info.phtml:14