silverstripe-framework/tests/control/SessionTest.php

120 lines
3.4 KiB
PHP
Raw Normal View History

<?php
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Control\Session;
/**
* Tests to cover the {@link Session} class
*
* @package framework
* @subpackage tests
*/
class SessionTest extends SapphireTest {
2014-08-15 08:53:05 +02:00
public function testGetSetBasics() {
Session::set('Test', 'Test');
2014-08-15 08:53:05 +02:00
$this->assertEquals(Session::get('Test'), 'Test');
}
2014-08-15 08:53:05 +02:00
public function testClearElement() {
Session::set('Test', 'Test');
Session::clear('Test');
2014-08-15 08:53:05 +02:00
$this->assertEquals(Session::get('Test'), '');
}
2014-08-15 08:53:05 +02:00
public function testClearAllElements() {
Session::set('Test', 'Test');
Session::set('Test-1', 'Test-1');
2014-08-15 08:53:05 +02:00
Session::clear_all();
2014-08-15 08:53:05 +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
public function testGetAllElements() {
Session::clear_all(); // Remove all session that might've been set by the test harness
2014-08-15 08:53:05 +02:00
Session::set('Test', 'Test');
Session::set('Test-2', 'Test-2');
2014-08-15 08:53:05 +02:00
$session = Session::get_all();
unset($session['HTTP_USER_AGENT']);
$this->assertEquals($session, array('Test' => 'Test', 'Test-2' => 'Test-2'));
}
public function testSettingExistingDoesntClear() {
$s = Injector::inst()->create('SilverStripe\\Control\\Session', array('something' => array('does' => 'exist')));
$s->inst_set('something.does', 'exist');
$result = $s->inst_changedData();
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() {
$s = Injector::inst()->create('SilverStripe\\Control\\Session', array('something' => array('does' => 'exist')));
$s->inst_clear('something.doesnt.exist');
$result = $s->inst_changedData();
unset($result['HTTP_USER_AGENT']);
$this->assertEquals(array(), $result);
$s->inst_set('something-else', 'val');
$s->inst_clear('something-new');
$result = $s->inst_changedData();
unset($result['HTTP_USER_AGENT']);
$this->assertEquals(array('something-else' => 'val'), $result);
}
/**
* Check that changedData is populated with clearing data.
*/
public function testClearElementThatDoesExist() {
$s = Injector::inst()->create('SilverStripe\\Control\\Session', array('something' => array('does' => 'exist')));
$s->inst_clear('something.does');
$result = $s->inst_changedData();
unset($result['HTTP_USER_AGENT']);
$this->assertEquals(array('something' => array('does' => null)), $result);
}
public function testNonStandardPath(){
Config::inst()->update('SilverStripe\\Control\\Session', 'store_path', (realpath(dirname($_SERVER['DOCUMENT_ROOT']) . '/../session')));
Session::start();
$this->assertEquals(Config::inst()->get('SilverStripe\\Control\\Session', 'store_path'), '');
}
public function testUserAgentLockout() {
// Set a user agent
$_SERVER['HTTP_USER_AGENT'] = 'Test Agent';
// Generate our session
$s = Injector::inst()->create('SilverStripe\\Control\\Session', array());
$s->inst_set('val', 123);
$s->inst_finalize();
// Change our UA
$_SERVER['HTTP_USER_AGENT'] = 'Fake Agent';
2014-08-15 08:53:05 +02:00
// Verify the new session reset our values
$s2 = Injector::inst()->create('SilverStripe\\Control\\Session', $s);
$this->assertNotEquals($s2->inst_get('val'), 123);
}
}