|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\PageCache\Test\Unit\Model\App\Request\Http; |
| 9 | + |
| 10 | +use Magento\Framework\App\Http\Context; |
| 11 | +use Magento\Framework\App\PageCache\Identifier; |
| 12 | +use Magento\Framework\App\Request\Http as HttpRequest; |
| 13 | +use Magento\Framework\Serialize\Serializer\Json; |
| 14 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 15 | +use Magento\PageCache\Model\App\Request\Http\IdentifierStoreReader; |
| 16 | +use PHPUnit\Framework\MockObject\MockObject; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | + |
| 19 | +class IdentifierTest extends TestCase |
| 20 | +{ |
| 21 | + /** |
| 22 | + * Test value for cache vary string |
| 23 | + */ |
| 24 | + const VARY = '123'; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var ObjectManager |
| 28 | + */ |
| 29 | + private $objectManager; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var Context|MockObject |
| 33 | + */ |
| 34 | + private $contextMock; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var HttpRequest|MockObject |
| 38 | + */ |
| 39 | + private $requestMock; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var Identifier |
| 43 | + */ |
| 44 | + private $model; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var Json|MockObject |
| 48 | + */ |
| 49 | + private $serializerMock; |
| 50 | + /** |
| 51 | + * @var IdentifierStoreReader|MockObject |
| 52 | + */ |
| 53 | + private $identifierStoreReader; |
| 54 | + |
| 55 | + /** |
| 56 | + * @inheritdoc |
| 57 | + */ |
| 58 | + protected function setUp(): void |
| 59 | + { |
| 60 | + $this->objectManager = new ObjectManager($this); |
| 61 | + $this->contextMock = $this->getMockBuilder(Context::class) |
| 62 | + ->disableOriginalConstructor() |
| 63 | + ->getMock(); |
| 64 | + |
| 65 | + $this->requestMock = $this->getMockBuilder(HttpRequest::class) |
| 66 | + ->disableOriginalConstructor() |
| 67 | + ->getMock(); |
| 68 | + |
| 69 | + $this->serializerMock = $this->getMockBuilder(Json::class) |
| 70 | + ->onlyMethods(['serialize']) |
| 71 | + ->disableOriginalConstructor() |
| 72 | + ->getMock(); |
| 73 | + $this->serializerMock->expects($this->any()) |
| 74 | + ->method('serialize') |
| 75 | + ->willReturnCallback( |
| 76 | + function ($value) { |
| 77 | + return json_encode($value); |
| 78 | + } |
| 79 | + ); |
| 80 | + |
| 81 | + $this->identifierStoreReader = $this->getMockBuilder(IdentifierStoreReader::class) |
| 82 | + ->onlyMethods(['getPageTagsWithStoreCacheTags']) |
| 83 | + ->disableOriginalConstructor() |
| 84 | + ->getMock(); |
| 85 | + |
| 86 | + |
| 87 | + $this->model = $this->objectManager->getObject( |
| 88 | + Identifier::class, |
| 89 | + [ |
| 90 | + 'request' => $this->requestMock, |
| 91 | + 'context' => $this->contextMock, |
| 92 | + 'serializer' => $this->serializerMock, |
| 93 | + 'identifierStoreReader' => $this->identifierStoreReader |
| 94 | + ] |
| 95 | + ); |
| 96 | + parent::setUp(); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @return void |
| 101 | + */ |
| 102 | + public function testSecureDifferentiator(): void |
| 103 | + { |
| 104 | + $this->requestMock |
| 105 | + ->method('isSecure') |
| 106 | + ->willReturnOnConsecutiveCalls(true, false); |
| 107 | + $this->requestMock->method('getUriString') |
| 108 | + ->willReturn('http://example.com/path/'); |
| 109 | + $this->contextMock->method('getVaryString')->willReturn(self::VARY); |
| 110 | + |
| 111 | + $this->identifierStoreReader->method('getPageTagsWithStoreCacheTags')->willReturnCallback( |
| 112 | + function ($value) { |
| 113 | + return $value; |
| 114 | + } |
| 115 | + ); |
| 116 | + |
| 117 | + $valueWithSecureRequest = $this->model->getValue(); |
| 118 | + $valueWithInsecureRequest = $this->model->getValue(); |
| 119 | + $this->assertNotEquals($valueWithSecureRequest, $valueWithInsecureRequest); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * @return void |
| 124 | + */ |
| 125 | + public function testDomainDifferentiator(): void |
| 126 | + { |
| 127 | + $this->requestMock->method('isSecure')->willReturn(true); |
| 128 | + $this->requestMock |
| 129 | + ->method('getUriString') |
| 130 | + ->willReturnOnConsecutiveCalls('http://example.com/path/', 'http://example.net/path/'); |
| 131 | + $this->contextMock->method('getVaryString')->willReturn(self::VARY); |
| 132 | + |
| 133 | + $this->identifierStoreReader->method('getPageTagsWithStoreCacheTags')->willReturnCallback( |
| 134 | + function ($value) { |
| 135 | + return $value; |
| 136 | + } |
| 137 | + ); |
| 138 | + |
| 139 | + $valueDomain1 = $this->model->getValue(); |
| 140 | + $valueDomain2 = $this->model->getValue(); |
| 141 | + $this->assertNotEquals($valueDomain1, $valueDomain2); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * @return void |
| 146 | + */ |
| 147 | + public function testPathDifferentiator(): void |
| 148 | + { |
| 149 | + $this->requestMock->method('isSecure')->willReturn(true); |
| 150 | + $this->requestMock |
| 151 | + ->method('getUriString') |
| 152 | + ->willReturnOnConsecutiveCalls('http://example.com/path/', 'http://example.com/path1/'); |
| 153 | + $this->contextMock->method('getVaryString')->willReturn(self::VARY); |
| 154 | + |
| 155 | + $this->identifierStoreReader->method('getPageTagsWithStoreCacheTags')->willReturnCallback( |
| 156 | + function ($value) { |
| 157 | + return $value; |
| 158 | + } |
| 159 | + ); |
| 160 | + |
| 161 | + $valuePath1 = $this->model->getValue(); |
| 162 | + $valuePath2 = $this->model->getValue(); |
| 163 | + $this->assertNotEquals($valuePath1, $valuePath2); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * @param $cookieExists |
| 168 | + * |
| 169 | + * @return void |
| 170 | + * @dataProvider trueFalseDataProvider |
| 171 | + */ |
| 172 | + public function testVaryStringSource($cookieExists): void |
| 173 | + { |
| 174 | + $this->requestMock->method('get')->willReturn($cookieExists ? 'vary-string-from-cookie' : null); |
| 175 | + $this->contextMock->expects($cookieExists ? $this->never() : $this->once())->method('getVaryString'); |
| 176 | + $this->model->getValue(); |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * @return array |
| 181 | + */ |
| 182 | + public function trueFalseDataProvider(): array |
| 183 | + { |
| 184 | + return [[true], [false]]; |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Test get identifier value. |
| 189 | + * |
| 190 | + * @return void |
| 191 | + */ |
| 192 | + public function testGetValue(): void |
| 193 | + { |
| 194 | + $this->requestMock->expects($this->any()) |
| 195 | + ->method('isSecure') |
| 196 | + ->willReturn(true); |
| 197 | + |
| 198 | + $this->requestMock->expects($this->any()) |
| 199 | + ->method('getUriString') |
| 200 | + ->willReturn('http://example.com/path1/'); |
| 201 | + |
| 202 | + $this->contextMock->expects($this->any()) |
| 203 | + ->method('getVaryString') |
| 204 | + ->willReturn(self::VARY); |
| 205 | + |
| 206 | + $this->identifierStoreReader->method('getPageTagsWithStoreCacheTags')->willReturnCallback( |
| 207 | + function ($value) { |
| 208 | + return $value; |
| 209 | + } |
| 210 | + ); |
| 211 | + |
| 212 | + $this->assertEquals( |
| 213 | + sha1( |
| 214 | + json_encode( |
| 215 | + [ |
| 216 | + true, |
| 217 | + 'http://example.com/path1/', |
| 218 | + self::VARY |
| 219 | + ] |
| 220 | + ) |
| 221 | + ), |
| 222 | + $this->model->getValue() |
| 223 | + ); |
| 224 | + } |
| 225 | +} |
0 commit comments