Skip to content

Commit 5caa27b

Browse files
committed
test: cover cookie merge, request_order precedence, and stale $_REQUEST
- Make Superglobals::getRequestData() accept an optional request_order override so precedence and cookie branches can be tested deterministically. - Add tests for cookie merging, order-sensitive overwrite behavior, and unknown order types. - Add a regression test proving getVar() reflects $_GET changes even when $_REQUEST is stale.
1 parent eef87e3 commit 5caa27b

3 files changed

Lines changed: 53 additions & 2 deletions

File tree

‎system/Superglobals.php‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,14 +397,20 @@ public function setRequestArray(array $array): self
397397
* stale. This method returns the current merged values so callers can
398398
* read up-to-date request data without relying on the stale $_REQUEST.
399399
*
400+
* @param string|null $requestOrder Overrides the ini setting for testing.
401+
*
400402
* @return array<string, request_items>
401403
*/
402-
public function getRequestData(): array
404+
public function getRequestData(?string $requestOrder = null): array
403405
{
404-
$requestOrder = ini_get('request_order');
406+
if ($requestOrder === null) {
407+
$requestOrder = ini_get('request_order');
408+
}
409+
405410
if ($requestOrder === false || $requestOrder === '') {
406411
$requestOrder = ini_get('variables_order');
407412
}
413+
408414
if ($requestOrder === false || $requestOrder === '') {
409415
$requestOrder = 'GP';
410416
}

‎tests/system/HTTP/IncomingRequestTest.php‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,17 @@ public function testCanGrabRequestVars(): void
7676
$this->assertNull($this->request->getVar('TESTY'));
7777
}
7878

79+
public function testGetVarReflectsGetChangesWhenRequestIsStale(): void
80+
{
81+
// Simulate the state after SiteURIFactory updates $_GET: $_REQUEST
82+
// still holds the original value while $_GET has been refreshed.
83+
service('superglobals')
84+
->setGetArray(['code' => 'good'])
85+
->setRequestArray(['code' => 'stale']);
86+
87+
$this->assertSame('good', $this->request->getVar('code'));
88+
}
89+
7990
public function testCanGrabGetVars(): void
8091
{
8192
service('superglobals')->setGet('TEST', '5');

‎tests/system/SuperglobalsTest.php‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,40 @@ public function testGetRequestDataReflectsGetChanges(): void
325325
$this->assertSame('new', $this->superglobals->getRequestData()['key']);
326326
}
327327

328+
public function testGetRequestDataMergesCookie(): void
329+
{
330+
$this->superglobals->setGetArray(['get_key' => 'get_value']);
331+
$this->superglobals->setPostArray(['post_key' => 'post_value']);
332+
$this->superglobals->setCookieArray(['cookie_key' => 'cookie_value']);
333+
334+
$data = $this->superglobals->getRequestData('GPC');
335+
336+
$this->assertSame('get_value', $data['get_key']);
337+
$this->assertSame('post_value', $data['post_key']);
338+
$this->assertSame('cookie_value', $data['cookie_key']);
339+
}
340+
341+
public function testGetRequestDataRespectsOrder(): void
342+
{
343+
$this->superglobals->setGetArray(['shared' => 'get']);
344+
$this->superglobals->setPostArray(['shared' => 'post']);
345+
$this->superglobals->setCookieArray(['shared' => 'cookie']);
346+
347+
// Later sources overwrite earlier ones, matching PHP's request_order.
348+
$this->assertSame('post', $this->superglobals->getRequestData('GP')['shared']);
349+
$this->assertSame('cookie', $this->superglobals->getRequestData('GPC')['shared']);
350+
$this->assertSame('get', $this->superglobals->getRequestData('PG')['shared']);
351+
}
352+
353+
public function testGetRequestDataIgnoresUnknownOrderTypes(): void
354+
{
355+
$this->superglobals->setGetArray(['get_key' => 'get_value']);
356+
357+
$data = $this->superglobals->getRequestData('GX');
358+
359+
$this->assertSame(['get_key' => 'get_value'], $data);
360+
}
361+
328362
// $_FILES tests
329363
public function testFilesGetArray(): void
330364
{

0 commit comments

Comments
 (0)