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,10 +1504,37 @@ 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) {
$match['2'] = $this->value($match['2']); if (strpos($match['2'], '()') === false) {
$match['2'] = $this->value($match['2']);
}
$match['2'] = str_replace(' AND ', "' AND '", $match['2']); $match['2'] = str_replace(' AND ', "' AND '", $match['2']);
} }
$data = $this->name($key) . ' ' . $match['1'] . ' ' . $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'];
}
} }
} }

View file

@ -924,27 +924,27 @@ class DboSourceTest extends UnitTestCase {
function testStringConditionsParsing() { function testStringConditionsParsing() {
$result = $this->db->conditions("Candy.name LIKE 'a' AND HardCandy.name LIKE 'c'"); $result = $this->db->conditions("Candy.name LIKE 'a' AND HardCandy.name LIKE 'c'");
$expected = " WHERE `Candy`.`name` LIKE 'a' AND `HardCandy`.`name` LIKE 'c'"; $expected = " WHERE `Candy`.`name` LIKE 'a' AND `HardCandy`.`name` LIKE 'c'";
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = $this->db->conditions("HardCandy.name LIKE 'a' AND Candy.name LIKE 'c'"); $result = $this->db->conditions("HardCandy.name LIKE 'a' AND Candy.name LIKE 'c'");
$expected = " WHERE `HardCandy`.`name` LIKE 'a' AND `Candy`.`name` LIKE 'c'"; $expected = " WHERE `HardCandy`.`name` LIKE 'a' AND `Candy`.`name` LIKE 'c'";
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = $this->db->conditions("Post.title = '1.1'"); $result = $this->db->conditions("Post.title = '1.1'");
$expected = " WHERE `Post`.`title` = '1.1'"; $expected = " WHERE `Post`.`title` = '1.1'";
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = $this->db->conditions("User.id != 0 AND User.user LIKE '%arr%'"); $result = $this->db->conditions("User.id != 0 AND User.user LIKE '%arr%'");
$expected = " WHERE `User`.`id` != 0 AND `User`.`user` LIKE '%arr%'"; $expected = " WHERE `User`.`id` != 0 AND `User`.`user` LIKE '%arr%'";
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = $this->db->conditions("SUM(Post.comments_count) > 500"); $result = $this->db->conditions("SUM(Post.comments_count) > 500");
$expected = " WHERE SUM( `Post`.`comments_count`) > 500"; $expected = " WHERE SUM(`Post`.`comments_count`) > 500";
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = $this->db->conditions("(Post.created < '" . date('Y-m-d H:i:s') . "') GROUP BY YEAR(Post.created), MONTH(Post.created)"); $result = $this->db->conditions("(Post.created < '" . date('Y-m-d H:i:s') . "') GROUP BY YEAR(Post.created), MONTH(Post.created)");
$expected = " WHERE ( `Post`.`created` < '" . date('Y-m-d H:i:s') . "') GROUP BY YEAR( `Post`.`created`), MONTH( `Post`.`created`)"; $expected = " WHERE (`Post`.`created` < '" . date('Y-m-d H:i:s') . "') GROUP BY YEAR(`Post`.`created`), MONTH(`Post`.`created`)";
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = $this->db->conditions("score BETWEEN 90.1 AND 95.7"); $result = $this->db->conditions("score BETWEEN 90.1 AND 95.7");
@ -952,32 +952,32 @@ class DboSourceTest extends UnitTestCase {
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = $this->db->conditions("Aro.rght = Aro.lft + 1.1"); $result = $this->db->conditions("Aro.rght = Aro.lft + 1.1");
$expected = " WHERE `Aro`.`rght` = `Aro`.`lft` + 1.1"; $expected = " WHERE `Aro`.`rght` = `Aro`.`lft` + 1.1";
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = $this->db->conditions("(Post.created < '" . date('Y-m-d H:i:s') . "') GROUP BY YEAR(Post.created), MONTH(Post.created)"); $result = $this->db->conditions("(Post.created < '" . date('Y-m-d H:i:s') . "') GROUP BY YEAR(Post.created), MONTH(Post.created)");
$expected = " WHERE ( `Post`.`created` < '" . date('Y-m-d H:i:s') . "') GROUP BY YEAR( `Post`.`created`), MONTH( `Post`.`created`)"; $expected = " WHERE (`Post`.`created` < '" . date('Y-m-d H:i:s') . "') GROUP BY YEAR(`Post`.`created`), MONTH(`Post`.`created`)";
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = $this->db->conditions('Sportstaette.sportstaette LIKE "%ru%" AND Sportstaette.sportstaettenart_id = 2'); $result = $this->db->conditions('Sportstaette.sportstaette LIKE "%ru%" AND Sportstaette.sportstaettenart_id = 2');
$expected = ' WHERE `Sportstaette`.`sportstaette` LIKE "%ru%" AND `Sportstaette`.`sportstaettenart_id` = 2'; $expected = ' WHERE `Sportstaette`.`sportstaette` LIKE "%ru%" AND `Sportstaette`.`sportstaettenart_id` = 2';
$this->assertPattern('/\s*WHERE\s+`Sportstaette`\.`sportstaette`\s+LIKE\s+"%ru%"\s+AND\s+`Sports/', $result); $this->assertPattern('/\s*WHERE\s+`Sportstaette`\.`sportstaette`\s+LIKE\s+"%ru%"\s+AND\s+`Sports/', $result);
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = $this->db->conditions('Sportstaette.sportstaettenart_id = 2 AND Sportstaette.sportstaette LIKE "%ru%"'); $result = $this->db->conditions('Sportstaette.sportstaettenart_id = 2 AND Sportstaette.sportstaette LIKE "%ru%"');
$expected = ' WHERE `Sportstaette`.`sportstaettenart_id` = 2 AND `Sportstaette`.`sportstaette` LIKE "%ru%"'; $expected = ' WHERE `Sportstaette`.`sportstaettenart_id` = 2 AND `Sportstaette`.`sportstaette` LIKE "%ru%"';
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = $this->db->conditions('SUM(Post.comments_count) > 500 AND NOT Post.title IS NULL AND NOT Post.extended_title IS NULL'); $result = $this->db->conditions('SUM(Post.comments_count) > 500 AND NOT Post.title IS NULL AND NOT Post.extended_title IS NULL');
$expected = ' WHERE SUM( `Post`.`comments_count`) > 500 AND NOT `Post`.`title` IS NULL AND NOT `Post`.`extended_title` IS NULL'; $expected = ' WHERE SUM(`Post`.`comments_count`) > 500 AND NOT `Post`.`title` IS NULL AND NOT `Post`.`extended_title` IS NULL';
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = $this->db->conditions('NOT Post.title IS NULL AND NOT Post.extended_title IS NULL AND SUM(Post.comments_count) > 500'); $result = $this->db->conditions('NOT Post.title IS NULL AND NOT Post.extended_title IS NULL AND SUM(Post.comments_count) > 500');
$expected = ' WHERE NOT `Post`.`title` IS NULL AND NOT `Post`.`extended_title` IS NULL AND SUM( `Post`.`comments_count`) > 500'; $expected = ' WHERE NOT `Post`.`title` IS NULL AND NOT `Post`.`extended_title` IS NULL AND SUM(`Post`.`comments_count`) > 500';
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = $this->db->conditions('NOT Post.extended_title IS NULL AND NOT Post.title IS NULL AND Post.title != "" AND SPOON(SUM(Post.comments_count) + 1.1) > 500'); $result = $this->db->conditions('NOT Post.extended_title IS NULL AND NOT Post.title IS NULL AND Post.title != "" AND SPOON(SUM(Post.comments_count) + 1.1) > 500');
$expected = ' WHERE NOT `Post`.`extended_title` IS NULL AND NOT `Post`.`title` IS NULL AND `Post`.`title` != "" AND SPOON(SUM( `Post`.`comments_count`) + 1.1) > 500'; $expected = ' WHERE NOT `Post`.`extended_title` IS NULL AND NOT `Post`.`title` IS NULL AND `Post`.`title` != "" AND SPOON(SUM(`Post`.`comments_count`) + 1.1) > 500';
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = $this->db->conditions('NOT Post.title_extended IS NULL AND NOT Post.title IS NULL AND Post.title_extended != Post.title'); $result = $this->db->conditions('NOT Post.title_extended IS NULL AND NOT Post.title IS NULL AND Post.title_extended != Post.title');
@ -985,15 +985,19 @@ class DboSourceTest extends UnitTestCase {
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = $this->db->conditions("Comment.id = 'a'"); $result = $this->db->conditions("Comment.id = 'a'");
$expected = " WHERE `Comment`.`id` = 'a'"; $expected = " WHERE `Comment`.`id` = 'a'";
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = $this->db->conditions("lower(Article.title) LIKE 'a%'"); $result = $this->db->conditions("lower(Article.title) LIKE 'a%'");
$expected = " WHERE lower( `Article`.`title`) LIKE 'a%'"; $expected = " WHERE lower(`Article`.`title`) LIKE 'a%'";
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$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);
$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); $this->assertEqual($result, $expected);
} }
@ -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)),