Skip to content

Commit d87d202

Browse files
author
Roger
committed
update unit test
1 parent b4439e0 commit d87d202

File tree

3 files changed

+132
-2
lines changed

3 files changed

+132
-2
lines changed

app/code/Magento/PageCache/Test/Unit/Model/App/Request/Http/IdentifierForSaveTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\PageCache\Test\Unit\Model\App\Request\Http;
99

10+
use Laminas\Stdlib\Parameters;
1011
use Magento\Framework\App\Http\Context;
1112
use Magento\Framework\App\Request\Http as HttpRequest;
1213
use Magento\Framework\Serialize\Serializer\Json;
@@ -41,6 +42,9 @@ class IdentifierForSaveTest extends TestCase
4142
*/
4243
private mixed $serializerMock;
4344

45+
/** @var Parameters|MockObject */
46+
private $fileParams;
47+
4448
/**
4549
* @inheritdoc
4650
*/
@@ -63,6 +67,7 @@ function ($value) {
6367
return json_encode($value);
6468
}
6569
);
70+
$this->fileParams = $this->createMock(Parameters::class);
6671

6772
$this->model = new IdentifierForSave(
6873
$this->requestMock,
@@ -87,6 +92,54 @@ public function testGetValue(): void
8792
->method('getUriString')
8893
->willReturn('http://example.com/path1/');
8994

95+
$this->requestMock->expects($this->any())
96+
->method('getQuery')
97+
->willReturn($this->fileParams);
98+
99+
$this->fileParams->expects($this->any())
100+
->method('toArray')
101+
->willReturn([]);
102+
103+
$this->contextMock->expects($this->any())
104+
->method('getVaryString')
105+
->willReturn(self::VARY);
106+
107+
$this->assertEquals(
108+
sha1(
109+
json_encode(
110+
[
111+
true,
112+
'http://example.com/path1/',
113+
'',
114+
self::VARY
115+
]
116+
)
117+
),
118+
$this->model->getValue()
119+
);
120+
}
121+
122+
public function testGetValueWithQuery(): void
123+
{
124+
$this->requestMock->expects($this->any())
125+
->method('isSecure')
126+
->willReturn(true);
127+
128+
$this->requestMock->expects($this->any())
129+
->method('getUriString')
130+
->willReturn('http://example.com/path1/?b=2&a=1');
131+
132+
$this->requestMock->expects($this->any())
133+
->method('getQuery')
134+
->willReturn($this->fileParams);
135+
136+
$this->fileParams->expects($this->any())
137+
->method('toArray')
138+
->willReturn([
139+
'b' => 2,
140+
'a' => 1,
141+
]);
142+
90143
$this->contextMock->expects($this->any())
91144
->method('getVaryString')
92145
->willReturn(self::VARY);
@@ -97,6 +150,7 @@ public function testGetValue(): void
97150
[
98151
true,
99152
'http://example.com/path1/',
153+
'a=1&b=2',
100154
self::VARY
101155
]
102156
)

lib/internal/Magento/Framework/App/PageCache/Identifier.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function getValue()
7070
*/
7171
private function reconstructUrl($url)
7272
{
73-
$baseUrl = strtok($url, '?');
73+
$baseUrl = strtok((string)$url, '?');
7474
$query = $this->request->getQuery()->toArray();
7575
if (!empty($query)) {
7676
ksort($query);

lib/internal/Magento/Framework/App/Test/Unit/PageCache/IdentifierTest.php

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\Framework\App\Test\Unit\PageCache;
99

10+
use Laminas\Stdlib\Parameters;
1011
use Magento\Framework\App\Http\Context;
1112
use Magento\Framework\App\PageCache\Identifier;
1213
use Magento\Framework\App\Request\Http as HttpRequest;
@@ -20,7 +21,7 @@ class IdentifierTest extends TestCase
2021
/**
2122
* Test value for cache vary string
2223
*/
23-
const VARY = '123';
24+
private const VARY = '123';
2425

2526
/**
2627
* @var ObjectManager
@@ -47,6 +48,9 @@ class IdentifierTest extends TestCase
4748
*/
4849
private $serializerMock;
4950

51+
/** @var Parameters|MockObject */
52+
private $fileParams;
53+
5054
/**
5155
* @inheritdoc
5256
*/
@@ -72,6 +76,7 @@ function ($value) {
7276
return json_encode($value);
7377
}
7478
);
79+
$this->fileParams = $this->createMock(Parameters::class);
7580

7681
$this->model = $this->objectManager->getObject(
7782
Identifier::class,
@@ -95,6 +100,12 @@ public function testSecureDifferentiator(): void
95100
$this->requestMock->method('getUriString')
96101
->willReturn('http://example.com/path/');
97102
$this->contextMock->method('getVaryString')->willReturn(self::VARY);
103+
$this->requestMock->expects($this->any())
104+
->method('getQuery')
105+
->willReturn($this->fileParams);
106+
$this->fileParams->expects($this->any())
107+
->method('toArray')
108+
->willReturn([]);
98109

99110
$valueWithSecureRequest = $this->model->getValue();
100111
$valueWithInsecureRequest = $this->model->getValue();
@@ -111,6 +122,12 @@ public function testDomainDifferentiator(): void
111122
->method('getUriString')
112123
->willReturnOnConsecutiveCalls('http://example.com/path/', 'http://example.net/path/');
113124
$this->contextMock->method('getVaryString')->willReturn(self::VARY);
125+
$this->requestMock->expects($this->any())
126+
->method('getQuery')
127+
->willReturn($this->fileParams);
128+
$this->fileParams->expects($this->any())
129+
->method('toArray')
130+
->willReturn([]);
114131

115132
$valueDomain1 = $this->model->getValue();
116133
$valueDomain2 = $this->model->getValue();
@@ -127,6 +144,12 @@ public function testPathDifferentiator(): void
127144
->method('getUriString')
128145
->willReturnOnConsecutiveCalls('http://example.com/path/', 'http://example.com/path1/');
129146
$this->contextMock->method('getVaryString')->willReturn(self::VARY);
147+
$this->requestMock->expects($this->any())
148+
->method('getQuery')
149+
->willReturn($this->fileParams);
150+
$this->fileParams->expects($this->any())
151+
->method('toArray')
152+
->willReturn([]);
130153

131154
$valuePath1 = $this->model->getValue();
132155
$valuePath2 = $this->model->getValue();
@@ -143,6 +166,12 @@ public function testVaryStringSource($cookieExists): void
143166
{
144167
$this->requestMock->method('get')->willReturn($cookieExists ? 'vary-string-from-cookie' : null);
145168
$this->contextMock->expects($cookieExists ? $this->never() : $this->once())->method('getVaryString');
169+
$this->requestMock->expects($this->any())
170+
->method('getQuery')
171+
->willReturn($this->fileParams);
172+
$this->fileParams->expects($this->any())
173+
->method('toArray')
174+
->willReturn([]);
146175
$this->model->getValue();
147176
}
148177

@@ -173,12 +202,59 @@ public function testGetValue(): void
173202
->method('getVaryString')
174203
->willReturn(self::VARY);
175204

205+
$this->requestMock->expects($this->any())
206+
->method('getQuery')
207+
->willReturn($this->fileParams);
208+
$this->fileParams->expects($this->any())
209+
->method('toArray')
210+
->willReturn([]);
211+
212+
$this->assertEquals(
213+
sha1(
214+
json_encode(
215+
[
216+
true,
217+
'http://example.com/path1/',
218+
'',
219+
self::VARY
220+
]
221+
)
222+
),
223+
$this->model->getValue()
224+
);
225+
}
226+
227+
public function testGetValueWithQuery(): void
228+
{
229+
$this->requestMock->expects($this->any())
230+
->method('isSecure')
231+
->willReturn(true);
232+
233+
$this->requestMock->expects($this->any())
234+
->method('getUriString')
235+
->willReturn('http://example.com/path1/?b=2&a=1');
236+
237+
$this->contextMock->expects($this->any())
238+
->method('getVaryString')
239+
->willReturn(self::VARY);
240+
241+
$this->requestMock->expects($this->any())
242+
->method('getQuery')
243+
->willReturn($this->fileParams);
244+
$this->fileParams->expects($this->any())
245+
->method('toArray')
246+
->willReturn([
247+
'b' => 2,
248+
'a' => 1,
249+
]);
250+
176251
$this->assertEquals(
177252
sha1(
178253
json_encode(
179254
[
180255
true,
181256
'http://example.com/path1/',
257+
'a=1&b=2',
182258
self::VARY
183259
]
184260
)

0 commit comments

Comments
 (0)