From 55eb4e2268cd9eb81c896f5b3cc3c123d08be3b5 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Wed, 28 May 2014 16:20:05 +1200 Subject: [PATCH] CRM-14263 test & fix search builder failure to retrieve address results --- CRM/Contact/BAO/Query.php | 4 +- tests/phpunit/CRM/Contact/BAO/QueryTest.php | 91 +++++++++++++++++++-- 2 files changed, 86 insertions(+), 9 deletions(-) diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index dbd973e26c..25b1aa86ad 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -4414,7 +4414,7 @@ civicrm_relationship.is_permission_a_b = 0 * * @param bool $skipOrderAndLimit * - * @return CRM_Contact_DAO_Contact + * @return CRM_Core_DAO * @access public */ function searchQuery( @@ -4477,7 +4477,7 @@ civicrm_relationship.is_permission_a_b = 0 } if (!empty($orderBy)) { // this is special case while searching for - // changelog CRM-1718 + // change log CRM-1718 if (preg_match('/sort_name/i', $orderBy)) { $orderBy = str_replace('sort_name', 'contact_a.sort_name', $orderBy); } diff --git a/tests/phpunit/CRM/Contact/BAO/QueryTest.php b/tests/phpunit/CRM/Contact/BAO/QueryTest.php index bead922055..85463182aa 100644 --- a/tests/phpunit/CRM/Contact/BAO/QueryTest.php +++ b/tests/phpunit/CRM/Contact/BAO/QueryTest.php @@ -72,10 +72,10 @@ class CRM_Contact_BAO_QueryTest extends CiviUnitTestCase { } /** + * Check that we get a successful result querying for home address * CRM-14263 search builder failure with search profile & address in criteria */ - function testSearchProfile() - { + function testSearchProfileHomeCityCRM14263() { $contactID = $this->individualCreate(); CRM_Core_Config::singleton()->defaultSearchProfileID = 1; $this->callAPISuccess('address', 'create', array('contact_id' => $contactID, 'city' => 'Cool City', 'location_type_id' => 1,)); @@ -96,17 +96,94 @@ class CRM_Contact_BAO_QueryTest extends CiviUnitTestCase { $queryObj = new CRM_Contact_BAO_Query($params, $returnProperties); try { - $queryObj->searchQuery(0, 0, NULL, + $resultDAO = $queryObj->searchQuery(0, 0, NULL, FALSE, FALSE, FALSE, FALSE, FALSE); + $this->assertTrue($resultDAO->fetch()); } - catch (PEAR_Exception $e) { - $err = $e->getCause(); - $this->fail('invalid SQL created' . $e->getMessage() . " " . $err->userinfo); + catch (PEAR_Exception $e) { + $err = $e->getCause(); + $this->fail('invalid SQL created' . $e->getMessage() . " " . $err->userinfo); + } } -} + /** + * Check that we get a successful result querying for home address + * CRM-14263 search builder failure with search profile & address in criteria + */ + function testSearchProfileHomeCityNoResultsCRM14263() { + $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 => 'Dumb 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 { + $resultDAO = $queryObj->searchQuery(0, 0, NULL, + FALSE, FALSE, + FALSE, FALSE, + FALSE); + $this->assertFalse($resultDAO->fetch()); + } + catch (PEAR_Exception $e) { + $err = $e->getCause(); + $this->fail('invalid SQL created' . $e->getMessage() . " " . $err->userinfo); + + } + } + /** + * CRM-14263 search builder failure with search profile & address in criteria + * We are retrieving primary here - checking the actual sql seems super prescriptive - but since the massive query object has + * so few tests detecting any change seems good here :-) + */ + function testSearchProfilePrimaryCityCRM14263() + { + $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 => '=', + 2 => 'Cool City', + 3 => 1, + 4 => 0, + ) + ); + $returnProperties = array( + 'contact_type' => 1, + 'contact_sub_type' => 1, + 'sort_name' => 1, + ); + $expectedSQL = "SELECT contact_a.id as contact_id, contact_a.contact_type as `contact_type`, contact_a.contact_sub_type as `contact_sub_type`, contact_a.sort_name as `sort_name`, civicrm_address.id as address_id, civicrm_address.city as `city` FROM civicrm_contact contact_a LEFT JOIN civicrm_address ON ( contact_a.id = civicrm_address.contact_id AND civicrm_address.is_primary = 1 ) WHERE ( ( LOWER(civicrm_address.city) = 'cool city' ) ) AND (contact_a.is_deleted = 0) ORDER BY contact_a.sort_name asc, contact_a.id "; + $queryObj = new CRM_Contact_BAO_Query($params, $returnProperties); + try { + $this->assertEquals($expectedSQL ,$queryObj->searchQuery(0, 0, NULL, + FALSE, FALSE, + FALSE, FALSE, + TRUE)); + } + catch (PEAR_Exception $e) { + $err = $e->getCause(); + $this->fail('invalid SQL created' . $e->getMessage() . " " . $err->userinfo); + + } + } + } -- 2.25.1