mirror of
https://github.com/silverstripe/silverstripe-testsession
synced 2024-10-22 14:06:00 +02:00
Merge pull request #50 from open-sausages/pulls/4.0/update-middleware
API Replace request filter with middleware
This commit is contained in:
commit
62cd6303da
@ -1,8 +1,12 @@
|
||||
---
|
||||
Name: testsessionprocessors
|
||||
After:
|
||||
- requestprocessors
|
||||
Before:
|
||||
- coresecurity
|
||||
---
|
||||
SilverStripe\Core\Injector\Injector:
|
||||
SilverStripe\Control\RequestProcessor:
|
||||
SilverStripe\Control\Director:
|
||||
properties:
|
||||
filters:
|
||||
- '%$SilverStripe\TestSession\TestSessionRequestFilter'
|
||||
Middlewares:
|
||||
TestSessionHTTPMiddleware: %$SilverStripe\TestSession\TestSessionHTTPMiddleware
|
||||
|
@ -20,7 +20,7 @@
|
||||
},
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "2.x-dev"
|
||||
"dev-master": "2.0.x-dev"
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
|
@ -2,11 +2,12 @@
|
||||
|
||||
namespace SilverStripe\TestSession;
|
||||
|
||||
use FilesystemIterator;
|
||||
use LogicException;
|
||||
use SilverStripe\Control\Controller;
|
||||
use SilverStripe\Control\Director;
|
||||
use SilverStripe\Control\HTTPRequest;
|
||||
use SilverStripe\Core\Injector\Injector;
|
||||
use SilverStripe\Dev\SapphireTest;
|
||||
use SilverStripe\Forms\CheckboxField;
|
||||
use SilverStripe\Forms\DatetimeField;
|
||||
use SilverStripe\Forms\DropdownField;
|
||||
@ -24,8 +25,6 @@ use SilverStripe\Security\RandomGenerator;
|
||||
use SilverStripe\Security\Security;
|
||||
use SilverStripe\View\ArrayData;
|
||||
use SilverStripe\View\Requirements;
|
||||
use LogicException;
|
||||
use FilesystemIterator;
|
||||
|
||||
/**
|
||||
* Requires PHP's mycrypt extension in order to set the database name as an encrypted cookie.
|
||||
|
@ -85,7 +85,7 @@ class TestSessionEnvironment
|
||||
public function init(HTTPRequest $request)
|
||||
{
|
||||
if (!$this->id) {
|
||||
$request->getSession()->init();
|
||||
$request->getSession()->init($request);
|
||||
// $_SESSION != Session::get() in some execution paths, suspect Controller->pushCurrent()
|
||||
// as part of the issue, easiest resolution is to use session directly for now
|
||||
$this->id = $request->getSession()->get('TestSessionId');
|
||||
@ -98,9 +98,9 @@ class TestSessionEnvironment
|
||||
public function getFilePath()
|
||||
{
|
||||
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 {
|
||||
$path = Director::getAbsFile($this->config()->test_state_file);
|
||||
$path = Director::getAbsFile($this->config()->get('test_state_file'));
|
||||
}
|
||||
|
||||
return $path;
|
||||
|
@ -6,8 +6,7 @@ use SilverStripe\Control\Director;
|
||||
use SilverStripe\Control\Email\Email;
|
||||
use SilverStripe\Control\Email\Mailer;
|
||||
use SilverStripe\Control\HTTPRequest;
|
||||
use SilverStripe\Control\HTTPResponse;
|
||||
use SilverStripe\Control\RequestFilter;
|
||||
use SilverStripe\Control\Middleware\HTTPMiddleware;
|
||||
use SilverStripe\Core\Injector\Injector;
|
||||
use SilverStripe\ORM\DB;
|
||||
use SilverStripe\ORM\FieldType\DBDatetime;
|
||||
@ -15,7 +14,7 @@ use SilverStripe\ORM\FieldType\DBDatetime;
|
||||
/**
|
||||
* Sets state previously initialized through {@link TestSessionController}.
|
||||
*/
|
||||
class TestSessionRequestFilter implements RequestFilter
|
||||
class TestSessionHTTPMiddleware implements HTTPMiddleware
|
||||
{
|
||||
/**
|
||||
* @var TestSessionEnvironment
|
||||
@ -27,14 +26,35 @@ class TestSessionRequestFilter implements RequestFilter
|
||||
$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);
|
||||
|
||||
// If not running tests, just pass through
|
||||
$isRunningTests = $this->testSessionEnvironment->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();
|
||||
|
||||
// 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
|
||||
$state = $this->testSessionEnvironment->getState();
|
||||
$state->session = $request->getSession()->getAll();
|
Loading…
Reference in New Issue
Block a user