Skip to content

Commit 4ad6144

Browse files
committed
MAGE-1245 Test backend render on and off
1 parent fe5a92a commit 4ad6144

File tree

1 file changed

+77
-22
lines changed

1 file changed

+77
-22
lines changed

Test/Integration/Category/CategoryCacheTest.php

Lines changed: 77 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,16 @@ public static function getCategoryProvider(): array
2121
// ['categoryId' => 20, 'name' => 'Women'],
2222
['categoryId' => 21, 'name' => 'Women > Tops'],
2323
['categoryId' => 22, 'name' => 'Women > Bottoms'],
24-
2524
];
2625
}
2726

2827
protected function setUp(): void
2928
{
3029
parent::setUp();
3130
$this->cacheManager = $this->_objectManager->get(CacheManager::class);
31+
32+
// Default user agent
33+
$_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';
3234
}
3335

3436
public static function setUpBeforeClass(): void
@@ -65,7 +67,7 @@ protected static function reindexAll(): void
6567
* @param string $name
6668
* @return void
6769
*/
68-
public function testCategoryPlpMiss(int $categoryId, string $name): void
70+
public function testCategoryPlpMissBackendRenderOn(int $categoryId, string $name): void
6971
{
7072
$config = $this->_objectManager->get(ScopeConfigInterface::class);
7173
$replace = $config->getValue('algoliasearch_instant/instant/replace_categories', ScopeInterface::SCOPE_STORE);
@@ -88,19 +90,6 @@ public function testCategoryPlpMiss(int $categoryId, string $name): void
8890
$this->assertMatchesRegularExpression('/<div.*class=.*products-grid.*>/', $response->getContent(), $response->getContent(), 'Backend content was not rendered.');
8991
}
9092

91-
/**
92-
* @magentoDbIsolation disabled
93-
*/
94-
public function testCategoryPageLoadsProducts()
95-
{
96-
$this->reindexAll();
97-
$this->dispatch('/catalog/category/view/id/21');
98-
99-
$responseBody = $this->getResponse()->getBody();
100-
101-
// Assert that the response contains product data
102-
$this->assertStringContainsString('product-item', $responseBody, 'Expected product items were not found.');
103-
}
10493

10594
/**
10695
* The response object is modified differently by the BuiltinPlugin which prevents anything useful
@@ -110,7 +99,7 @@ public function testCategoryPageLoadsProducts()
11099
* when the cache has been warmed (by the first MISS)
111100
*
112101
* @dataProvider getCategoryProvider
113-
* @depends testCategoryPlpMiss
102+
* @depends testCategoryPlpMissBackendRenderOn
114103
* @magentoConfigFixture current_store system/full_page_cache/caching_application 1
115104
* @magentoConfigFixture current_store algoliasearch_advanced/advanced/prevent_backend_rendering 0
116105
* @magentoConfigFixture current_store algoliasearch_instant/instant/replace_categories 1
@@ -119,8 +108,78 @@ public function testCategoryPageLoadsProducts()
119108
* @param string $name
120109
* @return void
121110
*/
122-
public function testCategoryPlpHit(int $categoryId, string $name): void
111+
public function testCategoryPlpHitBackendRenderOn(int $categoryId, string $name): void
123112
{
113+
$config = $this->_objectManager->get(ScopeConfigInterface::class);
114+
$replace = $config->getValue('algoliasearch_instant/instant/replace_categories', ScopeInterface::SCOPE_STORE);
115+
$this->assertEquals(1, $replace,"Replace categories must be enabled for this test.");
116+
117+
$this->registerPageHitSpy();
118+
119+
$this->dispatch("catalog/category/view/id/{$categoryId}");
120+
$response = $this->getResponse();
121+
$this->assertEquals(200, $response->getHttpResponseCode(), 'Request failed');
122+
}
123+
124+
/**
125+
* @dataProvider getCategoryProvider
126+
* @depends testFullPageCacheAvailable
127+
* @magentoConfigFixture current_store system/full_page_cache/caching_application 1
128+
* @magentoConfigFixture current_store algoliasearch_advanced/advanced/prevent_backend_rendering 1
129+
* @magentoConfigFixture current_store algoliasearch_instant/instant/replace_categories 1
130+
* @magentoCache full_page enabled
131+
* @param int $categoryId
132+
* @param string $name
133+
* @return void
134+
*/
135+
public function testCategoryPlpMissBackendRenderOff(int $categoryId, string $name): void
136+
{
137+
$config = $this->_objectManager->get(ScopeConfigInterface::class);
138+
$preventBackend = $config->getValue('algoliasearch_advanced/advanced/prevent_backend_rendering', ScopeInterface::SCOPE_STORE);
139+
$this->assertEquals(1, $preventBackend,"Prevent backend rendering must be enabled for this test.");
140+
141+
$this->cacheManager->clean(['full_page']);
142+
$this->dispatch($this->url . $categoryId);
143+
$response = $this->getResponse();
144+
$this->assertEquals(200, $response->getHttpResponseCode(), 'Request failed');
145+
$this->assertEquals(
146+
'MISS',
147+
$response->getHeader('X-Magento-Cache-Debug')->getFieldValue(),
148+
"expected MISS on category {$name} id {$categoryId}"
149+
);
150+
$this->assertContains(
151+
'FPC',
152+
explode(',', $response->getHeader('X-Magento-Tags')->getFieldValue()),
153+
"expected FPC tag on category {$name} id {$categoryId}"
154+
);
155+
$this->assertDoesNotMatchRegularExpression('/<div.*class=.*products-grid.*>/', $response->getContent(), $response->getContent(), 'Backend content was not rendered.');
156+
}
157+
158+
/**
159+
* @dataProvider getCategoryProvider
160+
* @depends testCategoryPlpMissBackendRenderOff
161+
* @magentoConfigFixture current_store system/full_page_cache/caching_application 1
162+
* @magentoConfigFixture current_store algoliasearch_advanced/advanced/prevent_backend_rendering 1
163+
* @magentoConfigFixture current_store algoliasearch_instant/instant/replace_categories 1
164+
* @magentoCache full_page enabled
165+
* @param int $categoryId
166+
* @param string $name
167+
* @return void
168+
*/
169+
public function testCategoryPlpHitBackendRenderOff(int $categoryId, string $name): void
170+
{
171+
$config = $this->_objectManager->get(ScopeConfigInterface::class);
172+
$preventBackend = $config->getValue('algoliasearch_advanced/advanced/prevent_backend_rendering', ScopeInterface::SCOPE_STORE);
173+
$this->assertEquals(1, $preventBackend,"Prevent backend rendering must be enabled for this test.");
174+
175+
$this->registerPageHitSpy();
176+
177+
$this->dispatch("catalog/category/view/id/{$categoryId}");
178+
$response = $this->getResponse();
179+
$this->assertEquals(200, $response->getHttpResponseCode(), 'Request failed');
180+
}
181+
182+
protected function registerPageHitSpy() {
124183
$mockedPluginClass = \Magento\PageCache\Model\App\FrontController\BuiltinPlugin::class;
125184
$mockedPluginMethod = 'addDebugHeader';
126185
$cachePluginMock = $this->getMockBuilder($mockedPluginClass)
@@ -143,7 +202,7 @@ public function testCategoryPlpHit(int $categoryId, string $name): void
143202
)
144203
->willReturnCallback(
145204
function (ResponseHttp $response, $name, $value, $replace)
146-
use ($mockedPluginClass, $mockedPluginMethod, $cachePluginMock)
205+
use ($mockedPluginClass, $mockedPluginMethod, $cachePluginMock)
147206
{
148207
$originalMethod = new \ReflectionMethod($mockedPluginClass, $mockedPluginMethod);
149208
return $originalMethod->invoke($cachePluginMock, $response, $name, $value, $replace);
@@ -153,10 +212,6 @@ function (ResponseHttp $response, $name, $value, $replace)
153212
$cachePluginMock,
154213
$mockedPluginClass
155214
);
156-
157-
$this->dispatch("catalog/category/view/id/{$categoryId}");
158-
$response = $this->getResponse();
159-
$this->assertEquals(200, $response->getHttpResponseCode(), 'Request failed');
160215
}
161216

162217
public function testFullPageCacheAvailable(): void

0 commit comments

Comments
 (0)