8
8
use Magento \Framework \Indexer \IndexerRegistry ;
9
9
use Magento \Store \Model \ScopeInterface ;
10
10
use Magento \TestFramework \ObjectManager ;
11
+ use Magento \TestFramework \TestCase \AbstractController ;
11
12
12
- class CategoryCacheTest extends \ Magento \ TestFramework \ TestCase \ AbstractController
13
+ class CategoryCacheTest extends AbstractController
13
14
{
14
15
protected ?CacheManager $ cacheManager ;
15
16
protected ?ScopeConfigInterface $ config ;
@@ -40,6 +41,7 @@ public static function setUpBeforeClass(): void
40
41
// self::reindexAll();
41
42
}
42
43
44
+ /** You must index to OpenSearch to get the default backend render */
43
45
protected static function reindexAll (): void
44
46
{
45
47
$ objectManager = ObjectManager::getInstance ();
@@ -75,7 +77,7 @@ public function testCategoryPlpMissBackendRenderOn(int $categoryId, string $name
75
77
$ this ->assertEquals (1 , $ replace ,"Replace categories must be enabled for this test. " );
76
78
77
79
$ this ->cacheManager ->clean (['full_page ' ]);
78
- $ this ->dispatch ($ this ->url . $ categoryId );
80
+ $ this ->dispatchHttpRequest ($ this ->url . $ categoryId );
79
81
$ response = $ this ->getResponse ();
80
82
$ this ->assertEquals (200 , $ response ->getHttpResponseCode (), 'Request failed ' );
81
83
$ this ->assertEquals (
@@ -91,6 +93,23 @@ public function testCategoryPlpMissBackendRenderOn(int $categoryId, string $name
91
93
$ this ->assertMatchesRegularExpression ('/<div.*class=.*products-grid.*>/ ' , $ response ->getContent (), $ response ->getContent (), 'Backend content was not rendered. ' );
92
94
}
93
95
96
+ /**
97
+ * The \Magento\TestFramework\TestCase\AbstractController::dispatch is flawed for this use case as it does not
98
+ * populate the URI which is used to build the cache key in \Magento\Framework\App\PageCache\Identifier::getValue
99
+ *
100
+ * This provides a workaround
101
+ *
102
+ * @param string $uri
103
+ * @return void
104
+ */
105
+ public function dispatchHttpRequest (string $ uri ): void
106
+ {
107
+ $ request = $ this ->_objectManager ->get (\Magento \Framework \App \Request \Http::class);
108
+ $ request ->setDispatched (false );
109
+ $ request ->setUri ($ uri );
110
+ $ request ->setRequestUri ($ uri );
111
+ $ this ->_getBootstrap ()->runApp ();
112
+ }
94
113
95
114
/**
96
115
* @dataProvider getCategoryProvider
@@ -110,7 +129,7 @@ public function testCategoryPlpHitBackendRenderOn(int $categoryId, string $name)
110
129
111
130
$ this ->registerPageHitSpy ();
112
131
113
- $ this ->dispatch ("catalog/category/view/id/ {$ categoryId }" );
132
+ $ this ->dispatchHttpRequest ("catalog/category/view/id/ {$ categoryId }" );
114
133
$ response = $ this ->getResponse ();
115
134
$ this ->assertEquals (200 , $ response ->getHttpResponseCode (), 'Request failed ' );
116
135
}
@@ -132,7 +151,7 @@ public function testCategoryPlpMissBackendRenderOff(int $categoryId, string $nam
132
151
$ this ->assertEquals (1 , $ preventBackend ,"Prevent backend rendering must be enabled for this test. " );
133
152
134
153
$ this ->cacheManager ->clean (['full_page ' ]);
135
- $ this ->dispatch ($ this ->url . $ categoryId );
154
+ $ this ->dispatchHttpRequest ($ this ->url . $ categoryId );
136
155
$ response = $ this ->getResponse ();
137
156
$ this ->assertEquals (200 , $ response ->getHttpResponseCode (), 'Request failed ' );
138
157
$ this ->assertEquals (
@@ -166,7 +185,7 @@ public function testCategoryPlpHitBackendRenderOff(int $categoryId, string $name
166
185
167
186
$ this ->registerPageHitSpy ();
168
187
169
- $ this ->dispatch ("catalog/category/view/id/ {$ categoryId }" );
188
+ $ this ->dispatchHttpRequest ("catalog/category/view/id/ {$ categoryId }" );
170
189
$ response = $ this ->getResponse ();
171
190
$ this ->assertEquals (200 , $ response ->getHttpResponseCode (), 'Request failed ' );
172
191
}
@@ -194,7 +213,7 @@ public function testCategoryPlpMissBackendRenderWhiteList(int $categoryId, strin
194
213
195
214
$ _SERVER ['HTTP_USER_AGENT ' ] = $ testUserAgent ;
196
215
197
- $ this ->dispatch ($ this ->url . $ categoryId );
216
+ $ this ->dispatchHttpRequest ($ this ->url . $ categoryId );
198
217
$ response = $ this ->getResponse ();
199
218
$ this ->assertEquals (200 , $ response ->getHttpResponseCode (), 'Request failed ' );
200
219
$ this ->assertEquals (
0 commit comments