Added Director::test() for unit testing

Changed From::testSubmission to use Director::test(), so that it can check whether URLs are correctly set
Made Director::is_ajax() look at the controller data

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@40590 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sam Minnee 2007-08-20 22:39:44 +00:00
parent b5848c9b87
commit 734cf2ad4f
3 changed files with 57 additions and 7 deletions

View File

@ -284,6 +284,13 @@ class Controller extends ViewableData {
user_error("No current controller available", E_USER_WARNING); user_error("No current controller available", E_USER_WARNING);
} }
} }
/**
* Tests whether we have a currently active controller or not
*/
public static function has_curr() {
return Controller::$controller_stack ? true : false;
}
/** /**
* Returns true if the member is allowed to do the given action. * Returns true if the member is allowed to do the given action.
@ -395,6 +402,16 @@ class Controller extends ViewableData {
$this->session = $session; $this->session = $session;
} }
/**
* Returns true if this controller is processing an ajax request
*/
function isAjax() {
return (
isset($this->requestParams['ajax']) ||
(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == "XMLHttpRequest")
);
}
} }
?> ?>

View File

@ -54,12 +54,39 @@ class Director {
// Save the updated session back // Save the updated session back
$_SESSION = $controllerObj->getSession()->inst_getAll(); $_SESSION = $controllerObj->getSession()->inst_getAll();
$response->output(); $response->output();
} }
if(isset($_GET['debug_profile'])) Profiler::unmark("Director","direct"); if(isset($_GET['debug_profile'])) Profiler::unmark("Director","direct");
} }
/**
* Test a URL request, returning a response object.
* @param $url The URL to visit
* @param $post The $_POST & $_FILES variables
* @param $session The {@link Session} object representing the current session.
*/
function test($url, $post = null, $session = null) {
$getVars = array();
if(strpos($url,'?') !== false) {
list($url, $getVarsEncoded) = explode('?', $url, 2);
parse_str($getVarsEncoded, $getVars);
}
$controllerObj = Director::getControllerForURL($url);
// Load the session into the controller
$controllerObj->setSession($session ? $session : new Session(null));
if(is_string($controllerObj) && substr($controllerObj,0,9) == 'redirect:') {
user_error("Redirection not implemented in Director::test", E_USER_ERROR);
} else if($controllerObj) {
$response = $controllerObj->run( array_merge($getVars, (array)$post) );
return $response;
}
}
static function getControllerForURL($url) { static function getControllerForURL($url) {
if(isset($_GET['debug_profile'])) Profiler::mark("Director","getControllerForURL"); if(isset($_GET['debug_profile'])) Profiler::mark("Director","getControllerForURL");
@ -289,10 +316,14 @@ class Director {
* @return boolean * @return boolean
*/ */
static function is_ajax() { static function is_ajax() {
return ( if(Controller::has_curr()) {
isset($_REQUEST['ajax']) || return Controller::curr()->isAjax();
(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == "XMLHttpRequest") } else {
); return (
isset($_REQUEST['ajax']) ||
(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == "XMLHttpRequest")
);
}
} }

View File

@ -645,9 +645,11 @@ class Form extends ViewableData {
function testSubmission($action, $data) { function testSubmission($action, $data) {
$data['action_' . $action] = true; $data['action_' . $action] = true;
$data['executeForm'] = $this->name; $data['executeForm'] = $this->name;
return Director::test($this->FormAction(), $data, Controller::curr()->getSession());
$response = $this->controller->run($data); //$response = $this->controller->run($data);
return $response; //return $response;
} }
function testAjaxSubmission($action, $data) { function testAjaxSubmission($action, $data) {