Skip to content

Commit

Permalink
Merge pull request #638 from jellehenkens/2.1-router-querystring-bugfix
Browse files Browse the repository at this point in the history
Router::queryString() order inconsistency and bug with appending to a string instead of array
  • Loading branch information
markstory committed May 10, 2012
2 parents 12ae03b + 0773506 commit a4bbfc0
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/Cake/Routing/Router.php
Expand Up @@ -957,12 +957,19 @@ public static function queryString($q, $extra = array(), $escape = false) {
$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 && substr($out, strlen($join) * -1, strlen($join)) != $join) {
$out .= $join;
}

$out .= $addition;

if (isset($out[0]) && $out[0] != '?') {
$out = '?' . $out;
}
Expand Down
45 changes: 45 additions & 0 deletions lib/Cake/Test/Case/Routing/RouterTest.php
Expand Up @@ -2590,4 +2590,49 @@ public function testSettingNonExistentDefaultRouteException() {
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);

$result = Router::queryString('foo=bar&', array('php' => 'nut', 'jose' => 'zap'), true);
$expected = '?foo=bar&php=nut&jose=zap';
$this->assertEquals($expected, $result);

$result = Router::queryString('foo=bar&', array('php' => 'nut', 'jose' => 'zap'));
$expected = '?foo=bar&php=nut&jose=zap';
$this->assertEquals($expected, $result);
}
}

0 comments on commit a4bbfc0

Please sign in to comment.