Minor fixes and add unit tests
authorSeamus Lee <seamuslee001@gmail.com>
Sat, 27 Jan 2018 23:54:13 +0000 (10:54 +1100)
committerSeamus Lee <seamuslee001@gmail.com>
Sat, 27 Jan 2018 23:54:13 +0000 (10:54 +1100)
CRM/Contact/BAO/Query.php
tests/phpunit/CRM/Contact/BAO/QueryTest.php

index c52ab366d134165945363825ea851725e9315b5b..cc1f6147391782aeb32b3504448479b372c0b563 100644 (file)
@@ -4050,16 +4050,20 @@ WHERE  $smartGroupClause
       }
     }
 
-    $rTypeValues = $relTypes = array();
+    $rTypeValues = $relTypes = $relTypesIds = array();
     if (!empty($relationType)) {
       $relationType[2] = (array) $relationType[2];
       foreach ($relationType[2] as $relType) {
         $rel = explode('_', $relType);
         self::$_relType .= $rel[1];
         $params = array('id' => $rel[0]);
-        $relTypes[] = $rel[0];
         $typeValues = array();
-        $rTypeValues[] = CRM_Contact_BAO_RelationshipType::retrieve($params, $typeValues);
+        $rTypeValue = CRM_Contact_BAO_RelationshipType::retrieve($params, $typeValues);
+        if (!empty($rTypeValue)) {
+          $rTypeValues[] = $rTypeValue;
+          $relTypesIds[] = $rel[0];
+          $relTypes[] = $relType;
+        }
       }
     }
     if (!empty($rTypeValues)) {
@@ -4091,7 +4095,7 @@ WHERE  $smartGroupClause
     if ($nameClause || !$targetGroup) {
       if (!empty($relationType)) {
         $relQill = '';
-        foreach ($relationType[2] as $rel) {
+        foreach ($relTypes as $rel) {
           if (!empty($relQill)) {
             $relQill .= ' OR ';
           }
@@ -4131,7 +4135,7 @@ WHERE  $smartGroupClause
       }
       if (!empty($relationType)) {
         $relQill = '';
-        foreach ($relationType[2] as $rel) {
+        foreach ($relTypes as $rel) {
           if (!empty($relQill)) {
             $relQill .= ' OR ';
           }
@@ -4188,7 +4192,7 @@ civicrm_relationship.is_permission_a_b = 0
     $this->addRelationshipDateClauses($grouping, $where);
     $this->addRelationshipActivePeriodClauses($grouping, $where);
     if (!empty($relTypes)) {
-      $where[$grouping][] = 'civicrm_relationship.relationship_type_id IN (' . implode(',', $relTypes) . ')';
+      $where[$grouping][] = 'civicrm_relationship.relationship_type_id IN (' . implode(',', $relTypesIds) . ')';
     }
     $this->_tables['civicrm_relationship'] = $this->_whereTables['civicrm_relationship'] = 1;
     $this->_useDistinct = TRUE;
index 6c8061d240868b1000bee5e58c6053a7922828ad..c9dc80bebceff0a9b96ffec47fa441b8f8947861 100644 (file)
@@ -369,6 +369,63 @@ class CRM_Contact_BAO_QueryTest extends CiviUnitTestCase {
 
   }
 
+  /**
+   * Test Relationship Clause
+   */
+  public function testRelationshipClause() {
+    $today = date('Ymd');
+    $where1 = "WHERE  ( (
+civicrm_relationship.is_active = 1 AND
+( civicrm_relationship.end_date IS NULL OR civicrm_relationship.end_date >= {$today} ) AND
+( civicrm_relationship.start_date IS NULL OR civicrm_relationship.start_date <= {$today} )
+) AND (contact_b.is_deleted = 0) AND civicrm_relationship.relationship_type_id IN (8) )  AND (contact_a.is_deleted = 0)";
+    $where2 = "WHERE  ( (
+civicrm_relationship.is_active = 1 AND
+( civicrm_relationship.end_date IS NULL OR civicrm_relationship.end_date >= {$today} ) AND
+( civicrm_relationship.start_date IS NULL OR civicrm_relationship.start_date <= {$today} )
+) AND (contact_b.is_deleted = 0) AND civicrm_relationship.relationship_type_id IN (8,10) )  AND (contact_a.is_deleted = 0)";
+    // Test Traditional single select format
+    $params1 = array(array('relation_type_id', '=', '8_a_b', 0, 0));
+    $query1 = new CRM_Contact_BAO_Query(
+      $params1, array('contact_id'),
+      NULL, TRUE, FALSE, 1,
+      TRUE,
+      TRUE, FALSE
+    );
+    $sql1 = $query1->query(FALSE);
+    $this->assertEquals($where1, $sql1[2]);
+    // Test single relationship type selected in multiple select.
+    $params2 = array(array('relation_type_id', 'IN', array('8_a_b'), 0, 0));
+    $query2 = new CRM_Contact_BAO_Query(
+      $params2, array('contact_id'),
+      NULL, TRUE, FALSE, 1,
+      TRUE,
+      TRUE, FALSE
+    );
+    $sql2 = $query2->query(FALSE);
+    $this->assertEquals($where1, $sql2[2]);
+    // Test multiple relationship types selected.
+    $params3 = array(array('relation_type_id', 'IN', array('8_a_b', '10_a_b'), 0, 0));
+    $query3 = new CRM_Contact_BAO_Query(
+      $params3, array('contact_id'),
+      NULL, TRUE, FALSE, 1,
+      TRUE,
+      TRUE, FALSE
+    );
+    $sql3 = $query3->query(FALSE);
+    $this->assertEquals($where2, $sql3[2]);
+    // Test Multiple Relationship type selected where one doesn't actually exist.
+    $params4 = array(array('relation_type_id', 'IN', array('8_a_b', '10_a_b', '14_a_b'), 0, 0));
+    $query4 = new CRM_Contact_BAO_Query(
+      $params4, array('contact_id'),
+      NULL, TRUE, FALSE, 1,
+      TRUE,
+      TRUE, FALSE
+    );
+    $sql4 = $query4->query(FALSE);
+    $this->assertEquals($where2, $sql4[2]);
+  }
+
   /**
    * Test the group contact clause does not contain an OR.
    *