From ee5f131b59f1ec9e29279d4dbc86f8e95cad83ad Mon Sep 17 00:00:00 2001 From: Instrye Date: Wed, 20 May 2020 08:54:21 +0800 Subject: [PATCH] Fix: index not exists, getPostGet and getGetPost should return null --- system/HTTP/IncomingRequest.php | 4 ++-- tests/system/HTTP/IncomingRequestTest.php | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/system/HTTP/IncomingRequest.php b/system/HTTP/IncomingRequest.php index 13031fd64774..ffe5cca85592 100755 --- a/system/HTTP/IncomingRequest.php +++ b/system/HTTP/IncomingRequest.php @@ -427,7 +427,7 @@ public function getPostGet($index = null, $filter = null, $flags = null) // Use $_POST directly here, since filter_has_var only // checks the initial POST data, not anything that might // have been added since. - return isset($_POST[$index]) ? $this->getPost($index, $filter, $flags) : (isset($_GET[$index]) ? $this->getGet($index, $filter, $flags) : $this->getPost()); + return isset($_POST[$index]) ? $this->getPost($index, $filter, $flags) : (isset($_GET[$index]) ? $this->getGet($index, $filter, $flags) : $this->getPost($index, $filter, $flags)); } //-------------------------------------------------------------------- @@ -446,7 +446,7 @@ public function getGetPost($index = null, $filter = null, $flags = null) // Use $_GET directly here, since filter_has_var only // checks the initial GET data, not anything that might // have been added since. - return isset($_GET[$index]) ? $this->getGet($index, $filter, $flags) : (isset($_POST[$index]) ? $this->getPost($index, $filter, $flags) : $this->getGet()); + return isset($_GET[$index]) ? $this->getGet($index, $filter, $flags) : (isset($_POST[$index]) ? $this->getPost($index, $filter, $flags) : $this->getGet($index, $filter, $flags)); } //-------------------------------------------------------------------- diff --git a/tests/system/HTTP/IncomingRequestTest.php b/tests/system/HTTP/IncomingRequestTest.php index 23b4b6c77d9a..111e451b654c 100644 --- a/tests/system/HTTP/IncomingRequestTest.php +++ b/tests/system/HTTP/IncomingRequestTest.php @@ -463,4 +463,15 @@ public function testWithFalseBody() $this->assertTrue($request->getBody() !== false); $this->assertTrue($request->getBody() === null); } + + /** + * @see https://github.com/codeigniter4/CodeIgniter4/issues/3020 + */ + public function testGetPostIndexNotExists() + { + $_POST['TEST'] = 5; + $_GET['TEST'] = 3; + $this->assertNull($this->request->getPostGet('gc')); + $this->assertNull($this->request->getGetPost('gc')); + } }