Adding tests and features for CakeRequest to parse querystring args from the url parameter in the constructor. Added tests.

This commit is contained in:
Mark Story 2010-05-01 11:04:41 -04:00
parent fbd70bf23c
commit 40bca71985
2 changed files with 41 additions and 55 deletions

View file

@ -109,12 +109,9 @@ class CakeRequest implements ArrayAccess {
$url = $this->_url();
}
$this->url = $url;
if (isset($_POST)) {
$this->_processPost();
}
if (isset($_GET)) {
$this->_processGet();
}
$this->_processPost();
$this->_processGet();
$this->_processFiles();
if (!empty($additionalParams)) {
@ -157,14 +154,19 @@ class CakeRequest implements ArrayAccess {
*/
protected function _processGet() {
if (ini_get('magic_quotes_gpc') === '1') {
$url = stripslashes_deep($_GET);
$query = stripslashes_deep($_GET);
} else {
$url = $_GET;
$query = $_GET;
}
if (strpos($this->url, '?') !== false) {
list(, $querystr) = explode('?', $this->url);
parse_str($querystr, $queryArgs);
$query += $queryArgs;
}
if (isset($this->params['url'])) {
$url = array_merge($this->params['url'], $url);
$query = array_merge($this->params['url'], $query);
}
$this->query = $url;
$this->query = $query;
}
/**
@ -173,7 +175,7 @@ class CakeRequest implements ArrayAccess {
*
* @return string URI
*/
public function uri() {
protected function _uri() {
foreach (array('HTTP_X_REWRITE_URL', 'REQUEST_URI', 'argv') as $var) {
if ($uri = env($var)) {
if ($var == 'argv') {
@ -220,7 +222,7 @@ class CakeRequest implements ArrayAccess {
*/
protected function _url() {
if (empty($_GET['url'])) {
$uri = $this->uri();
$uri = $this->_uri();
$base = $this->base;
$url = null;
@ -496,6 +498,16 @@ class CakeRequest implements ArrayAccess {
$this->_detectors[$name] = $options;
}
/**
* Add parameters to the request's parsed parameter set.
*
* @param array $params Array of parameters to merge in
* @return void
*/
public function addParams($params) {
$this->params = array_merge($this->params, $params);
}
/**
* Array access read implementation
*
@ -506,6 +518,9 @@ class CakeRequest implements ArrayAccess {
if (isset($this->params[$name])) {
return $this->params[$name];
}
if ($name == 'url') {
return $this->query;
}
return null;
}

View file

@ -52,6 +52,17 @@ class CakeRequestTestCase extends CakeTestCase {
$this->assertEqual($request->url, 'some/path');
}
/**
* Test that querystring args provided in the url string are parsed.
*
* @return void
*/
function testQueryStringParsingFromInputUrl() {
$_GET = array();
$request = new CakeRequest('some/path?one=something&two=else');
$this->assertEqual($request->query, array('one' => 'something', 'two' => 'else'));
}
/**
* test parsing POST data into the object.
*
@ -530,6 +541,9 @@ class CakeRequestTestCase extends CakeTestCase {
$this->assertFalse(isset($request['plugin']));
$this->assertNull($request['plugin']);
$this->assertNull($request->plugin);
$request = new CakeRequest('some/path?one=something&two=else');
$this->assertTrue(isset($request['url']['one']));
}
/**
@ -579,49 +593,6 @@ class CakeRequestTestCase extends CakeTestCase {
return $request->return == true;
}
/**
* testGetUrl method
*
* @return void
*/
public function XXtestGetUrl() {
$request = new CakeRequest();
$request->base = '/app/webroot/index.php';
$uri = '/app/webroot/index.php/posts/add';
unset($_GET['url']);
$result = $request->getUrl($uri);
$expected = 'posts/add';
$this->assertEqual($expected, $result);
Configure::write('App.baseUrl', '/app/webroot/index.php');
$uri = '/posts/add';
$result = $request->getUrl($uri);
$expected = 'posts/add';
$this->assertEqual($expected, $result);
$_GET['url'] = array();
Configure::write('App.base', '/control');
$request = new CakeRequest();
unset($_GET['url']);
$request->baseUrl();
$uri = '/control/students/browse';
$result = $request->getUrl($uri);
$expected = 'students/browse';
$this->assertEqual($expected, $result);
$request = new CakeRequest();
$_GET['url'] = array();
$request->base = '';
$uri = '/?/home';
$result = $request->getUrl($uri);
$expected = '?/home';
$this->assertEqual($expected, $result);
}
/**
* testBaseUrlAndWebrootWithModRewrite method
*