Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: populateGlobals for JSON and XML body format #9211

Closed
wants to merge 1 commit into from

Conversation

michalsn
Copy link
Member

@michalsn michalsn commented Oct 2, 2024

Description
This PR fixes a bug in the populateGlobals() method in the FeatureTestTrait class.
If the body format is set to json or xml, we should not use setGlobal() for post requests.
The POST array should be empty since the data is stored in the raw input stream.

I think this is the proper way to solve the problem, although if anyone sees any issues, feedback is welcome (as always).

Closes #9204

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@kenjis kenjis added the bug Verified issues on the current code behavior or pull requests that will fix them label Oct 3, 2024
Co-authored-by: Felix Freeman <libsys@hacktivista.org>
@michalsn michalsn force-pushed the fix/featureTestTrait branch from 639b7bc to f5d98b8 Compare October 4, 2024 07:46
@neznaika0
Copy link
Contributor

I think there is a potential error in fetchGlobal() - it does not understand the numeric keys $_GET[0]['one'] = 1

@neznaika0
Copy link
Contributor

In any case, $_GET is set to $request->setGlobal('get', $get), which means $_REQUEST must have values from $_GET. Your change is not necessary.

See other test:

diff --git a/system/HTTP/RequestTrait.php b/system/HTTP/RequestTrait.php
index 43a4f23a0e..a47d4c7a69 100644
--- a/system/HTTP/RequestTrait.php
+++ b/system/HTTP/RequestTrait.php
@@ -290,7 +290,7 @@ trait RequestTrait
         }
 
         // 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 e2073268d7..2205f29dfe 100644
--- a/tests/system/HTTP/RequestTest.php
+++ b/tests/system/HTTP/RequestTest.php
@@ -196,6 +196,21 @@ final class RequestTest extends CIUnitTestCase
         $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 = [

@michalsn
Copy link
Member Author

michalsn commented Nov 3, 2024

@neznaika0 That makes sense. I haven't seen the real problem. Nice!

Would you like to send a PR?

@michalsn michalsn closed this Nov 3, 2024
@michalsn michalsn deleted the fix/featureTestTrait branch December 31, 2024 13:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants