From 673fae6c4f88642118e8c700523f43c65ce143fc Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 24 Jun 2019 07:03:34 +1200 Subject: [PATCH] Cast = to IN as tests show this PR otherwise breaks on resolving parent-child --- CRM/Contact/BAO/Query.php | 17 +++++++++++++---- .../CRM/Contact/BAO/GroupContactTest.php | 4 +++- tests/phpunit/CRM/Contact/BAO/QueryTest.php | 1 + tests/phpunit/CRM/Contact/SelectorTest.php | 5 +---- 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index 16520e629f..11aa9f4996 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -2973,9 +2973,18 @@ class CRM_Contact_BAO_Query { $op = key($value); $value = $value[$op]; } - - // Replace pseudo operators from search builder - $op = str_replace('EMPTY', 'NULL', $op); + // Translate EMPTY to NULL as EMPTY is cannot be used in it's intended meaning here + // so has to be 'squashed into' NULL. (ie. group membership cannot be ''). + // even one group might equate to multiple when looking at children so IN is simpler. + // @todo - also look at != casting but there are rows below to review. + $opReplacements = [ + 'EMPTY' => 'NULL', + 'NOT EMPTY' => 'NOT NULL', + '=' => 'IN', + ]; + if (isset($opReplacements[$op])) { + $op = $opReplacements[$op]; + } if (strpos($op, 'NULL')) { $value = NULL; @@ -3051,7 +3060,7 @@ class CRM_Contact_BAO_Query { $clause = "{$gcTable}.contact_id NOT IN (SELECT contact_id FROM civicrm_group_contact cgc WHERE cgc.group_id = $groupIds )"; } else { - $clause = self::buildClause("{$gcTable}.group_id", $op, ($op === '=' ? $regularGroupIDs[0] : $regularGroupIDs)); + $clause = self::buildClause("{$gcTable}.group_id", $op, $regularGroupIDs); } $groupClause[] = "( {$clause} )"; diff --git a/tests/phpunit/CRM/Contact/BAO/GroupContactTest.php b/tests/phpunit/CRM/Contact/BAO/GroupContactTest.php index 718a623249..8002a15b60 100644 --- a/tests/phpunit/CRM/Contact/BAO/GroupContactTest.php +++ b/tests/phpunit/CRM/Contact/BAO/GroupContactTest.php @@ -91,6 +91,8 @@ class CRM_Contact_BAO_GroupContactTest extends CiviUnitTestCase { /** * Test case for contact search: CRM-6706, CRM-6586 Parent Group search should return contacts from child groups too. + * + * @throws \Exception */ public function testContactSearchByParentGroup() { // create a parent group @@ -146,7 +148,7 @@ class CRM_Contact_BAO_GroupContactTest extends CiviUnitTestCase { $childContactParams = array( 'first_name' => 'Child1 Fname', 'last_name' => 'Child2 Lname', - 'group' => array($childGroup['id'] => 1), + 'group' => [$childGroup['id'] => 1], ); $childContact = $this->individualCreate($childContactParams); diff --git a/tests/phpunit/CRM/Contact/BAO/QueryTest.php b/tests/phpunit/CRM/Contact/BAO/QueryTest.php index a33d6edd27..2303dd6e81 100644 --- a/tests/phpunit/CRM/Contact/BAO/QueryTest.php +++ b/tests/phpunit/CRM/Contact/BAO/QueryTest.php @@ -36,6 +36,7 @@ class CRM_Contact_BAO_QueryTest extends CiviUnitTestCase { 'civicrm_address', ]; $this->quickCleanup($tablesToTruncate); + parent::tearDown(); } /** diff --git a/tests/phpunit/CRM/Contact/SelectorTest.php b/tests/phpunit/CRM/Contact/SelectorTest.php index f19e125342..c45ba19b56 100644 --- a/tests/phpunit/CRM/Contact/SelectorTest.php +++ b/tests/phpunit/CRM/Contact/SelectorTest.php @@ -37,10 +37,6 @@ */ class CRM_Contact_SelectorTest extends CiviUnitTestCase { - public function tearDown() { - - } - /** * Test the query from the selector class is consistent with the dataset expectation. * @@ -189,6 +185,7 @@ class CRM_Contact_SelectorTest extends CiviUnitTestCase { 'action' => CRM_Core_Action::NONE, 'includeContactIds' => NULL, 'searchDescendentGroups' => FALSE, + 'expected_query' => [], ), ), array( -- 2.25.1