2010-04-12 05:32:28 +02:00
|
|
|
<?php
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use SilverStripe\Core\Injector\Injector;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\Control\Session;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-04-12 05:32:28 +02:00
|
|
|
/**
|
|
|
|
* Tests to cover the {@link Session} class
|
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2010-04-12 05:32:28 +02:00
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
|
|
|
|
class SessionTest extends SapphireTest {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testGetSetBasics() {
|
2010-04-12 05:32:28 +02:00
|
|
|
Session::set('Test', 'Test');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-12 05:32:28 +02:00
|
|
|
$this->assertEquals(Session::get('Test'), 'Test');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testClearElement() {
|
2010-04-12 05:32:28 +02:00
|
|
|
Session::set('Test', 'Test');
|
|
|
|
Session::clear('Test');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-12 05:32:28 +02:00
|
|
|
$this->assertEquals(Session::get('Test'), '');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testClearAllElements() {
|
2010-04-12 05:32:28 +02:00
|
|
|
Session::set('Test', 'Test');
|
|
|
|
Session::set('Test-1', 'Test-1');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 04:50:43 +02:00
|
|
|
Session::clear_all();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-12 05:32:28 +02:00
|
|
|
// should session get return null? The array key should probably be
|
|
|
|
// unset from the data array
|
|
|
|
$this->assertEquals(Session::get('Test'), '');
|
|
|
|
$this->assertEquals(Session::get('Test-1'), '');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testGetAllElements() {
|
2010-10-19 05:38:31 +02:00
|
|
|
Session::clear_all(); // Remove all session that might've been set by the test harness
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-12 05:32:28 +02:00
|
|
|
Session::set('Test', 'Test');
|
|
|
|
Session::set('Test-2', 'Test-2');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 04:50:43 +02:00
|
|
|
$session = Session::get_all();
|
2013-05-25 08:49:59 +02:00
|
|
|
unset($session['HTTP_USER_AGENT']);
|
|
|
|
|
2010-04-12 05:32:28 +02:00
|
|
|
$this->assertEquals($session, array('Test' => 'Test', 'Test-2' => 'Test-2'));
|
|
|
|
}
|
2011-03-22 22:47:22 +01:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSettingExistingDoesntClear() {
|
2016-08-19 00:51:35 +02:00
|
|
|
$s = Injector::inst()->create('SilverStripe\\Control\\Session', array('something' => array('does' => 'exist')));
|
2012-06-17 23:46:52 +02:00
|
|
|
|
|
|
|
$s->inst_set('something.does', 'exist');
|
2013-05-25 08:49:59 +02:00
|
|
|
$result = $s->inst_changedData();
|
|
|
|
unset($result['HTTP_USER_AGENT']);
|
|
|
|
$this->assertEquals(array(), $result);
|
2012-06-17 23:46:52 +02:00
|
|
|
}
|
|
|
|
|
2012-04-26 06:43:58 +02:00
|
|
|
/**
|
|
|
|
* Check that changedData isn't populated with junk when clearing non-existent entries.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testClearElementThatDoesntExist() {
|
2016-08-19 00:51:35 +02:00
|
|
|
$s = Injector::inst()->create('SilverStripe\\Control\\Session', array('something' => array('does' => 'exist')));
|
2012-04-26 06:43:58 +02:00
|
|
|
|
|
|
|
$s->inst_clear('something.doesnt.exist');
|
2013-05-25 08:49:59 +02:00
|
|
|
$result = $s->inst_changedData();
|
|
|
|
unset($result['HTTP_USER_AGENT']);
|
|
|
|
$this->assertEquals(array(), $result);
|
2012-04-26 06:43:58 +02:00
|
|
|
|
|
|
|
$s->inst_set('something-else', 'val');
|
|
|
|
$s->inst_clear('something-new');
|
2013-05-25 08:49:59 +02:00
|
|
|
$result = $s->inst_changedData();
|
|
|
|
unset($result['HTTP_USER_AGENT']);
|
|
|
|
$this->assertEquals(array('something-else' => 'val'), $result);
|
2012-04-26 06:43:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check that changedData is populated with clearing data.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testClearElementThatDoesExist() {
|
2016-08-19 00:51:35 +02:00
|
|
|
$s = Injector::inst()->create('SilverStripe\\Control\\Session', array('something' => array('does' => 'exist')));
|
2012-04-26 06:43:58 +02:00
|
|
|
|
|
|
|
$s->inst_clear('something.does');
|
2013-05-25 08:49:59 +02:00
|
|
|
$result = $s->inst_changedData();
|
|
|
|
unset($result['HTTP_USER_AGENT']);
|
|
|
|
$this->assertEquals(array('something' => array('does' => null)), $result);
|
2012-04-26 06:43:58 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testNonStandardPath(){
|
2016-08-19 00:51:35 +02:00
|
|
|
Config::inst()->update('SilverStripe\\Control\\Session', 'store_path', (realpath(dirname($_SERVER['DOCUMENT_ROOT']) . '/../session')));
|
2011-03-22 22:47:22 +01:00
|
|
|
Session::start();
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
$this->assertEquals(Config::inst()->get('SilverStripe\\Control\\Session', 'store_path'), '');
|
2011-03-22 22:47:22 +01:00
|
|
|
}
|
2013-05-25 08:49:59 +02:00
|
|
|
|
|
|
|
public function testUserAgentLockout() {
|
|
|
|
// Set a user agent
|
|
|
|
$_SERVER['HTTP_USER_AGENT'] = 'Test Agent';
|
|
|
|
|
|
|
|
// Generate our session
|
2016-08-19 00:51:35 +02:00
|
|
|
$s = Injector::inst()->create('SilverStripe\\Control\\Session', array());
|
2013-05-25 08:49:59 +02:00
|
|
|
$s->inst_set('val', 123);
|
2013-07-06 05:15:49 +02:00
|
|
|
$s->inst_finalize();
|
2013-05-25 08:49:59 +02:00
|
|
|
|
|
|
|
// Change our UA
|
|
|
|
$_SERVER['HTTP_USER_AGENT'] = 'Fake Agent';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-25 08:49:59 +02:00
|
|
|
// Verify the new session reset our values
|
2016-08-19 00:51:35 +02:00
|
|
|
$s2 = Injector::inst()->create('SilverStripe\\Control\\Session', $s);
|
2013-05-25 08:49:59 +02:00
|
|
|
$this->assertNotEquals($s2->inst_get('val'), 123);
|
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|