Allow setting PHP session state

This commit is contained in:
Ingo Schommer 2014-02-26 13:24:59 +13:00
parent e198f738b4
commit 54a8e673a6
2 changed files with 38 additions and 0 deletions

View File

@ -2,6 +2,9 @@
## Overview
*IMPORTANT: This module poses a security risk if used on production servers.*
*It is a testing module not intended for production use.*
This module starts a testing session in a browser,
in order to test a SilverStripe application in a clean state.
Usually the session is started on a fresh database with only default records loaded.
@ -39,6 +42,8 @@ Commands:
* `dev/testsession/end`: Removes the test state, and resets to the original database.
* `dev/testsession/loadfixture?fixture=<path>`: Loads a fixture into an existing test state.
* `dev/testsession/clear`: Empties the test state.
* `dev/testsession/browsersessionstate`: Set or unset browser session state (different from test session state).
Use query parameters to define states.
While you can use the interface to set the test session state,
it can be useful to set them programmatically through query parameters

View File

@ -10,6 +10,7 @@ class TestSessionController extends Controller {
'set',
'end',
'clear',
'browsersessionstate',
'StartForm',
'ProgressForm',
);
@ -87,6 +88,29 @@ class TestSessionController extends Controller {
return $this->renderWith('TestSession_inprogress');
}
/**
* Set $_SESSION state for the current browser session.
*/
public function browsersessionstate($request) {
if(!$this->environment->isRunningTests()) {
throw new LogicException("No test session in progress.");
}
$newSessionStates = array_diff_key($request->getVars(), array('url' => true));
if(!$newSessionStates) {
throw new LogicException('No query parameters detected');
}
$sessionStates = (array)Session::get('_TestSessionController.BrowserSessionState');
foreach($newSessionStates as $k => $v) {
Session::set($k, $v);
}
// Track which state we're setting so we can unset later in end()
Session::set('_TestSessionController.BrowserSessionState', array_merge($sessionStates, $newSessionStates));
}
public function StartForm() {
$databaseTemplates = $this->getDatabaseTemplates();
$fields = new FieldList(
@ -231,6 +255,15 @@ class TestSessionController extends Controller {
$this->environment->endTestSession();
// Clear out all PHP session states which have been set previously
if($sessionStates = Session::get('_TestSessionController.BrowserSessionState')) {
foreach($sessionStates as $k => $v) {
Session::clear($k);
}
Session::clear('_TestSessionController');
}
return $this->renderWith('TestSession_end');
}