Adding fixes for Ticket #2357, Ticket #2358

Added tests from Ticket #2357, Ticket #2358
Fixed failing DboSourceTest:
source:/branches/1.2.x.x/cake/tests/cases/libs/model/datasources/dbo_source.test.php@4778#L1080


git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@4801 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
phpnut 2007-04-06 11:15:45 +00:00
parent 48f3349c9a
commit d5fa0a961b
2 changed files with 57 additions and 28 deletions

View file

@ -1403,13 +1403,7 @@ class DboSource extends DataSource {
for($i = 0; $i < $pregCount; $i++) { for($i = 0; $i < $pregCount; $i++) {
if (!empty($match['1'][$i]) && !is_numeric($match['1'][$i])) { if (!empty($match['1'][$i]) && !is_numeric($match['1'][$i])) {
$conditions = preg_replace('/^' . $match['0'][$i] . '(?=[^\\w])/', ' ' . $this->name($match['1'][$i]), $conditions . ' '); $conditions = preg_replace('/\b' . $match['0'][$i] . '\b/', $this->name($match['1'][$i]), $conditions);}
if (strpos($conditions, '(' . $match['0'][$i]) === false) {
$conditions = preg_replace('/[^\w]' . $match['0'][$i] . '(?=[^\\w])/', ' '.$this->name($match['1'][$i]), $conditions);
} else {
$conditions = preg_replace('/' . $match['0'][$i] . '(?=[^\\w])/', ' '.$this->name($match['1'][$i]), $conditions);
}
}
} }
$conditions = rtrim($conditions); $conditions = rtrim($conditions);
} }
@ -1510,12 +1504,39 @@ class DboSource extends DataSource {
$data = $this->name($key) . ' ' . $match['1'] . ' ' . $match['2']; $data = $this->name($key) . ' ' . $match['1'] . ' ' . $match['2'];
} else { } else {
if (!empty($match['2']) && $quoteValues) { if (!empty($match['2']) && $quoteValues) {
if (strpos($match['2'], '()') === false) {
$match['2'] = $this->value($match['2']); $match['2'] = $this->value($match['2']);
}
$match['2'] = str_replace(' AND ', "' AND '", $match['2']); $match['2'] = str_replace(' AND ', "' AND '", $match['2']);
} }
$start = null;
$end = null;
if (!empty($this->startQuote)) {
$start = '\\\\' . $this->startQuote . '\\\\';
}
$end = $this->endQuote;
if (!empty($this->endQuote)) {
$end = '\\\\' . $this->endQuote . '\\\\';
}
$key = str_replace(array($this->startQuote, $this->endQuote), '', $key);
preg_match_all('/(?:\'[^\'\\\]*(?:\\\.[^\'\\\]*)*\')|([a-z0-9_' . $start . $end . ']*\\.[a-z0-9_' . $start . $end . ']*)/i', $key, $replace, PREG_PATTERN_ORDER);
if (isset($replace['1']['0'])) {
$pregCount = count($replace['1']);
for($i = 0; $i < $pregCount; $i++) {
if (!empty($replace['1'][$i]) && !is_numeric($replace['1'][$i])) {
$key = preg_replace('/\b' . $replace['0'][$i] . '\b/', $this->name($replace['1'][$i]), $key);}
}
$data = $key . ' ' . $match['1'] . ' ' . $match['2'];
} else {
$data = $this->name($key) . ' ' . $match['1'] . ' ' . $match['2']; $data = $this->name($key) . ' ' . $match['1'] . ' ' . $match['2'];
} }
} }
}
if ($data != null) { if ($data != null) {
$out[] = $data; $out[] = $data;

View file

@ -995,6 +995,10 @@ class DboSourceTest extends UnitTestCase {
$result = $this->db->conditions('((MATCH(Video.title) AGAINST(\'My Search*\' IN BOOLEAN MODE) * 2) + (MATCH(Video.description) AGAINST(\'My Search*\' IN BOOLEAN MODE) * 0.4) + (MATCH(Video.tags) AGAINST(\'My Search*\' IN BOOLEAN MODE) * 1.5))'); $result = $this->db->conditions('((MATCH(Video.title) AGAINST(\'My Search*\' IN BOOLEAN MODE) * 2) + (MATCH(Video.description) AGAINST(\'My Search*\' IN BOOLEAN MODE) * 0.4) + (MATCH(Video.tags) AGAINST(\'My Search*\' IN BOOLEAN MODE) * 1.5))');
$expected = ' WHERE ((MATCH(`Video`.`title`) AGAINST(\'My Search*\' IN BOOLEAN MODE) * 2) + (MATCH(`Video`.`description`) AGAINST(\'My Search*\' IN BOOLEAN MODE) * 0.4) + (MATCH(`Video`.`tags`) AGAINST(\'My Search*\' IN BOOLEAN MODE) * 1.5))'; $expected = ' WHERE ((MATCH(`Video`.`title`) AGAINST(\'My Search*\' IN BOOLEAN MODE) * 2) + (MATCH(`Video`.`description`) AGAINST(\'My Search*\' IN BOOLEAN MODE) * 0.4) + (MATCH(`Video`.`tags`) AGAINST(\'My Search*\' IN BOOLEAN MODE) * 1.5))';
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = $this->db->conditions('DATEDIFF(NOW(),Article.published) < 1 && Article.live=1');
$expected = " WHERE DATEDIFF(NOW(),`Article`.`published`) < 1 && `Article`.`live`=1";
$this->assertEqual($result, $expected);
} }
function testArrayConditionsParsing() { function testArrayConditionsParsing() {
@ -1077,8 +1081,12 @@ class DboSourceTest extends UnitTestCase {
$result = $this->db->conditions(array('(Usergroup.permissions) & 4' => 4)); $result = $this->db->conditions(array('(Usergroup.permissions) & 4' => 4));
$this->assertPattern('/^\s*WHERE\s+\(`Usergroup`\.`permissions`\)\s+& 4\s+=\s+4\s*$/', $result); $this->assertPattern('/^\s*WHERE\s+\(`Usergroup`\.`permissions`\)\s+& 4\s+=\s+4\s*$/', $result);
//$result = $this->db->conditions(array('((Usergroup.permissions) & 4)' => 4)); $result = $this->db->conditions(array('((Usergroup.permissions) & 4)' => 4));
//$this->assertPattern('/^\s*WHERE\s+\(\(`Usergroup`\.`permissions`\)\s+& 4\)\s+=\s+4\s*$/', $result); $this->assertPattern('/^\s*WHERE\s+\(\(`Usergroup`\.`permissions`\)\s+& 4\)\s+=\s+4\s*$/', $result);
$result = $this->db->conditions(array('Post.modified' => '>= DATE_SUB(NOW(), INTERVAL 7 DAY)'));
$expected = " WHERE `Post`.`modified` >= DATE_SUB(NOW(), INTERVAL 7 DAY)";
$this->assertEqual($result, $expected);
$result = $this->db->conditions(array( $result = $this->db->conditions(array(
'NOT' => array('Course.id' => null, 'Course.vet' => 'N', 'level_of_education_id' => array(912,999)), 'NOT' => array('Course.id' => null, 'Course.vet' => 'N', 'level_of_education_id' => array(912,999)),