Skip to content
This repository
Browse code

Fixing bug in String::insert that produces unexpected results with

multiple keys that start with the same substring. Fixes #984

Signed-off-by: mark_story <mark@mark-story.com>
  • Loading branch information...
commit ea11b2aa1eec4bb25cb21e95df20b9b91898b083 1 parent 4ca3266
Dirk Brünsicke authored markstory committed
3  cake/libs/string.php
@@ -240,6 +240,7 @@ function insert($str, $data, $options = array()) {
240 240
 
241 241
 			$hashKeys = array_map('md5', array_keys($data));
242 242
 			$tempData = array_combine(array_keys($data), array_values($hashKeys));
  243
+			krsort($tempData);
243 244
 			foreach ($tempData as $key => $hashVal) {
244 245
 				$key = sprintf($format, preg_quote($key, '/'));
245 246
 				$str = preg_replace($key, $hashVal, $str);
@@ -323,4 +324,4 @@ function cleanInsert($str, $options) {
323 324
 		return $str;
324 325
 	}
325 326
 }
326  
-?>
  327
+?>
22  cake/tests/cases/libs/string.test.php
@@ -197,6 +197,26 @@ function testInsert() {
197 197
 		$result = String::insert('?-pended result', array('Pre'));
198 198
 		$expected = "Pre-pended result";
199 199
 		$this->assertEqual($result, $expected);
  200
+
  201
+		$string = 'switching :timeout / :timeout_count';
  202
+		$expected = 'switching 5 / 10';
  203
+		$result = String::insert($string, array('timeout' => 5, 'timeout_count' => 10));
  204
+		$this->assertEqual($result, $expected);
  205
+
  206
+		$string = 'switching :timeout / :timeout_count';
  207
+		$expected = 'switching 5 / 10';
  208
+		$result = String::insert($string, array('timeout_count' => 10, 'timeout' => 5));
  209
+		$this->assertEqual($result, $expected);
  210
+
  211
+		$string = 'switching :timeout_count by :timeout';
  212
+		$expected = 'switching 10 by 5';
  213
+		$result = String::insert($string, array('timeout' => 5, 'timeout_count' => 10));
  214
+		$this->assertEqual($result, $expected);
  215
+
  216
+		$string = 'switching :timeout_count by :timeout';
  217
+		$expected = 'switching 10 by 5';
  218
+		$result = String::insert($string, array('timeout_count' => 10, 'timeout' => 5));
  219
+		$this->assertEqual($result, $expected);
200 220
 	}
201 221
 /**
202 222
  * test Clean Insert
@@ -253,4 +273,4 @@ function testTokenize() {
253 273
 		$this->assertEqual($expected, $result);
254 274
 	}
255 275
 }
256  
-?>
  276
+?>

0 notes on commit ea11b2a

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