[REF] premlinary tidy up of code as tested - ready for actual fix
authoreileen <emcnaughton@wikimedia.org>
Thu, 2 May 2019 06:25:31 +0000 (18:25 +1200)
committereileen <emcnaughton@wikimedia.org>
Thu, 2 May 2019 06:33:05 +0000 (18:33 +1200)
CRM/Contact/BAO/Query.php
tests/phpunit/CRM/Contact/BAO/QueryTest.php

index e5f9b4c6048bb034e2715314bcacf8551eb933f9..b7f353ad7fdbcbc02f8b1f6acab5be008959f679 100644 (file)
@@ -3002,25 +3002,13 @@ class CRM_Contact_BAO_Query {
         $regularGroupIDs[] = trim($id);
       }
     }
+    $hasNonSmartGroups = count($regularGroupIDs);
 
     $isNotOp = ($op == 'NOT IN' || $op == '!=');
 
-    $statii = [];
-    $gcsValues = $this->getWhereValues('group_contact_status', $grouping);
-    if ($gcsValues &&
-      is_array($gcsValues[2])
-    ) {
-      foreach ($gcsValues[2] as $k => $v) {
-        if ($v) {
-          $statii[] = "'" . CRM_Utils_Type::escape($k, 'String') . "'";
-        }
-      }
-    }
-    else {
-      $statii[] = "'Added'";
-    }
+    $statusJoinClause = $this->getGroupStatusClause($grouping);
     $groupClause = [];
-    if (count($regularGroupIDs) || empty($value)) {
+    if ($hasNonSmartGroups || empty($value)) {
       // include child groups IDs if any
       $childGroupIds = (array) CRM_Contact_BAO_Group::getChildGroupIds($regularGroupIDs);
       foreach ($childGroupIds as $key => $id) {
@@ -3063,8 +3051,8 @@ class CRM_Contact_BAO_Query {
       }
       $groupClause[] = "( {$clause} )";
 
-      if ($statii) {
-        $joinClause[] = "{$gcTable}.status IN (" . implode(', ', $statii) . ")";
+      if ($statusJoinClause) {
+        $joinClause[] = "{$gcTable}.$statusJoinClause";
       }
       $this->_tables[$gcTable] = $this->_whereTables[$gcTable] = " LEFT JOIN civicrm_group_contact {$gcTable} ON (" . implode(' AND ', $joinClause) . ")";
     }
@@ -3101,7 +3089,7 @@ class CRM_Contact_BAO_Query {
     list($qillop, $qillVal) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Contact_DAO_Group', 'id', $value, $op);
     $this->_qill[$grouping][] = ts("Group(s) %1 %2", [1 => $qillop, 2 => $qillVal]);
     if (strpos($op, 'NULL') === FALSE) {
-      $this->_qill[$grouping][] = ts("Group Status %1", [1 => implode(' ' . ts('or') . ' ', $statii)]);
+      $this->_qill[$grouping][] = ts("Group Status %1", [1 => implode(' ' . ts('or') . ' ', $this->getSelectedGroupStatuses($grouping))]);
     }
   }
 
@@ -6974,4 +6962,41 @@ AND   displayRelType.is_active = 1
     ];
   }
 
+  /**
+   * Get the clause for group status.
+   *
+   * @param int $grouping
+   *
+   * @return string
+   */
+  protected function getGroupStatusClause($grouping) {
+    $statuses = $this->getSelectedGroupStatuses($grouping);
+    return "status IN (" . implode(', ', $statuses) . ")";
+  }
+
+  /**
+   * Get an array of the statuses that have been selected.
+   *
+   * @param string $grouping
+   *
+   * @return array
+   */
+  protected function getSelectedGroupStatuses($grouping) {
+    $statuses = [];
+    $gcsValues = $this->getWhereValues('group_contact_status', $grouping);
+    if ($gcsValues &&
+      is_array($gcsValues[2])
+    ) {
+      foreach ($gcsValues[2] as $k => $v) {
+        if ($v) {
+          $statuses[] = "'" . CRM_Utils_Type::escape($k, 'String') . "'";
+        }
+      }
+    }
+    else {
+      $statuses[] = "'Added'";
+    }
+    return $statuses;
+  }
+
 }
index 632e38979353cb403e2cf7ff05505b17fd6d928f..d3d42b94843a268d64fc39b799f7c9f9b310bca5 100644 (file)
@@ -629,7 +629,7 @@ civicrm_relationship.is_active = 1 AND
    */
   public function testGetByGroupWithStatus() {
     $groupID = $this->groupCreate();
-    $this->groupContactCreate($groupID , 3);
+    $this->groupContactCreate($groupID, 3);
     $groupContactID = $this->callAPISuccessGetSingle('GroupContact', ['group_id' => $groupID, 'options' => ['limit' => 1]])['id'];
     $this->callAPISuccess('GroupContact', 'create', ['id' => $groupContactID, 'status' => 'Removed']);
     $queryObj = new CRM_Contact_BAO_Query([['group', '=', $groupID, 0, 0], ['group_contact_status', 'IN', ['Removed' => 1], 0, 0]]);