Use correct value when using virtualFields in conditions and IN (), refs PR#897

Squashed commit of the following:

commit 6a986953e16601fb34a132df24ae16f882f218cf
Merge: babd714 118dd8c
Author: Ceeram <c33ram@gmail.com>
Date:   Fri Nov 2 13:02:15 2012 +0100

    Merge branch 'master' into virtualcondition

commit babd714d80178c68b0e3fbcc21fc53b846484fd8
Author: Ceeram <c33ram@gmail.com>
Date:   Fri Nov 2 00:22:43 2012 +0100

    fix incorrect tests

commit 9a46c13e1aa13905b8e5474947933676e03009ce
Author: Ceeram <c33ram@gmail.com>
Date:   Thu Nov 1 11:44:19 2012 +0100

    add test for regular fields and conditionKeysToString with IN

commit 3aa62e5cb93aa0c7bbb47e874fc1446873dd0d27
Merge: a8f0c3d 1f31340
Author: Ceeram <c33ram@gmail.com>
Date:   Thu Nov 1 11:37:01 2012 +0100

    Merge branch 'master' into virtualcondition

commit a8f0c3d918761ff2c456cc3a53f1e3ee5b1c6173
Author: Ceeram <c33ram@gmail.com>
Date:   Thu Oct 11 16:46:10 2012 +0200

    use correct value when using virtualFields in conditions and IN ()
This commit is contained in:
Ceeram 2012-11-02 14:29:38 +01:00
parent 118dd8c534
commit 093275aef6
3 changed files with 81 additions and 12 deletions

View file

@ -2490,16 +2490,16 @@ class DboSource extends DataSource {
$count = count($value);
if ($count === 1 && !preg_match("/\s+NOT$/", $key)) {
$data = $this->_quoteFields($key) . ' = (';
} else {
$data = $this->_quoteFields($key) . ' IN (';
}
if ($quoteValues) {
if (is_object($model)) {
$columnType = $model->getColumnType($key);
if ($quoteValues) {
if (is_object($model)) {
$columnType = $model->getColumnType($key);
}
$data .= implode(', ', $this->value($value, $columnType));
}
$data .= implode(', ', $this->value($value, $columnType));
$data .= ')';
} else {
$data = $this->_parseKey($model, $key, $value);
}
$data .= ')';
} else {
$ret = $this->conditionKeysToString($value, $quoteValues, $model);
if (count($ret) > 1) {

View file

@ -1960,7 +1960,7 @@ class MysqlTest extends CakeTestCase {
$this->assertEquals($expected, $result);
$result = $this->Dbo->conditions(array('score' => array(2 => 1, 2, 10)));
$expected = " WHERE score IN (1, 2, 10)";
$expected = " WHERE `score` IN (1, 2, 10)";
$this->assertEquals($expected, $result);
$result = $this->Dbo->conditions("Aro.rght = Aro.lft + 1.1");
@ -2240,7 +2240,7 @@ class MysqlTest extends CakeTestCase {
$this->assertEquals($expected, $result);
$result = $this->Dbo->conditions(array('score' => array(1, 2, 10)));
$expected = " WHERE score IN (1, 2, 10)";
$expected = " WHERE `score` IN (1, 2, 10)";
$this->assertEquals($expected, $result);
$result = $this->Dbo->conditions(array('score' => array()));
@ -2331,7 +2331,7 @@ class MysqlTest extends CakeTestCase {
'NOT' => array('Course.id' => null, 'Course.vet' => 'N', 'level_of_education_id' => array(912,999)),
'Enrollment.yearcompleted >' => '0')
);
$this->assertRegExp('/^\s*WHERE\s+\(NOT\s+\(`Course`\.`id` IS NULL\)\s+AND NOT\s+\(`Course`\.`vet`\s+=\s+\'N\'\)\s+AND NOT\s+\(level_of_education_id IN \(912, 999\)\)\)\s+AND\s+`Enrollment`\.`yearcompleted`\s+>\s+\'0\'\s*$/', $result);
$this->assertRegExp('/^\s*WHERE\s+\(NOT\s+\(`Course`\.`id` IS NULL\)\s+AND NOT\s+\(`Course`\.`vet`\s+=\s+\'N\'\)\s+AND NOT\s+\(`level_of_education_id` IN \(912, 999\)\)\)\s+AND\s+`Enrollment`\.`yearcompleted`\s+>\s+\'0\'\s*$/', $result);
$result = $this->Dbo->conditions(array('id <>' => '8'));
$this->assertRegExp('/^\s*WHERE\s+`id`\s+<>\s+\'8\'\s*$/', $result);
@ -2367,7 +2367,7 @@ class MysqlTest extends CakeTestCase {
$conditions = array('id' => array(2, 5, 6, 9, 12, 45, 78, 43, 76));
$result = $this->Dbo->conditions($conditions);
$expected = " WHERE id IN (2, 5, 6, 9, 12, 45, 78, 43, 76)";
$expected = " WHERE `id` IN (2, 5, 6, 9, 12, 45, 78, 43, 76)";
$this->assertEquals($expected, $result);
$conditions = array('title' => 'user(s)');

View file

@ -1103,4 +1103,73 @@ class DboSourceTest extends CakeTestCase {
$this->assertEquals($expected, $result);
}
/**
* Test conditionKeysToString()
*
* @return void
*/
public function testConditionKeysToString() {
$Article = ClassRegistry::init('Article');
$conn = $this->getMock('MockPDO', array('quote'));
$db = new DboTestSource;
$db->setConnection($conn);
$conn->expects($this->at(0))
->method('quote')
->will($this->returnValue('just text'));
$conditions = array('Article.name' => 'just text');
$result = $db->conditionKeysToString($conditions, true, $Article);
$expected = "Article.name = just text";
$this->assertEquals($expected, $result[0]);
$conn->expects($this->at(0))
->method('quote')
->will($this->returnValue('just text'));
$conn->expects($this->at(1))
->method('quote')
->will($this->returnValue('other text'));
$conditions = array('Article.name' => array('just text', 'other text'));
$result = $db->conditionKeysToString($conditions, true, $Article);
$expected = "Article.name IN (just text, other text)";
$this->assertEquals($expected, $result[0]);
}
/**
* Test conditionKeysToString() with virtual field
*
* @return void
*/
public function testConditionKeysToStringVirtualField() {
$Article = ClassRegistry::init('Article');
$Article->virtualFields = array(
'extra' => 'something virtual'
);
$conn = $this->getMock('MockPDO', array('quote'));
$db = new DboTestSource;
$db->setConnection($conn);
$conn->expects($this->at(0))
->method('quote')
->will($this->returnValue('just text'));
$conditions = array('Article.extra' => 'just text');
$result = $db->conditionKeysToString($conditions, true, $Article);
$expected = "(" . $Article->virtualFields['extra'] . ") = just text";
$this->assertEquals($expected, $result[0]);
$conn->expects($this->at(0))
->method('quote')
->will($this->returnValue('just text'));
$conn->expects($this->at(1))
->method('quote')
->will($this->returnValue('other text'));
$conditions = array('Article.extra' => array('just text', 'other text'));
$result = $db->conditionKeysToString($conditions, true, $Article);
$expected = "(" . $Article->virtualFields['extra'] . ") IN (just text, other text)";
$this->assertEquals($expected, $result[0]);
}
}