From 13b439fd475308d11296397a129eb1bc75c396e9 Mon Sep 17 00:00:00 2001 From: Master Yoda Date: Sat, 17 Nov 2018 09:43:32 -0800 Subject: [PATCH 1/2] Fix IncomingRequest::getOldInput() --- system/Common.php | 15 +++++---- system/HTTP/IncomingRequest.php | 3 ++ tests/system/CommonFunctionsTest.php | 37 +++++++++++++++++++++++ tests/system/HTTP/IncomingRequestTest.php | 30 ++++++++++++++++-- tests/system/Session/SessionTest.php | 18 ++++++++++- 5 files changed, 93 insertions(+), 10 deletions(-) diff --git a/system/Common.php b/system/Common.php index ae65bd41d372..1814c882d4dd 100644 --- a/system/Common.php +++ b/system/Common.php @@ -264,13 +264,13 @@ function esc($data, $context = 'html', $encoding = null) static $escaper; if (! $escaper) - { + { $escaper = new \Zend\Escaper\Escaper($encoding); } if ($encoding && $escaper->getEncoding() !== $encoding) - { - $escaper = new \Zend\Escaper\Escaper($encoding); + { + $escaper = new \Zend\Escaper\Escaper($encoding); } $data = $escaper->$method($data); @@ -745,7 +745,7 @@ function force_https(int $duration = 31536000, RequestInterface $request = null, $uri = \CodeIgniter\HTTP\URI::createURIString( $uri->getScheme(), $uri->getAuthority(true), $uri->getPath(), // Absolute URIs should use a "/" for an empty path - $uri->getQuery(), $uri->getFragment() + $uri->getQuery(), $uri->getFragment() ); // Set an HSTS header @@ -783,9 +783,12 @@ function old(string $key, $default = null, $escape = 'html') } // If the result was serialized array or string, then unserialize it for use... - if (strpos($value, 'a:') === 0 || strpos($value, 's:') === 0) + if (is_string($value)) { - $value = unserialize($value); + if (strpos($value, 'a:') === 0 || strpos($value, 's:') === 0) + { + $value = unserialize($value); + } } return $escape === false ? $value : esc($value, $escape); diff --git a/system/HTTP/IncomingRequest.php b/system/HTTP/IncomingRequest.php index 577862d45dc7..393bbe1ac483 100755 --- a/system/HTTP/IncomingRequest.php +++ b/system/HTTP/IncomingRequest.php @@ -521,6 +521,9 @@ public function getOldInput(string $key) return $value; } } + + // return null if requested session key not found + return null; } /** diff --git a/tests/system/CommonFunctionsTest.php b/tests/system/CommonFunctionsTest.php index 939522a8e588..e3705d82b406 100644 --- a/tests/system/CommonFunctionsTest.php +++ b/tests/system/CommonFunctionsTest.php @@ -289,6 +289,43 @@ public function testOldInput() $this->assertEquals('fritz', old('zibble')); // serialized parameter } + // Reference: https://github.com/codeigniter4/CodeIgniter4/issues/1492 + /** + * @runInSeparateProcess + * @preserveGlobalState disabled + */ + public function testOldInputArray() + { + $this->injectSessionMock(); + // setup from RedirectResponseTest... + $_SERVER['REQUEST_METHOD'] = 'GET'; + + $this->config = new App(); + $this->config->baseURL = 'http://example.com'; + + $this->routes = new RouteCollection(new MockFileLocator(new Autoload()), new \Config\Modules()); + Services::injectMock('routes', $this->routes); + + $this->request = new MockIncomingRequest($this->config, new URI('http://example.com'), null, new UserAgent()); + Services::injectMock('request', $this->request); + + $locations = [ + 'AB' => 'Alberta', + 'BC' => 'British Columbia', + 'SK' => 'Saskatchewan', + ]; + + // setup & ask for a redirect... + $_SESSION = []; + $_GET = []; + $_POST = ['location' => $locations]; + + $response = new RedirectResponse(new App()); + $returned = $response->withInput(); + + $this->assertEquals($locations, old('location')); + } + // ------------------------------------------------------------------------ public function testReallyWritable() diff --git a/tests/system/HTTP/IncomingRequestTest.php b/tests/system/HTTP/IncomingRequestTest.php index 7ec796b012f5..a6b73ba9686e 100644 --- a/tests/system/HTTP/IncomingRequestTest.php +++ b/tests/system/HTTP/IncomingRequestTest.php @@ -1,5 +1,4 @@ assertEquals('two', $this->request->getOldInput('apple.name')); } + // Reference: https://github.com/codeigniter4/CodeIgniter4/issues/1492 + public function testCanGetOldInputArray() + { + $_SESSION['_ci_old_input'] = [ + 'get' => ['apple' => ['name' => 'two']], + 'post' => ['banana' => ['name' => 'foo']], + ]; + + $this->assertEquals(['name' => 'two'], $this->request->getOldInput('apple')); + $this->assertEquals(['name' => 'foo'], $this->request->getOldInput('banana')); + } + + // Reference: https://github.com/codeigniter4/CodeIgniter4/issues/1492 + public function testCanSerializeOldArray() + { + $locations = [ + 'AB' => 'Alberta', + 'BC' => 'British Columbia', + 'SK' => 'Saskatchewan', + ]; + $session = service('session'); + $session->set(['_ci_old_input' => ['post' => ['location' => $locations]]]); + + $this->assertEquals($locations, $this->request->getOldInput('location')); + } + //-------------------------------------------------------------------- public function testCanGrabServerVars() @@ -319,6 +344,5 @@ public function testFileCollectionFactory() $this->assertEquals(124, $file->getSize()); } - //-------------------------------------------------------------------- - + //-------------------------------------------------------------------- } diff --git a/tests/system/Session/SessionTest.php b/tests/system/Session/SessionTest.php index 3064fd13d9d3..f7ecb9f4a3f5 100644 --- a/tests/system/Session/SessionTest.php +++ b/tests/system/Session/SessionTest.php @@ -40,7 +40,7 @@ protected function getInstance($options = []) ]; $config = array_merge($defaults, $options); - $config = (object)$config; + $config = (object) $config; $session = new MockSession(new FileHandler($config, '127.0.0.1'), $config); $session->setLogger(new TestLogger(new Logger())); @@ -93,6 +93,22 @@ public function testCanSetArray() $this->assertArrayNotHasKey('__ci_vars', $_SESSION); } + // Reference: https://github.com/codeigniter4/CodeIgniter4/issues/1492 + public function testCanSerializeArray() + { + $session = $this->getInstance(); + $session->start(); + + $locations = [ + 'AB' => 'Alberta', + 'BC' => 'British Columbia', + 'SK' => 'Saskatchewan', + ]; + $session->set(['_ci_old_input' => ['location' => $locations]]); + + $this->assertEquals($locations, $session->get('_ci_old_input')['location']); + } + public function testGetSimpleKey() { $session = $this->getInstance(); From e1be4261d5324f68097fc7a41813b78e2e53043f Mon Sep 17 00:00:00 2001 From: Master Yoda Date: Sat, 17 Nov 2018 10:20:04 -0800 Subject: [PATCH 2/2] Session test needs to be independent --- tests/system/HTTP/IncomingRequestTest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/system/HTTP/IncomingRequestTest.php b/tests/system/HTTP/IncomingRequestTest.php index a6b73ba9686e..395593744038 100644 --- a/tests/system/HTTP/IncomingRequestTest.php +++ b/tests/system/HTTP/IncomingRequestTest.php @@ -96,6 +96,10 @@ public function testCanGetOldInputArray() } // Reference: https://github.com/codeigniter4/CodeIgniter4/issues/1492 + /** + * @runInSeparateProcess + * @preserveGlobalState disabled + */ public function testCanSerializeOldArray() { $locations = [