Skip to content
Permalink
Browse files

Allow '' to be a valid key for Hash, and Session

By removing a bunch of empty() guards we can make '' behave like all the
other key names. This does change the existing behavior/tests around ''
key, but I think that is ok given the need to manipulate ''.

Refs #9632
  • Loading branch information...
markstory committed Oct 21, 2016
1 parent 40c32e8 commit 816801902eb30d5a44ca1580e17df7f27acb30d4
@@ -230,9 +230,12 @@ public static function started() {
* @return bool True if variable is there
*/
public static function check($name) {
if (empty($name) || !static::_hasSession() || !static::start()) {
if (!static::_hasSession() || !static::start()) {
return false;
}
if (isset($_SESSION[$name])) {
return true;
}
return Hash::get($_SESSION, $name) !== null;
}
@@ -380,9 +383,6 @@ public static function userAgent($userAgent = null) {
* session not started, or provided name not found in the session, false on failure.
*/
public static function read($name = null) {
if (empty($name) && $name !== null) {
return null;
}
if (!static::_hasSession() || !static::start()) {
return null;
}
@@ -418,7 +418,7 @@ protected static function _returnSessionVars() {
* @return bool True if the write was successful, false if the write failed
*/
public static function write($name, $value = null) {
if (empty($name) || !static::start()) {
if (!static::start()) {
return false;
}
@@ -307,9 +307,9 @@ public function testWriteArray() {
* @return void
*/
public function testWriteEmptyKey() {
$this->assertFalse(TestCakeSession::write('', 'graham'));
$this->assertFalse(TestCakeSession::write('', ''));
$this->assertFalse(TestCakeSession::write(''));
$this->assertTrue(TestCakeSession::write('', 'graham'));
$this->assertTrue(TestCakeSession::write('', ''));
$this->assertTrue(TestCakeSession::write(''));
}
/**
@@ -403,6 +403,17 @@ public function testDelete() {
$this->assertFalse(TestCakeSession::check('Clearing'));
}
/**
* test delete
*
* @return void
*/
public function testDeleteEmptyString() {
TestCakeSession::write('', 'empty string');
$this->assertTrue(TestCakeSession::delete(''));
$this->assertFalse(TestCakeSession::check(''));
}
/**
* testClear method
*
@@ -500,6 +511,10 @@ public function testKeyExploit() {
* @return void
*/
public function testReadingSavedEmpty() {
TestCakeSession::write('', 'empty string');
$this->assertTrue(TestCakeSession::check(''));
$this->assertEquals('empty string', TestCakeSession::read(''));
TestCakeSession::write('SessionTestCase', 0);
$this->assertEquals(0, TestCakeSession::read('SessionTestCase'));
@@ -511,7 +526,7 @@ public function testReadingSavedEmpty() {
$this->assertFalse(TestCakeSession::read('SessionTestCase'));
TestCakeSession::write('SessionTestCase', null);
$this->assertEquals(null, TestCakeSession::read('SessionTestCase'));
$this->assertNull(TestCakeSession::read('SessionTestCase'));
}
/**
@@ -229,6 +229,20 @@ public function testGet() {
$this->assertEquals($data[1]['Article'], $result);
}
/**
* Test that get() can extract '' key data.
*
* @return void
*/
public function testGetEmptyKey()
{
$data = array(
'' => 'some value'
);
$result = Hash::get($data, '');
$this->assertSame($data[''], $result);
}
/**
* Test get() with an invalid path
*
@@ -43,7 +43,7 @@ class Hash {
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::get
*/
public static function get(array $data, $path, $default = null) {
if (empty($data) || $path === '' || $path === null) {
if (empty($data) || $path === null) {
return $default;
}
if (is_string($path) || is_numeric($path)) {

0 comments on commit 8168019

Please sign in to comment.
You can’t perform that action at this time.