Cast = to IN as tests show this PR otherwise breaks on resolving parent-child
authoreileen <emcnaughton@wikimedia.org>
Sun, 23 Jun 2019 19:03:34 +0000 (07:03 +1200)
committereileen <emcnaughton@wikimedia.org>
Mon, 24 Jun 2019 01:52:20 +0000 (13:52 +1200)
CRM/Contact/BAO/Query.php
tests/phpunit/CRM/Contact/BAO/GroupContactTest.php
tests/phpunit/CRM/Contact/BAO/QueryTest.php
tests/phpunit/CRM/Contact/SelectorTest.php

index 16520e629f9a615572d9dc0f7c51eec16c419817..11aa9f4996f3900dcd791ced6372f72952c57333 100644 (file)
@@ -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} )";
 
index 718a6232497d4f66cffe0b7713c654cacf03aeb4..8002a15b600eeca024f89c3ef4f13bb6799e0b76 100644 (file)
@@ -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);
 
index a33d6edd279856f7f83bdda322ce331d9fe9ae35..2303dd6e81ffed6d8b3ca8b0007309705b67f8f9 100644 (file)
@@ -36,6 +36,7 @@ class CRM_Contact_BAO_QueryTest extends CiviUnitTestCase {
       'civicrm_address',
     ];
     $this->quickCleanup($tablesToTruncate);
+    parent::tearDown();
   }
 
   /**
index f19e125342ecd64dc6e678ab806c95a2f40aa40b..c45ba19b5692e6e8c901c1f023927fae5fe1ff9e 100644 (file)
  */
 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(