Skip to content

Commit 7277fe3

Browse files
committed
MAGE-1245 Add allowed user agent testing
1 parent 4ad6144 commit 7277fe3

File tree

2 files changed

+71
-17
lines changed

2 files changed

+71
-17
lines changed

Test/Integration/Category/CategoryCacheTest.php

Lines changed: 58 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
class CategoryCacheTest extends \Magento\TestFramework\TestCase\AbstractController
1313
{
14-
protected $cacheManager;
14+
protected ?CacheManager $cacheManager;
15+
protected ?ScopeConfigInterface $config;
1516

1617
protected $url = '/catalog/category/view/id/';
1718

@@ -28,14 +29,15 @@ protected function setUp(): void
2829
{
2930
parent::setUp();
3031
$this->cacheManager = $this->_objectManager->get(CacheManager::class);
32+
$this->config = $this->_objectManager->get(ScopeConfigInterface::class);
3133

3234
// Default user agent
3335
$_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';
3436
}
3537

3638
public static function setUpBeforeClass(): void
3739
{
38-
self::reindexAll();
40+
// self::reindexAll();
3941
}
4042

4143
protected static function reindexAll(): void
@@ -69,8 +71,7 @@ protected static function reindexAll(): void
6971
*/
7072
public function testCategoryPlpMissBackendRenderOn(int $categoryId, string $name): void
7173
{
72-
$config = $this->_objectManager->get(ScopeConfigInterface::class);
73-
$replace = $config->getValue('algoliasearch_instant/instant/replace_categories', ScopeInterface::SCOPE_STORE);
74+
$replace = $this->config->getValue('algoliasearch_instant/instant/replace_categories', ScopeInterface::SCOPE_STORE);
7475
$this->assertEquals(1, $replace,"Replace categories must be enabled for this test.");
7576

7677
$this->cacheManager->clean(['full_page']);
@@ -92,12 +93,6 @@ public function testCategoryPlpMissBackendRenderOn(int $categoryId, string $name
9293

9394

9495
/**
95-
* The response object is modified differently by the BuiltinPlugin which prevents anything useful
96-
* being returned by AbstractController::getResponse
97-
*
98-
* Therefore we apply a "spy" on the plugin via a mock to ensure that the proper header is added
99-
* when the cache has been warmed (by the first MISS)
100-
*
10196
* @dataProvider getCategoryProvider
10297
* @depends testCategoryPlpMissBackendRenderOn
10398
* @magentoConfigFixture current_store system/full_page_cache/caching_application 1
@@ -110,8 +105,7 @@ public function testCategoryPlpMissBackendRenderOn(int $categoryId, string $name
110105
*/
111106
public function testCategoryPlpHitBackendRenderOn(int $categoryId, string $name): void
112107
{
113-
$config = $this->_objectManager->get(ScopeConfigInterface::class);
114-
$replace = $config->getValue('algoliasearch_instant/instant/replace_categories', ScopeInterface::SCOPE_STORE);
108+
$replace = $this->config->getValue('algoliasearch_instant/instant/replace_categories', ScopeInterface::SCOPE_STORE);
115109
$this->assertEquals(1, $replace,"Replace categories must be enabled for this test.");
116110

117111
$this->registerPageHitSpy();
@@ -134,8 +128,7 @@ public function testCategoryPlpHitBackendRenderOn(int $categoryId, string $name)
134128
*/
135129
public function testCategoryPlpMissBackendRenderOff(int $categoryId, string $name): void
136130
{
137-
$config = $this->_objectManager->get(ScopeConfigInterface::class);
138-
$preventBackend = $config->getValue('algoliasearch_advanced/advanced/prevent_backend_rendering', ScopeInterface::SCOPE_STORE);
131+
$preventBackend = $this->config->getValue('algoliasearch_advanced/advanced/prevent_backend_rendering', ScopeInterface::SCOPE_STORE);
139132
$this->assertEquals(1, $preventBackend,"Prevent backend rendering must be enabled for this test.");
140133

141134
$this->cacheManager->clean(['full_page']);
@@ -168,8 +161,7 @@ public function testCategoryPlpMissBackendRenderOff(int $categoryId, string $nam
168161
*/
169162
public function testCategoryPlpHitBackendRenderOff(int $categoryId, string $name): void
170163
{
171-
$config = $this->_objectManager->get(ScopeConfigInterface::class);
172-
$preventBackend = $config->getValue('algoliasearch_advanced/advanced/prevent_backend_rendering', ScopeInterface::SCOPE_STORE);
164+
$preventBackend = $this->config->getValue('algoliasearch_advanced/advanced/prevent_backend_rendering', ScopeInterface::SCOPE_STORE);
173165
$this->assertEquals(1, $preventBackend,"Prevent backend rendering must be enabled for this test.");
174166

175167
$this->registerPageHitSpy();
@@ -179,7 +171,56 @@ public function testCategoryPlpHitBackendRenderOff(int $categoryId, string $name
179171
$this->assertEquals(200, $response->getHttpResponseCode(), 'Request failed');
180172
}
181173

182-
protected function registerPageHitSpy() {
174+
/**
175+
* @dataProvider getCategoryProvider
176+
* @depends testCategoryPlpHitBackendRenderOff
177+
* @magentoConfigFixture current_store system/full_page_cache/caching_application 1
178+
* @magentoConfigFixture current_store algoliasearch_advanced/advanced/prevent_backend_rendering 1
179+
* @magentoConfigFixture current_store algoliasearch_instant/instant/replace_categories 1
180+
* @magentoDataFixture Algolia_AlgoliaSearch::Test/Integration/_files/backend_render_user_agents.php
181+
* @magentoCache full_page enabled
182+
* @param int $categoryId
183+
* @param string $name
184+
* @return void
185+
*/
186+
public function testCategoryPlpMissBackendRenderWhiteList(int $categoryId, string $name): void
187+
{
188+
$preventBackend = $this->config->getValue('algoliasearch_advanced/advanced/prevent_backend_rendering', ScopeInterface::SCOPE_STORE);
189+
$this->assertEquals(1, $preventBackend,"Prevent backend rendering must be enabled for this test.");
190+
191+
$testUserAgent = "Foobot";
192+
$whitelist = $this->config->getValue('algoliasearch_advanced/advanced/backend_rendering_allowed_user_agents', ScopeInterface::SCOPE_STORE);
193+
$this->assertStringContainsString($testUserAgent, $whitelist, "Allowed user agents for backend render must include $testUserAgent");
194+
195+
$_SERVER['HTTP_USER_AGENT'] = $testUserAgent;
196+
197+
$this->dispatch($this->url . $categoryId);
198+
$response = $this->getResponse();
199+
$this->assertEquals(200, $response->getHttpResponseCode(), 'Request failed');
200+
$this->assertEquals(
201+
'MISS',
202+
$response->getHeader('X-Magento-Cache-Debug')->getFieldValue(),
203+
"expected MISS on category {$name} id {$categoryId}"
204+
);
205+
$this->assertContains(
206+
'FPC',
207+
explode(',', $response->getHeader('X-Magento-Tags')->getFieldValue()),
208+
"expected FPC tag on category {$name} id {$categoryId}"
209+
);
210+
$this->assertMatchesRegularExpression('/<div.*class=.*products-grid.*>/', $response->getContent(), $response->getContent(), 'Backend content was not rendered.');
211+
}
212+
213+
/**
214+
* The response object is modified differently by the BuiltinPlugin which prevents anything useful
215+
* being returned by AbstractController::getResponse when a HIT is encountered
216+
*
217+
* Therefore we apply a "spy" on the plugin via a mock to ensure that the proper header is added
218+
* when the cache has been warmed (by the first MISS)
219+
*
220+
* @return void
221+
*/
222+
protected function registerPageHitSpy(): void
223+
{
183224
$mockedPluginClass = \Magento\PageCache\Model\App\FrontController\BuiltinPlugin::class;
184225
$mockedPluginMethod = 'addDebugHeader';
185226
$cachePluginMock = $this->getMockBuilder($mockedPluginClass)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
use Magento\TestFramework\Helper\Bootstrap;
3+
use Magento\Framework\App\Config\MutableScopeConfigInterface;
4+
5+
$objectManager = Bootstrap::getObjectManager();
6+
$scopeConfig = $objectManager->get(MutableScopeConfigInterface::class);
7+
8+
// Set complex configuration value
9+
$scopeConfig->setValue(
10+
'algoliasearch_advanced/advanced/backend_rendering_allowed_user_agents',
11+
join("\n", ["Googlebot", "Bingbot", "Foobot"]),
12+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
13+
);

0 commit comments

Comments
 (0)