2 require_once 'CiviTest/CiviUnitTestCase.php';
3 require_once 'CiviTest/Contact.php';
6 * Include dataProvider for tests
8 class CRM_Contact_BAO_QueryTest
extends CiviUnitTestCase
{
14 'name' => 'Contact BAO Query',
15 'description' => 'Test all Contact_BAO_Query methods.',
16 'group' => 'CiviCRM BAO Query Tests',
21 * @return CRM_Contact_BAO_QueryTestDataProvider
23 public function dataProvider() {
24 return new CRM_Contact_BAO_QueryTestDataProvider
;
32 $tablesToTruncate = array(
33 'civicrm_group_contact',
35 'civicrm_saved_search',
40 $this->quickCleanup($tablesToTruncate);
44 * Test CRM_Contact_BAO_Query::searchQuery()
45 * @dataProvider dataProvider
47 function testSearch($fv, $count, $ids, $full) {
48 $op = new PHPUnit_Extensions_Database_Operation_Insert();
49 $op->execute($this->_dbconn
,
50 $this->createFlatXMLDataSet(
51 dirname(__FILE__
) . '/queryDataset.xml'
55 $params = CRM_Contact_BAO_Query
::convertFormValues($fv);
56 $obj = new CRM_Contact_BAO_Query($params);
58 // let's set useGroupBy=true since we are listing contacts here who might belong to
59 // more than one group / tag / notes etc.
60 $obj->_useGroupBy
= TRUE;
62 $dao = $obj->searchQuery();
65 while ($dao->fetch()) {
66 $contacts[] = $dao->contact_id
;
69 sort($contacts, SORT_NUMERIC
);
71 $this->assertEquals($ids, $contacts, 'In line ' . __LINE__
);
75 * Check that we get a successful result querying for home address
76 * CRM-14263 search builder failure with search profile & address in criteria
78 function testSearchProfileHomeCityCRM14263() {
79 $contactID = $this->individualCreate();
80 CRM_Core_Config
::singleton()->defaultSearchProfileID
= 1;
81 $this->callAPISuccess('address', 'create', array('contact_id' => $contactID, 'city' => 'Cool City', 'location_type_id' => 1,));
91 $returnProperties = array(
93 'contact_sub_type' => 1,
97 $queryObj = new CRM_Contact_BAO_Query($params, $returnProperties);
99 $resultDAO = $queryObj->searchQuery(0, 0, NULL,
103 $this->assertTrue($resultDAO->fetch());
105 catch (PEAR_Exception
$e) {
106 $err = $e->getCause();
107 $this->fail('invalid SQL created' . $e->getMessage() . " " . $err->userinfo
);
113 * Check that we get a successful result querying for home address
114 * CRM-14263 search builder failure with search profile & address in criteria
116 function testSearchProfileHomeCityNoResultsCRM14263() {
117 $contactID = $this->individualCreate();
118 CRM_Core_Config
::singleton()->defaultSearchProfileID
= 1;
119 $this->callAPISuccess('address', 'create', array('contact_id' => $contactID, 'city' => 'Cool City', 'location_type_id' => 1,));
129 $returnProperties = array(
131 'contact_sub_type' => 1,
135 $queryObj = new CRM_Contact_BAO_Query($params, $returnProperties);
137 $resultDAO = $queryObj->searchQuery(0, 0, NULL,
141 $this->assertFalse($resultDAO->fetch());
143 catch (PEAR_Exception
$e) {
144 $err = $e->getCause();
145 $this->fail('invalid SQL created' . $e->getMessage() . " " . $err->userinfo
);
150 * CRM-14263 search builder failure with search profile & address in criteria
151 * We are retrieving primary here - checking the actual sql seems super prescriptive - but since the massive query object has
152 * so few tests detecting any change seems good here :-)
154 function testSearchProfilePrimaryCityCRM14263()
156 $contactID = $this->individualCreate();
157 CRM_Core_Config
::singleton()->defaultSearchProfileID
= 1;
158 $this->callAPISuccess('address', 'create', array('contact_id' => $contactID, 'city' => 'Cool City', 'location_type_id' => 1,));
168 $returnProperties = array(
170 'contact_sub_type' => 1,
173 $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 ";
174 $queryObj = new CRM_Contact_BAO_Query($params, $returnProperties);
176 $this->assertEquals($expectedSQL ,$queryObj->searchQuery(0, 0, NULL,
181 catch (PEAR_Exception
$e) {
182 $err = $e->getCause();
183 $this->fail('invalid SQL created' . $e->getMessage() . " " . $err->userinfo
);