11
11
12
12
class CategoryCacheTest extends \Magento \TestFramework \TestCase \AbstractController
13
13
{
14
- protected $ cacheManager ;
14
+ protected ?CacheManager $ cacheManager ;
15
+ protected ?ScopeConfigInterface $ config ;
15
16
16
17
protected $ url = '/catalog/category/view/id/ ' ;
17
18
@@ -28,14 +29,15 @@ protected function setUp(): void
28
29
{
29
30
parent ::setUp ();
30
31
$ this ->cacheManager = $ this ->_objectManager ->get (CacheManager::class);
32
+ $ this ->config = $ this ->_objectManager ->get (ScopeConfigInterface::class);
31
33
32
34
// Default user agent
33
35
$ _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 ' ;
34
36
}
35
37
36
38
public static function setUpBeforeClass (): void
37
39
{
38
- self ::reindexAll ();
40
+ // self::reindexAll();
39
41
}
40
42
41
43
protected static function reindexAll (): void
@@ -69,8 +71,7 @@ protected static function reindexAll(): void
69
71
*/
70
72
public function testCategoryPlpMissBackendRenderOn (int $ categoryId , string $ name ): void
71
73
{
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 );
74
75
$ this ->assertEquals (1 , $ replace ,"Replace categories must be enabled for this test. " );
75
76
76
77
$ this ->cacheManager ->clean (['full_page ' ]);
@@ -92,12 +93,6 @@ public function testCategoryPlpMissBackendRenderOn(int $categoryId, string $name
92
93
93
94
94
95
/**
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
- *
101
96
* @dataProvider getCategoryProvider
102
97
* @depends testCategoryPlpMissBackendRenderOn
103
98
* @magentoConfigFixture current_store system/full_page_cache/caching_application 1
@@ -110,8 +105,7 @@ public function testCategoryPlpMissBackendRenderOn(int $categoryId, string $name
110
105
*/
111
106
public function testCategoryPlpHitBackendRenderOn (int $ categoryId , string $ name ): void
112
107
{
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 );
115
109
$ this ->assertEquals (1 , $ replace ,"Replace categories must be enabled for this test. " );
116
110
117
111
$ this ->registerPageHitSpy ();
@@ -134,8 +128,7 @@ public function testCategoryPlpHitBackendRenderOn(int $categoryId, string $name)
134
128
*/
135
129
public function testCategoryPlpMissBackendRenderOff (int $ categoryId , string $ name ): void
136
130
{
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 );
139
132
$ this ->assertEquals (1 , $ preventBackend ,"Prevent backend rendering must be enabled for this test. " );
140
133
141
134
$ this ->cacheManager ->clean (['full_page ' ]);
@@ -168,8 +161,7 @@ public function testCategoryPlpMissBackendRenderOff(int $categoryId, string $nam
168
161
*/
169
162
public function testCategoryPlpHitBackendRenderOff (int $ categoryId , string $ name ): void
170
163
{
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 );
173
165
$ this ->assertEquals (1 , $ preventBackend ,"Prevent backend rendering must be enabled for this test. " );
174
166
175
167
$ this ->registerPageHitSpy ();
@@ -179,7 +171,56 @@ public function testCategoryPlpHitBackendRenderOff(int $categoryId, string $name
179
171
$ this ->assertEquals (200 , $ response ->getHttpResponseCode (), 'Request failed ' );
180
172
}
181
173
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
+ {
183
224
$ mockedPluginClass = \Magento \PageCache \Model \App \FrontController \BuiltinPlugin::class;
184
225
$ mockedPluginMethod = 'addDebugHeader ' ;
185
226
$ cachePluginMock = $ this ->getMockBuilder ($ mockedPluginClass )
0 commit comments