2008-08-09 05:19:54 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
* Tests for RequestHandler and SS_HTTPRequest.
|
2014-08-15 08:53:05 +02:00
|
|
|
* We've set up a simple URL handling model based on
|
2008-08-09 05:19:54 +02:00
|
|
|
*/
|
2010-12-20 01:00:38 +01:00
|
|
|
class RequestHandlingTest extends FunctionalTest {
|
2013-03-21 19:48:54 +01:00
|
|
|
protected static $fixture_file = null;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-01-29 14:14:47 +01:00
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
Config::inst()->update('Director', 'rules', array(
|
2014-03-30 08:51:38 +02:00
|
|
|
// If we don't request any variables, then the whole URL will get shifted off.
|
|
|
|
// This is fine, but it means that the controller will have to parse the Action from the URL itself.
|
2013-01-29 14:14:47 +01:00
|
|
|
'testGoodBase1' => "RequestHandlingTest_Controller",
|
|
|
|
|
2014-03-30 08:51:38 +02:00
|
|
|
// The double-slash indicates how much of the URL should be shifted off the stack.
|
|
|
|
// This is important for dealing with nested request handlers appropriately.
|
2013-01-29 14:14:47 +01:00
|
|
|
'testGoodBase2//$Action/$ID/$OtherID' => "RequestHandlingTest_Controller",
|
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
// By default, the entire URL will be shifted off. This creates a bit of
|
2014-03-30 08:51:38 +02:00
|
|
|
// backward-incompatability, but makes the URL rules much more explicit.
|
2013-01-29 14:14:47 +01:00
|
|
|
'testBadBase/$Action/$ID/$OtherID' => "RequestHandlingTest_Controller",
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-01-29 14:14:47 +01:00
|
|
|
// Rules with an extension always default to the index() action
|
|
|
|
'testBaseWithExtension/virtualfile.xml' => "RequestHandlingTest_Controller",
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-01-29 14:14:47 +01:00
|
|
|
// Without the extension, the methodname should be matched
|
|
|
|
'testBaseWithExtension//$Action/$ID/$OtherID' => "RequestHandlingTest_Controller",
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-01-29 14:14:47 +01:00
|
|
|
// Test nested base
|
|
|
|
'testParentBase/testChildBase//$Action/$ID/$OtherID' => "RequestHandlingTest_Controller",
|
|
|
|
));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
// public function testRequestHandlerChainingLatestParams() {
|
2010-10-15 04:53:11 +02:00
|
|
|
// $c = new RequestHandlingTest_Controller();
|
|
|
|
// $c->init();
|
|
|
|
// $response = $c->handleRequest(new SS_HTTPRequest('GET', 'testGoodBase1/TestForm/fields/MyField'));
|
|
|
|
// $this->assertEquals(
|
|
|
|
// $c->getRequest()->latestParams(),
|
|
|
|
// array(
|
|
|
|
// 'Action' => 'fields',
|
|
|
|
// 'ID' => 'MyField'
|
|
|
|
// )
|
|
|
|
// );
|
|
|
|
// }
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testConstructedWithNullRequest() {
|
2010-12-16 05:06:13 +01:00
|
|
|
$r = new RequestHandler();
|
2012-05-09 12:43:22 +02:00
|
|
|
$this->assertInstanceOf('NullHTTPRequest', $r->getRequest());
|
2010-12-16 05:06:13 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testRequestHandlerChainingAllParams() {
|
2012-11-23 14:55:19 +01:00
|
|
|
$this->markTestIncomplete();
|
2010-10-15 04:53:11 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testMethodCallingOnController() {
|
2008-08-09 05:19:54 +02:00
|
|
|
/* Calling a controller works just like it always has */
|
|
|
|
$response = Director::test("testGoodBase1");
|
|
|
|
$this->assertEquals("This is the controller", $response->getBody());
|
|
|
|
|
|
|
|
/* ID and OtherID are extracted from the URL and passed in $request->params. */
|
|
|
|
$response = Director::test("testGoodBase1/method/1/2");
|
|
|
|
$this->assertEquals("This is a method on the controller: 1, 2", $response->getBody());
|
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
/* In addition, these values are availalbe in $controller->urlParams. This is mainly for backward
|
|
|
|
* compatability. */
|
2008-08-09 05:19:54 +02:00
|
|
|
$response = Director::test("testGoodBase1/legacymethod/3/4");
|
|
|
|
$this->assertEquals("\$this->urlParams can be used, for backward compatibility: 3, 4", $response->getBody());
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testPostRequests() {
|
2008-08-09 05:19:54 +02:00
|
|
|
/* The HTTP Request handler can trigger special behaviour for GET and POST. */
|
|
|
|
$response = Director::test("testGoodBase1/TestForm", array("MyField" => 3), null, "POST");
|
|
|
|
$this->assertEquals("Form posted", $response->getBody());
|
|
|
|
|
|
|
|
$response = Director::test("testGoodBase1/TestForm");
|
|
|
|
$this->assertEquals("Get request on form", $response->getBody());
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testRequestHandlerChaining() {
|
2008-08-09 05:19:54 +02:00
|
|
|
/* Request handlers can be chained, from Director to Controller to Form to FormField. Here, we can make a get
|
|
|
|
request on a FormField. */
|
|
|
|
$response = Director::test("testGoodBase1/TestForm/fields/MyField");
|
|
|
|
$this->assertEquals("MyField requested", $response->getBody());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
/* We can also make a POST request on a form field, which could be used for in-place editing, for example. */
|
|
|
|
$response = Director::test("testGoodBase1/TestForm/fields/MyField" ,array("MyField" => 5));
|
|
|
|
$this->assertEquals("MyField posted, update to 5", $response->getBody());
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testBadBase() {
|
2013-01-24 20:44:39 +01:00
|
|
|
/* We no longer support using hacky attempting to handle URL parsing with broken rules */
|
2008-08-09 05:19:54 +02:00
|
|
|
$response = Director::test("testBadBase/method/1/2");
|
2013-01-24 20:44:39 +01:00
|
|
|
$this->assertNotEquals("This is a method on the controller: 1, 2", $response->getBody());
|
2008-08-09 05:19:54 +02:00
|
|
|
|
|
|
|
$response = Director::test("testBadBase/TestForm", array("MyField" => 3), null, "POST");
|
2013-01-24 20:44:39 +01:00
|
|
|
$this->assertNotEquals("Form posted", $response->getBody());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
$response = Director::test("testBadBase/TestForm/fields/MyField");
|
|
|
|
$this->assertNotEquals("MyField requested", $response->getBody());
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testBaseWithExtension() {
|
2008-09-16 22:37:46 +02:00
|
|
|
/* Rules with an extension always default to the index() action */
|
|
|
|
$response = Director::test("testBaseWithExtension/virtualfile.xml");
|
|
|
|
$this->assertEquals("This is the controller", $response->getBody());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-09-16 22:37:46 +02:00
|
|
|
/* Without the extension, the methodname should be matched */
|
|
|
|
$response = Director::test("testBaseWithExtension/virtualfile");
|
|
|
|
$this->assertEquals("This is the virtualfile method", $response->getBody());
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testNestedBase() {
|
2008-10-05 21:21:35 +02:00
|
|
|
/* Nested base should leave out the two parts and correctly map arguments */
|
|
|
|
$response = Director::test("testParentBase/testChildBase/method/1/2");
|
|
|
|
$this->assertEquals("This is a method on the controller: 1, 2", $response->getBody());
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testInheritedUrlHandlers() {
|
2008-10-30 23:28:01 +01:00
|
|
|
/* $url_handlers can be defined on any class, and */
|
|
|
|
$response = Director::test("testGoodBase1/TestForm/fields/SubclassedField/something");
|
|
|
|
$this->assertEquals("customSomething", $response->getBody());
|
|
|
|
|
|
|
|
/* However, if the subclass' url_handlers don't match, then the parent class' url_handlers will be used */
|
|
|
|
$response = Director::test("testGoodBase1/TestForm/fields/SubclassedField");
|
|
|
|
$this->assertEquals("SubclassedField requested", $response->getBody());
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testDisallowedExtendedActions() {
|
2008-10-31 03:16:51 +01:00
|
|
|
/* Actions on an extension are allowed because they specifically provided appropriate allowed_actions items */
|
|
|
|
$response = Director::test("testGoodBase1/otherExtendedMethod");
|
|
|
|
$this->assertEquals("otherExtendedMethod", $response->getBody());
|
|
|
|
|
|
|
|
/* The failoverMethod action wasn't explicitly listed and so isnt' allowed */
|
|
|
|
$response = Director::test("testGoodBase1/failoverMethod");
|
2009-10-11 02:07:24 +02:00
|
|
|
$this->assertEquals(404, $response->getStatusCode());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-31 03:16:51 +01:00
|
|
|
/* However, on RequestHandlingTest_AllowedController it has been explicitly allowed */
|
|
|
|
$response = Director::test("RequestHandlingTest_AllowedController/failoverMethod");
|
|
|
|
$this->assertEquals("failoverMethod", $response->getBody());
|
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
/* The action on the extension is allowed when explicitly allowed on extension,
|
2013-06-20 11:40:55 +02:00
|
|
|
even if its not mentioned in controller */
|
2008-10-31 03:16:51 +01:00
|
|
|
$response = Director::test("RequestHandlingTest_AllowedController/extendedMethod");
|
2013-06-20 11:40:55 +02:00
|
|
|
$this->assertEquals(200, $response->getStatusCode());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-07-23 07:44:37 +02:00
|
|
|
/* This action has been blocked by an argument to a method */
|
|
|
|
$response = Director::test('RequestHandlingTest_AllowedController/blockMethod');
|
|
|
|
$this->assertEquals(403, $response->getStatusCode());
|
|
|
|
|
|
|
|
/* Whereas this one has been allowed by a method without an argument */
|
|
|
|
$response = Director::test('RequestHandlingTest_AllowedController/allowMethod');
|
|
|
|
$this->assertEquals('allowMethod', $response->getBody());
|
2008-10-31 03:16:51 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-27 10:48:44 +02:00
|
|
|
public function testHTTPException() {
|
|
|
|
$exception = Director::test('RequestHandlingTest_Controller/throwexception');
|
|
|
|
$this->assertEquals(400, $exception->getStatusCode());
|
|
|
|
$this->assertEquals('This request was invalid.', $exception->getBody());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-27 10:48:44 +02:00
|
|
|
$responseException = (Director::test('RequestHandlingTest_Controller/throwresponseexception'));
|
|
|
|
$this->assertEquals(500, $responseException->getStatusCode());
|
|
|
|
$this->assertEquals('There was an internal server error.', $responseException->getBody());
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-27 10:48:44 +02:00
|
|
|
public function testHTTPError() {
|
2012-09-27 02:26:25 +02:00
|
|
|
RequestHandlingTest_ControllerExtension::$called_error = false;
|
|
|
|
RequestHandlingTest_ControllerExtension::$called_404_error = false;
|
|
|
|
|
2009-06-27 10:48:44 +02:00
|
|
|
$response = Director::test('RequestHandlingTest_Controller/throwhttperror');
|
|
|
|
$this->assertEquals(404, $response->getStatusCode());
|
|
|
|
$this->assertEquals('This page does not exist.', $response->getBody());
|
2012-09-27 02:26:25 +02:00
|
|
|
|
|
|
|
// Confirm that RequestHandlingTest_ControllerExtension::onBeforeHTTPError() called
|
|
|
|
$this->assertTrue(RequestHandlingTest_ControllerExtension::$called_error);
|
|
|
|
// Confirm that RequestHandlingTest_ControllerExtension::onBeforeHTTPError404() called
|
|
|
|
$this->assertTrue(RequestHandlingTest_ControllerExtension::$called_404_error);
|
2009-06-27 10:48:44 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testMethodsOnParentClassesOfRequestHandlerDeclined() {
|
2012-02-11 03:08:39 +01:00
|
|
|
$response = Director::test('testGoodBase1/getIterator');
|
2013-01-15 00:34:05 +01:00
|
|
|
$this->assertEquals(404, $response->getStatusCode());
|
2010-10-13 03:24:15 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testFormActionsCanBypassAllowedActions() {
|
2014-08-15 08:53:05 +02:00
|
|
|
SecurityToken::enable();
|
|
|
|
|
2010-12-20 01:00:38 +01:00
|
|
|
$response = $this->get('RequestHandlingTest_FormActionController');
|
|
|
|
$this->assertEquals(200, $response->getStatusCode());
|
|
|
|
$tokenEls = $this->cssParser()->getBySelector('#Form_Form_SecurityID');
|
|
|
|
$securityId = (string)$tokenEls[0]['value'];
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-12-20 01:00:38 +01:00
|
|
|
$data = array('action_formaction' => 1);
|
|
|
|
$response = $this->post('RequestHandlingTest_FormActionController/Form', $data);
|
|
|
|
$this->assertEquals(400, $response->getStatusCode(),
|
|
|
|
'Should fail: Invocation through POST form handler, not contained in $allowed_actions, without CSRF token'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-12-20 01:00:38 +01:00
|
|
|
$data = array('action_disallowedcontrollermethod' => 1, 'SecurityID' => $securityId);
|
|
|
|
$response = $this->post('RequestHandlingTest_FormActionController/Form', $data);
|
2014-08-15 08:53:05 +02:00
|
|
|
$this->assertEquals(403, $response->getStatusCode(),
|
2012-09-26 23:34:00 +02:00
|
|
|
'Should fail: Invocation through POST form handler, controller action instead of form action,'
|
|
|
|
.' not contained in $allowed_actions, with CSRF token'
|
2010-12-20 01:00:38 +01:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-12-20 01:00:38 +01:00
|
|
|
$data = array('action_formaction' => 1, 'SecurityID' => $securityId);
|
|
|
|
$response = $this->post('RequestHandlingTest_FormActionController/Form', $data);
|
|
|
|
$this->assertEquals(200, $response->getStatusCode());
|
2014-08-15 08:53:05 +02:00
|
|
|
$this->assertEquals('formaction', $response->getBody(),
|
2010-12-20 01:00:38 +01:00
|
|
|
'Should pass: Invocation through POST form handler, not contained in $allowed_actions, with CSRF token'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-12-20 01:00:38 +01:00
|
|
|
$data = array('action_controlleraction' => 1, 'SecurityID' => $securityId);
|
|
|
|
$response = $this->post('RequestHandlingTest_FormActionController/Form', $data);
|
2014-08-15 08:53:05 +02:00
|
|
|
$this->assertEquals(200, $response->getStatusCode(),
|
2012-09-26 23:34:00 +02:00
|
|
|
'Should pass: Invocation through POST form handler, controller action instead of form action, contained in'
|
|
|
|
. ' $allowed_actions, with CSRF token'
|
2010-12-20 01:00:38 +01:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-12-20 01:00:38 +01:00
|
|
|
$data = array('action_formactionInAllowedActions' => 1);
|
|
|
|
$response = $this->post('RequestHandlingTest_FormActionController/Form', $data);
|
|
|
|
$this->assertEquals(400, $response->getStatusCode(),
|
|
|
|
'Should fail: Invocation through POST form handler, contained in $allowed_actions, without CSRF token'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-12-20 01:00:38 +01:00
|
|
|
$data = array('action_formactionInAllowedActions' => 1, 'SecurityID' => $securityId);
|
|
|
|
$response = $this->post('RequestHandlingTest_FormActionController/Form', $data);
|
|
|
|
$this->assertEquals(200, $response->getStatusCode(),
|
|
|
|
'Should pass: Invocation through POST form handler, contained in $allowed_actions, with CSRF token'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-12-20 01:00:38 +01:00
|
|
|
$data = array();
|
|
|
|
$response = $this->post('RequestHandlingTest_FormActionController/formaction', $data);
|
|
|
|
$this->assertEquals(404, $response->getStatusCode(),
|
|
|
|
'Should fail: Invocation through POST URL, not contained in $allowed_actions, without CSRF token'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-12-20 01:00:38 +01:00
|
|
|
$data = array();
|
|
|
|
$response = $this->post('RequestHandlingTest_FormActionController/formactionInAllowedActions', $data);
|
|
|
|
$this->assertEquals(200, $response->getStatusCode(),
|
|
|
|
'Should pass: Invocation of form action through POST URL, contained in $allowed_actions, without CSRF token'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-12-20 01:00:38 +01:00
|
|
|
$data = array('SecurityID' => $securityId);
|
|
|
|
$response = $this->post('RequestHandlingTest_FormActionController/formactionInAllowedActions', $data);
|
|
|
|
$this->assertEquals(200, $response->getStatusCode(),
|
|
|
|
'Should pass: Invocation of form action through POST URL, contained in $allowed_actions, with CSRF token'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-12-20 01:00:38 +01:00
|
|
|
$data = array(); // CSRF protection doesnt kick in for direct requests
|
|
|
|
$response = $this->post('RequestHandlingTest_FormActionController/formactionInAllowedActions', $data);
|
|
|
|
$this->assertEquals(200, $response->getStatusCode(),
|
|
|
|
'Should pass: Invocation of form action through POST URL, contained in $allowed_actions, without CSRF token'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-12-20 01:00:38 +01:00
|
|
|
SecurityToken::disable();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testAllowedActionsEnforcedOnForm() {
|
2010-12-20 01:00:38 +01:00
|
|
|
$data = array('action_allowedformaction' => 1);
|
|
|
|
$response = $this->post('RequestHandlingTest_ControllerFormWithAllowedActions/Form', $data);
|
|
|
|
$this->assertEquals(200, $response->getStatusCode());
|
|
|
|
$this->assertEquals('allowedformaction', $response->getBody());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-12-20 01:00:38 +01:00
|
|
|
$data = array('action_disallowedformaction' => 1);
|
|
|
|
$response = $this->post('RequestHandlingTest_ControllerFormWithAllowedActions/Form', $data);
|
|
|
|
$this->assertEquals(403, $response->getStatusCode());
|
|
|
|
// Note: Looks for a specific 403 thrown by Form->httpSubmission(), not RequestHandler->handleRequest()
|
|
|
|
$this->assertContains('not allowed on form', $response->getBody());
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testActionHandlingOnField() {
|
2011-12-06 01:56:24 +01:00
|
|
|
$data = array('action_actionOnField' => 1);
|
|
|
|
$response = $this->post('RequestHandlingFieldTest_Controller/TestForm', $data);
|
|
|
|
$this->assertEquals(200, $response->getStatusCode());
|
|
|
|
$this->assertEquals('Test method on MyField', $response->getBody());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-06 01:56:24 +01:00
|
|
|
$data = array('action_actionNotAllowedOnField' => 1);
|
|
|
|
$response = $this->post('RequestHandlingFieldTest_Controller/TestForm', $data);
|
|
|
|
$this->assertEquals(404, $response->getStatusCode());
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Controller for the test
|
|
|
|
*/
|
2011-02-13 23:14:51 +01:00
|
|
|
class RequestHandlingTest_Controller extends Controller implements TestOnly {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $allowed_actions = array(
|
2013-01-15 00:34:05 +01:00
|
|
|
'method',
|
|
|
|
'legacymethod',
|
|
|
|
'virtualfile',
|
|
|
|
'TestForm',
|
|
|
|
'throwexception',
|
|
|
|
'throwresponseexception',
|
|
|
|
'throwhttperror',
|
|
|
|
);
|
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $url_handlers = array(
|
2014-08-15 08:53:05 +02:00
|
|
|
// The double-slash is need here to ensure that
|
2008-08-09 05:19:54 +02:00
|
|
|
'$Action//$ID/$OtherID' => "handleAction",
|
|
|
|
);
|
2008-10-31 03:16:51 +01:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $extensions = array(
|
2008-10-31 03:16:51 +01:00
|
|
|
'RequestHandlingTest_ControllerExtension',
|
|
|
|
'RequestHandlingTest_AllowedControllerExtension',
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __construct() {
|
2008-10-31 03:16:51 +01:00
|
|
|
$this->failover = new RequestHandlingTest_ControllerFailover();
|
|
|
|
parent::__construct();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function index($request) {
|
2008-08-09 05:19:54 +02:00
|
|
|
return "This is the controller";
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function method($request) {
|
2008-08-09 05:19:54 +02:00
|
|
|
return "This is a method on the controller: " . $request->param('ID') . ', ' . $request->param('OtherID');
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function legacymethod($request) {
|
2012-09-26 23:34:00 +02:00
|
|
|
return "\$this->urlParams can be used, for backward compatibility: " . $this->urlParams['ID'] . ', '
|
|
|
|
. $this->urlParams['OtherID'];
|
2008-08-09 05:19:54 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function virtualfile($request) {
|
2008-09-16 22:37:46 +02:00
|
|
|
return "This is the virtualfile method";
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function TestForm() {
|
2011-05-11 09:51:54 +02:00
|
|
|
return new RequestHandlingTest_Form($this, "TestForm", new FieldList(
|
2008-10-30 23:28:01 +01:00
|
|
|
new RequestHandlingTest_FormField("MyField"),
|
|
|
|
new RequestHandlingTest_SubclassedFormField("SubclassedField")
|
2011-05-11 09:51:54 +02:00
|
|
|
), new FieldList(
|
2008-08-09 05:19:54 +02:00
|
|
|
new FormAction("myAction")
|
|
|
|
));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-27 10:48:44 +02:00
|
|
|
public function throwexception() {
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
throw new SS_HTTPResponse_Exception('This request was invalid.', 400);
|
2009-06-27 10:48:44 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-27 10:48:44 +02:00
|
|
|
public function throwresponseexception() {
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
throw new SS_HTTPResponse_Exception(new SS_HTTPResponse('There was an internal server error.', 500));
|
2009-06-27 10:48:44 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-27 10:48:44 +02:00
|
|
|
public function throwhttperror() {
|
|
|
|
$this->httpError(404, 'This page does not exist.');
|
|
|
|
}
|
2011-03-18 03:01:09 +01:00
|
|
|
|
2012-03-27 06:04:11 +02:00
|
|
|
public function getViewer($action) {
|
2011-03-23 04:32:24 +01:00
|
|
|
return new SSViewer('BlankPage');
|
2011-03-18 03:01:09 +01:00
|
|
|
}
|
2008-08-09 05:19:54 +02:00
|
|
|
}
|
|
|
|
|
2010-12-20 01:00:38 +01:00
|
|
|
class RequestHandlingTest_FormActionController extends Controller {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-12-20 01:00:38 +01:00
|
|
|
protected $template = 'BlankPage';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $allowed_actions = array(
|
2010-12-20 01:00:38 +01:00
|
|
|
'controlleraction',
|
|
|
|
'Form',
|
|
|
|
'formactionInAllowedActions'
|
|
|
|
//'formaction', // left out, implicitly allowed through form action
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Link($action = null) {
|
2010-12-20 01:00:38 +01:00
|
|
|
return Controller::join_links('RequestHandlingTest_FormActionController', $action);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function controlleraction($request) {
|
2010-12-20 01:00:38 +01:00
|
|
|
return 'controlleraction';
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function disallowedcontrollermethod() {
|
2010-12-20 01:00:38 +01:00
|
|
|
return 'disallowedcontrollermethod';
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Form() {
|
2010-12-20 01:00:38 +01:00
|
|
|
return new Form(
|
2014-08-15 08:53:05 +02:00
|
|
|
$this,
|
|
|
|
"Form",
|
2011-05-11 09:51:54 +02:00
|
|
|
new FieldList(
|
2010-12-20 01:00:38 +01:00
|
|
|
new TextField("MyField")
|
2014-08-15 08:53:05 +02:00
|
|
|
),
|
2011-05-11 09:51:54 +02:00
|
|
|
new FieldList(
|
2010-12-20 01:00:38 +01:00
|
|
|
new FormAction("formaction"),
|
|
|
|
new FormAction('formactionInAllowedActions')
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-12-20 01:00:38 +01:00
|
|
|
/**
|
|
|
|
* @param $data
|
|
|
|
* @param $form Made optional to simulate error behaviour in "live" environment
|
|
|
|
* (missing arguments don't throw a fatal error there)
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function formaction($data, $form = null) {
|
2010-12-20 01:00:38 +01:00
|
|
|
return 'formaction';
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function formactionInAllowedActions($data, $form = null) {
|
2010-12-20 01:00:38 +01:00
|
|
|
return 'formactionInAllowedActions';
|
|
|
|
}
|
2011-03-18 03:01:09 +01:00
|
|
|
|
2012-03-27 06:04:11 +02:00
|
|
|
public function getViewer($action = null) {
|
2011-03-23 04:32:24 +01:00
|
|
|
return new SSViewer('BlankPage');
|
2011-03-18 03:01:09 +01:00
|
|
|
}
|
|
|
|
|
2010-12-20 01:00:38 +01:00
|
|
|
}
|
|
|
|
|
2008-10-31 03:16:51 +01:00
|
|
|
/**
|
|
|
|
* Simple extension for the test controller
|
|
|
|
*/
|
|
|
|
class RequestHandlingTest_ControllerExtension extends Extension {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-27 02:26:25 +02:00
|
|
|
public static $called_error = false;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-27 02:26:25 +02:00
|
|
|
public static $called_404_error = false;
|
|
|
|
|
2013-06-20 11:40:55 +02:00
|
|
|
private static $allowed_actions = array('extendedMethod');
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function extendedMethod() {
|
2008-10-31 03:16:51 +01:00
|
|
|
return "extendedMethod";
|
|
|
|
}
|
2012-09-27 02:26:25 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called whenever there is an HTTP error
|
|
|
|
*/
|
|
|
|
public function onBeforeHTTPError() {
|
|
|
|
self::$called_error = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called whenever there is an 404 error
|
|
|
|
*/
|
|
|
|
public function onBeforeHTTPError404() {
|
|
|
|
self::$called_404_error = true;
|
|
|
|
}
|
|
|
|
|
2008-10-31 03:16:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Controller for the test
|
|
|
|
*/
|
2011-02-13 23:14:51 +01:00
|
|
|
class RequestHandlingTest_AllowedController extends Controller implements TestOnly {
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $url_handlers = array(
|
2014-08-15 08:53:05 +02:00
|
|
|
// The double-slash is need here to ensure that
|
2008-10-31 03:16:51 +01:00
|
|
|
'$Action//$ID/$OtherID' => "handleAction",
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $allowed_actions = array(
|
2008-10-31 03:16:51 +01:00
|
|
|
'failoverMethod', // part of the failover object
|
2012-07-23 07:44:37 +02:00
|
|
|
'blockMethod' => '->provideAccess(false)',
|
|
|
|
'allowMethod' => '->provideAccess',
|
2008-10-31 03:16:51 +01:00
|
|
|
);
|
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $extensions = array(
|
2008-10-31 03:16:51 +01:00
|
|
|
'RequestHandlingTest_ControllerExtension',
|
|
|
|
'RequestHandlingTest_AllowedControllerExtension',
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __construct() {
|
2008-10-31 03:16:51 +01:00
|
|
|
$this->failover = new RequestHandlingTest_ControllerFailover();
|
|
|
|
parent::__construct();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function index($request) {
|
2008-10-31 03:16:51 +01:00
|
|
|
return "This is the controller";
|
|
|
|
}
|
2012-07-23 07:44:37 +02:00
|
|
|
|
|
|
|
function provideAccess($access = true) {
|
|
|
|
return $access;
|
|
|
|
}
|
|
|
|
|
|
|
|
function blockMethod($request) {
|
|
|
|
return 'blockMethod';
|
|
|
|
}
|
|
|
|
|
|
|
|
function allowMethod($request) {
|
|
|
|
return 'allowMethod';
|
|
|
|
}
|
2008-10-31 03:16:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simple extension for the test controller - with allowed_actions define
|
|
|
|
*/
|
|
|
|
class RequestHandlingTest_AllowedControllerExtension extends Extension {
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $allowed_actions = array(
|
2008-10-31 03:16:51 +01:00
|
|
|
'otherExtendedMethod'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function otherExtendedMethod() {
|
2008-10-31 03:16:51 +01:00
|
|
|
return "otherExtendedMethod";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class RequestHandlingTest_ControllerFailover extends ViewableData {
|
2012-09-19 12:07:39 +02:00
|
|
|
public function failoverMethod() {
|
2008-10-31 03:16:51 +01:00
|
|
|
return "failoverMethod";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
/**
|
|
|
|
* Form for the test
|
|
|
|
*/
|
|
|
|
class RequestHandlingTest_Form extends Form {
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $url_handlers = array(
|
2008-08-09 05:19:54 +02:00
|
|
|
'fields/$FieldName' => 'handleField',
|
|
|
|
"POST " => "handleSubmission",
|
|
|
|
"GET " => "handleGet",
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-09-11 02:15:31 +02:00
|
|
|
// These are a different case from those in url_handlers to confirm that it's all case-insensitive
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $allowed_actions = array(
|
2008-09-11 02:15:31 +02:00
|
|
|
'handlesubmission',
|
|
|
|
'handlefield',
|
|
|
|
'handleget',
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function handleField($request) {
|
2012-01-02 15:03:35 +01:00
|
|
|
return $this->Fields()->dataFieldByName($request->param('FieldName'));
|
2008-08-09 05:19:54 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function handleSubmission($request) {
|
2008-08-09 05:19:54 +02:00
|
|
|
return "Form posted";
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function handleGet($request) {
|
2008-08-09 05:19:54 +02:00
|
|
|
return "Get request on form";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-13 23:14:51 +01:00
|
|
|
class RequestHandlingTest_ControllerFormWithAllowedActions extends Controller implements TestOnly {
|
2013-06-20 11:40:55 +02:00
|
|
|
|
|
|
|
private static $allowed_actions = array('Form');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Form() {
|
2010-12-20 01:00:38 +01:00
|
|
|
return new RequestHandlingTest_FormWithAllowedActions(
|
|
|
|
$this,
|
|
|
|
'Form',
|
2011-05-11 09:51:54 +02:00
|
|
|
new FieldList(),
|
|
|
|
new FieldList(
|
2013-06-20 11:40:55 +02:00
|
|
|
new FormAction('allowedformaction')
|
2010-12-20 01:00:38 +01:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class RequestHandlingTest_FormWithAllowedActions extends Form {
|
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $allowed_actions = array(
|
2010-12-20 01:00:38 +01:00
|
|
|
'allowedformaction' => 1,
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function allowedformaction() {
|
2010-12-20 01:00:38 +01:00
|
|
|
return 'allowedformaction';
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function disallowedformaction() {
|
2010-12-20 01:00:38 +01:00
|
|
|
return 'disallowedformaction';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Form field for the test
|
|
|
|
*/
|
|
|
|
class RequestHandlingTest_FormField extends FormField {
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $url_handlers = array(
|
2008-08-09 05:19:54 +02:00
|
|
|
"POST " => "handleInPlaceEdit",
|
|
|
|
'' => 'handleField',
|
|
|
|
'$Action' => '$Action',
|
|
|
|
);
|
2008-09-11 02:15:31 +02:00
|
|
|
|
|
|
|
// These contain uppercase letters to test that allowed_actions doesn't need to be all lowercase
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $allowed_actions = array(
|
2008-09-11 02:15:31 +02:00
|
|
|
'TEST',
|
|
|
|
'handleField',
|
|
|
|
'handleInPLACEEDIT',
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function test() {
|
2008-08-09 05:19:54 +02:00
|
|
|
return "Test method on $this->name";
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function handleField() {
|
2008-08-09 05:19:54 +02:00
|
|
|
return "$this->name requested";
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function handleInPlaceEdit($request) {
|
2008-08-09 05:19:54 +02:00
|
|
|
return "$this->name posted, update to " . $request->postVar($this->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-30 23:28:01 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Form field for the test
|
|
|
|
*/
|
|
|
|
class RequestHandlingTest_SubclassedFormField extends RequestHandlingTest_FormField {
|
2013-06-20 11:40:55 +02:00
|
|
|
|
|
|
|
private static $allowed_actions = array('customSomething');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-30 23:28:01 +01:00
|
|
|
// We have some url_handlers defined that override RequestHandlingTest_FormField handlers.
|
|
|
|
// We will confirm that the url_handlers inherit.
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $url_handlers = array(
|
2008-10-30 23:28:01 +01:00
|
|
|
'something' => 'customSomething',
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-30 23:28:01 +01:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function customSomething() {
|
2008-10-30 23:28:01 +01:00
|
|
|
return "customSomething";
|
|
|
|
}
|
2009-06-27 10:48:44 +02:00
|
|
|
}
|
2011-12-06 01:56:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Controller for the test
|
|
|
|
*/
|
|
|
|
class RequestHandlingFieldTest_Controller extends Controller implements TestOnly {
|
2013-06-20 11:40:55 +02:00
|
|
|
|
|
|
|
private static $allowed_actions = array('TestForm');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function TestForm() {
|
2011-12-06 01:56:24 +01:00
|
|
|
return new Form($this, "TestForm", new FieldList(
|
|
|
|
new RequestHandlingTest_HandlingField("MyField")
|
|
|
|
), new FieldList(
|
|
|
|
new FormAction("myAction")
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Form field for the test
|
|
|
|
*/
|
|
|
|
class RequestHandlingTest_HandlingField extends FormField {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $allowed_actions = array(
|
2011-12-06 01:56:24 +01:00
|
|
|
'actionOnField'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function actionOnField() {
|
2011-12-06 01:56:24 +01:00
|
|
|
return "Test method on $this->name";
|
|
|
|
}
|
2013-06-20 11:40:55 +02:00
|
|
|
|
|
|
|
public function actionNotAllowedOnField() {
|
|
|
|
return "actionNotAllowedOnField on $this->name";
|
|
|
|
}
|
2011-12-06 01:56:24 +01:00
|
|
|
}
|