From e53074dab9dd2a579a5691939eb0a0e27ef9e5bb Mon Sep 17 00:00:00 2001 From: Jelle Henkens Date: Mon, 7 May 2012 21:48:58 +0100 Subject: [PATCH] Fixing order inconsistency in queryString and bug with string based existing querystring --- lib/Cake/Routing/Router.php | 11 +++++-- lib/Cake/Test/Case/Routing/RouterTest.php | 37 +++++++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/lib/Cake/Routing/Router.php b/lib/Cake/Routing/Router.php index 0f0a60947..93d25c353 100644 --- a/lib/Cake/Routing/Router.php +++ b/lib/Cake/Routing/Router.php @@ -957,12 +957,19 @@ class Router { $out = ''; if (is_array($q)) { - $q = array_merge($extra, $q); + $q = array_merge($q, $extra); } else { $out = $q; $q = $extra; } - $out .= http_build_query($q, null, $join); + $addition = http_build_query($q, null, $join); + + if ($out && $addition) { + $out .= $join; + } + + $out .= $addition; + if (isset($out[0]) && $out[0] != '?') { $out = '?' . $out; } diff --git a/lib/Cake/Test/Case/Routing/RouterTest.php b/lib/Cake/Test/Case/Routing/RouterTest.php index 919798130..09638f50b 100644 --- a/lib/Cake/Test/Case/Routing/RouterTest.php +++ b/lib/Cake/Test/Case/Routing/RouterTest.php @@ -2590,4 +2590,41 @@ class RouterTest extends CakeTestCase { Router::defaultRouteClass('NonExistentClass'); } +/** + * Tests generating well-formed querystrings + * + * @return void + */ + public function testQueryString() { + $result = Router::queryString(array('var' => 'foo bar')); + $expected = '?var=foo+bar'; + $this->assertEquals($expected, $result); + + $result = Router::queryString(false, array('some' => 'param', 'foo' => 'bar')); + $expected = '?some=param&foo=bar'; + $this->assertEquals($expected, $result); + + $existing = array('apple' => 'red', 'pear' => 'green'); + $result = Router::queryString($existing, array('some' => 'param', 'foo' => 'bar')); + $expected = '?apple=red&pear=green&some=param&foo=bar'; + $this->assertEquals($expected, $result); + + $existing = 'apple=red&pear=green'; + $result = Router::queryString($existing, array('some' => 'param', 'foo' => 'bar')); + $expected = '?apple=red&pear=green&some=param&foo=bar'; + $this->assertEquals($expected, $result); + + $existing = '?apple=red&pear=green'; + $result = Router::queryString($existing, array('some' => 'param', 'foo' => 'bar')); + $expected = '?apple=red&pear=green&some=param&foo=bar'; + $this->assertEquals($expected, $result); + + $result = Router::queryString('apple=red&pear=green'); + $expected = '?apple=red&pear=green'; + $this->assertEquals($expected, $result); + + $result = Router::queryString('foo=bar', array('php' => 'nut', 'jose' => 'zap'), true); + $expected = '?foo=bar&php=nut&jose=zap'; + $this->assertEquals($expected, $result); + } }