Fix controller namespace

Move states to sub namespace
This commit is contained in:
Damian Mooyman 2017-06-19 16:59:34 +12:00
parent 2a278e2953
commit 06364af3c3
19 changed files with 284 additions and 189 deletions

View File

@ -2,18 +2,19 @@
Name: sapphiretest
---
SilverStripe\Core\Injector\Injector:
SilverStripe\Dev\SapphireTestState:
SilverStripe\Dev\State\SapphireTestState:
properties:
States:
extensions: %$SilverStripe\Dev\ExtensionTestState
flushable: %$SilverStripe\Dev\FlushableTestState
globals: %$SilverStripe\Dev\State\GlobalsTestState
extensions: %$SilverStripe\Dev\State\ExtensionTestState
flushable: %$SilverStripe\Dev\State\FlushableTestState
requirements: %$SilverStripe\View\Dev\RequirementsTestState
---
Name: kerneltest
Before: '*'
---
SilverStripe\Core\Injector\Injector:
SilverStripe\Dev\SapphireTestState:
SilverStripe\Dev\State\SapphireTestState:
properties:
States:
kernel: %$SilverStripe\Dev\KernelTestState
kernel: %$SilverStripe\Dev\State\KernelTestState

View File

@ -176,6 +176,49 @@ class Director implements TemplateGlobalProvider
$headers = array(),
$cookies = array(),
&$request = null
) {
return static::mockRequest(
function (HTTPRequest $request) {
return static::direct($request);
},
$url,
$postVars,
$session,
$httpMethod,
$body,
$headers,
$cookies,
$request
);
}
/**
* Mock a request, passing this to the given callback, before resetting.
*
* @param callable $callback Action to pass the HTTPRequst object
* @param string $url The URL to build
* @param array $postVars The $_POST & $_FILES variables.
* @param array|Session $session The {@link Session} object representing the current session.
* By passing the same object to multiple calls of Director::test(), you can simulate a persisted
* session.
* @param string $httpMethod The HTTP method, such as GET or POST. It will default to POST if
* postVars is set, GET otherwise. Overwritten by $postVars['_method'] if present.
* @param string $body The HTTP body.
* @param array $headers HTTP headers with key-value pairs.
* @param array|Cookie_Backend $cookies to populate $_COOKIE.
* @param HTTPRequest $request The {@see SS_HTTP_Request} object generated as a part of this request.
* @return mixed Result of callback
*/
public static function mockRequest(
$callback,
$url,
$postVars = [],
$session = [],
$httpMethod = null,
$body = null,
$headers = [],
$cookies = [],
&$request = null
) {
// Build list of cleanup promises
$finally = [];
@ -261,6 +304,7 @@ class Director implements TemplateGlobalProvider
$newVars['_GET'] = [];
}
$newVars['_SERVER']['REQUEST_URI'] = Director::baseURL() . $url;
$newVars['_REQUEST'] = array_merge($newVars['_GET'], $newVars['_POST']);
// Create new request
$request = HTTPRequest::createFromVariables($newVars, $body);
@ -275,7 +319,7 @@ class Director implements TemplateGlobalProvider
try {
// Normal request handling
return static::direct($request);
return call_user_func($callback, $request);
} finally {
// Restore state in reverse order to assignment
foreach (array_reverse($finally) as $callback) {

View File

@ -132,7 +132,7 @@ class CoreKernel implements Kernel
public function setEnvironment($environment)
{
if (!in_array($environment, [self::DEV, self::TEST, self::LIVE])) {
if (!in_array($environment, [self::DEV, self::TEST, self::LIVE, null])) {
throw new InvalidArgumentException(
"Director::set_environment_type passed '$environment'. It should be passed dev, test, or live"
);

View File

@ -85,12 +85,13 @@ class FunctionalTest extends SapphireTest
protected function setUp()
{
parent::setUp();
// Skip calling FunctionalTest directly.
if (static::class == __CLASS__) {
$this->markTestSkipped(sprintf('Skipping %s ', static::class));
}
parent::setUp();
$this->mainSession = new TestSession();
// Disable theme, if necessary
@ -115,9 +116,8 @@ class FunctionalTest extends SapphireTest
protected function tearDown()
{
SecurityToken::enable();
parent::tearDown();
unset($this->mainSession);
parent::tearDown();
}
/**

View File

@ -20,6 +20,8 @@ use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Injector\InjectorLoader;
use SilverStripe\Core\Manifest\ClassLoader;
use SilverStripe\Core\TestKernel;
use SilverStripe\Dev\State\SapphireTestState;
use SilverStripe\Dev\State\TestState;
use SilverStripe\i18n\i18n;
use SilverStripe\ORM\DataExtension;
use SilverStripe\ORM\DataObject;

View File

@ -1,10 +1,12 @@
<?php
namespace SilverStripe\Dev;
namespace SilverStripe\Dev\State;
use LogicException;
use SilverStripe\Core\Extension;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\Debug;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\ORM\DataObject;
/**
@ -38,7 +40,9 @@ class ExtensionTestState implements TestState
public function setUpOnce($class)
{
// May be altered by another class
$isAltered = $this->extensionsToReapply || $this->extensionsToRemove;
$isAltered = false;
$this->extensionsToReapply = [];
$this->extensionsToRemove = [];
/** @var string|SapphireTest $class */
/** @var string|DataObject $dataClass */
@ -68,7 +72,6 @@ class ExtensionTestState implements TestState
if (!class_exists($dataClass)) {
throw new LogicException("Test {$class} requires dataClass {$dataClass} which doesn't exist");
}
$this->extensionsToRemove[$dataClass] = array();
foreach ($extensions as $extension) {
$extension = Extension::get_classname_without_arguments($extension);
if (!class_exists($extension)) {
@ -76,7 +79,7 @@ class ExtensionTestState implements TestState
}
if (!$dataClass::has_extension($extension)) {
if (!isset($this->extensionsToRemove[$dataClass])) {
$this->extensionsToReapply[$dataClass] = [];
$this->extensionsToRemove[$dataClass] = [];
}
$this->extensionsToRemove[$dataClass][] = $extension;
$dataClass::add_extension($extension);

View File

@ -1,10 +1,11 @@
<?php
namespace SilverStripe\Dev;
namespace SilverStripe\Dev\State;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Flushable;
use SilverStripe\Core\Resettable;
use SilverStripe\Dev\SapphireTest;
/**
* Clears flushable / resettable objects

View File

@ -0,0 +1,44 @@
<?php
namespace SilverStripe\Dev\State;
use SilverStripe\Control\Director;
use SilverStripe\Dev\SapphireTest;
/**
* Cleans up and reset global env vars between tests
*/
class GlobalsTestState implements TestState
{
/**
* Var backed up for the class
* @var array
*/
protected $staticVars = [];
/**
* Vars backed up for the test
* @var array
*/
protected $vars = [];
public function setUp(SapphireTest $test)
{
$this->vars = Director::envToVars();
}
public function tearDown(SapphireTest $test)
{
Director::varsToEnv($this->vars);
}
public function setUpOnce($class)
{
$this->staticVars = Director::envToVars();
}
public function tearDownOnce($class)
{
Director::varsToEnv($this->staticVars);
}
}

View File

@ -1,10 +1,11 @@
<?php
namespace SilverStripe\Dev;
namespace SilverStripe\Dev\State;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Kernel;
use SilverStripe\Core\TestKernel;
use SilverStripe\Dev\SapphireTest;
/**
* Handles nesting of kernel before / after tests

View File

@ -1,8 +1,10 @@
<?php
namespace SilverStripe\Dev;
namespace SilverStripe\Dev\State;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Dev\Debug;
use SilverStripe\Dev\SapphireTest;
class SapphireTestState implements TestState
{

View File

@ -1,6 +1,9 @@
<?php
namespace SilverStripe\Dev;
namespace SilverStripe\Dev\State;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Dev\TestOnly;
/**
* Helper for resetting, booting, or cleaning up test state.

View File

@ -4,7 +4,7 @@
namespace SilverStripe\View\Dev;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Dev\TestState;
use SilverStripe\Dev\State\TestState;
use SilverStripe\View\Requirements;
use SilverStripe\View\Requirements_Backend;

View File

@ -5,7 +5,6 @@ namespace SilverStripe\Control\Tests;
use InvalidArgumentException;
use SilverStripe\Control\Controller;
use SilverStripe\Control\Director;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\Tests\ControllerTest\AccessBaseController;
use SilverStripe\Control\Tests\ControllerTest\AccessSecuredController;
use SilverStripe\Control\Tests\ControllerTest\AccessWildcardSecuredController;
@ -522,22 +521,12 @@ class ControllerTest extends FunctionalTest
public function testSubActions()
{
/* If a controller action returns another controller, ensure that the $action variable is correctly forwarded */
// If a controller action returns another controller, ensure that the $action variable is correctly forwarded
$response = $this->get("ContainerController/subcontroller/subaction");
$this->assertEquals('subaction', $response->getBody());
$request = new HTTPRequest(
'GET',
'ContainerController/subcontroller/substring/subvieweraction'
);
/* Shift to emulate the director selecting the controller */
$request->shift();
/* Handle the request to create conditions where improperly passing the action to the viewer might fail */
$controller = new ControllerTest\ContainerController();
try {
$controller->handleRequest($request);
} catch (ControllerTest\SubController_Exception $e) {
$this->fail($e->getMessage());
}
// Handle nested action
$response = $this->get('ContainerController/subcontroller/substring/subvieweraction');
$this->assertEquals('Hope this works', $response->getBody());
}
}

View File

@ -34,10 +34,6 @@ class SubController extends Controller implements TestOnly
public function subvieweraction()
{
return $this->customise(
array(
'Thoughts' => 'Hope this works',
)
);
return 'Hope this works';
}
}

View File

@ -3,31 +3,21 @@
namespace SilverStripe\Control\Tests;
use SilverStripe\Control\Cookie_Backend;
use SilverStripe\Control\Director;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Control\HTTPResponse_Exception;
use SilverStripe\Control\Tests\DirectorTest\TestController;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Control\Director;
use SilverStripe\Control\RequestProcessor;
use SilverStripe\Security\Security;
use SilverStripe\Control\Tests\DirectorTest\TestController;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Kernel;
use SilverStripe\Dev\SapphireTest;
/**
* @todo test Director::alternateBaseFolder()
*/
class DirectorTest extends SapphireTest
{
protected static $originalRequestURI;
protected $originalProtocolHeaders = array();
protected $originalGet = array();
protected $originalSession = array();
protected static $extra_controllers = [
TestController::class,
];
@ -35,28 +25,8 @@ class DirectorTest extends SapphireTest
protected function setUp()
{
parent::setUp();
// Hold the original request URI once so it doesn't get overwritten
if (!self::$originalRequestURI) {
self::$originalRequestURI = $_SERVER['REQUEST_URI'];
}
$_SERVER['REQUEST_URI'] = 'http://www.mysite.com';
$this->originalGet = $_GET;
$this->originalSession = $_SESSION;
$_SESSION = array();
$headers = array(
'HTTP_X_FORWARDED_PROTOCOL', 'HTTPS', 'SSL'
);
foreach ($headers as $header) {
if (isset($_SERVER[$header])) {
$this->originalProtocolHeaders[$header] = $_SERVER[$header];
}
}
Config::modify()->set(Director::class, 'alternate_base_url', '/');
Director::config()->set('alternate_base_url', 'http://www.mysite.com/');
$this->expectedRedirect = null;
}
protected function getExtraRoutes()
@ -77,24 +47,6 @@ class DirectorTest extends SapphireTest
Director::config()->set('rules', $this->getExtraRoutes());
}
protected function tearDown()
{
$_GET = $this->originalGet;
$_SESSION = $this->originalSession;
// Reinstate the original REQUEST_URI after it was modified by some tests
$_SERVER['REQUEST_URI'] = self::$originalRequestURI;
if ($this->originalProtocolHeaders) {
foreach ($this->originalProtocolHeaders as $header => $value) {
$_SERVER[$header] = $value;
}
}
parent::tearDown();
}
public function testFileExists()
{
$tempFileName = 'DirectorTest_testFileExists.tmp';
@ -118,55 +70,53 @@ class DirectorTest extends SapphireTest
public function testAbsoluteURL()
{
$rootURL = Director::protocolAndHost();
$_SERVER['REQUEST_URI'] = "$rootURL/mysite/sub-page/";
Director::config()->set('alternate_base_url', '/mysite/');
Director::config()->set('alternate_base_url', 'http://www.mysite.com/mysite/');
$_SERVER['REQUEST_URI'] = "http://www.mysite.com/mysite/sub-page/";
//test empty / local urls
foreach (array('', './', '.') as $url) {
$this->assertEquals("$rootURL/mysite/", Director::absoluteURL($url, Director::BASE));
$this->assertEquals("$rootURL/", Director::absoluteURL($url, Director::ROOT));
$this->assertEquals("$rootURL/mysite/sub-page/", Director::absoluteURL($url, Director::REQUEST));
$this->assertEquals("http://www.mysite.com/mysite/", Director::absoluteURL($url, Director::BASE));
$this->assertEquals("http://www.mysite.com/", Director::absoluteURL($url, Director::ROOT));
$this->assertEquals("http://www.mysite.com/mysite/sub-page/", Director::absoluteURL($url, Director::REQUEST));
}
// Test site root url
$this->assertEquals("$rootURL/", Director::absoluteURL('/'));
$this->assertEquals("http://www.mysite.com/", Director::absoluteURL('/'));
// Test Director::BASE
$this->assertEquals($rootURL, Director::absoluteURL($rootURL, Director::BASE));
$this->assertEquals('http://www.mysite.com/', Director::absoluteURL('http://www.mysite.com/', Director::BASE));
$this->assertEquals('http://www.mytest.com', Director::absoluteURL('http://www.mytest.com', Director::BASE));
$this->assertEquals("$rootURL/test", Director::absoluteURL("$rootURL/test", Director::BASE));
$this->assertEquals("$rootURL/root", Director::absoluteURL("/root", Director::BASE));
$this->assertEquals("$rootURL/root/url", Director::absoluteURL("/root/url", Director::BASE));
$this->assertEquals("http://www.mysite.com/test", Director::absoluteURL("http://www.mysite.com/test", Director::BASE));
$this->assertEquals("http://www.mysite.com/root", Director::absoluteURL("/root", Director::BASE));
$this->assertEquals("http://www.mysite.com/root/url", Director::absoluteURL("/root/url", Director::BASE));
// Test Director::ROOT
$this->assertEquals($rootURL, Director::absoluteURL($rootURL, Director::ROOT));
$this->assertEquals('http://www.mysite.com/', Director::absoluteURL('http://www.mysite.com/', Director::ROOT));
$this->assertEquals('http://www.mytest.com', Director::absoluteURL('http://www.mytest.com', Director::ROOT));
$this->assertEquals("$rootURL/test", Director::absoluteURL("$rootURL/test", Director::ROOT));
$this->assertEquals("$rootURL/root", Director::absoluteURL("/root", Director::ROOT));
$this->assertEquals("$rootURL/root/url", Director::absoluteURL("/root/url", Director::ROOT));
$this->assertEquals("http://www.mysite.com/test", Director::absoluteURL("http://www.mysite.com/test", Director::ROOT));
$this->assertEquals("http://www.mysite.com/root", Director::absoluteURL("/root", Director::ROOT));
$this->assertEquals("http://www.mysite.com/root/url", Director::absoluteURL("/root/url", Director::ROOT));
// Test Director::REQUEST
$this->assertEquals($rootURL, Director::absoluteURL($rootURL, Director::REQUEST));
$this->assertEquals('http://www.mysite.com/', Director::absoluteURL('http://www.mysite.com/', Director::REQUEST));
$this->assertEquals('http://www.mytest.com', Director::absoluteURL('http://www.mytest.com', Director::REQUEST));
$this->assertEquals("$rootURL/test", Director::absoluteURL("$rootURL/test", Director::REQUEST));
$this->assertEquals("$rootURL/root", Director::absoluteURL("/root", Director::REQUEST));
$this->assertEquals("$rootURL/root/url", Director::absoluteURL("/root/url", Director::REQUEST));
$this->assertEquals("http://www.mysite.com/test", Director::absoluteURL("http://www.mysite.com/test", Director::REQUEST));
$this->assertEquals("http://www.mysite.com/root", Director::absoluteURL("/root", Director::REQUEST));
$this->assertEquals("http://www.mysite.com/root/url", Director::absoluteURL("/root/url", Director::REQUEST));
// Test evaluating relative urls relative to base (default)
$this->assertEquals("$rootURL/mysite/test", Director::absoluteURL("test"));
$this->assertEquals("$rootURL/mysite/test/url", Director::absoluteURL("test/url"));
$this->assertEquals("$rootURL/mysite/test", Director::absoluteURL("test", Director::BASE));
$this->assertEquals("$rootURL/mysite/test/url", Director::absoluteURL("test/url", Director::BASE));
$this->assertEquals("http://www.mysite.com/mysite/test", Director::absoluteURL("test"));
$this->assertEquals("http://www.mysite.com/mysite/test/url", Director::absoluteURL("test/url"));
$this->assertEquals("http://www.mysite.com/mysite/test", Director::absoluteURL("test", Director::BASE));
$this->assertEquals("http://www.mysite.com/mysite/test/url", Director::absoluteURL("test/url", Director::BASE));
// Test evaluting relative urls relative to root
$this->assertEquals("$rootURL/test", Director::absoluteURL("test", Director::ROOT));
$this->assertEquals("$rootURL/test/url", Director::absoluteURL("test/url", Director::ROOT));
$this->assertEquals("http://www.mysite.com/test", Director::absoluteURL("test", Director::ROOT));
$this->assertEquals("http://www.mysite.com/test/url", Director::absoluteURL("test/url", Director::ROOT));
// Test relative to requested page
$this->assertEquals("$rootURL/mysite/sub-page/test", Director::absoluteURL("test", Director::REQUEST));
$this->assertEquals("$rootURL/mysite/sub-page/test/url", Director::absoluteURL("test/url", Director::REQUEST));
$this->assertEquals("http://www.mysite.com/mysite/sub-page/test", Director::absoluteURL("test", Director::REQUEST));
$this->assertEquals("http://www.mysite.com/mysite/sub-page/test/url", Director::absoluteURL("test/url", Director::REQUEST));
// Test that javascript links are not left intact
$this->assertStringStartsNotWith('javascript', Director::absoluteURL('javascript:alert("attack")'));
@ -177,17 +127,15 @@ class DirectorTest extends SapphireTest
public function testAlternativeBaseURL()
{
// Get original protocol and hostname
$rootURL = Director::protocolAndHost();
// relative base URLs - you should end them in a /
Director::config()->set('alternate_base_url', '/relativebase/');
$_SERVER['REQUEST_URI'] = "$rootURL/relativebase/sub-page/";
$_SERVER['HTTP_HOST'] = 'www.somesite.com';
$_SERVER['REQUEST_URI'] = "/relativebase/sub-page/";
$this->assertEquals('/relativebase/', Director::baseURL());
$this->assertEquals($rootURL . '/relativebase/', Director::absoluteBaseURL());
$this->assertEquals('http://www.somesite.com/relativebase/', Director::absoluteBaseURL());
$this->assertEquals(
$rootURL . '/relativebase/subfolder/test',
'http://www.somesite.com/relativebase/subfolder/test',
Director::absoluteURL('subfolder/test')
);
@ -336,6 +284,10 @@ class DirectorTest extends SapphireTest
unset($_GET['isTest']);
unset($_GET['isDev']);
/** @var Kernel $kernel */
$kernel = Injector::inst()->get(Kernel::class);
$kernel->setEnvironment(null);
// Test isDev=1
$_GET['isDev'] = '1';
$this->assertTrue(Director::isDev());
@ -399,14 +351,28 @@ class DirectorTest extends SapphireTest
);
}
public function testTestRequestCarriesGlobals()
public function providerTestTestRequestCarriesGlobals()
{
$fixture = array('somekey' => 'sometestvalue');
$tests = [];
$fixture = [ 'somekey' => 'sometestvalue' ];
foreach (array('get', 'post') as $method) {
foreach (array('return%sValue', 'returnRequestValue', 'returnCookieValue') as $testfunction) {
$url = 'TestController/' . sprintf($testfunction, ucfirst($method))
. '?' . http_build_query($fixture);
$tests[] = [$url, $fixture, $method];
}
}
return $tests;
}
/**
* @dataProvider providerTestTestRequestCarriesGlobals
* @param $url
* @param $fixture
* @param $method
*/
public function testTestRequestCarriesGlobals($url, $fixture, $method)
{
$getresponse = Director::test(
$url,
$fixture,
@ -418,9 +384,7 @@ class DirectorTest extends SapphireTest
);
$this->assertInstanceOf(HTTPResponse::class, $getresponse, 'Director::test() returns HTTPResponse');
$this->assertEquals($fixture['somekey'], $getresponse->getBody(), 'Director::test() ' . $testfunction);
}
}
$this->assertEquals($fixture['somekey'], $getresponse->getBody(), "Director::test({$url}, {$method})");
}
/**
@ -446,46 +410,87 @@ class DirectorTest extends SapphireTest
public function testForceSSLProtectsEntireSite()
{
$_SERVER['REQUEST_URI'] = '/admin';
$output = Director::forceSSL();
$this->assertEquals($output, 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
$_SERVER['REQUEST_URI'] = Director::baseURL() . 'some-url';
$output = Director::forceSSL();
$this->assertEquals($output, 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
$this->expectExceptionRedirect('https://www.mysite.com/some-url');
Director::mockRequest(function () {
Director::forceSSL();
}, '/some-url');
}
public function testForceSSLOnTopLevelPagePattern()
{
$_SERVER['REQUEST_URI'] = Director::baseURL() . 'admin';
$output = Director::forceSSL(array('/^admin/'));
$this->assertEquals($output, 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
// Expect admin to trigger redirect
$this->expectExceptionRedirect('https://www.mysite.com/admin');
Director::mockRequest(function () {
Director::forceSSL(array('/^admin/'));
}, '/admin');
}
public function testForceSSLOnSubPagesPattern()
{
$_SERVER['REQUEST_URI'] = Director::baseURL() . Config::inst()->get(Security::class, 'login_url');
$output = Director::forceSSL(array('/^Security/'));
$this->assertEquals($output, 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
// Expect to redirect to security login page
$this->expectExceptionRedirect('https://www.mysite.com/Security/login');
Director::mockRequest(function () {
Director::forceSSL(array('/^Security/'));
}, '/Security/login');
}
public function testForceSSLWithPatternDoesNotMatchOtherPages()
{
$_SERVER['REQUEST_URI'] = Director::baseURL() . 'normal-page';
$output = Director::forceSSL(array('/^admin/'));
$this->assertFalse($output);
// Not on same url should not trigger redirect
Director::mockRequest(function () {
$this->assertFalse(Director::forceSSL(array('/^admin/')));
}, Director::baseURL() . 'normal-page');
$_SERVER['REQUEST_URI'] = Director::baseURL() . 'just-another-page/sub-url';
$output = Director::forceSSL(array('/^admin/', '/^Security/'));
$this->assertFalse($output);
// nested url should not triger redirect either
Director::mockRequest(function () {
$this->assertFalse(Director::forceSSL(array('/^admin/', '/^Security/')));
}, Director::baseURL() . 'just-another-page/sub-url');
}
public function testForceSSLAlternateDomain()
{
Director::config()->set('alternate_base_url', '/');
$_SERVER['REQUEST_URI'] = Director::baseURL() . 'admin';
$output = Director::forceSSL(array('/^admin/'), 'secure.mysite.com');
$this->assertEquals($output, 'https://secure.mysite.com/admin');
// Ensure that forceSSL throws the appropriate exception
$this->expectExceptionRedirect('https://secure.mysite.com/admin');
Director::mockRequest(function (HTTPRequest $request) {
return Director::forceSSL(array('/^admin/'), 'secure.mysite.com');
}, Director::baseURL() . 'admin');
}
/**
* Set url to redirect to
*
* @var string
*/
protected $expectedRedirect = null;
/**
* Expects this test to throw a HTTPResponse_Exception with the given redirect
*
* @param string $url
*/
protected function expectExceptionRedirect($url)
{
$this->expectedRedirect = $url;
}
protected function runTest()
{
try {
$result = parent::runTest();
if ($this->expectedRedirect) {
$this->fail("Expected to redirect to {$this->expectedRedirect} but no redirect found");
}
return $result;
} catch (HTTPResponse_Exception $exception) {
// Check URL
if ($this->expectedRedirect) {
$url = $exception->getResponse()->getHeader('Location');
$this->assertEquals($this->expectedRedirect, $url, "Expected to redirect to {$this->expectedRedirect}");
return null;
} else {
throw $exception;
}
}
}
/**
@ -614,28 +619,30 @@ class DirectorTest extends SapphireTest
$processor = new RequestProcessor(array($filter));
Injector::inst()->registerService($processor, RequestProcessor::class);
Director::test('some-dummy-url');
$response = Director::test('some-dummy-url');
$this->assertEquals(404, $response->getStatusCode());
$this->assertEquals(1, $filter->preCalls);
$this->assertEquals(1, $filter->postCalls);
$filter->failPost = true;
$this->expectException(HTTPResponse_Exception::class);
Director::test('some-dummy-url');
$response = Director::test('some-dummy-url');
$this->assertEquals(500, $response->getStatusCode());
$this->assertEquals(_t(Director::class.'.REQUEST_ABORTED', 'Request aborted'), $response->getBody());
$this->assertEquals(2, $filter->preCalls);
$this->assertEquals(2, $filter->postCalls);
$filter->failPre = true;
Director::test('some-dummy-url');
$response = Director::test('some-dummy-url');
$this->assertEquals(400, $response->getStatusCode());
$this->assertEquals(_t(Director::class.'.INVALID_REQUEST', 'Invalid request'), $response->getBody());
$this->assertEquals(3, $filter->preCalls);
// preCall 'false' will trigger an exception and prevent post call execution
// preCall 'true' will trigger an exception and prevent post call execution
$this->assertEquals(2, $filter->postCalls);
}
}

View File

@ -26,21 +26,33 @@ class TestController extends Controller implements TestOnly
public function returnGetValue($request)
{
if (isset($_GET['somekey'])) {
return $_GET['somekey'];
}
return null;
}
public function returnPostValue($request)
{
if (isset($_POST['somekey'])) {
return $_POST['somekey'];
}
return null;
}
public function returnRequestValue($request)
{
if (isset($_REQUEST['somekey'])) {
return $_REQUEST['somekey'];
}
return null;
}
public function returnCookieValue($request)
{
if (isset($_COOKIE['somekey'])) {
return $_COOKIE['somekey'];
}
return null;
}
}

View File

@ -13,9 +13,7 @@ class FlushRequestFilterTest extends FunctionalTest
public function testImplementorsAreCalled()
{
TestFlushable::$flushed = false;
$this->get('?flush=1');
$this->assertTrue(TestFlushable::$flushed);
}
}

View File

@ -7,7 +7,6 @@ use SilverStripe\Dev\TestOnly;
class TestFlushable implements Flushable, TestOnly
{
public static $flushed = false;
public static function flush()

View File

@ -3,7 +3,6 @@
namespace SilverStripe\Control\Tests;
use SilverStripe\Control\Session;
use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\SapphireTest;
/**
@ -105,14 +104,6 @@ class SessionTest extends SapphireTest
$this->assertEquals(array('something' => array('does' => null)), $result);
}
public function testNonStandardPath()
{
Session::config()->set('store_path', (realpath(dirname($_SERVER['DOCUMENT_ROOT']) . '/../session')));
$this->session->start();
$this->assertEquals(Config::inst()->get('SilverStripe\\Control\\Session', 'store_path'), '');
}
public function testUserAgentLockout()
{
// Set a user agent
@ -120,6 +111,7 @@ class SessionTest extends SapphireTest
// Generate our session
$s = new Session(array());
$s->init();
$s->set('val', 123);
$s->finalize();
@ -128,6 +120,7 @@ class SessionTest extends SapphireTest
// Verify the new session reset our values
$s2 = new Session($s);
$s2->init();
$this->assertNotEquals($s2->get('val'), 123);
}
}