From 40bca7198560a7ed939be82d8a99e06143c61fd3 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Sat, 1 May 2010 11:04:41 -0400 Subject: [PATCH] Adding tests and features for CakeRequest to parse querystring args from the url parameter in the constructor. Added tests. --- cake/libs/cake_request.php | 39 +++++++++----- cake/tests/cases/libs/cake_request.test.php | 57 +++++---------------- 2 files changed, 41 insertions(+), 55 deletions(-) diff --git a/cake/libs/cake_request.php b/cake/libs/cake_request.php index 57384efd7..ccad1463d 100644 --- a/cake/libs/cake_request.php +++ b/cake/libs/cake_request.php @@ -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; } diff --git a/cake/tests/cases/libs/cake_request.test.php b/cake/tests/cases/libs/cake_request.test.php index 5a99080c9..312b09372 100644 --- a/cake/tests/cases/libs/cake_request.test.php +++ b/cake/tests/cases/libs/cake_request.test.php @@ -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 *