Merge pull request #10 from silverstripe-labs/pulls/php-session-state

Allow setting PHP session state
This commit is contained in:
Ingo Schommer 2014-02-27 15:10:25 +13:00
commit adc3ac5c38
2 changed files with 38 additions and 0 deletions

View File

@ -2,6 +2,9 @@
## Overview
*IMPORTANT: This module is intended for development and testing, it poses a security risk if used on production servers.*
*It's completely possible to allow any user to become an admin, or do other nefarious things, if this is installed on a live site*
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(
@ -232,6 +256,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');
}