From 9c7f357029bd7526b30ddfe993f1c3afe4e7373d Mon Sep 17 00:00:00 2001 From: Nicolas Date: Sat, 17 Nov 2012 12:39:08 +0100 Subject: [PATCH 1/3] Check if bool operators in find conditions are empty. --- lib/Cake/Model/Datasource/DboSource.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/Cake/Model/Datasource/DboSource.php b/lib/Cake/Model/Datasource/DboSource.php index 623b7a01f..f4fe7d777 100644 --- a/lib/Cake/Model/Datasource/DboSource.php +++ b/lib/Cake/Model/Datasource/DboSource.php @@ -2464,6 +2464,10 @@ class DboSource extends DataSource { $not = 'NOT '; } + if (empty($value)) { + continue; + } + if (empty($value[1])) { if ($not) { $out[] = $not . '(' . $value[0] . ')'; From 1b704fb7cf4d3b18e883a9979fe0e87983c7dec4 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Sat, 17 Nov 2012 19:34:02 +0100 Subject: [PATCH 2/3] Unit tests added. --- .../Case/Model/Datasource/DboSourceTest.php | 82 +++---------------- 1 file changed, 13 insertions(+), 69 deletions(-) diff --git a/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php b/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php index c38e1d085..0faec562a 100644 --- a/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php +++ b/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php @@ -159,6 +159,19 @@ class DboSourceTest extends CakeTestCase { $this->assertEquals(' WHERE 1 = 1', $result); } +/** + * test that booleans work on empty set. + * + * @return void + */ + public function testBooleanEmptyConditionsParsing() { + $result = $this->testDb->conditions(array('OR' => array())); + $this->assertEquals(' WHERE 1 = 1', $result, 'empty conditions failed %s'); + + $result = $this->testDb->conditions(array('OR' => array('OR' => array()))); + $this->assertEquals(' WHERE 1 = 1', $result, 'nested empty conditions failed %s'); + } + /** * test that order() will accept objects made from DboSource::expression * @@ -1103,73 +1116,4 @@ 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]); - } - } From a77e46cbfda7786e1ff51f84995e7b90900c77d4 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Sat, 17 Nov 2012 23:14:40 +0100 Subject: [PATCH 3/3] Added accidentally removed tests back in. --- .../Case/Model/Datasource/DboSourceTest.php | 73 ++++++++++++++++++- 1 file changed, 71 insertions(+), 2 deletions(-) diff --git a/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php b/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php index 0faec562a..e049be480 100644 --- a/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php +++ b/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php @@ -166,10 +166,10 @@ class DboSourceTest extends CakeTestCase { */ public function testBooleanEmptyConditionsParsing() { $result = $this->testDb->conditions(array('OR' => array())); - $this->assertEquals(' WHERE 1 = 1', $result, 'empty conditions failed %s'); + $this->assertEquals(' WHERE 1 = 1', $result, 'empty conditions failed'); $result = $this->testDb->conditions(array('OR' => array('OR' => array()))); - $this->assertEquals(' WHERE 1 = 1', $result, 'nested empty conditions failed %s'); + $this->assertEquals(' WHERE 1 = 1', $result, 'nested empty conditions failed'); } /** @@ -1116,4 +1116,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]); + } + }