Fix that it's not possible to set non-existing values in a session with setAll()

refs #4639
This commit is contained in:
Johannes Meyer 2014-04-09 16:18:51 +02:00
parent bd986e8c81
commit 6056327c6d
3 changed files with 63 additions and 4 deletions

View File

@ -125,8 +125,7 @@ class SessionNamespace implements IteratorAggregate
*/
public function __unset($key)
{
$this->removed[] = $key;
unset($this->values[$key]);
$this->delete($key);
}
/**
@ -161,6 +160,17 @@ class SessionNamespace implements IteratorAggregate
return isset($this->values[$key]) ? $this->values[$key] : $default;
}
/**
* Delete the given value from the session
*
* @param string $key The value's name
*/
public function delete($key)
{
$this->removed[] = $key;
unset($this->values[$key]);
}
/**
* Getter for all session values
*
@ -180,7 +190,7 @@ class SessionNamespace implements IteratorAggregate
public function setAll(array $values, $overwrite = false)
{
foreach ($values as $key => $value) {
if ($this->get($key) !== $value && !$overwrite) {
if ($this->get($key, $value) !== $value && !$overwrite) {
continue;
}
$this->set($key, $value);

View File

@ -88,7 +88,7 @@ class PhpSessionTest extends BaseTestCase
}
/**
* Test whether session namespaces are properly written and loaded
* Test whether session namespaces are properly written, cleared and loaded
*
* @runInSeparateProcess
*/
@ -100,9 +100,45 @@ class PhpSessionTest extends BaseTestCase
$namespace->set('an_array', array(1, 2, 3));
$session->write();
$session->clear();
$this->assertFalse($session->hasNamespace('test'));
$session->read();
$namespace = $session->getNamespace('test');
$this->assertEquals($namespace->get('some_key'), 'some_val');
$this->assertEquals($namespace->get('an_array'), array(1, 2, 3));
}
/**
* Test whether session values are properly removed
*
* @runInSeparateProcess
*/
public function testValueRemoval()
{
$session = $this->getSession();
$session->set('key', 'value');
$session->write();
$session->delete('key');
$session->write();
$session->clear();
$session->read();
$this->assertNull($session->get('key'));
}
/**
* Test whether session namespaces are properly removed
*
* @runInSeparateProcess
*/
public function testNamespaceRemoval()
{
$session = $this->getSession();
$namespace = $session->getNamespace('test');
$namespace->key = 'value';
$session->write();
$session->removeNamespace('test');
$session->write();
$session->clear();
$session->read();
$this->assertFalse($session->hasNamespace('test'));
}
}

View File

@ -98,4 +98,17 @@ class SessionNamespaceTest extends BaseTestCase
$ns = new SessionNamespace();
$ns->missing;
}
/**
* Check whether iterating over session namespaces works
*/
public function testIteration()
{
$ns = new SessionNamespace();
$values = array('key1' => 'val1', 'key2' => 'val2');
$ns->setAll($values);
foreach ($ns as $key => $value) {
$this->assertEquals($value, $values[$key]);
}
}
}