API Replace request filter with middleware

This commit is contained in:
Damian Mooyman 2017-06-27 10:55:09 +12:00
parent 81130c7025
commit 3e08c1aad0
5 changed files with 40 additions and 21 deletions

View File

@ -1,8 +1,12 @@
--- ---
Name: testsessionprocessors Name: testsessionprocessors
After:
- requestprocessors
Before:
- coresecurity
--- ---
SilverStripe\Core\Injector\Injector: SilverStripe\Core\Injector\Injector:
SilverStripe\Control\RequestProcessor: SilverStripe\Control\Director:
properties: properties:
filters: Middlewares:
- '%$SilverStripe\TestSession\TestSessionRequestFilter' TestSessionHTTPMiddleware: %$SilverStripe\TestSession\TestSessionHTTPMiddleware

View File

@ -20,7 +20,7 @@
}, },
"extra": { "extra": {
"branch-alias": { "branch-alias": {
"dev-master": "2.x-dev" "dev-master": "2.0.x-dev"
} }
}, },
"scripts": { "scripts": {

View File

@ -2,11 +2,12 @@
namespace SilverStripe\TestSession; namespace SilverStripe\TestSession;
use FilesystemIterator;
use LogicException;
use SilverStripe\Control\Controller; use SilverStripe\Control\Controller;
use SilverStripe\Control\Director; use SilverStripe\Control\Director;
use SilverStripe\Control\HTTPRequest; use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\Injector\Injector; use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\CheckboxField; use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\DatetimeField; use SilverStripe\Forms\DatetimeField;
use SilverStripe\Forms\DropdownField; use SilverStripe\Forms\DropdownField;
@ -24,8 +25,6 @@ use SilverStripe\Security\RandomGenerator;
use SilverStripe\Security\Security; use SilverStripe\Security\Security;
use SilverStripe\View\ArrayData; use SilverStripe\View\ArrayData;
use SilverStripe\View\Requirements; use SilverStripe\View\Requirements;
use LogicException;
use FilesystemIterator;
/** /**
* Requires PHP's mycrypt extension in order to set the database name as an encrypted cookie. * Requires PHP's mycrypt extension in order to set the database name as an encrypted cookie.

View File

@ -85,7 +85,7 @@ class TestSessionEnvironment
public function init(HTTPRequest $request) public function init(HTTPRequest $request)
{ {
if (!$this->id) { if (!$this->id) {
$request->getSession()->init(); $request->getSession()->init($request);
// $_SESSION != Session::get() in some execution paths, suspect Controller->pushCurrent() // $_SESSION != Session::get() in some execution paths, suspect Controller->pushCurrent()
// as part of the issue, easiest resolution is to use session directly for now // as part of the issue, easiest resolution is to use session directly for now
$this->id = $request->getSession()->get('TestSessionId'); $this->id = $request->getSession()->get('TestSessionId');
@ -98,9 +98,9 @@ class TestSessionEnvironment
public function getFilePath() public function getFilePath()
{ {
if ($this->id) { if ($this->id) {
$path = Director::getAbsFile(sprintf($this->config()->test_state_id_file, $this->id)); $path = Director::getAbsFile(sprintf($this->config()->get('test_state_id_file'), $this->id));
} else { } else {
$path = Director::getAbsFile($this->config()->test_state_file); $path = Director::getAbsFile($this->config()->get('test_state_file'));
} }
return $path; return $path;

View File

@ -6,8 +6,7 @@ use SilverStripe\Control\Director;
use SilverStripe\Control\Email\Email; use SilverStripe\Control\Email\Email;
use SilverStripe\Control\Email\Mailer; use SilverStripe\Control\Email\Mailer;
use SilverStripe\Control\HTTPRequest; use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse; use SilverStripe\Control\Middleware\HTTPMiddleware;
use SilverStripe\Control\RequestFilter;
use SilverStripe\Core\Injector\Injector; use SilverStripe\Core\Injector\Injector;
use SilverStripe\ORM\DB; use SilverStripe\ORM\DB;
use SilverStripe\ORM\FieldType\DBDatetime; use SilverStripe\ORM\FieldType\DBDatetime;
@ -15,7 +14,7 @@ use SilverStripe\ORM\FieldType\DBDatetime;
/** /**
* Sets state previously initialized through {@link TestSessionController}. * Sets state previously initialized through {@link TestSessionController}.
*/ */
class TestSessionRequestFilter implements RequestFilter class TestSessionHTTPMiddleware implements HTTPMiddleware
{ {
/** /**
* @var TestSessionEnvironment * @var TestSessionEnvironment
@ -27,14 +26,35 @@ class TestSessionRequestFilter implements RequestFilter
$this->testSessionEnvironment = TestSessionEnvironment::singleton(); $this->testSessionEnvironment = TestSessionEnvironment::singleton();
} }
public function preRequest(HTTPRequest $request) public function process(HTTPRequest $request, callable $delegate)
{ {
$isRunningTests = $this->testSessionEnvironment->isRunningTests(); // Init environment
$this->testSessionEnvironment->init($request); $this->testSessionEnvironment->init($request);
// If not running tests, just pass through
$isRunningTests = $this->testSessionEnvironment->isRunningTests();
if (!$isRunningTests) { if (!$isRunningTests) {
return; return $delegate($request);
} }
// Load test state
$this->loadTestState($request);
// Call with safe teardown
try {
return $delegate($request);
} finally {
$this->restoreTestState($request);
}
}
/**
* Load test state from environment into "real" environment
*
* @param HTTPRequest $request
*/
protected function loadTestState(HTTPRequest $request)
{
$testState = $this->testSessionEnvironment->getState(); $testState = $this->testSessionEnvironment->getState();
// Date and time // Date and time
@ -67,12 +87,8 @@ class TestSessionRequestFilter implements RequestFilter
} }
} }
public function postRequest(HTTPRequest $request, HTTPResponse $response) protected function restoreTestState(HTTPRequest $request)
{ {
if (!$this->testSessionEnvironment->isRunningTests()) {
return;
}
// Store PHP session // Store PHP session
$state = $this->testSessionEnvironment->getState(); $state = $this->testSessionEnvironment->getState();
$state->session = $request->getSession()->getAll(); $state->session = $request->getSession()->getAll();