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 */
66declare (strict_types=1 );
77
88namespace Magento \PageCache \Test \Unit \Model \App \Request \Http ;
99
1010use Magento \Framework \App \Http \Context ;
11+ use Magento \Framework \App \ObjectManager ;
12+ use Magento \Framework \App \PageCache \Identifier ;
1113use Magento \Framework \App \Request \Http as HttpRequest ;
14+ use Magento \Framework \ObjectManagerInterface ;
1215use Magento \Framework \Serialize \Serializer \Json ;
1316use Magento \PageCache \Model \App \Request \Http \IdentifierForSave ;
1417use 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}
0 commit comments