CRM-14263 - search profiles from search builder not working
authorEileen McNaughton <eileen@fuzion.co.nz>
Wed, 28 May 2014 03:59:55 +0000 (15:59 +1200)
committerEileen McNaughton <eileen@fuzion.co.nz>
Thu, 5 Jun 2014 01:46:05 +0000 (13:46 +1200)
CRM/Contact/BAO/Query.php
tests/phpunit/CRM/Contact/BAO/QueryTest.php
tests/phpunit/api/v3/ContactTest.php

index c643bcb6abbfda9ab9125781271750113bc77562..dbd973e26c6647139cd1c6baf399eabf0e2d9567 100644 (file)
@@ -2141,7 +2141,8 @@ class CRM_Contact_BAO_Query {
           $where = "`$tName`.$fldName";
 
           $this->_where[$grouping][] = self::buildClause("LOWER($where)", $op, $value);
-          $this->_whereTables[$tName] = $this->_tables[$tName];
+          // we set both _tables & whereTables because whereTables doesn't seem to do what the name implies it should
+          $this->_tables[$tName] = $this->_whereTables[$tName] = 1;
           $this->_qill[$grouping][] = "$field[title] $op '$value'";
         }
         else {
@@ -2212,7 +2213,7 @@ class CRM_Contact_BAO_Query {
           )
         )) {
         //fix for search by profile with address fields.
-        $tName = "{$locationType[$locType[1]]}-address";
+        $tName = "{$locationType[$locType[1]]}_address";
       }
       elseif ($locType[0] == 'on_hold') {
         $tName = "{$locationType[$locType[1]]}-email";
@@ -2284,6 +2285,9 @@ class CRM_Contact_BAO_Query {
   }
 
   /**
+   * Where tables is sometimes used to create the from clause, but, not reliably, set this AND set tables
+   * It's unclear the intent - there is a 'simpleFrom' clause which takes whereTables into account & a fromClause which doesn't
+   * logic may have eroded
    * @return array
    */
   function whereTables() {
@@ -2430,6 +2434,7 @@ class CRM_Contact_BAO_Query {
             $from .= " $side JOIN civicrm_address ON ( contact_a.id = civicrm_address.contact_id AND civicrm_address.is_primary = 1 )";
           }
           else {
+            //CRM-14263 further handling of address joins further down...
             $from .= " $side JOIN civicrm_address ON ( contact_a.id = civicrm_address.contact_id ) ";
           }
           continue;
@@ -2539,13 +2544,20 @@ class CRM_Contact_BAO_Query {
           continue;
 
         default:
-          $from .= CRM_Core_Component::from($name, $mode, $side);
+          if (strpos($name, '_address') != 0) {
+            //we have a join on an address table - possibly in conjunction with search builder - CRM-14263
+            $parts = explode('_', $name);
+            $locationID = array_search($parts[0], CRM_Core_BAO_Address::buildOptions('location_type_id', 'get', array('name' => $parts[0])));
+            $from .= " $side JOIN civicrm_address $name ON ( contact_a.id = {$name}.contact_id ) and location_type_id = $locationID ";
+          }
+          else {
+            $from .= CRM_Core_Component::from($name, $mode, $side);
+          }
           $from .= CRM_Contact_BAO_Query_Hook::singleton()->buildSearchfrom($name, $mode, $side);
 
           continue;
       }
     }
-
     return $from;
   }
 
index 878961b872da8a60b3fb92e68aada9f1cd0d4c4c..bead9220554e6003a6b76518cdf8fe4398a96d55 100644 (file)
@@ -70,5 +70,43 @@ class CRM_Contact_BAO_QueryTest extends CiviUnitTestCase {
 
     $this->assertEquals($ids, $contacts, 'In line ' . __LINE__);
   }
+
+  /**
+   * CRM-14263 search builder failure with search profile & address in criteria
+   */
+  function testSearchProfile()
+  {
+    $contactID = $this->individualCreate();
+    CRM_Core_Config::singleton()->defaultSearchProfileID = 1;
+    $this->callAPISuccess('address', 'create', array('contact_id' => $contactID, 'city' => 'Cool City', 'location_type_id' => 1,));
+    $params = array(
+      0 => array(
+        0 => 'city-1',
+        1 => '=',
+        2 => 'Cool City',
+        3 => 1,
+        4 => 0,
+      )
+    );
+    $returnProperties = array(
+      'contact_type' => 1,
+      'contact_sub_type' => 1,
+      'sort_name' => 1,
+    );
+
+    $queryObj = new CRM_Contact_BAO_Query($params, $returnProperties);
+    try {
+      $queryObj->searchQuery(0, 0, NULL,
+        FALSE, FALSE,
+        FALSE, FALSE,
+        FALSE);
+    }
+  catch (PEAR_Exception $e) {
+    $err = $e->getCause();
+    $this->fail('invalid SQL created' . $e->getMessage() . " " . $err->userinfo);
+
+  }
+
+}
 }
 
index 0fc15677c4c96e1409bc716164b92caf7dcb18a1..bdf8f60d9abd5bd8d07e0393c01c6d8ce7366eed 100644 (file)
@@ -1801,7 +1801,6 @@ class api_v3_ContactTest extends CiviUnitTestCase {
     $this->assertEquals($contacts['count'], CRM_Core_DAO::singleValueQuery('select count(*) FROM civicrm_contact WHERE legal_name IS NULL'));
   }
 
-  /**
   /**
    * CRM-14743 - test api respects search operators
    */
@@ -1822,7 +1821,6 @@ class api_v3_ContactTest extends CiviUnitTestCase {
     $this->assertEquals($contacts['count'], $preExistingContactCount);
   }
 
-  /**
   /**
    * CRM-14743 - test api respects search operators
    */
@@ -1842,4 +1840,15 @@ class api_v3_ContactTest extends CiviUnitTestCase {
     $contacts = $this->callAPISuccess('contact', 'get', array('created_date' => array('>' => '2014-01-01')));
     $this->assertEquals($contacts['count'], $preExistingContactCount);
   }
+
+  /**
+   * CRM-14263 check that API is not affected by search profile related bug
+   */
+  function testReturnCityProfile () {
+    $contactID = $this->individualCreate();
+    CRM_Core_Config::singleton()->defaultSearchProfileID = 1;
+    $this->callAPISuccess('address', 'create', array('contact_id' => $contactID, 'city' => 'Cool City', 'location_type_id' => 1,));
+    $result = $this->callAPISuccess('contact', 'get', array('city' => 'Cool City', 'return' => 'contact_type'));
+    $this->assertEquals(1, $result['count']);
+  }
 }