Permalink
Browse files

Use correct value when using virtualFields in conditions and IN (), r…

…efs PR#897

Squashed commit of the following:

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

    Merge branch 'master' into virtualcondition

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

    fix incorrect tests

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

    add test for regular fields and conditionKeysToString with IN

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

    Merge branch 'master' into virtualcondition

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

    use correct value when using virtualFields in conditions and IN ()
  • Loading branch information...
1 parent 118dd8c commit 093275aef6ec79db2e6135925a37bd37b8eec4a3 @ceeram ceeram committed Nov 2, 2012
@@ -2490,16 +2490,16 @@ public function conditionKeysToString($conditions, $quoteValues = true, $model =
$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) {
@@ -1960,7 +1960,7 @@ public function testStringConditionsParsing() {
$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 @@ public function testArrayConditionsParsing() {
$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 @@ public function testArrayConditionsParsing() {
'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 @@ public function testArrayConditionsParsing() {
$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)');
@@ -1103,4 +1103,73 @@ public function testBuildJoinStatement($join, $expected) {
$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'));
@renan
renan Nov 2, 2012 Member

Why not quote it?

+ $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]);
+ }
+
}

0 comments on commit 093275a

Please sign in to comment.