Test nested controller actions and Controller->getViewer

Adds tests and supporting classes for testing that the correct action is passed to Controller->getViewer inside Controller->handleRequest.
This commit is contained in:
Will Morgan 2014-04-16 13:34:09 +01:00
parent 438fe02116
commit c6797f52ea

View File

@ -373,6 +373,27 @@ class ControllerTest extends FunctionalTest {
"Doesn't redirect on external URLs"
);
}
public function testSubActions() {
/* If a controller action returns another controller, ensure that the $action variable is correctly forwarded */
$response = $this->get("ControllerTest_ContainerController/subcontroller/subaction");
$this->assertEquals('subaction', $response->getBody());
$request = new SS_HTTPRequest(
'GET',
'ControllerTest_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, DataModel::inst());
}
catch(ControllerTest_SubController_Exception $e) {
$this->fail($e->getMessage());
}
}
}
/**
@ -500,3 +521,50 @@ class ControllerTest_HasAction_Unsecured extends ControllerTest_HasAction implem
public function defined_action() { }
}
class ControllerTest_ContainerController extends Controller implements TestOnly {
private static $allowed_actions = array(
'subcontroller',
);
public function subcontroller() {
return new ControllerTest_SubController();
}
}
class ControllerTest_SubController extends Controller implements TestOnly {
private static $allowed_actions = array(
'subaction',
'subvieweraction',
);
private static $url_handlers = array(
'substring/subvieweraction' => 'subvieweraction',
);
public function subaction() {
return $this->getAction();
}
/* This is messy, but Controller->handleRequest is a hard to test method which warrants such measures... */
public function getViewer($action) {
if(empty($action)) {
throw new ControllerTest_SubController_Exception("Null action passed, getViewer will break");
}
return parent::getViewer($action);
}
public function subvieweraction() {
return $this->customise(array(
'Thoughts' => 'Hope this works',
));
}
}
class ControllerTest_SubController_Exception extends Exception {
}