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 Common::old() #1496

Merged
merged 2 commits into from
Nov 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions system/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions system/HTTP/IncomingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,9 @@ public function getOldInput(string $key)
return $value;
}
}

// return null if requested session key not found
return null;
}

/**
Expand Down
37 changes: 37 additions & 0 deletions tests/system/CommonFunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
34 changes: 31 additions & 3 deletions tests/system/HTTP/IncomingRequestTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

namespace CodeIgniter\HTTP;

use Config\App;
Expand Down Expand Up @@ -84,6 +83,36 @@ public function testCanGetOldInputDotted()
$this->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
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
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()
Expand Down Expand Up @@ -319,6 +348,5 @@ public function testFileCollectionFactory()
$this->assertEquals(124, $file->getSize());
}

//--------------------------------------------------------------------

//--------------------------------------------------------------------
}
18 changes: 17 additions & 1 deletion tests/system/Session/SessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down Expand Up @@ -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();
Expand Down