IdentifierTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App\Test\Unit\PageCache;
  7. use Magento\Framework\App\Http\Context;
  8. use Magento\Framework\App\PageCache\Identifier;
  9. use Magento\Framework\App\Response\Http;
  10. use Magento\Framework\App\Request\Http as HttpRequest;
  11. use Magento\Framework\Serialize\Serializer\Json;
  12. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  13. class IdentifierTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * Test value for cache vary string
  17. */
  18. const VARY = '123';
  19. /**
  20. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  21. */
  22. private $objectManager;
  23. /**
  24. * @var Context|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. private $contextMock;
  27. /**
  28. * @var HttpRequest|\PHPUnit_Framework_MockObject_MockObject
  29. */
  30. private $requestMock;
  31. /**
  32. * @var Identifier
  33. */
  34. private $model;
  35. /**
  36. * @var Json|\PHPUnit_Framework_MockObject_MockObject
  37. */
  38. private $serializerMock;
  39. /**
  40. * @return \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  41. */
  42. protected function setUp()
  43. {
  44. $this->objectManager = new ObjectManager($this);
  45. $this->contextMock = $this->getMockBuilder(Context::class)
  46. ->disableOriginalConstructor()
  47. ->getMock();
  48. $this->requestMock = $this->getMockBuilder(HttpRequest::class)
  49. ->disableOriginalConstructor()
  50. ->getMock();
  51. $this->serializerMock = $this->getMockBuilder(Json::class)
  52. ->setMethods(['serialize'])
  53. ->disableOriginalConstructor()
  54. ->getMock();
  55. $this->serializerMock->expects($this->any())
  56. ->method('serialize')
  57. ->will(
  58. $this->returnCallback(
  59. function ($value) {
  60. return json_encode($value);
  61. }
  62. )
  63. );
  64. $this->model = $this->objectManager->getObject(
  65. Identifier::class,
  66. [
  67. 'request' => $this->requestMock,
  68. 'context' => $this->contextMock,
  69. 'serializer' => $this->serializerMock,
  70. ]
  71. );
  72. return parent::setUp();
  73. }
  74. public function testSecureDifferentiator()
  75. {
  76. $this->requestMock->expects($this->at(0))
  77. ->method('isSecure')
  78. ->willReturn(true);
  79. $this->requestMock->expects($this->at(3))
  80. ->method('isSecure')
  81. ->willReturn(false);
  82. $this->requestMock->method('getUriString')
  83. ->willReturn('http://example.com/path/');
  84. $this->contextMock->method('getVaryString')->willReturn(self::VARY);
  85. $valueWithSecureRequest = $this->model->getValue();
  86. $valueWithInsecureRequest = $this->model->getValue();
  87. $this->assertNotEquals($valueWithSecureRequest, $valueWithInsecureRequest);
  88. }
  89. public function testDomainDifferentiator()
  90. {
  91. $this->requestMock->method('isSecure')->willReturn(true);
  92. $this->requestMock->expects($this->at(1))
  93. ->method('getUriString')
  94. ->willReturn('http://example.com/path/');
  95. $this->requestMock->expects($this->at(4))
  96. ->method('getUriString')
  97. ->willReturn('http://example.net/path/');
  98. $this->contextMock->method('getVaryString')->willReturn(self::VARY);
  99. $valueDomain1 = $this->model->getValue();
  100. $valueDomain2 = $this->model->getValue();
  101. $this->assertNotEquals($valueDomain1, $valueDomain2);
  102. }
  103. public function testPathDifferentiator()
  104. {
  105. $this->requestMock->method('isSecure')->willReturn(true);
  106. $this->requestMock->expects($this->at(1))
  107. ->method('getUriString')
  108. ->willReturn('http://example.com/path/');
  109. $this->requestMock->expects($this->at(4))
  110. ->method('getUriString')
  111. ->willReturn('http://example.com/path1/');
  112. $this->contextMock->method('getVaryString')->willReturn(self::VARY);
  113. $valuePath1 = $this->model->getValue();
  114. $valuePath2 = $this->model->getValue();
  115. $this->assertNotEquals($valuePath1, $valuePath2);
  116. }
  117. /**
  118. * @param $cookieExists
  119. *
  120. * @dataProvider trueFalseDataProvider
  121. */
  122. public function testVaryStringSource($cookieExists)
  123. {
  124. $this->requestMock->method('get')->willReturn($cookieExists ? 'vary-string-from-cookie' : null);
  125. $this->contextMock->expects($cookieExists ? $this->never() : $this->once())->method('getVaryString');
  126. $this->model->getValue();
  127. }
  128. /**
  129. * @return array
  130. */
  131. public function trueFalseDataProvider()
  132. {
  133. return [[true], [false]];
  134. }
  135. /**
  136. * Test get identifier value
  137. */
  138. public function testGetValue()
  139. {
  140. $this->requestMock->expects($this->any())
  141. ->method('isSecure')
  142. ->will($this->returnValue(true));
  143. $this->requestMock->expects($this->any())
  144. ->method('getUriString')
  145. ->willReturn('http://example.com/path1/');
  146. $this->contextMock->expects($this->any())
  147. ->method('getVaryString')
  148. ->will($this->returnValue(self::VARY));
  149. $this->assertEquals(
  150. sha1(
  151. json_encode(
  152. [
  153. true,
  154. 'http://example.com/path1/',
  155. self::VARY
  156. ]
  157. )
  158. ),
  159. $this->model->getValue()
  160. );
  161. }
  162. }