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
{
11 * @return CRM_Contact_BAO_QueryTestDataProvider
13 public function dataProvider() {
14 return new CRM_Contact_BAO_QueryTestDataProvider
;
22 $tablesToTruncate = array(
23 'civicrm_group_contact',
25 'civicrm_saved_search',
30 $this->quickCleanup($tablesToTruncate);
34 * Test CRM_Contact_BAO_Query::searchQuery()
35 * @dataProvider dataProvider
37 function testSearch($fv, $count, $ids, $full) {
38 $op = new PHPUnit_Extensions_Database_Operation_Insert();
39 $op->execute($this->_dbconn
,
40 $this->createFlatXMLDataSet(
41 dirname(__FILE__
) . '/queryDataset.xml'
45 $params = CRM_Contact_BAO_Query
::convertFormValues($fv);
46 $obj = new CRM_Contact_BAO_Query($params);
48 // let's set useGroupBy=true since we are listing contacts here who might belong to
49 // more than one group / tag / notes etc.
50 $obj->_useGroupBy
= TRUE;
52 $dao = $obj->searchQuery();
55 while ($dao->fetch()) {
56 $contacts[] = $dao->contact_id
;
59 sort($contacts, SORT_NUMERIC
);
61 $this->assertEquals($ids, $contacts, 'In line ' . __LINE__
);
65 * Check that we get a successful result querying for home address
66 * CRM-14263 search builder failure with search profile & address in criteria
68 function testSearchProfileHomeCityCRM14263() {
69 $contactID = $this->individualCreate();
70 CRM_Core_Config
::singleton()->defaultSearchProfileID
= 1;
71 $this->callAPISuccess('address', 'create', array('contact_id' => $contactID, 'city' => 'Cool City', 'location_type_id' => 1,));
81 $returnProperties = array(
83 'contact_sub_type' => 1,
87 $queryObj = new CRM_Contact_BAO_Query($params, $returnProperties);
89 $resultDAO = $queryObj->searchQuery(0, 0, NULL,
93 $this->assertTrue($resultDAO->fetch());
95 catch (PEAR_Exception
$e) {
96 $err = $e->getCause();
97 $this->fail('invalid SQL created' . $e->getMessage() . " " . $err->userinfo
);
103 * Check that we get a successful result querying for home address
104 * CRM-14263 search builder failure with search profile & address in criteria
106 function testSearchProfileHomeCityNoResultsCRM14263() {
107 $contactID = $this->individualCreate();
108 CRM_Core_Config
::singleton()->defaultSearchProfileID
= 1;
109 $this->callAPISuccess('address', 'create', array('contact_id' => $contactID, 'city' => 'Cool City', 'location_type_id' => 1,));
119 $returnProperties = array(
121 'contact_sub_type' => 1,
125 $queryObj = new CRM_Contact_BAO_Query($params, $returnProperties);
127 $resultDAO = $queryObj->searchQuery(0, 0, NULL,
131 $this->assertFalse($resultDAO->fetch());
133 catch (PEAR_Exception
$e) {
134 $err = $e->getCause();
135 $this->fail('invalid SQL created' . $e->getMessage() . " " . $err->userinfo
);
140 * CRM-14263 search builder failure with search profile & address in criteria
141 * We are retrieving primary here - checking the actual sql seems super prescriptive - but since the massive query object has
142 * so few tests detecting any change seems good here :-)
144 function testSearchProfilePrimaryCityCRM14263()
146 $contactID = $this->individualCreate();
147 CRM_Core_Config
::singleton()->defaultSearchProfileID
= 1;
148 $this->callAPISuccess('address', 'create', array('contact_id' => $contactID, 'city' => 'Cool City', 'location_type_id' => 1,));
158 $returnProperties = array(
160 'contact_sub_type' => 1,
163 $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 ";
164 $queryObj = new CRM_Contact_BAO_Query($params, $returnProperties);
166 $this->assertEquals($expectedSQL ,$queryObj->searchQuery(0, 0, NULL,
171 catch (PEAR_Exception
$e) {
172 $err = $e->getCause();
173 $this->fail('invalid SQL created' . $e->getMessage() . " " . $err->userinfo
);