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

@ -285,6 +285,13 @@ class Controller extends ViewableData {
} }
} }
/**
* 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.
* @param perm The permission to be checked, such as 'View'. * @param perm The permission to be checked, such as 'View'.
@ -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,13 +54,40 @@ 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");
$url = preg_replace( array( '/\/+/','/^\//', '/\/$/'),array('/','',''),$url); $url = preg_replace( array( '/\/+/','/^\//', '/\/$/'),array('/','',''),$url);
@ -289,11 +316,15 @@ class Director {
* @return boolean * @return boolean
*/ */
static function is_ajax() { static function is_ajax() {
if(Controller::has_curr()) {
return Controller::curr()->isAjax();
} else {
return ( return (
isset($_REQUEST['ajax']) || isset($_REQUEST['ajax']) ||
(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == "XMLHttpRequest") (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == "XMLHttpRequest")
); );
} }
}

View File

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