Skip to content

Commit 8f36bc3

Browse files
Merge remote-tracking branch '39099/remove_marketing' into 37018-test
2 parents 7546f27 + ebcc806 commit 8f36bc3

File tree

4 files changed

+180
-12
lines changed

4 files changed

+180
-12
lines changed

app/code/Magento/PageCache/Model/App/Request/Http/IdentifierForSave.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2023 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

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

1010
use Magento\Framework\App\Http\Context;
11+
use Magento\Framework\App\ObjectManager;
12+
use Magento\Framework\App\PageCache\Identifier;
13+
use Magento\Framework\App\PageCache\IdentifierInterface;
1114
use Magento\Framework\App\Request\Http;
1215
use Magento\Framework\Serialize\Serializer\Json;
13-
use Magento\Framework\App\PageCache\IdentifierInterface;
1416

1517
/**
1618
* Page unique identifier
@@ -22,13 +24,17 @@ class IdentifierForSave implements IdentifierInterface
2224
* @param Context $context
2325
* @param Json $serializer
2426
* @param IdentifierStoreReader $identifierStoreReader
27+
* @param Identifier|null $identifier
2528
*/
2629
public function __construct(
2730
private Http $request,
2831
private Context $context,
2932
private Json $serializer,
3033
private IdentifierStoreReader $identifierStoreReader,
34+
private ?Identifier $identifier = null
3135
) {
36+
$this->identifier = $identifier ?: ObjectManager::getInstance()
37+
->get(Identifier::class);
3238
}
3339

3440
/**
@@ -38,9 +44,11 @@ public function __construct(
3844
*/
3945
public function getValue()
4046
{
47+
$pattern = $this->identifier->getMarketingParameterPatterns();
48+
$replace = array_fill(0, count($pattern), '');
4149
$data = [
4250
$this->request->isSecure(),
43-
$this->request->getUriString(),
51+
preg_replace($pattern, $replace, (string)$this->request->getUriString()),
4452
$this->context->getVaryString()
4553
];
4654

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

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2023 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

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

1010
use Magento\Framework\App\Http\Context;
11+
use Magento\Framework\App\ObjectManager;
12+
use Magento\Framework\App\PageCache\Identifier;
1113
use Magento\Framework\App\Request\Http as HttpRequest;
14+
use Magento\Framework\ObjectManagerInterface;
1215
use Magento\Framework\Serialize\Serializer\Json;
1316
use Magento\PageCache\Model\App\Request\Http\IdentifierForSave;
1417
use Magento\PageCache\Model\App\Request\Http\IdentifierStoreReader;
@@ -46,6 +49,11 @@ class IdentifierForSaveTest extends TestCase
4649
*/
4750
private $identifierStoreReader;
4851

52+
/**
53+
* @var Identifier
54+
*/
55+
private $identifierMock;
56+
4957
/**
5058
* @inheritdoc
5159
*/
@@ -74,6 +82,16 @@ function ($value) {
7482
->disableOriginalConstructor()
7583
->getMock();
7684

85+
$this->identifierMock = $this->getMockBuilder(Identifier::class)
86+
->disableOriginalConstructor()
87+
->getMock();
88+
89+
$objectManagerMock = $this->getMockForAbstractClass(ObjectManagerInterface::class);
90+
$objectManagerMock->expects($this->once())
91+
->method('get')
92+
->willReturn($this->identifierMock);
93+
ObjectManager::setInstance($objectManagerMock);
94+
7795
$this->model = new IdentifierForSave(
7896
$this->requestMock,
7997
$this->contextMock,
@@ -90,6 +108,10 @@ function ($value) {
90108
*/
91109
public function testGetValue(): void
92110
{
111+
$this->identifierMock->expects($this->once())
112+
->method('getMarketingParameterPatterns')
113+
->willReturn($this->getpattern());
114+
93115
$this->requestMock->expects($this->any())
94116
->method('isSecure')
95117
->willReturn(true);
@@ -121,4 +143,76 @@ function ($value) {
121143
$this->model->getValue()
122144
);
123145
}
146+
147+
/**
148+
* Test get identifier for save value with marketing parameters.
149+
*
150+
* @return void
151+
*/
152+
public function testGetValueWithMarketingParameters(): void
153+
{
154+
$this->identifierMock->expects($this->any())
155+
->method('getMarketingParameterPatterns')
156+
->willReturn($this->getPattern());
157+
158+
$this->requestMock->expects($this->any())
159+
->method('isSecure')
160+
->willReturn(true);
161+
162+
$this->requestMock->expects($this->any())
163+
->method('getUriString')
164+
->willReturn('http://example.com/path1/?abc=123&gclid=456&utm_source=abc');
165+
166+
$this->contextMock->expects($this->any())
167+
->method('getVaryString')
168+
->willReturn(self::VARY);
169+
170+
$this->identifierStoreReader->method('getPageTagsWithStoreCacheTags')->willReturnCallback(
171+
function ($value) {
172+
return $value;
173+
}
174+
);
175+
176+
$this->assertEquals(
177+
sha1(
178+
json_encode(
179+
[
180+
true,
181+
'http://example.com/path1/?abc=123',
182+
self::VARY
183+
]
184+
)
185+
),
186+
$this->model->getValue()
187+
);
188+
}
189+
190+
/**
191+
* @return string[]
192+
*/
193+
public function getPattern(): array
194+
{
195+
return [
196+
'/&?gad_source\=[^&]+/',
197+
'/&?gbraid\=[^&]+/',
198+
'/&?wbraid\=[^&]+/',
199+
'/&?_gl\=[^&]+/',
200+
'/&?dclid\=[^&]+/',
201+
'/&?gclsrc\=[^&]+/',
202+
'/&?srsltid\=[^&]+/',
203+
'/&?msclkid\=[^&]+/',
204+
'/&?_kx\=[^&]+/',
205+
'/&?gclid\=[^&]+/',
206+
'/&?cx\=[^&]+/',
207+
'/&?ie\=[^&]+/',
208+
'/&?cof\=[^&]+/',
209+
'/&?siteurl\=[^&]+/',
210+
'/&?zanpid\=[^&]+/',
211+
'/&?origin\=[^&]+/',
212+
'/&?fbclid\=[^&]+/',
213+
'/&?mc_(.*?)\=[^&]+/',
214+
'/&?utm_(.*?)\=[^&]+/',
215+
'/&?_bta_(.*?)\=[^&]+/',
216+
];
217+
}
124218
}

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

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2011 Adobe
4+
* All Rights Reserved.
55
*/
66
namespace Magento\Framework\App\PageCache;
77

@@ -50,13 +50,46 @@ public function __construct(
5050
*/
5151
public function getValue()
5252
{
53+
$pattern = $this->getMarketingParameterPatterns();
54+
$replace = array_fill(0, count($pattern), '');
5355
$data = [
5456
$this->request->isSecure(),
55-
$this->request->getUriString(),
57+
preg_replace($pattern, $replace, (string)$this->request->getUriString()),
5658
$this->request->get(\Magento\Framework\App\Response\Http::COOKIE_VARY_STRING)
5759
?: $this->context->getVaryString()
5860
];
5961

6062
return sha1($this->serializer->serialize($data));
6163
}
64+
65+
/**
66+
* Pattern detect marketing parameters
67+
*
68+
* @return array
69+
*/
70+
public function getMarketingParameterPatterns(): array
71+
{
72+
return [
73+
'/&?gad_source\=[^&]+/',
74+
'/&?gbraid\=[^&]+/',
75+
'/&?wbraid\=[^&]+/',
76+
'/&?_gl\=[^&]+/',
77+
'/&?dclid\=[^&]+/',
78+
'/&?gclsrc\=[^&]+/',
79+
'/&?srsltid\=[^&]+/',
80+
'/&?msclkid\=[^&]+/',
81+
'/&?_kx\=[^&]+/',
82+
'/&?gclid\=[^&]+/',
83+
'/&?cx\=[^&]+/',
84+
'/&?ie\=[^&]+/',
85+
'/&?cof\=[^&]+/',
86+
'/&?siteurl\=[^&]+/',
87+
'/&?zanpid\=[^&]+/',
88+
'/&?origin\=[^&]+/',
89+
'/&?fbclid\=[^&]+/',
90+
'/&?mc_(.*?)\=[^&]+/',
91+
'/&?utm_(.*?)\=[^&]+/',
92+
'/&?_bta_(.*?)\=[^&]+/',
93+
];
94+
}
6295
}

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

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

@@ -20,7 +20,7 @@ class IdentifierTest extends TestCase
2020
/**
2121
* Test value for cache vary string
2222
*/
23-
const VARY = '123';
23+
public const VARY = '123';
2424

2525
/**
2626
* @var ObjectManager
@@ -186,4 +186,37 @@ public function testGetValue(): void
186186
$this->model->getValue()
187187
);
188188
}
189+
190+
/**
191+
* Test get identifier value with marketing parameters.
192+
*
193+
* @return void
194+
*/
195+
public function testGetValueWithMarketingParameters(): void
196+
{
197+
$this->requestMock->expects($this->any())
198+
->method('isSecure')
199+
->willReturn(true);
200+
201+
$this->requestMock->expects($this->any())
202+
->method('getUriString')
203+
->willReturn('http://example.com/path1/?abc=123&gclid=456&utm_source=abc');
204+
205+
$this->contextMock->expects($this->any())
206+
->method('getVaryString')
207+
->willReturn(self::VARY);
208+
209+
$this->assertEquals(
210+
sha1(
211+
json_encode(
212+
[
213+
true,
214+
'http://example.com/path1/?abc=123',
215+
self::VARY
216+
]
217+
)
218+
),
219+
$this->model->getValue()
220+
);
221+
}
189222
}

0 commit comments

Comments
 (0)