Skip to content

Commit

Permalink
Allow '' to be a valid key for Hash/Session data.
Browse files Browse the repository at this point in the history
Port the fixes in cakephp#9635 to 3.x
  • Loading branch information
markstory authored and ndm2 committed Nov 2, 2016
1 parent ff2dc01 commit b443fd0
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 17 deletions.
12 changes: 0 additions & 12 deletions src/Network/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,10 +355,6 @@ public function started()
*/
public function check($name = null)
{
if (empty($name)) {
return false;
}

if ($this->_hasSession() && !$this->started()) {
$this->start();
}
Expand All @@ -379,10 +375,6 @@ public function check($name = null)
*/
public function read($name = null)
{
if (empty($name) && $name !== null) {
return null;
}

if ($this->_hasSession() && !$this->started()) {
$this->start();
}
Expand Down Expand Up @@ -427,10 +419,6 @@ public function consume($name)
*/
public function write($name, $value = null)
{
if (empty($name)) {
return;
}

if (!$this->started()) {
$this->start();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Utility/Hash.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static function get($data, $path, $default = null)
);
}

if (empty($data) || $path === null || $path === '') {
if (empty($data) || $path === null) {
return $default;
}

Expand Down
40 changes: 36 additions & 4 deletions tests/TestCase/Network/SessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,11 @@ public function testCheck()
}

/**
* testSimpleRead method
* test read with simple values
*
* @return void
*/
public function testSimpleRead()
public function testReadSimple()
{
$session = new Session();
$session->write('testing', '1,2,3');
Expand All @@ -181,7 +181,7 @@ public function testSimpleRead()
$this->assertEquals(['1' => 'one', '2' => 'two', '3' => 'three'], $result);

$result = $session->read();
$this->assertTrue(isset($result['testing']));
$this->assertArrayHasKey('testing', $result);

$session->write('This.is.a.deep.array.my.friend', 'value');
$result = $session->read('This.is.a.deep.array');
Expand All @@ -200,7 +200,22 @@ public function testReadEmpty()
}

/**
* test writing a hash of values/
* Test writing simple keys
*
* @return void
*/
public function testWriteSimple()
{
$session = new Session();
$session->write('', 'empty');
$this->assertEquals('empty', $session->read(''));

$session->write('Simple', ['values']);
$this->assertEquals(['values'], $session->read('Simple'));
}

/**
* test writing a hash of values
*
* @return void
*/
Expand Down Expand Up @@ -334,6 +349,19 @@ public function testDelete()
$this->assertFalse($session->check('Clearing'));
}

/**
* test delete
*
* @return void
*/
public function testDeleteEmptyString()
{
$session = new Session();
$session->write('', 'empty string');
$session->delete('');
$this->assertFalse($session->check(''));
}

/**
* testDestroy method
*
Expand Down Expand Up @@ -421,6 +449,10 @@ public function testKeyExploit()
public function testReadingSavedEmpty()
{
$session = new Session();
$session->write('', 'empty string');
$this->assertTrue($session->check(''));
$this->assertEquals('empty string', $session->read(''));

$session->write('SessionTestCase', 0);
$this->assertEquals(0, $session->read('SessionTestCase'));

Expand Down
14 changes: 14 additions & 0 deletions tests/TestCase/Utility/HashTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,20 @@ public function testGet()
$this->assertEquals(1, Hash::get(new ArrayObject($data), 'a.b.c.d'));
}

/**
* Test that get() can extract '' key data.
*
* @return void
*/
public function testGetEmptyKey()
{
$data = [
'' => 'some value'
];
$result = Hash::get($data, '');
$this->assertSame($data[''], $result);
}

/**
* Test get() for invalid $data type
*
Expand Down

0 comments on commit b443fd0

Please sign in to comment.