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();
17 public function setUp() {
21 public function tearDown() {
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
41 public function testSearch($fv, $count, $ids, $full) {
42 $op = new PHPUnit_Extensions_Database_Operation_Insert();
43 $op->execute($this->_dbconn
,
44 $this->createFlatXMLDataSet(
45 dirname(__FILE__
) . '/queryDataset.xml'
49 $params = CRM_Contact_BAO_Query
::convertFormValues($fv);
50 $obj = new CRM_Contact_BAO_Query($params);
52 // let's set useGroupBy=true since we are listing contacts here who might belong to
53 // more than one group / tag / notes etc.
54 $obj->_useGroupBy
= TRUE;
56 $dao = $obj->searchQuery();
59 while ($dao->fetch()) {
60 $contacts[] = $dao->contact_id
;
63 sort($contacts, SORT_NUMERIC
);
65 $this->assertEquals($ids, $contacts, 'In line ' . __LINE__
);
69 * Check that we get a successful result querying for home address.
70 * CRM-14263 search builder failure with search profile & address in criteria
72 public function testSearchProfileHomeCityCRM14263() {
73 $contactID = $this->individualCreate();
74 CRM_Core_Config
::singleton()->defaultSearchProfileID
= 1;
75 $this->callAPISuccess('address', 'create', array(
76 'contact_id' => $contactID,
77 'city' => 'Cool City',
78 'location_type_id' => 1,
89 $returnProperties = array(
91 'contact_sub_type' => 1,
95 $queryObj = new CRM_Contact_BAO_Query($params, $returnProperties);
97 $resultDAO = $queryObj->searchQuery(0, 0, NULL,
101 $this->assertTrue($resultDAO->fetch());
103 catch (PEAR_Exception
$e) {
104 $err = $e->getCause();
105 $this->fail('invalid SQL created' . $e->getMessage() . " " . $err->userinfo
);
111 * Check that we get a successful result querying for home address.
112 * CRM-14263 search builder failure with search profile & address in criteria
114 public function testSearchProfileHomeCityNoResultsCRM14263() {
115 $contactID = $this->individualCreate();
116 CRM_Core_Config
::singleton()->defaultSearchProfileID
= 1;
117 $this->callAPISuccess('address', 'create', array(
118 'contact_id' => $contactID,
119 'city' => 'Cool City',
120 'location_type_id' => 1,
131 $returnProperties = array(
133 'contact_sub_type' => 1,
137 $queryObj = new CRM_Contact_BAO_Query($params, $returnProperties);
139 $resultDAO = $queryObj->searchQuery(0, 0, NULL,
143 $this->assertFalse($resultDAO->fetch());
145 catch (PEAR_Exception
$e) {
146 $err = $e->getCause();
147 $this->fail('invalid SQL created' . $e->getMessage() . " " . $err->userinfo
);
153 * CRM-14263 search builder failure with search profile & address in criteria
154 * We are retrieving primary here - checking the actual sql seems super prescriptive - but since the massive query object has
155 * so few tests detecting any change seems good here :-)
157 public function testSearchProfilePrimaryCityCRM14263() {
158 $contactID = $this->individualCreate();
159 CRM_Core_Config
::singleton()->defaultSearchProfileID
= 1;
160 $this->callAPISuccess('address', 'create', array(
161 'contact_id' => $contactID,
162 'city' => 'Cool City',
163 'location_type_id' => 1,
174 $returnProperties = array(
176 'contact_sub_type' => 1,
179 $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` ";
180 $queryObj = new CRM_Contact_BAO_Query($params, $returnProperties);
182 $this->assertEquals($expectedSQL, $queryObj->searchQuery(0, 0, NULL,
187 catch (PEAR_Exception
$e) {
188 $err = $e->getCause();
189 $this->fail('invalid SQL created' . $e->getMessage() . " " . $err->userinfo
);
195 * CRM-19562 ensure that only ids are used for contactid searching.
197 public function testContactIDClause() {
199 array("mark_x_2", "=", 1, 0, 0),
200 array("mark_x_foo@example.com", "=", 1, 0, 0),
202 $returnProperties = array(
209 "preferred_mail_format" => 1,
211 $numberofContacts = 2;
212 $query = new CRM_Contact_BAO_Query($params, $returnProperties);
214 $query->apiQuery($params, $returnProperties, NULL, NULL, 0, $numberofContacts);
216 catch (Exception
$e) {
217 $this->assertEquals("A fatal error was triggered: One of parameters (value: foo@example.com) is not of the type Positive",
219 return $this->assertTrue(TRUE);
221 return $this->fail('Test failed for some reason which is not good');