From 808d6aa87a0efbf11f167310b7242f0efcf6528f Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Sat, 1 Mar 2014 20:31:22 +1300 Subject: [PATCH] Tie test session files to browser session --- README.md | 5 ++++- _config/_config.yml | 7 +------ code/TestSessionController.php | 9 +++++++-- code/TestSessionEnvironment.php | 9 ++++++++- code/TestSessionRequestFilter.php | 5 ++++- 5 files changed, 24 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 04f69f0..82a0321 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,10 @@ Further data can be loaded from YAML fixtures or database dumps. The session is persisted in a file which is generated upon starting the session. As long as this file exists, the test session is considered in progress, both in web browsers and command-line execution. By default, the file -is stored in the webroot under `TESTS_RUNNING.js`. +is stored in the webroot under `TESTS_RUNNING-.js`. The `` value +is a random token stored in the browser session, in order to make the +test session specific to the executing browser, and allow multiple +people using their own test session in the same webroot. The module also serves as an initializer for the [SilverStripe Behat Extension](https://github.com/silverstripe-labs/silverstripe-behat-extension/). diff --git a/_config/_config.yml b/_config/_config.yml index 75ffce3..425ffd9 100644 --- a/_config/_config.yml +++ b/_config/_config.yml @@ -5,9 +5,4 @@ Injector: RequestProcessor: properties: filters: - - '%$TestSessionRequestFilter' ---- -Name: testsession-setup ---- -TestSessionEnvironment: - test_state_file: TESTS_RUNNING.json \ No newline at end of file + - '%$TestSessionRequestFilter' \ No newline at end of file diff --git a/code/TestSessionController.php b/code/TestSessionController.php index 79d68be..3384054 100644 --- a/code/TestSessionController.php +++ b/code/TestSessionController.php @@ -30,7 +30,7 @@ class TestSessionController extends Controller { public function __construct() { parent::__construct(); - $this->environment = Injector::inst()->get('TestSessionEnvironment'); + $this->environment = Injector::inst()->get('TestSessionEnvironment', Session::get('TestSessionId')); } public function init() { @@ -68,6 +68,10 @@ class TestSessionController extends Controller { public function start() { $params = $this->request->requestVars(); + $generator = Injector::inst()->get('RandomGenerator'); + $id = substr($generator->randomToken(), 0, 10); + Session::set('TestSessionId', $id); + // Convert datetime from form object into a single string $params = $this->fixDatetimeFormField($params); @@ -83,7 +87,7 @@ class TestSessionController extends Controller { ) ); - $this->environment->startTestSession($params); + $this->environment->startTestSession($params, $id); return $this->renderWith('TestSession_inprogress'); } @@ -255,6 +259,7 @@ class TestSessionController extends Controller { } $this->environment->endTestSession(); + Session::clear('TestSessionId'); // Clear out all PHP session states which have been set previously if($sessionStates = Session::get('_TestSessionController.BrowserSessionState')) { diff --git a/code/TestSessionEnvironment.php b/code/TestSessionEnvironment.php index 1cbbec8..32e6f21 100644 --- a/code/TestSessionEnvironment.php +++ b/code/TestSessionEnvironment.php @@ -43,7 +43,13 @@ class TestSessionEnvironment extends Object { * @var string Path (from web-root) to the test state file that indicates a testsession is in progress. * Defaults to value stored in testsession/_config/_config.yml */ - private static $test_state_file; + private static $test_state_file = 'TESTS_RUNNING.json'; + + /** + * @config + * @var [type] + */ + private static $test_state_id_file = 'TESTS_RUNNING-%s.json'; public function __construct($id = null) { parent::__construct(); @@ -84,6 +90,7 @@ class TestSessionEnvironment extends Object { */ public function startTestSession($state, $id = null) { $this->removeStateFile(); + $this->id = $id; $extendedState = $this->extend('onBeforeStartTestSession', $state); diff --git a/code/TestSessionRequestFilter.php b/code/TestSessionRequestFilter.php index f672af1..cc3cc21 100644 --- a/code/TestSessionRequestFilter.php +++ b/code/TestSessionRequestFilter.php @@ -10,7 +10,10 @@ class TestSessionRequestFilter { protected $testSessionEnvironment; public function __construct() { - $this->testSessionEnvironment = Injector::inst()->get('testSessionEnvironment'); + $this->testSessionEnvironment = Injector::inst()->get( + 'TestSessionEnvironment', + Session::get('TestSessionId') + ); } public function preRequest($req, $session, $model) {