Skip to content
This repository
Browse code

Fixing order inconsistency in queryString and bug with string based e…

…xisting querystring
  • Loading branch information...
commit 45a96ae588f88ea1046b327a12a564fb094903fc 1 parent a1e06b9
Jelle Henkens authored May 07, 2012
11  lib/Cake/Routing/Router.php
@@ -957,12 +957,19 @@ public static function queryString($q, $extra = array(), $escape = false) {
957 957
 		$out = '';
958 958
 
959 959
 		if (is_array($q)) {
960  
-			$q = array_merge($extra, $q);
  960
+			$q = array_merge($q, $extra);
961 961
 		} else {
962 962
 			$out = $q;
963 963
 			$q = $extra;
964 964
 		}
965  
-		$out .= http_build_query($q, null, $join);
  965
+		$addition = http_build_query($q, null, $join);
  966
+
  967
+		if ($out && $addition) {
  968
+			$out .= $join;
  969
+		}
  970
+
  971
+		$out .= $addition;
  972
+
966 973
 		if (isset($out[0]) && $out[0] != '?') {
967 974
 			$out = '?' . $out;
968 975
 		}
37  lib/Cake/Test/Case/Routing/RouterTest.php
@@ -2590,4 +2590,41 @@ public function testSettingNonExistentDefaultRouteException() {
2590 2590
 		Router::defaultRouteClass('NonExistentClass');
2591 2591
 	}
2592 2592
 
  2593
+/**
  2594
+ * Tests generating well-formed querystrings
  2595
+ *
  2596
+ * @return void
  2597
+ */
  2598
+	public function testQueryString() {
  2599
+		$result = Router::queryString(array('var' => 'foo bar'));
  2600
+		$expected = '?var=foo+bar';
  2601
+		$this->assertEquals($expected, $result);
  2602
+
  2603
+		$result = Router::queryString(false, array('some' => 'param', 'foo' => 'bar'));
  2604
+		$expected = '?some=param&foo=bar';
  2605
+		$this->assertEquals($expected, $result);
  2606
+
  2607
+		$existing = array('apple' => 'red', 'pear' => 'green');
  2608
+		$result = Router::queryString($existing, array('some' => 'param', 'foo' => 'bar'));
  2609
+		$expected = '?apple=red&pear=green&some=param&foo=bar';
  2610
+		$this->assertEquals($expected, $result);
  2611
+
  2612
+		$existing = 'apple=red&pear=green';
  2613
+		$result = Router::queryString($existing, array('some' => 'param', 'foo' => 'bar'));
  2614
+		$expected = '?apple=red&pear=green&some=param&foo=bar';
  2615
+		$this->assertEquals($expected, $result);
  2616
+
  2617
+		$existing = '?apple=red&pear=green';
  2618
+		$result = Router::queryString($existing, array('some' => 'param', 'foo' => 'bar'));
  2619
+		$expected = '?apple=red&pear=green&some=param&foo=bar';
  2620
+		$this->assertEquals($expected, $result);
  2621
+
  2622
+		$result = Router::queryString('apple=red&pear=green');
  2623
+		$expected = '?apple=red&pear=green';
  2624
+		$this->assertEquals($expected, $result);
  2625
+
  2626
+		$result = Router::queryString('foo=bar', array('php' => 'nut', 'jose' => 'zap'), true);
  2627
+		$expected = '?foo=bar&php=nut&jose=zap';
  2628
+		$this->assertEquals($expected, $result);
  2629
+	}
2593 2630
 }

0 notes on commit 45a96ae

Please sign in to comment.
Something went wrong with that request. Please try again.