Skip to content

Commit

Permalink
Correcting issue in String::insert() where 0 offsets were handled inc…
Browse files Browse the repository at this point in the history
…orrectly, affecting issue where values were not properly quoted and inserted into conditions strings. Fixes #6163.

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8085 3807eeeb-6ff5-0310-8944-8be069107fe0
  • Loading branch information
nateabele committed Mar 9, 2009
1 parent eea46e7 commit cb95f13
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 17 deletions.
8 changes: 5 additions & 3 deletions cake/libs/model/datasources/dbo_source.php
Expand Up @@ -1904,6 +1904,7 @@ function __parseKey($model, $key, $value) {
$key = substr($key, 0, $split);
}
}

$type = (is_object($model) ? $model->getColumnType($key) : null);
$null = ($value === null || (is_array($value) && empty($value)));

Expand All @@ -1915,9 +1916,10 @@ function __parseKey($model, $key, $value) {
}
$value = $this->value($value, $type);

$key = (strpos($key, '(') !== false || strpos($key, ')') !== false) ?
$this->__quoteFields($key) :
$this->name($key);
if ($key !== '?') {
$isKey = (strpos($key, '(') !== false || strpos($key, ')') !== false);
$key = $isKey ? $this->__quoteFields($key) : $this->name($key);
}

if ($bound) {
return String::insert($key . ' ' . trim($operator), $value);
Expand Down
2 changes: 1 addition & 1 deletion cake/libs/router.php
Expand Up @@ -215,7 +215,7 @@ function connect($route, $default = array(), $params = array()) {
return $_this->routes;
}
/**
*Specifies what named parameters CakePHP should be parsing. The most common setups are:
* Specifies what named parameters CakePHP should be parsing. The most common setups are:
*
* Do not parse any named parameters:
* {{{ Router::connectNamed(false); }}}
Expand Down
8 changes: 4 additions & 4 deletions cake/libs/string.php
Expand Up @@ -211,10 +211,10 @@ function tokenize($data, $separator = ',', $leftBound = '(', $rightBound = ')')
* @static
*/
function insert($str, $data, $options = array()) {
$options = array_merge(
array('before' => ':', 'after' => null, 'escape' => '\\', 'format' => null, 'clean' => false),
$options
$defaults = array(
'before' => ':', 'after' => null, 'escape' => '\\', 'format' => null, 'clean' => false
);
$options += $defaults;
$format = $options['format'];

if (!isset($format)) {
Expand All @@ -231,7 +231,7 @@ function insert($str, $data, $options = array()) {

if (array_keys($data) === array_keys(array_values($data))) {
$offset = 0;
while ($pos = strpos($str, '?', $offset)) {
while (($pos = strpos($str, '?', $offset)) !== false) {
$val = array_shift($data);
$offset = $pos + strlen($val);
$str = substr_replace($str, $val, $pos, 1);
Expand Down
16 changes: 10 additions & 6 deletions cake/tests/cases/libs/model/datasources/dbo_source.test.php
Expand Up @@ -2404,12 +2404,10 @@ function testArrayConditionsParsing() {
$expected = " WHERE `client_id` > 20";
$this->assertEqual($result, $expected);

$result = $this->testDb->conditions(array(
'OR' => array(
array('User.user' => 'mariano'),
array('User.user' => 'nate')
)
));
$result = $this->testDb->conditions(array('OR' => array(
array('User.user' => 'mariano'),
array('User.user' => 'nate')
)));

$expected = " WHERE ((`User`.`user` = 'mariano') OR (`User`.`user` = 'nate'))";
$this->assertEqual($result, $expected);
Expand Down Expand Up @@ -2545,6 +2543,12 @@ function testArrayConditionsParsing() {
));
$expected = ' WHERE ASCII(SUBSTRING(keyword, 1, 1)) BETWEEN 65 AND 90';
$this->assertEqual($result, $expected);

$result = $this->testDb->conditions(array('or' => array(
'? BETWEEN Model.field1 AND Model.field2' => '2009-03-04'
)));
$expected = " WHERE '2009-03-04' BETWEEN Model.field1 AND Model.field2";
$this->assertEqual($result, $expected);
}
/**
* testArrayConditionsParsingComplexKeys method
Expand Down
17 changes: 14 additions & 3 deletions cake/tests/cases/libs/string.test.php
Expand Up @@ -40,7 +40,8 @@ class StringTest extends CakeTestCase {
*/
function testUuidGeneration() {
$result = String::uuid();
$match = preg_match("/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/", $result);
$pattern = "/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/";
$match = preg_match($pattern, $result);
$this->assertTrue($match);
}
/**
Expand All @@ -52,9 +53,11 @@ function testUuidGeneration() {
function testMultipleUuidGeneration() {
$check = array();
$count = mt_rand(10, 1000);
$pattern = "/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/";

for($i = 0; $i < $count; $i++) {
$result = String::uuid();
$match = preg_match("/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/", $result);
$match = preg_match($pattern, $result);
$this->assertTrue($match);
$this->assertFalse(in_array($result, $check));
$check[] = $result;
Expand Down Expand Up @@ -179,9 +182,17 @@ function testInsert() {
$expected = "We are of course passing.";
$this->assertEqual($result, $expected);

$result = String::insert(':I.am: :not.yet: passing.', array('I.am' => 'We are'), array('before' => ':', 'after' => ':', 'clean' => true));
$result = String::insert(
':I.am: :not.yet: passing.',
array('I.am' => 'We are'),
array('before' => ':', 'after' => ':', 'clean' => true)
);
$expected = "We are passing.";
$this->assertEqual($result, $expected);

$result = String::insert('?-pended result', array('Pre'));
$expected = "Pre-pended result";
$this->assertEqual($result, $expected);
}
/**
* test Clean Insert
Expand Down

0 comments on commit cb95f13

Please sign in to comment.