Skip to content

Commit aebe90e

Browse files
committed
MAGE-1245 Add custom dispatch method to pass URI for cache ID gen
1 parent 7277fe3 commit aebe90e

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

Test/Integration/Category/CategoryCacheTest.php

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
use Magento\Framework\Indexer\IndexerRegistry;
99
use Magento\Store\Model\ScopeInterface;
1010
use Magento\TestFramework\ObjectManager;
11+
use Magento\TestFramework\TestCase\AbstractController;
1112

12-
class CategoryCacheTest extends \Magento\TestFramework\TestCase\AbstractController
13+
class CategoryCacheTest extends AbstractController
1314
{
1415
protected ?CacheManager $cacheManager;
1516
protected ?ScopeConfigInterface $config;
@@ -40,6 +41,7 @@ public static function setUpBeforeClass(): void
4041
// self::reindexAll();
4142
}
4243

44+
/** You must index to OpenSearch to get the default backend render */
4345
protected static function reindexAll(): void
4446
{
4547
$objectManager = ObjectManager::getInstance();
@@ -75,7 +77,7 @@ public function testCategoryPlpMissBackendRenderOn(int $categoryId, string $name
7577
$this->assertEquals(1, $replace,"Replace categories must be enabled for this test.");
7678

7779
$this->cacheManager->clean(['full_page']);
78-
$this->dispatch($this->url . $categoryId);
80+
$this->dispatchHttpRequest($this->url . $categoryId);
7981
$response = $this->getResponse();
8082
$this->assertEquals(200, $response->getHttpResponseCode(), 'Request failed');
8183
$this->assertEquals(
@@ -91,6 +93,23 @@ public function testCategoryPlpMissBackendRenderOn(int $categoryId, string $name
9193
$this->assertMatchesRegularExpression('/<div.*class=.*products-grid.*>/', $response->getContent(), $response->getContent(), 'Backend content was not rendered.');
9294
}
9395

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+
}
94113

95114
/**
96115
* @dataProvider getCategoryProvider
@@ -110,7 +129,7 @@ public function testCategoryPlpHitBackendRenderOn(int $categoryId, string $name)
110129

111130
$this->registerPageHitSpy();
112131

113-
$this->dispatch("catalog/category/view/id/{$categoryId}");
132+
$this->dispatchHttpRequest("catalog/category/view/id/{$categoryId}");
114133
$response = $this->getResponse();
115134
$this->assertEquals(200, $response->getHttpResponseCode(), 'Request failed');
116135
}
@@ -132,7 +151,7 @@ public function testCategoryPlpMissBackendRenderOff(int $categoryId, string $nam
132151
$this->assertEquals(1, $preventBackend,"Prevent backend rendering must be enabled for this test.");
133152

134153
$this->cacheManager->clean(['full_page']);
135-
$this->dispatch($this->url . $categoryId);
154+
$this->dispatchHttpRequest($this->url . $categoryId);
136155
$response = $this->getResponse();
137156
$this->assertEquals(200, $response->getHttpResponseCode(), 'Request failed');
138157
$this->assertEquals(
@@ -166,7 +185,7 @@ public function testCategoryPlpHitBackendRenderOff(int $categoryId, string $name
166185

167186
$this->registerPageHitSpy();
168187

169-
$this->dispatch("catalog/category/view/id/{$categoryId}");
188+
$this->dispatchHttpRequest("catalog/category/view/id/{$categoryId}");
170189
$response = $this->getResponse();
171190
$this->assertEquals(200, $response->getHttpResponseCode(), 'Request failed');
172191
}
@@ -194,7 +213,7 @@ public function testCategoryPlpMissBackendRenderWhiteList(int $categoryId, strin
194213

195214
$_SERVER['HTTP_USER_AGENT'] = $testUserAgent;
196215

197-
$this->dispatch($this->url . $categoryId);
216+
$this->dispatchHttpRequest($this->url . $categoryId);
198217
$response = $this->getResponse();
199218
$this->assertEquals(200, $response->getHttpResponseCode(), 'Request failed');
200219
$this->assertEquals(

0 commit comments

Comments
 (0)