From ea11b2aa1eec4bb25cb21e95df20b9b91898b083 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dirk=20Br=C3=BCnsicke?= Date: Tue, 3 Aug 2010 10:10:14 +0200 Subject: [PATCH] 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 --- cake/libs/string.php | 3 ++- cake/tests/cases/libs/string.test.php | 22 +++++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/cake/libs/string.php b/cake/libs/string.php index 978f3163e04..b9ae4be1bbb 100644 --- a/cake/libs/string.php +++ b/cake/libs/string.php @@ -240,6 +240,7 @@ function insert($str, $data, $options = array()) { $hashKeys = array_map('md5', array_keys($data)); $tempData = array_combine(array_keys($data), array_values($hashKeys)); + krsort($tempData); foreach ($tempData as $key => $hashVal) { $key = sprintf($format, preg_quote($key, '/')); $str = preg_replace($key, $hashVal, $str); @@ -323,4 +324,4 @@ function cleanInsert($str, $options) { return $str; } } -?> \ No newline at end of file +?> diff --git a/cake/tests/cases/libs/string.test.php b/cake/tests/cases/libs/string.test.php index 738eba27047..7c47f97e7ca 100644 --- a/cake/tests/cases/libs/string.test.php +++ b/cake/tests/cases/libs/string.test.php @@ -197,6 +197,26 @@ function testInsert() { $result = String::insert('?-pended result', array('Pre')); $expected = "Pre-pended result"; $this->assertEqual($result, $expected); + + $string = 'switching :timeout / :timeout_count'; + $expected = 'switching 5 / 10'; + $result = String::insert($string, array('timeout' => 5, 'timeout_count' => 10)); + $this->assertEqual($result, $expected); + + $string = 'switching :timeout / :timeout_count'; + $expected = 'switching 5 / 10'; + $result = String::insert($string, array('timeout_count' => 10, 'timeout' => 5)); + $this->assertEqual($result, $expected); + + $string = 'switching :timeout_count by :timeout'; + $expected = 'switching 10 by 5'; + $result = String::insert($string, array('timeout' => 5, 'timeout_count' => 10)); + $this->assertEqual($result, $expected); + + $string = 'switching :timeout_count by :timeout'; + $expected = 'switching 10 by 5'; + $result = String::insert($string, array('timeout_count' => 10, 'timeout' => 5)); + $this->assertEqual($result, $expected); } /** * test Clean Insert @@ -253,4 +273,4 @@ function testTokenize() { $this->assertEqual($expected, $result); } } -?> \ No newline at end of file +?>