Skip to content

Commit 26275c7

Browse files
committed
MAGE-1245 Test hit on backend render with user agent
1 parent afebfa9 commit 26275c7

File tree

1 file changed

+87
-49
lines changed

1 file changed

+87
-49
lines changed

Test/Integration/Category/CategoryCacheTest.php

Lines changed: 87 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,9 @@ protected function setUp(): void
3838
$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36';
3939
}
4040

41-
protected function resetCache($testMethod): void
42-
{
43-
if (!in_array($testMethod, self::$cacheResets)) {
44-
$this->cacheManager->clean(['full_page']);
45-
self::$cacheResets[] = $testMethod;
46-
}
47-
}
48-
4941
public static function setUpBeforeClass(): void
5042
{
51-
// self::reindexAll();
43+
self::reindexAll();
5244
}
5345

5446
/** You must index to OpenSearch to get the default backend render */
@@ -70,6 +62,21 @@ protected static function reindexAll(): void
7062
}
7163
}
7264

65+
/**
66+
* Selectively refresh the FPC cache (must be done at intervals)
67+
* Warm the cache via MISS tests but only reset the cache once per MISS test
68+
* Due to data provider test methods can be called multiple times
69+
* @param $testMethod
70+
* @return void
71+
*/
72+
protected function resetCache($testMethod): void
73+
{
74+
if (!in_array($testMethod, self::$cacheResets)) {
75+
$this->cacheManager->clean(['full_page']);
76+
self::$cacheResets[] = $testMethod;
77+
}
78+
}
79+
7380
/**
7481
* @dataProvider getCategoryProvider
7582
* @depends testFullPageCacheAvailable
@@ -103,40 +110,6 @@ public function testCategoryPlpMissBackendRenderOn(int $categoryId, string $name
103110
$this->assertMatchesRegularExpression('/<div.*class=.*products-grid.*>/', $response->getContent(), $response->getContent(), 'Backend content was not rendered.');
104111
}
105112

106-
/**
107-
* The \Magento\TestFramework\TestCase\AbstractController::dispatch is flawed for this use case as it does not
108-
* populate the URI which is used to build the cache key in \Magento\Framework\App\PageCache\Identifier::getValue
109-
*
110-
* This provides a workaround
111-
*
112-
* @param string $uri
113-
* @return void
114-
*/
115-
protected function dispatchHttpRequest(string $uri): void
116-
{
117-
$request = $this->_objectManager->get(\Magento\Framework\App\Request\Http::class);
118-
$request->setDispatched(false);
119-
$request->setUri($uri);
120-
$request->setRequestUri($uri);
121-
$this->_getBootstrap()->runApp();
122-
}
123-
124-
/**
125-
* It is imperative to always use the same URL format between MISS and HIT to ensure
126-
* that the cache key is generated consistently
127-
* @param int $categoryId
128-
* @return string
129-
*/
130-
protected function getCategoryUrl(int $categoryId): string
131-
{
132-
return $this->url . $categoryId;
133-
}
134-
135-
protected function dispatchCategoryPlpRequest(int $categoryId): void
136-
{
137-
$this->dispatchHttpRequest($this->getCategoryUrl($categoryId));
138-
}
139-
140113
/**
141114
* @dataProvider getCategoryProvider
142115
* @depends testCategoryPlpMissBackendRenderOn
@@ -257,6 +230,77 @@ public function testCategoryPlpMissBackendRenderWhiteList(int $categoryId, strin
257230
$this->assertMatchesRegularExpression('/<div.*class=.*products-grid.*>/', $response->getContent(), $response->getContent(), 'Backend content was not rendered.');
258231
}
259232

233+
/**
234+
* @dataProvider getCategoryProvider
235+
* @depends testCategoryPlpMissBackendRenderOff
236+
* @magentoConfigFixture current_store system/full_page_cache/caching_application 1
237+
* @magentoConfigFixture current_store algoliasearch_advanced/advanced/prevent_backend_rendering 1
238+
* @magentoConfigFixture current_store algoliasearch_instant/instant/replace_categories 1
239+
* @magentoDataFixture Algolia_AlgoliaSearch::Test/Integration/_files/backend_render_user_agents.php
240+
* @magentoCache full_page enabled
241+
* @param int $categoryId
242+
* @param string $name
243+
* @return void
244+
*/
245+
public function testCategoryPlpHitBackendRenderWhiteList(int $categoryId, string $name): void
246+
{
247+
$config = $this->_objectManager->get(ScopeConfigInterface::class);
248+
$preventBackend = $config->getValue('algoliasearch_advanced/advanced/prevent_backend_rendering', ScopeInterface::SCOPE_STORE);
249+
$this->assertEquals(1, $preventBackend,"Prevent backend rendering must be enabled for this test.");
250+
251+
$testUserAgent = "Foobot";
252+
$whitelist = $this->config->getValue('algoliasearch_advanced/advanced/backend_rendering_allowed_user_agents', ScopeInterface::SCOPE_STORE);
253+
$this->assertStringContainsString($testUserAgent, $whitelist, "Allowed user agents for backend render must include $testUserAgent");
254+
255+
$this->registerPageHitSpy();
256+
257+
$_SERVER['HTTP_USER_AGENT'] = $testUserAgent;
258+
259+
$this->dispatchCategoryPlpRequest($categoryId);
260+
$response = $this->getResponse();
261+
$this->assertEquals(200, $response->getHttpResponseCode(), 'Request failed');
262+
}
263+
264+
public function testFullPageCacheAvailable(): void
265+
{
266+
$types = $this->cacheManager->getAvailableTypes();
267+
$this->assertContains('full_page', $types);
268+
}
269+
270+
/**
271+
* The \Magento\TestFramework\TestCase\AbstractController::dispatch is flawed for this use case as it does not
272+
* populate the URI which is used to build the cache key in \Magento\Framework\App\PageCache\Identifier::getValue
273+
*
274+
* This provides a workaround
275+
*
276+
* @param string $uri
277+
* @return void
278+
*/
279+
protected function dispatchHttpRequest(string $uri): void
280+
{
281+
$request = $this->_objectManager->get(\Magento\Framework\App\Request\Http::class);
282+
$request->setDispatched(false);
283+
$request->setUri($uri);
284+
$request->setRequestUri($uri);
285+
$this->_getBootstrap()->runApp();
286+
}
287+
288+
/**
289+
* It is imperative to always use the same URL format between MISS and HIT to ensure
290+
* that the cache key is generated consistently
291+
* @param int $categoryId
292+
* @return string
293+
*/
294+
protected function getCategoryUrl(int $categoryId): string
295+
{
296+
return $this->url . $categoryId;
297+
}
298+
299+
protected function dispatchCategoryPlpRequest(int $categoryId): void
300+
{
301+
$this->dispatchHttpRequest($this->getCategoryUrl($categoryId));
302+
}
303+
260304
/**
261305
* The response object is modified differently by the BuiltinPlugin which prevents anything useful
262306
* being returned by AbstractController::getResponse when a HIT is encountered
@@ -302,10 +346,4 @@ function (ResponseHttp $response, $name, $value, $replace)
302346
);
303347
}
304348

305-
public function testFullPageCacheAvailable(): void
306-
{
307-
$types = $this->cacheManager->getAvailableTypes();
308-
$this->assertContains('full_page', $types);
309-
}
310-
311349
}

0 commit comments

Comments
 (0)