Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
HttpTest.php
Go to the documentation of this file.
1 <?php
8 
13 
17 class HttpTest extends \PHPUnit\Framework\TestCase
18 {
22  protected $model;
23 
27  protected $cookieManagerMock;
28 
33 
37  protected $contextMock;
38 
42  protected $dateTimeMock;
43 
47  protected $requestMock;
48 
52  protected $objectManager;
53 
57  private $sessionConfigMock;
58 
62  private $cookieLifeTime = 3600;
63 
67  protected function setUp()
68  {
69  $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
70  $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
71  ->disableOriginalConstructor()
72  ->getMock();
73  $this->cookieMetadataFactoryMock = $this->getMockBuilder(
74  \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory::class
75  )->disableOriginalConstructor()->getMock();
76  $this->cookieManagerMock = $this->createMock(\Magento\Framework\Stdlib\CookieManagerInterface::class);
77  $this->contextMock = $this->getMockBuilder(
78  \Magento\Framework\App\Http\Context::class
79  )->disableOriginalConstructor()
80  ->getMock();
81 
82  $this->dateTimeMock = $this->getMockBuilder(\Magento\Framework\Stdlib\DateTime::class)
83  ->disableOriginalConstructor()
84  ->getMock();
85 
86  $this->sessionConfigMock = $this->getMockBuilder(ConfigInterface::class)
87  ->disableOriginalConstructor()
88  ->getMockForAbstractClass();
89 
90  $this->model = $this->objectManager->getObject(
91  \Magento\Framework\App\Response\Http::class,
92  [
93  'request' => $this->requestMock,
94  'cookieManager' => $this->cookieManagerMock,
95  'cookieMetadataFactory' => $this->cookieMetadataFactoryMock,
96  'context' => $this->contextMock,
97  'dateTime' => $this->dateTimeMock,
98  'sessionConfig' => $this->sessionConfigMock,
99  ]
100  );
101  $this->model->headersSentThrowsException = false;
102  $this->model->setHeader('Name', 'Value');
103  }
104 
108  protected function tearDown()
109  {
110  unset($this->model);
112  $objectManagerMock = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
114  }
115 
116  public function testSendVary()
117  {
118  $expectedCookieName = Http::COOKIE_VARY_STRING;
119  $expectedCookieValue = 'SHA1 Serialized String';
120  $sensitiveCookieMetadataMock = $this->getMockBuilder(
121  \Magento\Framework\Stdlib\Cookie\SensitiveCookieMetadata::class
122  )
123  ->disableOriginalConstructor()
124  ->getMock();
125  $sensitiveCookieMetadataMock->expects($this->once())
126  ->method('setPath')
127  ->with('/')
128  ->will($this->returnSelf());
129 
130  $this->contextMock->expects($this->once())
131  ->method('getVaryString')
132  ->will($this->returnValue($expectedCookieValue));
133 
134  $this->sessionConfigMock->expects($this->once())
135  ->method('getCookieLifetime')
136  ->willReturn($this->cookieLifeTime);
137 
138  $this->cookieMetadataFactoryMock->expects($this->once())
139  ->method('createSensitiveCookieMetadata')
140  ->with([CookieMetadata::KEY_DURATION => $this->cookieLifeTime])
141  ->willReturn($sensitiveCookieMetadataMock);
142 
143  $this->cookieManagerMock->expects($this->once())
144  ->method('setSensitiveCookie')
145  ->with($expectedCookieName, $expectedCookieValue, $sensitiveCookieMetadataMock);
146  $this->model->sendVary();
147  }
148 
150  {
151  $expectedCookieName = Http::COOKIE_VARY_STRING;
152  $cookieMetadataMock = $this->createMock(\Magento\Framework\Stdlib\Cookie\CookieMetadata::class);
153  $cookieMetadataMock->expects($this->once())
154  ->method('setPath')
155  ->with('/')
156  ->will($this->returnSelf());
157  $this->contextMock->expects($this->once())
158  ->method('getVaryString')
159  ->willReturn(null);
160  $this->cookieMetadataFactoryMock->expects($this->once())
161  ->method('createSensitiveCookieMetadata')
162  ->willReturn($cookieMetadataMock);
163  $this->cookieManagerMock->expects($this->once())
164  ->method('deleteCookie')
165  ->with($expectedCookieName, $cookieMetadataMock);
166  $this->requestMock->expects($this->once())
167  ->method('get')
168  ->willReturn('value');
169  $this->model->sendVary();
170  }
171 
172  public function testSendVaryEmptyData()
173  {
174  $this->contextMock->expects($this->once())
175  ->method('getVaryString')
176  ->willReturn(null);
177  $this->cookieMetadataFactoryMock->expects($this->never())
178  ->method('createSensitiveCookieMetadata');
179  $this->requestMock->expects($this->once())
180  ->method('get')
181  ->willReturn(null);
182  $this->model->sendVary();
183  }
184 
188  public function testSetPublicHeaders()
189  {
190  $ttl = 120;
191  $timestamp = 1000000;
192  $pragma = 'cache';
193  $cacheControl = 'max-age=' . $ttl . ', public, s-maxage=' . $ttl;
194  $expiresResult ='Thu, 01 Jan 1970 00:00:00 GMT';
195 
196  $this->dateTimeMock->expects($this->once())
197  ->method('strToTime')
198  ->with('+' . $ttl . ' seconds')
199  ->willReturn($timestamp);
200  $this->dateTimeMock->expects($this->once())
201  ->method('gmDate')
202  ->with(Http::EXPIRATION_TIMESTAMP_FORMAT, $timestamp)
203  ->willReturn($expiresResult);
204 
205  $this->model->setPublicHeaders($ttl);
206  $this->assertEquals($pragma, $this->model->getHeader('Pragma')->getFieldValue());
207  $this->assertEquals($cacheControl, $this->model->getHeader('Cache-Control')->getFieldValue());
208  $this->assertSame($expiresResult, $this->model->getHeader('Expires')->getFieldValue());
209  }
210 
215  {
216  $this->expectException('InvalidArgumentException');
217  $this->expectExceptionMessage('Time to live is a mandatory parameter for set public headers');
218  $this->model->setPublicHeaders(null);
219  }
220 
224  public function testSetPrivateHeaders()
225  {
226  $ttl = 120;
227  $timestamp = 1000000;
228  $pragma = 'cache';
229  $cacheControl = 'max-age=' . $ttl . ', private';
230  $expiresResult ='Thu, 01 Jan 1970 00:00:00 GMT';
231 
232  $this->dateTimeMock->expects($this->once())
233  ->method('strToTime')
234  ->with('+' . $ttl . ' seconds')
235  ->willReturn($timestamp);
236  $this->dateTimeMock->expects($this->once())
237  ->method('gmDate')
238  ->with(Http::EXPIRATION_TIMESTAMP_FORMAT, $timestamp)
239  ->willReturn($expiresResult);
240 
241  $this->model->setPrivateHeaders($ttl);
242  $this->assertEquals($pragma, $this->model->getHeader('Pragma')->getFieldValue());
243  $this->assertEquals($cacheControl, $this->model->getHeader('Cache-Control')->getFieldValue());
244  $this->assertEquals($expiresResult, $this->model->getHeader('Expires')->getFieldValue());
245  }
246 
251  {
252  $this->expectException('InvalidArgumentException');
253  $this->expectExceptionMessage('Time to live is a mandatory parameter for set private headers');
254  $this->model->setPrivateHeaders(null);
255  }
256 
260  public function testSetNoCacheHeaders()
261  {
262  $timestamp = 1000000;
263  $pragma = 'no-cache';
264  $cacheControl = 'max-age=0, must-revalidate, no-cache, no-store';
265  $expiresResult ='Thu, 01 Jan 1970 00:00:00 GMT';
266 
267  $this->dateTimeMock->expects($this->once())
268  ->method('strToTime')
269  ->with('-1 year')
270  ->willReturn($timestamp);
271  $this->dateTimeMock->expects($this->once())
272  ->method('gmDate')
273  ->with(Http::EXPIRATION_TIMESTAMP_FORMAT, $timestamp)
274  ->willReturn($expiresResult);
275 
276  $this->model->setNoCacheHeaders();
277  $this->assertEquals($pragma, $this->model->getHeader('Pragma')->getFieldValue());
278  $this->assertEquals($cacheControl, $this->model->getHeader('Cache-Control')->getFieldValue());
279  $this->assertEquals($expiresResult, $this->model->getHeader('Expires')->getFieldValue());
280  }
281 
285  public function testRepresentJson()
286  {
287  $this->model->setHeader('Content-Type', 'text/javascript');
288  $this->model->representJson('json_string');
289  $this->assertEquals('application/json', $this->model->getHeader('Content-Type')->getFieldValue());
290  $this->assertEquals('json_string', $this->model->getBody('default'));
291  }
292 
298  public function testWakeUpWithException()
299  {
300  /* ensure that the test preconditions are met */
301  $objectManagerClass = new \ReflectionClass(\Magento\Framework\App\ObjectManager::class);
302  $instanceProperty = $objectManagerClass->getProperty('_instance');
303  $instanceProperty->setAccessible(true);
304  $instanceProperty->setValue(null);
305 
306  $this->model->__wakeup();
307  $this->assertNull($this->cookieMetadataFactoryMock);
308  $this->assertNull($this->cookieManagerMock);
309  }
310 
316  public function testWakeUpWith()
317  {
318  $objectManagerMock = $this->createMock(\Magento\Framework\App\ObjectManager::class);
319  $objectManagerMock->expects($this->once())
320  ->method('create')
321  ->with(\Magento\Framework\Stdlib\CookieManagerInterface::class)
322  ->will($this->returnValue($this->cookieManagerMock));
323  $objectManagerMock->expects($this->at(1))
324  ->method('get')
325  ->with(\Magento\Framework\Stdlib\Cookie\CookieMetadataFactory::class)
326  ->will($this->returnValue($this->cookieMetadataFactoryMock));
327 
329  $this->model->__wakeup();
330  }
331 
332  public function testSetXFrameOptions()
333  {
334  $value = 'DENY';
335  $this->model->setXFrameOptions($value);
336  $this->assertSame($value, $this->model->getHeader(Http::HEADER_X_FRAME_OPT)->getFieldValue());
337  }
338 }
static setInstance(\Magento\Framework\ObjectManagerInterface $objectManager)
$value
Definition: gender.phtml:16