From d03a02d98db3f8a3830899fc22b0f3df6cd29ef7 Mon Sep 17 00:00:00 2001 From: eileen Date: Tue, 5 Mar 2019 13:21:36 +1300 Subject: [PATCH] Fix api bug whereby 0 & '0' are not accepted as range parameters for BETWEEN In other news I'm not thrilled about the 0 casting to a string here but that is for another time --- CRM/Core/DAO.php | 2 +- tests/phpunit/api/v3/ContactTest.php | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/CRM/Core/DAO.php b/CRM/Core/DAO.php index 55a25aeb9a..02b3d3d158 100644 --- a/CRM/Core/DAO.php +++ b/CRM/Core/DAO.php @@ -2542,7 +2542,7 @@ SELECT contact_id // ternary operators case 'BETWEEN': case 'NOT BETWEEN': - if (empty($criteria[0]) || empty($criteria[1])) { + if ((empty($criteria[0]) && !in_array($criteria[0], ['0', 0]))|| (empty($criteria[1]) && !in_array($criteria[1], ['0', 0]))) { throw new Exception("invalid criteria for $operator"); } if (!$returnSanitisedArray) { diff --git a/tests/phpunit/api/v3/ContactTest.php b/tests/phpunit/api/v3/ContactTest.php index 16bac6e300..db06ecd190 100644 --- a/tests/phpunit/api/v3/ContactTest.php +++ b/tests/phpunit/api/v3/ContactTest.php @@ -1017,7 +1017,26 @@ class api_v3_ContactTest extends CiviUnitTestCase { 'last_name' => "O'Connor", 'sequential' => 1, )); - $this->assertEquals("O'Connor", $result['last_name'], 'in line' . __LINE__); + $this->assertEquals("O'Connor", $result['last_name']); + } + + /** + * Test between accepts zero. + * + * In the past it incorrectly required !empty. + */ + public function testGetBetweenZeroWorks() { + $this->callAPISuccess($this->_entity, 'get', [ + 'contact_id' => ['BETWEEN' => [0, 9]], + ]); + $this->callAPISuccess($this->_entity, 'get', [ + 'contact_id' => [ + 'BETWEEN' => [ + (0 - 9), + 0, + ], + ], + ]); } /** -- 2.25.1