2010-04-12 05:32:28 +02:00
|
|
|
<?php
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Control\Tests;
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Control\Session;
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2017-06-26 01:24:50 +02:00
|
|
|
use SilverStripe\Control\HTTPRequest;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2010-04-12 05:32:28 +02:00
|
|
|
/**
|
|
|
|
* Tests to cover the {@link Session} class
|
|
|
|
*/
|
2016-12-16 05:34:21 +01:00
|
|
|
class SessionTest extends SapphireTest
|
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
/**
|
|
|
|
* @var Session
|
|
|
|
*/
|
|
|
|
protected $session = null;
|
|
|
|
|
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
$this->session = new Session([]);
|
|
|
|
return parent::setUp();
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
public function testGetSetBasics()
|
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
$this->session->set('Test', 'Test');
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
$this->assertEquals($this->session->get('Test'), 'Test');
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testClearElement()
|
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
$this->session->set('Test', 'Test');
|
|
|
|
$this->session->clear('Test');
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
$this->assertEquals($this->session->get('Test'), '');
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testClearAllElements()
|
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
$this->session->set('Test', 'Test');
|
|
|
|
$this->session->set('Test-1', 'Test-1');
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
$this->session->clearAll();
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
// should session get return null? The array key should probably be
|
|
|
|
// unset from the data array
|
2017-06-22 12:50:45 +02:00
|
|
|
$this->assertEquals($this->session->get('Test'), '');
|
|
|
|
$this->assertEquals($this->session->get('Test-1'), '');
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetAllElements()
|
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
$this->session->clearAll(); // Remove all session that might've been set by the test harness
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
$this->session->set('Test', 'Test');
|
|
|
|
$this->session->set('Test-2', 'Test-2');
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
$session = $this->session->getAll();
|
2016-12-16 05:34:21 +01:00
|
|
|
unset($session['HTTP_USER_AGENT']);
|
|
|
|
|
|
|
|
$this->assertEquals($session, array('Test' => 'Test', 'Test-2' => 'Test-2'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSettingExistingDoesntClear()
|
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
$s = new Session(array('something' => array('does' => 'exist')));
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
$s->set('something.does', 'exist');
|
|
|
|
$result = $s->changedData();
|
2016-12-16 05:34:21 +01:00
|
|
|
unset($result['HTTP_USER_AGENT']);
|
|
|
|
$this->assertEquals(array(), $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check that changedData isn't populated with junk when clearing non-existent entries.
|
|
|
|
*/
|
|
|
|
public function testClearElementThatDoesntExist()
|
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
$s = new Session(array('something' => array('does' => 'exist')));
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
$s->clear('something.doesnt.exist');
|
|
|
|
$result = $s->changedData();
|
2016-12-16 05:34:21 +01:00
|
|
|
unset($result['HTTP_USER_AGENT']);
|
|
|
|
$this->assertEquals(array(), $result);
|
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
$s->set('something-else', 'val');
|
|
|
|
$s->clear('something-new');
|
|
|
|
$result = $s->changedData();
|
2016-12-16 05:34:21 +01:00
|
|
|
unset($result['HTTP_USER_AGENT']);
|
|
|
|
$this->assertEquals(array('something-else' => 'val'), $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check that changedData is populated with clearing data.
|
|
|
|
*/
|
|
|
|
public function testClearElementThatDoesExist()
|
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
$s = new Session(array('something' => array('does' => 'exist')));
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
$s->clear('something.does');
|
|
|
|
$result = $s->changedData();
|
2016-12-16 05:34:21 +01:00
|
|
|
unset($result['HTTP_USER_AGENT']);
|
|
|
|
$this->assertEquals(array('something' => array('does' => null)), $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUserAgentLockout()
|
|
|
|
{
|
|
|
|
// Set a user agent
|
2017-06-26 01:24:50 +02:00
|
|
|
$req1 = new HTTPRequest('GET', '/');
|
2017-06-25 05:12:29 +02:00
|
|
|
$req1->addHeader('User-Agent', 'Test Agent');
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
// Generate our session
|
2017-06-22 12:50:45 +02:00
|
|
|
$s = new Session(array());
|
2017-06-26 01:24:50 +02:00
|
|
|
$s->init($req1);
|
2017-06-22 12:50:45 +02:00
|
|
|
$s->set('val', 123);
|
2017-06-26 01:24:50 +02:00
|
|
|
$s->finalize($req1);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
// Change our UA
|
2017-06-26 01:24:50 +02:00
|
|
|
$req2 = new HTTPRequest('GET', '/');
|
2017-06-25 05:12:29 +02:00
|
|
|
$req2->addHeader('User-Agent', 'Fake Agent');
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
// Verify the new session reset our values
|
2017-06-22 12:50:45 +02:00
|
|
|
$s2 = new Session($s);
|
2017-06-26 01:24:50 +02:00
|
|
|
$s2->init($req2);
|
2017-06-22 12:50:45 +02:00
|
|
|
$this->assertNotEquals($s2->get('val'), 123);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|