From 07006c9e4a7d7b354bd65213c4623c1bcedb48b7 Mon Sep 17 00:00:00 2001 From: nate Date: Sun, 27 May 2007 04:10:29 +0000 Subject: [PATCH] Allowing spaces in session keys (Ticket #2639) git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5208 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/session.php | 6 ++--- cake/tests/cases/libs/session.test.php | 25 +++++++++++++------ cake/tests/cases/libs/set.test.php | 34 ++++++++++++++++++++------ 3 files changed, 46 insertions(+), 19 deletions(-) diff --git a/cake/libs/session.php b/cake/libs/session.php index 59add7ed2..8db6eab11 100644 --- a/cake/libs/session.php +++ b/cake/libs/session.php @@ -181,13 +181,13 @@ class CakeSession extends Object { * @access private */ function __sessionVarNames($name) { - if (is_string($name) && preg_match("/^[0-9a-zA-Z._-]+$/", $name)) { + if (is_string($name) && preg_match("/^[ 0-9a-zA-Z._-]*$/", $name)) { if (strpos($name, ".")) { $names = explode(".", $name); } else { $names = array($name); } - $expression="\$_SESSION"; + $expression = "\$_SESSION"; foreach($names as $item) { $expression .= is_numeric($item) ? "[$item]" : "['$item']"; } @@ -538,7 +538,7 @@ class CakeSession extends Object { * @access private */ function __validateKeys($name) { - if (is_string($name) && preg_match("/^[0-9a-zA-Z._-]+$/", $name)) { + if (is_string($name) && preg_match("/^[ 0-9a-zA-Z._-]*$/", $name)) { return $name; } $this->__setError(3, "$name is not a string"); diff --git a/cake/tests/cases/libs/session.test.php b/cake/tests/cases/libs/session.test.php index b502aeaa2..8c0bc17a8 100644 --- a/cake/tests/cases/libs/session.test.php +++ b/cake/tests/cases/libs/session.test.php @@ -52,19 +52,28 @@ class SessionTest extends UnitTestCase { } function testCheckingSavedEmpty() { - $this->Session->write('SessionTestCase', 0); + $this->assertTrue($this->Session->write('SessionTestCase', 0)); + $this->assertTrue($this->Session->check('SessionTestCase')); + + $this->assertTrue($this->Session->write('SessionTestCase', '0')); $this->assertTrue($this->Session->check('SessionTestCase')); - $this->Session->write('SessionTestCase', '0'); + $this->assertTrue($this->Session->write('SessionTestCase', false)); $this->assertTrue($this->Session->check('SessionTestCase')); - $this->Session->write('SessionTestCase', false); - $this->assertTrue($this->Session->check('SessionTestCase')); - - $this->Session->write('SessionTestCase', null); + $this->assertTrue($this->Session->write('SessionTestCase', null)); $this->assertFalse($this->Session->check('SessionTestCase')); } - + + function testCheckKeyWithSpaces() { + $this->assertTrue($this->Session->write('Session Test', "test")); + $this->assertEqual($this->Session->check('Session Test'), 'test'); + $this->Session->del('Session Test'); + + $this->assertTrue($this->Session->write('Session Test.Test Case', "test")); + $this->assertTrue($this->Session->check('Session Test.Test Case')); + } + function testReadingSavedEmpty() { $this->Session->write('SessionTestCase', 0); $this->assertEqual($this->Session->read('SessionTestCase'), 0); @@ -79,7 +88,7 @@ class SessionTest extends UnitTestCase { $this->Session->write('SessionTestCase', null); $this->assertEqual($this->Session->read('SessionTestCase'), null); } - + function tearDown() { $this->Session->del('SessionTestCase'); unset($this->Session); diff --git a/cake/tests/cases/libs/set.test.php b/cake/tests/cases/libs/set.test.php index 5a2c490ca..df03611cd 100644 --- a/cake/tests/cases/libs/set.test.php +++ b/cake/tests/cases/libs/set.test.php @@ -134,19 +134,13 @@ class SetTest extends UnitTestCase { ); $this->assertIdentical(Set::merge($a, $b, $c), $expected); - // Test that passing in an empty array does not mess things up $this->assertIdentical(Set::merge($a, $b, array(), $c), $expected); - // Create a new Set instance from the $a array $Set =& new Set($a); - // Merge $b, an empty array and $c over it $r = $Set->merge($b, array(), $c); - // And test that it produces the same result as a static call would $this->assertIdentical($r, $expected); - // And also updates it's own value property $this->assertIdentical($Set->value, $expected); - // Let the garbage collector eat the Set instance unset($Set); $Set =& new Set(); @@ -156,9 +150,7 @@ class SetTest extends UnitTestCase { $SetC =& new Set($c); $r = $Set->merge($SetA, $SetB, $SetC); - // And test that it produces the same result as a static call would $this->assertIdentical($r, $expected); - // And also updates it's own value property $this->assertIdentical($Set->value, $expected); } @@ -257,6 +249,32 @@ class SetTest extends UnitTestCase { ); $this->assertIdentical($result, $expected); } + + function testCheck() { + $set = new Set(array( + 'My Index 1' => array('First' => 'The first item') + )); + $this->assertTrue($set->check('My Index 1.First')); + + $set = new Set(array( + 'My Index 1' => array('First' => array('Second' => array('Third' => array('Fourth' => 'Heavy. Nesting.')))) + )); + $this->assertTrue($set->check('My Index 1.First.Second')); + $this->assertTrue($set->check('My Index 1.First.Second.Third')); + $this->assertTrue($set->check('My Index 1.First.Second.Third.Fourth')); + } + + function testWritingWithFunkyKeys() { + $set = new Set(); + $set->insert('Session Test', "test"); + $this->assertEqual($set->extract('Session Test'), 'test'); + + $set->remove('Session Test'); + $this->assertFalse($set->check('Session Test')); + + $this->assertTrue($set->insert('Session Test.Test Case', "test")); + $this->assertTrue($set->check('Session Test.Test Case')); + } } ?> \ No newline at end of file