@@ -17,7 +17,8 @@ class CategoryCacheTest extends AbstractController
17
17
18
18
protected static $ cacheResets = [];
19
19
20
- protected $ url = '/catalog/category/view/id/ ' ;
20
+ public const BASE_CATEGORY_URL = '/catalog/category/view/id/ ' ;
21
+ public const TEST_USER_AGENT = 'Foobot ' ;
21
22
22
23
public static function getCategoryProvider (): array
23
24
{
@@ -88,8 +89,7 @@ protected function resetCache(string $testMethod): void
88
89
*/
89
90
public function testCategoryPlpMissBackendRenderOn (int $ categoryId , string $ name , bool $ hasProducts ): void
90
91
{
91
- $ replace = $ this ->config ->getValue ('algoliasearch_instant/instant/replace_categories ' , ScopeInterface::SCOPE_STORE );
92
- $ this ->assertEquals (1 , $ replace ,"Replace categories must be enabled for this test. " );
92
+ $ this ->assertReplaceCategories ();
93
93
94
94
$ this ->resetCache (__METHOD__ );
95
95
$ this ->dispatchCategoryPlpRequest ($ categoryId );
@@ -121,8 +121,7 @@ public function testCategoryPlpMissBackendRenderOn(int $categoryId, string $name
121
121
*/
122
122
public function testCategoryPlpHitBackendRenderOn (int $ categoryId , string $ name ): void
123
123
{
124
- $ replace = $ this ->config ->getValue ('algoliasearch_instant/instant/replace_categories ' , ScopeInterface::SCOPE_STORE );
125
- $ this ->assertEquals (1 , $ replace ,"Replace categories must be enabled for this test. " );
124
+ $ this ->assertReplaceCategories ();
126
125
127
126
$ this ->registerPageHitSpy ();
128
127
@@ -141,8 +140,7 @@ public function testCategoryPlpHitBackendRenderOn(int $categoryId, string $name)
141
140
*/
142
141
public function testCategoryPlpMissBackendRenderOff (int $ categoryId , string $ name , bool $ hasProducts ): void
143
142
{
144
- $ preventBackend = $ this ->config ->getValue ('algoliasearch_advanced/advanced/prevent_backend_rendering ' , ScopeInterface::SCOPE_STORE );
145
- $ this ->assertEquals (1 , $ preventBackend ,"Prevent backend rendering must be enabled for this test. " );
143
+ $ this ->assertPreventBackend ();
146
144
147
145
$ this ->resetCache (__METHOD__ );
148
146
$ this ->dispatchCategoryPlpRequest ($ categoryId );
@@ -174,8 +172,7 @@ public function testCategoryPlpMissBackendRenderOff(int $categoryId, string $nam
174
172
*/
175
173
public function testCategoryPlpHitBackendRenderOff (int $ categoryId , string $ name ): void
176
174
{
177
- $ preventBackend = $ this ->config ->getValue ('algoliasearch_advanced/advanced/prevent_backend_rendering ' , ScopeInterface::SCOPE_STORE );
178
- $ this ->assertEquals (1 , $ preventBackend ,"Prevent backend rendering must be enabled for this test. " );
175
+ $ this ->assertPreventBackend ();
179
176
180
177
$ this ->registerPageHitSpy ();
181
178
@@ -196,14 +193,9 @@ public function testCategoryPlpHitBackendRenderOff(int $categoryId, string $name
196
193
*/
197
194
public function testCategoryPlpMissBackendRenderWhiteList (int $ categoryId , string $ name , bool $ hasProducts ): void
198
195
{
199
- $ preventBackend = $ this ->config ->getValue ('algoliasearch_advanced/advanced/prevent_backend_rendering ' , ScopeInterface::SCOPE_STORE );
200
- $ this ->assertEquals (1 , $ preventBackend ,"Prevent backend rendering must be enabled for this test. " );
196
+ $ this ->assertPreventBackend ();
201
197
202
- $ testUserAgent = "Foobot " ;
203
- $ whitelist = $ this ->config ->getValue ('algoliasearch_advanced/advanced/backend_rendering_allowed_user_agents ' , ScopeInterface::SCOPE_STORE );
204
- $ this ->assertStringContainsString ($ testUserAgent , $ whitelist , "Allowed user agents for backend render must include $ testUserAgent " );
205
-
206
- $ _SERVER ['HTTP_USER_AGENT ' ] = $ testUserAgent ;
198
+ $ this ->setupUserAgent ();
207
199
208
200
$ this ->dispatchCategoryPlpRequest ($ categoryId );
209
201
@@ -236,18 +228,12 @@ public function testCategoryPlpMissBackendRenderWhiteList(int $categoryId, strin
236
228
*/
237
229
public function testCategoryPlpHitBackendRenderWhiteList (int $ categoryId , string $ name ): void
238
230
{
239
- $ config = $ this ->_objectManager ->get (ScopeConfigInterface::class);
240
- $ preventBackend = $ config ->getValue ('algoliasearch_advanced/advanced/prevent_backend_rendering ' , ScopeInterface::SCOPE_STORE );
241
- $ this ->assertEquals (1 , $ preventBackend ,"Prevent backend rendering must be enabled for this test. " );
231
+ $ this ->assertPreventBackend ();
242
232
243
- $ testUserAgent = "Foobot " ;
244
- $ whitelist = $ this ->config ->getValue ('algoliasearch_advanced/advanced/backend_rendering_allowed_user_agents ' , ScopeInterface::SCOPE_STORE );
245
- $ this ->assertStringContainsString ($ testUserAgent , $ whitelist , "Allowed user agents for backend render must include $ testUserAgent " );
233
+ $ this ->setupUserAgent ();
246
234
247
235
$ this ->registerPageHitSpy ();
248
236
249
- $ _SERVER ['HTTP_USER_AGENT ' ] = $ testUserAgent ;
250
-
251
237
$ this ->dispatchCategoryPlpRequest ($ categoryId );
252
238
$ response = $ this ->getResponse ();
253
239
$ this ->assertEquals (200 , $ response ->getHttpResponseCode (), 'Request failed ' );
@@ -259,6 +245,46 @@ public function testFullPageCacheAvailable(): void
259
245
$ this ->assertContains ('full_page ' , $ types );
260
246
}
261
247
248
+
249
+ protected function assertConfig (string $ path , string $ expected , string $ message ): void
250
+ {
251
+ $ this ->assertEquals ($ expected , $ this ->config ->getValue ($ path , ScopeInterface::SCOPE_STORE ), $ message );
252
+ }
253
+
254
+ protected function assertReplaceCategories (): void
255
+ {
256
+ $ this ->assertConfig (
257
+ 'algoliasearch_instant/instant/replace_categories ' ,
258
+ 1 ,
259
+ "Replace categories must be enabled for this test. "
260
+ );
261
+ }
262
+
263
+ protected function assertPreventBackend (): void
264
+ {
265
+ $ this ->assertConfig (
266
+ 'algoliasearch_advanced/advanced/prevent_backend_rendering ' ,
267
+ 1 ,
268
+ "Prevent backend rendering must be enabled for this test. "
269
+ );
270
+ }
271
+
272
+ protected function assertUserAgentAllowed (string $ userAgent ): void
273
+ {
274
+ $ this ->assertStringContainsString (
275
+ $ userAgent ,
276
+ $ this ->config ->getValue ('algoliasearch_advanced/advanced/backend_rendering_allowed_user_agents ' , ScopeInterface::SCOPE_STORE ),
277
+ "Allowed user agents for backend render must include $ userAgent "
278
+ );
279
+ }
280
+
281
+ protected function setupUserAgent (): void
282
+ {
283
+ $ testUserAgent = self ::TEST_USER_AGENT ;
284
+ $ this ->assertUserAgentAllowed ($ testUserAgent );
285
+ $ _SERVER ['HTTP_USER_AGENT ' ] = $ testUserAgent ;
286
+ }
287
+
262
288
/**
263
289
* The \Magento\TestFramework\TestCase\AbstractController::dispatch is flawed for this use case as it does not
264
290
* populate the URI which is used to build the cache key in \Magento\Framework\App\PageCache\Identifier::getValue
@@ -285,9 +311,12 @@ protected function dispatchHttpRequest(string $uri): void
285
311
*/
286
312
protected function getCategoryUrl (int $ categoryId ): string
287
313
{
288
- return $ this -> url . $ categoryId ;
314
+ return self :: BASE_CATEGORY_URL . $ categoryId ;
289
315
}
290
316
317
+ /**
318
+ * Dispatches a request using a properly formatted URL to ensure consistent cache key creation
319
+ */
291
320
protected function dispatchCategoryPlpRequest (int $ categoryId ): void
292
321
{
293
322
$ this ->dispatchHttpRequest ($ this ->getCategoryUrl ($ categoryId ));
0 commit comments