From a37136a2f44d1549fa18984d92f888a1872f9aea Mon Sep 17 00:00:00 2001 From: neznaika0 Date: Sun, 3 Nov 2024 21:16:45 +0300 Subject: [PATCH 1/4] fix: Allows get by numeric key --- system/HTTP/RequestTrait.php | 4 ++-- tests/system/HTTP/RequestTest.php | 15 +++++++++++++++ tests/system/Test/FeatureTestTraitTest.php | 20 ++++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/system/HTTP/RequestTrait.php b/system/HTTP/RequestTrait.php index 43a4f23a0e87..51de3ea5c0fc 100644 --- a/system/HTTP/RequestTrait.php +++ b/system/HTTP/RequestTrait.php @@ -249,7 +249,7 @@ public function setGlobal(string $name, $value) * * @param string $name Supergrlobal name (lowercase) * @phpstan-param 'get'|'post'|'request'|'cookie'|'server' $name - * @param array|string|null $index + * @param array|int|string|null $index * @param int|null $filter Filter constant * @param array|int|null $flags Options * @@ -290,7 +290,7 @@ public function fetchGlobal(string $name, $index = null, ?int $filter = null, $f } // Does the index contain array notation? - if (($count = preg_match_all('/(?:^[^\[]+)|\[[^]]*\]/', $index, $matches)) > 1) { + if (is_string($index) && ($count = preg_match_all('/(?:^[^\[]+)|\[[^]]*\]/', $index, $matches)) > 1) { $value = $this->globals[$name]; for ($i = 0; $i < $count; $i++) { diff --git a/tests/system/HTTP/RequestTest.php b/tests/system/HTTP/RequestTest.php index e2073268d794..2205f29dfe19 100644 --- a/tests/system/HTTP/RequestTest.php +++ b/tests/system/HTTP/RequestTest.php @@ -196,6 +196,21 @@ public function testFetchGlobalReturnsArrayValues(): void $this->assertCount(2, $result['ANNOUNCEMENTS']); } + public function testFetchGlobalReturnsWithListValues(): void + { + $post = [ + ['foo' => 0], + ['bar' => 1], + ['baz' => 2], + ]; + + $this->request->setGlobal('post', $post); + $result = $this->request->fetchGlobal('post'); + + $this->assertIsList($result); + $this->assertSame($post, $result); + } + public function testFetchGlobalWithArrayTop(): void { $post = [ diff --git a/tests/system/Test/FeatureTestTraitTest.php b/tests/system/Test/FeatureTestTraitTest.php index 360cee2d437f..47aaea07d1d6 100644 --- a/tests/system/Test/FeatureTestTraitTest.php +++ b/tests/system/Test/FeatureTestTraitTest.php @@ -580,6 +580,26 @@ public function testCallWithJsonRequest(): void $response->assertJSONExact($data); } + public function testCallWithListJsonRequest(): void + { + $this->withRoutes([ + [ + 'POST', + 'home', + '\Tests\Support\Controllers\Popcorn::echoJson', + ], + ]); + $data = [ + ['one' => 1, 'two' => 2], + ['one' => 3, 'two' => 4], + ]; + $response = $this->withBodyFormat('json') + ->call(Method::POST, 'home', $data); + + $response->assertOK(); + $response->assertJSONExact($data); + } + public function testSetupRequestBodyWithParams(): void { $request = $this->setupRequest('post', 'home'); From 8a1ca7b2afb7ed0d0d92e7e43829652afa24b3a5 Mon Sep 17 00:00:00 2001 From: neznaika0 Date: Sun, 3 Nov 2024 21:17:09 +0300 Subject: [PATCH 2/4] docs: Update changelog --- user_guide_src/source/changelogs/v4.5.6.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/user_guide_src/source/changelogs/v4.5.6.rst b/user_guide_src/source/changelogs/v4.5.6.rst index 2ea1f1ce46b7..9277017522ad 100644 --- a/user_guide_src/source/changelogs/v4.5.6.rst +++ b/user_guide_src/source/changelogs/v4.5.6.rst @@ -29,6 +29,10 @@ Deprecations ********** Bugs Fixed ********** +- **FeatureTestTrait:** Allows `#call` and `#post` to process JSON lists. It failed because `RequestTrait#fetchGlobal` expected the second parameter ($index) to be an associative array. + +- **RequestTrait:** Allows to get a value by a numeric key if the data is stored as a list and not an associative array. + - **Session Library:** The session initialization debug message now uses the correct log type "debug" instead of "info". - **Validation:** Fixed the `getValidated()` method that did not return valid data when validation rules used multiple asterisks. From 342296523e15eb989fcc08b414e0fca01f0de156 Mon Sep 17 00:00:00 2001 From: neznaika0 Date: Mon, 4 Nov 2024 10:38:09 +0300 Subject: [PATCH 3/4] refactor: Add indexes --- tests/system/HTTP/RequestTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/system/HTTP/RequestTest.php b/tests/system/HTTP/RequestTest.php index 2205f29dfe19..6659c6966861 100644 --- a/tests/system/HTTP/RequestTest.php +++ b/tests/system/HTTP/RequestTest.php @@ -199,9 +199,9 @@ public function testFetchGlobalReturnsArrayValues(): void public function testFetchGlobalReturnsWithListValues(): void { $post = [ - ['foo' => 0], - ['bar' => 1], - ['baz' => 2], + 0 => ['foo' => 0], + 1 => ['bar' => 1], + 2 => ['baz' => 2], ]; $this->request->setGlobal('post', $post); From 2642a9b74c36154214328e74a036c416ced0f010 Mon Sep 17 00:00:00 2001 From: neznaika0 Date: Mon, 4 Nov 2024 10:39:00 +0300 Subject: [PATCH 4/4] docs: Clarify the log --- user_guide_src/source/changelogs/v4.5.6.rst | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/user_guide_src/source/changelogs/v4.5.6.rst b/user_guide_src/source/changelogs/v4.5.6.rst index 9277017522ad..8a486ae9de3f 100644 --- a/user_guide_src/source/changelogs/v4.5.6.rst +++ b/user_guide_src/source/changelogs/v4.5.6.rst @@ -29,13 +29,12 @@ Deprecations ********** Bugs Fixed ********** -- **FeatureTestTrait:** Allows `#call` and `#post` to process JSON lists. It failed because `RequestTrait#fetchGlobal` expected the second parameter ($index) to be an associative array. - -- **RequestTrait:** Allows to get a value by a numeric key if the data is stored as a list and not an associative array. +- **RequestTrait:** Fixed a bug where the ``fetchGlobal()`` method did not allow handling data by numeric key when stored as a list. - **Session Library:** The session initialization debug message now uses the correct log type "debug" instead of "info". - **Validation:** Fixed the `getValidated()` method that did not return valid data when validation rules used multiple asterisks. + - **Database:** Fixed the case insensitivity option in the ``like()`` method when dealing with accented characters. - **Parser:** Fixed bug that caused equal key names to be replaced by the key name defined first.