From e5fccefbc19b32a4828e570e3324bd5e6eccdae3 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Wed, 28 May 2014 15:59:55 +1200 Subject: [PATCH] CRM-14263 - search profiles from search builder not working --- CRM/Contact/BAO/Query.php | 20 ++++++++--- tests/phpunit/CRM/Contact/BAO/QueryTest.php | 38 +++++++++++++++++++++ tests/phpunit/api/v3/ContactTest.php | 13 +++++-- 3 files changed, 65 insertions(+), 6 deletions(-) diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index c643bcb6ab..dbd973e26c 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -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; } diff --git a/tests/phpunit/CRM/Contact/BAO/QueryTest.php b/tests/phpunit/CRM/Contact/BAO/QueryTest.php index 878961b872..bead922055 100644 --- a/tests/phpunit/CRM/Contact/BAO/QueryTest.php +++ b/tests/phpunit/CRM/Contact/BAO/QueryTest.php @@ -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); + + } + +} } diff --git a/tests/phpunit/api/v3/ContactTest.php b/tests/phpunit/api/v3/ContactTest.php index 0fc15677c4..bdf8f60d9a 100644 --- a/tests/phpunit/api/v3/ContactTest.php +++ b/tests/phpunit/api/v3/ContactTest.php @@ -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']); + } } -- 2.25.1